blob: b4ae7afa6b3bdbed2a089eeea2d4e7550a64375a [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
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800151#define RMAP_EXT 4
152
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
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800167struct kvm_rmap_desc {
Avi Kivityd555c332009-06-10 14:24:23 +0300168 u64 *sptes[RMAP_EXT];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800169 struct kvm_rmap_desc *more;
170};
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 Guangrong1047df12010-06-11 21:35:15 +0800185typedef void (*mmu_parent_walk_fn) (struct kvm_mmu_page *sp, u64 *spte);
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300186
Avi Kivityb5a33a72007-04-15 16:31:09 +0300187static struct kmem_cache *pte_chain_cache;
188static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300189static struct kmem_cache *mmu_page_header_cache;
Dave Hansen45221ab2010-08-19 18:11:37 -0700190static struct percpu_counter kvm_total_used_mmu_pages;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300191
Avi Kivityc7addb92007-09-16 18:58:32 +0200192static u64 __read_mostly shadow_trap_nonpresent_pte;
193static u64 __read_mostly shadow_notrap_nonpresent_pte;
Sheng Yang7b523452008-04-25 21:13:50 +0800194static u64 __read_mostly shadow_nx_mask;
195static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
196static u64 __read_mostly shadow_user_mask;
197static u64 __read_mostly shadow_accessed_mask;
198static u64 __read_mostly shadow_dirty_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200199
Dong, Eddie82725b22009-03-30 16:21:08 +0800200static inline u64 rsvd_bits(int s, int e)
201{
202 return ((1ULL << (e - s + 1)) - 1) << s;
203}
204
Avi Kivityc7addb92007-09-16 18:58:32 +0200205void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
206{
207 shadow_trap_nonpresent_pte = trap_pte;
208 shadow_notrap_nonpresent_pte = notrap_pte;
209}
210EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
211
Sheng Yang7b523452008-04-25 21:13:50 +0800212void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang4b12f0d2009-04-27 20:35:42 +0800213 u64 dirty_mask, u64 nx_mask, u64 x_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800214{
215 shadow_user_mask = user_mask;
216 shadow_accessed_mask = accessed_mask;
217 shadow_dirty_mask = dirty_mask;
218 shadow_nx_mask = nx_mask;
219 shadow_x_mask = x_mask;
220}
221EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
222
Avi Kivity3dbe1412010-05-12 11:48:18 +0300223static bool is_write_protection(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800224{
Avi Kivity4d4ec082009-12-29 18:07:30 +0200225 return kvm_read_cr0_bits(vcpu, X86_CR0_WP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800226}
227
228static int is_cpuid_PSE36(void)
229{
230 return 1;
231}
232
Avi Kivity73b10872007-01-26 00:56:41 -0800233static int is_nx(struct kvm_vcpu *vcpu)
234{
Avi Kivityf6801df2010-01-21 15:31:50 +0200235 return vcpu->arch.efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800236}
237
Avi Kivityc7addb92007-09-16 18:58:32 +0200238static int is_shadow_present_pte(u64 pte)
239{
Avi Kivityc7addb92007-09-16 18:58:32 +0200240 return pte != shadow_trap_nonpresent_pte
241 && pte != shadow_notrap_nonpresent_pte;
242}
243
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300244static int is_large_pte(u64 pte)
245{
246 return pte & PT_PAGE_SIZE_MASK;
247}
248
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900249static int is_writable_pte(unsigned long pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800250{
251 return pte & PT_WRITABLE_MASK;
252}
253
Avi Kivity43a37952009-06-10 14:12:05 +0300254static int is_dirty_gpte(unsigned long pte)
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200255{
Avi Kivity439e2182009-06-10 12:56:54 +0300256 return pte & PT_DIRTY_MASK;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200257}
258
Avi Kivity43a37952009-06-10 14:12:05 +0300259static int is_rmap_spte(u64 pte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800260{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200261 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800262}
263
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300264static int is_last_spte(u64 pte, int level)
265{
266 if (level == PT_PAGE_TABLE_LEVEL)
267 return 1;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200268 if (is_large_pte(pte))
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300269 return 1;
270 return 0;
271}
272
Anthony Liguori35149e22008-04-02 14:46:56 -0500273static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200274{
Anthony Liguori35149e22008-04-02 14:46:56 -0500275 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200276}
277
Avi Kivityda9285212007-11-21 13:54:47 +0200278static gfn_t pse36_gfn_delta(u32 gpte)
279{
280 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
281
282 return (gpte & PT32_DIR_PSE36_MASK) << shift;
283}
284
Avi Kivityd555c332009-06-10 14:24:23 +0300285static void __set_spte(u64 *sptep, u64 spte)
Avi Kivitye663ee62007-05-31 15:46:04 +0300286{
H. Peter Anvin7645e432010-08-06 12:18:11 -0700287 set_64bit(sptep, spte);
Avi Kivitye663ee62007-05-31 15:46:04 +0300288}
289
Avi Kivitya9221dd2010-06-06 14:48:06 +0300290static u64 __xchg_spte(u64 *sptep, u64 new_spte)
291{
292#ifdef CONFIG_X86_64
293 return xchg(sptep, new_spte);
294#else
295 u64 old_spte;
296
297 do {
298 old_spte = *sptep;
299 } while (cmpxchg64(sptep, old_spte, new_spte) != old_spte);
300
301 return old_spte;
302#endif
303}
304
Xiao Guangrong8672b722010-08-02 16:14:04 +0800305static bool spte_has_volatile_bits(u64 spte)
306{
307 if (!shadow_accessed_mask)
308 return false;
309
310 if (!is_shadow_present_pte(spte))
311 return false;
312
Xiao Guangrong41327792010-08-02 16:15:08 +0800313 if ((spte & shadow_accessed_mask) &&
314 (!is_writable_pte(spte) || (spte & shadow_dirty_mask)))
Xiao Guangrong8672b722010-08-02 16:14:04 +0800315 return false;
316
317 return true;
318}
319
Xiao Guangrong41327792010-08-02 16:15:08 +0800320static bool spte_is_bit_cleared(u64 old_spte, u64 new_spte, u64 bit_mask)
321{
322 return (old_spte & bit_mask) && !(new_spte & bit_mask);
323}
324
Avi Kivityb79b93f2010-06-06 15:46:44 +0300325static void update_spte(u64 *sptep, u64 new_spte)
326{
Xiao Guangrong41327792010-08-02 16:15:08 +0800327 u64 mask, old_spte = *sptep;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300328
Xiao Guangrong41327792010-08-02 16:15:08 +0800329 WARN_ON(!is_rmap_spte(new_spte));
330
331 new_spte |= old_spte & shadow_dirty_mask;
332
333 mask = shadow_accessed_mask;
334 if (is_writable_pte(old_spte))
335 mask |= shadow_dirty_mask;
336
337 if (!spte_has_volatile_bits(old_spte) || (new_spte & mask) == mask)
Avi Kivityb79b93f2010-06-06 15:46:44 +0300338 __set_spte(sptep, new_spte);
Xiao Guangrong41327792010-08-02 16:15:08 +0800339 else
Avi Kivityb79b93f2010-06-06 15:46:44 +0300340 old_spte = __xchg_spte(sptep, new_spte);
Xiao Guangrong41327792010-08-02 16:15:08 +0800341
342 if (!shadow_accessed_mask)
343 return;
344
345 if (spte_is_bit_cleared(old_spte, new_spte, shadow_accessed_mask))
346 kvm_set_pfn_accessed(spte_to_pfn(old_spte));
347 if (spte_is_bit_cleared(old_spte, new_spte, shadow_dirty_mask))
348 kvm_set_pfn_dirty(spte_to_pfn(old_spte));
Avi Kivityb79b93f2010-06-06 15:46:44 +0300349}
350
Avi Kivitye2dec932007-01-05 16:36:54 -0800351static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300352 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800353{
354 void *obj;
355
356 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800357 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800358 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300359 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800360 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800361 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800362 cache->objects[cache->nobjs++] = obj;
363 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800364 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800365}
366
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800367static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc,
368 struct kmem_cache *cache)
Avi Kivity714b93d2007-01-05 16:36:53 -0800369{
370 while (mc->nobjs)
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800371 kmem_cache_free(cache, mc->objects[--mc->nobjs]);
Avi Kivity714b93d2007-01-05 16:36:53 -0800372}
373
Avi Kivityc1158e62007-07-20 08:18:27 +0300374static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300375 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300376{
Xiao Guangrong842f22e2011-03-04 19:01:10 +0800377 void *page;
Avi Kivityc1158e62007-07-20 08:18:27 +0300378
379 if (cache->nobjs >= min)
380 return 0;
381 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Xiao Guangrong842f22e2011-03-04 19:01:10 +0800382 page = (void *)__get_free_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300383 if (!page)
384 return -ENOMEM;
Xiao Guangrong842f22e2011-03-04 19:01:10 +0800385 cache->objects[cache->nobjs++] = page;
Avi Kivityc1158e62007-07-20 08:18:27 +0300386 }
387 return 0;
388}
389
390static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
391{
392 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300393 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300394}
395
Avi Kivity8c438502007-04-16 11:53:17 +0300396static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
397{
398 int r;
399
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800400 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300401 pte_chain_cache, 4);
402 if (r)
403 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800404 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Xiao Guangrong957ed9e2010-08-22 19:12:48 +0800405 rmap_desc_cache, 4 + PTE_PREFETCH_NUM);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300406 if (r)
407 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800408 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300409 if (r)
410 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800411 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300412 mmu_page_header_cache, 4);
413out:
Avi Kivity8c438502007-04-16 11:53:17 +0300414 return r;
415}
416
Avi Kivity714b93d2007-01-05 16:36:53 -0800417static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
418{
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800419 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache, pte_chain_cache);
420 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache, rmap_desc_cache);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800421 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800422 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache,
423 mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800424}
425
426static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
427 size_t size)
428{
429 void *p;
430
431 BUG_ON(!mc->nobjs);
432 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800433 return p;
434}
435
Avi Kivity714b93d2007-01-05 16:36:53 -0800436static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
437{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800438 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800439 sizeof(struct kvm_pte_chain));
440}
441
Avi Kivity90cb0522007-07-17 13:04:56 +0300442static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800443{
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800444 kmem_cache_free(pte_chain_cache, pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800445}
446
447static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
448{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800449 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800450 sizeof(struct kvm_rmap_desc));
451}
452
Avi Kivity90cb0522007-07-17 13:04:56 +0300453static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800454{
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800455 kmem_cache_free(rmap_desc_cache, rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800456}
457
Lai Jiangshan2032a932010-05-26 16:49:59 +0800458static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
459{
460 if (!sp->role.direct)
461 return sp->gfns[index];
462
463 return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS));
464}
465
466static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn)
467{
468 if (sp->role.direct)
469 BUG_ON(gfn != kvm_mmu_page_get_gfn(sp, index));
470 else
471 sp->gfns[index] = gfn;
472}
473
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800474/*
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900475 * Return the pointer to the large page information for a given gfn,
476 * handling slots that are not large page aligned.
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300477 */
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900478static struct kvm_lpage_info *lpage_info_slot(gfn_t gfn,
479 struct kvm_memory_slot *slot,
480 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300481{
482 unsigned long idx;
483
Joerg Roedel82855412010-07-01 16:00:11 +0200484 idx = (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
485 (slot->base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900486 return &slot->lpage_info[level - 2][idx];
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300487}
488
489static void account_shadowed(struct kvm *kvm, gfn_t gfn)
490{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200491 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900492 struct kvm_lpage_info *linfo;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200493 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300494
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300495 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200496 for (i = PT_DIRECTORY_LEVEL;
497 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900498 linfo = lpage_info_slot(gfn, slot, i);
499 linfo->write_count += 1;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200500 }
Xiao Guangrong332b2072011-05-15 23:20:27 +0800501 kvm->arch.indirect_shadow_pages++;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300502}
503
504static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
505{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200506 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900507 struct kvm_lpage_info *linfo;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200508 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300509
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300510 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200511 for (i = PT_DIRECTORY_LEVEL;
512 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900513 linfo = lpage_info_slot(gfn, slot, i);
514 linfo->write_count -= 1;
515 WARN_ON(linfo->write_count < 0);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200516 }
Xiao Guangrong332b2072011-05-15 23:20:27 +0800517 kvm->arch.indirect_shadow_pages--;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300518}
519
Joerg Roedeld25797b2009-07-27 16:30:43 +0200520static int has_wrprotected_page(struct kvm *kvm,
521 gfn_t gfn,
522 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300523{
Izik Eidus28430992008-10-03 17:40:32 +0300524 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900525 struct kvm_lpage_info *linfo;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300526
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300527 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300528 if (slot) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900529 linfo = lpage_info_slot(gfn, slot, level);
530 return linfo->write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300531 }
532
533 return 1;
534}
535
Joerg Roedeld25797b2009-07-27 16:30:43 +0200536static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300537{
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100538 unsigned long page_size;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200539 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300540
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100541 page_size = kvm_host_page_size(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300542
Joerg Roedeld25797b2009-07-27 16:30:43 +0200543 for (i = PT_PAGE_TABLE_LEVEL;
544 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
545 if (page_size >= KVM_HPAGE_SIZE(i))
546 ret = i;
547 else
548 break;
549 }
550
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300551 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300552}
553
Xiao Guangrong5d163b12011-03-09 15:43:00 +0800554static struct kvm_memory_slot *
555gfn_to_memslot_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t gfn,
556 bool no_dirty_log)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300557{
558 struct kvm_memory_slot *slot;
Xiao Guangrong5d163b12011-03-09 15:43:00 +0800559
560 slot = gfn_to_memslot(vcpu->kvm, gfn);
561 if (!slot || slot->flags & KVM_MEMSLOT_INVALID ||
562 (no_dirty_log && slot->dirty_bitmap))
563 slot = NULL;
564
565 return slot;
566}
567
568static bool mapping_level_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t large_gfn)
569{
Stevea0a8eab2011-06-17 10:25:39 +0800570 return !gfn_to_memslot_dirty_bitmap(vcpu, large_gfn, true);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800571}
572
573static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
574{
575 int host_level, level, max_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300576
Joerg Roedeld25797b2009-07-27 16:30:43 +0200577 host_level = host_mapping_level(vcpu->kvm, large_gfn);
578
579 if (host_level == PT_PAGE_TABLE_LEVEL)
580 return host_level;
581
Sheng Yang878403b2010-01-05 19:02:29 +0800582 max_level = kvm_x86_ops->get_lpage_level() < host_level ?
583 kvm_x86_ops->get_lpage_level() : host_level;
584
585 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200586 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
587 break;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200588
589 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300590}
591
592/*
Izik Eidus290fc382007-09-27 14:11:22 +0200593 * Take gfn and return the reverse mapping to it.
Izik Eidus290fc382007-09-27 14:11:22 +0200594 */
595
Joerg Roedel44ad9942009-07-27 16:30:42 +0200596static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
Izik Eidus290fc382007-09-27 14:11:22 +0200597{
598 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900599 struct kvm_lpage_info *linfo;
Izik Eidus290fc382007-09-27 14:11:22 +0200600
601 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200602 if (likely(level == PT_PAGE_TABLE_LEVEL))
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300603 return &slot->rmap[gfn - slot->base_gfn];
604
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900605 linfo = lpage_info_slot(gfn, slot, level);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300606
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900607 return &linfo->rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200608}
609
610/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800611 * Reverse mapping data structures:
612 *
Izik Eidus290fc382007-09-27 14:11:22 +0200613 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
614 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800615 *
Izik Eidus290fc382007-09-27 14:11:22 +0200616 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
617 * containing more mappings.
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300618 *
619 * Returns the number of rmap entries before the spte was added or zero if
620 * the spte was not added.
621 *
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800622 */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200623static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800624{
Avi Kivity4db35312007-11-21 15:28:32 +0200625 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800626 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200627 unsigned long *rmapp;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300628 int i, count = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800629
Avi Kivity43a37952009-06-10 14:12:05 +0300630 if (!is_rmap_spte(*spte))
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300631 return count;
Avi Kivity4db35312007-11-21 15:28:32 +0200632 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800633 kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200634 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200635 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800636 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200637 *rmapp = (unsigned long)spte;
638 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800639 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800640 desc = mmu_alloc_rmap_desc(vcpu);
Avi Kivityd555c332009-06-10 14:24:23 +0300641 desc->sptes[0] = (u64 *)*rmapp;
642 desc->sptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200643 *rmapp = (unsigned long)desc | 1;
Hillf Dantoncb16a7b2010-09-18 08:41:02 +0800644 ++count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800645 } else {
646 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200647 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivityd555c332009-06-10 14:24:23 +0300648 while (desc->sptes[RMAP_EXT-1] && desc->more) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800649 desc = desc->more;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300650 count += RMAP_EXT;
651 }
Avi Kivityd555c332009-06-10 14:24:23 +0300652 if (desc->sptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800653 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800654 desc = desc->more;
655 }
Avi Kivityd555c332009-06-10 14:24:23 +0300656 for (i = 0; desc->sptes[i]; ++i)
Hillf Dantoncb16a7b2010-09-18 08:41:02 +0800657 ++count;
Avi Kivityd555c332009-06-10 14:24:23 +0300658 desc->sptes[i] = spte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800659 }
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300660 return count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800661}
662
Izik Eidus290fc382007-09-27 14:11:22 +0200663static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800664 struct kvm_rmap_desc *desc,
665 int i,
666 struct kvm_rmap_desc *prev_desc)
667{
668 int j;
669
Avi Kivityd555c332009-06-10 14:24:23 +0300670 for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800671 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300672 desc->sptes[i] = desc->sptes[j];
673 desc->sptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800674 if (j != 0)
675 return;
676 if (!prev_desc && !desc->more)
Avi Kivityd555c332009-06-10 14:24:23 +0300677 *rmapp = (unsigned long)desc->sptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800678 else
679 if (prev_desc)
680 prev_desc->more = desc->more;
681 else
Izik Eidus290fc382007-09-27 14:11:22 +0200682 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300683 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800684}
685
Izik Eidus290fc382007-09-27 14:11:22 +0200686static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800687{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800688 struct kvm_rmap_desc *desc;
689 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200690 struct kvm_mmu_page *sp;
Lai Jiangshan2032a932010-05-26 16:49:59 +0800691 gfn_t gfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200692 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800693 int i;
694
Avi Kivity4db35312007-11-21 15:28:32 +0200695 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800696 gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt);
697 rmapp = gfn_to_rmap(kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200698 if (!*rmapp) {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800699 printk(KERN_ERR "rmap_remove: %p 0->BUG\n", spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800700 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200701 } else if (!(*rmapp & 1)) {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800702 rmap_printk("rmap_remove: %p 1->0\n", spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200703 if ((u64 *)*rmapp != spte) {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800704 printk(KERN_ERR "rmap_remove: %p 1->BUG\n", spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800705 BUG();
706 }
Izik Eidus290fc382007-09-27 14:11:22 +0200707 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800708 } else {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800709 rmap_printk("rmap_remove: %p many->many\n", spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200710 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800711 prev_desc = NULL;
712 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300713 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
714 if (desc->sptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200715 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800716 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800717 prev_desc);
718 return;
719 }
720 prev_desc = desc;
721 desc = desc->more;
722 }
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800723 pr_err("rmap_remove: %p many->many\n", spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800724 BUG();
725 }
726}
727
Marcelo Tosattieb45fda2010-10-25 11:58:22 -0200728static int set_spte_track_bits(u64 *sptep, u64 new_spte)
Avi Kivitybe38d272010-06-06 14:31:27 +0300729{
Avi Kivityce061862010-06-06 14:38:12 +0300730 pfn_t pfn;
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800731 u64 old_spte = *sptep;
Avi Kivityce061862010-06-06 14:38:12 +0300732
Xiao Guangrong8672b722010-08-02 16:14:04 +0800733 if (!spte_has_volatile_bits(old_spte))
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800734 __set_spte(sptep, new_spte);
Xiao Guangrong8672b722010-08-02 16:14:04 +0800735 else
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800736 old_spte = __xchg_spte(sptep, new_spte);
737
Avi Kivitya9221dd2010-06-06 14:48:06 +0300738 if (!is_rmap_spte(old_spte))
Marcelo Tosattieb45fda2010-10-25 11:58:22 -0200739 return 0;
Xiao Guangrong8672b722010-08-02 16:14:04 +0800740
Avi Kivitya9221dd2010-06-06 14:48:06 +0300741 pfn = spte_to_pfn(old_spte);
Xiao Guangrongdaa3db62010-07-16 11:23:04 +0800742 if (!shadow_accessed_mask || old_spte & shadow_accessed_mask)
Avi Kivityce061862010-06-06 14:38:12 +0300743 kvm_set_pfn_accessed(pfn);
Xiao Guangrong41327792010-08-02 16:15:08 +0800744 if (!shadow_dirty_mask || (old_spte & shadow_dirty_mask))
Avi Kivityce061862010-06-06 14:38:12 +0300745 kvm_set_pfn_dirty(pfn);
Marcelo Tosattieb45fda2010-10-25 11:58:22 -0200746 return 1;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800747}
748
749static void drop_spte(struct kvm *kvm, u64 *sptep, u64 new_spte)
750{
Marcelo Tosattieb45fda2010-10-25 11:58:22 -0200751 if (set_spte_track_bits(sptep, new_spte))
752 rmap_remove(kvm, sptep);
Avi Kivitybe38d272010-06-06 14:31:27 +0300753}
754
Izik Eidus98348e92007-10-16 14:42:30 +0200755static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800756{
Avi Kivity374cbac2007-01-05 16:36:43 -0800757 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200758 u64 *prev_spte;
759 int i;
760
761 if (!*rmapp)
762 return NULL;
763 else if (!(*rmapp & 1)) {
764 if (!spte)
765 return (u64 *)*rmapp;
766 return NULL;
767 }
768 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Izik Eidus98348e92007-10-16 14:42:30 +0200769 prev_spte = NULL;
770 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300771 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
Izik Eidus98348e92007-10-16 14:42:30 +0200772 if (prev_spte == spte)
Avi Kivityd555c332009-06-10 14:24:23 +0300773 return desc->sptes[i];
774 prev_spte = desc->sptes[i];
Izik Eidus98348e92007-10-16 14:42:30 +0200775 }
776 desc = desc->more;
777 }
778 return NULL;
779}
780
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200781static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200782{
Izik Eidus290fc382007-09-27 14:11:22 +0200783 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800784 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200785 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800786
Joerg Roedel44ad9942009-07-27 16:30:42 +0200787 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
Avi Kivity374cbac2007-01-05 16:36:43 -0800788
Izik Eidus98348e92007-10-16 14:42:30 +0200789 spte = rmap_next(kvm, rmapp, NULL);
790 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800791 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800792 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800793 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900794 if (is_writable_pte(*spte)) {
Avi Kivityb79b93f2010-06-06 15:46:44 +0300795 update_spte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800796 write_protected = 1;
797 }
Izik Eidus9647c142007-10-16 14:43:46 +0200798 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800799 }
Izik Eidus855149a2008-03-20 18:17:24 +0200800
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300801 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200802 for (i = PT_DIRECTORY_LEVEL;
803 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
804 rmapp = gfn_to_rmap(kvm, gfn, i);
805 spte = rmap_next(kvm, rmapp, NULL);
806 while (spte) {
807 BUG_ON(!spte);
808 BUG_ON(!(*spte & PT_PRESENT_MASK));
809 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
810 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900811 if (is_writable_pte(*spte)) {
Avi Kivitybe38d272010-06-06 14:31:27 +0300812 drop_spte(kvm, spte,
813 shadow_trap_nonpresent_pte);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200814 --kvm->stat.lpages;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200815 spte = NULL;
816 write_protected = 1;
817 }
818 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300819 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300820 }
821
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200822 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800823}
824
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000825static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
826 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200827{
828 u64 *spte;
829 int need_tlb_flush = 0;
830
831 while ((spte = rmap_next(kvm, rmapp, NULL))) {
832 BUG_ON(!(*spte & PT_PRESENT_MASK));
833 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
Avi Kivitybe38d272010-06-06 14:31:27 +0300834 drop_spte(kvm, spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200835 need_tlb_flush = 1;
836 }
837 return need_tlb_flush;
838}
839
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000840static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
841 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +0300842{
843 int need_flush = 0;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800844 u64 *spte, new_spte;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300845 pte_t *ptep = (pte_t *)data;
846 pfn_t new_pfn;
847
848 WARN_ON(pte_huge(*ptep));
849 new_pfn = pte_pfn(*ptep);
850 spte = rmap_next(kvm, rmapp, NULL);
851 while (spte) {
852 BUG_ON(!is_shadow_present_pte(*spte));
853 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
854 need_flush = 1;
855 if (pte_write(*ptep)) {
Avi Kivitybe38d272010-06-06 14:31:27 +0300856 drop_spte(kvm, spte, shadow_trap_nonpresent_pte);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300857 spte = rmap_next(kvm, rmapp, NULL);
858 } else {
859 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
860 new_spte |= (u64)new_pfn << PAGE_SHIFT;
861
862 new_spte &= ~PT_WRITABLE_MASK;
863 new_spte &= ~SPTE_HOST_WRITEABLE;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300864 new_spte &= ~shadow_accessed_mask;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800865 set_spte_track_bits(spte, new_spte);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300866 spte = rmap_next(kvm, rmapp, spte);
867 }
868 }
869 if (need_flush)
870 kvm_flush_remote_tlbs(kvm);
871
872 return 0;
873}
874
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000875static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
876 unsigned long data,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300877 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000878 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200879{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200880 int i, j;
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200881 int ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200882 int retval = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200883 struct kvm_memslots *slots;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200884
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800885 slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200886
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200887 for (i = 0; i < slots->nmemslots; i++) {
888 struct kvm_memory_slot *memslot = &slots->memslots[i];
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200889 unsigned long start = memslot->userspace_addr;
890 unsigned long end;
891
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200892 end = start + (memslot->npages << PAGE_SHIFT);
893 if (hva >= start && hva < end) {
894 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900895 gfn_t gfn = memslot->base_gfn + gfn_offset;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200896
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200897 ret = handler(kvm, &memslot->rmap[gfn_offset], data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200898
899 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900900 struct kvm_lpage_info *linfo;
Andrea Arcangeli6e3e2432010-07-16 11:52:55 +0200901
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900902 linfo = lpage_info_slot(gfn, memslot,
903 PT_DIRECTORY_LEVEL + j);
904 ret |= handler(kvm, &linfo->rmap_pde, data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200905 }
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200906 trace_kvm_age_page(hva, memslot, ret);
907 retval |= ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200908 }
909 }
910
911 return retval;
912}
913
914int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
915{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300916 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200917}
918
Izik Eidus3da0dd42009-09-23 21:47:18 +0300919void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
920{
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000921 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300922}
923
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000924static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
925 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200926{
927 u64 *spte;
928 int young = 0;
929
Rik van Riel6316e1c2010-02-03 16:11:03 -0500930 /*
931 * Emulate the accessed bit for EPT, by checking if this page has
932 * an EPT mapping, and clearing it if it does. On the next access,
933 * a new EPT mapping will be established.
934 * This has some overhead, but not as much as the cost of swapping
935 * out actively used pages or breaking up actively used hugepages.
936 */
Sheng Yang534e38b2008-09-08 15:12:30 +0800937 if (!shadow_accessed_mask)
Rik van Riel6316e1c2010-02-03 16:11:03 -0500938 return kvm_unmap_rmapp(kvm, rmapp, data);
Sheng Yang534e38b2008-09-08 15:12:30 +0800939
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200940 spte = rmap_next(kvm, rmapp, NULL);
941 while (spte) {
942 int _young;
943 u64 _spte = *spte;
944 BUG_ON(!(_spte & PT_PRESENT_MASK));
945 _young = _spte & PT_ACCESSED_MASK;
946 if (_young) {
947 young = 1;
948 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
949 }
950 spte = rmap_next(kvm, rmapp, spte);
951 }
952 return young;
953}
954
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800955static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
956 unsigned long data)
957{
958 u64 *spte;
959 int young = 0;
960
961 /*
962 * If there's no access bit in the secondary pte set by the
963 * hardware it's up to gup-fast/gup to set the access bit in
964 * the primary pte or in the page structure.
965 */
966 if (!shadow_accessed_mask)
967 goto out;
968
969 spte = rmap_next(kvm, rmapp, NULL);
970 while (spte) {
971 u64 _spte = *spte;
972 BUG_ON(!(_spte & PT_PRESENT_MASK));
973 young = _spte & PT_ACCESSED_MASK;
974 if (young) {
975 young = 1;
976 break;
977 }
978 spte = rmap_next(kvm, rmapp, spte);
979 }
980out:
981 return young;
982}
983
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300984#define RMAP_RECYCLE_THRESHOLD 1000
985
Joerg Roedel852e3c12009-07-27 16:30:44 +0200986static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300987{
988 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200989 struct kvm_mmu_page *sp;
990
991 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300992
Joerg Roedel852e3c12009-07-27 16:30:44 +0200993 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300994
Izik Eidus3da0dd42009-09-23 21:47:18 +0300995 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300996 kvm_flush_remote_tlbs(vcpu->kvm);
997}
998
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200999int kvm_age_hva(struct kvm *kvm, unsigned long hva)
1000{
Izik Eidus3da0dd42009-09-23 21:47:18 +03001001 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001002}
1003
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001004int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
1005{
1006 return kvm_handle_hva(kvm, hva, 0, kvm_test_age_rmapp);
1007}
1008
Yaozu Dongd6c69ee2007-04-25 14:17:25 +08001009#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +03001010static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011{
Avi Kivity139bdb22007-01-05 16:36:50 -08001012 u64 *pos;
1013 u64 *end;
1014
Avi Kivity47ad8e62007-05-06 15:50:58 +03001015 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +03001016 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001017 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -08001018 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001019 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -08001020 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021 return 1;
1022}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +08001023#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001024
Dave Hansen45221ab2010-08-19 18:11:37 -07001025/*
1026 * This value is the sum of all of the kvm instances's
1027 * kvm->arch.n_used_mmu_pages values. We need a global,
1028 * aggregate version in order to make the slab shrinker
1029 * faster
1030 */
1031static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, int nr)
1032{
1033 kvm->arch.n_used_mmu_pages += nr;
1034 percpu_counter_add(&kvm_total_used_mmu_pages, nr);
1035}
1036
Avi Kivity4db35312007-11-21 15:28:32 +02001037static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -08001038{
Avi Kivity4db35312007-11-21 15:28:32 +02001039 ASSERT(is_empty_shadow_page(sp->spt));
Xiao Guangrong77758342010-06-04 21:53:54 +08001040 hlist_del(&sp->hash_link);
Avi Kivity4db35312007-11-21 15:28:32 +02001041 list_del(&sp->link);
Xiao Guangrong842f22e2011-03-04 19:01:10 +08001042 free_page((unsigned long)sp->spt);
Lai Jiangshan2032a932010-05-26 16:49:59 +08001043 if (!sp->role.direct)
Xiao Guangrong842f22e2011-03-04 19:01:10 +08001044 free_page((unsigned long)sp->gfns);
Xiao Guangronge8ad9a72010-05-13 10:06:02 +08001045 kmem_cache_free(mmu_page_header_cache, sp);
Dave Hansen45221ab2010-08-19 18:11:37 -07001046 kvm_mod_used_mmu_pages(kvm, -1);
Avi Kivity260746c2007-01-05 16:36:49 -08001047}
1048
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001049static unsigned kvm_page_table_hashfn(gfn_t gfn)
1050{
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001051 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001052}
1053
Avi Kivity25c0de22007-01-05 16:36:42 -08001054static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
Lai Jiangshan2032a932010-05-26 16:49:59 +08001055 u64 *parent_pte, int direct)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001056{
Avi Kivity4db35312007-11-21 15:28:32 +02001057 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001058
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001059 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
1060 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Lai Jiangshan2032a932010-05-26 16:49:59 +08001061 if (!direct)
1062 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache,
1063 PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +02001064 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001065 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Sheng Yang291f26b2008-10-16 17:30:57 +08001066 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +02001067 sp->multimapped = 0;
1068 sp->parent_pte = parent_pte;
Dave Hansen45221ab2010-08-19 18:11:37 -07001069 kvm_mod_used_mmu_pages(vcpu->kvm, +1);
Avi Kivity4db35312007-11-21 15:28:32 +02001070 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001071}
1072
Avi Kivity714b93d2007-01-05 16:36:53 -08001073static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001074 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001075{
1076 struct kvm_pte_chain *pte_chain;
1077 struct hlist_node *node;
1078 int i;
1079
1080 if (!parent_pte)
1081 return;
Avi Kivity4db35312007-11-21 15:28:32 +02001082 if (!sp->multimapped) {
1083 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001084
1085 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +02001086 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001087 return;
1088 }
Avi Kivity4db35312007-11-21 15:28:32 +02001089 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -08001090 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02001091 INIT_HLIST_HEAD(&sp->parent_ptes);
1092 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001093 pte_chain->parent_ptes[0] = old;
1094 }
Avi Kivity4db35312007-11-21 15:28:32 +02001095 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001096 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
1097 continue;
1098 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
1099 if (!pte_chain->parent_ptes[i]) {
1100 pte_chain->parent_ptes[i] = parent_pte;
1101 return;
1102 }
1103 }
Avi Kivity714b93d2007-01-05 16:36:53 -08001104 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001105 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +02001106 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001107 pte_chain->parent_ptes[0] = parent_pte;
1108}
1109
Avi Kivity4db35312007-11-21 15:28:32 +02001110static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001111 u64 *parent_pte)
1112{
1113 struct kvm_pte_chain *pte_chain;
1114 struct hlist_node *node;
1115 int i;
1116
Avi Kivity4db35312007-11-21 15:28:32 +02001117 if (!sp->multimapped) {
1118 BUG_ON(sp->parent_pte != parent_pte);
1119 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001120 return;
1121 }
Avi Kivity4db35312007-11-21 15:28:32 +02001122 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001123 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1124 if (!pte_chain->parent_ptes[i])
1125 break;
1126 if (pte_chain->parent_ptes[i] != parent_pte)
1127 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001128 while (i + 1 < NR_PTE_CHAIN_ENTRIES
1129 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001130 pte_chain->parent_ptes[i]
1131 = pte_chain->parent_ptes[i + 1];
1132 ++i;
1133 }
1134 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001135 if (i == 0) {
1136 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001137 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +02001138 if (hlist_empty(&sp->parent_ptes)) {
1139 sp->multimapped = 0;
1140 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001141 }
1142 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001143 return;
1144 }
1145 BUG();
1146}
1147
Xiao Guangrong6b184932010-04-16 21:29:17 +08001148static void mmu_parent_walk(struct kvm_mmu_page *sp, mmu_parent_walk_fn fn)
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001149{
1150 struct kvm_pte_chain *pte_chain;
1151 struct hlist_node *node;
1152 struct kvm_mmu_page *parent_sp;
1153 int i;
1154
1155 if (!sp->multimapped && sp->parent_pte) {
1156 parent_sp = page_header(__pa(sp->parent_pte));
Xiao Guangrong1047df12010-06-11 21:35:15 +08001157 fn(parent_sp, sp->parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001158 return;
1159 }
1160
1161 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1162 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
Xiao Guangrong1047df12010-06-11 21:35:15 +08001163 u64 *spte = pte_chain->parent_ptes[i];
1164
1165 if (!spte)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001166 break;
Xiao Guangrong1047df12010-06-11 21:35:15 +08001167 parent_sp = page_header(__pa(spte));
1168 fn(parent_sp, spte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001169 }
1170}
1171
Xiao Guangrong1047df12010-06-11 21:35:15 +08001172static void mark_unsync(struct kvm_mmu_page *sp, u64 *spte);
Xiao Guangrong6b184932010-04-16 21:29:17 +08001173static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001174{
Xiao Guangrong1047df12010-06-11 21:35:15 +08001175 mmu_parent_walk(sp, mark_unsync);
1176}
1177
1178static void mark_unsync(struct kvm_mmu_page *sp, u64 *spte)
1179{
1180 unsigned int index;
1181
1182 index = spte - sp->spt;
1183 if (__test_and_set_bit(index, sp->unsync_child_bitmap))
1184 return;
1185 if (sp->unsync_children++)
1186 return;
1187 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001188}
1189
Avi Kivityd761a502008-05-29 14:55:03 +03001190static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1191 struct kvm_mmu_page *sp)
1192{
1193 int i;
1194
1195 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1196 sp->spt[i] = shadow_trap_nonpresent_pte;
1197}
1198
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001199static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001200 struct kvm_mmu_page *sp)
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001201{
1202 return 1;
1203}
1204
Marcelo Tosattia7052892008-09-23 13:18:35 -03001205static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1206{
1207}
1208
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08001209static void nonpaging_update_pte(struct kvm_vcpu *vcpu,
1210 struct kvm_mmu_page *sp, u64 *spte,
Xiao Guangrong7c562522011-03-28 10:29:27 +08001211 const void *pte)
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08001212{
1213 WARN_ON(1);
1214}
1215
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001216#define KVM_PAGE_ARRAY_NR 16
1217
1218struct kvm_mmu_pages {
1219 struct mmu_page_and_offset {
1220 struct kvm_mmu_page *sp;
1221 unsigned int idx;
1222 } page[KVM_PAGE_ARRAY_NR];
1223 unsigned int nr;
1224};
1225
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001226#define for_each_unsync_children(bitmap, idx) \
1227 for (idx = find_first_bit(bitmap, 512); \
1228 idx < 512; \
1229 idx = find_next_bit(bitmap, 512, idx+1))
1230
Hannes Edercded19f2009-02-21 02:19:13 +01001231static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1232 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001233{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001234 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001235
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001236 if (sp->unsync)
1237 for (i=0; i < pvec->nr; i++)
1238 if (pvec->page[i].sp == sp)
1239 return 0;
1240
1241 pvec->page[pvec->nr].sp = sp;
1242 pvec->page[pvec->nr].idx = idx;
1243 pvec->nr++;
1244 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1245}
1246
1247static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1248 struct kvm_mmu_pages *pvec)
1249{
1250 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001251
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001252 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001253 struct kvm_mmu_page *child;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001254 u64 ent = sp->spt[i];
1255
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001256 if (!is_shadow_present_pte(ent) || is_large_pte(ent))
1257 goto clear_child_bitmap;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001258
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001259 child = page_header(ent & PT64_BASE_ADDR_MASK);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001260
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001261 if (child->unsync_children) {
1262 if (mmu_pages_add(pvec, child, i))
1263 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001264
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001265 ret = __mmu_unsync_walk(child, pvec);
1266 if (!ret)
1267 goto clear_child_bitmap;
1268 else if (ret > 0)
1269 nr_unsync_leaf += ret;
1270 else
1271 return ret;
1272 } else if (child->unsync) {
1273 nr_unsync_leaf++;
1274 if (mmu_pages_add(pvec, child, i))
1275 return -ENOSPC;
1276 } else
1277 goto clear_child_bitmap;
1278
1279 continue;
1280
1281clear_child_bitmap:
1282 __clear_bit(i, sp->unsync_child_bitmap);
1283 sp->unsync_children--;
1284 WARN_ON((int)sp->unsync_children < 0);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001285 }
1286
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001287
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001288 return nr_unsync_leaf;
1289}
1290
1291static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1292 struct kvm_mmu_pages *pvec)
1293{
1294 if (!sp->unsync_children)
1295 return 0;
1296
1297 mmu_pages_add(pvec, sp, 0);
1298 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001299}
1300
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001301static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1302{
1303 WARN_ON(!sp->unsync);
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001304 trace_kvm_mmu_sync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001305 sp->unsync = 0;
1306 --kvm->stat.mmu_unsync;
1307}
1308
Xiao Guangrong77758342010-06-04 21:53:54 +08001309static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1310 struct list_head *invalid_list);
1311static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1312 struct list_head *invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001313
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001314#define for_each_gfn_sp(kvm, sp, gfn, pos) \
1315 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001316 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1317 if ((sp)->gfn != (gfn)) {} else
1318
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001319#define for_each_gfn_indirect_valid_sp(kvm, sp, gfn, pos) \
1320 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001321 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1322 if ((sp)->gfn != (gfn) || (sp)->role.direct || \
1323 (sp)->role.invalid) {} else
1324
Xiao Guangrongf918b442010-06-11 21:30:36 +08001325/* @sp->gfn should be write-protected at the call site */
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001326static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001327 struct list_head *invalid_list, bool clear_unsync)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001328{
Avi Kivity5b7e0102010-04-14 19:20:03 +03001329 if (sp->role.cr4_pae != !!is_pae(vcpu)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001330 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001331 return 1;
1332 }
1333
Xiao Guangrongf918b442010-06-11 21:30:36 +08001334 if (clear_unsync)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001335 kvm_unlink_unsync_page(vcpu->kvm, sp);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001336
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001337 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001338 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001339 return 1;
1340 }
1341
1342 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001343 return 0;
1344}
1345
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001346static int kvm_sync_page_transient(struct kvm_vcpu *vcpu,
1347 struct kvm_mmu_page *sp)
1348{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001349 LIST_HEAD(invalid_list);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001350 int ret;
1351
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001352 ret = __kvm_sync_page(vcpu, sp, &invalid_list, false);
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001353 if (ret)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001354 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
1355
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001356 return ret;
1357}
1358
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001359static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1360 struct list_head *invalid_list)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001361{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001362 return __kvm_sync_page(vcpu, sp, invalid_list, true);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001363}
1364
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001365/* @gfn should be write-protected at the call site */
1366static void kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1367{
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001368 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001369 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001370 LIST_HEAD(invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001371 bool flush = false;
1372
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001373 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001374 if (!s->unsync)
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001375 continue;
1376
1377 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001378 kvm_unlink_unsync_page(vcpu->kvm, s);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001379 if ((s->role.cr4_pae != !!is_pae(vcpu)) ||
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001380 (vcpu->arch.mmu.sync_page(vcpu, s))) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001381 kvm_mmu_prepare_zap_page(vcpu->kvm, s, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001382 continue;
1383 }
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001384 flush = true;
1385 }
1386
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001387 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001388 if (flush)
1389 kvm_mmu_flush_tlb(vcpu);
1390}
1391
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001392struct mmu_page_path {
1393 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1394 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001395};
1396
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001397#define for_each_sp(pvec, sp, parents, i) \
1398 for (i = mmu_pages_next(&pvec, &parents, -1), \
1399 sp = pvec.page[i].sp; \
1400 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1401 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001402
Hannes Edercded19f2009-02-21 02:19:13 +01001403static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1404 struct mmu_page_path *parents,
1405 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001406{
1407 int n;
1408
1409 for (n = i+1; n < pvec->nr; n++) {
1410 struct kvm_mmu_page *sp = pvec->page[n].sp;
1411
1412 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1413 parents->idx[0] = pvec->page[n].idx;
1414 return n;
1415 }
1416
1417 parents->parent[sp->role.level-2] = sp;
1418 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1419 }
1420
1421 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001422}
1423
Hannes Edercded19f2009-02-21 02:19:13 +01001424static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001425{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001426 struct kvm_mmu_page *sp;
1427 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001428
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001429 do {
1430 unsigned int idx = parents->idx[level];
1431
1432 sp = parents->parent[level];
1433 if (!sp)
1434 return;
1435
1436 --sp->unsync_children;
1437 WARN_ON((int)sp->unsync_children < 0);
1438 __clear_bit(idx, sp->unsync_child_bitmap);
1439 level++;
1440 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1441}
1442
1443static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1444 struct mmu_page_path *parents,
1445 struct kvm_mmu_pages *pvec)
1446{
1447 parents->parent[parent->role.level-1] = NULL;
1448 pvec->nr = 0;
1449}
1450
1451static void mmu_sync_children(struct kvm_vcpu *vcpu,
1452 struct kvm_mmu_page *parent)
1453{
1454 int i;
1455 struct kvm_mmu_page *sp;
1456 struct mmu_page_path parents;
1457 struct kvm_mmu_pages pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001458 LIST_HEAD(invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001459
1460 kvm_mmu_pages_init(parent, &parents, &pages);
1461 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001462 int protected = 0;
1463
1464 for_each_sp(pages, sp, parents, i)
1465 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1466
1467 if (protected)
1468 kvm_flush_remote_tlbs(vcpu->kvm);
1469
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001470 for_each_sp(pages, sp, parents, i) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001471 kvm_sync_page(vcpu, sp, &invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001472 mmu_pages_clear_parents(&parents);
1473 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001474 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001475 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001476 kvm_mmu_pages_init(parent, &parents, &pages);
1477 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001478}
1479
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001480static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1481 gfn_t gfn,
1482 gva_t gaddr,
1483 unsigned level,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001484 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001485 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001486 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001487{
1488 union kvm_mmu_page_role role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001489 unsigned quadrant;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001490 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001491 struct hlist_node *node;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001492 bool need_sync = false;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001493
Avi Kivitya770f6f2008-12-21 19:20:09 +02001494 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001495 role.level = level;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001496 role.direct = direct;
Avi Kivity84b0c8c2010-03-14 10:16:40 +02001497 if (role.direct)
Avi Kivity5b7e0102010-04-14 19:20:03 +03001498 role.cr4_pae = 0;
Avi Kivity41074d02007-12-09 17:00:02 +02001499 role.access = access;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02001500 if (!vcpu->arch.mmu.direct_map
1501 && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001502 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1503 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1504 role.quadrant = quadrant;
1505 }
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001506 for_each_gfn_sp(vcpu->kvm, sp, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001507 if (!need_sync && sp->unsync)
1508 need_sync = true;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001509
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001510 if (sp->role.word != role.word)
1511 continue;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001512
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001513 if (sp->unsync && kvm_sync_page_transient(vcpu, sp))
1514 break;
Xiao Guangronge02aa902010-05-15 18:52:34 +08001515
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001516 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1517 if (sp->unsync_children) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001518 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001519 kvm_mmu_mark_parents_unsync(sp);
1520 } else if (sp->unsync)
1521 kvm_mmu_mark_parents_unsync(sp);
Xiao Guangronge02aa902010-05-15 18:52:34 +08001522
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001523 trace_kvm_mmu_get_page(sp, false);
1524 return sp;
1525 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001526 ++vcpu->kvm->stat.mmu_cache_miss;
Lai Jiangshan2032a932010-05-26 16:49:59 +08001527 sp = kvm_mmu_alloc_page(vcpu, parent_pte, direct);
Avi Kivity4db35312007-11-21 15:28:32 +02001528 if (!sp)
1529 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001530 sp->gfn = gfn;
1531 sp->role = role;
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001532 hlist_add_head(&sp->hash_link,
1533 &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001534 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001535 if (rmap_write_protect(vcpu->kvm, gfn))
1536 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001537 if (level > PT_PAGE_TABLE_LEVEL && need_sync)
1538 kvm_sync_pages(vcpu, gfn);
1539
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001540 account_shadowed(vcpu->kvm, gfn);
1541 }
Avi Kivity131d8272008-05-29 14:56:28 +03001542 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1543 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1544 else
1545 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001546 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001547 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001548}
1549
Avi Kivity2d111232008-12-25 14:39:47 +02001550static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1551 struct kvm_vcpu *vcpu, u64 addr)
1552{
1553 iterator->addr = addr;
1554 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1555 iterator->level = vcpu->arch.mmu.shadow_root_level;
Joerg Roedel81407ca2010-09-10 17:31:00 +02001556
1557 if (iterator->level == PT64_ROOT_LEVEL &&
1558 vcpu->arch.mmu.root_level < PT64_ROOT_LEVEL &&
1559 !vcpu->arch.mmu.direct_map)
1560 --iterator->level;
1561
Avi Kivity2d111232008-12-25 14:39:47 +02001562 if (iterator->level == PT32E_ROOT_LEVEL) {
1563 iterator->shadow_addr
1564 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1565 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1566 --iterator->level;
1567 if (!iterator->shadow_addr)
1568 iterator->level = 0;
1569 }
1570}
1571
1572static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1573{
1574 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1575 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001576
1577 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1578 if (is_large_pte(*iterator->sptep))
1579 return false;
1580
Avi Kivity2d111232008-12-25 14:39:47 +02001581 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1582 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1583 return true;
1584}
1585
1586static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1587{
1588 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1589 --iterator->level;
1590}
1591
Avi Kivity32ef26a2010-07-13 14:27:04 +03001592static void link_shadow_page(u64 *sptep, struct kvm_mmu_page *sp)
1593{
1594 u64 spte;
1595
1596 spte = __pa(sp->spt)
1597 | PT_PRESENT_MASK | PT_ACCESSED_MASK
1598 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity121eee92010-07-13 14:27:05 +03001599 __set_spte(sptep, spte);
Avi Kivity32ef26a2010-07-13 14:27:04 +03001600}
1601
Avi Kivitya3aa51c2010-07-13 14:27:06 +03001602static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep)
1603{
1604 if (is_large_pte(*sptep)) {
1605 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
1606 kvm_flush_remote_tlbs(vcpu->kvm);
1607 }
1608}
1609
Avi Kivitya357bd22010-07-13 14:27:07 +03001610static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1611 unsigned direct_access)
1612{
1613 if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
1614 struct kvm_mmu_page *child;
1615
1616 /*
1617 * For the direct sp, if the guest pte's dirty bit
1618 * changed form clean to dirty, it will corrupt the
1619 * sp's access: allow writable in the read-only sp,
1620 * so we should update the spte at this point to get
1621 * a new sp with the correct access.
1622 */
1623 child = page_header(*sptep & PT64_BASE_ADDR_MASK);
1624 if (child->role.access == direct_access)
1625 return;
1626
1627 mmu_page_remove_parent_pte(child, sptep);
1628 __set_spte(sptep, shadow_trap_nonpresent_pte);
1629 kvm_flush_remote_tlbs(vcpu->kvm);
1630 }
1631}
1632
Avi Kivity90cb0522007-07-17 13:04:56 +03001633static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001634 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001635{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001636 unsigned i;
1637 u64 *pt;
1638 u64 ent;
1639
Avi Kivity4db35312007-11-21 15:28:32 +02001640 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001641
Avi Kivity697fe2e2007-01-05 16:36:46 -08001642 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1643 ent = pt[i];
1644
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001645 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001646 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001647 ent &= PT64_BASE_ADDR_MASK;
1648 mmu_page_remove_parent_pte(page_header(ent),
1649 &pt[i]);
1650 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001651 if (is_large_pte(ent))
1652 --kvm->stat.lpages;
Avi Kivitybe38d272010-06-06 14:31:27 +03001653 drop_spte(kvm, &pt[i],
1654 shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001655 }
1656 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001657 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001658 }
Avi Kivitya4360362007-01-05 16:36:45 -08001659}
1660
Avi Kivity4db35312007-11-21 15:28:32 +02001661static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001662{
Avi Kivity4db35312007-11-21 15:28:32 +02001663 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001664}
1665
Avi Kivity12b7d282007-09-23 14:10:49 +02001666static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1667{
1668 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001669 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001670
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001671 kvm_for_each_vcpu(i, vcpu, kvm)
1672 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001673}
1674
Avi Kivity31aa2b42008-07-11 17:59:46 +03001675static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001676{
1677 u64 *parent_pte;
1678
Avi Kivity4db35312007-11-21 15:28:32 +02001679 while (sp->multimapped || sp->parent_pte) {
1680 if (!sp->multimapped)
1681 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001682 else {
1683 struct kvm_pte_chain *chain;
1684
Avi Kivity4db35312007-11-21 15:28:32 +02001685 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001686 struct kvm_pte_chain, link);
1687 parent_pte = chain->parent_ptes[0];
1688 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001689 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001690 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001691 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001692 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001693}
1694
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001695static int mmu_zap_unsync_children(struct kvm *kvm,
Xiao Guangrong77758342010-06-04 21:53:54 +08001696 struct kvm_mmu_page *parent,
1697 struct list_head *invalid_list)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001698{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001699 int i, zapped = 0;
1700 struct mmu_page_path parents;
1701 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001702
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001703 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001704 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001705
1706 kvm_mmu_pages_init(parent, &parents, &pages);
1707 while (mmu_unsync_walk(parent, &pages)) {
1708 struct kvm_mmu_page *sp;
1709
1710 for_each_sp(pages, sp, parents, i) {
Xiao Guangrong77758342010-06-04 21:53:54 +08001711 kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001712 mmu_pages_clear_parents(&parents);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001713 zapped++;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001714 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001715 kvm_mmu_pages_init(parent, &parents, &pages);
1716 }
1717
1718 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001719}
1720
Xiao Guangrong77758342010-06-04 21:53:54 +08001721static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1722 struct list_head *invalid_list)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001723{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001724 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001725
Xiao Guangrong77758342010-06-04 21:53:54 +08001726 trace_kvm_mmu_prepare_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001727 ++kvm->stat.mmu_shadow_zapped;
Xiao Guangrong77758342010-06-04 21:53:54 +08001728 ret = mmu_zap_unsync_children(kvm, sp, invalid_list);
Avi Kivity4db35312007-11-21 15:28:32 +02001729 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001730 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001731 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001732 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001733 if (sp->unsync)
1734 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001735 if (!sp->root_count) {
Gui Jianfeng54a4f022010-05-05 09:03:49 +08001736 /* Count self */
1737 ret++;
Xiao Guangrong77758342010-06-04 21:53:54 +08001738 list_move(&sp->link, invalid_list);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001739 } else {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001740 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001741 kvm_reload_remote_mmus(kvm);
1742 }
Xiao Guangrong77758342010-06-04 21:53:54 +08001743
1744 sp->role.invalid = 1;
Avi Kivity12b7d282007-09-23 14:10:49 +02001745 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001746 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001747}
1748
Xiao Guangrong77758342010-06-04 21:53:54 +08001749static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1750 struct list_head *invalid_list)
1751{
1752 struct kvm_mmu_page *sp;
1753
1754 if (list_empty(invalid_list))
1755 return;
1756
1757 kvm_flush_remote_tlbs(kvm);
1758
1759 do {
1760 sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
1761 WARN_ON(!sp->role.invalid || sp->root_count);
1762 kvm_mmu_free_page(kvm, sp);
1763 } while (!list_empty(invalid_list));
1764
1765}
1766
Izik Eidus82ce2c92007-10-02 18:52:55 +02001767/*
1768 * Changing the number of mmu pages allocated to the vm
Dave Hansen49d5ca22010-08-19 18:11:28 -07001769 * Note: if goal_nr_mmu_pages is too small, you will get dead lock
Izik Eidus82ce2c92007-10-02 18:52:55 +02001770 */
Dave Hansen49d5ca22010-08-19 18:11:28 -07001771void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages)
Izik Eidus82ce2c92007-10-02 18:52:55 +02001772{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001773 LIST_HEAD(invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001774 /*
1775 * If we set the number of mmu pages to be smaller be than the
1776 * number of actived pages , we must to free some mmu pages before we
1777 * change the value
1778 */
1779
Dave Hansen49d5ca22010-08-19 18:11:28 -07001780 if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) {
1781 while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages &&
Xiao Guangrong77662e02010-04-16 16:34:42 +08001782 !list_empty(&kvm->arch.active_mmu_pages)) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001783 struct kvm_mmu_page *page;
1784
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001785 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001786 struct kvm_mmu_page, link);
Xiaotian Feng80b63fa2010-08-24 10:31:07 +08001787 kvm_mmu_prepare_zap_page(kvm, page, &invalid_list);
1788 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001789 }
Dave Hansen49d5ca22010-08-19 18:11:28 -07001790 goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001791 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02001792
Dave Hansen49d5ca22010-08-19 18:11:28 -07001793 kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001794}
1795
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001796static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001797{
Avi Kivity4db35312007-11-21 15:28:32 +02001798 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001799 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001800 LIST_HEAD(invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001801 int r;
1802
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08001803 pgprintk("%s: looking for gfn %llx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001804 r = 0;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001805
1806 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08001807 pgprintk("%s: gfn %llx role %x\n", __func__, gfn,
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001808 sp->role.word);
1809 r = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001810 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001811 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001812 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001813 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001814}
1815
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001816static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001817{
Avi Kivity4db35312007-11-21 15:28:32 +02001818 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001819 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001820 LIST_HEAD(invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001821
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001822 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08001823 pgprintk("%s: zap %llx %x\n",
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001824 __func__, gfn, sp->role.word);
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001825 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001826 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001827 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001828}
1829
Avi Kivity38c335f2007-11-21 14:20:22 +02001830static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001831{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001832 int slot = memslot_id(kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001833 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001834
Sheng Yang291f26b2008-10-16 17:30:57 +08001835 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001836}
1837
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001838static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1839{
1840 int i;
1841 u64 *pt = sp->spt;
1842
1843 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1844 return;
1845
1846 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1847 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001848 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001849 }
1850}
1851
Sheng Yang74be52e2008-10-09 16:01:56 +08001852/*
1853 * The function is based on mtrr_type_lookup() in
1854 * arch/x86/kernel/cpu/mtrr/generic.c
1855 */
1856static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1857 u64 start, u64 end)
1858{
1859 int i;
1860 u64 base, mask;
1861 u8 prev_match, curr_match;
1862 int num_var_ranges = KVM_NR_VAR_MTRR;
1863
1864 if (!mtrr_state->enabled)
1865 return 0xFF;
1866
1867 /* Make end inclusive end, instead of exclusive */
1868 end--;
1869
1870 /* Look in fixed ranges. Just return the type as per start */
1871 if (mtrr_state->have_fixed && (start < 0x100000)) {
1872 int idx;
1873
1874 if (start < 0x80000) {
1875 idx = 0;
1876 idx += (start >> 16);
1877 return mtrr_state->fixed_ranges[idx];
1878 } else if (start < 0xC0000) {
1879 idx = 1 * 8;
1880 idx += ((start - 0x80000) >> 14);
1881 return mtrr_state->fixed_ranges[idx];
1882 } else if (start < 0x1000000) {
1883 idx = 3 * 8;
1884 idx += ((start - 0xC0000) >> 12);
1885 return mtrr_state->fixed_ranges[idx];
1886 }
1887 }
1888
1889 /*
1890 * Look in variable ranges
1891 * Look of multiple ranges matching this address and pick type
1892 * as per MTRR precedence
1893 */
1894 if (!(mtrr_state->enabled & 2))
1895 return mtrr_state->def_type;
1896
1897 prev_match = 0xFF;
1898 for (i = 0; i < num_var_ranges; ++i) {
1899 unsigned short start_state, end_state;
1900
1901 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1902 continue;
1903
1904 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1905 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1906 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1907 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1908
1909 start_state = ((start & mask) == (base & mask));
1910 end_state = ((end & mask) == (base & mask));
1911 if (start_state != end_state)
1912 return 0xFE;
1913
1914 if ((start & mask) != (base & mask))
1915 continue;
1916
1917 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1918 if (prev_match == 0xFF) {
1919 prev_match = curr_match;
1920 continue;
1921 }
1922
1923 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1924 curr_match == MTRR_TYPE_UNCACHABLE)
1925 return MTRR_TYPE_UNCACHABLE;
1926
1927 if ((prev_match == MTRR_TYPE_WRBACK &&
1928 curr_match == MTRR_TYPE_WRTHROUGH) ||
1929 (prev_match == MTRR_TYPE_WRTHROUGH &&
1930 curr_match == MTRR_TYPE_WRBACK)) {
1931 prev_match = MTRR_TYPE_WRTHROUGH;
1932 curr_match = MTRR_TYPE_WRTHROUGH;
1933 }
1934
1935 if (prev_match != curr_match)
1936 return MTRR_TYPE_UNCACHABLE;
1937 }
1938
1939 if (prev_match != 0xFF)
1940 return prev_match;
1941
1942 return mtrr_state->def_type;
1943}
1944
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001945u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001946{
1947 u8 mtrr;
1948
1949 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1950 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1951 if (mtrr == 0xfe || mtrr == 0xff)
1952 mtrr = MTRR_TYPE_WRBACK;
1953 return mtrr;
1954}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001955EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001956
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001957static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001958{
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001959 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001960 ++vcpu->kvm->stat.mmu_unsync;
1961 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001962
Xiao Guangrong6b184932010-04-16 21:29:17 +08001963 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001964 mmu_convert_notrap(sp);
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001965}
1966
1967static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1968{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001969 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001970 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001971
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001972 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001973 if (s->unsync)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001974 continue;
1975 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1976 __kvm_unsync_page(vcpu, s);
1977 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001978}
1979
1980static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1981 bool can_unsync)
1982{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001983 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001984 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001985 bool need_unsync = false;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001986
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001987 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001988 if (!can_unsync)
1989 return 1;
1990
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001991 if (s->role.level != PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001992 return 1;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001993
1994 if (!need_unsync && !s->unsync) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001995 if (!oos_shadow)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001996 return 1;
1997 need_unsync = true;
1998 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001999 }
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002000 if (need_unsync)
2001 kvm_unsync_pages(vcpu, gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002002 return 0;
2003}
2004
Avi Kivityd555c332009-06-10 14:24:23 +03002005static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002006 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002007 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03002008 gfn_t gfn, pfn_t pfn, bool speculative,
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08002009 bool can_unsync, bool host_writable)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002010{
Xiao Guangrongb330aa02010-11-19 17:02:35 +08002011 u64 spte, entry = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002012 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08002013
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002014 /*
2015 * We don't set the accessed bit, since we sometimes want to see
2016 * whether the guest actually used the pte (in order to detect
2017 * demand paging).
2018 */
Marcelo Tosatti982c2562010-10-22 14:18:16 -02002019 spte = PT_PRESENT_MASK;
Avi Kivity947da532008-03-18 11:05:52 +02002020 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03002021 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002022 if (!dirty)
2023 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08002024 if (pte_access & ACC_EXEC_MASK)
2025 spte |= shadow_x_mask;
2026 else
2027 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002028 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08002029 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002030 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002031 spte |= PT_PAGE_SIZE_MASK;
Avi Kivityb0bc3ee2010-09-13 16:45:28 +02002032 if (tdp_enabled)
Sheng Yang4b12f0d2009-04-27 20:35:42 +08002033 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
2034 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002035
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08002036 if (host_writable)
Izik Eidus14032832009-09-23 21:47:17 +03002037 spte |= SPTE_HOST_WRITEABLE;
Xiao Guangrongf8e453b2010-12-23 16:09:29 +08002038 else
2039 pte_access &= ~ACC_WRITE_MASK;
Izik Eidus14032832009-09-23 21:47:17 +03002040
Anthony Liguori35149e22008-04-02 14:46:56 -05002041 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002042
2043 if ((pte_access & ACC_WRITE_MASK)
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002044 || (!vcpu->arch.mmu.direct_map && write_fault
2045 && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002046
Joerg Roedel852e3c12009-07-27 16:30:44 +02002047 if (level > PT_PAGE_TABLE_LEVEL &&
2048 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002049 ret = 1;
Avi Kivitybe38d272010-06-06 14:31:27 +03002050 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
2051 goto done;
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002052 }
2053
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002054 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002055
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002056 if (!vcpu->arch.mmu.direct_map
2057 && !(pte_access & ACC_WRITE_MASK))
Avi Kivity69325a12010-05-27 14:35:58 +03002058 spte &= ~PT_USER_MASK;
2059
Marcelo Tosattiecc55892008-11-25 15:58:07 +01002060 /*
2061 * Optimization: for pte sync, if spte was writable the hash
2062 * lookup is unnecessary (and expensive). Write protection
2063 * is responsibility of mmu_get_page / kvm_sync_page.
2064 * Same reasoning can be applied to dirty page accounting.
2065 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002066 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01002067 goto set_pte;
2068
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002069 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002070 pgprintk("%s: found shadow page for %llx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002071 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002072 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002073 pte_access &= ~ACC_WRITE_MASK;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002074 if (is_writable_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002075 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002076 }
2077 }
2078
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002079 if (pte_access & ACC_WRITE_MASK)
2080 mark_page_dirty(vcpu->kvm, gfn);
2081
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002082set_pte:
Avi Kivityb79b93f2010-06-06 15:46:44 +03002083 update_spte(sptep, spte);
Xiao Guangrongb330aa02010-11-19 17:02:35 +08002084 /*
2085 * If we overwrite a writable spte with a read-only one we
2086 * should flush remote TLBs. Otherwise rmap_write_protect
2087 * will find a read-only spte, even though the writable spte
2088 * might be cached on a CPU's TLB.
2089 */
2090 if (is_writable_pte(entry) && !is_writable_pte(*sptep))
2091 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivitybe38d272010-06-06 14:31:27 +03002092done:
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002093 return ret;
2094}
2095
Avi Kivityd555c332009-06-10 14:24:23 +03002096static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002097 unsigned pt_access, unsigned pte_access,
2098 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002099 int *ptwrite, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03002100 pfn_t pfn, bool speculative,
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08002101 bool host_writable)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002102{
2103 int was_rmapped = 0;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002104 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002105
2106 pgprintk("%s: spte %llx access %x write_fault %d"
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002107 " user_fault %d gfn %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002108 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002109 write_fault, user_fault, gfn);
2110
Avi Kivityd555c332009-06-10 14:24:23 +03002111 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002112 /*
2113 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
2114 * the parent of the now unreachable PTE.
2115 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02002116 if (level > PT_PAGE_TABLE_LEVEL &&
2117 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002118 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03002119 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002120
2121 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03002122 mmu_page_remove_parent_pte(child, sptep);
Marcelo Tosatti3be22642010-05-28 09:44:59 -03002123 __set_spte(sptep, shadow_trap_nonpresent_pte);
2124 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityd555c332009-06-10 14:24:23 +03002125 } else if (pfn != spte_to_pfn(*sptep)) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002126 pgprintk("hfn old %llx new %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002127 spte_to_pfn(*sptep), pfn);
Avi Kivitybe38d272010-06-06 14:31:27 +03002128 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
Xiao Guangrong91546352010-06-30 16:04:06 +08002129 kvm_flush_remote_tlbs(vcpu->kvm);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01002130 } else
2131 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002132 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02002133
Avi Kivityd555c332009-06-10 14:24:23 +03002134 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Izik Eidus14032832009-09-23 21:47:17 +03002135 dirty, level, gfn, pfn, speculative, true,
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08002136 host_writable)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002137 if (write_fault)
2138 *ptwrite = 1;
Xiao Guangrong5304efd2010-06-08 20:05:57 +08002139 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03002140 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002141
Avi Kivityd555c332009-06-10 14:24:23 +03002142 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002143 pgprintk("instantiating %s PTE (%s) at %llx (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002144 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02002145 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
2146 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03002147 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002148 ++vcpu->kvm->stat.lpages;
2149
Avi Kivityd555c332009-06-10 14:24:23 +03002150 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002151 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02002152 rmap_count = rmap_add(vcpu, sptep, gfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002153 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02002154 rmap_recycle(vcpu, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002155 }
Xiao Guangrong9ed55202010-07-16 11:25:17 +08002156 kvm_release_pfn_clean(pfn);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002157 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03002158 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002159 vcpu->arch.last_pte_gfn = gfn;
2160 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002161}
2162
Avi Kivity6aa8b732006-12-10 02:21:36 -08002163static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
2164{
2165}
2166
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002167static pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn,
2168 bool no_dirty_log)
2169{
2170 struct kvm_memory_slot *slot;
2171 unsigned long hva;
2172
Xiao Guangrong5d163b12011-03-09 15:43:00 +08002173 slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, no_dirty_log);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002174 if (!slot) {
2175 get_page(bad_page);
2176 return page_to_pfn(bad_page);
2177 }
2178
2179 hva = gfn_to_hva_memslot(slot, gfn);
2180
2181 return hva_to_pfn_atomic(vcpu->kvm, hva);
2182}
2183
2184static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,
2185 struct kvm_mmu_page *sp,
2186 u64 *start, u64 *end)
2187{
2188 struct page *pages[PTE_PREFETCH_NUM];
2189 unsigned access = sp->role.access;
2190 int i, ret;
2191 gfn_t gfn;
2192
2193 gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt);
Xiao Guangrong5d163b12011-03-09 15:43:00 +08002194 if (!gfn_to_memslot_dirty_bitmap(vcpu, gfn, access & ACC_WRITE_MASK))
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002195 return -1;
2196
2197 ret = gfn_to_page_many_atomic(vcpu->kvm, gfn, pages, end - start);
2198 if (ret <= 0)
2199 return -1;
2200
2201 for (i = 0; i < ret; i++, gfn++, start++)
2202 mmu_set_spte(vcpu, start, ACC_ALL,
2203 access, 0, 0, 1, NULL,
2204 sp->role.level, gfn,
2205 page_to_pfn(pages[i]), true, true);
2206
2207 return 0;
2208}
2209
2210static void __direct_pte_prefetch(struct kvm_vcpu *vcpu,
2211 struct kvm_mmu_page *sp, u64 *sptep)
2212{
2213 u64 *spte, *start = NULL;
2214 int i;
2215
2216 WARN_ON(!sp->role.direct);
2217
2218 i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
2219 spte = sp->spt + i;
2220
2221 for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
2222 if (*spte != shadow_trap_nonpresent_pte || spte == sptep) {
2223 if (!start)
2224 continue;
2225 if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0)
2226 break;
2227 start = NULL;
2228 } else if (!start)
2229 start = spte;
2230 }
2231}
2232
2233static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)
2234{
2235 struct kvm_mmu_page *sp;
2236
2237 /*
2238 * Since it's no accessed bit on EPT, it's no way to
2239 * distinguish between actually accessed translations
2240 * and prefetched, so disable pte prefetch if EPT is
2241 * enabled.
2242 */
2243 if (!shadow_accessed_mask)
2244 return;
2245
2246 sp = page_header(__pa(sptep));
2247 if (sp->role.level > PT_PAGE_TABLE_LEVEL)
2248 return;
2249
2250 __direct_pte_prefetch(vcpu, sp, sptep);
2251}
2252
Joerg Roedel4d9976b2008-02-07 13:47:42 +01002253static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002254 int map_writable, int level, gfn_t gfn, pfn_t pfn,
2255 bool prefault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002256{
Avi Kivity9f652d212008-12-25 14:54:25 +02002257 struct kvm_shadow_walk_iterator iterator;
2258 struct kvm_mmu_page *sp;
2259 int pt_write = 0;
2260 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002261
Avi Kivity9f652d212008-12-25 14:54:25 +02002262 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02002263 if (iterator.level == level) {
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002264 unsigned pte_access = ACC_ALL;
2265
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002266 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, pte_access,
Avi Kivity9f652d212008-12-25 14:54:25 +02002267 0, write, 1, &pt_write,
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002268 level, gfn, pfn, prefault, map_writable);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002269 direct_pte_prefetch(vcpu, iterator.sptep);
Avi Kivity9f652d212008-12-25 14:54:25 +02002270 ++vcpu->stat.pf_fixed;
2271 break;
2272 }
2273
2274 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
Lai Jiangshanc9fa0b32010-05-26 16:48:25 +08002275 u64 base_addr = iterator.addr;
2276
2277 base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
2278 pseudo_gfn = base_addr >> PAGE_SHIFT;
Avi Kivity9f652d212008-12-25 14:54:25 +02002279 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
2280 iterator.level - 1,
2281 1, ACC_ALL, iterator.sptep);
2282 if (!sp) {
2283 pgprintk("nonpaging_map: ENOMEM\n");
2284 kvm_release_pfn_clean(pfn);
2285 return -ENOMEM;
2286 }
2287
Avi Kivityd555c332009-06-10 14:24:23 +03002288 __set_spte(iterator.sptep,
2289 __pa(sp->spt)
2290 | PT_PRESENT_MASK | PT_WRITABLE_MASK
Xiao Guangrong33f91ed2010-09-27 18:05:00 +08002291 | shadow_user_mask | shadow_x_mask
2292 | shadow_accessed_mask);
Avi Kivity9f652d212008-12-25 14:54:25 +02002293 }
2294 }
2295 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002296}
2297
Huang Ying77db5cb2010-10-08 16:24:15 +08002298static void kvm_send_hwpoison_signal(unsigned long address, struct task_struct *tsk)
Huang Yingbf998152010-05-31 14:28:19 +08002299{
Huang Ying77db5cb2010-10-08 16:24:15 +08002300 siginfo_t info;
Huang Yingbf998152010-05-31 14:28:19 +08002301
Huang Ying77db5cb2010-10-08 16:24:15 +08002302 info.si_signo = SIGBUS;
2303 info.si_errno = 0;
2304 info.si_code = BUS_MCEERR_AR;
2305 info.si_addr = (void __user *)address;
2306 info.si_addr_lsb = PAGE_SHIFT;
2307
2308 send_sig_info(SIGBUS, &info, tsk);
Huang Yingbf998152010-05-31 14:28:19 +08002309}
2310
2311static int kvm_handle_bad_page(struct kvm *kvm, gfn_t gfn, pfn_t pfn)
2312{
2313 kvm_release_pfn_clean(pfn);
2314 if (is_hwpoison_pfn(pfn)) {
Huang Ying77db5cb2010-10-08 16:24:15 +08002315 kvm_send_hwpoison_signal(gfn_to_hva(kvm, gfn), current);
Huang Yingbf998152010-05-31 14:28:19 +08002316 return 0;
Gleb Natapovedba23e2010-07-07 20:16:45 +03002317 } else if (is_fault_pfn(pfn))
2318 return -EFAULT;
2319
Huang Yingbf998152010-05-31 14:28:19 +08002320 return 1;
2321}
2322
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002323static void transparent_hugepage_adjust(struct kvm_vcpu *vcpu,
2324 gfn_t *gfnp, pfn_t *pfnp, int *levelp)
2325{
2326 pfn_t pfn = *pfnp;
2327 gfn_t gfn = *gfnp;
2328 int level = *levelp;
2329
2330 /*
2331 * Check if it's a transparent hugepage. If this would be an
2332 * hugetlbfs page, level wouldn't be set to
2333 * PT_PAGE_TABLE_LEVEL and there would be no adjustment done
2334 * here.
2335 */
2336 if (!is_error_pfn(pfn) && !kvm_is_mmio_pfn(pfn) &&
2337 level == PT_PAGE_TABLE_LEVEL &&
2338 PageTransCompound(pfn_to_page(pfn)) &&
2339 !has_wrprotected_page(vcpu->kvm, gfn, PT_DIRECTORY_LEVEL)) {
2340 unsigned long mask;
2341 /*
2342 * mmu_notifier_retry was successful and we hold the
2343 * mmu_lock here, so the pmd can't become splitting
2344 * from under us, and in turn
2345 * __split_huge_page_refcount() can't run from under
2346 * us and we can safely transfer the refcount from
2347 * PG_tail to PG_head as we switch the pfn to tail to
2348 * head.
2349 */
2350 *levelp = level = PT_DIRECTORY_LEVEL;
2351 mask = KVM_PAGES_PER_HPAGE(level) - 1;
2352 VM_BUG_ON((gfn & mask) != (pfn & mask));
2353 if (pfn & mask) {
2354 gfn &= ~mask;
2355 *gfnp = gfn;
2356 kvm_release_pfn_clean(pfn);
2357 pfn &= ~mask;
2358 if (!get_page_unless_zero(pfn_to_page(pfn)))
2359 BUG();
2360 *pfnp = pfn;
2361 }
2362 }
2363}
2364
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002365static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
Xiao Guangrong060c2ab2010-11-12 14:49:11 +08002366 gva_t gva, pfn_t *pfn, bool write, bool *writable);
2367
2368static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002369 bool prefault)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002370{
2371 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002372 int level;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002373 int force_pt_level;
Anthony Liguori35149e22008-04-02 14:46:56 -05002374 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002375 unsigned long mmu_seq;
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002376 bool map_writable;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002377
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002378 force_pt_level = mapping_level_dirty_bitmap(vcpu, gfn);
2379 if (likely(!force_pt_level)) {
2380 level = mapping_level(vcpu, gfn);
2381 /*
2382 * This path builds a PAE pagetable - so we can map
2383 * 2mb pages at maximum. Therefore check if the level
2384 * is larger than that.
2385 */
2386 if (level > PT_DIRECTORY_LEVEL)
2387 level = PT_DIRECTORY_LEVEL;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002388
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002389 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2390 } else
2391 level = PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002392
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002393 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002394 smp_rmb();
Xiao Guangrong060c2ab2010-11-12 14:49:11 +08002395
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002396 if (try_async_pf(vcpu, prefault, gfn, v, &pfn, write, &map_writable))
Xiao Guangrong060c2ab2010-11-12 14:49:11 +08002397 return 0;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002398
Avi Kivityd196e342008-01-24 11:44:11 +02002399 /* mmio */
Huang Yingbf998152010-05-31 14:28:19 +08002400 if (is_error_pfn(pfn))
2401 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002402
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002403 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002404 if (mmu_notifier_retry(vcpu, mmu_seq))
2405 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02002406 kvm_mmu_free_some_pages(vcpu);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002407 if (likely(!force_pt_level))
2408 transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002409 r = __direct_map(vcpu, v, write, map_writable, level, gfn, pfn,
2410 prefault);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002411 spin_unlock(&vcpu->kvm->mmu_lock);
2412
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002413
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002414 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002415
2416out_unlock:
2417 spin_unlock(&vcpu->kvm->mmu_lock);
2418 kvm_release_pfn_clean(pfn);
2419 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002420}
2421
2422
Avi Kivity17ac10a2007-01-05 16:36:40 -08002423static void mmu_free_roots(struct kvm_vcpu *vcpu)
2424{
2425 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002426 struct kvm_mmu_page *sp;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002427 LIST_HEAD(invalid_list);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002428
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002429 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002430 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002431 spin_lock(&vcpu->kvm->mmu_lock);
Joerg Roedel81407ca2010-09-10 17:31:00 +02002432 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL &&
2433 (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL ||
2434 vcpu->arch.mmu.direct_map)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002435 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002436
Avi Kivity4db35312007-11-21 15:28:32 +02002437 sp = page_header(root);
2438 --sp->root_count;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002439 if (!sp->root_count && sp->role.invalid) {
2440 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
2441 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
2442 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002443 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002444 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002445 return;
2446 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002447 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002448 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002449
Avi Kivity417726a2007-04-12 17:35:58 +03002450 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002451 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002452 sp = page_header(root);
2453 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002454 if (!sp->root_count && sp->role.invalid)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002455 kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
2456 &invalid_list);
Avi Kivity417726a2007-04-12 17:35:58 +03002457 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002458 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002459 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002460 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002461 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002462 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002463}
2464
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002465static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2466{
2467 int ret = 0;
2468
2469 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03002470 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002471 ret = 1;
2472 }
2473
2474 return ret;
2475}
2476
Joerg Roedel651dd372010-09-10 17:30:59 +02002477static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu)
2478{
2479 struct kvm_mmu_page *sp;
Avi Kivity7ebaf152010-10-03 18:51:39 +02002480 unsigned i;
Joerg Roedel651dd372010-09-10 17:30:59 +02002481
2482 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2483 spin_lock(&vcpu->kvm->mmu_lock);
2484 kvm_mmu_free_some_pages(vcpu);
2485 sp = kvm_mmu_get_page(vcpu, 0, 0, PT64_ROOT_LEVEL,
2486 1, ACC_ALL, NULL);
2487 ++sp->root_count;
2488 spin_unlock(&vcpu->kvm->mmu_lock);
2489 vcpu->arch.mmu.root_hpa = __pa(sp->spt);
2490 } else if (vcpu->arch.mmu.shadow_root_level == PT32E_ROOT_LEVEL) {
2491 for (i = 0; i < 4; ++i) {
2492 hpa_t root = vcpu->arch.mmu.pae_root[i];
2493
2494 ASSERT(!VALID_PAGE(root));
2495 spin_lock(&vcpu->kvm->mmu_lock);
2496 kvm_mmu_free_some_pages(vcpu);
Avi Kivity649497d2010-12-28 12:09:07 +02002497 sp = kvm_mmu_get_page(vcpu, i << (30 - PAGE_SHIFT),
2498 i << 30,
Joerg Roedel651dd372010-09-10 17:30:59 +02002499 PT32_ROOT_LEVEL, 1, ACC_ALL,
2500 NULL);
2501 root = __pa(sp->spt);
2502 ++sp->root_count;
2503 spin_unlock(&vcpu->kvm->mmu_lock);
2504 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Joerg Roedel651dd372010-09-10 17:30:59 +02002505 }
Xiao Guangrong62927572010-09-27 18:02:12 +08002506 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Joerg Roedel651dd372010-09-10 17:30:59 +02002507 } else
2508 BUG();
2509
2510 return 0;
2511}
2512
2513static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002514{
Avi Kivity4db35312007-11-21 15:28:32 +02002515 struct kvm_mmu_page *sp;
Joerg Roedel81407ca2010-09-10 17:31:00 +02002516 u64 pdptr, pm_mask;
2517 gfn_t root_gfn;
2518 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002519
Joerg Roedel5777ed32010-09-10 17:30:42 +02002520 root_gfn = vcpu->arch.mmu.get_cr3(vcpu) >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002521
Joerg Roedel651dd372010-09-10 17:30:59 +02002522 if (mmu_check_root(vcpu, root_gfn))
2523 return 1;
2524
2525 /*
2526 * Do we shadow a long mode page table? If so we need to
2527 * write-protect the guests page table root.
2528 */
2529 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002530 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002531
2532 ASSERT(!VALID_PAGE(root));
Joerg Roedel651dd372010-09-10 17:30:59 +02002533
Avi Kivity8facbbf2010-05-04 12:58:32 +03002534 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b6c92010-05-12 21:00:35 -03002535 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel651dd372010-09-10 17:30:59 +02002536 sp = kvm_mmu_get_page(vcpu, root_gfn, 0, PT64_ROOT_LEVEL,
2537 0, ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002538 root = __pa(sp->spt);
2539 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002540 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002541 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002542 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002543 }
Joerg Roedelf87f9282010-09-02 17:29:45 +02002544
Joerg Roedel651dd372010-09-10 17:30:59 +02002545 /*
2546 * We shadow a 32 bit page table. This may be a legacy 2-level
Joerg Roedel81407ca2010-09-10 17:31:00 +02002547 * or a PAE 3-level page table. In either case we need to be aware that
2548 * the shadow page table may be a PAE or a long mode page table.
Joerg Roedel651dd372010-09-10 17:30:59 +02002549 */
Joerg Roedel81407ca2010-09-10 17:31:00 +02002550 pm_mask = PT_PRESENT_MASK;
2551 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL)
2552 pm_mask |= PT_ACCESSED_MASK | PT_WRITABLE_MASK | PT_USER_MASK;
2553
Avi Kivity17ac10a2007-01-05 16:36:40 -08002554 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002555 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002556
2557 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002558 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Joerg Roedeld41d1892010-09-10 17:30:58 +02002559 pdptr = kvm_pdptr_read_mmu(vcpu, &vcpu->arch.mmu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002560 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002561 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002562 continue;
2563 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002564 root_gfn = pdptr >> PAGE_SHIFT;
Joerg Roedelf87f9282010-09-02 17:29:45 +02002565 if (mmu_check_root(vcpu, root_gfn))
2566 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002567 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002568 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b6c92010-05-12 21:00:35 -03002569 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02002570 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Joerg Roedel651dd372010-09-10 17:30:59 +02002571 PT32_ROOT_LEVEL, 0,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002572 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002573 root = __pa(sp->spt);
2574 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002575 spin_unlock(&vcpu->kvm->mmu_lock);
2576
Joerg Roedel81407ca2010-09-10 17:31:00 +02002577 vcpu->arch.mmu.pae_root[i] = root | pm_mask;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002578 }
Xiao Guangrong62927572010-09-27 18:02:12 +08002579 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Joerg Roedel81407ca2010-09-10 17:31:00 +02002580
2581 /*
2582 * If we shadow a 32 bit page table with a long mode page
2583 * table we enter this path.
2584 */
2585 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2586 if (vcpu->arch.mmu.lm_root == NULL) {
2587 /*
2588 * The additional page necessary for this is only
2589 * allocated on demand.
2590 */
2591
2592 u64 *lm_root;
2593
2594 lm_root = (void*)get_zeroed_page(GFP_KERNEL);
2595 if (lm_root == NULL)
2596 return 1;
2597
2598 lm_root[0] = __pa(vcpu->arch.mmu.pae_root) | pm_mask;
2599
2600 vcpu->arch.mmu.lm_root = lm_root;
2601 }
2602
2603 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.lm_root);
2604 }
2605
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002606 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002607}
2608
Joerg Roedel651dd372010-09-10 17:30:59 +02002609static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
2610{
2611 if (vcpu->arch.mmu.direct_map)
2612 return mmu_alloc_direct_roots(vcpu);
2613 else
2614 return mmu_alloc_shadow_roots(vcpu);
2615}
2616
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002617static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2618{
2619 int i;
2620 struct kvm_mmu_page *sp;
2621
Joerg Roedel81407ca2010-09-10 17:31:00 +02002622 if (vcpu->arch.mmu.direct_map)
2623 return;
2624
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002625 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2626 return;
Xiao Guangrong69030742010-09-27 18:09:29 +08002627
2628 trace_kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC);
Joerg Roedel81407ca2010-09-10 17:31:00 +02002629 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002630 hpa_t root = vcpu->arch.mmu.root_hpa;
2631 sp = page_header(root);
2632 mmu_sync_children(vcpu, sp);
Xiao Guangrong5054c0d2010-11-12 14:46:08 +08002633 trace_kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002634 return;
2635 }
2636 for (i = 0; i < 4; ++i) {
2637 hpa_t root = vcpu->arch.mmu.pae_root[i];
2638
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002639 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002640 root &= PT64_BASE_ADDR_MASK;
2641 sp = page_header(root);
2642 mmu_sync_children(vcpu, sp);
2643 }
2644 }
Xiao Guangrong69030742010-09-27 18:09:29 +08002645 trace_kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002646}
2647
2648void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2649{
2650 spin_lock(&vcpu->kvm->mmu_lock);
2651 mmu_sync_roots(vcpu);
2652 spin_unlock(&vcpu->kvm->mmu_lock);
2653}
2654
Gleb Natapov1871c602010-02-10 14:21:32 +02002655static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
Avi Kivityab9ae312010-11-22 17:53:26 +02002656 u32 access, struct x86_exception *exception)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002657{
Avi Kivityab9ae312010-11-22 17:53:26 +02002658 if (exception)
2659 exception->error_code = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660 return vaddr;
2661}
2662
Joerg Roedel6539e732010-09-10 17:30:50 +02002663static gpa_t nonpaging_gva_to_gpa_nested(struct kvm_vcpu *vcpu, gva_t vaddr,
Avi Kivityab9ae312010-11-22 17:53:26 +02002664 u32 access,
2665 struct x86_exception *exception)
Joerg Roedel6539e732010-09-10 17:30:50 +02002666{
Avi Kivityab9ae312010-11-22 17:53:26 +02002667 if (exception)
2668 exception->error_code = 0;
Joerg Roedel6539e732010-09-10 17:30:50 +02002669 return vcpu->arch.nested_mmu.translate_gpa(vcpu, vaddr, access);
2670}
2671
Avi Kivity6aa8b732006-12-10 02:21:36 -08002672static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002673 u32 error_code, bool prefault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002674{
Avi Kivitye8332402007-12-09 18:43:00 +02002675 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002676 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002677
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002678 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002679 r = mmu_topup_memory_caches(vcpu);
2680 if (r)
2681 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002682
Avi Kivity6aa8b732006-12-10 02:21:36 -08002683 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002684 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002685
Avi Kivitye8332402007-12-09 18:43:00 +02002686 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002687
Avi Kivitye8332402007-12-09 18:43:00 +02002688 return nonpaging_map(vcpu, gva & PAGE_MASK,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002689 error_code & PFERR_WRITE_MASK, gfn, prefault);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002690}
2691
Jan Kiszka7e1fbea2010-10-20 15:18:02 +02002692static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn)
Gleb Natapovaf585b92010-10-14 11:22:46 +02002693{
2694 struct kvm_arch_async_pf arch;
Xiao Guangrongfb67e142010-12-07 10:35:25 +08002695
Gleb Natapov7c907052010-10-14 11:22:53 +02002696 arch.token = (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id;
Gleb Natapovaf585b92010-10-14 11:22:46 +02002697 arch.gfn = gfn;
Xiao Guangrongc4806ac2010-11-12 14:49:55 +08002698 arch.direct_map = vcpu->arch.mmu.direct_map;
Xiao Guangrongfb67e142010-12-07 10:35:25 +08002699 arch.cr3 = vcpu->arch.mmu.get_cr3(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002700
2701 return kvm_setup_async_pf(vcpu, gva, gfn, &arch);
2702}
2703
2704static bool can_do_async_pf(struct kvm_vcpu *vcpu)
2705{
2706 if (unlikely(!irqchip_in_kernel(vcpu->kvm) ||
2707 kvm_event_needs_reinjection(vcpu)))
2708 return false;
2709
2710 return kvm_x86_ops->interrupt_allowed(vcpu);
2711}
2712
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002713static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002714 gva_t gva, pfn_t *pfn, bool write, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02002715{
2716 bool async;
2717
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002718 *pfn = gfn_to_pfn_async(vcpu->kvm, gfn, &async, write, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002719
2720 if (!async)
2721 return false; /* *pfn has correct page already */
2722
2723 put_page(pfn_to_page(*pfn));
2724
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002725 if (!prefault && can_do_async_pf(vcpu)) {
Xiao Guangrongc9b263d2010-11-01 16:58:43 +08002726 trace_kvm_try_async_get_page(gva, gfn);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002727 if (kvm_find_async_pf_gfn(vcpu, gfn)) {
2728 trace_kvm_async_pf_doublefault(gva, gfn);
2729 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
2730 return true;
2731 } else if (kvm_arch_setup_async_pf(vcpu, gva, gfn))
2732 return true;
2733 }
2734
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002735 *pfn = gfn_to_pfn_prot(vcpu->kvm, gfn, write, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002736
2737 return false;
2738}
2739
Gleb Natapov56028d02010-10-17 18:13:42 +02002740static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, u32 error_code,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002741 bool prefault)
Joerg Roedelfb72d162008-02-07 13:47:44 +01002742{
Anthony Liguori35149e22008-04-02 14:46:56 -05002743 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002744 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002745 int level;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002746 int force_pt_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002747 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002748 unsigned long mmu_seq;
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002749 int write = error_code & PFERR_WRITE_MASK;
2750 bool map_writable;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002751
2752 ASSERT(vcpu);
2753 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2754
2755 r = mmu_topup_memory_caches(vcpu);
2756 if (r)
2757 return r;
2758
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002759 force_pt_level = mapping_level_dirty_bitmap(vcpu, gfn);
2760 if (likely(!force_pt_level)) {
2761 level = mapping_level(vcpu, gfn);
2762 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2763 } else
2764 level = PT_PAGE_TABLE_LEVEL;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002765
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002766 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002767 smp_rmb();
Gleb Natapovaf585b92010-10-14 11:22:46 +02002768
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002769 if (try_async_pf(vcpu, prefault, gfn, gpa, &pfn, write, &map_writable))
Gleb Natapovaf585b92010-10-14 11:22:46 +02002770 return 0;
2771
2772 /* mmio */
Huang Yingbf998152010-05-31 14:28:19 +08002773 if (is_error_pfn(pfn))
2774 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002775 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002776 if (mmu_notifier_retry(vcpu, mmu_seq))
2777 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002778 kvm_mmu_free_some_pages(vcpu);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002779 if (likely(!force_pt_level))
2780 transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002781 r = __direct_map(vcpu, gpa, write, map_writable,
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002782 level, gfn, pfn, prefault);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002783 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002784
2785 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002786
2787out_unlock:
2788 spin_unlock(&vcpu->kvm->mmu_lock);
2789 kvm_release_pfn_clean(pfn);
2790 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002791}
2792
Avi Kivity6aa8b732006-12-10 02:21:36 -08002793static void nonpaging_free(struct kvm_vcpu *vcpu)
2794{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002795 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002796}
2797
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002798static int nonpaging_init_context(struct kvm_vcpu *vcpu,
2799 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002800{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002801 context->new_cr3 = nonpaging_new_cr3;
2802 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002803 context->gva_to_gpa = nonpaging_gva_to_gpa;
2804 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002805 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002806 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002807 context->invlpg = nonpaging_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08002808 context->update_pte = nonpaging_update_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002809 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002810 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002811 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002812 context->direct_map = true;
Joerg Roedel2d48a982010-09-10 17:31:01 +02002813 context->nx = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002814 return 0;
2815}
2816
Avi Kivityd835dfe2007-11-21 02:57:59 +02002817void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002818{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002819 ++vcpu->stat.tlb_flush;
Avi Kivitya8eeb042010-05-10 12:34:53 +03002820 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002821}
2822
2823static void paging_new_cr3(struct kvm_vcpu *vcpu)
2824{
Avi Kivity9f8fe502010-12-05 17:30:00 +02002825 pgprintk("%s: cr3 %lx\n", __func__, kvm_read_cr3(vcpu));
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002826 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002827}
2828
Joerg Roedel5777ed32010-09-10 17:30:42 +02002829static unsigned long get_cr3(struct kvm_vcpu *vcpu)
2830{
Avi Kivity9f8fe502010-12-05 17:30:00 +02002831 return kvm_read_cr3(vcpu);
Joerg Roedel5777ed32010-09-10 17:30:42 +02002832}
2833
Avi Kivity6389ee92010-11-29 16:12:30 +02002834static void inject_page_fault(struct kvm_vcpu *vcpu,
2835 struct x86_exception *fault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002836{
Avi Kivity6389ee92010-11-29 16:12:30 +02002837 vcpu->arch.mmu.inject_page_fault(vcpu, fault);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002838}
2839
Avi Kivity6aa8b732006-12-10 02:21:36 -08002840static void paging_free(struct kvm_vcpu *vcpu)
2841{
2842 nonpaging_free(vcpu);
2843}
2844
Joerg Roedel3241f222010-09-10 17:30:45 +02002845static bool is_rsvd_bits_set(struct kvm_mmu *mmu, u64 gpte, int level)
Dong, Eddie82725b22009-03-30 16:21:08 +08002846{
2847 int bit7;
2848
2849 bit7 = (gpte >> 7) & 1;
Joerg Roedel3241f222010-09-10 17:30:45 +02002850 return (gpte & mmu->rsvd_bits_mask[bit7][level-1]) != 0;
Dong, Eddie82725b22009-03-30 16:21:08 +08002851}
2852
Avi Kivity6aa8b732006-12-10 02:21:36 -08002853#define PTTYPE 64
2854#include "paging_tmpl.h"
2855#undef PTTYPE
2856
2857#define PTTYPE 32
2858#include "paging_tmpl.h"
2859#undef PTTYPE
2860
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002861static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
2862 struct kvm_mmu *context,
2863 int level)
Dong, Eddie82725b22009-03-30 16:21:08 +08002864{
Dong, Eddie82725b22009-03-30 16:21:08 +08002865 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2866 u64 exb_bit_rsvd = 0;
2867
Joerg Roedel2d48a982010-09-10 17:31:01 +02002868 if (!context->nx)
Dong, Eddie82725b22009-03-30 16:21:08 +08002869 exb_bit_rsvd = rsvd_bits(63, 63);
2870 switch (level) {
2871 case PT32_ROOT_LEVEL:
2872 /* no rsvd bits for 2 level 4K page table entries */
2873 context->rsvd_bits_mask[0][1] = 0;
2874 context->rsvd_bits_mask[0][0] = 0;
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002875 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2876
2877 if (!is_pse(vcpu)) {
2878 context->rsvd_bits_mask[1][1] = 0;
2879 break;
2880 }
2881
Dong, Eddie82725b22009-03-30 16:21:08 +08002882 if (is_cpuid_PSE36())
2883 /* 36bits PSE 4MB page */
2884 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2885 else
2886 /* 32 bits PSE 4MB page */
2887 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08002888 break;
2889 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002890 context->rsvd_bits_mask[0][2] =
2891 rsvd_bits(maxphyaddr, 63) |
2892 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002893 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002894 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002895 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2896 rsvd_bits(maxphyaddr, 62); /* PTE */
2897 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2898 rsvd_bits(maxphyaddr, 62) |
2899 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002900 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002901 break;
2902 case PT64_ROOT_LEVEL:
2903 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2904 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2905 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2906 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2907 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002908 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002909 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2910 rsvd_bits(maxphyaddr, 51);
2911 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002912 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2913 rsvd_bits(maxphyaddr, 51) |
2914 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002915 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002916 rsvd_bits(maxphyaddr, 51) |
2917 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002918 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002919 break;
2920 }
2921}
2922
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002923static int paging64_init_context_common(struct kvm_vcpu *vcpu,
2924 struct kvm_mmu *context,
2925 int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002926{
Joerg Roedel2d48a982010-09-10 17:31:01 +02002927 context->nx = is_nx(vcpu);
2928
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002929 reset_rsvds_bits_mask(vcpu, context, level);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002930
2931 ASSERT(is_pae(vcpu));
2932 context->new_cr3 = paging_new_cr3;
2933 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002934 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002935 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002936 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002937 context->invlpg = paging64_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08002938 context->update_pte = paging64_update_pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002939 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002940 context->root_level = level;
2941 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002942 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002943 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002944 return 0;
2945}
2946
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002947static int paging64_init_context(struct kvm_vcpu *vcpu,
2948 struct kvm_mmu *context)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002949{
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002950 return paging64_init_context_common(vcpu, context, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002951}
2952
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002953static int paging32_init_context(struct kvm_vcpu *vcpu,
2954 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002955{
Joerg Roedel2d48a982010-09-10 17:31:01 +02002956 context->nx = false;
2957
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002958 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002959
2960 context->new_cr3 = paging_new_cr3;
2961 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002962 context->gva_to_gpa = paging32_gva_to_gpa;
2963 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002964 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002965 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002966 context->invlpg = paging32_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08002967 context->update_pte = paging32_update_pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002968 context->root_level = PT32_ROOT_LEVEL;
2969 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002970 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002971 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002972 return 0;
2973}
2974
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002975static int paging32E_init_context(struct kvm_vcpu *vcpu,
2976 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002977{
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002978 return paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002979}
2980
Joerg Roedelfb72d162008-02-07 13:47:44 +01002981static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2982{
Joerg Roedel14dfe852010-09-10 17:30:49 +02002983 struct kvm_mmu *context = vcpu->arch.walk_mmu;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002984
Avi Kivityc445f8e2010-12-21 16:26:01 +02002985 context->base_role.word = 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002986 context->new_cr3 = nonpaging_new_cr3;
2987 context->page_fault = tdp_page_fault;
2988 context->free = nonpaging_free;
2989 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002990 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002991 context->invlpg = nonpaging_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08002992 context->update_pte = nonpaging_update_pte;
Sheng Yang67253af2008-04-25 10:20:22 +08002993 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002994 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002995 context->direct_map = true;
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02002996 context->set_cr3 = kvm_x86_ops->set_tdp_cr3;
Joerg Roedel5777ed32010-09-10 17:30:42 +02002997 context->get_cr3 = get_cr3;
Joerg Roedelcb659db2010-09-10 17:30:43 +02002998 context->inject_page_fault = kvm_inject_page_fault;
Joerg Roedel2d48a982010-09-10 17:31:01 +02002999 context->nx = is_nx(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01003000
3001 if (!is_paging(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003002 context->nx = false;
Joerg Roedelfb72d162008-02-07 13:47:44 +01003003 context->gva_to_gpa = nonpaging_gva_to_gpa;
3004 context->root_level = 0;
3005 } else if (is_long_mode(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003006 context->nx = is_nx(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003007 reset_rsvds_bits_mask(vcpu, context, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01003008 context->gva_to_gpa = paging64_gva_to_gpa;
3009 context->root_level = PT64_ROOT_LEVEL;
3010 } else if (is_pae(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003011 context->nx = is_nx(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003012 reset_rsvds_bits_mask(vcpu, context, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01003013 context->gva_to_gpa = paging64_gva_to_gpa;
3014 context->root_level = PT32E_ROOT_LEVEL;
3015 } else {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003016 context->nx = false;
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003017 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01003018 context->gva_to_gpa = paging32_gva_to_gpa;
3019 context->root_level = PT32_ROOT_LEVEL;
3020 }
3021
3022 return 0;
3023}
3024
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003025int kvm_init_shadow_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003026{
Avi Kivitya770f6f2008-12-21 19:20:09 +02003027 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003028 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003029 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003030
3031 if (!is_paging(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003032 r = nonpaging_init_context(vcpu, context);
Avi Kivitya9058ec2006-12-29 16:49:37 -08003033 else if (is_long_mode(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003034 r = paging64_init_context(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003035 else if (is_pae(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003036 r = paging32E_init_context(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003037 else
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003038 r = paging32_init_context(vcpu, context);
Avi Kivitya770f6f2008-12-21 19:20:09 +02003039
Avi Kivity5b7e0102010-04-14 19:20:03 +03003040 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
Joerg Roedelf43addd2010-09-10 17:30:40 +02003041 vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003042
3043 return r;
3044}
3045EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu);
3046
3047static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
3048{
Joerg Roedel14dfe852010-09-10 17:30:49 +02003049 int r = kvm_init_shadow_mmu(vcpu, vcpu->arch.walk_mmu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003050
Joerg Roedel14dfe852010-09-10 17:30:49 +02003051 vcpu->arch.walk_mmu->set_cr3 = kvm_x86_ops->set_cr3;
3052 vcpu->arch.walk_mmu->get_cr3 = get_cr3;
3053 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
Avi Kivitya770f6f2008-12-21 19:20:09 +02003054
3055 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003056}
3057
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003058static int init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
3059{
3060 struct kvm_mmu *g_context = &vcpu->arch.nested_mmu;
3061
3062 g_context->get_cr3 = get_cr3;
3063 g_context->inject_page_fault = kvm_inject_page_fault;
3064
3065 /*
3066 * Note that arch.mmu.gva_to_gpa translates l2_gva to l1_gpa. The
3067 * translation of l2_gpa to l1_gpa addresses is done using the
3068 * arch.nested_mmu.gva_to_gpa function. Basically the gva_to_gpa
3069 * functions between mmu and nested_mmu are swapped.
3070 */
3071 if (!is_paging(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003072 g_context->nx = false;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003073 g_context->root_level = 0;
3074 g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested;
3075 } else if (is_long_mode(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003076 g_context->nx = is_nx(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003077 reset_rsvds_bits_mask(vcpu, g_context, PT64_ROOT_LEVEL);
3078 g_context->root_level = PT64_ROOT_LEVEL;
3079 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
3080 } else if (is_pae(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003081 g_context->nx = is_nx(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003082 reset_rsvds_bits_mask(vcpu, g_context, PT32E_ROOT_LEVEL);
3083 g_context->root_level = PT32E_ROOT_LEVEL;
3084 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
3085 } else {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003086 g_context->nx = false;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003087 reset_rsvds_bits_mask(vcpu, g_context, PT32_ROOT_LEVEL);
3088 g_context->root_level = PT32_ROOT_LEVEL;
3089 g_context->gva_to_gpa = paging32_gva_to_gpa_nested;
3090 }
3091
3092 return 0;
3093}
3094
Joerg Roedelfb72d162008-02-07 13:47:44 +01003095static int init_kvm_mmu(struct kvm_vcpu *vcpu)
3096{
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003097 if (mmu_is_nested(vcpu))
3098 return init_kvm_nested_mmu(vcpu);
3099 else if (tdp_enabled)
Joerg Roedelfb72d162008-02-07 13:47:44 +01003100 return init_kvm_tdp_mmu(vcpu);
3101 else
3102 return init_kvm_softmmu(vcpu);
3103}
3104
Avi Kivity6aa8b732006-12-10 02:21:36 -08003105static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
3106{
3107 ASSERT(vcpu);
Sheng Yang62ad0752010-05-12 16:40:41 +08003108 if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
3109 /* mmu.free() should set root_hpa = INVALID_PAGE */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003110 vcpu->arch.mmu.free(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003111}
3112
3113int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
3114{
Avi Kivity17c3ba92007-06-04 15:58:30 +03003115 destroy_kvm_mmu(vcpu);
3116 return init_kvm_mmu(vcpu);
3117}
Eddie Dong8668a3c2007-10-10 14:26:45 +08003118EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03003119
3120int kvm_mmu_load(struct kvm_vcpu *vcpu)
3121{
Avi Kivity714b93d2007-01-05 16:36:53 -08003122 int r;
3123
Avi Kivitye2dec932007-01-05 16:36:54 -08003124 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03003125 if (r)
3126 goto out;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003127 r = mmu_alloc_roots(vcpu);
Avi Kivity8facbbf2010-05-04 12:58:32 +03003128 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003129 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003130 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003131 if (r)
3132 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08003133 /* set_cr3() should ensure TLB has been flushed */
Joerg Roedelf43addd2010-09-10 17:30:40 +02003134 vcpu->arch.mmu.set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08003135out:
3136 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003137}
Avi Kivity17c3ba92007-06-04 15:58:30 +03003138EXPORT_SYMBOL_GPL(kvm_mmu_load);
3139
3140void kvm_mmu_unload(struct kvm_vcpu *vcpu)
3141{
3142 mmu_free_roots(vcpu);
3143}
Joerg Roedel4b161842010-09-10 17:31:03 +02003144EXPORT_SYMBOL_GPL(kvm_mmu_unload);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003145
Avi Kivity09072da2007-05-01 14:16:52 +03003146static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02003147 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02003148 u64 *spte)
3149{
3150 u64 pte;
3151 struct kvm_mmu_page *child;
3152
3153 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02003154 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03003155 if (is_last_spte(pte, sp->role.level))
Avi Kivitybe38d272010-06-06 14:31:27 +03003156 drop_spte(vcpu->kvm, spte, shadow_trap_nonpresent_pte);
Avi Kivityac1b7142007-03-08 17:13:32 +02003157 else {
3158 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03003159 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02003160 }
3161 }
Avi Kivityd555c332009-06-10 14:24:23 +03003162 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03003163 if (is_large_pte(pte))
3164 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02003165}
3166
Avi Kivity00284252007-05-01 16:53:31 +03003167static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Xiao Guangrong7c562522011-03-28 10:29:27 +08003168 struct kvm_mmu_page *sp, u64 *spte,
3169 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03003170{
Marcelo Tosatti30945382008-06-11 20:32:40 -03003171 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02003172 ++vcpu->kvm->stat.mmu_pde_zapped;
3173 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03003174 }
Avi Kivity00284252007-05-01 16:53:31 +03003175
Avi Kivity4cee5762007-11-18 16:37:07 +02003176 ++vcpu->kvm->stat.mmu_pte_updated;
Xiao Guangrong7c562522011-03-28 10:29:27 +08003177 vcpu->arch.mmu.update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03003178}
3179
Avi Kivity79539ce2007-11-21 02:06:21 +02003180static bool need_remote_flush(u64 old, u64 new)
3181{
3182 if (!is_shadow_present_pte(old))
3183 return false;
3184 if (!is_shadow_present_pte(new))
3185 return true;
3186 if ((old ^ new) & PT64_BASE_ADDR_MASK)
3187 return true;
3188 old ^= PT64_NX_MASK;
3189 new ^= PT64_NX_MASK;
3190 return (old & ~new & PT64_PERM_MASK) != 0;
3191}
3192
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003193static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page,
3194 bool remote_flush, bool local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003195{
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003196 if (zap_page)
3197 return;
3198
3199 if (remote_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003200 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003201 else if (local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003202 kvm_mmu_flush_tlb(vcpu);
3203}
3204
Avi Kivity12b7d282007-09-23 14:10:49 +02003205static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
3206{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003207 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02003208
Sheng Yang7b523452008-04-25 21:13:50 +08003209 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02003210}
3211
Avi Kivity1b7fcd32008-05-15 13:51:35 +03003212static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
3213{
3214 u64 *spte = vcpu->arch.last_pte_updated;
3215
3216 if (spte
3217 && vcpu->arch.last_pte_gfn == gfn
3218 && shadow_accessed_mask
3219 && !(*spte & shadow_accessed_mask)
3220 && is_shadow_present_pte(*spte))
3221 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
3222}
3223
Avi Kivity09072da2007-05-01 14:16:52 +03003224void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003225 const u8 *new, int bytes,
3226 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08003227{
Avi Kivity9b7a0322007-01-05 16:36:45 -08003228 gfn_t gfn = gpa >> PAGE_SHIFT;
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003229 union kvm_mmu_page_role mask = { .word = 0 };
Avi Kivity4db35312007-11-21 15:28:32 +02003230 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003231 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003232 LIST_HEAD(invalid_list);
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08003233 u64 entry, gentry, *spte;
3234 unsigned pte_size, page_offset, misaligned, quadrant, offset;
3235 int level, npte, invlpg_counter, r, flooded = 0;
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003236 bool remote_flush, local_flush, zap_page;
3237
Xiao Guangrong332b2072011-05-15 23:20:27 +08003238 /*
3239 * If we don't have indirect shadow pages, it means no page is
3240 * write-protected, so we can exit simply.
3241 */
3242 if (!ACCESS_ONCE(vcpu->kvm->arch.indirect_shadow_pages))
3243 return;
3244
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003245 zap_page = remote_flush = local_flush = false;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08003246 offset = offset_in_page(gpa);
Avi Kivity9b7a0322007-01-05 16:36:45 -08003247
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003248 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivity72016f32010-03-15 13:59:53 +02003249
Avi Kivity08e850c2010-03-15 13:59:57 +02003250 invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
3251
3252 /*
3253 * Assume that the pte write on a page table of the same type
Xiao Guangrong49b26e22011-03-04 19:00:00 +08003254 * as the current vcpu paging mode since we update the sptes only
3255 * when they have the same mode.
Avi Kivity08e850c2010-03-15 13:59:57 +02003256 */
3257 if ((is_pae(vcpu) && bytes == 4) || !new) {
3258 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
3259 if (is_pae(vcpu)) {
3260 gpa &= ~(gpa_t)7;
3261 bytes = 8;
3262 }
3263 r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
3264 if (r)
3265 gentry = 0;
3266 new = (const u8 *)&gentry;
3267 }
3268
Avi Kivity72016f32010-03-15 13:59:53 +02003269 switch (bytes) {
3270 case 4:
3271 gentry = *(const u32 *)new;
3272 break;
3273 case 8:
3274 gentry = *(const u64 *)new;
3275 break;
3276 default:
3277 gentry = 0;
3278 break;
3279 }
3280
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003281 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity08e850c2010-03-15 13:59:57 +02003282 if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
3283 gentry = 0;
Avi Kivityeb787d12007-12-31 15:27:49 +02003284 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02003285 ++vcpu->kvm->stat.mmu_pte_write;
Xiao Guangrong8b1fe172010-08-30 18:22:53 +08003286 trace_kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE);
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003287 if (guest_initiated) {
Xiao Guangrong1b7fd452011-03-04 18:58:02 +08003288 kvm_mmu_access_page(vcpu, gfn);
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003289 if (gfn == vcpu->arch.last_pt_write_gfn
3290 && !last_updated_pte_accessed(vcpu)) {
3291 ++vcpu->arch.last_pt_write_count;
3292 if (vcpu->arch.last_pt_write_count >= 3)
3293 flooded = 1;
3294 } else {
3295 vcpu->arch.last_pt_write_gfn = gfn;
3296 vcpu->arch.last_pt_write_count = 1;
3297 vcpu->arch.last_pte_updated = NULL;
3298 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08003299 }
Xiao Guangrong3246af02010-04-16 16:35:54 +08003300
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003301 mask.cr0_wp = mask.cr4_pae = mask.nxe = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003302 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node) {
Avi Kivity5b7e0102010-04-14 19:20:03 +03003303 pte_size = sp->role.cr4_pae ? 8 : 4;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003304 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03003305 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08003306 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003307 /*
3308 * Misaligned accesses are too much trouble to fix
3309 * up; also, they usually indicate a page is not used
3310 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08003311 *
3312 * If we're seeing too many writes to a page,
3313 * it may no longer be a page table, or we may be
3314 * forking, in which case it is better to unmap the
3315 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003316 */
3317 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02003318 gpa, bytes, sp->role.word);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003319 zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003320 &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02003321 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003322 continue;
3323 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08003324 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02003325 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02003326 npte = 1;
Avi Kivity5b7e0102010-04-14 19:20:03 +03003327 if (!sp->role.cr4_pae) {
Avi Kivityac1b7142007-03-08 17:13:32 +02003328 page_offset <<= 1; /* 32->64 */
3329 /*
3330 * A 32-bit pde maps 4MB while the shadow pdes map
3331 * only 2MB. So we need to double the offset again
3332 * and zap two pdes instead of one.
3333 */
3334 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03003335 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02003336 page_offset <<= 1;
3337 npte = 2;
3338 }
Avi Kivityfce06572007-05-01 16:44:05 +03003339 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003340 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02003341 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03003342 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003343 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003344 local_flush = true;
Avi Kivity4db35312007-11-21 15:28:32 +02003345 spte = &sp->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02003346 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02003347 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02003348 mmu_pte_write_zap_pte(vcpu, sp, spte);
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003349 if (gentry &&
3350 !((sp->role.word ^ vcpu->arch.mmu.base_role.word)
3351 & mask.word))
Xiao Guangrong7c562522011-03-28 10:29:27 +08003352 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003353 if (!remote_flush && need_remote_flush(entry, *spte))
3354 remote_flush = true;
Avi Kivityac1b7142007-03-08 17:13:32 +02003355 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003356 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08003357 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003358 mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003359 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong8b1fe172010-08-30 18:22:53 +08003360 trace_kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003361 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivityda4a00f2007-01-05 16:36:44 -08003362}
3363
Avi Kivitya4360362007-01-05 16:36:45 -08003364int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
3365{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003366 gpa_t gpa;
3367 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08003368
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02003369 if (vcpu->arch.mmu.direct_map)
Avi Kivity60f24782009-08-27 13:37:06 +03003370 return 0;
3371
Gleb Natapov1871c602010-02-10 14:21:32 +02003372 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003373
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003374 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003375 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003376 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003377 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08003378}
Avi Kivity577bdc42008-07-19 08:57:05 +03003379EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08003380
Avi Kivity22d95b12007-09-14 20:26:06 +03003381void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08003382{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003383 LIST_HEAD(invalid_list);
Xiao Guangrong103ad252010-06-04 21:54:38 +08003384
Dave Hansene0df7b92010-08-19 18:11:05 -07003385 while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES &&
Izik Eidus3b80fff2009-07-28 15:26:58 -03003386 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02003387 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08003388
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003389 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02003390 struct kvm_mmu_page, link);
Dave Hansene0df7b92010-08-19 18:11:05 -07003391 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
Xiaotian Feng80b63fa2010-08-24 10:31:07 +08003392 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02003393 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08003394 }
3395}
Avi Kivityebeace82007-01-05 16:36:47 -08003396
Andre Przywaradc25e892010-12-21 11:12:07 +01003397int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code,
3398 void *insn, int insn_len)
Avi Kivity30677142007-10-28 18:48:59 +02003399{
3400 int r;
3401 enum emulation_result er;
3402
Gleb Natapov56028d02010-10-17 18:13:42 +02003403 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code, false);
Avi Kivity30677142007-10-28 18:48:59 +02003404 if (r < 0)
3405 goto out;
3406
3407 if (!r) {
3408 r = 1;
3409 goto out;
3410 }
3411
Avi Kivityb733bfb2007-10-28 18:52:05 +02003412 r = mmu_topup_memory_caches(vcpu);
3413 if (r)
3414 goto out;
3415
Andre Przywaradc25e892010-12-21 11:12:07 +01003416 er = x86_emulate_instruction(vcpu, cr2, 0, insn, insn_len);
Avi Kivity30677142007-10-28 18:48:59 +02003417
3418 switch (er) {
3419 case EMULATE_DONE:
3420 return 1;
3421 case EMULATE_DO_MMIO:
3422 ++vcpu->stat.mmio_exits;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03003423 /* fall through */
Avi Kivity30677142007-10-28 18:48:59 +02003424 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03003425 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02003426 default:
3427 BUG();
3428 }
3429out:
Avi Kivity30677142007-10-28 18:48:59 +02003430 return r;
3431}
3432EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
3433
Marcelo Tosattia7052892008-09-23 13:18:35 -03003434void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
3435{
Marcelo Tosattia7052892008-09-23 13:18:35 -03003436 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003437 kvm_mmu_flush_tlb(vcpu);
3438 ++vcpu->stat.invlpg;
3439}
3440EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
3441
Joerg Roedel18552672008-02-07 13:47:41 +01003442void kvm_enable_tdp(void)
3443{
3444 tdp_enabled = true;
3445}
3446EXPORT_SYMBOL_GPL(kvm_enable_tdp);
3447
Joerg Roedel5f4cb662008-07-14 20:36:36 +02003448void kvm_disable_tdp(void)
3449{
3450 tdp_enabled = false;
3451}
3452EXPORT_SYMBOL_GPL(kvm_disable_tdp);
3453
Avi Kivity6aa8b732006-12-10 02:21:36 -08003454static void free_mmu_pages(struct kvm_vcpu *vcpu)
3455{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003456 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Joerg Roedel81407ca2010-09-10 17:31:00 +02003457 if (vcpu->arch.mmu.lm_root != NULL)
3458 free_page((unsigned long)vcpu->arch.mmu.lm_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003459}
3460
3461static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
3462{
Avi Kivity17ac10a2007-01-05 16:36:40 -08003463 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003464 int i;
3465
3466 ASSERT(vcpu);
3467
Avi Kivity17ac10a2007-01-05 16:36:40 -08003468 /*
3469 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
3470 * Therefore we need to allocate shadow page tables in the first
3471 * 4GB of memory, which happens to fit the DMA32 zone.
3472 */
3473 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
3474 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08003475 return -ENOMEM;
3476
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003477 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003478 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003479 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003480
Avi Kivity6aa8b732006-12-10 02:21:36 -08003481 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003482}
3483
Ingo Molnar8018c272006-12-29 16:50:01 -08003484int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003485{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003486 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003487 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003488
Ingo Molnar8018c272006-12-29 16:50:01 -08003489 return alloc_mmu_pages(vcpu);
3490}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003491
Ingo Molnar8018c272006-12-29 16:50:01 -08003492int kvm_mmu_setup(struct kvm_vcpu *vcpu)
3493{
3494 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003495 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08003496
Ingo Molnar8018c272006-12-29 16:50:01 -08003497 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003498}
3499
Avi Kivity90cb0522007-07-17 13:04:56 +03003500void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003501{
Avi Kivity4db35312007-11-21 15:28:32 +02003502 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003503
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003504 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003505 int i;
3506 u64 *pt;
3507
Sheng Yang291f26b2008-10-16 17:30:57 +08003508 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003509 continue;
3510
Avi Kivity4db35312007-11-21 15:28:32 +02003511 pt = sp->spt;
Avi Kivity8234b222010-12-27 12:08:45 +02003512 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Xiao Guangrongda8dc752011-03-04 18:56:41 +08003513 if (!is_shadow_present_pte(pt[i]) ||
3514 !is_last_spte(pt[i], sp->role.level))
3515 continue;
3516
3517 if (is_large_pte(pt[i])) {
Avi Kivity8234b222010-12-27 12:08:45 +02003518 drop_spte(kvm, &pt[i],
3519 shadow_trap_nonpresent_pte);
3520 --kvm->stat.lpages;
Xiao Guangrongda8dc752011-03-04 18:56:41 +08003521 continue;
Avi Kivity8234b222010-12-27 12:08:45 +02003522 }
Xiao Guangrongda8dc752011-03-04 18:56:41 +08003523
Avi Kivity6aa8b732006-12-10 02:21:36 -08003524 /* avoid RMW */
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003525 if (is_writable_pte(pt[i]))
Takuya Yoshikawa700e1b12010-12-06 01:11:33 +09003526 update_spte(&pt[i], pt[i] & ~PT_WRITABLE_MASK);
Avi Kivity8234b222010-12-27 12:08:45 +02003527 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003528 }
Avi Kivity171d5952008-08-27 16:40:51 +03003529 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003530}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003531
Avi Kivity90cb0522007-07-17 13:04:56 +03003532void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03003533{
Avi Kivity4db35312007-11-21 15:28:32 +02003534 struct kvm_mmu_page *sp, *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003535 LIST_HEAD(invalid_list);
Dor Laore0fa8262007-03-30 13:06:33 +03003536
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003537 spin_lock(&kvm->mmu_lock);
Xiao Guangrong3246af02010-04-16 16:35:54 +08003538restart:
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003539 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003540 if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list))
Xiao Guangrong3246af02010-04-16 16:35:54 +08003541 goto restart;
3542
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003543 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003544 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03003545}
3546
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003547static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm,
3548 struct list_head *invalid_list)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003549{
3550 struct kvm_mmu_page *page;
3551
3552 page = container_of(kvm->arch.active_mmu_pages.prev,
3553 struct kvm_mmu_page, link);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003554 return kvm_mmu_prepare_zap_page(kvm, page, invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003555}
3556
Ying Han1495f232011-05-24 17:12:27 -07003557static int mmu_shrink(struct shrinker *shrink, struct shrink_control *sc)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003558{
3559 struct kvm *kvm;
3560 struct kvm *kvm_freed = NULL;
Ying Han1495f232011-05-24 17:12:27 -07003561 int nr_to_scan = sc->nr_to_scan;
Dave Hansen45221ab2010-08-19 18:11:37 -07003562
3563 if (nr_to_scan == 0)
3564 goto out;
Izik Eidus3ee16c82008-03-30 15:17:21 +03003565
Jan Kiszkae935b832011-02-08 12:55:33 +01003566 raw_spin_lock(&kvm_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003567
3568 list_for_each_entry(kvm, &vm_list, vm_list) {
Dave Hansen45221ab2010-08-19 18:11:37 -07003569 int idx, freed_pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003570 LIST_HEAD(invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003571
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003572 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003573 spin_lock(&kvm->mmu_lock);
Dave Hansen45221ab2010-08-19 18:11:37 -07003574 if (!kvm_freed && nr_to_scan > 0 &&
3575 kvm->arch.n_used_mmu_pages > 0) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003576 freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm,
3577 &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003578 kvm_freed = kvm;
3579 }
3580 nr_to_scan--;
3581
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003582 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003583 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003584 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003585 }
3586 if (kvm_freed)
3587 list_move_tail(&kvm_freed->vm_list, &vm_list);
3588
Jan Kiszkae935b832011-02-08 12:55:33 +01003589 raw_spin_unlock(&kvm_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003590
Dave Hansen45221ab2010-08-19 18:11:37 -07003591out:
3592 return percpu_counter_read_positive(&kvm_total_used_mmu_pages);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003593}
3594
3595static struct shrinker mmu_shrinker = {
3596 .shrink = mmu_shrink,
3597 .seeks = DEFAULT_SEEKS * 10,
3598};
3599
Ingo Molnar2ddfd202008-05-22 10:37:48 +02003600static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03003601{
3602 if (pte_chain_cache)
3603 kmem_cache_destroy(pte_chain_cache);
3604 if (rmap_desc_cache)
3605 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003606 if (mmu_page_header_cache)
3607 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003608}
3609
3610int kvm_mmu_module_init(void)
3611{
3612 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
3613 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09003614 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003615 if (!pte_chain_cache)
3616 goto nomem;
3617 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
3618 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09003619 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003620 if (!rmap_desc_cache)
3621 goto nomem;
3622
Avi Kivityd3d25b02007-05-30 12:34:53 +03003623 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
3624 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09003625 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003626 if (!mmu_page_header_cache)
3627 goto nomem;
3628
Wei Yongjun45bf21a2010-08-23 16:13:15 +08003629 if (percpu_counter_init(&kvm_total_used_mmu_pages, 0))
3630 goto nomem;
3631
Izik Eidus3ee16c82008-03-30 15:17:21 +03003632 register_shrinker(&mmu_shrinker);
3633
Avi Kivityb5a33a72007-04-15 16:31:09 +03003634 return 0;
3635
3636nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03003637 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003638 return -ENOMEM;
3639}
3640
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003641/*
3642 * Caculate mmu pages needed for kvm.
3643 */
3644unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3645{
3646 int i;
3647 unsigned int nr_mmu_pages;
3648 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003649 struct kvm_memslots *slots;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003650
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003651 slots = kvm_memslots(kvm);
3652
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003653 for (i = 0; i < slots->nmemslots; i++)
3654 nr_pages += slots->memslots[i].npages;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003655
3656 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3657 nr_mmu_pages = max(nr_mmu_pages,
3658 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3659
3660 return nr_mmu_pages;
3661}
3662
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003663static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3664 unsigned len)
3665{
3666 if (len > buffer->len)
3667 return NULL;
3668 return buffer->ptr;
3669}
3670
3671static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3672 unsigned len)
3673{
3674 void *ret;
3675
3676 ret = pv_mmu_peek_buffer(buffer, len);
3677 if (!ret)
3678 return ret;
3679 buffer->ptr += len;
3680 buffer->len -= len;
3681 buffer->processed += len;
3682 return ret;
3683}
3684
3685static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3686 gpa_t addr, gpa_t value)
3687{
3688 int bytes = 8;
3689 int r;
3690
3691 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3692 bytes = 4;
3693
3694 r = mmu_topup_memory_caches(vcpu);
3695 if (r)
3696 return r;
3697
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003698 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003699 return -EFAULT;
3700
3701 return 1;
3702}
3703
3704static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3705{
Avi Kivity9f8fe502010-12-05 17:30:00 +02003706 (void)kvm_set_cr3(vcpu, kvm_read_cr3(vcpu));
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003707 return 1;
3708}
3709
3710static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3711{
3712 spin_lock(&vcpu->kvm->mmu_lock);
3713 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3714 spin_unlock(&vcpu->kvm->mmu_lock);
3715 return 1;
3716}
3717
3718static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3719 struct kvm_pv_mmu_op_buffer *buffer)
3720{
3721 struct kvm_mmu_op_header *header;
3722
3723 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3724 if (!header)
3725 return 0;
3726 switch (header->op) {
3727 case KVM_MMU_OP_WRITE_PTE: {
3728 struct kvm_mmu_op_write_pte *wpte;
3729
3730 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3731 if (!wpte)
3732 return 0;
3733 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3734 wpte->pte_val);
3735 }
3736 case KVM_MMU_OP_FLUSH_TLB: {
3737 struct kvm_mmu_op_flush_tlb *ftlb;
3738
3739 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3740 if (!ftlb)
3741 return 0;
3742 return kvm_pv_mmu_flush_tlb(vcpu);
3743 }
3744 case KVM_MMU_OP_RELEASE_PT: {
3745 struct kvm_mmu_op_release_pt *rpt;
3746
3747 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3748 if (!rpt)
3749 return 0;
3750 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3751 }
3752 default: return 0;
3753 }
3754}
3755
3756int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3757 gpa_t addr, unsigned long *ret)
3758{
3759 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003760 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003761
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003762 buffer->ptr = buffer->buf;
3763 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3764 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003765
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003766 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003767 if (r)
3768 goto out;
3769
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003770 while (buffer->len) {
3771 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003772 if (r < 0)
3773 goto out;
3774 if (r == 0)
3775 break;
3776 }
3777
3778 r = 1;
3779out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003780 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003781 return r;
3782}
3783
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003784int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3785{
3786 struct kvm_shadow_walk_iterator iterator;
3787 int nr_sptes = 0;
3788
3789 spin_lock(&vcpu->kvm->mmu_lock);
3790 for_each_shadow_entry(vcpu, addr, iterator) {
3791 sptes[iterator.level-1] = *iterator.sptep;
3792 nr_sptes++;
3793 if (!is_shadow_present_pte(*iterator.sptep))
3794 break;
3795 }
3796 spin_unlock(&vcpu->kvm->mmu_lock);
3797
3798 return nr_sptes;
3799}
3800EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3801
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08003802void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
3803{
3804 ASSERT(vcpu);
3805
3806 destroy_kvm_mmu(vcpu);
3807 free_mmu_pages(vcpu);
3808 mmu_free_memory_caches(vcpu);
Xiao Guangrongb034cf02010-12-23 16:08:35 +08003809}
3810
3811#ifdef CONFIG_KVM_MMU_AUDIT
3812#include "mmu_audit.c"
3813#else
3814static void mmu_audit_disable(void) { }
3815#endif
3816
3817void kvm_mmu_module_exit(void)
3818{
3819 mmu_destroy_caches();
3820 percpu_counter_destroy(&kvm_total_used_mmu_pages);
3821 unregister_shrinker(&mmu_shrinker);
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08003822 mmu_audit_disable();
3823}