blob: 62f69dbf6b52475adba3b06a4a67033641ff1a11 [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"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025
Avi Kivityedf88412007-12-16 11:02:48 +020026#include <linux/kvm_host.h>
Avi Kivitye4956062007-06-28 14:15:57 -040027#include <linux/types.h>
28#include <linux/string.h>
29#include <linux/mm.h>
30#include <linux/highmem.h>
31#include <linux/module.h>
Izik Eidus448353c2007-11-26 14:08:14 +020032#include <linux/swap.h>
Marcelo Tosatti05da4552008-02-23 11:44:30 -030033#include <linux/hugetlb.h>
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050034#include <linux/compiler.h>
Marcelo Tosattibc6678a2009-12-23 14:35:21 -020035#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Huang Yingbf998152010-05-31 14:28:19 +080037#include <linux/uaccess.h>
Avi Kivitye4956062007-06-28 14:15:57 -040038
39#include <asm/page.h>
40#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020041#include <asm/io.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020042#include <asm/vmx.h>
Avi Kivitye4956062007-06-28 14:15:57 -040043
Joerg Roedel18552672008-02-07 13:47:41 +010044/*
45 * When setting this variable to true it enables Two-Dimensional-Paging
46 * where the hardware walks 2 page tables:
47 * 1. the guest-virtual to guest-physical
48 * 2. while doing 1. it walks guest-physical to host-physical
49 * If the hardware supports that we don't need to do shadow paging.
50 */
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050051bool tdp_enabled = false;
Joerg Roedel18552672008-02-07 13:47:41 +010052
Xiao Guangrong8b1fe172010-08-30 18:22:53 +080053enum {
54 AUDIT_PRE_PAGE_FAULT,
55 AUDIT_POST_PAGE_FAULT,
56 AUDIT_PRE_PTE_WRITE,
Xiao Guangrong69030742010-09-27 18:09:29 +080057 AUDIT_POST_PTE_WRITE,
58 AUDIT_PRE_SYNC,
59 AUDIT_POST_SYNC
Xiao Guangrong8b1fe172010-08-30 18:22:53 +080060};
61
62char *audit_point_name[] = {
63 "pre page fault",
64 "post page fault",
65 "pre pte write",
Xiao Guangrong69030742010-09-27 18:09:29 +080066 "post pte write",
67 "pre sync",
68 "post sync"
Xiao Guangrong8b1fe172010-08-30 18:22:53 +080069};
70
Xiao Guangrong0375f7f2011-11-28 20:41:00 +080071#ifdef CONFIG_KVM_MMU_AUDIT
72static void kvm_mmu_audit(struct kvm_vcpu *vcpu, int point);
73#else
74static void kvm_mmu_audit(struct kvm_vcpu *vcpu, int point) { }
75#endif
76
Avi Kivity37a7d8b2007-01-05 16:36:56 -080077#undef MMU_DEBUG
78
Avi Kivity37a7d8b2007-01-05 16:36:56 -080079#ifdef MMU_DEBUG
80
81#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
82#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
83
84#else
85
86#define pgprintk(x...) do { } while (0)
87#define rmap_printk(x...) do { } while (0)
88
89#endif
90
Xiao Guangrong8b1fe172010-08-30 18:22:53 +080091#ifdef MMU_DEBUG
Avi Kivity6ada8cc2008-06-22 16:45:24 +030092static int dbg = 0;
93module_param(dbg, bool, 0644);
Avi Kivity37a7d8b2007-01-05 16:36:56 -080094#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080095
Marcelo Tosatti582801a2008-09-23 13:18:41 -030096static int oos_shadow = 1;
97module_param(oos_shadow, bool, 0644);
98
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080099#ifndef MMU_DEBUG
100#define ASSERT(x) do { } while (0)
101#else
Avi Kivity6aa8b732006-12-10 02:21:36 -0800102#define ASSERT(x) \
103 if (!(x)) { \
104 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
105 __FILE__, __LINE__, #x); \
106 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800107#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800108
Xiao Guangrong957ed9e2010-08-22 19:12:48 +0800109#define PTE_PREFETCH_NUM 8
110
Avi Kivity6aa8b732006-12-10 02:21:36 -0800111#define PT_FIRST_AVAIL_BITS_SHIFT 9
112#define PT64_SECOND_AVAIL_BITS_SHIFT 52
113
Avi Kivity6aa8b732006-12-10 02:21:36 -0800114#define PT64_LEVEL_BITS 9
115
116#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400117 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800118
Avi Kivity6aa8b732006-12-10 02:21:36 -0800119#define PT64_INDEX(address, level)\
120 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
121
122
123#define PT32_LEVEL_BITS 10
124
125#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400126 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800127
Joerg Roedele04da982009-07-27 16:30:45 +0200128#define PT32_LVL_OFFSET_MASK(level) \
129 (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
130 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131
132#define PT32_INDEX(address, level)\
133 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
134
135
Avi Kivity27aba762007-03-09 13:04:31 +0200136#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800137#define PT64_DIR_BASE_ADDR_MASK \
138 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200139#define PT64_LVL_ADDR_MASK(level) \
140 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
141 * PT64_LEVEL_BITS))) - 1))
142#define PT64_LVL_OFFSET_MASK(level) \
143 (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
144 * PT64_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800145
146#define PT32_BASE_ADDR_MASK PAGE_MASK
147#define PT32_DIR_BASE_ADDR_MASK \
148 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200149#define PT32_LVL_ADDR_MASK(level) \
150 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
151 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800152
Avi Kivity79539ce2007-11-21 02:06:21 +0200153#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
154 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800155
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800156#define PTE_LIST_EXT 4
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800157
Avi Kivityfe135d22007-12-09 16:15:46 +0200158#define ACC_EXEC_MASK 1
159#define ACC_WRITE_MASK PT_WRITABLE_MASK
160#define ACC_USER_MASK PT_USER_MASK
161#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
162
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200163#include <trace/events/kvm.h>
164
Avi Kivity07420172009-07-06 12:21:32 +0300165#define CREATE_TRACE_POINTS
166#include "mmutrace.h"
167
Izik Eidus14032832009-09-23 21:47:17 +0300168#define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
169
Avi Kivity135f8c22008-08-21 17:49:56 +0300170#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
171
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800172struct pte_list_desc {
173 u64 *sptes[PTE_LIST_EXT];
174 struct pte_list_desc *more;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800175};
176
Avi Kivity2d111232008-12-25 14:39:47 +0200177struct kvm_shadow_walk_iterator {
178 u64 addr;
179 hpa_t shadow_addr;
Avi Kivity2d111232008-12-25 14:39:47 +0200180 u64 *sptep;
Xiao Guangrongdd3bfd52011-07-12 03:32:54 +0800181 int level;
Avi Kivity2d111232008-12-25 14:39:47 +0200182 unsigned index;
183};
184
185#define for_each_shadow_entry(_vcpu, _addr, _walker) \
186 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
187 shadow_walk_okay(&(_walker)); \
188 shadow_walk_next(&(_walker)))
189
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800190#define for_each_shadow_entry_lockless(_vcpu, _addr, _walker, spte) \
191 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
192 shadow_walk_okay(&(_walker)) && \
193 ({ spte = mmu_spte_get_lockless(_walker.sptep); 1; }); \
194 __shadow_walk_next(&(_walker), spte))
195
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800196static struct kmem_cache *pte_list_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300197static struct kmem_cache *mmu_page_header_cache;
Dave Hansen45221ab2010-08-19 18:11:37 -0700198static struct percpu_counter kvm_total_used_mmu_pages;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300199
Sheng Yang7b523452008-04-25 21:13:50 +0800200static u64 __read_mostly shadow_nx_mask;
201static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
202static u64 __read_mostly shadow_user_mask;
203static u64 __read_mostly shadow_accessed_mask;
204static u64 __read_mostly shadow_dirty_mask;
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800205static u64 __read_mostly shadow_mmio_mask;
206
207static void mmu_spte_set(u64 *sptep, u64 spte);
208
209void kvm_mmu_set_mmio_spte_mask(u64 mmio_mask)
210{
211 shadow_mmio_mask = mmio_mask;
212}
213EXPORT_SYMBOL_GPL(kvm_mmu_set_mmio_spte_mask);
214
215static void mark_mmio_spte(u64 *sptep, u64 gfn, unsigned access)
216{
217 access &= ACC_WRITE_MASK | ACC_USER_MASK;
218
Xiao Guangrong4f022642011-07-12 03:34:24 +0800219 trace_mark_mmio_spte(sptep, gfn, access);
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800220 mmu_spte_set(sptep, shadow_mmio_mask | access | gfn << PAGE_SHIFT);
221}
222
223static bool is_mmio_spte(u64 spte)
224{
225 return (spte & shadow_mmio_mask) == shadow_mmio_mask;
226}
227
228static gfn_t get_mmio_spte_gfn(u64 spte)
229{
230 return (spte & ~shadow_mmio_mask) >> PAGE_SHIFT;
231}
232
233static unsigned get_mmio_spte_access(u64 spte)
234{
235 return (spte & ~shadow_mmio_mask) & ~PAGE_MASK;
236}
237
238static bool set_mmio_spte(u64 *sptep, gfn_t gfn, pfn_t pfn, unsigned access)
239{
240 if (unlikely(is_noslot_pfn(pfn))) {
241 mark_mmio_spte(sptep, gfn, access);
242 return true;
243 }
244
245 return false;
246}
Avi Kivityc7addb92007-09-16 18:58:32 +0200247
Dong, Eddie82725b22009-03-30 16:21:08 +0800248static inline u64 rsvd_bits(int s, int e)
249{
250 return ((1ULL << (e - s + 1)) - 1) << s;
251}
252
Sheng Yang7b523452008-04-25 21:13:50 +0800253void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang4b12f0d2009-04-27 20:35:42 +0800254 u64 dirty_mask, u64 nx_mask, u64 x_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800255{
256 shadow_user_mask = user_mask;
257 shadow_accessed_mask = accessed_mask;
258 shadow_dirty_mask = dirty_mask;
259 shadow_nx_mask = nx_mask;
260 shadow_x_mask = x_mask;
261}
262EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
263
Avi Kivity6aa8b732006-12-10 02:21:36 -0800264static int is_cpuid_PSE36(void)
265{
266 return 1;
267}
268
Avi Kivity73b10872007-01-26 00:56:41 -0800269static int is_nx(struct kvm_vcpu *vcpu)
270{
Avi Kivityf6801df2010-01-21 15:31:50 +0200271 return vcpu->arch.efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800272}
273
Avi Kivityc7addb92007-09-16 18:58:32 +0200274static int is_shadow_present_pte(u64 pte)
275{
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800276 return pte & PT_PRESENT_MASK && !is_mmio_spte(pte);
Avi Kivityc7addb92007-09-16 18:58:32 +0200277}
278
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300279static int is_large_pte(u64 pte)
280{
281 return pte & PT_PAGE_SIZE_MASK;
282}
283
Avi Kivity43a37952009-06-10 14:12:05 +0300284static int is_dirty_gpte(unsigned long pte)
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200285{
Avi Kivity439e2182009-06-10 12:56:54 +0300286 return pte & PT_DIRTY_MASK;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200287}
288
Avi Kivity43a37952009-06-10 14:12:05 +0300289static int is_rmap_spte(u64 pte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800290{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200291 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800292}
293
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300294static int is_last_spte(u64 pte, int level)
295{
296 if (level == PT_PAGE_TABLE_LEVEL)
297 return 1;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200298 if (is_large_pte(pte))
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300299 return 1;
300 return 0;
301}
302
Anthony Liguori35149e22008-04-02 14:46:56 -0500303static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200304{
Anthony Liguori35149e22008-04-02 14:46:56 -0500305 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200306}
307
Avi Kivityda9285212007-11-21 13:54:47 +0200308static gfn_t pse36_gfn_delta(u32 gpte)
309{
310 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
311
312 return (gpte & PT32_DIR_PSE36_MASK) << shift;
313}
314
Xiao Guangrong603e0652011-07-12 03:31:28 +0800315#ifdef CONFIG_X86_64
Avi Kivityd555c332009-06-10 14:24:23 +0300316static void __set_spte(u64 *sptep, u64 spte)
Avi Kivitye663ee62007-05-31 15:46:04 +0300317{
Xiao Guangrong603e0652011-07-12 03:31:28 +0800318 *sptep = spte;
Avi Kivitye663ee62007-05-31 15:46:04 +0300319}
320
Xiao Guangrong603e0652011-07-12 03:31:28 +0800321static void __update_clear_spte_fast(u64 *sptep, u64 spte)
Avi Kivitya9221dd2010-06-06 14:48:06 +0300322{
Xiao Guangrong603e0652011-07-12 03:31:28 +0800323 *sptep = spte;
Avi Kivitya9221dd2010-06-06 14:48:06 +0300324}
325
Xiao Guangrong603e0652011-07-12 03:31:28 +0800326static u64 __update_clear_spte_slow(u64 *sptep, u64 spte)
327{
328 return xchg(sptep, spte);
329}
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800330
331static u64 __get_spte_lockless(u64 *sptep)
332{
333 return ACCESS_ONCE(*sptep);
334}
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800335
336static bool __check_direct_spte_mmio_pf(u64 spte)
337{
338 /* It is valid if the spte is zapped. */
339 return spte == 0ull;
340}
Xiao Guangrong603e0652011-07-12 03:31:28 +0800341#else
342union split_spte {
343 struct {
344 u32 spte_low;
345 u32 spte_high;
346 };
347 u64 spte;
348};
349
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800350static void count_spte_clear(u64 *sptep, u64 spte)
351{
352 struct kvm_mmu_page *sp = page_header(__pa(sptep));
353
354 if (is_shadow_present_pte(spte))
355 return;
356
357 /* Ensure the spte is completely set before we increase the count */
358 smp_wmb();
359 sp->clear_spte_count++;
360}
361
Xiao Guangrong603e0652011-07-12 03:31:28 +0800362static void __set_spte(u64 *sptep, u64 spte)
363{
364 union split_spte *ssptep, sspte;
365
366 ssptep = (union split_spte *)sptep;
367 sspte = (union split_spte)spte;
368
369 ssptep->spte_high = sspte.spte_high;
370
371 /*
372 * If we map the spte from nonpresent to present, We should store
373 * the high bits firstly, then set present bit, so cpu can not
374 * fetch this spte while we are setting the spte.
375 */
376 smp_wmb();
377
378 ssptep->spte_low = sspte.spte_low;
379}
380
381static void __update_clear_spte_fast(u64 *sptep, u64 spte)
382{
383 union split_spte *ssptep, sspte;
384
385 ssptep = (union split_spte *)sptep;
386 sspte = (union split_spte)spte;
387
388 ssptep->spte_low = sspte.spte_low;
389
390 /*
391 * If we map the spte from present to nonpresent, we should clear
392 * present bit firstly to avoid vcpu fetch the old high bits.
393 */
394 smp_wmb();
395
396 ssptep->spte_high = sspte.spte_high;
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800397 count_spte_clear(sptep, spte);
Xiao Guangrong603e0652011-07-12 03:31:28 +0800398}
399
400static u64 __update_clear_spte_slow(u64 *sptep, u64 spte)
401{
402 union split_spte *ssptep, sspte, orig;
403
404 ssptep = (union split_spte *)sptep;
405 sspte = (union split_spte)spte;
406
407 /* xchg acts as a barrier before the setting of the high bits */
408 orig.spte_low = xchg(&ssptep->spte_low, sspte.spte_low);
Zhao Jin41bc3182011-09-19 12:19:51 +0800409 orig.spte_high = ssptep->spte_high;
410 ssptep->spte_high = sspte.spte_high;
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800411 count_spte_clear(sptep, spte);
Xiao Guangrong603e0652011-07-12 03:31:28 +0800412
413 return orig.spte;
414}
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800415
416/*
417 * The idea using the light way get the spte on x86_32 guest is from
418 * gup_get_pte(arch/x86/mm/gup.c).
419 * The difference is we can not catch the spte tlb flush if we leave
420 * guest mode, so we emulate it by increase clear_spte_count when spte
421 * is cleared.
422 */
423static u64 __get_spte_lockless(u64 *sptep)
424{
425 struct kvm_mmu_page *sp = page_header(__pa(sptep));
426 union split_spte spte, *orig = (union split_spte *)sptep;
427 int count;
428
429retry:
430 count = sp->clear_spte_count;
431 smp_rmb();
432
433 spte.spte_low = orig->spte_low;
434 smp_rmb();
435
436 spte.spte_high = orig->spte_high;
437 smp_rmb();
438
439 if (unlikely(spte.spte_low != orig->spte_low ||
440 count != sp->clear_spte_count))
441 goto retry;
442
443 return spte.spte;
444}
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800445
446static bool __check_direct_spte_mmio_pf(u64 spte)
447{
448 union split_spte sspte = (union split_spte)spte;
449 u32 high_mmio_mask = shadow_mmio_mask >> 32;
450
451 /* It is valid if the spte is zapped. */
452 if (spte == 0ull)
453 return true;
454
455 /* It is valid if the spte is being zapped. */
456 if (sspte.spte_low == 0ull &&
457 (sspte.spte_high & high_mmio_mask) == high_mmio_mask)
458 return true;
459
460 return false;
461}
Xiao Guangrong603e0652011-07-12 03:31:28 +0800462#endif
463
Xiao Guangrong8672b722010-08-02 16:14:04 +0800464static bool spte_has_volatile_bits(u64 spte)
465{
466 if (!shadow_accessed_mask)
467 return false;
468
469 if (!is_shadow_present_pte(spte))
470 return false;
471
Xiao Guangrong41327792010-08-02 16:15:08 +0800472 if ((spte & shadow_accessed_mask) &&
473 (!is_writable_pte(spte) || (spte & shadow_dirty_mask)))
Xiao Guangrong8672b722010-08-02 16:14:04 +0800474 return false;
475
476 return true;
477}
478
Xiao Guangrong41327792010-08-02 16:15:08 +0800479static bool spte_is_bit_cleared(u64 old_spte, u64 new_spte, u64 bit_mask)
480{
481 return (old_spte & bit_mask) && !(new_spte & bit_mask);
482}
483
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800484/* Rules for using mmu_spte_set:
485 * Set the sptep from nonpresent to present.
486 * Note: the sptep being assigned *must* be either not present
487 * or in a state where the hardware will not attempt to update
488 * the spte.
489 */
490static void mmu_spte_set(u64 *sptep, u64 new_spte)
491{
492 WARN_ON(is_shadow_present_pte(*sptep));
493 __set_spte(sptep, new_spte);
494}
495
496/* Rules for using mmu_spte_update:
497 * Update the state bits, it means the mapped pfn is not changged.
498 */
499static void mmu_spte_update(u64 *sptep, u64 new_spte)
Avi Kivityb79b93f2010-06-06 15:46:44 +0300500{
Xiao Guangrong41327792010-08-02 16:15:08 +0800501 u64 mask, old_spte = *sptep;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300502
Xiao Guangrong41327792010-08-02 16:15:08 +0800503 WARN_ON(!is_rmap_spte(new_spte));
504
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800505 if (!is_shadow_present_pte(old_spte))
506 return mmu_spte_set(sptep, new_spte);
507
Xiao Guangrong41327792010-08-02 16:15:08 +0800508 new_spte |= old_spte & shadow_dirty_mask;
509
510 mask = shadow_accessed_mask;
511 if (is_writable_pte(old_spte))
512 mask |= shadow_dirty_mask;
513
514 if (!spte_has_volatile_bits(old_spte) || (new_spte & mask) == mask)
Xiao Guangrong603e0652011-07-12 03:31:28 +0800515 __update_clear_spte_fast(sptep, new_spte);
Xiao Guangrong41327792010-08-02 16:15:08 +0800516 else
Xiao Guangrong603e0652011-07-12 03:31:28 +0800517 old_spte = __update_clear_spte_slow(sptep, new_spte);
Xiao Guangrong41327792010-08-02 16:15:08 +0800518
519 if (!shadow_accessed_mask)
520 return;
521
522 if (spte_is_bit_cleared(old_spte, new_spte, shadow_accessed_mask))
523 kvm_set_pfn_accessed(spte_to_pfn(old_spte));
524 if (spte_is_bit_cleared(old_spte, new_spte, shadow_dirty_mask))
525 kvm_set_pfn_dirty(spte_to_pfn(old_spte));
Avi Kivityb79b93f2010-06-06 15:46:44 +0300526}
527
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800528/*
529 * Rules for using mmu_spte_clear_track_bits:
530 * It sets the sptep from present to nonpresent, and track the
531 * state bits, it is used to clear the last level sptep.
532 */
533static int mmu_spte_clear_track_bits(u64 *sptep)
534{
535 pfn_t pfn;
536 u64 old_spte = *sptep;
537
538 if (!spte_has_volatile_bits(old_spte))
Xiao Guangrong603e0652011-07-12 03:31:28 +0800539 __update_clear_spte_fast(sptep, 0ull);
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800540 else
Xiao Guangrong603e0652011-07-12 03:31:28 +0800541 old_spte = __update_clear_spte_slow(sptep, 0ull);
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800542
543 if (!is_rmap_spte(old_spte))
544 return 0;
545
546 pfn = spte_to_pfn(old_spte);
547 if (!shadow_accessed_mask || old_spte & shadow_accessed_mask)
548 kvm_set_pfn_accessed(pfn);
549 if (!shadow_dirty_mask || (old_spte & shadow_dirty_mask))
550 kvm_set_pfn_dirty(pfn);
551 return 1;
552}
553
554/*
555 * Rules for using mmu_spte_clear_no_track:
556 * Directly clear spte without caring the state bits of sptep,
557 * it is used to set the upper level spte.
558 */
559static void mmu_spte_clear_no_track(u64 *sptep)
560{
Xiao Guangrong603e0652011-07-12 03:31:28 +0800561 __update_clear_spte_fast(sptep, 0ull);
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800562}
563
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800564static u64 mmu_spte_get_lockless(u64 *sptep)
565{
566 return __get_spte_lockless(sptep);
567}
568
569static void walk_shadow_page_lockless_begin(struct kvm_vcpu *vcpu)
570{
571 rcu_read_lock();
572 atomic_inc(&vcpu->kvm->arch.reader_counter);
573
574 /* Increase the counter before walking shadow page table */
575 smp_mb__after_atomic_inc();
576}
577
578static void walk_shadow_page_lockless_end(struct kvm_vcpu *vcpu)
579{
580 /* Decrease the counter after walking shadow page table finished */
581 smp_mb__before_atomic_dec();
582 atomic_dec(&vcpu->kvm->arch.reader_counter);
583 rcu_read_unlock();
584}
585
Avi Kivitye2dec932007-01-05 16:36:54 -0800586static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300587 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800588{
589 void *obj;
590
591 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800592 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800593 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300594 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800595 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800596 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800597 cache->objects[cache->nobjs++] = obj;
598 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800599 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800600}
601
Xiao Guangrongf759e2b2011-09-22 16:53:17 +0800602static int mmu_memory_cache_free_objects(struct kvm_mmu_memory_cache *cache)
603{
604 return cache->nobjs;
605}
606
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800607static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc,
608 struct kmem_cache *cache)
Avi Kivity714b93d2007-01-05 16:36:53 -0800609{
610 while (mc->nobjs)
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800611 kmem_cache_free(cache, mc->objects[--mc->nobjs]);
Avi Kivity714b93d2007-01-05 16:36:53 -0800612}
613
Avi Kivityc1158e62007-07-20 08:18:27 +0300614static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300615 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300616{
Xiao Guangrong842f22e2011-03-04 19:01:10 +0800617 void *page;
Avi Kivityc1158e62007-07-20 08:18:27 +0300618
619 if (cache->nobjs >= min)
620 return 0;
621 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Xiao Guangrong842f22e2011-03-04 19:01:10 +0800622 page = (void *)__get_free_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300623 if (!page)
624 return -ENOMEM;
Xiao Guangrong842f22e2011-03-04 19:01:10 +0800625 cache->objects[cache->nobjs++] = page;
Avi Kivityc1158e62007-07-20 08:18:27 +0300626 }
627 return 0;
628}
629
630static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
631{
632 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300633 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300634}
635
Avi Kivity8c438502007-04-16 11:53:17 +0300636static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
637{
638 int r;
639
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800640 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
Xiao Guangrong67052b32011-05-15 23:27:08 +0800641 pte_list_desc_cache, 8 + PTE_PREFETCH_NUM);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300642 if (r)
643 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800644 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300645 if (r)
646 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800647 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300648 mmu_page_header_cache, 4);
649out:
Avi Kivity8c438502007-04-16 11:53:17 +0300650 return r;
651}
652
Avi Kivity714b93d2007-01-05 16:36:53 -0800653static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
654{
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800655 mmu_free_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
656 pte_list_desc_cache);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800657 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800658 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache,
659 mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800660}
661
662static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
663 size_t size)
664{
665 void *p;
666
667 BUG_ON(!mc->nobjs);
668 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800669 return p;
670}
671
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800672static struct pte_list_desc *mmu_alloc_pte_list_desc(struct kvm_vcpu *vcpu)
Avi Kivity714b93d2007-01-05 16:36:53 -0800673{
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800674 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_list_desc_cache,
675 sizeof(struct pte_list_desc));
Avi Kivity714b93d2007-01-05 16:36:53 -0800676}
677
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800678static void mmu_free_pte_list_desc(struct pte_list_desc *pte_list_desc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800679{
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800680 kmem_cache_free(pte_list_desc_cache, pte_list_desc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800681}
682
Lai Jiangshan2032a932010-05-26 16:49:59 +0800683static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
684{
685 if (!sp->role.direct)
686 return sp->gfns[index];
687
688 return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS));
689}
690
691static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn)
692{
693 if (sp->role.direct)
694 BUG_ON(gfn != kvm_mmu_page_get_gfn(sp, index));
695 else
696 sp->gfns[index] = gfn;
697}
698
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800699/*
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900700 * Return the pointer to the large page information for a given gfn,
701 * handling slots that are not large page aligned.
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300702 */
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900703static struct kvm_lpage_info *lpage_info_slot(gfn_t gfn,
704 struct kvm_memory_slot *slot,
705 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300706{
707 unsigned long idx;
708
Joerg Roedel82855412010-07-01 16:00:11 +0200709 idx = (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
710 (slot->base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900711 return &slot->lpage_info[level - 2][idx];
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300712}
713
714static void account_shadowed(struct kvm *kvm, gfn_t gfn)
715{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200716 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900717 struct kvm_lpage_info *linfo;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200718 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300719
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300720 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200721 for (i = PT_DIRECTORY_LEVEL;
722 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900723 linfo = lpage_info_slot(gfn, slot, i);
724 linfo->write_count += 1;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200725 }
Xiao Guangrong332b2072011-05-15 23:20:27 +0800726 kvm->arch.indirect_shadow_pages++;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300727}
728
729static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
730{
Joerg Roedeld25797b2009-07-27 16:30:43 +0200731 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900732 struct kvm_lpage_info *linfo;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200733 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300734
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300735 slot = gfn_to_memslot(kvm, gfn);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200736 for (i = PT_DIRECTORY_LEVEL;
737 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900738 linfo = lpage_info_slot(gfn, slot, i);
739 linfo->write_count -= 1;
740 WARN_ON(linfo->write_count < 0);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200741 }
Xiao Guangrong332b2072011-05-15 23:20:27 +0800742 kvm->arch.indirect_shadow_pages--;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300743}
744
Joerg Roedeld25797b2009-07-27 16:30:43 +0200745static int has_wrprotected_page(struct kvm *kvm,
746 gfn_t gfn,
747 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300748{
Izik Eidus28430992008-10-03 17:40:32 +0300749 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900750 struct kvm_lpage_info *linfo;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300751
Avi Kivitya1f4d3952010-06-21 11:44:20 +0300752 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300753 if (slot) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900754 linfo = lpage_info_slot(gfn, slot, level);
755 return linfo->write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300756 }
757
758 return 1;
759}
760
Joerg Roedeld25797b2009-07-27 16:30:43 +0200761static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300762{
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100763 unsigned long page_size;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200764 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300765
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100766 page_size = kvm_host_page_size(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300767
Joerg Roedeld25797b2009-07-27 16:30:43 +0200768 for (i = PT_PAGE_TABLE_LEVEL;
769 i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
770 if (page_size >= KVM_HPAGE_SIZE(i))
771 ret = i;
772 else
773 break;
774 }
775
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300776 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300777}
778
Xiao Guangrong5d163b12011-03-09 15:43:00 +0800779static struct kvm_memory_slot *
780gfn_to_memslot_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t gfn,
781 bool no_dirty_log)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300782{
783 struct kvm_memory_slot *slot;
Xiao Guangrong5d163b12011-03-09 15:43:00 +0800784
785 slot = gfn_to_memslot(vcpu->kvm, gfn);
786 if (!slot || slot->flags & KVM_MEMSLOT_INVALID ||
787 (no_dirty_log && slot->dirty_bitmap))
788 slot = NULL;
789
790 return slot;
791}
792
793static bool mapping_level_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t large_gfn)
794{
Stevea0a8eab2011-06-17 10:25:39 +0800795 return !gfn_to_memslot_dirty_bitmap(vcpu, large_gfn, true);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800796}
797
798static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
799{
800 int host_level, level, max_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300801
Joerg Roedeld25797b2009-07-27 16:30:43 +0200802 host_level = host_mapping_level(vcpu->kvm, large_gfn);
803
804 if (host_level == PT_PAGE_TABLE_LEVEL)
805 return host_level;
806
Sheng Yang878403b2010-01-05 19:02:29 +0800807 max_level = kvm_x86_ops->get_lpage_level() < host_level ?
808 kvm_x86_ops->get_lpage_level() : host_level;
809
810 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
Joerg Roedeld25797b2009-07-27 16:30:43 +0200811 if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
812 break;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200813
814 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300815}
816
817/*
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800818 * Pte mapping structures:
819 *
820 * If pte_list bit zero is zero, then pte_list point to the spte.
821 *
822 * If pte_list bit zero is one, (then pte_list & ~1) points to a struct
823 * pte_list_desc containing more mappings.
824 *
825 * Returns the number of pte entries before the spte was added or zero if
826 * the spte was not added.
827 *
828 */
829static int pte_list_add(struct kvm_vcpu *vcpu, u64 *spte,
830 unsigned long *pte_list)
831{
832 struct pte_list_desc *desc;
833 int i, count = 0;
834
835 if (!*pte_list) {
836 rmap_printk("pte_list_add: %p %llx 0->1\n", spte, *spte);
837 *pte_list = (unsigned long)spte;
838 } else if (!(*pte_list & 1)) {
839 rmap_printk("pte_list_add: %p %llx 1->many\n", spte, *spte);
840 desc = mmu_alloc_pte_list_desc(vcpu);
841 desc->sptes[0] = (u64 *)*pte_list;
842 desc->sptes[1] = spte;
843 *pte_list = (unsigned long)desc | 1;
844 ++count;
845 } else {
846 rmap_printk("pte_list_add: %p %llx many->many\n", spte, *spte);
847 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
848 while (desc->sptes[PTE_LIST_EXT-1] && desc->more) {
849 desc = desc->more;
850 count += PTE_LIST_EXT;
851 }
852 if (desc->sptes[PTE_LIST_EXT-1]) {
853 desc->more = mmu_alloc_pte_list_desc(vcpu);
854 desc = desc->more;
855 }
856 for (i = 0; desc->sptes[i]; ++i)
857 ++count;
858 desc->sptes[i] = spte;
859 }
860 return count;
861}
862
863static u64 *pte_list_next(unsigned long *pte_list, u64 *spte)
864{
865 struct pte_list_desc *desc;
866 u64 *prev_spte;
867 int i;
868
869 if (!*pte_list)
870 return NULL;
871 else if (!(*pte_list & 1)) {
872 if (!spte)
873 return (u64 *)*pte_list;
874 return NULL;
875 }
876 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
877 prev_spte = NULL;
878 while (desc) {
879 for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i) {
880 if (prev_spte == spte)
881 return desc->sptes[i];
882 prev_spte = desc->sptes[i];
883 }
884 desc = desc->more;
885 }
886 return NULL;
887}
888
889static void
890pte_list_desc_remove_entry(unsigned long *pte_list, struct pte_list_desc *desc,
891 int i, struct pte_list_desc *prev_desc)
892{
893 int j;
894
895 for (j = PTE_LIST_EXT - 1; !desc->sptes[j] && j > i; --j)
896 ;
897 desc->sptes[i] = desc->sptes[j];
898 desc->sptes[j] = NULL;
899 if (j != 0)
900 return;
901 if (!prev_desc && !desc->more)
902 *pte_list = (unsigned long)desc->sptes[0];
903 else
904 if (prev_desc)
905 prev_desc->more = desc->more;
906 else
907 *pte_list = (unsigned long)desc->more | 1;
908 mmu_free_pte_list_desc(desc);
909}
910
911static void pte_list_remove(u64 *spte, unsigned long *pte_list)
912{
913 struct pte_list_desc *desc;
914 struct pte_list_desc *prev_desc;
915 int i;
916
917 if (!*pte_list) {
918 printk(KERN_ERR "pte_list_remove: %p 0->BUG\n", spte);
919 BUG();
920 } else if (!(*pte_list & 1)) {
921 rmap_printk("pte_list_remove: %p 1->0\n", spte);
922 if ((u64 *)*pte_list != spte) {
923 printk(KERN_ERR "pte_list_remove: %p 1->BUG\n", spte);
924 BUG();
925 }
926 *pte_list = 0;
927 } else {
928 rmap_printk("pte_list_remove: %p many->many\n", spte);
929 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
930 prev_desc = NULL;
931 while (desc) {
932 for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i)
933 if (desc->sptes[i] == spte) {
934 pte_list_desc_remove_entry(pte_list,
935 desc, i,
936 prev_desc);
937 return;
938 }
939 prev_desc = desc;
940 desc = desc->more;
941 }
942 pr_err("pte_list_remove: %p many->many\n", spte);
943 BUG();
944 }
945}
946
Xiao Guangrong67052b32011-05-15 23:27:08 +0800947typedef void (*pte_list_walk_fn) (u64 *spte);
948static void pte_list_walk(unsigned long *pte_list, pte_list_walk_fn fn)
949{
950 struct pte_list_desc *desc;
951 int i;
952
953 if (!*pte_list)
954 return;
955
956 if (!(*pte_list & 1))
957 return fn((u64 *)*pte_list);
958
959 desc = (struct pte_list_desc *)(*pte_list & ~1ul);
960 while (desc) {
961 for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i)
962 fn(desc->sptes[i]);
963 desc = desc->more;
964 }
965}
966
Takuya Yoshikawa9b9b1492011-11-14 18:22:28 +0900967static unsigned long *__gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level,
968 struct kvm_memory_slot *slot)
969{
970 struct kvm_lpage_info *linfo;
971
972 if (likely(level == PT_PAGE_TABLE_LEVEL))
973 return &slot->rmap[gfn - slot->base_gfn];
974
975 linfo = lpage_info_slot(gfn, slot, level);
976 return &linfo->rmap_pde;
977}
978
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800979/*
Izik Eidus290fc382007-09-27 14:11:22 +0200980 * Take gfn and return the reverse mapping to it.
Izik Eidus290fc382007-09-27 14:11:22 +0200981 */
Joerg Roedel44ad9942009-07-27 16:30:42 +0200982static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
Izik Eidus290fc382007-09-27 14:11:22 +0200983{
984 struct kvm_memory_slot *slot;
985
986 slot = gfn_to_memslot(kvm, gfn);
Takuya Yoshikawa9b9b1492011-11-14 18:22:28 +0900987 return __gfn_to_rmap(kvm, gfn, level, slot);
Izik Eidus290fc382007-09-27 14:11:22 +0200988}
989
Xiao Guangrongf759e2b2011-09-22 16:53:17 +0800990static bool rmap_can_add(struct kvm_vcpu *vcpu)
991{
992 struct kvm_mmu_memory_cache *cache;
993
994 cache = &vcpu->arch.mmu_pte_list_desc_cache;
995 return mmu_memory_cache_free_objects(cache);
996}
997
Joerg Roedel44ad9942009-07-27 16:30:42 +0200998static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800999{
Avi Kivity4db35312007-11-21 15:28:32 +02001000 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02001001 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001002
Avi Kivity4db35312007-11-21 15:28:32 +02001003 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +08001004 kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn);
Joerg Roedel44ad9942009-07-27 16:30:42 +02001005 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001006 return pte_list_add(vcpu, spte, rmapp);
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001007}
1008
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001009static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001010{
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001011 return pte_list_next(rmapp, spte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001012}
1013
Izik Eidus290fc382007-09-27 14:11:22 +02001014static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001015{
Avi Kivity4db35312007-11-21 15:28:32 +02001016 struct kvm_mmu_page *sp;
Lai Jiangshan2032a932010-05-26 16:49:59 +08001017 gfn_t gfn;
Izik Eidus290fc382007-09-27 14:11:22 +02001018 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001019
Avi Kivity4db35312007-11-21 15:28:32 +02001020 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +08001021 gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt);
1022 rmapp = gfn_to_rmap(kvm, gfn, sp->role.level);
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001023 pte_list_remove(spte, rmapp);
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001024}
1025
Xiao Guangrongc3707952011-07-12 03:28:04 +08001026static void drop_spte(struct kvm *kvm, u64 *sptep)
Xiao Guangronge4b502e2010-07-16 11:28:09 +08001027{
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08001028 if (mmu_spte_clear_track_bits(sptep))
Marcelo Tosattieb45fda2010-10-25 11:58:22 -02001029 rmap_remove(kvm, sptep);
Avi Kivitybe38d272010-06-06 14:31:27 +03001030}
1031
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09001032int kvm_mmu_rmap_write_protect(struct kvm *kvm, u64 gfn,
1033 struct kvm_memory_slot *slot)
Izik Eidus98348e92007-10-16 14:42:30 +02001034{
Izik Eidus290fc382007-09-27 14:11:22 +02001035 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -08001036 u64 *spte;
Joerg Roedel44ad9942009-07-27 16:30:42 +02001037 int i, write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -08001038
Takuya Yoshikawa9b9b1492011-11-14 18:22:28 +09001039 rmapp = __gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL, slot);
Izik Eidus98348e92007-10-16 14:42:30 +02001040 spte = rmap_next(kvm, rmapp, NULL);
1041 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -08001042 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -08001043 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001044 if (is_writable_pte(*spte)) {
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08001045 mmu_spte_update(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +08001046 write_protected = 1;
1047 }
Izik Eidus9647c142007-10-16 14:43:46 +02001048 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -08001049 }
Izik Eidus855149a2008-03-20 18:17:24 +02001050
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001051 /* check for huge page mappings */
Joerg Roedel44ad9942009-07-27 16:30:42 +02001052 for (i = PT_DIRECTORY_LEVEL;
1053 i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
Takuya Yoshikawa9b9b1492011-11-14 18:22:28 +09001054 rmapp = __gfn_to_rmap(kvm, gfn, i, slot);
Joerg Roedel44ad9942009-07-27 16:30:42 +02001055 spte = rmap_next(kvm, rmapp, NULL);
1056 while (spte) {
Joerg Roedel44ad9942009-07-27 16:30:42 +02001057 BUG_ON(!(*spte & PT_PRESENT_MASK));
Takuya Yoshikawad6eebf82011-11-14 18:21:34 +09001058 BUG_ON(!is_large_pte(*spte));
Joerg Roedel44ad9942009-07-27 16:30:42 +02001059 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09001060 if (is_writable_pte(*spte)) {
Xiao Guangrongc3707952011-07-12 03:28:04 +08001061 drop_spte(kvm, spte);
Joerg Roedel44ad9942009-07-27 16:30:42 +02001062 --kvm->stat.lpages;
Joerg Roedel44ad9942009-07-27 16:30:42 +02001063 spte = NULL;
1064 write_protected = 1;
1065 }
1066 spte = rmap_next(kvm, rmapp, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001067 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001068 }
1069
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001070 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -08001071}
1072
Takuya Yoshikawa95d4c162011-11-14 18:24:50 +09001073static int rmap_write_protect(struct kvm *kvm, u64 gfn)
1074{
1075 struct kvm_memory_slot *slot;
1076
1077 slot = gfn_to_memslot(kvm, gfn);
1078 return kvm_mmu_rmap_write_protect(kvm, gfn, slot);
1079}
1080
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +00001081static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
1082 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001083{
1084 u64 *spte;
1085 int need_tlb_flush = 0;
1086
1087 while ((spte = rmap_next(kvm, rmapp, NULL))) {
1088 BUG_ON(!(*spte & PT_PRESENT_MASK));
1089 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
Xiao Guangrongc3707952011-07-12 03:28:04 +08001090 drop_spte(kvm, spte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001091 need_tlb_flush = 1;
1092 }
1093 return need_tlb_flush;
1094}
1095
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +00001096static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
1097 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +03001098{
1099 int need_flush = 0;
Xiao Guangronge4b502e2010-07-16 11:28:09 +08001100 u64 *spte, new_spte;
Izik Eidus3da0dd42009-09-23 21:47:18 +03001101 pte_t *ptep = (pte_t *)data;
1102 pfn_t new_pfn;
1103
1104 WARN_ON(pte_huge(*ptep));
1105 new_pfn = pte_pfn(*ptep);
1106 spte = rmap_next(kvm, rmapp, NULL);
1107 while (spte) {
1108 BUG_ON(!is_shadow_present_pte(*spte));
1109 rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
1110 need_flush = 1;
1111 if (pte_write(*ptep)) {
Xiao Guangrongc3707952011-07-12 03:28:04 +08001112 drop_spte(kvm, spte);
Izik Eidus3da0dd42009-09-23 21:47:18 +03001113 spte = rmap_next(kvm, rmapp, NULL);
1114 } else {
1115 new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
1116 new_spte |= (u64)new_pfn << PAGE_SHIFT;
1117
1118 new_spte &= ~PT_WRITABLE_MASK;
1119 new_spte &= ~SPTE_HOST_WRITEABLE;
Avi Kivityb79b93f2010-06-06 15:46:44 +03001120 new_spte &= ~shadow_accessed_mask;
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08001121 mmu_spte_clear_track_bits(spte);
1122 mmu_spte_set(spte, new_spte);
Izik Eidus3da0dd42009-09-23 21:47:18 +03001123 spte = rmap_next(kvm, rmapp, spte);
1124 }
1125 }
1126 if (need_flush)
1127 kvm_flush_remote_tlbs(kvm);
1128
1129 return 0;
1130}
1131
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +00001132static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
1133 unsigned long data,
Izik Eidus3da0dd42009-09-23 21:47:18 +03001134 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +00001135 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001136{
Xiao Guangrongbe6ba0f2011-11-24 17:39:18 +08001137 int j;
Avi Kivity90bb6fc2009-12-31 12:10:16 +02001138 int ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001139 int retval = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001140 struct kvm_memslots *slots;
Xiao Guangrongbe6ba0f2011-11-24 17:39:18 +08001141 struct kvm_memory_slot *memslot;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001142
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08001143 slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001144
Xiao Guangrongbe6ba0f2011-11-24 17:39:18 +08001145 kvm_for_each_memslot(memslot, slots) {
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001146 unsigned long start = memslot->userspace_addr;
1147 unsigned long end;
1148
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001149 end = start + (memslot->npages << PAGE_SHIFT);
1150 if (hva >= start && hva < end) {
1151 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +09001152 gfn_t gfn = memslot->base_gfn + gfn_offset;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001153
Avi Kivity90bb6fc2009-12-31 12:10:16 +02001154 ret = handler(kvm, &memslot->rmap[gfn_offset], data);
Joerg Roedel852e3c12009-07-27 16:30:44 +02001155
1156 for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +09001157 struct kvm_lpage_info *linfo;
Andrea Arcangeli6e3e2432010-07-16 11:52:55 +02001158
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +09001159 linfo = lpage_info_slot(gfn, memslot,
1160 PT_DIRECTORY_LEVEL + j);
1161 ret |= handler(kvm, &linfo->rmap_pde, data);
Joerg Roedel852e3c12009-07-27 16:30:44 +02001162 }
Avi Kivity90bb6fc2009-12-31 12:10:16 +02001163 trace_kvm_age_page(hva, memslot, ret);
1164 retval |= ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001165 }
1166 }
1167
1168 return retval;
1169}
1170
1171int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
1172{
Izik Eidus3da0dd42009-09-23 21:47:18 +03001173 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001174}
1175
Izik Eidus3da0dd42009-09-23 21:47:18 +03001176void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
1177{
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +00001178 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
Izik Eidus3da0dd42009-09-23 21:47:18 +03001179}
1180
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +00001181static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
1182 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001183{
1184 u64 *spte;
1185 int young = 0;
1186
Rik van Riel6316e1c2010-02-03 16:11:03 -05001187 /*
1188 * Emulate the accessed bit for EPT, by checking if this page has
1189 * an EPT mapping, and clearing it if it does. On the next access,
1190 * a new EPT mapping will be established.
1191 * This has some overhead, but not as much as the cost of swapping
1192 * out actively used pages or breaking up actively used hugepages.
1193 */
Sheng Yang534e38b2008-09-08 15:12:30 +08001194 if (!shadow_accessed_mask)
Rik van Riel6316e1c2010-02-03 16:11:03 -05001195 return kvm_unmap_rmapp(kvm, rmapp, data);
Sheng Yang534e38b2008-09-08 15:12:30 +08001196
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001197 spte = rmap_next(kvm, rmapp, NULL);
1198 while (spte) {
1199 int _young;
1200 u64 _spte = *spte;
1201 BUG_ON(!(_spte & PT_PRESENT_MASK));
1202 _young = _spte & PT_ACCESSED_MASK;
1203 if (_young) {
1204 young = 1;
1205 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
1206 }
1207 spte = rmap_next(kvm, rmapp, spte);
1208 }
1209 return young;
1210}
1211
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001212static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
1213 unsigned long data)
1214{
1215 u64 *spte;
1216 int young = 0;
1217
1218 /*
1219 * If there's no access bit in the secondary pte set by the
1220 * hardware it's up to gup-fast/gup to set the access bit in
1221 * the primary pte or in the page structure.
1222 */
1223 if (!shadow_accessed_mask)
1224 goto out;
1225
1226 spte = rmap_next(kvm, rmapp, NULL);
1227 while (spte) {
1228 u64 _spte = *spte;
1229 BUG_ON(!(_spte & PT_PRESENT_MASK));
1230 young = _spte & PT_ACCESSED_MASK;
1231 if (young) {
1232 young = 1;
1233 break;
1234 }
1235 spte = rmap_next(kvm, rmapp, spte);
1236 }
1237out:
1238 return young;
1239}
1240
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001241#define RMAP_RECYCLE_THRESHOLD 1000
1242
Joerg Roedel852e3c12009-07-27 16:30:44 +02001243static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001244{
1245 unsigned long *rmapp;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001246 struct kvm_mmu_page *sp;
1247
1248 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001249
Joerg Roedel852e3c12009-07-27 16:30:44 +02001250 rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001251
Izik Eidus3da0dd42009-09-23 21:47:18 +03001252 kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001253 kvm_flush_remote_tlbs(vcpu->kvm);
1254}
1255
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001256int kvm_age_hva(struct kvm *kvm, unsigned long hva)
1257{
Izik Eidus3da0dd42009-09-23 21:47:18 +03001258 return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001259}
1260
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001261int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
1262{
1263 return kvm_handle_hva(kvm, hva, 0, kvm_test_age_rmapp);
1264}
1265
Yaozu Dongd6c69ee2007-04-25 14:17:25 +08001266#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +03001267static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001268{
Avi Kivity139bdb22007-01-05 16:36:50 -08001269 u64 *pos;
1270 u64 *end;
1271
Avi Kivity47ad8e62007-05-06 15:50:58 +03001272 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +03001273 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001274 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -08001275 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001276 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -08001277 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001278 return 1;
1279}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +08001280#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001281
Dave Hansen45221ab2010-08-19 18:11:37 -07001282/*
1283 * This value is the sum of all of the kvm instances's
1284 * kvm->arch.n_used_mmu_pages values. We need a global,
1285 * aggregate version in order to make the slab shrinker
1286 * faster
1287 */
1288static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, int nr)
1289{
1290 kvm->arch.n_used_mmu_pages += nr;
1291 percpu_counter_add(&kvm_total_used_mmu_pages, nr);
1292}
1293
Xiao Guangrongbd4c86e2011-07-12 03:27:14 +08001294/*
1295 * Remove the sp from shadow page cache, after call it,
1296 * we can not find this sp from the cache, and the shadow
1297 * page table is still valid.
1298 * It should be under the protection of mmu lock.
1299 */
1300static void kvm_mmu_isolate_page(struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -08001301{
Avi Kivity4db35312007-11-21 15:28:32 +02001302 ASSERT(is_empty_shadow_page(sp->spt));
Xiao Guangrong77758342010-06-04 21:53:54 +08001303 hlist_del(&sp->hash_link);
Lai Jiangshan2032a932010-05-26 16:49:59 +08001304 if (!sp->role.direct)
Xiao Guangrong842f22e2011-03-04 19:01:10 +08001305 free_page((unsigned long)sp->gfns);
Xiao Guangrongbd4c86e2011-07-12 03:27:14 +08001306}
1307
1308/*
1309 * Free the shadow page table and the sp, we can do it
1310 * out of the protection of mmu lock.
1311 */
1312static void kvm_mmu_free_page(struct kvm_mmu_page *sp)
1313{
1314 list_del(&sp->link);
1315 free_page((unsigned long)sp->spt);
Xiao Guangronge8ad9a72010-05-13 10:06:02 +08001316 kmem_cache_free(mmu_page_header_cache, sp);
Avi Kivity260746c2007-01-05 16:36:49 -08001317}
1318
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001319static unsigned kvm_page_table_hashfn(gfn_t gfn)
1320{
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001321 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001322}
1323
Xiao Guangrong67052b32011-05-15 23:27:08 +08001324static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
1325 struct kvm_mmu_page *sp, u64 *parent_pte)
1326{
1327 if (!parent_pte)
1328 return;
1329
1330 pte_list_add(vcpu, parent_pte, &sp->parent_ptes);
1331}
1332
1333static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
1334 u64 *parent_pte)
1335{
1336 pte_list_remove(parent_pte, &sp->parent_ptes);
1337}
1338
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001339static void drop_parent_pte(struct kvm_mmu_page *sp,
1340 u64 *parent_pte)
1341{
1342 mmu_page_remove_parent_pte(sp, parent_pte);
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08001343 mmu_spte_clear_no_track(parent_pte);
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001344}
1345
Avi Kivity25c0de22007-01-05 16:36:42 -08001346static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
Lai Jiangshan2032a932010-05-26 16:49:59 +08001347 u64 *parent_pte, int direct)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348{
Avi Kivity4db35312007-11-21 15:28:32 +02001349 struct kvm_mmu_page *sp;
Xiao Guangrong67052b32011-05-15 23:27:08 +08001350 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache,
1351 sizeof *sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001352 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Lai Jiangshan2032a932010-05-26 16:49:59 +08001353 if (!direct)
1354 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache,
1355 PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +02001356 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001357 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Xiao Guangrong93a5cef2011-11-24 17:37:48 +08001358 bitmap_zero(sp->slot_bitmap, KVM_MEM_SLOTS_NUM);
Xiao Guangrong67052b32011-05-15 23:27:08 +08001359 sp->parent_ptes = 0;
1360 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Dave Hansen45221ab2010-08-19 18:11:37 -07001361 kvm_mod_used_mmu_pages(vcpu->kvm, +1);
Avi Kivity4db35312007-11-21 15:28:32 +02001362 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363}
1364
Xiao Guangrong67052b32011-05-15 23:27:08 +08001365static void mark_unsync(u64 *spte);
Xiao Guangrong6b184932010-04-16 21:29:17 +08001366static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001367{
Xiao Guangrong67052b32011-05-15 23:27:08 +08001368 pte_list_walk(&sp->parent_ptes, mark_unsync);
Xiao Guangrong1047df12010-06-11 21:35:15 +08001369}
1370
Xiao Guangrong67052b32011-05-15 23:27:08 +08001371static void mark_unsync(u64 *spte)
Xiao Guangrong1047df12010-06-11 21:35:15 +08001372{
Xiao Guangrong67052b32011-05-15 23:27:08 +08001373 struct kvm_mmu_page *sp;
Xiao Guangrong1047df12010-06-11 21:35:15 +08001374 unsigned int index;
1375
Xiao Guangrong67052b32011-05-15 23:27:08 +08001376 sp = page_header(__pa(spte));
Xiao Guangrong1047df12010-06-11 21:35:15 +08001377 index = spte - sp->spt;
1378 if (__test_and_set_bit(index, sp->unsync_child_bitmap))
1379 return;
1380 if (sp->unsync_children++)
1381 return;
1382 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001383}
1384
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001385static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001386 struct kvm_mmu_page *sp)
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001387{
1388 return 1;
1389}
1390
Marcelo Tosattia7052892008-09-23 13:18:35 -03001391static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1392{
1393}
1394
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08001395static void nonpaging_update_pte(struct kvm_vcpu *vcpu,
1396 struct kvm_mmu_page *sp, u64 *spte,
Xiao Guangrong7c562522011-03-28 10:29:27 +08001397 const void *pte)
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08001398{
1399 WARN_ON(1);
1400}
1401
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001402#define KVM_PAGE_ARRAY_NR 16
1403
1404struct kvm_mmu_pages {
1405 struct mmu_page_and_offset {
1406 struct kvm_mmu_page *sp;
1407 unsigned int idx;
1408 } page[KVM_PAGE_ARRAY_NR];
1409 unsigned int nr;
1410};
1411
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001412#define for_each_unsync_children(bitmap, idx) \
1413 for (idx = find_first_bit(bitmap, 512); \
1414 idx < 512; \
1415 idx = find_next_bit(bitmap, 512, idx+1))
1416
Hannes Edercded19f2009-02-21 02:19:13 +01001417static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1418 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001419{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001420 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001421
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001422 if (sp->unsync)
1423 for (i=0; i < pvec->nr; i++)
1424 if (pvec->page[i].sp == sp)
1425 return 0;
1426
1427 pvec->page[pvec->nr].sp = sp;
1428 pvec->page[pvec->nr].idx = idx;
1429 pvec->nr++;
1430 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1431}
1432
1433static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1434 struct kvm_mmu_pages *pvec)
1435{
1436 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001437
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001438 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001439 struct kvm_mmu_page *child;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001440 u64 ent = sp->spt[i];
1441
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001442 if (!is_shadow_present_pte(ent) || is_large_pte(ent))
1443 goto clear_child_bitmap;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001444
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001445 child = page_header(ent & PT64_BASE_ADDR_MASK);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001446
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001447 if (child->unsync_children) {
1448 if (mmu_pages_add(pvec, child, i))
1449 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001450
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001451 ret = __mmu_unsync_walk(child, pvec);
1452 if (!ret)
1453 goto clear_child_bitmap;
1454 else if (ret > 0)
1455 nr_unsync_leaf += ret;
1456 else
1457 return ret;
1458 } else if (child->unsync) {
1459 nr_unsync_leaf++;
1460 if (mmu_pages_add(pvec, child, i))
1461 return -ENOSPC;
1462 } else
1463 goto clear_child_bitmap;
1464
1465 continue;
1466
1467clear_child_bitmap:
1468 __clear_bit(i, sp->unsync_child_bitmap);
1469 sp->unsync_children--;
1470 WARN_ON((int)sp->unsync_children < 0);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001471 }
1472
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001473
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001474 return nr_unsync_leaf;
1475}
1476
1477static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1478 struct kvm_mmu_pages *pvec)
1479{
1480 if (!sp->unsync_children)
1481 return 0;
1482
1483 mmu_pages_add(pvec, sp, 0);
1484 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001485}
1486
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001487static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1488{
1489 WARN_ON(!sp->unsync);
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001490 trace_kvm_mmu_sync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001491 sp->unsync = 0;
1492 --kvm->stat.mmu_unsync;
1493}
1494
Xiao Guangrong77758342010-06-04 21:53:54 +08001495static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1496 struct list_head *invalid_list);
1497static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1498 struct list_head *invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001499
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001500#define for_each_gfn_sp(kvm, sp, gfn, pos) \
1501 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001502 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1503 if ((sp)->gfn != (gfn)) {} else
1504
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001505#define for_each_gfn_indirect_valid_sp(kvm, sp, gfn, pos) \
1506 hlist_for_each_entry(sp, pos, \
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001507 &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
1508 if ((sp)->gfn != (gfn) || (sp)->role.direct || \
1509 (sp)->role.invalid) {} else
1510
Xiao Guangrongf918b442010-06-11 21:30:36 +08001511/* @sp->gfn should be write-protected at the call site */
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001512static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001513 struct list_head *invalid_list, bool clear_unsync)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001514{
Avi Kivity5b7e0102010-04-14 19:20:03 +03001515 if (sp->role.cr4_pae != !!is_pae(vcpu)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001516 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001517 return 1;
1518 }
1519
Xiao Guangrongf918b442010-06-11 21:30:36 +08001520 if (clear_unsync)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001521 kvm_unlink_unsync_page(vcpu->kvm, sp);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001522
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001523 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001524 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001525 return 1;
1526 }
1527
1528 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001529 return 0;
1530}
1531
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001532static int kvm_sync_page_transient(struct kvm_vcpu *vcpu,
1533 struct kvm_mmu_page *sp)
1534{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001535 LIST_HEAD(invalid_list);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001536 int ret;
1537
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001538 ret = __kvm_sync_page(vcpu, sp, &invalid_list, false);
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001539 if (ret)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001540 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
1541
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001542 return ret;
1543}
1544
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001545static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1546 struct list_head *invalid_list)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001547{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001548 return __kvm_sync_page(vcpu, sp, invalid_list, true);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001549}
1550
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001551/* @gfn should be write-protected at the call site */
1552static void kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1553{
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001554 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001555 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001556 LIST_HEAD(invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001557 bool flush = false;
1558
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001559 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001560 if (!s->unsync)
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001561 continue;
1562
1563 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001564 kvm_unlink_unsync_page(vcpu->kvm, s);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001565 if ((s->role.cr4_pae != !!is_pae(vcpu)) ||
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001566 (vcpu->arch.mmu.sync_page(vcpu, s))) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001567 kvm_mmu_prepare_zap_page(vcpu->kvm, s, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001568 continue;
1569 }
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001570 flush = true;
1571 }
1572
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001573 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001574 if (flush)
1575 kvm_mmu_flush_tlb(vcpu);
1576}
1577
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001578struct mmu_page_path {
1579 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1580 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001581};
1582
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001583#define for_each_sp(pvec, sp, parents, i) \
1584 for (i = mmu_pages_next(&pvec, &parents, -1), \
1585 sp = pvec.page[i].sp; \
1586 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1587 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001588
Hannes Edercded19f2009-02-21 02:19:13 +01001589static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1590 struct mmu_page_path *parents,
1591 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001592{
1593 int n;
1594
1595 for (n = i+1; n < pvec->nr; n++) {
1596 struct kvm_mmu_page *sp = pvec->page[n].sp;
1597
1598 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1599 parents->idx[0] = pvec->page[n].idx;
1600 return n;
1601 }
1602
1603 parents->parent[sp->role.level-2] = sp;
1604 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1605 }
1606
1607 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001608}
1609
Hannes Edercded19f2009-02-21 02:19:13 +01001610static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001611{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001612 struct kvm_mmu_page *sp;
1613 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001614
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001615 do {
1616 unsigned int idx = parents->idx[level];
1617
1618 sp = parents->parent[level];
1619 if (!sp)
1620 return;
1621
1622 --sp->unsync_children;
1623 WARN_ON((int)sp->unsync_children < 0);
1624 __clear_bit(idx, sp->unsync_child_bitmap);
1625 level++;
1626 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1627}
1628
1629static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1630 struct mmu_page_path *parents,
1631 struct kvm_mmu_pages *pvec)
1632{
1633 parents->parent[parent->role.level-1] = NULL;
1634 pvec->nr = 0;
1635}
1636
1637static void mmu_sync_children(struct kvm_vcpu *vcpu,
1638 struct kvm_mmu_page *parent)
1639{
1640 int i;
1641 struct kvm_mmu_page *sp;
1642 struct mmu_page_path parents;
1643 struct kvm_mmu_pages pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001644 LIST_HEAD(invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001645
1646 kvm_mmu_pages_init(parent, &parents, &pages);
1647 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001648 int protected = 0;
1649
1650 for_each_sp(pages, sp, parents, i)
1651 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1652
1653 if (protected)
1654 kvm_flush_remote_tlbs(vcpu->kvm);
1655
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001656 for_each_sp(pages, sp, parents, i) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001657 kvm_sync_page(vcpu, sp, &invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001658 mmu_pages_clear_parents(&parents);
1659 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001660 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001661 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001662 kvm_mmu_pages_init(parent, &parents, &pages);
1663 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001664}
1665
Xiao Guangrongc3707952011-07-12 03:28:04 +08001666static void init_shadow_page_table(struct kvm_mmu_page *sp)
1667{
1668 int i;
1669
1670 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1671 sp->spt[i] = 0ull;
1672}
1673
Xiao Guangronga30f47c2011-09-22 16:58:36 +08001674static void __clear_sp_write_flooding_count(struct kvm_mmu_page *sp)
1675{
1676 sp->write_flooding_count = 0;
1677}
1678
1679static void clear_sp_write_flooding_count(u64 *spte)
1680{
1681 struct kvm_mmu_page *sp = page_header(__pa(spte));
1682
1683 __clear_sp_write_flooding_count(sp);
1684}
1685
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001686static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1687 gfn_t gfn,
1688 gva_t gaddr,
1689 unsigned level,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001690 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001691 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001692 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001693{
1694 union kvm_mmu_page_role role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001695 unsigned quadrant;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001696 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001697 struct hlist_node *node;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001698 bool need_sync = false;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001699
Avi Kivitya770f6f2008-12-21 19:20:09 +02001700 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001701 role.level = level;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001702 role.direct = direct;
Avi Kivity84b0c8c2010-03-14 10:16:40 +02001703 if (role.direct)
Avi Kivity5b7e0102010-04-14 19:20:03 +03001704 role.cr4_pae = 0;
Avi Kivity41074d02007-12-09 17:00:02 +02001705 role.access = access;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02001706 if (!vcpu->arch.mmu.direct_map
1707 && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001708 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1709 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1710 role.quadrant = quadrant;
1711 }
Xiao Guangrongf41d3352010-06-04 21:56:11 +08001712 for_each_gfn_sp(vcpu->kvm, sp, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001713 if (!need_sync && sp->unsync)
1714 need_sync = true;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001715
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001716 if (sp->role.word != role.word)
1717 continue;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001718
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001719 if (sp->unsync && kvm_sync_page_transient(vcpu, sp))
1720 break;
Xiao Guangronge02aa902010-05-15 18:52:34 +08001721
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001722 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
1723 if (sp->unsync_children) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03001724 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001725 kvm_mmu_mark_parents_unsync(sp);
1726 } else if (sp->unsync)
1727 kvm_mmu_mark_parents_unsync(sp);
Xiao Guangronge02aa902010-05-15 18:52:34 +08001728
Xiao Guangronga30f47c2011-09-22 16:58:36 +08001729 __clear_sp_write_flooding_count(sp);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001730 trace_kvm_mmu_get_page(sp, false);
1731 return sp;
1732 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001733 ++vcpu->kvm->stat.mmu_cache_miss;
Lai Jiangshan2032a932010-05-26 16:49:59 +08001734 sp = kvm_mmu_alloc_page(vcpu, parent_pte, direct);
Avi Kivity4db35312007-11-21 15:28:32 +02001735 if (!sp)
1736 return sp;
Avi Kivity4db35312007-11-21 15:28:32 +02001737 sp->gfn = gfn;
1738 sp->role = role;
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001739 hlist_add_head(&sp->hash_link,
1740 &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001741 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001742 if (rmap_write_protect(vcpu->kvm, gfn))
1743 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001744 if (level > PT_PAGE_TABLE_LEVEL && need_sync)
1745 kvm_sync_pages(vcpu, gfn);
1746
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001747 account_shadowed(vcpu->kvm, gfn);
1748 }
Xiao Guangrongc3707952011-07-12 03:28:04 +08001749 init_shadow_page_table(sp);
Avi Kivityf691fe12009-07-06 15:58:14 +03001750 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02001751 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001752}
1753
Avi Kivity2d111232008-12-25 14:39:47 +02001754static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1755 struct kvm_vcpu *vcpu, u64 addr)
1756{
1757 iterator->addr = addr;
1758 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1759 iterator->level = vcpu->arch.mmu.shadow_root_level;
Joerg Roedel81407ca2010-09-10 17:31:00 +02001760
1761 if (iterator->level == PT64_ROOT_LEVEL &&
1762 vcpu->arch.mmu.root_level < PT64_ROOT_LEVEL &&
1763 !vcpu->arch.mmu.direct_map)
1764 --iterator->level;
1765
Avi Kivity2d111232008-12-25 14:39:47 +02001766 if (iterator->level == PT32E_ROOT_LEVEL) {
1767 iterator->shadow_addr
1768 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1769 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1770 --iterator->level;
1771 if (!iterator->shadow_addr)
1772 iterator->level = 0;
1773 }
1774}
1775
1776static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1777{
1778 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1779 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001780
Avi Kivity2d111232008-12-25 14:39:47 +02001781 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1782 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1783 return true;
1784}
1785
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08001786static void __shadow_walk_next(struct kvm_shadow_walk_iterator *iterator,
1787 u64 spte)
Avi Kivity2d111232008-12-25 14:39:47 +02001788{
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08001789 if (is_last_spte(spte, iterator->level)) {
Xiao Guangrong052331b2011-07-12 03:21:17 +08001790 iterator->level = 0;
1791 return;
1792 }
1793
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08001794 iterator->shadow_addr = spte & PT64_BASE_ADDR_MASK;
Avi Kivity2d111232008-12-25 14:39:47 +02001795 --iterator->level;
1796}
1797
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08001798static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1799{
1800 return __shadow_walk_next(iterator, *iterator->sptep);
1801}
1802
Avi Kivity32ef26a2010-07-13 14:27:04 +03001803static void link_shadow_page(u64 *sptep, struct kvm_mmu_page *sp)
1804{
1805 u64 spte;
1806
1807 spte = __pa(sp->spt)
1808 | PT_PRESENT_MASK | PT_ACCESSED_MASK
1809 | PT_WRITABLE_MASK | PT_USER_MASK;
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08001810 mmu_spte_set(sptep, spte);
Avi Kivity32ef26a2010-07-13 14:27:04 +03001811}
1812
Avi Kivitya3aa51c2010-07-13 14:27:06 +03001813static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep)
1814{
1815 if (is_large_pte(*sptep)) {
Xiao Guangrongc3707952011-07-12 03:28:04 +08001816 drop_spte(vcpu->kvm, sptep);
Avi Kivitya3aa51c2010-07-13 14:27:06 +03001817 kvm_flush_remote_tlbs(vcpu->kvm);
1818 }
1819}
1820
Avi Kivitya357bd22010-07-13 14:27:07 +03001821static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
1822 unsigned direct_access)
1823{
1824 if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
1825 struct kvm_mmu_page *child;
1826
1827 /*
1828 * For the direct sp, if the guest pte's dirty bit
1829 * changed form clean to dirty, it will corrupt the
1830 * sp's access: allow writable in the read-only sp,
1831 * so we should update the spte at this point to get
1832 * a new sp with the correct access.
1833 */
1834 child = page_header(*sptep & PT64_BASE_ADDR_MASK);
1835 if (child->role.access == direct_access)
1836 return;
1837
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001838 drop_parent_pte(child, sptep);
Avi Kivitya357bd22010-07-13 14:27:07 +03001839 kvm_flush_remote_tlbs(vcpu->kvm);
1840 }
1841}
1842
Xiao Guangrong505aef82011-09-22 16:56:06 +08001843static bool mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08001844 u64 *spte)
1845{
1846 u64 pte;
1847 struct kvm_mmu_page *child;
1848
1849 pte = *spte;
1850 if (is_shadow_present_pte(pte)) {
Xiao Guangrong505aef82011-09-22 16:56:06 +08001851 if (is_last_spte(pte, sp->role.level)) {
Xiao Guangrongc3707952011-07-12 03:28:04 +08001852 drop_spte(kvm, spte);
Xiao Guangrong505aef82011-09-22 16:56:06 +08001853 if (is_large_pte(pte))
1854 --kvm->stat.lpages;
1855 } else {
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08001856 child = page_header(pte & PT64_BASE_ADDR_MASK);
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001857 drop_parent_pte(child, spte);
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08001858 }
Xiao Guangrong505aef82011-09-22 16:56:06 +08001859 return true;
1860 }
1861
1862 if (is_mmio_spte(pte))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08001863 mmu_spte_clear_no_track(spte);
Xiao Guangrongc3707952011-07-12 03:28:04 +08001864
Xiao Guangrong505aef82011-09-22 16:56:06 +08001865 return false;
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08001866}
1867
Avi Kivity90cb0522007-07-17 13:04:56 +03001868static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001869 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001870{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001871 unsigned i;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001872
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08001873 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1874 mmu_page_zap_pte(kvm, sp, sp->spt + i);
Avi Kivitya4360362007-01-05 16:36:45 -08001875}
1876
Avi Kivity4db35312007-11-21 15:28:32 +02001877static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001878{
Avi Kivity4db35312007-11-21 15:28:32 +02001879 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001880}
1881
Avi Kivity31aa2b42008-07-11 17:59:46 +03001882static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001883{
1884 u64 *parent_pte;
1885
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001886 while ((parent_pte = pte_list_next(&sp->parent_ptes, NULL)))
1887 drop_parent_pte(sp, parent_pte);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001888}
1889
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001890static int mmu_zap_unsync_children(struct kvm *kvm,
Xiao Guangrong77758342010-06-04 21:53:54 +08001891 struct kvm_mmu_page *parent,
1892 struct list_head *invalid_list)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001893{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001894 int i, zapped = 0;
1895 struct mmu_page_path parents;
1896 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001897
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001898 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001899 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001900
1901 kvm_mmu_pages_init(parent, &parents, &pages);
1902 while (mmu_unsync_walk(parent, &pages)) {
1903 struct kvm_mmu_page *sp;
1904
1905 for_each_sp(pages, sp, parents, i) {
Xiao Guangrong77758342010-06-04 21:53:54 +08001906 kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001907 mmu_pages_clear_parents(&parents);
Xiao Guangrong77662e02010-04-16 16:34:42 +08001908 zapped++;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001909 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001910 kvm_mmu_pages_init(parent, &parents, &pages);
1911 }
1912
1913 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001914}
1915
Xiao Guangrong77758342010-06-04 21:53:54 +08001916static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1917 struct list_head *invalid_list)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001918{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001919 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03001920
Xiao Guangrong77758342010-06-04 21:53:54 +08001921 trace_kvm_mmu_prepare_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001922 ++kvm->stat.mmu_shadow_zapped;
Xiao Guangrong77758342010-06-04 21:53:54 +08001923 ret = mmu_zap_unsync_children(kvm, sp, invalid_list);
Avi Kivity4db35312007-11-21 15:28:32 +02001924 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001925 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001926 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001927 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001928 if (sp->unsync)
1929 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001930 if (!sp->root_count) {
Gui Jianfeng54a4f022010-05-05 09:03:49 +08001931 /* Count self */
1932 ret++;
Xiao Guangrong77758342010-06-04 21:53:54 +08001933 list_move(&sp->link, invalid_list);
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08001934 kvm_mod_used_mmu_pages(kvm, -1);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001935 } else {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001936 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001937 kvm_reload_remote_mmus(kvm);
1938 }
Xiao Guangrong77758342010-06-04 21:53:54 +08001939
1940 sp->role.invalid = 1;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001941 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001942}
1943
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08001944static void kvm_mmu_isolate_pages(struct list_head *invalid_list)
1945{
1946 struct kvm_mmu_page *sp;
1947
1948 list_for_each_entry(sp, invalid_list, link)
1949 kvm_mmu_isolate_page(sp);
1950}
1951
1952static void free_pages_rcu(struct rcu_head *head)
1953{
1954 struct kvm_mmu_page *next, *sp;
1955
1956 sp = container_of(head, struct kvm_mmu_page, rcu);
1957 while (sp) {
1958 if (!list_empty(&sp->link))
1959 next = list_first_entry(&sp->link,
1960 struct kvm_mmu_page, link);
1961 else
1962 next = NULL;
1963 kvm_mmu_free_page(sp);
1964 sp = next;
1965 }
1966}
1967
Xiao Guangrong77758342010-06-04 21:53:54 +08001968static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1969 struct list_head *invalid_list)
1970{
1971 struct kvm_mmu_page *sp;
1972
1973 if (list_empty(invalid_list))
1974 return;
1975
1976 kvm_flush_remote_tlbs(kvm);
1977
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08001978 if (atomic_read(&kvm->arch.reader_counter)) {
1979 kvm_mmu_isolate_pages(invalid_list);
1980 sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
1981 list_del_init(invalid_list);
Xiao Guangrong4f022642011-07-12 03:34:24 +08001982
1983 trace_kvm_mmu_delay_free_pages(sp);
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08001984 call_rcu(&sp->rcu, free_pages_rcu);
1985 return;
1986 }
1987
Xiao Guangrong77758342010-06-04 21:53:54 +08001988 do {
1989 sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
1990 WARN_ON(!sp->role.invalid || sp->root_count);
Xiao Guangrongbd4c86e2011-07-12 03:27:14 +08001991 kvm_mmu_isolate_page(sp);
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08001992 kvm_mmu_free_page(sp);
Xiao Guangrong77758342010-06-04 21:53:54 +08001993 } while (!list_empty(invalid_list));
1994
1995}
1996
Izik Eidus82ce2c92007-10-02 18:52:55 +02001997/*
1998 * Changing the number of mmu pages allocated to the vm
Dave Hansen49d5ca22010-08-19 18:11:28 -07001999 * Note: if goal_nr_mmu_pages is too small, you will get dead lock
Izik Eidus82ce2c92007-10-02 18:52:55 +02002000 */
Dave Hansen49d5ca22010-08-19 18:11:28 -07002001void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages)
Izik Eidus82ce2c92007-10-02 18:52:55 +02002002{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002003 LIST_HEAD(invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02002004 /*
2005 * If we set the number of mmu pages to be smaller be than the
2006 * number of actived pages , we must to free some mmu pages before we
2007 * change the value
2008 */
2009
Dave Hansen49d5ca22010-08-19 18:11:28 -07002010 if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) {
2011 while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages &&
Xiao Guangrong77662e02010-04-16 16:34:42 +08002012 !list_empty(&kvm->arch.active_mmu_pages)) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02002013 struct kvm_mmu_page *page;
2014
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002015 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02002016 struct kvm_mmu_page, link);
Xiaotian Feng80b63fa2010-08-24 10:31:07 +08002017 kvm_mmu_prepare_zap_page(kvm, page, &invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02002018 }
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08002019 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Dave Hansen49d5ca22010-08-19 18:11:28 -07002020 goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002021 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02002022
Dave Hansen49d5ca22010-08-19 18:11:28 -07002023 kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002024}
2025
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08002026int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08002027{
Avi Kivity4db35312007-11-21 15:28:32 +02002028 struct kvm_mmu_page *sp;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002029 struct hlist_node *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002030 LIST_HEAD(invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08002031 int r;
2032
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002033 pgprintk("%s: looking for gfn %llx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08002034 r = 0;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08002035 spin_lock(&kvm->mmu_lock);
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002036 for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002037 pgprintk("%s: gfn %llx role %x\n", __func__, gfn,
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002038 sp->role.word);
2039 r = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002040 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002041 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002042 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08002043 spin_unlock(&kvm->mmu_lock);
2044
Avi Kivitya4360362007-01-05 16:36:45 -08002045 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002046}
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08002047EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002048
Avi Kivity38c335f2007-11-21 14:20:22 +02002049static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002050{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02002051 int slot = memslot_id(kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02002052 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002053
Sheng Yang291f26b2008-10-16 17:30:57 +08002054 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002055}
2056
Sheng Yang74be52e2008-10-09 16:01:56 +08002057/*
2058 * The function is based on mtrr_type_lookup() in
2059 * arch/x86/kernel/cpu/mtrr/generic.c
2060 */
2061static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
2062 u64 start, u64 end)
2063{
2064 int i;
2065 u64 base, mask;
2066 u8 prev_match, curr_match;
2067 int num_var_ranges = KVM_NR_VAR_MTRR;
2068
2069 if (!mtrr_state->enabled)
2070 return 0xFF;
2071
2072 /* Make end inclusive end, instead of exclusive */
2073 end--;
2074
2075 /* Look in fixed ranges. Just return the type as per start */
2076 if (mtrr_state->have_fixed && (start < 0x100000)) {
2077 int idx;
2078
2079 if (start < 0x80000) {
2080 idx = 0;
2081 idx += (start >> 16);
2082 return mtrr_state->fixed_ranges[idx];
2083 } else if (start < 0xC0000) {
2084 idx = 1 * 8;
2085 idx += ((start - 0x80000) >> 14);
2086 return mtrr_state->fixed_ranges[idx];
2087 } else if (start < 0x1000000) {
2088 idx = 3 * 8;
2089 idx += ((start - 0xC0000) >> 12);
2090 return mtrr_state->fixed_ranges[idx];
2091 }
2092 }
2093
2094 /*
2095 * Look in variable ranges
2096 * Look of multiple ranges matching this address and pick type
2097 * as per MTRR precedence
2098 */
2099 if (!(mtrr_state->enabled & 2))
2100 return mtrr_state->def_type;
2101
2102 prev_match = 0xFF;
2103 for (i = 0; i < num_var_ranges; ++i) {
2104 unsigned short start_state, end_state;
2105
2106 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
2107 continue;
2108
2109 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
2110 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
2111 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
2112 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
2113
2114 start_state = ((start & mask) == (base & mask));
2115 end_state = ((end & mask) == (base & mask));
2116 if (start_state != end_state)
2117 return 0xFE;
2118
2119 if ((start & mask) != (base & mask))
2120 continue;
2121
2122 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
2123 if (prev_match == 0xFF) {
2124 prev_match = curr_match;
2125 continue;
2126 }
2127
2128 if (prev_match == MTRR_TYPE_UNCACHABLE ||
2129 curr_match == MTRR_TYPE_UNCACHABLE)
2130 return MTRR_TYPE_UNCACHABLE;
2131
2132 if ((prev_match == MTRR_TYPE_WRBACK &&
2133 curr_match == MTRR_TYPE_WRTHROUGH) ||
2134 (prev_match == MTRR_TYPE_WRTHROUGH &&
2135 curr_match == MTRR_TYPE_WRBACK)) {
2136 prev_match = MTRR_TYPE_WRTHROUGH;
2137 curr_match = MTRR_TYPE_WRTHROUGH;
2138 }
2139
2140 if (prev_match != curr_match)
2141 return MTRR_TYPE_UNCACHABLE;
2142 }
2143
2144 if (prev_match != 0xFF)
2145 return prev_match;
2146
2147 return mtrr_state->def_type;
2148}
2149
Sheng Yang4b12f0d2009-04-27 20:35:42 +08002150u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08002151{
2152 u8 mtrr;
2153
2154 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
2155 (gfn << PAGE_SHIFT) + PAGE_SIZE);
2156 if (mtrr == 0xfe || mtrr == 0xff)
2157 mtrr = MTRR_TYPE_WRBACK;
2158 return mtrr;
2159}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08002160EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08002161
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002162static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002163{
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08002164 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002165 ++vcpu->kvm->stat.mmu_unsync;
2166 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02002167
Xiao Guangrong6b184932010-04-16 21:29:17 +08002168 kvm_mmu_mark_parents_unsync(sp);
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002169}
2170
2171static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
2172{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002173 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002174 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002175
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002176 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002177 if (s->unsync)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002178 continue;
2179 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
2180 __kvm_unsync_page(vcpu, s);
2181 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002182}
2183
2184static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
2185 bool can_unsync)
2186{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002187 struct kvm_mmu_page *s;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002188 struct hlist_node *node;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002189 bool need_unsync = false;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002190
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002191 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08002192 if (!can_unsync)
2193 return 1;
2194
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002195 if (s->role.level != PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002196 return 1;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002197
2198 if (!need_unsync && !s->unsync) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08002199 if (!oos_shadow)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002200 return 1;
2201 need_unsync = true;
2202 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002203 }
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002204 if (need_unsync)
2205 kvm_unsync_pages(vcpu, gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002206 return 0;
2207}
2208
Avi Kivityd555c332009-06-10 14:24:23 +03002209static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002210 unsigned pte_access, int user_fault,
Xiao Guangrong640d9b02011-07-12 03:24:39 +08002211 int write_fault, int level,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03002212 gfn_t gfn, pfn_t pfn, bool speculative,
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08002213 bool can_unsync, bool host_writable)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002214{
Xiao Guangrongb330aa02010-11-19 17:02:35 +08002215 u64 spte, entry = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002216 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08002217
Xiao Guangrongce88dec2011-07-12 03:33:44 +08002218 if (set_mmio_spte(sptep, gfn, pfn, pte_access))
2219 return 0;
2220
Marcelo Tosatti982c2562010-10-22 14:18:16 -02002221 spte = PT_PRESENT_MASK;
Avi Kivity947da532008-03-18 11:05:52 +02002222 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03002223 spte |= shadow_accessed_mask;
Xiao Guangrong640d9b02011-07-12 03:24:39 +08002224
Sheng Yang7b523452008-04-25 21:13:50 +08002225 if (pte_access & ACC_EXEC_MASK)
2226 spte |= shadow_x_mask;
2227 else
2228 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002229 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08002230 spte |= shadow_user_mask;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002231 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002232 spte |= PT_PAGE_SIZE_MASK;
Avi Kivityb0bc3ee2010-09-13 16:45:28 +02002233 if (tdp_enabled)
Sheng Yang4b12f0d2009-04-27 20:35:42 +08002234 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
2235 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002236
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08002237 if (host_writable)
Izik Eidus14032832009-09-23 21:47:17 +03002238 spte |= SPTE_HOST_WRITEABLE;
Xiao Guangrongf8e453b2010-12-23 16:09:29 +08002239 else
2240 pte_access &= ~ACC_WRITE_MASK;
Izik Eidus14032832009-09-23 21:47:17 +03002241
Anthony Liguori35149e22008-04-02 14:46:56 -05002242 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002243
2244 if ((pte_access & ACC_WRITE_MASK)
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002245 || (!vcpu->arch.mmu.direct_map && write_fault
2246 && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002247
Joerg Roedel852e3c12009-07-27 16:30:44 +02002248 if (level > PT_PAGE_TABLE_LEVEL &&
2249 has_wrprotected_page(vcpu->kvm, gfn, level)) {
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002250 ret = 1;
Xiao Guangrongc3707952011-07-12 03:28:04 +08002251 drop_spte(vcpu->kvm, sptep);
Avi Kivitybe38d272010-06-06 14:31:27 +03002252 goto done;
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002253 }
2254
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002255 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002256
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002257 if (!vcpu->arch.mmu.direct_map
Avi Kivity411c5882011-06-06 16:11:54 +03002258 && !(pte_access & ACC_WRITE_MASK)) {
Avi Kivity69325a12010-05-27 14:35:58 +03002259 spte &= ~PT_USER_MASK;
Avi Kivity411c5882011-06-06 16:11:54 +03002260 /*
2261 * If we converted a user page to a kernel page,
2262 * so that the kernel can write to it when cr0.wp=0,
2263 * then we should prevent the kernel from executing it
2264 * if SMEP is enabled.
2265 */
2266 if (kvm_read_cr4_bits(vcpu, X86_CR4_SMEP))
2267 spte |= PT64_NX_MASK;
2268 }
Avi Kivity69325a12010-05-27 14:35:58 +03002269
Marcelo Tosattiecc55892008-11-25 15:58:07 +01002270 /*
2271 * Optimization: for pte sync, if spte was writable the hash
2272 * lookup is unnecessary (and expensive). Write protection
2273 * is responsibility of mmu_get_page / kvm_sync_page.
2274 * Same reasoning can be applied to dirty page accounting.
2275 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002276 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01002277 goto set_pte;
2278
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002279 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002280 pgprintk("%s: found shadow page for %llx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002281 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002282 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002283 pte_access &= ~ACC_WRITE_MASK;
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002284 if (is_writable_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002285 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002286 }
2287 }
2288
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002289 if (pte_access & ACC_WRITE_MASK)
2290 mark_page_dirty(vcpu->kvm, gfn);
2291
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002292set_pte:
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08002293 mmu_spte_update(sptep, spte);
Xiao Guangrongb330aa02010-11-19 17:02:35 +08002294 /*
2295 * If we overwrite a writable spte with a read-only one we
2296 * should flush remote TLBs. Otherwise rmap_write_protect
2297 * will find a read-only spte, even though the writable spte
2298 * might be cached on a CPU's TLB.
2299 */
2300 if (is_writable_pte(entry) && !is_writable_pte(*sptep))
2301 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivitybe38d272010-06-06 14:31:27 +03002302done:
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002303 return ret;
2304}
2305
Avi Kivityd555c332009-06-10 14:24:23 +03002306static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002307 unsigned pt_access, unsigned pte_access,
Xiao Guangrong640d9b02011-07-12 03:24:39 +08002308 int user_fault, int write_fault,
Xiao Guangrongb90a0e62011-07-12 03:25:56 +08002309 int *emulate, int level, gfn_t gfn,
Izik Eidus14032832009-09-23 21:47:17 +03002310 pfn_t pfn, bool speculative,
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08002311 bool host_writable)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002312{
2313 int was_rmapped = 0;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002314 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002315
2316 pgprintk("%s: spte %llx access %x write_fault %d"
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002317 " user_fault %d gfn %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002318 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002319 write_fault, user_fault, gfn);
2320
Avi Kivityd555c332009-06-10 14:24:23 +03002321 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002322 /*
2323 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
2324 * the parent of the now unreachable PTE.
2325 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02002326 if (level > PT_PAGE_TABLE_LEVEL &&
2327 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002328 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03002329 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002330
2331 child = page_header(pte & PT64_BASE_ADDR_MASK);
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08002332 drop_parent_pte(child, sptep);
Marcelo Tosatti3be22642010-05-28 09:44:59 -03002333 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityd555c332009-06-10 14:24:23 +03002334 } else if (pfn != spte_to_pfn(*sptep)) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002335 pgprintk("hfn old %llx new %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002336 spte_to_pfn(*sptep), pfn);
Xiao Guangrongc3707952011-07-12 03:28:04 +08002337 drop_spte(vcpu->kvm, sptep);
Xiao Guangrong91546352010-06-30 16:04:06 +08002338 kvm_flush_remote_tlbs(vcpu->kvm);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01002339 } else
2340 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002341 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02002342
Avi Kivityd555c332009-06-10 14:24:23 +03002343 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Xiao Guangrong640d9b02011-07-12 03:24:39 +08002344 level, gfn, pfn, speculative, true,
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08002345 host_writable)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002346 if (write_fault)
Xiao Guangrongb90a0e62011-07-12 03:25:56 +08002347 *emulate = 1;
Xiao Guangrong5304efd2010-06-08 20:05:57 +08002348 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03002349 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002350
Xiao Guangrongce88dec2011-07-12 03:33:44 +08002351 if (unlikely(is_mmio_spte(*sptep) && emulate))
2352 *emulate = 1;
2353
Avi Kivityd555c332009-06-10 14:24:23 +03002354 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002355 pgprintk("instantiating %s PTE (%s) at %llx (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002356 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02002357 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
2358 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03002359 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002360 ++vcpu->kvm->stat.lpages;
2361
Xiao Guangrongffb61bb2011-07-12 03:22:01 +08002362 if (is_shadow_present_pte(*sptep)) {
2363 page_header_update_slot(vcpu->kvm, sptep, gfn);
2364 if (!was_rmapped) {
2365 rmap_count = rmap_add(vcpu, sptep, gfn);
2366 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
2367 rmap_recycle(vcpu, sptep, gfn);
2368 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002369 }
Xiao Guangrong9ed55202010-07-16 11:25:17 +08002370 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002371}
2372
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
2374{
2375}
2376
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002377static pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn,
2378 bool no_dirty_log)
2379{
2380 struct kvm_memory_slot *slot;
2381 unsigned long hva;
2382
Xiao Guangrong5d163b12011-03-09 15:43:00 +08002383 slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, no_dirty_log);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002384 if (!slot) {
Xiao Guangrongfce92dc2011-07-12 03:28:54 +08002385 get_page(fault_page);
2386 return page_to_pfn(fault_page);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002387 }
2388
2389 hva = gfn_to_hva_memslot(slot, gfn);
2390
2391 return hva_to_pfn_atomic(vcpu->kvm, hva);
2392}
2393
2394static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,
2395 struct kvm_mmu_page *sp,
2396 u64 *start, u64 *end)
2397{
2398 struct page *pages[PTE_PREFETCH_NUM];
2399 unsigned access = sp->role.access;
2400 int i, ret;
2401 gfn_t gfn;
2402
2403 gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt);
Xiao Guangrong5d163b12011-03-09 15:43:00 +08002404 if (!gfn_to_memslot_dirty_bitmap(vcpu, gfn, access & ACC_WRITE_MASK))
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002405 return -1;
2406
2407 ret = gfn_to_page_many_atomic(vcpu->kvm, gfn, pages, end - start);
2408 if (ret <= 0)
2409 return -1;
2410
2411 for (i = 0; i < ret; i++, gfn++, start++)
2412 mmu_set_spte(vcpu, start, ACC_ALL,
Xiao Guangrong640d9b02011-07-12 03:24:39 +08002413 access, 0, 0, NULL,
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002414 sp->role.level, gfn,
2415 page_to_pfn(pages[i]), true, true);
2416
2417 return 0;
2418}
2419
2420static void __direct_pte_prefetch(struct kvm_vcpu *vcpu,
2421 struct kvm_mmu_page *sp, u64 *sptep)
2422{
2423 u64 *spte, *start = NULL;
2424 int i;
2425
2426 WARN_ON(!sp->role.direct);
2427
2428 i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
2429 spte = sp->spt + i;
2430
2431 for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
Xiao Guangrongc3707952011-07-12 03:28:04 +08002432 if (is_shadow_present_pte(*spte) || spte == sptep) {
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002433 if (!start)
2434 continue;
2435 if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0)
2436 break;
2437 start = NULL;
2438 } else if (!start)
2439 start = spte;
2440 }
2441}
2442
2443static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)
2444{
2445 struct kvm_mmu_page *sp;
2446
2447 /*
2448 * Since it's no accessed bit on EPT, it's no way to
2449 * distinguish between actually accessed translations
2450 * and prefetched, so disable pte prefetch if EPT is
2451 * enabled.
2452 */
2453 if (!shadow_accessed_mask)
2454 return;
2455
2456 sp = page_header(__pa(sptep));
2457 if (sp->role.level > PT_PAGE_TABLE_LEVEL)
2458 return;
2459
2460 __direct_pte_prefetch(vcpu, sp, sptep);
2461}
2462
Joerg Roedel4d9976b2008-02-07 13:47:42 +01002463static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002464 int map_writable, int level, gfn_t gfn, pfn_t pfn,
2465 bool prefault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002466{
Avi Kivity9f652d212008-12-25 14:54:25 +02002467 struct kvm_shadow_walk_iterator iterator;
2468 struct kvm_mmu_page *sp;
Xiao Guangrongb90a0e62011-07-12 03:25:56 +08002469 int emulate = 0;
Avi Kivity9f652d212008-12-25 14:54:25 +02002470 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002471
Avi Kivity9f652d212008-12-25 14:54:25 +02002472 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02002473 if (iterator.level == level) {
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002474 unsigned pte_access = ACC_ALL;
2475
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002476 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, pte_access,
Xiao Guangrongb90a0e62011-07-12 03:25:56 +08002477 0, write, &emulate,
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002478 level, gfn, pfn, prefault, map_writable);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002479 direct_pte_prefetch(vcpu, iterator.sptep);
Avi Kivity9f652d212008-12-25 14:54:25 +02002480 ++vcpu->stat.pf_fixed;
2481 break;
2482 }
2483
Xiao Guangrongc3707952011-07-12 03:28:04 +08002484 if (!is_shadow_present_pte(*iterator.sptep)) {
Lai Jiangshanc9fa0b32010-05-26 16:48:25 +08002485 u64 base_addr = iterator.addr;
2486
2487 base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
2488 pseudo_gfn = base_addr >> PAGE_SHIFT;
Avi Kivity9f652d212008-12-25 14:54:25 +02002489 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
2490 iterator.level - 1,
2491 1, ACC_ALL, iterator.sptep);
2492 if (!sp) {
2493 pgprintk("nonpaging_map: ENOMEM\n");
2494 kvm_release_pfn_clean(pfn);
2495 return -ENOMEM;
2496 }
2497
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08002498 mmu_spte_set(iterator.sptep,
2499 __pa(sp->spt)
2500 | PT_PRESENT_MASK | PT_WRITABLE_MASK
2501 | shadow_user_mask | shadow_x_mask
2502 | shadow_accessed_mask);
Avi Kivity9f652d212008-12-25 14:54:25 +02002503 }
2504 }
Xiao Guangrongb90a0e62011-07-12 03:25:56 +08002505 return emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002506}
2507
Huang Ying77db5cb2010-10-08 16:24:15 +08002508static void kvm_send_hwpoison_signal(unsigned long address, struct task_struct *tsk)
Huang Yingbf998152010-05-31 14:28:19 +08002509{
Huang Ying77db5cb2010-10-08 16:24:15 +08002510 siginfo_t info;
Huang Yingbf998152010-05-31 14:28:19 +08002511
Huang Ying77db5cb2010-10-08 16:24:15 +08002512 info.si_signo = SIGBUS;
2513 info.si_errno = 0;
2514 info.si_code = BUS_MCEERR_AR;
2515 info.si_addr = (void __user *)address;
2516 info.si_addr_lsb = PAGE_SHIFT;
2517
2518 send_sig_info(SIGBUS, &info, tsk);
Huang Yingbf998152010-05-31 14:28:19 +08002519}
2520
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002521static int kvm_handle_bad_page(struct kvm_vcpu *vcpu, gfn_t gfn, pfn_t pfn)
Huang Yingbf998152010-05-31 14:28:19 +08002522{
2523 kvm_release_pfn_clean(pfn);
2524 if (is_hwpoison_pfn(pfn)) {
Xiao Guangrongbebb1062011-07-12 03:23:20 +08002525 kvm_send_hwpoison_signal(gfn_to_hva(vcpu->kvm, gfn), current);
Huang Yingbf998152010-05-31 14:28:19 +08002526 return 0;
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002527 }
Gleb Natapovedba23e2010-07-07 20:16:45 +03002528
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002529 return -EFAULT;
Huang Yingbf998152010-05-31 14:28:19 +08002530}
2531
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002532static void transparent_hugepage_adjust(struct kvm_vcpu *vcpu,
2533 gfn_t *gfnp, pfn_t *pfnp, int *levelp)
2534{
2535 pfn_t pfn = *pfnp;
2536 gfn_t gfn = *gfnp;
2537 int level = *levelp;
2538
2539 /*
2540 * Check if it's a transparent hugepage. If this would be an
2541 * hugetlbfs page, level wouldn't be set to
2542 * PT_PAGE_TABLE_LEVEL and there would be no adjustment done
2543 * here.
2544 */
2545 if (!is_error_pfn(pfn) && !kvm_is_mmio_pfn(pfn) &&
2546 level == PT_PAGE_TABLE_LEVEL &&
2547 PageTransCompound(pfn_to_page(pfn)) &&
2548 !has_wrprotected_page(vcpu->kvm, gfn, PT_DIRECTORY_LEVEL)) {
2549 unsigned long mask;
2550 /*
2551 * mmu_notifier_retry was successful and we hold the
2552 * mmu_lock here, so the pmd can't become splitting
2553 * from under us, and in turn
2554 * __split_huge_page_refcount() can't run from under
2555 * us and we can safely transfer the refcount from
2556 * PG_tail to PG_head as we switch the pfn to tail to
2557 * head.
2558 */
2559 *levelp = level = PT_DIRECTORY_LEVEL;
2560 mask = KVM_PAGES_PER_HPAGE(level) - 1;
2561 VM_BUG_ON((gfn & mask) != (pfn & mask));
2562 if (pfn & mask) {
2563 gfn &= ~mask;
2564 *gfnp = gfn;
2565 kvm_release_pfn_clean(pfn);
2566 pfn &= ~mask;
2567 if (!get_page_unless_zero(pfn_to_page(pfn)))
2568 BUG();
2569 *pfnp = pfn;
2570 }
2571 }
2572}
2573
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002574static bool mmu_invalid_pfn(pfn_t pfn)
2575{
Xiao Guangrongce88dec2011-07-12 03:33:44 +08002576 return unlikely(is_invalid_pfn(pfn));
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002577}
2578
2579static bool handle_abnormal_pfn(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn,
2580 pfn_t pfn, unsigned access, int *ret_val)
2581{
2582 bool ret = true;
2583
2584 /* The pfn is invalid, report the error! */
2585 if (unlikely(is_invalid_pfn(pfn))) {
2586 *ret_val = kvm_handle_bad_page(vcpu, gfn, pfn);
2587 goto exit;
2588 }
2589
Xiao Guangrongce88dec2011-07-12 03:33:44 +08002590 if (unlikely(is_noslot_pfn(pfn)))
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002591 vcpu_cache_mmio_info(vcpu, gva, gfn, access);
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002592
2593 ret = false;
2594exit:
2595 return ret;
2596}
2597
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002598static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
Xiao Guangrong060c2ab2010-11-12 14:49:11 +08002599 gva_t gva, pfn_t *pfn, bool write, bool *writable);
2600
2601static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002602 bool prefault)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002603{
2604 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002605 int level;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002606 int force_pt_level;
Anthony Liguori35149e22008-04-02 14:46:56 -05002607 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002608 unsigned long mmu_seq;
Marcelo Tosatti612819c2010-10-22 14:18:18 -02002609 bool map_writable;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002610
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002611 force_pt_level = mapping_level_dirty_bitmap(vcpu, gfn);
2612 if (likely(!force_pt_level)) {
2613 level = mapping_level(vcpu, gfn);
2614 /*
2615 * This path builds a PAE pagetable - so we can map
2616 * 2mb pages at maximum. Therefore check if the level
2617 * is larger than that.
2618 */
2619 if (level > PT_DIRECTORY_LEVEL)
2620 level = PT_DIRECTORY_LEVEL;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002621
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002622 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
2623 } else
2624 level = PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002625
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002626 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002627 smp_rmb();
Xiao Guangrong060c2ab2010-11-12 14:49:11 +08002628
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002629 if (try_async_pf(vcpu, prefault, gfn, v, &pfn, write, &map_writable))
Xiao Guangrong060c2ab2010-11-12 14:49:11 +08002630 return 0;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002631
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002632 if (handle_abnormal_pfn(vcpu, v, gfn, pfn, ACC_ALL, &r))
2633 return r;
Avi Kivityd196e342008-01-24 11:44:11 +02002634
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002635 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002636 if (mmu_notifier_retry(vcpu, mmu_seq))
2637 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02002638 kvm_mmu_free_some_pages(vcpu);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002639 if (likely(!force_pt_level))
2640 transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
Xiao Guangrong2ec47392010-12-07 10:34:42 +08002641 r = __direct_map(vcpu, v, write, map_writable, level, gfn, pfn,
2642 prefault);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002643 spin_unlock(&vcpu->kvm->mmu_lock);
2644
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002645
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002646 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002647
2648out_unlock:
2649 spin_unlock(&vcpu->kvm->mmu_lock);
2650 kvm_release_pfn_clean(pfn);
2651 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002652}
2653
2654
Avi Kivity17ac10a2007-01-05 16:36:40 -08002655static void mmu_free_roots(struct kvm_vcpu *vcpu)
2656{
2657 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02002658 struct kvm_mmu_page *sp;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002659 LIST_HEAD(invalid_list);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002660
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002661 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03002662 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002663 spin_lock(&vcpu->kvm->mmu_lock);
Joerg Roedel81407ca2010-09-10 17:31:00 +02002664 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL &&
2665 (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL ||
2666 vcpu->arch.mmu.direct_map)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002667 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002668
Avi Kivity4db35312007-11-21 15:28:32 +02002669 sp = page_header(root);
2670 --sp->root_count;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002671 if (!sp->root_count && sp->role.invalid) {
2672 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
2673 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
2674 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002675 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002676 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002677 return;
2678 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08002679 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002680 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002681
Avi Kivity417726a2007-04-12 17:35:58 +03002682 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03002683 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002684 sp = page_header(root);
2685 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002686 if (!sp->root_count && sp->role.invalid)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002687 kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
2688 &invalid_list);
Avi Kivity417726a2007-04-12 17:35:58 +03002689 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002690 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002691 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002692 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002693 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002694 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002695}
2696
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002697static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
2698{
2699 int ret = 0;
2700
2701 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03002702 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002703 ret = 1;
2704 }
2705
2706 return ret;
2707}
2708
Joerg Roedel651dd372010-09-10 17:30:59 +02002709static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu)
2710{
2711 struct kvm_mmu_page *sp;
Avi Kivity7ebaf152010-10-03 18:51:39 +02002712 unsigned i;
Joerg Roedel651dd372010-09-10 17:30:59 +02002713
2714 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2715 spin_lock(&vcpu->kvm->mmu_lock);
2716 kvm_mmu_free_some_pages(vcpu);
2717 sp = kvm_mmu_get_page(vcpu, 0, 0, PT64_ROOT_LEVEL,
2718 1, ACC_ALL, NULL);
2719 ++sp->root_count;
2720 spin_unlock(&vcpu->kvm->mmu_lock);
2721 vcpu->arch.mmu.root_hpa = __pa(sp->spt);
2722 } else if (vcpu->arch.mmu.shadow_root_level == PT32E_ROOT_LEVEL) {
2723 for (i = 0; i < 4; ++i) {
2724 hpa_t root = vcpu->arch.mmu.pae_root[i];
2725
2726 ASSERT(!VALID_PAGE(root));
2727 spin_lock(&vcpu->kvm->mmu_lock);
2728 kvm_mmu_free_some_pages(vcpu);
Avi Kivity649497d2010-12-28 12:09:07 +02002729 sp = kvm_mmu_get_page(vcpu, i << (30 - PAGE_SHIFT),
2730 i << 30,
Joerg Roedel651dd372010-09-10 17:30:59 +02002731 PT32_ROOT_LEVEL, 1, ACC_ALL,
2732 NULL);
2733 root = __pa(sp->spt);
2734 ++sp->root_count;
2735 spin_unlock(&vcpu->kvm->mmu_lock);
2736 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Joerg Roedel651dd372010-09-10 17:30:59 +02002737 }
Xiao Guangrong62927572010-09-27 18:02:12 +08002738 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Joerg Roedel651dd372010-09-10 17:30:59 +02002739 } else
2740 BUG();
2741
2742 return 0;
2743}
2744
2745static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08002746{
Avi Kivity4db35312007-11-21 15:28:32 +02002747 struct kvm_mmu_page *sp;
Joerg Roedel81407ca2010-09-10 17:31:00 +02002748 u64 pdptr, pm_mask;
2749 gfn_t root_gfn;
2750 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -08002751
Joerg Roedel5777ed32010-09-10 17:30:42 +02002752 root_gfn = vcpu->arch.mmu.get_cr3(vcpu) >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002753
Joerg Roedel651dd372010-09-10 17:30:59 +02002754 if (mmu_check_root(vcpu, root_gfn))
2755 return 1;
2756
2757 /*
2758 * Do we shadow a long mode page table? If so we need to
2759 * write-protect the guests page table root.
2760 */
2761 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002762 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002763
2764 ASSERT(!VALID_PAGE(root));
Joerg Roedel651dd372010-09-10 17:30:59 +02002765
Avi Kivity8facbbf2010-05-04 12:58:32 +03002766 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b6c92010-05-12 21:00:35 -03002767 kvm_mmu_free_some_pages(vcpu);
Joerg Roedel651dd372010-09-10 17:30:59 +02002768 sp = kvm_mmu_get_page(vcpu, root_gfn, 0, PT64_ROOT_LEVEL,
2769 0, ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002770 root = __pa(sp->spt);
2771 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002772 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002773 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002774 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002775 }
Joerg Roedelf87f9282010-09-02 17:29:45 +02002776
Joerg Roedel651dd372010-09-10 17:30:59 +02002777 /*
2778 * We shadow a 32 bit page table. This may be a legacy 2-level
Joerg Roedel81407ca2010-09-10 17:31:00 +02002779 * or a PAE 3-level page table. In either case we need to be aware that
2780 * the shadow page table may be a PAE or a long mode page table.
Joerg Roedel651dd372010-09-10 17:30:59 +02002781 */
Joerg Roedel81407ca2010-09-10 17:31:00 +02002782 pm_mask = PT_PRESENT_MASK;
2783 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL)
2784 pm_mask |= PT_ACCESSED_MASK | PT_WRITABLE_MASK | PT_USER_MASK;
2785
Avi Kivity17ac10a2007-01-05 16:36:40 -08002786 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002787 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08002788
2789 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002790 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivitye4e517b2011-07-28 11:36:17 +03002791 pdptr = vcpu->arch.mmu.get_pdptr(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03002792 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002793 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03002794 continue;
2795 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002796 root_gfn = pdptr >> PAGE_SHIFT;
Joerg Roedelf87f9282010-09-02 17:29:45 +02002797 if (mmu_check_root(vcpu, root_gfn))
2798 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07002799 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03002800 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti24955b6c92010-05-12 21:00:35 -03002801 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +02002802 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Joerg Roedel651dd372010-09-10 17:30:59 +02002803 PT32_ROOT_LEVEL, 0,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002804 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002805 root = __pa(sp->spt);
2806 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03002807 spin_unlock(&vcpu->kvm->mmu_lock);
2808
Joerg Roedel81407ca2010-09-10 17:31:00 +02002809 vcpu->arch.mmu.pae_root[i] = root | pm_mask;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002810 }
Xiao Guangrong62927572010-09-27 18:02:12 +08002811 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Joerg Roedel81407ca2010-09-10 17:31:00 +02002812
2813 /*
2814 * If we shadow a 32 bit page table with a long mode page
2815 * table we enter this path.
2816 */
2817 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2818 if (vcpu->arch.mmu.lm_root == NULL) {
2819 /*
2820 * The additional page necessary for this is only
2821 * allocated on demand.
2822 */
2823
2824 u64 *lm_root;
2825
2826 lm_root = (void*)get_zeroed_page(GFP_KERNEL);
2827 if (lm_root == NULL)
2828 return 1;
2829
2830 lm_root[0] = __pa(vcpu->arch.mmu.pae_root) | pm_mask;
2831
2832 vcpu->arch.mmu.lm_root = lm_root;
2833 }
2834
2835 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.lm_root);
2836 }
2837
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002838 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002839}
2840
Joerg Roedel651dd372010-09-10 17:30:59 +02002841static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
2842{
2843 if (vcpu->arch.mmu.direct_map)
2844 return mmu_alloc_direct_roots(vcpu);
2845 else
2846 return mmu_alloc_shadow_roots(vcpu);
2847}
2848
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002849static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2850{
2851 int i;
2852 struct kvm_mmu_page *sp;
2853
Joerg Roedel81407ca2010-09-10 17:31:00 +02002854 if (vcpu->arch.mmu.direct_map)
2855 return;
2856
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002857 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2858 return;
Xiao Guangrong69030742010-09-27 18:09:29 +08002859
Xiao Guangrongbebb1062011-07-12 03:23:20 +08002860 vcpu_clear_mmio_info(vcpu, ~0ul);
Xiao Guangrong0375f7f2011-11-28 20:41:00 +08002861 kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC);
Joerg Roedel81407ca2010-09-10 17:31:00 +02002862 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002863 hpa_t root = vcpu->arch.mmu.root_hpa;
2864 sp = page_header(root);
2865 mmu_sync_children(vcpu, sp);
Xiao Guangrong0375f7f2011-11-28 20:41:00 +08002866 kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002867 return;
2868 }
2869 for (i = 0; i < 4; ++i) {
2870 hpa_t root = vcpu->arch.mmu.pae_root[i];
2871
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002872 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002873 root &= PT64_BASE_ADDR_MASK;
2874 sp = page_header(root);
2875 mmu_sync_children(vcpu, sp);
2876 }
2877 }
Xiao Guangrong0375f7f2011-11-28 20:41:00 +08002878 kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002879}
2880
2881void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2882{
2883 spin_lock(&vcpu->kvm->mmu_lock);
2884 mmu_sync_roots(vcpu);
2885 spin_unlock(&vcpu->kvm->mmu_lock);
2886}
2887
Gleb Natapov1871c602010-02-10 14:21:32 +02002888static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
Avi Kivityab9ae312010-11-22 17:53:26 +02002889 u32 access, struct x86_exception *exception)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002890{
Avi Kivityab9ae312010-11-22 17:53:26 +02002891 if (exception)
2892 exception->error_code = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002893 return vaddr;
2894}
2895
Joerg Roedel6539e732010-09-10 17:30:50 +02002896static gpa_t nonpaging_gva_to_gpa_nested(struct kvm_vcpu *vcpu, gva_t vaddr,
Avi Kivityab9ae312010-11-22 17:53:26 +02002897 u32 access,
2898 struct x86_exception *exception)
Joerg Roedel6539e732010-09-10 17:30:50 +02002899{
Avi Kivityab9ae312010-11-22 17:53:26 +02002900 if (exception)
2901 exception->error_code = 0;
Joerg Roedel6539e732010-09-10 17:30:50 +02002902 return vcpu->arch.nested_mmu.translate_gpa(vcpu, vaddr, access);
2903}
2904
Xiao Guangrongce88dec2011-07-12 03:33:44 +08002905static bool quickly_check_mmio_pf(struct kvm_vcpu *vcpu, u64 addr, bool direct)
2906{
2907 if (direct)
2908 return vcpu_match_mmio_gpa(vcpu, addr);
2909
2910 return vcpu_match_mmio_gva(vcpu, addr);
2911}
2912
2913
2914/*
2915 * On direct hosts, the last spte is only allows two states
2916 * for mmio page fault:
2917 * - It is the mmio spte
2918 * - It is zapped or it is being zapped.
2919 *
2920 * This function completely checks the spte when the last spte
2921 * is not the mmio spte.
2922 */
2923static bool check_direct_spte_mmio_pf(u64 spte)
2924{
2925 return __check_direct_spte_mmio_pf(spte);
2926}
2927
2928static u64 walk_shadow_page_get_mmio_spte(struct kvm_vcpu *vcpu, u64 addr)
2929{
2930 struct kvm_shadow_walk_iterator iterator;
2931 u64 spte = 0ull;
2932
2933 walk_shadow_page_lockless_begin(vcpu);
2934 for_each_shadow_entry_lockless(vcpu, addr, iterator, spte)
2935 if (!is_shadow_present_pte(spte))
2936 break;
2937 walk_shadow_page_lockless_end(vcpu);
2938
2939 return spte;
2940}
2941
2942/*
2943 * If it is a real mmio page fault, return 1 and emulat the instruction
2944 * directly, return 0 to let CPU fault again on the address, -1 is
2945 * returned if bug is detected.
2946 */
2947int handle_mmio_page_fault_common(struct kvm_vcpu *vcpu, u64 addr, bool direct)
2948{
2949 u64 spte;
2950
2951 if (quickly_check_mmio_pf(vcpu, addr, direct))
2952 return 1;
2953
2954 spte = walk_shadow_page_get_mmio_spte(vcpu, addr);
2955
2956 if (is_mmio_spte(spte)) {
2957 gfn_t gfn = get_mmio_spte_gfn(spte);
2958 unsigned access = get_mmio_spte_access(spte);
2959
2960 if (direct)
2961 addr = 0;
Xiao Guangrong4f022642011-07-12 03:34:24 +08002962
2963 trace_handle_mmio_page_fault(addr, gfn, access);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08002964 vcpu_cache_mmio_info(vcpu, addr, gfn, access);
2965 return 1;
2966 }
2967
2968 /*
2969 * It's ok if the gva is remapped by other cpus on shadow guest,
2970 * it's a BUG if the gfn is not a mmio page.
2971 */
2972 if (direct && !check_direct_spte_mmio_pf(spte))
2973 return -1;
2974
2975 /*
2976 * If the page table is zapped by other cpus, let CPU fault again on
2977 * the address.
2978 */
2979 return 0;
2980}
2981EXPORT_SYMBOL_GPL(handle_mmio_page_fault_common);
2982
2983static int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr,
2984 u32 error_code, bool direct)
2985{
2986 int ret;
2987
2988 ret = handle_mmio_page_fault_common(vcpu, addr, direct);
2989 WARN_ON(ret < 0);
2990 return ret;
2991}
2992
Avi Kivity6aa8b732006-12-10 02:21:36 -08002993static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002994 u32 error_code, bool prefault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002995{
Avi Kivitye8332402007-12-09 18:43:00 +02002996 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002997 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002998
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002999 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003000
3001 if (unlikely(error_code & PFERR_RSVD_MASK))
3002 return handle_mmio_page_fault(vcpu, gva, error_code, true);
3003
Avi Kivitye2dec932007-01-05 16:36:54 -08003004 r = mmu_topup_memory_caches(vcpu);
3005 if (r)
3006 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08003007
Avi Kivity6aa8b732006-12-10 02:21:36 -08003008 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003009 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003010
Avi Kivitye8332402007-12-09 18:43:00 +02003011 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003012
Avi Kivitye8332402007-12-09 18:43:00 +02003013 return nonpaging_map(vcpu, gva & PAGE_MASK,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08003014 error_code & PFERR_WRITE_MASK, gfn, prefault);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003015}
3016
Jan Kiszka7e1fbea2010-10-20 15:18:02 +02003017static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn)
Gleb Natapovaf585b92010-10-14 11:22:46 +02003018{
3019 struct kvm_arch_async_pf arch;
Xiao Guangrongfb67e142010-12-07 10:35:25 +08003020
Gleb Natapov7c907052010-10-14 11:22:53 +02003021 arch.token = (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id;
Gleb Natapovaf585b92010-10-14 11:22:46 +02003022 arch.gfn = gfn;
Xiao Guangrongc4806ac2010-11-12 14:49:55 +08003023 arch.direct_map = vcpu->arch.mmu.direct_map;
Xiao Guangrongfb67e142010-12-07 10:35:25 +08003024 arch.cr3 = vcpu->arch.mmu.get_cr3(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02003025
3026 return kvm_setup_async_pf(vcpu, gva, gfn, &arch);
3027}
3028
3029static bool can_do_async_pf(struct kvm_vcpu *vcpu)
3030{
3031 if (unlikely(!irqchip_in_kernel(vcpu->kvm) ||
3032 kvm_event_needs_reinjection(vcpu)))
3033 return false;
3034
3035 return kvm_x86_ops->interrupt_allowed(vcpu);
3036}
3037
Xiao Guangrong78b2c542010-12-07 10:48:06 +08003038static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
Marcelo Tosatti612819c2010-10-22 14:18:18 -02003039 gva_t gva, pfn_t *pfn, bool write, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02003040{
3041 bool async;
3042
Marcelo Tosatti612819c2010-10-22 14:18:18 -02003043 *pfn = gfn_to_pfn_async(vcpu->kvm, gfn, &async, write, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02003044
3045 if (!async)
3046 return false; /* *pfn has correct page already */
3047
3048 put_page(pfn_to_page(*pfn));
3049
Xiao Guangrong78b2c542010-12-07 10:48:06 +08003050 if (!prefault && can_do_async_pf(vcpu)) {
Xiao Guangrongc9b263d2010-11-01 16:58:43 +08003051 trace_kvm_try_async_get_page(gva, gfn);
Gleb Natapovaf585b92010-10-14 11:22:46 +02003052 if (kvm_find_async_pf_gfn(vcpu, gfn)) {
3053 trace_kvm_async_pf_doublefault(gva, gfn);
3054 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
3055 return true;
3056 } else if (kvm_arch_setup_async_pf(vcpu, gva, gfn))
3057 return true;
3058 }
3059
Marcelo Tosatti612819c2010-10-22 14:18:18 -02003060 *pfn = gfn_to_pfn_prot(vcpu->kvm, gfn, write, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02003061
3062 return false;
3063}
3064
Gleb Natapov56028d02010-10-17 18:13:42 +02003065static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, u32 error_code,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08003066 bool prefault)
Joerg Roedelfb72d162008-02-07 13:47:44 +01003067{
Anthony Liguori35149e22008-04-02 14:46:56 -05003068 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01003069 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02003070 int level;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08003071 int force_pt_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03003072 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02003073 unsigned long mmu_seq;
Marcelo Tosatti612819c2010-10-22 14:18:18 -02003074 int write = error_code & PFERR_WRITE_MASK;
3075 bool map_writable;
Joerg Roedelfb72d162008-02-07 13:47:44 +01003076
3077 ASSERT(vcpu);
3078 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
3079
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003080 if (unlikely(error_code & PFERR_RSVD_MASK))
3081 return handle_mmio_page_fault(vcpu, gpa, error_code, true);
3082
Joerg Roedelfb72d162008-02-07 13:47:44 +01003083 r = mmu_topup_memory_caches(vcpu);
3084 if (r)
3085 return r;
3086
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08003087 force_pt_level = mapping_level_dirty_bitmap(vcpu, gfn);
3088 if (likely(!force_pt_level)) {
3089 level = mapping_level(vcpu, gfn);
3090 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
3091 } else
3092 level = PT_PAGE_TABLE_LEVEL;
Joerg Roedel852e3c12009-07-27 16:30:44 +02003093
Andrea Arcangelie930bff2008-07-25 16:24:52 +02003094 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03003095 smp_rmb();
Gleb Natapovaf585b92010-10-14 11:22:46 +02003096
Xiao Guangrong78b2c542010-12-07 10:48:06 +08003097 if (try_async_pf(vcpu, prefault, gfn, gpa, &pfn, write, &map_writable))
Gleb Natapovaf585b92010-10-14 11:22:46 +02003098 return 0;
3099
Xiao Guangrongd7c55202011-07-12 03:29:38 +08003100 if (handle_abnormal_pfn(vcpu, 0, gfn, pfn, ACC_ALL, &r))
3101 return r;
3102
Joerg Roedelfb72d162008-02-07 13:47:44 +01003103 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02003104 if (mmu_notifier_retry(vcpu, mmu_seq))
3105 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01003106 kvm_mmu_free_some_pages(vcpu);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08003107 if (likely(!force_pt_level))
3108 transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02003109 r = __direct_map(vcpu, gpa, write, map_writable,
Xiao Guangrong2ec47392010-12-07 10:34:42 +08003110 level, gfn, pfn, prefault);
Joerg Roedelfb72d162008-02-07 13:47:44 +01003111 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01003112
3113 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02003114
3115out_unlock:
3116 spin_unlock(&vcpu->kvm->mmu_lock);
3117 kvm_release_pfn_clean(pfn);
3118 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01003119}
3120
Avi Kivity6aa8b732006-12-10 02:21:36 -08003121static void nonpaging_free(struct kvm_vcpu *vcpu)
3122{
Avi Kivity17ac10a2007-01-05 16:36:40 -08003123 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003124}
3125
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003126static int nonpaging_init_context(struct kvm_vcpu *vcpu,
3127 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003128{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003129 context->new_cr3 = nonpaging_new_cr3;
3130 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003131 context->gva_to_gpa = nonpaging_gva_to_gpa;
3132 context->free = nonpaging_free;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03003133 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03003134 context->invlpg = nonpaging_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08003135 context->update_pte = nonpaging_update_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08003136 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003137 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03003138 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02003139 context->direct_map = true;
Joerg Roedel2d48a982010-09-10 17:31:01 +02003140 context->nx = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003141 return 0;
3142}
3143
Avi Kivityd835dfe2007-11-21 02:57:59 +02003144void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003145{
Avi Kivity1165f5f2007-04-19 17:27:43 +03003146 ++vcpu->stat.tlb_flush;
Avi Kivitya8eeb042010-05-10 12:34:53 +03003147 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003148}
3149
3150static void paging_new_cr3(struct kvm_vcpu *vcpu)
3151{
Avi Kivity9f8fe502010-12-05 17:30:00 +02003152 pgprintk("%s: cr3 %lx\n", __func__, kvm_read_cr3(vcpu));
Avi Kivitycea0f0e2007-01-05 16:36:43 -08003153 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003154}
3155
Joerg Roedel5777ed32010-09-10 17:30:42 +02003156static unsigned long get_cr3(struct kvm_vcpu *vcpu)
3157{
Avi Kivity9f8fe502010-12-05 17:30:00 +02003158 return kvm_read_cr3(vcpu);
Joerg Roedel5777ed32010-09-10 17:30:42 +02003159}
3160
Avi Kivity6389ee92010-11-29 16:12:30 +02003161static void inject_page_fault(struct kvm_vcpu *vcpu,
3162 struct x86_exception *fault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003163{
Avi Kivity6389ee92010-11-29 16:12:30 +02003164 vcpu->arch.mmu.inject_page_fault(vcpu, fault);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003165}
3166
Avi Kivity6aa8b732006-12-10 02:21:36 -08003167static void paging_free(struct kvm_vcpu *vcpu)
3168{
3169 nonpaging_free(vcpu);
3170}
3171
Joerg Roedel3241f222010-09-10 17:30:45 +02003172static bool is_rsvd_bits_set(struct kvm_mmu *mmu, u64 gpte, int level)
Dong, Eddie82725b22009-03-30 16:21:08 +08003173{
3174 int bit7;
3175
3176 bit7 = (gpte >> 7) & 1;
Joerg Roedel3241f222010-09-10 17:30:45 +02003177 return (gpte & mmu->rsvd_bits_mask[bit7][level-1]) != 0;
Dong, Eddie82725b22009-03-30 16:21:08 +08003178}
3179
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003180static bool sync_mmio_spte(u64 *sptep, gfn_t gfn, unsigned access,
3181 int *nr_present)
3182{
3183 if (unlikely(is_mmio_spte(*sptep))) {
3184 if (gfn != get_mmio_spte_gfn(*sptep)) {
3185 mmu_spte_clear_no_track(sptep);
3186 return true;
3187 }
3188
3189 (*nr_present)++;
3190 mark_mmio_spte(sptep, gfn, access);
3191 return true;
3192 }
3193
3194 return false;
3195}
3196
Avi Kivity6aa8b732006-12-10 02:21:36 -08003197#define PTTYPE 64
3198#include "paging_tmpl.h"
3199#undef PTTYPE
3200
3201#define PTTYPE 32
3202#include "paging_tmpl.h"
3203#undef PTTYPE
3204
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003205static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
3206 struct kvm_mmu *context,
3207 int level)
Dong, Eddie82725b22009-03-30 16:21:08 +08003208{
Dong, Eddie82725b22009-03-30 16:21:08 +08003209 int maxphyaddr = cpuid_maxphyaddr(vcpu);
3210 u64 exb_bit_rsvd = 0;
3211
Joerg Roedel2d48a982010-09-10 17:31:01 +02003212 if (!context->nx)
Dong, Eddie82725b22009-03-30 16:21:08 +08003213 exb_bit_rsvd = rsvd_bits(63, 63);
3214 switch (level) {
3215 case PT32_ROOT_LEVEL:
3216 /* no rsvd bits for 2 level 4K page table entries */
3217 context->rsvd_bits_mask[0][1] = 0;
3218 context->rsvd_bits_mask[0][0] = 0;
Xiao Guangrongf815bce2010-03-19 17:58:53 +08003219 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
3220
3221 if (!is_pse(vcpu)) {
3222 context->rsvd_bits_mask[1][1] = 0;
3223 break;
3224 }
3225
Dong, Eddie82725b22009-03-30 16:21:08 +08003226 if (is_cpuid_PSE36())
3227 /* 36bits PSE 4MB page */
3228 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
3229 else
3230 /* 32 bits PSE 4MB page */
3231 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08003232 break;
3233 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08003234 context->rsvd_bits_mask[0][2] =
3235 rsvd_bits(maxphyaddr, 63) |
3236 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08003237 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08003238 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08003239 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
3240 rsvd_bits(maxphyaddr, 62); /* PTE */
3241 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
3242 rsvd_bits(maxphyaddr, 62) |
3243 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08003244 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08003245 break;
3246 case PT64_ROOT_LEVEL:
3247 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
3248 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
3249 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
3250 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
3251 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08003252 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08003253 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
3254 rsvd_bits(maxphyaddr, 51);
3255 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
Joerg Roedele04da982009-07-27 16:30:45 +02003256 context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
3257 rsvd_bits(maxphyaddr, 51) |
3258 rsvd_bits(13, 29);
Dong, Eddie82725b22009-03-30 16:21:08 +08003259 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08003260 rsvd_bits(maxphyaddr, 51) |
3261 rsvd_bits(13, 20); /* large page */
Xiao Guangrongf815bce2010-03-19 17:58:53 +08003262 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08003263 break;
3264 }
3265}
3266
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003267static int paging64_init_context_common(struct kvm_vcpu *vcpu,
3268 struct kvm_mmu *context,
3269 int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003270{
Joerg Roedel2d48a982010-09-10 17:31:01 +02003271 context->nx = is_nx(vcpu);
3272
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003273 reset_rsvds_bits_mask(vcpu, context, level);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003274
3275 ASSERT(is_pae(vcpu));
3276 context->new_cr3 = paging_new_cr3;
3277 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003278 context->gva_to_gpa = paging64_gva_to_gpa;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03003279 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03003280 context->invlpg = paging64_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08003281 context->update_pte = paging64_update_pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003282 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003283 context->root_level = level;
3284 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03003285 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02003286 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003287 return 0;
3288}
3289
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003290static int paging64_init_context(struct kvm_vcpu *vcpu,
3291 struct kvm_mmu *context)
Avi Kivity17ac10a2007-01-05 16:36:40 -08003292{
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003293 return paging64_init_context_common(vcpu, context, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003294}
3295
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003296static int paging32_init_context(struct kvm_vcpu *vcpu,
3297 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003298{
Joerg Roedel2d48a982010-09-10 17:31:01 +02003299 context->nx = false;
3300
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003301 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003302
3303 context->new_cr3 = paging_new_cr3;
3304 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003305 context->gva_to_gpa = paging32_gva_to_gpa;
3306 context->free = paging_free;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03003307 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03003308 context->invlpg = paging32_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08003309 context->update_pte = paging32_update_pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003310 context->root_level = PT32_ROOT_LEVEL;
3311 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03003312 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02003313 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003314 return 0;
3315}
3316
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003317static int paging32E_init_context(struct kvm_vcpu *vcpu,
3318 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003319{
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003320 return paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003321}
3322
Joerg Roedelfb72d162008-02-07 13:47:44 +01003323static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
3324{
Joerg Roedel14dfe852010-09-10 17:30:49 +02003325 struct kvm_mmu *context = vcpu->arch.walk_mmu;
Joerg Roedelfb72d162008-02-07 13:47:44 +01003326
Avi Kivityc445f8e2010-12-21 16:26:01 +02003327 context->base_role.word = 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01003328 context->new_cr3 = nonpaging_new_cr3;
3329 context->page_fault = tdp_page_fault;
3330 context->free = nonpaging_free;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03003331 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03003332 context->invlpg = nonpaging_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08003333 context->update_pte = nonpaging_update_pte;
Sheng Yang67253af2008-04-25 10:20:22 +08003334 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01003335 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02003336 context->direct_map = true;
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003337 context->set_cr3 = kvm_x86_ops->set_tdp_cr3;
Joerg Roedel5777ed32010-09-10 17:30:42 +02003338 context->get_cr3 = get_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03003339 context->get_pdptr = kvm_pdptr_read;
Joerg Roedelcb659db2010-09-10 17:30:43 +02003340 context->inject_page_fault = kvm_inject_page_fault;
Joerg Roedel2d48a982010-09-10 17:31:01 +02003341 context->nx = is_nx(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01003342
3343 if (!is_paging(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003344 context->nx = false;
Joerg Roedelfb72d162008-02-07 13:47:44 +01003345 context->gva_to_gpa = nonpaging_gva_to_gpa;
3346 context->root_level = 0;
3347 } else if (is_long_mode(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003348 context->nx = is_nx(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003349 reset_rsvds_bits_mask(vcpu, context, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01003350 context->gva_to_gpa = paging64_gva_to_gpa;
3351 context->root_level = PT64_ROOT_LEVEL;
3352 } else if (is_pae(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003353 context->nx = is_nx(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003354 reset_rsvds_bits_mask(vcpu, context, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01003355 context->gva_to_gpa = paging64_gva_to_gpa;
3356 context->root_level = PT32E_ROOT_LEVEL;
3357 } else {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003358 context->nx = false;
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003359 reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01003360 context->gva_to_gpa = paging32_gva_to_gpa;
3361 context->root_level = PT32_ROOT_LEVEL;
3362 }
3363
3364 return 0;
3365}
3366
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003367int kvm_init_shadow_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003368{
Avi Kivitya770f6f2008-12-21 19:20:09 +02003369 int r;
Avi Kivity411c5882011-06-06 16:11:54 +03003370 bool smep = kvm_read_cr4_bits(vcpu, X86_CR4_SMEP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003371 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003372 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003373
3374 if (!is_paging(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003375 r = nonpaging_init_context(vcpu, context);
Avi Kivitya9058ec2006-12-29 16:49:37 -08003376 else if (is_long_mode(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003377 r = paging64_init_context(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003378 else if (is_pae(vcpu))
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003379 r = paging32E_init_context(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003380 else
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003381 r = paging32_init_context(vcpu, context);
Avi Kivitya770f6f2008-12-21 19:20:09 +02003382
Avi Kivity5b7e0102010-04-14 19:20:03 +03003383 vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
Joerg Roedelf43addd2010-09-10 17:30:40 +02003384 vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
Avi Kivity411c5882011-06-06 16:11:54 +03003385 vcpu->arch.mmu.base_role.smep_andnot_wp
3386 = smep && !is_write_protection(vcpu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003387
3388 return r;
3389}
3390EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu);
3391
3392static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
3393{
Joerg Roedel14dfe852010-09-10 17:30:49 +02003394 int r = kvm_init_shadow_mmu(vcpu, vcpu->arch.walk_mmu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003395
Joerg Roedel14dfe852010-09-10 17:30:49 +02003396 vcpu->arch.walk_mmu->set_cr3 = kvm_x86_ops->set_cr3;
3397 vcpu->arch.walk_mmu->get_cr3 = get_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03003398 vcpu->arch.walk_mmu->get_pdptr = kvm_pdptr_read;
Joerg Roedel14dfe852010-09-10 17:30:49 +02003399 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
Avi Kivitya770f6f2008-12-21 19:20:09 +02003400
3401 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003402}
3403
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003404static int init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
3405{
3406 struct kvm_mmu *g_context = &vcpu->arch.nested_mmu;
3407
3408 g_context->get_cr3 = get_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03003409 g_context->get_pdptr = kvm_pdptr_read;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003410 g_context->inject_page_fault = kvm_inject_page_fault;
3411
3412 /*
3413 * Note that arch.mmu.gva_to_gpa translates l2_gva to l1_gpa. The
3414 * translation of l2_gpa to l1_gpa addresses is done using the
3415 * arch.nested_mmu.gva_to_gpa function. Basically the gva_to_gpa
3416 * functions between mmu and nested_mmu are swapped.
3417 */
3418 if (!is_paging(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003419 g_context->nx = false;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003420 g_context->root_level = 0;
3421 g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested;
3422 } else if (is_long_mode(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003423 g_context->nx = is_nx(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003424 reset_rsvds_bits_mask(vcpu, g_context, PT64_ROOT_LEVEL);
3425 g_context->root_level = PT64_ROOT_LEVEL;
3426 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
3427 } else if (is_pae(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003428 g_context->nx = is_nx(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003429 reset_rsvds_bits_mask(vcpu, g_context, PT32E_ROOT_LEVEL);
3430 g_context->root_level = PT32E_ROOT_LEVEL;
3431 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
3432 } else {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003433 g_context->nx = false;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003434 reset_rsvds_bits_mask(vcpu, g_context, PT32_ROOT_LEVEL);
3435 g_context->root_level = PT32_ROOT_LEVEL;
3436 g_context->gva_to_gpa = paging32_gva_to_gpa_nested;
3437 }
3438
3439 return 0;
3440}
3441
Joerg Roedelfb72d162008-02-07 13:47:44 +01003442static int init_kvm_mmu(struct kvm_vcpu *vcpu)
3443{
Joerg Roedel02f59dc2010-09-10 17:30:54 +02003444 if (mmu_is_nested(vcpu))
3445 return init_kvm_nested_mmu(vcpu);
3446 else if (tdp_enabled)
Joerg Roedelfb72d162008-02-07 13:47:44 +01003447 return init_kvm_tdp_mmu(vcpu);
3448 else
3449 return init_kvm_softmmu(vcpu);
3450}
3451
Avi Kivity6aa8b732006-12-10 02:21:36 -08003452static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
3453{
3454 ASSERT(vcpu);
Sheng Yang62ad0752010-05-12 16:40:41 +08003455 if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
3456 /* mmu.free() should set root_hpa = INVALID_PAGE */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003457 vcpu->arch.mmu.free(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003458}
3459
3460int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
3461{
Avi Kivity17c3ba92007-06-04 15:58:30 +03003462 destroy_kvm_mmu(vcpu);
Marcelo Tosattif8f7e5e2011-06-21 14:00:10 -03003463 return init_kvm_mmu(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03003464}
Eddie Dong8668a3c2007-10-10 14:26:45 +08003465EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03003466
3467int kvm_mmu_load(struct kvm_vcpu *vcpu)
3468{
Avi Kivity714b93d2007-01-05 16:36:53 -08003469 int r;
3470
Avi Kivitye2dec932007-01-05 16:36:54 -08003471 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03003472 if (r)
3473 goto out;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003474 r = mmu_alloc_roots(vcpu);
Avi Kivity8facbbf2010-05-04 12:58:32 +03003475 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003476 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003477 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003478 if (r)
3479 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08003480 /* set_cr3() should ensure TLB has been flushed */
Joerg Roedelf43addd2010-09-10 17:30:40 +02003481 vcpu->arch.mmu.set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08003482out:
3483 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003484}
Avi Kivity17c3ba92007-06-04 15:58:30 +03003485EXPORT_SYMBOL_GPL(kvm_mmu_load);
3486
3487void kvm_mmu_unload(struct kvm_vcpu *vcpu)
3488{
3489 mmu_free_roots(vcpu);
3490}
Joerg Roedel4b161842010-09-10 17:31:03 +02003491EXPORT_SYMBOL_GPL(kvm_mmu_unload);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003492
Avi Kivity00284252007-05-01 16:53:31 +03003493static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Xiao Guangrong7c562522011-03-28 10:29:27 +08003494 struct kvm_mmu_page *sp, u64 *spte,
3495 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03003496{
Marcelo Tosatti30945382008-06-11 20:32:40 -03003497 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02003498 ++vcpu->kvm->stat.mmu_pde_zapped;
3499 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03003500 }
Avi Kivity00284252007-05-01 16:53:31 +03003501
Avi Kivity4cee5762007-11-18 16:37:07 +02003502 ++vcpu->kvm->stat.mmu_pte_updated;
Xiao Guangrong7c562522011-03-28 10:29:27 +08003503 vcpu->arch.mmu.update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03003504}
3505
Avi Kivity79539ce2007-11-21 02:06:21 +02003506static bool need_remote_flush(u64 old, u64 new)
3507{
3508 if (!is_shadow_present_pte(old))
3509 return false;
3510 if (!is_shadow_present_pte(new))
3511 return true;
3512 if ((old ^ new) & PT64_BASE_ADDR_MASK)
3513 return true;
3514 old ^= PT64_NX_MASK;
3515 new ^= PT64_NX_MASK;
3516 return (old & ~new & PT64_PERM_MASK) != 0;
3517}
3518
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003519static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page,
3520 bool remote_flush, bool local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003521{
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003522 if (zap_page)
3523 return;
3524
3525 if (remote_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003526 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003527 else if (local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02003528 kvm_mmu_flush_tlb(vcpu);
3529}
3530
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08003531static u64 mmu_pte_write_fetch_gpte(struct kvm_vcpu *vcpu, gpa_t *gpa,
3532 const u8 *new, int *bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08003533{
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08003534 u64 gentry;
3535 int r;
Avi Kivity72016f32010-03-15 13:59:53 +02003536
Avi Kivity08e850c2010-03-15 13:59:57 +02003537 /*
3538 * Assume that the pte write on a page table of the same type
Xiao Guangrong49b26e22011-03-04 19:00:00 +08003539 * as the current vcpu paging mode since we update the sptes only
3540 * when they have the same mode.
Avi Kivity08e850c2010-03-15 13:59:57 +02003541 */
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08003542 if (is_pae(vcpu) && *bytes == 4) {
Avi Kivity08e850c2010-03-15 13:59:57 +02003543 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08003544 *gpa &= ~(gpa_t)7;
3545 *bytes = 8;
3546 r = kvm_read_guest(vcpu->kvm, *gpa, &gentry, min(*bytes, 8));
Avi Kivity08e850c2010-03-15 13:59:57 +02003547 if (r)
3548 gentry = 0;
3549 new = (const u8 *)&gentry;
3550 }
3551
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08003552 switch (*bytes) {
Avi Kivity72016f32010-03-15 13:59:53 +02003553 case 4:
3554 gentry = *(const u32 *)new;
3555 break;
3556 case 8:
3557 gentry = *(const u64 *)new;
3558 break;
3559 default:
3560 gentry = 0;
3561 break;
3562 }
3563
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08003564 return gentry;
3565}
3566
3567/*
3568 * If we're seeing too many writes to a page, it may no longer be a page table,
3569 * or we may be forking, in which case it is better to unmap the page.
3570 */
Xiao Guangronga30f47c2011-09-22 16:58:36 +08003571static bool detect_write_flooding(struct kvm_mmu_page *sp, u64 *spte)
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08003572{
Xiao Guangronga30f47c2011-09-22 16:58:36 +08003573 /*
3574 * Skip write-flooding detected for the sp whose level is 1, because
3575 * it can become unsync, then the guest page is not write-protected.
3576 */
3577 if (sp->role.level == 1)
3578 return false;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08003579
Xiao Guangronga30f47c2011-09-22 16:58:36 +08003580 return ++sp->write_flooding_count >= 3;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08003581}
3582
3583/*
3584 * Misaligned accesses are too much trouble to fix up; also, they usually
3585 * indicate a page is not used as a page table.
3586 */
3587static bool detect_write_misaligned(struct kvm_mmu_page *sp, gpa_t gpa,
3588 int bytes)
3589{
3590 unsigned offset, pte_size, misaligned;
3591
3592 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
3593 gpa, bytes, sp->role.word);
3594
3595 offset = offset_in_page(gpa);
3596 pte_size = sp->role.cr4_pae ? 8 : 4;
Xiao Guangrong5d9ca302011-09-22 16:57:55 +08003597
3598 /*
3599 * Sometimes, the OS only writes the last one bytes to update status
3600 * bits, for example, in linux, andb instruction is used in clear_bit().
3601 */
3602 if (!(offset & (pte_size - 1)) && bytes == 1)
3603 return false;
3604
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08003605 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
3606 misaligned |= bytes < 4;
3607
3608 return misaligned;
3609}
3610
3611static u64 *get_written_sptes(struct kvm_mmu_page *sp, gpa_t gpa, int *nspte)
3612{
3613 unsigned page_offset, quadrant;
3614 u64 *spte;
3615 int level;
3616
3617 page_offset = offset_in_page(gpa);
3618 level = sp->role.level;
3619 *nspte = 1;
3620 if (!sp->role.cr4_pae) {
3621 page_offset <<= 1; /* 32->64 */
3622 /*
3623 * A 32-bit pde maps 4MB while the shadow pdes map
3624 * only 2MB. So we need to double the offset again
3625 * and zap two pdes instead of one.
3626 */
3627 if (level == PT32_ROOT_LEVEL) {
3628 page_offset &= ~7; /* kill rounding error */
3629 page_offset <<= 1;
3630 *nspte = 2;
3631 }
3632 quadrant = page_offset >> PAGE_SHIFT;
3633 page_offset &= ~PAGE_MASK;
3634 if (quadrant != sp->role.quadrant)
3635 return NULL;
3636 }
3637
3638 spte = &sp->spt[page_offset / sizeof(*spte)];
3639 return spte;
3640}
3641
3642void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
3643 const u8 *new, int bytes)
3644{
3645 gfn_t gfn = gpa >> PAGE_SHIFT;
3646 union kvm_mmu_page_role mask = { .word = 0 };
3647 struct kvm_mmu_page *sp;
3648 struct hlist_node *node;
3649 LIST_HEAD(invalid_list);
3650 u64 entry, gentry, *spte;
3651 int npte;
Xiao Guangronga30f47c2011-09-22 16:58:36 +08003652 bool remote_flush, local_flush, zap_page;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08003653
3654 /*
3655 * If we don't have indirect shadow pages, it means no page is
3656 * write-protected, so we can exit simply.
3657 */
3658 if (!ACCESS_ONCE(vcpu->kvm->arch.indirect_shadow_pages))
3659 return;
3660
3661 zap_page = remote_flush = local_flush = false;
3662
3663 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
3664
3665 gentry = mmu_pte_write_fetch_gpte(vcpu, &gpa, new, &bytes);
3666
3667 /*
3668 * No need to care whether allocation memory is successful
3669 * or not since pte prefetch is skiped if it does not have
3670 * enough objects in the cache.
3671 */
3672 mmu_topup_memory_caches(vcpu);
3673
3674 spin_lock(&vcpu->kvm->mmu_lock);
3675 ++vcpu->kvm->stat.mmu_pte_write;
Xiao Guangrong0375f7f2011-11-28 20:41:00 +08003676 kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE);
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08003677
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003678 mask.cr0_wp = mask.cr4_pae = mask.nxe = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003679 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node) {
Xiao Guangronga30f47c2011-09-22 16:58:36 +08003680 spte = get_written_sptes(sp, gpa, &npte);
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08003681
Xiao Guangronga30f47c2011-09-22 16:58:36 +08003682 if (detect_write_misaligned(sp, gpa, bytes) ||
3683 detect_write_flooding(sp, spte)) {
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003684 zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
Xiao Guangrongf41d3352010-06-04 21:56:11 +08003685 &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02003686 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08003687 continue;
3688 }
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08003689
3690 spte = get_written_sptes(sp, gpa, &npte);
3691 if (!spte)
3692 continue;
3693
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003694 local_flush = true;
Avi Kivityac1b7142007-03-08 17:13:32 +02003695 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02003696 entry = *spte;
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08003697 mmu_page_zap_pte(vcpu->kvm, sp, spte);
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08003698 if (gentry &&
3699 !((sp->role.word ^ vcpu->arch.mmu.base_role.word)
Xiao Guangrongf759e2b2011-09-22 16:53:17 +08003700 & mask.word) && rmap_can_add(vcpu))
Xiao Guangrong7c562522011-03-28 10:29:27 +08003701 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003702 if (!remote_flush && need_remote_flush(entry, *spte))
3703 remote_flush = true;
Avi Kivityac1b7142007-03-08 17:13:32 +02003704 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08003705 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08003706 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08003707 mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003708 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong0375f7f2011-11-28 20:41:00 +08003709 kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003710 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivityda4a00f2007-01-05 16:36:44 -08003711}
3712
Avi Kivitya4360362007-01-05 16:36:45 -08003713int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
3714{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003715 gpa_t gpa;
3716 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08003717
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02003718 if (vcpu->arch.mmu.direct_map)
Avi Kivity60f24782009-08-27 13:37:06 +03003719 return 0;
3720
Gleb Natapov1871c602010-02-10 14:21:32 +02003721 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003722
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003723 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08003724
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003725 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08003726}
Avi Kivity577bdc42008-07-19 08:57:05 +03003727EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08003728
Avi Kivity22d95b12007-09-14 20:26:06 +03003729void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08003730{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003731 LIST_HEAD(invalid_list);
Xiao Guangrong103ad252010-06-04 21:54:38 +08003732
Dave Hansene0df7b92010-08-19 18:11:05 -07003733 while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES &&
Izik Eidus3b80fff2009-07-28 15:26:58 -03003734 !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
Avi Kivity4db35312007-11-21 15:28:32 +02003735 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08003736
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003737 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02003738 struct kvm_mmu_page, link);
Dave Hansene0df7b92010-08-19 18:11:05 -07003739 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02003740 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08003741 }
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08003742 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Avi Kivityebeace82007-01-05 16:36:47 -08003743}
Avi Kivityebeace82007-01-05 16:36:47 -08003744
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08003745static bool is_mmio_page_fault(struct kvm_vcpu *vcpu, gva_t addr)
3746{
3747 if (vcpu->arch.mmu.direct_map || mmu_is_nested(vcpu))
3748 return vcpu_match_mmio_gpa(vcpu, addr);
3749
3750 return vcpu_match_mmio_gva(vcpu, addr);
3751}
3752
Andre Przywaradc25e892010-12-21 11:12:07 +01003753int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code,
3754 void *insn, int insn_len)
Avi Kivity30677142007-10-28 18:48:59 +02003755{
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08003756 int r, emulation_type = EMULTYPE_RETRY;
Avi Kivity30677142007-10-28 18:48:59 +02003757 enum emulation_result er;
3758
Gleb Natapov56028d02010-10-17 18:13:42 +02003759 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code, false);
Avi Kivity30677142007-10-28 18:48:59 +02003760 if (r < 0)
3761 goto out;
3762
3763 if (!r) {
3764 r = 1;
3765 goto out;
3766 }
3767
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08003768 if (is_mmio_page_fault(vcpu, cr2))
3769 emulation_type = 0;
3770
3771 er = x86_emulate_instruction(vcpu, cr2, emulation_type, insn, insn_len);
Avi Kivity30677142007-10-28 18:48:59 +02003772
3773 switch (er) {
3774 case EMULATE_DONE:
3775 return 1;
3776 case EMULATE_DO_MMIO:
3777 ++vcpu->stat.mmio_exits;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03003778 /* fall through */
Avi Kivity30677142007-10-28 18:48:59 +02003779 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03003780 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02003781 default:
3782 BUG();
3783 }
3784out:
Avi Kivity30677142007-10-28 18:48:59 +02003785 return r;
3786}
3787EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
3788
Marcelo Tosattia7052892008-09-23 13:18:35 -03003789void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
3790{
Marcelo Tosattia7052892008-09-23 13:18:35 -03003791 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003792 kvm_mmu_flush_tlb(vcpu);
3793 ++vcpu->stat.invlpg;
3794}
3795EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
3796
Joerg Roedel18552672008-02-07 13:47:41 +01003797void kvm_enable_tdp(void)
3798{
3799 tdp_enabled = true;
3800}
3801EXPORT_SYMBOL_GPL(kvm_enable_tdp);
3802
Joerg Roedel5f4cb662008-07-14 20:36:36 +02003803void kvm_disable_tdp(void)
3804{
3805 tdp_enabled = false;
3806}
3807EXPORT_SYMBOL_GPL(kvm_disable_tdp);
3808
Avi Kivity6aa8b732006-12-10 02:21:36 -08003809static void free_mmu_pages(struct kvm_vcpu *vcpu)
3810{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003811 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Joerg Roedel81407ca2010-09-10 17:31:00 +02003812 if (vcpu->arch.mmu.lm_root != NULL)
3813 free_page((unsigned long)vcpu->arch.mmu.lm_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003814}
3815
3816static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
3817{
Avi Kivity17ac10a2007-01-05 16:36:40 -08003818 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003819 int i;
3820
3821 ASSERT(vcpu);
3822
Avi Kivity17ac10a2007-01-05 16:36:40 -08003823 /*
3824 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
3825 * Therefore we need to allocate shadow page tables in the first
3826 * 4GB of memory, which happens to fit the DMA32 zone.
3827 */
3828 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
3829 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08003830 return -ENOMEM;
3831
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003832 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003833 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003834 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003835
Avi Kivity6aa8b732006-12-10 02:21:36 -08003836 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003837}
3838
Ingo Molnar8018c272006-12-29 16:50:01 -08003839int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003840{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003841 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003842 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003843
Ingo Molnar8018c272006-12-29 16:50:01 -08003844 return alloc_mmu_pages(vcpu);
3845}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003846
Ingo Molnar8018c272006-12-29 16:50:01 -08003847int kvm_mmu_setup(struct kvm_vcpu *vcpu)
3848{
3849 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003850 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08003851
Ingo Molnar8018c272006-12-29 16:50:01 -08003852 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003853}
3854
Avi Kivity90cb0522007-07-17 13:04:56 +03003855void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003856{
Avi Kivity4db35312007-11-21 15:28:32 +02003857 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003858
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003859 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003860 int i;
3861 u64 *pt;
3862
Sheng Yang291f26b2008-10-16 17:30:57 +08003863 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003864 continue;
3865
Avi Kivity4db35312007-11-21 15:28:32 +02003866 pt = sp->spt;
Avi Kivity8234b222010-12-27 12:08:45 +02003867 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Xiao Guangrongda8dc752011-03-04 18:56:41 +08003868 if (!is_shadow_present_pte(pt[i]) ||
3869 !is_last_spte(pt[i], sp->role.level))
3870 continue;
3871
3872 if (is_large_pte(pt[i])) {
Xiao Guangrongc3707952011-07-12 03:28:04 +08003873 drop_spte(kvm, &pt[i]);
Avi Kivity8234b222010-12-27 12:08:45 +02003874 --kvm->stat.lpages;
Xiao Guangrongda8dc752011-03-04 18:56:41 +08003875 continue;
Avi Kivity8234b222010-12-27 12:08:45 +02003876 }
Xiao Guangrongda8dc752011-03-04 18:56:41 +08003877
Avi Kivity6aa8b732006-12-10 02:21:36 -08003878 /* avoid RMW */
Gui Jianfeng01c168a2010-05-27 16:09:48 +08003879 if (is_writable_pte(pt[i]))
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08003880 mmu_spte_update(&pt[i],
3881 pt[i] & ~PT_WRITABLE_MASK);
Avi Kivity8234b222010-12-27 12:08:45 +02003882 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003883 }
Avi Kivity171d5952008-08-27 16:40:51 +03003884 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003885}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003886
Avi Kivity90cb0522007-07-17 13:04:56 +03003887void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03003888{
Avi Kivity4db35312007-11-21 15:28:32 +02003889 struct kvm_mmu_page *sp, *node;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003890 LIST_HEAD(invalid_list);
Dor Laore0fa8262007-03-30 13:06:33 +03003891
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003892 spin_lock(&kvm->mmu_lock);
Xiao Guangrong3246af02010-04-16 16:35:54 +08003893restart:
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003894 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003895 if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list))
Xiao Guangrong3246af02010-04-16 16:35:54 +08003896 goto restart;
3897
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003898 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003899 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03003900}
3901
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003902static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm,
3903 struct list_head *invalid_list)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003904{
3905 struct kvm_mmu_page *page;
3906
3907 page = container_of(kvm->arch.active_mmu_pages.prev,
3908 struct kvm_mmu_page, link);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003909 return kvm_mmu_prepare_zap_page(kvm, page, invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003910}
3911
Ying Han1495f232011-05-24 17:12:27 -07003912static int mmu_shrink(struct shrinker *shrink, struct shrink_control *sc)
Izik Eidus3ee16c82008-03-30 15:17:21 +03003913{
3914 struct kvm *kvm;
3915 struct kvm *kvm_freed = NULL;
Ying Han1495f232011-05-24 17:12:27 -07003916 int nr_to_scan = sc->nr_to_scan;
Dave Hansen45221ab2010-08-19 18:11:37 -07003917
3918 if (nr_to_scan == 0)
3919 goto out;
Izik Eidus3ee16c82008-03-30 15:17:21 +03003920
Jan Kiszkae935b832011-02-08 12:55:33 +01003921 raw_spin_lock(&kvm_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003922
3923 list_for_each_entry(kvm, &vm_list, vm_list) {
Dave Hansen45221ab2010-08-19 18:11:37 -07003924 int idx, freed_pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003925 LIST_HEAD(invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003926
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003927 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003928 spin_lock(&kvm->mmu_lock);
Dave Hansen45221ab2010-08-19 18:11:37 -07003929 if (!kvm_freed && nr_to_scan > 0 &&
3930 kvm->arch.n_used_mmu_pages > 0) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003931 freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm,
3932 &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003933 kvm_freed = kvm;
3934 }
3935 nr_to_scan--;
3936
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003937 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003938 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02003939 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003940 }
3941 if (kvm_freed)
3942 list_move_tail(&kvm_freed->vm_list, &vm_list);
3943
Jan Kiszkae935b832011-02-08 12:55:33 +01003944 raw_spin_unlock(&kvm_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003945
Dave Hansen45221ab2010-08-19 18:11:37 -07003946out:
3947 return percpu_counter_read_positive(&kvm_total_used_mmu_pages);
Izik Eidus3ee16c82008-03-30 15:17:21 +03003948}
3949
3950static struct shrinker mmu_shrinker = {
3951 .shrink = mmu_shrink,
3952 .seeks = DEFAULT_SEEKS * 10,
3953};
3954
Ingo Molnar2ddfd202008-05-22 10:37:48 +02003955static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03003956{
Xiao Guangrong53c07b12011-05-15 23:26:20 +08003957 if (pte_list_desc_cache)
3958 kmem_cache_destroy(pte_list_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003959 if (mmu_page_header_cache)
3960 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003961}
3962
3963int kvm_mmu_module_init(void)
3964{
Xiao Guangrong53c07b12011-05-15 23:26:20 +08003965 pte_list_desc_cache = kmem_cache_create("pte_list_desc",
3966 sizeof(struct pte_list_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09003967 0, 0, NULL);
Xiao Guangrong53c07b12011-05-15 23:26:20 +08003968 if (!pte_list_desc_cache)
Avi Kivityb5a33a72007-04-15 16:31:09 +03003969 goto nomem;
3970
Avi Kivityd3d25b02007-05-30 12:34:53 +03003971 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
3972 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09003973 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03003974 if (!mmu_page_header_cache)
3975 goto nomem;
3976
Wei Yongjun45bf21a2010-08-23 16:13:15 +08003977 if (percpu_counter_init(&kvm_total_used_mmu_pages, 0))
3978 goto nomem;
3979
Izik Eidus3ee16c82008-03-30 15:17:21 +03003980 register_shrinker(&mmu_shrinker);
3981
Avi Kivityb5a33a72007-04-15 16:31:09 +03003982 return 0;
3983
3984nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03003985 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003986 return -ENOMEM;
3987}
3988
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003989/*
3990 * Caculate mmu pages needed for kvm.
3991 */
3992unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
3993{
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003994 unsigned int nr_mmu_pages;
3995 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02003996 struct kvm_memslots *slots;
Xiao Guangrongbe6ba0f2011-11-24 17:39:18 +08003997 struct kvm_memory_slot *memslot;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08003998
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08003999 slots = kvm_memslots(kvm);
4000
Xiao Guangrongbe6ba0f2011-11-24 17:39:18 +08004001 kvm_for_each_memslot(memslot, slots)
4002 nr_pages += memslot->npages;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08004003
4004 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
4005 nr_mmu_pages = max(nr_mmu_pages,
4006 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
4007
4008 return nr_mmu_pages;
4009}
4010
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03004011int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
4012{
4013 struct kvm_shadow_walk_iterator iterator;
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08004014 u64 spte;
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03004015 int nr_sptes = 0;
4016
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08004017 walk_shadow_page_lockless_begin(vcpu);
4018 for_each_shadow_entry_lockless(vcpu, addr, iterator, spte) {
4019 sptes[iterator.level-1] = spte;
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03004020 nr_sptes++;
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08004021 if (!is_shadow_present_pte(spte))
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03004022 break;
4023 }
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08004024 walk_shadow_page_lockless_end(vcpu);
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03004025
4026 return nr_sptes;
4027}
4028EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
4029
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08004030void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
4031{
4032 ASSERT(vcpu);
4033
4034 destroy_kvm_mmu(vcpu);
4035 free_mmu_pages(vcpu);
4036 mmu_free_memory_caches(vcpu);
Xiao Guangrongb034cf02010-12-23 16:08:35 +08004037}
4038
4039#ifdef CONFIG_KVM_MMU_AUDIT
4040#include "mmu_audit.c"
4041#else
4042static void mmu_audit_disable(void) { }
4043#endif
4044
4045void kvm_mmu_module_exit(void)
4046{
4047 mmu_destroy_caches();
4048 percpu_counter_destroy(&kvm_total_used_mmu_pages);
4049 unregister_shrinker(&mmu_shrinker);
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08004050 mmu_audit_disable();
4051}