blob: 685a4ffac8e6a66cf3241775768c175a90d2962a [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
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity6de4f3a2009-05-31 22:58:47 +030021#include "kvm_cache_regs.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>
Eduardo Habkost13673a92008-11-17 19:03:13 -020036#include <asm/vmx.h>
Avi Kivitye4956062007-06-28 14:15:57 -040037
Joerg Roedel18552672008-02-07 13:47:41 +010038/*
39 * When setting this variable to true it enables Two-Dimensional-Paging
40 * where the hardware walks 2 page tables:
41 * 1. the guest-virtual to guest-physical
42 * 2. while doing 1. it walks guest-physical to host-physical
43 * If the hardware supports that we don't need to do shadow paging.
44 */
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050045bool tdp_enabled = false;
Joerg Roedel18552672008-02-07 13:47:41 +010046
Avi Kivity37a7d8b2007-01-05 16:36:56 -080047#undef MMU_DEBUG
48
49#undef AUDIT
50
51#ifdef AUDIT
52static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
53#else
54static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
55#endif
56
57#ifdef MMU_DEBUG
58
59#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
60#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
61
62#else
63
64#define pgprintk(x...) do { } while (0)
65#define rmap_printk(x...) do { } while (0)
66
67#endif
68
69#if defined(MMU_DEBUG) || defined(AUDIT)
Avi Kivity6ada8cc2008-06-22 16:45:24 +030070static int dbg = 0;
71module_param(dbg, bool, 0644);
Avi Kivity37a7d8b2007-01-05 16:36:56 -080072#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080073
Marcelo Tosatti582801a2008-09-23 13:18:41 -030074static int oos_shadow = 1;
75module_param(oos_shadow, bool, 0644);
76
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080077#ifndef MMU_DEBUG
78#define ASSERT(x) do { } while (0)
79#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080080#define ASSERT(x) \
81 if (!(x)) { \
82 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
83 __FILE__, __LINE__, #x); \
84 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080085#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080086
Avi Kivity6aa8b732006-12-10 02:21:36 -080087#define PT_FIRST_AVAIL_BITS_SHIFT 9
88#define PT64_SECOND_AVAIL_BITS_SHIFT 52
89
Avi Kivity6aa8b732006-12-10 02:21:36 -080090#define VALID_PAGE(x) ((x) != INVALID_PAGE)
91
92#define PT64_LEVEL_BITS 9
93
94#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -040095 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080096
97#define PT64_LEVEL_MASK(level) \
98 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
99
100#define PT64_INDEX(address, level)\
101 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
102
103
104#define PT32_LEVEL_BITS 10
105
106#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400107 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800108
109#define PT32_LEVEL_MASK(level) \
110 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
Joerg Roedele04da982009-07-27 16:30:45 +0200111#define PT32_LVL_OFFSET_MASK(level) \
112 (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
113 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800114
115#define PT32_INDEX(address, level)\
116 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
117
118
Avi Kivity27aba762007-03-09 13:04:31 +0200119#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800120#define PT64_DIR_BASE_ADDR_MASK \
121 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200122#define PT64_LVL_ADDR_MASK(level) \
123 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
124 * PT64_LEVEL_BITS))) - 1))
125#define PT64_LVL_OFFSET_MASK(level) \
126 (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
127 * PT64_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800128
129#define PT32_BASE_ADDR_MASK PAGE_MASK
130#define PT32_DIR_BASE_ADDR_MASK \
131 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200132#define PT32_LVL_ADDR_MASK(level) \
133 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
134 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800135
Avi Kivity79539ce2007-11-21 02:06:21 +0200136#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
137 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800138
139#define PFERR_PRESENT_MASK (1U << 0)
140#define PFERR_WRITE_MASK (1U << 1)
141#define PFERR_USER_MASK (1U << 2)
Dong, Eddie82725b22009-03-30 16:21:08 +0800142#define PFERR_RSVD_MASK (1U << 3)
Avi Kivity73b10872007-01-26 00:56:41 -0800143#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800144
Joerg Roedele04da982009-07-27 16:30:45 +0200145#define PT_PDPE_LEVEL 3
Avi Kivity6aa8b732006-12-10 02:21:36 -0800146#define PT_DIRECTORY_LEVEL 2
147#define PT_PAGE_TABLE_LEVEL 1
148
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800149#define RMAP_EXT 4
150
Avi Kivityfe135d22007-12-09 16:15:46 +0200151#define ACC_EXEC_MASK 1
152#define ACC_WRITE_MASK PT_WRITABLE_MASK
153#define ACC_USER_MASK PT_USER_MASK
154#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
155
Avi Kivity07420172009-07-06 12:21:32 +0300156#define CREATE_TRACE_POINTS
157#include "mmutrace.h"
158
Izik Eidus14032832009-09-23 21:47:17 +0300159#define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
160
Avi Kivity135f8c22008-08-21 17:49:56 +0300161#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
162
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800163struct kvm_rmap_desc {
Avi Kivityd555c332009-06-10 14:24:23 +0300164 u64 *sptes[RMAP_EXT];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800165 struct kvm_rmap_desc *more;
166};
167
Avi Kivity2d111232008-12-25 14:39:47 +0200168struct kvm_shadow_walk_iterator {
169 u64 addr;
170 hpa_t shadow_addr;
171 int level;
172 u64 *sptep;
173 unsigned index;
174};
175
176#define for_each_shadow_entry(_vcpu, _addr, _walker) \
177 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
178 shadow_walk_okay(&(_walker)); \
179 shadow_walk_next(&(_walker)))
180
181
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300182struct kvm_unsync_walk {
183 int (*entry) (struct kvm_mmu_page *sp, struct kvm_unsync_walk *walk);
184};
185
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300186typedef int (*mmu_parent_walk_fn) (struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp);
187
Avi Kivityb5a33a72007-04-15 16:31:09 +0300188static struct kmem_cache *pte_chain_cache;
189static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300190static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300191
Avi Kivityc7addb92007-09-16 18:58:32 +0200192static u64 __read_mostly shadow_trap_nonpresent_pte;
193static u64 __read_mostly shadow_notrap_nonpresent_pte;
Sheng Yang7b523452008-04-25 21:13:50 +0800194static u64 __read_mostly shadow_base_present_pte;
195static u64 __read_mostly shadow_nx_mask;
196static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
197static u64 __read_mostly shadow_user_mask;
198static u64 __read_mostly shadow_accessed_mask;
199static u64 __read_mostly shadow_dirty_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200200
Dong, Eddie82725b22009-03-30 16:21:08 +0800201static inline u64 rsvd_bits(int s, int e)
202{
203 return ((1ULL << (e - s + 1)) - 1) << s;
204}
205
Avi Kivityc7addb92007-09-16 18:58:32 +0200206void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
207{
208 shadow_trap_nonpresent_pte = trap_pte;
209 shadow_notrap_nonpresent_pte = notrap_pte;
210}
211EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
212
Sheng Yang7b523452008-04-25 21:13:50 +0800213void kvm_mmu_set_base_ptes(u64 base_pte)
214{
215 shadow_base_present_pte = base_pte;
216}
217EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
218
219void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang4b12f0d2009-04-27 20:35:42 +0800220 u64 dirty_mask, u64 nx_mask, u64 x_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800221{
222 shadow_user_mask = user_mask;
223 shadow_accessed_mask = accessed_mask;
224 shadow_dirty_mask = dirty_mask;
225 shadow_nx_mask = nx_mask;
226 shadow_x_mask = x_mask;
227}
228EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
229
Avi Kivity6aa8b732006-12-10 02:21:36 -0800230static int is_write_protection(struct kvm_vcpu *vcpu)
231{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800232 return vcpu->arch.cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800233}
234
235static int is_cpuid_PSE36(void)
236{
237 return 1;
238}
239
Avi Kivity73b10872007-01-26 00:56:41 -0800240static int is_nx(struct kvm_vcpu *vcpu)
241{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800242 return vcpu->arch.shadow_efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800243}
244
Avi Kivityc7addb92007-09-16 18:58:32 +0200245static int is_shadow_present_pte(u64 pte)
246{
Avi Kivityc7addb92007-09-16 18:58:32 +0200247 return pte != shadow_trap_nonpresent_pte
248 && pte != shadow_notrap_nonpresent_pte;
249}
250
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300251static int is_large_pte(u64 pte)
252{
253 return pte & PT_PAGE_SIZE_MASK;
254}
255
Avi Kivity6aa8b732006-12-10 02:21:36 -0800256static int is_writeble_pte(unsigned long pte)
257{
258 return pte & PT_WRITABLE_MASK;
259}
260
Avi Kivity43a37952009-06-10 14:12:05 +0300261static int is_dirty_gpte(unsigned long pte)
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200262{
Avi Kivity439e2182009-06-10 12:56:54 +0300263 return pte & PT_DIRTY_MASK;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200264}
265
Avi Kivity43a37952009-06-10 14:12:05 +0300266static int is_rmap_spte(u64 pte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800267{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200268 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800269}
270
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300271static int is_last_spte(u64 pte, int level)
272{
273 if (level == PT_PAGE_TABLE_LEVEL)
274 return 1;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200275 if (is_large_pte(pte))
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300276 return 1;
277 return 0;
278}
279
Anthony Liguori35149e22008-04-02 14:46:56 -0500280static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200281{
Anthony Liguori35149e22008-04-02 14:46:56 -0500282 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200283}
284
Avi Kivityda9285212007-11-21 13:54:47 +0200285static gfn_t pse36_gfn_delta(u32 gpte)
286{
287 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
288
289 return (gpte & PT32_DIR_PSE36_MASK) << shift;
290}
291
Avi Kivityd555c332009-06-10 14:24:23 +0300292static void __set_spte(u64 *sptep, u64 spte)
Avi Kivitye663ee62007-05-31 15:46:04 +0300293{
294#ifdef CONFIG_X86_64
295 set_64bit((unsigned long *)sptep, spte);
296#else
297 set_64bit((unsigned long long *)sptep, spte);
298#endif
299}
300
Avi Kivitye2dec932007-01-05 16:36:54 -0800301static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300302 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800303{
304 void *obj;
305
306 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800307 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800308 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300309 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800310 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800311 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800312 cache->objects[cache->nobjs++] = obj;
313 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800314 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800315}
316
317static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
318{
319 while (mc->nobjs)
320 kfree(mc->objects[--mc->nobjs]);
321}
322
Avi Kivityc1158e62007-07-20 08:18:27 +0300323static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300324 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300325{
326 struct page *page;
327
328 if (cache->nobjs >= min)
329 return 0;
330 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300331 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300332 if (!page)
333 return -ENOMEM;
334 set_page_private(page, 0);
335 cache->objects[cache->nobjs++] = page_address(page);
336 }
337 return 0;
338}
339
340static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
341{
342 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300343 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300344}
345
Avi Kivity8c438502007-04-16 11:53:17 +0300346static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
347{
348 int r;
349
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800350 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300351 pte_chain_cache, 4);
352 if (r)
353 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800354 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Marcelo Tosattic41ef342008-10-28 18:16:58 -0200355 rmap_desc_cache, 4);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300356 if (r)
357 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800358 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300359 if (r)
360 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800361 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300362 mmu_page_header_cache, 4);
363out:
Avi Kivity8c438502007-04-16 11:53:17 +0300364 return r;
365}
366
Avi Kivity714b93d2007-01-05 16:36:53 -0800367static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
368{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800369 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
370 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
371 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
372 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800373}
374
375static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
376 size_t size)
377{
378 void *p;
379
380 BUG_ON(!mc->nobjs);
381 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800382 return p;
383}
384
Avi Kivity714b93d2007-01-05 16:36:53 -0800385static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
386{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800387 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800388 sizeof(struct kvm_pte_chain));
389}
390
Avi Kivity90cb0522007-07-17 13:04:56 +0300391static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800392{
Avi Kivity90cb0522007-07-17 13:04:56 +0300393 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800394}
395
396static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
397{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800398 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800399 sizeof(struct kvm_rmap_desc));
400}
401
Avi Kivity90cb0522007-07-17 13:04:56 +0300402static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800403{
Avi Kivity90cb0522007-07-17 13:04:56 +0300404 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800405}
406
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800407/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300408 * Return the pointer to the largepage write count for a given
409 * gfn, handling slots that are not large page aligned.
410 */
Joerg Roedeld25797b2009-07-27 16:30:43 +0200411static int *slot_largepage_idx(gfn_t gfn,
412 struct kvm_memory_slot *slot,
413 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300414{
415 unsigned long idx;
416
Joerg Roedeld25797b2009-07-27 16:30:43 +0200417 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
418 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
419 return &slot->lpage_info[level - 2][idx].write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300420}
421
422static void account_shadowed(struct kvm *kvm, gfn_t gfn)
423{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200424 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300425 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200426 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300427
Izik Eidus28430992008-10-03 17:40:32 +0300428 gfn = unalias_gfn(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200429
430 slot = gfn_to_memslot_unaliased(kvm, gfn);
431 for (i = PT_DIRECTORY_LEVEL;
432 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
433 write_count = slot_largepage_idx(gfn, slot, i);
434 *write_count += 1;
435 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300436}
437
438static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
439{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200440 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300441 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200442 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300443
Izik Eidus28430992008-10-03 17:40:32 +0300444 gfn = unalias_gfn(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200445 for (i = PT_DIRECTORY_LEVEL;
446 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
447 slot = gfn_to_memslot_unaliased(kvm, gfn);
448 write_count = slot_largepage_idx(gfn, slot, i);
449 *write_count -= 1;
450 WARN_ON(*write_count < 0);
451 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300452}
453
Joerg Roedeld25797b2009-07-27 16:30:43 +0200454static int has_wrprotected_page(struct kvm *kvm,
455 gfn_t gfn,
456 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300457{
Izik Eidus28430992008-10-03 17:40:32 +0300458 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300459 int *largepage_idx;
460
Izik Eidus28430992008-10-03 17:40:32 +0300461 gfn = unalias_gfn(kvm, gfn);
462 slot = gfn_to_memslot_unaliased(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300463 if (slot) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200464 largepage_idx = slot_largepage_idx(gfn, slot, level);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300465 return *largepage_idx;
466 }
467
468 return 1;
469}
470
Joerg Roedeld25797b2009-07-27 16:30:43 +0200471static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300472{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200473 unsigned long page_size = PAGE_SIZE;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300474 struct vm_area_struct *vma;
475 unsigned long addr;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200476 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300477
478 addr = gfn_to_hva(kvm, gfn);
479 if (kvm_is_error_hva(addr))
Joerg Roedeld25797b2009-07-27 16:30:43 +0200480 return page_size;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300481
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300482 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300483 vma = find_vma(current->mm, addr);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200484 if (!vma)
485 goto out;
486
487 page_size = vma_kernel_pagesize(vma);
488
489out:
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300490 up_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300491
Joerg Roedeld25797b2009-07-27 16:30:43 +0200492 for (i = PT_PAGE_TABLE_LEVEL;
493 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
494 if (page_size >= KVM_HPAGE_SIZE(i))
495 ret = i;
496 else
497 break;
498 }
499
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300500 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300501}
502
Joerg Roedeld25797b2009-07-27 16:30:43 +0200503static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300504{
505 struct kvm_memory_slot *slot;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200506 int host_level;
507 int level = PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300508
509 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
510 if (slot && slot->dirty_bitmap)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200511 return PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300512
Joerg Roedeld25797b2009-07-27 16:30:43 +0200513 host_level = host_mapping_level(vcpu->kvm, large_gfn);
514
515 if (host_level == PT_PAGE_TABLE_LEVEL)
516 return host_level;
517
518 for (level = PT_DIRECTORY_LEVEL; level <= host_level; ++level) {
519
520 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
521 break;
522 }
523
524 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300525}
526
527/*
Izik Eidus290fc382007-09-27 14:11:22 +0200528 * Take gfn and return the reverse mapping to it.
529 * Note: gfn must be unaliased before this function get called
530 */
531
Joerg Roedel44ad9942009-07-27 16:30:42 +0200532static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
Izik Eidus290fc382007-09-27 14:11:22 +0200533{
534 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300535 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200536
537 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200538 if (likely(level == PT_PAGE_TABLE_LEVEL))
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300539 return &slot->rmap[gfn - slot->base_gfn];
540
Joerg Roedel44ad9942009-07-27 16:30:42 +0200541 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
542 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300543
Joerg Roedel44ad9942009-07-27 16:30:42 +0200544 return &slot->lpage_info[level - 2][idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200545}
546
547/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800548 * Reverse mapping data structures:
549 *
Izik Eidus290fc382007-09-27 14:11:22 +0200550 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
551 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800552 *
Izik Eidus290fc382007-09-27 14:11:22 +0200553 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
554 * containing more mappings.
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300555 *
556 * Returns the number of rmap entries before the spte was added or zero if
557 * the spte was not added.
558 *
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800559 */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200560static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800561{
Avi Kivity4db35312007-11-21 15:28:32 +0200562 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800563 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200564 unsigned long *rmapp;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300565 int i, count = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800566
Avi Kivity43a37952009-06-10 14:12:05 +0300567 if (!is_rmap_spte(*spte))
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300568 return count;
Izik Eidus290fc382007-09-27 14:11:22 +0200569 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200570 sp = page_header(__pa(spte));
571 sp->gfns[spte - sp->spt] = gfn;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200572 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200573 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800574 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200575 *rmapp = (unsigned long)spte;
576 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800577 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800578 desc = mmu_alloc_rmap_desc(vcpu);
Avi Kivityd555c332009-06-10 14:24:23 +0300579 desc->sptes[0] = (u64 *)*rmapp;
580 desc->sptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200581 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800582 } else {
583 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200584 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivityd555c332009-06-10 14:24:23 +0300585 while (desc->sptes[RMAP_EXT-1] && desc->more) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800586 desc = desc->more;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300587 count += RMAP_EXT;
588 }
Avi Kivityd555c332009-06-10 14:24:23 +0300589 if (desc->sptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800590 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800591 desc = desc->more;
592 }
Avi Kivityd555c332009-06-10 14:24:23 +0300593 for (i = 0; desc->sptes[i]; ++i)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800594 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300595 desc->sptes[i] = spte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800596 }
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300597 return count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800598}
599
Izik Eidus290fc382007-09-27 14:11:22 +0200600static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800601 struct kvm_rmap_desc *desc,
602 int i,
603 struct kvm_rmap_desc *prev_desc)
604{
605 int j;
606
Avi Kivityd555c332009-06-10 14:24:23 +0300607 for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800608 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300609 desc->sptes[i] = desc->sptes[j];
610 desc->sptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800611 if (j != 0)
612 return;
613 if (!prev_desc && !desc->more)
Avi Kivityd555c332009-06-10 14:24:23 +0300614 *rmapp = (unsigned long)desc->sptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800615 else
616 if (prev_desc)
617 prev_desc->more = desc->more;
618 else
Izik Eidus290fc382007-09-27 14:11:22 +0200619 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300620 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800621}
622
Izik Eidus290fc382007-09-27 14:11:22 +0200623static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800624{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800625 struct kvm_rmap_desc *desc;
626 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200627 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500628 pfn_t pfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200629 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800630 int i;
631
Avi Kivity43a37952009-06-10 14:12:05 +0300632 if (!is_rmap_spte(*spte))
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800633 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200634 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500635 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800636 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500637 kvm_set_pfn_accessed(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200638 if (is_writeble_pte(*spte))
Izik Eidusacb66dd2009-09-23 21:47:16 +0300639 kvm_set_pfn_dirty(pfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200640 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200641 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800642 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
643 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200644 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800645 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200646 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800647 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
648 spte, *spte);
649 BUG();
650 }
Izik Eidus290fc382007-09-27 14:11:22 +0200651 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800652 } else {
653 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200654 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800655 prev_desc = NULL;
656 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300657 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
658 if (desc->sptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200659 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800660 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800661 prev_desc);
662 return;
663 }
664 prev_desc = desc;
665 desc = desc->more;
666 }
667 BUG();
668 }
669}
670
Izik Eidus98348e92007-10-16 14:42:30 +0200671static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800672{
Avi Kivity374cbac2007-01-05 16:36:43 -0800673 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200674 struct kvm_rmap_desc *prev_desc;
675 u64 *prev_spte;
676 int i;
677
678 if (!*rmapp)
679 return NULL;
680 else if (!(*rmapp & 1)) {
681 if (!spte)
682 return (u64 *)*rmapp;
683 return NULL;
684 }
685 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
686 prev_desc = NULL;
687 prev_spte = NULL;
688 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300689 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
Izik Eidus98348e92007-10-16 14:42:30 +0200690 if (prev_spte == spte)
Avi Kivityd555c332009-06-10 14:24:23 +0300691 return desc->sptes[i];
692 prev_spte = desc->sptes[i];
Izik Eidus98348e92007-10-16 14:42:30 +0200693 }
694 desc = desc->more;
695 }
696 return NULL;
697}
698
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200699static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200700{
Izik Eidus290fc382007-09-27 14:11:22 +0200701 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800702 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200703 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800704
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500705 gfn = unalias_gfn(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200706 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
Avi Kivity374cbac2007-01-05 16:36:43 -0800707
Izik Eidus98348e92007-10-16 14:42:30 +0200708 spte = rmap_next(kvm, rmapp, NULL);
709 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800710 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800711 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800712 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800713 if (is_writeble_pte(*spte)) {
Avi Kivityd555c332009-06-10 14:24:23 +0300714 __set_spte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800715 write_protected = 1;
716 }
Izik Eidus9647c142007-10-16 14:43:46 +0200717 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800718 }
Izik Eidus855149a2008-03-20 18:17:24 +0200719 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500720 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200721
722 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500723 pfn = spte_to_pfn(*spte);
724 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200725 }
726
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300727 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200728 for (i = PT_DIRECTORY_LEVEL;
729 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
730 rmapp = gfn_to_rmap(kvm, gfn, i);
731 spte = rmap_next(kvm, rmapp, NULL);
732 while (spte) {
733 BUG_ON(!spte);
734 BUG_ON(!(*spte & PT_PRESENT_MASK));
735 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
736 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
737 if (is_writeble_pte(*spte)) {
738 rmap_remove(kvm, spte);
739 --kvm->stat.lpages;
740 __set_spte(spte, shadow_trap_nonpresent_pte);
741 spte = NULL;
742 write_protected = 1;
743 }
744 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300745 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300746 }
747
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200748 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800749}
750
Izik Eidus3da0dd42009-09-23 21:47:18 +0300751static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp, u64 data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200752{
753 u64 *spte;
754 int need_tlb_flush = 0;
755
756 while ((spte = rmap_next(kvm, rmapp, NULL))) {
757 BUG_ON(!(*spte & PT_PRESENT_MASK));
758 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
759 rmap_remove(kvm, spte);
Avi Kivityd555c332009-06-10 14:24:23 +0300760 __set_spte(spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200761 need_tlb_flush = 1;
762 }
763 return need_tlb_flush;
764}
765
Izik Eidus3da0dd42009-09-23 21:47:18 +0300766static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp, u64 data)
767{
768 int need_flush = 0;
769 u64 *spte, new_spte;
770 pte_t *ptep = (pte_t *)data;
771 pfn_t new_pfn;
772
773 WARN_ON(pte_huge(*ptep));
774 new_pfn = pte_pfn(*ptep);
775 spte = rmap_next(kvm, rmapp, NULL);
776 while (spte) {
777 BUG_ON(!is_shadow_present_pte(*spte));
778 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
779 need_flush = 1;
780 if (pte_write(*ptep)) {
781 rmap_remove(kvm, spte);
782 __set_spte(spte, shadow_trap_nonpresent_pte);
783 spte = rmap_next(kvm, rmapp, NULL);
784 } else {
785 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
786 new_spte |= (u64)new_pfn << PAGE_SHIFT;
787
788 new_spte &= ~PT_WRITABLE_MASK;
789 new_spte &= ~SPTE_HOST_WRITEABLE;
790 if (is_writeble_pte(*spte))
791 kvm_set_pfn_dirty(spte_to_pfn(*spte));
792 __set_spte(spte, new_spte);
793 spte = rmap_next(kvm, rmapp, spte);
794 }
795 }
796 if (need_flush)
797 kvm_flush_remote_tlbs(kvm);
798
799 return 0;
800}
801
802static int kvm_handle_hva(struct kvm *kvm, unsigned long hva, u64 data,
803 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
804 u64 data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200805{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200806 int i, j;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200807 int retval = 0;
808
809 /*
810 * If mmap_sem isn't taken, we can look the memslots with only
811 * the mmu_lock by skipping over the slots with userspace_addr == 0.
812 */
813 for (i = 0; i < kvm->nmemslots; i++) {
814 struct kvm_memory_slot *memslot = &kvm->memslots[i];
815 unsigned long start = memslot->userspace_addr;
816 unsigned long end;
817
818 /* mmu_lock protects userspace_addr */
819 if (!start)
820 continue;
821
822 end = start + (memslot->npages << PAGE_SHIFT);
823 if (hva >= start && hva < end) {
824 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200825
Izik Eidus3da0dd42009-09-23 21:47:18 +0300826 retval |= handler(kvm, &memslot->rmap[gfn_offset],
827 data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200828
829 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
830 int idx = gfn_offset;
831 idx /= KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL + j);
832 retval |= handler(kvm,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300833 &memslot->lpage_info[j][idx].rmap_pde,
834 data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200835 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200836 }
837 }
838
839 return retval;
840}
841
842int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
843{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300844 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200845}
846
Izik Eidus3da0dd42009-09-23 21:47:18 +0300847void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
848{
849 kvm_handle_hva(kvm, hva, (u64)&pte, kvm_set_pte_rmapp);
850}
851
852static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp, u64 data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200853{
854 u64 *spte;
855 int young = 0;
856
Sheng Yang534e38b2008-09-08 15:12:30 +0800857 /* always return old for EPT */
858 if (!shadow_accessed_mask)
859 return 0;
860
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200861 spte = rmap_next(kvm, rmapp, NULL);
862 while (spte) {
863 int _young;
864 u64 _spte = *spte;
865 BUG_ON(!(_spte & PT_PRESENT_MASK));
866 _young = _spte & PT_ACCESSED_MASK;
867 if (_young) {
868 young = 1;
869 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
870 }
871 spte = rmap_next(kvm, rmapp, spte);
872 }
873 return young;
874}
875
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300876#define RMAP_RECYCLE_THRESHOLD 1000
877
Joerg Roedel852e3c12009-07-27 16:30:44 +0200878static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300879{
880 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200881 struct kvm_mmu_page *sp;
882
883 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300884
885 gfn = unalias_gfn(vcpu->kvm, gfn);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200886 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300887
Izik Eidus3da0dd42009-09-23 21:47:18 +0300888 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300889 kvm_flush_remote_tlbs(vcpu->kvm);
890}
891
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200892int kvm_age_hva(struct kvm *kvm, unsigned long hva)
893{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300894 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200895}
896
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800897#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300898static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899{
Avi Kivity139bdb22007-01-05 16:36:50 -0800900 u64 *pos;
901 u64 *end;
902
Avi Kivity47ad8e62007-05-06 15:50:58 +0300903 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300904 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800905 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800906 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800907 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800908 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800909 return 1;
910}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800911#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912
Avi Kivity4db35312007-11-21 15:28:32 +0200913static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800914{
Avi Kivity4db35312007-11-21 15:28:32 +0200915 ASSERT(is_empty_shadow_page(sp->spt));
916 list_del(&sp->link);
917 __free_page(virt_to_page(sp->spt));
918 __free_page(virt_to_page(sp->gfns));
919 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800920 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800921}
922
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800923static unsigned kvm_page_table_hashfn(gfn_t gfn)
924{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200925 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800926}
927
Avi Kivity25c0de22007-01-05 16:36:42 -0800928static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
929 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800930{
Avi Kivity4db35312007-11-21 15:28:32 +0200931 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800932
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800933 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
934 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
935 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200936 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800937 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -0200938 INIT_LIST_HEAD(&sp->oos_link);
Sheng Yang291f26b2008-10-16 17:30:57 +0800939 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200940 sp->multimapped = 0;
941 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800942 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200943 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944}
945
Avi Kivity714b93d2007-01-05 16:36:53 -0800946static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200947 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800948{
949 struct kvm_pte_chain *pte_chain;
950 struct hlist_node *node;
951 int i;
952
953 if (!parent_pte)
954 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200955 if (!sp->multimapped) {
956 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800957
958 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200959 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800960 return;
961 }
Avi Kivity4db35312007-11-21 15:28:32 +0200962 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800963 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200964 INIT_HLIST_HEAD(&sp->parent_ptes);
965 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800966 pte_chain->parent_ptes[0] = old;
967 }
Avi Kivity4db35312007-11-21 15:28:32 +0200968 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800969 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
970 continue;
971 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
972 if (!pte_chain->parent_ptes[i]) {
973 pte_chain->parent_ptes[i] = parent_pte;
974 return;
975 }
976 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800977 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800978 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200979 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800980 pte_chain->parent_ptes[0] = parent_pte;
981}
982
Avi Kivity4db35312007-11-21 15:28:32 +0200983static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800984 u64 *parent_pte)
985{
986 struct kvm_pte_chain *pte_chain;
987 struct hlist_node *node;
988 int i;
989
Avi Kivity4db35312007-11-21 15:28:32 +0200990 if (!sp->multimapped) {
991 BUG_ON(sp->parent_pte != parent_pte);
992 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800993 return;
994 }
Avi Kivity4db35312007-11-21 15:28:32 +0200995 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800996 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
997 if (!pte_chain->parent_ptes[i])
998 break;
999 if (pte_chain->parent_ptes[i] != parent_pte)
1000 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001001 while (i + 1 < NR_PTE_CHAIN_ENTRIES
1002 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001003 pte_chain->parent_ptes[i]
1004 = pte_chain->parent_ptes[i + 1];
1005 ++i;
1006 }
1007 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001008 if (i == 0) {
1009 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001010 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +02001011 if (hlist_empty(&sp->parent_ptes)) {
1012 sp->multimapped = 0;
1013 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001014 }
1015 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001016 return;
1017 }
1018 BUG();
1019}
1020
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001021
1022static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1023 mmu_parent_walk_fn fn)
1024{
1025 struct kvm_pte_chain *pte_chain;
1026 struct hlist_node *node;
1027 struct kvm_mmu_page *parent_sp;
1028 int i;
1029
1030 if (!sp->multimapped && sp->parent_pte) {
1031 parent_sp = page_header(__pa(sp->parent_pte));
1032 fn(vcpu, parent_sp);
1033 mmu_parent_walk(vcpu, parent_sp, fn);
1034 return;
1035 }
1036 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1037 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1038 if (!pte_chain->parent_ptes[i])
1039 break;
1040 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
1041 fn(vcpu, parent_sp);
1042 mmu_parent_walk(vcpu, parent_sp, fn);
1043 }
1044}
1045
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001046static void kvm_mmu_update_unsync_bitmap(u64 *spte)
1047{
1048 unsigned int index;
1049 struct kvm_mmu_page *sp = page_header(__pa(spte));
1050
1051 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001052 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
1053 sp->unsync_children++;
1054 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001055}
1056
1057static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
1058{
1059 struct kvm_pte_chain *pte_chain;
1060 struct hlist_node *node;
1061 int i;
1062
1063 if (!sp->parent_pte)
1064 return;
1065
1066 if (!sp->multimapped) {
1067 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
1068 return;
1069 }
1070
1071 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1072 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1073 if (!pte_chain->parent_ptes[i])
1074 break;
1075 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
1076 }
1077}
1078
1079static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1080{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001081 kvm_mmu_update_parents_unsync(sp);
1082 return 1;
1083}
1084
1085static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
1086 struct kvm_mmu_page *sp)
1087{
1088 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
1089 kvm_mmu_update_parents_unsync(sp);
1090}
1091
Avi Kivityd761a502008-05-29 14:55:03 +03001092static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1093 struct kvm_mmu_page *sp)
1094{
1095 int i;
1096
1097 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1098 sp->spt[i] = shadow_trap_nonpresent_pte;
1099}
1100
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001101static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
1102 struct kvm_mmu_page *sp)
1103{
1104 return 1;
1105}
1106
Marcelo Tosattia7052892008-09-23 13:18:35 -03001107static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1108{
1109}
1110
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001111#define KVM_PAGE_ARRAY_NR 16
1112
1113struct kvm_mmu_pages {
1114 struct mmu_page_and_offset {
1115 struct kvm_mmu_page *sp;
1116 unsigned int idx;
1117 } page[KVM_PAGE_ARRAY_NR];
1118 unsigned int nr;
1119};
1120
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001121#define for_each_unsync_children(bitmap, idx) \
1122 for (idx = find_first_bit(bitmap, 512); \
1123 idx < 512; \
1124 idx = find_next_bit(bitmap, 512, idx+1))
1125
Hannes Edercded19f2009-02-21 02:19:13 +01001126static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1127 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001128{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001129 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001130
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001131 if (sp->unsync)
1132 for (i=0; i < pvec->nr; i++)
1133 if (pvec->page[i].sp == sp)
1134 return 0;
1135
1136 pvec->page[pvec->nr].sp = sp;
1137 pvec->page[pvec->nr].idx = idx;
1138 pvec->nr++;
1139 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1140}
1141
1142static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1143 struct kvm_mmu_pages *pvec)
1144{
1145 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001146
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001147 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001148 u64 ent = sp->spt[i];
1149
Marcelo Tosatti87917232008-12-22 18:49:30 -02001150 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001151 struct kvm_mmu_page *child;
1152 child = page_header(ent & PT64_BASE_ADDR_MASK);
1153
1154 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001155 if (mmu_pages_add(pvec, child, i))
1156 return -ENOSPC;
1157
1158 ret = __mmu_unsync_walk(child, pvec);
1159 if (!ret)
1160 __clear_bit(i, sp->unsync_child_bitmap);
1161 else if (ret > 0)
1162 nr_unsync_leaf += ret;
1163 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001164 return ret;
1165 }
1166
1167 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001168 nr_unsync_leaf++;
1169 if (mmu_pages_add(pvec, child, i))
1170 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001171 }
1172 }
1173 }
1174
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001175 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001176 sp->unsync_children = 0;
1177
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001178 return nr_unsync_leaf;
1179}
1180
1181static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1182 struct kvm_mmu_pages *pvec)
1183{
1184 if (!sp->unsync_children)
1185 return 0;
1186
1187 mmu_pages_add(pvec, sp, 0);
1188 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001189}
1190
Avi Kivity4db35312007-11-21 15:28:32 +02001191static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001192{
1193 unsigned index;
1194 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001195 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001196 struct hlist_node *node;
1197
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001198 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001199 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001200 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001201 hlist_for_each_entry(sp, node, bucket, hash_link)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001202 if (sp->gfn == gfn && !sp->role.direct
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001203 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001204 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001205 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001206 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001207 }
1208 return NULL;
1209}
1210
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001211static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1212{
1213 WARN_ON(!sp->unsync);
1214 sp->unsync = 0;
1215 --kvm->stat.mmu_unsync;
1216}
1217
1218static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1219
1220static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1221{
1222 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1223 kvm_mmu_zap_page(vcpu->kvm, sp);
1224 return 1;
1225 }
1226
Avi Kivityf691fe12009-07-06 15:58:14 +03001227 trace_kvm_mmu_sync_page(sp);
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001228 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1229 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001230 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001231 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1232 kvm_mmu_zap_page(vcpu->kvm, sp);
1233 return 1;
1234 }
1235
1236 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001237 return 0;
1238}
1239
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001240struct mmu_page_path {
1241 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1242 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001243};
1244
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001245#define for_each_sp(pvec, sp, parents, i) \
1246 for (i = mmu_pages_next(&pvec, &parents, -1), \
1247 sp = pvec.page[i].sp; \
1248 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1249 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001250
Hannes Edercded19f2009-02-21 02:19:13 +01001251static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1252 struct mmu_page_path *parents,
1253 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001254{
1255 int n;
1256
1257 for (n = i+1; n < pvec->nr; n++) {
1258 struct kvm_mmu_page *sp = pvec->page[n].sp;
1259
1260 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1261 parents->idx[0] = pvec->page[n].idx;
1262 return n;
1263 }
1264
1265 parents->parent[sp->role.level-2] = sp;
1266 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1267 }
1268
1269 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001270}
1271
Hannes Edercded19f2009-02-21 02:19:13 +01001272static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001273{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001274 struct kvm_mmu_page *sp;
1275 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001276
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001277 do {
1278 unsigned int idx = parents->idx[level];
1279
1280 sp = parents->parent[level];
1281 if (!sp)
1282 return;
1283
1284 --sp->unsync_children;
1285 WARN_ON((int)sp->unsync_children < 0);
1286 __clear_bit(idx, sp->unsync_child_bitmap);
1287 level++;
1288 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1289}
1290
1291static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1292 struct mmu_page_path *parents,
1293 struct kvm_mmu_pages *pvec)
1294{
1295 parents->parent[parent->role.level-1] = NULL;
1296 pvec->nr = 0;
1297}
1298
1299static void mmu_sync_children(struct kvm_vcpu *vcpu,
1300 struct kvm_mmu_page *parent)
1301{
1302 int i;
1303 struct kvm_mmu_page *sp;
1304 struct mmu_page_path parents;
1305 struct kvm_mmu_pages pages;
1306
1307 kvm_mmu_pages_init(parent, &parents, &pages);
1308 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001309 int protected = 0;
1310
1311 for_each_sp(pages, sp, parents, i)
1312 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1313
1314 if (protected)
1315 kvm_flush_remote_tlbs(vcpu->kvm);
1316
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001317 for_each_sp(pages, sp, parents, i) {
1318 kvm_sync_page(vcpu, sp);
1319 mmu_pages_clear_parents(&parents);
1320 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001321 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001322 kvm_mmu_pages_init(parent, &parents, &pages);
1323 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001324}
1325
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001326static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1327 gfn_t gfn,
1328 gva_t gaddr,
1329 unsigned level,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001330 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001331 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001332 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001333{
1334 union kvm_mmu_page_role role;
1335 unsigned index;
1336 unsigned quadrant;
1337 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001338 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001339 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001340
Avi Kivitya770f6f2008-12-21 19:20:09 +02001341 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001342 role.level = level;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001343 role.direct = direct;
Avi Kivity41074d02007-12-09 17:00:02 +02001344 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001345 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001346 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1347 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1348 role.quadrant = quadrant;
1349 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001350 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001351 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001352 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1353 if (sp->gfn == gfn) {
1354 if (sp->unsync)
1355 if (kvm_sync_page(vcpu, sp))
1356 continue;
1357
1358 if (sp->role.word != role.word)
1359 continue;
1360
Avi Kivity4db35312007-11-21 15:28:32 +02001361 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001362 if (sp->unsync_children) {
1363 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1364 kvm_mmu_mark_parents_unsync(vcpu, sp);
1365 }
Avi Kivityf691fe12009-07-06 15:58:14 +03001366 trace_kvm_mmu_get_page(sp, false);
Avi Kivity4db35312007-11-21 15:28:32 +02001367 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001368 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001369 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001370 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1371 if (!sp)
1372 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001373 sp->gfn = gfn;
1374 sp->role = role;
1375 hlist_add_head(&sp->hash_link, bucket);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001376 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001377 if (rmap_write_protect(vcpu->kvm, gfn))
1378 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001379 account_shadowed(vcpu->kvm, gfn);
1380 }
Avi Kivity131d8272008-05-29 14:56:28 +03001381 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1382 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1383 else
1384 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001385 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001386 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001387}
1388
Avi Kivity2d111232008-12-25 14:39:47 +02001389static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1390 struct kvm_vcpu *vcpu, u64 addr)
1391{
1392 iterator->addr = addr;
1393 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1394 iterator->level = vcpu->arch.mmu.shadow_root_level;
1395 if (iterator->level == PT32E_ROOT_LEVEL) {
1396 iterator->shadow_addr
1397 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1398 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1399 --iterator->level;
1400 if (!iterator->shadow_addr)
1401 iterator->level = 0;
1402 }
1403}
1404
1405static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1406{
1407 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1408 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001409
1410 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1411 if (is_large_pte(*iterator->sptep))
1412 return false;
1413
Avi Kivity2d111232008-12-25 14:39:47 +02001414 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1415 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1416 return true;
1417}
1418
1419static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1420{
1421 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1422 --iterator->level;
1423}
1424
Avi Kivity90cb0522007-07-17 13:04:56 +03001425static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001426 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001427{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001428 unsigned i;
1429 u64 *pt;
1430 u64 ent;
1431
Avi Kivity4db35312007-11-21 15:28:32 +02001432 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001433
Avi Kivity697fe2e2007-01-05 16:36:46 -08001434 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1435 ent = pt[i];
1436
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001437 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001438 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001439 ent &= PT64_BASE_ADDR_MASK;
1440 mmu_page_remove_parent_pte(page_header(ent),
1441 &pt[i]);
1442 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001443 if (is_large_pte(ent))
1444 --kvm->stat.lpages;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001445 rmap_remove(kvm, &pt[i]);
1446 }
1447 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001448 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001449 }
Avi Kivitya4360362007-01-05 16:36:45 -08001450}
1451
Avi Kivity4db35312007-11-21 15:28:32 +02001452static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001453{
Avi Kivity4db35312007-11-21 15:28:32 +02001454 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001455}
1456
Avi Kivity12b7d282007-09-23 14:10:49 +02001457static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1458{
1459 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001460 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001461
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001462 kvm_for_each_vcpu(i, vcpu, kvm)
1463 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001464}
1465
Avi Kivity31aa2b42008-07-11 17:59:46 +03001466static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001467{
1468 u64 *parent_pte;
1469
Avi Kivity4db35312007-11-21 15:28:32 +02001470 while (sp->multimapped || sp->parent_pte) {
1471 if (!sp->multimapped)
1472 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001473 else {
1474 struct kvm_pte_chain *chain;
1475
Avi Kivity4db35312007-11-21 15:28:32 +02001476 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001477 struct kvm_pte_chain, link);
1478 parent_pte = chain->parent_ptes[0];
1479 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001480 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001481 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001482 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001483 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001484}
1485
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001486static int mmu_zap_unsync_children(struct kvm *kvm,
1487 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001488{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001489 int i, zapped = 0;
1490 struct mmu_page_path parents;
1491 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001492
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001493 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001494 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001495
1496 kvm_mmu_pages_init(parent, &parents, &pages);
1497 while (mmu_unsync_walk(parent, &pages)) {
1498 struct kvm_mmu_page *sp;
1499
1500 for_each_sp(pages, sp, parents, i) {
1501 kvm_mmu_zap_page(kvm, sp);
1502 mmu_pages_clear_parents(&parents);
1503 }
1504 zapped += pages.nr;
1505 kvm_mmu_pages_init(parent, &parents, &pages);
1506 }
1507
1508 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001509}
1510
Marcelo Tosatti07385412008-09-23 13:18:37 -03001511static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001512{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001513 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001514
1515 trace_kvm_mmu_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001516 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001517 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001518 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001519 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001520 kvm_flush_remote_tlbs(kvm);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001521 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001522 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001523 if (sp->unsync)
1524 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001525 if (!sp->root_count) {
1526 hlist_del(&sp->hash_link);
1527 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001528 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001529 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001530 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001531 kvm_reload_remote_mmus(kvm);
1532 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001533 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001534 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001535}
1536
Izik Eidus82ce2c92007-10-02 18:52:55 +02001537/*
1538 * Changing the number of mmu pages allocated to the vm
1539 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1540 */
1541void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1542{
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001543 int used_pages;
1544
1545 used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1546 used_pages = max(0, used_pages);
1547
Izik Eidus82ce2c92007-10-02 18:52:55 +02001548 /*
1549 * If we set the number of mmu pages to be smaller be than the
1550 * number of actived pages , we must to free some mmu pages before we
1551 * change the value
1552 */
1553
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001554 if (used_pages > kvm_nr_mmu_pages) {
1555 while (used_pages > kvm_nr_mmu_pages) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001556 struct kvm_mmu_page *page;
1557
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001558 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001559 struct kvm_mmu_page, link);
1560 kvm_mmu_zap_page(kvm, page);
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001561 used_pages--;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001562 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001563 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001564 }
1565 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001566 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1567 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001568
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001569 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001570}
1571
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001572static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001573{
1574 unsigned index;
1575 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001576 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001577 struct hlist_node *node, *n;
1578 int r;
1579
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001580 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001581 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001582 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001583 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001584 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001585 if (sp->gfn == gfn && !sp->role.direct) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001586 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001587 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001588 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001589 if (kvm_mmu_zap_page(kvm, sp))
1590 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001591 }
1592 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001593}
1594
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001595static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001596{
Avi Kivity4677a3b2009-01-06 13:00:27 +02001597 unsigned index;
1598 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001599 struct kvm_mmu_page *sp;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001600 struct hlist_node *node, *nn;
Avi Kivity97a0a012007-05-31 15:08:29 +03001601
Avi Kivity4677a3b2009-01-06 13:00:27 +02001602 index = kvm_page_table_hashfn(gfn);
1603 bucket = &kvm->arch.mmu_page_hash[index];
1604 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001605 if (sp->gfn == gfn && !sp->role.direct
Avi Kivity4677a3b2009-01-06 13:00:27 +02001606 && !sp->role.invalid) {
1607 pgprintk("%s: zap %lx %x\n",
1608 __func__, gfn, sp->role.word);
1609 kvm_mmu_zap_page(kvm, sp);
1610 }
Avi Kivity97a0a012007-05-31 15:08:29 +03001611 }
1612}
1613
Avi Kivity38c335f2007-11-21 14:20:22 +02001614static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001615{
Avi Kivity38c335f2007-11-21 14:20:22 +02001616 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001617 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001618
Sheng Yang291f26b2008-10-16 17:30:57 +08001619 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001620}
1621
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001622static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1623{
1624 int i;
1625 u64 *pt = sp->spt;
1626
1627 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1628 return;
1629
1630 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1631 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001632 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001633 }
1634}
1635
Avi Kivity039576c2007-03-20 12:46:50 +02001636struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1637{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001638 struct page *page;
1639
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001640 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001641
1642 if (gpa == UNMAPPED_GVA)
1643 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001644
Izik Eidus72dc67a2008-02-10 18:04:15 +02001645 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001646
1647 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001648}
1649
Sheng Yang74be52e2008-10-09 16:01:56 +08001650/*
1651 * The function is based on mtrr_type_lookup() in
1652 * arch/x86/kernel/cpu/mtrr/generic.c
1653 */
1654static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1655 u64 start, u64 end)
1656{
1657 int i;
1658 u64 base, mask;
1659 u8 prev_match, curr_match;
1660 int num_var_ranges = KVM_NR_VAR_MTRR;
1661
1662 if (!mtrr_state->enabled)
1663 return 0xFF;
1664
1665 /* Make end inclusive end, instead of exclusive */
1666 end--;
1667
1668 /* Look in fixed ranges. Just return the type as per start */
1669 if (mtrr_state->have_fixed && (start < 0x100000)) {
1670 int idx;
1671
1672 if (start < 0x80000) {
1673 idx = 0;
1674 idx += (start >> 16);
1675 return mtrr_state->fixed_ranges[idx];
1676 } else if (start < 0xC0000) {
1677 idx = 1 * 8;
1678 idx += ((start - 0x80000) >> 14);
1679 return mtrr_state->fixed_ranges[idx];
1680 } else if (start < 0x1000000) {
1681 idx = 3 * 8;
1682 idx += ((start - 0xC0000) >> 12);
1683 return mtrr_state->fixed_ranges[idx];
1684 }
1685 }
1686
1687 /*
1688 * Look in variable ranges
1689 * Look of multiple ranges matching this address and pick type
1690 * as per MTRR precedence
1691 */
1692 if (!(mtrr_state->enabled & 2))
1693 return mtrr_state->def_type;
1694
1695 prev_match = 0xFF;
1696 for (i = 0; i < num_var_ranges; ++i) {
1697 unsigned short start_state, end_state;
1698
1699 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1700 continue;
1701
1702 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1703 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1704 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1705 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1706
1707 start_state = ((start & mask) == (base & mask));
1708 end_state = ((end & mask) == (base & mask));
1709 if (start_state != end_state)
1710 return 0xFE;
1711
1712 if ((start & mask) != (base & mask))
1713 continue;
1714
1715 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1716 if (prev_match == 0xFF) {
1717 prev_match = curr_match;
1718 continue;
1719 }
1720
1721 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1722 curr_match == MTRR_TYPE_UNCACHABLE)
1723 return MTRR_TYPE_UNCACHABLE;
1724
1725 if ((prev_match == MTRR_TYPE_WRBACK &&
1726 curr_match == MTRR_TYPE_WRTHROUGH) ||
1727 (prev_match == MTRR_TYPE_WRTHROUGH &&
1728 curr_match == MTRR_TYPE_WRBACK)) {
1729 prev_match = MTRR_TYPE_WRTHROUGH;
1730 curr_match = MTRR_TYPE_WRTHROUGH;
1731 }
1732
1733 if (prev_match != curr_match)
1734 return MTRR_TYPE_UNCACHABLE;
1735 }
1736
1737 if (prev_match != 0xFF)
1738 return prev_match;
1739
1740 return mtrr_state->def_type;
1741}
1742
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001743u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001744{
1745 u8 mtrr;
1746
1747 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1748 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1749 if (mtrr == 0xfe || mtrr == 0xff)
1750 mtrr = MTRR_TYPE_WRBACK;
1751 return mtrr;
1752}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001753EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001754
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001755static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1756{
1757 unsigned index;
1758 struct hlist_head *bucket;
1759 struct kvm_mmu_page *s;
1760 struct hlist_node *node, *n;
1761
Avi Kivityf691fe12009-07-06 15:58:14 +03001762 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001763 index = kvm_page_table_hashfn(sp->gfn);
1764 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1765 /* don't unsync if pagetable is shadowed with multiple roles */
1766 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001767 if (s->gfn != sp->gfn || s->role.direct)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001768 continue;
1769 if (s->role.word != sp->role.word)
1770 return 1;
1771 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001772 ++vcpu->kvm->stat.mmu_unsync;
1773 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001774
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001775 kvm_mmu_mark_parents_unsync(vcpu, sp);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001776
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001777 mmu_convert_notrap(sp);
1778 return 0;
1779}
1780
1781static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1782 bool can_unsync)
1783{
1784 struct kvm_mmu_page *shadow;
1785
1786 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1787 if (shadow) {
1788 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1789 return 1;
1790 if (shadow->unsync)
1791 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001792 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001793 return kvm_unsync_page(vcpu, shadow);
1794 return 1;
1795 }
1796 return 0;
1797}
1798
Avi Kivityd555c332009-06-10 14:24:23 +03001799static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001800 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001801 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001802 gfn_t gfn, pfn_t pfn, bool speculative,
Izik Eidus14032832009-09-23 21:47:17 +03001803 bool can_unsync, bool reset_host_protection)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001804{
1805 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001806 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001807
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001808 /*
1809 * We don't set the accessed bit, since we sometimes want to see
1810 * whether the guest actually used the pte (in order to detect
1811 * demand paging).
1812 */
Sheng Yang7b523452008-04-25 21:13:50 +08001813 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001814 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001815 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001816 if (!dirty)
1817 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001818 if (pte_access & ACC_EXEC_MASK)
1819 spte |= shadow_x_mask;
1820 else
1821 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001822 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001823 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001824 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001825 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001826 if (tdp_enabled)
1827 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1828 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001829
Izik Eidus14032832009-09-23 21:47:17 +03001830 if (reset_host_protection)
1831 spte |= SPTE_HOST_WRITEABLE;
1832
Anthony Liguori35149e22008-04-02 14:46:56 -05001833 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001834
1835 if ((pte_access & ACC_WRITE_MASK)
1836 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001837
Joerg Roedel852e3c12009-07-27 16:30:44 +02001838 if (level > PT_PAGE_TABLE_LEVEL &&
1839 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001840 ret = 1;
1841 spte = shadow_trap_nonpresent_pte;
1842 goto set_pte;
1843 }
1844
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001845 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001846
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001847 /*
1848 * Optimization: for pte sync, if spte was writable the hash
1849 * lookup is unnecessary (and expensive). Write protection
1850 * is responsibility of mmu_get_page / kvm_sync_page.
1851 * Same reasoning can be applied to dirty page accounting.
1852 */
Avi Kivityd555c332009-06-10 14:24:23 +03001853 if (!can_unsync && is_writeble_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001854 goto set_pte;
1855
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001856 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001857 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001858 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001859 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001860 pte_access &= ~ACC_WRITE_MASK;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001861 if (is_writeble_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001862 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001863 }
1864 }
1865
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001866 if (pte_access & ACC_WRITE_MASK)
1867 mark_page_dirty(vcpu->kvm, gfn);
1868
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001869set_pte:
Avi Kivityd555c332009-06-10 14:24:23 +03001870 __set_spte(sptep, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001871 return ret;
1872}
1873
Avi Kivityd555c332009-06-10 14:24:23 +03001874static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001875 unsigned pt_access, unsigned pte_access,
1876 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001877 int *ptwrite, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03001878 pfn_t pfn, bool speculative,
1879 bool reset_host_protection)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001880{
1881 int was_rmapped = 0;
Avi Kivityd555c332009-06-10 14:24:23 +03001882 int was_writeble = is_writeble_pte(*sptep);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001883 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001884
1885 pgprintk("%s: spte %llx access %x write_fault %d"
1886 " user_fault %d gfn %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001887 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001888 write_fault, user_fault, gfn);
1889
Avi Kivityd555c332009-06-10 14:24:23 +03001890 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001891 /*
1892 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1893 * the parent of the now unreachable PTE.
1894 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02001895 if (level > PT_PAGE_TABLE_LEVEL &&
1896 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001897 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03001898 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001899
1900 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03001901 mmu_page_remove_parent_pte(child, sptep);
1902 } else if (pfn != spte_to_pfn(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001903 pgprintk("hfn old %lx new %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001904 spte_to_pfn(*sptep), pfn);
1905 rmap_remove(vcpu->kvm, sptep);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001906 } else
1907 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001908 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02001909
Avi Kivityd555c332009-06-10 14:24:23 +03001910 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Izik Eidus14032832009-09-23 21:47:17 +03001911 dirty, level, gfn, pfn, speculative, true,
1912 reset_host_protection)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001913 if (write_fault)
1914 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001915 kvm_x86_ops->tlb_flush(vcpu);
1916 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001917
Avi Kivityd555c332009-06-10 14:24:23 +03001918 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001919 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001920 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02001921 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
1922 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03001923 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001924 ++vcpu->kvm->stat.lpages;
1925
Avi Kivityd555c332009-06-10 14:24:23 +03001926 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001927 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02001928 rmap_count = rmap_add(vcpu, sptep, gfn);
Izik Eidusacb66dd2009-09-23 21:47:16 +03001929 kvm_release_pfn_clean(pfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001930 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02001931 rmap_recycle(vcpu, sptep, gfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001932 } else {
1933 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001934 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001935 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001936 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001937 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001938 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03001939 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001940 vcpu->arch.last_pte_gfn = gfn;
1941 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001942}
1943
Avi Kivity6aa8b732006-12-10 02:21:36 -08001944static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1945{
1946}
1947
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001948static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001949 int level, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001950{
Avi Kivity9f652d212008-12-25 14:54:25 +02001951 struct kvm_shadow_walk_iterator iterator;
1952 struct kvm_mmu_page *sp;
1953 int pt_write = 0;
1954 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001955
Avi Kivity9f652d212008-12-25 14:54:25 +02001956 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02001957 if (iterator.level == level) {
Avi Kivity9f652d212008-12-25 14:54:25 +02001958 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1959 0, write, 1, &pt_write,
Izik Eidus14032832009-09-23 21:47:17 +03001960 level, gfn, pfn, false, true);
Avi Kivity9f652d212008-12-25 14:54:25 +02001961 ++vcpu->stat.pf_fixed;
1962 break;
1963 }
1964
1965 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1966 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1967 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1968 iterator.level - 1,
1969 1, ACC_ALL, iterator.sptep);
1970 if (!sp) {
1971 pgprintk("nonpaging_map: ENOMEM\n");
1972 kvm_release_pfn_clean(pfn);
1973 return -ENOMEM;
1974 }
1975
Avi Kivityd555c332009-06-10 14:24:23 +03001976 __set_spte(iterator.sptep,
1977 __pa(sp->spt)
1978 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1979 | shadow_user_mask | shadow_x_mask);
Avi Kivity9f652d212008-12-25 14:54:25 +02001980 }
1981 }
1982 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001983}
1984
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001985static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1986{
1987 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001988 int level;
Anthony Liguori35149e22008-04-02 14:46:56 -05001989 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001990 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001991
Joerg Roedel852e3c12009-07-27 16:30:44 +02001992 level = mapping_level(vcpu, gfn);
1993
1994 /*
1995 * This path builds a PAE pagetable - so we can map 2mb pages at
1996 * maximum. Therefore check if the level is larger than that.
1997 */
1998 if (level > PT_DIRECTORY_LEVEL)
1999 level = PT_DIRECTORY_LEVEL;
2000
2001 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002002
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002003 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002004 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002005 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002006
Avi Kivityd196e342008-01-24 11:44:11 +02002007 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05002008 if (is_error_pfn(pfn)) {
2009 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002010 return 1;
2011 }
2012
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002013 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002014 if (mmu_notifier_retry(vcpu, mmu_seq))
2015 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02002016 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel852e3c12009-07-27 16:30:44 +02002017 r = __direct_map(vcpu, v, write, level, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002018 spin_unlock(&vcpu->kvm->mmu_lock);
2019
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002020
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002021 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002022
2023out_unlock:
2024 spin_unlock(&vcpu->kvm->mmu_lock);
2025 kvm_release_pfn_clean(pfn);
2026 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002027}
2028
2029
Avi Kivity17ac10a2007-01-05 16:36:40 -08002030static void mmu_free_roots(struct kvm_vcpu *vcpu)
2031{
2032 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002033 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002034
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002035 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002036 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002037 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002038 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2039 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002040
Avi Kivity4db35312007-11-21 15:28:32 +02002041 sp = page_header(root);
2042 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002043 if (!sp->root_count && sp->role.invalid)
2044 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002045 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002046 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002047 return;
2048 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002049 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002050 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002051
Avi Kivity417726a2007-04-12 17:35:58 +03002052 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002053 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002054 sp = page_header(root);
2055 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002056 if (!sp->root_count && sp->role.invalid)
2057 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03002058 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002059 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002060 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002061 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002062 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002063}
2064
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002065static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2066{
2067 int ret = 0;
2068
2069 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
2070 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
2071 ret = 1;
2072 }
2073
2074 return ret;
2075}
2076
2077static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002078{
2079 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002080 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02002081 struct kvm_mmu_page *sp;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002082 int direct = 0;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002083 u64 pdptr;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002084
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002085 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002086
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002087 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2088 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002089
2090 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01002091 if (tdp_enabled)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002092 direct = 1;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002093 if (mmu_check_root(vcpu, root_gfn))
2094 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002095 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002096 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01002097 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002098 root = __pa(sp->spt);
2099 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002100 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002101 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002102 }
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002103 direct = !is_paging(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002104 if (tdp_enabled)
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002105 direct = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002106 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002107 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002108
2109 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002110 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002111 pdptr = kvm_pdptr_read(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002112 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002113 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002114 continue;
2115 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002116 root_gfn = pdptr >> PAGE_SHIFT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002117 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002118 root_gfn = 0;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002119 if (mmu_check_root(vcpu, root_gfn))
2120 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002121 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002122 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002123 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002124 root = __pa(sp->spt);
2125 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002126 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002127 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002128 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002129 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002130}
2131
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002132static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2133{
2134 int i;
2135 struct kvm_mmu_page *sp;
2136
2137 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2138 return;
2139 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2140 hpa_t root = vcpu->arch.mmu.root_hpa;
2141 sp = page_header(root);
2142 mmu_sync_children(vcpu, sp);
2143 return;
2144 }
2145 for (i = 0; i < 4; ++i) {
2146 hpa_t root = vcpu->arch.mmu.pae_root[i];
2147
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002148 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002149 root &= PT64_BASE_ADDR_MASK;
2150 sp = page_header(root);
2151 mmu_sync_children(vcpu, sp);
2152 }
2153 }
2154}
2155
2156void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2157{
2158 spin_lock(&vcpu->kvm->mmu_lock);
2159 mmu_sync_roots(vcpu);
2160 spin_unlock(&vcpu->kvm->mmu_lock);
2161}
2162
Avi Kivity6aa8b732006-12-10 02:21:36 -08002163static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2164{
2165 return vaddr;
2166}
2167
2168static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002169 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002170{
Avi Kivitye8332402007-12-09 18:43:00 +02002171 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002172 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002173
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002174 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002175 r = mmu_topup_memory_caches(vcpu);
2176 if (r)
2177 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002178
Avi Kivity6aa8b732006-12-10 02:21:36 -08002179 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002180 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002181
Avi Kivitye8332402007-12-09 18:43:00 +02002182 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002183
Avi Kivitye8332402007-12-09 18:43:00 +02002184 return nonpaging_map(vcpu, gva & PAGE_MASK,
2185 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002186}
2187
Joerg Roedelfb72d162008-02-07 13:47:44 +01002188static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2189 u32 error_code)
2190{
Anthony Liguori35149e22008-04-02 14:46:56 -05002191 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002192 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002193 int level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002194 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002195 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002196
2197 ASSERT(vcpu);
2198 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2199
2200 r = mmu_topup_memory_caches(vcpu);
2201 if (r)
2202 return r;
2203
Joerg Roedel852e3c12009-07-27 16:30:44 +02002204 level = mapping_level(vcpu, gfn);
2205
2206 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2207
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002208 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002209 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002210 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002211 if (is_error_pfn(pfn)) {
2212 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002213 return 1;
2214 }
2215 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002216 if (mmu_notifier_retry(vcpu, mmu_seq))
2217 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002218 kvm_mmu_free_some_pages(vcpu);
2219 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002220 level, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002221 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002222
2223 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002224
2225out_unlock:
2226 spin_unlock(&vcpu->kvm->mmu_lock);
2227 kvm_release_pfn_clean(pfn);
2228 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002229}
2230
Avi Kivity6aa8b732006-12-10 02:21:36 -08002231static void nonpaging_free(struct kvm_vcpu *vcpu)
2232{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002233 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002234}
2235
2236static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2237{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002238 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002239
2240 context->new_cr3 = nonpaging_new_cr3;
2241 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002242 context->gva_to_gpa = nonpaging_gva_to_gpa;
2243 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002244 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002245 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002246 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002247 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002248 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002249 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002250 return 0;
2251}
2252
Avi Kivityd835dfe2007-11-21 02:57:59 +02002253void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002254{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002255 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002256 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002257}
2258
2259static void paging_new_cr3(struct kvm_vcpu *vcpu)
2260{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002261 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002262 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002263}
2264
Avi Kivity6aa8b732006-12-10 02:21:36 -08002265static void inject_page_fault(struct kvm_vcpu *vcpu,
2266 u64 addr,
2267 u32 err_code)
2268{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002269 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270}
2271
Avi Kivity6aa8b732006-12-10 02:21:36 -08002272static void paging_free(struct kvm_vcpu *vcpu)
2273{
2274 nonpaging_free(vcpu);
2275}
2276
Dong, Eddie82725b22009-03-30 16:21:08 +08002277static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2278{
2279 int bit7;
2280
2281 bit7 = (gpte >> 7) & 1;
2282 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2283}
2284
Avi Kivity6aa8b732006-12-10 02:21:36 -08002285#define PTTYPE 64
2286#include "paging_tmpl.h"
2287#undef PTTYPE
2288
2289#define PTTYPE 32
2290#include "paging_tmpl.h"
2291#undef PTTYPE
2292
Dong, Eddie82725b22009-03-30 16:21:08 +08002293static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2294{
2295 struct kvm_mmu *context = &vcpu->arch.mmu;
2296 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2297 u64 exb_bit_rsvd = 0;
2298
2299 if (!is_nx(vcpu))
2300 exb_bit_rsvd = rsvd_bits(63, 63);
2301 switch (level) {
2302 case PT32_ROOT_LEVEL:
2303 /* no rsvd bits for 2 level 4K page table entries */
2304 context->rsvd_bits_mask[0][1] = 0;
2305 context->rsvd_bits_mask[0][0] = 0;
2306 if (is_cpuid_PSE36())
2307 /* 36bits PSE 4MB page */
2308 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2309 else
2310 /* 32 bits PSE 4MB page */
2311 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Avi Kivity29a4b932009-05-19 13:29:27 +03002312 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002313 break;
2314 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002315 context->rsvd_bits_mask[0][2] =
2316 rsvd_bits(maxphyaddr, 63) |
2317 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002318 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002319 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002320 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2321 rsvd_bits(maxphyaddr, 62); /* PTE */
2322 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2323 rsvd_bits(maxphyaddr, 62) |
2324 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002325 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002326 break;
2327 case PT64_ROOT_LEVEL:
2328 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2329 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2330 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2331 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2332 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002333 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002334 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2335 rsvd_bits(maxphyaddr, 51);
2336 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002337 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2338 rsvd_bits(maxphyaddr, 51) |
2339 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002340 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002341 rsvd_bits(maxphyaddr, 51) |
2342 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002343 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002344 break;
2345 }
2346}
2347
Avi Kivity17ac10a2007-01-05 16:36:40 -08002348static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002349{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002350 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002351
2352 ASSERT(is_pae(vcpu));
2353 context->new_cr3 = paging_new_cr3;
2354 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002355 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002356 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002357 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002358 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002359 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002360 context->root_level = level;
2361 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002362 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002363 return 0;
2364}
2365
Avi Kivity17ac10a2007-01-05 16:36:40 -08002366static int paging64_init_context(struct kvm_vcpu *vcpu)
2367{
Dong, Eddie82725b22009-03-30 16:21:08 +08002368 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002369 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2370}
2371
Avi Kivity6aa8b732006-12-10 02:21:36 -08002372static int paging32_init_context(struct kvm_vcpu *vcpu)
2373{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002374 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002375
Dong, Eddie82725b22009-03-30 16:21:08 +08002376 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002377 context->new_cr3 = paging_new_cr3;
2378 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379 context->gva_to_gpa = paging32_gva_to_gpa;
2380 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002381 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002382 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002383 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002384 context->root_level = PT32_ROOT_LEVEL;
2385 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002386 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002387 return 0;
2388}
2389
2390static int paging32E_init_context(struct kvm_vcpu *vcpu)
2391{
Dong, Eddie82725b22009-03-30 16:21:08 +08002392 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002393 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002394}
2395
Joerg Roedelfb72d162008-02-07 13:47:44 +01002396static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2397{
2398 struct kvm_mmu *context = &vcpu->arch.mmu;
2399
2400 context->new_cr3 = nonpaging_new_cr3;
2401 context->page_fault = tdp_page_fault;
2402 context->free = nonpaging_free;
2403 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002404 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002405 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002406 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002407 context->root_hpa = INVALID_PAGE;
2408
2409 if (!is_paging(vcpu)) {
2410 context->gva_to_gpa = nonpaging_gva_to_gpa;
2411 context->root_level = 0;
2412 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002413 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002414 context->gva_to_gpa = paging64_gva_to_gpa;
2415 context->root_level = PT64_ROOT_LEVEL;
2416 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002417 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002418 context->gva_to_gpa = paging64_gva_to_gpa;
2419 context->root_level = PT32E_ROOT_LEVEL;
2420 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002421 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002422 context->gva_to_gpa = paging32_gva_to_gpa;
2423 context->root_level = PT32_ROOT_LEVEL;
2424 }
2425
2426 return 0;
2427}
2428
2429static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002430{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002431 int r;
2432
Avi Kivity6aa8b732006-12-10 02:21:36 -08002433 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002434 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002435
2436 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002437 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002438 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002439 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002440 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002441 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002442 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002443 r = paging32_init_context(vcpu);
2444
2445 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2446
2447 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002448}
2449
Joerg Roedelfb72d162008-02-07 13:47:44 +01002450static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2451{
Anthony Liguori35149e22008-04-02 14:46:56 -05002452 vcpu->arch.update_pte.pfn = bad_pfn;
2453
Joerg Roedelfb72d162008-02-07 13:47:44 +01002454 if (tdp_enabled)
2455 return init_kvm_tdp_mmu(vcpu);
2456 else
2457 return init_kvm_softmmu(vcpu);
2458}
2459
Avi Kivity6aa8b732006-12-10 02:21:36 -08002460static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2461{
2462 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002463 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2464 vcpu->arch.mmu.free(vcpu);
2465 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002466 }
2467}
2468
2469int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2470{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002471 destroy_kvm_mmu(vcpu);
2472 return init_kvm_mmu(vcpu);
2473}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002474EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002475
2476int kvm_mmu_load(struct kvm_vcpu *vcpu)
2477{
Avi Kivity714b93d2007-01-05 16:36:53 -08002478 int r;
2479
Avi Kivitye2dec932007-01-05 16:36:54 -08002480 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002481 if (r)
2482 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002483 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002484 kvm_mmu_free_some_pages(vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002485 r = mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002486 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002487 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002488 if (r)
2489 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08002490 /* set_cr3() should ensure TLB has been flushed */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002491 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08002492out:
2493 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002494}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002495EXPORT_SYMBOL_GPL(kvm_mmu_load);
2496
2497void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2498{
2499 mmu_free_roots(vcpu);
2500}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002501
Avi Kivity09072da2007-05-01 14:16:52 +03002502static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002503 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002504 u64 *spte)
2505{
2506 u64 pte;
2507 struct kvm_mmu_page *child;
2508
2509 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002510 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03002511 if (is_last_spte(pte, sp->role.level))
Izik Eidus290fc382007-09-27 14:11:22 +02002512 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002513 else {
2514 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002515 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002516 }
2517 }
Avi Kivityd555c332009-06-10 14:24:23 +03002518 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002519 if (is_large_pte(pte))
2520 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002521}
2522
Avi Kivity00284252007-05-01 16:53:31 +03002523static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002524 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002525 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002526 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002527{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002528 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02002529 ++vcpu->kvm->stat.mmu_pde_zapped;
2530 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03002531 }
Avi Kivity00284252007-05-01 16:53:31 +03002532
Avi Kivity4cee5762007-11-18 16:37:07 +02002533 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002534 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002535 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002536 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002537 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002538}
2539
Avi Kivity79539ce2007-11-21 02:06:21 +02002540static bool need_remote_flush(u64 old, u64 new)
2541{
2542 if (!is_shadow_present_pte(old))
2543 return false;
2544 if (!is_shadow_present_pte(new))
2545 return true;
2546 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2547 return true;
2548 old ^= PT64_NX_MASK;
2549 new ^= PT64_NX_MASK;
2550 return (old & ~new & PT64_PERM_MASK) != 0;
2551}
2552
2553static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2554{
2555 if (need_remote_flush(old, new))
2556 kvm_flush_remote_tlbs(vcpu->kvm);
2557 else
2558 kvm_mmu_flush_tlb(vcpu);
2559}
2560
Avi Kivity12b7d282007-09-23 14:10:49 +02002561static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2562{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002563 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002564
Sheng Yang7b523452008-04-25 21:13:50 +08002565 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002566}
2567
Avi Kivityd7824ff2007-12-30 12:29:05 +02002568static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2569 const u8 *new, int bytes)
2570{
2571 gfn_t gfn;
2572 int r;
2573 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002574 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002575
2576 if (bytes != 4 && bytes != 8)
2577 return;
2578
2579 /*
2580 * Assume that the pte write on a page table of the same type
2581 * as the current vcpu paging mode. This is nearly always true
2582 * (might be false while changing modes). Note it is verified later
2583 * by update_pte().
2584 */
2585 if (is_pae(vcpu)) {
2586 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2587 if ((bytes == 4) && (gpa % 4 == 0)) {
2588 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2589 if (r)
2590 return;
2591 memcpy((void *)&gpte + (gpa % 8), new, 4);
2592 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2593 memcpy((void *)&gpte, new, 8);
2594 }
2595 } else {
2596 if ((bytes == 4) && (gpa % 4 == 0))
2597 memcpy((void *)&gpte, new, 4);
2598 }
Avi Kivity43a37952009-06-10 14:12:05 +03002599 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02002600 return;
2601 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002602
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002603 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002604 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002605 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002606
Anthony Liguori35149e22008-04-02 14:46:56 -05002607 if (is_error_pfn(pfn)) {
2608 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002609 return;
2610 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002611 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002612 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002613}
2614
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002615static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2616{
2617 u64 *spte = vcpu->arch.last_pte_updated;
2618
2619 if (spte
2620 && vcpu->arch.last_pte_gfn == gfn
2621 && shadow_accessed_mask
2622 && !(*spte & shadow_accessed_mask)
2623 && is_shadow_present_pte(*spte))
2624 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2625}
2626
Avi Kivity09072da2007-05-01 14:16:52 +03002627void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002628 const u8 *new, int bytes,
2629 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002630{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002631 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002632 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002633 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002634 struct hlist_head *bucket;
2635 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002636 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002637 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002638 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002639 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002640 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002641 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002642 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002643 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002644 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002645 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002646 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002647
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002648 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002649 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002650 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002651 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002652 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002653 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002654 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002655 if (guest_initiated) {
2656 if (gfn == vcpu->arch.last_pt_write_gfn
2657 && !last_updated_pte_accessed(vcpu)) {
2658 ++vcpu->arch.last_pt_write_count;
2659 if (vcpu->arch.last_pt_write_count >= 3)
2660 flooded = 1;
2661 } else {
2662 vcpu->arch.last_pt_write_gfn = gfn;
2663 vcpu->arch.last_pt_write_count = 1;
2664 vcpu->arch.last_pte_updated = NULL;
2665 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002666 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002667 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002668 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002669 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002670 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002671 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002672 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002673 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002674 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002675 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002676 /*
2677 * Misaligned accesses are too much trouble to fix
2678 * up; also, they usually indicate a page is not used
2679 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002680 *
2681 * If we're seeing too many writes to a page,
2682 * it may no longer be a page table, or we may be
2683 * forking, in which case it is better to unmap the
2684 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002685 */
2686 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002687 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002688 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2689 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002690 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002691 continue;
2692 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002693 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002694 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002695 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002696 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002697 page_offset <<= 1; /* 32->64 */
2698 /*
2699 * A 32-bit pde maps 4MB while the shadow pdes map
2700 * only 2MB. So we need to double the offset again
2701 * and zap two pdes instead of one.
2702 */
2703 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002704 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002705 page_offset <<= 1;
2706 npte = 2;
2707 }
Avi Kivityfce06572007-05-01 16:44:05 +03002708 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002709 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002710 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002711 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002712 }
Avi Kivity4db35312007-11-21 15:28:32 +02002713 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002714 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2715 gentry = 0;
2716 r = kvm_read_guest_atomic(vcpu->kvm,
2717 gpa & ~(u64)(pte_size - 1),
2718 &gentry, pte_size);
2719 new = (const void *)&gentry;
2720 if (r < 0)
2721 new = NULL;
2722 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002723 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002724 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002725 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002726 if (new)
2727 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002728 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002729 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002730 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002731 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002732 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002733 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002734 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2735 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2736 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002737 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002738}
2739
Avi Kivitya4360362007-01-05 16:36:45 -08002740int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2741{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002742 gpa_t gpa;
2743 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002744
Avi Kivity60f24782009-08-27 13:37:06 +03002745 if (tdp_enabled)
2746 return 0;
2747
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002748 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002749
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002750 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002751 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002752 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002753 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002754}
Avi Kivity577bdc42008-07-19 08:57:05 +03002755EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002756
Avi Kivity22d95b12007-09-14 20:26:06 +03002757void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002758{
Izik Eidus3b80fff2009-07-28 15:26:58 -03002759 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES &&
2760 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02002761 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002762
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002763 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002764 struct kvm_mmu_page, link);
2765 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002766 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002767 }
2768}
Avi Kivityebeace82007-01-05 16:36:47 -08002769
Avi Kivity30677142007-10-28 18:48:59 +02002770int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2771{
2772 int r;
2773 enum emulation_result er;
2774
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002775 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002776 if (r < 0)
2777 goto out;
2778
2779 if (!r) {
2780 r = 1;
2781 goto out;
2782 }
2783
Avi Kivityb733bfb2007-10-28 18:52:05 +02002784 r = mmu_topup_memory_caches(vcpu);
2785 if (r)
2786 goto out;
2787
Avi Kivity30677142007-10-28 18:48:59 +02002788 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002789
2790 switch (er) {
2791 case EMULATE_DONE:
2792 return 1;
2793 case EMULATE_DO_MMIO:
2794 ++vcpu->stat.mmio_exits;
2795 return 0;
2796 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002797 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2798 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2799 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02002800 default:
2801 BUG();
2802 }
2803out:
Avi Kivity30677142007-10-28 18:48:59 +02002804 return r;
2805}
2806EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2807
Marcelo Tosattia7052892008-09-23 13:18:35 -03002808void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2809{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002810 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002811 kvm_mmu_flush_tlb(vcpu);
2812 ++vcpu->stat.invlpg;
2813}
2814EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2815
Joerg Roedel18552672008-02-07 13:47:41 +01002816void kvm_enable_tdp(void)
2817{
2818 tdp_enabled = true;
2819}
2820EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2821
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002822void kvm_disable_tdp(void)
2823{
2824 tdp_enabled = false;
2825}
2826EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2827
Avi Kivity6aa8b732006-12-10 02:21:36 -08002828static void free_mmu_pages(struct kvm_vcpu *vcpu)
2829{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002830 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002831}
2832
2833static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2834{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002835 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002836 int i;
2837
2838 ASSERT(vcpu);
2839
Avi Kivity17ac10a2007-01-05 16:36:40 -08002840 /*
2841 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2842 * Therefore we need to allocate shadow page tables in the first
2843 * 4GB of memory, which happens to fit the DMA32 zone.
2844 */
2845 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2846 if (!page)
2847 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002848 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002849 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002850 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002851
Avi Kivity6aa8b732006-12-10 02:21:36 -08002852 return 0;
2853
2854error_1:
2855 free_mmu_pages(vcpu);
2856 return -ENOMEM;
2857}
2858
Ingo Molnar8018c272006-12-29 16:50:01 -08002859int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002860{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002861 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002862 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002863
Ingo Molnar8018c272006-12-29 16:50:01 -08002864 return alloc_mmu_pages(vcpu);
2865}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002866
Ingo Molnar8018c272006-12-29 16:50:01 -08002867int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2868{
2869 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002870 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002871
Ingo Molnar8018c272006-12-29 16:50:01 -08002872 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002873}
2874
2875void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2876{
2877 ASSERT(vcpu);
2878
2879 destroy_kvm_mmu(vcpu);
2880 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002881 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882}
2883
Avi Kivity90cb0522007-07-17 13:04:56 +03002884void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002885{
Avi Kivity4db35312007-11-21 15:28:32 +02002886 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002887
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002888 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002889 int i;
2890 u64 *pt;
2891
Sheng Yang291f26b2008-10-16 17:30:57 +08002892 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002893 continue;
2894
Avi Kivity4db35312007-11-21 15:28:32 +02002895 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002896 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2897 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002898 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002899 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002900 }
Avi Kivity171d5952008-08-27 16:40:51 +03002901 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002902}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002903
Avi Kivity90cb0522007-07-17 13:04:56 +03002904void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002905{
Avi Kivity4db35312007-11-21 15:28:32 +02002906 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002907
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002908 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002909 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002910 if (kvm_mmu_zap_page(kvm, sp))
2911 node = container_of(kvm->arch.active_mmu_pages.next,
2912 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002913 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002914
Avi Kivity90cb0522007-07-17 13:04:56 +03002915 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002916}
2917
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002918static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002919{
2920 struct kvm_mmu_page *page;
2921
2922 page = container_of(kvm->arch.active_mmu_pages.prev,
2923 struct kvm_mmu_page, link);
2924 kvm_mmu_zap_page(kvm, page);
2925}
2926
2927static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2928{
2929 struct kvm *kvm;
2930 struct kvm *kvm_freed = NULL;
2931 int cache_count = 0;
2932
2933 spin_lock(&kvm_lock);
2934
2935 list_for_each_entry(kvm, &vm_list, vm_list) {
2936 int npages;
2937
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002938 if (!down_read_trylock(&kvm->slots_lock))
2939 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002940 spin_lock(&kvm->mmu_lock);
2941 npages = kvm->arch.n_alloc_mmu_pages -
2942 kvm->arch.n_free_mmu_pages;
2943 cache_count += npages;
2944 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2945 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2946 cache_count--;
2947 kvm_freed = kvm;
2948 }
2949 nr_to_scan--;
2950
2951 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002952 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002953 }
2954 if (kvm_freed)
2955 list_move_tail(&kvm_freed->vm_list, &vm_list);
2956
2957 spin_unlock(&kvm_lock);
2958
2959 return cache_count;
2960}
2961
2962static struct shrinker mmu_shrinker = {
2963 .shrink = mmu_shrink,
2964 .seeks = DEFAULT_SEEKS * 10,
2965};
2966
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002967static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002968{
2969 if (pte_chain_cache)
2970 kmem_cache_destroy(pte_chain_cache);
2971 if (rmap_desc_cache)
2972 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002973 if (mmu_page_header_cache)
2974 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002975}
2976
Izik Eidus3ee16c82008-03-30 15:17:21 +03002977void kvm_mmu_module_exit(void)
2978{
2979 mmu_destroy_caches();
2980 unregister_shrinker(&mmu_shrinker);
2981}
2982
Avi Kivityb5a33a72007-04-15 16:31:09 +03002983int kvm_mmu_module_init(void)
2984{
2985 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2986 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002987 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002988 if (!pte_chain_cache)
2989 goto nomem;
2990 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2991 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002992 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002993 if (!rmap_desc_cache)
2994 goto nomem;
2995
Avi Kivityd3d25b02007-05-30 12:34:53 +03002996 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2997 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002998 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002999 if (!mmu_page_header_cache)
3000 goto nomem;
3001
Izik Eidus3ee16c82008-03-30 15:17:21 +03003002 register_shrinker(&mmu_shrinker);
3003
Avi Kivityb5a33a72007-04-15 16:31:09 +03003004 return 0;
3005
3006nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03003007 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003008 return -ENOMEM;
3009}
3010
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003011/*
3012 * Caculate mmu pages needed for kvm.
3013 */
3014unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3015{
3016 int i;
3017 unsigned int nr_mmu_pages;
3018 unsigned int nr_pages = 0;
3019
3020 for (i = 0; i < kvm->nmemslots; i++)
3021 nr_pages += kvm->memslots[i].npages;
3022
3023 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3024 nr_mmu_pages = max(nr_mmu_pages,
3025 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3026
3027 return nr_mmu_pages;
3028}
3029
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003030static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3031 unsigned len)
3032{
3033 if (len > buffer->len)
3034 return NULL;
3035 return buffer->ptr;
3036}
3037
3038static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3039 unsigned len)
3040{
3041 void *ret;
3042
3043 ret = pv_mmu_peek_buffer(buffer, len);
3044 if (!ret)
3045 return ret;
3046 buffer->ptr += len;
3047 buffer->len -= len;
3048 buffer->processed += len;
3049 return ret;
3050}
3051
3052static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3053 gpa_t addr, gpa_t value)
3054{
3055 int bytes = 8;
3056 int r;
3057
3058 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3059 bytes = 4;
3060
3061 r = mmu_topup_memory_caches(vcpu);
3062 if (r)
3063 return r;
3064
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003065 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003066 return -EFAULT;
3067
3068 return 1;
3069}
3070
3071static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3072{
Avi Kivitya8cd0242009-05-24 22:15:25 +03003073 kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003074 return 1;
3075}
3076
3077static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3078{
3079 spin_lock(&vcpu->kvm->mmu_lock);
3080 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3081 spin_unlock(&vcpu->kvm->mmu_lock);
3082 return 1;
3083}
3084
3085static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3086 struct kvm_pv_mmu_op_buffer *buffer)
3087{
3088 struct kvm_mmu_op_header *header;
3089
3090 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3091 if (!header)
3092 return 0;
3093 switch (header->op) {
3094 case KVM_MMU_OP_WRITE_PTE: {
3095 struct kvm_mmu_op_write_pte *wpte;
3096
3097 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3098 if (!wpte)
3099 return 0;
3100 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3101 wpte->pte_val);
3102 }
3103 case KVM_MMU_OP_FLUSH_TLB: {
3104 struct kvm_mmu_op_flush_tlb *ftlb;
3105
3106 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3107 if (!ftlb)
3108 return 0;
3109 return kvm_pv_mmu_flush_tlb(vcpu);
3110 }
3111 case KVM_MMU_OP_RELEASE_PT: {
3112 struct kvm_mmu_op_release_pt *rpt;
3113
3114 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3115 if (!rpt)
3116 return 0;
3117 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3118 }
3119 default: return 0;
3120 }
3121}
3122
3123int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3124 gpa_t addr, unsigned long *ret)
3125{
3126 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003127 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003128
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003129 buffer->ptr = buffer->buf;
3130 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3131 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003132
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003133 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003134 if (r)
3135 goto out;
3136
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003137 while (buffer->len) {
3138 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003139 if (r < 0)
3140 goto out;
3141 if (r == 0)
3142 break;
3143 }
3144
3145 r = 1;
3146out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003147 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003148 return r;
3149}
3150
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003151int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3152{
3153 struct kvm_shadow_walk_iterator iterator;
3154 int nr_sptes = 0;
3155
3156 spin_lock(&vcpu->kvm->mmu_lock);
3157 for_each_shadow_entry(vcpu, addr, iterator) {
3158 sptes[iterator.level-1] = *iterator.sptep;
3159 nr_sptes++;
3160 if (!is_shadow_present_pte(*iterator.sptep))
3161 break;
3162 }
3163 spin_unlock(&vcpu->kvm->mmu_lock);
3164
3165 return nr_sptes;
3166}
3167EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3168
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003169#ifdef AUDIT
3170
3171static const char *audit_msg;
3172
3173static gva_t canonicalize(gva_t gva)
3174{
3175#ifdef CONFIG_X86_64
3176 gva = (long long)(gva << 16) >> 16;
3177#endif
3178 return gva;
3179}
3180
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003181
3182typedef void (*inspect_spte_fn) (struct kvm *kvm, struct kvm_mmu_page *sp,
3183 u64 *sptep);
3184
3185static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3186 inspect_spte_fn fn)
3187{
3188 int i;
3189
3190 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3191 u64 ent = sp->spt[i];
3192
3193 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003194 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003195 struct kvm_mmu_page *child;
3196 child = page_header(ent & PT64_BASE_ADDR_MASK);
3197 __mmu_spte_walk(kvm, child, fn);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003198 } else
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003199 fn(kvm, sp, &sp->spt[i]);
3200 }
3201 }
3202}
3203
3204static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3205{
3206 int i;
3207 struct kvm_mmu_page *sp;
3208
3209 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3210 return;
3211 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3212 hpa_t root = vcpu->arch.mmu.root_hpa;
3213 sp = page_header(root);
3214 __mmu_spte_walk(vcpu->kvm, sp, fn);
3215 return;
3216 }
3217 for (i = 0; i < 4; ++i) {
3218 hpa_t root = vcpu->arch.mmu.pae_root[i];
3219
3220 if (root && VALID_PAGE(root)) {
3221 root &= PT64_BASE_ADDR_MASK;
3222 sp = page_header(root);
3223 __mmu_spte_walk(vcpu->kvm, sp, fn);
3224 }
3225 }
3226 return;
3227}
3228
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003229static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3230 gva_t va, int level)
3231{
3232 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3233 int i;
3234 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3235
3236 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3237 u64 ent = pt[i];
3238
Avi Kivityc7addb92007-09-16 18:58:32 +02003239 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003240 continue;
3241
3242 va = canonicalize(va);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003243 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3244 audit_mappings_page(vcpu, ent, va, level - 1);
3245 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003246 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Jan Kiszka34382532009-04-25 12:43:21 +02003247 gfn_t gfn = gpa >> PAGE_SHIFT;
3248 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3249 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003250
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003251 if (is_error_pfn(pfn)) {
3252 kvm_release_pfn_clean(pfn);
3253 continue;
3254 }
3255
Avi Kivityc7addb92007-09-16 18:58:32 +02003256 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003257 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003258 printk(KERN_ERR "xx audit error: (%s) levels %d"
3259 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003260 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003261 va, gpa, hpa, ent,
3262 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003263 else if (ent == shadow_notrap_nonpresent_pte
3264 && !is_error_hpa(hpa))
3265 printk(KERN_ERR "audit: (%s) notrap shadow,"
3266 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003267 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003268
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003269 }
3270 }
3271}
3272
3273static void audit_mappings(struct kvm_vcpu *vcpu)
3274{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003275 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003276
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003277 if (vcpu->arch.mmu.root_level == 4)
3278 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003279 else
3280 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003281 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003282 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003283 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003284 i << 30,
3285 2);
3286}
3287
3288static int count_rmaps(struct kvm_vcpu *vcpu)
3289{
3290 int nmaps = 0;
3291 int i, j, k;
3292
3293 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3294 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
3295 struct kvm_rmap_desc *d;
3296
3297 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003298 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003299
Izik Eidus290fc382007-09-27 14:11:22 +02003300 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003301 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003302 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003303 ++nmaps;
3304 continue;
3305 }
Izik Eidus290fc382007-09-27 14:11:22 +02003306 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003307 while (d) {
3308 for (k = 0; k < RMAP_EXT; ++k)
Avi Kivityd555c332009-06-10 14:24:23 +03003309 if (d->sptes[k])
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003310 ++nmaps;
3311 else
3312 break;
3313 d = d->more;
3314 }
3315 }
3316 }
3317 return nmaps;
3318}
3319
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003320void inspect_spte_has_rmap(struct kvm *kvm, struct kvm_mmu_page *sp, u64 *sptep)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003321{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003322 unsigned long *rmapp;
3323 struct kvm_mmu_page *rev_sp;
3324 gfn_t gfn;
3325
3326 if (*sptep & PT_WRITABLE_MASK) {
3327 rev_sp = page_header(__pa(sptep));
3328 gfn = rev_sp->gfns[sptep - rev_sp->spt];
3329
3330 if (!gfn_to_memslot(kvm, gfn)) {
3331 if (!printk_ratelimit())
3332 return;
3333 printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3334 audit_msg, gfn);
3335 printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
3336 audit_msg, sptep - rev_sp->spt,
3337 rev_sp->gfn);
3338 dump_stack();
3339 return;
3340 }
3341
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003342 rmapp = gfn_to_rmap(kvm, rev_sp->gfns[sptep - rev_sp->spt],
3343 is_large_pte(*sptep));
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003344 if (!*rmapp) {
3345 if (!printk_ratelimit())
3346 return;
3347 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3348 audit_msg, *sptep);
3349 dump_stack();
3350 }
3351 }
3352
3353}
3354
3355void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3356{
3357 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3358}
3359
3360static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3361{
Avi Kivity4db35312007-11-21 15:28:32 +02003362 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003363 int i;
3364
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003365 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003366 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003367
Avi Kivity4db35312007-11-21 15:28:32 +02003368 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003369 continue;
3370
3371 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3372 u64 ent = pt[i];
3373
3374 if (!(ent & PT_PRESENT_MASK))
3375 continue;
3376 if (!(ent & PT_WRITABLE_MASK))
3377 continue;
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003378 inspect_spte_has_rmap(vcpu->kvm, sp, &pt[i]);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003379 }
3380 }
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003381 return;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003382}
3383
3384static void audit_rmap(struct kvm_vcpu *vcpu)
3385{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003386 check_writable_mappings_rmap(vcpu);
3387 count_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003388}
3389
3390static void audit_write_protection(struct kvm_vcpu *vcpu)
3391{
Avi Kivity4db35312007-11-21 15:28:32 +02003392 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003393 struct kvm_memory_slot *slot;
3394 unsigned long *rmapp;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003395 u64 *spte;
Izik Eidus290fc382007-09-27 14:11:22 +02003396 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003397
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003398 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02003399 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003400 continue;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003401 if (sp->unsync)
3402 continue;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003403
Avi Kivity4db35312007-11-21 15:28:32 +02003404 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003405 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003406 rmapp = &slot->rmap[gfn - slot->base_gfn];
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003407
3408 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3409 while (spte) {
3410 if (*spte & PT_WRITABLE_MASK)
3411 printk(KERN_ERR "%s: (%s) shadow page has "
3412 "writable mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003413 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003414 sp->role.word);
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003415 spte = rmap_next(vcpu->kvm, rmapp, spte);
3416 }
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003417 }
3418}
3419
3420static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3421{
3422 int olddbg = dbg;
3423
3424 dbg = 0;
3425 audit_msg = msg;
3426 audit_rmap(vcpu);
3427 audit_write_protection(vcpu);
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003428 if (strcmp("pre pte write", audit_msg) != 0)
3429 audit_mappings(vcpu);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003430 audit_writable_sptes_have_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003431 dbg = olddbg;
3432}
3433
3434#endif