blob: eca41ae9f453cda6dc61112ab925ce66cdb7fef3 [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
Avi Kivity135f8c22008-08-21 17:49:56 +0300159#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
160
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800161struct kvm_rmap_desc {
Avi Kivityd555c332009-06-10 14:24:23 +0300162 u64 *sptes[RMAP_EXT];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800163 struct kvm_rmap_desc *more;
164};
165
Avi Kivity2d111232008-12-25 14:39:47 +0200166struct kvm_shadow_walk_iterator {
167 u64 addr;
168 hpa_t shadow_addr;
169 int level;
170 u64 *sptep;
171 unsigned index;
172};
173
174#define for_each_shadow_entry(_vcpu, _addr, _walker) \
175 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
176 shadow_walk_okay(&(_walker)); \
177 shadow_walk_next(&(_walker)))
178
179
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300180struct kvm_unsync_walk {
181 int (*entry) (struct kvm_mmu_page *sp, struct kvm_unsync_walk *walk);
182};
183
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300184typedef int (*mmu_parent_walk_fn) (struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp);
185
Avi Kivityb5a33a72007-04-15 16:31:09 +0300186static struct kmem_cache *pte_chain_cache;
187static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300188static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300189
Avi Kivityc7addb92007-09-16 18:58:32 +0200190static u64 __read_mostly shadow_trap_nonpresent_pte;
191static u64 __read_mostly shadow_notrap_nonpresent_pte;
Sheng Yang7b523452008-04-25 21:13:50 +0800192static u64 __read_mostly shadow_base_present_pte;
193static u64 __read_mostly shadow_nx_mask;
194static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
195static u64 __read_mostly shadow_user_mask;
196static u64 __read_mostly shadow_accessed_mask;
197static u64 __read_mostly shadow_dirty_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200198
Dong, Eddie82725b22009-03-30 16:21:08 +0800199static inline u64 rsvd_bits(int s, int e)
200{
201 return ((1ULL << (e - s + 1)) - 1) << s;
202}
203
Avi Kivityc7addb92007-09-16 18:58:32 +0200204void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
205{
206 shadow_trap_nonpresent_pte = trap_pte;
207 shadow_notrap_nonpresent_pte = notrap_pte;
208}
209EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
210
Sheng Yang7b523452008-04-25 21:13:50 +0800211void kvm_mmu_set_base_ptes(u64 base_pte)
212{
213 shadow_base_present_pte = base_pte;
214}
215EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
216
217void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang4b12f0d2009-04-27 20:35:42 +0800218 u64 dirty_mask, u64 nx_mask, u64 x_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800219{
220 shadow_user_mask = user_mask;
221 shadow_accessed_mask = accessed_mask;
222 shadow_dirty_mask = dirty_mask;
223 shadow_nx_mask = nx_mask;
224 shadow_x_mask = x_mask;
225}
226EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
227
Avi Kivity6aa8b732006-12-10 02:21:36 -0800228static int is_write_protection(struct kvm_vcpu *vcpu)
229{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800230 return vcpu->arch.cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800231}
232
233static int is_cpuid_PSE36(void)
234{
235 return 1;
236}
237
Avi Kivity73b10872007-01-26 00:56:41 -0800238static int is_nx(struct kvm_vcpu *vcpu)
239{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800240 return vcpu->arch.shadow_efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800241}
242
Avi Kivityc7addb92007-09-16 18:58:32 +0200243static int is_shadow_present_pte(u64 pte)
244{
Avi Kivityc7addb92007-09-16 18:58:32 +0200245 return pte != shadow_trap_nonpresent_pte
246 && pte != shadow_notrap_nonpresent_pte;
247}
248
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300249static int is_large_pte(u64 pte)
250{
251 return pte & PT_PAGE_SIZE_MASK;
252}
253
Avi Kivity6aa8b732006-12-10 02:21:36 -0800254static int is_writeble_pte(unsigned long pte)
255{
256 return pte & PT_WRITABLE_MASK;
257}
258
Avi Kivity43a37952009-06-10 14:12:05 +0300259static int is_dirty_gpte(unsigned long pte)
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200260{
Avi Kivity439e2182009-06-10 12:56:54 +0300261 return pte & PT_DIRTY_MASK;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200262}
263
Avi Kivity43a37952009-06-10 14:12:05 +0300264static int is_rmap_spte(u64 pte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800265{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200266 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800267}
268
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300269static int is_last_spte(u64 pte, int level)
270{
271 if (level == PT_PAGE_TABLE_LEVEL)
272 return 1;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200273 if (is_large_pte(pte))
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300274 return 1;
275 return 0;
276}
277
Anthony Liguori35149e22008-04-02 14:46:56 -0500278static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200279{
Anthony Liguori35149e22008-04-02 14:46:56 -0500280 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200281}
282
Avi Kivityda928522007-11-21 13:54:47 +0200283static gfn_t pse36_gfn_delta(u32 gpte)
284{
285 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
286
287 return (gpte & PT32_DIR_PSE36_MASK) << shift;
288}
289
Avi Kivityd555c332009-06-10 14:24:23 +0300290static void __set_spte(u64 *sptep, u64 spte)
Avi Kivitye663ee62007-05-31 15:46:04 +0300291{
292#ifdef CONFIG_X86_64
293 set_64bit((unsigned long *)sptep, spte);
294#else
295 set_64bit((unsigned long long *)sptep, spte);
296#endif
297}
298
Avi Kivitye2dec932007-01-05 16:36:54 -0800299static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300300 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800301{
302 void *obj;
303
304 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800305 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800306 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300307 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800308 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800309 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800310 cache->objects[cache->nobjs++] = obj;
311 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800312 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800313}
314
315static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
316{
317 while (mc->nobjs)
318 kfree(mc->objects[--mc->nobjs]);
319}
320
Avi Kivityc1158e62007-07-20 08:18:27 +0300321static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300322 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300323{
324 struct page *page;
325
326 if (cache->nobjs >= min)
327 return 0;
328 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300329 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300330 if (!page)
331 return -ENOMEM;
332 set_page_private(page, 0);
333 cache->objects[cache->nobjs++] = page_address(page);
334 }
335 return 0;
336}
337
338static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
339{
340 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300341 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300342}
343
Avi Kivity8c438502007-04-16 11:53:17 +0300344static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
345{
346 int r;
347
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800348 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300349 pte_chain_cache, 4);
350 if (r)
351 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800352 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Marcelo Tosattic41ef342008-10-28 18:16:58 -0200353 rmap_desc_cache, 4);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300354 if (r)
355 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800356 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300357 if (r)
358 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800359 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300360 mmu_page_header_cache, 4);
361out:
Avi Kivity8c438502007-04-16 11:53:17 +0300362 return r;
363}
364
Avi Kivity714b93d2007-01-05 16:36:53 -0800365static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
366{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800367 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
368 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
369 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
370 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800371}
372
373static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
374 size_t size)
375{
376 void *p;
377
378 BUG_ON(!mc->nobjs);
379 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800380 return p;
381}
382
Avi Kivity714b93d2007-01-05 16:36:53 -0800383static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
384{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800385 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800386 sizeof(struct kvm_pte_chain));
387}
388
Avi Kivity90cb0522007-07-17 13:04:56 +0300389static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800390{
Avi Kivity90cb0522007-07-17 13:04:56 +0300391 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800392}
393
394static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
395{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800396 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800397 sizeof(struct kvm_rmap_desc));
398}
399
Avi Kivity90cb0522007-07-17 13:04:56 +0300400static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800401{
Avi Kivity90cb0522007-07-17 13:04:56 +0300402 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800403}
404
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800405/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300406 * Return the pointer to the largepage write count for a given
407 * gfn, handling slots that are not large page aligned.
408 */
Joerg Roedeld25797b2009-07-27 16:30:43 +0200409static int *slot_largepage_idx(gfn_t gfn,
410 struct kvm_memory_slot *slot,
411 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300412{
413 unsigned long idx;
414
Joerg Roedeld25797b2009-07-27 16:30:43 +0200415 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
416 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
417 return &slot->lpage_info[level - 2][idx].write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300418}
419
420static void account_shadowed(struct kvm *kvm, gfn_t gfn)
421{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200422 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300423 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200424 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300425
Izik Eidus28430992008-10-03 17:40:32 +0300426 gfn = unalias_gfn(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200427
428 slot = gfn_to_memslot_unaliased(kvm, gfn);
429 for (i = PT_DIRECTORY_LEVEL;
430 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
431 write_count = slot_largepage_idx(gfn, slot, i);
432 *write_count += 1;
433 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300434}
435
436static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
437{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200438 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300439 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200440 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300441
Izik Eidus28430992008-10-03 17:40:32 +0300442 gfn = unalias_gfn(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200443 for (i = PT_DIRECTORY_LEVEL;
444 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
445 slot = gfn_to_memslot_unaliased(kvm, gfn);
446 write_count = slot_largepage_idx(gfn, slot, i);
447 *write_count -= 1;
448 WARN_ON(*write_count < 0);
449 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300450}
451
Joerg Roedeld25797b2009-07-27 16:30:43 +0200452static int has_wrprotected_page(struct kvm *kvm,
453 gfn_t gfn,
454 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300455{
Izik Eidus28430992008-10-03 17:40:32 +0300456 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300457 int *largepage_idx;
458
Izik Eidus28430992008-10-03 17:40:32 +0300459 gfn = unalias_gfn(kvm, gfn);
460 slot = gfn_to_memslot_unaliased(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300461 if (slot) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200462 largepage_idx = slot_largepage_idx(gfn, slot, level);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300463 return *largepage_idx;
464 }
465
466 return 1;
467}
468
Joerg Roedeld25797b2009-07-27 16:30:43 +0200469static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300470{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200471 unsigned long page_size = PAGE_SIZE;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300472 struct vm_area_struct *vma;
473 unsigned long addr;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200474 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300475
476 addr = gfn_to_hva(kvm, gfn);
477 if (kvm_is_error_hva(addr))
Joerg Roedeld25797b2009-07-27 16:30:43 +0200478 return page_size;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300479
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300480 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300481 vma = find_vma(current->mm, addr);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200482 if (!vma)
483 goto out;
484
485 page_size = vma_kernel_pagesize(vma);
486
487out:
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300488 up_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300489
Joerg Roedeld25797b2009-07-27 16:30:43 +0200490 for (i = PT_PAGE_TABLE_LEVEL;
491 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
492 if (page_size >= KVM_HPAGE_SIZE(i))
493 ret = i;
494 else
495 break;
496 }
497
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300498 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300499}
500
Joerg Roedeld25797b2009-07-27 16:30:43 +0200501static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300502{
503 struct kvm_memory_slot *slot;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200504 int host_level;
505 int level = PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300506
507 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
508 if (slot && slot->dirty_bitmap)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200509 return PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300510
Joerg Roedeld25797b2009-07-27 16:30:43 +0200511 host_level = host_mapping_level(vcpu->kvm, large_gfn);
512
513 if (host_level == PT_PAGE_TABLE_LEVEL)
514 return host_level;
515
516 for (level = PT_DIRECTORY_LEVEL; level <= host_level; ++level) {
517
518 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
519 break;
520 }
521
522 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300523}
524
525/*
Izik Eidus290fc382007-09-27 14:11:22 +0200526 * Take gfn and return the reverse mapping to it.
527 * Note: gfn must be unaliased before this function get called
528 */
529
Joerg Roedel44ad9942009-07-27 16:30:42 +0200530static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
Izik Eidus290fc382007-09-27 14:11:22 +0200531{
532 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300533 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200534
535 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200536 if (likely(level == PT_PAGE_TABLE_LEVEL))
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300537 return &slot->rmap[gfn - slot->base_gfn];
538
Joerg Roedel44ad9942009-07-27 16:30:42 +0200539 idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
540 (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300541
Joerg Roedel44ad9942009-07-27 16:30:42 +0200542 return &slot->lpage_info[level - 2][idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200543}
544
545/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800546 * Reverse mapping data structures:
547 *
Izik Eidus290fc382007-09-27 14:11:22 +0200548 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
549 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800550 *
Izik Eidus290fc382007-09-27 14:11:22 +0200551 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
552 * containing more mappings.
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300553 *
554 * Returns the number of rmap entries before the spte was added or zero if
555 * the spte was not added.
556 *
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800557 */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200558static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800559{
Avi Kivity4db35312007-11-21 15:28:32 +0200560 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800561 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200562 unsigned long *rmapp;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300563 int i, count = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800564
Avi Kivity43a37952009-06-10 14:12:05 +0300565 if (!is_rmap_spte(*spte))
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300566 return count;
Izik Eidus290fc382007-09-27 14:11:22 +0200567 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200568 sp = page_header(__pa(spte));
569 sp->gfns[spte - sp->spt] = gfn;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200570 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200571 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800572 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200573 *rmapp = (unsigned long)spte;
574 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800575 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800576 desc = mmu_alloc_rmap_desc(vcpu);
Avi Kivityd555c332009-06-10 14:24:23 +0300577 desc->sptes[0] = (u64 *)*rmapp;
578 desc->sptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200579 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800580 } else {
581 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200582 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivityd555c332009-06-10 14:24:23 +0300583 while (desc->sptes[RMAP_EXT-1] && desc->more) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800584 desc = desc->more;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300585 count += RMAP_EXT;
586 }
Avi Kivityd555c332009-06-10 14:24:23 +0300587 if (desc->sptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800588 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800589 desc = desc->more;
590 }
Avi Kivityd555c332009-06-10 14:24:23 +0300591 for (i = 0; desc->sptes[i]; ++i)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800592 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300593 desc->sptes[i] = spte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800594 }
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300595 return count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800596}
597
Izik Eidus290fc382007-09-27 14:11:22 +0200598static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800599 struct kvm_rmap_desc *desc,
600 int i,
601 struct kvm_rmap_desc *prev_desc)
602{
603 int j;
604
Avi Kivityd555c332009-06-10 14:24:23 +0300605 for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800606 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300607 desc->sptes[i] = desc->sptes[j];
608 desc->sptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800609 if (j != 0)
610 return;
611 if (!prev_desc && !desc->more)
Avi Kivityd555c332009-06-10 14:24:23 +0300612 *rmapp = (unsigned long)desc->sptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800613 else
614 if (prev_desc)
615 prev_desc->more = desc->more;
616 else
Izik Eidus290fc382007-09-27 14:11:22 +0200617 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300618 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800619}
620
Izik Eidus290fc382007-09-27 14:11:22 +0200621static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800622{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800623 struct kvm_rmap_desc *desc;
624 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200625 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500626 pfn_t pfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200627 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800628 int i;
629
Avi Kivity43a37952009-06-10 14:12:05 +0300630 if (!is_rmap_spte(*spte))
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800631 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200632 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500633 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800634 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500635 kvm_set_pfn_accessed(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200636 if (is_writeble_pte(*spte))
Anthony Liguori35149e22008-04-02 14:46:56 -0500637 kvm_release_pfn_dirty(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200638 else
Anthony Liguori35149e22008-04-02 14:46:56 -0500639 kvm_release_pfn_clean(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
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200751static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
752{
753 u64 *spte;
754 int need_tlb_flush = 0;
755
756 while ((spte = rmap_next(kvm, rmapp, NULL))) {
757 BUG_ON(!(*spte & PT_PRESENT_MASK));
758 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
759 rmap_remove(kvm, spte);
Avi Kivityd555c332009-06-10 14:24:23 +0300760 __set_spte(spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200761 need_tlb_flush = 1;
762 }
763 return need_tlb_flush;
764}
765
766static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
767 int (*handler)(struct kvm *kvm, unsigned long *rmapp))
768{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200769 int i, j;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200770 int retval = 0;
771
772 /*
773 * If mmap_sem isn't taken, we can look the memslots with only
774 * the mmu_lock by skipping over the slots with userspace_addr == 0.
775 */
776 for (i = 0; i < kvm->nmemslots; i++) {
777 struct kvm_memory_slot *memslot = &kvm->memslots[i];
778 unsigned long start = memslot->userspace_addr;
779 unsigned long end;
780
781 /* mmu_lock protects userspace_addr */
782 if (!start)
783 continue;
784
785 end = start + (memslot->npages << PAGE_SHIFT);
786 if (hva >= start && hva < end) {
787 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200788
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200789 retval |= handler(kvm, &memslot->rmap[gfn_offset]);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200790
791 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
792 int idx = gfn_offset;
793 idx /= KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL + j);
794 retval |= handler(kvm,
795 &memslot->lpage_info[j][idx].rmap_pde);
796 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200797 }
798 }
799
800 return retval;
801}
802
803int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
804{
805 return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
806}
807
808static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
809{
810 u64 *spte;
811 int young = 0;
812
Sheng Yang534e38b2008-09-08 15:12:30 +0800813 /* always return old for EPT */
814 if (!shadow_accessed_mask)
815 return 0;
816
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200817 spte = rmap_next(kvm, rmapp, NULL);
818 while (spte) {
819 int _young;
820 u64 _spte = *spte;
821 BUG_ON(!(_spte & PT_PRESENT_MASK));
822 _young = _spte & PT_ACCESSED_MASK;
823 if (_young) {
824 young = 1;
825 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
826 }
827 spte = rmap_next(kvm, rmapp, spte);
828 }
829 return young;
830}
831
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300832#define RMAP_RECYCLE_THRESHOLD 1000
833
Joerg Roedel852e3c12009-07-27 16:30:44 +0200834static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300835{
836 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200837 struct kvm_mmu_page *sp;
838
839 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300840
841 gfn = unalias_gfn(vcpu->kvm, gfn);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200842 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300843
844 kvm_unmap_rmapp(vcpu->kvm, rmapp);
845 kvm_flush_remote_tlbs(vcpu->kvm);
846}
847
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200848int kvm_age_hva(struct kvm *kvm, unsigned long hva)
849{
850 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
851}
852
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800853#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300854static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800855{
Avi Kivity139bdb22007-01-05 16:36:50 -0800856 u64 *pos;
857 u64 *end;
858
Avi Kivity47ad8e62007-05-06 15:50:58 +0300859 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300860 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800861 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800862 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800863 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800864 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800865 return 1;
866}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800867#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800868
Avi Kivity4db35312007-11-21 15:28:32 +0200869static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800870{
Avi Kivity4db35312007-11-21 15:28:32 +0200871 ASSERT(is_empty_shadow_page(sp->spt));
872 list_del(&sp->link);
873 __free_page(virt_to_page(sp->spt));
874 __free_page(virt_to_page(sp->gfns));
875 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800876 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800877}
878
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800879static unsigned kvm_page_table_hashfn(gfn_t gfn)
880{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200881 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800882}
883
Avi Kivity25c0de22007-01-05 16:36:42 -0800884static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
885 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800886{
Avi Kivity4db35312007-11-21 15:28:32 +0200887 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800888
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800889 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
890 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
891 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200892 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800893 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -0200894 INIT_LIST_HEAD(&sp->oos_link);
Sheng Yang291f26b2008-10-16 17:30:57 +0800895 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200896 sp->multimapped = 0;
897 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800898 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200899 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800900}
901
Avi Kivity714b93d2007-01-05 16:36:53 -0800902static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200903 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800904{
905 struct kvm_pte_chain *pte_chain;
906 struct hlist_node *node;
907 int i;
908
909 if (!parent_pte)
910 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200911 if (!sp->multimapped) {
912 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800913
914 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200915 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800916 return;
917 }
Avi Kivity4db35312007-11-21 15:28:32 +0200918 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800919 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200920 INIT_HLIST_HEAD(&sp->parent_ptes);
921 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800922 pte_chain->parent_ptes[0] = old;
923 }
Avi Kivity4db35312007-11-21 15:28:32 +0200924 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800925 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
926 continue;
927 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
928 if (!pte_chain->parent_ptes[i]) {
929 pte_chain->parent_ptes[i] = parent_pte;
930 return;
931 }
932 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800933 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800934 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200935 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800936 pte_chain->parent_ptes[0] = parent_pte;
937}
938
Avi Kivity4db35312007-11-21 15:28:32 +0200939static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800940 u64 *parent_pte)
941{
942 struct kvm_pte_chain *pte_chain;
943 struct hlist_node *node;
944 int i;
945
Avi Kivity4db35312007-11-21 15:28:32 +0200946 if (!sp->multimapped) {
947 BUG_ON(sp->parent_pte != parent_pte);
948 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800949 return;
950 }
Avi Kivity4db35312007-11-21 15:28:32 +0200951 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800952 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
953 if (!pte_chain->parent_ptes[i])
954 break;
955 if (pte_chain->parent_ptes[i] != parent_pte)
956 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800957 while (i + 1 < NR_PTE_CHAIN_ENTRIES
958 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800959 pte_chain->parent_ptes[i]
960 = pte_chain->parent_ptes[i + 1];
961 ++i;
962 }
963 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800964 if (i == 0) {
965 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300966 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200967 if (hlist_empty(&sp->parent_ptes)) {
968 sp->multimapped = 0;
969 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800970 }
971 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800972 return;
973 }
974 BUG();
975}
976
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300977
978static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
979 mmu_parent_walk_fn fn)
980{
981 struct kvm_pte_chain *pte_chain;
982 struct hlist_node *node;
983 struct kvm_mmu_page *parent_sp;
984 int i;
985
986 if (!sp->multimapped && sp->parent_pte) {
987 parent_sp = page_header(__pa(sp->parent_pte));
988 fn(vcpu, parent_sp);
989 mmu_parent_walk(vcpu, parent_sp, fn);
990 return;
991 }
992 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
993 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
994 if (!pte_chain->parent_ptes[i])
995 break;
996 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
997 fn(vcpu, parent_sp);
998 mmu_parent_walk(vcpu, parent_sp, fn);
999 }
1000}
1001
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001002static void kvm_mmu_update_unsync_bitmap(u64 *spte)
1003{
1004 unsigned int index;
1005 struct kvm_mmu_page *sp = page_header(__pa(spte));
1006
1007 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001008 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
1009 sp->unsync_children++;
1010 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001011}
1012
1013static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
1014{
1015 struct kvm_pte_chain *pte_chain;
1016 struct hlist_node *node;
1017 int i;
1018
1019 if (!sp->parent_pte)
1020 return;
1021
1022 if (!sp->multimapped) {
1023 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
1024 return;
1025 }
1026
1027 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1028 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1029 if (!pte_chain->parent_ptes[i])
1030 break;
1031 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
1032 }
1033}
1034
1035static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1036{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001037 kvm_mmu_update_parents_unsync(sp);
1038 return 1;
1039}
1040
1041static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
1042 struct kvm_mmu_page *sp)
1043{
1044 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
1045 kvm_mmu_update_parents_unsync(sp);
1046}
1047
Avi Kivityd761a502008-05-29 14:55:03 +03001048static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1049 struct kvm_mmu_page *sp)
1050{
1051 int i;
1052
1053 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1054 sp->spt[i] = shadow_trap_nonpresent_pte;
1055}
1056
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001057static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
1058 struct kvm_mmu_page *sp)
1059{
1060 return 1;
1061}
1062
Marcelo Tosattia7052892008-09-23 13:18:35 -03001063static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1064{
1065}
1066
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001067#define KVM_PAGE_ARRAY_NR 16
1068
1069struct kvm_mmu_pages {
1070 struct mmu_page_and_offset {
1071 struct kvm_mmu_page *sp;
1072 unsigned int idx;
1073 } page[KVM_PAGE_ARRAY_NR];
1074 unsigned int nr;
1075};
1076
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001077#define for_each_unsync_children(bitmap, idx) \
1078 for (idx = find_first_bit(bitmap, 512); \
1079 idx < 512; \
1080 idx = find_next_bit(bitmap, 512, idx+1))
1081
Hannes Edercded19f2009-02-21 02:19:13 +01001082static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1083 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001084{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001085 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001086
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001087 if (sp->unsync)
1088 for (i=0; i < pvec->nr; i++)
1089 if (pvec->page[i].sp == sp)
1090 return 0;
1091
1092 pvec->page[pvec->nr].sp = sp;
1093 pvec->page[pvec->nr].idx = idx;
1094 pvec->nr++;
1095 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1096}
1097
1098static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1099 struct kvm_mmu_pages *pvec)
1100{
1101 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001102
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001103 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001104 u64 ent = sp->spt[i];
1105
Marcelo Tosatti87917232008-12-22 18:49:30 -02001106 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001107 struct kvm_mmu_page *child;
1108 child = page_header(ent & PT64_BASE_ADDR_MASK);
1109
1110 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001111 if (mmu_pages_add(pvec, child, i))
1112 return -ENOSPC;
1113
1114 ret = __mmu_unsync_walk(child, pvec);
1115 if (!ret)
1116 __clear_bit(i, sp->unsync_child_bitmap);
1117 else if (ret > 0)
1118 nr_unsync_leaf += ret;
1119 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001120 return ret;
1121 }
1122
1123 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001124 nr_unsync_leaf++;
1125 if (mmu_pages_add(pvec, child, i))
1126 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001127 }
1128 }
1129 }
1130
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001131 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001132 sp->unsync_children = 0;
1133
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001134 return nr_unsync_leaf;
1135}
1136
1137static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1138 struct kvm_mmu_pages *pvec)
1139{
1140 if (!sp->unsync_children)
1141 return 0;
1142
1143 mmu_pages_add(pvec, sp, 0);
1144 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001145}
1146
Avi Kivity4db35312007-11-21 15:28:32 +02001147static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001148{
1149 unsigned index;
1150 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001151 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001152 struct hlist_node *node;
1153
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001154 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001155 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001156 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001157 hlist_for_each_entry(sp, node, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001158 if (sp->gfn == gfn && !sp->role.direct
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001159 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001160 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001161 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001162 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001163 }
1164 return NULL;
1165}
1166
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001167static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1168{
1169 WARN_ON(!sp->unsync);
1170 sp->unsync = 0;
1171 --kvm->stat.mmu_unsync;
1172}
1173
1174static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1175
1176static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1177{
1178 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1179 kvm_mmu_zap_page(vcpu->kvm, sp);
1180 return 1;
1181 }
1182
Avi Kivityf691fe12009-07-06 15:58:14 +03001183 trace_kvm_mmu_sync_page(sp);
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001184 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1185 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001186 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001187 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1188 kvm_mmu_zap_page(vcpu->kvm, sp);
1189 return 1;
1190 }
1191
1192 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001193 return 0;
1194}
1195
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001196struct mmu_page_path {
1197 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1198 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001199};
1200
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001201#define for_each_sp(pvec, sp, parents, i) \
1202 for (i = mmu_pages_next(&pvec, &parents, -1), \
1203 sp = pvec.page[i].sp; \
1204 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1205 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001206
Hannes Edercded19f2009-02-21 02:19:13 +01001207static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1208 struct mmu_page_path *parents,
1209 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001210{
1211 int n;
1212
1213 for (n = i+1; n < pvec->nr; n++) {
1214 struct kvm_mmu_page *sp = pvec->page[n].sp;
1215
1216 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1217 parents->idx[0] = pvec->page[n].idx;
1218 return n;
1219 }
1220
1221 parents->parent[sp->role.level-2] = sp;
1222 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1223 }
1224
1225 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001226}
1227
Hannes Edercded19f2009-02-21 02:19:13 +01001228static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001229{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001230 struct kvm_mmu_page *sp;
1231 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001232
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001233 do {
1234 unsigned int idx = parents->idx[level];
1235
1236 sp = parents->parent[level];
1237 if (!sp)
1238 return;
1239
1240 --sp->unsync_children;
1241 WARN_ON((int)sp->unsync_children < 0);
1242 __clear_bit(idx, sp->unsync_child_bitmap);
1243 level++;
1244 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1245}
1246
1247static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1248 struct mmu_page_path *parents,
1249 struct kvm_mmu_pages *pvec)
1250{
1251 parents->parent[parent->role.level-1] = NULL;
1252 pvec->nr = 0;
1253}
1254
1255static void mmu_sync_children(struct kvm_vcpu *vcpu,
1256 struct kvm_mmu_page *parent)
1257{
1258 int i;
1259 struct kvm_mmu_page *sp;
1260 struct mmu_page_path parents;
1261 struct kvm_mmu_pages pages;
1262
1263 kvm_mmu_pages_init(parent, &parents, &pages);
1264 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001265 int protected = 0;
1266
1267 for_each_sp(pages, sp, parents, i)
1268 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1269
1270 if (protected)
1271 kvm_flush_remote_tlbs(vcpu->kvm);
1272
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001273 for_each_sp(pages, sp, parents, i) {
1274 kvm_sync_page(vcpu, sp);
1275 mmu_pages_clear_parents(&parents);
1276 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001277 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001278 kvm_mmu_pages_init(parent, &parents, &pages);
1279 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001280}
1281
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001282static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1283 gfn_t gfn,
1284 gva_t gaddr,
1285 unsigned level,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001286 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001287 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001288 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001289{
1290 union kvm_mmu_page_role role;
1291 unsigned index;
1292 unsigned quadrant;
1293 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001294 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001295 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001296
Avi Kivitya770f6f2008-12-21 19:20:09 +02001297 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001298 role.level = level;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001299 role.direct = direct;
Avi Kivity41074d02007-12-09 17:00:02 +02001300 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001301 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001302 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1303 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1304 role.quadrant = quadrant;
1305 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001306 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001307 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001308 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1309 if (sp->gfn == gfn) {
1310 if (sp->unsync)
1311 if (kvm_sync_page(vcpu, sp))
1312 continue;
1313
1314 if (sp->role.word != role.word)
1315 continue;
1316
Avi Kivity4db35312007-11-21 15:28:32 +02001317 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001318 if (sp->unsync_children) {
1319 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1320 kvm_mmu_mark_parents_unsync(vcpu, sp);
1321 }
Avi Kivityf691fe12009-07-06 15:58:14 +03001322 trace_kvm_mmu_get_page(sp, false);
Avi Kivity4db35312007-11-21 15:28:32 +02001323 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001324 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001325 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001326 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1327 if (!sp)
1328 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001329 sp->gfn = gfn;
1330 sp->role = role;
1331 hlist_add_head(&sp->hash_link, bucket);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001332 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001333 if (rmap_write_protect(vcpu->kvm, gfn))
1334 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001335 account_shadowed(vcpu->kvm, gfn);
1336 }
Avi Kivity131d8272008-05-29 14:56:28 +03001337 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1338 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1339 else
1340 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001341 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001342 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001343}
1344
Avi Kivity2d111232008-12-25 14:39:47 +02001345static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1346 struct kvm_vcpu *vcpu, u64 addr)
1347{
1348 iterator->addr = addr;
1349 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1350 iterator->level = vcpu->arch.mmu.shadow_root_level;
1351 if (iterator->level == PT32E_ROOT_LEVEL) {
1352 iterator->shadow_addr
1353 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1354 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1355 --iterator->level;
1356 if (!iterator->shadow_addr)
1357 iterator->level = 0;
1358 }
1359}
1360
1361static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1362{
1363 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1364 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001365
1366 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1367 if (is_large_pte(*iterator->sptep))
1368 return false;
1369
Avi Kivity2d111232008-12-25 14:39:47 +02001370 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1371 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1372 return true;
1373}
1374
1375static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1376{
1377 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1378 --iterator->level;
1379}
1380
Avi Kivity90cb0522007-07-17 13:04:56 +03001381static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001382 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001383{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001384 unsigned i;
1385 u64 *pt;
1386 u64 ent;
1387
Avi Kivity4db35312007-11-21 15:28:32 +02001388 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001389
Avi Kivity697fe2e2007-01-05 16:36:46 -08001390 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1391 ent = pt[i];
1392
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001393 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001394 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001395 ent &= PT64_BASE_ADDR_MASK;
1396 mmu_page_remove_parent_pte(page_header(ent),
1397 &pt[i]);
1398 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001399 if (is_large_pte(ent))
1400 --kvm->stat.lpages;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001401 rmap_remove(kvm, &pt[i]);
1402 }
1403 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001404 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001405 }
Avi Kivitya4360362007-01-05 16:36:45 -08001406}
1407
Avi Kivity4db35312007-11-21 15:28:32 +02001408static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001409{
Avi Kivity4db35312007-11-21 15:28:32 +02001410 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001411}
1412
Avi Kivity12b7d282007-09-23 14:10:49 +02001413static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1414{
1415 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001416 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001417
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001418 kvm_for_each_vcpu(i, vcpu, kvm)
1419 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001420}
1421
Avi Kivity31aa2b42008-07-11 17:59:46 +03001422static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001423{
1424 u64 *parent_pte;
1425
Avi Kivity4db35312007-11-21 15:28:32 +02001426 while (sp->multimapped || sp->parent_pte) {
1427 if (!sp->multimapped)
1428 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001429 else {
1430 struct kvm_pte_chain *chain;
1431
Avi Kivity4db35312007-11-21 15:28:32 +02001432 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001433 struct kvm_pte_chain, link);
1434 parent_pte = chain->parent_ptes[0];
1435 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001436 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001437 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001438 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001439 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001440}
1441
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001442static int mmu_zap_unsync_children(struct kvm *kvm,
1443 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001444{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001445 int i, zapped = 0;
1446 struct mmu_page_path parents;
1447 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001448
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001449 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001450 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001451
1452 kvm_mmu_pages_init(parent, &parents, &pages);
1453 while (mmu_unsync_walk(parent, &pages)) {
1454 struct kvm_mmu_page *sp;
1455
1456 for_each_sp(pages, sp, parents, i) {
1457 kvm_mmu_zap_page(kvm, sp);
1458 mmu_pages_clear_parents(&parents);
1459 }
1460 zapped += pages.nr;
1461 kvm_mmu_pages_init(parent, &parents, &pages);
1462 }
1463
1464 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001465}
1466
Marcelo Tosatti07385412008-09-23 13:18:37 -03001467static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001468{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001469 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001470
1471 trace_kvm_mmu_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001472 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001473 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001474 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001475 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001476 kvm_flush_remote_tlbs(kvm);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001477 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001478 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001479 if (sp->unsync)
1480 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001481 if (!sp->root_count) {
1482 hlist_del(&sp->hash_link);
1483 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001484 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001485 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001486 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001487 kvm_reload_remote_mmus(kvm);
1488 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001489 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001490 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001491}
1492
Izik Eidus82ce2c92007-10-02 18:52:55 +02001493/*
1494 * Changing the number of mmu pages allocated to the vm
1495 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1496 */
1497void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1498{
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001499 int used_pages;
1500
1501 used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1502 used_pages = max(0, used_pages);
1503
Izik Eidus82ce2c92007-10-02 18:52:55 +02001504 /*
1505 * If we set the number of mmu pages to be smaller be than the
1506 * number of actived pages , we must to free some mmu pages before we
1507 * change the value
1508 */
1509
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001510 if (used_pages > kvm_nr_mmu_pages) {
1511 while (used_pages > kvm_nr_mmu_pages) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001512 struct kvm_mmu_page *page;
1513
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001514 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001515 struct kvm_mmu_page, link);
1516 kvm_mmu_zap_page(kvm, page);
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001517 used_pages--;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001518 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001519 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001520 }
1521 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001522 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1523 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001524
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001525 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001526}
1527
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001528static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001529{
1530 unsigned index;
1531 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001532 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001533 struct hlist_node *node, *n;
1534 int r;
1535
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001536 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001537 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001538 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001539 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001540 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001541 if (sp->gfn == gfn && !sp->role.direct) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001542 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001543 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001544 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001545 if (kvm_mmu_zap_page(kvm, sp))
1546 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001547 }
1548 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001549}
1550
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001551static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001552{
Avi Kivity4677a3b2009-01-06 13:00:27 +02001553 unsigned index;
1554 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001555 struct kvm_mmu_page *sp;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001556 struct hlist_node *node, *nn;
Avi Kivity97a0a012007-05-31 15:08:29 +03001557
Avi Kivity4677a3b2009-01-06 13:00:27 +02001558 index = kvm_page_table_hashfn(gfn);
1559 bucket = &kvm->arch.mmu_page_hash[index];
1560 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001561 if (sp->gfn == gfn && !sp->role.direct
Avi Kivity4677a3b2009-01-06 13:00:27 +02001562 && !sp->role.invalid) {
1563 pgprintk("%s: zap %lx %x\n",
1564 __func__, gfn, sp->role.word);
1565 kvm_mmu_zap_page(kvm, sp);
1566 }
Avi Kivity97a0a012007-05-31 15:08:29 +03001567 }
1568}
1569
Avi Kivity38c335f2007-11-21 14:20:22 +02001570static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001571{
Avi Kivity38c335f2007-11-21 14:20:22 +02001572 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001573 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001574
Sheng Yang291f26b2008-10-16 17:30:57 +08001575 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001576}
1577
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001578static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1579{
1580 int i;
1581 u64 *pt = sp->spt;
1582
1583 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1584 return;
1585
1586 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1587 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001588 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001589 }
1590}
1591
Avi Kivity039576c2007-03-20 12:46:50 +02001592struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1593{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001594 struct page *page;
1595
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001596 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001597
1598 if (gpa == UNMAPPED_GVA)
1599 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001600
Izik Eidus72dc67a2008-02-10 18:04:15 +02001601 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001602
1603 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001604}
1605
Sheng Yang74be52e2008-10-09 16:01:56 +08001606/*
1607 * The function is based on mtrr_type_lookup() in
1608 * arch/x86/kernel/cpu/mtrr/generic.c
1609 */
1610static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1611 u64 start, u64 end)
1612{
1613 int i;
1614 u64 base, mask;
1615 u8 prev_match, curr_match;
1616 int num_var_ranges = KVM_NR_VAR_MTRR;
1617
1618 if (!mtrr_state->enabled)
1619 return 0xFF;
1620
1621 /* Make end inclusive end, instead of exclusive */
1622 end--;
1623
1624 /* Look in fixed ranges. Just return the type as per start */
1625 if (mtrr_state->have_fixed && (start < 0x100000)) {
1626 int idx;
1627
1628 if (start < 0x80000) {
1629 idx = 0;
1630 idx += (start >> 16);
1631 return mtrr_state->fixed_ranges[idx];
1632 } else if (start < 0xC0000) {
1633 idx = 1 * 8;
1634 idx += ((start - 0x80000) >> 14);
1635 return mtrr_state->fixed_ranges[idx];
1636 } else if (start < 0x1000000) {
1637 idx = 3 * 8;
1638 idx += ((start - 0xC0000) >> 12);
1639 return mtrr_state->fixed_ranges[idx];
1640 }
1641 }
1642
1643 /*
1644 * Look in variable ranges
1645 * Look of multiple ranges matching this address and pick type
1646 * as per MTRR precedence
1647 */
1648 if (!(mtrr_state->enabled & 2))
1649 return mtrr_state->def_type;
1650
1651 prev_match = 0xFF;
1652 for (i = 0; i < num_var_ranges; ++i) {
1653 unsigned short start_state, end_state;
1654
1655 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1656 continue;
1657
1658 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1659 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1660 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1661 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1662
1663 start_state = ((start & mask) == (base & mask));
1664 end_state = ((end & mask) == (base & mask));
1665 if (start_state != end_state)
1666 return 0xFE;
1667
1668 if ((start & mask) != (base & mask))
1669 continue;
1670
1671 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1672 if (prev_match == 0xFF) {
1673 prev_match = curr_match;
1674 continue;
1675 }
1676
1677 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1678 curr_match == MTRR_TYPE_UNCACHABLE)
1679 return MTRR_TYPE_UNCACHABLE;
1680
1681 if ((prev_match == MTRR_TYPE_WRBACK &&
1682 curr_match == MTRR_TYPE_WRTHROUGH) ||
1683 (prev_match == MTRR_TYPE_WRTHROUGH &&
1684 curr_match == MTRR_TYPE_WRBACK)) {
1685 prev_match = MTRR_TYPE_WRTHROUGH;
1686 curr_match = MTRR_TYPE_WRTHROUGH;
1687 }
1688
1689 if (prev_match != curr_match)
1690 return MTRR_TYPE_UNCACHABLE;
1691 }
1692
1693 if (prev_match != 0xFF)
1694 return prev_match;
1695
1696 return mtrr_state->def_type;
1697}
1698
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001699u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001700{
1701 u8 mtrr;
1702
1703 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1704 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1705 if (mtrr == 0xfe || mtrr == 0xff)
1706 mtrr = MTRR_TYPE_WRBACK;
1707 return mtrr;
1708}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001709EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001710
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001711static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1712{
1713 unsigned index;
1714 struct hlist_head *bucket;
1715 struct kvm_mmu_page *s;
1716 struct hlist_node *node, *n;
1717
Avi Kivityf691fe12009-07-06 15:58:14 +03001718 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001719 index = kvm_page_table_hashfn(sp->gfn);
1720 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1721 /* don't unsync if pagetable is shadowed with multiple roles */
1722 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001723 if (s->gfn != sp->gfn || s->role.direct)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001724 continue;
1725 if (s->role.word != sp->role.word)
1726 return 1;
1727 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001728 ++vcpu->kvm->stat.mmu_unsync;
1729 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001730
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001731 kvm_mmu_mark_parents_unsync(vcpu, sp);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001732
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001733 mmu_convert_notrap(sp);
1734 return 0;
1735}
1736
1737static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1738 bool can_unsync)
1739{
1740 struct kvm_mmu_page *shadow;
1741
1742 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1743 if (shadow) {
1744 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1745 return 1;
1746 if (shadow->unsync)
1747 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001748 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001749 return kvm_unsync_page(vcpu, shadow);
1750 return 1;
1751 }
1752 return 0;
1753}
1754
Avi Kivityd555c332009-06-10 14:24:23 +03001755static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001756 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001757 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001758 gfn_t gfn, pfn_t pfn, bool speculative,
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001759 bool can_unsync)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001760{
1761 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001762 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001763
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001764 /*
1765 * We don't set the accessed bit, since we sometimes want to see
1766 * whether the guest actually used the pte (in order to detect
1767 * demand paging).
1768 */
Sheng Yang7b523452008-04-25 21:13:50 +08001769 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001770 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001771 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001772 if (!dirty)
1773 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001774 if (pte_access & ACC_EXEC_MASK)
1775 spte |= shadow_x_mask;
1776 else
1777 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001778 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001779 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001780 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001781 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001782 if (tdp_enabled)
1783 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1784 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001785
Anthony Liguori35149e22008-04-02 14:46:56 -05001786 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001787
1788 if ((pte_access & ACC_WRITE_MASK)
1789 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001790
Joerg Roedel852e3c12009-07-27 16:30:44 +02001791 if (level > PT_PAGE_TABLE_LEVEL &&
1792 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001793 ret = 1;
1794 spte = shadow_trap_nonpresent_pte;
1795 goto set_pte;
1796 }
1797
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001798 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001799
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001800 /*
1801 * Optimization: for pte sync, if spte was writable the hash
1802 * lookup is unnecessary (and expensive). Write protection
1803 * is responsibility of mmu_get_page / kvm_sync_page.
1804 * Same reasoning can be applied to dirty page accounting.
1805 */
Avi Kivityd555c332009-06-10 14:24:23 +03001806 if (!can_unsync && is_writeble_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001807 goto set_pte;
1808
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001809 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001810 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001811 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001812 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001813 pte_access &= ~ACC_WRITE_MASK;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001814 if (is_writeble_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001815 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001816 }
1817 }
1818
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001819 if (pte_access & ACC_WRITE_MASK)
1820 mark_page_dirty(vcpu->kvm, gfn);
1821
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001822set_pte:
Avi Kivityd555c332009-06-10 14:24:23 +03001823 __set_spte(sptep, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001824 return ret;
1825}
1826
Avi Kivityd555c332009-06-10 14:24:23 +03001827static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001828 unsigned pt_access, unsigned pte_access,
1829 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001830 int *ptwrite, int level, gfn_t gfn,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001831 pfn_t pfn, bool speculative)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001832{
1833 int was_rmapped = 0;
Avi Kivityd555c332009-06-10 14:24:23 +03001834 int was_writeble = is_writeble_pte(*sptep);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001835 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001836
1837 pgprintk("%s: spte %llx access %x write_fault %d"
1838 " user_fault %d gfn %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001839 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001840 write_fault, user_fault, gfn);
1841
Avi Kivityd555c332009-06-10 14:24:23 +03001842 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001843 /*
1844 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1845 * the parent of the now unreachable PTE.
1846 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02001847 if (level > PT_PAGE_TABLE_LEVEL &&
1848 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001849 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03001850 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001851
1852 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03001853 mmu_page_remove_parent_pte(child, sptep);
1854 } else if (pfn != spte_to_pfn(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001855 pgprintk("hfn old %lx new %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001856 spte_to_pfn(*sptep), pfn);
1857 rmap_remove(vcpu->kvm, sptep);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001858 } else
1859 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001860 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02001861
Avi Kivityd555c332009-06-10 14:24:23 +03001862 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001863 dirty, level, gfn, pfn, speculative, true)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001864 if (write_fault)
1865 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001866 kvm_x86_ops->tlb_flush(vcpu);
1867 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001868
Avi Kivityd555c332009-06-10 14:24:23 +03001869 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001870 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001871 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02001872 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
1873 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03001874 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001875 ++vcpu->kvm->stat.lpages;
1876
Avi Kivityd555c332009-06-10 14:24:23 +03001877 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001878 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02001879 rmap_count = rmap_add(vcpu, sptep, gfn);
Avi Kivityd555c332009-06-10 14:24:23 +03001880 if (!is_rmap_spte(*sptep))
Anthony Liguori35149e22008-04-02 14:46:56 -05001881 kvm_release_pfn_clean(pfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001882 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02001883 rmap_recycle(vcpu, sptep, gfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001884 } else {
1885 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001886 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001887 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001888 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001889 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001890 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03001891 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001892 vcpu->arch.last_pte_gfn = gfn;
1893 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001894}
1895
Avi Kivity6aa8b732006-12-10 02:21:36 -08001896static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1897{
1898}
1899
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001900static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001901 int level, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001902{
Avi Kivity9f652d22008-12-25 14:54:25 +02001903 struct kvm_shadow_walk_iterator iterator;
1904 struct kvm_mmu_page *sp;
1905 int pt_write = 0;
1906 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001907
Avi Kivity9f652d22008-12-25 14:54:25 +02001908 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02001909 if (iterator.level == level) {
Avi Kivity9f652d22008-12-25 14:54:25 +02001910 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1911 0, write, 1, &pt_write,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001912 level, gfn, pfn, false);
Avi Kivity9f652d22008-12-25 14:54:25 +02001913 ++vcpu->stat.pf_fixed;
1914 break;
1915 }
1916
1917 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1918 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1919 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1920 iterator.level - 1,
1921 1, ACC_ALL, iterator.sptep);
1922 if (!sp) {
1923 pgprintk("nonpaging_map: ENOMEM\n");
1924 kvm_release_pfn_clean(pfn);
1925 return -ENOMEM;
1926 }
1927
Avi Kivityd555c332009-06-10 14:24:23 +03001928 __set_spte(iterator.sptep,
1929 __pa(sp->spt)
1930 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1931 | shadow_user_mask | shadow_x_mask);
Avi Kivity9f652d22008-12-25 14:54:25 +02001932 }
1933 }
1934 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001935}
1936
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001937static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1938{
1939 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001940 int level;
Anthony Liguori35149e22008-04-02 14:46:56 -05001941 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001942 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001943
Joerg Roedel852e3c12009-07-27 16:30:44 +02001944 level = mapping_level(vcpu, gfn);
1945
1946 /*
1947 * This path builds a PAE pagetable - so we can map 2mb pages at
1948 * maximum. Therefore check if the level is larger than that.
1949 */
1950 if (level > PT_DIRECTORY_LEVEL)
1951 level = PT_DIRECTORY_LEVEL;
1952
1953 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001954
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001955 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001956 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001957 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001958
Avi Kivityd196e342008-01-24 11:44:11 +02001959 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001960 if (is_error_pfn(pfn)) {
1961 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001962 return 1;
1963 }
1964
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001965 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001966 if (mmu_notifier_retry(vcpu, mmu_seq))
1967 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02001968 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel852e3c12009-07-27 16:30:44 +02001969 r = __direct_map(vcpu, v, write, level, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001970 spin_unlock(&vcpu->kvm->mmu_lock);
1971
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001972
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001973 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001974
1975out_unlock:
1976 spin_unlock(&vcpu->kvm->mmu_lock);
1977 kvm_release_pfn_clean(pfn);
1978 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001979}
1980
1981
Avi Kivity17ac10a2007-01-05 16:36:40 -08001982static void mmu_free_roots(struct kvm_vcpu *vcpu)
1983{
1984 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001985 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001986
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001987 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001988 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001989 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001990 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1991 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001992
Avi Kivity4db35312007-11-21 15:28:32 +02001993 sp = page_header(root);
1994 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001995 if (!sp->root_count && sp->role.invalid)
1996 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001997 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001998 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001999 return;
2000 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002001 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002002 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002003
Avi Kivity417726a2007-04-12 17:35:58 +03002004 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002005 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002006 sp = page_header(root);
2007 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002008 if (!sp->root_count && sp->role.invalid)
2009 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03002010 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002011 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002012 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002013 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002014 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002015}
2016
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002017static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2018{
2019 int ret = 0;
2020
2021 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
2022 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
2023 ret = 1;
2024 }
2025
2026 return ret;
2027}
2028
2029static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002030{
2031 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002032 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02002033 struct kvm_mmu_page *sp;
Avi Kivityf6e2c022009-01-11 13:02:10 +02002034 int direct = 0;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002035 u64 pdptr;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002036
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002037 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002038
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002039 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2040 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002041
2042 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01002043 if (tdp_enabled)
Avi Kivityf6e2c022009-01-11 13:02:10 +02002044 direct = 1;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002045 if (mmu_check_root(vcpu, root_gfn))
2046 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002047 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c022009-01-11 13:02:10 +02002048 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01002049 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002050 root = __pa(sp->spt);
2051 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002052 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002053 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002054 }
Avi Kivityf6e2c022009-01-11 13:02:10 +02002055 direct = !is_paging(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002056 if (tdp_enabled)
Avi Kivityf6e2c022009-01-11 13:02:10 +02002057 direct = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002058 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002059 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002060
2061 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002062 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002063 pdptr = kvm_pdptr_read(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002064 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002065 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002066 continue;
2067 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002068 root_gfn = pdptr >> PAGE_SHIFT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002069 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002070 root_gfn = 0;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002071 if (mmu_check_root(vcpu, root_gfn))
2072 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002073 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c022009-01-11 13:02:10 +02002074 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002075 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002076 root = __pa(sp->spt);
2077 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002078 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002079 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002080 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002081 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002082}
2083
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002084static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2085{
2086 int i;
2087 struct kvm_mmu_page *sp;
2088
2089 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2090 return;
2091 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2092 hpa_t root = vcpu->arch.mmu.root_hpa;
2093 sp = page_header(root);
2094 mmu_sync_children(vcpu, sp);
2095 return;
2096 }
2097 for (i = 0; i < 4; ++i) {
2098 hpa_t root = vcpu->arch.mmu.pae_root[i];
2099
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002100 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002101 root &= PT64_BASE_ADDR_MASK;
2102 sp = page_header(root);
2103 mmu_sync_children(vcpu, sp);
2104 }
2105 }
2106}
2107
2108void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2109{
2110 spin_lock(&vcpu->kvm->mmu_lock);
2111 mmu_sync_roots(vcpu);
2112 spin_unlock(&vcpu->kvm->mmu_lock);
2113}
2114
Avi Kivity6aa8b732006-12-10 02:21:36 -08002115static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2116{
2117 return vaddr;
2118}
2119
2120static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002121 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002122{
Avi Kivitye8332402007-12-09 18:43:00 +02002123 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002124 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002125
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002126 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002127 r = mmu_topup_memory_caches(vcpu);
2128 if (r)
2129 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002130
Avi Kivity6aa8b732006-12-10 02:21:36 -08002131 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002132 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002133
Avi Kivitye8332402007-12-09 18:43:00 +02002134 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002135
Avi Kivitye8332402007-12-09 18:43:00 +02002136 return nonpaging_map(vcpu, gva & PAGE_MASK,
2137 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002138}
2139
Joerg Roedelfb72d162008-02-07 13:47:44 +01002140static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2141 u32 error_code)
2142{
Anthony Liguori35149e22008-04-02 14:46:56 -05002143 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002144 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002145 int level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002146 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002147 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002148
2149 ASSERT(vcpu);
2150 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2151
2152 r = mmu_topup_memory_caches(vcpu);
2153 if (r)
2154 return r;
2155
Joerg Roedel852e3c12009-07-27 16:30:44 +02002156 level = mapping_level(vcpu, gfn);
2157
2158 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2159
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002160 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002161 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002162 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002163 if (is_error_pfn(pfn)) {
2164 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002165 return 1;
2166 }
2167 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002168 if (mmu_notifier_retry(vcpu, mmu_seq))
2169 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002170 kvm_mmu_free_some_pages(vcpu);
2171 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002172 level, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002173 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002174
2175 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002176
2177out_unlock:
2178 spin_unlock(&vcpu->kvm->mmu_lock);
2179 kvm_release_pfn_clean(pfn);
2180 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002181}
2182
Avi Kivity6aa8b732006-12-10 02:21:36 -08002183static void nonpaging_free(struct kvm_vcpu *vcpu)
2184{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002185 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002186}
2187
2188static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2189{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002190 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002191
2192 context->new_cr3 = nonpaging_new_cr3;
2193 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002194 context->gva_to_gpa = nonpaging_gva_to_gpa;
2195 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002196 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002197 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002198 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002199 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002200 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002201 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002202 return 0;
2203}
2204
Avi Kivityd835dfe2007-11-21 02:57:59 +02002205void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002206{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002207 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002208 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002209}
2210
2211static void paging_new_cr3(struct kvm_vcpu *vcpu)
2212{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002213 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002214 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002215}
2216
Avi Kivity6aa8b732006-12-10 02:21:36 -08002217static void inject_page_fault(struct kvm_vcpu *vcpu,
2218 u64 addr,
2219 u32 err_code)
2220{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002221 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002222}
2223
Avi Kivity6aa8b732006-12-10 02:21:36 -08002224static void paging_free(struct kvm_vcpu *vcpu)
2225{
2226 nonpaging_free(vcpu);
2227}
2228
Dong, Eddie82725b22009-03-30 16:21:08 +08002229static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2230{
2231 int bit7;
2232
2233 bit7 = (gpte >> 7) & 1;
2234 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2235}
2236
Avi Kivity6aa8b732006-12-10 02:21:36 -08002237#define PTTYPE 64
2238#include "paging_tmpl.h"
2239#undef PTTYPE
2240
2241#define PTTYPE 32
2242#include "paging_tmpl.h"
2243#undef PTTYPE
2244
Dong, Eddie82725b22009-03-30 16:21:08 +08002245static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2246{
2247 struct kvm_mmu *context = &vcpu->arch.mmu;
2248 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2249 u64 exb_bit_rsvd = 0;
2250
2251 if (!is_nx(vcpu))
2252 exb_bit_rsvd = rsvd_bits(63, 63);
2253 switch (level) {
2254 case PT32_ROOT_LEVEL:
2255 /* no rsvd bits for 2 level 4K page table entries */
2256 context->rsvd_bits_mask[0][1] = 0;
2257 context->rsvd_bits_mask[0][0] = 0;
2258 if (is_cpuid_PSE36())
2259 /* 36bits PSE 4MB page */
2260 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2261 else
2262 /* 32 bits PSE 4MB page */
2263 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Avi Kivity29a4b932009-05-19 13:29:27 +03002264 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002265 break;
2266 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002267 context->rsvd_bits_mask[0][2] =
2268 rsvd_bits(maxphyaddr, 63) |
2269 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002270 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002271 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002272 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2273 rsvd_bits(maxphyaddr, 62); /* PTE */
2274 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2275 rsvd_bits(maxphyaddr, 62) |
2276 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002277 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002278 break;
2279 case PT64_ROOT_LEVEL:
2280 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2281 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2282 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2283 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2284 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002285 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002286 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2287 rsvd_bits(maxphyaddr, 51);
2288 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002289 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2290 rsvd_bits(maxphyaddr, 51) |
2291 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002292 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002293 rsvd_bits(maxphyaddr, 51) |
2294 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002295 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002296 break;
2297 }
2298}
2299
Avi Kivity17ac10a2007-01-05 16:36:40 -08002300static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002301{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002302 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002303
2304 ASSERT(is_pae(vcpu));
2305 context->new_cr3 = paging_new_cr3;
2306 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002307 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002308 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002309 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002310 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002311 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002312 context->root_level = level;
2313 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002314 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002315 return 0;
2316}
2317
Avi Kivity17ac10a2007-01-05 16:36:40 -08002318static int paging64_init_context(struct kvm_vcpu *vcpu)
2319{
Dong, Eddie82725b22009-03-30 16:21:08 +08002320 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002321 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2322}
2323
Avi Kivity6aa8b732006-12-10 02:21:36 -08002324static int paging32_init_context(struct kvm_vcpu *vcpu)
2325{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002326 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002327
Dong, Eddie82725b22009-03-30 16:21:08 +08002328 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002329 context->new_cr3 = paging_new_cr3;
2330 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002331 context->gva_to_gpa = paging32_gva_to_gpa;
2332 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002333 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002334 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002335 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002336 context->root_level = PT32_ROOT_LEVEL;
2337 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002338 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002339 return 0;
2340}
2341
2342static int paging32E_init_context(struct kvm_vcpu *vcpu)
2343{
Dong, Eddie82725b22009-03-30 16:21:08 +08002344 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002345 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002346}
2347
Joerg Roedelfb72d162008-02-07 13:47:44 +01002348static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2349{
2350 struct kvm_mmu *context = &vcpu->arch.mmu;
2351
2352 context->new_cr3 = nonpaging_new_cr3;
2353 context->page_fault = tdp_page_fault;
2354 context->free = nonpaging_free;
2355 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002356 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002357 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002358 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002359 context->root_hpa = INVALID_PAGE;
2360
2361 if (!is_paging(vcpu)) {
2362 context->gva_to_gpa = nonpaging_gva_to_gpa;
2363 context->root_level = 0;
2364 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002365 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002366 context->gva_to_gpa = paging64_gva_to_gpa;
2367 context->root_level = PT64_ROOT_LEVEL;
2368 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002369 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002370 context->gva_to_gpa = paging64_gva_to_gpa;
2371 context->root_level = PT32E_ROOT_LEVEL;
2372 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002373 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002374 context->gva_to_gpa = paging32_gva_to_gpa;
2375 context->root_level = PT32_ROOT_LEVEL;
2376 }
2377
2378 return 0;
2379}
2380
2381static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002382{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002383 int r;
2384
Avi Kivity6aa8b732006-12-10 02:21:36 -08002385 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002386 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002387
2388 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002389 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002390 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002391 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002392 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002393 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002394 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002395 r = paging32_init_context(vcpu);
2396
2397 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2398
2399 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002400}
2401
Joerg Roedelfb72d162008-02-07 13:47:44 +01002402static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2403{
Anthony Liguori35149e22008-04-02 14:46:56 -05002404 vcpu->arch.update_pte.pfn = bad_pfn;
2405
Joerg Roedelfb72d162008-02-07 13:47:44 +01002406 if (tdp_enabled)
2407 return init_kvm_tdp_mmu(vcpu);
2408 else
2409 return init_kvm_softmmu(vcpu);
2410}
2411
Avi Kivity6aa8b732006-12-10 02:21:36 -08002412static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2413{
2414 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002415 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2416 vcpu->arch.mmu.free(vcpu);
2417 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002418 }
2419}
2420
2421int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2422{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002423 destroy_kvm_mmu(vcpu);
2424 return init_kvm_mmu(vcpu);
2425}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002426EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002427
2428int kvm_mmu_load(struct kvm_vcpu *vcpu)
2429{
Avi Kivity714b93d2007-01-05 16:36:53 -08002430 int r;
2431
Avi Kivitye2dec932007-01-05 16:36:54 -08002432 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002433 if (r)
2434 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002435 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002436 kvm_mmu_free_some_pages(vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002437 r = mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002438 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002439 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002440 if (r)
2441 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08002442 /* set_cr3() should ensure TLB has been flushed */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002443 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08002444out:
2445 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002446}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002447EXPORT_SYMBOL_GPL(kvm_mmu_load);
2448
2449void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2450{
2451 mmu_free_roots(vcpu);
2452}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002453
Avi Kivity09072da2007-05-01 14:16:52 +03002454static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002455 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002456 u64 *spte)
2457{
2458 u64 pte;
2459 struct kvm_mmu_page *child;
2460
2461 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002462 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03002463 if (is_last_spte(pte, sp->role.level))
Izik Eidus290fc382007-09-27 14:11:22 +02002464 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002465 else {
2466 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002467 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002468 }
2469 }
Avi Kivityd555c332009-06-10 14:24:23 +03002470 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002471 if (is_large_pte(pte))
2472 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002473}
2474
Avi Kivity00284252007-05-01 16:53:31 +03002475static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002476 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002477 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002478 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002479{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002480 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02002481 ++vcpu->kvm->stat.mmu_pde_zapped;
2482 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03002483 }
Avi Kivity00284252007-05-01 16:53:31 +03002484
Avi Kivity4cee5762007-11-18 16:37:07 +02002485 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002486 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002487 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002488 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002489 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002490}
2491
Avi Kivity79539ce2007-11-21 02:06:21 +02002492static bool need_remote_flush(u64 old, u64 new)
2493{
2494 if (!is_shadow_present_pte(old))
2495 return false;
2496 if (!is_shadow_present_pte(new))
2497 return true;
2498 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2499 return true;
2500 old ^= PT64_NX_MASK;
2501 new ^= PT64_NX_MASK;
2502 return (old & ~new & PT64_PERM_MASK) != 0;
2503}
2504
2505static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2506{
2507 if (need_remote_flush(old, new))
2508 kvm_flush_remote_tlbs(vcpu->kvm);
2509 else
2510 kvm_mmu_flush_tlb(vcpu);
2511}
2512
Avi Kivity12b7d282007-09-23 14:10:49 +02002513static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2514{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002515 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002516
Sheng Yang7b523452008-04-25 21:13:50 +08002517 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002518}
2519
Avi Kivityd7824ff2007-12-30 12:29:05 +02002520static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2521 const u8 *new, int bytes)
2522{
2523 gfn_t gfn;
2524 int r;
2525 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002526 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002527
2528 if (bytes != 4 && bytes != 8)
2529 return;
2530
2531 /*
2532 * Assume that the pte write on a page table of the same type
2533 * as the current vcpu paging mode. This is nearly always true
2534 * (might be false while changing modes). Note it is verified later
2535 * by update_pte().
2536 */
2537 if (is_pae(vcpu)) {
2538 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2539 if ((bytes == 4) && (gpa % 4 == 0)) {
2540 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2541 if (r)
2542 return;
2543 memcpy((void *)&gpte + (gpa % 8), new, 4);
2544 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2545 memcpy((void *)&gpte, new, 8);
2546 }
2547 } else {
2548 if ((bytes == 4) && (gpa % 4 == 0))
2549 memcpy((void *)&gpte, new, 4);
2550 }
Avi Kivity43a37952009-06-10 14:12:05 +03002551 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02002552 return;
2553 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002554
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002555 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002556 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002557 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002558
Anthony Liguori35149e22008-04-02 14:46:56 -05002559 if (is_error_pfn(pfn)) {
2560 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002561 return;
2562 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002563 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002564 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002565}
2566
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002567static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2568{
2569 u64 *spte = vcpu->arch.last_pte_updated;
2570
2571 if (spte
2572 && vcpu->arch.last_pte_gfn == gfn
2573 && shadow_accessed_mask
2574 && !(*spte & shadow_accessed_mask)
2575 && is_shadow_present_pte(*spte))
2576 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2577}
2578
Avi Kivity09072da2007-05-01 14:16:52 +03002579void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002580 const u8 *new, int bytes,
2581 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002582{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002583 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002584 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002585 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002586 struct hlist_head *bucket;
2587 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002588 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002589 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002590 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002591 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002592 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002593 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002594 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002595 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002596 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002597 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002598 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002599
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002600 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002601 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002602 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002603 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002604 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002605 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002606 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002607 if (guest_initiated) {
2608 if (gfn == vcpu->arch.last_pt_write_gfn
2609 && !last_updated_pte_accessed(vcpu)) {
2610 ++vcpu->arch.last_pt_write_count;
2611 if (vcpu->arch.last_pt_write_count >= 3)
2612 flooded = 1;
2613 } else {
2614 vcpu->arch.last_pt_write_gfn = gfn;
2615 vcpu->arch.last_pt_write_count = 1;
2616 vcpu->arch.last_pte_updated = NULL;
2617 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002618 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002619 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002620 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002621 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02002622 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002623 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002624 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002625 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002626 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002627 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002628 /*
2629 * Misaligned accesses are too much trouble to fix
2630 * up; also, they usually indicate a page is not used
2631 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002632 *
2633 * If we're seeing too many writes to a page,
2634 * it may no longer be a page table, or we may be
2635 * forking, in which case it is better to unmap the
2636 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002637 */
2638 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002639 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002640 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2641 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002642 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002643 continue;
2644 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002645 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002646 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002647 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002648 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002649 page_offset <<= 1; /* 32->64 */
2650 /*
2651 * A 32-bit pde maps 4MB while the shadow pdes map
2652 * only 2MB. So we need to double the offset again
2653 * and zap two pdes instead of one.
2654 */
2655 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002656 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002657 page_offset <<= 1;
2658 npte = 2;
2659 }
Avi Kivityfce06572007-05-01 16:44:05 +03002660 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002661 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002662 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002663 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002664 }
Avi Kivity4db35312007-11-21 15:28:32 +02002665 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002666 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2667 gentry = 0;
2668 r = kvm_read_guest_atomic(vcpu->kvm,
2669 gpa & ~(u64)(pte_size - 1),
2670 &gentry, pte_size);
2671 new = (const void *)&gentry;
2672 if (r < 0)
2673 new = NULL;
2674 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002675 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002676 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002677 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002678 if (new)
2679 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002680 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002681 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002682 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002683 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002684 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002685 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002686 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2687 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2688 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002689 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002690}
2691
Avi Kivitya4360362007-01-05 16:36:45 -08002692int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2693{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002694 gpa_t gpa;
2695 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002696
Avi Kivity60f24782009-08-27 13:37:06 +03002697 if (tdp_enabled)
2698 return 0;
2699
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002700 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002701
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002702 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002703 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002704 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002705 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002706}
Avi Kivity577bdc42008-07-19 08:57:05 +03002707EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002708
Avi Kivity22d95b12007-09-14 20:26:06 +03002709void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002710{
Izik Eidus3b80fff2009-07-28 15:26:58 -03002711 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES &&
2712 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02002713 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002714
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002715 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002716 struct kvm_mmu_page, link);
2717 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002718 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002719 }
2720}
Avi Kivityebeace82007-01-05 16:36:47 -08002721
Avi Kivity30677142007-10-28 18:48:59 +02002722int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2723{
2724 int r;
2725 enum emulation_result er;
2726
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002727 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002728 if (r < 0)
2729 goto out;
2730
2731 if (!r) {
2732 r = 1;
2733 goto out;
2734 }
2735
Avi Kivityb733bfb2007-10-28 18:52:05 +02002736 r = mmu_topup_memory_caches(vcpu);
2737 if (r)
2738 goto out;
2739
Avi Kivity30677142007-10-28 18:48:59 +02002740 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002741
2742 switch (er) {
2743 case EMULATE_DONE:
2744 return 1;
2745 case EMULATE_DO_MMIO:
2746 ++vcpu->stat.mmio_exits;
2747 return 0;
2748 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002749 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2750 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2751 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02002752 default:
2753 BUG();
2754 }
2755out:
Avi Kivity30677142007-10-28 18:48:59 +02002756 return r;
2757}
2758EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2759
Marcelo Tosattia7052892008-09-23 13:18:35 -03002760void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2761{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002762 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002763 kvm_mmu_flush_tlb(vcpu);
2764 ++vcpu->stat.invlpg;
2765}
2766EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2767
Joerg Roedel18552672008-02-07 13:47:41 +01002768void kvm_enable_tdp(void)
2769{
2770 tdp_enabled = true;
2771}
2772EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2773
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002774void kvm_disable_tdp(void)
2775{
2776 tdp_enabled = false;
2777}
2778EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2779
Avi Kivity6aa8b732006-12-10 02:21:36 -08002780static void free_mmu_pages(struct kvm_vcpu *vcpu)
2781{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002782 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002783}
2784
2785static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2786{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002787 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002788 int i;
2789
2790 ASSERT(vcpu);
2791
Avi Kivity17ac10a2007-01-05 16:36:40 -08002792 /*
2793 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2794 * Therefore we need to allocate shadow page tables in the first
2795 * 4GB of memory, which happens to fit the DMA32 zone.
2796 */
2797 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2798 if (!page)
2799 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002800 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002801 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002802 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002803
Avi Kivity6aa8b732006-12-10 02:21:36 -08002804 return 0;
2805
2806error_1:
2807 free_mmu_pages(vcpu);
2808 return -ENOMEM;
2809}
2810
Ingo Molnar8018c272006-12-29 16:50:01 -08002811int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002812{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002813 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002814 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002815
Ingo Molnar8018c272006-12-29 16:50:01 -08002816 return alloc_mmu_pages(vcpu);
2817}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002818
Ingo Molnar8018c272006-12-29 16:50:01 -08002819int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2820{
2821 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002822 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002823
Ingo Molnar8018c272006-12-29 16:50:01 -08002824 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002825}
2826
2827void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2828{
2829 ASSERT(vcpu);
2830
2831 destroy_kvm_mmu(vcpu);
2832 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002833 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002834}
2835
Avi Kivity90cb0522007-07-17 13:04:56 +03002836void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002837{
Avi Kivity4db35312007-11-21 15:28:32 +02002838 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002839
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002840 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002841 int i;
2842 u64 *pt;
2843
Sheng Yang291f26b2008-10-16 17:30:57 +08002844 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002845 continue;
2846
Avi Kivity4db35312007-11-21 15:28:32 +02002847 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002848 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2849 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002850 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002851 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002852 }
Avi Kivity171d5952008-08-27 16:40:51 +03002853 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002854}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002855
Avi Kivity90cb0522007-07-17 13:04:56 +03002856void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002857{
Avi Kivity4db35312007-11-21 15:28:32 +02002858 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002859
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002860 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002861 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002862 if (kvm_mmu_zap_page(kvm, sp))
2863 node = container_of(kvm->arch.active_mmu_pages.next,
2864 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002865 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002866
Avi Kivity90cb0522007-07-17 13:04:56 +03002867 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002868}
2869
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002870static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002871{
2872 struct kvm_mmu_page *page;
2873
2874 page = container_of(kvm->arch.active_mmu_pages.prev,
2875 struct kvm_mmu_page, link);
2876 kvm_mmu_zap_page(kvm, page);
2877}
2878
2879static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2880{
2881 struct kvm *kvm;
2882 struct kvm *kvm_freed = NULL;
2883 int cache_count = 0;
2884
2885 spin_lock(&kvm_lock);
2886
2887 list_for_each_entry(kvm, &vm_list, vm_list) {
2888 int npages;
2889
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002890 if (!down_read_trylock(&kvm->slots_lock))
2891 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002892 spin_lock(&kvm->mmu_lock);
2893 npages = kvm->arch.n_alloc_mmu_pages -
2894 kvm->arch.n_free_mmu_pages;
2895 cache_count += npages;
2896 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2897 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2898 cache_count--;
2899 kvm_freed = kvm;
2900 }
2901 nr_to_scan--;
2902
2903 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002904 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002905 }
2906 if (kvm_freed)
2907 list_move_tail(&kvm_freed->vm_list, &vm_list);
2908
2909 spin_unlock(&kvm_lock);
2910
2911 return cache_count;
2912}
2913
2914static struct shrinker mmu_shrinker = {
2915 .shrink = mmu_shrink,
2916 .seeks = DEFAULT_SEEKS * 10,
2917};
2918
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002919static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002920{
2921 if (pte_chain_cache)
2922 kmem_cache_destroy(pte_chain_cache);
2923 if (rmap_desc_cache)
2924 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002925 if (mmu_page_header_cache)
2926 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002927}
2928
Izik Eidus3ee16c82008-03-30 15:17:21 +03002929void kvm_mmu_module_exit(void)
2930{
2931 mmu_destroy_caches();
2932 unregister_shrinker(&mmu_shrinker);
2933}
2934
Avi Kivityb5a33a72007-04-15 16:31:09 +03002935int kvm_mmu_module_init(void)
2936{
2937 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2938 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002939 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002940 if (!pte_chain_cache)
2941 goto nomem;
2942 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2943 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002944 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002945 if (!rmap_desc_cache)
2946 goto nomem;
2947
Avi Kivityd3d25b02007-05-30 12:34:53 +03002948 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2949 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002950 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002951 if (!mmu_page_header_cache)
2952 goto nomem;
2953
Izik Eidus3ee16c82008-03-30 15:17:21 +03002954 register_shrinker(&mmu_shrinker);
2955
Avi Kivityb5a33a72007-04-15 16:31:09 +03002956 return 0;
2957
2958nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002959 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002960 return -ENOMEM;
2961}
2962
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002963/*
2964 * Caculate mmu pages needed for kvm.
2965 */
2966unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2967{
2968 int i;
2969 unsigned int nr_mmu_pages;
2970 unsigned int nr_pages = 0;
2971
2972 for (i = 0; i < kvm->nmemslots; i++)
2973 nr_pages += kvm->memslots[i].npages;
2974
2975 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2976 nr_mmu_pages = max(nr_mmu_pages,
2977 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2978
2979 return nr_mmu_pages;
2980}
2981
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002982static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2983 unsigned len)
2984{
2985 if (len > buffer->len)
2986 return NULL;
2987 return buffer->ptr;
2988}
2989
2990static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2991 unsigned len)
2992{
2993 void *ret;
2994
2995 ret = pv_mmu_peek_buffer(buffer, len);
2996 if (!ret)
2997 return ret;
2998 buffer->ptr += len;
2999 buffer->len -= len;
3000 buffer->processed += len;
3001 return ret;
3002}
3003
3004static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3005 gpa_t addr, gpa_t value)
3006{
3007 int bytes = 8;
3008 int r;
3009
3010 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3011 bytes = 4;
3012
3013 r = mmu_topup_memory_caches(vcpu);
3014 if (r)
3015 return r;
3016
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003017 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003018 return -EFAULT;
3019
3020 return 1;
3021}
3022
3023static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3024{
Avi Kivitya8cd0242009-05-24 22:15:25 +03003025 kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003026 return 1;
3027}
3028
3029static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3030{
3031 spin_lock(&vcpu->kvm->mmu_lock);
3032 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3033 spin_unlock(&vcpu->kvm->mmu_lock);
3034 return 1;
3035}
3036
3037static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3038 struct kvm_pv_mmu_op_buffer *buffer)
3039{
3040 struct kvm_mmu_op_header *header;
3041
3042 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3043 if (!header)
3044 return 0;
3045 switch (header->op) {
3046 case KVM_MMU_OP_WRITE_PTE: {
3047 struct kvm_mmu_op_write_pte *wpte;
3048
3049 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3050 if (!wpte)
3051 return 0;
3052 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3053 wpte->pte_val);
3054 }
3055 case KVM_MMU_OP_FLUSH_TLB: {
3056 struct kvm_mmu_op_flush_tlb *ftlb;
3057
3058 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3059 if (!ftlb)
3060 return 0;
3061 return kvm_pv_mmu_flush_tlb(vcpu);
3062 }
3063 case KVM_MMU_OP_RELEASE_PT: {
3064 struct kvm_mmu_op_release_pt *rpt;
3065
3066 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3067 if (!rpt)
3068 return 0;
3069 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3070 }
3071 default: return 0;
3072 }
3073}
3074
3075int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3076 gpa_t addr, unsigned long *ret)
3077{
3078 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003079 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003080
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003081 buffer->ptr = buffer->buf;
3082 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3083 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003084
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003085 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003086 if (r)
3087 goto out;
3088
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003089 while (buffer->len) {
3090 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003091 if (r < 0)
3092 goto out;
3093 if (r == 0)
3094 break;
3095 }
3096
3097 r = 1;
3098out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003099 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003100 return r;
3101}
3102
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003103int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3104{
3105 struct kvm_shadow_walk_iterator iterator;
3106 int nr_sptes = 0;
3107
3108 spin_lock(&vcpu->kvm->mmu_lock);
3109 for_each_shadow_entry(vcpu, addr, iterator) {
3110 sptes[iterator.level-1] = *iterator.sptep;
3111 nr_sptes++;
3112 if (!is_shadow_present_pte(*iterator.sptep))
3113 break;
3114 }
3115 spin_unlock(&vcpu->kvm->mmu_lock);
3116
3117 return nr_sptes;
3118}
3119EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3120
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003121#ifdef AUDIT
3122
3123static const char *audit_msg;
3124
3125static gva_t canonicalize(gva_t gva)
3126{
3127#ifdef CONFIG_X86_64
3128 gva = (long long)(gva << 16) >> 16;
3129#endif
3130 return gva;
3131}
3132
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003133
3134typedef void (*inspect_spte_fn) (struct kvm *kvm, struct kvm_mmu_page *sp,
3135 u64 *sptep);
3136
3137static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3138 inspect_spte_fn fn)
3139{
3140 int i;
3141
3142 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3143 u64 ent = sp->spt[i];
3144
3145 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003146 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003147 struct kvm_mmu_page *child;
3148 child = page_header(ent & PT64_BASE_ADDR_MASK);
3149 __mmu_spte_walk(kvm, child, fn);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003150 } else
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003151 fn(kvm, sp, &sp->spt[i]);
3152 }
3153 }
3154}
3155
3156static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3157{
3158 int i;
3159 struct kvm_mmu_page *sp;
3160
3161 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3162 return;
3163 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3164 hpa_t root = vcpu->arch.mmu.root_hpa;
3165 sp = page_header(root);
3166 __mmu_spte_walk(vcpu->kvm, sp, fn);
3167 return;
3168 }
3169 for (i = 0; i < 4; ++i) {
3170 hpa_t root = vcpu->arch.mmu.pae_root[i];
3171
3172 if (root && VALID_PAGE(root)) {
3173 root &= PT64_BASE_ADDR_MASK;
3174 sp = page_header(root);
3175 __mmu_spte_walk(vcpu->kvm, sp, fn);
3176 }
3177 }
3178 return;
3179}
3180
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003181static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3182 gva_t va, int level)
3183{
3184 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3185 int i;
3186 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3187
3188 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3189 u64 ent = pt[i];
3190
Avi Kivityc7addb92007-09-16 18:58:32 +02003191 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003192 continue;
3193
3194 va = canonicalize(va);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003195 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3196 audit_mappings_page(vcpu, ent, va, level - 1);
3197 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003198 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Jan Kiszka34382532009-04-25 12:43:21 +02003199 gfn_t gfn = gpa >> PAGE_SHIFT;
3200 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3201 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003202
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003203 if (is_error_pfn(pfn)) {
3204 kvm_release_pfn_clean(pfn);
3205 continue;
3206 }
3207
Avi Kivityc7addb92007-09-16 18:58:32 +02003208 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003209 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003210 printk(KERN_ERR "xx audit error: (%s) levels %d"
3211 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003212 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003213 va, gpa, hpa, ent,
3214 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003215 else if (ent == shadow_notrap_nonpresent_pte
3216 && !is_error_hpa(hpa))
3217 printk(KERN_ERR "audit: (%s) notrap shadow,"
3218 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003219 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003220
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003221 }
3222 }
3223}
3224
3225static void audit_mappings(struct kvm_vcpu *vcpu)
3226{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003227 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003228
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003229 if (vcpu->arch.mmu.root_level == 4)
3230 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003231 else
3232 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003233 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003234 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003235 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003236 i << 30,
3237 2);
3238}
3239
3240static int count_rmaps(struct kvm_vcpu *vcpu)
3241{
3242 int nmaps = 0;
3243 int i, j, k;
3244
3245 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3246 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
3247 struct kvm_rmap_desc *d;
3248
3249 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003250 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003251
Izik Eidus290fc382007-09-27 14:11:22 +02003252 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003253 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003254 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003255 ++nmaps;
3256 continue;
3257 }
Izik Eidus290fc382007-09-27 14:11:22 +02003258 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003259 while (d) {
3260 for (k = 0; k < RMAP_EXT; ++k)
Avi Kivityd555c332009-06-10 14:24:23 +03003261 if (d->sptes[k])
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003262 ++nmaps;
3263 else
3264 break;
3265 d = d->more;
3266 }
3267 }
3268 }
3269 return nmaps;
3270}
3271
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003272void inspect_spte_has_rmap(struct kvm *kvm, struct kvm_mmu_page *sp, u64 *sptep)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003273{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003274 unsigned long *rmapp;
3275 struct kvm_mmu_page *rev_sp;
3276 gfn_t gfn;
3277
3278 if (*sptep & PT_WRITABLE_MASK) {
3279 rev_sp = page_header(__pa(sptep));
3280 gfn = rev_sp->gfns[sptep - rev_sp->spt];
3281
3282 if (!gfn_to_memslot(kvm, gfn)) {
3283 if (!printk_ratelimit())
3284 return;
3285 printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3286 audit_msg, gfn);
3287 printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
3288 audit_msg, sptep - rev_sp->spt,
3289 rev_sp->gfn);
3290 dump_stack();
3291 return;
3292 }
3293
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003294 rmapp = gfn_to_rmap(kvm, rev_sp->gfns[sptep - rev_sp->spt],
3295 is_large_pte(*sptep));
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003296 if (!*rmapp) {
3297 if (!printk_ratelimit())
3298 return;
3299 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3300 audit_msg, *sptep);
3301 dump_stack();
3302 }
3303 }
3304
3305}
3306
3307void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3308{
3309 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3310}
3311
3312static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3313{
Avi Kivity4db35312007-11-21 15:28:32 +02003314 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003315 int i;
3316
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003317 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003318 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003319
Avi Kivity4db35312007-11-21 15:28:32 +02003320 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003321 continue;
3322
3323 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3324 u64 ent = pt[i];
3325
3326 if (!(ent & PT_PRESENT_MASK))
3327 continue;
3328 if (!(ent & PT_WRITABLE_MASK))
3329 continue;
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003330 inspect_spte_has_rmap(vcpu->kvm, sp, &pt[i]);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003331 }
3332 }
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003333 return;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003334}
3335
3336static void audit_rmap(struct kvm_vcpu *vcpu)
3337{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003338 check_writable_mappings_rmap(vcpu);
3339 count_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003340}
3341
3342static void audit_write_protection(struct kvm_vcpu *vcpu)
3343{
Avi Kivity4db35312007-11-21 15:28:32 +02003344 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003345 struct kvm_memory_slot *slot;
3346 unsigned long *rmapp;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003347 u64 *spte;
Izik Eidus290fc382007-09-27 14:11:22 +02003348 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003349
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003350 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02003351 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003352 continue;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003353 if (sp->unsync)
3354 continue;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003355
Avi Kivity4db35312007-11-21 15:28:32 +02003356 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003357 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003358 rmapp = &slot->rmap[gfn - slot->base_gfn];
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003359
3360 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3361 while (spte) {
3362 if (*spte & PT_WRITABLE_MASK)
3363 printk(KERN_ERR "%s: (%s) shadow page has "
3364 "writable mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003365 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003366 sp->role.word);
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003367 spte = rmap_next(vcpu->kvm, rmapp, spte);
3368 }
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003369 }
3370}
3371
3372static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3373{
3374 int olddbg = dbg;
3375
3376 dbg = 0;
3377 audit_msg = msg;
3378 audit_rmap(vcpu);
3379 audit_write_protection(vcpu);
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003380 if (strcmp("pre pte write", audit_msg) != 0)
3381 audit_mappings(vcpu);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003382 audit_writable_sptes_have_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003383 dbg = olddbg;
3384}
3385
3386#endif