blob: 1fd8e3b58cc05cc86cc6bc100b35e853f247c165 [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
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -0500138struct kvm_pv_mmu_op_buffer {
139 void *ptr;
140 unsigned len;
141 unsigned processed;
142 char buf[512] __aligned(sizeof(long));
143};
144
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800145struct kvm_rmap_desc {
146 u64 *shadow_ptes[RMAP_EXT];
147 struct kvm_rmap_desc *more;
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
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800656#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300657static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800658{
Avi Kivity139bdb22007-01-05 16:36:50 -0800659 u64 *pos;
660 u64 *end;
661
Avi Kivity47ad8e62007-05-06 15:50:58 +0300662 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300663 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800664 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800665 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800666 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800667 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800668 return 1;
669}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800670#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800671
Avi Kivity4db35312007-11-21 15:28:32 +0200672static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800673{
Avi Kivity4db35312007-11-21 15:28:32 +0200674 ASSERT(is_empty_shadow_page(sp->spt));
675 list_del(&sp->link);
676 __free_page(virt_to_page(sp->spt));
677 __free_page(virt_to_page(sp->gfns));
678 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800679 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800680}
681
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800682static unsigned kvm_page_table_hashfn(gfn_t gfn)
683{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200684 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800685}
686
Avi Kivity25c0de22007-01-05 16:36:42 -0800687static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
688 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800689{
Avi Kivity4db35312007-11-21 15:28:32 +0200690 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800691
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800692 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
693 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
694 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200695 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800696 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Avi Kivity4db35312007-11-21 15:28:32 +0200697 ASSERT(is_empty_shadow_page(sp->spt));
698 sp->slot_bitmap = 0;
699 sp->multimapped = 0;
700 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800701 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200702 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800703}
704
Avi Kivity714b93d2007-01-05 16:36:53 -0800705static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200706 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800707{
708 struct kvm_pte_chain *pte_chain;
709 struct hlist_node *node;
710 int i;
711
712 if (!parent_pte)
713 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200714 if (!sp->multimapped) {
715 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800716
717 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200718 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800719 return;
720 }
Avi Kivity4db35312007-11-21 15:28:32 +0200721 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800722 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200723 INIT_HLIST_HEAD(&sp->parent_ptes);
724 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800725 pte_chain->parent_ptes[0] = old;
726 }
Avi Kivity4db35312007-11-21 15:28:32 +0200727 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800728 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
729 continue;
730 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
731 if (!pte_chain->parent_ptes[i]) {
732 pte_chain->parent_ptes[i] = parent_pte;
733 return;
734 }
735 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800736 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800737 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200738 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800739 pte_chain->parent_ptes[0] = parent_pte;
740}
741
Avi Kivity4db35312007-11-21 15:28:32 +0200742static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800743 u64 *parent_pte)
744{
745 struct kvm_pte_chain *pte_chain;
746 struct hlist_node *node;
747 int i;
748
Avi Kivity4db35312007-11-21 15:28:32 +0200749 if (!sp->multimapped) {
750 BUG_ON(sp->parent_pte != parent_pte);
751 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800752 return;
753 }
Avi Kivity4db35312007-11-21 15:28:32 +0200754 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800755 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
756 if (!pte_chain->parent_ptes[i])
757 break;
758 if (pte_chain->parent_ptes[i] != parent_pte)
759 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800760 while (i + 1 < NR_PTE_CHAIN_ENTRIES
761 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800762 pte_chain->parent_ptes[i]
763 = pte_chain->parent_ptes[i + 1];
764 ++i;
765 }
766 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800767 if (i == 0) {
768 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300769 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200770 if (hlist_empty(&sp->parent_ptes)) {
771 sp->multimapped = 0;
772 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800773 }
774 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800775 return;
776 }
777 BUG();
778}
779
Avi Kivityd761a502008-05-29 14:55:03 +0300780static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
781 struct kvm_mmu_page *sp)
782{
783 int i;
784
785 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
786 sp->spt[i] = shadow_trap_nonpresent_pte;
787}
788
Avi Kivity4db35312007-11-21 15:28:32 +0200789static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800790{
791 unsigned index;
792 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200793 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800794 struct hlist_node *node;
795
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800796 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200797 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800798 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200799 hlist_for_each_entry(sp, node, bucket, hash_link)
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500800 if (sp->gfn == gfn && !sp->role.metaphysical
801 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800802 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800803 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +0200804 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800805 }
806 return NULL;
807}
808
809static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
810 gfn_t gfn,
811 gva_t gaddr,
812 unsigned level,
813 int metaphysical,
Avi Kivity41074d02007-12-09 17:00:02 +0200814 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +0200815 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800816{
817 union kvm_mmu_page_role role;
818 unsigned index;
819 unsigned quadrant;
820 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200821 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800822 struct hlist_node *node;
823
824 role.word = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800825 role.glevels = vcpu->arch.mmu.root_level;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800826 role.level = level;
827 role.metaphysical = metaphysical;
Avi Kivity41074d02007-12-09 17:00:02 +0200828 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800829 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800830 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
831 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
832 role.quadrant = quadrant;
833 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800834 pgprintk("%s: looking gfn %lx role %x\n", __func__,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800835 gfn, role.word);
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200836 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800837 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200838 hlist_for_each_entry(sp, node, bucket, hash_link)
839 if (sp->gfn == gfn && sp->role.word == role.word) {
840 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800841 pgprintk("%s: found\n", __func__);
Avi Kivity4db35312007-11-21 15:28:32 +0200842 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800843 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200844 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +0200845 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
846 if (!sp)
847 return sp;
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800848 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +0200849 sp->gfn = gfn;
850 sp->role = role;
851 hlist_add_head(&sp->hash_link, bucket);
Avi Kivity374cbac2007-01-05 16:36:43 -0800852 if (!metaphysical)
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500853 rmap_write_protect(vcpu->kvm, gfn);
Avi Kivity131d8272008-05-29 14:56:28 +0300854 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
855 vcpu->arch.mmu.prefetch_page(vcpu, sp);
856 else
857 nonpaging_prefetch_page(vcpu, sp);
Avi Kivity4db35312007-11-21 15:28:32 +0200858 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800859}
860
Avi Kivity90cb0522007-07-17 13:04:56 +0300861static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +0200862 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -0800863{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800864 unsigned i;
865 u64 *pt;
866 u64 ent;
867
Avi Kivity4db35312007-11-21 15:28:32 +0200868 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800869
Avi Kivity4db35312007-11-21 15:28:32 +0200870 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800871 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +0200872 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +0200873 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +0200874 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800875 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300876 kvm_flush_remote_tlbs(kvm);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800877 return;
878 }
879
880 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
881 ent = pt[i];
882
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300883 if (is_shadow_present_pte(ent)) {
884 if (!is_large_pte(ent)) {
885 ent &= PT64_BASE_ADDR_MASK;
886 mmu_page_remove_parent_pte(page_header(ent),
887 &pt[i]);
888 } else {
889 --kvm->stat.lpages;
890 rmap_remove(kvm, &pt[i]);
891 }
892 }
Avi Kivityc7addb92007-09-16 18:58:32 +0200893 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800894 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300895 kvm_flush_remote_tlbs(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800896}
897
Avi Kivity4db35312007-11-21 15:28:32 +0200898static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800899{
Avi Kivity4db35312007-11-21 15:28:32 +0200900 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800901}
902
Avi Kivity12b7d282007-09-23 14:10:49 +0200903static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
904{
905 int i;
906
907 for (i = 0; i < KVM_MAX_VCPUS; ++i)
908 if (kvm->vcpus[i])
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800909 kvm->vcpus[i]->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +0200910}
911
Avi Kivity4db35312007-11-21 15:28:32 +0200912static void kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -0800913{
914 u64 *parent_pte;
915
Avi Kivity4cee5762007-11-18 16:37:07 +0200916 ++kvm->stat.mmu_shadow_zapped;
Avi Kivity4db35312007-11-21 15:28:32 +0200917 while (sp->multimapped || sp->parent_pte) {
918 if (!sp->multimapped)
919 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -0800920 else {
921 struct kvm_pte_chain *chain;
922
Avi Kivity4db35312007-11-21 15:28:32 +0200923 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -0800924 struct kvm_pte_chain, link);
925 parent_pte = chain->parent_ptes[0];
926 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800927 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +0200928 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +0200929 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800930 }
Avi Kivity4db35312007-11-21 15:28:32 +0200931 kvm_mmu_page_unlink_children(kvm, sp);
932 if (!sp->root_count) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300933 if (!sp->role.metaphysical)
934 unaccount_shadowed(kvm, sp->gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200935 hlist_del(&sp->hash_link);
936 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500937 } else {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800938 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500939 sp->role.invalid = 1;
940 kvm_reload_remote_mmus(kvm);
941 }
Avi Kivity12b7d282007-09-23 14:10:49 +0200942 kvm_mmu_reset_last_pte_updated(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800943}
944
Izik Eidus82ce2c92007-10-02 18:52:55 +0200945/*
946 * Changing the number of mmu pages allocated to the vm
947 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
948 */
949void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
950{
951 /*
952 * If we set the number of mmu pages to be smaller be than the
953 * number of actived pages , we must to free some mmu pages before we
954 * change the value
955 */
956
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800957 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
Izik Eidus82ce2c92007-10-02 18:52:55 +0200958 kvm_nr_mmu_pages) {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800959 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
960 - kvm->arch.n_free_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200961
962 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
963 struct kvm_mmu_page *page;
964
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800965 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +0200966 struct kvm_mmu_page, link);
967 kvm_mmu_zap_page(kvm, page);
968 n_used_mmu_pages--;
969 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800970 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200971 }
972 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800973 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
974 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200975
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800976 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200977}
978
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500979static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -0800980{
981 unsigned index;
982 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200983 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -0800984 struct hlist_node *node, *n;
985 int r;
986
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800987 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -0800988 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200989 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800990 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200991 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
992 if (sp->gfn == gfn && !sp->role.metaphysical) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800993 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +0200994 sp->role.word);
995 kvm_mmu_zap_page(kvm, sp);
Avi Kivitya4360362007-01-05 16:36:45 -0800996 r = 1;
997 }
998 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800999}
1000
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001001static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001002{
Avi Kivity4db35312007-11-21 15:28:32 +02001003 struct kvm_mmu_page *sp;
Avi Kivity97a0a012007-05-31 15:08:29 +03001004
Avi Kivity4db35312007-11-21 15:28:32 +02001005 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001006 pgprintk("%s: zap %lx %x\n", __func__, gfn, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001007 kvm_mmu_zap_page(kvm, sp);
Avi Kivity97a0a012007-05-31 15:08:29 +03001008 }
1009}
1010
Avi Kivity38c335f2007-11-21 14:20:22 +02001011static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012{
Avi Kivity38c335f2007-11-21 14:20:22 +02001013 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001014 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015
Avi Kivity4db35312007-11-21 15:28:32 +02001016 __set_bit(slot, &sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017}
1018
Avi Kivity039576c2007-03-20 12:46:50 +02001019struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1020{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001021 struct page *page;
1022
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001023 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001024
1025 if (gpa == UNMAPPED_GVA)
1026 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001027
1028 down_read(&current->mm->mmap_sem);
1029 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1030 up_read(&current->mm->mmap_sem);
1031
1032 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001033}
1034
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001035static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1036 unsigned pt_access, unsigned pte_access,
1037 int user_fault, int write_fault, int dirty,
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001038 int *ptwrite, int largepage, gfn_t gfn,
Anthony Liguori35149e22008-04-02 14:46:56 -05001039 pfn_t pfn, bool speculative)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001040{
1041 u64 spte;
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001042 int was_rmapped = 0;
Izik Eidus75e68e62008-01-12 23:49:09 +02001043 int was_writeble = is_writeble_pte(*shadow_pte);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001044
Avi Kivitybc750ba2007-12-09 18:39:41 +02001045 pgprintk("%s: spte %llx access %x write_fault %d"
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001046 " user_fault %d gfn %lx\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001047 __func__, *shadow_pte, pt_access,
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001048 write_fault, user_fault, gfn);
1049
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001050 if (is_rmap_pte(*shadow_pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001051 /*
1052 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1053 * the parent of the now unreachable PTE.
1054 */
1055 if (largepage && !is_large_pte(*shadow_pte)) {
1056 struct kvm_mmu_page *child;
1057 u64 pte = *shadow_pte;
1058
1059 child = page_header(pte & PT64_BASE_ADDR_MASK);
1060 mmu_page_remove_parent_pte(child, shadow_pte);
Anthony Liguori35149e22008-04-02 14:46:56 -05001061 } else if (pfn != spte_to_pfn(*shadow_pte)) {
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001062 pgprintk("hfn old %lx new %lx\n",
Anthony Liguori35149e22008-04-02 14:46:56 -05001063 spte_to_pfn(*shadow_pte), pfn);
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001064 rmap_remove(vcpu->kvm, shadow_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001065 } else {
1066 if (largepage)
1067 was_rmapped = is_large_pte(*shadow_pte);
1068 else
1069 was_rmapped = 1;
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001070 }
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001071 }
1072
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001073 /*
1074 * We don't set the accessed bit, since we sometimes want to see
1075 * whether the guest actually used the pte (in order to detect
1076 * demand paging).
1077 */
Sheng Yang7b523452008-04-25 21:13:50 +08001078 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001079 if (!speculative)
1080 pte_access |= PT_ACCESSED_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001081 if (!dirty)
1082 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001083 if (pte_access & ACC_EXEC_MASK)
1084 spte |= shadow_x_mask;
1085 else
1086 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001087 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001088 spte |= shadow_user_mask;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001089 if (largepage)
1090 spte |= PT_PAGE_SIZE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001091
Anthony Liguori35149e22008-04-02 14:46:56 -05001092 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001093
1094 if ((pte_access & ACC_WRITE_MASK)
1095 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
1096 struct kvm_mmu_page *shadow;
1097
1098 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001099
1100 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001101 if (shadow ||
1102 (largepage && has_wrprotected_page(vcpu->kvm, gfn))) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001103 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001104 __func__, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001105 pte_access &= ~ACC_WRITE_MASK;
1106 if (is_writeble_pte(spte)) {
1107 spte &= ~PT_WRITABLE_MASK;
1108 kvm_x86_ops->tlb_flush(vcpu);
1109 }
1110 if (write_fault)
1111 *ptwrite = 1;
1112 }
1113 }
1114
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001115 if (pte_access & ACC_WRITE_MASK)
1116 mark_page_dirty(vcpu->kvm, gfn);
1117
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001118 pgprintk("%s: setting spte %llx\n", __func__, spte);
Avi Kivitydb475c32008-06-22 16:46:22 +03001119 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001120 (spte&PT_PAGE_SIZE_MASK)? "2MB" : "4kB",
1121 (spte&PT_WRITABLE_MASK)?"RW":"R", gfn, spte, shadow_pte);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001122 set_shadow_pte(shadow_pte, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001123 if (!was_rmapped && (spte & PT_PAGE_SIZE_MASK)
1124 && (spte & PT_PRESENT_MASK))
1125 ++vcpu->kvm->stat.lpages;
1126
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001127 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1128 if (!was_rmapped) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001129 rmap_add(vcpu, shadow_pte, gfn, largepage);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001130 if (!is_rmap_pte(*shadow_pte))
Anthony Liguori35149e22008-04-02 14:46:56 -05001131 kvm_release_pfn_clean(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001132 } else {
1133 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001134 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001135 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001136 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001137 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001138 if (speculative) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001139 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001140 vcpu->arch.last_pte_gfn = gfn;
1141 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001142}
1143
Avi Kivity6aa8b732006-12-10 02:21:36 -08001144static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1145{
1146}
1147
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001148static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Anthony Liguori35149e22008-04-02 14:46:56 -05001149 int largepage, gfn_t gfn, pfn_t pfn,
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001150 int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001151{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001152 hpa_t table_addr = vcpu->arch.mmu.root_hpa;
Avi Kivitye8332402007-12-09 18:43:00 +02001153 int pt_write = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001154
1155 for (; ; level--) {
1156 u32 index = PT64_INDEX(v, level);
1157 u64 *table;
1158
1159 ASSERT(VALID_PAGE(table_addr));
1160 table = __va(table_addr);
1161
1162 if (level == 1) {
Avi Kivitye8332402007-12-09 18:43:00 +02001163 mmu_set_spte(vcpu, &table[index], ACC_ALL, ACC_ALL,
Anthony Liguori35149e22008-04-02 14:46:56 -05001164 0, write, 1, &pt_write, 0, gfn, pfn, false);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001165 return pt_write;
1166 }
1167
1168 if (largepage && level == 2) {
1169 mmu_set_spte(vcpu, &table[index], ACC_ALL, ACC_ALL,
Anthony Liguori35149e22008-04-02 14:46:56 -05001170 0, write, 1, &pt_write, 1, gfn, pfn, false);
Avi Kivityd196e342008-01-24 11:44:11 +02001171 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001172 }
1173
Avi Kivityc7addb92007-09-16 18:58:32 +02001174 if (table[index] == shadow_trap_nonpresent_pte) {
Avi Kivity25c0de22007-01-05 16:36:42 -08001175 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001176 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001177
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001178 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
1179 >> PAGE_SHIFT;
1180 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
1181 v, level - 1,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001182 1, ACC_ALL, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -08001183 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001184 pgprintk("nonpaging_map: ENOMEM\n");
Anthony Liguori35149e22008-04-02 14:46:56 -05001185 kvm_release_pfn_clean(pfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001186 return -ENOMEM;
1187 }
1188
Sheng Yang14394422008-04-28 12:24:45 +08001189 table[index] = __pa(new_table->spt)
1190 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1191 | shadow_user_mask | shadow_x_mask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001192 }
1193 table_addr = table[index] & PT64_BASE_ADDR_MASK;
1194 }
1195}
1196
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001197static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1198{
1199 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001200 int largepage = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001201 pfn_t pfn;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001202
1203 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001204 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1205 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1206 largepage = 1;
1207 }
1208
Anthony Liguori35149e22008-04-02 14:46:56 -05001209 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001210 up_read(&current->mm->mmap_sem);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001211
Avi Kivityd196e342008-01-24 11:44:11 +02001212 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001213 if (is_error_pfn(pfn)) {
1214 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001215 return 1;
1216 }
1217
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001218 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001219 kvm_mmu_free_some_pages(vcpu);
Anthony Liguori35149e22008-04-02 14:46:56 -05001220 r = __direct_map(vcpu, v, write, largepage, gfn, pfn,
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001221 PT32E_ROOT_LEVEL);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001222 spin_unlock(&vcpu->kvm->mmu_lock);
1223
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001224
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001225 return r;
1226}
1227
1228
Avi Kivity17ac10a2007-01-05 16:36:40 -08001229static void mmu_free_roots(struct kvm_vcpu *vcpu)
1230{
1231 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001232 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001233
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001234 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001235 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001236 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001237 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1238 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001239
Avi Kivity4db35312007-11-21 15:28:32 +02001240 sp = page_header(root);
1241 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001242 if (!sp->root_count && sp->role.invalid)
1243 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001244 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001245 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001246 return;
1247 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001248 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001249 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001250
Avi Kivity417726a2007-04-12 17:35:58 +03001251 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001252 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001253 sp = page_header(root);
1254 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001255 if (!sp->root_count && sp->role.invalid)
1256 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001257 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001258 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001259 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001260 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001261 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001262}
1263
1264static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1265{
1266 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001267 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001268 struct kvm_mmu_page *sp;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001269 int metaphysical = 0;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001270
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001271 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001272
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001273 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1274 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001275
1276 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01001277 if (tdp_enabled)
1278 metaphysical = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001279 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001280 PT64_ROOT_LEVEL, metaphysical,
1281 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001282 root = __pa(sp->spt);
1283 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001284 vcpu->arch.mmu.root_hpa = root;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001285 return;
1286 }
Joerg Roedelfb72d162008-02-07 13:47:44 +01001287 metaphysical = !is_paging(vcpu);
1288 if (tdp_enabled)
1289 metaphysical = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001290 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001291 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001292
1293 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001294 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1295 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1296 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001297 continue;
1298 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001299 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1300 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001301 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001302 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001303 PT32_ROOT_LEVEL, metaphysical,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001304 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001305 root = __pa(sp->spt);
1306 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001307 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001308 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001309 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001310}
1311
Avi Kivity6aa8b732006-12-10 02:21:36 -08001312static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1313{
1314 return vaddr;
1315}
1316
1317static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02001318 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001319{
Avi Kivitye8332402007-12-09 18:43:00 +02001320 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08001321 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001322
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001323 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08001324 r = mmu_topup_memory_caches(vcpu);
1325 if (r)
1326 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08001327
Avi Kivity6aa8b732006-12-10 02:21:36 -08001328 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001329 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001330
Avi Kivitye8332402007-12-09 18:43:00 +02001331 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001332
Avi Kivitye8332402007-12-09 18:43:00 +02001333 return nonpaging_map(vcpu, gva & PAGE_MASK,
1334 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001335}
1336
Joerg Roedelfb72d162008-02-07 13:47:44 +01001337static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
1338 u32 error_code)
1339{
Anthony Liguori35149e22008-04-02 14:46:56 -05001340 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001341 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001342 int largepage = 0;
1343 gfn_t gfn = gpa >> PAGE_SHIFT;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001344
1345 ASSERT(vcpu);
1346 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
1347
1348 r = mmu_topup_memory_caches(vcpu);
1349 if (r)
1350 return r;
1351
1352 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001353 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1354 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1355 largepage = 1;
1356 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001357 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03001358 up_read(&current->mm->mmap_sem);
Anthony Liguori35149e22008-04-02 14:46:56 -05001359 if (is_error_pfn(pfn)) {
1360 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001361 return 1;
1362 }
1363 spin_lock(&vcpu->kvm->mmu_lock);
1364 kvm_mmu_free_some_pages(vcpu);
1365 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Sheng Yang67253af2008-04-25 10:20:22 +08001366 largepage, gfn, pfn, kvm_x86_ops->get_tdp_level());
Joerg Roedelfb72d162008-02-07 13:47:44 +01001367 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001368
1369 return r;
1370}
1371
Avi Kivity6aa8b732006-12-10 02:21:36 -08001372static void nonpaging_free(struct kvm_vcpu *vcpu)
1373{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001374 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001375}
1376
1377static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1378{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001379 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001380
1381 context->new_cr3 = nonpaging_new_cr3;
1382 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001383 context->gva_to_gpa = nonpaging_gva_to_gpa;
1384 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001385 context->prefetch_page = nonpaging_prefetch_page;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001386 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001387 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001388 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001389 return 0;
1390}
1391
Avi Kivityd835dfe2007-11-21 02:57:59 +02001392void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001393{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001394 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001395 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001396}
1397
1398static void paging_new_cr3(struct kvm_vcpu *vcpu)
1399{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001400 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001401 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001402}
1403
Avi Kivity6aa8b732006-12-10 02:21:36 -08001404static void inject_page_fault(struct kvm_vcpu *vcpu,
1405 u64 addr,
1406 u32 err_code)
1407{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02001408 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001409}
1410
Avi Kivity6aa8b732006-12-10 02:21:36 -08001411static void paging_free(struct kvm_vcpu *vcpu)
1412{
1413 nonpaging_free(vcpu);
1414}
1415
1416#define PTTYPE 64
1417#include "paging_tmpl.h"
1418#undef PTTYPE
1419
1420#define PTTYPE 32
1421#include "paging_tmpl.h"
1422#undef PTTYPE
1423
Avi Kivity17ac10a2007-01-05 16:36:40 -08001424static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001425{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001426 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001427
1428 ASSERT(is_pae(vcpu));
1429 context->new_cr3 = paging_new_cr3;
1430 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001431 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02001432 context->prefetch_page = paging64_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001433 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001434 context->root_level = level;
1435 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001436 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001437 return 0;
1438}
1439
Avi Kivity17ac10a2007-01-05 16:36:40 -08001440static int paging64_init_context(struct kvm_vcpu *vcpu)
1441{
1442 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1443}
1444
Avi Kivity6aa8b732006-12-10 02:21:36 -08001445static int paging32_init_context(struct kvm_vcpu *vcpu)
1446{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001447 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001448
1449 context->new_cr3 = paging_new_cr3;
1450 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001451 context->gva_to_gpa = paging32_gva_to_gpa;
1452 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001453 context->prefetch_page = paging32_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001454 context->root_level = PT32_ROOT_LEVEL;
1455 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001456 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001457 return 0;
1458}
1459
1460static int paging32E_init_context(struct kvm_vcpu *vcpu)
1461{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001462 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001463}
1464
Joerg Roedelfb72d162008-02-07 13:47:44 +01001465static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
1466{
1467 struct kvm_mmu *context = &vcpu->arch.mmu;
1468
1469 context->new_cr3 = nonpaging_new_cr3;
1470 context->page_fault = tdp_page_fault;
1471 context->free = nonpaging_free;
1472 context->prefetch_page = nonpaging_prefetch_page;
Sheng Yang67253af2008-04-25 10:20:22 +08001473 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01001474 context->root_hpa = INVALID_PAGE;
1475
1476 if (!is_paging(vcpu)) {
1477 context->gva_to_gpa = nonpaging_gva_to_gpa;
1478 context->root_level = 0;
1479 } else if (is_long_mode(vcpu)) {
1480 context->gva_to_gpa = paging64_gva_to_gpa;
1481 context->root_level = PT64_ROOT_LEVEL;
1482 } else if (is_pae(vcpu)) {
1483 context->gva_to_gpa = paging64_gva_to_gpa;
1484 context->root_level = PT32E_ROOT_LEVEL;
1485 } else {
1486 context->gva_to_gpa = paging32_gva_to_gpa;
1487 context->root_level = PT32_ROOT_LEVEL;
1488 }
1489
1490 return 0;
1491}
1492
1493static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494{
1495 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001496 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001497
1498 if (!is_paging(vcpu))
1499 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001500 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001501 return paging64_init_context(vcpu);
1502 else if (is_pae(vcpu))
1503 return paging32E_init_context(vcpu);
1504 else
1505 return paging32_init_context(vcpu);
1506}
1507
Joerg Roedelfb72d162008-02-07 13:47:44 +01001508static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1509{
Anthony Liguori35149e22008-04-02 14:46:56 -05001510 vcpu->arch.update_pte.pfn = bad_pfn;
1511
Joerg Roedelfb72d162008-02-07 13:47:44 +01001512 if (tdp_enabled)
1513 return init_kvm_tdp_mmu(vcpu);
1514 else
1515 return init_kvm_softmmu(vcpu);
1516}
1517
Avi Kivity6aa8b732006-12-10 02:21:36 -08001518static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1519{
1520 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001521 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
1522 vcpu->arch.mmu.free(vcpu);
1523 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001524 }
1525}
1526
1527int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1528{
Avi Kivity17c3ba92007-06-04 15:58:30 +03001529 destroy_kvm_mmu(vcpu);
1530 return init_kvm_mmu(vcpu);
1531}
Eddie Dong8668a3c2007-10-10 14:26:45 +08001532EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001533
1534int kvm_mmu_load(struct kvm_vcpu *vcpu)
1535{
Avi Kivity714b93d2007-01-05 16:36:53 -08001536 int r;
1537
Avi Kivitye2dec932007-01-05 16:36:54 -08001538 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001539 if (r)
1540 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001541 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001542 kvm_mmu_free_some_pages(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001543 mmu_alloc_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001544 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001545 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001546 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001547out:
1548 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001549}
Avi Kivity17c3ba92007-06-04 15:58:30 +03001550EXPORT_SYMBOL_GPL(kvm_mmu_load);
1551
1552void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1553{
1554 mmu_free_roots(vcpu);
1555}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001556
Avi Kivity09072da2007-05-01 14:16:52 +03001557static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001558 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02001559 u64 *spte)
1560{
1561 u64 pte;
1562 struct kvm_mmu_page *child;
1563
1564 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02001565 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001566 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
1567 is_large_pte(pte))
Izik Eidus290fc382007-09-27 14:11:22 +02001568 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001569 else {
1570 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03001571 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001572 }
1573 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001574 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001575 if (is_large_pte(pte))
1576 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02001577}
1578
Avi Kivity00284252007-05-01 16:53:31 +03001579static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001580 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03001581 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02001582 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03001583{
Marcelo Tosatti30945382008-06-11 20:32:40 -03001584 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
1585 if (!vcpu->arch.update_pte.largepage ||
1586 sp->role.glevels == PT32_ROOT_LEVEL) {
1587 ++vcpu->kvm->stat.mmu_pde_zapped;
1588 return;
1589 }
1590 }
Avi Kivity00284252007-05-01 16:53:31 +03001591
Avi Kivity4cee5762007-11-18 16:37:07 +02001592 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02001593 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02001594 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03001595 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02001596 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03001597}
1598
Avi Kivity79539ce2007-11-21 02:06:21 +02001599static bool need_remote_flush(u64 old, u64 new)
1600{
1601 if (!is_shadow_present_pte(old))
1602 return false;
1603 if (!is_shadow_present_pte(new))
1604 return true;
1605 if ((old ^ new) & PT64_BASE_ADDR_MASK)
1606 return true;
1607 old ^= PT64_NX_MASK;
1608 new ^= PT64_NX_MASK;
1609 return (old & ~new & PT64_PERM_MASK) != 0;
1610}
1611
1612static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
1613{
1614 if (need_remote_flush(old, new))
1615 kvm_flush_remote_tlbs(vcpu->kvm);
1616 else
1617 kvm_mmu_flush_tlb(vcpu);
1618}
1619
Avi Kivity12b7d282007-09-23 14:10:49 +02001620static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1621{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001622 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02001623
Sheng Yang7b523452008-04-25 21:13:50 +08001624 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02001625}
1626
Avi Kivityd7824ff2007-12-30 12:29:05 +02001627static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1628 const u8 *new, int bytes)
1629{
1630 gfn_t gfn;
1631 int r;
1632 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001633 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02001634
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001635 vcpu->arch.update_pte.largepage = 0;
1636
Avi Kivityd7824ff2007-12-30 12:29:05 +02001637 if (bytes != 4 && bytes != 8)
1638 return;
1639
1640 /*
1641 * Assume that the pte write on a page table of the same type
1642 * as the current vcpu paging mode. This is nearly always true
1643 * (might be false while changing modes). Note it is verified later
1644 * by update_pte().
1645 */
1646 if (is_pae(vcpu)) {
1647 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
1648 if ((bytes == 4) && (gpa % 4 == 0)) {
1649 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
1650 if (r)
1651 return;
1652 memcpy((void *)&gpte + (gpa % 8), new, 4);
1653 } else if ((bytes == 8) && (gpa % 8 == 0)) {
1654 memcpy((void *)&gpte, new, 8);
1655 }
1656 } else {
1657 if ((bytes == 4) && (gpa % 4 == 0))
1658 memcpy((void *)&gpte, new, 4);
1659 }
1660 if (!is_present_pte(gpte))
1661 return;
1662 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001663
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001664 down_read(&current->mm->mmap_sem);
1665 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
1666 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1667 vcpu->arch.update_pte.largepage = 1;
1668 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001669 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001670 up_read(&current->mm->mmap_sem);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001671
Anthony Liguori35149e22008-04-02 14:46:56 -05001672 if (is_error_pfn(pfn)) {
1673 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001674 return;
1675 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02001676 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001677 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02001678}
1679
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001680static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
1681{
1682 u64 *spte = vcpu->arch.last_pte_updated;
1683
1684 if (spte
1685 && vcpu->arch.last_pte_gfn == gfn
1686 && shadow_accessed_mask
1687 && !(*spte & shadow_accessed_mask)
1688 && is_shadow_present_pte(*spte))
1689 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
1690}
1691
Avi Kivity09072da2007-05-01 14:16:52 +03001692void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life551882007-07-23 14:51:39 +08001693 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001694{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001695 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02001696 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001697 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001698 struct hlist_head *bucket;
1699 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02001700 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001701 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001702 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001703 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001704 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001705 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001706 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001707 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001708 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001709 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02001710 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001711
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001712 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02001713 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001714 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001715 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02001716 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02001717 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02001718 kvm_mmu_audit(vcpu, "pre pte write");
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001719 if (gfn == vcpu->arch.last_pt_write_gfn
Avi Kivity12b7d282007-09-23 14:10:49 +02001720 && !last_updated_pte_accessed(vcpu)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001721 ++vcpu->arch.last_pt_write_count;
1722 if (vcpu->arch.last_pt_write_count >= 3)
Avi Kivity86a5ba02007-01-05 16:36:50 -08001723 flooded = 1;
1724 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001725 vcpu->arch.last_pt_write_gfn = gfn;
1726 vcpu->arch.last_pt_write_count = 1;
1727 vcpu->arch.last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001728 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001729 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001730 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001731 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
1732 if (sp->gfn != gfn || sp->role.metaphysical)
Avi Kivity9b7a0322007-01-05 16:36:45 -08001733 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02001734 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001735 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001736 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001737 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001738 /*
1739 * Misaligned accesses are too much trouble to fix
1740 * up; also, they usually indicate a page is not used
1741 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001742 *
1743 * If we're seeing too many writes to a page,
1744 * it may no longer be a page table, or we may be
1745 * forking, in which case it is better to unmap the
1746 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001747 */
1748 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02001749 gpa, bytes, sp->role.word);
1750 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001751 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001752 continue;
1753 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001754 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02001755 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001756 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001757 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001758 page_offset <<= 1; /* 32->64 */
1759 /*
1760 * A 32-bit pde maps 4MB while the shadow pdes map
1761 * only 2MB. So we need to double the offset again
1762 * and zap two pdes instead of one.
1763 */
1764 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001765 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001766 page_offset <<= 1;
1767 npte = 2;
1768 }
Avi Kivityfce06572007-05-01 16:44:05 +03001769 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001770 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001771 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03001772 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001773 }
Avi Kivity4db35312007-11-21 15:28:32 +02001774 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02001775 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
1776 gentry = 0;
1777 r = kvm_read_guest_atomic(vcpu->kvm,
1778 gpa & ~(u64)(pte_size - 1),
1779 &gentry, pte_size);
1780 new = (const void *)&gentry;
1781 if (r < 0)
1782 new = NULL;
1783 }
Avi Kivityac1b7142007-03-08 17:13:32 +02001784 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02001785 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02001786 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02001787 if (new)
1788 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02001789 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001790 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001791 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001792 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001793 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001794 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05001795 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
1796 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
1797 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02001798 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08001799}
1800
Avi Kivitya4360362007-01-05 16:36:45 -08001801int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1802{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001803 gpa_t gpa;
1804 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08001805
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001806 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001807
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001808 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001809 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001810 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001811 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08001812}
1813
Avi Kivity22d95b12007-09-14 20:26:06 +03001814void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08001815{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001816 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02001817 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08001818
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001819 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02001820 struct kvm_mmu_page, link);
1821 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001822 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08001823 }
1824}
Avi Kivityebeace82007-01-05 16:36:47 -08001825
Avi Kivity30677142007-10-28 18:48:59 +02001826int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
1827{
1828 int r;
1829 enum emulation_result er;
1830
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001831 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02001832 if (r < 0)
1833 goto out;
1834
1835 if (!r) {
1836 r = 1;
1837 goto out;
1838 }
1839
Avi Kivityb733bfb2007-10-28 18:52:05 +02001840 r = mmu_topup_memory_caches(vcpu);
1841 if (r)
1842 goto out;
1843
Avi Kivity30677142007-10-28 18:48:59 +02001844 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02001845
1846 switch (er) {
1847 case EMULATE_DONE:
1848 return 1;
1849 case EMULATE_DO_MMIO:
1850 ++vcpu->stat.mmio_exits;
1851 return 0;
1852 case EMULATE_FAIL:
1853 kvm_report_emulation_failure(vcpu, "pagetable");
1854 return 1;
1855 default:
1856 BUG();
1857 }
1858out:
Avi Kivity30677142007-10-28 18:48:59 +02001859 return r;
1860}
1861EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
1862
Joerg Roedel18552672008-02-07 13:47:41 +01001863void kvm_enable_tdp(void)
1864{
1865 tdp_enabled = true;
1866}
1867EXPORT_SYMBOL_GPL(kvm_enable_tdp);
1868
Avi Kivity6aa8b732006-12-10 02:21:36 -08001869static void free_mmu_pages(struct kvm_vcpu *vcpu)
1870{
Avi Kivity4db35312007-11-21 15:28:32 +02001871 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001872
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001873 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
1874 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
Avi Kivity4db35312007-11-21 15:28:32 +02001875 struct kvm_mmu_page, link);
1876 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity8d2d73b2008-06-04 18:42:24 +03001877 cond_resched();
Avi Kivityf51234c2007-01-05 16:36:52 -08001878 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001879 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001880}
1881
1882static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1883{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001884 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001885 int i;
1886
1887 ASSERT(vcpu);
1888
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001889 if (vcpu->kvm->arch.n_requested_mmu_pages)
1890 vcpu->kvm->arch.n_free_mmu_pages =
1891 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001892 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001893 vcpu->kvm->arch.n_free_mmu_pages =
1894 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001895 /*
1896 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1897 * Therefore we need to allocate shadow page tables in the first
1898 * 4GB of memory, which happens to fit the DMA32 zone.
1899 */
1900 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1901 if (!page)
1902 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001903 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001904 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001905 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001906
Avi Kivity6aa8b732006-12-10 02:21:36 -08001907 return 0;
1908
1909error_1:
1910 free_mmu_pages(vcpu);
1911 return -ENOMEM;
1912}
1913
Ingo Molnar8018c272006-12-29 16:50:01 -08001914int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001915{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001916 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001917 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001918
Ingo Molnar8018c272006-12-29 16:50:01 -08001919 return alloc_mmu_pages(vcpu);
1920}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001921
Ingo Molnar8018c272006-12-29 16:50:01 -08001922int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1923{
1924 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001925 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08001926
Ingo Molnar8018c272006-12-29 16:50:01 -08001927 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001928}
1929
1930void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1931{
1932 ASSERT(vcpu);
1933
1934 destroy_kvm_mmu(vcpu);
1935 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001936 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001937}
1938
Avi Kivity90cb0522007-07-17 13:04:56 +03001939void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001940{
Avi Kivity4db35312007-11-21 15:28:32 +02001941 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001942
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001943 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001944 int i;
1945 u64 *pt;
1946
Avi Kivity4db35312007-11-21 15:28:32 +02001947 if (!test_bit(slot, &sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001948 continue;
1949
Avi Kivity4db35312007-11-21 15:28:32 +02001950 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001951 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1952 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02001953 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001954 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001955 }
1956}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001957
Avi Kivity90cb0522007-07-17 13:04:56 +03001958void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03001959{
Avi Kivity4db35312007-11-21 15:28:32 +02001960 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03001961
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001962 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001963 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Avi Kivity4db35312007-11-21 15:28:32 +02001964 kvm_mmu_zap_page(kvm, sp);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001965 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03001966
Avi Kivity90cb0522007-07-17 13:04:56 +03001967 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03001968}
1969
Harvey Harrison8b2cf732008-04-27 12:14:13 -07001970static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03001971{
1972 struct kvm_mmu_page *page;
1973
1974 page = container_of(kvm->arch.active_mmu_pages.prev,
1975 struct kvm_mmu_page, link);
1976 kvm_mmu_zap_page(kvm, page);
1977}
1978
1979static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
1980{
1981 struct kvm *kvm;
1982 struct kvm *kvm_freed = NULL;
1983 int cache_count = 0;
1984
1985 spin_lock(&kvm_lock);
1986
1987 list_for_each_entry(kvm, &vm_list, vm_list) {
1988 int npages;
1989
1990 spin_lock(&kvm->mmu_lock);
1991 npages = kvm->arch.n_alloc_mmu_pages -
1992 kvm->arch.n_free_mmu_pages;
1993 cache_count += npages;
1994 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
1995 kvm_mmu_remove_one_alloc_mmu_page(kvm);
1996 cache_count--;
1997 kvm_freed = kvm;
1998 }
1999 nr_to_scan--;
2000
2001 spin_unlock(&kvm->mmu_lock);
2002 }
2003 if (kvm_freed)
2004 list_move_tail(&kvm_freed->vm_list, &vm_list);
2005
2006 spin_unlock(&kvm_lock);
2007
2008 return cache_count;
2009}
2010
2011static struct shrinker mmu_shrinker = {
2012 .shrink = mmu_shrink,
2013 .seeks = DEFAULT_SEEKS * 10,
2014};
2015
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002016static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002017{
2018 if (pte_chain_cache)
2019 kmem_cache_destroy(pte_chain_cache);
2020 if (rmap_desc_cache)
2021 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002022 if (mmu_page_header_cache)
2023 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002024}
2025
Izik Eidus3ee16c82008-03-30 15:17:21 +03002026void kvm_mmu_module_exit(void)
2027{
2028 mmu_destroy_caches();
2029 unregister_shrinker(&mmu_shrinker);
2030}
2031
Avi Kivityb5a33a72007-04-15 16:31:09 +03002032int kvm_mmu_module_init(void)
2033{
2034 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2035 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002036 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002037 if (!pte_chain_cache)
2038 goto nomem;
2039 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2040 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002041 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002042 if (!rmap_desc_cache)
2043 goto nomem;
2044
Avi Kivityd3d25b02007-05-30 12:34:53 +03002045 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2046 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002047 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002048 if (!mmu_page_header_cache)
2049 goto nomem;
2050
Izik Eidus3ee16c82008-03-30 15:17:21 +03002051 register_shrinker(&mmu_shrinker);
2052
Avi Kivityb5a33a72007-04-15 16:31:09 +03002053 return 0;
2054
2055nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002056 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002057 return -ENOMEM;
2058}
2059
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002060/*
2061 * Caculate mmu pages needed for kvm.
2062 */
2063unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2064{
2065 int i;
2066 unsigned int nr_mmu_pages;
2067 unsigned int nr_pages = 0;
2068
2069 for (i = 0; i < kvm->nmemslots; i++)
2070 nr_pages += kvm->memslots[i].npages;
2071
2072 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2073 nr_mmu_pages = max(nr_mmu_pages,
2074 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2075
2076 return nr_mmu_pages;
2077}
2078
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002079static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2080 unsigned len)
2081{
2082 if (len > buffer->len)
2083 return NULL;
2084 return buffer->ptr;
2085}
2086
2087static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2088 unsigned len)
2089{
2090 void *ret;
2091
2092 ret = pv_mmu_peek_buffer(buffer, len);
2093 if (!ret)
2094 return ret;
2095 buffer->ptr += len;
2096 buffer->len -= len;
2097 buffer->processed += len;
2098 return ret;
2099}
2100
2101static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2102 gpa_t addr, gpa_t value)
2103{
2104 int bytes = 8;
2105 int r;
2106
2107 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2108 bytes = 4;
2109
2110 r = mmu_topup_memory_caches(vcpu);
2111 if (r)
2112 return r;
2113
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002114 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002115 return -EFAULT;
2116
2117 return 1;
2118}
2119
2120static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2121{
2122 kvm_x86_ops->tlb_flush(vcpu);
2123 return 1;
2124}
2125
2126static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2127{
2128 spin_lock(&vcpu->kvm->mmu_lock);
2129 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2130 spin_unlock(&vcpu->kvm->mmu_lock);
2131 return 1;
2132}
2133
2134static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2135 struct kvm_pv_mmu_op_buffer *buffer)
2136{
2137 struct kvm_mmu_op_header *header;
2138
2139 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2140 if (!header)
2141 return 0;
2142 switch (header->op) {
2143 case KVM_MMU_OP_WRITE_PTE: {
2144 struct kvm_mmu_op_write_pte *wpte;
2145
2146 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2147 if (!wpte)
2148 return 0;
2149 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2150 wpte->pte_val);
2151 }
2152 case KVM_MMU_OP_FLUSH_TLB: {
2153 struct kvm_mmu_op_flush_tlb *ftlb;
2154
2155 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2156 if (!ftlb)
2157 return 0;
2158 return kvm_pv_mmu_flush_tlb(vcpu);
2159 }
2160 case KVM_MMU_OP_RELEASE_PT: {
2161 struct kvm_mmu_op_release_pt *rpt;
2162
2163 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2164 if (!rpt)
2165 return 0;
2166 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2167 }
2168 default: return 0;
2169 }
2170}
2171
2172int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
2173 gpa_t addr, unsigned long *ret)
2174{
2175 int r;
2176 struct kvm_pv_mmu_op_buffer buffer;
2177
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002178 buffer.ptr = buffer.buf;
2179 buffer.len = min_t(unsigned long, bytes, sizeof buffer.buf);
2180 buffer.processed = 0;
2181
2182 r = kvm_read_guest(vcpu->kvm, addr, buffer.buf, buffer.len);
2183 if (r)
2184 goto out;
2185
2186 while (buffer.len) {
2187 r = kvm_pv_mmu_op_one(vcpu, &buffer);
2188 if (r < 0)
2189 goto out;
2190 if (r == 0)
2191 break;
2192 }
2193
2194 r = 1;
2195out:
2196 *ret = buffer.processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002197 return r;
2198}
2199
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002200#ifdef AUDIT
2201
2202static const char *audit_msg;
2203
2204static gva_t canonicalize(gva_t gva)
2205{
2206#ifdef CONFIG_X86_64
2207 gva = (long long)(gva << 16) >> 16;
2208#endif
2209 return gva;
2210}
2211
2212static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
2213 gva_t va, int level)
2214{
2215 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
2216 int i;
2217 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
2218
2219 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
2220 u64 ent = pt[i];
2221
Avi Kivityc7addb92007-09-16 18:58:32 +02002222 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002223 continue;
2224
2225 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02002226 if (level > 1) {
2227 if (ent == shadow_notrap_nonpresent_pte)
2228 printk(KERN_ERR "audit: (%s) nontrapping pte"
2229 " in nonleaf level: levels %d gva %lx"
2230 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002231 vcpu->arch.mmu.root_level, va, level, ent);
Avi Kivityc7addb92007-09-16 18:58:32 +02002232
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002233 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02002234 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002235 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05002236 hpa_t hpa = (hpa_t)gpa_to_pfn(vcpu, gpa) << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002237
Avi Kivityc7addb92007-09-16 18:58:32 +02002238 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002239 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02002240 printk(KERN_ERR "xx audit error: (%s) levels %d"
2241 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002242 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04002243 va, gpa, hpa, ent,
2244 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02002245 else if (ent == shadow_notrap_nonpresent_pte
2246 && !is_error_hpa(hpa))
2247 printk(KERN_ERR "audit: (%s) notrap shadow,"
2248 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05002249 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02002250
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002251 }
2252 }
2253}
2254
2255static void audit_mappings(struct kvm_vcpu *vcpu)
2256{
Avi Kivity1ea252a2007-03-08 11:48:09 +02002257 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002258
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002259 if (vcpu->arch.mmu.root_level == 4)
2260 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002261 else
2262 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002263 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002264 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002265 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002266 i << 30,
2267 2);
2268}
2269
2270static int count_rmaps(struct kvm_vcpu *vcpu)
2271{
2272 int nmaps = 0;
2273 int i, j, k;
2274
2275 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
2276 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
2277 struct kvm_rmap_desc *d;
2278
2279 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02002280 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002281
Izik Eidus290fc382007-09-27 14:11:22 +02002282 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002283 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02002284 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002285 ++nmaps;
2286 continue;
2287 }
Izik Eidus290fc382007-09-27 14:11:22 +02002288 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002289 while (d) {
2290 for (k = 0; k < RMAP_EXT; ++k)
2291 if (d->shadow_ptes[k])
2292 ++nmaps;
2293 else
2294 break;
2295 d = d->more;
2296 }
2297 }
2298 }
2299 return nmaps;
2300}
2301
2302static int count_writable_mappings(struct kvm_vcpu *vcpu)
2303{
2304 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02002305 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002306 int i;
2307
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002308 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02002309 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002310
Avi Kivity4db35312007-11-21 15:28:32 +02002311 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002312 continue;
2313
2314 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
2315 u64 ent = pt[i];
2316
2317 if (!(ent & PT_PRESENT_MASK))
2318 continue;
2319 if (!(ent & PT_WRITABLE_MASK))
2320 continue;
2321 ++nmaps;
2322 }
2323 }
2324 return nmaps;
2325}
2326
2327static void audit_rmap(struct kvm_vcpu *vcpu)
2328{
2329 int n_rmap = count_rmaps(vcpu);
2330 int n_actual = count_writable_mappings(vcpu);
2331
2332 if (n_rmap != n_actual)
2333 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002334 __func__, audit_msg, n_rmap, n_actual);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002335}
2336
2337static void audit_write_protection(struct kvm_vcpu *vcpu)
2338{
Avi Kivity4db35312007-11-21 15:28:32 +02002339 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02002340 struct kvm_memory_slot *slot;
2341 unsigned long *rmapp;
2342 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002343
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002344 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02002345 if (sp->role.metaphysical)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002346 continue;
2347
Avi Kivity4db35312007-11-21 15:28:32 +02002348 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
2349 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02002350 rmapp = &slot->rmap[gfn - slot->base_gfn];
2351 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002352 printk(KERN_ERR "%s: (%s) shadow page has writable"
2353 " mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002354 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02002355 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002356 }
2357}
2358
2359static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
2360{
2361 int olddbg = dbg;
2362
2363 dbg = 0;
2364 audit_msg = msg;
2365 audit_rmap(vcpu);
2366 audit_write_protection(vcpu);
2367 audit_mappings(vcpu);
2368 dbg = olddbg;
2369}
2370
2371#endif