blob: 866d7133cad8cf68d1de4f9432c7b67c4758c32f [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,
147 gva_t addr, u64 *spte, int level);
148};
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;
408
409 addr = gfn_to_hva(kvm, gfn);
410 if (kvm_is_error_hva(addr))
411 return 0;
412
413 vma = find_vma(current->mm, addr);
414 if (vma && is_vm_hugetlb_page(vma))
415 return 1;
416
417 return 0;
418}
419
420static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
421{
422 struct kvm_memory_slot *slot;
423
424 if (has_wrprotected_page(vcpu->kvm, large_gfn))
425 return 0;
426
427 if (!host_largepage_backed(vcpu->kvm, large_gfn))
428 return 0;
429
430 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
431 if (slot && slot->dirty_bitmap)
432 return 0;
433
434 return 1;
435}
436
437/*
Izik Eidus290fc382007-09-27 14:11:22 +0200438 * Take gfn and return the reverse mapping to it.
439 * Note: gfn must be unaliased before this function get called
440 */
441
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300442static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
Izik Eidus290fc382007-09-27 14:11:22 +0200443{
444 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300445 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200446
447 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300448 if (!lpage)
449 return &slot->rmap[gfn - slot->base_gfn];
450
451 idx = (gfn / KVM_PAGES_PER_HPAGE) -
452 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
453
454 return &slot->lpage_info[idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200455}
456
457/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800458 * Reverse mapping data structures:
459 *
Izik Eidus290fc382007-09-27 14:11:22 +0200460 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
461 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800462 *
Izik Eidus290fc382007-09-27 14:11:22 +0200463 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
464 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800465 */
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300466static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800467{
Avi Kivity4db35312007-11-21 15:28:32 +0200468 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800469 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200470 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800471 int i;
472
473 if (!is_rmap_pte(*spte))
474 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200475 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200476 sp = page_header(__pa(spte));
477 sp->gfns[spte - sp->spt] = gfn;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300478 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
Izik Eidus290fc382007-09-27 14:11:22 +0200479 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800480 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200481 *rmapp = (unsigned long)spte;
482 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800483 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800484 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200485 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800486 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200487 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800488 } else {
489 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200490 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800491 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
492 desc = desc->more;
493 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800494 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800495 desc = desc->more;
496 }
497 for (i = 0; desc->shadow_ptes[i]; ++i)
498 ;
499 desc->shadow_ptes[i] = spte;
500 }
501}
502
Izik Eidus290fc382007-09-27 14:11:22 +0200503static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800504 struct kvm_rmap_desc *desc,
505 int i,
506 struct kvm_rmap_desc *prev_desc)
507{
508 int j;
509
510 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
511 ;
512 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000513 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800514 if (j != 0)
515 return;
516 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200517 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800518 else
519 if (prev_desc)
520 prev_desc->more = desc->more;
521 else
Izik Eidus290fc382007-09-27 14:11:22 +0200522 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300523 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800524}
525
Izik Eidus290fc382007-09-27 14:11:22 +0200526static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800527{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800528 struct kvm_rmap_desc *desc;
529 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200530 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500531 pfn_t pfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200532 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800533 int i;
534
535 if (!is_rmap_pte(*spte))
536 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200537 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500538 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800539 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500540 kvm_set_pfn_accessed(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200541 if (is_writeble_pte(*spte))
Anthony Liguori35149e22008-04-02 14:46:56 -0500542 kvm_release_pfn_dirty(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200543 else
Anthony Liguori35149e22008-04-02 14:46:56 -0500544 kvm_release_pfn_clean(pfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300545 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
Izik Eidus290fc382007-09-27 14:11:22 +0200546 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800547 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
548 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200549 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800550 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200551 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800552 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
553 spte, *spte);
554 BUG();
555 }
Izik Eidus290fc382007-09-27 14:11:22 +0200556 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800557 } else {
558 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200559 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800560 prev_desc = NULL;
561 while (desc) {
562 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
563 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200564 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800565 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800566 prev_desc);
567 return;
568 }
569 prev_desc = desc;
570 desc = desc->more;
571 }
572 BUG();
573 }
574}
575
Izik Eidus98348e92007-10-16 14:42:30 +0200576static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800577{
Avi Kivity374cbac2007-01-05 16:36:43 -0800578 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200579 struct kvm_rmap_desc *prev_desc;
580 u64 *prev_spte;
581 int i;
582
583 if (!*rmapp)
584 return NULL;
585 else if (!(*rmapp & 1)) {
586 if (!spte)
587 return (u64 *)*rmapp;
588 return NULL;
589 }
590 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
591 prev_desc = NULL;
592 prev_spte = NULL;
593 while (desc) {
594 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
595 if (prev_spte == spte)
596 return desc->shadow_ptes[i];
597 prev_spte = desc->shadow_ptes[i];
598 }
599 desc = desc->more;
600 }
601 return NULL;
602}
603
604static void rmap_write_protect(struct kvm *kvm, u64 gfn)
605{
Izik Eidus290fc382007-09-27 14:11:22 +0200606 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800607 u64 *spte;
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800608 int write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800609
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500610 gfn = unalias_gfn(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300611 rmapp = gfn_to_rmap(kvm, gfn, 0);
Avi Kivity374cbac2007-01-05 16:36:43 -0800612
Izik Eidus98348e92007-10-16 14:42:30 +0200613 spte = rmap_next(kvm, rmapp, NULL);
614 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800615 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800616 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800617 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800618 if (is_writeble_pte(*spte)) {
Izik Eidus9647c142007-10-16 14:43:46 +0200619 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800620 write_protected = 1;
621 }
Izik Eidus9647c142007-10-16 14:43:46 +0200622 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800623 }
Izik Eidus855149a2008-03-20 18:17:24 +0200624 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500625 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200626
627 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500628 pfn = spte_to_pfn(*spte);
629 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200630 }
631
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300632 /* check for huge page mappings */
633 rmapp = gfn_to_rmap(kvm, gfn, 1);
634 spte = rmap_next(kvm, rmapp, NULL);
635 while (spte) {
636 BUG_ON(!spte);
637 BUG_ON(!(*spte & PT_PRESENT_MASK));
638 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
639 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
640 if (is_writeble_pte(*spte)) {
641 rmap_remove(kvm, spte);
642 --kvm->stat.lpages;
643 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti6597ca02008-06-08 01:48:53 -0300644 spte = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300645 write_protected = 1;
646 }
647 spte = rmap_next(kvm, rmapp, spte);
648 }
649
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800650 if (write_protected)
651 kvm_flush_remote_tlbs(kvm);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300652
653 account_shadowed(kvm, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800654}
655
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200656static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
657{
658 u64 *spte;
659 int need_tlb_flush = 0;
660
661 while ((spte = rmap_next(kvm, rmapp, NULL))) {
662 BUG_ON(!(*spte & PT_PRESENT_MASK));
663 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
664 rmap_remove(kvm, spte);
665 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
666 need_tlb_flush = 1;
667 }
668 return need_tlb_flush;
669}
670
671static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
672 int (*handler)(struct kvm *kvm, unsigned long *rmapp))
673{
674 int i;
675 int retval = 0;
676
677 /*
678 * If mmap_sem isn't taken, we can look the memslots with only
679 * the mmu_lock by skipping over the slots with userspace_addr == 0.
680 */
681 for (i = 0; i < kvm->nmemslots; i++) {
682 struct kvm_memory_slot *memslot = &kvm->memslots[i];
683 unsigned long start = memslot->userspace_addr;
684 unsigned long end;
685
686 /* mmu_lock protects userspace_addr */
687 if (!start)
688 continue;
689
690 end = start + (memslot->npages << PAGE_SHIFT);
691 if (hva >= start && hva < end) {
692 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
693 retval |= handler(kvm, &memslot->rmap[gfn_offset]);
694 retval |= handler(kvm,
695 &memslot->lpage_info[
696 gfn_offset /
697 KVM_PAGES_PER_HPAGE].rmap_pde);
698 }
699 }
700
701 return retval;
702}
703
704int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
705{
706 return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
707}
708
709static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
710{
711 u64 *spte;
712 int young = 0;
713
Sheng Yang534e38b2008-09-08 15:12:30 +0800714 /* always return old for EPT */
715 if (!shadow_accessed_mask)
716 return 0;
717
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200718 spte = rmap_next(kvm, rmapp, NULL);
719 while (spte) {
720 int _young;
721 u64 _spte = *spte;
722 BUG_ON(!(_spte & PT_PRESENT_MASK));
723 _young = _spte & PT_ACCESSED_MASK;
724 if (_young) {
725 young = 1;
726 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
727 }
728 spte = rmap_next(kvm, rmapp, spte);
729 }
730 return young;
731}
732
733int kvm_age_hva(struct kvm *kvm, unsigned long hva)
734{
735 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
736}
737
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800738#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300739static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800740{
Avi Kivity139bdb22007-01-05 16:36:50 -0800741 u64 *pos;
742 u64 *end;
743
Avi Kivity47ad8e62007-05-06 15:50:58 +0300744 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300745 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800746 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800747 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800748 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800749 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800750 return 1;
751}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800752#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800753
Avi Kivity4db35312007-11-21 15:28:32 +0200754static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800755{
Avi Kivity4db35312007-11-21 15:28:32 +0200756 ASSERT(is_empty_shadow_page(sp->spt));
757 list_del(&sp->link);
758 __free_page(virt_to_page(sp->spt));
759 __free_page(virt_to_page(sp->gfns));
760 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800761 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800762}
763
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800764static unsigned kvm_page_table_hashfn(gfn_t gfn)
765{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200766 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800767}
768
Avi Kivity25c0de22007-01-05 16:36:42 -0800769static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
770 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800771{
Avi Kivity4db35312007-11-21 15:28:32 +0200772 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800773
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800774 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
775 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
776 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200777 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800778 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Avi Kivity4db35312007-11-21 15:28:32 +0200779 ASSERT(is_empty_shadow_page(sp->spt));
780 sp->slot_bitmap = 0;
781 sp->multimapped = 0;
782 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800783 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200784 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800785}
786
Avi Kivity714b93d2007-01-05 16:36:53 -0800787static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200788 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800789{
790 struct kvm_pte_chain *pte_chain;
791 struct hlist_node *node;
792 int i;
793
794 if (!parent_pte)
795 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200796 if (!sp->multimapped) {
797 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800798
799 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200800 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800801 return;
802 }
Avi Kivity4db35312007-11-21 15:28:32 +0200803 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800804 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200805 INIT_HLIST_HEAD(&sp->parent_ptes);
806 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800807 pte_chain->parent_ptes[0] = old;
808 }
Avi Kivity4db35312007-11-21 15:28:32 +0200809 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800810 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
811 continue;
812 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
813 if (!pte_chain->parent_ptes[i]) {
814 pte_chain->parent_ptes[i] = parent_pte;
815 return;
816 }
817 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800818 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800819 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200820 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800821 pte_chain->parent_ptes[0] = parent_pte;
822}
823
Avi Kivity4db35312007-11-21 15:28:32 +0200824static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800825 u64 *parent_pte)
826{
827 struct kvm_pte_chain *pte_chain;
828 struct hlist_node *node;
829 int i;
830
Avi Kivity4db35312007-11-21 15:28:32 +0200831 if (!sp->multimapped) {
832 BUG_ON(sp->parent_pte != parent_pte);
833 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800834 return;
835 }
Avi Kivity4db35312007-11-21 15:28:32 +0200836 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800837 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
838 if (!pte_chain->parent_ptes[i])
839 break;
840 if (pte_chain->parent_ptes[i] != parent_pte)
841 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800842 while (i + 1 < NR_PTE_CHAIN_ENTRIES
843 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800844 pte_chain->parent_ptes[i]
845 = pte_chain->parent_ptes[i + 1];
846 ++i;
847 }
848 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800849 if (i == 0) {
850 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300851 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200852 if (hlist_empty(&sp->parent_ptes)) {
853 sp->multimapped = 0;
854 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800855 }
856 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800857 return;
858 }
859 BUG();
860}
861
Avi Kivityd761a502008-05-29 14:55:03 +0300862static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
863 struct kvm_mmu_page *sp)
864{
865 int i;
866
867 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
868 sp->spt[i] = shadow_trap_nonpresent_pte;
869}
870
Avi Kivity4db35312007-11-21 15:28:32 +0200871static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800872{
873 unsigned index;
874 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200875 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800876 struct hlist_node *node;
877
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800878 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200879 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800880 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200881 hlist_for_each_entry(sp, node, bucket, hash_link)
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500882 if (sp->gfn == gfn && !sp->role.metaphysical
883 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800884 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800885 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +0200886 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800887 }
888 return NULL;
889}
890
891static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
892 gfn_t gfn,
893 gva_t gaddr,
894 unsigned level,
895 int metaphysical,
Avi Kivity41074d02007-12-09 17:00:02 +0200896 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +0200897 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800898{
899 union kvm_mmu_page_role role;
900 unsigned index;
901 unsigned quadrant;
902 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200903 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800904 struct hlist_node *node;
905
906 role.word = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800907 role.glevels = vcpu->arch.mmu.root_level;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800908 role.level = level;
909 role.metaphysical = metaphysical;
Avi Kivity41074d02007-12-09 17:00:02 +0200910 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800911 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800912 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
913 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
914 role.quadrant = quadrant;
915 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800916 pgprintk("%s: looking gfn %lx role %x\n", __func__,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800917 gfn, role.word);
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200918 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800919 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200920 hlist_for_each_entry(sp, node, bucket, hash_link)
921 if (sp->gfn == gfn && sp->role.word == role.word) {
922 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800923 pgprintk("%s: found\n", __func__);
Avi Kivity4db35312007-11-21 15:28:32 +0200924 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800925 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200926 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +0200927 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
928 if (!sp)
929 return sp;
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800930 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +0200931 sp->gfn = gfn;
932 sp->role = role;
933 hlist_add_head(&sp->hash_link, bucket);
Avi Kivity374cbac2007-01-05 16:36:43 -0800934 if (!metaphysical)
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500935 rmap_write_protect(vcpu->kvm, gfn);
Avi Kivity131d8272008-05-29 14:56:28 +0300936 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
937 vcpu->arch.mmu.prefetch_page(vcpu, sp);
938 else
939 nonpaging_prefetch_page(vcpu, sp);
Avi Kivity4db35312007-11-21 15:28:32 +0200940 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800941}
942
Avi Kivity3d000db2008-08-22 19:24:38 +0300943static int walk_shadow(struct kvm_shadow_walk *walker,
944 struct kvm_vcpu *vcpu, gva_t addr)
945{
946 hpa_t shadow_addr;
947 int level;
948 int r;
949 u64 *sptep;
950 unsigned index;
951
952 shadow_addr = vcpu->arch.mmu.root_hpa;
953 level = vcpu->arch.mmu.shadow_root_level;
954 if (level == PT32E_ROOT_LEVEL) {
955 shadow_addr = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
956 shadow_addr &= PT64_BASE_ADDR_MASK;
957 --level;
958 }
959
960 while (level >= PT_PAGE_TABLE_LEVEL) {
961 index = SHADOW_PT_INDEX(addr, level);
962 sptep = ((u64 *)__va(shadow_addr)) + index;
963 r = walker->entry(walker, vcpu, addr, sptep, level);
964 if (r)
965 return r;
966 shadow_addr = *sptep & PT64_BASE_ADDR_MASK;
967 --level;
968 }
969 return 0;
970}
971
Avi Kivity90cb0522007-07-17 13:04:56 +0300972static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +0200973 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -0800974{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800975 unsigned i;
976 u64 *pt;
977 u64 ent;
978
Avi Kivity4db35312007-11-21 15:28:32 +0200979 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800980
Avi Kivity4db35312007-11-21 15:28:32 +0200981 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800982 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +0200983 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +0200984 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +0200985 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800986 }
987 return;
988 }
989
990 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
991 ent = pt[i];
992
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300993 if (is_shadow_present_pte(ent)) {
994 if (!is_large_pte(ent)) {
995 ent &= PT64_BASE_ADDR_MASK;
996 mmu_page_remove_parent_pte(page_header(ent),
997 &pt[i]);
998 } else {
999 --kvm->stat.lpages;
1000 rmap_remove(kvm, &pt[i]);
1001 }
1002 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001003 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001004 }
Avi Kivitya4360362007-01-05 16:36:45 -08001005}
1006
Avi Kivity4db35312007-11-21 15:28:32 +02001007static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001008{
Avi Kivity4db35312007-11-21 15:28:32 +02001009 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001010}
1011
Avi Kivity12b7d282007-09-23 14:10:49 +02001012static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1013{
1014 int i;
1015
1016 for (i = 0; i < KVM_MAX_VCPUS; ++i)
1017 if (kvm->vcpus[i])
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001018 kvm->vcpus[i]->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001019}
1020
Avi Kivity31aa2b42008-07-11 17:59:46 +03001021static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001022{
1023 u64 *parent_pte;
1024
Avi Kivity4db35312007-11-21 15:28:32 +02001025 while (sp->multimapped || sp->parent_pte) {
1026 if (!sp->multimapped)
1027 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001028 else {
1029 struct kvm_pte_chain *chain;
1030
Avi Kivity4db35312007-11-21 15:28:32 +02001031 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001032 struct kvm_pte_chain, link);
1033 parent_pte = chain->parent_ptes[0];
1034 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001035 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001036 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +02001037 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001038 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001039}
1040
1041static void kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1042{
1043 ++kvm->stat.mmu_shadow_zapped;
Avi Kivity4db35312007-11-21 15:28:32 +02001044 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001045 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001046 kvm_flush_remote_tlbs(kvm);
1047 if (!sp->role.invalid && !sp->role.metaphysical)
1048 unaccount_shadowed(kvm, sp->gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001049 if (!sp->root_count) {
1050 hlist_del(&sp->hash_link);
1051 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001052 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001053 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001054 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001055 kvm_reload_remote_mmus(kvm);
1056 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001057 kvm_mmu_reset_last_pte_updated(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -08001058}
1059
Izik Eidus82ce2c92007-10-02 18:52:55 +02001060/*
1061 * Changing the number of mmu pages allocated to the vm
1062 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1063 */
1064void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1065{
1066 /*
1067 * If we set the number of mmu pages to be smaller be than the
1068 * number of actived pages , we must to free some mmu pages before we
1069 * change the value
1070 */
1071
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001072 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
Izik Eidus82ce2c92007-10-02 18:52:55 +02001073 kvm_nr_mmu_pages) {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001074 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
1075 - kvm->arch.n_free_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001076
1077 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
1078 struct kvm_mmu_page *page;
1079
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001080 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001081 struct kvm_mmu_page, link);
1082 kvm_mmu_zap_page(kvm, page);
1083 n_used_mmu_pages--;
1084 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001085 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001086 }
1087 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001088 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1089 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001090
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001091 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001092}
1093
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001094static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001095{
1096 unsigned index;
1097 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001098 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001099 struct hlist_node *node, *n;
1100 int r;
1101
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001102 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001103 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001104 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001105 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001106 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
1107 if (sp->gfn == gfn && !sp->role.metaphysical) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001108 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001109 sp->role.word);
1110 kvm_mmu_zap_page(kvm, sp);
Avi Kivitya4360362007-01-05 16:36:45 -08001111 r = 1;
1112 }
1113 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001114}
1115
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001116static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001117{
Avi Kivity4db35312007-11-21 15:28:32 +02001118 struct kvm_mmu_page *sp;
Avi Kivity97a0a012007-05-31 15:08:29 +03001119
Avi Kivity4db35312007-11-21 15:28:32 +02001120 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001121 pgprintk("%s: zap %lx %x\n", __func__, gfn, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001122 kvm_mmu_zap_page(kvm, sp);
Avi Kivity97a0a012007-05-31 15:08:29 +03001123 }
1124}
1125
Avi Kivity38c335f2007-11-21 14:20:22 +02001126static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001127{
Avi Kivity38c335f2007-11-21 14:20:22 +02001128 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001129 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001130
Avi Kivity4db35312007-11-21 15:28:32 +02001131 __set_bit(slot, &sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001132}
1133
Avi Kivity039576c2007-03-20 12:46:50 +02001134struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1135{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001136 struct page *page;
1137
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001138 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001139
1140 if (gpa == UNMAPPED_GVA)
1141 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001142
1143 down_read(&current->mm->mmap_sem);
1144 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1145 up_read(&current->mm->mmap_sem);
1146
1147 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001148}
1149
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001150static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1151 unsigned pt_access, unsigned pte_access,
1152 int user_fault, int write_fault, int dirty,
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001153 int *ptwrite, int largepage, gfn_t gfn,
Anthony Liguori35149e22008-04-02 14:46:56 -05001154 pfn_t pfn, bool speculative)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001155{
1156 u64 spte;
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001157 int was_rmapped = 0;
Izik Eidus75e68e62008-01-12 23:49:09 +02001158 int was_writeble = is_writeble_pte(*shadow_pte);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001159
Avi Kivitybc750ba2007-12-09 18:39:41 +02001160 pgprintk("%s: spte %llx access %x write_fault %d"
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001161 " user_fault %d gfn %lx\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001162 __func__, *shadow_pte, pt_access,
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001163 write_fault, user_fault, gfn);
1164
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001165 if (is_rmap_pte(*shadow_pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001166 /*
1167 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1168 * the parent of the now unreachable PTE.
1169 */
1170 if (largepage && !is_large_pte(*shadow_pte)) {
1171 struct kvm_mmu_page *child;
1172 u64 pte = *shadow_pte;
1173
1174 child = page_header(pte & PT64_BASE_ADDR_MASK);
1175 mmu_page_remove_parent_pte(child, shadow_pte);
Anthony Liguori35149e22008-04-02 14:46:56 -05001176 } else if (pfn != spte_to_pfn(*shadow_pte)) {
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001177 pgprintk("hfn old %lx new %lx\n",
Anthony Liguori35149e22008-04-02 14:46:56 -05001178 spte_to_pfn(*shadow_pte), pfn);
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001179 rmap_remove(vcpu->kvm, shadow_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001180 } else {
1181 if (largepage)
1182 was_rmapped = is_large_pte(*shadow_pte);
1183 else
1184 was_rmapped = 1;
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001185 }
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001186 }
1187
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001188 /*
1189 * We don't set the accessed bit, since we sometimes want to see
1190 * whether the guest actually used the pte (in order to detect
1191 * demand paging).
1192 */
Sheng Yang7b523452008-04-25 21:13:50 +08001193 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001194 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001195 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001196 if (!dirty)
1197 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001198 if (pte_access & ACC_EXEC_MASK)
1199 spte |= shadow_x_mask;
1200 else
1201 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001202 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001203 spte |= shadow_user_mask;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001204 if (largepage)
1205 spte |= PT_PAGE_SIZE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001206
Anthony Liguori35149e22008-04-02 14:46:56 -05001207 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001208
1209 if ((pte_access & ACC_WRITE_MASK)
1210 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
1211 struct kvm_mmu_page *shadow;
1212
1213 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001214
1215 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001216 if (shadow ||
1217 (largepage && has_wrprotected_page(vcpu->kvm, gfn))) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001218 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001219 __func__, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001220 pte_access &= ~ACC_WRITE_MASK;
1221 if (is_writeble_pte(spte)) {
1222 spte &= ~PT_WRITABLE_MASK;
1223 kvm_x86_ops->tlb_flush(vcpu);
1224 }
1225 if (write_fault)
1226 *ptwrite = 1;
1227 }
1228 }
1229
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001230 if (pte_access & ACC_WRITE_MASK)
1231 mark_page_dirty(vcpu->kvm, gfn);
1232
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001233 pgprintk("%s: setting spte %llx\n", __func__, spte);
Avi Kivitydb475c32008-06-22 16:46:22 +03001234 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001235 (spte&PT_PAGE_SIZE_MASK)? "2MB" : "4kB",
1236 (spte&PT_WRITABLE_MASK)?"RW":"R", gfn, spte, shadow_pte);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001237 set_shadow_pte(shadow_pte, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001238 if (!was_rmapped && (spte & PT_PAGE_SIZE_MASK)
1239 && (spte & PT_PRESENT_MASK))
1240 ++vcpu->kvm->stat.lpages;
1241
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001242 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1243 if (!was_rmapped) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001244 rmap_add(vcpu, shadow_pte, gfn, largepage);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001245 if (!is_rmap_pte(*shadow_pte))
Anthony Liguori35149e22008-04-02 14:46:56 -05001246 kvm_release_pfn_clean(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001247 } else {
1248 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001249 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001250 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001251 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001252 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001253 if (speculative) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001254 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001255 vcpu->arch.last_pte_gfn = gfn;
1256 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001257}
1258
Avi Kivity6aa8b732006-12-10 02:21:36 -08001259static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1260{
1261}
1262
Avi Kivity140754b2008-08-22 19:28:04 +03001263struct direct_shadow_walk {
1264 struct kvm_shadow_walk walker;
1265 pfn_t pfn;
1266 int write;
1267 int largepage;
1268 int pt_write;
1269};
1270
1271static int direct_map_entry(struct kvm_shadow_walk *_walk,
1272 struct kvm_vcpu *vcpu,
1273 gva_t addr, u64 *sptep, int level)
1274{
1275 struct direct_shadow_walk *walk =
1276 container_of(_walk, struct direct_shadow_walk, walker);
1277 struct kvm_mmu_page *sp;
1278 gfn_t pseudo_gfn;
1279 gfn_t gfn = addr >> PAGE_SHIFT;
1280
1281 if (level == PT_PAGE_TABLE_LEVEL
1282 || (walk->largepage && level == PT_DIRECTORY_LEVEL)) {
1283 mmu_set_spte(vcpu, sptep, ACC_ALL, ACC_ALL,
1284 0, walk->write, 1, &walk->pt_write,
1285 walk->largepage, gfn, walk->pfn, false);
Avi Kivitybc2d4292008-08-27 16:30:56 +03001286 ++vcpu->stat.pf_fixed;
Avi Kivity140754b2008-08-22 19:28:04 +03001287 return 1;
1288 }
1289
1290 if (*sptep == shadow_trap_nonpresent_pte) {
1291 pseudo_gfn = (addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1292 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, addr, level - 1,
1293 1, ACC_ALL, sptep);
1294 if (!sp) {
1295 pgprintk("nonpaging_map: ENOMEM\n");
1296 kvm_release_pfn_clean(walk->pfn);
1297 return -ENOMEM;
1298 }
1299
1300 set_shadow_pte(sptep,
1301 __pa(sp->spt)
1302 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1303 | shadow_user_mask | shadow_x_mask);
1304 }
1305 return 0;
1306}
1307
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001308static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Avi Kivity6c41f422008-08-26 16:16:08 +03001309 int largepage, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310{
Avi Kivity140754b2008-08-22 19:28:04 +03001311 int r;
1312 struct direct_shadow_walk walker = {
1313 .walker = { .entry = direct_map_entry, },
1314 .pfn = pfn,
1315 .largepage = largepage,
1316 .write = write,
1317 .pt_write = 0,
1318 };
Avi Kivity6aa8b732006-12-10 02:21:36 -08001319
Avi Kivity140754b2008-08-22 19:28:04 +03001320 r = walk_shadow(&walker.walker, vcpu, (gva_t)gfn << PAGE_SHIFT);
1321 if (r < 0)
1322 return r;
1323 return walker.pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001324}
1325
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001326static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1327{
1328 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001329 int largepage = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001330 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001331 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001332
1333 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001334 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1335 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1336 largepage = 1;
1337 }
1338
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001339 mmu_seq = vcpu->kvm->mmu_notifier_seq;
1340 /* implicit mb(), we'll read before PT lock is unlocked */
Anthony Liguori35149e22008-04-02 14:46:56 -05001341 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001342 up_read(&current->mm->mmap_sem);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001343
Avi Kivityd196e342008-01-24 11:44:11 +02001344 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001345 if (is_error_pfn(pfn)) {
1346 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001347 return 1;
1348 }
1349
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001350 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001351 if (mmu_notifier_retry(vcpu, mmu_seq))
1352 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02001353 kvm_mmu_free_some_pages(vcpu);
Avi Kivity6c41f422008-08-26 16:16:08 +03001354 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001355 spin_unlock(&vcpu->kvm->mmu_lock);
1356
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001357
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001358 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001359
1360out_unlock:
1361 spin_unlock(&vcpu->kvm->mmu_lock);
1362 kvm_release_pfn_clean(pfn);
1363 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001364}
1365
1366
Avi Kivity17ac10a2007-01-05 16:36:40 -08001367static void mmu_free_roots(struct kvm_vcpu *vcpu)
1368{
1369 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001370 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001371
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001372 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001373 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001374 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001375 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1376 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001377
Avi Kivity4db35312007-11-21 15:28:32 +02001378 sp = page_header(root);
1379 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001380 if (!sp->root_count && sp->role.invalid)
1381 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001382 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001383 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001384 return;
1385 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001386 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001387 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001388
Avi Kivity417726a2007-04-12 17:35:58 +03001389 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001390 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001391 sp = page_header(root);
1392 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001393 if (!sp->root_count && sp->role.invalid)
1394 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001395 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001396 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001397 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001398 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001399 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001400}
1401
1402static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1403{
1404 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001405 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001406 struct kvm_mmu_page *sp;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001407 int metaphysical = 0;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001408
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001409 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001410
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001411 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1412 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001413
1414 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01001415 if (tdp_enabled)
1416 metaphysical = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001417 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001418 PT64_ROOT_LEVEL, metaphysical,
1419 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001420 root = __pa(sp->spt);
1421 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001422 vcpu->arch.mmu.root_hpa = root;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001423 return;
1424 }
Joerg Roedelfb72d162008-02-07 13:47:44 +01001425 metaphysical = !is_paging(vcpu);
1426 if (tdp_enabled)
1427 metaphysical = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001428 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001429 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001430
1431 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001432 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1433 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1434 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001435 continue;
1436 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001437 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1438 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001439 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001440 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001441 PT32_ROOT_LEVEL, metaphysical,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001442 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001443 root = __pa(sp->spt);
1444 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001445 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001446 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001447 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001448}
1449
Avi Kivity6aa8b732006-12-10 02:21:36 -08001450static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1451{
1452 return vaddr;
1453}
1454
1455static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02001456 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001457{
Avi Kivitye8332402007-12-09 18:43:00 +02001458 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08001459 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001460
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001461 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08001462 r = mmu_topup_memory_caches(vcpu);
1463 if (r)
1464 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08001465
Avi Kivity6aa8b732006-12-10 02:21:36 -08001466 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001467 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001468
Avi Kivitye8332402007-12-09 18:43:00 +02001469 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001470
Avi Kivitye8332402007-12-09 18:43:00 +02001471 return nonpaging_map(vcpu, gva & PAGE_MASK,
1472 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001473}
1474
Joerg Roedelfb72d162008-02-07 13:47:44 +01001475static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
1476 u32 error_code)
1477{
Anthony Liguori35149e22008-04-02 14:46:56 -05001478 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001479 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001480 int largepage = 0;
1481 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001482 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001483
1484 ASSERT(vcpu);
1485 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
1486
1487 r = mmu_topup_memory_caches(vcpu);
1488 if (r)
1489 return r;
1490
1491 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001492 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1493 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1494 largepage = 1;
1495 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001496 mmu_seq = vcpu->kvm->mmu_notifier_seq;
1497 /* implicit mb(), we'll read before PT lock is unlocked */
Anthony Liguori35149e22008-04-02 14:46:56 -05001498 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03001499 up_read(&current->mm->mmap_sem);
Anthony Liguori35149e22008-04-02 14:46:56 -05001500 if (is_error_pfn(pfn)) {
1501 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001502 return 1;
1503 }
1504 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001505 if (mmu_notifier_retry(vcpu, mmu_seq))
1506 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001507 kvm_mmu_free_some_pages(vcpu);
1508 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Avi Kivity6c41f422008-08-26 16:16:08 +03001509 largepage, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001510 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001511
1512 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001513
1514out_unlock:
1515 spin_unlock(&vcpu->kvm->mmu_lock);
1516 kvm_release_pfn_clean(pfn);
1517 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001518}
1519
Avi Kivity6aa8b732006-12-10 02:21:36 -08001520static void nonpaging_free(struct kvm_vcpu *vcpu)
1521{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001522 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001523}
1524
1525static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1526{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001527 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001528
1529 context->new_cr3 = nonpaging_new_cr3;
1530 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001531 context->gva_to_gpa = nonpaging_gva_to_gpa;
1532 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001533 context->prefetch_page = nonpaging_prefetch_page;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001534 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001535 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001536 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001537 return 0;
1538}
1539
Avi Kivityd835dfe2007-11-21 02:57:59 +02001540void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001541{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001542 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001543 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001544}
1545
1546static void paging_new_cr3(struct kvm_vcpu *vcpu)
1547{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001548 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001549 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001550}
1551
Avi Kivity6aa8b732006-12-10 02:21:36 -08001552static void inject_page_fault(struct kvm_vcpu *vcpu,
1553 u64 addr,
1554 u32 err_code)
1555{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02001556 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001557}
1558
Avi Kivity6aa8b732006-12-10 02:21:36 -08001559static void paging_free(struct kvm_vcpu *vcpu)
1560{
1561 nonpaging_free(vcpu);
1562}
1563
1564#define PTTYPE 64
1565#include "paging_tmpl.h"
1566#undef PTTYPE
1567
1568#define PTTYPE 32
1569#include "paging_tmpl.h"
1570#undef PTTYPE
1571
Avi Kivity17ac10a2007-01-05 16:36:40 -08001572static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001573{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001574 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001575
1576 ASSERT(is_pae(vcpu));
1577 context->new_cr3 = paging_new_cr3;
1578 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001579 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02001580 context->prefetch_page = paging64_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001581 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001582 context->root_level = level;
1583 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001584 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001585 return 0;
1586}
1587
Avi Kivity17ac10a2007-01-05 16:36:40 -08001588static int paging64_init_context(struct kvm_vcpu *vcpu)
1589{
1590 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1591}
1592
Avi Kivity6aa8b732006-12-10 02:21:36 -08001593static int paging32_init_context(struct kvm_vcpu *vcpu)
1594{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001595 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001596
1597 context->new_cr3 = paging_new_cr3;
1598 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001599 context->gva_to_gpa = paging32_gva_to_gpa;
1600 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001601 context->prefetch_page = paging32_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001602 context->root_level = PT32_ROOT_LEVEL;
1603 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001604 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001605 return 0;
1606}
1607
1608static int paging32E_init_context(struct kvm_vcpu *vcpu)
1609{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001610 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611}
1612
Joerg Roedelfb72d162008-02-07 13:47:44 +01001613static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
1614{
1615 struct kvm_mmu *context = &vcpu->arch.mmu;
1616
1617 context->new_cr3 = nonpaging_new_cr3;
1618 context->page_fault = tdp_page_fault;
1619 context->free = nonpaging_free;
1620 context->prefetch_page = nonpaging_prefetch_page;
Sheng Yang67253af2008-04-25 10:20:22 +08001621 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01001622 context->root_hpa = INVALID_PAGE;
1623
1624 if (!is_paging(vcpu)) {
1625 context->gva_to_gpa = nonpaging_gva_to_gpa;
1626 context->root_level = 0;
1627 } else if (is_long_mode(vcpu)) {
1628 context->gva_to_gpa = paging64_gva_to_gpa;
1629 context->root_level = PT64_ROOT_LEVEL;
1630 } else if (is_pae(vcpu)) {
1631 context->gva_to_gpa = paging64_gva_to_gpa;
1632 context->root_level = PT32E_ROOT_LEVEL;
1633 } else {
1634 context->gva_to_gpa = paging32_gva_to_gpa;
1635 context->root_level = PT32_ROOT_LEVEL;
1636 }
1637
1638 return 0;
1639}
1640
1641static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001642{
1643 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001644 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001645
1646 if (!is_paging(vcpu))
1647 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001648 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001649 return paging64_init_context(vcpu);
1650 else if (is_pae(vcpu))
1651 return paging32E_init_context(vcpu);
1652 else
1653 return paging32_init_context(vcpu);
1654}
1655
Joerg Roedelfb72d162008-02-07 13:47:44 +01001656static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1657{
Anthony Liguori35149e22008-04-02 14:46:56 -05001658 vcpu->arch.update_pte.pfn = bad_pfn;
1659
Joerg Roedelfb72d162008-02-07 13:47:44 +01001660 if (tdp_enabled)
1661 return init_kvm_tdp_mmu(vcpu);
1662 else
1663 return init_kvm_softmmu(vcpu);
1664}
1665
Avi Kivity6aa8b732006-12-10 02:21:36 -08001666static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1667{
1668 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001669 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
1670 vcpu->arch.mmu.free(vcpu);
1671 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001672 }
1673}
1674
1675int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1676{
Avi Kivity17c3ba92007-06-04 15:58:30 +03001677 destroy_kvm_mmu(vcpu);
1678 return init_kvm_mmu(vcpu);
1679}
Eddie Dong8668a3c2007-10-10 14:26:45 +08001680EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001681
1682int kvm_mmu_load(struct kvm_vcpu *vcpu)
1683{
Avi Kivity714b93d2007-01-05 16:36:53 -08001684 int r;
1685
Avi Kivitye2dec932007-01-05 16:36:54 -08001686 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001687 if (r)
1688 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001689 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001690 kvm_mmu_free_some_pages(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001691 mmu_alloc_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001692 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001693 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001694 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001695out:
1696 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001697}
Avi Kivity17c3ba92007-06-04 15:58:30 +03001698EXPORT_SYMBOL_GPL(kvm_mmu_load);
1699
1700void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1701{
1702 mmu_free_roots(vcpu);
1703}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001704
Avi Kivity09072da2007-05-01 14:16:52 +03001705static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001706 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02001707 u64 *spte)
1708{
1709 u64 pte;
1710 struct kvm_mmu_page *child;
1711
1712 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02001713 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001714 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
1715 is_large_pte(pte))
Izik Eidus290fc382007-09-27 14:11:22 +02001716 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001717 else {
1718 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03001719 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001720 }
1721 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001722 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001723 if (is_large_pte(pte))
1724 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02001725}
1726
Avi Kivity00284252007-05-01 16:53:31 +03001727static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001728 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03001729 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02001730 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03001731{
Marcelo Tosatti30945382008-06-11 20:32:40 -03001732 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
1733 if (!vcpu->arch.update_pte.largepage ||
1734 sp->role.glevels == PT32_ROOT_LEVEL) {
1735 ++vcpu->kvm->stat.mmu_pde_zapped;
1736 return;
1737 }
1738 }
Avi Kivity00284252007-05-01 16:53:31 +03001739
Avi Kivity4cee5762007-11-18 16:37:07 +02001740 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02001741 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02001742 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03001743 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02001744 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03001745}
1746
Avi Kivity79539ce2007-11-21 02:06:21 +02001747static bool need_remote_flush(u64 old, u64 new)
1748{
1749 if (!is_shadow_present_pte(old))
1750 return false;
1751 if (!is_shadow_present_pte(new))
1752 return true;
1753 if ((old ^ new) & PT64_BASE_ADDR_MASK)
1754 return true;
1755 old ^= PT64_NX_MASK;
1756 new ^= PT64_NX_MASK;
1757 return (old & ~new & PT64_PERM_MASK) != 0;
1758}
1759
1760static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
1761{
1762 if (need_remote_flush(old, new))
1763 kvm_flush_remote_tlbs(vcpu->kvm);
1764 else
1765 kvm_mmu_flush_tlb(vcpu);
1766}
1767
Avi Kivity12b7d282007-09-23 14:10:49 +02001768static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1769{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001770 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02001771
Sheng Yang7b523452008-04-25 21:13:50 +08001772 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02001773}
1774
Avi Kivityd7824ff2007-12-30 12:29:05 +02001775static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1776 const u8 *new, int bytes)
1777{
1778 gfn_t gfn;
1779 int r;
1780 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001781 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02001782
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001783 vcpu->arch.update_pte.largepage = 0;
1784
Avi Kivityd7824ff2007-12-30 12:29:05 +02001785 if (bytes != 4 && bytes != 8)
1786 return;
1787
1788 /*
1789 * Assume that the pte write on a page table of the same type
1790 * as the current vcpu paging mode. This is nearly always true
1791 * (might be false while changing modes). Note it is verified later
1792 * by update_pte().
1793 */
1794 if (is_pae(vcpu)) {
1795 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
1796 if ((bytes == 4) && (gpa % 4 == 0)) {
1797 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
1798 if (r)
1799 return;
1800 memcpy((void *)&gpte + (gpa % 8), new, 4);
1801 } else if ((bytes == 8) && (gpa % 8 == 0)) {
1802 memcpy((void *)&gpte, new, 8);
1803 }
1804 } else {
1805 if ((bytes == 4) && (gpa % 4 == 0))
1806 memcpy((void *)&gpte, new, 4);
1807 }
1808 if (!is_present_pte(gpte))
1809 return;
1810 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001811
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001812 down_read(&current->mm->mmap_sem);
1813 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
1814 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1815 vcpu->arch.update_pte.largepage = 1;
1816 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001817 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
1818 /* implicit mb(), we'll read before PT lock is unlocked */
Anthony Liguori35149e22008-04-02 14:46:56 -05001819 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001820 up_read(&current->mm->mmap_sem);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001821
Anthony Liguori35149e22008-04-02 14:46:56 -05001822 if (is_error_pfn(pfn)) {
1823 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001824 return;
1825 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02001826 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001827 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02001828}
1829
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001830static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
1831{
1832 u64 *spte = vcpu->arch.last_pte_updated;
1833
1834 if (spte
1835 && vcpu->arch.last_pte_gfn == gfn
1836 && shadow_accessed_mask
1837 && !(*spte & shadow_accessed_mask)
1838 && is_shadow_present_pte(*spte))
1839 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
1840}
1841
Avi Kivity09072da2007-05-01 14:16:52 +03001842void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life551882007-07-23 14:51:39 +08001843 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001844{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001845 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02001846 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001847 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001848 struct hlist_head *bucket;
1849 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02001850 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001851 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001852 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001853 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001854 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001855 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001856 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001857 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001858 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001859 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02001860 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001861
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001862 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02001863 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001864 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001865 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02001866 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02001867 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02001868 kvm_mmu_audit(vcpu, "pre pte write");
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001869 if (gfn == vcpu->arch.last_pt_write_gfn
Avi Kivity12b7d282007-09-23 14:10:49 +02001870 && !last_updated_pte_accessed(vcpu)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001871 ++vcpu->arch.last_pt_write_count;
1872 if (vcpu->arch.last_pt_write_count >= 3)
Avi Kivity86a5ba02007-01-05 16:36:50 -08001873 flooded = 1;
1874 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001875 vcpu->arch.last_pt_write_gfn = gfn;
1876 vcpu->arch.last_pt_write_count = 1;
1877 vcpu->arch.last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001878 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001879 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001880 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001881 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001882 if (sp->gfn != gfn || sp->role.metaphysical || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08001883 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02001884 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001885 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001886 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001887 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001888 /*
1889 * Misaligned accesses are too much trouble to fix
1890 * up; also, they usually indicate a page is not used
1891 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001892 *
1893 * If we're seeing too many writes to a page,
1894 * it may no longer be a page table, or we may be
1895 * forking, in which case it is better to unmap the
1896 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001897 */
1898 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02001899 gpa, bytes, sp->role.word);
1900 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001901 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001902 continue;
1903 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001904 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02001905 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001906 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001907 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001908 page_offset <<= 1; /* 32->64 */
1909 /*
1910 * A 32-bit pde maps 4MB while the shadow pdes map
1911 * only 2MB. So we need to double the offset again
1912 * and zap two pdes instead of one.
1913 */
1914 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001915 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001916 page_offset <<= 1;
1917 npte = 2;
1918 }
Avi Kivityfce06572007-05-01 16:44:05 +03001919 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001920 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001921 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03001922 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001923 }
Avi Kivity4db35312007-11-21 15:28:32 +02001924 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02001925 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
1926 gentry = 0;
1927 r = kvm_read_guest_atomic(vcpu->kvm,
1928 gpa & ~(u64)(pte_size - 1),
1929 &gentry, pte_size);
1930 new = (const void *)&gentry;
1931 if (r < 0)
1932 new = NULL;
1933 }
Avi Kivityac1b7142007-03-08 17:13:32 +02001934 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02001935 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02001936 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02001937 if (new)
1938 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02001939 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001940 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001941 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001942 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001943 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001944 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05001945 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
1946 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
1947 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02001948 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08001949}
1950
Avi Kivitya4360362007-01-05 16:36:45 -08001951int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1952{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001953 gpa_t gpa;
1954 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08001955
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001956 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001957
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001958 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001959 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001960 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001961 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08001962}
Avi Kivity577bdc42008-07-19 08:57:05 +03001963EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08001964
Avi Kivity22d95b12007-09-14 20:26:06 +03001965void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08001966{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001967 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02001968 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08001969
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001970 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02001971 struct kvm_mmu_page, link);
1972 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001973 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08001974 }
1975}
Avi Kivityebeace82007-01-05 16:36:47 -08001976
Avi Kivity30677142007-10-28 18:48:59 +02001977int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
1978{
1979 int r;
1980 enum emulation_result er;
1981
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001982 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02001983 if (r < 0)
1984 goto out;
1985
1986 if (!r) {
1987 r = 1;
1988 goto out;
1989 }
1990
Avi Kivityb733bfb2007-10-28 18:52:05 +02001991 r = mmu_topup_memory_caches(vcpu);
1992 if (r)
1993 goto out;
1994
Avi Kivity30677142007-10-28 18:48:59 +02001995 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02001996
1997 switch (er) {
1998 case EMULATE_DONE:
1999 return 1;
2000 case EMULATE_DO_MMIO:
2001 ++vcpu->stat.mmio_exits;
2002 return 0;
2003 case EMULATE_FAIL:
2004 kvm_report_emulation_failure(vcpu, "pagetable");
2005 return 1;
2006 default:
2007 BUG();
2008 }
2009out:
Avi Kivity30677142007-10-28 18:48:59 +02002010 return r;
2011}
2012EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2013
Joerg Roedel18552672008-02-07 13:47:41 +01002014void kvm_enable_tdp(void)
2015{
2016 tdp_enabled = true;
2017}
2018EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2019
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002020void kvm_disable_tdp(void)
2021{
2022 tdp_enabled = false;
2023}
2024EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2025
Avi Kivity6aa8b732006-12-10 02:21:36 -08002026static void free_mmu_pages(struct kvm_vcpu *vcpu)
2027{
Avi Kivity4db35312007-11-21 15:28:32 +02002028 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002029
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002030 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
2031 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
Avi Kivity4db35312007-11-21 15:28:32 +02002032 struct kvm_mmu_page, link);
2033 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity8d2d73b2008-06-04 18:42:24 +03002034 cond_resched();
Avi Kivityf51234c2007-01-05 16:36:52 -08002035 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002036 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002037}
2038
2039static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2040{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002041 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002042 int i;
2043
2044 ASSERT(vcpu);
2045
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002046 if (vcpu->kvm->arch.n_requested_mmu_pages)
2047 vcpu->kvm->arch.n_free_mmu_pages =
2048 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002049 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002050 vcpu->kvm->arch.n_free_mmu_pages =
2051 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002052 /*
2053 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2054 * Therefore we need to allocate shadow page tables in the first
2055 * 4GB of memory, which happens to fit the DMA32 zone.
2056 */
2057 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2058 if (!page)
2059 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002060 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002061 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002062 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002063
Avi Kivity6aa8b732006-12-10 02:21:36 -08002064 return 0;
2065
2066error_1:
2067 free_mmu_pages(vcpu);
2068 return -ENOMEM;
2069}
2070
Ingo Molnar8018c272006-12-29 16:50:01 -08002071int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002072{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002073 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002074 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002075
Ingo Molnar8018c272006-12-29 16:50:01 -08002076 return alloc_mmu_pages(vcpu);
2077}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002078
Ingo Molnar8018c272006-12-29 16:50:01 -08002079int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2080{
2081 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002082 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002083
Ingo Molnar8018c272006-12-29 16:50:01 -08002084 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002085}
2086
2087void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2088{
2089 ASSERT(vcpu);
2090
2091 destroy_kvm_mmu(vcpu);
2092 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002093 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002094}
2095
Avi Kivity90cb0522007-07-17 13:04:56 +03002096void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002097{
Avi Kivity4db35312007-11-21 15:28:32 +02002098 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002099
Avi Kivity2245a282008-08-27 16:32:24 +03002100 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002101 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002102 int i;
2103 u64 *pt;
2104
Avi Kivity4db35312007-11-21 15:28:32 +02002105 if (!test_bit(slot, &sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002106 continue;
2107
Avi Kivity4db35312007-11-21 15:28:32 +02002108 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002109 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2110 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002111 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002112 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002113 }
Avi Kivity171d5952008-08-27 16:40:51 +03002114 kvm_flush_remote_tlbs(kvm);
Avi Kivity2245a282008-08-27 16:32:24 +03002115 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002116}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002117
Avi Kivity90cb0522007-07-17 13:04:56 +03002118void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002119{
Avi Kivity4db35312007-11-21 15:28:32 +02002120 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002121
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002122 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002123 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Avi Kivity4db35312007-11-21 15:28:32 +02002124 kvm_mmu_zap_page(kvm, sp);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002125 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002126
Avi Kivity90cb0522007-07-17 13:04:56 +03002127 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002128}
2129
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002130static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002131{
2132 struct kvm_mmu_page *page;
2133
2134 page = container_of(kvm->arch.active_mmu_pages.prev,
2135 struct kvm_mmu_page, link);
2136 kvm_mmu_zap_page(kvm, page);
2137}
2138
2139static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2140{
2141 struct kvm *kvm;
2142 struct kvm *kvm_freed = NULL;
2143 int cache_count = 0;
2144
2145 spin_lock(&kvm_lock);
2146
2147 list_for_each_entry(kvm, &vm_list, vm_list) {
2148 int npages;
2149
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002150 if (!down_read_trylock(&kvm->slots_lock))
2151 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002152 spin_lock(&kvm->mmu_lock);
2153 npages = kvm->arch.n_alloc_mmu_pages -
2154 kvm->arch.n_free_mmu_pages;
2155 cache_count += npages;
2156 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2157 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2158 cache_count--;
2159 kvm_freed = kvm;
2160 }
2161 nr_to_scan--;
2162
2163 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002164 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002165 }
2166 if (kvm_freed)
2167 list_move_tail(&kvm_freed->vm_list, &vm_list);
2168
2169 spin_unlock(&kvm_lock);
2170
2171 return cache_count;
2172}
2173
2174static struct shrinker mmu_shrinker = {
2175 .shrink = mmu_shrink,
2176 .seeks = DEFAULT_SEEKS * 10,
2177};
2178
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002179static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002180{
2181 if (pte_chain_cache)
2182 kmem_cache_destroy(pte_chain_cache);
2183 if (rmap_desc_cache)
2184 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002185 if (mmu_page_header_cache)
2186 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002187}
2188
Izik Eidus3ee16c82008-03-30 15:17:21 +03002189void kvm_mmu_module_exit(void)
2190{
2191 mmu_destroy_caches();
2192 unregister_shrinker(&mmu_shrinker);
2193}
2194
Avi Kivityb5a33a72007-04-15 16:31:09 +03002195int kvm_mmu_module_init(void)
2196{
2197 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2198 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002199 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002200 if (!pte_chain_cache)
2201 goto nomem;
2202 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2203 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002204 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002205 if (!rmap_desc_cache)
2206 goto nomem;
2207
Avi Kivityd3d25b02007-05-30 12:34:53 +03002208 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2209 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002210 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002211 if (!mmu_page_header_cache)
2212 goto nomem;
2213
Izik Eidus3ee16c82008-03-30 15:17:21 +03002214 register_shrinker(&mmu_shrinker);
2215
Avi Kivityb5a33a72007-04-15 16:31:09 +03002216 return 0;
2217
2218nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002219 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002220 return -ENOMEM;
2221}
2222
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002223/*
2224 * Caculate mmu pages needed for kvm.
2225 */
2226unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2227{
2228 int i;
2229 unsigned int nr_mmu_pages;
2230 unsigned int nr_pages = 0;
2231
2232 for (i = 0; i < kvm->nmemslots; i++)
2233 nr_pages += kvm->memslots[i].npages;
2234
2235 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2236 nr_mmu_pages = max(nr_mmu_pages,
2237 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2238
2239 return nr_mmu_pages;
2240}
2241
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002242static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2243 unsigned len)
2244{
2245 if (len > buffer->len)
2246 return NULL;
2247 return buffer->ptr;
2248}
2249
2250static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2251 unsigned len)
2252{
2253 void *ret;
2254
2255 ret = pv_mmu_peek_buffer(buffer, len);
2256 if (!ret)
2257 return ret;
2258 buffer->ptr += len;
2259 buffer->len -= len;
2260 buffer->processed += len;
2261 return ret;
2262}
2263
2264static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2265 gpa_t addr, gpa_t value)
2266{
2267 int bytes = 8;
2268 int r;
2269
2270 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2271 bytes = 4;
2272
2273 r = mmu_topup_memory_caches(vcpu);
2274 if (r)
2275 return r;
2276
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002277 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002278 return -EFAULT;
2279
2280 return 1;
2281}
2282
2283static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2284{
2285 kvm_x86_ops->tlb_flush(vcpu);
2286 return 1;
2287}
2288
2289static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2290{
2291 spin_lock(&vcpu->kvm->mmu_lock);
2292 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2293 spin_unlock(&vcpu->kvm->mmu_lock);
2294 return 1;
2295}
2296
2297static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2298 struct kvm_pv_mmu_op_buffer *buffer)
2299{
2300 struct kvm_mmu_op_header *header;
2301
2302 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2303 if (!header)
2304 return 0;
2305 switch (header->op) {
2306 case KVM_MMU_OP_WRITE_PTE: {
2307 struct kvm_mmu_op_write_pte *wpte;
2308
2309 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2310 if (!wpte)
2311 return 0;
2312 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2313 wpte->pte_val);
2314 }
2315 case KVM_MMU_OP_FLUSH_TLB: {
2316 struct kvm_mmu_op_flush_tlb *ftlb;
2317
2318 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2319 if (!ftlb)
2320 return 0;
2321 return kvm_pv_mmu_flush_tlb(vcpu);
2322 }
2323 case KVM_MMU_OP_RELEASE_PT: {
2324 struct kvm_mmu_op_release_pt *rpt;
2325
2326 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2327 if (!rpt)
2328 return 0;
2329 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2330 }
2331 default: return 0;
2332 }
2333}
2334
2335int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
2336 gpa_t addr, unsigned long *ret)
2337{
2338 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002339 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002340
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002341 buffer->ptr = buffer->buf;
2342 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
2343 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002344
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002345 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002346 if (r)
2347 goto out;
2348
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002349 while (buffer->len) {
2350 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002351 if (r < 0)
2352 goto out;
2353 if (r == 0)
2354 break;
2355 }
2356
2357 r = 1;
2358out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002359 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002360 return r;
2361}
2362
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002363#ifdef AUDIT
2364
2365static const char *audit_msg;
2366
2367static gva_t canonicalize(gva_t gva)
2368{
2369#ifdef CONFIG_X86_64
2370 gva = (long long)(gva << 16) >> 16;
2371#endif
2372 return gva;
2373}
2374
2375static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
2376 gva_t va, int level)
2377{
2378 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
2379 int i;
2380 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
2381
2382 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
2383 u64 ent = pt[i];
2384
Avi Kivityc7addb92007-09-16 18:58:32 +02002385 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002386 continue;
2387
2388 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02002389 if (level > 1) {
2390 if (ent == shadow_notrap_nonpresent_pte)
2391 printk(KERN_ERR "audit: (%s) nontrapping pte"
2392 " in nonleaf level: levels %d gva %lx"
2393 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002394 vcpu->arch.mmu.root_level, va, level, ent);
Avi Kivityc7addb92007-09-16 18:58:32 +02002395
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002396 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02002397 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002398 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05002399 hpa_t hpa = (hpa_t)gpa_to_pfn(vcpu, gpa) << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002400
Avi Kivityc7addb92007-09-16 18:58:32 +02002401 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002402 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02002403 printk(KERN_ERR "xx audit error: (%s) levels %d"
2404 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002405 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04002406 va, gpa, hpa, ent,
2407 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02002408 else if (ent == shadow_notrap_nonpresent_pte
2409 && !is_error_hpa(hpa))
2410 printk(KERN_ERR "audit: (%s) notrap shadow,"
2411 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05002412 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02002413
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002414 }
2415 }
2416}
2417
2418static void audit_mappings(struct kvm_vcpu *vcpu)
2419{
Avi Kivity1ea252a2007-03-08 11:48:09 +02002420 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002421
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002422 if (vcpu->arch.mmu.root_level == 4)
2423 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002424 else
2425 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002426 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002427 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002428 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002429 i << 30,
2430 2);
2431}
2432
2433static int count_rmaps(struct kvm_vcpu *vcpu)
2434{
2435 int nmaps = 0;
2436 int i, j, k;
2437
2438 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
2439 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
2440 struct kvm_rmap_desc *d;
2441
2442 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02002443 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002444
Izik Eidus290fc382007-09-27 14:11:22 +02002445 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002446 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02002447 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002448 ++nmaps;
2449 continue;
2450 }
Izik Eidus290fc382007-09-27 14:11:22 +02002451 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002452 while (d) {
2453 for (k = 0; k < RMAP_EXT; ++k)
2454 if (d->shadow_ptes[k])
2455 ++nmaps;
2456 else
2457 break;
2458 d = d->more;
2459 }
2460 }
2461 }
2462 return nmaps;
2463}
2464
2465static int count_writable_mappings(struct kvm_vcpu *vcpu)
2466{
2467 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02002468 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002469 int i;
2470
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002471 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02002472 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002473
Avi Kivity4db35312007-11-21 15:28:32 +02002474 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002475 continue;
2476
2477 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
2478 u64 ent = pt[i];
2479
2480 if (!(ent & PT_PRESENT_MASK))
2481 continue;
2482 if (!(ent & PT_WRITABLE_MASK))
2483 continue;
2484 ++nmaps;
2485 }
2486 }
2487 return nmaps;
2488}
2489
2490static void audit_rmap(struct kvm_vcpu *vcpu)
2491{
2492 int n_rmap = count_rmaps(vcpu);
2493 int n_actual = count_writable_mappings(vcpu);
2494
2495 if (n_rmap != n_actual)
2496 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002497 __func__, audit_msg, n_rmap, n_actual);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002498}
2499
2500static void audit_write_protection(struct kvm_vcpu *vcpu)
2501{
Avi Kivity4db35312007-11-21 15:28:32 +02002502 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02002503 struct kvm_memory_slot *slot;
2504 unsigned long *rmapp;
2505 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002506
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002507 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02002508 if (sp->role.metaphysical)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002509 continue;
2510
Avi Kivity4db35312007-11-21 15:28:32 +02002511 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
2512 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02002513 rmapp = &slot->rmap[gfn - slot->base_gfn];
2514 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002515 printk(KERN_ERR "%s: (%s) shadow page has writable"
2516 " mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002517 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02002518 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002519 }
2520}
2521
2522static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
2523{
2524 int olddbg = dbg;
2525
2526 dbg = 0;
2527 audit_msg = msg;
2528 audit_rmap(vcpu);
2529 audit_write_protection(vcpu);
2530 audit_mappings(vcpu);
2531 dbg = olddbg;
2532}
2533
2534#endif