blob: 91d30695677b2b9e9e84a5c4fc9b7452335a0d32 [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 Kivity6aa8b732006-12-10 02:21:36 -0800220static int is_cpuid_PSE36(void)
221{
222 return 1;
223}
224
Avi Kivity73b10872007-01-26 00:56:41 -0800225static int is_nx(struct kvm_vcpu *vcpu)
226{
Avi Kivityf6801df2010-01-21 15:31:50 +0200227 return vcpu->arch.efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800228}
229
Avi Kivityc7addb92007-09-16 18:58:32 +0200230static int is_shadow_present_pte(u64 pte)
231{
Avi Kivityc7addb92007-09-16 18:58:32 +0200232 return pte != shadow_trap_nonpresent_pte
233 && pte != shadow_notrap_nonpresent_pte;
234}
235
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300236static int is_large_pte(u64 pte)
237{
238 return pte & PT_PAGE_SIZE_MASK;
239}
240
Avi Kivity43a37952009-06-10 14:12:05 +0300241static int is_dirty_gpte(unsigned long pte)
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200242{
Avi Kivity439e2182009-06-10 12:56:54 +0300243 return pte & PT_DIRTY_MASK;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200244}
245
Avi Kivity43a37952009-06-10 14:12:05 +0300246static int is_rmap_spte(u64 pte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800247{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200248 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800249}
250
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300251static int is_last_spte(u64 pte, int level)
252{
253 if (level == PT_PAGE_TABLE_LEVEL)
254 return 1;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200255 if (is_large_pte(pte))
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300256 return 1;
257 return 0;
258}
259
Anthony Liguori35149e22008-04-02 14:46:56 -0500260static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200261{
Anthony Liguori35149e22008-04-02 14:46:56 -0500262 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200263}
264
Avi Kivityda928522007-11-21 13:54:47 +0200265static gfn_t pse36_gfn_delta(u32 gpte)
266{
267 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
268
269 return (gpte & PT32_DIR_PSE36_MASK) << shift;
270}
271
Avi Kivityd555c332009-06-10 14:24:23 +0300272static void __set_spte(u64 *sptep, u64 spte)
Avi Kivitye663ee62007-05-31 15:46:04 +0300273{
H. Peter Anvin7645e432010-08-06 12:18:11 -0700274 set_64bit(sptep, spte);
Avi Kivitye663ee62007-05-31 15:46:04 +0300275}
276
Avi Kivitya9221dd2010-06-06 14:48:06 +0300277static u64 __xchg_spte(u64 *sptep, u64 new_spte)
278{
279#ifdef CONFIG_X86_64
280 return xchg(sptep, new_spte);
281#else
282 u64 old_spte;
283
284 do {
285 old_spte = *sptep;
286 } while (cmpxchg64(sptep, old_spte, new_spte) != old_spte);
287
288 return old_spte;
289#endif
290}
291
Xiao Guangrong8672b722010-08-02 16:14:04 +0800292static bool spte_has_volatile_bits(u64 spte)
293{
294 if (!shadow_accessed_mask)
295 return false;
296
297 if (!is_shadow_present_pte(spte))
298 return false;
299
Xiao Guangrong41327792010-08-02 16:15:08 +0800300 if ((spte & shadow_accessed_mask) &&
301 (!is_writable_pte(spte) || (spte & shadow_dirty_mask)))
Xiao Guangrong8672b722010-08-02 16:14:04 +0800302 return false;
303
304 return true;
305}
306
Xiao Guangrong41327792010-08-02 16:15:08 +0800307static bool spte_is_bit_cleared(u64 old_spte, u64 new_spte, u64 bit_mask)
308{
309 return (old_spte & bit_mask) && !(new_spte & bit_mask);
310}
311
Avi Kivityb79b93f2010-06-06 15:46:44 +0300312static void update_spte(u64 *sptep, u64 new_spte)
313{
Xiao Guangrong41327792010-08-02 16:15:08 +0800314 u64 mask, old_spte = *sptep;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300315
Xiao Guangrong41327792010-08-02 16:15:08 +0800316 WARN_ON(!is_rmap_spte(new_spte));
317
318 new_spte |= old_spte & shadow_dirty_mask;
319
320 mask = shadow_accessed_mask;
321 if (is_writable_pte(old_spte))
322 mask |= shadow_dirty_mask;
323
324 if (!spte_has_volatile_bits(old_spte) || (new_spte & mask) == mask)
Avi Kivityb79b93f2010-06-06 15:46:44 +0300325 __set_spte(sptep, new_spte);
Xiao Guangrong41327792010-08-02 16:15:08 +0800326 else
Avi Kivityb79b93f2010-06-06 15:46:44 +0300327 old_spte = __xchg_spte(sptep, new_spte);
Xiao Guangrong41327792010-08-02 16:15:08 +0800328
329 if (!shadow_accessed_mask)
330 return;
331
332 if (spte_is_bit_cleared(old_spte, new_spte, shadow_accessed_mask))
333 kvm_set_pfn_accessed(spte_to_pfn(old_spte));
334 if (spte_is_bit_cleared(old_spte, new_spte, shadow_dirty_mask))
335 kvm_set_pfn_dirty(spte_to_pfn(old_spte));
Avi Kivityb79b93f2010-06-06 15:46:44 +0300336}
337
Avi Kivitye2dec932007-01-05 16:36:54 -0800338static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300339 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800340{
341 void *obj;
342
343 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800344 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800345 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300346 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800347 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800348 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800349 cache->objects[cache->nobjs++] = obj;
350 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800351 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800352}
353
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800354static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc,
355 struct kmem_cache *cache)
Avi Kivity714b93d2007-01-05 16:36:53 -0800356{
357 while (mc->nobjs)
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800358 kmem_cache_free(cache, mc->objects[--mc->nobjs]);
Avi Kivity714b93d2007-01-05 16:36:53 -0800359}
360
Avi Kivityc1158e62007-07-20 08:18:27 +0300361static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300362 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300363{
Xiao Guangrong842f22e2011-03-04 19:01:10 +0800364 void *page;
Avi Kivityc1158e62007-07-20 08:18:27 +0300365
366 if (cache->nobjs >= min)
367 return 0;
368 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Xiao Guangrong842f22e2011-03-04 19:01:10 +0800369 page = (void *)__get_free_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300370 if (!page)
371 return -ENOMEM;
Xiao Guangrong842f22e2011-03-04 19:01:10 +0800372 cache->objects[cache->nobjs++] = page;
Avi Kivityc1158e62007-07-20 08:18:27 +0300373 }
374 return 0;
375}
376
377static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
378{
379 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300380 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300381}
382
Avi Kivity8c438502007-04-16 11:53:17 +0300383static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
384{
385 int r;
386
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800387 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
Xiao Guangrong67052b32011-05-15 23:27:08 +0800388 pte_list_desc_cache, 8 + PTE_PREFETCH_NUM);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300389 if (r)
390 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800391 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300392 if (r)
393 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800394 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300395 mmu_page_header_cache, 4);
396out:
Avi Kivity8c438502007-04-16 11:53:17 +0300397 return r;
398}
399
Avi Kivity714b93d2007-01-05 16:36:53 -0800400static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
401{
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800402 mmu_free_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
403 pte_list_desc_cache);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800404 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800405 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache,
406 mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800407}
408
409static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
410 size_t size)
411{
412 void *p;
413
414 BUG_ON(!mc->nobjs);
415 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800416 return p;
417}
418
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800419static struct pte_list_desc *mmu_alloc_pte_list_desc(struct kvm_vcpu *vcpu)
Avi Kivity714b93d2007-01-05 16:36:53 -0800420{
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800421 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_list_desc_cache,
422 sizeof(struct pte_list_desc));
Avi Kivity714b93d2007-01-05 16:36:53 -0800423}
424
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800425static void mmu_free_pte_list_desc(struct pte_list_desc *pte_list_desc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800426{
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800427 kmem_cache_free(pte_list_desc_cache, pte_list_desc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800428}
429
Lai Jiangshan2032a932010-05-26 16:49:59 +0800430static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
431{
432 if (!sp->role.direct)
433 return sp->gfns[index];
434
435 return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS));
436}
437
438static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn)
439{
440 if (sp->role.direct)
441 BUG_ON(gfn != kvm_mmu_page_get_gfn(sp, index));
442 else
443 sp->gfns[index] = gfn;
444}
445
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800446/*
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900447 * Return the pointer to the large page information for a given gfn,
448 * handling slots that are not large page aligned.
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300449 */
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900450static struct kvm_lpage_info *lpage_info_slot(gfn_t gfn,
451 struct kvm_memory_slot *slot,
452 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300453{
454 unsigned long idx;
455
Joerg Roedel82855412010-07-01 16:00:11 +0200456 idx = (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
457 (slot->base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900458 return &slot->lpage_info[level - 2][idx];
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300459}
460
461static void account_shadowed(struct kvm *kvm, gfn_t gfn)
462{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200463 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900464 struct kvm_lpage_info *linfo;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200465 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300466
Avi Kivitya1f4d392010-06-21 11:44:20 +0300467 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200468 for (i = PT_DIRECTORY_LEVEL;
469 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900470 linfo = lpage_info_slot(gfn, slot, i);
471 linfo->write_count += 1;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200472 }
Xiao Guangrong332b2072011-05-15 23:20:27 +0800473 kvm->arch.indirect_shadow_pages++;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300474}
475
476static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
477{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200478 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900479 struct kvm_lpage_info *linfo;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200480 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300481
Avi Kivitya1f4d392010-06-21 11:44:20 +0300482 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200483 for (i = PT_DIRECTORY_LEVEL;
484 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900485 linfo = lpage_info_slot(gfn, slot, i);
486 linfo->write_count -= 1;
487 WARN_ON(linfo->write_count < 0);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200488 }
Xiao Guangrong332b2072011-05-15 23:20:27 +0800489 kvm->arch.indirect_shadow_pages--;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300490}
491
Joerg Roedeld25797b2009-07-27 16:30:43 +0200492static int has_wrprotected_page(struct kvm *kvm,
493 gfn_t gfn,
494 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300495{
Izik Eidus28430992008-10-03 17:40:32 +0300496 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900497 struct kvm_lpage_info *linfo;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300498
Avi Kivitya1f4d392010-06-21 11:44:20 +0300499 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300500 if (slot) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900501 linfo = lpage_info_slot(gfn, slot, level);
502 return linfo->write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300503 }
504
505 return 1;
506}
507
Joerg Roedeld25797b2009-07-27 16:30:43 +0200508static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300509{
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100510 unsigned long page_size;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200511 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300512
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100513 page_size = kvm_host_page_size(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300514
Joerg Roedeld25797b2009-07-27 16:30:43 +0200515 for (i = PT_PAGE_TABLE_LEVEL;
516 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
517 if (page_size >= KVM_HPAGE_SIZE(i))
518 ret = i;
519 else
520 break;
521 }
522
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300523 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300524}
525
Xiao Guangrong5d163b12011-03-09 15:43:00 +0800526static struct kvm_memory_slot *
527gfn_to_memslot_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t gfn,
528 bool no_dirty_log)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300529{
530 struct kvm_memory_slot *slot;
Xiao Guangrong5d163b12011-03-09 15:43:00 +0800531
532 slot = gfn_to_memslot(vcpu->kvm, gfn);
533 if (!slot || slot->flags & KVM_MEMSLOT_INVALID ||
534 (no_dirty_log && slot->dirty_bitmap))
535 slot = NULL;
536
537 return slot;
538}
539
540static bool mapping_level_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t large_gfn)
541{
Stevea0a8eab2011-06-17 10:25:39 +0800542 return !gfn_to_memslot_dirty_bitmap(vcpu, large_gfn, true);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800543}
544
545static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
546{
547 int host_level, level, max_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300548
Joerg Roedeld25797b2009-07-27 16:30:43 +0200549 host_level = host_mapping_level(vcpu->kvm, large_gfn);
550
551 if (host_level == PT_PAGE_TABLE_LEVEL)
552 return host_level;
553
Sheng Yang878403b2010-01-05 19:02:29 +0800554 max_level = kvm_x86_ops->get_lpage_level() < host_level ?
555 kvm_x86_ops->get_lpage_level() : host_level;
556
557 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200558 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
559 break;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200560
561 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300562}
563
564/*
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800565 * Pte mapping structures:
566 *
567 * If pte_list bit zero is zero, then pte_list point to the spte.
568 *
569 * If pte_list bit zero is one, (then pte_list & ~1) points to a struct
570 * pte_list_desc containing more mappings.
571 *
572 * Returns the number of pte entries before the spte was added or zero if
573 * the spte was not added.
574 *
575 */
576static int pte_list_add(struct kvm_vcpu *vcpu, u64 *spte,
577 unsigned long *pte_list)
578{
579 struct pte_list_desc *desc;
580 int i, count = 0;
581
582 if (!*pte_list) {
583 rmap_printk("pte_list_add: %p %llx 0->1\n", spte, *spte);
584 *pte_list = (unsigned long)spte;
585 } else if (!(*pte_list & 1)) {
586 rmap_printk("pte_list_add: %p %llx 1->many\n", spte, *spte);
587 desc = mmu_alloc_pte_list_desc(vcpu);
588 desc->sptes[0] = (u64 *)*pte_list;
589 desc->sptes[1] = spte;
590 *pte_list = (unsigned long)desc | 1;
591 ++count;
592 } else {
593 rmap_printk("pte_list_add: %p %llx many->many\n", spte, *spte);
594 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
595 while (desc->sptes[PTE_LIST_EXT-1] && desc->more) {
596 desc = desc->more;
597 count += PTE_LIST_EXT;
598 }
599 if (desc->sptes[PTE_LIST_EXT-1]) {
600 desc->more = mmu_alloc_pte_list_desc(vcpu);
601 desc = desc->more;
602 }
603 for (i = 0; desc->sptes[i]; ++i)
604 ++count;
605 desc->sptes[i] = spte;
606 }
607 return count;
608}
609
610static u64 *pte_list_next(unsigned long *pte_list, u64 *spte)
611{
612 struct pte_list_desc *desc;
613 u64 *prev_spte;
614 int i;
615
616 if (!*pte_list)
617 return NULL;
618 else if (!(*pte_list & 1)) {
619 if (!spte)
620 return (u64 *)*pte_list;
621 return NULL;
622 }
623 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
624 prev_spte = NULL;
625 while (desc) {
626 for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i) {
627 if (prev_spte == spte)
628 return desc->sptes[i];
629 prev_spte = desc->sptes[i];
630 }
631 desc = desc->more;
632 }
633 return NULL;
634}
635
636static void
637pte_list_desc_remove_entry(unsigned long *pte_list, struct pte_list_desc *desc,
638 int i, struct pte_list_desc *prev_desc)
639{
640 int j;
641
642 for (j = PTE_LIST_EXT - 1; !desc->sptes[j] && j > i; --j)
643 ;
644 desc->sptes[i] = desc->sptes[j];
645 desc->sptes[j] = NULL;
646 if (j != 0)
647 return;
648 if (!prev_desc && !desc->more)
649 *pte_list = (unsigned long)desc->sptes[0];
650 else
651 if (prev_desc)
652 prev_desc->more = desc->more;
653 else
654 *pte_list = (unsigned long)desc->more | 1;
655 mmu_free_pte_list_desc(desc);
656}
657
658static void pte_list_remove(u64 *spte, unsigned long *pte_list)
659{
660 struct pte_list_desc *desc;
661 struct pte_list_desc *prev_desc;
662 int i;
663
664 if (!*pte_list) {
665 printk(KERN_ERR "pte_list_remove: %p 0->BUG\n", spte);
666 BUG();
667 } else if (!(*pte_list & 1)) {
668 rmap_printk("pte_list_remove: %p 1->0\n", spte);
669 if ((u64 *)*pte_list != spte) {
670 printk(KERN_ERR "pte_list_remove: %p 1->BUG\n", spte);
671 BUG();
672 }
673 *pte_list = 0;
674 } else {
675 rmap_printk("pte_list_remove: %p many->many\n", spte);
676 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
677 prev_desc = NULL;
678 while (desc) {
679 for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i)
680 if (desc->sptes[i] == spte) {
681 pte_list_desc_remove_entry(pte_list,
682 desc, i,
683 prev_desc);
684 return;
685 }
686 prev_desc = desc;
687 desc = desc->more;
688 }
689 pr_err("pte_list_remove: %p many->many\n", spte);
690 BUG();
691 }
692}
693
Xiao Guangrong67052b32011-05-15 23:27:08 +0800694typedef void (*pte_list_walk_fn) (u64 *spte);
695static void pte_list_walk(unsigned long *pte_list, pte_list_walk_fn fn)
696{
697 struct pte_list_desc *desc;
698 int i;
699
700 if (!*pte_list)
701 return;
702
703 if (!(*pte_list & 1))
704 return fn((u64 *)*pte_list);
705
706 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
707 while (desc) {
708 for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i)
709 fn(desc->sptes[i]);
710 desc = desc->more;
711 }
712}
713
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800714/*
Izik Eidus290fc382007-09-27 14:11:22 +0200715 * Take gfn and return the reverse mapping to it.
Izik Eidus290fc382007-09-27 14:11:22 +0200716 */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200717static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
Izik Eidus290fc382007-09-27 14:11:22 +0200718{
719 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900720 struct kvm_lpage_info *linfo;
Izik Eidus290fc382007-09-27 14:11:22 +0200721
722 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200723 if (likely(level == PT_PAGE_TABLE_LEVEL))
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300724 return &slot->rmap[gfn - slot->base_gfn];
725
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900726 linfo = lpage_info_slot(gfn, slot, level);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300727
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900728 return &linfo->rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200729}
730
Joerg Roedel44ad9942009-07-27 16:30:42 +0200731static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800732{
Avi Kivity4db35312007-11-21 15:28:32 +0200733 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +0200734 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800735
Avi Kivity4db35312007-11-21 15:28:32 +0200736 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800737 kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200738 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800739 return pte_list_add(vcpu, spte, rmapp);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800740}
741
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800742static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800743{
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800744 return pte_list_next(rmapp, spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800745}
746
Izik Eidus290fc382007-09-27 14:11:22 +0200747static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800748{
Avi Kivity4db35312007-11-21 15:28:32 +0200749 struct kvm_mmu_page *sp;
Lai Jiangshan2032a932010-05-26 16:49:59 +0800750 gfn_t gfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200751 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800752
Avi Kivity4db35312007-11-21 15:28:32 +0200753 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800754 gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt);
755 rmapp = gfn_to_rmap(kvm, gfn, sp->role.level);
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800756 pte_list_remove(spte, rmapp);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800757}
758
Marcelo Tosattieb45fda2010-10-25 11:58:22 -0200759static int set_spte_track_bits(u64 *sptep, u64 new_spte)
Avi Kivitybe38d272010-06-06 14:31:27 +0300760{
Avi Kivityce061862010-06-06 14:38:12 +0300761 pfn_t pfn;
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800762 u64 old_spte = *sptep;
Avi Kivityce061862010-06-06 14:38:12 +0300763
Xiao Guangrong8672b722010-08-02 16:14:04 +0800764 if (!spte_has_volatile_bits(old_spte))
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800765 __set_spte(sptep, new_spte);
Xiao Guangrong8672b722010-08-02 16:14:04 +0800766 else
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800767 old_spte = __xchg_spte(sptep, new_spte);
768
Avi Kivitya9221dd2010-06-06 14:48:06 +0300769 if (!is_rmap_spte(old_spte))
Marcelo Tosattieb45fda2010-10-25 11:58:22 -0200770 return 0;
Xiao Guangrong8672b722010-08-02 16:14:04 +0800771
Avi Kivitya9221dd2010-06-06 14:48:06 +0300772 pfn = spte_to_pfn(old_spte);
Xiao Guangrongdaa3db62010-07-16 11:23:04 +0800773 if (!shadow_accessed_mask || old_spte & shadow_accessed_mask)
Avi Kivityce061862010-06-06 14:38:12 +0300774 kvm_set_pfn_accessed(pfn);
Xiao Guangrong41327792010-08-02 16:15:08 +0800775 if (!shadow_dirty_mask || (old_spte & shadow_dirty_mask))
Avi Kivityce061862010-06-06 14:38:12 +0300776 kvm_set_pfn_dirty(pfn);
Marcelo Tosattieb45fda2010-10-25 11:58:22 -0200777 return 1;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800778}
779
780static void drop_spte(struct kvm *kvm, u64 *sptep, u64 new_spte)
781{
Marcelo Tosattieb45fda2010-10-25 11:58:22 -0200782 if (set_spte_track_bits(sptep, new_spte))
783 rmap_remove(kvm, sptep);
Avi Kivitybe38d272010-06-06 14:31:27 +0300784}
785
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200786static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200787{
Izik Eidus290fc382007-09-27 14:11:22 +0200788 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800789 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200790 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800791
Joerg Roedel44ad9942009-07-27 16:30:42 +0200792 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
Avi Kivity374cbac2007-01-05 16:36:43 -0800793
Izik Eidus98348e92007-10-16 14:42:30 +0200794 spte = rmap_next(kvm, rmapp, NULL);
795 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800796 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800797 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800798 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900799 if (is_writable_pte(*spte)) {
Avi Kivityb79b93f2010-06-06 15:46:44 +0300800 update_spte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800801 write_protected = 1;
802 }
Izik Eidus9647c142007-10-16 14:43:46 +0200803 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800804 }
Izik Eidus855149a2008-03-20 18:17:24 +0200805
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300806 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200807 for (i = PT_DIRECTORY_LEVEL;
808 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
809 rmapp = gfn_to_rmap(kvm, gfn, i);
810 spte = rmap_next(kvm, rmapp, NULL);
811 while (spte) {
812 BUG_ON(!spte);
813 BUG_ON(!(*spte & PT_PRESENT_MASK));
814 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
815 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900816 if (is_writable_pte(*spte)) {
Avi Kivitybe38d272010-06-06 14:31:27 +0300817 drop_spte(kvm, spte,
818 shadow_trap_nonpresent_pte);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200819 --kvm->stat.lpages;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200820 spte = NULL;
821 write_protected = 1;
822 }
823 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300824 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300825 }
826
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200827 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800828}
829
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000830static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
831 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200832{
833 u64 *spte;
834 int need_tlb_flush = 0;
835
836 while ((spte = rmap_next(kvm, rmapp, NULL))) {
837 BUG_ON(!(*spte & PT_PRESENT_MASK));
838 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
Avi Kivitybe38d272010-06-06 14:31:27 +0300839 drop_spte(kvm, spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200840 need_tlb_flush = 1;
841 }
842 return need_tlb_flush;
843}
844
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000845static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
846 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +0300847{
848 int need_flush = 0;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800849 u64 *spte, new_spte;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300850 pte_t *ptep = (pte_t *)data;
851 pfn_t new_pfn;
852
853 WARN_ON(pte_huge(*ptep));
854 new_pfn = pte_pfn(*ptep);
855 spte = rmap_next(kvm, rmapp, NULL);
856 while (spte) {
857 BUG_ON(!is_shadow_present_pte(*spte));
858 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
859 need_flush = 1;
860 if (pte_write(*ptep)) {
Avi Kivitybe38d272010-06-06 14:31:27 +0300861 drop_spte(kvm, spte, shadow_trap_nonpresent_pte);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300862 spte = rmap_next(kvm, rmapp, NULL);
863 } else {
864 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
865 new_spte |= (u64)new_pfn << PAGE_SHIFT;
866
867 new_spte &= ~PT_WRITABLE_MASK;
868 new_spte &= ~SPTE_HOST_WRITEABLE;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300869 new_spte &= ~shadow_accessed_mask;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800870 set_spte_track_bits(spte, new_spte);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300871 spte = rmap_next(kvm, rmapp, spte);
872 }
873 }
874 if (need_flush)
875 kvm_flush_remote_tlbs(kvm);
876
877 return 0;
878}
879
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000880static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
881 unsigned long data,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300882 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000883 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200884{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200885 int i, j;
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200886 int ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200887 int retval = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200888 struct kvm_memslots *slots;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200889
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800890 slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200891
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200892 for (i = 0; i < slots->nmemslots; i++) {
893 struct kvm_memory_slot *memslot = &slots->memslots[i];
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200894 unsigned long start = memslot->userspace_addr;
895 unsigned long end;
896
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200897 end = start + (memslot->npages << PAGE_SHIFT);
898 if (hva >= start && hva < end) {
899 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900900 gfn_t gfn = memslot->base_gfn + gfn_offset;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200901
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200902 ret = handler(kvm, &memslot->rmap[gfn_offset], data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200903
904 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900905 struct kvm_lpage_info *linfo;
Andrea Arcangeli6e3e2432010-07-16 11:52:55 +0200906
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900907 linfo = lpage_info_slot(gfn, memslot,
908 PT_DIRECTORY_LEVEL + j);
909 ret |= handler(kvm, &linfo->rmap_pde, data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200910 }
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200911 trace_kvm_age_page(hva, memslot, ret);
912 retval |= ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200913 }
914 }
915
916 return retval;
917}
918
919int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
920{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300921 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200922}
923
Izik Eidus3da0dd42009-09-23 21:47:18 +0300924void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
925{
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000926 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300927}
928
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000929static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
930 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200931{
932 u64 *spte;
933 int young = 0;
934
Rik van Riel6316e1c2010-02-03 16:11:03 -0500935 /*
936 * Emulate the accessed bit for EPT, by checking if this page has
937 * an EPT mapping, and clearing it if it does. On the next access,
938 * a new EPT mapping will be established.
939 * This has some overhead, but not as much as the cost of swapping
940 * out actively used pages or breaking up actively used hugepages.
941 */
Sheng Yang534e38b2008-09-08 15:12:30 +0800942 if (!shadow_accessed_mask)
Rik van Riel6316e1c2010-02-03 16:11:03 -0500943 return kvm_unmap_rmapp(kvm, rmapp, data);
Sheng Yang534e38b2008-09-08 15:12:30 +0800944
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200945 spte = rmap_next(kvm, rmapp, NULL);
946 while (spte) {
947 int _young;
948 u64 _spte = *spte;
949 BUG_ON(!(_spte & PT_PRESENT_MASK));
950 _young = _spte & PT_ACCESSED_MASK;
951 if (_young) {
952 young = 1;
953 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
954 }
955 spte = rmap_next(kvm, rmapp, spte);
956 }
957 return young;
958}
959
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800960static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
961 unsigned long data)
962{
963 u64 *spte;
964 int young = 0;
965
966 /*
967 * If there's no access bit in the secondary pte set by the
968 * hardware it's up to gup-fast/gup to set the access bit in
969 * the primary pte or in the page structure.
970 */
971 if (!shadow_accessed_mask)
972 goto out;
973
974 spte = rmap_next(kvm, rmapp, NULL);
975 while (spte) {
976 u64 _spte = *spte;
977 BUG_ON(!(_spte & PT_PRESENT_MASK));
978 young = _spte & PT_ACCESSED_MASK;
979 if (young) {
980 young = 1;
981 break;
982 }
983 spte = rmap_next(kvm, rmapp, spte);
984 }
985out:
986 return young;
987}
988
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300989#define RMAP_RECYCLE_THRESHOLD 1000
990
Joerg Roedel852e3c12009-07-27 16:30:44 +0200991static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300992{
993 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200994 struct kvm_mmu_page *sp;
995
996 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300997
Joerg Roedel852e3c12009-07-27 16:30:44 +0200998 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300999
Izik Eidus3da0dd42009-09-23 21:47:18 +03001000 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001001 kvm_flush_remote_tlbs(vcpu->kvm);
1002}
1003
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001004int kvm_age_hva(struct kvm *kvm, unsigned long hva)
1005{
Izik Eidus3da0dd42009-09-23 21:47:18 +03001006 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001007}
1008
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001009int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
1010{
1011 return kvm_handle_hva(kvm, hva, 0, kvm_test_age_rmapp);
1012}
1013
Yaozu Dongd6c69ee2007-04-25 14:17:25 +08001014#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +03001015static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016{
Avi Kivity139bdb22007-01-05 16:36:50 -08001017 u64 *pos;
1018 u64 *end;
1019
Avi Kivity47ad8e62007-05-06 15:50:58 +03001020 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +03001021 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001022 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -08001023 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001024 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -08001025 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026 return 1;
1027}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +08001028#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001029
Dave Hansen45221ab2010-08-19 18:11:37 -07001030/*
1031 * This value is the sum of all of the kvm instances's
1032 * kvm->arch.n_used_mmu_pages values. We need a global,
1033 * aggregate version in order to make the slab shrinker
1034 * faster
1035 */
1036static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, int nr)
1037{
1038 kvm->arch.n_used_mmu_pages += nr;
1039 percpu_counter_add(&kvm_total_used_mmu_pages, nr);
1040}
1041
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08001042static void kvm_mmu_free_page(struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -08001043{
Avi Kivity4db35312007-11-21 15:28:32 +02001044 ASSERT(is_empty_shadow_page(sp->spt));
Xiao Guangrong77758342010-06-04 21:53:54 +08001045 hlist_del(&sp->hash_link);
Avi Kivity4db35312007-11-21 15:28:32 +02001046 list_del(&sp->link);
Xiao Guangrong842f22e2011-03-04 19:01:10 +08001047 free_page((unsigned long)sp->spt);
Lai Jiangshan2032a932010-05-26 16:49:59 +08001048 if (!sp->role.direct)
Xiao Guangrong842f22e2011-03-04 19:01:10 +08001049 free_page((unsigned long)sp->gfns);
Xiao Guangronge8ad9a72010-05-13 10:06:02 +08001050 kmem_cache_free(mmu_page_header_cache, sp);
Avi Kivity260746c2007-01-05 16:36:49 -08001051}
1052
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001053static unsigned kvm_page_table_hashfn(gfn_t gfn)
1054{
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001055 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001056}
1057
Xiao Guangrong67052b32011-05-15 23:27:08 +08001058static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
1059 struct kvm_mmu_page *sp, u64 *parent_pte)
1060{
1061 if (!parent_pte)
1062 return;
1063
1064 pte_list_add(vcpu, parent_pte, &sp->parent_ptes);
1065}
1066
1067static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
1068 u64 *parent_pte)
1069{
1070 pte_list_remove(parent_pte, &sp->parent_ptes);
1071}
1072
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001073static void drop_parent_pte(struct kvm_mmu_page *sp,
1074 u64 *parent_pte)
1075{
1076 mmu_page_remove_parent_pte(sp, parent_pte);
1077 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
1078}
1079
Avi Kivity25c0de22007-01-05 16:36:42 -08001080static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
Lai Jiangshan2032a932010-05-26 16:49:59 +08001081 u64 *parent_pte, int direct)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001082{
Avi Kivity4db35312007-11-21 15:28:32 +02001083 struct kvm_mmu_page *sp;
Xiao Guangrong67052b32011-05-15 23:27:08 +08001084 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache,
1085 sizeof *sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001086 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Lai Jiangshan2032a932010-05-26 16:49:59 +08001087 if (!direct)
1088 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache,
1089 PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +02001090 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001091 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Sheng Yang291f26b2008-10-16 17:30:57 +08001092 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Xiao Guangrong67052b32011-05-15 23:27:08 +08001093 sp->parent_ptes = 0;
1094 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Dave Hansen45221ab2010-08-19 18:11:37 -07001095 kvm_mod_used_mmu_pages(vcpu->kvm, +1);
Avi Kivity4db35312007-11-21 15:28:32 +02001096 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001097}
1098
Xiao Guangrong67052b32011-05-15 23:27:08 +08001099static void mark_unsync(u64 *spte);
Xiao Guangrong6b184932010-04-16 21:29:17 +08001100static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001101{
Xiao Guangrong67052b32011-05-15 23:27:08 +08001102 pte_list_walk(&sp->parent_ptes, mark_unsync);
Xiao Guangrong1047df12010-06-11 21:35:15 +08001103}
1104
Xiao Guangrong67052b32011-05-15 23:27:08 +08001105static void mark_unsync(u64 *spte)
Xiao Guangrong1047df12010-06-11 21:35:15 +08001106{
Xiao Guangrong67052b32011-05-15 23:27:08 +08001107 struct kvm_mmu_page *sp;
Xiao Guangrong1047df12010-06-11 21:35:15 +08001108 unsigned int index;
1109
Xiao Guangrong67052b32011-05-15 23:27:08 +08001110 sp = page_header(__pa(spte));
Xiao Guangrong1047df12010-06-11 21:35:15 +08001111 index = spte - sp->spt;
1112 if (__test_and_set_bit(index, sp->unsync_child_bitmap))
1113 return;
1114 if (sp->unsync_children++)
1115 return;
1116 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001117}
1118
Avi Kivityd761a502008-05-29 14:55:03 +03001119static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1120 struct kvm_mmu_page *sp)
1121{
1122 int i;
1123
1124 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1125 sp->spt[i] = shadow_trap_nonpresent_pte;
1126}
1127
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001128static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001129 struct kvm_mmu_page *sp)
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001130{
1131 return 1;
1132}
1133
Marcelo Tosattia7052892008-09-23 13:18:35 -03001134static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1135{
1136}
1137
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08001138static void nonpaging_update_pte(struct kvm_vcpu *vcpu,
1139 struct kvm_mmu_page *sp, u64 *spte,
Xiao Guangrong7c562522011-03-28 10:29:27 +08001140 const void *pte)
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08001141{
1142 WARN_ON(1);
1143}
1144
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001145#define KVM_PAGE_ARRAY_NR 16
1146
1147struct kvm_mmu_pages {
1148 struct mmu_page_and_offset {
1149 struct kvm_mmu_page *sp;
1150 unsigned int idx;
1151 } page[KVM_PAGE_ARRAY_NR];
1152 unsigned int nr;
1153};
1154
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001155#define for_each_unsync_children(bitmap, idx) \
1156 for (idx = find_first_bit(bitmap, 512); \
1157 idx < 512; \
1158 idx = find_next_bit(bitmap, 512, idx+1))
1159
Hannes Edercded19f2009-02-21 02:19:13 +01001160static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1161 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001162{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001163 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001164
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001165 if (sp->unsync)
1166 for (i=0; i < pvec->nr; i++)
1167 if (pvec->page[i].sp == sp)
1168 return 0;
1169
1170 pvec->page[pvec->nr].sp = sp;
1171 pvec->page[pvec->nr].idx = idx;
1172 pvec->nr++;
1173 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1174}
1175
1176static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1177 struct kvm_mmu_pages *pvec)
1178{
1179 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001180
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001181 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001182 struct kvm_mmu_page *child;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001183 u64 ent = sp->spt[i];
1184
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001185 if (!is_shadow_present_pte(ent) || is_large_pte(ent))
1186 goto clear_child_bitmap;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001187
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001188 child = page_header(ent & PT64_BASE_ADDR_MASK);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001189
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001190 if (child->unsync_children) {
1191 if (mmu_pages_add(pvec, child, i))
1192 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001193
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001194 ret = __mmu_unsync_walk(child, pvec);
1195 if (!ret)
1196 goto clear_child_bitmap;
1197 else if (ret > 0)
1198 nr_unsync_leaf += ret;
1199 else
1200 return ret;
1201 } else if (child->unsync) {
1202 nr_unsync_leaf++;
1203 if (mmu_pages_add(pvec, child, i))
1204 return -ENOSPC;
1205 } else
1206 goto clear_child_bitmap;
1207
1208 continue;
1209
1210clear_child_bitmap:
1211 __clear_bit(i, sp->unsync_child_bitmap);
1212 sp->unsync_children--;
1213 WARN_ON((int)sp->unsync_children < 0);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001214 }
1215
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001216
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001217 return nr_unsync_leaf;
1218}
1219
1220static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1221 struct kvm_mmu_pages *pvec)
1222{
1223 if (!sp->unsync_children)
1224 return 0;
1225
1226 mmu_pages_add(pvec, sp, 0);
1227 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001228}
1229
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001230static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1231{
1232 WARN_ON(!sp->unsync);
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001233 trace_kvm_mmu_sync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001234 sp->unsync = 0;
1235 --kvm->stat.mmu_unsync;
1236}
1237
Xiao Guangrong77758342010-06-04 21:53:54 +08001238static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1239 struct list_head *invalid_list);
1240static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1241 struct list_head *invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001242
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001243#define for_each_gfn_sp(kvm, sp, gfn, pos) \
1244 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001245 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1246 if ((sp)->gfn != (gfn)) {} else
1247
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001248#define for_each_gfn_indirect_valid_sp(kvm, sp, gfn, pos) \
1249 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001250 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1251 if ((sp)->gfn != (gfn) || (sp)->role.direct || \
1252 (sp)->role.invalid) {} else
1253
Xiao Guangrongf918b442010-06-11 21:30:36 +08001254/* @sp->gfn should be write-protected at the call site */
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001255static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001256 struct list_head *invalid_list, bool clear_unsync)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001257{
Avi Kivity5b7e0102010-04-14 19:20:03 +03001258 if (sp->role.cr4_pae != !!is_pae(vcpu)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001259 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001260 return 1;
1261 }
1262
Xiao Guangrongf918b442010-06-11 21:30:36 +08001263 if (clear_unsync)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001264 kvm_unlink_unsync_page(vcpu->kvm, sp);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001265
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001266 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001267 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001268 return 1;
1269 }
1270
1271 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001272 return 0;
1273}
1274
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001275static int kvm_sync_page_transient(struct kvm_vcpu *vcpu,
1276 struct kvm_mmu_page *sp)
1277{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001278 LIST_HEAD(invalid_list);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001279 int ret;
1280
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001281 ret = __kvm_sync_page(vcpu, sp, &invalid_list, false);
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001282 if (ret)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001283 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
1284
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001285 return ret;
1286}
1287
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001288static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1289 struct list_head *invalid_list)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001290{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001291 return __kvm_sync_page(vcpu, sp, invalid_list, true);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001292}
1293
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001294/* @gfn should be write-protected at the call site */
1295static void kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1296{
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001297 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001298 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001299 LIST_HEAD(invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001300 bool flush = false;
1301
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001302 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001303 if (!s->unsync)
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001304 continue;
1305
1306 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001307 kvm_unlink_unsync_page(vcpu->kvm, s);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001308 if ((s->role.cr4_pae != !!is_pae(vcpu)) ||
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001309 (vcpu->arch.mmu.sync_page(vcpu, s))) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001310 kvm_mmu_prepare_zap_page(vcpu->kvm, s, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001311 continue;
1312 }
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001313 flush = true;
1314 }
1315
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001316 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001317 if (flush)
1318 kvm_mmu_flush_tlb(vcpu);
1319}
1320
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001321struct mmu_page_path {
1322 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1323 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001324};
1325
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001326#define for_each_sp(pvec, sp, parents, i) \
1327 for (i = mmu_pages_next(&pvec, &parents, -1), \
1328 sp = pvec.page[i].sp; \
1329 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1330 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001331
Hannes Edercded19f2009-02-21 02:19:13 +01001332static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1333 struct mmu_page_path *parents,
1334 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001335{
1336 int n;
1337
1338 for (n = i+1; n < pvec->nr; n++) {
1339 struct kvm_mmu_page *sp = pvec->page[n].sp;
1340
1341 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1342 parents->idx[0] = pvec->page[n].idx;
1343 return n;
1344 }
1345
1346 parents->parent[sp->role.level-2] = sp;
1347 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1348 }
1349
1350 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001351}
1352
Hannes Edercded19f2009-02-21 02:19:13 +01001353static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001354{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001355 struct kvm_mmu_page *sp;
1356 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001357
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001358 do {
1359 unsigned int idx = parents->idx[level];
1360
1361 sp = parents->parent[level];
1362 if (!sp)
1363 return;
1364
1365 --sp->unsync_children;
1366 WARN_ON((int)sp->unsync_children < 0);
1367 __clear_bit(idx, sp->unsync_child_bitmap);
1368 level++;
1369 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1370}
1371
1372static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1373 struct mmu_page_path *parents,
1374 struct kvm_mmu_pages *pvec)
1375{
1376 parents->parent[parent->role.level-1] = NULL;
1377 pvec->nr = 0;
1378}
1379
1380static void mmu_sync_children(struct kvm_vcpu *vcpu,
1381 struct kvm_mmu_page *parent)
1382{
1383 int i;
1384 struct kvm_mmu_page *sp;
1385 struct mmu_page_path parents;
1386 struct kvm_mmu_pages pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001387 LIST_HEAD(invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001388
1389 kvm_mmu_pages_init(parent, &parents, &pages);
1390 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001391 int protected = 0;
1392
1393 for_each_sp(pages, sp, parents, i)
1394 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1395
1396 if (protected)
1397 kvm_flush_remote_tlbs(vcpu->kvm);
1398
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001399 for_each_sp(pages, sp, parents, i) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001400 kvm_sync_page(vcpu, sp, &invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001401 mmu_pages_clear_parents(&parents);
1402 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001403 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001404 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001405 kvm_mmu_pages_init(parent, &parents, &pages);
1406 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001407}
1408
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001409static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1410 gfn_t gfn,
1411 gva_t gaddr,
1412 unsigned level,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001413 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001414 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001415 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001416{
1417 union kvm_mmu_page_role role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001418 unsigned quadrant;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001419 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001420 struct hlist_node *node;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001421 bool need_sync = false;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001422
Avi Kivitya770f6f2008-12-21 19:20:09 +02001423 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001424 role.level = level;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001425 role.direct = direct;
Avi Kivity84b0c8c2010-03-14 10:16:40 +02001426 if (role.direct)
Avi Kivity5b7e0102010-04-14 19:20:03 +03001427 role.cr4_pae = 0;
Avi Kivity41074d02007-12-09 17:00:02 +02001428 role.access = access;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02001429 if (!vcpu->arch.mmu.direct_map
1430 && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001431 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1432 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1433 role.quadrant = quadrant;
1434 }
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001435 for_each_gfn_sp(vcpu->kvm, sp, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001436 if (!need_sync && sp->unsync)
1437 need_sync = true;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001438
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001439 if (sp->role.word != role.word)
1440 continue;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001441
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001442 if (sp->unsync && kvm_sync_page_transient(vcpu, sp))
1443 break;
Xiao Guangronge02aa902010-05-15 18:52:34 +08001444
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001445 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1446 if (sp->unsync_children) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001447 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001448 kvm_mmu_mark_parents_unsync(sp);
1449 } else if (sp->unsync)
1450 kvm_mmu_mark_parents_unsync(sp);
Xiao Guangronge02aa902010-05-15 18:52:34 +08001451
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001452 trace_kvm_mmu_get_page(sp, false);
1453 return sp;
1454 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001455 ++vcpu->kvm->stat.mmu_cache_miss;
Lai Jiangshan2032a932010-05-26 16:49:59 +08001456 sp = kvm_mmu_alloc_page(vcpu, parent_pte, direct);
Avi Kivity4db35312007-11-21 15:28:32 +02001457 if (!sp)
1458 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001459 sp->gfn = gfn;
1460 sp->role = role;
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001461 hlist_add_head(&sp->hash_link,
1462 &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001463 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001464 if (rmap_write_protect(vcpu->kvm, gfn))
1465 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001466 if (level > PT_PAGE_TABLE_LEVEL && need_sync)
1467 kvm_sync_pages(vcpu, gfn);
1468
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001469 account_shadowed(vcpu->kvm, gfn);
1470 }
Avi Kivity131d8272008-05-29 14:56:28 +03001471 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1472 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1473 else
1474 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001475 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001476 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001477}
1478
Avi Kivity2d111232008-12-25 14:39:47 +02001479static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1480 struct kvm_vcpu *vcpu, u64 addr)
1481{
1482 iterator->addr = addr;
1483 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1484 iterator->level = vcpu->arch.mmu.shadow_root_level;
Joerg Roedel81407ca2010-09-10 17:31:00 +02001485
1486 if (iterator->level == PT64_ROOT_LEVEL &&
1487 vcpu->arch.mmu.root_level < PT64_ROOT_LEVEL &&
1488 !vcpu->arch.mmu.direct_map)
1489 --iterator->level;
1490
Avi Kivity2d111232008-12-25 14:39:47 +02001491 if (iterator->level == PT32E_ROOT_LEVEL) {
1492 iterator->shadow_addr
1493 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1494 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1495 --iterator->level;
1496 if (!iterator->shadow_addr)
1497 iterator->level = 0;
1498 }
1499}
1500
1501static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1502{
1503 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1504 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001505
Avi Kivity2d111232008-12-25 14:39:47 +02001506 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1507 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1508 return true;
1509}
1510
1511static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1512{
Xiao Guangrong052331b2011-07-12 03:21:17 +08001513 if (is_last_spte(*iterator->sptep, iterator->level)) {
1514 iterator->level = 0;
1515 return;
1516 }
1517
Avi Kivity2d111232008-12-25 14:39:47 +02001518 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1519 --iterator->level;
1520}
1521
Avi Kivity32ef26a2010-07-13 14:27:04 +03001522static void link_shadow_page(u64 *sptep, struct kvm_mmu_page *sp)
1523{
1524 u64 spte;
1525
1526 spte = __pa(sp->spt)
1527 | PT_PRESENT_MASK | PT_ACCESSED_MASK
1528 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity121eee92010-07-13 14:27:05 +03001529 __set_spte(sptep, spte);
Avi Kivity32ef26a2010-07-13 14:27:04 +03001530}
1531
Avi Kivitya3aa51c2010-07-13 14:27:06 +03001532static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep)
1533{
1534 if (is_large_pte(*sptep)) {
1535 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
1536 kvm_flush_remote_tlbs(vcpu->kvm);
1537 }
1538}
1539
Avi Kivitya357bd22010-07-13 14:27:07 +03001540static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1541 unsigned direct_access)
1542{
1543 if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
1544 struct kvm_mmu_page *child;
1545
1546 /*
1547 * For the direct sp, if the guest pte's dirty bit
1548 * changed form clean to dirty, it will corrupt the
1549 * sp's access: allow writable in the read-only sp,
1550 * so we should update the spte at this point to get
1551 * a new sp with the correct access.
1552 */
1553 child = page_header(*sptep & PT64_BASE_ADDR_MASK);
1554 if (child->role.access == direct_access)
1555 return;
1556
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001557 drop_parent_pte(child, sptep);
Avi Kivitya357bd22010-07-13 14:27:07 +03001558 kvm_flush_remote_tlbs(vcpu->kvm);
1559 }
1560}
1561
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08001562static void mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,
1563 u64 *spte)
1564{
1565 u64 pte;
1566 struct kvm_mmu_page *child;
1567
1568 pte = *spte;
1569 if (is_shadow_present_pte(pte)) {
1570 if (is_last_spte(pte, sp->role.level))
1571 drop_spte(kvm, spte, shadow_trap_nonpresent_pte);
1572 else {
1573 child = page_header(pte & PT64_BASE_ADDR_MASK);
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001574 drop_parent_pte(child, spte);
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08001575 }
1576 }
1577 __set_spte(spte, shadow_trap_nonpresent_pte);
1578 if (is_large_pte(pte))
1579 --kvm->stat.lpages;
1580}
1581
Avi Kivity90cb0522007-07-17 13:04:56 +03001582static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001583 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001584{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001585 unsigned i;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001586
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08001587 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1588 mmu_page_zap_pte(kvm, sp, sp->spt + i);
Avi Kivitya4360362007-01-05 16:36:45 -08001589}
1590
Avi Kivity4db35312007-11-21 15:28:32 +02001591static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001592{
Avi Kivity4db35312007-11-21 15:28:32 +02001593 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001594}
1595
Avi Kivity12b7d282007-09-23 14:10:49 +02001596static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1597{
1598 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001599 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001600
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001601 kvm_for_each_vcpu(i, vcpu, kvm)
1602 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001603}
1604
Avi Kivity31aa2b42008-07-11 17:59:46 +03001605static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001606{
1607 u64 *parent_pte;
1608
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001609 while ((parent_pte = pte_list_next(&sp->parent_ptes, NULL)))
1610 drop_parent_pte(sp, parent_pte);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001611}
1612
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001613static int mmu_zap_unsync_children(struct kvm *kvm,
Xiao Guangrong77758342010-06-04 21:53:54 +08001614 struct kvm_mmu_page *parent,
1615 struct list_head *invalid_list)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001616{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001617 int i, zapped = 0;
1618 struct mmu_page_path parents;
1619 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001620
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001621 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001622 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001623
1624 kvm_mmu_pages_init(parent, &parents, &pages);
1625 while (mmu_unsync_walk(parent, &pages)) {
1626 struct kvm_mmu_page *sp;
1627
1628 for_each_sp(pages, sp, parents, i) {
Xiao Guangrong77758342010-06-04 21:53:54 +08001629 kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001630 mmu_pages_clear_parents(&parents);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001631 zapped++;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001632 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001633 kvm_mmu_pages_init(parent, &parents, &pages);
1634 }
1635
1636 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001637}
1638
Xiao Guangrong77758342010-06-04 21:53:54 +08001639static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1640 struct list_head *invalid_list)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001641{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001642 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001643
Xiao Guangrong77758342010-06-04 21:53:54 +08001644 trace_kvm_mmu_prepare_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001645 ++kvm->stat.mmu_shadow_zapped;
Xiao Guangrong77758342010-06-04 21:53:54 +08001646 ret = mmu_zap_unsync_children(kvm, sp, invalid_list);
Avi Kivity4db35312007-11-21 15:28:32 +02001647 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001648 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001649 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001650 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001651 if (sp->unsync)
1652 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001653 if (!sp->root_count) {
Gui Jianfeng54a4f022010-05-05 09:03:49 +08001654 /* Count self */
1655 ret++;
Xiao Guangrong77758342010-06-04 21:53:54 +08001656 list_move(&sp->link, invalid_list);
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08001657 kvm_mod_used_mmu_pages(kvm, -1);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001658 } else {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001659 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001660 kvm_reload_remote_mmus(kvm);
1661 }
Xiao Guangrong77758342010-06-04 21:53:54 +08001662
1663 sp->role.invalid = 1;
Avi Kivity12b7d282007-09-23 14:10:49 +02001664 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001665 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001666}
1667
Xiao Guangrong77758342010-06-04 21:53:54 +08001668static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1669 struct list_head *invalid_list)
1670{
1671 struct kvm_mmu_page *sp;
1672
1673 if (list_empty(invalid_list))
1674 return;
1675
1676 kvm_flush_remote_tlbs(kvm);
1677
1678 do {
1679 sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
1680 WARN_ON(!sp->role.invalid || sp->root_count);
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08001681 kvm_mmu_free_page(sp);
Xiao Guangrong77758342010-06-04 21:53:54 +08001682 } while (!list_empty(invalid_list));
1683
1684}
1685
Izik Eidus82ce2c92007-10-02 18:52:55 +02001686/*
1687 * Changing the number of mmu pages allocated to the vm
Dave Hansen49d5ca22010-08-19 18:11:28 -07001688 * Note: if goal_nr_mmu_pages is too small, you will get dead lock
Izik Eidus82ce2c92007-10-02 18:52:55 +02001689 */
Dave Hansen49d5ca22010-08-19 18:11:28 -07001690void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages)
Izik Eidus82ce2c92007-10-02 18:52:55 +02001691{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001692 LIST_HEAD(invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001693 /*
1694 * If we set the number of mmu pages to be smaller be than the
1695 * number of actived pages , we must to free some mmu pages before we
1696 * change the value
1697 */
1698
Dave Hansen49d5ca22010-08-19 18:11:28 -07001699 if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) {
1700 while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages &&
Xiao Guangrong77662e02010-04-16 16:34:42 +08001701 !list_empty(&kvm->arch.active_mmu_pages)) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001702 struct kvm_mmu_page *page;
1703
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001704 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001705 struct kvm_mmu_page, link);
Xiaotian Feng80b63fa2010-08-24 10:31:07 +08001706 kvm_mmu_prepare_zap_page(kvm, page, &invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001707 }
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08001708 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Dave Hansen49d5ca22010-08-19 18:11:28 -07001709 goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001710 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02001711
Dave Hansen49d5ca22010-08-19 18:11:28 -07001712 kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001713}
1714
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001715static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001716{
Avi Kivity4db35312007-11-21 15:28:32 +02001717 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001718 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001719 LIST_HEAD(invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001720 int r;
1721
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08001722 pgprintk("%s: looking for gfn %llx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001723 r = 0;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001724
1725 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08001726 pgprintk("%s: gfn %llx role %x\n", __func__, gfn,
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001727 sp->role.word);
1728 r = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001729 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001730 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001731 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001732 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001733}
1734
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001735static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001736{
Avi Kivity4db35312007-11-21 15:28:32 +02001737 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001738 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001739 LIST_HEAD(invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001740
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001741 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08001742 pgprintk("%s: zap %llx %x\n",
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001743 __func__, gfn, sp->role.word);
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001744 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001745 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001746 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001747}
1748
Avi Kivity38c335f2007-11-21 14:20:22 +02001749static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001750{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001751 int slot = memslot_id(kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001752 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001753
Sheng Yang291f26b2008-10-16 17:30:57 +08001754 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001755}
1756
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001757static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1758{
1759 int i;
1760 u64 *pt = sp->spt;
1761
1762 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1763 return;
1764
1765 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1766 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001767 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001768 }
1769}
1770
Sheng Yang74be52e2008-10-09 16:01:56 +08001771/*
1772 * The function is based on mtrr_type_lookup() in
1773 * arch/x86/kernel/cpu/mtrr/generic.c
1774 */
1775static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1776 u64 start, u64 end)
1777{
1778 int i;
1779 u64 base, mask;
1780 u8 prev_match, curr_match;
1781 int num_var_ranges = KVM_NR_VAR_MTRR;
1782
1783 if (!mtrr_state->enabled)
1784 return 0xFF;
1785
1786 /* Make end inclusive end, instead of exclusive */
1787 end--;
1788
1789 /* Look in fixed ranges. Just return the type as per start */
1790 if (mtrr_state->have_fixed && (start < 0x100000)) {
1791 int idx;
1792
1793 if (start < 0x80000) {
1794 idx = 0;
1795 idx += (start >> 16);
1796 return mtrr_state->fixed_ranges[idx];
1797 } else if (start < 0xC0000) {
1798 idx = 1 * 8;
1799 idx += ((start - 0x80000) >> 14);
1800 return mtrr_state->fixed_ranges[idx];
1801 } else if (start < 0x1000000) {
1802 idx = 3 * 8;
1803 idx += ((start - 0xC0000) >> 12);
1804 return mtrr_state->fixed_ranges[idx];
1805 }
1806 }
1807
1808 /*
1809 * Look in variable ranges
1810 * Look of multiple ranges matching this address and pick type
1811 * as per MTRR precedence
1812 */
1813 if (!(mtrr_state->enabled & 2))
1814 return mtrr_state->def_type;
1815
1816 prev_match = 0xFF;
1817 for (i = 0; i < num_var_ranges; ++i) {
1818 unsigned short start_state, end_state;
1819
1820 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1821 continue;
1822
1823 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1824 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1825 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1826 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1827
1828 start_state = ((start & mask) == (base & mask));
1829 end_state = ((end & mask) == (base & mask));
1830 if (start_state != end_state)
1831 return 0xFE;
1832
1833 if ((start & mask) != (base & mask))
1834 continue;
1835
1836 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1837 if (prev_match == 0xFF) {
1838 prev_match = curr_match;
1839 continue;
1840 }
1841
1842 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1843 curr_match == MTRR_TYPE_UNCACHABLE)
1844 return MTRR_TYPE_UNCACHABLE;
1845
1846 if ((prev_match == MTRR_TYPE_WRBACK &&
1847 curr_match == MTRR_TYPE_WRTHROUGH) ||
1848 (prev_match == MTRR_TYPE_WRTHROUGH &&
1849 curr_match == MTRR_TYPE_WRBACK)) {
1850 prev_match = MTRR_TYPE_WRTHROUGH;
1851 curr_match = MTRR_TYPE_WRTHROUGH;
1852 }
1853
1854 if (prev_match != curr_match)
1855 return MTRR_TYPE_UNCACHABLE;
1856 }
1857
1858 if (prev_match != 0xFF)
1859 return prev_match;
1860
1861 return mtrr_state->def_type;
1862}
1863
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001864u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001865{
1866 u8 mtrr;
1867
1868 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1869 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1870 if (mtrr == 0xfe || mtrr == 0xff)
1871 mtrr = MTRR_TYPE_WRBACK;
1872 return mtrr;
1873}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001874EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001875
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001876static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001877{
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001878 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001879 ++vcpu->kvm->stat.mmu_unsync;
1880 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001881
Xiao Guangrong6b184932010-04-16 21:29:17 +08001882 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001883 mmu_convert_notrap(sp);
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001884}
1885
1886static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1887{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001888 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001889 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001890
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001891 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001892 if (s->unsync)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001893 continue;
1894 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1895 __kvm_unsync_page(vcpu, s);
1896 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001897}
1898
1899static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1900 bool can_unsync)
1901{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001902 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001903 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001904 bool need_unsync = false;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001905
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001906 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001907 if (!can_unsync)
1908 return 1;
1909
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001910 if (s->role.level != PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001911 return 1;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001912
1913 if (!need_unsync && !s->unsync) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001914 if (!oos_shadow)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001915 return 1;
1916 need_unsync = true;
1917 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001918 }
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001919 if (need_unsync)
1920 kvm_unsync_pages(vcpu, gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001921 return 0;
1922}
1923
Avi Kivityd555c332009-06-10 14:24:23 +03001924static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001925 unsigned pte_access, int user_fault,
Xiao Guangrong640d9b02011-07-12 03:24:39 +08001926 int write_fault, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001927 gfn_t gfn, pfn_t pfn, bool speculative,
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08001928 bool can_unsync, bool host_writable)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001929{
Xiao Guangrongb330aa02010-11-19 17:02:35 +08001930 u64 spte, entry = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001931 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001932
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001933 /*
1934 * We don't set the accessed bit, since we sometimes want to see
1935 * whether the guest actually used the pte (in order to detect
1936 * demand paging).
1937 */
Marcelo Tosatti982c2562010-10-22 14:18:16 -02001938 spte = PT_PRESENT_MASK;
Avi Kivity947da532008-03-18 11:05:52 +02001939 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001940 spte |= shadow_accessed_mask;
Xiao Guangrong640d9b02011-07-12 03:24:39 +08001941
Sheng Yang7b523452008-04-25 21:13:50 +08001942 if (pte_access & ACC_EXEC_MASK)
1943 spte |= shadow_x_mask;
1944 else
1945 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001946 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001947 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001948 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001949 spte |= PT_PAGE_SIZE_MASK;
Avi Kivityb0bc3ee2010-09-13 16:45:28 +02001950 if (tdp_enabled)
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001951 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1952 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001953
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08001954 if (host_writable)
Izik Eidus14032832009-09-23 21:47:17 +03001955 spte |= SPTE_HOST_WRITEABLE;
Xiao Guangrongf8e453b2010-12-23 16:09:29 +08001956 else
1957 pte_access &= ~ACC_WRITE_MASK;
Izik Eidus14032832009-09-23 21:47:17 +03001958
Anthony Liguori35149e22008-04-02 14:46:56 -05001959 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001960
1961 if ((pte_access & ACC_WRITE_MASK)
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02001962 || (!vcpu->arch.mmu.direct_map && write_fault
1963 && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001964
Joerg Roedel852e3c12009-07-27 16:30:44 +02001965 if (level > PT_PAGE_TABLE_LEVEL &&
1966 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001967 ret = 1;
Avi Kivitybe38d272010-06-06 14:31:27 +03001968 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
1969 goto done;
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001970 }
1971
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001972 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001973
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02001974 if (!vcpu->arch.mmu.direct_map
Avi Kivity411c5882011-06-06 16:11:54 +03001975 && !(pte_access & ACC_WRITE_MASK)) {
Avi Kivity69325a12010-05-27 14:35:58 +03001976 spte &= ~PT_USER_MASK;
Avi Kivity411c5882011-06-06 16:11:54 +03001977 /*
1978 * If we converted a user page to a kernel page,
1979 * so that the kernel can write to it when cr0.wp=0,
1980 * then we should prevent the kernel from executing it
1981 * if SMEP is enabled.
1982 */
1983 if (kvm_read_cr4_bits(vcpu, X86_CR4_SMEP))
1984 spte |= PT64_NX_MASK;
1985 }
Avi Kivity69325a12010-05-27 14:35:58 +03001986
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001987 /*
1988 * Optimization: for pte sync, if spte was writable the hash
1989 * lookup is unnecessary (and expensive). Write protection
1990 * is responsibility of mmu_get_page / kvm_sync_page.
1991 * Same reasoning can be applied to dirty page accounting.
1992 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001993 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001994 goto set_pte;
1995
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001996 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08001997 pgprintk("%s: found shadow page for %llx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001998 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001999 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002000 pte_access &= ~ACC_WRITE_MASK;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002001 if (is_writable_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002002 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002003 }
2004 }
2005
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002006 if (pte_access & ACC_WRITE_MASK)
2007 mark_page_dirty(vcpu->kvm, gfn);
2008
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002009set_pte:
Avi Kivityb79b93f2010-06-06 15:46:44 +03002010 update_spte(sptep, spte);
Xiao Guangrongb330aa02010-11-19 17:02:35 +08002011 /*
2012 * If we overwrite a writable spte with a read-only one we
2013 * should flush remote TLBs. Otherwise rmap_write_protect
2014 * will find a read-only spte, even though the writable spte
2015 * might be cached on a CPU's TLB.
2016 */
2017 if (is_writable_pte(entry) && !is_writable_pte(*sptep))
2018 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivitybe38d272010-06-06 14:31:27 +03002019done:
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002020 return ret;
2021}
2022
Avi Kivityd555c332009-06-10 14:24:23 +03002023static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002024 unsigned pt_access, unsigned pte_access,
Xiao Guangrong640d9b02011-07-12 03:24:39 +08002025 int user_fault, int write_fault,
Xiao Guangrongb90a0e62011-07-12 03:25:56 +08002026 int *emulate, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03002027 pfn_t pfn, bool speculative,
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08002028 bool host_writable)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002029{
2030 int was_rmapped = 0;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002031 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002032
2033 pgprintk("%s: spte %llx access %x write_fault %d"
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08002034 " user_fault %d gfn %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002035 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002036 write_fault, user_fault, gfn);
2037
Avi Kivityd555c332009-06-10 14:24:23 +03002038 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002039 /*
2040 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
2041 * the parent of the now unreachable PTE.
2042 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02002043 if (level > PT_PAGE_TABLE_LEVEL &&
2044 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002045 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03002046 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002047
2048 child = page_header(pte & PT64_BASE_ADDR_MASK);
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08002049 drop_parent_pte(child, sptep);
Marcelo Tosatti3be22642010-05-28 09:44:59 -03002050 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityd555c332009-06-10 14:24:23 +03002051 } else if (pfn != spte_to_pfn(*sptep)) {
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08002052 pgprintk("hfn old %llx new %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002053 spte_to_pfn(*sptep), pfn);
Avi Kivitybe38d272010-06-06 14:31:27 +03002054 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
Xiao Guangrong91546352010-06-30 16:04:06 +08002055 kvm_flush_remote_tlbs(vcpu->kvm);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01002056 } else
2057 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002058 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02002059
Avi Kivityd555c332009-06-10 14:24:23 +03002060 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Xiao Guangrong640d9b02011-07-12 03:24:39 +08002061 level, gfn, pfn, speculative, true,
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08002062 host_writable)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002063 if (write_fault)
Xiao Guangrongb90a0e62011-07-12 03:25:56 +08002064 *emulate = 1;
Xiao Guangrong5304efd2010-06-08 20:05:57 +08002065 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03002066 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002067
Avi Kivityd555c332009-06-10 14:24:23 +03002068 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08002069 pgprintk("instantiating %s PTE (%s) at %llx (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002070 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02002071 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
2072 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03002073 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002074 ++vcpu->kvm->stat.lpages;
2075
Xiao Guangrongffb61bb2011-07-12 03:22:01 +08002076 if (is_shadow_present_pte(*sptep)) {
2077 page_header_update_slot(vcpu->kvm, sptep, gfn);
2078 if (!was_rmapped) {
2079 rmap_count = rmap_add(vcpu, sptep, gfn);
2080 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
2081 rmap_recycle(vcpu, sptep, gfn);
2082 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002083 }
Xiao Guangrong9ed55202010-07-16 11:25:17 +08002084 kvm_release_pfn_clean(pfn);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002085 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03002086 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002087 vcpu->arch.last_pte_gfn = gfn;
2088 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002089}
2090
Avi Kivity6aa8b732006-12-10 02:21:36 -08002091static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
2092{
2093}
2094
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002095static pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn,
2096 bool no_dirty_log)
2097{
2098 struct kvm_memory_slot *slot;
2099 unsigned long hva;
2100
Xiao Guangrong5d163b12011-03-09 15:43:00 +08002101 slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, no_dirty_log);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002102 if (!slot) {
2103 get_page(bad_page);
2104 return page_to_pfn(bad_page);
2105 }
2106
2107 hva = gfn_to_hva_memslot(slot, gfn);
2108
2109 return hva_to_pfn_atomic(vcpu->kvm, hva);
2110}
2111
2112static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,
2113 struct kvm_mmu_page *sp,
2114 u64 *start, u64 *end)
2115{
2116 struct page *pages[PTE_PREFETCH_NUM];
2117 unsigned access = sp->role.access;
2118 int i, ret;
2119 gfn_t gfn;
2120
2121 gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt);
Xiao Guangrong5d163b12011-03-09 15:43:00 +08002122 if (!gfn_to_memslot_dirty_bitmap(vcpu, gfn, access & ACC_WRITE_MASK))
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002123 return -1;
2124
2125 ret = gfn_to_page_many_atomic(vcpu->kvm, gfn, pages, end - start);
2126 if (ret <= 0)
2127 return -1;
2128
2129 for (i = 0; i < ret; i++, gfn++, start++)
2130 mmu_set_spte(vcpu, start, ACC_ALL,
Xiao Guangrong640d9b02011-07-12 03:24:39 +08002131 access, 0, 0, NULL,
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002132 sp->role.level, gfn,
2133 page_to_pfn(pages[i]), true, true);
2134
2135 return 0;
2136}
2137
2138static void __direct_pte_prefetch(struct kvm_vcpu *vcpu,
2139 struct kvm_mmu_page *sp, u64 *sptep)
2140{
2141 u64 *spte, *start = NULL;
2142 int i;
2143
2144 WARN_ON(!sp->role.direct);
2145
2146 i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
2147 spte = sp->spt + i;
2148
2149 for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
2150 if (*spte != shadow_trap_nonpresent_pte || spte == sptep) {
2151 if (!start)
2152 continue;
2153 if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0)
2154 break;
2155 start = NULL;
2156 } else if (!start)
2157 start = spte;
2158 }
2159}
2160
2161static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)
2162{
2163 struct kvm_mmu_page *sp;
2164
2165 /*
2166 * Since it's no accessed bit on EPT, it's no way to
2167 * distinguish between actually accessed translations
2168 * and prefetched, so disable pte prefetch if EPT is
2169 * enabled.
2170 */
2171 if (!shadow_accessed_mask)
2172 return;
2173
2174 sp = page_header(__pa(sptep));
2175 if (sp->role.level > PT_PAGE_TABLE_LEVEL)
2176 return;
2177
2178 __direct_pte_prefetch(vcpu, sp, sptep);
2179}
2180
Joerg Roedel4d9976b2008-02-07 13:47:42 +01002181static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002182 int map_writable, int level, gfn_t gfn, pfn_t pfn,
2183 bool prefault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002184{
Avi Kivity9f652d22008-12-25 14:54:25 +02002185 struct kvm_shadow_walk_iterator iterator;
2186 struct kvm_mmu_page *sp;
Xiao Guangrongb90a0e62011-07-12 03:25:56 +08002187 int emulate = 0;
Avi Kivity9f652d22008-12-25 14:54:25 +02002188 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002189
Avi Kivity9f652d22008-12-25 14:54:25 +02002190 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02002191 if (iterator.level == level) {
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002192 unsigned pte_access = ACC_ALL;
2193
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002194 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, pte_access,
Xiao Guangrongb90a0e62011-07-12 03:25:56 +08002195 0, write, &emulate,
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002196 level, gfn, pfn, prefault, map_writable);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002197 direct_pte_prefetch(vcpu, iterator.sptep);
Avi Kivity9f652d22008-12-25 14:54:25 +02002198 ++vcpu->stat.pf_fixed;
2199 break;
2200 }
2201
2202 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
Lai Jiangshanc9fa0b32010-05-26 16:48:25 +08002203 u64 base_addr = iterator.addr;
2204
2205 base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
2206 pseudo_gfn = base_addr >> PAGE_SHIFT;
Avi Kivity9f652d22008-12-25 14:54:25 +02002207 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
2208 iterator.level - 1,
2209 1, ACC_ALL, iterator.sptep);
2210 if (!sp) {
2211 pgprintk("nonpaging_map: ENOMEM\n");
2212 kvm_release_pfn_clean(pfn);
2213 return -ENOMEM;
2214 }
2215
Avi Kivityd555c332009-06-10 14:24:23 +03002216 __set_spte(iterator.sptep,
2217 __pa(sp->spt)
2218 | PT_PRESENT_MASK | PT_WRITABLE_MASK
Xiao Guangrong33f91ed2010-09-27 18:05:00 +08002219 | shadow_user_mask | shadow_x_mask
2220 | shadow_accessed_mask);
Avi Kivity9f652d22008-12-25 14:54:25 +02002221 }
2222 }
Xiao Guangrongb90a0e62011-07-12 03:25:56 +08002223 return emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002224}
2225
Huang Ying77db5cb2010-10-08 16:24:15 +08002226static void kvm_send_hwpoison_signal(unsigned long address, struct task_struct *tsk)
Huang Yingbf998152010-05-31 14:28:19 +08002227{
Huang Ying77db5cb2010-10-08 16:24:15 +08002228 siginfo_t info;
Huang Yingbf998152010-05-31 14:28:19 +08002229
Huang Ying77db5cb2010-10-08 16:24:15 +08002230 info.si_signo = SIGBUS;
2231 info.si_errno = 0;
2232 info.si_code = BUS_MCEERR_AR;
2233 info.si_addr = (void __user *)address;
2234 info.si_addr_lsb = PAGE_SHIFT;
2235
2236 send_sig_info(SIGBUS, &info, tsk);
Huang Yingbf998152010-05-31 14:28:19 +08002237}
2238
Xiao Guangrongbebb1062011-07-12 03:23:20 +08002239static int kvm_handle_bad_page(struct kvm_vcpu *vcpu, gva_t gva,
2240 unsigned access, gfn_t gfn, pfn_t pfn)
Huang Yingbf998152010-05-31 14:28:19 +08002241{
2242 kvm_release_pfn_clean(pfn);
2243 if (is_hwpoison_pfn(pfn)) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08002244 kvm_send_hwpoison_signal(gfn_to_hva(vcpu->kvm, gfn), current);
Huang Yingbf998152010-05-31 14:28:19 +08002245 return 0;
Gleb Natapovedba23e2010-07-07 20:16:45 +03002246 } else if (is_fault_pfn(pfn))
2247 return -EFAULT;
2248
Xiao Guangrongbebb1062011-07-12 03:23:20 +08002249 vcpu_cache_mmio_info(vcpu, gva, gfn, access);
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))
Xiao Guangrongbebb1062011-07-12 03:23:20 +08002331 return kvm_handle_bad_page(vcpu, v, ACC_ALL, 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
Xiao Guangrongbebb1062011-07-12 03:23:20 +08002558 vcpu_clear_mmio_info(vcpu, ~0ul);
Xiao Guangrong69030742010-09-27 18:09:29 +08002559 trace_kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC);
Joerg Roedel81407ca2010-09-10 17:31:00 +02002560 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002561 hpa_t root = vcpu->arch.mmu.root_hpa;
2562 sp = page_header(root);
2563 mmu_sync_children(vcpu, sp);
Xiao Guangrong5054c0d2010-11-12 14:46:08 +08002564 trace_kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002565 return;
2566 }
2567 for (i = 0; i < 4; ++i) {
2568 hpa_t root = vcpu->arch.mmu.pae_root[i];
2569
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002570 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002571 root &= PT64_BASE_ADDR_MASK;
2572 sp = page_header(root);
2573 mmu_sync_children(vcpu, sp);
2574 }
2575 }
Xiao Guangrong69030742010-09-27 18:09:29 +08002576 trace_kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002577}
2578
2579void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2580{
2581 spin_lock(&vcpu->kvm->mmu_lock);
2582 mmu_sync_roots(vcpu);
2583 spin_unlock(&vcpu->kvm->mmu_lock);
2584}
2585
Gleb Natapov1871c602010-02-10 14:21:32 +02002586static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
Avi Kivityab9ae312010-11-22 17:53:26 +02002587 u32 access, struct x86_exception *exception)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002588{
Avi Kivityab9ae312010-11-22 17:53:26 +02002589 if (exception)
2590 exception->error_code = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002591 return vaddr;
2592}
2593
Joerg Roedel6539e732010-09-10 17:30:50 +02002594static gpa_t nonpaging_gva_to_gpa_nested(struct kvm_vcpu *vcpu, gva_t vaddr,
Avi Kivityab9ae312010-11-22 17:53:26 +02002595 u32 access,
2596 struct x86_exception *exception)
Joerg Roedel6539e732010-09-10 17:30:50 +02002597{
Avi Kivityab9ae312010-11-22 17:53:26 +02002598 if (exception)
2599 exception->error_code = 0;
Joerg Roedel6539e732010-09-10 17:30:50 +02002600 return vcpu->arch.nested_mmu.translate_gpa(vcpu, vaddr, access);
2601}
2602
Avi Kivity6aa8b732006-12-10 02:21:36 -08002603static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002604 u32 error_code, bool prefault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002605{
Avi Kivitye8332402007-12-09 18:43:00 +02002606 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002607 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002608
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002609 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002610 r = mmu_topup_memory_caches(vcpu);
2611 if (r)
2612 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002613
Avi Kivity6aa8b732006-12-10 02:21:36 -08002614 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002615 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002616
Avi Kivitye8332402007-12-09 18:43:00 +02002617 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002618
Avi Kivitye8332402007-12-09 18:43:00 +02002619 return nonpaging_map(vcpu, gva & PAGE_MASK,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002620 error_code & PFERR_WRITE_MASK, gfn, prefault);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002621}
2622
Jan Kiszka7e1fbea2010-10-20 15:18:02 +02002623static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn)
Gleb Natapovaf585b92010-10-14 11:22:46 +02002624{
2625 struct kvm_arch_async_pf arch;
Xiao Guangrongfb67e142010-12-07 10:35:25 +08002626
Gleb Natapov7c907052010-10-14 11:22:53 +02002627 arch.token = (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id;
Gleb Natapovaf585b92010-10-14 11:22:46 +02002628 arch.gfn = gfn;
Xiao Guangrongc4806ac2010-11-12 14:49:55 +08002629 arch.direct_map = vcpu->arch.mmu.direct_map;
Xiao Guangrongfb67e142010-12-07 10:35:25 +08002630 arch.cr3 = vcpu->arch.mmu.get_cr3(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002631
2632 return kvm_setup_async_pf(vcpu, gva, gfn, &arch);
2633}
2634
2635static bool can_do_async_pf(struct kvm_vcpu *vcpu)
2636{
2637 if (unlikely(!irqchip_in_kernel(vcpu->kvm) ||
2638 kvm_event_needs_reinjection(vcpu)))
2639 return false;
2640
2641 return kvm_x86_ops->interrupt_allowed(vcpu);
2642}
2643
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002644static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002645 gva_t gva, pfn_t *pfn, bool write, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02002646{
2647 bool async;
2648
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002649 *pfn = gfn_to_pfn_async(vcpu->kvm, gfn, &async, write, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002650
2651 if (!async)
2652 return false; /* *pfn has correct page already */
2653
2654 put_page(pfn_to_page(*pfn));
2655
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002656 if (!prefault && can_do_async_pf(vcpu)) {
Xiao Guangrongc9b263d2010-11-01 16:58:43 +08002657 trace_kvm_try_async_get_page(gva, gfn);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002658 if (kvm_find_async_pf_gfn(vcpu, gfn)) {
2659 trace_kvm_async_pf_doublefault(gva, gfn);
2660 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
2661 return true;
2662 } else if (kvm_arch_setup_async_pf(vcpu, gva, gfn))
2663 return true;
2664 }
2665
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002666 *pfn = gfn_to_pfn_prot(vcpu->kvm, gfn, write, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002667
2668 return false;
2669}
2670
Gleb Natapov56028d02010-10-17 18:13:42 +02002671static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, u32 error_code,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002672 bool prefault)
Joerg Roedelfb72d162008-02-07 13:47:44 +01002673{
Anthony Liguori35149e22008-04-02 14:46:56 -05002674 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002675 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002676 int level;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002677 int force_pt_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002678 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002679 unsigned long mmu_seq;
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002680 int write = error_code & PFERR_WRITE_MASK;
2681 bool map_writable;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002682
2683 ASSERT(vcpu);
2684 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2685
2686 r = mmu_topup_memory_caches(vcpu);
2687 if (r)
2688 return r;
2689
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002690 force_pt_level = mapping_level_dirty_bitmap(vcpu, gfn);
2691 if (likely(!force_pt_level)) {
2692 level = mapping_level(vcpu, gfn);
2693 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2694 } else
2695 level = PT_PAGE_TABLE_LEVEL;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002696
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002697 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002698 smp_rmb();
Gleb Natapovaf585b92010-10-14 11:22:46 +02002699
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002700 if (try_async_pf(vcpu, prefault, gfn, gpa, &pfn, write, &map_writable))
Gleb Natapovaf585b92010-10-14 11:22:46 +02002701 return 0;
2702
2703 /* mmio */
Huang Yingbf998152010-05-31 14:28:19 +08002704 if (is_error_pfn(pfn))
Xiao Guangrongbebb1062011-07-12 03:23:20 +08002705 return kvm_handle_bad_page(vcpu, 0, 0, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002706 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002707 if (mmu_notifier_retry(vcpu, mmu_seq))
2708 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002709 kvm_mmu_free_some_pages(vcpu);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002710 if (likely(!force_pt_level))
2711 transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002712 r = __direct_map(vcpu, gpa, write, map_writable,
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002713 level, gfn, pfn, prefault);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002714 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002715
2716 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002717
2718out_unlock:
2719 spin_unlock(&vcpu->kvm->mmu_lock);
2720 kvm_release_pfn_clean(pfn);
2721 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002722}
2723
Avi Kivity6aa8b732006-12-10 02:21:36 -08002724static void nonpaging_free(struct kvm_vcpu *vcpu)
2725{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002726 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002727}
2728
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002729static int nonpaging_init_context(struct kvm_vcpu *vcpu,
2730 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002731{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002732 context->new_cr3 = nonpaging_new_cr3;
2733 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002734 context->gva_to_gpa = nonpaging_gva_to_gpa;
2735 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002736 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002737 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002738 context->invlpg = nonpaging_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08002739 context->update_pte = nonpaging_update_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002740 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002741 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002742 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002743 context->direct_map = true;
Joerg Roedel2d48a982010-09-10 17:31:01 +02002744 context->nx = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002745 return 0;
2746}
2747
Avi Kivityd835dfe2007-11-21 02:57:59 +02002748void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002749{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002750 ++vcpu->stat.tlb_flush;
Avi Kivitya8eeb042010-05-10 12:34:53 +03002751 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002752}
2753
2754static void paging_new_cr3(struct kvm_vcpu *vcpu)
2755{
Avi Kivity9f8fe502010-12-05 17:30:00 +02002756 pgprintk("%s: cr3 %lx\n", __func__, kvm_read_cr3(vcpu));
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002757 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002758}
2759
Joerg Roedel5777ed32010-09-10 17:30:42 +02002760static unsigned long get_cr3(struct kvm_vcpu *vcpu)
2761{
Avi Kivity9f8fe502010-12-05 17:30:00 +02002762 return kvm_read_cr3(vcpu);
Joerg Roedel5777ed32010-09-10 17:30:42 +02002763}
2764
Avi Kivity6389ee92010-11-29 16:12:30 +02002765static void inject_page_fault(struct kvm_vcpu *vcpu,
2766 struct x86_exception *fault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002767{
Avi Kivity6389ee92010-11-29 16:12:30 +02002768 vcpu->arch.mmu.inject_page_fault(vcpu, fault);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002769}
2770
Avi Kivity6aa8b732006-12-10 02:21:36 -08002771static void paging_free(struct kvm_vcpu *vcpu)
2772{
2773 nonpaging_free(vcpu);
2774}
2775
Joerg Roedel3241f222010-09-10 17:30:45 +02002776static bool is_rsvd_bits_set(struct kvm_mmu *mmu, u64 gpte, int level)
Dong, Eddie82725b22009-03-30 16:21:08 +08002777{
2778 int bit7;
2779
2780 bit7 = (gpte >> 7) & 1;
Joerg Roedel3241f222010-09-10 17:30:45 +02002781 return (gpte & mmu->rsvd_bits_mask[bit7][level-1]) != 0;
Dong, Eddie82725b22009-03-30 16:21:08 +08002782}
2783
Avi Kivity6aa8b732006-12-10 02:21:36 -08002784#define PTTYPE 64
2785#include "paging_tmpl.h"
2786#undef PTTYPE
2787
2788#define PTTYPE 32
2789#include "paging_tmpl.h"
2790#undef PTTYPE
2791
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002792static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
2793 struct kvm_mmu *context,
2794 int level)
Dong, Eddie82725b22009-03-30 16:21:08 +08002795{
Dong, Eddie82725b22009-03-30 16:21:08 +08002796 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2797 u64 exb_bit_rsvd = 0;
2798
Joerg Roedel2d48a982010-09-10 17:31:01 +02002799 if (!context->nx)
Dong, Eddie82725b22009-03-30 16:21:08 +08002800 exb_bit_rsvd = rsvd_bits(63, 63);
2801 switch (level) {
2802 case PT32_ROOT_LEVEL:
2803 /* no rsvd bits for 2 level 4K page table entries */
2804 context->rsvd_bits_mask[0][1] = 0;
2805 context->rsvd_bits_mask[0][0] = 0;
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002806 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2807
2808 if (!is_pse(vcpu)) {
2809 context->rsvd_bits_mask[1][1] = 0;
2810 break;
2811 }
2812
Dong, Eddie82725b22009-03-30 16:21:08 +08002813 if (is_cpuid_PSE36())
2814 /* 36bits PSE 4MB page */
2815 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2816 else
2817 /* 32 bits PSE 4MB page */
2818 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08002819 break;
2820 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002821 context->rsvd_bits_mask[0][2] =
2822 rsvd_bits(maxphyaddr, 63) |
2823 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002824 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002825 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002826 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2827 rsvd_bits(maxphyaddr, 62); /* PTE */
2828 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2829 rsvd_bits(maxphyaddr, 62) |
2830 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002831 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002832 break;
2833 case PT64_ROOT_LEVEL:
2834 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2835 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2836 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2837 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2838 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002839 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002840 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2841 rsvd_bits(maxphyaddr, 51);
2842 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002843 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2844 rsvd_bits(maxphyaddr, 51) |
2845 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002846 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002847 rsvd_bits(maxphyaddr, 51) |
2848 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002849 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002850 break;
2851 }
2852}
2853
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002854static int paging64_init_context_common(struct kvm_vcpu *vcpu,
2855 struct kvm_mmu *context,
2856 int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002857{
Joerg Roedel2d48a982010-09-10 17:31:01 +02002858 context->nx = is_nx(vcpu);
2859
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002860 reset_rsvds_bits_mask(vcpu, context, level);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002861
2862 ASSERT(is_pae(vcpu));
2863 context->new_cr3 = paging_new_cr3;
2864 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002865 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002866 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002867 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002868 context->invlpg = paging64_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08002869 context->update_pte = paging64_update_pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002870 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002871 context->root_level = level;
2872 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002873 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002874 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002875 return 0;
2876}
2877
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002878static int paging64_init_context(struct kvm_vcpu *vcpu,
2879 struct kvm_mmu *context)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002880{
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002881 return paging64_init_context_common(vcpu, context, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002882}
2883
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002884static int paging32_init_context(struct kvm_vcpu *vcpu,
2885 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002886{
Joerg Roedel2d48a982010-09-10 17:31:01 +02002887 context->nx = false;
2888
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002889 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002890
2891 context->new_cr3 = paging_new_cr3;
2892 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002893 context->gva_to_gpa = paging32_gva_to_gpa;
2894 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002895 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002896 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002897 context->invlpg = paging32_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08002898 context->update_pte = paging32_update_pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002899 context->root_level = PT32_ROOT_LEVEL;
2900 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002901 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002902 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002903 return 0;
2904}
2905
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002906static int paging32E_init_context(struct kvm_vcpu *vcpu,
2907 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002908{
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002909 return paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002910}
2911
Joerg Roedelfb72d162008-02-07 13:47:44 +01002912static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2913{
Joerg Roedel14dfe852010-09-10 17:30:49 +02002914 struct kvm_mmu *context = vcpu->arch.walk_mmu;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002915
Avi Kivityc445f8e2010-12-21 16:26:01 +02002916 context->base_role.word = 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002917 context->new_cr3 = nonpaging_new_cr3;
2918 context->page_fault = tdp_page_fault;
2919 context->free = nonpaging_free;
2920 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002921 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002922 context->invlpg = nonpaging_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08002923 context->update_pte = nonpaging_update_pte;
Sheng Yang67253af2008-04-25 10:20:22 +08002924 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002925 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002926 context->direct_map = true;
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02002927 context->set_cr3 = kvm_x86_ops->set_tdp_cr3;
Joerg Roedel5777ed32010-09-10 17:30:42 +02002928 context->get_cr3 = get_cr3;
Joerg Roedelcb659db2010-09-10 17:30:43 +02002929 context->inject_page_fault = kvm_inject_page_fault;
Joerg Roedel2d48a982010-09-10 17:31:01 +02002930 context->nx = is_nx(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002931
2932 if (!is_paging(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002933 context->nx = false;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002934 context->gva_to_gpa = nonpaging_gva_to_gpa;
2935 context->root_level = 0;
2936 } else if (is_long_mode(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002937 context->nx = is_nx(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002938 reset_rsvds_bits_mask(vcpu, context, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002939 context->gva_to_gpa = paging64_gva_to_gpa;
2940 context->root_level = PT64_ROOT_LEVEL;
2941 } else if (is_pae(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002942 context->nx = is_nx(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002943 reset_rsvds_bits_mask(vcpu, context, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002944 context->gva_to_gpa = paging64_gva_to_gpa;
2945 context->root_level = PT32E_ROOT_LEVEL;
2946 } else {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002947 context->nx = false;
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002948 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002949 context->gva_to_gpa = paging32_gva_to_gpa;
2950 context->root_level = PT32_ROOT_LEVEL;
2951 }
2952
2953 return 0;
2954}
2955
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002956int kvm_init_shadow_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002957{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002958 int r;
Avi Kivity411c5882011-06-06 16:11:54 +03002959 bool smep = kvm_read_cr4_bits(vcpu, X86_CR4_SMEP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002960 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002961 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002962
2963 if (!is_paging(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002964 r = nonpaging_init_context(vcpu, context);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002965 else if (is_long_mode(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002966 r = paging64_init_context(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002967 else if (is_pae(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002968 r = paging32E_init_context(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002969 else
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002970 r = paging32_init_context(vcpu, context);
Avi Kivitya770f6f2008-12-21 19:20:09 +02002971
Avi Kivity5b7e0102010-04-14 19:20:03 +03002972 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
Joerg Roedelf43addd2010-09-10 17:30:40 +02002973 vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
Avi Kivity411c5882011-06-06 16:11:54 +03002974 vcpu->arch.mmu.base_role.smep_andnot_wp
2975 = smep && !is_write_protection(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002976
2977 return r;
2978}
2979EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu);
2980
2981static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
2982{
Joerg Roedel14dfe852010-09-10 17:30:49 +02002983 int r = kvm_init_shadow_mmu(vcpu, vcpu->arch.walk_mmu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002984
Joerg Roedel14dfe852010-09-10 17:30:49 +02002985 vcpu->arch.walk_mmu->set_cr3 = kvm_x86_ops->set_cr3;
2986 vcpu->arch.walk_mmu->get_cr3 = get_cr3;
2987 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
Avi Kivitya770f6f2008-12-21 19:20:09 +02002988
2989 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002990}
2991
Joerg Roedel02f59dc2010-09-10 17:30:54 +02002992static int init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
2993{
2994 struct kvm_mmu *g_context = &vcpu->arch.nested_mmu;
2995
2996 g_context->get_cr3 = get_cr3;
2997 g_context->inject_page_fault = kvm_inject_page_fault;
2998
2999 /*
3000 * Note that arch.mmu.gva_to_gpa translates l2_gva to l1_gpa. The
3001 * translation of l2_gpa to l1_gpa addresses is done using the
3002 * arch.nested_mmu.gva_to_gpa function. Basically the gva_to_gpa
3003 * functions between mmu and nested_mmu are swapped.
3004 */
3005 if (!is_paging(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003006 g_context->nx = false;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003007 g_context->root_level = 0;
3008 g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested;
3009 } else if (is_long_mode(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003010 g_context->nx = is_nx(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003011 reset_rsvds_bits_mask(vcpu, g_context, PT64_ROOT_LEVEL);
3012 g_context->root_level = PT64_ROOT_LEVEL;
3013 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
3014 } else if (is_pae(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003015 g_context->nx = is_nx(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003016 reset_rsvds_bits_mask(vcpu, g_context, PT32E_ROOT_LEVEL);
3017 g_context->root_level = PT32E_ROOT_LEVEL;
3018 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
3019 } else {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003020 g_context->nx = false;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003021 reset_rsvds_bits_mask(vcpu, g_context, PT32_ROOT_LEVEL);
3022 g_context->root_level = PT32_ROOT_LEVEL;
3023 g_context->gva_to_gpa = paging32_gva_to_gpa_nested;
3024 }
3025
3026 return 0;
3027}
3028
Joerg Roedelfb72d162008-02-07 13:47:44 +01003029static int init_kvm_mmu(struct kvm_vcpu *vcpu)
3030{
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003031 if (mmu_is_nested(vcpu))
3032 return init_kvm_nested_mmu(vcpu);
3033 else if (tdp_enabled)
Joerg Roedelfb72d162008-02-07 13:47:44 +01003034 return init_kvm_tdp_mmu(vcpu);
3035 else
3036 return init_kvm_softmmu(vcpu);
3037}
3038
Avi Kivity6aa8b732006-12-10 02:21:36 -08003039static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
3040{
3041 ASSERT(vcpu);
Sheng Yang62ad0752010-05-12 16:40:41 +08003042 if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
3043 /* mmu.free() should set root_hpa = INVALID_PAGE */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003044 vcpu->arch.mmu.free(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003045}
3046
3047int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
3048{
Avi Kivity17c3ba92007-06-04 15:58:30 +03003049 destroy_kvm_mmu(vcpu);
Marcelo Tosattif8f7e5e2011-06-21 14:00:10 -03003050 return init_kvm_mmu(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03003051}
Eddie Dong8668a3c2007-10-10 14:26:45 +08003052EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03003053
3054int kvm_mmu_load(struct kvm_vcpu *vcpu)
3055{
Avi Kivity714b93d2007-01-05 16:36:53 -08003056 int r;
3057
Avi Kivitye2dec932007-01-05 16:36:54 -08003058 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03003059 if (r)
3060 goto out;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003061 r = mmu_alloc_roots(vcpu);
Avi Kivity8facbbf2010-05-04 12:58:32 +03003062 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003063 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003064 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003065 if (r)
3066 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08003067 /* set_cr3() should ensure TLB has been flushed */
Joerg Roedelf43addd2010-09-10 17:30:40 +02003068 vcpu->arch.mmu.set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08003069out:
3070 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003071}
Avi Kivity17c3ba92007-06-04 15:58:30 +03003072EXPORT_SYMBOL_GPL(kvm_mmu_load);
3073
3074void kvm_mmu_unload(struct kvm_vcpu *vcpu)
3075{
3076 mmu_free_roots(vcpu);
3077}
Joerg Roedel4b161842010-09-10 17:31:03 +02003078EXPORT_SYMBOL_GPL(kvm_mmu_unload);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003079
Avi Kivity00284252007-05-01 16:53:31 +03003080static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Xiao Guangrong7c562522011-03-28 10:29:27 +08003081 struct kvm_mmu_page *sp, u64 *spte,
3082 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03003083{
Marcelo Tosatti30945382008-06-11 20:32:40 -03003084 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02003085 ++vcpu->kvm->stat.mmu_pde_zapped;
3086 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03003087 }
Avi Kivity00284252007-05-01 16:53:31 +03003088
Avi Kivity4cee5762007-11-18 16:37:07 +02003089 ++vcpu->kvm->stat.mmu_pte_updated;
Xiao Guangrong7c562522011-03-28 10:29:27 +08003090 vcpu->arch.mmu.update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03003091}
3092
Avi Kivity79539ce2007-11-21 02:06:21 +02003093static bool need_remote_flush(u64 old, u64 new)
3094{
3095 if (!is_shadow_present_pte(old))
3096 return false;
3097 if (!is_shadow_present_pte(new))
3098 return true;
3099 if ((old ^ new) & PT64_BASE_ADDR_MASK)
3100 return true;
3101 old ^= PT64_NX_MASK;
3102 new ^= PT64_NX_MASK;
3103 return (old & ~new & PT64_PERM_MASK) != 0;
3104}
3105
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003106static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page,
3107 bool remote_flush, bool local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003108{
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003109 if (zap_page)
3110 return;
3111
3112 if (remote_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003113 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003114 else if (local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003115 kvm_mmu_flush_tlb(vcpu);
3116}
3117
Avi Kivity12b7d282007-09-23 14:10:49 +02003118static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
3119{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003120 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02003121
Sheng Yang7b523452008-04-25 21:13:50 +08003122 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02003123}
3124
Avi Kivity1b7fcd32008-05-15 13:51:35 +03003125static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
3126{
3127 u64 *spte = vcpu->arch.last_pte_updated;
3128
3129 if (spte
3130 && vcpu->arch.last_pte_gfn == gfn
3131 && shadow_accessed_mask
3132 && !(*spte & shadow_accessed_mask)
3133 && is_shadow_present_pte(*spte))
3134 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
3135}
3136
Avi Kivity09072da2007-05-01 14:16:52 +03003137void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003138 const u8 *new, int bytes,
3139 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08003140{
Avi Kivity9b7a0322007-01-05 16:36:45 -08003141 gfn_t gfn = gpa >> PAGE_SHIFT;
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003142 union kvm_mmu_page_role mask = { .word = 0 };
Avi Kivity4db35312007-11-21 15:28:32 +02003143 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003144 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003145 LIST_HEAD(invalid_list);
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08003146 u64 entry, gentry, *spte;
3147 unsigned pte_size, page_offset, misaligned, quadrant, offset;
3148 int level, npte, invlpg_counter, r, flooded = 0;
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003149 bool remote_flush, local_flush, zap_page;
3150
Xiao Guangrong332b2072011-05-15 23:20:27 +08003151 /*
3152 * If we don't have indirect shadow pages, it means no page is
3153 * write-protected, so we can exit simply.
3154 */
3155 if (!ACCESS_ONCE(vcpu->kvm->arch.indirect_shadow_pages))
3156 return;
3157
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003158 zap_page = remote_flush = local_flush = false;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08003159 offset = offset_in_page(gpa);
Avi Kivity9b7a0322007-01-05 16:36:45 -08003160
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003161 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivity72016f32010-03-15 13:59:53 +02003162
Avi Kivity08e850c2010-03-15 13:59:57 +02003163 invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
3164
3165 /*
3166 * Assume that the pte write on a page table of the same type
Xiao Guangrong49b26e22011-03-04 19:00:00 +08003167 * as the current vcpu paging mode since we update the sptes only
3168 * when they have the same mode.
Avi Kivity08e850c2010-03-15 13:59:57 +02003169 */
3170 if ((is_pae(vcpu) && bytes == 4) || !new) {
3171 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
3172 if (is_pae(vcpu)) {
3173 gpa &= ~(gpa_t)7;
3174 bytes = 8;
3175 }
3176 r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
3177 if (r)
3178 gentry = 0;
3179 new = (const u8 *)&gentry;
3180 }
3181
Avi Kivity72016f32010-03-15 13:59:53 +02003182 switch (bytes) {
3183 case 4:
3184 gentry = *(const u32 *)new;
3185 break;
3186 case 8:
3187 gentry = *(const u64 *)new;
3188 break;
3189 default:
3190 gentry = 0;
3191 break;
3192 }
3193
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003194 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity08e850c2010-03-15 13:59:57 +02003195 if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
3196 gentry = 0;
Avi Kivityeb787d12007-12-31 15:27:49 +02003197 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02003198 ++vcpu->kvm->stat.mmu_pte_write;
Xiao Guangrong8b1fe172010-08-30 18:22:53 +08003199 trace_kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE);
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003200 if (guest_initiated) {
Xiao Guangrong1b7fd452011-03-04 18:58:02 +08003201 kvm_mmu_access_page(vcpu, gfn);
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003202 if (gfn == vcpu->arch.last_pt_write_gfn
3203 && !last_updated_pte_accessed(vcpu)) {
3204 ++vcpu->arch.last_pt_write_count;
3205 if (vcpu->arch.last_pt_write_count >= 3)
3206 flooded = 1;
3207 } else {
3208 vcpu->arch.last_pt_write_gfn = gfn;
3209 vcpu->arch.last_pt_write_count = 1;
3210 vcpu->arch.last_pte_updated = NULL;
3211 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08003212 }
Xiao Guangrong3246af02010-04-16 16:35:54 +08003213
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003214 mask.cr0_wp = mask.cr4_pae = mask.nxe = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003215 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node) {
Avi Kivity5b7e0102010-04-14 19:20:03 +03003216 pte_size = sp->role.cr4_pae ? 8 : 4;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003217 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03003218 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08003219 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003220 /*
3221 * Misaligned accesses are too much trouble to fix
3222 * up; also, they usually indicate a page is not used
3223 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08003224 *
3225 * If we're seeing too many writes to a page,
3226 * it may no longer be a page table, or we may be
3227 * forking, in which case it is better to unmap the
3228 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003229 */
3230 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02003231 gpa, bytes, sp->role.word);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003232 zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003233 &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02003234 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003235 continue;
3236 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08003237 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02003238 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02003239 npte = 1;
Avi Kivity5b7e0102010-04-14 19:20:03 +03003240 if (!sp->role.cr4_pae) {
Avi Kivityac1b7142007-03-08 17:13:32 +02003241 page_offset <<= 1; /* 32->64 */
3242 /*
3243 * A 32-bit pde maps 4MB while the shadow pdes map
3244 * only 2MB. So we need to double the offset again
3245 * and zap two pdes instead of one.
3246 */
3247 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03003248 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02003249 page_offset <<= 1;
3250 npte = 2;
3251 }
Avi Kivityfce06572007-05-01 16:44:05 +03003252 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003253 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02003254 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03003255 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003256 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003257 local_flush = true;
Avi Kivity4db35312007-11-21 15:28:32 +02003258 spte = &sp->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02003259 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02003260 entry = *spte;
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08003261 mmu_page_zap_pte(vcpu->kvm, sp, spte);
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003262 if (gentry &&
3263 !((sp->role.word ^ vcpu->arch.mmu.base_role.word)
3264 & mask.word))
Xiao Guangrong7c562522011-03-28 10:29:27 +08003265 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003266 if (!remote_flush && need_remote_flush(entry, *spte))
3267 remote_flush = true;
Avi Kivityac1b7142007-03-08 17:13:32 +02003268 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003269 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08003270 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003271 mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003272 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong8b1fe172010-08-30 18:22:53 +08003273 trace_kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003274 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivityda4a00f2007-01-05 16:36:44 -08003275}
3276
Avi Kivitya4360362007-01-05 16:36:45 -08003277int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
3278{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003279 gpa_t gpa;
3280 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08003281
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02003282 if (vcpu->arch.mmu.direct_map)
Avi Kivity60f24782009-08-27 13:37:06 +03003283 return 0;
3284
Gleb Natapov1871c602010-02-10 14:21:32 +02003285 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003286
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003287 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003288 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003289 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003290 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08003291}
Avi Kivity577bdc42008-07-19 08:57:05 +03003292EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08003293
Avi Kivity22d95b12007-09-14 20:26:06 +03003294void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08003295{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003296 LIST_HEAD(invalid_list);
Xiao Guangrong103ad252010-06-04 21:54:38 +08003297
Dave Hansene0df7b92010-08-19 18:11:05 -07003298 while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES &&
Izik Eidus3b80fff2009-07-28 15:26:58 -03003299 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02003300 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08003301
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003302 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02003303 struct kvm_mmu_page, link);
Dave Hansene0df7b92010-08-19 18:11:05 -07003304 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02003305 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08003306 }
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08003307 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Avi Kivityebeace82007-01-05 16:36:47 -08003308}
Avi Kivityebeace82007-01-05 16:36:47 -08003309
Andre Przywaradc25e892010-12-21 11:12:07 +01003310int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code,
3311 void *insn, int insn_len)
Avi Kivity30677142007-10-28 18:48:59 +02003312{
3313 int r;
3314 enum emulation_result er;
3315
Gleb Natapov56028d02010-10-17 18:13:42 +02003316 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code, false);
Avi Kivity30677142007-10-28 18:48:59 +02003317 if (r < 0)
3318 goto out;
3319
3320 if (!r) {
3321 r = 1;
3322 goto out;
3323 }
3324
Avi Kivityb733bfb2007-10-28 18:52:05 +02003325 r = mmu_topup_memory_caches(vcpu);
3326 if (r)
3327 goto out;
3328
Andre Przywaradc25e892010-12-21 11:12:07 +01003329 er = x86_emulate_instruction(vcpu, cr2, 0, insn, insn_len);
Avi Kivity30677142007-10-28 18:48:59 +02003330
3331 switch (er) {
3332 case EMULATE_DONE:
3333 return 1;
3334 case EMULATE_DO_MMIO:
3335 ++vcpu->stat.mmio_exits;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03003336 /* fall through */
Avi Kivity30677142007-10-28 18:48:59 +02003337 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03003338 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02003339 default:
3340 BUG();
3341 }
3342out:
Avi Kivity30677142007-10-28 18:48:59 +02003343 return r;
3344}
3345EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
3346
Marcelo Tosattia7052892008-09-23 13:18:35 -03003347void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
3348{
Marcelo Tosattia7052892008-09-23 13:18:35 -03003349 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003350 kvm_mmu_flush_tlb(vcpu);
3351 ++vcpu->stat.invlpg;
3352}
3353EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
3354
Joerg Roedel18552672008-02-07 13:47:41 +01003355void kvm_enable_tdp(void)
3356{
3357 tdp_enabled = true;
3358}
3359EXPORT_SYMBOL_GPL(kvm_enable_tdp);
3360
Joerg Roedel5f4cb662008-07-14 20:36:36 +02003361void kvm_disable_tdp(void)
3362{
3363 tdp_enabled = false;
3364}
3365EXPORT_SYMBOL_GPL(kvm_disable_tdp);
3366
Avi Kivity6aa8b732006-12-10 02:21:36 -08003367static void free_mmu_pages(struct kvm_vcpu *vcpu)
3368{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003369 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Joerg Roedel81407ca2010-09-10 17:31:00 +02003370 if (vcpu->arch.mmu.lm_root != NULL)
3371 free_page((unsigned long)vcpu->arch.mmu.lm_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003372}
3373
3374static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
3375{
Avi Kivity17ac10a2007-01-05 16:36:40 -08003376 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003377 int i;
3378
3379 ASSERT(vcpu);
3380
Avi Kivity17ac10a2007-01-05 16:36:40 -08003381 /*
3382 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
3383 * Therefore we need to allocate shadow page tables in the first
3384 * 4GB of memory, which happens to fit the DMA32 zone.
3385 */
3386 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
3387 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08003388 return -ENOMEM;
3389
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003390 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003391 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003392 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003393
Avi Kivity6aa8b732006-12-10 02:21:36 -08003394 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003395}
3396
Ingo Molnar8018c272006-12-29 16:50:01 -08003397int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003398{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003399 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003400 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003401
Ingo Molnar8018c272006-12-29 16:50:01 -08003402 return alloc_mmu_pages(vcpu);
3403}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003404
Ingo Molnar8018c272006-12-29 16:50:01 -08003405int kvm_mmu_setup(struct kvm_vcpu *vcpu)
3406{
3407 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003408 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08003409
Ingo Molnar8018c272006-12-29 16:50:01 -08003410 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003411}
3412
Avi Kivity90cb0522007-07-17 13:04:56 +03003413void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003414{
Avi Kivity4db35312007-11-21 15:28:32 +02003415 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003416
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003417 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003418 int i;
3419 u64 *pt;
3420
Sheng Yang291f26b2008-10-16 17:30:57 +08003421 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003422 continue;
3423
Avi Kivity4db35312007-11-21 15:28:32 +02003424 pt = sp->spt;
Avi Kivity8234b222010-12-27 12:08:45 +02003425 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Xiao Guangrongda8dc752011-03-04 18:56:41 +08003426 if (!is_shadow_present_pte(pt[i]) ||
3427 !is_last_spte(pt[i], sp->role.level))
3428 continue;
3429
3430 if (is_large_pte(pt[i])) {
Avi Kivity8234b222010-12-27 12:08:45 +02003431 drop_spte(kvm, &pt[i],
3432 shadow_trap_nonpresent_pte);
3433 --kvm->stat.lpages;
Xiao Guangrongda8dc752011-03-04 18:56:41 +08003434 continue;
Avi Kivity8234b222010-12-27 12:08:45 +02003435 }
Xiao Guangrongda8dc752011-03-04 18:56:41 +08003436
Avi Kivity6aa8b732006-12-10 02:21:36 -08003437 /* avoid RMW */
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003438 if (is_writable_pte(pt[i]))
Takuya Yoshikawa700e1b12010-12-06 01:11:33 +09003439 update_spte(&pt[i], pt[i] & ~PT_WRITABLE_MASK);
Avi Kivity8234b222010-12-27 12:08:45 +02003440 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003441 }
Avi Kivity171d5952008-08-27 16:40:51 +03003442 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003443}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003444
Avi Kivity90cb0522007-07-17 13:04:56 +03003445void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03003446{
Avi Kivity4db35312007-11-21 15:28:32 +02003447 struct kvm_mmu_page *sp, *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003448 LIST_HEAD(invalid_list);
Dor Laore0fa8262007-03-30 13:06:33 +03003449
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003450 spin_lock(&kvm->mmu_lock);
Xiao Guangrong3246af02010-04-16 16:35:54 +08003451restart:
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003452 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003453 if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list))
Xiao Guangrong3246af02010-04-16 16:35:54 +08003454 goto restart;
3455
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003456 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003457 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03003458}
3459
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003460static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm,
3461 struct list_head *invalid_list)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003462{
3463 struct kvm_mmu_page *page;
3464
3465 page = container_of(kvm->arch.active_mmu_pages.prev,
3466 struct kvm_mmu_page, link);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003467 return kvm_mmu_prepare_zap_page(kvm, page, invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003468}
3469
Ying Han1495f232011-05-24 17:12:27 -07003470static int mmu_shrink(struct shrinker *shrink, struct shrink_control *sc)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003471{
3472 struct kvm *kvm;
3473 struct kvm *kvm_freed = NULL;
Ying Han1495f232011-05-24 17:12:27 -07003474 int nr_to_scan = sc->nr_to_scan;
Dave Hansen45221ab2010-08-19 18:11:37 -07003475
3476 if (nr_to_scan == 0)
3477 goto out;
Izik Eidus3ee16c82008-03-30 15:17:21 +03003478
Jan Kiszkae935b832011-02-08 12:55:33 +01003479 raw_spin_lock(&kvm_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003480
3481 list_for_each_entry(kvm, &vm_list, vm_list) {
Dave Hansen45221ab2010-08-19 18:11:37 -07003482 int idx, freed_pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003483 LIST_HEAD(invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003484
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003485 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003486 spin_lock(&kvm->mmu_lock);
Dave Hansen45221ab2010-08-19 18:11:37 -07003487 if (!kvm_freed && nr_to_scan > 0 &&
3488 kvm->arch.n_used_mmu_pages > 0) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003489 freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm,
3490 &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003491 kvm_freed = kvm;
3492 }
3493 nr_to_scan--;
3494
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003495 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003496 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003497 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003498 }
3499 if (kvm_freed)
3500 list_move_tail(&kvm_freed->vm_list, &vm_list);
3501
Jan Kiszkae935b832011-02-08 12:55:33 +01003502 raw_spin_unlock(&kvm_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003503
Dave Hansen45221ab2010-08-19 18:11:37 -07003504out:
3505 return percpu_counter_read_positive(&kvm_total_used_mmu_pages);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003506}
3507
3508static struct shrinker mmu_shrinker = {
3509 .shrink = mmu_shrink,
3510 .seeks = DEFAULT_SEEKS * 10,
3511};
3512
Ingo Molnar2ddfd202008-05-22 10:37:48 +02003513static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03003514{
Xiao Guangrong53c07b12011-05-15 23:26:20 +08003515 if (pte_list_desc_cache)
3516 kmem_cache_destroy(pte_list_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003517 if (mmu_page_header_cache)
3518 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003519}
3520
3521int kvm_mmu_module_init(void)
3522{
Xiao Guangrong53c07b12011-05-15 23:26:20 +08003523 pte_list_desc_cache = kmem_cache_create("pte_list_desc",
3524 sizeof(struct pte_list_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09003525 0, 0, NULL);
Xiao Guangrong53c07b12011-05-15 23:26:20 +08003526 if (!pte_list_desc_cache)
Avi Kivityb5a33a72007-04-15 16:31:09 +03003527 goto nomem;
3528
Avi Kivityd3d25b02007-05-30 12:34:53 +03003529 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
3530 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09003531 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003532 if (!mmu_page_header_cache)
3533 goto nomem;
3534
Wei Yongjun45bf21a2010-08-23 16:13:15 +08003535 if (percpu_counter_init(&kvm_total_used_mmu_pages, 0))
3536 goto nomem;
3537
Izik Eidus3ee16c82008-03-30 15:17:21 +03003538 register_shrinker(&mmu_shrinker);
3539
Avi Kivityb5a33a72007-04-15 16:31:09 +03003540 return 0;
3541
3542nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03003543 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003544 return -ENOMEM;
3545}
3546
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003547/*
3548 * Caculate mmu pages needed for kvm.
3549 */
3550unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3551{
3552 int i;
3553 unsigned int nr_mmu_pages;
3554 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003555 struct kvm_memslots *slots;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003556
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003557 slots = kvm_memslots(kvm);
3558
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003559 for (i = 0; i < slots->nmemslots; i++)
3560 nr_pages += slots->memslots[i].npages;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003561
3562 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3563 nr_mmu_pages = max(nr_mmu_pages,
3564 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3565
3566 return nr_mmu_pages;
3567}
3568
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003569static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3570 unsigned len)
3571{
3572 if (len > buffer->len)
3573 return NULL;
3574 return buffer->ptr;
3575}
3576
3577static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3578 unsigned len)
3579{
3580 void *ret;
3581
3582 ret = pv_mmu_peek_buffer(buffer, len);
3583 if (!ret)
3584 return ret;
3585 buffer->ptr += len;
3586 buffer->len -= len;
3587 buffer->processed += len;
3588 return ret;
3589}
3590
3591static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3592 gpa_t addr, gpa_t value)
3593{
3594 int bytes = 8;
3595 int r;
3596
3597 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3598 bytes = 4;
3599
3600 r = mmu_topup_memory_caches(vcpu);
3601 if (r)
3602 return r;
3603
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003604 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003605 return -EFAULT;
3606
3607 return 1;
3608}
3609
3610static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3611{
Avi Kivity9f8fe502010-12-05 17:30:00 +02003612 (void)kvm_set_cr3(vcpu, kvm_read_cr3(vcpu));
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003613 return 1;
3614}
3615
3616static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3617{
3618 spin_lock(&vcpu->kvm->mmu_lock);
3619 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3620 spin_unlock(&vcpu->kvm->mmu_lock);
3621 return 1;
3622}
3623
3624static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3625 struct kvm_pv_mmu_op_buffer *buffer)
3626{
3627 struct kvm_mmu_op_header *header;
3628
3629 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3630 if (!header)
3631 return 0;
3632 switch (header->op) {
3633 case KVM_MMU_OP_WRITE_PTE: {
3634 struct kvm_mmu_op_write_pte *wpte;
3635
3636 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3637 if (!wpte)
3638 return 0;
3639 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3640 wpte->pte_val);
3641 }
3642 case KVM_MMU_OP_FLUSH_TLB: {
3643 struct kvm_mmu_op_flush_tlb *ftlb;
3644
3645 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3646 if (!ftlb)
3647 return 0;
3648 return kvm_pv_mmu_flush_tlb(vcpu);
3649 }
3650 case KVM_MMU_OP_RELEASE_PT: {
3651 struct kvm_mmu_op_release_pt *rpt;
3652
3653 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3654 if (!rpt)
3655 return 0;
3656 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3657 }
3658 default: return 0;
3659 }
3660}
3661
3662int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3663 gpa_t addr, unsigned long *ret)
3664{
3665 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003666 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003667
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003668 buffer->ptr = buffer->buf;
3669 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3670 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003671
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003672 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003673 if (r)
3674 goto out;
3675
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003676 while (buffer->len) {
3677 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003678 if (r < 0)
3679 goto out;
3680 if (r == 0)
3681 break;
3682 }
3683
3684 r = 1;
3685out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003686 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003687 return r;
3688}
3689
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003690int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3691{
3692 struct kvm_shadow_walk_iterator iterator;
3693 int nr_sptes = 0;
3694
3695 spin_lock(&vcpu->kvm->mmu_lock);
3696 for_each_shadow_entry(vcpu, addr, iterator) {
3697 sptes[iterator.level-1] = *iterator.sptep;
3698 nr_sptes++;
3699 if (!is_shadow_present_pte(*iterator.sptep))
3700 break;
3701 }
3702 spin_unlock(&vcpu->kvm->mmu_lock);
3703
3704 return nr_sptes;
3705}
3706EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3707
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08003708void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
3709{
3710 ASSERT(vcpu);
3711
3712 destroy_kvm_mmu(vcpu);
3713 free_mmu_pages(vcpu);
3714 mmu_free_memory_caches(vcpu);
Xiao Guangrongb034cf02010-12-23 16:08:35 +08003715}
3716
3717#ifdef CONFIG_KVM_MMU_AUDIT
3718#include "mmu_audit.c"
3719#else
3720static void mmu_audit_disable(void) { }
3721#endif
3722
3723void kvm_mmu_module_exit(void)
3724{
3725 mmu_destroy_caches();
3726 percpu_counter_destroy(&kvm_total_used_mmu_pages);
3727 unregister_shrinker(&mmu_shrinker);
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08003728 mmu_audit_disable();
3729}