blob: 71a2ee75522486cef23b88828013640d528b19cc [file] [log] [blame]
Thomas Gleixner20c8ccb2019-06-04 10:11:32 +02001// SPDX-License-Identifier: GPL-2.0-only
Avi Kivity6aa8b732006-12-10 02:21:36 -08002/*
3 * Kernel-based Virtual Machine driver for Linux
4 *
5 * This module enables machines with Intel VT-x extensions to run virtual
6 * machines without emulation or binary translation.
7 *
8 * MMU support
9 *
10 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +020011 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080012 *
13 * Authors:
14 * Yaniv Kamay <yaniv@qumranet.com>
15 * Avi Kivity <avi@qumranet.com>
Avi Kivity6aa8b732006-12-10 02:21:36 -080016 */
Avi Kivity6aa8b732006-12-10 02:21:36 -080017
Gleb Natapovaf585b92010-10-14 11:22:46 +020018#include "irq.h"
彭浩(Richard)88197e62020-05-21 05:57:49 +000019#include "ioapic.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Sean Christopherson6ca9a6f2020-06-22 13:20:31 -070021#include "mmu_internal.h"
Ben Gardonfe5db272020-10-14 11:26:43 -070022#include "tdp_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"
Sean Christopherson2f728d62020-02-18 15:29:49 -080025#include "kvm_emulate.h"
Nadav Amit5f7dde72014-05-07 15:32:50 +030026#include "cpuid.h"
Paolo Bonzini5a9624a2020-10-16 10:29:37 -040027#include "spte.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
Avi Kivityedf88412007-12-16 11:02:48 +020029#include <linux/kvm_host.h>
Avi Kivitye4956062007-06-28 14:15:57 -040030#include <linux/types.h>
31#include <linux/string.h>
32#include <linux/mm.h>
33#include <linux/highmem.h>
Paul Gortmaker1767e932016-07-13 20:19:00 -040034#include <linux/moduleparam.h>
35#include <linux/export.h>
Izik Eidus448353c2007-11-26 14:08:14 +020036#include <linux/swap.h>
Marcelo Tosatti05da4552008-02-23 11:44:30 -030037#include <linux/hugetlb.h>
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050038#include <linux/compiler.h>
Marcelo Tosattibc6678a2009-12-23 14:35:21 -020039#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010041#include <linux/sched/signal.h>
Huang Yingbf998152010-05-31 14:28:19 +080042#include <linux/uaccess.h>
David Matlack114df302016-12-19 13:58:25 -080043#include <linux/hash.h>
Junaid Shahidf160c7b2016-12-06 16:46:16 -080044#include <linux/kern_levels.h>
Junaid Shahid1aa9b952019-11-04 20:26:00 +010045#include <linux/kthread.h>
Avi Kivitye4956062007-06-28 14:15:57 -040046
47#include <asm/page.h>
Ingo Molnareb243d12019-11-20 15:33:57 +010048#include <asm/memtype.h>
Avi Kivitye4956062007-06-28 14:15:57 -040049#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020050#include <asm/io.h>
Sean Christopherson4a986232021-03-09 14:42:07 -080051#include <asm/set_memory.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020052#include <asm/vmx.h>
Xiao Guangrong3d0c27a2016-02-24 17:51:11 +080053#include <asm/kvm_page_track.h>
Wanpeng Li1261bfa2017-07-13 18:30:40 -070054#include "trace.h"
Avi Kivitye4956062007-06-28 14:15:57 -040055
Paolo Bonzinib8e8c832019-11-04 12:22:02 +010056extern bool itlb_multihit_kvm_mitigation;
57
Shaokun Zhanga9d64962021-05-27 15:57:51 +080058int __read_mostly nx_huge_pages = -1;
Paolo Bonzini13fb5922019-11-13 15:47:06 +010059#ifdef CONFIG_PREEMPT_RT
60/* Recovery can cause latency spikes, disable it for PREEMPT_RT. */
61static uint __read_mostly nx_huge_pages_recovery_ratio = 0;
62#else
Junaid Shahid1aa9b952019-11-04 20:26:00 +010063static uint __read_mostly nx_huge_pages_recovery_ratio = 60;
Paolo Bonzini13fb5922019-11-13 15:47:06 +010064#endif
Paolo Bonzinib8e8c832019-11-04 12:22:02 +010065
66static int set_nx_huge_pages(const char *val, const struct kernel_param *kp);
Junaid Shahid1aa9b952019-11-04 20:26:00 +010067static int set_nx_huge_pages_recovery_ratio(const char *val, const struct kernel_param *kp);
Paolo Bonzinib8e8c832019-11-04 12:22:02 +010068
Joe Perchesd5d6c182020-10-03 17:18:07 -070069static const struct kernel_param_ops nx_huge_pages_ops = {
Paolo Bonzinib8e8c832019-11-04 12:22:02 +010070 .set = set_nx_huge_pages,
71 .get = param_get_bool,
72};
73
Joe Perchesd5d6c182020-10-03 17:18:07 -070074static const struct kernel_param_ops nx_huge_pages_recovery_ratio_ops = {
Junaid Shahid1aa9b952019-11-04 20:26:00 +010075 .set = set_nx_huge_pages_recovery_ratio,
76 .get = param_get_uint,
77};
78
Paolo Bonzinib8e8c832019-11-04 12:22:02 +010079module_param_cb(nx_huge_pages, &nx_huge_pages_ops, &nx_huge_pages, 0644);
80__MODULE_PARM_TYPE(nx_huge_pages, "bool");
Junaid Shahid1aa9b952019-11-04 20:26:00 +010081module_param_cb(nx_huge_pages_recovery_ratio, &nx_huge_pages_recovery_ratio_ops,
82 &nx_huge_pages_recovery_ratio, 0644);
83__MODULE_PARM_TYPE(nx_huge_pages_recovery_ratio, "uint");
Paolo Bonzinib8e8c832019-11-04 12:22:02 +010084
Sean Christopherson71fe7012020-03-20 14:28:28 -070085static bool __read_mostly force_flush_and_sync_on_reuse;
86module_param_named(flush_on_reuse, force_flush_and_sync_on_reuse, bool, 0644);
87
Joerg Roedel18552672008-02-07 13:47:41 +010088/*
89 * When setting this variable to true it enables Two-Dimensional-Paging
90 * where the hardware walks 2 page tables:
91 * 1. the guest-virtual to guest-physical
92 * 2. while doing 1. it walks guest-physical to host-physical
93 * If the hardware supports that we don't need to do shadow paging.
94 */
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050095bool tdp_enabled = false;
Joerg Roedel18552672008-02-07 13:47:41 +010096
Sean Christopherson1d92d2e2020-07-15 20:41:21 -070097static int max_huge_page_level __read_mostly;
Sean Christopherson83013052020-07-15 20:41:22 -070098static int max_tdp_level __read_mostly;
Sean Christopherson703c3352020-03-02 15:57:03 -080099
Xiao Guangrong8b1fe172010-08-30 18:22:53 +0800100enum {
101 AUDIT_PRE_PAGE_FAULT,
102 AUDIT_POST_PAGE_FAULT,
103 AUDIT_PRE_PTE_WRITE,
Xiao Guangrong69030742010-09-27 18:09:29 +0800104 AUDIT_POST_PTE_WRITE,
105 AUDIT_PRE_SYNC,
106 AUDIT_POST_SYNC
Xiao Guangrong8b1fe172010-08-30 18:22:53 +0800107};
108
Avi Kivity37a7d8b2007-01-05 16:36:56 -0800109#ifdef MMU_DEBUG
Paolo Bonzini5a9624a2020-10-16 10:29:37 -0400110bool dbg = 0;
Paolo Bonzinifa4a2c02013-10-02 16:56:16 +0200111module_param(dbg, bool, 0644);
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800112#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800113
Xiao Guangrong957ed9e2010-08-22 19:12:48 +0800114#define PTE_PREFETCH_NUM 8
115
Avi Kivity6aa8b732006-12-10 02:21:36 -0800116#define PT32_LEVEL_BITS 10
117
118#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400119 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800120
Joerg Roedele04da982009-07-27 16:30:45 +0200121#define PT32_LVL_OFFSET_MASK(level) \
122 (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
123 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800124
125#define PT32_INDEX(address, level)\
126 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
127
128
Avi Kivity6aa8b732006-12-10 02:21:36 -0800129#define PT32_BASE_ADDR_MASK PAGE_MASK
130#define PT32_DIR_BASE_ADDR_MASK \
131 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
Joerg Roedele04da982009-07-27 16:30:45 +0200132#define PT32_LVL_ADDR_MASK(level) \
133 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
134 * PT32_LEVEL_BITS))) - 1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800135
Avi Kivity90bb6fc2009-12-31 12:10:16 +0200136#include <trace/events/kvm.h>
137
Takuya Yoshikawa220f7732012-03-21 23:49:39 +0900138/* make pte_list_desc fit well in cache line */
139#define PTE_LIST_EXT 3
140
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800141struct pte_list_desc {
142 u64 *sptes[PTE_LIST_EXT];
143 struct pte_list_desc *more;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800144};
145
Avi Kivity2d111232008-12-25 14:39:47 +0200146struct kvm_shadow_walk_iterator {
147 u64 addr;
148 hpa_t shadow_addr;
Avi Kivity2d111232008-12-25 14:39:47 +0200149 u64 *sptep;
Xiao Guangrongdd3bfd52011-07-12 03:32:54 +0800150 int level;
Avi Kivity2d111232008-12-25 14:39:47 +0200151 unsigned index;
152};
153
Junaid Shahid7eb77e92018-06-27 14:59:16 -0700154#define for_each_shadow_entry_using_root(_vcpu, _root, _addr, _walker) \
155 for (shadow_walk_init_using_root(&(_walker), (_vcpu), \
156 (_root), (_addr)); \
157 shadow_walk_okay(&(_walker)); \
158 shadow_walk_next(&(_walker)))
159
160#define for_each_shadow_entry(_vcpu, _addr, _walker) \
Avi Kivity2d111232008-12-25 14:39:47 +0200161 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
162 shadow_walk_okay(&(_walker)); \
163 shadow_walk_next(&(_walker)))
164
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800165#define for_each_shadow_entry_lockless(_vcpu, _addr, _walker, spte) \
166 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
167 shadow_walk_okay(&(_walker)) && \
168 ({ spte = mmu_spte_get_lockless(_walker.sptep); 1; }); \
169 __shadow_walk_next(&(_walker), spte))
170
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800171static struct kmem_cache *pte_list_desc_cache;
Ben Gardon02c00b32020-10-14 20:26:44 +0200172struct kmem_cache *mmu_page_header_cache;
Dave Hansen45221ab2010-08-19 18:11:37 -0700173static struct percpu_counter kvm_total_used_mmu_pages;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300174
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800175static void mmu_spte_set(u64 *sptep, u64 spte);
Junaid Shahid9fa72112018-06-27 14:59:07 -0700176static union kvm_mmu_page_role
177kvm_mmu_calc_root_page_role(struct kvm_vcpu *vcpu);
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800178
Paolo Bonzini335e1922019-07-01 06:22:57 -0400179#define CREATE_TRACE_POINTS
180#include "mmutrace.h"
181
Lan Tianyu40ef75a2018-12-06 21:21:08 +0800182
183static inline bool kvm_available_flush_tlb_with_range(void)
184{
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700185 return kvm_x86_ops.tlb_remote_flush_with_range;
Lan Tianyu40ef75a2018-12-06 21:21:08 +0800186}
187
188static void kvm_flush_remote_tlbs_with_range(struct kvm *kvm,
189 struct kvm_tlb_range *range)
190{
191 int ret = -ENOTSUPP;
192
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700193 if (range && kvm_x86_ops.tlb_remote_flush_with_range)
Jason Baronb36464772021-01-14 22:27:56 -0500194 ret = static_call(kvm_x86_tlb_remote_flush_with_range)(kvm, range);
Lan Tianyu40ef75a2018-12-06 21:21:08 +0800195
196 if (ret)
197 kvm_flush_remote_tlbs(kvm);
198}
199
Ben Gardon2f2fad02020-10-14 20:26:45 +0200200void kvm_flush_remote_tlbs_with_address(struct kvm *kvm,
Lan Tianyu40ef75a2018-12-06 21:21:08 +0800201 u64 start_gfn, u64 pages)
202{
203 struct kvm_tlb_range range;
204
205 range.start_gfn = start_gfn;
206 range.pages = pages;
207
208 kvm_flush_remote_tlbs_with_range(kvm, &range);
209}
210
Ben Gardon8f79b062020-02-03 15:09:10 -0800211static void mark_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, u64 gfn,
212 unsigned int access)
213{
Sean Christophersonc236d962021-02-25 12:47:34 -0800214 u64 spte = make_mmio_spte(vcpu, gfn, access);
Ben Gardon8f79b062020-02-03 15:09:10 -0800215
Sean Christophersonc236d962021-02-25 12:47:34 -0800216 trace_mark_mmio_spte(sptep, gfn, spte);
217 mmu_spte_set(sptep, spte);
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800218}
219
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800220static gfn_t get_mmio_spte_gfn(u64 spte)
221{
Sean Christophersondaa07cb2018-09-25 13:20:00 -0700222 u64 gpa = spte & shadow_nonpresent_or_rsvd_lower_gfn_mask;
Junaid Shahid28a1f3a2018-08-14 10:15:34 -0700223
Paolo Bonzini8a967d62020-10-30 13:39:55 -0400224 gpa |= (spte >> SHADOW_NONPRESENT_OR_RSVD_MASK_LEN)
Junaid Shahid28a1f3a2018-08-14 10:15:34 -0700225 & shadow_nonpresent_or_rsvd_mask;
226
227 return gpa >> PAGE_SHIFT;
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800228}
229
230static unsigned get_mmio_spte_access(u64 spte)
231{
Sean Christopherson4af77152019-08-01 13:35:22 -0700232 return spte & shadow_mmio_access_mask;
Xiao Guangrongce88dec2011-07-12 03:33:44 +0800233}
234
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200235static bool check_mmio_spte(struct kvm_vcpu *vcpu, u64 spte)
Xiao Guangrongf8f55942013-06-07 16:51:26 +0800236{
Sean Christophersoncae7ed32019-02-05 13:01:16 -0800237 u64 kvm_gen, spte_gen, gen;
Xiao Guangrong089504c2013-06-07 16:51:27 +0800238
Sean Christophersoncae7ed32019-02-05 13:01:16 -0800239 gen = kvm_vcpu_memslots(vcpu)->generation;
240 if (unlikely(gen & KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS))
241 return false;
242
243 kvm_gen = gen & MMIO_SPTE_GEN_MASK;
Xiao Guangrong089504c2013-06-07 16:51:27 +0800244 spte_gen = get_mmio_spte_generation(spte);
245
246 trace_check_mmio_spte(spte, kvm_gen, spte_gen);
247 return likely(kvm_gen == spte_gen);
Xiao Guangrongf8f55942013-06-07 16:51:26 +0800248}
249
Mohammed Gamalcd313562020-07-10 17:48:04 +0200250static gpa_t translate_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
251 struct x86_exception *exception)
252{
Mohammed Gamalec7771a2020-07-10 17:48:05 +0200253 /* Check if guest physical address doesn't exceed guest maximum */
Sean Christophersondc465152020-09-24 12:42:49 -0700254 if (kvm_vcpu_is_illegal_gpa(vcpu, gpa)) {
Mohammed Gamalec7771a2020-07-10 17:48:05 +0200255 exception->error_code |= PFERR_RSVD_MASK;
256 return UNMAPPED_GVA;
257 }
258
Mohammed Gamalcd313562020-07-10 17:48:04 +0200259 return gpa;
260}
261
Avi Kivity6aa8b732006-12-10 02:21:36 -0800262static int is_cpuid_PSE36(void)
263{
264 return 1;
265}
266
Avi Kivity73b10872007-01-26 00:56:41 -0800267static int is_nx(struct kvm_vcpu *vcpu)
268{
Avi Kivityf6801df2010-01-21 15:31:50 +0200269 return vcpu->arch.efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800270}
271
Avi Kivityda9285212007-11-21 13:54:47 +0200272static gfn_t pse36_gfn_delta(u32 gpte)
273{
274 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
275
276 return (gpte & PT32_DIR_PSE36_MASK) << shift;
277}
278
Xiao Guangrong603e0652011-07-12 03:31:28 +0800279#ifdef CONFIG_X86_64
Avi Kivityd555c332009-06-10 14:24:23 +0300280static void __set_spte(u64 *sptep, u64 spte)
Avi Kivitye663ee62007-05-31 15:46:04 +0300281{
Nadav Amitb19ee2f2016-05-11 08:04:29 -0700282 WRITE_ONCE(*sptep, spte);
Avi Kivitye663ee62007-05-31 15:46:04 +0300283}
284
Xiao Guangrong603e0652011-07-12 03:31:28 +0800285static void __update_clear_spte_fast(u64 *sptep, u64 spte)
Avi Kivitya9221dd2010-06-06 14:48:06 +0300286{
Nadav Amitb19ee2f2016-05-11 08:04:29 -0700287 WRITE_ONCE(*sptep, spte);
Avi Kivitya9221dd2010-06-06 14:48:06 +0300288}
289
Xiao Guangrong603e0652011-07-12 03:31:28 +0800290static u64 __update_clear_spte_slow(u64 *sptep, u64 spte)
291{
292 return xchg(sptep, spte);
293}
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800294
295static u64 __get_spte_lockless(u64 *sptep)
296{
Mark Rutland6aa7de02017-10-23 14:07:29 -0700297 return READ_ONCE(*sptep);
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800298}
Xiao Guangrong603e0652011-07-12 03:31:28 +0800299#else
300union split_spte {
301 struct {
302 u32 spte_low;
303 u32 spte_high;
304 };
305 u64 spte;
306};
307
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800308static void count_spte_clear(u64 *sptep, u64 spte)
309{
Sean Christopherson57354682020-06-22 13:20:33 -0700310 struct kvm_mmu_page *sp = sptep_to_sp(sptep);
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800311
312 if (is_shadow_present_pte(spte))
313 return;
314
315 /* Ensure the spte is completely set before we increase the count */
316 smp_wmb();
317 sp->clear_spte_count++;
318}
319
Xiao Guangrong603e0652011-07-12 03:31:28 +0800320static void __set_spte(u64 *sptep, u64 spte)
321{
322 union split_spte *ssptep, sspte;
323
324 ssptep = (union split_spte *)sptep;
325 sspte = (union split_spte)spte;
326
327 ssptep->spte_high = sspte.spte_high;
328
329 /*
330 * If we map the spte from nonpresent to present, We should store
331 * the high bits firstly, then set present bit, so cpu can not
332 * fetch this spte while we are setting the spte.
333 */
334 smp_wmb();
335
Nadav Amitb19ee2f2016-05-11 08:04:29 -0700336 WRITE_ONCE(ssptep->spte_low, sspte.spte_low);
Xiao Guangrong603e0652011-07-12 03:31:28 +0800337}
338
339static void __update_clear_spte_fast(u64 *sptep, u64 spte)
340{
341 union split_spte *ssptep, sspte;
342
343 ssptep = (union split_spte *)sptep;
344 sspte = (union split_spte)spte;
345
Nadav Amitb19ee2f2016-05-11 08:04:29 -0700346 WRITE_ONCE(ssptep->spte_low, sspte.spte_low);
Xiao Guangrong603e0652011-07-12 03:31:28 +0800347
348 /*
349 * If we map the spte from present to nonpresent, we should clear
350 * present bit firstly to avoid vcpu fetch the old high bits.
351 */
352 smp_wmb();
353
354 ssptep->spte_high = sspte.spte_high;
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800355 count_spte_clear(sptep, spte);
Xiao Guangrong603e0652011-07-12 03:31:28 +0800356}
357
358static u64 __update_clear_spte_slow(u64 *sptep, u64 spte)
359{
360 union split_spte *ssptep, sspte, orig;
361
362 ssptep = (union split_spte *)sptep;
363 sspte = (union split_spte)spte;
364
365 /* xchg acts as a barrier before the setting of the high bits */
366 orig.spte_low = xchg(&ssptep->spte_low, sspte.spte_low);
Zhao Jin41bc3182011-09-19 12:19:51 +0800367 orig.spte_high = ssptep->spte_high;
368 ssptep->spte_high = sspte.spte_high;
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800369 count_spte_clear(sptep, spte);
Xiao Guangrong603e0652011-07-12 03:31:28 +0800370
371 return orig.spte;
372}
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800373
374/*
375 * The idea using the light way get the spte on x86_32 guest is from
Christoph Hellwig39656e82019-07-11 20:56:49 -0700376 * gup_get_pte (mm/gup.c).
Xiao Guangrongaccaefe2013-06-19 17:09:20 +0800377 *
378 * An spte tlb flush may be pending, because kvm_set_pte_rmapp
379 * coalesces them and we are running out of the MMU lock. Therefore
380 * we need to protect against in-progress updates of the spte.
381 *
382 * Reading the spte while an update is in progress may get the old value
383 * for the high part of the spte. The race is fine for a present->non-present
384 * change (because the high part of the spte is ignored for non-present spte),
385 * but for a present->present change we must reread the spte.
386 *
387 * All such changes are done in two steps (present->non-present and
388 * non-present->present), hence it is enough to count the number of
389 * present->non-present updates: if it changed while reading the spte,
390 * we might have hit the race. This is done using clear_spte_count.
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800391 */
392static u64 __get_spte_lockless(u64 *sptep)
393{
Sean Christopherson57354682020-06-22 13:20:33 -0700394 struct kvm_mmu_page *sp = sptep_to_sp(sptep);
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800395 union split_spte spte, *orig = (union split_spte *)sptep;
396 int count;
397
398retry:
399 count = sp->clear_spte_count;
400 smp_rmb();
401
402 spte.spte_low = orig->spte_low;
403 smp_rmb();
404
405 spte.spte_high = orig->spte_high;
406 smp_rmb();
407
408 if (unlikely(spte.spte_low != orig->spte_low ||
409 count != sp->clear_spte_count))
410 goto retry;
411
412 return spte.spte;
413}
Xiao Guangrong603e0652011-07-12 03:31:28 +0800414#endif
415
Xiao Guangrong8672b722010-08-02 16:14:04 +0800416static bool spte_has_volatile_bits(u64 spte)
417{
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800418 if (!is_shadow_present_pte(spte))
419 return false;
420
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +0800421 /*
Adam Buchbinder6a6256f2016-02-23 15:34:30 -0800422 * Always atomically update spte if it can be updated
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +0800423 * out of mmu-lock, it can ensure dirty bit is not lost,
424 * also, it can help us to get a stable is_writable_pte()
425 * to ensure tlb flush is not missed.
426 */
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800427 if (spte_can_locklessly_be_made_writable(spte) ||
428 is_access_track_spte(spte))
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +0800429 return true;
430
Peter Feinerac8d57e2017-06-30 17:26:31 -0700431 if (spte_ad_enabled(spte)) {
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800432 if ((spte & shadow_accessed_mask) == 0 ||
433 (is_writable_pte(spte) && (spte & shadow_dirty_mask) == 0))
434 return true;
435 }
Xiao Guangrong8672b722010-08-02 16:14:04 +0800436
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800437 return false;
Xiao Guangrong8672b722010-08-02 16:14:04 +0800438}
439
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800440/* Rules for using mmu_spte_set:
441 * Set the sptep from nonpresent to present.
442 * Note: the sptep being assigned *must* be either not present
443 * or in a state where the hardware will not attempt to update
444 * the spte.
445 */
446static void mmu_spte_set(u64 *sptep, u64 new_spte)
447{
448 WARN_ON(is_shadow_present_pte(*sptep));
449 __set_spte(sptep, new_spte);
450}
451
Junaid Shahidf39a0582016-12-06 16:46:14 -0800452/*
453 * Update the SPTE (excluding the PFN), but do not track changes in its
454 * accessed/dirty status.
455 */
456static u64 mmu_spte_update_no_track(u64 *sptep, u64 new_spte)
457{
458 u64 old_spte = *sptep;
459
460 WARN_ON(!is_shadow_present_pte(new_spte));
461
462 if (!is_shadow_present_pte(old_spte)) {
463 mmu_spte_set(sptep, new_spte);
464 return old_spte;
465 }
466
467 if (!spte_has_volatile_bits(old_spte))
468 __update_clear_spte_fast(sptep, new_spte);
469 else
470 old_spte = __update_clear_spte_slow(sptep, new_spte);
471
472 WARN_ON(spte_to_pfn(old_spte) != spte_to_pfn(new_spte));
473
474 return old_spte;
475}
476
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800477/* Rules for using mmu_spte_update:
Andrea Gelminibb3541f2016-05-21 14:14:44 +0200478 * Update the state bits, it means the mapped pfn is not changed.
Xiao Guangrong6e7d0352012-06-20 15:58:33 +0800479 *
480 * Whenever we overwrite a writable spte with a read-only one we
481 * should flush remote TLBs. Otherwise rmap_write_protect
482 * will find a read-only spte, even though the writable spte
483 * might be cached on a CPU's TLB, the return value indicates this
484 * case.
Junaid Shahid83ef6c82016-12-06 16:46:13 -0800485 *
486 * Returns true if the TLB needs to be flushed
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800487 */
Xiao Guangrong6e7d0352012-06-20 15:58:33 +0800488static bool mmu_spte_update(u64 *sptep, u64 new_spte)
Avi Kivityb79b93f2010-06-06 15:46:44 +0300489{
Junaid Shahid83ef6c82016-12-06 16:46:13 -0800490 bool flush = false;
Junaid Shahidf39a0582016-12-06 16:46:14 -0800491 u64 old_spte = mmu_spte_update_no_track(sptep, new_spte);
Avi Kivityb79b93f2010-06-06 15:46:44 +0300492
Junaid Shahidf39a0582016-12-06 16:46:14 -0800493 if (!is_shadow_present_pte(old_spte))
494 return false;
Junaid Shahid83ef6c82016-12-06 16:46:13 -0800495
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +0800496 /*
497 * For the spte updated out of mmu-lock is safe, since
Adam Buchbinder6a6256f2016-02-23 15:34:30 -0800498 * we always atomically update it, see the comments in
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +0800499 * spte_has_volatile_bits().
500 */
Junaid Shahidea4114b2016-12-06 16:46:11 -0800501 if (spte_can_locklessly_be_made_writable(old_spte) &&
Xiao Guangrong7f31c952014-04-17 17:06:15 +0800502 !is_writable_pte(new_spte))
Junaid Shahid83ef6c82016-12-06 16:46:13 -0800503 flush = true;
Xiao Guangrong41327792010-08-02 16:15:08 +0800504
Kai Huang7e71a592015-01-09 16:44:30 +0800505 /*
Junaid Shahid83ef6c82016-12-06 16:46:13 -0800506 * Flush TLB when accessed/dirty states are changed in the page tables,
Kai Huang7e71a592015-01-09 16:44:30 +0800507 * to guarantee consistency between TLB and page tables.
508 */
Kai Huang7e71a592015-01-09 16:44:30 +0800509
Junaid Shahid83ef6c82016-12-06 16:46:13 -0800510 if (is_accessed_spte(old_spte) && !is_accessed_spte(new_spte)) {
511 flush = true;
Xiao Guangrong41327792010-08-02 16:15:08 +0800512 kvm_set_pfn_accessed(spte_to_pfn(old_spte));
Junaid Shahid83ef6c82016-12-06 16:46:13 -0800513 }
Xiao Guangrong6e7d0352012-06-20 15:58:33 +0800514
Junaid Shahid83ef6c82016-12-06 16:46:13 -0800515 if (is_dirty_spte(old_spte) && !is_dirty_spte(new_spte)) {
516 flush = true;
517 kvm_set_pfn_dirty(spte_to_pfn(old_spte));
518 }
519
520 return flush;
Avi Kivityb79b93f2010-06-06 15:46:44 +0300521}
522
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800523/*
524 * Rules for using mmu_spte_clear_track_bits:
525 * It sets the sptep from present to nonpresent, and track the
526 * state bits, it is used to clear the last level sptep.
Junaid Shahid83ef6c82016-12-06 16:46:13 -0800527 * Returns non-zero if the PTE was previously valid.
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800528 */
529static int mmu_spte_clear_track_bits(u64 *sptep)
530{
Dan Williamsba049e92016-01-15 16:56:11 -0800531 kvm_pfn_t pfn;
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800532 u64 old_spte = *sptep;
533
534 if (!spte_has_volatile_bits(old_spte))
Xiao Guangrong603e0652011-07-12 03:31:28 +0800535 __update_clear_spte_fast(sptep, 0ull);
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800536 else
Xiao Guangrong603e0652011-07-12 03:31:28 +0800537 old_spte = __update_clear_spte_slow(sptep, 0ull);
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800538
Takuya Yoshikawaafd28fe2015-11-20 17:44:55 +0900539 if (!is_shadow_present_pte(old_spte))
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800540 return 0;
541
542 pfn = spte_to_pfn(old_spte);
Xiao Guangrong86fde742012-07-17 21:52:52 +0800543
544 /*
545 * KVM does not hold the refcount of the page used by
546 * kvm mmu, before reclaiming the page, we should
547 * unmap it from mmu first.
548 */
Ard Biesheuvelbf4bea82014-11-10 08:33:56 +0000549 WARN_ON(!kvm_is_reserved_pfn(pfn) && !page_count(pfn_to_page(pfn)));
Xiao Guangrong86fde742012-07-17 21:52:52 +0800550
Junaid Shahid83ef6c82016-12-06 16:46:13 -0800551 if (is_accessed_spte(old_spte))
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800552 kvm_set_pfn_accessed(pfn);
Junaid Shahid83ef6c82016-12-06 16:46:13 -0800553
554 if (is_dirty_spte(old_spte))
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800555 kvm_set_pfn_dirty(pfn);
Junaid Shahid83ef6c82016-12-06 16:46:13 -0800556
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800557 return 1;
558}
559
560/*
561 * Rules for using mmu_spte_clear_no_track:
562 * Directly clear spte without caring the state bits of sptep,
563 * it is used to set the upper level spte.
564 */
565static void mmu_spte_clear_no_track(u64 *sptep)
566{
Xiao Guangrong603e0652011-07-12 03:31:28 +0800567 __update_clear_spte_fast(sptep, 0ull);
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +0800568}
569
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800570static u64 mmu_spte_get_lockless(u64 *sptep)
571{
572 return __get_spte_lockless(sptep);
573}
574
Junaid Shahidd3e328f22016-12-21 20:29:32 -0800575/* Restore an acc-track PTE back to a regular PTE */
576static u64 restore_acc_track_spte(u64 spte)
577{
578 u64 new_spte = spte;
Paolo Bonzini8a967d62020-10-30 13:39:55 -0400579 u64 saved_bits = (spte >> SHADOW_ACC_TRACK_SAVED_BITS_SHIFT)
580 & SHADOW_ACC_TRACK_SAVED_BITS_MASK;
Junaid Shahidd3e328f22016-12-21 20:29:32 -0800581
Peter Feinerac8d57e2017-06-30 17:26:31 -0700582 WARN_ON_ONCE(spte_ad_enabled(spte));
Junaid Shahidd3e328f22016-12-21 20:29:32 -0800583 WARN_ON_ONCE(!is_access_track_spte(spte));
584
585 new_spte &= ~shadow_acc_track_mask;
Paolo Bonzini8a967d62020-10-30 13:39:55 -0400586 new_spte &= ~(SHADOW_ACC_TRACK_SAVED_BITS_MASK <<
587 SHADOW_ACC_TRACK_SAVED_BITS_SHIFT);
Junaid Shahidd3e328f22016-12-21 20:29:32 -0800588 new_spte |= saved_bits;
589
590 return new_spte;
591}
592
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800593/* Returns the Accessed status of the PTE and resets it at the same time. */
594static bool mmu_spte_age(u64 *sptep)
595{
596 u64 spte = mmu_spte_get_lockless(sptep);
597
598 if (!is_accessed_spte(spte))
599 return false;
600
Peter Feinerac8d57e2017-06-30 17:26:31 -0700601 if (spte_ad_enabled(spte)) {
Junaid Shahidf160c7b2016-12-06 16:46:16 -0800602 clear_bit((ffs(shadow_accessed_mask) - 1),
603 (unsigned long *)sptep);
604 } else {
605 /*
606 * Capture the dirty status of the page, so that it doesn't get
607 * lost when the SPTE is marked for access tracking.
608 */
609 if (is_writable_pte(spte))
610 kvm_set_pfn_dirty(spte_to_pfn(spte));
611
612 spte = mark_spte_for_access_track(spte);
613 mmu_spte_update_no_track(sptep, spte);
614 }
615
616 return true;
617}
618
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800619static void walk_shadow_page_lockless_begin(struct kvm_vcpu *vcpu)
620{
Avi Kivityc1427862012-05-14 15:44:06 +0300621 /*
622 * Prevent page table teardown by making any free-er wait during
623 * kvm_flush_remote_tlbs() IPI to all active vcpus.
624 */
625 local_irq_disable();
Lan Tianyu36ca7e02016-03-13 11:10:25 +0800626
Avi Kivityc1427862012-05-14 15:44:06 +0300627 /*
628 * Make sure a following spte read is not reordered ahead of the write
629 * to vcpu->mode.
630 */
Lan Tianyu36ca7e02016-03-13 11:10:25 +0800631 smp_store_mb(vcpu->mode, READING_SHADOW_PAGE_TABLES);
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800632}
633
634static void walk_shadow_page_lockless_end(struct kvm_vcpu *vcpu)
635{
Avi Kivityc1427862012-05-14 15:44:06 +0300636 /*
637 * Make sure the write to vcpu->mode is not reordered in front of
Tianyu Lan9a984582018-09-07 05:45:02 +0000638 * reads to sptes. If it does, kvm_mmu_commit_zap_page() can see us
Avi Kivityc1427862012-05-14 15:44:06 +0300639 * OUTSIDE_GUEST_MODE and proceed to free the shadow page table.
640 */
Lan Tianyu36ca7e02016-03-13 11:10:25 +0800641 smp_store_release(&vcpu->mode, OUTSIDE_GUEST_MODE);
Avi Kivityc1427862012-05-14 15:44:06 +0300642 local_irq_enable();
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +0800643}
644
Sean Christopherson378f5cd2020-07-02 19:35:36 -0700645static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu, bool maybe_indirect)
Avi Kivity8c438502007-04-16 11:53:17 +0300646{
647 int r;
648
Sean Christopherson531281a2020-07-02 19:35:32 -0700649 /* 1 rmap, 1 parent PTE per level, and the prefetched rmaps. */
Sean Christopherson94ce87e2020-07-02 19:35:37 -0700650 r = kvm_mmu_topup_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
651 1 + PT64_ROOT_MAX_LEVEL + PTE_PREFETCH_NUM);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300652 if (r)
Sean Christopherson284aa862020-07-02 19:35:28 -0700653 return r;
Sean Christopherson94ce87e2020-07-02 19:35:37 -0700654 r = kvm_mmu_topup_memory_cache(&vcpu->arch.mmu_shadow_page_cache,
655 PT64_ROOT_MAX_LEVEL);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300656 if (r)
Sean Christopherson171a90d2020-07-02 19:35:33 -0700657 return r;
Sean Christopherson378f5cd2020-07-02 19:35:36 -0700658 if (maybe_indirect) {
Sean Christopherson94ce87e2020-07-02 19:35:37 -0700659 r = kvm_mmu_topup_memory_cache(&vcpu->arch.mmu_gfn_array_cache,
660 PT64_ROOT_MAX_LEVEL);
Sean Christopherson378f5cd2020-07-02 19:35:36 -0700661 if (r)
662 return r;
663 }
Sean Christopherson94ce87e2020-07-02 19:35:37 -0700664 return kvm_mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
665 PT64_ROOT_MAX_LEVEL);
Avi Kivity8c438502007-04-16 11:53:17 +0300666}
667
Avi Kivity714b93d2007-01-05 16:36:53 -0800668static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
669{
Sean Christopherson94ce87e2020-07-02 19:35:37 -0700670 kvm_mmu_free_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache);
671 kvm_mmu_free_memory_cache(&vcpu->arch.mmu_shadow_page_cache);
672 kvm_mmu_free_memory_cache(&vcpu->arch.mmu_gfn_array_cache);
673 kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800674}
675
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800676static struct pte_list_desc *mmu_alloc_pte_list_desc(struct kvm_vcpu *vcpu)
Avi Kivity714b93d2007-01-05 16:36:53 -0800677{
Sean Christopherson94ce87e2020-07-02 19:35:37 -0700678 return kvm_mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_list_desc_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800679}
680
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800681static void mmu_free_pte_list_desc(struct pte_list_desc *pte_list_desc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800682{
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800683 kmem_cache_free(pte_list_desc_cache, pte_list_desc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800684}
685
Lai Jiangshan2032a932010-05-26 16:49:59 +0800686static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
687{
688 if (!sp->role.direct)
689 return sp->gfns[index];
690
691 return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS));
692}
693
694static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn)
695{
Paolo Bonzinie9f2a762019-06-30 08:36:21 -0400696 if (!sp->role.direct) {
Lai Jiangshan2032a932010-05-26 16:49:59 +0800697 sp->gfns[index] = gfn;
Paolo Bonzinie9f2a762019-06-30 08:36:21 -0400698 return;
699 }
700
701 if (WARN_ON(gfn != kvm_mmu_page_get_gfn(sp, index)))
702 pr_err_ratelimited("gfn mismatch under direct page %llx "
703 "(expected %llx, got %llx)\n",
704 sp->gfn,
705 kvm_mmu_page_get_gfn(sp, index), gfn);
Lai Jiangshan2032a932010-05-26 16:49:59 +0800706}
707
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800708/*
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900709 * Return the pointer to the large page information for a given gfn,
710 * handling slots that are not large page aligned.
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300711 */
Takuya Yoshikawad4dbf472010-12-07 12:59:07 +0900712static struct kvm_lpage_info *lpage_info_slot(gfn_t gfn,
Ben Gardon8ca6f062021-04-01 16:37:24 -0700713 const struct kvm_memory_slot *slot, int level)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300714{
715 unsigned long idx;
716
Takuya Yoshikawafb03cb62012-02-08 12:59:10 +0900717 idx = gfn_to_index(gfn, slot->base_gfn, level);
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +0900718 return &slot->arch.lpage_info[level - 2][idx];
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300719}
720
Xiao Guangrong547ffae2016-02-24 17:51:07 +0800721static void update_gfn_disallow_lpage_count(struct kvm_memory_slot *slot,
722 gfn_t gfn, int count)
723{
724 struct kvm_lpage_info *linfo;
725 int i;
726
Sean Christopherson3bae0452020-04-27 17:54:22 -0700727 for (i = PG_LEVEL_2M; i <= KVM_MAX_HUGEPAGE_LEVEL; ++i) {
Xiao Guangrong547ffae2016-02-24 17:51:07 +0800728 linfo = lpage_info_slot(gfn, slot, i);
729 linfo->disallow_lpage += count;
730 WARN_ON(linfo->disallow_lpage < 0);
731 }
732}
733
734void kvm_mmu_gfn_disallow_lpage(struct kvm_memory_slot *slot, gfn_t gfn)
735{
736 update_gfn_disallow_lpage_count(slot, gfn, 1);
737}
738
739void kvm_mmu_gfn_allow_lpage(struct kvm_memory_slot *slot, gfn_t gfn)
740{
741 update_gfn_disallow_lpage_count(slot, gfn, -1);
742}
743
Paolo Bonzini3ed1a472015-05-19 16:29:22 +0200744static void account_shadowed(struct kvm *kvm, struct kvm_mmu_page *sp)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300745{
Paolo Bonzini699023e2015-05-18 15:03:39 +0200746 struct kvm_memslots *slots;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200747 struct kvm_memory_slot *slot;
Paolo Bonzini3ed1a472015-05-19 16:29:22 +0200748 gfn_t gfn;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300749
Xiao Guangrong56ca57f2016-02-24 17:51:14 +0800750 kvm->arch.indirect_shadow_pages++;
Paolo Bonzini3ed1a472015-05-19 16:29:22 +0200751 gfn = sp->gfn;
Paolo Bonzini699023e2015-05-18 15:03:39 +0200752 slots = kvm_memslots_for_spte_role(kvm, sp->role);
753 slot = __gfn_to_memslot(slots, gfn);
Xiao Guangrong56ca57f2016-02-24 17:51:14 +0800754
755 /* the non-leaf shadow pages are keeping readonly. */
Sean Christopherson3bae0452020-04-27 17:54:22 -0700756 if (sp->role.level > PG_LEVEL_4K)
Xiao Guangrong56ca57f2016-02-24 17:51:14 +0800757 return kvm_slot_page_track_add_page(kvm, slot, gfn,
758 KVM_PAGE_TRACK_WRITE);
759
Xiao Guangrong547ffae2016-02-24 17:51:07 +0800760 kvm_mmu_gfn_disallow_lpage(slot, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300761}
762
Ben Gardon29cf0f52020-10-14 11:27:00 -0700763void account_huge_nx_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Paolo Bonzinib8e8c832019-11-04 12:22:02 +0100764{
765 if (sp->lpage_disallowed)
766 return;
767
768 ++kvm->stat.nx_lpage_splits;
Junaid Shahid1aa9b952019-11-04 20:26:00 +0100769 list_add_tail(&sp->lpage_disallowed_link,
770 &kvm->arch.lpage_disallowed_mmu_pages);
Paolo Bonzinib8e8c832019-11-04 12:22:02 +0100771 sp->lpage_disallowed = true;
772}
773
Paolo Bonzini3ed1a472015-05-19 16:29:22 +0200774static void unaccount_shadowed(struct kvm *kvm, struct kvm_mmu_page *sp)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300775{
Paolo Bonzini699023e2015-05-18 15:03:39 +0200776 struct kvm_memslots *slots;
Joerg Roedeld25797b2009-07-27 16:30:43 +0200777 struct kvm_memory_slot *slot;
Paolo Bonzini3ed1a472015-05-19 16:29:22 +0200778 gfn_t gfn;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300779
Xiao Guangrong56ca57f2016-02-24 17:51:14 +0800780 kvm->arch.indirect_shadow_pages--;
Paolo Bonzini3ed1a472015-05-19 16:29:22 +0200781 gfn = sp->gfn;
Paolo Bonzini699023e2015-05-18 15:03:39 +0200782 slots = kvm_memslots_for_spte_role(kvm, sp->role);
783 slot = __gfn_to_memslot(slots, gfn);
Sean Christopherson3bae0452020-04-27 17:54:22 -0700784 if (sp->role.level > PG_LEVEL_4K)
Xiao Guangrong56ca57f2016-02-24 17:51:14 +0800785 return kvm_slot_page_track_remove_page(kvm, slot, gfn,
786 KVM_PAGE_TRACK_WRITE);
787
Xiao Guangrong547ffae2016-02-24 17:51:07 +0800788 kvm_mmu_gfn_allow_lpage(slot, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300789}
790
Ben Gardon29cf0f52020-10-14 11:27:00 -0700791void unaccount_huge_nx_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Paolo Bonzinib8e8c832019-11-04 12:22:02 +0100792{
793 --kvm->stat.nx_lpage_splits;
794 sp->lpage_disallowed = false;
Junaid Shahid1aa9b952019-11-04 20:26:00 +0100795 list_del(&sp->lpage_disallowed_link);
Paolo Bonzinib8e8c832019-11-04 12:22:02 +0100796}
797
Xiao Guangrong5d163b12011-03-09 15:43:00 +0800798static struct kvm_memory_slot *
799gfn_to_memslot_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t gfn,
800 bool no_dirty_log)
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300801{
802 struct kvm_memory_slot *slot;
Xiao Guangrong5d163b12011-03-09 15:43:00 +0800803
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200804 slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
Paolo Bonzini91b0d262020-01-21 16:16:32 +0100805 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
806 return NULL;
Peter Xu044c59c2020-09-30 21:22:26 -0400807 if (no_dirty_log && kvm_slot_dirty_track_enabled(slot))
Paolo Bonzini91b0d262020-01-21 16:16:32 +0100808 return NULL;
Xiao Guangrong5d163b12011-03-09 15:43:00 +0800809
810 return slot;
811}
812
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300813/*
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900814 * About rmap_head encoding:
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800815 *
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900816 * If the bit zero of rmap_head->val is clear, then it points to the only spte
817 * in this rmap chain. Otherwise, (rmap_head->val & ~1) points to a struct
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800818 * pte_list_desc containing more mappings.
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900819 */
820
821/*
822 * Returns the number of pointers in the rmap chain, not counting the new one.
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800823 */
824static int pte_list_add(struct kvm_vcpu *vcpu, u64 *spte,
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900825 struct kvm_rmap_head *rmap_head)
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800826{
827 struct pte_list_desc *desc;
828 int i, count = 0;
829
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900830 if (!rmap_head->val) {
Stephen Zhang805a0f82021-01-27 10:08:45 +0800831 rmap_printk("%p %llx 0->1\n", spte, *spte);
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900832 rmap_head->val = (unsigned long)spte;
833 } else if (!(rmap_head->val & 1)) {
Stephen Zhang805a0f82021-01-27 10:08:45 +0800834 rmap_printk("%p %llx 1->many\n", spte, *spte);
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800835 desc = mmu_alloc_pte_list_desc(vcpu);
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900836 desc->sptes[0] = (u64 *)rmap_head->val;
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800837 desc->sptes[1] = spte;
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900838 rmap_head->val = (unsigned long)desc | 1;
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800839 ++count;
840 } else {
Stephen Zhang805a0f82021-01-27 10:08:45 +0800841 rmap_printk("%p %llx many->many\n", spte, *spte);
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900842 desc = (struct pte_list_desc *)(rmap_head->val & ~1ul);
Li RongQingc6c4f962020-09-27 16:44:57 +0800843 while (desc->sptes[PTE_LIST_EXT-1]) {
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800844 count += PTE_LIST_EXT;
Li RongQingc6c4f962020-09-27 16:44:57 +0800845
846 if (!desc->more) {
847 desc->more = mmu_alloc_pte_list_desc(vcpu);
848 desc = desc->more;
849 break;
850 }
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800851 desc = desc->more;
852 }
853 for (i = 0; desc->sptes[i]; ++i)
854 ++count;
855 desc->sptes[i] = spte;
856 }
857 return count;
858}
859
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800860static void
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900861pte_list_desc_remove_entry(struct kvm_rmap_head *rmap_head,
862 struct pte_list_desc *desc, int i,
863 struct pte_list_desc *prev_desc)
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800864{
865 int j;
866
867 for (j = PTE_LIST_EXT - 1; !desc->sptes[j] && j > i; --j)
868 ;
869 desc->sptes[i] = desc->sptes[j];
870 desc->sptes[j] = NULL;
871 if (j != 0)
872 return;
873 if (!prev_desc && !desc->more)
Miaohe Linfe3c2b42019-12-05 11:40:16 +0800874 rmap_head->val = 0;
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800875 else
876 if (prev_desc)
877 prev_desc->more = desc->more;
878 else
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900879 rmap_head->val = (unsigned long)desc->more | 1;
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800880 mmu_free_pte_list_desc(desc);
881}
882
Wei Yang8daf3462018-10-04 10:04:22 +0800883static void __pte_list_remove(u64 *spte, struct kvm_rmap_head *rmap_head)
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800884{
885 struct pte_list_desc *desc;
886 struct pte_list_desc *prev_desc;
887 int i;
888
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900889 if (!rmap_head->val) {
Wei Yang8daf3462018-10-04 10:04:22 +0800890 pr_err("%s: %p 0->BUG\n", __func__, spte);
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800891 BUG();
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900892 } else if (!(rmap_head->val & 1)) {
Stephen Zhang805a0f82021-01-27 10:08:45 +0800893 rmap_printk("%p 1->0\n", spte);
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900894 if ((u64 *)rmap_head->val != spte) {
Wei Yang8daf3462018-10-04 10:04:22 +0800895 pr_err("%s: %p 1->BUG\n", __func__, spte);
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800896 BUG();
897 }
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900898 rmap_head->val = 0;
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800899 } else {
Stephen Zhang805a0f82021-01-27 10:08:45 +0800900 rmap_printk("%p many->many\n", spte);
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900901 desc = (struct pte_list_desc *)(rmap_head->val & ~1ul);
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800902 prev_desc = NULL;
903 while (desc) {
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900904 for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i) {
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800905 if (desc->sptes[i] == spte) {
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900906 pte_list_desc_remove_entry(rmap_head,
907 desc, i, prev_desc);
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800908 return;
909 }
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900910 }
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800911 prev_desc = desc;
912 desc = desc->more;
913 }
Wei Yang8daf3462018-10-04 10:04:22 +0800914 pr_err("%s: %p many->many\n", __func__, spte);
Xiao Guangrong53c07b12011-05-15 23:26:20 +0800915 BUG();
916 }
917}
918
Wei Yange7912382018-10-04 10:04:23 +0800919static void pte_list_remove(struct kvm_rmap_head *rmap_head, u64 *sptep)
920{
921 mmu_spte_clear_track_bits(sptep);
922 __pte_list_remove(sptep, rmap_head);
923}
924
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900925static struct kvm_rmap_head *__gfn_to_rmap(gfn_t gfn, int level,
926 struct kvm_memory_slot *slot)
Takuya Yoshikawa9b9b1492011-11-14 18:22:28 +0900927{
Takuya Yoshikawa77d11302012-07-02 17:57:17 +0900928 unsigned long idx;
Takuya Yoshikawa9b9b1492011-11-14 18:22:28 +0900929
Takuya Yoshikawa77d11302012-07-02 17:57:17 +0900930 idx = gfn_to_index(gfn, slot->base_gfn, level);
Sean Christopherson3bae0452020-04-27 17:54:22 -0700931 return &slot->arch.rmap[level - PG_LEVEL_4K][idx];
Takuya Yoshikawa9b9b1492011-11-14 18:22:28 +0900932}
933
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900934static struct kvm_rmap_head *gfn_to_rmap(struct kvm *kvm, gfn_t gfn,
935 struct kvm_mmu_page *sp)
Izik Eidus290fc382007-09-27 14:11:22 +0200936{
Paolo Bonzini699023e2015-05-18 15:03:39 +0200937 struct kvm_memslots *slots;
Izik Eidus290fc382007-09-27 14:11:22 +0200938 struct kvm_memory_slot *slot;
939
Paolo Bonzini699023e2015-05-18 15:03:39 +0200940 slots = kvm_memslots_for_spte_role(kvm, sp->role);
941 slot = __gfn_to_memslot(slots, gfn);
Paolo Bonzinie4cd1da2015-05-18 15:11:46 +0200942 return __gfn_to_rmap(gfn, sp->role.level, slot);
Izik Eidus290fc382007-09-27 14:11:22 +0200943}
944
Xiao Guangrongf759e2b2011-09-22 16:53:17 +0800945static bool rmap_can_add(struct kvm_vcpu *vcpu)
946{
Sean Christopherson356ec692020-07-02 19:35:27 -0700947 struct kvm_mmu_memory_cache *mc;
Xiao Guangrongf759e2b2011-09-22 16:53:17 +0800948
Sean Christopherson356ec692020-07-02 19:35:27 -0700949 mc = &vcpu->arch.mmu_pte_list_desc_cache;
Sean Christopherson94ce87e2020-07-02 19:35:37 -0700950 return kvm_mmu_memory_cache_nr_free_objects(mc);
Xiao Guangrongf759e2b2011-09-22 16:53:17 +0800951}
952
Joerg Roedel44ad9942009-07-27 16:30:42 +0200953static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800954{
Avi Kivity4db35312007-11-21 15:28:32 +0200955 struct kvm_mmu_page *sp;
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900956 struct kvm_rmap_head *rmap_head;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800957
Sean Christopherson57354682020-06-22 13:20:33 -0700958 sp = sptep_to_sp(spte);
Lai Jiangshan2032a932010-05-26 16:49:59 +0800959 kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn);
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900960 rmap_head = gfn_to_rmap(vcpu->kvm, gfn, sp);
961 return pte_list_add(vcpu, spte, rmap_head);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800962}
963
Izik Eidus290fc382007-09-27 14:11:22 +0200964static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800965{
Avi Kivity4db35312007-11-21 15:28:32 +0200966 struct kvm_mmu_page *sp;
Lai Jiangshan2032a932010-05-26 16:49:59 +0800967 gfn_t gfn;
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900968 struct kvm_rmap_head *rmap_head;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800969
Sean Christopherson57354682020-06-22 13:20:33 -0700970 sp = sptep_to_sp(spte);
Lai Jiangshan2032a932010-05-26 16:49:59 +0800971 gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt);
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900972 rmap_head = gfn_to_rmap(kvm, gfn, sp);
Wei Yang8daf3462018-10-04 10:04:22 +0800973 __pte_list_remove(spte, rmap_head);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800974}
975
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +0900976/*
977 * Used by the following functions to iterate through the sptes linked by a
978 * rmap. All fields are private and not assumed to be used outside.
979 */
980struct rmap_iterator {
981 /* private fields */
982 struct pte_list_desc *desc; /* holds the sptep if not NULL */
983 int pos; /* index of the sptep */
984};
985
986/*
987 * Iteration must be started by this function. This should also be used after
988 * removing/dropping sptes from the rmap link because in such cases the
Miaohe Lin0a03cbd2019-12-06 16:20:18 +0800989 * information in the iterator may not be valid.
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +0900990 *
991 * Returns sptep if found, NULL otherwise.
992 */
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900993static u64 *rmap_get_first(struct kvm_rmap_head *rmap_head,
994 struct rmap_iterator *iter)
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +0900995{
Takuya Yoshikawa77fbbbd2015-11-20 17:45:44 +0900996 u64 *sptep;
997
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +0900998 if (!rmap_head->val)
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +0900999 return NULL;
1000
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001001 if (!(rmap_head->val & 1)) {
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001002 iter->desc = NULL;
Takuya Yoshikawa77fbbbd2015-11-20 17:45:44 +09001003 sptep = (u64 *)rmap_head->val;
1004 goto out;
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001005 }
1006
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001007 iter->desc = (struct pte_list_desc *)(rmap_head->val & ~1ul);
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001008 iter->pos = 0;
Takuya Yoshikawa77fbbbd2015-11-20 17:45:44 +09001009 sptep = iter->desc->sptes[iter->pos];
1010out:
1011 BUG_ON(!is_shadow_present_pte(*sptep));
1012 return sptep;
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001013}
1014
1015/*
1016 * Must be used with a valid iterator: e.g. after rmap_get_first().
1017 *
1018 * Returns sptep if found, NULL otherwise.
1019 */
1020static u64 *rmap_get_next(struct rmap_iterator *iter)
1021{
Takuya Yoshikawa77fbbbd2015-11-20 17:45:44 +09001022 u64 *sptep;
1023
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001024 if (iter->desc) {
1025 if (iter->pos < PTE_LIST_EXT - 1) {
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001026 ++iter->pos;
1027 sptep = iter->desc->sptes[iter->pos];
1028 if (sptep)
Takuya Yoshikawa77fbbbd2015-11-20 17:45:44 +09001029 goto out;
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001030 }
1031
1032 iter->desc = iter->desc->more;
1033
1034 if (iter->desc) {
1035 iter->pos = 0;
1036 /* desc->sptes[0] cannot be NULL */
Takuya Yoshikawa77fbbbd2015-11-20 17:45:44 +09001037 sptep = iter->desc->sptes[iter->pos];
1038 goto out;
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001039 }
1040 }
1041
1042 return NULL;
Takuya Yoshikawa77fbbbd2015-11-20 17:45:44 +09001043out:
1044 BUG_ON(!is_shadow_present_pte(*sptep));
1045 return sptep;
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001046}
1047
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001048#define for_each_rmap_spte(_rmap_head_, _iter_, _spte_) \
1049 for (_spte_ = rmap_get_first(_rmap_head_, _iter_); \
Takuya Yoshikawa77fbbbd2015-11-20 17:45:44 +09001050 _spte_; _spte_ = rmap_get_next(_iter_))
Xiao Guangrong0d536792015-05-13 14:42:20 +08001051
Xiao Guangrongc3707952011-07-12 03:28:04 +08001052static void drop_spte(struct kvm *kvm, u64 *sptep)
Xiao Guangronge4b502e2010-07-16 11:28:09 +08001053{
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08001054 if (mmu_spte_clear_track_bits(sptep))
Marcelo Tosattieb45fda2010-10-25 11:58:22 -02001055 rmap_remove(kvm, sptep);
Avi Kivitybe38d272010-06-06 14:31:27 +03001056}
1057
Xiao Guangrong8e22f952012-06-20 15:57:39 +08001058
1059static bool __drop_large_spte(struct kvm *kvm, u64 *sptep)
1060{
1061 if (is_large_pte(*sptep)) {
Sean Christopherson57354682020-06-22 13:20:33 -07001062 WARN_ON(sptep_to_sp(sptep)->role.level == PG_LEVEL_4K);
Xiao Guangrong8e22f952012-06-20 15:57:39 +08001063 drop_spte(kvm, sptep);
1064 --kvm->stat.lpages;
1065 return true;
1066 }
1067
1068 return false;
1069}
1070
1071static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep)
1072{
Lan Tianyuc3134ce2018-12-06 21:21:09 +08001073 if (__drop_large_spte(vcpu->kvm, sptep)) {
Sean Christopherson57354682020-06-22 13:20:33 -07001074 struct kvm_mmu_page *sp = sptep_to_sp(sptep);
Lan Tianyuc3134ce2018-12-06 21:21:09 +08001075
1076 kvm_flush_remote_tlbs_with_address(vcpu->kvm, sp->gfn,
1077 KVM_PAGES_PER_HPAGE(sp->role.level));
1078 }
Xiao Guangrong8e22f952012-06-20 15:57:39 +08001079}
1080
1081/*
Xiao Guangrong49fde342012-06-20 15:58:58 +08001082 * Write-protect on the specified @sptep, @pt_protect indicates whether
Xiao Guangrongc126d942014-04-17 17:06:14 +08001083 * spte write-protection is caused by protecting shadow page table.
Xiao Guangrong49fde342012-06-20 15:58:58 +08001084 *
Tiejun Chenb4619662014-09-22 10:31:38 +08001085 * Note: write protection is difference between dirty logging and spte
Xiao Guangrong49fde342012-06-20 15:58:58 +08001086 * protection:
1087 * - for dirty logging, the spte can be set to writable at anytime if
1088 * its dirty bitmap is properly set.
1089 * - for spte protection, the spte can be writable only after unsync-ing
1090 * shadow page.
Xiao Guangrong8e22f952012-06-20 15:57:39 +08001091 *
Xiao Guangrongc126d942014-04-17 17:06:14 +08001092 * Return true if tlb need be flushed.
Xiao Guangrong8e22f952012-06-20 15:57:39 +08001093 */
Bandan Dasc4f138b2016-08-02 16:32:37 -04001094static bool spte_write_protect(u64 *sptep, bool pt_protect)
Xiao Guangrongd13bc5b2012-06-20 15:57:15 +08001095{
1096 u64 spte = *sptep;
1097
Xiao Guangrong49fde342012-06-20 15:58:58 +08001098 if (!is_writable_pte(spte) &&
Junaid Shahidea4114b2016-12-06 16:46:11 -08001099 !(pt_protect && spte_can_locklessly_be_made_writable(spte)))
Xiao Guangrongd13bc5b2012-06-20 15:57:15 +08001100 return false;
1101
Stephen Zhang805a0f82021-01-27 10:08:45 +08001102 rmap_printk("spte %p %llx\n", sptep, *sptep);
Xiao Guangrongd13bc5b2012-06-20 15:57:15 +08001103
Xiao Guangrong49fde342012-06-20 15:58:58 +08001104 if (pt_protect)
Sean Christopherson5fc34242021-02-25 12:47:43 -08001105 spte &= ~shadow_mmu_writable_mask;
Xiao Guangrongd13bc5b2012-06-20 15:57:15 +08001106 spte = spte & ~PT_WRITABLE_MASK;
Xiao Guangrong49fde342012-06-20 15:58:58 +08001107
Xiao Guangrongc126d942014-04-17 17:06:14 +08001108 return mmu_spte_update(sptep, spte);
Xiao Guangrongd13bc5b2012-06-20 15:57:15 +08001109}
1110
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001111static bool __rmap_write_protect(struct kvm *kvm,
1112 struct kvm_rmap_head *rmap_head,
Takuya Yoshikawa245c3912013-01-08 19:44:09 +09001113 bool pt_protect)
Takuya Yoshikawaa0ed4602012-03-01 19:31:22 +09001114{
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001115 u64 *sptep;
1116 struct rmap_iterator iter;
Xiao Guangrongd13bc5b2012-06-20 15:57:15 +08001117 bool flush = false;
Takuya Yoshikawaa0ed4602012-03-01 19:31:22 +09001118
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001119 for_each_rmap_spte(rmap_head, &iter, sptep)
Bandan Dasc4f138b2016-08-02 16:32:37 -04001120 flush |= spte_write_protect(sptep, pt_protect);
Takuya Yoshikawaa0ed4602012-03-01 19:31:22 +09001121
Xiao Guangrongd13bc5b2012-06-20 15:57:15 +08001122 return flush;
Takuya Yoshikawaa0ed4602012-03-01 19:31:22 +09001123}
1124
Bandan Dasc4f138b2016-08-02 16:32:37 -04001125static bool spte_clear_dirty(u64 *sptep)
Kai Huangf4b4b182015-01-28 10:54:24 +08001126{
1127 u64 spte = *sptep;
1128
Stephen Zhang805a0f82021-01-27 10:08:45 +08001129 rmap_printk("spte %p %llx\n", sptep, *sptep);
Kai Huangf4b4b182015-01-28 10:54:24 +08001130
Paolo Bonzini1f4e5fc2019-09-26 18:47:59 +02001131 MMU_WARN_ON(!spte_ad_enabled(spte));
Kai Huangf4b4b182015-01-28 10:54:24 +08001132 spte &= ~shadow_dirty_mask;
Kai Huangf4b4b182015-01-28 10:54:24 +08001133 return mmu_spte_update(sptep, spte);
1134}
1135
Paolo Bonzini1f4e5fc2019-09-26 18:47:59 +02001136static bool spte_wrprot_for_clear_dirty(u64 *sptep)
Peter Feinerac8d57e2017-06-30 17:26:31 -07001137{
1138 bool was_writable = test_and_clear_bit(PT_WRITABLE_SHIFT,
1139 (unsigned long *)sptep);
Paolo Bonzini1f4e5fc2019-09-26 18:47:59 +02001140 if (was_writable && !spte_ad_enabled(*sptep))
Peter Feinerac8d57e2017-06-30 17:26:31 -07001141 kvm_set_pfn_dirty(spte_to_pfn(*sptep));
1142
1143 return was_writable;
1144}
1145
1146/*
1147 * Gets the GFN ready for another round of dirty logging by clearing the
1148 * - D bit on ad-enabled SPTEs, and
1149 * - W bit on ad-disabled SPTEs.
1150 * Returns true iff any D or W bits were cleared.
1151 */
Sean Christopherson0a234f52021-02-12 16:50:05 -08001152static bool __rmap_clear_dirty(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
1153 struct kvm_memory_slot *slot)
Kai Huangf4b4b182015-01-28 10:54:24 +08001154{
1155 u64 *sptep;
1156 struct rmap_iterator iter;
1157 bool flush = false;
1158
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001159 for_each_rmap_spte(rmap_head, &iter, sptep)
Paolo Bonzini1f4e5fc2019-09-26 18:47:59 +02001160 if (spte_ad_need_write_protect(*sptep))
1161 flush |= spte_wrprot_for_clear_dirty(sptep);
Peter Feinerac8d57e2017-06-30 17:26:31 -07001162 else
Paolo Bonzini1f4e5fc2019-09-26 18:47:59 +02001163 flush |= spte_clear_dirty(sptep);
Kai Huangf4b4b182015-01-28 10:54:24 +08001164
1165 return flush;
1166}
1167
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001168/**
Kai Huang3b0f1d02015-01-28 10:54:23 +08001169 * kvm_mmu_write_protect_pt_masked - write protect selected PT level pages
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001170 * @kvm: kvm instance
1171 * @slot: slot to protect
1172 * @gfn_offset: start of the BITS_PER_LONG pages we care about
1173 * @mask: indicates which pages we should protect
1174 *
Keqian Zhu89212912021-04-29 11:41:15 +08001175 * Used when we do not need to care about huge page mappings.
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001176 */
Kai Huang3b0f1d02015-01-28 10:54:23 +08001177static void kvm_mmu_write_protect_pt_masked(struct kvm *kvm,
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001178 struct kvm_memory_slot *slot,
1179 gfn_t gfn_offset, unsigned long mask)
Izik Eidus98348e92007-10-16 14:42:30 +02001180{
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001181 struct kvm_rmap_head *rmap_head;
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001182
Paolo Bonzini897218f2021-02-06 09:53:33 -05001183 if (is_tdp_mmu_enabled(kvm))
Ben Gardona6a0b052020-10-14 11:26:55 -07001184 kvm_tdp_mmu_clear_dirty_pt_masked(kvm, slot,
1185 slot->base_gfn + gfn_offset, mask, true);
Ben Gardone2209712021-05-18 10:34:13 -07001186
1187 if (!kvm_memslots_have_rmaps(kvm))
1188 return;
1189
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001190 while (mask) {
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001191 rmap_head = __gfn_to_rmap(slot->base_gfn + gfn_offset + __ffs(mask),
Sean Christopherson3bae0452020-04-27 17:54:22 -07001192 PG_LEVEL_4K, slot);
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001193 __rmap_write_protect(kvm, rmap_head, false);
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001194
1195 /* clear the first set bit */
1196 mask &= mask - 1;
1197 }
1198}
1199
Kai Huang3b0f1d02015-01-28 10:54:23 +08001200/**
Peter Feinerac8d57e2017-06-30 17:26:31 -07001201 * kvm_mmu_clear_dirty_pt_masked - clear MMU D-bit for PT level pages, or write
1202 * protect the page if the D-bit isn't supported.
Kai Huangf4b4b182015-01-28 10:54:24 +08001203 * @kvm: kvm instance
1204 * @slot: slot to clear D-bit
1205 * @gfn_offset: start of the BITS_PER_LONG pages we care about
1206 * @mask: indicates which pages we should clear D-bit
1207 *
1208 * Used for PML to re-log the dirty GPAs after userspace querying dirty_bitmap.
1209 */
Sean Christophersona018eba2021-02-12 16:50:10 -08001210static void kvm_mmu_clear_dirty_pt_masked(struct kvm *kvm,
1211 struct kvm_memory_slot *slot,
1212 gfn_t gfn_offset, unsigned long mask)
Kai Huangf4b4b182015-01-28 10:54:24 +08001213{
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001214 struct kvm_rmap_head *rmap_head;
Kai Huangf4b4b182015-01-28 10:54:24 +08001215
Paolo Bonzini897218f2021-02-06 09:53:33 -05001216 if (is_tdp_mmu_enabled(kvm))
Ben Gardona6a0b052020-10-14 11:26:55 -07001217 kvm_tdp_mmu_clear_dirty_pt_masked(kvm, slot,
1218 slot->base_gfn + gfn_offset, mask, false);
Ben Gardone2209712021-05-18 10:34:13 -07001219
1220 if (!kvm_memslots_have_rmaps(kvm))
1221 return;
1222
Kai Huangf4b4b182015-01-28 10:54:24 +08001223 while (mask) {
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001224 rmap_head = __gfn_to_rmap(slot->base_gfn + gfn_offset + __ffs(mask),
Sean Christopherson3bae0452020-04-27 17:54:22 -07001225 PG_LEVEL_4K, slot);
Sean Christopherson0a234f52021-02-12 16:50:05 -08001226 __rmap_clear_dirty(kvm, rmap_head, slot);
Kai Huangf4b4b182015-01-28 10:54:24 +08001227
1228 /* clear the first set bit */
1229 mask &= mask - 1;
1230 }
1231}
Kai Huangf4b4b182015-01-28 10:54:24 +08001232
1233/**
Kai Huang3b0f1d02015-01-28 10:54:23 +08001234 * kvm_arch_mmu_enable_log_dirty_pt_masked - enable dirty logging for selected
1235 * PT level pages.
1236 *
1237 * It calls kvm_mmu_write_protect_pt_masked to write protect selected pages to
1238 * enable dirty logging for them.
1239 *
Keqian Zhu89212912021-04-29 11:41:15 +08001240 * We need to care about huge page mappings: e.g. during dirty logging we may
1241 * have such mappings.
Kai Huang3b0f1d02015-01-28 10:54:23 +08001242 */
1243void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
1244 struct kvm_memory_slot *slot,
1245 gfn_t gfn_offset, unsigned long mask)
1246{
Keqian Zhu89212912021-04-29 11:41:15 +08001247 /*
1248 * Huge pages are NOT write protected when we start dirty logging in
1249 * initially-all-set mode; must write protect them here so that they
1250 * are split to 4K on the first write.
1251 *
1252 * The gfn_offset is guaranteed to be aligned to 64, but the base_gfn
1253 * of memslot has no such restriction, so the range can cross two large
1254 * pages.
1255 */
1256 if (kvm_dirty_log_manual_protect_and_init_set(kvm)) {
1257 gfn_t start = slot->base_gfn + gfn_offset + __ffs(mask);
1258 gfn_t end = slot->base_gfn + gfn_offset + __fls(mask);
1259
1260 kvm_mmu_slot_gfn_write_protect(kvm, slot, start, PG_LEVEL_2M);
1261
1262 /* Cross two large pages? */
1263 if (ALIGN(start << PAGE_SHIFT, PMD_SIZE) !=
1264 ALIGN(end << PAGE_SHIFT, PMD_SIZE))
1265 kvm_mmu_slot_gfn_write_protect(kvm, slot, end,
1266 PG_LEVEL_2M);
1267 }
1268
1269 /* Now handle 4K PTEs. */
Sean Christophersona018eba2021-02-12 16:50:10 -08001270 if (kvm_x86_ops.cpu_dirty_log_size)
1271 kvm_mmu_clear_dirty_pt_masked(kvm, slot, gfn_offset, mask);
Kai Huang88178fd2015-01-28 10:54:27 +08001272 else
1273 kvm_mmu_write_protect_pt_masked(kvm, slot, gfn_offset, mask);
Kai Huang3b0f1d02015-01-28 10:54:23 +08001274}
1275
Peter Xufb04a1e2020-09-30 21:22:22 -04001276int kvm_cpu_dirty_log_size(void)
1277{
Sean Christopherson6dd03802021-02-12 16:50:09 -08001278 return kvm_x86_ops.cpu_dirty_log_size;
Peter Xufb04a1e2020-09-30 21:22:22 -04001279}
1280
Xiao Guangrongaeecee22016-02-24 17:51:08 +08001281bool kvm_mmu_slot_gfn_write_protect(struct kvm *kvm,
Keqian Zhu3ad93562021-04-29 11:41:14 +08001282 struct kvm_memory_slot *slot, u64 gfn,
1283 int min_level)
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001284{
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001285 struct kvm_rmap_head *rmap_head;
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001286 int i;
Xiao Guangrong2f845692012-06-20 15:56:53 +08001287 bool write_protected = false;
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001288
Ben Gardone2209712021-05-18 10:34:13 -07001289 if (kvm_memslots_have_rmaps(kvm)) {
1290 for (i = min_level; i <= KVM_MAX_HUGEPAGE_LEVEL; ++i) {
1291 rmap_head = __gfn_to_rmap(gfn, i, slot);
1292 write_protected |= __rmap_write_protect(kvm, rmap_head, true);
1293 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001294 }
1295
Paolo Bonzini897218f2021-02-06 09:53:33 -05001296 if (is_tdp_mmu_enabled(kvm))
Ben Gardon46044f72020-10-14 11:26:57 -07001297 write_protected |=
Keqian Zhu3ad93562021-04-29 11:41:14 +08001298 kvm_tdp_mmu_write_protect_gfn(kvm, slot, gfn, min_level);
Ben Gardon46044f72020-10-14 11:26:57 -07001299
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001300 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -08001301}
1302
Xiao Guangrongaeecee22016-02-24 17:51:08 +08001303static bool rmap_write_protect(struct kvm_vcpu *vcpu, u64 gfn)
1304{
1305 struct kvm_memory_slot *slot;
1306
1307 slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
Keqian Zhu3ad93562021-04-29 11:41:14 +08001308 return kvm_mmu_slot_gfn_write_protect(vcpu->kvm, slot, gfn, PG_LEVEL_4K);
Xiao Guangrongaeecee22016-02-24 17:51:08 +08001309}
1310
Sean Christopherson0a234f52021-02-12 16:50:05 -08001311static bool kvm_zap_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
1312 struct kvm_memory_slot *slot)
Xiao Guangrong6a49f852015-05-13 14:42:25 +08001313{
1314 u64 *sptep;
1315 struct rmap_iterator iter;
1316 bool flush = false;
1317
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001318 while ((sptep = rmap_get_first(rmap_head, &iter))) {
Stephen Zhang805a0f82021-01-27 10:08:45 +08001319 rmap_printk("spte %p %llx.\n", sptep, *sptep);
Xiao Guangrong6a49f852015-05-13 14:42:25 +08001320
Wei Yange7912382018-10-04 10:04:23 +08001321 pte_list_remove(rmap_head, sptep);
Xiao Guangrong6a49f852015-05-13 14:42:25 +08001322 flush = true;
1323 }
1324
1325 return flush;
1326}
1327
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001328static bool kvm_unmap_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
1329 struct kvm_memory_slot *slot, gfn_t gfn, int level,
1330 pte_t unused)
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001331{
Sean Christopherson0a234f52021-02-12 16:50:05 -08001332 return kvm_zap_rmapp(kvm, rmap_head, slot);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001333}
1334
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001335static bool kvm_set_pte_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
1336 struct kvm_memory_slot *slot, gfn_t gfn, int level,
1337 pte_t pte)
Izik Eidus3da0dd42009-09-23 21:47:18 +03001338{
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001339 u64 *sptep;
1340 struct rmap_iterator iter;
Izik Eidus3da0dd42009-09-23 21:47:18 +03001341 int need_flush = 0;
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001342 u64 new_spte;
Dan Williamsba049e92016-01-15 16:56:11 -08001343 kvm_pfn_t new_pfn;
Izik Eidus3da0dd42009-09-23 21:47:18 +03001344
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001345 WARN_ON(pte_huge(pte));
1346 new_pfn = pte_pfn(pte);
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001347
Xiao Guangrong0d536792015-05-13 14:42:20 +08001348restart:
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001349 for_each_rmap_spte(rmap_head, &iter, sptep) {
Stephen Zhang805a0f82021-01-27 10:08:45 +08001350 rmap_printk("spte %p %llx gfn %llx (%d)\n",
Junaid Shahidf160c7b2016-12-06 16:46:16 -08001351 sptep, *sptep, gfn, level);
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001352
Izik Eidus3da0dd42009-09-23 21:47:18 +03001353 need_flush = 1;
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001354
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001355 if (pte_write(pte)) {
Wei Yange7912382018-10-04 10:04:23 +08001356 pte_list_remove(rmap_head, sptep);
Xiao Guangrong0d536792015-05-13 14:42:20 +08001357 goto restart;
Izik Eidus3da0dd42009-09-23 21:47:18 +03001358 } else {
Paolo Bonzinicb3eeda2020-09-28 10:17:17 -04001359 new_spte = kvm_mmu_changed_pte_notifier_make_spte(
1360 *sptep, new_pfn);
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001361
1362 mmu_spte_clear_track_bits(sptep);
1363 mmu_spte_set(sptep, new_spte);
Izik Eidus3da0dd42009-09-23 21:47:18 +03001364 }
1365 }
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001366
Lan Tianyu3cc5ea92018-12-06 21:21:12 +08001367 if (need_flush && kvm_available_flush_tlb_with_range()) {
1368 kvm_flush_remote_tlbs_with_address(kvm, gfn, 1);
1369 return 0;
1370 }
1371
Lan Tianyu0cf853c2018-12-06 21:21:11 +08001372 return need_flush;
Izik Eidus3da0dd42009-09-23 21:47:18 +03001373}
1374
Xiao Guangrong6ce1f4e2015-05-13 14:42:22 +08001375struct slot_rmap_walk_iterator {
1376 /* input fields. */
1377 struct kvm_memory_slot *slot;
1378 gfn_t start_gfn;
1379 gfn_t end_gfn;
1380 int start_level;
1381 int end_level;
1382
1383 /* output fields. */
1384 gfn_t gfn;
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001385 struct kvm_rmap_head *rmap;
Xiao Guangrong6ce1f4e2015-05-13 14:42:22 +08001386 int level;
1387
1388 /* private field. */
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001389 struct kvm_rmap_head *end_rmap;
Xiao Guangrong6ce1f4e2015-05-13 14:42:22 +08001390};
1391
1392static void
1393rmap_walk_init_level(struct slot_rmap_walk_iterator *iterator, int level)
1394{
1395 iterator->level = level;
1396 iterator->gfn = iterator->start_gfn;
1397 iterator->rmap = __gfn_to_rmap(iterator->gfn, level, iterator->slot);
1398 iterator->end_rmap = __gfn_to_rmap(iterator->end_gfn, level,
1399 iterator->slot);
1400}
1401
1402static void
1403slot_rmap_walk_init(struct slot_rmap_walk_iterator *iterator,
1404 struct kvm_memory_slot *slot, int start_level,
1405 int end_level, gfn_t start_gfn, gfn_t end_gfn)
1406{
1407 iterator->slot = slot;
1408 iterator->start_level = start_level;
1409 iterator->end_level = end_level;
1410 iterator->start_gfn = start_gfn;
1411 iterator->end_gfn = end_gfn;
1412
1413 rmap_walk_init_level(iterator, iterator->start_level);
1414}
1415
1416static bool slot_rmap_walk_okay(struct slot_rmap_walk_iterator *iterator)
1417{
1418 return !!iterator->rmap;
1419}
1420
1421static void slot_rmap_walk_next(struct slot_rmap_walk_iterator *iterator)
1422{
1423 if (++iterator->rmap <= iterator->end_rmap) {
1424 iterator->gfn += (1UL << KVM_HPAGE_GFN_SHIFT(iterator->level));
1425 return;
1426 }
1427
1428 if (++iterator->level > iterator->end_level) {
1429 iterator->rmap = NULL;
1430 return;
1431 }
1432
1433 rmap_walk_init_level(iterator, iterator->level);
1434}
1435
1436#define for_each_slot_rmap_range(_slot_, _start_level_, _end_level_, \
1437 _start_gfn, _end_gfn, _iter_) \
1438 for (slot_rmap_walk_init(_iter_, _slot_, _start_level_, \
1439 _end_level_, _start_gfn, _end_gfn); \
1440 slot_rmap_walk_okay(_iter_); \
1441 slot_rmap_walk_next(_iter_))
1442
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001443typedef bool (*rmap_handler_t)(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
1444 struct kvm_memory_slot *slot, gfn_t gfn,
1445 int level, pte_t pte);
Sean Christophersonc1b91492021-02-25 17:03:28 -08001446
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001447static __always_inline bool kvm_handle_gfn_range(struct kvm *kvm,
1448 struct kvm_gfn_range *range,
1449 rmap_handler_t handler)
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001450{
Xiao Guangrong6ce1f4e2015-05-13 14:42:22 +08001451 struct slot_rmap_walk_iterator iterator;
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001452 bool ret = false;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001453
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001454 for_each_slot_rmap_range(range->slot, PG_LEVEL_4K, KVM_MAX_HUGEPAGE_LEVEL,
1455 range->start, range->end - 1, &iterator)
1456 ret |= handler(kvm, iterator.rmap, range->slot, iterator.gfn,
1457 iterator.level, range->pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001458
Takuya Yoshikawaf3953022012-07-02 17:58:48 +09001459 return ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001460}
1461
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001462bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
Takuya Yoshikawa84504ef2012-07-02 17:55:48 +09001463{
Ben Gardone2209712021-05-18 10:34:13 -07001464 bool flush = false;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001465
Ben Gardone2209712021-05-18 10:34:13 -07001466 if (kvm_memslots_have_rmaps(kvm))
1467 flush = kvm_handle_gfn_range(kvm, range, kvm_unmap_rmapp);
Ben Gardon063afac2020-10-14 11:26:52 -07001468
Paolo Bonzini897218f2021-02-06 09:53:33 -05001469 if (is_tdp_mmu_enabled(kvm))
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001470 flush |= kvm_tdp_mmu_unmap_gfn_range(kvm, range, flush);
Ben Gardon063afac2020-10-14 11:26:52 -07001471
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001472 return flush;
Takuya Yoshikawab3ae2092012-07-02 17:56:33 +09001473}
1474
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001475bool kvm_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
Izik Eidus3da0dd42009-09-23 21:47:18 +03001476{
Ben Gardone2209712021-05-18 10:34:13 -07001477 bool flush = false;
Ben Gardon1d8dd6b2020-10-14 11:26:54 -07001478
Ben Gardone2209712021-05-18 10:34:13 -07001479 if (kvm_memslots_have_rmaps(kvm))
1480 flush = kvm_handle_gfn_range(kvm, range, kvm_set_pte_rmapp);
Ben Gardon1d8dd6b2020-10-14 11:26:54 -07001481
Paolo Bonzini897218f2021-02-06 09:53:33 -05001482 if (is_tdp_mmu_enabled(kvm))
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001483 flush |= kvm_tdp_mmu_set_spte_gfn(kvm, range);
Ben Gardon1d8dd6b2020-10-14 11:26:54 -07001484
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001485 return flush;
Izik Eidus3da0dd42009-09-23 21:47:18 +03001486}
1487
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001488static bool kvm_age_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
1489 struct kvm_memory_slot *slot, gfn_t gfn, int level,
1490 pte_t unused)
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001491{
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001492 u64 *sptep;
Kees Cook3f649ab2020-06-03 13:09:38 -07001493 struct rmap_iterator iter;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001494 int young = 0;
1495
Junaid Shahidf160c7b2016-12-06 16:46:16 -08001496 for_each_rmap_spte(rmap_head, &iter, sptep)
1497 young |= mmu_spte_age(sptep);
Xiao Guangrong0d536792015-05-13 14:42:20 +08001498
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001499 return young;
1500}
1501
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001502static bool kvm_test_age_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
1503 struct kvm_memory_slot *slot, gfn_t gfn,
1504 int level, pte_t unused)
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001505{
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001506 u64 *sptep;
1507 struct rmap_iterator iter;
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001508
Junaid Shahid83ef6c82016-12-06 16:46:13 -08001509 for_each_rmap_spte(rmap_head, &iter, sptep)
1510 if (is_accessed_spte(*sptep))
1511 return 1;
Junaid Shahid83ef6c82016-12-06 16:46:13 -08001512 return 0;
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001513}
1514
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001515#define RMAP_RECYCLE_THRESHOLD 1000
1516
Joerg Roedel852e3c12009-07-27 16:30:44 +02001517static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001518{
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001519 struct kvm_rmap_head *rmap_head;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001520 struct kvm_mmu_page *sp;
1521
Sean Christopherson57354682020-06-22 13:20:33 -07001522 sp = sptep_to_sp(spte);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001523
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001524 rmap_head = gfn_to_rmap(vcpu->kvm, gfn, sp);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001525
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001526 kvm_unmap_rmapp(vcpu->kvm, rmap_head, NULL, gfn, sp->role.level, __pte(0));
Lan Tianyuc3134ce2018-12-06 21:21:09 +08001527 kvm_flush_remote_tlbs_with_address(vcpu->kvm, sp->gfn,
1528 KVM_PAGES_PER_HPAGE(sp->role.level));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001529}
1530
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001531bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001532{
Ben Gardone2209712021-05-18 10:34:13 -07001533 bool young = false;
Ben Gardonf8e14492020-10-14 11:26:53 -07001534
Ben Gardone2209712021-05-18 10:34:13 -07001535 if (kvm_memslots_have_rmaps(kvm))
1536 young = kvm_handle_gfn_range(kvm, range, kvm_age_rmapp);
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001537
Paolo Bonzini897218f2021-02-06 09:53:33 -05001538 if (is_tdp_mmu_enabled(kvm))
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001539 young |= kvm_tdp_mmu_age_gfn_range(kvm, range);
Ben Gardonf8e14492020-10-14 11:26:53 -07001540
1541 return young;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001542}
1543
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001544bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001545{
Ben Gardone2209712021-05-18 10:34:13 -07001546 bool young = false;
Ben Gardonf8e14492020-10-14 11:26:53 -07001547
Ben Gardone2209712021-05-18 10:34:13 -07001548 if (kvm_memslots_have_rmaps(kvm))
1549 young = kvm_handle_gfn_range(kvm, range, kvm_test_age_rmapp);
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001550
Paolo Bonzini897218f2021-02-06 09:53:33 -05001551 if (is_tdp_mmu_enabled(kvm))
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001552 young |= kvm_tdp_mmu_test_age_gfn(kvm, range);
Ben Gardonf8e14492020-10-14 11:26:53 -07001553
1554 return young;
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001555}
1556
Yaozu Dongd6c69ee2007-04-25 14:17:25 +08001557#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +03001558static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001559{
Avi Kivity139bdb22007-01-05 16:36:50 -08001560 u64 *pos;
1561 u64 *end;
1562
Avi Kivity47ad8e62007-05-06 15:50:58 +03001563 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +03001564 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001565 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -08001566 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001567 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -08001568 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001569 return 1;
1570}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +08001571#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001572
Dave Hansen45221ab2010-08-19 18:11:37 -07001573/*
1574 * This value is the sum of all of the kvm instances's
1575 * kvm->arch.n_used_mmu_pages values. We need a global,
1576 * aggregate version in order to make the slab shrinker
1577 * faster
1578 */
Ben Gardonbc8a3d82019-04-08 11:07:30 -07001579static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, unsigned long nr)
Dave Hansen45221ab2010-08-19 18:11:37 -07001580{
1581 kvm->arch.n_used_mmu_pages += nr;
1582 percpu_counter_add(&kvm_total_used_mmu_pages, nr);
1583}
1584
Gleb Natapov834be0d2013-01-30 16:45:05 +02001585static void kvm_mmu_free_page(struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -08001586{
Paolo Bonzinifa4a2c02013-10-02 16:56:16 +02001587 MMU_WARN_ON(!is_empty_shadow_page(sp->spt));
Xiao Guangrong77758342010-06-04 21:53:54 +08001588 hlist_del(&sp->hash_link);
Xiao Guangrongbd4c86e2011-07-12 03:27:14 +08001589 list_del(&sp->link);
1590 free_page((unsigned long)sp->spt);
Gleb Natapov834be0d2013-01-30 16:45:05 +02001591 if (!sp->role.direct)
1592 free_page((unsigned long)sp->gfns);
Xiao Guangronge8ad9a72010-05-13 10:06:02 +08001593 kmem_cache_free(mmu_page_header_cache, sp);
Avi Kivity260746c2007-01-05 16:36:49 -08001594}
1595
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001596static unsigned kvm_page_table_hashfn(gfn_t gfn)
1597{
David Matlack114df302016-12-19 13:58:25 -08001598 return hash_64(gfn, KVM_MMU_HASH_SHIFT);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001599}
1600
Xiao Guangrong67052b32011-05-15 23:27:08 +08001601static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
1602 struct kvm_mmu_page *sp, u64 *parent_pte)
1603{
1604 if (!parent_pte)
1605 return;
1606
1607 pte_list_add(vcpu, parent_pte, &sp->parent_ptes);
1608}
1609
1610static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
1611 u64 *parent_pte)
1612{
Wei Yang8daf3462018-10-04 10:04:22 +08001613 __pte_list_remove(parent_pte, &sp->parent_ptes);
Xiao Guangrong67052b32011-05-15 23:27:08 +08001614}
1615
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001616static void drop_parent_pte(struct kvm_mmu_page *sp,
1617 u64 *parent_pte)
1618{
1619 mmu_page_remove_parent_pte(sp, parent_pte);
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08001620 mmu_spte_clear_no_track(parent_pte);
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001621}
1622
Takuya Yoshikawa47005792015-11-20 17:46:29 +09001623static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, int direct)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001624{
Avi Kivity4db35312007-11-21 15:28:32 +02001625 struct kvm_mmu_page *sp;
Takuya Yoshikawa7ddca7e2013-03-21 19:33:43 +09001626
Sean Christopherson94ce87e2020-07-02 19:35:37 -07001627 sp = kvm_mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache);
1628 sp->spt = kvm_mmu_memory_cache_alloc(&vcpu->arch.mmu_shadow_page_cache);
Lai Jiangshan2032a932010-05-26 16:49:59 +08001629 if (!direct)
Sean Christopherson94ce87e2020-07-02 19:35:37 -07001630 sp->gfns = kvm_mmu_memory_cache_alloc(&vcpu->arch.mmu_gfn_array_cache);
Avi Kivity4db35312007-11-21 15:28:32 +02001631 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Sean Christopherson002c5f72019-09-12 19:46:02 -07001632
1633 /*
1634 * active_mmu_pages must be a FIFO list, as kvm_zap_obsolete_pages()
1635 * depends on valid pages being added to the head of the list. See
1636 * comments in kvm_zap_obsolete_pages().
1637 */
Sean Christophersonca333ad2019-09-12 19:46:11 -07001638 sp->mmu_valid_gen = vcpu->kvm->arch.mmu_valid_gen;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001639 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Dave Hansen45221ab2010-08-19 18:11:37 -07001640 kvm_mod_used_mmu_pages(vcpu->kvm, +1);
Avi Kivity4db35312007-11-21 15:28:32 +02001641 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001642}
1643
Xiao Guangrong67052b32011-05-15 23:27:08 +08001644static void mark_unsync(u64 *spte);
Xiao Guangrong6b184932010-04-16 21:29:17 +08001645static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001646{
Takuya Yoshikawa74c4e632015-11-26 21:15:38 +09001647 u64 *sptep;
1648 struct rmap_iterator iter;
1649
1650 for_each_rmap_spte(&sp->parent_ptes, &iter, sptep) {
1651 mark_unsync(sptep);
1652 }
Xiao Guangrong1047df12010-06-11 21:35:15 +08001653}
1654
Xiao Guangrong67052b32011-05-15 23:27:08 +08001655static void mark_unsync(u64 *spte)
Xiao Guangrong1047df12010-06-11 21:35:15 +08001656{
Xiao Guangrong67052b32011-05-15 23:27:08 +08001657 struct kvm_mmu_page *sp;
Xiao Guangrong1047df12010-06-11 21:35:15 +08001658 unsigned int index;
1659
Sean Christopherson57354682020-06-22 13:20:33 -07001660 sp = sptep_to_sp(spte);
Xiao Guangrong1047df12010-06-11 21:35:15 +08001661 index = spte - sp->spt;
1662 if (__test_and_set_bit(index, sp->unsync_child_bitmap))
1663 return;
1664 if (sp->unsync_children++)
1665 return;
1666 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001667}
1668
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001669static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001670 struct kvm_mmu_page *sp)
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001671{
Paolo Bonzini1f50f1b2016-02-24 11:07:14 +01001672 return 0;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001673}
1674
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001675#define KVM_PAGE_ARRAY_NR 16
1676
1677struct kvm_mmu_pages {
1678 struct mmu_page_and_offset {
1679 struct kvm_mmu_page *sp;
1680 unsigned int idx;
1681 } page[KVM_PAGE_ARRAY_NR];
1682 unsigned int nr;
1683};
1684
Hannes Edercded19f2009-02-21 02:19:13 +01001685static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1686 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001687{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001688 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001689
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001690 if (sp->unsync)
1691 for (i=0; i < pvec->nr; i++)
1692 if (pvec->page[i].sp == sp)
1693 return 0;
1694
1695 pvec->page[pvec->nr].sp = sp;
1696 pvec->page[pvec->nr].idx = idx;
1697 pvec->nr++;
1698 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1699}
1700
Takuya Yoshikawafd951452015-11-20 17:43:13 +09001701static inline void clear_unsync_child_bit(struct kvm_mmu_page *sp, int idx)
1702{
1703 --sp->unsync_children;
1704 WARN_ON((int)sp->unsync_children < 0);
1705 __clear_bit(idx, sp->unsync_child_bitmap);
1706}
1707
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001708static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1709 struct kvm_mmu_pages *pvec)
1710{
1711 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001712
Takuya Yoshikawa37178b82011-11-29 14:02:45 +09001713 for_each_set_bit(i, sp->unsync_child_bitmap, 512) {
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001714 struct kvm_mmu_page *child;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001715 u64 ent = sp->spt[i];
1716
Takuya Yoshikawafd951452015-11-20 17:43:13 +09001717 if (!is_shadow_present_pte(ent) || is_large_pte(ent)) {
1718 clear_unsync_child_bit(sp, i);
1719 continue;
1720 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001721
Sean Christophersone47c4ae2020-06-22 13:20:34 -07001722 child = to_shadow_page(ent & PT64_BASE_ADDR_MASK);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001723
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001724 if (child->unsync_children) {
1725 if (mmu_pages_add(pvec, child, i))
1726 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001727
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001728 ret = __mmu_unsync_walk(child, pvec);
Takuya Yoshikawafd951452015-11-20 17:43:13 +09001729 if (!ret) {
1730 clear_unsync_child_bit(sp, i);
1731 continue;
1732 } else if (ret > 0) {
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001733 nr_unsync_leaf += ret;
Takuya Yoshikawafd951452015-11-20 17:43:13 +09001734 } else
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001735 return ret;
1736 } else if (child->unsync) {
1737 nr_unsync_leaf++;
1738 if (mmu_pages_add(pvec, child, i))
1739 return -ENOSPC;
1740 } else
Takuya Yoshikawafd951452015-11-20 17:43:13 +09001741 clear_unsync_child_bit(sp, i);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001742 }
1743
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001744 return nr_unsync_leaf;
1745}
1746
Xiao Guangronge23d3fe2016-02-24 09:46:06 +01001747#define INVALID_INDEX (-1)
1748
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001749static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1750 struct kvm_mmu_pages *pvec)
1751{
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01001752 pvec->nr = 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001753 if (!sp->unsync_children)
1754 return 0;
1755
Xiao Guangronge23d3fe2016-02-24 09:46:06 +01001756 mmu_pages_add(pvec, sp, INVALID_INDEX);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001757 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001758}
1759
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001760static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1761{
1762 WARN_ON(!sp->unsync);
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001763 trace_kvm_mmu_sync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001764 sp->unsync = 0;
1765 --kvm->stat.mmu_unsync;
1766}
1767
Sean Christopherson83cdb562019-02-05 13:01:35 -08001768static bool kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1769 struct list_head *invalid_list);
Xiao Guangrong77758342010-06-04 21:53:54 +08001770static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1771 struct list_head *invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001772
Sean Christophersonac101b72020-06-23 12:40:26 -07001773#define for_each_valid_sp(_kvm, _sp, _list) \
1774 hlist_for_each_entry(_sp, _list, hash_link) \
Sean Christophersonfac026d2019-09-12 19:46:03 -07001775 if (is_obsolete_sp((_kvm), (_sp))) { \
David Matlackf3414bc2016-12-20 15:25:57 -08001776 } else
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001777
Takuya Yoshikawa1044b032013-03-06 16:05:07 +09001778#define for_each_gfn_indirect_valid_sp(_kvm, _sp, _gfn) \
Sean Christophersonac101b72020-06-23 12:40:26 -07001779 for_each_valid_sp(_kvm, _sp, \
1780 &(_kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(_gfn)]) \
David Matlackf3414bc2016-12-20 15:25:57 -08001781 if ((_sp)->gfn != (_gfn) || (_sp)->role.direct) {} else
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001782
Xiao Guangrongf918b442010-06-11 21:30:36 +08001783/* @sp->gfn should be write-protected at the call site */
Paolo Bonzini1f50f1b2016-02-24 11:07:14 +01001784static bool __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1785 struct list_head *invalid_list)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001786{
Sean Christopherson00a66972021-06-22 10:56:55 -07001787 union kvm_mmu_page_role mmu_role = vcpu->arch.mmu->mmu_role.base;
1788
1789 if (sp->role.gpte_is_8_bytes != mmu_role.gpte_is_8_bytes ||
Sean Christopherson47c42e62019-03-07 15:27:44 -08001790 vcpu->arch.mmu->sync_page(vcpu, sp) == 0) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001791 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Paolo Bonzini1f50f1b2016-02-24 11:07:14 +01001792 return false;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001793 }
1794
Paolo Bonzini1f50f1b2016-02-24 11:07:14 +01001795 return true;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001796}
1797
Sean Christophersona2113632019-02-05 13:01:20 -08001798static bool kvm_mmu_remote_flush_or_zap(struct kvm *kvm,
1799 struct list_head *invalid_list,
1800 bool remote_flush)
1801{
Sean Christophersoncfd32ac2019-04-12 19:55:41 -07001802 if (!remote_flush && list_empty(invalid_list))
Sean Christophersona2113632019-02-05 13:01:20 -08001803 return false;
1804
1805 if (!list_empty(invalid_list))
1806 kvm_mmu_commit_zap_page(kvm, invalid_list);
1807 else
1808 kvm_flush_remote_tlbs(kvm);
1809 return true;
1810}
1811
Paolo Bonzini35a70512016-02-24 10:03:27 +01001812static void kvm_mmu_flush_or_zap(struct kvm_vcpu *vcpu,
1813 struct list_head *invalid_list,
1814 bool remote_flush, bool local_flush)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001815{
Sean Christophersona2113632019-02-05 13:01:20 -08001816 if (kvm_mmu_remote_flush_or_zap(vcpu->kvm, invalid_list, remote_flush))
Paolo Bonzini35a70512016-02-24 10:03:27 +01001817 return;
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001818
Sean Christophersona2113632019-02-05 13:01:20 -08001819 if (local_flush)
Sean Christopherson8c8560b2020-03-20 14:28:21 -07001820 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001821}
1822
Xiao Guangronge37fa782011-11-30 17:43:24 +08001823#ifdef CONFIG_KVM_MMU_AUDIT
1824#include "mmu_audit.c"
1825#else
1826static void kvm_mmu_audit(struct kvm_vcpu *vcpu, int point) { }
1827static void mmu_audit_disable(void) { }
1828#endif
1829
Sean Christopherson002c5f72019-09-12 19:46:02 -07001830static bool is_obsolete_sp(struct kvm *kvm, struct kvm_mmu_page *sp)
1831{
Sean Christophersonfac026d2019-09-12 19:46:03 -07001832 return sp->role.invalid ||
1833 unlikely(sp->mmu_valid_gen != kvm->arch.mmu_valid_gen);
Sean Christopherson002c5f72019-09-12 19:46:02 -07001834}
1835
Paolo Bonzini1f50f1b2016-02-24 11:07:14 +01001836static bool kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001837 struct list_head *invalid_list)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001838{
Paolo Bonzini9a43c5d2016-02-24 10:28:01 +01001839 kvm_unlink_unsync_page(vcpu->kvm, sp);
1840 return __kvm_sync_page(vcpu, sp, invalid_list);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001841}
1842
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001843struct mmu_page_path {
Yu Zhang2a7266a2017-08-24 20:27:54 +08001844 struct kvm_mmu_page *parent[PT64_ROOT_MAX_LEVEL];
1845 unsigned int idx[PT64_ROOT_MAX_LEVEL];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001846};
1847
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001848#define for_each_sp(pvec, sp, parents, i) \
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01001849 for (i = mmu_pages_first(&pvec, &parents); \
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001850 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1851 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001852
Hannes Edercded19f2009-02-21 02:19:13 +01001853static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1854 struct mmu_page_path *parents,
1855 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001856{
1857 int n;
1858
1859 for (n = i+1; n < pvec->nr; n++) {
1860 struct kvm_mmu_page *sp = pvec->page[n].sp;
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01001861 unsigned idx = pvec->page[n].idx;
1862 int level = sp->role.level;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001863
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01001864 parents->idx[level-1] = idx;
Sean Christopherson3bae0452020-04-27 17:54:22 -07001865 if (level == PG_LEVEL_4K)
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01001866 break;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001867
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01001868 parents->parent[level-2] = sp;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001869 }
1870
1871 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001872}
1873
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01001874static int mmu_pages_first(struct kvm_mmu_pages *pvec,
1875 struct mmu_page_path *parents)
1876{
1877 struct kvm_mmu_page *sp;
1878 int level;
1879
1880 if (pvec->nr == 0)
1881 return 0;
1882
Xiao Guangronge23d3fe2016-02-24 09:46:06 +01001883 WARN_ON(pvec->page[0].idx != INVALID_INDEX);
1884
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01001885 sp = pvec->page[0].sp;
1886 level = sp->role.level;
Sean Christopherson3bae0452020-04-27 17:54:22 -07001887 WARN_ON(level == PG_LEVEL_4K);
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01001888
1889 parents->parent[level-2] = sp;
1890
1891 /* Also set up a sentinel. Further entries in pvec are all
1892 * children of sp, so this element is never overwritten.
1893 */
1894 parents->parent[level-1] = NULL;
1895 return mmu_pages_next(pvec, parents, 0);
1896}
1897
Hannes Edercded19f2009-02-21 02:19:13 +01001898static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001899{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001900 struct kvm_mmu_page *sp;
1901 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001902
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001903 do {
1904 unsigned int idx = parents->idx[level];
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001905 sp = parents->parent[level];
1906 if (!sp)
1907 return;
1908
Xiao Guangronge23d3fe2016-02-24 09:46:06 +01001909 WARN_ON(idx == INVALID_INDEX);
Takuya Yoshikawafd951452015-11-20 17:43:13 +09001910 clear_unsync_child_bit(sp, idx);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001911 level++;
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01001912 } while (!sp->unsync_children);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001913}
1914
1915static void mmu_sync_children(struct kvm_vcpu *vcpu,
1916 struct kvm_mmu_page *parent)
1917{
1918 int i;
1919 struct kvm_mmu_page *sp;
1920 struct mmu_page_path parents;
1921 struct kvm_mmu_pages pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001922 LIST_HEAD(invalid_list);
Paolo Bonzini50c9e6f2016-02-25 10:47:38 +01001923 bool flush = false;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001924
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001925 while (mmu_unsync_walk(parent, &pages)) {
Xiao Guangrong2f845692012-06-20 15:56:53 +08001926 bool protected = false;
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001927
1928 for_each_sp(pages, sp, parents, i)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02001929 protected |= rmap_write_protect(vcpu, sp->gfn);
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001930
Paolo Bonzini50c9e6f2016-02-25 10:47:38 +01001931 if (protected) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001932 kvm_flush_remote_tlbs(vcpu->kvm);
Paolo Bonzini50c9e6f2016-02-25 10:47:38 +01001933 flush = false;
1934 }
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001935
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001936 for_each_sp(pages, sp, parents, i) {
Paolo Bonzini1f50f1b2016-02-24 11:07:14 +01001937 flush |= kvm_sync_page(vcpu, sp, &invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001938 mmu_pages_clear_parents(&parents);
1939 }
Ben Gardon531810c2021-02-02 10:57:24 -08001940 if (need_resched() || rwlock_needbreak(&vcpu->kvm->mmu_lock)) {
Paolo Bonzini50c9e6f2016-02-25 10:47:38 +01001941 kvm_mmu_flush_or_zap(vcpu, &invalid_list, false, flush);
Ben Gardon531810c2021-02-02 10:57:24 -08001942 cond_resched_rwlock_write(&vcpu->kvm->mmu_lock);
Paolo Bonzini50c9e6f2016-02-25 10:47:38 +01001943 flush = false;
1944 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001945 }
Paolo Bonzini50c9e6f2016-02-25 10:47:38 +01001946
1947 kvm_mmu_flush_or_zap(vcpu, &invalid_list, false, flush);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001948}
1949
Xiao Guangronga30f47c2011-09-22 16:58:36 +08001950static void __clear_sp_write_flooding_count(struct kvm_mmu_page *sp)
1951{
Xiao Guangronge5691a82016-02-24 17:51:12 +08001952 atomic_set(&sp->write_flooding_count, 0);
Xiao Guangronga30f47c2011-09-22 16:58:36 +08001953}
1954
1955static void clear_sp_write_flooding_count(u64 *spte)
1956{
Sean Christopherson57354682020-06-22 13:20:33 -07001957 __clear_sp_write_flooding_count(sptep_to_sp(spte));
Xiao Guangronga30f47c2011-09-22 16:58:36 +08001958}
1959
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001960static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1961 gfn_t gfn,
1962 gva_t gaddr,
1963 unsigned level,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001964 int direct,
Ben Gardon0a2b64c2020-02-03 15:09:09 -08001965 unsigned int access)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001966{
Sean Christophersonfb58a9c2020-06-23 12:40:27 -07001967 bool direct_mmu = vcpu->arch.mmu->direct_map;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001968 union kvm_mmu_page_role role;
Sean Christophersonac101b72020-06-23 12:40:26 -07001969 struct hlist_head *sp_list;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001970 unsigned quadrant;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001971 struct kvm_mmu_page *sp;
David Matlackf3414bc2016-12-20 15:25:57 -08001972 int collisions = 0;
Paolo Bonzini2a74003a2016-02-24 11:26:10 +01001973 LIST_HEAD(invalid_list);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001974
Vitaly Kuznetsov36d9594d2018-10-08 21:28:10 +02001975 role = vcpu->arch.mmu->mmu_role.base;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001976 role.level = level;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001977 role.direct = direct;
Avi Kivity84b0c8c2010-03-14 10:16:40 +02001978 if (role.direct)
Sean Christopherson47c42e62019-03-07 15:27:44 -08001979 role.gpte_is_8_bytes = true;
Avi Kivity41074d02007-12-09 17:00:02 +02001980 role.access = access;
Sean Christophersonfb58a9c2020-06-23 12:40:27 -07001981 if (!direct_mmu && vcpu->arch.mmu->root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001982 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1983 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1984 role.quadrant = quadrant;
1985 }
Sean Christophersonac101b72020-06-23 12:40:26 -07001986
1987 sp_list = &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)];
1988 for_each_valid_sp(vcpu->kvm, sp, sp_list) {
David Matlackf3414bc2016-12-20 15:25:57 -08001989 if (sp->gfn != gfn) {
1990 collisions++;
1991 continue;
1992 }
1993
Sean Christophersonddc16ab2021-06-22 10:56:54 -07001994 if (sp->role.word != role.word) {
1995 /*
1996 * If the guest is creating an upper-level page, zap
1997 * unsync pages for the same gfn. While it's possible
1998 * the guest is using recursive page tables, in all
1999 * likelihood the guest has stopped using the unsync
2000 * page and is installing a completely unrelated page.
2001 * Unsync pages must not be left as is, because the new
2002 * upper-level page will be write-protected.
2003 */
2004 if (level > PG_LEVEL_4K && sp->unsync)
2005 kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
2006 &invalid_list);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002007 continue;
Sean Christophersonddc16ab2021-06-22 10:56:54 -07002008 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002009
Sean Christophersonfb58a9c2020-06-23 12:40:27 -07002010 if (direct_mmu)
2011 goto trace_get_page;
2012
Paolo Bonzini2a74003a2016-02-24 11:26:10 +01002013 if (sp->unsync) {
2014 /* The page is good, but __kvm_sync_page might still end
2015 * up zapping it. If so, break in order to rebuild it.
2016 */
2017 if (!__kvm_sync_page(vcpu, sp, &invalid_list))
2018 break;
2019
2020 WARN_ON(!list_empty(&invalid_list));
Sean Christopherson8c8560b2020-03-20 14:28:21 -07002021 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Paolo Bonzini2a74003a2016-02-24 11:26:10 +01002022 }
Xiao Guangronge02aa902010-05-15 18:52:34 +08002023
Takuya Yoshikawa98bba232015-11-26 21:14:34 +09002024 if (sp->unsync_children)
Lai Jiangshanf6f61952020-09-02 21:54:21 +08002025 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
Xiao Guangronge02aa902010-05-15 18:52:34 +08002026
Xiao Guangronga30f47c2011-09-22 16:58:36 +08002027 __clear_sp_write_flooding_count(sp);
Sean Christophersonfb58a9c2020-06-23 12:40:27 -07002028
2029trace_get_page:
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002030 trace_kvm_mmu_get_page(sp, false);
David Matlackf3414bc2016-12-20 15:25:57 -08002031 goto out;
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002032 }
Takuya Yoshikawa47005792015-11-20 17:46:29 +09002033
Avi Kivitydfc5aa02007-12-18 19:47:18 +02002034 ++vcpu->kvm->stat.mmu_cache_miss;
Takuya Yoshikawa47005792015-11-20 17:46:29 +09002035
2036 sp = kvm_mmu_alloc_page(vcpu, direct);
2037
Avi Kivity4db35312007-11-21 15:28:32 +02002038 sp->gfn = gfn;
2039 sp->role = role;
Sean Christophersonac101b72020-06-23 12:40:26 -07002040 hlist_add_head(&sp->hash_link, sp_list);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002041 if (!direct) {
Paolo Bonzini3ed1a472015-05-19 16:29:22 +02002042 account_shadowed(vcpu->kvm, sp);
Sean Christopherson3bae0452020-04-27 17:54:22 -07002043 if (level == PG_LEVEL_4K && rmap_write_protect(vcpu, gfn))
Lan Tianyuc3134ce2018-12-06 21:21:09 +08002044 kvm_flush_remote_tlbs_with_address(vcpu->kvm, gfn, 1);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002045 }
Avi Kivityf691fe12009-07-06 15:58:14 +03002046 trace_kvm_mmu_get_page(sp, true);
David Matlackf3414bc2016-12-20 15:25:57 -08002047out:
Sean Christophersonddc16ab2021-06-22 10:56:54 -07002048 kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
2049
David Matlackf3414bc2016-12-20 15:25:57 -08002050 if (collisions > vcpu->kvm->stat.max_mmu_page_hash_collisions)
2051 vcpu->kvm->stat.max_mmu_page_hash_collisions = collisions;
Avi Kivity4db35312007-11-21 15:28:32 +02002052 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002053}
2054
Junaid Shahid7eb77e92018-06-27 14:59:16 -07002055static void shadow_walk_init_using_root(struct kvm_shadow_walk_iterator *iterator,
2056 struct kvm_vcpu *vcpu, hpa_t root,
2057 u64 addr)
Avi Kivity2d111232008-12-25 14:39:47 +02002058{
2059 iterator->addr = addr;
Junaid Shahid7eb77e92018-06-27 14:59:16 -07002060 iterator->shadow_addr = root;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02002061 iterator->level = vcpu->arch.mmu->shadow_root_level;
Joerg Roedel81407ca2010-09-10 17:31:00 +02002062
Yu Zhang2a7266a2017-08-24 20:27:54 +08002063 if (iterator->level == PT64_ROOT_4LEVEL &&
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02002064 vcpu->arch.mmu->root_level < PT64_ROOT_4LEVEL &&
2065 !vcpu->arch.mmu->direct_map)
Joerg Roedel81407ca2010-09-10 17:31:00 +02002066 --iterator->level;
2067
Avi Kivity2d111232008-12-25 14:39:47 +02002068 if (iterator->level == PT32E_ROOT_LEVEL) {
Junaid Shahid7eb77e92018-06-27 14:59:16 -07002069 /*
2070 * prev_root is currently only used for 64-bit hosts. So only
2071 * the active root_hpa is valid here.
2072 */
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02002073 BUG_ON(root != vcpu->arch.mmu->root_hpa);
Junaid Shahid7eb77e92018-06-27 14:59:16 -07002074
Avi Kivity2d111232008-12-25 14:39:47 +02002075 iterator->shadow_addr
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02002076 = vcpu->arch.mmu->pae_root[(addr >> 30) & 3];
Avi Kivity2d111232008-12-25 14:39:47 +02002077 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
2078 --iterator->level;
2079 if (!iterator->shadow_addr)
2080 iterator->level = 0;
2081 }
2082}
2083
Junaid Shahid7eb77e92018-06-27 14:59:16 -07002084static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
2085 struct kvm_vcpu *vcpu, u64 addr)
2086{
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02002087 shadow_walk_init_using_root(iterator, vcpu, vcpu->arch.mmu->root_hpa,
Junaid Shahid7eb77e92018-06-27 14:59:16 -07002088 addr);
2089}
2090
Avi Kivity2d111232008-12-25 14:39:47 +02002091static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
2092{
Sean Christopherson3bae0452020-04-27 17:54:22 -07002093 if (iterator->level < PG_LEVEL_4K)
Avi Kivity2d111232008-12-25 14:39:47 +02002094 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03002095
Avi Kivity2d111232008-12-25 14:39:47 +02002096 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
2097 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
2098 return true;
2099}
2100
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08002101static void __shadow_walk_next(struct kvm_shadow_walk_iterator *iterator,
2102 u64 spte)
Avi Kivity2d111232008-12-25 14:39:47 +02002103{
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08002104 if (is_last_spte(spte, iterator->level)) {
Xiao Guangrong052331b2011-07-12 03:21:17 +08002105 iterator->level = 0;
2106 return;
2107 }
2108
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08002109 iterator->shadow_addr = spte & PT64_BASE_ADDR_MASK;
Avi Kivity2d111232008-12-25 14:39:47 +02002110 --iterator->level;
2111}
2112
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08002113static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
2114{
David Hildenbrandbb606a92017-08-24 20:51:23 +02002115 __shadow_walk_next(iterator, *iterator->sptep);
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08002116}
2117
Takuya Yoshikawa98bba232015-11-26 21:14:34 +09002118static void link_shadow_page(struct kvm_vcpu *vcpu, u64 *sptep,
2119 struct kvm_mmu_page *sp)
Avi Kivity32ef26a2010-07-13 14:27:04 +03002120{
2121 u64 spte;
2122
Bandan Dasffb128c2016-07-12 18:18:49 -04002123 BUILD_BUG_ON(VMX_EPT_WRITABLE_MASK != PT_WRITABLE_MASK);
Yang Zhang7a1638c2013-08-05 11:07:13 +03002124
Ben Gardoncc4674d2020-09-25 14:22:48 -07002125 spte = make_nonleaf_spte(sp->spt, sp_ad_disabled(sp));
Xiao Guangrong24db2732013-02-05 15:28:02 +08002126
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08002127 mmu_spte_set(sptep, spte);
Takuya Yoshikawa98bba232015-11-26 21:14:34 +09002128
2129 mmu_page_add_parent_pte(vcpu, sp, sptep);
2130
2131 if (sp->unsync_children || sp->unsync)
2132 mark_unsync(sptep);
Avi Kivity32ef26a2010-07-13 14:27:04 +03002133}
2134
Avi Kivitya357bd22010-07-13 14:27:07 +03002135static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
2136 unsigned direct_access)
2137{
2138 if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
2139 struct kvm_mmu_page *child;
2140
2141 /*
2142 * For the direct sp, if the guest pte's dirty bit
2143 * changed form clean to dirty, it will corrupt the
2144 * sp's access: allow writable in the read-only sp,
2145 * so we should update the spte at this point to get
2146 * a new sp with the correct access.
2147 */
Sean Christophersone47c4ae2020-06-22 13:20:34 -07002148 child = to_shadow_page(*sptep & PT64_BASE_ADDR_MASK);
Avi Kivitya357bd22010-07-13 14:27:07 +03002149 if (child->role.access == direct_access)
2150 return;
2151
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08002152 drop_parent_pte(child, sptep);
Lan Tianyuc3134ce2018-12-06 21:21:09 +08002153 kvm_flush_remote_tlbs_with_address(vcpu->kvm, child->gfn, 1);
Avi Kivitya357bd22010-07-13 14:27:07 +03002154 }
2155}
2156
Ben Gardon2de40852020-09-23 15:14:06 -07002157/* Returns the number of zapped non-leaf child shadow pages. */
2158static int mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,
2159 u64 *spte, struct list_head *invalid_list)
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08002160{
2161 u64 pte;
2162 struct kvm_mmu_page *child;
2163
2164 pte = *spte;
2165 if (is_shadow_present_pte(pte)) {
Xiao Guangrong505aef82011-09-22 16:56:06 +08002166 if (is_last_spte(pte, sp->role.level)) {
Xiao Guangrongc3707952011-07-12 03:28:04 +08002167 drop_spte(kvm, spte);
Xiao Guangrong505aef82011-09-22 16:56:06 +08002168 if (is_large_pte(pte))
2169 --kvm->stat.lpages;
2170 } else {
Sean Christophersone47c4ae2020-06-22 13:20:34 -07002171 child = to_shadow_page(pte & PT64_BASE_ADDR_MASK);
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08002172 drop_parent_pte(child, spte);
Ben Gardon2de40852020-09-23 15:14:06 -07002173
2174 /*
2175 * Recursively zap nested TDP SPs, parentless SPs are
2176 * unlikely to be used again in the near future. This
2177 * avoids retaining a large number of stale nested SPs.
2178 */
2179 if (tdp_enabled && invalid_list &&
2180 child->role.guest_mode && !child->parent_ptes.val)
2181 return kvm_mmu_prepare_zap_page(kvm, child,
2182 invalid_list);
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08002183 }
Sean Christophersonace569e2020-09-23 15:14:05 -07002184 } else if (is_mmio_spte(pte)) {
Xiao Guangrongce88dec2011-07-12 03:33:44 +08002185 mmu_spte_clear_no_track(spte);
Sean Christophersonace569e2020-09-23 15:14:05 -07002186 }
Ben Gardon2de40852020-09-23 15:14:06 -07002187 return 0;
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08002188}
2189
Ben Gardon2de40852020-09-23 15:14:06 -07002190static int kvm_mmu_page_unlink_children(struct kvm *kvm,
2191 struct kvm_mmu_page *sp,
2192 struct list_head *invalid_list)
Avi Kivitya4360362007-01-05 16:36:45 -08002193{
Ben Gardon2de40852020-09-23 15:14:06 -07002194 int zapped = 0;
Avi Kivity697fe2e2007-01-05 16:36:46 -08002195 unsigned i;
Avi Kivity697fe2e2007-01-05 16:36:46 -08002196
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08002197 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
Ben Gardon2de40852020-09-23 15:14:06 -07002198 zapped += mmu_page_zap_pte(kvm, sp, sp->spt + i, invalid_list);
2199
2200 return zapped;
Avi Kivitya4360362007-01-05 16:36:45 -08002201}
2202
Avi Kivity31aa2b42008-07-11 17:59:46 +03002203static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08002204{
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09002205 u64 *sptep;
2206 struct rmap_iterator iter;
Avi Kivitya4360362007-01-05 16:36:45 -08002207
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09002208 while ((sptep = rmap_get_first(&sp->parent_ptes, &iter)))
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09002209 drop_parent_pte(sp, sptep);
Avi Kivity31aa2b42008-07-11 17:59:46 +03002210}
2211
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002212static int mmu_zap_unsync_children(struct kvm *kvm,
Xiao Guangrong77758342010-06-04 21:53:54 +08002213 struct kvm_mmu_page *parent,
2214 struct list_head *invalid_list)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002215{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002216 int i, zapped = 0;
2217 struct mmu_page_path parents;
2218 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002219
Sean Christopherson3bae0452020-04-27 17:54:22 -07002220 if (parent->role.level == PG_LEVEL_4K)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002221 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002222
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002223 while (mmu_unsync_walk(parent, &pages)) {
2224 struct kvm_mmu_page *sp;
2225
2226 for_each_sp(pages, sp, parents, i) {
Xiao Guangrong77758342010-06-04 21:53:54 +08002227 kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002228 mmu_pages_clear_parents(&parents);
Xiao Guangrong77662e02010-04-16 16:34:42 +08002229 zapped++;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002230 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002231 }
2232
2233 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002234}
2235
Sean Christopherson83cdb562019-02-05 13:01:35 -08002236static bool __kvm_mmu_prepare_zap_page(struct kvm *kvm,
2237 struct kvm_mmu_page *sp,
2238 struct list_head *invalid_list,
2239 int *nr_zapped)
Avi Kivity31aa2b42008-07-11 17:59:46 +03002240{
Sean Christopherson83cdb562019-02-05 13:01:35 -08002241 bool list_unstable;
Avi Kivityf691fe12009-07-06 15:58:14 +03002242
Xiao Guangrong77758342010-06-04 21:53:54 +08002243 trace_kvm_mmu_prepare_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03002244 ++kvm->stat.mmu_shadow_zapped;
Sean Christopherson83cdb562019-02-05 13:01:35 -08002245 *nr_zapped = mmu_zap_unsync_children(kvm, sp, invalid_list);
Ben Gardon2de40852020-09-23 15:14:06 -07002246 *nr_zapped += kvm_mmu_page_unlink_children(kvm, sp, invalid_list);
Avi Kivity31aa2b42008-07-11 17:59:46 +03002247 kvm_mmu_unlink_parents(kvm, sp);
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08002248
Sean Christopherson83cdb562019-02-05 13:01:35 -08002249 /* Zapping children means active_mmu_pages has become unstable. */
2250 list_unstable = *nr_zapped;
2251
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002252 if (!sp->role.invalid && !sp->role.direct)
Paolo Bonzini3ed1a472015-05-19 16:29:22 +02002253 unaccount_shadowed(kvm, sp);
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08002254
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002255 if (sp->unsync)
2256 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02002257 if (!sp->root_count) {
Gui Jianfeng54a4f022010-05-05 09:03:49 +08002258 /* Count self */
Sean Christopherson83cdb562019-02-05 13:01:35 -08002259 (*nr_zapped)++;
Sean Christophersonf95eec92020-06-23 12:35:39 -07002260
2261 /*
2262 * Already invalid pages (previously active roots) are not on
2263 * the active page list. See list_del() in the "else" case of
2264 * !sp->root_count.
2265 */
2266 if (sp->role.invalid)
2267 list_add(&sp->link, invalid_list);
2268 else
2269 list_move(&sp->link, invalid_list);
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08002270 kvm_mod_used_mmu_pages(kvm, -1);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002271 } else {
Sean Christophersonf95eec92020-06-23 12:35:39 -07002272 /*
2273 * Remove the active root from the active page list, the root
2274 * will be explicitly freed when the root_count hits zero.
2275 */
2276 list_del(&sp->link);
Gleb Natapov05988d72013-05-31 08:36:30 +08002277
Sean Christopherson10605202019-09-12 19:46:10 -07002278 /*
2279 * Obsolete pages cannot be used on any vCPUs, see the comment
2280 * in kvm_mmu_zap_all_fast(). Note, is_obsolete_sp() also
2281 * treats invalid shadow pages as being obsolete.
2282 */
2283 if (!is_obsolete_sp(kvm, sp))
Gleb Natapov05988d72013-05-31 08:36:30 +08002284 kvm_reload_remote_mmus(kvm);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002285 }
Xiao Guangrong77758342010-06-04 21:53:54 +08002286
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01002287 if (sp->lpage_disallowed)
2288 unaccount_huge_nx_page(kvm, sp);
2289
Xiao Guangrong77758342010-06-04 21:53:54 +08002290 sp->role.invalid = 1;
Sean Christopherson83cdb562019-02-05 13:01:35 -08002291 return list_unstable;
2292}
2293
2294static bool kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
2295 struct list_head *invalid_list)
2296{
2297 int nr_zapped;
2298
2299 __kvm_mmu_prepare_zap_page(kvm, sp, invalid_list, &nr_zapped);
2300 return nr_zapped;
Avi Kivitya4360362007-01-05 16:36:45 -08002301}
2302
Xiao Guangrong77758342010-06-04 21:53:54 +08002303static void kvm_mmu_commit_zap_page(struct kvm *kvm,
2304 struct list_head *invalid_list)
2305{
Takuya Yoshikawa945315b2013-03-06 16:05:52 +09002306 struct kvm_mmu_page *sp, *nsp;
Xiao Guangrong77758342010-06-04 21:53:54 +08002307
2308 if (list_empty(invalid_list))
2309 return;
2310
Avi Kivityc1427862012-05-14 15:44:06 +03002311 /*
Lan Tianyu9753f522016-03-13 11:10:24 +08002312 * We need to make sure everyone sees our modifications to
2313 * the page tables and see changes to vcpu->mode here. The barrier
2314 * in the kvm_flush_remote_tlbs() achieves this. This pairs
2315 * with vcpu_enter_guest and walk_shadow_page_lockless_begin/end.
2316 *
2317 * In addition, kvm_flush_remote_tlbs waits for all vcpus to exit
2318 * guest mode and/or lockless shadow page table walks.
Avi Kivityc1427862012-05-14 15:44:06 +03002319 */
Xiao Guangrong77758342010-06-04 21:53:54 +08002320 kvm_flush_remote_tlbs(kvm);
2321
Takuya Yoshikawa945315b2013-03-06 16:05:52 +09002322 list_for_each_entry_safe(sp, nsp, invalid_list, link) {
Xiao Guangrong77758342010-06-04 21:53:54 +08002323 WARN_ON(!sp->role.invalid || sp->root_count);
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08002324 kvm_mmu_free_page(sp);
Takuya Yoshikawa945315b2013-03-06 16:05:52 +09002325 }
Xiao Guangrong77758342010-06-04 21:53:54 +08002326}
2327
Sean Christopherson6b82ef22020-06-23 12:35:40 -07002328static unsigned long kvm_mmu_zap_oldest_mmu_pages(struct kvm *kvm,
2329 unsigned long nr_to_zap)
Takuya Yoshikawa5da59602013-03-06 16:06:58 +09002330{
Sean Christopherson6b82ef22020-06-23 12:35:40 -07002331 unsigned long total_zapped = 0;
2332 struct kvm_mmu_page *sp, *tmp;
Sean Christophersonba7888d2019-12-06 15:57:15 -08002333 LIST_HEAD(invalid_list);
Sean Christopherson6b82ef22020-06-23 12:35:40 -07002334 bool unstable;
2335 int nr_zapped;
Takuya Yoshikawa5da59602013-03-06 16:06:58 +09002336
2337 if (list_empty(&kvm->arch.active_mmu_pages))
Sean Christophersonba7888d2019-12-06 15:57:15 -08002338 return 0;
Takuya Yoshikawa5da59602013-03-06 16:06:58 +09002339
Sean Christopherson6b82ef22020-06-23 12:35:40 -07002340restart:
Sean Christopherson8fc51722021-01-13 12:50:30 -08002341 list_for_each_entry_safe_reverse(sp, tmp, &kvm->arch.active_mmu_pages, link) {
Sean Christopherson6b82ef22020-06-23 12:35:40 -07002342 /*
2343 * Don't zap active root pages, the page itself can't be freed
2344 * and zapping it will just force vCPUs to realloc and reload.
2345 */
2346 if (sp->root_count)
2347 continue;
2348
2349 unstable = __kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list,
2350 &nr_zapped);
2351 total_zapped += nr_zapped;
2352 if (total_zapped >= nr_to_zap)
Sean Christophersonba7888d2019-12-06 15:57:15 -08002353 break;
2354
Sean Christopherson6b82ef22020-06-23 12:35:40 -07002355 if (unstable)
2356 goto restart;
Sean Christophersonba7888d2019-12-06 15:57:15 -08002357 }
Sean Christopherson6b82ef22020-06-23 12:35:40 -07002358
2359 kvm_mmu_commit_zap_page(kvm, &invalid_list);
2360
2361 kvm->stat.mmu_recycled += total_zapped;
2362 return total_zapped;
2363}
2364
Sean Christophersonafe8d7e2020-06-22 13:20:30 -07002365static inline unsigned long kvm_mmu_available_pages(struct kvm *kvm)
2366{
2367 if (kvm->arch.n_max_mmu_pages > kvm->arch.n_used_mmu_pages)
2368 return kvm->arch.n_max_mmu_pages -
2369 kvm->arch.n_used_mmu_pages;
2370
2371 return 0;
Takuya Yoshikawa5da59602013-03-06 16:06:58 +09002372}
2373
Sean Christophersonba7888d2019-12-06 15:57:15 -08002374static int make_mmu_pages_available(struct kvm_vcpu *vcpu)
2375{
Sean Christopherson6b82ef22020-06-23 12:35:40 -07002376 unsigned long avail = kvm_mmu_available_pages(vcpu->kvm);
Sean Christophersonba7888d2019-12-06 15:57:15 -08002377
Sean Christopherson6b82ef22020-06-23 12:35:40 -07002378 if (likely(avail >= KVM_MIN_FREE_MMU_PAGES))
Sean Christophersonba7888d2019-12-06 15:57:15 -08002379 return 0;
2380
Sean Christopherson6b82ef22020-06-23 12:35:40 -07002381 kvm_mmu_zap_oldest_mmu_pages(vcpu->kvm, KVM_REFILL_PAGES - avail);
Sean Christophersonba7888d2019-12-06 15:57:15 -08002382
Sean Christopherson6e6ec582021-03-04 17:10:50 -08002383 /*
2384 * Note, this check is intentionally soft, it only guarantees that one
2385 * page is available, while the caller may end up allocating as many as
2386 * four pages, e.g. for PAE roots or for 5-level paging. Temporarily
2387 * exceeding the (arbitrary by default) limit will not harm the host,
2388 * being too agressive may unnecessarily kill the guest, and getting an
2389 * exact count is far more trouble than it's worth, especially in the
2390 * page fault paths.
2391 */
Sean Christophersonba7888d2019-12-06 15:57:15 -08002392 if (!kvm_mmu_available_pages(vcpu->kvm))
2393 return -ENOSPC;
2394 return 0;
2395}
2396
Izik Eidus82ce2c92007-10-02 18:52:55 +02002397/*
2398 * Changing the number of mmu pages allocated to the vm
Dave Hansen49d5ca22010-08-19 18:11:28 -07002399 * Note: if goal_nr_mmu_pages is too small, you will get dead lock
Izik Eidus82ce2c92007-10-02 18:52:55 +02002400 */
Ben Gardonbc8a3d82019-04-08 11:07:30 -07002401void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned long goal_nr_mmu_pages)
Izik Eidus82ce2c92007-10-02 18:52:55 +02002402{
Ben Gardon531810c2021-02-02 10:57:24 -08002403 write_lock(&kvm->mmu_lock);
Takuya Yoshikawab34cb592013-01-08 19:46:07 +09002404
Dave Hansen49d5ca22010-08-19 18:11:28 -07002405 if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) {
Sean Christopherson6b82ef22020-06-23 12:35:40 -07002406 kvm_mmu_zap_oldest_mmu_pages(kvm, kvm->arch.n_used_mmu_pages -
2407 goal_nr_mmu_pages);
Izik Eidus82ce2c92007-10-02 18:52:55 +02002408
Dave Hansen49d5ca22010-08-19 18:11:28 -07002409 goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002410 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02002411
Dave Hansen49d5ca22010-08-19 18:11:28 -07002412 kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages;
Takuya Yoshikawab34cb592013-01-08 19:46:07 +09002413
Ben Gardon531810c2021-02-02 10:57:24 -08002414 write_unlock(&kvm->mmu_lock);
Izik Eidus82ce2c92007-10-02 18:52:55 +02002415}
2416
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08002417int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08002418{
Avi Kivity4db35312007-11-21 15:28:32 +02002419 struct kvm_mmu_page *sp;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002420 LIST_HEAD(invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08002421 int r;
2422
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002423 pgprintk("%s: looking for gfn %llx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08002424 r = 0;
Ben Gardon531810c2021-02-02 10:57:24 -08002425 write_lock(&kvm->mmu_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002426 for_each_gfn_indirect_valid_sp(kvm, sp, gfn) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002427 pgprintk("%s: gfn %llx role %x\n", __func__, gfn,
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002428 sp->role.word);
2429 r = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002430 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002431 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002432 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Ben Gardon531810c2021-02-02 10:57:24 -08002433 write_unlock(&kvm->mmu_lock);
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08002434
Avi Kivitya4360362007-01-05 16:36:45 -08002435 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002436}
Sean Christopherson96ad91a2021-02-12 16:50:15 -08002437
2438static int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2439{
2440 gpa_t gpa;
2441 int r;
2442
2443 if (vcpu->arch.mmu->direct_map)
2444 return 0;
2445
2446 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
2447
2448 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
2449
2450 return r;
2451}
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002452
Xiao Guangrong5c520e92016-02-24 17:51:15 +08002453static void kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002454{
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08002455 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002456 ++vcpu->kvm->stat.mmu_unsync;
2457 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02002458
Xiao Guangrong6b184932010-04-16 21:29:17 +08002459 kvm_mmu_mark_parents_unsync(sp);
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002460}
2461
Paolo Bonzini5a9624a2020-10-16 10:29:37 -04002462bool mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
2463 bool can_unsync)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002464{
Xiao Guangrong5c520e92016-02-24 17:51:15 +08002465 struct kvm_mmu_page *sp;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002466
Xiao Guangrong3d0c27a2016-02-24 17:51:11 +08002467 if (kvm_page_track_is_active(vcpu, gfn, KVM_PAGE_TRACK_WRITE))
2468 return true;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002469
Xiao Guangrong5c520e92016-02-24 17:51:15 +08002470 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08002471 if (!can_unsync)
Xiao Guangrong3d0c27a2016-02-24 17:51:11 +08002472 return true;
Xiao Guangrong36a2e672010-06-30 16:02:02 +08002473
Xiao Guangrong5c520e92016-02-24 17:51:15 +08002474 if (sp->unsync)
2475 continue;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002476
Sean Christopherson3bae0452020-04-27 17:54:22 -07002477 WARN_ON(sp->role.level != PG_LEVEL_4K);
Xiao Guangrong5c520e92016-02-24 17:51:15 +08002478 kvm_unsync_page(vcpu, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002479 }
Xiao Guangrong3d0c27a2016-02-24 17:51:11 +08002480
Junaid Shahid578e1c42018-06-27 14:59:05 -07002481 /*
2482 * We need to ensure that the marking of unsync pages is visible
2483 * before the SPTE is updated to allow writes because
2484 * kvm_mmu_sync_roots() checks the unsync flags without holding
2485 * the MMU lock and so can race with this. If the SPTE was updated
2486 * before the page had been marked as unsync-ed, something like the
2487 * following could happen:
2488 *
2489 * CPU 1 CPU 2
2490 * ---------------------------------------------------------------------
2491 * 1.2 Host updates SPTE
2492 * to be writable
2493 * 2.1 Guest writes a GPTE for GVA X.
2494 * (GPTE being in the guest page table shadowed
2495 * by the SP from CPU 1.)
2496 * This reads SPTE during the page table walk.
2497 * Since SPTE.W is read as 1, there is no
2498 * fault.
2499 *
2500 * 2.2 Guest issues TLB flush.
2501 * That causes a VM Exit.
2502 *
2503 * 2.3 kvm_mmu_sync_pages() reads sp->unsync.
2504 * Since it is false, so it just returns.
2505 *
2506 * 2.4 Guest accesses GVA X.
2507 * Since the mapping in the SP was not updated,
2508 * so the old mapping for GVA X incorrectly
2509 * gets used.
2510 * 1.1 Host marks SP
2511 * as unsync
2512 * (sp->unsync = true)
2513 *
2514 * The write barrier below ensures that 1.1 happens before 1.2 and thus
2515 * the situation in 2.4 does not arise. The implicit barrier in 2.2
2516 * pairs with this write barrier.
2517 */
2518 smp_wmb();
2519
Xiao Guangrong3d0c27a2016-02-24 17:51:11 +08002520 return false;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002521}
2522
Ben Gardon799a4192020-10-14 20:26:41 +02002523static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
2524 unsigned int pte_access, int level,
2525 gfn_t gfn, kvm_pfn_t pfn, bool speculative,
2526 bool can_unsync, bool host_writable)
2527{
2528 u64 spte;
2529 struct kvm_mmu_page *sp;
2530 int ret;
2531
Ben Gardon799a4192020-10-14 20:26:41 +02002532 sp = sptep_to_sp(sptep);
2533
2534 ret = make_spte(vcpu, pte_access, level, gfn, pfn, *sptep, speculative,
2535 can_unsync, host_writable, sp_ad_disabled(sp), &spte);
2536
2537 if (spte & PT_WRITABLE_MASK)
2538 kvm_vcpu_mark_page_dirty(vcpu, gfn);
2539
Sean Christopherson12703752020-09-23 15:04:25 -07002540 if (*sptep == spte)
2541 ret |= SET_SPTE_SPURIOUS;
2542 else if (mmu_spte_update(sptep, spte))
Junaid Shahid5ce47862018-06-27 14:59:04 -07002543 ret |= SET_SPTE_NEED_REMOTE_TLB_FLUSH;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002544 return ret;
2545}
2546
Ben Gardon0a2b64c2020-02-03 15:09:09 -08002547static int mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Sean Christophersone88b8092020-09-23 11:37:35 -07002548 unsigned int pte_access, bool write_fault, int level,
Ben Gardon0a2b64c2020-02-03 15:09:09 -08002549 gfn_t gfn, kvm_pfn_t pfn, bool speculative,
2550 bool host_writable)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002551{
2552 int was_rmapped = 0;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002553 int rmap_count;
Junaid Shahid5ce47862018-06-27 14:59:04 -07002554 int set_spte_ret;
Sean Christophersonc4371c22020-09-23 15:04:24 -07002555 int ret = RET_PF_FIXED;
Tianyu Lanc2a4ead2018-07-24 08:17:07 +00002556 bool flush = false;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002557
Xiao Guangrongf7616202013-02-05 15:27:27 +08002558 pgprintk("%s: spte %llx write_fault %d gfn %llx\n", __func__,
2559 *sptep, write_fault, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002560
Sean Christophersona54aa152021-02-25 12:47:32 -08002561 if (unlikely(is_noslot_pfn(pfn))) {
2562 mark_mmio_spte(vcpu, sptep, gfn, pte_access);
2563 return RET_PF_EMULATE;
2564 }
2565
Takuya Yoshikawaafd28fe2015-11-20 17:44:55 +09002566 if (is_shadow_present_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002567 /*
2568 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
2569 * the parent of the now unreachable PTE.
2570 */
Sean Christopherson3bae0452020-04-27 17:54:22 -07002571 if (level > PG_LEVEL_4K && !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002572 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03002573 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002574
Sean Christophersone47c4ae2020-06-22 13:20:34 -07002575 child = to_shadow_page(pte & PT64_BASE_ADDR_MASK);
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08002576 drop_parent_pte(child, sptep);
Tianyu Lanc2a4ead2018-07-24 08:17:07 +00002577 flush = true;
Avi Kivityd555c332009-06-10 14:24:23 +03002578 } else if (pfn != spte_to_pfn(*sptep)) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002579 pgprintk("hfn old %llx new %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002580 spte_to_pfn(*sptep), pfn);
Xiao Guangrongc3707952011-07-12 03:28:04 +08002581 drop_spte(vcpu->kvm, sptep);
Tianyu Lanc2a4ead2018-07-24 08:17:07 +00002582 flush = true;
Joerg Roedel6bed6b92009-02-18 14:08:59 +01002583 } else
2584 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002585 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02002586
Junaid Shahid5ce47862018-06-27 14:59:04 -07002587 set_spte_ret = set_spte(vcpu, sptep, pte_access, level, gfn, pfn,
2588 speculative, true, host_writable);
2589 if (set_spte_ret & SET_SPTE_WRITE_PROTECTED_PT) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002590 if (write_fault)
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02002591 ret = RET_PF_EMULATE;
Sean Christopherson8c8560b2020-03-20 14:28:21 -07002592 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03002593 }
Lan Tianyuc3134ce2018-12-06 21:21:09 +08002594
Tianyu Lanc2a4ead2018-07-24 08:17:07 +00002595 if (set_spte_ret & SET_SPTE_NEED_REMOTE_TLB_FLUSH || flush)
Lan Tianyuc3134ce2018-12-06 21:21:09 +08002596 kvm_flush_remote_tlbs_with_address(vcpu->kvm, gfn,
2597 KVM_PAGES_PER_HPAGE(level));
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002598
Sean Christopherson12703752020-09-23 15:04:25 -07002599 /*
2600 * The fault is fully spurious if and only if the new SPTE and old SPTE
2601 * are identical, and emulation is not required.
2602 */
2603 if ((set_spte_ret & SET_SPTE_SPURIOUS) && ret == RET_PF_FIXED) {
2604 WARN_ON_ONCE(!was_rmapped);
2605 return RET_PF_SPURIOUS;
2606 }
2607
Avi Kivityd555c332009-06-10 14:24:23 +03002608 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Paolo Bonzini335e1922019-07-01 06:22:57 -04002609 trace_kvm_mmu_set_spte(level, gfn, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03002610 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002611 ++vcpu->kvm->stat.lpages;
2612
Xiao Guangrongffb61bb2011-07-12 03:22:01 +08002613 if (is_shadow_present_pte(*sptep)) {
Xiao Guangrongffb61bb2011-07-12 03:22:01 +08002614 if (!was_rmapped) {
2615 rmap_count = rmap_add(vcpu, sptep, gfn);
2616 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
2617 rmap_recycle(vcpu, sptep, gfn);
2618 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002619 }
Xiao Guangrongcb9aaa32012-08-03 15:42:10 +08002620
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02002621 return ret;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002622}
2623
Dan Williamsba049e92016-01-15 16:56:11 -08002624static kvm_pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn,
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002625 bool no_dirty_log)
2626{
2627 struct kvm_memory_slot *slot;
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002628
Xiao Guangrong5d163b12011-03-09 15:43:00 +08002629 slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, no_dirty_log);
Xiao Guangrong903816f2012-07-17 21:54:11 +08002630 if (!slot)
Xiao Guangrong6c8ee572012-08-03 15:37:54 +08002631 return KVM_PFN_ERR_FAULT;
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002632
Xiao Guangrong037d92d2012-08-21 10:59:12 +08002633 return gfn_to_pfn_memslot_atomic(slot, gfn);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002634}
2635
2636static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,
2637 struct kvm_mmu_page *sp,
2638 u64 *start, u64 *end)
2639{
2640 struct page *pages[PTE_PREFETCH_NUM];
Paolo Bonzinid9ef13c2015-05-19 16:01:50 +02002641 struct kvm_memory_slot *slot;
Ben Gardon0a2b64c2020-02-03 15:09:09 -08002642 unsigned int access = sp->role.access;
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002643 int i, ret;
2644 gfn_t gfn;
2645
2646 gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt);
Paolo Bonzinid9ef13c2015-05-19 16:01:50 +02002647 slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, access & ACC_WRITE_MASK);
2648 if (!slot)
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002649 return -1;
2650
Paolo Bonzinid9ef13c2015-05-19 16:01:50 +02002651 ret = gfn_to_page_many_atomic(slot, gfn, pages, end - start);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002652 if (ret <= 0)
2653 return -1;
2654
Junaid Shahid43fdcda2019-01-03 16:22:21 -08002655 for (i = 0; i < ret; i++, gfn++, start++) {
Sean Christophersone88b8092020-09-23 11:37:35 -07002656 mmu_set_spte(vcpu, start, access, false, sp->role.level, gfn,
Takuya Yoshikawa029499b2015-11-20 17:44:05 +09002657 page_to_pfn(pages[i]), true, true);
Junaid Shahid43fdcda2019-01-03 16:22:21 -08002658 put_page(pages[i]);
2659 }
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002660
2661 return 0;
2662}
2663
2664static void __direct_pte_prefetch(struct kvm_vcpu *vcpu,
2665 struct kvm_mmu_page *sp, u64 *sptep)
2666{
2667 u64 *spte, *start = NULL;
2668 int i;
2669
2670 WARN_ON(!sp->role.direct);
2671
2672 i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
2673 spte = sp->spt + i;
2674
2675 for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
Xiao Guangrongc3707952011-07-12 03:28:04 +08002676 if (is_shadow_present_pte(*spte) || spte == sptep) {
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002677 if (!start)
2678 continue;
2679 if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0)
2680 break;
2681 start = NULL;
2682 } else if (!start)
2683 start = spte;
2684 }
2685}
2686
2687static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)
2688{
2689 struct kvm_mmu_page *sp;
2690
Sean Christopherson57354682020-06-22 13:20:33 -07002691 sp = sptep_to_sp(sptep);
Peter Feinerac8d57e2017-06-30 17:26:31 -07002692
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002693 /*
Peter Feinerac8d57e2017-06-30 17:26:31 -07002694 * Without accessed bits, there's no way to distinguish between
2695 * actually accessed translations and prefetched, so disable pte
2696 * prefetch if accessed bits aren't available.
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002697 */
Peter Feinerac8d57e2017-06-30 17:26:31 -07002698 if (sp_ad_disabled(sp))
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002699 return;
2700
Sean Christopherson3bae0452020-04-27 17:54:22 -07002701 if (sp->role.level > PG_LEVEL_4K)
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002702 return;
2703
David Stevens4a42d842021-02-22 11:45:22 +09002704 /*
2705 * If addresses are being invalidated, skip prefetching to avoid
2706 * accidentally prefetching those addresses.
2707 */
2708 if (unlikely(vcpu->kvm->mmu_notifier_count))
2709 return;
2710
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002711 __direct_pte_prefetch(vcpu, sp, sptep);
2712}
2713
Sean Christopherson1b6d9d92021-02-12 16:50:04 -08002714static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn,
Ben Gardon8ca6f062021-04-01 16:37:24 -07002715 const struct kvm_memory_slot *slot)
Sean Christophersondb543212020-01-08 12:24:41 -08002716{
Sean Christophersondb543212020-01-08 12:24:41 -08002717 unsigned long hva;
2718 pte_t *pte;
2719 int level;
2720
Sean Christophersone8512652020-01-08 12:24:48 -08002721 if (!PageCompound(pfn_to_page(pfn)) && !kvm_is_zone_device_pfn(pfn))
Sean Christopherson3bae0452020-04-27 17:54:22 -07002722 return PG_LEVEL_4K;
Sean Christophersondb543212020-01-08 12:24:41 -08002723
Sean Christopherson293e3062020-01-08 12:24:46 -08002724 /*
2725 * Note, using the already-retrieved memslot and __gfn_to_hva_memslot()
2726 * is not solely for performance, it's also necessary to avoid the
2727 * "writable" check in __gfn_to_hva_many(), which will always fail on
2728 * read-only memslots due to gfn_to_hva() assuming writes. Earlier
2729 * page fault steps have already verified the guest isn't writing a
2730 * read-only memslot.
2731 */
Sean Christophersondb543212020-01-08 12:24:41 -08002732 hva = __gfn_to_hva_memslot(slot, gfn);
2733
Sean Christopherson1b6d9d92021-02-12 16:50:04 -08002734 pte = lookup_address_in_mm(kvm->mm, hva, &level);
Sean Christophersondb543212020-01-08 12:24:41 -08002735 if (unlikely(!pte))
Sean Christopherson3bae0452020-04-27 17:54:22 -07002736 return PG_LEVEL_4K;
Sean Christophersondb543212020-01-08 12:24:41 -08002737
2738 return level;
2739}
2740
Ben Gardon8ca6f062021-04-01 16:37:24 -07002741int kvm_mmu_max_mapping_level(struct kvm *kvm,
2742 const struct kvm_memory_slot *slot, gfn_t gfn,
2743 kvm_pfn_t pfn, int max_level)
Sean Christopherson1b6d9d92021-02-12 16:50:04 -08002744{
2745 struct kvm_lpage_info *linfo;
2746
2747 max_level = min(max_level, max_huge_page_level);
2748 for ( ; max_level > PG_LEVEL_4K; max_level--) {
2749 linfo = lpage_info_slot(gfn, slot, max_level);
2750 if (!linfo->disallow_lpage)
2751 break;
2752 }
2753
2754 if (max_level == PG_LEVEL_4K)
2755 return PG_LEVEL_4K;
2756
2757 return host_pfn_mapping_level(kvm, gfn, pfn, slot);
2758}
2759
Ben Gardonbb188422020-10-14 11:26:50 -07002760int kvm_mmu_hugepage_adjust(struct kvm_vcpu *vcpu, gfn_t gfn,
2761 int max_level, kvm_pfn_t *pfnp,
2762 bool huge_page_disallowed, int *req_level)
Sean Christopherson08859042019-12-06 15:57:25 -08002763{
Sean Christopherson293e3062020-01-08 12:24:46 -08002764 struct kvm_memory_slot *slot;
Sean Christopherson08859042019-12-06 15:57:25 -08002765 kvm_pfn_t pfn = *pfnp;
Sean Christopherson17eff012020-01-08 12:24:40 -08002766 kvm_pfn_t mask;
Sean Christopherson83f06fa2020-01-08 12:24:43 -08002767 int level;
Sean Christopherson17eff012020-01-08 12:24:40 -08002768
Sean Christopherson3cf06612020-09-23 11:37:31 -07002769 *req_level = PG_LEVEL_4K;
2770
Sean Christopherson3bae0452020-04-27 17:54:22 -07002771 if (unlikely(max_level == PG_LEVEL_4K))
2772 return PG_LEVEL_4K;
Sean Christopherson17eff012020-01-08 12:24:40 -08002773
Sean Christophersone8512652020-01-08 12:24:48 -08002774 if (is_error_noslot_pfn(pfn) || kvm_is_reserved_pfn(pfn))
Sean Christopherson3bae0452020-04-27 17:54:22 -07002775 return PG_LEVEL_4K;
Sean Christopherson17eff012020-01-08 12:24:40 -08002776
Sean Christopherson293e3062020-01-08 12:24:46 -08002777 slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, true);
2778 if (!slot)
Sean Christopherson3bae0452020-04-27 17:54:22 -07002779 return PG_LEVEL_4K;
Sean Christopherson293e3062020-01-08 12:24:46 -08002780
Sean Christopherson1b6d9d92021-02-12 16:50:04 -08002781 level = kvm_mmu_max_mapping_level(vcpu->kvm, slot, gfn, pfn, max_level);
Sean Christopherson3bae0452020-04-27 17:54:22 -07002782 if (level == PG_LEVEL_4K)
Sean Christopherson83f06fa2020-01-08 12:24:43 -08002783 return level;
Sean Christopherson17eff012020-01-08 12:24:40 -08002784
Sean Christopherson3cf06612020-09-23 11:37:31 -07002785 *req_level = level = min(level, max_level);
2786
2787 /*
2788 * Enforce the iTLB multihit workaround after capturing the requested
2789 * level, which will be used to do precise, accurate accounting.
2790 */
2791 if (huge_page_disallowed)
2792 return PG_LEVEL_4K;
Sean Christopherson08859042019-12-06 15:57:25 -08002793
2794 /*
Sean Christopherson17eff012020-01-08 12:24:40 -08002795 * mmu_notifier_retry() was successful and mmu_lock is held, so
2796 * the pmd can't be split from under us.
Sean Christopherson08859042019-12-06 15:57:25 -08002797 */
Sean Christopherson17eff012020-01-08 12:24:40 -08002798 mask = KVM_PAGES_PER_HPAGE(level) - 1;
2799 VM_BUG_ON((gfn & mask) != (pfn & mask));
2800 *pfnp = pfn & ~mask;
Sean Christopherson83f06fa2020-01-08 12:24:43 -08002801
2802 return level;
Sean Christopherson08859042019-12-06 15:57:25 -08002803}
2804
Ben Gardonbb188422020-10-14 11:26:50 -07002805void disallowed_hugepage_adjust(u64 spte, gfn_t gfn, int cur_level,
2806 kvm_pfn_t *pfnp, int *goal_levelp)
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01002807{
Ben Gardonbb188422020-10-14 11:26:50 -07002808 int level = *goal_levelp;
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01002809
Ben Gardon7d945312020-10-14 11:26:49 -07002810 if (cur_level == level && level > PG_LEVEL_4K &&
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01002811 is_shadow_present_pte(spte) &&
2812 !is_large_pte(spte)) {
2813 /*
2814 * A small SPTE exists for this pfn, but FNAME(fetch)
2815 * and __direct_map would like to create a large PTE
2816 * instead: just force them to go down another level,
2817 * patching back for them into pfn the next 9 bits of
2818 * the address.
2819 */
Ben Gardon7d945312020-10-14 11:26:49 -07002820 u64 page_mask = KVM_PAGES_PER_HPAGE(level) -
2821 KVM_PAGES_PER_HPAGE(level - 1);
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01002822 *pfnp |= gfn & page_mask;
Ben Gardonbb188422020-10-14 11:26:50 -07002823 (*goal_levelp)--;
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01002824 }
2825}
2826
Sean Christopherson6c2fd342020-09-23 11:37:30 -07002827static int __direct_map(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code,
Sean Christopherson83f06fa2020-01-08 12:24:43 -08002828 int map_writable, int max_level, kvm_pfn_t pfn,
Sean Christopherson6c2fd342020-09-23 11:37:30 -07002829 bool prefault, bool is_tdp)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002830{
Sean Christopherson6c2fd342020-09-23 11:37:30 -07002831 bool nx_huge_page_workaround_enabled = is_nx_huge_page_enabled();
2832 bool write = error_code & PFERR_WRITE_MASK;
2833 bool exec = error_code & PFERR_FETCH_MASK;
2834 bool huge_page_disallowed = exec && nx_huge_page_workaround_enabled;
Paolo Bonzini3fcf2d12019-06-24 13:06:21 +02002835 struct kvm_shadow_walk_iterator it;
Avi Kivity9f652d212008-12-25 14:54:25 +02002836 struct kvm_mmu_page *sp;
Sean Christopherson3cf06612020-09-23 11:37:31 -07002837 int level, req_level, ret;
Paolo Bonzini3fcf2d12019-06-24 13:06:21 +02002838 gfn_t gfn = gpa >> PAGE_SHIFT;
2839 gfn_t base_gfn = gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002840
Sean Christopherson3cf06612020-09-23 11:37:31 -07002841 level = kvm_mmu_hugepage_adjust(vcpu, gfn, max_level, &pfn,
2842 huge_page_disallowed, &req_level);
Sean Christopherson4cd071d2019-12-06 15:57:26 -08002843
Paolo Bonzini335e1922019-07-01 06:22:57 -04002844 trace_kvm_mmu_spte_requested(gpa, level, pfn);
Paolo Bonzini3fcf2d12019-06-24 13:06:21 +02002845 for_each_shadow_entry(vcpu, gpa, it) {
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01002846 /*
2847 * We cannot overwrite existing page tables with an NX
2848 * large page, as the leaf could be executable.
2849 */
Sean Christophersondcc70652020-09-23 11:37:34 -07002850 if (nx_huge_page_workaround_enabled)
Ben Gardon7d945312020-10-14 11:26:49 -07002851 disallowed_hugepage_adjust(*it.sptep, gfn, it.level,
2852 &pfn, &level);
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01002853
Paolo Bonzini3fcf2d12019-06-24 13:06:21 +02002854 base_gfn = gfn & ~(KVM_PAGES_PER_HPAGE(it.level) - 1);
2855 if (it.level == level)
Avi Kivity9f652d212008-12-25 14:54:25 +02002856 break;
Avi Kivity9f652d212008-12-25 14:54:25 +02002857
Paolo Bonzini3fcf2d12019-06-24 13:06:21 +02002858 drop_large_spte(vcpu, it.sptep);
2859 if (!is_shadow_present_pte(*it.sptep)) {
2860 sp = kvm_mmu_get_page(vcpu, base_gfn, it.addr,
2861 it.level - 1, true, ACC_ALL);
Lai Jiangshanc9fa0b32010-05-26 16:48:25 +08002862
Paolo Bonzini3fcf2d12019-06-24 13:06:21 +02002863 link_shadow_page(vcpu, it.sptep, sp);
Sean Christopherson5bcaf3e2020-09-23 11:37:32 -07002864 if (is_tdp && huge_page_disallowed &&
2865 req_level >= it.level)
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01002866 account_huge_nx_page(vcpu->kvm, sp);
Avi Kivity9f652d212008-12-25 14:54:25 +02002867 }
2868 }
Paolo Bonzini3fcf2d12019-06-24 13:06:21 +02002869
2870 ret = mmu_set_spte(vcpu, it.sptep, ACC_ALL,
2871 write, level, base_gfn, pfn, prefault,
2872 map_writable);
Sean Christopherson12703752020-09-23 15:04:25 -07002873 if (ret == RET_PF_SPURIOUS)
2874 return ret;
2875
Paolo Bonzini3fcf2d12019-06-24 13:06:21 +02002876 direct_pte_prefetch(vcpu, it.sptep);
2877 ++vcpu->stat.pf_fixed;
2878 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002879}
2880
Huang Ying77db5cb2010-10-08 16:24:15 +08002881static void kvm_send_hwpoison_signal(unsigned long address, struct task_struct *tsk)
Huang Yingbf998152010-05-31 14:28:19 +08002882{
Eric W. Biederman585a8b92018-04-16 14:23:27 -05002883 send_sig_mceerr(BUS_MCEERR_AR, (void __user *)address, PAGE_SHIFT, tsk);
Huang Yingbf998152010-05-31 14:28:19 +08002884}
2885
Dan Williamsba049e92016-01-15 16:56:11 -08002886static int kvm_handle_bad_page(struct kvm_vcpu *vcpu, gfn_t gfn, kvm_pfn_t pfn)
Huang Yingbf998152010-05-31 14:28:19 +08002887{
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08002888 /*
2889 * Do not cache the mmio info caused by writing the readonly gfn
2890 * into the spte otherwise read access on readonly gfn also can
2891 * caused mmio page fault and treat it as mmio access.
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08002892 */
2893 if (pfn == KVM_PFN_ERR_RO_FAULT)
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02002894 return RET_PF_EMULATE;
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08002895
Xiao Guangronge6c15022012-08-03 15:38:36 +08002896 if (pfn == KVM_PFN_ERR_HWPOISON) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002897 kvm_send_hwpoison_signal(kvm_vcpu_gfn_to_hva(vcpu, gfn), current);
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02002898 return RET_PF_RETRY;
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002899 }
Gleb Natapovedba23e2010-07-07 20:16:45 +03002900
Sean Christopherson2c151b22018-03-29 14:48:30 -07002901 return -EFAULT;
Huang Yingbf998152010-05-31 14:28:19 +08002902}
2903
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002904static bool handle_abnormal_pfn(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn,
Ben Gardon0a2b64c2020-02-03 15:09:09 -08002905 kvm_pfn_t pfn, unsigned int access,
2906 int *ret_val)
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002907{
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002908 /* The pfn is invalid, report the error! */
Xiao Guangrong81c52c52012-10-16 20:10:59 +08002909 if (unlikely(is_error_pfn(pfn))) {
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002910 *ret_val = kvm_handle_bad_page(vcpu, gfn, pfn);
Paolo Bonzini798e88b2016-02-23 15:28:51 +01002911 return true;
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002912 }
2913
Sean Christopherson30ab5902021-02-25 12:47:31 -08002914 if (unlikely(is_noslot_pfn(pfn))) {
Sean Christopherson4af77152019-08-01 13:35:22 -07002915 vcpu_cache_mmio_info(vcpu, gva, gfn,
2916 access & shadow_mmio_access_mask);
Sean Christopherson30ab5902021-02-25 12:47:31 -08002917 /*
2918 * If MMIO caching is disabled, emulate immediately without
2919 * touching the shadow page tables as attempting to install an
2920 * MMIO SPTE will just be an expensive nop.
2921 */
2922 if (unlikely(!shadow_mmio_value)) {
2923 *ret_val = RET_PF_EMULATE;
2924 return true;
2925 }
2926 }
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002927
Paolo Bonzini798e88b2016-02-23 15:28:51 +01002928 return false;
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002929}
2930
Xiao Guangronge5552fd2013-07-30 21:01:59 +08002931static bool page_fault_can_be_fast(u32 error_code)
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002932{
2933 /*
Xiao Guangrong1c118b82013-07-18 12:52:37 +08002934 * Do not fix the mmio spte with invalid generation number which
2935 * need to be updated by slow page fault path.
2936 */
2937 if (unlikely(error_code & PFERR_RSVD_MASK))
2938 return false;
2939
Junaid Shahidf160c7b2016-12-06 16:46:16 -08002940 /* See if the page fault is due to an NX violation */
2941 if (unlikely(((error_code & (PFERR_FETCH_MASK | PFERR_PRESENT_MASK))
2942 == (PFERR_FETCH_MASK | PFERR_PRESENT_MASK))))
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002943 return false;
2944
Junaid Shahidf160c7b2016-12-06 16:46:16 -08002945 /*
2946 * #PF can be fast if:
2947 * 1. The shadow page table entry is not present, which could mean that
2948 * the fault is potentially caused by access tracking (if enabled).
2949 * 2. The shadow page table entry is present and the fault
2950 * is caused by write-protect, that means we just need change the W
2951 * bit of the spte which can be done out of mmu-lock.
2952 *
2953 * However, if access tracking is disabled we know that a non-present
2954 * page must be a genuine page fault where we have to create a new SPTE.
2955 * So, if access tracking is disabled, we return true only for write
2956 * accesses to a present page.
2957 */
2958
2959 return shadow_acc_track_mask != 0 ||
2960 ((error_code & (PFERR_WRITE_MASK | PFERR_PRESENT_MASK))
2961 == (PFERR_WRITE_MASK | PFERR_PRESENT_MASK));
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002962}
2963
Junaid Shahid97dceba2016-12-06 16:46:12 -08002964/*
2965 * Returns true if the SPTE was fixed successfully. Otherwise,
2966 * someone else modified the SPTE from its original value.
2967 */
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002968static bool
Xiao Guangrong92a476c2014-04-17 17:06:13 +08002969fast_pf_fix_direct_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
Junaid Shahidd3e328f22016-12-21 20:29:32 -08002970 u64 *sptep, u64 old_spte, u64 new_spte)
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002971{
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002972 gfn_t gfn;
2973
2974 WARN_ON(!sp->role.direct);
2975
Kai Huang9b51a632015-01-28 10:54:25 +08002976 /*
2977 * Theoretically we could also set dirty bit (and flush TLB) here in
2978 * order to eliminate unnecessary PML logging. See comments in
2979 * set_spte. But fast_page_fault is very unlikely to happen with PML
2980 * enabled, so we do not do this. This might result in the same GPA
2981 * to be logged in PML buffer again when the write really happens, and
2982 * eventually to be called by mark_page_dirty twice. But it's also no
2983 * harm. This also avoids the TLB flush needed after setting dirty bit
2984 * so non-PML cases won't be impacted.
2985 *
2986 * Compare with set_spte where instead shadow_dirty_mask is set.
2987 */
Junaid Shahidf160c7b2016-12-06 16:46:16 -08002988 if (cmpxchg64(sptep, old_spte, new_spte) != old_spte)
Junaid Shahid97dceba2016-12-06 16:46:12 -08002989 return false;
2990
Junaid Shahidd3e328f22016-12-21 20:29:32 -08002991 if (is_writable_pte(new_spte) && !is_writable_pte(old_spte)) {
Junaid Shahidf160c7b2016-12-06 16:46:16 -08002992 /*
2993 * The gfn of direct spte is stable since it is
2994 * calculated by sp->gfn.
2995 */
2996 gfn = kvm_mmu_page_get_gfn(sp, sptep - sp->spt);
2997 kvm_vcpu_mark_page_dirty(vcpu, gfn);
2998 }
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002999
3000 return true;
3001}
3002
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003003static bool is_access_allowed(u32 fault_err_code, u64 spte)
3004{
3005 if (fault_err_code & PFERR_FETCH_MASK)
3006 return is_executable_pte(spte);
3007
3008 if (fault_err_code & PFERR_WRITE_MASK)
3009 return is_writable_pte(spte);
3010
3011 /* Fault was on Read access */
3012 return spte & PT_PRESENT_MASK;
3013}
3014
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003015/*
Sean Christophersonc4371c22020-09-23 15:04:24 -07003016 * Returns one of RET_PF_INVALID, RET_PF_FIXED or RET_PF_SPURIOUS.
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003017 */
Sean Christophersonc4371c22020-09-23 15:04:24 -07003018static int fast_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
3019 u32 error_code)
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003020{
3021 struct kvm_shadow_walk_iterator iterator;
Xiao Guangrong92a476c2014-04-17 17:06:13 +08003022 struct kvm_mmu_page *sp;
Sean Christophersonc4371c22020-09-23 15:04:24 -07003023 int ret = RET_PF_INVALID;
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003024 u64 spte = 0ull;
Junaid Shahid97dceba2016-12-06 16:46:12 -08003025 uint retry_count = 0;
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003026
Xiao Guangronge5552fd2013-07-30 21:01:59 +08003027 if (!page_fault_can_be_fast(error_code))
Sean Christophersonc4371c22020-09-23 15:04:24 -07003028 return ret;
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003029
3030 walk_shadow_page_lockless_begin(vcpu);
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003031
Junaid Shahid97dceba2016-12-06 16:46:12 -08003032 do {
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003033 u64 new_spte;
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003034
Sean Christopherson736c2912019-12-06 15:57:14 -08003035 for_each_shadow_entry_lockless(vcpu, cr2_or_gpa, iterator, spte)
Sean Christophersonf9fa2502020-01-08 12:24:42 -08003036 if (!is_shadow_present_pte(spte))
Junaid Shahidd162f302016-12-21 20:29:30 -08003037 break;
3038
Sean Christophersonec89e642021-02-25 12:47:28 -08003039 if (!is_shadow_present_pte(spte))
3040 break;
3041
Sean Christopherson57354682020-06-22 13:20:33 -07003042 sp = sptep_to_sp(iterator.sptep);
Junaid Shahid97dceba2016-12-06 16:46:12 -08003043 if (!is_last_spte(spte, sp->role.level))
3044 break;
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003045
Junaid Shahid97dceba2016-12-06 16:46:12 -08003046 /*
Junaid Shahidf160c7b2016-12-06 16:46:16 -08003047 * Check whether the memory access that caused the fault would
3048 * still cause it if it were to be performed right now. If not,
3049 * then this is a spurious fault caused by TLB lazily flushed,
3050 * or some other CPU has already fixed the PTE after the
3051 * current CPU took the fault.
Junaid Shahid97dceba2016-12-06 16:46:12 -08003052 *
3053 * Need not check the access of upper level table entries since
3054 * they are always ACC_ALL.
3055 */
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003056 if (is_access_allowed(error_code, spte)) {
Sean Christophersonc4371c22020-09-23 15:04:24 -07003057 ret = RET_PF_SPURIOUS;
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003058 break;
Junaid Shahid97dceba2016-12-06 16:46:12 -08003059 }
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003060
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003061 new_spte = spte;
Junaid Shahidf160c7b2016-12-06 16:46:16 -08003062
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003063 if (is_access_track_spte(spte))
3064 new_spte = restore_acc_track_spte(new_spte);
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003065
Junaid Shahid97dceba2016-12-06 16:46:12 -08003066 /*
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003067 * Currently, to simplify the code, write-protection can
3068 * be removed in the fast path only if the SPTE was
3069 * write-protected for dirty-logging or access tracking.
Junaid Shahid97dceba2016-12-06 16:46:12 -08003070 */
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003071 if ((error_code & PFERR_WRITE_MASK) &&
Miaohe Line6302692020-02-15 10:44:22 +08003072 spte_can_locklessly_be_made_writable(spte)) {
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003073 new_spte |= PT_WRITABLE_MASK;
3074
3075 /*
3076 * Do not fix write-permission on the large spte. Since
3077 * we only dirty the first page into the dirty-bitmap in
3078 * fast_pf_fix_direct_spte(), other pages are missed
3079 * if its slot has dirty logging enabled.
3080 *
3081 * Instead, we let the slow page fault path create a
3082 * normal spte to fix the access.
3083 *
3084 * See the comments in kvm_arch_commit_memory_region().
3085 */
Sean Christopherson3bae0452020-04-27 17:54:22 -07003086 if (sp->role.level > PG_LEVEL_4K)
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003087 break;
3088 }
3089
3090 /* Verify that the fault can be handled in the fast path */
3091 if (new_spte == spte ||
3092 !is_access_allowed(error_code, new_spte))
Junaid Shahid97dceba2016-12-06 16:46:12 -08003093 break;
Xiao Guangrongc126d942014-04-17 17:06:14 +08003094
Junaid Shahid97dceba2016-12-06 16:46:12 -08003095 /*
3096 * Currently, fast page fault only works for direct mapping
3097 * since the gfn is not stable for indirect shadow page. See
Mauro Carvalho Chehab3ecad8c2020-04-14 18:48:36 +02003098 * Documentation/virt/kvm/locking.rst to get more detail.
Junaid Shahid97dceba2016-12-06 16:46:12 -08003099 */
Sean Christophersonc4371c22020-09-23 15:04:24 -07003100 if (fast_pf_fix_direct_spte(vcpu, sp, iterator.sptep, spte,
3101 new_spte)) {
3102 ret = RET_PF_FIXED;
Junaid Shahid97dceba2016-12-06 16:46:12 -08003103 break;
Sean Christophersonc4371c22020-09-23 15:04:24 -07003104 }
Junaid Shahid97dceba2016-12-06 16:46:12 -08003105
3106 if (++retry_count > 4) {
3107 printk_once(KERN_WARNING
3108 "kvm: Fast #PF retrying more than 4 times.\n");
3109 break;
3110 }
3111
Junaid Shahid97dceba2016-12-06 16:46:12 -08003112 } while (true);
3113
Sean Christopherson736c2912019-12-06 15:57:14 -08003114 trace_fast_page_fault(vcpu, cr2_or_gpa, error_code, iterator.sptep,
Sean Christophersonc4371c22020-09-23 15:04:24 -07003115 spte, ret);
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003116 walk_shadow_page_lockless_end(vcpu);
3117
Sean Christophersonc4371c22020-09-23 15:04:24 -07003118 return ret;
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003119}
3120
Junaid Shahid74b566e2018-05-04 11:37:11 -07003121static void mmu_free_root_page(struct kvm *kvm, hpa_t *root_hpa,
3122 struct list_head *invalid_list)
3123{
3124 struct kvm_mmu_page *sp;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003125
Junaid Shahid74b566e2018-05-04 11:37:11 -07003126 if (!VALID_PAGE(*root_hpa))
3127 return;
3128
Sean Christophersone47c4ae2020-06-22 13:20:34 -07003129 sp = to_shadow_page(*root_hpa & PT64_BASE_ADDR_MASK);
Ben Gardon02c00b32020-10-14 20:26:44 +02003130
Ben Gardon2bdb3d82021-04-01 16:37:27 -07003131 if (is_tdp_mmu_page(sp))
Ben Gardon6103bc02021-04-01 16:37:32 -07003132 kvm_tdp_mmu_put_root(kvm, sp, false);
Ben Gardon76eb54e2021-04-01 16:37:25 -07003133 else if (!--sp->root_count && sp->role.invalid)
3134 kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
Junaid Shahid74b566e2018-05-04 11:37:11 -07003135
3136 *root_hpa = INVALID_PAGE;
3137}
3138
Junaid Shahid08fb59d2018-06-27 14:59:17 -07003139/* roots_to_free must be some combination of the KVM_MMU_ROOT_* flags */
Vitaly Kuznetsov6a82cd12018-10-08 21:28:07 +02003140void kvm_mmu_free_roots(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
3141 ulong roots_to_free)
Avi Kivity17ac10a2007-01-05 16:36:40 -08003142{
Sean Christopherson4d710de2020-09-23 12:12:04 -07003143 struct kvm *kvm = vcpu->kvm;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003144 int i;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003145 LIST_HEAD(invalid_list);
Junaid Shahid08fb59d2018-06-27 14:59:17 -07003146 bool free_active_root = roots_to_free & KVM_MMU_ROOT_CURRENT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003147
Junaid Shahidb94742c2018-06-27 14:59:20 -07003148 BUILD_BUG_ON(KVM_MMU_NUM_PREV_ROOTS >= BITS_PER_LONG);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003149
Junaid Shahid08fb59d2018-06-27 14:59:17 -07003150 /* Before acquiring the MMU lock, see if we need to do any real work. */
Junaid Shahidb94742c2018-06-27 14:59:20 -07003151 if (!(free_active_root && VALID_PAGE(mmu->root_hpa))) {
3152 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
3153 if ((roots_to_free & KVM_MMU_ROOT_PREVIOUS(i)) &&
3154 VALID_PAGE(mmu->prev_roots[i].hpa))
3155 break;
3156
3157 if (i == KVM_MMU_NUM_PREV_ROOTS)
3158 return;
3159 }
Gleb Natapov35af5772013-05-16 11:55:51 +03003160
Ben Gardon531810c2021-02-02 10:57:24 -08003161 write_lock(&kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003162
Junaid Shahidb94742c2018-06-27 14:59:20 -07003163 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
3164 if (roots_to_free & KVM_MMU_ROOT_PREVIOUS(i))
Sean Christopherson4d710de2020-09-23 12:12:04 -07003165 mmu_free_root_page(kvm, &mmu->prev_roots[i].hpa,
Junaid Shahidb94742c2018-06-27 14:59:20 -07003166 &invalid_list);
Junaid Shahid7c390d32018-06-27 14:59:06 -07003167
Junaid Shahid08fb59d2018-06-27 14:59:17 -07003168 if (free_active_root) {
3169 if (mmu->shadow_root_level >= PT64_ROOT_4LEVEL &&
3170 (mmu->root_level >= PT64_ROOT_4LEVEL || mmu->direct_map)) {
Sean Christopherson4d710de2020-09-23 12:12:04 -07003171 mmu_free_root_page(kvm, &mmu->root_hpa, &invalid_list);
Sean Christopherson04d45552021-03-04 17:10:46 -08003172 } else if (mmu->pae_root) {
Sean Christophersonc834e5e42021-03-09 14:42:06 -08003173 for (i = 0; i < 4; ++i) {
3174 if (!IS_VALID_PAE_ROOT(mmu->pae_root[i]))
3175 continue;
3176
3177 mmu_free_root_page(kvm, &mmu->pae_root[i],
3178 &invalid_list);
3179 mmu->pae_root[i] = INVALID_PAE_ROOT;
3180 }
Junaid Shahid08fb59d2018-06-27 14:59:17 -07003181 }
Sean Christopherson04d45552021-03-04 17:10:46 -08003182 mmu->root_hpa = INVALID_PAGE;
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003183 mmu->root_pgd = 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003184 }
Junaid Shahid74b566e2018-05-04 11:37:11 -07003185
Sean Christopherson4d710de2020-09-23 12:12:04 -07003186 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Ben Gardon531810c2021-02-02 10:57:24 -08003187 write_unlock(&kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003188}
Junaid Shahid74b566e2018-05-04 11:37:11 -07003189EXPORT_SYMBOL_GPL(kvm_mmu_free_roots);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003190
Sean Christopherson25b62c62021-06-09 16:42:29 -07003191void kvm_mmu_free_guest_mode_roots(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu)
3192{
3193 unsigned long roots_to_free = 0;
3194 hpa_t root_hpa;
3195 int i;
3196
3197 /*
3198 * This should not be called while L2 is active, L2 can't invalidate
3199 * _only_ its own roots, e.g. INVVPID unconditionally exits.
3200 */
3201 WARN_ON_ONCE(mmu->mmu_role.base.guest_mode);
3202
3203 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
3204 root_hpa = mmu->prev_roots[i].hpa;
3205 if (!VALID_PAGE(root_hpa))
3206 continue;
3207
3208 if (!to_shadow_page(root_hpa) ||
3209 to_shadow_page(root_hpa)->role.guest_mode)
3210 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
3211 }
3212
3213 kvm_mmu_free_roots(vcpu, mmu, roots_to_free);
3214}
3215EXPORT_SYMBOL_GPL(kvm_mmu_free_guest_mode_roots);
3216
3217
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003218static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
3219{
3220 int ret = 0;
3221
Vitaly Kuznetsov995decb2020-07-08 16:00:23 +02003222 if (!kvm_vcpu_is_visible_gfn(vcpu, root_gfn)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03003223 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003224 ret = 1;
3225 }
3226
3227 return ret;
3228}
3229
Sean Christopherson8123f262020-04-27 19:37:14 -07003230static hpa_t mmu_alloc_root(struct kvm_vcpu *vcpu, gfn_t gfn, gva_t gva,
3231 u8 level, bool direct)
Joerg Roedel651dd372010-09-10 17:30:59 +02003232{
3233 struct kvm_mmu_page *sp;
Sean Christopherson8123f262020-04-27 19:37:14 -07003234
Sean Christopherson8123f262020-04-27 19:37:14 -07003235 sp = kvm_mmu_get_page(vcpu, gfn, gva, level, direct, ACC_ALL);
3236 ++sp->root_count;
3237
Sean Christopherson8123f262020-04-27 19:37:14 -07003238 return __pa(sp->spt);
3239}
3240
3241static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu)
3242{
Sean Christophersonb37233c2021-03-04 17:10:47 -08003243 struct kvm_mmu *mmu = vcpu->arch.mmu;
3244 u8 shadow_root_level = mmu->shadow_root_level;
Sean Christopherson8123f262020-04-27 19:37:14 -07003245 hpa_t root;
Avi Kivity7ebaf152010-10-03 18:51:39 +02003246 unsigned i;
Paolo Bonzini4a381622021-04-08 08:10:25 -04003247 int r;
3248
3249 write_lock(&vcpu->kvm->mmu_lock);
3250 r = make_mmu_pages_available(vcpu);
3251 if (r < 0)
3252 goto out_unlock;
Joerg Roedel651dd372010-09-10 17:30:59 +02003253
Paolo Bonzini897218f2021-02-06 09:53:33 -05003254 if (is_tdp_mmu_enabled(vcpu->kvm)) {
Ben Gardon02c00b32020-10-14 20:26:44 +02003255 root = kvm_tdp_mmu_get_vcpu_root_hpa(vcpu);
Sean Christophersonb37233c2021-03-04 17:10:47 -08003256 mmu->root_hpa = root;
Ben Gardon02c00b32020-10-14 20:26:44 +02003257 } else if (shadow_root_level >= PT64_ROOT_4LEVEL) {
Sean Christopherson6e6ec582021-03-04 17:10:50 -08003258 root = mmu_alloc_root(vcpu, 0, 0, shadow_root_level, true);
Sean Christophersonb37233c2021-03-04 17:10:47 -08003259 mmu->root_hpa = root;
Sean Christopherson8123f262020-04-27 19:37:14 -07003260 } else if (shadow_root_level == PT32E_ROOT_LEVEL) {
Paolo Bonzini4a381622021-04-08 08:10:25 -04003261 if (WARN_ON_ONCE(!mmu->pae_root)) {
3262 r = -EIO;
3263 goto out_unlock;
3264 }
Sean Christopherson73ad1602021-03-04 17:11:01 -08003265
Joerg Roedel651dd372010-09-10 17:30:59 +02003266 for (i = 0; i < 4; ++i) {
Sean Christophersonc834e5e42021-03-09 14:42:06 -08003267 WARN_ON_ONCE(IS_VALID_PAE_ROOT(mmu->pae_root[i]));
Joerg Roedel651dd372010-09-10 17:30:59 +02003268
Sean Christopherson8123f262020-04-27 19:37:14 -07003269 root = mmu_alloc_root(vcpu, i << (30 - PAGE_SHIFT),
3270 i << 30, PT32_ROOT_LEVEL, true);
Sean Christopherson17e368d2021-03-04 17:10:54 -08003271 mmu->pae_root[i] = root | PT_PRESENT_MASK |
3272 shadow_me_mask;
Joerg Roedel651dd372010-09-10 17:30:59 +02003273 }
Sean Christophersonb37233c2021-03-04 17:10:47 -08003274 mmu->root_hpa = __pa(mmu->pae_root);
Sean Christopherson73ad1602021-03-04 17:11:01 -08003275 } else {
3276 WARN_ONCE(1, "Bad TDP root level = %d\n", shadow_root_level);
Paolo Bonzini4a381622021-04-08 08:10:25 -04003277 r = -EIO;
3278 goto out_unlock;
Sean Christopherson73ad1602021-03-04 17:11:01 -08003279 }
Sean Christopherson3651c7f2020-02-28 14:52:39 -08003280
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003281 /* root_pgd is ignored for direct MMUs. */
Sean Christophersonb37233c2021-03-04 17:10:47 -08003282 mmu->root_pgd = 0;
Paolo Bonzini4a381622021-04-08 08:10:25 -04003283out_unlock:
3284 write_unlock(&vcpu->kvm->mmu_lock);
3285 return r;
Joerg Roedel651dd372010-09-10 17:30:59 +02003286}
3287
3288static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08003289{
Sean Christophersonb37233c2021-03-04 17:10:47 -08003290 struct kvm_mmu *mmu = vcpu->arch.mmu;
Sean Christopherson6e0918a2021-03-04 17:10:51 -08003291 u64 pdptrs[4], pm_mask;
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003292 gfn_t root_gfn, root_pgd;
Sean Christopherson8123f262020-04-27 19:37:14 -07003293 hpa_t root;
Paolo Bonzini4a381622021-04-08 08:10:25 -04003294 unsigned i;
3295 int r;
Avi Kivity3bb65a22007-01-05 16:36:51 -08003296
Sean Christophersonb37233c2021-03-04 17:10:47 -08003297 root_pgd = mmu->get_guest_pgd(vcpu);
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003298 root_gfn = root_pgd >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003299
Joerg Roedel651dd372010-09-10 17:30:59 +02003300 if (mmu_check_root(vcpu, root_gfn))
3301 return 1;
3302
3303 /*
Paolo Bonzini4a381622021-04-08 08:10:25 -04003304 * On SVM, reading PDPTRs might access guest memory, which might fault
3305 * and thus might sleep. Grab the PDPTRs before acquiring mmu_lock.
3306 */
Sean Christopherson6e0918a2021-03-04 17:10:51 -08003307 if (mmu->root_level == PT32E_ROOT_LEVEL) {
3308 for (i = 0; i < 4; ++i) {
3309 pdptrs[i] = mmu->get_pdptr(vcpu, i);
3310 if (!(pdptrs[i] & PT_PRESENT_MASK))
3311 continue;
3312
3313 if (mmu_check_root(vcpu, pdptrs[i] >> PAGE_SHIFT))
3314 return 1;
3315 }
3316 }
3317
Ben Gardond501f742021-05-18 10:34:14 -07003318 r = alloc_all_memslots_rmaps(vcpu->kvm);
3319 if (r)
3320 return r;
3321
Paolo Bonzini4a381622021-04-08 08:10:25 -04003322 write_lock(&vcpu->kvm->mmu_lock);
3323 r = make_mmu_pages_available(vcpu);
3324 if (r < 0)
3325 goto out_unlock;
3326
Joerg Roedel651dd372010-09-10 17:30:59 +02003327 /*
3328 * Do we shadow a long mode page table? If so we need to
3329 * write-protect the guests page table root.
3330 */
Sean Christophersonb37233c2021-03-04 17:10:47 -08003331 if (mmu->root_level >= PT64_ROOT_4LEVEL) {
Sean Christopherson8123f262020-04-27 19:37:14 -07003332 root = mmu_alloc_root(vcpu, root_gfn, 0,
Sean Christophersonb37233c2021-03-04 17:10:47 -08003333 mmu->shadow_root_level, false);
Sean Christophersonb37233c2021-03-04 17:10:47 -08003334 mmu->root_hpa = root;
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003335 goto set_root_pgd;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003336 }
Joerg Roedelf87f9282010-09-02 17:29:45 +02003337
Paolo Bonzini4a381622021-04-08 08:10:25 -04003338 if (WARN_ON_ONCE(!mmu->pae_root)) {
3339 r = -EIO;
3340 goto out_unlock;
3341 }
Sean Christopherson73ad1602021-03-04 17:11:01 -08003342
Joerg Roedel651dd372010-09-10 17:30:59 +02003343 /*
3344 * We shadow a 32 bit page table. This may be a legacy 2-level
Joerg Roedel81407ca2010-09-10 17:31:00 +02003345 * or a PAE 3-level page table. In either case we need to be aware that
3346 * the shadow page table may be a PAE or a long mode page table.
Joerg Roedel651dd372010-09-10 17:30:59 +02003347 */
Sean Christopherson17e368d2021-03-04 17:10:54 -08003348 pm_mask = PT_PRESENT_MASK | shadow_me_mask;
Sean Christopherson748e52b2021-03-04 17:10:49 -08003349 if (mmu->shadow_root_level == PT64_ROOT_4LEVEL) {
Joerg Roedel81407ca2010-09-10 17:31:00 +02003350 pm_mask |= PT_ACCESSED_MASK | PT_WRITABLE_MASK | PT_USER_MASK;
3351
Sean Christopherson03ca4582021-05-05 13:42:21 -07003352 if (WARN_ON_ONCE(!mmu->pml4_root)) {
Paolo Bonzini4a381622021-04-08 08:10:25 -04003353 r = -EIO;
3354 goto out_unlock;
3355 }
Sean Christopherson73ad1602021-03-04 17:11:01 -08003356
Sean Christopherson03ca4582021-05-05 13:42:21 -07003357 mmu->pml4_root[0] = __pa(mmu->pae_root) | pm_mask;
Sean Christopherson04d45552021-03-04 17:10:46 -08003358 }
3359
Avi Kivity17ac10a2007-01-05 16:36:40 -08003360 for (i = 0; i < 4; ++i) {
Sean Christophersonc834e5e42021-03-09 14:42:06 -08003361 WARN_ON_ONCE(IS_VALID_PAE_ROOT(mmu->pae_root[i]));
Sean Christopherson6e6ec582021-03-04 17:10:50 -08003362
Sean Christophersonb37233c2021-03-04 17:10:47 -08003363 if (mmu->root_level == PT32E_ROOT_LEVEL) {
Sean Christopherson6e0918a2021-03-04 17:10:51 -08003364 if (!(pdptrs[i] & PT_PRESENT_MASK)) {
Sean Christophersonc834e5e42021-03-09 14:42:06 -08003365 mmu->pae_root[i] = INVALID_PAE_ROOT;
Avi Kivity417726a2007-04-12 17:35:58 +03003366 continue;
3367 }
Sean Christopherson6e0918a2021-03-04 17:10:51 -08003368 root_gfn = pdptrs[i] >> PAGE_SHIFT;
Eric Northup5a7388c2010-04-26 17:00:05 -07003369 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03003370
Sean Christopherson8123f262020-04-27 19:37:14 -07003371 root = mmu_alloc_root(vcpu, root_gfn, i << 30,
3372 PT32_ROOT_LEVEL, false);
Sean Christophersonb37233c2021-03-04 17:10:47 -08003373 mmu->pae_root[i] = root | pm_mask;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003374 }
Joerg Roedel81407ca2010-09-10 17:31:00 +02003375
Sean Christophersonba0a1942021-03-04 17:10:48 -08003376 if (mmu->shadow_root_level == PT64_ROOT_4LEVEL)
Sean Christopherson03ca4582021-05-05 13:42:21 -07003377 mmu->root_hpa = __pa(mmu->pml4_root);
Sean Christophersonba0a1942021-03-04 17:10:48 -08003378 else
3379 mmu->root_hpa = __pa(mmu->pae_root);
Joerg Roedel81407ca2010-09-10 17:31:00 +02003380
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003381set_root_pgd:
Sean Christophersonb37233c2021-03-04 17:10:47 -08003382 mmu->root_pgd = root_pgd;
Paolo Bonzini4a381622021-04-08 08:10:25 -04003383out_unlock:
3384 write_unlock(&vcpu->kvm->mmu_lock);
Vitaly Kuznetsovad7dc692019-02-22 17:45:01 +01003385
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003386 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003387}
3388
Sean Christopherson748e52b2021-03-04 17:10:49 -08003389static int mmu_alloc_special_roots(struct kvm_vcpu *vcpu)
Joerg Roedel651dd372010-09-10 17:30:59 +02003390{
Sean Christopherson748e52b2021-03-04 17:10:49 -08003391 struct kvm_mmu *mmu = vcpu->arch.mmu;
Sean Christopherson03ca4582021-05-05 13:42:21 -07003392 u64 *pml4_root, *pae_root;
Sean Christopherson748e52b2021-03-04 17:10:49 -08003393
3394 /*
3395 * When shadowing 32-bit or PAE NPT with 64-bit NPT, the PML4 and PDP
3396 * tables are allocated and initialized at root creation as there is no
3397 * equivalent level in the guest's NPT to shadow. Allocate the tables
3398 * on demand, as running a 32-bit L1 VMM on 64-bit KVM is very rare.
3399 */
3400 if (mmu->direct_map || mmu->root_level >= PT64_ROOT_4LEVEL ||
3401 mmu->shadow_root_level < PT64_ROOT_4LEVEL)
3402 return 0;
3403
3404 /*
3405 * This mess only works with 4-level paging and needs to be updated to
3406 * work with 5-level paging.
3407 */
3408 if (WARN_ON_ONCE(mmu->shadow_root_level != PT64_ROOT_4LEVEL))
3409 return -EIO;
3410
Sean Christopherson03ca4582021-05-05 13:42:21 -07003411 if (mmu->pae_root && mmu->pml4_root)
Sean Christopherson748e52b2021-03-04 17:10:49 -08003412 return 0;
3413
3414 /*
3415 * The special roots should always be allocated in concert. Yell and
3416 * bail if KVM ends up in a state where only one of the roots is valid.
3417 */
Sean Christopherson03ca4582021-05-05 13:42:21 -07003418 if (WARN_ON_ONCE(!tdp_enabled || mmu->pae_root || mmu->pml4_root))
Sean Christopherson748e52b2021-03-04 17:10:49 -08003419 return -EIO;
3420
Sean Christopherson4a986232021-03-09 14:42:07 -08003421 /*
3422 * Unlike 32-bit NPT, the PDP table doesn't need to be in low mem, and
3423 * doesn't need to be decrypted.
3424 */
Sean Christopherson748e52b2021-03-04 17:10:49 -08003425 pae_root = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
3426 if (!pae_root)
3427 return -ENOMEM;
3428
Sean Christopherson03ca4582021-05-05 13:42:21 -07003429 pml4_root = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
3430 if (!pml4_root) {
Sean Christopherson748e52b2021-03-04 17:10:49 -08003431 free_page((unsigned long)pae_root);
3432 return -ENOMEM;
3433 }
3434
3435 mmu->pae_root = pae_root;
Sean Christopherson03ca4582021-05-05 13:42:21 -07003436 mmu->pml4_root = pml4_root;
Sean Christopherson748e52b2021-03-04 17:10:49 -08003437
3438 return 0;
Joerg Roedel651dd372010-09-10 17:30:59 +02003439}
3440
Junaid Shahid578e1c42018-06-27 14:59:05 -07003441void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003442{
3443 int i;
3444 struct kvm_mmu_page *sp;
3445
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003446 if (vcpu->arch.mmu->direct_map)
Joerg Roedel81407ca2010-09-10 17:31:00 +02003447 return;
3448
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003449 if (!VALID_PAGE(vcpu->arch.mmu->root_hpa))
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003450 return;
Xiao Guangrong69030742010-09-27 18:09:29 +08003451
David Matlack56f17dd2014-08-18 15:46:07 -07003452 vcpu_clear_mmio_info(vcpu, MMIO_GVA_ANY);
Junaid Shahid578e1c42018-06-27 14:59:05 -07003453
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003454 if (vcpu->arch.mmu->root_level >= PT64_ROOT_4LEVEL) {
3455 hpa_t root = vcpu->arch.mmu->root_hpa;
Sean Christophersone47c4ae2020-06-22 13:20:34 -07003456 sp = to_shadow_page(root);
Junaid Shahid578e1c42018-06-27 14:59:05 -07003457
3458 /*
3459 * Even if another CPU was marking the SP as unsync-ed
3460 * simultaneously, any guest page table changes are not
3461 * guaranteed to be visible anyway until this VCPU issues a TLB
3462 * flush strictly after those changes are made. We only need to
3463 * ensure that the other CPU sets these flags before any actual
3464 * changes to the page tables are made. The comments in
3465 * mmu_need_write_protect() describe what could go wrong if this
3466 * requirement isn't satisfied.
3467 */
3468 if (!smp_load_acquire(&sp->unsync) &&
3469 !smp_load_acquire(&sp->unsync_children))
3470 return;
3471
Ben Gardon531810c2021-02-02 10:57:24 -08003472 write_lock(&vcpu->kvm->mmu_lock);
Junaid Shahid578e1c42018-06-27 14:59:05 -07003473 kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC);
3474
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003475 mmu_sync_children(vcpu, sp);
Junaid Shahid578e1c42018-06-27 14:59:05 -07003476
Xiao Guangrong0375f7f2011-11-28 20:41:00 +08003477 kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
Ben Gardon531810c2021-02-02 10:57:24 -08003478 write_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003479 return;
3480 }
Junaid Shahid578e1c42018-06-27 14:59:05 -07003481
Ben Gardon531810c2021-02-02 10:57:24 -08003482 write_lock(&vcpu->kvm->mmu_lock);
Junaid Shahid578e1c42018-06-27 14:59:05 -07003483 kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC);
3484
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003485 for (i = 0; i < 4; ++i) {
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003486 hpa_t root = vcpu->arch.mmu->pae_root[i];
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003487
Sean Christophersonc834e5e42021-03-09 14:42:06 -08003488 if (IS_VALID_PAE_ROOT(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003489 root &= PT64_BASE_ADDR_MASK;
Sean Christophersone47c4ae2020-06-22 13:20:34 -07003490 sp = to_shadow_page(root);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003491 mmu_sync_children(vcpu, sp);
3492 }
3493 }
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003494
Junaid Shahid578e1c42018-06-27 14:59:05 -07003495 kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
Ben Gardon531810c2021-02-02 10:57:24 -08003496 write_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003497}
3498
Sean Christopherson736c2912019-12-06 15:57:14 -08003499static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gpa_t vaddr,
Avi Kivityab9ae312010-11-22 17:53:26 +02003500 u32 access, struct x86_exception *exception)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003501{
Avi Kivityab9ae312010-11-22 17:53:26 +02003502 if (exception)
3503 exception->error_code = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003504 return vaddr;
3505}
3506
Sean Christopherson736c2912019-12-06 15:57:14 -08003507static gpa_t nonpaging_gva_to_gpa_nested(struct kvm_vcpu *vcpu, gpa_t vaddr,
Avi Kivityab9ae312010-11-22 17:53:26 +02003508 u32 access,
3509 struct x86_exception *exception)
Joerg Roedel6539e732010-09-10 17:30:50 +02003510{
Avi Kivityab9ae312010-11-22 17:53:26 +02003511 if (exception)
3512 exception->error_code = 0;
Paolo Bonzini54987b72014-09-02 13:23:06 +02003513 return vcpu->arch.nested_mmu.translate_gpa(vcpu, vaddr, access, exception);
Joerg Roedel6539e732010-09-10 17:30:50 +02003514}
3515
Xiao Guangrongd625b152015-08-05 12:04:25 +08003516static bool
3517__is_rsvd_bits_set(struct rsvd_bits_validate *rsvd_check, u64 pte, int level)
3518{
Sean Christophersonb5c3c1b2020-01-09 15:06:40 -08003519 int bit7 = (pte >> 7) & 1;
Xiao Guangrongd625b152015-08-05 12:04:25 +08003520
Sean Christophersonb5c3c1b2020-01-09 15:06:40 -08003521 return pte & rsvd_check->rsvd_bits_mask[bit7][level-1];
Xiao Guangrongd625b152015-08-05 12:04:25 +08003522}
3523
Sean Christophersonb5c3c1b2020-01-09 15:06:40 -08003524static bool __is_bad_mt_xwr(struct rsvd_bits_validate *rsvd_check, u64 pte)
Xiao Guangrongd625b152015-08-05 12:04:25 +08003525{
Sean Christophersonb5c3c1b2020-01-09 15:06:40 -08003526 return rsvd_check->bad_mt_xwr & BIT_ULL(pte & 0x3f);
Xiao Guangrongd625b152015-08-05 12:04:25 +08003527}
3528
Takuya Yoshikawaded58742016-02-22 17:23:40 +09003529static bool mmio_info_in_cache(struct kvm_vcpu *vcpu, u64 addr, bool direct)
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003530{
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02003531 /*
3532 * A nested guest cannot use the MMIO cache if it is using nested
3533 * page tables, because cr2 is a nGPA while the cache stores GPAs.
3534 */
3535 if (mmu_is_nested(vcpu))
3536 return false;
3537
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003538 if (direct)
3539 return vcpu_match_mmio_gpa(vcpu, addr);
3540
3541 return vcpu_match_mmio_gva(vcpu, addr);
3542}
3543
Ben Gardon95fb5b02020-10-14 11:26:58 -07003544/*
3545 * Return the level of the lowest level SPTE added to sptes.
3546 * That SPTE may be non-present.
3547 */
Sean Christopherson39b4d432020-12-17 16:31:37 -08003548static int get_walk(struct kvm_vcpu *vcpu, u64 addr, u64 *sptes, int *root_level)
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003549{
3550 struct kvm_shadow_walk_iterator iterator;
Sean Christopherson2aa078932020-12-17 16:31:36 -08003551 int leaf = -1;
Ben Gardon95fb5b02020-10-14 11:26:58 -07003552 u64 spte;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003553
3554 walk_shadow_page_lockless_begin(vcpu);
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003555
Sean Christopherson39b4d432020-12-17 16:31:37 -08003556 for (shadow_walk_init(&iterator, vcpu, addr),
3557 *root_level = iterator.level;
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003558 shadow_walk_okay(&iterator);
3559 __shadow_walk_next(&iterator, spte)) {
Ben Gardon95fb5b02020-10-14 11:26:58 -07003560 leaf = iterator.level;
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003561 spte = mmu_spte_get_lockless(iterator.sptep);
3562
Sean Christophersondde81f92020-12-17 16:31:38 -08003563 sptes[leaf] = spte;
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003564
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003565 if (!is_shadow_present_pte(spte))
3566 break;
Ben Gardon95fb5b02020-10-14 11:26:58 -07003567 }
3568
3569 walk_shadow_page_lockless_end(vcpu);
3570
3571 return leaf;
3572}
3573
Sean Christopherson9aa41872020-12-17 16:31:39 -08003574/* return true if reserved bit(s) are detected on a valid, non-MMIO SPTE. */
Ben Gardon95fb5b02020-10-14 11:26:58 -07003575static bool get_mmio_spte(struct kvm_vcpu *vcpu, u64 addr, u64 *sptep)
3576{
Sean Christophersondde81f92020-12-17 16:31:38 -08003577 u64 sptes[PT64_ROOT_MAX_LEVEL + 1];
Ben Gardon95fb5b02020-10-14 11:26:58 -07003578 struct rsvd_bits_validate *rsvd_check;
Sean Christopherson39b4d432020-12-17 16:31:37 -08003579 int root, leaf, level;
Ben Gardon95fb5b02020-10-14 11:26:58 -07003580 bool reserved = false;
3581
David Matlack63c0cac2021-06-17 23:19:47 +00003582 if (is_tdp_mmu(vcpu->arch.mmu))
Sean Christopherson39b4d432020-12-17 16:31:37 -08003583 leaf = kvm_tdp_mmu_get_walk(vcpu, addr, sptes, &root);
Ben Gardon95fb5b02020-10-14 11:26:58 -07003584 else
Sean Christopherson39b4d432020-12-17 16:31:37 -08003585 leaf = get_walk(vcpu, addr, sptes, &root);
Ben Gardon95fb5b02020-10-14 11:26:58 -07003586
Sean Christopherson2aa078932020-12-17 16:31:36 -08003587 if (unlikely(leaf < 0)) {
3588 *sptep = 0ull;
3589 return reserved;
3590 }
3591
Sean Christopherson9aa41872020-12-17 16:31:39 -08003592 *sptep = sptes[leaf];
3593
3594 /*
3595 * Skip reserved bits checks on the terminal leaf if it's not a valid
3596 * SPTE. Note, this also (intentionally) skips MMIO SPTEs, which, by
3597 * design, always have reserved bits set. The purpose of the checks is
3598 * to detect reserved bits on non-MMIO SPTEs. i.e. buggy SPTEs.
3599 */
3600 if (!is_shadow_present_pte(sptes[leaf]))
3601 leaf++;
Ben Gardon95fb5b02020-10-14 11:26:58 -07003602
3603 rsvd_check = &vcpu->arch.mmu->shadow_zero_check;
3604
Sean Christopherson9aa41872020-12-17 16:31:39 -08003605 for (level = root; level >= leaf; level--)
Sean Christophersonb5c3c1b2020-01-09 15:06:40 -08003606 /*
3607 * Use a bitwise-OR instead of a logical-OR to aggregate the
3608 * reserved bit and EPT's invalid memtype/XWR checks to avoid
3609 * adding a Jcc in the loop.
3610 */
Sean Christophersondde81f92020-12-17 16:31:38 -08003611 reserved |= __is_bad_mt_xwr(rsvd_check, sptes[level]) |
3612 __is_rsvd_bits_set(rsvd_check, sptes[level], level);
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003613
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003614 if (reserved) {
Sean Christophersonbb4cdf32021-02-25 12:47:49 -08003615 pr_err("%s: reserved bits set on MMU-present spte, addr 0x%llx, hierarchy:\n",
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003616 __func__, addr);
Ben Gardon95fb5b02020-10-14 11:26:58 -07003617 for (level = root; level >= leaf; level--)
Sean Christophersonbb4cdf32021-02-25 12:47:49 -08003618 pr_err("------ spte = 0x%llx level = %d, rsvd bits = 0x%llx",
3619 sptes[level], level,
3620 rsvd_check->rsvd_bits_mask[(sptes[level] >> 7) & 1][level-1]);
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003621 }
Sean Christophersonddce6202019-12-06 15:57:27 -08003622
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003623 return reserved;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003624}
3625
Paolo Bonzinie08d26f2017-08-17 18:36:56 +02003626static int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr, bool direct)
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003627{
3628 u64 spte;
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003629 bool reserved;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003630
Takuya Yoshikawaded58742016-02-22 17:23:40 +09003631 if (mmio_info_in_cache(vcpu, addr, direct))
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02003632 return RET_PF_EMULATE;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003633
Ben Gardon95fb5b02020-10-14 11:26:58 -07003634 reserved = get_mmio_spte(vcpu, addr, &spte);
Paolo Bonzini450869d2015-11-04 13:41:21 +01003635 if (WARN_ON(reserved))
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02003636 return -EINVAL;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003637
3638 if (is_mmio_spte(spte)) {
3639 gfn_t gfn = get_mmio_spte_gfn(spte);
Ben Gardon0a2b64c2020-02-03 15:09:09 -08003640 unsigned int access = get_mmio_spte_access(spte);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003641
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003642 if (!check_mmio_spte(vcpu, spte))
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02003643 return RET_PF_INVALID;
Xiao Guangrongf8f55942013-06-07 16:51:26 +08003644
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003645 if (direct)
3646 addr = 0;
Xiao Guangrong4f022642011-07-12 03:34:24 +08003647
3648 trace_handle_mmio_page_fault(addr, gfn, access);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003649 vcpu_cache_mmio_info(vcpu, addr, gfn, access);
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02003650 return RET_PF_EMULATE;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003651 }
3652
3653 /*
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003654 * If the page table is zapped by other cpus, let CPU fault again on
3655 * the address.
3656 */
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02003657 return RET_PF_RETRY;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003658}
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003659
Xiao Guangrong3d0c27a2016-02-24 17:51:11 +08003660static bool page_fault_handle_page_track(struct kvm_vcpu *vcpu,
3661 u32 error_code, gfn_t gfn)
3662{
3663 if (unlikely(error_code & PFERR_RSVD_MASK))
3664 return false;
3665
3666 if (!(error_code & PFERR_PRESENT_MASK) ||
3667 !(error_code & PFERR_WRITE_MASK))
3668 return false;
3669
3670 /*
3671 * guest is writing the page which is write tracked which can
3672 * not be fixed by page fault handler.
3673 */
3674 if (kvm_page_track_is_active(vcpu, gfn, KVM_PAGE_TRACK_WRITE))
3675 return true;
3676
3677 return false;
3678}
3679
Xiao Guangronge5691a82016-02-24 17:51:12 +08003680static void shadow_page_table_clear_flood(struct kvm_vcpu *vcpu, gva_t addr)
3681{
3682 struct kvm_shadow_walk_iterator iterator;
3683 u64 spte;
3684
Xiao Guangronge5691a82016-02-24 17:51:12 +08003685 walk_shadow_page_lockless_begin(vcpu);
3686 for_each_shadow_entry_lockless(vcpu, addr, iterator, spte) {
3687 clear_sp_write_flooding_count(iterator.sptep);
3688 if (!is_shadow_present_pte(spte))
3689 break;
3690 }
3691 walk_shadow_page_lockless_end(vcpu);
3692}
3693
Vitaly Kuznetsove8c22262020-06-15 14:13:34 +02003694static bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
3695 gfn_t gfn)
Gleb Natapovaf585b92010-10-14 11:22:46 +02003696{
3697 struct kvm_arch_async_pf arch;
Xiao Guangrongfb67e142010-12-07 10:35:25 +08003698
Gleb Natapov7c907052010-10-14 11:22:53 +02003699 arch.token = (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id;
Gleb Natapovaf585b92010-10-14 11:22:46 +02003700 arch.gfn = gfn;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003701 arch.direct_map = vcpu->arch.mmu->direct_map;
Sean Christophersond8dd54e2020-03-02 18:02:39 -08003702 arch.cr3 = vcpu->arch.mmu->get_guest_pgd(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02003703
Sean Christopherson9f1a8522019-12-06 15:57:17 -08003704 return kvm_setup_async_pf(vcpu, cr2_or_gpa,
3705 kvm_vcpu_gfn_to_hva(vcpu, gfn), &arch);
Gleb Natapovaf585b92010-10-14 11:22:46 +02003706}
3707
Xiao Guangrong78b2c542010-12-07 10:48:06 +08003708static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
David Stevens4a42d842021-02-22 11:45:22 +09003709 gpa_t cr2_or_gpa, kvm_pfn_t *pfn, hva_t *hva,
3710 bool write, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02003711{
Paolo Bonzinic36b7152020-04-16 09:48:07 -04003712 struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
Gleb Natapovaf585b92010-10-14 11:22:46 +02003713 bool async;
3714
Sean Christophersone0c37862021-02-25 12:47:30 -08003715 /*
3716 * Retry the page fault if the gfn hit a memslot that is being deleted
3717 * or moved. This ensures any existing SPTEs for the old memslot will
3718 * be zapped before KVM inserts a new MMIO SPTE for the gfn.
3719 */
3720 if (slot && (slot->flags & KVM_MEMSLOT_INVALID))
3721 return true;
3722
Paolo Bonzinic36b7152020-04-16 09:48:07 -04003723 /* Don't expose private memslots to L2. */
3724 if (is_guest_mode(vcpu) && !kvm_is_visible_memslot(slot)) {
Jim Mattson3a2936d2018-05-09 17:02:05 -04003725 *pfn = KVM_PFN_NOSLOT;
Sean Christophersonc583eed2020-04-15 14:44:13 -07003726 *writable = false;
Jim Mattson3a2936d2018-05-09 17:02:05 -04003727 return false;
3728 }
3729
Paolo Bonzini35204692015-04-02 11:20:48 +02003730 async = false;
David Stevens4a42d842021-02-22 11:45:22 +09003731 *pfn = __gfn_to_pfn_memslot(slot, gfn, false, &async,
3732 write, writable, hva);
Gleb Natapovaf585b92010-10-14 11:22:46 +02003733 if (!async)
3734 return false; /* *pfn has correct page already */
3735
Wanpeng Li9bc1f092017-06-08 20:13:40 -07003736 if (!prefault && kvm_can_do_async_pf(vcpu)) {
Sean Christopherson9f1a8522019-12-06 15:57:17 -08003737 trace_kvm_try_async_get_page(cr2_or_gpa, gfn);
Gleb Natapovaf585b92010-10-14 11:22:46 +02003738 if (kvm_find_async_pf_gfn(vcpu, gfn)) {
Sean Christopherson9f1a8522019-12-06 15:57:17 -08003739 trace_kvm_async_pf_doublefault(cr2_or_gpa, gfn);
Gleb Natapovaf585b92010-10-14 11:22:46 +02003740 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
3741 return true;
Sean Christopherson9f1a8522019-12-06 15:57:17 -08003742 } else if (kvm_arch_setup_async_pf(vcpu, cr2_or_gpa, gfn))
Gleb Natapovaf585b92010-10-14 11:22:46 +02003743 return true;
3744 }
3745
David Stevens4a42d842021-02-22 11:45:22 +09003746 *pfn = __gfn_to_pfn_memslot(slot, gfn, false, NULL,
3747 write, writable, hva);
Gleb Natapovaf585b92010-10-14 11:22:46 +02003748 return false;
3749}
3750
Sean Christopherson0f90e1c2019-12-06 15:57:24 -08003751static int direct_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code,
3752 bool prefault, int max_level, bool is_tdp)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003753{
David Matlack63c0cac2021-06-17 23:19:47 +00003754 bool is_tdp_mmu_fault = is_tdp_mmu(vcpu->arch.mmu);
Sean Christopherson367fd792019-12-06 15:57:16 -08003755 bool write = error_code & PFERR_WRITE_MASK;
Sean Christopherson0f90e1c2019-12-06 15:57:24 -08003756 bool map_writable;
Avi Kivityebeace82007-01-05 16:36:47 -08003757
Sean Christopherson0f90e1c2019-12-06 15:57:24 -08003758 gfn_t gfn = gpa >> PAGE_SHIFT;
3759 unsigned long mmu_seq;
3760 kvm_pfn_t pfn;
David Stevens4a42d842021-02-22 11:45:22 +09003761 hva_t hva;
Sean Christopherson83f06fa2020-01-08 12:24:43 -08003762 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003763
3764 if (page_fault_handle_page_track(vcpu, error_code, gfn))
3765 return RET_PF_EMULATE;
3766
David Matlack0b873fd2021-06-17 23:19:46 +00003767 if (!is_tdp_mmu_fault) {
Ben Gardonbb188422020-10-14 11:26:50 -07003768 r = fast_page_fault(vcpu, gpa, error_code);
3769 if (r != RET_PF_INVALID)
3770 return r;
3771 }
Sean Christopherson83291442020-07-02 19:35:30 -07003772
Sean Christopherson378f5cd2020-07-02 19:35:36 -07003773 r = mmu_topup_memory_caches(vcpu, false);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003774 if (r)
3775 return r;
3776
Sean Christopherson367fd792019-12-06 15:57:16 -08003777 mmu_seq = vcpu->kvm->mmu_notifier_seq;
3778 smp_rmb();
3779
David Stevens4a42d842021-02-22 11:45:22 +09003780 if (try_async_pf(vcpu, prefault, gfn, gpa, &pfn, &hva,
3781 write, &map_writable))
Sean Christopherson367fd792019-12-06 15:57:16 -08003782 return RET_PF_RETRY;
3783
Sean Christopherson0f90e1c2019-12-06 15:57:24 -08003784 if (handle_abnormal_pfn(vcpu, is_tdp ? 0 : gpa, gfn, pfn, ACC_ALL, &r))
Sean Christopherson367fd792019-12-06 15:57:16 -08003785 return r;
3786
3787 r = RET_PF_RETRY;
Ben Gardona2855af2021-02-02 10:57:29 -08003788
David Matlack0b873fd2021-06-17 23:19:46 +00003789 if (is_tdp_mmu_fault)
Ben Gardona2855af2021-02-02 10:57:29 -08003790 read_lock(&vcpu->kvm->mmu_lock);
3791 else
3792 write_lock(&vcpu->kvm->mmu_lock);
3793
David Stevens4a42d842021-02-22 11:45:22 +09003794 if (!is_noslot_pfn(pfn) && mmu_notifier_retry_hva(vcpu->kvm, mmu_seq, hva))
Sean Christopherson367fd792019-12-06 15:57:16 -08003795 goto out_unlock;
Sean Christopherson7bd7ded2020-06-23 12:35:42 -07003796 r = make_mmu_pages_available(vcpu);
3797 if (r)
Sean Christopherson367fd792019-12-06 15:57:16 -08003798 goto out_unlock;
Ben Gardonbb188422020-10-14 11:26:50 -07003799
David Matlack0b873fd2021-06-17 23:19:46 +00003800 if (is_tdp_mmu_fault)
Ben Gardonbb188422020-10-14 11:26:50 -07003801 r = kvm_tdp_mmu_map(vcpu, gpa, error_code, map_writable, max_level,
3802 pfn, prefault);
3803 else
3804 r = __direct_map(vcpu, gpa, error_code, map_writable, max_level, pfn,
3805 prefault, is_tdp);
Sean Christopherson0f90e1c2019-12-06 15:57:24 -08003806
Sean Christopherson367fd792019-12-06 15:57:16 -08003807out_unlock:
David Matlack0b873fd2021-06-17 23:19:46 +00003808 if (is_tdp_mmu_fault)
Ben Gardona2855af2021-02-02 10:57:29 -08003809 read_unlock(&vcpu->kvm->mmu_lock);
3810 else
3811 write_unlock(&vcpu->kvm->mmu_lock);
Sean Christopherson367fd792019-12-06 15:57:16 -08003812 kvm_release_pfn_clean(pfn);
3813 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003814}
3815
Sean Christopherson0f90e1c2019-12-06 15:57:24 -08003816static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa,
3817 u32 error_code, bool prefault)
3818{
3819 pgprintk("%s: gva %lx error %x\n", __func__, gpa, error_code);
3820
3821 /* This path builds a PAE pagetable, we can map 2mb pages at maximum. */
3822 return direct_page_fault(vcpu, gpa & PAGE_MASK, error_code, prefault,
Sean Christopherson3bae0452020-04-27 17:54:22 -07003823 PG_LEVEL_2M, false);
Sean Christopherson0f90e1c2019-12-06 15:57:24 -08003824}
3825
Wanpeng Li1261bfa2017-07-13 18:30:40 -07003826int kvm_handle_page_fault(struct kvm_vcpu *vcpu, u64 error_code,
Paolo Bonzinid0006532017-08-11 18:36:43 +02003827 u64 fault_address, char *insn, int insn_len)
Wanpeng Li1261bfa2017-07-13 18:30:40 -07003828{
3829 int r = 1;
Vitaly Kuznetsov9ce372b2020-05-07 16:36:02 +02003830 u32 flags = vcpu->arch.apf.host_apf_flags;
Wanpeng Li1261bfa2017-07-13 18:30:40 -07003831
Sean Christopherson736c2912019-12-06 15:57:14 -08003832#ifndef CONFIG_X86_64
3833 /* A 64-bit CR2 should be impossible on 32-bit KVM. */
3834 if (WARN_ON_ONCE(fault_address >> 32))
3835 return -EFAULT;
3836#endif
3837
Paolo Bonzinic595cee2018-07-02 13:07:14 +02003838 vcpu->arch.l1tf_flush_l1d = true;
Vitaly Kuznetsov9ce372b2020-05-07 16:36:02 +02003839 if (!flags) {
Wanpeng Li1261bfa2017-07-13 18:30:40 -07003840 trace_kvm_page_fault(fault_address, error_code);
3841
Paolo Bonzinid0006532017-08-11 18:36:43 +02003842 if (kvm_event_needs_reinjection(vcpu))
Wanpeng Li1261bfa2017-07-13 18:30:40 -07003843 kvm_mmu_unprotect_page_virt(vcpu, fault_address);
3844 r = kvm_mmu_page_fault(vcpu, fault_address, error_code, insn,
3845 insn_len);
Vitaly Kuznetsov9ce372b2020-05-07 16:36:02 +02003846 } else if (flags & KVM_PV_REASON_PAGE_NOT_PRESENT) {
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02003847 vcpu->arch.apf.host_apf_flags = 0;
Wanpeng Li1261bfa2017-07-13 18:30:40 -07003848 local_irq_disable();
Thomas Gleixner6bca69a2020-03-07 00:42:06 +01003849 kvm_async_pf_task_wait_schedule(fault_address);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07003850 local_irq_enable();
Vitaly Kuznetsov9ce372b2020-05-07 16:36:02 +02003851 } else {
3852 WARN_ONCE(1, "Unexpected host async PF flags: %x\n", flags);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07003853 }
Vitaly Kuznetsov9ce372b2020-05-07 16:36:02 +02003854
Wanpeng Li1261bfa2017-07-13 18:30:40 -07003855 return r;
3856}
3857EXPORT_SYMBOL_GPL(kvm_handle_page_fault);
3858
Sean Christopherson7a026742020-02-06 14:14:34 -08003859int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code,
3860 bool prefault)
Joerg Roedelfb72d162008-02-07 13:47:44 +01003861{
Sean Christophersoncb9b88c2019-12-06 15:57:18 -08003862 int max_level;
Joerg Roedelfb72d162008-02-07 13:47:44 +01003863
Sean Christophersone662ec32020-04-27 17:54:21 -07003864 for (max_level = KVM_MAX_HUGEPAGE_LEVEL;
Sean Christopherson3bae0452020-04-27 17:54:22 -07003865 max_level > PG_LEVEL_4K;
Sean Christophersoncb9b88c2019-12-06 15:57:18 -08003866 max_level--) {
3867 int page_num = KVM_PAGES_PER_HPAGE(max_level);
Sean Christopherson0f90e1c2019-12-06 15:57:24 -08003868 gfn_t base = (gpa >> PAGE_SHIFT) & ~(page_num - 1);
Joerg Roedelfb72d162008-02-07 13:47:44 +01003869
Sean Christophersoncb9b88c2019-12-06 15:57:18 -08003870 if (kvm_mtrr_check_gfn_range_consistency(vcpu, base, page_num))
3871 break;
Takuya Yoshikawafd136902015-10-16 17:06:02 +09003872 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02003873
Sean Christopherson0f90e1c2019-12-06 15:57:24 -08003874 return direct_page_fault(vcpu, gpa, error_code, prefault,
3875 max_level, true);
Joerg Roedelfb72d162008-02-07 13:47:44 +01003876}
3877
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02003878static void nonpaging_init_context(struct kvm_vcpu *vcpu,
3879 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003880{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003881 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003882 context->gva_to_gpa = nonpaging_gva_to_gpa;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03003883 context->sync_page = nonpaging_sync_page;
Paolo Bonzini5efac072020-03-23 20:42:57 -04003884 context->invlpg = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08003885 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003886 context->shadow_root_level = PT32E_ROOT_LEVEL;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02003887 context->direct_map = true;
Joerg Roedel2d48a982010-09-10 17:31:01 +02003888 context->nx = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003889}
3890
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003891static inline bool is_root_usable(struct kvm_mmu_root_info *root, gpa_t pgd,
Sean Christopherson0be44352020-02-28 14:52:40 -08003892 union kvm_mmu_page_role role)
3893{
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003894 return (role.direct || pgd == root->pgd) &&
Sean Christophersone47c4ae2020-06-22 13:20:34 -07003895 VALID_PAGE(root->hpa) && to_shadow_page(root->hpa) &&
3896 role.word == to_shadow_page(root->hpa)->role.word;
Sean Christopherson0be44352020-02-28 14:52:40 -08003897}
3898
Junaid Shahidb94742c2018-06-27 14:59:20 -07003899/*
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003900 * Find out if a previously cached root matching the new pgd/role is available.
Junaid Shahidb94742c2018-06-27 14:59:20 -07003901 * The current root is also inserted into the cache.
3902 * If a matching root was found, it is assigned to kvm_mmu->root_hpa and true is
3903 * returned.
3904 * Otherwise, the LRU root from the cache is assigned to kvm_mmu->root_hpa and
3905 * false is returned. This root should now be freed by the caller.
3906 */
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003907static bool cached_root_available(struct kvm_vcpu *vcpu, gpa_t new_pgd,
Junaid Shahidb94742c2018-06-27 14:59:20 -07003908 union kvm_mmu_page_role new_role)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003909{
Junaid Shahidb94742c2018-06-27 14:59:20 -07003910 uint i;
3911 struct kvm_mmu_root_info root;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003912 struct kvm_mmu *mmu = vcpu->arch.mmu;
Junaid Shahidb94742c2018-06-27 14:59:20 -07003913
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003914 root.pgd = mmu->root_pgd;
Junaid Shahidb94742c2018-06-27 14:59:20 -07003915 root.hpa = mmu->root_hpa;
3916
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003917 if (is_root_usable(&root, new_pgd, new_role))
Sean Christopherson0be44352020-02-28 14:52:40 -08003918 return true;
3919
Junaid Shahidb94742c2018-06-27 14:59:20 -07003920 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
3921 swap(root, mmu->prev_roots[i]);
3922
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003923 if (is_root_usable(&root, new_pgd, new_role))
Junaid Shahidb94742c2018-06-27 14:59:20 -07003924 break;
3925 }
3926
3927 mmu->root_hpa = root.hpa;
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003928 mmu->root_pgd = root.pgd;
Junaid Shahidb94742c2018-06-27 14:59:20 -07003929
3930 return i < KVM_MMU_NUM_PREV_ROOTS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003931}
3932
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003933static bool fast_pgd_switch(struct kvm_vcpu *vcpu, gpa_t new_pgd,
Sean Christophersonb8698552020-03-20 14:28:26 -07003934 union kvm_mmu_page_role new_role)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003935{
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003936 struct kvm_mmu *mmu = vcpu->arch.mmu;
Junaid Shahid7c390d32018-06-27 14:59:06 -07003937
3938 /*
3939 * For now, limit the fast switch to 64-bit hosts+VMs in order to avoid
3940 * having to deal with PDPTEs. We may add support for 32-bit hosts/VMs
3941 * later if necessary.
3942 */
3943 if (mmu->shadow_root_level >= PT64_ROOT_4LEVEL &&
Sean Christophersonb8698552020-03-20 14:28:26 -07003944 mmu->root_level >= PT64_ROOT_4LEVEL)
Vitaly Kuznetsovfe9304d2020-07-10 16:11:57 +02003945 return cached_root_available(vcpu, new_pgd, new_role);
Junaid Shahid7c390d32018-06-27 14:59:06 -07003946
3947 return false;
3948}
3949
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003950static void __kvm_mmu_new_pgd(struct kvm_vcpu *vcpu, gpa_t new_pgd,
Sean Christophersonb5129102021-06-09 16:42:27 -07003951 union kvm_mmu_page_role new_role)
Junaid Shahid0aab33e2018-06-27 14:59:09 -07003952{
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003953 if (!fast_pgd_switch(vcpu, new_pgd, new_role)) {
Sean Christophersonb8698552020-03-20 14:28:26 -07003954 kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, KVM_MMU_ROOT_CURRENT);
3955 return;
3956 }
3957
3958 /*
3959 * It's possible that the cached previous root page is obsolete because
3960 * of a change in the MMU generation number. However, changing the
3961 * generation number is accompanied by KVM_REQ_MMU_RELOAD, which will
3962 * free the root set here and allocate a new one.
3963 */
3964 kvm_make_request(KVM_REQ_LOAD_MMU_PGD, vcpu);
3965
Sean Christophersonb5129102021-06-09 16:42:27 -07003966 if (force_flush_and_sync_on_reuse) {
Sean Christophersonb8698552020-03-20 14:28:26 -07003967 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
3968 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Sean Christophersonb5129102021-06-09 16:42:27 -07003969 }
Sean Christophersonb8698552020-03-20 14:28:26 -07003970
3971 /*
3972 * The last MMIO access's GVA and GPA are cached in the VCPU. When
3973 * switching to a new CR3, that GVA->GPA mapping may no longer be
3974 * valid. So clear any cached MMIO info even when we don't need to sync
3975 * the shadow page tables.
3976 */
3977 vcpu_clear_mmio_info(vcpu, MMIO_GVA_ANY);
3978
Ben Gardondaa5b6c2020-10-14 11:26:59 -07003979 /*
3980 * If this is a direct root page, it doesn't have a write flooding
3981 * count. Otherwise, clear the write flooding count.
3982 */
3983 if (!new_role.direct)
3984 __clear_sp_write_flooding_count(
3985 to_shadow_page(vcpu->arch.mmu->root_hpa));
Junaid Shahid0aab33e2018-06-27 14:59:09 -07003986}
3987
Sean Christophersonb5129102021-06-09 16:42:27 -07003988void kvm_mmu_new_pgd(struct kvm_vcpu *vcpu, gpa_t new_pgd)
Junaid Shahid7c390d32018-06-27 14:59:06 -07003989{
Sean Christophersonb5129102021-06-09 16:42:27 -07003990 __kvm_mmu_new_pgd(vcpu, new_pgd, kvm_mmu_calc_root_page_role(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003991}
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003992EXPORT_SYMBOL_GPL(kvm_mmu_new_pgd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003993
Joerg Roedel5777ed32010-09-10 17:30:42 +02003994static unsigned long get_cr3(struct kvm_vcpu *vcpu)
3995{
Avi Kivity9f8fe502010-12-05 17:30:00 +02003996 return kvm_read_cr3(vcpu);
Joerg Roedel5777ed32010-09-10 17:30:42 +02003997}
3998
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003999static bool sync_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, gfn_t gfn,
Ben Gardon0a2b64c2020-02-03 15:09:09 -08004000 unsigned int access, int *nr_present)
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004001{
4002 if (unlikely(is_mmio_spte(*sptep))) {
4003 if (gfn != get_mmio_spte_gfn(*sptep)) {
4004 mmu_spte_clear_no_track(sptep);
4005 return true;
4006 }
4007
4008 (*nr_present)++;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02004009 mark_mmio_spte(vcpu, sptep, gfn, access);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004010 return true;
4011 }
4012
4013 return false;
4014}
4015
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004016static inline bool is_last_gpte(struct kvm_mmu *mmu,
4017 unsigned level, unsigned gpte)
Avi Kivity6fd01b72012-09-12 20:46:56 +03004018{
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004019 /*
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004020 * The RHS has bit 7 set iff level < mmu->last_nonleaf_level.
4021 * If it is clear, there are no large pages at this level, so clear
4022 * PT_PAGE_SIZE_MASK in gpte if that is the case.
4023 */
4024 gpte &= level - mmu->last_nonleaf_level;
4025
Ladi Prosek829ee272017-10-05 11:10:23 +02004026 /*
Sean Christopherson3bae0452020-04-27 17:54:22 -07004027 * PG_LEVEL_4K always terminates. The RHS has bit 7 set
4028 * iff level <= PG_LEVEL_4K, which for our purpose means
4029 * level == PG_LEVEL_4K; set PT_PAGE_SIZE_MASK in gpte then.
Ladi Prosek829ee272017-10-05 11:10:23 +02004030 */
Sean Christopherson3bae0452020-04-27 17:54:22 -07004031 gpte |= level - PG_LEVEL_4K - 1;
Ladi Prosek829ee272017-10-05 11:10:23 +02004032
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004033 return gpte & PT_PAGE_SIZE_MASK;
Avi Kivity6fd01b72012-09-12 20:46:56 +03004034}
4035
Nadav Har'El37406aa2013-08-05 11:07:12 +03004036#define PTTYPE_EPT 18 /* arbitrary */
4037#define PTTYPE PTTYPE_EPT
4038#include "paging_tmpl.h"
4039#undef PTTYPE
4040
Avi Kivity6aa8b732006-12-10 02:21:36 -08004041#define PTTYPE 64
4042#include "paging_tmpl.h"
4043#undef PTTYPE
4044
4045#define PTTYPE 32
4046#include "paging_tmpl.h"
4047#undef PTTYPE
4048
Xiao Guangrong6dc98b82015-08-05 12:04:22 +08004049static void
4050__reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
4051 struct rsvd_bits_validate *rsvd_check,
Sean Christopherson5b7f5752021-02-03 16:01:13 -08004052 u64 pa_bits_rsvd, int level, bool nx, bool gbpages,
Paolo Bonzini6fec2142015-09-22 23:02:14 +02004053 bool pse, bool amd)
Dong, Eddie82725b22009-03-30 16:21:08 +08004054{
Nadav Amit5f7dde72014-05-07 15:32:50 +03004055 u64 gbpages_bit_rsvd = 0;
Paolo Bonzinia0c0feb2014-09-02 13:24:12 +02004056 u64 nonleaf_bit8_rsvd = 0;
Sean Christopherson5b7f5752021-02-03 16:01:13 -08004057 u64 high_bits_rsvd;
Dong, Eddie82725b22009-03-30 16:21:08 +08004058
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004059 rsvd_check->bad_mt_xwr = 0;
Yang Zhang25d92082013-08-06 12:00:32 +03004060
Xiao Guangrong6dc98b82015-08-05 12:04:22 +08004061 if (!gbpages)
Nadav Amit5f7dde72014-05-07 15:32:50 +03004062 gbpages_bit_rsvd = rsvd_bits(7, 7);
Paolo Bonzinia0c0feb2014-09-02 13:24:12 +02004063
Sean Christopherson5b7f5752021-02-03 16:01:13 -08004064 if (level == PT32E_ROOT_LEVEL)
4065 high_bits_rsvd = pa_bits_rsvd & rsvd_bits(0, 62);
4066 else
4067 high_bits_rsvd = pa_bits_rsvd & rsvd_bits(0, 51);
4068
4069 /* Note, NX doesn't exist in PDPTEs, this is handled below. */
4070 if (!nx)
4071 high_bits_rsvd |= rsvd_bits(63, 63);
4072
Paolo Bonzinia0c0feb2014-09-02 13:24:12 +02004073 /*
4074 * Non-leaf PML4Es and PDPEs reserve bit 8 (which would be the G bit for
4075 * leaf entries) on AMD CPUs only.
4076 */
Paolo Bonzini6fec2142015-09-22 23:02:14 +02004077 if (amd)
Paolo Bonzinia0c0feb2014-09-02 13:24:12 +02004078 nonleaf_bit8_rsvd = rsvd_bits(8, 8);
4079
Xiao Guangrong6dc98b82015-08-05 12:04:22 +08004080 switch (level) {
Dong, Eddie82725b22009-03-30 16:21:08 +08004081 case PT32_ROOT_LEVEL:
4082 /* no rsvd bits for 2 level 4K page table entries */
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004083 rsvd_check->rsvd_bits_mask[0][1] = 0;
4084 rsvd_check->rsvd_bits_mask[0][0] = 0;
4085 rsvd_check->rsvd_bits_mask[1][0] =
4086 rsvd_check->rsvd_bits_mask[0][0];
Xiao Guangrongf815bce2010-03-19 17:58:53 +08004087
Xiao Guangrong6dc98b82015-08-05 12:04:22 +08004088 if (!pse) {
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004089 rsvd_check->rsvd_bits_mask[1][1] = 0;
Xiao Guangrongf815bce2010-03-19 17:58:53 +08004090 break;
4091 }
4092
Dong, Eddie82725b22009-03-30 16:21:08 +08004093 if (is_cpuid_PSE36())
4094 /* 36bits PSE 4MB page */
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004095 rsvd_check->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08004096 else
4097 /* 32 bits PSE 4MB page */
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004098 rsvd_check->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08004099 break;
4100 case PT32E_ROOT_LEVEL:
Sean Christopherson5b7f5752021-02-03 16:01:13 -08004101 rsvd_check->rsvd_bits_mask[0][2] = rsvd_bits(63, 63) |
4102 high_bits_rsvd |
4103 rsvd_bits(5, 8) |
4104 rsvd_bits(1, 2); /* PDPTE */
4105 rsvd_check->rsvd_bits_mask[0][1] = high_bits_rsvd; /* PDE */
4106 rsvd_check->rsvd_bits_mask[0][0] = high_bits_rsvd; /* PTE */
4107 rsvd_check->rsvd_bits_mask[1][1] = high_bits_rsvd |
4108 rsvd_bits(13, 20); /* large page */
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004109 rsvd_check->rsvd_bits_mask[1][0] =
4110 rsvd_check->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08004111 break;
Yu Zhang855feb62017-08-24 20:27:55 +08004112 case PT64_ROOT_5LEVEL:
Sean Christopherson5b7f5752021-02-03 16:01:13 -08004113 rsvd_check->rsvd_bits_mask[0][4] = high_bits_rsvd |
4114 nonleaf_bit8_rsvd |
4115 rsvd_bits(7, 7);
Yu Zhang855feb62017-08-24 20:27:55 +08004116 rsvd_check->rsvd_bits_mask[1][4] =
4117 rsvd_check->rsvd_bits_mask[0][4];
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004118 fallthrough;
Yu Zhang2a7266a2017-08-24 20:27:54 +08004119 case PT64_ROOT_4LEVEL:
Sean Christopherson5b7f5752021-02-03 16:01:13 -08004120 rsvd_check->rsvd_bits_mask[0][3] = high_bits_rsvd |
4121 nonleaf_bit8_rsvd |
4122 rsvd_bits(7, 7);
4123 rsvd_check->rsvd_bits_mask[0][2] = high_bits_rsvd |
4124 gbpages_bit_rsvd;
4125 rsvd_check->rsvd_bits_mask[0][1] = high_bits_rsvd;
4126 rsvd_check->rsvd_bits_mask[0][0] = high_bits_rsvd;
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004127 rsvd_check->rsvd_bits_mask[1][3] =
4128 rsvd_check->rsvd_bits_mask[0][3];
Sean Christopherson5b7f5752021-02-03 16:01:13 -08004129 rsvd_check->rsvd_bits_mask[1][2] = high_bits_rsvd |
4130 gbpages_bit_rsvd |
4131 rsvd_bits(13, 29);
4132 rsvd_check->rsvd_bits_mask[1][1] = high_bits_rsvd |
4133 rsvd_bits(13, 20); /* large page */
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004134 rsvd_check->rsvd_bits_mask[1][0] =
4135 rsvd_check->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08004136 break;
4137 }
4138}
4139
Xiao Guangrong6dc98b82015-08-05 12:04:22 +08004140static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
4141 struct kvm_mmu *context)
4142{
4143 __reset_rsvds_bits_mask(vcpu, &context->guest_rsvd_check,
Sean Christopherson5b7f5752021-02-03 16:01:13 -08004144 vcpu->arch.reserved_gpa_bits,
4145 context->root_level, context->nx,
Radim Krčmářd6321d42017-08-05 00:12:49 +02004146 guest_cpuid_has(vcpu, X86_FEATURE_GBPAGES),
Sean Christopherson23493d02020-03-04 17:34:33 -08004147 is_pse(vcpu),
4148 guest_cpuid_is_amd_or_hygon(vcpu));
Xiao Guangrong6dc98b82015-08-05 12:04:22 +08004149}
4150
Xiao Guangrong81b8eeb2015-08-05 12:04:23 +08004151static void
4152__reset_rsvds_bits_mask_ept(struct rsvd_bits_validate *rsvd_check,
Sean Christopherson5b7f5752021-02-03 16:01:13 -08004153 u64 pa_bits_rsvd, bool execonly)
Yang Zhang25d92082013-08-06 12:00:32 +03004154{
Sean Christopherson5b7f5752021-02-03 16:01:13 -08004155 u64 high_bits_rsvd = pa_bits_rsvd & rsvd_bits(0, 51);
Paolo Bonzini951f9fd2015-09-23 10:34:26 +02004156 u64 bad_mt_xwr;
Yang Zhang25d92082013-08-06 12:00:32 +03004157
Sean Christopherson5b7f5752021-02-03 16:01:13 -08004158 rsvd_check->rsvd_bits_mask[0][4] = high_bits_rsvd | rsvd_bits(3, 7);
4159 rsvd_check->rsvd_bits_mask[0][3] = high_bits_rsvd | rsvd_bits(3, 7);
4160 rsvd_check->rsvd_bits_mask[0][2] = high_bits_rsvd | rsvd_bits(3, 6);
4161 rsvd_check->rsvd_bits_mask[0][1] = high_bits_rsvd | rsvd_bits(3, 6);
4162 rsvd_check->rsvd_bits_mask[0][0] = high_bits_rsvd;
Yang Zhang25d92082013-08-06 12:00:32 +03004163
4164 /* large page */
Yu Zhang855feb62017-08-24 20:27:55 +08004165 rsvd_check->rsvd_bits_mask[1][4] = rsvd_check->rsvd_bits_mask[0][4];
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004166 rsvd_check->rsvd_bits_mask[1][3] = rsvd_check->rsvd_bits_mask[0][3];
Sean Christopherson5b7f5752021-02-03 16:01:13 -08004167 rsvd_check->rsvd_bits_mask[1][2] = high_bits_rsvd | rsvd_bits(12, 29);
4168 rsvd_check->rsvd_bits_mask[1][1] = high_bits_rsvd | rsvd_bits(12, 20);
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004169 rsvd_check->rsvd_bits_mask[1][0] = rsvd_check->rsvd_bits_mask[0][0];
Yang Zhang25d92082013-08-06 12:00:32 +03004170
Paolo Bonzini951f9fd2015-09-23 10:34:26 +02004171 bad_mt_xwr = 0xFFull << (2 * 8); /* bits 3..5 must not be 2 */
4172 bad_mt_xwr |= 0xFFull << (3 * 8); /* bits 3..5 must not be 3 */
4173 bad_mt_xwr |= 0xFFull << (7 * 8); /* bits 3..5 must not be 7 */
4174 bad_mt_xwr |= REPEAT_BYTE(1ull << 2); /* bits 0..2 must not be 010 */
4175 bad_mt_xwr |= REPEAT_BYTE(1ull << 6); /* bits 0..2 must not be 110 */
4176 if (!execonly) {
4177 /* bits 0..2 must not be 100 unless VMX capabilities allow it */
4178 bad_mt_xwr |= REPEAT_BYTE(1ull << 4);
Yang Zhang25d92082013-08-06 12:00:32 +03004179 }
Paolo Bonzini951f9fd2015-09-23 10:34:26 +02004180 rsvd_check->bad_mt_xwr = bad_mt_xwr;
Yang Zhang25d92082013-08-06 12:00:32 +03004181}
4182
Xiao Guangrong81b8eeb2015-08-05 12:04:23 +08004183static void reset_rsvds_bits_mask_ept(struct kvm_vcpu *vcpu,
4184 struct kvm_mmu *context, bool execonly)
4185{
4186 __reset_rsvds_bits_mask_ept(&context->guest_rsvd_check,
Sean Christopherson5b7f5752021-02-03 16:01:13 -08004187 vcpu->arch.reserved_gpa_bits, execonly);
Xiao Guangrong81b8eeb2015-08-05 12:04:23 +08004188}
4189
Sean Christopherson6f8e65a2021-02-03 16:01:14 -08004190static inline u64 reserved_hpa_bits(void)
4191{
4192 return rsvd_bits(shadow_phys_bits, 63);
4193}
4194
Xiao Guangrongc258b622015-08-05 12:04:24 +08004195/*
4196 * the page table on host is the shadow page table for the page
4197 * table in guest or amd nested guest, its mmu features completely
4198 * follow the features in guest.
4199 */
4200void
4201reset_shadow_zero_bits_mask(struct kvm_vcpu *vcpu, struct kvm_mmu *context)
4202{
Sean Christopherson112022b2021-06-22 10:56:47 -07004203 /*
4204 * KVM uses NX when TDP is disabled to handle a variety of scenarios,
4205 * notably for huge SPTEs if iTLB multi-hit mitigation is enabled and
4206 * to generate correct permissions for CR0.WP=0/CR4.SMEP=1/EFER.NX=0.
4207 * The iTLB multi-hit workaround can be toggled at any time, so assume
4208 * NX can be used by any non-nested shadow MMU to avoid having to reset
4209 * MMU contexts. Note, KVM forces EFER.NX=1 when TDP is disabled.
4210 */
4211 bool uses_nx = context->nx || !tdp_enabled ||
Vitaly Kuznetsov36d9594d2018-10-08 21:28:10 +02004212 context->mmu_role.base.smep_andnot_wp;
Brijesh Singhea2800d2017-08-25 15:55:40 -05004213 struct rsvd_bits_validate *shadow_zero_check;
4214 int i;
Paolo Bonzini5f0b8192016-03-09 14:28:02 +01004215
Paolo Bonzini6fec2142015-09-22 23:02:14 +02004216 /*
4217 * Passing "true" to the last argument is okay; it adds a check
4218 * on bit 8 of the SPTEs which KVM doesn't use anyway.
4219 */
Brijesh Singhea2800d2017-08-25 15:55:40 -05004220 shadow_zero_check = &context->shadow_zero_check;
4221 __reset_rsvds_bits_mask(vcpu, shadow_zero_check,
Sean Christopherson6f8e65a2021-02-03 16:01:14 -08004222 reserved_hpa_bits(),
Paolo Bonzini5f0b8192016-03-09 14:28:02 +01004223 context->shadow_root_level, uses_nx,
Radim Krčmářd6321d42017-08-05 00:12:49 +02004224 guest_cpuid_has(vcpu, X86_FEATURE_GBPAGES),
4225 is_pse(vcpu), true);
Brijesh Singhea2800d2017-08-25 15:55:40 -05004226
4227 if (!shadow_me_mask)
4228 return;
4229
4230 for (i = context->shadow_root_level; --i >= 0;) {
4231 shadow_zero_check->rsvd_bits_mask[0][i] &= ~shadow_me_mask;
4232 shadow_zero_check->rsvd_bits_mask[1][i] &= ~shadow_me_mask;
4233 }
4234
Xiao Guangrongc258b622015-08-05 12:04:24 +08004235}
4236EXPORT_SYMBOL_GPL(reset_shadow_zero_bits_mask);
4237
Paolo Bonzini6fec2142015-09-22 23:02:14 +02004238static inline bool boot_cpu_is_amd(void)
4239{
4240 WARN_ON_ONCE(!tdp_enabled);
4241 return shadow_x_mask == 0;
4242}
4243
Xiao Guangrongc258b622015-08-05 12:04:24 +08004244/*
4245 * the direct page table on host, use as much mmu features as
4246 * possible, however, kvm currently does not do execution-protection.
4247 */
4248static void
4249reset_tdp_shadow_zero_bits_mask(struct kvm_vcpu *vcpu,
4250 struct kvm_mmu *context)
4251{
Brijesh Singhea2800d2017-08-25 15:55:40 -05004252 struct rsvd_bits_validate *shadow_zero_check;
4253 int i;
4254
4255 shadow_zero_check = &context->shadow_zero_check;
4256
Paolo Bonzini6fec2142015-09-22 23:02:14 +02004257 if (boot_cpu_is_amd())
Brijesh Singhea2800d2017-08-25 15:55:40 -05004258 __reset_rsvds_bits_mask(vcpu, shadow_zero_check,
Sean Christopherson6f8e65a2021-02-03 16:01:14 -08004259 reserved_hpa_bits(),
Xiao Guangrongc258b622015-08-05 12:04:24 +08004260 context->shadow_root_level, false,
Borislav Petkovb8291adc2016-03-29 17:41:58 +02004261 boot_cpu_has(X86_FEATURE_GBPAGES),
4262 true, true);
Xiao Guangrongc258b622015-08-05 12:04:24 +08004263 else
Brijesh Singhea2800d2017-08-25 15:55:40 -05004264 __reset_rsvds_bits_mask_ept(shadow_zero_check,
Sean Christopherson6f8e65a2021-02-03 16:01:14 -08004265 reserved_hpa_bits(), false);
Xiao Guangrongc258b622015-08-05 12:04:24 +08004266
Brijesh Singhea2800d2017-08-25 15:55:40 -05004267 if (!shadow_me_mask)
4268 return;
4269
4270 for (i = context->shadow_root_level; --i >= 0;) {
4271 shadow_zero_check->rsvd_bits_mask[0][i] &= ~shadow_me_mask;
4272 shadow_zero_check->rsvd_bits_mask[1][i] &= ~shadow_me_mask;
4273 }
Xiao Guangrongc258b622015-08-05 12:04:24 +08004274}
4275
4276/*
4277 * as the comments in reset_shadow_zero_bits_mask() except it
4278 * is the shadow page table for intel nested guest.
4279 */
4280static void
4281reset_ept_shadow_zero_bits_mask(struct kvm_vcpu *vcpu,
4282 struct kvm_mmu *context, bool execonly)
4283{
4284 __reset_rsvds_bits_mask_ept(&context->shadow_zero_check,
Sean Christopherson6f8e65a2021-02-03 16:01:14 -08004285 reserved_hpa_bits(), execonly);
Xiao Guangrongc258b622015-08-05 12:04:24 +08004286}
4287
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004288#define BYTE_MASK(access) \
4289 ((1 & (access) ? 2 : 0) | \
4290 (2 & (access) ? 4 : 0) | \
4291 (3 & (access) ? 8 : 0) | \
4292 (4 & (access) ? 16 : 0) | \
4293 (5 & (access) ? 32 : 0) | \
4294 (6 & (access) ? 64 : 0) | \
4295 (7 & (access) ? 128 : 0))
4296
4297
Xiao Guangrongedc90b72015-05-11 22:55:21 +08004298static void update_permission_bitmask(struct kvm_vcpu *vcpu,
4299 struct kvm_mmu *mmu, bool ept)
Avi Kivity97d64b72012-09-12 14:52:00 +03004300{
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004301 unsigned byte;
Avi Kivity97d64b72012-09-12 14:52:00 +03004302
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004303 const u8 x = BYTE_MASK(ACC_EXEC_MASK);
4304 const u8 w = BYTE_MASK(ACC_WRITE_MASK);
4305 const u8 u = BYTE_MASK(ACC_USER_MASK);
4306
4307 bool cr4_smep = kvm_read_cr4_bits(vcpu, X86_CR4_SMEP) != 0;
4308 bool cr4_smap = kvm_read_cr4_bits(vcpu, X86_CR4_SMAP) != 0;
4309 bool cr0_wp = is_write_protection(vcpu);
4310
Avi Kivity97d64b72012-09-12 14:52:00 +03004311 for (byte = 0; byte < ARRAY_SIZE(mmu->permissions); ++byte) {
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004312 unsigned pfec = byte << 1;
4313
Feng Wu97ec8c02014-04-01 17:46:34 +08004314 /*
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004315 * Each "*f" variable has a 1 bit for each UWX value
4316 * that causes a fault with the given PFEC.
Feng Wu97ec8c02014-04-01 17:46:34 +08004317 */
Avi Kivity97d64b72012-09-12 14:52:00 +03004318
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004319 /* Faults from writes to non-writable pages */
Arnd Bergmanna6a6d3b2019-07-12 11:12:30 +02004320 u8 wf = (pfec & PFERR_WRITE_MASK) ? (u8)~w : 0;
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004321 /* Faults from user mode accesses to supervisor pages */
Arnd Bergmanna6a6d3b2019-07-12 11:12:30 +02004322 u8 uf = (pfec & PFERR_USER_MASK) ? (u8)~u : 0;
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004323 /* Faults from fetches of non-executable pages*/
Arnd Bergmanna6a6d3b2019-07-12 11:12:30 +02004324 u8 ff = (pfec & PFERR_FETCH_MASK) ? (u8)~x : 0;
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004325 /* Faults from kernel mode fetches of user pages */
4326 u8 smepf = 0;
4327 /* Faults from kernel mode accesses of user pages */
4328 u8 smapf = 0;
Feng Wu97ec8c02014-04-01 17:46:34 +08004329
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004330 if (!ept) {
4331 /* Faults from kernel mode accesses to user pages */
4332 u8 kf = (pfec & PFERR_USER_MASK) ? 0 : u;
Avi Kivity97d64b72012-09-12 14:52:00 +03004333
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004334 /* Not really needed: !nx will cause pte.nx to fault */
4335 if (!mmu->nx)
4336 ff = 0;
4337
4338 /* Allow supervisor writes if !cr0.wp */
4339 if (!cr0_wp)
4340 wf = (pfec & PFERR_USER_MASK) ? wf : 0;
4341
4342 /* Disallow supervisor fetches of user code if cr4.smep */
4343 if (cr4_smep)
4344 smepf = (pfec & PFERR_FETCH_MASK) ? kf : 0;
4345
4346 /*
4347 * SMAP:kernel-mode data accesses from user-mode
4348 * mappings should fault. A fault is considered
4349 * as a SMAP violation if all of the following
Peng Hao39337ad2018-10-04 11:45:00 -04004350 * conditions are true:
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004351 * - X86_CR4_SMAP is set in CR4
4352 * - A user page is accessed
4353 * - The access is not a fetch
4354 * - Page fault in kernel mode
4355 * - if CPL = 3 or X86_EFLAGS_AC is clear
4356 *
4357 * Here, we cover the first three conditions.
4358 * The fourth is computed dynamically in permission_fault();
4359 * PFERR_RSVD_MASK bit will be set in PFEC if the access is
4360 * *not* subject to SMAP restrictions.
4361 */
4362 if (cr4_smap)
4363 smapf = (pfec & (PFERR_RSVD_MASK|PFERR_FETCH_MASK)) ? 0 : kf;
Avi Kivity97d64b72012-09-12 14:52:00 +03004364 }
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004365
4366 mmu->permissions[byte] = ff | uf | wf | smepf | smapf;
Avi Kivity97d64b72012-09-12 14:52:00 +03004367 }
4368}
4369
Huaitong Han2d344102016-03-22 16:51:19 +08004370/*
4371* PKU is an additional mechanism by which the paging controls access to
4372* user-mode addresses based on the value in the PKRU register. Protection
4373* key violations are reported through a bit in the page fault error code.
4374* Unlike other bits of the error code, the PK bit is not known at the
4375* call site of e.g. gva_to_gpa; it must be computed directly in
4376* permission_fault based on two bits of PKRU, on some machine state (CR4,
4377* CR0, EFER, CPL), and on other bits of the error code and the page tables.
4378*
4379* In particular the following conditions come from the error code, the
4380* page tables and the machine state:
4381* - PK is always zero unless CR4.PKE=1 and EFER.LMA=1
4382* - PK is always zero if RSVD=1 (reserved bit set) or F=1 (instruction fetch)
4383* - PK is always zero if U=0 in the page tables
4384* - PKRU.WD is ignored if CR0.WP=0 and the access is a supervisor access.
4385*
4386* The PKRU bitmask caches the result of these four conditions. The error
4387* code (minus the P bit) and the page table's U bit form an index into the
4388* PKRU bitmask. Two bits of the PKRU bitmask are then extracted and ANDed
4389* with the two bits of the PKRU register corresponding to the protection key.
4390* For the first three conditions above the bits will be 00, thus masking
4391* away both AD and WD. For all reads or if the last condition holds, WD
4392* only will be masked away.
4393*/
4394static void update_pkru_bitmask(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
4395 bool ept)
4396{
4397 unsigned bit;
4398 bool wp;
4399
4400 if (ept) {
4401 mmu->pkru_mask = 0;
4402 return;
4403 }
4404
4405 /* PKEY is enabled only if CR4.PKE and EFER.LMA are both set. */
4406 if (!kvm_read_cr4_bits(vcpu, X86_CR4_PKE) || !is_long_mode(vcpu)) {
4407 mmu->pkru_mask = 0;
4408 return;
4409 }
4410
4411 wp = is_write_protection(vcpu);
4412
4413 for (bit = 0; bit < ARRAY_SIZE(mmu->permissions); ++bit) {
4414 unsigned pfec, pkey_bits;
4415 bool check_pkey, check_write, ff, uf, wf, pte_user;
4416
4417 pfec = bit << 1;
4418 ff = pfec & PFERR_FETCH_MASK;
4419 uf = pfec & PFERR_USER_MASK;
4420 wf = pfec & PFERR_WRITE_MASK;
4421
4422 /* PFEC.RSVD is replaced by ACC_USER_MASK. */
4423 pte_user = pfec & PFERR_RSVD_MASK;
4424
4425 /*
4426 * Only need to check the access which is not an
4427 * instruction fetch and is to a user page.
4428 */
4429 check_pkey = (!ff && pte_user);
4430 /*
4431 * write access is controlled by PKRU if it is a
4432 * user access or CR0.WP = 1.
4433 */
4434 check_write = check_pkey && wf && (uf || wp);
4435
4436 /* PKRU.AD stops both read and write access. */
4437 pkey_bits = !!check_pkey;
4438 /* PKRU.WD stops write access. */
4439 pkey_bits |= (!!check_write) << 1;
4440
4441 mmu->pkru_mask |= (pkey_bits & 3) << pfec;
4442 }
4443}
4444
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004445static void update_last_nonleaf_level(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu)
Avi Kivity6fd01b72012-09-12 20:46:56 +03004446{
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004447 unsigned root_level = mmu->root_level;
Avi Kivity6fd01b72012-09-12 20:46:56 +03004448
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004449 mmu->last_nonleaf_level = root_level;
4450 if (root_level == PT32_ROOT_LEVEL && is_pse(vcpu))
4451 mmu->last_nonleaf_level++;
Avi Kivity6fd01b72012-09-12 20:46:56 +03004452}
4453
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004454static void paging64_init_context_common(struct kvm_vcpu *vcpu,
4455 struct kvm_mmu *context,
4456 int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004457{
Joerg Roedel2d48a982010-09-10 17:31:01 +02004458 context->nx = is_nx(vcpu);
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004459 context->root_level = level;
Joerg Roedel2d48a982010-09-10 17:31:01 +02004460
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004461 reset_rsvds_bits_mask(vcpu, context);
Yang Zhang25d92082013-08-06 12:00:32 +03004462 update_permission_bitmask(vcpu, context, false);
Huaitong Han2d344102016-03-22 16:51:19 +08004463 update_pkru_bitmask(vcpu, context, false);
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004464 update_last_nonleaf_level(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004465
Paolo Bonzinifa4a2c02013-10-02 16:56:16 +02004466 MMU_WARN_ON(!is_pae(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004467 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004468 context->gva_to_gpa = paging64_gva_to_gpa;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03004469 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03004470 context->invlpg = paging64_invlpg;
Avi Kivity17ac10a2007-01-05 16:36:40 -08004471 context->shadow_root_level = level;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02004472 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004473}
4474
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004475static void paging64_init_context(struct kvm_vcpu *vcpu,
4476 struct kvm_mmu *context)
Avi Kivity17ac10a2007-01-05 16:36:40 -08004477{
Yu Zhang855feb62017-08-24 20:27:55 +08004478 int root_level = is_la57_mode(vcpu) ?
4479 PT64_ROOT_5LEVEL : PT64_ROOT_4LEVEL;
4480
4481 paging64_init_context_common(vcpu, context, root_level);
Avi Kivity17ac10a2007-01-05 16:36:40 -08004482}
4483
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004484static void paging32_init_context(struct kvm_vcpu *vcpu,
4485 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004486{
Joerg Roedel2d48a982010-09-10 17:31:01 +02004487 context->nx = false;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004488 context->root_level = PT32_ROOT_LEVEL;
Joerg Roedel2d48a982010-09-10 17:31:01 +02004489
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004490 reset_rsvds_bits_mask(vcpu, context);
Yang Zhang25d92082013-08-06 12:00:32 +03004491 update_permission_bitmask(vcpu, context, false);
Huaitong Han2d344102016-03-22 16:51:19 +08004492 update_pkru_bitmask(vcpu, context, false);
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004493 update_last_nonleaf_level(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004494
Avi Kivity6aa8b732006-12-10 02:21:36 -08004495 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004496 context->gva_to_gpa = paging32_gva_to_gpa;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03004497 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03004498 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004499 context->shadow_root_level = PT32E_ROOT_LEVEL;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02004500 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004501}
4502
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004503static void paging32E_init_context(struct kvm_vcpu *vcpu,
4504 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004505{
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004506 paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004507}
4508
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004509static union kvm_mmu_extended_role kvm_calc_mmu_role_ext(struct kvm_vcpu *vcpu)
Junaid Shahid9fa72112018-06-27 14:59:07 -07004510{
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004511 union kvm_mmu_extended_role ext = {0};
Junaid Shahid9fa72112018-06-27 14:59:07 -07004512
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004513 ext.cr0_pg = !!is_paging(vcpu);
Vitaly Kuznetsov0699c642019-04-30 19:33:26 +02004514 ext.cr4_pae = !!is_pae(vcpu);
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004515 ext.cr4_smep = !!kvm_read_cr4_bits(vcpu, X86_CR4_SMEP);
4516 ext.cr4_smap = !!kvm_read_cr4_bits(vcpu, X86_CR4_SMAP);
4517 ext.cr4_pse = !!is_pse(vcpu);
4518 ext.cr4_pke = !!kvm_read_cr4_bits(vcpu, X86_CR4_PKE);
Sean Christophersonf71a53d2021-06-22 10:56:50 -07004519 ext.cr4_la57 = !!kvm_read_cr4_bits(vcpu, X86_CR4_LA57);
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004520
4521 ext.valid = 1;
4522
4523 return ext;
4524}
4525
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004526static union kvm_mmu_role kvm_calc_mmu_role_common(struct kvm_vcpu *vcpu,
4527 bool base_only)
Junaid Shahid9fa72112018-06-27 14:59:07 -07004528{
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004529 union kvm_mmu_role role = {0};
Junaid Shahid9fa72112018-06-27 14:59:07 -07004530
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004531 role.base.access = ACC_ALL;
4532 role.base.nxe = !!is_nx(vcpu);
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004533 role.base.cr0_wp = is_write_protection(vcpu);
4534 role.base.smm = is_smm(vcpu);
4535 role.base.guest_mode = is_guest_mode(vcpu);
4536
4537 if (base_only)
4538 return role;
4539
4540 role.ext = kvm_calc_mmu_role_ext(vcpu);
4541
4542 return role;
4543}
4544
Sean Christophersond468d942020-07-15 20:41:20 -07004545static inline int kvm_mmu_get_tdp_level(struct kvm_vcpu *vcpu)
4546{
4547 /* Use 5-level TDP if and only if it's useful/necessary. */
Sean Christopherson83013052020-07-15 20:41:22 -07004548 if (max_tdp_level == 5 && cpuid_maxphyaddr(vcpu) <= 48)
Sean Christophersond468d942020-07-15 20:41:20 -07004549 return 4;
4550
Sean Christopherson83013052020-07-15 20:41:22 -07004551 return max_tdp_level;
Sean Christophersond468d942020-07-15 20:41:20 -07004552}
4553
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004554static union kvm_mmu_role
4555kvm_calc_tdp_mmu_root_page_role(struct kvm_vcpu *vcpu, bool base_only)
4556{
4557 union kvm_mmu_role role = kvm_calc_mmu_role_common(vcpu, base_only);
4558
4559 role.base.ad_disabled = (shadow_accessed_mask == 0);
Sean Christophersond468d942020-07-15 20:41:20 -07004560 role.base.level = kvm_mmu_get_tdp_level(vcpu);
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004561 role.base.direct = true;
Sean Christopherson47c42e62019-03-07 15:27:44 -08004562 role.base.gpte_is_8_bytes = true;
Junaid Shahid9fa72112018-06-27 14:59:07 -07004563
4564 return role;
4565}
4566
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004567static void init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
Joerg Roedelfb72d162008-02-07 13:47:44 +01004568{
Paolo Bonzini8c008652020-07-10 16:11:50 +02004569 struct kvm_mmu *context = &vcpu->arch.root_mmu;
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004570 union kvm_mmu_role new_role =
4571 kvm_calc_tdp_mmu_root_page_role(vcpu, false);
Joerg Roedelfb72d162008-02-07 13:47:44 +01004572
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004573 if (new_role.as_u64 == context->mmu_role.as_u64)
4574 return;
4575
4576 context->mmu_role.as_u64 = new_role.as_u64;
Sean Christopherson7a026742020-02-06 14:14:34 -08004577 context->page_fault = kvm_tdp_page_fault;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03004578 context->sync_page = nonpaging_sync_page;
Paolo Bonzini5efac072020-03-23 20:42:57 -04004579 context->invlpg = NULL;
Sean Christophersond468d942020-07-15 20:41:20 -07004580 context->shadow_root_level = kvm_mmu_get_tdp_level(vcpu);
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02004581 context->direct_map = true;
Sean Christophersond8dd54e2020-03-02 18:02:39 -08004582 context->get_guest_pgd = get_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03004583 context->get_pdptr = kvm_pdptr_read;
Joerg Roedelcb659db2010-09-10 17:30:43 +02004584 context->inject_page_fault = kvm_inject_page_fault;
Joerg Roedelfb72d162008-02-07 13:47:44 +01004585
4586 if (!is_paging(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02004587 context->nx = false;
Joerg Roedelfb72d162008-02-07 13:47:44 +01004588 context->gva_to_gpa = nonpaging_gva_to_gpa;
4589 context->root_level = 0;
4590 } else if (is_long_mode(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02004591 context->nx = is_nx(vcpu);
Yu Zhang855feb62017-08-24 20:27:55 +08004592 context->root_level = is_la57_mode(vcpu) ?
4593 PT64_ROOT_5LEVEL : PT64_ROOT_4LEVEL;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004594 reset_rsvds_bits_mask(vcpu, context);
4595 context->gva_to_gpa = paging64_gva_to_gpa;
Joerg Roedelfb72d162008-02-07 13:47:44 +01004596 } else if (is_pae(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02004597 context->nx = is_nx(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01004598 context->root_level = PT32E_ROOT_LEVEL;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004599 reset_rsvds_bits_mask(vcpu, context);
4600 context->gva_to_gpa = paging64_gva_to_gpa;
Joerg Roedelfb72d162008-02-07 13:47:44 +01004601 } else {
Joerg Roedel2d48a982010-09-10 17:31:01 +02004602 context->nx = false;
Joerg Roedelfb72d162008-02-07 13:47:44 +01004603 context->root_level = PT32_ROOT_LEVEL;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004604 reset_rsvds_bits_mask(vcpu, context);
4605 context->gva_to_gpa = paging32_gva_to_gpa;
Joerg Roedelfb72d162008-02-07 13:47:44 +01004606 }
4607
Yang Zhang25d92082013-08-06 12:00:32 +03004608 update_permission_bitmask(vcpu, context, false);
Huaitong Han2d344102016-03-22 16:51:19 +08004609 update_pkru_bitmask(vcpu, context, false);
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004610 update_last_nonleaf_level(vcpu, context);
Xiao Guangrongc258b622015-08-05 12:04:24 +08004611 reset_tdp_shadow_zero_bits_mask(vcpu, context);
Joerg Roedelfb72d162008-02-07 13:47:44 +01004612}
4613
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004614static union kvm_mmu_role
Sean Christopherson59505b52020-07-15 20:41:15 -07004615kvm_calc_shadow_root_page_role_common(struct kvm_vcpu *vcpu, bool base_only)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004616{
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004617 union kvm_mmu_role role = kvm_calc_mmu_role_common(vcpu, base_only);
Paolo Bonziniad896af2013-10-02 16:56:14 +02004618
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004619 role.base.smep_andnot_wp = role.ext.cr4_smep &&
4620 !is_write_protection(vcpu);
4621 role.base.smap_andnot_wp = role.ext.cr4_smap &&
4622 !is_write_protection(vcpu);
Sean Christopherson47c42e62019-03-07 15:27:44 -08004623 role.base.gpte_is_8_bytes = !!is_pae(vcpu);
Junaid Shahid9fa72112018-06-27 14:59:07 -07004624
Sean Christopherson59505b52020-07-15 20:41:15 -07004625 return role;
4626}
4627
4628static union kvm_mmu_role
4629kvm_calc_shadow_mmu_root_page_role(struct kvm_vcpu *vcpu, bool base_only)
4630{
4631 union kvm_mmu_role role =
4632 kvm_calc_shadow_root_page_role_common(vcpu, base_only);
4633
4634 role.base.direct = !is_paging(vcpu);
4635
Junaid Shahid9fa72112018-06-27 14:59:07 -07004636 if (!is_long_mode(vcpu))
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004637 role.base.level = PT32E_ROOT_LEVEL;
Junaid Shahid9fa72112018-06-27 14:59:07 -07004638 else if (is_la57_mode(vcpu))
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004639 role.base.level = PT64_ROOT_5LEVEL;
Junaid Shahid9fa72112018-06-27 14:59:07 -07004640 else
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004641 role.base.level = PT64_ROOT_4LEVEL;
Junaid Shahid9fa72112018-06-27 14:59:07 -07004642
4643 return role;
4644}
4645
Paolo Bonzini8c008652020-07-10 16:11:50 +02004646static void shadow_mmu_init_context(struct kvm_vcpu *vcpu, struct kvm_mmu *context,
4647 u32 cr0, u32 cr4, u32 efer,
4648 union kvm_mmu_role new_role)
Junaid Shahid9fa72112018-06-27 14:59:07 -07004649{
Paolo Bonzini929d1cf2020-05-19 06:18:31 -04004650 if (!(cr0 & X86_CR0_PG))
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004651 nonpaging_init_context(vcpu, context);
Paolo Bonzini929d1cf2020-05-19 06:18:31 -04004652 else if (efer & EFER_LMA)
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004653 paging64_init_context(vcpu, context);
Paolo Bonzini929d1cf2020-05-19 06:18:31 -04004654 else if (cr4 & X86_CR4_PAE)
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004655 paging32E_init_context(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004656 else
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004657 paging32_init_context(vcpu, context);
Avi Kivitya770f6f2008-12-21 19:20:09 +02004658
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004659 context->mmu_role.as_u64 = new_role.as_u64;
Xiao Guangrongc258b622015-08-05 12:04:24 +08004660 reset_shadow_zero_bits_mask(vcpu, context);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02004661}
Vitaly Kuznetsov0f04a2a2020-07-10 16:11:49 +02004662
4663static void kvm_init_shadow_mmu(struct kvm_vcpu *vcpu, u32 cr0, u32 cr4, u32 efer)
4664{
Paolo Bonzini8c008652020-07-10 16:11:50 +02004665 struct kvm_mmu *context = &vcpu->arch.root_mmu;
Vitaly Kuznetsov0f04a2a2020-07-10 16:11:49 +02004666 union kvm_mmu_role new_role =
4667 kvm_calc_shadow_mmu_root_page_role(vcpu, false);
4668
4669 if (new_role.as_u64 != context->mmu_role.as_u64)
Paolo Bonzini8c008652020-07-10 16:11:50 +02004670 shadow_mmu_init_context(vcpu, context, cr0, cr4, efer, new_role);
Vitaly Kuznetsov0f04a2a2020-07-10 16:11:49 +02004671}
4672
Sean Christopherson59505b52020-07-15 20:41:15 -07004673static union kvm_mmu_role
4674kvm_calc_shadow_npt_root_page_role(struct kvm_vcpu *vcpu)
4675{
4676 union kvm_mmu_role role =
4677 kvm_calc_shadow_root_page_role_common(vcpu, false);
4678
4679 role.base.direct = false;
Sean Christophersond468d942020-07-15 20:41:20 -07004680 role.base.level = kvm_mmu_get_tdp_level(vcpu);
Sean Christopherson59505b52020-07-15 20:41:15 -07004681
4682 return role;
4683}
4684
Vitaly Kuznetsov0f04a2a2020-07-10 16:11:49 +02004685void kvm_init_shadow_npt_mmu(struct kvm_vcpu *vcpu, u32 cr0, u32 cr4, u32 efer,
4686 gpa_t nested_cr3)
4687{
Paolo Bonzini8c008652020-07-10 16:11:50 +02004688 struct kvm_mmu *context = &vcpu->arch.guest_mmu;
Sean Christopherson59505b52020-07-15 20:41:15 -07004689 union kvm_mmu_role new_role = kvm_calc_shadow_npt_root_page_role(vcpu);
Vitaly Kuznetsov0f04a2a2020-07-10 16:11:49 +02004690
Sean Christophersonb5129102021-06-09 16:42:27 -07004691 __kvm_mmu_new_pgd(vcpu, nested_cr3, new_role.base);
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +02004692
Sean Christophersona3322d52021-03-04 17:10:45 -08004693 if (new_role.as_u64 != context->mmu_role.as_u64) {
Paolo Bonzini8c008652020-07-10 16:11:50 +02004694 shadow_mmu_init_context(vcpu, context, cr0, cr4, efer, new_role);
Sean Christophersona3322d52021-03-04 17:10:45 -08004695
4696 /*
4697 * Override the level set by the common init helper, nested TDP
4698 * always uses the host's TDP configuration.
4699 */
4700 context->shadow_root_level = new_role.base.level;
4701 }
Vitaly Kuznetsov0f04a2a2020-07-10 16:11:49 +02004702}
4703EXPORT_SYMBOL_GPL(kvm_init_shadow_npt_mmu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02004704
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004705static union kvm_mmu_role
4706kvm_calc_shadow_ept_root_page_role(struct kvm_vcpu *vcpu, bool accessed_dirty,
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08004707 bool execonly, u8 level)
Junaid Shahid9fa72112018-06-27 14:59:07 -07004708{
Sean Christopherson552c69b12019-03-07 15:27:43 -08004709 union kvm_mmu_role role = {0};
Junaid Shahid9fa72112018-06-27 14:59:07 -07004710
Sean Christopherson47c42e62019-03-07 15:27:44 -08004711 /* SMM flag is inherited from root_mmu */
4712 role.base.smm = vcpu->arch.root_mmu.mmu_role.base.smm;
Junaid Shahid9fa72112018-06-27 14:59:07 -07004713
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08004714 role.base.level = level;
Sean Christopherson47c42e62019-03-07 15:27:44 -08004715 role.base.gpte_is_8_bytes = true;
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004716 role.base.direct = false;
4717 role.base.ad_disabled = !accessed_dirty;
4718 role.base.guest_mode = true;
4719 role.base.access = ACC_ALL;
4720
Sean Christopherson552c69b12019-03-07 15:27:43 -08004721 role.ext = kvm_calc_mmu_role_ext(vcpu);
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004722 role.ext.execonly = execonly;
Junaid Shahid9fa72112018-06-27 14:59:07 -07004723
4724 return role;
4725}
4726
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02004727void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly,
Junaid Shahid50c28f22018-06-27 14:59:11 -07004728 bool accessed_dirty, gpa_t new_eptp)
Nadav Har'El155a97a2013-08-05 11:07:16 +03004729{
Paolo Bonzini8c008652020-07-10 16:11:50 +02004730 struct kvm_mmu *context = &vcpu->arch.guest_mmu;
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08004731 u8 level = vmx_eptp_page_walk_level(new_eptp);
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004732 union kvm_mmu_role new_role =
4733 kvm_calc_shadow_ept_root_page_role(vcpu, accessed_dirty,
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08004734 execonly, level);
Paolo Bonziniad896af2013-10-02 16:56:14 +02004735
Sean Christophersonb5129102021-06-09 16:42:27 -07004736 __kvm_mmu_new_pgd(vcpu, new_eptp, new_role.base);
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004737
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004738 if (new_role.as_u64 == context->mmu_role.as_u64)
4739 return;
4740
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08004741 context->shadow_root_level = level;
Nadav Har'El155a97a2013-08-05 11:07:16 +03004742
4743 context->nx = true;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02004744 context->ept_ad = accessed_dirty;
Nadav Har'El155a97a2013-08-05 11:07:16 +03004745 context->page_fault = ept_page_fault;
4746 context->gva_to_gpa = ept_gva_to_gpa;
4747 context->sync_page = ept_sync_page;
4748 context->invlpg = ept_invlpg;
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08004749 context->root_level = level;
Nadav Har'El155a97a2013-08-05 11:07:16 +03004750 context->direct_map = false;
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004751 context->mmu_role.as_u64 = new_role.as_u64;
Vitaly Kuznetsov3dc773e2018-10-08 21:28:06 +02004752
Nadav Har'El155a97a2013-08-05 11:07:16 +03004753 update_permission_bitmask(vcpu, context, true);
Huaitong Han2d344102016-03-22 16:51:19 +08004754 update_pkru_bitmask(vcpu, context, true);
Ladi Prosekfd19d3b42017-10-05 11:10:22 +02004755 update_last_nonleaf_level(vcpu, context);
Nadav Har'El155a97a2013-08-05 11:07:16 +03004756 reset_rsvds_bits_mask_ept(vcpu, context, execonly);
Xiao Guangrongc258b622015-08-05 12:04:24 +08004757 reset_ept_shadow_zero_bits_mask(vcpu, context, execonly);
Nadav Har'El155a97a2013-08-05 11:07:16 +03004758}
4759EXPORT_SYMBOL_GPL(kvm_init_shadow_ept_mmu);
4760
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004761static void init_kvm_softmmu(struct kvm_vcpu *vcpu)
Joerg Roedel52fde8d2010-09-10 17:30:44 +02004762{
Paolo Bonzini8c008652020-07-10 16:11:50 +02004763 struct kvm_mmu *context = &vcpu->arch.root_mmu;
Paolo Bonziniad896af2013-10-02 16:56:14 +02004764
Paolo Bonzini929d1cf2020-05-19 06:18:31 -04004765 kvm_init_shadow_mmu(vcpu,
4766 kvm_read_cr0_bits(vcpu, X86_CR0_PG),
4767 kvm_read_cr4_bits(vcpu, X86_CR4_PAE),
4768 vcpu->arch.efer);
4769
Sean Christophersond8dd54e2020-03-02 18:02:39 -08004770 context->get_guest_pgd = get_cr3;
Paolo Bonziniad896af2013-10-02 16:56:14 +02004771 context->get_pdptr = kvm_pdptr_read;
4772 context->inject_page_fault = kvm_inject_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004773}
4774
Sean Christopherson654430ef2021-06-10 15:00:26 -07004775static union kvm_mmu_role kvm_calc_nested_mmu_role(struct kvm_vcpu *vcpu)
4776{
4777 union kvm_mmu_role role = kvm_calc_shadow_root_page_role_common(vcpu, false);
4778
4779 /*
4780 * Nested MMUs are used only for walking L2's gva->gpa, they never have
4781 * shadow pages of their own and so "direct" has no meaning. Set it
4782 * to "true" to try to detect bogus usage of the nested MMU.
4783 */
4784 role.base.direct = true;
4785
4786 if (!is_paging(vcpu))
4787 role.base.level = 0;
4788 else if (is_long_mode(vcpu))
4789 role.base.level = is_la57_mode(vcpu) ? PT64_ROOT_5LEVEL :
4790 PT64_ROOT_4LEVEL;
4791 else if (is_pae(vcpu))
4792 role.base.level = PT32E_ROOT_LEVEL;
4793 else
4794 role.base.level = PT32_ROOT_LEVEL;
4795
4796 return role;
4797}
4798
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004799static void init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004800{
Sean Christopherson654430ef2021-06-10 15:00:26 -07004801 union kvm_mmu_role new_role = kvm_calc_nested_mmu_role(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004802 struct kvm_mmu *g_context = &vcpu->arch.nested_mmu;
4803
Vitaly Kuznetsovbf627a92018-10-08 21:28:13 +02004804 if (new_role.as_u64 == g_context->mmu_role.as_u64)
4805 return;
4806
4807 g_context->mmu_role.as_u64 = new_role.as_u64;
Sean Christophersond8dd54e2020-03-02 18:02:39 -08004808 g_context->get_guest_pgd = get_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03004809 g_context->get_pdptr = kvm_pdptr_read;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004810 g_context->inject_page_fault = kvm_inject_page_fault;
4811
4812 /*
Paolo Bonzini5efac072020-03-23 20:42:57 -04004813 * L2 page tables are never shadowed, so there is no need to sync
4814 * SPTEs.
4815 */
4816 g_context->invlpg = NULL;
4817
4818 /*
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02004819 * Note that arch.mmu->gva_to_gpa translates l2_gpa to l1_gpa using
David Matlack0af25932015-12-30 08:26:17 -08004820 * L1's nested page tables (e.g. EPT12). The nested translation
4821 * of l2_gva to l1_gpa is done by arch.nested_mmu.gva_to_gpa using
4822 * L2's page tables as the first level of translation and L1's
4823 * nested page tables as the second level of translation. Basically
4824 * the gva_to_gpa functions between mmu and nested_mmu are swapped.
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004825 */
4826 if (!is_paging(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02004827 g_context->nx = false;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004828 g_context->root_level = 0;
4829 g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested;
4830 } else if (is_long_mode(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02004831 g_context->nx = is_nx(vcpu);
Yu Zhang855feb62017-08-24 20:27:55 +08004832 g_context->root_level = is_la57_mode(vcpu) ?
4833 PT64_ROOT_5LEVEL : PT64_ROOT_4LEVEL;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004834 reset_rsvds_bits_mask(vcpu, g_context);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004835 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
4836 } else if (is_pae(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02004837 g_context->nx = is_nx(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004838 g_context->root_level = PT32E_ROOT_LEVEL;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004839 reset_rsvds_bits_mask(vcpu, g_context);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004840 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
4841 } else {
Joerg Roedel2d48a982010-09-10 17:31:01 +02004842 g_context->nx = false;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004843 g_context->root_level = PT32_ROOT_LEVEL;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004844 reset_rsvds_bits_mask(vcpu, g_context);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004845 g_context->gva_to_gpa = paging32_gva_to_gpa_nested;
4846 }
4847
Yang Zhang25d92082013-08-06 12:00:32 +03004848 update_permission_bitmask(vcpu, g_context, false);
Huaitong Han2d344102016-03-22 16:51:19 +08004849 update_pkru_bitmask(vcpu, g_context, false);
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004850 update_last_nonleaf_level(vcpu, g_context);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004851}
4852
Sean Christophersonc9060662021-06-09 16:42:33 -07004853void kvm_init_mmu(struct kvm_vcpu *vcpu)
Joerg Roedelfb72d162008-02-07 13:47:44 +01004854{
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004855 if (mmu_is_nested(vcpu))
Paolo Bonzinie0c6db32014-12-23 13:39:46 +01004856 init_kvm_nested_mmu(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004857 else if (tdp_enabled)
Paolo Bonzinie0c6db32014-12-23 13:39:46 +01004858 init_kvm_tdp_mmu(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01004859 else
Paolo Bonzinie0c6db32014-12-23 13:39:46 +01004860 init_kvm_softmmu(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01004861}
Junaid Shahid1c53da32018-06-27 14:59:10 -07004862EXPORT_SYMBOL_GPL(kvm_init_mmu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01004863
Junaid Shahid9fa72112018-06-27 14:59:07 -07004864static union kvm_mmu_page_role
4865kvm_mmu_calc_root_page_role(struct kvm_vcpu *vcpu)
4866{
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004867 union kvm_mmu_role role;
4868
Junaid Shahid9fa72112018-06-27 14:59:07 -07004869 if (tdp_enabled)
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004870 role = kvm_calc_tdp_mmu_root_page_role(vcpu, true);
Junaid Shahid9fa72112018-06-27 14:59:07 -07004871 else
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004872 role = kvm_calc_shadow_mmu_root_page_role(vcpu, true);
4873
4874 return role.base;
Junaid Shahid9fa72112018-06-27 14:59:07 -07004875}
Dong, Eddie489f1d62008-01-07 11:14:20 +02004876
Sean Christopherson49c6f872021-06-22 10:56:51 -07004877void kvm_mmu_after_set_cpuid(struct kvm_vcpu *vcpu)
4878{
4879 /*
4880 * Invalidate all MMU roles to force them to reinitialize as CPUID
4881 * information is factored into reserved bit calculations.
4882 */
4883 vcpu->arch.root_mmu.mmu_role.ext.valid = 0;
4884 vcpu->arch.guest_mmu.mmu_role.ext.valid = 0;
4885 vcpu->arch.nested_mmu.mmu_role.ext.valid = 0;
4886 kvm_mmu_reset_context(vcpu);
Sean Christopherson63f5a192021-06-22 10:56:52 -07004887
4888 /*
4889 * KVM does not correctly handle changing guest CPUID after KVM_RUN, as
4890 * MAXPHYADDR, GBPAGES support, AMD reserved bit behavior, etc.. aren't
4891 * tracked in kvm_mmu_page_role. As a result, KVM may miss guest page
4892 * faults due to reusing SPs/SPTEs. Alert userspace, but otherwise
4893 * sweep the problem under the rug.
4894 *
4895 * KVM's horrific CPUID ABI makes the problem all but impossible to
4896 * solve, as correctly handling multiple vCPU models (with respect to
4897 * paging and physical address properties) in a single VM would require
4898 * tracking all relevant CPUID information in kvm_mmu_page_role. That
4899 * is very undesirable as it would double the memory requirements for
4900 * gfn_track (see struct kvm_mmu_page_role comments), and in practice
4901 * no sane VMM mucks with the core vCPU model on the fly.
4902 */
4903 if (vcpu->arch.last_vmentry_cpu != -1) {
4904 pr_warn_ratelimited("KVM: KVM_SET_CPUID{,2} after KVM_RUN may cause guest instability\n");
4905 pr_warn_ratelimited("KVM: KVM_SET_CPUID{,2} will fail after KVM_RUN starting with Linux 5.16\n");
4906 }
Sean Christopherson49c6f872021-06-22 10:56:51 -07004907}
4908
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004909void kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
Avi Kivity17c3ba92007-06-04 15:58:30 +03004910{
Paolo Bonzini95f93af2013-10-02 16:56:12 +02004911 kvm_mmu_unload(vcpu);
Sean Christophersonc9060662021-06-09 16:42:33 -07004912 kvm_init_mmu(vcpu);
Eddie Dong8668a3c2007-10-10 14:26:45 +08004913}
Avi Kivity17c3ba92007-06-04 15:58:30 +03004914EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
4915
4916int kvm_mmu_load(struct kvm_vcpu *vcpu)
Avi Kivity714b93d2007-01-05 16:36:53 -08004917{
4918 int r;
Avi Kivitye2dec932007-01-05 16:36:54 -08004919
Sean Christopherson378f5cd2020-07-02 19:35:36 -07004920 r = mmu_topup_memory_caches(vcpu, !vcpu->arch.mmu->direct_map);
Avi Kivity17c3ba92007-06-04 15:58:30 +03004921 if (r)
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05004922 goto out;
Sean Christopherson748e52b2021-03-04 17:10:49 -08004923 r = mmu_alloc_special_roots(vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03004924 if (r)
4925 goto out;
Paolo Bonzini4a381622021-04-08 08:10:25 -04004926 if (vcpu->arch.mmu->direct_map)
Sean Christopherson6e6ec582021-03-04 17:10:50 -08004927 r = mmu_alloc_direct_roots(vcpu);
4928 else
4929 r = mmu_alloc_shadow_roots(vcpu);
Avi Kivityac1b7142007-03-08 17:13:32 +02004930 if (r)
4931 goto out;
Sean Christophersona91f3872021-03-04 17:11:00 -08004932
4933 kvm_mmu_sync_roots(vcpu);
4934
Paolo Bonzini727a7e22020-03-05 03:52:50 -05004935 kvm_mmu_load_pgd(vcpu);
Jason Baronb36464772021-01-14 22:27:56 -05004936 static_call(kvm_x86_tlb_flush_current)(vcpu);
Avi Kivity9b7a0322007-01-05 16:36:45 -08004937out:
4938 return r;
4939}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004940
4941void kvm_mmu_unload(struct kvm_vcpu *vcpu)
4942{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02004943 kvm_mmu_free_roots(vcpu, &vcpu->arch.root_mmu, KVM_MMU_ROOTS_ALL);
4944 WARN_ON(VALID_PAGE(vcpu->arch.root_mmu.root_hpa));
4945 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
4946 WARN_ON(VALID_PAGE(vcpu->arch.guest_mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004947}
Avi Kivityac1b7142007-03-08 17:13:32 +02004948
Avi Kivity79539ce2007-11-21 02:06:21 +02004949static bool need_remote_flush(u64 old, u64 new)
4950{
4951 if (!is_shadow_present_pte(old))
4952 return false;
4953 if (!is_shadow_present_pte(new))
4954 return true;
4955 if ((old ^ new) & PT64_BASE_ADDR_MASK)
4956 return true;
Gleb Natapov53166222013-08-05 11:07:14 +03004957 old ^= shadow_nx_mask;
4958 new ^= shadow_nx_mask;
Avi Kivity79539ce2007-11-21 02:06:21 +02004959 return (old & ~new & PT64_PERM_MASK) != 0;
4960}
4961
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004962static u64 mmu_pte_write_fetch_gpte(struct kvm_vcpu *vcpu, gpa_t *gpa,
Junaid Shahid0e0fee52018-10-31 14:53:57 -07004963 int *bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08004964{
Junaid Shahid0e0fee52018-10-31 14:53:57 -07004965 u64 gentry = 0;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004966 int r;
Avi Kivity72016f32010-03-15 13:59:53 +02004967
Avi Kivity08e850c2010-03-15 13:59:57 +02004968 /*
4969 * Assume that the pte write on a page table of the same type
Xiao Guangrong49b26e22011-03-04 19:00:00 +08004970 * as the current vcpu paging mode since we update the sptes only
4971 * when they have the same mode.
Avi Kivity08e850c2010-03-15 13:59:57 +02004972 */
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004973 if (is_pae(vcpu) && *bytes == 4) {
Avi Kivity08e850c2010-03-15 13:59:57 +02004974 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004975 *gpa &= ~(gpa_t)7;
4976 *bytes = 8;
Avi Kivity08e850c2010-03-15 13:59:57 +02004977 }
4978
Junaid Shahid0e0fee52018-10-31 14:53:57 -07004979 if (*bytes == 4 || *bytes == 8) {
4980 r = kvm_vcpu_read_guest_atomic(vcpu, *gpa, &gentry, *bytes);
4981 if (r)
4982 gentry = 0;
Avi Kivity72016f32010-03-15 13:59:53 +02004983 }
4984
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004985 return gentry;
4986}
4987
4988/*
4989 * If we're seeing too many writes to a page, it may no longer be a page table,
4990 * or we may be forking, in which case it is better to unmap the page.
4991 */
Xiao Guangronga138fe72011-12-16 18:18:10 +08004992static bool detect_write_flooding(struct kvm_mmu_page *sp)
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004993{
Xiao Guangronga30f47c2011-09-22 16:58:36 +08004994 /*
4995 * Skip write-flooding detected for the sp whose level is 1, because
4996 * it can become unsync, then the guest page is not write-protected.
4997 */
Sean Christopherson3bae0452020-04-27 17:54:22 -07004998 if (sp->role.level == PG_LEVEL_4K)
Xiao Guangronga30f47c2011-09-22 16:58:36 +08004999 return false;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005000
Xiao Guangronge5691a82016-02-24 17:51:12 +08005001 atomic_inc(&sp->write_flooding_count);
5002 return atomic_read(&sp->write_flooding_count) >= 3;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005003}
5004
5005/*
5006 * Misaligned accesses are too much trouble to fix up; also, they usually
5007 * indicate a page is not used as a page table.
5008 */
5009static bool detect_write_misaligned(struct kvm_mmu_page *sp, gpa_t gpa,
5010 int bytes)
5011{
5012 unsigned offset, pte_size, misaligned;
5013
5014 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
5015 gpa, bytes, sp->role.word);
5016
5017 offset = offset_in_page(gpa);
Sean Christopherson47c42e62019-03-07 15:27:44 -08005018 pte_size = sp->role.gpte_is_8_bytes ? 8 : 4;
Xiao Guangrong5d9ca302011-09-22 16:57:55 +08005019
5020 /*
5021 * Sometimes, the OS only writes the last one bytes to update status
5022 * bits, for example, in linux, andb instruction is used in clear_bit().
5023 */
5024 if (!(offset & (pte_size - 1)) && bytes == 1)
5025 return false;
5026
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005027 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
5028 misaligned |= bytes < 4;
5029
5030 return misaligned;
5031}
5032
5033static u64 *get_written_sptes(struct kvm_mmu_page *sp, gpa_t gpa, int *nspte)
5034{
5035 unsigned page_offset, quadrant;
5036 u64 *spte;
5037 int level;
5038
5039 page_offset = offset_in_page(gpa);
5040 level = sp->role.level;
5041 *nspte = 1;
Sean Christopherson47c42e62019-03-07 15:27:44 -08005042 if (!sp->role.gpte_is_8_bytes) {
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005043 page_offset <<= 1; /* 32->64 */
5044 /*
5045 * A 32-bit pde maps 4MB while the shadow pdes map
5046 * only 2MB. So we need to double the offset again
5047 * and zap two pdes instead of one.
5048 */
5049 if (level == PT32_ROOT_LEVEL) {
5050 page_offset &= ~7; /* kill rounding error */
5051 page_offset <<= 1;
5052 *nspte = 2;
5053 }
5054 quadrant = page_offset >> PAGE_SHIFT;
5055 page_offset &= ~PAGE_MASK;
5056 if (quadrant != sp->role.quadrant)
5057 return NULL;
5058 }
5059
5060 spte = &sp->spt[page_offset / sizeof(*spte)];
5061 return spte;
5062}
5063
Xiao Guangrong13d268c2016-02-24 17:51:16 +08005064static void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Jike Songd1263632016-10-25 15:50:42 +08005065 const u8 *new, int bytes,
5066 struct kvm_page_track_notifier_node *node)
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005067{
5068 gfn_t gfn = gpa >> PAGE_SHIFT;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005069 struct kvm_mmu_page *sp;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005070 LIST_HEAD(invalid_list);
5071 u64 entry, gentry, *spte;
5072 int npte;
Paolo Bonzinib8c67b72016-02-24 11:21:55 +01005073 bool remote_flush, local_flush;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005074
5075 /*
5076 * If we don't have indirect shadow pages, it means no page is
5077 * write-protected, so we can exit simply.
5078 */
Mark Rutland6aa7de02017-10-23 14:07:29 -07005079 if (!READ_ONCE(vcpu->kvm->arch.indirect_shadow_pages))
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005080 return;
5081
Paolo Bonzinib8c67b72016-02-24 11:21:55 +01005082 remote_flush = local_flush = false;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005083
5084 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
5085
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005086 /*
5087 * No need to care whether allocation memory is successful
Ingo Molnard9f6e122021-03-18 15:28:01 +01005088 * or not since pte prefetch is skipped if it does not have
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005089 * enough objects in the cache.
5090 */
Sean Christopherson378f5cd2020-07-02 19:35:36 -07005091 mmu_topup_memory_caches(vcpu, true);
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005092
Ben Gardon531810c2021-02-02 10:57:24 -08005093 write_lock(&vcpu->kvm->mmu_lock);
Junaid Shahid0e0fee52018-10-31 14:53:57 -07005094
5095 gentry = mmu_pte_write_fetch_gpte(vcpu, &gpa, &bytes);
5096
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005097 ++vcpu->kvm->stat.mmu_pte_write;
Xiao Guangrong0375f7f2011-11-28 20:41:00 +08005098 kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE);
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005099
Sasha Levinb67bfe02013-02-27 17:06:00 -08005100 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn) {
Xiao Guangronga30f47c2011-09-22 16:58:36 +08005101 if (detect_write_misaligned(sp, gpa, bytes) ||
Xiao Guangronga138fe72011-12-16 18:18:10 +08005102 detect_write_flooding(sp)) {
Paolo Bonzinib8c67b72016-02-24 11:21:55 +01005103 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02005104 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08005105 continue;
5106 }
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005107
5108 spte = get_written_sptes(sp, gpa, &npte);
5109 if (!spte)
5110 continue;
5111
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08005112 local_flush = true;
Avi Kivityac1b7142007-03-08 17:13:32 +02005113 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02005114 entry = *spte;
Ben Gardon2de40852020-09-23 15:14:06 -07005115 mmu_page_zap_pte(vcpu->kvm, sp, spte, NULL);
Sean Christophersonc5e21842021-01-14 16:40:51 -08005116 if (gentry && sp->role.level != PG_LEVEL_4K)
5117 ++vcpu->kvm->stat.mmu_pde_zapped;
Gleb Natapov9bb4f6b2013-01-30 16:45:01 +02005118 if (need_remote_flush(entry, *spte))
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08005119 remote_flush = true;
Avi Kivityac1b7142007-03-08 17:13:32 +02005120 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08005121 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08005122 }
Paolo Bonzinib8c67b72016-02-24 11:21:55 +01005123 kvm_mmu_flush_or_zap(vcpu, &invalid_list, remote_flush, local_flush);
Xiao Guangrong0375f7f2011-11-28 20:41:00 +08005124 kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE);
Ben Gardon531810c2021-02-02 10:57:24 -08005125 write_unlock(&vcpu->kvm->mmu_lock);
Avi Kivityda4a00f2007-01-05 16:36:44 -08005126}
5127
Sean Christopherson736c2912019-12-06 15:57:14 -08005128int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code,
Andre Przywaradc25e892010-12-21 11:12:07 +01005129 void *insn, int insn_len)
Avi Kivity30677142007-10-28 18:48:59 +02005130{
Sean Christopherson92daa482020-02-18 15:03:08 -08005131 int r, emulation_type = EMULTYPE_PF;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005132 bool direct = vcpu->arch.mmu->direct_map;
Avi Kivity30677142007-10-28 18:48:59 +02005133
Sean Christopherson69481992019-12-06 15:57:29 -08005134 if (WARN_ON(!VALID_PAGE(vcpu->arch.mmu->root_hpa)))
Sean Christophersonddce6202019-12-06 15:57:27 -08005135 return RET_PF_RETRY;
5136
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02005137 r = RET_PF_INVALID;
Takuya Yoshikawae9ee9562016-02-22 17:23:41 +09005138 if (unlikely(error_code & PFERR_RSVD_MASK)) {
Sean Christopherson736c2912019-12-06 15:57:14 -08005139 r = handle_mmio_page_fault(vcpu, cr2_or_gpa, direct);
Sean Christopherson472faff2018-08-23 13:56:50 -07005140 if (r == RET_PF_EMULATE)
Takuya Yoshikawae9ee9562016-02-22 17:23:41 +09005141 goto emulate;
Takuya Yoshikawae9ee9562016-02-22 17:23:41 +09005142 }
Avi Kivity30677142007-10-28 18:48:59 +02005143
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02005144 if (r == RET_PF_INVALID) {
Sean Christopherson7a026742020-02-06 14:14:34 -08005145 r = kvm_mmu_do_page_fault(vcpu, cr2_or_gpa,
5146 lower_32_bits(error_code), false);
Sean Christopherson7b367bc2020-09-23 15:04:22 -07005147 if (WARN_ON_ONCE(r == RET_PF_INVALID))
5148 return -EIO;
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02005149 }
5150
Avi Kivity30677142007-10-28 18:48:59 +02005151 if (r < 0)
Takuya Yoshikawae9ee9562016-02-22 17:23:41 +09005152 return r;
Sean Christopherson83a2ba42020-09-23 15:04:23 -07005153 if (r != RET_PF_EMULATE)
5154 return 1;
Avi Kivity30677142007-10-28 18:48:59 +02005155
Tom Lendacky14727752016-11-23 12:01:38 -05005156 /*
5157 * Before emulating the instruction, check if the error code
5158 * was due to a RO violation while translating the guest page.
5159 * This can occur when using nested virtualization with nested
5160 * paging in both guests. If true, we simply unprotect the page
5161 * and resume the guest.
Tom Lendacky14727752016-11-23 12:01:38 -05005162 */
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005163 if (vcpu->arch.mmu->direct_map &&
Paolo Bonzinieebed242016-11-28 14:39:58 +01005164 (error_code & PFERR_NESTED_GUEST_PAGE) == PFERR_NESTED_GUEST_PAGE) {
Sean Christopherson736c2912019-12-06 15:57:14 -08005165 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(cr2_or_gpa));
Tom Lendacky14727752016-11-23 12:01:38 -05005166 return 1;
5167 }
5168
Sean Christopherson472faff2018-08-23 13:56:50 -07005169 /*
5170 * vcpu->arch.mmu.page_fault returned RET_PF_EMULATE, but we can still
5171 * optimistically try to just unprotect the page and let the processor
5172 * re-execute the instruction that caused the page fault. Do not allow
5173 * retrying MMIO emulation, as it's not only pointless but could also
5174 * cause us to enter an infinite loop because the processor will keep
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07005175 * faulting on the non-existent MMIO address. Retrying an instruction
5176 * from a nested guest is also pointless and dangerous as we are only
5177 * explicitly shadowing L1's page tables, i.e. unprotecting something
5178 * for L1 isn't going to magically fix whatever issue cause L2 to fail.
Sean Christopherson472faff2018-08-23 13:56:50 -07005179 */
Sean Christopherson736c2912019-12-06 15:57:14 -08005180 if (!mmio_info_in_cache(vcpu, cr2_or_gpa, direct) && !is_guest_mode(vcpu))
Sean Christopherson92daa482020-02-18 15:03:08 -08005181 emulation_type |= EMULTYPE_ALLOW_RETRY_PF;
Takuya Yoshikawae9ee9562016-02-22 17:23:41 +09005182emulate:
Sean Christopherson736c2912019-12-06 15:57:14 -08005183 return x86_emulate_instruction(vcpu, cr2_or_gpa, emulation_type, insn,
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005184 insn_len);
Avi Kivity30677142007-10-28 18:48:59 +02005185}
5186EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
5187
Paolo Bonzini5efac072020-03-23 20:42:57 -04005188void kvm_mmu_invalidate_gva(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
5189 gva_t gva, hpa_t root_hpa)
Marcelo Tosattia7052892008-09-23 13:18:35 -03005190{
Junaid Shahidb94742c2018-06-27 14:59:20 -07005191 int i;
Junaid Shahid7eb77e92018-06-27 14:59:16 -07005192
Paolo Bonzini5efac072020-03-23 20:42:57 -04005193 /* It's actually a GPA for vcpu->arch.guest_mmu. */
5194 if (mmu != &vcpu->arch.guest_mmu) {
5195 /* INVLPG on a non-canonical address is a NOP according to the SDM. */
5196 if (is_noncanonical_address(gva, vcpu))
5197 return;
5198
Jason Baronb36464772021-01-14 22:27:56 -05005199 static_call(kvm_x86_tlb_flush_gva)(vcpu, gva);
Paolo Bonzini5efac072020-03-23 20:42:57 -04005200 }
5201
5202 if (!mmu->invlpg)
Junaid Shahidfaff8752018-06-29 13:10:05 -07005203 return;
5204
Paolo Bonzini5efac072020-03-23 20:42:57 -04005205 if (root_hpa == INVALID_PAGE) {
5206 mmu->invlpg(vcpu, gva, mmu->root_hpa);
Junaid Shahid956bf352018-06-27 14:59:18 -07005207
Paolo Bonzini5efac072020-03-23 20:42:57 -04005208 /*
5209 * INVLPG is required to invalidate any global mappings for the VA,
5210 * irrespective of PCID. Since it would take us roughly similar amount
5211 * of work to determine whether any of the prev_root mappings of the VA
5212 * is marked global, or to just sync it blindly, so we might as well
5213 * just always sync it.
5214 *
5215 * Mappings not reachable via the current cr3 or the prev_roots will be
5216 * synced when switching to that cr3, so nothing needs to be done here
5217 * for them.
5218 */
5219 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
5220 if (VALID_PAGE(mmu->prev_roots[i].hpa))
5221 mmu->invlpg(vcpu, gva, mmu->prev_roots[i].hpa);
5222 } else {
5223 mmu->invlpg(vcpu, gva, root_hpa);
5224 }
5225}
Junaid Shahid956bf352018-06-27 14:59:18 -07005226
Paolo Bonzini5efac072020-03-23 20:42:57 -04005227void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
5228{
5229 kvm_mmu_invalidate_gva(vcpu, vcpu->arch.mmu, gva, INVALID_PAGE);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005230 ++vcpu->stat.invlpg;
5231}
5232EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
5233
Paolo Bonzini5efac072020-03-23 20:42:57 -04005234
Junaid Shahideb4b2482018-06-27 14:59:14 -07005235void kvm_mmu_invpcid_gva(struct kvm_vcpu *vcpu, gva_t gva, unsigned long pcid)
5236{
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005237 struct kvm_mmu *mmu = vcpu->arch.mmu;
Junaid Shahidfaff8752018-06-29 13:10:05 -07005238 bool tlb_flush = false;
Junaid Shahidb94742c2018-06-27 14:59:20 -07005239 uint i;
Junaid Shahideb4b2482018-06-27 14:59:14 -07005240
5241 if (pcid == kvm_get_active_pcid(vcpu)) {
Junaid Shahid7eb77e92018-06-27 14:59:16 -07005242 mmu->invlpg(vcpu, gva, mmu->root_hpa);
Junaid Shahidfaff8752018-06-29 13:10:05 -07005243 tlb_flush = true;
Junaid Shahideb4b2482018-06-27 14:59:14 -07005244 }
5245
Junaid Shahidb94742c2018-06-27 14:59:20 -07005246 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
5247 if (VALID_PAGE(mmu->prev_roots[i].hpa) &&
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07005248 pcid == kvm_get_pcid(vcpu, mmu->prev_roots[i].pgd)) {
Junaid Shahidb94742c2018-06-27 14:59:20 -07005249 mmu->invlpg(vcpu, gva, mmu->prev_roots[i].hpa);
5250 tlb_flush = true;
5251 }
Junaid Shahid956bf352018-06-27 14:59:18 -07005252 }
Junaid Shahidade61e22018-06-27 14:59:15 -07005253
Junaid Shahidfaff8752018-06-29 13:10:05 -07005254 if (tlb_flush)
Jason Baronb36464772021-01-14 22:27:56 -05005255 static_call(kvm_x86_tlb_flush_gva)(vcpu, gva);
Junaid Shahidfaff8752018-06-29 13:10:05 -07005256
Junaid Shahideb4b2482018-06-27 14:59:14 -07005257 ++vcpu->stat.invlpg;
5258
5259 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07005260 * Mappings not reachable via the current cr3 or the prev_roots will be
5261 * synced when switching to that cr3, so nothing needs to be done here
5262 * for them.
Junaid Shahideb4b2482018-06-27 14:59:14 -07005263 */
5264}
Junaid Shahideb4b2482018-06-27 14:59:14 -07005265
Sean Christopherson83013052020-07-15 20:41:22 -07005266void kvm_configure_mmu(bool enable_tdp, int tdp_max_root_level,
5267 int tdp_huge_page_level)
Joerg Roedel18552672008-02-07 13:47:41 +01005268{
Sean Christophersonbde77232020-03-02 15:57:02 -08005269 tdp_enabled = enable_tdp;
Sean Christopherson83013052020-07-15 20:41:22 -07005270 max_tdp_level = tdp_max_root_level;
Sean Christopherson703c3352020-03-02 15:57:03 -08005271
5272 /*
Sean Christopherson1d92d2e2020-07-15 20:41:21 -07005273 * max_huge_page_level reflects KVM's MMU capabilities irrespective
Sean Christopherson703c3352020-03-02 15:57:03 -08005274 * of kernel support, e.g. KVM may be capable of using 1GB pages when
5275 * the kernel is not. But, KVM never creates a page size greater than
5276 * what is used by the kernel for any given HVA, i.e. the kernel's
5277 * capabilities are ultimately consulted by kvm_mmu_hugepage_adjust().
5278 */
5279 if (tdp_enabled)
Sean Christopherson1d92d2e2020-07-15 20:41:21 -07005280 max_huge_page_level = tdp_huge_page_level;
Sean Christopherson703c3352020-03-02 15:57:03 -08005281 else if (boot_cpu_has(X86_FEATURE_GBPAGES))
Sean Christopherson1d92d2e2020-07-15 20:41:21 -07005282 max_huge_page_level = PG_LEVEL_1G;
Sean Christopherson703c3352020-03-02 15:57:03 -08005283 else
Sean Christopherson1d92d2e2020-07-15 20:41:21 -07005284 max_huge_page_level = PG_LEVEL_2M;
Joerg Roedel18552672008-02-07 13:47:41 +01005285}
Sean Christophersonbde77232020-03-02 15:57:02 -08005286EXPORT_SYMBOL_GPL(kvm_configure_mmu);
Xiao Guangrong13d268c2016-02-24 17:51:16 +08005287
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005288/* The return value indicates if tlb flush on all vcpus is needed. */
Sean Christopherson0a234f52021-02-12 16:50:05 -08005289typedef bool (*slot_level_handler) (struct kvm *kvm, struct kvm_rmap_head *rmap_head,
5290 struct kvm_memory_slot *slot);
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005291
5292/* The caller should hold mmu-lock before calling this function. */
David Woodhouse928a4c32018-02-10 23:39:24 +00005293static __always_inline bool
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005294slot_handle_level_range(struct kvm *kvm, struct kvm_memory_slot *memslot,
5295 slot_level_handler fn, int start_level, int end_level,
Sean Christopherson1a61b7d2021-03-25 19:19:43 -07005296 gfn_t start_gfn, gfn_t end_gfn, bool flush_on_yield,
5297 bool flush)
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005298{
5299 struct slot_rmap_walk_iterator iterator;
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005300
5301 for_each_slot_rmap_range(memslot, start_level, end_level, start_gfn,
5302 end_gfn, &iterator) {
5303 if (iterator.rmap)
Sean Christopherson0a234f52021-02-12 16:50:05 -08005304 flush |= fn(kvm, iterator.rmap, memslot);
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005305
Ben Gardon531810c2021-02-02 10:57:24 -08005306 if (need_resched() || rwlock_needbreak(&kvm->mmu_lock)) {
Sean Christopherson302695a2021-03-25 19:19:41 -07005307 if (flush && flush_on_yield) {
Ben Gardonf285c632019-03-12 11:45:59 -07005308 kvm_flush_remote_tlbs_with_address(kvm,
5309 start_gfn,
5310 iterator.gfn - start_gfn + 1);
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005311 flush = false;
5312 }
Ben Gardon531810c2021-02-02 10:57:24 -08005313 cond_resched_rwlock_write(&kvm->mmu_lock);
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005314 }
5315 }
5316
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005317 return flush;
5318}
5319
David Woodhouse928a4c32018-02-10 23:39:24 +00005320static __always_inline bool
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005321slot_handle_level(struct kvm *kvm, struct kvm_memory_slot *memslot,
5322 slot_level_handler fn, int start_level, int end_level,
Sean Christopherson302695a2021-03-25 19:19:41 -07005323 bool flush_on_yield)
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005324{
5325 return slot_handle_level_range(kvm, memslot, fn, start_level,
5326 end_level, memslot->base_gfn,
5327 memslot->base_gfn + memslot->npages - 1,
Sean Christopherson1a61b7d2021-03-25 19:19:43 -07005328 flush_on_yield, false);
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005329}
5330
David Woodhouse928a4c32018-02-10 23:39:24 +00005331static __always_inline bool
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005332slot_handle_leaf(struct kvm *kvm, struct kvm_memory_slot *memslot,
Sean Christopherson302695a2021-03-25 19:19:41 -07005333 slot_level_handler fn, bool flush_on_yield)
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005334{
Sean Christopherson3bae0452020-04-27 17:54:22 -07005335 return slot_handle_level(kvm, memslot, fn, PG_LEVEL_4K,
Sean Christopherson302695a2021-03-25 19:19:41 -07005336 PG_LEVEL_4K, flush_on_yield);
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005337}
5338
Jiří Paleček1cfff4d2019-06-22 19:42:04 +02005339static void free_mmu_pages(struct kvm_mmu *mmu)
Takuya Yoshikawab99db1d2013-01-08 19:44:48 +09005340{
Sean Christopherson4a986232021-03-09 14:42:07 -08005341 if (!tdp_enabled && mmu->pae_root)
5342 set_memory_encrypted((unsigned long)mmu->pae_root, 1);
Jiří Paleček1cfff4d2019-06-22 19:42:04 +02005343 free_page((unsigned long)mmu->pae_root);
Sean Christopherson03ca4582021-05-05 13:42:21 -07005344 free_page((unsigned long)mmu->pml4_root);
Takuya Yoshikawa6b81b052013-01-08 19:47:33 +09005345}
Xiao Guangrong198c74f2014-04-17 17:06:16 +08005346
Sean Christopherson04d28e32020-09-23 09:33:14 -07005347static int __kvm_mmu_create(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu)
Avi Kivity8234b222010-12-27 12:08:45 +02005348{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005349 struct page *page;
Takuya Yoshikawab99db1d2013-01-08 19:44:48 +09005350 int i;
Takuya Yoshikawa9d1beef2013-01-08 19:46:48 +09005351
Sean Christopherson04d28e32020-09-23 09:33:14 -07005352 mmu->root_hpa = INVALID_PAGE;
5353 mmu->root_pgd = 0;
5354 mmu->translate_gpa = translate_gpa;
5355 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
5356 mmu->prev_roots[i] = KVM_MMU_ROOT_INFO_INVALID;
5357
Sean Christophersonb6b80c72019-06-13 10:22:23 -07005358 /*
5359 * When using PAE paging, the four PDPTEs are treated as 'root' pages,
5360 * while the PDP table is a per-vCPU construct that's allocated at MMU
5361 * creation. When emulating 32-bit mode, cr3 is only 32 bits even on
5362 * x86_64. Therefore we need to allocate the PDP table in the first
Sean Christopherson04d45552021-03-04 17:10:46 -08005363 * 4GB of memory, which happens to fit the DMA32 zone. TDP paging
5364 * generally doesn't use PAE paging and can skip allocating the PDP
5365 * table. The main exception, handled here, is SVM's 32-bit NPT. The
5366 * other exception is for shadowing L1's 32-bit or PAE NPT on 64-bit
5367 * KVM; that horror is handled on-demand by mmu_alloc_shadow_roots().
Sean Christophersonb6b80c72019-06-13 10:22:23 -07005368 */
Sean Christophersond468d942020-07-15 20:41:20 -07005369 if (tdp_enabled && kvm_mmu_get_tdp_level(vcpu) > PT32E_ROOT_LEVEL)
Xiao Guangrong198c74f2014-04-17 17:06:16 +08005370 return 0;
5371
Ben Gardon254272c2019-02-11 11:02:50 -08005372 page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_DMA32);
Xiao Guangrong198c74f2014-04-17 17:06:16 +08005373 if (!page)
5374 return -ENOMEM;
5375
Jiří Paleček1cfff4d2019-06-22 19:42:04 +02005376 mmu->pae_root = page_address(page);
Sean Christopherson4a986232021-03-09 14:42:07 -08005377
5378 /*
5379 * CR3 is only 32 bits when PAE paging is used, thus it's impossible to
5380 * get the CPU to treat the PDPTEs as encrypted. Decrypt the page so
5381 * that KVM's writes and the CPU's reads get along. Note, this is
5382 * only necessary when using shadow paging, as 64-bit NPT can get at
5383 * the C-bit even when shadowing 32-bit NPT, and SME isn't supported
5384 * by 32-bit kernels (when KVM itself uses 32-bit NPT).
5385 */
5386 if (!tdp_enabled)
5387 set_memory_decrypted((unsigned long)mmu->pae_root, 1);
5388 else
5389 WARN_ON_ONCE(shadow_me_mask);
5390
Xiao Guangrong198c74f2014-04-17 17:06:16 +08005391 for (i = 0; i < 4; ++i)
Sean Christophersonc834e5e42021-03-09 14:42:06 -08005392 mmu->pae_root[i] = INVALID_PAE_ROOT;
Xiao Guangrong198c74f2014-04-17 17:06:16 +08005393
5394 return 0;
5395}
5396
Kai Huangd91ffee2015-01-12 15:28:54 +08005397int kvm_mmu_create(struct kvm_vcpu *vcpu)
5398{
Jiří Paleček1cfff4d2019-06-22 19:42:04 +02005399 int ret;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08005400
Sean Christopherson5962bfb2020-07-02 19:35:25 -07005401 vcpu->arch.mmu_pte_list_desc_cache.kmem_cache = pte_list_desc_cache;
Sean Christopherson5f6078f2020-07-02 19:35:34 -07005402 vcpu->arch.mmu_pte_list_desc_cache.gfp_zero = __GFP_ZERO;
5403
Sean Christopherson5962bfb2020-07-02 19:35:25 -07005404 vcpu->arch.mmu_page_header_cache.kmem_cache = mmu_page_header_cache;
Sean Christopherson5f6078f2020-07-02 19:35:34 -07005405 vcpu->arch.mmu_page_header_cache.gfp_zero = __GFP_ZERO;
Sean Christopherson5962bfb2020-07-02 19:35:25 -07005406
Sean Christopherson96880882020-07-02 19:35:35 -07005407 vcpu->arch.mmu_shadow_page_cache.gfp_zero = __GFP_ZERO;
5408
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005409 vcpu->arch.mmu = &vcpu->arch.root_mmu;
5410 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
5411
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005412 vcpu->arch.nested_mmu.translate_gpa = translate_nested_gpa;
Jiří Paleček1cfff4d2019-06-22 19:42:04 +02005413
Sean Christopherson04d28e32020-09-23 09:33:14 -07005414 ret = __kvm_mmu_create(vcpu, &vcpu->arch.guest_mmu);
Jiří Paleček1cfff4d2019-06-22 19:42:04 +02005415 if (ret)
5416 return ret;
5417
Sean Christopherson04d28e32020-09-23 09:33:14 -07005418 ret = __kvm_mmu_create(vcpu, &vcpu->arch.root_mmu);
Jiří Paleček1cfff4d2019-06-22 19:42:04 +02005419 if (ret)
5420 goto fail_allocate_root;
5421
5422 return ret;
5423 fail_allocate_root:
5424 free_mmu_pages(&vcpu->arch.guest_mmu);
5425 return ret;
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005426}
5427
Sean Christophersonfbb158c2019-09-12 19:46:07 -07005428#define BATCH_ZAP_PAGES 10
Sean Christopherson002c5f72019-09-12 19:46:02 -07005429static void kvm_zap_obsolete_pages(struct kvm *kvm)
5430{
5431 struct kvm_mmu_page *sp, *node;
Sean Christophersonfbb158c2019-09-12 19:46:07 -07005432 int nr_zapped, batch = 0;
Sean Christopherson002c5f72019-09-12 19:46:02 -07005433
5434restart:
5435 list_for_each_entry_safe_reverse(sp, node,
5436 &kvm->arch.active_mmu_pages, link) {
5437 /*
5438 * No obsolete valid page exists before a newly created page
5439 * since active_mmu_pages is a FIFO list.
5440 */
5441 if (!is_obsolete_sp(kvm, sp))
5442 break;
5443
5444 /*
Sean Christophersonf95eec92020-06-23 12:35:39 -07005445 * Invalid pages should never land back on the list of active
5446 * pages. Skip the bogus page, otherwise we'll get stuck in an
5447 * infinite loop if the page gets put back on the list (again).
Sean Christopherson002c5f72019-09-12 19:46:02 -07005448 */
Sean Christophersonf95eec92020-06-23 12:35:39 -07005449 if (WARN_ON(sp->role.invalid))
Sean Christopherson002c5f72019-09-12 19:46:02 -07005450 continue;
5451
Sean Christopherson4506ecf2019-09-12 19:46:08 -07005452 /*
5453 * No need to flush the TLB since we're only zapping shadow
5454 * pages with an obsolete generation number and all vCPUS have
5455 * loaded a new root, i.e. the shadow pages being zapped cannot
5456 * be in active use by the guest.
5457 */
Sean Christophersonfbb158c2019-09-12 19:46:07 -07005458 if (batch >= BATCH_ZAP_PAGES &&
Ben Gardon531810c2021-02-02 10:57:24 -08005459 cond_resched_rwlock_write(&kvm->mmu_lock)) {
Sean Christophersonfbb158c2019-09-12 19:46:07 -07005460 batch = 0;
Sean Christopherson002c5f72019-09-12 19:46:02 -07005461 goto restart;
5462 }
5463
Sean Christopherson10605202019-09-12 19:46:10 -07005464 if (__kvm_mmu_prepare_zap_page(kvm, sp,
5465 &kvm->arch.zapped_obsolete_pages, &nr_zapped)) {
Sean Christophersonfbb158c2019-09-12 19:46:07 -07005466 batch += nr_zapped;
Sean Christopherson002c5f72019-09-12 19:46:02 -07005467 goto restart;
Sean Christophersonfbb158c2019-09-12 19:46:07 -07005468 }
Sean Christopherson002c5f72019-09-12 19:46:02 -07005469 }
5470
Sean Christopherson4506ecf2019-09-12 19:46:08 -07005471 /*
5472 * Trigger a remote TLB flush before freeing the page tables to ensure
5473 * KVM is not in the middle of a lockless shadow page table walk, which
5474 * may reference the pages.
5475 */
Sean Christopherson10605202019-09-12 19:46:10 -07005476 kvm_mmu_commit_zap_page(kvm, &kvm->arch.zapped_obsolete_pages);
Sean Christopherson002c5f72019-09-12 19:46:02 -07005477}
5478
5479/*
5480 * Fast invalidate all shadow pages and use lock-break technique
5481 * to zap obsolete pages.
5482 *
5483 * It's required when memslot is being deleted or VM is being
5484 * destroyed, in these cases, we should ensure that KVM MMU does
5485 * not use any resource of the being-deleted slot or all slots
5486 * after calling the function.
5487 */
5488static void kvm_mmu_zap_all_fast(struct kvm *kvm)
5489{
Sean Christophersonca333ad2019-09-12 19:46:11 -07005490 lockdep_assert_held(&kvm->slots_lock);
5491
Ben Gardon531810c2021-02-02 10:57:24 -08005492 write_lock(&kvm->mmu_lock);
Sean Christopherson14a3c4f2019-09-12 19:46:06 -07005493 trace_kvm_mmu_zap_all_fast(kvm);
Sean Christophersonca333ad2019-09-12 19:46:11 -07005494
5495 /*
5496 * Toggle mmu_valid_gen between '0' and '1'. Because slots_lock is
5497 * held for the entire duration of zapping obsolete pages, it's
5498 * impossible for there to be multiple invalid generations associated
5499 * with *valid* shadow pages at any given time, i.e. there is exactly
5500 * one valid generation and (at most) one invalid generation.
5501 */
5502 kvm->arch.mmu_valid_gen = kvm->arch.mmu_valid_gen ? 0 : 1;
Sean Christopherson002c5f72019-09-12 19:46:02 -07005503
Ben Gardonb7cccd392021-04-01 16:37:35 -07005504 /* In order to ensure all threads see this change when
5505 * handling the MMU reload signal, this must happen in the
5506 * same critical section as kvm_reload_remote_mmus, and
5507 * before kvm_zap_obsolete_pages as kvm_zap_obsolete_pages
5508 * could drop the MMU lock and yield.
5509 */
5510 if (is_tdp_mmu_enabled(kvm))
5511 kvm_tdp_mmu_invalidate_all_roots(kvm);
5512
Sean Christopherson4506ecf2019-09-12 19:46:08 -07005513 /*
5514 * Notify all vcpus to reload its shadow page table and flush TLB.
5515 * Then all vcpus will switch to new shadow page table with the new
5516 * mmu_valid_gen.
5517 *
5518 * Note: we need to do this under the protection of mmu_lock,
5519 * otherwise, vcpu would purge shadow page but miss tlb flush.
5520 */
5521 kvm_reload_remote_mmus(kvm);
5522
Sean Christopherson002c5f72019-09-12 19:46:02 -07005523 kvm_zap_obsolete_pages(kvm);
Ben Gardonfaaf05b02020-10-14 11:26:47 -07005524
Ben Gardon531810c2021-02-02 10:57:24 -08005525 write_unlock(&kvm->mmu_lock);
Ben Gardon4c6654b2021-04-01 16:37:36 -07005526
5527 if (is_tdp_mmu_enabled(kvm)) {
5528 read_lock(&kvm->mmu_lock);
5529 kvm_tdp_mmu_zap_invalidated_roots(kvm);
5530 read_unlock(&kvm->mmu_lock);
5531 }
Sean Christopherson002c5f72019-09-12 19:46:02 -07005532}
5533
Sean Christopherson10605202019-09-12 19:46:10 -07005534static bool kvm_has_zapped_obsolete_pages(struct kvm *kvm)
5535{
5536 return unlikely(!list_empty_careful(&kvm->arch.zapped_obsolete_pages));
5537}
5538
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005539static void kvm_mmu_invalidate_zap_pages_in_memslot(struct kvm *kvm,
5540 struct kvm_memory_slot *slot,
5541 struct kvm_page_track_notifier_node *node)
Xiao Guangrongdecf6332015-04-14 12:04:10 +08005542{
Sean Christopherson002c5f72019-09-12 19:46:02 -07005543 kvm_mmu_zap_all_fast(kvm);
Xiao Guangrongdecf6332015-04-14 12:04:10 +08005544}
5545
Xiao Guangrongefdfe532015-05-13 14:42:27 +08005546void kvm_mmu_init_vm(struct kvm *kvm)
5547{
5548 struct kvm_page_track_notifier_node *node = &kvm->arch.mmu_sp_tracker;
5549
Ben Gardond501f742021-05-18 10:34:14 -07005550 if (!kvm_mmu_init_tdp_mmu(kvm))
5551 /*
5552 * No smp_load/store wrappers needed here as we are in
5553 * VM init and there cannot be any memslots / other threads
5554 * accessing this struct kvm yet.
5555 */
5556 kvm->arch.memslots_have_rmaps = true;
Ben Gardona2557402021-05-18 10:34:12 -07005557
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02005558 node->track_write = kvm_mmu_pte_write;
Xiao Guangrongefdfe532015-05-13 14:42:27 +08005559 node->track_flush_slot = kvm_mmu_invalidate_zap_pages_in_memslot;
5560 kvm_page_track_register_notifier(kvm, node);
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02005561}
5562
5563void kvm_mmu_uninit_vm(struct kvm *kvm)
5564{
Xiao Guangrongefdfe532015-05-13 14:42:27 +08005565 struct kvm_page_track_notifier_node *node = &kvm->arch.mmu_sp_tracker;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02005566
5567 kvm_page_track_unregister_notifier(kvm, node);
Ben Gardonfe5db272020-10-14 11:26:43 -07005568
5569 kvm_mmu_uninit_tdp_mmu(kvm);
Xiao Guangrongefdfe532015-05-13 14:42:27 +08005570}
5571
Xiao Guangrongdecf6332015-04-14 12:04:10 +08005572void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end)
Xiao Guangrongefdfe532015-05-13 14:42:27 +08005573{
5574 struct kvm_memslots *slots;
5575 struct kvm_memory_slot *memslot;
5576 int i;
Sean Christopherson1a61b7d2021-03-25 19:19:43 -07005577 bool flush = false;
Xiao Guangrongefdfe532015-05-13 14:42:27 +08005578
Ben Gardone2209712021-05-18 10:34:13 -07005579 if (kvm_memslots_have_rmaps(kvm)) {
5580 write_lock(&kvm->mmu_lock);
5581 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
5582 slots = __kvm_memslots(kvm, i);
5583 kvm_for_each_memslot(memslot, slots) {
5584 gfn_t start, end;
Xiao Guangrongefdfe532015-05-13 14:42:27 +08005585
Ben Gardone2209712021-05-18 10:34:13 -07005586 start = max(gfn_start, memslot->base_gfn);
5587 end = min(gfn_end, memslot->base_gfn + memslot->npages);
5588 if (start >= end)
5589 continue;
Xiao Guangrongefdfe532015-05-13 14:42:27 +08005590
Ben Gardone2209712021-05-18 10:34:13 -07005591 flush = slot_handle_level_range(kvm, memslot,
5592 kvm_zap_rmapp, PG_LEVEL_4K,
5593 KVM_MAX_HUGEPAGE_LEVEL, start,
5594 end - 1, true, flush);
5595 }
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02005596 }
Ben Gardone2209712021-05-18 10:34:13 -07005597 if (flush)
5598 kvm_flush_remote_tlbs_with_address(kvm, gfn_start, gfn_end);
5599 write_unlock(&kvm->mmu_lock);
Xiao Guangrongefdfe532015-05-13 14:42:27 +08005600 }
5601
Ben Gardon6103bc02021-04-01 16:37:32 -07005602 if (is_tdp_mmu_enabled(kvm)) {
5603 flush = false;
5604
5605 read_lock(&kvm->mmu_lock);
5606 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++)
5607 flush = kvm_tdp_mmu_zap_gfn_range(kvm, i, gfn_start,
5608 gfn_end, flush, true);
5609 if (flush)
5610 kvm_flush_remote_tlbs_with_address(kvm, gfn_start,
5611 gfn_end);
5612
5613 read_unlock(&kvm->mmu_lock);
5614 }
Xiao Guangrongefdfe532015-05-13 14:42:27 +08005615}
5616
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09005617static bool slot_rmap_write_protect(struct kvm *kvm,
Sean Christopherson0a234f52021-02-12 16:50:05 -08005618 struct kvm_rmap_head *rmap_head,
5619 struct kvm_memory_slot *slot)
Xiao Guangrongd77aa732015-05-13 14:42:24 +08005620{
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09005621 return __rmap_write_protect(kvm, rmap_head, false);
Xiao Guangrongd77aa732015-05-13 14:42:24 +08005622}
5623
Dor Laore0fa8262007-03-30 13:06:33 +03005624void kvm_mmu_slot_remove_write_access(struct kvm *kvm,
Jay Zhou3c9bd402020-02-27 09:32:27 +08005625 struct kvm_memory_slot *memslot,
5626 int start_level)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08005627{
Ben Gardone2209712021-05-18 10:34:13 -07005628 bool flush = false;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08005629
Ben Gardone2209712021-05-18 10:34:13 -07005630 if (kvm_memslots_have_rmaps(kvm)) {
5631 write_lock(&kvm->mmu_lock);
5632 flush = slot_handle_level(kvm, memslot, slot_rmap_write_protect,
5633 start_level, KVM_MAX_HUGEPAGE_LEVEL,
5634 false);
5635 write_unlock(&kvm->mmu_lock);
5636 }
Paul Mundt20c2df82007-07-20 10:11:58 +09005637
Ben Gardon24ae4cf2021-04-01 16:37:34 -07005638 if (is_tdp_mmu_enabled(kvm)) {
5639 read_lock(&kvm->mmu_lock);
5640 flush |= kvm_tdp_mmu_wrprot_slot(kvm, memslot, start_level);
5641 read_unlock(&kvm->mmu_lock);
5642 }
5643
Avi Kivityb5a33a72007-04-15 16:31:09 +03005644 /*
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08005645 * We can flush all the TLBs out of the mmu lock without TLB
5646 * corruption since we just change the spte from writable to
Xiao Guangronge7d11c72013-05-31 08:36:27 +08005647 * readonly so that we only need to care the case of changing
5648 * spte from present to present (changing the spte from present
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08005649 * to nonpresent will flush all the TLBs immediately), in other
5650 * words, the only case we care is mmu_spte_update() where we
Sean Christopherson5fc34242021-02-25 12:47:43 -08005651 * have checked Host-writable | MMU-writable instead of
5652 * PT_WRITABLE_MASK, that means it does not depend on PT_WRITABLE_MASK
5653 * anymore.
Avi Kivityb5a33a72007-04-15 16:31:09 +03005654 */
5655 if (flush)
Sean Christopherson7f42aa72020-02-18 13:07:36 -08005656 kvm_arch_flush_remote_tlbs_memslot(kvm, memslot);
Avi Kivityb5a33a72007-04-15 16:31:09 +03005657}
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005658
5659static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm,
Sean Christopherson0a234f52021-02-12 16:50:05 -08005660 struct kvm_rmap_head *rmap_head,
5661 struct kvm_memory_slot *slot)
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005662{
5663 u64 *sptep;
5664 struct rmap_iterator iter;
5665 int need_tlb_flush = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005666 kvm_pfn_t pfn;
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005667 struct kvm_mmu_page *sp;
5668
5669restart:
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09005670 for_each_rmap_spte(rmap_head, &iter, sptep) {
Sean Christopherson57354682020-06-22 13:20:33 -07005671 sp = sptep_to_sp(sptep);
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005672 pfn = spte_to_pfn(*sptep);
5673
5674 /*
Xiao Guangrongdecf6332015-04-14 12:04:10 +08005675 * We cannot do huge page mapping for indirect shadow pages,
5676 * which are found on the last rmap (level = 1) when not using
5677 * tdp; such shadow pages are synced with the page table in
5678 * the guest, and the guest page table is using 4K page size
5679 * mapping if the indirect sp has level = 1.
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005680 */
Sean Christophersona78986a2019-11-11 14:12:27 -08005681 if (sp->role.direct && !kvm_is_reserved_pfn(pfn) &&
Sean Christopherson9eba50f2021-02-12 16:50:06 -08005682 sp->role.level < kvm_mmu_max_mapping_level(kvm, slot, sp->gfn,
5683 pfn, PG_LEVEL_NUM)) {
Wei Yange7912382018-10-04 10:04:23 +08005684 pte_list_remove(rmap_head, sptep);
Lan Tianyu40ef75a2018-12-06 21:21:08 +08005685
5686 if (kvm_available_flush_tlb_with_range())
5687 kvm_flush_remote_tlbs_with_address(kvm, sp->gfn,
5688 KVM_PAGES_PER_HPAGE(sp->role.level));
5689 else
5690 need_tlb_flush = 1;
5691
Xiao Guangrong0d536792015-05-13 14:42:20 +08005692 goto restart;
5693 }
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005694 }
5695
5696 return need_tlb_flush;
5697}
5698
5699void kvm_mmu_zap_collapsible_sptes(struct kvm *kvm,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02005700 const struct kvm_memory_slot *memslot)
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005701{
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02005702 /* FIXME: const-ify all uses of struct kvm_memory_slot. */
Sean Christopherson9eba50f2021-02-12 16:50:06 -08005703 struct kvm_memory_slot *slot = (struct kvm_memory_slot *)memslot;
Colin Ian King31c65652021-06-22 16:09:12 +01005704 bool flush = false;
Sean Christopherson9eba50f2021-02-12 16:50:06 -08005705
Ben Gardone2209712021-05-18 10:34:13 -07005706 if (kvm_memslots_have_rmaps(kvm)) {
5707 write_lock(&kvm->mmu_lock);
5708 flush = slot_handle_leaf(kvm, slot, kvm_mmu_zap_collapsible_spte, true);
5709 if (flush)
5710 kvm_arch_flush_remote_tlbs_memslot(kvm, slot);
5711 write_unlock(&kvm->mmu_lock);
5712 }
Ben Gardon2db6f772021-04-01 16:37:33 -07005713
5714 if (is_tdp_mmu_enabled(kvm)) {
Ben Gardon2db6f772021-04-01 16:37:33 -07005715 read_lock(&kvm->mmu_lock);
5716 flush = kvm_tdp_mmu_zap_collapsible_sptes(kvm, slot, flush);
5717 if (flush)
5718 kvm_arch_flush_remote_tlbs_memslot(kvm, slot);
5719 read_unlock(&kvm->mmu_lock);
5720 }
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005721}
5722
Sean Christophersonb3594ff2020-02-18 13:07:34 -08005723void kvm_arch_flush_remote_tlbs_memslot(struct kvm *kvm,
Paolo Bonzini6c9dd6d2021-04-02 17:53:09 +02005724 const struct kvm_memory_slot *memslot)
Sean Christophersonb3594ff2020-02-18 13:07:34 -08005725{
5726 /*
Sean Christopherson7f42aa72020-02-18 13:07:36 -08005727 * All current use cases for flushing the TLBs for a specific memslot
Sean Christopherson302695a2021-03-25 19:19:41 -07005728 * related to dirty logging, and many do the TLB flush out of mmu_lock.
Sean Christopherson7f42aa72020-02-18 13:07:36 -08005729 * The interaction between the various operations on memslot must be
5730 * serialized by slots_locks to ensure the TLB flush from one operation
5731 * is observed by any other operation on the same memslot.
Sean Christophersonb3594ff2020-02-18 13:07:34 -08005732 */
5733 lockdep_assert_held(&kvm->slots_lock);
Sean Christophersoncec37642020-02-18 13:07:35 -08005734 kvm_flush_remote_tlbs_with_address(kvm, memslot->base_gfn,
5735 memslot->npages);
Sean Christophersonb3594ff2020-02-18 13:07:34 -08005736}
5737
Kai Huangf4b4b182015-01-28 10:54:24 +08005738void kvm_mmu_slot_leaf_clear_dirty(struct kvm *kvm,
5739 struct kvm_memory_slot *memslot)
5740{
Ben Gardone2209712021-05-18 10:34:13 -07005741 bool flush = false;
Kai Huangf4b4b182015-01-28 10:54:24 +08005742
Ben Gardone2209712021-05-18 10:34:13 -07005743 if (kvm_memslots_have_rmaps(kvm)) {
5744 write_lock(&kvm->mmu_lock);
5745 flush = slot_handle_leaf(kvm, memslot, __rmap_clear_dirty,
5746 false);
5747 write_unlock(&kvm->mmu_lock);
5748 }
Kai Huangf4b4b182015-01-28 10:54:24 +08005749
Ben Gardon24ae4cf2021-04-01 16:37:34 -07005750 if (is_tdp_mmu_enabled(kvm)) {
5751 read_lock(&kvm->mmu_lock);
5752 flush |= kvm_tdp_mmu_clear_dirty_slot(kvm, memslot);
5753 read_unlock(&kvm->mmu_lock);
5754 }
5755
Kai Huangf4b4b182015-01-28 10:54:24 +08005756 /*
5757 * It's also safe to flush TLBs out of mmu lock here as currently this
5758 * function is only used for dirty logging, in which case flushing TLB
5759 * out of mmu lock also guarantees no dirty pages will be lost in
5760 * dirty_bitmap.
5761 */
5762 if (flush)
Sean Christopherson7f42aa72020-02-18 13:07:36 -08005763 kvm_arch_flush_remote_tlbs_memslot(kvm, memslot);
Kai Huangf4b4b182015-01-28 10:54:24 +08005764}
Kai Huangf4b4b182015-01-28 10:54:24 +08005765
Sean Christopherson92f58b52019-09-12 19:46:04 -07005766void kvm_mmu_zap_all(struct kvm *kvm)
Avi Kivityb5a33a72007-04-15 16:31:09 +03005767{
5768 struct kvm_mmu_page *sp, *node;
Sean Christopherson7390de12019-02-05 13:01:31 -08005769 LIST_HEAD(invalid_list);
Sean Christopherson83cdb562019-02-05 13:01:35 -08005770 int ign;
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08005771
Ben Gardon531810c2021-02-02 10:57:24 -08005772 write_lock(&kvm->mmu_lock);
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08005773restart:
Sean Christopherson8a674ad2019-02-05 13:01:32 -08005774 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link) {
Sean Christophersonf95eec92020-06-23 12:35:39 -07005775 if (WARN_ON(sp->role.invalid))
Sean Christopherson8a674ad2019-02-05 13:01:32 -08005776 continue;
Sean Christopherson92f58b52019-09-12 19:46:04 -07005777 if (__kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list, &ign))
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08005778 goto restart;
Ben Gardon531810c2021-02-02 10:57:24 -08005779 if (cond_resched_rwlock_write(&kvm->mmu_lock))
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08005780 goto restart;
5781 }
5782
Sean Christopherson47714502019-02-05 13:01:23 -08005783 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Ben Gardonfaaf05b02020-10-14 11:26:47 -07005784
Paolo Bonzini897218f2021-02-06 09:53:33 -05005785 if (is_tdp_mmu_enabled(kvm))
Ben Gardonfaaf05b02020-10-14 11:26:47 -07005786 kvm_tdp_mmu_zap_all(kvm);
5787
Ben Gardon531810c2021-02-02 10:57:24 -08005788 write_unlock(&kvm->mmu_lock);
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08005789}
5790
Sean Christopherson15248252019-02-05 12:54:17 -08005791void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm, u64 gen)
Xiao Guangrongf8f55942013-06-07 16:51:26 +08005792{
Sean Christopherson164bf7e2019-02-05 13:01:18 -08005793 WARN_ON(gen & KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS);
Sean Christophersone1359e22019-02-05 13:01:12 -08005794
Sean Christopherson164bf7e2019-02-05 13:01:18 -08005795 gen &= MMIO_SPTE_GEN_MASK;
Sean Christophersone1359e22019-02-05 13:01:12 -08005796
Xiao Guangrongf8f55942013-06-07 16:51:26 +08005797 /*
Sean Christophersone1359e22019-02-05 13:01:12 -08005798 * Generation numbers are incremented in multiples of the number of
5799 * address spaces in order to provide unique generations across all
5800 * address spaces. Strip what is effectively the address space
5801 * modifier prior to checking for a wrap of the MMIO generation so
5802 * that a wrap in any address space is detected.
5803 */
5804 gen &= ~((u64)KVM_ADDRESS_SPACE_NUM - 1);
5805
5806 /*
5807 * The very rare case: if the MMIO generation number has wrapped,
Xiao Guangrongf8f55942013-06-07 16:51:26 +08005808 * zap all shadow pages.
Xiao Guangrongf8f55942013-06-07 16:51:26 +08005809 */
Sean Christophersone1359e22019-02-05 13:01:12 -08005810 if (unlikely(gen == 0)) {
Bandan Dasae0f5492016-11-15 01:36:18 -05005811 kvm_debug_ratelimited("kvm: zapping shadow pages for mmio generation wraparound\n");
Sean Christopherson92f58b52019-09-12 19:46:04 -07005812 kvm_mmu_zap_all_fast(kvm);
Takuya Yoshikawa7a2e8aa2013-06-21 01:34:31 +09005813 }
Xiao Guangrongf8f55942013-06-07 16:51:26 +08005814}
5815
Dave Chinner70534a72013-08-28 10:18:14 +10005816static unsigned long
5817mmu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
Izik Eidus3ee16c82008-03-30 15:17:21 +03005818{
5819 struct kvm *kvm;
Ying Han1495f232011-05-24 17:12:27 -07005820 int nr_to_scan = sc->nr_to_scan;
Dave Chinner70534a72013-08-28 10:18:14 +10005821 unsigned long freed = 0;
Izik Eidus3ee16c82008-03-30 15:17:21 +03005822
Junaid Shahid0d9ce162019-01-03 17:14:28 -08005823 mutex_lock(&kvm_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03005824
5825 list_for_each_entry(kvm, &vm_list, vm_list) {
Jan Kiszka3d56cbd2011-12-02 18:35:24 +01005826 int idx;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08005827 LIST_HEAD(invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03005828
Gleb Natapov19526392012-06-04 14:53:23 +03005829 /*
Takuya Yoshikawa35f2d162012-08-20 18:35:39 +09005830 * Never scan more than sc->nr_to_scan VM instances.
5831 * Will not hit this condition practically since we do not try
5832 * to shrink more than one VM and it is very unlikely to see
5833 * !n_used_mmu_pages so many times.
5834 */
5835 if (!nr_to_scan--)
5836 break;
5837 /*
Gleb Natapov19526392012-06-04 14:53:23 +03005838 * n_used_mmu_pages is accessed without holding kvm->mmu_lock
5839 * here. We may skip a VM instance errorneosly, but we do not
5840 * want to shrink a VM that only started to populate its MMU
5841 * anyway.
5842 */
Sean Christopherson10605202019-09-12 19:46:10 -07005843 if (!kvm->arch.n_used_mmu_pages &&
5844 !kvm_has_zapped_obsolete_pages(kvm))
Gleb Natapov19526392012-06-04 14:53:23 +03005845 continue;
Gleb Natapov19526392012-06-04 14:53:23 +03005846
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005847 idx = srcu_read_lock(&kvm->srcu);
Ben Gardon531810c2021-02-02 10:57:24 -08005848 write_lock(&kvm->mmu_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03005849
Sean Christopherson10605202019-09-12 19:46:10 -07005850 if (kvm_has_zapped_obsolete_pages(kvm)) {
5851 kvm_mmu_commit_zap_page(kvm,
5852 &kvm->arch.zapped_obsolete_pages);
5853 goto unlock;
5854 }
5855
Sean Christophersonebdb2922020-06-23 12:35:41 -07005856 freed = kvm_mmu_zap_oldest_mmu_pages(kvm, sc->nr_to_scan);
Gleb Natapov19526392012-06-04 14:53:23 +03005857
Sean Christopherson10605202019-09-12 19:46:10 -07005858unlock:
Ben Gardon531810c2021-02-02 10:57:24 -08005859 write_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005860 srcu_read_unlock(&kvm->srcu, idx);
Gleb Natapov19526392012-06-04 14:53:23 +03005861
Dave Chinner70534a72013-08-28 10:18:14 +10005862 /*
5863 * unfair on small ones
5864 * per-vm shrinkers cry out
5865 * sadness comes quickly
5866 */
Gleb Natapov19526392012-06-04 14:53:23 +03005867 list_move_tail(&kvm->vm_list, &vm_list);
5868 break;
Izik Eidus3ee16c82008-03-30 15:17:21 +03005869 }
Izik Eidus3ee16c82008-03-30 15:17:21 +03005870
Junaid Shahid0d9ce162019-01-03 17:14:28 -08005871 mutex_unlock(&kvm_lock);
Dave Chinner70534a72013-08-28 10:18:14 +10005872 return freed;
Dave Chinner70534a72013-08-28 10:18:14 +10005873}
5874
5875static unsigned long
5876mmu_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
5877{
Dave Hansen45221ab2010-08-19 18:11:37 -07005878 return percpu_counter_read_positive(&kvm_total_used_mmu_pages);
Izik Eidus3ee16c82008-03-30 15:17:21 +03005879}
5880
5881static struct shrinker mmu_shrinker = {
Dave Chinner70534a72013-08-28 10:18:14 +10005882 .count_objects = mmu_shrink_count,
5883 .scan_objects = mmu_shrink_scan,
Izik Eidus3ee16c82008-03-30 15:17:21 +03005884 .seeks = DEFAULT_SEEKS * 10,
5885};
5886
Ingo Molnar2ddfd202008-05-22 10:37:48 +02005887static void mmu_destroy_caches(void)
Dor Laore0fa8262007-03-30 13:06:33 +03005888{
Tim Hansenc1bd7432017-10-07 23:15:23 -04005889 kmem_cache_destroy(pte_list_desc_cache);
5890 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03005891}
5892
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01005893static bool get_nx_auto_mode(void)
5894{
5895 /* Return true when CPU has the bug, and mitigations are ON */
5896 return boot_cpu_has_bug(X86_BUG_ITLB_MULTIHIT) && !cpu_mitigations_off();
5897}
5898
5899static void __set_nx_huge_pages(bool val)
5900{
5901 nx_huge_pages = itlb_multihit_kvm_mitigation = val;
5902}
5903
5904static int set_nx_huge_pages(const char *val, const struct kernel_param *kp)
5905{
5906 bool old_val = nx_huge_pages;
5907 bool new_val;
5908
5909 /* In "auto" mode deploy workaround only if CPU has the bug. */
5910 if (sysfs_streq(val, "off"))
5911 new_val = 0;
5912 else if (sysfs_streq(val, "force"))
5913 new_val = 1;
5914 else if (sysfs_streq(val, "auto"))
5915 new_val = get_nx_auto_mode();
5916 else if (strtobool(val, &new_val) < 0)
5917 return -EINVAL;
5918
5919 __set_nx_huge_pages(new_val);
5920
5921 if (new_val != old_val) {
5922 struct kvm *kvm;
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01005923
5924 mutex_lock(&kvm_lock);
5925
5926 list_for_each_entry(kvm, &vm_list, vm_list) {
Sean Christophersoned69a6c2019-11-13 11:30:32 -08005927 mutex_lock(&kvm->slots_lock);
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01005928 kvm_mmu_zap_all_fast(kvm);
Sean Christophersoned69a6c2019-11-13 11:30:32 -08005929 mutex_unlock(&kvm->slots_lock);
Junaid Shahid1aa9b952019-11-04 20:26:00 +01005930
5931 wake_up_process(kvm->arch.nx_lpage_recovery_thread);
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01005932 }
5933 mutex_unlock(&kvm_lock);
5934 }
5935
5936 return 0;
5937}
5938
Avi Kivityb5a33a72007-04-15 16:31:09 +03005939int kvm_mmu_module_init(void)
5940{
Arnd Bergmannab271bd2018-01-10 17:26:59 +01005941 int ret = -ENOMEM;
5942
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01005943 if (nx_huge_pages == -1)
5944 __set_nx_huge_pages(get_nx_auto_mode());
5945
Vitaly Kuznetsov36d9594d2018-10-08 21:28:10 +02005946 /*
5947 * MMU roles use union aliasing which is, generally speaking, an
5948 * undefined behavior. However, we supposedly know how compilers behave
5949 * and the current status quo is unlikely to change. Guardians below are
5950 * supposed to let us know if the assumption becomes false.
5951 */
5952 BUILD_BUG_ON(sizeof(union kvm_mmu_page_role) != sizeof(u32));
5953 BUILD_BUG_ON(sizeof(union kvm_mmu_extended_role) != sizeof(u32));
5954 BUILD_BUG_ON(sizeof(union kvm_mmu_role) != sizeof(u64));
5955
Junaid Shahid28a1f3a2018-08-14 10:15:34 -07005956 kvm_mmu_reset_all_pte_masks();
Junaid Shahidf160c7b2016-12-06 16:46:16 -08005957
Xiao Guangrong53c07b12011-05-15 23:26:20 +08005958 pte_list_desc_cache = kmem_cache_create("pte_list_desc",
5959 sizeof(struct pte_list_desc),
Shakeel Butt46bea482017-10-05 18:07:24 -07005960 0, SLAB_ACCOUNT, NULL);
Xiao Guangrong53c07b12011-05-15 23:26:20 +08005961 if (!pte_list_desc_cache)
Arnd Bergmannab271bd2018-01-10 17:26:59 +01005962 goto out;
Avi Kivityb5a33a72007-04-15 16:31:09 +03005963
Avi Kivityd3d25b02007-05-30 12:34:53 +03005964 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
5965 sizeof(struct kvm_mmu_page),
Shakeel Butt46bea482017-10-05 18:07:24 -07005966 0, SLAB_ACCOUNT, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03005967 if (!mmu_page_header_cache)
Arnd Bergmannab271bd2018-01-10 17:26:59 +01005968 goto out;
Avi Kivityd3d25b02007-05-30 12:34:53 +03005969
Tejun Heo908c7f12014-09-08 09:51:29 +09005970 if (percpu_counter_init(&kvm_total_used_mmu_pages, 0, GFP_KERNEL))
Arnd Bergmannab271bd2018-01-10 17:26:59 +01005971 goto out;
Wei Yongjun45bf21a2010-08-23 16:13:15 +08005972
Arnd Bergmannab271bd2018-01-10 17:26:59 +01005973 ret = register_shrinker(&mmu_shrinker);
5974 if (ret)
5975 goto out;
Izik Eidus3ee16c82008-03-30 15:17:21 +03005976
Avi Kivityb5a33a72007-04-15 16:31:09 +03005977 return 0;
5978
Arnd Bergmannab271bd2018-01-10 17:26:59 +01005979out:
Izik Eidus3ee16c82008-03-30 15:17:21 +03005980 mmu_destroy_caches();
Arnd Bergmannab271bd2018-01-10 17:26:59 +01005981 return ret;
Avi Kivityb5a33a72007-04-15 16:31:09 +03005982}
5983
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08005984/*
Peng Hao39337ad2018-10-04 11:45:00 -04005985 * Calculate mmu pages needed for kvm.
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08005986 */
Ben Gardonbc8a3d82019-04-08 11:07:30 -07005987unsigned long kvm_mmu_calculate_default_mmu_pages(struct kvm *kvm)
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08005988{
Ben Gardonbc8a3d82019-04-08 11:07:30 -07005989 unsigned long nr_mmu_pages;
5990 unsigned long nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02005991 struct kvm_memslots *slots;
Xiao Guangrongbe6ba0f2011-11-24 17:39:18 +08005992 struct kvm_memory_slot *memslot;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02005993 int i;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08005994
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02005995 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
5996 slots = __kvm_memslots(kvm, i);
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08005997
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02005998 kvm_for_each_memslot(memslot, slots)
5999 nr_pages += memslot->npages;
6000 }
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08006001
6002 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
Ben Gardonbc8a3d82019-04-08 11:07:30 -07006003 nr_mmu_pages = max(nr_mmu_pages, KVM_MIN_ALLOC_MMU_PAGES);
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08006004
6005 return nr_mmu_pages;
6006}
6007
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08006008void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
6009{
Paolo Bonzini95f93af2013-10-02 16:56:12 +02006010 kvm_mmu_unload(vcpu);
Jiří Paleček1cfff4d2019-06-22 19:42:04 +02006011 free_mmu_pages(&vcpu->arch.root_mmu);
6012 free_mmu_pages(&vcpu->arch.guest_mmu);
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08006013 mmu_free_memory_caches(vcpu);
Xiao Guangrongb034cf02010-12-23 16:08:35 +08006014}
6015
Xiao Guangrongb034cf02010-12-23 16:08:35 +08006016void kvm_mmu_module_exit(void)
6017{
6018 mmu_destroy_caches();
6019 percpu_counter_destroy(&kvm_total_used_mmu_pages);
6020 unregister_shrinker(&mmu_shrinker);
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08006021 mmu_audit_disable();
6022}
Junaid Shahid1aa9b952019-11-04 20:26:00 +01006023
6024static int set_nx_huge_pages_recovery_ratio(const char *val, const struct kernel_param *kp)
6025{
6026 unsigned int old_val;
6027 int err;
6028
6029 old_val = nx_huge_pages_recovery_ratio;
6030 err = param_set_uint(val, kp);
6031 if (err)
6032 return err;
6033
6034 if (READ_ONCE(nx_huge_pages) &&
6035 !old_val && nx_huge_pages_recovery_ratio) {
6036 struct kvm *kvm;
6037
6038 mutex_lock(&kvm_lock);
6039
6040 list_for_each_entry(kvm, &vm_list, vm_list)
6041 wake_up_process(kvm->arch.nx_lpage_recovery_thread);
6042
6043 mutex_unlock(&kvm_lock);
6044 }
6045
6046 return err;
6047}
6048
6049static void kvm_recover_nx_lpages(struct kvm *kvm)
6050{
Sean Christophersonade74e12021-06-15 09:29:05 -07006051 unsigned long nx_lpage_splits = kvm->stat.nx_lpage_splits;
Junaid Shahid1aa9b952019-11-04 20:26:00 +01006052 int rcu_idx;
6053 struct kvm_mmu_page *sp;
6054 unsigned int ratio;
6055 LIST_HEAD(invalid_list);
Sean Christopherson048f4982021-03-25 13:01:18 -07006056 bool flush = false;
Junaid Shahid1aa9b952019-11-04 20:26:00 +01006057 ulong to_zap;
6058
6059 rcu_idx = srcu_read_lock(&kvm->srcu);
Ben Gardon531810c2021-02-02 10:57:24 -08006060 write_lock(&kvm->mmu_lock);
Junaid Shahid1aa9b952019-11-04 20:26:00 +01006061
6062 ratio = READ_ONCE(nx_huge_pages_recovery_ratio);
Sean Christophersonade74e12021-06-15 09:29:05 -07006063 to_zap = ratio ? DIV_ROUND_UP(nx_lpage_splits, ratio) : 0;
Sean Christopherson7d919c72020-09-23 11:37:29 -07006064 for ( ; to_zap; --to_zap) {
6065 if (list_empty(&kvm->arch.lpage_disallowed_mmu_pages))
6066 break;
6067
Junaid Shahid1aa9b952019-11-04 20:26:00 +01006068 /*
6069 * We use a separate list instead of just using active_mmu_pages
6070 * because the number of lpage_disallowed pages is expected to
6071 * be relatively small compared to the total.
6072 */
6073 sp = list_first_entry(&kvm->arch.lpage_disallowed_mmu_pages,
6074 struct kvm_mmu_page,
6075 lpage_disallowed_link);
6076 WARN_ON_ONCE(!sp->lpage_disallowed);
Paolo Bonzini897218f2021-02-06 09:53:33 -05006077 if (is_tdp_mmu_page(sp)) {
Paolo Bonzini315f02c2021-04-06 11:08:51 -04006078 flush |= kvm_tdp_mmu_zap_sp(kvm, sp);
Ben Gardon8d1a1822021-02-02 10:57:15 -08006079 } else {
Ben Gardon29cf0f52020-10-14 11:27:00 -07006080 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
6081 WARN_ON_ONCE(sp->lpage_disallowed);
6082 }
Junaid Shahid1aa9b952019-11-04 20:26:00 +01006083
Ben Gardon531810c2021-02-02 10:57:24 -08006084 if (need_resched() || rwlock_needbreak(&kvm->mmu_lock)) {
Sean Christopherson048f4982021-03-25 13:01:18 -07006085 kvm_mmu_remote_flush_or_zap(kvm, &invalid_list, flush);
Ben Gardon531810c2021-02-02 10:57:24 -08006086 cond_resched_rwlock_write(&kvm->mmu_lock);
Sean Christopherson048f4982021-03-25 13:01:18 -07006087 flush = false;
Junaid Shahid1aa9b952019-11-04 20:26:00 +01006088 }
6089 }
Sean Christopherson048f4982021-03-25 13:01:18 -07006090 kvm_mmu_remote_flush_or_zap(kvm, &invalid_list, flush);
Junaid Shahid1aa9b952019-11-04 20:26:00 +01006091
Ben Gardon531810c2021-02-02 10:57:24 -08006092 write_unlock(&kvm->mmu_lock);
Junaid Shahid1aa9b952019-11-04 20:26:00 +01006093 srcu_read_unlock(&kvm->srcu, rcu_idx);
6094}
6095
6096static long get_nx_lpage_recovery_timeout(u64 start_time)
6097{
6098 return READ_ONCE(nx_huge_pages) && READ_ONCE(nx_huge_pages_recovery_ratio)
6099 ? start_time + 60 * HZ - get_jiffies_64()
6100 : MAX_SCHEDULE_TIMEOUT;
6101}
6102
6103static int kvm_nx_lpage_recovery_worker(struct kvm *kvm, uintptr_t data)
6104{
6105 u64 start_time;
6106 long remaining_time;
6107
6108 while (true) {
6109 start_time = get_jiffies_64();
6110 remaining_time = get_nx_lpage_recovery_timeout(start_time);
6111
6112 set_current_state(TASK_INTERRUPTIBLE);
6113 while (!kthread_should_stop() && remaining_time > 0) {
6114 schedule_timeout(remaining_time);
6115 remaining_time = get_nx_lpage_recovery_timeout(start_time);
6116 set_current_state(TASK_INTERRUPTIBLE);
6117 }
6118
6119 set_current_state(TASK_RUNNING);
6120
6121 if (kthread_should_stop())
6122 return 0;
6123
6124 kvm_recover_nx_lpages(kvm);
6125 }
6126}
6127
6128int kvm_mmu_post_init_vm(struct kvm *kvm)
6129{
6130 int err;
6131
6132 err = kvm_vm_create_worker_thread(kvm, kvm_nx_lpage_recovery_worker, 0,
6133 "kvm-nx-lpage-recovery",
6134 &kvm->arch.nx_lpage_recovery_thread);
6135 if (!err)
6136 kthread_unpark(kvm->arch.nx_lpage_recovery_thread);
6137
6138 return err;
6139}
6140
6141void kvm_mmu_pre_destroy_vm(struct kvm *kvm)
6142{
6143 if (kvm->arch.nx_lpage_recovery_thread)
6144 kthread_stop(kvm->arch.nx_lpage_recovery_thread);
6145}