blob: 64f90db369fba94d9c44bbdb3023bd9b8c20e0bb [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
114#define PT64_LEVEL_MASK(level) \
115 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
116
117#define PT64_INDEX(address, level)\
118 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
119
120
121#define PT32_LEVEL_BITS 10
122
123#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400124 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800125
126#define PT32_LEVEL_MASK(level) \
127 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
Joerg Roedele04da982009-07-27 16:30:45 +0200128#define PT32_LVL_OFFSET_MASK(level) \
129 (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
130 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131
132#define PT32_INDEX(address, level)\
133 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
134
135
Avi Kivity27aba762007-03-09 13:04:31 +0200136#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800137#define PT64_DIR_BASE_ADDR_MASK \
138 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200139#define PT64_LVL_ADDR_MASK(level) \
140 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
141 * PT64_LEVEL_BITS))) - 1))
142#define PT64_LVL_OFFSET_MASK(level) \
143 (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
144 * PT64_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800145
146#define PT32_BASE_ADDR_MASK PAGE_MASK
147#define PT32_DIR_BASE_ADDR_MASK \
148 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200149#define PT32_LVL_ADDR_MASK(level) \
150 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
151 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800152
Avi Kivity79539ce2007-11-21 02:06:21 +0200153#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
154 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800155
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800156#define RMAP_EXT 4
157
Avi Kivityfe135d22007-12-09 16:15:46 +0200158#define ACC_EXEC_MASK 1
159#define ACC_WRITE_MASK PT_WRITABLE_MASK
160#define ACC_USER_MASK PT_USER_MASK
161#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
162
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200163#include <trace/events/kvm.h>
164
Avi Kivity07420172009-07-06 12:21:32 +0300165#define CREATE_TRACE_POINTS
166#include "mmutrace.h"
167
Izik Eidus14032832009-09-23 21:47:17 +0300168#define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
169
Avi Kivity135f8c22008-08-21 17:49:56 +0300170#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
171
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800172struct kvm_rmap_desc {
Avi Kivityd555c332009-06-10 14:24:23 +0300173 u64 *sptes[RMAP_EXT];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800174 struct kvm_rmap_desc *more;
175};
176
Avi Kivity2d111232008-12-25 14:39:47 +0200177struct kvm_shadow_walk_iterator {
178 u64 addr;
179 hpa_t shadow_addr;
180 int level;
181 u64 *sptep;
182 unsigned index;
183};
184
185#define for_each_shadow_entry(_vcpu, _addr, _walker) \
186 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
187 shadow_walk_okay(&(_walker)); \
188 shadow_walk_next(&(_walker)))
189
Xiao Guangrong1047df12010-06-11 21:35:15 +0800190typedef void (*mmu_parent_walk_fn) (struct kvm_mmu_page *sp, u64 *spte);
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300191
Avi Kivityb5a33a72007-04-15 16:31:09 +0300192static struct kmem_cache *pte_chain_cache;
193static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300194static struct kmem_cache *mmu_page_header_cache;
Dave Hansen45221ab2010-08-19 18:11:37 -0700195static struct percpu_counter kvm_total_used_mmu_pages;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300196
Avi Kivityc7addb92007-09-16 18:58:32 +0200197static u64 __read_mostly shadow_trap_nonpresent_pte;
198static u64 __read_mostly shadow_notrap_nonpresent_pte;
Sheng Yang7b523452008-04-25 21:13:50 +0800199static u64 __read_mostly shadow_base_present_pte;
200static u64 __read_mostly shadow_nx_mask;
201static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
202static u64 __read_mostly shadow_user_mask;
203static u64 __read_mostly shadow_accessed_mask;
204static u64 __read_mostly shadow_dirty_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200205
Dong, Eddie82725b22009-03-30 16:21:08 +0800206static inline u64 rsvd_bits(int s, int e)
207{
208 return ((1ULL << (e - s + 1)) - 1) << s;
209}
210
Avi Kivityc7addb92007-09-16 18:58:32 +0200211void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
212{
213 shadow_trap_nonpresent_pte = trap_pte;
214 shadow_notrap_nonpresent_pte = notrap_pte;
215}
216EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
217
Sheng Yang7b523452008-04-25 21:13:50 +0800218void kvm_mmu_set_base_ptes(u64 base_pte)
219{
220 shadow_base_present_pte = base_pte;
221}
222EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
223
224void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang4b12f0d2009-04-27 20:35:42 +0800225 u64 dirty_mask, u64 nx_mask, u64 x_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800226{
227 shadow_user_mask = user_mask;
228 shadow_accessed_mask = accessed_mask;
229 shadow_dirty_mask = dirty_mask;
230 shadow_nx_mask = nx_mask;
231 shadow_x_mask = x_mask;
232}
233EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
234
Avi Kivity3dbe1412010-05-12 11:48:18 +0300235static bool is_write_protection(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800236{
Avi Kivity4d4ec082009-12-29 18:07:30 +0200237 return kvm_read_cr0_bits(vcpu, X86_CR0_WP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800238}
239
240static int is_cpuid_PSE36(void)
241{
242 return 1;
243}
244
Avi Kivity73b10872007-01-26 00:56:41 -0800245static int is_nx(struct kvm_vcpu *vcpu)
246{
Avi Kivityf6801df2010-01-21 15:31:50 +0200247 return vcpu->arch.efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800248}
249
Avi Kivityc7addb92007-09-16 18:58:32 +0200250static int is_shadow_present_pte(u64 pte)
251{
Avi Kivityc7addb92007-09-16 18:58:32 +0200252 return pte != shadow_trap_nonpresent_pte
253 && pte != shadow_notrap_nonpresent_pte;
254}
255
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300256static int is_large_pte(u64 pte)
257{
258 return pte & PT_PAGE_SIZE_MASK;
259}
260
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900261static int is_writable_pte(unsigned long pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800262{
263 return pte & PT_WRITABLE_MASK;
264}
265
Avi Kivity43a37952009-06-10 14:12:05 +0300266static int is_dirty_gpte(unsigned long pte)
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200267{
Avi Kivity439e2182009-06-10 12:56:54 +0300268 return pte & PT_DIRTY_MASK;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200269}
270
Avi Kivity43a37952009-06-10 14:12:05 +0300271static int is_rmap_spte(u64 pte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800272{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200273 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800274}
275
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300276static int is_last_spte(u64 pte, int level)
277{
278 if (level == PT_PAGE_TABLE_LEVEL)
279 return 1;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200280 if (is_large_pte(pte))
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300281 return 1;
282 return 0;
283}
284
Anthony Liguori35149e22008-04-02 14:46:56 -0500285static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200286{
Anthony Liguori35149e22008-04-02 14:46:56 -0500287 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200288}
289
Avi Kivityda928522007-11-21 13:54:47 +0200290static gfn_t pse36_gfn_delta(u32 gpte)
291{
292 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
293
294 return (gpte & PT32_DIR_PSE36_MASK) << shift;
295}
296
Avi Kivityd555c332009-06-10 14:24:23 +0300297static void __set_spte(u64 *sptep, u64 spte)
Avi Kivitye663ee62007-05-31 15:46:04 +0300298{
H. Peter Anvin7645e432010-08-06 12:18:11 -0700299 set_64bit(sptep, spte);
Avi Kivitye663ee62007-05-31 15:46:04 +0300300}
301
Avi Kivitya9221dd2010-06-06 14:48:06 +0300302static u64 __xchg_spte(u64 *sptep, u64 new_spte)
303{
304#ifdef CONFIG_X86_64
305 return xchg(sptep, new_spte);
306#else
307 u64 old_spte;
308
309 do {
310 old_spte = *sptep;
311 } while (cmpxchg64(sptep, old_spte, new_spte) != old_spte);
312
313 return old_spte;
314#endif
315}
316
Xiao Guangrong8672b722010-08-02 16:14:04 +0800317static bool spte_has_volatile_bits(u64 spte)
318{
319 if (!shadow_accessed_mask)
320 return false;
321
322 if (!is_shadow_present_pte(spte))
323 return false;
324
Xiao Guangrong41327792010-08-02 16:15:08 +0800325 if ((spte & shadow_accessed_mask) &&
326 (!is_writable_pte(spte) || (spte & shadow_dirty_mask)))
Xiao Guangrong8672b722010-08-02 16:14:04 +0800327 return false;
328
329 return true;
330}
331
Xiao Guangrong41327792010-08-02 16:15:08 +0800332static bool spte_is_bit_cleared(u64 old_spte, u64 new_spte, u64 bit_mask)
333{
334 return (old_spte & bit_mask) && !(new_spte & bit_mask);
335}
336
Avi Kivityb79b93f2010-06-06 15:46:44 +0300337static void update_spte(u64 *sptep, u64 new_spte)
338{
Xiao Guangrong41327792010-08-02 16:15:08 +0800339 u64 mask, old_spte = *sptep;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300340
Xiao Guangrong41327792010-08-02 16:15:08 +0800341 WARN_ON(!is_rmap_spte(new_spte));
342
343 new_spte |= old_spte & shadow_dirty_mask;
344
345 mask = shadow_accessed_mask;
346 if (is_writable_pte(old_spte))
347 mask |= shadow_dirty_mask;
348
349 if (!spte_has_volatile_bits(old_spte) || (new_spte & mask) == mask)
Avi Kivityb79b93f2010-06-06 15:46:44 +0300350 __set_spte(sptep, new_spte);
Xiao Guangrong41327792010-08-02 16:15:08 +0800351 else
Avi Kivityb79b93f2010-06-06 15:46:44 +0300352 old_spte = __xchg_spte(sptep, new_spte);
Xiao Guangrong41327792010-08-02 16:15:08 +0800353
354 if (!shadow_accessed_mask)
355 return;
356
357 if (spte_is_bit_cleared(old_spte, new_spte, shadow_accessed_mask))
358 kvm_set_pfn_accessed(spte_to_pfn(old_spte));
359 if (spte_is_bit_cleared(old_spte, new_spte, shadow_dirty_mask))
360 kvm_set_pfn_dirty(spte_to_pfn(old_spte));
Avi Kivityb79b93f2010-06-06 15:46:44 +0300361}
362
Avi Kivitye2dec932007-01-05 16:36:54 -0800363static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300364 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800365{
366 void *obj;
367
368 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800369 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800370 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300371 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800372 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800373 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800374 cache->objects[cache->nobjs++] = obj;
375 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800376 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800377}
378
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800379static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc,
380 struct kmem_cache *cache)
Avi Kivity714b93d2007-01-05 16:36:53 -0800381{
382 while (mc->nobjs)
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800383 kmem_cache_free(cache, mc->objects[--mc->nobjs]);
Avi Kivity714b93d2007-01-05 16:36:53 -0800384}
385
Avi Kivityc1158e62007-07-20 08:18:27 +0300386static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300387 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300388{
389 struct page *page;
390
391 if (cache->nobjs >= min)
392 return 0;
393 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300394 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300395 if (!page)
396 return -ENOMEM;
Avi Kivityc1158e62007-07-20 08:18:27 +0300397 cache->objects[cache->nobjs++] = page_address(page);
398 }
399 return 0;
400}
401
402static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
403{
404 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300405 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300406}
407
Avi Kivity8c438502007-04-16 11:53:17 +0300408static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
409{
410 int r;
411
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800412 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300413 pte_chain_cache, 4);
414 if (r)
415 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800416 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Xiao Guangrong957ed9e2010-08-22 19:12:48 +0800417 rmap_desc_cache, 4 + PTE_PREFETCH_NUM);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300418 if (r)
419 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800420 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300421 if (r)
422 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800423 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300424 mmu_page_header_cache, 4);
425out:
Avi Kivity8c438502007-04-16 11:53:17 +0300426 return r;
427}
428
Avi Kivity714b93d2007-01-05 16:36:53 -0800429static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
430{
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800431 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache, pte_chain_cache);
432 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache, rmap_desc_cache);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800433 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800434 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache,
435 mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800436}
437
438static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
439 size_t size)
440{
441 void *p;
442
443 BUG_ON(!mc->nobjs);
444 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800445 return p;
446}
447
Avi Kivity714b93d2007-01-05 16:36:53 -0800448static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
449{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800450 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800451 sizeof(struct kvm_pte_chain));
452}
453
Avi Kivity90cb0522007-07-17 13:04:56 +0300454static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800455{
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800456 kmem_cache_free(pte_chain_cache, pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800457}
458
459static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
460{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800461 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800462 sizeof(struct kvm_rmap_desc));
463}
464
Avi Kivity90cb0522007-07-17 13:04:56 +0300465static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800466{
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800467 kmem_cache_free(rmap_desc_cache, rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800468}
469
Lai Jiangshan2032a932010-05-26 16:49:59 +0800470static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
471{
472 if (!sp->role.direct)
473 return sp->gfns[index];
474
475 return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS));
476}
477
478static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn)
479{
480 if (sp->role.direct)
481 BUG_ON(gfn != kvm_mmu_page_get_gfn(sp, index));
482 else
483 sp->gfns[index] = gfn;
484}
485
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800486/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300487 * Return the pointer to the largepage write count for a given
488 * gfn, handling slots that are not large page aligned.
489 */
Joerg Roedeld25797b2009-07-27 16:30:43 +0200490static int *slot_largepage_idx(gfn_t gfn,
491 struct kvm_memory_slot *slot,
492 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300493{
494 unsigned long idx;
495
Joerg Roedel82855412010-07-01 16:00:11 +0200496 idx = (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
497 (slot->base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
Joerg Roedeld25797b2009-07-27 16:30:43 +0200498 return &slot->lpage_info[level - 2][idx].write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300499}
500
501static void account_shadowed(struct kvm *kvm, gfn_t gfn)
502{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200503 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300504 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200505 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300506
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300507 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200508 for (i = PT_DIRECTORY_LEVEL;
509 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
510 write_count = slot_largepage_idx(gfn, slot, i);
511 *write_count += 1;
512 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300513}
514
515static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
516{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200517 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300518 int *write_count;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200519 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300520
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300521 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200522 for (i = PT_DIRECTORY_LEVEL;
523 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200524 write_count = slot_largepage_idx(gfn, slot, i);
525 *write_count -= 1;
526 WARN_ON(*write_count < 0);
527 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300528}
529
Joerg Roedeld25797b2009-07-27 16:30:43 +0200530static int has_wrprotected_page(struct kvm *kvm,
531 gfn_t gfn,
532 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300533{
Izik Eidus28430992008-10-03 17:40:32 +0300534 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300535 int *largepage_idx;
536
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300537 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300538 if (slot) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200539 largepage_idx = slot_largepage_idx(gfn, slot, level);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300540 return *largepage_idx;
541 }
542
543 return 1;
544}
545
Joerg Roedeld25797b2009-07-27 16:30:43 +0200546static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300547{
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100548 unsigned long page_size;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200549 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300550
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100551 page_size = kvm_host_page_size(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300552
Joerg Roedeld25797b2009-07-27 16:30:43 +0200553 for (i = PT_PAGE_TABLE_LEVEL;
554 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
555 if (page_size >= KVM_HPAGE_SIZE(i))
556 ret = i;
557 else
558 break;
559 }
560
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300561 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300562}
563
Joerg Roedeld25797b2009-07-27 16:30:43 +0200564static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300565{
566 struct kvm_memory_slot *slot;
Sheng Yang878403b2010-01-05 19:02:29 +0800567 int host_level, level, max_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300568
569 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
570 if (slot && slot->dirty_bitmap)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200571 return PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300572
Joerg Roedeld25797b2009-07-27 16:30:43 +0200573 host_level = host_mapping_level(vcpu->kvm, large_gfn);
574
575 if (host_level == PT_PAGE_TABLE_LEVEL)
576 return host_level;
577
Sheng Yang878403b2010-01-05 19:02:29 +0800578 max_level = kvm_x86_ops->get_lpage_level() < host_level ?
579 kvm_x86_ops->get_lpage_level() : host_level;
580
581 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200582 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
583 break;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200584
585 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300586}
587
588/*
Izik Eidus290fc382007-09-27 14:11:22 +0200589 * Take gfn and return the reverse mapping to it.
Izik Eidus290fc382007-09-27 14:11:22 +0200590 */
591
Joerg Roedel44ad9942009-07-27 16:30:42 +0200592static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
Izik Eidus290fc382007-09-27 14:11:22 +0200593{
594 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300595 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200596
597 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200598 if (likely(level == PT_PAGE_TABLE_LEVEL))
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300599 return &slot->rmap[gfn - slot->base_gfn];
600
Joerg Roedel82855412010-07-01 16:00:11 +0200601 idx = (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
602 (slot->base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300603
Joerg Roedel44ad9942009-07-27 16:30:42 +0200604 return &slot->lpage_info[level - 2][idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200605}
606
607/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800608 * Reverse mapping data structures:
609 *
Izik Eidus290fc382007-09-27 14:11:22 +0200610 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
611 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800612 *
Izik Eidus290fc382007-09-27 14:11:22 +0200613 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
614 * containing more mappings.
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300615 *
616 * Returns the number of rmap entries before the spte was added or zero if
617 * the spte was not added.
618 *
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800619 */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200620static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800621{
Avi Kivity4db35312007-11-21 15:28:32 +0200622 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800623 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200624 unsigned long *rmapp;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300625 int i, count = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800626
Avi Kivity43a37952009-06-10 14:12:05 +0300627 if (!is_rmap_spte(*spte))
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300628 return count;
Avi Kivity4db35312007-11-21 15:28:32 +0200629 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800630 kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200631 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200632 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800633 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200634 *rmapp = (unsigned long)spte;
635 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800636 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800637 desc = mmu_alloc_rmap_desc(vcpu);
Avi Kivityd555c332009-06-10 14:24:23 +0300638 desc->sptes[0] = (u64 *)*rmapp;
639 desc->sptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200640 *rmapp = (unsigned long)desc | 1;
Hillf Dantoncb16a7b2010-09-18 08:41:02 +0800641 ++count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800642 } else {
643 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200644 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivityd555c332009-06-10 14:24:23 +0300645 while (desc->sptes[RMAP_EXT-1] && desc->more) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800646 desc = desc->more;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300647 count += RMAP_EXT;
648 }
Avi Kivityd555c332009-06-10 14:24:23 +0300649 if (desc->sptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800650 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800651 desc = desc->more;
652 }
Avi Kivityd555c332009-06-10 14:24:23 +0300653 for (i = 0; desc->sptes[i]; ++i)
Hillf Dantoncb16a7b2010-09-18 08:41:02 +0800654 ++count;
Avi Kivityd555c332009-06-10 14:24:23 +0300655 desc->sptes[i] = spte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800656 }
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300657 return count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800658}
659
Izik Eidus290fc382007-09-27 14:11:22 +0200660static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800661 struct kvm_rmap_desc *desc,
662 int i,
663 struct kvm_rmap_desc *prev_desc)
664{
665 int j;
666
Avi Kivityd555c332009-06-10 14:24:23 +0300667 for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800668 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300669 desc->sptes[i] = desc->sptes[j];
670 desc->sptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800671 if (j != 0)
672 return;
673 if (!prev_desc && !desc->more)
Avi Kivityd555c332009-06-10 14:24:23 +0300674 *rmapp = (unsigned long)desc->sptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800675 else
676 if (prev_desc)
677 prev_desc->more = desc->more;
678 else
Izik Eidus290fc382007-09-27 14:11:22 +0200679 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300680 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800681}
682
Izik Eidus290fc382007-09-27 14:11:22 +0200683static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800684{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800685 struct kvm_rmap_desc *desc;
686 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200687 struct kvm_mmu_page *sp;
Lai Jiangshan2032a932010-05-26 16:49:59 +0800688 gfn_t gfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200689 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800690 int i;
691
Avi Kivity4db35312007-11-21 15:28:32 +0200692 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800693 gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt);
694 rmapp = gfn_to_rmap(kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200695 if (!*rmapp) {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800696 printk(KERN_ERR "rmap_remove: %p 0->BUG\n", spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800697 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200698 } else if (!(*rmapp & 1)) {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800699 rmap_printk("rmap_remove: %p 1->0\n", spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200700 if ((u64 *)*rmapp != spte) {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800701 printk(KERN_ERR "rmap_remove: %p 1->BUG\n", spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800702 BUG();
703 }
Izik Eidus290fc382007-09-27 14:11:22 +0200704 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800705 } else {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800706 rmap_printk("rmap_remove: %p many->many\n", spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200707 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800708 prev_desc = NULL;
709 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300710 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
711 if (desc->sptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200712 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800713 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800714 prev_desc);
715 return;
716 }
717 prev_desc = desc;
718 desc = desc->more;
719 }
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800720 pr_err("rmap_remove: %p many->many\n", spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800721 BUG();
722 }
723}
724
Marcelo Tosattieb45fda2010-10-25 11:58:22 -0200725static int set_spte_track_bits(u64 *sptep, u64 new_spte)
Avi Kivitybe38d272010-06-06 14:31:27 +0300726{
Avi Kivityce061862010-06-06 14:38:12 +0300727 pfn_t pfn;
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800728 u64 old_spte = *sptep;
Avi Kivityce061862010-06-06 14:38:12 +0300729
Xiao Guangrong8672b722010-08-02 16:14:04 +0800730 if (!spte_has_volatile_bits(old_spte))
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800731 __set_spte(sptep, new_spte);
Xiao Guangrong8672b722010-08-02 16:14:04 +0800732 else
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800733 old_spte = __xchg_spte(sptep, new_spte);
734
Avi Kivitya9221dd2010-06-06 14:48:06 +0300735 if (!is_rmap_spte(old_spte))
Marcelo Tosattieb45fda2010-10-25 11:58:22 -0200736 return 0;
Xiao Guangrong8672b722010-08-02 16:14:04 +0800737
Avi Kivitya9221dd2010-06-06 14:48:06 +0300738 pfn = spte_to_pfn(old_spte);
Xiao Guangrongdaa3db62010-07-16 11:23:04 +0800739 if (!shadow_accessed_mask || old_spte & shadow_accessed_mask)
Avi Kivityce061862010-06-06 14:38:12 +0300740 kvm_set_pfn_accessed(pfn);
Xiao Guangrong41327792010-08-02 16:15:08 +0800741 if (!shadow_dirty_mask || (old_spte & shadow_dirty_mask))
Avi Kivityce061862010-06-06 14:38:12 +0300742 kvm_set_pfn_dirty(pfn);
Marcelo Tosattieb45fda2010-10-25 11:58:22 -0200743 return 1;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800744}
745
746static void drop_spte(struct kvm *kvm, u64 *sptep, u64 new_spte)
747{
Marcelo Tosattieb45fda2010-10-25 11:58:22 -0200748 if (set_spte_track_bits(sptep, new_spte))
749 rmap_remove(kvm, sptep);
Avi Kivitybe38d272010-06-06 14:31:27 +0300750}
751
Izik Eidus98348e92007-10-16 14:42:30 +0200752static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800753{
Avi Kivity374cbac2007-01-05 16:36:43 -0800754 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200755 u64 *prev_spte;
756 int i;
757
758 if (!*rmapp)
759 return NULL;
760 else if (!(*rmapp & 1)) {
761 if (!spte)
762 return (u64 *)*rmapp;
763 return NULL;
764 }
765 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Izik Eidus98348e92007-10-16 14:42:30 +0200766 prev_spte = NULL;
767 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300768 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
Izik Eidus98348e92007-10-16 14:42:30 +0200769 if (prev_spte == spte)
Avi Kivityd555c332009-06-10 14:24:23 +0300770 return desc->sptes[i];
771 prev_spte = desc->sptes[i];
Izik Eidus98348e92007-10-16 14:42:30 +0200772 }
773 desc = desc->more;
774 }
775 return NULL;
776}
777
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200778static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200779{
Izik Eidus290fc382007-09-27 14:11:22 +0200780 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800781 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200782 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800783
Joerg Roedel44ad9942009-07-27 16:30:42 +0200784 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
Avi Kivity374cbac2007-01-05 16:36:43 -0800785
Izik Eidus98348e92007-10-16 14:42:30 +0200786 spte = rmap_next(kvm, rmapp, NULL);
787 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800788 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800789 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800790 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900791 if (is_writable_pte(*spte)) {
Avi Kivityb79b93f2010-06-06 15:46:44 +0300792 update_spte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800793 write_protected = 1;
794 }
Izik Eidus9647c142007-10-16 14:43:46 +0200795 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800796 }
Izik Eidus855149a2008-03-20 18:17:24 +0200797
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300798 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200799 for (i = PT_DIRECTORY_LEVEL;
800 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
801 rmapp = gfn_to_rmap(kvm, gfn, i);
802 spte = rmap_next(kvm, rmapp, NULL);
803 while (spte) {
804 BUG_ON(!spte);
805 BUG_ON(!(*spte & PT_PRESENT_MASK));
806 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
807 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900808 if (is_writable_pte(*spte)) {
Avi Kivitybe38d272010-06-06 14:31:27 +0300809 drop_spte(kvm, spte,
810 shadow_trap_nonpresent_pte);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200811 --kvm->stat.lpages;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200812 spte = NULL;
813 write_protected = 1;
814 }
815 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300816 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300817 }
818
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200819 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800820}
821
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000822static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
823 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200824{
825 u64 *spte;
826 int need_tlb_flush = 0;
827
828 while ((spte = rmap_next(kvm, rmapp, NULL))) {
829 BUG_ON(!(*spte & PT_PRESENT_MASK));
830 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
Avi Kivitybe38d272010-06-06 14:31:27 +0300831 drop_spte(kvm, spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200832 need_tlb_flush = 1;
833 }
834 return need_tlb_flush;
835}
836
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000837static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
838 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +0300839{
840 int need_flush = 0;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800841 u64 *spte, new_spte;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300842 pte_t *ptep = (pte_t *)data;
843 pfn_t new_pfn;
844
845 WARN_ON(pte_huge(*ptep));
846 new_pfn = pte_pfn(*ptep);
847 spte = rmap_next(kvm, rmapp, NULL);
848 while (spte) {
849 BUG_ON(!is_shadow_present_pte(*spte));
850 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
851 need_flush = 1;
852 if (pte_write(*ptep)) {
Avi Kivitybe38d272010-06-06 14:31:27 +0300853 drop_spte(kvm, spte, shadow_trap_nonpresent_pte);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300854 spte = rmap_next(kvm, rmapp, NULL);
855 } else {
856 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
857 new_spte |= (u64)new_pfn << PAGE_SHIFT;
858
859 new_spte &= ~PT_WRITABLE_MASK;
860 new_spte &= ~SPTE_HOST_WRITEABLE;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300861 new_spte &= ~shadow_accessed_mask;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800862 set_spte_track_bits(spte, new_spte);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300863 spte = rmap_next(kvm, rmapp, spte);
864 }
865 }
866 if (need_flush)
867 kvm_flush_remote_tlbs(kvm);
868
869 return 0;
870}
871
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000872static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
873 unsigned long data,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300874 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000875 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200876{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200877 int i, j;
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200878 int ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200879 int retval = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200880 struct kvm_memslots *slots;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200881
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800882 slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200883
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200884 for (i = 0; i < slots->nmemslots; i++) {
885 struct kvm_memory_slot *memslot = &slots->memslots[i];
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200886 unsigned long start = memslot->userspace_addr;
887 unsigned long end;
888
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200889 end = start + (memslot->npages << PAGE_SHIFT);
890 if (hva >= start && hva < end) {
891 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200892
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200893 ret = handler(kvm, &memslot->rmap[gfn_offset], data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200894
895 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
Andrea Arcangeli6e3e2432010-07-16 11:52:55 +0200896 unsigned long idx;
897 int sh;
898
899 sh = KVM_HPAGE_GFN_SHIFT(PT_DIRECTORY_LEVEL+j);
900 idx = ((memslot->base_gfn+gfn_offset) >> sh) -
901 (memslot->base_gfn >> sh);
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200902 ret |= handler(kvm,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300903 &memslot->lpage_info[j][idx].rmap_pde,
904 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
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300955#define RMAP_RECYCLE_THRESHOLD 1000
956
Joerg Roedel852e3c12009-07-27 16:30:44 +0200957static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300958{
959 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200960 struct kvm_mmu_page *sp;
961
962 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300963
Joerg Roedel852e3c12009-07-27 16:30:44 +0200964 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300965
Izik Eidus3da0dd42009-09-23 21:47:18 +0300966 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300967 kvm_flush_remote_tlbs(vcpu->kvm);
968}
969
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200970int kvm_age_hva(struct kvm *kvm, unsigned long hva)
971{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300972 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200973}
974
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800975#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300976static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800977{
Avi Kivity139bdb22007-01-05 16:36:50 -0800978 u64 *pos;
979 u64 *end;
980
Avi Kivity47ad8e62007-05-06 15:50:58 +0300981 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300982 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800983 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800984 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800986 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987 return 1;
988}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800989#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800990
Dave Hansen45221ab2010-08-19 18:11:37 -0700991/*
992 * This value is the sum of all of the kvm instances's
993 * kvm->arch.n_used_mmu_pages values. We need a global,
994 * aggregate version in order to make the slab shrinker
995 * faster
996 */
997static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, int nr)
998{
999 kvm->arch.n_used_mmu_pages += nr;
1000 percpu_counter_add(&kvm_total_used_mmu_pages, nr);
1001}
1002
Avi Kivity4db35312007-11-21 15:28:32 +02001003static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -08001004{
Avi Kivity4db35312007-11-21 15:28:32 +02001005 ASSERT(is_empty_shadow_page(sp->spt));
Xiao Guangrong77758342010-06-04 21:53:54 +08001006 hlist_del(&sp->hash_link);
Avi Kivity4db35312007-11-21 15:28:32 +02001007 list_del(&sp->link);
1008 __free_page(virt_to_page(sp->spt));
Lai Jiangshan2032a932010-05-26 16:49:59 +08001009 if (!sp->role.direct)
1010 __free_page(virt_to_page(sp->gfns));
Xiao Guangronge8ad9a72010-05-13 10:06:02 +08001011 kmem_cache_free(mmu_page_header_cache, sp);
Dave Hansen45221ab2010-08-19 18:11:37 -07001012 kvm_mod_used_mmu_pages(kvm, -1);
Avi Kivity260746c2007-01-05 16:36:49 -08001013}
1014
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001015static unsigned kvm_page_table_hashfn(gfn_t gfn)
1016{
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001017 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001018}
1019
Avi Kivity25c0de22007-01-05 16:36:42 -08001020static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
Lai Jiangshan2032a932010-05-26 16:49:59 +08001021 u64 *parent_pte, int direct)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022{
Avi Kivity4db35312007-11-21 15:28:32 +02001023 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001024
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001025 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
1026 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Lai Jiangshan2032a932010-05-26 16:49:59 +08001027 if (!direct)
1028 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache,
1029 PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +02001030 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001031 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Sheng Yang291f26b2008-10-16 17:30:57 +08001032 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +02001033 sp->multimapped = 0;
1034 sp->parent_pte = parent_pte;
Dave Hansen45221ab2010-08-19 18:11:37 -07001035 kvm_mod_used_mmu_pages(vcpu->kvm, +1);
Avi Kivity4db35312007-11-21 15:28:32 +02001036 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001037}
1038
Avi Kivity714b93d2007-01-05 16:36:53 -08001039static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001040 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001041{
1042 struct kvm_pte_chain *pte_chain;
1043 struct hlist_node *node;
1044 int i;
1045
1046 if (!parent_pte)
1047 return;
Avi Kivity4db35312007-11-21 15:28:32 +02001048 if (!sp->multimapped) {
1049 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001050
1051 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +02001052 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001053 return;
1054 }
Avi Kivity4db35312007-11-21 15:28:32 +02001055 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -08001056 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02001057 INIT_HLIST_HEAD(&sp->parent_ptes);
1058 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001059 pte_chain->parent_ptes[0] = old;
1060 }
Avi Kivity4db35312007-11-21 15:28:32 +02001061 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001062 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
1063 continue;
1064 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
1065 if (!pte_chain->parent_ptes[i]) {
1066 pte_chain->parent_ptes[i] = parent_pte;
1067 return;
1068 }
1069 }
Avi Kivity714b93d2007-01-05 16:36:53 -08001070 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001071 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +02001072 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001073 pte_chain->parent_ptes[0] = parent_pte;
1074}
1075
Avi Kivity4db35312007-11-21 15:28:32 +02001076static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001077 u64 *parent_pte)
1078{
1079 struct kvm_pte_chain *pte_chain;
1080 struct hlist_node *node;
1081 int i;
1082
Avi Kivity4db35312007-11-21 15:28:32 +02001083 if (!sp->multimapped) {
1084 BUG_ON(sp->parent_pte != parent_pte);
1085 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001086 return;
1087 }
Avi Kivity4db35312007-11-21 15:28:32 +02001088 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001089 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1090 if (!pte_chain->parent_ptes[i])
1091 break;
1092 if (pte_chain->parent_ptes[i] != parent_pte)
1093 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001094 while (i + 1 < NR_PTE_CHAIN_ENTRIES
1095 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001096 pte_chain->parent_ptes[i]
1097 = pte_chain->parent_ptes[i + 1];
1098 ++i;
1099 }
1100 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001101 if (i == 0) {
1102 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001103 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +02001104 if (hlist_empty(&sp->parent_ptes)) {
1105 sp->multimapped = 0;
1106 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001107 }
1108 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001109 return;
1110 }
1111 BUG();
1112}
1113
Xiao Guangrong6b184932010-04-16 21:29:17 +08001114static void mmu_parent_walk(struct kvm_mmu_page *sp, mmu_parent_walk_fn fn)
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001115{
1116 struct kvm_pte_chain *pte_chain;
1117 struct hlist_node *node;
1118 struct kvm_mmu_page *parent_sp;
1119 int i;
1120
1121 if (!sp->multimapped && sp->parent_pte) {
1122 parent_sp = page_header(__pa(sp->parent_pte));
Xiao Guangrong1047df12010-06-11 21:35:15 +08001123 fn(parent_sp, sp->parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001124 return;
1125 }
1126
1127 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1128 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
Xiao Guangrong1047df12010-06-11 21:35:15 +08001129 u64 *spte = pte_chain->parent_ptes[i];
1130
1131 if (!spte)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001132 break;
Xiao Guangrong1047df12010-06-11 21:35:15 +08001133 parent_sp = page_header(__pa(spte));
1134 fn(parent_sp, spte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001135 }
1136}
1137
Xiao Guangrong1047df12010-06-11 21:35:15 +08001138static void mark_unsync(struct kvm_mmu_page *sp, u64 *spte);
Xiao Guangrong6b184932010-04-16 21:29:17 +08001139static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001140{
Xiao Guangrong1047df12010-06-11 21:35:15 +08001141 mmu_parent_walk(sp, mark_unsync);
1142}
1143
1144static void mark_unsync(struct kvm_mmu_page *sp, u64 *spte)
1145{
1146 unsigned int index;
1147
1148 index = spte - sp->spt;
1149 if (__test_and_set_bit(index, sp->unsync_child_bitmap))
1150 return;
1151 if (sp->unsync_children++)
1152 return;
1153 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001154}
1155
Avi Kivityd761a502008-05-29 14:55:03 +03001156static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1157 struct kvm_mmu_page *sp)
1158{
1159 int i;
1160
1161 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1162 sp->spt[i] = shadow_trap_nonpresent_pte;
1163}
1164
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001165static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001166 struct kvm_mmu_page *sp, bool clear_unsync)
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001167{
1168 return 1;
1169}
1170
Marcelo Tosattia7052892008-09-23 13:18:35 -03001171static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1172{
1173}
1174
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001175#define KVM_PAGE_ARRAY_NR 16
1176
1177struct kvm_mmu_pages {
1178 struct mmu_page_and_offset {
1179 struct kvm_mmu_page *sp;
1180 unsigned int idx;
1181 } page[KVM_PAGE_ARRAY_NR];
1182 unsigned int nr;
1183};
1184
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001185#define for_each_unsync_children(bitmap, idx) \
1186 for (idx = find_first_bit(bitmap, 512); \
1187 idx < 512; \
1188 idx = find_next_bit(bitmap, 512, idx+1))
1189
Hannes Edercded19f2009-02-21 02:19:13 +01001190static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1191 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001192{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001193 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001194
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001195 if (sp->unsync)
1196 for (i=0; i < pvec->nr; i++)
1197 if (pvec->page[i].sp == sp)
1198 return 0;
1199
1200 pvec->page[pvec->nr].sp = sp;
1201 pvec->page[pvec->nr].idx = idx;
1202 pvec->nr++;
1203 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1204}
1205
1206static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1207 struct kvm_mmu_pages *pvec)
1208{
1209 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001210
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001211 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001212 struct kvm_mmu_page *child;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001213 u64 ent = sp->spt[i];
1214
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001215 if (!is_shadow_present_pte(ent) || is_large_pte(ent))
1216 goto clear_child_bitmap;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001217
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001218 child = page_header(ent & PT64_BASE_ADDR_MASK);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001219
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001220 if (child->unsync_children) {
1221 if (mmu_pages_add(pvec, child, i))
1222 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001223
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001224 ret = __mmu_unsync_walk(child, pvec);
1225 if (!ret)
1226 goto clear_child_bitmap;
1227 else if (ret > 0)
1228 nr_unsync_leaf += ret;
1229 else
1230 return ret;
1231 } else if (child->unsync) {
1232 nr_unsync_leaf++;
1233 if (mmu_pages_add(pvec, child, i))
1234 return -ENOSPC;
1235 } else
1236 goto clear_child_bitmap;
1237
1238 continue;
1239
1240clear_child_bitmap:
1241 __clear_bit(i, sp->unsync_child_bitmap);
1242 sp->unsync_children--;
1243 WARN_ON((int)sp->unsync_children < 0);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001244 }
1245
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001246
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001247 return nr_unsync_leaf;
1248}
1249
1250static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1251 struct kvm_mmu_pages *pvec)
1252{
1253 if (!sp->unsync_children)
1254 return 0;
1255
1256 mmu_pages_add(pvec, sp, 0);
1257 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001258}
1259
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001260static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1261{
1262 WARN_ON(!sp->unsync);
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001263 trace_kvm_mmu_sync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001264 sp->unsync = 0;
1265 --kvm->stat.mmu_unsync;
1266}
1267
Xiao Guangrong77758342010-06-04 21:53:54 +08001268static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1269 struct list_head *invalid_list);
1270static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1271 struct list_head *invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001272
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001273#define for_each_gfn_sp(kvm, sp, gfn, pos) \
1274 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001275 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1276 if ((sp)->gfn != (gfn)) {} else
1277
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001278#define for_each_gfn_indirect_valid_sp(kvm, sp, gfn, pos) \
1279 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001280 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1281 if ((sp)->gfn != (gfn) || (sp)->role.direct || \
1282 (sp)->role.invalid) {} else
1283
Xiao Guangrongf918b442010-06-11 21:30:36 +08001284/* @sp->gfn should be write-protected at the call site */
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001285static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001286 struct list_head *invalid_list, bool clear_unsync)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001287{
Avi Kivity5b7e0102010-04-14 19:20:03 +03001288 if (sp->role.cr4_pae != !!is_pae(vcpu)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001289 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001290 return 1;
1291 }
1292
Xiao Guangrongf918b442010-06-11 21:30:36 +08001293 if (clear_unsync)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001294 kvm_unlink_unsync_page(vcpu->kvm, sp);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001295
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001296 if (vcpu->arch.mmu.sync_page(vcpu, sp, clear_unsync)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001297 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001298 return 1;
1299 }
1300
1301 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001302 return 0;
1303}
1304
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001305static int kvm_sync_page_transient(struct kvm_vcpu *vcpu,
1306 struct kvm_mmu_page *sp)
1307{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001308 LIST_HEAD(invalid_list);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001309 int ret;
1310
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001311 ret = __kvm_sync_page(vcpu, sp, &invalid_list, false);
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001312 if (ret)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001313 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
1314
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001315 return ret;
1316}
1317
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001318static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1319 struct list_head *invalid_list)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001320{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001321 return __kvm_sync_page(vcpu, sp, invalid_list, true);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001322}
1323
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001324/* @gfn should be write-protected at the call site */
1325static void kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1326{
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001327 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001328 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001329 LIST_HEAD(invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001330 bool flush = false;
1331
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001332 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001333 if (!s->unsync)
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001334 continue;
1335
1336 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1337 if ((s->role.cr4_pae != !!is_pae(vcpu)) ||
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001338 (vcpu->arch.mmu.sync_page(vcpu, s, true))) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001339 kvm_mmu_prepare_zap_page(vcpu->kvm, s, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001340 continue;
1341 }
1342 kvm_unlink_unsync_page(vcpu->kvm, s);
1343 flush = true;
1344 }
1345
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001346 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001347 if (flush)
1348 kvm_mmu_flush_tlb(vcpu);
1349}
1350
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001351struct mmu_page_path {
1352 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1353 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001354};
1355
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001356#define for_each_sp(pvec, sp, parents, i) \
1357 for (i = mmu_pages_next(&pvec, &parents, -1), \
1358 sp = pvec.page[i].sp; \
1359 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1360 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001361
Hannes Edercded19f2009-02-21 02:19:13 +01001362static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1363 struct mmu_page_path *parents,
1364 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001365{
1366 int n;
1367
1368 for (n = i+1; n < pvec->nr; n++) {
1369 struct kvm_mmu_page *sp = pvec->page[n].sp;
1370
1371 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1372 parents->idx[0] = pvec->page[n].idx;
1373 return n;
1374 }
1375
1376 parents->parent[sp->role.level-2] = sp;
1377 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1378 }
1379
1380 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001381}
1382
Hannes Edercded19f2009-02-21 02:19:13 +01001383static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001384{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001385 struct kvm_mmu_page *sp;
1386 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001387
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001388 do {
1389 unsigned int idx = parents->idx[level];
1390
1391 sp = parents->parent[level];
1392 if (!sp)
1393 return;
1394
1395 --sp->unsync_children;
1396 WARN_ON((int)sp->unsync_children < 0);
1397 __clear_bit(idx, sp->unsync_child_bitmap);
1398 level++;
1399 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1400}
1401
1402static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1403 struct mmu_page_path *parents,
1404 struct kvm_mmu_pages *pvec)
1405{
1406 parents->parent[parent->role.level-1] = NULL;
1407 pvec->nr = 0;
1408}
1409
1410static void mmu_sync_children(struct kvm_vcpu *vcpu,
1411 struct kvm_mmu_page *parent)
1412{
1413 int i;
1414 struct kvm_mmu_page *sp;
1415 struct mmu_page_path parents;
1416 struct kvm_mmu_pages pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001417 LIST_HEAD(invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001418
1419 kvm_mmu_pages_init(parent, &parents, &pages);
1420 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001421 int protected = 0;
1422
1423 for_each_sp(pages, sp, parents, i)
1424 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1425
1426 if (protected)
1427 kvm_flush_remote_tlbs(vcpu->kvm);
1428
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001429 for_each_sp(pages, sp, parents, i) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001430 kvm_sync_page(vcpu, sp, &invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001431 mmu_pages_clear_parents(&parents);
1432 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001433 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001434 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001435 kvm_mmu_pages_init(parent, &parents, &pages);
1436 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001437}
1438
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001439static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1440 gfn_t gfn,
1441 gva_t gaddr,
1442 unsigned level,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001443 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001444 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001445 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001446{
1447 union kvm_mmu_page_role role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001448 unsigned quadrant;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001449 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001450 struct hlist_node *node;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001451 bool need_sync = false;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001452
Avi Kivitya770f6f2008-12-21 19:20:09 +02001453 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001454 role.level = level;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001455 role.direct = direct;
Avi Kivity84b0c8c2010-03-14 10:16:40 +02001456 if (role.direct)
Avi Kivity5b7e0102010-04-14 19:20:03 +03001457 role.cr4_pae = 0;
Avi Kivity41074d02007-12-09 17:00:02 +02001458 role.access = access;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02001459 if (!vcpu->arch.mmu.direct_map
1460 && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001461 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1462 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1463 role.quadrant = quadrant;
1464 }
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001465 for_each_gfn_sp(vcpu->kvm, sp, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001466 if (!need_sync && sp->unsync)
1467 need_sync = true;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001468
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001469 if (sp->role.word != role.word)
1470 continue;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001471
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001472 if (sp->unsync && kvm_sync_page_transient(vcpu, sp))
1473 break;
Xiao Guangronge02aa902010-05-15 18:52:34 +08001474
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001475 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1476 if (sp->unsync_children) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001477 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001478 kvm_mmu_mark_parents_unsync(sp);
1479 } else if (sp->unsync)
1480 kvm_mmu_mark_parents_unsync(sp);
Xiao Guangronge02aa902010-05-15 18:52:34 +08001481
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001482 trace_kvm_mmu_get_page(sp, false);
1483 return sp;
1484 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001485 ++vcpu->kvm->stat.mmu_cache_miss;
Lai Jiangshan2032a932010-05-26 16:49:59 +08001486 sp = kvm_mmu_alloc_page(vcpu, parent_pte, direct);
Avi Kivity4db35312007-11-21 15:28:32 +02001487 if (!sp)
1488 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001489 sp->gfn = gfn;
1490 sp->role = role;
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001491 hlist_add_head(&sp->hash_link,
1492 &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001493 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001494 if (rmap_write_protect(vcpu->kvm, gfn))
1495 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001496 if (level > PT_PAGE_TABLE_LEVEL && need_sync)
1497 kvm_sync_pages(vcpu, gfn);
1498
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001499 account_shadowed(vcpu->kvm, gfn);
1500 }
Avi Kivity131d8272008-05-29 14:56:28 +03001501 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1502 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1503 else
1504 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001505 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001506 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001507}
1508
Avi Kivity2d111232008-12-25 14:39:47 +02001509static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1510 struct kvm_vcpu *vcpu, u64 addr)
1511{
1512 iterator->addr = addr;
1513 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1514 iterator->level = vcpu->arch.mmu.shadow_root_level;
Joerg Roedel81407ca2010-09-10 17:31:00 +02001515
1516 if (iterator->level == PT64_ROOT_LEVEL &&
1517 vcpu->arch.mmu.root_level < PT64_ROOT_LEVEL &&
1518 !vcpu->arch.mmu.direct_map)
1519 --iterator->level;
1520
Avi Kivity2d111232008-12-25 14:39:47 +02001521 if (iterator->level == PT32E_ROOT_LEVEL) {
1522 iterator->shadow_addr
1523 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1524 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1525 --iterator->level;
1526 if (!iterator->shadow_addr)
1527 iterator->level = 0;
1528 }
1529}
1530
1531static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1532{
1533 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1534 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001535
1536 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1537 if (is_large_pte(*iterator->sptep))
1538 return false;
1539
Avi Kivity2d111232008-12-25 14:39:47 +02001540 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1541 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1542 return true;
1543}
1544
1545static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1546{
1547 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1548 --iterator->level;
1549}
1550
Avi Kivity32ef26a2010-07-13 14:27:04 +03001551static void link_shadow_page(u64 *sptep, struct kvm_mmu_page *sp)
1552{
1553 u64 spte;
1554
1555 spte = __pa(sp->spt)
1556 | PT_PRESENT_MASK | PT_ACCESSED_MASK
1557 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity121eee92010-07-13 14:27:05 +03001558 __set_spte(sptep, spte);
Avi Kivity32ef26a2010-07-13 14:27:04 +03001559}
1560
Avi Kivitya3aa51c2010-07-13 14:27:06 +03001561static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep)
1562{
1563 if (is_large_pte(*sptep)) {
1564 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
1565 kvm_flush_remote_tlbs(vcpu->kvm);
1566 }
1567}
1568
Avi Kivitya357bd22010-07-13 14:27:07 +03001569static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1570 unsigned direct_access)
1571{
1572 if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
1573 struct kvm_mmu_page *child;
1574
1575 /*
1576 * For the direct sp, if the guest pte's dirty bit
1577 * changed form clean to dirty, it will corrupt the
1578 * sp's access: allow writable in the read-only sp,
1579 * so we should update the spte at this point to get
1580 * a new sp with the correct access.
1581 */
1582 child = page_header(*sptep & PT64_BASE_ADDR_MASK);
1583 if (child->role.access == direct_access)
1584 return;
1585
1586 mmu_page_remove_parent_pte(child, sptep);
1587 __set_spte(sptep, shadow_trap_nonpresent_pte);
1588 kvm_flush_remote_tlbs(vcpu->kvm);
1589 }
1590}
1591
Avi Kivity90cb0522007-07-17 13:04:56 +03001592static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001593 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001594{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001595 unsigned i;
1596 u64 *pt;
1597 u64 ent;
1598
Avi Kivity4db35312007-11-21 15:28:32 +02001599 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001600
Avi Kivity697fe2e2007-01-05 16:36:46 -08001601 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1602 ent = pt[i];
1603
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001604 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001605 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001606 ent &= PT64_BASE_ADDR_MASK;
1607 mmu_page_remove_parent_pte(page_header(ent),
1608 &pt[i]);
1609 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001610 if (is_large_pte(ent))
1611 --kvm->stat.lpages;
Avi Kivitybe38d272010-06-06 14:31:27 +03001612 drop_spte(kvm, &pt[i],
1613 shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001614 }
1615 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001616 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001617 }
Avi Kivitya4360362007-01-05 16:36:45 -08001618}
1619
Avi Kivity4db35312007-11-21 15:28:32 +02001620static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001621{
Avi Kivity4db35312007-11-21 15:28:32 +02001622 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001623}
1624
Avi Kivity12b7d282007-09-23 14:10:49 +02001625static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1626{
1627 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001628 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001629
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001630 kvm_for_each_vcpu(i, vcpu, kvm)
1631 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001632}
1633
Avi Kivity31aa2b42008-07-11 17:59:46 +03001634static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001635{
1636 u64 *parent_pte;
1637
Avi Kivity4db35312007-11-21 15:28:32 +02001638 while (sp->multimapped || sp->parent_pte) {
1639 if (!sp->multimapped)
1640 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001641 else {
1642 struct kvm_pte_chain *chain;
1643
Avi Kivity4db35312007-11-21 15:28:32 +02001644 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001645 struct kvm_pte_chain, link);
1646 parent_pte = chain->parent_ptes[0];
1647 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001648 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001649 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001650 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001651 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001652}
1653
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001654static int mmu_zap_unsync_children(struct kvm *kvm,
Xiao Guangrong77758342010-06-04 21:53:54 +08001655 struct kvm_mmu_page *parent,
1656 struct list_head *invalid_list)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001657{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001658 int i, zapped = 0;
1659 struct mmu_page_path parents;
1660 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001661
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001662 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001663 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001664
1665 kvm_mmu_pages_init(parent, &parents, &pages);
1666 while (mmu_unsync_walk(parent, &pages)) {
1667 struct kvm_mmu_page *sp;
1668
1669 for_each_sp(pages, sp, parents, i) {
Xiao Guangrong77758342010-06-04 21:53:54 +08001670 kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001671 mmu_pages_clear_parents(&parents);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001672 zapped++;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001673 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001674 kvm_mmu_pages_init(parent, &parents, &pages);
1675 }
1676
1677 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001678}
1679
Xiao Guangrong77758342010-06-04 21:53:54 +08001680static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1681 struct list_head *invalid_list)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001682{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001683 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001684
Xiao Guangrong77758342010-06-04 21:53:54 +08001685 trace_kvm_mmu_prepare_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001686 ++kvm->stat.mmu_shadow_zapped;
Xiao Guangrong77758342010-06-04 21:53:54 +08001687 ret = mmu_zap_unsync_children(kvm, sp, invalid_list);
Avi Kivity4db35312007-11-21 15:28:32 +02001688 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001689 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001690 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001691 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001692 if (sp->unsync)
1693 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001694 if (!sp->root_count) {
Gui Jianfeng54a4f022010-05-05 09:03:49 +08001695 /* Count self */
1696 ret++;
Xiao Guangrong77758342010-06-04 21:53:54 +08001697 list_move(&sp->link, invalid_list);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001698 } else {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001699 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001700 kvm_reload_remote_mmus(kvm);
1701 }
Xiao Guangrong77758342010-06-04 21:53:54 +08001702
1703 sp->role.invalid = 1;
Avi Kivity12b7d282007-09-23 14:10:49 +02001704 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001705 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001706}
1707
Xiao Guangrong77758342010-06-04 21:53:54 +08001708static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1709 struct list_head *invalid_list)
1710{
1711 struct kvm_mmu_page *sp;
1712
1713 if (list_empty(invalid_list))
1714 return;
1715
1716 kvm_flush_remote_tlbs(kvm);
1717
1718 do {
1719 sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
1720 WARN_ON(!sp->role.invalid || sp->root_count);
1721 kvm_mmu_free_page(kvm, sp);
1722 } while (!list_empty(invalid_list));
1723
1724}
1725
Izik Eidus82ce2c92007-10-02 18:52:55 +02001726/*
1727 * Changing the number of mmu pages allocated to the vm
Dave Hansen49d5ca22010-08-19 18:11:28 -07001728 * Note: if goal_nr_mmu_pages is too small, you will get dead lock
Izik Eidus82ce2c92007-10-02 18:52:55 +02001729 */
Dave Hansen49d5ca22010-08-19 18:11:28 -07001730void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages)
Izik Eidus82ce2c92007-10-02 18:52:55 +02001731{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001732 LIST_HEAD(invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001733 /*
1734 * If we set the number of mmu pages to be smaller be than the
1735 * number of actived pages , we must to free some mmu pages before we
1736 * change the value
1737 */
1738
Dave Hansen49d5ca22010-08-19 18:11:28 -07001739 if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) {
1740 while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages &&
Xiao Guangrong77662e02010-04-16 16:34:42 +08001741 !list_empty(&kvm->arch.active_mmu_pages)) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001742 struct kvm_mmu_page *page;
1743
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001744 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001745 struct kvm_mmu_page, link);
Xiaotian Feng80b63fa2010-08-24 10:31:07 +08001746 kvm_mmu_prepare_zap_page(kvm, page, &invalid_list);
1747 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001748 }
Dave Hansen49d5ca22010-08-19 18:11:28 -07001749 goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001750 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02001751
Dave Hansen49d5ca22010-08-19 18:11:28 -07001752 kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001753}
1754
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001755static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001756{
Avi Kivity4db35312007-11-21 15:28:32 +02001757 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001758 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001759 LIST_HEAD(invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001760 int r;
1761
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08001762 pgprintk("%s: looking for gfn %llx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001763 r = 0;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001764
1765 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08001766 pgprintk("%s: gfn %llx role %x\n", __func__, gfn,
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001767 sp->role.word);
1768 r = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001769 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001770 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001771 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001772 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001773}
1774
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001775static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001776{
Avi Kivity4db35312007-11-21 15:28:32 +02001777 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001778 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001779 LIST_HEAD(invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001780
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001781 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08001782 pgprintk("%s: zap %llx %x\n",
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001783 __func__, gfn, sp->role.word);
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001784 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001785 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001786 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001787}
1788
Avi Kivity38c335f2007-11-21 14:20:22 +02001789static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001790{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001791 int slot = memslot_id(kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001792 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001793
Sheng Yang291f26b2008-10-16 17:30:57 +08001794 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001795}
1796
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001797static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1798{
1799 int i;
1800 u64 *pt = sp->spt;
1801
1802 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1803 return;
1804
1805 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1806 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001807 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001808 }
1809}
1810
Sheng Yang74be52e2008-10-09 16:01:56 +08001811/*
1812 * The function is based on mtrr_type_lookup() in
1813 * arch/x86/kernel/cpu/mtrr/generic.c
1814 */
1815static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1816 u64 start, u64 end)
1817{
1818 int i;
1819 u64 base, mask;
1820 u8 prev_match, curr_match;
1821 int num_var_ranges = KVM_NR_VAR_MTRR;
1822
1823 if (!mtrr_state->enabled)
1824 return 0xFF;
1825
1826 /* Make end inclusive end, instead of exclusive */
1827 end--;
1828
1829 /* Look in fixed ranges. Just return the type as per start */
1830 if (mtrr_state->have_fixed && (start < 0x100000)) {
1831 int idx;
1832
1833 if (start < 0x80000) {
1834 idx = 0;
1835 idx += (start >> 16);
1836 return mtrr_state->fixed_ranges[idx];
1837 } else if (start < 0xC0000) {
1838 idx = 1 * 8;
1839 idx += ((start - 0x80000) >> 14);
1840 return mtrr_state->fixed_ranges[idx];
1841 } else if (start < 0x1000000) {
1842 idx = 3 * 8;
1843 idx += ((start - 0xC0000) >> 12);
1844 return mtrr_state->fixed_ranges[idx];
1845 }
1846 }
1847
1848 /*
1849 * Look in variable ranges
1850 * Look of multiple ranges matching this address and pick type
1851 * as per MTRR precedence
1852 */
1853 if (!(mtrr_state->enabled & 2))
1854 return mtrr_state->def_type;
1855
1856 prev_match = 0xFF;
1857 for (i = 0; i < num_var_ranges; ++i) {
1858 unsigned short start_state, end_state;
1859
1860 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1861 continue;
1862
1863 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1864 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1865 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1866 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1867
1868 start_state = ((start & mask) == (base & mask));
1869 end_state = ((end & mask) == (base & mask));
1870 if (start_state != end_state)
1871 return 0xFE;
1872
1873 if ((start & mask) != (base & mask))
1874 continue;
1875
1876 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1877 if (prev_match == 0xFF) {
1878 prev_match = curr_match;
1879 continue;
1880 }
1881
1882 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1883 curr_match == MTRR_TYPE_UNCACHABLE)
1884 return MTRR_TYPE_UNCACHABLE;
1885
1886 if ((prev_match == MTRR_TYPE_WRBACK &&
1887 curr_match == MTRR_TYPE_WRTHROUGH) ||
1888 (prev_match == MTRR_TYPE_WRTHROUGH &&
1889 curr_match == MTRR_TYPE_WRBACK)) {
1890 prev_match = MTRR_TYPE_WRTHROUGH;
1891 curr_match = MTRR_TYPE_WRTHROUGH;
1892 }
1893
1894 if (prev_match != curr_match)
1895 return MTRR_TYPE_UNCACHABLE;
1896 }
1897
1898 if (prev_match != 0xFF)
1899 return prev_match;
1900
1901 return mtrr_state->def_type;
1902}
1903
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001904u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001905{
1906 u8 mtrr;
1907
1908 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1909 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1910 if (mtrr == 0xfe || mtrr == 0xff)
1911 mtrr = MTRR_TYPE_WRBACK;
1912 return mtrr;
1913}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001914EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001915
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001916static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001917{
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001918 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001919 ++vcpu->kvm->stat.mmu_unsync;
1920 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001921
Xiao Guangrong6b184932010-04-16 21:29:17 +08001922 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001923 mmu_convert_notrap(sp);
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001924}
1925
1926static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1927{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001928 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001929 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001930
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001931 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001932 if (s->unsync)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001933 continue;
1934 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1935 __kvm_unsync_page(vcpu, s);
1936 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001937}
1938
1939static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1940 bool can_unsync)
1941{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001942 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001943 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001944 bool need_unsync = false;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001945
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001946 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001947 if (!can_unsync)
1948 return 1;
1949
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001950 if (s->role.level != PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001951 return 1;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001952
1953 if (!need_unsync && !s->unsync) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001954 if (!oos_shadow)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001955 return 1;
1956 need_unsync = true;
1957 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001958 }
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001959 if (need_unsync)
1960 kvm_unsync_pages(vcpu, gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001961 return 0;
1962}
1963
Avi Kivityd555c332009-06-10 14:24:23 +03001964static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001965 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001966 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001967 gfn_t gfn, pfn_t pfn, bool speculative,
Izik Eidus14032832009-09-23 21:47:17 +03001968 bool can_unsync, bool reset_host_protection)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001969{
1970 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001971 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001972
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001973 /*
1974 * We don't set the accessed bit, since we sometimes want to see
1975 * whether the guest actually used the pte (in order to detect
1976 * demand paging).
1977 */
Xiao Guangrong41327792010-08-02 16:15:08 +08001978 spte = shadow_base_present_pte;
Avi Kivity947da532008-03-18 11:05:52 +02001979 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001980 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001981 if (!dirty)
1982 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001983 if (pte_access & ACC_EXEC_MASK)
1984 spte |= shadow_x_mask;
1985 else
1986 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001987 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001988 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001989 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001990 spte |= PT_PAGE_SIZE_MASK;
Avi Kivityb0bc3ee2010-09-13 16:45:28 +02001991 if (tdp_enabled)
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001992 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1993 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001994
Izik Eidus14032832009-09-23 21:47:17 +03001995 if (reset_host_protection)
1996 spte |= SPTE_HOST_WRITEABLE;
1997
Anthony Liguori35149e22008-04-02 14:46:56 -05001998 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001999
2000 if ((pte_access & ACC_WRITE_MASK)
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002001 || (!vcpu->arch.mmu.direct_map && write_fault
2002 && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002003
Joerg Roedel852e3c12009-07-27 16:30:44 +02002004 if (level > PT_PAGE_TABLE_LEVEL &&
2005 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002006 ret = 1;
Avi Kivitybe38d272010-06-06 14:31:27 +03002007 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
2008 goto done;
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002009 }
2010
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002011 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002012
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002013 if (!vcpu->arch.mmu.direct_map
2014 && !(pte_access & ACC_WRITE_MASK))
Avi Kivity69325a12010-05-27 14:35:58 +03002015 spte &= ~PT_USER_MASK;
2016
Marcelo Tosattiecc55892008-11-25 15:58:07 +01002017 /*
2018 * Optimization: for pte sync, if spte was writable the hash
2019 * lookup is unnecessary (and expensive). Write protection
2020 * is responsibility of mmu_get_page / kvm_sync_page.
2021 * Same reasoning can be applied to dirty page accounting.
2022 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002023 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01002024 goto set_pte;
2025
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002026 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002027 pgprintk("%s: found shadow page for %llx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002028 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002029 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002030 pte_access &= ~ACC_WRITE_MASK;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002031 if (is_writable_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002032 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002033 }
2034 }
2035
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002036 if (pte_access & ACC_WRITE_MASK)
2037 mark_page_dirty(vcpu->kvm, gfn);
2038
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002039set_pte:
Avi Kivityb79b93f2010-06-06 15:46:44 +03002040 update_spte(sptep, spte);
Avi Kivitybe38d272010-06-06 14:31:27 +03002041done:
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002042 return ret;
2043}
2044
Avi Kivityd555c332009-06-10 14:24:23 +03002045static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002046 unsigned pt_access, unsigned pte_access,
2047 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002048 int *ptwrite, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03002049 pfn_t pfn, bool speculative,
2050 bool reset_host_protection)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002051{
2052 int was_rmapped = 0;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002053 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002054
2055 pgprintk("%s: spte %llx access %x write_fault %d"
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002056 " user_fault %d gfn %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002057 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002058 write_fault, user_fault, gfn);
2059
Avi Kivityd555c332009-06-10 14:24:23 +03002060 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002061 /*
2062 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
2063 * the parent of the now unreachable PTE.
2064 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02002065 if (level > PT_PAGE_TABLE_LEVEL &&
2066 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002067 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03002068 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002069
2070 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03002071 mmu_page_remove_parent_pte(child, sptep);
Marcelo Tosatti3be22642010-05-28 09:44:59 -03002072 __set_spte(sptep, shadow_trap_nonpresent_pte);
2073 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityd555c332009-06-10 14:24:23 +03002074 } else if (pfn != spte_to_pfn(*sptep)) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002075 pgprintk("hfn old %llx new %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002076 spte_to_pfn(*sptep), pfn);
Avi Kivitybe38d272010-06-06 14:31:27 +03002077 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
Xiao Guangrong91546352010-06-30 16:04:06 +08002078 kvm_flush_remote_tlbs(vcpu->kvm);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01002079 } else
2080 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002081 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02002082
Avi Kivityd555c332009-06-10 14:24:23 +03002083 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Izik Eidus14032832009-09-23 21:47:17 +03002084 dirty, level, gfn, pfn, speculative, true,
2085 reset_host_protection)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002086 if (write_fault)
2087 *ptwrite = 1;
Xiao Guangrong5304efd2010-06-08 20:05:57 +08002088 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03002089 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002090
Avi Kivityd555c332009-06-10 14:24:23 +03002091 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002092 pgprintk("instantiating %s PTE (%s) at %llx (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002093 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02002094 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
2095 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03002096 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002097 ++vcpu->kvm->stat.lpages;
2098
Avi Kivityd555c332009-06-10 14:24:23 +03002099 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002100 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02002101 rmap_count = rmap_add(vcpu, sptep, gfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002102 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02002103 rmap_recycle(vcpu, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002104 }
Xiao Guangrong9ed55202010-07-16 11:25:17 +08002105 kvm_release_pfn_clean(pfn);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002106 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03002107 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002108 vcpu->arch.last_pte_gfn = gfn;
2109 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002110}
2111
Avi Kivity6aa8b732006-12-10 02:21:36 -08002112static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
2113{
2114}
2115
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002116static struct kvm_memory_slot *
2117pte_prefetch_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn, bool no_dirty_log)
2118{
2119 struct kvm_memory_slot *slot;
2120
2121 slot = gfn_to_memslot(vcpu->kvm, gfn);
2122 if (!slot || slot->flags & KVM_MEMSLOT_INVALID ||
2123 (no_dirty_log && slot->dirty_bitmap))
2124 slot = NULL;
2125
2126 return slot;
2127}
2128
2129static pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn,
2130 bool no_dirty_log)
2131{
2132 struct kvm_memory_slot *slot;
2133 unsigned long hva;
2134
2135 slot = pte_prefetch_gfn_to_memslot(vcpu, gfn, no_dirty_log);
2136 if (!slot) {
2137 get_page(bad_page);
2138 return page_to_pfn(bad_page);
2139 }
2140
2141 hva = gfn_to_hva_memslot(slot, gfn);
2142
2143 return hva_to_pfn_atomic(vcpu->kvm, hva);
2144}
2145
2146static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,
2147 struct kvm_mmu_page *sp,
2148 u64 *start, u64 *end)
2149{
2150 struct page *pages[PTE_PREFETCH_NUM];
2151 unsigned access = sp->role.access;
2152 int i, ret;
2153 gfn_t gfn;
2154
2155 gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt);
2156 if (!pte_prefetch_gfn_to_memslot(vcpu, gfn, access & ACC_WRITE_MASK))
2157 return -1;
2158
2159 ret = gfn_to_page_many_atomic(vcpu->kvm, gfn, pages, end - start);
2160 if (ret <= 0)
2161 return -1;
2162
2163 for (i = 0; i < ret; i++, gfn++, start++)
2164 mmu_set_spte(vcpu, start, ACC_ALL,
2165 access, 0, 0, 1, NULL,
2166 sp->role.level, gfn,
2167 page_to_pfn(pages[i]), true, true);
2168
2169 return 0;
2170}
2171
2172static void __direct_pte_prefetch(struct kvm_vcpu *vcpu,
2173 struct kvm_mmu_page *sp, u64 *sptep)
2174{
2175 u64 *spte, *start = NULL;
2176 int i;
2177
2178 WARN_ON(!sp->role.direct);
2179
2180 i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
2181 spte = sp->spt + i;
2182
2183 for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
2184 if (*spte != shadow_trap_nonpresent_pte || spte == sptep) {
2185 if (!start)
2186 continue;
2187 if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0)
2188 break;
2189 start = NULL;
2190 } else if (!start)
2191 start = spte;
2192 }
2193}
2194
2195static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)
2196{
2197 struct kvm_mmu_page *sp;
2198
2199 /*
2200 * Since it's no accessed bit on EPT, it's no way to
2201 * distinguish between actually accessed translations
2202 * and prefetched, so disable pte prefetch if EPT is
2203 * enabled.
2204 */
2205 if (!shadow_accessed_mask)
2206 return;
2207
2208 sp = page_header(__pa(sptep));
2209 if (sp->role.level > PT_PAGE_TABLE_LEVEL)
2210 return;
2211
2212 __direct_pte_prefetch(vcpu, sp, sptep);
2213}
2214
Joerg Roedel4d9976b2008-02-07 13:47:42 +01002215static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002216 int level, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002217{
Avi Kivity9f652d22008-12-25 14:54:25 +02002218 struct kvm_shadow_walk_iterator iterator;
2219 struct kvm_mmu_page *sp;
2220 int pt_write = 0;
2221 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002222
Avi Kivity9f652d22008-12-25 14:54:25 +02002223 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02002224 if (iterator.level == level) {
Avi Kivity9f652d22008-12-25 14:54:25 +02002225 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
2226 0, write, 1, &pt_write,
Izik Eidus14032832009-09-23 21:47:17 +03002227 level, gfn, pfn, false, true);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002228 direct_pte_prefetch(vcpu, iterator.sptep);
Avi Kivity9f652d22008-12-25 14:54:25 +02002229 ++vcpu->stat.pf_fixed;
2230 break;
2231 }
2232
2233 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
Lai Jiangshanc9fa0b32010-05-26 16:48:25 +08002234 u64 base_addr = iterator.addr;
2235
2236 base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
2237 pseudo_gfn = base_addr >> PAGE_SHIFT;
Avi Kivity9f652d22008-12-25 14:54:25 +02002238 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
2239 iterator.level - 1,
2240 1, ACC_ALL, iterator.sptep);
2241 if (!sp) {
2242 pgprintk("nonpaging_map: ENOMEM\n");
2243 kvm_release_pfn_clean(pfn);
2244 return -ENOMEM;
2245 }
2246
Avi Kivityd555c332009-06-10 14:24:23 +03002247 __set_spte(iterator.sptep,
2248 __pa(sp->spt)
2249 | PT_PRESENT_MASK | PT_WRITABLE_MASK
Xiao Guangrong33f91ed2010-09-27 18:05:00 +08002250 | shadow_user_mask | shadow_x_mask
2251 | shadow_accessed_mask);
Avi Kivity9f652d22008-12-25 14:54:25 +02002252 }
2253 }
2254 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002255}
2256
Huang Ying77db5cb2010-10-08 16:24:15 +08002257static void kvm_send_hwpoison_signal(unsigned long address, struct task_struct *tsk)
Huang Yingbf998152010-05-31 14:28:19 +08002258{
Huang Ying77db5cb2010-10-08 16:24:15 +08002259 siginfo_t info;
Huang Yingbf998152010-05-31 14:28:19 +08002260
Huang Ying77db5cb2010-10-08 16:24:15 +08002261 info.si_signo = SIGBUS;
2262 info.si_errno = 0;
2263 info.si_code = BUS_MCEERR_AR;
2264 info.si_addr = (void __user *)address;
2265 info.si_addr_lsb = PAGE_SHIFT;
2266
2267 send_sig_info(SIGBUS, &info, tsk);
Huang Yingbf998152010-05-31 14:28:19 +08002268}
2269
2270static int kvm_handle_bad_page(struct kvm *kvm, gfn_t gfn, pfn_t pfn)
2271{
2272 kvm_release_pfn_clean(pfn);
2273 if (is_hwpoison_pfn(pfn)) {
Huang Ying77db5cb2010-10-08 16:24:15 +08002274 kvm_send_hwpoison_signal(gfn_to_hva(kvm, gfn), current);
Huang Yingbf998152010-05-31 14:28:19 +08002275 return 0;
Gleb Natapovedba23e2010-07-07 20:16:45 +03002276 } else if (is_fault_pfn(pfn))
2277 return -EFAULT;
2278
Huang Yingbf998152010-05-31 14:28:19 +08002279 return 1;
2280}
2281
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002282static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
2283{
2284 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002285 int level;
Anthony Liguori35149e22008-04-02 14:46:56 -05002286 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002287 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002288
Joerg Roedel852e3c12009-07-27 16:30:44 +02002289 level = mapping_level(vcpu, gfn);
2290
2291 /*
2292 * This path builds a PAE pagetable - so we can map 2mb pages at
2293 * maximum. Therefore check if the level is larger than that.
2294 */
2295 if (level > PT_DIRECTORY_LEVEL)
2296 level = PT_DIRECTORY_LEVEL;
2297
2298 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002299
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002300 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002301 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002302 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002303
Avi Kivityd196e342008-01-24 11:44:11 +02002304 /* mmio */
Huang Yingbf998152010-05-31 14:28:19 +08002305 if (is_error_pfn(pfn))
2306 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002307
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002308 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002309 if (mmu_notifier_retry(vcpu, mmu_seq))
2310 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02002311 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel852e3c12009-07-27 16:30:44 +02002312 r = __direct_map(vcpu, v, write, level, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002313 spin_unlock(&vcpu->kvm->mmu_lock);
2314
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002315
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002316 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002317
2318out_unlock:
2319 spin_unlock(&vcpu->kvm->mmu_lock);
2320 kvm_release_pfn_clean(pfn);
2321 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002322}
2323
2324
Avi Kivity17ac10a2007-01-05 16:36:40 -08002325static void mmu_free_roots(struct kvm_vcpu *vcpu)
2326{
2327 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002328 struct kvm_mmu_page *sp;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002329 LIST_HEAD(invalid_list);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002330
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002331 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002332 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002333 spin_lock(&vcpu->kvm->mmu_lock);
Joerg Roedel81407ca2010-09-10 17:31:00 +02002334 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL &&
2335 (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL ||
2336 vcpu->arch.mmu.direct_map)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002337 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002338
Avi Kivity4db35312007-11-21 15:28:32 +02002339 sp = page_header(root);
2340 --sp->root_count;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002341 if (!sp->root_count && sp->role.invalid) {
2342 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
2343 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
2344 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002345 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002346 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002347 return;
2348 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002349 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002350 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002351
Avi Kivity417726a2007-04-12 17:35:58 +03002352 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002353 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002354 sp = page_header(root);
2355 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002356 if (!sp->root_count && sp->role.invalid)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002357 kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
2358 &invalid_list);
Avi Kivity417726a2007-04-12 17:35:58 +03002359 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002360 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002361 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002362 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002363 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002364 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002365}
2366
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002367static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2368{
2369 int ret = 0;
2370
2371 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03002372 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002373 ret = 1;
2374 }
2375
2376 return ret;
2377}
2378
Joerg Roedel651dd372010-09-10 17:30:59 +02002379static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu)
2380{
2381 struct kvm_mmu_page *sp;
Avi Kivity7ebaf152010-10-03 18:51:39 +02002382 unsigned i;
Joerg Roedel651dd372010-09-10 17:30:59 +02002383
2384 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2385 spin_lock(&vcpu->kvm->mmu_lock);
2386 kvm_mmu_free_some_pages(vcpu);
2387 sp = kvm_mmu_get_page(vcpu, 0, 0, PT64_ROOT_LEVEL,
2388 1, ACC_ALL, NULL);
2389 ++sp->root_count;
2390 spin_unlock(&vcpu->kvm->mmu_lock);
2391 vcpu->arch.mmu.root_hpa = __pa(sp->spt);
2392 } else if (vcpu->arch.mmu.shadow_root_level == PT32E_ROOT_LEVEL) {
2393 for (i = 0; i < 4; ++i) {
2394 hpa_t root = vcpu->arch.mmu.pae_root[i];
2395
2396 ASSERT(!VALID_PAGE(root));
2397 spin_lock(&vcpu->kvm->mmu_lock);
2398 kvm_mmu_free_some_pages(vcpu);
Avi Kivity649497d2010-12-28 12:09:07 +02002399 sp = kvm_mmu_get_page(vcpu, i << (30 - PAGE_SHIFT),
2400 i << 30,
Joerg Roedel651dd372010-09-10 17:30:59 +02002401 PT32_ROOT_LEVEL, 1, ACC_ALL,
2402 NULL);
2403 root = __pa(sp->spt);
2404 ++sp->root_count;
2405 spin_unlock(&vcpu->kvm->mmu_lock);
2406 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Joerg Roedel651dd372010-09-10 17:30:59 +02002407 }
Xiao Guangrong62927572010-09-27 18:02:12 +08002408 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Joerg Roedel651dd372010-09-10 17:30:59 +02002409 } else
2410 BUG();
2411
2412 return 0;
2413}
2414
2415static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002416{
Avi Kivity4db35312007-11-21 15:28:32 +02002417 struct kvm_mmu_page *sp;
Joerg Roedel81407ca2010-09-10 17:31:00 +02002418 u64 pdptr, pm_mask;
2419 gfn_t root_gfn;
2420 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002421
Joerg Roedel5777ed32010-09-10 17:30:42 +02002422 root_gfn = vcpu->arch.mmu.get_cr3(vcpu) >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002423
Joerg Roedel651dd372010-09-10 17:30:59 +02002424 if (mmu_check_root(vcpu, root_gfn))
2425 return 1;
2426
2427 /*
2428 * Do we shadow a long mode page table? If so we need to
2429 * write-protect the guests page table root.
2430 */
2431 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002432 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002433
2434 ASSERT(!VALID_PAGE(root));
Joerg Roedel651dd372010-09-10 17:30:59 +02002435
Avi Kivity8facbbf2010-05-04 12:58:32 +03002436 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b62010-05-12 21:00:35 -03002437 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel651dd372010-09-10 17:30:59 +02002438 sp = kvm_mmu_get_page(vcpu, root_gfn, 0, PT64_ROOT_LEVEL,
2439 0, ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002440 root = __pa(sp->spt);
2441 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002442 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002443 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002444 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002445 }
Joerg Roedelf87f9282010-09-02 17:29:45 +02002446
Joerg Roedel651dd372010-09-10 17:30:59 +02002447 /*
2448 * We shadow a 32 bit page table. This may be a legacy 2-level
Joerg Roedel81407ca2010-09-10 17:31:00 +02002449 * or a PAE 3-level page table. In either case we need to be aware that
2450 * the shadow page table may be a PAE or a long mode page table.
Joerg Roedel651dd372010-09-10 17:30:59 +02002451 */
Joerg Roedel81407ca2010-09-10 17:31:00 +02002452 pm_mask = PT_PRESENT_MASK;
2453 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL)
2454 pm_mask |= PT_ACCESSED_MASK | PT_WRITABLE_MASK | PT_USER_MASK;
2455
Avi Kivity17ac10a2007-01-05 16:36:40 -08002456 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002457 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002458
2459 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002460 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Joerg Roedeld41d1892010-09-10 17:30:58 +02002461 pdptr = kvm_pdptr_read_mmu(vcpu, &vcpu->arch.mmu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002462 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002463 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002464 continue;
2465 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002466 root_gfn = pdptr >> PAGE_SHIFT;
Joerg Roedelf87f9282010-09-02 17:29:45 +02002467 if (mmu_check_root(vcpu, root_gfn))
2468 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002469 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002470 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b62010-05-12 21:00:35 -03002471 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02002472 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Joerg Roedel651dd372010-09-10 17:30:59 +02002473 PT32_ROOT_LEVEL, 0,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002474 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002475 root = __pa(sp->spt);
2476 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002477 spin_unlock(&vcpu->kvm->mmu_lock);
2478
Joerg Roedel81407ca2010-09-10 17:31:00 +02002479 vcpu->arch.mmu.pae_root[i] = root | pm_mask;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002480 }
Xiao Guangrong62927572010-09-27 18:02:12 +08002481 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Joerg Roedel81407ca2010-09-10 17:31:00 +02002482
2483 /*
2484 * If we shadow a 32 bit page table with a long mode page
2485 * table we enter this path.
2486 */
2487 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2488 if (vcpu->arch.mmu.lm_root == NULL) {
2489 /*
2490 * The additional page necessary for this is only
2491 * allocated on demand.
2492 */
2493
2494 u64 *lm_root;
2495
2496 lm_root = (void*)get_zeroed_page(GFP_KERNEL);
2497 if (lm_root == NULL)
2498 return 1;
2499
2500 lm_root[0] = __pa(vcpu->arch.mmu.pae_root) | pm_mask;
2501
2502 vcpu->arch.mmu.lm_root = lm_root;
2503 }
2504
2505 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.lm_root);
2506 }
2507
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002508 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002509}
2510
Joerg Roedel651dd372010-09-10 17:30:59 +02002511static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
2512{
2513 if (vcpu->arch.mmu.direct_map)
2514 return mmu_alloc_direct_roots(vcpu);
2515 else
2516 return mmu_alloc_shadow_roots(vcpu);
2517}
2518
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002519static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2520{
2521 int i;
2522 struct kvm_mmu_page *sp;
2523
Joerg Roedel81407ca2010-09-10 17:31:00 +02002524 if (vcpu->arch.mmu.direct_map)
2525 return;
2526
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002527 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2528 return;
Xiao Guangrong69030742010-09-27 18:09:29 +08002529
2530 trace_kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC);
Joerg Roedel81407ca2010-09-10 17:31:00 +02002531 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002532 hpa_t root = vcpu->arch.mmu.root_hpa;
2533 sp = page_header(root);
2534 mmu_sync_children(vcpu, sp);
2535 return;
2536 }
2537 for (i = 0; i < 4; ++i) {
2538 hpa_t root = vcpu->arch.mmu.pae_root[i];
2539
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002540 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002541 root &= PT64_BASE_ADDR_MASK;
2542 sp = page_header(root);
2543 mmu_sync_children(vcpu, sp);
2544 }
2545 }
Xiao Guangrong69030742010-09-27 18:09:29 +08002546 trace_kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002547}
2548
2549void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2550{
2551 spin_lock(&vcpu->kvm->mmu_lock);
2552 mmu_sync_roots(vcpu);
2553 spin_unlock(&vcpu->kvm->mmu_lock);
2554}
2555
Gleb Natapov1871c602010-02-10 14:21:32 +02002556static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
2557 u32 access, u32 *error)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002558{
Gleb Natapov1871c602010-02-10 14:21:32 +02002559 if (error)
2560 *error = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002561 return vaddr;
2562}
2563
Joerg Roedel6539e732010-09-10 17:30:50 +02002564static gpa_t nonpaging_gva_to_gpa_nested(struct kvm_vcpu *vcpu, gva_t vaddr,
2565 u32 access, u32 *error)
2566{
2567 if (error)
2568 *error = 0;
2569 return vcpu->arch.nested_mmu.translate_gpa(vcpu, vaddr, access);
2570}
2571
Avi Kivity6aa8b732006-12-10 02:21:36 -08002572static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Gleb Natapov56028d02010-10-17 18:13:42 +02002573 u32 error_code, bool no_apf)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002574{
Avi Kivitye8332402007-12-09 18:43:00 +02002575 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002576 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002577
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002578 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002579 r = mmu_topup_memory_caches(vcpu);
2580 if (r)
2581 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002582
Avi Kivity6aa8b732006-12-10 02:21:36 -08002583 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002584 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002585
Avi Kivitye8332402007-12-09 18:43:00 +02002586 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002587
Avi Kivitye8332402007-12-09 18:43:00 +02002588 return nonpaging_map(vcpu, gva & PAGE_MASK,
2589 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002590}
2591
Gleb Natapovaf585b92010-10-14 11:22:46 +02002592int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn)
2593{
2594 struct kvm_arch_async_pf arch;
Gleb Natapov7c907052010-10-14 11:22:53 +02002595 arch.token = (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id;
Gleb Natapovaf585b92010-10-14 11:22:46 +02002596 arch.gfn = gfn;
2597
2598 return kvm_setup_async_pf(vcpu, gva, gfn, &arch);
2599}
2600
2601static bool can_do_async_pf(struct kvm_vcpu *vcpu)
2602{
2603 if (unlikely(!irqchip_in_kernel(vcpu->kvm) ||
2604 kvm_event_needs_reinjection(vcpu)))
2605 return false;
2606
2607 return kvm_x86_ops->interrupt_allowed(vcpu);
2608}
2609
Gleb Natapov56028d02010-10-17 18:13:42 +02002610static bool try_async_pf(struct kvm_vcpu *vcpu, bool no_apf, gfn_t gfn,
2611 gva_t gva, pfn_t *pfn)
Gleb Natapovaf585b92010-10-14 11:22:46 +02002612{
2613 bool async;
2614
2615 *pfn = gfn_to_pfn_async(vcpu->kvm, gfn, &async);
2616
2617 if (!async)
2618 return false; /* *pfn has correct page already */
2619
2620 put_page(pfn_to_page(*pfn));
2621
Gleb Natapov56028d02010-10-17 18:13:42 +02002622 if (!no_apf && can_do_async_pf(vcpu)) {
Gleb Natapovaf585b92010-10-14 11:22:46 +02002623 trace_kvm_try_async_get_page(async, *pfn);
2624 if (kvm_find_async_pf_gfn(vcpu, gfn)) {
2625 trace_kvm_async_pf_doublefault(gva, gfn);
2626 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
2627 return true;
2628 } else if (kvm_arch_setup_async_pf(vcpu, gva, gfn))
2629 return true;
2630 }
2631
2632 *pfn = gfn_to_pfn(vcpu->kvm, gfn);
2633
2634 return false;
2635}
2636
Gleb Natapov56028d02010-10-17 18:13:42 +02002637static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, u32 error_code,
2638 bool no_apf)
Joerg Roedelfb72d162008-02-07 13:47:44 +01002639{
Anthony Liguori35149e22008-04-02 14:46:56 -05002640 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002641 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002642 int level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002643 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002644 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002645
2646 ASSERT(vcpu);
2647 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2648
2649 r = mmu_topup_memory_caches(vcpu);
2650 if (r)
2651 return r;
2652
Joerg Roedel852e3c12009-07-27 16:30:44 +02002653 level = mapping_level(vcpu, gfn);
2654
2655 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2656
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002657 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002658 smp_rmb();
Gleb Natapovaf585b92010-10-14 11:22:46 +02002659
Gleb Natapov56028d02010-10-17 18:13:42 +02002660 if (try_async_pf(vcpu, no_apf, gfn, gpa, &pfn))
Gleb Natapovaf585b92010-10-14 11:22:46 +02002661 return 0;
2662
2663 /* mmio */
Huang Yingbf998152010-05-31 14:28:19 +08002664 if (is_error_pfn(pfn))
2665 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002666 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002667 if (mmu_notifier_retry(vcpu, mmu_seq))
2668 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002669 kvm_mmu_free_some_pages(vcpu);
2670 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002671 level, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002672 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002673
2674 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002675
2676out_unlock:
2677 spin_unlock(&vcpu->kvm->mmu_lock);
2678 kvm_release_pfn_clean(pfn);
2679 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002680}
2681
Avi Kivity6aa8b732006-12-10 02:21:36 -08002682static void nonpaging_free(struct kvm_vcpu *vcpu)
2683{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002684 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002685}
2686
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002687static int nonpaging_init_context(struct kvm_vcpu *vcpu,
2688 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002689{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002690 context->new_cr3 = nonpaging_new_cr3;
2691 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002692 context->gva_to_gpa = nonpaging_gva_to_gpa;
2693 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002694 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002695 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002696 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002697 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002698 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002699 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002700 context->direct_map = true;
Joerg Roedel2d48a982010-09-10 17:31:01 +02002701 context->nx = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002702 return 0;
2703}
2704
Avi Kivityd835dfe2007-11-21 02:57:59 +02002705void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002706{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002707 ++vcpu->stat.tlb_flush;
Avi Kivitya8eeb042010-05-10 12:34:53 +03002708 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002709}
2710
2711static void paging_new_cr3(struct kvm_vcpu *vcpu)
2712{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002713 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002714 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002715}
2716
Joerg Roedel5777ed32010-09-10 17:30:42 +02002717static unsigned long get_cr3(struct kvm_vcpu *vcpu)
2718{
2719 return vcpu->arch.cr3;
2720}
2721
Joerg Roedel8df25a32010-09-10 17:30:46 +02002722static void inject_page_fault(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002723{
Joerg Roedel8df25a32010-09-10 17:30:46 +02002724 vcpu->arch.mmu.inject_page_fault(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002725}
2726
Avi Kivity6aa8b732006-12-10 02:21:36 -08002727static void paging_free(struct kvm_vcpu *vcpu)
2728{
2729 nonpaging_free(vcpu);
2730}
2731
Joerg Roedel3241f222010-09-10 17:30:45 +02002732static bool is_rsvd_bits_set(struct kvm_mmu *mmu, u64 gpte, int level)
Dong, Eddie82725b22009-03-30 16:21:08 +08002733{
2734 int bit7;
2735
2736 bit7 = (gpte >> 7) & 1;
Joerg Roedel3241f222010-09-10 17:30:45 +02002737 return (gpte & mmu->rsvd_bits_mask[bit7][level-1]) != 0;
Dong, Eddie82725b22009-03-30 16:21:08 +08002738}
2739
Avi Kivity6aa8b732006-12-10 02:21:36 -08002740#define PTTYPE 64
2741#include "paging_tmpl.h"
2742#undef PTTYPE
2743
2744#define PTTYPE 32
2745#include "paging_tmpl.h"
2746#undef PTTYPE
2747
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002748static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
2749 struct kvm_mmu *context,
2750 int level)
Dong, Eddie82725b22009-03-30 16:21:08 +08002751{
Dong, Eddie82725b22009-03-30 16:21:08 +08002752 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2753 u64 exb_bit_rsvd = 0;
2754
Joerg Roedel2d48a982010-09-10 17:31:01 +02002755 if (!context->nx)
Dong, Eddie82725b22009-03-30 16:21:08 +08002756 exb_bit_rsvd = rsvd_bits(63, 63);
2757 switch (level) {
2758 case PT32_ROOT_LEVEL:
2759 /* no rsvd bits for 2 level 4K page table entries */
2760 context->rsvd_bits_mask[0][1] = 0;
2761 context->rsvd_bits_mask[0][0] = 0;
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002762 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2763
2764 if (!is_pse(vcpu)) {
2765 context->rsvd_bits_mask[1][1] = 0;
2766 break;
2767 }
2768
Dong, Eddie82725b22009-03-30 16:21:08 +08002769 if (is_cpuid_PSE36())
2770 /* 36bits PSE 4MB page */
2771 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2772 else
2773 /* 32 bits PSE 4MB page */
2774 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08002775 break;
2776 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002777 context->rsvd_bits_mask[0][2] =
2778 rsvd_bits(maxphyaddr, 63) |
2779 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002780 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002781 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002782 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2783 rsvd_bits(maxphyaddr, 62); /* PTE */
2784 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2785 rsvd_bits(maxphyaddr, 62) |
2786 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002787 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002788 break;
2789 case PT64_ROOT_LEVEL:
2790 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2791 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2792 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2793 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2794 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002795 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002796 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2797 rsvd_bits(maxphyaddr, 51);
2798 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002799 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2800 rsvd_bits(maxphyaddr, 51) |
2801 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002802 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002803 rsvd_bits(maxphyaddr, 51) |
2804 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002805 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002806 break;
2807 }
2808}
2809
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002810static int paging64_init_context_common(struct kvm_vcpu *vcpu,
2811 struct kvm_mmu *context,
2812 int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002813{
Joerg Roedel2d48a982010-09-10 17:31:01 +02002814 context->nx = is_nx(vcpu);
2815
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002816 reset_rsvds_bits_mask(vcpu, context, level);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002817
2818 ASSERT(is_pae(vcpu));
2819 context->new_cr3 = paging_new_cr3;
2820 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002821 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002822 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002823 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002824 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002825 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002826 context->root_level = level;
2827 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002828 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002829 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002830 return 0;
2831}
2832
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002833static int paging64_init_context(struct kvm_vcpu *vcpu,
2834 struct kvm_mmu *context)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002835{
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002836 return paging64_init_context_common(vcpu, context, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002837}
2838
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002839static int paging32_init_context(struct kvm_vcpu *vcpu,
2840 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002841{
Joerg Roedel2d48a982010-09-10 17:31:01 +02002842 context->nx = false;
2843
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002844 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002845
2846 context->new_cr3 = paging_new_cr3;
2847 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002848 context->gva_to_gpa = paging32_gva_to_gpa;
2849 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002850 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002851 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002852 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002853 context->root_level = PT32_ROOT_LEVEL;
2854 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002855 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002856 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002857 return 0;
2858}
2859
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002860static int paging32E_init_context(struct kvm_vcpu *vcpu,
2861 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002862{
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002863 return paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002864}
2865
Joerg Roedelfb72d162008-02-07 13:47:44 +01002866static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2867{
Joerg Roedel14dfe852010-09-10 17:30:49 +02002868 struct kvm_mmu *context = vcpu->arch.walk_mmu;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002869
2870 context->new_cr3 = nonpaging_new_cr3;
2871 context->page_fault = tdp_page_fault;
2872 context->free = nonpaging_free;
2873 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002874 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002875 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002876 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002877 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002878 context->direct_map = true;
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02002879 context->set_cr3 = kvm_x86_ops->set_tdp_cr3;
Joerg Roedel5777ed32010-09-10 17:30:42 +02002880 context->get_cr3 = get_cr3;
Joerg Roedelcb659db2010-09-10 17:30:43 +02002881 context->inject_page_fault = kvm_inject_page_fault;
Joerg Roedel2d48a982010-09-10 17:31:01 +02002882 context->nx = is_nx(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002883
2884 if (!is_paging(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002885 context->nx = false;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002886 context->gva_to_gpa = nonpaging_gva_to_gpa;
2887 context->root_level = 0;
2888 } else if (is_long_mode(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002889 context->nx = is_nx(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002890 reset_rsvds_bits_mask(vcpu, context, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002891 context->gva_to_gpa = paging64_gva_to_gpa;
2892 context->root_level = PT64_ROOT_LEVEL;
2893 } else if (is_pae(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002894 context->nx = is_nx(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002895 reset_rsvds_bits_mask(vcpu, context, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002896 context->gva_to_gpa = paging64_gva_to_gpa;
2897 context->root_level = PT32E_ROOT_LEVEL;
2898 } else {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002899 context->nx = false;
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002900 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002901 context->gva_to_gpa = paging32_gva_to_gpa;
2902 context->root_level = PT32_ROOT_LEVEL;
2903 }
2904
2905 return 0;
2906}
2907
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002908int kvm_init_shadow_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002909{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002910 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002911 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002912 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002913
2914 if (!is_paging(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002915 r = nonpaging_init_context(vcpu, context);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002916 else if (is_long_mode(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002917 r = paging64_init_context(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002918 else if (is_pae(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002919 r = paging32E_init_context(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002920 else
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002921 r = paging32_init_context(vcpu, context);
Avi Kivitya770f6f2008-12-21 19:20:09 +02002922
Avi Kivity5b7e0102010-04-14 19:20:03 +03002923 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
Joerg Roedelf43addd2010-09-10 17:30:40 +02002924 vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002925
2926 return r;
2927}
2928EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu);
2929
2930static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
2931{
Joerg Roedel14dfe852010-09-10 17:30:49 +02002932 int r = kvm_init_shadow_mmu(vcpu, vcpu->arch.walk_mmu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002933
Joerg Roedel14dfe852010-09-10 17:30:49 +02002934 vcpu->arch.walk_mmu->set_cr3 = kvm_x86_ops->set_cr3;
2935 vcpu->arch.walk_mmu->get_cr3 = get_cr3;
2936 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
Avi Kivitya770f6f2008-12-21 19:20:09 +02002937
2938 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002939}
2940
Joerg Roedel02f59dc2010-09-10 17:30:54 +02002941static int init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
2942{
2943 struct kvm_mmu *g_context = &vcpu->arch.nested_mmu;
2944
2945 g_context->get_cr3 = get_cr3;
2946 g_context->inject_page_fault = kvm_inject_page_fault;
2947
2948 /*
2949 * Note that arch.mmu.gva_to_gpa translates l2_gva to l1_gpa. The
2950 * translation of l2_gpa to l1_gpa addresses is done using the
2951 * arch.nested_mmu.gva_to_gpa function. Basically the gva_to_gpa
2952 * functions between mmu and nested_mmu are swapped.
2953 */
2954 if (!is_paging(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002955 g_context->nx = false;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02002956 g_context->root_level = 0;
2957 g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested;
2958 } else if (is_long_mode(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002959 g_context->nx = is_nx(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02002960 reset_rsvds_bits_mask(vcpu, g_context, PT64_ROOT_LEVEL);
2961 g_context->root_level = PT64_ROOT_LEVEL;
2962 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
2963 } else if (is_pae(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002964 g_context->nx = is_nx(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02002965 reset_rsvds_bits_mask(vcpu, g_context, PT32E_ROOT_LEVEL);
2966 g_context->root_level = PT32E_ROOT_LEVEL;
2967 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
2968 } else {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002969 g_context->nx = false;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02002970 reset_rsvds_bits_mask(vcpu, g_context, PT32_ROOT_LEVEL);
2971 g_context->root_level = PT32_ROOT_LEVEL;
2972 g_context->gva_to_gpa = paging32_gva_to_gpa_nested;
2973 }
2974
2975 return 0;
2976}
2977
Joerg Roedelfb72d162008-02-07 13:47:44 +01002978static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2979{
Anthony Liguori35149e22008-04-02 14:46:56 -05002980 vcpu->arch.update_pte.pfn = bad_pfn;
2981
Joerg Roedel02f59dc2010-09-10 17:30:54 +02002982 if (mmu_is_nested(vcpu))
2983 return init_kvm_nested_mmu(vcpu);
2984 else if (tdp_enabled)
Joerg Roedelfb72d162008-02-07 13:47:44 +01002985 return init_kvm_tdp_mmu(vcpu);
2986 else
2987 return init_kvm_softmmu(vcpu);
2988}
2989
Avi Kivity6aa8b732006-12-10 02:21:36 -08002990static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2991{
2992 ASSERT(vcpu);
Sheng Yang62ad0752010-05-12 16:40:41 +08002993 if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
2994 /* mmu.free() should set root_hpa = INVALID_PAGE */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002995 vcpu->arch.mmu.free(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002996}
2997
2998int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2999{
Avi Kivity17c3ba92007-06-04 15:58:30 +03003000 destroy_kvm_mmu(vcpu);
3001 return init_kvm_mmu(vcpu);
3002}
Eddie Dong8668a3c2007-10-10 14:26:45 +08003003EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03003004
3005int kvm_mmu_load(struct kvm_vcpu *vcpu)
3006{
Avi Kivity714b93d2007-01-05 16:36:53 -08003007 int r;
3008
Avi Kivitye2dec932007-01-05 16:36:54 -08003009 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03003010 if (r)
3011 goto out;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003012 r = mmu_alloc_roots(vcpu);
Avi Kivity8facbbf2010-05-04 12:58:32 +03003013 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003014 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003015 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003016 if (r)
3017 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08003018 /* set_cr3() should ensure TLB has been flushed */
Joerg Roedelf43addd2010-09-10 17:30:40 +02003019 vcpu->arch.mmu.set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08003020out:
3021 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003022}
Avi Kivity17c3ba92007-06-04 15:58:30 +03003023EXPORT_SYMBOL_GPL(kvm_mmu_load);
3024
3025void kvm_mmu_unload(struct kvm_vcpu *vcpu)
3026{
3027 mmu_free_roots(vcpu);
3028}
Joerg Roedel4b161842010-09-10 17:31:03 +02003029EXPORT_SYMBOL_GPL(kvm_mmu_unload);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003030
Avi Kivity09072da2007-05-01 14:16:52 +03003031static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02003032 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02003033 u64 *spte)
3034{
3035 u64 pte;
3036 struct kvm_mmu_page *child;
3037
3038 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02003039 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03003040 if (is_last_spte(pte, sp->role.level))
Avi Kivitybe38d272010-06-06 14:31:27 +03003041 drop_spte(vcpu->kvm, spte, shadow_trap_nonpresent_pte);
Avi Kivityac1b7142007-03-08 17:13:32 +02003042 else {
3043 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03003044 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02003045 }
3046 }
Avi Kivityd555c332009-06-10 14:24:23 +03003047 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03003048 if (is_large_pte(pte))
3049 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02003050}
3051
Avi Kivity00284252007-05-01 16:53:31 +03003052static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02003053 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03003054 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02003055 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03003056{
Marcelo Tosatti30945382008-06-11 20:32:40 -03003057 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02003058 ++vcpu->kvm->stat.mmu_pde_zapped;
3059 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03003060 }
Avi Kivity00284252007-05-01 16:53:31 +03003061
Joerg Roedel3241f222010-09-10 17:30:45 +02003062 if (is_rsvd_bits_set(&vcpu->arch.mmu, *(u64 *)new, PT_PAGE_TABLE_LEVEL))
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003063 return;
3064
Avi Kivity4cee5762007-11-18 16:37:07 +02003065 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity5b7e0102010-04-14 19:20:03 +03003066 if (!sp->role.cr4_pae)
Dong, Eddie489f1d62008-01-07 11:14:20 +02003067 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03003068 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02003069 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03003070}
3071
Avi Kivity79539ce2007-11-21 02:06:21 +02003072static bool need_remote_flush(u64 old, u64 new)
3073{
3074 if (!is_shadow_present_pte(old))
3075 return false;
3076 if (!is_shadow_present_pte(new))
3077 return true;
3078 if ((old ^ new) & PT64_BASE_ADDR_MASK)
3079 return true;
3080 old ^= PT64_NX_MASK;
3081 new ^= PT64_NX_MASK;
3082 return (old & ~new & PT64_PERM_MASK) != 0;
3083}
3084
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003085static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page,
3086 bool remote_flush, bool local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003087{
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003088 if (zap_page)
3089 return;
3090
3091 if (remote_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003092 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003093 else if (local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003094 kvm_mmu_flush_tlb(vcpu);
3095}
3096
Avi Kivity12b7d282007-09-23 14:10:49 +02003097static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
3098{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003099 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02003100
Sheng Yang7b523452008-04-25 21:13:50 +08003101 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02003102}
3103
Avi Kivityd7824ff2007-12-30 12:29:05 +02003104static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity72016f32010-03-15 13:59:53 +02003105 u64 gpte)
Avi Kivityd7824ff2007-12-30 12:29:05 +02003106{
3107 gfn_t gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05003108 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02003109
Avi Kivity43a37952009-06-10 14:12:05 +03003110 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02003111 return;
3112 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02003113
Andrea Arcangelie930bff2008-07-25 16:24:52 +02003114 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03003115 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05003116 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003117
Anthony Liguori35149e22008-04-02 14:46:56 -05003118 if (is_error_pfn(pfn)) {
3119 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02003120 return;
3121 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02003122 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05003123 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02003124}
3125
Avi Kivity1b7fcd32008-05-15 13:51:35 +03003126static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
3127{
3128 u64 *spte = vcpu->arch.last_pte_updated;
3129
3130 if (spte
3131 && vcpu->arch.last_pte_gfn == gfn
3132 && shadow_accessed_mask
3133 && !(*spte & shadow_accessed_mask)
3134 && is_shadow_present_pte(*spte))
3135 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
3136}
3137
Avi Kivity09072da2007-05-01 14:16:52 +03003138void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003139 const u8 *new, int bytes,
3140 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08003141{
Avi Kivity9b7a0322007-01-05 16:36:45 -08003142 gfn_t gfn = gpa >> PAGE_SHIFT;
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003143 union kvm_mmu_page_role mask = { .word = 0 };
Avi Kivity4db35312007-11-21 15:28:32 +02003144 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003145 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003146 LIST_HEAD(invalid_list);
Dong, Eddie489f1d62008-01-07 11:14:20 +02003147 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003148 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003149 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003150 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003151 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003152 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03003153 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003154 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08003155 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02003156 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02003157 int r;
Avi Kivity08e850c2010-03-15 13:59:57 +02003158 int invlpg_counter;
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003159 bool remote_flush, local_flush, zap_page;
3160
3161 zap_page = remote_flush = local_flush = false;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003162
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003163 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivity72016f32010-03-15 13:59:53 +02003164
Avi Kivity08e850c2010-03-15 13:59:57 +02003165 invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
3166
3167 /*
3168 * Assume that the pte write on a page table of the same type
3169 * as the current vcpu paging mode. This is nearly always true
3170 * (might be false while changing modes). Note it is verified later
3171 * by update_pte().
3172 */
3173 if ((is_pae(vcpu) && bytes == 4) || !new) {
3174 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
3175 if (is_pae(vcpu)) {
3176 gpa &= ~(gpa_t)7;
3177 bytes = 8;
3178 }
3179 r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
3180 if (r)
3181 gentry = 0;
3182 new = (const u8 *)&gentry;
3183 }
3184
Avi Kivity72016f32010-03-15 13:59:53 +02003185 switch (bytes) {
3186 case 4:
3187 gentry = *(const u32 *)new;
3188 break;
3189 case 8:
3190 gentry = *(const u64 *)new;
3191 break;
3192 default:
3193 gentry = 0;
3194 break;
3195 }
3196
Avi Kivity72016f32010-03-15 13:59:53 +02003197 mmu_guess_page_from_pte_write(vcpu, gpa, gentry);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003198 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity08e850c2010-03-15 13:59:57 +02003199 if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
3200 gentry = 0;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03003201 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02003202 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02003203 ++vcpu->kvm->stat.mmu_pte_write;
Xiao Guangrong8b1fe172010-08-30 18:22:53 +08003204 trace_kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE);
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003205 if (guest_initiated) {
3206 if (gfn == vcpu->arch.last_pt_write_gfn
3207 && !last_updated_pte_accessed(vcpu)) {
3208 ++vcpu->arch.last_pt_write_count;
3209 if (vcpu->arch.last_pt_write_count >= 3)
3210 flooded = 1;
3211 } else {
3212 vcpu->arch.last_pt_write_gfn = gfn;
3213 vcpu->arch.last_pt_write_count = 1;
3214 vcpu->arch.last_pte_updated = NULL;
3215 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08003216 }
Xiao Guangrong3246af02010-04-16 16:35:54 +08003217
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003218 mask.cr0_wp = mask.cr4_pae = mask.nxe = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003219 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node) {
Avi Kivity5b7e0102010-04-14 19:20:03 +03003220 pte_size = sp->role.cr4_pae ? 8 : 4;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003221 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03003222 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08003223 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003224 /*
3225 * Misaligned accesses are too much trouble to fix
3226 * up; also, they usually indicate a page is not used
3227 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08003228 *
3229 * If we're seeing too many writes to a page,
3230 * it may no longer be a page table, or we may be
3231 * forking, in which case it is better to unmap the
3232 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003233 */
3234 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02003235 gpa, bytes, sp->role.word);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003236 zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003237 &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02003238 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003239 continue;
3240 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08003241 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02003242 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02003243 npte = 1;
Avi Kivity5b7e0102010-04-14 19:20:03 +03003244 if (!sp->role.cr4_pae) {
Avi Kivityac1b7142007-03-08 17:13:32 +02003245 page_offset <<= 1; /* 32->64 */
3246 /*
3247 * A 32-bit pde maps 4MB while the shadow pdes map
3248 * only 2MB. So we need to double the offset again
3249 * and zap two pdes instead of one.
3250 */
3251 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03003252 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02003253 page_offset <<= 1;
3254 npte = 2;
3255 }
Avi Kivityfce06572007-05-01 16:44:05 +03003256 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003257 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02003258 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03003259 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003260 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003261 local_flush = true;
Avi Kivity4db35312007-11-21 15:28:32 +02003262 spte = &sp->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02003263 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02003264 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02003265 mmu_pte_write_zap_pte(vcpu, sp, spte);
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003266 if (gentry &&
3267 !((sp->role.word ^ vcpu->arch.mmu.base_role.word)
3268 & mask.word))
Avi Kivity72016f32010-03-15 13:59:53 +02003269 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003270 if (!remote_flush && need_remote_flush(entry, *spte))
3271 remote_flush = true;
Avi Kivityac1b7142007-03-08 17:13:32 +02003272 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003273 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08003274 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003275 mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003276 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong8b1fe172010-08-30 18:22:53 +08003277 trace_kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003278 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05003279 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
3280 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
3281 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02003282 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08003283}
3284
Avi Kivitya4360362007-01-05 16:36:45 -08003285int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
3286{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003287 gpa_t gpa;
3288 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08003289
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02003290 if (vcpu->arch.mmu.direct_map)
Avi Kivity60f24782009-08-27 13:37:06 +03003291 return 0;
3292
Gleb Natapov1871c602010-02-10 14:21:32 +02003293 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003294
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003295 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003296 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003297 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003298 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08003299}
Avi Kivity577bdc42008-07-19 08:57:05 +03003300EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08003301
Avi Kivity22d95b12007-09-14 20:26:06 +03003302void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08003303{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003304 LIST_HEAD(invalid_list);
Xiao Guangrong103ad252010-06-04 21:54:38 +08003305
Dave Hansene0df7b92010-08-19 18:11:05 -07003306 while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES &&
Izik Eidus3b80fff2009-07-28 15:26:58 -03003307 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02003308 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08003309
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003310 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02003311 struct kvm_mmu_page, link);
Dave Hansene0df7b92010-08-19 18:11:05 -07003312 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
Xiaotian Feng80b63fa2010-08-24 10:31:07 +08003313 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02003314 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08003315 }
3316}
Avi Kivityebeace82007-01-05 16:36:47 -08003317
Avi Kivity30677142007-10-28 18:48:59 +02003318int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
3319{
3320 int r;
3321 enum emulation_result er;
3322
Gleb Natapov56028d02010-10-17 18:13:42 +02003323 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code, false);
Avi Kivity30677142007-10-28 18:48:59 +02003324 if (r < 0)
3325 goto out;
3326
3327 if (!r) {
3328 r = 1;
3329 goto out;
3330 }
3331
Avi Kivityb733bfb2007-10-28 18:52:05 +02003332 r = mmu_topup_memory_caches(vcpu);
3333 if (r)
3334 goto out;
3335
Avi Kivity851ba692009-08-24 11:10:17 +03003336 er = emulate_instruction(vcpu, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02003337
3338 switch (er) {
3339 case EMULATE_DONE:
3340 return 1;
3341 case EMULATE_DO_MMIO:
3342 ++vcpu->stat.mmio_exits;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03003343 /* fall through */
Avi Kivity30677142007-10-28 18:48:59 +02003344 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03003345 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02003346 default:
3347 BUG();
3348 }
3349out:
Avi Kivity30677142007-10-28 18:48:59 +02003350 return r;
3351}
3352EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
3353
Marcelo Tosattia7052892008-09-23 13:18:35 -03003354void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
3355{
Marcelo Tosattia7052892008-09-23 13:18:35 -03003356 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003357 kvm_mmu_flush_tlb(vcpu);
3358 ++vcpu->stat.invlpg;
3359}
3360EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
3361
Joerg Roedel18552672008-02-07 13:47:41 +01003362void kvm_enable_tdp(void)
3363{
3364 tdp_enabled = true;
3365}
3366EXPORT_SYMBOL_GPL(kvm_enable_tdp);
3367
Joerg Roedel5f4cb662008-07-14 20:36:36 +02003368void kvm_disable_tdp(void)
3369{
3370 tdp_enabled = false;
3371}
3372EXPORT_SYMBOL_GPL(kvm_disable_tdp);
3373
Avi Kivity6aa8b732006-12-10 02:21:36 -08003374static void free_mmu_pages(struct kvm_vcpu *vcpu)
3375{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003376 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Joerg Roedel81407ca2010-09-10 17:31:00 +02003377 if (vcpu->arch.mmu.lm_root != NULL)
3378 free_page((unsigned long)vcpu->arch.mmu.lm_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003379}
3380
3381static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
3382{
Avi Kivity17ac10a2007-01-05 16:36:40 -08003383 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003384 int i;
3385
3386 ASSERT(vcpu);
3387
Avi Kivity17ac10a2007-01-05 16:36:40 -08003388 /*
3389 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
3390 * Therefore we need to allocate shadow page tables in the first
3391 * 4GB of memory, which happens to fit the DMA32 zone.
3392 */
3393 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
3394 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08003395 return -ENOMEM;
3396
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003397 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003398 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003399 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003400
Avi Kivity6aa8b732006-12-10 02:21:36 -08003401 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003402}
3403
Ingo Molnar8018c272006-12-29 16:50:01 -08003404int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003405{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003406 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003407 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003408
Ingo Molnar8018c272006-12-29 16:50:01 -08003409 return alloc_mmu_pages(vcpu);
3410}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003411
Ingo Molnar8018c272006-12-29 16:50:01 -08003412int kvm_mmu_setup(struct kvm_vcpu *vcpu)
3413{
3414 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003415 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08003416
Ingo Molnar8018c272006-12-29 16:50:01 -08003417 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003418}
3419
Avi Kivity90cb0522007-07-17 13:04:56 +03003420void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003421{
Avi Kivity4db35312007-11-21 15:28:32 +02003422 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003423
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003424 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003425 int i;
3426 u64 *pt;
3427
Sheng Yang291f26b2008-10-16 17:30:57 +08003428 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003429 continue;
3430
Avi Kivity4db35312007-11-21 15:28:32 +02003431 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003432 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
3433 /* avoid RMW */
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003434 if (is_writable_pte(pt[i]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003435 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003436 }
Avi Kivity171d5952008-08-27 16:40:51 +03003437 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003438}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003439
Avi Kivity90cb0522007-07-17 13:04:56 +03003440void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03003441{
Avi Kivity4db35312007-11-21 15:28:32 +02003442 struct kvm_mmu_page *sp, *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003443 LIST_HEAD(invalid_list);
Dor Laore0fa8262007-03-30 13:06:33 +03003444
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003445 spin_lock(&kvm->mmu_lock);
Xiao Guangrong3246af02010-04-16 16:35:54 +08003446restart:
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003447 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003448 if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list))
Xiao Guangrong3246af02010-04-16 16:35:54 +08003449 goto restart;
3450
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003451 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003452 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03003453}
3454
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003455static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm,
3456 struct list_head *invalid_list)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003457{
3458 struct kvm_mmu_page *page;
3459
3460 page = container_of(kvm->arch.active_mmu_pages.prev,
3461 struct kvm_mmu_page, link);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003462 return kvm_mmu_prepare_zap_page(kvm, page, invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003463}
3464
Dave Chinner7f8275d2010-07-19 14:56:17 +10003465static int mmu_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003466{
3467 struct kvm *kvm;
3468 struct kvm *kvm_freed = NULL;
Dave Hansen45221ab2010-08-19 18:11:37 -07003469
3470 if (nr_to_scan == 0)
3471 goto out;
Izik Eidus3ee16c82008-03-30 15:17:21 +03003472
3473 spin_lock(&kvm_lock);
3474
3475 list_for_each_entry(kvm, &vm_list, vm_list) {
Dave Hansen45221ab2010-08-19 18:11:37 -07003476 int idx, freed_pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003477 LIST_HEAD(invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003478
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003479 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003480 spin_lock(&kvm->mmu_lock);
Dave Hansen45221ab2010-08-19 18:11:37 -07003481 if (!kvm_freed && nr_to_scan > 0 &&
3482 kvm->arch.n_used_mmu_pages > 0) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003483 freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm,
3484 &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003485 kvm_freed = kvm;
3486 }
3487 nr_to_scan--;
3488
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003489 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003490 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003491 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003492 }
3493 if (kvm_freed)
3494 list_move_tail(&kvm_freed->vm_list, &vm_list);
3495
3496 spin_unlock(&kvm_lock);
3497
Dave Hansen45221ab2010-08-19 18:11:37 -07003498out:
3499 return percpu_counter_read_positive(&kvm_total_used_mmu_pages);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003500}
3501
3502static struct shrinker mmu_shrinker = {
3503 .shrink = mmu_shrink,
3504 .seeks = DEFAULT_SEEKS * 10,
3505};
3506
Ingo Molnar2ddfd202008-05-22 10:37:48 +02003507static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03003508{
3509 if (pte_chain_cache)
3510 kmem_cache_destroy(pte_chain_cache);
3511 if (rmap_desc_cache)
3512 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003513 if (mmu_page_header_cache)
3514 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003515}
3516
Izik Eidus3ee16c82008-03-30 15:17:21 +03003517void kvm_mmu_module_exit(void)
3518{
3519 mmu_destroy_caches();
Wei Yongjun45bf21a2010-08-23 16:13:15 +08003520 percpu_counter_destroy(&kvm_total_used_mmu_pages);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003521 unregister_shrinker(&mmu_shrinker);
3522}
3523
Avi Kivityb5a33a72007-04-15 16:31:09 +03003524int kvm_mmu_module_init(void)
3525{
3526 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
3527 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09003528 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003529 if (!pte_chain_cache)
3530 goto nomem;
3531 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
3532 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09003533 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003534 if (!rmap_desc_cache)
3535 goto nomem;
3536
Avi Kivityd3d25b02007-05-30 12:34:53 +03003537 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
3538 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09003539 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003540 if (!mmu_page_header_cache)
3541 goto nomem;
3542
Wei Yongjun45bf21a2010-08-23 16:13:15 +08003543 if (percpu_counter_init(&kvm_total_used_mmu_pages, 0))
3544 goto nomem;
3545
Izik Eidus3ee16c82008-03-30 15:17:21 +03003546 register_shrinker(&mmu_shrinker);
3547
Avi Kivityb5a33a72007-04-15 16:31:09 +03003548 return 0;
3549
3550nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03003551 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003552 return -ENOMEM;
3553}
3554
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003555/*
3556 * Caculate mmu pages needed for kvm.
3557 */
3558unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3559{
3560 int i;
3561 unsigned int nr_mmu_pages;
3562 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003563 struct kvm_memslots *slots;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003564
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003565 slots = kvm_memslots(kvm);
3566
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003567 for (i = 0; i < slots->nmemslots; i++)
3568 nr_pages += slots->memslots[i].npages;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003569
3570 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3571 nr_mmu_pages = max(nr_mmu_pages,
3572 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3573
3574 return nr_mmu_pages;
3575}
3576
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003577static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3578 unsigned len)
3579{
3580 if (len > buffer->len)
3581 return NULL;
3582 return buffer->ptr;
3583}
3584
3585static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3586 unsigned len)
3587{
3588 void *ret;
3589
3590 ret = pv_mmu_peek_buffer(buffer, len);
3591 if (!ret)
3592 return ret;
3593 buffer->ptr += len;
3594 buffer->len -= len;
3595 buffer->processed += len;
3596 return ret;
3597}
3598
3599static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3600 gpa_t addr, gpa_t value)
3601{
3602 int bytes = 8;
3603 int r;
3604
3605 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3606 bytes = 4;
3607
3608 r = mmu_topup_memory_caches(vcpu);
3609 if (r)
3610 return r;
3611
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003612 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003613 return -EFAULT;
3614
3615 return 1;
3616}
3617
3618static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3619{
Avi Kivity23902182010-06-10 17:02:16 +03003620 (void)kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003621 return 1;
3622}
3623
3624static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3625{
3626 spin_lock(&vcpu->kvm->mmu_lock);
3627 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3628 spin_unlock(&vcpu->kvm->mmu_lock);
3629 return 1;
3630}
3631
3632static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3633 struct kvm_pv_mmu_op_buffer *buffer)
3634{
3635 struct kvm_mmu_op_header *header;
3636
3637 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3638 if (!header)
3639 return 0;
3640 switch (header->op) {
3641 case KVM_MMU_OP_WRITE_PTE: {
3642 struct kvm_mmu_op_write_pte *wpte;
3643
3644 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3645 if (!wpte)
3646 return 0;
3647 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3648 wpte->pte_val);
3649 }
3650 case KVM_MMU_OP_FLUSH_TLB: {
3651 struct kvm_mmu_op_flush_tlb *ftlb;
3652
3653 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3654 if (!ftlb)
3655 return 0;
3656 return kvm_pv_mmu_flush_tlb(vcpu);
3657 }
3658 case KVM_MMU_OP_RELEASE_PT: {
3659 struct kvm_mmu_op_release_pt *rpt;
3660
3661 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3662 if (!rpt)
3663 return 0;
3664 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3665 }
3666 default: return 0;
3667 }
3668}
3669
3670int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3671 gpa_t addr, unsigned long *ret)
3672{
3673 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003674 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003675
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003676 buffer->ptr = buffer->buf;
3677 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3678 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003679
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003680 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003681 if (r)
3682 goto out;
3683
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003684 while (buffer->len) {
3685 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003686 if (r < 0)
3687 goto out;
3688 if (r == 0)
3689 break;
3690 }
3691
3692 r = 1;
3693out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003694 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003695 return r;
3696}
3697
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003698int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3699{
3700 struct kvm_shadow_walk_iterator iterator;
3701 int nr_sptes = 0;
3702
3703 spin_lock(&vcpu->kvm->mmu_lock);
3704 for_each_shadow_entry(vcpu, addr, iterator) {
3705 sptes[iterator.level-1] = *iterator.sptep;
3706 nr_sptes++;
3707 if (!is_shadow_present_pte(*iterator.sptep))
3708 break;
3709 }
3710 spin_unlock(&vcpu->kvm->mmu_lock);
3711
3712 return nr_sptes;
3713}
3714EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3715
Xiao Guangrong8b1fe172010-08-30 18:22:53 +08003716#ifdef CONFIG_KVM_MMU_AUDIT
Xiao Guangrong2f4f3372010-08-30 18:24:10 +08003717#include "mmu_audit.c"
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08003718#else
3719static void mmu_audit_disable(void) { }
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003720#endif
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08003721
3722void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
3723{
3724 ASSERT(vcpu);
3725
3726 destroy_kvm_mmu(vcpu);
3727 free_mmu_pages(vcpu);
3728 mmu_free_memory_caches(vcpu);
3729 mmu_audit_disable();
3730}