blob: a22b5fe92167b268d344168e4f1f571be91c0701 [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 Kivityda9285212007-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
Xiao Guangrong603e0652011-07-12 03:31:28 +0800262#ifdef CONFIG_X86_64
Avi Kivityd555c332009-06-10 14:24:23 +0300263static void __set_spte(u64 *sptep, u64 spte)
Avi Kivitye663ee62007-05-31 15:46:04 +0300264{
Xiao Guangrong603e0652011-07-12 03:31:28 +0800265 *sptep = spte;
Avi Kivitye663ee62007-05-31 15:46:04 +0300266}
267
Xiao Guangrong603e0652011-07-12 03:31:28 +0800268static void __update_clear_spte_fast(u64 *sptep, u64 spte)
Avi Kivitya9221dd2010-06-06 14:48:06 +0300269{
Xiao Guangrong603e0652011-07-12 03:31:28 +0800270 *sptep = spte;
Avi Kivitya9221dd2010-06-06 14:48:06 +0300271}
272
Xiao Guangrong603e0652011-07-12 03:31:28 +0800273static u64 __update_clear_spte_slow(u64 *sptep, u64 spte)
274{
275 return xchg(sptep, spte);
276}
277#else
278union split_spte {
279 struct {
280 u32 spte_low;
281 u32 spte_high;
282 };
283 u64 spte;
284};
285
286static void __set_spte(u64 *sptep, u64 spte)
287{
288 union split_spte *ssptep, sspte;
289
290 ssptep = (union split_spte *)sptep;
291 sspte = (union split_spte)spte;
292
293 ssptep->spte_high = sspte.spte_high;
294
295 /*
296 * If we map the spte from nonpresent to present, We should store
297 * the high bits firstly, then set present bit, so cpu can not
298 * fetch this spte while we are setting the spte.
299 */
300 smp_wmb();
301
302 ssptep->spte_low = sspte.spte_low;
303}
304
305static void __update_clear_spte_fast(u64 *sptep, u64 spte)
306{
307 union split_spte *ssptep, sspte;
308
309 ssptep = (union split_spte *)sptep;
310 sspte = (union split_spte)spte;
311
312 ssptep->spte_low = sspte.spte_low;
313
314 /*
315 * If we map the spte from present to nonpresent, we should clear
316 * present bit firstly to avoid vcpu fetch the old high bits.
317 */
318 smp_wmb();
319
320 ssptep->spte_high = sspte.spte_high;
321}
322
323static u64 __update_clear_spte_slow(u64 *sptep, u64 spte)
324{
325 union split_spte *ssptep, sspte, orig;
326
327 ssptep = (union split_spte *)sptep;
328 sspte = (union split_spte)spte;
329
330 /* xchg acts as a barrier before the setting of the high bits */
331 orig.spte_low = xchg(&ssptep->spte_low, sspte.spte_low);
332 orig.spte_high = ssptep->spte_high = sspte.spte_high;
333
334 return orig.spte;
335}
336#endif
337
Xiao Guangrong8672b722010-08-02 16:14:04 +0800338static bool spte_has_volatile_bits(u64 spte)
339{
340 if (!shadow_accessed_mask)
341 return false;
342
343 if (!is_shadow_present_pte(spte))
344 return false;
345
Xiao Guangrong41327792010-08-02 16:15:08 +0800346 if ((spte & shadow_accessed_mask) &&
347 (!is_writable_pte(spte) || (spte & shadow_dirty_mask)))
Xiao Guangrong8672b722010-08-02 16:14:04 +0800348 return false;
349
350 return true;
351}
352
Xiao Guangrong41327792010-08-02 16:15:08 +0800353static bool spte_is_bit_cleared(u64 old_spte, u64 new_spte, u64 bit_mask)
354{
355 return (old_spte & bit_mask) && !(new_spte & bit_mask);
356}
357
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800358/* Rules for using mmu_spte_set:
359 * Set the sptep from nonpresent to present.
360 * Note: the sptep being assigned *must* be either not present
361 * or in a state where the hardware will not attempt to update
362 * the spte.
363 */
364static void mmu_spte_set(u64 *sptep, u64 new_spte)
365{
366 WARN_ON(is_shadow_present_pte(*sptep));
367 __set_spte(sptep, new_spte);
368}
369
370/* Rules for using mmu_spte_update:
371 * Update the state bits, it means the mapped pfn is not changged.
372 */
373static void mmu_spte_update(u64 *sptep, u64 new_spte)
Avi Kivityb79b93f2010-06-06 15:46:44 +0300374{
Xiao Guangrong41327792010-08-02 16:15:08 +0800375 u64 mask, old_spte = *sptep;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300376
Xiao Guangrong41327792010-08-02 16:15:08 +0800377 WARN_ON(!is_rmap_spte(new_spte));
378
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800379 if (!is_shadow_present_pte(old_spte))
380 return mmu_spte_set(sptep, new_spte);
381
Xiao Guangrong41327792010-08-02 16:15:08 +0800382 new_spte |= old_spte & shadow_dirty_mask;
383
384 mask = shadow_accessed_mask;
385 if (is_writable_pte(old_spte))
386 mask |= shadow_dirty_mask;
387
388 if (!spte_has_volatile_bits(old_spte) || (new_spte & mask) == mask)
Xiao Guangrong603e0652011-07-12 03:31:28 +0800389 __update_clear_spte_fast(sptep, new_spte);
Xiao Guangrong41327792010-08-02 16:15:08 +0800390 else
Xiao Guangrong603e0652011-07-12 03:31:28 +0800391 old_spte = __update_clear_spte_slow(sptep, new_spte);
Xiao Guangrong41327792010-08-02 16:15:08 +0800392
393 if (!shadow_accessed_mask)
394 return;
395
396 if (spte_is_bit_cleared(old_spte, new_spte, shadow_accessed_mask))
397 kvm_set_pfn_accessed(spte_to_pfn(old_spte));
398 if (spte_is_bit_cleared(old_spte, new_spte, shadow_dirty_mask))
399 kvm_set_pfn_dirty(spte_to_pfn(old_spte));
Avi Kivityb79b93f2010-06-06 15:46:44 +0300400}
401
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800402/*
403 * Rules for using mmu_spte_clear_track_bits:
404 * It sets the sptep from present to nonpresent, and track the
405 * state bits, it is used to clear the last level sptep.
406 */
407static int mmu_spte_clear_track_bits(u64 *sptep)
408{
409 pfn_t pfn;
410 u64 old_spte = *sptep;
411
412 if (!spte_has_volatile_bits(old_spte))
Xiao Guangrong603e0652011-07-12 03:31:28 +0800413 __update_clear_spte_fast(sptep, 0ull);
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800414 else
Xiao Guangrong603e0652011-07-12 03:31:28 +0800415 old_spte = __update_clear_spte_slow(sptep, 0ull);
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800416
417 if (!is_rmap_spte(old_spte))
418 return 0;
419
420 pfn = spte_to_pfn(old_spte);
421 if (!shadow_accessed_mask || old_spte & shadow_accessed_mask)
422 kvm_set_pfn_accessed(pfn);
423 if (!shadow_dirty_mask || (old_spte & shadow_dirty_mask))
424 kvm_set_pfn_dirty(pfn);
425 return 1;
426}
427
428/*
429 * Rules for using mmu_spte_clear_no_track:
430 * Directly clear spte without caring the state bits of sptep,
431 * it is used to set the upper level spte.
432 */
433static void mmu_spte_clear_no_track(u64 *sptep)
434{
Xiao Guangrong603e0652011-07-12 03:31:28 +0800435 __update_clear_spte_fast(sptep, 0ull);
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800436}
437
Avi Kivitye2dec932007-01-05 16:36:54 -0800438static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300439 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800440{
441 void *obj;
442
443 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800444 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800445 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300446 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800447 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800448 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800449 cache->objects[cache->nobjs++] = obj;
450 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800451 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800452}
453
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800454static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc,
455 struct kmem_cache *cache)
Avi Kivity714b93d2007-01-05 16:36:53 -0800456{
457 while (mc->nobjs)
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800458 kmem_cache_free(cache, mc->objects[--mc->nobjs]);
Avi Kivity714b93d2007-01-05 16:36:53 -0800459}
460
Avi Kivityc1158e62007-07-20 08:18:27 +0300461static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300462 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300463{
Xiao Guangrong842f22e2011-03-04 19:01:10 +0800464 void *page;
Avi Kivityc1158e62007-07-20 08:18:27 +0300465
466 if (cache->nobjs >= min)
467 return 0;
468 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Xiao Guangrong842f22e2011-03-04 19:01:10 +0800469 page = (void *)__get_free_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300470 if (!page)
471 return -ENOMEM;
Xiao Guangrong842f22e2011-03-04 19:01:10 +0800472 cache->objects[cache->nobjs++] = page;
Avi Kivityc1158e62007-07-20 08:18:27 +0300473 }
474 return 0;
475}
476
477static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
478{
479 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300480 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300481}
482
Avi Kivity8c438502007-04-16 11:53:17 +0300483static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
484{
485 int r;
486
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800487 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
Xiao Guangrong67052b32011-05-15 23:27:08 +0800488 pte_list_desc_cache, 8 + PTE_PREFETCH_NUM);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300489 if (r)
490 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800491 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300492 if (r)
493 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800494 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300495 mmu_page_header_cache, 4);
496out:
Avi Kivity8c438502007-04-16 11:53:17 +0300497 return r;
498}
499
Avi Kivity714b93d2007-01-05 16:36:53 -0800500static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
501{
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800502 mmu_free_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
503 pte_list_desc_cache);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800504 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800505 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache,
506 mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800507}
508
509static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
510 size_t size)
511{
512 void *p;
513
514 BUG_ON(!mc->nobjs);
515 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800516 return p;
517}
518
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800519static struct pte_list_desc *mmu_alloc_pte_list_desc(struct kvm_vcpu *vcpu)
Avi Kivity714b93d2007-01-05 16:36:53 -0800520{
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800521 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_list_desc_cache,
522 sizeof(struct pte_list_desc));
Avi Kivity714b93d2007-01-05 16:36:53 -0800523}
524
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800525static void mmu_free_pte_list_desc(struct pte_list_desc *pte_list_desc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800526{
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800527 kmem_cache_free(pte_list_desc_cache, pte_list_desc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800528}
529
Lai Jiangshan2032a932010-05-26 16:49:59 +0800530static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
531{
532 if (!sp->role.direct)
533 return sp->gfns[index];
534
535 return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS));
536}
537
538static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn)
539{
540 if (sp->role.direct)
541 BUG_ON(gfn != kvm_mmu_page_get_gfn(sp, index));
542 else
543 sp->gfns[index] = gfn;
544}
545
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800546/*
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900547 * Return the pointer to the large page information for a given gfn,
548 * handling slots that are not large page aligned.
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300549 */
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900550static struct kvm_lpage_info *lpage_info_slot(gfn_t gfn,
551 struct kvm_memory_slot *slot,
552 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300553{
554 unsigned long idx;
555
Joerg Roedel82855412010-07-01 16:00:11 +0200556 idx = (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
557 (slot->base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900558 return &slot->lpage_info[level - 2][idx];
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300559}
560
561static void account_shadowed(struct kvm *kvm, gfn_t gfn)
562{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200563 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900564 struct kvm_lpage_info *linfo;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200565 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300566
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300567 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200568 for (i = PT_DIRECTORY_LEVEL;
569 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900570 linfo = lpage_info_slot(gfn, slot, i);
571 linfo->write_count += 1;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200572 }
Xiao Guangrong332b2072011-05-15 23:20:27 +0800573 kvm->arch.indirect_shadow_pages++;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300574}
575
576static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
577{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200578 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900579 struct kvm_lpage_info *linfo;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200580 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300581
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300582 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200583 for (i = PT_DIRECTORY_LEVEL;
584 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900585 linfo = lpage_info_slot(gfn, slot, i);
586 linfo->write_count -= 1;
587 WARN_ON(linfo->write_count < 0);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200588 }
Xiao Guangrong332b2072011-05-15 23:20:27 +0800589 kvm->arch.indirect_shadow_pages--;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300590}
591
Joerg Roedeld25797b2009-07-27 16:30:43 +0200592static int has_wrprotected_page(struct kvm *kvm,
593 gfn_t gfn,
594 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300595{
Izik Eidus28430992008-10-03 17:40:32 +0300596 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900597 struct kvm_lpage_info *linfo;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300598
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300599 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300600 if (slot) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900601 linfo = lpage_info_slot(gfn, slot, level);
602 return linfo->write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300603 }
604
605 return 1;
606}
607
Joerg Roedeld25797b2009-07-27 16:30:43 +0200608static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300609{
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100610 unsigned long page_size;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200611 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300612
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100613 page_size = kvm_host_page_size(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300614
Joerg Roedeld25797b2009-07-27 16:30:43 +0200615 for (i = PT_PAGE_TABLE_LEVEL;
616 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
617 if (page_size >= KVM_HPAGE_SIZE(i))
618 ret = i;
619 else
620 break;
621 }
622
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300623 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300624}
625
Xiao Guangrong5d163b12011-03-09 15:43:00 +0800626static struct kvm_memory_slot *
627gfn_to_memslot_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t gfn,
628 bool no_dirty_log)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300629{
630 struct kvm_memory_slot *slot;
Xiao Guangrong5d163b12011-03-09 15:43:00 +0800631
632 slot = gfn_to_memslot(vcpu->kvm, gfn);
633 if (!slot || slot->flags & KVM_MEMSLOT_INVALID ||
634 (no_dirty_log && slot->dirty_bitmap))
635 slot = NULL;
636
637 return slot;
638}
639
640static bool mapping_level_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t large_gfn)
641{
Stevea0a8eab2011-06-17 10:25:39 +0800642 return !gfn_to_memslot_dirty_bitmap(vcpu, large_gfn, true);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800643}
644
645static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
646{
647 int host_level, level, max_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300648
Joerg Roedeld25797b2009-07-27 16:30:43 +0200649 host_level = host_mapping_level(vcpu->kvm, large_gfn);
650
651 if (host_level == PT_PAGE_TABLE_LEVEL)
652 return host_level;
653
Sheng Yang878403b2010-01-05 19:02:29 +0800654 max_level = kvm_x86_ops->get_lpage_level() < host_level ?
655 kvm_x86_ops->get_lpage_level() : host_level;
656
657 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200658 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
659 break;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200660
661 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300662}
663
664/*
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800665 * Pte mapping structures:
666 *
667 * If pte_list bit zero is zero, then pte_list point to the spte.
668 *
669 * If pte_list bit zero is one, (then pte_list & ~1) points to a struct
670 * pte_list_desc containing more mappings.
671 *
672 * Returns the number of pte entries before the spte was added or zero if
673 * the spte was not added.
674 *
675 */
676static int pte_list_add(struct kvm_vcpu *vcpu, u64 *spte,
677 unsigned long *pte_list)
678{
679 struct pte_list_desc *desc;
680 int i, count = 0;
681
682 if (!*pte_list) {
683 rmap_printk("pte_list_add: %p %llx 0->1\n", spte, *spte);
684 *pte_list = (unsigned long)spte;
685 } else if (!(*pte_list & 1)) {
686 rmap_printk("pte_list_add: %p %llx 1->many\n", spte, *spte);
687 desc = mmu_alloc_pte_list_desc(vcpu);
688 desc->sptes[0] = (u64 *)*pte_list;
689 desc->sptes[1] = spte;
690 *pte_list = (unsigned long)desc | 1;
691 ++count;
692 } else {
693 rmap_printk("pte_list_add: %p %llx many->many\n", spte, *spte);
694 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
695 while (desc->sptes[PTE_LIST_EXT-1] && desc->more) {
696 desc = desc->more;
697 count += PTE_LIST_EXT;
698 }
699 if (desc->sptes[PTE_LIST_EXT-1]) {
700 desc->more = mmu_alloc_pte_list_desc(vcpu);
701 desc = desc->more;
702 }
703 for (i = 0; desc->sptes[i]; ++i)
704 ++count;
705 desc->sptes[i] = spte;
706 }
707 return count;
708}
709
710static u64 *pte_list_next(unsigned long *pte_list, u64 *spte)
711{
712 struct pte_list_desc *desc;
713 u64 *prev_spte;
714 int i;
715
716 if (!*pte_list)
717 return NULL;
718 else if (!(*pte_list & 1)) {
719 if (!spte)
720 return (u64 *)*pte_list;
721 return NULL;
722 }
723 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
724 prev_spte = NULL;
725 while (desc) {
726 for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i) {
727 if (prev_spte == spte)
728 return desc->sptes[i];
729 prev_spte = desc->sptes[i];
730 }
731 desc = desc->more;
732 }
733 return NULL;
734}
735
736static void
737pte_list_desc_remove_entry(unsigned long *pte_list, struct pte_list_desc *desc,
738 int i, struct pte_list_desc *prev_desc)
739{
740 int j;
741
742 for (j = PTE_LIST_EXT - 1; !desc->sptes[j] && j > i; --j)
743 ;
744 desc->sptes[i] = desc->sptes[j];
745 desc->sptes[j] = NULL;
746 if (j != 0)
747 return;
748 if (!prev_desc && !desc->more)
749 *pte_list = (unsigned long)desc->sptes[0];
750 else
751 if (prev_desc)
752 prev_desc->more = desc->more;
753 else
754 *pte_list = (unsigned long)desc->more | 1;
755 mmu_free_pte_list_desc(desc);
756}
757
758static void pte_list_remove(u64 *spte, unsigned long *pte_list)
759{
760 struct pte_list_desc *desc;
761 struct pte_list_desc *prev_desc;
762 int i;
763
764 if (!*pte_list) {
765 printk(KERN_ERR "pte_list_remove: %p 0->BUG\n", spte);
766 BUG();
767 } else if (!(*pte_list & 1)) {
768 rmap_printk("pte_list_remove: %p 1->0\n", spte);
769 if ((u64 *)*pte_list != spte) {
770 printk(KERN_ERR "pte_list_remove: %p 1->BUG\n", spte);
771 BUG();
772 }
773 *pte_list = 0;
774 } else {
775 rmap_printk("pte_list_remove: %p many->many\n", spte);
776 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
777 prev_desc = NULL;
778 while (desc) {
779 for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i)
780 if (desc->sptes[i] == spte) {
781 pte_list_desc_remove_entry(pte_list,
782 desc, i,
783 prev_desc);
784 return;
785 }
786 prev_desc = desc;
787 desc = desc->more;
788 }
789 pr_err("pte_list_remove: %p many->many\n", spte);
790 BUG();
791 }
792}
793
Xiao Guangrong67052b32011-05-15 23:27:08 +0800794typedef void (*pte_list_walk_fn) (u64 *spte);
795static void pte_list_walk(unsigned long *pte_list, pte_list_walk_fn fn)
796{
797 struct pte_list_desc *desc;
798 int i;
799
800 if (!*pte_list)
801 return;
802
803 if (!(*pte_list & 1))
804 return fn((u64 *)*pte_list);
805
806 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
807 while (desc) {
808 for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i)
809 fn(desc->sptes[i]);
810 desc = desc->more;
811 }
812}
813
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800814/*
Izik Eidus290fc382007-09-27 14:11:22 +0200815 * Take gfn and return the reverse mapping to it.
Izik Eidus290fc382007-09-27 14:11:22 +0200816 */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200817static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
Izik Eidus290fc382007-09-27 14:11:22 +0200818{
819 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900820 struct kvm_lpage_info *linfo;
Izik Eidus290fc382007-09-27 14:11:22 +0200821
822 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200823 if (likely(level == PT_PAGE_TABLE_LEVEL))
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300824 return &slot->rmap[gfn - slot->base_gfn];
825
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900826 linfo = lpage_info_slot(gfn, slot, level);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300827
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900828 return &linfo->rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200829}
830
Joerg Roedel44ad9942009-07-27 16:30:42 +0200831static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800832{
Avi Kivity4db35312007-11-21 15:28:32 +0200833 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +0200834 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800835
Avi Kivity4db35312007-11-21 15:28:32 +0200836 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800837 kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200838 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800839 return pte_list_add(vcpu, spte, rmapp);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800840}
841
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800842static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800843{
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800844 return pte_list_next(rmapp, spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800845}
846
Izik Eidus290fc382007-09-27 14:11:22 +0200847static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800848{
Avi Kivity4db35312007-11-21 15:28:32 +0200849 struct kvm_mmu_page *sp;
Lai Jiangshan2032a932010-05-26 16:49:59 +0800850 gfn_t gfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200851 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800852
Avi Kivity4db35312007-11-21 15:28:32 +0200853 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +0800854 gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt);
855 rmapp = gfn_to_rmap(kvm, gfn, sp->role.level);
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800856 pte_list_remove(spte, rmapp);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800857}
858
Xiao Guangrongc3707952011-07-12 03:28:04 +0800859static void drop_spte(struct kvm *kvm, u64 *sptep)
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800860{
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800861 if (mmu_spte_clear_track_bits(sptep))
Marcelo Tosattieb45fda2010-10-25 11:58:22 -0200862 rmap_remove(kvm, sptep);
Avi Kivitybe38d272010-06-06 14:31:27 +0300863}
864
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200865static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200866{
Izik Eidus290fc382007-09-27 14:11:22 +0200867 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800868 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200869 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800870
Joerg Roedel44ad9942009-07-27 16:30:42 +0200871 rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
Avi Kivity374cbac2007-01-05 16:36:43 -0800872
Izik Eidus98348e92007-10-16 14:42:30 +0200873 spte = rmap_next(kvm, rmapp, NULL);
874 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800875 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800876 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800877 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900878 if (is_writable_pte(*spte)) {
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800879 mmu_spte_update(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800880 write_protected = 1;
881 }
Izik Eidus9647c142007-10-16 14:43:46 +0200882 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800883 }
Izik Eidus855149a2008-03-20 18:17:24 +0200884
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300885 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200886 for (i = PT_DIRECTORY_LEVEL;
887 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
888 rmapp = gfn_to_rmap(kvm, gfn, i);
889 spte = rmap_next(kvm, rmapp, NULL);
890 while (spte) {
891 BUG_ON(!spte);
892 BUG_ON(!(*spte & PT_PRESENT_MASK));
893 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
894 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +0900895 if (is_writable_pte(*spte)) {
Xiao Guangrongc3707952011-07-12 03:28:04 +0800896 drop_spte(kvm, spte);
Joerg Roedel44ad9942009-07-27 16:30:42 +0200897 --kvm->stat.lpages;
Joerg Roedel44ad9942009-07-27 16:30:42 +0200898 spte = NULL;
899 write_protected = 1;
900 }
901 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300902 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300903 }
904
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200905 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800906}
907
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000908static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
909 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200910{
911 u64 *spte;
912 int need_tlb_flush = 0;
913
914 while ((spte = rmap_next(kvm, rmapp, NULL))) {
915 BUG_ON(!(*spte & PT_PRESENT_MASK));
916 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
Xiao Guangrongc3707952011-07-12 03:28:04 +0800917 drop_spte(kvm, spte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200918 need_tlb_flush = 1;
919 }
920 return need_tlb_flush;
921}
922
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000923static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
924 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +0300925{
926 int need_flush = 0;
Xiao Guangronge4b502e2010-07-16 11:28:09 +0800927 u64 *spte, new_spte;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300928 pte_t *ptep = (pte_t *)data;
929 pfn_t new_pfn;
930
931 WARN_ON(pte_huge(*ptep));
932 new_pfn = pte_pfn(*ptep);
933 spte = rmap_next(kvm, rmapp, NULL);
934 while (spte) {
935 BUG_ON(!is_shadow_present_pte(*spte));
936 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
937 need_flush = 1;
938 if (pte_write(*ptep)) {
Xiao Guangrongc3707952011-07-12 03:28:04 +0800939 drop_spte(kvm, spte);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300940 spte = rmap_next(kvm, rmapp, NULL);
941 } else {
942 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
943 new_spte |= (u64)new_pfn << PAGE_SHIFT;
944
945 new_spte &= ~PT_WRITABLE_MASK;
946 new_spte &= ~SPTE_HOST_WRITEABLE;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300947 new_spte &= ~shadow_accessed_mask;
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800948 mmu_spte_clear_track_bits(spte);
949 mmu_spte_set(spte, new_spte);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300950 spte = rmap_next(kvm, rmapp, spte);
951 }
952 }
953 if (need_flush)
954 kvm_flush_remote_tlbs(kvm);
955
956 return 0;
957}
958
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000959static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
960 unsigned long data,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300961 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +0000962 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200963{
Joerg Roedel852e3c12009-07-27 16:30:44 +0200964 int i, j;
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200965 int ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200966 int retval = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200967 struct kvm_memslots *slots;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200968
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800969 slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200970
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200971 for (i = 0; i < slots->nmemslots; i++) {
972 struct kvm_memory_slot *memslot = &slots->memslots[i];
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200973 unsigned long start = memslot->userspace_addr;
974 unsigned long end;
975
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200976 end = start + (memslot->npages << PAGE_SHIFT);
977 if (hva >= start && hva < end) {
978 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900979 gfn_t gfn = memslot->base_gfn + gfn_offset;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200980
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200981 ret = handler(kvm, &memslot->rmap[gfn_offset], data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200982
983 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900984 struct kvm_lpage_info *linfo;
Andrea Arcangeli6e3e2432010-07-16 11:52:55 +0200985
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900986 linfo = lpage_info_slot(gfn, memslot,
987 PT_DIRECTORY_LEVEL + j);
988 ret |= handler(kvm, &linfo->rmap_pde, data);
Joerg Roedel852e3c12009-07-27 16:30:44 +0200989 }
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200990 trace_kvm_age_page(hva, memslot, ret);
991 retval |= ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200992 }
993 }
994
995 return retval;
996}
997
998int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
999{
Izik Eidus3da0dd42009-09-23 21:47:18 +03001000 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001001}
1002
Izik Eidus3da0dd42009-09-23 21:47:18 +03001003void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
1004{
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +00001005 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
Izik Eidus3da0dd42009-09-23 21:47:18 +03001006}
1007
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +00001008static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
1009 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001010{
1011 u64 *spte;
1012 int young = 0;
1013
Rik van Riel6316e1c2010-02-03 16:11:03 -05001014 /*
1015 * Emulate the accessed bit for EPT, by checking if this page has
1016 * an EPT mapping, and clearing it if it does. On the next access,
1017 * a new EPT mapping will be established.
1018 * This has some overhead, but not as much as the cost of swapping
1019 * out actively used pages or breaking up actively used hugepages.
1020 */
Sheng Yang534e38b2008-09-08 15:12:30 +08001021 if (!shadow_accessed_mask)
Rik van Riel6316e1c2010-02-03 16:11:03 -05001022 return kvm_unmap_rmapp(kvm, rmapp, data);
Sheng Yang534e38b2008-09-08 15:12:30 +08001023
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001024 spte = rmap_next(kvm, rmapp, NULL);
1025 while (spte) {
1026 int _young;
1027 u64 _spte = *spte;
1028 BUG_ON(!(_spte & PT_PRESENT_MASK));
1029 _young = _spte & PT_ACCESSED_MASK;
1030 if (_young) {
1031 young = 1;
1032 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
1033 }
1034 spte = rmap_next(kvm, rmapp, spte);
1035 }
1036 return young;
1037}
1038
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001039static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
1040 unsigned long data)
1041{
1042 u64 *spte;
1043 int young = 0;
1044
1045 /*
1046 * If there's no access bit in the secondary pte set by the
1047 * hardware it's up to gup-fast/gup to set the access bit in
1048 * the primary pte or in the page structure.
1049 */
1050 if (!shadow_accessed_mask)
1051 goto out;
1052
1053 spte = rmap_next(kvm, rmapp, NULL);
1054 while (spte) {
1055 u64 _spte = *spte;
1056 BUG_ON(!(_spte & PT_PRESENT_MASK));
1057 young = _spte & PT_ACCESSED_MASK;
1058 if (young) {
1059 young = 1;
1060 break;
1061 }
1062 spte = rmap_next(kvm, rmapp, spte);
1063 }
1064out:
1065 return young;
1066}
1067
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001068#define RMAP_RECYCLE_THRESHOLD 1000
1069
Joerg Roedel852e3c12009-07-27 16:30:44 +02001070static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001071{
1072 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001073 struct kvm_mmu_page *sp;
1074
1075 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001076
Joerg Roedel852e3c12009-07-27 16:30:44 +02001077 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001078
Izik Eidus3da0dd42009-09-23 21:47:18 +03001079 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001080 kvm_flush_remote_tlbs(vcpu->kvm);
1081}
1082
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001083int kvm_age_hva(struct kvm *kvm, unsigned long hva)
1084{
Izik Eidus3da0dd42009-09-23 21:47:18 +03001085 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001086}
1087
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001088int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
1089{
1090 return kvm_handle_hva(kvm, hva, 0, kvm_test_age_rmapp);
1091}
1092
Yaozu Dongd6c69ee2007-04-25 14:17:25 +08001093#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +03001094static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001095{
Avi Kivity139bdb22007-01-05 16:36:50 -08001096 u64 *pos;
1097 u64 *end;
1098
Avi Kivity47ad8e62007-05-06 15:50:58 +03001099 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +03001100 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001101 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -08001102 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001103 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -08001104 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001105 return 1;
1106}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +08001107#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108
Dave Hansen45221ab2010-08-19 18:11:37 -07001109/*
1110 * This value is the sum of all of the kvm instances's
1111 * kvm->arch.n_used_mmu_pages values. We need a global,
1112 * aggregate version in order to make the slab shrinker
1113 * faster
1114 */
1115static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, int nr)
1116{
1117 kvm->arch.n_used_mmu_pages += nr;
1118 percpu_counter_add(&kvm_total_used_mmu_pages, nr);
1119}
1120
Xiao Guangrongbd4c86e2011-07-12 03:27:14 +08001121/*
1122 * Remove the sp from shadow page cache, after call it,
1123 * we can not find this sp from the cache, and the shadow
1124 * page table is still valid.
1125 * It should be under the protection of mmu lock.
1126 */
1127static void kvm_mmu_isolate_page(struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -08001128{
Avi Kivity4db35312007-11-21 15:28:32 +02001129 ASSERT(is_empty_shadow_page(sp->spt));
Xiao Guangrong77758342010-06-04 21:53:54 +08001130 hlist_del(&sp->hash_link);
Lai Jiangshan2032a932010-05-26 16:49:59 +08001131 if (!sp->role.direct)
Xiao Guangrong842f22e2011-03-04 19:01:10 +08001132 free_page((unsigned long)sp->gfns);
Xiao Guangrongbd4c86e2011-07-12 03:27:14 +08001133}
1134
1135/*
1136 * Free the shadow page table and the sp, we can do it
1137 * out of the protection of mmu lock.
1138 */
1139static void kvm_mmu_free_page(struct kvm_mmu_page *sp)
1140{
1141 list_del(&sp->link);
1142 free_page((unsigned long)sp->spt);
Xiao Guangronge8ad9a72010-05-13 10:06:02 +08001143 kmem_cache_free(mmu_page_header_cache, sp);
Avi Kivity260746c2007-01-05 16:36:49 -08001144}
1145
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001146static unsigned kvm_page_table_hashfn(gfn_t gfn)
1147{
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001148 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001149}
1150
Xiao Guangrong67052b32011-05-15 23:27:08 +08001151static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
1152 struct kvm_mmu_page *sp, u64 *parent_pte)
1153{
1154 if (!parent_pte)
1155 return;
1156
1157 pte_list_add(vcpu, parent_pte, &sp->parent_ptes);
1158}
1159
1160static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
1161 u64 *parent_pte)
1162{
1163 pte_list_remove(parent_pte, &sp->parent_ptes);
1164}
1165
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001166static void drop_parent_pte(struct kvm_mmu_page *sp,
1167 u64 *parent_pte)
1168{
1169 mmu_page_remove_parent_pte(sp, parent_pte);
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08001170 mmu_spte_clear_no_track(parent_pte);
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001171}
1172
Avi Kivity25c0de22007-01-05 16:36:42 -08001173static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
Lai Jiangshan2032a932010-05-26 16:49:59 +08001174 u64 *parent_pte, int direct)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001175{
Avi Kivity4db35312007-11-21 15:28:32 +02001176 struct kvm_mmu_page *sp;
Xiao Guangrong67052b32011-05-15 23:27:08 +08001177 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache,
1178 sizeof *sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001179 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Lai Jiangshan2032a932010-05-26 16:49:59 +08001180 if (!direct)
1181 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache,
1182 PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +02001183 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001184 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Sheng Yang291f26b2008-10-16 17:30:57 +08001185 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Xiao Guangrong67052b32011-05-15 23:27:08 +08001186 sp->parent_ptes = 0;
1187 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Dave Hansen45221ab2010-08-19 18:11:37 -07001188 kvm_mod_used_mmu_pages(vcpu->kvm, +1);
Avi Kivity4db35312007-11-21 15:28:32 +02001189 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001190}
1191
Xiao Guangrong67052b32011-05-15 23:27:08 +08001192static void mark_unsync(u64 *spte);
Xiao Guangrong6b184932010-04-16 21:29:17 +08001193static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001194{
Xiao Guangrong67052b32011-05-15 23:27:08 +08001195 pte_list_walk(&sp->parent_ptes, mark_unsync);
Xiao Guangrong1047df12010-06-11 21:35:15 +08001196}
1197
Xiao Guangrong67052b32011-05-15 23:27:08 +08001198static void mark_unsync(u64 *spte)
Xiao Guangrong1047df12010-06-11 21:35:15 +08001199{
Xiao Guangrong67052b32011-05-15 23:27:08 +08001200 struct kvm_mmu_page *sp;
Xiao Guangrong1047df12010-06-11 21:35:15 +08001201 unsigned int index;
1202
Xiao Guangrong67052b32011-05-15 23:27:08 +08001203 sp = page_header(__pa(spte));
Xiao Guangrong1047df12010-06-11 21:35:15 +08001204 index = spte - sp->spt;
1205 if (__test_and_set_bit(index, sp->unsync_child_bitmap))
1206 return;
1207 if (sp->unsync_children++)
1208 return;
1209 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001210}
1211
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001212static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001213 struct kvm_mmu_page *sp)
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001214{
1215 return 1;
1216}
1217
Marcelo Tosattia7052892008-09-23 13:18:35 -03001218static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1219{
1220}
1221
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08001222static void nonpaging_update_pte(struct kvm_vcpu *vcpu,
1223 struct kvm_mmu_page *sp, u64 *spte,
Xiao Guangrong7c562522011-03-28 10:29:27 +08001224 const void *pte)
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08001225{
1226 WARN_ON(1);
1227}
1228
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001229#define KVM_PAGE_ARRAY_NR 16
1230
1231struct kvm_mmu_pages {
1232 struct mmu_page_and_offset {
1233 struct kvm_mmu_page *sp;
1234 unsigned int idx;
1235 } page[KVM_PAGE_ARRAY_NR];
1236 unsigned int nr;
1237};
1238
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001239#define for_each_unsync_children(bitmap, idx) \
1240 for (idx = find_first_bit(bitmap, 512); \
1241 idx < 512; \
1242 idx = find_next_bit(bitmap, 512, idx+1))
1243
Hannes Edercded19f2009-02-21 02:19:13 +01001244static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1245 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001246{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001247 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001248
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001249 if (sp->unsync)
1250 for (i=0; i < pvec->nr; i++)
1251 if (pvec->page[i].sp == sp)
1252 return 0;
1253
1254 pvec->page[pvec->nr].sp = sp;
1255 pvec->page[pvec->nr].idx = idx;
1256 pvec->nr++;
1257 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1258}
1259
1260static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1261 struct kvm_mmu_pages *pvec)
1262{
1263 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001264
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001265 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001266 struct kvm_mmu_page *child;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001267 u64 ent = sp->spt[i];
1268
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001269 if (!is_shadow_present_pte(ent) || is_large_pte(ent))
1270 goto clear_child_bitmap;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001271
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001272 child = page_header(ent & PT64_BASE_ADDR_MASK);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001273
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001274 if (child->unsync_children) {
1275 if (mmu_pages_add(pvec, child, i))
1276 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001277
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001278 ret = __mmu_unsync_walk(child, pvec);
1279 if (!ret)
1280 goto clear_child_bitmap;
1281 else if (ret > 0)
1282 nr_unsync_leaf += ret;
1283 else
1284 return ret;
1285 } else if (child->unsync) {
1286 nr_unsync_leaf++;
1287 if (mmu_pages_add(pvec, child, i))
1288 return -ENOSPC;
1289 } else
1290 goto clear_child_bitmap;
1291
1292 continue;
1293
1294clear_child_bitmap:
1295 __clear_bit(i, sp->unsync_child_bitmap);
1296 sp->unsync_children--;
1297 WARN_ON((int)sp->unsync_children < 0);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001298 }
1299
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001300
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001301 return nr_unsync_leaf;
1302}
1303
1304static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1305 struct kvm_mmu_pages *pvec)
1306{
1307 if (!sp->unsync_children)
1308 return 0;
1309
1310 mmu_pages_add(pvec, sp, 0);
1311 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001312}
1313
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001314static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1315{
1316 WARN_ON(!sp->unsync);
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001317 trace_kvm_mmu_sync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001318 sp->unsync = 0;
1319 --kvm->stat.mmu_unsync;
1320}
1321
Xiao Guangrong77758342010-06-04 21:53:54 +08001322static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1323 struct list_head *invalid_list);
1324static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1325 struct list_head *invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001326
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001327#define for_each_gfn_sp(kvm, sp, gfn, pos) \
1328 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001329 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1330 if ((sp)->gfn != (gfn)) {} else
1331
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001332#define for_each_gfn_indirect_valid_sp(kvm, sp, gfn, pos) \
1333 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001334 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1335 if ((sp)->gfn != (gfn) || (sp)->role.direct || \
1336 (sp)->role.invalid) {} else
1337
Xiao Guangrongf918b442010-06-11 21:30:36 +08001338/* @sp->gfn should be write-protected at the call site */
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001339static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001340 struct list_head *invalid_list, bool clear_unsync)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001341{
Avi Kivity5b7e0102010-04-14 19:20:03 +03001342 if (sp->role.cr4_pae != !!is_pae(vcpu)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001343 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001344 return 1;
1345 }
1346
Xiao Guangrongf918b442010-06-11 21:30:36 +08001347 if (clear_unsync)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001348 kvm_unlink_unsync_page(vcpu->kvm, sp);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001349
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001350 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001351 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001352 return 1;
1353 }
1354
1355 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001356 return 0;
1357}
1358
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001359static int kvm_sync_page_transient(struct kvm_vcpu *vcpu,
1360 struct kvm_mmu_page *sp)
1361{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001362 LIST_HEAD(invalid_list);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001363 int ret;
1364
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001365 ret = __kvm_sync_page(vcpu, sp, &invalid_list, false);
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001366 if (ret)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001367 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
1368
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001369 return ret;
1370}
1371
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001372static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1373 struct list_head *invalid_list)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001374{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001375 return __kvm_sync_page(vcpu, sp, invalid_list, true);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001376}
1377
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001378/* @gfn should be write-protected at the call site */
1379static void kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1380{
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001381 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001382 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001383 LIST_HEAD(invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001384 bool flush = false;
1385
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001386 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001387 if (!s->unsync)
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001388 continue;
1389
1390 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001391 kvm_unlink_unsync_page(vcpu->kvm, s);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001392 if ((s->role.cr4_pae != !!is_pae(vcpu)) ||
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001393 (vcpu->arch.mmu.sync_page(vcpu, s))) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001394 kvm_mmu_prepare_zap_page(vcpu->kvm, s, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001395 continue;
1396 }
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001397 flush = true;
1398 }
1399
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001400 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001401 if (flush)
1402 kvm_mmu_flush_tlb(vcpu);
1403}
1404
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001405struct mmu_page_path {
1406 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1407 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001408};
1409
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001410#define for_each_sp(pvec, sp, parents, i) \
1411 for (i = mmu_pages_next(&pvec, &parents, -1), \
1412 sp = pvec.page[i].sp; \
1413 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1414 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001415
Hannes Edercded19f2009-02-21 02:19:13 +01001416static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1417 struct mmu_page_path *parents,
1418 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001419{
1420 int n;
1421
1422 for (n = i+1; n < pvec->nr; n++) {
1423 struct kvm_mmu_page *sp = pvec->page[n].sp;
1424
1425 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1426 parents->idx[0] = pvec->page[n].idx;
1427 return n;
1428 }
1429
1430 parents->parent[sp->role.level-2] = sp;
1431 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1432 }
1433
1434 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001435}
1436
Hannes Edercded19f2009-02-21 02:19:13 +01001437static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001438{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001439 struct kvm_mmu_page *sp;
1440 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001441
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001442 do {
1443 unsigned int idx = parents->idx[level];
1444
1445 sp = parents->parent[level];
1446 if (!sp)
1447 return;
1448
1449 --sp->unsync_children;
1450 WARN_ON((int)sp->unsync_children < 0);
1451 __clear_bit(idx, sp->unsync_child_bitmap);
1452 level++;
1453 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1454}
1455
1456static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1457 struct mmu_page_path *parents,
1458 struct kvm_mmu_pages *pvec)
1459{
1460 parents->parent[parent->role.level-1] = NULL;
1461 pvec->nr = 0;
1462}
1463
1464static void mmu_sync_children(struct kvm_vcpu *vcpu,
1465 struct kvm_mmu_page *parent)
1466{
1467 int i;
1468 struct kvm_mmu_page *sp;
1469 struct mmu_page_path parents;
1470 struct kvm_mmu_pages pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001471 LIST_HEAD(invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001472
1473 kvm_mmu_pages_init(parent, &parents, &pages);
1474 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001475 int protected = 0;
1476
1477 for_each_sp(pages, sp, parents, i)
1478 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1479
1480 if (protected)
1481 kvm_flush_remote_tlbs(vcpu->kvm);
1482
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001483 for_each_sp(pages, sp, parents, i) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001484 kvm_sync_page(vcpu, sp, &invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001485 mmu_pages_clear_parents(&parents);
1486 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001487 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001488 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001489 kvm_mmu_pages_init(parent, &parents, &pages);
1490 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001491}
1492
Xiao Guangrongc3707952011-07-12 03:28:04 +08001493static void init_shadow_page_table(struct kvm_mmu_page *sp)
1494{
1495 int i;
1496
1497 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1498 sp->spt[i] = 0ull;
1499}
1500
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001501static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1502 gfn_t gfn,
1503 gva_t gaddr,
1504 unsigned level,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001505 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001506 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001507 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001508{
1509 union kvm_mmu_page_role role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001510 unsigned quadrant;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001511 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001512 struct hlist_node *node;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001513 bool need_sync = false;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001514
Avi Kivitya770f6f2008-12-21 19:20:09 +02001515 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001516 role.level = level;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001517 role.direct = direct;
Avi Kivity84b0c8c2010-03-14 10:16:40 +02001518 if (role.direct)
Avi Kivity5b7e0102010-04-14 19:20:03 +03001519 role.cr4_pae = 0;
Avi Kivity41074d02007-12-09 17:00:02 +02001520 role.access = access;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02001521 if (!vcpu->arch.mmu.direct_map
1522 && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001523 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1524 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1525 role.quadrant = quadrant;
1526 }
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001527 for_each_gfn_sp(vcpu->kvm, sp, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001528 if (!need_sync && sp->unsync)
1529 need_sync = true;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001530
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001531 if (sp->role.word != role.word)
1532 continue;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001533
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001534 if (sp->unsync && kvm_sync_page_transient(vcpu, sp))
1535 break;
Xiao Guangronge02aa902010-05-15 18:52:34 +08001536
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001537 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1538 if (sp->unsync_children) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001539 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001540 kvm_mmu_mark_parents_unsync(sp);
1541 } else if (sp->unsync)
1542 kvm_mmu_mark_parents_unsync(sp);
Xiao Guangronge02aa902010-05-15 18:52:34 +08001543
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001544 trace_kvm_mmu_get_page(sp, false);
1545 return sp;
1546 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001547 ++vcpu->kvm->stat.mmu_cache_miss;
Lai Jiangshan2032a932010-05-26 16:49:59 +08001548 sp = kvm_mmu_alloc_page(vcpu, parent_pte, direct);
Avi Kivity4db35312007-11-21 15:28:32 +02001549 if (!sp)
1550 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001551 sp->gfn = gfn;
1552 sp->role = role;
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001553 hlist_add_head(&sp->hash_link,
1554 &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001555 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001556 if (rmap_write_protect(vcpu->kvm, gfn))
1557 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001558 if (level > PT_PAGE_TABLE_LEVEL && need_sync)
1559 kvm_sync_pages(vcpu, gfn);
1560
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001561 account_shadowed(vcpu->kvm, gfn);
1562 }
Xiao Guangrongc3707952011-07-12 03:28:04 +08001563 init_shadow_page_table(sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001564 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001565 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001566}
1567
Avi Kivity2d111232008-12-25 14:39:47 +02001568static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1569 struct kvm_vcpu *vcpu, u64 addr)
1570{
1571 iterator->addr = addr;
1572 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1573 iterator->level = vcpu->arch.mmu.shadow_root_level;
Joerg Roedel81407ca2010-09-10 17:31:00 +02001574
1575 if (iterator->level == PT64_ROOT_LEVEL &&
1576 vcpu->arch.mmu.root_level < PT64_ROOT_LEVEL &&
1577 !vcpu->arch.mmu.direct_map)
1578 --iterator->level;
1579
Avi Kivity2d111232008-12-25 14:39:47 +02001580 if (iterator->level == PT32E_ROOT_LEVEL) {
1581 iterator->shadow_addr
1582 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1583 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1584 --iterator->level;
1585 if (!iterator->shadow_addr)
1586 iterator->level = 0;
1587 }
1588}
1589
1590static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1591{
1592 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1593 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001594
Avi Kivity2d111232008-12-25 14:39:47 +02001595 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1596 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1597 return true;
1598}
1599
1600static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1601{
Xiao Guangrong052331b2011-07-12 03:21:17 +08001602 if (is_last_spte(*iterator->sptep, iterator->level)) {
1603 iterator->level = 0;
1604 return;
1605 }
1606
Avi Kivity2d111232008-12-25 14:39:47 +02001607 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1608 --iterator->level;
1609}
1610
Avi Kivity32ef26a2010-07-13 14:27:04 +03001611static void link_shadow_page(u64 *sptep, struct kvm_mmu_page *sp)
1612{
1613 u64 spte;
1614
1615 spte = __pa(sp->spt)
1616 | PT_PRESENT_MASK | PT_ACCESSED_MASK
1617 | PT_WRITABLE_MASK | PT_USER_MASK;
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08001618 mmu_spte_set(sptep, spte);
Avi Kivity32ef26a2010-07-13 14:27:04 +03001619}
1620
Avi Kivitya3aa51c2010-07-13 14:27:06 +03001621static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep)
1622{
1623 if (is_large_pte(*sptep)) {
Xiao Guangrongc3707952011-07-12 03:28:04 +08001624 drop_spte(vcpu->kvm, sptep);
Avi Kivitya3aa51c2010-07-13 14:27:06 +03001625 kvm_flush_remote_tlbs(vcpu->kvm);
1626 }
1627}
1628
Avi Kivitya357bd22010-07-13 14:27:07 +03001629static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1630 unsigned direct_access)
1631{
1632 if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
1633 struct kvm_mmu_page *child;
1634
1635 /*
1636 * For the direct sp, if the guest pte's dirty bit
1637 * changed form clean to dirty, it will corrupt the
1638 * sp's access: allow writable in the read-only sp,
1639 * so we should update the spte at this point to get
1640 * a new sp with the correct access.
1641 */
1642 child = page_header(*sptep & PT64_BASE_ADDR_MASK);
1643 if (child->role.access == direct_access)
1644 return;
1645
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001646 drop_parent_pte(child, sptep);
Avi Kivitya357bd22010-07-13 14:27:07 +03001647 kvm_flush_remote_tlbs(vcpu->kvm);
1648 }
1649}
1650
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08001651static void mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,
1652 u64 *spte)
1653{
1654 u64 pte;
1655 struct kvm_mmu_page *child;
1656
1657 pte = *spte;
1658 if (is_shadow_present_pte(pte)) {
1659 if (is_last_spte(pte, sp->role.level))
Xiao Guangrongc3707952011-07-12 03:28:04 +08001660 drop_spte(kvm, spte);
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08001661 else {
1662 child = page_header(pte & PT64_BASE_ADDR_MASK);
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001663 drop_parent_pte(child, spte);
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08001664 }
1665 }
Xiao Guangrongc3707952011-07-12 03:28:04 +08001666
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08001667 if (is_large_pte(pte))
1668 --kvm->stat.lpages;
1669}
1670
Avi Kivity90cb0522007-07-17 13:04:56 +03001671static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001672 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001673{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001674 unsigned i;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001675
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08001676 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1677 mmu_page_zap_pte(kvm, sp, sp->spt + i);
Avi Kivitya4360362007-01-05 16:36:45 -08001678}
1679
Avi Kivity4db35312007-11-21 15:28:32 +02001680static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001681{
Avi Kivity4db35312007-11-21 15:28:32 +02001682 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001683}
1684
Avi Kivity12b7d282007-09-23 14:10:49 +02001685static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1686{
1687 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001688 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001689
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001690 kvm_for_each_vcpu(i, vcpu, kvm)
1691 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001692}
1693
Avi Kivity31aa2b42008-07-11 17:59:46 +03001694static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001695{
1696 u64 *parent_pte;
1697
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001698 while ((parent_pte = pte_list_next(&sp->parent_ptes, NULL)))
1699 drop_parent_pte(sp, parent_pte);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001700}
1701
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001702static int mmu_zap_unsync_children(struct kvm *kvm,
Xiao Guangrong77758342010-06-04 21:53:54 +08001703 struct kvm_mmu_page *parent,
1704 struct list_head *invalid_list)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001705{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001706 int i, zapped = 0;
1707 struct mmu_page_path parents;
1708 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001709
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001710 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001711 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001712
1713 kvm_mmu_pages_init(parent, &parents, &pages);
1714 while (mmu_unsync_walk(parent, &pages)) {
1715 struct kvm_mmu_page *sp;
1716
1717 for_each_sp(pages, sp, parents, i) {
Xiao Guangrong77758342010-06-04 21:53:54 +08001718 kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001719 mmu_pages_clear_parents(&parents);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001720 zapped++;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001721 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001722 kvm_mmu_pages_init(parent, &parents, &pages);
1723 }
1724
1725 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001726}
1727
Xiao Guangrong77758342010-06-04 21:53:54 +08001728static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1729 struct list_head *invalid_list)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001730{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001731 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001732
Xiao Guangrong77758342010-06-04 21:53:54 +08001733 trace_kvm_mmu_prepare_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001734 ++kvm->stat.mmu_shadow_zapped;
Xiao Guangrong77758342010-06-04 21:53:54 +08001735 ret = mmu_zap_unsync_children(kvm, sp, invalid_list);
Avi Kivity4db35312007-11-21 15:28:32 +02001736 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001737 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001738 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001739 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001740 if (sp->unsync)
1741 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001742 if (!sp->root_count) {
Gui Jianfeng54a4f022010-05-05 09:03:49 +08001743 /* Count self */
1744 ret++;
Xiao Guangrong77758342010-06-04 21:53:54 +08001745 list_move(&sp->link, invalid_list);
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08001746 kvm_mod_used_mmu_pages(kvm, -1);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001747 } else {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001748 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001749 kvm_reload_remote_mmus(kvm);
1750 }
Xiao Guangrong77758342010-06-04 21:53:54 +08001751
1752 sp->role.invalid = 1;
Avi Kivity12b7d282007-09-23 14:10:49 +02001753 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001754 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001755}
1756
Xiao Guangrong77758342010-06-04 21:53:54 +08001757static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1758 struct list_head *invalid_list)
1759{
1760 struct kvm_mmu_page *sp;
1761
1762 if (list_empty(invalid_list))
1763 return;
1764
1765 kvm_flush_remote_tlbs(kvm);
1766
1767 do {
1768 sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
1769 WARN_ON(!sp->role.invalid || sp->root_count);
Xiao Guangrongbd4c86e2011-07-12 03:27:14 +08001770 kvm_mmu_isolate_page(sp);
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08001771 kvm_mmu_free_page(sp);
Xiao Guangrong77758342010-06-04 21:53:54 +08001772 } while (!list_empty(invalid_list));
1773
1774}
1775
Izik Eidus82ce2c92007-10-02 18:52:55 +02001776/*
1777 * Changing the number of mmu pages allocated to the vm
Dave Hansen49d5ca22010-08-19 18:11:28 -07001778 * Note: if goal_nr_mmu_pages is too small, you will get dead lock
Izik Eidus82ce2c92007-10-02 18:52:55 +02001779 */
Dave Hansen49d5ca22010-08-19 18:11:28 -07001780void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages)
Izik Eidus82ce2c92007-10-02 18:52:55 +02001781{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001782 LIST_HEAD(invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001783 /*
1784 * If we set the number of mmu pages to be smaller be than the
1785 * number of actived pages , we must to free some mmu pages before we
1786 * change the value
1787 */
1788
Dave Hansen49d5ca22010-08-19 18:11:28 -07001789 if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) {
1790 while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages &&
Xiao Guangrong77662e02010-04-16 16:34:42 +08001791 !list_empty(&kvm->arch.active_mmu_pages)) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001792 struct kvm_mmu_page *page;
1793
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001794 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001795 struct kvm_mmu_page, link);
Xiaotian Feng80b63fa2010-08-24 10:31:07 +08001796 kvm_mmu_prepare_zap_page(kvm, page, &invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02001797 }
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08001798 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Dave Hansen49d5ca22010-08-19 18:11:28 -07001799 goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001800 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02001801
Dave Hansen49d5ca22010-08-19 18:11:28 -07001802 kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001803}
1804
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001805static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001806{
Avi Kivity4db35312007-11-21 15:28:32 +02001807 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001808 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001809 LIST_HEAD(invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001810 int r;
1811
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08001812 pgprintk("%s: looking for gfn %llx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001813 r = 0;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001814
1815 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08001816 pgprintk("%s: gfn %llx role %x\n", __func__, gfn,
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001817 sp->role.word);
1818 r = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001819 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001820 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001821 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08001822 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001823}
1824
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001825static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001826{
Avi Kivity4db35312007-11-21 15:28:32 +02001827 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001828 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001829 LIST_HEAD(invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001830
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001831 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08001832 pgprintk("%s: zap %llx %x\n",
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001833 __func__, gfn, sp->role.word);
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001834 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001835 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001836 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Avi Kivity97a0a012007-05-31 15:08:29 +03001837}
1838
Avi Kivity38c335f2007-11-21 14:20:22 +02001839static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001840{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001841 int slot = memslot_id(kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001842 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001843
Sheng Yang291f26b2008-10-16 17:30:57 +08001844 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001845}
1846
Sheng Yang74be52e2008-10-09 16:01:56 +08001847/*
1848 * The function is based on mtrr_type_lookup() in
1849 * arch/x86/kernel/cpu/mtrr/generic.c
1850 */
1851static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1852 u64 start, u64 end)
1853{
1854 int i;
1855 u64 base, mask;
1856 u8 prev_match, curr_match;
1857 int num_var_ranges = KVM_NR_VAR_MTRR;
1858
1859 if (!mtrr_state->enabled)
1860 return 0xFF;
1861
1862 /* Make end inclusive end, instead of exclusive */
1863 end--;
1864
1865 /* Look in fixed ranges. Just return the type as per start */
1866 if (mtrr_state->have_fixed && (start < 0x100000)) {
1867 int idx;
1868
1869 if (start < 0x80000) {
1870 idx = 0;
1871 idx += (start >> 16);
1872 return mtrr_state->fixed_ranges[idx];
1873 } else if (start < 0xC0000) {
1874 idx = 1 * 8;
1875 idx += ((start - 0x80000) >> 14);
1876 return mtrr_state->fixed_ranges[idx];
1877 } else if (start < 0x1000000) {
1878 idx = 3 * 8;
1879 idx += ((start - 0xC0000) >> 12);
1880 return mtrr_state->fixed_ranges[idx];
1881 }
1882 }
1883
1884 /*
1885 * Look in variable ranges
1886 * Look of multiple ranges matching this address and pick type
1887 * as per MTRR precedence
1888 */
1889 if (!(mtrr_state->enabled & 2))
1890 return mtrr_state->def_type;
1891
1892 prev_match = 0xFF;
1893 for (i = 0; i < num_var_ranges; ++i) {
1894 unsigned short start_state, end_state;
1895
1896 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1897 continue;
1898
1899 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1900 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1901 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1902 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1903
1904 start_state = ((start & mask) == (base & mask));
1905 end_state = ((end & mask) == (base & mask));
1906 if (start_state != end_state)
1907 return 0xFE;
1908
1909 if ((start & mask) != (base & mask))
1910 continue;
1911
1912 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1913 if (prev_match == 0xFF) {
1914 prev_match = curr_match;
1915 continue;
1916 }
1917
1918 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1919 curr_match == MTRR_TYPE_UNCACHABLE)
1920 return MTRR_TYPE_UNCACHABLE;
1921
1922 if ((prev_match == MTRR_TYPE_WRBACK &&
1923 curr_match == MTRR_TYPE_WRTHROUGH) ||
1924 (prev_match == MTRR_TYPE_WRTHROUGH &&
1925 curr_match == MTRR_TYPE_WRBACK)) {
1926 prev_match = MTRR_TYPE_WRTHROUGH;
1927 curr_match = MTRR_TYPE_WRTHROUGH;
1928 }
1929
1930 if (prev_match != curr_match)
1931 return MTRR_TYPE_UNCACHABLE;
1932 }
1933
1934 if (prev_match != 0xFF)
1935 return prev_match;
1936
1937 return mtrr_state->def_type;
1938}
1939
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001940u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001941{
1942 u8 mtrr;
1943
1944 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1945 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1946 if (mtrr == 0xfe || mtrr == 0xff)
1947 mtrr = MTRR_TYPE_WRBACK;
1948 return mtrr;
1949}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001950EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001951
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001952static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001953{
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001954 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001955 ++vcpu->kvm->stat.mmu_unsync;
1956 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001957
Xiao Guangrong6b184932010-04-16 21:29:17 +08001958 kvm_mmu_mark_parents_unsync(sp);
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001959}
1960
1961static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1962{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001963 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001964 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001965
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001966 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001967 if (s->unsync)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001968 continue;
1969 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
1970 __kvm_unsync_page(vcpu, s);
1971 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001972}
1973
1974static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1975 bool can_unsync)
1976{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001977 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001978 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001979 bool need_unsync = false;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001980
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001981 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001982 if (!can_unsync)
1983 return 1;
1984
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001985 if (s->role.level != PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001986 return 1;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001987
1988 if (!need_unsync && !s->unsync) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08001989 if (!oos_shadow)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001990 return 1;
1991 need_unsync = true;
1992 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001993 }
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08001994 if (need_unsync)
1995 kvm_unsync_pages(vcpu, gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001996 return 0;
1997}
1998
Avi Kivityd555c332009-06-10 14:24:23 +03001999static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002000 unsigned pte_access, int user_fault,
Xiao Guangrong640d9b02011-07-12 03:24:39 +08002001 int write_fault, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03002002 gfn_t gfn, pfn_t pfn, bool speculative,
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08002003 bool can_unsync, bool host_writable)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002004{
Xiao Guangrongb330aa02010-11-19 17:02:35 +08002005 u64 spte, entry = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002006 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08002007
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002008 /*
2009 * We don't set the accessed bit, since we sometimes want to see
2010 * whether the guest actually used the pte (in order to detect
2011 * demand paging).
2012 */
Marcelo Tosatti982c2562010-10-22 14:18:16 -02002013 spte = PT_PRESENT_MASK;
Avi Kivity947da532008-03-18 11:05:52 +02002014 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03002015 spte |= shadow_accessed_mask;
Xiao Guangrong640d9b02011-07-12 03:24:39 +08002016
Sheng Yang7b523452008-04-25 21:13:50 +08002017 if (pte_access & ACC_EXEC_MASK)
2018 spte |= shadow_x_mask;
2019 else
2020 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002021 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08002022 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002023 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002024 spte |= PT_PAGE_SIZE_MASK;
Avi Kivityb0bc3ee2010-09-13 16:45:28 +02002025 if (tdp_enabled)
Sheng Yang4b12f0d2009-04-27 20:35:42 +08002026 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
2027 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002028
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08002029 if (host_writable)
Izik Eidus14032832009-09-23 21:47:17 +03002030 spte |= SPTE_HOST_WRITEABLE;
Xiao Guangrongf8e453b2010-12-23 16:09:29 +08002031 else
2032 pte_access &= ~ACC_WRITE_MASK;
Izik Eidus14032832009-09-23 21:47:17 +03002033
Anthony Liguori35149e22008-04-02 14:46:56 -05002034 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002035
2036 if ((pte_access & ACC_WRITE_MASK)
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002037 || (!vcpu->arch.mmu.direct_map && write_fault
2038 && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002039
Joerg Roedel852e3c12009-07-27 16:30:44 +02002040 if (level > PT_PAGE_TABLE_LEVEL &&
2041 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002042 ret = 1;
Xiao Guangrongc3707952011-07-12 03:28:04 +08002043 drop_spte(vcpu->kvm, sptep);
Avi Kivitybe38d272010-06-06 14:31:27 +03002044 goto done;
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002045 }
2046
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002047 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002048
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002049 if (!vcpu->arch.mmu.direct_map
Avi Kivity411c5882011-06-06 16:11:54 +03002050 && !(pte_access & ACC_WRITE_MASK)) {
Avi Kivity69325a12010-05-27 14:35:58 +03002051 spte &= ~PT_USER_MASK;
Avi Kivity411c5882011-06-06 16:11:54 +03002052 /*
2053 * If we converted a user page to a kernel page,
2054 * so that the kernel can write to it when cr0.wp=0,
2055 * then we should prevent the kernel from executing it
2056 * if SMEP is enabled.
2057 */
2058 if (kvm_read_cr4_bits(vcpu, X86_CR4_SMEP))
2059 spte |= PT64_NX_MASK;
2060 }
Avi Kivity69325a12010-05-27 14:35:58 +03002061
Marcelo Tosattiecc55892008-11-25 15:58:07 +01002062 /*
2063 * Optimization: for pte sync, if spte was writable the hash
2064 * lookup is unnecessary (and expensive). Write protection
2065 * is responsibility of mmu_get_page / kvm_sync_page.
2066 * Same reasoning can be applied to dirty page accounting.
2067 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002068 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01002069 goto set_pte;
2070
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002071 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002072 pgprintk("%s: found shadow page for %llx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002073 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002074 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002075 pte_access &= ~ACC_WRITE_MASK;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002076 if (is_writable_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002077 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002078 }
2079 }
2080
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002081 if (pte_access & ACC_WRITE_MASK)
2082 mark_page_dirty(vcpu->kvm, gfn);
2083
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002084set_pte:
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08002085 mmu_spte_update(sptep, spte);
Xiao Guangrongb330aa02010-11-19 17:02:35 +08002086 /*
2087 * If we overwrite a writable spte with a read-only one we
2088 * should flush remote TLBs. Otherwise rmap_write_protect
2089 * will find a read-only spte, even though the writable spte
2090 * might be cached on a CPU's TLB.
2091 */
2092 if (is_writable_pte(entry) && !is_writable_pte(*sptep))
2093 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivitybe38d272010-06-06 14:31:27 +03002094done:
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002095 return ret;
2096}
2097
Avi Kivityd555c332009-06-10 14:24:23 +03002098static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002099 unsigned pt_access, unsigned pte_access,
Xiao Guangrong640d9b02011-07-12 03:24:39 +08002100 int user_fault, int write_fault,
Xiao Guangrongb90a0e62011-07-12 03:25:56 +08002101 int *emulate, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03002102 pfn_t pfn, bool speculative,
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08002103 bool host_writable)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002104{
2105 int was_rmapped = 0;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002106 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002107
2108 pgprintk("%s: spte %llx access %x write_fault %d"
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002109 " user_fault %d gfn %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002110 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002111 write_fault, user_fault, gfn);
2112
Avi Kivityd555c332009-06-10 14:24:23 +03002113 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002114 /*
2115 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
2116 * the parent of the now unreachable PTE.
2117 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02002118 if (level > PT_PAGE_TABLE_LEVEL &&
2119 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002120 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03002121 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002122
2123 child = page_header(pte & PT64_BASE_ADDR_MASK);
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08002124 drop_parent_pte(child, sptep);
Marcelo Tosatti3be22642010-05-28 09:44:59 -03002125 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityd555c332009-06-10 14:24:23 +03002126 } else if (pfn != spte_to_pfn(*sptep)) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002127 pgprintk("hfn old %llx new %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002128 spte_to_pfn(*sptep), pfn);
Xiao Guangrongc3707952011-07-12 03:28:04 +08002129 drop_spte(vcpu->kvm, sptep);
Xiao Guangrong91546352010-06-30 16:04:06 +08002130 kvm_flush_remote_tlbs(vcpu->kvm);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01002131 } else
2132 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002133 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02002134
Avi Kivityd555c332009-06-10 14:24:23 +03002135 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Xiao Guangrong640d9b02011-07-12 03:24:39 +08002136 level, gfn, pfn, speculative, true,
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08002137 host_writable)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002138 if (write_fault)
Xiao Guangrongb90a0e62011-07-12 03:25:56 +08002139 *emulate = 1;
Xiao Guangrong5304efd2010-06-08 20:05:57 +08002140 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03002141 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002142
Avi Kivityd555c332009-06-10 14:24:23 +03002143 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002144 pgprintk("instantiating %s PTE (%s) at %llx (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002145 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02002146 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
2147 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03002148 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002149 ++vcpu->kvm->stat.lpages;
2150
Xiao Guangrongffb61bb2011-07-12 03:22:01 +08002151 if (is_shadow_present_pte(*sptep)) {
2152 page_header_update_slot(vcpu->kvm, sptep, gfn);
2153 if (!was_rmapped) {
2154 rmap_count = rmap_add(vcpu, sptep, gfn);
2155 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
2156 rmap_recycle(vcpu, sptep, gfn);
2157 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002158 }
Xiao Guangrong9ed55202010-07-16 11:25:17 +08002159 kvm_release_pfn_clean(pfn);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002160 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03002161 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002162 vcpu->arch.last_pte_gfn = gfn;
2163 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002164}
2165
Avi Kivity6aa8b732006-12-10 02:21:36 -08002166static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
2167{
2168}
2169
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002170static pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn,
2171 bool no_dirty_log)
2172{
2173 struct kvm_memory_slot *slot;
2174 unsigned long hva;
2175
Xiao Guangrong5d163b12011-03-09 15:43:00 +08002176 slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, no_dirty_log);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002177 if (!slot) {
Xiao Guangrongfce92dc2011-07-12 03:28:54 +08002178 get_page(fault_page);
2179 return page_to_pfn(fault_page);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002180 }
2181
2182 hva = gfn_to_hva_memslot(slot, gfn);
2183
2184 return hva_to_pfn_atomic(vcpu->kvm, hva);
2185}
2186
2187static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,
2188 struct kvm_mmu_page *sp,
2189 u64 *start, u64 *end)
2190{
2191 struct page *pages[PTE_PREFETCH_NUM];
2192 unsigned access = sp->role.access;
2193 int i, ret;
2194 gfn_t gfn;
2195
2196 gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt);
Xiao Guangrong5d163b12011-03-09 15:43:00 +08002197 if (!gfn_to_memslot_dirty_bitmap(vcpu, gfn, access & ACC_WRITE_MASK))
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002198 return -1;
2199
2200 ret = gfn_to_page_many_atomic(vcpu->kvm, gfn, pages, end - start);
2201 if (ret <= 0)
2202 return -1;
2203
2204 for (i = 0; i < ret; i++, gfn++, start++)
2205 mmu_set_spte(vcpu, start, ACC_ALL,
Xiao Guangrong640d9b02011-07-12 03:24:39 +08002206 access, 0, 0, NULL,
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002207 sp->role.level, gfn,
2208 page_to_pfn(pages[i]), true, true);
2209
2210 return 0;
2211}
2212
2213static void __direct_pte_prefetch(struct kvm_vcpu *vcpu,
2214 struct kvm_mmu_page *sp, u64 *sptep)
2215{
2216 u64 *spte, *start = NULL;
2217 int i;
2218
2219 WARN_ON(!sp->role.direct);
2220
2221 i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
2222 spte = sp->spt + i;
2223
2224 for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
Xiao Guangrongc3707952011-07-12 03:28:04 +08002225 if (is_shadow_present_pte(*spte) || spte == sptep) {
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002226 if (!start)
2227 continue;
2228 if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0)
2229 break;
2230 start = NULL;
2231 } else if (!start)
2232 start = spte;
2233 }
2234}
2235
2236static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)
2237{
2238 struct kvm_mmu_page *sp;
2239
2240 /*
2241 * Since it's no accessed bit on EPT, it's no way to
2242 * distinguish between actually accessed translations
2243 * and prefetched, so disable pte prefetch if EPT is
2244 * enabled.
2245 */
2246 if (!shadow_accessed_mask)
2247 return;
2248
2249 sp = page_header(__pa(sptep));
2250 if (sp->role.level > PT_PAGE_TABLE_LEVEL)
2251 return;
2252
2253 __direct_pte_prefetch(vcpu, sp, sptep);
2254}
2255
Joerg Roedel4d9976b2008-02-07 13:47:42 +01002256static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002257 int map_writable, int level, gfn_t gfn, pfn_t pfn,
2258 bool prefault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002259{
Avi Kivity9f652d212008-12-25 14:54:25 +02002260 struct kvm_shadow_walk_iterator iterator;
2261 struct kvm_mmu_page *sp;
Xiao Guangrongb90a0e62011-07-12 03:25:56 +08002262 int emulate = 0;
Avi Kivity9f652d212008-12-25 14:54:25 +02002263 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002264
Avi Kivity9f652d212008-12-25 14:54:25 +02002265 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02002266 if (iterator.level == level) {
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002267 unsigned pte_access = ACC_ALL;
2268
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002269 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, pte_access,
Xiao Guangrongb90a0e62011-07-12 03:25:56 +08002270 0, write, &emulate,
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002271 level, gfn, pfn, prefault, map_writable);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002272 direct_pte_prefetch(vcpu, iterator.sptep);
Avi Kivity9f652d212008-12-25 14:54:25 +02002273 ++vcpu->stat.pf_fixed;
2274 break;
2275 }
2276
Xiao Guangrongc3707952011-07-12 03:28:04 +08002277 if (!is_shadow_present_pte(*iterator.sptep)) {
Lai Jiangshanc9fa0b32010-05-26 16:48:25 +08002278 u64 base_addr = iterator.addr;
2279
2280 base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
2281 pseudo_gfn = base_addr >> PAGE_SHIFT;
Avi Kivity9f652d212008-12-25 14:54:25 +02002282 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
2283 iterator.level - 1,
2284 1, ACC_ALL, iterator.sptep);
2285 if (!sp) {
2286 pgprintk("nonpaging_map: ENOMEM\n");
2287 kvm_release_pfn_clean(pfn);
2288 return -ENOMEM;
2289 }
2290
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08002291 mmu_spte_set(iterator.sptep,
2292 __pa(sp->spt)
2293 | PT_PRESENT_MASK | PT_WRITABLE_MASK
2294 | shadow_user_mask | shadow_x_mask
2295 | shadow_accessed_mask);
Avi Kivity9f652d212008-12-25 14:54:25 +02002296 }
2297 }
Xiao Guangrongb90a0e62011-07-12 03:25:56 +08002298 return emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002299}
2300
Huang Ying77db5cb2010-10-08 16:24:15 +08002301static void kvm_send_hwpoison_signal(unsigned long address, struct task_struct *tsk)
Huang Yingbf998152010-05-31 14:28:19 +08002302{
Huang Ying77db5cb2010-10-08 16:24:15 +08002303 siginfo_t info;
Huang Yingbf998152010-05-31 14:28:19 +08002304
Huang Ying77db5cb2010-10-08 16:24:15 +08002305 info.si_signo = SIGBUS;
2306 info.si_errno = 0;
2307 info.si_code = BUS_MCEERR_AR;
2308 info.si_addr = (void __user *)address;
2309 info.si_addr_lsb = PAGE_SHIFT;
2310
2311 send_sig_info(SIGBUS, &info, tsk);
Huang Yingbf998152010-05-31 14:28:19 +08002312}
2313
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002314static int kvm_handle_bad_page(struct kvm_vcpu *vcpu, gfn_t gfn, pfn_t pfn)
Huang Yingbf998152010-05-31 14:28:19 +08002315{
2316 kvm_release_pfn_clean(pfn);
2317 if (is_hwpoison_pfn(pfn)) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08002318 kvm_send_hwpoison_signal(gfn_to_hva(vcpu->kvm, gfn), current);
Huang Yingbf998152010-05-31 14:28:19 +08002319 return 0;
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002320 }
Gleb Natapovedba23e2010-07-07 20:16:45 +03002321
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002322 return -EFAULT;
Huang Yingbf998152010-05-31 14:28:19 +08002323}
2324
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002325static void transparent_hugepage_adjust(struct kvm_vcpu *vcpu,
2326 gfn_t *gfnp, pfn_t *pfnp, int *levelp)
2327{
2328 pfn_t pfn = *pfnp;
2329 gfn_t gfn = *gfnp;
2330 int level = *levelp;
2331
2332 /*
2333 * Check if it's a transparent hugepage. If this would be an
2334 * hugetlbfs page, level wouldn't be set to
2335 * PT_PAGE_TABLE_LEVEL and there would be no adjustment done
2336 * here.
2337 */
2338 if (!is_error_pfn(pfn) && !kvm_is_mmio_pfn(pfn) &&
2339 level == PT_PAGE_TABLE_LEVEL &&
2340 PageTransCompound(pfn_to_page(pfn)) &&
2341 !has_wrprotected_page(vcpu->kvm, gfn, PT_DIRECTORY_LEVEL)) {
2342 unsigned long mask;
2343 /*
2344 * mmu_notifier_retry was successful and we hold the
2345 * mmu_lock here, so the pmd can't become splitting
2346 * from under us, and in turn
2347 * __split_huge_page_refcount() can't run from under
2348 * us and we can safely transfer the refcount from
2349 * PG_tail to PG_head as we switch the pfn to tail to
2350 * head.
2351 */
2352 *levelp = level = PT_DIRECTORY_LEVEL;
2353 mask = KVM_PAGES_PER_HPAGE(level) - 1;
2354 VM_BUG_ON((gfn & mask) != (pfn & mask));
2355 if (pfn & mask) {
2356 gfn &= ~mask;
2357 *gfnp = gfn;
2358 kvm_release_pfn_clean(pfn);
2359 pfn &= ~mask;
2360 if (!get_page_unless_zero(pfn_to_page(pfn)))
2361 BUG();
2362 *pfnp = pfn;
2363 }
2364 }
2365}
2366
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002367static bool mmu_invalid_pfn(pfn_t pfn)
2368{
2369 return unlikely(is_invalid_pfn(pfn) || is_noslot_pfn(pfn));
2370}
2371
2372static bool handle_abnormal_pfn(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn,
2373 pfn_t pfn, unsigned access, int *ret_val)
2374{
2375 bool ret = true;
2376
2377 /* The pfn is invalid, report the error! */
2378 if (unlikely(is_invalid_pfn(pfn))) {
2379 *ret_val = kvm_handle_bad_page(vcpu, gfn, pfn);
2380 goto exit;
2381 }
2382
2383 if (unlikely(is_noslot_pfn(pfn))) {
2384 vcpu_cache_mmio_info(vcpu, gva, gfn, access);
2385 *ret_val = 1;
2386 goto exit;
2387 }
2388
2389 ret = false;
2390exit:
2391 return ret;
2392}
2393
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002394static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
Xiao Guangrong060c2ab2010-11-12 14:49:11 +08002395 gva_t gva, pfn_t *pfn, bool write, bool *writable);
2396
2397static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002398 bool prefault)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002399{
2400 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002401 int level;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002402 int force_pt_level;
Anthony Liguori35149e22008-04-02 14:46:56 -05002403 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002404 unsigned long mmu_seq;
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002405 bool map_writable;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002406
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002407 force_pt_level = mapping_level_dirty_bitmap(vcpu, gfn);
2408 if (likely(!force_pt_level)) {
2409 level = mapping_level(vcpu, gfn);
2410 /*
2411 * This path builds a PAE pagetable - so we can map
2412 * 2mb pages at maximum. Therefore check if the level
2413 * is larger than that.
2414 */
2415 if (level > PT_DIRECTORY_LEVEL)
2416 level = PT_DIRECTORY_LEVEL;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002417
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002418 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2419 } else
2420 level = PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002421
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002422 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002423 smp_rmb();
Xiao Guangrong060c2ab2010-11-12 14:49:11 +08002424
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002425 if (try_async_pf(vcpu, prefault, gfn, v, &pfn, write, &map_writable))
Xiao Guangrong060c2ab2010-11-12 14:49:11 +08002426 return 0;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002427
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002428 if (handle_abnormal_pfn(vcpu, v, gfn, pfn, ACC_ALL, &r))
2429 return r;
Avi Kivityd196e342008-01-24 11:44:11 +02002430
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002431 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002432 if (mmu_notifier_retry(vcpu, mmu_seq))
2433 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02002434 kvm_mmu_free_some_pages(vcpu);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002435 if (likely(!force_pt_level))
2436 transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002437 r = __direct_map(vcpu, v, write, map_writable, level, gfn, pfn,
2438 prefault);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002439 spin_unlock(&vcpu->kvm->mmu_lock);
2440
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002441
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002442 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002443
2444out_unlock:
2445 spin_unlock(&vcpu->kvm->mmu_lock);
2446 kvm_release_pfn_clean(pfn);
2447 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002448}
2449
2450
Avi Kivity17ac10a2007-01-05 16:36:40 -08002451static void mmu_free_roots(struct kvm_vcpu *vcpu)
2452{
2453 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002454 struct kvm_mmu_page *sp;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002455 LIST_HEAD(invalid_list);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002456
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002457 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002458 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002459 spin_lock(&vcpu->kvm->mmu_lock);
Joerg Roedel81407ca2010-09-10 17:31:00 +02002460 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL &&
2461 (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL ||
2462 vcpu->arch.mmu.direct_map)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002463 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002464
Avi Kivity4db35312007-11-21 15:28:32 +02002465 sp = page_header(root);
2466 --sp->root_count;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002467 if (!sp->root_count && sp->role.invalid) {
2468 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
2469 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
2470 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002471 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002472 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002473 return;
2474 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002475 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002476 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002477
Avi Kivity417726a2007-04-12 17:35:58 +03002478 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002479 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002480 sp = page_header(root);
2481 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002482 if (!sp->root_count && sp->role.invalid)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002483 kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
2484 &invalid_list);
Avi Kivity417726a2007-04-12 17:35:58 +03002485 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002486 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002487 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002488 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002489 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002490 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002491}
2492
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002493static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2494{
2495 int ret = 0;
2496
2497 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03002498 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002499 ret = 1;
2500 }
2501
2502 return ret;
2503}
2504
Joerg Roedel651dd372010-09-10 17:30:59 +02002505static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu)
2506{
2507 struct kvm_mmu_page *sp;
Avi Kivity7ebaf152010-10-03 18:51:39 +02002508 unsigned i;
Joerg Roedel651dd372010-09-10 17:30:59 +02002509
2510 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2511 spin_lock(&vcpu->kvm->mmu_lock);
2512 kvm_mmu_free_some_pages(vcpu);
2513 sp = kvm_mmu_get_page(vcpu, 0, 0, PT64_ROOT_LEVEL,
2514 1, ACC_ALL, NULL);
2515 ++sp->root_count;
2516 spin_unlock(&vcpu->kvm->mmu_lock);
2517 vcpu->arch.mmu.root_hpa = __pa(sp->spt);
2518 } else if (vcpu->arch.mmu.shadow_root_level == PT32E_ROOT_LEVEL) {
2519 for (i = 0; i < 4; ++i) {
2520 hpa_t root = vcpu->arch.mmu.pae_root[i];
2521
2522 ASSERT(!VALID_PAGE(root));
2523 spin_lock(&vcpu->kvm->mmu_lock);
2524 kvm_mmu_free_some_pages(vcpu);
Avi Kivity649497d2010-12-28 12:09:07 +02002525 sp = kvm_mmu_get_page(vcpu, i << (30 - PAGE_SHIFT),
2526 i << 30,
Joerg Roedel651dd372010-09-10 17:30:59 +02002527 PT32_ROOT_LEVEL, 1, ACC_ALL,
2528 NULL);
2529 root = __pa(sp->spt);
2530 ++sp->root_count;
2531 spin_unlock(&vcpu->kvm->mmu_lock);
2532 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Joerg Roedel651dd372010-09-10 17:30:59 +02002533 }
Xiao Guangrong62927572010-09-27 18:02:12 +08002534 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Joerg Roedel651dd372010-09-10 17:30:59 +02002535 } else
2536 BUG();
2537
2538 return 0;
2539}
2540
2541static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002542{
Avi Kivity4db35312007-11-21 15:28:32 +02002543 struct kvm_mmu_page *sp;
Joerg Roedel81407ca2010-09-10 17:31:00 +02002544 u64 pdptr, pm_mask;
2545 gfn_t root_gfn;
2546 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002547
Joerg Roedel5777ed32010-09-10 17:30:42 +02002548 root_gfn = vcpu->arch.mmu.get_cr3(vcpu) >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002549
Joerg Roedel651dd372010-09-10 17:30:59 +02002550 if (mmu_check_root(vcpu, root_gfn))
2551 return 1;
2552
2553 /*
2554 * Do we shadow a long mode page table? If so we need to
2555 * write-protect the guests page table root.
2556 */
2557 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002558 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002559
2560 ASSERT(!VALID_PAGE(root));
Joerg Roedel651dd372010-09-10 17:30:59 +02002561
Avi Kivity8facbbf2010-05-04 12:58:32 +03002562 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b6c92010-05-12 21:00:35 -03002563 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel651dd372010-09-10 17:30:59 +02002564 sp = kvm_mmu_get_page(vcpu, root_gfn, 0, PT64_ROOT_LEVEL,
2565 0, ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002566 root = __pa(sp->spt);
2567 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002568 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002569 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002570 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002571 }
Joerg Roedelf87f9282010-09-02 17:29:45 +02002572
Joerg Roedel651dd372010-09-10 17:30:59 +02002573 /*
2574 * We shadow a 32 bit page table. This may be a legacy 2-level
Joerg Roedel81407ca2010-09-10 17:31:00 +02002575 * or a PAE 3-level page table. In either case we need to be aware that
2576 * the shadow page table may be a PAE or a long mode page table.
Joerg Roedel651dd372010-09-10 17:30:59 +02002577 */
Joerg Roedel81407ca2010-09-10 17:31:00 +02002578 pm_mask = PT_PRESENT_MASK;
2579 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL)
2580 pm_mask |= PT_ACCESSED_MASK | PT_WRITABLE_MASK | PT_USER_MASK;
2581
Avi Kivity17ac10a2007-01-05 16:36:40 -08002582 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002583 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002584
2585 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002586 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Joerg Roedeld41d1892010-09-10 17:30:58 +02002587 pdptr = kvm_pdptr_read_mmu(vcpu, &vcpu->arch.mmu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002588 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002589 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002590 continue;
2591 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002592 root_gfn = pdptr >> PAGE_SHIFT;
Joerg Roedelf87f9282010-09-02 17:29:45 +02002593 if (mmu_check_root(vcpu, root_gfn))
2594 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002595 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002596 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b6c92010-05-12 21:00:35 -03002597 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02002598 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Joerg Roedel651dd372010-09-10 17:30:59 +02002599 PT32_ROOT_LEVEL, 0,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002600 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002601 root = __pa(sp->spt);
2602 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002603 spin_unlock(&vcpu->kvm->mmu_lock);
2604
Joerg Roedel81407ca2010-09-10 17:31:00 +02002605 vcpu->arch.mmu.pae_root[i] = root | pm_mask;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002606 }
Xiao Guangrong62927572010-09-27 18:02:12 +08002607 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Joerg Roedel81407ca2010-09-10 17:31:00 +02002608
2609 /*
2610 * If we shadow a 32 bit page table with a long mode page
2611 * table we enter this path.
2612 */
2613 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2614 if (vcpu->arch.mmu.lm_root == NULL) {
2615 /*
2616 * The additional page necessary for this is only
2617 * allocated on demand.
2618 */
2619
2620 u64 *lm_root;
2621
2622 lm_root = (void*)get_zeroed_page(GFP_KERNEL);
2623 if (lm_root == NULL)
2624 return 1;
2625
2626 lm_root[0] = __pa(vcpu->arch.mmu.pae_root) | pm_mask;
2627
2628 vcpu->arch.mmu.lm_root = lm_root;
2629 }
2630
2631 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.lm_root);
2632 }
2633
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002634 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002635}
2636
Joerg Roedel651dd372010-09-10 17:30:59 +02002637static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
2638{
2639 if (vcpu->arch.mmu.direct_map)
2640 return mmu_alloc_direct_roots(vcpu);
2641 else
2642 return mmu_alloc_shadow_roots(vcpu);
2643}
2644
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002645static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2646{
2647 int i;
2648 struct kvm_mmu_page *sp;
2649
Joerg Roedel81407ca2010-09-10 17:31:00 +02002650 if (vcpu->arch.mmu.direct_map)
2651 return;
2652
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002653 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2654 return;
Xiao Guangrong69030742010-09-27 18:09:29 +08002655
Xiao Guangrongbebb1062011-07-12 03:23:20 +08002656 vcpu_clear_mmio_info(vcpu, ~0ul);
Xiao Guangrong69030742010-09-27 18:09:29 +08002657 trace_kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC);
Joerg Roedel81407ca2010-09-10 17:31:00 +02002658 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002659 hpa_t root = vcpu->arch.mmu.root_hpa;
2660 sp = page_header(root);
2661 mmu_sync_children(vcpu, sp);
Xiao Guangrong5054c0d2010-11-12 14:46:08 +08002662 trace_kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002663 return;
2664 }
2665 for (i = 0; i < 4; ++i) {
2666 hpa_t root = vcpu->arch.mmu.pae_root[i];
2667
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002668 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002669 root &= PT64_BASE_ADDR_MASK;
2670 sp = page_header(root);
2671 mmu_sync_children(vcpu, sp);
2672 }
2673 }
Xiao Guangrong69030742010-09-27 18:09:29 +08002674 trace_kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002675}
2676
2677void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2678{
2679 spin_lock(&vcpu->kvm->mmu_lock);
2680 mmu_sync_roots(vcpu);
2681 spin_unlock(&vcpu->kvm->mmu_lock);
2682}
2683
Gleb Natapov1871c602010-02-10 14:21:32 +02002684static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
Avi Kivityab9ae312010-11-22 17:53:26 +02002685 u32 access, struct x86_exception *exception)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002686{
Avi Kivityab9ae312010-11-22 17:53:26 +02002687 if (exception)
2688 exception->error_code = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002689 return vaddr;
2690}
2691
Joerg Roedel6539e732010-09-10 17:30:50 +02002692static gpa_t nonpaging_gva_to_gpa_nested(struct kvm_vcpu *vcpu, gva_t vaddr,
Avi Kivityab9ae312010-11-22 17:53:26 +02002693 u32 access,
2694 struct x86_exception *exception)
Joerg Roedel6539e732010-09-10 17:30:50 +02002695{
Avi Kivityab9ae312010-11-22 17:53:26 +02002696 if (exception)
2697 exception->error_code = 0;
Joerg Roedel6539e732010-09-10 17:30:50 +02002698 return vcpu->arch.nested_mmu.translate_gpa(vcpu, vaddr, access);
2699}
2700
Avi Kivity6aa8b732006-12-10 02:21:36 -08002701static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002702 u32 error_code, bool prefault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002703{
Avi Kivitye8332402007-12-09 18:43:00 +02002704 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002705 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002706
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002707 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002708 r = mmu_topup_memory_caches(vcpu);
2709 if (r)
2710 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002711
Avi Kivity6aa8b732006-12-10 02:21:36 -08002712 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002713 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002714
Avi Kivitye8332402007-12-09 18:43:00 +02002715 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002716
Avi Kivitye8332402007-12-09 18:43:00 +02002717 return nonpaging_map(vcpu, gva & PAGE_MASK,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002718 error_code & PFERR_WRITE_MASK, gfn, prefault);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002719}
2720
Jan Kiszka7e1fbea2010-10-20 15:18:02 +02002721static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn)
Gleb Natapovaf585b92010-10-14 11:22:46 +02002722{
2723 struct kvm_arch_async_pf arch;
Xiao Guangrongfb67e142010-12-07 10:35:25 +08002724
Gleb Natapov7c907052010-10-14 11:22:53 +02002725 arch.token = (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id;
Gleb Natapovaf585b92010-10-14 11:22:46 +02002726 arch.gfn = gfn;
Xiao Guangrongc4806ac2010-11-12 14:49:55 +08002727 arch.direct_map = vcpu->arch.mmu.direct_map;
Xiao Guangrongfb67e142010-12-07 10:35:25 +08002728 arch.cr3 = vcpu->arch.mmu.get_cr3(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002729
2730 return kvm_setup_async_pf(vcpu, gva, gfn, &arch);
2731}
2732
2733static bool can_do_async_pf(struct kvm_vcpu *vcpu)
2734{
2735 if (unlikely(!irqchip_in_kernel(vcpu->kvm) ||
2736 kvm_event_needs_reinjection(vcpu)))
2737 return false;
2738
2739 return kvm_x86_ops->interrupt_allowed(vcpu);
2740}
2741
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002742static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002743 gva_t gva, pfn_t *pfn, bool write, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02002744{
2745 bool async;
2746
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002747 *pfn = gfn_to_pfn_async(vcpu->kvm, gfn, &async, write, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002748
2749 if (!async)
2750 return false; /* *pfn has correct page already */
2751
2752 put_page(pfn_to_page(*pfn));
2753
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002754 if (!prefault && can_do_async_pf(vcpu)) {
Xiao Guangrongc9b263d2010-11-01 16:58:43 +08002755 trace_kvm_try_async_get_page(gva, gfn);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002756 if (kvm_find_async_pf_gfn(vcpu, gfn)) {
2757 trace_kvm_async_pf_doublefault(gva, gfn);
2758 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
2759 return true;
2760 } else if (kvm_arch_setup_async_pf(vcpu, gva, gfn))
2761 return true;
2762 }
2763
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002764 *pfn = gfn_to_pfn_prot(vcpu->kvm, gfn, write, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02002765
2766 return false;
2767}
2768
Gleb Natapov56028d02010-10-17 18:13:42 +02002769static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, u32 error_code,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002770 bool prefault)
Joerg Roedelfb72d162008-02-07 13:47:44 +01002771{
Anthony Liguori35149e22008-04-02 14:46:56 -05002772 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002773 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002774 int level;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002775 int force_pt_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002776 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002777 unsigned long mmu_seq;
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002778 int write = error_code & PFERR_WRITE_MASK;
2779 bool map_writable;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002780
2781 ASSERT(vcpu);
2782 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2783
2784 r = mmu_topup_memory_caches(vcpu);
2785 if (r)
2786 return r;
2787
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002788 force_pt_level = mapping_level_dirty_bitmap(vcpu, gfn);
2789 if (likely(!force_pt_level)) {
2790 level = mapping_level(vcpu, gfn);
2791 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2792 } else
2793 level = PT_PAGE_TABLE_LEVEL;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002794
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002795 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002796 smp_rmb();
Gleb Natapovaf585b92010-10-14 11:22:46 +02002797
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002798 if (try_async_pf(vcpu, prefault, gfn, gpa, &pfn, write, &map_writable))
Gleb Natapovaf585b92010-10-14 11:22:46 +02002799 return 0;
2800
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002801 if (handle_abnormal_pfn(vcpu, 0, gfn, pfn, ACC_ALL, &r))
2802 return r;
2803
Joerg Roedelfb72d162008-02-07 13:47:44 +01002804 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002805 if (mmu_notifier_retry(vcpu, mmu_seq))
2806 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002807 kvm_mmu_free_some_pages(vcpu);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002808 if (likely(!force_pt_level))
2809 transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002810 r = __direct_map(vcpu, gpa, write, map_writable,
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002811 level, gfn, pfn, prefault);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002812 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002813
2814 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002815
2816out_unlock:
2817 spin_unlock(&vcpu->kvm->mmu_lock);
2818 kvm_release_pfn_clean(pfn);
2819 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002820}
2821
Avi Kivity6aa8b732006-12-10 02:21:36 -08002822static void nonpaging_free(struct kvm_vcpu *vcpu)
2823{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002824 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002825}
2826
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002827static int nonpaging_init_context(struct kvm_vcpu *vcpu,
2828 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002829{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002830 context->new_cr3 = nonpaging_new_cr3;
2831 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002832 context->gva_to_gpa = nonpaging_gva_to_gpa;
2833 context->free = nonpaging_free;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002834 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002835 context->invlpg = nonpaging_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08002836 context->update_pte = nonpaging_update_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002837 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002838 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002839 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002840 context->direct_map = true;
Joerg Roedel2d48a982010-09-10 17:31:01 +02002841 context->nx = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002842 return 0;
2843}
2844
Avi Kivityd835dfe2007-11-21 02:57:59 +02002845void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002846{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002847 ++vcpu->stat.tlb_flush;
Avi Kivitya8eeb042010-05-10 12:34:53 +03002848 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002849}
2850
2851static void paging_new_cr3(struct kvm_vcpu *vcpu)
2852{
Avi Kivity9f8fe502010-12-05 17:30:00 +02002853 pgprintk("%s: cr3 %lx\n", __func__, kvm_read_cr3(vcpu));
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002854 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002855}
2856
Joerg Roedel5777ed32010-09-10 17:30:42 +02002857static unsigned long get_cr3(struct kvm_vcpu *vcpu)
2858{
Avi Kivity9f8fe502010-12-05 17:30:00 +02002859 return kvm_read_cr3(vcpu);
Joerg Roedel5777ed32010-09-10 17:30:42 +02002860}
2861
Avi Kivity6389ee92010-11-29 16:12:30 +02002862static void inject_page_fault(struct kvm_vcpu *vcpu,
2863 struct x86_exception *fault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002864{
Avi Kivity6389ee92010-11-29 16:12:30 +02002865 vcpu->arch.mmu.inject_page_fault(vcpu, fault);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002866}
2867
Avi Kivity6aa8b732006-12-10 02:21:36 -08002868static void paging_free(struct kvm_vcpu *vcpu)
2869{
2870 nonpaging_free(vcpu);
2871}
2872
Joerg Roedel3241f222010-09-10 17:30:45 +02002873static bool is_rsvd_bits_set(struct kvm_mmu *mmu, u64 gpte, int level)
Dong, Eddie82725b22009-03-30 16:21:08 +08002874{
2875 int bit7;
2876
2877 bit7 = (gpte >> 7) & 1;
Joerg Roedel3241f222010-09-10 17:30:45 +02002878 return (gpte & mmu->rsvd_bits_mask[bit7][level-1]) != 0;
Dong, Eddie82725b22009-03-30 16:21:08 +08002879}
2880
Avi Kivity6aa8b732006-12-10 02:21:36 -08002881#define PTTYPE 64
2882#include "paging_tmpl.h"
2883#undef PTTYPE
2884
2885#define PTTYPE 32
2886#include "paging_tmpl.h"
2887#undef PTTYPE
2888
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002889static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
2890 struct kvm_mmu *context,
2891 int level)
Dong, Eddie82725b22009-03-30 16:21:08 +08002892{
Dong, Eddie82725b22009-03-30 16:21:08 +08002893 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2894 u64 exb_bit_rsvd = 0;
2895
Joerg Roedel2d48a982010-09-10 17:31:01 +02002896 if (!context->nx)
Dong, Eddie82725b22009-03-30 16:21:08 +08002897 exb_bit_rsvd = rsvd_bits(63, 63);
2898 switch (level) {
2899 case PT32_ROOT_LEVEL:
2900 /* no rsvd bits for 2 level 4K page table entries */
2901 context->rsvd_bits_mask[0][1] = 0;
2902 context->rsvd_bits_mask[0][0] = 0;
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002903 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
2904
2905 if (!is_pse(vcpu)) {
2906 context->rsvd_bits_mask[1][1] = 0;
2907 break;
2908 }
2909
Dong, Eddie82725b22009-03-30 16:21:08 +08002910 if (is_cpuid_PSE36())
2911 /* 36bits PSE 4MB page */
2912 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2913 else
2914 /* 32 bits PSE 4MB page */
2915 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08002916 break;
2917 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002918 context->rsvd_bits_mask[0][2] =
2919 rsvd_bits(maxphyaddr, 63) |
2920 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002921 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002922 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002923 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2924 rsvd_bits(maxphyaddr, 62); /* PTE */
2925 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2926 rsvd_bits(maxphyaddr, 62) |
2927 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002928 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002929 break;
2930 case PT64_ROOT_LEVEL:
2931 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2932 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2933 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2934 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2935 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002936 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002937 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2938 rsvd_bits(maxphyaddr, 51);
2939 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02002940 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
2941 rsvd_bits(maxphyaddr, 51) |
2942 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08002943 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002944 rsvd_bits(maxphyaddr, 51) |
2945 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08002946 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002947 break;
2948 }
2949}
2950
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002951static int paging64_init_context_common(struct kvm_vcpu *vcpu,
2952 struct kvm_mmu *context,
2953 int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002954{
Joerg Roedel2d48a982010-09-10 17:31:01 +02002955 context->nx = is_nx(vcpu);
2956
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002957 reset_rsvds_bits_mask(vcpu, context, level);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002958
2959 ASSERT(is_pae(vcpu));
2960 context->new_cr3 = paging_new_cr3;
2961 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002962 context->gva_to_gpa = paging64_gva_to_gpa;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002963 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002964 context->invlpg = paging64_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08002965 context->update_pte = paging64_update_pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002966 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002967 context->root_level = level;
2968 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002969 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002970 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002971 return 0;
2972}
2973
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002974static int paging64_init_context(struct kvm_vcpu *vcpu,
2975 struct kvm_mmu *context)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002976{
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002977 return paging64_init_context_common(vcpu, context, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002978}
2979
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002980static int paging32_init_context(struct kvm_vcpu *vcpu,
2981 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002982{
Joerg Roedel2d48a982010-09-10 17:31:01 +02002983 context->nx = false;
2984
Joerg Roedel52fde8d2010-09-10 17:30:44 +02002985 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002986
2987 context->new_cr3 = paging_new_cr3;
2988 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002989 context->gva_to_gpa = paging32_gva_to_gpa;
2990 context->free = paging_free;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002991 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002992 context->invlpg = paging32_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08002993 context->update_pte = paging32_update_pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002994 context->root_level = PT32_ROOT_LEVEL;
2995 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002996 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002997 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002998 return 0;
2999}
3000
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003001static int paging32E_init_context(struct kvm_vcpu *vcpu,
3002 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003003{
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003004 return paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003005}
3006
Joerg Roedelfb72d162008-02-07 13:47:44 +01003007static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
3008{
Joerg Roedel14dfe852010-09-10 17:30:49 +02003009 struct kvm_mmu *context = vcpu->arch.walk_mmu;
Joerg Roedelfb72d162008-02-07 13:47:44 +01003010
Avi Kivityc445f8e2010-12-21 16:26:01 +02003011 context->base_role.word = 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01003012 context->new_cr3 = nonpaging_new_cr3;
3013 context->page_fault = tdp_page_fault;
3014 context->free = nonpaging_free;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03003015 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03003016 context->invlpg = nonpaging_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08003017 context->update_pte = nonpaging_update_pte;
Sheng Yang67253af2008-04-25 10:20:22 +08003018 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01003019 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02003020 context->direct_map = true;
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003021 context->set_cr3 = kvm_x86_ops->set_tdp_cr3;
Joerg Roedel5777ed32010-09-10 17:30:42 +02003022 context->get_cr3 = get_cr3;
Joerg Roedelcb659db2010-09-10 17:30:43 +02003023 context->inject_page_fault = kvm_inject_page_fault;
Joerg Roedel2d48a982010-09-10 17:31:01 +02003024 context->nx = is_nx(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01003025
3026 if (!is_paging(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003027 context->nx = false;
Joerg Roedelfb72d162008-02-07 13:47:44 +01003028 context->gva_to_gpa = nonpaging_gva_to_gpa;
3029 context->root_level = 0;
3030 } else if (is_long_mode(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003031 context->nx = is_nx(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003032 reset_rsvds_bits_mask(vcpu, context, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01003033 context->gva_to_gpa = paging64_gva_to_gpa;
3034 context->root_level = PT64_ROOT_LEVEL;
3035 } else if (is_pae(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003036 context->nx = is_nx(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003037 reset_rsvds_bits_mask(vcpu, context, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01003038 context->gva_to_gpa = paging64_gva_to_gpa;
3039 context->root_level = PT32E_ROOT_LEVEL;
3040 } else {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003041 context->nx = false;
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003042 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01003043 context->gva_to_gpa = paging32_gva_to_gpa;
3044 context->root_level = PT32_ROOT_LEVEL;
3045 }
3046
3047 return 0;
3048}
3049
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003050int kvm_init_shadow_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003051{
Avi Kivitya770f6f2008-12-21 19:20:09 +02003052 int r;
Avi Kivity411c5882011-06-06 16:11:54 +03003053 bool smep = kvm_read_cr4_bits(vcpu, X86_CR4_SMEP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003054 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003055 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003056
3057 if (!is_paging(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003058 r = nonpaging_init_context(vcpu, context);
Avi Kivitya9058ec2006-12-29 16:49:37 -08003059 else if (is_long_mode(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003060 r = paging64_init_context(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003061 else if (is_pae(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003062 r = paging32E_init_context(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003063 else
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003064 r = paging32_init_context(vcpu, context);
Avi Kivitya770f6f2008-12-21 19:20:09 +02003065
Avi Kivity5b7e0102010-04-14 19:20:03 +03003066 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
Joerg Roedelf43addd2010-09-10 17:30:40 +02003067 vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
Avi Kivity411c5882011-06-06 16:11:54 +03003068 vcpu->arch.mmu.base_role.smep_andnot_wp
3069 = smep && !is_write_protection(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003070
3071 return r;
3072}
3073EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu);
3074
3075static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
3076{
Joerg Roedel14dfe852010-09-10 17:30:49 +02003077 int r = kvm_init_shadow_mmu(vcpu, vcpu->arch.walk_mmu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003078
Joerg Roedel14dfe852010-09-10 17:30:49 +02003079 vcpu->arch.walk_mmu->set_cr3 = kvm_x86_ops->set_cr3;
3080 vcpu->arch.walk_mmu->get_cr3 = get_cr3;
3081 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
Avi Kivitya770f6f2008-12-21 19:20:09 +02003082
3083 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003084}
3085
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003086static int init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
3087{
3088 struct kvm_mmu *g_context = &vcpu->arch.nested_mmu;
3089
3090 g_context->get_cr3 = get_cr3;
3091 g_context->inject_page_fault = kvm_inject_page_fault;
3092
3093 /*
3094 * Note that arch.mmu.gva_to_gpa translates l2_gva to l1_gpa. The
3095 * translation of l2_gpa to l1_gpa addresses is done using the
3096 * arch.nested_mmu.gva_to_gpa function. Basically the gva_to_gpa
3097 * functions between mmu and nested_mmu are swapped.
3098 */
3099 if (!is_paging(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003100 g_context->nx = false;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003101 g_context->root_level = 0;
3102 g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested;
3103 } else if (is_long_mode(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003104 g_context->nx = is_nx(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003105 reset_rsvds_bits_mask(vcpu, g_context, PT64_ROOT_LEVEL);
3106 g_context->root_level = PT64_ROOT_LEVEL;
3107 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
3108 } else if (is_pae(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003109 g_context->nx = is_nx(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003110 reset_rsvds_bits_mask(vcpu, g_context, PT32E_ROOT_LEVEL);
3111 g_context->root_level = PT32E_ROOT_LEVEL;
3112 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
3113 } else {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003114 g_context->nx = false;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003115 reset_rsvds_bits_mask(vcpu, g_context, PT32_ROOT_LEVEL);
3116 g_context->root_level = PT32_ROOT_LEVEL;
3117 g_context->gva_to_gpa = paging32_gva_to_gpa_nested;
3118 }
3119
3120 return 0;
3121}
3122
Joerg Roedelfb72d162008-02-07 13:47:44 +01003123static int init_kvm_mmu(struct kvm_vcpu *vcpu)
3124{
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003125 if (mmu_is_nested(vcpu))
3126 return init_kvm_nested_mmu(vcpu);
3127 else if (tdp_enabled)
Joerg Roedelfb72d162008-02-07 13:47:44 +01003128 return init_kvm_tdp_mmu(vcpu);
3129 else
3130 return init_kvm_softmmu(vcpu);
3131}
3132
Avi Kivity6aa8b732006-12-10 02:21:36 -08003133static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
3134{
3135 ASSERT(vcpu);
Sheng Yang62ad0752010-05-12 16:40:41 +08003136 if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
3137 /* mmu.free() should set root_hpa = INVALID_PAGE */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003138 vcpu->arch.mmu.free(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003139}
3140
3141int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
3142{
Avi Kivity17c3ba92007-06-04 15:58:30 +03003143 destroy_kvm_mmu(vcpu);
Marcelo Tosattif8f7e5e2011-06-21 14:00:10 -03003144 return init_kvm_mmu(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03003145}
Eddie Dong8668a3c2007-10-10 14:26:45 +08003146EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03003147
3148int kvm_mmu_load(struct kvm_vcpu *vcpu)
3149{
Avi Kivity714b93d2007-01-05 16:36:53 -08003150 int r;
3151
Avi Kivitye2dec932007-01-05 16:36:54 -08003152 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03003153 if (r)
3154 goto out;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003155 r = mmu_alloc_roots(vcpu);
Avi Kivity8facbbf2010-05-04 12:58:32 +03003156 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003157 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003158 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003159 if (r)
3160 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08003161 /* set_cr3() should ensure TLB has been flushed */
Joerg Roedelf43addd2010-09-10 17:30:40 +02003162 vcpu->arch.mmu.set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08003163out:
3164 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003165}
Avi Kivity17c3ba92007-06-04 15:58:30 +03003166EXPORT_SYMBOL_GPL(kvm_mmu_load);
3167
3168void kvm_mmu_unload(struct kvm_vcpu *vcpu)
3169{
3170 mmu_free_roots(vcpu);
3171}
Joerg Roedel4b161842010-09-10 17:31:03 +02003172EXPORT_SYMBOL_GPL(kvm_mmu_unload);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003173
Avi Kivity00284252007-05-01 16:53:31 +03003174static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Xiao Guangrong7c562522011-03-28 10:29:27 +08003175 struct kvm_mmu_page *sp, u64 *spte,
3176 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03003177{
Marcelo Tosatti30945382008-06-11 20:32:40 -03003178 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02003179 ++vcpu->kvm->stat.mmu_pde_zapped;
3180 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03003181 }
Avi Kivity00284252007-05-01 16:53:31 +03003182
Avi Kivity4cee5762007-11-18 16:37:07 +02003183 ++vcpu->kvm->stat.mmu_pte_updated;
Xiao Guangrong7c562522011-03-28 10:29:27 +08003184 vcpu->arch.mmu.update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03003185}
3186
Avi Kivity79539ce2007-11-21 02:06:21 +02003187static bool need_remote_flush(u64 old, u64 new)
3188{
3189 if (!is_shadow_present_pte(old))
3190 return false;
3191 if (!is_shadow_present_pte(new))
3192 return true;
3193 if ((old ^ new) & PT64_BASE_ADDR_MASK)
3194 return true;
3195 old ^= PT64_NX_MASK;
3196 new ^= PT64_NX_MASK;
3197 return (old & ~new & PT64_PERM_MASK) != 0;
3198}
3199
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003200static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page,
3201 bool remote_flush, bool local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003202{
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003203 if (zap_page)
3204 return;
3205
3206 if (remote_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003207 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003208 else if (local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003209 kvm_mmu_flush_tlb(vcpu);
3210}
3211
Avi Kivity12b7d282007-09-23 14:10:49 +02003212static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
3213{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003214 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02003215
Sheng Yang7b523452008-04-25 21:13:50 +08003216 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02003217}
3218
Avi Kivity1b7fcd32008-05-15 13:51:35 +03003219static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
3220{
3221 u64 *spte = vcpu->arch.last_pte_updated;
3222
3223 if (spte
3224 && vcpu->arch.last_pte_gfn == gfn
3225 && shadow_accessed_mask
3226 && !(*spte & shadow_accessed_mask)
3227 && is_shadow_present_pte(*spte))
3228 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
3229}
3230
Avi Kivity09072da2007-05-01 14:16:52 +03003231void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003232 const u8 *new, int bytes,
3233 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08003234{
Avi Kivity9b7a0322007-01-05 16:36:45 -08003235 gfn_t gfn = gpa >> PAGE_SHIFT;
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003236 union kvm_mmu_page_role mask = { .word = 0 };
Avi Kivity4db35312007-11-21 15:28:32 +02003237 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003238 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003239 LIST_HEAD(invalid_list);
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08003240 u64 entry, gentry, *spte;
3241 unsigned pte_size, page_offset, misaligned, quadrant, offset;
3242 int level, npte, invlpg_counter, r, flooded = 0;
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003243 bool remote_flush, local_flush, zap_page;
3244
Xiao Guangrong332b2072011-05-15 23:20:27 +08003245 /*
3246 * If we don't have indirect shadow pages, it means no page is
3247 * write-protected, so we can exit simply.
3248 */
3249 if (!ACCESS_ONCE(vcpu->kvm->arch.indirect_shadow_pages))
3250 return;
3251
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003252 zap_page = remote_flush = local_flush = false;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08003253 offset = offset_in_page(gpa);
Avi Kivity9b7a0322007-01-05 16:36:45 -08003254
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003255 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivity72016f32010-03-15 13:59:53 +02003256
Avi Kivity08e850c2010-03-15 13:59:57 +02003257 invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
3258
3259 /*
3260 * Assume that the pte write on a page table of the same type
Xiao Guangrong49b26e22011-03-04 19:00:00 +08003261 * as the current vcpu paging mode since we update the sptes only
3262 * when they have the same mode.
Avi Kivity08e850c2010-03-15 13:59:57 +02003263 */
3264 if ((is_pae(vcpu) && bytes == 4) || !new) {
3265 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
3266 if (is_pae(vcpu)) {
3267 gpa &= ~(gpa_t)7;
3268 bytes = 8;
3269 }
3270 r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
3271 if (r)
3272 gentry = 0;
3273 new = (const u8 *)&gentry;
3274 }
3275
Avi Kivity72016f32010-03-15 13:59:53 +02003276 switch (bytes) {
3277 case 4:
3278 gentry = *(const u32 *)new;
3279 break;
3280 case 8:
3281 gentry = *(const u64 *)new;
3282 break;
3283 default:
3284 gentry = 0;
3285 break;
3286 }
3287
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003288 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity08e850c2010-03-15 13:59:57 +02003289 if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
3290 gentry = 0;
Avi Kivityeb787d12007-12-31 15:27:49 +02003291 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02003292 ++vcpu->kvm->stat.mmu_pte_write;
Xiao Guangrong8b1fe172010-08-30 18:22:53 +08003293 trace_kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE);
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003294 if (guest_initiated) {
Xiao Guangrong1b7fd452011-03-04 18:58:02 +08003295 kvm_mmu_access_page(vcpu, gfn);
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003296 if (gfn == vcpu->arch.last_pt_write_gfn
3297 && !last_updated_pte_accessed(vcpu)) {
3298 ++vcpu->arch.last_pt_write_count;
3299 if (vcpu->arch.last_pt_write_count >= 3)
3300 flooded = 1;
3301 } else {
3302 vcpu->arch.last_pt_write_gfn = gfn;
3303 vcpu->arch.last_pt_write_count = 1;
3304 vcpu->arch.last_pte_updated = NULL;
3305 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08003306 }
Xiao Guangrong3246af02010-04-16 16:35:54 +08003307
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003308 mask.cr0_wp = mask.cr4_pae = mask.nxe = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003309 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node) {
Avi Kivity5b7e0102010-04-14 19:20:03 +03003310 pte_size = sp->role.cr4_pae ? 8 : 4;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003311 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03003312 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08003313 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003314 /*
3315 * Misaligned accesses are too much trouble to fix
3316 * up; also, they usually indicate a page is not used
3317 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08003318 *
3319 * If we're seeing too many writes to a page,
3320 * it may no longer be a page table, or we may be
3321 * forking, in which case it is better to unmap the
3322 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003323 */
3324 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02003325 gpa, bytes, sp->role.word);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003326 zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003327 &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02003328 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003329 continue;
3330 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08003331 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02003332 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02003333 npte = 1;
Avi Kivity5b7e0102010-04-14 19:20:03 +03003334 if (!sp->role.cr4_pae) {
Avi Kivityac1b7142007-03-08 17:13:32 +02003335 page_offset <<= 1; /* 32->64 */
3336 /*
3337 * A 32-bit pde maps 4MB while the shadow pdes map
3338 * only 2MB. So we need to double the offset again
3339 * and zap two pdes instead of one.
3340 */
3341 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03003342 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02003343 page_offset <<= 1;
3344 npte = 2;
3345 }
Avi Kivityfce06572007-05-01 16:44:05 +03003346 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003347 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02003348 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03003349 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003350 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003351 local_flush = true;
Avi Kivity4db35312007-11-21 15:28:32 +02003352 spte = &sp->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02003353 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02003354 entry = *spte;
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08003355 mmu_page_zap_pte(vcpu->kvm, sp, spte);
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003356 if (gentry &&
3357 !((sp->role.word ^ vcpu->arch.mmu.base_role.word)
3358 & mask.word))
Xiao Guangrong7c562522011-03-28 10:29:27 +08003359 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003360 if (!remote_flush && need_remote_flush(entry, *spte))
3361 remote_flush = true;
Avi Kivityac1b7142007-03-08 17:13:32 +02003362 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003363 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08003364 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003365 mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003366 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong8b1fe172010-08-30 18:22:53 +08003367 trace_kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003368 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivityda4a00f2007-01-05 16:36:44 -08003369}
3370
Avi Kivitya4360362007-01-05 16:36:45 -08003371int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
3372{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003373 gpa_t gpa;
3374 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08003375
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02003376 if (vcpu->arch.mmu.direct_map)
Avi Kivity60f24782009-08-27 13:37:06 +03003377 return 0;
3378
Gleb Natapov1871c602010-02-10 14:21:32 +02003379 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003380
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003381 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003382 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003383 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003384 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08003385}
Avi Kivity577bdc42008-07-19 08:57:05 +03003386EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08003387
Avi Kivity22d95b12007-09-14 20:26:06 +03003388void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08003389{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003390 LIST_HEAD(invalid_list);
Xiao Guangrong103ad252010-06-04 21:54:38 +08003391
Dave Hansene0df7b92010-08-19 18:11:05 -07003392 while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES &&
Izik Eidus3b80fff2009-07-28 15:26:58 -03003393 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02003394 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08003395
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003396 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02003397 struct kvm_mmu_page, link);
Dave Hansene0df7b92010-08-19 18:11:05 -07003398 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02003399 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08003400 }
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08003401 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Avi Kivityebeace82007-01-05 16:36:47 -08003402}
Avi Kivityebeace82007-01-05 16:36:47 -08003403
Andre Przywaradc25e892010-12-21 11:12:07 +01003404int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code,
3405 void *insn, int insn_len)
Avi Kivity30677142007-10-28 18:48:59 +02003406{
3407 int r;
3408 enum emulation_result er;
3409
Gleb Natapov56028d02010-10-17 18:13:42 +02003410 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code, false);
Avi Kivity30677142007-10-28 18:48:59 +02003411 if (r < 0)
3412 goto out;
3413
3414 if (!r) {
3415 r = 1;
3416 goto out;
3417 }
3418
Avi Kivityb733bfb2007-10-28 18:52:05 +02003419 r = mmu_topup_memory_caches(vcpu);
3420 if (r)
3421 goto out;
3422
Andre Przywaradc25e892010-12-21 11:12:07 +01003423 er = x86_emulate_instruction(vcpu, cr2, 0, insn, insn_len);
Avi Kivity30677142007-10-28 18:48:59 +02003424
3425 switch (er) {
3426 case EMULATE_DONE:
3427 return 1;
3428 case EMULATE_DO_MMIO:
3429 ++vcpu->stat.mmio_exits;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03003430 /* fall through */
Avi Kivity30677142007-10-28 18:48:59 +02003431 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03003432 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02003433 default:
3434 BUG();
3435 }
3436out:
Avi Kivity30677142007-10-28 18:48:59 +02003437 return r;
3438}
3439EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
3440
Marcelo Tosattia7052892008-09-23 13:18:35 -03003441void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
3442{
Marcelo Tosattia7052892008-09-23 13:18:35 -03003443 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003444 kvm_mmu_flush_tlb(vcpu);
3445 ++vcpu->stat.invlpg;
3446}
3447EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
3448
Joerg Roedel18552672008-02-07 13:47:41 +01003449void kvm_enable_tdp(void)
3450{
3451 tdp_enabled = true;
3452}
3453EXPORT_SYMBOL_GPL(kvm_enable_tdp);
3454
Joerg Roedel5f4cb662008-07-14 20:36:36 +02003455void kvm_disable_tdp(void)
3456{
3457 tdp_enabled = false;
3458}
3459EXPORT_SYMBOL_GPL(kvm_disable_tdp);
3460
Avi Kivity6aa8b732006-12-10 02:21:36 -08003461static void free_mmu_pages(struct kvm_vcpu *vcpu)
3462{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003463 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Joerg Roedel81407ca2010-09-10 17:31:00 +02003464 if (vcpu->arch.mmu.lm_root != NULL)
3465 free_page((unsigned long)vcpu->arch.mmu.lm_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003466}
3467
3468static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
3469{
Avi Kivity17ac10a2007-01-05 16:36:40 -08003470 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003471 int i;
3472
3473 ASSERT(vcpu);
3474
Avi Kivity17ac10a2007-01-05 16:36:40 -08003475 /*
3476 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
3477 * Therefore we need to allocate shadow page tables in the first
3478 * 4GB of memory, which happens to fit the DMA32 zone.
3479 */
3480 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
3481 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08003482 return -ENOMEM;
3483
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003484 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003485 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003486 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003487
Avi Kivity6aa8b732006-12-10 02:21:36 -08003488 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003489}
3490
Ingo Molnar8018c272006-12-29 16:50:01 -08003491int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003492{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003493 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003494 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003495
Ingo Molnar8018c272006-12-29 16:50:01 -08003496 return alloc_mmu_pages(vcpu);
3497}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003498
Ingo Molnar8018c272006-12-29 16:50:01 -08003499int kvm_mmu_setup(struct kvm_vcpu *vcpu)
3500{
3501 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003502 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08003503
Ingo Molnar8018c272006-12-29 16:50:01 -08003504 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003505}
3506
Avi Kivity90cb0522007-07-17 13:04:56 +03003507void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003508{
Avi Kivity4db35312007-11-21 15:28:32 +02003509 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003510
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003511 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003512 int i;
3513 u64 *pt;
3514
Sheng Yang291f26b2008-10-16 17:30:57 +08003515 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003516 continue;
3517
Avi Kivity4db35312007-11-21 15:28:32 +02003518 pt = sp->spt;
Avi Kivity8234b222010-12-27 12:08:45 +02003519 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Xiao Guangrongda8dc752011-03-04 18:56:41 +08003520 if (!is_shadow_present_pte(pt[i]) ||
3521 !is_last_spte(pt[i], sp->role.level))
3522 continue;
3523
3524 if (is_large_pte(pt[i])) {
Xiao Guangrongc3707952011-07-12 03:28:04 +08003525 drop_spte(kvm, &pt[i]);
Avi Kivity8234b222010-12-27 12:08:45 +02003526 --kvm->stat.lpages;
Xiao Guangrongda8dc752011-03-04 18:56:41 +08003527 continue;
Avi Kivity8234b222010-12-27 12:08:45 +02003528 }
Xiao Guangrongda8dc752011-03-04 18:56:41 +08003529
Avi Kivity6aa8b732006-12-10 02:21:36 -08003530 /* avoid RMW */
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003531 if (is_writable_pte(pt[i]))
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08003532 mmu_spte_update(&pt[i],
3533 pt[i] & ~PT_WRITABLE_MASK);
Avi Kivity8234b222010-12-27 12:08:45 +02003534 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003535 }
Avi Kivity171d5952008-08-27 16:40:51 +03003536 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003537}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003538
Avi Kivity90cb0522007-07-17 13:04:56 +03003539void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03003540{
Avi Kivity4db35312007-11-21 15:28:32 +02003541 struct kvm_mmu_page *sp, *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003542 LIST_HEAD(invalid_list);
Dor Laore0fa8262007-03-30 13:06:33 +03003543
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003544 spin_lock(&kvm->mmu_lock);
Xiao Guangrong3246af02010-04-16 16:35:54 +08003545restart:
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003546 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003547 if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list))
Xiao Guangrong3246af02010-04-16 16:35:54 +08003548 goto restart;
3549
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003550 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003551 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03003552}
3553
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003554static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm,
3555 struct list_head *invalid_list)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003556{
3557 struct kvm_mmu_page *page;
3558
3559 page = container_of(kvm->arch.active_mmu_pages.prev,
3560 struct kvm_mmu_page, link);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003561 return kvm_mmu_prepare_zap_page(kvm, page, invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003562}
3563
Ying Han1495f232011-05-24 17:12:27 -07003564static int mmu_shrink(struct shrinker *shrink, struct shrink_control *sc)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003565{
3566 struct kvm *kvm;
3567 struct kvm *kvm_freed = NULL;
Ying Han1495f232011-05-24 17:12:27 -07003568 int nr_to_scan = sc->nr_to_scan;
Dave Hansen45221ab2010-08-19 18:11:37 -07003569
3570 if (nr_to_scan == 0)
3571 goto out;
Izik Eidus3ee16c82008-03-30 15:17:21 +03003572
Jan Kiszkae935b832011-02-08 12:55:33 +01003573 raw_spin_lock(&kvm_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003574
3575 list_for_each_entry(kvm, &vm_list, vm_list) {
Dave Hansen45221ab2010-08-19 18:11:37 -07003576 int idx, freed_pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003577 LIST_HEAD(invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003578
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003579 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003580 spin_lock(&kvm->mmu_lock);
Dave Hansen45221ab2010-08-19 18:11:37 -07003581 if (!kvm_freed && nr_to_scan > 0 &&
3582 kvm->arch.n_used_mmu_pages > 0) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003583 freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm,
3584 &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003585 kvm_freed = kvm;
3586 }
3587 nr_to_scan--;
3588
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003589 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003590 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003591 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003592 }
3593 if (kvm_freed)
3594 list_move_tail(&kvm_freed->vm_list, &vm_list);
3595
Jan Kiszkae935b832011-02-08 12:55:33 +01003596 raw_spin_unlock(&kvm_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003597
Dave Hansen45221ab2010-08-19 18:11:37 -07003598out:
3599 return percpu_counter_read_positive(&kvm_total_used_mmu_pages);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003600}
3601
3602static struct shrinker mmu_shrinker = {
3603 .shrink = mmu_shrink,
3604 .seeks = DEFAULT_SEEKS * 10,
3605};
3606
Ingo Molnar2ddfd202008-05-22 10:37:48 +02003607static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03003608{
Xiao Guangrong53c07b12011-05-15 23:26:20 +08003609 if (pte_list_desc_cache)
3610 kmem_cache_destroy(pte_list_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003611 if (mmu_page_header_cache)
3612 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003613}
3614
3615int kvm_mmu_module_init(void)
3616{
Xiao Guangrong53c07b12011-05-15 23:26:20 +08003617 pte_list_desc_cache = kmem_cache_create("pte_list_desc",
3618 sizeof(struct pte_list_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09003619 0, 0, NULL);
Xiao Guangrong53c07b12011-05-15 23:26:20 +08003620 if (!pte_list_desc_cache)
Avi Kivityb5a33a72007-04-15 16:31:09 +03003621 goto nomem;
3622
Avi Kivityd3d25b02007-05-30 12:34:53 +03003623 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
3624 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09003625 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003626 if (!mmu_page_header_cache)
3627 goto nomem;
3628
Wei Yongjun45bf21a2010-08-23 16:13:15 +08003629 if (percpu_counter_init(&kvm_total_used_mmu_pages, 0))
3630 goto nomem;
3631
Izik Eidus3ee16c82008-03-30 15:17:21 +03003632 register_shrinker(&mmu_shrinker);
3633
Avi Kivityb5a33a72007-04-15 16:31:09 +03003634 return 0;
3635
3636nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03003637 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003638 return -ENOMEM;
3639}
3640
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003641/*
3642 * Caculate mmu pages needed for kvm.
3643 */
3644unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3645{
3646 int i;
3647 unsigned int nr_mmu_pages;
3648 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003649 struct kvm_memslots *slots;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003650
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003651 slots = kvm_memslots(kvm);
3652
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003653 for (i = 0; i < slots->nmemslots; i++)
3654 nr_pages += slots->memslots[i].npages;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003655
3656 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
3657 nr_mmu_pages = max(nr_mmu_pages,
3658 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
3659
3660 return nr_mmu_pages;
3661}
3662
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003663static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3664 unsigned len)
3665{
3666 if (len > buffer->len)
3667 return NULL;
3668 return buffer->ptr;
3669}
3670
3671static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
3672 unsigned len)
3673{
3674 void *ret;
3675
3676 ret = pv_mmu_peek_buffer(buffer, len);
3677 if (!ret)
3678 return ret;
3679 buffer->ptr += len;
3680 buffer->len -= len;
3681 buffer->processed += len;
3682 return ret;
3683}
3684
3685static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
3686 gpa_t addr, gpa_t value)
3687{
3688 int bytes = 8;
3689 int r;
3690
3691 if (!is_long_mode(vcpu) && !is_pae(vcpu))
3692 bytes = 4;
3693
3694 r = mmu_topup_memory_caches(vcpu);
3695 if (r)
3696 return r;
3697
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003698 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003699 return -EFAULT;
3700
3701 return 1;
3702}
3703
3704static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
3705{
Avi Kivity9f8fe502010-12-05 17:30:00 +02003706 (void)kvm_set_cr3(vcpu, kvm_read_cr3(vcpu));
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003707 return 1;
3708}
3709
3710static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
3711{
3712 spin_lock(&vcpu->kvm->mmu_lock);
3713 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
3714 spin_unlock(&vcpu->kvm->mmu_lock);
3715 return 1;
3716}
3717
3718static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
3719 struct kvm_pv_mmu_op_buffer *buffer)
3720{
3721 struct kvm_mmu_op_header *header;
3722
3723 header = pv_mmu_peek_buffer(buffer, sizeof *header);
3724 if (!header)
3725 return 0;
3726 switch (header->op) {
3727 case KVM_MMU_OP_WRITE_PTE: {
3728 struct kvm_mmu_op_write_pte *wpte;
3729
3730 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
3731 if (!wpte)
3732 return 0;
3733 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
3734 wpte->pte_val);
3735 }
3736 case KVM_MMU_OP_FLUSH_TLB: {
3737 struct kvm_mmu_op_flush_tlb *ftlb;
3738
3739 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
3740 if (!ftlb)
3741 return 0;
3742 return kvm_pv_mmu_flush_tlb(vcpu);
3743 }
3744 case KVM_MMU_OP_RELEASE_PT: {
3745 struct kvm_mmu_op_release_pt *rpt;
3746
3747 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3748 if (!rpt)
3749 return 0;
3750 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3751 }
3752 default: return 0;
3753 }
3754}
3755
3756int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3757 gpa_t addr, unsigned long *ret)
3758{
3759 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003760 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003761
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003762 buffer->ptr = buffer->buf;
3763 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3764 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003765
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003766 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003767 if (r)
3768 goto out;
3769
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003770 while (buffer->len) {
3771 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003772 if (r < 0)
3773 goto out;
3774 if (r == 0)
3775 break;
3776 }
3777
3778 r = 1;
3779out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003780 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003781 return r;
3782}
3783
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003784int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3785{
3786 struct kvm_shadow_walk_iterator iterator;
3787 int nr_sptes = 0;
3788
3789 spin_lock(&vcpu->kvm->mmu_lock);
3790 for_each_shadow_entry(vcpu, addr, iterator) {
3791 sptes[iterator.level-1] = *iterator.sptep;
3792 nr_sptes++;
3793 if (!is_shadow_present_pte(*iterator.sptep))
3794 break;
3795 }
3796 spin_unlock(&vcpu->kvm->mmu_lock);
3797
3798 return nr_sptes;
3799}
3800EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3801
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08003802void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
3803{
3804 ASSERT(vcpu);
3805
3806 destroy_kvm_mmu(vcpu);
3807 free_mmu_pages(vcpu);
3808 mmu_free_memory_caches(vcpu);
Xiao Guangrongb034cf02010-12-23 16:08:35 +08003809}
3810
3811#ifdef CONFIG_KVM_MMU_AUDIT
3812#include "mmu_audit.c"
3813#else
3814static void mmu_audit_disable(void) { }
3815#endif
3816
3817void kvm_mmu_module_exit(void)
3818{
3819 mmu_destroy_caches();
3820 percpu_counter_destroy(&kvm_total_used_mmu_pages);
3821 unregister_shrinker(&mmu_shrinker);
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08003822 mmu_audit_disable();
3823}