blob: 1e7a49bfc94fb323cbb11782693cab89743f1133 [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"
Nadav Amit5f7dde72014-05-07 15:32:50 +030025#include "cpuid.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080026
Avi Kivityedf88412007-12-16 11:02:48 +020027#include <linux/kvm_host.h>
Avi Kivitye4956062007-06-28 14:15:57 -040028#include <linux/types.h>
29#include <linux/string.h>
30#include <linux/mm.h>
31#include <linux/highmem.h>
32#include <linux/module.h>
Izik Eidus448353c2007-11-26 14:08:14 +020033#include <linux/swap.h>
Marcelo Tosatti05da4552008-02-23 11:44:30 -030034#include <linux/hugetlb.h>
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050035#include <linux/compiler.h>
Marcelo Tosattibc6678a2009-12-23 14:35:21 -020036#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Huang Yingbf998152010-05-31 14:28:19 +080038#include <linux/uaccess.h>
Avi Kivitye4956062007-06-28 14:15:57 -040039
40#include <asm/page.h>
41#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020042#include <asm/io.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020043#include <asm/vmx.h>
Avi Kivitye4956062007-06-28 14:15:57 -040044
Joerg Roedel18552672008-02-07 13:47:41 +010045/*
46 * When setting this variable to true it enables Two-Dimensional-Paging
47 * where the hardware walks 2 page tables:
48 * 1. the guest-virtual to guest-physical
49 * 2. while doing 1. it walks guest-physical to host-physical
50 * If the hardware supports that we don't need to do shadow paging.
51 */
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050052bool tdp_enabled = false;
Joerg Roedel18552672008-02-07 13:47:41 +010053
Xiao Guangrong8b1fe172010-08-30 18:22:53 +080054enum {
55 AUDIT_PRE_PAGE_FAULT,
56 AUDIT_POST_PAGE_FAULT,
57 AUDIT_PRE_PTE_WRITE,
Xiao Guangrong69030742010-09-27 18:09:29 +080058 AUDIT_POST_PTE_WRITE,
59 AUDIT_PRE_SYNC,
60 AUDIT_POST_SYNC
Xiao Guangrong8b1fe172010-08-30 18:22:53 +080061};
62
Avi Kivity37a7d8b2007-01-05 16:36:56 -080063#undef MMU_DEBUG
64
Avi Kivity37a7d8b2007-01-05 16:36:56 -080065#ifdef MMU_DEBUG
Paolo Bonzinifa4a2c02013-10-02 16:56:16 +020066static bool dbg = 0;
67module_param(dbg, bool, 0644);
Avi Kivity37a7d8b2007-01-05 16:36:56 -080068
69#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
70#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
Paolo Bonzinifa4a2c02013-10-02 16:56:16 +020071#define MMU_WARN_ON(x) WARN_ON(x)
Avi Kivity37a7d8b2007-01-05 16:36:56 -080072#else
Avi Kivity37a7d8b2007-01-05 16:36:56 -080073#define pgprintk(x...) do { } while (0)
74#define rmap_printk(x...) do { } while (0)
Paolo Bonzinifa4a2c02013-10-02 16:56:16 +020075#define MMU_WARN_ON(x) do { } while (0)
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080076#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080077
Xiao Guangrong957ed9e2010-08-22 19:12:48 +080078#define PTE_PREFETCH_NUM 8
79
Xudong Hao00763e42012-06-07 18:26:07 +080080#define PT_FIRST_AVAIL_BITS_SHIFT 10
Avi Kivity6aa8b732006-12-10 02:21:36 -080081#define PT64_SECOND_AVAIL_BITS_SHIFT 52
82
Avi Kivity6aa8b732006-12-10 02:21:36 -080083#define PT64_LEVEL_BITS 9
84
85#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -040086 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080087
Avi Kivity6aa8b732006-12-10 02:21:36 -080088#define PT64_INDEX(address, level)\
89 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
90
91
92#define PT32_LEVEL_BITS 10
93
94#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -040095 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080096
Joerg Roedele04da982009-07-27 16:30:45 +020097#define PT32_LVL_OFFSET_MASK(level) \
98 (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
99 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800100
101#define PT32_INDEX(address, level)\
102 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
103
104
Avi Kivity27aba762007-03-09 13:04:31 +0200105#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800106#define PT64_DIR_BASE_ADDR_MASK \
107 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200108#define PT64_LVL_ADDR_MASK(level) \
109 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
110 * PT64_LEVEL_BITS))) - 1))
111#define PT64_LVL_OFFSET_MASK(level) \
112 (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
113 * PT64_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800114
115#define PT32_BASE_ADDR_MASK PAGE_MASK
116#define PT32_DIR_BASE_ADDR_MASK \
117 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200118#define PT32_LVL_ADDR_MASK(level) \
119 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
120 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800121
Gleb Natapov53166222013-08-05 11:07:14 +0300122#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | shadow_user_mask \
123 | shadow_x_mask | shadow_nx_mask)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800124
Avi Kivityfe135d22007-12-09 16:15:46 +0200125#define ACC_EXEC_MASK 1
126#define ACC_WRITE_MASK PT_WRITABLE_MASK
127#define ACC_USER_MASK PT_USER_MASK
128#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
129
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200130#include <trace/events/kvm.h>
131
Avi Kivity07420172009-07-06 12:21:32 +0300132#define CREATE_TRACE_POINTS
133#include "mmutrace.h"
134
Xiao Guangrong49fde342012-06-20 15:58:58 +0800135#define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
136#define SPTE_MMU_WRITEABLE (1ULL << (PT_FIRST_AVAIL_BITS_SHIFT + 1))
Izik Eidus14032832009-09-23 21:47:17 +0300137
Avi Kivity135f8c22008-08-21 17:49:56 +0300138#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
139
Takuya Yoshikawa220f7732012-03-21 23:49:39 +0900140/* make pte_list_desc fit well in cache line */
141#define PTE_LIST_EXT 3
142
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800143struct pte_list_desc {
144 u64 *sptes[PTE_LIST_EXT];
145 struct pte_list_desc *more;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800146};
147
Avi Kivity2d111232008-12-25 14:39:47 +0200148struct kvm_shadow_walk_iterator {
149 u64 addr;
150 hpa_t shadow_addr;
Avi Kivity2d111232008-12-25 14:39:47 +0200151 u64 *sptep;
Xiao Guangrongdd3bfd52011-07-12 03:32:54 +0800152 int level;
Avi Kivity2d111232008-12-25 14:39:47 +0200153 unsigned index;
154};
155
156#define for_each_shadow_entry(_vcpu, _addr, _walker) \
157 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
158 shadow_walk_okay(&(_walker)); \
159 shadow_walk_next(&(_walker)))
160
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800161#define for_each_shadow_entry_lockless(_vcpu, _addr, _walker, spte) \
162 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
163 shadow_walk_okay(&(_walker)) && \
164 ({ spte = mmu_spte_get_lockless(_walker.sptep); 1; }); \
165 __shadow_walk_next(&(_walker), spte))
166
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800167static struct kmem_cache *pte_list_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300168static struct kmem_cache *mmu_page_header_cache;
Dave Hansen45221ab2010-08-19 18:11:37 -0700169static struct percpu_counter kvm_total_used_mmu_pages;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300170
Sheng Yang7b523452008-04-25 21:13:50 +0800171static u64 __read_mostly shadow_nx_mask;
172static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
173static u64 __read_mostly shadow_user_mask;
174static u64 __read_mostly shadow_accessed_mask;
175static u64 __read_mostly shadow_dirty_mask;
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800176static u64 __read_mostly shadow_mmio_mask;
177
178static void mmu_spte_set(u64 *sptep, u64 spte);
Avi Kivitye6765052012-07-08 17:16:30 +0300179static void mmu_free_roots(struct kvm_vcpu *vcpu);
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800180
181void kvm_mmu_set_mmio_spte_mask(u64 mmio_mask)
182{
183 shadow_mmio_mask = mmio_mask;
184}
185EXPORT_SYMBOL_GPL(kvm_mmu_set_mmio_spte_mask);
186
Xiao Guangrongf2fd1252013-06-07 16:51:24 +0800187/*
David Matlackee3d1572014-08-18 15:46:06 -0700188 * the low bit of the generation number is always presumed to be zero.
189 * This disables mmio caching during memslot updates. The concept is
190 * similar to a seqcount but instead of retrying the access we just punt
191 * and ignore the cache.
192 *
193 * spte bits 3-11 are used as bits 1-9 of the generation number,
194 * the bits 52-61 are used as bits 10-19 of the generation number.
Xiao Guangrongf2fd1252013-06-07 16:51:24 +0800195 */
David Matlackee3d1572014-08-18 15:46:06 -0700196#define MMIO_SPTE_GEN_LOW_SHIFT 2
Xiao Guangrongf2fd1252013-06-07 16:51:24 +0800197#define MMIO_SPTE_GEN_HIGH_SHIFT 52
198
David Matlackee3d1572014-08-18 15:46:06 -0700199#define MMIO_GEN_SHIFT 20
200#define MMIO_GEN_LOW_SHIFT 10
201#define MMIO_GEN_LOW_MASK ((1 << MMIO_GEN_LOW_SHIFT) - 2)
Xiao Guangrongf8f55942013-06-07 16:51:26 +0800202#define MMIO_GEN_MASK ((1 << MMIO_GEN_SHIFT) - 1)
Xiao Guangrongf2fd1252013-06-07 16:51:24 +0800203
204static u64 generation_mmio_spte_mask(unsigned int gen)
205{
206 u64 mask;
207
Tiejun Chen842bb262014-11-18 17:14:38 +0800208 WARN_ON(gen & ~MMIO_GEN_MASK);
Xiao Guangrongf2fd1252013-06-07 16:51:24 +0800209
210 mask = (gen & MMIO_GEN_LOW_MASK) << MMIO_SPTE_GEN_LOW_SHIFT;
211 mask |= ((u64)gen >> MMIO_GEN_LOW_SHIFT) << MMIO_SPTE_GEN_HIGH_SHIFT;
212 return mask;
213}
214
215static unsigned int get_mmio_spte_generation(u64 spte)
216{
217 unsigned int gen;
218
219 spte &= ~shadow_mmio_mask;
220
221 gen = (spte >> MMIO_SPTE_GEN_LOW_SHIFT) & MMIO_GEN_LOW_MASK;
222 gen |= (spte >> MMIO_SPTE_GEN_HIGH_SHIFT) << MMIO_GEN_LOW_SHIFT;
223 return gen;
224}
225
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200226static unsigned int kvm_current_mmio_generation(struct kvm_vcpu *vcpu)
Xiao Guangrongf8f55942013-06-07 16:51:26 +0800227{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200228 return kvm_vcpu_memslots(vcpu)->generation & MMIO_GEN_MASK;
Xiao Guangrongf8f55942013-06-07 16:51:26 +0800229}
230
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200231static void mark_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, u64 gfn,
Xiao Guangrongf2fd1252013-06-07 16:51:24 +0800232 unsigned access)
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800233{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200234 unsigned int gen = kvm_current_mmio_generation(vcpu);
Xiao Guangrongf8f55942013-06-07 16:51:26 +0800235 u64 mask = generation_mmio_spte_mask(gen);
Takuya Yoshikawa95b04302013-03-12 17:44:40 +0900236
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800237 access &= ACC_WRITE_MASK | ACC_USER_MASK;
Xiao Guangrongf2fd1252013-06-07 16:51:24 +0800238 mask |= shadow_mmio_mask | access | gfn << PAGE_SHIFT;
Xiao Guangrongf2fd1252013-06-07 16:51:24 +0800239
Xiao Guangrongf8f55942013-06-07 16:51:26 +0800240 trace_mark_mmio_spte(sptep, gfn, access, gen);
Xiao Guangrongf2fd1252013-06-07 16:51:24 +0800241 mmu_spte_set(sptep, mask);
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800242}
243
244static bool is_mmio_spte(u64 spte)
245{
246 return (spte & shadow_mmio_mask) == shadow_mmio_mask;
247}
248
249static gfn_t get_mmio_spte_gfn(u64 spte)
250{
Tiejun Chen842bb262014-11-18 17:14:38 +0800251 u64 mask = generation_mmio_spte_mask(MMIO_GEN_MASK) | shadow_mmio_mask;
Xiao Guangrongf2fd1252013-06-07 16:51:24 +0800252 return (spte & ~mask) >> PAGE_SHIFT;
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800253}
254
255static unsigned get_mmio_spte_access(u64 spte)
256{
Tiejun Chen842bb262014-11-18 17:14:38 +0800257 u64 mask = generation_mmio_spte_mask(MMIO_GEN_MASK) | shadow_mmio_mask;
Xiao Guangrongf2fd1252013-06-07 16:51:24 +0800258 return (spte & ~mask) & ~PAGE_MASK;
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800259}
260
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200261static bool set_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, gfn_t gfn,
Dan Williamsba049e92016-01-15 16:56:11 -0800262 kvm_pfn_t pfn, unsigned access)
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800263{
264 if (unlikely(is_noslot_pfn(pfn))) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200265 mark_mmio_spte(vcpu, sptep, gfn, access);
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800266 return true;
267 }
268
269 return false;
270}
Avi Kivityc7addb92007-09-16 18:58:32 +0200271
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200272static bool check_mmio_spte(struct kvm_vcpu *vcpu, u64 spte)
Xiao Guangrongf8f55942013-06-07 16:51:26 +0800273{
Xiao Guangrong089504c2013-06-07 16:51:27 +0800274 unsigned int kvm_gen, spte_gen;
275
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200276 kvm_gen = kvm_current_mmio_generation(vcpu);
Xiao Guangrong089504c2013-06-07 16:51:27 +0800277 spte_gen = get_mmio_spte_generation(spte);
278
279 trace_check_mmio_spte(spte, kvm_gen, spte_gen);
280 return likely(kvm_gen == spte_gen);
Xiao Guangrongf8f55942013-06-07 16:51:26 +0800281}
282
Sheng Yang7b523452008-04-25 21:13:50 +0800283void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang4b12f0d2009-04-27 20:35:42 +0800284 u64 dirty_mask, u64 nx_mask, u64 x_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800285{
286 shadow_user_mask = user_mask;
287 shadow_accessed_mask = accessed_mask;
288 shadow_dirty_mask = dirty_mask;
289 shadow_nx_mask = nx_mask;
290 shadow_x_mask = x_mask;
291}
292EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
293
Avi Kivity6aa8b732006-12-10 02:21:36 -0800294static int is_cpuid_PSE36(void)
295{
296 return 1;
297}
298
Avi Kivity73b10872007-01-26 00:56:41 -0800299static int is_nx(struct kvm_vcpu *vcpu)
300{
Avi Kivityf6801df2010-01-21 15:31:50 +0200301 return vcpu->arch.efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800302}
303
Avi Kivityc7addb92007-09-16 18:58:32 +0200304static int is_shadow_present_pte(u64 pte)
305{
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800306 return pte & PT_PRESENT_MASK && !is_mmio_spte(pte);
Avi Kivityc7addb92007-09-16 18:58:32 +0200307}
308
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300309static int is_large_pte(u64 pte)
310{
311 return pte & PT_PAGE_SIZE_MASK;
312}
313
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300314static int is_last_spte(u64 pte, int level)
315{
316 if (level == PT_PAGE_TABLE_LEVEL)
317 return 1;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200318 if (is_large_pte(pte))
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300319 return 1;
320 return 0;
321}
322
Dan Williamsba049e92016-01-15 16:56:11 -0800323static kvm_pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200324{
Anthony Liguori35149e22008-04-02 14:46:56 -0500325 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200326}
327
Avi Kivityda9285212007-11-21 13:54:47 +0200328static gfn_t pse36_gfn_delta(u32 gpte)
329{
330 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
331
332 return (gpte & PT32_DIR_PSE36_MASK) << shift;
333}
334
Xiao Guangrong603e0652011-07-12 03:31:28 +0800335#ifdef CONFIG_X86_64
Avi Kivityd555c332009-06-10 14:24:23 +0300336static void __set_spte(u64 *sptep, u64 spte)
Avi Kivitye663ee62007-05-31 15:46:04 +0300337{
Xiao Guangrong603e0652011-07-12 03:31:28 +0800338 *sptep = spte;
Avi Kivitye663ee62007-05-31 15:46:04 +0300339}
340
Xiao Guangrong603e0652011-07-12 03:31:28 +0800341static void __update_clear_spte_fast(u64 *sptep, u64 spte)
Avi Kivitya9221dd2010-06-06 14:48:06 +0300342{
Xiao Guangrong603e0652011-07-12 03:31:28 +0800343 *sptep = spte;
Avi Kivitya9221dd2010-06-06 14:48:06 +0300344}
345
Xiao Guangrong603e0652011-07-12 03:31:28 +0800346static u64 __update_clear_spte_slow(u64 *sptep, u64 spte)
347{
348 return xchg(sptep, spte);
349}
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800350
351static u64 __get_spte_lockless(u64 *sptep)
352{
353 return ACCESS_ONCE(*sptep);
354}
Xiao Guangrong603e0652011-07-12 03:31:28 +0800355#else
356union split_spte {
357 struct {
358 u32 spte_low;
359 u32 spte_high;
360 };
361 u64 spte;
362};
363
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800364static void count_spte_clear(u64 *sptep, u64 spte)
365{
366 struct kvm_mmu_page *sp = page_header(__pa(sptep));
367
368 if (is_shadow_present_pte(spte))
369 return;
370
371 /* Ensure the spte is completely set before we increase the count */
372 smp_wmb();
373 sp->clear_spte_count++;
374}
375
Xiao Guangrong603e0652011-07-12 03:31:28 +0800376static void __set_spte(u64 *sptep, u64 spte)
377{
378 union split_spte *ssptep, sspte;
379
380 ssptep = (union split_spte *)sptep;
381 sspte = (union split_spte)spte;
382
383 ssptep->spte_high = sspte.spte_high;
384
385 /*
386 * If we map the spte from nonpresent to present, We should store
387 * the high bits firstly, then set present bit, so cpu can not
388 * fetch this spte while we are setting the spte.
389 */
390 smp_wmb();
391
392 ssptep->spte_low = sspte.spte_low;
393}
394
395static void __update_clear_spte_fast(u64 *sptep, u64 spte)
396{
397 union split_spte *ssptep, sspte;
398
399 ssptep = (union split_spte *)sptep;
400 sspte = (union split_spte)spte;
401
402 ssptep->spte_low = sspte.spte_low;
403
404 /*
405 * If we map the spte from present to nonpresent, we should clear
406 * present bit firstly to avoid vcpu fetch the old high bits.
407 */
408 smp_wmb();
409
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
414static u64 __update_clear_spte_slow(u64 *sptep, u64 spte)
415{
416 union split_spte *ssptep, sspte, orig;
417
418 ssptep = (union split_spte *)sptep;
419 sspte = (union split_spte)spte;
420
421 /* xchg acts as a barrier before the setting of the high bits */
422 orig.spte_low = xchg(&ssptep->spte_low, sspte.spte_low);
Zhao Jin41bc3182011-09-19 12:19:51 +0800423 orig.spte_high = ssptep->spte_high;
424 ssptep->spte_high = sspte.spte_high;
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800425 count_spte_clear(sptep, spte);
Xiao Guangrong603e0652011-07-12 03:31:28 +0800426
427 return orig.spte;
428}
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800429
430/*
431 * The idea using the light way get the spte on x86_32 guest is from
432 * gup_get_pte(arch/x86/mm/gup.c).
Xiao Guangrongaccaefe2013-06-19 17:09:20 +0800433 *
434 * An spte tlb flush may be pending, because kvm_set_pte_rmapp
435 * coalesces them and we are running out of the MMU lock. Therefore
436 * we need to protect against in-progress updates of the spte.
437 *
438 * Reading the spte while an update is in progress may get the old value
439 * for the high part of the spte. The race is fine for a present->non-present
440 * change (because the high part of the spte is ignored for non-present spte),
441 * but for a present->present change we must reread the spte.
442 *
443 * All such changes are done in two steps (present->non-present and
444 * non-present->present), hence it is enough to count the number of
445 * present->non-present updates: if it changed while reading the spte,
446 * we might have hit the race. This is done using clear_spte_count.
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800447 */
448static u64 __get_spte_lockless(u64 *sptep)
449{
450 struct kvm_mmu_page *sp = page_header(__pa(sptep));
451 union split_spte spte, *orig = (union split_spte *)sptep;
452 int count;
453
454retry:
455 count = sp->clear_spte_count;
456 smp_rmb();
457
458 spte.spte_low = orig->spte_low;
459 smp_rmb();
460
461 spte.spte_high = orig->spte_high;
462 smp_rmb();
463
464 if (unlikely(spte.spte_low != orig->spte_low ||
465 count != sp->clear_spte_count))
466 goto retry;
467
468 return spte.spte;
469}
Xiao Guangrong603e0652011-07-12 03:31:28 +0800470#endif
471
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +0800472static bool spte_is_locklessly_modifiable(u64 spte)
473{
Gleb Natapovfeb3eb72013-01-30 16:45:00 +0200474 return (spte & (SPTE_HOST_WRITEABLE | SPTE_MMU_WRITEABLE)) ==
475 (SPTE_HOST_WRITEABLE | SPTE_MMU_WRITEABLE);
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +0800476}
477
Xiao Guangrong8672b722010-08-02 16:14:04 +0800478static bool spte_has_volatile_bits(u64 spte)
479{
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +0800480 /*
481 * Always atomicly update spte if it can be updated
482 * out of mmu-lock, it can ensure dirty bit is not lost,
483 * also, it can help us to get a stable is_writable_pte()
484 * to ensure tlb flush is not missed.
485 */
486 if (spte_is_locklessly_modifiable(spte))
487 return true;
488
Xiao Guangrong8672b722010-08-02 16:14:04 +0800489 if (!shadow_accessed_mask)
490 return false;
491
492 if (!is_shadow_present_pte(spte))
493 return false;
494
Xiao Guangrong41327792010-08-02 16:15:08 +0800495 if ((spte & shadow_accessed_mask) &&
496 (!is_writable_pte(spte) || (spte & shadow_dirty_mask)))
Xiao Guangrong8672b722010-08-02 16:14:04 +0800497 return false;
498
499 return true;
500}
501
Xiao Guangrong41327792010-08-02 16:15:08 +0800502static bool spte_is_bit_cleared(u64 old_spte, u64 new_spte, u64 bit_mask)
503{
504 return (old_spte & bit_mask) && !(new_spte & bit_mask);
505}
506
Kai Huang7e71a592015-01-09 16:44:30 +0800507static bool spte_is_bit_changed(u64 old_spte, u64 new_spte, u64 bit_mask)
508{
509 return (old_spte & bit_mask) != (new_spte & bit_mask);
510}
511
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800512/* Rules for using mmu_spte_set:
513 * Set the sptep from nonpresent to present.
514 * Note: the sptep being assigned *must* be either not present
515 * or in a state where the hardware will not attempt to update
516 * the spte.
517 */
518static void mmu_spte_set(u64 *sptep, u64 new_spte)
519{
520 WARN_ON(is_shadow_present_pte(*sptep));
521 __set_spte(sptep, new_spte);
522}
523
524/* Rules for using mmu_spte_update:
525 * Update the state bits, it means the mapped pfn is not changged.
Xiao Guangrong6e7d0352012-06-20 15:58:33 +0800526 *
527 * Whenever we overwrite a writable spte with a read-only one we
528 * should flush remote TLBs. Otherwise rmap_write_protect
529 * will find a read-only spte, even though the writable spte
530 * might be cached on a CPU's TLB, the return value indicates this
531 * case.
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800532 */
Xiao Guangrong6e7d0352012-06-20 15:58:33 +0800533static bool mmu_spte_update(u64 *sptep, u64 new_spte)
Avi Kivityb79b93f2010-06-06 15:46:44 +0300534{
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +0800535 u64 old_spte = *sptep;
Xiao Guangrong6e7d0352012-06-20 15:58:33 +0800536 bool ret = false;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300537
Takuya Yoshikawaafd28fe2015-11-20 17:44:55 +0900538 WARN_ON(!is_shadow_present_pte(new_spte));
Xiao Guangrong41327792010-08-02 16:15:08 +0800539
Xiao Guangrong6e7d0352012-06-20 15:58:33 +0800540 if (!is_shadow_present_pte(old_spte)) {
541 mmu_spte_set(sptep, new_spte);
542 return ret;
543 }
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800544
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +0800545 if (!spte_has_volatile_bits(old_spte))
Xiao Guangrong603e0652011-07-12 03:31:28 +0800546 __update_clear_spte_fast(sptep, new_spte);
Xiao Guangrong41327792010-08-02 16:15:08 +0800547 else
Xiao Guangrong603e0652011-07-12 03:31:28 +0800548 old_spte = __update_clear_spte_slow(sptep, new_spte);
Xiao Guangrong41327792010-08-02 16:15:08 +0800549
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +0800550 /*
551 * For the spte updated out of mmu-lock is safe, since
552 * we always atomicly update it, see the comments in
553 * spte_has_volatile_bits().
554 */
Xiao Guangrong7f31c952014-04-17 17:06:15 +0800555 if (spte_is_locklessly_modifiable(old_spte) &&
556 !is_writable_pte(new_spte))
Xiao Guangrong6e7d0352012-06-20 15:58:33 +0800557 ret = true;
558
Xiao Guangrong41327792010-08-02 16:15:08 +0800559 if (!shadow_accessed_mask)
Xiao Guangrong6e7d0352012-06-20 15:58:33 +0800560 return ret;
Xiao Guangrong41327792010-08-02 16:15:08 +0800561
Kai Huang7e71a592015-01-09 16:44:30 +0800562 /*
563 * Flush TLB when accessed/dirty bits are changed in the page tables,
564 * to guarantee consistency between TLB and page tables.
565 */
566 if (spte_is_bit_changed(old_spte, new_spte,
567 shadow_accessed_mask | shadow_dirty_mask))
568 ret = true;
569
Xiao Guangrong41327792010-08-02 16:15:08 +0800570 if (spte_is_bit_cleared(old_spte, new_spte, shadow_accessed_mask))
571 kvm_set_pfn_accessed(spte_to_pfn(old_spte));
572 if (spte_is_bit_cleared(old_spte, new_spte, shadow_dirty_mask))
573 kvm_set_pfn_dirty(spte_to_pfn(old_spte));
Xiao Guangrong6e7d0352012-06-20 15:58:33 +0800574
575 return ret;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300576}
577
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800578/*
579 * Rules for using mmu_spte_clear_track_bits:
580 * It sets the sptep from present to nonpresent, and track the
581 * state bits, it is used to clear the last level sptep.
582 */
583static int mmu_spte_clear_track_bits(u64 *sptep)
584{
Dan Williamsba049e92016-01-15 16:56:11 -0800585 kvm_pfn_t pfn;
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800586 u64 old_spte = *sptep;
587
588 if (!spte_has_volatile_bits(old_spte))
Xiao Guangrong603e0652011-07-12 03:31:28 +0800589 __update_clear_spte_fast(sptep, 0ull);
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800590 else
Xiao Guangrong603e0652011-07-12 03:31:28 +0800591 old_spte = __update_clear_spte_slow(sptep, 0ull);
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800592
Takuya Yoshikawaafd28fe2015-11-20 17:44:55 +0900593 if (!is_shadow_present_pte(old_spte))
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800594 return 0;
595
596 pfn = spte_to_pfn(old_spte);
Xiao Guangrong86fde742012-07-17 21:52:52 +0800597
598 /*
599 * KVM does not hold the refcount of the page used by
600 * kvm mmu, before reclaiming the page, we should
601 * unmap it from mmu first.
602 */
Ard Biesheuvelbf4bea82014-11-10 08:33:56 +0000603 WARN_ON(!kvm_is_reserved_pfn(pfn) && !page_count(pfn_to_page(pfn)));
Xiao Guangrong86fde742012-07-17 21:52:52 +0800604
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800605 if (!shadow_accessed_mask || old_spte & shadow_accessed_mask)
606 kvm_set_pfn_accessed(pfn);
607 if (!shadow_dirty_mask || (old_spte & shadow_dirty_mask))
608 kvm_set_pfn_dirty(pfn);
609 return 1;
610}
611
612/*
613 * Rules for using mmu_spte_clear_no_track:
614 * Directly clear spte without caring the state bits of sptep,
615 * it is used to set the upper level spte.
616 */
617static void mmu_spte_clear_no_track(u64 *sptep)
618{
Xiao Guangrong603e0652011-07-12 03:31:28 +0800619 __update_clear_spte_fast(sptep, 0ull);
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800620}
621
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800622static u64 mmu_spte_get_lockless(u64 *sptep)
623{
624 return __get_spte_lockless(sptep);
625}
626
627static void walk_shadow_page_lockless_begin(struct kvm_vcpu *vcpu)
628{
Avi Kivityc1427862012-05-14 15:44:06 +0300629 /*
630 * Prevent page table teardown by making any free-er wait during
631 * kvm_flush_remote_tlbs() IPI to all active vcpus.
632 */
633 local_irq_disable();
634 vcpu->mode = READING_SHADOW_PAGE_TABLES;
635 /*
636 * Make sure a following spte read is not reordered ahead of the write
637 * to vcpu->mode.
638 */
639 smp_mb();
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800640}
641
642static void walk_shadow_page_lockless_end(struct kvm_vcpu *vcpu)
643{
Avi Kivityc1427862012-05-14 15:44:06 +0300644 /*
645 * Make sure the write to vcpu->mode is not reordered in front of
646 * reads to sptes. If it does, kvm_commit_zap_page() can see us
647 * OUTSIDE_GUEST_MODE and proceed to free the shadow page table.
648 */
649 smp_mb();
650 vcpu->mode = OUTSIDE_GUEST_MODE;
651 local_irq_enable();
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800652}
653
Avi Kivitye2dec932007-01-05 16:36:54 -0800654static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300655 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800656{
657 void *obj;
658
659 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800660 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800661 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300662 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800663 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800664 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800665 cache->objects[cache->nobjs++] = obj;
666 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800667 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800668}
669
Xiao Guangrongf759e2b2011-09-22 16:53:17 +0800670static int mmu_memory_cache_free_objects(struct kvm_mmu_memory_cache *cache)
671{
672 return cache->nobjs;
673}
674
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800675static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc,
676 struct kmem_cache *cache)
Avi Kivity714b93d2007-01-05 16:36:53 -0800677{
678 while (mc->nobjs)
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800679 kmem_cache_free(cache, mc->objects[--mc->nobjs]);
Avi Kivity714b93d2007-01-05 16:36:53 -0800680}
681
Avi Kivityc1158e62007-07-20 08:18:27 +0300682static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300683 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300684{
Xiao Guangrong842f22e2011-03-04 19:01:10 +0800685 void *page;
Avi Kivityc1158e62007-07-20 08:18:27 +0300686
687 if (cache->nobjs >= min)
688 return 0;
689 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Xiao Guangrong842f22e2011-03-04 19:01:10 +0800690 page = (void *)__get_free_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300691 if (!page)
692 return -ENOMEM;
Xiao Guangrong842f22e2011-03-04 19:01:10 +0800693 cache->objects[cache->nobjs++] = page;
Avi Kivityc1158e62007-07-20 08:18:27 +0300694 }
695 return 0;
696}
697
698static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
699{
700 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300701 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300702}
703
Avi Kivity8c438502007-04-16 11:53:17 +0300704static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
705{
706 int r;
707
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800708 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
Xiao Guangrong67052b32011-05-15 23:27:08 +0800709 pte_list_desc_cache, 8 + PTE_PREFETCH_NUM);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300710 if (r)
711 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800712 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300713 if (r)
714 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800715 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300716 mmu_page_header_cache, 4);
717out:
Avi Kivity8c438502007-04-16 11:53:17 +0300718 return r;
719}
720
Avi Kivity714b93d2007-01-05 16:36:53 -0800721static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
722{
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800723 mmu_free_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
724 pte_list_desc_cache);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800725 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800726 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache,
727 mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800728}
729
Takuya Yoshikawa80feb892012-05-29 23:54:26 +0900730static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800731{
732 void *p;
733
734 BUG_ON(!mc->nobjs);
735 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800736 return p;
737}
738
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800739static struct pte_list_desc *mmu_alloc_pte_list_desc(struct kvm_vcpu *vcpu)
Avi Kivity714b93d2007-01-05 16:36:53 -0800740{
Takuya Yoshikawa80feb892012-05-29 23:54:26 +0900741 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_list_desc_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800742}
743
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800744static void mmu_free_pte_list_desc(struct pte_list_desc *pte_list_desc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800745{
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800746 kmem_cache_free(pte_list_desc_cache, pte_list_desc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800747}
748
Lai Jiangshan2032a932010-05-26 16:49:59 +0800749static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
750{
751 if (!sp->role.direct)
752 return sp->gfns[index];
753
754 return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS));
755}
756
757static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn)
758{
759 if (sp->role.direct)
760 BUG_ON(gfn != kvm_mmu_page_get_gfn(sp, index));
761 else
762 sp->gfns[index] = gfn;
763}
764
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800765/*
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900766 * Return the pointer to the large page information for a given gfn,
767 * handling slots that are not large page aligned.
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300768 */
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900769static struct kvm_lpage_info *lpage_info_slot(gfn_t gfn,
770 struct kvm_memory_slot *slot,
771 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300772{
773 unsigned long idx;
774
Takuya Yoshikawafb03cb62012-02-08 12:59:10 +0900775 idx = gfn_to_index(gfn, slot->base_gfn, level);
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +0900776 return &slot->arch.lpage_info[level - 2][idx];
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300777}
778
Paolo Bonzini3ed1a472015-05-19 16:29:22 +0200779static void account_shadowed(struct kvm *kvm, struct kvm_mmu_page *sp)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300780{
Paolo Bonzini699023e2015-05-18 15:03:39 +0200781 struct kvm_memslots *slots;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200782 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900783 struct kvm_lpage_info *linfo;
Paolo Bonzini3ed1a472015-05-19 16:29:22 +0200784 gfn_t gfn;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200785 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300786
Paolo Bonzini3ed1a472015-05-19 16:29:22 +0200787 gfn = sp->gfn;
Paolo Bonzini699023e2015-05-18 15:03:39 +0200788 slots = kvm_memslots_for_spte_role(kvm, sp->role);
789 slot = __gfn_to_memslot(slots, gfn);
Xiao Guangrong8a3d08f2015-05-13 14:42:21 +0800790 for (i = PT_DIRECTORY_LEVEL; i <= PT_MAX_HUGEPAGE_LEVEL; ++i) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900791 linfo = lpage_info_slot(gfn, slot, i);
792 linfo->write_count += 1;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200793 }
Xiao Guangrong332b2072011-05-15 23:20:27 +0800794 kvm->arch.indirect_shadow_pages++;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300795}
796
Paolo Bonzini3ed1a472015-05-19 16:29:22 +0200797static void unaccount_shadowed(struct kvm *kvm, struct kvm_mmu_page *sp)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300798{
Paolo Bonzini699023e2015-05-18 15:03:39 +0200799 struct kvm_memslots *slots;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200800 struct kvm_memory_slot *slot;
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900801 struct kvm_lpage_info *linfo;
Paolo Bonzini3ed1a472015-05-19 16:29:22 +0200802 gfn_t gfn;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200803 int i;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300804
Paolo Bonzini3ed1a472015-05-19 16:29:22 +0200805 gfn = sp->gfn;
Paolo Bonzini699023e2015-05-18 15:03:39 +0200806 slots = kvm_memslots_for_spte_role(kvm, sp->role);
807 slot = __gfn_to_memslot(slots, gfn);
Xiao Guangrong8a3d08f2015-05-13 14:42:21 +0800808 for (i = PT_DIRECTORY_LEVEL; i <= PT_MAX_HUGEPAGE_LEVEL; ++i) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900809 linfo = lpage_info_slot(gfn, slot, i);
810 linfo->write_count -= 1;
811 WARN_ON(linfo->write_count < 0);
Joerg Roedeld25797b2009-07-27 16:30:43 +0200812 }
Xiao Guangrong332b2072011-05-15 23:20:27 +0800813 kvm->arch.indirect_shadow_pages--;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300814}
815
Takuya Yoshikawa5225fdf2015-10-16 17:08:03 +0900816static int __has_wrprotected_page(gfn_t gfn, int level,
817 struct kvm_memory_slot *slot)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300818{
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900819 struct kvm_lpage_info *linfo;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300820
821 if (slot) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900822 linfo = lpage_info_slot(gfn, slot, level);
823 return linfo->write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300824 }
825
826 return 1;
827}
828
Takuya Yoshikawa5225fdf2015-10-16 17:08:03 +0900829static int has_wrprotected_page(struct kvm_vcpu *vcpu, gfn_t gfn, int level)
830{
831 struct kvm_memory_slot *slot;
832
833 slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
834 return __has_wrprotected_page(gfn, level, slot);
835}
836
Joerg Roedeld25797b2009-07-27 16:30:43 +0200837static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300838{
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100839 unsigned long page_size;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200840 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300841
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +0100842 page_size = kvm_host_page_size(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300843
Xiao Guangrong8a3d08f2015-05-13 14:42:21 +0800844 for (i = PT_PAGE_TABLE_LEVEL; i <= PT_MAX_HUGEPAGE_LEVEL; ++i) {
Joerg Roedeld25797b2009-07-27 16:30:43 +0200845 if (page_size >= KVM_HPAGE_SIZE(i))
846 ret = i;
847 else
848 break;
849 }
850
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300851 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300852}
853
Takuya Yoshikawad8aacf52015-10-16 17:07:01 +0900854static inline bool memslot_valid_for_gpte(struct kvm_memory_slot *slot,
855 bool no_dirty_log)
856{
857 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
858 return false;
859 if (no_dirty_log && slot->dirty_bitmap)
860 return false;
861
862 return true;
863}
864
Xiao Guangrong5d163b12011-03-09 15:43:00 +0800865static struct kvm_memory_slot *
866gfn_to_memslot_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t gfn,
867 bool no_dirty_log)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300868{
869 struct kvm_memory_slot *slot;
Xiao Guangrong5d163b12011-03-09 15:43:00 +0800870
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200871 slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
Takuya Yoshikawad8aacf52015-10-16 17:07:01 +0900872 if (!memslot_valid_for_gpte(slot, no_dirty_log))
Xiao Guangrong5d163b12011-03-09 15:43:00 +0800873 slot = NULL;
874
875 return slot;
876}
877
Takuya Yoshikawafd136902015-10-16 17:06:02 +0900878static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn,
879 bool *force_pt_level)
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -0800880{
881 int host_level, level, max_level;
Takuya Yoshikawad8aacf52015-10-16 17:07:01 +0900882 struct kvm_memory_slot *slot;
883
Takuya Yoshikawa8c85ac12015-10-19 15:13:29 +0900884 if (unlikely(*force_pt_level))
885 return PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300886
Takuya Yoshikawa8c85ac12015-10-19 15:13:29 +0900887 slot = kvm_vcpu_gfn_to_memslot(vcpu, large_gfn);
888 *force_pt_level = !memslot_valid_for_gpte(slot, true);
Takuya Yoshikawafd136902015-10-16 17:06:02 +0900889 if (unlikely(*force_pt_level))
890 return PT_PAGE_TABLE_LEVEL;
891
Joerg Roedeld25797b2009-07-27 16:30:43 +0200892 host_level = host_mapping_level(vcpu->kvm, large_gfn);
893
894 if (host_level == PT_PAGE_TABLE_LEVEL)
895 return host_level;
896
Xiao Guangrong55dd98c2013-02-05 15:26:54 +0800897 max_level = min(kvm_x86_ops->get_lpage_level(), host_level);
Sheng Yang878403b2010-01-05 19:02:29 +0800898
899 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
Takuya Yoshikawa5225fdf2015-10-16 17:08:03 +0900900 if (__has_wrprotected_page(large_gfn, level, slot))
Joerg Roedeld25797b2009-07-27 16:30:43 +0200901 break;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200902
903 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300904}
905
906/*
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900907 * About rmap_head encoding:
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800908 *
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900909 * If the bit zero of rmap_head->val is clear, then it points to the only spte
910 * in this rmap chain. Otherwise, (rmap_head->val & ~1) points to a struct
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800911 * pte_list_desc containing more mappings.
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900912 */
913
914/*
915 * Returns the number of pointers in the rmap chain, not counting the new one.
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800916 */
917static int pte_list_add(struct kvm_vcpu *vcpu, u64 *spte,
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900918 struct kvm_rmap_head *rmap_head)
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800919{
920 struct pte_list_desc *desc;
921 int i, count = 0;
922
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900923 if (!rmap_head->val) {
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800924 rmap_printk("pte_list_add: %p %llx 0->1\n", spte, *spte);
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900925 rmap_head->val = (unsigned long)spte;
926 } else if (!(rmap_head->val & 1)) {
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800927 rmap_printk("pte_list_add: %p %llx 1->many\n", spte, *spte);
928 desc = mmu_alloc_pte_list_desc(vcpu);
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900929 desc->sptes[0] = (u64 *)rmap_head->val;
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800930 desc->sptes[1] = spte;
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900931 rmap_head->val = (unsigned long)desc | 1;
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800932 ++count;
933 } else {
934 rmap_printk("pte_list_add: %p %llx many->many\n", spte, *spte);
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900935 desc = (struct pte_list_desc *)(rmap_head->val & ~1ul);
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800936 while (desc->sptes[PTE_LIST_EXT-1] && desc->more) {
937 desc = desc->more;
938 count += PTE_LIST_EXT;
939 }
940 if (desc->sptes[PTE_LIST_EXT-1]) {
941 desc->more = mmu_alloc_pte_list_desc(vcpu);
942 desc = desc->more;
943 }
944 for (i = 0; desc->sptes[i]; ++i)
945 ++count;
946 desc->sptes[i] = spte;
947 }
948 return count;
949}
950
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800951static void
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900952pte_list_desc_remove_entry(struct kvm_rmap_head *rmap_head,
953 struct pte_list_desc *desc, int i,
954 struct pte_list_desc *prev_desc)
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800955{
956 int j;
957
958 for (j = PTE_LIST_EXT - 1; !desc->sptes[j] && j > i; --j)
959 ;
960 desc->sptes[i] = desc->sptes[j];
961 desc->sptes[j] = NULL;
962 if (j != 0)
963 return;
964 if (!prev_desc && !desc->more)
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900965 rmap_head->val = (unsigned long)desc->sptes[0];
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800966 else
967 if (prev_desc)
968 prev_desc->more = desc->more;
969 else
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900970 rmap_head->val = (unsigned long)desc->more | 1;
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800971 mmu_free_pte_list_desc(desc);
972}
973
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900974static void pte_list_remove(u64 *spte, struct kvm_rmap_head *rmap_head)
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800975{
976 struct pte_list_desc *desc;
977 struct pte_list_desc *prev_desc;
978 int i;
979
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900980 if (!rmap_head->val) {
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800981 printk(KERN_ERR "pte_list_remove: %p 0->BUG\n", spte);
982 BUG();
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900983 } else if (!(rmap_head->val & 1)) {
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800984 rmap_printk("pte_list_remove: %p 1->0\n", spte);
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900985 if ((u64 *)rmap_head->val != spte) {
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800986 printk(KERN_ERR "pte_list_remove: %p 1->BUG\n", spte);
987 BUG();
988 }
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900989 rmap_head->val = 0;
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800990 } else {
991 rmap_printk("pte_list_remove: %p many->many\n", spte);
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900992 desc = (struct pte_list_desc *)(rmap_head->val & ~1ul);
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800993 prev_desc = NULL;
994 while (desc) {
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900995 for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i) {
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800996 if (desc->sptes[i] == spte) {
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900997 pte_list_desc_remove_entry(rmap_head,
998 desc, i, prev_desc);
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800999 return;
1000 }
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001001 }
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001002 prev_desc = desc;
1003 desc = desc->more;
1004 }
1005 pr_err("pte_list_remove: %p many->many\n", spte);
1006 BUG();
1007 }
1008}
1009
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001010static struct kvm_rmap_head *__gfn_to_rmap(gfn_t gfn, int level,
1011 struct kvm_memory_slot *slot)
Takuya Yoshikawa9b9b1492011-11-14 18:22:28 +09001012{
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09001013 unsigned long idx;
Takuya Yoshikawa9b9b1492011-11-14 18:22:28 +09001014
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09001015 idx = gfn_to_index(gfn, slot->base_gfn, level);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09001016 return &slot->arch.rmap[level - PT_PAGE_TABLE_LEVEL][idx];
Takuya Yoshikawa9b9b1492011-11-14 18:22:28 +09001017}
1018
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001019static struct kvm_rmap_head *gfn_to_rmap(struct kvm *kvm, gfn_t gfn,
1020 struct kvm_mmu_page *sp)
Izik Eidus290fc382007-09-27 14:11:22 +02001021{
Paolo Bonzini699023e2015-05-18 15:03:39 +02001022 struct kvm_memslots *slots;
Izik Eidus290fc382007-09-27 14:11:22 +02001023 struct kvm_memory_slot *slot;
1024
Paolo Bonzini699023e2015-05-18 15:03:39 +02001025 slots = kvm_memslots_for_spte_role(kvm, sp->role);
1026 slot = __gfn_to_memslot(slots, gfn);
Paolo Bonzinie4cd1da2015-05-18 15:11:46 +02001027 return __gfn_to_rmap(gfn, sp->role.level, slot);
Izik Eidus290fc382007-09-27 14:11:22 +02001028}
1029
Xiao Guangrongf759e2b2011-09-22 16:53:17 +08001030static bool rmap_can_add(struct kvm_vcpu *vcpu)
1031{
1032 struct kvm_mmu_memory_cache *cache;
1033
1034 cache = &vcpu->arch.mmu_pte_list_desc_cache;
1035 return mmu_memory_cache_free_objects(cache);
1036}
1037
Joerg Roedel44ad9942009-07-27 16:30:42 +02001038static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001039{
Avi Kivity4db35312007-11-21 15:28:32 +02001040 struct kvm_mmu_page *sp;
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001041 struct kvm_rmap_head *rmap_head;
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001042
Avi Kivity4db35312007-11-21 15:28:32 +02001043 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +08001044 kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn);
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001045 rmap_head = gfn_to_rmap(vcpu->kvm, gfn, sp);
1046 return pte_list_add(vcpu, spte, rmap_head);
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001047}
1048
Izik Eidus290fc382007-09-27 14:11:22 +02001049static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001050{
Avi Kivity4db35312007-11-21 15:28:32 +02001051 struct kvm_mmu_page *sp;
Lai Jiangshan2032a932010-05-26 16:49:59 +08001052 gfn_t gfn;
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001053 struct kvm_rmap_head *rmap_head;
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001054
Avi Kivity4db35312007-11-21 15:28:32 +02001055 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +08001056 gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt);
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001057 rmap_head = gfn_to_rmap(kvm, gfn, sp);
1058 pte_list_remove(spte, rmap_head);
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001059}
1060
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001061/*
1062 * Used by the following functions to iterate through the sptes linked by a
1063 * rmap. All fields are private and not assumed to be used outside.
1064 */
1065struct rmap_iterator {
1066 /* private fields */
1067 struct pte_list_desc *desc; /* holds the sptep if not NULL */
1068 int pos; /* index of the sptep */
1069};
1070
1071/*
1072 * Iteration must be started by this function. This should also be used after
1073 * removing/dropping sptes from the rmap link because in such cases the
1074 * information in the itererator may not be valid.
1075 *
1076 * Returns sptep if found, NULL otherwise.
1077 */
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001078static u64 *rmap_get_first(struct kvm_rmap_head *rmap_head,
1079 struct rmap_iterator *iter)
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001080{
Takuya Yoshikawa77fbbbd2015-11-20 17:45:44 +09001081 u64 *sptep;
1082
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001083 if (!rmap_head->val)
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001084 return NULL;
1085
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001086 if (!(rmap_head->val & 1)) {
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001087 iter->desc = NULL;
Takuya Yoshikawa77fbbbd2015-11-20 17:45:44 +09001088 sptep = (u64 *)rmap_head->val;
1089 goto out;
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001090 }
1091
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001092 iter->desc = (struct pte_list_desc *)(rmap_head->val & ~1ul);
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001093 iter->pos = 0;
Takuya Yoshikawa77fbbbd2015-11-20 17:45:44 +09001094 sptep = iter->desc->sptes[iter->pos];
1095out:
1096 BUG_ON(!is_shadow_present_pte(*sptep));
1097 return sptep;
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001098}
1099
1100/*
1101 * Must be used with a valid iterator: e.g. after rmap_get_first().
1102 *
1103 * Returns sptep if found, NULL otherwise.
1104 */
1105static u64 *rmap_get_next(struct rmap_iterator *iter)
1106{
Takuya Yoshikawa77fbbbd2015-11-20 17:45:44 +09001107 u64 *sptep;
1108
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001109 if (iter->desc) {
1110 if (iter->pos < PTE_LIST_EXT - 1) {
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001111 ++iter->pos;
1112 sptep = iter->desc->sptes[iter->pos];
1113 if (sptep)
Takuya Yoshikawa77fbbbd2015-11-20 17:45:44 +09001114 goto out;
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001115 }
1116
1117 iter->desc = iter->desc->more;
1118
1119 if (iter->desc) {
1120 iter->pos = 0;
1121 /* desc->sptes[0] cannot be NULL */
Takuya Yoshikawa77fbbbd2015-11-20 17:45:44 +09001122 sptep = iter->desc->sptes[iter->pos];
1123 goto out;
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001124 }
1125 }
1126
1127 return NULL;
Takuya Yoshikawa77fbbbd2015-11-20 17:45:44 +09001128out:
1129 BUG_ON(!is_shadow_present_pte(*sptep));
1130 return sptep;
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001131}
1132
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001133#define for_each_rmap_spte(_rmap_head_, _iter_, _spte_) \
1134 for (_spte_ = rmap_get_first(_rmap_head_, _iter_); \
Takuya Yoshikawa77fbbbd2015-11-20 17:45:44 +09001135 _spte_; _spte_ = rmap_get_next(_iter_))
Xiao Guangrong0d536792015-05-13 14:42:20 +08001136
Xiao Guangrongc3707952011-07-12 03:28:04 +08001137static void drop_spte(struct kvm *kvm, u64 *sptep)
Xiao Guangronge4b502e2010-07-16 11:28:09 +08001138{
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08001139 if (mmu_spte_clear_track_bits(sptep))
Marcelo Tosattieb45fda2010-10-25 11:58:22 -02001140 rmap_remove(kvm, sptep);
Avi Kivitybe38d272010-06-06 14:31:27 +03001141}
1142
Xiao Guangrong8e22f952012-06-20 15:57:39 +08001143
1144static bool __drop_large_spte(struct kvm *kvm, u64 *sptep)
1145{
1146 if (is_large_pte(*sptep)) {
1147 WARN_ON(page_header(__pa(sptep))->role.level ==
1148 PT_PAGE_TABLE_LEVEL);
1149 drop_spte(kvm, sptep);
1150 --kvm->stat.lpages;
1151 return true;
1152 }
1153
1154 return false;
1155}
1156
1157static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep)
1158{
1159 if (__drop_large_spte(vcpu->kvm, sptep))
1160 kvm_flush_remote_tlbs(vcpu->kvm);
1161}
1162
1163/*
Xiao Guangrong49fde342012-06-20 15:58:58 +08001164 * Write-protect on the specified @sptep, @pt_protect indicates whether
Xiao Guangrongc126d942014-04-17 17:06:14 +08001165 * spte write-protection is caused by protecting shadow page table.
Xiao Guangrong49fde342012-06-20 15:58:58 +08001166 *
Tiejun Chenb4619662014-09-22 10:31:38 +08001167 * Note: write protection is difference between dirty logging and spte
Xiao Guangrong49fde342012-06-20 15:58:58 +08001168 * protection:
1169 * - for dirty logging, the spte can be set to writable at anytime if
1170 * its dirty bitmap is properly set.
1171 * - for spte protection, the spte can be writable only after unsync-ing
1172 * shadow page.
Xiao Guangrong8e22f952012-06-20 15:57:39 +08001173 *
Xiao Guangrongc126d942014-04-17 17:06:14 +08001174 * Return true if tlb need be flushed.
Xiao Guangrong8e22f952012-06-20 15:57:39 +08001175 */
Xiao Guangrongc126d942014-04-17 17:06:14 +08001176static bool spte_write_protect(struct kvm *kvm, u64 *sptep, bool pt_protect)
Xiao Guangrongd13bc5b2012-06-20 15:57:15 +08001177{
1178 u64 spte = *sptep;
1179
Xiao Guangrong49fde342012-06-20 15:58:58 +08001180 if (!is_writable_pte(spte) &&
1181 !(pt_protect && spte_is_locklessly_modifiable(spte)))
Xiao Guangrongd13bc5b2012-06-20 15:57:15 +08001182 return false;
1183
1184 rmap_printk("rmap_write_protect: spte %p %llx\n", sptep, *sptep);
1185
Xiao Guangrong49fde342012-06-20 15:58:58 +08001186 if (pt_protect)
1187 spte &= ~SPTE_MMU_WRITEABLE;
Xiao Guangrongd13bc5b2012-06-20 15:57:15 +08001188 spte = spte & ~PT_WRITABLE_MASK;
Xiao Guangrong49fde342012-06-20 15:58:58 +08001189
Xiao Guangrongc126d942014-04-17 17:06:14 +08001190 return mmu_spte_update(sptep, spte);
Xiao Guangrongd13bc5b2012-06-20 15:57:15 +08001191}
1192
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001193static bool __rmap_write_protect(struct kvm *kvm,
1194 struct kvm_rmap_head *rmap_head,
Takuya Yoshikawa245c3912013-01-08 19:44:09 +09001195 bool pt_protect)
Takuya Yoshikawaa0ed4602012-03-01 19:31:22 +09001196{
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001197 u64 *sptep;
1198 struct rmap_iterator iter;
Xiao Guangrongd13bc5b2012-06-20 15:57:15 +08001199 bool flush = false;
Takuya Yoshikawaa0ed4602012-03-01 19:31:22 +09001200
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001201 for_each_rmap_spte(rmap_head, &iter, sptep)
Xiao Guangrongc126d942014-04-17 17:06:14 +08001202 flush |= spte_write_protect(kvm, sptep, pt_protect);
Takuya Yoshikawaa0ed4602012-03-01 19:31:22 +09001203
Xiao Guangrongd13bc5b2012-06-20 15:57:15 +08001204 return flush;
Takuya Yoshikawaa0ed4602012-03-01 19:31:22 +09001205}
1206
Kai Huangf4b4b182015-01-28 10:54:24 +08001207static bool spte_clear_dirty(struct kvm *kvm, u64 *sptep)
1208{
1209 u64 spte = *sptep;
1210
1211 rmap_printk("rmap_clear_dirty: spte %p %llx\n", sptep, *sptep);
1212
1213 spte &= ~shadow_dirty_mask;
1214
1215 return mmu_spte_update(sptep, spte);
1216}
1217
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001218static bool __rmap_clear_dirty(struct kvm *kvm, struct kvm_rmap_head *rmap_head)
Kai Huangf4b4b182015-01-28 10:54:24 +08001219{
1220 u64 *sptep;
1221 struct rmap_iterator iter;
1222 bool flush = false;
1223
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001224 for_each_rmap_spte(rmap_head, &iter, sptep)
Kai Huangf4b4b182015-01-28 10:54:24 +08001225 flush |= spte_clear_dirty(kvm, sptep);
Kai Huangf4b4b182015-01-28 10:54:24 +08001226
1227 return flush;
1228}
1229
1230static bool spte_set_dirty(struct kvm *kvm, u64 *sptep)
1231{
1232 u64 spte = *sptep;
1233
1234 rmap_printk("rmap_set_dirty: spte %p %llx\n", sptep, *sptep);
1235
1236 spte |= shadow_dirty_mask;
1237
1238 return mmu_spte_update(sptep, spte);
1239}
1240
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001241static bool __rmap_set_dirty(struct kvm *kvm, struct kvm_rmap_head *rmap_head)
Kai Huangf4b4b182015-01-28 10:54:24 +08001242{
1243 u64 *sptep;
1244 struct rmap_iterator iter;
1245 bool flush = false;
1246
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001247 for_each_rmap_spte(rmap_head, &iter, sptep)
Kai Huangf4b4b182015-01-28 10:54:24 +08001248 flush |= spte_set_dirty(kvm, sptep);
Kai Huangf4b4b182015-01-28 10:54:24 +08001249
1250 return flush;
1251}
1252
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001253/**
Kai Huang3b0f1d02015-01-28 10:54:23 +08001254 * kvm_mmu_write_protect_pt_masked - write protect selected PT level pages
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001255 * @kvm: kvm instance
1256 * @slot: slot to protect
1257 * @gfn_offset: start of the BITS_PER_LONG pages we care about
1258 * @mask: indicates which pages we should protect
1259 *
1260 * Used when we do not need to care about huge page mappings: e.g. during dirty
1261 * logging we do not have any such mappings.
1262 */
Kai Huang3b0f1d02015-01-28 10:54:23 +08001263static void kvm_mmu_write_protect_pt_masked(struct kvm *kvm,
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001264 struct kvm_memory_slot *slot,
1265 gfn_t gfn_offset, unsigned long mask)
Izik Eidus98348e92007-10-16 14:42:30 +02001266{
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001267 struct kvm_rmap_head *rmap_head;
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001268
1269 while (mask) {
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001270 rmap_head = __gfn_to_rmap(slot->base_gfn + gfn_offset + __ffs(mask),
1271 PT_PAGE_TABLE_LEVEL, slot);
1272 __rmap_write_protect(kvm, rmap_head, false);
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001273
1274 /* clear the first set bit */
1275 mask &= mask - 1;
1276 }
1277}
1278
Kai Huang3b0f1d02015-01-28 10:54:23 +08001279/**
Kai Huangf4b4b182015-01-28 10:54:24 +08001280 * kvm_mmu_clear_dirty_pt_masked - clear MMU D-bit for PT level pages
1281 * @kvm: kvm instance
1282 * @slot: slot to clear D-bit
1283 * @gfn_offset: start of the BITS_PER_LONG pages we care about
1284 * @mask: indicates which pages we should clear D-bit
1285 *
1286 * Used for PML to re-log the dirty GPAs after userspace querying dirty_bitmap.
1287 */
1288void kvm_mmu_clear_dirty_pt_masked(struct kvm *kvm,
1289 struct kvm_memory_slot *slot,
1290 gfn_t gfn_offset, unsigned long mask)
1291{
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001292 struct kvm_rmap_head *rmap_head;
Kai Huangf4b4b182015-01-28 10:54:24 +08001293
1294 while (mask) {
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001295 rmap_head = __gfn_to_rmap(slot->base_gfn + gfn_offset + __ffs(mask),
1296 PT_PAGE_TABLE_LEVEL, slot);
1297 __rmap_clear_dirty(kvm, rmap_head);
Kai Huangf4b4b182015-01-28 10:54:24 +08001298
1299 /* clear the first set bit */
1300 mask &= mask - 1;
1301 }
1302}
1303EXPORT_SYMBOL_GPL(kvm_mmu_clear_dirty_pt_masked);
1304
1305/**
Kai Huang3b0f1d02015-01-28 10:54:23 +08001306 * kvm_arch_mmu_enable_log_dirty_pt_masked - enable dirty logging for selected
1307 * PT level pages.
1308 *
1309 * It calls kvm_mmu_write_protect_pt_masked to write protect selected pages to
1310 * enable dirty logging for them.
1311 *
1312 * Used when we do not need to care about huge page mappings: e.g. during dirty
1313 * logging we do not have any such mappings.
1314 */
1315void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
1316 struct kvm_memory_slot *slot,
1317 gfn_t gfn_offset, unsigned long mask)
1318{
Kai Huang88178fd2015-01-28 10:54:27 +08001319 if (kvm_x86_ops->enable_log_dirty_pt_masked)
1320 kvm_x86_ops->enable_log_dirty_pt_masked(kvm, slot, gfn_offset,
1321 mask);
1322 else
1323 kvm_mmu_write_protect_pt_masked(kvm, slot, gfn_offset, mask);
Kai Huang3b0f1d02015-01-28 10:54:23 +08001324}
1325
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02001326static bool rmap_write_protect(struct kvm_vcpu *vcpu, u64 gfn)
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001327{
1328 struct kvm_memory_slot *slot;
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001329 struct kvm_rmap_head *rmap_head;
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001330 int i;
Xiao Guangrong2f845692012-06-20 15:56:53 +08001331 bool write_protected = false;
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001332
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02001333 slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -08001334
Xiao Guangrong8a3d08f2015-05-13 14:42:21 +08001335 for (i = PT_PAGE_TABLE_LEVEL; i <= PT_MAX_HUGEPAGE_LEVEL; ++i) {
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001336 rmap_head = __gfn_to_rmap(gfn, i, slot);
1337 write_protected |= __rmap_write_protect(vcpu->kvm, rmap_head, true);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001338 }
1339
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001340 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -08001341}
1342
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001343static bool kvm_zap_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head)
Xiao Guangrong6a49f852015-05-13 14:42:25 +08001344{
1345 u64 *sptep;
1346 struct rmap_iterator iter;
1347 bool flush = false;
1348
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001349 while ((sptep = rmap_get_first(rmap_head, &iter))) {
Xiao Guangrong6a49f852015-05-13 14:42:25 +08001350 rmap_printk("%s: spte %p %llx.\n", __func__, sptep, *sptep);
1351
1352 drop_spte(kvm, sptep);
1353 flush = true;
1354 }
1355
1356 return flush;
1357}
1358
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001359static int kvm_unmap_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
Andres Lagar-Cavilla8a9522d2014-09-23 12:34:54 -07001360 struct kvm_memory_slot *slot, gfn_t gfn, int level,
1361 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001362{
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001363 return kvm_zap_rmapp(kvm, rmap_head);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001364}
1365
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001366static int kvm_set_pte_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
Andres Lagar-Cavilla8a9522d2014-09-23 12:34:54 -07001367 struct kvm_memory_slot *slot, gfn_t gfn, int level,
1368 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +03001369{
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001370 u64 *sptep;
1371 struct rmap_iterator iter;
Izik Eidus3da0dd42009-09-23 21:47:18 +03001372 int need_flush = 0;
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001373 u64 new_spte;
Izik Eidus3da0dd42009-09-23 21:47:18 +03001374 pte_t *ptep = (pte_t *)data;
Dan Williamsba049e92016-01-15 16:56:11 -08001375 kvm_pfn_t new_pfn;
Izik Eidus3da0dd42009-09-23 21:47:18 +03001376
1377 WARN_ON(pte_huge(*ptep));
1378 new_pfn = pte_pfn(*ptep);
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001379
Xiao Guangrong0d536792015-05-13 14:42:20 +08001380restart:
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001381 for_each_rmap_spte(rmap_head, &iter, sptep) {
Andres Lagar-Cavilla8a9522d2014-09-23 12:34:54 -07001382 rmap_printk("kvm_set_pte_rmapp: spte %p %llx gfn %llx (%d)\n",
1383 sptep, *sptep, gfn, level);
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001384
Izik Eidus3da0dd42009-09-23 21:47:18 +03001385 need_flush = 1;
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001386
Izik Eidus3da0dd42009-09-23 21:47:18 +03001387 if (pte_write(*ptep)) {
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001388 drop_spte(kvm, sptep);
Xiao Guangrong0d536792015-05-13 14:42:20 +08001389 goto restart;
Izik Eidus3da0dd42009-09-23 21:47:18 +03001390 } else {
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001391 new_spte = *sptep & ~PT64_BASE_ADDR_MASK;
Izik Eidus3da0dd42009-09-23 21:47:18 +03001392 new_spte |= (u64)new_pfn << PAGE_SHIFT;
1393
1394 new_spte &= ~PT_WRITABLE_MASK;
1395 new_spte &= ~SPTE_HOST_WRITEABLE;
Avi Kivityb79b93f2010-06-06 15:46:44 +03001396 new_spte &= ~shadow_accessed_mask;
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001397
1398 mmu_spte_clear_track_bits(sptep);
1399 mmu_spte_set(sptep, new_spte);
Izik Eidus3da0dd42009-09-23 21:47:18 +03001400 }
1401 }
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001402
Izik Eidus3da0dd42009-09-23 21:47:18 +03001403 if (need_flush)
1404 kvm_flush_remote_tlbs(kvm);
1405
1406 return 0;
1407}
1408
Xiao Guangrong6ce1f4e2015-05-13 14:42:22 +08001409struct slot_rmap_walk_iterator {
1410 /* input fields. */
1411 struct kvm_memory_slot *slot;
1412 gfn_t start_gfn;
1413 gfn_t end_gfn;
1414 int start_level;
1415 int end_level;
1416
1417 /* output fields. */
1418 gfn_t gfn;
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001419 struct kvm_rmap_head *rmap;
Xiao Guangrong6ce1f4e2015-05-13 14:42:22 +08001420 int level;
1421
1422 /* private field. */
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001423 struct kvm_rmap_head *end_rmap;
Xiao Guangrong6ce1f4e2015-05-13 14:42:22 +08001424};
1425
1426static void
1427rmap_walk_init_level(struct slot_rmap_walk_iterator *iterator, int level)
1428{
1429 iterator->level = level;
1430 iterator->gfn = iterator->start_gfn;
1431 iterator->rmap = __gfn_to_rmap(iterator->gfn, level, iterator->slot);
1432 iterator->end_rmap = __gfn_to_rmap(iterator->end_gfn, level,
1433 iterator->slot);
1434}
1435
1436static void
1437slot_rmap_walk_init(struct slot_rmap_walk_iterator *iterator,
1438 struct kvm_memory_slot *slot, int start_level,
1439 int end_level, gfn_t start_gfn, gfn_t end_gfn)
1440{
1441 iterator->slot = slot;
1442 iterator->start_level = start_level;
1443 iterator->end_level = end_level;
1444 iterator->start_gfn = start_gfn;
1445 iterator->end_gfn = end_gfn;
1446
1447 rmap_walk_init_level(iterator, iterator->start_level);
1448}
1449
1450static bool slot_rmap_walk_okay(struct slot_rmap_walk_iterator *iterator)
1451{
1452 return !!iterator->rmap;
1453}
1454
1455static void slot_rmap_walk_next(struct slot_rmap_walk_iterator *iterator)
1456{
1457 if (++iterator->rmap <= iterator->end_rmap) {
1458 iterator->gfn += (1UL << KVM_HPAGE_GFN_SHIFT(iterator->level));
1459 return;
1460 }
1461
1462 if (++iterator->level > iterator->end_level) {
1463 iterator->rmap = NULL;
1464 return;
1465 }
1466
1467 rmap_walk_init_level(iterator, iterator->level);
1468}
1469
1470#define for_each_slot_rmap_range(_slot_, _start_level_, _end_level_, \
1471 _start_gfn, _end_gfn, _iter_) \
1472 for (slot_rmap_walk_init(_iter_, _slot_, _start_level_, \
1473 _end_level_, _start_gfn, _end_gfn); \
1474 slot_rmap_walk_okay(_iter_); \
1475 slot_rmap_walk_next(_iter_))
1476
Takuya Yoshikawa84504ef2012-07-02 17:55:48 +09001477static int kvm_handle_hva_range(struct kvm *kvm,
1478 unsigned long start,
1479 unsigned long end,
1480 unsigned long data,
1481 int (*handler)(struct kvm *kvm,
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001482 struct kvm_rmap_head *rmap_head,
Takuya Yoshikawa048212d2012-07-02 17:57:59 +09001483 struct kvm_memory_slot *slot,
Andres Lagar-Cavilla8a9522d2014-09-23 12:34:54 -07001484 gfn_t gfn,
1485 int level,
Takuya Yoshikawa84504ef2012-07-02 17:55:48 +09001486 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001487{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001488 struct kvm_memslots *slots;
Xiao Guangrongbe6ba0f2011-11-24 17:39:18 +08001489 struct kvm_memory_slot *memslot;
Xiao Guangrong6ce1f4e2015-05-13 14:42:22 +08001490 struct slot_rmap_walk_iterator iterator;
1491 int ret = 0;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02001492 int i;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001493
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02001494 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
1495 slots = __kvm_memslots(kvm, i);
1496 kvm_for_each_memslot(memslot, slots) {
1497 unsigned long hva_start, hva_end;
1498 gfn_t gfn_start, gfn_end;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001499
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02001500 hva_start = max(start, memslot->userspace_addr);
1501 hva_end = min(end, memslot->userspace_addr +
1502 (memslot->npages << PAGE_SHIFT));
1503 if (hva_start >= hva_end)
1504 continue;
1505 /*
1506 * {gfn(page) | page intersects with [hva_start, hva_end)} =
1507 * {gfn_start, gfn_start+1, ..., gfn_end-1}.
1508 */
1509 gfn_start = hva_to_gfn_memslot(hva_start, memslot);
1510 gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001511
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02001512 for_each_slot_rmap_range(memslot, PT_PAGE_TABLE_LEVEL,
1513 PT_MAX_HUGEPAGE_LEVEL,
1514 gfn_start, gfn_end - 1,
1515 &iterator)
1516 ret |= handler(kvm, iterator.rmap, memslot,
1517 iterator.gfn, iterator.level, data);
1518 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001519 }
1520
Takuya Yoshikawaf3953022012-07-02 17:58:48 +09001521 return ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001522}
1523
Takuya Yoshikawa84504ef2012-07-02 17:55:48 +09001524static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
1525 unsigned long data,
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001526 int (*handler)(struct kvm *kvm,
1527 struct kvm_rmap_head *rmap_head,
Takuya Yoshikawa048212d2012-07-02 17:57:59 +09001528 struct kvm_memory_slot *slot,
Andres Lagar-Cavilla8a9522d2014-09-23 12:34:54 -07001529 gfn_t gfn, int level,
Takuya Yoshikawa84504ef2012-07-02 17:55:48 +09001530 unsigned long data))
1531{
1532 return kvm_handle_hva_range(kvm, hva, hva + 1, data, handler);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001533}
1534
1535int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
1536{
Izik Eidus3da0dd42009-09-23 21:47:18 +03001537 return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001538}
1539
Takuya Yoshikawab3ae2092012-07-02 17:56:33 +09001540int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end)
1541{
1542 return kvm_handle_hva_range(kvm, start, end, 0, kvm_unmap_rmapp);
1543}
1544
Izik Eidus3da0dd42009-09-23 21:47:18 +03001545void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
1546{
Frederik Deweerdt8a8365c2009-10-09 11:42:56 +00001547 kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
Izik Eidus3da0dd42009-09-23 21:47:18 +03001548}
1549
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001550static int kvm_age_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
Andres Lagar-Cavilla8a9522d2014-09-23 12:34:54 -07001551 struct kvm_memory_slot *slot, gfn_t gfn, int level,
1552 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001553{
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001554 u64 *sptep;
Michael S. Tsirkin79f702a2012-06-03 11:34:08 +03001555 struct rmap_iterator uninitialized_var(iter);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001556 int young = 0;
1557
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -07001558 BUG_ON(!shadow_accessed_mask);
Sheng Yang534e38b2008-09-08 15:12:30 +08001559
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001560 for_each_rmap_spte(rmap_head, &iter, sptep) {
Xudong Hao3f6d8c82012-05-22 11:23:15 +08001561 if (*sptep & shadow_accessed_mask) {
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001562 young = 1;
Xudong Hao3f6d8c82012-05-22 11:23:15 +08001563 clear_bit((ffs(shadow_accessed_mask) - 1),
1564 (unsigned long *)sptep);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001565 }
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001566 }
Xiao Guangrong0d536792015-05-13 14:42:20 +08001567
Andres Lagar-Cavilla8a9522d2014-09-23 12:34:54 -07001568 trace_kvm_age_page(gfn, level, slot, young);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001569 return young;
1570}
1571
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001572static int kvm_test_age_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
Andres Lagar-Cavilla8a9522d2014-09-23 12:34:54 -07001573 struct kvm_memory_slot *slot, gfn_t gfn,
1574 int level, unsigned long data)
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001575{
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001576 u64 *sptep;
1577 struct rmap_iterator iter;
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001578 int young = 0;
1579
1580 /*
1581 * If there's no access bit in the secondary pte set by the
1582 * hardware it's up to gup-fast/gup to set the access bit in
1583 * the primary pte or in the page structure.
1584 */
1585 if (!shadow_accessed_mask)
1586 goto out;
1587
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001588 for_each_rmap_spte(rmap_head, &iter, sptep) {
Xudong Hao3f6d8c82012-05-22 11:23:15 +08001589 if (*sptep & shadow_accessed_mask) {
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001590 young = 1;
1591 break;
1592 }
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001593 }
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001594out:
1595 return young;
1596}
1597
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001598#define RMAP_RECYCLE_THRESHOLD 1000
1599
Joerg Roedel852e3c12009-07-27 16:30:44 +02001600static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001601{
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001602 struct kvm_rmap_head *rmap_head;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001603 struct kvm_mmu_page *sp;
1604
1605 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001606
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001607 rmap_head = gfn_to_rmap(vcpu->kvm, gfn, sp);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001608
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001609 kvm_unmap_rmapp(vcpu->kvm, rmap_head, NULL, gfn, sp->role.level, 0);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001610 kvm_flush_remote_tlbs(vcpu->kvm);
1611}
1612
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -07001613int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end)
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001614{
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -07001615 /*
1616 * In case of absence of EPT Access and Dirty Bits supports,
1617 * emulate the accessed bit for EPT, by checking if this page has
1618 * an EPT mapping, and clearing it if it does. On the next access,
1619 * a new EPT mapping will be established.
1620 * This has some overhead, but not as much as the cost of swapping
1621 * out actively used pages or breaking up actively used hugepages.
1622 */
1623 if (!shadow_accessed_mask) {
1624 /*
1625 * We are holding the kvm->mmu_lock, and we are blowing up
1626 * shadow PTEs. MMU notifier consumers need to be kept at bay.
1627 * This is correct as long as we don't decouple the mmu_lock
1628 * protected regions (like invalidate_range_start|end does).
1629 */
1630 kvm->mmu_notifier_seq++;
1631 return kvm_handle_hva_range(kvm, start, end, 0,
1632 kvm_unmap_rmapp);
1633 }
1634
1635 return kvm_handle_hva_range(kvm, start, end, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001636}
1637
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001638int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
1639{
1640 return kvm_handle_hva(kvm, hva, 0, kvm_test_age_rmapp);
1641}
1642
Yaozu Dongd6c69ee2007-04-25 14:17:25 +08001643#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +03001644static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001645{
Avi Kivity139bdb22007-01-05 16:36:50 -08001646 u64 *pos;
1647 u64 *end;
1648
Avi Kivity47ad8e62007-05-06 15:50:58 +03001649 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +03001650 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001651 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -08001652 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001653 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -08001654 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001655 return 1;
1656}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +08001657#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001658
Dave Hansen45221ab2010-08-19 18:11:37 -07001659/*
1660 * This value is the sum of all of the kvm instances's
1661 * kvm->arch.n_used_mmu_pages values. We need a global,
1662 * aggregate version in order to make the slab shrinker
1663 * faster
1664 */
1665static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, int nr)
1666{
1667 kvm->arch.n_used_mmu_pages += nr;
1668 percpu_counter_add(&kvm_total_used_mmu_pages, nr);
1669}
1670
Gleb Natapov834be0d2013-01-30 16:45:05 +02001671static void kvm_mmu_free_page(struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -08001672{
Paolo Bonzinifa4a2c02013-10-02 16:56:16 +02001673 MMU_WARN_ON(!is_empty_shadow_page(sp->spt));
Xiao Guangrong77758342010-06-04 21:53:54 +08001674 hlist_del(&sp->hash_link);
Xiao Guangrongbd4c86e2011-07-12 03:27:14 +08001675 list_del(&sp->link);
1676 free_page((unsigned long)sp->spt);
Gleb Natapov834be0d2013-01-30 16:45:05 +02001677 if (!sp->role.direct)
1678 free_page((unsigned long)sp->gfns);
Xiao Guangronge8ad9a72010-05-13 10:06:02 +08001679 kmem_cache_free(mmu_page_header_cache, sp);
Avi Kivity260746c2007-01-05 16:36:49 -08001680}
1681
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001682static unsigned kvm_page_table_hashfn(gfn_t gfn)
1683{
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001684 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001685}
1686
Xiao Guangrong67052b32011-05-15 23:27:08 +08001687static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
1688 struct kvm_mmu_page *sp, u64 *parent_pte)
1689{
1690 if (!parent_pte)
1691 return;
1692
1693 pte_list_add(vcpu, parent_pte, &sp->parent_ptes);
1694}
1695
1696static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
1697 u64 *parent_pte)
1698{
1699 pte_list_remove(parent_pte, &sp->parent_ptes);
1700}
1701
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001702static void drop_parent_pte(struct kvm_mmu_page *sp,
1703 u64 *parent_pte)
1704{
1705 mmu_page_remove_parent_pte(sp, parent_pte);
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08001706 mmu_spte_clear_no_track(parent_pte);
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001707}
1708
Takuya Yoshikawa47005792015-11-20 17:46:29 +09001709static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, int direct)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001710{
Avi Kivity4db35312007-11-21 15:28:32 +02001711 struct kvm_mmu_page *sp;
Takuya Yoshikawa7ddca7e2013-03-21 19:33:43 +09001712
Takuya Yoshikawa80feb892012-05-29 23:54:26 +09001713 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache);
1714 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache);
Lai Jiangshan2032a932010-05-26 16:49:59 +08001715 if (!direct)
Takuya Yoshikawa80feb892012-05-29 23:54:26 +09001716 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache);
Avi Kivity4db35312007-11-21 15:28:32 +02001717 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08001718
1719 /*
1720 * The active_mmu_pages list is the FIFO list, do not move the
1721 * page until it is zapped. kvm_zap_obsolete_pages depends on
1722 * this feature. See the comments in kvm_zap_obsolete_pages().
1723 */
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001724 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Dave Hansen45221ab2010-08-19 18:11:37 -07001725 kvm_mod_used_mmu_pages(vcpu->kvm, +1);
Avi Kivity4db35312007-11-21 15:28:32 +02001726 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001727}
1728
Xiao Guangrong67052b32011-05-15 23:27:08 +08001729static void mark_unsync(u64 *spte);
Xiao Guangrong6b184932010-04-16 21:29:17 +08001730static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001731{
Takuya Yoshikawa74c4e632015-11-26 21:15:38 +09001732 u64 *sptep;
1733 struct rmap_iterator iter;
1734
1735 for_each_rmap_spte(&sp->parent_ptes, &iter, sptep) {
1736 mark_unsync(sptep);
1737 }
Xiao Guangrong1047df12010-06-11 21:35:15 +08001738}
1739
Xiao Guangrong67052b32011-05-15 23:27:08 +08001740static void mark_unsync(u64 *spte)
Xiao Guangrong1047df12010-06-11 21:35:15 +08001741{
Xiao Guangrong67052b32011-05-15 23:27:08 +08001742 struct kvm_mmu_page *sp;
Xiao Guangrong1047df12010-06-11 21:35:15 +08001743 unsigned int index;
1744
Xiao Guangrong67052b32011-05-15 23:27:08 +08001745 sp = page_header(__pa(spte));
Xiao Guangrong1047df12010-06-11 21:35:15 +08001746 index = spte - sp->spt;
1747 if (__test_and_set_bit(index, sp->unsync_child_bitmap))
1748 return;
1749 if (sp->unsync_children++)
1750 return;
1751 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001752}
1753
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001754static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001755 struct kvm_mmu_page *sp)
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001756{
1757 return 1;
1758}
1759
Marcelo Tosattia7052892008-09-23 13:18:35 -03001760static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1761{
1762}
1763
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08001764static void nonpaging_update_pte(struct kvm_vcpu *vcpu,
1765 struct kvm_mmu_page *sp, u64 *spte,
Xiao Guangrong7c562522011-03-28 10:29:27 +08001766 const void *pte)
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08001767{
1768 WARN_ON(1);
1769}
1770
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001771#define KVM_PAGE_ARRAY_NR 16
1772
1773struct kvm_mmu_pages {
1774 struct mmu_page_and_offset {
1775 struct kvm_mmu_page *sp;
1776 unsigned int idx;
1777 } page[KVM_PAGE_ARRAY_NR];
1778 unsigned int nr;
1779};
1780
Hannes Edercded19f2009-02-21 02:19:13 +01001781static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1782 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001783{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001784 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001785
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001786 if (sp->unsync)
1787 for (i=0; i < pvec->nr; i++)
1788 if (pvec->page[i].sp == sp)
1789 return 0;
1790
1791 pvec->page[pvec->nr].sp = sp;
1792 pvec->page[pvec->nr].idx = idx;
1793 pvec->nr++;
1794 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1795}
1796
Takuya Yoshikawafd951452015-11-20 17:43:13 +09001797static inline void clear_unsync_child_bit(struct kvm_mmu_page *sp, int idx)
1798{
1799 --sp->unsync_children;
1800 WARN_ON((int)sp->unsync_children < 0);
1801 __clear_bit(idx, sp->unsync_child_bitmap);
1802}
1803
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001804static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1805 struct kvm_mmu_pages *pvec)
1806{
1807 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001808
Takuya Yoshikawa37178b82011-11-29 14:02:45 +09001809 for_each_set_bit(i, sp->unsync_child_bitmap, 512) {
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001810 struct kvm_mmu_page *child;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001811 u64 ent = sp->spt[i];
1812
Takuya Yoshikawafd951452015-11-20 17:43:13 +09001813 if (!is_shadow_present_pte(ent) || is_large_pte(ent)) {
1814 clear_unsync_child_bit(sp, i);
1815 continue;
1816 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001817
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001818 child = page_header(ent & PT64_BASE_ADDR_MASK);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001819
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001820 if (child->unsync_children) {
1821 if (mmu_pages_add(pvec, child, i))
1822 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001823
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001824 ret = __mmu_unsync_walk(child, pvec);
Takuya Yoshikawafd951452015-11-20 17:43:13 +09001825 if (!ret) {
1826 clear_unsync_child_bit(sp, i);
1827 continue;
1828 } else if (ret > 0) {
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001829 nr_unsync_leaf += ret;
Takuya Yoshikawafd951452015-11-20 17:43:13 +09001830 } else
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001831 return ret;
1832 } else if (child->unsync) {
1833 nr_unsync_leaf++;
1834 if (mmu_pages_add(pvec, child, i))
1835 return -ENOSPC;
1836 } else
Takuya Yoshikawafd951452015-11-20 17:43:13 +09001837 clear_unsync_child_bit(sp, i);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001838 }
1839
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001840 return nr_unsync_leaf;
1841}
1842
1843static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1844 struct kvm_mmu_pages *pvec)
1845{
1846 if (!sp->unsync_children)
1847 return 0;
1848
1849 mmu_pages_add(pvec, sp, 0);
1850 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001851}
1852
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001853static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1854{
1855 WARN_ON(!sp->unsync);
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001856 trace_kvm_mmu_sync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001857 sp->unsync = 0;
1858 --kvm->stat.mmu_unsync;
1859}
1860
Xiao Guangrong77758342010-06-04 21:53:54 +08001861static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1862 struct list_head *invalid_list);
1863static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1864 struct list_head *invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001865
Xiao Guangrongf34d2512013-05-31 08:36:28 +08001866/*
1867 * NOTE: we should pay more attention on the zapped-obsolete page
1868 * (is_obsolete_sp(sp) && sp->role.invalid) when you do hash list walk
1869 * since it has been deleted from active_mmu_pages but still can be found
1870 * at hast list.
1871 *
1872 * for_each_gfn_indirect_valid_sp has skipped that kind of page and
1873 * kvm_mmu_get_page(), the only user of for_each_gfn_sp(), has skipped
1874 * all the obsolete pages.
1875 */
Takuya Yoshikawa1044b032013-03-06 16:05:07 +09001876#define for_each_gfn_sp(_kvm, _sp, _gfn) \
1877 hlist_for_each_entry(_sp, \
1878 &(_kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(_gfn)], hash_link) \
1879 if ((_sp)->gfn != (_gfn)) {} else
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001880
Takuya Yoshikawa1044b032013-03-06 16:05:07 +09001881#define for_each_gfn_indirect_valid_sp(_kvm, _sp, _gfn) \
1882 for_each_gfn_sp(_kvm, _sp, _gfn) \
1883 if ((_sp)->role.direct || (_sp)->role.invalid) {} else
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001884
Xiao Guangrongf918b442010-06-11 21:30:36 +08001885/* @sp->gfn should be write-protected at the call site */
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001886static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001887 struct list_head *invalid_list, bool clear_unsync)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001888{
Avi Kivity5b7e0102010-04-14 19:20:03 +03001889 if (sp->role.cr4_pae != !!is_pae(vcpu)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001890 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001891 return 1;
1892 }
1893
Xiao Guangrongf918b442010-06-11 21:30:36 +08001894 if (clear_unsync)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001895 kvm_unlink_unsync_page(vcpu->kvm, sp);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001896
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001897 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001898 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001899 return 1;
1900 }
1901
Liang Chen77c39132014-09-18 12:38:37 -04001902 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001903 return 0;
1904}
1905
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001906static int kvm_sync_page_transient(struct kvm_vcpu *vcpu,
1907 struct kvm_mmu_page *sp)
1908{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001909 LIST_HEAD(invalid_list);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001910 int ret;
1911
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001912 ret = __kvm_sync_page(vcpu, sp, &invalid_list, false);
Xiao Guangrongbe71e062010-06-11 21:31:38 +08001913 if (ret)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001914 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
1915
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001916 return ret;
1917}
1918
Xiao Guangronge37fa782011-11-30 17:43:24 +08001919#ifdef CONFIG_KVM_MMU_AUDIT
1920#include "mmu_audit.c"
1921#else
1922static void kvm_mmu_audit(struct kvm_vcpu *vcpu, int point) { }
1923static void mmu_audit_disable(void) { }
1924#endif
1925
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001926static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1927 struct list_head *invalid_list)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001928{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001929 return __kvm_sync_page(vcpu, sp, invalid_list, true);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001930}
1931
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001932/* @gfn should be write-protected at the call site */
1933static void kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
1934{
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001935 struct kvm_mmu_page *s;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001936 LIST_HEAD(invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001937 bool flush = false;
1938
Sasha Levinb67bfe02013-02-27 17:06:00 -08001939 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001940 if (!s->unsync)
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001941 continue;
1942
1943 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001944 kvm_unlink_unsync_page(vcpu->kvm, s);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001945 if ((s->role.cr4_pae != !!is_pae(vcpu)) ||
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001946 (vcpu->arch.mmu.sync_page(vcpu, s))) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001947 kvm_mmu_prepare_zap_page(vcpu->kvm, s, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001948 continue;
1949 }
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001950 flush = true;
1951 }
1952
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001953 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001954 if (flush)
Liang Chen77c39132014-09-18 12:38:37 -04001955 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001956}
1957
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001958struct mmu_page_path {
1959 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1960 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001961};
1962
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001963#define for_each_sp(pvec, sp, parents, i) \
1964 for (i = mmu_pages_next(&pvec, &parents, -1), \
1965 sp = pvec.page[i].sp; \
1966 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1967 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001968
Hannes Edercded19f2009-02-21 02:19:13 +01001969static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1970 struct mmu_page_path *parents,
1971 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001972{
1973 int n;
1974
1975 for (n = i+1; n < pvec->nr; n++) {
1976 struct kvm_mmu_page *sp = pvec->page[n].sp;
1977
1978 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1979 parents->idx[0] = pvec->page[n].idx;
1980 return n;
1981 }
1982
1983 parents->parent[sp->role.level-2] = sp;
1984 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1985 }
1986
1987 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001988}
1989
Hannes Edercded19f2009-02-21 02:19:13 +01001990static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001991{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001992 struct kvm_mmu_page *sp;
1993 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001994
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001995 do {
1996 unsigned int idx = parents->idx[level];
1997
1998 sp = parents->parent[level];
1999 if (!sp)
2000 return;
2001
Takuya Yoshikawafd951452015-11-20 17:43:13 +09002002 clear_unsync_child_bit(sp, idx);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002003 level++;
2004 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
2005}
2006
2007static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
2008 struct mmu_page_path *parents,
2009 struct kvm_mmu_pages *pvec)
2010{
2011 parents->parent[parent->role.level-1] = NULL;
2012 pvec->nr = 0;
2013}
2014
2015static void mmu_sync_children(struct kvm_vcpu *vcpu,
2016 struct kvm_mmu_page *parent)
2017{
2018 int i;
2019 struct kvm_mmu_page *sp;
2020 struct mmu_page_path parents;
2021 struct kvm_mmu_pages pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002022 LIST_HEAD(invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002023
2024 kvm_mmu_pages_init(parent, &parents, &pages);
2025 while (mmu_unsync_walk(parent, &pages)) {
Xiao Guangrong2f845692012-06-20 15:56:53 +08002026 bool protected = false;
Marcelo Tosattib1a36822008-12-01 22:32:03 -02002027
2028 for_each_sp(pages, sp, parents, i)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002029 protected |= rmap_write_protect(vcpu, sp->gfn);
Marcelo Tosattib1a36822008-12-01 22:32:03 -02002030
2031 if (protected)
2032 kvm_flush_remote_tlbs(vcpu->kvm);
2033
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002034 for_each_sp(pages, sp, parents, i) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002035 kvm_sync_page(vcpu, sp, &invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002036 mmu_pages_clear_parents(&parents);
2037 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002038 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002039 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002040 kvm_mmu_pages_init(parent, &parents, &pages);
2041 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002042}
2043
Xiao Guangronga30f47c2011-09-22 16:58:36 +08002044static void __clear_sp_write_flooding_count(struct kvm_mmu_page *sp)
2045{
2046 sp->write_flooding_count = 0;
2047}
2048
2049static void clear_sp_write_flooding_count(u64 *spte)
2050{
2051 struct kvm_mmu_page *sp = page_header(__pa(spte));
2052
2053 __clear_sp_write_flooding_count(sp);
2054}
2055
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08002056static bool is_obsolete_sp(struct kvm *kvm, struct kvm_mmu_page *sp)
2057{
2058 return unlikely(sp->mmu_valid_gen != kvm->arch.mmu_valid_gen);
2059}
2060
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002061static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
2062 gfn_t gfn,
2063 gva_t gaddr,
2064 unsigned level,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002065 int direct,
Takuya Yoshikawabb11c6c2015-11-26 21:16:35 +09002066 unsigned access)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002067{
2068 union kvm_mmu_page_role role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002069 unsigned quadrant;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08002070 struct kvm_mmu_page *sp;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08002071 bool need_sync = false;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002072
Avi Kivitya770f6f2008-12-21 19:20:09 +02002073 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002074 role.level = level;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002075 role.direct = direct;
Avi Kivity84b0c8c2010-03-14 10:16:40 +02002076 if (role.direct)
Avi Kivity5b7e0102010-04-14 19:20:03 +03002077 role.cr4_pae = 0;
Avi Kivity41074d02007-12-09 17:00:02 +02002078 role.access = access;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02002079 if (!vcpu->arch.mmu.direct_map
2080 && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002081 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
2082 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
2083 role.quadrant = quadrant;
2084 }
Sasha Levinb67bfe02013-02-27 17:06:00 -08002085 for_each_gfn_sp(vcpu->kvm, sp, gfn) {
Xiao Guangrong7f52af72013-05-31 08:36:26 +08002086 if (is_obsolete_sp(vcpu->kvm, sp))
2087 continue;
2088
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002089 if (!need_sync && sp->unsync)
2090 need_sync = true;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002091
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002092 if (sp->role.word != role.word)
2093 continue;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002094
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002095 if (sp->unsync && kvm_sync_page_transient(vcpu, sp))
2096 break;
Xiao Guangronge02aa902010-05-15 18:52:34 +08002097
Takuya Yoshikawa98bba232015-11-26 21:14:34 +09002098 if (sp->unsync_children)
Avi Kivitya8eeb042010-05-10 12:34:53 +03002099 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
Xiao Guangronge02aa902010-05-15 18:52:34 +08002100
Xiao Guangronga30f47c2011-09-22 16:58:36 +08002101 __clear_sp_write_flooding_count(sp);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002102 trace_kvm_mmu_get_page(sp, false);
2103 return sp;
2104 }
Takuya Yoshikawa47005792015-11-20 17:46:29 +09002105
Avi Kivitydfc5aa02007-12-18 19:47:18 +02002106 ++vcpu->kvm->stat.mmu_cache_miss;
Takuya Yoshikawa47005792015-11-20 17:46:29 +09002107
2108 sp = kvm_mmu_alloc_page(vcpu, direct);
2109
Avi Kivity4db35312007-11-21 15:28:32 +02002110 sp->gfn = gfn;
2111 sp->role = role;
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002112 hlist_add_head(&sp->hash_link,
2113 &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002114 if (!direct) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002115 if (rmap_write_protect(vcpu, gfn))
Marcelo Tosattib1a36822008-12-01 22:32:03 -02002116 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08002117 if (level > PT_PAGE_TABLE_LEVEL && need_sync)
2118 kvm_sync_pages(vcpu, gfn);
2119
Paolo Bonzini3ed1a472015-05-19 16:29:22 +02002120 account_shadowed(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002121 }
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08002122 sp->mmu_valid_gen = vcpu->kvm->arch.mmu_valid_gen;
Takuya Yoshikawa77492662015-12-18 18:54:49 +09002123 clear_page(sp->spt);
Avi Kivityf691fe12009-07-06 15:58:14 +03002124 trace_kvm_mmu_get_page(sp, true);
Avi Kivity4db35312007-11-21 15:28:32 +02002125 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002126}
2127
Avi Kivity2d111232008-12-25 14:39:47 +02002128static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
2129 struct kvm_vcpu *vcpu, u64 addr)
2130{
2131 iterator->addr = addr;
2132 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
2133 iterator->level = vcpu->arch.mmu.shadow_root_level;
Joerg Roedel81407ca2010-09-10 17:31:00 +02002134
2135 if (iterator->level == PT64_ROOT_LEVEL &&
2136 vcpu->arch.mmu.root_level < PT64_ROOT_LEVEL &&
2137 !vcpu->arch.mmu.direct_map)
2138 --iterator->level;
2139
Avi Kivity2d111232008-12-25 14:39:47 +02002140 if (iterator->level == PT32E_ROOT_LEVEL) {
2141 iterator->shadow_addr
2142 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
2143 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
2144 --iterator->level;
2145 if (!iterator->shadow_addr)
2146 iterator->level = 0;
2147 }
2148}
2149
2150static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
2151{
2152 if (iterator->level < PT_PAGE_TABLE_LEVEL)
2153 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03002154
Avi Kivity2d111232008-12-25 14:39:47 +02002155 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
2156 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
2157 return true;
2158}
2159
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08002160static void __shadow_walk_next(struct kvm_shadow_walk_iterator *iterator,
2161 u64 spte)
Avi Kivity2d111232008-12-25 14:39:47 +02002162{
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08002163 if (is_last_spte(spte, iterator->level)) {
Xiao Guangrong052331b2011-07-12 03:21:17 +08002164 iterator->level = 0;
2165 return;
2166 }
2167
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08002168 iterator->shadow_addr = spte & PT64_BASE_ADDR_MASK;
Avi Kivity2d111232008-12-25 14:39:47 +02002169 --iterator->level;
2170}
2171
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08002172static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
2173{
2174 return __shadow_walk_next(iterator, *iterator->sptep);
2175}
2176
Takuya Yoshikawa98bba232015-11-26 21:14:34 +09002177static void link_shadow_page(struct kvm_vcpu *vcpu, u64 *sptep,
2178 struct kvm_mmu_page *sp)
Avi Kivity32ef26a2010-07-13 14:27:04 +03002179{
2180 u64 spte;
2181
Yang Zhang7a1638c2013-08-05 11:07:13 +03002182 BUILD_BUG_ON(VMX_EPT_READABLE_MASK != PT_PRESENT_MASK ||
2183 VMX_EPT_WRITABLE_MASK != PT_WRITABLE_MASK);
2184
Xiao Guangrong24db2732013-02-05 15:28:02 +08002185 spte = __pa(sp->spt) | PT_PRESENT_MASK | PT_WRITABLE_MASK |
Paolo Bonzini0e3d0642015-11-13 11:52:45 +01002186 shadow_user_mask | shadow_x_mask | shadow_accessed_mask;
Xiao Guangrong24db2732013-02-05 15:28:02 +08002187
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08002188 mmu_spte_set(sptep, spte);
Takuya Yoshikawa98bba232015-11-26 21:14:34 +09002189
2190 mmu_page_add_parent_pte(vcpu, sp, sptep);
2191
2192 if (sp->unsync_children || sp->unsync)
2193 mark_unsync(sptep);
Avi Kivity32ef26a2010-07-13 14:27:04 +03002194}
2195
Avi Kivitya357bd22010-07-13 14:27:07 +03002196static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
2197 unsigned direct_access)
2198{
2199 if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
2200 struct kvm_mmu_page *child;
2201
2202 /*
2203 * For the direct sp, if the guest pte's dirty bit
2204 * changed form clean to dirty, it will corrupt the
2205 * sp's access: allow writable in the read-only sp,
2206 * so we should update the spte at this point to get
2207 * a new sp with the correct access.
2208 */
2209 child = page_header(*sptep & PT64_BASE_ADDR_MASK);
2210 if (child->role.access == direct_access)
2211 return;
2212
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08002213 drop_parent_pte(child, sptep);
Avi Kivitya357bd22010-07-13 14:27:07 +03002214 kvm_flush_remote_tlbs(vcpu->kvm);
2215 }
2216}
2217
Xiao Guangrong505aef82011-09-22 16:56:06 +08002218static bool mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08002219 u64 *spte)
2220{
2221 u64 pte;
2222 struct kvm_mmu_page *child;
2223
2224 pte = *spte;
2225 if (is_shadow_present_pte(pte)) {
Xiao Guangrong505aef82011-09-22 16:56:06 +08002226 if (is_last_spte(pte, sp->role.level)) {
Xiao Guangrongc3707952011-07-12 03:28:04 +08002227 drop_spte(kvm, spte);
Xiao Guangrong505aef82011-09-22 16:56:06 +08002228 if (is_large_pte(pte))
2229 --kvm->stat.lpages;
2230 } else {
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08002231 child = page_header(pte & PT64_BASE_ADDR_MASK);
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08002232 drop_parent_pte(child, spte);
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08002233 }
Xiao Guangrong505aef82011-09-22 16:56:06 +08002234 return true;
2235 }
2236
2237 if (is_mmio_spte(pte))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08002238 mmu_spte_clear_no_track(spte);
Xiao Guangrongc3707952011-07-12 03:28:04 +08002239
Xiao Guangrong505aef82011-09-22 16:56:06 +08002240 return false;
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08002241}
2242
Avi Kivity90cb0522007-07-17 13:04:56 +03002243static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02002244 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08002245{
Avi Kivity697fe2e2007-01-05 16:36:46 -08002246 unsigned i;
Avi Kivity697fe2e2007-01-05 16:36:46 -08002247
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08002248 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2249 mmu_page_zap_pte(kvm, sp, sp->spt + i);
Avi Kivitya4360362007-01-05 16:36:45 -08002250}
2251
Avi Kivity31aa2b42008-07-11 17:59:46 +03002252static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08002253{
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09002254 u64 *sptep;
2255 struct rmap_iterator iter;
Avi Kivitya4360362007-01-05 16:36:45 -08002256
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09002257 while ((sptep = rmap_get_first(&sp->parent_ptes, &iter)))
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09002258 drop_parent_pte(sp, sptep);
Avi Kivity31aa2b42008-07-11 17:59:46 +03002259}
2260
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002261static int mmu_zap_unsync_children(struct kvm *kvm,
Xiao Guangrong77758342010-06-04 21:53:54 +08002262 struct kvm_mmu_page *parent,
2263 struct list_head *invalid_list)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002264{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002265 int i, zapped = 0;
2266 struct mmu_page_path parents;
2267 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002268
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002269 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002270 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002271
2272 kvm_mmu_pages_init(parent, &parents, &pages);
2273 while (mmu_unsync_walk(parent, &pages)) {
2274 struct kvm_mmu_page *sp;
2275
2276 for_each_sp(pages, sp, parents, i) {
Xiao Guangrong77758342010-06-04 21:53:54 +08002277 kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002278 mmu_pages_clear_parents(&parents);
Xiao Guangrong77662e02010-04-16 16:34:42 +08002279 zapped++;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002280 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002281 kvm_mmu_pages_init(parent, &parents, &pages);
2282 }
2283
2284 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002285}
2286
Xiao Guangrong77758342010-06-04 21:53:54 +08002287static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
2288 struct list_head *invalid_list)
Avi Kivity31aa2b42008-07-11 17:59:46 +03002289{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002290 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03002291
Xiao Guangrong77758342010-06-04 21:53:54 +08002292 trace_kvm_mmu_prepare_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03002293 ++kvm->stat.mmu_shadow_zapped;
Xiao Guangrong77758342010-06-04 21:53:54 +08002294 ret = mmu_zap_unsync_children(kvm, sp, invalid_list);
Avi Kivity4db35312007-11-21 15:28:32 +02002295 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03002296 kvm_mmu_unlink_parents(kvm, sp);
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08002297
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002298 if (!sp->role.invalid && !sp->role.direct)
Paolo Bonzini3ed1a472015-05-19 16:29:22 +02002299 unaccount_shadowed(kvm, sp);
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08002300
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002301 if (sp->unsync)
2302 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02002303 if (!sp->root_count) {
Gui Jianfeng54a4f022010-05-05 09:03:49 +08002304 /* Count self */
2305 ret++;
Xiao Guangrong77758342010-06-04 21:53:54 +08002306 list_move(&sp->link, invalid_list);
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08002307 kvm_mod_used_mmu_pages(kvm, -1);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002308 } else {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03002309 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Gleb Natapov05988d72013-05-31 08:36:30 +08002310
2311 /*
2312 * The obsolete pages can not be used on any vcpus.
2313 * See the comments in kvm_mmu_invalidate_zap_all_pages().
2314 */
2315 if (!sp->role.invalid && !is_obsolete_sp(kvm, sp))
2316 kvm_reload_remote_mmus(kvm);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002317 }
Xiao Guangrong77758342010-06-04 21:53:54 +08002318
2319 sp->role.invalid = 1;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002320 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08002321}
2322
Xiao Guangrong77758342010-06-04 21:53:54 +08002323static void kvm_mmu_commit_zap_page(struct kvm *kvm,
2324 struct list_head *invalid_list)
2325{
Takuya Yoshikawa945315b2013-03-06 16:05:52 +09002326 struct kvm_mmu_page *sp, *nsp;
Xiao Guangrong77758342010-06-04 21:53:54 +08002327
2328 if (list_empty(invalid_list))
2329 return;
2330
Avi Kivityc1427862012-05-14 15:44:06 +03002331 /*
2332 * wmb: make sure everyone sees our modifications to the page tables
2333 * rmb: make sure we see changes to vcpu->mode
2334 */
2335 smp_mb();
2336
2337 /*
2338 * Wait for all vcpus to exit guest mode and/or lockless shadow
2339 * page table walks.
2340 */
Xiao Guangrong77758342010-06-04 21:53:54 +08002341 kvm_flush_remote_tlbs(kvm);
2342
Takuya Yoshikawa945315b2013-03-06 16:05:52 +09002343 list_for_each_entry_safe(sp, nsp, invalid_list, link) {
Xiao Guangrong77758342010-06-04 21:53:54 +08002344 WARN_ON(!sp->role.invalid || sp->root_count);
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08002345 kvm_mmu_free_page(sp);
Takuya Yoshikawa945315b2013-03-06 16:05:52 +09002346 }
Xiao Guangrong77758342010-06-04 21:53:54 +08002347}
2348
Takuya Yoshikawa5da59602013-03-06 16:06:58 +09002349static bool prepare_zap_oldest_mmu_page(struct kvm *kvm,
2350 struct list_head *invalid_list)
2351{
2352 struct kvm_mmu_page *sp;
2353
2354 if (list_empty(&kvm->arch.active_mmu_pages))
2355 return false;
2356
2357 sp = list_entry(kvm->arch.active_mmu_pages.prev,
2358 struct kvm_mmu_page, link);
2359 kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
2360
2361 return true;
2362}
2363
Izik Eidus82ce2c92007-10-02 18:52:55 +02002364/*
2365 * Changing the number of mmu pages allocated to the vm
Dave Hansen49d5ca22010-08-19 18:11:28 -07002366 * Note: if goal_nr_mmu_pages is too small, you will get dead lock
Izik Eidus82ce2c92007-10-02 18:52:55 +02002367 */
Dave Hansen49d5ca22010-08-19 18:11:28 -07002368void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages)
Izik Eidus82ce2c92007-10-02 18:52:55 +02002369{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002370 LIST_HEAD(invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02002371
Takuya Yoshikawab34cb592013-01-08 19:46:07 +09002372 spin_lock(&kvm->mmu_lock);
2373
Dave Hansen49d5ca22010-08-19 18:11:28 -07002374 if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) {
Takuya Yoshikawa5da59602013-03-06 16:06:58 +09002375 /* Need to free some mmu pages to achieve the goal. */
2376 while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages)
2377 if (!prepare_zap_oldest_mmu_page(kvm, &invalid_list))
2378 break;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002379
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08002380 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Dave Hansen49d5ca22010-08-19 18:11:28 -07002381 goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002382 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02002383
Dave Hansen49d5ca22010-08-19 18:11:28 -07002384 kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages;
Takuya Yoshikawab34cb592013-01-08 19:46:07 +09002385
2386 spin_unlock(&kvm->mmu_lock);
Izik Eidus82ce2c92007-10-02 18:52:55 +02002387}
2388
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08002389int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08002390{
Avi Kivity4db35312007-11-21 15:28:32 +02002391 struct kvm_mmu_page *sp;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002392 LIST_HEAD(invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08002393 int r;
2394
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002395 pgprintk("%s: looking for gfn %llx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08002396 r = 0;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08002397 spin_lock(&kvm->mmu_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002398 for_each_gfn_indirect_valid_sp(kvm, sp, gfn) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002399 pgprintk("%s: gfn %llx role %x\n", __func__, gfn,
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002400 sp->role.word);
2401 r = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002402 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002403 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002404 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08002405 spin_unlock(&kvm->mmu_lock);
2406
Avi Kivitya4360362007-01-05 16:36:45 -08002407 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002408}
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08002409EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002410
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002411static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002412{
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08002413 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002414 ++vcpu->kvm->stat.mmu_unsync;
2415 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02002416
Xiao Guangrong6b184932010-04-16 21:29:17 +08002417 kvm_mmu_mark_parents_unsync(sp);
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002418}
2419
2420static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
2421{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002422 struct kvm_mmu_page *s;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002423
Sasha Levinb67bfe02013-02-27 17:06:00 -08002424 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002425 if (s->unsync)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002426 continue;
2427 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
2428 __kvm_unsync_page(vcpu, s);
2429 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002430}
2431
2432static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
2433 bool can_unsync)
2434{
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002435 struct kvm_mmu_page *s;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002436 bool need_unsync = false;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002437
Sasha Levinb67bfe02013-02-27 17:06:00 -08002438 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08002439 if (!can_unsync)
2440 return 1;
2441
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002442 if (s->role.level != PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002443 return 1;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002444
Gleb Natapov9bb4f6b2013-01-30 16:45:01 +02002445 if (!s->unsync)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002446 need_unsync = true;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002447 }
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002448 if (need_unsync)
2449 kvm_unsync_pages(vcpu, gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002450 return 0;
2451}
2452
Dan Williamsba049e92016-01-15 16:56:11 -08002453static bool kvm_is_mmio_pfn(kvm_pfn_t pfn)
Paolo Bonzinid1fe9212015-07-07 15:03:18 +02002454{
2455 if (pfn_valid(pfn))
2456 return !is_zero_pfn(pfn) && PageReserved(pfn_to_page(pfn));
2457
2458 return true;
2459}
2460
Avi Kivityd555c332009-06-10 14:24:23 +03002461static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Xiao Guangrongc22885052013-01-08 14:36:04 +08002462 unsigned pte_access, int level,
Dan Williamsba049e92016-01-15 16:56:11 -08002463 gfn_t gfn, kvm_pfn_t pfn, bool speculative,
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08002464 bool can_unsync, bool host_writable)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002465{
Xiao Guangrong6e7d0352012-06-20 15:58:33 +08002466 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002467 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08002468
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002469 if (set_mmio_spte(vcpu, sptep, gfn, pfn, pte_access))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08002470 return 0;
2471
Marcelo Tosatti982c2562010-10-22 14:18:16 -02002472 spte = PT_PRESENT_MASK;
Avi Kivity947da532008-03-18 11:05:52 +02002473 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03002474 spte |= shadow_accessed_mask;
Xiao Guangrong640d9b02011-07-12 03:24:39 +08002475
Sheng Yang7b523452008-04-25 21:13:50 +08002476 if (pte_access & ACC_EXEC_MASK)
2477 spte |= shadow_x_mask;
2478 else
2479 spte |= shadow_nx_mask;
Xiao Guangrong49fde342012-06-20 15:58:58 +08002480
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002481 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08002482 spte |= shadow_user_mask;
Xiao Guangrong49fde342012-06-20 15:58:58 +08002483
Joerg Roedel852e3c12009-07-27 16:30:44 +02002484 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002485 spte |= PT_PAGE_SIZE_MASK;
Avi Kivityb0bc3ee2010-09-13 16:45:28 +02002486 if (tdp_enabled)
Sheng Yang4b12f0d2009-04-27 20:35:42 +08002487 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
Paolo Bonzinid1fe9212015-07-07 15:03:18 +02002488 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002489
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08002490 if (host_writable)
Izik Eidus14032832009-09-23 21:47:17 +03002491 spte |= SPTE_HOST_WRITEABLE;
Xiao Guangrongf8e453b2010-12-23 16:09:29 +08002492 else
2493 pte_access &= ~ACC_WRITE_MASK;
Izik Eidus14032832009-09-23 21:47:17 +03002494
Anthony Liguori35149e22008-04-02 14:46:56 -05002495 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002496
Xiao Guangrongc22885052013-01-08 14:36:04 +08002497 if (pte_access & ACC_WRITE_MASK) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002498
Xiao Guangrongc2193462012-12-04 07:17:11 +08002499 /*
Xiao Guangrong7751bab2013-01-08 14:36:51 +08002500 * Other vcpu creates new sp in the window between
2501 * mapping_level() and acquiring mmu-lock. We can
2502 * allow guest to retry the access, the mapping can
2503 * be fixed if guest refault.
Xiao Guangrongc2193462012-12-04 07:17:11 +08002504 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02002505 if (level > PT_PAGE_TABLE_LEVEL &&
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002506 has_wrprotected_page(vcpu, gfn, level))
Avi Kivitybe38d272010-06-06 14:31:27 +03002507 goto done;
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002508
Xiao Guangrong49fde342012-06-20 15:58:58 +08002509 spte |= PT_WRITABLE_MASK | SPTE_MMU_WRITEABLE;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002510
Marcelo Tosattiecc55892008-11-25 15:58:07 +01002511 /*
2512 * Optimization: for pte sync, if spte was writable the hash
2513 * lookup is unnecessary (and expensive). Write protection
2514 * is responsibility of mmu_get_page / kvm_sync_page.
2515 * Same reasoning can be applied to dirty page accounting.
2516 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002517 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01002518 goto set_pte;
2519
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002520 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002521 pgprintk("%s: found shadow page for %llx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002522 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002523 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002524 pte_access &= ~ACC_WRITE_MASK;
Xiao Guangrong49fde342012-06-20 15:58:58 +08002525 spte &= ~(PT_WRITABLE_MASK | SPTE_MMU_WRITEABLE);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002526 }
2527 }
2528
Kai Huang9b51a632015-01-28 10:54:25 +08002529 if (pte_access & ACC_WRITE_MASK) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002530 kvm_vcpu_mark_page_dirty(vcpu, gfn);
Kai Huang9b51a632015-01-28 10:54:25 +08002531 spte |= shadow_dirty_mask;
2532 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002533
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002534set_pte:
Xiao Guangrong6e7d0352012-06-20 15:58:33 +08002535 if (mmu_spte_update(sptep, spte))
Xiao Guangrongb330aa02010-11-19 17:02:35 +08002536 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivitybe38d272010-06-06 14:31:27 +03002537done:
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002538 return ret;
2539}
2540
Takuya Yoshikawa029499b2015-11-20 17:44:05 +09002541static bool mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep, unsigned pte_access,
Dan Williamsba049e92016-01-15 16:56:11 -08002542 int write_fault, int level, gfn_t gfn, kvm_pfn_t pfn,
Takuya Yoshikawa029499b2015-11-20 17:44:05 +09002543 bool speculative, bool host_writable)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002544{
2545 int was_rmapped = 0;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002546 int rmap_count;
Takuya Yoshikawa029499b2015-11-20 17:44:05 +09002547 bool emulate = false;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002548
Xiao Guangrongf7616202013-02-05 15:27:27 +08002549 pgprintk("%s: spte %llx write_fault %d gfn %llx\n", __func__,
2550 *sptep, write_fault, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002551
Takuya Yoshikawaafd28fe2015-11-20 17:44:55 +09002552 if (is_shadow_present_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002553 /*
2554 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
2555 * the parent of the now unreachable PTE.
2556 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02002557 if (level > PT_PAGE_TABLE_LEVEL &&
2558 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002559 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03002560 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002561
2562 child = page_header(pte & PT64_BASE_ADDR_MASK);
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08002563 drop_parent_pte(child, sptep);
Marcelo Tosatti3be22642010-05-28 09:44:59 -03002564 kvm_flush_remote_tlbs(vcpu->kvm);
Avi Kivityd555c332009-06-10 14:24:23 +03002565 } else if (pfn != spte_to_pfn(*sptep)) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002566 pgprintk("hfn old %llx new %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002567 spte_to_pfn(*sptep), pfn);
Xiao Guangrongc3707952011-07-12 03:28:04 +08002568 drop_spte(vcpu->kvm, sptep);
Xiao Guangrong91546352010-06-30 16:04:06 +08002569 kvm_flush_remote_tlbs(vcpu->kvm);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01002570 } else
2571 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002572 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02002573
Xiao Guangrongc22885052013-01-08 14:36:04 +08002574 if (set_spte(vcpu, sptep, pte_access, level, gfn, pfn, speculative,
2575 true, host_writable)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002576 if (write_fault)
Takuya Yoshikawa029499b2015-11-20 17:44:05 +09002577 emulate = true;
Liang Chen77c39132014-09-18 12:38:37 -04002578 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03002579 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002580
Takuya Yoshikawa029499b2015-11-20 17:44:05 +09002581 if (unlikely(is_mmio_spte(*sptep)))
2582 emulate = true;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08002583
Avi Kivityd555c332009-06-10 14:24:23 +03002584 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002585 pgprintk("instantiating %s PTE (%s) at %llx (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002586 is_large_pte(*sptep)? "2MB" : "4kB",
Joerg Roedela205bc12009-07-09 16:36:01 +02002587 *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
2588 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03002589 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002590 ++vcpu->kvm->stat.lpages;
2591
Xiao Guangrongffb61bb2011-07-12 03:22:01 +08002592 if (is_shadow_present_pte(*sptep)) {
Xiao Guangrongffb61bb2011-07-12 03:22:01 +08002593 if (!was_rmapped) {
2594 rmap_count = rmap_add(vcpu, sptep, gfn);
2595 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
2596 rmap_recycle(vcpu, sptep, gfn);
2597 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002598 }
Xiao Guangrongcb9aaa32012-08-03 15:42:10 +08002599
Xiao Guangrongf3ac1a42012-10-16 20:07:03 +08002600 kvm_release_pfn_clean(pfn);
Takuya Yoshikawa029499b2015-11-20 17:44:05 +09002601
2602 return emulate;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002603}
2604
Dan Williamsba049e92016-01-15 16:56:11 -08002605static kvm_pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn,
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002606 bool no_dirty_log)
2607{
2608 struct kvm_memory_slot *slot;
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002609
Xiao Guangrong5d163b12011-03-09 15:43:00 +08002610 slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, no_dirty_log);
Xiao Guangrong903816f2012-07-17 21:54:11 +08002611 if (!slot)
Xiao Guangrong6c8ee572012-08-03 15:37:54 +08002612 return KVM_PFN_ERR_FAULT;
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002613
Xiao Guangrong037d92d2012-08-21 10:59:12 +08002614 return gfn_to_pfn_memslot_atomic(slot, gfn);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002615}
2616
2617static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,
2618 struct kvm_mmu_page *sp,
2619 u64 *start, u64 *end)
2620{
2621 struct page *pages[PTE_PREFETCH_NUM];
Paolo Bonzinid9ef13c2015-05-19 16:01:50 +02002622 struct kvm_memory_slot *slot;
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002623 unsigned access = sp->role.access;
2624 int i, ret;
2625 gfn_t gfn;
2626
2627 gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt);
Paolo Bonzinid9ef13c2015-05-19 16:01:50 +02002628 slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, access & ACC_WRITE_MASK);
2629 if (!slot)
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002630 return -1;
2631
Paolo Bonzinid9ef13c2015-05-19 16:01:50 +02002632 ret = gfn_to_page_many_atomic(slot, gfn, pages, end - start);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002633 if (ret <= 0)
2634 return -1;
2635
2636 for (i = 0; i < ret; i++, gfn++, start++)
Takuya Yoshikawa029499b2015-11-20 17:44:05 +09002637 mmu_set_spte(vcpu, start, access, 0, sp->role.level, gfn,
2638 page_to_pfn(pages[i]), true, true);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002639
2640 return 0;
2641}
2642
2643static void __direct_pte_prefetch(struct kvm_vcpu *vcpu,
2644 struct kvm_mmu_page *sp, u64 *sptep)
2645{
2646 u64 *spte, *start = NULL;
2647 int i;
2648
2649 WARN_ON(!sp->role.direct);
2650
2651 i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
2652 spte = sp->spt + i;
2653
2654 for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
Xiao Guangrongc3707952011-07-12 03:28:04 +08002655 if (is_shadow_present_pte(*spte) || spte == sptep) {
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002656 if (!start)
2657 continue;
2658 if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0)
2659 break;
2660 start = NULL;
2661 } else if (!start)
2662 start = spte;
2663 }
2664}
2665
2666static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)
2667{
2668 struct kvm_mmu_page *sp;
2669
2670 /*
2671 * Since it's no accessed bit on EPT, it's no way to
2672 * distinguish between actually accessed translations
2673 * and prefetched, so disable pte prefetch if EPT is
2674 * enabled.
2675 */
2676 if (!shadow_accessed_mask)
2677 return;
2678
2679 sp = page_header(__pa(sptep));
2680 if (sp->role.level > PT_PAGE_TABLE_LEVEL)
2681 return;
2682
2683 __direct_pte_prefetch(vcpu, sp, sptep);
2684}
2685
Takuya Yoshikawa7ee0e5b2015-11-20 17:42:23 +09002686static int __direct_map(struct kvm_vcpu *vcpu, int write, int map_writable,
Dan Williamsba049e92016-01-15 16:56:11 -08002687 int level, gfn_t gfn, kvm_pfn_t pfn, bool prefault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002688{
Avi Kivity9f652d212008-12-25 14:54:25 +02002689 struct kvm_shadow_walk_iterator iterator;
2690 struct kvm_mmu_page *sp;
Xiao Guangrongb90a0e62011-07-12 03:25:56 +08002691 int emulate = 0;
Avi Kivity9f652d212008-12-25 14:54:25 +02002692 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002693
Marcelo Tosatti989c6b32013-12-19 15:28:51 -02002694 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2695 return 0;
2696
Avi Kivity9f652d212008-12-25 14:54:25 +02002697 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02002698 if (iterator.level == level) {
Takuya Yoshikawa029499b2015-11-20 17:44:05 +09002699 emulate = mmu_set_spte(vcpu, iterator.sptep, ACC_ALL,
2700 write, level, gfn, pfn, prefault,
2701 map_writable);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002702 direct_pte_prefetch(vcpu, iterator.sptep);
Avi Kivity9f652d212008-12-25 14:54:25 +02002703 ++vcpu->stat.pf_fixed;
2704 break;
2705 }
2706
Marcelo Tosatti404381c2014-02-24 13:59:32 -03002707 drop_large_spte(vcpu, iterator.sptep);
Xiao Guangrongc3707952011-07-12 03:28:04 +08002708 if (!is_shadow_present_pte(*iterator.sptep)) {
Lai Jiangshanc9fa0b32010-05-26 16:48:25 +08002709 u64 base_addr = iterator.addr;
2710
2711 base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
2712 pseudo_gfn = base_addr >> PAGE_SHIFT;
Avi Kivity9f652d212008-12-25 14:54:25 +02002713 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
Takuya Yoshikawabb11c6c2015-11-26 21:16:35 +09002714 iterator.level - 1, 1, ACC_ALL);
Avi Kivity9f652d212008-12-25 14:54:25 +02002715
Takuya Yoshikawa98bba232015-11-26 21:14:34 +09002716 link_shadow_page(vcpu, iterator.sptep, sp);
Avi Kivity9f652d212008-12-25 14:54:25 +02002717 }
2718 }
Xiao Guangrongb90a0e62011-07-12 03:25:56 +08002719 return emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002720}
2721
Huang Ying77db5cb2010-10-08 16:24:15 +08002722static void kvm_send_hwpoison_signal(unsigned long address, struct task_struct *tsk)
Huang Yingbf998152010-05-31 14:28:19 +08002723{
Huang Ying77db5cb2010-10-08 16:24:15 +08002724 siginfo_t info;
Huang Yingbf998152010-05-31 14:28:19 +08002725
Huang Ying77db5cb2010-10-08 16:24:15 +08002726 info.si_signo = SIGBUS;
2727 info.si_errno = 0;
2728 info.si_code = BUS_MCEERR_AR;
2729 info.si_addr = (void __user *)address;
2730 info.si_addr_lsb = PAGE_SHIFT;
2731
2732 send_sig_info(SIGBUS, &info, tsk);
Huang Yingbf998152010-05-31 14:28:19 +08002733}
2734
Dan Williamsba049e92016-01-15 16:56:11 -08002735static int kvm_handle_bad_page(struct kvm_vcpu *vcpu, gfn_t gfn, kvm_pfn_t pfn)
Huang Yingbf998152010-05-31 14:28:19 +08002736{
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08002737 /*
2738 * Do not cache the mmio info caused by writing the readonly gfn
2739 * into the spte otherwise read access on readonly gfn also can
2740 * caused mmio page fault and treat it as mmio access.
2741 * Return 1 to tell kvm to emulate it.
2742 */
2743 if (pfn == KVM_PFN_ERR_RO_FAULT)
2744 return 1;
2745
Xiao Guangronge6c15022012-08-03 15:38:36 +08002746 if (pfn == KVM_PFN_ERR_HWPOISON) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002747 kvm_send_hwpoison_signal(kvm_vcpu_gfn_to_hva(vcpu, gfn), current);
Huang Yingbf998152010-05-31 14:28:19 +08002748 return 0;
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002749 }
Gleb Natapovedba23e2010-07-07 20:16:45 +03002750
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002751 return -EFAULT;
Huang Yingbf998152010-05-31 14:28:19 +08002752}
2753
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002754static void transparent_hugepage_adjust(struct kvm_vcpu *vcpu,
Dan Williamsba049e92016-01-15 16:56:11 -08002755 gfn_t *gfnp, kvm_pfn_t *pfnp,
2756 int *levelp)
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002757{
Dan Williamsba049e92016-01-15 16:56:11 -08002758 kvm_pfn_t pfn = *pfnp;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002759 gfn_t gfn = *gfnp;
2760 int level = *levelp;
2761
2762 /*
2763 * Check if it's a transparent hugepage. If this would be an
2764 * hugetlbfs page, level wouldn't be set to
2765 * PT_PAGE_TABLE_LEVEL and there would be no adjustment done
2766 * here.
2767 */
Ard Biesheuvelbf4bea82014-11-10 08:33:56 +00002768 if (!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn) &&
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002769 level == PT_PAGE_TABLE_LEVEL &&
2770 PageTransCompound(pfn_to_page(pfn)) &&
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002771 !has_wrprotected_page(vcpu, gfn, PT_DIRECTORY_LEVEL)) {
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002772 unsigned long mask;
2773 /*
2774 * mmu_notifier_retry was successful and we hold the
2775 * mmu_lock here, so the pmd can't become splitting
2776 * from under us, and in turn
2777 * __split_huge_page_refcount() can't run from under
2778 * us and we can safely transfer the refcount from
2779 * PG_tail to PG_head as we switch the pfn to tail to
2780 * head.
2781 */
2782 *levelp = level = PT_DIRECTORY_LEVEL;
2783 mask = KVM_PAGES_PER_HPAGE(level) - 1;
2784 VM_BUG_ON((gfn & mask) != (pfn & mask));
2785 if (pfn & mask) {
2786 gfn &= ~mask;
2787 *gfnp = gfn;
2788 kvm_release_pfn_clean(pfn);
2789 pfn &= ~mask;
Xiao Guangrongc3586662012-05-28 14:10:43 +08002790 kvm_get_pfn(pfn);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002791 *pfnp = pfn;
2792 }
2793 }
2794}
2795
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002796static bool handle_abnormal_pfn(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn,
Dan Williamsba049e92016-01-15 16:56:11 -08002797 kvm_pfn_t pfn, unsigned access, int *ret_val)
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002798{
2799 bool ret = true;
2800
2801 /* The pfn is invalid, report the error! */
Xiao Guangrong81c52c52012-10-16 20:10:59 +08002802 if (unlikely(is_error_pfn(pfn))) {
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002803 *ret_val = kvm_handle_bad_page(vcpu, gfn, pfn);
2804 goto exit;
2805 }
2806
Xiao Guangrongce88dec2011-07-12 03:33:44 +08002807 if (unlikely(is_noslot_pfn(pfn)))
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002808 vcpu_cache_mmio_info(vcpu, gva, gfn, access);
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002809
2810 ret = false;
2811exit:
2812 return ret;
2813}
2814
Xiao Guangronge5552fd2013-07-30 21:01:59 +08002815static bool page_fault_can_be_fast(u32 error_code)
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002816{
2817 /*
Xiao Guangrong1c118b82013-07-18 12:52:37 +08002818 * Do not fix the mmio spte with invalid generation number which
2819 * need to be updated by slow page fault path.
2820 */
2821 if (unlikely(error_code & PFERR_RSVD_MASK))
2822 return false;
2823
2824 /*
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002825 * #PF can be fast only if the shadow page table is present and it
2826 * is caused by write-protect, that means we just need change the
2827 * W bit of the spte which can be done out of mmu-lock.
2828 */
2829 if (!(error_code & PFERR_PRESENT_MASK) ||
2830 !(error_code & PFERR_WRITE_MASK))
2831 return false;
2832
2833 return true;
2834}
2835
2836static bool
Xiao Guangrong92a476c2014-04-17 17:06:13 +08002837fast_pf_fix_direct_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
2838 u64 *sptep, u64 spte)
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002839{
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002840 gfn_t gfn;
2841
2842 WARN_ON(!sp->role.direct);
2843
2844 /*
2845 * The gfn of direct spte is stable since it is calculated
2846 * by sp->gfn.
2847 */
2848 gfn = kvm_mmu_page_get_gfn(sp, sptep - sp->spt);
2849
Kai Huang9b51a632015-01-28 10:54:25 +08002850 /*
2851 * Theoretically we could also set dirty bit (and flush TLB) here in
2852 * order to eliminate unnecessary PML logging. See comments in
2853 * set_spte. But fast_page_fault is very unlikely to happen with PML
2854 * enabled, so we do not do this. This might result in the same GPA
2855 * to be logged in PML buffer again when the write really happens, and
2856 * eventually to be called by mark_page_dirty twice. But it's also no
2857 * harm. This also avoids the TLB flush needed after setting dirty bit
2858 * so non-PML cases won't be impacted.
2859 *
2860 * Compare with set_spte where instead shadow_dirty_mask is set.
2861 */
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002862 if (cmpxchg64(sptep, spte, spte | PT_WRITABLE_MASK) == spte)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002863 kvm_vcpu_mark_page_dirty(vcpu, gfn);
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002864
2865 return true;
2866}
2867
2868/*
2869 * Return value:
2870 * - true: let the vcpu to access on the same address again.
2871 * - false: let the real page fault path to fix it.
2872 */
2873static bool fast_page_fault(struct kvm_vcpu *vcpu, gva_t gva, int level,
2874 u32 error_code)
2875{
2876 struct kvm_shadow_walk_iterator iterator;
Xiao Guangrong92a476c2014-04-17 17:06:13 +08002877 struct kvm_mmu_page *sp;
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002878 bool ret = false;
2879 u64 spte = 0ull;
2880
Marcelo Tosatti37f6a4e2014-01-03 17:09:32 -02002881 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2882 return false;
2883
Xiao Guangronge5552fd2013-07-30 21:01:59 +08002884 if (!page_fault_can_be_fast(error_code))
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002885 return false;
2886
2887 walk_shadow_page_lockless_begin(vcpu);
2888 for_each_shadow_entry_lockless(vcpu, gva, iterator, spte)
2889 if (!is_shadow_present_pte(spte) || iterator.level < level)
2890 break;
2891
2892 /*
2893 * If the mapping has been changed, let the vcpu fault on the
2894 * same address again.
2895 */
Takuya Yoshikawaafd28fe2015-11-20 17:44:55 +09002896 if (!is_shadow_present_pte(spte)) {
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002897 ret = true;
2898 goto exit;
2899 }
2900
Xiao Guangrong92a476c2014-04-17 17:06:13 +08002901 sp = page_header(__pa(iterator.sptep));
2902 if (!is_last_spte(spte, sp->role.level))
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002903 goto exit;
2904
2905 /*
2906 * Check if it is a spurious fault caused by TLB lazily flushed.
2907 *
2908 * Need not check the access of upper level table entries since
2909 * they are always ACC_ALL.
2910 */
2911 if (is_writable_pte(spte)) {
2912 ret = true;
2913 goto exit;
2914 }
2915
2916 /*
2917 * Currently, to simplify the code, only the spte write-protected
2918 * by dirty-log can be fast fixed.
2919 */
2920 if (!spte_is_locklessly_modifiable(spte))
2921 goto exit;
2922
2923 /*
Xiao Guangrongc126d942014-04-17 17:06:14 +08002924 * Do not fix write-permission on the large spte since we only dirty
2925 * the first page into the dirty-bitmap in fast_pf_fix_direct_spte()
2926 * that means other pages are missed if its slot is dirty-logged.
2927 *
2928 * Instead, we let the slow page fault path create a normal spte to
2929 * fix the access.
2930 *
2931 * See the comments in kvm_arch_commit_memory_region().
2932 */
2933 if (sp->role.level > PT_PAGE_TABLE_LEVEL)
2934 goto exit;
2935
2936 /*
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002937 * Currently, fast page fault only works for direct mapping since
2938 * the gfn is not stable for indirect shadow page.
2939 * See Documentation/virtual/kvm/locking.txt to get more detail.
2940 */
Xiao Guangrong92a476c2014-04-17 17:06:13 +08002941 ret = fast_pf_fix_direct_spte(vcpu, sp, iterator.sptep, spte);
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002942exit:
Xiao Guangronga72faf22012-06-20 15:59:41 +08002943 trace_fast_page_fault(vcpu, gva, error_code, iterator.sptep,
2944 spte, ret);
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002945 walk_shadow_page_lockless_end(vcpu);
2946
2947 return ret;
2948}
2949
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002950static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
Dan Williamsba049e92016-01-15 16:56:11 -08002951 gva_t gva, kvm_pfn_t *pfn, bool write, bool *writable);
Takuya Yoshikawa450e0b42013-03-29 14:05:26 +09002952static void make_mmu_pages_available(struct kvm_vcpu *vcpu);
Xiao Guangrong060c2ab2010-11-12 14:49:11 +08002953
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002954static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, u32 error_code,
2955 gfn_t gfn, bool prefault)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002956{
2957 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002958 int level;
Takuya Yoshikawafd136902015-10-16 17:06:02 +09002959 bool force_pt_level = false;
Dan Williamsba049e92016-01-15 16:56:11 -08002960 kvm_pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002961 unsigned long mmu_seq;
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002962 bool map_writable, write = error_code & PFERR_WRITE_MASK;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002963
Takuya Yoshikawafd136902015-10-16 17:06:02 +09002964 level = mapping_level(vcpu, gfn, &force_pt_level);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002965 if (likely(!force_pt_level)) {
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002966 /*
2967 * This path builds a PAE pagetable - so we can map
2968 * 2mb pages at maximum. Therefore check if the level
2969 * is larger than that.
2970 */
2971 if (level > PT_DIRECTORY_LEVEL)
2972 level = PT_DIRECTORY_LEVEL;
Joerg Roedel852e3c12009-07-27 16:30:44 +02002973
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002974 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Takuya Yoshikawafd136902015-10-16 17:06:02 +09002975 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002976
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002977 if (fast_page_fault(vcpu, v, level, error_code))
2978 return 0;
2979
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002980 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002981 smp_rmb();
Xiao Guangrong060c2ab2010-11-12 14:49:11 +08002982
Xiao Guangrong78b2c542010-12-07 10:48:06 +08002983 if (try_async_pf(vcpu, prefault, gfn, v, &pfn, write, &map_writable))
Xiao Guangrong060c2ab2010-11-12 14:49:11 +08002984 return 0;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002985
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002986 if (handle_abnormal_pfn(vcpu, v, gfn, pfn, ACC_ALL, &r))
2987 return r;
Avi Kivityd196e342008-01-24 11:44:11 +02002988
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002989 spin_lock(&vcpu->kvm->mmu_lock);
Christoffer Dall8ca40a72012-10-14 23:10:18 -04002990 if (mmu_notifier_retry(vcpu->kvm, mmu_seq))
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002991 goto out_unlock;
Takuya Yoshikawa450e0b42013-03-29 14:05:26 +09002992 make_mmu_pages_available(vcpu);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08002993 if (likely(!force_pt_level))
2994 transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
Takuya Yoshikawa7ee0e5b2015-11-20 17:42:23 +09002995 r = __direct_map(vcpu, write, map_writable, level, gfn, pfn, prefault);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002996 spin_unlock(&vcpu->kvm->mmu_lock);
2997
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002998 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002999
3000out_unlock:
3001 spin_unlock(&vcpu->kvm->mmu_lock);
3002 kvm_release_pfn_clean(pfn);
3003 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003004}
3005
3006
Avi Kivity17ac10a2007-01-05 16:36:40 -08003007static void mmu_free_roots(struct kvm_vcpu *vcpu)
3008{
3009 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02003010 struct kvm_mmu_page *sp;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003011 LIST_HEAD(invalid_list);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003012
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003013 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03003014 return;
Gleb Natapov35af5772013-05-16 11:55:51 +03003015
Joerg Roedel81407ca2010-09-10 17:31:00 +02003016 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL &&
3017 (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL ||
3018 vcpu->arch.mmu.direct_map)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003019 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003020
Gleb Natapov35af5772013-05-16 11:55:51 +03003021 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity4db35312007-11-21 15:28:32 +02003022 sp = page_header(root);
3023 --sp->root_count;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003024 if (!sp->root_count && sp->role.invalid) {
3025 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
3026 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
3027 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003028 spin_unlock(&vcpu->kvm->mmu_lock);
Gleb Natapov35af5772013-05-16 11:55:51 +03003029 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003030 return;
3031 }
Gleb Natapov35af5772013-05-16 11:55:51 +03003032
3033 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003034 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003035 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08003036
Avi Kivity417726a2007-04-12 17:35:58 +03003037 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03003038 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02003039 sp = page_header(root);
3040 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05003041 if (!sp->root_count && sp->role.invalid)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003042 kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
3043 &invalid_list);
Avi Kivity417726a2007-04-12 17:35:58 +03003044 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003045 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003046 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003047 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003048 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003049 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003050}
3051
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003052static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
3053{
3054 int ret = 0;
3055
3056 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03003057 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003058 ret = 1;
3059 }
3060
3061 return ret;
3062}
3063
Joerg Roedel651dd372010-09-10 17:30:59 +02003064static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu)
3065{
3066 struct kvm_mmu_page *sp;
Avi Kivity7ebaf152010-10-03 18:51:39 +02003067 unsigned i;
Joerg Roedel651dd372010-09-10 17:30:59 +02003068
3069 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3070 spin_lock(&vcpu->kvm->mmu_lock);
Takuya Yoshikawa450e0b42013-03-29 14:05:26 +09003071 make_mmu_pages_available(vcpu);
Takuya Yoshikawabb11c6c2015-11-26 21:16:35 +09003072 sp = kvm_mmu_get_page(vcpu, 0, 0, PT64_ROOT_LEVEL, 1, ACC_ALL);
Joerg Roedel651dd372010-09-10 17:30:59 +02003073 ++sp->root_count;
3074 spin_unlock(&vcpu->kvm->mmu_lock);
3075 vcpu->arch.mmu.root_hpa = __pa(sp->spt);
3076 } else if (vcpu->arch.mmu.shadow_root_level == PT32E_ROOT_LEVEL) {
3077 for (i = 0; i < 4; ++i) {
3078 hpa_t root = vcpu->arch.mmu.pae_root[i];
3079
Paolo Bonzinifa4a2c02013-10-02 16:56:16 +02003080 MMU_WARN_ON(VALID_PAGE(root));
Joerg Roedel651dd372010-09-10 17:30:59 +02003081 spin_lock(&vcpu->kvm->mmu_lock);
Takuya Yoshikawa450e0b42013-03-29 14:05:26 +09003082 make_mmu_pages_available(vcpu);
Avi Kivity649497d2010-12-28 12:09:07 +02003083 sp = kvm_mmu_get_page(vcpu, i << (30 - PAGE_SHIFT),
Takuya Yoshikawabb11c6c2015-11-26 21:16:35 +09003084 i << 30, PT32_ROOT_LEVEL, 1, ACC_ALL);
Joerg Roedel651dd372010-09-10 17:30:59 +02003085 root = __pa(sp->spt);
3086 ++sp->root_count;
3087 spin_unlock(&vcpu->kvm->mmu_lock);
3088 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Joerg Roedel651dd372010-09-10 17:30:59 +02003089 }
Xiao Guangrong62927572010-09-27 18:02:12 +08003090 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Joerg Roedel651dd372010-09-10 17:30:59 +02003091 } else
3092 BUG();
3093
3094 return 0;
3095}
3096
3097static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08003098{
Avi Kivity4db35312007-11-21 15:28:32 +02003099 struct kvm_mmu_page *sp;
Joerg Roedel81407ca2010-09-10 17:31:00 +02003100 u64 pdptr, pm_mask;
3101 gfn_t root_gfn;
3102 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -08003103
Joerg Roedel5777ed32010-09-10 17:30:42 +02003104 root_gfn = vcpu->arch.mmu.get_cr3(vcpu) >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003105
Joerg Roedel651dd372010-09-10 17:30:59 +02003106 if (mmu_check_root(vcpu, root_gfn))
3107 return 1;
3108
3109 /*
3110 * Do we shadow a long mode page table? If so we need to
3111 * write-protect the guests page table root.
3112 */
3113 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003114 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003115
Paolo Bonzinifa4a2c02013-10-02 16:56:16 +02003116 MMU_WARN_ON(VALID_PAGE(root));
Joerg Roedel651dd372010-09-10 17:30:59 +02003117
Avi Kivity8facbbf2010-05-04 12:58:32 +03003118 spin_lock(&vcpu->kvm->mmu_lock);
Takuya Yoshikawa450e0b42013-03-29 14:05:26 +09003119 make_mmu_pages_available(vcpu);
Joerg Roedel651dd372010-09-10 17:30:59 +02003120 sp = kvm_mmu_get_page(vcpu, root_gfn, 0, PT64_ROOT_LEVEL,
Takuya Yoshikawabb11c6c2015-11-26 21:16:35 +09003121 0, ACC_ALL);
Avi Kivity4db35312007-11-21 15:28:32 +02003122 root = __pa(sp->spt);
3123 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03003124 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003125 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003126 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003127 }
Joerg Roedelf87f9282010-09-02 17:29:45 +02003128
Joerg Roedel651dd372010-09-10 17:30:59 +02003129 /*
3130 * We shadow a 32 bit page table. This may be a legacy 2-level
Joerg Roedel81407ca2010-09-10 17:31:00 +02003131 * or a PAE 3-level page table. In either case we need to be aware that
3132 * the shadow page table may be a PAE or a long mode page table.
Joerg Roedel651dd372010-09-10 17:30:59 +02003133 */
Joerg Roedel81407ca2010-09-10 17:31:00 +02003134 pm_mask = PT_PRESENT_MASK;
3135 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL)
3136 pm_mask |= PT_ACCESSED_MASK | PT_WRITABLE_MASK | PT_USER_MASK;
3137
Avi Kivity17ac10a2007-01-05 16:36:40 -08003138 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003139 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08003140
Paolo Bonzinifa4a2c02013-10-02 16:56:16 +02003141 MMU_WARN_ON(VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003142 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivitye4e517b2011-07-28 11:36:17 +03003143 pdptr = vcpu->arch.mmu.get_pdptr(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03003144 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003145 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03003146 continue;
3147 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003148 root_gfn = pdptr >> PAGE_SHIFT;
Joerg Roedelf87f9282010-09-02 17:29:45 +02003149 if (mmu_check_root(vcpu, root_gfn))
3150 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07003151 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03003152 spin_lock(&vcpu->kvm->mmu_lock);
Takuya Yoshikawa450e0b42013-03-29 14:05:26 +09003153 make_mmu_pages_available(vcpu);
Takuya Yoshikawabb11c6c2015-11-26 21:16:35 +09003154 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30, PT32_ROOT_LEVEL,
3155 0, ACC_ALL);
Avi Kivity4db35312007-11-21 15:28:32 +02003156 root = __pa(sp->spt);
3157 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03003158 spin_unlock(&vcpu->kvm->mmu_lock);
3159
Joerg Roedel81407ca2010-09-10 17:31:00 +02003160 vcpu->arch.mmu.pae_root[i] = root | pm_mask;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003161 }
Xiao Guangrong62927572010-09-27 18:02:12 +08003162 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Joerg Roedel81407ca2010-09-10 17:31:00 +02003163
3164 /*
3165 * If we shadow a 32 bit page table with a long mode page
3166 * table we enter this path.
3167 */
3168 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3169 if (vcpu->arch.mmu.lm_root == NULL) {
3170 /*
3171 * The additional page necessary for this is only
3172 * allocated on demand.
3173 */
3174
3175 u64 *lm_root;
3176
3177 lm_root = (void*)get_zeroed_page(GFP_KERNEL);
3178 if (lm_root == NULL)
3179 return 1;
3180
3181 lm_root[0] = __pa(vcpu->arch.mmu.pae_root) | pm_mask;
3182
3183 vcpu->arch.mmu.lm_root = lm_root;
3184 }
3185
3186 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.lm_root);
3187 }
3188
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003189 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003190}
3191
Joerg Roedel651dd372010-09-10 17:30:59 +02003192static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
3193{
3194 if (vcpu->arch.mmu.direct_map)
3195 return mmu_alloc_direct_roots(vcpu);
3196 else
3197 return mmu_alloc_shadow_roots(vcpu);
3198}
3199
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003200static void mmu_sync_roots(struct kvm_vcpu *vcpu)
3201{
3202 int i;
3203 struct kvm_mmu_page *sp;
3204
Joerg Roedel81407ca2010-09-10 17:31:00 +02003205 if (vcpu->arch.mmu.direct_map)
3206 return;
3207
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003208 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3209 return;
Xiao Guangrong69030742010-09-27 18:09:29 +08003210
David Matlack56f17dd2014-08-18 15:46:07 -07003211 vcpu_clear_mmio_info(vcpu, MMIO_GVA_ANY);
Xiao Guangrong0375f7f2011-11-28 20:41:00 +08003212 kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC);
Joerg Roedel81407ca2010-09-10 17:31:00 +02003213 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003214 hpa_t root = vcpu->arch.mmu.root_hpa;
3215 sp = page_header(root);
3216 mmu_sync_children(vcpu, sp);
Xiao Guangrong0375f7f2011-11-28 20:41:00 +08003217 kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003218 return;
3219 }
3220 for (i = 0; i < 4; ++i) {
3221 hpa_t root = vcpu->arch.mmu.pae_root[i];
3222
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003223 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003224 root &= PT64_BASE_ADDR_MASK;
3225 sp = page_header(root);
3226 mmu_sync_children(vcpu, sp);
3227 }
3228 }
Xiao Guangrong0375f7f2011-11-28 20:41:00 +08003229 kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003230}
3231
3232void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
3233{
3234 spin_lock(&vcpu->kvm->mmu_lock);
3235 mmu_sync_roots(vcpu);
3236 spin_unlock(&vcpu->kvm->mmu_lock);
3237}
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03003238EXPORT_SYMBOL_GPL(kvm_mmu_sync_roots);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003239
Gleb Natapov1871c602010-02-10 14:21:32 +02003240static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
Avi Kivityab9ae312010-11-22 17:53:26 +02003241 u32 access, struct x86_exception *exception)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003242{
Avi Kivityab9ae312010-11-22 17:53:26 +02003243 if (exception)
3244 exception->error_code = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003245 return vaddr;
3246}
3247
Joerg Roedel6539e732010-09-10 17:30:50 +02003248static gpa_t nonpaging_gva_to_gpa_nested(struct kvm_vcpu *vcpu, gva_t vaddr,
Avi Kivityab9ae312010-11-22 17:53:26 +02003249 u32 access,
3250 struct x86_exception *exception)
Joerg Roedel6539e732010-09-10 17:30:50 +02003251{
Avi Kivityab9ae312010-11-22 17:53:26 +02003252 if (exception)
3253 exception->error_code = 0;
Paolo Bonzini54987b72014-09-02 13:23:06 +02003254 return vcpu->arch.nested_mmu.translate_gpa(vcpu, vaddr, access, exception);
Joerg Roedel6539e732010-09-10 17:30:50 +02003255}
3256
Xiao Guangrongd625b152015-08-05 12:04:25 +08003257static bool
3258__is_rsvd_bits_set(struct rsvd_bits_validate *rsvd_check, u64 pte, int level)
3259{
3260 int bit7 = (pte >> 7) & 1, low6 = pte & 0x3f;
3261
3262 return (pte & rsvd_check->rsvd_bits_mask[bit7][level-1]) |
3263 ((rsvd_check->bad_mt_xwr & (1ull << low6)) != 0);
3264}
3265
3266static bool is_rsvd_bits_set(struct kvm_mmu *mmu, u64 gpte, int level)
3267{
3268 return __is_rsvd_bits_set(&mmu->guest_rsvd_check, gpte, level);
3269}
3270
3271static bool is_shadow_zero_bits_set(struct kvm_mmu *mmu, u64 spte, int level)
3272{
3273 return __is_rsvd_bits_set(&mmu->shadow_zero_check, spte, level);
3274}
3275
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003276static bool quickly_check_mmio_pf(struct kvm_vcpu *vcpu, u64 addr, bool direct)
3277{
3278 if (direct)
3279 return vcpu_match_mmio_gpa(vcpu, addr);
3280
3281 return vcpu_match_mmio_gva(vcpu, addr);
3282}
3283
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003284/* return true if reserved bit is detected on spte. */
3285static bool
3286walk_shadow_page_get_mmio_spte(struct kvm_vcpu *vcpu, u64 addr, u64 *sptep)
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003287{
3288 struct kvm_shadow_walk_iterator iterator;
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003289 u64 sptes[PT64_ROOT_LEVEL], spte = 0ull;
3290 int root, leaf;
3291 bool reserved = false;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003292
Marcelo Tosatti37f6a4e2014-01-03 17:09:32 -02003293 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003294 goto exit;
Marcelo Tosatti37f6a4e2014-01-03 17:09:32 -02003295
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003296 walk_shadow_page_lockless_begin(vcpu);
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003297
Paolo Bonzini29ecd662015-09-06 16:24:50 +02003298 for (shadow_walk_init(&iterator, vcpu, addr),
3299 leaf = root = iterator.level;
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003300 shadow_walk_okay(&iterator);
3301 __shadow_walk_next(&iterator, spte)) {
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003302 spte = mmu_spte_get_lockless(iterator.sptep);
3303
3304 sptes[leaf - 1] = spte;
Paolo Bonzini29ecd662015-09-06 16:24:50 +02003305 leaf--;
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003306
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003307 if (!is_shadow_present_pte(spte))
3308 break;
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003309
3310 reserved |= is_shadow_zero_bits_set(&vcpu->arch.mmu, spte,
Paolo Bonzini58c95072015-09-22 10:15:59 +02003311 iterator.level);
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003312 }
3313
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003314 walk_shadow_page_lockless_end(vcpu);
3315
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003316 if (reserved) {
3317 pr_err("%s: detect reserved bits on spte, addr 0x%llx, dump hierarchy:\n",
3318 __func__, addr);
Paolo Bonzini29ecd662015-09-06 16:24:50 +02003319 while (root > leaf) {
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003320 pr_err("------ spte 0x%llx level %d.\n",
3321 sptes[root - 1], root);
3322 root--;
3323 }
3324 }
3325exit:
3326 *sptep = spte;
3327 return reserved;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003328}
3329
Paolo Bonzini450869d2015-11-04 13:41:21 +01003330int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr, bool direct)
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003331{
3332 u64 spte;
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003333 bool reserved;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003334
3335 if (quickly_check_mmio_pf(vcpu, addr, direct))
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08003336 return RET_MMIO_PF_EMULATE;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003337
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003338 reserved = walk_shadow_page_get_mmio_spte(vcpu, addr, &spte);
Paolo Bonzini450869d2015-11-04 13:41:21 +01003339 if (WARN_ON(reserved))
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003340 return RET_MMIO_PF_BUG;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003341
3342 if (is_mmio_spte(spte)) {
3343 gfn_t gfn = get_mmio_spte_gfn(spte);
3344 unsigned access = get_mmio_spte_access(spte);
3345
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003346 if (!check_mmio_spte(vcpu, spte))
Xiao Guangrongf8f55942013-06-07 16:51:26 +08003347 return RET_MMIO_PF_INVALID;
3348
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003349 if (direct)
3350 addr = 0;
Xiao Guangrong4f022642011-07-12 03:34:24 +08003351
3352 trace_handle_mmio_page_fault(addr, gfn, access);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003353 vcpu_cache_mmio_info(vcpu, addr, gfn, access);
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08003354 return RET_MMIO_PF_EMULATE;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003355 }
3356
3357 /*
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003358 * If the page table is zapped by other cpus, let CPU fault again on
3359 * the address.
3360 */
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08003361 return RET_MMIO_PF_RETRY;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003362}
Paolo Bonzini450869d2015-11-04 13:41:21 +01003363EXPORT_SYMBOL_GPL(handle_mmio_page_fault);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003364
Avi Kivity6aa8b732006-12-10 02:21:36 -08003365static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08003366 u32 error_code, bool prefault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003367{
Avi Kivitye8332402007-12-09 18:43:00 +02003368 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08003369 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003370
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003371 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003372
Xiao Guangrongf8f55942013-06-07 16:51:26 +08003373 if (unlikely(error_code & PFERR_RSVD_MASK)) {
Paolo Bonzini450869d2015-11-04 13:41:21 +01003374 r = handle_mmio_page_fault(vcpu, gva, true);
Xiao Guangrongf8f55942013-06-07 16:51:26 +08003375
3376 if (likely(r != RET_MMIO_PF_INVALID))
3377 return r;
3378 }
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003379
Avi Kivitye2dec932007-01-05 16:36:54 -08003380 r = mmu_topup_memory_caches(vcpu);
3381 if (r)
3382 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08003383
Paolo Bonzinifa4a2c02013-10-02 16:56:16 +02003384 MMU_WARN_ON(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003385
Avi Kivitye8332402007-12-09 18:43:00 +02003386 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003387
Avi Kivitye8332402007-12-09 18:43:00 +02003388 return nonpaging_map(vcpu, gva & PAGE_MASK,
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003389 error_code, gfn, prefault);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003390}
3391
Jan Kiszka7e1fbea2010-10-20 15:18:02 +02003392static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn)
Gleb Natapovaf585b92010-10-14 11:22:46 +02003393{
3394 struct kvm_arch_async_pf arch;
Xiao Guangrongfb67e142010-12-07 10:35:25 +08003395
Gleb Natapov7c907052010-10-14 11:22:53 +02003396 arch.token = (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id;
Gleb Natapovaf585b92010-10-14 11:22:46 +02003397 arch.gfn = gfn;
Xiao Guangrongc4806ac2010-11-12 14:49:55 +08003398 arch.direct_map = vcpu->arch.mmu.direct_map;
Xiao Guangrongfb67e142010-12-07 10:35:25 +08003399 arch.cr3 = vcpu->arch.mmu.get_cr3(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02003400
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003401 return kvm_setup_async_pf(vcpu, gva, kvm_vcpu_gfn_to_hva(vcpu, gfn), &arch);
Gleb Natapovaf585b92010-10-14 11:22:46 +02003402}
3403
3404static bool can_do_async_pf(struct kvm_vcpu *vcpu)
3405{
Paolo Bonzini35754c92015-07-29 12:05:37 +02003406 if (unlikely(!lapic_in_kernel(vcpu) ||
Gleb Natapovaf585b92010-10-14 11:22:46 +02003407 kvm_event_needs_reinjection(vcpu)))
3408 return false;
3409
3410 return kvm_x86_ops->interrupt_allowed(vcpu);
3411}
3412
Xiao Guangrong78b2c542010-12-07 10:48:06 +08003413static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
Dan Williamsba049e92016-01-15 16:56:11 -08003414 gva_t gva, kvm_pfn_t *pfn, bool write, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02003415{
Paolo Bonzini35204692015-04-02 11:20:48 +02003416 struct kvm_memory_slot *slot;
Gleb Natapovaf585b92010-10-14 11:22:46 +02003417 bool async;
3418
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003419 slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
Paolo Bonzini35204692015-04-02 11:20:48 +02003420 async = false;
3421 *pfn = __gfn_to_pfn_memslot(slot, gfn, false, &async, write, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02003422 if (!async)
3423 return false; /* *pfn has correct page already */
3424
Xiao Guangrong78b2c542010-12-07 10:48:06 +08003425 if (!prefault && can_do_async_pf(vcpu)) {
Xiao Guangrongc9b263d2010-11-01 16:58:43 +08003426 trace_kvm_try_async_get_page(gva, gfn);
Gleb Natapovaf585b92010-10-14 11:22:46 +02003427 if (kvm_find_async_pf_gfn(vcpu, gfn)) {
3428 trace_kvm_async_pf_doublefault(gva, gfn);
3429 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
3430 return true;
3431 } else if (kvm_arch_setup_async_pf(vcpu, gva, gfn))
3432 return true;
3433 }
3434
Paolo Bonzini35204692015-04-02 11:20:48 +02003435 *pfn = __gfn_to_pfn_memslot(slot, gfn, false, NULL, write, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02003436 return false;
3437}
3438
Xiao Guangrong6a39bbc2015-06-15 16:55:35 +08003439static bool
3440check_hugepage_cache_consistency(struct kvm_vcpu *vcpu, gfn_t gfn, int level)
3441{
3442 int page_num = KVM_PAGES_PER_HPAGE(level);
3443
3444 gfn &= ~(page_num - 1);
3445
3446 return kvm_mtrr_check_gfn_range_consistency(vcpu, gfn, page_num);
3447}
3448
Gleb Natapov56028d02010-10-17 18:13:42 +02003449static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, u32 error_code,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08003450 bool prefault)
Joerg Roedelfb72d162008-02-07 13:47:44 +01003451{
Dan Williamsba049e92016-01-15 16:56:11 -08003452 kvm_pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01003453 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02003454 int level;
Takuya Yoshikawacd1872f2015-10-16 17:04:13 +09003455 bool force_pt_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03003456 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02003457 unsigned long mmu_seq;
Marcelo Tosatti612819c2010-10-22 14:18:18 -02003458 int write = error_code & PFERR_WRITE_MASK;
3459 bool map_writable;
Joerg Roedelfb72d162008-02-07 13:47:44 +01003460
Paolo Bonzinifa4a2c02013-10-02 16:56:16 +02003461 MMU_WARN_ON(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Joerg Roedelfb72d162008-02-07 13:47:44 +01003462
Xiao Guangrongf8f55942013-06-07 16:51:26 +08003463 if (unlikely(error_code & PFERR_RSVD_MASK)) {
Paolo Bonzini450869d2015-11-04 13:41:21 +01003464 r = handle_mmio_page_fault(vcpu, gpa, true);
Xiao Guangrongf8f55942013-06-07 16:51:26 +08003465
3466 if (likely(r != RET_MMIO_PF_INVALID))
3467 return r;
3468 }
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003469
Joerg Roedelfb72d162008-02-07 13:47:44 +01003470 r = mmu_topup_memory_caches(vcpu);
3471 if (r)
3472 return r;
3473
Takuya Yoshikawafd136902015-10-16 17:06:02 +09003474 force_pt_level = !check_hugepage_cache_consistency(vcpu, gfn,
3475 PT_DIRECTORY_LEVEL);
3476 level = mapping_level(vcpu, gfn, &force_pt_level);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08003477 if (likely(!force_pt_level)) {
Xiao Guangrong6a39bbc2015-06-15 16:55:35 +08003478 if (level > PT_DIRECTORY_LEVEL &&
3479 !check_hugepage_cache_consistency(vcpu, gfn, level))
3480 level = PT_DIRECTORY_LEVEL;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08003481 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Takuya Yoshikawafd136902015-10-16 17:06:02 +09003482 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02003483
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003484 if (fast_page_fault(vcpu, gpa, level, error_code))
3485 return 0;
3486
Andrea Arcangelie930bff2008-07-25 16:24:52 +02003487 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03003488 smp_rmb();
Gleb Natapovaf585b92010-10-14 11:22:46 +02003489
Xiao Guangrong78b2c542010-12-07 10:48:06 +08003490 if (try_async_pf(vcpu, prefault, gfn, gpa, &pfn, write, &map_writable))
Gleb Natapovaf585b92010-10-14 11:22:46 +02003491 return 0;
3492
Xiao Guangrongd7c55202011-07-12 03:29:38 +08003493 if (handle_abnormal_pfn(vcpu, 0, gfn, pfn, ACC_ALL, &r))
3494 return r;
3495
Joerg Roedelfb72d162008-02-07 13:47:44 +01003496 spin_lock(&vcpu->kvm->mmu_lock);
Christoffer Dall8ca40a72012-10-14 23:10:18 -04003497 if (mmu_notifier_retry(vcpu->kvm, mmu_seq))
Andrea Arcangelie930bff2008-07-25 16:24:52 +02003498 goto out_unlock;
Takuya Yoshikawa450e0b42013-03-29 14:05:26 +09003499 make_mmu_pages_available(vcpu);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08003500 if (likely(!force_pt_level))
3501 transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
Takuya Yoshikawa7ee0e5b2015-11-20 17:42:23 +09003502 r = __direct_map(vcpu, write, map_writable, level, gfn, pfn, prefault);
Joerg Roedelfb72d162008-02-07 13:47:44 +01003503 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01003504
3505 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02003506
3507out_unlock:
3508 spin_unlock(&vcpu->kvm->mmu_lock);
3509 kvm_release_pfn_clean(pfn);
3510 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01003511}
3512
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02003513static void nonpaging_init_context(struct kvm_vcpu *vcpu,
3514 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003515{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003516 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003517 context->gva_to_gpa = nonpaging_gva_to_gpa;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03003518 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03003519 context->invlpg = nonpaging_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08003520 context->update_pte = nonpaging_update_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08003521 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003522 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03003523 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02003524 context->direct_map = true;
Joerg Roedel2d48a982010-09-10 17:31:01 +02003525 context->nx = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003526}
3527
Paolo Bonzinid8d173d2013-10-02 16:56:11 +02003528void kvm_mmu_new_cr3(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003529{
Avi Kivitycea0f0e2007-01-05 16:36:43 -08003530 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003531}
3532
Joerg Roedel5777ed32010-09-10 17:30:42 +02003533static unsigned long get_cr3(struct kvm_vcpu *vcpu)
3534{
Avi Kivity9f8fe502010-12-05 17:30:00 +02003535 return kvm_read_cr3(vcpu);
Joerg Roedel5777ed32010-09-10 17:30:42 +02003536}
3537
Avi Kivity6389ee92010-11-29 16:12:30 +02003538static void inject_page_fault(struct kvm_vcpu *vcpu,
3539 struct x86_exception *fault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003540{
Avi Kivity6389ee92010-11-29 16:12:30 +02003541 vcpu->arch.mmu.inject_page_fault(vcpu, fault);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003542}
3543
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003544static bool sync_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, gfn_t gfn,
Xiao Guangrongf2fd1252013-06-07 16:51:24 +08003545 unsigned access, int *nr_present)
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003546{
3547 if (unlikely(is_mmio_spte(*sptep))) {
3548 if (gfn != get_mmio_spte_gfn(*sptep)) {
3549 mmu_spte_clear_no_track(sptep);
3550 return true;
3551 }
3552
3553 (*nr_present)++;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003554 mark_mmio_spte(vcpu, sptep, gfn, access);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003555 return true;
3556 }
3557
3558 return false;
3559}
3560
Avi Kivity6fd01b72012-09-12 20:46:56 +03003561static inline bool is_last_gpte(struct kvm_mmu *mmu, unsigned level, unsigned gpte)
3562{
3563 unsigned index;
3564
3565 index = level - 1;
3566 index |= (gpte & PT_PAGE_SIZE_MASK) >> (PT_PAGE_SIZE_SHIFT - 2);
3567 return mmu->last_pte_bitmap & (1 << index);
3568}
3569
Nadav Har'El37406aa2013-08-05 11:07:12 +03003570#define PTTYPE_EPT 18 /* arbitrary */
3571#define PTTYPE PTTYPE_EPT
3572#include "paging_tmpl.h"
3573#undef PTTYPE
3574
Avi Kivity6aa8b732006-12-10 02:21:36 -08003575#define PTTYPE 64
3576#include "paging_tmpl.h"
3577#undef PTTYPE
3578
3579#define PTTYPE 32
3580#include "paging_tmpl.h"
3581#undef PTTYPE
3582
Xiao Guangrong6dc98b82015-08-05 12:04:22 +08003583static void
3584__reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
3585 struct rsvd_bits_validate *rsvd_check,
3586 int maxphyaddr, int level, bool nx, bool gbpages,
Paolo Bonzini6fec2142015-09-22 23:02:14 +02003587 bool pse, bool amd)
Dong, Eddie82725b22009-03-30 16:21:08 +08003588{
Dong, Eddie82725b22009-03-30 16:21:08 +08003589 u64 exb_bit_rsvd = 0;
Nadav Amit5f7dde72014-05-07 15:32:50 +03003590 u64 gbpages_bit_rsvd = 0;
Paolo Bonzinia0c0feb2014-09-02 13:24:12 +02003591 u64 nonleaf_bit8_rsvd = 0;
Dong, Eddie82725b22009-03-30 16:21:08 +08003592
Xiao Guangronga0a64f52015-08-05 12:04:21 +08003593 rsvd_check->bad_mt_xwr = 0;
Yang Zhang25d92082013-08-06 12:00:32 +03003594
Xiao Guangrong6dc98b82015-08-05 12:04:22 +08003595 if (!nx)
Dong, Eddie82725b22009-03-30 16:21:08 +08003596 exb_bit_rsvd = rsvd_bits(63, 63);
Xiao Guangrong6dc98b82015-08-05 12:04:22 +08003597 if (!gbpages)
Nadav Amit5f7dde72014-05-07 15:32:50 +03003598 gbpages_bit_rsvd = rsvd_bits(7, 7);
Paolo Bonzinia0c0feb2014-09-02 13:24:12 +02003599
3600 /*
3601 * Non-leaf PML4Es and PDPEs reserve bit 8 (which would be the G bit for
3602 * leaf entries) on AMD CPUs only.
3603 */
Paolo Bonzini6fec2142015-09-22 23:02:14 +02003604 if (amd)
Paolo Bonzinia0c0feb2014-09-02 13:24:12 +02003605 nonleaf_bit8_rsvd = rsvd_bits(8, 8);
3606
Xiao Guangrong6dc98b82015-08-05 12:04:22 +08003607 switch (level) {
Dong, Eddie82725b22009-03-30 16:21:08 +08003608 case PT32_ROOT_LEVEL:
3609 /* no rsvd bits for 2 level 4K page table entries */
Xiao Guangronga0a64f52015-08-05 12:04:21 +08003610 rsvd_check->rsvd_bits_mask[0][1] = 0;
3611 rsvd_check->rsvd_bits_mask[0][0] = 0;
3612 rsvd_check->rsvd_bits_mask[1][0] =
3613 rsvd_check->rsvd_bits_mask[0][0];
Xiao Guangrongf815bce2010-03-19 17:58:53 +08003614
Xiao Guangrong6dc98b82015-08-05 12:04:22 +08003615 if (!pse) {
Xiao Guangronga0a64f52015-08-05 12:04:21 +08003616 rsvd_check->rsvd_bits_mask[1][1] = 0;
Xiao Guangrongf815bce2010-03-19 17:58:53 +08003617 break;
3618 }
3619
Dong, Eddie82725b22009-03-30 16:21:08 +08003620 if (is_cpuid_PSE36())
3621 /* 36bits PSE 4MB page */
Xiao Guangronga0a64f52015-08-05 12:04:21 +08003622 rsvd_check->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08003623 else
3624 /* 32 bits PSE 4MB page */
Xiao Guangronga0a64f52015-08-05 12:04:21 +08003625 rsvd_check->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08003626 break;
3627 case PT32E_ROOT_LEVEL:
Xiao Guangronga0a64f52015-08-05 12:04:21 +08003628 rsvd_check->rsvd_bits_mask[0][2] =
Dong, Eddie20c466b2009-03-31 23:03:45 +08003629 rsvd_bits(maxphyaddr, 63) |
Nadav Amitcd9ae5f2014-04-04 06:31:04 +03003630 rsvd_bits(5, 8) | rsvd_bits(1, 2); /* PDPTE */
Xiao Guangronga0a64f52015-08-05 12:04:21 +08003631 rsvd_check->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08003632 rsvd_bits(maxphyaddr, 62); /* PDE */
Xiao Guangronga0a64f52015-08-05 12:04:21 +08003633 rsvd_check->rsvd_bits_mask[0][0] = exb_bit_rsvd |
Dong, Eddie82725b22009-03-30 16:21:08 +08003634 rsvd_bits(maxphyaddr, 62); /* PTE */
Xiao Guangronga0a64f52015-08-05 12:04:21 +08003635 rsvd_check->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Dong, Eddie82725b22009-03-30 16:21:08 +08003636 rsvd_bits(maxphyaddr, 62) |
3637 rsvd_bits(13, 20); /* large page */
Xiao Guangronga0a64f52015-08-05 12:04:21 +08003638 rsvd_check->rsvd_bits_mask[1][0] =
3639 rsvd_check->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08003640 break;
3641 case PT64_ROOT_LEVEL:
Xiao Guangronga0a64f52015-08-05 12:04:21 +08003642 rsvd_check->rsvd_bits_mask[0][3] = exb_bit_rsvd |
3643 nonleaf_bit8_rsvd | rsvd_bits(7, 7) |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08003644 rsvd_bits(maxphyaddr, 51);
Xiao Guangronga0a64f52015-08-05 12:04:21 +08003645 rsvd_check->rsvd_bits_mask[0][2] = exb_bit_rsvd |
3646 nonleaf_bit8_rsvd | gbpages_bit_rsvd |
Dong, Eddie82725b22009-03-30 16:21:08 +08003647 rsvd_bits(maxphyaddr, 51);
Xiao Guangronga0a64f52015-08-05 12:04:21 +08003648 rsvd_check->rsvd_bits_mask[0][1] = exb_bit_rsvd |
3649 rsvd_bits(maxphyaddr, 51);
3650 rsvd_check->rsvd_bits_mask[0][0] = exb_bit_rsvd |
3651 rsvd_bits(maxphyaddr, 51);
3652 rsvd_check->rsvd_bits_mask[1][3] =
3653 rsvd_check->rsvd_bits_mask[0][3];
3654 rsvd_check->rsvd_bits_mask[1][2] = exb_bit_rsvd |
Nadav Amit5f7dde72014-05-07 15:32:50 +03003655 gbpages_bit_rsvd | rsvd_bits(maxphyaddr, 51) |
Joerg Roedele04da982009-07-27 16:30:45 +02003656 rsvd_bits(13, 29);
Xiao Guangronga0a64f52015-08-05 12:04:21 +08003657 rsvd_check->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08003658 rsvd_bits(maxphyaddr, 51) |
3659 rsvd_bits(13, 20); /* large page */
Xiao Guangronga0a64f52015-08-05 12:04:21 +08003660 rsvd_check->rsvd_bits_mask[1][0] =
3661 rsvd_check->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08003662 break;
3663 }
3664}
3665
Xiao Guangrong6dc98b82015-08-05 12:04:22 +08003666static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
3667 struct kvm_mmu *context)
3668{
3669 __reset_rsvds_bits_mask(vcpu, &context->guest_rsvd_check,
3670 cpuid_maxphyaddr(vcpu), context->root_level,
3671 context->nx, guest_cpuid_has_gbpages(vcpu),
Paolo Bonzini6fec2142015-09-22 23:02:14 +02003672 is_pse(vcpu), guest_cpuid_is_amd(vcpu));
Xiao Guangrong6dc98b82015-08-05 12:04:22 +08003673}
3674
Xiao Guangrong81b8eeb2015-08-05 12:04:23 +08003675static void
3676__reset_rsvds_bits_mask_ept(struct rsvd_bits_validate *rsvd_check,
3677 int maxphyaddr, bool execonly)
Yang Zhang25d92082013-08-06 12:00:32 +03003678{
Paolo Bonzini951f9fd2015-09-23 10:34:26 +02003679 u64 bad_mt_xwr;
Yang Zhang25d92082013-08-06 12:00:32 +03003680
Xiao Guangronga0a64f52015-08-05 12:04:21 +08003681 rsvd_check->rsvd_bits_mask[0][3] =
Yang Zhang25d92082013-08-06 12:00:32 +03003682 rsvd_bits(maxphyaddr, 51) | rsvd_bits(3, 7);
Xiao Guangronga0a64f52015-08-05 12:04:21 +08003683 rsvd_check->rsvd_bits_mask[0][2] =
Yang Zhang25d92082013-08-06 12:00:32 +03003684 rsvd_bits(maxphyaddr, 51) | rsvd_bits(3, 6);
Xiao Guangronga0a64f52015-08-05 12:04:21 +08003685 rsvd_check->rsvd_bits_mask[0][1] =
Yang Zhang25d92082013-08-06 12:00:32 +03003686 rsvd_bits(maxphyaddr, 51) | rsvd_bits(3, 6);
Xiao Guangronga0a64f52015-08-05 12:04:21 +08003687 rsvd_check->rsvd_bits_mask[0][0] = rsvd_bits(maxphyaddr, 51);
Yang Zhang25d92082013-08-06 12:00:32 +03003688
3689 /* large page */
Xiao Guangronga0a64f52015-08-05 12:04:21 +08003690 rsvd_check->rsvd_bits_mask[1][3] = rsvd_check->rsvd_bits_mask[0][3];
3691 rsvd_check->rsvd_bits_mask[1][2] =
Yang Zhang25d92082013-08-06 12:00:32 +03003692 rsvd_bits(maxphyaddr, 51) | rsvd_bits(12, 29);
Xiao Guangronga0a64f52015-08-05 12:04:21 +08003693 rsvd_check->rsvd_bits_mask[1][1] =
Yang Zhang25d92082013-08-06 12:00:32 +03003694 rsvd_bits(maxphyaddr, 51) | rsvd_bits(12, 20);
Xiao Guangronga0a64f52015-08-05 12:04:21 +08003695 rsvd_check->rsvd_bits_mask[1][0] = rsvd_check->rsvd_bits_mask[0][0];
Yang Zhang25d92082013-08-06 12:00:32 +03003696
Paolo Bonzini951f9fd2015-09-23 10:34:26 +02003697 bad_mt_xwr = 0xFFull << (2 * 8); /* bits 3..5 must not be 2 */
3698 bad_mt_xwr |= 0xFFull << (3 * 8); /* bits 3..5 must not be 3 */
3699 bad_mt_xwr |= 0xFFull << (7 * 8); /* bits 3..5 must not be 7 */
3700 bad_mt_xwr |= REPEAT_BYTE(1ull << 2); /* bits 0..2 must not be 010 */
3701 bad_mt_xwr |= REPEAT_BYTE(1ull << 6); /* bits 0..2 must not be 110 */
3702 if (!execonly) {
3703 /* bits 0..2 must not be 100 unless VMX capabilities allow it */
3704 bad_mt_xwr |= REPEAT_BYTE(1ull << 4);
Yang Zhang25d92082013-08-06 12:00:32 +03003705 }
Paolo Bonzini951f9fd2015-09-23 10:34:26 +02003706 rsvd_check->bad_mt_xwr = bad_mt_xwr;
Yang Zhang25d92082013-08-06 12:00:32 +03003707}
3708
Xiao Guangrong81b8eeb2015-08-05 12:04:23 +08003709static void reset_rsvds_bits_mask_ept(struct kvm_vcpu *vcpu,
3710 struct kvm_mmu *context, bool execonly)
3711{
3712 __reset_rsvds_bits_mask_ept(&context->guest_rsvd_check,
3713 cpuid_maxphyaddr(vcpu), execonly);
3714}
3715
Xiao Guangrongc258b622015-08-05 12:04:24 +08003716/*
3717 * the page table on host is the shadow page table for the page
3718 * table in guest or amd nested guest, its mmu features completely
3719 * follow the features in guest.
3720 */
3721void
3722reset_shadow_zero_bits_mask(struct kvm_vcpu *vcpu, struct kvm_mmu *context)
3723{
Paolo Bonzini5f0b8192016-03-09 14:28:02 +01003724 bool uses_nx = context->nx || context->base_role.smep_andnot_wp;
3725
Paolo Bonzini6fec2142015-09-22 23:02:14 +02003726 /*
3727 * Passing "true" to the last argument is okay; it adds a check
3728 * on bit 8 of the SPTEs which KVM doesn't use anyway.
3729 */
Xiao Guangrongc258b622015-08-05 12:04:24 +08003730 __reset_rsvds_bits_mask(vcpu, &context->shadow_zero_check,
3731 boot_cpu_data.x86_phys_bits,
Paolo Bonzini5f0b8192016-03-09 14:28:02 +01003732 context->shadow_root_level, uses_nx,
Paolo Bonzini6fec2142015-09-22 23:02:14 +02003733 guest_cpuid_has_gbpages(vcpu), is_pse(vcpu),
3734 true);
Xiao Guangrongc258b622015-08-05 12:04:24 +08003735}
3736EXPORT_SYMBOL_GPL(reset_shadow_zero_bits_mask);
3737
Paolo Bonzini6fec2142015-09-22 23:02:14 +02003738static inline bool boot_cpu_is_amd(void)
3739{
3740 WARN_ON_ONCE(!tdp_enabled);
3741 return shadow_x_mask == 0;
3742}
3743
Xiao Guangrongc258b622015-08-05 12:04:24 +08003744/*
3745 * the direct page table on host, use as much mmu features as
3746 * possible, however, kvm currently does not do execution-protection.
3747 */
3748static void
3749reset_tdp_shadow_zero_bits_mask(struct kvm_vcpu *vcpu,
3750 struct kvm_mmu *context)
3751{
Paolo Bonzini6fec2142015-09-22 23:02:14 +02003752 if (boot_cpu_is_amd())
Xiao Guangrongc258b622015-08-05 12:04:24 +08003753 __reset_rsvds_bits_mask(vcpu, &context->shadow_zero_check,
3754 boot_cpu_data.x86_phys_bits,
3755 context->shadow_root_level, false,
Paolo Bonzini6fec2142015-09-22 23:02:14 +02003756 cpu_has_gbpages, true, true);
Xiao Guangrongc258b622015-08-05 12:04:24 +08003757 else
3758 __reset_rsvds_bits_mask_ept(&context->shadow_zero_check,
3759 boot_cpu_data.x86_phys_bits,
3760 false);
3761
3762}
3763
3764/*
3765 * as the comments in reset_shadow_zero_bits_mask() except it
3766 * is the shadow page table for intel nested guest.
3767 */
3768static void
3769reset_ept_shadow_zero_bits_mask(struct kvm_vcpu *vcpu,
3770 struct kvm_mmu *context, bool execonly)
3771{
3772 __reset_rsvds_bits_mask_ept(&context->shadow_zero_check,
3773 boot_cpu_data.x86_phys_bits, execonly);
3774}
3775
Xiao Guangrongedc90b72015-05-11 22:55:21 +08003776static void update_permission_bitmask(struct kvm_vcpu *vcpu,
3777 struct kvm_mmu *mmu, bool ept)
Avi Kivity97d64b72012-09-12 14:52:00 +03003778{
3779 unsigned bit, byte, pfec;
3780 u8 map;
Feng Wu66386ade2014-04-01 17:56:48 +08003781 bool fault, x, w, u, wf, uf, ff, smapf, cr4_smap, cr4_smep, smap = 0;
Avi Kivity97d64b72012-09-12 14:52:00 +03003782
Feng Wu66386ade2014-04-01 17:56:48 +08003783 cr4_smep = kvm_read_cr4_bits(vcpu, X86_CR4_SMEP);
Feng Wu97ec8c02014-04-01 17:46:34 +08003784 cr4_smap = kvm_read_cr4_bits(vcpu, X86_CR4_SMAP);
Avi Kivity97d64b72012-09-12 14:52:00 +03003785 for (byte = 0; byte < ARRAY_SIZE(mmu->permissions); ++byte) {
3786 pfec = byte << 1;
3787 map = 0;
3788 wf = pfec & PFERR_WRITE_MASK;
3789 uf = pfec & PFERR_USER_MASK;
3790 ff = pfec & PFERR_FETCH_MASK;
Feng Wu97ec8c02014-04-01 17:46:34 +08003791 /*
3792 * PFERR_RSVD_MASK bit is set in PFEC if the access is not
3793 * subject to SMAP restrictions, and cleared otherwise. The
3794 * bit is only meaningful if the SMAP bit is set in CR4.
3795 */
3796 smapf = !(pfec & PFERR_RSVD_MASK);
Avi Kivity97d64b72012-09-12 14:52:00 +03003797 for (bit = 0; bit < 8; ++bit) {
3798 x = bit & ACC_EXEC_MASK;
3799 w = bit & ACC_WRITE_MASK;
3800 u = bit & ACC_USER_MASK;
3801
Yang Zhang25d92082013-08-06 12:00:32 +03003802 if (!ept) {
3803 /* Not really needed: !nx will cause pte.nx to fault */
3804 x |= !mmu->nx;
3805 /* Allow supervisor writes if !cr0.wp */
3806 w |= !is_write_protection(vcpu) && !uf;
3807 /* Disallow supervisor fetches of user code if cr4.smep */
Feng Wu66386ade2014-04-01 17:56:48 +08003808 x &= !(cr4_smep && u && !uf);
Feng Wu97ec8c02014-04-01 17:46:34 +08003809
3810 /*
3811 * SMAP:kernel-mode data accesses from user-mode
3812 * mappings should fault. A fault is considered
3813 * as a SMAP violation if all of the following
3814 * conditions are ture:
3815 * - X86_CR4_SMAP is set in CR4
3816 * - An user page is accessed
3817 * - Page fault in kernel mode
3818 * - if CPL = 3 or X86_EFLAGS_AC is clear
3819 *
3820 * Here, we cover the first three conditions.
3821 * The fourth is computed dynamically in
3822 * permission_fault() and is in smapf.
3823 *
3824 * Also, SMAP does not affect instruction
3825 * fetches, add the !ff check here to make it
3826 * clearer.
3827 */
3828 smap = cr4_smap && u && !uf && !ff;
Yang Zhang25d92082013-08-06 12:00:32 +03003829 } else
3830 /* Not really needed: no U/S accesses on ept */
3831 u = 1;
Avi Kivity97d64b72012-09-12 14:52:00 +03003832
Feng Wu97ec8c02014-04-01 17:46:34 +08003833 fault = (ff && !x) || (uf && !u) || (wf && !w) ||
3834 (smapf && smap);
Avi Kivity97d64b72012-09-12 14:52:00 +03003835 map |= fault << bit;
3836 }
3837 mmu->permissions[byte] = map;
3838 }
3839}
3840
Avi Kivity6fd01b72012-09-12 20:46:56 +03003841static void update_last_pte_bitmap(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu)
3842{
3843 u8 map;
3844 unsigned level, root_level = mmu->root_level;
3845 const unsigned ps_set_index = 1 << 2; /* bit 2 of index: ps */
3846
3847 if (root_level == PT32E_ROOT_LEVEL)
3848 --root_level;
3849 /* PT_PAGE_TABLE_LEVEL always terminates */
3850 map = 1 | (1 << ps_set_index);
3851 for (level = PT_DIRECTORY_LEVEL; level <= root_level; ++level) {
3852 if (level <= PT_PDPE_LEVEL
3853 && (mmu->root_level >= PT32E_ROOT_LEVEL || is_pse(vcpu)))
3854 map |= 1 << (ps_set_index | (level - 1));
3855 }
3856 mmu->last_pte_bitmap = map;
3857}
3858
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02003859static void paging64_init_context_common(struct kvm_vcpu *vcpu,
3860 struct kvm_mmu *context,
3861 int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003862{
Joerg Roedel2d48a982010-09-10 17:31:01 +02003863 context->nx = is_nx(vcpu);
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01003864 context->root_level = level;
Joerg Roedel2d48a982010-09-10 17:31:01 +02003865
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01003866 reset_rsvds_bits_mask(vcpu, context);
Yang Zhang25d92082013-08-06 12:00:32 +03003867 update_permission_bitmask(vcpu, context, false);
Avi Kivity6fd01b72012-09-12 20:46:56 +03003868 update_last_pte_bitmap(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003869
Paolo Bonzinifa4a2c02013-10-02 16:56:16 +02003870 MMU_WARN_ON(!is_pae(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003871 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003872 context->gva_to_gpa = paging64_gva_to_gpa;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03003873 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03003874 context->invlpg = paging64_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08003875 context->update_pte = paging64_update_pte;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003876 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03003877 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02003878 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003879}
3880
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02003881static void paging64_init_context(struct kvm_vcpu *vcpu,
3882 struct kvm_mmu *context)
Avi Kivity17ac10a2007-01-05 16:36:40 -08003883{
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02003884 paging64_init_context_common(vcpu, context, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003885}
3886
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02003887static void paging32_init_context(struct kvm_vcpu *vcpu,
3888 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003889{
Joerg Roedel2d48a982010-09-10 17:31:01 +02003890 context->nx = false;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01003891 context->root_level = PT32_ROOT_LEVEL;
Joerg Roedel2d48a982010-09-10 17:31:01 +02003892
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01003893 reset_rsvds_bits_mask(vcpu, context);
Yang Zhang25d92082013-08-06 12:00:32 +03003894 update_permission_bitmask(vcpu, context, false);
Avi Kivity6fd01b72012-09-12 20:46:56 +03003895 update_last_pte_bitmap(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003896
Avi Kivity6aa8b732006-12-10 02:21:36 -08003897 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003898 context->gva_to_gpa = paging32_gva_to_gpa;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03003899 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03003900 context->invlpg = paging32_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08003901 context->update_pte = paging32_update_pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003902 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03003903 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02003904 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003905}
3906
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02003907static void paging32E_init_context(struct kvm_vcpu *vcpu,
3908 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003909{
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02003910 paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003911}
3912
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02003913static void init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
Joerg Roedelfb72d162008-02-07 13:47:44 +01003914{
Paolo Bonziniad896af2013-10-02 16:56:14 +02003915 struct kvm_mmu *context = &vcpu->arch.mmu;
Joerg Roedelfb72d162008-02-07 13:47:44 +01003916
Avi Kivityc445f8e2010-12-21 16:26:01 +02003917 context->base_role.word = 0;
Paolo Bonzini699023e2015-05-18 15:03:39 +02003918 context->base_role.smm = is_smm(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01003919 context->page_fault = tdp_page_fault;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03003920 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03003921 context->invlpg = nonpaging_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08003922 context->update_pte = nonpaging_update_pte;
Sheng Yang67253af2008-04-25 10:20:22 +08003923 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01003924 context->root_hpa = INVALID_PAGE;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02003925 context->direct_map = true;
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003926 context->set_cr3 = kvm_x86_ops->set_tdp_cr3;
Joerg Roedel5777ed32010-09-10 17:30:42 +02003927 context->get_cr3 = get_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03003928 context->get_pdptr = kvm_pdptr_read;
Joerg Roedelcb659db2010-09-10 17:30:43 +02003929 context->inject_page_fault = kvm_inject_page_fault;
Joerg Roedelfb72d162008-02-07 13:47:44 +01003930
3931 if (!is_paging(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003932 context->nx = false;
Joerg Roedelfb72d162008-02-07 13:47:44 +01003933 context->gva_to_gpa = nonpaging_gva_to_gpa;
3934 context->root_level = 0;
3935 } else if (is_long_mode(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003936 context->nx = is_nx(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01003937 context->root_level = PT64_ROOT_LEVEL;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01003938 reset_rsvds_bits_mask(vcpu, context);
3939 context->gva_to_gpa = paging64_gva_to_gpa;
Joerg Roedelfb72d162008-02-07 13:47:44 +01003940 } else if (is_pae(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003941 context->nx = is_nx(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01003942 context->root_level = PT32E_ROOT_LEVEL;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01003943 reset_rsvds_bits_mask(vcpu, context);
3944 context->gva_to_gpa = paging64_gva_to_gpa;
Joerg Roedelfb72d162008-02-07 13:47:44 +01003945 } else {
Joerg Roedel2d48a982010-09-10 17:31:01 +02003946 context->nx = false;
Joerg Roedelfb72d162008-02-07 13:47:44 +01003947 context->root_level = PT32_ROOT_LEVEL;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01003948 reset_rsvds_bits_mask(vcpu, context);
3949 context->gva_to_gpa = paging32_gva_to_gpa;
Joerg Roedelfb72d162008-02-07 13:47:44 +01003950 }
3951
Yang Zhang25d92082013-08-06 12:00:32 +03003952 update_permission_bitmask(vcpu, context, false);
Avi Kivity6fd01b72012-09-12 20:46:56 +03003953 update_last_pte_bitmap(vcpu, context);
Xiao Guangrongc258b622015-08-05 12:04:24 +08003954 reset_tdp_shadow_zero_bits_mask(vcpu, context);
Joerg Roedelfb72d162008-02-07 13:47:44 +01003955}
3956
Paolo Bonziniad896af2013-10-02 16:56:14 +02003957void kvm_init_shadow_mmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003958{
Avi Kivity411c5882011-06-06 16:11:54 +03003959 bool smep = kvm_read_cr4_bits(vcpu, X86_CR4_SMEP);
Xiao Guangrongedc90b72015-05-11 22:55:21 +08003960 bool smap = kvm_read_cr4_bits(vcpu, X86_CR4_SMAP);
Paolo Bonziniad896af2013-10-02 16:56:14 +02003961 struct kvm_mmu *context = &vcpu->arch.mmu;
3962
Paolo Bonzinifa4a2c02013-10-02 16:56:16 +02003963 MMU_WARN_ON(VALID_PAGE(context->root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003964
3965 if (!is_paging(vcpu))
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02003966 nonpaging_init_context(vcpu, context);
Avi Kivitya9058ec2006-12-29 16:49:37 -08003967 else if (is_long_mode(vcpu))
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02003968 paging64_init_context(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003969 else if (is_pae(vcpu))
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02003970 paging32E_init_context(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003971 else
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02003972 paging32_init_context(vcpu, context);
Avi Kivitya770f6f2008-12-21 19:20:09 +02003973
Paolo Bonziniad896af2013-10-02 16:56:14 +02003974 context->base_role.nxe = is_nx(vcpu);
3975 context->base_role.cr4_pae = !!is_pae(vcpu);
3976 context->base_role.cr0_wp = is_write_protection(vcpu);
3977 context->base_role.smep_andnot_wp
Avi Kivity411c5882011-06-06 16:11:54 +03003978 = smep && !is_write_protection(vcpu);
Xiao Guangrongedc90b72015-05-11 22:55:21 +08003979 context->base_role.smap_andnot_wp
3980 = smap && !is_write_protection(vcpu);
Paolo Bonzini699023e2015-05-18 15:03:39 +02003981 context->base_role.smm = is_smm(vcpu);
Xiao Guangrongc258b622015-08-05 12:04:24 +08003982 reset_shadow_zero_bits_mask(vcpu, context);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02003983}
3984EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu);
3985
Paolo Bonziniad896af2013-10-02 16:56:14 +02003986void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly)
Nadav Har'El155a97a2013-08-05 11:07:16 +03003987{
Paolo Bonziniad896af2013-10-02 16:56:14 +02003988 struct kvm_mmu *context = &vcpu->arch.mmu;
3989
Paolo Bonzinifa4a2c02013-10-02 16:56:16 +02003990 MMU_WARN_ON(VALID_PAGE(context->root_hpa));
Nadav Har'El155a97a2013-08-05 11:07:16 +03003991
3992 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
3993
3994 context->nx = true;
Nadav Har'El155a97a2013-08-05 11:07:16 +03003995 context->page_fault = ept_page_fault;
3996 context->gva_to_gpa = ept_gva_to_gpa;
3997 context->sync_page = ept_sync_page;
3998 context->invlpg = ept_invlpg;
3999 context->update_pte = ept_update_pte;
Nadav Har'El155a97a2013-08-05 11:07:16 +03004000 context->root_level = context->shadow_root_level;
4001 context->root_hpa = INVALID_PAGE;
4002 context->direct_map = false;
4003
4004 update_permission_bitmask(vcpu, context, true);
4005 reset_rsvds_bits_mask_ept(vcpu, context, execonly);
Xiao Guangrongc258b622015-08-05 12:04:24 +08004006 reset_ept_shadow_zero_bits_mask(vcpu, context, execonly);
Nadav Har'El155a97a2013-08-05 11:07:16 +03004007}
4008EXPORT_SYMBOL_GPL(kvm_init_shadow_ept_mmu);
4009
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004010static void init_kvm_softmmu(struct kvm_vcpu *vcpu)
Joerg Roedel52fde8d2010-09-10 17:30:44 +02004011{
Paolo Bonziniad896af2013-10-02 16:56:14 +02004012 struct kvm_mmu *context = &vcpu->arch.mmu;
4013
4014 kvm_init_shadow_mmu(vcpu);
4015 context->set_cr3 = kvm_x86_ops->set_cr3;
4016 context->get_cr3 = get_cr3;
4017 context->get_pdptr = kvm_pdptr_read;
4018 context->inject_page_fault = kvm_inject_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004019}
4020
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004021static void init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004022{
4023 struct kvm_mmu *g_context = &vcpu->arch.nested_mmu;
4024
4025 g_context->get_cr3 = get_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03004026 g_context->get_pdptr = kvm_pdptr_read;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004027 g_context->inject_page_fault = kvm_inject_page_fault;
4028
4029 /*
David Matlack0af25932015-12-30 08:26:17 -08004030 * Note that arch.mmu.gva_to_gpa translates l2_gpa to l1_gpa using
4031 * L1's nested page tables (e.g. EPT12). The nested translation
4032 * of l2_gva to l1_gpa is done by arch.nested_mmu.gva_to_gpa using
4033 * L2's page tables as the first level of translation and L1's
4034 * nested page tables as the second level of translation. Basically
4035 * the gva_to_gpa functions between mmu and nested_mmu are swapped.
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004036 */
4037 if (!is_paging(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02004038 g_context->nx = false;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004039 g_context->root_level = 0;
4040 g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested;
4041 } else if (is_long_mode(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02004042 g_context->nx = is_nx(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004043 g_context->root_level = PT64_ROOT_LEVEL;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004044 reset_rsvds_bits_mask(vcpu, g_context);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004045 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
4046 } else if (is_pae(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02004047 g_context->nx = is_nx(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004048 g_context->root_level = PT32E_ROOT_LEVEL;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004049 reset_rsvds_bits_mask(vcpu, g_context);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004050 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
4051 } else {
Joerg Roedel2d48a982010-09-10 17:31:01 +02004052 g_context->nx = false;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004053 g_context->root_level = PT32_ROOT_LEVEL;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004054 reset_rsvds_bits_mask(vcpu, g_context);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004055 g_context->gva_to_gpa = paging32_gva_to_gpa_nested;
4056 }
4057
Yang Zhang25d92082013-08-06 12:00:32 +03004058 update_permission_bitmask(vcpu, g_context, false);
Avi Kivity6fd01b72012-09-12 20:46:56 +03004059 update_last_pte_bitmap(vcpu, g_context);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004060}
4061
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004062static void init_kvm_mmu(struct kvm_vcpu *vcpu)
Joerg Roedelfb72d162008-02-07 13:47:44 +01004063{
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004064 if (mmu_is_nested(vcpu))
Paolo Bonzinie0c6db32014-12-23 13:39:46 +01004065 init_kvm_nested_mmu(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004066 else if (tdp_enabled)
Paolo Bonzinie0c6db32014-12-23 13:39:46 +01004067 init_kvm_tdp_mmu(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01004068 else
Paolo Bonzinie0c6db32014-12-23 13:39:46 +01004069 init_kvm_softmmu(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01004070}
4071
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004072void kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004073{
Paolo Bonzini95f93af2013-10-02 16:56:12 +02004074 kvm_mmu_unload(vcpu);
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004075 init_kvm_mmu(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03004076}
Eddie Dong8668a3c2007-10-10 14:26:45 +08004077EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03004078
4079int kvm_mmu_load(struct kvm_vcpu *vcpu)
4080{
Avi Kivity714b93d2007-01-05 16:36:53 -08004081 int r;
4082
Avi Kivitye2dec932007-01-05 16:36:54 -08004083 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03004084 if (r)
4085 goto out;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03004086 r = mmu_alloc_roots(vcpu);
Takuya Yoshikawae2858b42013-05-09 15:45:12 +09004087 kvm_mmu_sync_roots(vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03004088 if (r)
4089 goto out;
Sheng Yang3662cb12009-07-09 17:00:42 +08004090 /* set_cr3() should ensure TLB has been flushed */
Joerg Roedelf43addd2010-09-10 17:30:40 +02004091 vcpu->arch.mmu.set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity714b93d2007-01-05 16:36:53 -08004092out:
4093 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004094}
Avi Kivity17c3ba92007-06-04 15:58:30 +03004095EXPORT_SYMBOL_GPL(kvm_mmu_load);
4096
4097void kvm_mmu_unload(struct kvm_vcpu *vcpu)
4098{
4099 mmu_free_roots(vcpu);
Paolo Bonzini95f93af2013-10-02 16:56:12 +02004100 WARN_ON(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity17c3ba92007-06-04 15:58:30 +03004101}
Joerg Roedel4b161842010-09-10 17:31:03 +02004102EXPORT_SYMBOL_GPL(kvm_mmu_unload);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004103
Avi Kivity00284252007-05-01 16:53:31 +03004104static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Xiao Guangrong7c562522011-03-28 10:29:27 +08004105 struct kvm_mmu_page *sp, u64 *spte,
4106 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03004107{
Marcelo Tosatti30945382008-06-11 20:32:40 -03004108 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02004109 ++vcpu->kvm->stat.mmu_pde_zapped;
4110 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03004111 }
Avi Kivity00284252007-05-01 16:53:31 +03004112
Avi Kivity4cee5762007-11-18 16:37:07 +02004113 ++vcpu->kvm->stat.mmu_pte_updated;
Xiao Guangrong7c562522011-03-28 10:29:27 +08004114 vcpu->arch.mmu.update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03004115}
4116
Avi Kivity79539ce2007-11-21 02:06:21 +02004117static bool need_remote_flush(u64 old, u64 new)
4118{
4119 if (!is_shadow_present_pte(old))
4120 return false;
4121 if (!is_shadow_present_pte(new))
4122 return true;
4123 if ((old ^ new) & PT64_BASE_ADDR_MASK)
4124 return true;
Gleb Natapov53166222013-08-05 11:07:14 +03004125 old ^= shadow_nx_mask;
4126 new ^= shadow_nx_mask;
Avi Kivity79539ce2007-11-21 02:06:21 +02004127 return (old & ~new & PT64_PERM_MASK) != 0;
4128}
4129
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08004130static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page,
4131 bool remote_flush, bool local_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02004132{
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08004133 if (zap_page)
4134 return;
4135
4136 if (remote_flush)
Avi Kivity79539ce2007-11-21 02:06:21 +02004137 kvm_flush_remote_tlbs(vcpu->kvm);
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08004138 else if (local_flush)
Liang Chen77c39132014-09-18 12:38:37 -04004139 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Avi Kivity79539ce2007-11-21 02:06:21 +02004140}
4141
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004142static u64 mmu_pte_write_fetch_gpte(struct kvm_vcpu *vcpu, gpa_t *gpa,
4143 const u8 *new, int *bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08004144{
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004145 u64 gentry;
4146 int r;
Avi Kivity72016f32010-03-15 13:59:53 +02004147
Avi Kivity08e850c2010-03-15 13:59:57 +02004148 /*
4149 * Assume that the pte write on a page table of the same type
Xiao Guangrong49b26e22011-03-04 19:00:00 +08004150 * as the current vcpu paging mode since we update the sptes only
4151 * when they have the same mode.
Avi Kivity08e850c2010-03-15 13:59:57 +02004152 */
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004153 if (is_pae(vcpu) && *bytes == 4) {
Avi Kivity08e850c2010-03-15 13:59:57 +02004154 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004155 *gpa &= ~(gpa_t)7;
4156 *bytes = 8;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004157 r = kvm_vcpu_read_guest(vcpu, *gpa, &gentry, 8);
Avi Kivity08e850c2010-03-15 13:59:57 +02004158 if (r)
4159 gentry = 0;
4160 new = (const u8 *)&gentry;
4161 }
4162
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004163 switch (*bytes) {
Avi Kivity72016f32010-03-15 13:59:53 +02004164 case 4:
4165 gentry = *(const u32 *)new;
4166 break;
4167 case 8:
4168 gentry = *(const u64 *)new;
4169 break;
4170 default:
4171 gentry = 0;
4172 break;
4173 }
4174
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004175 return gentry;
4176}
4177
4178/*
4179 * If we're seeing too many writes to a page, it may no longer be a page table,
4180 * or we may be forking, in which case it is better to unmap the page.
4181 */
Xiao Guangronga138fe72011-12-16 18:18:10 +08004182static bool detect_write_flooding(struct kvm_mmu_page *sp)
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004183{
Xiao Guangronga30f47c2011-09-22 16:58:36 +08004184 /*
4185 * Skip write-flooding detected for the sp whose level is 1, because
4186 * it can become unsync, then the guest page is not write-protected.
4187 */
Davidlohr Buesof71fa312012-04-18 12:24:29 +02004188 if (sp->role.level == PT_PAGE_TABLE_LEVEL)
Xiao Guangronga30f47c2011-09-22 16:58:36 +08004189 return false;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004190
Xiao Guangronga30f47c2011-09-22 16:58:36 +08004191 return ++sp->write_flooding_count >= 3;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004192}
4193
4194/*
4195 * Misaligned accesses are too much trouble to fix up; also, they usually
4196 * indicate a page is not used as a page table.
4197 */
4198static bool detect_write_misaligned(struct kvm_mmu_page *sp, gpa_t gpa,
4199 int bytes)
4200{
4201 unsigned offset, pte_size, misaligned;
4202
4203 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
4204 gpa, bytes, sp->role.word);
4205
4206 offset = offset_in_page(gpa);
4207 pte_size = sp->role.cr4_pae ? 8 : 4;
Xiao Guangrong5d9ca302011-09-22 16:57:55 +08004208
4209 /*
4210 * Sometimes, the OS only writes the last one bytes to update status
4211 * bits, for example, in linux, andb instruction is used in clear_bit().
4212 */
4213 if (!(offset & (pte_size - 1)) && bytes == 1)
4214 return false;
4215
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004216 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
4217 misaligned |= bytes < 4;
4218
4219 return misaligned;
4220}
4221
4222static u64 *get_written_sptes(struct kvm_mmu_page *sp, gpa_t gpa, int *nspte)
4223{
4224 unsigned page_offset, quadrant;
4225 u64 *spte;
4226 int level;
4227
4228 page_offset = offset_in_page(gpa);
4229 level = sp->role.level;
4230 *nspte = 1;
4231 if (!sp->role.cr4_pae) {
4232 page_offset <<= 1; /* 32->64 */
4233 /*
4234 * A 32-bit pde maps 4MB while the shadow pdes map
4235 * only 2MB. So we need to double the offset again
4236 * and zap two pdes instead of one.
4237 */
4238 if (level == PT32_ROOT_LEVEL) {
4239 page_offset &= ~7; /* kill rounding error */
4240 page_offset <<= 1;
4241 *nspte = 2;
4242 }
4243 quadrant = page_offset >> PAGE_SHIFT;
4244 page_offset &= ~PAGE_MASK;
4245 if (quadrant != sp->role.quadrant)
4246 return NULL;
4247 }
4248
4249 spte = &sp->spt[page_offset / sizeof(*spte)];
4250 return spte;
4251}
4252
4253void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
4254 const u8 *new, int bytes)
4255{
4256 gfn_t gfn = gpa >> PAGE_SHIFT;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004257 struct kvm_mmu_page *sp;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004258 LIST_HEAD(invalid_list);
4259 u64 entry, gentry, *spte;
4260 int npte;
Xiao Guangronga30f47c2011-09-22 16:58:36 +08004261 bool remote_flush, local_flush, zap_page;
Andrew Morton41412592015-05-27 11:53:06 +02004262 union kvm_mmu_page_role mask = { };
4263
4264 mask.cr0_wp = 1;
4265 mask.cr4_pae = 1;
4266 mask.nxe = 1;
4267 mask.smep_andnot_wp = 1;
4268 mask.smap_andnot_wp = 1;
Paolo Bonzini699023e2015-05-18 15:03:39 +02004269 mask.smm = 1;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004270
4271 /*
4272 * If we don't have indirect shadow pages, it means no page is
4273 * write-protected, so we can exit simply.
4274 */
4275 if (!ACCESS_ONCE(vcpu->kvm->arch.indirect_shadow_pages))
4276 return;
4277
4278 zap_page = remote_flush = local_flush = false;
4279
4280 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
4281
4282 gentry = mmu_pte_write_fetch_gpte(vcpu, &gpa, new, &bytes);
4283
4284 /*
4285 * No need to care whether allocation memory is successful
4286 * or not since pte prefetch is skiped if it does not have
4287 * enough objects in the cache.
4288 */
4289 mmu_topup_memory_caches(vcpu);
4290
4291 spin_lock(&vcpu->kvm->mmu_lock);
4292 ++vcpu->kvm->stat.mmu_pte_write;
Xiao Guangrong0375f7f2011-11-28 20:41:00 +08004293 kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE);
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004294
Sasha Levinb67bfe02013-02-27 17:06:00 -08004295 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn) {
Xiao Guangronga30f47c2011-09-22 16:58:36 +08004296 if (detect_write_misaligned(sp, gpa, bytes) ||
Xiao Guangronga138fe72011-12-16 18:18:10 +08004297 detect_write_flooding(sp)) {
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08004298 zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
Xiao Guangrongf41d3352010-06-04 21:56:11 +08004299 &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02004300 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08004301 continue;
4302 }
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004303
4304 spte = get_written_sptes(sp, gpa, &npte);
4305 if (!spte)
4306 continue;
4307
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08004308 local_flush = true;
Avi Kivityac1b7142007-03-08 17:13:32 +02004309 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02004310 entry = *spte;
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08004311 mmu_page_zap_pte(vcpu->kvm, sp, spte);
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08004312 if (gentry &&
4313 !((sp->role.word ^ vcpu->arch.mmu.base_role.word)
Xiao Guangrongf759e2b2011-09-22 16:53:17 +08004314 & mask.word) && rmap_can_add(vcpu))
Xiao Guangrong7c562522011-03-28 10:29:27 +08004315 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
Gleb Natapov9bb4f6b2013-01-30 16:45:01 +02004316 if (need_remote_flush(entry, *spte))
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08004317 remote_flush = true;
Avi Kivityac1b7142007-03-08 17:13:32 +02004318 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08004319 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08004320 }
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08004321 mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush);
Xiao Guangrongd98ba052010-06-04 21:55:29 +08004322 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Xiao Guangrong0375f7f2011-11-28 20:41:00 +08004323 kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05004324 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivityda4a00f2007-01-05 16:36:44 -08004325}
4326
Avi Kivitya4360362007-01-05 16:36:45 -08004327int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
4328{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004329 gpa_t gpa;
4330 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08004331
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02004332 if (vcpu->arch.mmu.direct_map)
Avi Kivity60f24782009-08-27 13:37:06 +03004333 return 0;
4334
Gleb Natapov1871c602010-02-10 14:21:32 +02004335 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004336
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004337 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004338
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004339 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08004340}
Avi Kivity577bdc42008-07-19 08:57:05 +03004341EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08004342
Takuya Yoshikawa81f4f762013-03-21 19:34:27 +09004343static void make_mmu_pages_available(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08004344{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08004345 LIST_HEAD(invalid_list);
Xiao Guangrong103ad252010-06-04 21:54:38 +08004346
Takuya Yoshikawa81f4f762013-03-21 19:34:27 +09004347 if (likely(kvm_mmu_available_pages(vcpu->kvm) >= KVM_MIN_FREE_MMU_PAGES))
4348 return;
4349
Takuya Yoshikawa5da59602013-03-06 16:06:58 +09004350 while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES) {
4351 if (!prepare_zap_oldest_mmu_page(vcpu->kvm, &invalid_list))
4352 break;
Avi Kivityebeace82007-01-05 16:36:47 -08004353
Avi Kivity4cee5762007-11-18 16:37:07 +02004354 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08004355 }
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08004356 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Avi Kivityebeace82007-01-05 16:36:47 -08004357}
Avi Kivityebeace82007-01-05 16:36:47 -08004358
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004359static bool is_mmio_page_fault(struct kvm_vcpu *vcpu, gva_t addr)
4360{
4361 if (vcpu->arch.mmu.direct_map || mmu_is_nested(vcpu))
4362 return vcpu_match_mmio_gpa(vcpu, addr);
4363
4364 return vcpu_match_mmio_gva(vcpu, addr);
4365}
4366
Andre Przywaradc25e892010-12-21 11:12:07 +01004367int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code,
4368 void *insn, int insn_len)
Avi Kivity30677142007-10-28 18:48:59 +02004369{
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004370 int r, emulation_type = EMULTYPE_RETRY;
Avi Kivity30677142007-10-28 18:48:59 +02004371 enum emulation_result er;
4372
Gleb Natapov56028d02010-10-17 18:13:42 +02004373 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code, false);
Avi Kivity30677142007-10-28 18:48:59 +02004374 if (r < 0)
4375 goto out;
4376
4377 if (!r) {
4378 r = 1;
4379 goto out;
4380 }
4381
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004382 if (is_mmio_page_fault(vcpu, cr2))
4383 emulation_type = 0;
4384
4385 er = x86_emulate_instruction(vcpu, cr2, emulation_type, insn, insn_len);
Avi Kivity30677142007-10-28 18:48:59 +02004386
4387 switch (er) {
4388 case EMULATE_DONE:
4389 return 1;
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02004390 case EMULATE_USER_EXIT:
Avi Kivity30677142007-10-28 18:48:59 +02004391 ++vcpu->stat.mmio_exits;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03004392 /* fall through */
Avi Kivity30677142007-10-28 18:48:59 +02004393 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03004394 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02004395 default:
4396 BUG();
4397 }
4398out:
Avi Kivity30677142007-10-28 18:48:59 +02004399 return r;
4400}
4401EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
4402
Marcelo Tosattia7052892008-09-23 13:18:35 -03004403void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
4404{
Marcelo Tosattia7052892008-09-23 13:18:35 -03004405 vcpu->arch.mmu.invlpg(vcpu, gva);
Liang Chen77c39132014-09-18 12:38:37 -04004406 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03004407 ++vcpu->stat.invlpg;
4408}
4409EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
4410
Joerg Roedel18552672008-02-07 13:47:41 +01004411void kvm_enable_tdp(void)
4412{
4413 tdp_enabled = true;
4414}
4415EXPORT_SYMBOL_GPL(kvm_enable_tdp);
4416
Joerg Roedel5f4cb662008-07-14 20:36:36 +02004417void kvm_disable_tdp(void)
4418{
4419 tdp_enabled = false;
4420}
4421EXPORT_SYMBOL_GPL(kvm_disable_tdp);
4422
Avi Kivity6aa8b732006-12-10 02:21:36 -08004423static void free_mmu_pages(struct kvm_vcpu *vcpu)
4424{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004425 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Joerg Roedel81407ca2010-09-10 17:31:00 +02004426 if (vcpu->arch.mmu.lm_root != NULL)
4427 free_page((unsigned long)vcpu->arch.mmu.lm_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004428}
4429
4430static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
4431{
Avi Kivity17ac10a2007-01-05 16:36:40 -08004432 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004433 int i;
4434
Avi Kivity17ac10a2007-01-05 16:36:40 -08004435 /*
4436 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
4437 * Therefore we need to allocate shadow page tables in the first
4438 * 4GB of memory, which happens to fit the DMA32 zone.
4439 */
4440 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
4441 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08004442 return -ENOMEM;
4443
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004444 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08004445 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004446 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08004447
Avi Kivity6aa8b732006-12-10 02:21:36 -08004448 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004449}
4450
Ingo Molnar8018c272006-12-29 16:50:01 -08004451int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004452{
Xiao Guangronge459e322011-11-28 20:42:16 +08004453 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
4454 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
4455 vcpu->arch.mmu.translate_gpa = translate_gpa;
4456 vcpu->arch.nested_mmu.translate_gpa = translate_nested_gpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004457
Ingo Molnar8018c272006-12-29 16:50:01 -08004458 return alloc_mmu_pages(vcpu);
4459}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004460
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004461void kvm_mmu_setup(struct kvm_vcpu *vcpu)
Ingo Molnar8018c272006-12-29 16:50:01 -08004462{
Paolo Bonzinifa4a2c02013-10-02 16:56:16 +02004463 MMU_WARN_ON(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08004464
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004465 init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004466}
4467
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08004468/* The return value indicates if tlb flush on all vcpus is needed. */
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09004469typedef bool (*slot_level_handler) (struct kvm *kvm, struct kvm_rmap_head *rmap_head);
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08004470
4471/* The caller should hold mmu-lock before calling this function. */
4472static bool
4473slot_handle_level_range(struct kvm *kvm, struct kvm_memory_slot *memslot,
4474 slot_level_handler fn, int start_level, int end_level,
4475 gfn_t start_gfn, gfn_t end_gfn, bool lock_flush_tlb)
4476{
4477 struct slot_rmap_walk_iterator iterator;
4478 bool flush = false;
4479
4480 for_each_slot_rmap_range(memslot, start_level, end_level, start_gfn,
4481 end_gfn, &iterator) {
4482 if (iterator.rmap)
4483 flush |= fn(kvm, iterator.rmap);
4484
4485 if (need_resched() || spin_needbreak(&kvm->mmu_lock)) {
4486 if (flush && lock_flush_tlb) {
4487 kvm_flush_remote_tlbs(kvm);
4488 flush = false;
4489 }
4490 cond_resched_lock(&kvm->mmu_lock);
4491 }
4492 }
4493
4494 if (flush && lock_flush_tlb) {
4495 kvm_flush_remote_tlbs(kvm);
4496 flush = false;
4497 }
4498
4499 return flush;
4500}
4501
4502static bool
4503slot_handle_level(struct kvm *kvm, struct kvm_memory_slot *memslot,
4504 slot_level_handler fn, int start_level, int end_level,
4505 bool lock_flush_tlb)
4506{
4507 return slot_handle_level_range(kvm, memslot, fn, start_level,
4508 end_level, memslot->base_gfn,
4509 memslot->base_gfn + memslot->npages - 1,
4510 lock_flush_tlb);
4511}
4512
4513static bool
4514slot_handle_all_level(struct kvm *kvm, struct kvm_memory_slot *memslot,
4515 slot_level_handler fn, bool lock_flush_tlb)
4516{
4517 return slot_handle_level(kvm, memslot, fn, PT_PAGE_TABLE_LEVEL,
4518 PT_MAX_HUGEPAGE_LEVEL, lock_flush_tlb);
4519}
4520
4521static bool
4522slot_handle_large_level(struct kvm *kvm, struct kvm_memory_slot *memslot,
4523 slot_level_handler fn, bool lock_flush_tlb)
4524{
4525 return slot_handle_level(kvm, memslot, fn, PT_PAGE_TABLE_LEVEL + 1,
4526 PT_MAX_HUGEPAGE_LEVEL, lock_flush_tlb);
4527}
4528
4529static bool
4530slot_handle_leaf(struct kvm *kvm, struct kvm_memory_slot *memslot,
4531 slot_level_handler fn, bool lock_flush_tlb)
4532{
4533 return slot_handle_level(kvm, memslot, fn, PT_PAGE_TABLE_LEVEL,
4534 PT_PAGE_TABLE_LEVEL, lock_flush_tlb);
4535}
4536
Xiao Guangrongefdfe532015-05-13 14:42:27 +08004537void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end)
4538{
4539 struct kvm_memslots *slots;
4540 struct kvm_memory_slot *memslot;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02004541 int i;
Xiao Guangrongefdfe532015-05-13 14:42:27 +08004542
4543 spin_lock(&kvm->mmu_lock);
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02004544 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
4545 slots = __kvm_memslots(kvm, i);
4546 kvm_for_each_memslot(memslot, slots) {
4547 gfn_t start, end;
Xiao Guangrongefdfe532015-05-13 14:42:27 +08004548
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02004549 start = max(gfn_start, memslot->base_gfn);
4550 end = min(gfn_end, memslot->base_gfn + memslot->npages);
4551 if (start >= end)
4552 continue;
Xiao Guangrongefdfe532015-05-13 14:42:27 +08004553
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02004554 slot_handle_level_range(kvm, memslot, kvm_zap_rmapp,
4555 PT_PAGE_TABLE_LEVEL, PT_MAX_HUGEPAGE_LEVEL,
4556 start, end - 1, true);
4557 }
Xiao Guangrongefdfe532015-05-13 14:42:27 +08004558 }
4559
4560 spin_unlock(&kvm->mmu_lock);
4561}
4562
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09004563static bool slot_rmap_write_protect(struct kvm *kvm,
4564 struct kvm_rmap_head *rmap_head)
Xiao Guangrongd77aa732015-05-13 14:42:24 +08004565{
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09004566 return __rmap_write_protect(kvm, rmap_head, false);
Xiao Guangrongd77aa732015-05-13 14:42:24 +08004567}
4568
Kai Huang1c91cad42015-01-28 10:54:26 +08004569void kvm_mmu_slot_remove_write_access(struct kvm *kvm,
4570 struct kvm_memory_slot *memslot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004571{
Xiao Guangrongd77aa732015-05-13 14:42:24 +08004572 bool flush;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004573
Takuya Yoshikawa9d1beef2013-01-08 19:46:48 +09004574 spin_lock(&kvm->mmu_lock);
Xiao Guangrongd77aa732015-05-13 14:42:24 +08004575 flush = slot_handle_all_level(kvm, memslot, slot_rmap_write_protect,
4576 false);
Takuya Yoshikawa9d1beef2013-01-08 19:46:48 +09004577 spin_unlock(&kvm->mmu_lock);
Xiao Guangrong198c74f2014-04-17 17:06:16 +08004578
4579 /*
4580 * kvm_mmu_slot_remove_write_access() and kvm_vm_ioctl_get_dirty_log()
4581 * which do tlb flush out of mmu-lock should be serialized by
4582 * kvm->slots_lock otherwise tlb flush would be missed.
4583 */
4584 lockdep_assert_held(&kvm->slots_lock);
4585
4586 /*
4587 * We can flush all the TLBs out of the mmu lock without TLB
4588 * corruption since we just change the spte from writable to
4589 * readonly so that we only need to care the case of changing
4590 * spte from present to present (changing the spte from present
4591 * to nonpresent will flush all the TLBs immediately), in other
4592 * words, the only case we care is mmu_spte_update() where we
4593 * haved checked SPTE_HOST_WRITEABLE | SPTE_MMU_WRITEABLE
4594 * instead of PT_WRITABLE_MASK, that means it does not depend
4595 * on PT_WRITABLE_MASK anymore.
4596 */
Kai Huangd91ffee2015-01-12 15:28:54 +08004597 if (flush)
4598 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004599}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08004600
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08004601static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm,
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09004602 struct kvm_rmap_head *rmap_head)
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08004603{
4604 u64 *sptep;
4605 struct rmap_iterator iter;
4606 int need_tlb_flush = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08004607 kvm_pfn_t pfn;
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08004608 struct kvm_mmu_page *sp;
4609
Xiao Guangrong0d536792015-05-13 14:42:20 +08004610restart:
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09004611 for_each_rmap_spte(rmap_head, &iter, sptep) {
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08004612 sp = page_header(__pa(sptep));
4613 pfn = spte_to_pfn(*sptep);
4614
4615 /*
Xiao Guangrongdecf6332015-04-14 12:04:10 +08004616 * We cannot do huge page mapping for indirect shadow pages,
4617 * which are found on the last rmap (level = 1) when not using
4618 * tdp; such shadow pages are synced with the page table in
4619 * the guest, and the guest page table is using 4K page size
4620 * mapping if the indirect sp has level = 1.
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08004621 */
4622 if (sp->role.direct &&
4623 !kvm_is_reserved_pfn(pfn) &&
4624 PageTransCompound(pfn_to_page(pfn))) {
4625 drop_spte(kvm, sptep);
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08004626 need_tlb_flush = 1;
Xiao Guangrong0d536792015-05-13 14:42:20 +08004627 goto restart;
4628 }
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08004629 }
4630
4631 return need_tlb_flush;
4632}
4633
4634void kvm_mmu_zap_collapsible_sptes(struct kvm *kvm,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02004635 const struct kvm_memory_slot *memslot)
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08004636{
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02004637 /* FIXME: const-ify all uses of struct kvm_memory_slot. */
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08004638 spin_lock(&kvm->mmu_lock);
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02004639 slot_handle_leaf(kvm, (struct kvm_memory_slot *)memslot,
4640 kvm_mmu_zap_collapsible_spte, true);
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08004641 spin_unlock(&kvm->mmu_lock);
4642}
4643
Kai Huangf4b4b182015-01-28 10:54:24 +08004644void kvm_mmu_slot_leaf_clear_dirty(struct kvm *kvm,
4645 struct kvm_memory_slot *memslot)
4646{
Xiao Guangrongd77aa732015-05-13 14:42:24 +08004647 bool flush;
Kai Huangf4b4b182015-01-28 10:54:24 +08004648
4649 spin_lock(&kvm->mmu_lock);
Xiao Guangrongd77aa732015-05-13 14:42:24 +08004650 flush = slot_handle_leaf(kvm, memslot, __rmap_clear_dirty, false);
Kai Huangf4b4b182015-01-28 10:54:24 +08004651 spin_unlock(&kvm->mmu_lock);
4652
4653 lockdep_assert_held(&kvm->slots_lock);
4654
4655 /*
4656 * It's also safe to flush TLBs out of mmu lock here as currently this
4657 * function is only used for dirty logging, in which case flushing TLB
4658 * out of mmu lock also guarantees no dirty pages will be lost in
4659 * dirty_bitmap.
4660 */
4661 if (flush)
4662 kvm_flush_remote_tlbs(kvm);
4663}
4664EXPORT_SYMBOL_GPL(kvm_mmu_slot_leaf_clear_dirty);
4665
4666void kvm_mmu_slot_largepage_remove_write_access(struct kvm *kvm,
4667 struct kvm_memory_slot *memslot)
4668{
Xiao Guangrongd77aa732015-05-13 14:42:24 +08004669 bool flush;
Kai Huangf4b4b182015-01-28 10:54:24 +08004670
4671 spin_lock(&kvm->mmu_lock);
Xiao Guangrongd77aa732015-05-13 14:42:24 +08004672 flush = slot_handle_large_level(kvm, memslot, slot_rmap_write_protect,
4673 false);
Kai Huangf4b4b182015-01-28 10:54:24 +08004674 spin_unlock(&kvm->mmu_lock);
4675
4676 /* see kvm_mmu_slot_remove_write_access */
4677 lockdep_assert_held(&kvm->slots_lock);
4678
4679 if (flush)
4680 kvm_flush_remote_tlbs(kvm);
4681}
4682EXPORT_SYMBOL_GPL(kvm_mmu_slot_largepage_remove_write_access);
4683
4684void kvm_mmu_slot_set_dirty(struct kvm *kvm,
4685 struct kvm_memory_slot *memslot)
4686{
Xiao Guangrongd77aa732015-05-13 14:42:24 +08004687 bool flush;
Kai Huangf4b4b182015-01-28 10:54:24 +08004688
4689 spin_lock(&kvm->mmu_lock);
Xiao Guangrongd77aa732015-05-13 14:42:24 +08004690 flush = slot_handle_all_level(kvm, memslot, __rmap_set_dirty, false);
Kai Huangf4b4b182015-01-28 10:54:24 +08004691 spin_unlock(&kvm->mmu_lock);
4692
4693 lockdep_assert_held(&kvm->slots_lock);
4694
4695 /* see kvm_mmu_slot_leaf_clear_dirty */
4696 if (flush)
4697 kvm_flush_remote_tlbs(kvm);
4698}
4699EXPORT_SYMBOL_GPL(kvm_mmu_slot_set_dirty);
4700
Xiao Guangronge7d11c72013-05-31 08:36:27 +08004701#define BATCH_ZAP_PAGES 10
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08004702static void kvm_zap_obsolete_pages(struct kvm *kvm)
4703{
4704 struct kvm_mmu_page *sp, *node;
Xiao Guangronge7d11c72013-05-31 08:36:27 +08004705 int batch = 0;
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08004706
4707restart:
4708 list_for_each_entry_safe_reverse(sp, node,
4709 &kvm->arch.active_mmu_pages, link) {
Xiao Guangronge7d11c72013-05-31 08:36:27 +08004710 int ret;
4711
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08004712 /*
4713 * No obsolete page exists before new created page since
4714 * active_mmu_pages is the FIFO list.
4715 */
4716 if (!is_obsolete_sp(kvm, sp))
4717 break;
4718
4719 /*
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08004720 * Since we are reversely walking the list and the invalid
4721 * list will be moved to the head, skip the invalid page
4722 * can help us to avoid the infinity list walking.
4723 */
4724 if (sp->role.invalid)
4725 continue;
4726
Xiao Guangrongf34d2512013-05-31 08:36:28 +08004727 /*
4728 * Need not flush tlb since we only zap the sp with invalid
4729 * generation number.
4730 */
Xiao Guangronge7d11c72013-05-31 08:36:27 +08004731 if (batch >= BATCH_ZAP_PAGES &&
Xiao Guangrongf34d2512013-05-31 08:36:28 +08004732 cond_resched_lock(&kvm->mmu_lock)) {
Xiao Guangronge7d11c72013-05-31 08:36:27 +08004733 batch = 0;
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08004734 goto restart;
4735 }
4736
Xiao Guangrong365c8862013-05-31 08:36:29 +08004737 ret = kvm_mmu_prepare_zap_page(kvm, sp,
4738 &kvm->arch.zapped_obsolete_pages);
Xiao Guangronge7d11c72013-05-31 08:36:27 +08004739 batch += ret;
4740
4741 if (ret)
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08004742 goto restart;
4743 }
4744
Xiao Guangrongf34d2512013-05-31 08:36:28 +08004745 /*
4746 * Should flush tlb before free page tables since lockless-walking
4747 * may use the pages.
4748 */
Xiao Guangrong365c8862013-05-31 08:36:29 +08004749 kvm_mmu_commit_zap_page(kvm, &kvm->arch.zapped_obsolete_pages);
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08004750}
4751
4752/*
4753 * Fast invalidate all shadow pages and use lock-break technique
4754 * to zap obsolete pages.
4755 *
4756 * It's required when memslot is being deleted or VM is being
4757 * destroyed, in these cases, we should ensure that KVM MMU does
4758 * not use any resource of the being-deleted slot or all slots
4759 * after calling the function.
4760 */
4761void kvm_mmu_invalidate_zap_all_pages(struct kvm *kvm)
4762{
4763 spin_lock(&kvm->mmu_lock);
Xiao Guangrong35006122013-05-31 08:36:25 +08004764 trace_kvm_mmu_invalidate_zap_all_pages(kvm);
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08004765 kvm->arch.mmu_valid_gen++;
4766
Xiao Guangrongf34d2512013-05-31 08:36:28 +08004767 /*
4768 * Notify all vcpus to reload its shadow page table
4769 * and flush TLB. Then all vcpus will switch to new
4770 * shadow page table with the new mmu_valid_gen.
4771 *
4772 * Note: we should do this under the protection of
4773 * mmu-lock, otherwise, vcpu would purge shadow page
4774 * but miss tlb flush.
4775 */
4776 kvm_reload_remote_mmus(kvm);
4777
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08004778 kvm_zap_obsolete_pages(kvm);
4779 spin_unlock(&kvm->mmu_lock);
4780}
4781
Xiao Guangrong365c8862013-05-31 08:36:29 +08004782static bool kvm_has_zapped_obsolete_pages(struct kvm *kvm)
4783{
4784 return unlikely(!list_empty_careful(&kvm->arch.zapped_obsolete_pages));
4785}
4786
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004787void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm, struct kvm_memslots *slots)
Xiao Guangrongf8f55942013-06-07 16:51:26 +08004788{
4789 /*
4790 * The very rare case: if the generation-number is round,
4791 * zap all shadow pages.
Xiao Guangrongf8f55942013-06-07 16:51:26 +08004792 */
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004793 if (unlikely((slots->generation & MMIO_GEN_MASK) == 0)) {
Paolo Bonzinia629df72014-12-22 10:43:39 +01004794 printk_ratelimited(KERN_DEBUG "kvm: zapping shadow pages for mmio generation wraparound\n");
Xiao Guangronga8eca9d2013-06-10 16:28:55 +08004795 kvm_mmu_invalidate_zap_all_pages(kvm);
Takuya Yoshikawa7a2e8aa2013-06-21 01:34:31 +09004796 }
Xiao Guangrongf8f55942013-06-07 16:51:26 +08004797}
4798
Dave Chinner70534a72013-08-28 10:18:14 +10004799static unsigned long
4800mmu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
Izik Eidus3ee16c82008-03-30 15:17:21 +03004801{
4802 struct kvm *kvm;
Ying Han1495f232011-05-24 17:12:27 -07004803 int nr_to_scan = sc->nr_to_scan;
Dave Chinner70534a72013-08-28 10:18:14 +10004804 unsigned long freed = 0;
Izik Eidus3ee16c82008-03-30 15:17:21 +03004805
Paolo Bonzini2f303b72013-09-25 13:53:07 +02004806 spin_lock(&kvm_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03004807
4808 list_for_each_entry(kvm, &vm_list, vm_list) {
Jan Kiszka3d56cbd2011-12-02 18:35:24 +01004809 int idx;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08004810 LIST_HEAD(invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03004811
Gleb Natapov19526392012-06-04 14:53:23 +03004812 /*
Takuya Yoshikawa35f2d162012-08-20 18:35:39 +09004813 * Never scan more than sc->nr_to_scan VM instances.
4814 * Will not hit this condition practically since we do not try
4815 * to shrink more than one VM and it is very unlikely to see
4816 * !n_used_mmu_pages so many times.
4817 */
4818 if (!nr_to_scan--)
4819 break;
4820 /*
Gleb Natapov19526392012-06-04 14:53:23 +03004821 * n_used_mmu_pages is accessed without holding kvm->mmu_lock
4822 * here. We may skip a VM instance errorneosly, but we do not
4823 * want to shrink a VM that only started to populate its MMU
4824 * anyway.
4825 */
Xiao Guangrong365c8862013-05-31 08:36:29 +08004826 if (!kvm->arch.n_used_mmu_pages &&
4827 !kvm_has_zapped_obsolete_pages(kvm))
Gleb Natapov19526392012-06-04 14:53:23 +03004828 continue;
Gleb Natapov19526392012-06-04 14:53:23 +03004829
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004830 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03004831 spin_lock(&kvm->mmu_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03004832
Xiao Guangrong365c8862013-05-31 08:36:29 +08004833 if (kvm_has_zapped_obsolete_pages(kvm)) {
4834 kvm_mmu_commit_zap_page(kvm,
4835 &kvm->arch.zapped_obsolete_pages);
4836 goto unlock;
4837 }
4838
Dave Chinner70534a72013-08-28 10:18:14 +10004839 if (prepare_zap_oldest_mmu_page(kvm, &invalid_list))
4840 freed++;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08004841 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Gleb Natapov19526392012-06-04 14:53:23 +03004842
Xiao Guangrong365c8862013-05-31 08:36:29 +08004843unlock:
Izik Eidus3ee16c82008-03-30 15:17:21 +03004844 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004845 srcu_read_unlock(&kvm->srcu, idx);
Gleb Natapov19526392012-06-04 14:53:23 +03004846
Dave Chinner70534a72013-08-28 10:18:14 +10004847 /*
4848 * unfair on small ones
4849 * per-vm shrinkers cry out
4850 * sadness comes quickly
4851 */
Gleb Natapov19526392012-06-04 14:53:23 +03004852 list_move_tail(&kvm->vm_list, &vm_list);
4853 break;
Izik Eidus3ee16c82008-03-30 15:17:21 +03004854 }
Izik Eidus3ee16c82008-03-30 15:17:21 +03004855
Paolo Bonzini2f303b72013-09-25 13:53:07 +02004856 spin_unlock(&kvm_lock);
Dave Chinner70534a72013-08-28 10:18:14 +10004857 return freed;
Dave Chinner70534a72013-08-28 10:18:14 +10004858}
4859
4860static unsigned long
4861mmu_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
4862{
Dave Hansen45221ab2010-08-19 18:11:37 -07004863 return percpu_counter_read_positive(&kvm_total_used_mmu_pages);
Izik Eidus3ee16c82008-03-30 15:17:21 +03004864}
4865
4866static struct shrinker mmu_shrinker = {
Dave Chinner70534a72013-08-28 10:18:14 +10004867 .count_objects = mmu_shrink_count,
4868 .scan_objects = mmu_shrink_scan,
Izik Eidus3ee16c82008-03-30 15:17:21 +03004869 .seeks = DEFAULT_SEEKS * 10,
4870};
4871
Ingo Molnar2ddfd202008-05-22 10:37:48 +02004872static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03004873{
Xiao Guangrong53c07b12011-05-15 23:26:20 +08004874 if (pte_list_desc_cache)
4875 kmem_cache_destroy(pte_list_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03004876 if (mmu_page_header_cache)
4877 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03004878}
4879
4880int kvm_mmu_module_init(void)
4881{
Xiao Guangrong53c07b12011-05-15 23:26:20 +08004882 pte_list_desc_cache = kmem_cache_create("pte_list_desc",
4883 sizeof(struct pte_list_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09004884 0, 0, NULL);
Xiao Guangrong53c07b12011-05-15 23:26:20 +08004885 if (!pte_list_desc_cache)
Avi Kivityb5a33a72007-04-15 16:31:09 +03004886 goto nomem;
4887
Avi Kivityd3d25b02007-05-30 12:34:53 +03004888 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
4889 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09004890 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03004891 if (!mmu_page_header_cache)
4892 goto nomem;
4893
Tejun Heo908c7f12014-09-08 09:51:29 +09004894 if (percpu_counter_init(&kvm_total_used_mmu_pages, 0, GFP_KERNEL))
Wei Yongjun45bf21a2010-08-23 16:13:15 +08004895 goto nomem;
4896
Izik Eidus3ee16c82008-03-30 15:17:21 +03004897 register_shrinker(&mmu_shrinker);
4898
Avi Kivityb5a33a72007-04-15 16:31:09 +03004899 return 0;
4900
4901nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03004902 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03004903 return -ENOMEM;
4904}
4905
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08004906/*
4907 * Caculate mmu pages needed for kvm.
4908 */
4909unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
4910{
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08004911 unsigned int nr_mmu_pages;
4912 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02004913 struct kvm_memslots *slots;
Xiao Guangrongbe6ba0f2011-11-24 17:39:18 +08004914 struct kvm_memory_slot *memslot;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02004915 int i;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08004916
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02004917 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
4918 slots = __kvm_memslots(kvm, i);
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08004919
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02004920 kvm_for_each_memslot(memslot, slots)
4921 nr_pages += memslot->npages;
4922 }
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08004923
4924 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
4925 nr_mmu_pages = max(nr_mmu_pages,
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02004926 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08004927
4928 return nr_mmu_pages;
4929}
4930
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08004931void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
4932{
Paolo Bonzini95f93af2013-10-02 16:56:12 +02004933 kvm_mmu_unload(vcpu);
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08004934 free_mmu_pages(vcpu);
4935 mmu_free_memory_caches(vcpu);
Xiao Guangrongb034cf02010-12-23 16:08:35 +08004936}
4937
Xiao Guangrongb034cf02010-12-23 16:08:35 +08004938void kvm_mmu_module_exit(void)
4939{
4940 mmu_destroy_caches();
4941 percpu_counter_destroy(&kvm_total_used_mmu_pages);
4942 unregister_shrinker(&mmu_shrinker);
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08004943 mmu_audit_disable();
4944}