blob: 982718fe12a7b1700838d559005822e7d6ec55f2 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * MMU support
8 *
9 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +020010 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080011 *
12 * Authors:
13 * Yaniv Kamay <yaniv@qumranet.com>
14 * Avi Kivity <avi@qumranet.com>
15 *
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
18 *
19 */
Avi Kivity6aa8b732006-12-10 02:21:36 -080020
Gleb Natapovaf585b92010-10-14 11:22:46 +020021#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080022#include "mmu.h"
Avi Kivity836a1b32010-01-21 15:31:49 +020023#include "x86.h"
Avi Kivity6de4f3a2009-05-31 22:58:47 +030024#include "kvm_cache_regs.h"
Gleb Natapovaf585b92010-10-14 11:22:46 +020025#include "x86.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080026
Avi Kivityedf88412007-12-16 11:02:48 +020027#include <linux/kvm_host.h>
Avi Kivitye4956062007-06-28 14:15:57 -040028#include <linux/types.h>
29#include <linux/string.h>
30#include <linux/mm.h>
31#include <linux/highmem.h>
32#include <linux/module.h>
Izik Eidus448353c2007-11-26 14:08:14 +020033#include <linux/swap.h>
Marcelo Tosatti05da4552008-02-23 11:44:30 -030034#include <linux/hugetlb.h>
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050035#include <linux/compiler.h>
Marcelo Tosattibc6678a2009-12-23 14:35:21 -020036#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Huang Yingbf998152010-05-31 14:28:19 +080038#include <linux/uaccess.h>
Avi Kivitye4956062007-06-28 14:15:57 -040039
40#include <asm/page.h>
41#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020042#include <asm/io.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020043#include <asm/vmx.h>
Avi Kivitye4956062007-06-28 14:15:57 -040044
Joerg Roedel18552672008-02-07 13:47:41 +010045/*
46 * When setting this variable to true it enables Two-Dimensional-Paging
47 * where the hardware walks 2 page tables:
48 * 1. the guest-virtual to guest-physical
49 * 2. while doing 1. it walks guest-physical to host-physical
50 * If the hardware supports that we don't need to do shadow paging.
51 */
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050052bool tdp_enabled = false;
Joerg Roedel18552672008-02-07 13:47:41 +010053
Xiao Guangrong8b1fe172010-08-30 18:22:53 +080054enum {
55 AUDIT_PRE_PAGE_FAULT,
56 AUDIT_POST_PAGE_FAULT,
57 AUDIT_PRE_PTE_WRITE,
Xiao Guangrong69030742010-09-27 18:09:29 +080058 AUDIT_POST_PTE_WRITE,
59 AUDIT_PRE_SYNC,
60 AUDIT_POST_SYNC
Xiao Guangrong8b1fe172010-08-30 18:22:53 +080061};
62
63char *audit_point_name[] = {
64 "pre page fault",
65 "post page fault",
66 "pre pte write",
Xiao Guangrong69030742010-09-27 18:09:29 +080067 "post pte write",
68 "pre sync",
69 "post sync"
Xiao Guangrong8b1fe172010-08-30 18:22:53 +080070};
71
Avi Kivity37a7d8b2007-01-05 16:36:56 -080072#undef MMU_DEBUG
73
Avi Kivity37a7d8b2007-01-05 16:36:56 -080074#ifdef MMU_DEBUG
75
76#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
77#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
78
79#else
80
81#define pgprintk(x...) do { } while (0)
82#define rmap_printk(x...) do { } while (0)
83
84#endif
85
Xiao Guangrong8b1fe172010-08-30 18:22:53 +080086#ifdef MMU_DEBUG
Avi Kivity6ada8cc2008-06-22 16:45:24 +030087static int dbg = 0;
88module_param(dbg, bool, 0644);
Avi Kivity37a7d8b2007-01-05 16:36:56 -080089#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080090
Marcelo Tosatti582801a2008-09-23 13:18:41 -030091static int oos_shadow = 1;
92module_param(oos_shadow, bool, 0644);
93
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080094#ifndef MMU_DEBUG
95#define ASSERT(x) do { } while (0)
96#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080097#define ASSERT(x) \
98 if (!(x)) { \
99 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
100 __FILE__, __LINE__, #x); \
101 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800102#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800103
Xiao Guangrong957ed9e2010-08-22 19:12:48 +0800104#define PTE_PREFETCH_NUM 8
105
Avi Kivity6aa8b732006-12-10 02:21:36 -0800106#define PT_FIRST_AVAIL_BITS_SHIFT 9
107#define PT64_SECOND_AVAIL_BITS_SHIFT 52
108
Avi Kivity6aa8b732006-12-10 02:21:36 -0800109#define PT64_LEVEL_BITS 9
110
111#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400112 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800113
Avi Kivity6aa8b732006-12-10 02:21:36 -0800114#define PT64_INDEX(address, level)\
115 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
116
117
118#define PT32_LEVEL_BITS 10
119
120#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400121 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800122
Joerg Roedele04da982009-07-27 16:30:45 +0200123#define PT32_LVL_OFFSET_MASK(level) \
124 (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
125 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800126
127#define PT32_INDEX(address, level)\
128 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
129
130
Avi Kivity27aba762007-03-09 13:04:31 +0200131#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800132#define PT64_DIR_BASE_ADDR_MASK \
133 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200134#define PT64_LVL_ADDR_MASK(level) \
135 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
136 * PT64_LEVEL_BITS))) - 1))
137#define PT64_LVL_OFFSET_MASK(level) \
138 (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
139 * PT64_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800140
141#define PT32_BASE_ADDR_MASK PAGE_MASK
142#define PT32_DIR_BASE_ADDR_MASK \
143 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200144#define PT32_LVL_ADDR_MASK(level) \
145 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
146 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800147
Avi Kivity79539ce2007-11-21 02:06:21 +0200148#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
149 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800150
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800151#define PTE_LIST_EXT 4
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800152
Avi Kivityfe135d22007-12-09 16:15:46 +0200153#define ACC_EXEC_MASK 1
154#define ACC_WRITE_MASK PT_WRITABLE_MASK
155#define ACC_USER_MASK PT_USER_MASK
156#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
157
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200158#include <trace/events/kvm.h>
159
Avi Kivity07420172009-07-06 12:21:32 +0300160#define CREATE_TRACE_POINTS
161#include "mmutrace.h"
162
Izik Eidus14032832009-09-23 21:47:17 +0300163#define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
164
Avi Kivity135f8c22008-08-21 17:49:56 +0300165#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
166
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800167struct pte_list_desc {
168 u64 *sptes[PTE_LIST_EXT];
169 struct pte_list_desc *more;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800170};
171
Avi Kivity2d111232008-12-25 14:39:47 +0200172struct kvm_shadow_walk_iterator {
173 u64 addr;
174 hpa_t shadow_addr;
175 int level;
176 u64 *sptep;
177 unsigned index;
178};
179
180#define for_each_shadow_entry(_vcpu, _addr, _walker) \
181 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
182 shadow_walk_okay(&(_walker)); \
183 shadow_walk_next(&(_walker)))
184
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800185static struct kmem_cache *pte_list_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300186static struct kmem_cache *mmu_page_header_cache;
Dave Hansen45221ab2010-08-19 18:11:37 -0700187static struct percpu_counter kvm_total_used_mmu_pages;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300188
Sheng Yang7b523452008-04-25 21:13:50 +0800189static u64 __read_mostly shadow_nx_mask;
190static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
191static u64 __read_mostly shadow_user_mask;
192static u64 __read_mostly shadow_accessed_mask;
193static u64 __read_mostly shadow_dirty_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200194
Dong, Eddie82725b22009-03-30 16:21:08 +0800195static inline u64 rsvd_bits(int s, int e)
196{
197 return ((1ULL << (e - s + 1)) - 1) << s;
198}
199
Sheng Yang7b523452008-04-25 21:13:50 +0800200void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang4b12f0d2009-04-27 20:35:42 +0800201 u64 dirty_mask, u64 nx_mask, u64 x_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800202{
203 shadow_user_mask = user_mask;
204 shadow_accessed_mask = accessed_mask;
205 shadow_dirty_mask = dirty_mask;
206 shadow_nx_mask = nx_mask;
207 shadow_x_mask = x_mask;
208}
209EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
210
Avi Kivity6aa8b732006-12-10 02:21:36 -0800211static int is_cpuid_PSE36(void)
212{
213 return 1;
214}
215
Avi Kivity73b10872007-01-26 00:56:41 -0800216static int is_nx(struct kvm_vcpu *vcpu)
217{
Avi Kivityf6801df2010-01-21 15:31:50 +0200218 return vcpu->arch.efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800219}
220
Avi Kivityc7addb92007-09-16 18:58:32 +0200221static int is_shadow_present_pte(u64 pte)
222{
Xiao Guangrongc3707952011-07-12 03:28:04 +0800223 return pte & PT_PRESENT_MASK;
Avi Kivityc7addb92007-09-16 18:58:32 +0200224}
225
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300226static int is_large_pte(u64 pte)
227{
228 return pte & PT_PAGE_SIZE_MASK;
229}
230
Avi Kivity43a37952009-06-10 14:12:05 +0300231static int is_dirty_gpte(unsigned long pte)
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200232{
Avi Kivity439e2182009-06-10 12:56:54 +0300233 return pte & PT_DIRTY_MASK;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200234}
235
Avi Kivity43a37952009-06-10 14:12:05 +0300236static int is_rmap_spte(u64 pte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800237{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200238 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800239}
240
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300241static int is_last_spte(u64 pte, int level)
242{
243 if (level == PT_PAGE_TABLE_LEVEL)
244 return 1;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200245 if (is_large_pte(pte))
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300246 return 1;
247 return 0;
248}
249
Anthony Liguori35149e22008-04-02 14:46:56 -0500250static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200251{
Anthony Liguori35149e22008-04-02 14:46:56 -0500252 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200253}
254
Avi Kivityda928522007-11-21 13:54:47 +0200255static gfn_t pse36_gfn_delta(u32 gpte)
256{
257 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
258
259 return (gpte & PT32_DIR_PSE36_MASK) << shift;
260}
261
Avi Kivityd555c332009-06-10 14:24:23 +0300262static void __set_spte(u64 *sptep, u64 spte)
Avi Kivitye663ee62007-05-31 15:46:04 +0300263{
H. Peter Anvin7645e432010-08-06 12:18:11 -0700264 set_64bit(sptep, spte);
Avi Kivitye663ee62007-05-31 15:46:04 +0300265}
266
Avi Kivitya9221dd2010-06-06 14:48:06 +0300267static u64 __xchg_spte(u64 *sptep, u64 new_spte)
268{
269#ifdef CONFIG_X86_64
270 return xchg(sptep, new_spte);
271#else
272 u64 old_spte;
273
274 do {
275 old_spte = *sptep;
276 } while (cmpxchg64(sptep, old_spte, new_spte) != old_spte);
277
278 return old_spte;
279#endif
280}
281
Xiao Guangrong8672b722010-08-02 16:14:04 +0800282static bool spte_has_volatile_bits(u64 spte)
283{
284 if (!shadow_accessed_mask)
285 return false;
286
287 if (!is_shadow_present_pte(spte))
288 return false;
289
Xiao Guangrong41327792010-08-02 16:15:08 +0800290 if ((spte & shadow_accessed_mask) &&
291 (!is_writable_pte(spte) || (spte & shadow_dirty_mask)))
Xiao Guangrong8672b722010-08-02 16:14:04 +0800292 return false;
293
294 return true;
295}
296
Xiao Guangrong41327792010-08-02 16:15:08 +0800297static bool spte_is_bit_cleared(u64 old_spte, u64 new_spte, u64 bit_mask)
298{
299 return (old_spte & bit_mask) && !(new_spte & bit_mask);
300}
301
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800302/* Rules for using mmu_spte_set:
303 * Set the sptep from nonpresent to present.
304 * Note: the sptep being assigned *must* be either not present
305 * or in a state where the hardware will not attempt to update
306 * the spte.
307 */
308static void mmu_spte_set(u64 *sptep, u64 new_spte)
309{
310 WARN_ON(is_shadow_present_pte(*sptep));
311 __set_spte(sptep, new_spte);
312}
313
314/* Rules for using mmu_spte_update:
315 * Update the state bits, it means the mapped pfn is not changged.
316 */
317static void mmu_spte_update(u64 *sptep, u64 new_spte)
Avi Kivityb79b93f2010-06-06 15:46:44 +0300318{
Xiao Guangrong41327792010-08-02 16:15:08 +0800319 u64 mask, old_spte = *sptep;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300320
Xiao Guangrong41327792010-08-02 16:15:08 +0800321 WARN_ON(!is_rmap_spte(new_spte));
322
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800323 if (!is_shadow_present_pte(old_spte))
324 return mmu_spte_set(sptep, new_spte);
325
Xiao Guangrong41327792010-08-02 16:15:08 +0800326 new_spte |= old_spte & shadow_dirty_mask;
327
328 mask = shadow_accessed_mask;
329 if (is_writable_pte(old_spte))
330 mask |= shadow_dirty_mask;
331
332 if (!spte_has_volatile_bits(old_spte) || (new_spte & mask) == mask)
Avi Kivityb79b93f2010-06-06 15:46:44 +0300333 __set_spte(sptep, new_spte);
Xiao Guangrong41327792010-08-02 16:15:08 +0800334 else
Avi Kivityb79b93f2010-06-06 15:46:44 +0300335 old_spte = __xchg_spte(sptep, new_spte);
Xiao Guangrong41327792010-08-02 16:15:08 +0800336
337 if (!shadow_accessed_mask)
338 return;
339
340 if (spte_is_bit_cleared(old_spte, new_spte, shadow_accessed_mask))
341 kvm_set_pfn_accessed(spte_to_pfn(old_spte));
342 if (spte_is_bit_cleared(old_spte, new_spte, shadow_dirty_mask))
343 kvm_set_pfn_dirty(spte_to_pfn(old_spte));
Avi Kivityb79b93f2010-06-06 15:46:44 +0300344}
345
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800346/*
347 * Rules for using mmu_spte_clear_track_bits:
348 * It sets the sptep from present to nonpresent, and track the
349 * state bits, it is used to clear the last level sptep.
350 */
351static int mmu_spte_clear_track_bits(u64 *sptep)
352{
353 pfn_t pfn;
354 u64 old_spte = *sptep;
355
356 if (!spte_has_volatile_bits(old_spte))
357 __set_spte(sptep, 0ull);
358 else
359 old_spte = __xchg_spte(sptep, 0ull);
360
361 if (!is_rmap_spte(old_spte))
362 return 0;
363
364 pfn = spte_to_pfn(old_spte);
365 if (!shadow_accessed_mask || old_spte & shadow_accessed_mask)
366 kvm_set_pfn_accessed(pfn);
367 if (!shadow_dirty_mask || (old_spte & shadow_dirty_mask))
368 kvm_set_pfn_dirty(pfn);
369 return 1;
370}
371
372/*
373 * Rules for using mmu_spte_clear_no_track:
374 * Directly clear spte without caring the state bits of sptep,
375 * it is used to set the upper level spte.
376 */
377static void mmu_spte_clear_no_track(u64 *sptep)
378{
379 __set_spte(sptep, 0ull);
380}
381
Avi Kivitye2dec932007-01-05 16:36:54 -0800382static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300383 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800384{
385 void *obj;
386
387 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800388 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800389 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300390 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800391 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800392 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800393 cache->objects[cache->nobjs++] = obj;
394 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800395 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800396}
397
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800398static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc,
399 struct kmem_cache *cache)
Avi Kivity714b93d2007-01-05 16:36:53 -0800400{
401 while (mc->nobjs)
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800402 kmem_cache_free(cache, mc->objects[--mc->nobjs]);
Avi Kivity714b93d2007-01-05 16:36:53 -0800403}
404
Avi Kivityc1158e62007-07-20 08:18:27 +0300405static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300406 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300407{
Xiao Guangrong842f22e2011-03-04 19:01:10 +0800408 void *page;
Avi Kivityc1158e62007-07-20 08:18:27 +0300409
410 if (cache->nobjs >= min)
411 return 0;
412 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Xiao Guangrong842f22e2011-03-04 19:01:10 +0800413 page = (void *)__get_free_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300414 if (!page)
415 return -ENOMEM;
Xiao Guangrong842f22e2011-03-04 19:01:10 +0800416 cache->objects[cache->nobjs++] = page;
Avi Kivityc1158e62007-07-20 08:18:27 +0300417 }
418 return 0;
419}
420
421static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
422{
423 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300424 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300425}
426
Avi Kivity8c438502007-04-16 11:53:17 +0300427static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
428{
429 int r;
430
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800431 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
Xiao Guangrong67052b32011-05-15 23:27:08 +0800432 pte_list_desc_cache, 8 + PTE_PREFETCH_NUM);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300433 if (r)
434 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800435 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300436 if (r)
437 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800438 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300439 mmu_page_header_cache, 4);
440out:
Avi Kivity8c438502007-04-16 11:53:17 +0300441 return r;
442}
443
Avi Kivity714b93d2007-01-05 16:36:53 -0800444static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
445{
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800446 mmu_free_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
447 pte_list_desc_cache);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800448 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800449 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache,
450 mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800451}
452
453static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
454 size_t size)
455{
456 void *p;
457
458 BUG_ON(!mc->nobjs);
459 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800460 return p;
461}
462
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800463static struct pte_list_desc *mmu_alloc_pte_list_desc(struct kvm_vcpu *vcpu)
Avi Kivity714b93d2007-01-05 16:36:53 -0800464{
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800465 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_list_desc_cache,
466 sizeof(struct pte_list_desc));
Avi Kivity714b93d2007-01-05 16:36:53 -0800467}
468
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800469static void mmu_free_pte_list_desc(struct pte_list_desc *pte_list_desc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800470{
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800471 kmem_cache_free(pte_list_desc_cache, pte_list_desc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800472}
473
Lai Jiangshan2032a932010-05-26 16:49:59 +0800474static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
475{
476 if (!sp->role.direct)
477 return sp->gfns[index];
478
479 return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS));
480}
481
482static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn)
483{
484 if (sp->role.direct)
485 BUG_ON(gfn != kvm_mmu_page_get_gfn(sp, index));
486 else
487 sp->gfns[index] = gfn;
488}
489
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800490/*
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900491 * Return the pointer to the large page information for a given gfn,
492 * handling slots that are not large page aligned.
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300493 */
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900494static struct kvm_lpage_info *lpage_info_slot(gfn_t gfn,
495 struct kvm_memory_slot *slot,
496 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300497{
498 unsigned long idx;
499
Joerg Roedel82855412010-07-01 16:00:11 +0200500 idx = (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
501 (slot->base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900502 return &slot->lpage_info[level - 2][idx];
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300503}
504
505static void account_shadowed(struct kvm *kvm, gfn_t gfn)
506{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200507 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900508 struct kvm_lpage_info *linfo;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200509 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300510
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300511 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200512 for (i = PT_DIRECTORY_LEVEL;
513 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900514 linfo = lpage_info_slot(gfn, slot, i);
515 linfo->write_count += 1;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200516 }
Xiao Guangrong332b2072011-05-15 23:20:27 +0800517 kvm->arch.indirect_shadow_pages++;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300518}
519
520static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
521{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200522 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900523 struct kvm_lpage_info *linfo;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200524 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300525
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300526 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200527 for (i = PT_DIRECTORY_LEVEL;
528 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900529 linfo = lpage_info_slot(gfn, slot, i);
530 linfo->write_count -= 1;
531 WARN_ON(linfo->write_count < 0);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200532 }
Xiao Guangrong332b2072011-05-15 23:20:27 +0800533 kvm->arch.indirect_shadow_pages--;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300534}
535
Joerg Roedeld25797b2009-07-27 16:30:43 +0200536static int has_wrprotected_page(struct kvm *kvm,
537 gfn_t gfn,
538 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300539{
Izik Eidus28430992008-10-03 17:40:32 +0300540 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900541 struct kvm_lpage_info *linfo;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300542
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300543 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300544 if (slot) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900545 linfo = lpage_info_slot(gfn, slot, level);
546 return linfo->write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300547 }
548
549 return 1;
550}
551
Joerg Roedeld25797b2009-07-27 16:30:43 +0200552static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300553{
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100554 unsigned long page_size;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200555 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300556
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100557 page_size = kvm_host_page_size(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300558
Joerg Roedeld25797b2009-07-27 16:30:43 +0200559 for (i = PT_PAGE_TABLE_LEVEL;
560 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
561 if (page_size >= KVM_HPAGE_SIZE(i))
562 ret = i;
563 else
564 break;
565 }
566
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300567 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300568}
569
Xiao Guangrong5d163b12011-03-09 15:43:00 +0800570static struct kvm_memory_slot *
571gfn_to_memslot_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t gfn,
572 bool no_dirty_log)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300573{
574 struct kvm_memory_slot *slot;
Xiao Guangrong5d163b12011-03-09 15:43:00 +0800575
576 slot = gfn_to_memslot(vcpu->kvm, gfn);
577 if (!slot || slot->flags & KVM_MEMSLOT_INVALID ||
578 (no_dirty_log && slot->dirty_bitmap))
579 slot = NULL;
580
581 return slot;
582}
583
584static bool mapping_level_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t large_gfn)
585{
Stevea0a8eab2011-06-17 10:25:39 +0800586 return !gfn_to_memslot_dirty_bitmap(vcpu, large_gfn, true);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800587}
588
589static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
590{
591 int host_level, level, max_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300592
Joerg Roedeld25797b2009-07-27 16:30:43 +0200593 host_level = host_mapping_level(vcpu->kvm, large_gfn);
594
595 if (host_level == PT_PAGE_TABLE_LEVEL)
596 return host_level;
597
Sheng Yang878403b2010-01-05 19:02:29 +0800598 max_level = kvm_x86_ops->get_lpage_level() < host_level ?
599 kvm_x86_ops->get_lpage_level() : host_level;
600
601 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200602 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
603 break;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200604
605 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300606}
607
608/*
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800609 * Pte mapping structures:
610 *
611 * If pte_list bit zero is zero, then pte_list point to the spte.
612 *
613 * If pte_list bit zero is one, (then pte_list & ~1) points to a struct
614 * pte_list_desc containing more mappings.
615 *
616 * Returns the number of pte entries before the spte was added or zero if
617 * the spte was not added.
618 *
619 */
620static int pte_list_add(struct kvm_vcpu *vcpu, u64 *spte,
621 unsigned long *pte_list)
622{
623 struct pte_list_desc *desc;
624 int i, count = 0;
625
626 if (!*pte_list) {
627 rmap_printk("pte_list_add: %p %llx 0->1\n", spte, *spte);
628 *pte_list = (unsigned long)spte;
629 } else if (!(*pte_list & 1)) {
630 rmap_printk("pte_list_add: %p %llx 1->many\n", spte, *spte);
631 desc = mmu_alloc_pte_list_desc(vcpu);
632 desc->sptes[0] = (u64 *)*pte_list;
633 desc->sptes[1] = spte;
634 *pte_list = (unsigned long)desc | 1;
635 ++count;
636 } else {
637 rmap_printk("pte_list_add: %p %llx many->many\n", spte, *spte);
638 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
639 while (desc->sptes[PTE_LIST_EXT-1] && desc->more) {
640 desc = desc->more;
641 count += PTE_LIST_EXT;
642 }
643 if (desc->sptes[PTE_LIST_EXT-1]) {
644 desc->more = mmu_alloc_pte_list_desc(vcpu);
645 desc = desc->more;
646 }
647 for (i = 0; desc->sptes[i]; ++i)
648 ++count;
649 desc->sptes[i] = spte;
650 }
651 return count;
652}
653
654static u64 *pte_list_next(unsigned long *pte_list, u64 *spte)
655{
656 struct pte_list_desc *desc;
657 u64 *prev_spte;
658 int i;
659
660 if (!*pte_list)
661 return NULL;
662 else if (!(*pte_list & 1)) {
663 if (!spte)
664 return (u64 *)*pte_list;
665 return NULL;
666 }
667 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
668 prev_spte = NULL;
669 while (desc) {
670 for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i) {
671 if (prev_spte == spte)
672 return desc->sptes[i];
673 prev_spte = desc->sptes[i];
674 }
675 desc = desc->more;
676 }
677 return NULL;
678}
679
680static void
681pte_list_desc_remove_entry(unsigned long *pte_list, struct pte_list_desc *desc,
682 int i, struct pte_list_desc *prev_desc)
683{
684 int j;
685
686 for (j = PTE_LIST_EXT - 1; !desc->sptes[j] && j > i; --j)
687 ;
688 desc->sptes[i] = desc->sptes[j];
689 desc->sptes[j] = NULL;
690 if (j != 0)
691 return;
692 if (!prev_desc && !desc->more)
693 *pte_list = (unsigned long)desc->sptes[0];
694 else
695 if (prev_desc)
696 prev_desc->more = desc->more;
697 else
698 *pte_list = (unsigned long)desc->more | 1;
699 mmu_free_pte_list_desc(desc);
700}
701
702static void pte_list_remove(u64 *spte, unsigned long *pte_list)
703{
704 struct pte_list_desc *desc;
705 struct pte_list_desc *prev_desc;
706 int i;
707
708 if (!*pte_list) {
709 printk(KERN_ERR "pte_list_remove: %p 0->BUG\n", spte);
710 BUG();
711 } else if (!(*pte_list & 1)) {
712 rmap_printk("pte_list_remove: %p 1->0\n", spte);
713 if ((u64 *)*pte_list != spte) {
714 printk(KERN_ERR "pte_list_remove: %p 1->BUG\n", spte);
715 BUG();
716 }
717 *pte_list = 0;
718 } else {
719 rmap_printk("pte_list_remove: %p many->many\n", spte);
720 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
721 prev_desc = NULL;
722 while (desc) {
723 for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i)
724 if (desc->sptes[i] == spte) {
725 pte_list_desc_remove_entry(pte_list,
726 desc, i,
727 prev_desc);
728 return;
729 }
730 prev_desc = desc;
731 desc = desc->more;
732 }
733 pr_err("pte_list_remove: %p many->many\n", spte);
734 BUG();
735 }
736}
737
Xiao Guangrong67052b32011-05-15 23:27:08 +0800738typedef void (*pte_list_walk_fn) (u64 *spte);
739static void pte_list_walk(unsigned long *pte_list, pte_list_walk_fn fn)
740{
741 struct pte_list_desc *desc;
742 int i;
743
744 if (!*pte_list)
745 return;
746
747 if (!(*pte_list & 1))
748 return fn((u64 *)*pte_list);
749
750 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
751 while (desc) {
752 for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i)
753 fn(desc->sptes[i]);
754 desc = desc->more;
755 }
756}
757
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800758/*
Izik Eidus290fc382007-09-27 14:11:22 +0200759 * Take gfn and return the reverse mapping to it.
Izik Eidus290fc382007-09-27 14:11:22 +0200760 */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200761static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
Izik Eidus290fc382007-09-27 14:11:22 +0200762{
763 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900764 struct kvm_lpage_info *linfo;
Izik Eidus290fc382007-09-27 14:11:22 +0200765
766 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200767 if (likely(level == PT_PAGE_TABLE_LEVEL))
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300768 return &slot->rmap[gfn - slot->base_gfn];
769
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900770 linfo = lpage_info_slot(gfn, slot, level);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300771
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900772 return &linfo->rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200773}
774
Joerg Roedel44ad9942009-07-27 16:30:42 +0200775static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800776{
Avi Kivity4db35312007-11-21 15:28:32 +0200777 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +0200778 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800779
Avi Kivity4db35312007-11-21 15:28:32 +0200780 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800781 kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200782 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800783 return pte_list_add(vcpu, spte, rmapp);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800784}
785
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800786static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800787{
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800788 return pte_list_next(rmapp, spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800789}
790
Izik Eidus290fc382007-09-27 14:11:22 +0200791static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800792{
Avi Kivity4db35312007-11-21 15:28:32 +0200793 struct kvm_mmu_page *sp;
Lai Jiangshan2032a932010-05-26 16:49:59 +0800794 gfn_t gfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200795 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800796
Avi Kivity4db35312007-11-21 15:28:32 +0200797 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800798 gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt);
799 rmapp = gfn_to_rmap(kvm, gfn, sp->role.level);
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800800 pte_list_remove(spte, rmapp);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800801}
802
Xiao Guangrongc3707952011-07-12 03:28:04 +0800803static void drop_spte(struct kvm *kvm, u64 *sptep)
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800804{
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800805 if (mmu_spte_clear_track_bits(sptep))
Marcelo Tosattieb45fda2010-10-25 11:58:22 -0200806 rmap_remove(kvm, sptep);
Avi Kivitybe38d272010-06-06 14:31:27 +0300807}
808
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200809static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200810{
Izik Eidus290fc382007-09-27 14:11:22 +0200811 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800812 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200813 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800814
Joerg Roedel44ad9942009-07-27 16:30:42 +0200815 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
Avi Kivity374cbac2007-01-05 16:36:43 -0800816
Izik Eidus98348e92007-10-16 14:42:30 +0200817 spte = rmap_next(kvm, rmapp, NULL);
818 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800819 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800820 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800821 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900822 if (is_writable_pte(*spte)) {
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800823 mmu_spte_update(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800824 write_protected = 1;
825 }
Izik Eidus9647c142007-10-16 14:43:46 +0200826 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800827 }
Izik Eidus855149a2008-03-20 18:17:24 +0200828
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300829 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200830 for (i = PT_DIRECTORY_LEVEL;
831 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
832 rmapp = gfn_to_rmap(kvm, gfn, i);
833 spte = rmap_next(kvm, rmapp, NULL);
834 while (spte) {
835 BUG_ON(!spte);
836 BUG_ON(!(*spte & PT_PRESENT_MASK));
837 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
838 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900839 if (is_writable_pte(*spte)) {
Xiao Guangrongc3707952011-07-12 03:28:04 +0800840 drop_spte(kvm, spte);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200841 --kvm->stat.lpages;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200842 spte = NULL;
843 write_protected = 1;
844 }
845 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300846 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300847 }
848
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200849 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800850}
851
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000852static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
853 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200854{
855 u64 *spte;
856 int need_tlb_flush = 0;
857
858 while ((spte = rmap_next(kvm, rmapp, NULL))) {
859 BUG_ON(!(*spte & PT_PRESENT_MASK));
860 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
Xiao Guangrongc3707952011-07-12 03:28:04 +0800861 drop_spte(kvm, spte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200862 need_tlb_flush = 1;
863 }
864 return need_tlb_flush;
865}
866
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000867static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
868 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +0300869{
870 int need_flush = 0;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800871 u64 *spte, new_spte;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300872 pte_t *ptep = (pte_t *)data;
873 pfn_t new_pfn;
874
875 WARN_ON(pte_huge(*ptep));
876 new_pfn = pte_pfn(*ptep);
877 spte = rmap_next(kvm, rmapp, NULL);
878 while (spte) {
879 BUG_ON(!is_shadow_present_pte(*spte));
880 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
881 need_flush = 1;
882 if (pte_write(*ptep)) {
Xiao Guangrongc3707952011-07-12 03:28:04 +0800883 drop_spte(kvm, spte);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300884 spte = rmap_next(kvm, rmapp, NULL);
885 } else {
886 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
887 new_spte |= (u64)new_pfn << PAGE_SHIFT;
888
889 new_spte &= ~PT_WRITABLE_MASK;
890 new_spte &= ~SPTE_HOST_WRITEABLE;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300891 new_spte &= ~shadow_accessed_mask;
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800892 mmu_spte_clear_track_bits(spte);
893 mmu_spte_set(spte, new_spte);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300894 spte = rmap_next(kvm, rmapp, spte);
895 }
896 }
897 if (need_flush)
898 kvm_flush_remote_tlbs(kvm);
899
900 return 0;
901}
902
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000903static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
904 unsigned long data,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300905 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000906 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200907{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200908 int i, j;
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200909 int ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200910 int retval = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200911 struct kvm_memslots *slots;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200912
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800913 slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200914
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200915 for (i = 0; i < slots->nmemslots; i++) {
916 struct kvm_memory_slot *memslot = &slots->memslots[i];
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200917 unsigned long start = memslot->userspace_addr;
918 unsigned long end;
919
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200920 end = start + (memslot->npages << PAGE_SHIFT);
921 if (hva >= start && hva < end) {
922 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900923 gfn_t gfn = memslot->base_gfn + gfn_offset;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200924
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200925 ret = handler(kvm, &memslot->rmap[gfn_offset], data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200926
927 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900928 struct kvm_lpage_info *linfo;
Andrea Arcangeli6e3e2432010-07-16 11:52:55 +0200929
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900930 linfo = lpage_info_slot(gfn, memslot,
931 PT_DIRECTORY_LEVEL + j);
932 ret |= handler(kvm, &linfo->rmap_pde, data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200933 }
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200934 trace_kvm_age_page(hva, memslot, ret);
935 retval |= ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200936 }
937 }
938
939 return retval;
940}
941
942int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
943{
Izik Eidus3da0dd42009-09-23 21:47:18 +0300944 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200945}
946
Izik Eidus3da0dd42009-09-23 21:47:18 +0300947void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
948{
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000949 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300950}
951
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000952static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
953 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200954{
955 u64 *spte;
956 int young = 0;
957
Rik van Riel6316e1c2010-02-03 16:11:03 -0500958 /*
959 * Emulate the accessed bit for EPT, by checking if this page has
960 * an EPT mapping, and clearing it if it does. On the next access,
961 * a new EPT mapping will be established.
962 * This has some overhead, but not as much as the cost of swapping
963 * out actively used pages or breaking up actively used hugepages.
964 */
Sheng Yang534e38b2008-09-08 15:12:30 +0800965 if (!shadow_accessed_mask)
Rik van Riel6316e1c2010-02-03 16:11:03 -0500966 return kvm_unmap_rmapp(kvm, rmapp, data);
Sheng Yang534e38b2008-09-08 15:12:30 +0800967
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200968 spte = rmap_next(kvm, rmapp, NULL);
969 while (spte) {
970 int _young;
971 u64 _spte = *spte;
972 BUG_ON(!(_spte & PT_PRESENT_MASK));
973 _young = _spte & PT_ACCESSED_MASK;
974 if (_young) {
975 young = 1;
976 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
977 }
978 spte = rmap_next(kvm, rmapp, spte);
979 }
980 return young;
981}
982
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800983static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
984 unsigned long data)
985{
986 u64 *spte;
987 int young = 0;
988
989 /*
990 * If there's no access bit in the secondary pte set by the
991 * hardware it's up to gup-fast/gup to set the access bit in
992 * the primary pte or in the page structure.
993 */
994 if (!shadow_accessed_mask)
995 goto out;
996
997 spte = rmap_next(kvm, rmapp, NULL);
998 while (spte) {
999 u64 _spte = *spte;
1000 BUG_ON(!(_spte & PT_PRESENT_MASK));
1001 young = _spte & PT_ACCESSED_MASK;
1002 if (young) {
1003 young = 1;
1004 break;
1005 }
1006 spte = rmap_next(kvm, rmapp, spte);
1007 }
1008out:
1009 return young;
1010}
1011
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001012#define RMAP_RECYCLE_THRESHOLD 1000
1013
Joerg Roedel852e3c12009-07-27 16:30:44 +02001014static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001015{
1016 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001017 struct kvm_mmu_page *sp;
1018
1019 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001020
Joerg Roedel852e3c12009-07-27 16:30:44 +02001021 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001022
Izik Eidus3da0dd42009-09-23 21:47:18 +03001023 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001024 kvm_flush_remote_tlbs(vcpu->kvm);
1025}
1026
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001027int kvm_age_hva(struct kvm *kvm, unsigned long hva)
1028{
Izik Eidus3da0dd42009-09-23 21:47:18 +03001029 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001030}
1031
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001032int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
1033{
1034 return kvm_handle_hva(kvm, hva, 0, kvm_test_age_rmapp);
1035}
1036
Yaozu Dongd6c69ee2007-04-25 14:17:25 +08001037#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +03001038static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001039{
Avi Kivity139bdb22007-01-05 16:36:50 -08001040 u64 *pos;
1041 u64 *end;
1042
Avi Kivity47ad8e62007-05-06 15:50:58 +03001043 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +03001044 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001045 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -08001046 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001047 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -08001048 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001049 return 1;
1050}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +08001051#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001052
Dave Hansen45221ab2010-08-19 18:11:37 -07001053/*
1054 * This value is the sum of all of the kvm instances's
1055 * kvm->arch.n_used_mmu_pages values. We need a global,
1056 * aggregate version in order to make the slab shrinker
1057 * faster
1058 */
1059static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, int nr)
1060{
1061 kvm->arch.n_used_mmu_pages += nr;
1062 percpu_counter_add(&kvm_total_used_mmu_pages, nr);
1063}
1064
Xiao Guangrongbd4c86e2011-07-12 03:27:14 +08001065/*
1066 * Remove the sp from shadow page cache, after call it,
1067 * we can not find this sp from the cache, and the shadow
1068 * page table is still valid.
1069 * It should be under the protection of mmu lock.
1070 */
1071static void kvm_mmu_isolate_page(struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -08001072{
Avi Kivity4db35312007-11-21 15:28:32 +02001073 ASSERT(is_empty_shadow_page(sp->spt));
Xiao Guangrong77758342010-06-04 21:53:54 +08001074 hlist_del(&sp->hash_link);
Lai Jiangshan2032a932010-05-26 16:49:59 +08001075 if (!sp->role.direct)
Xiao Guangrong842f22e2011-03-04 19:01:10 +08001076 free_page((unsigned long)sp->gfns);
Xiao Guangrongbd4c86e2011-07-12 03:27:14 +08001077}
1078
1079/*
1080 * Free the shadow page table and the sp, we can do it
1081 * out of the protection of mmu lock.
1082 */
1083static void kvm_mmu_free_page(struct kvm_mmu_page *sp)
1084{
1085 list_del(&sp->link);
1086 free_page((unsigned long)sp->spt);
Xiao Guangronge8ad9a72010-05-13 10:06:02 +08001087 kmem_cache_free(mmu_page_header_cache, sp);
Avi Kivity260746c2007-01-05 16:36:49 -08001088}
1089
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001090static unsigned kvm_page_table_hashfn(gfn_t gfn)
1091{
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001092 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001093}
1094
Xiao Guangrong67052b32011-05-15 23:27:08 +08001095static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
1096 struct kvm_mmu_page *sp, u64 *parent_pte)
1097{
1098 if (!parent_pte)
1099 return;
1100
1101 pte_list_add(vcpu, parent_pte, &sp->parent_ptes);
1102}
1103
1104static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
1105 u64 *parent_pte)
1106{
1107 pte_list_remove(parent_pte, &sp->parent_ptes);
1108}
1109
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001110static void drop_parent_pte(struct kvm_mmu_page *sp,
1111 u64 *parent_pte)
1112{
1113 mmu_page_remove_parent_pte(sp, parent_pte);
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08001114 mmu_spte_clear_no_track(parent_pte);
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001115}
1116
Avi Kivity25c0de22007-01-05 16:36:42 -08001117static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
Lai Jiangshan2032a932010-05-26 16:49:59 +08001118 u64 *parent_pte, int direct)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119{
Avi Kivity4db35312007-11-21 15:28:32 +02001120 struct kvm_mmu_page *sp;
Xiao Guangrong67052b32011-05-15 23:27:08 +08001121 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache,
1122 sizeof *sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001123 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Lai Jiangshan2032a932010-05-26 16:49:59 +08001124 if (!direct)
1125 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache,
1126 PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +02001127 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001128 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Sheng Yang291f26b2008-10-16 17:30:57 +08001129 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Xiao Guangrong67052b32011-05-15 23:27:08 +08001130 sp->parent_ptes = 0;
1131 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Dave Hansen45221ab2010-08-19 18:11:37 -07001132 kvm_mod_used_mmu_pages(vcpu->kvm, +1);
Avi Kivity4db35312007-11-21 15:28:32 +02001133 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001134}
1135
Xiao Guangrong67052b32011-05-15 23:27:08 +08001136static void mark_unsync(u64 *spte);
Xiao Guangrong6b184932010-04-16 21:29:17 +08001137static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001138{
Xiao Guangrong67052b32011-05-15 23:27:08 +08001139 pte_list_walk(&sp->parent_ptes, mark_unsync);
Xiao Guangrong1047df12010-06-11 21:35:15 +08001140}
1141
Xiao Guangrong67052b32011-05-15 23:27:08 +08001142static void mark_unsync(u64 *spte)
Xiao Guangrong1047df12010-06-11 21:35:15 +08001143{
Xiao Guangrong67052b32011-05-15 23:27:08 +08001144 struct kvm_mmu_page *sp;
Xiao Guangrong1047df12010-06-11 21:35:15 +08001145 unsigned int index;
1146
Xiao Guangrong67052b32011-05-15 23:27:08 +08001147 sp = page_header(__pa(spte));
Xiao Guangrong1047df12010-06-11 21:35:15 +08001148 index = spte - sp->spt;
1149 if (__test_and_set_bit(index, sp->unsync_child_bitmap))
1150 return;
1151 if (sp->unsync_children++)
1152 return;
1153 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001154}
1155
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001156static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001157 struct kvm_mmu_page *sp)
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001158{
1159 return 1;
1160}
1161
Marcelo Tosattia7052892008-09-23 13:18:35 -03001162static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1163{
1164}
1165
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08001166static void nonpaging_update_pte(struct kvm_vcpu *vcpu,
1167 struct kvm_mmu_page *sp, u64 *spte,
Xiao Guangrong7c562522011-03-28 10:29:27 +08001168 const void *pte)
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08001169{
1170 WARN_ON(1);
1171}
1172
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001173#define KVM_PAGE_ARRAY_NR 16
1174
1175struct kvm_mmu_pages {
1176 struct mmu_page_and_offset {
1177 struct kvm_mmu_page *sp;
1178 unsigned int idx;
1179 } page[KVM_PAGE_ARRAY_NR];
1180 unsigned int nr;
1181};
1182
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001183#define for_each_unsync_children(bitmap, idx) \
1184 for (idx = find_first_bit(bitmap, 512); \
1185 idx < 512; \
1186 idx = find_next_bit(bitmap, 512, idx+1))
1187
Hannes Edercded19f2009-02-21 02:19:13 +01001188static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1189 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001190{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001191 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001192
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001193 if (sp->unsync)
1194 for (i=0; i < pvec->nr; i++)
1195 if (pvec->page[i].sp == sp)
1196 return 0;
1197
1198 pvec->page[pvec->nr].sp = sp;
1199 pvec->page[pvec->nr].idx = idx;
1200 pvec->nr++;
1201 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1202}
1203
1204static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1205 struct kvm_mmu_pages *pvec)
1206{
1207 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001208
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001209 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001210 struct kvm_mmu_page *child;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001211 u64 ent = sp->spt[i];
1212
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001213 if (!is_shadow_present_pte(ent) || is_large_pte(ent))
1214 goto clear_child_bitmap;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001215
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001216 child = page_header(ent & PT64_BASE_ADDR_MASK);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001217
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001218 if (child->unsync_children) {
1219 if (mmu_pages_add(pvec, child, i))
1220 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001221
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001222 ret = __mmu_unsync_walk(child, pvec);
1223 if (!ret)
1224 goto clear_child_bitmap;
1225 else if (ret > 0)
1226 nr_unsync_leaf += ret;
1227 else
1228 return ret;
1229 } else if (child->unsync) {
1230 nr_unsync_leaf++;
1231 if (mmu_pages_add(pvec, child, i))
1232 return -ENOSPC;
1233 } else
1234 goto clear_child_bitmap;
1235
1236 continue;
1237
1238clear_child_bitmap:
1239 __clear_bit(i, sp->unsync_child_bitmap);
1240 sp->unsync_children--;
1241 WARN_ON((int)sp->unsync_children < 0);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001242 }
1243
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001244
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001245 return nr_unsync_leaf;
1246}
1247
1248static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1249 struct kvm_mmu_pages *pvec)
1250{
1251 if (!sp->unsync_children)
1252 return 0;
1253
1254 mmu_pages_add(pvec, sp, 0);
1255 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001256}
1257
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001258static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1259{
1260 WARN_ON(!sp->unsync);
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001261 trace_kvm_mmu_sync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001262 sp->unsync = 0;
1263 --kvm->stat.mmu_unsync;
1264}
1265
Xiao Guangrong77758342010-06-04 21:53:54 +08001266static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1267 struct list_head *invalid_list);
1268static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1269 struct list_head *invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001270
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001271#define for_each_gfn_sp(kvm, sp, gfn, pos) \
1272 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001273 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1274 if ((sp)->gfn != (gfn)) {} else
1275
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001276#define for_each_gfn_indirect_valid_sp(kvm, sp, gfn, pos) \
1277 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001278 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1279 if ((sp)->gfn != (gfn) || (sp)->role.direct || \
1280 (sp)->role.invalid) {} else
1281
Xiao Guangrongf918b442010-06-11 21:30:36 +08001282/* @sp->gfn should be write-protected at the call site */
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001283static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001284 struct list_head *invalid_list, bool clear_unsync)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001285{
Avi Kivity5b7e0102010-04-14 19:20:03 +03001286 if (sp->role.cr4_pae != !!is_pae(vcpu)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001287 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001288 return 1;
1289 }
1290
Xiao Guangrongf918b442010-06-11 21:30:36 +08001291 if (clear_unsync)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001292 kvm_unlink_unsync_page(vcpu->kvm, sp);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001293
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001294 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001295 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001296 return 1;
1297 }
1298
1299 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001300 return 0;
1301}
1302
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001303static int kvm_sync_page_transient(struct kvm_vcpu *vcpu,
1304 struct kvm_mmu_page *sp)
1305{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001306 LIST_HEAD(invalid_list);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001307 int ret;
1308
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001309 ret = __kvm_sync_page(vcpu, sp, &invalid_list, false);
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001310 if (ret)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001311 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
1312
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001313 return ret;
1314}
1315
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001316static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1317 struct list_head *invalid_list)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001318{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001319 return __kvm_sync_page(vcpu, sp, invalid_list, true);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001320}
1321
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001322/* @gfn should be write-protected at the call site */
1323static void kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1324{
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001325 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001326 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001327 LIST_HEAD(invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001328 bool flush = false;
1329
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001330 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001331 if (!s->unsync)
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001332 continue;
1333
1334 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001335 kvm_unlink_unsync_page(vcpu->kvm, s);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001336 if ((s->role.cr4_pae != !!is_pae(vcpu)) ||
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001337 (vcpu->arch.mmu.sync_page(vcpu, s))) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001338 kvm_mmu_prepare_zap_page(vcpu->kvm, s, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001339 continue;
1340 }
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001341 flush = true;
1342 }
1343
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001344 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001345 if (flush)
1346 kvm_mmu_flush_tlb(vcpu);
1347}
1348
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001349struct mmu_page_path {
1350 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1351 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001352};
1353
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001354#define for_each_sp(pvec, sp, parents, i) \
1355 for (i = mmu_pages_next(&pvec, &parents, -1), \
1356 sp = pvec.page[i].sp; \
1357 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1358 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001359
Hannes Edercded19f2009-02-21 02:19:13 +01001360static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1361 struct mmu_page_path *parents,
1362 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001363{
1364 int n;
1365
1366 for (n = i+1; n < pvec->nr; n++) {
1367 struct kvm_mmu_page *sp = pvec->page[n].sp;
1368
1369 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1370 parents->idx[0] = pvec->page[n].idx;
1371 return n;
1372 }
1373
1374 parents->parent[sp->role.level-2] = sp;
1375 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1376 }
1377
1378 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001379}
1380
Hannes Edercded19f2009-02-21 02:19:13 +01001381static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001382{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001383 struct kvm_mmu_page *sp;
1384 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001385
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001386 do {
1387 unsigned int idx = parents->idx[level];
1388
1389 sp = parents->parent[level];
1390 if (!sp)
1391 return;
1392
1393 --sp->unsync_children;
1394 WARN_ON((int)sp->unsync_children < 0);
1395 __clear_bit(idx, sp->unsync_child_bitmap);
1396 level++;
1397 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1398}
1399
1400static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1401 struct mmu_page_path *parents,
1402 struct kvm_mmu_pages *pvec)
1403{
1404 parents->parent[parent->role.level-1] = NULL;
1405 pvec->nr = 0;
1406}
1407
1408static void mmu_sync_children(struct kvm_vcpu *vcpu,
1409 struct kvm_mmu_page *parent)
1410{
1411 int i;
1412 struct kvm_mmu_page *sp;
1413 struct mmu_page_path parents;
1414 struct kvm_mmu_pages pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001415 LIST_HEAD(invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001416
1417 kvm_mmu_pages_init(parent, &parents, &pages);
1418 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001419 int protected = 0;
1420
1421 for_each_sp(pages, sp, parents, i)
1422 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1423
1424 if (protected)
1425 kvm_flush_remote_tlbs(vcpu->kvm);
1426
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001427 for_each_sp(pages, sp, parents, i) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001428 kvm_sync_page(vcpu, sp, &invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001429 mmu_pages_clear_parents(&parents);
1430 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001431 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001432 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001433 kvm_mmu_pages_init(parent, &parents, &pages);
1434 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001435}
1436
Xiao Guangrongc3707952011-07-12 03:28:04 +08001437static void init_shadow_page_table(struct kvm_mmu_page *sp)
1438{
1439 int i;
1440
1441 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1442 sp->spt[i] = 0ull;
1443}
1444
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001445static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1446 gfn_t gfn,
1447 gva_t gaddr,
1448 unsigned level,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001449 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001450 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001451 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001452{
1453 union kvm_mmu_page_role role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001454 unsigned quadrant;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001455 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001456 struct hlist_node *node;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001457 bool need_sync = false;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001458
Avi Kivitya770f6f2008-12-21 19:20:09 +02001459 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001460 role.level = level;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001461 role.direct = direct;
Avi Kivity84b0c8c2010-03-14 10:16:40 +02001462 if (role.direct)
Avi Kivity5b7e0102010-04-14 19:20:03 +03001463 role.cr4_pae = 0;
Avi Kivity41074d02007-12-09 17:00:02 +02001464 role.access = access;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02001465 if (!vcpu->arch.mmu.direct_map
1466 && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001467 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1468 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1469 role.quadrant = quadrant;
1470 }
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001471 for_each_gfn_sp(vcpu->kvm, sp, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001472 if (!need_sync && sp->unsync)
1473 need_sync = true;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001474
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001475 if (sp->role.word != role.word)
1476 continue;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001477
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001478 if (sp->unsync && kvm_sync_page_transient(vcpu, sp))
1479 break;
Xiao Guangronge02aa902010-05-15 18:52:34 +08001480
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001481 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1482 if (sp->unsync_children) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001483 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001484 kvm_mmu_mark_parents_unsync(sp);
1485 } else if (sp->unsync)
1486 kvm_mmu_mark_parents_unsync(sp);
Xiao Guangronge02aa902010-05-15 18:52:34 +08001487
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001488 trace_kvm_mmu_get_page(sp, false);
1489 return sp;
1490 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001491 ++vcpu->kvm->stat.mmu_cache_miss;
Lai Jiangshan2032a932010-05-26 16:49:59 +08001492 sp = kvm_mmu_alloc_page(vcpu, parent_pte, direct);
Avi Kivity4db35312007-11-21 15:28:32 +02001493 if (!sp)
1494 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001495 sp->gfn = gfn;
1496 sp->role = role;
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001497 hlist_add_head(&sp->hash_link,
1498 &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001499 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001500 if (rmap_write_protect(vcpu->kvm, gfn))
1501 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001502 if (level > PT_PAGE_TABLE_LEVEL && need_sync)
1503 kvm_sync_pages(vcpu, gfn);
1504
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001505 account_shadowed(vcpu->kvm, gfn);
1506 }
Xiao Guangrongc3707952011-07-12 03:28:04 +08001507 init_shadow_page_table(sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001508 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001509 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001510}
1511
Avi Kivity2d111232008-12-25 14:39:47 +02001512static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1513 struct kvm_vcpu *vcpu, u64 addr)
1514{
1515 iterator->addr = addr;
1516 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1517 iterator->level = vcpu->arch.mmu.shadow_root_level;
Joerg Roedel81407ca2010-09-10 17:31:00 +02001518
1519 if (iterator->level == PT64_ROOT_LEVEL &&
1520 vcpu->arch.mmu.root_level < PT64_ROOT_LEVEL &&
1521 !vcpu->arch.mmu.direct_map)
1522 --iterator->level;
1523
Avi Kivity2d111232008-12-25 14:39:47 +02001524 if (iterator->level == PT32E_ROOT_LEVEL) {
1525 iterator->shadow_addr
1526 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1527 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1528 --iterator->level;
1529 if (!iterator->shadow_addr)
1530 iterator->level = 0;
1531 }
1532}
1533
1534static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1535{
1536 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1537 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001538
Avi Kivity2d111232008-12-25 14:39:47 +02001539 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1540 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1541 return true;
1542}
1543
1544static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1545{
Xiao Guangrong052331b2011-07-12 03:21:17 +08001546 if (is_last_spte(*iterator->sptep, iterator->level)) {
1547 iterator->level = 0;
1548 return;
1549 }
1550
Avi Kivity2d111232008-12-25 14:39:47 +02001551 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1552 --iterator->level;
1553}
1554
Avi Kivity32ef26a2010-07-13 14:27:04 +03001555static void link_shadow_page(u64 *sptep, struct kvm_mmu_page *sp)
1556{
1557 u64 spte;
1558
1559 spte = __pa(sp->spt)
1560 | PT_PRESENT_MASK | PT_ACCESSED_MASK
1561 | PT_WRITABLE_MASK | PT_USER_MASK;
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08001562 mmu_spte_set(sptep, spte);
Avi Kivity32ef26a2010-07-13 14:27:04 +03001563}
1564
Avi Kivitya3aa51c2010-07-13 14:27:06 +03001565static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep)
1566{
1567 if (is_large_pte(*sptep)) {
Xiao Guangrongc3707952011-07-12 03:28:04 +08001568 drop_spte(vcpu->kvm, sptep);
Avi Kivitya3aa51c2010-07-13 14:27:06 +03001569 kvm_flush_remote_tlbs(vcpu->kvm);
1570 }
1571}
1572
Avi Kivitya357bd22010-07-13 14:27:07 +03001573static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1574 unsigned direct_access)
1575{
1576 if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
1577 struct kvm_mmu_page *child;
1578
1579 /*
1580 * For the direct sp, if the guest pte's dirty bit
1581 * changed form clean to dirty, it will corrupt the
1582 * sp's access: allow writable in the read-only sp,
1583 * so we should update the spte at this point to get
1584 * a new sp with the correct access.
1585 */
1586 child = page_header(*sptep & PT64_BASE_ADDR_MASK);
1587 if (child->role.access == direct_access)
1588 return;
1589
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001590 drop_parent_pte(child, sptep);
Avi Kivitya357bd22010-07-13 14:27:07 +03001591 kvm_flush_remote_tlbs(vcpu->kvm);
1592 }
1593}
1594
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08001595static void mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,
1596 u64 *spte)
1597{
1598 u64 pte;
1599 struct kvm_mmu_page *child;
1600
1601 pte = *spte;
1602 if (is_shadow_present_pte(pte)) {
1603 if (is_last_spte(pte, sp->role.level))
Xiao Guangrongc3707952011-07-12 03:28:04 +08001604 drop_spte(kvm, spte);
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08001605 else {
1606 child = page_header(pte & PT64_BASE_ADDR_MASK);
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001607 drop_parent_pte(child, spte);
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08001608 }
1609 }
Xiao Guangrongc3707952011-07-12 03:28:04 +08001610
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08001611 if (is_large_pte(pte))
1612 --kvm->stat.lpages;
1613}
1614
Avi Kivity90cb0522007-07-17 13:04:56 +03001615static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001616 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001617{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001618 unsigned i;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001619
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08001620 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1621 mmu_page_zap_pte(kvm, sp, sp->spt + i);
Avi Kivitya4360362007-01-05 16:36:45 -08001622}
1623
Avi Kivity4db35312007-11-21 15:28:32 +02001624static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001625{
Avi Kivity4db35312007-11-21 15:28:32 +02001626 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001627}
1628
Avi Kivity12b7d282007-09-23 14:10:49 +02001629static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1630{
1631 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001632 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001633
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001634 kvm_for_each_vcpu(i, vcpu, kvm)
1635 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001636}
1637
Avi Kivity31aa2b42008-07-11 17:59:46 +03001638static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001639{
1640 u64 *parent_pte;
1641
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001642 while ((parent_pte = pte_list_next(&sp->parent_ptes, NULL)))
1643 drop_parent_pte(sp, parent_pte);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001644}
1645
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001646static int mmu_zap_unsync_children(struct kvm *kvm,
Xiao Guangrong77758342010-06-04 21:53:54 +08001647 struct kvm_mmu_page *parent,
1648 struct list_head *invalid_list)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001649{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001650 int i, zapped = 0;
1651 struct mmu_page_path parents;
1652 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001653
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001654 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001655 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001656
1657 kvm_mmu_pages_init(parent, &parents, &pages);
1658 while (mmu_unsync_walk(parent, &pages)) {
1659 struct kvm_mmu_page *sp;
1660
1661 for_each_sp(pages, sp, parents, i) {
Xiao Guangrong77758342010-06-04 21:53:54 +08001662 kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001663 mmu_pages_clear_parents(&parents);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001664 zapped++;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001665 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001666 kvm_mmu_pages_init(parent, &parents, &pages);
1667 }
1668
1669 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001670}
1671
Xiao Guangrong77758342010-06-04 21:53:54 +08001672static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1673 struct list_head *invalid_list)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001674{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001675 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001676
Xiao Guangrong77758342010-06-04 21:53:54 +08001677 trace_kvm_mmu_prepare_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001678 ++kvm->stat.mmu_shadow_zapped;
Xiao Guangrong77758342010-06-04 21:53:54 +08001679 ret = mmu_zap_unsync_children(kvm, sp, invalid_list);
Avi Kivity4db35312007-11-21 15:28:32 +02001680 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001681 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001682 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001683 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001684 if (sp->unsync)
1685 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001686 if (!sp->root_count) {
Gui Jianfeng54a4f022010-05-05 09:03:49 +08001687 /* Count self */
1688 ret++;
Xiao Guangrong77758342010-06-04 21:53:54 +08001689 list_move(&sp->link, invalid_list);
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08001690 kvm_mod_used_mmu_pages(kvm, -1);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001691 } else {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001692 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001693 kvm_reload_remote_mmus(kvm);
1694 }
Xiao Guangrong77758342010-06-04 21:53:54 +08001695
1696 sp->role.invalid = 1;
Avi Kivity12b7d282007-09-23 14:10:49 +02001697 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001698 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001699}
1700
Xiao Guangrong77758342010-06-04 21:53:54 +08001701static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1702 struct list_head *invalid_list)
1703{
1704 struct kvm_mmu_page *sp;
1705
1706 if (list_empty(invalid_list))
1707 return;
1708
1709 kvm_flush_remote_tlbs(kvm);
1710
1711 do {
1712 sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
1713 WARN_ON(!sp->role.invalid || sp->root_count);
Xiao Guangrongbd4c86e2011-07-12 03:27:14 +08001714 kvm_mmu_isolate_page(sp);
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08001715 kvm_mmu_free_page(sp);
Xiao Guangrong77758342010-06-04 21:53:54 +08001716 } while (!list_empty(invalid_list));
1717
1718}
1719
Izik Eidus82ce2c92007-10-02 18:52:55 +02001720/*
1721 * Changing the number of mmu pages allocated to the vm
Dave Hansen49d5ca22010-08-19 18:11:28 -07001722 * Note: if goal_nr_mmu_pages is too small, you will get dead lock
Izik Eidus82ce2c92007-10-02 18:52:55 +02001723 */
Dave Hansen49d5ca22010-08-19 18:11:28 -07001724void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages)
Izik Eidus82ce2c92007-10-02 18:52:55 +02001725{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001726 LIST_HEAD(invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001727 /*
1728 * If we set the number of mmu pages to be smaller be than the
1729 * number of actived pages , we must to free some mmu pages before we
1730 * change the value
1731 */
1732
Dave Hansen49d5ca22010-08-19 18:11:28 -07001733 if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) {
1734 while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages &&
Xiao Guangrong77662e02010-04-16 16:34:42 +08001735 !list_empty(&kvm->arch.active_mmu_pages)) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001736 struct kvm_mmu_page *page;
1737
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001738 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001739 struct kvm_mmu_page, link);
Xiaotian Feng80b63fa2010-08-24 10:31:07 +08001740 kvm_mmu_prepare_zap_page(kvm, page, &invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001741 }
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08001742 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Dave Hansen49d5ca22010-08-19 18:11:28 -07001743 goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001744 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02001745
Dave Hansen49d5ca22010-08-19 18:11:28 -07001746 kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001747}
1748
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001749static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001750{
Avi Kivity4db35312007-11-21 15:28:32 +02001751 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001752 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001753 LIST_HEAD(invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001754 int r;
1755
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08001756 pgprintk("%s: looking for gfn %llx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001757 r = 0;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001758
1759 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08001760 pgprintk("%s: gfn %llx role %x\n", __func__, gfn,
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001761 sp->role.word);
1762 r = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001763 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001764 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001765 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001766 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001767}
1768
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001769static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001770{
Avi Kivity4db35312007-11-21 15:28:32 +02001771 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001772 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001773 LIST_HEAD(invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001774
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001775 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08001776 pgprintk("%s: zap %llx %x\n",
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001777 __func__, gfn, sp->role.word);
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001778 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001779 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001780 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001781}
1782
Avi Kivity38c335f2007-11-21 14:20:22 +02001783static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001784{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001785 int slot = memslot_id(kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001786 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001787
Sheng Yang291f26b2008-10-16 17:30:57 +08001788 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001789}
1790
Sheng Yang74be52e2008-10-09 16:01:56 +08001791/*
1792 * The function is based on mtrr_type_lookup() in
1793 * arch/x86/kernel/cpu/mtrr/generic.c
1794 */
1795static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1796 u64 start, u64 end)
1797{
1798 int i;
1799 u64 base, mask;
1800 u8 prev_match, curr_match;
1801 int num_var_ranges = KVM_NR_VAR_MTRR;
1802
1803 if (!mtrr_state->enabled)
1804 return 0xFF;
1805
1806 /* Make end inclusive end, instead of exclusive */
1807 end--;
1808
1809 /* Look in fixed ranges. Just return the type as per start */
1810 if (mtrr_state->have_fixed && (start < 0x100000)) {
1811 int idx;
1812
1813 if (start < 0x80000) {
1814 idx = 0;
1815 idx += (start >> 16);
1816 return mtrr_state->fixed_ranges[idx];
1817 } else if (start < 0xC0000) {
1818 idx = 1 * 8;
1819 idx += ((start - 0x80000) >> 14);
1820 return mtrr_state->fixed_ranges[idx];
1821 } else if (start < 0x1000000) {
1822 idx = 3 * 8;
1823 idx += ((start - 0xC0000) >> 12);
1824 return mtrr_state->fixed_ranges[idx];
1825 }
1826 }
1827
1828 /*
1829 * Look in variable ranges
1830 * Look of multiple ranges matching this address and pick type
1831 * as per MTRR precedence
1832 */
1833 if (!(mtrr_state->enabled & 2))
1834 return mtrr_state->def_type;
1835
1836 prev_match = 0xFF;
1837 for (i = 0; i < num_var_ranges; ++i) {
1838 unsigned short start_state, end_state;
1839
1840 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1841 continue;
1842
1843 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1844 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1845 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1846 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1847
1848 start_state = ((start & mask) == (base & mask));
1849 end_state = ((end & mask) == (base & mask));
1850 if (start_state != end_state)
1851 return 0xFE;
1852
1853 if ((start & mask) != (base & mask))
1854 continue;
1855
1856 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1857 if (prev_match == 0xFF) {
1858 prev_match = curr_match;
1859 continue;
1860 }
1861
1862 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1863 curr_match == MTRR_TYPE_UNCACHABLE)
1864 return MTRR_TYPE_UNCACHABLE;
1865
1866 if ((prev_match == MTRR_TYPE_WRBACK &&
1867 curr_match == MTRR_TYPE_WRTHROUGH) ||
1868 (prev_match == MTRR_TYPE_WRTHROUGH &&
1869 curr_match == MTRR_TYPE_WRBACK)) {
1870 prev_match = MTRR_TYPE_WRTHROUGH;
1871 curr_match = MTRR_TYPE_WRTHROUGH;
1872 }
1873
1874 if (prev_match != curr_match)
1875 return MTRR_TYPE_UNCACHABLE;
1876 }
1877
1878 if (prev_match != 0xFF)
1879 return prev_match;
1880
1881 return mtrr_state->def_type;
1882}
1883
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001884u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001885{
1886 u8 mtrr;
1887
1888 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1889 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1890 if (mtrr == 0xfe || mtrr == 0xff)
1891 mtrr = MTRR_TYPE_WRBACK;
1892 return mtrr;
1893}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001894EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001895
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001896static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001897{
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001898 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001899 ++vcpu->kvm->stat.mmu_unsync;
1900 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001901
Xiao Guangrong6b184932010-04-16 21:29:17 +08001902 kvm_mmu_mark_parents_unsync(sp);
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001903}
1904
1905static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1906{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001907 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001908 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001909
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001910 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001911 if (s->unsync)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001912 continue;
1913 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1914 __kvm_unsync_page(vcpu, s);
1915 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001916}
1917
1918static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1919 bool can_unsync)
1920{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001921 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001922 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001923 bool need_unsync = false;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001924
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001925 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001926 if (!can_unsync)
1927 return 1;
1928
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001929 if (s->role.level != PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001930 return 1;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001931
1932 if (!need_unsync && !s->unsync) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001933 if (!oos_shadow)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001934 return 1;
1935 need_unsync = true;
1936 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001937 }
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001938 if (need_unsync)
1939 kvm_unsync_pages(vcpu, gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001940 return 0;
1941}
1942
Avi Kivityd555c332009-06-10 14:24:23 +03001943static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001944 unsigned pte_access, int user_fault,
Xiao Guangrong640d9b02011-07-12 03:24:39 +08001945 int write_fault, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001946 gfn_t gfn, pfn_t pfn, bool speculative,
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08001947 bool can_unsync, bool host_writable)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001948{
Xiao Guangrongb330aa02010-11-19 17:02:35 +08001949 u64 spte, entry = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001950 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001951
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001952 /*
1953 * We don't set the accessed bit, since we sometimes want to see
1954 * whether the guest actually used the pte (in order to detect
1955 * demand paging).
1956 */
Marcelo Tosatti982c2562010-10-22 14:18:16 -02001957 spte = PT_PRESENT_MASK;
Avi Kivity947da532008-03-18 11:05:52 +02001958 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001959 spte |= shadow_accessed_mask;
Xiao Guangrong640d9b02011-07-12 03:24:39 +08001960
Sheng Yang7b523452008-04-25 21:13:50 +08001961 if (pte_access & ACC_EXEC_MASK)
1962 spte |= shadow_x_mask;
1963 else
1964 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001965 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001966 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001967 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001968 spte |= PT_PAGE_SIZE_MASK;
Avi Kivityb0bc3ee2010-09-13 16:45:28 +02001969 if (tdp_enabled)
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001970 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1971 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001972
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08001973 if (host_writable)
Izik Eidus14032832009-09-23 21:47:17 +03001974 spte |= SPTE_HOST_WRITEABLE;
Xiao Guangrongf8e453b2010-12-23 16:09:29 +08001975 else
1976 pte_access &= ~ACC_WRITE_MASK;
Izik Eidus14032832009-09-23 21:47:17 +03001977
Anthony Liguori35149e22008-04-02 14:46:56 -05001978 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001979
1980 if ((pte_access & ACC_WRITE_MASK)
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02001981 || (!vcpu->arch.mmu.direct_map && write_fault
1982 && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001983
Joerg Roedel852e3c12009-07-27 16:30:44 +02001984 if (level > PT_PAGE_TABLE_LEVEL &&
1985 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001986 ret = 1;
Xiao Guangrongc3707952011-07-12 03:28:04 +08001987 drop_spte(vcpu->kvm, sptep);
Avi Kivitybe38d272010-06-06 14:31:27 +03001988 goto done;
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001989 }
1990
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001991 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001992
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02001993 if (!vcpu->arch.mmu.direct_map
Avi Kivity411c5882011-06-06 16:11:54 +03001994 && !(pte_access & ACC_WRITE_MASK)) {
Avi Kivity69325a12010-05-27 14:35:58 +03001995 spte &= ~PT_USER_MASK;
Avi Kivity411c5882011-06-06 16:11:54 +03001996 /*
1997 * If we converted a user page to a kernel page,
1998 * so that the kernel can write to it when cr0.wp=0,
1999 * then we should prevent the kernel from executing it
2000 * if SMEP is enabled.
2001 */
2002 if (kvm_read_cr4_bits(vcpu, X86_CR4_SMEP))
2003 spte |= PT64_NX_MASK;
2004 }
Avi Kivity69325a12010-05-27 14:35:58 +03002005
Marcelo Tosattiecc55892008-11-25 15:58:07 +01002006 /*
2007 * Optimization: for pte sync, if spte was writable the hash
2008 * lookup is unnecessary (and expensive). Write protection
2009 * is responsibility of mmu_get_page / kvm_sync_page.
2010 * Same reasoning can be applied to dirty page accounting.
2011 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002012 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01002013 goto set_pte;
2014
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002015 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002016 pgprintk("%s: found shadow page for %llx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002017 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002018 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002019 pte_access &= ~ACC_WRITE_MASK;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002020 if (is_writable_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002021 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002022 }
2023 }
2024
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002025 if (pte_access & ACC_WRITE_MASK)
2026 mark_page_dirty(vcpu->kvm, gfn);
2027
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002028set_pte:
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08002029 mmu_spte_update(sptep, spte);
Xiao Guangrongb330aa02010-11-19 17:02:35 +08002030 /*
2031 * If we overwrite a writable spte with a read-only one we
2032 * should flush remote TLBs. Otherwise rmap_write_protect
2033 * will find a read-only spte, even though the writable spte
2034 * might be cached on a CPU's TLB.
2035 */
2036 if (is_writable_pte(entry) && !is_writable_pte(*sptep))
2037 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivitybe38d272010-06-06 14:31:27 +03002038done:
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002039 return ret;
2040}
2041
Avi Kivityd555c332009-06-10 14:24:23 +03002042static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002043 unsigned pt_access, unsigned pte_access,
Xiao Guangrong640d9b02011-07-12 03:24:39 +08002044 int user_fault, int write_fault,
Xiao Guangrongb90a0e62011-07-12 03:25:56 +08002045 int *emulate, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03002046 pfn_t pfn, bool speculative,
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08002047 bool host_writable)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002048{
2049 int was_rmapped = 0;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002050 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002051
2052 pgprintk("%s: spte %llx access %x write_fault %d"
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002053 " user_fault %d gfn %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002054 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002055 write_fault, user_fault, gfn);
2056
Avi Kivityd555c332009-06-10 14:24:23 +03002057 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002058 /*
2059 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
2060 * the parent of the now unreachable PTE.
2061 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02002062 if (level > PT_PAGE_TABLE_LEVEL &&
2063 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002064 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03002065 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002066
2067 child = page_header(pte & PT64_BASE_ADDR_MASK);
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08002068 drop_parent_pte(child, sptep);
Marcelo Tosatti3be22642010-05-28 09:44:59 -03002069 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityd555c332009-06-10 14:24:23 +03002070 } else if (pfn != spte_to_pfn(*sptep)) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002071 pgprintk("hfn old %llx new %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002072 spte_to_pfn(*sptep), pfn);
Xiao Guangrongc3707952011-07-12 03:28:04 +08002073 drop_spte(vcpu->kvm, sptep);
Xiao Guangrong91546352010-06-30 16:04:06 +08002074 kvm_flush_remote_tlbs(vcpu->kvm);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01002075 } else
2076 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002077 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02002078
Avi Kivityd555c332009-06-10 14:24:23 +03002079 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Xiao Guangrong640d9b02011-07-12 03:24:39 +08002080 level, gfn, pfn, speculative, true,
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08002081 host_writable)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002082 if (write_fault)
Xiao Guangrongb90a0e62011-07-12 03:25:56 +08002083 *emulate = 1;
Xiao Guangrong5304efd2010-06-08 20:05:57 +08002084 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03002085 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002086
Avi Kivityd555c332009-06-10 14:24:23 +03002087 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002088 pgprintk("instantiating %s PTE (%s) at %llx (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002089 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02002090 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
2091 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03002092 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002093 ++vcpu->kvm->stat.lpages;
2094
Xiao Guangrongffb61bb2011-07-12 03:22:01 +08002095 if (is_shadow_present_pte(*sptep)) {
2096 page_header_update_slot(vcpu->kvm, sptep, gfn);
2097 if (!was_rmapped) {
2098 rmap_count = rmap_add(vcpu, sptep, gfn);
2099 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
2100 rmap_recycle(vcpu, sptep, gfn);
2101 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002102 }
Xiao Guangrong9ed55202010-07-16 11:25:17 +08002103 kvm_release_pfn_clean(pfn);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002104 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03002105 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002106 vcpu->arch.last_pte_gfn = gfn;
2107 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002108}
2109
Avi Kivity6aa8b732006-12-10 02:21:36 -08002110static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
2111{
2112}
2113
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002114static pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn,
2115 bool no_dirty_log)
2116{
2117 struct kvm_memory_slot *slot;
2118 unsigned long hva;
2119
Xiao Guangrong5d163b12011-03-09 15:43:00 +08002120 slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, no_dirty_log);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002121 if (!slot) {
Xiao Guangrongfce92dc2011-07-12 03:28:54 +08002122 get_page(fault_page);
2123 return page_to_pfn(fault_page);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002124 }
2125
2126 hva = gfn_to_hva_memslot(slot, gfn);
2127
2128 return hva_to_pfn_atomic(vcpu->kvm, hva);
2129}
2130
2131static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,
2132 struct kvm_mmu_page *sp,
2133 u64 *start, u64 *end)
2134{
2135 struct page *pages[PTE_PREFETCH_NUM];
2136 unsigned access = sp->role.access;
2137 int i, ret;
2138 gfn_t gfn;
2139
2140 gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt);
Xiao Guangrong5d163b12011-03-09 15:43:00 +08002141 if (!gfn_to_memslot_dirty_bitmap(vcpu, gfn, access & ACC_WRITE_MASK))
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002142 return -1;
2143
2144 ret = gfn_to_page_many_atomic(vcpu->kvm, gfn, pages, end - start);
2145 if (ret <= 0)
2146 return -1;
2147
2148 for (i = 0; i < ret; i++, gfn++, start++)
2149 mmu_set_spte(vcpu, start, ACC_ALL,
Xiao Guangrong640d9b02011-07-12 03:24:39 +08002150 access, 0, 0, NULL,
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002151 sp->role.level, gfn,
2152 page_to_pfn(pages[i]), true, true);
2153
2154 return 0;
2155}
2156
2157static void __direct_pte_prefetch(struct kvm_vcpu *vcpu,
2158 struct kvm_mmu_page *sp, u64 *sptep)
2159{
2160 u64 *spte, *start = NULL;
2161 int i;
2162
2163 WARN_ON(!sp->role.direct);
2164
2165 i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
2166 spte = sp->spt + i;
2167
2168 for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
Xiao Guangrongc3707952011-07-12 03:28:04 +08002169 if (is_shadow_present_pte(*spte) || spte == sptep) {
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002170 if (!start)
2171 continue;
2172 if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0)
2173 break;
2174 start = NULL;
2175 } else if (!start)
2176 start = spte;
2177 }
2178}
2179
2180static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)
2181{
2182 struct kvm_mmu_page *sp;
2183
2184 /*
2185 * Since it's no accessed bit on EPT, it's no way to
2186 * distinguish between actually accessed translations
2187 * and prefetched, so disable pte prefetch if EPT is
2188 * enabled.
2189 */
2190 if (!shadow_accessed_mask)
2191 return;
2192
2193 sp = page_header(__pa(sptep));
2194 if (sp->role.level > PT_PAGE_TABLE_LEVEL)
2195 return;
2196
2197 __direct_pte_prefetch(vcpu, sp, sptep);
2198}
2199
Joerg Roedel4d9976b2008-02-07 13:47:42 +01002200static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002201 int map_writable, int level, gfn_t gfn, pfn_t pfn,
2202 bool prefault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002203{
Avi Kivity9f652d22008-12-25 14:54:25 +02002204 struct kvm_shadow_walk_iterator iterator;
2205 struct kvm_mmu_page *sp;
Xiao Guangrongb90a0e62011-07-12 03:25:56 +08002206 int emulate = 0;
Avi Kivity9f652d22008-12-25 14:54:25 +02002207 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002208
Avi Kivity9f652d22008-12-25 14:54:25 +02002209 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02002210 if (iterator.level == level) {
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002211 unsigned pte_access = ACC_ALL;
2212
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002213 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, pte_access,
Xiao Guangrongb90a0e62011-07-12 03:25:56 +08002214 0, write, &emulate,
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002215 level, gfn, pfn, prefault, map_writable);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002216 direct_pte_prefetch(vcpu, iterator.sptep);
Avi Kivity9f652d22008-12-25 14:54:25 +02002217 ++vcpu->stat.pf_fixed;
2218 break;
2219 }
2220
Xiao Guangrongc3707952011-07-12 03:28:04 +08002221 if (!is_shadow_present_pte(*iterator.sptep)) {
Lai Jiangshanc9fa0b32010-05-26 16:48:25 +08002222 u64 base_addr = iterator.addr;
2223
2224 base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
2225 pseudo_gfn = base_addr >> PAGE_SHIFT;
Avi Kivity9f652d22008-12-25 14:54:25 +02002226 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
2227 iterator.level - 1,
2228 1, ACC_ALL, iterator.sptep);
2229 if (!sp) {
2230 pgprintk("nonpaging_map: ENOMEM\n");
2231 kvm_release_pfn_clean(pfn);
2232 return -ENOMEM;
2233 }
2234
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08002235 mmu_spte_set(iterator.sptep,
2236 __pa(sp->spt)
2237 | PT_PRESENT_MASK | PT_WRITABLE_MASK
2238 | shadow_user_mask | shadow_x_mask
2239 | shadow_accessed_mask);
Avi Kivity9f652d22008-12-25 14:54:25 +02002240 }
2241 }
Xiao Guangrongb90a0e62011-07-12 03:25:56 +08002242 return emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002243}
2244
Huang Ying77db5cb2010-10-08 16:24:15 +08002245static void kvm_send_hwpoison_signal(unsigned long address, struct task_struct *tsk)
Huang Yingbf998152010-05-31 14:28:19 +08002246{
Huang Ying77db5cb2010-10-08 16:24:15 +08002247 siginfo_t info;
Huang Yingbf998152010-05-31 14:28:19 +08002248
Huang Ying77db5cb2010-10-08 16:24:15 +08002249 info.si_signo = SIGBUS;
2250 info.si_errno = 0;
2251 info.si_code = BUS_MCEERR_AR;
2252 info.si_addr = (void __user *)address;
2253 info.si_addr_lsb = PAGE_SHIFT;
2254
2255 send_sig_info(SIGBUS, &info, tsk);
Huang Yingbf998152010-05-31 14:28:19 +08002256}
2257
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002258static int kvm_handle_bad_page(struct kvm_vcpu *vcpu, gfn_t gfn, pfn_t pfn)
Huang Yingbf998152010-05-31 14:28:19 +08002259{
2260 kvm_release_pfn_clean(pfn);
2261 if (is_hwpoison_pfn(pfn)) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08002262 kvm_send_hwpoison_signal(gfn_to_hva(vcpu->kvm, gfn), current);
Huang Yingbf998152010-05-31 14:28:19 +08002263 return 0;
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002264 }
Gleb Natapovedba23e2010-07-07 20:16:45 +03002265
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002266 return -EFAULT;
Huang Yingbf998152010-05-31 14:28:19 +08002267}
2268
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002269static void transparent_hugepage_adjust(struct kvm_vcpu *vcpu,
2270 gfn_t *gfnp, pfn_t *pfnp, int *levelp)
2271{
2272 pfn_t pfn = *pfnp;
2273 gfn_t gfn = *gfnp;
2274 int level = *levelp;
2275
2276 /*
2277 * Check if it's a transparent hugepage. If this would be an
2278 * hugetlbfs page, level wouldn't be set to
2279 * PT_PAGE_TABLE_LEVEL and there would be no adjustment done
2280 * here.
2281 */
2282 if (!is_error_pfn(pfn) && !kvm_is_mmio_pfn(pfn) &&
2283 level == PT_PAGE_TABLE_LEVEL &&
2284 PageTransCompound(pfn_to_page(pfn)) &&
2285 !has_wrprotected_page(vcpu->kvm, gfn, PT_DIRECTORY_LEVEL)) {
2286 unsigned long mask;
2287 /*
2288 * mmu_notifier_retry was successful and we hold the
2289 * mmu_lock here, so the pmd can't become splitting
2290 * from under us, and in turn
2291 * __split_huge_page_refcount() can't run from under
2292 * us and we can safely transfer the refcount from
2293 * PG_tail to PG_head as we switch the pfn to tail to
2294 * head.
2295 */
2296 *levelp = level = PT_DIRECTORY_LEVEL;
2297 mask = KVM_PAGES_PER_HPAGE(level) - 1;
2298 VM_BUG_ON((gfn & mask) != (pfn & mask));
2299 if (pfn & mask) {
2300 gfn &= ~mask;
2301 *gfnp = gfn;
2302 kvm_release_pfn_clean(pfn);
2303 pfn &= ~mask;
2304 if (!get_page_unless_zero(pfn_to_page(pfn)))
2305 BUG();
2306 *pfnp = pfn;
2307 }
2308 }
2309}
2310
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002311static bool mmu_invalid_pfn(pfn_t pfn)
2312{
2313 return unlikely(is_invalid_pfn(pfn) || is_noslot_pfn(pfn));
2314}
2315
2316static bool handle_abnormal_pfn(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn,
2317 pfn_t pfn, unsigned access, int *ret_val)
2318{
2319 bool ret = true;
2320
2321 /* The pfn is invalid, report the error! */
2322 if (unlikely(is_invalid_pfn(pfn))) {
2323 *ret_val = kvm_handle_bad_page(vcpu, gfn, pfn);
2324 goto exit;
2325 }
2326
2327 if (unlikely(is_noslot_pfn(pfn))) {
2328 vcpu_cache_mmio_info(vcpu, gva, gfn, access);
2329 *ret_val = 1;
2330 goto exit;
2331 }
2332
2333 ret = false;
2334exit:
2335 return ret;
2336}
2337
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002338static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
Xiao Guangrong060c2ab2010-11-12 14:49:11 +08002339 gva_t gva, pfn_t *pfn, bool write, bool *writable);
2340
2341static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002342 bool prefault)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002343{
2344 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002345 int level;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002346 int force_pt_level;
Anthony Liguori35149e22008-04-02 14:46:56 -05002347 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002348 unsigned long mmu_seq;
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002349 bool map_writable;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002350
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002351 force_pt_level = mapping_level_dirty_bitmap(vcpu, gfn);
2352 if (likely(!force_pt_level)) {
2353 level = mapping_level(vcpu, gfn);
2354 /*
2355 * This path builds a PAE pagetable - so we can map
2356 * 2mb pages at maximum. Therefore check if the level
2357 * is larger than that.
2358 */
2359 if (level > PT_DIRECTORY_LEVEL)
2360 level = PT_DIRECTORY_LEVEL;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002361
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002362 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2363 } else
2364 level = PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002365
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002366 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002367 smp_rmb();
Xiao Guangrong060c2ab2010-11-12 14:49:11 +08002368
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002369 if (try_async_pf(vcpu, prefault, gfn, v, &pfn, write, &map_writable))
Xiao Guangrong060c2ab2010-11-12 14:49:11 +08002370 return 0;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002371
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002372 if (handle_abnormal_pfn(vcpu, v, gfn, pfn, ACC_ALL, &r))
2373 return r;
Avi Kivityd196e342008-01-24 11:44:11 +02002374
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002375 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002376 if (mmu_notifier_retry(vcpu, mmu_seq))
2377 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02002378 kvm_mmu_free_some_pages(vcpu);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002379 if (likely(!force_pt_level))
2380 transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002381 r = __direct_map(vcpu, v, write, map_writable, level, gfn, pfn,
2382 prefault);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002383 spin_unlock(&vcpu->kvm->mmu_lock);
2384
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002385
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002386 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002387
2388out_unlock:
2389 spin_unlock(&vcpu->kvm->mmu_lock);
2390 kvm_release_pfn_clean(pfn);
2391 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002392}
2393
2394
Avi Kivity17ac10a2007-01-05 16:36:40 -08002395static void mmu_free_roots(struct kvm_vcpu *vcpu)
2396{
2397 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002398 struct kvm_mmu_page *sp;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002399 LIST_HEAD(invalid_list);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002400
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002401 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002402 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002403 spin_lock(&vcpu->kvm->mmu_lock);
Joerg Roedel81407ca2010-09-10 17:31:00 +02002404 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL &&
2405 (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL ||
2406 vcpu->arch.mmu.direct_map)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002407 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002408
Avi Kivity4db35312007-11-21 15:28:32 +02002409 sp = page_header(root);
2410 --sp->root_count;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002411 if (!sp->root_count && sp->role.invalid) {
2412 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
2413 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
2414 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002415 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002416 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002417 return;
2418 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002419 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002420 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002421
Avi Kivity417726a2007-04-12 17:35:58 +03002422 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002423 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002424 sp = page_header(root);
2425 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002426 if (!sp->root_count && sp->role.invalid)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002427 kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
2428 &invalid_list);
Avi Kivity417726a2007-04-12 17:35:58 +03002429 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002430 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002431 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002432 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002433 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002434 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002435}
2436
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002437static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2438{
2439 int ret = 0;
2440
2441 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03002442 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002443 ret = 1;
2444 }
2445
2446 return ret;
2447}
2448
Joerg Roedel651dd372010-09-10 17:30:59 +02002449static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu)
2450{
2451 struct kvm_mmu_page *sp;
Avi Kivity7ebaf152010-10-03 18:51:39 +02002452 unsigned i;
Joerg Roedel651dd372010-09-10 17:30:59 +02002453
2454 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2455 spin_lock(&vcpu->kvm->mmu_lock);
2456 kvm_mmu_free_some_pages(vcpu);
2457 sp = kvm_mmu_get_page(vcpu, 0, 0, PT64_ROOT_LEVEL,
2458 1, ACC_ALL, NULL);
2459 ++sp->root_count;
2460 spin_unlock(&vcpu->kvm->mmu_lock);
2461 vcpu->arch.mmu.root_hpa = __pa(sp->spt);
2462 } else if (vcpu->arch.mmu.shadow_root_level == PT32E_ROOT_LEVEL) {
2463 for (i = 0; i < 4; ++i) {
2464 hpa_t root = vcpu->arch.mmu.pae_root[i];
2465
2466 ASSERT(!VALID_PAGE(root));
2467 spin_lock(&vcpu->kvm->mmu_lock);
2468 kvm_mmu_free_some_pages(vcpu);
Avi Kivity649497d2010-12-28 12:09:07 +02002469 sp = kvm_mmu_get_page(vcpu, i << (30 - PAGE_SHIFT),
2470 i << 30,
Joerg Roedel651dd372010-09-10 17:30:59 +02002471 PT32_ROOT_LEVEL, 1, ACC_ALL,
2472 NULL);
2473 root = __pa(sp->spt);
2474 ++sp->root_count;
2475 spin_unlock(&vcpu->kvm->mmu_lock);
2476 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Joerg Roedel651dd372010-09-10 17:30:59 +02002477 }
Xiao Guangrong62927572010-09-27 18:02:12 +08002478 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Joerg Roedel651dd372010-09-10 17:30:59 +02002479 } else
2480 BUG();
2481
2482 return 0;
2483}
2484
2485static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002486{
Avi Kivity4db35312007-11-21 15:28:32 +02002487 struct kvm_mmu_page *sp;
Joerg Roedel81407ca2010-09-10 17:31:00 +02002488 u64 pdptr, pm_mask;
2489 gfn_t root_gfn;
2490 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002491
Joerg Roedel5777ed32010-09-10 17:30:42 +02002492 root_gfn = vcpu->arch.mmu.get_cr3(vcpu) >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002493
Joerg Roedel651dd372010-09-10 17:30:59 +02002494 if (mmu_check_root(vcpu, root_gfn))
2495 return 1;
2496
2497 /*
2498 * Do we shadow a long mode page table? If so we need to
2499 * write-protect the guests page table root.
2500 */
2501 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002502 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002503
2504 ASSERT(!VALID_PAGE(root));
Joerg Roedel651dd372010-09-10 17:30:59 +02002505
Avi Kivity8facbbf2010-05-04 12:58:32 +03002506 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b62010-05-12 21:00:35 -03002507 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel651dd372010-09-10 17:30:59 +02002508 sp = kvm_mmu_get_page(vcpu, root_gfn, 0, PT64_ROOT_LEVEL,
2509 0, ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002510 root = __pa(sp->spt);
2511 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002512 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002513 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002514 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002515 }
Joerg Roedelf87f9282010-09-02 17:29:45 +02002516
Joerg Roedel651dd372010-09-10 17:30:59 +02002517 /*
2518 * We shadow a 32 bit page table. This may be a legacy 2-level
Joerg Roedel81407ca2010-09-10 17:31:00 +02002519 * or a PAE 3-level page table. In either case we need to be aware that
2520 * the shadow page table may be a PAE or a long mode page table.
Joerg Roedel651dd372010-09-10 17:30:59 +02002521 */
Joerg Roedel81407ca2010-09-10 17:31:00 +02002522 pm_mask = PT_PRESENT_MASK;
2523 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL)
2524 pm_mask |= PT_ACCESSED_MASK | PT_WRITABLE_MASK | PT_USER_MASK;
2525
Avi Kivity17ac10a2007-01-05 16:36:40 -08002526 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002527 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002528
2529 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002530 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Joerg Roedeld41d1892010-09-10 17:30:58 +02002531 pdptr = kvm_pdptr_read_mmu(vcpu, &vcpu->arch.mmu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002532 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002533 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002534 continue;
2535 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002536 root_gfn = pdptr >> PAGE_SHIFT;
Joerg Roedelf87f9282010-09-02 17:29:45 +02002537 if (mmu_check_root(vcpu, root_gfn))
2538 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002539 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002540 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b62010-05-12 21:00:35 -03002541 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02002542 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Joerg Roedel651dd372010-09-10 17:30:59 +02002543 PT32_ROOT_LEVEL, 0,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002544 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002545 root = __pa(sp->spt);
2546 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002547 spin_unlock(&vcpu->kvm->mmu_lock);
2548
Joerg Roedel81407ca2010-09-10 17:31:00 +02002549 vcpu->arch.mmu.pae_root[i] = root | pm_mask;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002550 }
Xiao Guangrong62927572010-09-27 18:02:12 +08002551 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Joerg Roedel81407ca2010-09-10 17:31:00 +02002552
2553 /*
2554 * If we shadow a 32 bit page table with a long mode page
2555 * table we enter this path.
2556 */
2557 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2558 if (vcpu->arch.mmu.lm_root == NULL) {
2559 /*
2560 * The additional page necessary for this is only
2561 * allocated on demand.
2562 */
2563
2564 u64 *lm_root;
2565
2566 lm_root = (void*)get_zeroed_page(GFP_KERNEL);
2567 if (lm_root == NULL)
2568 return 1;
2569
2570 lm_root[0] = __pa(vcpu->arch.mmu.pae_root) | pm_mask;
2571
2572 vcpu->arch.mmu.lm_root = lm_root;
2573 }
2574
2575 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.lm_root);
2576 }
2577
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002578 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002579}
2580
Joerg Roedel651dd372010-09-10 17:30:59 +02002581static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
2582{
2583 if (vcpu->arch.mmu.direct_map)
2584 return mmu_alloc_direct_roots(vcpu);
2585 else
2586 return mmu_alloc_shadow_roots(vcpu);
2587}
2588
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002589static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2590{
2591 int i;
2592 struct kvm_mmu_page *sp;
2593
Joerg Roedel81407ca2010-09-10 17:31:00 +02002594 if (vcpu->arch.mmu.direct_map)
2595 return;
2596
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002597 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2598 return;
Xiao Guangrong69030742010-09-27 18:09:29 +08002599
Xiao Guangrongbebb1062011-07-12 03:23:20 +08002600 vcpu_clear_mmio_info(vcpu, ~0ul);
Xiao Guangrong69030742010-09-27 18:09:29 +08002601 trace_kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC);
Joerg Roedel81407ca2010-09-10 17:31:00 +02002602 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002603 hpa_t root = vcpu->arch.mmu.root_hpa;
2604 sp = page_header(root);
2605 mmu_sync_children(vcpu, sp);
Xiao Guangrong5054c0d2010-11-12 14:46:08 +08002606 trace_kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002607 return;
2608 }
2609 for (i = 0; i < 4; ++i) {
2610 hpa_t root = vcpu->arch.mmu.pae_root[i];
2611
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002612 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002613 root &= PT64_BASE_ADDR_MASK;
2614 sp = page_header(root);
2615 mmu_sync_children(vcpu, sp);
2616 }
2617 }
Xiao Guangrong69030742010-09-27 18:09:29 +08002618 trace_kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002619}
2620
2621void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2622{
2623 spin_lock(&vcpu->kvm->mmu_lock);
2624 mmu_sync_roots(vcpu);
2625 spin_unlock(&vcpu->kvm->mmu_lock);
2626}
2627
Gleb Natapov1871c602010-02-10 14:21:32 +02002628static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
Avi Kivityab9ae312010-11-22 17:53:26 +02002629 u32 access, struct x86_exception *exception)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002630{
Avi Kivityab9ae312010-11-22 17:53:26 +02002631 if (exception)
2632 exception->error_code = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002633 return vaddr;
2634}
2635
Joerg Roedel6539e732010-09-10 17:30:50 +02002636static gpa_t nonpaging_gva_to_gpa_nested(struct kvm_vcpu *vcpu, gva_t vaddr,
Avi Kivityab9ae312010-11-22 17:53:26 +02002637 u32 access,
2638 struct x86_exception *exception)
Joerg Roedel6539e732010-09-10 17:30:50 +02002639{
Avi Kivityab9ae312010-11-22 17:53:26 +02002640 if (exception)
2641 exception->error_code = 0;
Joerg Roedel6539e732010-09-10 17:30:50 +02002642 return vcpu->arch.nested_mmu.translate_gpa(vcpu, vaddr, access);
2643}
2644
Avi Kivity6aa8b732006-12-10 02:21:36 -08002645static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002646 u32 error_code, bool prefault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002647{
Avi Kivitye8332402007-12-09 18:43:00 +02002648 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002649 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002650
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002651 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002652 r = mmu_topup_memory_caches(vcpu);
2653 if (r)
2654 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002655
Avi Kivity6aa8b732006-12-10 02:21:36 -08002656 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002657 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002658
Avi Kivitye8332402007-12-09 18:43:00 +02002659 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660
Avi Kivitye8332402007-12-09 18:43:00 +02002661 return nonpaging_map(vcpu, gva & PAGE_MASK,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002662 error_code & PFERR_WRITE_MASK, gfn, prefault);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002663}
2664
Jan Kiszka7e1fbea2010-10-20 15:18:02 +02002665static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn)
Gleb Natapovaf585b92010-10-14 11:22:46 +02002666{
2667 struct kvm_arch_async_pf arch;
Xiao Guangrongfb67e142010-12-07 10:35:25 +08002668
Gleb Natapov7c907052010-10-14 11:22:53 +02002669 arch.token = (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id;
Gleb Natapovaf585b92010-10-14 11:22:46 +02002670 arch.gfn = gfn;
Xiao Guangrongc4806ac2010-11-12 14:49:55 +08002671 arch.direct_map = vcpu->arch.mmu.direct_map;
Xiao Guangrongfb67e142010-12-07 10:35:25 +08002672 arch.cr3 = vcpu->arch.mmu.get_cr3(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002673
2674 return kvm_setup_async_pf(vcpu, gva, gfn, &arch);
2675}
2676
2677static bool can_do_async_pf(struct kvm_vcpu *vcpu)
2678{
2679 if (unlikely(!irqchip_in_kernel(vcpu->kvm) ||
2680 kvm_event_needs_reinjection(vcpu)))
2681 return false;
2682
2683 return kvm_x86_ops->interrupt_allowed(vcpu);
2684}
2685
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002686static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002687 gva_t gva, pfn_t *pfn, bool write, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02002688{
2689 bool async;
2690
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002691 *pfn = gfn_to_pfn_async(vcpu->kvm, gfn, &async, write, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002692
2693 if (!async)
2694 return false; /* *pfn has correct page already */
2695
2696 put_page(pfn_to_page(*pfn));
2697
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002698 if (!prefault && can_do_async_pf(vcpu)) {
Xiao Guangrongc9b263d2010-11-01 16:58:43 +08002699 trace_kvm_try_async_get_page(gva, gfn);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002700 if (kvm_find_async_pf_gfn(vcpu, gfn)) {
2701 trace_kvm_async_pf_doublefault(gva, gfn);
2702 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
2703 return true;
2704 } else if (kvm_arch_setup_async_pf(vcpu, gva, gfn))
2705 return true;
2706 }
2707
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002708 *pfn = gfn_to_pfn_prot(vcpu->kvm, gfn, write, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002709
2710 return false;
2711}
2712
Gleb Natapov56028d02010-10-17 18:13:42 +02002713static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, u32 error_code,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002714 bool prefault)
Joerg Roedelfb72d162008-02-07 13:47:44 +01002715{
Anthony Liguori35149e22008-04-02 14:46:56 -05002716 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002717 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002718 int level;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002719 int force_pt_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002720 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002721 unsigned long mmu_seq;
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002722 int write = error_code & PFERR_WRITE_MASK;
2723 bool map_writable;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002724
2725 ASSERT(vcpu);
2726 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2727
2728 r = mmu_topup_memory_caches(vcpu);
2729 if (r)
2730 return r;
2731
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002732 force_pt_level = mapping_level_dirty_bitmap(vcpu, gfn);
2733 if (likely(!force_pt_level)) {
2734 level = mapping_level(vcpu, gfn);
2735 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2736 } else
2737 level = PT_PAGE_TABLE_LEVEL;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002738
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002739 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002740 smp_rmb();
Gleb Natapovaf585b92010-10-14 11:22:46 +02002741
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002742 if (try_async_pf(vcpu, prefault, gfn, gpa, &pfn, write, &map_writable))
Gleb Natapovaf585b92010-10-14 11:22:46 +02002743 return 0;
2744
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002745 if (handle_abnormal_pfn(vcpu, 0, gfn, pfn, ACC_ALL, &r))
2746 return r;
2747
Joerg Roedelfb72d162008-02-07 13:47:44 +01002748 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002749 if (mmu_notifier_retry(vcpu, mmu_seq))
2750 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002751 kvm_mmu_free_some_pages(vcpu);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002752 if (likely(!force_pt_level))
2753 transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002754 r = __direct_map(vcpu, gpa, write, map_writable,
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002755 level, gfn, pfn, prefault);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002756 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002757
2758 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002759
2760out_unlock:
2761 spin_unlock(&vcpu->kvm->mmu_lock);
2762 kvm_release_pfn_clean(pfn);
2763 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002764}
2765
Avi Kivity6aa8b732006-12-10 02:21:36 -08002766static void nonpaging_free(struct kvm_vcpu *vcpu)
2767{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002768 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002769}
2770
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002771static int nonpaging_init_context(struct kvm_vcpu *vcpu,
2772 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002773{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002774 context->new_cr3 = nonpaging_new_cr3;
2775 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002776 context->gva_to_gpa = nonpaging_gva_to_gpa;
2777 context->free = nonpaging_free;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002778 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002779 context->invlpg = nonpaging_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08002780 context->update_pte = nonpaging_update_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002781 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002782 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002783 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002784 context->direct_map = true;
Joerg Roedel2d48a982010-09-10 17:31:01 +02002785 context->nx = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002786 return 0;
2787}
2788
Avi Kivityd835dfe2007-11-21 02:57:59 +02002789void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002790{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002791 ++vcpu->stat.tlb_flush;
Avi Kivitya8eeb042010-05-10 12:34:53 +03002792 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002793}
2794
2795static void paging_new_cr3(struct kvm_vcpu *vcpu)
2796{
Avi Kivity9f8fe502010-12-05 17:30:00 +02002797 pgprintk("%s: cr3 %lx\n", __func__, kvm_read_cr3(vcpu));
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002798 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002799}
2800
Joerg Roedel5777ed32010-09-10 17:30:42 +02002801static unsigned long get_cr3(struct kvm_vcpu *vcpu)
2802{
Avi Kivity9f8fe502010-12-05 17:30:00 +02002803 return kvm_read_cr3(vcpu);
Joerg Roedel5777ed32010-09-10 17:30:42 +02002804}
2805
Avi Kivity6389ee92010-11-29 16:12:30 +02002806static void inject_page_fault(struct kvm_vcpu *vcpu,
2807 struct x86_exception *fault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002808{
Avi Kivity6389ee92010-11-29 16:12:30 +02002809 vcpu->arch.mmu.inject_page_fault(vcpu, fault);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002810}
2811
Avi Kivity6aa8b732006-12-10 02:21:36 -08002812static void paging_free(struct kvm_vcpu *vcpu)
2813{
2814 nonpaging_free(vcpu);
2815}
2816
Joerg Roedel3241f222010-09-10 17:30:45 +02002817static bool is_rsvd_bits_set(struct kvm_mmu *mmu, u64 gpte, int level)
Dong, Eddie82725b22009-03-30 16:21:08 +08002818{
2819 int bit7;
2820
2821 bit7 = (gpte >> 7) & 1;
Joerg Roedel3241f222010-09-10 17:30:45 +02002822 return (gpte & mmu->rsvd_bits_mask[bit7][level-1]) != 0;
Dong, Eddie82725b22009-03-30 16:21:08 +08002823}
2824
Avi Kivity6aa8b732006-12-10 02:21:36 -08002825#define PTTYPE 64
2826#include "paging_tmpl.h"
2827#undef PTTYPE
2828
2829#define PTTYPE 32
2830#include "paging_tmpl.h"
2831#undef PTTYPE
2832
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002833static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
2834 struct kvm_mmu *context,
2835 int level)
Dong, Eddie82725b22009-03-30 16:21:08 +08002836{
Dong, Eddie82725b22009-03-30 16:21:08 +08002837 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2838 u64 exb_bit_rsvd = 0;
2839
Joerg Roedel2d48a982010-09-10 17:31:01 +02002840 if (!context->nx)
Dong, Eddie82725b22009-03-30 16:21:08 +08002841 exb_bit_rsvd = rsvd_bits(63, 63);
2842 switch (level) {
2843 case PT32_ROOT_LEVEL:
2844 /* no rsvd bits for 2 level 4K page table entries */
2845 context->rsvd_bits_mask[0][1] = 0;
2846 context->rsvd_bits_mask[0][0] = 0;
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002847 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2848
2849 if (!is_pse(vcpu)) {
2850 context->rsvd_bits_mask[1][1] = 0;
2851 break;
2852 }
2853
Dong, Eddie82725b22009-03-30 16:21:08 +08002854 if (is_cpuid_PSE36())
2855 /* 36bits PSE 4MB page */
2856 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2857 else
2858 /* 32 bits PSE 4MB page */
2859 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08002860 break;
2861 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002862 context->rsvd_bits_mask[0][2] =
2863 rsvd_bits(maxphyaddr, 63) |
2864 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002865 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002866 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002867 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2868 rsvd_bits(maxphyaddr, 62); /* PTE */
2869 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2870 rsvd_bits(maxphyaddr, 62) |
2871 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002872 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002873 break;
2874 case PT64_ROOT_LEVEL:
2875 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2876 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2877 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2878 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2879 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002880 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002881 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2882 rsvd_bits(maxphyaddr, 51);
2883 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002884 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2885 rsvd_bits(maxphyaddr, 51) |
2886 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002887 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002888 rsvd_bits(maxphyaddr, 51) |
2889 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002890 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002891 break;
2892 }
2893}
2894
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002895static int paging64_init_context_common(struct kvm_vcpu *vcpu,
2896 struct kvm_mmu *context,
2897 int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002898{
Joerg Roedel2d48a982010-09-10 17:31:01 +02002899 context->nx = is_nx(vcpu);
2900
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002901 reset_rsvds_bits_mask(vcpu, context, level);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002902
2903 ASSERT(is_pae(vcpu));
2904 context->new_cr3 = paging_new_cr3;
2905 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002906 context->gva_to_gpa = paging64_gva_to_gpa;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002907 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002908 context->invlpg = paging64_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08002909 context->update_pte = paging64_update_pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002910 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002911 context->root_level = level;
2912 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002913 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002914 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002915 return 0;
2916}
2917
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002918static int paging64_init_context(struct kvm_vcpu *vcpu,
2919 struct kvm_mmu *context)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002920{
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002921 return paging64_init_context_common(vcpu, context, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002922}
2923
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002924static int paging32_init_context(struct kvm_vcpu *vcpu,
2925 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002926{
Joerg Roedel2d48a982010-09-10 17:31:01 +02002927 context->nx = false;
2928
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002929 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002930
2931 context->new_cr3 = paging_new_cr3;
2932 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002933 context->gva_to_gpa = paging32_gva_to_gpa;
2934 context->free = paging_free;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002935 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002936 context->invlpg = paging32_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08002937 context->update_pte = paging32_update_pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002938 context->root_level = PT32_ROOT_LEVEL;
2939 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002940 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002941 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002942 return 0;
2943}
2944
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002945static int paging32E_init_context(struct kvm_vcpu *vcpu,
2946 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002947{
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002948 return paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002949}
2950
Joerg Roedelfb72d162008-02-07 13:47:44 +01002951static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2952{
Joerg Roedel14dfe852010-09-10 17:30:49 +02002953 struct kvm_mmu *context = vcpu->arch.walk_mmu;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002954
Avi Kivityc445f8e2010-12-21 16:26:01 +02002955 context->base_role.word = 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002956 context->new_cr3 = nonpaging_new_cr3;
2957 context->page_fault = tdp_page_fault;
2958 context->free = nonpaging_free;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002959 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002960 context->invlpg = nonpaging_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08002961 context->update_pte = nonpaging_update_pte;
Sheng Yang67253af2008-04-25 10:20:22 +08002962 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002963 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002964 context->direct_map = true;
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02002965 context->set_cr3 = kvm_x86_ops->set_tdp_cr3;
Joerg Roedel5777ed32010-09-10 17:30:42 +02002966 context->get_cr3 = get_cr3;
Joerg Roedelcb659db2010-09-10 17:30:43 +02002967 context->inject_page_fault = kvm_inject_page_fault;
Joerg Roedel2d48a982010-09-10 17:31:01 +02002968 context->nx = is_nx(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002969
2970 if (!is_paging(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002971 context->nx = false;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002972 context->gva_to_gpa = nonpaging_gva_to_gpa;
2973 context->root_level = 0;
2974 } else if (is_long_mode(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002975 context->nx = is_nx(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002976 reset_rsvds_bits_mask(vcpu, context, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002977 context->gva_to_gpa = paging64_gva_to_gpa;
2978 context->root_level = PT64_ROOT_LEVEL;
2979 } else if (is_pae(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002980 context->nx = is_nx(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002981 reset_rsvds_bits_mask(vcpu, context, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002982 context->gva_to_gpa = paging64_gva_to_gpa;
2983 context->root_level = PT32E_ROOT_LEVEL;
2984 } else {
Joerg Roedel2d48a982010-09-10 17:31:01 +02002985 context->nx = false;
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002986 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002987 context->gva_to_gpa = paging32_gva_to_gpa;
2988 context->root_level = PT32_ROOT_LEVEL;
2989 }
2990
2991 return 0;
2992}
2993
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002994int kvm_init_shadow_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002995{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002996 int r;
Avi Kivity411c5882011-06-06 16:11:54 +03002997 bool smep = kvm_read_cr4_bits(vcpu, X86_CR4_SMEP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002998 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002999 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003000
3001 if (!is_paging(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003002 r = nonpaging_init_context(vcpu, context);
Avi Kivitya9058ec2006-12-29 16:49:37 -08003003 else if (is_long_mode(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003004 r = paging64_init_context(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003005 else if (is_pae(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003006 r = paging32E_init_context(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003007 else
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003008 r = paging32_init_context(vcpu, context);
Avi Kivitya770f6f2008-12-21 19:20:09 +02003009
Avi Kivity5b7e0102010-04-14 19:20:03 +03003010 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
Joerg Roedelf43addd2010-09-10 17:30:40 +02003011 vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
Avi Kivity411c5882011-06-06 16:11:54 +03003012 vcpu->arch.mmu.base_role.smep_andnot_wp
3013 = smep && !is_write_protection(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003014
3015 return r;
3016}
3017EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu);
3018
3019static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
3020{
Joerg Roedel14dfe852010-09-10 17:30:49 +02003021 int r = kvm_init_shadow_mmu(vcpu, vcpu->arch.walk_mmu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003022
Joerg Roedel14dfe852010-09-10 17:30:49 +02003023 vcpu->arch.walk_mmu->set_cr3 = kvm_x86_ops->set_cr3;
3024 vcpu->arch.walk_mmu->get_cr3 = get_cr3;
3025 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
Avi Kivitya770f6f2008-12-21 19:20:09 +02003026
3027 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003028}
3029
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003030static int init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
3031{
3032 struct kvm_mmu *g_context = &vcpu->arch.nested_mmu;
3033
3034 g_context->get_cr3 = get_cr3;
3035 g_context->inject_page_fault = kvm_inject_page_fault;
3036
3037 /*
3038 * Note that arch.mmu.gva_to_gpa translates l2_gva to l1_gpa. The
3039 * translation of l2_gpa to l1_gpa addresses is done using the
3040 * arch.nested_mmu.gva_to_gpa function. Basically the gva_to_gpa
3041 * functions between mmu and nested_mmu are swapped.
3042 */
3043 if (!is_paging(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003044 g_context->nx = false;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003045 g_context->root_level = 0;
3046 g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested;
3047 } else if (is_long_mode(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003048 g_context->nx = is_nx(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003049 reset_rsvds_bits_mask(vcpu, g_context, PT64_ROOT_LEVEL);
3050 g_context->root_level = PT64_ROOT_LEVEL;
3051 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
3052 } else if (is_pae(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003053 g_context->nx = is_nx(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003054 reset_rsvds_bits_mask(vcpu, g_context, PT32E_ROOT_LEVEL);
3055 g_context->root_level = PT32E_ROOT_LEVEL;
3056 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
3057 } else {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003058 g_context->nx = false;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003059 reset_rsvds_bits_mask(vcpu, g_context, PT32_ROOT_LEVEL);
3060 g_context->root_level = PT32_ROOT_LEVEL;
3061 g_context->gva_to_gpa = paging32_gva_to_gpa_nested;
3062 }
3063
3064 return 0;
3065}
3066
Joerg Roedelfb72d162008-02-07 13:47:44 +01003067static int init_kvm_mmu(struct kvm_vcpu *vcpu)
3068{
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003069 if (mmu_is_nested(vcpu))
3070 return init_kvm_nested_mmu(vcpu);
3071 else if (tdp_enabled)
Joerg Roedelfb72d162008-02-07 13:47:44 +01003072 return init_kvm_tdp_mmu(vcpu);
3073 else
3074 return init_kvm_softmmu(vcpu);
3075}
3076
Avi Kivity6aa8b732006-12-10 02:21:36 -08003077static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
3078{
3079 ASSERT(vcpu);
Sheng Yang62ad0752010-05-12 16:40:41 +08003080 if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
3081 /* mmu.free() should set root_hpa = INVALID_PAGE */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003082 vcpu->arch.mmu.free(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003083}
3084
3085int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
3086{
Avi Kivity17c3ba92007-06-04 15:58:30 +03003087 destroy_kvm_mmu(vcpu);
Marcelo Tosattif8f7e5e2011-06-21 14:00:10 -03003088 return init_kvm_mmu(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03003089}
Eddie Dong8668a3c2007-10-10 14:26:45 +08003090EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03003091
3092int kvm_mmu_load(struct kvm_vcpu *vcpu)
3093{
Avi Kivity714b93d2007-01-05 16:36:53 -08003094 int r;
3095
Avi Kivitye2dec932007-01-05 16:36:54 -08003096 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03003097 if (r)
3098 goto out;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003099 r = mmu_alloc_roots(vcpu);
Avi Kivity8facbbf2010-05-04 12:58:32 +03003100 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003101 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003102 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003103 if (r)
3104 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08003105 /* set_cr3() should ensure TLB has been flushed */
Joerg Roedelf43addd2010-09-10 17:30:40 +02003106 vcpu->arch.mmu.set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08003107out:
3108 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003109}
Avi Kivity17c3ba92007-06-04 15:58:30 +03003110EXPORT_SYMBOL_GPL(kvm_mmu_load);
3111
3112void kvm_mmu_unload(struct kvm_vcpu *vcpu)
3113{
3114 mmu_free_roots(vcpu);
3115}
Joerg Roedel4b161842010-09-10 17:31:03 +02003116EXPORT_SYMBOL_GPL(kvm_mmu_unload);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003117
Avi Kivity00284252007-05-01 16:53:31 +03003118static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Xiao Guangrong7c562522011-03-28 10:29:27 +08003119 struct kvm_mmu_page *sp, u64 *spte,
3120 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03003121{
Marcelo Tosatti30945382008-06-11 20:32:40 -03003122 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02003123 ++vcpu->kvm->stat.mmu_pde_zapped;
3124 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03003125 }
Avi Kivity00284252007-05-01 16:53:31 +03003126
Avi Kivity4cee5762007-11-18 16:37:07 +02003127 ++vcpu->kvm->stat.mmu_pte_updated;
Xiao Guangrong7c562522011-03-28 10:29:27 +08003128 vcpu->arch.mmu.update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03003129}
3130
Avi Kivity79539ce2007-11-21 02:06:21 +02003131static bool need_remote_flush(u64 old, u64 new)
3132{
3133 if (!is_shadow_present_pte(old))
3134 return false;
3135 if (!is_shadow_present_pte(new))
3136 return true;
3137 if ((old ^ new) & PT64_BASE_ADDR_MASK)
3138 return true;
3139 old ^= PT64_NX_MASK;
3140 new ^= PT64_NX_MASK;
3141 return (old & ~new & PT64_PERM_MASK) != 0;
3142}
3143
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003144static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page,
3145 bool remote_flush, bool local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003146{
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003147 if (zap_page)
3148 return;
3149
3150 if (remote_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003151 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003152 else if (local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003153 kvm_mmu_flush_tlb(vcpu);
3154}
3155
Avi Kivity12b7d282007-09-23 14:10:49 +02003156static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
3157{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003158 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02003159
Sheng Yang7b523452008-04-25 21:13:50 +08003160 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02003161}
3162
Avi Kivity1b7fcd32008-05-15 13:51:35 +03003163static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
3164{
3165 u64 *spte = vcpu->arch.last_pte_updated;
3166
3167 if (spte
3168 && vcpu->arch.last_pte_gfn == gfn
3169 && shadow_accessed_mask
3170 && !(*spte & shadow_accessed_mask)
3171 && is_shadow_present_pte(*spte))
3172 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
3173}
3174
Avi Kivity09072da2007-05-01 14:16:52 +03003175void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003176 const u8 *new, int bytes,
3177 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08003178{
Avi Kivity9b7a0322007-01-05 16:36:45 -08003179 gfn_t gfn = gpa >> PAGE_SHIFT;
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003180 union kvm_mmu_page_role mask = { .word = 0 };
Avi Kivity4db35312007-11-21 15:28:32 +02003181 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003182 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003183 LIST_HEAD(invalid_list);
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08003184 u64 entry, gentry, *spte;
3185 unsigned pte_size, page_offset, misaligned, quadrant, offset;
3186 int level, npte, invlpg_counter, r, flooded = 0;
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003187 bool remote_flush, local_flush, zap_page;
3188
Xiao Guangrong332b2072011-05-15 23:20:27 +08003189 /*
3190 * If we don't have indirect shadow pages, it means no page is
3191 * write-protected, so we can exit simply.
3192 */
3193 if (!ACCESS_ONCE(vcpu->kvm->arch.indirect_shadow_pages))
3194 return;
3195
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003196 zap_page = remote_flush = local_flush = false;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08003197 offset = offset_in_page(gpa);
Avi Kivity9b7a0322007-01-05 16:36:45 -08003198
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003199 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivity72016f32010-03-15 13:59:53 +02003200
Avi Kivity08e850c2010-03-15 13:59:57 +02003201 invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
3202
3203 /*
3204 * Assume that the pte write on a page table of the same type
Xiao Guangrong49b26e22011-03-04 19:00:00 +08003205 * as the current vcpu paging mode since we update the sptes only
3206 * when they have the same mode.
Avi Kivity08e850c2010-03-15 13:59:57 +02003207 */
3208 if ((is_pae(vcpu) && bytes == 4) || !new) {
3209 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
3210 if (is_pae(vcpu)) {
3211 gpa &= ~(gpa_t)7;
3212 bytes = 8;
3213 }
3214 r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
3215 if (r)
3216 gentry = 0;
3217 new = (const u8 *)&gentry;
3218 }
3219
Avi Kivity72016f32010-03-15 13:59:53 +02003220 switch (bytes) {
3221 case 4:
3222 gentry = *(const u32 *)new;
3223 break;
3224 case 8:
3225 gentry = *(const u64 *)new;
3226 break;
3227 default:
3228 gentry = 0;
3229 break;
3230 }
3231
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003232 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity08e850c2010-03-15 13:59:57 +02003233 if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
3234 gentry = 0;
Avi Kivityeb787d12007-12-31 15:27:49 +02003235 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02003236 ++vcpu->kvm->stat.mmu_pte_write;
Xiao Guangrong8b1fe172010-08-30 18:22:53 +08003237 trace_kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE);
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003238 if (guest_initiated) {
Xiao Guangrong1b7fd452011-03-04 18:58:02 +08003239 kvm_mmu_access_page(vcpu, gfn);
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003240 if (gfn == vcpu->arch.last_pt_write_gfn
3241 && !last_updated_pte_accessed(vcpu)) {
3242 ++vcpu->arch.last_pt_write_count;
3243 if (vcpu->arch.last_pt_write_count >= 3)
3244 flooded = 1;
3245 } else {
3246 vcpu->arch.last_pt_write_gfn = gfn;
3247 vcpu->arch.last_pt_write_count = 1;
3248 vcpu->arch.last_pte_updated = NULL;
3249 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08003250 }
Xiao Guangrong3246af02010-04-16 16:35:54 +08003251
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003252 mask.cr0_wp = mask.cr4_pae = mask.nxe = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003253 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node) {
Avi Kivity5b7e0102010-04-14 19:20:03 +03003254 pte_size = sp->role.cr4_pae ? 8 : 4;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003255 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03003256 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08003257 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003258 /*
3259 * Misaligned accesses are too much trouble to fix
3260 * up; also, they usually indicate a page is not used
3261 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08003262 *
3263 * If we're seeing too many writes to a page,
3264 * it may no longer be a page table, or we may be
3265 * forking, in which case it is better to unmap the
3266 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003267 */
3268 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02003269 gpa, bytes, sp->role.word);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003270 zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003271 &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02003272 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003273 continue;
3274 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08003275 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02003276 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02003277 npte = 1;
Avi Kivity5b7e0102010-04-14 19:20:03 +03003278 if (!sp->role.cr4_pae) {
Avi Kivityac1b7142007-03-08 17:13:32 +02003279 page_offset <<= 1; /* 32->64 */
3280 /*
3281 * A 32-bit pde maps 4MB while the shadow pdes map
3282 * only 2MB. So we need to double the offset again
3283 * and zap two pdes instead of one.
3284 */
3285 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03003286 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02003287 page_offset <<= 1;
3288 npte = 2;
3289 }
Avi Kivityfce06572007-05-01 16:44:05 +03003290 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003291 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02003292 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03003293 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003294 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003295 local_flush = true;
Avi Kivity4db35312007-11-21 15:28:32 +02003296 spte = &sp->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02003297 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02003298 entry = *spte;
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08003299 mmu_page_zap_pte(vcpu->kvm, sp, spte);
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003300 if (gentry &&
3301 !((sp->role.word ^ vcpu->arch.mmu.base_role.word)
3302 & mask.word))
Xiao Guangrong7c562522011-03-28 10:29:27 +08003303 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003304 if (!remote_flush && need_remote_flush(entry, *spte))
3305 remote_flush = true;
Avi Kivityac1b7142007-03-08 17:13:32 +02003306 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003307 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08003308 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003309 mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003310 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong8b1fe172010-08-30 18:22:53 +08003311 trace_kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003312 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivityda4a00f2007-01-05 16:36:44 -08003313}
3314
Avi Kivitya4360362007-01-05 16:36:45 -08003315int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
3316{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003317 gpa_t gpa;
3318 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08003319
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02003320 if (vcpu->arch.mmu.direct_map)
Avi Kivity60f24782009-08-27 13:37:06 +03003321 return 0;
3322
Gleb Natapov1871c602010-02-10 14:21:32 +02003323 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003324
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003325 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003326 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003327 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003328 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08003329}
Avi Kivity577bdc42008-07-19 08:57:05 +03003330EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08003331
Avi Kivity22d95b12007-09-14 20:26:06 +03003332void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08003333{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003334 LIST_HEAD(invalid_list);
Xiao Guangrong103ad252010-06-04 21:54:38 +08003335
Dave Hansene0df7b92010-08-19 18:11:05 -07003336 while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES &&
Izik Eidus3b80fff2009-07-28 15:26:58 -03003337 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02003338 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08003339
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003340 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02003341 struct kvm_mmu_page, link);
Dave Hansene0df7b92010-08-19 18:11:05 -07003342 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02003343 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08003344 }
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08003345 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Avi Kivityebeace82007-01-05 16:36:47 -08003346}
Avi Kivityebeace82007-01-05 16:36:47 -08003347
Andre Przywaradc25e892010-12-21 11:12:07 +01003348int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code,
3349 void *insn, int insn_len)
Avi Kivity30677142007-10-28 18:48:59 +02003350{
3351 int r;
3352 enum emulation_result er;
3353
Gleb Natapov56028d02010-10-17 18:13:42 +02003354 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code, false);
Avi Kivity30677142007-10-28 18:48:59 +02003355 if (r < 0)
3356 goto out;
3357
3358 if (!r) {
3359 r = 1;
3360 goto out;
3361 }
3362
Avi Kivityb733bfb2007-10-28 18:52:05 +02003363 r = mmu_topup_memory_caches(vcpu);
3364 if (r)
3365 goto out;
3366
Andre Przywaradc25e892010-12-21 11:12:07 +01003367 er = x86_emulate_instruction(vcpu, cr2, 0, insn, insn_len);
Avi Kivity30677142007-10-28 18:48:59 +02003368
3369 switch (er) {
3370 case EMULATE_DONE:
3371 return 1;
3372 case EMULATE_DO_MMIO:
3373 ++vcpu->stat.mmio_exits;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03003374 /* fall through */
Avi Kivity30677142007-10-28 18:48:59 +02003375 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03003376 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02003377 default:
3378 BUG();
3379 }
3380out:
Avi Kivity30677142007-10-28 18:48:59 +02003381 return r;
3382}
3383EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
3384
Marcelo Tosattia7052892008-09-23 13:18:35 -03003385void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
3386{
Marcelo Tosattia7052892008-09-23 13:18:35 -03003387 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003388 kvm_mmu_flush_tlb(vcpu);
3389 ++vcpu->stat.invlpg;
3390}
3391EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
3392
Joerg Roedel18552672008-02-07 13:47:41 +01003393void kvm_enable_tdp(void)
3394{
3395 tdp_enabled = true;
3396}
3397EXPORT_SYMBOL_GPL(kvm_enable_tdp);
3398
Joerg Roedel5f4cb662008-07-14 20:36:36 +02003399void kvm_disable_tdp(void)
3400{
3401 tdp_enabled = false;
3402}
3403EXPORT_SYMBOL_GPL(kvm_disable_tdp);
3404
Avi Kivity6aa8b732006-12-10 02:21:36 -08003405static void free_mmu_pages(struct kvm_vcpu *vcpu)
3406{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003407 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Joerg Roedel81407ca2010-09-10 17:31:00 +02003408 if (vcpu->arch.mmu.lm_root != NULL)
3409 free_page((unsigned long)vcpu->arch.mmu.lm_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003410}
3411
3412static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
3413{
Avi Kivity17ac10a2007-01-05 16:36:40 -08003414 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003415 int i;
3416
3417 ASSERT(vcpu);
3418
Avi Kivity17ac10a2007-01-05 16:36:40 -08003419 /*
3420 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
3421 * Therefore we need to allocate shadow page tables in the first
3422 * 4GB of memory, which happens to fit the DMA32 zone.
3423 */
3424 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
3425 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08003426 return -ENOMEM;
3427
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003428 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003429 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003430 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003431
Avi Kivity6aa8b732006-12-10 02:21:36 -08003432 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003433}
3434
Ingo Molnar8018c272006-12-29 16:50:01 -08003435int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003436{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003437 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003438 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003439
Ingo Molnar8018c272006-12-29 16:50:01 -08003440 return alloc_mmu_pages(vcpu);
3441}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003442
Ingo Molnar8018c272006-12-29 16:50:01 -08003443int kvm_mmu_setup(struct kvm_vcpu *vcpu)
3444{
3445 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003446 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08003447
Ingo Molnar8018c272006-12-29 16:50:01 -08003448 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003449}
3450
Avi Kivity90cb0522007-07-17 13:04:56 +03003451void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003452{
Avi Kivity4db35312007-11-21 15:28:32 +02003453 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003454
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003455 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003456 int i;
3457 u64 *pt;
3458
Sheng Yang291f26b2008-10-16 17:30:57 +08003459 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003460 continue;
3461
Avi Kivity4db35312007-11-21 15:28:32 +02003462 pt = sp->spt;
Avi Kivity8234b222010-12-27 12:08:45 +02003463 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Xiao Guangrongda8dc752011-03-04 18:56:41 +08003464 if (!is_shadow_present_pte(pt[i]) ||
3465 !is_last_spte(pt[i], sp->role.level))
3466 continue;
3467
3468 if (is_large_pte(pt[i])) {
Xiao Guangrongc3707952011-07-12 03:28:04 +08003469 drop_spte(kvm, &pt[i]);
Avi Kivity8234b222010-12-27 12:08:45 +02003470 --kvm->stat.lpages;
Xiao Guangrongda8dc752011-03-04 18:56:41 +08003471 continue;
Avi Kivity8234b222010-12-27 12:08:45 +02003472 }
Xiao Guangrongda8dc752011-03-04 18:56:41 +08003473
Avi Kivity6aa8b732006-12-10 02:21:36 -08003474 /* avoid RMW */
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003475 if (is_writable_pte(pt[i]))
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08003476 mmu_spte_update(&pt[i],
3477 pt[i] & ~PT_WRITABLE_MASK);
Avi Kivity8234b222010-12-27 12:08:45 +02003478 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003479 }
Avi Kivity171d5952008-08-27 16:40:51 +03003480 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003481}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003482
Avi Kivity90cb0522007-07-17 13:04:56 +03003483void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03003484{
Avi Kivity4db35312007-11-21 15:28:32 +02003485 struct kvm_mmu_page *sp, *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003486 LIST_HEAD(invalid_list);
Dor Laore0fa8262007-03-30 13:06:33 +03003487
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003488 spin_lock(&kvm->mmu_lock);
Xiao Guangrong3246af02010-04-16 16:35:54 +08003489restart:
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003490 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003491 if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list))
Xiao Guangrong3246af02010-04-16 16:35:54 +08003492 goto restart;
3493
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003494 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003495 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03003496}
3497
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003498static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm,
3499 struct list_head *invalid_list)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003500{
3501 struct kvm_mmu_page *page;
3502
3503 page = container_of(kvm->arch.active_mmu_pages.prev,
3504 struct kvm_mmu_page, link);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003505 return kvm_mmu_prepare_zap_page(kvm, page, invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003506}
3507
Ying Han1495f232011-05-24 17:12:27 -07003508static int mmu_shrink(struct shrinker *shrink, struct shrink_control *sc)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003509{
3510 struct kvm *kvm;
3511 struct kvm *kvm_freed = NULL;
Ying Han1495f232011-05-24 17:12:27 -07003512 int nr_to_scan = sc->nr_to_scan;
Dave Hansen45221ab2010-08-19 18:11:37 -07003513
3514 if (nr_to_scan == 0)
3515 goto out;
Izik Eidus3ee16c82008-03-30 15:17:21 +03003516
Jan Kiszkae935b832011-02-08 12:55:33 +01003517 raw_spin_lock(&kvm_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003518
3519 list_for_each_entry(kvm, &vm_list, vm_list) {
Dave Hansen45221ab2010-08-19 18:11:37 -07003520 int idx, freed_pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003521 LIST_HEAD(invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003522
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003523 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003524 spin_lock(&kvm->mmu_lock);
Dave Hansen45221ab2010-08-19 18:11:37 -07003525 if (!kvm_freed && nr_to_scan > 0 &&
3526 kvm->arch.n_used_mmu_pages > 0) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003527 freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm,
3528 &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003529 kvm_freed = kvm;
3530 }
3531 nr_to_scan--;
3532
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003533 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003534 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003535 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003536 }
3537 if (kvm_freed)
3538 list_move_tail(&kvm_freed->vm_list, &vm_list);
3539
Jan Kiszkae935b832011-02-08 12:55:33 +01003540 raw_spin_unlock(&kvm_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003541
Dave Hansen45221ab2010-08-19 18:11:37 -07003542out:
3543 return percpu_counter_read_positive(&kvm_total_used_mmu_pages);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003544}
3545
3546static struct shrinker mmu_shrinker = {
3547 .shrink = mmu_shrink,
3548 .seeks = DEFAULT_SEEKS * 10,
3549};
3550
Ingo Molnar2ddfd202008-05-22 10:37:48 +02003551static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03003552{
Xiao Guangrong53c07b12011-05-15 23:26:20 +08003553 if (pte_list_desc_cache)
3554 kmem_cache_destroy(pte_list_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003555 if (mmu_page_header_cache)
3556 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003557}
3558
3559int kvm_mmu_module_init(void)
3560{
Xiao Guangrong53c07b12011-05-15 23:26:20 +08003561 pte_list_desc_cache = kmem_cache_create("pte_list_desc",
3562 sizeof(struct pte_list_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09003563 0, 0, NULL);
Xiao Guangrong53c07b12011-05-15 23:26:20 +08003564 if (!pte_list_desc_cache)
Avi Kivityb5a33a72007-04-15 16:31:09 +03003565 goto nomem;
3566
Avi Kivityd3d25b02007-05-30 12:34:53 +03003567 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
3568 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09003569 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003570 if (!mmu_page_header_cache)
3571 goto nomem;
3572
Wei Yongjun45bf21a2010-08-23 16:13:15 +08003573 if (percpu_counter_init(&kvm_total_used_mmu_pages, 0))
3574 goto nomem;
3575
Izik Eidus3ee16c82008-03-30 15:17:21 +03003576 register_shrinker(&mmu_shrinker);
3577
Avi Kivityb5a33a72007-04-15 16:31:09 +03003578 return 0;
3579
3580nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03003581 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003582 return -ENOMEM;
3583}
3584
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003585/*
3586 * Caculate mmu pages needed for kvm.
3587 */
3588unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3589{
3590 int i;
3591 unsigned int nr_mmu_pages;
3592 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003593 struct kvm_memslots *slots;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003594
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003595 slots = kvm_memslots(kvm);
3596
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003597 for (i = 0; i < slots->nmemslots; i++)
3598 nr_pages += slots->memslots[i].npages;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003599
3600 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3601 nr_mmu_pages = max(nr_mmu_pages,
3602 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3603
3604 return nr_mmu_pages;
3605}
3606
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003607static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3608 unsigned len)
3609{
3610 if (len > buffer->len)
3611 return NULL;
3612 return buffer->ptr;
3613}
3614
3615static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3616 unsigned len)
3617{
3618 void *ret;
3619
3620 ret = pv_mmu_peek_buffer(buffer, len);
3621 if (!ret)
3622 return ret;
3623 buffer->ptr += len;
3624 buffer->len -= len;
3625 buffer->processed += len;
3626 return ret;
3627}
3628
3629static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3630 gpa_t addr, gpa_t value)
3631{
3632 int bytes = 8;
3633 int r;
3634
3635 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3636 bytes = 4;
3637
3638 r = mmu_topup_memory_caches(vcpu);
3639 if (r)
3640 return r;
3641
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003642 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003643 return -EFAULT;
3644
3645 return 1;
3646}
3647
3648static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3649{
Avi Kivity9f8fe502010-12-05 17:30:00 +02003650 (void)kvm_set_cr3(vcpu, kvm_read_cr3(vcpu));
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003651 return 1;
3652}
3653
3654static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3655{
3656 spin_lock(&vcpu->kvm->mmu_lock);
3657 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3658 spin_unlock(&vcpu->kvm->mmu_lock);
3659 return 1;
3660}
3661
3662static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3663 struct kvm_pv_mmu_op_buffer *buffer)
3664{
3665 struct kvm_mmu_op_header *header;
3666
3667 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3668 if (!header)
3669 return 0;
3670 switch (header->op) {
3671 case KVM_MMU_OP_WRITE_PTE: {
3672 struct kvm_mmu_op_write_pte *wpte;
3673
3674 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3675 if (!wpte)
3676 return 0;
3677 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3678 wpte->pte_val);
3679 }
3680 case KVM_MMU_OP_FLUSH_TLB: {
3681 struct kvm_mmu_op_flush_tlb *ftlb;
3682
3683 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3684 if (!ftlb)
3685 return 0;
3686 return kvm_pv_mmu_flush_tlb(vcpu);
3687 }
3688 case KVM_MMU_OP_RELEASE_PT: {
3689 struct kvm_mmu_op_release_pt *rpt;
3690
3691 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3692 if (!rpt)
3693 return 0;
3694 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3695 }
3696 default: return 0;
3697 }
3698}
3699
3700int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3701 gpa_t addr, unsigned long *ret)
3702{
3703 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003704 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003705
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003706 buffer->ptr = buffer->buf;
3707 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3708 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003709
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003710 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003711 if (r)
3712 goto out;
3713
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003714 while (buffer->len) {
3715 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003716 if (r < 0)
3717 goto out;
3718 if (r == 0)
3719 break;
3720 }
3721
3722 r = 1;
3723out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003724 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003725 return r;
3726}
3727
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003728int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3729{
3730 struct kvm_shadow_walk_iterator iterator;
3731 int nr_sptes = 0;
3732
3733 spin_lock(&vcpu->kvm->mmu_lock);
3734 for_each_shadow_entry(vcpu, addr, iterator) {
3735 sptes[iterator.level-1] = *iterator.sptep;
3736 nr_sptes++;
3737 if (!is_shadow_present_pte(*iterator.sptep))
3738 break;
3739 }
3740 spin_unlock(&vcpu->kvm->mmu_lock);
3741
3742 return nr_sptes;
3743}
3744EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3745
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08003746void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
3747{
3748 ASSERT(vcpu);
3749
3750 destroy_kvm_mmu(vcpu);
3751 free_mmu_pages(vcpu);
3752 mmu_free_memory_caches(vcpu);
Xiao Guangrongb034cf02010-12-23 16:08:35 +08003753}
3754
3755#ifdef CONFIG_KVM_MMU_AUDIT
3756#include "mmu_audit.c"
3757#else
3758static void mmu_audit_disable(void) { }
3759#endif
3760
3761void kvm_mmu_module_exit(void)
3762{
3763 mmu_destroy_caches();
3764 percpu_counter_destroy(&kvm_total_used_mmu_pages);
3765 unregister_shrinker(&mmu_shrinker);
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08003766 mmu_audit_disable();
3767}