blob: 818b92ad82cf14bc9aae8cf3b8a93fa9e3e1f87e [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 Kivityda928522007-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
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000751static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
752 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200753{
754 u64 *spte;
755 int need_tlb_flush = 0;
756
757 while ((spte = rmap_next(kvm, rmapp, NULL))) {
758 BUG_ON(!(*spte & PT_PRESENT_MASK));
759 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
760 rmap_remove(kvm, spte);
Avi Kivityd555c332009-06-10 14:24:23 +0300761 __set_spte(spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200762 need_tlb_flush = 1;
763 }
764 return need_tlb_flush;
765}
766
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000767static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
768 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +0300769{
770 int need_flush = 0;
771 u64 *spte, new_spte;
772 pte_t *ptep = (pte_t *)data;
773 pfn_t new_pfn;
774
775 WARN_ON(pte_huge(*ptep));
776 new_pfn = pte_pfn(*ptep);
777 spte = rmap_next(kvm, rmapp, NULL);
778 while (spte) {
779 BUG_ON(!is_shadow_present_pte(*spte));
780 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
781 need_flush = 1;
782 if (pte_write(*ptep)) {
783 rmap_remove(kvm, spte);
784 __set_spte(spte, shadow_trap_nonpresent_pte);
785 spte = rmap_next(kvm, rmapp, NULL);
786 } else {
787 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
788 new_spte |= (u64)new_pfn << PAGE_SHIFT;
789
790 new_spte &= ~PT_WRITABLE_MASK;
791 new_spte &= ~SPTE_HOST_WRITEABLE;
792 if (is_writeble_pte(*spte))
793 kvm_set_pfn_dirty(spte_to_pfn(*spte));
794 __set_spte(spte, new_spte);
795 spte = rmap_next(kvm, rmapp, spte);
796 }
797 }
798 if (need_flush)
799 kvm_flush_remote_tlbs(kvm);
800
801 return 0;
802}
803
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000804static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
805 unsigned long data,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300806 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000807 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200808{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200809 int i, j;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200810 int retval = 0;
811
812 /*
813 * If mmap_sem isn't taken, we can look the memslots with only
814 * the mmu_lock by skipping over the slots with userspace_addr == 0.
815 */
816 for (i = 0; i < kvm->nmemslots; i++) {
817 struct kvm_memory_slot *memslot = &kvm->memslots[i];
818 unsigned long start = memslot->userspace_addr;
819 unsigned long end;
820
821 /* mmu_lock protects userspace_addr */
822 if (!start)
823 continue;
824
825 end = start + (memslot->npages << PAGE_SHIFT);
826 if (hva >= start && hva < end) {
827 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200828
Izik Eidus3da0dd42009-09-23 21:47:18 +0300829 retval |= handler(kvm, &memslot->rmap[gfn_offset],
830 data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200831
832 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
833 int idx = gfn_offset;
834 idx /= KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL + j);
835 retval |= handler(kvm,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300836 &memslot->lpage_info[j][idx].rmap_pde,
837 data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200838 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200839 }
840 }
841
842 return retval;
843}
844
845int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
846{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300847 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200848}
849
Izik Eidus3da0dd42009-09-23 21:47:18 +0300850void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
851{
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000852 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300853}
854
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000855static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
856 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200857{
858 u64 *spte;
859 int young = 0;
860
Sheng Yang534e38b2008-09-08 15:12:30 +0800861 /* always return old for EPT */
862 if (!shadow_accessed_mask)
863 return 0;
864
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200865 spte = rmap_next(kvm, rmapp, NULL);
866 while (spte) {
867 int _young;
868 u64 _spte = *spte;
869 BUG_ON(!(_spte & PT_PRESENT_MASK));
870 _young = _spte & PT_ACCESSED_MASK;
871 if (_young) {
872 young = 1;
873 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
874 }
875 spte = rmap_next(kvm, rmapp, spte);
876 }
877 return young;
878}
879
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300880#define RMAP_RECYCLE_THRESHOLD 1000
881
Joerg Roedel852e3c12009-07-27 16:30:44 +0200882static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300883{
884 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200885 struct kvm_mmu_page *sp;
886
887 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300888
889 gfn = unalias_gfn(vcpu->kvm, gfn);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200890 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300891
Izik Eidus3da0dd42009-09-23 21:47:18 +0300892 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300893 kvm_flush_remote_tlbs(vcpu->kvm);
894}
895
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200896int kvm_age_hva(struct kvm *kvm, unsigned long hva)
897{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300898 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200899}
900
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800901#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300902static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800903{
Avi Kivity139bdb22007-01-05 16:36:50 -0800904 u64 *pos;
905 u64 *end;
906
Avi Kivity47ad8e62007-05-06 15:50:58 +0300907 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300908 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800909 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800910 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800911 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800912 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913 return 1;
914}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800915#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800916
Avi Kivity4db35312007-11-21 15:28:32 +0200917static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800918{
Avi Kivity4db35312007-11-21 15:28:32 +0200919 ASSERT(is_empty_shadow_page(sp->spt));
920 list_del(&sp->link);
921 __free_page(virt_to_page(sp->spt));
922 __free_page(virt_to_page(sp->gfns));
923 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800924 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800925}
926
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800927static unsigned kvm_page_table_hashfn(gfn_t gfn)
928{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200929 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800930}
931
Avi Kivity25c0de22007-01-05 16:36:42 -0800932static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
933 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934{
Avi Kivity4db35312007-11-21 15:28:32 +0200935 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800937 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
938 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
939 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200940 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800941 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -0200942 INIT_LIST_HEAD(&sp->oos_link);
Sheng Yang291f26b2008-10-16 17:30:57 +0800943 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200944 sp->multimapped = 0;
945 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800946 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200947 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800948}
949
Avi Kivity714b93d2007-01-05 16:36:53 -0800950static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200951 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800952{
953 struct kvm_pte_chain *pte_chain;
954 struct hlist_node *node;
955 int i;
956
957 if (!parent_pte)
958 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200959 if (!sp->multimapped) {
960 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800961
962 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200963 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800964 return;
965 }
Avi Kivity4db35312007-11-21 15:28:32 +0200966 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800967 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200968 INIT_HLIST_HEAD(&sp->parent_ptes);
969 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800970 pte_chain->parent_ptes[0] = old;
971 }
Avi Kivity4db35312007-11-21 15:28:32 +0200972 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800973 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
974 continue;
975 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
976 if (!pte_chain->parent_ptes[i]) {
977 pte_chain->parent_ptes[i] = parent_pte;
978 return;
979 }
980 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800981 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800982 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200983 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800984 pte_chain->parent_ptes[0] = parent_pte;
985}
986
Avi Kivity4db35312007-11-21 15:28:32 +0200987static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800988 u64 *parent_pte)
989{
990 struct kvm_pte_chain *pte_chain;
991 struct hlist_node *node;
992 int i;
993
Avi Kivity4db35312007-11-21 15:28:32 +0200994 if (!sp->multimapped) {
995 BUG_ON(sp->parent_pte != parent_pte);
996 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800997 return;
998 }
Avi Kivity4db35312007-11-21 15:28:32 +0200999 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001000 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1001 if (!pte_chain->parent_ptes[i])
1002 break;
1003 if (pte_chain->parent_ptes[i] != parent_pte)
1004 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001005 while (i + 1 < NR_PTE_CHAIN_ENTRIES
1006 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001007 pte_chain->parent_ptes[i]
1008 = pte_chain->parent_ptes[i + 1];
1009 ++i;
1010 }
1011 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001012 if (i == 0) {
1013 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001014 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +02001015 if (hlist_empty(&sp->parent_ptes)) {
1016 sp->multimapped = 0;
1017 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001018 }
1019 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001020 return;
1021 }
1022 BUG();
1023}
1024
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001025
1026static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1027 mmu_parent_walk_fn fn)
1028{
1029 struct kvm_pte_chain *pte_chain;
1030 struct hlist_node *node;
1031 struct kvm_mmu_page *parent_sp;
1032 int i;
1033
1034 if (!sp->multimapped && sp->parent_pte) {
1035 parent_sp = page_header(__pa(sp->parent_pte));
1036 fn(vcpu, parent_sp);
1037 mmu_parent_walk(vcpu, parent_sp, fn);
1038 return;
1039 }
1040 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1041 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1042 if (!pte_chain->parent_ptes[i])
1043 break;
1044 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
1045 fn(vcpu, parent_sp);
1046 mmu_parent_walk(vcpu, parent_sp, fn);
1047 }
1048}
1049
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001050static void kvm_mmu_update_unsync_bitmap(u64 *spte)
1051{
1052 unsigned int index;
1053 struct kvm_mmu_page *sp = page_header(__pa(spte));
1054
1055 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001056 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
1057 sp->unsync_children++;
1058 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001059}
1060
1061static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
1062{
1063 struct kvm_pte_chain *pte_chain;
1064 struct hlist_node *node;
1065 int i;
1066
1067 if (!sp->parent_pte)
1068 return;
1069
1070 if (!sp->multimapped) {
1071 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
1072 return;
1073 }
1074
1075 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1076 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1077 if (!pte_chain->parent_ptes[i])
1078 break;
1079 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
1080 }
1081}
1082
1083static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1084{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001085 kvm_mmu_update_parents_unsync(sp);
1086 return 1;
1087}
1088
1089static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
1090 struct kvm_mmu_page *sp)
1091{
1092 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
1093 kvm_mmu_update_parents_unsync(sp);
1094}
1095
Avi Kivityd761a502008-05-29 14:55:03 +03001096static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1097 struct kvm_mmu_page *sp)
1098{
1099 int i;
1100
1101 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1102 sp->spt[i] = shadow_trap_nonpresent_pte;
1103}
1104
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001105static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
1106 struct kvm_mmu_page *sp)
1107{
1108 return 1;
1109}
1110
Marcelo Tosattia7052892008-09-23 13:18:35 -03001111static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1112{
1113}
1114
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001115#define KVM_PAGE_ARRAY_NR 16
1116
1117struct kvm_mmu_pages {
1118 struct mmu_page_and_offset {
1119 struct kvm_mmu_page *sp;
1120 unsigned int idx;
1121 } page[KVM_PAGE_ARRAY_NR];
1122 unsigned int nr;
1123};
1124
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001125#define for_each_unsync_children(bitmap, idx) \
1126 for (idx = find_first_bit(bitmap, 512); \
1127 idx < 512; \
1128 idx = find_next_bit(bitmap, 512, idx+1))
1129
Hannes Edercded19f2009-02-21 02:19:13 +01001130static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1131 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001132{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001133 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001134
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001135 if (sp->unsync)
1136 for (i=0; i < pvec->nr; i++)
1137 if (pvec->page[i].sp == sp)
1138 return 0;
1139
1140 pvec->page[pvec->nr].sp = sp;
1141 pvec->page[pvec->nr].idx = idx;
1142 pvec->nr++;
1143 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1144}
1145
1146static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1147 struct kvm_mmu_pages *pvec)
1148{
1149 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001150
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001151 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001152 u64 ent = sp->spt[i];
1153
Marcelo Tosatti87917232008-12-22 18:49:30 -02001154 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001155 struct kvm_mmu_page *child;
1156 child = page_header(ent & PT64_BASE_ADDR_MASK);
1157
1158 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001159 if (mmu_pages_add(pvec, child, i))
1160 return -ENOSPC;
1161
1162 ret = __mmu_unsync_walk(child, pvec);
1163 if (!ret)
1164 __clear_bit(i, sp->unsync_child_bitmap);
1165 else if (ret > 0)
1166 nr_unsync_leaf += ret;
1167 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001168 return ret;
1169 }
1170
1171 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001172 nr_unsync_leaf++;
1173 if (mmu_pages_add(pvec, child, i))
1174 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001175 }
1176 }
1177 }
1178
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001179 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001180 sp->unsync_children = 0;
1181
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001182 return nr_unsync_leaf;
1183}
1184
1185static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1186 struct kvm_mmu_pages *pvec)
1187{
1188 if (!sp->unsync_children)
1189 return 0;
1190
1191 mmu_pages_add(pvec, sp, 0);
1192 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001193}
1194
Avi Kivity4db35312007-11-21 15:28:32 +02001195static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001196{
1197 unsigned index;
1198 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001199 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001200 struct hlist_node *node;
1201
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001202 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001203 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001204 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001205 hlist_for_each_entry(sp, node, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001206 if (sp->gfn == gfn && !sp->role.direct
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001207 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001208 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001209 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001210 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001211 }
1212 return NULL;
1213}
1214
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001215static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1216{
1217 WARN_ON(!sp->unsync);
1218 sp->unsync = 0;
1219 --kvm->stat.mmu_unsync;
1220}
1221
1222static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1223
1224static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1225{
1226 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1227 kvm_mmu_zap_page(vcpu->kvm, sp);
1228 return 1;
1229 }
1230
Avi Kivityf691fe12009-07-06 15:58:14 +03001231 trace_kvm_mmu_sync_page(sp);
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001232 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1233 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001234 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001235 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1236 kvm_mmu_zap_page(vcpu->kvm, sp);
1237 return 1;
1238 }
1239
1240 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001241 return 0;
1242}
1243
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001244struct mmu_page_path {
1245 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1246 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001247};
1248
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001249#define for_each_sp(pvec, sp, parents, i) \
1250 for (i = mmu_pages_next(&pvec, &parents, -1), \
1251 sp = pvec.page[i].sp; \
1252 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1253 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001254
Hannes Edercded19f2009-02-21 02:19:13 +01001255static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1256 struct mmu_page_path *parents,
1257 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001258{
1259 int n;
1260
1261 for (n = i+1; n < pvec->nr; n++) {
1262 struct kvm_mmu_page *sp = pvec->page[n].sp;
1263
1264 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1265 parents->idx[0] = pvec->page[n].idx;
1266 return n;
1267 }
1268
1269 parents->parent[sp->role.level-2] = sp;
1270 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1271 }
1272
1273 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001274}
1275
Hannes Edercded19f2009-02-21 02:19:13 +01001276static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001277{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001278 struct kvm_mmu_page *sp;
1279 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001280
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001281 do {
1282 unsigned int idx = parents->idx[level];
1283
1284 sp = parents->parent[level];
1285 if (!sp)
1286 return;
1287
1288 --sp->unsync_children;
1289 WARN_ON((int)sp->unsync_children < 0);
1290 __clear_bit(idx, sp->unsync_child_bitmap);
1291 level++;
1292 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1293}
1294
1295static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1296 struct mmu_page_path *parents,
1297 struct kvm_mmu_pages *pvec)
1298{
1299 parents->parent[parent->role.level-1] = NULL;
1300 pvec->nr = 0;
1301}
1302
1303static void mmu_sync_children(struct kvm_vcpu *vcpu,
1304 struct kvm_mmu_page *parent)
1305{
1306 int i;
1307 struct kvm_mmu_page *sp;
1308 struct mmu_page_path parents;
1309 struct kvm_mmu_pages pages;
1310
1311 kvm_mmu_pages_init(parent, &parents, &pages);
1312 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001313 int protected = 0;
1314
1315 for_each_sp(pages, sp, parents, i)
1316 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1317
1318 if (protected)
1319 kvm_flush_remote_tlbs(vcpu->kvm);
1320
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001321 for_each_sp(pages, sp, parents, i) {
1322 kvm_sync_page(vcpu, sp);
1323 mmu_pages_clear_parents(&parents);
1324 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001325 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001326 kvm_mmu_pages_init(parent, &parents, &pages);
1327 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001328}
1329
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001330static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1331 gfn_t gfn,
1332 gva_t gaddr,
1333 unsigned level,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001334 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001335 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001336 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001337{
1338 union kvm_mmu_page_role role;
1339 unsigned index;
1340 unsigned quadrant;
1341 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001342 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001343 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001344
Avi Kivitya770f6f2008-12-21 19:20:09 +02001345 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001346 role.level = level;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001347 role.direct = direct;
Avi Kivity41074d02007-12-09 17:00:02 +02001348 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001349 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001350 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1351 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1352 role.quadrant = quadrant;
1353 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001354 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001355 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001356 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1357 if (sp->gfn == gfn) {
1358 if (sp->unsync)
1359 if (kvm_sync_page(vcpu, sp))
1360 continue;
1361
1362 if (sp->role.word != role.word)
1363 continue;
1364
Avi Kivity4db35312007-11-21 15:28:32 +02001365 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001366 if (sp->unsync_children) {
1367 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1368 kvm_mmu_mark_parents_unsync(vcpu, sp);
1369 }
Avi Kivityf691fe12009-07-06 15:58:14 +03001370 trace_kvm_mmu_get_page(sp, false);
Avi Kivity4db35312007-11-21 15:28:32 +02001371 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001372 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001373 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001374 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1375 if (!sp)
1376 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001377 sp->gfn = gfn;
1378 sp->role = role;
1379 hlist_add_head(&sp->hash_link, bucket);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001380 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001381 if (rmap_write_protect(vcpu->kvm, gfn))
1382 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001383 account_shadowed(vcpu->kvm, gfn);
1384 }
Avi Kivity131d8272008-05-29 14:56:28 +03001385 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1386 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1387 else
1388 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001389 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001390 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001391}
1392
Avi Kivity2d111232008-12-25 14:39:47 +02001393static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1394 struct kvm_vcpu *vcpu, u64 addr)
1395{
1396 iterator->addr = addr;
1397 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1398 iterator->level = vcpu->arch.mmu.shadow_root_level;
1399 if (iterator->level == PT32E_ROOT_LEVEL) {
1400 iterator->shadow_addr
1401 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1402 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1403 --iterator->level;
1404 if (!iterator->shadow_addr)
1405 iterator->level = 0;
1406 }
1407}
1408
1409static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1410{
1411 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1412 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001413
1414 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1415 if (is_large_pte(*iterator->sptep))
1416 return false;
1417
Avi Kivity2d111232008-12-25 14:39:47 +02001418 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1419 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1420 return true;
1421}
1422
1423static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1424{
1425 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1426 --iterator->level;
1427}
1428
Avi Kivity90cb0522007-07-17 13:04:56 +03001429static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001430 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001431{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001432 unsigned i;
1433 u64 *pt;
1434 u64 ent;
1435
Avi Kivity4db35312007-11-21 15:28:32 +02001436 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001437
Avi Kivity697fe2e2007-01-05 16:36:46 -08001438 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1439 ent = pt[i];
1440
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001441 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001442 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001443 ent &= PT64_BASE_ADDR_MASK;
1444 mmu_page_remove_parent_pte(page_header(ent),
1445 &pt[i]);
1446 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001447 if (is_large_pte(ent))
1448 --kvm->stat.lpages;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001449 rmap_remove(kvm, &pt[i]);
1450 }
1451 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001452 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001453 }
Avi Kivitya4360362007-01-05 16:36:45 -08001454}
1455
Avi Kivity4db35312007-11-21 15:28:32 +02001456static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001457{
Avi Kivity4db35312007-11-21 15:28:32 +02001458 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001459}
1460
Avi Kivity12b7d282007-09-23 14:10:49 +02001461static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1462{
1463 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001464 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001465
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001466 kvm_for_each_vcpu(i, vcpu, kvm)
1467 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001468}
1469
Avi Kivity31aa2b42008-07-11 17:59:46 +03001470static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001471{
1472 u64 *parent_pte;
1473
Avi Kivity4db35312007-11-21 15:28:32 +02001474 while (sp->multimapped || sp->parent_pte) {
1475 if (!sp->multimapped)
1476 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001477 else {
1478 struct kvm_pte_chain *chain;
1479
Avi Kivity4db35312007-11-21 15:28:32 +02001480 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001481 struct kvm_pte_chain, link);
1482 parent_pte = chain->parent_ptes[0];
1483 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001484 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001485 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001486 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001487 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001488}
1489
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001490static int mmu_zap_unsync_children(struct kvm *kvm,
1491 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001492{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001493 int i, zapped = 0;
1494 struct mmu_page_path parents;
1495 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001496
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001497 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001498 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001499
1500 kvm_mmu_pages_init(parent, &parents, &pages);
1501 while (mmu_unsync_walk(parent, &pages)) {
1502 struct kvm_mmu_page *sp;
1503
1504 for_each_sp(pages, sp, parents, i) {
1505 kvm_mmu_zap_page(kvm, sp);
1506 mmu_pages_clear_parents(&parents);
1507 }
1508 zapped += pages.nr;
1509 kvm_mmu_pages_init(parent, &parents, &pages);
1510 }
1511
1512 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001513}
1514
Marcelo Tosatti07385412008-09-23 13:18:37 -03001515static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001516{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001517 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001518
1519 trace_kvm_mmu_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001520 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001521 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001522 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001523 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001524 kvm_flush_remote_tlbs(kvm);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001525 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001526 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001527 if (sp->unsync)
1528 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001529 if (!sp->root_count) {
1530 hlist_del(&sp->hash_link);
1531 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001532 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001533 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001534 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001535 kvm_reload_remote_mmus(kvm);
1536 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001537 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001538 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001539}
1540
Izik Eidus82ce2c92007-10-02 18:52:55 +02001541/*
1542 * Changing the number of mmu pages allocated to the vm
1543 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1544 */
1545void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1546{
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001547 int used_pages;
1548
1549 used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1550 used_pages = max(0, used_pages);
1551
Izik Eidus82ce2c92007-10-02 18:52:55 +02001552 /*
1553 * If we set the number of mmu pages to be smaller be than the
1554 * number of actived pages , we must to free some mmu pages before we
1555 * change the value
1556 */
1557
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001558 if (used_pages > kvm_nr_mmu_pages) {
1559 while (used_pages > kvm_nr_mmu_pages) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001560 struct kvm_mmu_page *page;
1561
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001562 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001563 struct kvm_mmu_page, link);
1564 kvm_mmu_zap_page(kvm, page);
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001565 used_pages--;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001566 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001567 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001568 }
1569 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001570 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1571 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001572
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001573 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001574}
1575
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001576static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001577{
1578 unsigned index;
1579 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001580 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001581 struct hlist_node *node, *n;
1582 int r;
1583
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001584 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001585 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001586 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001587 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001588 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001589 if (sp->gfn == gfn && !sp->role.direct) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001590 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001591 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001592 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001593 if (kvm_mmu_zap_page(kvm, sp))
1594 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001595 }
1596 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001597}
1598
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001599static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001600{
Avi Kivity4677a3b2009-01-06 13:00:27 +02001601 unsigned index;
1602 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001603 struct kvm_mmu_page *sp;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001604 struct hlist_node *node, *nn;
Avi Kivity97a0a012007-05-31 15:08:29 +03001605
Avi Kivity4677a3b2009-01-06 13:00:27 +02001606 index = kvm_page_table_hashfn(gfn);
1607 bucket = &kvm->arch.mmu_page_hash[index];
1608 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001609 if (sp->gfn == gfn && !sp->role.direct
Avi Kivity4677a3b2009-01-06 13:00:27 +02001610 && !sp->role.invalid) {
1611 pgprintk("%s: zap %lx %x\n",
1612 __func__, gfn, sp->role.word);
1613 kvm_mmu_zap_page(kvm, sp);
1614 }
Avi Kivity97a0a012007-05-31 15:08:29 +03001615 }
1616}
1617
Avi Kivity38c335f2007-11-21 14:20:22 +02001618static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001619{
Avi Kivity38c335f2007-11-21 14:20:22 +02001620 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001621 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001622
Sheng Yang291f26b2008-10-16 17:30:57 +08001623 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001624}
1625
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001626static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1627{
1628 int i;
1629 u64 *pt = sp->spt;
1630
1631 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1632 return;
1633
1634 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1635 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001636 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001637 }
1638}
1639
Avi Kivity039576c2007-03-20 12:46:50 +02001640struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1641{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001642 struct page *page;
1643
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001644 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001645
1646 if (gpa == UNMAPPED_GVA)
1647 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001648
Izik Eidus72dc67a2008-02-10 18:04:15 +02001649 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001650
1651 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001652}
1653
Sheng Yang74be52e2008-10-09 16:01:56 +08001654/*
1655 * The function is based on mtrr_type_lookup() in
1656 * arch/x86/kernel/cpu/mtrr/generic.c
1657 */
1658static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1659 u64 start, u64 end)
1660{
1661 int i;
1662 u64 base, mask;
1663 u8 prev_match, curr_match;
1664 int num_var_ranges = KVM_NR_VAR_MTRR;
1665
1666 if (!mtrr_state->enabled)
1667 return 0xFF;
1668
1669 /* Make end inclusive end, instead of exclusive */
1670 end--;
1671
1672 /* Look in fixed ranges. Just return the type as per start */
1673 if (mtrr_state->have_fixed && (start < 0x100000)) {
1674 int idx;
1675
1676 if (start < 0x80000) {
1677 idx = 0;
1678 idx += (start >> 16);
1679 return mtrr_state->fixed_ranges[idx];
1680 } else if (start < 0xC0000) {
1681 idx = 1 * 8;
1682 idx += ((start - 0x80000) >> 14);
1683 return mtrr_state->fixed_ranges[idx];
1684 } else if (start < 0x1000000) {
1685 idx = 3 * 8;
1686 idx += ((start - 0xC0000) >> 12);
1687 return mtrr_state->fixed_ranges[idx];
1688 }
1689 }
1690
1691 /*
1692 * Look in variable ranges
1693 * Look of multiple ranges matching this address and pick type
1694 * as per MTRR precedence
1695 */
1696 if (!(mtrr_state->enabled & 2))
1697 return mtrr_state->def_type;
1698
1699 prev_match = 0xFF;
1700 for (i = 0; i < num_var_ranges; ++i) {
1701 unsigned short start_state, end_state;
1702
1703 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1704 continue;
1705
1706 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1707 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1708 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1709 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1710
1711 start_state = ((start & mask) == (base & mask));
1712 end_state = ((end & mask) == (base & mask));
1713 if (start_state != end_state)
1714 return 0xFE;
1715
1716 if ((start & mask) != (base & mask))
1717 continue;
1718
1719 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1720 if (prev_match == 0xFF) {
1721 prev_match = curr_match;
1722 continue;
1723 }
1724
1725 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1726 curr_match == MTRR_TYPE_UNCACHABLE)
1727 return MTRR_TYPE_UNCACHABLE;
1728
1729 if ((prev_match == MTRR_TYPE_WRBACK &&
1730 curr_match == MTRR_TYPE_WRTHROUGH) ||
1731 (prev_match == MTRR_TYPE_WRTHROUGH &&
1732 curr_match == MTRR_TYPE_WRBACK)) {
1733 prev_match = MTRR_TYPE_WRTHROUGH;
1734 curr_match = MTRR_TYPE_WRTHROUGH;
1735 }
1736
1737 if (prev_match != curr_match)
1738 return MTRR_TYPE_UNCACHABLE;
1739 }
1740
1741 if (prev_match != 0xFF)
1742 return prev_match;
1743
1744 return mtrr_state->def_type;
1745}
1746
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001747u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001748{
1749 u8 mtrr;
1750
1751 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1752 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1753 if (mtrr == 0xfe || mtrr == 0xff)
1754 mtrr = MTRR_TYPE_WRBACK;
1755 return mtrr;
1756}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001757EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001758
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001759static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1760{
1761 unsigned index;
1762 struct hlist_head *bucket;
1763 struct kvm_mmu_page *s;
1764 struct hlist_node *node, *n;
1765
Avi Kivityf691fe12009-07-06 15:58:14 +03001766 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001767 index = kvm_page_table_hashfn(sp->gfn);
1768 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1769 /* don't unsync if pagetable is shadowed with multiple roles */
1770 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001771 if (s->gfn != sp->gfn || s->role.direct)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001772 continue;
1773 if (s->role.word != sp->role.word)
1774 return 1;
1775 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001776 ++vcpu->kvm->stat.mmu_unsync;
1777 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001778
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001779 kvm_mmu_mark_parents_unsync(vcpu, sp);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001780
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001781 mmu_convert_notrap(sp);
1782 return 0;
1783}
1784
1785static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1786 bool can_unsync)
1787{
1788 struct kvm_mmu_page *shadow;
1789
1790 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1791 if (shadow) {
1792 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1793 return 1;
1794 if (shadow->unsync)
1795 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001796 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001797 return kvm_unsync_page(vcpu, shadow);
1798 return 1;
1799 }
1800 return 0;
1801}
1802
Avi Kivityd555c332009-06-10 14:24:23 +03001803static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001804 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001805 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001806 gfn_t gfn, pfn_t pfn, bool speculative,
Izik Eidus14032832009-09-23 21:47:17 +03001807 bool can_unsync, bool reset_host_protection)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001808{
1809 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001810 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001811
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001812 /*
1813 * We don't set the accessed bit, since we sometimes want to see
1814 * whether the guest actually used the pte (in order to detect
1815 * demand paging).
1816 */
Sheng Yang7b523452008-04-25 21:13:50 +08001817 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001818 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001819 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001820 if (!dirty)
1821 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001822 if (pte_access & ACC_EXEC_MASK)
1823 spte |= shadow_x_mask;
1824 else
1825 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001826 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001827 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001828 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001829 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001830 if (tdp_enabled)
1831 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1832 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001833
Izik Eidus14032832009-09-23 21:47:17 +03001834 if (reset_host_protection)
1835 spte |= SPTE_HOST_WRITEABLE;
1836
Anthony Liguori35149e22008-04-02 14:46:56 -05001837 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001838
1839 if ((pte_access & ACC_WRITE_MASK)
1840 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001841
Joerg Roedel852e3c12009-07-27 16:30:44 +02001842 if (level > PT_PAGE_TABLE_LEVEL &&
1843 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001844 ret = 1;
1845 spte = shadow_trap_nonpresent_pte;
1846 goto set_pte;
1847 }
1848
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001849 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001850
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001851 /*
1852 * Optimization: for pte sync, if spte was writable the hash
1853 * lookup is unnecessary (and expensive). Write protection
1854 * is responsibility of mmu_get_page / kvm_sync_page.
1855 * Same reasoning can be applied to dirty page accounting.
1856 */
Avi Kivityd555c332009-06-10 14:24:23 +03001857 if (!can_unsync && is_writeble_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001858 goto set_pte;
1859
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001860 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001861 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001862 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001863 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001864 pte_access &= ~ACC_WRITE_MASK;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001865 if (is_writeble_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001866 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001867 }
1868 }
1869
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001870 if (pte_access & ACC_WRITE_MASK)
1871 mark_page_dirty(vcpu->kvm, gfn);
1872
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001873set_pte:
Avi Kivityd555c332009-06-10 14:24:23 +03001874 __set_spte(sptep, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001875 return ret;
1876}
1877
Avi Kivityd555c332009-06-10 14:24:23 +03001878static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001879 unsigned pt_access, unsigned pte_access,
1880 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001881 int *ptwrite, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03001882 pfn_t pfn, bool speculative,
1883 bool reset_host_protection)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001884{
1885 int was_rmapped = 0;
Avi Kivityd555c332009-06-10 14:24:23 +03001886 int was_writeble = is_writeble_pte(*sptep);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001887 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001888
1889 pgprintk("%s: spte %llx access %x write_fault %d"
1890 " user_fault %d gfn %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001891 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001892 write_fault, user_fault, gfn);
1893
Avi Kivityd555c332009-06-10 14:24:23 +03001894 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001895 /*
1896 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1897 * the parent of the now unreachable PTE.
1898 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02001899 if (level > PT_PAGE_TABLE_LEVEL &&
1900 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001901 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03001902 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001903
1904 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03001905 mmu_page_remove_parent_pte(child, sptep);
1906 } else if (pfn != spte_to_pfn(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001907 pgprintk("hfn old %lx new %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001908 spte_to_pfn(*sptep), pfn);
1909 rmap_remove(vcpu->kvm, sptep);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001910 } else
1911 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001912 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02001913
Avi Kivityd555c332009-06-10 14:24:23 +03001914 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Izik Eidus14032832009-09-23 21:47:17 +03001915 dirty, level, gfn, pfn, speculative, true,
1916 reset_host_protection)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001917 if (write_fault)
1918 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001919 kvm_x86_ops->tlb_flush(vcpu);
1920 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001921
Avi Kivityd555c332009-06-10 14:24:23 +03001922 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001923 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001924 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02001925 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
1926 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03001927 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001928 ++vcpu->kvm->stat.lpages;
1929
Avi Kivityd555c332009-06-10 14:24:23 +03001930 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001931 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02001932 rmap_count = rmap_add(vcpu, sptep, gfn);
Izik Eidusacb66dd2009-09-23 21:47:16 +03001933 kvm_release_pfn_clean(pfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001934 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02001935 rmap_recycle(vcpu, sptep, gfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001936 } else {
1937 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001938 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001939 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001940 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001941 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001942 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03001943 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001944 vcpu->arch.last_pte_gfn = gfn;
1945 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001946}
1947
Avi Kivity6aa8b732006-12-10 02:21:36 -08001948static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1949{
1950}
1951
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001952static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001953 int level, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001954{
Avi Kivity9f652d22008-12-25 14:54:25 +02001955 struct kvm_shadow_walk_iterator iterator;
1956 struct kvm_mmu_page *sp;
1957 int pt_write = 0;
1958 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001959
Avi Kivity9f652d22008-12-25 14:54:25 +02001960 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02001961 if (iterator.level == level) {
Avi Kivity9f652d22008-12-25 14:54:25 +02001962 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1963 0, write, 1, &pt_write,
Izik Eidus14032832009-09-23 21:47:17 +03001964 level, gfn, pfn, false, true);
Avi Kivity9f652d22008-12-25 14:54:25 +02001965 ++vcpu->stat.pf_fixed;
1966 break;
1967 }
1968
1969 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1970 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1971 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1972 iterator.level - 1,
1973 1, ACC_ALL, iterator.sptep);
1974 if (!sp) {
1975 pgprintk("nonpaging_map: ENOMEM\n");
1976 kvm_release_pfn_clean(pfn);
1977 return -ENOMEM;
1978 }
1979
Avi Kivityd555c332009-06-10 14:24:23 +03001980 __set_spte(iterator.sptep,
1981 __pa(sp->spt)
1982 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1983 | shadow_user_mask | shadow_x_mask);
Avi Kivity9f652d22008-12-25 14:54:25 +02001984 }
1985 }
1986 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001987}
1988
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001989static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1990{
1991 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001992 int level;
Anthony Liguori35149e22008-04-02 14:46:56 -05001993 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001994 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001995
Joerg Roedel852e3c12009-07-27 16:30:44 +02001996 level = mapping_level(vcpu, gfn);
1997
1998 /*
1999 * This path builds a PAE pagetable - so we can map 2mb pages at
2000 * maximum. Therefore check if the level is larger than that.
2001 */
2002 if (level > PT_DIRECTORY_LEVEL)
2003 level = PT_DIRECTORY_LEVEL;
2004
2005 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002006
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002007 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002008 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002009 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002010
Avi Kivityd196e342008-01-24 11:44:11 +02002011 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05002012 if (is_error_pfn(pfn)) {
2013 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002014 return 1;
2015 }
2016
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002017 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002018 if (mmu_notifier_retry(vcpu, mmu_seq))
2019 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02002020 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel852e3c12009-07-27 16:30:44 +02002021 r = __direct_map(vcpu, v, write, level, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002022 spin_unlock(&vcpu->kvm->mmu_lock);
2023
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002024
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002025 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002026
2027out_unlock:
2028 spin_unlock(&vcpu->kvm->mmu_lock);
2029 kvm_release_pfn_clean(pfn);
2030 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002031}
2032
2033
Avi Kivity17ac10a2007-01-05 16:36:40 -08002034static void mmu_free_roots(struct kvm_vcpu *vcpu)
2035{
2036 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002037 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002038
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002039 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002040 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002041 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002042 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2043 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002044
Avi Kivity4db35312007-11-21 15:28:32 +02002045 sp = page_header(root);
2046 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002047 if (!sp->root_count && sp->role.invalid)
2048 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002049 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002050 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002051 return;
2052 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002053 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002054 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002055
Avi Kivity417726a2007-04-12 17:35:58 +03002056 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002057 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002058 sp = page_header(root);
2059 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002060 if (!sp->root_count && sp->role.invalid)
2061 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03002062 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002063 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002064 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002065 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002066 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002067}
2068
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002069static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2070{
2071 int ret = 0;
2072
2073 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
2074 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
2075 ret = 1;
2076 }
2077
2078 return ret;
2079}
2080
2081static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002082{
2083 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002084 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02002085 struct kvm_mmu_page *sp;
Avi Kivityf6e2c022009-01-11 13:02:10 +02002086 int direct = 0;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002087 u64 pdptr;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002088
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002089 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002090
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002091 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2092 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002093
2094 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01002095 if (tdp_enabled)
Avi Kivityf6e2c022009-01-11 13:02:10 +02002096 direct = 1;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002097 if (mmu_check_root(vcpu, root_gfn))
2098 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002099 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c022009-01-11 13:02:10 +02002100 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01002101 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002102 root = __pa(sp->spt);
2103 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002104 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002105 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002106 }
Avi Kivityf6e2c022009-01-11 13:02:10 +02002107 direct = !is_paging(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002108 if (tdp_enabled)
Avi Kivityf6e2c022009-01-11 13:02:10 +02002109 direct = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002110 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002111 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002112
2113 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002114 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002115 pdptr = kvm_pdptr_read(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002116 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002117 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002118 continue;
2119 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002120 root_gfn = pdptr >> PAGE_SHIFT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002121 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002122 root_gfn = 0;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002123 if (mmu_check_root(vcpu, root_gfn))
2124 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002125 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c022009-01-11 13:02:10 +02002126 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002127 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002128 root = __pa(sp->spt);
2129 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002130 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002131 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002132 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002133 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002134}
2135
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002136static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2137{
2138 int i;
2139 struct kvm_mmu_page *sp;
2140
2141 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2142 return;
2143 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2144 hpa_t root = vcpu->arch.mmu.root_hpa;
2145 sp = page_header(root);
2146 mmu_sync_children(vcpu, sp);
2147 return;
2148 }
2149 for (i = 0; i < 4; ++i) {
2150 hpa_t root = vcpu->arch.mmu.pae_root[i];
2151
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002152 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002153 root &= PT64_BASE_ADDR_MASK;
2154 sp = page_header(root);
2155 mmu_sync_children(vcpu, sp);
2156 }
2157 }
2158}
2159
2160void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2161{
2162 spin_lock(&vcpu->kvm->mmu_lock);
2163 mmu_sync_roots(vcpu);
2164 spin_unlock(&vcpu->kvm->mmu_lock);
2165}
2166
Avi Kivity6aa8b732006-12-10 02:21:36 -08002167static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2168{
2169 return vaddr;
2170}
2171
2172static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002173 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002174{
Avi Kivitye8332402007-12-09 18:43:00 +02002175 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002176 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002177
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002178 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002179 r = mmu_topup_memory_caches(vcpu);
2180 if (r)
2181 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002182
Avi Kivity6aa8b732006-12-10 02:21:36 -08002183 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002184 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002185
Avi Kivitye8332402007-12-09 18:43:00 +02002186 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002187
Avi Kivitye8332402007-12-09 18:43:00 +02002188 return nonpaging_map(vcpu, gva & PAGE_MASK,
2189 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002190}
2191
Joerg Roedelfb72d162008-02-07 13:47:44 +01002192static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2193 u32 error_code)
2194{
Anthony Liguori35149e22008-04-02 14:46:56 -05002195 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002196 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002197 int level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002198 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002199 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002200
2201 ASSERT(vcpu);
2202 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2203
2204 r = mmu_topup_memory_caches(vcpu);
2205 if (r)
2206 return r;
2207
Joerg Roedel852e3c12009-07-27 16:30:44 +02002208 level = mapping_level(vcpu, gfn);
2209
2210 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2211
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002212 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002213 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002214 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002215 if (is_error_pfn(pfn)) {
2216 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002217 return 1;
2218 }
2219 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002220 if (mmu_notifier_retry(vcpu, mmu_seq))
2221 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002222 kvm_mmu_free_some_pages(vcpu);
2223 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002224 level, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002225 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002226
2227 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002228
2229out_unlock:
2230 spin_unlock(&vcpu->kvm->mmu_lock);
2231 kvm_release_pfn_clean(pfn);
2232 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002233}
2234
Avi Kivity6aa8b732006-12-10 02:21:36 -08002235static void nonpaging_free(struct kvm_vcpu *vcpu)
2236{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002237 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002238}
2239
2240static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2241{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002242 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002243
2244 context->new_cr3 = nonpaging_new_cr3;
2245 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002246 context->gva_to_gpa = nonpaging_gva_to_gpa;
2247 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002248 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002249 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002250 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002251 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002252 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002253 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002254 return 0;
2255}
2256
Avi Kivityd835dfe2007-11-21 02:57:59 +02002257void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002258{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002259 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002260 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002261}
2262
2263static void paging_new_cr3(struct kvm_vcpu *vcpu)
2264{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002265 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002266 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002267}
2268
Avi Kivity6aa8b732006-12-10 02:21:36 -08002269static void inject_page_fault(struct kvm_vcpu *vcpu,
2270 u64 addr,
2271 u32 err_code)
2272{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002273 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002274}
2275
Avi Kivity6aa8b732006-12-10 02:21:36 -08002276static void paging_free(struct kvm_vcpu *vcpu)
2277{
2278 nonpaging_free(vcpu);
2279}
2280
Dong, Eddie82725b22009-03-30 16:21:08 +08002281static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2282{
2283 int bit7;
2284
2285 bit7 = (gpte >> 7) & 1;
2286 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2287}
2288
Avi Kivity6aa8b732006-12-10 02:21:36 -08002289#define PTTYPE 64
2290#include "paging_tmpl.h"
2291#undef PTTYPE
2292
2293#define PTTYPE 32
2294#include "paging_tmpl.h"
2295#undef PTTYPE
2296
Dong, Eddie82725b22009-03-30 16:21:08 +08002297static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2298{
2299 struct kvm_mmu *context = &vcpu->arch.mmu;
2300 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2301 u64 exb_bit_rsvd = 0;
2302
2303 if (!is_nx(vcpu))
2304 exb_bit_rsvd = rsvd_bits(63, 63);
2305 switch (level) {
2306 case PT32_ROOT_LEVEL:
2307 /* no rsvd bits for 2 level 4K page table entries */
2308 context->rsvd_bits_mask[0][1] = 0;
2309 context->rsvd_bits_mask[0][0] = 0;
2310 if (is_cpuid_PSE36())
2311 /* 36bits PSE 4MB page */
2312 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2313 else
2314 /* 32 bits PSE 4MB page */
2315 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Avi Kivity29a4b932009-05-19 13:29:27 +03002316 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002317 break;
2318 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002319 context->rsvd_bits_mask[0][2] =
2320 rsvd_bits(maxphyaddr, 63) |
2321 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002322 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002323 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002324 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2325 rsvd_bits(maxphyaddr, 62); /* PTE */
2326 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2327 rsvd_bits(maxphyaddr, 62) |
2328 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002329 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002330 break;
2331 case PT64_ROOT_LEVEL:
2332 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2333 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2334 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2335 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2336 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002337 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002338 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2339 rsvd_bits(maxphyaddr, 51);
2340 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002341 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2342 rsvd_bits(maxphyaddr, 51) |
2343 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002344 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002345 rsvd_bits(maxphyaddr, 51) |
2346 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002347 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002348 break;
2349 }
2350}
2351
Avi Kivity17ac10a2007-01-05 16:36:40 -08002352static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002353{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002354 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002355
2356 ASSERT(is_pae(vcpu));
2357 context->new_cr3 = paging_new_cr3;
2358 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002359 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002360 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002361 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002362 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002363 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002364 context->root_level = level;
2365 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002366 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002367 return 0;
2368}
2369
Avi Kivity17ac10a2007-01-05 16:36:40 -08002370static int paging64_init_context(struct kvm_vcpu *vcpu)
2371{
Dong, Eddie82725b22009-03-30 16:21:08 +08002372 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002373 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2374}
2375
Avi Kivity6aa8b732006-12-10 02:21:36 -08002376static int paging32_init_context(struct kvm_vcpu *vcpu)
2377{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002378 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379
Dong, Eddie82725b22009-03-30 16:21:08 +08002380 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002381 context->new_cr3 = paging_new_cr3;
2382 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002383 context->gva_to_gpa = paging32_gva_to_gpa;
2384 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002385 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002386 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002387 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002388 context->root_level = PT32_ROOT_LEVEL;
2389 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002390 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002391 return 0;
2392}
2393
2394static int paging32E_init_context(struct kvm_vcpu *vcpu)
2395{
Dong, Eddie82725b22009-03-30 16:21:08 +08002396 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002397 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002398}
2399
Joerg Roedelfb72d162008-02-07 13:47:44 +01002400static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2401{
2402 struct kvm_mmu *context = &vcpu->arch.mmu;
2403
2404 context->new_cr3 = nonpaging_new_cr3;
2405 context->page_fault = tdp_page_fault;
2406 context->free = nonpaging_free;
2407 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002408 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002409 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002410 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002411 context->root_hpa = INVALID_PAGE;
2412
2413 if (!is_paging(vcpu)) {
2414 context->gva_to_gpa = nonpaging_gva_to_gpa;
2415 context->root_level = 0;
2416 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002417 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002418 context->gva_to_gpa = paging64_gva_to_gpa;
2419 context->root_level = PT64_ROOT_LEVEL;
2420 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002421 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002422 context->gva_to_gpa = paging64_gva_to_gpa;
2423 context->root_level = PT32E_ROOT_LEVEL;
2424 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002425 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002426 context->gva_to_gpa = paging32_gva_to_gpa;
2427 context->root_level = PT32_ROOT_LEVEL;
2428 }
2429
2430 return 0;
2431}
2432
2433static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002434{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002435 int r;
2436
Avi Kivity6aa8b732006-12-10 02:21:36 -08002437 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002438 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002439
2440 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002441 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002442 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002443 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002444 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002445 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002446 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002447 r = paging32_init_context(vcpu);
2448
2449 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2450
2451 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002452}
2453
Joerg Roedelfb72d162008-02-07 13:47:44 +01002454static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2455{
Anthony Liguori35149e22008-04-02 14:46:56 -05002456 vcpu->arch.update_pte.pfn = bad_pfn;
2457
Joerg Roedelfb72d162008-02-07 13:47:44 +01002458 if (tdp_enabled)
2459 return init_kvm_tdp_mmu(vcpu);
2460 else
2461 return init_kvm_softmmu(vcpu);
2462}
2463
Avi Kivity6aa8b732006-12-10 02:21:36 -08002464static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2465{
2466 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002467 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2468 vcpu->arch.mmu.free(vcpu);
2469 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002470 }
2471}
2472
2473int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2474{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002475 destroy_kvm_mmu(vcpu);
2476 return init_kvm_mmu(vcpu);
2477}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002478EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002479
2480int kvm_mmu_load(struct kvm_vcpu *vcpu)
2481{
Avi Kivity714b93d2007-01-05 16:36:53 -08002482 int r;
2483
Avi Kivitye2dec932007-01-05 16:36:54 -08002484 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002485 if (r)
2486 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002487 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002488 kvm_mmu_free_some_pages(vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002489 r = mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002490 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002491 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002492 if (r)
2493 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08002494 /* set_cr3() should ensure TLB has been flushed */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002495 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08002496out:
2497 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002498}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002499EXPORT_SYMBOL_GPL(kvm_mmu_load);
2500
2501void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2502{
2503 mmu_free_roots(vcpu);
2504}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002505
Avi Kivity09072da2007-05-01 14:16:52 +03002506static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002507 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002508 u64 *spte)
2509{
2510 u64 pte;
2511 struct kvm_mmu_page *child;
2512
2513 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002514 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03002515 if (is_last_spte(pte, sp->role.level))
Izik Eidus290fc382007-09-27 14:11:22 +02002516 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002517 else {
2518 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002519 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002520 }
2521 }
Avi Kivityd555c332009-06-10 14:24:23 +03002522 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002523 if (is_large_pte(pte))
2524 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002525}
2526
Avi Kivity00284252007-05-01 16:53:31 +03002527static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002528 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002529 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002530 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002531{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002532 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02002533 ++vcpu->kvm->stat.mmu_pde_zapped;
2534 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03002535 }
Avi Kivity00284252007-05-01 16:53:31 +03002536
Avi Kivity4cee5762007-11-18 16:37:07 +02002537 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002538 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002539 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002540 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002541 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002542}
2543
Avi Kivity79539ce2007-11-21 02:06:21 +02002544static bool need_remote_flush(u64 old, u64 new)
2545{
2546 if (!is_shadow_present_pte(old))
2547 return false;
2548 if (!is_shadow_present_pte(new))
2549 return true;
2550 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2551 return true;
2552 old ^= PT64_NX_MASK;
2553 new ^= PT64_NX_MASK;
2554 return (old & ~new & PT64_PERM_MASK) != 0;
2555}
2556
2557static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2558{
2559 if (need_remote_flush(old, new))
2560 kvm_flush_remote_tlbs(vcpu->kvm);
2561 else
2562 kvm_mmu_flush_tlb(vcpu);
2563}
2564
Avi Kivity12b7d282007-09-23 14:10:49 +02002565static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2566{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002567 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002568
Sheng Yang7b523452008-04-25 21:13:50 +08002569 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002570}
2571
Avi Kivityd7824ff2007-12-30 12:29:05 +02002572static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2573 const u8 *new, int bytes)
2574{
2575 gfn_t gfn;
2576 int r;
2577 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002578 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002579
2580 if (bytes != 4 && bytes != 8)
2581 return;
2582
2583 /*
2584 * Assume that the pte write on a page table of the same type
2585 * as the current vcpu paging mode. This is nearly always true
2586 * (might be false while changing modes). Note it is verified later
2587 * by update_pte().
2588 */
2589 if (is_pae(vcpu)) {
2590 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2591 if ((bytes == 4) && (gpa % 4 == 0)) {
2592 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2593 if (r)
2594 return;
2595 memcpy((void *)&gpte + (gpa % 8), new, 4);
2596 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2597 memcpy((void *)&gpte, new, 8);
2598 }
2599 } else {
2600 if ((bytes == 4) && (gpa % 4 == 0))
2601 memcpy((void *)&gpte, new, 4);
2602 }
Avi Kivity43a37952009-06-10 14:12:05 +03002603 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02002604 return;
2605 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002606
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002607 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002608 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002609 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002610
Anthony Liguori35149e22008-04-02 14:46:56 -05002611 if (is_error_pfn(pfn)) {
2612 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002613 return;
2614 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002615 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002616 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002617}
2618
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002619static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2620{
2621 u64 *spte = vcpu->arch.last_pte_updated;
2622
2623 if (spte
2624 && vcpu->arch.last_pte_gfn == gfn
2625 && shadow_accessed_mask
2626 && !(*spte & shadow_accessed_mask)
2627 && is_shadow_present_pte(*spte))
2628 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2629}
2630
Avi Kivity09072da2007-05-01 14:16:52 +03002631void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002632 const u8 *new, int bytes,
2633 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002634{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002635 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002636 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002637 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002638 struct hlist_head *bucket;
2639 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002640 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002641 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002642 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002643 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002644 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002645 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002646 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002647 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002648 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002649 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002650 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002651
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002652 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002653 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002654 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002655 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002656 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002657 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002658 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002659 if (guest_initiated) {
2660 if (gfn == vcpu->arch.last_pt_write_gfn
2661 && !last_updated_pte_accessed(vcpu)) {
2662 ++vcpu->arch.last_pt_write_count;
2663 if (vcpu->arch.last_pt_write_count >= 3)
2664 flooded = 1;
2665 } else {
2666 vcpu->arch.last_pt_write_gfn = gfn;
2667 vcpu->arch.last_pt_write_count = 1;
2668 vcpu->arch.last_pte_updated = NULL;
2669 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002670 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002671 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002672 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002673 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02002674 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002675 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002676 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002677 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002678 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002679 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002680 /*
2681 * Misaligned accesses are too much trouble to fix
2682 * up; also, they usually indicate a page is not used
2683 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002684 *
2685 * If we're seeing too many writes to a page,
2686 * it may no longer be a page table, or we may be
2687 * forking, in which case it is better to unmap the
2688 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002689 */
2690 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002691 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002692 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2693 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002694 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002695 continue;
2696 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002697 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002698 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002699 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002700 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002701 page_offset <<= 1; /* 32->64 */
2702 /*
2703 * A 32-bit pde maps 4MB while the shadow pdes map
2704 * only 2MB. So we need to double the offset again
2705 * and zap two pdes instead of one.
2706 */
2707 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002708 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002709 page_offset <<= 1;
2710 npte = 2;
2711 }
Avi Kivityfce06572007-05-01 16:44:05 +03002712 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002713 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002714 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002715 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002716 }
Avi Kivity4db35312007-11-21 15:28:32 +02002717 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002718 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2719 gentry = 0;
2720 r = kvm_read_guest_atomic(vcpu->kvm,
2721 gpa & ~(u64)(pte_size - 1),
2722 &gentry, pte_size);
2723 new = (const void *)&gentry;
2724 if (r < 0)
2725 new = NULL;
2726 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002727 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002728 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002729 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002730 if (new)
2731 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002732 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002733 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002734 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002735 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002736 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002737 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002738 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2739 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2740 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002741 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002742}
2743
Avi Kivitya4360362007-01-05 16:36:45 -08002744int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2745{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002746 gpa_t gpa;
2747 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002748
Avi Kivity60f24782009-08-27 13:37:06 +03002749 if (tdp_enabled)
2750 return 0;
2751
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002752 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002753
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002754 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002755 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002756 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002757 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002758}
Avi Kivity577bdc42008-07-19 08:57:05 +03002759EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002760
Avi Kivity22d95b12007-09-14 20:26:06 +03002761void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002762{
Izik Eidus3b80fff2009-07-28 15:26:58 -03002763 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES &&
2764 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02002765 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002766
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002767 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002768 struct kvm_mmu_page, link);
2769 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002770 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002771 }
2772}
Avi Kivityebeace82007-01-05 16:36:47 -08002773
Avi Kivity30677142007-10-28 18:48:59 +02002774int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2775{
2776 int r;
2777 enum emulation_result er;
2778
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002779 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002780 if (r < 0)
2781 goto out;
2782
2783 if (!r) {
2784 r = 1;
2785 goto out;
2786 }
2787
Avi Kivityb733bfb2007-10-28 18:52:05 +02002788 r = mmu_topup_memory_caches(vcpu);
2789 if (r)
2790 goto out;
2791
Avi Kivity30677142007-10-28 18:48:59 +02002792 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002793
2794 switch (er) {
2795 case EMULATE_DONE:
2796 return 1;
2797 case EMULATE_DO_MMIO:
2798 ++vcpu->stat.mmio_exits;
2799 return 0;
2800 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002801 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2802 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2803 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02002804 default:
2805 BUG();
2806 }
2807out:
Avi Kivity30677142007-10-28 18:48:59 +02002808 return r;
2809}
2810EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2811
Marcelo Tosattia7052892008-09-23 13:18:35 -03002812void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2813{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002814 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002815 kvm_mmu_flush_tlb(vcpu);
2816 ++vcpu->stat.invlpg;
2817}
2818EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2819
Joerg Roedel18552672008-02-07 13:47:41 +01002820void kvm_enable_tdp(void)
2821{
2822 tdp_enabled = true;
2823}
2824EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2825
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002826void kvm_disable_tdp(void)
2827{
2828 tdp_enabled = false;
2829}
2830EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2831
Avi Kivity6aa8b732006-12-10 02:21:36 -08002832static void free_mmu_pages(struct kvm_vcpu *vcpu)
2833{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002834 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002835}
2836
2837static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2838{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002839 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002840 int i;
2841
2842 ASSERT(vcpu);
2843
Avi Kivity17ac10a2007-01-05 16:36:40 -08002844 /*
2845 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2846 * Therefore we need to allocate shadow page tables in the first
2847 * 4GB of memory, which happens to fit the DMA32 zone.
2848 */
2849 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2850 if (!page)
2851 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002852 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002853 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002854 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002855
Avi Kivity6aa8b732006-12-10 02:21:36 -08002856 return 0;
2857
2858error_1:
2859 free_mmu_pages(vcpu);
2860 return -ENOMEM;
2861}
2862
Ingo Molnar8018c272006-12-29 16:50:01 -08002863int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002864{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002865 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002866 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002867
Ingo Molnar8018c272006-12-29 16:50:01 -08002868 return alloc_mmu_pages(vcpu);
2869}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002870
Ingo Molnar8018c272006-12-29 16:50:01 -08002871int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2872{
2873 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002874 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002875
Ingo Molnar8018c272006-12-29 16:50:01 -08002876 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002877}
2878
2879void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2880{
2881 ASSERT(vcpu);
2882
2883 destroy_kvm_mmu(vcpu);
2884 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002885 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002886}
2887
Avi Kivity90cb0522007-07-17 13:04:56 +03002888void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002889{
Avi Kivity4db35312007-11-21 15:28:32 +02002890 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002891
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002892 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002893 int i;
2894 u64 *pt;
2895
Sheng Yang291f26b2008-10-16 17:30:57 +08002896 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002897 continue;
2898
Avi Kivity4db35312007-11-21 15:28:32 +02002899 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002900 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2901 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002902 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002903 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002904 }
Avi Kivity171d5952008-08-27 16:40:51 +03002905 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002906}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002907
Avi Kivity90cb0522007-07-17 13:04:56 +03002908void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002909{
Avi Kivity4db35312007-11-21 15:28:32 +02002910 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002911
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002912 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002913 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002914 if (kvm_mmu_zap_page(kvm, sp))
2915 node = container_of(kvm->arch.active_mmu_pages.next,
2916 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002917 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002918
Avi Kivity90cb0522007-07-17 13:04:56 +03002919 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002920}
2921
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002922static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002923{
2924 struct kvm_mmu_page *page;
2925
2926 page = container_of(kvm->arch.active_mmu_pages.prev,
2927 struct kvm_mmu_page, link);
2928 kvm_mmu_zap_page(kvm, page);
2929}
2930
2931static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2932{
2933 struct kvm *kvm;
2934 struct kvm *kvm_freed = NULL;
2935 int cache_count = 0;
2936
2937 spin_lock(&kvm_lock);
2938
2939 list_for_each_entry(kvm, &vm_list, vm_list) {
2940 int npages;
2941
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002942 if (!down_read_trylock(&kvm->slots_lock))
2943 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002944 spin_lock(&kvm->mmu_lock);
2945 npages = kvm->arch.n_alloc_mmu_pages -
2946 kvm->arch.n_free_mmu_pages;
2947 cache_count += npages;
2948 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2949 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2950 cache_count--;
2951 kvm_freed = kvm;
2952 }
2953 nr_to_scan--;
2954
2955 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002956 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002957 }
2958 if (kvm_freed)
2959 list_move_tail(&kvm_freed->vm_list, &vm_list);
2960
2961 spin_unlock(&kvm_lock);
2962
2963 return cache_count;
2964}
2965
2966static struct shrinker mmu_shrinker = {
2967 .shrink = mmu_shrink,
2968 .seeks = DEFAULT_SEEKS * 10,
2969};
2970
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002971static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002972{
2973 if (pte_chain_cache)
2974 kmem_cache_destroy(pte_chain_cache);
2975 if (rmap_desc_cache)
2976 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002977 if (mmu_page_header_cache)
2978 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002979}
2980
Izik Eidus3ee16c82008-03-30 15:17:21 +03002981void kvm_mmu_module_exit(void)
2982{
2983 mmu_destroy_caches();
2984 unregister_shrinker(&mmu_shrinker);
2985}
2986
Avi Kivityb5a33a72007-04-15 16:31:09 +03002987int kvm_mmu_module_init(void)
2988{
2989 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2990 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002991 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002992 if (!pte_chain_cache)
2993 goto nomem;
2994 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2995 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002996 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002997 if (!rmap_desc_cache)
2998 goto nomem;
2999
Avi Kivityd3d25b02007-05-30 12:34:53 +03003000 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
3001 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09003002 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003003 if (!mmu_page_header_cache)
3004 goto nomem;
3005
Izik Eidus3ee16c82008-03-30 15:17:21 +03003006 register_shrinker(&mmu_shrinker);
3007
Avi Kivityb5a33a72007-04-15 16:31:09 +03003008 return 0;
3009
3010nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03003011 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003012 return -ENOMEM;
3013}
3014
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003015/*
3016 * Caculate mmu pages needed for kvm.
3017 */
3018unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3019{
3020 int i;
3021 unsigned int nr_mmu_pages;
3022 unsigned int nr_pages = 0;
3023
3024 for (i = 0; i < kvm->nmemslots; i++)
3025 nr_pages += kvm->memslots[i].npages;
3026
3027 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3028 nr_mmu_pages = max(nr_mmu_pages,
3029 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3030
3031 return nr_mmu_pages;
3032}
3033
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003034static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3035 unsigned len)
3036{
3037 if (len > buffer->len)
3038 return NULL;
3039 return buffer->ptr;
3040}
3041
3042static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3043 unsigned len)
3044{
3045 void *ret;
3046
3047 ret = pv_mmu_peek_buffer(buffer, len);
3048 if (!ret)
3049 return ret;
3050 buffer->ptr += len;
3051 buffer->len -= len;
3052 buffer->processed += len;
3053 return ret;
3054}
3055
3056static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3057 gpa_t addr, gpa_t value)
3058{
3059 int bytes = 8;
3060 int r;
3061
3062 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3063 bytes = 4;
3064
3065 r = mmu_topup_memory_caches(vcpu);
3066 if (r)
3067 return r;
3068
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003069 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003070 return -EFAULT;
3071
3072 return 1;
3073}
3074
3075static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3076{
Avi Kivitya8cd0242009-05-24 22:15:25 +03003077 kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003078 return 1;
3079}
3080
3081static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3082{
3083 spin_lock(&vcpu->kvm->mmu_lock);
3084 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3085 spin_unlock(&vcpu->kvm->mmu_lock);
3086 return 1;
3087}
3088
3089static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3090 struct kvm_pv_mmu_op_buffer *buffer)
3091{
3092 struct kvm_mmu_op_header *header;
3093
3094 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3095 if (!header)
3096 return 0;
3097 switch (header->op) {
3098 case KVM_MMU_OP_WRITE_PTE: {
3099 struct kvm_mmu_op_write_pte *wpte;
3100
3101 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3102 if (!wpte)
3103 return 0;
3104 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3105 wpte->pte_val);
3106 }
3107 case KVM_MMU_OP_FLUSH_TLB: {
3108 struct kvm_mmu_op_flush_tlb *ftlb;
3109
3110 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3111 if (!ftlb)
3112 return 0;
3113 return kvm_pv_mmu_flush_tlb(vcpu);
3114 }
3115 case KVM_MMU_OP_RELEASE_PT: {
3116 struct kvm_mmu_op_release_pt *rpt;
3117
3118 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3119 if (!rpt)
3120 return 0;
3121 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3122 }
3123 default: return 0;
3124 }
3125}
3126
3127int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3128 gpa_t addr, unsigned long *ret)
3129{
3130 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003131 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003132
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003133 buffer->ptr = buffer->buf;
3134 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3135 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003136
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003137 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003138 if (r)
3139 goto out;
3140
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003141 while (buffer->len) {
3142 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003143 if (r < 0)
3144 goto out;
3145 if (r == 0)
3146 break;
3147 }
3148
3149 r = 1;
3150out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003151 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003152 return r;
3153}
3154
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003155int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3156{
3157 struct kvm_shadow_walk_iterator iterator;
3158 int nr_sptes = 0;
3159
3160 spin_lock(&vcpu->kvm->mmu_lock);
3161 for_each_shadow_entry(vcpu, addr, iterator) {
3162 sptes[iterator.level-1] = *iterator.sptep;
3163 nr_sptes++;
3164 if (!is_shadow_present_pte(*iterator.sptep))
3165 break;
3166 }
3167 spin_unlock(&vcpu->kvm->mmu_lock);
3168
3169 return nr_sptes;
3170}
3171EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3172
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003173#ifdef AUDIT
3174
3175static const char *audit_msg;
3176
3177static gva_t canonicalize(gva_t gva)
3178{
3179#ifdef CONFIG_X86_64
3180 gva = (long long)(gva << 16) >> 16;
3181#endif
3182 return gva;
3183}
3184
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003185
3186typedef void (*inspect_spte_fn) (struct kvm *kvm, struct kvm_mmu_page *sp,
3187 u64 *sptep);
3188
3189static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3190 inspect_spte_fn fn)
3191{
3192 int i;
3193
3194 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3195 u64 ent = sp->spt[i];
3196
3197 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003198 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003199 struct kvm_mmu_page *child;
3200 child = page_header(ent & PT64_BASE_ADDR_MASK);
3201 __mmu_spte_walk(kvm, child, fn);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003202 } else
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003203 fn(kvm, sp, &sp->spt[i]);
3204 }
3205 }
3206}
3207
3208static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3209{
3210 int i;
3211 struct kvm_mmu_page *sp;
3212
3213 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3214 return;
3215 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3216 hpa_t root = vcpu->arch.mmu.root_hpa;
3217 sp = page_header(root);
3218 __mmu_spte_walk(vcpu->kvm, sp, fn);
3219 return;
3220 }
3221 for (i = 0; i < 4; ++i) {
3222 hpa_t root = vcpu->arch.mmu.pae_root[i];
3223
3224 if (root && VALID_PAGE(root)) {
3225 root &= PT64_BASE_ADDR_MASK;
3226 sp = page_header(root);
3227 __mmu_spte_walk(vcpu->kvm, sp, fn);
3228 }
3229 }
3230 return;
3231}
3232
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003233static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3234 gva_t va, int level)
3235{
3236 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3237 int i;
3238 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3239
3240 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3241 u64 ent = pt[i];
3242
Avi Kivityc7addb92007-09-16 18:58:32 +02003243 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003244 continue;
3245
3246 va = canonicalize(va);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003247 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3248 audit_mappings_page(vcpu, ent, va, level - 1);
3249 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003250 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Jan Kiszka34382532009-04-25 12:43:21 +02003251 gfn_t gfn = gpa >> PAGE_SHIFT;
3252 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3253 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003254
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003255 if (is_error_pfn(pfn)) {
3256 kvm_release_pfn_clean(pfn);
3257 continue;
3258 }
3259
Avi Kivityc7addb92007-09-16 18:58:32 +02003260 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003261 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003262 printk(KERN_ERR "xx audit error: (%s) levels %d"
3263 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003264 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003265 va, gpa, hpa, ent,
3266 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003267 else if (ent == shadow_notrap_nonpresent_pte
3268 && !is_error_hpa(hpa))
3269 printk(KERN_ERR "audit: (%s) notrap shadow,"
3270 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003271 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003272
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003273 }
3274 }
3275}
3276
3277static void audit_mappings(struct kvm_vcpu *vcpu)
3278{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003279 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003280
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003281 if (vcpu->arch.mmu.root_level == 4)
3282 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003283 else
3284 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003285 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003286 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003287 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003288 i << 30,
3289 2);
3290}
3291
3292static int count_rmaps(struct kvm_vcpu *vcpu)
3293{
3294 int nmaps = 0;
3295 int i, j, k;
3296
3297 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3298 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
3299 struct kvm_rmap_desc *d;
3300
3301 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003302 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003303
Izik Eidus290fc382007-09-27 14:11:22 +02003304 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003305 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003306 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003307 ++nmaps;
3308 continue;
3309 }
Izik Eidus290fc382007-09-27 14:11:22 +02003310 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003311 while (d) {
3312 for (k = 0; k < RMAP_EXT; ++k)
Avi Kivityd555c332009-06-10 14:24:23 +03003313 if (d->sptes[k])
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003314 ++nmaps;
3315 else
3316 break;
3317 d = d->more;
3318 }
3319 }
3320 }
3321 return nmaps;
3322}
3323
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003324void inspect_spte_has_rmap(struct kvm *kvm, struct kvm_mmu_page *sp, u64 *sptep)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003325{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003326 unsigned long *rmapp;
3327 struct kvm_mmu_page *rev_sp;
3328 gfn_t gfn;
3329
3330 if (*sptep & PT_WRITABLE_MASK) {
3331 rev_sp = page_header(__pa(sptep));
3332 gfn = rev_sp->gfns[sptep - rev_sp->spt];
3333
3334 if (!gfn_to_memslot(kvm, gfn)) {
3335 if (!printk_ratelimit())
3336 return;
3337 printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3338 audit_msg, gfn);
3339 printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
3340 audit_msg, sptep - rev_sp->spt,
3341 rev_sp->gfn);
3342 dump_stack();
3343 return;
3344 }
3345
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003346 rmapp = gfn_to_rmap(kvm, rev_sp->gfns[sptep - rev_sp->spt],
3347 is_large_pte(*sptep));
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003348 if (!*rmapp) {
3349 if (!printk_ratelimit())
3350 return;
3351 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3352 audit_msg, *sptep);
3353 dump_stack();
3354 }
3355 }
3356
3357}
3358
3359void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3360{
3361 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3362}
3363
3364static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3365{
Avi Kivity4db35312007-11-21 15:28:32 +02003366 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003367 int i;
3368
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003369 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003370 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003371
Avi Kivity4db35312007-11-21 15:28:32 +02003372 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003373 continue;
3374
3375 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3376 u64 ent = pt[i];
3377
3378 if (!(ent & PT_PRESENT_MASK))
3379 continue;
3380 if (!(ent & PT_WRITABLE_MASK))
3381 continue;
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003382 inspect_spte_has_rmap(vcpu->kvm, sp, &pt[i]);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003383 }
3384 }
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003385 return;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003386}
3387
3388static void audit_rmap(struct kvm_vcpu *vcpu)
3389{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003390 check_writable_mappings_rmap(vcpu);
3391 count_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003392}
3393
3394static void audit_write_protection(struct kvm_vcpu *vcpu)
3395{
Avi Kivity4db35312007-11-21 15:28:32 +02003396 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003397 struct kvm_memory_slot *slot;
3398 unsigned long *rmapp;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003399 u64 *spte;
Izik Eidus290fc382007-09-27 14:11:22 +02003400 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003401
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003402 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02003403 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003404 continue;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003405 if (sp->unsync)
3406 continue;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003407
Avi Kivity4db35312007-11-21 15:28:32 +02003408 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003409 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003410 rmapp = &slot->rmap[gfn - slot->base_gfn];
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003411
3412 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3413 while (spte) {
3414 if (*spte & PT_WRITABLE_MASK)
3415 printk(KERN_ERR "%s: (%s) shadow page has "
3416 "writable mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003417 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003418 sp->role.word);
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003419 spte = rmap_next(vcpu->kvm, rmapp, spte);
3420 }
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003421 }
3422}
3423
3424static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3425{
3426 int olddbg = dbg;
3427
3428 dbg = 0;
3429 audit_msg = msg;
3430 audit_rmap(vcpu);
3431 audit_write_protection(vcpu);
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003432 if (strcmp("pre pte write", audit_msg) != 0)
3433 audit_mappings(vcpu);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003434 audit_writable_sptes_have_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003435 dbg = olddbg;
3436}
3437
3438#endif