blob: 15afa1e1eaf9f42820694a971a5c692fd3b34b28 [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.
Nicolas Kaiser9611c182010-10-06 14:23:22 +020010 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080011 *
12 * Authors:
13 * Yaniv Kamay <yaniv@qumranet.com>
14 * Avi Kivity <avi@qumranet.com>
15 *
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
18 *
19 */
Avi Kivity6aa8b732006-12-10 02:21:36 -080020
Gleb Natapovaf585b92010-10-14 11:22:46 +020021#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080022#include "mmu.h"
Avi Kivity836a1b32010-01-21 15:31:49 +020023#include "x86.h"
Avi Kivity6de4f3a2009-05-31 22:58:47 +030024#include "kvm_cache_regs.h"
Gleb Natapovaf585b92010-10-14 11:22:46 +020025#include "x86.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080026
Avi Kivityedf88412007-12-16 11:02:48 +020027#include <linux/kvm_host.h>
Avi Kivitye4956062007-06-28 14:15:57 -040028#include <linux/types.h>
29#include <linux/string.h>
30#include <linux/mm.h>
31#include <linux/highmem.h>
32#include <linux/module.h>
Izik Eidus448353c2007-11-26 14:08:14 +020033#include <linux/swap.h>
Marcelo Tosatti05da4552008-02-23 11:44:30 -030034#include <linux/hugetlb.h>
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050035#include <linux/compiler.h>
Marcelo Tosattibc6678a2009-12-23 14:35:21 -020036#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Huang Yingbf998152010-05-31 14:28:19 +080038#include <linux/uaccess.h>
Avi Kivitye4956062007-06-28 14:15:57 -040039
40#include <asm/page.h>
41#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020042#include <asm/io.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020043#include <asm/vmx.h>
Avi Kivitye4956062007-06-28 14:15:57 -040044
Joerg Roedel18552672008-02-07 13:47:41 +010045/*
46 * When setting this variable to true it enables Two-Dimensional-Paging
47 * where the hardware walks 2 page tables:
48 * 1. the guest-virtual to guest-physical
49 * 2. while doing 1. it walks guest-physical to host-physical
50 * If the hardware supports that we don't need to do shadow paging.
51 */
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050052bool tdp_enabled = false;
Joerg Roedel18552672008-02-07 13:47:41 +010053
Xiao Guangrong8b1fe172010-08-30 18:22:53 +080054enum {
55 AUDIT_PRE_PAGE_FAULT,
56 AUDIT_POST_PAGE_FAULT,
57 AUDIT_PRE_PTE_WRITE,
Xiao Guangrong69030742010-09-27 18:09:29 +080058 AUDIT_POST_PTE_WRITE,
59 AUDIT_PRE_SYNC,
60 AUDIT_POST_SYNC
Xiao Guangrong8b1fe172010-08-30 18:22:53 +080061};
62
63char *audit_point_name[] = {
64 "pre page fault",
65 "post page fault",
66 "pre pte write",
Xiao Guangrong69030742010-09-27 18:09:29 +080067 "post pte write",
68 "pre sync",
69 "post sync"
Xiao Guangrong8b1fe172010-08-30 18:22:53 +080070};
71
Avi Kivity37a7d8b2007-01-05 16:36:56 -080072#undef MMU_DEBUG
73
Avi Kivity37a7d8b2007-01-05 16:36:56 -080074#ifdef MMU_DEBUG
75
76#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
77#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
78
79#else
80
81#define pgprintk(x...) do { } while (0)
82#define rmap_printk(x...) do { } while (0)
83
84#endif
85
Xiao Guangrong8b1fe172010-08-30 18:22:53 +080086#ifdef MMU_DEBUG
Avi Kivity6ada8cc2008-06-22 16:45:24 +030087static int dbg = 0;
88module_param(dbg, bool, 0644);
Avi Kivity37a7d8b2007-01-05 16:36:56 -080089#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080090
Marcelo Tosatti582801a2008-09-23 13:18:41 -030091static int oos_shadow = 1;
92module_param(oos_shadow, bool, 0644);
93
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080094#ifndef MMU_DEBUG
95#define ASSERT(x) do { } while (0)
96#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080097#define ASSERT(x) \
98 if (!(x)) { \
99 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
100 __FILE__, __LINE__, #x); \
101 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800102#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800103
Xiao Guangrong957ed9e2010-08-22 19:12:48 +0800104#define PTE_PREFETCH_NUM 8
105
Avi Kivity6aa8b732006-12-10 02:21:36 -0800106#define PT_FIRST_AVAIL_BITS_SHIFT 9
107#define PT64_SECOND_AVAIL_BITS_SHIFT 52
108
Avi Kivity6aa8b732006-12-10 02:21:36 -0800109#define PT64_LEVEL_BITS 9
110
111#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400112 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800113
Avi Kivity6aa8b732006-12-10 02:21:36 -0800114#define PT64_INDEX(address, level)\
115 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
116
117
118#define PT32_LEVEL_BITS 10
119
120#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400121 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800122
Joerg Roedele04da982009-07-27 16:30:45 +0200123#define PT32_LVL_OFFSET_MASK(level) \
124 (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
125 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800126
127#define PT32_INDEX(address, level)\
128 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
129
130
Avi Kivity27aba762007-03-09 13:04:31 +0200131#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800132#define PT64_DIR_BASE_ADDR_MASK \
133 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200134#define PT64_LVL_ADDR_MASK(level) \
135 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
136 * PT64_LEVEL_BITS))) - 1))
137#define PT64_LVL_OFFSET_MASK(level) \
138 (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
139 * PT64_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800140
141#define PT32_BASE_ADDR_MASK PAGE_MASK
142#define PT32_DIR_BASE_ADDR_MASK \
143 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200144#define PT32_LVL_ADDR_MASK(level) \
145 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
146 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800147
Avi Kivity79539ce2007-11-21 02:06:21 +0200148#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
149 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800150
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800151#define PTE_LIST_EXT 4
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800152
Avi Kivityfe135d22007-12-09 16:15:46 +0200153#define ACC_EXEC_MASK 1
154#define ACC_WRITE_MASK PT_WRITABLE_MASK
155#define ACC_USER_MASK PT_USER_MASK
156#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
157
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200158#include <trace/events/kvm.h>
159
Avi Kivity07420172009-07-06 12:21:32 +0300160#define CREATE_TRACE_POINTS
161#include "mmutrace.h"
162
Izik Eidus14032832009-09-23 21:47:17 +0300163#define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
164
Avi Kivity135f8c22008-08-21 17:49:56 +0300165#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
166
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800167struct pte_list_desc {
168 u64 *sptes[PTE_LIST_EXT];
169 struct pte_list_desc *more;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800170};
171
Avi Kivity2d111232008-12-25 14:39:47 +0200172struct kvm_shadow_walk_iterator {
173 u64 addr;
174 hpa_t shadow_addr;
175 int level;
176 u64 *sptep;
177 unsigned index;
178};
179
180#define for_each_shadow_entry(_vcpu, _addr, _walker) \
181 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
182 shadow_walk_okay(&(_walker)); \
183 shadow_walk_next(&(_walker)))
184
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800185static struct kmem_cache *pte_list_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300186static struct kmem_cache *mmu_page_header_cache;
Dave Hansen45221ab2010-08-19 18:11:37 -0700187static struct percpu_counter kvm_total_used_mmu_pages;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300188
Avi Kivityc7addb92007-09-16 18:58:32 +0200189static u64 __read_mostly shadow_trap_nonpresent_pte;
190static u64 __read_mostly shadow_notrap_nonpresent_pte;
Sheng Yang7b523452008-04-25 21:13:50 +0800191static u64 __read_mostly shadow_nx_mask;
192static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
193static u64 __read_mostly shadow_user_mask;
194static u64 __read_mostly shadow_accessed_mask;
195static u64 __read_mostly shadow_dirty_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200196
Dong, Eddie82725b22009-03-30 16:21:08 +0800197static inline u64 rsvd_bits(int s, int e)
198{
199 return ((1ULL << (e - s + 1)) - 1) << s;
200}
201
Avi Kivityc7addb92007-09-16 18:58:32 +0200202void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
203{
204 shadow_trap_nonpresent_pte = trap_pte;
205 shadow_notrap_nonpresent_pte = notrap_pte;
206}
207EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
208
Sheng Yang7b523452008-04-25 21:13:50 +0800209void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang4b12f0d2009-04-27 20:35:42 +0800210 u64 dirty_mask, u64 nx_mask, u64 x_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800211{
212 shadow_user_mask = user_mask;
213 shadow_accessed_mask = accessed_mask;
214 shadow_dirty_mask = dirty_mask;
215 shadow_nx_mask = nx_mask;
216 shadow_x_mask = x_mask;
217}
218EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
219
Avi Kivity3dbe1412010-05-12 11:48:18 +0300220static bool is_write_protection(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800221{
Avi Kivity4d4ec082009-12-29 18:07:30 +0200222 return kvm_read_cr0_bits(vcpu, X86_CR0_WP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800223}
224
225static int is_cpuid_PSE36(void)
226{
227 return 1;
228}
229
Avi Kivity73b10872007-01-26 00:56:41 -0800230static int is_nx(struct kvm_vcpu *vcpu)
231{
Avi Kivityf6801df2010-01-21 15:31:50 +0200232 return vcpu->arch.efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800233}
234
Avi Kivityc7addb92007-09-16 18:58:32 +0200235static int is_shadow_present_pte(u64 pte)
236{
Avi Kivityc7addb92007-09-16 18:58:32 +0200237 return pte != shadow_trap_nonpresent_pte
238 && pte != shadow_notrap_nonpresent_pte;
239}
240
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300241static int is_large_pte(u64 pte)
242{
243 return pte & PT_PAGE_SIZE_MASK;
244}
245
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900246static int is_writable_pte(unsigned long pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800247{
248 return pte & PT_WRITABLE_MASK;
249}
250
Avi Kivity43a37952009-06-10 14:12:05 +0300251static int is_dirty_gpte(unsigned long pte)
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200252{
Avi Kivity439e2182009-06-10 12:56:54 +0300253 return pte & PT_DIRTY_MASK;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200254}
255
Avi Kivity43a37952009-06-10 14:12:05 +0300256static int is_rmap_spte(u64 pte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800257{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200258 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800259}
260
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300261static int is_last_spte(u64 pte, int level)
262{
263 if (level == PT_PAGE_TABLE_LEVEL)
264 return 1;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200265 if (is_large_pte(pte))
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300266 return 1;
267 return 0;
268}
269
Anthony Liguori35149e22008-04-02 14:46:56 -0500270static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200271{
Anthony Liguori35149e22008-04-02 14:46:56 -0500272 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200273}
274
Avi Kivityda928522007-11-21 13:54:47 +0200275static gfn_t pse36_gfn_delta(u32 gpte)
276{
277 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
278
279 return (gpte & PT32_DIR_PSE36_MASK) << shift;
280}
281
Avi Kivityd555c332009-06-10 14:24:23 +0300282static void __set_spte(u64 *sptep, u64 spte)
Avi Kivitye663ee62007-05-31 15:46:04 +0300283{
H. Peter Anvin7645e432010-08-06 12:18:11 -0700284 set_64bit(sptep, spte);
Avi Kivitye663ee62007-05-31 15:46:04 +0300285}
286
Avi Kivitya9221dd2010-06-06 14:48:06 +0300287static u64 __xchg_spte(u64 *sptep, u64 new_spte)
288{
289#ifdef CONFIG_X86_64
290 return xchg(sptep, new_spte);
291#else
292 u64 old_spte;
293
294 do {
295 old_spte = *sptep;
296 } while (cmpxchg64(sptep, old_spte, new_spte) != old_spte);
297
298 return old_spte;
299#endif
300}
301
Xiao Guangrong8672b722010-08-02 16:14:04 +0800302static bool spte_has_volatile_bits(u64 spte)
303{
304 if (!shadow_accessed_mask)
305 return false;
306
307 if (!is_shadow_present_pte(spte))
308 return false;
309
Xiao Guangrong41327792010-08-02 16:15:08 +0800310 if ((spte & shadow_accessed_mask) &&
311 (!is_writable_pte(spte) || (spte & shadow_dirty_mask)))
Xiao Guangrong8672b722010-08-02 16:14:04 +0800312 return false;
313
314 return true;
315}
316
Xiao Guangrong41327792010-08-02 16:15:08 +0800317static bool spte_is_bit_cleared(u64 old_spte, u64 new_spte, u64 bit_mask)
318{
319 return (old_spte & bit_mask) && !(new_spte & bit_mask);
320}
321
Avi Kivityb79b93f2010-06-06 15:46:44 +0300322static void update_spte(u64 *sptep, u64 new_spte)
323{
Xiao Guangrong41327792010-08-02 16:15:08 +0800324 u64 mask, old_spte = *sptep;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300325
Xiao Guangrong41327792010-08-02 16:15:08 +0800326 WARN_ON(!is_rmap_spte(new_spte));
327
328 new_spte |= old_spte & shadow_dirty_mask;
329
330 mask = shadow_accessed_mask;
331 if (is_writable_pte(old_spte))
332 mask |= shadow_dirty_mask;
333
334 if (!spte_has_volatile_bits(old_spte) || (new_spte & mask) == mask)
Avi Kivityb79b93f2010-06-06 15:46:44 +0300335 __set_spte(sptep, new_spte);
Xiao Guangrong41327792010-08-02 16:15:08 +0800336 else
Avi Kivityb79b93f2010-06-06 15:46:44 +0300337 old_spte = __xchg_spte(sptep, new_spte);
Xiao Guangrong41327792010-08-02 16:15:08 +0800338
339 if (!shadow_accessed_mask)
340 return;
341
342 if (spte_is_bit_cleared(old_spte, new_spte, shadow_accessed_mask))
343 kvm_set_pfn_accessed(spte_to_pfn(old_spte));
344 if (spte_is_bit_cleared(old_spte, new_spte, shadow_dirty_mask))
345 kvm_set_pfn_dirty(spte_to_pfn(old_spte));
Avi Kivityb79b93f2010-06-06 15:46:44 +0300346}
347
Avi Kivitye2dec932007-01-05 16:36:54 -0800348static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300349 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800350{
351 void *obj;
352
353 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800354 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800355 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300356 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800357 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800358 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800359 cache->objects[cache->nobjs++] = obj;
360 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800361 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800362}
363
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800364static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc,
365 struct kmem_cache *cache)
Avi Kivity714b93d2007-01-05 16:36:53 -0800366{
367 while (mc->nobjs)
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800368 kmem_cache_free(cache, mc->objects[--mc->nobjs]);
Avi Kivity714b93d2007-01-05 16:36:53 -0800369}
370
Avi Kivityc1158e62007-07-20 08:18:27 +0300371static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300372 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300373{
Xiao Guangrong842f22e2011-03-04 19:01:10 +0800374 void *page;
Avi Kivityc1158e62007-07-20 08:18:27 +0300375
376 if (cache->nobjs >= min)
377 return 0;
378 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Xiao Guangrong842f22e2011-03-04 19:01:10 +0800379 page = (void *)__get_free_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300380 if (!page)
381 return -ENOMEM;
Xiao Guangrong842f22e2011-03-04 19:01:10 +0800382 cache->objects[cache->nobjs++] = page;
Avi Kivityc1158e62007-07-20 08:18:27 +0300383 }
384 return 0;
385}
386
387static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
388{
389 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300390 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300391}
392
Avi Kivity8c438502007-04-16 11:53:17 +0300393static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
394{
395 int r;
396
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800397 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
Xiao Guangrong67052b32011-05-15 23:27:08 +0800398 pte_list_desc_cache, 8 + PTE_PREFETCH_NUM);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300399 if (r)
400 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800401 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300402 if (r)
403 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800404 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300405 mmu_page_header_cache, 4);
406out:
Avi Kivity8c438502007-04-16 11:53:17 +0300407 return r;
408}
409
Avi Kivity714b93d2007-01-05 16:36:53 -0800410static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
411{
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800412 mmu_free_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
413 pte_list_desc_cache);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800414 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800415 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache,
416 mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800417}
418
419static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
420 size_t size)
421{
422 void *p;
423
424 BUG_ON(!mc->nobjs);
425 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800426 return p;
427}
428
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800429static struct pte_list_desc *mmu_alloc_pte_list_desc(struct kvm_vcpu *vcpu)
Avi Kivity714b93d2007-01-05 16:36:53 -0800430{
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800431 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_list_desc_cache,
432 sizeof(struct pte_list_desc));
Avi Kivity714b93d2007-01-05 16:36:53 -0800433}
434
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800435static void mmu_free_pte_list_desc(struct pte_list_desc *pte_list_desc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800436{
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800437 kmem_cache_free(pte_list_desc_cache, pte_list_desc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800438}
439
Lai Jiangshan2032a932010-05-26 16:49:59 +0800440static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
441{
442 if (!sp->role.direct)
443 return sp->gfns[index];
444
445 return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS));
446}
447
448static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn)
449{
450 if (sp->role.direct)
451 BUG_ON(gfn != kvm_mmu_page_get_gfn(sp, index));
452 else
453 sp->gfns[index] = gfn;
454}
455
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800456/*
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900457 * Return the pointer to the large page information for a given gfn,
458 * handling slots that are not large page aligned.
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300459 */
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900460static struct kvm_lpage_info *lpage_info_slot(gfn_t gfn,
461 struct kvm_memory_slot *slot,
462 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300463{
464 unsigned long idx;
465
Joerg Roedel82855412010-07-01 16:00:11 +0200466 idx = (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
467 (slot->base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900468 return &slot->lpage_info[level - 2][idx];
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300469}
470
471static void account_shadowed(struct kvm *kvm, gfn_t gfn)
472{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200473 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900474 struct kvm_lpage_info *linfo;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200475 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300476
Avi Kivitya1f4d392010-06-21 11:44:20 +0300477 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200478 for (i = PT_DIRECTORY_LEVEL;
479 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900480 linfo = lpage_info_slot(gfn, slot, i);
481 linfo->write_count += 1;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200482 }
Xiao Guangrong332b2072011-05-15 23:20:27 +0800483 kvm->arch.indirect_shadow_pages++;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300484}
485
486static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
487{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200488 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900489 struct kvm_lpage_info *linfo;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200490 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300491
Avi Kivitya1f4d392010-06-21 11:44:20 +0300492 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200493 for (i = PT_DIRECTORY_LEVEL;
494 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900495 linfo = lpage_info_slot(gfn, slot, i);
496 linfo->write_count -= 1;
497 WARN_ON(linfo->write_count < 0);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200498 }
Xiao Guangrong332b2072011-05-15 23:20:27 +0800499 kvm->arch.indirect_shadow_pages--;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300500}
501
Joerg Roedeld25797b2009-07-27 16:30:43 +0200502static int has_wrprotected_page(struct kvm *kvm,
503 gfn_t gfn,
504 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300505{
Izik Eidus28430992008-10-03 17:40:32 +0300506 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900507 struct kvm_lpage_info *linfo;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300508
Avi Kivitya1f4d392010-06-21 11:44:20 +0300509 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300510 if (slot) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900511 linfo = lpage_info_slot(gfn, slot, level);
512 return linfo->write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300513 }
514
515 return 1;
516}
517
Joerg Roedeld25797b2009-07-27 16:30:43 +0200518static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300519{
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100520 unsigned long page_size;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200521 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300522
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100523 page_size = kvm_host_page_size(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300524
Joerg Roedeld25797b2009-07-27 16:30:43 +0200525 for (i = PT_PAGE_TABLE_LEVEL;
526 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
527 if (page_size >= KVM_HPAGE_SIZE(i))
528 ret = i;
529 else
530 break;
531 }
532
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300533 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300534}
535
Xiao Guangrong5d163b12011-03-09 15:43:00 +0800536static struct kvm_memory_slot *
537gfn_to_memslot_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t gfn,
538 bool no_dirty_log)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300539{
540 struct kvm_memory_slot *slot;
Xiao Guangrong5d163b12011-03-09 15:43:00 +0800541
542 slot = gfn_to_memslot(vcpu->kvm, gfn);
543 if (!slot || slot->flags & KVM_MEMSLOT_INVALID ||
544 (no_dirty_log && slot->dirty_bitmap))
545 slot = NULL;
546
547 return slot;
548}
549
550static bool mapping_level_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t large_gfn)
551{
Stevea0a8eab2011-06-17 10:25:39 +0800552 return !gfn_to_memslot_dirty_bitmap(vcpu, large_gfn, true);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800553}
554
555static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
556{
557 int host_level, level, max_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300558
Joerg Roedeld25797b2009-07-27 16:30:43 +0200559 host_level = host_mapping_level(vcpu->kvm, large_gfn);
560
561 if (host_level == PT_PAGE_TABLE_LEVEL)
562 return host_level;
563
Sheng Yang878403b2010-01-05 19:02:29 +0800564 max_level = kvm_x86_ops->get_lpage_level() < host_level ?
565 kvm_x86_ops->get_lpage_level() : host_level;
566
567 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200568 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
569 break;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200570
571 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300572}
573
574/*
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800575 * Pte mapping structures:
576 *
577 * If pte_list bit zero is zero, then pte_list point to the spte.
578 *
579 * If pte_list bit zero is one, (then pte_list & ~1) points to a struct
580 * pte_list_desc containing more mappings.
581 *
582 * Returns the number of pte entries before the spte was added or zero if
583 * the spte was not added.
584 *
585 */
586static int pte_list_add(struct kvm_vcpu *vcpu, u64 *spte,
587 unsigned long *pte_list)
588{
589 struct pte_list_desc *desc;
590 int i, count = 0;
591
592 if (!*pte_list) {
593 rmap_printk("pte_list_add: %p %llx 0->1\n", spte, *spte);
594 *pte_list = (unsigned long)spte;
595 } else if (!(*pte_list & 1)) {
596 rmap_printk("pte_list_add: %p %llx 1->many\n", spte, *spte);
597 desc = mmu_alloc_pte_list_desc(vcpu);
598 desc->sptes[0] = (u64 *)*pte_list;
599 desc->sptes[1] = spte;
600 *pte_list = (unsigned long)desc | 1;
601 ++count;
602 } else {
603 rmap_printk("pte_list_add: %p %llx many->many\n", spte, *spte);
604 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
605 while (desc->sptes[PTE_LIST_EXT-1] && desc->more) {
606 desc = desc->more;
607 count += PTE_LIST_EXT;
608 }
609 if (desc->sptes[PTE_LIST_EXT-1]) {
610 desc->more = mmu_alloc_pte_list_desc(vcpu);
611 desc = desc->more;
612 }
613 for (i = 0; desc->sptes[i]; ++i)
614 ++count;
615 desc->sptes[i] = spte;
616 }
617 return count;
618}
619
620static u64 *pte_list_next(unsigned long *pte_list, u64 *spte)
621{
622 struct pte_list_desc *desc;
623 u64 *prev_spte;
624 int i;
625
626 if (!*pte_list)
627 return NULL;
628 else if (!(*pte_list & 1)) {
629 if (!spte)
630 return (u64 *)*pte_list;
631 return NULL;
632 }
633 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
634 prev_spte = NULL;
635 while (desc) {
636 for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i) {
637 if (prev_spte == spte)
638 return desc->sptes[i];
639 prev_spte = desc->sptes[i];
640 }
641 desc = desc->more;
642 }
643 return NULL;
644}
645
646static void
647pte_list_desc_remove_entry(unsigned long *pte_list, struct pte_list_desc *desc,
648 int i, struct pte_list_desc *prev_desc)
649{
650 int j;
651
652 for (j = PTE_LIST_EXT - 1; !desc->sptes[j] && j > i; --j)
653 ;
654 desc->sptes[i] = desc->sptes[j];
655 desc->sptes[j] = NULL;
656 if (j != 0)
657 return;
658 if (!prev_desc && !desc->more)
659 *pte_list = (unsigned long)desc->sptes[0];
660 else
661 if (prev_desc)
662 prev_desc->more = desc->more;
663 else
664 *pte_list = (unsigned long)desc->more | 1;
665 mmu_free_pte_list_desc(desc);
666}
667
668static void pte_list_remove(u64 *spte, unsigned long *pte_list)
669{
670 struct pte_list_desc *desc;
671 struct pte_list_desc *prev_desc;
672 int i;
673
674 if (!*pte_list) {
675 printk(KERN_ERR "pte_list_remove: %p 0->BUG\n", spte);
676 BUG();
677 } else if (!(*pte_list & 1)) {
678 rmap_printk("pte_list_remove: %p 1->0\n", spte);
679 if ((u64 *)*pte_list != spte) {
680 printk(KERN_ERR "pte_list_remove: %p 1->BUG\n", spte);
681 BUG();
682 }
683 *pte_list = 0;
684 } else {
685 rmap_printk("pte_list_remove: %p many->many\n", spte);
686 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
687 prev_desc = NULL;
688 while (desc) {
689 for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i)
690 if (desc->sptes[i] == spte) {
691 pte_list_desc_remove_entry(pte_list,
692 desc, i,
693 prev_desc);
694 return;
695 }
696 prev_desc = desc;
697 desc = desc->more;
698 }
699 pr_err("pte_list_remove: %p many->many\n", spte);
700 BUG();
701 }
702}
703
Xiao Guangrong67052b32011-05-15 23:27:08 +0800704typedef void (*pte_list_walk_fn) (u64 *spte);
705static void pte_list_walk(unsigned long *pte_list, pte_list_walk_fn fn)
706{
707 struct pte_list_desc *desc;
708 int i;
709
710 if (!*pte_list)
711 return;
712
713 if (!(*pte_list & 1))
714 return fn((u64 *)*pte_list);
715
716 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
717 while (desc) {
718 for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i)
719 fn(desc->sptes[i]);
720 desc = desc->more;
721 }
722}
723
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800724/*
Izik Eidus290fc382007-09-27 14:11:22 +0200725 * Take gfn and return the reverse mapping to it.
Izik Eidus290fc382007-09-27 14:11:22 +0200726 */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200727static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
Izik Eidus290fc382007-09-27 14:11:22 +0200728{
729 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900730 struct kvm_lpage_info *linfo;
Izik Eidus290fc382007-09-27 14:11:22 +0200731
732 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200733 if (likely(level == PT_PAGE_TABLE_LEVEL))
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300734 return &slot->rmap[gfn - slot->base_gfn];
735
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900736 linfo = lpage_info_slot(gfn, slot, level);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300737
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900738 return &linfo->rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200739}
740
Joerg Roedel44ad9942009-07-27 16:30:42 +0200741static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800742{
Avi Kivity4db35312007-11-21 15:28:32 +0200743 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +0200744 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800745
Avi Kivity43a37952009-06-10 14:12:05 +0300746 if (!is_rmap_spte(*spte))
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800747 return 0;
748
Avi Kivity4db35312007-11-21 15:28:32 +0200749 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800750 kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200751 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800752 return pte_list_add(vcpu, spte, rmapp);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800753}
754
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800755static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800756{
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800757 return pte_list_next(rmapp, spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800758}
759
Izik Eidus290fc382007-09-27 14:11:22 +0200760static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800761{
Avi Kivity4db35312007-11-21 15:28:32 +0200762 struct kvm_mmu_page *sp;
Lai Jiangshan2032a932010-05-26 16:49:59 +0800763 gfn_t gfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200764 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800765
Avi Kivity4db35312007-11-21 15:28:32 +0200766 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800767 gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt);
768 rmapp = gfn_to_rmap(kvm, gfn, sp->role.level);
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800769 pte_list_remove(spte, rmapp);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800770}
771
Marcelo Tosattieb45fda2010-10-25 11:58:22 -0200772static int set_spte_track_bits(u64 *sptep, u64 new_spte)
Avi Kivitybe38d272010-06-06 14:31:27 +0300773{
Avi Kivityce061862010-06-06 14:38:12 +0300774 pfn_t pfn;
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800775 u64 old_spte = *sptep;
Avi Kivityce061862010-06-06 14:38:12 +0300776
Xiao Guangrong8672b722010-08-02 16:14:04 +0800777 if (!spte_has_volatile_bits(old_spte))
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800778 __set_spte(sptep, new_spte);
Xiao Guangrong8672b722010-08-02 16:14:04 +0800779 else
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800780 old_spte = __xchg_spte(sptep, new_spte);
781
Avi Kivitya9221dd2010-06-06 14:48:06 +0300782 if (!is_rmap_spte(old_spte))
Marcelo Tosattieb45fda2010-10-25 11:58:22 -0200783 return 0;
Xiao Guangrong8672b722010-08-02 16:14:04 +0800784
Avi Kivitya9221dd2010-06-06 14:48:06 +0300785 pfn = spte_to_pfn(old_spte);
Xiao Guangrongdaa3db62010-07-16 11:23:04 +0800786 if (!shadow_accessed_mask || old_spte & shadow_accessed_mask)
Avi Kivityce061862010-06-06 14:38:12 +0300787 kvm_set_pfn_accessed(pfn);
Xiao Guangrong41327792010-08-02 16:15:08 +0800788 if (!shadow_dirty_mask || (old_spte & shadow_dirty_mask))
Avi Kivityce061862010-06-06 14:38:12 +0300789 kvm_set_pfn_dirty(pfn);
Marcelo Tosattieb45fda2010-10-25 11:58:22 -0200790 return 1;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800791}
792
793static void drop_spte(struct kvm *kvm, u64 *sptep, u64 new_spte)
794{
Marcelo Tosattieb45fda2010-10-25 11:58:22 -0200795 if (set_spte_track_bits(sptep, new_spte))
796 rmap_remove(kvm, sptep);
Avi Kivitybe38d272010-06-06 14:31:27 +0300797}
798
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200799static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200800{
Izik Eidus290fc382007-09-27 14:11:22 +0200801 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800802 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200803 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800804
Joerg Roedel44ad9942009-07-27 16:30:42 +0200805 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
Avi Kivity374cbac2007-01-05 16:36:43 -0800806
Izik Eidus98348e92007-10-16 14:42:30 +0200807 spte = rmap_next(kvm, rmapp, NULL);
808 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800809 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800810 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800811 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900812 if (is_writable_pte(*spte)) {
Avi Kivityb79b93f2010-06-06 15:46:44 +0300813 update_spte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800814 write_protected = 1;
815 }
Izik Eidus9647c142007-10-16 14:43:46 +0200816 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800817 }
Izik Eidus855149a2008-03-20 18:17:24 +0200818
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300819 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200820 for (i = PT_DIRECTORY_LEVEL;
821 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
822 rmapp = gfn_to_rmap(kvm, gfn, i);
823 spte = rmap_next(kvm, rmapp, NULL);
824 while (spte) {
825 BUG_ON(!spte);
826 BUG_ON(!(*spte & PT_PRESENT_MASK));
827 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
828 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900829 if (is_writable_pte(*spte)) {
Avi Kivitybe38d272010-06-06 14:31:27 +0300830 drop_spte(kvm, spte,
831 shadow_trap_nonpresent_pte);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200832 --kvm->stat.lpages;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200833 spte = NULL;
834 write_protected = 1;
835 }
836 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300837 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300838 }
839
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200840 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800841}
842
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000843static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
844 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200845{
846 u64 *spte;
847 int need_tlb_flush = 0;
848
849 while ((spte = rmap_next(kvm, rmapp, NULL))) {
850 BUG_ON(!(*spte & PT_PRESENT_MASK));
851 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
Avi Kivitybe38d272010-06-06 14:31:27 +0300852 drop_spte(kvm, spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200853 need_tlb_flush = 1;
854 }
855 return need_tlb_flush;
856}
857
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000858static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
859 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +0300860{
861 int need_flush = 0;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800862 u64 *spte, new_spte;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300863 pte_t *ptep = (pte_t *)data;
864 pfn_t new_pfn;
865
866 WARN_ON(pte_huge(*ptep));
867 new_pfn = pte_pfn(*ptep);
868 spte = rmap_next(kvm, rmapp, NULL);
869 while (spte) {
870 BUG_ON(!is_shadow_present_pte(*spte));
871 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
872 need_flush = 1;
873 if (pte_write(*ptep)) {
Avi Kivitybe38d272010-06-06 14:31:27 +0300874 drop_spte(kvm, spte, shadow_trap_nonpresent_pte);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300875 spte = rmap_next(kvm, rmapp, NULL);
876 } else {
877 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
878 new_spte |= (u64)new_pfn << PAGE_SHIFT;
879
880 new_spte &= ~PT_WRITABLE_MASK;
881 new_spte &= ~SPTE_HOST_WRITEABLE;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300882 new_spte &= ~shadow_accessed_mask;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800883 set_spte_track_bits(spte, new_spte);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300884 spte = rmap_next(kvm, rmapp, spte);
885 }
886 }
887 if (need_flush)
888 kvm_flush_remote_tlbs(kvm);
889
890 return 0;
891}
892
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000893static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
894 unsigned long data,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300895 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000896 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200897{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200898 int i, j;
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200899 int ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200900 int retval = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200901 struct kvm_memslots *slots;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200902
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800903 slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200904
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200905 for (i = 0; i < slots->nmemslots; i++) {
906 struct kvm_memory_slot *memslot = &slots->memslots[i];
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200907 unsigned long start = memslot->userspace_addr;
908 unsigned long end;
909
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200910 end = start + (memslot->npages << PAGE_SHIFT);
911 if (hva >= start && hva < end) {
912 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900913 gfn_t gfn = memslot->base_gfn + gfn_offset;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200914
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200915 ret = handler(kvm, &memslot->rmap[gfn_offset], data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200916
917 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900918 struct kvm_lpage_info *linfo;
Andrea Arcangeli6e3e2432010-07-16 11:52:55 +0200919
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900920 linfo = lpage_info_slot(gfn, memslot,
921 PT_DIRECTORY_LEVEL + j);
922 ret |= handler(kvm, &linfo->rmap_pde, data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200923 }
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200924 trace_kvm_age_page(hva, memslot, ret);
925 retval |= ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200926 }
927 }
928
929 return retval;
930}
931
932int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
933{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300934 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200935}
936
Izik Eidus3da0dd42009-09-23 21:47:18 +0300937void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
938{
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000939 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300940}
941
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000942static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
943 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200944{
945 u64 *spte;
946 int young = 0;
947
Rik van Riel6316e1c2010-02-03 16:11:03 -0500948 /*
949 * Emulate the accessed bit for EPT, by checking if this page has
950 * an EPT mapping, and clearing it if it does. On the next access,
951 * a new EPT mapping will be established.
952 * This has some overhead, but not as much as the cost of swapping
953 * out actively used pages or breaking up actively used hugepages.
954 */
Sheng Yang534e38b2008-09-08 15:12:30 +0800955 if (!shadow_accessed_mask)
Rik van Riel6316e1c2010-02-03 16:11:03 -0500956 return kvm_unmap_rmapp(kvm, rmapp, data);
Sheng Yang534e38b2008-09-08 15:12:30 +0800957
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200958 spte = rmap_next(kvm, rmapp, NULL);
959 while (spte) {
960 int _young;
961 u64 _spte = *spte;
962 BUG_ON(!(_spte & PT_PRESENT_MASK));
963 _young = _spte & PT_ACCESSED_MASK;
964 if (_young) {
965 young = 1;
966 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
967 }
968 spte = rmap_next(kvm, rmapp, spte);
969 }
970 return young;
971}
972
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800973static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
974 unsigned long data)
975{
976 u64 *spte;
977 int young = 0;
978
979 /*
980 * If there's no access bit in the secondary pte set by the
981 * hardware it's up to gup-fast/gup to set the access bit in
982 * the primary pte or in the page structure.
983 */
984 if (!shadow_accessed_mask)
985 goto out;
986
987 spte = rmap_next(kvm, rmapp, NULL);
988 while (spte) {
989 u64 _spte = *spte;
990 BUG_ON(!(_spte & PT_PRESENT_MASK));
991 young = _spte & PT_ACCESSED_MASK;
992 if (young) {
993 young = 1;
994 break;
995 }
996 spte = rmap_next(kvm, rmapp, spte);
997 }
998out:
999 return young;
1000}
1001
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001002#define RMAP_RECYCLE_THRESHOLD 1000
1003
Joerg Roedel852e3c12009-07-27 16:30:44 +02001004static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001005{
1006 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001007 struct kvm_mmu_page *sp;
1008
1009 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001010
Joerg Roedel852e3c12009-07-27 16:30:44 +02001011 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001012
Izik Eidus3da0dd42009-09-23 21:47:18 +03001013 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001014 kvm_flush_remote_tlbs(vcpu->kvm);
1015}
1016
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001017int kvm_age_hva(struct kvm *kvm, unsigned long hva)
1018{
Izik Eidus3da0dd42009-09-23 21:47:18 +03001019 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001020}
1021
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001022int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
1023{
1024 return kvm_handle_hva(kvm, hva, 0, kvm_test_age_rmapp);
1025}
1026
Yaozu Dongd6c69ee2007-04-25 14:17:25 +08001027#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +03001028static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001029{
Avi Kivity139bdb22007-01-05 16:36:50 -08001030 u64 *pos;
1031 u64 *end;
1032
Avi Kivity47ad8e62007-05-06 15:50:58 +03001033 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +03001034 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001035 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -08001036 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001037 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -08001038 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001039 return 1;
1040}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +08001041#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001042
Dave Hansen45221ab2010-08-19 18:11:37 -07001043/*
1044 * This value is the sum of all of the kvm instances's
1045 * kvm->arch.n_used_mmu_pages values. We need a global,
1046 * aggregate version in order to make the slab shrinker
1047 * faster
1048 */
1049static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, int nr)
1050{
1051 kvm->arch.n_used_mmu_pages += nr;
1052 percpu_counter_add(&kvm_total_used_mmu_pages, nr);
1053}
1054
Avi Kivity4db35312007-11-21 15:28:32 +02001055static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -08001056{
Avi Kivity4db35312007-11-21 15:28:32 +02001057 ASSERT(is_empty_shadow_page(sp->spt));
Xiao Guangrong77758342010-06-04 21:53:54 +08001058 hlist_del(&sp->hash_link);
Avi Kivity4db35312007-11-21 15:28:32 +02001059 list_del(&sp->link);
Xiao Guangrong842f22e2011-03-04 19:01:10 +08001060 free_page((unsigned long)sp->spt);
Lai Jiangshan2032a932010-05-26 16:49:59 +08001061 if (!sp->role.direct)
Xiao Guangrong842f22e2011-03-04 19:01:10 +08001062 free_page((unsigned long)sp->gfns);
Xiao Guangronge8ad9a72010-05-13 10:06:02 +08001063 kmem_cache_free(mmu_page_header_cache, sp);
Dave Hansen45221ab2010-08-19 18:11:37 -07001064 kvm_mod_used_mmu_pages(kvm, -1);
Avi Kivity260746c2007-01-05 16:36:49 -08001065}
1066
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001067static unsigned kvm_page_table_hashfn(gfn_t gfn)
1068{
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001069 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001070}
1071
Xiao Guangrong67052b32011-05-15 23:27:08 +08001072static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
1073 struct kvm_mmu_page *sp, u64 *parent_pte)
1074{
1075 if (!parent_pte)
1076 return;
1077
1078 pte_list_add(vcpu, parent_pte, &sp->parent_ptes);
1079}
1080
1081static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
1082 u64 *parent_pte)
1083{
1084 pte_list_remove(parent_pte, &sp->parent_ptes);
1085}
1086
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001087static void drop_parent_pte(struct kvm_mmu_page *sp,
1088 u64 *parent_pte)
1089{
1090 mmu_page_remove_parent_pte(sp, parent_pte);
1091 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
1092}
1093
Avi Kivity25c0de22007-01-05 16:36:42 -08001094static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
Lai Jiangshan2032a932010-05-26 16:49:59 +08001095 u64 *parent_pte, int direct)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001096{
Avi Kivity4db35312007-11-21 15:28:32 +02001097 struct kvm_mmu_page *sp;
Xiao Guangrong67052b32011-05-15 23:27:08 +08001098 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache,
1099 sizeof *sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001100 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Lai Jiangshan2032a932010-05-26 16:49:59 +08001101 if (!direct)
1102 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache,
1103 PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +02001104 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001105 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Sheng Yang291f26b2008-10-16 17:30:57 +08001106 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Xiao Guangrong67052b32011-05-15 23:27:08 +08001107 sp->parent_ptes = 0;
1108 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Dave Hansen45221ab2010-08-19 18:11:37 -07001109 kvm_mod_used_mmu_pages(vcpu->kvm, +1);
Avi Kivity4db35312007-11-21 15:28:32 +02001110 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001111}
1112
Xiao Guangrong67052b32011-05-15 23:27:08 +08001113static void mark_unsync(u64 *spte);
Xiao Guangrong6b184932010-04-16 21:29:17 +08001114static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001115{
Xiao Guangrong67052b32011-05-15 23:27:08 +08001116 pte_list_walk(&sp->parent_ptes, mark_unsync);
Xiao Guangrong1047df12010-06-11 21:35:15 +08001117}
1118
Xiao Guangrong67052b32011-05-15 23:27:08 +08001119static void mark_unsync(u64 *spte)
Xiao Guangrong1047df12010-06-11 21:35:15 +08001120{
Xiao Guangrong67052b32011-05-15 23:27:08 +08001121 struct kvm_mmu_page *sp;
Xiao Guangrong1047df12010-06-11 21:35:15 +08001122 unsigned int index;
1123
Xiao Guangrong67052b32011-05-15 23:27:08 +08001124 sp = page_header(__pa(spte));
Xiao Guangrong1047df12010-06-11 21:35:15 +08001125 index = spte - sp->spt;
1126 if (__test_and_set_bit(index, sp->unsync_child_bitmap))
1127 return;
1128 if (sp->unsync_children++)
1129 return;
1130 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001131}
1132
Avi Kivityd761a502008-05-29 14:55:03 +03001133static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1134 struct kvm_mmu_page *sp)
1135{
1136 int i;
1137
1138 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1139 sp->spt[i] = shadow_trap_nonpresent_pte;
1140}
1141
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001142static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001143 struct kvm_mmu_page *sp)
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001144{
1145 return 1;
1146}
1147
Marcelo Tosattia7052892008-09-23 13:18:35 -03001148static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1149{
1150}
1151
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08001152static void nonpaging_update_pte(struct kvm_vcpu *vcpu,
1153 struct kvm_mmu_page *sp, u64 *spte,
Xiao Guangrong7c562522011-03-28 10:29:27 +08001154 const void *pte)
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08001155{
1156 WARN_ON(1);
1157}
1158
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001159#define KVM_PAGE_ARRAY_NR 16
1160
1161struct kvm_mmu_pages {
1162 struct mmu_page_and_offset {
1163 struct kvm_mmu_page *sp;
1164 unsigned int idx;
1165 } page[KVM_PAGE_ARRAY_NR];
1166 unsigned int nr;
1167};
1168
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001169#define for_each_unsync_children(bitmap, idx) \
1170 for (idx = find_first_bit(bitmap, 512); \
1171 idx < 512; \
1172 idx = find_next_bit(bitmap, 512, idx+1))
1173
Hannes Edercded19f2009-02-21 02:19:13 +01001174static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1175 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001176{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001177 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001178
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001179 if (sp->unsync)
1180 for (i=0; i < pvec->nr; i++)
1181 if (pvec->page[i].sp == sp)
1182 return 0;
1183
1184 pvec->page[pvec->nr].sp = sp;
1185 pvec->page[pvec->nr].idx = idx;
1186 pvec->nr++;
1187 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1188}
1189
1190static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1191 struct kvm_mmu_pages *pvec)
1192{
1193 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001194
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001195 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001196 struct kvm_mmu_page *child;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001197 u64 ent = sp->spt[i];
1198
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001199 if (!is_shadow_present_pte(ent) || is_large_pte(ent))
1200 goto clear_child_bitmap;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001201
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001202 child = page_header(ent & PT64_BASE_ADDR_MASK);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001203
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001204 if (child->unsync_children) {
1205 if (mmu_pages_add(pvec, child, i))
1206 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001207
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001208 ret = __mmu_unsync_walk(child, pvec);
1209 if (!ret)
1210 goto clear_child_bitmap;
1211 else if (ret > 0)
1212 nr_unsync_leaf += ret;
1213 else
1214 return ret;
1215 } else if (child->unsync) {
1216 nr_unsync_leaf++;
1217 if (mmu_pages_add(pvec, child, i))
1218 return -ENOSPC;
1219 } else
1220 goto clear_child_bitmap;
1221
1222 continue;
1223
1224clear_child_bitmap:
1225 __clear_bit(i, sp->unsync_child_bitmap);
1226 sp->unsync_children--;
1227 WARN_ON((int)sp->unsync_children < 0);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001228 }
1229
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001230
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001231 return nr_unsync_leaf;
1232}
1233
1234static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1235 struct kvm_mmu_pages *pvec)
1236{
1237 if (!sp->unsync_children)
1238 return 0;
1239
1240 mmu_pages_add(pvec, sp, 0);
1241 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001242}
1243
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001244static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1245{
1246 WARN_ON(!sp->unsync);
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001247 trace_kvm_mmu_sync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001248 sp->unsync = 0;
1249 --kvm->stat.mmu_unsync;
1250}
1251
Xiao Guangrong77758342010-06-04 21:53:54 +08001252static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1253 struct list_head *invalid_list);
1254static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1255 struct list_head *invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001256
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001257#define for_each_gfn_sp(kvm, sp, gfn, pos) \
1258 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001259 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1260 if ((sp)->gfn != (gfn)) {} else
1261
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001262#define for_each_gfn_indirect_valid_sp(kvm, sp, gfn, pos) \
1263 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001264 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1265 if ((sp)->gfn != (gfn) || (sp)->role.direct || \
1266 (sp)->role.invalid) {} else
1267
Xiao Guangrongf918b442010-06-11 21:30:36 +08001268/* @sp->gfn should be write-protected at the call site */
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001269static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001270 struct list_head *invalid_list, bool clear_unsync)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001271{
Avi Kivity5b7e0102010-04-14 19:20:03 +03001272 if (sp->role.cr4_pae != !!is_pae(vcpu)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001273 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001274 return 1;
1275 }
1276
Xiao Guangrongf918b442010-06-11 21:30:36 +08001277 if (clear_unsync)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001278 kvm_unlink_unsync_page(vcpu->kvm, sp);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001279
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001280 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001281 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001282 return 1;
1283 }
1284
1285 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001286 return 0;
1287}
1288
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001289static int kvm_sync_page_transient(struct kvm_vcpu *vcpu,
1290 struct kvm_mmu_page *sp)
1291{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001292 LIST_HEAD(invalid_list);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001293 int ret;
1294
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001295 ret = __kvm_sync_page(vcpu, sp, &invalid_list, false);
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001296 if (ret)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001297 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
1298
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001299 return ret;
1300}
1301
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001302static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1303 struct list_head *invalid_list)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001304{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001305 return __kvm_sync_page(vcpu, sp, invalid_list, true);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001306}
1307
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001308/* @gfn should be write-protected at the call site */
1309static void kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1310{
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001311 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001312 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001313 LIST_HEAD(invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001314 bool flush = false;
1315
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001316 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001317 if (!s->unsync)
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001318 continue;
1319
1320 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001321 kvm_unlink_unsync_page(vcpu->kvm, s);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001322 if ((s->role.cr4_pae != !!is_pae(vcpu)) ||
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001323 (vcpu->arch.mmu.sync_page(vcpu, s))) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001324 kvm_mmu_prepare_zap_page(vcpu->kvm, s, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001325 continue;
1326 }
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001327 flush = true;
1328 }
1329
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001330 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001331 if (flush)
1332 kvm_mmu_flush_tlb(vcpu);
1333}
1334
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001335struct mmu_page_path {
1336 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1337 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001338};
1339
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001340#define for_each_sp(pvec, sp, parents, i) \
1341 for (i = mmu_pages_next(&pvec, &parents, -1), \
1342 sp = pvec.page[i].sp; \
1343 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1344 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001345
Hannes Edercded19f2009-02-21 02:19:13 +01001346static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1347 struct mmu_page_path *parents,
1348 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001349{
1350 int n;
1351
1352 for (n = i+1; n < pvec->nr; n++) {
1353 struct kvm_mmu_page *sp = pvec->page[n].sp;
1354
1355 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1356 parents->idx[0] = pvec->page[n].idx;
1357 return n;
1358 }
1359
1360 parents->parent[sp->role.level-2] = sp;
1361 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1362 }
1363
1364 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001365}
1366
Hannes Edercded19f2009-02-21 02:19:13 +01001367static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001368{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001369 struct kvm_mmu_page *sp;
1370 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001371
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001372 do {
1373 unsigned int idx = parents->idx[level];
1374
1375 sp = parents->parent[level];
1376 if (!sp)
1377 return;
1378
1379 --sp->unsync_children;
1380 WARN_ON((int)sp->unsync_children < 0);
1381 __clear_bit(idx, sp->unsync_child_bitmap);
1382 level++;
1383 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1384}
1385
1386static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1387 struct mmu_page_path *parents,
1388 struct kvm_mmu_pages *pvec)
1389{
1390 parents->parent[parent->role.level-1] = NULL;
1391 pvec->nr = 0;
1392}
1393
1394static void mmu_sync_children(struct kvm_vcpu *vcpu,
1395 struct kvm_mmu_page *parent)
1396{
1397 int i;
1398 struct kvm_mmu_page *sp;
1399 struct mmu_page_path parents;
1400 struct kvm_mmu_pages pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001401 LIST_HEAD(invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001402
1403 kvm_mmu_pages_init(parent, &parents, &pages);
1404 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001405 int protected = 0;
1406
1407 for_each_sp(pages, sp, parents, i)
1408 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1409
1410 if (protected)
1411 kvm_flush_remote_tlbs(vcpu->kvm);
1412
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001413 for_each_sp(pages, sp, parents, i) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001414 kvm_sync_page(vcpu, sp, &invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001415 mmu_pages_clear_parents(&parents);
1416 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001417 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001418 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001419 kvm_mmu_pages_init(parent, &parents, &pages);
1420 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001421}
1422
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001423static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1424 gfn_t gfn,
1425 gva_t gaddr,
1426 unsigned level,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001427 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001428 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001429 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001430{
1431 union kvm_mmu_page_role role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001432 unsigned quadrant;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001433 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001434 struct hlist_node *node;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001435 bool need_sync = false;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001436
Avi Kivitya770f6f2008-12-21 19:20:09 +02001437 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001438 role.level = level;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001439 role.direct = direct;
Avi Kivity84b0c8c2010-03-14 10:16:40 +02001440 if (role.direct)
Avi Kivity5b7e0102010-04-14 19:20:03 +03001441 role.cr4_pae = 0;
Avi Kivity41074d02007-12-09 17:00:02 +02001442 role.access = access;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02001443 if (!vcpu->arch.mmu.direct_map
1444 && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001445 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1446 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1447 role.quadrant = quadrant;
1448 }
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001449 for_each_gfn_sp(vcpu->kvm, sp, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001450 if (!need_sync && sp->unsync)
1451 need_sync = true;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001452
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001453 if (sp->role.word != role.word)
1454 continue;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001455
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001456 if (sp->unsync && kvm_sync_page_transient(vcpu, sp))
1457 break;
Xiao Guangronge02aa902010-05-15 18:52:34 +08001458
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001459 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1460 if (sp->unsync_children) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001461 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001462 kvm_mmu_mark_parents_unsync(sp);
1463 } else if (sp->unsync)
1464 kvm_mmu_mark_parents_unsync(sp);
Xiao Guangronge02aa902010-05-15 18:52:34 +08001465
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001466 trace_kvm_mmu_get_page(sp, false);
1467 return sp;
1468 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001469 ++vcpu->kvm->stat.mmu_cache_miss;
Lai Jiangshan2032a932010-05-26 16:49:59 +08001470 sp = kvm_mmu_alloc_page(vcpu, parent_pte, direct);
Avi Kivity4db35312007-11-21 15:28:32 +02001471 if (!sp)
1472 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001473 sp->gfn = gfn;
1474 sp->role = role;
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001475 hlist_add_head(&sp->hash_link,
1476 &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001477 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001478 if (rmap_write_protect(vcpu->kvm, gfn))
1479 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001480 if (level > PT_PAGE_TABLE_LEVEL && need_sync)
1481 kvm_sync_pages(vcpu, gfn);
1482
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001483 account_shadowed(vcpu->kvm, gfn);
1484 }
Avi Kivity131d8272008-05-29 14:56:28 +03001485 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1486 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1487 else
1488 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001489 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001490 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001491}
1492
Avi Kivity2d111232008-12-25 14:39:47 +02001493static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1494 struct kvm_vcpu *vcpu, u64 addr)
1495{
1496 iterator->addr = addr;
1497 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1498 iterator->level = vcpu->arch.mmu.shadow_root_level;
Joerg Roedel81407ca2010-09-10 17:31:00 +02001499
1500 if (iterator->level == PT64_ROOT_LEVEL &&
1501 vcpu->arch.mmu.root_level < PT64_ROOT_LEVEL &&
1502 !vcpu->arch.mmu.direct_map)
1503 --iterator->level;
1504
Avi Kivity2d111232008-12-25 14:39:47 +02001505 if (iterator->level == PT32E_ROOT_LEVEL) {
1506 iterator->shadow_addr
1507 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1508 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1509 --iterator->level;
1510 if (!iterator->shadow_addr)
1511 iterator->level = 0;
1512 }
1513}
1514
1515static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1516{
1517 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1518 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001519
1520 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1521 if (is_large_pte(*iterator->sptep))
1522 return false;
1523
Avi Kivity2d111232008-12-25 14:39:47 +02001524 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1525 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1526 return true;
1527}
1528
1529static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1530{
1531 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1532 --iterator->level;
1533}
1534
Avi Kivity32ef26a2010-07-13 14:27:04 +03001535static void link_shadow_page(u64 *sptep, struct kvm_mmu_page *sp)
1536{
1537 u64 spte;
1538
1539 spte = __pa(sp->spt)
1540 | PT_PRESENT_MASK | PT_ACCESSED_MASK
1541 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity121eee92010-07-13 14:27:05 +03001542 __set_spte(sptep, spte);
Avi Kivity32ef26a2010-07-13 14:27:04 +03001543}
1544
Avi Kivitya3aa51c2010-07-13 14:27:06 +03001545static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep)
1546{
1547 if (is_large_pte(*sptep)) {
1548 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
1549 kvm_flush_remote_tlbs(vcpu->kvm);
1550 }
1551}
1552
Avi Kivitya357bd22010-07-13 14:27:07 +03001553static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1554 unsigned direct_access)
1555{
1556 if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
1557 struct kvm_mmu_page *child;
1558
1559 /*
1560 * For the direct sp, if the guest pte's dirty bit
1561 * changed form clean to dirty, it will corrupt the
1562 * sp's access: allow writable in the read-only sp,
1563 * so we should update the spte at this point to get
1564 * a new sp with the correct access.
1565 */
1566 child = page_header(*sptep & PT64_BASE_ADDR_MASK);
1567 if (child->role.access == direct_access)
1568 return;
1569
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001570 drop_parent_pte(child, sptep);
Avi Kivitya357bd22010-07-13 14:27:07 +03001571 kvm_flush_remote_tlbs(vcpu->kvm);
1572 }
1573}
1574
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08001575static void mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,
1576 u64 *spte)
1577{
1578 u64 pte;
1579 struct kvm_mmu_page *child;
1580
1581 pte = *spte;
1582 if (is_shadow_present_pte(pte)) {
1583 if (is_last_spte(pte, sp->role.level))
1584 drop_spte(kvm, spte, shadow_trap_nonpresent_pte);
1585 else {
1586 child = page_header(pte & PT64_BASE_ADDR_MASK);
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001587 drop_parent_pte(child, spte);
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08001588 }
1589 }
1590 __set_spte(spte, shadow_trap_nonpresent_pte);
1591 if (is_large_pte(pte))
1592 --kvm->stat.lpages;
1593}
1594
Avi Kivity90cb0522007-07-17 13:04:56 +03001595static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001596 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001597{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001598 unsigned i;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001599
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08001600 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1601 mmu_page_zap_pte(kvm, sp, sp->spt + i);
Avi Kivitya4360362007-01-05 16:36:45 -08001602}
1603
Avi Kivity4db35312007-11-21 15:28:32 +02001604static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001605{
Avi Kivity4db35312007-11-21 15:28:32 +02001606 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001607}
1608
Avi Kivity12b7d282007-09-23 14:10:49 +02001609static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1610{
1611 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001612 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001613
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001614 kvm_for_each_vcpu(i, vcpu, kvm)
1615 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001616}
1617
Avi Kivity31aa2b42008-07-11 17:59:46 +03001618static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001619{
1620 u64 *parent_pte;
1621
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001622 while ((parent_pte = pte_list_next(&sp->parent_ptes, NULL)))
1623 drop_parent_pte(sp, parent_pte);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001624}
1625
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001626static int mmu_zap_unsync_children(struct kvm *kvm,
Xiao Guangrong77758342010-06-04 21:53:54 +08001627 struct kvm_mmu_page *parent,
1628 struct list_head *invalid_list)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001629{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001630 int i, zapped = 0;
1631 struct mmu_page_path parents;
1632 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001633
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001634 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001635 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001636
1637 kvm_mmu_pages_init(parent, &parents, &pages);
1638 while (mmu_unsync_walk(parent, &pages)) {
1639 struct kvm_mmu_page *sp;
1640
1641 for_each_sp(pages, sp, parents, i) {
Xiao Guangrong77758342010-06-04 21:53:54 +08001642 kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001643 mmu_pages_clear_parents(&parents);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001644 zapped++;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001645 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001646 kvm_mmu_pages_init(parent, &parents, &pages);
1647 }
1648
1649 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001650}
1651
Xiao Guangrong77758342010-06-04 21:53:54 +08001652static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1653 struct list_head *invalid_list)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001654{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001655 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001656
Xiao Guangrong77758342010-06-04 21:53:54 +08001657 trace_kvm_mmu_prepare_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001658 ++kvm->stat.mmu_shadow_zapped;
Xiao Guangrong77758342010-06-04 21:53:54 +08001659 ret = mmu_zap_unsync_children(kvm, sp, invalid_list);
Avi Kivity4db35312007-11-21 15:28:32 +02001660 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001661 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001662 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001663 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001664 if (sp->unsync)
1665 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001666 if (!sp->root_count) {
Gui Jianfeng54a4f022010-05-05 09:03:49 +08001667 /* Count self */
1668 ret++;
Xiao Guangrong77758342010-06-04 21:53:54 +08001669 list_move(&sp->link, invalid_list);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001670 } else {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001671 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001672 kvm_reload_remote_mmus(kvm);
1673 }
Xiao Guangrong77758342010-06-04 21:53:54 +08001674
1675 sp->role.invalid = 1;
Avi Kivity12b7d282007-09-23 14:10:49 +02001676 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001677 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001678}
1679
Xiao Guangrong77758342010-06-04 21:53:54 +08001680static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1681 struct list_head *invalid_list)
1682{
1683 struct kvm_mmu_page *sp;
1684
1685 if (list_empty(invalid_list))
1686 return;
1687
1688 kvm_flush_remote_tlbs(kvm);
1689
1690 do {
1691 sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
1692 WARN_ON(!sp->role.invalid || sp->root_count);
1693 kvm_mmu_free_page(kvm, sp);
1694 } while (!list_empty(invalid_list));
1695
1696}
1697
Izik Eidus82ce2c92007-10-02 18:52:55 +02001698/*
1699 * Changing the number of mmu pages allocated to the vm
Dave Hansen49d5ca22010-08-19 18:11:28 -07001700 * Note: if goal_nr_mmu_pages is too small, you will get dead lock
Izik Eidus82ce2c92007-10-02 18:52:55 +02001701 */
Dave Hansen49d5ca22010-08-19 18:11:28 -07001702void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages)
Izik Eidus82ce2c92007-10-02 18:52:55 +02001703{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001704 LIST_HEAD(invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001705 /*
1706 * If we set the number of mmu pages to be smaller be than the
1707 * number of actived pages , we must to free some mmu pages before we
1708 * change the value
1709 */
1710
Dave Hansen49d5ca22010-08-19 18:11:28 -07001711 if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) {
1712 while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages &&
Xiao Guangrong77662e02010-04-16 16:34:42 +08001713 !list_empty(&kvm->arch.active_mmu_pages)) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001714 struct kvm_mmu_page *page;
1715
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001716 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001717 struct kvm_mmu_page, link);
Xiaotian Feng80b63fa2010-08-24 10:31:07 +08001718 kvm_mmu_prepare_zap_page(kvm, page, &invalid_list);
1719 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001720 }
Dave Hansen49d5ca22010-08-19 18:11:28 -07001721 goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001722 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02001723
Dave Hansen49d5ca22010-08-19 18:11:28 -07001724 kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001725}
1726
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001727static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001728{
Avi Kivity4db35312007-11-21 15:28:32 +02001729 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001730 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001731 LIST_HEAD(invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001732 int r;
1733
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08001734 pgprintk("%s: looking for gfn %llx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001735 r = 0;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001736
1737 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08001738 pgprintk("%s: gfn %llx role %x\n", __func__, gfn,
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001739 sp->role.word);
1740 r = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001741 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001742 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001743 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001744 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001745}
1746
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001747static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001748{
Avi Kivity4db35312007-11-21 15:28:32 +02001749 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001750 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001751 LIST_HEAD(invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001752
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001753 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08001754 pgprintk("%s: zap %llx %x\n",
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001755 __func__, gfn, sp->role.word);
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001756 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001757 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001758 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001759}
1760
Avi Kivity38c335f2007-11-21 14:20:22 +02001761static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001762{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001763 int slot = memslot_id(kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001764 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001765
Sheng Yang291f26b2008-10-16 17:30:57 +08001766 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001767}
1768
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001769static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1770{
1771 int i;
1772 u64 *pt = sp->spt;
1773
1774 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1775 return;
1776
1777 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1778 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001779 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001780 }
1781}
1782
Sheng Yang74be52e2008-10-09 16:01:56 +08001783/*
1784 * The function is based on mtrr_type_lookup() in
1785 * arch/x86/kernel/cpu/mtrr/generic.c
1786 */
1787static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1788 u64 start, u64 end)
1789{
1790 int i;
1791 u64 base, mask;
1792 u8 prev_match, curr_match;
1793 int num_var_ranges = KVM_NR_VAR_MTRR;
1794
1795 if (!mtrr_state->enabled)
1796 return 0xFF;
1797
1798 /* Make end inclusive end, instead of exclusive */
1799 end--;
1800
1801 /* Look in fixed ranges. Just return the type as per start */
1802 if (mtrr_state->have_fixed && (start < 0x100000)) {
1803 int idx;
1804
1805 if (start < 0x80000) {
1806 idx = 0;
1807 idx += (start >> 16);
1808 return mtrr_state->fixed_ranges[idx];
1809 } else if (start < 0xC0000) {
1810 idx = 1 * 8;
1811 idx += ((start - 0x80000) >> 14);
1812 return mtrr_state->fixed_ranges[idx];
1813 } else if (start < 0x1000000) {
1814 idx = 3 * 8;
1815 idx += ((start - 0xC0000) >> 12);
1816 return mtrr_state->fixed_ranges[idx];
1817 }
1818 }
1819
1820 /*
1821 * Look in variable ranges
1822 * Look of multiple ranges matching this address and pick type
1823 * as per MTRR precedence
1824 */
1825 if (!(mtrr_state->enabled & 2))
1826 return mtrr_state->def_type;
1827
1828 prev_match = 0xFF;
1829 for (i = 0; i < num_var_ranges; ++i) {
1830 unsigned short start_state, end_state;
1831
1832 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1833 continue;
1834
1835 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1836 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1837 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1838 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1839
1840 start_state = ((start & mask) == (base & mask));
1841 end_state = ((end & mask) == (base & mask));
1842 if (start_state != end_state)
1843 return 0xFE;
1844
1845 if ((start & mask) != (base & mask))
1846 continue;
1847
1848 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1849 if (prev_match == 0xFF) {
1850 prev_match = curr_match;
1851 continue;
1852 }
1853
1854 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1855 curr_match == MTRR_TYPE_UNCACHABLE)
1856 return MTRR_TYPE_UNCACHABLE;
1857
1858 if ((prev_match == MTRR_TYPE_WRBACK &&
1859 curr_match == MTRR_TYPE_WRTHROUGH) ||
1860 (prev_match == MTRR_TYPE_WRTHROUGH &&
1861 curr_match == MTRR_TYPE_WRBACK)) {
1862 prev_match = MTRR_TYPE_WRTHROUGH;
1863 curr_match = MTRR_TYPE_WRTHROUGH;
1864 }
1865
1866 if (prev_match != curr_match)
1867 return MTRR_TYPE_UNCACHABLE;
1868 }
1869
1870 if (prev_match != 0xFF)
1871 return prev_match;
1872
1873 return mtrr_state->def_type;
1874}
1875
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001876u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001877{
1878 u8 mtrr;
1879
1880 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1881 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1882 if (mtrr == 0xfe || mtrr == 0xff)
1883 mtrr = MTRR_TYPE_WRBACK;
1884 return mtrr;
1885}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001886EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001887
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001888static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001889{
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001890 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001891 ++vcpu->kvm->stat.mmu_unsync;
1892 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001893
Xiao Guangrong6b184932010-04-16 21:29:17 +08001894 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001895 mmu_convert_notrap(sp);
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001896}
1897
1898static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1899{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001900 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001901 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001902
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001903 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001904 if (s->unsync)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001905 continue;
1906 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1907 __kvm_unsync_page(vcpu, s);
1908 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001909}
1910
1911static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1912 bool can_unsync)
1913{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001914 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001915 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001916 bool need_unsync = false;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001917
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001918 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001919 if (!can_unsync)
1920 return 1;
1921
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001922 if (s->role.level != PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001923 return 1;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001924
1925 if (!need_unsync && !s->unsync) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001926 if (!oos_shadow)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001927 return 1;
1928 need_unsync = true;
1929 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001930 }
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001931 if (need_unsync)
1932 kvm_unsync_pages(vcpu, gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001933 return 0;
1934}
1935
Avi Kivityd555c332009-06-10 14:24:23 +03001936static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001937 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001938 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001939 gfn_t gfn, pfn_t pfn, bool speculative,
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08001940 bool can_unsync, bool host_writable)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001941{
Xiao Guangrongb330aa02010-11-19 17:02:35 +08001942 u64 spte, entry = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001943 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001944
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001945 /*
1946 * We don't set the accessed bit, since we sometimes want to see
1947 * whether the guest actually used the pte (in order to detect
1948 * demand paging).
1949 */
Marcelo Tosatti982c2562010-10-22 14:18:16 -02001950 spte = PT_PRESENT_MASK;
Avi Kivity947da532008-03-18 11:05:52 +02001951 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001952 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001953 if (!dirty)
1954 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001955 if (pte_access & ACC_EXEC_MASK)
1956 spte |= shadow_x_mask;
1957 else
1958 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001959 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001960 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001961 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001962 spte |= PT_PAGE_SIZE_MASK;
Avi Kivityb0bc3ee2010-09-13 16:45:28 +02001963 if (tdp_enabled)
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001964 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1965 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001966
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08001967 if (host_writable)
Izik Eidus14032832009-09-23 21:47:17 +03001968 spte |= SPTE_HOST_WRITEABLE;
Xiao Guangrongf8e453b2010-12-23 16:09:29 +08001969 else
1970 pte_access &= ~ACC_WRITE_MASK;
Izik Eidus14032832009-09-23 21:47:17 +03001971
Anthony Liguori35149e22008-04-02 14:46:56 -05001972 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001973
1974 if ((pte_access & ACC_WRITE_MASK)
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02001975 || (!vcpu->arch.mmu.direct_map && write_fault
1976 && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001977
Joerg Roedel852e3c12009-07-27 16:30:44 +02001978 if (level > PT_PAGE_TABLE_LEVEL &&
1979 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001980 ret = 1;
Avi Kivitybe38d272010-06-06 14:31:27 +03001981 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
1982 goto done;
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001983 }
1984
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001985 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001986
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02001987 if (!vcpu->arch.mmu.direct_map
1988 && !(pte_access & ACC_WRITE_MASK))
Avi Kivity69325a12010-05-27 14:35:58 +03001989 spte &= ~PT_USER_MASK;
1990
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001991 /*
1992 * Optimization: for pte sync, if spte was writable the hash
1993 * lookup is unnecessary (and expensive). Write protection
1994 * is responsibility of mmu_get_page / kvm_sync_page.
1995 * Same reasoning can be applied to dirty page accounting.
1996 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001997 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001998 goto set_pte;
1999
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002000 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08002001 pgprintk("%s: found shadow page for %llx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002002 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002003 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002004 pte_access &= ~ACC_WRITE_MASK;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002005 if (is_writable_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002006 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002007 }
2008 }
2009
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002010 if (pte_access & ACC_WRITE_MASK)
2011 mark_page_dirty(vcpu->kvm, gfn);
2012
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002013set_pte:
Avi Kivityb79b93f2010-06-06 15:46:44 +03002014 update_spte(sptep, spte);
Xiao Guangrongb330aa02010-11-19 17:02:35 +08002015 /*
2016 * If we overwrite a writable spte with a read-only one we
2017 * should flush remote TLBs. Otherwise rmap_write_protect
2018 * will find a read-only spte, even though the writable spte
2019 * might be cached on a CPU's TLB.
2020 */
2021 if (is_writable_pte(entry) && !is_writable_pte(*sptep))
2022 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivitybe38d272010-06-06 14:31:27 +03002023done:
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002024 return ret;
2025}
2026
Avi Kivityd555c332009-06-10 14:24:23 +03002027static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002028 unsigned pt_access, unsigned pte_access,
2029 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002030 int *ptwrite, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03002031 pfn_t pfn, bool speculative,
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08002032 bool host_writable)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002033{
2034 int was_rmapped = 0;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002035 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002036
2037 pgprintk("%s: spte %llx access %x write_fault %d"
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08002038 " user_fault %d gfn %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002039 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002040 write_fault, user_fault, gfn);
2041
Avi Kivityd555c332009-06-10 14:24:23 +03002042 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002043 /*
2044 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
2045 * the parent of the now unreachable PTE.
2046 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02002047 if (level > PT_PAGE_TABLE_LEVEL &&
2048 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002049 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03002050 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002051
2052 child = page_header(pte & PT64_BASE_ADDR_MASK);
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08002053 drop_parent_pte(child, sptep);
Marcelo Tosatti3be22642010-05-28 09:44:59 -03002054 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityd555c332009-06-10 14:24:23 +03002055 } else if (pfn != spte_to_pfn(*sptep)) {
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08002056 pgprintk("hfn old %llx new %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002057 spte_to_pfn(*sptep), pfn);
Avi Kivitybe38d272010-06-06 14:31:27 +03002058 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
Xiao Guangrong91546352010-06-30 16:04:06 +08002059 kvm_flush_remote_tlbs(vcpu->kvm);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01002060 } else
2061 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002062 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02002063
Avi Kivityd555c332009-06-10 14:24:23 +03002064 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Izik Eidus14032832009-09-23 21:47:17 +03002065 dirty, level, gfn, pfn, speculative, true,
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08002066 host_writable)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002067 if (write_fault)
2068 *ptwrite = 1;
Xiao Guangrong5304efd2010-06-08 20:05:57 +08002069 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03002070 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002071
Avi Kivityd555c332009-06-10 14:24:23 +03002072 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08002073 pgprintk("instantiating %s PTE (%s) at %llx (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002074 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02002075 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
2076 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03002077 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002078 ++vcpu->kvm->stat.lpages;
2079
Avi Kivityd555c332009-06-10 14:24:23 +03002080 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002081 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02002082 rmap_count = rmap_add(vcpu, sptep, gfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002083 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02002084 rmap_recycle(vcpu, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002085 }
Xiao Guangrong9ed55202010-07-16 11:25:17 +08002086 kvm_release_pfn_clean(pfn);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002087 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03002088 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002089 vcpu->arch.last_pte_gfn = gfn;
2090 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002091}
2092
Avi Kivity6aa8b732006-12-10 02:21:36 -08002093static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
2094{
2095}
2096
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002097static pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn,
2098 bool no_dirty_log)
2099{
2100 struct kvm_memory_slot *slot;
2101 unsigned long hva;
2102
Xiao Guangrong5d163b12011-03-09 15:43:00 +08002103 slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, no_dirty_log);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002104 if (!slot) {
2105 get_page(bad_page);
2106 return page_to_pfn(bad_page);
2107 }
2108
2109 hva = gfn_to_hva_memslot(slot, gfn);
2110
2111 return hva_to_pfn_atomic(vcpu->kvm, hva);
2112}
2113
2114static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,
2115 struct kvm_mmu_page *sp,
2116 u64 *start, u64 *end)
2117{
2118 struct page *pages[PTE_PREFETCH_NUM];
2119 unsigned access = sp->role.access;
2120 int i, ret;
2121 gfn_t gfn;
2122
2123 gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt);
Xiao Guangrong5d163b12011-03-09 15:43:00 +08002124 if (!gfn_to_memslot_dirty_bitmap(vcpu, gfn, access & ACC_WRITE_MASK))
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002125 return -1;
2126
2127 ret = gfn_to_page_many_atomic(vcpu->kvm, gfn, pages, end - start);
2128 if (ret <= 0)
2129 return -1;
2130
2131 for (i = 0; i < ret; i++, gfn++, start++)
2132 mmu_set_spte(vcpu, start, ACC_ALL,
2133 access, 0, 0, 1, NULL,
2134 sp->role.level, gfn,
2135 page_to_pfn(pages[i]), true, true);
2136
2137 return 0;
2138}
2139
2140static void __direct_pte_prefetch(struct kvm_vcpu *vcpu,
2141 struct kvm_mmu_page *sp, u64 *sptep)
2142{
2143 u64 *spte, *start = NULL;
2144 int i;
2145
2146 WARN_ON(!sp->role.direct);
2147
2148 i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
2149 spte = sp->spt + i;
2150
2151 for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
2152 if (*spte != shadow_trap_nonpresent_pte || spte == sptep) {
2153 if (!start)
2154 continue;
2155 if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0)
2156 break;
2157 start = NULL;
2158 } else if (!start)
2159 start = spte;
2160 }
2161}
2162
2163static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)
2164{
2165 struct kvm_mmu_page *sp;
2166
2167 /*
2168 * Since it's no accessed bit on EPT, it's no way to
2169 * distinguish between actually accessed translations
2170 * and prefetched, so disable pte prefetch if EPT is
2171 * enabled.
2172 */
2173 if (!shadow_accessed_mask)
2174 return;
2175
2176 sp = page_header(__pa(sptep));
2177 if (sp->role.level > PT_PAGE_TABLE_LEVEL)
2178 return;
2179
2180 __direct_pte_prefetch(vcpu, sp, sptep);
2181}
2182
Joerg Roedel4d9976b2008-02-07 13:47:42 +01002183static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002184 int map_writable, int level, gfn_t gfn, pfn_t pfn,
2185 bool prefault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002186{
Avi Kivity9f652d22008-12-25 14:54:25 +02002187 struct kvm_shadow_walk_iterator iterator;
2188 struct kvm_mmu_page *sp;
2189 int pt_write = 0;
2190 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002191
Avi Kivity9f652d22008-12-25 14:54:25 +02002192 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02002193 if (iterator.level == level) {
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002194 unsigned pte_access = ACC_ALL;
2195
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002196 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, pte_access,
Avi Kivity9f652d22008-12-25 14:54:25 +02002197 0, write, 1, &pt_write,
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002198 level, gfn, pfn, prefault, map_writable);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002199 direct_pte_prefetch(vcpu, iterator.sptep);
Avi Kivity9f652d22008-12-25 14:54:25 +02002200 ++vcpu->stat.pf_fixed;
2201 break;
2202 }
2203
2204 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
Lai Jiangshanc9fa0b32010-05-26 16:48:25 +08002205 u64 base_addr = iterator.addr;
2206
2207 base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
2208 pseudo_gfn = base_addr >> PAGE_SHIFT;
Avi Kivity9f652d22008-12-25 14:54:25 +02002209 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
2210 iterator.level - 1,
2211 1, ACC_ALL, iterator.sptep);
2212 if (!sp) {
2213 pgprintk("nonpaging_map: ENOMEM\n");
2214 kvm_release_pfn_clean(pfn);
2215 return -ENOMEM;
2216 }
2217
Avi Kivityd555c332009-06-10 14:24:23 +03002218 __set_spte(iterator.sptep,
2219 __pa(sp->spt)
2220 | PT_PRESENT_MASK | PT_WRITABLE_MASK
Xiao Guangrong33f91ed2010-09-27 18:05:00 +08002221 | shadow_user_mask | shadow_x_mask
2222 | shadow_accessed_mask);
Avi Kivity9f652d22008-12-25 14:54:25 +02002223 }
2224 }
2225 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002226}
2227
Huang Ying77db5cb2010-10-08 16:24:15 +08002228static void kvm_send_hwpoison_signal(unsigned long address, struct task_struct *tsk)
Huang Yingbf998152010-05-31 14:28:19 +08002229{
Huang Ying77db5cb2010-10-08 16:24:15 +08002230 siginfo_t info;
Huang Yingbf998152010-05-31 14:28:19 +08002231
Huang Ying77db5cb2010-10-08 16:24:15 +08002232 info.si_signo = SIGBUS;
2233 info.si_errno = 0;
2234 info.si_code = BUS_MCEERR_AR;
2235 info.si_addr = (void __user *)address;
2236 info.si_addr_lsb = PAGE_SHIFT;
2237
2238 send_sig_info(SIGBUS, &info, tsk);
Huang Yingbf998152010-05-31 14:28:19 +08002239}
2240
2241static int kvm_handle_bad_page(struct kvm *kvm, gfn_t gfn, pfn_t pfn)
2242{
2243 kvm_release_pfn_clean(pfn);
2244 if (is_hwpoison_pfn(pfn)) {
Huang Ying77db5cb2010-10-08 16:24:15 +08002245 kvm_send_hwpoison_signal(gfn_to_hva(kvm, gfn), current);
Huang Yingbf998152010-05-31 14:28:19 +08002246 return 0;
Gleb Natapovedba23e2010-07-07 20:16:45 +03002247 } else if (is_fault_pfn(pfn))
2248 return -EFAULT;
2249
Huang Yingbf998152010-05-31 14:28:19 +08002250 return 1;
2251}
2252
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002253static void transparent_hugepage_adjust(struct kvm_vcpu *vcpu,
2254 gfn_t *gfnp, pfn_t *pfnp, int *levelp)
2255{
2256 pfn_t pfn = *pfnp;
2257 gfn_t gfn = *gfnp;
2258 int level = *levelp;
2259
2260 /*
2261 * Check if it's a transparent hugepage. If this would be an
2262 * hugetlbfs page, level wouldn't be set to
2263 * PT_PAGE_TABLE_LEVEL and there would be no adjustment done
2264 * here.
2265 */
2266 if (!is_error_pfn(pfn) && !kvm_is_mmio_pfn(pfn) &&
2267 level == PT_PAGE_TABLE_LEVEL &&
2268 PageTransCompound(pfn_to_page(pfn)) &&
2269 !has_wrprotected_page(vcpu->kvm, gfn, PT_DIRECTORY_LEVEL)) {
2270 unsigned long mask;
2271 /*
2272 * mmu_notifier_retry was successful and we hold the
2273 * mmu_lock here, so the pmd can't become splitting
2274 * from under us, and in turn
2275 * __split_huge_page_refcount() can't run from under
2276 * us and we can safely transfer the refcount from
2277 * PG_tail to PG_head as we switch the pfn to tail to
2278 * head.
2279 */
2280 *levelp = level = PT_DIRECTORY_LEVEL;
2281 mask = KVM_PAGES_PER_HPAGE(level) - 1;
2282 VM_BUG_ON((gfn & mask) != (pfn & mask));
2283 if (pfn & mask) {
2284 gfn &= ~mask;
2285 *gfnp = gfn;
2286 kvm_release_pfn_clean(pfn);
2287 pfn &= ~mask;
2288 if (!get_page_unless_zero(pfn_to_page(pfn)))
2289 BUG();
2290 *pfnp = pfn;
2291 }
2292 }
2293}
2294
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002295static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
Xiao Guangrong060c2ab2010-11-12 14:49:11 +08002296 gva_t gva, pfn_t *pfn, bool write, bool *writable);
2297
2298static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002299 bool prefault)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002300{
2301 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002302 int level;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002303 int force_pt_level;
Anthony Liguori35149e22008-04-02 14:46:56 -05002304 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002305 unsigned long mmu_seq;
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002306 bool map_writable;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002307
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002308 force_pt_level = mapping_level_dirty_bitmap(vcpu, gfn);
2309 if (likely(!force_pt_level)) {
2310 level = mapping_level(vcpu, gfn);
2311 /*
2312 * This path builds a PAE pagetable - so we can map
2313 * 2mb pages at maximum. Therefore check if the level
2314 * is larger than that.
2315 */
2316 if (level > PT_DIRECTORY_LEVEL)
2317 level = PT_DIRECTORY_LEVEL;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002318
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002319 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2320 } else
2321 level = PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002322
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002323 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002324 smp_rmb();
Xiao Guangrong060c2ab2010-11-12 14:49:11 +08002325
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002326 if (try_async_pf(vcpu, prefault, gfn, v, &pfn, write, &map_writable))
Xiao Guangrong060c2ab2010-11-12 14:49:11 +08002327 return 0;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002328
Avi Kivityd196e342008-01-24 11:44:11 +02002329 /* mmio */
Huang Yingbf998152010-05-31 14:28:19 +08002330 if (is_error_pfn(pfn))
2331 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002332
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002333 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002334 if (mmu_notifier_retry(vcpu, mmu_seq))
2335 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02002336 kvm_mmu_free_some_pages(vcpu);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002337 if (likely(!force_pt_level))
2338 transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002339 r = __direct_map(vcpu, v, write, map_writable, level, gfn, pfn,
2340 prefault);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002341 spin_unlock(&vcpu->kvm->mmu_lock);
2342
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002343
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002344 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002345
2346out_unlock:
2347 spin_unlock(&vcpu->kvm->mmu_lock);
2348 kvm_release_pfn_clean(pfn);
2349 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002350}
2351
2352
Avi Kivity17ac10a2007-01-05 16:36:40 -08002353static void mmu_free_roots(struct kvm_vcpu *vcpu)
2354{
2355 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002356 struct kvm_mmu_page *sp;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002357 LIST_HEAD(invalid_list);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002358
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002359 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002360 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002361 spin_lock(&vcpu->kvm->mmu_lock);
Joerg Roedel81407ca2010-09-10 17:31:00 +02002362 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL &&
2363 (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL ||
2364 vcpu->arch.mmu.direct_map)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002365 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002366
Avi Kivity4db35312007-11-21 15:28:32 +02002367 sp = page_header(root);
2368 --sp->root_count;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002369 if (!sp->root_count && sp->role.invalid) {
2370 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
2371 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
2372 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002373 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002374 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002375 return;
2376 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002377 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002378 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002379
Avi Kivity417726a2007-04-12 17:35:58 +03002380 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002381 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002382 sp = page_header(root);
2383 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002384 if (!sp->root_count && sp->role.invalid)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002385 kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
2386 &invalid_list);
Avi Kivity417726a2007-04-12 17:35:58 +03002387 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002388 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002389 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002390 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002391 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002392 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002393}
2394
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002395static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2396{
2397 int ret = 0;
2398
2399 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03002400 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002401 ret = 1;
2402 }
2403
2404 return ret;
2405}
2406
Joerg Roedel651dd372010-09-10 17:30:59 +02002407static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu)
2408{
2409 struct kvm_mmu_page *sp;
Avi Kivity7ebaf152010-10-03 18:51:39 +02002410 unsigned i;
Joerg Roedel651dd372010-09-10 17:30:59 +02002411
2412 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2413 spin_lock(&vcpu->kvm->mmu_lock);
2414 kvm_mmu_free_some_pages(vcpu);
2415 sp = kvm_mmu_get_page(vcpu, 0, 0, PT64_ROOT_LEVEL,
2416 1, ACC_ALL, NULL);
2417 ++sp->root_count;
2418 spin_unlock(&vcpu->kvm->mmu_lock);
2419 vcpu->arch.mmu.root_hpa = __pa(sp->spt);
2420 } else if (vcpu->arch.mmu.shadow_root_level == PT32E_ROOT_LEVEL) {
2421 for (i = 0; i < 4; ++i) {
2422 hpa_t root = vcpu->arch.mmu.pae_root[i];
2423
2424 ASSERT(!VALID_PAGE(root));
2425 spin_lock(&vcpu->kvm->mmu_lock);
2426 kvm_mmu_free_some_pages(vcpu);
Avi Kivity649497d2010-12-28 12:09:07 +02002427 sp = kvm_mmu_get_page(vcpu, i << (30 - PAGE_SHIFT),
2428 i << 30,
Joerg Roedel651dd372010-09-10 17:30:59 +02002429 PT32_ROOT_LEVEL, 1, ACC_ALL,
2430 NULL);
2431 root = __pa(sp->spt);
2432 ++sp->root_count;
2433 spin_unlock(&vcpu->kvm->mmu_lock);
2434 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Joerg Roedel651dd372010-09-10 17:30:59 +02002435 }
Xiao Guangrong62927572010-09-27 18:02:12 +08002436 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Joerg Roedel651dd372010-09-10 17:30:59 +02002437 } else
2438 BUG();
2439
2440 return 0;
2441}
2442
2443static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002444{
Avi Kivity4db35312007-11-21 15:28:32 +02002445 struct kvm_mmu_page *sp;
Joerg Roedel81407ca2010-09-10 17:31:00 +02002446 u64 pdptr, pm_mask;
2447 gfn_t root_gfn;
2448 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002449
Joerg Roedel5777ed32010-09-10 17:30:42 +02002450 root_gfn = vcpu->arch.mmu.get_cr3(vcpu) >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002451
Joerg Roedel651dd372010-09-10 17:30:59 +02002452 if (mmu_check_root(vcpu, root_gfn))
2453 return 1;
2454
2455 /*
2456 * Do we shadow a long mode page table? If so we need to
2457 * write-protect the guests page table root.
2458 */
2459 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002460 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002461
2462 ASSERT(!VALID_PAGE(root));
Joerg Roedel651dd372010-09-10 17:30:59 +02002463
Avi Kivity8facbbf2010-05-04 12:58:32 +03002464 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b62010-05-12 21:00:35 -03002465 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel651dd372010-09-10 17:30:59 +02002466 sp = kvm_mmu_get_page(vcpu, root_gfn, 0, PT64_ROOT_LEVEL,
2467 0, ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002468 root = __pa(sp->spt);
2469 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002470 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002471 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002472 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002473 }
Joerg Roedelf87f9282010-09-02 17:29:45 +02002474
Joerg Roedel651dd372010-09-10 17:30:59 +02002475 /*
2476 * We shadow a 32 bit page table. This may be a legacy 2-level
Joerg Roedel81407ca2010-09-10 17:31:00 +02002477 * or a PAE 3-level page table. In either case we need to be aware that
2478 * the shadow page table may be a PAE or a long mode page table.
Joerg Roedel651dd372010-09-10 17:30:59 +02002479 */
Joerg Roedel81407ca2010-09-10 17:31:00 +02002480 pm_mask = PT_PRESENT_MASK;
2481 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL)
2482 pm_mask |= PT_ACCESSED_MASK | PT_WRITABLE_MASK | PT_USER_MASK;
2483
Avi Kivity17ac10a2007-01-05 16:36:40 -08002484 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002485 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002486
2487 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002488 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Joerg Roedeld41d1892010-09-10 17:30:58 +02002489 pdptr = kvm_pdptr_read_mmu(vcpu, &vcpu->arch.mmu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002490 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002491 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002492 continue;
2493 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002494 root_gfn = pdptr >> PAGE_SHIFT;
Joerg Roedelf87f9282010-09-02 17:29:45 +02002495 if (mmu_check_root(vcpu, root_gfn))
2496 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002497 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002498 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b62010-05-12 21:00:35 -03002499 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02002500 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Joerg Roedel651dd372010-09-10 17:30:59 +02002501 PT32_ROOT_LEVEL, 0,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002502 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002503 root = __pa(sp->spt);
2504 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002505 spin_unlock(&vcpu->kvm->mmu_lock);
2506
Joerg Roedel81407ca2010-09-10 17:31:00 +02002507 vcpu->arch.mmu.pae_root[i] = root | pm_mask;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002508 }
Xiao Guangrong62927572010-09-27 18:02:12 +08002509 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Joerg Roedel81407ca2010-09-10 17:31:00 +02002510
2511 /*
2512 * If we shadow a 32 bit page table with a long mode page
2513 * table we enter this path.
2514 */
2515 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2516 if (vcpu->arch.mmu.lm_root == NULL) {
2517 /*
2518 * The additional page necessary for this is only
2519 * allocated on demand.
2520 */
2521
2522 u64 *lm_root;
2523
2524 lm_root = (void*)get_zeroed_page(GFP_KERNEL);
2525 if (lm_root == NULL)
2526 return 1;
2527
2528 lm_root[0] = __pa(vcpu->arch.mmu.pae_root) | pm_mask;
2529
2530 vcpu->arch.mmu.lm_root = lm_root;
2531 }
2532
2533 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.lm_root);
2534 }
2535
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002536 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002537}
2538
Joerg Roedel651dd372010-09-10 17:30:59 +02002539static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
2540{
2541 if (vcpu->arch.mmu.direct_map)
2542 return mmu_alloc_direct_roots(vcpu);
2543 else
2544 return mmu_alloc_shadow_roots(vcpu);
2545}
2546
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002547static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2548{
2549 int i;
2550 struct kvm_mmu_page *sp;
2551
Joerg Roedel81407ca2010-09-10 17:31:00 +02002552 if (vcpu->arch.mmu.direct_map)
2553 return;
2554
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002555 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2556 return;
Xiao Guangrong69030742010-09-27 18:09:29 +08002557
2558 trace_kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC);
Joerg Roedel81407ca2010-09-10 17:31:00 +02002559 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002560 hpa_t root = vcpu->arch.mmu.root_hpa;
2561 sp = page_header(root);
2562 mmu_sync_children(vcpu, sp);
Xiao Guangrong5054c0d2010-11-12 14:46:08 +08002563 trace_kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002564 return;
2565 }
2566 for (i = 0; i < 4; ++i) {
2567 hpa_t root = vcpu->arch.mmu.pae_root[i];
2568
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002569 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002570 root &= PT64_BASE_ADDR_MASK;
2571 sp = page_header(root);
2572 mmu_sync_children(vcpu, sp);
2573 }
2574 }
Xiao Guangrong69030742010-09-27 18:09:29 +08002575 trace_kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002576}
2577
2578void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2579{
2580 spin_lock(&vcpu->kvm->mmu_lock);
2581 mmu_sync_roots(vcpu);
2582 spin_unlock(&vcpu->kvm->mmu_lock);
2583}
2584
Gleb Natapov1871c602010-02-10 14:21:32 +02002585static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
Avi Kivityab9ae312010-11-22 17:53:26 +02002586 u32 access, struct x86_exception *exception)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002587{
Avi Kivityab9ae312010-11-22 17:53:26 +02002588 if (exception)
2589 exception->error_code = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002590 return vaddr;
2591}
2592
Joerg Roedel6539e732010-09-10 17:30:50 +02002593static gpa_t nonpaging_gva_to_gpa_nested(struct kvm_vcpu *vcpu, gva_t vaddr,
Avi Kivityab9ae312010-11-22 17:53:26 +02002594 u32 access,
2595 struct x86_exception *exception)
Joerg Roedel6539e732010-09-10 17:30:50 +02002596{
Avi Kivityab9ae312010-11-22 17:53:26 +02002597 if (exception)
2598 exception->error_code = 0;
Joerg Roedel6539e732010-09-10 17:30:50 +02002599 return vcpu->arch.nested_mmu.translate_gpa(vcpu, vaddr, access);
2600}
2601
Avi Kivity6aa8b732006-12-10 02:21:36 -08002602static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002603 u32 error_code, bool prefault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002604{
Avi Kivitye8332402007-12-09 18:43:00 +02002605 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002606 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002607
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002608 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002609 r = mmu_topup_memory_caches(vcpu);
2610 if (r)
2611 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002612
Avi Kivity6aa8b732006-12-10 02:21:36 -08002613 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002614 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002615
Avi Kivitye8332402007-12-09 18:43:00 +02002616 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002617
Avi Kivitye8332402007-12-09 18:43:00 +02002618 return nonpaging_map(vcpu, gva & PAGE_MASK,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002619 error_code & PFERR_WRITE_MASK, gfn, prefault);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002620}
2621
Jan Kiszka7e1fbea2010-10-20 15:18:02 +02002622static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn)
Gleb Natapovaf585b92010-10-14 11:22:46 +02002623{
2624 struct kvm_arch_async_pf arch;
Xiao Guangrongfb67e142010-12-07 10:35:25 +08002625
Gleb Natapov7c907052010-10-14 11:22:53 +02002626 arch.token = (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id;
Gleb Natapovaf585b92010-10-14 11:22:46 +02002627 arch.gfn = gfn;
Xiao Guangrongc4806ac2010-11-12 14:49:55 +08002628 arch.direct_map = vcpu->arch.mmu.direct_map;
Xiao Guangrongfb67e142010-12-07 10:35:25 +08002629 arch.cr3 = vcpu->arch.mmu.get_cr3(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002630
2631 return kvm_setup_async_pf(vcpu, gva, gfn, &arch);
2632}
2633
2634static bool can_do_async_pf(struct kvm_vcpu *vcpu)
2635{
2636 if (unlikely(!irqchip_in_kernel(vcpu->kvm) ||
2637 kvm_event_needs_reinjection(vcpu)))
2638 return false;
2639
2640 return kvm_x86_ops->interrupt_allowed(vcpu);
2641}
2642
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002643static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002644 gva_t gva, pfn_t *pfn, bool write, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02002645{
2646 bool async;
2647
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002648 *pfn = gfn_to_pfn_async(vcpu->kvm, gfn, &async, write, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002649
2650 if (!async)
2651 return false; /* *pfn has correct page already */
2652
2653 put_page(pfn_to_page(*pfn));
2654
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002655 if (!prefault && can_do_async_pf(vcpu)) {
Xiao Guangrongc9b263d2010-11-01 16:58:43 +08002656 trace_kvm_try_async_get_page(gva, gfn);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002657 if (kvm_find_async_pf_gfn(vcpu, gfn)) {
2658 trace_kvm_async_pf_doublefault(gva, gfn);
2659 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
2660 return true;
2661 } else if (kvm_arch_setup_async_pf(vcpu, gva, gfn))
2662 return true;
2663 }
2664
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002665 *pfn = gfn_to_pfn_prot(vcpu->kvm, gfn, write, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002666
2667 return false;
2668}
2669
Gleb Natapov56028d02010-10-17 18:13:42 +02002670static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, u32 error_code,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002671 bool prefault)
Joerg Roedelfb72d162008-02-07 13:47:44 +01002672{
Anthony Liguori35149e22008-04-02 14:46:56 -05002673 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002674 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002675 int level;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002676 int force_pt_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002677 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002678 unsigned long mmu_seq;
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002679 int write = error_code & PFERR_WRITE_MASK;
2680 bool map_writable;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002681
2682 ASSERT(vcpu);
2683 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2684
2685 r = mmu_topup_memory_caches(vcpu);
2686 if (r)
2687 return r;
2688
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002689 force_pt_level = mapping_level_dirty_bitmap(vcpu, gfn);
2690 if (likely(!force_pt_level)) {
2691 level = mapping_level(vcpu, gfn);
2692 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2693 } else
2694 level = PT_PAGE_TABLE_LEVEL;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002695
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002696 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002697 smp_rmb();
Gleb Natapovaf585b92010-10-14 11:22:46 +02002698
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002699 if (try_async_pf(vcpu, prefault, gfn, gpa, &pfn, write, &map_writable))
Gleb Natapovaf585b92010-10-14 11:22:46 +02002700 return 0;
2701
2702 /* mmio */
Huang Yingbf998152010-05-31 14:28:19 +08002703 if (is_error_pfn(pfn))
2704 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002705 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002706 if (mmu_notifier_retry(vcpu, mmu_seq))
2707 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002708 kvm_mmu_free_some_pages(vcpu);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002709 if (likely(!force_pt_level))
2710 transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002711 r = __direct_map(vcpu, gpa, write, map_writable,
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002712 level, gfn, pfn, prefault);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002713 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002714
2715 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002716
2717out_unlock:
2718 spin_unlock(&vcpu->kvm->mmu_lock);
2719 kvm_release_pfn_clean(pfn);
2720 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002721}
2722
Avi Kivity6aa8b732006-12-10 02:21:36 -08002723static void nonpaging_free(struct kvm_vcpu *vcpu)
2724{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002725 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002726}
2727
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002728static int nonpaging_init_context(struct kvm_vcpu *vcpu,
2729 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002730{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002731 context->new_cr3 = nonpaging_new_cr3;
2732 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002733 context->gva_to_gpa = nonpaging_gva_to_gpa;
2734 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002735 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002736 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002737 context->invlpg = nonpaging_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08002738 context->update_pte = nonpaging_update_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002739 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002740 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002741 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002742 context->direct_map = true;
Joerg Roedel2d48a982010-09-10 17:31:01 +02002743 context->nx = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002744 return 0;
2745}
2746
Avi Kivityd835dfe2007-11-21 02:57:59 +02002747void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002748{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002749 ++vcpu->stat.tlb_flush;
Avi Kivitya8eeb042010-05-10 12:34:53 +03002750 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002751}
2752
2753static void paging_new_cr3(struct kvm_vcpu *vcpu)
2754{
Avi Kivity9f8fe502010-12-05 17:30:00 +02002755 pgprintk("%s: cr3 %lx\n", __func__, kvm_read_cr3(vcpu));
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002756 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002757}
2758
Joerg Roedel5777ed32010-09-10 17:30:42 +02002759static unsigned long get_cr3(struct kvm_vcpu *vcpu)
2760{
Avi Kivity9f8fe502010-12-05 17:30:00 +02002761 return kvm_read_cr3(vcpu);
Joerg Roedel5777ed32010-09-10 17:30:42 +02002762}
2763
Avi Kivity6389ee92010-11-29 16:12:30 +02002764static void inject_page_fault(struct kvm_vcpu *vcpu,
2765 struct x86_exception *fault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002766{
Avi Kivity6389ee92010-11-29 16:12:30 +02002767 vcpu->arch.mmu.inject_page_fault(vcpu, fault);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002768}
2769
Avi Kivity6aa8b732006-12-10 02:21:36 -08002770static void paging_free(struct kvm_vcpu *vcpu)
2771{
2772 nonpaging_free(vcpu);
2773}
2774
Joerg Roedel3241f222010-09-10 17:30:45 +02002775static bool is_rsvd_bits_set(struct kvm_mmu *mmu, u64 gpte, int level)
Dong, Eddie82725b22009-03-30 16:21:08 +08002776{
2777 int bit7;
2778
2779 bit7 = (gpte >> 7) & 1;
Joerg Roedel3241f222010-09-10 17:30:45 +02002780 return (gpte & mmu->rsvd_bits_mask[bit7][level-1]) != 0;
Dong, Eddie82725b22009-03-30 16:21:08 +08002781}
2782
Avi Kivity6aa8b732006-12-10 02:21:36 -08002783#define PTTYPE 64
2784#include "paging_tmpl.h"
2785#undef PTTYPE
2786
2787#define PTTYPE 32
2788#include "paging_tmpl.h"
2789#undef PTTYPE
2790
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002791static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
2792 struct kvm_mmu *context,
2793 int level)
Dong, Eddie82725b22009-03-30 16:21:08 +08002794{
Dong, Eddie82725b22009-03-30 16:21:08 +08002795 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2796 u64 exb_bit_rsvd = 0;
2797
Joerg Roedel2d48a982010-09-10 17:31:01 +02002798 if (!context->nx)
Dong, Eddie82725b22009-03-30 16:21:08 +08002799 exb_bit_rsvd = rsvd_bits(63, 63);
2800 switch (level) {
2801 case PT32_ROOT_LEVEL:
2802 /* no rsvd bits for 2 level 4K page table entries */
2803 context->rsvd_bits_mask[0][1] = 0;
2804 context->rsvd_bits_mask[0][0] = 0;
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002805 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2806
2807 if (!is_pse(vcpu)) {
2808 context->rsvd_bits_mask[1][1] = 0;
2809 break;
2810 }
2811
Dong, Eddie82725b22009-03-30 16:21:08 +08002812 if (is_cpuid_PSE36())
2813 /* 36bits PSE 4MB page */
2814 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2815 else
2816 /* 32 bits PSE 4MB page */
2817 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08002818 break;
2819 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002820 context->rsvd_bits_mask[0][2] =
2821 rsvd_bits(maxphyaddr, 63) |
2822 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002823 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002824 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002825 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2826 rsvd_bits(maxphyaddr, 62); /* PTE */
2827 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2828 rsvd_bits(maxphyaddr, 62) |
2829 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002830 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002831 break;
2832 case PT64_ROOT_LEVEL:
2833 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2834 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2835 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2836 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2837 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002838 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002839 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2840 rsvd_bits(maxphyaddr, 51);
2841 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002842 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2843 rsvd_bits(maxphyaddr, 51) |
2844 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002845 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002846 rsvd_bits(maxphyaddr, 51) |
2847 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002848 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002849 break;
2850 }
2851}
2852
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002853static int paging64_init_context_common(struct kvm_vcpu *vcpu,
2854 struct kvm_mmu *context,
2855 int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002856{
Joerg Roedel2d48a982010-09-10 17:31:01 +02002857 context->nx = is_nx(vcpu);
2858
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002859 reset_rsvds_bits_mask(vcpu, context, level);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002860
2861 ASSERT(is_pae(vcpu));
2862 context->new_cr3 = paging_new_cr3;
2863 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002864 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002865 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002866 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002867 context->invlpg = paging64_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08002868 context->update_pte = paging64_update_pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002869 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002870 context->root_level = level;
2871 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002872 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002873 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002874 return 0;
2875}
2876
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002877static int paging64_init_context(struct kvm_vcpu *vcpu,
2878 struct kvm_mmu *context)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002879{
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002880 return paging64_init_context_common(vcpu, context, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002881}
2882
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002883static int paging32_init_context(struct kvm_vcpu *vcpu,
2884 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002885{
Joerg Roedel2d48a982010-09-10 17:31:01 +02002886 context->nx = false;
2887
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002888 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002889
2890 context->new_cr3 = paging_new_cr3;
2891 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002892 context->gva_to_gpa = paging32_gva_to_gpa;
2893 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002894 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002895 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002896 context->invlpg = paging32_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08002897 context->update_pte = paging32_update_pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002898 context->root_level = PT32_ROOT_LEVEL;
2899 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002900 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002901 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002902 return 0;
2903}
2904
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002905static int paging32E_init_context(struct kvm_vcpu *vcpu,
2906 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002907{
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002908 return paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002909}
2910
Joerg Roedelfb72d162008-02-07 13:47:44 +01002911static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2912{
Joerg Roedel14dfe852010-09-10 17:30:49 +02002913 struct kvm_mmu *context = vcpu->arch.walk_mmu;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002914
Avi Kivityc445f8e2010-12-21 16:26:01 +02002915 context->base_role.word = 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002916 context->new_cr3 = nonpaging_new_cr3;
2917 context->page_fault = tdp_page_fault;
2918 context->free = nonpaging_free;
2919 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002920 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002921 context->invlpg = nonpaging_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08002922 context->update_pte = nonpaging_update_pte;
Sheng Yang67253af2008-04-25 10:20:22 +08002923 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002924 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002925 context->direct_map = true;
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02002926 context->set_cr3 = kvm_x86_ops->set_tdp_cr3;
Joerg Roedel5777ed32010-09-10 17:30:42 +02002927 context->get_cr3 = get_cr3;
Joerg Roedelcb659db2010-09-10 17:30:43 +02002928 context->inject_page_fault = kvm_inject_page_fault;
Joerg Roedel2d48a982010-09-10 17:31:01 +02002929 context->nx = is_nx(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002930
2931 if (!is_paging(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002932 context->nx = false;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002933 context->gva_to_gpa = nonpaging_gva_to_gpa;
2934 context->root_level = 0;
2935 } else if (is_long_mode(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002936 context->nx = is_nx(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002937 reset_rsvds_bits_mask(vcpu, context, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002938 context->gva_to_gpa = paging64_gva_to_gpa;
2939 context->root_level = PT64_ROOT_LEVEL;
2940 } else if (is_pae(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002941 context->nx = is_nx(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002942 reset_rsvds_bits_mask(vcpu, context, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002943 context->gva_to_gpa = paging64_gva_to_gpa;
2944 context->root_level = PT32E_ROOT_LEVEL;
2945 } else {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002946 context->nx = false;
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002947 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002948 context->gva_to_gpa = paging32_gva_to_gpa;
2949 context->root_level = PT32_ROOT_LEVEL;
2950 }
2951
2952 return 0;
2953}
2954
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002955int kvm_init_shadow_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002956{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002957 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002958 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002959 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002960
2961 if (!is_paging(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002962 r = nonpaging_init_context(vcpu, context);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002963 else if (is_long_mode(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002964 r = paging64_init_context(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002965 else if (is_pae(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002966 r = paging32E_init_context(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002967 else
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002968 r = paging32_init_context(vcpu, context);
Avi Kivitya770f6f2008-12-21 19:20:09 +02002969
Avi Kivity5b7e0102010-04-14 19:20:03 +03002970 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
Joerg Roedelf43addd2010-09-10 17:30:40 +02002971 vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002972
2973 return r;
2974}
2975EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu);
2976
2977static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
2978{
Joerg Roedel14dfe852010-09-10 17:30:49 +02002979 int r = kvm_init_shadow_mmu(vcpu, vcpu->arch.walk_mmu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002980
Joerg Roedel14dfe852010-09-10 17:30:49 +02002981 vcpu->arch.walk_mmu->set_cr3 = kvm_x86_ops->set_cr3;
2982 vcpu->arch.walk_mmu->get_cr3 = get_cr3;
2983 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
Avi Kivitya770f6f2008-12-21 19:20:09 +02002984
2985 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002986}
2987
Joerg Roedel02f59dc2010-09-10 17:30:54 +02002988static int init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
2989{
2990 struct kvm_mmu *g_context = &vcpu->arch.nested_mmu;
2991
2992 g_context->get_cr3 = get_cr3;
2993 g_context->inject_page_fault = kvm_inject_page_fault;
2994
2995 /*
2996 * Note that arch.mmu.gva_to_gpa translates l2_gva to l1_gpa. The
2997 * translation of l2_gpa to l1_gpa addresses is done using the
2998 * arch.nested_mmu.gva_to_gpa function. Basically the gva_to_gpa
2999 * functions between mmu and nested_mmu are swapped.
3000 */
3001 if (!is_paging(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003002 g_context->nx = false;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003003 g_context->root_level = 0;
3004 g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested;
3005 } else if (is_long_mode(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003006 g_context->nx = is_nx(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003007 reset_rsvds_bits_mask(vcpu, g_context, PT64_ROOT_LEVEL);
3008 g_context->root_level = PT64_ROOT_LEVEL;
3009 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
3010 } else if (is_pae(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003011 g_context->nx = is_nx(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003012 reset_rsvds_bits_mask(vcpu, g_context, PT32E_ROOT_LEVEL);
3013 g_context->root_level = PT32E_ROOT_LEVEL;
3014 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
3015 } else {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003016 g_context->nx = false;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003017 reset_rsvds_bits_mask(vcpu, g_context, PT32_ROOT_LEVEL);
3018 g_context->root_level = PT32_ROOT_LEVEL;
3019 g_context->gva_to_gpa = paging32_gva_to_gpa_nested;
3020 }
3021
3022 return 0;
3023}
3024
Joerg Roedelfb72d162008-02-07 13:47:44 +01003025static int init_kvm_mmu(struct kvm_vcpu *vcpu)
3026{
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003027 if (mmu_is_nested(vcpu))
3028 return init_kvm_nested_mmu(vcpu);
3029 else if (tdp_enabled)
Joerg Roedelfb72d162008-02-07 13:47:44 +01003030 return init_kvm_tdp_mmu(vcpu);
3031 else
3032 return init_kvm_softmmu(vcpu);
3033}
3034
Avi Kivity6aa8b732006-12-10 02:21:36 -08003035static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
3036{
3037 ASSERT(vcpu);
Sheng Yang62ad0752010-05-12 16:40:41 +08003038 if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
3039 /* mmu.free() should set root_hpa = INVALID_PAGE */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003040 vcpu->arch.mmu.free(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003041}
3042
3043int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
3044{
Avi Kivity17c3ba92007-06-04 15:58:30 +03003045 destroy_kvm_mmu(vcpu);
3046 return init_kvm_mmu(vcpu);
3047}
Eddie Dong8668a3c2007-10-10 14:26:45 +08003048EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03003049
3050int kvm_mmu_load(struct kvm_vcpu *vcpu)
3051{
Avi Kivity714b93d2007-01-05 16:36:53 -08003052 int r;
3053
Avi Kivitye2dec932007-01-05 16:36:54 -08003054 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03003055 if (r)
3056 goto out;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003057 r = mmu_alloc_roots(vcpu);
Avi Kivity8facbbf2010-05-04 12:58:32 +03003058 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003059 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003060 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003061 if (r)
3062 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08003063 /* set_cr3() should ensure TLB has been flushed */
Joerg Roedelf43addd2010-09-10 17:30:40 +02003064 vcpu->arch.mmu.set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08003065out:
3066 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003067}
Avi Kivity17c3ba92007-06-04 15:58:30 +03003068EXPORT_SYMBOL_GPL(kvm_mmu_load);
3069
3070void kvm_mmu_unload(struct kvm_vcpu *vcpu)
3071{
3072 mmu_free_roots(vcpu);
3073}
Joerg Roedel4b161842010-09-10 17:31:03 +02003074EXPORT_SYMBOL_GPL(kvm_mmu_unload);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003075
Avi Kivity00284252007-05-01 16:53:31 +03003076static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Xiao Guangrong7c562522011-03-28 10:29:27 +08003077 struct kvm_mmu_page *sp, u64 *spte,
3078 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03003079{
Marcelo Tosatti30945382008-06-11 20:32:40 -03003080 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02003081 ++vcpu->kvm->stat.mmu_pde_zapped;
3082 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03003083 }
Avi Kivity00284252007-05-01 16:53:31 +03003084
Avi Kivity4cee5762007-11-18 16:37:07 +02003085 ++vcpu->kvm->stat.mmu_pte_updated;
Xiao Guangrong7c562522011-03-28 10:29:27 +08003086 vcpu->arch.mmu.update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03003087}
3088
Avi Kivity79539ce2007-11-21 02:06:21 +02003089static bool need_remote_flush(u64 old, u64 new)
3090{
3091 if (!is_shadow_present_pte(old))
3092 return false;
3093 if (!is_shadow_present_pte(new))
3094 return true;
3095 if ((old ^ new) & PT64_BASE_ADDR_MASK)
3096 return true;
3097 old ^= PT64_NX_MASK;
3098 new ^= PT64_NX_MASK;
3099 return (old & ~new & PT64_PERM_MASK) != 0;
3100}
3101
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003102static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page,
3103 bool remote_flush, bool local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003104{
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003105 if (zap_page)
3106 return;
3107
3108 if (remote_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003109 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003110 else if (local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003111 kvm_mmu_flush_tlb(vcpu);
3112}
3113
Avi Kivity12b7d282007-09-23 14:10:49 +02003114static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
3115{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003116 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02003117
Sheng Yang7b523452008-04-25 21:13:50 +08003118 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02003119}
3120
Avi Kivity1b7fcd32008-05-15 13:51:35 +03003121static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
3122{
3123 u64 *spte = vcpu->arch.last_pte_updated;
3124
3125 if (spte
3126 && vcpu->arch.last_pte_gfn == gfn
3127 && shadow_accessed_mask
3128 && !(*spte & shadow_accessed_mask)
3129 && is_shadow_present_pte(*spte))
3130 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
3131}
3132
Avi Kivity09072da2007-05-01 14:16:52 +03003133void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003134 const u8 *new, int bytes,
3135 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08003136{
Avi Kivity9b7a0322007-01-05 16:36:45 -08003137 gfn_t gfn = gpa >> PAGE_SHIFT;
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003138 union kvm_mmu_page_role mask = { .word = 0 };
Avi Kivity4db35312007-11-21 15:28:32 +02003139 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003140 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003141 LIST_HEAD(invalid_list);
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08003142 u64 entry, gentry, *spte;
3143 unsigned pte_size, page_offset, misaligned, quadrant, offset;
3144 int level, npte, invlpg_counter, r, flooded = 0;
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003145 bool remote_flush, local_flush, zap_page;
3146
Xiao Guangrong332b2072011-05-15 23:20:27 +08003147 /*
3148 * If we don't have indirect shadow pages, it means no page is
3149 * write-protected, so we can exit simply.
3150 */
3151 if (!ACCESS_ONCE(vcpu->kvm->arch.indirect_shadow_pages))
3152 return;
3153
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003154 zap_page = remote_flush = local_flush = false;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08003155 offset = offset_in_page(gpa);
Avi Kivity9b7a0322007-01-05 16:36:45 -08003156
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003157 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivity72016f32010-03-15 13:59:53 +02003158
Avi Kivity08e850c2010-03-15 13:59:57 +02003159 invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
3160
3161 /*
3162 * Assume that the pte write on a page table of the same type
Xiao Guangrong49b26e22011-03-04 19:00:00 +08003163 * as the current vcpu paging mode since we update the sptes only
3164 * when they have the same mode.
Avi Kivity08e850c2010-03-15 13:59:57 +02003165 */
3166 if ((is_pae(vcpu) && bytes == 4) || !new) {
3167 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
3168 if (is_pae(vcpu)) {
3169 gpa &= ~(gpa_t)7;
3170 bytes = 8;
3171 }
3172 r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
3173 if (r)
3174 gentry = 0;
3175 new = (const u8 *)&gentry;
3176 }
3177
Avi Kivity72016f32010-03-15 13:59:53 +02003178 switch (bytes) {
3179 case 4:
3180 gentry = *(const u32 *)new;
3181 break;
3182 case 8:
3183 gentry = *(const u64 *)new;
3184 break;
3185 default:
3186 gentry = 0;
3187 break;
3188 }
3189
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003190 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity08e850c2010-03-15 13:59:57 +02003191 if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
3192 gentry = 0;
Avi Kivityeb787d12007-12-31 15:27:49 +02003193 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02003194 ++vcpu->kvm->stat.mmu_pte_write;
Xiao Guangrong8b1fe172010-08-30 18:22:53 +08003195 trace_kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE);
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003196 if (guest_initiated) {
Xiao Guangrong1b7fd452011-03-04 18:58:02 +08003197 kvm_mmu_access_page(vcpu, gfn);
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003198 if (gfn == vcpu->arch.last_pt_write_gfn
3199 && !last_updated_pte_accessed(vcpu)) {
3200 ++vcpu->arch.last_pt_write_count;
3201 if (vcpu->arch.last_pt_write_count >= 3)
3202 flooded = 1;
3203 } else {
3204 vcpu->arch.last_pt_write_gfn = gfn;
3205 vcpu->arch.last_pt_write_count = 1;
3206 vcpu->arch.last_pte_updated = NULL;
3207 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08003208 }
Xiao Guangrong3246af02010-04-16 16:35:54 +08003209
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003210 mask.cr0_wp = mask.cr4_pae = mask.nxe = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003211 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node) {
Avi Kivity5b7e0102010-04-14 19:20:03 +03003212 pte_size = sp->role.cr4_pae ? 8 : 4;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003213 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03003214 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08003215 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003216 /*
3217 * Misaligned accesses are too much trouble to fix
3218 * up; also, they usually indicate a page is not used
3219 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08003220 *
3221 * If we're seeing too many writes to a page,
3222 * it may no longer be a page table, or we may be
3223 * forking, in which case it is better to unmap the
3224 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003225 */
3226 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02003227 gpa, bytes, sp->role.word);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003228 zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003229 &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02003230 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003231 continue;
3232 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08003233 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02003234 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02003235 npte = 1;
Avi Kivity5b7e0102010-04-14 19:20:03 +03003236 if (!sp->role.cr4_pae) {
Avi Kivityac1b7142007-03-08 17:13:32 +02003237 page_offset <<= 1; /* 32->64 */
3238 /*
3239 * A 32-bit pde maps 4MB while the shadow pdes map
3240 * only 2MB. So we need to double the offset again
3241 * and zap two pdes instead of one.
3242 */
3243 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03003244 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02003245 page_offset <<= 1;
3246 npte = 2;
3247 }
Avi Kivityfce06572007-05-01 16:44:05 +03003248 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003249 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02003250 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03003251 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003252 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003253 local_flush = true;
Avi Kivity4db35312007-11-21 15:28:32 +02003254 spte = &sp->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02003255 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02003256 entry = *spte;
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08003257 mmu_page_zap_pte(vcpu->kvm, sp, spte);
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003258 if (gentry &&
3259 !((sp->role.word ^ vcpu->arch.mmu.base_role.word)
3260 & mask.word))
Xiao Guangrong7c562522011-03-28 10:29:27 +08003261 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003262 if (!remote_flush && need_remote_flush(entry, *spte))
3263 remote_flush = true;
Avi Kivityac1b7142007-03-08 17:13:32 +02003264 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003265 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08003266 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003267 mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003268 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong8b1fe172010-08-30 18:22:53 +08003269 trace_kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003270 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivityda4a00f2007-01-05 16:36:44 -08003271}
3272
Avi Kivitya4360362007-01-05 16:36:45 -08003273int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
3274{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003275 gpa_t gpa;
3276 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08003277
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02003278 if (vcpu->arch.mmu.direct_map)
Avi Kivity60f24782009-08-27 13:37:06 +03003279 return 0;
3280
Gleb Natapov1871c602010-02-10 14:21:32 +02003281 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003282
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003283 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003284 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003285 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003286 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08003287}
Avi Kivity577bdc42008-07-19 08:57:05 +03003288EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08003289
Avi Kivity22d95b12007-09-14 20:26:06 +03003290void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08003291{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003292 LIST_HEAD(invalid_list);
Xiao Guangrong103ad252010-06-04 21:54:38 +08003293
Dave Hansene0df7b92010-08-19 18:11:05 -07003294 while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES &&
Izik Eidus3b80fff2009-07-28 15:26:58 -03003295 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02003296 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08003297
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003298 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02003299 struct kvm_mmu_page, link);
Dave Hansene0df7b92010-08-19 18:11:05 -07003300 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
Xiaotian Feng80b63fa2010-08-24 10:31:07 +08003301 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02003302 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08003303 }
3304}
Avi Kivityebeace82007-01-05 16:36:47 -08003305
Andre Przywaradc25e892010-12-21 11:12:07 +01003306int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code,
3307 void *insn, int insn_len)
Avi Kivity30677142007-10-28 18:48:59 +02003308{
3309 int r;
3310 enum emulation_result er;
3311
Gleb Natapov56028d02010-10-17 18:13:42 +02003312 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code, false);
Avi Kivity30677142007-10-28 18:48:59 +02003313 if (r < 0)
3314 goto out;
3315
3316 if (!r) {
3317 r = 1;
3318 goto out;
3319 }
3320
Avi Kivityb733bfb2007-10-28 18:52:05 +02003321 r = mmu_topup_memory_caches(vcpu);
3322 if (r)
3323 goto out;
3324
Andre Przywaradc25e892010-12-21 11:12:07 +01003325 er = x86_emulate_instruction(vcpu, cr2, 0, insn, insn_len);
Avi Kivity30677142007-10-28 18:48:59 +02003326
3327 switch (er) {
3328 case EMULATE_DONE:
3329 return 1;
3330 case EMULATE_DO_MMIO:
3331 ++vcpu->stat.mmio_exits;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03003332 /* fall through */
Avi Kivity30677142007-10-28 18:48:59 +02003333 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03003334 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02003335 default:
3336 BUG();
3337 }
3338out:
Avi Kivity30677142007-10-28 18:48:59 +02003339 return r;
3340}
3341EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
3342
Marcelo Tosattia7052892008-09-23 13:18:35 -03003343void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
3344{
Marcelo Tosattia7052892008-09-23 13:18:35 -03003345 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003346 kvm_mmu_flush_tlb(vcpu);
3347 ++vcpu->stat.invlpg;
3348}
3349EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
3350
Joerg Roedel18552672008-02-07 13:47:41 +01003351void kvm_enable_tdp(void)
3352{
3353 tdp_enabled = true;
3354}
3355EXPORT_SYMBOL_GPL(kvm_enable_tdp);
3356
Joerg Roedel5f4cb662008-07-14 20:36:36 +02003357void kvm_disable_tdp(void)
3358{
3359 tdp_enabled = false;
3360}
3361EXPORT_SYMBOL_GPL(kvm_disable_tdp);
3362
Avi Kivity6aa8b732006-12-10 02:21:36 -08003363static void free_mmu_pages(struct kvm_vcpu *vcpu)
3364{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003365 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Joerg Roedel81407ca2010-09-10 17:31:00 +02003366 if (vcpu->arch.mmu.lm_root != NULL)
3367 free_page((unsigned long)vcpu->arch.mmu.lm_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003368}
3369
3370static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
3371{
Avi Kivity17ac10a2007-01-05 16:36:40 -08003372 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003373 int i;
3374
3375 ASSERT(vcpu);
3376
Avi Kivity17ac10a2007-01-05 16:36:40 -08003377 /*
3378 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
3379 * Therefore we need to allocate shadow page tables in the first
3380 * 4GB of memory, which happens to fit the DMA32 zone.
3381 */
3382 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
3383 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08003384 return -ENOMEM;
3385
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003386 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003387 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003388 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003389
Avi Kivity6aa8b732006-12-10 02:21:36 -08003390 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003391}
3392
Ingo Molnar8018c272006-12-29 16:50:01 -08003393int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003394{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003395 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003396 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003397
Ingo Molnar8018c272006-12-29 16:50:01 -08003398 return alloc_mmu_pages(vcpu);
3399}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003400
Ingo Molnar8018c272006-12-29 16:50:01 -08003401int kvm_mmu_setup(struct kvm_vcpu *vcpu)
3402{
3403 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003404 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08003405
Ingo Molnar8018c272006-12-29 16:50:01 -08003406 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003407}
3408
Avi Kivity90cb0522007-07-17 13:04:56 +03003409void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003410{
Avi Kivity4db35312007-11-21 15:28:32 +02003411 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003412
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003413 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003414 int i;
3415 u64 *pt;
3416
Sheng Yang291f26b2008-10-16 17:30:57 +08003417 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003418 continue;
3419
Avi Kivity4db35312007-11-21 15:28:32 +02003420 pt = sp->spt;
Avi Kivity8234b222010-12-27 12:08:45 +02003421 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Xiao Guangrongda8dc752011-03-04 18:56:41 +08003422 if (!is_shadow_present_pte(pt[i]) ||
3423 !is_last_spte(pt[i], sp->role.level))
3424 continue;
3425
3426 if (is_large_pte(pt[i])) {
Avi Kivity8234b222010-12-27 12:08:45 +02003427 drop_spte(kvm, &pt[i],
3428 shadow_trap_nonpresent_pte);
3429 --kvm->stat.lpages;
Xiao Guangrongda8dc752011-03-04 18:56:41 +08003430 continue;
Avi Kivity8234b222010-12-27 12:08:45 +02003431 }
Xiao Guangrongda8dc752011-03-04 18:56:41 +08003432
Avi Kivity6aa8b732006-12-10 02:21:36 -08003433 /* avoid RMW */
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003434 if (is_writable_pte(pt[i]))
Takuya Yoshikawa700e1b12010-12-06 01:11:33 +09003435 update_spte(&pt[i], pt[i] & ~PT_WRITABLE_MASK);
Avi Kivity8234b222010-12-27 12:08:45 +02003436 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003437 }
Avi Kivity171d5952008-08-27 16:40:51 +03003438 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003439}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003440
Avi Kivity90cb0522007-07-17 13:04:56 +03003441void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03003442{
Avi Kivity4db35312007-11-21 15:28:32 +02003443 struct kvm_mmu_page *sp, *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003444 LIST_HEAD(invalid_list);
Dor Laore0fa8262007-03-30 13:06:33 +03003445
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003446 spin_lock(&kvm->mmu_lock);
Xiao Guangrong3246af02010-04-16 16:35:54 +08003447restart:
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003448 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003449 if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list))
Xiao Guangrong3246af02010-04-16 16:35:54 +08003450 goto restart;
3451
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003452 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003453 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03003454}
3455
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003456static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm,
3457 struct list_head *invalid_list)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003458{
3459 struct kvm_mmu_page *page;
3460
3461 page = container_of(kvm->arch.active_mmu_pages.prev,
3462 struct kvm_mmu_page, link);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003463 return kvm_mmu_prepare_zap_page(kvm, page, invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003464}
3465
Ying Han1495f232011-05-24 17:12:27 -07003466static int mmu_shrink(struct shrinker *shrink, struct shrink_control *sc)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003467{
3468 struct kvm *kvm;
3469 struct kvm *kvm_freed = NULL;
Ying Han1495f232011-05-24 17:12:27 -07003470 int nr_to_scan = sc->nr_to_scan;
Dave Hansen45221ab2010-08-19 18:11:37 -07003471
3472 if (nr_to_scan == 0)
3473 goto out;
Izik Eidus3ee16c82008-03-30 15:17:21 +03003474
Jan Kiszkae935b832011-02-08 12:55:33 +01003475 raw_spin_lock(&kvm_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003476
3477 list_for_each_entry(kvm, &vm_list, vm_list) {
Dave Hansen45221ab2010-08-19 18:11:37 -07003478 int idx, freed_pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003479 LIST_HEAD(invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003480
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003481 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003482 spin_lock(&kvm->mmu_lock);
Dave Hansen45221ab2010-08-19 18:11:37 -07003483 if (!kvm_freed && nr_to_scan > 0 &&
3484 kvm->arch.n_used_mmu_pages > 0) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003485 freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm,
3486 &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003487 kvm_freed = kvm;
3488 }
3489 nr_to_scan--;
3490
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003491 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003492 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003493 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003494 }
3495 if (kvm_freed)
3496 list_move_tail(&kvm_freed->vm_list, &vm_list);
3497
Jan Kiszkae935b832011-02-08 12:55:33 +01003498 raw_spin_unlock(&kvm_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003499
Dave Hansen45221ab2010-08-19 18:11:37 -07003500out:
3501 return percpu_counter_read_positive(&kvm_total_used_mmu_pages);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003502}
3503
3504static struct shrinker mmu_shrinker = {
3505 .shrink = mmu_shrink,
3506 .seeks = DEFAULT_SEEKS * 10,
3507};
3508
Ingo Molnar2ddfd202008-05-22 10:37:48 +02003509static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03003510{
Xiao Guangrong53c07b12011-05-15 23:26:20 +08003511 if (pte_list_desc_cache)
3512 kmem_cache_destroy(pte_list_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003513 if (mmu_page_header_cache)
3514 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003515}
3516
3517int kvm_mmu_module_init(void)
3518{
Xiao Guangrong53c07b12011-05-15 23:26:20 +08003519 pte_list_desc_cache = kmem_cache_create("pte_list_desc",
3520 sizeof(struct pte_list_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09003521 0, 0, NULL);
Xiao Guangrong53c07b12011-05-15 23:26:20 +08003522 if (!pte_list_desc_cache)
Avi Kivityb5a33a72007-04-15 16:31:09 +03003523 goto nomem;
3524
Avi Kivityd3d25b02007-05-30 12:34:53 +03003525 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
3526 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09003527 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003528 if (!mmu_page_header_cache)
3529 goto nomem;
3530
Wei Yongjun45bf21a2010-08-23 16:13:15 +08003531 if (percpu_counter_init(&kvm_total_used_mmu_pages, 0))
3532 goto nomem;
3533
Izik Eidus3ee16c82008-03-30 15:17:21 +03003534 register_shrinker(&mmu_shrinker);
3535
Avi Kivityb5a33a72007-04-15 16:31:09 +03003536 return 0;
3537
3538nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03003539 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003540 return -ENOMEM;
3541}
3542
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003543/*
3544 * Caculate mmu pages needed for kvm.
3545 */
3546unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3547{
3548 int i;
3549 unsigned int nr_mmu_pages;
3550 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003551 struct kvm_memslots *slots;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003552
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003553 slots = kvm_memslots(kvm);
3554
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003555 for (i = 0; i < slots->nmemslots; i++)
3556 nr_pages += slots->memslots[i].npages;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003557
3558 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3559 nr_mmu_pages = max(nr_mmu_pages,
3560 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3561
3562 return nr_mmu_pages;
3563}
3564
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003565static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3566 unsigned len)
3567{
3568 if (len > buffer->len)
3569 return NULL;
3570 return buffer->ptr;
3571}
3572
3573static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3574 unsigned len)
3575{
3576 void *ret;
3577
3578 ret = pv_mmu_peek_buffer(buffer, len);
3579 if (!ret)
3580 return ret;
3581 buffer->ptr += len;
3582 buffer->len -= len;
3583 buffer->processed += len;
3584 return ret;
3585}
3586
3587static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3588 gpa_t addr, gpa_t value)
3589{
3590 int bytes = 8;
3591 int r;
3592
3593 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3594 bytes = 4;
3595
3596 r = mmu_topup_memory_caches(vcpu);
3597 if (r)
3598 return r;
3599
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003600 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003601 return -EFAULT;
3602
3603 return 1;
3604}
3605
3606static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3607{
Avi Kivity9f8fe502010-12-05 17:30:00 +02003608 (void)kvm_set_cr3(vcpu, kvm_read_cr3(vcpu));
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003609 return 1;
3610}
3611
3612static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3613{
3614 spin_lock(&vcpu->kvm->mmu_lock);
3615 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3616 spin_unlock(&vcpu->kvm->mmu_lock);
3617 return 1;
3618}
3619
3620static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3621 struct kvm_pv_mmu_op_buffer *buffer)
3622{
3623 struct kvm_mmu_op_header *header;
3624
3625 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3626 if (!header)
3627 return 0;
3628 switch (header->op) {
3629 case KVM_MMU_OP_WRITE_PTE: {
3630 struct kvm_mmu_op_write_pte *wpte;
3631
3632 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3633 if (!wpte)
3634 return 0;
3635 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3636 wpte->pte_val);
3637 }
3638 case KVM_MMU_OP_FLUSH_TLB: {
3639 struct kvm_mmu_op_flush_tlb *ftlb;
3640
3641 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3642 if (!ftlb)
3643 return 0;
3644 return kvm_pv_mmu_flush_tlb(vcpu);
3645 }
3646 case KVM_MMU_OP_RELEASE_PT: {
3647 struct kvm_mmu_op_release_pt *rpt;
3648
3649 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3650 if (!rpt)
3651 return 0;
3652 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3653 }
3654 default: return 0;
3655 }
3656}
3657
3658int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3659 gpa_t addr, unsigned long *ret)
3660{
3661 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003662 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003663
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003664 buffer->ptr = buffer->buf;
3665 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3666 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003667
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003668 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003669 if (r)
3670 goto out;
3671
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003672 while (buffer->len) {
3673 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003674 if (r < 0)
3675 goto out;
3676 if (r == 0)
3677 break;
3678 }
3679
3680 r = 1;
3681out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003682 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003683 return r;
3684}
3685
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003686int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3687{
3688 struct kvm_shadow_walk_iterator iterator;
3689 int nr_sptes = 0;
3690
3691 spin_lock(&vcpu->kvm->mmu_lock);
3692 for_each_shadow_entry(vcpu, addr, iterator) {
3693 sptes[iterator.level-1] = *iterator.sptep;
3694 nr_sptes++;
3695 if (!is_shadow_present_pte(*iterator.sptep))
3696 break;
3697 }
3698 spin_unlock(&vcpu->kvm->mmu_lock);
3699
3700 return nr_sptes;
3701}
3702EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3703
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08003704void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
3705{
3706 ASSERT(vcpu);
3707
3708 destroy_kvm_mmu(vcpu);
3709 free_mmu_pages(vcpu);
3710 mmu_free_memory_caches(vcpu);
Xiao Guangrongb034cf02010-12-23 16:08:35 +08003711}
3712
3713#ifdef CONFIG_KVM_MMU_AUDIT
3714#include "mmu_audit.c"
3715#else
3716static void mmu_audit_disable(void) { }
3717#endif
3718
3719void kvm_mmu_module_exit(void)
3720{
3721 mmu_destroy_caches();
3722 percpu_counter_destroy(&kvm_total_used_mmu_pages);
3723 unregister_shrinker(&mmu_shrinker);
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08003724 mmu_audit_disable();
3725}