blob: 6c67b230e9586bab2953650b8c2b97ed2725f07d [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))
Izik Eidusacb66dd2009-09-23 21:47:16 +0300637 kvm_set_pfn_dirty(pfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200638 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200639 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800640 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
641 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200642 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800643 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200644 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800645 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
646 spte, *spte);
647 BUG();
648 }
Izik Eidus290fc382007-09-27 14:11:22 +0200649 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800650 } else {
651 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200652 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800653 prev_desc = NULL;
654 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300655 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
656 if (desc->sptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200657 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800658 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800659 prev_desc);
660 return;
661 }
662 prev_desc = desc;
663 desc = desc->more;
664 }
665 BUG();
666 }
667}
668
Izik Eidus98348e92007-10-16 14:42:30 +0200669static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800670{
Avi Kivity374cbac2007-01-05 16:36:43 -0800671 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200672 struct kvm_rmap_desc *prev_desc;
673 u64 *prev_spte;
674 int i;
675
676 if (!*rmapp)
677 return NULL;
678 else if (!(*rmapp & 1)) {
679 if (!spte)
680 return (u64 *)*rmapp;
681 return NULL;
682 }
683 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
684 prev_desc = NULL;
685 prev_spte = NULL;
686 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300687 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
Izik Eidus98348e92007-10-16 14:42:30 +0200688 if (prev_spte == spte)
Avi Kivityd555c332009-06-10 14:24:23 +0300689 return desc->sptes[i];
690 prev_spte = desc->sptes[i];
Izik Eidus98348e92007-10-16 14:42:30 +0200691 }
692 desc = desc->more;
693 }
694 return NULL;
695}
696
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200697static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200698{
Izik Eidus290fc382007-09-27 14:11:22 +0200699 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800700 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200701 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800702
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500703 gfn = unalias_gfn(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200704 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
Avi Kivity374cbac2007-01-05 16:36:43 -0800705
Izik Eidus98348e92007-10-16 14:42:30 +0200706 spte = rmap_next(kvm, rmapp, NULL);
707 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800708 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800709 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800710 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800711 if (is_writeble_pte(*spte)) {
Avi Kivityd555c332009-06-10 14:24:23 +0300712 __set_spte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800713 write_protected = 1;
714 }
Izik Eidus9647c142007-10-16 14:43:46 +0200715 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800716 }
Izik Eidus855149a2008-03-20 18:17:24 +0200717 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500718 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200719
720 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500721 pfn = spte_to_pfn(*spte);
722 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200723 }
724
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300725 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200726 for (i = PT_DIRECTORY_LEVEL;
727 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
728 rmapp = gfn_to_rmap(kvm, gfn, i);
729 spte = rmap_next(kvm, rmapp, NULL);
730 while (spte) {
731 BUG_ON(!spte);
732 BUG_ON(!(*spte & PT_PRESENT_MASK));
733 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
734 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
735 if (is_writeble_pte(*spte)) {
736 rmap_remove(kvm, spte);
737 --kvm->stat.lpages;
738 __set_spte(spte, shadow_trap_nonpresent_pte);
739 spte = NULL;
740 write_protected = 1;
741 }
742 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300743 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300744 }
745
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200746 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800747}
748
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200749static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
750{
751 u64 *spte;
752 int need_tlb_flush = 0;
753
754 while ((spte = rmap_next(kvm, rmapp, NULL))) {
755 BUG_ON(!(*spte & PT_PRESENT_MASK));
756 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
757 rmap_remove(kvm, spte);
Avi Kivityd555c332009-06-10 14:24:23 +0300758 __set_spte(spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200759 need_tlb_flush = 1;
760 }
761 return need_tlb_flush;
762}
763
764static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
765 int (*handler)(struct kvm *kvm, unsigned long *rmapp))
766{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200767 int i, j;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200768 int retval = 0;
769
770 /*
771 * If mmap_sem isn't taken, we can look the memslots with only
772 * the mmu_lock by skipping over the slots with userspace_addr == 0.
773 */
774 for (i = 0; i < kvm->nmemslots; i++) {
775 struct kvm_memory_slot *memslot = &kvm->memslots[i];
776 unsigned long start = memslot->userspace_addr;
777 unsigned long end;
778
779 /* mmu_lock protects userspace_addr */
780 if (!start)
781 continue;
782
783 end = start + (memslot->npages << PAGE_SHIFT);
784 if (hva >= start && hva < end) {
785 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200786
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200787 retval |= handler(kvm, &memslot->rmap[gfn_offset]);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200788
789 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
790 int idx = gfn_offset;
791 idx /= KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL + j);
792 retval |= handler(kvm,
793 &memslot->lpage_info[j][idx].rmap_pde);
794 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200795 }
796 }
797
798 return retval;
799}
800
801int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
802{
803 return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
804}
805
806static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
807{
808 u64 *spte;
809 int young = 0;
810
Sheng Yang534e38b2008-09-08 15:12:30 +0800811 /* always return old for EPT */
812 if (!shadow_accessed_mask)
813 return 0;
814
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200815 spte = rmap_next(kvm, rmapp, NULL);
816 while (spte) {
817 int _young;
818 u64 _spte = *spte;
819 BUG_ON(!(_spte & PT_PRESENT_MASK));
820 _young = _spte & PT_ACCESSED_MASK;
821 if (_young) {
822 young = 1;
823 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
824 }
825 spte = rmap_next(kvm, rmapp, spte);
826 }
827 return young;
828}
829
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300830#define RMAP_RECYCLE_THRESHOLD 1000
831
Joerg Roedel852e3c12009-07-27 16:30:44 +0200832static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300833{
834 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200835 struct kvm_mmu_page *sp;
836
837 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300838
839 gfn = unalias_gfn(vcpu->kvm, gfn);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200840 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300841
842 kvm_unmap_rmapp(vcpu->kvm, rmapp);
843 kvm_flush_remote_tlbs(vcpu->kvm);
844}
845
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200846int kvm_age_hva(struct kvm *kvm, unsigned long hva)
847{
848 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
849}
850
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800851#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300852static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800853{
Avi Kivity139bdb22007-01-05 16:36:50 -0800854 u64 *pos;
855 u64 *end;
856
Avi Kivity47ad8e62007-05-06 15:50:58 +0300857 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300858 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800859 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800860 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800862 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800863 return 1;
864}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800865#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800866
Avi Kivity4db35312007-11-21 15:28:32 +0200867static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800868{
Avi Kivity4db35312007-11-21 15:28:32 +0200869 ASSERT(is_empty_shadow_page(sp->spt));
870 list_del(&sp->link);
871 __free_page(virt_to_page(sp->spt));
872 __free_page(virt_to_page(sp->gfns));
873 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800874 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800875}
876
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800877static unsigned kvm_page_table_hashfn(gfn_t gfn)
878{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200879 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800880}
881
Avi Kivity25c0de22007-01-05 16:36:42 -0800882static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
883 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800884{
Avi Kivity4db35312007-11-21 15:28:32 +0200885 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800886
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800887 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
888 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
889 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200890 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800891 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -0200892 INIT_LIST_HEAD(&sp->oos_link);
Sheng Yang291f26b2008-10-16 17:30:57 +0800893 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200894 sp->multimapped = 0;
895 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800896 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200897 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800898}
899
Avi Kivity714b93d2007-01-05 16:36:53 -0800900static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200901 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800902{
903 struct kvm_pte_chain *pte_chain;
904 struct hlist_node *node;
905 int i;
906
907 if (!parent_pte)
908 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200909 if (!sp->multimapped) {
910 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800911
912 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200913 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800914 return;
915 }
Avi Kivity4db35312007-11-21 15:28:32 +0200916 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800917 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200918 INIT_HLIST_HEAD(&sp->parent_ptes);
919 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800920 pte_chain->parent_ptes[0] = old;
921 }
Avi Kivity4db35312007-11-21 15:28:32 +0200922 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800923 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
924 continue;
925 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
926 if (!pte_chain->parent_ptes[i]) {
927 pte_chain->parent_ptes[i] = parent_pte;
928 return;
929 }
930 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800931 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800932 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200933 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800934 pte_chain->parent_ptes[0] = parent_pte;
935}
936
Avi Kivity4db35312007-11-21 15:28:32 +0200937static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800938 u64 *parent_pte)
939{
940 struct kvm_pte_chain *pte_chain;
941 struct hlist_node *node;
942 int i;
943
Avi Kivity4db35312007-11-21 15:28:32 +0200944 if (!sp->multimapped) {
945 BUG_ON(sp->parent_pte != parent_pte);
946 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800947 return;
948 }
Avi Kivity4db35312007-11-21 15:28:32 +0200949 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800950 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
951 if (!pte_chain->parent_ptes[i])
952 break;
953 if (pte_chain->parent_ptes[i] != parent_pte)
954 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800955 while (i + 1 < NR_PTE_CHAIN_ENTRIES
956 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800957 pte_chain->parent_ptes[i]
958 = pte_chain->parent_ptes[i + 1];
959 ++i;
960 }
961 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800962 if (i == 0) {
963 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300964 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200965 if (hlist_empty(&sp->parent_ptes)) {
966 sp->multimapped = 0;
967 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800968 }
969 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800970 return;
971 }
972 BUG();
973}
974
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300975
976static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
977 mmu_parent_walk_fn fn)
978{
979 struct kvm_pte_chain *pte_chain;
980 struct hlist_node *node;
981 struct kvm_mmu_page *parent_sp;
982 int i;
983
984 if (!sp->multimapped && sp->parent_pte) {
985 parent_sp = page_header(__pa(sp->parent_pte));
986 fn(vcpu, parent_sp);
987 mmu_parent_walk(vcpu, parent_sp, fn);
988 return;
989 }
990 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
991 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
992 if (!pte_chain->parent_ptes[i])
993 break;
994 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
995 fn(vcpu, parent_sp);
996 mmu_parent_walk(vcpu, parent_sp, fn);
997 }
998}
999
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001000static void kvm_mmu_update_unsync_bitmap(u64 *spte)
1001{
1002 unsigned int index;
1003 struct kvm_mmu_page *sp = page_header(__pa(spte));
1004
1005 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001006 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
1007 sp->unsync_children++;
1008 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001009}
1010
1011static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
1012{
1013 struct kvm_pte_chain *pte_chain;
1014 struct hlist_node *node;
1015 int i;
1016
1017 if (!sp->parent_pte)
1018 return;
1019
1020 if (!sp->multimapped) {
1021 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
1022 return;
1023 }
1024
1025 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1026 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1027 if (!pte_chain->parent_ptes[i])
1028 break;
1029 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
1030 }
1031}
1032
1033static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1034{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001035 kvm_mmu_update_parents_unsync(sp);
1036 return 1;
1037}
1038
1039static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
1040 struct kvm_mmu_page *sp)
1041{
1042 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
1043 kvm_mmu_update_parents_unsync(sp);
1044}
1045
Avi Kivityd761a502008-05-29 14:55:03 +03001046static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1047 struct kvm_mmu_page *sp)
1048{
1049 int i;
1050
1051 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1052 sp->spt[i] = shadow_trap_nonpresent_pte;
1053}
1054
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001055static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
1056 struct kvm_mmu_page *sp)
1057{
1058 return 1;
1059}
1060
Marcelo Tosattia7052892008-09-23 13:18:35 -03001061static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1062{
1063}
1064
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001065#define KVM_PAGE_ARRAY_NR 16
1066
1067struct kvm_mmu_pages {
1068 struct mmu_page_and_offset {
1069 struct kvm_mmu_page *sp;
1070 unsigned int idx;
1071 } page[KVM_PAGE_ARRAY_NR];
1072 unsigned int nr;
1073};
1074
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001075#define for_each_unsync_children(bitmap, idx) \
1076 for (idx = find_first_bit(bitmap, 512); \
1077 idx < 512; \
1078 idx = find_next_bit(bitmap, 512, idx+1))
1079
Hannes Edercded19f2009-02-21 02:19:13 +01001080static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1081 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001082{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001083 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001084
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001085 if (sp->unsync)
1086 for (i=0; i < pvec->nr; i++)
1087 if (pvec->page[i].sp == sp)
1088 return 0;
1089
1090 pvec->page[pvec->nr].sp = sp;
1091 pvec->page[pvec->nr].idx = idx;
1092 pvec->nr++;
1093 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1094}
1095
1096static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1097 struct kvm_mmu_pages *pvec)
1098{
1099 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001100
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001101 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001102 u64 ent = sp->spt[i];
1103
Marcelo Tosatti87917232008-12-22 18:49:30 -02001104 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001105 struct kvm_mmu_page *child;
1106 child = page_header(ent & PT64_BASE_ADDR_MASK);
1107
1108 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001109 if (mmu_pages_add(pvec, child, i))
1110 return -ENOSPC;
1111
1112 ret = __mmu_unsync_walk(child, pvec);
1113 if (!ret)
1114 __clear_bit(i, sp->unsync_child_bitmap);
1115 else if (ret > 0)
1116 nr_unsync_leaf += ret;
1117 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001118 return ret;
1119 }
1120
1121 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001122 nr_unsync_leaf++;
1123 if (mmu_pages_add(pvec, child, i))
1124 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001125 }
1126 }
1127 }
1128
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001129 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001130 sp->unsync_children = 0;
1131
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001132 return nr_unsync_leaf;
1133}
1134
1135static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1136 struct kvm_mmu_pages *pvec)
1137{
1138 if (!sp->unsync_children)
1139 return 0;
1140
1141 mmu_pages_add(pvec, sp, 0);
1142 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001143}
1144
Avi Kivity4db35312007-11-21 15:28:32 +02001145static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001146{
1147 unsigned index;
1148 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001149 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001150 struct hlist_node *node;
1151
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001152 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001153 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001154 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001155 hlist_for_each_entry(sp, node, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001156 if (sp->gfn == gfn && !sp->role.direct
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001157 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001158 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001159 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001160 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001161 }
1162 return NULL;
1163}
1164
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001165static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1166{
1167 WARN_ON(!sp->unsync);
1168 sp->unsync = 0;
1169 --kvm->stat.mmu_unsync;
1170}
1171
1172static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1173
1174static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1175{
1176 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1177 kvm_mmu_zap_page(vcpu->kvm, sp);
1178 return 1;
1179 }
1180
Avi Kivityf691fe12009-07-06 15:58:14 +03001181 trace_kvm_mmu_sync_page(sp);
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001182 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1183 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001184 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001185 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1186 kvm_mmu_zap_page(vcpu->kvm, sp);
1187 return 1;
1188 }
1189
1190 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001191 return 0;
1192}
1193
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001194struct mmu_page_path {
1195 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1196 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001197};
1198
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001199#define for_each_sp(pvec, sp, parents, i) \
1200 for (i = mmu_pages_next(&pvec, &parents, -1), \
1201 sp = pvec.page[i].sp; \
1202 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1203 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001204
Hannes Edercded19f2009-02-21 02:19:13 +01001205static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1206 struct mmu_page_path *parents,
1207 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001208{
1209 int n;
1210
1211 for (n = i+1; n < pvec->nr; n++) {
1212 struct kvm_mmu_page *sp = pvec->page[n].sp;
1213
1214 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1215 parents->idx[0] = pvec->page[n].idx;
1216 return n;
1217 }
1218
1219 parents->parent[sp->role.level-2] = sp;
1220 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1221 }
1222
1223 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001224}
1225
Hannes Edercded19f2009-02-21 02:19:13 +01001226static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001227{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001228 struct kvm_mmu_page *sp;
1229 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001230
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001231 do {
1232 unsigned int idx = parents->idx[level];
1233
1234 sp = parents->parent[level];
1235 if (!sp)
1236 return;
1237
1238 --sp->unsync_children;
1239 WARN_ON((int)sp->unsync_children < 0);
1240 __clear_bit(idx, sp->unsync_child_bitmap);
1241 level++;
1242 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1243}
1244
1245static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1246 struct mmu_page_path *parents,
1247 struct kvm_mmu_pages *pvec)
1248{
1249 parents->parent[parent->role.level-1] = NULL;
1250 pvec->nr = 0;
1251}
1252
1253static void mmu_sync_children(struct kvm_vcpu *vcpu,
1254 struct kvm_mmu_page *parent)
1255{
1256 int i;
1257 struct kvm_mmu_page *sp;
1258 struct mmu_page_path parents;
1259 struct kvm_mmu_pages pages;
1260
1261 kvm_mmu_pages_init(parent, &parents, &pages);
1262 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001263 int protected = 0;
1264
1265 for_each_sp(pages, sp, parents, i)
1266 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1267
1268 if (protected)
1269 kvm_flush_remote_tlbs(vcpu->kvm);
1270
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001271 for_each_sp(pages, sp, parents, i) {
1272 kvm_sync_page(vcpu, sp);
1273 mmu_pages_clear_parents(&parents);
1274 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001275 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001276 kvm_mmu_pages_init(parent, &parents, &pages);
1277 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001278}
1279
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001280static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1281 gfn_t gfn,
1282 gva_t gaddr,
1283 unsigned level,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001284 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001285 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001286 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001287{
1288 union kvm_mmu_page_role role;
1289 unsigned index;
1290 unsigned quadrant;
1291 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001292 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001293 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001294
Avi Kivitya770f6f2008-12-21 19:20:09 +02001295 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001296 role.level = level;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001297 role.direct = direct;
Avi Kivity41074d02007-12-09 17:00:02 +02001298 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001299 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001300 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1301 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1302 role.quadrant = quadrant;
1303 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001304 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001305 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001306 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1307 if (sp->gfn == gfn) {
1308 if (sp->unsync)
1309 if (kvm_sync_page(vcpu, sp))
1310 continue;
1311
1312 if (sp->role.word != role.word)
1313 continue;
1314
Avi Kivity4db35312007-11-21 15:28:32 +02001315 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001316 if (sp->unsync_children) {
1317 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1318 kvm_mmu_mark_parents_unsync(vcpu, sp);
1319 }
Avi Kivityf691fe12009-07-06 15:58:14 +03001320 trace_kvm_mmu_get_page(sp, false);
Avi Kivity4db35312007-11-21 15:28:32 +02001321 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001322 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001323 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001324 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1325 if (!sp)
1326 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001327 sp->gfn = gfn;
1328 sp->role = role;
1329 hlist_add_head(&sp->hash_link, bucket);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001330 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001331 if (rmap_write_protect(vcpu->kvm, gfn))
1332 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001333 account_shadowed(vcpu->kvm, gfn);
1334 }
Avi Kivity131d8272008-05-29 14:56:28 +03001335 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1336 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1337 else
1338 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001339 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001340 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001341}
1342
Avi Kivity2d111232008-12-25 14:39:47 +02001343static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1344 struct kvm_vcpu *vcpu, u64 addr)
1345{
1346 iterator->addr = addr;
1347 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1348 iterator->level = vcpu->arch.mmu.shadow_root_level;
1349 if (iterator->level == PT32E_ROOT_LEVEL) {
1350 iterator->shadow_addr
1351 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1352 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1353 --iterator->level;
1354 if (!iterator->shadow_addr)
1355 iterator->level = 0;
1356 }
1357}
1358
1359static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1360{
1361 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1362 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001363
1364 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1365 if (is_large_pte(*iterator->sptep))
1366 return false;
1367
Avi Kivity2d111232008-12-25 14:39:47 +02001368 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1369 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1370 return true;
1371}
1372
1373static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1374{
1375 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1376 --iterator->level;
1377}
1378
Avi Kivity90cb0522007-07-17 13:04:56 +03001379static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001380 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001381{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001382 unsigned i;
1383 u64 *pt;
1384 u64 ent;
1385
Avi Kivity4db35312007-11-21 15:28:32 +02001386 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001387
Avi Kivity697fe2e2007-01-05 16:36:46 -08001388 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1389 ent = pt[i];
1390
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001391 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001392 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001393 ent &= PT64_BASE_ADDR_MASK;
1394 mmu_page_remove_parent_pte(page_header(ent),
1395 &pt[i]);
1396 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001397 if (is_large_pte(ent))
1398 --kvm->stat.lpages;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001399 rmap_remove(kvm, &pt[i]);
1400 }
1401 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001402 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001403 }
Avi Kivitya4360362007-01-05 16:36:45 -08001404}
1405
Avi Kivity4db35312007-11-21 15:28:32 +02001406static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001407{
Avi Kivity4db35312007-11-21 15:28:32 +02001408 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001409}
1410
Avi Kivity12b7d282007-09-23 14:10:49 +02001411static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1412{
1413 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001414 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001415
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001416 kvm_for_each_vcpu(i, vcpu, kvm)
1417 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001418}
1419
Avi Kivity31aa2b42008-07-11 17:59:46 +03001420static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001421{
1422 u64 *parent_pte;
1423
Avi Kivity4db35312007-11-21 15:28:32 +02001424 while (sp->multimapped || sp->parent_pte) {
1425 if (!sp->multimapped)
1426 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001427 else {
1428 struct kvm_pte_chain *chain;
1429
Avi Kivity4db35312007-11-21 15:28:32 +02001430 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001431 struct kvm_pte_chain, link);
1432 parent_pte = chain->parent_ptes[0];
1433 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001434 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001435 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001436 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001437 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001438}
1439
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001440static int mmu_zap_unsync_children(struct kvm *kvm,
1441 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001442{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001443 int i, zapped = 0;
1444 struct mmu_page_path parents;
1445 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001446
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001447 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001448 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001449
1450 kvm_mmu_pages_init(parent, &parents, &pages);
1451 while (mmu_unsync_walk(parent, &pages)) {
1452 struct kvm_mmu_page *sp;
1453
1454 for_each_sp(pages, sp, parents, i) {
1455 kvm_mmu_zap_page(kvm, sp);
1456 mmu_pages_clear_parents(&parents);
1457 }
1458 zapped += pages.nr;
1459 kvm_mmu_pages_init(parent, &parents, &pages);
1460 }
1461
1462 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001463}
1464
Marcelo Tosatti07385412008-09-23 13:18:37 -03001465static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001466{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001467 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001468
1469 trace_kvm_mmu_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001470 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001471 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001472 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001473 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001474 kvm_flush_remote_tlbs(kvm);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001475 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001476 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001477 if (sp->unsync)
1478 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001479 if (!sp->root_count) {
1480 hlist_del(&sp->hash_link);
1481 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001482 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001483 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001484 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001485 kvm_reload_remote_mmus(kvm);
1486 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001487 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001488 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001489}
1490
Izik Eidus82ce2c92007-10-02 18:52:55 +02001491/*
1492 * Changing the number of mmu pages allocated to the vm
1493 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1494 */
1495void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1496{
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001497 int used_pages;
1498
1499 used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1500 used_pages = max(0, used_pages);
1501
Izik Eidus82ce2c92007-10-02 18:52:55 +02001502 /*
1503 * If we set the number of mmu pages to be smaller be than the
1504 * number of actived pages , we must to free some mmu pages before we
1505 * change the value
1506 */
1507
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001508 if (used_pages > kvm_nr_mmu_pages) {
1509 while (used_pages > kvm_nr_mmu_pages) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001510 struct kvm_mmu_page *page;
1511
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001512 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001513 struct kvm_mmu_page, link);
1514 kvm_mmu_zap_page(kvm, page);
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001515 used_pages--;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001516 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001517 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001518 }
1519 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001520 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1521 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001522
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001523 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001524}
1525
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001526static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001527{
1528 unsigned index;
1529 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001530 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001531 struct hlist_node *node, *n;
1532 int r;
1533
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001534 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001535 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001536 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001537 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001538 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001539 if (sp->gfn == gfn && !sp->role.direct) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001540 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001541 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001542 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001543 if (kvm_mmu_zap_page(kvm, sp))
1544 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001545 }
1546 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001547}
1548
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001549static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001550{
Avi Kivity4677a3b2009-01-06 13:00:27 +02001551 unsigned index;
1552 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001553 struct kvm_mmu_page *sp;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001554 struct hlist_node *node, *nn;
Avi Kivity97a0a012007-05-31 15:08:29 +03001555
Avi Kivity4677a3b2009-01-06 13:00:27 +02001556 index = kvm_page_table_hashfn(gfn);
1557 bucket = &kvm->arch.mmu_page_hash[index];
1558 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001559 if (sp->gfn == gfn && !sp->role.direct
Avi Kivity4677a3b2009-01-06 13:00:27 +02001560 && !sp->role.invalid) {
1561 pgprintk("%s: zap %lx %x\n",
1562 __func__, gfn, sp->role.word);
1563 kvm_mmu_zap_page(kvm, sp);
1564 }
Avi Kivity97a0a012007-05-31 15:08:29 +03001565 }
1566}
1567
Avi Kivity38c335f2007-11-21 14:20:22 +02001568static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001569{
Avi Kivity38c335f2007-11-21 14:20:22 +02001570 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001571 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001572
Sheng Yang291f26b2008-10-16 17:30:57 +08001573 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001574}
1575
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001576static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1577{
1578 int i;
1579 u64 *pt = sp->spt;
1580
1581 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1582 return;
1583
1584 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1585 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001586 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001587 }
1588}
1589
Avi Kivity039576c2007-03-20 12:46:50 +02001590struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1591{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001592 struct page *page;
1593
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001594 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001595
1596 if (gpa == UNMAPPED_GVA)
1597 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001598
Izik Eidus72dc67a2008-02-10 18:04:15 +02001599 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001600
1601 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001602}
1603
Sheng Yang74be52e2008-10-09 16:01:56 +08001604/*
1605 * The function is based on mtrr_type_lookup() in
1606 * arch/x86/kernel/cpu/mtrr/generic.c
1607 */
1608static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1609 u64 start, u64 end)
1610{
1611 int i;
1612 u64 base, mask;
1613 u8 prev_match, curr_match;
1614 int num_var_ranges = KVM_NR_VAR_MTRR;
1615
1616 if (!mtrr_state->enabled)
1617 return 0xFF;
1618
1619 /* Make end inclusive end, instead of exclusive */
1620 end--;
1621
1622 /* Look in fixed ranges. Just return the type as per start */
1623 if (mtrr_state->have_fixed && (start < 0x100000)) {
1624 int idx;
1625
1626 if (start < 0x80000) {
1627 idx = 0;
1628 idx += (start >> 16);
1629 return mtrr_state->fixed_ranges[idx];
1630 } else if (start < 0xC0000) {
1631 idx = 1 * 8;
1632 idx += ((start - 0x80000) >> 14);
1633 return mtrr_state->fixed_ranges[idx];
1634 } else if (start < 0x1000000) {
1635 idx = 3 * 8;
1636 idx += ((start - 0xC0000) >> 12);
1637 return mtrr_state->fixed_ranges[idx];
1638 }
1639 }
1640
1641 /*
1642 * Look in variable ranges
1643 * Look of multiple ranges matching this address and pick type
1644 * as per MTRR precedence
1645 */
1646 if (!(mtrr_state->enabled & 2))
1647 return mtrr_state->def_type;
1648
1649 prev_match = 0xFF;
1650 for (i = 0; i < num_var_ranges; ++i) {
1651 unsigned short start_state, end_state;
1652
1653 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1654 continue;
1655
1656 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1657 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1658 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1659 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1660
1661 start_state = ((start & mask) == (base & mask));
1662 end_state = ((end & mask) == (base & mask));
1663 if (start_state != end_state)
1664 return 0xFE;
1665
1666 if ((start & mask) != (base & mask))
1667 continue;
1668
1669 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1670 if (prev_match == 0xFF) {
1671 prev_match = curr_match;
1672 continue;
1673 }
1674
1675 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1676 curr_match == MTRR_TYPE_UNCACHABLE)
1677 return MTRR_TYPE_UNCACHABLE;
1678
1679 if ((prev_match == MTRR_TYPE_WRBACK &&
1680 curr_match == MTRR_TYPE_WRTHROUGH) ||
1681 (prev_match == MTRR_TYPE_WRTHROUGH &&
1682 curr_match == MTRR_TYPE_WRBACK)) {
1683 prev_match = MTRR_TYPE_WRTHROUGH;
1684 curr_match = MTRR_TYPE_WRTHROUGH;
1685 }
1686
1687 if (prev_match != curr_match)
1688 return MTRR_TYPE_UNCACHABLE;
1689 }
1690
1691 if (prev_match != 0xFF)
1692 return prev_match;
1693
1694 return mtrr_state->def_type;
1695}
1696
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001697u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001698{
1699 u8 mtrr;
1700
1701 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1702 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1703 if (mtrr == 0xfe || mtrr == 0xff)
1704 mtrr = MTRR_TYPE_WRBACK;
1705 return mtrr;
1706}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001707EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001708
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001709static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1710{
1711 unsigned index;
1712 struct hlist_head *bucket;
1713 struct kvm_mmu_page *s;
1714 struct hlist_node *node, *n;
1715
Avi Kivityf691fe12009-07-06 15:58:14 +03001716 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001717 index = kvm_page_table_hashfn(sp->gfn);
1718 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1719 /* don't unsync if pagetable is shadowed with multiple roles */
1720 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001721 if (s->gfn != sp->gfn || s->role.direct)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001722 continue;
1723 if (s->role.word != sp->role.word)
1724 return 1;
1725 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001726 ++vcpu->kvm->stat.mmu_unsync;
1727 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001728
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001729 kvm_mmu_mark_parents_unsync(vcpu, sp);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001730
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001731 mmu_convert_notrap(sp);
1732 return 0;
1733}
1734
1735static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1736 bool can_unsync)
1737{
1738 struct kvm_mmu_page *shadow;
1739
1740 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1741 if (shadow) {
1742 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1743 return 1;
1744 if (shadow->unsync)
1745 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001746 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001747 return kvm_unsync_page(vcpu, shadow);
1748 return 1;
1749 }
1750 return 0;
1751}
1752
Avi Kivityd555c332009-06-10 14:24:23 +03001753static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001754 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001755 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001756 gfn_t gfn, pfn_t pfn, bool speculative,
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001757 bool can_unsync)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001758{
1759 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001760 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001761
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001762 /*
1763 * We don't set the accessed bit, since we sometimes want to see
1764 * whether the guest actually used the pte (in order to detect
1765 * demand paging).
1766 */
Sheng Yang7b523452008-04-25 21:13:50 +08001767 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001768 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001769 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001770 if (!dirty)
1771 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001772 if (pte_access & ACC_EXEC_MASK)
1773 spte |= shadow_x_mask;
1774 else
1775 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001776 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001777 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001778 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001779 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001780 if (tdp_enabled)
1781 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1782 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001783
Anthony Liguori35149e22008-04-02 14:46:56 -05001784 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001785
1786 if ((pte_access & ACC_WRITE_MASK)
1787 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001788
Joerg Roedel852e3c12009-07-27 16:30:44 +02001789 if (level > PT_PAGE_TABLE_LEVEL &&
1790 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001791 ret = 1;
1792 spte = shadow_trap_nonpresent_pte;
1793 goto set_pte;
1794 }
1795
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001796 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001797
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001798 /*
1799 * Optimization: for pte sync, if spte was writable the hash
1800 * lookup is unnecessary (and expensive). Write protection
1801 * is responsibility of mmu_get_page / kvm_sync_page.
1802 * Same reasoning can be applied to dirty page accounting.
1803 */
Avi Kivityd555c332009-06-10 14:24:23 +03001804 if (!can_unsync && is_writeble_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001805 goto set_pte;
1806
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001807 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001808 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001809 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001810 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001811 pte_access &= ~ACC_WRITE_MASK;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001812 if (is_writeble_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001813 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001814 }
1815 }
1816
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001817 if (pte_access & ACC_WRITE_MASK)
1818 mark_page_dirty(vcpu->kvm, gfn);
1819
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001820set_pte:
Avi Kivityd555c332009-06-10 14:24:23 +03001821 __set_spte(sptep, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001822 return ret;
1823}
1824
Avi Kivityd555c332009-06-10 14:24:23 +03001825static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001826 unsigned pt_access, unsigned pte_access,
1827 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001828 int *ptwrite, int level, gfn_t gfn,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001829 pfn_t pfn, bool speculative)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001830{
1831 int was_rmapped = 0;
Avi Kivityd555c332009-06-10 14:24:23 +03001832 int was_writeble = is_writeble_pte(*sptep);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001833 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001834
1835 pgprintk("%s: spte %llx access %x write_fault %d"
1836 " user_fault %d gfn %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001837 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001838 write_fault, user_fault, gfn);
1839
Avi Kivityd555c332009-06-10 14:24:23 +03001840 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001841 /*
1842 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1843 * the parent of the now unreachable PTE.
1844 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02001845 if (level > PT_PAGE_TABLE_LEVEL &&
1846 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001847 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03001848 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001849
1850 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03001851 mmu_page_remove_parent_pte(child, sptep);
1852 } else if (pfn != spte_to_pfn(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001853 pgprintk("hfn old %lx new %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001854 spte_to_pfn(*sptep), pfn);
1855 rmap_remove(vcpu->kvm, sptep);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001856 } else
1857 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001858 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02001859
Avi Kivityd555c332009-06-10 14:24:23 +03001860 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001861 dirty, level, gfn, pfn, speculative, true)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001862 if (write_fault)
1863 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001864 kvm_x86_ops->tlb_flush(vcpu);
1865 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001866
Avi Kivityd555c332009-06-10 14:24:23 +03001867 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001868 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001869 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02001870 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
1871 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03001872 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001873 ++vcpu->kvm->stat.lpages;
1874
Avi Kivityd555c332009-06-10 14:24:23 +03001875 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001876 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02001877 rmap_count = rmap_add(vcpu, sptep, gfn);
Izik Eidusacb66dd2009-09-23 21:47:16 +03001878 kvm_release_pfn_clean(pfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001879 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02001880 rmap_recycle(vcpu, sptep, gfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001881 } else {
1882 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001883 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001884 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001885 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001886 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001887 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03001888 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001889 vcpu->arch.last_pte_gfn = gfn;
1890 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001891}
1892
Avi Kivity6aa8b732006-12-10 02:21:36 -08001893static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1894{
1895}
1896
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001897static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001898 int level, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001899{
Avi Kivity9f652d22008-12-25 14:54:25 +02001900 struct kvm_shadow_walk_iterator iterator;
1901 struct kvm_mmu_page *sp;
1902 int pt_write = 0;
1903 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001904
Avi Kivity9f652d22008-12-25 14:54:25 +02001905 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02001906 if (iterator.level == level) {
Avi Kivity9f652d22008-12-25 14:54:25 +02001907 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1908 0, write, 1, &pt_write,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001909 level, gfn, pfn, false);
Avi Kivity9f652d22008-12-25 14:54:25 +02001910 ++vcpu->stat.pf_fixed;
1911 break;
1912 }
1913
1914 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1915 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1916 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1917 iterator.level - 1,
1918 1, ACC_ALL, iterator.sptep);
1919 if (!sp) {
1920 pgprintk("nonpaging_map: ENOMEM\n");
1921 kvm_release_pfn_clean(pfn);
1922 return -ENOMEM;
1923 }
1924
Avi Kivityd555c332009-06-10 14:24:23 +03001925 __set_spte(iterator.sptep,
1926 __pa(sp->spt)
1927 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1928 | shadow_user_mask | shadow_x_mask);
Avi Kivity9f652d22008-12-25 14:54:25 +02001929 }
1930 }
1931 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001932}
1933
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001934static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1935{
1936 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001937 int level;
Anthony Liguori35149e22008-04-02 14:46:56 -05001938 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001939 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001940
Joerg Roedel852e3c12009-07-27 16:30:44 +02001941 level = mapping_level(vcpu, gfn);
1942
1943 /*
1944 * This path builds a PAE pagetable - so we can map 2mb pages at
1945 * maximum. Therefore check if the level is larger than that.
1946 */
1947 if (level > PT_DIRECTORY_LEVEL)
1948 level = PT_DIRECTORY_LEVEL;
1949
1950 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001951
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001952 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001953 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001954 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001955
Avi Kivityd196e342008-01-24 11:44:11 +02001956 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001957 if (is_error_pfn(pfn)) {
1958 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001959 return 1;
1960 }
1961
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001962 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001963 if (mmu_notifier_retry(vcpu, mmu_seq))
1964 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02001965 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel852e3c12009-07-27 16:30:44 +02001966 r = __direct_map(vcpu, v, write, level, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001967 spin_unlock(&vcpu->kvm->mmu_lock);
1968
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001969
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001970 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001971
1972out_unlock:
1973 spin_unlock(&vcpu->kvm->mmu_lock);
1974 kvm_release_pfn_clean(pfn);
1975 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001976}
1977
1978
Avi Kivity17ac10a2007-01-05 16:36:40 -08001979static void mmu_free_roots(struct kvm_vcpu *vcpu)
1980{
1981 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001982 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001983
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001984 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001985 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001986 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001987 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1988 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001989
Avi Kivity4db35312007-11-21 15:28:32 +02001990 sp = page_header(root);
1991 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001992 if (!sp->root_count && sp->role.invalid)
1993 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001994 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001995 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001996 return;
1997 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001998 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001999 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002000
Avi Kivity417726a2007-04-12 17:35:58 +03002001 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002002 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002003 sp = page_header(root);
2004 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002005 if (!sp->root_count && sp->role.invalid)
2006 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03002007 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002008 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002009 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002010 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002011 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002012}
2013
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002014static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2015{
2016 int ret = 0;
2017
2018 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
2019 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
2020 ret = 1;
2021 }
2022
2023 return ret;
2024}
2025
2026static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002027{
2028 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002029 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02002030 struct kvm_mmu_page *sp;
Avi Kivityf6e2c022009-01-11 13:02:10 +02002031 int direct = 0;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002032 u64 pdptr;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002033
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002034 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002035
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002036 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2037 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002038
2039 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01002040 if (tdp_enabled)
Avi Kivityf6e2c022009-01-11 13:02:10 +02002041 direct = 1;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002042 if (mmu_check_root(vcpu, root_gfn))
2043 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002044 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c022009-01-11 13:02:10 +02002045 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01002046 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002047 root = __pa(sp->spt);
2048 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002049 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002050 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002051 }
Avi Kivityf6e2c022009-01-11 13:02:10 +02002052 direct = !is_paging(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002053 if (tdp_enabled)
Avi Kivityf6e2c022009-01-11 13:02:10 +02002054 direct = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002055 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002056 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002057
2058 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002059 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002060 pdptr = kvm_pdptr_read(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002061 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002062 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002063 continue;
2064 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002065 root_gfn = pdptr >> PAGE_SHIFT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002066 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002067 root_gfn = 0;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002068 if (mmu_check_root(vcpu, root_gfn))
2069 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002070 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c022009-01-11 13:02:10 +02002071 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002072 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002073 root = __pa(sp->spt);
2074 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002075 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002076 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002077 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002078 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002079}
2080
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002081static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2082{
2083 int i;
2084 struct kvm_mmu_page *sp;
2085
2086 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2087 return;
2088 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2089 hpa_t root = vcpu->arch.mmu.root_hpa;
2090 sp = page_header(root);
2091 mmu_sync_children(vcpu, sp);
2092 return;
2093 }
2094 for (i = 0; i < 4; ++i) {
2095 hpa_t root = vcpu->arch.mmu.pae_root[i];
2096
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002097 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002098 root &= PT64_BASE_ADDR_MASK;
2099 sp = page_header(root);
2100 mmu_sync_children(vcpu, sp);
2101 }
2102 }
2103}
2104
2105void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2106{
2107 spin_lock(&vcpu->kvm->mmu_lock);
2108 mmu_sync_roots(vcpu);
2109 spin_unlock(&vcpu->kvm->mmu_lock);
2110}
2111
Avi Kivity6aa8b732006-12-10 02:21:36 -08002112static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2113{
2114 return vaddr;
2115}
2116
2117static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002118 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002119{
Avi Kivitye8332402007-12-09 18:43:00 +02002120 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002121 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002122
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002123 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002124 r = mmu_topup_memory_caches(vcpu);
2125 if (r)
2126 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002127
Avi Kivity6aa8b732006-12-10 02:21:36 -08002128 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002129 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002130
Avi Kivitye8332402007-12-09 18:43:00 +02002131 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002132
Avi Kivitye8332402007-12-09 18:43:00 +02002133 return nonpaging_map(vcpu, gva & PAGE_MASK,
2134 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002135}
2136
Joerg Roedelfb72d162008-02-07 13:47:44 +01002137static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2138 u32 error_code)
2139{
Anthony Liguori35149e22008-04-02 14:46:56 -05002140 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002141 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002142 int level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002143 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002144 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002145
2146 ASSERT(vcpu);
2147 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2148
2149 r = mmu_topup_memory_caches(vcpu);
2150 if (r)
2151 return r;
2152
Joerg Roedel852e3c12009-07-27 16:30:44 +02002153 level = mapping_level(vcpu, gfn);
2154
2155 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2156
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002157 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002158 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002159 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002160 if (is_error_pfn(pfn)) {
2161 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002162 return 1;
2163 }
2164 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002165 if (mmu_notifier_retry(vcpu, mmu_seq))
2166 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002167 kvm_mmu_free_some_pages(vcpu);
2168 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002169 level, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002170 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002171
2172 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002173
2174out_unlock:
2175 spin_unlock(&vcpu->kvm->mmu_lock);
2176 kvm_release_pfn_clean(pfn);
2177 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002178}
2179
Avi Kivity6aa8b732006-12-10 02:21:36 -08002180static void nonpaging_free(struct kvm_vcpu *vcpu)
2181{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002182 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002183}
2184
2185static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2186{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002187 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002188
2189 context->new_cr3 = nonpaging_new_cr3;
2190 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002191 context->gva_to_gpa = nonpaging_gva_to_gpa;
2192 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002193 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002194 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002195 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002196 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002197 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002198 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002199 return 0;
2200}
2201
Avi Kivityd835dfe2007-11-21 02:57:59 +02002202void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002203{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002204 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002205 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002206}
2207
2208static void paging_new_cr3(struct kvm_vcpu *vcpu)
2209{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002210 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002211 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002212}
2213
Avi Kivity6aa8b732006-12-10 02:21:36 -08002214static void inject_page_fault(struct kvm_vcpu *vcpu,
2215 u64 addr,
2216 u32 err_code)
2217{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002218 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002219}
2220
Avi Kivity6aa8b732006-12-10 02:21:36 -08002221static void paging_free(struct kvm_vcpu *vcpu)
2222{
2223 nonpaging_free(vcpu);
2224}
2225
Dong, Eddie82725b22009-03-30 16:21:08 +08002226static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2227{
2228 int bit7;
2229
2230 bit7 = (gpte >> 7) & 1;
2231 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2232}
2233
Avi Kivity6aa8b732006-12-10 02:21:36 -08002234#define PTTYPE 64
2235#include "paging_tmpl.h"
2236#undef PTTYPE
2237
2238#define PTTYPE 32
2239#include "paging_tmpl.h"
2240#undef PTTYPE
2241
Dong, Eddie82725b22009-03-30 16:21:08 +08002242static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2243{
2244 struct kvm_mmu *context = &vcpu->arch.mmu;
2245 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2246 u64 exb_bit_rsvd = 0;
2247
2248 if (!is_nx(vcpu))
2249 exb_bit_rsvd = rsvd_bits(63, 63);
2250 switch (level) {
2251 case PT32_ROOT_LEVEL:
2252 /* no rsvd bits for 2 level 4K page table entries */
2253 context->rsvd_bits_mask[0][1] = 0;
2254 context->rsvd_bits_mask[0][0] = 0;
2255 if (is_cpuid_PSE36())
2256 /* 36bits PSE 4MB page */
2257 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2258 else
2259 /* 32 bits PSE 4MB page */
2260 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Avi Kivity29a4b932009-05-19 13:29:27 +03002261 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002262 break;
2263 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002264 context->rsvd_bits_mask[0][2] =
2265 rsvd_bits(maxphyaddr, 63) |
2266 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002267 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002268 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002269 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2270 rsvd_bits(maxphyaddr, 62); /* PTE */
2271 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2272 rsvd_bits(maxphyaddr, 62) |
2273 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002274 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002275 break;
2276 case PT64_ROOT_LEVEL:
2277 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2278 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2279 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2280 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2281 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002282 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002283 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2284 rsvd_bits(maxphyaddr, 51);
2285 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002286 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2287 rsvd_bits(maxphyaddr, 51) |
2288 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002289 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002290 rsvd_bits(maxphyaddr, 51) |
2291 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002292 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002293 break;
2294 }
2295}
2296
Avi Kivity17ac10a2007-01-05 16:36:40 -08002297static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002298{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002299 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002300
2301 ASSERT(is_pae(vcpu));
2302 context->new_cr3 = paging_new_cr3;
2303 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002304 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002305 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002306 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002307 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002308 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002309 context->root_level = level;
2310 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002311 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002312 return 0;
2313}
2314
Avi Kivity17ac10a2007-01-05 16:36:40 -08002315static int paging64_init_context(struct kvm_vcpu *vcpu)
2316{
Dong, Eddie82725b22009-03-30 16:21:08 +08002317 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002318 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2319}
2320
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321static int paging32_init_context(struct kvm_vcpu *vcpu)
2322{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002323 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002324
Dong, Eddie82725b22009-03-30 16:21:08 +08002325 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002326 context->new_cr3 = paging_new_cr3;
2327 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002328 context->gva_to_gpa = paging32_gva_to_gpa;
2329 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002330 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002331 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002332 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333 context->root_level = PT32_ROOT_LEVEL;
2334 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002335 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002336 return 0;
2337}
2338
2339static int paging32E_init_context(struct kvm_vcpu *vcpu)
2340{
Dong, Eddie82725b22009-03-30 16:21:08 +08002341 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002342 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002343}
2344
Joerg Roedelfb72d162008-02-07 13:47:44 +01002345static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2346{
2347 struct kvm_mmu *context = &vcpu->arch.mmu;
2348
2349 context->new_cr3 = nonpaging_new_cr3;
2350 context->page_fault = tdp_page_fault;
2351 context->free = nonpaging_free;
2352 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002353 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002354 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002355 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002356 context->root_hpa = INVALID_PAGE;
2357
2358 if (!is_paging(vcpu)) {
2359 context->gva_to_gpa = nonpaging_gva_to_gpa;
2360 context->root_level = 0;
2361 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002362 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002363 context->gva_to_gpa = paging64_gva_to_gpa;
2364 context->root_level = PT64_ROOT_LEVEL;
2365 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002366 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002367 context->gva_to_gpa = paging64_gva_to_gpa;
2368 context->root_level = PT32E_ROOT_LEVEL;
2369 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002370 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002371 context->gva_to_gpa = paging32_gva_to_gpa;
2372 context->root_level = PT32_ROOT_LEVEL;
2373 }
2374
2375 return 0;
2376}
2377
2378static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002380 int r;
2381
Avi Kivity6aa8b732006-12-10 02:21:36 -08002382 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002383 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002384
2385 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002386 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002387 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002388 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002389 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002390 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002391 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002392 r = paging32_init_context(vcpu);
2393
2394 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2395
2396 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002397}
2398
Joerg Roedelfb72d162008-02-07 13:47:44 +01002399static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2400{
Anthony Liguori35149e22008-04-02 14:46:56 -05002401 vcpu->arch.update_pte.pfn = bad_pfn;
2402
Joerg Roedelfb72d162008-02-07 13:47:44 +01002403 if (tdp_enabled)
2404 return init_kvm_tdp_mmu(vcpu);
2405 else
2406 return init_kvm_softmmu(vcpu);
2407}
2408
Avi Kivity6aa8b732006-12-10 02:21:36 -08002409static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2410{
2411 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002412 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2413 vcpu->arch.mmu.free(vcpu);
2414 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002415 }
2416}
2417
2418int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2419{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002420 destroy_kvm_mmu(vcpu);
2421 return init_kvm_mmu(vcpu);
2422}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002423EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002424
2425int kvm_mmu_load(struct kvm_vcpu *vcpu)
2426{
Avi Kivity714b93d2007-01-05 16:36:53 -08002427 int r;
2428
Avi Kivitye2dec932007-01-05 16:36:54 -08002429 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002430 if (r)
2431 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002432 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002433 kvm_mmu_free_some_pages(vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002434 r = mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002435 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002436 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002437 if (r)
2438 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08002439 /* set_cr3() should ensure TLB has been flushed */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002440 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08002441out:
2442 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002443}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002444EXPORT_SYMBOL_GPL(kvm_mmu_load);
2445
2446void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2447{
2448 mmu_free_roots(vcpu);
2449}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002450
Avi Kivity09072da2007-05-01 14:16:52 +03002451static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002452 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002453 u64 *spte)
2454{
2455 u64 pte;
2456 struct kvm_mmu_page *child;
2457
2458 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002459 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03002460 if (is_last_spte(pte, sp->role.level))
Izik Eidus290fc382007-09-27 14:11:22 +02002461 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002462 else {
2463 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002464 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002465 }
2466 }
Avi Kivityd555c332009-06-10 14:24:23 +03002467 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002468 if (is_large_pte(pte))
2469 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002470}
2471
Avi Kivity00284252007-05-01 16:53:31 +03002472static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002473 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002474 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002475 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002476{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002477 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02002478 ++vcpu->kvm->stat.mmu_pde_zapped;
2479 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03002480 }
Avi Kivity00284252007-05-01 16:53:31 +03002481
Avi Kivity4cee5762007-11-18 16:37:07 +02002482 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002483 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002484 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002485 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002486 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002487}
2488
Avi Kivity79539ce2007-11-21 02:06:21 +02002489static bool need_remote_flush(u64 old, u64 new)
2490{
2491 if (!is_shadow_present_pte(old))
2492 return false;
2493 if (!is_shadow_present_pte(new))
2494 return true;
2495 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2496 return true;
2497 old ^= PT64_NX_MASK;
2498 new ^= PT64_NX_MASK;
2499 return (old & ~new & PT64_PERM_MASK) != 0;
2500}
2501
2502static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2503{
2504 if (need_remote_flush(old, new))
2505 kvm_flush_remote_tlbs(vcpu->kvm);
2506 else
2507 kvm_mmu_flush_tlb(vcpu);
2508}
2509
Avi Kivity12b7d282007-09-23 14:10:49 +02002510static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2511{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002512 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002513
Sheng Yang7b523452008-04-25 21:13:50 +08002514 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002515}
2516
Avi Kivityd7824ff2007-12-30 12:29:05 +02002517static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2518 const u8 *new, int bytes)
2519{
2520 gfn_t gfn;
2521 int r;
2522 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002523 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002524
2525 if (bytes != 4 && bytes != 8)
2526 return;
2527
2528 /*
2529 * Assume that the pte write on a page table of the same type
2530 * as the current vcpu paging mode. This is nearly always true
2531 * (might be false while changing modes). Note it is verified later
2532 * by update_pte().
2533 */
2534 if (is_pae(vcpu)) {
2535 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2536 if ((bytes == 4) && (gpa % 4 == 0)) {
2537 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2538 if (r)
2539 return;
2540 memcpy((void *)&gpte + (gpa % 8), new, 4);
2541 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2542 memcpy((void *)&gpte, new, 8);
2543 }
2544 } else {
2545 if ((bytes == 4) && (gpa % 4 == 0))
2546 memcpy((void *)&gpte, new, 4);
2547 }
Avi Kivity43a37952009-06-10 14:12:05 +03002548 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02002549 return;
2550 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002551
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002552 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002553 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002554 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002555
Anthony Liguori35149e22008-04-02 14:46:56 -05002556 if (is_error_pfn(pfn)) {
2557 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002558 return;
2559 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002560 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002561 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002562}
2563
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002564static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2565{
2566 u64 *spte = vcpu->arch.last_pte_updated;
2567
2568 if (spte
2569 && vcpu->arch.last_pte_gfn == gfn
2570 && shadow_accessed_mask
2571 && !(*spte & shadow_accessed_mask)
2572 && is_shadow_present_pte(*spte))
2573 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2574}
2575
Avi Kivity09072da2007-05-01 14:16:52 +03002576void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002577 const u8 *new, int bytes,
2578 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002579{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002580 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002581 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002582 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002583 struct hlist_head *bucket;
2584 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002585 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002586 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002587 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002588 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002589 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002590 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002591 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002592 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002593 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002594 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002595 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002596
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002597 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002598 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002599 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002600 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002601 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002602 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002603 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002604 if (guest_initiated) {
2605 if (gfn == vcpu->arch.last_pt_write_gfn
2606 && !last_updated_pte_accessed(vcpu)) {
2607 ++vcpu->arch.last_pt_write_count;
2608 if (vcpu->arch.last_pt_write_count >= 3)
2609 flooded = 1;
2610 } else {
2611 vcpu->arch.last_pt_write_gfn = gfn;
2612 vcpu->arch.last_pt_write_count = 1;
2613 vcpu->arch.last_pte_updated = NULL;
2614 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002615 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002616 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002617 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002618 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02002619 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002620 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002621 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002622 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002623 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002624 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002625 /*
2626 * Misaligned accesses are too much trouble to fix
2627 * up; also, they usually indicate a page is not used
2628 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002629 *
2630 * If we're seeing too many writes to a page,
2631 * it may no longer be a page table, or we may be
2632 * forking, in which case it is better to unmap the
2633 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002634 */
2635 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002636 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002637 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2638 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002639 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002640 continue;
2641 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002642 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002643 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002644 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002645 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002646 page_offset <<= 1; /* 32->64 */
2647 /*
2648 * A 32-bit pde maps 4MB while the shadow pdes map
2649 * only 2MB. So we need to double the offset again
2650 * and zap two pdes instead of one.
2651 */
2652 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002653 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002654 page_offset <<= 1;
2655 npte = 2;
2656 }
Avi Kivityfce06572007-05-01 16:44:05 +03002657 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002658 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002659 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002660 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002661 }
Avi Kivity4db35312007-11-21 15:28:32 +02002662 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002663 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2664 gentry = 0;
2665 r = kvm_read_guest_atomic(vcpu->kvm,
2666 gpa & ~(u64)(pte_size - 1),
2667 &gentry, pte_size);
2668 new = (const void *)&gentry;
2669 if (r < 0)
2670 new = NULL;
2671 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002672 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002673 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002674 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002675 if (new)
2676 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002677 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002678 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002679 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002680 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002681 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002682 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002683 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2684 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2685 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002686 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002687}
2688
Avi Kivitya4360362007-01-05 16:36:45 -08002689int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2690{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002691 gpa_t gpa;
2692 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002693
Avi Kivity60f24782009-08-27 13:37:06 +03002694 if (tdp_enabled)
2695 return 0;
2696
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002697 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002698
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002699 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002700 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002701 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002702 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002703}
Avi Kivity577bdc42008-07-19 08:57:05 +03002704EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002705
Avi Kivity22d95b12007-09-14 20:26:06 +03002706void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002707{
Izik Eidus3b80fff2009-07-28 15:26:58 -03002708 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES &&
2709 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02002710 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002711
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002712 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002713 struct kvm_mmu_page, link);
2714 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002715 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002716 }
2717}
Avi Kivityebeace82007-01-05 16:36:47 -08002718
Avi Kivity30677142007-10-28 18:48:59 +02002719int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2720{
2721 int r;
2722 enum emulation_result er;
2723
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002724 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002725 if (r < 0)
2726 goto out;
2727
2728 if (!r) {
2729 r = 1;
2730 goto out;
2731 }
2732
Avi Kivityb733bfb2007-10-28 18:52:05 +02002733 r = mmu_topup_memory_caches(vcpu);
2734 if (r)
2735 goto out;
2736
Avi Kivity30677142007-10-28 18:48:59 +02002737 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002738
2739 switch (er) {
2740 case EMULATE_DONE:
2741 return 1;
2742 case EMULATE_DO_MMIO:
2743 ++vcpu->stat.mmio_exits;
2744 return 0;
2745 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002746 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2747 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2748 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02002749 default:
2750 BUG();
2751 }
2752out:
Avi Kivity30677142007-10-28 18:48:59 +02002753 return r;
2754}
2755EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2756
Marcelo Tosattia7052892008-09-23 13:18:35 -03002757void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2758{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002759 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002760 kvm_mmu_flush_tlb(vcpu);
2761 ++vcpu->stat.invlpg;
2762}
2763EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2764
Joerg Roedel18552672008-02-07 13:47:41 +01002765void kvm_enable_tdp(void)
2766{
2767 tdp_enabled = true;
2768}
2769EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2770
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002771void kvm_disable_tdp(void)
2772{
2773 tdp_enabled = false;
2774}
2775EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2776
Avi Kivity6aa8b732006-12-10 02:21:36 -08002777static void free_mmu_pages(struct kvm_vcpu *vcpu)
2778{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002779 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002780}
2781
2782static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2783{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002784 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002785 int i;
2786
2787 ASSERT(vcpu);
2788
Avi Kivity17ac10a2007-01-05 16:36:40 -08002789 /*
2790 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2791 * Therefore we need to allocate shadow page tables in the first
2792 * 4GB of memory, which happens to fit the DMA32 zone.
2793 */
2794 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2795 if (!page)
2796 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002797 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002798 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002799 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002800
Avi Kivity6aa8b732006-12-10 02:21:36 -08002801 return 0;
2802
2803error_1:
2804 free_mmu_pages(vcpu);
2805 return -ENOMEM;
2806}
2807
Ingo Molnar8018c272006-12-29 16:50:01 -08002808int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002809{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002810 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002811 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002812
Ingo Molnar8018c272006-12-29 16:50:01 -08002813 return alloc_mmu_pages(vcpu);
2814}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002815
Ingo Molnar8018c272006-12-29 16:50:01 -08002816int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2817{
2818 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002819 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002820
Ingo Molnar8018c272006-12-29 16:50:01 -08002821 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002822}
2823
2824void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2825{
2826 ASSERT(vcpu);
2827
2828 destroy_kvm_mmu(vcpu);
2829 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002830 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002831}
2832
Avi Kivity90cb0522007-07-17 13:04:56 +03002833void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002834{
Avi Kivity4db35312007-11-21 15:28:32 +02002835 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002836
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002837 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002838 int i;
2839 u64 *pt;
2840
Sheng Yang291f26b2008-10-16 17:30:57 +08002841 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002842 continue;
2843
Avi Kivity4db35312007-11-21 15:28:32 +02002844 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002845 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2846 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002847 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002848 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002849 }
Avi Kivity171d5952008-08-27 16:40:51 +03002850 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002851}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002852
Avi Kivity90cb0522007-07-17 13:04:56 +03002853void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002854{
Avi Kivity4db35312007-11-21 15:28:32 +02002855 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002856
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002857 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002858 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002859 if (kvm_mmu_zap_page(kvm, sp))
2860 node = container_of(kvm->arch.active_mmu_pages.next,
2861 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002862 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002863
Avi Kivity90cb0522007-07-17 13:04:56 +03002864 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002865}
2866
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002867static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002868{
2869 struct kvm_mmu_page *page;
2870
2871 page = container_of(kvm->arch.active_mmu_pages.prev,
2872 struct kvm_mmu_page, link);
2873 kvm_mmu_zap_page(kvm, page);
2874}
2875
2876static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2877{
2878 struct kvm *kvm;
2879 struct kvm *kvm_freed = NULL;
2880 int cache_count = 0;
2881
2882 spin_lock(&kvm_lock);
2883
2884 list_for_each_entry(kvm, &vm_list, vm_list) {
2885 int npages;
2886
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002887 if (!down_read_trylock(&kvm->slots_lock))
2888 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002889 spin_lock(&kvm->mmu_lock);
2890 npages = kvm->arch.n_alloc_mmu_pages -
2891 kvm->arch.n_free_mmu_pages;
2892 cache_count += npages;
2893 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2894 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2895 cache_count--;
2896 kvm_freed = kvm;
2897 }
2898 nr_to_scan--;
2899
2900 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002901 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002902 }
2903 if (kvm_freed)
2904 list_move_tail(&kvm_freed->vm_list, &vm_list);
2905
2906 spin_unlock(&kvm_lock);
2907
2908 return cache_count;
2909}
2910
2911static struct shrinker mmu_shrinker = {
2912 .shrink = mmu_shrink,
2913 .seeks = DEFAULT_SEEKS * 10,
2914};
2915
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002916static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002917{
2918 if (pte_chain_cache)
2919 kmem_cache_destroy(pte_chain_cache);
2920 if (rmap_desc_cache)
2921 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002922 if (mmu_page_header_cache)
2923 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002924}
2925
Izik Eidus3ee16c82008-03-30 15:17:21 +03002926void kvm_mmu_module_exit(void)
2927{
2928 mmu_destroy_caches();
2929 unregister_shrinker(&mmu_shrinker);
2930}
2931
Avi Kivityb5a33a72007-04-15 16:31:09 +03002932int kvm_mmu_module_init(void)
2933{
2934 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2935 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002936 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002937 if (!pte_chain_cache)
2938 goto nomem;
2939 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2940 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002941 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002942 if (!rmap_desc_cache)
2943 goto nomem;
2944
Avi Kivityd3d25b02007-05-30 12:34:53 +03002945 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2946 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002947 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002948 if (!mmu_page_header_cache)
2949 goto nomem;
2950
Izik Eidus3ee16c82008-03-30 15:17:21 +03002951 register_shrinker(&mmu_shrinker);
2952
Avi Kivityb5a33a72007-04-15 16:31:09 +03002953 return 0;
2954
2955nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002956 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002957 return -ENOMEM;
2958}
2959
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002960/*
2961 * Caculate mmu pages needed for kvm.
2962 */
2963unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2964{
2965 int i;
2966 unsigned int nr_mmu_pages;
2967 unsigned int nr_pages = 0;
2968
2969 for (i = 0; i < kvm->nmemslots; i++)
2970 nr_pages += kvm->memslots[i].npages;
2971
2972 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2973 nr_mmu_pages = max(nr_mmu_pages,
2974 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2975
2976 return nr_mmu_pages;
2977}
2978
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002979static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2980 unsigned len)
2981{
2982 if (len > buffer->len)
2983 return NULL;
2984 return buffer->ptr;
2985}
2986
2987static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2988 unsigned len)
2989{
2990 void *ret;
2991
2992 ret = pv_mmu_peek_buffer(buffer, len);
2993 if (!ret)
2994 return ret;
2995 buffer->ptr += len;
2996 buffer->len -= len;
2997 buffer->processed += len;
2998 return ret;
2999}
3000
3001static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3002 gpa_t addr, gpa_t value)
3003{
3004 int bytes = 8;
3005 int r;
3006
3007 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3008 bytes = 4;
3009
3010 r = mmu_topup_memory_caches(vcpu);
3011 if (r)
3012 return r;
3013
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003014 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003015 return -EFAULT;
3016
3017 return 1;
3018}
3019
3020static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3021{
Avi Kivitya8cd0242009-05-24 22:15:25 +03003022 kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003023 return 1;
3024}
3025
3026static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3027{
3028 spin_lock(&vcpu->kvm->mmu_lock);
3029 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3030 spin_unlock(&vcpu->kvm->mmu_lock);
3031 return 1;
3032}
3033
3034static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3035 struct kvm_pv_mmu_op_buffer *buffer)
3036{
3037 struct kvm_mmu_op_header *header;
3038
3039 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3040 if (!header)
3041 return 0;
3042 switch (header->op) {
3043 case KVM_MMU_OP_WRITE_PTE: {
3044 struct kvm_mmu_op_write_pte *wpte;
3045
3046 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3047 if (!wpte)
3048 return 0;
3049 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3050 wpte->pte_val);
3051 }
3052 case KVM_MMU_OP_FLUSH_TLB: {
3053 struct kvm_mmu_op_flush_tlb *ftlb;
3054
3055 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3056 if (!ftlb)
3057 return 0;
3058 return kvm_pv_mmu_flush_tlb(vcpu);
3059 }
3060 case KVM_MMU_OP_RELEASE_PT: {
3061 struct kvm_mmu_op_release_pt *rpt;
3062
3063 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3064 if (!rpt)
3065 return 0;
3066 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3067 }
3068 default: return 0;
3069 }
3070}
3071
3072int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3073 gpa_t addr, unsigned long *ret)
3074{
3075 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003076 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003077
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003078 buffer->ptr = buffer->buf;
3079 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3080 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003081
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003082 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003083 if (r)
3084 goto out;
3085
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003086 while (buffer->len) {
3087 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003088 if (r < 0)
3089 goto out;
3090 if (r == 0)
3091 break;
3092 }
3093
3094 r = 1;
3095out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003096 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003097 return r;
3098}
3099
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003100int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3101{
3102 struct kvm_shadow_walk_iterator iterator;
3103 int nr_sptes = 0;
3104
3105 spin_lock(&vcpu->kvm->mmu_lock);
3106 for_each_shadow_entry(vcpu, addr, iterator) {
3107 sptes[iterator.level-1] = *iterator.sptep;
3108 nr_sptes++;
3109 if (!is_shadow_present_pte(*iterator.sptep))
3110 break;
3111 }
3112 spin_unlock(&vcpu->kvm->mmu_lock);
3113
3114 return nr_sptes;
3115}
3116EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3117
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003118#ifdef AUDIT
3119
3120static const char *audit_msg;
3121
3122static gva_t canonicalize(gva_t gva)
3123{
3124#ifdef CONFIG_X86_64
3125 gva = (long long)(gva << 16) >> 16;
3126#endif
3127 return gva;
3128}
3129
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003130
3131typedef void (*inspect_spte_fn) (struct kvm *kvm, struct kvm_mmu_page *sp,
3132 u64 *sptep);
3133
3134static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3135 inspect_spte_fn fn)
3136{
3137 int i;
3138
3139 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3140 u64 ent = sp->spt[i];
3141
3142 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003143 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003144 struct kvm_mmu_page *child;
3145 child = page_header(ent & PT64_BASE_ADDR_MASK);
3146 __mmu_spte_walk(kvm, child, fn);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003147 } else
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003148 fn(kvm, sp, &sp->spt[i]);
3149 }
3150 }
3151}
3152
3153static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3154{
3155 int i;
3156 struct kvm_mmu_page *sp;
3157
3158 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3159 return;
3160 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3161 hpa_t root = vcpu->arch.mmu.root_hpa;
3162 sp = page_header(root);
3163 __mmu_spte_walk(vcpu->kvm, sp, fn);
3164 return;
3165 }
3166 for (i = 0; i < 4; ++i) {
3167 hpa_t root = vcpu->arch.mmu.pae_root[i];
3168
3169 if (root && VALID_PAGE(root)) {
3170 root &= PT64_BASE_ADDR_MASK;
3171 sp = page_header(root);
3172 __mmu_spte_walk(vcpu->kvm, sp, fn);
3173 }
3174 }
3175 return;
3176}
3177
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003178static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3179 gva_t va, int level)
3180{
3181 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3182 int i;
3183 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3184
3185 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3186 u64 ent = pt[i];
3187
Avi Kivityc7addb92007-09-16 18:58:32 +02003188 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003189 continue;
3190
3191 va = canonicalize(va);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003192 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3193 audit_mappings_page(vcpu, ent, va, level - 1);
3194 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003195 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Jan Kiszka34382532009-04-25 12:43:21 +02003196 gfn_t gfn = gpa >> PAGE_SHIFT;
3197 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3198 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003199
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003200 if (is_error_pfn(pfn)) {
3201 kvm_release_pfn_clean(pfn);
3202 continue;
3203 }
3204
Avi Kivityc7addb92007-09-16 18:58:32 +02003205 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003206 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003207 printk(KERN_ERR "xx audit error: (%s) levels %d"
3208 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003209 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003210 va, gpa, hpa, ent,
3211 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003212 else if (ent == shadow_notrap_nonpresent_pte
3213 && !is_error_hpa(hpa))
3214 printk(KERN_ERR "audit: (%s) notrap shadow,"
3215 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003216 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003217
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003218 }
3219 }
3220}
3221
3222static void audit_mappings(struct kvm_vcpu *vcpu)
3223{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003224 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003225
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003226 if (vcpu->arch.mmu.root_level == 4)
3227 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003228 else
3229 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003230 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003231 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003232 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003233 i << 30,
3234 2);
3235}
3236
3237static int count_rmaps(struct kvm_vcpu *vcpu)
3238{
3239 int nmaps = 0;
3240 int i, j, k;
3241
3242 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3243 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
3244 struct kvm_rmap_desc *d;
3245
3246 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003247 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003248
Izik Eidus290fc382007-09-27 14:11:22 +02003249 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003250 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003251 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003252 ++nmaps;
3253 continue;
3254 }
Izik Eidus290fc382007-09-27 14:11:22 +02003255 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003256 while (d) {
3257 for (k = 0; k < RMAP_EXT; ++k)
Avi Kivityd555c332009-06-10 14:24:23 +03003258 if (d->sptes[k])
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003259 ++nmaps;
3260 else
3261 break;
3262 d = d->more;
3263 }
3264 }
3265 }
3266 return nmaps;
3267}
3268
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003269void inspect_spte_has_rmap(struct kvm *kvm, struct kvm_mmu_page *sp, u64 *sptep)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003270{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003271 unsigned long *rmapp;
3272 struct kvm_mmu_page *rev_sp;
3273 gfn_t gfn;
3274
3275 if (*sptep & PT_WRITABLE_MASK) {
3276 rev_sp = page_header(__pa(sptep));
3277 gfn = rev_sp->gfns[sptep - rev_sp->spt];
3278
3279 if (!gfn_to_memslot(kvm, gfn)) {
3280 if (!printk_ratelimit())
3281 return;
3282 printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3283 audit_msg, gfn);
3284 printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
3285 audit_msg, sptep - rev_sp->spt,
3286 rev_sp->gfn);
3287 dump_stack();
3288 return;
3289 }
3290
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003291 rmapp = gfn_to_rmap(kvm, rev_sp->gfns[sptep - rev_sp->spt],
3292 is_large_pte(*sptep));
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003293 if (!*rmapp) {
3294 if (!printk_ratelimit())
3295 return;
3296 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3297 audit_msg, *sptep);
3298 dump_stack();
3299 }
3300 }
3301
3302}
3303
3304void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3305{
3306 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3307}
3308
3309static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3310{
Avi Kivity4db35312007-11-21 15:28:32 +02003311 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003312 int i;
3313
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003314 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003315 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003316
Avi Kivity4db35312007-11-21 15:28:32 +02003317 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003318 continue;
3319
3320 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3321 u64 ent = pt[i];
3322
3323 if (!(ent & PT_PRESENT_MASK))
3324 continue;
3325 if (!(ent & PT_WRITABLE_MASK))
3326 continue;
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003327 inspect_spte_has_rmap(vcpu->kvm, sp, &pt[i]);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003328 }
3329 }
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003330 return;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003331}
3332
3333static void audit_rmap(struct kvm_vcpu *vcpu)
3334{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003335 check_writable_mappings_rmap(vcpu);
3336 count_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003337}
3338
3339static void audit_write_protection(struct kvm_vcpu *vcpu)
3340{
Avi Kivity4db35312007-11-21 15:28:32 +02003341 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003342 struct kvm_memory_slot *slot;
3343 unsigned long *rmapp;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003344 u64 *spte;
Izik Eidus290fc382007-09-27 14:11:22 +02003345 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003346
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003347 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02003348 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003349 continue;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003350 if (sp->unsync)
3351 continue;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003352
Avi Kivity4db35312007-11-21 15:28:32 +02003353 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003354 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003355 rmapp = &slot->rmap[gfn - slot->base_gfn];
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003356
3357 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3358 while (spte) {
3359 if (*spte & PT_WRITABLE_MASK)
3360 printk(KERN_ERR "%s: (%s) shadow page has "
3361 "writable mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003362 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003363 sp->role.word);
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003364 spte = rmap_next(vcpu->kvm, rmapp, spte);
3365 }
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003366 }
3367}
3368
3369static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3370{
3371 int olddbg = dbg;
3372
3373 dbg = 0;
3374 audit_msg = msg;
3375 audit_rmap(vcpu);
3376 audit_write_protection(vcpu);
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003377 if (strcmp("pre pte write", audit_msg) != 0)
3378 audit_mappings(vcpu);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003379 audit_writable_sptes_have_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003380 dbg = olddbg;
3381}
3382
3383#endif