blob: 475a1225f6ec90ee25c369a4040ab91a93b296fc [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_nx_mask;
200static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
201static u64 __read_mostly shadow_user_mask;
202static u64 __read_mostly shadow_accessed_mask;
203static u64 __read_mostly shadow_dirty_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200204
Dong, Eddie82725b22009-03-30 16:21:08 +0800205static inline u64 rsvd_bits(int s, int e)
206{
207 return ((1ULL << (e - s + 1)) - 1) << s;
208}
209
Avi Kivityc7addb92007-09-16 18:58:32 +0200210void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
211{
212 shadow_trap_nonpresent_pte = trap_pte;
213 shadow_notrap_nonpresent_pte = notrap_pte;
214}
215EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
216
Sheng Yang7b523452008-04-25 21:13:50 +0800217void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang4b12f0d2009-04-27 20:35:42 +0800218 u64 dirty_mask, u64 nx_mask, u64 x_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800219{
220 shadow_user_mask = user_mask;
221 shadow_accessed_mask = accessed_mask;
222 shadow_dirty_mask = dirty_mask;
223 shadow_nx_mask = nx_mask;
224 shadow_x_mask = x_mask;
225}
226EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
227
Avi Kivity3dbe1412010-05-12 11:48:18 +0300228static bool is_write_protection(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800229{
Avi Kivity4d4ec082009-12-29 18:07:30 +0200230 return kvm_read_cr0_bits(vcpu, X86_CR0_WP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800231}
232
233static int is_cpuid_PSE36(void)
234{
235 return 1;
236}
237
Avi Kivity73b10872007-01-26 00:56:41 -0800238static int is_nx(struct kvm_vcpu *vcpu)
239{
Avi Kivityf6801df2010-01-21 15:31:50 +0200240 return vcpu->arch.efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800241}
242
Avi Kivityc7addb92007-09-16 18:58:32 +0200243static int is_shadow_present_pte(u64 pte)
244{
Avi Kivityc7addb92007-09-16 18:58:32 +0200245 return pte != shadow_trap_nonpresent_pte
246 && pte != shadow_notrap_nonpresent_pte;
247}
248
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300249static int is_large_pte(u64 pte)
250{
251 return pte & PT_PAGE_SIZE_MASK;
252}
253
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900254static int is_writable_pte(unsigned long pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800255{
256 return pte & PT_WRITABLE_MASK;
257}
258
Avi Kivity43a37952009-06-10 14:12:05 +0300259static int is_dirty_gpte(unsigned long pte)
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200260{
Avi Kivity439e2182009-06-10 12:56:54 +0300261 return pte & PT_DIRTY_MASK;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200262}
263
Avi Kivity43a37952009-06-10 14:12:05 +0300264static int is_rmap_spte(u64 pte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800265{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200266 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800267}
268
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300269static int is_last_spte(u64 pte, int level)
270{
271 if (level == PT_PAGE_TABLE_LEVEL)
272 return 1;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200273 if (is_large_pte(pte))
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300274 return 1;
275 return 0;
276}
277
Anthony Liguori35149e22008-04-02 14:46:56 -0500278static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200279{
Anthony Liguori35149e22008-04-02 14:46:56 -0500280 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200281}
282
Avi Kivityda928522007-11-21 13:54:47 +0200283static gfn_t pse36_gfn_delta(u32 gpte)
284{
285 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
286
287 return (gpte & PT32_DIR_PSE36_MASK) << shift;
288}
289
Avi Kivityd555c332009-06-10 14:24:23 +0300290static void __set_spte(u64 *sptep, u64 spte)
Avi Kivitye663ee62007-05-31 15:46:04 +0300291{
H. Peter Anvin7645e432010-08-06 12:18:11 -0700292 set_64bit(sptep, spte);
Avi Kivitye663ee62007-05-31 15:46:04 +0300293}
294
Avi Kivitya9221dd2010-06-06 14:48:06 +0300295static u64 __xchg_spte(u64 *sptep, u64 new_spte)
296{
297#ifdef CONFIG_X86_64
298 return xchg(sptep, new_spte);
299#else
300 u64 old_spte;
301
302 do {
303 old_spte = *sptep;
304 } while (cmpxchg64(sptep, old_spte, new_spte) != old_spte);
305
306 return old_spte;
307#endif
308}
309
Xiao Guangrong8672b722010-08-02 16:14:04 +0800310static bool spte_has_volatile_bits(u64 spte)
311{
312 if (!shadow_accessed_mask)
313 return false;
314
315 if (!is_shadow_present_pte(spte))
316 return false;
317
Xiao Guangrong41327792010-08-02 16:15:08 +0800318 if ((spte & shadow_accessed_mask) &&
319 (!is_writable_pte(spte) || (spte & shadow_dirty_mask)))
Xiao Guangrong8672b722010-08-02 16:14:04 +0800320 return false;
321
322 return true;
323}
324
Xiao Guangrong41327792010-08-02 16:15:08 +0800325static bool spte_is_bit_cleared(u64 old_spte, u64 new_spte, u64 bit_mask)
326{
327 return (old_spte & bit_mask) && !(new_spte & bit_mask);
328}
329
Avi Kivityb79b93f2010-06-06 15:46:44 +0300330static void update_spte(u64 *sptep, u64 new_spte)
331{
Xiao Guangrong41327792010-08-02 16:15:08 +0800332 u64 mask, old_spte = *sptep;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300333
Xiao Guangrong41327792010-08-02 16:15:08 +0800334 WARN_ON(!is_rmap_spte(new_spte));
335
336 new_spte |= old_spte & shadow_dirty_mask;
337
338 mask = shadow_accessed_mask;
339 if (is_writable_pte(old_spte))
340 mask |= shadow_dirty_mask;
341
342 if (!spte_has_volatile_bits(old_spte) || (new_spte & mask) == mask)
Avi Kivityb79b93f2010-06-06 15:46:44 +0300343 __set_spte(sptep, new_spte);
Xiao Guangrong41327792010-08-02 16:15:08 +0800344 else
Avi Kivityb79b93f2010-06-06 15:46:44 +0300345 old_spte = __xchg_spte(sptep, new_spte);
Xiao Guangrong41327792010-08-02 16:15:08 +0800346
347 if (!shadow_accessed_mask)
348 return;
349
350 if (spte_is_bit_cleared(old_spte, new_spte, shadow_accessed_mask))
351 kvm_set_pfn_accessed(spte_to_pfn(old_spte));
352 if (spte_is_bit_cleared(old_spte, new_spte, shadow_dirty_mask))
353 kvm_set_pfn_dirty(spte_to_pfn(old_spte));
Avi Kivityb79b93f2010-06-06 15:46:44 +0300354}
355
Avi Kivitye2dec932007-01-05 16:36:54 -0800356static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300357 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800358{
359 void *obj;
360
361 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800362 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800363 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300364 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800365 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800366 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800367 cache->objects[cache->nobjs++] = obj;
368 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800369 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800370}
371
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800372static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc,
373 struct kmem_cache *cache)
Avi Kivity714b93d2007-01-05 16:36:53 -0800374{
375 while (mc->nobjs)
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800376 kmem_cache_free(cache, mc->objects[--mc->nobjs]);
Avi Kivity714b93d2007-01-05 16:36:53 -0800377}
378
Avi Kivityc1158e62007-07-20 08:18:27 +0300379static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300380 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300381{
382 struct page *page;
383
384 if (cache->nobjs >= min)
385 return 0;
386 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300387 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300388 if (!page)
389 return -ENOMEM;
Avi Kivityc1158e62007-07-20 08:18:27 +0300390 cache->objects[cache->nobjs++] = page_address(page);
391 }
392 return 0;
393}
394
395static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
396{
397 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300398 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300399}
400
Avi Kivity8c438502007-04-16 11:53:17 +0300401static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
402{
403 int r;
404
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800405 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300406 pte_chain_cache, 4);
407 if (r)
408 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800409 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Xiao Guangrong957ed9e2010-08-22 19:12:48 +0800410 rmap_desc_cache, 4 + PTE_PREFETCH_NUM);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300411 if (r)
412 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800413 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300414 if (r)
415 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800416 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300417 mmu_page_header_cache, 4);
418out:
Avi Kivity8c438502007-04-16 11:53:17 +0300419 return r;
420}
421
Avi Kivity714b93d2007-01-05 16:36:53 -0800422static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
423{
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800424 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache, pte_chain_cache);
425 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache, rmap_desc_cache);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800426 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800427 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache,
428 mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800429}
430
431static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
432 size_t size)
433{
434 void *p;
435
436 BUG_ON(!mc->nobjs);
437 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800438 return p;
439}
440
Avi Kivity714b93d2007-01-05 16:36:53 -0800441static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
442{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800443 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800444 sizeof(struct kvm_pte_chain));
445}
446
Avi Kivity90cb0522007-07-17 13:04:56 +0300447static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800448{
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800449 kmem_cache_free(pte_chain_cache, pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800450}
451
452static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
453{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800454 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800455 sizeof(struct kvm_rmap_desc));
456}
457
Avi Kivity90cb0522007-07-17 13:04:56 +0300458static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800459{
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800460 kmem_cache_free(rmap_desc_cache, rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800461}
462
Lai Jiangshan2032a932010-05-26 16:49:59 +0800463static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
464{
465 if (!sp->role.direct)
466 return sp->gfns[index];
467
468 return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS));
469}
470
471static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn)
472{
473 if (sp->role.direct)
474 BUG_ON(gfn != kvm_mmu_page_get_gfn(sp, index));
475 else
476 sp->gfns[index] = gfn;
477}
478
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800479/*
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900480 * Return the pointer to the large page information for a given gfn,
481 * handling slots that are not large page aligned.
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300482 */
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900483static struct kvm_lpage_info *lpage_info_slot(gfn_t gfn,
484 struct kvm_memory_slot *slot,
485 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300486{
487 unsigned long idx;
488
Joerg Roedel82855412010-07-01 16:00:11 +0200489 idx = (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
490 (slot->base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900491 return &slot->lpage_info[level - 2][idx];
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300492}
493
494static void account_shadowed(struct kvm *kvm, gfn_t gfn)
495{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200496 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900497 struct kvm_lpage_info *linfo;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200498 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300499
Avi Kivitya1f4d392010-06-21 11:44:20 +0300500 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200501 for (i = PT_DIRECTORY_LEVEL;
502 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900503 linfo = lpage_info_slot(gfn, slot, i);
504 linfo->write_count += 1;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200505 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300506}
507
508static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
509{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200510 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900511 struct kvm_lpage_info *linfo;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200512 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300513
Avi Kivitya1f4d392010-06-21 11:44:20 +0300514 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200515 for (i = PT_DIRECTORY_LEVEL;
516 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900517 linfo = lpage_info_slot(gfn, slot, i);
518 linfo->write_count -= 1;
519 WARN_ON(linfo->write_count < 0);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200520 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300521}
522
Joerg Roedeld25797b2009-07-27 16:30:43 +0200523static int has_wrprotected_page(struct kvm *kvm,
524 gfn_t gfn,
525 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300526{
Izik Eidus28430992008-10-03 17:40:32 +0300527 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900528 struct kvm_lpage_info *linfo;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300529
Avi Kivitya1f4d392010-06-21 11:44:20 +0300530 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300531 if (slot) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900532 linfo = lpage_info_slot(gfn, slot, level);
533 return linfo->write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300534 }
535
536 return 1;
537}
538
Joerg Roedeld25797b2009-07-27 16:30:43 +0200539static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300540{
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100541 unsigned long page_size;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200542 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300543
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100544 page_size = kvm_host_page_size(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300545
Joerg Roedeld25797b2009-07-27 16:30:43 +0200546 for (i = PT_PAGE_TABLE_LEVEL;
547 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
548 if (page_size >= KVM_HPAGE_SIZE(i))
549 ret = i;
550 else
551 break;
552 }
553
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300554 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300555}
556
Joerg Roedeld25797b2009-07-27 16:30:43 +0200557static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300558{
559 struct kvm_memory_slot *slot;
Sheng Yang878403b2010-01-05 19:02:29 +0800560 int host_level, level, max_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300561
562 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
563 if (slot && slot->dirty_bitmap)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200564 return PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300565
Joerg Roedeld25797b2009-07-27 16:30:43 +0200566 host_level = host_mapping_level(vcpu->kvm, large_gfn);
567
568 if (host_level == PT_PAGE_TABLE_LEVEL)
569 return host_level;
570
Sheng Yang878403b2010-01-05 19:02:29 +0800571 max_level = kvm_x86_ops->get_lpage_level() < host_level ?
572 kvm_x86_ops->get_lpage_level() : host_level;
573
574 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200575 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
576 break;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200577
578 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300579}
580
581/*
Izik Eidus290fc382007-09-27 14:11:22 +0200582 * Take gfn and return the reverse mapping to it.
Izik Eidus290fc382007-09-27 14:11:22 +0200583 */
584
Joerg Roedel44ad9942009-07-27 16:30:42 +0200585static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
Izik Eidus290fc382007-09-27 14:11:22 +0200586{
587 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900588 struct kvm_lpage_info *linfo;
Izik Eidus290fc382007-09-27 14:11:22 +0200589
590 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200591 if (likely(level == PT_PAGE_TABLE_LEVEL))
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300592 return &slot->rmap[gfn - slot->base_gfn];
593
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900594 linfo = lpage_info_slot(gfn, slot, level);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300595
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900596 return &linfo->rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200597}
598
599/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800600 * Reverse mapping data structures:
601 *
Izik Eidus290fc382007-09-27 14:11:22 +0200602 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
603 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800604 *
Izik Eidus290fc382007-09-27 14:11:22 +0200605 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
606 * containing more mappings.
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300607 *
608 * Returns the number of rmap entries before the spte was added or zero if
609 * the spte was not added.
610 *
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800611 */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200612static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800613{
Avi Kivity4db35312007-11-21 15:28:32 +0200614 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800615 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200616 unsigned long *rmapp;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300617 int i, count = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800618
Avi Kivity43a37952009-06-10 14:12:05 +0300619 if (!is_rmap_spte(*spte))
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300620 return count;
Avi Kivity4db35312007-11-21 15:28:32 +0200621 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800622 kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200623 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200624 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800625 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200626 *rmapp = (unsigned long)spte;
627 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800628 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800629 desc = mmu_alloc_rmap_desc(vcpu);
Avi Kivityd555c332009-06-10 14:24:23 +0300630 desc->sptes[0] = (u64 *)*rmapp;
631 desc->sptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200632 *rmapp = (unsigned long)desc | 1;
Hillf Dantoncb16a7b2010-09-18 08:41:02 +0800633 ++count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800634 } else {
635 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200636 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivityd555c332009-06-10 14:24:23 +0300637 while (desc->sptes[RMAP_EXT-1] && desc->more) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800638 desc = desc->more;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300639 count += RMAP_EXT;
640 }
Avi Kivityd555c332009-06-10 14:24:23 +0300641 if (desc->sptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800642 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800643 desc = desc->more;
644 }
Avi Kivityd555c332009-06-10 14:24:23 +0300645 for (i = 0; desc->sptes[i]; ++i)
Hillf Dantoncb16a7b2010-09-18 08:41:02 +0800646 ++count;
Avi Kivityd555c332009-06-10 14:24:23 +0300647 desc->sptes[i] = spte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800648 }
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300649 return count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800650}
651
Izik Eidus290fc382007-09-27 14:11:22 +0200652static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800653 struct kvm_rmap_desc *desc,
654 int i,
655 struct kvm_rmap_desc *prev_desc)
656{
657 int j;
658
Avi Kivityd555c332009-06-10 14:24:23 +0300659 for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800660 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300661 desc->sptes[i] = desc->sptes[j];
662 desc->sptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800663 if (j != 0)
664 return;
665 if (!prev_desc && !desc->more)
Avi Kivityd555c332009-06-10 14:24:23 +0300666 *rmapp = (unsigned long)desc->sptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800667 else
668 if (prev_desc)
669 prev_desc->more = desc->more;
670 else
Izik Eidus290fc382007-09-27 14:11:22 +0200671 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300672 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800673}
674
Izik Eidus290fc382007-09-27 14:11:22 +0200675static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800676{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800677 struct kvm_rmap_desc *desc;
678 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200679 struct kvm_mmu_page *sp;
Lai Jiangshan2032a932010-05-26 16:49:59 +0800680 gfn_t gfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200681 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800682 int i;
683
Avi Kivity4db35312007-11-21 15:28:32 +0200684 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800685 gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt);
686 rmapp = gfn_to_rmap(kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200687 if (!*rmapp) {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800688 printk(KERN_ERR "rmap_remove: %p 0->BUG\n", spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800689 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200690 } else if (!(*rmapp & 1)) {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800691 rmap_printk("rmap_remove: %p 1->0\n", spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200692 if ((u64 *)*rmapp != spte) {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800693 printk(KERN_ERR "rmap_remove: %p 1->BUG\n", spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800694 BUG();
695 }
Izik Eidus290fc382007-09-27 14:11:22 +0200696 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800697 } else {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800698 rmap_printk("rmap_remove: %p many->many\n", spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200699 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800700 prev_desc = NULL;
701 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300702 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
703 if (desc->sptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200704 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800705 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800706 prev_desc);
707 return;
708 }
709 prev_desc = desc;
710 desc = desc->more;
711 }
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800712 pr_err("rmap_remove: %p many->many\n", spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800713 BUG();
714 }
715}
716
Marcelo Tosattieb45fda2010-10-25 11:58:22 -0200717static int set_spte_track_bits(u64 *sptep, u64 new_spte)
Avi Kivitybe38d272010-06-06 14:31:27 +0300718{
Avi Kivityce061862010-06-06 14:38:12 +0300719 pfn_t pfn;
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800720 u64 old_spte = *sptep;
Avi Kivityce061862010-06-06 14:38:12 +0300721
Xiao Guangrong8672b722010-08-02 16:14:04 +0800722 if (!spte_has_volatile_bits(old_spte))
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800723 __set_spte(sptep, new_spte);
Xiao Guangrong8672b722010-08-02 16:14:04 +0800724 else
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800725 old_spte = __xchg_spte(sptep, new_spte);
726
Avi Kivitya9221dd2010-06-06 14:48:06 +0300727 if (!is_rmap_spte(old_spte))
Marcelo Tosattieb45fda2010-10-25 11:58:22 -0200728 return 0;
Xiao Guangrong8672b722010-08-02 16:14:04 +0800729
Avi Kivitya9221dd2010-06-06 14:48:06 +0300730 pfn = spte_to_pfn(old_spte);
Xiao Guangrongdaa3db62010-07-16 11:23:04 +0800731 if (!shadow_accessed_mask || old_spte & shadow_accessed_mask)
Avi Kivityce061862010-06-06 14:38:12 +0300732 kvm_set_pfn_accessed(pfn);
Xiao Guangrong41327792010-08-02 16:15:08 +0800733 if (!shadow_dirty_mask || (old_spte & shadow_dirty_mask))
Avi Kivityce061862010-06-06 14:38:12 +0300734 kvm_set_pfn_dirty(pfn);
Marcelo Tosattieb45fda2010-10-25 11:58:22 -0200735 return 1;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800736}
737
738static void drop_spte(struct kvm *kvm, u64 *sptep, u64 new_spte)
739{
Marcelo Tosattieb45fda2010-10-25 11:58:22 -0200740 if (set_spte_track_bits(sptep, new_spte))
741 rmap_remove(kvm, sptep);
Avi Kivitybe38d272010-06-06 14:31:27 +0300742}
743
Izik Eidus98348e92007-10-16 14:42:30 +0200744static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800745{
Avi Kivity374cbac2007-01-05 16:36:43 -0800746 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200747 u64 *prev_spte;
748 int i;
749
750 if (!*rmapp)
751 return NULL;
752 else if (!(*rmapp & 1)) {
753 if (!spte)
754 return (u64 *)*rmapp;
755 return NULL;
756 }
757 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Izik Eidus98348e92007-10-16 14:42:30 +0200758 prev_spte = NULL;
759 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300760 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
Izik Eidus98348e92007-10-16 14:42:30 +0200761 if (prev_spte == spte)
Avi Kivityd555c332009-06-10 14:24:23 +0300762 return desc->sptes[i];
763 prev_spte = desc->sptes[i];
Izik Eidus98348e92007-10-16 14:42:30 +0200764 }
765 desc = desc->more;
766 }
767 return NULL;
768}
769
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200770static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200771{
Izik Eidus290fc382007-09-27 14:11:22 +0200772 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800773 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200774 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800775
Joerg Roedel44ad9942009-07-27 16:30:42 +0200776 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
Avi Kivity374cbac2007-01-05 16:36:43 -0800777
Izik Eidus98348e92007-10-16 14:42:30 +0200778 spte = rmap_next(kvm, rmapp, NULL);
779 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800780 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800781 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800782 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900783 if (is_writable_pte(*spte)) {
Avi Kivityb79b93f2010-06-06 15:46:44 +0300784 update_spte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800785 write_protected = 1;
786 }
Izik Eidus9647c142007-10-16 14:43:46 +0200787 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800788 }
Izik Eidus855149a2008-03-20 18:17:24 +0200789
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300790 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200791 for (i = PT_DIRECTORY_LEVEL;
792 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
793 rmapp = gfn_to_rmap(kvm, gfn, i);
794 spte = rmap_next(kvm, rmapp, NULL);
795 while (spte) {
796 BUG_ON(!spte);
797 BUG_ON(!(*spte & PT_PRESENT_MASK));
798 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
799 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900800 if (is_writable_pte(*spte)) {
Avi Kivitybe38d272010-06-06 14:31:27 +0300801 drop_spte(kvm, spte,
802 shadow_trap_nonpresent_pte);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200803 --kvm->stat.lpages;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200804 spte = NULL;
805 write_protected = 1;
806 }
807 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300808 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300809 }
810
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200811 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800812}
813
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000814static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
815 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200816{
817 u64 *spte;
818 int need_tlb_flush = 0;
819
820 while ((spte = rmap_next(kvm, rmapp, NULL))) {
821 BUG_ON(!(*spte & PT_PRESENT_MASK));
822 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
Avi Kivitybe38d272010-06-06 14:31:27 +0300823 drop_spte(kvm, spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200824 need_tlb_flush = 1;
825 }
826 return need_tlb_flush;
827}
828
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000829static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
830 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +0300831{
832 int need_flush = 0;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800833 u64 *spte, new_spte;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300834 pte_t *ptep = (pte_t *)data;
835 pfn_t new_pfn;
836
837 WARN_ON(pte_huge(*ptep));
838 new_pfn = pte_pfn(*ptep);
839 spte = rmap_next(kvm, rmapp, NULL);
840 while (spte) {
841 BUG_ON(!is_shadow_present_pte(*spte));
842 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
843 need_flush = 1;
844 if (pte_write(*ptep)) {
Avi Kivitybe38d272010-06-06 14:31:27 +0300845 drop_spte(kvm, spte, shadow_trap_nonpresent_pte);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300846 spte = rmap_next(kvm, rmapp, NULL);
847 } else {
848 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
849 new_spte |= (u64)new_pfn << PAGE_SHIFT;
850
851 new_spte &= ~PT_WRITABLE_MASK;
852 new_spte &= ~SPTE_HOST_WRITEABLE;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300853 new_spte &= ~shadow_accessed_mask;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800854 set_spte_track_bits(spte, new_spte);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300855 spte = rmap_next(kvm, rmapp, spte);
856 }
857 }
858 if (need_flush)
859 kvm_flush_remote_tlbs(kvm);
860
861 return 0;
862}
863
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000864static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
865 unsigned long data,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300866 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000867 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200868{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200869 int i, j;
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200870 int ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200871 int retval = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200872 struct kvm_memslots *slots;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200873
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800874 slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200875
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200876 for (i = 0; i < slots->nmemslots; i++) {
877 struct kvm_memory_slot *memslot = &slots->memslots[i];
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200878 unsigned long start = memslot->userspace_addr;
879 unsigned long end;
880
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200881 end = start + (memslot->npages << PAGE_SHIFT);
882 if (hva >= start && hva < end) {
883 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900884 gfn_t gfn = memslot->base_gfn + gfn_offset;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200885
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200886 ret = handler(kvm, &memslot->rmap[gfn_offset], data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200887
888 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900889 struct kvm_lpage_info *linfo;
Andrea Arcangeli6e3e2432010-07-16 11:52:55 +0200890
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900891 linfo = lpage_info_slot(gfn, memslot,
892 PT_DIRECTORY_LEVEL + j);
893 ret |= handler(kvm, &linfo->rmap_pde, data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200894 }
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200895 trace_kvm_age_page(hva, memslot, ret);
896 retval |= ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200897 }
898 }
899
900 return retval;
901}
902
903int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
904{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300905 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200906}
907
Izik Eidus3da0dd42009-09-23 21:47:18 +0300908void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
909{
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000910 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300911}
912
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000913static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
914 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200915{
916 u64 *spte;
917 int young = 0;
918
Rik van Riel6316e1c2010-02-03 16:11:03 -0500919 /*
920 * Emulate the accessed bit for EPT, by checking if this page has
921 * an EPT mapping, and clearing it if it does. On the next access,
922 * a new EPT mapping will be established.
923 * This has some overhead, but not as much as the cost of swapping
924 * out actively used pages or breaking up actively used hugepages.
925 */
Sheng Yang534e38b2008-09-08 15:12:30 +0800926 if (!shadow_accessed_mask)
Rik van Riel6316e1c2010-02-03 16:11:03 -0500927 return kvm_unmap_rmapp(kvm, rmapp, data);
Sheng Yang534e38b2008-09-08 15:12:30 +0800928
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200929 spte = rmap_next(kvm, rmapp, NULL);
930 while (spte) {
931 int _young;
932 u64 _spte = *spte;
933 BUG_ON(!(_spte & PT_PRESENT_MASK));
934 _young = _spte & PT_ACCESSED_MASK;
935 if (_young) {
936 young = 1;
937 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
938 }
939 spte = rmap_next(kvm, rmapp, spte);
940 }
941 return young;
942}
943
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300944#define RMAP_RECYCLE_THRESHOLD 1000
945
Joerg Roedel852e3c12009-07-27 16:30:44 +0200946static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300947{
948 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200949 struct kvm_mmu_page *sp;
950
951 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300952
Joerg Roedel852e3c12009-07-27 16:30:44 +0200953 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300954
Izik Eidus3da0dd42009-09-23 21:47:18 +0300955 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300956 kvm_flush_remote_tlbs(vcpu->kvm);
957}
958
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200959int kvm_age_hva(struct kvm *kvm, unsigned long hva)
960{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300961 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200962}
963
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800964#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300965static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966{
Avi Kivity139bdb22007-01-05 16:36:50 -0800967 u64 *pos;
968 u64 *end;
969
Avi Kivity47ad8e62007-05-06 15:50:58 +0300970 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300971 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800972 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800973 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800975 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976 return 1;
977}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800978#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979
Dave Hansen45221ab2010-08-19 18:11:37 -0700980/*
981 * This value is the sum of all of the kvm instances's
982 * kvm->arch.n_used_mmu_pages values. We need a global,
983 * aggregate version in order to make the slab shrinker
984 * faster
985 */
986static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, int nr)
987{
988 kvm->arch.n_used_mmu_pages += nr;
989 percpu_counter_add(&kvm_total_used_mmu_pages, nr);
990}
991
Avi Kivity4db35312007-11-21 15:28:32 +0200992static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800993{
Avi Kivity4db35312007-11-21 15:28:32 +0200994 ASSERT(is_empty_shadow_page(sp->spt));
Xiao Guangrong77758342010-06-04 21:53:54 +0800995 hlist_del(&sp->hash_link);
Avi Kivity4db35312007-11-21 15:28:32 +0200996 list_del(&sp->link);
997 __free_page(virt_to_page(sp->spt));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800998 if (!sp->role.direct)
999 __free_page(virt_to_page(sp->gfns));
Xiao Guangronge8ad9a72010-05-13 10:06:02 +08001000 kmem_cache_free(mmu_page_header_cache, sp);
Dave Hansen45221ab2010-08-19 18:11:37 -07001001 kvm_mod_used_mmu_pages(kvm, -1);
Avi Kivity260746c2007-01-05 16:36:49 -08001002}
1003
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001004static unsigned kvm_page_table_hashfn(gfn_t gfn)
1005{
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001006 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001007}
1008
Avi Kivity25c0de22007-01-05 16:36:42 -08001009static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
Lai Jiangshan2032a932010-05-26 16:49:59 +08001010 u64 *parent_pte, int direct)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011{
Avi Kivity4db35312007-11-21 15:28:32 +02001012 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001013
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001014 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
1015 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Lai Jiangshan2032a932010-05-26 16:49:59 +08001016 if (!direct)
1017 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache,
1018 PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +02001019 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001020 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Sheng Yang291f26b2008-10-16 17:30:57 +08001021 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +02001022 sp->multimapped = 0;
1023 sp->parent_pte = parent_pte;
Dave Hansen45221ab2010-08-19 18:11:37 -07001024 kvm_mod_used_mmu_pages(vcpu->kvm, +1);
Avi Kivity4db35312007-11-21 15:28:32 +02001025 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026}
1027
Avi Kivity714b93d2007-01-05 16:36:53 -08001028static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001029 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001030{
1031 struct kvm_pte_chain *pte_chain;
1032 struct hlist_node *node;
1033 int i;
1034
1035 if (!parent_pte)
1036 return;
Avi Kivity4db35312007-11-21 15:28:32 +02001037 if (!sp->multimapped) {
1038 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001039
1040 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +02001041 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001042 return;
1043 }
Avi Kivity4db35312007-11-21 15:28:32 +02001044 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -08001045 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02001046 INIT_HLIST_HEAD(&sp->parent_ptes);
1047 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001048 pte_chain->parent_ptes[0] = old;
1049 }
Avi Kivity4db35312007-11-21 15:28:32 +02001050 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001051 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
1052 continue;
1053 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
1054 if (!pte_chain->parent_ptes[i]) {
1055 pte_chain->parent_ptes[i] = parent_pte;
1056 return;
1057 }
1058 }
Avi Kivity714b93d2007-01-05 16:36:53 -08001059 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001060 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +02001061 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001062 pte_chain->parent_ptes[0] = parent_pte;
1063}
1064
Avi Kivity4db35312007-11-21 15:28:32 +02001065static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001066 u64 *parent_pte)
1067{
1068 struct kvm_pte_chain *pte_chain;
1069 struct hlist_node *node;
1070 int i;
1071
Avi Kivity4db35312007-11-21 15:28:32 +02001072 if (!sp->multimapped) {
1073 BUG_ON(sp->parent_pte != parent_pte);
1074 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001075 return;
1076 }
Avi Kivity4db35312007-11-21 15:28:32 +02001077 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001078 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1079 if (!pte_chain->parent_ptes[i])
1080 break;
1081 if (pte_chain->parent_ptes[i] != parent_pte)
1082 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001083 while (i + 1 < NR_PTE_CHAIN_ENTRIES
1084 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001085 pte_chain->parent_ptes[i]
1086 = pte_chain->parent_ptes[i + 1];
1087 ++i;
1088 }
1089 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001090 if (i == 0) {
1091 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001092 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +02001093 if (hlist_empty(&sp->parent_ptes)) {
1094 sp->multimapped = 0;
1095 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001096 }
1097 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001098 return;
1099 }
1100 BUG();
1101}
1102
Xiao Guangrong6b184932010-04-16 21:29:17 +08001103static void mmu_parent_walk(struct kvm_mmu_page *sp, mmu_parent_walk_fn fn)
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001104{
1105 struct kvm_pte_chain *pte_chain;
1106 struct hlist_node *node;
1107 struct kvm_mmu_page *parent_sp;
1108 int i;
1109
1110 if (!sp->multimapped && sp->parent_pte) {
1111 parent_sp = page_header(__pa(sp->parent_pte));
Xiao Guangrong1047df12010-06-11 21:35:15 +08001112 fn(parent_sp, sp->parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001113 return;
1114 }
1115
1116 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1117 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
Xiao Guangrong1047df12010-06-11 21:35:15 +08001118 u64 *spte = pte_chain->parent_ptes[i];
1119
1120 if (!spte)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001121 break;
Xiao Guangrong1047df12010-06-11 21:35:15 +08001122 parent_sp = page_header(__pa(spte));
1123 fn(parent_sp, spte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001124 }
1125}
1126
Xiao Guangrong1047df12010-06-11 21:35:15 +08001127static void mark_unsync(struct kvm_mmu_page *sp, u64 *spte);
Xiao Guangrong6b184932010-04-16 21:29:17 +08001128static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001129{
Xiao Guangrong1047df12010-06-11 21:35:15 +08001130 mmu_parent_walk(sp, mark_unsync);
1131}
1132
1133static void mark_unsync(struct kvm_mmu_page *sp, u64 *spte)
1134{
1135 unsigned int index;
1136
1137 index = spte - sp->spt;
1138 if (__test_and_set_bit(index, sp->unsync_child_bitmap))
1139 return;
1140 if (sp->unsync_children++)
1141 return;
1142 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001143}
1144
Avi Kivityd761a502008-05-29 14:55:03 +03001145static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1146 struct kvm_mmu_page *sp)
1147{
1148 int i;
1149
1150 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1151 sp->spt[i] = shadow_trap_nonpresent_pte;
1152}
1153
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001154static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001155 struct kvm_mmu_page *sp)
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001156{
1157 return 1;
1158}
1159
Marcelo Tosattia7052892008-09-23 13:18:35 -03001160static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1161{
1162}
1163
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001164#define KVM_PAGE_ARRAY_NR 16
1165
1166struct kvm_mmu_pages {
1167 struct mmu_page_and_offset {
1168 struct kvm_mmu_page *sp;
1169 unsigned int idx;
1170 } page[KVM_PAGE_ARRAY_NR];
1171 unsigned int nr;
1172};
1173
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001174#define for_each_unsync_children(bitmap, idx) \
1175 for (idx = find_first_bit(bitmap, 512); \
1176 idx < 512; \
1177 idx = find_next_bit(bitmap, 512, idx+1))
1178
Hannes Edercded19f2009-02-21 02:19:13 +01001179static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1180 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001181{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001182 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001183
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001184 if (sp->unsync)
1185 for (i=0; i < pvec->nr; i++)
1186 if (pvec->page[i].sp == sp)
1187 return 0;
1188
1189 pvec->page[pvec->nr].sp = sp;
1190 pvec->page[pvec->nr].idx = idx;
1191 pvec->nr++;
1192 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1193}
1194
1195static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1196 struct kvm_mmu_pages *pvec)
1197{
1198 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001199
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001200 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001201 struct kvm_mmu_page *child;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001202 u64 ent = sp->spt[i];
1203
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001204 if (!is_shadow_present_pte(ent) || is_large_pte(ent))
1205 goto clear_child_bitmap;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001206
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001207 child = page_header(ent & PT64_BASE_ADDR_MASK);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001208
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001209 if (child->unsync_children) {
1210 if (mmu_pages_add(pvec, child, i))
1211 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001212
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001213 ret = __mmu_unsync_walk(child, pvec);
1214 if (!ret)
1215 goto clear_child_bitmap;
1216 else if (ret > 0)
1217 nr_unsync_leaf += ret;
1218 else
1219 return ret;
1220 } else if (child->unsync) {
1221 nr_unsync_leaf++;
1222 if (mmu_pages_add(pvec, child, i))
1223 return -ENOSPC;
1224 } else
1225 goto clear_child_bitmap;
1226
1227 continue;
1228
1229clear_child_bitmap:
1230 __clear_bit(i, sp->unsync_child_bitmap);
1231 sp->unsync_children--;
1232 WARN_ON((int)sp->unsync_children < 0);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001233 }
1234
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001235
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001236 return nr_unsync_leaf;
1237}
1238
1239static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1240 struct kvm_mmu_pages *pvec)
1241{
1242 if (!sp->unsync_children)
1243 return 0;
1244
1245 mmu_pages_add(pvec, sp, 0);
1246 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001247}
1248
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001249static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1250{
1251 WARN_ON(!sp->unsync);
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001252 trace_kvm_mmu_sync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001253 sp->unsync = 0;
1254 --kvm->stat.mmu_unsync;
1255}
1256
Xiao Guangrong77758342010-06-04 21:53:54 +08001257static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1258 struct list_head *invalid_list);
1259static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1260 struct list_head *invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001261
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001262#define for_each_gfn_sp(kvm, sp, gfn, pos) \
1263 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001264 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1265 if ((sp)->gfn != (gfn)) {} else
1266
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001267#define for_each_gfn_indirect_valid_sp(kvm, sp, gfn, pos) \
1268 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001269 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1270 if ((sp)->gfn != (gfn) || (sp)->role.direct || \
1271 (sp)->role.invalid) {} else
1272
Xiao Guangrongf918b442010-06-11 21:30:36 +08001273/* @sp->gfn should be write-protected at the call site */
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001274static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001275 struct list_head *invalid_list, bool clear_unsync)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001276{
Avi Kivity5b7e0102010-04-14 19:20:03 +03001277 if (sp->role.cr4_pae != !!is_pae(vcpu)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001278 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001279 return 1;
1280 }
1281
Xiao Guangrongf918b442010-06-11 21:30:36 +08001282 if (clear_unsync)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001283 kvm_unlink_unsync_page(vcpu->kvm, sp);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001284
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001285 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001286 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001287 return 1;
1288 }
1289
1290 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001291 return 0;
1292}
1293
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001294static int kvm_sync_page_transient(struct kvm_vcpu *vcpu,
1295 struct kvm_mmu_page *sp)
1296{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001297 LIST_HEAD(invalid_list);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001298 int ret;
1299
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001300 ret = __kvm_sync_page(vcpu, sp, &invalid_list, false);
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001301 if (ret)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001302 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
1303
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001304 return ret;
1305}
1306
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001307static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1308 struct list_head *invalid_list)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001309{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001310 return __kvm_sync_page(vcpu, sp, invalid_list, true);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001311}
1312
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001313/* @gfn should be write-protected at the call site */
1314static void kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1315{
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001316 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001317 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001318 LIST_HEAD(invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001319 bool flush = false;
1320
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001321 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001322 if (!s->unsync)
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001323 continue;
1324
1325 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001326 kvm_unlink_unsync_page(vcpu->kvm, s);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001327 if ((s->role.cr4_pae != !!is_pae(vcpu)) ||
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001328 (vcpu->arch.mmu.sync_page(vcpu, s))) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001329 kvm_mmu_prepare_zap_page(vcpu->kvm, s, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001330 continue;
1331 }
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001332 flush = true;
1333 }
1334
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001335 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001336 if (flush)
1337 kvm_mmu_flush_tlb(vcpu);
1338}
1339
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001340struct mmu_page_path {
1341 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1342 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001343};
1344
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001345#define for_each_sp(pvec, sp, parents, i) \
1346 for (i = mmu_pages_next(&pvec, &parents, -1), \
1347 sp = pvec.page[i].sp; \
1348 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1349 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001350
Hannes Edercded19f2009-02-21 02:19:13 +01001351static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1352 struct mmu_page_path *parents,
1353 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001354{
1355 int n;
1356
1357 for (n = i+1; n < pvec->nr; n++) {
1358 struct kvm_mmu_page *sp = pvec->page[n].sp;
1359
1360 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1361 parents->idx[0] = pvec->page[n].idx;
1362 return n;
1363 }
1364
1365 parents->parent[sp->role.level-2] = sp;
1366 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1367 }
1368
1369 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001370}
1371
Hannes Edercded19f2009-02-21 02:19:13 +01001372static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001373{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001374 struct kvm_mmu_page *sp;
1375 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001376
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001377 do {
1378 unsigned int idx = parents->idx[level];
1379
1380 sp = parents->parent[level];
1381 if (!sp)
1382 return;
1383
1384 --sp->unsync_children;
1385 WARN_ON((int)sp->unsync_children < 0);
1386 __clear_bit(idx, sp->unsync_child_bitmap);
1387 level++;
1388 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1389}
1390
1391static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1392 struct mmu_page_path *parents,
1393 struct kvm_mmu_pages *pvec)
1394{
1395 parents->parent[parent->role.level-1] = NULL;
1396 pvec->nr = 0;
1397}
1398
1399static void mmu_sync_children(struct kvm_vcpu *vcpu,
1400 struct kvm_mmu_page *parent)
1401{
1402 int i;
1403 struct kvm_mmu_page *sp;
1404 struct mmu_page_path parents;
1405 struct kvm_mmu_pages pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001406 LIST_HEAD(invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001407
1408 kvm_mmu_pages_init(parent, &parents, &pages);
1409 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001410 int protected = 0;
1411
1412 for_each_sp(pages, sp, parents, i)
1413 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1414
1415 if (protected)
1416 kvm_flush_remote_tlbs(vcpu->kvm);
1417
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001418 for_each_sp(pages, sp, parents, i) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001419 kvm_sync_page(vcpu, sp, &invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001420 mmu_pages_clear_parents(&parents);
1421 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001422 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001423 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001424 kvm_mmu_pages_init(parent, &parents, &pages);
1425 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001426}
1427
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001428static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1429 gfn_t gfn,
1430 gva_t gaddr,
1431 unsigned level,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001432 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001433 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001434 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001435{
1436 union kvm_mmu_page_role role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001437 unsigned quadrant;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001438 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001439 struct hlist_node *node;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001440 bool need_sync = false;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001441
Avi Kivitya770f6f2008-12-21 19:20:09 +02001442 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001443 role.level = level;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001444 role.direct = direct;
Avi Kivity84b0c8c2010-03-14 10:16:40 +02001445 if (role.direct)
Avi Kivity5b7e0102010-04-14 19:20:03 +03001446 role.cr4_pae = 0;
Avi Kivity41074d02007-12-09 17:00:02 +02001447 role.access = access;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02001448 if (!vcpu->arch.mmu.direct_map
1449 && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001450 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1451 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1452 role.quadrant = quadrant;
1453 }
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001454 for_each_gfn_sp(vcpu->kvm, sp, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001455 if (!need_sync && sp->unsync)
1456 need_sync = true;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001457
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001458 if (sp->role.word != role.word)
1459 continue;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001460
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001461 if (sp->unsync && kvm_sync_page_transient(vcpu, sp))
1462 break;
Xiao Guangronge02aa902010-05-15 18:52:34 +08001463
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001464 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1465 if (sp->unsync_children) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001466 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001467 kvm_mmu_mark_parents_unsync(sp);
1468 } else if (sp->unsync)
1469 kvm_mmu_mark_parents_unsync(sp);
Xiao Guangronge02aa902010-05-15 18:52:34 +08001470
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001471 trace_kvm_mmu_get_page(sp, false);
1472 return sp;
1473 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001474 ++vcpu->kvm->stat.mmu_cache_miss;
Lai Jiangshan2032a932010-05-26 16:49:59 +08001475 sp = kvm_mmu_alloc_page(vcpu, parent_pte, direct);
Avi Kivity4db35312007-11-21 15:28:32 +02001476 if (!sp)
1477 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001478 sp->gfn = gfn;
1479 sp->role = role;
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001480 hlist_add_head(&sp->hash_link,
1481 &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001482 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001483 if (rmap_write_protect(vcpu->kvm, gfn))
1484 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001485 if (level > PT_PAGE_TABLE_LEVEL && need_sync)
1486 kvm_sync_pages(vcpu, gfn);
1487
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001488 account_shadowed(vcpu->kvm, gfn);
1489 }
Avi Kivity131d8272008-05-29 14:56:28 +03001490 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1491 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1492 else
1493 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001494 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001495 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001496}
1497
Avi Kivity2d111232008-12-25 14:39:47 +02001498static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1499 struct kvm_vcpu *vcpu, u64 addr)
1500{
1501 iterator->addr = addr;
1502 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1503 iterator->level = vcpu->arch.mmu.shadow_root_level;
Joerg Roedel81407ca2010-09-10 17:31:00 +02001504
1505 if (iterator->level == PT64_ROOT_LEVEL &&
1506 vcpu->arch.mmu.root_level < PT64_ROOT_LEVEL &&
1507 !vcpu->arch.mmu.direct_map)
1508 --iterator->level;
1509
Avi Kivity2d111232008-12-25 14:39:47 +02001510 if (iterator->level == PT32E_ROOT_LEVEL) {
1511 iterator->shadow_addr
1512 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1513 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1514 --iterator->level;
1515 if (!iterator->shadow_addr)
1516 iterator->level = 0;
1517 }
1518}
1519
1520static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1521{
1522 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1523 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001524
1525 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1526 if (is_large_pte(*iterator->sptep))
1527 return false;
1528
Avi Kivity2d111232008-12-25 14:39:47 +02001529 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1530 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1531 return true;
1532}
1533
1534static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1535{
1536 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1537 --iterator->level;
1538}
1539
Avi Kivity32ef26a2010-07-13 14:27:04 +03001540static void link_shadow_page(u64 *sptep, struct kvm_mmu_page *sp)
1541{
1542 u64 spte;
1543
1544 spte = __pa(sp->spt)
1545 | PT_PRESENT_MASK | PT_ACCESSED_MASK
1546 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity121eee92010-07-13 14:27:05 +03001547 __set_spte(sptep, spte);
Avi Kivity32ef26a2010-07-13 14:27:04 +03001548}
1549
Avi Kivitya3aa51c2010-07-13 14:27:06 +03001550static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep)
1551{
1552 if (is_large_pte(*sptep)) {
1553 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
1554 kvm_flush_remote_tlbs(vcpu->kvm);
1555 }
1556}
1557
Avi Kivitya357bd22010-07-13 14:27:07 +03001558static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1559 unsigned direct_access)
1560{
1561 if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
1562 struct kvm_mmu_page *child;
1563
1564 /*
1565 * For the direct sp, if the guest pte's dirty bit
1566 * changed form clean to dirty, it will corrupt the
1567 * sp's access: allow writable in the read-only sp,
1568 * so we should update the spte at this point to get
1569 * a new sp with the correct access.
1570 */
1571 child = page_header(*sptep & PT64_BASE_ADDR_MASK);
1572 if (child->role.access == direct_access)
1573 return;
1574
1575 mmu_page_remove_parent_pte(child, sptep);
1576 __set_spte(sptep, shadow_trap_nonpresent_pte);
1577 kvm_flush_remote_tlbs(vcpu->kvm);
1578 }
1579}
1580
Avi Kivity90cb0522007-07-17 13:04:56 +03001581static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001582 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001583{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001584 unsigned i;
1585 u64 *pt;
1586 u64 ent;
1587
Avi Kivity4db35312007-11-21 15:28:32 +02001588 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001589
Avi Kivity697fe2e2007-01-05 16:36:46 -08001590 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1591 ent = pt[i];
1592
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001593 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001594 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001595 ent &= PT64_BASE_ADDR_MASK;
1596 mmu_page_remove_parent_pte(page_header(ent),
1597 &pt[i]);
1598 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001599 if (is_large_pte(ent))
1600 --kvm->stat.lpages;
Avi Kivitybe38d272010-06-06 14:31:27 +03001601 drop_spte(kvm, &pt[i],
1602 shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001603 }
1604 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001605 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001606 }
Avi Kivitya4360362007-01-05 16:36:45 -08001607}
1608
Avi Kivity4db35312007-11-21 15:28:32 +02001609static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001610{
Avi Kivity4db35312007-11-21 15:28:32 +02001611 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001612}
1613
Avi Kivity12b7d282007-09-23 14:10:49 +02001614static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1615{
1616 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001617 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001618
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001619 kvm_for_each_vcpu(i, vcpu, kvm)
1620 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001621}
1622
Avi Kivity31aa2b42008-07-11 17:59:46 +03001623static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001624{
1625 u64 *parent_pte;
1626
Avi Kivity4db35312007-11-21 15:28:32 +02001627 while (sp->multimapped || sp->parent_pte) {
1628 if (!sp->multimapped)
1629 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001630 else {
1631 struct kvm_pte_chain *chain;
1632
Avi Kivity4db35312007-11-21 15:28:32 +02001633 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001634 struct kvm_pte_chain, link);
1635 parent_pte = chain->parent_ptes[0];
1636 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001637 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001638 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001639 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001640 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001641}
1642
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001643static int mmu_zap_unsync_children(struct kvm *kvm,
Xiao Guangrong77758342010-06-04 21:53:54 +08001644 struct kvm_mmu_page *parent,
1645 struct list_head *invalid_list)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001646{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001647 int i, zapped = 0;
1648 struct mmu_page_path parents;
1649 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001650
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001651 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001652 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001653
1654 kvm_mmu_pages_init(parent, &parents, &pages);
1655 while (mmu_unsync_walk(parent, &pages)) {
1656 struct kvm_mmu_page *sp;
1657
1658 for_each_sp(pages, sp, parents, i) {
Xiao Guangrong77758342010-06-04 21:53:54 +08001659 kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001660 mmu_pages_clear_parents(&parents);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001661 zapped++;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001662 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001663 kvm_mmu_pages_init(parent, &parents, &pages);
1664 }
1665
1666 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001667}
1668
Xiao Guangrong77758342010-06-04 21:53:54 +08001669static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1670 struct list_head *invalid_list)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001671{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001672 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001673
Xiao Guangrong77758342010-06-04 21:53:54 +08001674 trace_kvm_mmu_prepare_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001675 ++kvm->stat.mmu_shadow_zapped;
Xiao Guangrong77758342010-06-04 21:53:54 +08001676 ret = mmu_zap_unsync_children(kvm, sp, invalid_list);
Avi Kivity4db35312007-11-21 15:28:32 +02001677 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001678 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001679 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001680 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001681 if (sp->unsync)
1682 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001683 if (!sp->root_count) {
Gui Jianfeng54a4f022010-05-05 09:03:49 +08001684 /* Count self */
1685 ret++;
Xiao Guangrong77758342010-06-04 21:53:54 +08001686 list_move(&sp->link, invalid_list);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001687 } else {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001688 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001689 kvm_reload_remote_mmus(kvm);
1690 }
Xiao Guangrong77758342010-06-04 21:53:54 +08001691
1692 sp->role.invalid = 1;
Avi Kivity12b7d282007-09-23 14:10:49 +02001693 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001694 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001695}
1696
Xiao Guangrong77758342010-06-04 21:53:54 +08001697static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1698 struct list_head *invalid_list)
1699{
1700 struct kvm_mmu_page *sp;
1701
1702 if (list_empty(invalid_list))
1703 return;
1704
1705 kvm_flush_remote_tlbs(kvm);
1706
1707 do {
1708 sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
1709 WARN_ON(!sp->role.invalid || sp->root_count);
1710 kvm_mmu_free_page(kvm, sp);
1711 } while (!list_empty(invalid_list));
1712
1713}
1714
Izik Eidus82ce2c92007-10-02 18:52:55 +02001715/*
1716 * Changing the number of mmu pages allocated to the vm
Dave Hansen49d5ca22010-08-19 18:11:28 -07001717 * Note: if goal_nr_mmu_pages is too small, you will get dead lock
Izik Eidus82ce2c92007-10-02 18:52:55 +02001718 */
Dave Hansen49d5ca22010-08-19 18:11:28 -07001719void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages)
Izik Eidus82ce2c92007-10-02 18:52:55 +02001720{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001721 LIST_HEAD(invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001722 /*
1723 * If we set the number of mmu pages to be smaller be than the
1724 * number of actived pages , we must to free some mmu pages before we
1725 * change the value
1726 */
1727
Dave Hansen49d5ca22010-08-19 18:11:28 -07001728 if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) {
1729 while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages &&
Xiao Guangrong77662e02010-04-16 16:34:42 +08001730 !list_empty(&kvm->arch.active_mmu_pages)) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001731 struct kvm_mmu_page *page;
1732
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001733 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001734 struct kvm_mmu_page, link);
Xiaotian Feng80b63fa2010-08-24 10:31:07 +08001735 kvm_mmu_prepare_zap_page(kvm, page, &invalid_list);
1736 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001737 }
Dave Hansen49d5ca22010-08-19 18:11:28 -07001738 goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001739 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02001740
Dave Hansen49d5ca22010-08-19 18:11:28 -07001741 kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001742}
1743
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001744static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001745{
Avi Kivity4db35312007-11-21 15:28:32 +02001746 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001747 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001748 LIST_HEAD(invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001749 int r;
1750
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08001751 pgprintk("%s: looking for gfn %llx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001752 r = 0;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001753
1754 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08001755 pgprintk("%s: gfn %llx role %x\n", __func__, gfn,
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001756 sp->role.word);
1757 r = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001758 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001759 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001760 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001761 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001762}
1763
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001764static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001765{
Avi Kivity4db35312007-11-21 15:28:32 +02001766 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001767 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001768 LIST_HEAD(invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001769
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001770 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08001771 pgprintk("%s: zap %llx %x\n",
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001772 __func__, gfn, sp->role.word);
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001773 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001774 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001775 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001776}
1777
Avi Kivity38c335f2007-11-21 14:20:22 +02001778static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001779{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001780 int slot = memslot_id(kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001781 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001782
Sheng Yang291f26b2008-10-16 17:30:57 +08001783 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001784}
1785
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001786static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1787{
1788 int i;
1789 u64 *pt = sp->spt;
1790
1791 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1792 return;
1793
1794 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1795 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001796 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001797 }
1798}
1799
Sheng Yang74be52e2008-10-09 16:01:56 +08001800/*
1801 * The function is based on mtrr_type_lookup() in
1802 * arch/x86/kernel/cpu/mtrr/generic.c
1803 */
1804static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1805 u64 start, u64 end)
1806{
1807 int i;
1808 u64 base, mask;
1809 u8 prev_match, curr_match;
1810 int num_var_ranges = KVM_NR_VAR_MTRR;
1811
1812 if (!mtrr_state->enabled)
1813 return 0xFF;
1814
1815 /* Make end inclusive end, instead of exclusive */
1816 end--;
1817
1818 /* Look in fixed ranges. Just return the type as per start */
1819 if (mtrr_state->have_fixed && (start < 0x100000)) {
1820 int idx;
1821
1822 if (start < 0x80000) {
1823 idx = 0;
1824 idx += (start >> 16);
1825 return mtrr_state->fixed_ranges[idx];
1826 } else if (start < 0xC0000) {
1827 idx = 1 * 8;
1828 idx += ((start - 0x80000) >> 14);
1829 return mtrr_state->fixed_ranges[idx];
1830 } else if (start < 0x1000000) {
1831 idx = 3 * 8;
1832 idx += ((start - 0xC0000) >> 12);
1833 return mtrr_state->fixed_ranges[idx];
1834 }
1835 }
1836
1837 /*
1838 * Look in variable ranges
1839 * Look of multiple ranges matching this address and pick type
1840 * as per MTRR precedence
1841 */
1842 if (!(mtrr_state->enabled & 2))
1843 return mtrr_state->def_type;
1844
1845 prev_match = 0xFF;
1846 for (i = 0; i < num_var_ranges; ++i) {
1847 unsigned short start_state, end_state;
1848
1849 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1850 continue;
1851
1852 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1853 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1854 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1855 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1856
1857 start_state = ((start & mask) == (base & mask));
1858 end_state = ((end & mask) == (base & mask));
1859 if (start_state != end_state)
1860 return 0xFE;
1861
1862 if ((start & mask) != (base & mask))
1863 continue;
1864
1865 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1866 if (prev_match == 0xFF) {
1867 prev_match = curr_match;
1868 continue;
1869 }
1870
1871 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1872 curr_match == MTRR_TYPE_UNCACHABLE)
1873 return MTRR_TYPE_UNCACHABLE;
1874
1875 if ((prev_match == MTRR_TYPE_WRBACK &&
1876 curr_match == MTRR_TYPE_WRTHROUGH) ||
1877 (prev_match == MTRR_TYPE_WRTHROUGH &&
1878 curr_match == MTRR_TYPE_WRBACK)) {
1879 prev_match = MTRR_TYPE_WRTHROUGH;
1880 curr_match = MTRR_TYPE_WRTHROUGH;
1881 }
1882
1883 if (prev_match != curr_match)
1884 return MTRR_TYPE_UNCACHABLE;
1885 }
1886
1887 if (prev_match != 0xFF)
1888 return prev_match;
1889
1890 return mtrr_state->def_type;
1891}
1892
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001893u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001894{
1895 u8 mtrr;
1896
1897 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1898 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1899 if (mtrr == 0xfe || mtrr == 0xff)
1900 mtrr = MTRR_TYPE_WRBACK;
1901 return mtrr;
1902}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001903EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001904
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001905static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001906{
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001907 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001908 ++vcpu->kvm->stat.mmu_unsync;
1909 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001910
Xiao Guangrong6b184932010-04-16 21:29:17 +08001911 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001912 mmu_convert_notrap(sp);
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001913}
1914
1915static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1916{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001917 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001918 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001919
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001920 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001921 if (s->unsync)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001922 continue;
1923 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1924 __kvm_unsync_page(vcpu, s);
1925 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001926}
1927
1928static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1929 bool can_unsync)
1930{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001931 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001932 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001933 bool need_unsync = false;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001934
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001935 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001936 if (!can_unsync)
1937 return 1;
1938
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001939 if (s->role.level != PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001940 return 1;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001941
1942 if (!need_unsync && !s->unsync) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001943 if (!oos_shadow)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001944 return 1;
1945 need_unsync = true;
1946 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001947 }
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001948 if (need_unsync)
1949 kvm_unsync_pages(vcpu, gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001950 return 0;
1951}
1952
Avi Kivityd555c332009-06-10 14:24:23 +03001953static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001954 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001955 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001956 gfn_t gfn, pfn_t pfn, bool speculative,
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08001957 bool can_unsync, bool host_writable)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001958{
Xiao Guangrongb330aa02010-11-19 17:02:35 +08001959 u64 spte, entry = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001960 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001961
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001962 /*
1963 * We don't set the accessed bit, since we sometimes want to see
1964 * whether the guest actually used the pte (in order to detect
1965 * demand paging).
1966 */
Marcelo Tosatti982c2562010-10-22 14:18:16 -02001967 spte = PT_PRESENT_MASK;
Avi Kivity947da532008-03-18 11:05:52 +02001968 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001969 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001970 if (!dirty)
1971 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001972 if (pte_access & ACC_EXEC_MASK)
1973 spte |= shadow_x_mask;
1974 else
1975 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001976 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001977 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001978 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001979 spte |= PT_PAGE_SIZE_MASK;
Avi Kivityb0bc3ee2010-09-13 16:45:28 +02001980 if (tdp_enabled)
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001981 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1982 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001983
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08001984 if (host_writable)
Izik Eidus14032832009-09-23 21:47:17 +03001985 spte |= SPTE_HOST_WRITEABLE;
1986
Anthony Liguori35149e22008-04-02 14:46:56 -05001987 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001988
1989 if ((pte_access & ACC_WRITE_MASK)
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02001990 || (!vcpu->arch.mmu.direct_map && write_fault
1991 && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001992
Joerg Roedel852e3c12009-07-27 16:30:44 +02001993 if (level > PT_PAGE_TABLE_LEVEL &&
1994 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001995 ret = 1;
Avi Kivitybe38d272010-06-06 14:31:27 +03001996 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
1997 goto done;
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001998 }
1999
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002000 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002001
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002002 if (!vcpu->arch.mmu.direct_map
2003 && !(pte_access & ACC_WRITE_MASK))
Avi Kivity69325a12010-05-27 14:35:58 +03002004 spte &= ~PT_USER_MASK;
2005
Marcelo Tosattiecc55892008-11-25 15:58:07 +01002006 /*
2007 * Optimization: for pte sync, if spte was writable the hash
2008 * lookup is unnecessary (and expensive). Write protection
2009 * is responsibility of mmu_get_page / kvm_sync_page.
2010 * Same reasoning can be applied to dirty page accounting.
2011 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002012 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01002013 goto set_pte;
2014
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002015 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08002016 pgprintk("%s: found shadow page for %llx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002017 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002018 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002019 pte_access &= ~ACC_WRITE_MASK;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002020 if (is_writable_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002021 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002022 }
2023 }
2024
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002025 if (pte_access & ACC_WRITE_MASK)
2026 mark_page_dirty(vcpu->kvm, gfn);
2027
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002028set_pte:
Avi Kivityb79b93f2010-06-06 15:46:44 +03002029 update_spte(sptep, spte);
Xiao Guangrongb330aa02010-11-19 17:02:35 +08002030 /*
2031 * If we overwrite a writable spte with a read-only one we
2032 * should flush remote TLBs. Otherwise rmap_write_protect
2033 * will find a read-only spte, even though the writable spte
2034 * might be cached on a CPU's TLB.
2035 */
2036 if (is_writable_pte(entry) && !is_writable_pte(*sptep))
2037 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivitybe38d272010-06-06 14:31:27 +03002038done:
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002039 return ret;
2040}
2041
Avi Kivityd555c332009-06-10 14:24:23 +03002042static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002043 unsigned pt_access, unsigned pte_access,
2044 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002045 int *ptwrite, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03002046 pfn_t pfn, bool speculative,
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08002047 bool host_writable)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002048{
2049 int was_rmapped = 0;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002050 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002051
2052 pgprintk("%s: spte %llx access %x write_fault %d"
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08002053 " user_fault %d gfn %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002054 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002055 write_fault, user_fault, gfn);
2056
Avi Kivityd555c332009-06-10 14:24:23 +03002057 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002058 /*
2059 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
2060 * the parent of the now unreachable PTE.
2061 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02002062 if (level > PT_PAGE_TABLE_LEVEL &&
2063 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002064 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03002065 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002066
2067 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03002068 mmu_page_remove_parent_pte(child, sptep);
Marcelo Tosatti3be22642010-05-28 09:44:59 -03002069 __set_spte(sptep, shadow_trap_nonpresent_pte);
2070 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityd555c332009-06-10 14:24:23 +03002071 } else if (pfn != spte_to_pfn(*sptep)) {
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08002072 pgprintk("hfn old %llx new %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002073 spte_to_pfn(*sptep), pfn);
Avi Kivitybe38d272010-06-06 14:31:27 +03002074 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
Xiao Guangrong91546352010-06-30 16:04:06 +08002075 kvm_flush_remote_tlbs(vcpu->kvm);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01002076 } else
2077 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002078 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02002079
Avi Kivityd555c332009-06-10 14:24:23 +03002080 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Izik Eidus14032832009-09-23 21:47:17 +03002081 dirty, level, gfn, pfn, speculative, true,
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08002082 host_writable)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002083 if (write_fault)
2084 *ptwrite = 1;
Xiao Guangrong5304efd2010-06-08 20:05:57 +08002085 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03002086 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002087
Avi Kivityd555c332009-06-10 14:24:23 +03002088 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Xiao Guangrong9ad17b12010-08-28 19:19:42 +08002089 pgprintk("instantiating %s PTE (%s) at %llx (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002090 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02002091 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
2092 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03002093 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002094 ++vcpu->kvm->stat.lpages;
2095
Avi Kivityd555c332009-06-10 14:24:23 +03002096 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002097 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02002098 rmap_count = rmap_add(vcpu, sptep, gfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002099 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02002100 rmap_recycle(vcpu, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002101 }
Xiao Guangrong9ed55202010-07-16 11:25:17 +08002102 kvm_release_pfn_clean(pfn);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002103 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03002104 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002105 vcpu->arch.last_pte_gfn = gfn;
2106 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002107}
2108
Avi Kivity6aa8b732006-12-10 02:21:36 -08002109static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
2110{
2111}
2112
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002113static struct kvm_memory_slot *
2114pte_prefetch_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn, bool no_dirty_log)
2115{
2116 struct kvm_memory_slot *slot;
2117
2118 slot = gfn_to_memslot(vcpu->kvm, gfn);
2119 if (!slot || slot->flags & KVM_MEMSLOT_INVALID ||
2120 (no_dirty_log && slot->dirty_bitmap))
2121 slot = NULL;
2122
2123 return slot;
2124}
2125
2126static pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn,
2127 bool no_dirty_log)
2128{
2129 struct kvm_memory_slot *slot;
2130 unsigned long hva;
2131
2132 slot = pte_prefetch_gfn_to_memslot(vcpu, gfn, no_dirty_log);
2133 if (!slot) {
2134 get_page(bad_page);
2135 return page_to_pfn(bad_page);
2136 }
2137
2138 hva = gfn_to_hva_memslot(slot, gfn);
2139
2140 return hva_to_pfn_atomic(vcpu->kvm, hva);
2141}
2142
2143static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,
2144 struct kvm_mmu_page *sp,
2145 u64 *start, u64 *end)
2146{
2147 struct page *pages[PTE_PREFETCH_NUM];
2148 unsigned access = sp->role.access;
2149 int i, ret;
2150 gfn_t gfn;
2151
2152 gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt);
2153 if (!pte_prefetch_gfn_to_memslot(vcpu, gfn, access & ACC_WRITE_MASK))
2154 return -1;
2155
2156 ret = gfn_to_page_many_atomic(vcpu->kvm, gfn, pages, end - start);
2157 if (ret <= 0)
2158 return -1;
2159
2160 for (i = 0; i < ret; i++, gfn++, start++)
2161 mmu_set_spte(vcpu, start, ACC_ALL,
2162 access, 0, 0, 1, NULL,
2163 sp->role.level, gfn,
2164 page_to_pfn(pages[i]), true, true);
2165
2166 return 0;
2167}
2168
2169static void __direct_pte_prefetch(struct kvm_vcpu *vcpu,
2170 struct kvm_mmu_page *sp, u64 *sptep)
2171{
2172 u64 *spte, *start = NULL;
2173 int i;
2174
2175 WARN_ON(!sp->role.direct);
2176
2177 i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
2178 spte = sp->spt + i;
2179
2180 for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
2181 if (*spte != shadow_trap_nonpresent_pte || spte == sptep) {
2182 if (!start)
2183 continue;
2184 if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0)
2185 break;
2186 start = NULL;
2187 } else if (!start)
2188 start = spte;
2189 }
2190}
2191
2192static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)
2193{
2194 struct kvm_mmu_page *sp;
2195
2196 /*
2197 * Since it's no accessed bit on EPT, it's no way to
2198 * distinguish between actually accessed translations
2199 * and prefetched, so disable pte prefetch if EPT is
2200 * enabled.
2201 */
2202 if (!shadow_accessed_mask)
2203 return;
2204
2205 sp = page_header(__pa(sptep));
2206 if (sp->role.level > PT_PAGE_TABLE_LEVEL)
2207 return;
2208
2209 __direct_pte_prefetch(vcpu, sp, sptep);
2210}
2211
Joerg Roedel4d9976b2008-02-07 13:47:42 +01002212static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002213 int map_writable, int level, gfn_t gfn, pfn_t pfn,
2214 bool prefault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002215{
Avi Kivity9f652d22008-12-25 14:54:25 +02002216 struct kvm_shadow_walk_iterator iterator;
2217 struct kvm_mmu_page *sp;
2218 int pt_write = 0;
2219 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002220
Avi Kivity9f652d22008-12-25 14:54:25 +02002221 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02002222 if (iterator.level == level) {
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002223 unsigned pte_access = ACC_ALL;
2224
2225 if (!map_writable)
2226 pte_access &= ~ACC_WRITE_MASK;
2227 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, pte_access,
Avi Kivity9f652d22008-12-25 14:54:25 +02002228 0, write, 1, &pt_write,
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002229 level, gfn, pfn, prefault, map_writable);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002230 direct_pte_prefetch(vcpu, iterator.sptep);
Avi Kivity9f652d22008-12-25 14:54:25 +02002231 ++vcpu->stat.pf_fixed;
2232 break;
2233 }
2234
2235 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
Lai Jiangshanc9fa0b32010-05-26 16:48:25 +08002236 u64 base_addr = iterator.addr;
2237
2238 base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
2239 pseudo_gfn = base_addr >> PAGE_SHIFT;
Avi Kivity9f652d22008-12-25 14:54:25 +02002240 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
2241 iterator.level - 1,
2242 1, ACC_ALL, iterator.sptep);
2243 if (!sp) {
2244 pgprintk("nonpaging_map: ENOMEM\n");
2245 kvm_release_pfn_clean(pfn);
2246 return -ENOMEM;
2247 }
2248
Avi Kivityd555c332009-06-10 14:24:23 +03002249 __set_spte(iterator.sptep,
2250 __pa(sp->spt)
2251 | PT_PRESENT_MASK | PT_WRITABLE_MASK
Xiao Guangrong33f91ed2010-09-27 18:05:00 +08002252 | shadow_user_mask | shadow_x_mask
2253 | shadow_accessed_mask);
Avi Kivity9f652d22008-12-25 14:54:25 +02002254 }
2255 }
2256 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002257}
2258
Huang Ying77db5cb2010-10-08 16:24:15 +08002259static void kvm_send_hwpoison_signal(unsigned long address, struct task_struct *tsk)
Huang Yingbf998152010-05-31 14:28:19 +08002260{
Huang Ying77db5cb2010-10-08 16:24:15 +08002261 siginfo_t info;
Huang Yingbf998152010-05-31 14:28:19 +08002262
Huang Ying77db5cb2010-10-08 16:24:15 +08002263 info.si_signo = SIGBUS;
2264 info.si_errno = 0;
2265 info.si_code = BUS_MCEERR_AR;
2266 info.si_addr = (void __user *)address;
2267 info.si_addr_lsb = PAGE_SHIFT;
2268
2269 send_sig_info(SIGBUS, &info, tsk);
Huang Yingbf998152010-05-31 14:28:19 +08002270}
2271
2272static int kvm_handle_bad_page(struct kvm *kvm, gfn_t gfn, pfn_t pfn)
2273{
2274 kvm_release_pfn_clean(pfn);
2275 if (is_hwpoison_pfn(pfn)) {
Huang Ying77db5cb2010-10-08 16:24:15 +08002276 kvm_send_hwpoison_signal(gfn_to_hva(kvm, gfn), current);
Huang Yingbf998152010-05-31 14:28:19 +08002277 return 0;
Gleb Natapovedba23e2010-07-07 20:16:45 +03002278 } else if (is_fault_pfn(pfn))
2279 return -EFAULT;
2280
Huang Yingbf998152010-05-31 14:28:19 +08002281 return 1;
2282}
2283
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002284static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
Xiao Guangrong060c2ab2010-11-12 14:49:11 +08002285 gva_t gva, pfn_t *pfn, bool write, bool *writable);
2286
2287static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002288 bool prefault)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002289{
2290 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002291 int level;
Anthony Liguori35149e22008-04-02 14:46:56 -05002292 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002293 unsigned long mmu_seq;
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002294 bool map_writable;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002295
Joerg Roedel852e3c12009-07-27 16:30:44 +02002296 level = mapping_level(vcpu, gfn);
2297
2298 /*
2299 * This path builds a PAE pagetable - so we can map 2mb pages at
2300 * maximum. Therefore check if the level is larger than that.
2301 */
2302 if (level > PT_DIRECTORY_LEVEL)
2303 level = PT_DIRECTORY_LEVEL;
2304
2305 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002306
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002307 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002308 smp_rmb();
Xiao Guangrong060c2ab2010-11-12 14:49:11 +08002309
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002310 if (try_async_pf(vcpu, prefault, gfn, v, &pfn, write, &map_writable))
Xiao Guangrong060c2ab2010-11-12 14:49:11 +08002311 return 0;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002312
Avi Kivityd196e342008-01-24 11:44:11 +02002313 /* mmio */
Huang Yingbf998152010-05-31 14:28:19 +08002314 if (is_error_pfn(pfn))
2315 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002316
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002317 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002318 if (mmu_notifier_retry(vcpu, mmu_seq))
2319 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02002320 kvm_mmu_free_some_pages(vcpu);
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002321 r = __direct_map(vcpu, v, write, map_writable, level, gfn, pfn,
2322 prefault);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002323 spin_unlock(&vcpu->kvm->mmu_lock);
2324
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002325
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002326 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002327
2328out_unlock:
2329 spin_unlock(&vcpu->kvm->mmu_lock);
2330 kvm_release_pfn_clean(pfn);
2331 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002332}
2333
2334
Avi Kivity17ac10a2007-01-05 16:36:40 -08002335static void mmu_free_roots(struct kvm_vcpu *vcpu)
2336{
2337 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002338 struct kvm_mmu_page *sp;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002339 LIST_HEAD(invalid_list);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002340
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002341 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002342 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002343 spin_lock(&vcpu->kvm->mmu_lock);
Joerg Roedel81407ca2010-09-10 17:31:00 +02002344 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL &&
2345 (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL ||
2346 vcpu->arch.mmu.direct_map)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002347 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002348
Avi Kivity4db35312007-11-21 15:28:32 +02002349 sp = page_header(root);
2350 --sp->root_count;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002351 if (!sp->root_count && sp->role.invalid) {
2352 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
2353 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
2354 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002355 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002356 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002357 return;
2358 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002359 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002360 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002361
Avi Kivity417726a2007-04-12 17:35:58 +03002362 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002363 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002364 sp = page_header(root);
2365 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002366 if (!sp->root_count && sp->role.invalid)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002367 kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
2368 &invalid_list);
Avi Kivity417726a2007-04-12 17:35:58 +03002369 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002370 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002371 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002372 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002373 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002374 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002375}
2376
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002377static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2378{
2379 int ret = 0;
2380
2381 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03002382 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002383 ret = 1;
2384 }
2385
2386 return ret;
2387}
2388
Joerg Roedel651dd372010-09-10 17:30:59 +02002389static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu)
2390{
2391 struct kvm_mmu_page *sp;
Avi Kivity7ebaf152010-10-03 18:51:39 +02002392 unsigned i;
Joerg Roedel651dd372010-09-10 17:30:59 +02002393
2394 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2395 spin_lock(&vcpu->kvm->mmu_lock);
2396 kvm_mmu_free_some_pages(vcpu);
2397 sp = kvm_mmu_get_page(vcpu, 0, 0, PT64_ROOT_LEVEL,
2398 1, ACC_ALL, NULL);
2399 ++sp->root_count;
2400 spin_unlock(&vcpu->kvm->mmu_lock);
2401 vcpu->arch.mmu.root_hpa = __pa(sp->spt);
2402 } else if (vcpu->arch.mmu.shadow_root_level == PT32E_ROOT_LEVEL) {
2403 for (i = 0; i < 4; ++i) {
2404 hpa_t root = vcpu->arch.mmu.pae_root[i];
2405
2406 ASSERT(!VALID_PAGE(root));
2407 spin_lock(&vcpu->kvm->mmu_lock);
2408 kvm_mmu_free_some_pages(vcpu);
Avi Kivity649497d2010-12-28 12:09:07 +02002409 sp = kvm_mmu_get_page(vcpu, i << (30 - PAGE_SHIFT),
2410 i << 30,
Joerg Roedel651dd372010-09-10 17:30:59 +02002411 PT32_ROOT_LEVEL, 1, ACC_ALL,
2412 NULL);
2413 root = __pa(sp->spt);
2414 ++sp->root_count;
2415 spin_unlock(&vcpu->kvm->mmu_lock);
2416 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Joerg Roedel651dd372010-09-10 17:30:59 +02002417 }
Xiao Guangrong62927572010-09-27 18:02:12 +08002418 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Joerg Roedel651dd372010-09-10 17:30:59 +02002419 } else
2420 BUG();
2421
2422 return 0;
2423}
2424
2425static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002426{
Avi Kivity4db35312007-11-21 15:28:32 +02002427 struct kvm_mmu_page *sp;
Joerg Roedel81407ca2010-09-10 17:31:00 +02002428 u64 pdptr, pm_mask;
2429 gfn_t root_gfn;
2430 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002431
Joerg Roedel5777ed32010-09-10 17:30:42 +02002432 root_gfn = vcpu->arch.mmu.get_cr3(vcpu) >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002433
Joerg Roedel651dd372010-09-10 17:30:59 +02002434 if (mmu_check_root(vcpu, root_gfn))
2435 return 1;
2436
2437 /*
2438 * Do we shadow a long mode page table? If so we need to
2439 * write-protect the guests page table root.
2440 */
2441 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002442 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002443
2444 ASSERT(!VALID_PAGE(root));
Joerg Roedel651dd372010-09-10 17:30:59 +02002445
Avi Kivity8facbbf2010-05-04 12:58:32 +03002446 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b62010-05-12 21:00:35 -03002447 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel651dd372010-09-10 17:30:59 +02002448 sp = kvm_mmu_get_page(vcpu, root_gfn, 0, PT64_ROOT_LEVEL,
2449 0, ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002450 root = __pa(sp->spt);
2451 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002452 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002453 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002454 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002455 }
Joerg Roedelf87f9282010-09-02 17:29:45 +02002456
Joerg Roedel651dd372010-09-10 17:30:59 +02002457 /*
2458 * We shadow a 32 bit page table. This may be a legacy 2-level
Joerg Roedel81407ca2010-09-10 17:31:00 +02002459 * or a PAE 3-level page table. In either case we need to be aware that
2460 * the shadow page table may be a PAE or a long mode page table.
Joerg Roedel651dd372010-09-10 17:30:59 +02002461 */
Joerg Roedel81407ca2010-09-10 17:31:00 +02002462 pm_mask = PT_PRESENT_MASK;
2463 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL)
2464 pm_mask |= PT_ACCESSED_MASK | PT_WRITABLE_MASK | PT_USER_MASK;
2465
Avi Kivity17ac10a2007-01-05 16:36:40 -08002466 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002467 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002468
2469 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002470 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Joerg Roedeld41d1892010-09-10 17:30:58 +02002471 pdptr = kvm_pdptr_read_mmu(vcpu, &vcpu->arch.mmu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002472 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002473 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002474 continue;
2475 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002476 root_gfn = pdptr >> PAGE_SHIFT;
Joerg Roedelf87f9282010-09-02 17:29:45 +02002477 if (mmu_check_root(vcpu, root_gfn))
2478 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002479 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002480 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b62010-05-12 21:00:35 -03002481 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02002482 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Joerg Roedel651dd372010-09-10 17:30:59 +02002483 PT32_ROOT_LEVEL, 0,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002484 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002485 root = __pa(sp->spt);
2486 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002487 spin_unlock(&vcpu->kvm->mmu_lock);
2488
Joerg Roedel81407ca2010-09-10 17:31:00 +02002489 vcpu->arch.mmu.pae_root[i] = root | pm_mask;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002490 }
Xiao Guangrong62927572010-09-27 18:02:12 +08002491 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Joerg Roedel81407ca2010-09-10 17:31:00 +02002492
2493 /*
2494 * If we shadow a 32 bit page table with a long mode page
2495 * table we enter this path.
2496 */
2497 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2498 if (vcpu->arch.mmu.lm_root == NULL) {
2499 /*
2500 * The additional page necessary for this is only
2501 * allocated on demand.
2502 */
2503
2504 u64 *lm_root;
2505
2506 lm_root = (void*)get_zeroed_page(GFP_KERNEL);
2507 if (lm_root == NULL)
2508 return 1;
2509
2510 lm_root[0] = __pa(vcpu->arch.mmu.pae_root) | pm_mask;
2511
2512 vcpu->arch.mmu.lm_root = lm_root;
2513 }
2514
2515 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.lm_root);
2516 }
2517
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002518 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002519}
2520
Joerg Roedel651dd372010-09-10 17:30:59 +02002521static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
2522{
2523 if (vcpu->arch.mmu.direct_map)
2524 return mmu_alloc_direct_roots(vcpu);
2525 else
2526 return mmu_alloc_shadow_roots(vcpu);
2527}
2528
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002529static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2530{
2531 int i;
2532 struct kvm_mmu_page *sp;
2533
Joerg Roedel81407ca2010-09-10 17:31:00 +02002534 if (vcpu->arch.mmu.direct_map)
2535 return;
2536
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002537 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2538 return;
Xiao Guangrong69030742010-09-27 18:09:29 +08002539
2540 trace_kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC);
Joerg Roedel81407ca2010-09-10 17:31:00 +02002541 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002542 hpa_t root = vcpu->arch.mmu.root_hpa;
2543 sp = page_header(root);
2544 mmu_sync_children(vcpu, sp);
Xiao Guangrong5054c0d2010-11-12 14:46:08 +08002545 trace_kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002546 return;
2547 }
2548 for (i = 0; i < 4; ++i) {
2549 hpa_t root = vcpu->arch.mmu.pae_root[i];
2550
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002551 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002552 root &= PT64_BASE_ADDR_MASK;
2553 sp = page_header(root);
2554 mmu_sync_children(vcpu, sp);
2555 }
2556 }
Xiao Guangrong69030742010-09-27 18:09:29 +08002557 trace_kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002558}
2559
2560void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2561{
2562 spin_lock(&vcpu->kvm->mmu_lock);
2563 mmu_sync_roots(vcpu);
2564 spin_unlock(&vcpu->kvm->mmu_lock);
2565}
2566
Gleb Natapov1871c602010-02-10 14:21:32 +02002567static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
Avi Kivityab9ae312010-11-22 17:53:26 +02002568 u32 access, struct x86_exception *exception)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002569{
Avi Kivityab9ae312010-11-22 17:53:26 +02002570 if (exception)
2571 exception->error_code = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002572 return vaddr;
2573}
2574
Joerg Roedel6539e732010-09-10 17:30:50 +02002575static gpa_t nonpaging_gva_to_gpa_nested(struct kvm_vcpu *vcpu, gva_t vaddr,
Avi Kivityab9ae312010-11-22 17:53:26 +02002576 u32 access,
2577 struct x86_exception *exception)
Joerg Roedel6539e732010-09-10 17:30:50 +02002578{
Avi Kivityab9ae312010-11-22 17:53:26 +02002579 if (exception)
2580 exception->error_code = 0;
Joerg Roedel6539e732010-09-10 17:30:50 +02002581 return vcpu->arch.nested_mmu.translate_gpa(vcpu, vaddr, access);
2582}
2583
Avi Kivity6aa8b732006-12-10 02:21:36 -08002584static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002585 u32 error_code, bool prefault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002586{
Avi Kivitye8332402007-12-09 18:43:00 +02002587 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002588 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002589
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002590 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002591 r = mmu_topup_memory_caches(vcpu);
2592 if (r)
2593 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002594
Avi Kivity6aa8b732006-12-10 02:21:36 -08002595 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002596 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002597
Avi Kivitye8332402007-12-09 18:43:00 +02002598 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002599
Avi Kivitye8332402007-12-09 18:43:00 +02002600 return nonpaging_map(vcpu, gva & PAGE_MASK,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002601 error_code & PFERR_WRITE_MASK, gfn, prefault);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002602}
2603
Jan Kiszka7e1fbea2010-10-20 15:18:02 +02002604static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn)
Gleb Natapovaf585b92010-10-14 11:22:46 +02002605{
2606 struct kvm_arch_async_pf arch;
Xiao Guangrongfb67e142010-12-07 10:35:25 +08002607
Gleb Natapov7c907052010-10-14 11:22:53 +02002608 arch.token = (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id;
Gleb Natapovaf585b92010-10-14 11:22:46 +02002609 arch.gfn = gfn;
Xiao Guangrongc4806ac2010-11-12 14:49:55 +08002610 arch.direct_map = vcpu->arch.mmu.direct_map;
Xiao Guangrongfb67e142010-12-07 10:35:25 +08002611 arch.cr3 = vcpu->arch.mmu.get_cr3(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002612
2613 return kvm_setup_async_pf(vcpu, gva, gfn, &arch);
2614}
2615
2616static bool can_do_async_pf(struct kvm_vcpu *vcpu)
2617{
2618 if (unlikely(!irqchip_in_kernel(vcpu->kvm) ||
2619 kvm_event_needs_reinjection(vcpu)))
2620 return false;
2621
2622 return kvm_x86_ops->interrupt_allowed(vcpu);
2623}
2624
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002625static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002626 gva_t gva, pfn_t *pfn, bool write, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02002627{
2628 bool async;
2629
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002630 *pfn = gfn_to_pfn_async(vcpu->kvm, gfn, &async, write, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002631
2632 if (!async)
2633 return false; /* *pfn has correct page already */
2634
2635 put_page(pfn_to_page(*pfn));
2636
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002637 if (!prefault && can_do_async_pf(vcpu)) {
Xiao Guangrongc9b263d2010-11-01 16:58:43 +08002638 trace_kvm_try_async_get_page(gva, gfn);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002639 if (kvm_find_async_pf_gfn(vcpu, gfn)) {
2640 trace_kvm_async_pf_doublefault(gva, gfn);
2641 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
2642 return true;
2643 } else if (kvm_arch_setup_async_pf(vcpu, gva, gfn))
2644 return true;
2645 }
2646
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002647 *pfn = gfn_to_pfn_prot(vcpu->kvm, gfn, write, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002648
2649 return false;
2650}
2651
Gleb Natapov56028d02010-10-17 18:13:42 +02002652static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, u32 error_code,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002653 bool prefault)
Joerg Roedelfb72d162008-02-07 13:47:44 +01002654{
Anthony Liguori35149e22008-04-02 14:46:56 -05002655 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002656 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002657 int level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002658 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002659 unsigned long mmu_seq;
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002660 int write = error_code & PFERR_WRITE_MASK;
2661 bool map_writable;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002662
2663 ASSERT(vcpu);
2664 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2665
2666 r = mmu_topup_memory_caches(vcpu);
2667 if (r)
2668 return r;
2669
Joerg Roedel852e3c12009-07-27 16:30:44 +02002670 level = mapping_level(vcpu, gfn);
2671
2672 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2673
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002674 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002675 smp_rmb();
Gleb Natapovaf585b92010-10-14 11:22:46 +02002676
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002677 if (try_async_pf(vcpu, prefault, gfn, gpa, &pfn, write, &map_writable))
Gleb Natapovaf585b92010-10-14 11:22:46 +02002678 return 0;
2679
2680 /* mmio */
Huang Yingbf998152010-05-31 14:28:19 +08002681 if (is_error_pfn(pfn))
2682 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002683 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002684 if (mmu_notifier_retry(vcpu, mmu_seq))
2685 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002686 kvm_mmu_free_some_pages(vcpu);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002687 r = __direct_map(vcpu, gpa, write, map_writable,
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002688 level, gfn, pfn, prefault);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002689 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002690
2691 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002692
2693out_unlock:
2694 spin_unlock(&vcpu->kvm->mmu_lock);
2695 kvm_release_pfn_clean(pfn);
2696 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002697}
2698
Avi Kivity6aa8b732006-12-10 02:21:36 -08002699static void nonpaging_free(struct kvm_vcpu *vcpu)
2700{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002701 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002702}
2703
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002704static int nonpaging_init_context(struct kvm_vcpu *vcpu,
2705 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002706{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002707 context->new_cr3 = nonpaging_new_cr3;
2708 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002709 context->gva_to_gpa = nonpaging_gva_to_gpa;
2710 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002711 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002712 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002713 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002714 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002715 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002716 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002717 context->direct_map = true;
Joerg Roedel2d48a982010-09-10 17:31:01 +02002718 context->nx = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002719 return 0;
2720}
2721
Avi Kivityd835dfe2007-11-21 02:57:59 +02002722void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002723{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002724 ++vcpu->stat.tlb_flush;
Avi Kivitya8eeb042010-05-10 12:34:53 +03002725 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002726}
2727
2728static void paging_new_cr3(struct kvm_vcpu *vcpu)
2729{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002730 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002731 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002732}
2733
Joerg Roedel5777ed32010-09-10 17:30:42 +02002734static unsigned long get_cr3(struct kvm_vcpu *vcpu)
2735{
2736 return vcpu->arch.cr3;
2737}
2738
Avi Kivity6389ee92010-11-29 16:12:30 +02002739static void inject_page_fault(struct kvm_vcpu *vcpu,
2740 struct x86_exception *fault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002741{
Avi Kivity6389ee92010-11-29 16:12:30 +02002742 vcpu->arch.mmu.inject_page_fault(vcpu, fault);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002743}
2744
Avi Kivity6aa8b732006-12-10 02:21:36 -08002745static void paging_free(struct kvm_vcpu *vcpu)
2746{
2747 nonpaging_free(vcpu);
2748}
2749
Joerg Roedel3241f222010-09-10 17:30:45 +02002750static bool is_rsvd_bits_set(struct kvm_mmu *mmu, u64 gpte, int level)
Dong, Eddie82725b22009-03-30 16:21:08 +08002751{
2752 int bit7;
2753
2754 bit7 = (gpte >> 7) & 1;
Joerg Roedel3241f222010-09-10 17:30:45 +02002755 return (gpte & mmu->rsvd_bits_mask[bit7][level-1]) != 0;
Dong, Eddie82725b22009-03-30 16:21:08 +08002756}
2757
Avi Kivity6aa8b732006-12-10 02:21:36 -08002758#define PTTYPE 64
2759#include "paging_tmpl.h"
2760#undef PTTYPE
2761
2762#define PTTYPE 32
2763#include "paging_tmpl.h"
2764#undef PTTYPE
2765
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002766static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
2767 struct kvm_mmu *context,
2768 int level)
Dong, Eddie82725b22009-03-30 16:21:08 +08002769{
Dong, Eddie82725b22009-03-30 16:21:08 +08002770 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2771 u64 exb_bit_rsvd = 0;
2772
Joerg Roedel2d48a982010-09-10 17:31:01 +02002773 if (!context->nx)
Dong, Eddie82725b22009-03-30 16:21:08 +08002774 exb_bit_rsvd = rsvd_bits(63, 63);
2775 switch (level) {
2776 case PT32_ROOT_LEVEL:
2777 /* no rsvd bits for 2 level 4K page table entries */
2778 context->rsvd_bits_mask[0][1] = 0;
2779 context->rsvd_bits_mask[0][0] = 0;
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002780 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2781
2782 if (!is_pse(vcpu)) {
2783 context->rsvd_bits_mask[1][1] = 0;
2784 break;
2785 }
2786
Dong, Eddie82725b22009-03-30 16:21:08 +08002787 if (is_cpuid_PSE36())
2788 /* 36bits PSE 4MB page */
2789 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2790 else
2791 /* 32 bits PSE 4MB page */
2792 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08002793 break;
2794 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002795 context->rsvd_bits_mask[0][2] =
2796 rsvd_bits(maxphyaddr, 63) |
2797 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002798 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002799 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002800 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2801 rsvd_bits(maxphyaddr, 62); /* PTE */
2802 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2803 rsvd_bits(maxphyaddr, 62) |
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 case PT64_ROOT_LEVEL:
2808 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2809 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2810 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2811 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2812 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002813 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002814 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2815 rsvd_bits(maxphyaddr, 51);
2816 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002817 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2818 rsvd_bits(maxphyaddr, 51) |
2819 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002820 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002821 rsvd_bits(maxphyaddr, 51) |
2822 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002823 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002824 break;
2825 }
2826}
2827
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002828static int paging64_init_context_common(struct kvm_vcpu *vcpu,
2829 struct kvm_mmu *context,
2830 int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002831{
Joerg Roedel2d48a982010-09-10 17:31:01 +02002832 context->nx = is_nx(vcpu);
2833
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002834 reset_rsvds_bits_mask(vcpu, context, level);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002835
2836 ASSERT(is_pae(vcpu));
2837 context->new_cr3 = paging_new_cr3;
2838 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002839 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002840 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002841 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002842 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002843 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002844 context->root_level = level;
2845 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002846 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002847 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002848 return 0;
2849}
2850
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002851static int paging64_init_context(struct kvm_vcpu *vcpu,
2852 struct kvm_mmu *context)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002853{
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002854 return paging64_init_context_common(vcpu, context, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002855}
2856
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002857static int paging32_init_context(struct kvm_vcpu *vcpu,
2858 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002859{
Joerg Roedel2d48a982010-09-10 17:31:01 +02002860 context->nx = false;
2861
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002862 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002863
2864 context->new_cr3 = paging_new_cr3;
2865 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002866 context->gva_to_gpa = paging32_gva_to_gpa;
2867 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002868 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002869 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002870 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002871 context->root_level = PT32_ROOT_LEVEL;
2872 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002873 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002874 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002875 return 0;
2876}
2877
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002878static int paging32E_init_context(struct kvm_vcpu *vcpu,
2879 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002880{
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002881 return paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882}
2883
Joerg Roedelfb72d162008-02-07 13:47:44 +01002884static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2885{
Joerg Roedel14dfe852010-09-10 17:30:49 +02002886 struct kvm_mmu *context = vcpu->arch.walk_mmu;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002887
2888 context->new_cr3 = nonpaging_new_cr3;
2889 context->page_fault = tdp_page_fault;
2890 context->free = nonpaging_free;
2891 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002892 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002893 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002894 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002895 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002896 context->direct_map = true;
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02002897 context->set_cr3 = kvm_x86_ops->set_tdp_cr3;
Joerg Roedel5777ed32010-09-10 17:30:42 +02002898 context->get_cr3 = get_cr3;
Joerg Roedelcb659db2010-09-10 17:30:43 +02002899 context->inject_page_fault = kvm_inject_page_fault;
Joerg Roedel2d48a982010-09-10 17:31:01 +02002900 context->nx = is_nx(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002901
2902 if (!is_paging(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002903 context->nx = false;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002904 context->gva_to_gpa = nonpaging_gva_to_gpa;
2905 context->root_level = 0;
2906 } else if (is_long_mode(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002907 context->nx = is_nx(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002908 reset_rsvds_bits_mask(vcpu, context, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002909 context->gva_to_gpa = paging64_gva_to_gpa;
2910 context->root_level = PT64_ROOT_LEVEL;
2911 } else if (is_pae(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002912 context->nx = is_nx(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002913 reset_rsvds_bits_mask(vcpu, context, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002914 context->gva_to_gpa = paging64_gva_to_gpa;
2915 context->root_level = PT32E_ROOT_LEVEL;
2916 } else {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002917 context->nx = false;
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002918 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002919 context->gva_to_gpa = paging32_gva_to_gpa;
2920 context->root_level = PT32_ROOT_LEVEL;
2921 }
2922
2923 return 0;
2924}
2925
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002926int kvm_init_shadow_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002927{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002928 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002929 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002930 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002931
2932 if (!is_paging(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002933 r = nonpaging_init_context(vcpu, context);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002934 else if (is_long_mode(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002935 r = paging64_init_context(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002936 else if (is_pae(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002937 r = paging32E_init_context(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002938 else
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002939 r = paging32_init_context(vcpu, context);
Avi Kivitya770f6f2008-12-21 19:20:09 +02002940
Avi Kivity5b7e0102010-04-14 19:20:03 +03002941 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
Joerg Roedelf43addd2010-09-10 17:30:40 +02002942 vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002943
2944 return r;
2945}
2946EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu);
2947
2948static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
2949{
Joerg Roedel14dfe852010-09-10 17:30:49 +02002950 int r = kvm_init_shadow_mmu(vcpu, vcpu->arch.walk_mmu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002951
Joerg Roedel14dfe852010-09-10 17:30:49 +02002952 vcpu->arch.walk_mmu->set_cr3 = kvm_x86_ops->set_cr3;
2953 vcpu->arch.walk_mmu->get_cr3 = get_cr3;
2954 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
Avi Kivitya770f6f2008-12-21 19:20:09 +02002955
2956 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002957}
2958
Joerg Roedel02f59dc2010-09-10 17:30:54 +02002959static int init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
2960{
2961 struct kvm_mmu *g_context = &vcpu->arch.nested_mmu;
2962
2963 g_context->get_cr3 = get_cr3;
2964 g_context->inject_page_fault = kvm_inject_page_fault;
2965
2966 /*
2967 * Note that arch.mmu.gva_to_gpa translates l2_gva to l1_gpa. The
2968 * translation of l2_gpa to l1_gpa addresses is done using the
2969 * arch.nested_mmu.gva_to_gpa function. Basically the gva_to_gpa
2970 * functions between mmu and nested_mmu are swapped.
2971 */
2972 if (!is_paging(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002973 g_context->nx = false;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02002974 g_context->root_level = 0;
2975 g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested;
2976 } else if (is_long_mode(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002977 g_context->nx = is_nx(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02002978 reset_rsvds_bits_mask(vcpu, g_context, PT64_ROOT_LEVEL);
2979 g_context->root_level = PT64_ROOT_LEVEL;
2980 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
2981 } else if (is_pae(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002982 g_context->nx = is_nx(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02002983 reset_rsvds_bits_mask(vcpu, g_context, PT32E_ROOT_LEVEL);
2984 g_context->root_level = PT32E_ROOT_LEVEL;
2985 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
2986 } else {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002987 g_context->nx = false;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02002988 reset_rsvds_bits_mask(vcpu, g_context, PT32_ROOT_LEVEL);
2989 g_context->root_level = PT32_ROOT_LEVEL;
2990 g_context->gva_to_gpa = paging32_gva_to_gpa_nested;
2991 }
2992
2993 return 0;
2994}
2995
Joerg Roedelfb72d162008-02-07 13:47:44 +01002996static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2997{
Anthony Liguori35149e22008-04-02 14:46:56 -05002998 vcpu->arch.update_pte.pfn = bad_pfn;
2999
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003000 if (mmu_is_nested(vcpu))
3001 return init_kvm_nested_mmu(vcpu);
3002 else if (tdp_enabled)
Joerg Roedelfb72d162008-02-07 13:47:44 +01003003 return init_kvm_tdp_mmu(vcpu);
3004 else
3005 return init_kvm_softmmu(vcpu);
3006}
3007
Avi Kivity6aa8b732006-12-10 02:21:36 -08003008static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
3009{
3010 ASSERT(vcpu);
Sheng Yang62ad0752010-05-12 16:40:41 +08003011 if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
3012 /* mmu.free() should set root_hpa = INVALID_PAGE */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003013 vcpu->arch.mmu.free(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003014}
3015
3016int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
3017{
Avi Kivity17c3ba92007-06-04 15:58:30 +03003018 destroy_kvm_mmu(vcpu);
3019 return init_kvm_mmu(vcpu);
3020}
Eddie Dong8668a3c2007-10-10 14:26:45 +08003021EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03003022
3023int kvm_mmu_load(struct kvm_vcpu *vcpu)
3024{
Avi Kivity714b93d2007-01-05 16:36:53 -08003025 int r;
3026
Avi Kivitye2dec932007-01-05 16:36:54 -08003027 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03003028 if (r)
3029 goto out;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003030 r = mmu_alloc_roots(vcpu);
Avi Kivity8facbbf2010-05-04 12:58:32 +03003031 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003032 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003033 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003034 if (r)
3035 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08003036 /* set_cr3() should ensure TLB has been flushed */
Joerg Roedelf43addd2010-09-10 17:30:40 +02003037 vcpu->arch.mmu.set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08003038out:
3039 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003040}
Avi Kivity17c3ba92007-06-04 15:58:30 +03003041EXPORT_SYMBOL_GPL(kvm_mmu_load);
3042
3043void kvm_mmu_unload(struct kvm_vcpu *vcpu)
3044{
3045 mmu_free_roots(vcpu);
3046}
Joerg Roedel4b161842010-09-10 17:31:03 +02003047EXPORT_SYMBOL_GPL(kvm_mmu_unload);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003048
Avi Kivity09072da2007-05-01 14:16:52 +03003049static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02003050 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02003051 u64 *spte)
3052{
3053 u64 pte;
3054 struct kvm_mmu_page *child;
3055
3056 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02003057 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03003058 if (is_last_spte(pte, sp->role.level))
Avi Kivitybe38d272010-06-06 14:31:27 +03003059 drop_spte(vcpu->kvm, spte, shadow_trap_nonpresent_pte);
Avi Kivityac1b7142007-03-08 17:13:32 +02003060 else {
3061 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03003062 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02003063 }
3064 }
Avi Kivityd555c332009-06-10 14:24:23 +03003065 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03003066 if (is_large_pte(pte))
3067 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02003068}
3069
Avi Kivity00284252007-05-01 16:53:31 +03003070static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02003071 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03003072 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02003073 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03003074{
Marcelo Tosatti30945382008-06-11 20:32:40 -03003075 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02003076 ++vcpu->kvm->stat.mmu_pde_zapped;
3077 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03003078 }
Avi Kivity00284252007-05-01 16:53:31 +03003079
Avi Kivity4cee5762007-11-18 16:37:07 +02003080 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity5b7e0102010-04-14 19:20:03 +03003081 if (!sp->role.cr4_pae)
Dong, Eddie489f1d62008-01-07 11:14:20 +02003082 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03003083 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02003084 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03003085}
3086
Avi Kivity79539ce2007-11-21 02:06:21 +02003087static bool need_remote_flush(u64 old, u64 new)
3088{
3089 if (!is_shadow_present_pte(old))
3090 return false;
3091 if (!is_shadow_present_pte(new))
3092 return true;
3093 if ((old ^ new) & PT64_BASE_ADDR_MASK)
3094 return true;
3095 old ^= PT64_NX_MASK;
3096 new ^= PT64_NX_MASK;
3097 return (old & ~new & PT64_PERM_MASK) != 0;
3098}
3099
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003100static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page,
3101 bool remote_flush, bool local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003102{
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003103 if (zap_page)
3104 return;
3105
3106 if (remote_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003107 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003108 else if (local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003109 kvm_mmu_flush_tlb(vcpu);
3110}
3111
Avi Kivity12b7d282007-09-23 14:10:49 +02003112static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
3113{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003114 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02003115
Sheng Yang7b523452008-04-25 21:13:50 +08003116 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02003117}
3118
Avi Kivityd7824ff2007-12-30 12:29:05 +02003119static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity72016f32010-03-15 13:59:53 +02003120 u64 gpte)
Avi Kivityd7824ff2007-12-30 12:29:05 +02003121{
3122 gfn_t gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05003123 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02003124
Avi Kivity43a37952009-06-10 14:12:05 +03003125 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02003126 return;
3127 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02003128
Andrea Arcangelie930bff2008-07-25 16:24:52 +02003129 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03003130 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05003131 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003132
Anthony Liguori35149e22008-04-02 14:46:56 -05003133 if (is_error_pfn(pfn)) {
3134 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02003135 return;
3136 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02003137 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05003138 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02003139}
3140
Avi Kivity1b7fcd32008-05-15 13:51:35 +03003141static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
3142{
3143 u64 *spte = vcpu->arch.last_pte_updated;
3144
3145 if (spte
3146 && vcpu->arch.last_pte_gfn == gfn
3147 && shadow_accessed_mask
3148 && !(*spte & shadow_accessed_mask)
3149 && is_shadow_present_pte(*spte))
3150 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
3151}
3152
Avi Kivity09072da2007-05-01 14:16:52 +03003153void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003154 const u8 *new, int bytes,
3155 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08003156{
Avi Kivity9b7a0322007-01-05 16:36:45 -08003157 gfn_t gfn = gpa >> PAGE_SHIFT;
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003158 union kvm_mmu_page_role mask = { .word = 0 };
Avi Kivity4db35312007-11-21 15:28:32 +02003159 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003160 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003161 LIST_HEAD(invalid_list);
Dong, Eddie489f1d62008-01-07 11:14:20 +02003162 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003163 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003164 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003165 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003166 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003167 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03003168 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003169 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08003170 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02003171 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02003172 int r;
Avi Kivity08e850c2010-03-15 13:59:57 +02003173 int invlpg_counter;
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003174 bool remote_flush, local_flush, zap_page;
3175
3176 zap_page = remote_flush = local_flush = false;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003177
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003178 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivity72016f32010-03-15 13:59:53 +02003179
Avi Kivity08e850c2010-03-15 13:59:57 +02003180 invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
3181
3182 /*
3183 * Assume that the pte write on a page table of the same type
3184 * as the current vcpu paging mode. This is nearly always true
3185 * (might be false while changing modes). Note it is verified later
3186 * by update_pte().
3187 */
3188 if ((is_pae(vcpu) && bytes == 4) || !new) {
3189 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
3190 if (is_pae(vcpu)) {
3191 gpa &= ~(gpa_t)7;
3192 bytes = 8;
3193 }
3194 r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
3195 if (r)
3196 gentry = 0;
3197 new = (const u8 *)&gentry;
3198 }
3199
Avi Kivity72016f32010-03-15 13:59:53 +02003200 switch (bytes) {
3201 case 4:
3202 gentry = *(const u32 *)new;
3203 break;
3204 case 8:
3205 gentry = *(const u64 *)new;
3206 break;
3207 default:
3208 gentry = 0;
3209 break;
3210 }
3211
Avi Kivity72016f32010-03-15 13:59:53 +02003212 mmu_guess_page_from_pte_write(vcpu, gpa, gentry);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003213 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity08e850c2010-03-15 13:59:57 +02003214 if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
3215 gentry = 0;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03003216 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02003217 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02003218 ++vcpu->kvm->stat.mmu_pte_write;
Xiao Guangrong8b1fe172010-08-30 18:22:53 +08003219 trace_kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE);
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003220 if (guest_initiated) {
3221 if (gfn == vcpu->arch.last_pt_write_gfn
3222 && !last_updated_pte_accessed(vcpu)) {
3223 ++vcpu->arch.last_pt_write_count;
3224 if (vcpu->arch.last_pt_write_count >= 3)
3225 flooded = 1;
3226 } else {
3227 vcpu->arch.last_pt_write_gfn = gfn;
3228 vcpu->arch.last_pt_write_count = 1;
3229 vcpu->arch.last_pte_updated = NULL;
3230 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08003231 }
Xiao Guangrong3246af02010-04-16 16:35:54 +08003232
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003233 mask.cr0_wp = mask.cr4_pae = mask.nxe = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003234 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node) {
Avi Kivity5b7e0102010-04-14 19:20:03 +03003235 pte_size = sp->role.cr4_pae ? 8 : 4;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003236 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03003237 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08003238 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003239 /*
3240 * Misaligned accesses are too much trouble to fix
3241 * up; also, they usually indicate a page is not used
3242 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08003243 *
3244 * If we're seeing too many writes to a page,
3245 * it may no longer be a page table, or we may be
3246 * forking, in which case it is better to unmap the
3247 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003248 */
3249 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02003250 gpa, bytes, sp->role.word);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003251 zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003252 &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02003253 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003254 continue;
3255 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08003256 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02003257 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02003258 npte = 1;
Avi Kivity5b7e0102010-04-14 19:20:03 +03003259 if (!sp->role.cr4_pae) {
Avi Kivityac1b7142007-03-08 17:13:32 +02003260 page_offset <<= 1; /* 32->64 */
3261 /*
3262 * A 32-bit pde maps 4MB while the shadow pdes map
3263 * only 2MB. So we need to double the offset again
3264 * and zap two pdes instead of one.
3265 */
3266 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03003267 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02003268 page_offset <<= 1;
3269 npte = 2;
3270 }
Avi Kivityfce06572007-05-01 16:44:05 +03003271 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003272 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02003273 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03003274 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003275 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003276 local_flush = true;
Avi Kivity4db35312007-11-21 15:28:32 +02003277 spte = &sp->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02003278 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02003279 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02003280 mmu_pte_write_zap_pte(vcpu, sp, spte);
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003281 if (gentry &&
3282 !((sp->role.word ^ vcpu->arch.mmu.base_role.word)
3283 & mask.word))
Avi Kivity72016f32010-03-15 13:59:53 +02003284 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003285 if (!remote_flush && need_remote_flush(entry, *spte))
3286 remote_flush = true;
Avi Kivityac1b7142007-03-08 17:13:32 +02003287 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003288 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08003289 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003290 mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003291 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong8b1fe172010-08-30 18:22:53 +08003292 trace_kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003293 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05003294 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
3295 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
3296 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02003297 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08003298}
3299
Avi Kivitya4360362007-01-05 16:36:45 -08003300int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
3301{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003302 gpa_t gpa;
3303 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08003304
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02003305 if (vcpu->arch.mmu.direct_map)
Avi Kivity60f24782009-08-27 13:37:06 +03003306 return 0;
3307
Gleb Natapov1871c602010-02-10 14:21:32 +02003308 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003309
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003310 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003311 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003312 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003313 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08003314}
Avi Kivity577bdc42008-07-19 08:57:05 +03003315EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08003316
Avi Kivity22d95b12007-09-14 20:26:06 +03003317void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08003318{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003319 LIST_HEAD(invalid_list);
Xiao Guangrong103ad252010-06-04 21:54:38 +08003320
Dave Hansene0df7b92010-08-19 18:11:05 -07003321 while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES &&
Izik Eidus3b80fff2009-07-28 15:26:58 -03003322 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02003323 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08003324
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003325 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02003326 struct kvm_mmu_page, link);
Dave Hansene0df7b92010-08-19 18:11:05 -07003327 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
Xiaotian Feng80b63fa2010-08-24 10:31:07 +08003328 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02003329 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08003330 }
3331}
Avi Kivityebeace82007-01-05 16:36:47 -08003332
Avi Kivity30677142007-10-28 18:48:59 +02003333int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
3334{
3335 int r;
3336 enum emulation_result er;
3337
Gleb Natapov56028d02010-10-17 18:13:42 +02003338 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code, false);
Avi Kivity30677142007-10-28 18:48:59 +02003339 if (r < 0)
3340 goto out;
3341
3342 if (!r) {
3343 r = 1;
3344 goto out;
3345 }
3346
Avi Kivityb733bfb2007-10-28 18:52:05 +02003347 r = mmu_topup_memory_caches(vcpu);
3348 if (r)
3349 goto out;
3350
Avi Kivity851ba692009-08-24 11:10:17 +03003351 er = emulate_instruction(vcpu, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02003352
3353 switch (er) {
3354 case EMULATE_DONE:
3355 return 1;
3356 case EMULATE_DO_MMIO:
3357 ++vcpu->stat.mmio_exits;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03003358 /* fall through */
Avi Kivity30677142007-10-28 18:48:59 +02003359 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03003360 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02003361 default:
3362 BUG();
3363 }
3364out:
Avi Kivity30677142007-10-28 18:48:59 +02003365 return r;
3366}
3367EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
3368
Marcelo Tosattia7052892008-09-23 13:18:35 -03003369void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
3370{
Marcelo Tosattia7052892008-09-23 13:18:35 -03003371 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003372 kvm_mmu_flush_tlb(vcpu);
3373 ++vcpu->stat.invlpg;
3374}
3375EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
3376
Joerg Roedel18552672008-02-07 13:47:41 +01003377void kvm_enable_tdp(void)
3378{
3379 tdp_enabled = true;
3380}
3381EXPORT_SYMBOL_GPL(kvm_enable_tdp);
3382
Joerg Roedel5f4cb662008-07-14 20:36:36 +02003383void kvm_disable_tdp(void)
3384{
3385 tdp_enabled = false;
3386}
3387EXPORT_SYMBOL_GPL(kvm_disable_tdp);
3388
Avi Kivity6aa8b732006-12-10 02:21:36 -08003389static void free_mmu_pages(struct kvm_vcpu *vcpu)
3390{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003391 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Joerg Roedel81407ca2010-09-10 17:31:00 +02003392 if (vcpu->arch.mmu.lm_root != NULL)
3393 free_page((unsigned long)vcpu->arch.mmu.lm_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003394}
3395
3396static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
3397{
Avi Kivity17ac10a2007-01-05 16:36:40 -08003398 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003399 int i;
3400
3401 ASSERT(vcpu);
3402
Avi Kivity17ac10a2007-01-05 16:36:40 -08003403 /*
3404 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
3405 * Therefore we need to allocate shadow page tables in the first
3406 * 4GB of memory, which happens to fit the DMA32 zone.
3407 */
3408 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
3409 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08003410 return -ENOMEM;
3411
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003412 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003413 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003414 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003415
Avi Kivity6aa8b732006-12-10 02:21:36 -08003416 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003417}
3418
Ingo Molnar8018c272006-12-29 16:50:01 -08003419int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003420{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003421 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003422 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003423
Ingo Molnar8018c272006-12-29 16:50:01 -08003424 return alloc_mmu_pages(vcpu);
3425}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003426
Ingo Molnar8018c272006-12-29 16:50:01 -08003427int kvm_mmu_setup(struct kvm_vcpu *vcpu)
3428{
3429 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003430 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08003431
Ingo Molnar8018c272006-12-29 16:50:01 -08003432 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003433}
3434
Avi Kivity90cb0522007-07-17 13:04:56 +03003435void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003436{
Avi Kivity4db35312007-11-21 15:28:32 +02003437 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003438
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003439 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003440 int i;
3441 u64 *pt;
3442
Sheng Yang291f26b2008-10-16 17:30:57 +08003443 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003444 continue;
3445
Avi Kivity4db35312007-11-21 15:28:32 +02003446 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003447 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
3448 /* avoid RMW */
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003449 if (is_writable_pte(pt[i]))
Takuya Yoshikawa700e1b12010-12-06 01:11:33 +09003450 update_spte(&pt[i], pt[i] & ~PT_WRITABLE_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003451 }
Avi Kivity171d5952008-08-27 16:40:51 +03003452 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003453}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003454
Avi Kivity90cb0522007-07-17 13:04:56 +03003455void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03003456{
Avi Kivity4db35312007-11-21 15:28:32 +02003457 struct kvm_mmu_page *sp, *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003458 LIST_HEAD(invalid_list);
Dor Laore0fa8262007-03-30 13:06:33 +03003459
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003460 spin_lock(&kvm->mmu_lock);
Xiao Guangrong3246af02010-04-16 16:35:54 +08003461restart:
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003462 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003463 if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list))
Xiao Guangrong3246af02010-04-16 16:35:54 +08003464 goto restart;
3465
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003466 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003467 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03003468}
3469
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003470static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm,
3471 struct list_head *invalid_list)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003472{
3473 struct kvm_mmu_page *page;
3474
3475 page = container_of(kvm->arch.active_mmu_pages.prev,
3476 struct kvm_mmu_page, link);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003477 return kvm_mmu_prepare_zap_page(kvm, page, invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003478}
3479
Dave Chinner7f8275d2010-07-19 14:56:17 +10003480static int mmu_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003481{
3482 struct kvm *kvm;
3483 struct kvm *kvm_freed = NULL;
Dave Hansen45221ab2010-08-19 18:11:37 -07003484
3485 if (nr_to_scan == 0)
3486 goto out;
Izik Eidus3ee16c82008-03-30 15:17:21 +03003487
3488 spin_lock(&kvm_lock);
3489
3490 list_for_each_entry(kvm, &vm_list, vm_list) {
Dave Hansen45221ab2010-08-19 18:11:37 -07003491 int idx, freed_pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003492 LIST_HEAD(invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003493
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003494 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003495 spin_lock(&kvm->mmu_lock);
Dave Hansen45221ab2010-08-19 18:11:37 -07003496 if (!kvm_freed && nr_to_scan > 0 &&
3497 kvm->arch.n_used_mmu_pages > 0) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003498 freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm,
3499 &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003500 kvm_freed = kvm;
3501 }
3502 nr_to_scan--;
3503
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003504 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003505 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003506 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003507 }
3508 if (kvm_freed)
3509 list_move_tail(&kvm_freed->vm_list, &vm_list);
3510
3511 spin_unlock(&kvm_lock);
3512
Dave Hansen45221ab2010-08-19 18:11:37 -07003513out:
3514 return percpu_counter_read_positive(&kvm_total_used_mmu_pages);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003515}
3516
3517static struct shrinker mmu_shrinker = {
3518 .shrink = mmu_shrink,
3519 .seeks = DEFAULT_SEEKS * 10,
3520};
3521
Ingo Molnar2ddfd202008-05-22 10:37:48 +02003522static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03003523{
3524 if (pte_chain_cache)
3525 kmem_cache_destroy(pte_chain_cache);
3526 if (rmap_desc_cache)
3527 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003528 if (mmu_page_header_cache)
3529 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003530}
3531
Izik Eidus3ee16c82008-03-30 15:17:21 +03003532void kvm_mmu_module_exit(void)
3533{
3534 mmu_destroy_caches();
Wei Yongjun45bf21a2010-08-23 16:13:15 +08003535 percpu_counter_destroy(&kvm_total_used_mmu_pages);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003536 unregister_shrinker(&mmu_shrinker);
3537}
3538
Avi Kivityb5a33a72007-04-15 16:31:09 +03003539int kvm_mmu_module_init(void)
3540{
3541 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
3542 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09003543 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003544 if (!pte_chain_cache)
3545 goto nomem;
3546 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
3547 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09003548 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003549 if (!rmap_desc_cache)
3550 goto nomem;
3551
Avi Kivityd3d25b02007-05-30 12:34:53 +03003552 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
3553 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09003554 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003555 if (!mmu_page_header_cache)
3556 goto nomem;
3557
Wei Yongjun45bf21a2010-08-23 16:13:15 +08003558 if (percpu_counter_init(&kvm_total_used_mmu_pages, 0))
3559 goto nomem;
3560
Izik Eidus3ee16c82008-03-30 15:17:21 +03003561 register_shrinker(&mmu_shrinker);
3562
Avi Kivityb5a33a72007-04-15 16:31:09 +03003563 return 0;
3564
3565nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03003566 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003567 return -ENOMEM;
3568}
3569
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003570/*
3571 * Caculate mmu pages needed for kvm.
3572 */
3573unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3574{
3575 int i;
3576 unsigned int nr_mmu_pages;
3577 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003578 struct kvm_memslots *slots;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003579
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003580 slots = kvm_memslots(kvm);
3581
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003582 for (i = 0; i < slots->nmemslots; i++)
3583 nr_pages += slots->memslots[i].npages;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003584
3585 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3586 nr_mmu_pages = max(nr_mmu_pages,
3587 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3588
3589 return nr_mmu_pages;
3590}
3591
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003592static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3593 unsigned len)
3594{
3595 if (len > buffer->len)
3596 return NULL;
3597 return buffer->ptr;
3598}
3599
3600static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3601 unsigned len)
3602{
3603 void *ret;
3604
3605 ret = pv_mmu_peek_buffer(buffer, len);
3606 if (!ret)
3607 return ret;
3608 buffer->ptr += len;
3609 buffer->len -= len;
3610 buffer->processed += len;
3611 return ret;
3612}
3613
3614static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3615 gpa_t addr, gpa_t value)
3616{
3617 int bytes = 8;
3618 int r;
3619
3620 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3621 bytes = 4;
3622
3623 r = mmu_topup_memory_caches(vcpu);
3624 if (r)
3625 return r;
3626
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003627 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003628 return -EFAULT;
3629
3630 return 1;
3631}
3632
3633static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3634{
Avi Kivity23902182010-06-10 17:02:16 +03003635 (void)kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003636 return 1;
3637}
3638
3639static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3640{
3641 spin_lock(&vcpu->kvm->mmu_lock);
3642 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3643 spin_unlock(&vcpu->kvm->mmu_lock);
3644 return 1;
3645}
3646
3647static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3648 struct kvm_pv_mmu_op_buffer *buffer)
3649{
3650 struct kvm_mmu_op_header *header;
3651
3652 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3653 if (!header)
3654 return 0;
3655 switch (header->op) {
3656 case KVM_MMU_OP_WRITE_PTE: {
3657 struct kvm_mmu_op_write_pte *wpte;
3658
3659 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3660 if (!wpte)
3661 return 0;
3662 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3663 wpte->pte_val);
3664 }
3665 case KVM_MMU_OP_FLUSH_TLB: {
3666 struct kvm_mmu_op_flush_tlb *ftlb;
3667
3668 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3669 if (!ftlb)
3670 return 0;
3671 return kvm_pv_mmu_flush_tlb(vcpu);
3672 }
3673 case KVM_MMU_OP_RELEASE_PT: {
3674 struct kvm_mmu_op_release_pt *rpt;
3675
3676 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3677 if (!rpt)
3678 return 0;
3679 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3680 }
3681 default: return 0;
3682 }
3683}
3684
3685int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3686 gpa_t addr, unsigned long *ret)
3687{
3688 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003689 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003690
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003691 buffer->ptr = buffer->buf;
3692 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3693 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003694
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003695 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003696 if (r)
3697 goto out;
3698
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003699 while (buffer->len) {
3700 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003701 if (r < 0)
3702 goto out;
3703 if (r == 0)
3704 break;
3705 }
3706
3707 r = 1;
3708out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003709 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003710 return r;
3711}
3712
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003713int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3714{
3715 struct kvm_shadow_walk_iterator iterator;
3716 int nr_sptes = 0;
3717
3718 spin_lock(&vcpu->kvm->mmu_lock);
3719 for_each_shadow_entry(vcpu, addr, iterator) {
3720 sptes[iterator.level-1] = *iterator.sptep;
3721 nr_sptes++;
3722 if (!is_shadow_present_pte(*iterator.sptep))
3723 break;
3724 }
3725 spin_unlock(&vcpu->kvm->mmu_lock);
3726
3727 return nr_sptes;
3728}
3729EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3730
Xiao Guangrong8b1fe172010-08-30 18:22:53 +08003731#ifdef CONFIG_KVM_MMU_AUDIT
Xiao Guangrong2f4f3372010-08-30 18:24:10 +08003732#include "mmu_audit.c"
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08003733#else
3734static void mmu_audit_disable(void) { }
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003735#endif
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08003736
3737void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
3738{
3739 ASSERT(vcpu);
3740
3741 destroy_kvm_mmu(vcpu);
3742 free_mmu_pages(vcpu);
3743 mmu_free_memory_caches(vcpu);
3744 mmu_audit_disable();
3745}