blob: 52645e342db1e7716c99a5d2cb2db6a604082a81 [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 Guangrong8672b7212010-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 Guangrong8672b7212010-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{
Xiao Guangrong842f22e2011-03-04 19:01:10 +0800382 void *page;
Avi Kivityc1158e62007-07-20 08:18:27 +0300383
384 if (cache->nobjs >= min)
385 return 0;
386 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Xiao Guangrong842f22e2011-03-04 19:01:10 +0800387 page = (void *)__get_free_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300388 if (!page)
389 return -ENOMEM;
Xiao Guangrong842f22e2011-03-04 19:01:10 +0800390 cache->objects[cache->nobjs++] = page;
Avi Kivityc1158e62007-07-20 08:18:27 +0300391 }
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 Kivitya1f4d3952010-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 Kivitya1f4d3952010-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 Kivitya1f4d3952010-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
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800557static bool mapping_level_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t large_gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300558{
559 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300560 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
561 if (slot && slot->dirty_bitmap)
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800562 return true;
563 return false;
564}
565
566static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
567{
568 int host_level, level, max_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300569
Joerg Roedeld25797b2009-07-27 16:30:43 +0200570 host_level = host_mapping_level(vcpu->kvm, large_gfn);
571
572 if (host_level == PT_PAGE_TABLE_LEVEL)
573 return host_level;
574
Sheng Yang878403b2010-01-05 19:02:29 +0800575 max_level = kvm_x86_ops->get_lpage_level() < host_level ?
576 kvm_x86_ops->get_lpage_level() : host_level;
577
578 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200579 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
580 break;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200581
582 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300583}
584
585/*
Izik Eidus290fc382007-09-27 14:11:22 +0200586 * Take gfn and return the reverse mapping to it.
Izik Eidus290fc382007-09-27 14:11:22 +0200587 */
588
Joerg Roedel44ad9942009-07-27 16:30:42 +0200589static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
Izik Eidus290fc382007-09-27 14:11:22 +0200590{
591 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900592 struct kvm_lpage_info *linfo;
Izik Eidus290fc382007-09-27 14:11:22 +0200593
594 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200595 if (likely(level == PT_PAGE_TABLE_LEVEL))
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300596 return &slot->rmap[gfn - slot->base_gfn];
597
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900598 linfo = lpage_info_slot(gfn, slot, level);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300599
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900600 return &linfo->rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200601}
602
603/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800604 * Reverse mapping data structures:
605 *
Izik Eidus290fc382007-09-27 14:11:22 +0200606 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
607 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800608 *
Izik Eidus290fc382007-09-27 14:11:22 +0200609 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
610 * containing more mappings.
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300611 *
612 * Returns the number of rmap entries before the spte was added or zero if
613 * the spte was not added.
614 *
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800615 */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200616static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800617{
Avi Kivity4db35312007-11-21 15:28:32 +0200618 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800619 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200620 unsigned long *rmapp;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300621 int i, count = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800622
Avi Kivity43a37952009-06-10 14:12:05 +0300623 if (!is_rmap_spte(*spte))
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300624 return count;
Avi Kivity4db35312007-11-21 15:28:32 +0200625 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800626 kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200627 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200628 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800629 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200630 *rmapp = (unsigned long)spte;
631 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800632 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800633 desc = mmu_alloc_rmap_desc(vcpu);
Avi Kivityd555c332009-06-10 14:24:23 +0300634 desc->sptes[0] = (u64 *)*rmapp;
635 desc->sptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200636 *rmapp = (unsigned long)desc | 1;
Hillf Dantoncb16a7b2010-09-18 08:41:02 +0800637 ++count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800638 } else {
639 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200640 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivityd555c332009-06-10 14:24:23 +0300641 while (desc->sptes[RMAP_EXT-1] && desc->more) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800642 desc = desc->more;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300643 count += RMAP_EXT;
644 }
Avi Kivityd555c332009-06-10 14:24:23 +0300645 if (desc->sptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800646 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800647 desc = desc->more;
648 }
Avi Kivityd555c332009-06-10 14:24:23 +0300649 for (i = 0; desc->sptes[i]; ++i)
Hillf Dantoncb16a7b2010-09-18 08:41:02 +0800650 ++count;
Avi Kivityd555c332009-06-10 14:24:23 +0300651 desc->sptes[i] = spte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800652 }
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300653 return count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800654}
655
Izik Eidus290fc382007-09-27 14:11:22 +0200656static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800657 struct kvm_rmap_desc *desc,
658 int i,
659 struct kvm_rmap_desc *prev_desc)
660{
661 int j;
662
Avi Kivityd555c332009-06-10 14:24:23 +0300663 for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800664 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300665 desc->sptes[i] = desc->sptes[j];
666 desc->sptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800667 if (j != 0)
668 return;
669 if (!prev_desc && !desc->more)
Avi Kivityd555c332009-06-10 14:24:23 +0300670 *rmapp = (unsigned long)desc->sptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800671 else
672 if (prev_desc)
673 prev_desc->more = desc->more;
674 else
Izik Eidus290fc382007-09-27 14:11:22 +0200675 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300676 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800677}
678
Izik Eidus290fc382007-09-27 14:11:22 +0200679static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800680{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800681 struct kvm_rmap_desc *desc;
682 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200683 struct kvm_mmu_page *sp;
Lai Jiangshan2032a932010-05-26 16:49:59 +0800684 gfn_t gfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200685 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800686 int i;
687
Avi Kivity4db35312007-11-21 15:28:32 +0200688 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800689 gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt);
690 rmapp = gfn_to_rmap(kvm, gfn, sp->role.level);
Izik Eidus290fc382007-09-27 14:11:22 +0200691 if (!*rmapp) {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800692 printk(KERN_ERR "rmap_remove: %p 0->BUG\n", spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800693 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200694 } else if (!(*rmapp & 1)) {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800695 rmap_printk("rmap_remove: %p 1->0\n", spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200696 if ((u64 *)*rmapp != spte) {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800697 printk(KERN_ERR "rmap_remove: %p 1->BUG\n", spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800698 BUG();
699 }
Izik Eidus290fc382007-09-27 14:11:22 +0200700 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800701 } else {
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800702 rmap_printk("rmap_remove: %p many->many\n", spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200703 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800704 prev_desc = NULL;
705 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300706 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
707 if (desc->sptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200708 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800709 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800710 prev_desc);
711 return;
712 }
713 prev_desc = desc;
714 desc = desc->more;
715 }
Xiao Guangrong19ada5c2010-07-27 11:21:18 +0800716 pr_err("rmap_remove: %p many->many\n", spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800717 BUG();
718 }
719}
720
Marcelo Tosattieb45fda2010-10-25 11:58:22 -0200721static int set_spte_track_bits(u64 *sptep, u64 new_spte)
Avi Kivitybe38d272010-06-06 14:31:27 +0300722{
Avi Kivityce061862010-06-06 14:38:12 +0300723 pfn_t pfn;
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800724 u64 old_spte = *sptep;
Avi Kivityce061862010-06-06 14:38:12 +0300725
Xiao Guangrong8672b7212010-08-02 16:14:04 +0800726 if (!spte_has_volatile_bits(old_spte))
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800727 __set_spte(sptep, new_spte);
Xiao Guangrong8672b7212010-08-02 16:14:04 +0800728 else
Xiao Guangrong9a3aad72010-07-16 11:30:18 +0800729 old_spte = __xchg_spte(sptep, new_spte);
730
Avi Kivitya9221dd2010-06-06 14:48:06 +0300731 if (!is_rmap_spte(old_spte))
Marcelo Tosattieb45fda2010-10-25 11:58:22 -0200732 return 0;
Xiao Guangrong8672b7212010-08-02 16:14:04 +0800733
Avi Kivitya9221dd2010-06-06 14:48:06 +0300734 pfn = spte_to_pfn(old_spte);
Xiao Guangrongdaa3db62010-07-16 11:23:04 +0800735 if (!shadow_accessed_mask || old_spte & shadow_accessed_mask)
Avi Kivityce061862010-06-06 14:38:12 +0300736 kvm_set_pfn_accessed(pfn);
Xiao Guangrong41327792010-08-02 16:15:08 +0800737 if (!shadow_dirty_mask || (old_spte & shadow_dirty_mask))
Avi Kivityce061862010-06-06 14:38:12 +0300738 kvm_set_pfn_dirty(pfn);
Marcelo Tosattieb45fda2010-10-25 11:58:22 -0200739 return 1;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800740}
741
742static void drop_spte(struct kvm *kvm, u64 *sptep, u64 new_spte)
743{
Marcelo Tosattieb45fda2010-10-25 11:58:22 -0200744 if (set_spte_track_bits(sptep, new_spte))
745 rmap_remove(kvm, sptep);
Avi Kivitybe38d272010-06-06 14:31:27 +0300746}
747
Izik Eidus98348e92007-10-16 14:42:30 +0200748static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800749{
Avi Kivity374cbac2007-01-05 16:36:43 -0800750 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200751 u64 *prev_spte;
752 int i;
753
754 if (!*rmapp)
755 return NULL;
756 else if (!(*rmapp & 1)) {
757 if (!spte)
758 return (u64 *)*rmapp;
759 return NULL;
760 }
761 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Izik Eidus98348e92007-10-16 14:42:30 +0200762 prev_spte = NULL;
763 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300764 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
Izik Eidus98348e92007-10-16 14:42:30 +0200765 if (prev_spte == spte)
Avi Kivityd555c332009-06-10 14:24:23 +0300766 return desc->sptes[i];
767 prev_spte = desc->sptes[i];
Izik Eidus98348e92007-10-16 14:42:30 +0200768 }
769 desc = desc->more;
770 }
771 return NULL;
772}
773
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200774static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200775{
Izik Eidus290fc382007-09-27 14:11:22 +0200776 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800777 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200778 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800779
Joerg Roedel44ad9942009-07-27 16:30:42 +0200780 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
Avi Kivity374cbac2007-01-05 16:36:43 -0800781
Izik Eidus98348e92007-10-16 14:42:30 +0200782 spte = rmap_next(kvm, rmapp, NULL);
783 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800784 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800785 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800786 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900787 if (is_writable_pte(*spte)) {
Avi Kivityb79b93f2010-06-06 15:46:44 +0300788 update_spte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800789 write_protected = 1;
790 }
Izik Eidus9647c142007-10-16 14:43:46 +0200791 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800792 }
Izik Eidus855149a2008-03-20 18:17:24 +0200793
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300794 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200795 for (i = PT_DIRECTORY_LEVEL;
796 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
797 rmapp = gfn_to_rmap(kvm, gfn, i);
798 spte = rmap_next(kvm, rmapp, NULL);
799 while (spte) {
800 BUG_ON(!spte);
801 BUG_ON(!(*spte & PT_PRESENT_MASK));
802 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
803 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900804 if (is_writable_pte(*spte)) {
Avi Kivitybe38d272010-06-06 14:31:27 +0300805 drop_spte(kvm, spte,
806 shadow_trap_nonpresent_pte);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200807 --kvm->stat.lpages;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200808 spte = NULL;
809 write_protected = 1;
810 }
811 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300812 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300813 }
814
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200815 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800816}
817
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000818static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
819 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200820{
821 u64 *spte;
822 int need_tlb_flush = 0;
823
824 while ((spte = rmap_next(kvm, rmapp, NULL))) {
825 BUG_ON(!(*spte & PT_PRESENT_MASK));
826 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
Avi Kivitybe38d272010-06-06 14:31:27 +0300827 drop_spte(kvm, spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200828 need_tlb_flush = 1;
829 }
830 return need_tlb_flush;
831}
832
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000833static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
834 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +0300835{
836 int need_flush = 0;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800837 u64 *spte, new_spte;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300838 pte_t *ptep = (pte_t *)data;
839 pfn_t new_pfn;
840
841 WARN_ON(pte_huge(*ptep));
842 new_pfn = pte_pfn(*ptep);
843 spte = rmap_next(kvm, rmapp, NULL);
844 while (spte) {
845 BUG_ON(!is_shadow_present_pte(*spte));
846 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
847 need_flush = 1;
848 if (pte_write(*ptep)) {
Avi Kivitybe38d272010-06-06 14:31:27 +0300849 drop_spte(kvm, spte, shadow_trap_nonpresent_pte);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300850 spte = rmap_next(kvm, rmapp, NULL);
851 } else {
852 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
853 new_spte |= (u64)new_pfn << PAGE_SHIFT;
854
855 new_spte &= ~PT_WRITABLE_MASK;
856 new_spte &= ~SPTE_HOST_WRITEABLE;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300857 new_spte &= ~shadow_accessed_mask;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800858 set_spte_track_bits(spte, new_spte);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300859 spte = rmap_next(kvm, rmapp, spte);
860 }
861 }
862 if (need_flush)
863 kvm_flush_remote_tlbs(kvm);
864
865 return 0;
866}
867
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000868static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
869 unsigned long data,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300870 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000871 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200872{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200873 int i, j;
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200874 int ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200875 int retval = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200876 struct kvm_memslots *slots;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200877
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800878 slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200879
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200880 for (i = 0; i < slots->nmemslots; i++) {
881 struct kvm_memory_slot *memslot = &slots->memslots[i];
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200882 unsigned long start = memslot->userspace_addr;
883 unsigned long end;
884
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200885 end = start + (memslot->npages << PAGE_SHIFT);
886 if (hva >= start && hva < end) {
887 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900888 gfn_t gfn = memslot->base_gfn + gfn_offset;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200889
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200890 ret = handler(kvm, &memslot->rmap[gfn_offset], data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200891
892 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900893 struct kvm_lpage_info *linfo;
Andrea Arcangeli6e3e2432010-07-16 11:52:55 +0200894
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900895 linfo = lpage_info_slot(gfn, memslot,
896 PT_DIRECTORY_LEVEL + j);
897 ret |= handler(kvm, &linfo->rmap_pde, data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200898 }
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200899 trace_kvm_age_page(hva, memslot, ret);
900 retval |= ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200901 }
902 }
903
904 return retval;
905}
906
907int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
908{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300909 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200910}
911
Izik Eidus3da0dd42009-09-23 21:47:18 +0300912void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
913{
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000914 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300915}
916
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000917static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
918 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200919{
920 u64 *spte;
921 int young = 0;
922
Rik van Riel6316e1c2010-02-03 16:11:03 -0500923 /*
924 * Emulate the accessed bit for EPT, by checking if this page has
925 * an EPT mapping, and clearing it if it does. On the next access,
926 * a new EPT mapping will be established.
927 * This has some overhead, but not as much as the cost of swapping
928 * out actively used pages or breaking up actively used hugepages.
929 */
Sheng Yang534e38b2008-09-08 15:12:30 +0800930 if (!shadow_accessed_mask)
Rik van Riel6316e1c2010-02-03 16:11:03 -0500931 return kvm_unmap_rmapp(kvm, rmapp, data);
Sheng Yang534e38b2008-09-08 15:12:30 +0800932
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200933 spte = rmap_next(kvm, rmapp, NULL);
934 while (spte) {
935 int _young;
936 u64 _spte = *spte;
937 BUG_ON(!(_spte & PT_PRESENT_MASK));
938 _young = _spte & PT_ACCESSED_MASK;
939 if (_young) {
940 young = 1;
941 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
942 }
943 spte = rmap_next(kvm, rmapp, spte);
944 }
945 return young;
946}
947
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800948static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
949 unsigned long data)
950{
951 u64 *spte;
952 int young = 0;
953
954 /*
955 * If there's no access bit in the secondary pte set by the
956 * hardware it's up to gup-fast/gup to set the access bit in
957 * the primary pte or in the page structure.
958 */
959 if (!shadow_accessed_mask)
960 goto out;
961
962 spte = rmap_next(kvm, rmapp, NULL);
963 while (spte) {
964 u64 _spte = *spte;
965 BUG_ON(!(_spte & PT_PRESENT_MASK));
966 young = _spte & PT_ACCESSED_MASK;
967 if (young) {
968 young = 1;
969 break;
970 }
971 spte = rmap_next(kvm, rmapp, spte);
972 }
973out:
974 return young;
975}
976
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300977#define RMAP_RECYCLE_THRESHOLD 1000
978
Joerg Roedel852e3c12009-07-27 16:30:44 +0200979static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300980{
981 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200982 struct kvm_mmu_page *sp;
983
984 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300985
Joerg Roedel852e3c12009-07-27 16:30:44 +0200986 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300987
Izik Eidus3da0dd42009-09-23 21:47:18 +0300988 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300989 kvm_flush_remote_tlbs(vcpu->kvm);
990}
991
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200992int kvm_age_hva(struct kvm *kvm, unsigned long hva)
993{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300994 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200995}
996
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800997int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
998{
999 return kvm_handle_hva(kvm, hva, 0, kvm_test_age_rmapp);
1000}
1001
Yaozu Dongd6c69ee2007-04-25 14:17:25 +08001002#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +03001003static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001004{
Avi Kivity139bdb22007-01-05 16:36:50 -08001005 u64 *pos;
1006 u64 *end;
1007
Avi Kivity47ad8e62007-05-06 15:50:58 +03001008 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +03001009 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001010 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -08001011 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -08001013 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014 return 1;
1015}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +08001016#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017
Dave Hansen45221ab2010-08-19 18:11:37 -07001018/*
1019 * This value is the sum of all of the kvm instances's
1020 * kvm->arch.n_used_mmu_pages values. We need a global,
1021 * aggregate version in order to make the slab shrinker
1022 * faster
1023 */
1024static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, int nr)
1025{
1026 kvm->arch.n_used_mmu_pages += nr;
1027 percpu_counter_add(&kvm_total_used_mmu_pages, nr);
1028}
1029
Avi Kivity4db35312007-11-21 15:28:32 +02001030static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -08001031{
Avi Kivity4db35312007-11-21 15:28:32 +02001032 ASSERT(is_empty_shadow_page(sp->spt));
Xiao Guangrong77758342010-06-04 21:53:54 +08001033 hlist_del(&sp->hash_link);
Avi Kivity4db35312007-11-21 15:28:32 +02001034 list_del(&sp->link);
Xiao Guangrong842f22e2011-03-04 19:01:10 +08001035 free_page((unsigned long)sp->spt);
Lai Jiangshan2032a932010-05-26 16:49:59 +08001036 if (!sp->role.direct)
Xiao Guangrong842f22e2011-03-04 19:01:10 +08001037 free_page((unsigned long)sp->gfns);
Xiao Guangronge8ad9a72010-05-13 10:06:02 +08001038 kmem_cache_free(mmu_page_header_cache, sp);
Dave Hansen45221ab2010-08-19 18:11:37 -07001039 kvm_mod_used_mmu_pages(kvm, -1);
Avi Kivity260746c2007-01-05 16:36:49 -08001040}
1041
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001042static unsigned kvm_page_table_hashfn(gfn_t gfn)
1043{
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001044 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001045}
1046
Avi Kivity25c0de22007-01-05 16:36:42 -08001047static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
Lai Jiangshan2032a932010-05-26 16:49:59 +08001048 u64 *parent_pte, int direct)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001049{
Avi Kivity4db35312007-11-21 15:28:32 +02001050 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001051
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001052 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
1053 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Lai Jiangshan2032a932010-05-26 16:49:59 +08001054 if (!direct)
1055 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache,
1056 PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +02001057 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001058 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Sheng Yang291f26b2008-10-16 17:30:57 +08001059 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +02001060 sp->multimapped = 0;
1061 sp->parent_pte = parent_pte;
Dave Hansen45221ab2010-08-19 18:11:37 -07001062 kvm_mod_used_mmu_pages(vcpu->kvm, +1);
Avi Kivity4db35312007-11-21 15:28:32 +02001063 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064}
1065
Avi Kivity714b93d2007-01-05 16:36:53 -08001066static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001067 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001068{
1069 struct kvm_pte_chain *pte_chain;
1070 struct hlist_node *node;
1071 int i;
1072
1073 if (!parent_pte)
1074 return;
Avi Kivity4db35312007-11-21 15:28:32 +02001075 if (!sp->multimapped) {
1076 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001077
1078 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +02001079 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001080 return;
1081 }
Avi Kivity4db35312007-11-21 15:28:32 +02001082 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -08001083 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02001084 INIT_HLIST_HEAD(&sp->parent_ptes);
1085 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001086 pte_chain->parent_ptes[0] = old;
1087 }
Avi Kivity4db35312007-11-21 15:28:32 +02001088 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001089 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
1090 continue;
1091 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
1092 if (!pte_chain->parent_ptes[i]) {
1093 pte_chain->parent_ptes[i] = parent_pte;
1094 return;
1095 }
1096 }
Avi Kivity714b93d2007-01-05 16:36:53 -08001097 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001098 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +02001099 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001100 pte_chain->parent_ptes[0] = parent_pte;
1101}
1102
Avi Kivity4db35312007-11-21 15:28:32 +02001103static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001104 u64 *parent_pte)
1105{
1106 struct kvm_pte_chain *pte_chain;
1107 struct hlist_node *node;
1108 int i;
1109
Avi Kivity4db35312007-11-21 15:28:32 +02001110 if (!sp->multimapped) {
1111 BUG_ON(sp->parent_pte != parent_pte);
1112 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001113 return;
1114 }
Avi Kivity4db35312007-11-21 15:28:32 +02001115 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001116 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
1117 if (!pte_chain->parent_ptes[i])
1118 break;
1119 if (pte_chain->parent_ptes[i] != parent_pte)
1120 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001121 while (i + 1 < NR_PTE_CHAIN_ENTRIES
1122 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001123 pte_chain->parent_ptes[i]
1124 = pte_chain->parent_ptes[i + 1];
1125 ++i;
1126 }
1127 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001128 if (i == 0) {
1129 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +03001130 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +02001131 if (hlist_empty(&sp->parent_ptes)) {
1132 sp->multimapped = 0;
1133 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001134 }
1135 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001136 return;
1137 }
1138 BUG();
1139}
1140
Xiao Guangrong6b184932010-04-16 21:29:17 +08001141static void mmu_parent_walk(struct kvm_mmu_page *sp, mmu_parent_walk_fn fn)
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -03001142{
1143 struct kvm_pte_chain *pte_chain;
1144 struct hlist_node *node;
1145 struct kvm_mmu_page *parent_sp;
1146 int i;
1147
1148 if (!sp->multimapped && sp->parent_pte) {
1149 parent_sp = page_header(__pa(sp->parent_pte));
Xiao Guangrong1047df12010-06-11 21:35:15 +08001150 fn(parent_sp, sp->parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001151 return;
1152 }
1153
1154 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
1155 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
Xiao Guangrong1047df12010-06-11 21:35:15 +08001156 u64 *spte = pte_chain->parent_ptes[i];
1157
1158 if (!spte)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001159 break;
Xiao Guangrong1047df12010-06-11 21:35:15 +08001160 parent_sp = page_header(__pa(spte));
1161 fn(parent_sp, spte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001162 }
1163}
1164
Xiao Guangrong1047df12010-06-11 21:35:15 +08001165static void mark_unsync(struct kvm_mmu_page *sp, u64 *spte);
Xiao Guangrong6b184932010-04-16 21:29:17 +08001166static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001167{
Xiao Guangrong1047df12010-06-11 21:35:15 +08001168 mmu_parent_walk(sp, mark_unsync);
1169}
1170
1171static void mark_unsync(struct kvm_mmu_page *sp, u64 *spte)
1172{
1173 unsigned int index;
1174
1175 index = spte - sp->spt;
1176 if (__test_and_set_bit(index, sp->unsync_child_bitmap))
1177 return;
1178 if (sp->unsync_children++)
1179 return;
1180 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001181}
1182
Avi Kivityd761a502008-05-29 14:55:03 +03001183static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1184 struct kvm_mmu_page *sp)
1185{
1186 int i;
1187
1188 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1189 sp->spt[i] = shadow_trap_nonpresent_pte;
1190}
1191
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001192static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001193 struct kvm_mmu_page *sp)
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001194{
1195 return 1;
1196}
1197
Marcelo Tosattia7052892008-09-23 13:18:35 -03001198static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1199{
1200}
1201
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001202#define KVM_PAGE_ARRAY_NR 16
1203
1204struct kvm_mmu_pages {
1205 struct mmu_page_and_offset {
1206 struct kvm_mmu_page *sp;
1207 unsigned int idx;
1208 } page[KVM_PAGE_ARRAY_NR];
1209 unsigned int nr;
1210};
1211
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001212#define for_each_unsync_children(bitmap, idx) \
1213 for (idx = find_first_bit(bitmap, 512); \
1214 idx < 512; \
1215 idx = find_next_bit(bitmap, 512, idx+1))
1216
Hannes Edercded19f2009-02-21 02:19:13 +01001217static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1218 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001219{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001220 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001221
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001222 if (sp->unsync)
1223 for (i=0; i < pvec->nr; i++)
1224 if (pvec->page[i].sp == sp)
1225 return 0;
1226
1227 pvec->page[pvec->nr].sp = sp;
1228 pvec->page[pvec->nr].idx = idx;
1229 pvec->nr++;
1230 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1231}
1232
1233static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1234 struct kvm_mmu_pages *pvec)
1235{
1236 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001237
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001238 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001239 struct kvm_mmu_page *child;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001240 u64 ent = sp->spt[i];
1241
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001242 if (!is_shadow_present_pte(ent) || is_large_pte(ent))
1243 goto clear_child_bitmap;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001244
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001245 child = page_header(ent & PT64_BASE_ADDR_MASK);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001246
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001247 if (child->unsync_children) {
1248 if (mmu_pages_add(pvec, child, i))
1249 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001250
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001251 ret = __mmu_unsync_walk(child, pvec);
1252 if (!ret)
1253 goto clear_child_bitmap;
1254 else if (ret > 0)
1255 nr_unsync_leaf += ret;
1256 else
1257 return ret;
1258 } else if (child->unsync) {
1259 nr_unsync_leaf++;
1260 if (mmu_pages_add(pvec, child, i))
1261 return -ENOSPC;
1262 } else
1263 goto clear_child_bitmap;
1264
1265 continue;
1266
1267clear_child_bitmap:
1268 __clear_bit(i, sp->unsync_child_bitmap);
1269 sp->unsync_children--;
1270 WARN_ON((int)sp->unsync_children < 0);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001271 }
1272
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001273
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001274 return nr_unsync_leaf;
1275}
1276
1277static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1278 struct kvm_mmu_pages *pvec)
1279{
1280 if (!sp->unsync_children)
1281 return 0;
1282
1283 mmu_pages_add(pvec, sp, 0);
1284 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001285}
1286
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001287static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1288{
1289 WARN_ON(!sp->unsync);
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001290 trace_kvm_mmu_sync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001291 sp->unsync = 0;
1292 --kvm->stat.mmu_unsync;
1293}
1294
Xiao Guangrong77758342010-06-04 21:53:54 +08001295static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1296 struct list_head *invalid_list);
1297static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1298 struct list_head *invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001299
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001300#define for_each_gfn_sp(kvm, sp, gfn, pos) \
1301 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001302 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1303 if ((sp)->gfn != (gfn)) {} else
1304
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001305#define for_each_gfn_indirect_valid_sp(kvm, sp, gfn, pos) \
1306 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001307 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1308 if ((sp)->gfn != (gfn) || (sp)->role.direct || \
1309 (sp)->role.invalid) {} else
1310
Xiao Guangrongf918b442010-06-11 21:30:36 +08001311/* @sp->gfn should be write-protected at the call site */
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001312static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001313 struct list_head *invalid_list, bool clear_unsync)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001314{
Avi Kivity5b7e0102010-04-14 19:20:03 +03001315 if (sp->role.cr4_pae != !!is_pae(vcpu)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001316 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001317 return 1;
1318 }
1319
Xiao Guangrongf918b442010-06-11 21:30:36 +08001320 if (clear_unsync)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001321 kvm_unlink_unsync_page(vcpu->kvm, sp);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001322
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001323 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001324 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001325 return 1;
1326 }
1327
1328 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001329 return 0;
1330}
1331
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001332static int kvm_sync_page_transient(struct kvm_vcpu *vcpu,
1333 struct kvm_mmu_page *sp)
1334{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001335 LIST_HEAD(invalid_list);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001336 int ret;
1337
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001338 ret = __kvm_sync_page(vcpu, sp, &invalid_list, false);
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001339 if (ret)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001340 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
1341
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001342 return ret;
1343}
1344
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001345static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1346 struct list_head *invalid_list)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001347{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001348 return __kvm_sync_page(vcpu, sp, invalid_list, true);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001349}
1350
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001351/* @gfn should be write-protected at the call site */
1352static void kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1353{
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001354 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001355 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001356 LIST_HEAD(invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001357 bool flush = false;
1358
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001359 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001360 if (!s->unsync)
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001361 continue;
1362
1363 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001364 kvm_unlink_unsync_page(vcpu->kvm, s);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001365 if ((s->role.cr4_pae != !!is_pae(vcpu)) ||
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001366 (vcpu->arch.mmu.sync_page(vcpu, s))) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001367 kvm_mmu_prepare_zap_page(vcpu->kvm, s, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001368 continue;
1369 }
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001370 flush = true;
1371 }
1372
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001373 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001374 if (flush)
1375 kvm_mmu_flush_tlb(vcpu);
1376}
1377
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001378struct mmu_page_path {
1379 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1380 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001381};
1382
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001383#define for_each_sp(pvec, sp, parents, i) \
1384 for (i = mmu_pages_next(&pvec, &parents, -1), \
1385 sp = pvec.page[i].sp; \
1386 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1387 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001388
Hannes Edercded19f2009-02-21 02:19:13 +01001389static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1390 struct mmu_page_path *parents,
1391 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001392{
1393 int n;
1394
1395 for (n = i+1; n < pvec->nr; n++) {
1396 struct kvm_mmu_page *sp = pvec->page[n].sp;
1397
1398 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1399 parents->idx[0] = pvec->page[n].idx;
1400 return n;
1401 }
1402
1403 parents->parent[sp->role.level-2] = sp;
1404 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1405 }
1406
1407 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001408}
1409
Hannes Edercded19f2009-02-21 02:19:13 +01001410static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001411{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001412 struct kvm_mmu_page *sp;
1413 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001414
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001415 do {
1416 unsigned int idx = parents->idx[level];
1417
1418 sp = parents->parent[level];
1419 if (!sp)
1420 return;
1421
1422 --sp->unsync_children;
1423 WARN_ON((int)sp->unsync_children < 0);
1424 __clear_bit(idx, sp->unsync_child_bitmap);
1425 level++;
1426 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1427}
1428
1429static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1430 struct mmu_page_path *parents,
1431 struct kvm_mmu_pages *pvec)
1432{
1433 parents->parent[parent->role.level-1] = NULL;
1434 pvec->nr = 0;
1435}
1436
1437static void mmu_sync_children(struct kvm_vcpu *vcpu,
1438 struct kvm_mmu_page *parent)
1439{
1440 int i;
1441 struct kvm_mmu_page *sp;
1442 struct mmu_page_path parents;
1443 struct kvm_mmu_pages pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001444 LIST_HEAD(invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001445
1446 kvm_mmu_pages_init(parent, &parents, &pages);
1447 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001448 int protected = 0;
1449
1450 for_each_sp(pages, sp, parents, i)
1451 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1452
1453 if (protected)
1454 kvm_flush_remote_tlbs(vcpu->kvm);
1455
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001456 for_each_sp(pages, sp, parents, i) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001457 kvm_sync_page(vcpu, sp, &invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001458 mmu_pages_clear_parents(&parents);
1459 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001460 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001461 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001462 kvm_mmu_pages_init(parent, &parents, &pages);
1463 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001464}
1465
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001466static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1467 gfn_t gfn,
1468 gva_t gaddr,
1469 unsigned level,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001470 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001471 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001472 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001473{
1474 union kvm_mmu_page_role role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001475 unsigned quadrant;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001476 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001477 struct hlist_node *node;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001478 bool need_sync = false;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001479
Avi Kivitya770f6f2008-12-21 19:20:09 +02001480 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001481 role.level = level;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001482 role.direct = direct;
Avi Kivity84b0c8c2010-03-14 10:16:40 +02001483 if (role.direct)
Avi Kivity5b7e0102010-04-14 19:20:03 +03001484 role.cr4_pae = 0;
Avi Kivity41074d02007-12-09 17:00:02 +02001485 role.access = access;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02001486 if (!vcpu->arch.mmu.direct_map
1487 && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001488 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1489 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1490 role.quadrant = quadrant;
1491 }
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001492 for_each_gfn_sp(vcpu->kvm, sp, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001493 if (!need_sync && sp->unsync)
1494 need_sync = true;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001495
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001496 if (sp->role.word != role.word)
1497 continue;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001498
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001499 if (sp->unsync && kvm_sync_page_transient(vcpu, sp))
1500 break;
Xiao Guangronge02aa902010-05-15 18:52:34 +08001501
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001502 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1503 if (sp->unsync_children) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001504 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001505 kvm_mmu_mark_parents_unsync(sp);
1506 } else if (sp->unsync)
1507 kvm_mmu_mark_parents_unsync(sp);
Xiao Guangronge02aa902010-05-15 18:52:34 +08001508
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001509 trace_kvm_mmu_get_page(sp, false);
1510 return sp;
1511 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001512 ++vcpu->kvm->stat.mmu_cache_miss;
Lai Jiangshan2032a932010-05-26 16:49:59 +08001513 sp = kvm_mmu_alloc_page(vcpu, parent_pte, direct);
Avi Kivity4db35312007-11-21 15:28:32 +02001514 if (!sp)
1515 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001516 sp->gfn = gfn;
1517 sp->role = role;
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001518 hlist_add_head(&sp->hash_link,
1519 &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001520 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001521 if (rmap_write_protect(vcpu->kvm, gfn))
1522 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001523 if (level > PT_PAGE_TABLE_LEVEL && need_sync)
1524 kvm_sync_pages(vcpu, gfn);
1525
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001526 account_shadowed(vcpu->kvm, gfn);
1527 }
Avi Kivity131d8272008-05-29 14:56:28 +03001528 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1529 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1530 else
1531 nonpaging_prefetch_page(vcpu, sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001532 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001533 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001534}
1535
Avi Kivity2d111232008-12-25 14:39:47 +02001536static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1537 struct kvm_vcpu *vcpu, u64 addr)
1538{
1539 iterator->addr = addr;
1540 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1541 iterator->level = vcpu->arch.mmu.shadow_root_level;
Joerg Roedel81407ca2010-09-10 17:31:00 +02001542
1543 if (iterator->level == PT64_ROOT_LEVEL &&
1544 vcpu->arch.mmu.root_level < PT64_ROOT_LEVEL &&
1545 !vcpu->arch.mmu.direct_map)
1546 --iterator->level;
1547
Avi Kivity2d111232008-12-25 14:39:47 +02001548 if (iterator->level == PT32E_ROOT_LEVEL) {
1549 iterator->shadow_addr
1550 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1551 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1552 --iterator->level;
1553 if (!iterator->shadow_addr)
1554 iterator->level = 0;
1555 }
1556}
1557
1558static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1559{
1560 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1561 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001562
1563 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1564 if (is_large_pte(*iterator->sptep))
1565 return false;
1566
Avi Kivity2d111232008-12-25 14:39:47 +02001567 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1568 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1569 return true;
1570}
1571
1572static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1573{
1574 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1575 --iterator->level;
1576}
1577
Avi Kivity32ef26a2010-07-13 14:27:04 +03001578static void link_shadow_page(u64 *sptep, struct kvm_mmu_page *sp)
1579{
1580 u64 spte;
1581
1582 spte = __pa(sp->spt)
1583 | PT_PRESENT_MASK | PT_ACCESSED_MASK
1584 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity121eee92010-07-13 14:27:05 +03001585 __set_spte(sptep, spte);
Avi Kivity32ef26a2010-07-13 14:27:04 +03001586}
1587
Avi Kivitya3aa51c2010-07-13 14:27:06 +03001588static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep)
1589{
1590 if (is_large_pte(*sptep)) {
1591 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
1592 kvm_flush_remote_tlbs(vcpu->kvm);
1593 }
1594}
1595
Avi Kivitya357bd22010-07-13 14:27:07 +03001596static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1597 unsigned direct_access)
1598{
1599 if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
1600 struct kvm_mmu_page *child;
1601
1602 /*
1603 * For the direct sp, if the guest pte's dirty bit
1604 * changed form clean to dirty, it will corrupt the
1605 * sp's access: allow writable in the read-only sp,
1606 * so we should update the spte at this point to get
1607 * a new sp with the correct access.
1608 */
1609 child = page_header(*sptep & PT64_BASE_ADDR_MASK);
1610 if (child->role.access == direct_access)
1611 return;
1612
1613 mmu_page_remove_parent_pte(child, sptep);
1614 __set_spte(sptep, shadow_trap_nonpresent_pte);
1615 kvm_flush_remote_tlbs(vcpu->kvm);
1616 }
1617}
1618
Avi Kivity90cb0522007-07-17 13:04:56 +03001619static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001620 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001621{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001622 unsigned i;
1623 u64 *pt;
1624 u64 ent;
1625
Avi Kivity4db35312007-11-21 15:28:32 +02001626 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001627
Avi Kivity697fe2e2007-01-05 16:36:46 -08001628 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1629 ent = pt[i];
1630
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001631 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001632 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001633 ent &= PT64_BASE_ADDR_MASK;
1634 mmu_page_remove_parent_pte(page_header(ent),
1635 &pt[i]);
1636 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001637 if (is_large_pte(ent))
1638 --kvm->stat.lpages;
Avi Kivitybe38d272010-06-06 14:31:27 +03001639 drop_spte(kvm, &pt[i],
1640 shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001641 }
1642 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001643 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001644 }
Avi Kivitya4360362007-01-05 16:36:45 -08001645}
1646
Avi Kivity4db35312007-11-21 15:28:32 +02001647static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001648{
Avi Kivity4db35312007-11-21 15:28:32 +02001649 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001650}
1651
Avi Kivity12b7d282007-09-23 14:10:49 +02001652static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1653{
1654 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001655 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001656
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001657 kvm_for_each_vcpu(i, vcpu, kvm)
1658 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001659}
1660
Avi Kivity31aa2b42008-07-11 17:59:46 +03001661static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001662{
1663 u64 *parent_pte;
1664
Avi Kivity4db35312007-11-21 15:28:32 +02001665 while (sp->multimapped || sp->parent_pte) {
1666 if (!sp->multimapped)
1667 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001668 else {
1669 struct kvm_pte_chain *chain;
1670
Avi Kivity4db35312007-11-21 15:28:32 +02001671 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001672 struct kvm_pte_chain, link);
1673 parent_pte = chain->parent_ptes[0];
1674 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001675 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001676 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001677 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001678 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001679}
1680
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001681static int mmu_zap_unsync_children(struct kvm *kvm,
Xiao Guangrong77758342010-06-04 21:53:54 +08001682 struct kvm_mmu_page *parent,
1683 struct list_head *invalid_list)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001684{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001685 int i, zapped = 0;
1686 struct mmu_page_path parents;
1687 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001688
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001689 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001690 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001691
1692 kvm_mmu_pages_init(parent, &parents, &pages);
1693 while (mmu_unsync_walk(parent, &pages)) {
1694 struct kvm_mmu_page *sp;
1695
1696 for_each_sp(pages, sp, parents, i) {
Xiao Guangrong77758342010-06-04 21:53:54 +08001697 kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001698 mmu_pages_clear_parents(&parents);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001699 zapped++;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001700 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001701 kvm_mmu_pages_init(parent, &parents, &pages);
1702 }
1703
1704 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001705}
1706
Xiao Guangrong77758342010-06-04 21:53:54 +08001707static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1708 struct list_head *invalid_list)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001709{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001710 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001711
Xiao Guangrong77758342010-06-04 21:53:54 +08001712 trace_kvm_mmu_prepare_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001713 ++kvm->stat.mmu_shadow_zapped;
Xiao Guangrong77758342010-06-04 21:53:54 +08001714 ret = mmu_zap_unsync_children(kvm, sp, invalid_list);
Avi Kivity4db35312007-11-21 15:28:32 +02001715 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001716 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001717 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001718 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001719 if (sp->unsync)
1720 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001721 if (!sp->root_count) {
Gui Jianfeng54a4f022010-05-05 09:03:49 +08001722 /* Count self */
1723 ret++;
Xiao Guangrong77758342010-06-04 21:53:54 +08001724 list_move(&sp->link, invalid_list);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001725 } else {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001726 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001727 kvm_reload_remote_mmus(kvm);
1728 }
Xiao Guangrong77758342010-06-04 21:53:54 +08001729
1730 sp->role.invalid = 1;
Avi Kivity12b7d282007-09-23 14:10:49 +02001731 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001732 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001733}
1734
Xiao Guangrong77758342010-06-04 21:53:54 +08001735static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1736 struct list_head *invalid_list)
1737{
1738 struct kvm_mmu_page *sp;
1739
1740 if (list_empty(invalid_list))
1741 return;
1742
1743 kvm_flush_remote_tlbs(kvm);
1744
1745 do {
1746 sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
1747 WARN_ON(!sp->role.invalid || sp->root_count);
1748 kvm_mmu_free_page(kvm, sp);
1749 } while (!list_empty(invalid_list));
1750
1751}
1752
Izik Eidus82ce2c92007-10-02 18:52:55 +02001753/*
1754 * Changing the number of mmu pages allocated to the vm
Dave Hansen49d5ca22010-08-19 18:11:28 -07001755 * Note: if goal_nr_mmu_pages is too small, you will get dead lock
Izik Eidus82ce2c92007-10-02 18:52:55 +02001756 */
Dave Hansen49d5ca22010-08-19 18:11:28 -07001757void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages)
Izik Eidus82ce2c92007-10-02 18:52:55 +02001758{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001759 LIST_HEAD(invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001760 /*
1761 * If we set the number of mmu pages to be smaller be than the
1762 * number of actived pages , we must to free some mmu pages before we
1763 * change the value
1764 */
1765
Dave Hansen49d5ca22010-08-19 18:11:28 -07001766 if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) {
1767 while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages &&
Xiao Guangrong77662e02010-04-16 16:34:42 +08001768 !list_empty(&kvm->arch.active_mmu_pages)) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001769 struct kvm_mmu_page *page;
1770
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001771 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001772 struct kvm_mmu_page, link);
Xiaotian Feng80b63fa2010-08-24 10:31:07 +08001773 kvm_mmu_prepare_zap_page(kvm, page, &invalid_list);
1774 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001775 }
Dave Hansen49d5ca22010-08-19 18:11:28 -07001776 goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001777 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02001778
Dave Hansen49d5ca22010-08-19 18:11:28 -07001779 kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001780}
1781
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001782static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001783{
Avi Kivity4db35312007-11-21 15:28:32 +02001784 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001785 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001786 LIST_HEAD(invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001787 int r;
1788
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08001789 pgprintk("%s: looking for gfn %llx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001790 r = 0;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001791
1792 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08001793 pgprintk("%s: gfn %llx role %x\n", __func__, gfn,
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001794 sp->role.word);
1795 r = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001796 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001797 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001798 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001799 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001800}
1801
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001802static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001803{
Avi Kivity4db35312007-11-21 15:28:32 +02001804 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001805 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001806 LIST_HEAD(invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001807
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001808 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08001809 pgprintk("%s: zap %llx %x\n",
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001810 __func__, gfn, sp->role.word);
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001811 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001812 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001813 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001814}
1815
Avi Kivity38c335f2007-11-21 14:20:22 +02001816static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001817{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001818 int slot = memslot_id(kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001819 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001820
Sheng Yang291f26b2008-10-16 17:30:57 +08001821 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001822}
1823
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001824static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1825{
1826 int i;
1827 u64 *pt = sp->spt;
1828
1829 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1830 return;
1831
1832 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1833 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001834 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001835 }
1836}
1837
Sheng Yang74be52e2008-10-09 16:01:56 +08001838/*
1839 * The function is based on mtrr_type_lookup() in
1840 * arch/x86/kernel/cpu/mtrr/generic.c
1841 */
1842static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1843 u64 start, u64 end)
1844{
1845 int i;
1846 u64 base, mask;
1847 u8 prev_match, curr_match;
1848 int num_var_ranges = KVM_NR_VAR_MTRR;
1849
1850 if (!mtrr_state->enabled)
1851 return 0xFF;
1852
1853 /* Make end inclusive end, instead of exclusive */
1854 end--;
1855
1856 /* Look in fixed ranges. Just return the type as per start */
1857 if (mtrr_state->have_fixed && (start < 0x100000)) {
1858 int idx;
1859
1860 if (start < 0x80000) {
1861 idx = 0;
1862 idx += (start >> 16);
1863 return mtrr_state->fixed_ranges[idx];
1864 } else if (start < 0xC0000) {
1865 idx = 1 * 8;
1866 idx += ((start - 0x80000) >> 14);
1867 return mtrr_state->fixed_ranges[idx];
1868 } else if (start < 0x1000000) {
1869 idx = 3 * 8;
1870 idx += ((start - 0xC0000) >> 12);
1871 return mtrr_state->fixed_ranges[idx];
1872 }
1873 }
1874
1875 /*
1876 * Look in variable ranges
1877 * Look of multiple ranges matching this address and pick type
1878 * as per MTRR precedence
1879 */
1880 if (!(mtrr_state->enabled & 2))
1881 return mtrr_state->def_type;
1882
1883 prev_match = 0xFF;
1884 for (i = 0; i < num_var_ranges; ++i) {
1885 unsigned short start_state, end_state;
1886
1887 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1888 continue;
1889
1890 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1891 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1892 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1893 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1894
1895 start_state = ((start & mask) == (base & mask));
1896 end_state = ((end & mask) == (base & mask));
1897 if (start_state != end_state)
1898 return 0xFE;
1899
1900 if ((start & mask) != (base & mask))
1901 continue;
1902
1903 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1904 if (prev_match == 0xFF) {
1905 prev_match = curr_match;
1906 continue;
1907 }
1908
1909 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1910 curr_match == MTRR_TYPE_UNCACHABLE)
1911 return MTRR_TYPE_UNCACHABLE;
1912
1913 if ((prev_match == MTRR_TYPE_WRBACK &&
1914 curr_match == MTRR_TYPE_WRTHROUGH) ||
1915 (prev_match == MTRR_TYPE_WRTHROUGH &&
1916 curr_match == MTRR_TYPE_WRBACK)) {
1917 prev_match = MTRR_TYPE_WRTHROUGH;
1918 curr_match = MTRR_TYPE_WRTHROUGH;
1919 }
1920
1921 if (prev_match != curr_match)
1922 return MTRR_TYPE_UNCACHABLE;
1923 }
1924
1925 if (prev_match != 0xFF)
1926 return prev_match;
1927
1928 return mtrr_state->def_type;
1929}
1930
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001931u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001932{
1933 u8 mtrr;
1934
1935 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1936 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1937 if (mtrr == 0xfe || mtrr == 0xff)
1938 mtrr = MTRR_TYPE_WRBACK;
1939 return mtrr;
1940}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001941EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001942
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001943static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001944{
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001945 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001946 ++vcpu->kvm->stat.mmu_unsync;
1947 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001948
Xiao Guangrong6b184932010-04-16 21:29:17 +08001949 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001950 mmu_convert_notrap(sp);
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001951}
1952
1953static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1954{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001955 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001956 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001957
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001958 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001959 if (s->unsync)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001960 continue;
1961 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1962 __kvm_unsync_page(vcpu, s);
1963 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001964}
1965
1966static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1967 bool can_unsync)
1968{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001969 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001970 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001971 bool need_unsync = false;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001972
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001973 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001974 if (!can_unsync)
1975 return 1;
1976
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001977 if (s->role.level != PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001978 return 1;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001979
1980 if (!need_unsync && !s->unsync) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001981 if (!oos_shadow)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001982 return 1;
1983 need_unsync = true;
1984 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001985 }
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001986 if (need_unsync)
1987 kvm_unsync_pages(vcpu, gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001988 return 0;
1989}
1990
Avi Kivityd555c332009-06-10 14:24:23 +03001991static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001992 unsigned pte_access, int user_fault,
Joerg Roedel852e3c12009-07-27 16:30:44 +02001993 int write_fault, int dirty, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001994 gfn_t gfn, pfn_t pfn, bool speculative,
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08001995 bool can_unsync, bool host_writable)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001996{
Xiao Guangrongb330aa02010-11-19 17:02:35 +08001997 u64 spte, entry = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001998 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001999
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002000 /*
2001 * We don't set the accessed bit, since we sometimes want to see
2002 * whether the guest actually used the pte (in order to detect
2003 * demand paging).
2004 */
Marcelo Tosatti982c2562010-10-22 14:18:16 -02002005 spte = PT_PRESENT_MASK;
Avi Kivity947da532008-03-18 11:05:52 +02002006 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03002007 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002008 if (!dirty)
2009 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08002010 if (pte_access & ACC_EXEC_MASK)
2011 spte |= shadow_x_mask;
2012 else
2013 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002014 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08002015 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002016 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002017 spte |= PT_PAGE_SIZE_MASK;
Avi Kivityb0bc3ee2010-09-13 16:45:28 +02002018 if (tdp_enabled)
Sheng Yang4b12f0d2009-04-27 20:35:42 +08002019 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
2020 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002021
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08002022 if (host_writable)
Izik Eidus14032832009-09-23 21:47:17 +03002023 spte |= SPTE_HOST_WRITEABLE;
Xiao Guangrongf8e453b2010-12-23 16:09:29 +08002024 else
2025 pte_access &= ~ACC_WRITE_MASK;
Izik Eidus14032832009-09-23 21:47:17 +03002026
Anthony Liguori35149e22008-04-02 14:46:56 -05002027 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002028
2029 if ((pte_access & ACC_WRITE_MASK)
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002030 || (!vcpu->arch.mmu.direct_map && write_fault
2031 && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002032
Joerg Roedel852e3c12009-07-27 16:30:44 +02002033 if (level > PT_PAGE_TABLE_LEVEL &&
2034 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002035 ret = 1;
Avi Kivitybe38d272010-06-06 14:31:27 +03002036 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
2037 goto done;
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002038 }
2039
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002040 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002041
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002042 if (!vcpu->arch.mmu.direct_map
2043 && !(pte_access & ACC_WRITE_MASK))
Avi Kivity69325a12010-05-27 14:35:58 +03002044 spte &= ~PT_USER_MASK;
2045
Marcelo Tosattiecc55892008-11-25 15:58:07 +01002046 /*
2047 * Optimization: for pte sync, if spte was writable the hash
2048 * lookup is unnecessary (and expensive). Write protection
2049 * is responsibility of mmu_get_page / kvm_sync_page.
2050 * Same reasoning can be applied to dirty page accounting.
2051 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002052 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01002053 goto set_pte;
2054
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002055 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002056 pgprintk("%s: found shadow page for %llx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002057 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002058 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002059 pte_access &= ~ACC_WRITE_MASK;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002060 if (is_writable_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002061 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002062 }
2063 }
2064
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002065 if (pte_access & ACC_WRITE_MASK)
2066 mark_page_dirty(vcpu->kvm, gfn);
2067
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002068set_pte:
Avi Kivityb79b93f2010-06-06 15:46:44 +03002069 update_spte(sptep, spte);
Xiao Guangrongb330aa02010-11-19 17:02:35 +08002070 /*
2071 * If we overwrite a writable spte with a read-only one we
2072 * should flush remote TLBs. Otherwise rmap_write_protect
2073 * will find a read-only spte, even though the writable spte
2074 * might be cached on a CPU's TLB.
2075 */
2076 if (is_writable_pte(entry) && !is_writable_pte(*sptep))
2077 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivitybe38d272010-06-06 14:31:27 +03002078done:
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002079 return ret;
2080}
2081
Avi Kivityd555c332009-06-10 14:24:23 +03002082static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002083 unsigned pt_access, unsigned pte_access,
2084 int user_fault, int write_fault, int dirty,
Joerg Roedel852e3c12009-07-27 16:30:44 +02002085 int *ptwrite, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03002086 pfn_t pfn, bool speculative,
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08002087 bool host_writable)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002088{
2089 int was_rmapped = 0;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002090 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002091
2092 pgprintk("%s: spte %llx access %x write_fault %d"
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002093 " user_fault %d gfn %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002094 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002095 write_fault, user_fault, gfn);
2096
Avi Kivityd555c332009-06-10 14:24:23 +03002097 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002098 /*
2099 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
2100 * the parent of the now unreachable PTE.
2101 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02002102 if (level > PT_PAGE_TABLE_LEVEL &&
2103 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002104 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03002105 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002106
2107 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03002108 mmu_page_remove_parent_pte(child, sptep);
Marcelo Tosatti3be22642010-05-28 09:44:59 -03002109 __set_spte(sptep, shadow_trap_nonpresent_pte);
2110 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityd555c332009-06-10 14:24:23 +03002111 } else if (pfn != spte_to_pfn(*sptep)) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002112 pgprintk("hfn old %llx new %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002113 spte_to_pfn(*sptep), pfn);
Avi Kivitybe38d272010-06-06 14:31:27 +03002114 drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
Xiao Guangrong91546352010-06-30 16:04:06 +08002115 kvm_flush_remote_tlbs(vcpu->kvm);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01002116 } else
2117 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002118 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02002119
Avi Kivityd555c332009-06-10 14:24:23 +03002120 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Izik Eidus14032832009-09-23 21:47:17 +03002121 dirty, level, gfn, pfn, speculative, true,
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08002122 host_writable)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002123 if (write_fault)
2124 *ptwrite = 1;
Xiao Guangrong5304efd2010-06-08 20:05:57 +08002125 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03002126 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002127
Avi Kivityd555c332009-06-10 14:24:23 +03002128 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002129 pgprintk("instantiating %s PTE (%s) at %llx (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002130 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02002131 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
2132 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03002133 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002134 ++vcpu->kvm->stat.lpages;
2135
Avi Kivityd555c332009-06-10 14:24:23 +03002136 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002137 if (!was_rmapped) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02002138 rmap_count = rmap_add(vcpu, sptep, gfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002139 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
Joerg Roedel852e3c12009-07-27 16:30:44 +02002140 rmap_recycle(vcpu, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002141 }
Xiao Guangrong9ed55202010-07-16 11:25:17 +08002142 kvm_release_pfn_clean(pfn);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002143 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03002144 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002145 vcpu->arch.last_pte_gfn = gfn;
2146 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002147}
2148
Avi Kivity6aa8b732006-12-10 02:21:36 -08002149static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
2150{
2151}
2152
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002153static struct kvm_memory_slot *
2154pte_prefetch_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn, bool no_dirty_log)
2155{
2156 struct kvm_memory_slot *slot;
2157
2158 slot = gfn_to_memslot(vcpu->kvm, gfn);
2159 if (!slot || slot->flags & KVM_MEMSLOT_INVALID ||
2160 (no_dirty_log && slot->dirty_bitmap))
2161 slot = NULL;
2162
2163 return slot;
2164}
2165
2166static pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn,
2167 bool no_dirty_log)
2168{
2169 struct kvm_memory_slot *slot;
2170 unsigned long hva;
2171
2172 slot = pte_prefetch_gfn_to_memslot(vcpu, gfn, no_dirty_log);
2173 if (!slot) {
2174 get_page(bad_page);
2175 return page_to_pfn(bad_page);
2176 }
2177
2178 hva = gfn_to_hva_memslot(slot, gfn);
2179
2180 return hva_to_pfn_atomic(vcpu->kvm, hva);
2181}
2182
2183static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,
2184 struct kvm_mmu_page *sp,
2185 u64 *start, u64 *end)
2186{
2187 struct page *pages[PTE_PREFETCH_NUM];
2188 unsigned access = sp->role.access;
2189 int i, ret;
2190 gfn_t gfn;
2191
2192 gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt);
2193 if (!pte_prefetch_gfn_to_memslot(vcpu, gfn, access & ACC_WRITE_MASK))
2194 return -1;
2195
2196 ret = gfn_to_page_many_atomic(vcpu->kvm, gfn, pages, end - start);
2197 if (ret <= 0)
2198 return -1;
2199
2200 for (i = 0; i < ret; i++, gfn++, start++)
2201 mmu_set_spte(vcpu, start, ACC_ALL,
2202 access, 0, 0, 1, NULL,
2203 sp->role.level, gfn,
2204 page_to_pfn(pages[i]), true, true);
2205
2206 return 0;
2207}
2208
2209static void __direct_pte_prefetch(struct kvm_vcpu *vcpu,
2210 struct kvm_mmu_page *sp, u64 *sptep)
2211{
2212 u64 *spte, *start = NULL;
2213 int i;
2214
2215 WARN_ON(!sp->role.direct);
2216
2217 i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
2218 spte = sp->spt + i;
2219
2220 for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
2221 if (*spte != shadow_trap_nonpresent_pte || spte == sptep) {
2222 if (!start)
2223 continue;
2224 if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0)
2225 break;
2226 start = NULL;
2227 } else if (!start)
2228 start = spte;
2229 }
2230}
2231
2232static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)
2233{
2234 struct kvm_mmu_page *sp;
2235
2236 /*
2237 * Since it's no accessed bit on EPT, it's no way to
2238 * distinguish between actually accessed translations
2239 * and prefetched, so disable pte prefetch if EPT is
2240 * enabled.
2241 */
2242 if (!shadow_accessed_mask)
2243 return;
2244
2245 sp = page_header(__pa(sptep));
2246 if (sp->role.level > PT_PAGE_TABLE_LEVEL)
2247 return;
2248
2249 __direct_pte_prefetch(vcpu, sp, sptep);
2250}
2251
Joerg Roedel4d9976b2008-02-07 13:47:42 +01002252static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002253 int map_writable, int level, gfn_t gfn, pfn_t pfn,
2254 bool prefault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002255{
Avi Kivity9f652d22008-12-25 14:54:25 +02002256 struct kvm_shadow_walk_iterator iterator;
2257 struct kvm_mmu_page *sp;
2258 int pt_write = 0;
2259 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002260
Avi Kivity9f652d22008-12-25 14:54:25 +02002261 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02002262 if (iterator.level == level) {
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002263 unsigned pte_access = ACC_ALL;
2264
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002265 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, pte_access,
Avi Kivity9f652d22008-12-25 14:54:25 +02002266 0, write, 1, &pt_write,
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002267 level, gfn, pfn, prefault, map_writable);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002268 direct_pte_prefetch(vcpu, iterator.sptep);
Avi Kivity9f652d22008-12-25 14:54:25 +02002269 ++vcpu->stat.pf_fixed;
2270 break;
2271 }
2272
2273 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
Lai Jiangshanc9fa0b32010-05-26 16:48:25 +08002274 u64 base_addr = iterator.addr;
2275
2276 base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
2277 pseudo_gfn = base_addr >> PAGE_SHIFT;
Avi Kivity9f652d22008-12-25 14:54:25 +02002278 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
2279 iterator.level - 1,
2280 1, ACC_ALL, iterator.sptep);
2281 if (!sp) {
2282 pgprintk("nonpaging_map: ENOMEM\n");
2283 kvm_release_pfn_clean(pfn);
2284 return -ENOMEM;
2285 }
2286
Avi Kivityd555c332009-06-10 14:24:23 +03002287 __set_spte(iterator.sptep,
2288 __pa(sp->spt)
2289 | PT_PRESENT_MASK | PT_WRITABLE_MASK
Xiao Guangrong33f91ed2010-09-27 18:05:00 +08002290 | shadow_user_mask | shadow_x_mask
2291 | shadow_accessed_mask);
Avi Kivity9f652d22008-12-25 14:54:25 +02002292 }
2293 }
2294 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002295}
2296
Huang Ying77db5cb2010-10-08 16:24:15 +08002297static void kvm_send_hwpoison_signal(unsigned long address, struct task_struct *tsk)
Huang Yingbf998152010-05-31 14:28:19 +08002298{
Huang Ying77db5cb2010-10-08 16:24:15 +08002299 siginfo_t info;
Huang Yingbf998152010-05-31 14:28:19 +08002300
Huang Ying77db5cb2010-10-08 16:24:15 +08002301 info.si_signo = SIGBUS;
2302 info.si_errno = 0;
2303 info.si_code = BUS_MCEERR_AR;
2304 info.si_addr = (void __user *)address;
2305 info.si_addr_lsb = PAGE_SHIFT;
2306
2307 send_sig_info(SIGBUS, &info, tsk);
Huang Yingbf998152010-05-31 14:28:19 +08002308}
2309
2310static int kvm_handle_bad_page(struct kvm *kvm, gfn_t gfn, pfn_t pfn)
2311{
2312 kvm_release_pfn_clean(pfn);
2313 if (is_hwpoison_pfn(pfn)) {
Huang Ying77db5cb2010-10-08 16:24:15 +08002314 kvm_send_hwpoison_signal(gfn_to_hva(kvm, gfn), current);
Huang Yingbf998152010-05-31 14:28:19 +08002315 return 0;
Gleb Natapovedba23e2010-07-07 20:16:45 +03002316 } else if (is_fault_pfn(pfn))
2317 return -EFAULT;
2318
Huang Yingbf998152010-05-31 14:28:19 +08002319 return 1;
2320}
2321
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002322static void transparent_hugepage_adjust(struct kvm_vcpu *vcpu,
2323 gfn_t *gfnp, pfn_t *pfnp, int *levelp)
2324{
2325 pfn_t pfn = *pfnp;
2326 gfn_t gfn = *gfnp;
2327 int level = *levelp;
2328
2329 /*
2330 * Check if it's a transparent hugepage. If this would be an
2331 * hugetlbfs page, level wouldn't be set to
2332 * PT_PAGE_TABLE_LEVEL and there would be no adjustment done
2333 * here.
2334 */
2335 if (!is_error_pfn(pfn) && !kvm_is_mmio_pfn(pfn) &&
2336 level == PT_PAGE_TABLE_LEVEL &&
2337 PageTransCompound(pfn_to_page(pfn)) &&
2338 !has_wrprotected_page(vcpu->kvm, gfn, PT_DIRECTORY_LEVEL)) {
2339 unsigned long mask;
2340 /*
2341 * mmu_notifier_retry was successful and we hold the
2342 * mmu_lock here, so the pmd can't become splitting
2343 * from under us, and in turn
2344 * __split_huge_page_refcount() can't run from under
2345 * us and we can safely transfer the refcount from
2346 * PG_tail to PG_head as we switch the pfn to tail to
2347 * head.
2348 */
2349 *levelp = level = PT_DIRECTORY_LEVEL;
2350 mask = KVM_PAGES_PER_HPAGE(level) - 1;
2351 VM_BUG_ON((gfn & mask) != (pfn & mask));
2352 if (pfn & mask) {
2353 gfn &= ~mask;
2354 *gfnp = gfn;
2355 kvm_release_pfn_clean(pfn);
2356 pfn &= ~mask;
2357 if (!get_page_unless_zero(pfn_to_page(pfn)))
2358 BUG();
2359 *pfnp = pfn;
2360 }
2361 }
2362}
2363
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002364static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
Xiao Guangrong060c2ab2010-11-12 14:49:11 +08002365 gva_t gva, pfn_t *pfn, bool write, bool *writable);
2366
2367static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002368 bool prefault)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002369{
2370 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002371 int level;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002372 int force_pt_level;
Anthony Liguori35149e22008-04-02 14:46:56 -05002373 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002374 unsigned long mmu_seq;
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002375 bool map_writable;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002376
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002377 force_pt_level = mapping_level_dirty_bitmap(vcpu, gfn);
2378 if (likely(!force_pt_level)) {
2379 level = mapping_level(vcpu, gfn);
2380 /*
2381 * This path builds a PAE pagetable - so we can map
2382 * 2mb pages at maximum. Therefore check if the level
2383 * is larger than that.
2384 */
2385 if (level > PT_DIRECTORY_LEVEL)
2386 level = PT_DIRECTORY_LEVEL;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002387
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002388 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2389 } else
2390 level = PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002391
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002392 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002393 smp_rmb();
Xiao Guangrong060c2ab2010-11-12 14:49:11 +08002394
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002395 if (try_async_pf(vcpu, prefault, gfn, v, &pfn, write, &map_writable))
Xiao Guangrong060c2ab2010-11-12 14:49:11 +08002396 return 0;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002397
Avi Kivityd196e342008-01-24 11:44:11 +02002398 /* mmio */
Huang Yingbf998152010-05-31 14:28:19 +08002399 if (is_error_pfn(pfn))
2400 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002401
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002402 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002403 if (mmu_notifier_retry(vcpu, mmu_seq))
2404 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02002405 kvm_mmu_free_some_pages(vcpu);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002406 if (likely(!force_pt_level))
2407 transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002408 r = __direct_map(vcpu, v, write, map_writable, level, gfn, pfn,
2409 prefault);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002410 spin_unlock(&vcpu->kvm->mmu_lock);
2411
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002412
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002413 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002414
2415out_unlock:
2416 spin_unlock(&vcpu->kvm->mmu_lock);
2417 kvm_release_pfn_clean(pfn);
2418 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002419}
2420
2421
Avi Kivity17ac10a2007-01-05 16:36:40 -08002422static void mmu_free_roots(struct kvm_vcpu *vcpu)
2423{
2424 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002425 struct kvm_mmu_page *sp;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002426 LIST_HEAD(invalid_list);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002427
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002428 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002429 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002430 spin_lock(&vcpu->kvm->mmu_lock);
Joerg Roedel81407ca2010-09-10 17:31:00 +02002431 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL &&
2432 (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL ||
2433 vcpu->arch.mmu.direct_map)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002434 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002435
Avi Kivity4db35312007-11-21 15:28:32 +02002436 sp = page_header(root);
2437 --sp->root_count;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002438 if (!sp->root_count && sp->role.invalid) {
2439 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
2440 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
2441 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002442 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002443 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002444 return;
2445 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002446 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002447 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002448
Avi Kivity417726a2007-04-12 17:35:58 +03002449 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002450 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002451 sp = page_header(root);
2452 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002453 if (!sp->root_count && sp->role.invalid)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002454 kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
2455 &invalid_list);
Avi Kivity417726a2007-04-12 17:35:58 +03002456 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002457 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002458 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002459 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002460 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002461 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002462}
2463
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002464static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2465{
2466 int ret = 0;
2467
2468 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03002469 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002470 ret = 1;
2471 }
2472
2473 return ret;
2474}
2475
Joerg Roedel651dd372010-09-10 17:30:59 +02002476static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu)
2477{
2478 struct kvm_mmu_page *sp;
Avi Kivity7ebaf152010-10-03 18:51:39 +02002479 unsigned i;
Joerg Roedel651dd372010-09-10 17:30:59 +02002480
2481 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2482 spin_lock(&vcpu->kvm->mmu_lock);
2483 kvm_mmu_free_some_pages(vcpu);
2484 sp = kvm_mmu_get_page(vcpu, 0, 0, PT64_ROOT_LEVEL,
2485 1, ACC_ALL, NULL);
2486 ++sp->root_count;
2487 spin_unlock(&vcpu->kvm->mmu_lock);
2488 vcpu->arch.mmu.root_hpa = __pa(sp->spt);
2489 } else if (vcpu->arch.mmu.shadow_root_level == PT32E_ROOT_LEVEL) {
2490 for (i = 0; i < 4; ++i) {
2491 hpa_t root = vcpu->arch.mmu.pae_root[i];
2492
2493 ASSERT(!VALID_PAGE(root));
2494 spin_lock(&vcpu->kvm->mmu_lock);
2495 kvm_mmu_free_some_pages(vcpu);
Avi Kivity649497d2010-12-28 12:09:07 +02002496 sp = kvm_mmu_get_page(vcpu, i << (30 - PAGE_SHIFT),
2497 i << 30,
Joerg Roedel651dd372010-09-10 17:30:59 +02002498 PT32_ROOT_LEVEL, 1, ACC_ALL,
2499 NULL);
2500 root = __pa(sp->spt);
2501 ++sp->root_count;
2502 spin_unlock(&vcpu->kvm->mmu_lock);
2503 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Joerg Roedel651dd372010-09-10 17:30:59 +02002504 }
Xiao Guangrong62927572010-09-27 18:02:12 +08002505 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Joerg Roedel651dd372010-09-10 17:30:59 +02002506 } else
2507 BUG();
2508
2509 return 0;
2510}
2511
2512static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002513{
Avi Kivity4db35312007-11-21 15:28:32 +02002514 struct kvm_mmu_page *sp;
Joerg Roedel81407ca2010-09-10 17:31:00 +02002515 u64 pdptr, pm_mask;
2516 gfn_t root_gfn;
2517 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002518
Joerg Roedel5777ed32010-09-10 17:30:42 +02002519 root_gfn = vcpu->arch.mmu.get_cr3(vcpu) >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002520
Joerg Roedel651dd372010-09-10 17:30:59 +02002521 if (mmu_check_root(vcpu, root_gfn))
2522 return 1;
2523
2524 /*
2525 * Do we shadow a long mode page table? If so we need to
2526 * write-protect the guests page table root.
2527 */
2528 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002529 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002530
2531 ASSERT(!VALID_PAGE(root));
Joerg Roedel651dd372010-09-10 17:30:59 +02002532
Avi Kivity8facbbf2010-05-04 12:58:32 +03002533 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b62010-05-12 21:00:35 -03002534 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel651dd372010-09-10 17:30:59 +02002535 sp = kvm_mmu_get_page(vcpu, root_gfn, 0, PT64_ROOT_LEVEL,
2536 0, ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002537 root = __pa(sp->spt);
2538 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002539 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002540 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002541 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002542 }
Joerg Roedelf87f9282010-09-02 17:29:45 +02002543
Joerg Roedel651dd372010-09-10 17:30:59 +02002544 /*
2545 * We shadow a 32 bit page table. This may be a legacy 2-level
Joerg Roedel81407ca2010-09-10 17:31:00 +02002546 * or a PAE 3-level page table. In either case we need to be aware that
2547 * the shadow page table may be a PAE or a long mode page table.
Joerg Roedel651dd372010-09-10 17:30:59 +02002548 */
Joerg Roedel81407ca2010-09-10 17:31:00 +02002549 pm_mask = PT_PRESENT_MASK;
2550 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL)
2551 pm_mask |= PT_ACCESSED_MASK | PT_WRITABLE_MASK | PT_USER_MASK;
2552
Avi Kivity17ac10a2007-01-05 16:36:40 -08002553 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002554 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002555
2556 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002557 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Joerg Roedeld41d1892010-09-10 17:30:58 +02002558 pdptr = kvm_pdptr_read_mmu(vcpu, &vcpu->arch.mmu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002559 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002560 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002561 continue;
2562 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002563 root_gfn = pdptr >> PAGE_SHIFT;
Joerg Roedelf87f9282010-09-02 17:29:45 +02002564 if (mmu_check_root(vcpu, root_gfn))
2565 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002566 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002567 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b62010-05-12 21:00:35 -03002568 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02002569 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Joerg Roedel651dd372010-09-10 17:30:59 +02002570 PT32_ROOT_LEVEL, 0,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002571 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002572 root = __pa(sp->spt);
2573 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002574 spin_unlock(&vcpu->kvm->mmu_lock);
2575
Joerg Roedel81407ca2010-09-10 17:31:00 +02002576 vcpu->arch.mmu.pae_root[i] = root | pm_mask;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002577 }
Xiao Guangrong62927572010-09-27 18:02:12 +08002578 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Joerg Roedel81407ca2010-09-10 17:31:00 +02002579
2580 /*
2581 * If we shadow a 32 bit page table with a long mode page
2582 * table we enter this path.
2583 */
2584 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2585 if (vcpu->arch.mmu.lm_root == NULL) {
2586 /*
2587 * The additional page necessary for this is only
2588 * allocated on demand.
2589 */
2590
2591 u64 *lm_root;
2592
2593 lm_root = (void*)get_zeroed_page(GFP_KERNEL);
2594 if (lm_root == NULL)
2595 return 1;
2596
2597 lm_root[0] = __pa(vcpu->arch.mmu.pae_root) | pm_mask;
2598
2599 vcpu->arch.mmu.lm_root = lm_root;
2600 }
2601
2602 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.lm_root);
2603 }
2604
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002605 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002606}
2607
Joerg Roedel651dd372010-09-10 17:30:59 +02002608static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
2609{
2610 if (vcpu->arch.mmu.direct_map)
2611 return mmu_alloc_direct_roots(vcpu);
2612 else
2613 return mmu_alloc_shadow_roots(vcpu);
2614}
2615
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002616static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2617{
2618 int i;
2619 struct kvm_mmu_page *sp;
2620
Joerg Roedel81407ca2010-09-10 17:31:00 +02002621 if (vcpu->arch.mmu.direct_map)
2622 return;
2623
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002624 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2625 return;
Xiao Guangrong69030742010-09-27 18:09:29 +08002626
2627 trace_kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC);
Joerg Roedel81407ca2010-09-10 17:31:00 +02002628 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002629 hpa_t root = vcpu->arch.mmu.root_hpa;
2630 sp = page_header(root);
2631 mmu_sync_children(vcpu, sp);
Xiao Guangrong5054c0d2010-11-12 14:46:08 +08002632 trace_kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002633 return;
2634 }
2635 for (i = 0; i < 4; ++i) {
2636 hpa_t root = vcpu->arch.mmu.pae_root[i];
2637
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002638 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002639 root &= PT64_BASE_ADDR_MASK;
2640 sp = page_header(root);
2641 mmu_sync_children(vcpu, sp);
2642 }
2643 }
Xiao Guangrong69030742010-09-27 18:09:29 +08002644 trace_kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002645}
2646
2647void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2648{
2649 spin_lock(&vcpu->kvm->mmu_lock);
2650 mmu_sync_roots(vcpu);
2651 spin_unlock(&vcpu->kvm->mmu_lock);
2652}
2653
Gleb Natapov1871c602010-02-10 14:21:32 +02002654static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
Avi Kivityab9ae312010-11-22 17:53:26 +02002655 u32 access, struct x86_exception *exception)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002656{
Avi Kivityab9ae312010-11-22 17:53:26 +02002657 if (exception)
2658 exception->error_code = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002659 return vaddr;
2660}
2661
Joerg Roedel6539e732010-09-10 17:30:50 +02002662static gpa_t nonpaging_gva_to_gpa_nested(struct kvm_vcpu *vcpu, gva_t vaddr,
Avi Kivityab9ae312010-11-22 17:53:26 +02002663 u32 access,
2664 struct x86_exception *exception)
Joerg Roedel6539e732010-09-10 17:30:50 +02002665{
Avi Kivityab9ae312010-11-22 17:53:26 +02002666 if (exception)
2667 exception->error_code = 0;
Joerg Roedel6539e732010-09-10 17:30:50 +02002668 return vcpu->arch.nested_mmu.translate_gpa(vcpu, vaddr, access);
2669}
2670
Avi Kivity6aa8b732006-12-10 02:21:36 -08002671static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002672 u32 error_code, bool prefault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002673{
Avi Kivitye8332402007-12-09 18:43:00 +02002674 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002675 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002676
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002677 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002678 r = mmu_topup_memory_caches(vcpu);
2679 if (r)
2680 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002681
Avi Kivity6aa8b732006-12-10 02:21:36 -08002682 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002683 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002684
Avi Kivitye8332402007-12-09 18:43:00 +02002685 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002686
Avi Kivitye8332402007-12-09 18:43:00 +02002687 return nonpaging_map(vcpu, gva & PAGE_MASK,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002688 error_code & PFERR_WRITE_MASK, gfn, prefault);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002689}
2690
Jan Kiszka7e1fbea2010-10-20 15:18:02 +02002691static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn)
Gleb Natapovaf585b92010-10-14 11:22:46 +02002692{
2693 struct kvm_arch_async_pf arch;
Xiao Guangrongfb67e142010-12-07 10:35:25 +08002694
Gleb Natapov7c907052010-10-14 11:22:53 +02002695 arch.token = (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id;
Gleb Natapovaf585b92010-10-14 11:22:46 +02002696 arch.gfn = gfn;
Xiao Guangrongc4806ac2010-11-12 14:49:55 +08002697 arch.direct_map = vcpu->arch.mmu.direct_map;
Xiao Guangrongfb67e142010-12-07 10:35:25 +08002698 arch.cr3 = vcpu->arch.mmu.get_cr3(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002699
2700 return kvm_setup_async_pf(vcpu, gva, gfn, &arch);
2701}
2702
2703static bool can_do_async_pf(struct kvm_vcpu *vcpu)
2704{
2705 if (unlikely(!irqchip_in_kernel(vcpu->kvm) ||
2706 kvm_event_needs_reinjection(vcpu)))
2707 return false;
2708
2709 return kvm_x86_ops->interrupt_allowed(vcpu);
2710}
2711
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002712static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002713 gva_t gva, pfn_t *pfn, bool write, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02002714{
2715 bool async;
2716
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002717 *pfn = gfn_to_pfn_async(vcpu->kvm, gfn, &async, write, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002718
2719 if (!async)
2720 return false; /* *pfn has correct page already */
2721
2722 put_page(pfn_to_page(*pfn));
2723
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002724 if (!prefault && can_do_async_pf(vcpu)) {
Xiao Guangrongc9b263d2010-11-01 16:58:43 +08002725 trace_kvm_try_async_get_page(gva, gfn);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002726 if (kvm_find_async_pf_gfn(vcpu, gfn)) {
2727 trace_kvm_async_pf_doublefault(gva, gfn);
2728 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
2729 return true;
2730 } else if (kvm_arch_setup_async_pf(vcpu, gva, gfn))
2731 return true;
2732 }
2733
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002734 *pfn = gfn_to_pfn_prot(vcpu->kvm, gfn, write, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002735
2736 return false;
2737}
2738
Gleb Natapov56028d02010-10-17 18:13:42 +02002739static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, u32 error_code,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002740 bool prefault)
Joerg Roedelfb72d162008-02-07 13:47:44 +01002741{
Anthony Liguori35149e22008-04-02 14:46:56 -05002742 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002743 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002744 int level;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002745 int force_pt_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002746 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002747 unsigned long mmu_seq;
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002748 int write = error_code & PFERR_WRITE_MASK;
2749 bool map_writable;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002750
2751 ASSERT(vcpu);
2752 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2753
2754 r = mmu_topup_memory_caches(vcpu);
2755 if (r)
2756 return r;
2757
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002758 force_pt_level = mapping_level_dirty_bitmap(vcpu, gfn);
2759 if (likely(!force_pt_level)) {
2760 level = mapping_level(vcpu, gfn);
2761 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2762 } else
2763 level = PT_PAGE_TABLE_LEVEL;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002764
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002765 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002766 smp_rmb();
Gleb Natapovaf585b92010-10-14 11:22:46 +02002767
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002768 if (try_async_pf(vcpu, prefault, gfn, gpa, &pfn, write, &map_writable))
Gleb Natapovaf585b92010-10-14 11:22:46 +02002769 return 0;
2770
2771 /* mmio */
Huang Yingbf998152010-05-31 14:28:19 +08002772 if (is_error_pfn(pfn))
2773 return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002774 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002775 if (mmu_notifier_retry(vcpu, mmu_seq))
2776 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002777 kvm_mmu_free_some_pages(vcpu);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002778 if (likely(!force_pt_level))
2779 transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002780 r = __direct_map(vcpu, gpa, write, map_writable,
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002781 level, gfn, pfn, prefault);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002782 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002783
2784 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002785
2786out_unlock:
2787 spin_unlock(&vcpu->kvm->mmu_lock);
2788 kvm_release_pfn_clean(pfn);
2789 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002790}
2791
Avi Kivity6aa8b732006-12-10 02:21:36 -08002792static void nonpaging_free(struct kvm_vcpu *vcpu)
2793{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002794 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002795}
2796
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002797static int nonpaging_init_context(struct kvm_vcpu *vcpu,
2798 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002799{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002800 context->new_cr3 = nonpaging_new_cr3;
2801 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002802 context->gva_to_gpa = nonpaging_gva_to_gpa;
2803 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002804 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002805 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002806 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002807 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002808 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002809 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002810 context->direct_map = true;
Joerg Roedel2d48a982010-09-10 17:31:01 +02002811 context->nx = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002812 return 0;
2813}
2814
Avi Kivityd835dfe2007-11-21 02:57:59 +02002815void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002816{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002817 ++vcpu->stat.tlb_flush;
Avi Kivitya8eeb042010-05-10 12:34:53 +03002818 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002819}
2820
2821static void paging_new_cr3(struct kvm_vcpu *vcpu)
2822{
Avi Kivity9f8fe502010-12-05 17:30:00 +02002823 pgprintk("%s: cr3 %lx\n", __func__, kvm_read_cr3(vcpu));
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002824 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002825}
2826
Joerg Roedel5777ed32010-09-10 17:30:42 +02002827static unsigned long get_cr3(struct kvm_vcpu *vcpu)
2828{
Avi Kivity9f8fe502010-12-05 17:30:00 +02002829 return kvm_read_cr3(vcpu);
Joerg Roedel5777ed32010-09-10 17:30:42 +02002830}
2831
Avi Kivity6389ee92010-11-29 16:12:30 +02002832static void inject_page_fault(struct kvm_vcpu *vcpu,
2833 struct x86_exception *fault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002834{
Avi Kivity6389ee92010-11-29 16:12:30 +02002835 vcpu->arch.mmu.inject_page_fault(vcpu, fault);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002836}
2837
Avi Kivity6aa8b732006-12-10 02:21:36 -08002838static void paging_free(struct kvm_vcpu *vcpu)
2839{
2840 nonpaging_free(vcpu);
2841}
2842
Joerg Roedel3241f222010-09-10 17:30:45 +02002843static bool is_rsvd_bits_set(struct kvm_mmu *mmu, u64 gpte, int level)
Dong, Eddie82725b22009-03-30 16:21:08 +08002844{
2845 int bit7;
2846
2847 bit7 = (gpte >> 7) & 1;
Joerg Roedel3241f222010-09-10 17:30:45 +02002848 return (gpte & mmu->rsvd_bits_mask[bit7][level-1]) != 0;
Dong, Eddie82725b22009-03-30 16:21:08 +08002849}
2850
Avi Kivity6aa8b732006-12-10 02:21:36 -08002851#define PTTYPE 64
2852#include "paging_tmpl.h"
2853#undef PTTYPE
2854
2855#define PTTYPE 32
2856#include "paging_tmpl.h"
2857#undef PTTYPE
2858
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002859static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
2860 struct kvm_mmu *context,
2861 int level)
Dong, Eddie82725b22009-03-30 16:21:08 +08002862{
Dong, Eddie82725b22009-03-30 16:21:08 +08002863 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2864 u64 exb_bit_rsvd = 0;
2865
Joerg Roedel2d48a982010-09-10 17:31:01 +02002866 if (!context->nx)
Dong, Eddie82725b22009-03-30 16:21:08 +08002867 exb_bit_rsvd = rsvd_bits(63, 63);
2868 switch (level) {
2869 case PT32_ROOT_LEVEL:
2870 /* no rsvd bits for 2 level 4K page table entries */
2871 context->rsvd_bits_mask[0][1] = 0;
2872 context->rsvd_bits_mask[0][0] = 0;
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002873 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2874
2875 if (!is_pse(vcpu)) {
2876 context->rsvd_bits_mask[1][1] = 0;
2877 break;
2878 }
2879
Dong, Eddie82725b22009-03-30 16:21:08 +08002880 if (is_cpuid_PSE36())
2881 /* 36bits PSE 4MB page */
2882 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2883 else
2884 /* 32 bits PSE 4MB page */
2885 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08002886 break;
2887 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002888 context->rsvd_bits_mask[0][2] =
2889 rsvd_bits(maxphyaddr, 63) |
2890 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002891 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002892 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002893 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2894 rsvd_bits(maxphyaddr, 62); /* PTE */
2895 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2896 rsvd_bits(maxphyaddr, 62) |
2897 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002898 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002899 break;
2900 case PT64_ROOT_LEVEL:
2901 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2902 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2903 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2904 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2905 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002906 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002907 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2908 rsvd_bits(maxphyaddr, 51);
2909 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002910 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2911 rsvd_bits(maxphyaddr, 51) |
2912 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002913 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002914 rsvd_bits(maxphyaddr, 51) |
2915 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002916 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002917 break;
2918 }
2919}
2920
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002921static int paging64_init_context_common(struct kvm_vcpu *vcpu,
2922 struct kvm_mmu *context,
2923 int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002924{
Joerg Roedel2d48a982010-09-10 17:31:01 +02002925 context->nx = is_nx(vcpu);
2926
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002927 reset_rsvds_bits_mask(vcpu, context, level);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002928
2929 ASSERT(is_pae(vcpu));
2930 context->new_cr3 = paging_new_cr3;
2931 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002932 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002933 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002934 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002935 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002936 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002937 context->root_level = level;
2938 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002939 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002940 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002941 return 0;
2942}
2943
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002944static int paging64_init_context(struct kvm_vcpu *vcpu,
2945 struct kvm_mmu *context)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002946{
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002947 return paging64_init_context_common(vcpu, context, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002948}
2949
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002950static int paging32_init_context(struct kvm_vcpu *vcpu,
2951 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002952{
Joerg Roedel2d48a982010-09-10 17:31:01 +02002953 context->nx = false;
2954
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002955 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002956
2957 context->new_cr3 = paging_new_cr3;
2958 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002959 context->gva_to_gpa = paging32_gva_to_gpa;
2960 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002961 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002962 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002963 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002964 context->root_level = PT32_ROOT_LEVEL;
2965 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002966 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002967 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002968 return 0;
2969}
2970
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002971static int paging32E_init_context(struct kvm_vcpu *vcpu,
2972 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002973{
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002974 return paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002975}
2976
Joerg Roedelfb72d162008-02-07 13:47:44 +01002977static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2978{
Joerg Roedel14dfe852010-09-10 17:30:49 +02002979 struct kvm_mmu *context = vcpu->arch.walk_mmu;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002980
Avi Kivityc445f8e2010-12-21 16:26:01 +02002981 context->base_role.word = 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002982 context->new_cr3 = nonpaging_new_cr3;
2983 context->page_fault = tdp_page_fault;
2984 context->free = nonpaging_free;
2985 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002986 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002987 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002988 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002989 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002990 context->direct_map = true;
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02002991 context->set_cr3 = kvm_x86_ops->set_tdp_cr3;
Joerg Roedel5777ed32010-09-10 17:30:42 +02002992 context->get_cr3 = get_cr3;
Joerg Roedelcb659db2010-09-10 17:30:43 +02002993 context->inject_page_fault = kvm_inject_page_fault;
Joerg Roedel2d48a982010-09-10 17:31:01 +02002994 context->nx = is_nx(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002995
2996 if (!is_paging(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002997 context->nx = false;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002998 context->gva_to_gpa = nonpaging_gva_to_gpa;
2999 context->root_level = 0;
3000 } else if (is_long_mode(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003001 context->nx = is_nx(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003002 reset_rsvds_bits_mask(vcpu, context, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01003003 context->gva_to_gpa = paging64_gva_to_gpa;
3004 context->root_level = PT64_ROOT_LEVEL;
3005 } else if (is_pae(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003006 context->nx = is_nx(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003007 reset_rsvds_bits_mask(vcpu, context, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01003008 context->gva_to_gpa = paging64_gva_to_gpa;
3009 context->root_level = PT32E_ROOT_LEVEL;
3010 } else {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003011 context->nx = false;
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003012 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01003013 context->gva_to_gpa = paging32_gva_to_gpa;
3014 context->root_level = PT32_ROOT_LEVEL;
3015 }
3016
3017 return 0;
3018}
3019
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003020int kvm_init_shadow_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003021{
Avi Kivitya770f6f2008-12-21 19:20:09 +02003022 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003023 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003024 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003025
3026 if (!is_paging(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003027 r = nonpaging_init_context(vcpu, context);
Avi Kivitya9058ec2006-12-29 16:49:37 -08003028 else if (is_long_mode(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003029 r = paging64_init_context(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003030 else if (is_pae(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003031 r = paging32E_init_context(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003032 else
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003033 r = paging32_init_context(vcpu, context);
Avi Kivitya770f6f2008-12-21 19:20:09 +02003034
Avi Kivity5b7e0102010-04-14 19:20:03 +03003035 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
Joerg Roedelf43addd2010-09-10 17:30:40 +02003036 vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003037
3038 return r;
3039}
3040EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu);
3041
3042static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
3043{
Joerg Roedel14dfe852010-09-10 17:30:49 +02003044 int r = kvm_init_shadow_mmu(vcpu, vcpu->arch.walk_mmu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003045
Joerg Roedel14dfe852010-09-10 17:30:49 +02003046 vcpu->arch.walk_mmu->set_cr3 = kvm_x86_ops->set_cr3;
3047 vcpu->arch.walk_mmu->get_cr3 = get_cr3;
3048 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
Avi Kivitya770f6f2008-12-21 19:20:09 +02003049
3050 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003051}
3052
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003053static int init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
3054{
3055 struct kvm_mmu *g_context = &vcpu->arch.nested_mmu;
3056
3057 g_context->get_cr3 = get_cr3;
3058 g_context->inject_page_fault = kvm_inject_page_fault;
3059
3060 /*
3061 * Note that arch.mmu.gva_to_gpa translates l2_gva to l1_gpa. The
3062 * translation of l2_gpa to l1_gpa addresses is done using the
3063 * arch.nested_mmu.gva_to_gpa function. Basically the gva_to_gpa
3064 * functions between mmu and nested_mmu are swapped.
3065 */
3066 if (!is_paging(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003067 g_context->nx = false;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003068 g_context->root_level = 0;
3069 g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested;
3070 } else if (is_long_mode(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003071 g_context->nx = is_nx(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003072 reset_rsvds_bits_mask(vcpu, g_context, PT64_ROOT_LEVEL);
3073 g_context->root_level = PT64_ROOT_LEVEL;
3074 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
3075 } else if (is_pae(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003076 g_context->nx = is_nx(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003077 reset_rsvds_bits_mask(vcpu, g_context, PT32E_ROOT_LEVEL);
3078 g_context->root_level = PT32E_ROOT_LEVEL;
3079 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
3080 } else {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003081 g_context->nx = false;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003082 reset_rsvds_bits_mask(vcpu, g_context, PT32_ROOT_LEVEL);
3083 g_context->root_level = PT32_ROOT_LEVEL;
3084 g_context->gva_to_gpa = paging32_gva_to_gpa_nested;
3085 }
3086
3087 return 0;
3088}
3089
Joerg Roedelfb72d162008-02-07 13:47:44 +01003090static int init_kvm_mmu(struct kvm_vcpu *vcpu)
3091{
Anthony Liguori35149e22008-04-02 14:46:56 -05003092 vcpu->arch.update_pte.pfn = bad_pfn;
3093
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003094 if (mmu_is_nested(vcpu))
3095 return init_kvm_nested_mmu(vcpu);
3096 else if (tdp_enabled)
Joerg Roedelfb72d162008-02-07 13:47:44 +01003097 return init_kvm_tdp_mmu(vcpu);
3098 else
3099 return init_kvm_softmmu(vcpu);
3100}
3101
Avi Kivity6aa8b732006-12-10 02:21:36 -08003102static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
3103{
3104 ASSERT(vcpu);
Sheng Yang62ad0752010-05-12 16:40:41 +08003105 if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
3106 /* mmu.free() should set root_hpa = INVALID_PAGE */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003107 vcpu->arch.mmu.free(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003108}
3109
3110int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
3111{
Avi Kivity17c3ba92007-06-04 15:58:30 +03003112 destroy_kvm_mmu(vcpu);
3113 return init_kvm_mmu(vcpu);
3114}
Eddie Dong8668a3c2007-10-10 14:26:45 +08003115EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03003116
3117int kvm_mmu_load(struct kvm_vcpu *vcpu)
3118{
Avi Kivity714b93d2007-01-05 16:36:53 -08003119 int r;
3120
Avi Kivitye2dec932007-01-05 16:36:54 -08003121 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03003122 if (r)
3123 goto out;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003124 r = mmu_alloc_roots(vcpu);
Avi Kivity8facbbf2010-05-04 12:58:32 +03003125 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003126 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003127 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003128 if (r)
3129 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08003130 /* set_cr3() should ensure TLB has been flushed */
Joerg Roedelf43addd2010-09-10 17:30:40 +02003131 vcpu->arch.mmu.set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08003132out:
3133 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003134}
Avi Kivity17c3ba92007-06-04 15:58:30 +03003135EXPORT_SYMBOL_GPL(kvm_mmu_load);
3136
3137void kvm_mmu_unload(struct kvm_vcpu *vcpu)
3138{
3139 mmu_free_roots(vcpu);
3140}
Joerg Roedel4b161842010-09-10 17:31:03 +02003141EXPORT_SYMBOL_GPL(kvm_mmu_unload);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003142
Avi Kivity09072da2007-05-01 14:16:52 +03003143static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02003144 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02003145 u64 *spte)
3146{
3147 u64 pte;
3148 struct kvm_mmu_page *child;
3149
3150 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02003151 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03003152 if (is_last_spte(pte, sp->role.level))
Avi Kivitybe38d272010-06-06 14:31:27 +03003153 drop_spte(vcpu->kvm, spte, shadow_trap_nonpresent_pte);
Avi Kivityac1b7142007-03-08 17:13:32 +02003154 else {
3155 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03003156 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02003157 }
3158 }
Avi Kivityd555c332009-06-10 14:24:23 +03003159 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03003160 if (is_large_pte(pte))
3161 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02003162}
3163
Avi Kivity00284252007-05-01 16:53:31 +03003164static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02003165 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03003166 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02003167 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03003168{
Marcelo Tosatti30945382008-06-11 20:32:40 -03003169 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02003170 ++vcpu->kvm->stat.mmu_pde_zapped;
3171 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03003172 }
Avi Kivity00284252007-05-01 16:53:31 +03003173
Avi Kivity4cee5762007-11-18 16:37:07 +02003174 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity5b7e0102010-04-14 19:20:03 +03003175 if (!sp->role.cr4_pae)
Dong, Eddie489f1d62008-01-07 11:14:20 +02003176 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03003177 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02003178 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03003179}
3180
Avi Kivity79539ce2007-11-21 02:06:21 +02003181static bool need_remote_flush(u64 old, u64 new)
3182{
3183 if (!is_shadow_present_pte(old))
3184 return false;
3185 if (!is_shadow_present_pte(new))
3186 return true;
3187 if ((old ^ new) & PT64_BASE_ADDR_MASK)
3188 return true;
3189 old ^= PT64_NX_MASK;
3190 new ^= PT64_NX_MASK;
3191 return (old & ~new & PT64_PERM_MASK) != 0;
3192}
3193
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003194static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page,
3195 bool remote_flush, bool local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003196{
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003197 if (zap_page)
3198 return;
3199
3200 if (remote_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003201 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003202 else if (local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003203 kvm_mmu_flush_tlb(vcpu);
3204}
3205
Avi Kivity12b7d282007-09-23 14:10:49 +02003206static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
3207{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003208 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02003209
Sheng Yang7b523452008-04-25 21:13:50 +08003210 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02003211}
3212
Avi Kivityd7824ff2007-12-30 12:29:05 +02003213static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity72016f32010-03-15 13:59:53 +02003214 u64 gpte)
Avi Kivityd7824ff2007-12-30 12:29:05 +02003215{
3216 gfn_t gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05003217 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02003218
Avi Kivity43a37952009-06-10 14:12:05 +03003219 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02003220 return;
3221 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02003222
Andrea Arcangelie930bff2008-07-25 16:24:52 +02003223 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03003224 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05003225 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003226
Anthony Liguori35149e22008-04-02 14:46:56 -05003227 if (is_error_pfn(pfn)) {
3228 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02003229 return;
3230 }
Anthony Liguori35149e22008-04-02 14:46:56 -05003231 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02003232}
3233
Avi Kivity1b7fcd32008-05-15 13:51:35 +03003234static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
3235{
3236 u64 *spte = vcpu->arch.last_pte_updated;
3237
3238 if (spte
3239 && vcpu->arch.last_pte_gfn == gfn
3240 && shadow_accessed_mask
3241 && !(*spte & shadow_accessed_mask)
3242 && is_shadow_present_pte(*spte))
3243 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
3244}
3245
Avi Kivity09072da2007-05-01 14:16:52 +03003246void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003247 const u8 *new, int bytes,
3248 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08003249{
Avi Kivity9b7a0322007-01-05 16:36:45 -08003250 gfn_t gfn = gpa >> PAGE_SHIFT;
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003251 union kvm_mmu_page_role mask = { .word = 0 };
Avi Kivity4db35312007-11-21 15:28:32 +02003252 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003253 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003254 LIST_HEAD(invalid_list);
Dong, Eddie489f1d62008-01-07 11:14:20 +02003255 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003256 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003257 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003258 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003259 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003260 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03003261 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003262 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08003263 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02003264 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02003265 int r;
Avi Kivity08e850c2010-03-15 13:59:57 +02003266 int invlpg_counter;
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003267 bool remote_flush, local_flush, zap_page;
3268
3269 zap_page = remote_flush = local_flush = false;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003270
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003271 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivity72016f32010-03-15 13:59:53 +02003272
Avi Kivity08e850c2010-03-15 13:59:57 +02003273 invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
3274
3275 /*
3276 * Assume that the pte write on a page table of the same type
Xiao Guangrong49b26e22011-03-04 19:00:00 +08003277 * as the current vcpu paging mode since we update the sptes only
3278 * when they have the same mode.
Avi Kivity08e850c2010-03-15 13:59:57 +02003279 */
3280 if ((is_pae(vcpu) && bytes == 4) || !new) {
3281 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
3282 if (is_pae(vcpu)) {
3283 gpa &= ~(gpa_t)7;
3284 bytes = 8;
3285 }
3286 r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
3287 if (r)
3288 gentry = 0;
3289 new = (const u8 *)&gentry;
3290 }
3291
Avi Kivity72016f32010-03-15 13:59:53 +02003292 switch (bytes) {
3293 case 4:
3294 gentry = *(const u32 *)new;
3295 break;
3296 case 8:
3297 gentry = *(const u64 *)new;
3298 break;
3299 default:
3300 gentry = 0;
3301 break;
3302 }
3303
Avi Kivity72016f32010-03-15 13:59:53 +02003304 mmu_guess_page_from_pte_write(vcpu, gpa, gentry);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003305 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity08e850c2010-03-15 13:59:57 +02003306 if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
3307 gentry = 0;
Avi Kivityeb787d12007-12-31 15:27:49 +02003308 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02003309 ++vcpu->kvm->stat.mmu_pte_write;
Xiao Guangrong8b1fe172010-08-30 18:22:53 +08003310 trace_kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE);
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003311 if (guest_initiated) {
Xiao Guangrong1b7fd452011-03-04 18:58:02 +08003312 kvm_mmu_access_page(vcpu, gfn);
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003313 if (gfn == vcpu->arch.last_pt_write_gfn
3314 && !last_updated_pte_accessed(vcpu)) {
3315 ++vcpu->arch.last_pt_write_count;
3316 if (vcpu->arch.last_pt_write_count >= 3)
3317 flooded = 1;
3318 } else {
3319 vcpu->arch.last_pt_write_gfn = gfn;
3320 vcpu->arch.last_pt_write_count = 1;
3321 vcpu->arch.last_pte_updated = NULL;
3322 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08003323 }
Xiao Guangrong3246af02010-04-16 16:35:54 +08003324
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003325 mask.cr0_wp = mask.cr4_pae = mask.nxe = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003326 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node) {
Avi Kivity5b7e0102010-04-14 19:20:03 +03003327 pte_size = sp->role.cr4_pae ? 8 : 4;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003328 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03003329 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08003330 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003331 /*
3332 * Misaligned accesses are too much trouble to fix
3333 * up; also, they usually indicate a page is not used
3334 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08003335 *
3336 * If we're seeing too many writes to a page,
3337 * it may no longer be a page table, or we may be
3338 * forking, in which case it is better to unmap the
3339 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003340 */
3341 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02003342 gpa, bytes, sp->role.word);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003343 zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003344 &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02003345 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003346 continue;
3347 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08003348 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02003349 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02003350 npte = 1;
Avi Kivity5b7e0102010-04-14 19:20:03 +03003351 if (!sp->role.cr4_pae) {
Avi Kivityac1b7142007-03-08 17:13:32 +02003352 page_offset <<= 1; /* 32->64 */
3353 /*
3354 * A 32-bit pde maps 4MB while the shadow pdes map
3355 * only 2MB. So we need to double the offset again
3356 * and zap two pdes instead of one.
3357 */
3358 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03003359 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02003360 page_offset <<= 1;
3361 npte = 2;
3362 }
Avi Kivityfce06572007-05-01 16:44:05 +03003363 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003364 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02003365 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03003366 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003367 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003368 local_flush = true;
Avi Kivity4db35312007-11-21 15:28:32 +02003369 spte = &sp->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02003370 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02003371 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02003372 mmu_pte_write_zap_pte(vcpu, sp, spte);
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003373 if (gentry &&
3374 !((sp->role.word ^ vcpu->arch.mmu.base_role.word)
3375 & mask.word))
Avi Kivity72016f32010-03-15 13:59:53 +02003376 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003377 if (!remote_flush && need_remote_flush(entry, *spte))
3378 remote_flush = true;
Avi Kivityac1b7142007-03-08 17:13:32 +02003379 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003380 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08003381 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003382 mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003383 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong8b1fe172010-08-30 18:22:53 +08003384 trace_kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003385 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05003386 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
3387 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
3388 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02003389 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08003390}
3391
Avi Kivitya4360362007-01-05 16:36:45 -08003392int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
3393{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003394 gpa_t gpa;
3395 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08003396
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02003397 if (vcpu->arch.mmu.direct_map)
Avi Kivity60f24782009-08-27 13:37:06 +03003398 return 0;
3399
Gleb Natapov1871c602010-02-10 14:21:32 +02003400 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003401
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003402 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003403 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003404 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003405 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08003406}
Avi Kivity577bdc42008-07-19 08:57:05 +03003407EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08003408
Avi Kivity22d95b12007-09-14 20:26:06 +03003409void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08003410{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003411 LIST_HEAD(invalid_list);
Xiao Guangrong103ad252010-06-04 21:54:38 +08003412
Dave Hansene0df7b92010-08-19 18:11:05 -07003413 while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES &&
Izik Eidus3b80fff2009-07-28 15:26:58 -03003414 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02003415 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08003416
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003417 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02003418 struct kvm_mmu_page, link);
Dave Hansene0df7b92010-08-19 18:11:05 -07003419 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
Xiaotian Feng80b63fa2010-08-24 10:31:07 +08003420 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02003421 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08003422 }
3423}
Avi Kivityebeace82007-01-05 16:36:47 -08003424
Andre Przywaradc25e892010-12-21 11:12:07 +01003425int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code,
3426 void *insn, int insn_len)
Avi Kivity30677142007-10-28 18:48:59 +02003427{
3428 int r;
3429 enum emulation_result er;
3430
Gleb Natapov56028d02010-10-17 18:13:42 +02003431 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code, false);
Avi Kivity30677142007-10-28 18:48:59 +02003432 if (r < 0)
3433 goto out;
3434
3435 if (!r) {
3436 r = 1;
3437 goto out;
3438 }
3439
Avi Kivityb733bfb2007-10-28 18:52:05 +02003440 r = mmu_topup_memory_caches(vcpu);
3441 if (r)
3442 goto out;
3443
Andre Przywaradc25e892010-12-21 11:12:07 +01003444 er = x86_emulate_instruction(vcpu, cr2, 0, insn, insn_len);
Avi Kivity30677142007-10-28 18:48:59 +02003445
3446 switch (er) {
3447 case EMULATE_DONE:
3448 return 1;
3449 case EMULATE_DO_MMIO:
3450 ++vcpu->stat.mmio_exits;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03003451 /* fall through */
Avi Kivity30677142007-10-28 18:48:59 +02003452 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03003453 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02003454 default:
3455 BUG();
3456 }
3457out:
Avi Kivity30677142007-10-28 18:48:59 +02003458 return r;
3459}
3460EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
3461
Marcelo Tosattia7052892008-09-23 13:18:35 -03003462void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
3463{
Marcelo Tosattia7052892008-09-23 13:18:35 -03003464 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003465 kvm_mmu_flush_tlb(vcpu);
3466 ++vcpu->stat.invlpg;
3467}
3468EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
3469
Joerg Roedel18552672008-02-07 13:47:41 +01003470void kvm_enable_tdp(void)
3471{
3472 tdp_enabled = true;
3473}
3474EXPORT_SYMBOL_GPL(kvm_enable_tdp);
3475
Joerg Roedel5f4cb662008-07-14 20:36:36 +02003476void kvm_disable_tdp(void)
3477{
3478 tdp_enabled = false;
3479}
3480EXPORT_SYMBOL_GPL(kvm_disable_tdp);
3481
Avi Kivity6aa8b732006-12-10 02:21:36 -08003482static void free_mmu_pages(struct kvm_vcpu *vcpu)
3483{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003484 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Joerg Roedel81407ca2010-09-10 17:31:00 +02003485 if (vcpu->arch.mmu.lm_root != NULL)
3486 free_page((unsigned long)vcpu->arch.mmu.lm_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003487}
3488
3489static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
3490{
Avi Kivity17ac10a2007-01-05 16:36:40 -08003491 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003492 int i;
3493
3494 ASSERT(vcpu);
3495
Avi Kivity17ac10a2007-01-05 16:36:40 -08003496 /*
3497 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
3498 * Therefore we need to allocate shadow page tables in the first
3499 * 4GB of memory, which happens to fit the DMA32 zone.
3500 */
3501 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
3502 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08003503 return -ENOMEM;
3504
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003505 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003506 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003507 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003508
Avi Kivity6aa8b732006-12-10 02:21:36 -08003509 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003510}
3511
Ingo Molnar8018c272006-12-29 16:50:01 -08003512int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003513{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003514 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003515 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003516
Ingo Molnar8018c272006-12-29 16:50:01 -08003517 return alloc_mmu_pages(vcpu);
3518}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003519
Ingo Molnar8018c272006-12-29 16:50:01 -08003520int kvm_mmu_setup(struct kvm_vcpu *vcpu)
3521{
3522 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003523 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08003524
Ingo Molnar8018c272006-12-29 16:50:01 -08003525 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003526}
3527
Avi Kivity90cb0522007-07-17 13:04:56 +03003528void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003529{
Avi Kivity4db35312007-11-21 15:28:32 +02003530 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003531
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003532 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003533 int i;
3534 u64 *pt;
3535
Sheng Yang291f26b2008-10-16 17:30:57 +08003536 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003537 continue;
3538
Avi Kivity4db35312007-11-21 15:28:32 +02003539 pt = sp->spt;
Avi Kivity8234b222010-12-27 12:08:45 +02003540 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Xiao Guangrongda8dc752011-03-04 18:56:41 +08003541 if (!is_shadow_present_pte(pt[i]) ||
3542 !is_last_spte(pt[i], sp->role.level))
3543 continue;
3544
3545 if (is_large_pte(pt[i])) {
Avi Kivity8234b222010-12-27 12:08:45 +02003546 drop_spte(kvm, &pt[i],
3547 shadow_trap_nonpresent_pte);
3548 --kvm->stat.lpages;
Xiao Guangrongda8dc752011-03-04 18:56:41 +08003549 continue;
Avi Kivity8234b222010-12-27 12:08:45 +02003550 }
Xiao Guangrongda8dc752011-03-04 18:56:41 +08003551
Avi Kivity6aa8b732006-12-10 02:21:36 -08003552 /* avoid RMW */
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003553 if (is_writable_pte(pt[i]))
Takuya Yoshikawa700e1b12010-12-06 01:11:33 +09003554 update_spte(&pt[i], pt[i] & ~PT_WRITABLE_MASK);
Avi Kivity8234b222010-12-27 12:08:45 +02003555 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003556 }
Avi Kivity171d5952008-08-27 16:40:51 +03003557 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003558}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003559
Avi Kivity90cb0522007-07-17 13:04:56 +03003560void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03003561{
Avi Kivity4db35312007-11-21 15:28:32 +02003562 struct kvm_mmu_page *sp, *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003563 LIST_HEAD(invalid_list);
Dor Laore0fa8262007-03-30 13:06:33 +03003564
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003565 spin_lock(&kvm->mmu_lock);
Xiao Guangrong3246af02010-04-16 16:35:54 +08003566restart:
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003567 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003568 if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list))
Xiao Guangrong3246af02010-04-16 16:35:54 +08003569 goto restart;
3570
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003571 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003572 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03003573}
3574
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003575static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm,
3576 struct list_head *invalid_list)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003577{
3578 struct kvm_mmu_page *page;
3579
3580 page = container_of(kvm->arch.active_mmu_pages.prev,
3581 struct kvm_mmu_page, link);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003582 return kvm_mmu_prepare_zap_page(kvm, page, invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003583}
3584
Dave Chinner7f8275d2010-07-19 14:56:17 +10003585static int mmu_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003586{
3587 struct kvm *kvm;
3588 struct kvm *kvm_freed = NULL;
Dave Hansen45221ab2010-08-19 18:11:37 -07003589
3590 if (nr_to_scan == 0)
3591 goto out;
Izik Eidus3ee16c82008-03-30 15:17:21 +03003592
Jan Kiszkae935b832011-02-08 12:55:33 +01003593 raw_spin_lock(&kvm_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003594
3595 list_for_each_entry(kvm, &vm_list, vm_list) {
Dave Hansen45221ab2010-08-19 18:11:37 -07003596 int idx, freed_pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003597 LIST_HEAD(invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003598
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003599 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003600 spin_lock(&kvm->mmu_lock);
Dave Hansen45221ab2010-08-19 18:11:37 -07003601 if (!kvm_freed && nr_to_scan > 0 &&
3602 kvm->arch.n_used_mmu_pages > 0) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003603 freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm,
3604 &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003605 kvm_freed = kvm;
3606 }
3607 nr_to_scan--;
3608
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003609 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003610 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003611 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003612 }
3613 if (kvm_freed)
3614 list_move_tail(&kvm_freed->vm_list, &vm_list);
3615
Jan Kiszkae935b832011-02-08 12:55:33 +01003616 raw_spin_unlock(&kvm_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003617
Dave Hansen45221ab2010-08-19 18:11:37 -07003618out:
3619 return percpu_counter_read_positive(&kvm_total_used_mmu_pages);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003620}
3621
3622static struct shrinker mmu_shrinker = {
3623 .shrink = mmu_shrink,
3624 .seeks = DEFAULT_SEEKS * 10,
3625};
3626
Ingo Molnar2ddfd202008-05-22 10:37:48 +02003627static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03003628{
3629 if (pte_chain_cache)
3630 kmem_cache_destroy(pte_chain_cache);
3631 if (rmap_desc_cache)
3632 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003633 if (mmu_page_header_cache)
3634 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003635}
3636
3637int kvm_mmu_module_init(void)
3638{
3639 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
3640 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09003641 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003642 if (!pte_chain_cache)
3643 goto nomem;
3644 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
3645 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09003646 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003647 if (!rmap_desc_cache)
3648 goto nomem;
3649
Avi Kivityd3d25b02007-05-30 12:34:53 +03003650 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
3651 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09003652 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003653 if (!mmu_page_header_cache)
3654 goto nomem;
3655
Wei Yongjun45bf21a2010-08-23 16:13:15 +08003656 if (percpu_counter_init(&kvm_total_used_mmu_pages, 0))
3657 goto nomem;
3658
Izik Eidus3ee16c82008-03-30 15:17:21 +03003659 register_shrinker(&mmu_shrinker);
3660
Avi Kivityb5a33a72007-04-15 16:31:09 +03003661 return 0;
3662
3663nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03003664 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003665 return -ENOMEM;
3666}
3667
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003668/*
3669 * Caculate mmu pages needed for kvm.
3670 */
3671unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3672{
3673 int i;
3674 unsigned int nr_mmu_pages;
3675 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003676 struct kvm_memslots *slots;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003677
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003678 slots = kvm_memslots(kvm);
3679
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003680 for (i = 0; i < slots->nmemslots; i++)
3681 nr_pages += slots->memslots[i].npages;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003682
3683 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3684 nr_mmu_pages = max(nr_mmu_pages,
3685 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3686
3687 return nr_mmu_pages;
3688}
3689
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003690static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3691 unsigned len)
3692{
3693 if (len > buffer->len)
3694 return NULL;
3695 return buffer->ptr;
3696}
3697
3698static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3699 unsigned len)
3700{
3701 void *ret;
3702
3703 ret = pv_mmu_peek_buffer(buffer, len);
3704 if (!ret)
3705 return ret;
3706 buffer->ptr += len;
3707 buffer->len -= len;
3708 buffer->processed += len;
3709 return ret;
3710}
3711
3712static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3713 gpa_t addr, gpa_t value)
3714{
3715 int bytes = 8;
3716 int r;
3717
3718 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3719 bytes = 4;
3720
3721 r = mmu_topup_memory_caches(vcpu);
3722 if (r)
3723 return r;
3724
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003725 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003726 return -EFAULT;
3727
3728 return 1;
3729}
3730
3731static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3732{
Avi Kivity9f8fe502010-12-05 17:30:00 +02003733 (void)kvm_set_cr3(vcpu, kvm_read_cr3(vcpu));
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003734 return 1;
3735}
3736
3737static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3738{
3739 spin_lock(&vcpu->kvm->mmu_lock);
3740 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3741 spin_unlock(&vcpu->kvm->mmu_lock);
3742 return 1;
3743}
3744
3745static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3746 struct kvm_pv_mmu_op_buffer *buffer)
3747{
3748 struct kvm_mmu_op_header *header;
3749
3750 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3751 if (!header)
3752 return 0;
3753 switch (header->op) {
3754 case KVM_MMU_OP_WRITE_PTE: {
3755 struct kvm_mmu_op_write_pte *wpte;
3756
3757 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3758 if (!wpte)
3759 return 0;
3760 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3761 wpte->pte_val);
3762 }
3763 case KVM_MMU_OP_FLUSH_TLB: {
3764 struct kvm_mmu_op_flush_tlb *ftlb;
3765
3766 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3767 if (!ftlb)
3768 return 0;
3769 return kvm_pv_mmu_flush_tlb(vcpu);
3770 }
3771 case KVM_MMU_OP_RELEASE_PT: {
3772 struct kvm_mmu_op_release_pt *rpt;
3773
3774 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3775 if (!rpt)
3776 return 0;
3777 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3778 }
3779 default: return 0;
3780 }
3781}
3782
3783int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3784 gpa_t addr, unsigned long *ret)
3785{
3786 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003787 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003788
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003789 buffer->ptr = buffer->buf;
3790 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3791 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003792
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003793 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003794 if (r)
3795 goto out;
3796
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003797 while (buffer->len) {
3798 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003799 if (r < 0)
3800 goto out;
3801 if (r == 0)
3802 break;
3803 }
3804
3805 r = 1;
3806out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003807 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003808 return r;
3809}
3810
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003811int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3812{
3813 struct kvm_shadow_walk_iterator iterator;
3814 int nr_sptes = 0;
3815
3816 spin_lock(&vcpu->kvm->mmu_lock);
3817 for_each_shadow_entry(vcpu, addr, iterator) {
3818 sptes[iterator.level-1] = *iterator.sptep;
3819 nr_sptes++;
3820 if (!is_shadow_present_pte(*iterator.sptep))
3821 break;
3822 }
3823 spin_unlock(&vcpu->kvm->mmu_lock);
3824
3825 return nr_sptes;
3826}
3827EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3828
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08003829void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
3830{
3831 ASSERT(vcpu);
3832
3833 destroy_kvm_mmu(vcpu);
3834 free_mmu_pages(vcpu);
3835 mmu_free_memory_caches(vcpu);
Xiao Guangrongb034cf02010-12-23 16:08:35 +08003836}
3837
3838#ifdef CONFIG_KVM_MMU_AUDIT
3839#include "mmu_audit.c"
3840#else
3841static void mmu_audit_disable(void) { }
3842#endif
3843
3844void kvm_mmu_module_exit(void)
3845{
3846 mmu_destroy_caches();
3847 percpu_counter_destroy(&kvm_total_used_mmu_pages);
3848 unregister_shrinker(&mmu_shrinker);
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08003849 mmu_audit_disable();
3850}