blob: f2d1d230d5b8421827aa447984fb271360bfec00 [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>
Paul Gortmaker1767e932016-07-13 20:19:00 -040032#include <linux/moduleparam.h>
33#include <linux/export.h>
Izik Eidus448353c2007-11-26 14:08:14 +020034#include <linux/swap.h>
Marcelo Tosatti05da4552008-02-23 11:44:30 -030035#include <linux/hugetlb.h>
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050036#include <linux/compiler.h>
Marcelo Tosattibc6678a2009-12-23 14:35:21 -020037#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010039#include <linux/sched/signal.h>
Huang Yingbf998152010-05-31 14:28:19 +080040#include <linux/uaccess.h>
David Matlack114df302016-12-19 13:58:25 -080041#include <linux/hash.h>
Junaid Shahidf160c7b2016-12-06 16:46:16 -080042#include <linux/kern_levels.h>
Avi Kivitye4956062007-06-28 14:15:57 -040043
44#include <asm/page.h>
Haozhong Zhangaa2e0632017-12-20 15:29:29 +080045#include <asm/pat.h>
Avi Kivitye4956062007-06-28 14:15:57 -040046#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020047#include <asm/io.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020048#include <asm/vmx.h>
Xiao Guangrong3d0c27a2016-02-24 17:51:11 +080049#include <asm/kvm_page_track.h>
Wanpeng Li1261bfa2017-07-13 18:30:40 -070050#include "trace.h"
Avi Kivitye4956062007-06-28 14:15:57 -040051
Joerg Roedel18552672008-02-07 13:47:41 +010052/*
53 * When setting this variable to true it enables Two-Dimensional-Paging
54 * where the hardware walks 2 page tables:
55 * 1. the guest-virtual to guest-physical
56 * 2. while doing 1. it walks guest-physical to host-physical
57 * If the hardware supports that we don't need to do shadow paging.
58 */
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050059bool tdp_enabled = false;
Joerg Roedel18552672008-02-07 13:47:41 +010060
Xiao Guangrong8b1fe172010-08-30 18:22:53 +080061enum {
62 AUDIT_PRE_PAGE_FAULT,
63 AUDIT_POST_PAGE_FAULT,
64 AUDIT_PRE_PTE_WRITE,
Xiao Guangrong69030742010-09-27 18:09:29 +080065 AUDIT_POST_PTE_WRITE,
66 AUDIT_PRE_SYNC,
67 AUDIT_POST_SYNC
Xiao Guangrong8b1fe172010-08-30 18:22:53 +080068};
69
Avi Kivity37a7d8b2007-01-05 16:36:56 -080070#undef MMU_DEBUG
71
Avi Kivity37a7d8b2007-01-05 16:36:56 -080072#ifdef MMU_DEBUG
Paolo Bonzinifa4a2c02013-10-02 16:56:16 +020073static bool dbg = 0;
74module_param(dbg, bool, 0644);
Avi Kivity37a7d8b2007-01-05 16:36:56 -080075
76#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
77#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
Paolo Bonzinifa4a2c02013-10-02 16:56:16 +020078#define MMU_WARN_ON(x) WARN_ON(x)
Avi Kivity37a7d8b2007-01-05 16:36:56 -080079#else
Avi Kivity37a7d8b2007-01-05 16:36:56 -080080#define pgprintk(x...) do { } while (0)
81#define rmap_printk(x...) do { } while (0)
Paolo Bonzinifa4a2c02013-10-02 16:56:16 +020082#define MMU_WARN_ON(x) do { } while (0)
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080083#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080084
Xiao Guangrong957ed9e2010-08-22 19:12:48 +080085#define PTE_PREFETCH_NUM 8
86
Xudong Hao00763e42012-06-07 18:26:07 +080087#define PT_FIRST_AVAIL_BITS_SHIFT 10
Avi Kivity6aa8b732006-12-10 02:21:36 -080088#define PT64_SECOND_AVAIL_BITS_SHIFT 52
89
Avi Kivity6aa8b732006-12-10 02:21:36 -080090#define PT64_LEVEL_BITS 9
91
92#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -040093 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080094
Avi Kivity6aa8b732006-12-10 02:21:36 -080095#define PT64_INDEX(address, level)\
96 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
97
98
99#define PT32_LEVEL_BITS 10
100
101#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400102 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800103
Joerg Roedele04da982009-07-27 16:30:45 +0200104#define PT32_LVL_OFFSET_MASK(level) \
105 (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
106 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800107
108#define PT32_INDEX(address, level)\
109 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
110
111
Tom Lendackyd0ec49d2017-07-17 16:10:27 -0500112#define PT64_BASE_ADDR_MASK __sme_clr((((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1)))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800113#define PT64_DIR_BASE_ADDR_MASK \
114 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200115#define PT64_LVL_ADDR_MASK(level) \
116 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
117 * PT64_LEVEL_BITS))) - 1))
118#define PT64_LVL_OFFSET_MASK(level) \
119 (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
120 * PT64_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800121
122#define PT32_BASE_ADDR_MASK PAGE_MASK
123#define PT32_DIR_BASE_ADDR_MASK \
124 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200125#define PT32_LVL_ADDR_MASK(level) \
126 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
127 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800128
Gleb Natapov53166222013-08-05 11:07:14 +0300129#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | shadow_user_mask \
Tom Lendackyd0ec49d2017-07-17 16:10:27 -0500130 | shadow_x_mask | shadow_nx_mask | shadow_me_mask)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131
Avi Kivityfe135d22007-12-09 16:15:46 +0200132#define ACC_EXEC_MASK 1
133#define ACC_WRITE_MASK PT_WRITABLE_MASK
134#define ACC_USER_MASK PT_USER_MASK
135#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
136
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800137/* The mask for the R/X bits in EPT PTEs */
138#define PT64_EPT_READABLE_MASK 0x1ull
139#define PT64_EPT_EXECUTABLE_MASK 0x4ull
140
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200141#include <trace/events/kvm.h>
142
Avi Kivity07420172009-07-06 12:21:32 +0300143#define CREATE_TRACE_POINTS
144#include "mmutrace.h"
145
Xiao Guangrong49fde342012-06-20 15:58:58 +0800146#define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
147#define SPTE_MMU_WRITEABLE (1ULL << (PT_FIRST_AVAIL_BITS_SHIFT + 1))
Izik Eidus14032832009-09-23 21:47:17 +0300148
Avi Kivity135f8c22008-08-21 17:49:56 +0300149#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
150
Takuya Yoshikawa220f7732012-03-21 23:49:39 +0900151/* make pte_list_desc fit well in cache line */
152#define PTE_LIST_EXT 3
153
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +0200154/*
155 * Return values of handle_mmio_page_fault and mmu.page_fault:
156 * RET_PF_RETRY: let CPU fault again on the address.
157 * RET_PF_EMULATE: mmio page fault, emulate the instruction directly.
158 *
159 * For handle_mmio_page_fault only:
160 * RET_PF_INVALID: the spte is invalid, let the real page fault path update it.
161 */
162enum {
163 RET_PF_RETRY = 0,
164 RET_PF_EMULATE = 1,
165 RET_PF_INVALID = 2,
166};
167
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800168struct pte_list_desc {
169 u64 *sptes[PTE_LIST_EXT];
170 struct pte_list_desc *more;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800171};
172
Avi Kivity2d111232008-12-25 14:39:47 +0200173struct kvm_shadow_walk_iterator {
174 u64 addr;
175 hpa_t shadow_addr;
Avi Kivity2d111232008-12-25 14:39:47 +0200176 u64 *sptep;
Xiao Guangrongdd3bfd52011-07-12 03:32:54 +0800177 int level;
Avi Kivity2d111232008-12-25 14:39:47 +0200178 unsigned index;
179};
180
Junaid Shahid9fa72112018-06-27 14:59:07 -0700181static const union kvm_mmu_page_role mmu_base_role_mask = {
182 .cr0_wp = 1,
183 .cr4_pae = 1,
184 .nxe = 1,
185 .smep_andnot_wp = 1,
186 .smap_andnot_wp = 1,
187 .smm = 1,
188 .guest_mode = 1,
189 .ad_disabled = 1,
190};
191
Junaid Shahid7eb77e92018-06-27 14:59:16 -0700192#define for_each_shadow_entry_using_root(_vcpu, _root, _addr, _walker) \
193 for (shadow_walk_init_using_root(&(_walker), (_vcpu), \
194 (_root), (_addr)); \
195 shadow_walk_okay(&(_walker)); \
196 shadow_walk_next(&(_walker)))
197
198#define for_each_shadow_entry(_vcpu, _addr, _walker) \
Avi Kivity2d111232008-12-25 14:39:47 +0200199 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
200 shadow_walk_okay(&(_walker)); \
201 shadow_walk_next(&(_walker)))
202
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800203#define for_each_shadow_entry_lockless(_vcpu, _addr, _walker, spte) \
204 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
205 shadow_walk_okay(&(_walker)) && \
206 ({ spte = mmu_spte_get_lockless(_walker.sptep); 1; }); \
207 __shadow_walk_next(&(_walker), spte))
208
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800209static struct kmem_cache *pte_list_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300210static struct kmem_cache *mmu_page_header_cache;
Dave Hansen45221ab2010-08-19 18:11:37 -0700211static struct percpu_counter kvm_total_used_mmu_pages;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300212
Sheng Yang7b523452008-04-25 21:13:50 +0800213static u64 __read_mostly shadow_nx_mask;
214static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
215static u64 __read_mostly shadow_user_mask;
216static u64 __read_mostly shadow_accessed_mask;
217static u64 __read_mostly shadow_dirty_mask;
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800218static u64 __read_mostly shadow_mmio_mask;
Peter Feinerdcdca5f2017-06-30 17:26:30 -0700219static u64 __read_mostly shadow_mmio_value;
Bandan Dasffb128c2016-07-12 18:18:49 -0400220static u64 __read_mostly shadow_present_mask;
Tom Lendackyd0ec49d2017-07-17 16:10:27 -0500221static u64 __read_mostly shadow_me_mask;
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800222
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800223/*
Peter Feinerac8d57e2017-06-30 17:26:31 -0700224 * SPTEs used by MMUs without A/D bits are marked with shadow_acc_track_value.
225 * Non-present SPTEs with shadow_acc_track_value set are in place for access
226 * tracking.
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800227 */
228static u64 __read_mostly shadow_acc_track_mask;
229static const u64 shadow_acc_track_value = SPTE_SPECIAL_MASK;
230
231/*
232 * The mask/shift to use for saving the original R/X bits when marking the PTE
233 * as not-present for access tracking purposes. We do not save the W bit as the
234 * PTEs being access tracked also need to be dirty tracked, so the W bit will be
235 * restored only when a write is attempted to the page.
236 */
237static const u64 shadow_acc_track_saved_bits_mask = PT64_EPT_READABLE_MASK |
238 PT64_EPT_EXECUTABLE_MASK;
239static const u64 shadow_acc_track_saved_bits_shift = PT64_SECOND_AVAIL_BITS_SHIFT;
240
Junaid Shahid28a1f3a2018-08-14 10:15:34 -0700241/*
242 * This mask must be set on all non-zero Non-Present or Reserved SPTEs in order
243 * to guard against L1TF attacks.
244 */
245static u64 __read_mostly shadow_nonpresent_or_rsvd_mask;
246
247/*
248 * The number of high-order 1 bits to use in the mask above.
249 */
250static const u64 shadow_nonpresent_or_rsvd_mask_len = 5;
251
Sean Christophersondaa07cb2018-09-25 13:20:00 -0700252/*
253 * In some cases, we need to preserve the GFN of a non-present or reserved
254 * SPTE when we usurp the upper five bits of the physical address space to
255 * defend against L1TF, e.g. for MMIO SPTEs. To preserve the GFN, we'll
256 * shift bits of the GFN that overlap with shadow_nonpresent_or_rsvd_mask
257 * left into the reserved bits, i.e. the GFN in the SPTE will be split into
258 * high and low parts. This mask covers the lower bits of the GFN.
259 */
260static u64 __read_mostly shadow_nonpresent_or_rsvd_lower_gfn_mask;
261
262
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800263static void mmu_spte_set(u64 *sptep, u64 spte);
Junaid Shahid9fa72112018-06-27 14:59:07 -0700264static union kvm_mmu_page_role
265kvm_mmu_calc_root_page_role(struct kvm_vcpu *vcpu);
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800266
Lan Tianyu40ef75a2018-12-06 21:21:08 +0800267
268static inline bool kvm_available_flush_tlb_with_range(void)
269{
270 return kvm_x86_ops->tlb_remote_flush_with_range;
271}
272
273static void kvm_flush_remote_tlbs_with_range(struct kvm *kvm,
274 struct kvm_tlb_range *range)
275{
276 int ret = -ENOTSUPP;
277
278 if (range && kvm_x86_ops->tlb_remote_flush_with_range)
279 ret = kvm_x86_ops->tlb_remote_flush_with_range(kvm, range);
280
281 if (ret)
282 kvm_flush_remote_tlbs(kvm);
283}
284
285static void kvm_flush_remote_tlbs_with_address(struct kvm *kvm,
286 u64 start_gfn, u64 pages)
287{
288 struct kvm_tlb_range range;
289
290 range.start_gfn = start_gfn;
291 range.pages = pages;
292
293 kvm_flush_remote_tlbs_with_range(kvm, &range);
294}
295
Peter Feinerdcdca5f2017-06-30 17:26:30 -0700296void kvm_mmu_set_mmio_spte_mask(u64 mmio_mask, u64 mmio_value)
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800297{
Peter Feinerdcdca5f2017-06-30 17:26:30 -0700298 BUG_ON((mmio_mask & mmio_value) != mmio_value);
299 shadow_mmio_value = mmio_value | SPTE_SPECIAL_MASK;
Junaid Shahid312b6162016-12-21 20:29:29 -0800300 shadow_mmio_mask = mmio_mask | SPTE_SPECIAL_MASK;
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800301}
302EXPORT_SYMBOL_GPL(kvm_mmu_set_mmio_spte_mask);
303
Peter Feinerac8d57e2017-06-30 17:26:31 -0700304static inline bool sp_ad_disabled(struct kvm_mmu_page *sp)
305{
306 return sp->role.ad_disabled;
307}
308
309static inline bool spte_ad_enabled(u64 spte)
310{
311 MMU_WARN_ON((spte & shadow_mmio_mask) == shadow_mmio_value);
312 return !(spte & shadow_acc_track_value);
313}
314
315static inline u64 spte_shadow_accessed_mask(u64 spte)
316{
317 MMU_WARN_ON((spte & shadow_mmio_mask) == shadow_mmio_value);
318 return spte_ad_enabled(spte) ? shadow_accessed_mask : 0;
319}
320
321static inline u64 spte_shadow_dirty_mask(u64 spte)
322{
323 MMU_WARN_ON((spte & shadow_mmio_mask) == shadow_mmio_value);
324 return spte_ad_enabled(spte) ? shadow_dirty_mask : 0;
325}
326
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800327static inline bool is_access_track_spte(u64 spte)
328{
Peter Feinerac8d57e2017-06-30 17:26:31 -0700329 return !spte_ad_enabled(spte) && (spte & shadow_acc_track_mask) == 0;
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800330}
331
Xiao Guangrongf2fd1252013-06-07 16:51:24 +0800332/*
David Matlackee3d1572014-08-18 15:46:06 -0700333 * the low bit of the generation number is always presumed to be zero.
334 * This disables mmio caching during memslot updates. The concept is
335 * similar to a seqcount but instead of retrying the access we just punt
336 * and ignore the cache.
337 *
338 * spte bits 3-11 are used as bits 1-9 of the generation number,
339 * the bits 52-61 are used as bits 10-19 of the generation number.
Xiao Guangrongf2fd1252013-06-07 16:51:24 +0800340 */
David Matlackee3d1572014-08-18 15:46:06 -0700341#define MMIO_SPTE_GEN_LOW_SHIFT 2
Xiao Guangrongf2fd1252013-06-07 16:51:24 +0800342#define MMIO_SPTE_GEN_HIGH_SHIFT 52
343
David Matlackee3d1572014-08-18 15:46:06 -0700344#define MMIO_GEN_SHIFT 20
345#define MMIO_GEN_LOW_SHIFT 10
346#define MMIO_GEN_LOW_MASK ((1 << MMIO_GEN_LOW_SHIFT) - 2)
Xiao Guangrongf8f55942013-06-07 16:51:26 +0800347#define MMIO_GEN_MASK ((1 << MMIO_GEN_SHIFT) - 1)
Xiao Guangrongf2fd1252013-06-07 16:51:24 +0800348
349static u64 generation_mmio_spte_mask(unsigned int gen)
350{
351 u64 mask;
352
Tiejun Chen842bb262014-11-18 17:14:38 +0800353 WARN_ON(gen & ~MMIO_GEN_MASK);
Xiao Guangrongf2fd1252013-06-07 16:51:24 +0800354
355 mask = (gen & MMIO_GEN_LOW_MASK) << MMIO_SPTE_GEN_LOW_SHIFT;
356 mask |= ((u64)gen >> MMIO_GEN_LOW_SHIFT) << MMIO_SPTE_GEN_HIGH_SHIFT;
357 return mask;
358}
359
360static unsigned int get_mmio_spte_generation(u64 spte)
361{
362 unsigned int gen;
363
364 spte &= ~shadow_mmio_mask;
365
366 gen = (spte >> MMIO_SPTE_GEN_LOW_SHIFT) & MMIO_GEN_LOW_MASK;
367 gen |= (spte >> MMIO_SPTE_GEN_HIGH_SHIFT) << MMIO_GEN_LOW_SHIFT;
368 return gen;
369}
370
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200371static unsigned int kvm_current_mmio_generation(struct kvm_vcpu *vcpu)
Xiao Guangrongf8f55942013-06-07 16:51:26 +0800372{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200373 return kvm_vcpu_memslots(vcpu)->generation & MMIO_GEN_MASK;
Xiao Guangrongf8f55942013-06-07 16:51:26 +0800374}
375
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200376static void mark_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, u64 gfn,
Xiao Guangrongf2fd1252013-06-07 16:51:24 +0800377 unsigned access)
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800378{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200379 unsigned int gen = kvm_current_mmio_generation(vcpu);
Xiao Guangrongf8f55942013-06-07 16:51:26 +0800380 u64 mask = generation_mmio_spte_mask(gen);
Junaid Shahid28a1f3a2018-08-14 10:15:34 -0700381 u64 gpa = gfn << PAGE_SHIFT;
Takuya Yoshikawa95b04302013-03-12 17:44:40 +0900382
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800383 access &= ACC_WRITE_MASK | ACC_USER_MASK;
Junaid Shahid28a1f3a2018-08-14 10:15:34 -0700384 mask |= shadow_mmio_value | access;
385 mask |= gpa | shadow_nonpresent_or_rsvd_mask;
386 mask |= (gpa & shadow_nonpresent_or_rsvd_mask)
387 << shadow_nonpresent_or_rsvd_mask_len;
Xiao Guangrongf2fd1252013-06-07 16:51:24 +0800388
Xiao Guangrongf8f55942013-06-07 16:51:26 +0800389 trace_mark_mmio_spte(sptep, gfn, access, gen);
Xiao Guangrongf2fd1252013-06-07 16:51:24 +0800390 mmu_spte_set(sptep, mask);
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800391}
392
393static bool is_mmio_spte(u64 spte)
394{
Peter Feinerdcdca5f2017-06-30 17:26:30 -0700395 return (spte & shadow_mmio_mask) == shadow_mmio_value;
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800396}
397
398static gfn_t get_mmio_spte_gfn(u64 spte)
399{
Sean Christophersondaa07cb2018-09-25 13:20:00 -0700400 u64 gpa = spte & shadow_nonpresent_or_rsvd_lower_gfn_mask;
Junaid Shahid28a1f3a2018-08-14 10:15:34 -0700401
402 gpa |= (spte >> shadow_nonpresent_or_rsvd_mask_len)
403 & shadow_nonpresent_or_rsvd_mask;
404
405 return gpa >> PAGE_SHIFT;
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800406}
407
408static unsigned get_mmio_spte_access(u64 spte)
409{
Tiejun Chen842bb262014-11-18 17:14:38 +0800410 u64 mask = generation_mmio_spte_mask(MMIO_GEN_MASK) | shadow_mmio_mask;
Xiao Guangrongf2fd1252013-06-07 16:51:24 +0800411 return (spte & ~mask) & ~PAGE_MASK;
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800412}
413
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200414static bool set_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, gfn_t gfn,
Dan Williamsba049e92016-01-15 16:56:11 -0800415 kvm_pfn_t pfn, unsigned access)
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800416{
417 if (unlikely(is_noslot_pfn(pfn))) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200418 mark_mmio_spte(vcpu, sptep, gfn, access);
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800419 return true;
420 }
421
422 return false;
423}
Avi Kivityc7addb92007-09-16 18:58:32 +0200424
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200425static bool check_mmio_spte(struct kvm_vcpu *vcpu, u64 spte)
Xiao Guangrongf8f55942013-06-07 16:51:26 +0800426{
Xiao Guangrong089504c2013-06-07 16:51:27 +0800427 unsigned int kvm_gen, spte_gen;
428
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200429 kvm_gen = kvm_current_mmio_generation(vcpu);
Xiao Guangrong089504c2013-06-07 16:51:27 +0800430 spte_gen = get_mmio_spte_generation(spte);
431
432 trace_check_mmio_spte(spte, kvm_gen, spte_gen);
433 return likely(kvm_gen == spte_gen);
Xiao Guangrongf8f55942013-06-07 16:51:26 +0800434}
435
Peter Feinerce000532017-06-30 17:26:29 -0700436/*
437 * Sets the shadow PTE masks used by the MMU.
438 *
439 * Assumptions:
440 * - Setting either @accessed_mask or @dirty_mask requires setting both
441 * - At least one of @accessed_mask or @acc_track_mask must be set
442 */
Sheng Yang7b523452008-04-25 21:13:50 +0800443void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800444 u64 dirty_mask, u64 nx_mask, u64 x_mask, u64 p_mask,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -0500445 u64 acc_track_mask, u64 me_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800446{
Peter Feinerce000532017-06-30 17:26:29 -0700447 BUG_ON(!dirty_mask != !accessed_mask);
448 BUG_ON(!accessed_mask && !acc_track_mask);
Peter Feinerac8d57e2017-06-30 17:26:31 -0700449 BUG_ON(acc_track_mask & shadow_acc_track_value);
Junaid Shahid312b6162016-12-21 20:29:29 -0800450
Sheng Yang7b523452008-04-25 21:13:50 +0800451 shadow_user_mask = user_mask;
452 shadow_accessed_mask = accessed_mask;
453 shadow_dirty_mask = dirty_mask;
454 shadow_nx_mask = nx_mask;
455 shadow_x_mask = x_mask;
Bandan Dasffb128c2016-07-12 18:18:49 -0400456 shadow_present_mask = p_mask;
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800457 shadow_acc_track_mask = acc_track_mask;
Tom Lendackyd0ec49d2017-07-17 16:10:27 -0500458 shadow_me_mask = me_mask;
Sheng Yang7b523452008-04-25 21:13:50 +0800459}
460EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
461
Junaid Shahid28a1f3a2018-08-14 10:15:34 -0700462static void kvm_mmu_reset_all_pte_masks(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800463{
Sean Christophersondaa07cb2018-09-25 13:20:00 -0700464 u8 low_phys_bits;
465
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800466 shadow_user_mask = 0;
467 shadow_accessed_mask = 0;
468 shadow_dirty_mask = 0;
469 shadow_nx_mask = 0;
470 shadow_x_mask = 0;
471 shadow_mmio_mask = 0;
472 shadow_present_mask = 0;
473 shadow_acc_track_mask = 0;
Junaid Shahid28a1f3a2018-08-14 10:15:34 -0700474
475 /*
476 * If the CPU has 46 or less physical address bits, then set an
477 * appropriate mask to guard against L1TF attacks. Otherwise, it is
478 * assumed that the CPU is not vulnerable to L1TF.
479 */
Sean Christophersondaa07cb2018-09-25 13:20:00 -0700480 low_phys_bits = boot_cpu_data.x86_phys_bits;
Junaid Shahid28a1f3a2018-08-14 10:15:34 -0700481 if (boot_cpu_data.x86_phys_bits <
Sean Christophersondaa07cb2018-09-25 13:20:00 -0700482 52 - shadow_nonpresent_or_rsvd_mask_len) {
Junaid Shahid28a1f3a2018-08-14 10:15:34 -0700483 shadow_nonpresent_or_rsvd_mask =
484 rsvd_bits(boot_cpu_data.x86_phys_bits -
485 shadow_nonpresent_or_rsvd_mask_len,
486 boot_cpu_data.x86_phys_bits - 1);
Sean Christophersondaa07cb2018-09-25 13:20:00 -0700487 low_phys_bits -= shadow_nonpresent_or_rsvd_mask_len;
488 }
489 shadow_nonpresent_or_rsvd_lower_gfn_mask =
490 GENMASK_ULL(low_phys_bits - 1, PAGE_SHIFT);
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800491}
492
Avi Kivity6aa8b732006-12-10 02:21:36 -0800493static int is_cpuid_PSE36(void)
494{
495 return 1;
496}
497
Avi Kivity73b10872007-01-26 00:56:41 -0800498static int is_nx(struct kvm_vcpu *vcpu)
499{
Avi Kivityf6801df2010-01-21 15:31:50 +0200500 return vcpu->arch.efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800501}
502
Avi Kivityc7addb92007-09-16 18:58:32 +0200503static int is_shadow_present_pte(u64 pte)
504{
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800505 return (pte != 0) && !is_mmio_spte(pte);
Avi Kivityc7addb92007-09-16 18:58:32 +0200506}
507
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300508static int is_large_pte(u64 pte)
509{
510 return pte & PT_PAGE_SIZE_MASK;
511}
512
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300513static int is_last_spte(u64 pte, int level)
514{
515 if (level == PT_PAGE_TABLE_LEVEL)
516 return 1;
Joerg Roedel852e3c12009-07-27 16:30:44 +0200517 if (is_large_pte(pte))
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300518 return 1;
519 return 0;
520}
521
Junaid Shahidd3e328f22016-12-21 20:29:32 -0800522static bool is_executable_pte(u64 spte)
523{
524 return (spte & (shadow_x_mask | shadow_nx_mask)) == shadow_x_mask;
525}
526
Dan Williamsba049e92016-01-15 16:56:11 -0800527static kvm_pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200528{
Anthony Liguori35149e22008-04-02 14:46:56 -0500529 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200530}
531
Avi Kivityda9285212007-11-21 13:54:47 +0200532static gfn_t pse36_gfn_delta(u32 gpte)
533{
534 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
535
536 return (gpte & PT32_DIR_PSE36_MASK) << shift;
537}
538
Xiao Guangrong603e0652011-07-12 03:31:28 +0800539#ifdef CONFIG_X86_64
Avi Kivityd555c332009-06-10 14:24:23 +0300540static void __set_spte(u64 *sptep, u64 spte)
Avi Kivitye663ee62007-05-31 15:46:04 +0300541{
Nadav Amitb19ee2f2016-05-11 08:04:29 -0700542 WRITE_ONCE(*sptep, spte);
Avi Kivitye663ee62007-05-31 15:46:04 +0300543}
544
Xiao Guangrong603e0652011-07-12 03:31:28 +0800545static void __update_clear_spte_fast(u64 *sptep, u64 spte)
Avi Kivitya9221dd2010-06-06 14:48:06 +0300546{
Nadav Amitb19ee2f2016-05-11 08:04:29 -0700547 WRITE_ONCE(*sptep, spte);
Avi Kivitya9221dd2010-06-06 14:48:06 +0300548}
549
Xiao Guangrong603e0652011-07-12 03:31:28 +0800550static u64 __update_clear_spte_slow(u64 *sptep, u64 spte)
551{
552 return xchg(sptep, spte);
553}
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800554
555static u64 __get_spte_lockless(u64 *sptep)
556{
Mark Rutland6aa7de02017-10-23 14:07:29 -0700557 return READ_ONCE(*sptep);
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800558}
Xiao Guangrong603e0652011-07-12 03:31:28 +0800559#else
560union split_spte {
561 struct {
562 u32 spte_low;
563 u32 spte_high;
564 };
565 u64 spte;
566};
567
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800568static void count_spte_clear(u64 *sptep, u64 spte)
569{
570 struct kvm_mmu_page *sp = page_header(__pa(sptep));
571
572 if (is_shadow_present_pte(spte))
573 return;
574
575 /* Ensure the spte is completely set before we increase the count */
576 smp_wmb();
577 sp->clear_spte_count++;
578}
579
Xiao Guangrong603e0652011-07-12 03:31:28 +0800580static void __set_spte(u64 *sptep, u64 spte)
581{
582 union split_spte *ssptep, sspte;
583
584 ssptep = (union split_spte *)sptep;
585 sspte = (union split_spte)spte;
586
587 ssptep->spte_high = sspte.spte_high;
588
589 /*
590 * If we map the spte from nonpresent to present, We should store
591 * the high bits firstly, then set present bit, so cpu can not
592 * fetch this spte while we are setting the spte.
593 */
594 smp_wmb();
595
Nadav Amitb19ee2f2016-05-11 08:04:29 -0700596 WRITE_ONCE(ssptep->spte_low, sspte.spte_low);
Xiao Guangrong603e0652011-07-12 03:31:28 +0800597}
598
599static void __update_clear_spte_fast(u64 *sptep, u64 spte)
600{
601 union split_spte *ssptep, sspte;
602
603 ssptep = (union split_spte *)sptep;
604 sspte = (union split_spte)spte;
605
Nadav Amitb19ee2f2016-05-11 08:04:29 -0700606 WRITE_ONCE(ssptep->spte_low, sspte.spte_low);
Xiao Guangrong603e0652011-07-12 03:31:28 +0800607
608 /*
609 * If we map the spte from present to nonpresent, we should clear
610 * present bit firstly to avoid vcpu fetch the old high bits.
611 */
612 smp_wmb();
613
614 ssptep->spte_high = sspte.spte_high;
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800615 count_spte_clear(sptep, spte);
Xiao Guangrong603e0652011-07-12 03:31:28 +0800616}
617
618static u64 __update_clear_spte_slow(u64 *sptep, u64 spte)
619{
620 union split_spte *ssptep, sspte, orig;
621
622 ssptep = (union split_spte *)sptep;
623 sspte = (union split_spte)spte;
624
625 /* xchg acts as a barrier before the setting of the high bits */
626 orig.spte_low = xchg(&ssptep->spte_low, sspte.spte_low);
Zhao Jin41bc3182011-09-19 12:19:51 +0800627 orig.spte_high = ssptep->spte_high;
628 ssptep->spte_high = sspte.spte_high;
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800629 count_spte_clear(sptep, spte);
Xiao Guangrong603e0652011-07-12 03:31:28 +0800630
631 return orig.spte;
632}
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800633
634/*
635 * The idea using the light way get the spte on x86_32 guest is from
636 * gup_get_pte(arch/x86/mm/gup.c).
Xiao Guangrongaccaefe2013-06-19 17:09:20 +0800637 *
638 * An spte tlb flush may be pending, because kvm_set_pte_rmapp
639 * coalesces them and we are running out of the MMU lock. Therefore
640 * we need to protect against in-progress updates of the spte.
641 *
642 * Reading the spte while an update is in progress may get the old value
643 * for the high part of the spte. The race is fine for a present->non-present
644 * change (because the high part of the spte is ignored for non-present spte),
645 * but for a present->present change we must reread the spte.
646 *
647 * All such changes are done in two steps (present->non-present and
648 * non-present->present), hence it is enough to count the number of
649 * present->non-present updates: if it changed while reading the spte,
650 * we might have hit the race. This is done using clear_spte_count.
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800651 */
652static u64 __get_spte_lockless(u64 *sptep)
653{
654 struct kvm_mmu_page *sp = page_header(__pa(sptep));
655 union split_spte spte, *orig = (union split_spte *)sptep;
656 int count;
657
658retry:
659 count = sp->clear_spte_count;
660 smp_rmb();
661
662 spte.spte_low = orig->spte_low;
663 smp_rmb();
664
665 spte.spte_high = orig->spte_high;
666 smp_rmb();
667
668 if (unlikely(spte.spte_low != orig->spte_low ||
669 count != sp->clear_spte_count))
670 goto retry;
671
672 return spte.spte;
673}
Xiao Guangrong603e0652011-07-12 03:31:28 +0800674#endif
675
Junaid Shahidea4114b2016-12-06 16:46:11 -0800676static bool spte_can_locklessly_be_made_writable(u64 spte)
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +0800677{
Gleb Natapovfeb3eb72013-01-30 16:45:00 +0200678 return (spte & (SPTE_HOST_WRITEABLE | SPTE_MMU_WRITEABLE)) ==
679 (SPTE_HOST_WRITEABLE | SPTE_MMU_WRITEABLE);
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +0800680}
681
Xiao Guangrong8672b722010-08-02 16:14:04 +0800682static bool spte_has_volatile_bits(u64 spte)
683{
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800684 if (!is_shadow_present_pte(spte))
685 return false;
686
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +0800687 /*
Adam Buchbinder6a6256f2016-02-23 15:34:30 -0800688 * Always atomically update spte if it can be updated
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +0800689 * out of mmu-lock, it can ensure dirty bit is not lost,
690 * also, it can help us to get a stable is_writable_pte()
691 * to ensure tlb flush is not missed.
692 */
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800693 if (spte_can_locklessly_be_made_writable(spte) ||
694 is_access_track_spte(spte))
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +0800695 return true;
696
Peter Feinerac8d57e2017-06-30 17:26:31 -0700697 if (spte_ad_enabled(spte)) {
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800698 if ((spte & shadow_accessed_mask) == 0 ||
699 (is_writable_pte(spte) && (spte & shadow_dirty_mask) == 0))
700 return true;
701 }
Xiao Guangrong8672b722010-08-02 16:14:04 +0800702
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800703 return false;
Xiao Guangrong8672b722010-08-02 16:14:04 +0800704}
705
Junaid Shahid83ef6c82016-12-06 16:46:13 -0800706static bool is_accessed_spte(u64 spte)
Xiao Guangrong41327792010-08-02 16:15:08 +0800707{
Peter Feinerac8d57e2017-06-30 17:26:31 -0700708 u64 accessed_mask = spte_shadow_accessed_mask(spte);
709
710 return accessed_mask ? spte & accessed_mask
711 : !is_access_track_spte(spte);
Xiao Guangrong41327792010-08-02 16:15:08 +0800712}
713
Junaid Shahid83ef6c82016-12-06 16:46:13 -0800714static bool is_dirty_spte(u64 spte)
Kai Huang7e71a592015-01-09 16:44:30 +0800715{
Peter Feinerac8d57e2017-06-30 17:26:31 -0700716 u64 dirty_mask = spte_shadow_dirty_mask(spte);
717
718 return dirty_mask ? spte & dirty_mask : spte & PT_WRITABLE_MASK;
Kai Huang7e71a592015-01-09 16:44:30 +0800719}
720
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800721/* Rules for using mmu_spte_set:
722 * Set the sptep from nonpresent to present.
723 * Note: the sptep being assigned *must* be either not present
724 * or in a state where the hardware will not attempt to update
725 * the spte.
726 */
727static void mmu_spte_set(u64 *sptep, u64 new_spte)
728{
729 WARN_ON(is_shadow_present_pte(*sptep));
730 __set_spte(sptep, new_spte);
731}
732
Junaid Shahidf39a0582016-12-06 16:46:14 -0800733/*
734 * Update the SPTE (excluding the PFN), but do not track changes in its
735 * accessed/dirty status.
736 */
737static u64 mmu_spte_update_no_track(u64 *sptep, u64 new_spte)
738{
739 u64 old_spte = *sptep;
740
741 WARN_ON(!is_shadow_present_pte(new_spte));
742
743 if (!is_shadow_present_pte(old_spte)) {
744 mmu_spte_set(sptep, new_spte);
745 return old_spte;
746 }
747
748 if (!spte_has_volatile_bits(old_spte))
749 __update_clear_spte_fast(sptep, new_spte);
750 else
751 old_spte = __update_clear_spte_slow(sptep, new_spte);
752
753 WARN_ON(spte_to_pfn(old_spte) != spte_to_pfn(new_spte));
754
755 return old_spte;
756}
757
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800758/* Rules for using mmu_spte_update:
Andrea Gelminibb3541f2016-05-21 14:14:44 +0200759 * Update the state bits, it means the mapped pfn is not changed.
Xiao Guangrong6e7d0352012-06-20 15:58:33 +0800760 *
761 * Whenever we overwrite a writable spte with a read-only one we
762 * should flush remote TLBs. Otherwise rmap_write_protect
763 * will find a read-only spte, even though the writable spte
764 * might be cached on a CPU's TLB, the return value indicates this
765 * case.
Junaid Shahid83ef6c82016-12-06 16:46:13 -0800766 *
767 * Returns true if the TLB needs to be flushed
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800768 */
Xiao Guangrong6e7d0352012-06-20 15:58:33 +0800769static bool mmu_spte_update(u64 *sptep, u64 new_spte)
Avi Kivityb79b93f2010-06-06 15:46:44 +0300770{
Junaid Shahid83ef6c82016-12-06 16:46:13 -0800771 bool flush = false;
Junaid Shahidf39a0582016-12-06 16:46:14 -0800772 u64 old_spte = mmu_spte_update_no_track(sptep, new_spte);
Avi Kivityb79b93f2010-06-06 15:46:44 +0300773
Junaid Shahidf39a0582016-12-06 16:46:14 -0800774 if (!is_shadow_present_pte(old_spte))
775 return false;
Junaid Shahid83ef6c82016-12-06 16:46:13 -0800776
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +0800777 /*
778 * For the spte updated out of mmu-lock is safe, since
Adam Buchbinder6a6256f2016-02-23 15:34:30 -0800779 * we always atomically update it, see the comments in
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +0800780 * spte_has_volatile_bits().
781 */
Junaid Shahidea4114b2016-12-06 16:46:11 -0800782 if (spte_can_locklessly_be_made_writable(old_spte) &&
Xiao Guangrong7f31c952014-04-17 17:06:15 +0800783 !is_writable_pte(new_spte))
Junaid Shahid83ef6c82016-12-06 16:46:13 -0800784 flush = true;
Xiao Guangrong41327792010-08-02 16:15:08 +0800785
Kai Huang7e71a592015-01-09 16:44:30 +0800786 /*
Junaid Shahid83ef6c82016-12-06 16:46:13 -0800787 * Flush TLB when accessed/dirty states are changed in the page tables,
Kai Huang7e71a592015-01-09 16:44:30 +0800788 * to guarantee consistency between TLB and page tables.
789 */
Kai Huang7e71a592015-01-09 16:44:30 +0800790
Junaid Shahid83ef6c82016-12-06 16:46:13 -0800791 if (is_accessed_spte(old_spte) && !is_accessed_spte(new_spte)) {
792 flush = true;
Xiao Guangrong41327792010-08-02 16:15:08 +0800793 kvm_set_pfn_accessed(spte_to_pfn(old_spte));
Junaid Shahid83ef6c82016-12-06 16:46:13 -0800794 }
Xiao Guangrong6e7d0352012-06-20 15:58:33 +0800795
Junaid Shahid83ef6c82016-12-06 16:46:13 -0800796 if (is_dirty_spte(old_spte) && !is_dirty_spte(new_spte)) {
797 flush = true;
798 kvm_set_pfn_dirty(spte_to_pfn(old_spte));
799 }
800
801 return flush;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300802}
803
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800804/*
805 * Rules for using mmu_spte_clear_track_bits:
806 * It sets the sptep from present to nonpresent, and track the
807 * state bits, it is used to clear the last level sptep.
Junaid Shahid83ef6c82016-12-06 16:46:13 -0800808 * Returns non-zero if the PTE was previously valid.
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800809 */
810static int mmu_spte_clear_track_bits(u64 *sptep)
811{
Dan Williamsba049e92016-01-15 16:56:11 -0800812 kvm_pfn_t pfn;
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800813 u64 old_spte = *sptep;
814
815 if (!spte_has_volatile_bits(old_spte))
Xiao Guangrong603e0652011-07-12 03:31:28 +0800816 __update_clear_spte_fast(sptep, 0ull);
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800817 else
Xiao Guangrong603e0652011-07-12 03:31:28 +0800818 old_spte = __update_clear_spte_slow(sptep, 0ull);
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800819
Takuya Yoshikawaafd28fe2015-11-20 17:44:55 +0900820 if (!is_shadow_present_pte(old_spte))
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800821 return 0;
822
823 pfn = spte_to_pfn(old_spte);
Xiao Guangrong86fde742012-07-17 21:52:52 +0800824
825 /*
826 * KVM does not hold the refcount of the page used by
827 * kvm mmu, before reclaiming the page, we should
828 * unmap it from mmu first.
829 */
Ard Biesheuvelbf4bea82014-11-10 08:33:56 +0000830 WARN_ON(!kvm_is_reserved_pfn(pfn) && !page_count(pfn_to_page(pfn)));
Xiao Guangrong86fde742012-07-17 21:52:52 +0800831
Junaid Shahid83ef6c82016-12-06 16:46:13 -0800832 if (is_accessed_spte(old_spte))
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800833 kvm_set_pfn_accessed(pfn);
Junaid Shahid83ef6c82016-12-06 16:46:13 -0800834
835 if (is_dirty_spte(old_spte))
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800836 kvm_set_pfn_dirty(pfn);
Junaid Shahid83ef6c82016-12-06 16:46:13 -0800837
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800838 return 1;
839}
840
841/*
842 * Rules for using mmu_spte_clear_no_track:
843 * Directly clear spte without caring the state bits of sptep,
844 * it is used to set the upper level spte.
845 */
846static void mmu_spte_clear_no_track(u64 *sptep)
847{
Xiao Guangrong603e0652011-07-12 03:31:28 +0800848 __update_clear_spte_fast(sptep, 0ull);
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800849}
850
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800851static u64 mmu_spte_get_lockless(u64 *sptep)
852{
853 return __get_spte_lockless(sptep);
854}
855
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800856static u64 mark_spte_for_access_track(u64 spte)
857{
Peter Feinerac8d57e2017-06-30 17:26:31 -0700858 if (spte_ad_enabled(spte))
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800859 return spte & ~shadow_accessed_mask;
860
Peter Feinerac8d57e2017-06-30 17:26:31 -0700861 if (is_access_track_spte(spte))
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800862 return spte;
863
864 /*
Junaid Shahid20d65232016-12-21 20:29:31 -0800865 * Making an Access Tracking PTE will result in removal of write access
866 * from the PTE. So, verify that we will be able to restore the write
867 * access in the fast page fault path later on.
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800868 */
869 WARN_ONCE((spte & PT_WRITABLE_MASK) &&
870 !spte_can_locklessly_be_made_writable(spte),
871 "kvm: Writable SPTE is not locklessly dirty-trackable\n");
872
873 WARN_ONCE(spte & (shadow_acc_track_saved_bits_mask <<
874 shadow_acc_track_saved_bits_shift),
875 "kvm: Access Tracking saved bit locations are not zero\n");
876
877 spte |= (spte & shadow_acc_track_saved_bits_mask) <<
878 shadow_acc_track_saved_bits_shift;
879 spte &= ~shadow_acc_track_mask;
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800880
881 return spte;
882}
883
Junaid Shahidd3e328f22016-12-21 20:29:32 -0800884/* Restore an acc-track PTE back to a regular PTE */
885static u64 restore_acc_track_spte(u64 spte)
886{
887 u64 new_spte = spte;
888 u64 saved_bits = (spte >> shadow_acc_track_saved_bits_shift)
889 & shadow_acc_track_saved_bits_mask;
890
Peter Feinerac8d57e2017-06-30 17:26:31 -0700891 WARN_ON_ONCE(spte_ad_enabled(spte));
Junaid Shahidd3e328f22016-12-21 20:29:32 -0800892 WARN_ON_ONCE(!is_access_track_spte(spte));
893
894 new_spte &= ~shadow_acc_track_mask;
895 new_spte &= ~(shadow_acc_track_saved_bits_mask <<
896 shadow_acc_track_saved_bits_shift);
897 new_spte |= saved_bits;
898
899 return new_spte;
900}
901
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800902/* Returns the Accessed status of the PTE and resets it at the same time. */
903static bool mmu_spte_age(u64 *sptep)
904{
905 u64 spte = mmu_spte_get_lockless(sptep);
906
907 if (!is_accessed_spte(spte))
908 return false;
909
Peter Feinerac8d57e2017-06-30 17:26:31 -0700910 if (spte_ad_enabled(spte)) {
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800911 clear_bit((ffs(shadow_accessed_mask) - 1),
912 (unsigned long *)sptep);
913 } else {
914 /*
915 * Capture the dirty status of the page, so that it doesn't get
916 * lost when the SPTE is marked for access tracking.
917 */
918 if (is_writable_pte(spte))
919 kvm_set_pfn_dirty(spte_to_pfn(spte));
920
921 spte = mark_spte_for_access_track(spte);
922 mmu_spte_update_no_track(sptep, spte);
923 }
924
925 return true;
926}
927
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800928static void walk_shadow_page_lockless_begin(struct kvm_vcpu *vcpu)
929{
Avi Kivityc1427862012-05-14 15:44:06 +0300930 /*
931 * Prevent page table teardown by making any free-er wait during
932 * kvm_flush_remote_tlbs() IPI to all active vcpus.
933 */
934 local_irq_disable();
Lan Tianyu36ca7e02016-03-13 11:10:25 +0800935
Avi Kivityc1427862012-05-14 15:44:06 +0300936 /*
937 * Make sure a following spte read is not reordered ahead of the write
938 * to vcpu->mode.
939 */
Lan Tianyu36ca7e02016-03-13 11:10:25 +0800940 smp_store_mb(vcpu->mode, READING_SHADOW_PAGE_TABLES);
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800941}
942
943static void walk_shadow_page_lockless_end(struct kvm_vcpu *vcpu)
944{
Avi Kivityc1427862012-05-14 15:44:06 +0300945 /*
946 * Make sure the write to vcpu->mode is not reordered in front of
Tianyu Lan9a984582018-09-07 05:45:02 +0000947 * reads to sptes. If it does, kvm_mmu_commit_zap_page() can see us
Avi Kivityc1427862012-05-14 15:44:06 +0300948 * OUTSIDE_GUEST_MODE and proceed to free the shadow page table.
949 */
Lan Tianyu36ca7e02016-03-13 11:10:25 +0800950 smp_store_release(&vcpu->mode, OUTSIDE_GUEST_MODE);
Avi Kivityc1427862012-05-14 15:44:06 +0300951 local_irq_enable();
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800952}
953
Avi Kivitye2dec932007-01-05 16:36:54 -0800954static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300955 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800956{
957 void *obj;
958
959 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800960 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800961 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300962 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800963 if (!obj)
Wei Yangdaefb792018-09-04 23:57:32 +0800964 return cache->nobjs >= min ? 0 : -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800965 cache->objects[cache->nobjs++] = obj;
966 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800967 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800968}
969
Xiao Guangrongf759e2b2011-09-22 16:53:17 +0800970static int mmu_memory_cache_free_objects(struct kvm_mmu_memory_cache *cache)
971{
972 return cache->nobjs;
973}
974
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800975static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc,
976 struct kmem_cache *cache)
Avi Kivity714b93d2007-01-05 16:36:53 -0800977{
978 while (mc->nobjs)
Xiao Guangronge8ad9a72010-05-13 10:06:02 +0800979 kmem_cache_free(cache, mc->objects[--mc->nobjs]);
Avi Kivity714b93d2007-01-05 16:36:53 -0800980}
981
Avi Kivityc1158e62007-07-20 08:18:27 +0300982static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300983 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300984{
Xiao Guangrong842f22e2011-03-04 19:01:10 +0800985 void *page;
Avi Kivityc1158e62007-07-20 08:18:27 +0300986
987 if (cache->nobjs >= min)
988 return 0;
989 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Shakeel Buttd97e5e62018-07-26 16:37:45 -0700990 page = (void *)__get_free_page(GFP_KERNEL_ACCOUNT);
Avi Kivityc1158e62007-07-20 08:18:27 +0300991 if (!page)
Wei Yangdaefb792018-09-04 23:57:32 +0800992 return cache->nobjs >= min ? 0 : -ENOMEM;
Xiao Guangrong842f22e2011-03-04 19:01:10 +0800993 cache->objects[cache->nobjs++] = page;
Avi Kivityc1158e62007-07-20 08:18:27 +0300994 }
995 return 0;
996}
997
998static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
999{
1000 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +03001001 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +03001002}
1003
Avi Kivity8c438502007-04-16 11:53:17 +03001004static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
1005{
1006 int r;
1007
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001008 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
Xiao Guangrong67052b32011-05-15 23:27:08 +08001009 pte_list_desc_cache, 8 + PTE_PREFETCH_NUM);
Avi Kivity2e3e5882007-09-10 11:28:17 +03001010 if (r)
1011 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001012 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +03001013 if (r)
1014 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001015 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +03001016 mmu_page_header_cache, 4);
1017out:
Avi Kivity8c438502007-04-16 11:53:17 +03001018 return r;
1019}
1020
Avi Kivity714b93d2007-01-05 16:36:53 -08001021static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
1022{
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001023 mmu_free_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
1024 pte_list_desc_cache);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001025 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
Xiao Guangronge8ad9a72010-05-13 10:06:02 +08001026 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache,
1027 mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -08001028}
1029
Takuya Yoshikawa80feb892012-05-29 23:54:26 +09001030static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc)
Avi Kivity714b93d2007-01-05 16:36:53 -08001031{
1032 void *p;
1033
1034 BUG_ON(!mc->nobjs);
1035 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -08001036 return p;
1037}
1038
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001039static struct pte_list_desc *mmu_alloc_pte_list_desc(struct kvm_vcpu *vcpu)
Avi Kivity714b93d2007-01-05 16:36:53 -08001040{
Takuya Yoshikawa80feb892012-05-29 23:54:26 +09001041 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_list_desc_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -08001042}
1043
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001044static void mmu_free_pte_list_desc(struct pte_list_desc *pte_list_desc)
Avi Kivity714b93d2007-01-05 16:36:53 -08001045{
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001046 kmem_cache_free(pte_list_desc_cache, pte_list_desc);
Avi Kivity714b93d2007-01-05 16:36:53 -08001047}
1048
Lai Jiangshan2032a932010-05-26 16:49:59 +08001049static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
1050{
1051 if (!sp->role.direct)
1052 return sp->gfns[index];
1053
1054 return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS));
1055}
1056
1057static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn)
1058{
1059 if (sp->role.direct)
1060 BUG_ON(gfn != kvm_mmu_page_get_gfn(sp, index));
1061 else
1062 sp->gfns[index] = gfn;
1063}
1064
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001065/*
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +09001066 * Return the pointer to the large page information for a given gfn,
1067 * handling slots that are not large page aligned.
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001068 */
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +09001069static struct kvm_lpage_info *lpage_info_slot(gfn_t gfn,
1070 struct kvm_memory_slot *slot,
1071 int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001072{
1073 unsigned long idx;
1074
Takuya Yoshikawafb03cb62012-02-08 12:59:10 +09001075 idx = gfn_to_index(gfn, slot->base_gfn, level);
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09001076 return &slot->arch.lpage_info[level - 2][idx];
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001077}
1078
Xiao Guangrong547ffae2016-02-24 17:51:07 +08001079static void update_gfn_disallow_lpage_count(struct kvm_memory_slot *slot,
1080 gfn_t gfn, int count)
1081{
1082 struct kvm_lpage_info *linfo;
1083 int i;
1084
1085 for (i = PT_DIRECTORY_LEVEL; i <= PT_MAX_HUGEPAGE_LEVEL; ++i) {
1086 linfo = lpage_info_slot(gfn, slot, i);
1087 linfo->disallow_lpage += count;
1088 WARN_ON(linfo->disallow_lpage < 0);
1089 }
1090}
1091
1092void kvm_mmu_gfn_disallow_lpage(struct kvm_memory_slot *slot, gfn_t gfn)
1093{
1094 update_gfn_disallow_lpage_count(slot, gfn, 1);
1095}
1096
1097void kvm_mmu_gfn_allow_lpage(struct kvm_memory_slot *slot, gfn_t gfn)
1098{
1099 update_gfn_disallow_lpage_count(slot, gfn, -1);
1100}
1101
Paolo Bonzini3ed1a472015-05-19 16:29:22 +02001102static void account_shadowed(struct kvm *kvm, struct kvm_mmu_page *sp)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001103{
Paolo Bonzini699023e2015-05-18 15:03:39 +02001104 struct kvm_memslots *slots;
Joerg Roedeld25797b2009-07-27 16:30:43 +02001105 struct kvm_memory_slot *slot;
Paolo Bonzini3ed1a472015-05-19 16:29:22 +02001106 gfn_t gfn;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001107
Xiao Guangrong56ca57f2016-02-24 17:51:14 +08001108 kvm->arch.indirect_shadow_pages++;
Paolo Bonzini3ed1a472015-05-19 16:29:22 +02001109 gfn = sp->gfn;
Paolo Bonzini699023e2015-05-18 15:03:39 +02001110 slots = kvm_memslots_for_spte_role(kvm, sp->role);
1111 slot = __gfn_to_memslot(slots, gfn);
Xiao Guangrong56ca57f2016-02-24 17:51:14 +08001112
1113 /* the non-leaf shadow pages are keeping readonly. */
1114 if (sp->role.level > PT_PAGE_TABLE_LEVEL)
1115 return kvm_slot_page_track_add_page(kvm, slot, gfn,
1116 KVM_PAGE_TRACK_WRITE);
1117
Xiao Guangrong547ffae2016-02-24 17:51:07 +08001118 kvm_mmu_gfn_disallow_lpage(slot, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001119}
1120
Paolo Bonzini3ed1a472015-05-19 16:29:22 +02001121static void unaccount_shadowed(struct kvm *kvm, struct kvm_mmu_page *sp)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001122{
Paolo Bonzini699023e2015-05-18 15:03:39 +02001123 struct kvm_memslots *slots;
Joerg Roedeld25797b2009-07-27 16:30:43 +02001124 struct kvm_memory_slot *slot;
Paolo Bonzini3ed1a472015-05-19 16:29:22 +02001125 gfn_t gfn;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001126
Xiao Guangrong56ca57f2016-02-24 17:51:14 +08001127 kvm->arch.indirect_shadow_pages--;
Paolo Bonzini3ed1a472015-05-19 16:29:22 +02001128 gfn = sp->gfn;
Paolo Bonzini699023e2015-05-18 15:03:39 +02001129 slots = kvm_memslots_for_spte_role(kvm, sp->role);
1130 slot = __gfn_to_memslot(slots, gfn);
Xiao Guangrong56ca57f2016-02-24 17:51:14 +08001131 if (sp->role.level > PT_PAGE_TABLE_LEVEL)
1132 return kvm_slot_page_track_remove_page(kvm, slot, gfn,
1133 KVM_PAGE_TRACK_WRITE);
1134
Xiao Guangrong547ffae2016-02-24 17:51:07 +08001135 kvm_mmu_gfn_allow_lpage(slot, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001136}
1137
Xiao Guangrong92f94f12016-02-24 17:51:06 +08001138static bool __mmu_gfn_lpage_is_disallowed(gfn_t gfn, int level,
1139 struct kvm_memory_slot *slot)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001140{
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +09001141 struct kvm_lpage_info *linfo;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001142
1143 if (slot) {
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +09001144 linfo = lpage_info_slot(gfn, slot, level);
Xiao Guangrong92f94f12016-02-24 17:51:06 +08001145 return !!linfo->disallow_lpage;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001146 }
1147
Xiao Guangrong92f94f12016-02-24 17:51:06 +08001148 return true;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001149}
1150
Xiao Guangrong92f94f12016-02-24 17:51:06 +08001151static bool mmu_gfn_lpage_is_disallowed(struct kvm_vcpu *vcpu, gfn_t gfn,
1152 int level)
Takuya Yoshikawa5225fdf2015-10-16 17:08:03 +09001153{
1154 struct kvm_memory_slot *slot;
1155
1156 slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
Xiao Guangrong92f94f12016-02-24 17:51:06 +08001157 return __mmu_gfn_lpage_is_disallowed(gfn, level, slot);
Takuya Yoshikawa5225fdf2015-10-16 17:08:03 +09001158}
1159
Joerg Roedeld25797b2009-07-27 16:30:43 +02001160static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001161{
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +01001162 unsigned long page_size;
Joerg Roedeld25797b2009-07-27 16:30:43 +02001163 int i, ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001164
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +01001165 page_size = kvm_host_page_size(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001166
Xiao Guangrong8a3d08f2015-05-13 14:42:21 +08001167 for (i = PT_PAGE_TABLE_LEVEL; i <= PT_MAX_HUGEPAGE_LEVEL; ++i) {
Joerg Roedeld25797b2009-07-27 16:30:43 +02001168 if (page_size >= KVM_HPAGE_SIZE(i))
1169 ret = i;
1170 else
1171 break;
1172 }
1173
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001174 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001175}
1176
Takuya Yoshikawad8aacf52015-10-16 17:07:01 +09001177static inline bool memslot_valid_for_gpte(struct kvm_memory_slot *slot,
1178 bool no_dirty_log)
1179{
1180 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
1181 return false;
1182 if (no_dirty_log && slot->dirty_bitmap)
1183 return false;
1184
1185 return true;
1186}
1187
Xiao Guangrong5d163b12011-03-09 15:43:00 +08001188static struct kvm_memory_slot *
1189gfn_to_memslot_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t gfn,
1190 bool no_dirty_log)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001191{
1192 struct kvm_memory_slot *slot;
Xiao Guangrong5d163b12011-03-09 15:43:00 +08001193
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02001194 slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
Takuya Yoshikawad8aacf52015-10-16 17:07:01 +09001195 if (!memslot_valid_for_gpte(slot, no_dirty_log))
Xiao Guangrong5d163b12011-03-09 15:43:00 +08001196 slot = NULL;
1197
1198 return slot;
1199}
1200
Takuya Yoshikawafd136902015-10-16 17:06:02 +09001201static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn,
1202 bool *force_pt_level)
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08001203{
1204 int host_level, level, max_level;
Takuya Yoshikawad8aacf52015-10-16 17:07:01 +09001205 struct kvm_memory_slot *slot;
1206
Takuya Yoshikawa8c85ac12015-10-19 15:13:29 +09001207 if (unlikely(*force_pt_level))
1208 return PT_PAGE_TABLE_LEVEL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001209
Takuya Yoshikawa8c85ac12015-10-19 15:13:29 +09001210 slot = kvm_vcpu_gfn_to_memslot(vcpu, large_gfn);
1211 *force_pt_level = !memslot_valid_for_gpte(slot, true);
Takuya Yoshikawafd136902015-10-16 17:06:02 +09001212 if (unlikely(*force_pt_level))
1213 return PT_PAGE_TABLE_LEVEL;
1214
Joerg Roedeld25797b2009-07-27 16:30:43 +02001215 host_level = host_mapping_level(vcpu->kvm, large_gfn);
1216
1217 if (host_level == PT_PAGE_TABLE_LEVEL)
1218 return host_level;
1219
Xiao Guangrong55dd98c2013-02-05 15:26:54 +08001220 max_level = min(kvm_x86_ops->get_lpage_level(), host_level);
Sheng Yang878403b2010-01-05 19:02:29 +08001221
1222 for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
Xiao Guangrong92f94f12016-02-24 17:51:06 +08001223 if (__mmu_gfn_lpage_is_disallowed(large_gfn, level, slot))
Joerg Roedeld25797b2009-07-27 16:30:43 +02001224 break;
Joerg Roedeld25797b2009-07-27 16:30:43 +02001225
1226 return level - 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001227}
1228
1229/*
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001230 * About rmap_head encoding:
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001231 *
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001232 * If the bit zero of rmap_head->val is clear, then it points to the only spte
1233 * in this rmap chain. Otherwise, (rmap_head->val & ~1) points to a struct
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001234 * pte_list_desc containing more mappings.
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001235 */
1236
1237/*
1238 * Returns the number of pointers in the rmap chain, not counting the new one.
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001239 */
1240static int pte_list_add(struct kvm_vcpu *vcpu, u64 *spte,
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001241 struct kvm_rmap_head *rmap_head)
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001242{
1243 struct pte_list_desc *desc;
1244 int i, count = 0;
1245
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001246 if (!rmap_head->val) {
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001247 rmap_printk("pte_list_add: %p %llx 0->1\n", spte, *spte);
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001248 rmap_head->val = (unsigned long)spte;
1249 } else if (!(rmap_head->val & 1)) {
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001250 rmap_printk("pte_list_add: %p %llx 1->many\n", spte, *spte);
1251 desc = mmu_alloc_pte_list_desc(vcpu);
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001252 desc->sptes[0] = (u64 *)rmap_head->val;
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001253 desc->sptes[1] = spte;
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001254 rmap_head->val = (unsigned long)desc | 1;
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001255 ++count;
1256 } else {
1257 rmap_printk("pte_list_add: %p %llx many->many\n", spte, *spte);
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001258 desc = (struct pte_list_desc *)(rmap_head->val & ~1ul);
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001259 while (desc->sptes[PTE_LIST_EXT-1] && desc->more) {
1260 desc = desc->more;
1261 count += PTE_LIST_EXT;
1262 }
1263 if (desc->sptes[PTE_LIST_EXT-1]) {
1264 desc->more = mmu_alloc_pte_list_desc(vcpu);
1265 desc = desc->more;
1266 }
1267 for (i = 0; desc->sptes[i]; ++i)
1268 ++count;
1269 desc->sptes[i] = spte;
1270 }
1271 return count;
1272}
1273
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001274static void
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001275pte_list_desc_remove_entry(struct kvm_rmap_head *rmap_head,
1276 struct pte_list_desc *desc, int i,
1277 struct pte_list_desc *prev_desc)
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001278{
1279 int j;
1280
1281 for (j = PTE_LIST_EXT - 1; !desc->sptes[j] && j > i; --j)
1282 ;
1283 desc->sptes[i] = desc->sptes[j];
1284 desc->sptes[j] = NULL;
1285 if (j != 0)
1286 return;
1287 if (!prev_desc && !desc->more)
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001288 rmap_head->val = (unsigned long)desc->sptes[0];
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001289 else
1290 if (prev_desc)
1291 prev_desc->more = desc->more;
1292 else
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001293 rmap_head->val = (unsigned long)desc->more | 1;
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001294 mmu_free_pte_list_desc(desc);
1295}
1296
Wei Yang8daf3462018-10-04 10:04:22 +08001297static void __pte_list_remove(u64 *spte, struct kvm_rmap_head *rmap_head)
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001298{
1299 struct pte_list_desc *desc;
1300 struct pte_list_desc *prev_desc;
1301 int i;
1302
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001303 if (!rmap_head->val) {
Wei Yang8daf3462018-10-04 10:04:22 +08001304 pr_err("%s: %p 0->BUG\n", __func__, spte);
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001305 BUG();
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001306 } else if (!(rmap_head->val & 1)) {
Wei Yang8daf3462018-10-04 10:04:22 +08001307 rmap_printk("%s: %p 1->0\n", __func__, spte);
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001308 if ((u64 *)rmap_head->val != spte) {
Wei Yang8daf3462018-10-04 10:04:22 +08001309 pr_err("%s: %p 1->BUG\n", __func__, spte);
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001310 BUG();
1311 }
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001312 rmap_head->val = 0;
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001313 } else {
Wei Yang8daf3462018-10-04 10:04:22 +08001314 rmap_printk("%s: %p many->many\n", __func__, spte);
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001315 desc = (struct pte_list_desc *)(rmap_head->val & ~1ul);
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001316 prev_desc = NULL;
1317 while (desc) {
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001318 for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i) {
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001319 if (desc->sptes[i] == spte) {
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001320 pte_list_desc_remove_entry(rmap_head,
1321 desc, i, prev_desc);
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001322 return;
1323 }
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001324 }
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001325 prev_desc = desc;
1326 desc = desc->more;
1327 }
Wei Yang8daf3462018-10-04 10:04:22 +08001328 pr_err("%s: %p many->many\n", __func__, spte);
Xiao Guangrong53c07b12011-05-15 23:26:20 +08001329 BUG();
1330 }
1331}
1332
Wei Yange7912382018-10-04 10:04:23 +08001333static void pte_list_remove(struct kvm_rmap_head *rmap_head, u64 *sptep)
1334{
1335 mmu_spte_clear_track_bits(sptep);
1336 __pte_list_remove(sptep, rmap_head);
1337}
1338
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001339static struct kvm_rmap_head *__gfn_to_rmap(gfn_t gfn, int level,
1340 struct kvm_memory_slot *slot)
Takuya Yoshikawa9b9b1492011-11-14 18:22:28 +09001341{
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09001342 unsigned long idx;
Takuya Yoshikawa9b9b1492011-11-14 18:22:28 +09001343
Takuya Yoshikawa77d11302012-07-02 17:57:17 +09001344 idx = gfn_to_index(gfn, slot->base_gfn, level);
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09001345 return &slot->arch.rmap[level - PT_PAGE_TABLE_LEVEL][idx];
Takuya Yoshikawa9b9b1492011-11-14 18:22:28 +09001346}
1347
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001348static struct kvm_rmap_head *gfn_to_rmap(struct kvm *kvm, gfn_t gfn,
1349 struct kvm_mmu_page *sp)
Izik Eidus290fc382007-09-27 14:11:22 +02001350{
Paolo Bonzini699023e2015-05-18 15:03:39 +02001351 struct kvm_memslots *slots;
Izik Eidus290fc382007-09-27 14:11:22 +02001352 struct kvm_memory_slot *slot;
1353
Paolo Bonzini699023e2015-05-18 15:03:39 +02001354 slots = kvm_memslots_for_spte_role(kvm, sp->role);
1355 slot = __gfn_to_memslot(slots, gfn);
Paolo Bonzinie4cd1da2015-05-18 15:11:46 +02001356 return __gfn_to_rmap(gfn, sp->role.level, slot);
Izik Eidus290fc382007-09-27 14:11:22 +02001357}
1358
Xiao Guangrongf759e2b2011-09-22 16:53:17 +08001359static bool rmap_can_add(struct kvm_vcpu *vcpu)
1360{
1361 struct kvm_mmu_memory_cache *cache;
1362
1363 cache = &vcpu->arch.mmu_pte_list_desc_cache;
1364 return mmu_memory_cache_free_objects(cache);
1365}
1366
Joerg Roedel44ad9942009-07-27 16:30:42 +02001367static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001368{
Avi Kivity4db35312007-11-21 15:28:32 +02001369 struct kvm_mmu_page *sp;
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001370 struct kvm_rmap_head *rmap_head;
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001371
Avi Kivity4db35312007-11-21 15:28:32 +02001372 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +08001373 kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn);
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001374 rmap_head = gfn_to_rmap(vcpu->kvm, gfn, sp);
1375 return pte_list_add(vcpu, spte, rmap_head);
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001376}
1377
Izik Eidus290fc382007-09-27 14:11:22 +02001378static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001379{
Avi Kivity4db35312007-11-21 15:28:32 +02001380 struct kvm_mmu_page *sp;
Lai Jiangshan2032a932010-05-26 16:49:59 +08001381 gfn_t gfn;
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001382 struct kvm_rmap_head *rmap_head;
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001383
Avi Kivity4db35312007-11-21 15:28:32 +02001384 sp = page_header(__pa(spte));
Lai Jiangshan2032a932010-05-26 16:49:59 +08001385 gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt);
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001386 rmap_head = gfn_to_rmap(kvm, gfn, sp);
Wei Yang8daf3462018-10-04 10:04:22 +08001387 __pte_list_remove(spte, rmap_head);
Avi Kivitycd4a4e52007-01-05 16:36:38 -08001388}
1389
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001390/*
1391 * Used by the following functions to iterate through the sptes linked by a
1392 * rmap. All fields are private and not assumed to be used outside.
1393 */
1394struct rmap_iterator {
1395 /* private fields */
1396 struct pte_list_desc *desc; /* holds the sptep if not NULL */
1397 int pos; /* index of the sptep */
1398};
1399
1400/*
1401 * Iteration must be started by this function. This should also be used after
1402 * removing/dropping sptes from the rmap link because in such cases the
1403 * information in the itererator may not be valid.
1404 *
1405 * Returns sptep if found, NULL otherwise.
1406 */
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001407static u64 *rmap_get_first(struct kvm_rmap_head *rmap_head,
1408 struct rmap_iterator *iter)
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001409{
Takuya Yoshikawa77fbbbd2015-11-20 17:45:44 +09001410 u64 *sptep;
1411
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001412 if (!rmap_head->val)
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001413 return NULL;
1414
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001415 if (!(rmap_head->val & 1)) {
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001416 iter->desc = NULL;
Takuya Yoshikawa77fbbbd2015-11-20 17:45:44 +09001417 sptep = (u64 *)rmap_head->val;
1418 goto out;
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001419 }
1420
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001421 iter->desc = (struct pte_list_desc *)(rmap_head->val & ~1ul);
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001422 iter->pos = 0;
Takuya Yoshikawa77fbbbd2015-11-20 17:45:44 +09001423 sptep = iter->desc->sptes[iter->pos];
1424out:
1425 BUG_ON(!is_shadow_present_pte(*sptep));
1426 return sptep;
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001427}
1428
1429/*
1430 * Must be used with a valid iterator: e.g. after rmap_get_first().
1431 *
1432 * Returns sptep if found, NULL otherwise.
1433 */
1434static u64 *rmap_get_next(struct rmap_iterator *iter)
1435{
Takuya Yoshikawa77fbbbd2015-11-20 17:45:44 +09001436 u64 *sptep;
1437
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001438 if (iter->desc) {
1439 if (iter->pos < PTE_LIST_EXT - 1) {
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001440 ++iter->pos;
1441 sptep = iter->desc->sptes[iter->pos];
1442 if (sptep)
Takuya Yoshikawa77fbbbd2015-11-20 17:45:44 +09001443 goto out;
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001444 }
1445
1446 iter->desc = iter->desc->more;
1447
1448 if (iter->desc) {
1449 iter->pos = 0;
1450 /* desc->sptes[0] cannot be NULL */
Takuya Yoshikawa77fbbbd2015-11-20 17:45:44 +09001451 sptep = iter->desc->sptes[iter->pos];
1452 goto out;
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001453 }
1454 }
1455
1456 return NULL;
Takuya Yoshikawa77fbbbd2015-11-20 17:45:44 +09001457out:
1458 BUG_ON(!is_shadow_present_pte(*sptep));
1459 return sptep;
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001460}
1461
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001462#define for_each_rmap_spte(_rmap_head_, _iter_, _spte_) \
1463 for (_spte_ = rmap_get_first(_rmap_head_, _iter_); \
Takuya Yoshikawa77fbbbd2015-11-20 17:45:44 +09001464 _spte_; _spte_ = rmap_get_next(_iter_))
Xiao Guangrong0d536792015-05-13 14:42:20 +08001465
Xiao Guangrongc3707952011-07-12 03:28:04 +08001466static void drop_spte(struct kvm *kvm, u64 *sptep)
Xiao Guangronge4b502e2010-07-16 11:28:09 +08001467{
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08001468 if (mmu_spte_clear_track_bits(sptep))
Marcelo Tosattieb45fda2010-10-25 11:58:22 -02001469 rmap_remove(kvm, sptep);
Avi Kivitybe38d272010-06-06 14:31:27 +03001470}
1471
Xiao Guangrong8e22f952012-06-20 15:57:39 +08001472
1473static bool __drop_large_spte(struct kvm *kvm, u64 *sptep)
1474{
1475 if (is_large_pte(*sptep)) {
1476 WARN_ON(page_header(__pa(sptep))->role.level ==
1477 PT_PAGE_TABLE_LEVEL);
1478 drop_spte(kvm, sptep);
1479 --kvm->stat.lpages;
1480 return true;
1481 }
1482
1483 return false;
1484}
1485
1486static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep)
1487{
Lan Tianyuc3134ce2018-12-06 21:21:09 +08001488 if (__drop_large_spte(vcpu->kvm, sptep)) {
1489 struct kvm_mmu_page *sp = page_header(__pa(sptep));
1490
1491 kvm_flush_remote_tlbs_with_address(vcpu->kvm, sp->gfn,
1492 KVM_PAGES_PER_HPAGE(sp->role.level));
1493 }
Xiao Guangrong8e22f952012-06-20 15:57:39 +08001494}
1495
1496/*
Xiao Guangrong49fde342012-06-20 15:58:58 +08001497 * Write-protect on the specified @sptep, @pt_protect indicates whether
Xiao Guangrongc126d942014-04-17 17:06:14 +08001498 * spte write-protection is caused by protecting shadow page table.
Xiao Guangrong49fde342012-06-20 15:58:58 +08001499 *
Tiejun Chenb4619662014-09-22 10:31:38 +08001500 * Note: write protection is difference between dirty logging and spte
Xiao Guangrong49fde342012-06-20 15:58:58 +08001501 * protection:
1502 * - for dirty logging, the spte can be set to writable at anytime if
1503 * its dirty bitmap is properly set.
1504 * - for spte protection, the spte can be writable only after unsync-ing
1505 * shadow page.
Xiao Guangrong8e22f952012-06-20 15:57:39 +08001506 *
Xiao Guangrongc126d942014-04-17 17:06:14 +08001507 * Return true if tlb need be flushed.
Xiao Guangrong8e22f952012-06-20 15:57:39 +08001508 */
Bandan Dasc4f138b2016-08-02 16:32:37 -04001509static bool spte_write_protect(u64 *sptep, bool pt_protect)
Xiao Guangrongd13bc5b2012-06-20 15:57:15 +08001510{
1511 u64 spte = *sptep;
1512
Xiao Guangrong49fde342012-06-20 15:58:58 +08001513 if (!is_writable_pte(spte) &&
Junaid Shahidea4114b2016-12-06 16:46:11 -08001514 !(pt_protect && spte_can_locklessly_be_made_writable(spte)))
Xiao Guangrongd13bc5b2012-06-20 15:57:15 +08001515 return false;
1516
1517 rmap_printk("rmap_write_protect: spte %p %llx\n", sptep, *sptep);
1518
Xiao Guangrong49fde342012-06-20 15:58:58 +08001519 if (pt_protect)
1520 spte &= ~SPTE_MMU_WRITEABLE;
Xiao Guangrongd13bc5b2012-06-20 15:57:15 +08001521 spte = spte & ~PT_WRITABLE_MASK;
Xiao Guangrong49fde342012-06-20 15:58:58 +08001522
Xiao Guangrongc126d942014-04-17 17:06:14 +08001523 return mmu_spte_update(sptep, spte);
Xiao Guangrongd13bc5b2012-06-20 15:57:15 +08001524}
1525
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001526static bool __rmap_write_protect(struct kvm *kvm,
1527 struct kvm_rmap_head *rmap_head,
Takuya Yoshikawa245c3912013-01-08 19:44:09 +09001528 bool pt_protect)
Takuya Yoshikawaa0ed4602012-03-01 19:31:22 +09001529{
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001530 u64 *sptep;
1531 struct rmap_iterator iter;
Xiao Guangrongd13bc5b2012-06-20 15:57:15 +08001532 bool flush = false;
Takuya Yoshikawaa0ed4602012-03-01 19:31:22 +09001533
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001534 for_each_rmap_spte(rmap_head, &iter, sptep)
Bandan Dasc4f138b2016-08-02 16:32:37 -04001535 flush |= spte_write_protect(sptep, pt_protect);
Takuya Yoshikawaa0ed4602012-03-01 19:31:22 +09001536
Xiao Guangrongd13bc5b2012-06-20 15:57:15 +08001537 return flush;
Takuya Yoshikawaa0ed4602012-03-01 19:31:22 +09001538}
1539
Bandan Dasc4f138b2016-08-02 16:32:37 -04001540static bool spte_clear_dirty(u64 *sptep)
Kai Huangf4b4b182015-01-28 10:54:24 +08001541{
1542 u64 spte = *sptep;
1543
1544 rmap_printk("rmap_clear_dirty: spte %p %llx\n", sptep, *sptep);
1545
1546 spte &= ~shadow_dirty_mask;
1547
1548 return mmu_spte_update(sptep, spte);
1549}
1550
Peter Feinerac8d57e2017-06-30 17:26:31 -07001551static bool wrprot_ad_disabled_spte(u64 *sptep)
1552{
1553 bool was_writable = test_and_clear_bit(PT_WRITABLE_SHIFT,
1554 (unsigned long *)sptep);
1555 if (was_writable)
1556 kvm_set_pfn_dirty(spte_to_pfn(*sptep));
1557
1558 return was_writable;
1559}
1560
1561/*
1562 * Gets the GFN ready for another round of dirty logging by clearing the
1563 * - D bit on ad-enabled SPTEs, and
1564 * - W bit on ad-disabled SPTEs.
1565 * Returns true iff any D or W bits were cleared.
1566 */
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001567static bool __rmap_clear_dirty(struct kvm *kvm, struct kvm_rmap_head *rmap_head)
Kai Huangf4b4b182015-01-28 10:54:24 +08001568{
1569 u64 *sptep;
1570 struct rmap_iterator iter;
1571 bool flush = false;
1572
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001573 for_each_rmap_spte(rmap_head, &iter, sptep)
Peter Feinerac8d57e2017-06-30 17:26:31 -07001574 if (spte_ad_enabled(*sptep))
1575 flush |= spte_clear_dirty(sptep);
1576 else
1577 flush |= wrprot_ad_disabled_spte(sptep);
Kai Huangf4b4b182015-01-28 10:54:24 +08001578
1579 return flush;
1580}
1581
Bandan Dasc4f138b2016-08-02 16:32:37 -04001582static bool spte_set_dirty(u64 *sptep)
Kai Huangf4b4b182015-01-28 10:54:24 +08001583{
1584 u64 spte = *sptep;
1585
1586 rmap_printk("rmap_set_dirty: spte %p %llx\n", sptep, *sptep);
1587
1588 spte |= shadow_dirty_mask;
1589
1590 return mmu_spte_update(sptep, spte);
1591}
1592
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001593static bool __rmap_set_dirty(struct kvm *kvm, struct kvm_rmap_head *rmap_head)
Kai Huangf4b4b182015-01-28 10:54:24 +08001594{
1595 u64 *sptep;
1596 struct rmap_iterator iter;
1597 bool flush = false;
1598
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001599 for_each_rmap_spte(rmap_head, &iter, sptep)
Peter Feinerac8d57e2017-06-30 17:26:31 -07001600 if (spte_ad_enabled(*sptep))
1601 flush |= spte_set_dirty(sptep);
Kai Huangf4b4b182015-01-28 10:54:24 +08001602
1603 return flush;
1604}
1605
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001606/**
Kai Huang3b0f1d02015-01-28 10:54:23 +08001607 * kvm_mmu_write_protect_pt_masked - write protect selected PT level pages
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001608 * @kvm: kvm instance
1609 * @slot: slot to protect
1610 * @gfn_offset: start of the BITS_PER_LONG pages we care about
1611 * @mask: indicates which pages we should protect
1612 *
1613 * Used when we do not need to care about huge page mappings: e.g. during dirty
1614 * logging we do not have any such mappings.
1615 */
Kai Huang3b0f1d02015-01-28 10:54:23 +08001616static void kvm_mmu_write_protect_pt_masked(struct kvm *kvm,
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001617 struct kvm_memory_slot *slot,
1618 gfn_t gfn_offset, unsigned long mask)
Izik Eidus98348e92007-10-16 14:42:30 +02001619{
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001620 struct kvm_rmap_head *rmap_head;
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001621
1622 while (mask) {
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001623 rmap_head = __gfn_to_rmap(slot->base_gfn + gfn_offset + __ffs(mask),
1624 PT_PAGE_TABLE_LEVEL, slot);
1625 __rmap_write_protect(kvm, rmap_head, false);
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001626
1627 /* clear the first set bit */
1628 mask &= mask - 1;
1629 }
1630}
1631
Kai Huang3b0f1d02015-01-28 10:54:23 +08001632/**
Peter Feinerac8d57e2017-06-30 17:26:31 -07001633 * kvm_mmu_clear_dirty_pt_masked - clear MMU D-bit for PT level pages, or write
1634 * protect the page if the D-bit isn't supported.
Kai Huangf4b4b182015-01-28 10:54:24 +08001635 * @kvm: kvm instance
1636 * @slot: slot to clear D-bit
1637 * @gfn_offset: start of the BITS_PER_LONG pages we care about
1638 * @mask: indicates which pages we should clear D-bit
1639 *
1640 * Used for PML to re-log the dirty GPAs after userspace querying dirty_bitmap.
1641 */
1642void kvm_mmu_clear_dirty_pt_masked(struct kvm *kvm,
1643 struct kvm_memory_slot *slot,
1644 gfn_t gfn_offset, unsigned long mask)
1645{
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001646 struct kvm_rmap_head *rmap_head;
Kai Huangf4b4b182015-01-28 10:54:24 +08001647
1648 while (mask) {
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001649 rmap_head = __gfn_to_rmap(slot->base_gfn + gfn_offset + __ffs(mask),
1650 PT_PAGE_TABLE_LEVEL, slot);
1651 __rmap_clear_dirty(kvm, rmap_head);
Kai Huangf4b4b182015-01-28 10:54:24 +08001652
1653 /* clear the first set bit */
1654 mask &= mask - 1;
1655 }
1656}
1657EXPORT_SYMBOL_GPL(kvm_mmu_clear_dirty_pt_masked);
1658
1659/**
Kai Huang3b0f1d02015-01-28 10:54:23 +08001660 * kvm_arch_mmu_enable_log_dirty_pt_masked - enable dirty logging for selected
1661 * PT level pages.
1662 *
1663 * It calls kvm_mmu_write_protect_pt_masked to write protect selected pages to
1664 * enable dirty logging for them.
1665 *
1666 * Used when we do not need to care about huge page mappings: e.g. during dirty
1667 * logging we do not have any such mappings.
1668 */
1669void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
1670 struct kvm_memory_slot *slot,
1671 gfn_t gfn_offset, unsigned long mask)
1672{
Kai Huang88178fd2015-01-28 10:54:27 +08001673 if (kvm_x86_ops->enable_log_dirty_pt_masked)
1674 kvm_x86_ops->enable_log_dirty_pt_masked(kvm, slot, gfn_offset,
1675 mask);
1676 else
1677 kvm_mmu_write_protect_pt_masked(kvm, slot, gfn_offset, mask);
Kai Huang3b0f1d02015-01-28 10:54:23 +08001678}
1679
Bandan Dasbab41652017-05-05 15:25:13 -04001680/**
1681 * kvm_arch_write_log_dirty - emulate dirty page logging
1682 * @vcpu: Guest mode vcpu
1683 *
1684 * Emulate arch specific page modification logging for the
1685 * nested hypervisor
1686 */
1687int kvm_arch_write_log_dirty(struct kvm_vcpu *vcpu)
1688{
1689 if (kvm_x86_ops->write_log_dirty)
1690 return kvm_x86_ops->write_log_dirty(vcpu);
1691
1692 return 0;
1693}
1694
Xiao Guangrongaeecee22016-02-24 17:51:08 +08001695bool kvm_mmu_slot_gfn_write_protect(struct kvm *kvm,
1696 struct kvm_memory_slot *slot, u64 gfn)
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001697{
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001698 struct kvm_rmap_head *rmap_head;
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001699 int i;
Xiao Guangrong2f845692012-06-20 15:56:53 +08001700 bool write_protected = false;
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001701
Xiao Guangrong8a3d08f2015-05-13 14:42:21 +08001702 for (i = PT_PAGE_TABLE_LEVEL; i <= PT_MAX_HUGEPAGE_LEVEL; ++i) {
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001703 rmap_head = __gfn_to_rmap(gfn, i, slot);
Xiao Guangrongaeecee22016-02-24 17:51:08 +08001704 write_protected |= __rmap_write_protect(kvm, rmap_head, true);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001705 }
1706
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001707 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -08001708}
1709
Xiao Guangrongaeecee22016-02-24 17:51:08 +08001710static bool rmap_write_protect(struct kvm_vcpu *vcpu, u64 gfn)
1711{
1712 struct kvm_memory_slot *slot;
1713
1714 slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
1715 return kvm_mmu_slot_gfn_write_protect(vcpu->kvm, slot, gfn);
1716}
1717
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001718static bool kvm_zap_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head)
Xiao Guangrong6a49f852015-05-13 14:42:25 +08001719{
1720 u64 *sptep;
1721 struct rmap_iterator iter;
1722 bool flush = false;
1723
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001724 while ((sptep = rmap_get_first(rmap_head, &iter))) {
Xiao Guangrong6a49f852015-05-13 14:42:25 +08001725 rmap_printk("%s: spte %p %llx.\n", __func__, sptep, *sptep);
1726
Wei Yange7912382018-10-04 10:04:23 +08001727 pte_list_remove(rmap_head, sptep);
Xiao Guangrong6a49f852015-05-13 14:42:25 +08001728 flush = true;
1729 }
1730
1731 return flush;
1732}
1733
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001734static int kvm_unmap_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
Andres Lagar-Cavilla8a9522d2014-09-23 12:34:54 -07001735 struct kvm_memory_slot *slot, gfn_t gfn, int level,
1736 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001737{
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001738 return kvm_zap_rmapp(kvm, rmap_head);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001739}
1740
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001741static int kvm_set_pte_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
Andres Lagar-Cavilla8a9522d2014-09-23 12:34:54 -07001742 struct kvm_memory_slot *slot, gfn_t gfn, int level,
1743 unsigned long data)
Izik Eidus3da0dd42009-09-23 21:47:18 +03001744{
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001745 u64 *sptep;
1746 struct rmap_iterator iter;
Izik Eidus3da0dd42009-09-23 21:47:18 +03001747 int need_flush = 0;
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001748 u64 new_spte;
Izik Eidus3da0dd42009-09-23 21:47:18 +03001749 pte_t *ptep = (pte_t *)data;
Dan Williamsba049e92016-01-15 16:56:11 -08001750 kvm_pfn_t new_pfn;
Izik Eidus3da0dd42009-09-23 21:47:18 +03001751
1752 WARN_ON(pte_huge(*ptep));
1753 new_pfn = pte_pfn(*ptep);
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001754
Xiao Guangrong0d536792015-05-13 14:42:20 +08001755restart:
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001756 for_each_rmap_spte(rmap_head, &iter, sptep) {
Andres Lagar-Cavilla8a9522d2014-09-23 12:34:54 -07001757 rmap_printk("kvm_set_pte_rmapp: spte %p %llx gfn %llx (%d)\n",
Junaid Shahidf160c7b2016-12-06 16:46:16 -08001758 sptep, *sptep, gfn, level);
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001759
Izik Eidus3da0dd42009-09-23 21:47:18 +03001760 need_flush = 1;
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001761
Izik Eidus3da0dd42009-09-23 21:47:18 +03001762 if (pte_write(*ptep)) {
Wei Yange7912382018-10-04 10:04:23 +08001763 pte_list_remove(rmap_head, sptep);
Xiao Guangrong0d536792015-05-13 14:42:20 +08001764 goto restart;
Izik Eidus3da0dd42009-09-23 21:47:18 +03001765 } else {
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001766 new_spte = *sptep & ~PT64_BASE_ADDR_MASK;
Izik Eidus3da0dd42009-09-23 21:47:18 +03001767 new_spte |= (u64)new_pfn << PAGE_SHIFT;
1768
1769 new_spte &= ~PT_WRITABLE_MASK;
1770 new_spte &= ~SPTE_HOST_WRITEABLE;
Junaid Shahidf160c7b2016-12-06 16:46:16 -08001771
1772 new_spte = mark_spte_for_access_track(new_spte);
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001773
1774 mmu_spte_clear_track_bits(sptep);
1775 mmu_spte_set(sptep, new_spte);
Izik Eidus3da0dd42009-09-23 21:47:18 +03001776 }
1777 }
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001778
Lan Tianyu3cc5ea92018-12-06 21:21:12 +08001779 if (need_flush && kvm_available_flush_tlb_with_range()) {
1780 kvm_flush_remote_tlbs_with_address(kvm, gfn, 1);
1781 return 0;
1782 }
1783
Lan Tianyu0cf853c2018-12-06 21:21:11 +08001784 return need_flush;
Izik Eidus3da0dd42009-09-23 21:47:18 +03001785}
1786
Xiao Guangrong6ce1f4e2015-05-13 14:42:22 +08001787struct slot_rmap_walk_iterator {
1788 /* input fields. */
1789 struct kvm_memory_slot *slot;
1790 gfn_t start_gfn;
1791 gfn_t end_gfn;
1792 int start_level;
1793 int end_level;
1794
1795 /* output fields. */
1796 gfn_t gfn;
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001797 struct kvm_rmap_head *rmap;
Xiao Guangrong6ce1f4e2015-05-13 14:42:22 +08001798 int level;
1799
1800 /* private field. */
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001801 struct kvm_rmap_head *end_rmap;
Xiao Guangrong6ce1f4e2015-05-13 14:42:22 +08001802};
1803
1804static void
1805rmap_walk_init_level(struct slot_rmap_walk_iterator *iterator, int level)
1806{
1807 iterator->level = level;
1808 iterator->gfn = iterator->start_gfn;
1809 iterator->rmap = __gfn_to_rmap(iterator->gfn, level, iterator->slot);
1810 iterator->end_rmap = __gfn_to_rmap(iterator->end_gfn, level,
1811 iterator->slot);
1812}
1813
1814static void
1815slot_rmap_walk_init(struct slot_rmap_walk_iterator *iterator,
1816 struct kvm_memory_slot *slot, int start_level,
1817 int end_level, gfn_t start_gfn, gfn_t end_gfn)
1818{
1819 iterator->slot = slot;
1820 iterator->start_level = start_level;
1821 iterator->end_level = end_level;
1822 iterator->start_gfn = start_gfn;
1823 iterator->end_gfn = end_gfn;
1824
1825 rmap_walk_init_level(iterator, iterator->start_level);
1826}
1827
1828static bool slot_rmap_walk_okay(struct slot_rmap_walk_iterator *iterator)
1829{
1830 return !!iterator->rmap;
1831}
1832
1833static void slot_rmap_walk_next(struct slot_rmap_walk_iterator *iterator)
1834{
1835 if (++iterator->rmap <= iterator->end_rmap) {
1836 iterator->gfn += (1UL << KVM_HPAGE_GFN_SHIFT(iterator->level));
1837 return;
1838 }
1839
1840 if (++iterator->level > iterator->end_level) {
1841 iterator->rmap = NULL;
1842 return;
1843 }
1844
1845 rmap_walk_init_level(iterator, iterator->level);
1846}
1847
1848#define for_each_slot_rmap_range(_slot_, _start_level_, _end_level_, \
1849 _start_gfn, _end_gfn, _iter_) \
1850 for (slot_rmap_walk_init(_iter_, _slot_, _start_level_, \
1851 _end_level_, _start_gfn, _end_gfn); \
1852 slot_rmap_walk_okay(_iter_); \
1853 slot_rmap_walk_next(_iter_))
1854
Takuya Yoshikawa84504ef2012-07-02 17:55:48 +09001855static int kvm_handle_hva_range(struct kvm *kvm,
1856 unsigned long start,
1857 unsigned long end,
1858 unsigned long data,
1859 int (*handler)(struct kvm *kvm,
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001860 struct kvm_rmap_head *rmap_head,
Takuya Yoshikawa048212d2012-07-02 17:57:59 +09001861 struct kvm_memory_slot *slot,
Andres Lagar-Cavilla8a9522d2014-09-23 12:34:54 -07001862 gfn_t gfn,
1863 int level,
Takuya Yoshikawa84504ef2012-07-02 17:55:48 +09001864 unsigned long data))
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001865{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001866 struct kvm_memslots *slots;
Xiao Guangrongbe6ba0f2011-11-24 17:39:18 +08001867 struct kvm_memory_slot *memslot;
Xiao Guangrong6ce1f4e2015-05-13 14:42:22 +08001868 struct slot_rmap_walk_iterator iterator;
1869 int ret = 0;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02001870 int i;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001871
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02001872 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
1873 slots = __kvm_memslots(kvm, i);
1874 kvm_for_each_memslot(memslot, slots) {
1875 unsigned long hva_start, hva_end;
1876 gfn_t gfn_start, gfn_end;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001877
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02001878 hva_start = max(start, memslot->userspace_addr);
1879 hva_end = min(end, memslot->userspace_addr +
1880 (memslot->npages << PAGE_SHIFT));
1881 if (hva_start >= hva_end)
1882 continue;
1883 /*
1884 * {gfn(page) | page intersects with [hva_start, hva_end)} =
1885 * {gfn_start, gfn_start+1, ..., gfn_end-1}.
1886 */
1887 gfn_start = hva_to_gfn_memslot(hva_start, memslot);
1888 gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001889
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02001890 for_each_slot_rmap_range(memslot, PT_PAGE_TABLE_LEVEL,
1891 PT_MAX_HUGEPAGE_LEVEL,
1892 gfn_start, gfn_end - 1,
1893 &iterator)
1894 ret |= handler(kvm, iterator.rmap, memslot,
1895 iterator.gfn, iterator.level, data);
1896 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001897 }
1898
Takuya Yoshikawaf3953022012-07-02 17:58:48 +09001899 return ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001900}
1901
Takuya Yoshikawa84504ef2012-07-02 17:55:48 +09001902static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
1903 unsigned long data,
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001904 int (*handler)(struct kvm *kvm,
1905 struct kvm_rmap_head *rmap_head,
Takuya Yoshikawa048212d2012-07-02 17:57:59 +09001906 struct kvm_memory_slot *slot,
Andres Lagar-Cavilla8a9522d2014-09-23 12:34:54 -07001907 gfn_t gfn, int level,
Takuya Yoshikawa84504ef2012-07-02 17:55:48 +09001908 unsigned long data))
1909{
1910 return kvm_handle_hva_range(kvm, hva, hva + 1, data, handler);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001911}
1912
Takuya Yoshikawab3ae2092012-07-02 17:56:33 +09001913int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end)
1914{
1915 return kvm_handle_hva_range(kvm, start, end, 0, kvm_unmap_rmapp);
1916}
1917
Lan Tianyu748c0e32018-12-06 21:21:10 +08001918int kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
Izik Eidus3da0dd42009-09-23 21:47:18 +03001919{
Lan Tianyu0cf853c2018-12-06 21:21:11 +08001920 return kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
Izik Eidus3da0dd42009-09-23 21:47:18 +03001921}
1922
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001923static int kvm_age_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
Andres Lagar-Cavilla8a9522d2014-09-23 12:34:54 -07001924 struct kvm_memory_slot *slot, gfn_t gfn, int level,
1925 unsigned long data)
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001926{
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001927 u64 *sptep;
Michael S. Tsirkin79f702a2012-06-03 11:34:08 +03001928 struct rmap_iterator uninitialized_var(iter);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001929 int young = 0;
1930
Junaid Shahidf160c7b2016-12-06 16:46:16 -08001931 for_each_rmap_spte(rmap_head, &iter, sptep)
1932 young |= mmu_spte_age(sptep);
Xiao Guangrong0d536792015-05-13 14:42:20 +08001933
Andres Lagar-Cavilla8a9522d2014-09-23 12:34:54 -07001934 trace_kvm_age_page(gfn, level, slot, young);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001935 return young;
1936}
1937
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001938static int kvm_test_age_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
Andres Lagar-Cavilla8a9522d2014-09-23 12:34:54 -07001939 struct kvm_memory_slot *slot, gfn_t gfn,
1940 int level, unsigned long data)
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001941{
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001942 u64 *sptep;
1943 struct rmap_iterator iter;
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001944
Junaid Shahid83ef6c82016-12-06 16:46:13 -08001945 for_each_rmap_spte(rmap_head, &iter, sptep)
1946 if (is_accessed_spte(*sptep))
1947 return 1;
Junaid Shahid83ef6c82016-12-06 16:46:13 -08001948 return 0;
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001949}
1950
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001951#define RMAP_RECYCLE_THRESHOLD 1000
1952
Joerg Roedel852e3c12009-07-27 16:30:44 +02001953static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001954{
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001955 struct kvm_rmap_head *rmap_head;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001956 struct kvm_mmu_page *sp;
1957
1958 sp = page_header(__pa(spte));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001959
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001960 rmap_head = gfn_to_rmap(vcpu->kvm, gfn, sp);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001961
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001962 kvm_unmap_rmapp(vcpu->kvm, rmap_head, NULL, gfn, sp->role.level, 0);
Lan Tianyuc3134ce2018-12-06 21:21:09 +08001963 kvm_flush_remote_tlbs_with_address(vcpu->kvm, sp->gfn,
1964 KVM_PAGES_PER_HPAGE(sp->role.level));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001965}
1966
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -07001967int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end)
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001968{
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -07001969 return kvm_handle_hva_range(kvm, start, end, 0, kvm_age_rmapp);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001970}
1971
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001972int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
1973{
1974 return kvm_handle_hva(kvm, hva, 0, kvm_test_age_rmapp);
1975}
1976
Yaozu Dongd6c69ee2007-04-25 14:17:25 +08001977#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +03001978static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001979{
Avi Kivity139bdb22007-01-05 16:36:50 -08001980 u64 *pos;
1981 u64 *end;
1982
Avi Kivity47ad8e62007-05-06 15:50:58 +03001983 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +03001984 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001985 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -08001986 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001987 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -08001988 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001989 return 1;
1990}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +08001991#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001992
Dave Hansen45221ab2010-08-19 18:11:37 -07001993/*
1994 * This value is the sum of all of the kvm instances's
1995 * kvm->arch.n_used_mmu_pages values. We need a global,
1996 * aggregate version in order to make the slab shrinker
1997 * faster
1998 */
1999static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, int nr)
2000{
2001 kvm->arch.n_used_mmu_pages += nr;
2002 percpu_counter_add(&kvm_total_used_mmu_pages, nr);
2003}
2004
Gleb Natapov834be0d2013-01-30 16:45:05 +02002005static void kvm_mmu_free_page(struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -08002006{
Paolo Bonzinifa4a2c02013-10-02 16:56:16 +02002007 MMU_WARN_ON(!is_empty_shadow_page(sp->spt));
Xiao Guangrong77758342010-06-04 21:53:54 +08002008 hlist_del(&sp->hash_link);
Xiao Guangrongbd4c86e2011-07-12 03:27:14 +08002009 list_del(&sp->link);
2010 free_page((unsigned long)sp->spt);
Gleb Natapov834be0d2013-01-30 16:45:05 +02002011 if (!sp->role.direct)
2012 free_page((unsigned long)sp->gfns);
Xiao Guangronge8ad9a72010-05-13 10:06:02 +08002013 kmem_cache_free(mmu_page_header_cache, sp);
Avi Kivity260746c2007-01-05 16:36:49 -08002014}
2015
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002016static unsigned kvm_page_table_hashfn(gfn_t gfn)
2017{
David Matlack114df302016-12-19 13:58:25 -08002018 return hash_64(gfn, KVM_MMU_HASH_SHIFT);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002019}
2020
Xiao Guangrong67052b32011-05-15 23:27:08 +08002021static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
2022 struct kvm_mmu_page *sp, u64 *parent_pte)
2023{
2024 if (!parent_pte)
2025 return;
2026
2027 pte_list_add(vcpu, parent_pte, &sp->parent_ptes);
2028}
2029
2030static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
2031 u64 *parent_pte)
2032{
Wei Yang8daf3462018-10-04 10:04:22 +08002033 __pte_list_remove(parent_pte, &sp->parent_ptes);
Xiao Guangrong67052b32011-05-15 23:27:08 +08002034}
2035
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08002036static void drop_parent_pte(struct kvm_mmu_page *sp,
2037 u64 *parent_pte)
2038{
2039 mmu_page_remove_parent_pte(sp, parent_pte);
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08002040 mmu_spte_clear_no_track(parent_pte);
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08002041}
2042
Takuya Yoshikawa47005792015-11-20 17:46:29 +09002043static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, int direct)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002044{
Avi Kivity4db35312007-11-21 15:28:32 +02002045 struct kvm_mmu_page *sp;
Takuya Yoshikawa7ddca7e2013-03-21 19:33:43 +09002046
Takuya Yoshikawa80feb892012-05-29 23:54:26 +09002047 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache);
2048 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache);
Lai Jiangshan2032a932010-05-26 16:49:59 +08002049 if (!direct)
Takuya Yoshikawa80feb892012-05-29 23:54:26 +09002050 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache);
Avi Kivity4db35312007-11-21 15:28:32 +02002051 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08002052
2053 /*
2054 * The active_mmu_pages list is the FIFO list, do not move the
2055 * page until it is zapped. kvm_zap_obsolete_pages depends on
2056 * this feature. See the comments in kvm_zap_obsolete_pages().
2057 */
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002058 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Dave Hansen45221ab2010-08-19 18:11:37 -07002059 kvm_mod_used_mmu_pages(vcpu->kvm, +1);
Avi Kivity4db35312007-11-21 15:28:32 +02002060 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002061}
2062
Xiao Guangrong67052b32011-05-15 23:27:08 +08002063static void mark_unsync(u64 *spte);
Xiao Guangrong6b184932010-04-16 21:29:17 +08002064static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03002065{
Takuya Yoshikawa74c4e632015-11-26 21:15:38 +09002066 u64 *sptep;
2067 struct rmap_iterator iter;
2068
2069 for_each_rmap_spte(&sp->parent_ptes, &iter, sptep) {
2070 mark_unsync(sptep);
2071 }
Xiao Guangrong1047df12010-06-11 21:35:15 +08002072}
2073
Xiao Guangrong67052b32011-05-15 23:27:08 +08002074static void mark_unsync(u64 *spte)
Xiao Guangrong1047df12010-06-11 21:35:15 +08002075{
Xiao Guangrong67052b32011-05-15 23:27:08 +08002076 struct kvm_mmu_page *sp;
Xiao Guangrong1047df12010-06-11 21:35:15 +08002077 unsigned int index;
2078
Xiao Guangrong67052b32011-05-15 23:27:08 +08002079 sp = page_header(__pa(spte));
Xiao Guangrong1047df12010-06-11 21:35:15 +08002080 index = spte - sp->spt;
2081 if (__test_and_set_bit(index, sp->unsync_child_bitmap))
2082 return;
2083 if (sp->unsync_children++)
2084 return;
2085 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03002086}
2087
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002088static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08002089 struct kvm_mmu_page *sp)
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002090{
Paolo Bonzini1f50f1b2016-02-24 11:07:14 +01002091 return 0;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002092}
2093
Junaid Shahid7eb77e92018-06-27 14:59:16 -07002094static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva, hpa_t root)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002095{
2096}
2097
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08002098static void nonpaging_update_pte(struct kvm_vcpu *vcpu,
2099 struct kvm_mmu_page *sp, u64 *spte,
Xiao Guangrong7c562522011-03-28 10:29:27 +08002100 const void *pte)
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08002101{
2102 WARN_ON(1);
2103}
2104
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002105#define KVM_PAGE_ARRAY_NR 16
2106
2107struct kvm_mmu_pages {
2108 struct mmu_page_and_offset {
2109 struct kvm_mmu_page *sp;
2110 unsigned int idx;
2111 } page[KVM_PAGE_ARRAY_NR];
2112 unsigned int nr;
2113};
2114
Hannes Edercded19f2009-02-21 02:19:13 +01002115static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
2116 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002117{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002118 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002119
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002120 if (sp->unsync)
2121 for (i=0; i < pvec->nr; i++)
2122 if (pvec->page[i].sp == sp)
2123 return 0;
2124
2125 pvec->page[pvec->nr].sp = sp;
2126 pvec->page[pvec->nr].idx = idx;
2127 pvec->nr++;
2128 return (pvec->nr == KVM_PAGE_ARRAY_NR);
2129}
2130
Takuya Yoshikawafd951452015-11-20 17:43:13 +09002131static inline void clear_unsync_child_bit(struct kvm_mmu_page *sp, int idx)
2132{
2133 --sp->unsync_children;
2134 WARN_ON((int)sp->unsync_children < 0);
2135 __clear_bit(idx, sp->unsync_child_bitmap);
2136}
2137
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002138static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
2139 struct kvm_mmu_pages *pvec)
2140{
2141 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002142
Takuya Yoshikawa37178b82011-11-29 14:02:45 +09002143 for_each_set_bit(i, sp->unsync_child_bitmap, 512) {
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08002144 struct kvm_mmu_page *child;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002145 u64 ent = sp->spt[i];
2146
Takuya Yoshikawafd951452015-11-20 17:43:13 +09002147 if (!is_shadow_present_pte(ent) || is_large_pte(ent)) {
2148 clear_unsync_child_bit(sp, i);
2149 continue;
2150 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002151
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08002152 child = page_header(ent & PT64_BASE_ADDR_MASK);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002153
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08002154 if (child->unsync_children) {
2155 if (mmu_pages_add(pvec, child, i))
2156 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002157
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08002158 ret = __mmu_unsync_walk(child, pvec);
Takuya Yoshikawafd951452015-11-20 17:43:13 +09002159 if (!ret) {
2160 clear_unsync_child_bit(sp, i);
2161 continue;
2162 } else if (ret > 0) {
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08002163 nr_unsync_leaf += ret;
Takuya Yoshikawafd951452015-11-20 17:43:13 +09002164 } else
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08002165 return ret;
2166 } else if (child->unsync) {
2167 nr_unsync_leaf++;
2168 if (mmu_pages_add(pvec, child, i))
2169 return -ENOSPC;
2170 } else
Takuya Yoshikawafd951452015-11-20 17:43:13 +09002171 clear_unsync_child_bit(sp, i);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002172 }
2173
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002174 return nr_unsync_leaf;
2175}
2176
Xiao Guangronge23d3fe2016-02-24 09:46:06 +01002177#define INVALID_INDEX (-1)
2178
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002179static int mmu_unsync_walk(struct kvm_mmu_page *sp,
2180 struct kvm_mmu_pages *pvec)
2181{
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01002182 pvec->nr = 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002183 if (!sp->unsync_children)
2184 return 0;
2185
Xiao Guangronge23d3fe2016-02-24 09:46:06 +01002186 mmu_pages_add(pvec, sp, INVALID_INDEX);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002187 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002188}
2189
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002190static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
2191{
2192 WARN_ON(!sp->unsync);
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08002193 trace_kvm_mmu_sync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002194 sp->unsync = 0;
2195 --kvm->stat.mmu_unsync;
2196}
2197
Xiao Guangrong77758342010-06-04 21:53:54 +08002198static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
2199 struct list_head *invalid_list);
2200static void kvm_mmu_commit_zap_page(struct kvm *kvm,
2201 struct list_head *invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002202
Xiao Guangrongf34d2512013-05-31 08:36:28 +08002203/*
2204 * NOTE: we should pay more attention on the zapped-obsolete page
2205 * (is_obsolete_sp(sp) && sp->role.invalid) when you do hash list walk
2206 * since it has been deleted from active_mmu_pages but still can be found
2207 * at hast list.
2208 *
David Matlackf3414bc2016-12-20 15:25:57 -08002209 * for_each_valid_sp() has skipped that kind of pages.
Xiao Guangrongf34d2512013-05-31 08:36:28 +08002210 */
David Matlackf3414bc2016-12-20 15:25:57 -08002211#define for_each_valid_sp(_kvm, _sp, _gfn) \
Takuya Yoshikawa1044b032013-03-06 16:05:07 +09002212 hlist_for_each_entry(_sp, \
2213 &(_kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(_gfn)], hash_link) \
David Matlackf3414bc2016-12-20 15:25:57 -08002214 if (is_obsolete_sp((_kvm), (_sp)) || (_sp)->role.invalid) { \
2215 } else
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002216
Takuya Yoshikawa1044b032013-03-06 16:05:07 +09002217#define for_each_gfn_indirect_valid_sp(_kvm, _sp, _gfn) \
David Matlackf3414bc2016-12-20 15:25:57 -08002218 for_each_valid_sp(_kvm, _sp, _gfn) \
2219 if ((_sp)->gfn != (_gfn) || (_sp)->role.direct) {} else
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002220
Xiao Guangrongf918b442010-06-11 21:30:36 +08002221/* @sp->gfn should be write-protected at the call site */
Paolo Bonzini1f50f1b2016-02-24 11:07:14 +01002222static bool __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
2223 struct list_head *invalid_list)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002224{
Tianyu Lan450917b2018-07-18 06:12:04 +00002225 if (sp->role.cr4_pae != !!is_pae(vcpu)
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02002226 || vcpu->arch.mmu->sync_page(vcpu, sp) == 0) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002227 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Paolo Bonzini1f50f1b2016-02-24 11:07:14 +01002228 return false;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002229 }
2230
Paolo Bonzini1f50f1b2016-02-24 11:07:14 +01002231 return true;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002232}
2233
Paolo Bonzini35a70512016-02-24 10:03:27 +01002234static void kvm_mmu_flush_or_zap(struct kvm_vcpu *vcpu,
2235 struct list_head *invalid_list,
2236 bool remote_flush, bool local_flush)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08002237{
Paolo Bonzini35a70512016-02-24 10:03:27 +01002238 if (!list_empty(invalid_list)) {
2239 kvm_mmu_commit_zap_page(vcpu->kvm, invalid_list);
2240 return;
2241 }
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08002242
Paolo Bonzini35a70512016-02-24 10:03:27 +01002243 if (remote_flush)
2244 kvm_flush_remote_tlbs(vcpu->kvm);
2245 else if (local_flush)
2246 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08002247}
2248
Xiao Guangronge37fa782011-11-30 17:43:24 +08002249#ifdef CONFIG_KVM_MMU_AUDIT
2250#include "mmu_audit.c"
2251#else
2252static void kvm_mmu_audit(struct kvm_vcpu *vcpu, int point) { }
2253static void mmu_audit_disable(void) { }
2254#endif
2255
Xiao Guangrong46971a22016-03-25 21:19:38 +08002256static bool is_obsolete_sp(struct kvm *kvm, struct kvm_mmu_page *sp)
2257{
2258 return unlikely(sp->mmu_valid_gen != kvm->arch.mmu_valid_gen);
2259}
2260
Paolo Bonzini1f50f1b2016-02-24 11:07:14 +01002261static bool kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002262 struct list_head *invalid_list)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08002263{
Paolo Bonzini9a43c5d2016-02-24 10:28:01 +01002264 kvm_unlink_unsync_page(vcpu->kvm, sp);
2265 return __kvm_sync_page(vcpu, sp, invalid_list);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08002266}
2267
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08002268/* @gfn should be write-protected at the call site */
Paolo Bonzini2a74003a2016-02-24 11:26:10 +01002269static bool kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn,
2270 struct list_head *invalid_list)
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08002271{
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08002272 struct kvm_mmu_page *s;
Paolo Bonzini2a74003a2016-02-24 11:26:10 +01002273 bool ret = false;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08002274
Sasha Levinb67bfe02013-02-27 17:06:00 -08002275 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002276 if (!s->unsync)
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08002277 continue;
2278
2279 WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
Paolo Bonzini2a74003a2016-02-24 11:26:10 +01002280 ret |= kvm_sync_page(vcpu, s, invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08002281 }
2282
Paolo Bonzini2a74003a2016-02-24 11:26:10 +01002283 return ret;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08002284}
2285
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002286struct mmu_page_path {
Yu Zhang2a7266a2017-08-24 20:27:54 +08002287 struct kvm_mmu_page *parent[PT64_ROOT_MAX_LEVEL];
2288 unsigned int idx[PT64_ROOT_MAX_LEVEL];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002289};
2290
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002291#define for_each_sp(pvec, sp, parents, i) \
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01002292 for (i = mmu_pages_first(&pvec, &parents); \
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002293 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
2294 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002295
Hannes Edercded19f2009-02-21 02:19:13 +01002296static int mmu_pages_next(struct kvm_mmu_pages *pvec,
2297 struct mmu_page_path *parents,
2298 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002299{
2300 int n;
2301
2302 for (n = i+1; n < pvec->nr; n++) {
2303 struct kvm_mmu_page *sp = pvec->page[n].sp;
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01002304 unsigned idx = pvec->page[n].idx;
2305 int level = sp->role.level;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002306
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01002307 parents->idx[level-1] = idx;
2308 if (level == PT_PAGE_TABLE_LEVEL)
2309 break;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002310
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01002311 parents->parent[level-2] = sp;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002312 }
2313
2314 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002315}
2316
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01002317static int mmu_pages_first(struct kvm_mmu_pages *pvec,
2318 struct mmu_page_path *parents)
2319{
2320 struct kvm_mmu_page *sp;
2321 int level;
2322
2323 if (pvec->nr == 0)
2324 return 0;
2325
Xiao Guangronge23d3fe2016-02-24 09:46:06 +01002326 WARN_ON(pvec->page[0].idx != INVALID_INDEX);
2327
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01002328 sp = pvec->page[0].sp;
2329 level = sp->role.level;
2330 WARN_ON(level == PT_PAGE_TABLE_LEVEL);
2331
2332 parents->parent[level-2] = sp;
2333
2334 /* Also set up a sentinel. Further entries in pvec are all
2335 * children of sp, so this element is never overwritten.
2336 */
2337 parents->parent[level-1] = NULL;
2338 return mmu_pages_next(pvec, parents, 0);
2339}
2340
Hannes Edercded19f2009-02-21 02:19:13 +01002341static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002342{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002343 struct kvm_mmu_page *sp;
2344 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002345
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002346 do {
2347 unsigned int idx = parents->idx[level];
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002348 sp = parents->parent[level];
2349 if (!sp)
2350 return;
2351
Xiao Guangronge23d3fe2016-02-24 09:46:06 +01002352 WARN_ON(idx == INVALID_INDEX);
Takuya Yoshikawafd951452015-11-20 17:43:13 +09002353 clear_unsync_child_bit(sp, idx);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002354 level++;
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01002355 } while (!sp->unsync_children);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002356}
2357
2358static void mmu_sync_children(struct kvm_vcpu *vcpu,
2359 struct kvm_mmu_page *parent)
2360{
2361 int i;
2362 struct kvm_mmu_page *sp;
2363 struct mmu_page_path parents;
2364 struct kvm_mmu_pages pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002365 LIST_HEAD(invalid_list);
Paolo Bonzini50c9e6f2016-02-25 10:47:38 +01002366 bool flush = false;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002367
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002368 while (mmu_unsync_walk(parent, &pages)) {
Xiao Guangrong2f845692012-06-20 15:56:53 +08002369 bool protected = false;
Marcelo Tosattib1a36822008-12-01 22:32:03 -02002370
2371 for_each_sp(pages, sp, parents, i)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002372 protected |= rmap_write_protect(vcpu, sp->gfn);
Marcelo Tosattib1a36822008-12-01 22:32:03 -02002373
Paolo Bonzini50c9e6f2016-02-25 10:47:38 +01002374 if (protected) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02002375 kvm_flush_remote_tlbs(vcpu->kvm);
Paolo Bonzini50c9e6f2016-02-25 10:47:38 +01002376 flush = false;
2377 }
Marcelo Tosattib1a36822008-12-01 22:32:03 -02002378
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002379 for_each_sp(pages, sp, parents, i) {
Paolo Bonzini1f50f1b2016-02-24 11:07:14 +01002380 flush |= kvm_sync_page(vcpu, sp, &invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002381 mmu_pages_clear_parents(&parents);
2382 }
Paolo Bonzini50c9e6f2016-02-25 10:47:38 +01002383 if (need_resched() || spin_needbreak(&vcpu->kvm->mmu_lock)) {
2384 kvm_mmu_flush_or_zap(vcpu, &invalid_list, false, flush);
2385 cond_resched_lock(&vcpu->kvm->mmu_lock);
2386 flush = false;
2387 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002388 }
Paolo Bonzini50c9e6f2016-02-25 10:47:38 +01002389
2390 kvm_mmu_flush_or_zap(vcpu, &invalid_list, false, flush);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002391}
2392
Xiao Guangronga30f47c2011-09-22 16:58:36 +08002393static void __clear_sp_write_flooding_count(struct kvm_mmu_page *sp)
2394{
Xiao Guangronge5691a82016-02-24 17:51:12 +08002395 atomic_set(&sp->write_flooding_count, 0);
Xiao Guangronga30f47c2011-09-22 16:58:36 +08002396}
2397
2398static void clear_sp_write_flooding_count(u64 *spte)
2399{
2400 struct kvm_mmu_page *sp = page_header(__pa(spte));
2401
2402 __clear_sp_write_flooding_count(sp);
2403}
2404
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002405static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
2406 gfn_t gfn,
2407 gva_t gaddr,
2408 unsigned level,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002409 int direct,
Takuya Yoshikawabb11c6c2015-11-26 21:16:35 +09002410 unsigned access)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002411{
2412 union kvm_mmu_page_role role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002413 unsigned quadrant;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08002414 struct kvm_mmu_page *sp;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08002415 bool need_sync = false;
Paolo Bonzini2a74003a2016-02-24 11:26:10 +01002416 bool flush = false;
David Matlackf3414bc2016-12-20 15:25:57 -08002417 int collisions = 0;
Paolo Bonzini2a74003a2016-02-24 11:26:10 +01002418 LIST_HEAD(invalid_list);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002419
Vitaly Kuznetsov36d9594d2018-10-08 21:28:10 +02002420 role = vcpu->arch.mmu->mmu_role.base;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002421 role.level = level;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002422 role.direct = direct;
Avi Kivity84b0c8c2010-03-14 10:16:40 +02002423 if (role.direct)
Avi Kivity5b7e0102010-04-14 19:20:03 +03002424 role.cr4_pae = 0;
Avi Kivity41074d02007-12-09 17:00:02 +02002425 role.access = access;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02002426 if (!vcpu->arch.mmu->direct_map
2427 && vcpu->arch.mmu->root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002428 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
2429 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
2430 role.quadrant = quadrant;
2431 }
David Matlackf3414bc2016-12-20 15:25:57 -08002432 for_each_valid_sp(vcpu->kvm, sp, gfn) {
2433 if (sp->gfn != gfn) {
2434 collisions++;
2435 continue;
2436 }
2437
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002438 if (!need_sync && sp->unsync)
2439 need_sync = true;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002440
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002441 if (sp->role.word != role.word)
2442 continue;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002443
Paolo Bonzini2a74003a2016-02-24 11:26:10 +01002444 if (sp->unsync) {
2445 /* The page is good, but __kvm_sync_page might still end
2446 * up zapping it. If so, break in order to rebuild it.
2447 */
2448 if (!__kvm_sync_page(vcpu, sp, &invalid_list))
2449 break;
2450
2451 WARN_ON(!list_empty(&invalid_list));
2452 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
2453 }
Xiao Guangronge02aa902010-05-15 18:52:34 +08002454
Takuya Yoshikawa98bba232015-11-26 21:14:34 +09002455 if (sp->unsync_children)
Avi Kivitya8eeb042010-05-10 12:34:53 +03002456 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
Xiao Guangronge02aa902010-05-15 18:52:34 +08002457
Xiao Guangronga30f47c2011-09-22 16:58:36 +08002458 __clear_sp_write_flooding_count(sp);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002459 trace_kvm_mmu_get_page(sp, false);
David Matlackf3414bc2016-12-20 15:25:57 -08002460 goto out;
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002461 }
Takuya Yoshikawa47005792015-11-20 17:46:29 +09002462
Avi Kivitydfc5aa02007-12-18 19:47:18 +02002463 ++vcpu->kvm->stat.mmu_cache_miss;
Takuya Yoshikawa47005792015-11-20 17:46:29 +09002464
2465 sp = kvm_mmu_alloc_page(vcpu, direct);
2466
Avi Kivity4db35312007-11-21 15:28:32 +02002467 sp->gfn = gfn;
2468 sp->role = role;
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002469 hlist_add_head(&sp->hash_link,
2470 &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002471 if (!direct) {
Xiao Guangrong56ca57f2016-02-24 17:51:14 +08002472 /*
2473 * we should do write protection before syncing pages
2474 * otherwise the content of the synced shadow page may
2475 * be inconsistent with guest page table.
2476 */
Paolo Bonzini3ed1a472015-05-19 16:29:22 +02002477 account_shadowed(vcpu->kvm, sp);
Xiao Guangrong56ca57f2016-02-24 17:51:14 +08002478 if (level == PT_PAGE_TABLE_LEVEL &&
2479 rmap_write_protect(vcpu, gfn))
Lan Tianyuc3134ce2018-12-06 21:21:09 +08002480 kvm_flush_remote_tlbs_with_address(vcpu->kvm, gfn, 1);
Xiao Guangrong56ca57f2016-02-24 17:51:14 +08002481
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08002482 if (level > PT_PAGE_TABLE_LEVEL && need_sync)
Paolo Bonzini2a74003a2016-02-24 11:26:10 +01002483 flush |= kvm_sync_pages(vcpu, gfn, &invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002484 }
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08002485 sp->mmu_valid_gen = vcpu->kvm->arch.mmu_valid_gen;
Takuya Yoshikawa77492662015-12-18 18:54:49 +09002486 clear_page(sp->spt);
Avi Kivityf691fe12009-07-06 15:58:14 +03002487 trace_kvm_mmu_get_page(sp, true);
Paolo Bonzini2a74003a2016-02-24 11:26:10 +01002488
2489 kvm_mmu_flush_or_zap(vcpu, &invalid_list, false, flush);
David Matlackf3414bc2016-12-20 15:25:57 -08002490out:
2491 if (collisions > vcpu->kvm->stat.max_mmu_page_hash_collisions)
2492 vcpu->kvm->stat.max_mmu_page_hash_collisions = collisions;
Avi Kivity4db35312007-11-21 15:28:32 +02002493 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002494}
2495
Junaid Shahid7eb77e92018-06-27 14:59:16 -07002496static void shadow_walk_init_using_root(struct kvm_shadow_walk_iterator *iterator,
2497 struct kvm_vcpu *vcpu, hpa_t root,
2498 u64 addr)
Avi Kivity2d111232008-12-25 14:39:47 +02002499{
2500 iterator->addr = addr;
Junaid Shahid7eb77e92018-06-27 14:59:16 -07002501 iterator->shadow_addr = root;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02002502 iterator->level = vcpu->arch.mmu->shadow_root_level;
Joerg Roedel81407ca2010-09-10 17:31:00 +02002503
Yu Zhang2a7266a2017-08-24 20:27:54 +08002504 if (iterator->level == PT64_ROOT_4LEVEL &&
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02002505 vcpu->arch.mmu->root_level < PT64_ROOT_4LEVEL &&
2506 !vcpu->arch.mmu->direct_map)
Joerg Roedel81407ca2010-09-10 17:31:00 +02002507 --iterator->level;
2508
Avi Kivity2d111232008-12-25 14:39:47 +02002509 if (iterator->level == PT32E_ROOT_LEVEL) {
Junaid Shahid7eb77e92018-06-27 14:59:16 -07002510 /*
2511 * prev_root is currently only used for 64-bit hosts. So only
2512 * the active root_hpa is valid here.
2513 */
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02002514 BUG_ON(root != vcpu->arch.mmu->root_hpa);
Junaid Shahid7eb77e92018-06-27 14:59:16 -07002515
Avi Kivity2d111232008-12-25 14:39:47 +02002516 iterator->shadow_addr
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02002517 = vcpu->arch.mmu->pae_root[(addr >> 30) & 3];
Avi Kivity2d111232008-12-25 14:39:47 +02002518 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
2519 --iterator->level;
2520 if (!iterator->shadow_addr)
2521 iterator->level = 0;
2522 }
2523}
2524
Junaid Shahid7eb77e92018-06-27 14:59:16 -07002525static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
2526 struct kvm_vcpu *vcpu, u64 addr)
2527{
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02002528 shadow_walk_init_using_root(iterator, vcpu, vcpu->arch.mmu->root_hpa,
Junaid Shahid7eb77e92018-06-27 14:59:16 -07002529 addr);
2530}
2531
Avi Kivity2d111232008-12-25 14:39:47 +02002532static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
2533{
2534 if (iterator->level < PT_PAGE_TABLE_LEVEL)
2535 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03002536
Avi Kivity2d111232008-12-25 14:39:47 +02002537 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
2538 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
2539 return true;
2540}
2541
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08002542static void __shadow_walk_next(struct kvm_shadow_walk_iterator *iterator,
2543 u64 spte)
Avi Kivity2d111232008-12-25 14:39:47 +02002544{
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08002545 if (is_last_spte(spte, iterator->level)) {
Xiao Guangrong052331b2011-07-12 03:21:17 +08002546 iterator->level = 0;
2547 return;
2548 }
2549
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08002550 iterator->shadow_addr = spte & PT64_BASE_ADDR_MASK;
Avi Kivity2d111232008-12-25 14:39:47 +02002551 --iterator->level;
2552}
2553
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08002554static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
2555{
David Hildenbrandbb606a92017-08-24 20:51:23 +02002556 __shadow_walk_next(iterator, *iterator->sptep);
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08002557}
2558
Takuya Yoshikawa98bba232015-11-26 21:14:34 +09002559static void link_shadow_page(struct kvm_vcpu *vcpu, u64 *sptep,
2560 struct kvm_mmu_page *sp)
Avi Kivity32ef26a2010-07-13 14:27:04 +03002561{
2562 u64 spte;
2563
Bandan Dasffb128c2016-07-12 18:18:49 -04002564 BUILD_BUG_ON(VMX_EPT_WRITABLE_MASK != PT_WRITABLE_MASK);
Yang Zhang7a1638c2013-08-05 11:07:13 +03002565
Bandan Dasffb128c2016-07-12 18:18:49 -04002566 spte = __pa(sp->spt) | shadow_present_mask | PT_WRITABLE_MASK |
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002567 shadow_user_mask | shadow_x_mask | shadow_me_mask;
Peter Feinerac8d57e2017-06-30 17:26:31 -07002568
2569 if (sp_ad_disabled(sp))
2570 spte |= shadow_acc_track_value;
2571 else
2572 spte |= shadow_accessed_mask;
Xiao Guangrong24db2732013-02-05 15:28:02 +08002573
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08002574 mmu_spte_set(sptep, spte);
Takuya Yoshikawa98bba232015-11-26 21:14:34 +09002575
2576 mmu_page_add_parent_pte(vcpu, sp, sptep);
2577
2578 if (sp->unsync_children || sp->unsync)
2579 mark_unsync(sptep);
Avi Kivity32ef26a2010-07-13 14:27:04 +03002580}
2581
Avi Kivitya357bd22010-07-13 14:27:07 +03002582static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
2583 unsigned direct_access)
2584{
2585 if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
2586 struct kvm_mmu_page *child;
2587
2588 /*
2589 * For the direct sp, if the guest pte's dirty bit
2590 * changed form clean to dirty, it will corrupt the
2591 * sp's access: allow writable in the read-only sp,
2592 * so we should update the spte at this point to get
2593 * a new sp with the correct access.
2594 */
2595 child = page_header(*sptep & PT64_BASE_ADDR_MASK);
2596 if (child->role.access == direct_access)
2597 return;
2598
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08002599 drop_parent_pte(child, sptep);
Lan Tianyuc3134ce2018-12-06 21:21:09 +08002600 kvm_flush_remote_tlbs_with_address(vcpu->kvm, child->gfn, 1);
Avi Kivitya357bd22010-07-13 14:27:07 +03002601 }
2602}
2603
Xiao Guangrong505aef82011-09-22 16:56:06 +08002604static bool mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08002605 u64 *spte)
2606{
2607 u64 pte;
2608 struct kvm_mmu_page *child;
2609
2610 pte = *spte;
2611 if (is_shadow_present_pte(pte)) {
Xiao Guangrong505aef82011-09-22 16:56:06 +08002612 if (is_last_spte(pte, sp->role.level)) {
Xiao Guangrongc3707952011-07-12 03:28:04 +08002613 drop_spte(kvm, spte);
Xiao Guangrong505aef82011-09-22 16:56:06 +08002614 if (is_large_pte(pte))
2615 --kvm->stat.lpages;
2616 } else {
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08002617 child = page_header(pte & PT64_BASE_ADDR_MASK);
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08002618 drop_parent_pte(child, spte);
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08002619 }
Xiao Guangrong505aef82011-09-22 16:56:06 +08002620 return true;
2621 }
2622
2623 if (is_mmio_spte(pte))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08002624 mmu_spte_clear_no_track(spte);
Xiao Guangrongc3707952011-07-12 03:28:04 +08002625
Xiao Guangrong505aef82011-09-22 16:56:06 +08002626 return false;
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08002627}
2628
Avi Kivity90cb0522007-07-17 13:04:56 +03002629static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02002630 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08002631{
Avi Kivity697fe2e2007-01-05 16:36:46 -08002632 unsigned i;
Avi Kivity697fe2e2007-01-05 16:36:46 -08002633
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08002634 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2635 mmu_page_zap_pte(kvm, sp, sp->spt + i);
Avi Kivitya4360362007-01-05 16:36:45 -08002636}
2637
Avi Kivity31aa2b42008-07-11 17:59:46 +03002638static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08002639{
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09002640 u64 *sptep;
2641 struct rmap_iterator iter;
Avi Kivitya4360362007-01-05 16:36:45 -08002642
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09002643 while ((sptep = rmap_get_first(&sp->parent_ptes, &iter)))
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09002644 drop_parent_pte(sp, sptep);
Avi Kivity31aa2b42008-07-11 17:59:46 +03002645}
2646
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002647static int mmu_zap_unsync_children(struct kvm *kvm,
Xiao Guangrong77758342010-06-04 21:53:54 +08002648 struct kvm_mmu_page *parent,
2649 struct list_head *invalid_list)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002650{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002651 int i, zapped = 0;
2652 struct mmu_page_path parents;
2653 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002654
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002655 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002656 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002657
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002658 while (mmu_unsync_walk(parent, &pages)) {
2659 struct kvm_mmu_page *sp;
2660
2661 for_each_sp(pages, sp, parents, i) {
Xiao Guangrong77758342010-06-04 21:53:54 +08002662 kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002663 mmu_pages_clear_parents(&parents);
Xiao Guangrong77662e02010-04-16 16:34:42 +08002664 zapped++;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002665 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002666 }
2667
2668 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002669}
2670
Xiao Guangrong77758342010-06-04 21:53:54 +08002671static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
2672 struct list_head *invalid_list)
Avi Kivity31aa2b42008-07-11 17:59:46 +03002673{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002674 int ret;
Avi Kivityf691fe12009-07-06 15:58:14 +03002675
Xiao Guangrong77758342010-06-04 21:53:54 +08002676 trace_kvm_mmu_prepare_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03002677 ++kvm->stat.mmu_shadow_zapped;
Xiao Guangrong77758342010-06-04 21:53:54 +08002678 ret = mmu_zap_unsync_children(kvm, sp, invalid_list);
Avi Kivity4db35312007-11-21 15:28:32 +02002679 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03002680 kvm_mmu_unlink_parents(kvm, sp);
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08002681
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002682 if (!sp->role.invalid && !sp->role.direct)
Paolo Bonzini3ed1a472015-05-19 16:29:22 +02002683 unaccount_shadowed(kvm, sp);
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08002684
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002685 if (sp->unsync)
2686 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02002687 if (!sp->root_count) {
Gui Jianfeng54a4f022010-05-05 09:03:49 +08002688 /* Count self */
2689 ret++;
Xiao Guangrong77758342010-06-04 21:53:54 +08002690 list_move(&sp->link, invalid_list);
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08002691 kvm_mod_used_mmu_pages(kvm, -1);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002692 } else {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03002693 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Gleb Natapov05988d72013-05-31 08:36:30 +08002694
2695 /*
2696 * The obsolete pages can not be used on any vcpus.
2697 * See the comments in kvm_mmu_invalidate_zap_all_pages().
2698 */
2699 if (!sp->role.invalid && !is_obsolete_sp(kvm, sp))
2700 kvm_reload_remote_mmus(kvm);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002701 }
Xiao Guangrong77758342010-06-04 21:53:54 +08002702
2703 sp->role.invalid = 1;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002704 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08002705}
2706
Xiao Guangrong77758342010-06-04 21:53:54 +08002707static void kvm_mmu_commit_zap_page(struct kvm *kvm,
2708 struct list_head *invalid_list)
2709{
Takuya Yoshikawa945315b2013-03-06 16:05:52 +09002710 struct kvm_mmu_page *sp, *nsp;
Xiao Guangrong77758342010-06-04 21:53:54 +08002711
2712 if (list_empty(invalid_list))
2713 return;
2714
Avi Kivityc1427862012-05-14 15:44:06 +03002715 /*
Lan Tianyu9753f522016-03-13 11:10:24 +08002716 * We need to make sure everyone sees our modifications to
2717 * the page tables and see changes to vcpu->mode here. The barrier
2718 * in the kvm_flush_remote_tlbs() achieves this. This pairs
2719 * with vcpu_enter_guest and walk_shadow_page_lockless_begin/end.
2720 *
2721 * In addition, kvm_flush_remote_tlbs waits for all vcpus to exit
2722 * guest mode and/or lockless shadow page table walks.
Avi Kivityc1427862012-05-14 15:44:06 +03002723 */
Xiao Guangrong77758342010-06-04 21:53:54 +08002724 kvm_flush_remote_tlbs(kvm);
2725
Takuya Yoshikawa945315b2013-03-06 16:05:52 +09002726 list_for_each_entry_safe(sp, nsp, invalid_list, link) {
Xiao Guangrong77758342010-06-04 21:53:54 +08002727 WARN_ON(!sp->role.invalid || sp->root_count);
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08002728 kvm_mmu_free_page(sp);
Takuya Yoshikawa945315b2013-03-06 16:05:52 +09002729 }
Xiao Guangrong77758342010-06-04 21:53:54 +08002730}
2731
Takuya Yoshikawa5da59602013-03-06 16:06:58 +09002732static bool prepare_zap_oldest_mmu_page(struct kvm *kvm,
2733 struct list_head *invalid_list)
2734{
2735 struct kvm_mmu_page *sp;
2736
2737 if (list_empty(&kvm->arch.active_mmu_pages))
2738 return false;
2739
Geliang Tangd74c0e62016-01-01 19:47:14 +08002740 sp = list_last_entry(&kvm->arch.active_mmu_pages,
2741 struct kvm_mmu_page, link);
Wanpeng Li42bcbeb2017-08-10 06:55:51 -07002742 return kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
Takuya Yoshikawa5da59602013-03-06 16:06:58 +09002743}
2744
Izik Eidus82ce2c92007-10-02 18:52:55 +02002745/*
2746 * Changing the number of mmu pages allocated to the vm
Dave Hansen49d5ca22010-08-19 18:11:28 -07002747 * Note: if goal_nr_mmu_pages is too small, you will get dead lock
Izik Eidus82ce2c92007-10-02 18:52:55 +02002748 */
Dave Hansen49d5ca22010-08-19 18:11:28 -07002749void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages)
Izik Eidus82ce2c92007-10-02 18:52:55 +02002750{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002751 LIST_HEAD(invalid_list);
Izik Eidus82ce2c92007-10-02 18:52:55 +02002752
Takuya Yoshikawab34cb592013-01-08 19:46:07 +09002753 spin_lock(&kvm->mmu_lock);
2754
Dave Hansen49d5ca22010-08-19 18:11:28 -07002755 if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) {
Takuya Yoshikawa5da59602013-03-06 16:06:58 +09002756 /* Need to free some mmu pages to achieve the goal. */
2757 while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages)
2758 if (!prepare_zap_oldest_mmu_page(kvm, &invalid_list))
2759 break;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002760
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08002761 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Dave Hansen49d5ca22010-08-19 18:11:28 -07002762 goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002763 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02002764
Dave Hansen49d5ca22010-08-19 18:11:28 -07002765 kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages;
Takuya Yoshikawab34cb592013-01-08 19:46:07 +09002766
2767 spin_unlock(&kvm->mmu_lock);
Izik Eidus82ce2c92007-10-02 18:52:55 +02002768}
2769
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08002770int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08002771{
Avi Kivity4db35312007-11-21 15:28:32 +02002772 struct kvm_mmu_page *sp;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002773 LIST_HEAD(invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08002774 int r;
2775
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002776 pgprintk("%s: looking for gfn %llx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08002777 r = 0;
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08002778 spin_lock(&kvm->mmu_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002779 for_each_gfn_indirect_valid_sp(kvm, sp, gfn) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002780 pgprintk("%s: gfn %llx role %x\n", __func__, gfn,
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002781 sp->role.word);
2782 r = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002783 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002784 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002785 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08002786 spin_unlock(&kvm->mmu_lock);
2787
Avi Kivitya4360362007-01-05 16:36:45 -08002788 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002789}
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08002790EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002791
Xiao Guangrong5c520e92016-02-24 17:51:15 +08002792static void kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002793{
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08002794 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002795 ++vcpu->kvm->stat.mmu_unsync;
2796 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02002797
Xiao Guangrong6b184932010-04-16 21:29:17 +08002798 kvm_mmu_mark_parents_unsync(sp);
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002799}
2800
Xiao Guangrong3d0c27a2016-02-24 17:51:11 +08002801static bool mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
2802 bool can_unsync)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002803{
Xiao Guangrong5c520e92016-02-24 17:51:15 +08002804 struct kvm_mmu_page *sp;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002805
Xiao Guangrong3d0c27a2016-02-24 17:51:11 +08002806 if (kvm_page_track_is_active(vcpu, gfn, KVM_PAGE_TRACK_WRITE))
2807 return true;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002808
Xiao Guangrong5c520e92016-02-24 17:51:15 +08002809 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08002810 if (!can_unsync)
Xiao Guangrong3d0c27a2016-02-24 17:51:11 +08002811 return true;
Xiao Guangrong36a2e672010-06-30 16:02:02 +08002812
Xiao Guangrong5c520e92016-02-24 17:51:15 +08002813 if (sp->unsync)
2814 continue;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002815
Xiao Guangrong5c520e92016-02-24 17:51:15 +08002816 WARN_ON(sp->role.level != PT_PAGE_TABLE_LEVEL);
2817 kvm_unsync_page(vcpu, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002818 }
Xiao Guangrong3d0c27a2016-02-24 17:51:11 +08002819
Junaid Shahid578e1c42018-06-27 14:59:05 -07002820 /*
2821 * We need to ensure that the marking of unsync pages is visible
2822 * before the SPTE is updated to allow writes because
2823 * kvm_mmu_sync_roots() checks the unsync flags without holding
2824 * the MMU lock and so can race with this. If the SPTE was updated
2825 * before the page had been marked as unsync-ed, something like the
2826 * following could happen:
2827 *
2828 * CPU 1 CPU 2
2829 * ---------------------------------------------------------------------
2830 * 1.2 Host updates SPTE
2831 * to be writable
2832 * 2.1 Guest writes a GPTE for GVA X.
2833 * (GPTE being in the guest page table shadowed
2834 * by the SP from CPU 1.)
2835 * This reads SPTE during the page table walk.
2836 * Since SPTE.W is read as 1, there is no
2837 * fault.
2838 *
2839 * 2.2 Guest issues TLB flush.
2840 * That causes a VM Exit.
2841 *
2842 * 2.3 kvm_mmu_sync_pages() reads sp->unsync.
2843 * Since it is false, so it just returns.
2844 *
2845 * 2.4 Guest accesses GVA X.
2846 * Since the mapping in the SP was not updated,
2847 * so the old mapping for GVA X incorrectly
2848 * gets used.
2849 * 1.1 Host marks SP
2850 * as unsync
2851 * (sp->unsync = true)
2852 *
2853 * The write barrier below ensures that 1.1 happens before 1.2 and thus
2854 * the situation in 2.4 does not arise. The implicit barrier in 2.2
2855 * pairs with this write barrier.
2856 */
2857 smp_wmb();
2858
Xiao Guangrong3d0c27a2016-02-24 17:51:11 +08002859 return false;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002860}
2861
Dan Williamsba049e92016-01-15 16:56:11 -08002862static bool kvm_is_mmio_pfn(kvm_pfn_t pfn)
Paolo Bonzinid1fe9212015-07-07 15:03:18 +02002863{
2864 if (pfn_valid(pfn))
Haozhong Zhangaa2e0632017-12-20 15:29:29 +08002865 return !is_zero_pfn(pfn) && PageReserved(pfn_to_page(pfn)) &&
2866 /*
2867 * Some reserved pages, such as those from NVDIMM
2868 * DAX devices, are not for MMIO, and can be mapped
2869 * with cached memory type for better performance.
2870 * However, the above check misconceives those pages
2871 * as MMIO, and results in KVM mapping them with UC
2872 * memory type, which would hurt the performance.
2873 * Therefore, we check the host memory type in addition
2874 * and only treat UC/UC-/WC pages as MMIO.
2875 */
2876 (!pat_enabled() || pat_pfn_immune_to_uc_mtrr(pfn));
Paolo Bonzinid1fe9212015-07-07 15:03:18 +02002877
2878 return true;
2879}
2880
Junaid Shahid5ce47862018-06-27 14:59:04 -07002881/* Bits which may be returned by set_spte() */
2882#define SET_SPTE_WRITE_PROTECTED_PT BIT(0)
2883#define SET_SPTE_NEED_REMOTE_TLB_FLUSH BIT(1)
2884
Avi Kivityd555c332009-06-10 14:24:23 +03002885static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Xiao Guangrongc22885052013-01-08 14:36:04 +08002886 unsigned pte_access, int level,
Dan Williamsba049e92016-01-15 16:56:11 -08002887 gfn_t gfn, kvm_pfn_t pfn, bool speculative,
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08002888 bool can_unsync, bool host_writable)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002889{
Bandan Dasffb128c2016-07-12 18:18:49 -04002890 u64 spte = 0;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002891 int ret = 0;
Peter Feinerac8d57e2017-06-30 17:26:31 -07002892 struct kvm_mmu_page *sp;
Sheng Yang64d4d522008-10-09 16:01:57 +08002893
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002894 if (set_mmio_spte(vcpu, sptep, gfn, pfn, pte_access))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08002895 return 0;
2896
Peter Feinerac8d57e2017-06-30 17:26:31 -07002897 sp = page_header(__pa(sptep));
2898 if (sp_ad_disabled(sp))
2899 spte |= shadow_acc_track_value;
2900
Bandan Dasd95c5562016-07-12 18:18:51 -04002901 /*
2902 * For the EPT case, shadow_present_mask is 0 if hardware
2903 * supports exec-only page table entries. In that case,
2904 * ACC_USER_MASK and shadow_user_mask are used to represent
2905 * read access. See FNAME(gpte_access) in paging_tmpl.h.
2906 */
Bandan Dasffb128c2016-07-12 18:18:49 -04002907 spte |= shadow_present_mask;
Avi Kivity947da532008-03-18 11:05:52 +02002908 if (!speculative)
Peter Feinerac8d57e2017-06-30 17:26:31 -07002909 spte |= spte_shadow_accessed_mask(spte);
Xiao Guangrong640d9b02011-07-12 03:24:39 +08002910
Sheng Yang7b523452008-04-25 21:13:50 +08002911 if (pte_access & ACC_EXEC_MASK)
2912 spte |= shadow_x_mask;
2913 else
2914 spte |= shadow_nx_mask;
Xiao Guangrong49fde342012-06-20 15:58:58 +08002915
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002916 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08002917 spte |= shadow_user_mask;
Xiao Guangrong49fde342012-06-20 15:58:58 +08002918
Joerg Roedel852e3c12009-07-27 16:30:44 +02002919 if (level > PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002920 spte |= PT_PAGE_SIZE_MASK;
Avi Kivityb0bc3ee2010-09-13 16:45:28 +02002921 if (tdp_enabled)
Sheng Yang4b12f0d2009-04-27 20:35:42 +08002922 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
Paolo Bonzinid1fe9212015-07-07 15:03:18 +02002923 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002924
Lai Jiangshan9bdbba12010-11-19 17:03:22 +08002925 if (host_writable)
Izik Eidus14032832009-09-23 21:47:17 +03002926 spte |= SPTE_HOST_WRITEABLE;
Xiao Guangrongf8e453b2010-12-23 16:09:29 +08002927 else
2928 pte_access &= ~ACC_WRITE_MASK;
Izik Eidus14032832009-09-23 21:47:17 +03002929
Tom Lendackydaaf2162018-03-08 17:17:31 -06002930 if (!kvm_is_mmio_pfn(pfn))
2931 spte |= shadow_me_mask;
2932
Anthony Liguori35149e22008-04-02 14:46:56 -05002933 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002934
Xiao Guangrongc22885052013-01-08 14:36:04 +08002935 if (pte_access & ACC_WRITE_MASK) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002936
Xiao Guangrongc2193462012-12-04 07:17:11 +08002937 /*
Xiao Guangrong7751bab2013-01-08 14:36:51 +08002938 * Other vcpu creates new sp in the window between
2939 * mapping_level() and acquiring mmu-lock. We can
2940 * allow guest to retry the access, the mapping can
2941 * be fixed if guest refault.
Xiao Guangrongc2193462012-12-04 07:17:11 +08002942 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02002943 if (level > PT_PAGE_TABLE_LEVEL &&
Xiao Guangrong92f94f12016-02-24 17:51:06 +08002944 mmu_gfn_lpage_is_disallowed(vcpu, gfn, level))
Avi Kivitybe38d272010-06-06 14:31:27 +03002945 goto done;
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002946
Xiao Guangrong49fde342012-06-20 15:58:58 +08002947 spte |= PT_WRITABLE_MASK | SPTE_MMU_WRITEABLE;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002948
Marcelo Tosattiecc55892008-11-25 15:58:07 +01002949 /*
2950 * Optimization: for pte sync, if spte was writable the hash
2951 * lookup is unnecessary (and expensive). Write protection
2952 * is responsibility of mmu_get_page / kvm_sync_page.
2953 * Same reasoning can be applied to dirty page accounting.
2954 */
Takuya Yoshikawa8dae4442010-01-18 18:45:10 +09002955 if (!can_unsync && is_writable_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01002956 goto set_pte;
2957
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002958 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002959 pgprintk("%s: found shadow page for %llx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002960 __func__, gfn);
Junaid Shahid5ce47862018-06-27 14:59:04 -07002961 ret |= SET_SPTE_WRITE_PROTECTED_PT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002962 pte_access &= ~ACC_WRITE_MASK;
Xiao Guangrong49fde342012-06-20 15:58:58 +08002963 spte &= ~(PT_WRITABLE_MASK | SPTE_MMU_WRITEABLE);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002964 }
2965 }
2966
Kai Huang9b51a632015-01-28 10:54:25 +08002967 if (pte_access & ACC_WRITE_MASK) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002968 kvm_vcpu_mark_page_dirty(vcpu, gfn);
Peter Feinerac8d57e2017-06-30 17:26:31 -07002969 spte |= spte_shadow_dirty_mask(spte);
Kai Huang9b51a632015-01-28 10:54:25 +08002970 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002971
Junaid Shahidf160c7b2016-12-06 16:46:16 -08002972 if (speculative)
2973 spte = mark_spte_for_access_track(spte);
2974
Marcelo Tosatti38187c82008-09-23 13:18:32 -03002975set_pte:
Xiao Guangrong6e7d0352012-06-20 15:58:33 +08002976 if (mmu_spte_update(sptep, spte))
Junaid Shahid5ce47862018-06-27 14:59:04 -07002977 ret |= SET_SPTE_NEED_REMOTE_TLB_FLUSH;
Avi Kivitybe38d272010-06-06 14:31:27 +03002978done:
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002979 return ret;
2980}
2981
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02002982static int mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep, unsigned pte_access,
2983 int write_fault, int level, gfn_t gfn, kvm_pfn_t pfn,
2984 bool speculative, bool host_writable)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002985{
2986 int was_rmapped = 0;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002987 int rmap_count;
Junaid Shahid5ce47862018-06-27 14:59:04 -07002988 int set_spte_ret;
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02002989 int ret = RET_PF_RETRY;
Tianyu Lanc2a4ead2018-07-24 08:17:07 +00002990 bool flush = false;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002991
Xiao Guangrongf7616202013-02-05 15:27:27 +08002992 pgprintk("%s: spte %llx write_fault %d gfn %llx\n", __func__,
2993 *sptep, write_fault, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002994
Takuya Yoshikawaafd28fe2015-11-20 17:44:55 +09002995 if (is_shadow_present_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002996 /*
2997 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
2998 * the parent of the now unreachable PTE.
2999 */
Joerg Roedel852e3c12009-07-27 16:30:44 +02003000 if (level > PT_PAGE_TABLE_LEVEL &&
3001 !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03003002 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03003003 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03003004
3005 child = page_header(pte & PT64_BASE_ADDR_MASK);
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08003006 drop_parent_pte(child, sptep);
Tianyu Lanc2a4ead2018-07-24 08:17:07 +00003007 flush = true;
Avi Kivityd555c332009-06-10 14:24:23 +03003008 } else if (pfn != spte_to_pfn(*sptep)) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08003009 pgprintk("hfn old %llx new %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03003010 spte_to_pfn(*sptep), pfn);
Xiao Guangrongc3707952011-07-12 03:28:04 +08003011 drop_spte(vcpu->kvm, sptep);
Tianyu Lanc2a4ead2018-07-24 08:17:07 +00003012 flush = true;
Joerg Roedel6bed6b92009-02-18 14:08:59 +01003013 } else
3014 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03003015 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02003016
Junaid Shahid5ce47862018-06-27 14:59:04 -07003017 set_spte_ret = set_spte(vcpu, sptep, pte_access, level, gfn, pfn,
3018 speculative, true, host_writable);
3019 if (set_spte_ret & SET_SPTE_WRITE_PROTECTED_PT) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03003020 if (write_fault)
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02003021 ret = RET_PF_EMULATE;
Liang Chen77c39132014-09-18 12:38:37 -04003022 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03003023 }
Lan Tianyuc3134ce2018-12-06 21:21:09 +08003024
Tianyu Lanc2a4ead2018-07-24 08:17:07 +00003025 if (set_spte_ret & SET_SPTE_NEED_REMOTE_TLB_FLUSH || flush)
Lan Tianyuc3134ce2018-12-06 21:21:09 +08003026 kvm_flush_remote_tlbs_with_address(vcpu->kvm, gfn,
3027 KVM_PAGES_PER_HPAGE(level));
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03003028
Takuya Yoshikawa029499b2015-11-20 17:44:05 +09003029 if (unlikely(is_mmio_spte(*sptep)))
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02003030 ret = RET_PF_EMULATE;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003031
Avi Kivityd555c332009-06-10 14:24:23 +03003032 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08003033 pgprintk("instantiating %s PTE (%s) at %llx (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03003034 is_large_pte(*sptep)? "2MB" : "4kB",
Junaid Shahidf160c7b2016-12-06 16:46:16 -08003035 *sptep & PT_WRITABLE_MASK ? "RW" : "R", gfn,
Joerg Roedela205bc12009-07-09 16:36:01 +02003036 *sptep, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03003037 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03003038 ++vcpu->kvm->stat.lpages;
3039
Xiao Guangrongffb61bb2011-07-12 03:22:01 +08003040 if (is_shadow_present_pte(*sptep)) {
Xiao Guangrongffb61bb2011-07-12 03:22:01 +08003041 if (!was_rmapped) {
3042 rmap_count = rmap_add(vcpu, sptep, gfn);
3043 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
3044 rmap_recycle(vcpu, sptep, gfn);
3045 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02003046 }
Xiao Guangrongcb9aaa32012-08-03 15:42:10 +08003047
Xiao Guangrongf3ac1a42012-10-16 20:07:03 +08003048 kvm_release_pfn_clean(pfn);
Takuya Yoshikawa029499b2015-11-20 17:44:05 +09003049
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02003050 return ret;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02003051}
3052
Dan Williamsba049e92016-01-15 16:56:11 -08003053static kvm_pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn,
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08003054 bool no_dirty_log)
3055{
3056 struct kvm_memory_slot *slot;
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08003057
Xiao Guangrong5d163b12011-03-09 15:43:00 +08003058 slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, no_dirty_log);
Xiao Guangrong903816f2012-07-17 21:54:11 +08003059 if (!slot)
Xiao Guangrong6c8ee572012-08-03 15:37:54 +08003060 return KVM_PFN_ERR_FAULT;
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08003061
Xiao Guangrong037d92d2012-08-21 10:59:12 +08003062 return gfn_to_pfn_memslot_atomic(slot, gfn);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08003063}
3064
3065static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,
3066 struct kvm_mmu_page *sp,
3067 u64 *start, u64 *end)
3068{
3069 struct page *pages[PTE_PREFETCH_NUM];
Paolo Bonzinid9ef13c2015-05-19 16:01:50 +02003070 struct kvm_memory_slot *slot;
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08003071 unsigned access = sp->role.access;
3072 int i, ret;
3073 gfn_t gfn;
3074
3075 gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt);
Paolo Bonzinid9ef13c2015-05-19 16:01:50 +02003076 slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, access & ACC_WRITE_MASK);
3077 if (!slot)
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08003078 return -1;
3079
Paolo Bonzinid9ef13c2015-05-19 16:01:50 +02003080 ret = gfn_to_page_many_atomic(slot, gfn, pages, end - start);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08003081 if (ret <= 0)
3082 return -1;
3083
3084 for (i = 0; i < ret; i++, gfn++, start++)
Takuya Yoshikawa029499b2015-11-20 17:44:05 +09003085 mmu_set_spte(vcpu, start, access, 0, sp->role.level, gfn,
3086 page_to_pfn(pages[i]), true, true);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08003087
3088 return 0;
3089}
3090
3091static void __direct_pte_prefetch(struct kvm_vcpu *vcpu,
3092 struct kvm_mmu_page *sp, u64 *sptep)
3093{
3094 u64 *spte, *start = NULL;
3095 int i;
3096
3097 WARN_ON(!sp->role.direct);
3098
3099 i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
3100 spte = sp->spt + i;
3101
3102 for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
Xiao Guangrongc3707952011-07-12 03:28:04 +08003103 if (is_shadow_present_pte(*spte) || spte == sptep) {
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08003104 if (!start)
3105 continue;
3106 if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0)
3107 break;
3108 start = NULL;
3109 } else if (!start)
3110 start = spte;
3111 }
3112}
3113
3114static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)
3115{
3116 struct kvm_mmu_page *sp;
3117
Peter Feinerac8d57e2017-06-30 17:26:31 -07003118 sp = page_header(__pa(sptep));
3119
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08003120 /*
Peter Feinerac8d57e2017-06-30 17:26:31 -07003121 * Without accessed bits, there's no way to distinguish between
3122 * actually accessed translations and prefetched, so disable pte
3123 * prefetch if accessed bits aren't available.
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08003124 */
Peter Feinerac8d57e2017-06-30 17:26:31 -07003125 if (sp_ad_disabled(sp))
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08003126 return;
3127
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08003128 if (sp->role.level > PT_PAGE_TABLE_LEVEL)
3129 return;
3130
3131 __direct_pte_prefetch(vcpu, sp, sptep);
3132}
3133
Takuya Yoshikawa7ee0e5b2015-11-20 17:42:23 +09003134static int __direct_map(struct kvm_vcpu *vcpu, int write, int map_writable,
Dan Williamsba049e92016-01-15 16:56:11 -08003135 int level, gfn_t gfn, kvm_pfn_t pfn, bool prefault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003136{
Avi Kivity9f652d212008-12-25 14:54:25 +02003137 struct kvm_shadow_walk_iterator iterator;
3138 struct kvm_mmu_page *sp;
Xiao Guangrongb90a0e62011-07-12 03:25:56 +08003139 int emulate = 0;
Avi Kivity9f652d212008-12-25 14:54:25 +02003140 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003141
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003142 if (!VALID_PAGE(vcpu->arch.mmu->root_hpa))
Marcelo Tosatti989c6b32013-12-19 15:28:51 -02003143 return 0;
3144
Avi Kivity9f652d212008-12-25 14:54:25 +02003145 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
Joerg Roedel852e3c12009-07-27 16:30:44 +02003146 if (iterator.level == level) {
Takuya Yoshikawa029499b2015-11-20 17:44:05 +09003147 emulate = mmu_set_spte(vcpu, iterator.sptep, ACC_ALL,
3148 write, level, gfn, pfn, prefault,
3149 map_writable);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08003150 direct_pte_prefetch(vcpu, iterator.sptep);
Avi Kivity9f652d212008-12-25 14:54:25 +02003151 ++vcpu->stat.pf_fixed;
3152 break;
3153 }
3154
Marcelo Tosatti404381c2014-02-24 13:59:32 -03003155 drop_large_spte(vcpu, iterator.sptep);
Xiao Guangrongc3707952011-07-12 03:28:04 +08003156 if (!is_shadow_present_pte(*iterator.sptep)) {
Lai Jiangshanc9fa0b32010-05-26 16:48:25 +08003157 u64 base_addr = iterator.addr;
3158
3159 base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
3160 pseudo_gfn = base_addr >> PAGE_SHIFT;
Avi Kivity9f652d212008-12-25 14:54:25 +02003161 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
Takuya Yoshikawabb11c6c2015-11-26 21:16:35 +09003162 iterator.level - 1, 1, ACC_ALL);
Avi Kivity9f652d212008-12-25 14:54:25 +02003163
Takuya Yoshikawa98bba232015-11-26 21:14:34 +09003164 link_shadow_page(vcpu, iterator.sptep, sp);
Avi Kivity9f652d212008-12-25 14:54:25 +02003165 }
3166 }
Xiao Guangrongb90a0e62011-07-12 03:25:56 +08003167 return emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003168}
3169
Huang Ying77db5cb2010-10-08 16:24:15 +08003170static void kvm_send_hwpoison_signal(unsigned long address, struct task_struct *tsk)
Huang Yingbf998152010-05-31 14:28:19 +08003171{
Eric W. Biederman585a8b92018-04-16 14:23:27 -05003172 send_sig_mceerr(BUS_MCEERR_AR, (void __user *)address, PAGE_SHIFT, tsk);
Huang Yingbf998152010-05-31 14:28:19 +08003173}
3174
Dan Williamsba049e92016-01-15 16:56:11 -08003175static int kvm_handle_bad_page(struct kvm_vcpu *vcpu, gfn_t gfn, kvm_pfn_t pfn)
Huang Yingbf998152010-05-31 14:28:19 +08003176{
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08003177 /*
3178 * Do not cache the mmio info caused by writing the readonly gfn
3179 * into the spte otherwise read access on readonly gfn also can
3180 * caused mmio page fault and treat it as mmio access.
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08003181 */
3182 if (pfn == KVM_PFN_ERR_RO_FAULT)
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02003183 return RET_PF_EMULATE;
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08003184
Xiao Guangronge6c15022012-08-03 15:38:36 +08003185 if (pfn == KVM_PFN_ERR_HWPOISON) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003186 kvm_send_hwpoison_signal(kvm_vcpu_gfn_to_hva(vcpu, gfn), current);
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02003187 return RET_PF_RETRY;
Xiao Guangrongd7c55202011-07-12 03:29:38 +08003188 }
Gleb Natapovedba23e2010-07-07 20:16:45 +03003189
Sean Christopherson2c151b22018-03-29 14:48:30 -07003190 return -EFAULT;
Huang Yingbf998152010-05-31 14:28:19 +08003191}
3192
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08003193static void transparent_hugepage_adjust(struct kvm_vcpu *vcpu,
Dan Williamsba049e92016-01-15 16:56:11 -08003194 gfn_t *gfnp, kvm_pfn_t *pfnp,
3195 int *levelp)
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08003196{
Dan Williamsba049e92016-01-15 16:56:11 -08003197 kvm_pfn_t pfn = *pfnp;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08003198 gfn_t gfn = *gfnp;
3199 int level = *levelp;
3200
3201 /*
3202 * Check if it's a transparent hugepage. If this would be an
3203 * hugetlbfs page, level wouldn't be set to
3204 * PT_PAGE_TABLE_LEVEL and there would be no adjustment done
3205 * here.
3206 */
Ard Biesheuvelbf4bea82014-11-10 08:33:56 +00003207 if (!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn) &&
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08003208 level == PT_PAGE_TABLE_LEVEL &&
Andrea Arcangeli127393f2016-05-05 16:22:20 -07003209 PageTransCompoundMap(pfn_to_page(pfn)) &&
Xiao Guangrong92f94f12016-02-24 17:51:06 +08003210 !mmu_gfn_lpage_is_disallowed(vcpu, gfn, PT_DIRECTORY_LEVEL)) {
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08003211 unsigned long mask;
3212 /*
3213 * mmu_notifier_retry was successful and we hold the
3214 * mmu_lock here, so the pmd can't become splitting
3215 * from under us, and in turn
3216 * __split_huge_page_refcount() can't run from under
3217 * us and we can safely transfer the refcount from
3218 * PG_tail to PG_head as we switch the pfn to tail to
3219 * head.
3220 */
3221 *levelp = level = PT_DIRECTORY_LEVEL;
3222 mask = KVM_PAGES_PER_HPAGE(level) - 1;
3223 VM_BUG_ON((gfn & mask) != (pfn & mask));
3224 if (pfn & mask) {
3225 gfn &= ~mask;
3226 *gfnp = gfn;
3227 kvm_release_pfn_clean(pfn);
3228 pfn &= ~mask;
Xiao Guangrongc3586662012-05-28 14:10:43 +08003229 kvm_get_pfn(pfn);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08003230 *pfnp = pfn;
3231 }
3232 }
3233}
3234
Xiao Guangrongd7c55202011-07-12 03:29:38 +08003235static bool handle_abnormal_pfn(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn,
Dan Williamsba049e92016-01-15 16:56:11 -08003236 kvm_pfn_t pfn, unsigned access, int *ret_val)
Xiao Guangrongd7c55202011-07-12 03:29:38 +08003237{
Xiao Guangrongd7c55202011-07-12 03:29:38 +08003238 /* The pfn is invalid, report the error! */
Xiao Guangrong81c52c52012-10-16 20:10:59 +08003239 if (unlikely(is_error_pfn(pfn))) {
Xiao Guangrongd7c55202011-07-12 03:29:38 +08003240 *ret_val = kvm_handle_bad_page(vcpu, gfn, pfn);
Paolo Bonzini798e88b2016-02-23 15:28:51 +01003241 return true;
Xiao Guangrongd7c55202011-07-12 03:29:38 +08003242 }
3243
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003244 if (unlikely(is_noslot_pfn(pfn)))
Xiao Guangrongd7c55202011-07-12 03:29:38 +08003245 vcpu_cache_mmio_info(vcpu, gva, gfn, access);
Xiao Guangrongd7c55202011-07-12 03:29:38 +08003246
Paolo Bonzini798e88b2016-02-23 15:28:51 +01003247 return false;
Xiao Guangrongd7c55202011-07-12 03:29:38 +08003248}
3249
Xiao Guangronge5552fd2013-07-30 21:01:59 +08003250static bool page_fault_can_be_fast(u32 error_code)
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003251{
3252 /*
Xiao Guangrong1c118b82013-07-18 12:52:37 +08003253 * Do not fix the mmio spte with invalid generation number which
3254 * need to be updated by slow page fault path.
3255 */
3256 if (unlikely(error_code & PFERR_RSVD_MASK))
3257 return false;
3258
Junaid Shahidf160c7b2016-12-06 16:46:16 -08003259 /* See if the page fault is due to an NX violation */
3260 if (unlikely(((error_code & (PFERR_FETCH_MASK | PFERR_PRESENT_MASK))
3261 == (PFERR_FETCH_MASK | PFERR_PRESENT_MASK))))
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003262 return false;
3263
Junaid Shahidf160c7b2016-12-06 16:46:16 -08003264 /*
3265 * #PF can be fast if:
3266 * 1. The shadow page table entry is not present, which could mean that
3267 * the fault is potentially caused by access tracking (if enabled).
3268 * 2. The shadow page table entry is present and the fault
3269 * is caused by write-protect, that means we just need change the W
3270 * bit of the spte which can be done out of mmu-lock.
3271 *
3272 * However, if access tracking is disabled we know that a non-present
3273 * page must be a genuine page fault where we have to create a new SPTE.
3274 * So, if access tracking is disabled, we return true only for write
3275 * accesses to a present page.
3276 */
3277
3278 return shadow_acc_track_mask != 0 ||
3279 ((error_code & (PFERR_WRITE_MASK | PFERR_PRESENT_MASK))
3280 == (PFERR_WRITE_MASK | PFERR_PRESENT_MASK));
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003281}
3282
Junaid Shahid97dceba2016-12-06 16:46:12 -08003283/*
3284 * Returns true if the SPTE was fixed successfully. Otherwise,
3285 * someone else modified the SPTE from its original value.
3286 */
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003287static bool
Xiao Guangrong92a476c2014-04-17 17:06:13 +08003288fast_pf_fix_direct_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003289 u64 *sptep, u64 old_spte, u64 new_spte)
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003290{
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003291 gfn_t gfn;
3292
3293 WARN_ON(!sp->role.direct);
3294
Kai Huang9b51a632015-01-28 10:54:25 +08003295 /*
3296 * Theoretically we could also set dirty bit (and flush TLB) here in
3297 * order to eliminate unnecessary PML logging. See comments in
3298 * set_spte. But fast_page_fault is very unlikely to happen with PML
3299 * enabled, so we do not do this. This might result in the same GPA
3300 * to be logged in PML buffer again when the write really happens, and
3301 * eventually to be called by mark_page_dirty twice. But it's also no
3302 * harm. This also avoids the TLB flush needed after setting dirty bit
3303 * so non-PML cases won't be impacted.
3304 *
3305 * Compare with set_spte where instead shadow_dirty_mask is set.
3306 */
Junaid Shahidf160c7b2016-12-06 16:46:16 -08003307 if (cmpxchg64(sptep, old_spte, new_spte) != old_spte)
Junaid Shahid97dceba2016-12-06 16:46:12 -08003308 return false;
3309
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003310 if (is_writable_pte(new_spte) && !is_writable_pte(old_spte)) {
Junaid Shahidf160c7b2016-12-06 16:46:16 -08003311 /*
3312 * The gfn of direct spte is stable since it is
3313 * calculated by sp->gfn.
3314 */
3315 gfn = kvm_mmu_page_get_gfn(sp, sptep - sp->spt);
3316 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3317 }
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003318
3319 return true;
3320}
3321
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003322static bool is_access_allowed(u32 fault_err_code, u64 spte)
3323{
3324 if (fault_err_code & PFERR_FETCH_MASK)
3325 return is_executable_pte(spte);
3326
3327 if (fault_err_code & PFERR_WRITE_MASK)
3328 return is_writable_pte(spte);
3329
3330 /* Fault was on Read access */
3331 return spte & PT_PRESENT_MASK;
3332}
3333
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003334/*
3335 * Return value:
3336 * - true: let the vcpu to access on the same address again.
3337 * - false: let the real page fault path to fix it.
3338 */
3339static bool fast_page_fault(struct kvm_vcpu *vcpu, gva_t gva, int level,
3340 u32 error_code)
3341{
3342 struct kvm_shadow_walk_iterator iterator;
Xiao Guangrong92a476c2014-04-17 17:06:13 +08003343 struct kvm_mmu_page *sp;
Junaid Shahid97dceba2016-12-06 16:46:12 -08003344 bool fault_handled = false;
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003345 u64 spte = 0ull;
Junaid Shahid97dceba2016-12-06 16:46:12 -08003346 uint retry_count = 0;
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003347
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003348 if (!VALID_PAGE(vcpu->arch.mmu->root_hpa))
Marcelo Tosatti37f6a4e2014-01-03 17:09:32 -02003349 return false;
3350
Xiao Guangronge5552fd2013-07-30 21:01:59 +08003351 if (!page_fault_can_be_fast(error_code))
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003352 return false;
3353
3354 walk_shadow_page_lockless_begin(vcpu);
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003355
Junaid Shahid97dceba2016-12-06 16:46:12 -08003356 do {
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003357 u64 new_spte;
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003358
Junaid Shahidd162f302016-12-21 20:29:30 -08003359 for_each_shadow_entry_lockless(vcpu, gva, iterator, spte)
3360 if (!is_shadow_present_pte(spte) ||
3361 iterator.level < level)
3362 break;
3363
Junaid Shahid97dceba2016-12-06 16:46:12 -08003364 sp = page_header(__pa(iterator.sptep));
3365 if (!is_last_spte(spte, sp->role.level))
3366 break;
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003367
Junaid Shahid97dceba2016-12-06 16:46:12 -08003368 /*
Junaid Shahidf160c7b2016-12-06 16:46:16 -08003369 * Check whether the memory access that caused the fault would
3370 * still cause it if it were to be performed right now. If not,
3371 * then this is a spurious fault caused by TLB lazily flushed,
3372 * or some other CPU has already fixed the PTE after the
3373 * current CPU took the fault.
Junaid Shahid97dceba2016-12-06 16:46:12 -08003374 *
3375 * Need not check the access of upper level table entries since
3376 * they are always ACC_ALL.
3377 */
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003378 if (is_access_allowed(error_code, spte)) {
3379 fault_handled = true;
3380 break;
Junaid Shahid97dceba2016-12-06 16:46:12 -08003381 }
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003382
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003383 new_spte = spte;
Junaid Shahidf160c7b2016-12-06 16:46:16 -08003384
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003385 if (is_access_track_spte(spte))
3386 new_spte = restore_acc_track_spte(new_spte);
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003387
Junaid Shahid97dceba2016-12-06 16:46:12 -08003388 /*
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003389 * Currently, to simplify the code, write-protection can
3390 * be removed in the fast path only if the SPTE was
3391 * write-protected for dirty-logging or access tracking.
Junaid Shahid97dceba2016-12-06 16:46:12 -08003392 */
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003393 if ((error_code & PFERR_WRITE_MASK) &&
3394 spte_can_locklessly_be_made_writable(spte))
3395 {
3396 new_spte |= PT_WRITABLE_MASK;
3397
3398 /*
3399 * Do not fix write-permission on the large spte. Since
3400 * we only dirty the first page into the dirty-bitmap in
3401 * fast_pf_fix_direct_spte(), other pages are missed
3402 * if its slot has dirty logging enabled.
3403 *
3404 * Instead, we let the slow page fault path create a
3405 * normal spte to fix the access.
3406 *
3407 * See the comments in kvm_arch_commit_memory_region().
3408 */
3409 if (sp->role.level > PT_PAGE_TABLE_LEVEL)
3410 break;
3411 }
3412
3413 /* Verify that the fault can be handled in the fast path */
3414 if (new_spte == spte ||
3415 !is_access_allowed(error_code, new_spte))
Junaid Shahid97dceba2016-12-06 16:46:12 -08003416 break;
Xiao Guangrongc126d942014-04-17 17:06:14 +08003417
Junaid Shahid97dceba2016-12-06 16:46:12 -08003418 /*
3419 * Currently, fast page fault only works for direct mapping
3420 * since the gfn is not stable for indirect shadow page. See
3421 * Documentation/virtual/kvm/locking.txt to get more detail.
3422 */
3423 fault_handled = fast_pf_fix_direct_spte(vcpu, sp,
Junaid Shahidf160c7b2016-12-06 16:46:16 -08003424 iterator.sptep, spte,
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003425 new_spte);
Junaid Shahid97dceba2016-12-06 16:46:12 -08003426 if (fault_handled)
3427 break;
3428
3429 if (++retry_count > 4) {
3430 printk_once(KERN_WARNING
3431 "kvm: Fast #PF retrying more than 4 times.\n");
3432 break;
3433 }
3434
Junaid Shahid97dceba2016-12-06 16:46:12 -08003435 } while (true);
3436
Xiao Guangronga72faf22012-06-20 15:59:41 +08003437 trace_fast_page_fault(vcpu, gva, error_code, iterator.sptep,
Junaid Shahid97dceba2016-12-06 16:46:12 -08003438 spte, fault_handled);
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003439 walk_shadow_page_lockless_end(vcpu);
3440
Junaid Shahid97dceba2016-12-06 16:46:12 -08003441 return fault_handled;
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003442}
3443
Xiao Guangrong78b2c542010-12-07 10:48:06 +08003444static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
Dan Williamsba049e92016-01-15 16:56:11 -08003445 gva_t gva, kvm_pfn_t *pfn, bool write, bool *writable);
Wanpeng Li26eeb532017-08-10 16:28:02 -07003446static int make_mmu_pages_available(struct kvm_vcpu *vcpu);
Xiao Guangrong060c2ab2010-11-12 14:49:11 +08003447
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003448static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, u32 error_code,
3449 gfn_t gfn, bool prefault)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003450{
3451 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02003452 int level;
Takuya Yoshikawafd136902015-10-16 17:06:02 +09003453 bool force_pt_level = false;
Dan Williamsba049e92016-01-15 16:56:11 -08003454 kvm_pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02003455 unsigned long mmu_seq;
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003456 bool map_writable, write = error_code & PFERR_WRITE_MASK;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003457
Takuya Yoshikawafd136902015-10-16 17:06:02 +09003458 level = mapping_level(vcpu, gfn, &force_pt_level);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08003459 if (likely(!force_pt_level)) {
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08003460 /*
3461 * This path builds a PAE pagetable - so we can map
3462 * 2mb pages at maximum. Therefore check if the level
3463 * is larger than that.
3464 */
3465 if (level > PT_DIRECTORY_LEVEL)
3466 level = PT_DIRECTORY_LEVEL;
Joerg Roedel852e3c12009-07-27 16:30:44 +02003467
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08003468 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Takuya Yoshikawafd136902015-10-16 17:06:02 +09003469 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -03003470
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003471 if (fast_page_fault(vcpu, v, level, error_code))
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02003472 return RET_PF_RETRY;
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003473
Andrea Arcangelie930bff2008-07-25 16:24:52 +02003474 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03003475 smp_rmb();
Xiao Guangrong060c2ab2010-11-12 14:49:11 +08003476
Xiao Guangrong78b2c542010-12-07 10:48:06 +08003477 if (try_async_pf(vcpu, prefault, gfn, v, &pfn, write, &map_writable))
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02003478 return RET_PF_RETRY;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003479
Xiao Guangrongd7c55202011-07-12 03:29:38 +08003480 if (handle_abnormal_pfn(vcpu, v, gfn, pfn, ACC_ALL, &r))
3481 return r;
Avi Kivityd196e342008-01-24 11:44:11 +02003482
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003483 spin_lock(&vcpu->kvm->mmu_lock);
Christoffer Dall8ca40a72012-10-14 23:10:18 -04003484 if (mmu_notifier_retry(vcpu->kvm, mmu_seq))
Andrea Arcangelie930bff2008-07-25 16:24:52 +02003485 goto out_unlock;
Wanpeng Li26eeb532017-08-10 16:28:02 -07003486 if (make_mmu_pages_available(vcpu) < 0)
3487 goto out_unlock;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08003488 if (likely(!force_pt_level))
3489 transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
Takuya Yoshikawa7ee0e5b2015-11-20 17:42:23 +09003490 r = __direct_map(vcpu, write, map_writable, level, gfn, pfn, prefault);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003491 spin_unlock(&vcpu->kvm->mmu_lock);
3492
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003493 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02003494
3495out_unlock:
3496 spin_unlock(&vcpu->kvm->mmu_lock);
3497 kvm_release_pfn_clean(pfn);
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02003498 return RET_PF_RETRY;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003499}
3500
Junaid Shahid74b566e2018-05-04 11:37:11 -07003501static void mmu_free_root_page(struct kvm *kvm, hpa_t *root_hpa,
3502 struct list_head *invalid_list)
3503{
3504 struct kvm_mmu_page *sp;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003505
Junaid Shahid74b566e2018-05-04 11:37:11 -07003506 if (!VALID_PAGE(*root_hpa))
3507 return;
3508
3509 sp = page_header(*root_hpa & PT64_BASE_ADDR_MASK);
3510 --sp->root_count;
3511 if (!sp->root_count && sp->role.invalid)
3512 kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
3513
3514 *root_hpa = INVALID_PAGE;
3515}
3516
Junaid Shahid08fb59d2018-06-27 14:59:17 -07003517/* roots_to_free must be some combination of the KVM_MMU_ROOT_* flags */
Vitaly Kuznetsov6a82cd12018-10-08 21:28:07 +02003518void kvm_mmu_free_roots(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
3519 ulong roots_to_free)
Avi Kivity17ac10a2007-01-05 16:36:40 -08003520{
3521 int i;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003522 LIST_HEAD(invalid_list);
Junaid Shahid08fb59d2018-06-27 14:59:17 -07003523 bool free_active_root = roots_to_free & KVM_MMU_ROOT_CURRENT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003524
Junaid Shahidb94742c2018-06-27 14:59:20 -07003525 BUILD_BUG_ON(KVM_MMU_NUM_PREV_ROOTS >= BITS_PER_LONG);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003526
Junaid Shahid08fb59d2018-06-27 14:59:17 -07003527 /* Before acquiring the MMU lock, see if we need to do any real work. */
Junaid Shahidb94742c2018-06-27 14:59:20 -07003528 if (!(free_active_root && VALID_PAGE(mmu->root_hpa))) {
3529 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
3530 if ((roots_to_free & KVM_MMU_ROOT_PREVIOUS(i)) &&
3531 VALID_PAGE(mmu->prev_roots[i].hpa))
3532 break;
3533
3534 if (i == KVM_MMU_NUM_PREV_ROOTS)
3535 return;
3536 }
Gleb Natapov35af5772013-05-16 11:55:51 +03003537
Gleb Natapov35af5772013-05-16 11:55:51 +03003538 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003539
Junaid Shahidb94742c2018-06-27 14:59:20 -07003540 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
3541 if (roots_to_free & KVM_MMU_ROOT_PREVIOUS(i))
3542 mmu_free_root_page(vcpu->kvm, &mmu->prev_roots[i].hpa,
3543 &invalid_list);
Junaid Shahid7c390d32018-06-27 14:59:06 -07003544
Junaid Shahid08fb59d2018-06-27 14:59:17 -07003545 if (free_active_root) {
3546 if (mmu->shadow_root_level >= PT64_ROOT_4LEVEL &&
3547 (mmu->root_level >= PT64_ROOT_4LEVEL || mmu->direct_map)) {
3548 mmu_free_root_page(vcpu->kvm, &mmu->root_hpa,
3549 &invalid_list);
3550 } else {
3551 for (i = 0; i < 4; ++i)
3552 if (mmu->pae_root[i] != 0)
3553 mmu_free_root_page(vcpu->kvm,
3554 &mmu->pae_root[i],
3555 &invalid_list);
3556 mmu->root_hpa = INVALID_PAGE;
3557 }
Vitaly Kuznetsovad7dc692019-02-22 17:45:01 +01003558 mmu->root_cr3 = 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003559 }
Junaid Shahid74b566e2018-05-04 11:37:11 -07003560
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003561 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05003562 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003563}
Junaid Shahid74b566e2018-05-04 11:37:11 -07003564EXPORT_SYMBOL_GPL(kvm_mmu_free_roots);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003565
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003566static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
3567{
3568 int ret = 0;
3569
3570 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03003571 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003572 ret = 1;
3573 }
3574
3575 return ret;
3576}
3577
Joerg Roedel651dd372010-09-10 17:30:59 +02003578static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu)
3579{
3580 struct kvm_mmu_page *sp;
Avi Kivity7ebaf152010-10-03 18:51:39 +02003581 unsigned i;
Joerg Roedel651dd372010-09-10 17:30:59 +02003582
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003583 if (vcpu->arch.mmu->shadow_root_level >= PT64_ROOT_4LEVEL) {
Joerg Roedel651dd372010-09-10 17:30:59 +02003584 spin_lock(&vcpu->kvm->mmu_lock);
Wanpeng Li26eeb532017-08-10 16:28:02 -07003585 if(make_mmu_pages_available(vcpu) < 0) {
3586 spin_unlock(&vcpu->kvm->mmu_lock);
Wanpeng Lied528702017-12-04 22:21:30 -08003587 return -ENOSPC;
Wanpeng Li26eeb532017-08-10 16:28:02 -07003588 }
Yu Zhang855feb62017-08-24 20:27:55 +08003589 sp = kvm_mmu_get_page(vcpu, 0, 0,
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003590 vcpu->arch.mmu->shadow_root_level, 1, ACC_ALL);
Joerg Roedel651dd372010-09-10 17:30:59 +02003591 ++sp->root_count;
3592 spin_unlock(&vcpu->kvm->mmu_lock);
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003593 vcpu->arch.mmu->root_hpa = __pa(sp->spt);
3594 } else if (vcpu->arch.mmu->shadow_root_level == PT32E_ROOT_LEVEL) {
Joerg Roedel651dd372010-09-10 17:30:59 +02003595 for (i = 0; i < 4; ++i) {
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003596 hpa_t root = vcpu->arch.mmu->pae_root[i];
Joerg Roedel651dd372010-09-10 17:30:59 +02003597
Paolo Bonzinifa4a2c02013-10-02 16:56:16 +02003598 MMU_WARN_ON(VALID_PAGE(root));
Joerg Roedel651dd372010-09-10 17:30:59 +02003599 spin_lock(&vcpu->kvm->mmu_lock);
Wanpeng Li26eeb532017-08-10 16:28:02 -07003600 if (make_mmu_pages_available(vcpu) < 0) {
3601 spin_unlock(&vcpu->kvm->mmu_lock);
Wanpeng Lied528702017-12-04 22:21:30 -08003602 return -ENOSPC;
Wanpeng Li26eeb532017-08-10 16:28:02 -07003603 }
Avi Kivity649497d2010-12-28 12:09:07 +02003604 sp = kvm_mmu_get_page(vcpu, i << (30 - PAGE_SHIFT),
Takuya Yoshikawabb11c6c2015-11-26 21:16:35 +09003605 i << 30, PT32_ROOT_LEVEL, 1, ACC_ALL);
Joerg Roedel651dd372010-09-10 17:30:59 +02003606 root = __pa(sp->spt);
3607 ++sp->root_count;
3608 spin_unlock(&vcpu->kvm->mmu_lock);
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003609 vcpu->arch.mmu->pae_root[i] = root | PT_PRESENT_MASK;
Joerg Roedel651dd372010-09-10 17:30:59 +02003610 }
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003611 vcpu->arch.mmu->root_hpa = __pa(vcpu->arch.mmu->pae_root);
Joerg Roedel651dd372010-09-10 17:30:59 +02003612 } else
3613 BUG();
Vitaly Kuznetsovad7dc692019-02-22 17:45:01 +01003614 vcpu->arch.mmu->root_cr3 = vcpu->arch.mmu->get_cr3(vcpu);
Joerg Roedel651dd372010-09-10 17:30:59 +02003615
3616 return 0;
3617}
3618
3619static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08003620{
Avi Kivity4db35312007-11-21 15:28:32 +02003621 struct kvm_mmu_page *sp;
Joerg Roedel81407ca2010-09-10 17:31:00 +02003622 u64 pdptr, pm_mask;
Vitaly Kuznetsovad7dc692019-02-22 17:45:01 +01003623 gfn_t root_gfn, root_cr3;
Joerg Roedel81407ca2010-09-10 17:31:00 +02003624 int i;
Avi Kivity3bb65a22007-01-05 16:36:51 -08003625
Vitaly Kuznetsovad7dc692019-02-22 17:45:01 +01003626 root_cr3 = vcpu->arch.mmu->get_cr3(vcpu);
3627 root_gfn = root_cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003628
Joerg Roedel651dd372010-09-10 17:30:59 +02003629 if (mmu_check_root(vcpu, root_gfn))
3630 return 1;
3631
3632 /*
3633 * Do we shadow a long mode page table? If so we need to
3634 * write-protect the guests page table root.
3635 */
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003636 if (vcpu->arch.mmu->root_level >= PT64_ROOT_4LEVEL) {
3637 hpa_t root = vcpu->arch.mmu->root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003638
Paolo Bonzinifa4a2c02013-10-02 16:56:16 +02003639 MMU_WARN_ON(VALID_PAGE(root));
Joerg Roedel651dd372010-09-10 17:30:59 +02003640
Avi Kivity8facbbf2010-05-04 12:58:32 +03003641 spin_lock(&vcpu->kvm->mmu_lock);
Wanpeng Li26eeb532017-08-10 16:28:02 -07003642 if (make_mmu_pages_available(vcpu) < 0) {
3643 spin_unlock(&vcpu->kvm->mmu_lock);
Wanpeng Lied528702017-12-04 22:21:30 -08003644 return -ENOSPC;
Wanpeng Li26eeb532017-08-10 16:28:02 -07003645 }
Yu Zhang855feb62017-08-24 20:27:55 +08003646 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003647 vcpu->arch.mmu->shadow_root_level, 0, ACC_ALL);
Avi Kivity4db35312007-11-21 15:28:32 +02003648 root = __pa(sp->spt);
3649 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03003650 spin_unlock(&vcpu->kvm->mmu_lock);
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003651 vcpu->arch.mmu->root_hpa = root;
Vitaly Kuznetsovad7dc692019-02-22 17:45:01 +01003652 goto set_root_cr3;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003653 }
Joerg Roedelf87f9282010-09-02 17:29:45 +02003654
Joerg Roedel651dd372010-09-10 17:30:59 +02003655 /*
3656 * We shadow a 32 bit page table. This may be a legacy 2-level
Joerg Roedel81407ca2010-09-10 17:31:00 +02003657 * or a PAE 3-level page table. In either case we need to be aware that
3658 * the shadow page table may be a PAE or a long mode page table.
Joerg Roedel651dd372010-09-10 17:30:59 +02003659 */
Joerg Roedel81407ca2010-09-10 17:31:00 +02003660 pm_mask = PT_PRESENT_MASK;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003661 if (vcpu->arch.mmu->shadow_root_level == PT64_ROOT_4LEVEL)
Joerg Roedel81407ca2010-09-10 17:31:00 +02003662 pm_mask |= PT_ACCESSED_MASK | PT_WRITABLE_MASK | PT_USER_MASK;
3663
Avi Kivity17ac10a2007-01-05 16:36:40 -08003664 for (i = 0; i < 4; ++i) {
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003665 hpa_t root = vcpu->arch.mmu->pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08003666
Paolo Bonzinifa4a2c02013-10-02 16:56:16 +02003667 MMU_WARN_ON(VALID_PAGE(root));
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003668 if (vcpu->arch.mmu->root_level == PT32E_ROOT_LEVEL) {
3669 pdptr = vcpu->arch.mmu->get_pdptr(vcpu, i);
Bandan Das812f30b2016-07-12 18:18:50 -04003670 if (!(pdptr & PT_PRESENT_MASK)) {
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003671 vcpu->arch.mmu->pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03003672 continue;
3673 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003674 root_gfn = pdptr >> PAGE_SHIFT;
Joerg Roedelf87f9282010-09-02 17:29:45 +02003675 if (mmu_check_root(vcpu, root_gfn))
3676 return 1;
Eric Northup5a7388c2010-04-26 17:00:05 -07003677 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03003678 spin_lock(&vcpu->kvm->mmu_lock);
Wanpeng Li26eeb532017-08-10 16:28:02 -07003679 if (make_mmu_pages_available(vcpu) < 0) {
3680 spin_unlock(&vcpu->kvm->mmu_lock);
Wanpeng Lied528702017-12-04 22:21:30 -08003681 return -ENOSPC;
Wanpeng Li26eeb532017-08-10 16:28:02 -07003682 }
Takuya Yoshikawabb11c6c2015-11-26 21:16:35 +09003683 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30, PT32_ROOT_LEVEL,
3684 0, ACC_ALL);
Avi Kivity4db35312007-11-21 15:28:32 +02003685 root = __pa(sp->spt);
3686 ++sp->root_count;
Avi Kivity8facbbf2010-05-04 12:58:32 +03003687 spin_unlock(&vcpu->kvm->mmu_lock);
3688
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003689 vcpu->arch.mmu->pae_root[i] = root | pm_mask;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003690 }
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003691 vcpu->arch.mmu->root_hpa = __pa(vcpu->arch.mmu->pae_root);
Joerg Roedel81407ca2010-09-10 17:31:00 +02003692
3693 /*
3694 * If we shadow a 32 bit page table with a long mode page
3695 * table we enter this path.
3696 */
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003697 if (vcpu->arch.mmu->shadow_root_level == PT64_ROOT_4LEVEL) {
3698 if (vcpu->arch.mmu->lm_root == NULL) {
Joerg Roedel81407ca2010-09-10 17:31:00 +02003699 /*
3700 * The additional page necessary for this is only
3701 * allocated on demand.
3702 */
3703
3704 u64 *lm_root;
3705
3706 lm_root = (void*)get_zeroed_page(GFP_KERNEL);
3707 if (lm_root == NULL)
3708 return 1;
3709
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003710 lm_root[0] = __pa(vcpu->arch.mmu->pae_root) | pm_mask;
Joerg Roedel81407ca2010-09-10 17:31:00 +02003711
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003712 vcpu->arch.mmu->lm_root = lm_root;
Joerg Roedel81407ca2010-09-10 17:31:00 +02003713 }
3714
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003715 vcpu->arch.mmu->root_hpa = __pa(vcpu->arch.mmu->lm_root);
Joerg Roedel81407ca2010-09-10 17:31:00 +02003716 }
3717
Vitaly Kuznetsovad7dc692019-02-22 17:45:01 +01003718set_root_cr3:
3719 vcpu->arch.mmu->root_cr3 = root_cr3;
3720
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003721 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003722}
3723
Joerg Roedel651dd372010-09-10 17:30:59 +02003724static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
3725{
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003726 if (vcpu->arch.mmu->direct_map)
Joerg Roedel651dd372010-09-10 17:30:59 +02003727 return mmu_alloc_direct_roots(vcpu);
3728 else
3729 return mmu_alloc_shadow_roots(vcpu);
3730}
3731
Junaid Shahid578e1c42018-06-27 14:59:05 -07003732void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003733{
3734 int i;
3735 struct kvm_mmu_page *sp;
3736
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003737 if (vcpu->arch.mmu->direct_map)
Joerg Roedel81407ca2010-09-10 17:31:00 +02003738 return;
3739
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003740 if (!VALID_PAGE(vcpu->arch.mmu->root_hpa))
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003741 return;
Xiao Guangrong69030742010-09-27 18:09:29 +08003742
David Matlack56f17dd2014-08-18 15:46:07 -07003743 vcpu_clear_mmio_info(vcpu, MMIO_GVA_ANY);
Junaid Shahid578e1c42018-06-27 14:59:05 -07003744
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003745 if (vcpu->arch.mmu->root_level >= PT64_ROOT_4LEVEL) {
3746 hpa_t root = vcpu->arch.mmu->root_hpa;
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003747 sp = page_header(root);
Junaid Shahid578e1c42018-06-27 14:59:05 -07003748
3749 /*
3750 * Even if another CPU was marking the SP as unsync-ed
3751 * simultaneously, any guest page table changes are not
3752 * guaranteed to be visible anyway until this VCPU issues a TLB
3753 * flush strictly after those changes are made. We only need to
3754 * ensure that the other CPU sets these flags before any actual
3755 * changes to the page tables are made. The comments in
3756 * mmu_need_write_protect() describe what could go wrong if this
3757 * requirement isn't satisfied.
3758 */
3759 if (!smp_load_acquire(&sp->unsync) &&
3760 !smp_load_acquire(&sp->unsync_children))
3761 return;
3762
3763 spin_lock(&vcpu->kvm->mmu_lock);
3764 kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC);
3765
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003766 mmu_sync_children(vcpu, sp);
Junaid Shahid578e1c42018-06-27 14:59:05 -07003767
Xiao Guangrong0375f7f2011-11-28 20:41:00 +08003768 kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
Junaid Shahid578e1c42018-06-27 14:59:05 -07003769 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003770 return;
3771 }
Junaid Shahid578e1c42018-06-27 14:59:05 -07003772
3773 spin_lock(&vcpu->kvm->mmu_lock);
3774 kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC);
3775
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003776 for (i = 0; i < 4; ++i) {
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003777 hpa_t root = vcpu->arch.mmu->pae_root[i];
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003778
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003779 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003780 root &= PT64_BASE_ADDR_MASK;
3781 sp = page_header(root);
3782 mmu_sync_children(vcpu, sp);
3783 }
3784 }
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003785
Junaid Shahid578e1c42018-06-27 14:59:05 -07003786 kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003787 spin_unlock(&vcpu->kvm->mmu_lock);
3788}
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03003789EXPORT_SYMBOL_GPL(kvm_mmu_sync_roots);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003790
Gleb Natapov1871c602010-02-10 14:21:32 +02003791static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
Avi Kivityab9ae312010-11-22 17:53:26 +02003792 u32 access, struct x86_exception *exception)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003793{
Avi Kivityab9ae312010-11-22 17:53:26 +02003794 if (exception)
3795 exception->error_code = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003796 return vaddr;
3797}
3798
Joerg Roedel6539e732010-09-10 17:30:50 +02003799static gpa_t nonpaging_gva_to_gpa_nested(struct kvm_vcpu *vcpu, gva_t vaddr,
Avi Kivityab9ae312010-11-22 17:53:26 +02003800 u32 access,
3801 struct x86_exception *exception)
Joerg Roedel6539e732010-09-10 17:30:50 +02003802{
Avi Kivityab9ae312010-11-22 17:53:26 +02003803 if (exception)
3804 exception->error_code = 0;
Paolo Bonzini54987b72014-09-02 13:23:06 +02003805 return vcpu->arch.nested_mmu.translate_gpa(vcpu, vaddr, access, exception);
Joerg Roedel6539e732010-09-10 17:30:50 +02003806}
3807
Xiao Guangrongd625b152015-08-05 12:04:25 +08003808static bool
3809__is_rsvd_bits_set(struct rsvd_bits_validate *rsvd_check, u64 pte, int level)
3810{
3811 int bit7 = (pte >> 7) & 1, low6 = pte & 0x3f;
3812
3813 return (pte & rsvd_check->rsvd_bits_mask[bit7][level-1]) |
3814 ((rsvd_check->bad_mt_xwr & (1ull << low6)) != 0);
3815}
3816
3817static bool is_rsvd_bits_set(struct kvm_mmu *mmu, u64 gpte, int level)
3818{
3819 return __is_rsvd_bits_set(&mmu->guest_rsvd_check, gpte, level);
3820}
3821
3822static bool is_shadow_zero_bits_set(struct kvm_mmu *mmu, u64 spte, int level)
3823{
3824 return __is_rsvd_bits_set(&mmu->shadow_zero_check, spte, level);
3825}
3826
Takuya Yoshikawaded58742016-02-22 17:23:40 +09003827static bool mmio_info_in_cache(struct kvm_vcpu *vcpu, u64 addr, bool direct)
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003828{
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02003829 /*
3830 * A nested guest cannot use the MMIO cache if it is using nested
3831 * page tables, because cr2 is a nGPA while the cache stores GPAs.
3832 */
3833 if (mmu_is_nested(vcpu))
3834 return false;
3835
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003836 if (direct)
3837 return vcpu_match_mmio_gpa(vcpu, addr);
3838
3839 return vcpu_match_mmio_gva(vcpu, addr);
3840}
3841
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003842/* return true if reserved bit is detected on spte. */
3843static bool
3844walk_shadow_page_get_mmio_spte(struct kvm_vcpu *vcpu, u64 addr, u64 *sptep)
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003845{
3846 struct kvm_shadow_walk_iterator iterator;
Yu Zhang2a7266a2017-08-24 20:27:54 +08003847 u64 sptes[PT64_ROOT_MAX_LEVEL], spte = 0ull;
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003848 int root, leaf;
3849 bool reserved = false;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003850
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003851 if (!VALID_PAGE(vcpu->arch.mmu->root_hpa))
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003852 goto exit;
Marcelo Tosatti37f6a4e2014-01-03 17:09:32 -02003853
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003854 walk_shadow_page_lockless_begin(vcpu);
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003855
Paolo Bonzini29ecd662015-09-06 16:24:50 +02003856 for (shadow_walk_init(&iterator, vcpu, addr),
3857 leaf = root = iterator.level;
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003858 shadow_walk_okay(&iterator);
3859 __shadow_walk_next(&iterator, spte)) {
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003860 spte = mmu_spte_get_lockless(iterator.sptep);
3861
3862 sptes[leaf - 1] = spte;
Paolo Bonzini29ecd662015-09-06 16:24:50 +02003863 leaf--;
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003864
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003865 if (!is_shadow_present_pte(spte))
3866 break;
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003867
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003868 reserved |= is_shadow_zero_bits_set(vcpu->arch.mmu, spte,
Paolo Bonzini58c95072015-09-22 10:15:59 +02003869 iterator.level);
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003870 }
3871
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003872 walk_shadow_page_lockless_end(vcpu);
3873
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003874 if (reserved) {
3875 pr_err("%s: detect reserved bits on spte, addr 0x%llx, dump hierarchy:\n",
3876 __func__, addr);
Paolo Bonzini29ecd662015-09-06 16:24:50 +02003877 while (root > leaf) {
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003878 pr_err("------ spte 0x%llx level %d.\n",
3879 sptes[root - 1], root);
3880 root--;
3881 }
3882 }
3883exit:
3884 *sptep = spte;
3885 return reserved;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003886}
3887
Paolo Bonzinie08d26f2017-08-17 18:36:56 +02003888static int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr, bool direct)
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003889{
3890 u64 spte;
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003891 bool reserved;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003892
Takuya Yoshikawaded58742016-02-22 17:23:40 +09003893 if (mmio_info_in_cache(vcpu, addr, direct))
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02003894 return RET_PF_EMULATE;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003895
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003896 reserved = walk_shadow_page_get_mmio_spte(vcpu, addr, &spte);
Paolo Bonzini450869d2015-11-04 13:41:21 +01003897 if (WARN_ON(reserved))
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02003898 return -EINVAL;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003899
3900 if (is_mmio_spte(spte)) {
3901 gfn_t gfn = get_mmio_spte_gfn(spte);
3902 unsigned access = get_mmio_spte_access(spte);
3903
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003904 if (!check_mmio_spte(vcpu, spte))
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02003905 return RET_PF_INVALID;
Xiao Guangrongf8f55942013-06-07 16:51:26 +08003906
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003907 if (direct)
3908 addr = 0;
Xiao Guangrong4f022642011-07-12 03:34:24 +08003909
3910 trace_handle_mmio_page_fault(addr, gfn, access);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003911 vcpu_cache_mmio_info(vcpu, addr, gfn, access);
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02003912 return RET_PF_EMULATE;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003913 }
3914
3915 /*
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003916 * If the page table is zapped by other cpus, let CPU fault again on
3917 * the address.
3918 */
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02003919 return RET_PF_RETRY;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003920}
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003921
Xiao Guangrong3d0c27a2016-02-24 17:51:11 +08003922static bool page_fault_handle_page_track(struct kvm_vcpu *vcpu,
3923 u32 error_code, gfn_t gfn)
3924{
3925 if (unlikely(error_code & PFERR_RSVD_MASK))
3926 return false;
3927
3928 if (!(error_code & PFERR_PRESENT_MASK) ||
3929 !(error_code & PFERR_WRITE_MASK))
3930 return false;
3931
3932 /*
3933 * guest is writing the page which is write tracked which can
3934 * not be fixed by page fault handler.
3935 */
3936 if (kvm_page_track_is_active(vcpu, gfn, KVM_PAGE_TRACK_WRITE))
3937 return true;
3938
3939 return false;
3940}
3941
Xiao Guangronge5691a82016-02-24 17:51:12 +08003942static void shadow_page_table_clear_flood(struct kvm_vcpu *vcpu, gva_t addr)
3943{
3944 struct kvm_shadow_walk_iterator iterator;
3945 u64 spte;
3946
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003947 if (!VALID_PAGE(vcpu->arch.mmu->root_hpa))
Xiao Guangronge5691a82016-02-24 17:51:12 +08003948 return;
3949
3950 walk_shadow_page_lockless_begin(vcpu);
3951 for_each_shadow_entry_lockless(vcpu, addr, iterator, spte) {
3952 clear_sp_write_flooding_count(iterator.sptep);
3953 if (!is_shadow_present_pte(spte))
3954 break;
3955 }
3956 walk_shadow_page_lockless_end(vcpu);
3957}
3958
Avi Kivity6aa8b732006-12-10 02:21:36 -08003959static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08003960 u32 error_code, bool prefault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003961{
Xiao Guangrong3d0c27a2016-02-24 17:51:11 +08003962 gfn_t gfn = gva >> PAGE_SHIFT;
Avi Kivitye2dec932007-01-05 16:36:54 -08003963 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003964
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003965 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003966
Xiao Guangrong3d0c27a2016-02-24 17:51:11 +08003967 if (page_fault_handle_page_track(vcpu, error_code, gfn))
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02003968 return RET_PF_EMULATE;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003969
Avi Kivitye2dec932007-01-05 16:36:54 -08003970 r = mmu_topup_memory_caches(vcpu);
3971 if (r)
3972 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08003973
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003974 MMU_WARN_ON(!VALID_PAGE(vcpu->arch.mmu->root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003975
Avi Kivity6aa8b732006-12-10 02:21:36 -08003976
Avi Kivitye8332402007-12-09 18:43:00 +02003977 return nonpaging_map(vcpu, gva & PAGE_MASK,
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003978 error_code, gfn, prefault);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003979}
3980
Jan Kiszka7e1fbea2010-10-20 15:18:02 +02003981static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn)
Gleb Natapovaf585b92010-10-14 11:22:46 +02003982{
3983 struct kvm_arch_async_pf arch;
Xiao Guangrongfb67e142010-12-07 10:35:25 +08003984
Gleb Natapov7c907052010-10-14 11:22:53 +02003985 arch.token = (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id;
Gleb Natapovaf585b92010-10-14 11:22:46 +02003986 arch.gfn = gfn;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003987 arch.direct_map = vcpu->arch.mmu->direct_map;
3988 arch.cr3 = vcpu->arch.mmu->get_cr3(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02003989
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003990 return kvm_setup_async_pf(vcpu, gva, kvm_vcpu_gfn_to_hva(vcpu, gfn), &arch);
Gleb Natapovaf585b92010-10-14 11:22:46 +02003991}
3992
Wanpeng Li9bc1f092017-06-08 20:13:40 -07003993bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu)
Gleb Natapovaf585b92010-10-14 11:22:46 +02003994{
Paolo Bonzini35754c92015-07-29 12:05:37 +02003995 if (unlikely(!lapic_in_kernel(vcpu) ||
Haozhong Zhang2a266f22018-01-10 21:44:42 +08003996 kvm_event_needs_reinjection(vcpu) ||
3997 vcpu->arch.exception.pending))
Gleb Natapovaf585b92010-10-14 11:22:46 +02003998 return false;
3999
Wanpeng Li52a5c152017-07-13 18:30:42 -07004000 if (!vcpu->arch.apf.delivery_as_pf_vmexit && is_guest_mode(vcpu))
Wanpeng Li9bc1f092017-06-08 20:13:40 -07004001 return false;
4002
Gleb Natapovaf585b92010-10-14 11:22:46 +02004003 return kvm_x86_ops->interrupt_allowed(vcpu);
4004}
4005
Xiao Guangrong78b2c542010-12-07 10:48:06 +08004006static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
Dan Williamsba049e92016-01-15 16:56:11 -08004007 gva_t gva, kvm_pfn_t *pfn, bool write, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02004008{
Paolo Bonzini35204692015-04-02 11:20:48 +02004009 struct kvm_memory_slot *slot;
Gleb Natapovaf585b92010-10-14 11:22:46 +02004010 bool async;
4011
Jim Mattson3a2936d2018-05-09 17:02:05 -04004012 /*
4013 * Don't expose private memslots to L2.
4014 */
4015 if (is_guest_mode(vcpu) && !kvm_is_visible_gfn(vcpu->kvm, gfn)) {
4016 *pfn = KVM_PFN_NOSLOT;
4017 return false;
4018 }
4019
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004020 slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
Paolo Bonzini35204692015-04-02 11:20:48 +02004021 async = false;
4022 *pfn = __gfn_to_pfn_memslot(slot, gfn, false, &async, write, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02004023 if (!async)
4024 return false; /* *pfn has correct page already */
4025
Wanpeng Li9bc1f092017-06-08 20:13:40 -07004026 if (!prefault && kvm_can_do_async_pf(vcpu)) {
Xiao Guangrongc9b263d2010-11-01 16:58:43 +08004027 trace_kvm_try_async_get_page(gva, gfn);
Gleb Natapovaf585b92010-10-14 11:22:46 +02004028 if (kvm_find_async_pf_gfn(vcpu, gfn)) {
4029 trace_kvm_async_pf_doublefault(gva, gfn);
4030 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
4031 return true;
4032 } else if (kvm_arch_setup_async_pf(vcpu, gva, gfn))
4033 return true;
4034 }
4035
Paolo Bonzini35204692015-04-02 11:20:48 +02004036 *pfn = __gfn_to_pfn_memslot(slot, gfn, false, NULL, write, writable);
Gleb Natapovaf585b92010-10-14 11:22:46 +02004037 return false;
4038}
4039
Wanpeng Li1261bfa2017-07-13 18:30:40 -07004040int kvm_handle_page_fault(struct kvm_vcpu *vcpu, u64 error_code,
Paolo Bonzinid0006532017-08-11 18:36:43 +02004041 u64 fault_address, char *insn, int insn_len)
Wanpeng Li1261bfa2017-07-13 18:30:40 -07004042{
4043 int r = 1;
4044
Paolo Bonzinic595cee2018-07-02 13:07:14 +02004045 vcpu->arch.l1tf_flush_l1d = true;
Wanpeng Li1261bfa2017-07-13 18:30:40 -07004046 switch (vcpu->arch.apf.host_apf_reason) {
4047 default:
4048 trace_kvm_page_fault(fault_address, error_code);
4049
Paolo Bonzinid0006532017-08-11 18:36:43 +02004050 if (kvm_event_needs_reinjection(vcpu))
Wanpeng Li1261bfa2017-07-13 18:30:40 -07004051 kvm_mmu_unprotect_page_virt(vcpu, fault_address);
4052 r = kvm_mmu_page_fault(vcpu, fault_address, error_code, insn,
4053 insn_len);
4054 break;
4055 case KVM_PV_REASON_PAGE_NOT_PRESENT:
4056 vcpu->arch.apf.host_apf_reason = 0;
4057 local_irq_disable();
Boqun Fenga2b78612017-10-03 21:36:51 +08004058 kvm_async_pf_task_wait(fault_address, 0);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07004059 local_irq_enable();
4060 break;
4061 case KVM_PV_REASON_PAGE_READY:
4062 vcpu->arch.apf.host_apf_reason = 0;
4063 local_irq_disable();
4064 kvm_async_pf_task_wake(fault_address);
4065 local_irq_enable();
4066 break;
4067 }
4068 return r;
4069}
4070EXPORT_SYMBOL_GPL(kvm_handle_page_fault);
4071
Xiao Guangrong6a39bbc2015-06-15 16:55:35 +08004072static bool
4073check_hugepage_cache_consistency(struct kvm_vcpu *vcpu, gfn_t gfn, int level)
4074{
4075 int page_num = KVM_PAGES_PER_HPAGE(level);
4076
4077 gfn &= ~(page_num - 1);
4078
4079 return kvm_mtrr_check_gfn_range_consistency(vcpu, gfn, page_num);
4080}
4081
Gleb Natapov56028d02010-10-17 18:13:42 +02004082static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, u32 error_code,
Xiao Guangrong78b2c542010-12-07 10:48:06 +08004083 bool prefault)
Joerg Roedelfb72d162008-02-07 13:47:44 +01004084{
Dan Williamsba049e92016-01-15 16:56:11 -08004085 kvm_pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01004086 int r;
Joerg Roedel852e3c12009-07-27 16:30:44 +02004087 int level;
Takuya Yoshikawacd1872f2015-10-16 17:04:13 +09004088 bool force_pt_level;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03004089 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02004090 unsigned long mmu_seq;
Marcelo Tosatti612819c2010-10-22 14:18:18 -02004091 int write = error_code & PFERR_WRITE_MASK;
4092 bool map_writable;
Joerg Roedelfb72d162008-02-07 13:47:44 +01004093
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02004094 MMU_WARN_ON(!VALID_PAGE(vcpu->arch.mmu->root_hpa));
Joerg Roedelfb72d162008-02-07 13:47:44 +01004095
Xiao Guangrong3d0c27a2016-02-24 17:51:11 +08004096 if (page_fault_handle_page_track(vcpu, error_code, gfn))
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02004097 return RET_PF_EMULATE;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004098
Joerg Roedelfb72d162008-02-07 13:47:44 +01004099 r = mmu_topup_memory_caches(vcpu);
4100 if (r)
4101 return r;
4102
Takuya Yoshikawafd136902015-10-16 17:06:02 +09004103 force_pt_level = !check_hugepage_cache_consistency(vcpu, gfn,
4104 PT_DIRECTORY_LEVEL);
4105 level = mapping_level(vcpu, gfn, &force_pt_level);
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08004106 if (likely(!force_pt_level)) {
Xiao Guangrong6a39bbc2015-06-15 16:55:35 +08004107 if (level > PT_DIRECTORY_LEVEL &&
4108 !check_hugepage_cache_consistency(vcpu, gfn, level))
4109 level = PT_DIRECTORY_LEVEL;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08004110 gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
Takuya Yoshikawafd136902015-10-16 17:06:02 +09004111 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02004112
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08004113 if (fast_page_fault(vcpu, gpa, level, error_code))
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02004114 return RET_PF_RETRY;
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08004115
Andrea Arcangelie930bff2008-07-25 16:24:52 +02004116 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03004117 smp_rmb();
Gleb Natapovaf585b92010-10-14 11:22:46 +02004118
Xiao Guangrong78b2c542010-12-07 10:48:06 +08004119 if (try_async_pf(vcpu, prefault, gfn, gpa, &pfn, write, &map_writable))
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02004120 return RET_PF_RETRY;
Gleb Natapovaf585b92010-10-14 11:22:46 +02004121
Xiao Guangrongd7c55202011-07-12 03:29:38 +08004122 if (handle_abnormal_pfn(vcpu, 0, gfn, pfn, ACC_ALL, &r))
4123 return r;
4124
Joerg Roedelfb72d162008-02-07 13:47:44 +01004125 spin_lock(&vcpu->kvm->mmu_lock);
Christoffer Dall8ca40a72012-10-14 23:10:18 -04004126 if (mmu_notifier_retry(vcpu->kvm, mmu_seq))
Andrea Arcangelie930bff2008-07-25 16:24:52 +02004127 goto out_unlock;
Wanpeng Li26eeb532017-08-10 16:28:02 -07004128 if (make_mmu_pages_available(vcpu) < 0)
4129 goto out_unlock;
Andrea Arcangeli936a5fe2011-01-13 15:46:48 -08004130 if (likely(!force_pt_level))
4131 transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
Takuya Yoshikawa7ee0e5b2015-11-20 17:42:23 +09004132 r = __direct_map(vcpu, write, map_writable, level, gfn, pfn, prefault);
Joerg Roedelfb72d162008-02-07 13:47:44 +01004133 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01004134
4135 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02004136
4137out_unlock:
4138 spin_unlock(&vcpu->kvm->mmu_lock);
4139 kvm_release_pfn_clean(pfn);
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02004140 return RET_PF_RETRY;
Joerg Roedelfb72d162008-02-07 13:47:44 +01004141}
4142
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004143static void nonpaging_init_context(struct kvm_vcpu *vcpu,
4144 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004145{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004146 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004147 context->gva_to_gpa = nonpaging_gva_to_gpa;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03004148 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03004149 context->invlpg = nonpaging_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08004150 context->update_pte = nonpaging_update_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08004151 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004152 context->shadow_root_level = PT32E_ROOT_LEVEL;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02004153 context->direct_map = true;
Joerg Roedel2d48a982010-09-10 17:31:01 +02004154 context->nx = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004155}
4156
Junaid Shahidb94742c2018-06-27 14:59:20 -07004157/*
4158 * Find out if a previously cached root matching the new CR3/role is available.
4159 * The current root is also inserted into the cache.
4160 * If a matching root was found, it is assigned to kvm_mmu->root_hpa and true is
4161 * returned.
4162 * Otherwise, the LRU root from the cache is assigned to kvm_mmu->root_hpa and
4163 * false is returned. This root should now be freed by the caller.
4164 */
4165static bool cached_root_available(struct kvm_vcpu *vcpu, gpa_t new_cr3,
4166 union kvm_mmu_page_role new_role)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004167{
Junaid Shahidb94742c2018-06-27 14:59:20 -07004168 uint i;
4169 struct kvm_mmu_root_info root;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02004170 struct kvm_mmu *mmu = vcpu->arch.mmu;
Junaid Shahidb94742c2018-06-27 14:59:20 -07004171
Vitaly Kuznetsovad7dc692019-02-22 17:45:01 +01004172 root.cr3 = mmu->root_cr3;
Junaid Shahidb94742c2018-06-27 14:59:20 -07004173 root.hpa = mmu->root_hpa;
4174
4175 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
4176 swap(root, mmu->prev_roots[i]);
4177
4178 if (new_cr3 == root.cr3 && VALID_PAGE(root.hpa) &&
4179 page_header(root.hpa) != NULL &&
4180 new_role.word == page_header(root.hpa)->role.word)
4181 break;
4182 }
4183
4184 mmu->root_hpa = root.hpa;
Vitaly Kuznetsovad7dc692019-02-22 17:45:01 +01004185 mmu->root_cr3 = root.cr3;
Junaid Shahidb94742c2018-06-27 14:59:20 -07004186
4187 return i < KVM_MMU_NUM_PREV_ROOTS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004188}
4189
Junaid Shahid0aab33e2018-06-27 14:59:09 -07004190static bool fast_cr3_switch(struct kvm_vcpu *vcpu, gpa_t new_cr3,
Junaid Shahidade61e22018-06-27 14:59:15 -07004191 union kvm_mmu_page_role new_role,
4192 bool skip_tlb_flush)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004193{
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02004194 struct kvm_mmu *mmu = vcpu->arch.mmu;
Junaid Shahid7c390d32018-06-27 14:59:06 -07004195
4196 /*
4197 * For now, limit the fast switch to 64-bit hosts+VMs in order to avoid
4198 * having to deal with PDPTEs. We may add support for 32-bit hosts/VMs
4199 * later if necessary.
4200 */
4201 if (mmu->shadow_root_level >= PT64_ROOT_4LEVEL &&
4202 mmu->root_level >= PT64_ROOT_4LEVEL) {
Junaid Shahid7c390d32018-06-27 14:59:06 -07004203 if (mmu_check_root(vcpu, new_cr3 >> PAGE_SHIFT))
4204 return false;
4205
Junaid Shahidb94742c2018-06-27 14:59:20 -07004206 if (cached_root_available(vcpu, new_cr3, new_role)) {
Junaid Shahid7c390d32018-06-27 14:59:06 -07004207 /*
4208 * It is possible that the cached previous root page is
4209 * obsolete because of a change in the MMU
4210 * generation number. However, that is accompanied by
4211 * KVM_REQ_MMU_RELOAD, which will free the root that we
4212 * have set here and allocate a new one.
4213 */
4214
Junaid Shahid0aab33e2018-06-27 14:59:09 -07004215 kvm_make_request(KVM_REQ_LOAD_CR3, vcpu);
Junaid Shahid956bf352018-06-27 14:59:18 -07004216 if (!skip_tlb_flush) {
4217 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
Junaid Shahidade61e22018-06-27 14:59:15 -07004218 kvm_x86_ops->tlb_flush(vcpu, true);
Junaid Shahid956bf352018-06-27 14:59:18 -07004219 }
4220
4221 /*
4222 * The last MMIO access's GVA and GPA are cached in the
4223 * VCPU. When switching to a new CR3, that GVA->GPA
4224 * mapping may no longer be valid. So clear any cached
4225 * MMIO info even when we don't need to sync the shadow
4226 * page tables.
4227 */
4228 vcpu_clear_mmio_info(vcpu, MMIO_GVA_ANY);
Junaid Shahidade61e22018-06-27 14:59:15 -07004229
Junaid Shahid7c390d32018-06-27 14:59:06 -07004230 __clear_sp_write_flooding_count(
4231 page_header(mmu->root_hpa));
4232
Junaid Shahid7c390d32018-06-27 14:59:06 -07004233 return true;
4234 }
4235 }
4236
4237 return false;
4238}
4239
Junaid Shahid0aab33e2018-06-27 14:59:09 -07004240static void __kvm_mmu_new_cr3(struct kvm_vcpu *vcpu, gpa_t new_cr3,
Junaid Shahidade61e22018-06-27 14:59:15 -07004241 union kvm_mmu_page_role new_role,
4242 bool skip_tlb_flush)
Junaid Shahid0aab33e2018-06-27 14:59:09 -07004243{
Junaid Shahidade61e22018-06-27 14:59:15 -07004244 if (!fast_cr3_switch(vcpu, new_cr3, new_role, skip_tlb_flush))
Vitaly Kuznetsov6a82cd12018-10-08 21:28:07 +02004245 kvm_mmu_free_roots(vcpu, vcpu->arch.mmu,
4246 KVM_MMU_ROOT_CURRENT);
Junaid Shahid0aab33e2018-06-27 14:59:09 -07004247}
4248
Junaid Shahidade61e22018-06-27 14:59:15 -07004249void kvm_mmu_new_cr3(struct kvm_vcpu *vcpu, gpa_t new_cr3, bool skip_tlb_flush)
Junaid Shahid7c390d32018-06-27 14:59:06 -07004250{
Junaid Shahidade61e22018-06-27 14:59:15 -07004251 __kvm_mmu_new_cr3(vcpu, new_cr3, kvm_mmu_calc_root_page_role(vcpu),
4252 skip_tlb_flush);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004253}
Junaid Shahid50c28f22018-06-27 14:59:11 -07004254EXPORT_SYMBOL_GPL(kvm_mmu_new_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004255
Joerg Roedel5777ed32010-09-10 17:30:42 +02004256static unsigned long get_cr3(struct kvm_vcpu *vcpu)
4257{
Avi Kivity9f8fe502010-12-05 17:30:00 +02004258 return kvm_read_cr3(vcpu);
Joerg Roedel5777ed32010-09-10 17:30:42 +02004259}
4260
Avi Kivity6389ee92010-11-29 16:12:30 +02004261static void inject_page_fault(struct kvm_vcpu *vcpu,
4262 struct x86_exception *fault)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004263{
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02004264 vcpu->arch.mmu->inject_page_fault(vcpu, fault);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004265}
4266
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004267static bool sync_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, gfn_t gfn,
Xiao Guangrongf2fd1252013-06-07 16:51:24 +08004268 unsigned access, int *nr_present)
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004269{
4270 if (unlikely(is_mmio_spte(*sptep))) {
4271 if (gfn != get_mmio_spte_gfn(*sptep)) {
4272 mmu_spte_clear_no_track(sptep);
4273 return true;
4274 }
4275
4276 (*nr_present)++;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004277 mark_mmio_spte(vcpu, sptep, gfn, access);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004278 return true;
4279 }
4280
4281 return false;
4282}
4283
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004284static inline bool is_last_gpte(struct kvm_mmu *mmu,
4285 unsigned level, unsigned gpte)
Avi Kivity6fd01b72012-09-12 20:46:56 +03004286{
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004287 /*
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004288 * The RHS has bit 7 set iff level < mmu->last_nonleaf_level.
4289 * If it is clear, there are no large pages at this level, so clear
4290 * PT_PAGE_SIZE_MASK in gpte if that is the case.
4291 */
4292 gpte &= level - mmu->last_nonleaf_level;
4293
Ladi Prosek829ee272017-10-05 11:10:23 +02004294 /*
4295 * PT_PAGE_TABLE_LEVEL always terminates. The RHS has bit 7 set
4296 * iff level <= PT_PAGE_TABLE_LEVEL, which for our purpose means
4297 * level == PT_PAGE_TABLE_LEVEL; set PT_PAGE_SIZE_MASK in gpte then.
4298 */
4299 gpte |= level - PT_PAGE_TABLE_LEVEL - 1;
4300
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004301 return gpte & PT_PAGE_SIZE_MASK;
Avi Kivity6fd01b72012-09-12 20:46:56 +03004302}
4303
Nadav Har'El37406aa2013-08-05 11:07:12 +03004304#define PTTYPE_EPT 18 /* arbitrary */
4305#define PTTYPE PTTYPE_EPT
4306#include "paging_tmpl.h"
4307#undef PTTYPE
4308
Avi Kivity6aa8b732006-12-10 02:21:36 -08004309#define PTTYPE 64
4310#include "paging_tmpl.h"
4311#undef PTTYPE
4312
4313#define PTTYPE 32
4314#include "paging_tmpl.h"
4315#undef PTTYPE
4316
Xiao Guangrong6dc98b82015-08-05 12:04:22 +08004317static void
4318__reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
4319 struct rsvd_bits_validate *rsvd_check,
4320 int maxphyaddr, int level, bool nx, bool gbpages,
Paolo Bonzini6fec2142015-09-22 23:02:14 +02004321 bool pse, bool amd)
Dong, Eddie82725b22009-03-30 16:21:08 +08004322{
Dong, Eddie82725b22009-03-30 16:21:08 +08004323 u64 exb_bit_rsvd = 0;
Nadav Amit5f7dde72014-05-07 15:32:50 +03004324 u64 gbpages_bit_rsvd = 0;
Paolo Bonzinia0c0feb2014-09-02 13:24:12 +02004325 u64 nonleaf_bit8_rsvd = 0;
Dong, Eddie82725b22009-03-30 16:21:08 +08004326
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004327 rsvd_check->bad_mt_xwr = 0;
Yang Zhang25d92082013-08-06 12:00:32 +03004328
Xiao Guangrong6dc98b82015-08-05 12:04:22 +08004329 if (!nx)
Dong, Eddie82725b22009-03-30 16:21:08 +08004330 exb_bit_rsvd = rsvd_bits(63, 63);
Xiao Guangrong6dc98b82015-08-05 12:04:22 +08004331 if (!gbpages)
Nadav Amit5f7dde72014-05-07 15:32:50 +03004332 gbpages_bit_rsvd = rsvd_bits(7, 7);
Paolo Bonzinia0c0feb2014-09-02 13:24:12 +02004333
4334 /*
4335 * Non-leaf PML4Es and PDPEs reserve bit 8 (which would be the G bit for
4336 * leaf entries) on AMD CPUs only.
4337 */
Paolo Bonzini6fec2142015-09-22 23:02:14 +02004338 if (amd)
Paolo Bonzinia0c0feb2014-09-02 13:24:12 +02004339 nonleaf_bit8_rsvd = rsvd_bits(8, 8);
4340
Xiao Guangrong6dc98b82015-08-05 12:04:22 +08004341 switch (level) {
Dong, Eddie82725b22009-03-30 16:21:08 +08004342 case PT32_ROOT_LEVEL:
4343 /* no rsvd bits for 2 level 4K page table entries */
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004344 rsvd_check->rsvd_bits_mask[0][1] = 0;
4345 rsvd_check->rsvd_bits_mask[0][0] = 0;
4346 rsvd_check->rsvd_bits_mask[1][0] =
4347 rsvd_check->rsvd_bits_mask[0][0];
Xiao Guangrongf815bce2010-03-19 17:58:53 +08004348
Xiao Guangrong6dc98b82015-08-05 12:04:22 +08004349 if (!pse) {
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004350 rsvd_check->rsvd_bits_mask[1][1] = 0;
Xiao Guangrongf815bce2010-03-19 17:58:53 +08004351 break;
4352 }
4353
Dong, Eddie82725b22009-03-30 16:21:08 +08004354 if (is_cpuid_PSE36())
4355 /* 36bits PSE 4MB page */
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004356 rsvd_check->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08004357 else
4358 /* 32 bits PSE 4MB page */
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004359 rsvd_check->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08004360 break;
4361 case PT32E_ROOT_LEVEL:
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004362 rsvd_check->rsvd_bits_mask[0][2] =
Dong, Eddie20c466b2009-03-31 23:03:45 +08004363 rsvd_bits(maxphyaddr, 63) |
Nadav Amitcd9ae5f2014-04-04 06:31:04 +03004364 rsvd_bits(5, 8) | rsvd_bits(1, 2); /* PDPTE */
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004365 rsvd_check->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08004366 rsvd_bits(maxphyaddr, 62); /* PDE */
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004367 rsvd_check->rsvd_bits_mask[0][0] = exb_bit_rsvd |
Dong, Eddie82725b22009-03-30 16:21:08 +08004368 rsvd_bits(maxphyaddr, 62); /* PTE */
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004369 rsvd_check->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Dong, Eddie82725b22009-03-30 16:21:08 +08004370 rsvd_bits(maxphyaddr, 62) |
4371 rsvd_bits(13, 20); /* large page */
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004372 rsvd_check->rsvd_bits_mask[1][0] =
4373 rsvd_check->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08004374 break;
Yu Zhang855feb62017-08-24 20:27:55 +08004375 case PT64_ROOT_5LEVEL:
4376 rsvd_check->rsvd_bits_mask[0][4] = exb_bit_rsvd |
4377 nonleaf_bit8_rsvd | rsvd_bits(7, 7) |
4378 rsvd_bits(maxphyaddr, 51);
4379 rsvd_check->rsvd_bits_mask[1][4] =
4380 rsvd_check->rsvd_bits_mask[0][4];
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06004381 /* fall through */
Yu Zhang2a7266a2017-08-24 20:27:54 +08004382 case PT64_ROOT_4LEVEL:
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004383 rsvd_check->rsvd_bits_mask[0][3] = exb_bit_rsvd |
4384 nonleaf_bit8_rsvd | rsvd_bits(7, 7) |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08004385 rsvd_bits(maxphyaddr, 51);
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004386 rsvd_check->rsvd_bits_mask[0][2] = exb_bit_rsvd |
4387 nonleaf_bit8_rsvd | gbpages_bit_rsvd |
Dong, Eddie82725b22009-03-30 16:21:08 +08004388 rsvd_bits(maxphyaddr, 51);
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004389 rsvd_check->rsvd_bits_mask[0][1] = exb_bit_rsvd |
4390 rsvd_bits(maxphyaddr, 51);
4391 rsvd_check->rsvd_bits_mask[0][0] = exb_bit_rsvd |
4392 rsvd_bits(maxphyaddr, 51);
4393 rsvd_check->rsvd_bits_mask[1][3] =
4394 rsvd_check->rsvd_bits_mask[0][3];
4395 rsvd_check->rsvd_bits_mask[1][2] = exb_bit_rsvd |
Nadav Amit5f7dde72014-05-07 15:32:50 +03004396 gbpages_bit_rsvd | rsvd_bits(maxphyaddr, 51) |
Joerg Roedele04da982009-07-27 16:30:45 +02004397 rsvd_bits(13, 29);
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004398 rsvd_check->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08004399 rsvd_bits(maxphyaddr, 51) |
4400 rsvd_bits(13, 20); /* large page */
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004401 rsvd_check->rsvd_bits_mask[1][0] =
4402 rsvd_check->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08004403 break;
4404 }
4405}
4406
Xiao Guangrong6dc98b82015-08-05 12:04:22 +08004407static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
4408 struct kvm_mmu *context)
4409{
4410 __reset_rsvds_bits_mask(vcpu, &context->guest_rsvd_check,
4411 cpuid_maxphyaddr(vcpu), context->root_level,
Radim Krčmářd6321d42017-08-05 00:12:49 +02004412 context->nx,
4413 guest_cpuid_has(vcpu, X86_FEATURE_GBPAGES),
Paolo Bonzini6fec2142015-09-22 23:02:14 +02004414 is_pse(vcpu), guest_cpuid_is_amd(vcpu));
Xiao Guangrong6dc98b82015-08-05 12:04:22 +08004415}
4416
Xiao Guangrong81b8eeb2015-08-05 12:04:23 +08004417static void
4418__reset_rsvds_bits_mask_ept(struct rsvd_bits_validate *rsvd_check,
4419 int maxphyaddr, bool execonly)
Yang Zhang25d92082013-08-06 12:00:32 +03004420{
Paolo Bonzini951f9fd2015-09-23 10:34:26 +02004421 u64 bad_mt_xwr;
Yang Zhang25d92082013-08-06 12:00:32 +03004422
Yu Zhang855feb62017-08-24 20:27:55 +08004423 rsvd_check->rsvd_bits_mask[0][4] =
4424 rsvd_bits(maxphyaddr, 51) | rsvd_bits(3, 7);
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004425 rsvd_check->rsvd_bits_mask[0][3] =
Yang Zhang25d92082013-08-06 12:00:32 +03004426 rsvd_bits(maxphyaddr, 51) | rsvd_bits(3, 7);
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004427 rsvd_check->rsvd_bits_mask[0][2] =
Yang Zhang25d92082013-08-06 12:00:32 +03004428 rsvd_bits(maxphyaddr, 51) | rsvd_bits(3, 6);
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004429 rsvd_check->rsvd_bits_mask[0][1] =
Yang Zhang25d92082013-08-06 12:00:32 +03004430 rsvd_bits(maxphyaddr, 51) | rsvd_bits(3, 6);
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004431 rsvd_check->rsvd_bits_mask[0][0] = rsvd_bits(maxphyaddr, 51);
Yang Zhang25d92082013-08-06 12:00:32 +03004432
4433 /* large page */
Yu Zhang855feb62017-08-24 20:27:55 +08004434 rsvd_check->rsvd_bits_mask[1][4] = rsvd_check->rsvd_bits_mask[0][4];
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004435 rsvd_check->rsvd_bits_mask[1][3] = rsvd_check->rsvd_bits_mask[0][3];
4436 rsvd_check->rsvd_bits_mask[1][2] =
Yang Zhang25d92082013-08-06 12:00:32 +03004437 rsvd_bits(maxphyaddr, 51) | rsvd_bits(12, 29);
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004438 rsvd_check->rsvd_bits_mask[1][1] =
Yang Zhang25d92082013-08-06 12:00:32 +03004439 rsvd_bits(maxphyaddr, 51) | rsvd_bits(12, 20);
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004440 rsvd_check->rsvd_bits_mask[1][0] = rsvd_check->rsvd_bits_mask[0][0];
Yang Zhang25d92082013-08-06 12:00:32 +03004441
Paolo Bonzini951f9fd2015-09-23 10:34:26 +02004442 bad_mt_xwr = 0xFFull << (2 * 8); /* bits 3..5 must not be 2 */
4443 bad_mt_xwr |= 0xFFull << (3 * 8); /* bits 3..5 must not be 3 */
4444 bad_mt_xwr |= 0xFFull << (7 * 8); /* bits 3..5 must not be 7 */
4445 bad_mt_xwr |= REPEAT_BYTE(1ull << 2); /* bits 0..2 must not be 010 */
4446 bad_mt_xwr |= REPEAT_BYTE(1ull << 6); /* bits 0..2 must not be 110 */
4447 if (!execonly) {
4448 /* bits 0..2 must not be 100 unless VMX capabilities allow it */
4449 bad_mt_xwr |= REPEAT_BYTE(1ull << 4);
Yang Zhang25d92082013-08-06 12:00:32 +03004450 }
Paolo Bonzini951f9fd2015-09-23 10:34:26 +02004451 rsvd_check->bad_mt_xwr = bad_mt_xwr;
Yang Zhang25d92082013-08-06 12:00:32 +03004452}
4453
Xiao Guangrong81b8eeb2015-08-05 12:04:23 +08004454static void reset_rsvds_bits_mask_ept(struct kvm_vcpu *vcpu,
4455 struct kvm_mmu *context, bool execonly)
4456{
4457 __reset_rsvds_bits_mask_ept(&context->guest_rsvd_check,
4458 cpuid_maxphyaddr(vcpu), execonly);
4459}
4460
Xiao Guangrongc258b622015-08-05 12:04:24 +08004461/*
4462 * the page table on host is the shadow page table for the page
4463 * table in guest or amd nested guest, its mmu features completely
4464 * follow the features in guest.
4465 */
4466void
4467reset_shadow_zero_bits_mask(struct kvm_vcpu *vcpu, struct kvm_mmu *context)
4468{
Vitaly Kuznetsov36d9594d2018-10-08 21:28:10 +02004469 bool uses_nx = context->nx ||
4470 context->mmu_role.base.smep_andnot_wp;
Brijesh Singhea2800d2017-08-25 15:55:40 -05004471 struct rsvd_bits_validate *shadow_zero_check;
4472 int i;
Paolo Bonzini5f0b8192016-03-09 14:28:02 +01004473
Paolo Bonzini6fec2142015-09-22 23:02:14 +02004474 /*
4475 * Passing "true" to the last argument is okay; it adds a check
4476 * on bit 8 of the SPTEs which KVM doesn't use anyway.
4477 */
Brijesh Singhea2800d2017-08-25 15:55:40 -05004478 shadow_zero_check = &context->shadow_zero_check;
4479 __reset_rsvds_bits_mask(vcpu, shadow_zero_check,
Xiao Guangrongc258b622015-08-05 12:04:24 +08004480 boot_cpu_data.x86_phys_bits,
Paolo Bonzini5f0b8192016-03-09 14:28:02 +01004481 context->shadow_root_level, uses_nx,
Radim Krčmářd6321d42017-08-05 00:12:49 +02004482 guest_cpuid_has(vcpu, X86_FEATURE_GBPAGES),
4483 is_pse(vcpu), true);
Brijesh Singhea2800d2017-08-25 15:55:40 -05004484
4485 if (!shadow_me_mask)
4486 return;
4487
4488 for (i = context->shadow_root_level; --i >= 0;) {
4489 shadow_zero_check->rsvd_bits_mask[0][i] &= ~shadow_me_mask;
4490 shadow_zero_check->rsvd_bits_mask[1][i] &= ~shadow_me_mask;
4491 }
4492
Xiao Guangrongc258b622015-08-05 12:04:24 +08004493}
4494EXPORT_SYMBOL_GPL(reset_shadow_zero_bits_mask);
4495
Paolo Bonzini6fec2142015-09-22 23:02:14 +02004496static inline bool boot_cpu_is_amd(void)
4497{
4498 WARN_ON_ONCE(!tdp_enabled);
4499 return shadow_x_mask == 0;
4500}
4501
Xiao Guangrongc258b622015-08-05 12:04:24 +08004502/*
4503 * the direct page table on host, use as much mmu features as
4504 * possible, however, kvm currently does not do execution-protection.
4505 */
4506static void
4507reset_tdp_shadow_zero_bits_mask(struct kvm_vcpu *vcpu,
4508 struct kvm_mmu *context)
4509{
Brijesh Singhea2800d2017-08-25 15:55:40 -05004510 struct rsvd_bits_validate *shadow_zero_check;
4511 int i;
4512
4513 shadow_zero_check = &context->shadow_zero_check;
4514
Paolo Bonzini6fec2142015-09-22 23:02:14 +02004515 if (boot_cpu_is_amd())
Brijesh Singhea2800d2017-08-25 15:55:40 -05004516 __reset_rsvds_bits_mask(vcpu, shadow_zero_check,
Xiao Guangrongc258b622015-08-05 12:04:24 +08004517 boot_cpu_data.x86_phys_bits,
4518 context->shadow_root_level, false,
Borislav Petkovb8291adc2016-03-29 17:41:58 +02004519 boot_cpu_has(X86_FEATURE_GBPAGES),
4520 true, true);
Xiao Guangrongc258b622015-08-05 12:04:24 +08004521 else
Brijesh Singhea2800d2017-08-25 15:55:40 -05004522 __reset_rsvds_bits_mask_ept(shadow_zero_check,
Xiao Guangrongc258b622015-08-05 12:04:24 +08004523 boot_cpu_data.x86_phys_bits,
4524 false);
4525
Brijesh Singhea2800d2017-08-25 15:55:40 -05004526 if (!shadow_me_mask)
4527 return;
4528
4529 for (i = context->shadow_root_level; --i >= 0;) {
4530 shadow_zero_check->rsvd_bits_mask[0][i] &= ~shadow_me_mask;
4531 shadow_zero_check->rsvd_bits_mask[1][i] &= ~shadow_me_mask;
4532 }
Xiao Guangrongc258b622015-08-05 12:04:24 +08004533}
4534
4535/*
4536 * as the comments in reset_shadow_zero_bits_mask() except it
4537 * is the shadow page table for intel nested guest.
4538 */
4539static void
4540reset_ept_shadow_zero_bits_mask(struct kvm_vcpu *vcpu,
4541 struct kvm_mmu *context, bool execonly)
4542{
4543 __reset_rsvds_bits_mask_ept(&context->shadow_zero_check,
4544 boot_cpu_data.x86_phys_bits, execonly);
4545}
4546
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004547#define BYTE_MASK(access) \
4548 ((1 & (access) ? 2 : 0) | \
4549 (2 & (access) ? 4 : 0) | \
4550 (3 & (access) ? 8 : 0) | \
4551 (4 & (access) ? 16 : 0) | \
4552 (5 & (access) ? 32 : 0) | \
4553 (6 & (access) ? 64 : 0) | \
4554 (7 & (access) ? 128 : 0))
4555
4556
Xiao Guangrongedc90b72015-05-11 22:55:21 +08004557static void update_permission_bitmask(struct kvm_vcpu *vcpu,
4558 struct kvm_mmu *mmu, bool ept)
Avi Kivity97d64b72012-09-12 14:52:00 +03004559{
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004560 unsigned byte;
Avi Kivity97d64b72012-09-12 14:52:00 +03004561
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004562 const u8 x = BYTE_MASK(ACC_EXEC_MASK);
4563 const u8 w = BYTE_MASK(ACC_WRITE_MASK);
4564 const u8 u = BYTE_MASK(ACC_USER_MASK);
4565
4566 bool cr4_smep = kvm_read_cr4_bits(vcpu, X86_CR4_SMEP) != 0;
4567 bool cr4_smap = kvm_read_cr4_bits(vcpu, X86_CR4_SMAP) != 0;
4568 bool cr0_wp = is_write_protection(vcpu);
4569
Avi Kivity97d64b72012-09-12 14:52:00 +03004570 for (byte = 0; byte < ARRAY_SIZE(mmu->permissions); ++byte) {
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004571 unsigned pfec = byte << 1;
4572
Feng Wu97ec8c02014-04-01 17:46:34 +08004573 /*
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004574 * Each "*f" variable has a 1 bit for each UWX value
4575 * that causes a fault with the given PFEC.
Feng Wu97ec8c02014-04-01 17:46:34 +08004576 */
Avi Kivity97d64b72012-09-12 14:52:00 +03004577
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004578 /* Faults from writes to non-writable pages */
4579 u8 wf = (pfec & PFERR_WRITE_MASK) ? ~w : 0;
4580 /* Faults from user mode accesses to supervisor pages */
4581 u8 uf = (pfec & PFERR_USER_MASK) ? ~u : 0;
4582 /* Faults from fetches of non-executable pages*/
4583 u8 ff = (pfec & PFERR_FETCH_MASK) ? ~x : 0;
4584 /* Faults from kernel mode fetches of user pages */
4585 u8 smepf = 0;
4586 /* Faults from kernel mode accesses of user pages */
4587 u8 smapf = 0;
Feng Wu97ec8c02014-04-01 17:46:34 +08004588
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004589 if (!ept) {
4590 /* Faults from kernel mode accesses to user pages */
4591 u8 kf = (pfec & PFERR_USER_MASK) ? 0 : u;
Avi Kivity97d64b72012-09-12 14:52:00 +03004592
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004593 /* Not really needed: !nx will cause pte.nx to fault */
4594 if (!mmu->nx)
4595 ff = 0;
4596
4597 /* Allow supervisor writes if !cr0.wp */
4598 if (!cr0_wp)
4599 wf = (pfec & PFERR_USER_MASK) ? wf : 0;
4600
4601 /* Disallow supervisor fetches of user code if cr4.smep */
4602 if (cr4_smep)
4603 smepf = (pfec & PFERR_FETCH_MASK) ? kf : 0;
4604
4605 /*
4606 * SMAP:kernel-mode data accesses from user-mode
4607 * mappings should fault. A fault is considered
4608 * as a SMAP violation if all of the following
Peng Hao39337ad2018-10-04 11:45:00 -04004609 * conditions are true:
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004610 * - X86_CR4_SMAP is set in CR4
4611 * - A user page is accessed
4612 * - The access is not a fetch
4613 * - Page fault in kernel mode
4614 * - if CPL = 3 or X86_EFLAGS_AC is clear
4615 *
4616 * Here, we cover the first three conditions.
4617 * The fourth is computed dynamically in permission_fault();
4618 * PFERR_RSVD_MASK bit will be set in PFEC if the access is
4619 * *not* subject to SMAP restrictions.
4620 */
4621 if (cr4_smap)
4622 smapf = (pfec & (PFERR_RSVD_MASK|PFERR_FETCH_MASK)) ? 0 : kf;
Avi Kivity97d64b72012-09-12 14:52:00 +03004623 }
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004624
4625 mmu->permissions[byte] = ff | uf | wf | smepf | smapf;
Avi Kivity97d64b72012-09-12 14:52:00 +03004626 }
4627}
4628
Huaitong Han2d344102016-03-22 16:51:19 +08004629/*
4630* PKU is an additional mechanism by which the paging controls access to
4631* user-mode addresses based on the value in the PKRU register. Protection
4632* key violations are reported through a bit in the page fault error code.
4633* Unlike other bits of the error code, the PK bit is not known at the
4634* call site of e.g. gva_to_gpa; it must be computed directly in
4635* permission_fault based on two bits of PKRU, on some machine state (CR4,
4636* CR0, EFER, CPL), and on other bits of the error code and the page tables.
4637*
4638* In particular the following conditions come from the error code, the
4639* page tables and the machine state:
4640* - PK is always zero unless CR4.PKE=1 and EFER.LMA=1
4641* - PK is always zero if RSVD=1 (reserved bit set) or F=1 (instruction fetch)
4642* - PK is always zero if U=0 in the page tables
4643* - PKRU.WD is ignored if CR0.WP=0 and the access is a supervisor access.
4644*
4645* The PKRU bitmask caches the result of these four conditions. The error
4646* code (minus the P bit) and the page table's U bit form an index into the
4647* PKRU bitmask. Two bits of the PKRU bitmask are then extracted and ANDed
4648* with the two bits of the PKRU register corresponding to the protection key.
4649* For the first three conditions above the bits will be 00, thus masking
4650* away both AD and WD. For all reads or if the last condition holds, WD
4651* only will be masked away.
4652*/
4653static void update_pkru_bitmask(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
4654 bool ept)
4655{
4656 unsigned bit;
4657 bool wp;
4658
4659 if (ept) {
4660 mmu->pkru_mask = 0;
4661 return;
4662 }
4663
4664 /* PKEY is enabled only if CR4.PKE and EFER.LMA are both set. */
4665 if (!kvm_read_cr4_bits(vcpu, X86_CR4_PKE) || !is_long_mode(vcpu)) {
4666 mmu->pkru_mask = 0;
4667 return;
4668 }
4669
4670 wp = is_write_protection(vcpu);
4671
4672 for (bit = 0; bit < ARRAY_SIZE(mmu->permissions); ++bit) {
4673 unsigned pfec, pkey_bits;
4674 bool check_pkey, check_write, ff, uf, wf, pte_user;
4675
4676 pfec = bit << 1;
4677 ff = pfec & PFERR_FETCH_MASK;
4678 uf = pfec & PFERR_USER_MASK;
4679 wf = pfec & PFERR_WRITE_MASK;
4680
4681 /* PFEC.RSVD is replaced by ACC_USER_MASK. */
4682 pte_user = pfec & PFERR_RSVD_MASK;
4683
4684 /*
4685 * Only need to check the access which is not an
4686 * instruction fetch and is to a user page.
4687 */
4688 check_pkey = (!ff && pte_user);
4689 /*
4690 * write access is controlled by PKRU if it is a
4691 * user access or CR0.WP = 1.
4692 */
4693 check_write = check_pkey && wf && (uf || wp);
4694
4695 /* PKRU.AD stops both read and write access. */
4696 pkey_bits = !!check_pkey;
4697 /* PKRU.WD stops write access. */
4698 pkey_bits |= (!!check_write) << 1;
4699
4700 mmu->pkru_mask |= (pkey_bits & 3) << pfec;
4701 }
4702}
4703
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004704static void update_last_nonleaf_level(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu)
Avi Kivity6fd01b72012-09-12 20:46:56 +03004705{
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004706 unsigned root_level = mmu->root_level;
Avi Kivity6fd01b72012-09-12 20:46:56 +03004707
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004708 mmu->last_nonleaf_level = root_level;
4709 if (root_level == PT32_ROOT_LEVEL && is_pse(vcpu))
4710 mmu->last_nonleaf_level++;
Avi Kivity6fd01b72012-09-12 20:46:56 +03004711}
4712
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004713static void paging64_init_context_common(struct kvm_vcpu *vcpu,
4714 struct kvm_mmu *context,
4715 int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004716{
Joerg Roedel2d48a982010-09-10 17:31:01 +02004717 context->nx = is_nx(vcpu);
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004718 context->root_level = level;
Joerg Roedel2d48a982010-09-10 17:31:01 +02004719
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004720 reset_rsvds_bits_mask(vcpu, context);
Yang Zhang25d92082013-08-06 12:00:32 +03004721 update_permission_bitmask(vcpu, context, false);
Huaitong Han2d344102016-03-22 16:51:19 +08004722 update_pkru_bitmask(vcpu, context, false);
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004723 update_last_nonleaf_level(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004724
Paolo Bonzinifa4a2c02013-10-02 16:56:16 +02004725 MMU_WARN_ON(!is_pae(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004726 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004727 context->gva_to_gpa = paging64_gva_to_gpa;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03004728 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03004729 context->invlpg = paging64_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08004730 context->update_pte = paging64_update_pte;
Avi Kivity17ac10a2007-01-05 16:36:40 -08004731 context->shadow_root_level = level;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02004732 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004733}
4734
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004735static void paging64_init_context(struct kvm_vcpu *vcpu,
4736 struct kvm_mmu *context)
Avi Kivity17ac10a2007-01-05 16:36:40 -08004737{
Yu Zhang855feb62017-08-24 20:27:55 +08004738 int root_level = is_la57_mode(vcpu) ?
4739 PT64_ROOT_5LEVEL : PT64_ROOT_4LEVEL;
4740
4741 paging64_init_context_common(vcpu, context, root_level);
Avi Kivity17ac10a2007-01-05 16:36:40 -08004742}
4743
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004744static void paging32_init_context(struct kvm_vcpu *vcpu,
4745 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004746{
Joerg Roedel2d48a982010-09-10 17:31:01 +02004747 context->nx = false;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004748 context->root_level = PT32_ROOT_LEVEL;
Joerg Roedel2d48a982010-09-10 17:31:01 +02004749
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004750 reset_rsvds_bits_mask(vcpu, context);
Yang Zhang25d92082013-08-06 12:00:32 +03004751 update_permission_bitmask(vcpu, context, false);
Huaitong Han2d344102016-03-22 16:51:19 +08004752 update_pkru_bitmask(vcpu, context, false);
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004753 update_last_nonleaf_level(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004754
Avi Kivity6aa8b732006-12-10 02:21:36 -08004755 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004756 context->gva_to_gpa = paging32_gva_to_gpa;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03004757 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03004758 context->invlpg = paging32_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08004759 context->update_pte = paging32_update_pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004760 context->shadow_root_level = PT32E_ROOT_LEVEL;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02004761 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004762}
4763
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004764static void paging32E_init_context(struct kvm_vcpu *vcpu,
4765 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004766{
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004767 paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004768}
4769
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004770static union kvm_mmu_extended_role kvm_calc_mmu_role_ext(struct kvm_vcpu *vcpu)
Junaid Shahid9fa72112018-06-27 14:59:07 -07004771{
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004772 union kvm_mmu_extended_role ext = {0};
Junaid Shahid9fa72112018-06-27 14:59:07 -07004773
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004774 ext.cr0_pg = !!is_paging(vcpu);
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004775 ext.cr4_smep = !!kvm_read_cr4_bits(vcpu, X86_CR4_SMEP);
4776 ext.cr4_smap = !!kvm_read_cr4_bits(vcpu, X86_CR4_SMAP);
4777 ext.cr4_pse = !!is_pse(vcpu);
4778 ext.cr4_pke = !!kvm_read_cr4_bits(vcpu, X86_CR4_PKE);
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004779 ext.cr4_la57 = !!kvm_read_cr4_bits(vcpu, X86_CR4_LA57);
Yu Zhangde3ccd22019-02-01 00:09:23 +08004780 ext.maxphyaddr = cpuid_maxphyaddr(vcpu);
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004781
4782 ext.valid = 1;
4783
4784 return ext;
4785}
4786
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004787static union kvm_mmu_role kvm_calc_mmu_role_common(struct kvm_vcpu *vcpu,
4788 bool base_only)
Junaid Shahid9fa72112018-06-27 14:59:07 -07004789{
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004790 union kvm_mmu_role role = {0};
Junaid Shahid9fa72112018-06-27 14:59:07 -07004791
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004792 role.base.access = ACC_ALL;
4793 role.base.nxe = !!is_nx(vcpu);
4794 role.base.cr4_pae = !!is_pae(vcpu);
4795 role.base.cr0_wp = is_write_protection(vcpu);
4796 role.base.smm = is_smm(vcpu);
4797 role.base.guest_mode = is_guest_mode(vcpu);
4798
4799 if (base_only)
4800 return role;
4801
4802 role.ext = kvm_calc_mmu_role_ext(vcpu);
4803
4804 return role;
4805}
4806
4807static union kvm_mmu_role
4808kvm_calc_tdp_mmu_root_page_role(struct kvm_vcpu *vcpu, bool base_only)
4809{
4810 union kvm_mmu_role role = kvm_calc_mmu_role_common(vcpu, base_only);
4811
4812 role.base.ad_disabled = (shadow_accessed_mask == 0);
4813 role.base.level = kvm_x86_ops->get_tdp_level(vcpu);
4814 role.base.direct = true;
Junaid Shahid9fa72112018-06-27 14:59:07 -07004815
4816 return role;
4817}
4818
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004819static void init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
Joerg Roedelfb72d162008-02-07 13:47:44 +01004820{
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02004821 struct kvm_mmu *context = vcpu->arch.mmu;
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004822 union kvm_mmu_role new_role =
4823 kvm_calc_tdp_mmu_root_page_role(vcpu, false);
Joerg Roedelfb72d162008-02-07 13:47:44 +01004824
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004825 new_role.base.word &= mmu_base_role_mask.word;
4826 if (new_role.as_u64 == context->mmu_role.as_u64)
4827 return;
4828
4829 context->mmu_role.as_u64 = new_role.as_u64;
Joerg Roedelfb72d162008-02-07 13:47:44 +01004830 context->page_fault = tdp_page_fault;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03004831 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03004832 context->invlpg = nonpaging_invlpg;
Xiao Guangrong0f53b5b2011-03-09 15:43:51 +08004833 context->update_pte = nonpaging_update_pte;
Yu Zhang855feb62017-08-24 20:27:55 +08004834 context->shadow_root_level = kvm_x86_ops->get_tdp_level(vcpu);
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02004835 context->direct_map = true;
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004836 context->set_cr3 = kvm_x86_ops->set_tdp_cr3;
Joerg Roedel5777ed32010-09-10 17:30:42 +02004837 context->get_cr3 = get_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03004838 context->get_pdptr = kvm_pdptr_read;
Joerg Roedelcb659db2010-09-10 17:30:43 +02004839 context->inject_page_fault = kvm_inject_page_fault;
Joerg Roedelfb72d162008-02-07 13:47:44 +01004840
4841 if (!is_paging(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02004842 context->nx = false;
Joerg Roedelfb72d162008-02-07 13:47:44 +01004843 context->gva_to_gpa = nonpaging_gva_to_gpa;
4844 context->root_level = 0;
4845 } else if (is_long_mode(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02004846 context->nx = is_nx(vcpu);
Yu Zhang855feb62017-08-24 20:27:55 +08004847 context->root_level = is_la57_mode(vcpu) ?
4848 PT64_ROOT_5LEVEL : PT64_ROOT_4LEVEL;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004849 reset_rsvds_bits_mask(vcpu, context);
4850 context->gva_to_gpa = paging64_gva_to_gpa;
Joerg Roedelfb72d162008-02-07 13:47:44 +01004851 } else if (is_pae(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02004852 context->nx = is_nx(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01004853 context->root_level = PT32E_ROOT_LEVEL;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004854 reset_rsvds_bits_mask(vcpu, context);
4855 context->gva_to_gpa = paging64_gva_to_gpa;
Joerg Roedelfb72d162008-02-07 13:47:44 +01004856 } else {
Joerg Roedel2d48a982010-09-10 17:31:01 +02004857 context->nx = false;
Joerg Roedelfb72d162008-02-07 13:47:44 +01004858 context->root_level = PT32_ROOT_LEVEL;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004859 reset_rsvds_bits_mask(vcpu, context);
4860 context->gva_to_gpa = paging32_gva_to_gpa;
Joerg Roedelfb72d162008-02-07 13:47:44 +01004861 }
4862
Yang Zhang25d92082013-08-06 12:00:32 +03004863 update_permission_bitmask(vcpu, context, false);
Huaitong Han2d344102016-03-22 16:51:19 +08004864 update_pkru_bitmask(vcpu, context, false);
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004865 update_last_nonleaf_level(vcpu, context);
Xiao Guangrongc258b622015-08-05 12:04:24 +08004866 reset_tdp_shadow_zero_bits_mask(vcpu, context);
Joerg Roedelfb72d162008-02-07 13:47:44 +01004867}
4868
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004869static union kvm_mmu_role
4870kvm_calc_shadow_mmu_root_page_role(struct kvm_vcpu *vcpu, bool base_only)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004871{
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004872 union kvm_mmu_role role = kvm_calc_mmu_role_common(vcpu, base_only);
Paolo Bonziniad896af2013-10-02 16:56:14 +02004873
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004874 role.base.smep_andnot_wp = role.ext.cr4_smep &&
4875 !is_write_protection(vcpu);
4876 role.base.smap_andnot_wp = role.ext.cr4_smap &&
4877 !is_write_protection(vcpu);
4878 role.base.direct = !is_paging(vcpu);
Junaid Shahid9fa72112018-06-27 14:59:07 -07004879
4880 if (!is_long_mode(vcpu))
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004881 role.base.level = PT32E_ROOT_LEVEL;
Junaid Shahid9fa72112018-06-27 14:59:07 -07004882 else if (is_la57_mode(vcpu))
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004883 role.base.level = PT64_ROOT_5LEVEL;
Junaid Shahid9fa72112018-06-27 14:59:07 -07004884 else
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004885 role.base.level = PT64_ROOT_4LEVEL;
Junaid Shahid9fa72112018-06-27 14:59:07 -07004886
4887 return role;
4888}
4889
4890void kvm_init_shadow_mmu(struct kvm_vcpu *vcpu)
4891{
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02004892 struct kvm_mmu *context = vcpu->arch.mmu;
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004893 union kvm_mmu_role new_role =
4894 kvm_calc_shadow_mmu_root_page_role(vcpu, false);
4895
4896 new_role.base.word &= mmu_base_role_mask.word;
4897 if (new_role.as_u64 == context->mmu_role.as_u64)
4898 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004899
4900 if (!is_paging(vcpu))
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004901 nonpaging_init_context(vcpu, context);
Avi Kivitya9058ec2006-12-29 16:49:37 -08004902 else if (is_long_mode(vcpu))
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004903 paging64_init_context(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004904 else if (is_pae(vcpu))
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004905 paging32E_init_context(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004906 else
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004907 paging32_init_context(vcpu, context);
Avi Kivitya770f6f2008-12-21 19:20:09 +02004908
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004909 context->mmu_role.as_u64 = new_role.as_u64;
Xiao Guangrongc258b622015-08-05 12:04:24 +08004910 reset_shadow_zero_bits_mask(vcpu, context);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02004911}
4912EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu);
4913
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004914static union kvm_mmu_role
4915kvm_calc_shadow_ept_root_page_role(struct kvm_vcpu *vcpu, bool accessed_dirty,
4916 bool execonly)
Junaid Shahid9fa72112018-06-27 14:59:07 -07004917{
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004918 union kvm_mmu_role role;
Junaid Shahid9fa72112018-06-27 14:59:07 -07004919
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004920 /* Base role is inherited from root_mmu */
4921 role.base.word = vcpu->arch.root_mmu.mmu_role.base.word;
4922 role.ext = kvm_calc_mmu_role_ext(vcpu);
Junaid Shahid9fa72112018-06-27 14:59:07 -07004923
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004924 role.base.level = PT64_ROOT_4LEVEL;
4925 role.base.direct = false;
4926 role.base.ad_disabled = !accessed_dirty;
4927 role.base.guest_mode = true;
4928 role.base.access = ACC_ALL;
4929
4930 role.ext.execonly = execonly;
Junaid Shahid9fa72112018-06-27 14:59:07 -07004931
4932 return role;
4933}
4934
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02004935void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly,
Junaid Shahid50c28f22018-06-27 14:59:11 -07004936 bool accessed_dirty, gpa_t new_eptp)
Nadav Har'El155a97a2013-08-05 11:07:16 +03004937{
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02004938 struct kvm_mmu *context = vcpu->arch.mmu;
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004939 union kvm_mmu_role new_role =
4940 kvm_calc_shadow_ept_root_page_role(vcpu, accessed_dirty,
4941 execonly);
Paolo Bonziniad896af2013-10-02 16:56:14 +02004942
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004943 __kvm_mmu_new_cr3(vcpu, new_eptp, new_role.base, false);
4944
4945 new_role.base.word &= mmu_base_role_mask.word;
4946 if (new_role.as_u64 == context->mmu_role.as_u64)
4947 return;
4948
Yu Zhang855feb62017-08-24 20:27:55 +08004949 context->shadow_root_level = PT64_ROOT_4LEVEL;
Nadav Har'El155a97a2013-08-05 11:07:16 +03004950
4951 context->nx = true;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02004952 context->ept_ad = accessed_dirty;
Nadav Har'El155a97a2013-08-05 11:07:16 +03004953 context->page_fault = ept_page_fault;
4954 context->gva_to_gpa = ept_gva_to_gpa;
4955 context->sync_page = ept_sync_page;
4956 context->invlpg = ept_invlpg;
4957 context->update_pte = ept_update_pte;
Yu Zhang855feb62017-08-24 20:27:55 +08004958 context->root_level = PT64_ROOT_4LEVEL;
Nadav Har'El155a97a2013-08-05 11:07:16 +03004959 context->direct_map = false;
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004960 context->mmu_role.as_u64 = new_role.as_u64;
Vitaly Kuznetsov3dc773e2018-10-08 21:28:06 +02004961
Nadav Har'El155a97a2013-08-05 11:07:16 +03004962 update_permission_bitmask(vcpu, context, true);
Huaitong Han2d344102016-03-22 16:51:19 +08004963 update_pkru_bitmask(vcpu, context, true);
Ladi Prosekfd19d3b42017-10-05 11:10:22 +02004964 update_last_nonleaf_level(vcpu, context);
Nadav Har'El155a97a2013-08-05 11:07:16 +03004965 reset_rsvds_bits_mask_ept(vcpu, context, execonly);
Xiao Guangrongc258b622015-08-05 12:04:24 +08004966 reset_ept_shadow_zero_bits_mask(vcpu, context, execonly);
Nadav Har'El155a97a2013-08-05 11:07:16 +03004967}
4968EXPORT_SYMBOL_GPL(kvm_init_shadow_ept_mmu);
4969
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004970static void init_kvm_softmmu(struct kvm_vcpu *vcpu)
Joerg Roedel52fde8d2010-09-10 17:30:44 +02004971{
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02004972 struct kvm_mmu *context = vcpu->arch.mmu;
Paolo Bonziniad896af2013-10-02 16:56:14 +02004973
4974 kvm_init_shadow_mmu(vcpu);
4975 context->set_cr3 = kvm_x86_ops->set_cr3;
4976 context->get_cr3 = get_cr3;
4977 context->get_pdptr = kvm_pdptr_read;
4978 context->inject_page_fault = kvm_inject_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004979}
4980
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004981static void init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004982{
Vitaly Kuznetsovbf627a92018-10-08 21:28:13 +02004983 union kvm_mmu_role new_role = kvm_calc_mmu_role_common(vcpu, false);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004984 struct kvm_mmu *g_context = &vcpu->arch.nested_mmu;
4985
Vitaly Kuznetsovbf627a92018-10-08 21:28:13 +02004986 new_role.base.word &= mmu_base_role_mask.word;
4987 if (new_role.as_u64 == g_context->mmu_role.as_u64)
4988 return;
4989
4990 g_context->mmu_role.as_u64 = new_role.as_u64;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004991 g_context->get_cr3 = get_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03004992 g_context->get_pdptr = kvm_pdptr_read;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004993 g_context->inject_page_fault = kvm_inject_page_fault;
4994
4995 /*
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02004996 * Note that arch.mmu->gva_to_gpa translates l2_gpa to l1_gpa using
David Matlack0af25932015-12-30 08:26:17 -08004997 * L1's nested page tables (e.g. EPT12). The nested translation
4998 * of l2_gva to l1_gpa is done by arch.nested_mmu.gva_to_gpa using
4999 * L2's page tables as the first level of translation and L1's
5000 * nested page tables as the second level of translation. Basically
5001 * the gva_to_gpa functions between mmu and nested_mmu are swapped.
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005002 */
5003 if (!is_paging(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02005004 g_context->nx = false;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005005 g_context->root_level = 0;
5006 g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested;
5007 } else if (is_long_mode(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02005008 g_context->nx = is_nx(vcpu);
Yu Zhang855feb62017-08-24 20:27:55 +08005009 g_context->root_level = is_la57_mode(vcpu) ?
5010 PT64_ROOT_5LEVEL : PT64_ROOT_4LEVEL;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01005011 reset_rsvds_bits_mask(vcpu, g_context);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005012 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
5013 } else if (is_pae(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02005014 g_context->nx = is_nx(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005015 g_context->root_level = PT32E_ROOT_LEVEL;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01005016 reset_rsvds_bits_mask(vcpu, g_context);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005017 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
5018 } else {
Joerg Roedel2d48a982010-09-10 17:31:01 +02005019 g_context->nx = false;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005020 g_context->root_level = PT32_ROOT_LEVEL;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01005021 reset_rsvds_bits_mask(vcpu, g_context);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005022 g_context->gva_to_gpa = paging32_gva_to_gpa_nested;
5023 }
5024
Yang Zhang25d92082013-08-06 12:00:32 +03005025 update_permission_bitmask(vcpu, g_context, false);
Huaitong Han2d344102016-03-22 16:51:19 +08005026 update_pkru_bitmask(vcpu, g_context, false);
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01005027 update_last_nonleaf_level(vcpu, g_context);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005028}
5029
Junaid Shahid1c53da32018-06-27 14:59:10 -07005030void kvm_init_mmu(struct kvm_vcpu *vcpu, bool reset_roots)
Joerg Roedelfb72d162008-02-07 13:47:44 +01005031{
Junaid Shahid1c53da32018-06-27 14:59:10 -07005032 if (reset_roots) {
Junaid Shahidb94742c2018-06-27 14:59:20 -07005033 uint i;
5034
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005035 vcpu->arch.mmu->root_hpa = INVALID_PAGE;
Junaid Shahidb94742c2018-06-27 14:59:20 -07005036
5037 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005038 vcpu->arch.mmu->prev_roots[i] = KVM_MMU_ROOT_INFO_INVALID;
Junaid Shahid1c53da32018-06-27 14:59:10 -07005039 }
5040
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005041 if (mmu_is_nested(vcpu))
Paolo Bonzinie0c6db32014-12-23 13:39:46 +01005042 init_kvm_nested_mmu(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02005043 else if (tdp_enabled)
Paolo Bonzinie0c6db32014-12-23 13:39:46 +01005044 init_kvm_tdp_mmu(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01005045 else
Paolo Bonzinie0c6db32014-12-23 13:39:46 +01005046 init_kvm_softmmu(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01005047}
Junaid Shahid1c53da32018-06-27 14:59:10 -07005048EXPORT_SYMBOL_GPL(kvm_init_mmu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01005049
Junaid Shahid9fa72112018-06-27 14:59:07 -07005050static union kvm_mmu_page_role
5051kvm_mmu_calc_root_page_role(struct kvm_vcpu *vcpu)
5052{
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02005053 union kvm_mmu_role role;
5054
Junaid Shahid9fa72112018-06-27 14:59:07 -07005055 if (tdp_enabled)
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02005056 role = kvm_calc_tdp_mmu_root_page_role(vcpu, true);
Junaid Shahid9fa72112018-06-27 14:59:07 -07005057 else
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02005058 role = kvm_calc_shadow_mmu_root_page_role(vcpu, true);
5059
5060 return role.base;
Junaid Shahid9fa72112018-06-27 14:59:07 -07005061}
Dong, Eddie489f1d62008-01-07 11:14:20 +02005062
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02005063void kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
Avi Kivity17c3ba92007-06-04 15:58:30 +03005064{
Paolo Bonzini95f93af2013-10-02 16:56:12 +02005065 kvm_mmu_unload(vcpu);
Junaid Shahid1c53da32018-06-27 14:59:10 -07005066 kvm_init_mmu(vcpu, true);
Eddie Dong8668a3c2007-10-10 14:26:45 +08005067}
Avi Kivity17c3ba92007-06-04 15:58:30 +03005068EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
5069
5070int kvm_mmu_load(struct kvm_vcpu *vcpu)
Avi Kivity714b93d2007-01-05 16:36:53 -08005071{
5072 int r;
Avi Kivitye2dec932007-01-05 16:36:54 -08005073
Avi Kivity17c3ba92007-06-04 15:58:30 +03005074 r = mmu_topup_memory_caches(vcpu);
5075 if (r)
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05005076 goto out;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03005077 r = mmu_alloc_roots(vcpu);
Takuya Yoshikawae2858b42013-05-09 15:45:12 +09005078 kvm_mmu_sync_roots(vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03005079 if (r)
5080 goto out;
Junaid Shahid6e427822018-06-27 14:59:08 -07005081 kvm_mmu_load_cr3(vcpu);
Junaid Shahidafe828d2018-06-27 14:59:12 -07005082 kvm_x86_ops->tlb_flush(vcpu, true);
Avi Kivity714b93d2007-01-05 16:36:53 -08005083out:
Avi Kivity6aa8b732006-12-10 02:21:36 -08005084 return r;
Avi Kivity17c3ba92007-06-04 15:58:30 +03005085}
5086EXPORT_SYMBOL_GPL(kvm_mmu_load);
5087
5088void kvm_mmu_unload(struct kvm_vcpu *vcpu)
5089{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02005090 kvm_mmu_free_roots(vcpu, &vcpu->arch.root_mmu, KVM_MMU_ROOTS_ALL);
5091 WARN_ON(VALID_PAGE(vcpu->arch.root_mmu.root_hpa));
5092 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
5093 WARN_ON(VALID_PAGE(vcpu->arch.guest_mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005094}
Joerg Roedel4b161842010-09-10 17:31:03 +02005095EXPORT_SYMBOL_GPL(kvm_mmu_unload);
Avi Kivity09072da2007-05-01 14:16:52 +03005096
Avi Kivity4db35312007-11-21 15:28:32 +02005097static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Xiao Guangrong7c562522011-03-28 10:29:27 +08005098 struct kvm_mmu_page *sp, u64 *spte,
5099 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03005100{
Marcelo Tosatti30945382008-06-11 20:32:40 -03005101 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Joerg Roedel7e4e4052009-07-27 16:30:46 +02005102 ++vcpu->kvm->stat.mmu_pde_zapped;
5103 return;
Marcelo Tosatti30945382008-06-11 20:32:40 -03005104 }
Avi Kivity00284252007-05-01 16:53:31 +03005105
Avi Kivity4cee5762007-11-18 16:37:07 +02005106 ++vcpu->kvm->stat.mmu_pte_updated;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005107 vcpu->arch.mmu->update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03005108}
5109
Avi Kivity79539ce2007-11-21 02:06:21 +02005110static bool need_remote_flush(u64 old, u64 new)
5111{
5112 if (!is_shadow_present_pte(old))
5113 return false;
5114 if (!is_shadow_present_pte(new))
5115 return true;
5116 if ((old ^ new) & PT64_BASE_ADDR_MASK)
5117 return true;
Gleb Natapov53166222013-08-05 11:07:14 +03005118 old ^= shadow_nx_mask;
5119 new ^= shadow_nx_mask;
Avi Kivity79539ce2007-11-21 02:06:21 +02005120 return (old & ~new & PT64_PERM_MASK) != 0;
5121}
5122
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005123static u64 mmu_pte_write_fetch_gpte(struct kvm_vcpu *vcpu, gpa_t *gpa,
Junaid Shahid0e0fee52018-10-31 14:53:57 -07005124 int *bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08005125{
Junaid Shahid0e0fee52018-10-31 14:53:57 -07005126 u64 gentry = 0;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005127 int r;
Avi Kivity72016f32010-03-15 13:59:53 +02005128
Avi Kivity08e850c2010-03-15 13:59:57 +02005129 /*
5130 * Assume that the pte write on a page table of the same type
Xiao Guangrong49b26e22011-03-04 19:00:00 +08005131 * as the current vcpu paging mode since we update the sptes only
5132 * when they have the same mode.
Avi Kivity08e850c2010-03-15 13:59:57 +02005133 */
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005134 if (is_pae(vcpu) && *bytes == 4) {
Avi Kivity08e850c2010-03-15 13:59:57 +02005135 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005136 *gpa &= ~(gpa_t)7;
5137 *bytes = 8;
Avi Kivity08e850c2010-03-15 13:59:57 +02005138 }
5139
Junaid Shahid0e0fee52018-10-31 14:53:57 -07005140 if (*bytes == 4 || *bytes == 8) {
5141 r = kvm_vcpu_read_guest_atomic(vcpu, *gpa, &gentry, *bytes);
5142 if (r)
5143 gentry = 0;
Avi Kivity72016f32010-03-15 13:59:53 +02005144 }
5145
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005146 return gentry;
5147}
5148
5149/*
5150 * If we're seeing too many writes to a page, it may no longer be a page table,
5151 * or we may be forking, in which case it is better to unmap the page.
5152 */
Xiao Guangronga138fe72011-12-16 18:18:10 +08005153static bool detect_write_flooding(struct kvm_mmu_page *sp)
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005154{
Xiao Guangronga30f47c2011-09-22 16:58:36 +08005155 /*
5156 * Skip write-flooding detected for the sp whose level is 1, because
5157 * it can become unsync, then the guest page is not write-protected.
5158 */
Davidlohr Buesof71fa312012-04-18 12:24:29 +02005159 if (sp->role.level == PT_PAGE_TABLE_LEVEL)
Xiao Guangronga30f47c2011-09-22 16:58:36 +08005160 return false;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005161
Xiao Guangronge5691a82016-02-24 17:51:12 +08005162 atomic_inc(&sp->write_flooding_count);
5163 return atomic_read(&sp->write_flooding_count) >= 3;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005164}
5165
5166/*
5167 * Misaligned accesses are too much trouble to fix up; also, they usually
5168 * indicate a page is not used as a page table.
5169 */
5170static bool detect_write_misaligned(struct kvm_mmu_page *sp, gpa_t gpa,
5171 int bytes)
5172{
5173 unsigned offset, pte_size, misaligned;
5174
5175 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
5176 gpa, bytes, sp->role.word);
5177
5178 offset = offset_in_page(gpa);
5179 pte_size = sp->role.cr4_pae ? 8 : 4;
Xiao Guangrong5d9ca302011-09-22 16:57:55 +08005180
5181 /*
5182 * Sometimes, the OS only writes the last one bytes to update status
5183 * bits, for example, in linux, andb instruction is used in clear_bit().
5184 */
5185 if (!(offset & (pte_size - 1)) && bytes == 1)
5186 return false;
5187
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005188 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
5189 misaligned |= bytes < 4;
5190
5191 return misaligned;
5192}
5193
5194static u64 *get_written_sptes(struct kvm_mmu_page *sp, gpa_t gpa, int *nspte)
5195{
5196 unsigned page_offset, quadrant;
5197 u64 *spte;
5198 int level;
5199
5200 page_offset = offset_in_page(gpa);
5201 level = sp->role.level;
5202 *nspte = 1;
5203 if (!sp->role.cr4_pae) {
5204 page_offset <<= 1; /* 32->64 */
5205 /*
5206 * A 32-bit pde maps 4MB while the shadow pdes map
5207 * only 2MB. So we need to double the offset again
5208 * and zap two pdes instead of one.
5209 */
5210 if (level == PT32_ROOT_LEVEL) {
5211 page_offset &= ~7; /* kill rounding error */
5212 page_offset <<= 1;
5213 *nspte = 2;
5214 }
5215 quadrant = page_offset >> PAGE_SHIFT;
5216 page_offset &= ~PAGE_MASK;
5217 if (quadrant != sp->role.quadrant)
5218 return NULL;
5219 }
5220
5221 spte = &sp->spt[page_offset / sizeof(*spte)];
5222 return spte;
5223}
5224
Xiao Guangrong13d268c2016-02-24 17:51:16 +08005225static void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Jike Songd1263632016-10-25 15:50:42 +08005226 const u8 *new, int bytes,
5227 struct kvm_page_track_notifier_node *node)
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005228{
5229 gfn_t gfn = gpa >> PAGE_SHIFT;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005230 struct kvm_mmu_page *sp;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005231 LIST_HEAD(invalid_list);
5232 u64 entry, gentry, *spte;
5233 int npte;
Paolo Bonzinib8c67b72016-02-24 11:21:55 +01005234 bool remote_flush, local_flush;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005235
5236 /*
5237 * If we don't have indirect shadow pages, it means no page is
5238 * write-protected, so we can exit simply.
5239 */
Mark Rutland6aa7de02017-10-23 14:07:29 -07005240 if (!READ_ONCE(vcpu->kvm->arch.indirect_shadow_pages))
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005241 return;
5242
Paolo Bonzinib8c67b72016-02-24 11:21:55 +01005243 remote_flush = local_flush = false;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005244
5245 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
5246
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005247 /*
5248 * No need to care whether allocation memory is successful
5249 * or not since pte prefetch is skiped if it does not have
5250 * enough objects in the cache.
5251 */
5252 mmu_topup_memory_caches(vcpu);
5253
5254 spin_lock(&vcpu->kvm->mmu_lock);
Junaid Shahid0e0fee52018-10-31 14:53:57 -07005255
5256 gentry = mmu_pte_write_fetch_gpte(vcpu, &gpa, &bytes);
5257
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005258 ++vcpu->kvm->stat.mmu_pte_write;
Xiao Guangrong0375f7f2011-11-28 20:41:00 +08005259 kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE);
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005260
Sasha Levinb67bfe02013-02-27 17:06:00 -08005261 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn) {
Xiao Guangronga30f47c2011-09-22 16:58:36 +08005262 if (detect_write_misaligned(sp, gpa, bytes) ||
Xiao Guangronga138fe72011-12-16 18:18:10 +08005263 detect_write_flooding(sp)) {
Paolo Bonzinib8c67b72016-02-24 11:21:55 +01005264 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02005265 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08005266 continue;
5267 }
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005268
5269 spte = get_written_sptes(sp, gpa, &npte);
5270 if (!spte)
5271 continue;
5272
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08005273 local_flush = true;
Avi Kivityac1b7142007-03-08 17:13:32 +02005274 while (npte--) {
Vitaly Kuznetsov36d9594d2018-10-08 21:28:10 +02005275 u32 base_role = vcpu->arch.mmu->mmu_role.base.word;
5276
Avi Kivity79539ce2007-11-21 02:06:21 +02005277 entry = *spte;
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08005278 mmu_page_zap_pte(vcpu->kvm, sp, spte);
Xiao Guangrongfa1de2b2010-07-16 11:19:51 +08005279 if (gentry &&
Vitaly Kuznetsov36d9594d2018-10-08 21:28:10 +02005280 !((sp->role.word ^ base_role)
Junaid Shahid9fa72112018-06-27 14:59:07 -07005281 & mmu_base_role_mask.word) && rmap_can_add(vcpu))
Xiao Guangrong7c562522011-03-28 10:29:27 +08005282 mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
Gleb Natapov9bb4f6b2013-01-30 16:45:01 +02005283 if (need_remote_flush(entry, *spte))
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08005284 remote_flush = true;
Avi Kivityac1b7142007-03-08 17:13:32 +02005285 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08005286 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08005287 }
Paolo Bonzinib8c67b72016-02-24 11:21:55 +01005288 kvm_mmu_flush_or_zap(vcpu, &invalid_list, remote_flush, local_flush);
Xiao Guangrong0375f7f2011-11-28 20:41:00 +08005289 kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05005290 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivityda4a00f2007-01-05 16:36:44 -08005291}
5292
Avi Kivitya4360362007-01-05 16:36:45 -08005293int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
5294{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005295 gpa_t gpa;
5296 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08005297
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005298 if (vcpu->arch.mmu->direct_map)
Avi Kivity60f24782009-08-27 13:37:06 +03005299 return 0;
5300
Gleb Natapov1871c602010-02-10 14:21:32 +02005301 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005302
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005303 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005304
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005305 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08005306}
Avi Kivity577bdc42008-07-19 08:57:05 +03005307EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08005308
Wanpeng Li26eeb532017-08-10 16:28:02 -07005309static int make_mmu_pages_available(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08005310{
Xiao Guangrongd98ba052010-06-04 21:55:29 +08005311 LIST_HEAD(invalid_list);
Xiao Guangrong103ad252010-06-04 21:54:38 +08005312
Takuya Yoshikawa81f4f762013-03-21 19:34:27 +09005313 if (likely(kvm_mmu_available_pages(vcpu->kvm) >= KVM_MIN_FREE_MMU_PAGES))
Wanpeng Li26eeb532017-08-10 16:28:02 -07005314 return 0;
Takuya Yoshikawa81f4f762013-03-21 19:34:27 +09005315
Takuya Yoshikawa5da59602013-03-06 16:06:58 +09005316 while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES) {
5317 if (!prepare_zap_oldest_mmu_page(vcpu->kvm, &invalid_list))
5318 break;
Avi Kivityebeace82007-01-05 16:36:47 -08005319
Avi Kivity4cee5762007-11-18 16:37:07 +02005320 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08005321 }
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08005322 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
Wanpeng Li26eeb532017-08-10 16:28:02 -07005323
5324 if (!kvm_mmu_available_pages(vcpu->kvm))
5325 return -ENOSPC;
5326 return 0;
Avi Kivityebeace82007-01-05 16:36:47 -08005327}
Avi Kivityebeace82007-01-05 16:36:47 -08005328
Tom Lendacky14727752016-11-23 12:01:38 -05005329int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u64 error_code,
Andre Przywaradc25e892010-12-21 11:12:07 +01005330 void *insn, int insn_len)
Avi Kivity30677142007-10-28 18:48:59 +02005331{
Sean Christopherson472faff2018-08-23 13:56:50 -07005332 int r, emulation_type = 0;
Avi Kivity30677142007-10-28 18:48:59 +02005333 enum emulation_result er;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005334 bool direct = vcpu->arch.mmu->direct_map;
Avi Kivity30677142007-10-28 18:48:59 +02005335
Brijesh Singh618232e2017-08-17 18:36:57 +02005336 /* With shadow page tables, fault_address contains a GVA or nGPA. */
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005337 if (vcpu->arch.mmu->direct_map) {
Brijesh Singh618232e2017-08-17 18:36:57 +02005338 vcpu->arch.gpa_available = true;
5339 vcpu->arch.gpa_val = cr2;
5340 }
Avi Kivity30677142007-10-28 18:48:59 +02005341
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02005342 r = RET_PF_INVALID;
Takuya Yoshikawae9ee9562016-02-22 17:23:41 +09005343 if (unlikely(error_code & PFERR_RSVD_MASK)) {
5344 r = handle_mmio_page_fault(vcpu, cr2, direct);
Sean Christopherson472faff2018-08-23 13:56:50 -07005345 if (r == RET_PF_EMULATE)
Takuya Yoshikawae9ee9562016-02-22 17:23:41 +09005346 goto emulate;
Takuya Yoshikawae9ee9562016-02-22 17:23:41 +09005347 }
Avi Kivity30677142007-10-28 18:48:59 +02005348
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02005349 if (r == RET_PF_INVALID) {
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005350 r = vcpu->arch.mmu->page_fault(vcpu, cr2,
5351 lower_32_bits(error_code),
5352 false);
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02005353 WARN_ON(r == RET_PF_INVALID);
5354 }
5355
5356 if (r == RET_PF_RETRY)
5357 return 1;
Avi Kivity30677142007-10-28 18:48:59 +02005358 if (r < 0)
Takuya Yoshikawae9ee9562016-02-22 17:23:41 +09005359 return r;
Avi Kivity30677142007-10-28 18:48:59 +02005360
Tom Lendacky14727752016-11-23 12:01:38 -05005361 /*
5362 * Before emulating the instruction, check if the error code
5363 * was due to a RO violation while translating the guest page.
5364 * This can occur when using nested virtualization with nested
5365 * paging in both guests. If true, we simply unprotect the page
5366 * and resume the guest.
Tom Lendacky14727752016-11-23 12:01:38 -05005367 */
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005368 if (vcpu->arch.mmu->direct_map &&
Paolo Bonzinieebed242016-11-28 14:39:58 +01005369 (error_code & PFERR_NESTED_GUEST_PAGE) == PFERR_NESTED_GUEST_PAGE) {
Tom Lendacky14727752016-11-23 12:01:38 -05005370 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(cr2));
5371 return 1;
5372 }
5373
Sean Christopherson472faff2018-08-23 13:56:50 -07005374 /*
5375 * vcpu->arch.mmu.page_fault returned RET_PF_EMULATE, but we can still
5376 * optimistically try to just unprotect the page and let the processor
5377 * re-execute the instruction that caused the page fault. Do not allow
5378 * retrying MMIO emulation, as it's not only pointless but could also
5379 * cause us to enter an infinite loop because the processor will keep
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07005380 * faulting on the non-existent MMIO address. Retrying an instruction
5381 * from a nested guest is also pointless and dangerous as we are only
5382 * explicitly shadowing L1's page tables, i.e. unprotecting something
5383 * for L1 isn't going to magically fix whatever issue cause L2 to fail.
Sean Christopherson472faff2018-08-23 13:56:50 -07005384 */
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07005385 if (!mmio_info_in_cache(vcpu, cr2, direct) && !is_guest_mode(vcpu))
Sean Christopherson472faff2018-08-23 13:56:50 -07005386 emulation_type = EMULTYPE_ALLOW_RETRY;
Takuya Yoshikawae9ee9562016-02-22 17:23:41 +09005387emulate:
Brijesh Singh00b10fe2017-12-04 10:57:40 -06005388 /*
5389 * On AMD platforms, under certain conditions insn_len may be zero on #NPF.
5390 * This can happen if a guest gets a page-fault on data access but the HW
5391 * table walker is not able to read the instruction page (e.g instruction
5392 * page is not present in memory). In those cases we simply restart the
5393 * guest.
5394 */
5395 if (unlikely(insn && !insn_len))
5396 return 1;
5397
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005398 er = x86_emulate_instruction(vcpu, cr2, emulation_type, insn, insn_len);
Avi Kivity30677142007-10-28 18:48:59 +02005399
5400 switch (er) {
5401 case EMULATE_DONE:
5402 return 1;
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02005403 case EMULATE_USER_EXIT:
Avi Kivity30677142007-10-28 18:48:59 +02005404 ++vcpu->stat.mmio_exits;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005405 /* fall through */
Avi Kivity30677142007-10-28 18:48:59 +02005406 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03005407 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02005408 default:
5409 BUG();
5410 }
Avi Kivity30677142007-10-28 18:48:59 +02005411}
5412EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
5413
Marcelo Tosattia7052892008-09-23 13:18:35 -03005414void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
5415{
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005416 struct kvm_mmu *mmu = vcpu->arch.mmu;
Junaid Shahidb94742c2018-06-27 14:59:20 -07005417 int i;
Junaid Shahid7eb77e92018-06-27 14:59:16 -07005418
Junaid Shahidfaff8752018-06-29 13:10:05 -07005419 /* INVLPG on a * non-canonical address is a NOP according to the SDM. */
5420 if (is_noncanonical_address(gva, vcpu))
5421 return;
5422
Junaid Shahid7eb77e92018-06-27 14:59:16 -07005423 mmu->invlpg(vcpu, gva, mmu->root_hpa);
Junaid Shahid956bf352018-06-27 14:59:18 -07005424
5425 /*
5426 * INVLPG is required to invalidate any global mappings for the VA,
5427 * irrespective of PCID. Since it would take us roughly similar amount
Junaid Shahidb94742c2018-06-27 14:59:20 -07005428 * of work to determine whether any of the prev_root mappings of the VA
5429 * is marked global, or to just sync it blindly, so we might as well
5430 * just always sync it.
Junaid Shahid956bf352018-06-27 14:59:18 -07005431 *
Junaid Shahidb94742c2018-06-27 14:59:20 -07005432 * Mappings not reachable via the current cr3 or the prev_roots will be
5433 * synced when switching to that cr3, so nothing needs to be done here
5434 * for them.
Junaid Shahid956bf352018-06-27 14:59:18 -07005435 */
Junaid Shahidb94742c2018-06-27 14:59:20 -07005436 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
5437 if (VALID_PAGE(mmu->prev_roots[i].hpa))
5438 mmu->invlpg(vcpu, gva, mmu->prev_roots[i].hpa);
Junaid Shahid956bf352018-06-27 14:59:18 -07005439
Junaid Shahidfaff8752018-06-29 13:10:05 -07005440 kvm_x86_ops->tlb_flush_gva(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005441 ++vcpu->stat.invlpg;
5442}
5443EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
5444
Junaid Shahideb4b2482018-06-27 14:59:14 -07005445void kvm_mmu_invpcid_gva(struct kvm_vcpu *vcpu, gva_t gva, unsigned long pcid)
5446{
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005447 struct kvm_mmu *mmu = vcpu->arch.mmu;
Junaid Shahidfaff8752018-06-29 13:10:05 -07005448 bool tlb_flush = false;
Junaid Shahidb94742c2018-06-27 14:59:20 -07005449 uint i;
Junaid Shahideb4b2482018-06-27 14:59:14 -07005450
5451 if (pcid == kvm_get_active_pcid(vcpu)) {
Junaid Shahid7eb77e92018-06-27 14:59:16 -07005452 mmu->invlpg(vcpu, gva, mmu->root_hpa);
Junaid Shahidfaff8752018-06-29 13:10:05 -07005453 tlb_flush = true;
Junaid Shahideb4b2482018-06-27 14:59:14 -07005454 }
5455
Junaid Shahidb94742c2018-06-27 14:59:20 -07005456 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
5457 if (VALID_PAGE(mmu->prev_roots[i].hpa) &&
5458 pcid == kvm_get_pcid(vcpu, mmu->prev_roots[i].cr3)) {
5459 mmu->invlpg(vcpu, gva, mmu->prev_roots[i].hpa);
5460 tlb_flush = true;
5461 }
Junaid Shahid956bf352018-06-27 14:59:18 -07005462 }
Junaid Shahidade61e22018-06-27 14:59:15 -07005463
Junaid Shahidfaff8752018-06-29 13:10:05 -07005464 if (tlb_flush)
5465 kvm_x86_ops->tlb_flush_gva(vcpu, gva);
5466
Junaid Shahideb4b2482018-06-27 14:59:14 -07005467 ++vcpu->stat.invlpg;
5468
5469 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07005470 * Mappings not reachable via the current cr3 or the prev_roots will be
5471 * synced when switching to that cr3, so nothing needs to be done here
5472 * for them.
Junaid Shahideb4b2482018-06-27 14:59:14 -07005473 */
5474}
5475EXPORT_SYMBOL_GPL(kvm_mmu_invpcid_gva);
5476
Joerg Roedel18552672008-02-07 13:47:41 +01005477void kvm_enable_tdp(void)
5478{
5479 tdp_enabled = true;
5480}
5481EXPORT_SYMBOL_GPL(kvm_enable_tdp);
5482
Joerg Roedel5f4cb662008-07-14 20:36:36 +02005483void kvm_disable_tdp(void)
5484{
5485 tdp_enabled = false;
5486}
5487EXPORT_SYMBOL_GPL(kvm_disable_tdp);
5488
Avi Kivity6aa8b732006-12-10 02:21:36 -08005489static void free_mmu_pages(struct kvm_vcpu *vcpu)
5490{
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005491 free_page((unsigned long)vcpu->arch.mmu->pae_root);
5492 free_page((unsigned long)vcpu->arch.mmu->lm_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005493}
5494
5495static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
5496{
Avi Kivity17ac10a2007-01-05 16:36:40 -08005497 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005498 int i;
5499
Liang Chenee6268b2018-07-25 16:32:14 +08005500 if (tdp_enabled)
5501 return 0;
5502
Avi Kivity17ac10a2007-01-05 16:36:40 -08005503 /*
5504 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
5505 * Therefore we need to allocate shadow page tables in the first
5506 * 4GB of memory, which happens to fit the DMA32 zone.
5507 */
5508 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
5509 if (!page)
Wei Yongjund7fa6ab2010-01-22 16:55:05 +08005510 return -ENOMEM;
5511
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005512 vcpu->arch.mmu->pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08005513 for (i = 0; i < 4; ++i)
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005514 vcpu->arch.mmu->pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08005515
Avi Kivity6aa8b732006-12-10 02:21:36 -08005516 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005517}
5518
Ingo Molnar8018c272006-12-29 16:50:01 -08005519int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005520{
Junaid Shahidb94742c2018-06-27 14:59:20 -07005521 uint i;
5522
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005523 vcpu->arch.mmu = &vcpu->arch.root_mmu;
5524 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005525
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005526 vcpu->arch.root_mmu.root_hpa = INVALID_PAGE;
Vitaly Kuznetsovad7dc692019-02-22 17:45:01 +01005527 vcpu->arch.root_mmu.root_cr3 = 0;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005528 vcpu->arch.root_mmu.translate_gpa = translate_gpa;
Junaid Shahidb94742c2018-06-27 14:59:20 -07005529 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005530 vcpu->arch.root_mmu.prev_roots[i] = KVM_MMU_ROOT_INFO_INVALID;
Junaid Shahidb94742c2018-06-27 14:59:20 -07005531
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02005532 vcpu->arch.guest_mmu.root_hpa = INVALID_PAGE;
Vitaly Kuznetsovad7dc692019-02-22 17:45:01 +01005533 vcpu->arch.guest_mmu.root_cr3 = 0;
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02005534 vcpu->arch.guest_mmu.translate_gpa = translate_gpa;
5535 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
5536 vcpu->arch.guest_mmu.prev_roots[i] = KVM_MMU_ROOT_INFO_INVALID;
5537
5538 vcpu->arch.nested_mmu.translate_gpa = translate_nested_gpa;
Ingo Molnar8018c272006-12-29 16:50:01 -08005539 return alloc_mmu_pages(vcpu);
5540}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005541
Xiaoguang Chenb5f5fdc2016-10-09 15:41:44 +08005542static void kvm_mmu_invalidate_zap_pages_in_memslot(struct kvm *kvm,
Jike Songd1263632016-10-25 15:50:42 +08005543 struct kvm_memory_slot *slot,
5544 struct kvm_page_track_notifier_node *node)
Xiaoguang Chenb5f5fdc2016-10-09 15:41:44 +08005545{
5546 kvm_mmu_invalidate_zap_all_pages(kvm);
5547}
5548
Xiao Guangrong13d268c2016-02-24 17:51:16 +08005549void kvm_mmu_init_vm(struct kvm *kvm)
5550{
5551 struct kvm_page_track_notifier_node *node = &kvm->arch.mmu_sp_tracker;
5552
5553 node->track_write = kvm_mmu_pte_write;
Xiaoguang Chenb5f5fdc2016-10-09 15:41:44 +08005554 node->track_flush_slot = kvm_mmu_invalidate_zap_pages_in_memslot;
Xiao Guangrong13d268c2016-02-24 17:51:16 +08005555 kvm_page_track_register_notifier(kvm, node);
5556}
5557
5558void kvm_mmu_uninit_vm(struct kvm *kvm)
5559{
5560 struct kvm_page_track_notifier_node *node = &kvm->arch.mmu_sp_tracker;
5561
5562 kvm_page_track_unregister_notifier(kvm, node);
5563}
5564
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005565/* The return value indicates if tlb flush on all vcpus is needed. */
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09005566typedef bool (*slot_level_handler) (struct kvm *kvm, struct kvm_rmap_head *rmap_head);
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005567
5568/* The caller should hold mmu-lock before calling this function. */
David Woodhouse928a4c32018-02-10 23:39:24 +00005569static __always_inline bool
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005570slot_handle_level_range(struct kvm *kvm, struct kvm_memory_slot *memslot,
5571 slot_level_handler fn, int start_level, int end_level,
5572 gfn_t start_gfn, gfn_t end_gfn, bool lock_flush_tlb)
5573{
5574 struct slot_rmap_walk_iterator iterator;
5575 bool flush = false;
5576
5577 for_each_slot_rmap_range(memslot, start_level, end_level, start_gfn,
5578 end_gfn, &iterator) {
5579 if (iterator.rmap)
5580 flush |= fn(kvm, iterator.rmap);
5581
5582 if (need_resched() || spin_needbreak(&kvm->mmu_lock)) {
5583 if (flush && lock_flush_tlb) {
5584 kvm_flush_remote_tlbs(kvm);
5585 flush = false;
5586 }
5587 cond_resched_lock(&kvm->mmu_lock);
5588 }
5589 }
5590
5591 if (flush && lock_flush_tlb) {
5592 kvm_flush_remote_tlbs(kvm);
5593 flush = false;
5594 }
5595
5596 return flush;
5597}
5598
David Woodhouse928a4c32018-02-10 23:39:24 +00005599static __always_inline bool
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005600slot_handle_level(struct kvm *kvm, struct kvm_memory_slot *memslot,
5601 slot_level_handler fn, int start_level, int end_level,
5602 bool lock_flush_tlb)
5603{
5604 return slot_handle_level_range(kvm, memslot, fn, start_level,
5605 end_level, memslot->base_gfn,
5606 memslot->base_gfn + memslot->npages - 1,
5607 lock_flush_tlb);
5608}
5609
David Woodhouse928a4c32018-02-10 23:39:24 +00005610static __always_inline bool
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005611slot_handle_all_level(struct kvm *kvm, struct kvm_memory_slot *memslot,
5612 slot_level_handler fn, bool lock_flush_tlb)
5613{
5614 return slot_handle_level(kvm, memslot, fn, PT_PAGE_TABLE_LEVEL,
5615 PT_MAX_HUGEPAGE_LEVEL, lock_flush_tlb);
5616}
5617
David Woodhouse928a4c32018-02-10 23:39:24 +00005618static __always_inline bool
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005619slot_handle_large_level(struct kvm *kvm, struct kvm_memory_slot *memslot,
5620 slot_level_handler fn, bool lock_flush_tlb)
5621{
5622 return slot_handle_level(kvm, memslot, fn, PT_PAGE_TABLE_LEVEL + 1,
5623 PT_MAX_HUGEPAGE_LEVEL, lock_flush_tlb);
5624}
5625
David Woodhouse928a4c32018-02-10 23:39:24 +00005626static __always_inline bool
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005627slot_handle_leaf(struct kvm *kvm, struct kvm_memory_slot *memslot,
5628 slot_level_handler fn, bool lock_flush_tlb)
5629{
5630 return slot_handle_level(kvm, memslot, fn, PT_PAGE_TABLE_LEVEL,
5631 PT_PAGE_TABLE_LEVEL, lock_flush_tlb);
5632}
5633
Xiao Guangrongefdfe532015-05-13 14:42:27 +08005634void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end)
5635{
5636 struct kvm_memslots *slots;
5637 struct kvm_memory_slot *memslot;
Lan Tianyu71883a62018-12-06 21:21:13 +08005638 bool flush_tlb = true;
5639 bool flush = false;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02005640 int i;
Xiao Guangrongefdfe532015-05-13 14:42:27 +08005641
Lan Tianyu71883a62018-12-06 21:21:13 +08005642 if (kvm_available_flush_tlb_with_range())
5643 flush_tlb = false;
5644
Xiao Guangrongefdfe532015-05-13 14:42:27 +08005645 spin_lock(&kvm->mmu_lock);
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02005646 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
5647 slots = __kvm_memslots(kvm, i);
5648 kvm_for_each_memslot(memslot, slots) {
5649 gfn_t start, end;
Xiao Guangrongefdfe532015-05-13 14:42:27 +08005650
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02005651 start = max(gfn_start, memslot->base_gfn);
5652 end = min(gfn_end, memslot->base_gfn + memslot->npages);
5653 if (start >= end)
5654 continue;
Xiao Guangrongefdfe532015-05-13 14:42:27 +08005655
Lan Tianyu71883a62018-12-06 21:21:13 +08005656 flush |= slot_handle_level_range(kvm, memslot,
5657 kvm_zap_rmapp, PT_PAGE_TABLE_LEVEL,
5658 PT_MAX_HUGEPAGE_LEVEL, start,
5659 end - 1, flush_tlb);
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02005660 }
Xiao Guangrongefdfe532015-05-13 14:42:27 +08005661 }
5662
Lan Tianyu71883a62018-12-06 21:21:13 +08005663 if (flush)
5664 kvm_flush_remote_tlbs_with_address(kvm, gfn_start,
5665 gfn_end - gfn_start + 1);
5666
Xiao Guangrongefdfe532015-05-13 14:42:27 +08005667 spin_unlock(&kvm->mmu_lock);
5668}
5669
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09005670static bool slot_rmap_write_protect(struct kvm *kvm,
5671 struct kvm_rmap_head *rmap_head)
Xiao Guangrongd77aa732015-05-13 14:42:24 +08005672{
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09005673 return __rmap_write_protect(kvm, rmap_head, false);
Xiao Guangrongd77aa732015-05-13 14:42:24 +08005674}
5675
Kai Huang1c91cad42015-01-28 10:54:26 +08005676void kvm_mmu_slot_remove_write_access(struct kvm *kvm,
5677 struct kvm_memory_slot *memslot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005678{
Xiao Guangrongd77aa732015-05-13 14:42:24 +08005679 bool flush;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005680
Takuya Yoshikawa9d1beef2013-01-08 19:46:48 +09005681 spin_lock(&kvm->mmu_lock);
Xiao Guangrongd77aa732015-05-13 14:42:24 +08005682 flush = slot_handle_all_level(kvm, memslot, slot_rmap_write_protect,
5683 false);
Takuya Yoshikawa9d1beef2013-01-08 19:46:48 +09005684 spin_unlock(&kvm->mmu_lock);
Xiao Guangrong198c74f2014-04-17 17:06:16 +08005685
5686 /*
5687 * kvm_mmu_slot_remove_write_access() and kvm_vm_ioctl_get_dirty_log()
5688 * which do tlb flush out of mmu-lock should be serialized by
5689 * kvm->slots_lock otherwise tlb flush would be missed.
5690 */
5691 lockdep_assert_held(&kvm->slots_lock);
5692
5693 /*
5694 * We can flush all the TLBs out of the mmu lock without TLB
5695 * corruption since we just change the spte from writable to
5696 * readonly so that we only need to care the case of changing
5697 * spte from present to present (changing the spte from present
5698 * to nonpresent will flush all the TLBs immediately), in other
5699 * words, the only case we care is mmu_spte_update() where we
Wei Yangbdd303c2018-11-05 14:45:03 +08005700 * have checked SPTE_HOST_WRITEABLE | SPTE_MMU_WRITEABLE
Xiao Guangrong198c74f2014-04-17 17:06:16 +08005701 * instead of PT_WRITABLE_MASK, that means it does not depend
5702 * on PT_WRITABLE_MASK anymore.
5703 */
Kai Huangd91ffee2015-01-12 15:28:54 +08005704 if (flush)
Lan Tianyuc3134ce2018-12-06 21:21:09 +08005705 kvm_flush_remote_tlbs_with_address(kvm, memslot->base_gfn,
5706 memslot->npages);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005707}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08005708
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005709static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm,
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09005710 struct kvm_rmap_head *rmap_head)
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005711{
5712 u64 *sptep;
5713 struct rmap_iterator iter;
5714 int need_tlb_flush = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005715 kvm_pfn_t pfn;
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005716 struct kvm_mmu_page *sp;
5717
Xiao Guangrong0d536792015-05-13 14:42:20 +08005718restart:
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09005719 for_each_rmap_spte(rmap_head, &iter, sptep) {
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005720 sp = page_header(__pa(sptep));
5721 pfn = spte_to_pfn(*sptep);
5722
5723 /*
Xiao Guangrongdecf6332015-04-14 12:04:10 +08005724 * We cannot do huge page mapping for indirect shadow pages,
5725 * which are found on the last rmap (level = 1) when not using
5726 * tdp; such shadow pages are synced with the page table in
5727 * the guest, and the guest page table is using 4K page size
5728 * mapping if the indirect sp has level = 1.
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005729 */
5730 if (sp->role.direct &&
5731 !kvm_is_reserved_pfn(pfn) &&
Andrea Arcangeli127393f2016-05-05 16:22:20 -07005732 PageTransCompoundMap(pfn_to_page(pfn))) {
Wei Yange7912382018-10-04 10:04:23 +08005733 pte_list_remove(rmap_head, sptep);
Lan Tianyu40ef75a2018-12-06 21:21:08 +08005734
5735 if (kvm_available_flush_tlb_with_range())
5736 kvm_flush_remote_tlbs_with_address(kvm, sp->gfn,
5737 KVM_PAGES_PER_HPAGE(sp->role.level));
5738 else
5739 need_tlb_flush = 1;
5740
Xiao Guangrong0d536792015-05-13 14:42:20 +08005741 goto restart;
5742 }
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005743 }
5744
5745 return need_tlb_flush;
5746}
5747
5748void kvm_mmu_zap_collapsible_sptes(struct kvm *kvm,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02005749 const struct kvm_memory_slot *memslot)
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005750{
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02005751 /* FIXME: const-ify all uses of struct kvm_memory_slot. */
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005752 spin_lock(&kvm->mmu_lock);
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02005753 slot_handle_leaf(kvm, (struct kvm_memory_slot *)memslot,
5754 kvm_mmu_zap_collapsible_spte, true);
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005755 spin_unlock(&kvm->mmu_lock);
5756}
5757
Kai Huangf4b4b182015-01-28 10:54:24 +08005758void kvm_mmu_slot_leaf_clear_dirty(struct kvm *kvm,
5759 struct kvm_memory_slot *memslot)
5760{
Xiao Guangrongd77aa732015-05-13 14:42:24 +08005761 bool flush;
Kai Huangf4b4b182015-01-28 10:54:24 +08005762
5763 spin_lock(&kvm->mmu_lock);
Xiao Guangrongd77aa732015-05-13 14:42:24 +08005764 flush = slot_handle_leaf(kvm, memslot, __rmap_clear_dirty, false);
Kai Huangf4b4b182015-01-28 10:54:24 +08005765 spin_unlock(&kvm->mmu_lock);
5766
5767 lockdep_assert_held(&kvm->slots_lock);
5768
5769 /*
5770 * It's also safe to flush TLBs out of mmu lock here as currently this
5771 * function is only used for dirty logging, in which case flushing TLB
5772 * out of mmu lock also guarantees no dirty pages will be lost in
5773 * dirty_bitmap.
5774 */
5775 if (flush)
Lan Tianyuc3134ce2018-12-06 21:21:09 +08005776 kvm_flush_remote_tlbs_with_address(kvm, memslot->base_gfn,
5777 memslot->npages);
Kai Huangf4b4b182015-01-28 10:54:24 +08005778}
5779EXPORT_SYMBOL_GPL(kvm_mmu_slot_leaf_clear_dirty);
5780
5781void kvm_mmu_slot_largepage_remove_write_access(struct kvm *kvm,
5782 struct kvm_memory_slot *memslot)
5783{
Xiao Guangrongd77aa732015-05-13 14:42:24 +08005784 bool flush;
Kai Huangf4b4b182015-01-28 10:54:24 +08005785
5786 spin_lock(&kvm->mmu_lock);
Xiao Guangrongd77aa732015-05-13 14:42:24 +08005787 flush = slot_handle_large_level(kvm, memslot, slot_rmap_write_protect,
5788 false);
Kai Huangf4b4b182015-01-28 10:54:24 +08005789 spin_unlock(&kvm->mmu_lock);
5790
5791 /* see kvm_mmu_slot_remove_write_access */
5792 lockdep_assert_held(&kvm->slots_lock);
5793
5794 if (flush)
Lan Tianyuc3134ce2018-12-06 21:21:09 +08005795 kvm_flush_remote_tlbs_with_address(kvm, memslot->base_gfn,
5796 memslot->npages);
Kai Huangf4b4b182015-01-28 10:54:24 +08005797}
5798EXPORT_SYMBOL_GPL(kvm_mmu_slot_largepage_remove_write_access);
5799
5800void kvm_mmu_slot_set_dirty(struct kvm *kvm,
5801 struct kvm_memory_slot *memslot)
5802{
Xiao Guangrongd77aa732015-05-13 14:42:24 +08005803 bool flush;
Kai Huangf4b4b182015-01-28 10:54:24 +08005804
5805 spin_lock(&kvm->mmu_lock);
Xiao Guangrongd77aa732015-05-13 14:42:24 +08005806 flush = slot_handle_all_level(kvm, memslot, __rmap_set_dirty, false);
Kai Huangf4b4b182015-01-28 10:54:24 +08005807 spin_unlock(&kvm->mmu_lock);
5808
5809 lockdep_assert_held(&kvm->slots_lock);
5810
5811 /* see kvm_mmu_slot_leaf_clear_dirty */
5812 if (flush)
Lan Tianyuc3134ce2018-12-06 21:21:09 +08005813 kvm_flush_remote_tlbs_with_address(kvm, memslot->base_gfn,
5814 memslot->npages);
Kai Huangf4b4b182015-01-28 10:54:24 +08005815}
5816EXPORT_SYMBOL_GPL(kvm_mmu_slot_set_dirty);
5817
Xiao Guangronge7d11c72013-05-31 08:36:27 +08005818#define BATCH_ZAP_PAGES 10
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08005819static void kvm_zap_obsolete_pages(struct kvm *kvm)
5820{
5821 struct kvm_mmu_page *sp, *node;
Xiao Guangronge7d11c72013-05-31 08:36:27 +08005822 int batch = 0;
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08005823
5824restart:
5825 list_for_each_entry_safe_reverse(sp, node,
5826 &kvm->arch.active_mmu_pages, link) {
Xiao Guangronge7d11c72013-05-31 08:36:27 +08005827 int ret;
5828
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08005829 /*
5830 * No obsolete page exists before new created page since
5831 * active_mmu_pages is the FIFO list.
5832 */
5833 if (!is_obsolete_sp(kvm, sp))
5834 break;
5835
5836 /*
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08005837 * Since we are reversely walking the list and the invalid
5838 * list will be moved to the head, skip the invalid page
5839 * can help us to avoid the infinity list walking.
5840 */
5841 if (sp->role.invalid)
5842 continue;
5843
Xiao Guangrongf34d2512013-05-31 08:36:28 +08005844 /*
5845 * Need not flush tlb since we only zap the sp with invalid
5846 * generation number.
5847 */
Xiao Guangronge7d11c72013-05-31 08:36:27 +08005848 if (batch >= BATCH_ZAP_PAGES &&
Xiao Guangrongf34d2512013-05-31 08:36:28 +08005849 cond_resched_lock(&kvm->mmu_lock)) {
Xiao Guangronge7d11c72013-05-31 08:36:27 +08005850 batch = 0;
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08005851 goto restart;
5852 }
5853
Xiao Guangrong365c8862013-05-31 08:36:29 +08005854 ret = kvm_mmu_prepare_zap_page(kvm, sp,
5855 &kvm->arch.zapped_obsolete_pages);
Xiao Guangronge7d11c72013-05-31 08:36:27 +08005856 batch += ret;
5857
5858 if (ret)
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08005859 goto restart;
5860 }
5861
Xiao Guangrongf34d2512013-05-31 08:36:28 +08005862 /*
5863 * Should flush tlb before free page tables since lockless-walking
5864 * may use the pages.
5865 */
Xiao Guangrong365c8862013-05-31 08:36:29 +08005866 kvm_mmu_commit_zap_page(kvm, &kvm->arch.zapped_obsolete_pages);
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08005867}
5868
5869/*
5870 * Fast invalidate all shadow pages and use lock-break technique
5871 * to zap obsolete pages.
5872 *
5873 * It's required when memslot is being deleted or VM is being
5874 * destroyed, in these cases, we should ensure that KVM MMU does
5875 * not use any resource of the being-deleted slot or all slots
5876 * after calling the function.
5877 */
5878void kvm_mmu_invalidate_zap_all_pages(struct kvm *kvm)
5879{
5880 spin_lock(&kvm->mmu_lock);
Xiao Guangrong35006122013-05-31 08:36:25 +08005881 trace_kvm_mmu_invalidate_zap_all_pages(kvm);
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08005882 kvm->arch.mmu_valid_gen++;
5883
Xiao Guangrongf34d2512013-05-31 08:36:28 +08005884 /*
5885 * Notify all vcpus to reload its shadow page table
5886 * and flush TLB. Then all vcpus will switch to new
5887 * shadow page table with the new mmu_valid_gen.
5888 *
5889 * Note: we should do this under the protection of
5890 * mmu-lock, otherwise, vcpu would purge shadow page
5891 * but miss tlb flush.
5892 */
5893 kvm_reload_remote_mmus(kvm);
5894
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08005895 kvm_zap_obsolete_pages(kvm);
5896 spin_unlock(&kvm->mmu_lock);
5897}
5898
Xiao Guangrong365c8862013-05-31 08:36:29 +08005899static bool kvm_has_zapped_obsolete_pages(struct kvm *kvm)
5900{
5901 return unlikely(!list_empty_careful(&kvm->arch.zapped_obsolete_pages));
5902}
5903
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005904void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm, struct kvm_memslots *slots)
Xiao Guangrongf8f55942013-06-07 16:51:26 +08005905{
5906 /*
5907 * The very rare case: if the generation-number is round,
5908 * zap all shadow pages.
Xiao Guangrongf8f55942013-06-07 16:51:26 +08005909 */
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005910 if (unlikely((slots->generation & MMIO_GEN_MASK) == 0)) {
Bandan Dasae0f5492016-11-15 01:36:18 -05005911 kvm_debug_ratelimited("kvm: zapping shadow pages for mmio generation wraparound\n");
Xiao Guangronga8eca9d2013-06-10 16:28:55 +08005912 kvm_mmu_invalidate_zap_all_pages(kvm);
Takuya Yoshikawa7a2e8aa2013-06-21 01:34:31 +09005913 }
Xiao Guangrongf8f55942013-06-07 16:51:26 +08005914}
5915
Dave Chinner70534a72013-08-28 10:18:14 +10005916static unsigned long
5917mmu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
Izik Eidus3ee16c82008-03-30 15:17:21 +03005918{
5919 struct kvm *kvm;
Ying Han1495f232011-05-24 17:12:27 -07005920 int nr_to_scan = sc->nr_to_scan;
Dave Chinner70534a72013-08-28 10:18:14 +10005921 unsigned long freed = 0;
Izik Eidus3ee16c82008-03-30 15:17:21 +03005922
Paolo Bonzini2f303b72013-09-25 13:53:07 +02005923 spin_lock(&kvm_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03005924
5925 list_for_each_entry(kvm, &vm_list, vm_list) {
Jan Kiszka3d56cbd2011-12-02 18:35:24 +01005926 int idx;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08005927 LIST_HEAD(invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03005928
Gleb Natapov19526392012-06-04 14:53:23 +03005929 /*
Takuya Yoshikawa35f2d162012-08-20 18:35:39 +09005930 * Never scan more than sc->nr_to_scan VM instances.
5931 * Will not hit this condition practically since we do not try
5932 * to shrink more than one VM and it is very unlikely to see
5933 * !n_used_mmu_pages so many times.
5934 */
5935 if (!nr_to_scan--)
5936 break;
5937 /*
Gleb Natapov19526392012-06-04 14:53:23 +03005938 * n_used_mmu_pages is accessed without holding kvm->mmu_lock
5939 * here. We may skip a VM instance errorneosly, but we do not
5940 * want to shrink a VM that only started to populate its MMU
5941 * anyway.
5942 */
Xiao Guangrong365c8862013-05-31 08:36:29 +08005943 if (!kvm->arch.n_used_mmu_pages &&
5944 !kvm_has_zapped_obsolete_pages(kvm))
Gleb Natapov19526392012-06-04 14:53:23 +03005945 continue;
Gleb Natapov19526392012-06-04 14:53:23 +03005946
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005947 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3ee16c82008-03-30 15:17:21 +03005948 spin_lock(&kvm->mmu_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03005949
Xiao Guangrong365c8862013-05-31 08:36:29 +08005950 if (kvm_has_zapped_obsolete_pages(kvm)) {
5951 kvm_mmu_commit_zap_page(kvm,
5952 &kvm->arch.zapped_obsolete_pages);
5953 goto unlock;
5954 }
5955
Dave Chinner70534a72013-08-28 10:18:14 +10005956 if (prepare_zap_oldest_mmu_page(kvm, &invalid_list))
5957 freed++;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08005958 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Gleb Natapov19526392012-06-04 14:53:23 +03005959
Xiao Guangrong365c8862013-05-31 08:36:29 +08005960unlock:
Izik Eidus3ee16c82008-03-30 15:17:21 +03005961 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005962 srcu_read_unlock(&kvm->srcu, idx);
Gleb Natapov19526392012-06-04 14:53:23 +03005963
Dave Chinner70534a72013-08-28 10:18:14 +10005964 /*
5965 * unfair on small ones
5966 * per-vm shrinkers cry out
5967 * sadness comes quickly
5968 */
Gleb Natapov19526392012-06-04 14:53:23 +03005969 list_move_tail(&kvm->vm_list, &vm_list);
5970 break;
Izik Eidus3ee16c82008-03-30 15:17:21 +03005971 }
Izik Eidus3ee16c82008-03-30 15:17:21 +03005972
Paolo Bonzini2f303b72013-09-25 13:53:07 +02005973 spin_unlock(&kvm_lock);
Dave Chinner70534a72013-08-28 10:18:14 +10005974 return freed;
Dave Chinner70534a72013-08-28 10:18:14 +10005975}
5976
5977static unsigned long
5978mmu_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
5979{
Dave Hansen45221ab2010-08-19 18:11:37 -07005980 return percpu_counter_read_positive(&kvm_total_used_mmu_pages);
Izik Eidus3ee16c82008-03-30 15:17:21 +03005981}
5982
5983static struct shrinker mmu_shrinker = {
Dave Chinner70534a72013-08-28 10:18:14 +10005984 .count_objects = mmu_shrink_count,
5985 .scan_objects = mmu_shrink_scan,
Izik Eidus3ee16c82008-03-30 15:17:21 +03005986 .seeks = DEFAULT_SEEKS * 10,
5987};
5988
Ingo Molnar2ddfd202008-05-22 10:37:48 +02005989static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03005990{
Tim Hansenc1bd7432017-10-07 23:15:23 -04005991 kmem_cache_destroy(pte_list_desc_cache);
5992 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03005993}
5994
5995int kvm_mmu_module_init(void)
5996{
Arnd Bergmannab271bd2018-01-10 17:26:59 +01005997 int ret = -ENOMEM;
5998
Vitaly Kuznetsov36d9594d2018-10-08 21:28:10 +02005999 /*
6000 * MMU roles use union aliasing which is, generally speaking, an
6001 * undefined behavior. However, we supposedly know how compilers behave
6002 * and the current status quo is unlikely to change. Guardians below are
6003 * supposed to let us know if the assumption becomes false.
6004 */
6005 BUILD_BUG_ON(sizeof(union kvm_mmu_page_role) != sizeof(u32));
6006 BUILD_BUG_ON(sizeof(union kvm_mmu_extended_role) != sizeof(u32));
6007 BUILD_BUG_ON(sizeof(union kvm_mmu_role) != sizeof(u64));
6008
Junaid Shahid28a1f3a2018-08-14 10:15:34 -07006009 kvm_mmu_reset_all_pte_masks();
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006010
Xiao Guangrong53c07b12011-05-15 23:26:20 +08006011 pte_list_desc_cache = kmem_cache_create("pte_list_desc",
6012 sizeof(struct pte_list_desc),
Shakeel Butt46bea482017-10-05 18:07:24 -07006013 0, SLAB_ACCOUNT, NULL);
Xiao Guangrong53c07b12011-05-15 23:26:20 +08006014 if (!pte_list_desc_cache)
Arnd Bergmannab271bd2018-01-10 17:26:59 +01006015 goto out;
Avi Kivityb5a33a72007-04-15 16:31:09 +03006016
Avi Kivityd3d25b02007-05-30 12:34:53 +03006017 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
6018 sizeof(struct kvm_mmu_page),
Shakeel Butt46bea482017-10-05 18:07:24 -07006019 0, SLAB_ACCOUNT, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03006020 if (!mmu_page_header_cache)
Arnd Bergmannab271bd2018-01-10 17:26:59 +01006021 goto out;
Avi Kivityd3d25b02007-05-30 12:34:53 +03006022
Tejun Heo908c7f12014-09-08 09:51:29 +09006023 if (percpu_counter_init(&kvm_total_used_mmu_pages, 0, GFP_KERNEL))
Arnd Bergmannab271bd2018-01-10 17:26:59 +01006024 goto out;
Wei Yongjun45bf21a2010-08-23 16:13:15 +08006025
Arnd Bergmannab271bd2018-01-10 17:26:59 +01006026 ret = register_shrinker(&mmu_shrinker);
6027 if (ret)
6028 goto out;
Izik Eidus3ee16c82008-03-30 15:17:21 +03006029
Avi Kivityb5a33a72007-04-15 16:31:09 +03006030 return 0;
6031
Arnd Bergmannab271bd2018-01-10 17:26:59 +01006032out:
Izik Eidus3ee16c82008-03-30 15:17:21 +03006033 mmu_destroy_caches();
Arnd Bergmannab271bd2018-01-10 17:26:59 +01006034 return ret;
Avi Kivityb5a33a72007-04-15 16:31:09 +03006035}
6036
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08006037/*
Peng Hao39337ad2018-10-04 11:45:00 -04006038 * Calculate mmu pages needed for kvm.
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08006039 */
6040unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
6041{
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08006042 unsigned int nr_mmu_pages;
6043 unsigned int nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02006044 struct kvm_memslots *slots;
Xiao Guangrongbe6ba0f2011-11-24 17:39:18 +08006045 struct kvm_memory_slot *memslot;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02006046 int i;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08006047
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02006048 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
6049 slots = __kvm_memslots(kvm, i);
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08006050
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02006051 kvm_for_each_memslot(memslot, slots)
6052 nr_pages += memslot->npages;
6053 }
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08006054
6055 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
6056 nr_mmu_pages = max(nr_mmu_pages,
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02006057 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08006058
6059 return nr_mmu_pages;
6060}
6061
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08006062void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
6063{
Paolo Bonzini95f93af2013-10-02 16:56:12 +02006064 kvm_mmu_unload(vcpu);
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08006065 free_mmu_pages(vcpu);
6066 mmu_free_memory_caches(vcpu);
Xiao Guangrongb034cf02010-12-23 16:08:35 +08006067}
6068
Xiao Guangrongb034cf02010-12-23 16:08:35 +08006069void kvm_mmu_module_exit(void)
6070{
6071 mmu_destroy_caches();
6072 percpu_counter_destroy(&kvm_total_used_mmu_pages);
6073 unregister_shrinker(&mmu_shrinker);
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08006074 mmu_audit_disable();
6075}