blob: 66e4d096fe05a10c1edeee06f78176c1eae27154 [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);
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001186 while (mask) {
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001187 rmap_head = __gfn_to_rmap(slot->base_gfn + gfn_offset + __ffs(mask),
Sean Christopherson3bae0452020-04-27 17:54:22 -07001188 PG_LEVEL_4K, slot);
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001189 __rmap_write_protect(kvm, rmap_head, false);
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001190
1191 /* clear the first set bit */
1192 mask &= mask - 1;
1193 }
1194}
1195
Kai Huang3b0f1d02015-01-28 10:54:23 +08001196/**
Peter Feinerac8d57e2017-06-30 17:26:31 -07001197 * kvm_mmu_clear_dirty_pt_masked - clear MMU D-bit for PT level pages, or write
1198 * protect the page if the D-bit isn't supported.
Kai Huangf4b4b182015-01-28 10:54:24 +08001199 * @kvm: kvm instance
1200 * @slot: slot to clear D-bit
1201 * @gfn_offset: start of the BITS_PER_LONG pages we care about
1202 * @mask: indicates which pages we should clear D-bit
1203 *
1204 * Used for PML to re-log the dirty GPAs after userspace querying dirty_bitmap.
1205 */
Sean Christophersona018eba2021-02-12 16:50:10 -08001206static void kvm_mmu_clear_dirty_pt_masked(struct kvm *kvm,
1207 struct kvm_memory_slot *slot,
1208 gfn_t gfn_offset, unsigned long mask)
Kai Huangf4b4b182015-01-28 10:54:24 +08001209{
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001210 struct kvm_rmap_head *rmap_head;
Kai Huangf4b4b182015-01-28 10:54:24 +08001211
Paolo Bonzini897218f2021-02-06 09:53:33 -05001212 if (is_tdp_mmu_enabled(kvm))
Ben Gardona6a0b052020-10-14 11:26:55 -07001213 kvm_tdp_mmu_clear_dirty_pt_masked(kvm, slot,
1214 slot->base_gfn + gfn_offset, mask, false);
Kai Huangf4b4b182015-01-28 10:54:24 +08001215 while (mask) {
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001216 rmap_head = __gfn_to_rmap(slot->base_gfn + gfn_offset + __ffs(mask),
Sean Christopherson3bae0452020-04-27 17:54:22 -07001217 PG_LEVEL_4K, slot);
Sean Christopherson0a234f52021-02-12 16:50:05 -08001218 __rmap_clear_dirty(kvm, rmap_head, slot);
Kai Huangf4b4b182015-01-28 10:54:24 +08001219
1220 /* clear the first set bit */
1221 mask &= mask - 1;
1222 }
1223}
Kai Huangf4b4b182015-01-28 10:54:24 +08001224
1225/**
Kai Huang3b0f1d02015-01-28 10:54:23 +08001226 * kvm_arch_mmu_enable_log_dirty_pt_masked - enable dirty logging for selected
1227 * PT level pages.
1228 *
1229 * It calls kvm_mmu_write_protect_pt_masked to write protect selected pages to
1230 * enable dirty logging for them.
1231 *
Keqian Zhu89212912021-04-29 11:41:15 +08001232 * We need to care about huge page mappings: e.g. during dirty logging we may
1233 * have such mappings.
Kai Huang3b0f1d02015-01-28 10:54:23 +08001234 */
1235void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
1236 struct kvm_memory_slot *slot,
1237 gfn_t gfn_offset, unsigned long mask)
1238{
Keqian Zhu89212912021-04-29 11:41:15 +08001239 /*
1240 * Huge pages are NOT write protected when we start dirty logging in
1241 * initially-all-set mode; must write protect them here so that they
1242 * are split to 4K on the first write.
1243 *
1244 * The gfn_offset is guaranteed to be aligned to 64, but the base_gfn
1245 * of memslot has no such restriction, so the range can cross two large
1246 * pages.
1247 */
1248 if (kvm_dirty_log_manual_protect_and_init_set(kvm)) {
1249 gfn_t start = slot->base_gfn + gfn_offset + __ffs(mask);
1250 gfn_t end = slot->base_gfn + gfn_offset + __fls(mask);
1251
1252 kvm_mmu_slot_gfn_write_protect(kvm, slot, start, PG_LEVEL_2M);
1253
1254 /* Cross two large pages? */
1255 if (ALIGN(start << PAGE_SHIFT, PMD_SIZE) !=
1256 ALIGN(end << PAGE_SHIFT, PMD_SIZE))
1257 kvm_mmu_slot_gfn_write_protect(kvm, slot, end,
1258 PG_LEVEL_2M);
1259 }
1260
1261 /* Now handle 4K PTEs. */
Sean Christophersona018eba2021-02-12 16:50:10 -08001262 if (kvm_x86_ops.cpu_dirty_log_size)
1263 kvm_mmu_clear_dirty_pt_masked(kvm, slot, gfn_offset, mask);
Kai Huang88178fd2015-01-28 10:54:27 +08001264 else
1265 kvm_mmu_write_protect_pt_masked(kvm, slot, gfn_offset, mask);
Kai Huang3b0f1d02015-01-28 10:54:23 +08001266}
1267
Peter Xufb04a1e2020-09-30 21:22:22 -04001268int kvm_cpu_dirty_log_size(void)
1269{
Sean Christopherson6dd03802021-02-12 16:50:09 -08001270 return kvm_x86_ops.cpu_dirty_log_size;
Peter Xufb04a1e2020-09-30 21:22:22 -04001271}
1272
Xiao Guangrongaeecee22016-02-24 17:51:08 +08001273bool kvm_mmu_slot_gfn_write_protect(struct kvm *kvm,
Keqian Zhu3ad93562021-04-29 11:41:14 +08001274 struct kvm_memory_slot *slot, u64 gfn,
1275 int min_level)
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001276{
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001277 struct kvm_rmap_head *rmap_head;
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001278 int i;
Xiao Guangrong2f845692012-06-20 15:56:53 +08001279 bool write_protected = false;
Takuya Yoshikawa5dc99b232012-03-01 19:32:16 +09001280
Keqian Zhu3ad93562021-04-29 11:41:14 +08001281 for (i = min_level; i <= KVM_MAX_HUGEPAGE_LEVEL; ++i) {
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001282 rmap_head = __gfn_to_rmap(gfn, i, slot);
Xiao Guangrongaeecee22016-02-24 17:51:08 +08001283 write_protected |= __rmap_write_protect(kvm, rmap_head, true);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001284 }
1285
Paolo Bonzini897218f2021-02-06 09:53:33 -05001286 if (is_tdp_mmu_enabled(kvm))
Ben Gardon46044f72020-10-14 11:26:57 -07001287 write_protected |=
Keqian Zhu3ad93562021-04-29 11:41:14 +08001288 kvm_tdp_mmu_write_protect_gfn(kvm, slot, gfn, min_level);
Ben Gardon46044f72020-10-14 11:26:57 -07001289
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001290 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -08001291}
1292
Xiao Guangrongaeecee22016-02-24 17:51:08 +08001293static bool rmap_write_protect(struct kvm_vcpu *vcpu, u64 gfn)
1294{
1295 struct kvm_memory_slot *slot;
1296
1297 slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
Keqian Zhu3ad93562021-04-29 11:41:14 +08001298 return kvm_mmu_slot_gfn_write_protect(vcpu->kvm, slot, gfn, PG_LEVEL_4K);
Xiao Guangrongaeecee22016-02-24 17:51:08 +08001299}
1300
Sean Christopherson0a234f52021-02-12 16:50:05 -08001301static bool kvm_zap_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
1302 struct kvm_memory_slot *slot)
Xiao Guangrong6a49f852015-05-13 14:42:25 +08001303{
1304 u64 *sptep;
1305 struct rmap_iterator iter;
1306 bool flush = false;
1307
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001308 while ((sptep = rmap_get_first(rmap_head, &iter))) {
Stephen Zhang805a0f82021-01-27 10:08:45 +08001309 rmap_printk("spte %p %llx.\n", sptep, *sptep);
Xiao Guangrong6a49f852015-05-13 14:42:25 +08001310
Wei Yange7912382018-10-04 10:04:23 +08001311 pte_list_remove(rmap_head, sptep);
Xiao Guangrong6a49f852015-05-13 14:42:25 +08001312 flush = true;
1313 }
1314
1315 return flush;
1316}
1317
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001318static bool kvm_unmap_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
1319 struct kvm_memory_slot *slot, gfn_t gfn, int level,
1320 pte_t unused)
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001321{
Sean Christopherson0a234f52021-02-12 16:50:05 -08001322 return kvm_zap_rmapp(kvm, rmap_head, slot);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001323}
1324
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001325static bool kvm_set_pte_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
1326 struct kvm_memory_slot *slot, gfn_t gfn, int level,
1327 pte_t pte)
Izik Eidus3da0dd42009-09-23 21:47:18 +03001328{
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001329 u64 *sptep;
1330 struct rmap_iterator iter;
Izik Eidus3da0dd42009-09-23 21:47:18 +03001331 int need_flush = 0;
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001332 u64 new_spte;
Dan Williamsba049e92016-01-15 16:56:11 -08001333 kvm_pfn_t new_pfn;
Izik Eidus3da0dd42009-09-23 21:47:18 +03001334
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001335 WARN_ON(pte_huge(pte));
1336 new_pfn = pte_pfn(pte);
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001337
Xiao Guangrong0d536792015-05-13 14:42:20 +08001338restart:
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001339 for_each_rmap_spte(rmap_head, &iter, sptep) {
Stephen Zhang805a0f82021-01-27 10:08:45 +08001340 rmap_printk("spte %p %llx gfn %llx (%d)\n",
Junaid Shahidf160c7b2016-12-06 16:46:16 -08001341 sptep, *sptep, gfn, level);
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001342
Izik Eidus3da0dd42009-09-23 21:47:18 +03001343 need_flush = 1;
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001344
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001345 if (pte_write(pte)) {
Wei Yange7912382018-10-04 10:04:23 +08001346 pte_list_remove(rmap_head, sptep);
Xiao Guangrong0d536792015-05-13 14:42:20 +08001347 goto restart;
Izik Eidus3da0dd42009-09-23 21:47:18 +03001348 } else {
Paolo Bonzinicb3eeda2020-09-28 10:17:17 -04001349 new_spte = kvm_mmu_changed_pte_notifier_make_spte(
1350 *sptep, new_pfn);
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001351
1352 mmu_spte_clear_track_bits(sptep);
1353 mmu_spte_set(sptep, new_spte);
Izik Eidus3da0dd42009-09-23 21:47:18 +03001354 }
1355 }
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001356
Lan Tianyu3cc5ea92018-12-06 21:21:12 +08001357 if (need_flush && kvm_available_flush_tlb_with_range()) {
1358 kvm_flush_remote_tlbs_with_address(kvm, gfn, 1);
1359 return 0;
1360 }
1361
Lan Tianyu0cf853c2018-12-06 21:21:11 +08001362 return need_flush;
Izik Eidus3da0dd42009-09-23 21:47:18 +03001363}
1364
Xiao Guangrong6ce1f4e2015-05-13 14:42:22 +08001365struct slot_rmap_walk_iterator {
1366 /* input fields. */
1367 struct kvm_memory_slot *slot;
1368 gfn_t start_gfn;
1369 gfn_t end_gfn;
1370 int start_level;
1371 int end_level;
1372
1373 /* output fields. */
1374 gfn_t gfn;
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001375 struct kvm_rmap_head *rmap;
Xiao Guangrong6ce1f4e2015-05-13 14:42:22 +08001376 int level;
1377
1378 /* private field. */
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001379 struct kvm_rmap_head *end_rmap;
Xiao Guangrong6ce1f4e2015-05-13 14:42:22 +08001380};
1381
1382static void
1383rmap_walk_init_level(struct slot_rmap_walk_iterator *iterator, int level)
1384{
1385 iterator->level = level;
1386 iterator->gfn = iterator->start_gfn;
1387 iterator->rmap = __gfn_to_rmap(iterator->gfn, level, iterator->slot);
1388 iterator->end_rmap = __gfn_to_rmap(iterator->end_gfn, level,
1389 iterator->slot);
1390}
1391
1392static void
1393slot_rmap_walk_init(struct slot_rmap_walk_iterator *iterator,
1394 struct kvm_memory_slot *slot, int start_level,
1395 int end_level, gfn_t start_gfn, gfn_t end_gfn)
1396{
1397 iterator->slot = slot;
1398 iterator->start_level = start_level;
1399 iterator->end_level = end_level;
1400 iterator->start_gfn = start_gfn;
1401 iterator->end_gfn = end_gfn;
1402
1403 rmap_walk_init_level(iterator, iterator->start_level);
1404}
1405
1406static bool slot_rmap_walk_okay(struct slot_rmap_walk_iterator *iterator)
1407{
1408 return !!iterator->rmap;
1409}
1410
1411static void slot_rmap_walk_next(struct slot_rmap_walk_iterator *iterator)
1412{
1413 if (++iterator->rmap <= iterator->end_rmap) {
1414 iterator->gfn += (1UL << KVM_HPAGE_GFN_SHIFT(iterator->level));
1415 return;
1416 }
1417
1418 if (++iterator->level > iterator->end_level) {
1419 iterator->rmap = NULL;
1420 return;
1421 }
1422
1423 rmap_walk_init_level(iterator, iterator->level);
1424}
1425
1426#define for_each_slot_rmap_range(_slot_, _start_level_, _end_level_, \
1427 _start_gfn, _end_gfn, _iter_) \
1428 for (slot_rmap_walk_init(_iter_, _slot_, _start_level_, \
1429 _end_level_, _start_gfn, _end_gfn); \
1430 slot_rmap_walk_okay(_iter_); \
1431 slot_rmap_walk_next(_iter_))
1432
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001433typedef bool (*rmap_handler_t)(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
1434 struct kvm_memory_slot *slot, gfn_t gfn,
1435 int level, pte_t pte);
Sean Christophersonc1b91492021-02-25 17:03:28 -08001436
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001437static __always_inline bool kvm_handle_gfn_range(struct kvm *kvm,
1438 struct kvm_gfn_range *range,
1439 rmap_handler_t handler)
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001440{
Xiao Guangrong6ce1f4e2015-05-13 14:42:22 +08001441 struct slot_rmap_walk_iterator iterator;
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001442 bool ret = false;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001443
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001444 for_each_slot_rmap_range(range->slot, PG_LEVEL_4K, KVM_MAX_HUGEPAGE_LEVEL,
1445 range->start, range->end - 1, &iterator)
1446 ret |= handler(kvm, iterator.rmap, range->slot, iterator.gfn,
1447 iterator.level, range->pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001448
Takuya Yoshikawaf3953022012-07-02 17:58:48 +09001449 return ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001450}
1451
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001452bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
Takuya Yoshikawa84504ef2012-07-02 17:55:48 +09001453{
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001454 bool flush;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001455
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001456 flush = kvm_handle_gfn_range(kvm, range, kvm_unmap_rmapp);
Ben Gardon063afac2020-10-14 11:26:52 -07001457
Paolo Bonzini897218f2021-02-06 09:53:33 -05001458 if (is_tdp_mmu_enabled(kvm))
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001459 flush |= kvm_tdp_mmu_unmap_gfn_range(kvm, range, flush);
Ben Gardon063afac2020-10-14 11:26:52 -07001460
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001461 return flush;
Takuya Yoshikawab3ae2092012-07-02 17:56:33 +09001462}
1463
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001464bool kvm_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
Izik Eidus3da0dd42009-09-23 21:47:18 +03001465{
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001466 bool flush;
Ben Gardon1d8dd6b2020-10-14 11:26:54 -07001467
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001468 flush = kvm_handle_gfn_range(kvm, range, kvm_set_pte_rmapp);
Ben Gardon1d8dd6b2020-10-14 11:26:54 -07001469
Paolo Bonzini897218f2021-02-06 09:53:33 -05001470 if (is_tdp_mmu_enabled(kvm))
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001471 flush |= kvm_tdp_mmu_set_spte_gfn(kvm, range);
Ben Gardon1d8dd6b2020-10-14 11:26:54 -07001472
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001473 return flush;
Izik Eidus3da0dd42009-09-23 21:47:18 +03001474}
1475
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001476static bool kvm_age_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
1477 struct kvm_memory_slot *slot, gfn_t gfn, int level,
1478 pte_t unused)
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001479{
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001480 u64 *sptep;
Kees Cook3f649ab2020-06-03 13:09:38 -07001481 struct rmap_iterator iter;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001482 int young = 0;
1483
Junaid Shahidf160c7b2016-12-06 16:46:16 -08001484 for_each_rmap_spte(rmap_head, &iter, sptep)
1485 young |= mmu_spte_age(sptep);
Xiao Guangrong0d536792015-05-13 14:42:20 +08001486
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001487 return young;
1488}
1489
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001490static bool kvm_test_age_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
1491 struct kvm_memory_slot *slot, gfn_t gfn,
1492 int level, pte_t unused)
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001493{
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09001494 u64 *sptep;
1495 struct rmap_iterator iter;
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001496
Junaid Shahid83ef6c82016-12-06 16:46:13 -08001497 for_each_rmap_spte(rmap_head, &iter, sptep)
1498 if (is_accessed_spte(*sptep))
1499 return 1;
Junaid Shahid83ef6c82016-12-06 16:46:13 -08001500 return 0;
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001501}
1502
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001503#define RMAP_RECYCLE_THRESHOLD 1000
1504
Joerg Roedel852e3c12009-07-27 16:30:44 +02001505static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001506{
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001507 struct kvm_rmap_head *rmap_head;
Joerg Roedel852e3c12009-07-27 16:30:44 +02001508 struct kvm_mmu_page *sp;
1509
Sean Christopherson57354682020-06-22 13:20:33 -07001510 sp = sptep_to_sp(spte);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001511
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09001512 rmap_head = gfn_to_rmap(vcpu->kvm, gfn, sp);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001513
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001514 kvm_unmap_rmapp(vcpu->kvm, rmap_head, NULL, gfn, sp->role.level, __pte(0));
Lan Tianyuc3134ce2018-12-06 21:21:09 +08001515 kvm_flush_remote_tlbs_with_address(vcpu->kvm, sp->gfn,
1516 KVM_PAGES_PER_HPAGE(sp->role.level));
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001517}
1518
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001519bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001520{
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001521 bool young;
Ben Gardonf8e14492020-10-14 11:26:53 -07001522
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001523 young = kvm_handle_gfn_range(kvm, range, kvm_age_rmapp);
1524
Paolo Bonzini897218f2021-02-06 09:53:33 -05001525 if (is_tdp_mmu_enabled(kvm))
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001526 young |= kvm_tdp_mmu_age_gfn_range(kvm, range);
Ben Gardonf8e14492020-10-14 11:26:53 -07001527
1528 return young;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001529}
1530
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001531bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001532{
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001533 bool young;
Ben Gardonf8e14492020-10-14 11:26:53 -07001534
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001535 young = kvm_handle_gfn_range(kvm, range, kvm_test_age_rmapp);
1536
Paolo Bonzini897218f2021-02-06 09:53:33 -05001537 if (is_tdp_mmu_enabled(kvm))
Sean Christopherson3039bcc2021-04-01 17:56:50 -07001538 young |= kvm_tdp_mmu_test_age_gfn(kvm, range);
Ben Gardonf8e14492020-10-14 11:26:53 -07001539
1540 return young;
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001541}
1542
Yaozu Dongd6c69ee2007-04-25 14:17:25 +08001543#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +03001544static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001545{
Avi Kivity139bdb22007-01-05 16:36:50 -08001546 u64 *pos;
1547 u64 *end;
1548
Avi Kivity47ad8e62007-05-06 15:50:58 +03001549 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +03001550 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001551 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -08001552 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001553 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -08001554 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001555 return 1;
1556}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +08001557#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001558
Dave Hansen45221ab2010-08-19 18:11:37 -07001559/*
1560 * This value is the sum of all of the kvm instances's
1561 * kvm->arch.n_used_mmu_pages values. We need a global,
1562 * aggregate version in order to make the slab shrinker
1563 * faster
1564 */
Ben Gardonbc8a3d82019-04-08 11:07:30 -07001565static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, unsigned long nr)
Dave Hansen45221ab2010-08-19 18:11:37 -07001566{
1567 kvm->arch.n_used_mmu_pages += nr;
1568 percpu_counter_add(&kvm_total_used_mmu_pages, nr);
1569}
1570
Gleb Natapov834be0d2013-01-30 16:45:05 +02001571static void kvm_mmu_free_page(struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -08001572{
Paolo Bonzinifa4a2c02013-10-02 16:56:16 +02001573 MMU_WARN_ON(!is_empty_shadow_page(sp->spt));
Xiao Guangrong77758342010-06-04 21:53:54 +08001574 hlist_del(&sp->hash_link);
Xiao Guangrongbd4c86e2011-07-12 03:27:14 +08001575 list_del(&sp->link);
1576 free_page((unsigned long)sp->spt);
Gleb Natapov834be0d2013-01-30 16:45:05 +02001577 if (!sp->role.direct)
1578 free_page((unsigned long)sp->gfns);
Xiao Guangronge8ad9a72010-05-13 10:06:02 +08001579 kmem_cache_free(mmu_page_header_cache, sp);
Avi Kivity260746c2007-01-05 16:36:49 -08001580}
1581
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001582static unsigned kvm_page_table_hashfn(gfn_t gfn)
1583{
David Matlack114df302016-12-19 13:58:25 -08001584 return hash_64(gfn, KVM_MMU_HASH_SHIFT);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001585}
1586
Xiao Guangrong67052b32011-05-15 23:27:08 +08001587static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
1588 struct kvm_mmu_page *sp, u64 *parent_pte)
1589{
1590 if (!parent_pte)
1591 return;
1592
1593 pte_list_add(vcpu, parent_pte, &sp->parent_ptes);
1594}
1595
1596static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
1597 u64 *parent_pte)
1598{
Wei Yang8daf3462018-10-04 10:04:22 +08001599 __pte_list_remove(parent_pte, &sp->parent_ptes);
Xiao Guangrong67052b32011-05-15 23:27:08 +08001600}
1601
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001602static void drop_parent_pte(struct kvm_mmu_page *sp,
1603 u64 *parent_pte)
1604{
1605 mmu_page_remove_parent_pte(sp, parent_pte);
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08001606 mmu_spte_clear_no_track(parent_pte);
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08001607}
1608
Takuya Yoshikawa47005792015-11-20 17:46:29 +09001609static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, int direct)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001610{
Avi Kivity4db35312007-11-21 15:28:32 +02001611 struct kvm_mmu_page *sp;
Takuya Yoshikawa7ddca7e2013-03-21 19:33:43 +09001612
Sean Christopherson94ce87e2020-07-02 19:35:37 -07001613 sp = kvm_mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache);
1614 sp->spt = kvm_mmu_memory_cache_alloc(&vcpu->arch.mmu_shadow_page_cache);
Lai Jiangshan2032a932010-05-26 16:49:59 +08001615 if (!direct)
Sean Christopherson94ce87e2020-07-02 19:35:37 -07001616 sp->gfns = kvm_mmu_memory_cache_alloc(&vcpu->arch.mmu_gfn_array_cache);
Avi Kivity4db35312007-11-21 15:28:32 +02001617 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Sean Christopherson002c5f72019-09-12 19:46:02 -07001618
1619 /*
1620 * active_mmu_pages must be a FIFO list, as kvm_zap_obsolete_pages()
1621 * depends on valid pages being added to the head of the list. See
1622 * comments in kvm_zap_obsolete_pages().
1623 */
Sean Christophersonca333ad2019-09-12 19:46:11 -07001624 sp->mmu_valid_gen = vcpu->kvm->arch.mmu_valid_gen;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001625 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Dave Hansen45221ab2010-08-19 18:11:37 -07001626 kvm_mod_used_mmu_pages(vcpu->kvm, +1);
Avi Kivity4db35312007-11-21 15:28:32 +02001627 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001628}
1629
Xiao Guangrong67052b32011-05-15 23:27:08 +08001630static void mark_unsync(u64 *spte);
Xiao Guangrong6b184932010-04-16 21:29:17 +08001631static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001632{
Takuya Yoshikawa74c4e632015-11-26 21:15:38 +09001633 u64 *sptep;
1634 struct rmap_iterator iter;
1635
1636 for_each_rmap_spte(&sp->parent_ptes, &iter, sptep) {
1637 mark_unsync(sptep);
1638 }
Xiao Guangrong1047df12010-06-11 21:35:15 +08001639}
1640
Xiao Guangrong67052b32011-05-15 23:27:08 +08001641static void mark_unsync(u64 *spte)
Xiao Guangrong1047df12010-06-11 21:35:15 +08001642{
Xiao Guangrong67052b32011-05-15 23:27:08 +08001643 struct kvm_mmu_page *sp;
Xiao Guangrong1047df12010-06-11 21:35:15 +08001644 unsigned int index;
1645
Sean Christopherson57354682020-06-22 13:20:33 -07001646 sp = sptep_to_sp(spte);
Xiao Guangrong1047df12010-06-11 21:35:15 +08001647 index = spte - sp->spt;
1648 if (__test_and_set_bit(index, sp->unsync_child_bitmap))
1649 return;
1650 if (sp->unsync_children++)
1651 return;
1652 kvm_mmu_mark_parents_unsync(sp);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001653}
1654
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001655static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
Xiao Guangronga4a8e6f2010-11-19 17:04:03 +08001656 struct kvm_mmu_page *sp)
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001657{
Paolo Bonzini1f50f1b2016-02-24 11:07:14 +01001658 return 0;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001659}
1660
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001661#define KVM_PAGE_ARRAY_NR 16
1662
1663struct kvm_mmu_pages {
1664 struct mmu_page_and_offset {
1665 struct kvm_mmu_page *sp;
1666 unsigned int idx;
1667 } page[KVM_PAGE_ARRAY_NR];
1668 unsigned int nr;
1669};
1670
Hannes Edercded19f2009-02-21 02:19:13 +01001671static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1672 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001673{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001674 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001675
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001676 if (sp->unsync)
1677 for (i=0; i < pvec->nr; i++)
1678 if (pvec->page[i].sp == sp)
1679 return 0;
1680
1681 pvec->page[pvec->nr].sp = sp;
1682 pvec->page[pvec->nr].idx = idx;
1683 pvec->nr++;
1684 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1685}
1686
Takuya Yoshikawafd951452015-11-20 17:43:13 +09001687static inline void clear_unsync_child_bit(struct kvm_mmu_page *sp, int idx)
1688{
1689 --sp->unsync_children;
1690 WARN_ON((int)sp->unsync_children < 0);
1691 __clear_bit(idx, sp->unsync_child_bitmap);
1692}
1693
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001694static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1695 struct kvm_mmu_pages *pvec)
1696{
1697 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001698
Takuya Yoshikawa37178b82011-11-29 14:02:45 +09001699 for_each_set_bit(i, sp->unsync_child_bitmap, 512) {
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001700 struct kvm_mmu_page *child;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001701 u64 ent = sp->spt[i];
1702
Takuya Yoshikawafd951452015-11-20 17:43:13 +09001703 if (!is_shadow_present_pte(ent) || is_large_pte(ent)) {
1704 clear_unsync_child_bit(sp, i);
1705 continue;
1706 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001707
Sean Christophersone47c4ae2020-06-22 13:20:34 -07001708 child = to_shadow_page(ent & PT64_BASE_ADDR_MASK);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001709
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001710 if (child->unsync_children) {
1711 if (mmu_pages_add(pvec, child, i))
1712 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001713
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001714 ret = __mmu_unsync_walk(child, pvec);
Takuya Yoshikawafd951452015-11-20 17:43:13 +09001715 if (!ret) {
1716 clear_unsync_child_bit(sp, i);
1717 continue;
1718 } else if (ret > 0) {
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001719 nr_unsync_leaf += ret;
Takuya Yoshikawafd951452015-11-20 17:43:13 +09001720 } else
Xiao Guangrong7a8f1a72010-06-11 21:34:04 +08001721 return ret;
1722 } else if (child->unsync) {
1723 nr_unsync_leaf++;
1724 if (mmu_pages_add(pvec, child, i))
1725 return -ENOSPC;
1726 } else
Takuya Yoshikawafd951452015-11-20 17:43:13 +09001727 clear_unsync_child_bit(sp, i);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001728 }
1729
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001730 return nr_unsync_leaf;
1731}
1732
Xiao Guangronge23d3fe2016-02-24 09:46:06 +01001733#define INVALID_INDEX (-1)
1734
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001735static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1736 struct kvm_mmu_pages *pvec)
1737{
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01001738 pvec->nr = 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001739 if (!sp->unsync_children)
1740 return 0;
1741
Xiao Guangronge23d3fe2016-02-24 09:46:06 +01001742 mmu_pages_add(pvec, sp, INVALID_INDEX);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001743 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001744}
1745
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001746static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1747{
1748 WARN_ON(!sp->unsync);
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08001749 trace_kvm_mmu_sync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001750 sp->unsync = 0;
1751 --kvm->stat.mmu_unsync;
1752}
1753
Sean Christopherson83cdb562019-02-05 13:01:35 -08001754static bool kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
1755 struct list_head *invalid_list);
Xiao Guangrong77758342010-06-04 21:53:54 +08001756static void kvm_mmu_commit_zap_page(struct kvm *kvm,
1757 struct list_head *invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001758
Sean Christophersonac101b72020-06-23 12:40:26 -07001759#define for_each_valid_sp(_kvm, _sp, _list) \
1760 hlist_for_each_entry(_sp, _list, hash_link) \
Sean Christophersonfac026d2019-09-12 19:46:03 -07001761 if (is_obsolete_sp((_kvm), (_sp))) { \
David Matlackf3414bc2016-12-20 15:25:57 -08001762 } else
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001763
Takuya Yoshikawa1044b032013-03-06 16:05:07 +09001764#define for_each_gfn_indirect_valid_sp(_kvm, _sp, _gfn) \
Sean Christophersonac101b72020-06-23 12:40:26 -07001765 for_each_valid_sp(_kvm, _sp, \
1766 &(_kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(_gfn)]) \
David Matlackf3414bc2016-12-20 15:25:57 -08001767 if ((_sp)->gfn != (_gfn) || (_sp)->role.direct) {} else
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001768
Sean Christopherson47c42e62019-03-07 15:27:44 -08001769static inline bool is_ept_sp(struct kvm_mmu_page *sp)
1770{
1771 return sp->role.cr0_wp && sp->role.smap_andnot_wp;
1772}
1773
Xiao Guangrongf918b442010-06-11 21:30:36 +08001774/* @sp->gfn should be write-protected at the call site */
Paolo Bonzini1f50f1b2016-02-24 11:07:14 +01001775static bool __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
1776 struct list_head *invalid_list)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001777{
Sean Christopherson47c42e62019-03-07 15:27:44 -08001778 if ((!is_ept_sp(sp) && sp->role.gpte_is_8_bytes != !!is_pae(vcpu)) ||
1779 vcpu->arch.mmu->sync_page(vcpu, sp) == 0) {
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001780 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
Paolo Bonzini1f50f1b2016-02-24 11:07:14 +01001781 return false;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001782 }
1783
Paolo Bonzini1f50f1b2016-02-24 11:07:14 +01001784 return true;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001785}
1786
Sean Christophersona2113632019-02-05 13:01:20 -08001787static bool kvm_mmu_remote_flush_or_zap(struct kvm *kvm,
1788 struct list_head *invalid_list,
1789 bool remote_flush)
1790{
Sean Christophersoncfd32ac2019-04-12 19:55:41 -07001791 if (!remote_flush && list_empty(invalid_list))
Sean Christophersona2113632019-02-05 13:01:20 -08001792 return false;
1793
1794 if (!list_empty(invalid_list))
1795 kvm_mmu_commit_zap_page(kvm, invalid_list);
1796 else
1797 kvm_flush_remote_tlbs(kvm);
1798 return true;
1799}
1800
Paolo Bonzini35a70512016-02-24 10:03:27 +01001801static void kvm_mmu_flush_or_zap(struct kvm_vcpu *vcpu,
1802 struct list_head *invalid_list,
1803 bool remote_flush, bool local_flush)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001804{
Sean Christophersona2113632019-02-05 13:01:20 -08001805 if (kvm_mmu_remote_flush_or_zap(vcpu->kvm, invalid_list, remote_flush))
Paolo Bonzini35a70512016-02-24 10:03:27 +01001806 return;
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001807
Sean Christophersona2113632019-02-05 13:01:20 -08001808 if (local_flush)
Sean Christopherson8c8560b2020-03-20 14:28:21 -07001809 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001810}
1811
Xiao Guangronge37fa782011-11-30 17:43:24 +08001812#ifdef CONFIG_KVM_MMU_AUDIT
1813#include "mmu_audit.c"
1814#else
1815static void kvm_mmu_audit(struct kvm_vcpu *vcpu, int point) { }
1816static void mmu_audit_disable(void) { }
1817#endif
1818
Sean Christopherson002c5f72019-09-12 19:46:02 -07001819static bool is_obsolete_sp(struct kvm *kvm, struct kvm_mmu_page *sp)
1820{
Sean Christophersonfac026d2019-09-12 19:46:03 -07001821 return sp->role.invalid ||
1822 unlikely(sp->mmu_valid_gen != kvm->arch.mmu_valid_gen);
Sean Christopherson002c5f72019-09-12 19:46:02 -07001823}
1824
Paolo Bonzini1f50f1b2016-02-24 11:07:14 +01001825static bool kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001826 struct list_head *invalid_list)
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001827{
Paolo Bonzini9a43c5d2016-02-24 10:28:01 +01001828 kvm_unlink_unsync_page(vcpu->kvm, sp);
1829 return __kvm_sync_page(vcpu, sp, invalid_list);
Xiao Guangrong1d9dc7e2010-05-15 18:51:24 +08001830}
1831
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001832/* @gfn should be write-protected at the call site */
Paolo Bonzini2a74003a2016-02-24 11:26:10 +01001833static bool kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn,
1834 struct list_head *invalid_list)
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001835{
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001836 struct kvm_mmu_page *s;
Paolo Bonzini2a74003a2016-02-24 11:26:10 +01001837 bool ret = false;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001838
Sasha Levinb67bfe02013-02-27 17:06:00 -08001839 for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn) {
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08001840 if (!s->unsync)
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001841 continue;
1842
Sean Christopherson3bae0452020-04-27 17:54:22 -07001843 WARN_ON(s->role.level != PG_LEVEL_4K);
Paolo Bonzini2a74003a2016-02-24 11:26:10 +01001844 ret |= kvm_sync_page(vcpu, s, invalid_list);
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001845 }
1846
Paolo Bonzini2a74003a2016-02-24 11:26:10 +01001847 return ret;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001848}
1849
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001850struct mmu_page_path {
Yu Zhang2a7266a2017-08-24 20:27:54 +08001851 struct kvm_mmu_page *parent[PT64_ROOT_MAX_LEVEL];
1852 unsigned int idx[PT64_ROOT_MAX_LEVEL];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001853};
1854
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001855#define for_each_sp(pvec, sp, parents, i) \
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01001856 for (i = mmu_pages_first(&pvec, &parents); \
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001857 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1858 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001859
Hannes Edercded19f2009-02-21 02:19:13 +01001860static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1861 struct mmu_page_path *parents,
1862 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001863{
1864 int n;
1865
1866 for (n = i+1; n < pvec->nr; n++) {
1867 struct kvm_mmu_page *sp = pvec->page[n].sp;
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01001868 unsigned idx = pvec->page[n].idx;
1869 int level = sp->role.level;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001870
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01001871 parents->idx[level-1] = idx;
Sean Christopherson3bae0452020-04-27 17:54:22 -07001872 if (level == PG_LEVEL_4K)
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01001873 break;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001874
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01001875 parents->parent[level-2] = sp;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001876 }
1877
1878 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001879}
1880
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01001881static int mmu_pages_first(struct kvm_mmu_pages *pvec,
1882 struct mmu_page_path *parents)
1883{
1884 struct kvm_mmu_page *sp;
1885 int level;
1886
1887 if (pvec->nr == 0)
1888 return 0;
1889
Xiao Guangronge23d3fe2016-02-24 09:46:06 +01001890 WARN_ON(pvec->page[0].idx != INVALID_INDEX);
1891
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01001892 sp = pvec->page[0].sp;
1893 level = sp->role.level;
Sean Christopherson3bae0452020-04-27 17:54:22 -07001894 WARN_ON(level == PG_LEVEL_4K);
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01001895
1896 parents->parent[level-2] = sp;
1897
1898 /* Also set up a sentinel. Further entries in pvec are all
1899 * children of sp, so this element is never overwritten.
1900 */
1901 parents->parent[level-1] = NULL;
1902 return mmu_pages_next(pvec, parents, 0);
1903}
1904
Hannes Edercded19f2009-02-21 02:19:13 +01001905static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001906{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001907 struct kvm_mmu_page *sp;
1908 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001909
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001910 do {
1911 unsigned int idx = parents->idx[level];
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001912 sp = parents->parent[level];
1913 if (!sp)
1914 return;
1915
Xiao Guangronge23d3fe2016-02-24 09:46:06 +01001916 WARN_ON(idx == INVALID_INDEX);
Takuya Yoshikawafd951452015-11-20 17:43:13 +09001917 clear_unsync_child_bit(sp, idx);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001918 level++;
Paolo Bonzini0a47cd82016-02-23 13:54:25 +01001919 } while (!sp->unsync_children);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001920}
1921
1922static void mmu_sync_children(struct kvm_vcpu *vcpu,
1923 struct kvm_mmu_page *parent)
1924{
1925 int i;
1926 struct kvm_mmu_page *sp;
1927 struct mmu_page_path parents;
1928 struct kvm_mmu_pages pages;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08001929 LIST_HEAD(invalid_list);
Paolo Bonzini50c9e6f2016-02-25 10:47:38 +01001930 bool flush = false;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001931
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001932 while (mmu_unsync_walk(parent, &pages)) {
Xiao Guangrong2f845692012-06-20 15:56:53 +08001933 bool protected = false;
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001934
1935 for_each_sp(pages, sp, parents, i)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02001936 protected |= rmap_write_protect(vcpu, sp->gfn);
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001937
Paolo Bonzini50c9e6f2016-02-25 10:47:38 +01001938 if (protected) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001939 kvm_flush_remote_tlbs(vcpu->kvm);
Paolo Bonzini50c9e6f2016-02-25 10:47:38 +01001940 flush = false;
1941 }
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001942
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001943 for_each_sp(pages, sp, parents, i) {
Paolo Bonzini1f50f1b2016-02-24 11:07:14 +01001944 flush |= kvm_sync_page(vcpu, sp, &invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001945 mmu_pages_clear_parents(&parents);
1946 }
Ben Gardon531810c2021-02-02 10:57:24 -08001947 if (need_resched() || rwlock_needbreak(&vcpu->kvm->mmu_lock)) {
Paolo Bonzini50c9e6f2016-02-25 10:47:38 +01001948 kvm_mmu_flush_or_zap(vcpu, &invalid_list, false, flush);
Ben Gardon531810c2021-02-02 10:57:24 -08001949 cond_resched_rwlock_write(&vcpu->kvm->mmu_lock);
Paolo Bonzini50c9e6f2016-02-25 10:47:38 +01001950 flush = false;
1951 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001952 }
Paolo Bonzini50c9e6f2016-02-25 10:47:38 +01001953
1954 kvm_mmu_flush_or_zap(vcpu, &invalid_list, false, flush);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001955}
1956
Xiao Guangronga30f47c2011-09-22 16:58:36 +08001957static void __clear_sp_write_flooding_count(struct kvm_mmu_page *sp)
1958{
Xiao Guangronge5691a82016-02-24 17:51:12 +08001959 atomic_set(&sp->write_flooding_count, 0);
Xiao Guangronga30f47c2011-09-22 16:58:36 +08001960}
1961
1962static void clear_sp_write_flooding_count(u64 *spte)
1963{
Sean Christopherson57354682020-06-22 13:20:33 -07001964 __clear_sp_write_flooding_count(sptep_to_sp(spte));
Xiao Guangronga30f47c2011-09-22 16:58:36 +08001965}
1966
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001967static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1968 gfn_t gfn,
1969 gva_t gaddr,
1970 unsigned level,
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001971 int direct,
Ben Gardon0a2b64c2020-02-03 15:09:09 -08001972 unsigned int access)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001973{
Sean Christophersonfb58a9c2020-06-23 12:40:27 -07001974 bool direct_mmu = vcpu->arch.mmu->direct_map;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001975 union kvm_mmu_page_role role;
Sean Christophersonac101b72020-06-23 12:40:26 -07001976 struct hlist_head *sp_list;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001977 unsigned quadrant;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001978 struct kvm_mmu_page *sp;
Xiao Guangrong9f1a1222010-05-24 15:41:33 +08001979 bool need_sync = false;
Paolo Bonzini2a74003a2016-02-24 11:26:10 +01001980 bool flush = false;
David Matlackf3414bc2016-12-20 15:25:57 -08001981 int collisions = 0;
Paolo Bonzini2a74003a2016-02-24 11:26:10 +01001982 LIST_HEAD(invalid_list);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001983
Vitaly Kuznetsov36d9594d2018-10-08 21:28:10 +02001984 role = vcpu->arch.mmu->mmu_role.base;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001985 role.level = level;
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02001986 role.direct = direct;
Avi Kivity84b0c8c2010-03-14 10:16:40 +02001987 if (role.direct)
Sean Christopherson47c42e62019-03-07 15:27:44 -08001988 role.gpte_is_8_bytes = true;
Avi Kivity41074d02007-12-09 17:00:02 +02001989 role.access = access;
Sean Christophersonfb58a9c2020-06-23 12:40:27 -07001990 if (!direct_mmu && vcpu->arch.mmu->root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001991 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1992 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1993 role.quadrant = quadrant;
1994 }
Sean Christophersonac101b72020-06-23 12:40:26 -07001995
1996 sp_list = &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)];
1997 for_each_valid_sp(vcpu->kvm, sp, sp_list) {
David Matlackf3414bc2016-12-20 15:25:57 -08001998 if (sp->gfn != gfn) {
1999 collisions++;
2000 continue;
2001 }
2002
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002003 if (!need_sync && sp->unsync)
2004 need_sync = true;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002005
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002006 if (sp->role.word != role.word)
2007 continue;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002008
Sean Christophersonfb58a9c2020-06-23 12:40:27 -07002009 if (direct_mmu)
2010 goto trace_get_page;
2011
Paolo Bonzini2a74003a2016-02-24 11:26:10 +01002012 if (sp->unsync) {
2013 /* The page is good, but __kvm_sync_page might still end
2014 * up zapping it. If so, break in order to rebuild it.
2015 */
2016 if (!__kvm_sync_page(vcpu, sp, &invalid_list))
2017 break;
2018
2019 WARN_ON(!list_empty(&invalid_list));
Sean Christopherson8c8560b2020-03-20 14:28:21 -07002020 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Paolo Bonzini2a74003a2016-02-24 11:26:10 +01002021 }
Xiao Guangronge02aa902010-05-15 18:52:34 +08002022
Takuya Yoshikawa98bba232015-11-26 21:14:34 +09002023 if (sp->unsync_children)
Lai Jiangshanf6f61952020-09-02 21:54:21 +08002024 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
Xiao Guangronge02aa902010-05-15 18:52:34 +08002025
Xiao Guangronga30f47c2011-09-22 16:58:36 +08002026 __clear_sp_write_flooding_count(sp);
Sean Christophersonfb58a9c2020-06-23 12:40:27 -07002027
2028trace_get_page:
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002029 trace_kvm_mmu_get_page(sp, false);
David Matlackf3414bc2016-12-20 15:25:57 -08002030 goto out;
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002031 }
Takuya Yoshikawa47005792015-11-20 17:46:29 +09002032
Avi Kivitydfc5aa02007-12-18 19:47:18 +02002033 ++vcpu->kvm->stat.mmu_cache_miss;
Takuya Yoshikawa47005792015-11-20 17:46:29 +09002034
2035 sp = kvm_mmu_alloc_page(vcpu, direct);
2036
Avi Kivity4db35312007-11-21 15:28:32 +02002037 sp->gfn = gfn;
2038 sp->role = role;
Sean Christophersonac101b72020-06-23 12:40:26 -07002039 hlist_add_head(&sp->hash_link, sp_list);
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002040 if (!direct) {
Xiao Guangrong56ca57f2016-02-24 17:51:14 +08002041 /*
2042 * we should do write protection before syncing pages
2043 * otherwise the content of the synced shadow page may
2044 * be inconsistent with guest page table.
2045 */
Paolo Bonzini3ed1a472015-05-19 16:29:22 +02002046 account_shadowed(vcpu->kvm, sp);
Sean Christopherson3bae0452020-04-27 17:54:22 -07002047 if (level == PG_LEVEL_4K && rmap_write_protect(vcpu, gfn))
Lan Tianyuc3134ce2018-12-06 21:21:09 +08002048 kvm_flush_remote_tlbs_with_address(vcpu->kvm, gfn, 1);
Xiao Guangrong56ca57f2016-02-24 17:51:14 +08002049
Sean Christopherson3bae0452020-04-27 17:54:22 -07002050 if (level > PG_LEVEL_4K && need_sync)
Paolo Bonzini2a74003a2016-02-24 11:26:10 +01002051 flush |= kvm_sync_pages(vcpu, gfn, &invalid_list);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002052 }
Avi Kivityf691fe12009-07-06 15:58:14 +03002053 trace_kvm_mmu_get_page(sp, true);
Paolo Bonzini2a74003a2016-02-24 11:26:10 +01002054
2055 kvm_mmu_flush_or_zap(vcpu, &invalid_list, false, flush);
David Matlackf3414bc2016-12-20 15:25:57 -08002056out:
2057 if (collisions > vcpu->kvm->stat.max_mmu_page_hash_collisions)
2058 vcpu->kvm->stat.max_mmu_page_hash_collisions = collisions;
Avi Kivity4db35312007-11-21 15:28:32 +02002059 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002060}
2061
Junaid Shahid7eb77e92018-06-27 14:59:16 -07002062static void shadow_walk_init_using_root(struct kvm_shadow_walk_iterator *iterator,
2063 struct kvm_vcpu *vcpu, hpa_t root,
2064 u64 addr)
Avi Kivity2d111232008-12-25 14:39:47 +02002065{
2066 iterator->addr = addr;
Junaid Shahid7eb77e92018-06-27 14:59:16 -07002067 iterator->shadow_addr = root;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02002068 iterator->level = vcpu->arch.mmu->shadow_root_level;
Joerg Roedel81407ca2010-09-10 17:31:00 +02002069
Yu Zhang2a7266a2017-08-24 20:27:54 +08002070 if (iterator->level == PT64_ROOT_4LEVEL &&
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02002071 vcpu->arch.mmu->root_level < PT64_ROOT_4LEVEL &&
2072 !vcpu->arch.mmu->direct_map)
Joerg Roedel81407ca2010-09-10 17:31:00 +02002073 --iterator->level;
2074
Avi Kivity2d111232008-12-25 14:39:47 +02002075 if (iterator->level == PT32E_ROOT_LEVEL) {
Junaid Shahid7eb77e92018-06-27 14:59:16 -07002076 /*
2077 * prev_root is currently only used for 64-bit hosts. So only
2078 * the active root_hpa is valid here.
2079 */
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02002080 BUG_ON(root != vcpu->arch.mmu->root_hpa);
Junaid Shahid7eb77e92018-06-27 14:59:16 -07002081
Avi Kivity2d111232008-12-25 14:39:47 +02002082 iterator->shadow_addr
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02002083 = vcpu->arch.mmu->pae_root[(addr >> 30) & 3];
Avi Kivity2d111232008-12-25 14:39:47 +02002084 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
2085 --iterator->level;
2086 if (!iterator->shadow_addr)
2087 iterator->level = 0;
2088 }
2089}
2090
Junaid Shahid7eb77e92018-06-27 14:59:16 -07002091static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
2092 struct kvm_vcpu *vcpu, u64 addr)
2093{
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02002094 shadow_walk_init_using_root(iterator, vcpu, vcpu->arch.mmu->root_hpa,
Junaid Shahid7eb77e92018-06-27 14:59:16 -07002095 addr);
2096}
2097
Avi Kivity2d111232008-12-25 14:39:47 +02002098static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
2099{
Sean Christopherson3bae0452020-04-27 17:54:22 -07002100 if (iterator->level < PG_LEVEL_4K)
Avi Kivity2d111232008-12-25 14:39:47 +02002101 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03002102
Avi Kivity2d111232008-12-25 14:39:47 +02002103 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
2104 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
2105 return true;
2106}
2107
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08002108static void __shadow_walk_next(struct kvm_shadow_walk_iterator *iterator,
2109 u64 spte)
Avi Kivity2d111232008-12-25 14:39:47 +02002110{
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08002111 if (is_last_spte(spte, iterator->level)) {
Xiao Guangrong052331b2011-07-12 03:21:17 +08002112 iterator->level = 0;
2113 return;
2114 }
2115
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08002116 iterator->shadow_addr = spte & PT64_BASE_ADDR_MASK;
Avi Kivity2d111232008-12-25 14:39:47 +02002117 --iterator->level;
2118}
2119
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08002120static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
2121{
David Hildenbrandbb606a92017-08-24 20:51:23 +02002122 __shadow_walk_next(iterator, *iterator->sptep);
Xiao Guangrongc2a2ac22011-07-12 03:32:13 +08002123}
2124
Takuya Yoshikawa98bba232015-11-26 21:14:34 +09002125static void link_shadow_page(struct kvm_vcpu *vcpu, u64 *sptep,
2126 struct kvm_mmu_page *sp)
Avi Kivity32ef26a2010-07-13 14:27:04 +03002127{
2128 u64 spte;
2129
Bandan Dasffb128c2016-07-12 18:18:49 -04002130 BUILD_BUG_ON(VMX_EPT_WRITABLE_MASK != PT_WRITABLE_MASK);
Yang Zhang7a1638c2013-08-05 11:07:13 +03002131
Ben Gardoncc4674d2020-09-25 14:22:48 -07002132 spte = make_nonleaf_spte(sp->spt, sp_ad_disabled(sp));
Xiao Guangrong24db2732013-02-05 15:28:02 +08002133
Xiao Guangrong1df9f2d2011-07-12 03:30:35 +08002134 mmu_spte_set(sptep, spte);
Takuya Yoshikawa98bba232015-11-26 21:14:34 +09002135
2136 mmu_page_add_parent_pte(vcpu, sp, sptep);
2137
2138 if (sp->unsync_children || sp->unsync)
2139 mark_unsync(sptep);
Avi Kivity32ef26a2010-07-13 14:27:04 +03002140}
2141
Avi Kivitya357bd22010-07-13 14:27:07 +03002142static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
2143 unsigned direct_access)
2144{
2145 if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
2146 struct kvm_mmu_page *child;
2147
2148 /*
2149 * For the direct sp, if the guest pte's dirty bit
2150 * changed form clean to dirty, it will corrupt the
2151 * sp's access: allow writable in the read-only sp,
2152 * so we should update the spte at this point to get
2153 * a new sp with the correct access.
2154 */
Sean Christophersone47c4ae2020-06-22 13:20:34 -07002155 child = to_shadow_page(*sptep & PT64_BASE_ADDR_MASK);
Avi Kivitya357bd22010-07-13 14:27:07 +03002156 if (child->role.access == direct_access)
2157 return;
2158
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08002159 drop_parent_pte(child, sptep);
Lan Tianyuc3134ce2018-12-06 21:21:09 +08002160 kvm_flush_remote_tlbs_with_address(vcpu->kvm, child->gfn, 1);
Avi Kivitya357bd22010-07-13 14:27:07 +03002161 }
2162}
2163
Ben Gardon2de40852020-09-23 15:14:06 -07002164/* Returns the number of zapped non-leaf child shadow pages. */
2165static int mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,
2166 u64 *spte, struct list_head *invalid_list)
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08002167{
2168 u64 pte;
2169 struct kvm_mmu_page *child;
2170
2171 pte = *spte;
2172 if (is_shadow_present_pte(pte)) {
Xiao Guangrong505aef82011-09-22 16:56:06 +08002173 if (is_last_spte(pte, sp->role.level)) {
Xiao Guangrongc3707952011-07-12 03:28:04 +08002174 drop_spte(kvm, spte);
Xiao Guangrong505aef82011-09-22 16:56:06 +08002175 if (is_large_pte(pte))
2176 --kvm->stat.lpages;
2177 } else {
Sean Christophersone47c4ae2020-06-22 13:20:34 -07002178 child = to_shadow_page(pte & PT64_BASE_ADDR_MASK);
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08002179 drop_parent_pte(child, spte);
Ben Gardon2de40852020-09-23 15:14:06 -07002180
2181 /*
2182 * Recursively zap nested TDP SPs, parentless SPs are
2183 * unlikely to be used again in the near future. This
2184 * avoids retaining a large number of stale nested SPs.
2185 */
2186 if (tdp_enabled && invalid_list &&
2187 child->role.guest_mode && !child->parent_ptes.val)
2188 return kvm_mmu_prepare_zap_page(kvm, child,
2189 invalid_list);
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08002190 }
Sean Christophersonace569e2020-09-23 15:14:05 -07002191 } else if (is_mmio_spte(pte)) {
Xiao Guangrongce88dec2011-07-12 03:33:44 +08002192 mmu_spte_clear_no_track(spte);
Sean Christophersonace569e2020-09-23 15:14:05 -07002193 }
Ben Gardon2de40852020-09-23 15:14:06 -07002194 return 0;
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08002195}
2196
Ben Gardon2de40852020-09-23 15:14:06 -07002197static int kvm_mmu_page_unlink_children(struct kvm *kvm,
2198 struct kvm_mmu_page *sp,
2199 struct list_head *invalid_list)
Avi Kivitya4360362007-01-05 16:36:45 -08002200{
Ben Gardon2de40852020-09-23 15:14:06 -07002201 int zapped = 0;
Avi Kivity697fe2e2007-01-05 16:36:46 -08002202 unsigned i;
Avi Kivity697fe2e2007-01-05 16:36:46 -08002203
Xiao Guangrong38e3b2b2011-05-15 23:27:52 +08002204 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
Ben Gardon2de40852020-09-23 15:14:06 -07002205 zapped += mmu_page_zap_pte(kvm, sp, sp->spt + i, invalid_list);
2206
2207 return zapped;
Avi Kivitya4360362007-01-05 16:36:45 -08002208}
2209
Avi Kivity31aa2b42008-07-11 17:59:46 +03002210static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08002211{
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09002212 u64 *sptep;
2213 struct rmap_iterator iter;
Avi Kivitya4360362007-01-05 16:36:45 -08002214
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09002215 while ((sptep = rmap_get_first(&sp->parent_ptes, &iter)))
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +09002216 drop_parent_pte(sp, sptep);
Avi Kivity31aa2b42008-07-11 17:59:46 +03002217}
2218
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002219static int mmu_zap_unsync_children(struct kvm *kvm,
Xiao Guangrong77758342010-06-04 21:53:54 +08002220 struct kvm_mmu_page *parent,
2221 struct list_head *invalid_list)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002222{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002223 int i, zapped = 0;
2224 struct mmu_page_path parents;
2225 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002226
Sean Christopherson3bae0452020-04-27 17:54:22 -07002227 if (parent->role.level == PG_LEVEL_4K)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002228 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002229
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002230 while (mmu_unsync_walk(parent, &pages)) {
2231 struct kvm_mmu_page *sp;
2232
2233 for_each_sp(pages, sp, parents, i) {
Xiao Guangrong77758342010-06-04 21:53:54 +08002234 kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002235 mmu_pages_clear_parents(&parents);
Xiao Guangrong77662e02010-04-16 16:34:42 +08002236 zapped++;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002237 }
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02002238 }
2239
2240 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002241}
2242
Sean Christopherson83cdb562019-02-05 13:01:35 -08002243static bool __kvm_mmu_prepare_zap_page(struct kvm *kvm,
2244 struct kvm_mmu_page *sp,
2245 struct list_head *invalid_list,
2246 int *nr_zapped)
Avi Kivity31aa2b42008-07-11 17:59:46 +03002247{
Sean Christopherson83cdb562019-02-05 13:01:35 -08002248 bool list_unstable;
Avi Kivityf691fe12009-07-06 15:58:14 +03002249
Xiao Guangrong77758342010-06-04 21:53:54 +08002250 trace_kvm_mmu_prepare_zap_page(sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03002251 ++kvm->stat.mmu_shadow_zapped;
Sean Christopherson83cdb562019-02-05 13:01:35 -08002252 *nr_zapped = mmu_zap_unsync_children(kvm, sp, invalid_list);
Ben Gardon2de40852020-09-23 15:14:06 -07002253 *nr_zapped += kvm_mmu_page_unlink_children(kvm, sp, invalid_list);
Avi Kivity31aa2b42008-07-11 17:59:46 +03002254 kvm_mmu_unlink_parents(kvm, sp);
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08002255
Sean Christopherson83cdb562019-02-05 13:01:35 -08002256 /* Zapping children means active_mmu_pages has become unstable. */
2257 list_unstable = *nr_zapped;
2258
Avi Kivityf6e2c02b2009-01-11 13:02:10 +02002259 if (!sp->role.invalid && !sp->role.direct)
Paolo Bonzini3ed1a472015-05-19 16:29:22 +02002260 unaccount_shadowed(kvm, sp);
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08002261
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002262 if (sp->unsync)
2263 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02002264 if (!sp->root_count) {
Gui Jianfeng54a4f022010-05-05 09:03:49 +08002265 /* Count self */
Sean Christopherson83cdb562019-02-05 13:01:35 -08002266 (*nr_zapped)++;
Sean Christophersonf95eec92020-06-23 12:35:39 -07002267
2268 /*
2269 * Already invalid pages (previously active roots) are not on
2270 * the active page list. See list_del() in the "else" case of
2271 * !sp->root_count.
2272 */
2273 if (sp->role.invalid)
2274 list_add(&sp->link, invalid_list);
2275 else
2276 list_move(&sp->link, invalid_list);
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08002277 kvm_mod_used_mmu_pages(kvm, -1);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002278 } else {
Sean Christophersonf95eec92020-06-23 12:35:39 -07002279 /*
2280 * Remove the active root from the active page list, the root
2281 * will be explicitly freed when the root_count hits zero.
2282 */
2283 list_del(&sp->link);
Gleb Natapov05988d72013-05-31 08:36:30 +08002284
Sean Christopherson10605202019-09-12 19:46:10 -07002285 /*
2286 * Obsolete pages cannot be used on any vCPUs, see the comment
2287 * in kvm_mmu_zap_all_fast(). Note, is_obsolete_sp() also
2288 * treats invalid shadow pages as being obsolete.
2289 */
2290 if (!is_obsolete_sp(kvm, sp))
Gleb Natapov05988d72013-05-31 08:36:30 +08002291 kvm_reload_remote_mmus(kvm);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002292 }
Xiao Guangrong77758342010-06-04 21:53:54 +08002293
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01002294 if (sp->lpage_disallowed)
2295 unaccount_huge_nx_page(kvm, sp);
2296
Xiao Guangrong77758342010-06-04 21:53:54 +08002297 sp->role.invalid = 1;
Sean Christopherson83cdb562019-02-05 13:01:35 -08002298 return list_unstable;
2299}
2300
2301static bool kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
2302 struct list_head *invalid_list)
2303{
2304 int nr_zapped;
2305
2306 __kvm_mmu_prepare_zap_page(kvm, sp, invalid_list, &nr_zapped);
2307 return nr_zapped;
Avi Kivitya4360362007-01-05 16:36:45 -08002308}
2309
Xiao Guangrong77758342010-06-04 21:53:54 +08002310static void kvm_mmu_commit_zap_page(struct kvm *kvm,
2311 struct list_head *invalid_list)
2312{
Takuya Yoshikawa945315b2013-03-06 16:05:52 +09002313 struct kvm_mmu_page *sp, *nsp;
Xiao Guangrong77758342010-06-04 21:53:54 +08002314
2315 if (list_empty(invalid_list))
2316 return;
2317
Avi Kivityc1427862012-05-14 15:44:06 +03002318 /*
Lan Tianyu9753f522016-03-13 11:10:24 +08002319 * We need to make sure everyone sees our modifications to
2320 * the page tables and see changes to vcpu->mode here. The barrier
2321 * in the kvm_flush_remote_tlbs() achieves this. This pairs
2322 * with vcpu_enter_guest and walk_shadow_page_lockless_begin/end.
2323 *
2324 * In addition, kvm_flush_remote_tlbs waits for all vcpus to exit
2325 * guest mode and/or lockless shadow page table walks.
Avi Kivityc1427862012-05-14 15:44:06 +03002326 */
Xiao Guangrong77758342010-06-04 21:53:54 +08002327 kvm_flush_remote_tlbs(kvm);
2328
Takuya Yoshikawa945315b2013-03-06 16:05:52 +09002329 list_for_each_entry_safe(sp, nsp, invalid_list, link) {
Xiao Guangrong77758342010-06-04 21:53:54 +08002330 WARN_ON(!sp->role.invalid || sp->root_count);
Xiao Guangrongaa6bd182011-07-12 03:26:40 +08002331 kvm_mmu_free_page(sp);
Takuya Yoshikawa945315b2013-03-06 16:05:52 +09002332 }
Xiao Guangrong77758342010-06-04 21:53:54 +08002333}
2334
Sean Christopherson6b82ef22020-06-23 12:35:40 -07002335static unsigned long kvm_mmu_zap_oldest_mmu_pages(struct kvm *kvm,
2336 unsigned long nr_to_zap)
Takuya Yoshikawa5da59602013-03-06 16:06:58 +09002337{
Sean Christopherson6b82ef22020-06-23 12:35:40 -07002338 unsigned long total_zapped = 0;
2339 struct kvm_mmu_page *sp, *tmp;
Sean Christophersonba7888d2019-12-06 15:57:15 -08002340 LIST_HEAD(invalid_list);
Sean Christopherson6b82ef22020-06-23 12:35:40 -07002341 bool unstable;
2342 int nr_zapped;
Takuya Yoshikawa5da59602013-03-06 16:06:58 +09002343
2344 if (list_empty(&kvm->arch.active_mmu_pages))
Sean Christophersonba7888d2019-12-06 15:57:15 -08002345 return 0;
Takuya Yoshikawa5da59602013-03-06 16:06:58 +09002346
Sean Christopherson6b82ef22020-06-23 12:35:40 -07002347restart:
Sean Christopherson8fc51722021-01-13 12:50:30 -08002348 list_for_each_entry_safe_reverse(sp, tmp, &kvm->arch.active_mmu_pages, link) {
Sean Christopherson6b82ef22020-06-23 12:35:40 -07002349 /*
2350 * Don't zap active root pages, the page itself can't be freed
2351 * and zapping it will just force vCPUs to realloc and reload.
2352 */
2353 if (sp->root_count)
2354 continue;
2355
2356 unstable = __kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list,
2357 &nr_zapped);
2358 total_zapped += nr_zapped;
2359 if (total_zapped >= nr_to_zap)
Sean Christophersonba7888d2019-12-06 15:57:15 -08002360 break;
2361
Sean Christopherson6b82ef22020-06-23 12:35:40 -07002362 if (unstable)
2363 goto restart;
Sean Christophersonba7888d2019-12-06 15:57:15 -08002364 }
Sean Christopherson6b82ef22020-06-23 12:35:40 -07002365
2366 kvm_mmu_commit_zap_page(kvm, &invalid_list);
2367
2368 kvm->stat.mmu_recycled += total_zapped;
2369 return total_zapped;
2370}
2371
Sean Christophersonafe8d7e2020-06-22 13:20:30 -07002372static inline unsigned long kvm_mmu_available_pages(struct kvm *kvm)
2373{
2374 if (kvm->arch.n_max_mmu_pages > kvm->arch.n_used_mmu_pages)
2375 return kvm->arch.n_max_mmu_pages -
2376 kvm->arch.n_used_mmu_pages;
2377
2378 return 0;
Takuya Yoshikawa5da59602013-03-06 16:06:58 +09002379}
2380
Sean Christophersonba7888d2019-12-06 15:57:15 -08002381static int make_mmu_pages_available(struct kvm_vcpu *vcpu)
2382{
Sean Christopherson6b82ef22020-06-23 12:35:40 -07002383 unsigned long avail = kvm_mmu_available_pages(vcpu->kvm);
Sean Christophersonba7888d2019-12-06 15:57:15 -08002384
Sean Christopherson6b82ef22020-06-23 12:35:40 -07002385 if (likely(avail >= KVM_MIN_FREE_MMU_PAGES))
Sean Christophersonba7888d2019-12-06 15:57:15 -08002386 return 0;
2387
Sean Christopherson6b82ef22020-06-23 12:35:40 -07002388 kvm_mmu_zap_oldest_mmu_pages(vcpu->kvm, KVM_REFILL_PAGES - avail);
Sean Christophersonba7888d2019-12-06 15:57:15 -08002389
Sean Christopherson6e6ec582021-03-04 17:10:50 -08002390 /*
2391 * Note, this check is intentionally soft, it only guarantees that one
2392 * page is available, while the caller may end up allocating as many as
2393 * four pages, e.g. for PAE roots or for 5-level paging. Temporarily
2394 * exceeding the (arbitrary by default) limit will not harm the host,
2395 * being too agressive may unnecessarily kill the guest, and getting an
2396 * exact count is far more trouble than it's worth, especially in the
2397 * page fault paths.
2398 */
Sean Christophersonba7888d2019-12-06 15:57:15 -08002399 if (!kvm_mmu_available_pages(vcpu->kvm))
2400 return -ENOSPC;
2401 return 0;
2402}
2403
Izik Eidus82ce2c92007-10-02 18:52:55 +02002404/*
2405 * Changing the number of mmu pages allocated to the vm
Dave Hansen49d5ca22010-08-19 18:11:28 -07002406 * Note: if goal_nr_mmu_pages is too small, you will get dead lock
Izik Eidus82ce2c92007-10-02 18:52:55 +02002407 */
Ben Gardonbc8a3d82019-04-08 11:07:30 -07002408void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned long goal_nr_mmu_pages)
Izik Eidus82ce2c92007-10-02 18:52:55 +02002409{
Ben Gardon531810c2021-02-02 10:57:24 -08002410 write_lock(&kvm->mmu_lock);
Takuya Yoshikawab34cb592013-01-08 19:46:07 +09002411
Dave Hansen49d5ca22010-08-19 18:11:28 -07002412 if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) {
Sean Christopherson6b82ef22020-06-23 12:35:40 -07002413 kvm_mmu_zap_oldest_mmu_pages(kvm, kvm->arch.n_used_mmu_pages -
2414 goal_nr_mmu_pages);
Izik Eidus82ce2c92007-10-02 18:52:55 +02002415
Dave Hansen49d5ca22010-08-19 18:11:28 -07002416 goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002417 }
Izik Eidus82ce2c92007-10-02 18:52:55 +02002418
Dave Hansen49d5ca22010-08-19 18:11:28 -07002419 kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages;
Takuya Yoshikawab34cb592013-01-08 19:46:07 +09002420
Ben Gardon531810c2021-02-02 10:57:24 -08002421 write_unlock(&kvm->mmu_lock);
Izik Eidus82ce2c92007-10-02 18:52:55 +02002422}
2423
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08002424int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08002425{
Avi Kivity4db35312007-11-21 15:28:32 +02002426 struct kvm_mmu_page *sp;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002427 LIST_HEAD(invalid_list);
Avi Kivitya4360362007-01-05 16:36:45 -08002428 int r;
2429
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002430 pgprintk("%s: looking for gfn %llx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08002431 r = 0;
Ben Gardon531810c2021-02-02 10:57:24 -08002432 write_lock(&kvm->mmu_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002433 for_each_gfn_indirect_valid_sp(kvm, sp, gfn) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002434 pgprintk("%s: gfn %llx role %x\n", __func__, gfn,
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002435 sp->role.word);
2436 r = 1;
Xiao Guangrongf41d3352010-06-04 21:56:11 +08002437 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
Xiao Guangrong7ae680e2010-06-04 21:53:07 +08002438 }
Xiao Guangrongd98ba052010-06-04 21:55:29 +08002439 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Ben Gardon531810c2021-02-02 10:57:24 -08002440 write_unlock(&kvm->mmu_lock);
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08002441
Avi Kivitya4360362007-01-05 16:36:45 -08002442 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002443}
Sean Christopherson96ad91a2021-02-12 16:50:15 -08002444
2445static int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2446{
2447 gpa_t gpa;
2448 int r;
2449
2450 if (vcpu->arch.mmu->direct_map)
2451 return 0;
2452
2453 gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
2454
2455 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
2456
2457 return r;
2458}
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002459
Xiao Guangrong5c520e92016-02-24 17:51:15 +08002460static void kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002461{
Xiao Guangrong5e1b3dd2010-04-28 11:55:06 +08002462 trace_kvm_mmu_unsync_page(sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002463 ++vcpu->kvm->stat.mmu_unsync;
2464 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02002465
Xiao Guangrong6b184932010-04-16 21:29:17 +08002466 kvm_mmu_mark_parents_unsync(sp);
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002467}
2468
Paolo Bonzini5a9624a2020-10-16 10:29:37 -04002469bool mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
2470 bool can_unsync)
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002471{
Xiao Guangrong5c520e92016-02-24 17:51:15 +08002472 struct kvm_mmu_page *sp;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002473
Xiao Guangrong3d0c27a2016-02-24 17:51:11 +08002474 if (kvm_page_track_is_active(vcpu, gfn, KVM_PAGE_TRACK_WRITE))
2475 return true;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002476
Xiao Guangrong5c520e92016-02-24 17:51:15 +08002477 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn) {
Xiao Guangrong36a2e672010-06-30 16:02:02 +08002478 if (!can_unsync)
Xiao Guangrong3d0c27a2016-02-24 17:51:11 +08002479 return true;
Xiao Guangrong36a2e672010-06-30 16:02:02 +08002480
Xiao Guangrong5c520e92016-02-24 17:51:15 +08002481 if (sp->unsync)
2482 continue;
Xiao Guangrong9cf5cf52010-05-24 15:40:07 +08002483
Sean Christopherson3bae0452020-04-27 17:54:22 -07002484 WARN_ON(sp->role.level != PG_LEVEL_4K);
Xiao Guangrong5c520e92016-02-24 17:51:15 +08002485 kvm_unsync_page(vcpu, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002486 }
Xiao Guangrong3d0c27a2016-02-24 17:51:11 +08002487
Junaid Shahid578e1c42018-06-27 14:59:05 -07002488 /*
2489 * We need to ensure that the marking of unsync pages is visible
2490 * before the SPTE is updated to allow writes because
2491 * kvm_mmu_sync_roots() checks the unsync flags without holding
2492 * the MMU lock and so can race with this. If the SPTE was updated
2493 * before the page had been marked as unsync-ed, something like the
2494 * following could happen:
2495 *
2496 * CPU 1 CPU 2
2497 * ---------------------------------------------------------------------
2498 * 1.2 Host updates SPTE
2499 * to be writable
2500 * 2.1 Guest writes a GPTE for GVA X.
2501 * (GPTE being in the guest page table shadowed
2502 * by the SP from CPU 1.)
2503 * This reads SPTE during the page table walk.
2504 * Since SPTE.W is read as 1, there is no
2505 * fault.
2506 *
2507 * 2.2 Guest issues TLB flush.
2508 * That causes a VM Exit.
2509 *
2510 * 2.3 kvm_mmu_sync_pages() reads sp->unsync.
2511 * Since it is false, so it just returns.
2512 *
2513 * 2.4 Guest accesses GVA X.
2514 * Since the mapping in the SP was not updated,
2515 * so the old mapping for GVA X incorrectly
2516 * gets used.
2517 * 1.1 Host marks SP
2518 * as unsync
2519 * (sp->unsync = true)
2520 *
2521 * The write barrier below ensures that 1.1 happens before 1.2 and thus
2522 * the situation in 2.4 does not arise. The implicit barrier in 2.2
2523 * pairs with this write barrier.
2524 */
2525 smp_wmb();
2526
Xiao Guangrong3d0c27a2016-02-24 17:51:11 +08002527 return false;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002528}
2529
Ben Gardon799a4192020-10-14 20:26:41 +02002530static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
2531 unsigned int pte_access, int level,
2532 gfn_t gfn, kvm_pfn_t pfn, bool speculative,
2533 bool can_unsync, bool host_writable)
2534{
2535 u64 spte;
2536 struct kvm_mmu_page *sp;
2537 int ret;
2538
Ben Gardon799a4192020-10-14 20:26:41 +02002539 sp = sptep_to_sp(sptep);
2540
2541 ret = make_spte(vcpu, pte_access, level, gfn, pfn, *sptep, speculative,
2542 can_unsync, host_writable, sp_ad_disabled(sp), &spte);
2543
2544 if (spte & PT_WRITABLE_MASK)
2545 kvm_vcpu_mark_page_dirty(vcpu, gfn);
2546
Sean Christopherson12703752020-09-23 15:04:25 -07002547 if (*sptep == spte)
2548 ret |= SET_SPTE_SPURIOUS;
2549 else if (mmu_spte_update(sptep, spte))
Junaid Shahid5ce47862018-06-27 14:59:04 -07002550 ret |= SET_SPTE_NEED_REMOTE_TLB_FLUSH;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002551 return ret;
2552}
2553
Ben Gardon0a2b64c2020-02-03 15:09:09 -08002554static int mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Sean Christophersone88b8092020-09-23 11:37:35 -07002555 unsigned int pte_access, bool write_fault, int level,
Ben Gardon0a2b64c2020-02-03 15:09:09 -08002556 gfn_t gfn, kvm_pfn_t pfn, bool speculative,
2557 bool host_writable)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002558{
2559 int was_rmapped = 0;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03002560 int rmap_count;
Junaid Shahid5ce47862018-06-27 14:59:04 -07002561 int set_spte_ret;
Sean Christophersonc4371c22020-09-23 15:04:24 -07002562 int ret = RET_PF_FIXED;
Tianyu Lanc2a4ead2018-07-24 08:17:07 +00002563 bool flush = false;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002564
Xiao Guangrongf7616202013-02-05 15:27:27 +08002565 pgprintk("%s: spte %llx write_fault %d gfn %llx\n", __func__,
2566 *sptep, write_fault, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002567
Sean Christophersona54aa152021-02-25 12:47:32 -08002568 if (unlikely(is_noslot_pfn(pfn))) {
2569 mark_mmio_spte(vcpu, sptep, gfn, pte_access);
2570 return RET_PF_EMULATE;
2571 }
2572
Takuya Yoshikawaafd28fe2015-11-20 17:44:55 +09002573 if (is_shadow_present_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002574 /*
2575 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
2576 * the parent of the now unreachable PTE.
2577 */
Sean Christopherson3bae0452020-04-27 17:54:22 -07002578 if (level > PG_LEVEL_4K && !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002579 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03002580 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002581
Sean Christophersone47c4ae2020-06-22 13:20:34 -07002582 child = to_shadow_page(pte & PT64_BASE_ADDR_MASK);
Xiao Guangrongbcdd9a92011-05-15 23:28:29 +08002583 drop_parent_pte(child, sptep);
Tianyu Lanc2a4ead2018-07-24 08:17:07 +00002584 flush = true;
Avi Kivityd555c332009-06-10 14:24:23 +03002585 } else if (pfn != spte_to_pfn(*sptep)) {
Xiao Guangrong9ad17b102010-08-28 19:19:42 +08002586 pgprintk("hfn old %llx new %llx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03002587 spte_to_pfn(*sptep), pfn);
Xiao Guangrongc3707952011-07-12 03:28:04 +08002588 drop_spte(vcpu->kvm, sptep);
Tianyu Lanc2a4ead2018-07-24 08:17:07 +00002589 flush = true;
Joerg Roedel6bed6b92009-02-18 14:08:59 +01002590 } else
2591 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002592 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02002593
Junaid Shahid5ce47862018-06-27 14:59:04 -07002594 set_spte_ret = set_spte(vcpu, sptep, pte_access, level, gfn, pfn,
2595 speculative, true, host_writable);
2596 if (set_spte_ret & SET_SPTE_WRITE_PROTECTED_PT) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002597 if (write_fault)
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02002598 ret = RET_PF_EMULATE;
Sean Christopherson8c8560b2020-03-20 14:28:21 -07002599 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03002600 }
Lan Tianyuc3134ce2018-12-06 21:21:09 +08002601
Tianyu Lanc2a4ead2018-07-24 08:17:07 +00002602 if (set_spte_ret & SET_SPTE_NEED_REMOTE_TLB_FLUSH || flush)
Lan Tianyuc3134ce2018-12-06 21:21:09 +08002603 kvm_flush_remote_tlbs_with_address(vcpu->kvm, gfn,
2604 KVM_PAGES_PER_HPAGE(level));
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03002605
Sean Christopherson12703752020-09-23 15:04:25 -07002606 /*
2607 * The fault is fully spurious if and only if the new SPTE and old SPTE
2608 * are identical, and emulation is not required.
2609 */
2610 if ((set_spte_ret & SET_SPTE_SPURIOUS) && ret == RET_PF_FIXED) {
2611 WARN_ON_ONCE(!was_rmapped);
2612 return RET_PF_SPURIOUS;
2613 }
2614
Avi Kivityd555c332009-06-10 14:24:23 +03002615 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Paolo Bonzini335e1922019-07-01 06:22:57 -04002616 trace_kvm_mmu_set_spte(level, gfn, sptep);
Avi Kivityd555c332009-06-10 14:24:23 +03002617 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002618 ++vcpu->kvm->stat.lpages;
2619
Xiao Guangrongffb61bb2011-07-12 03:22:01 +08002620 if (is_shadow_present_pte(*sptep)) {
Xiao Guangrongffb61bb2011-07-12 03:22:01 +08002621 if (!was_rmapped) {
2622 rmap_count = rmap_add(vcpu, sptep, gfn);
2623 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
2624 rmap_recycle(vcpu, sptep, gfn);
2625 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002626 }
Xiao Guangrongcb9aaa32012-08-03 15:42:10 +08002627
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02002628 return ret;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02002629}
2630
Dan Williamsba049e92016-01-15 16:56:11 -08002631static kvm_pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn,
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002632 bool no_dirty_log)
2633{
2634 struct kvm_memory_slot *slot;
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002635
Xiao Guangrong5d163b12011-03-09 15:43:00 +08002636 slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, no_dirty_log);
Xiao Guangrong903816f2012-07-17 21:54:11 +08002637 if (!slot)
Xiao Guangrong6c8ee572012-08-03 15:37:54 +08002638 return KVM_PFN_ERR_FAULT;
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002639
Xiao Guangrong037d92d2012-08-21 10:59:12 +08002640 return gfn_to_pfn_memslot_atomic(slot, gfn);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002641}
2642
2643static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,
2644 struct kvm_mmu_page *sp,
2645 u64 *start, u64 *end)
2646{
2647 struct page *pages[PTE_PREFETCH_NUM];
Paolo Bonzinid9ef13c2015-05-19 16:01:50 +02002648 struct kvm_memory_slot *slot;
Ben Gardon0a2b64c2020-02-03 15:09:09 -08002649 unsigned int access = sp->role.access;
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002650 int i, ret;
2651 gfn_t gfn;
2652
2653 gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt);
Paolo Bonzinid9ef13c2015-05-19 16:01:50 +02002654 slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, access & ACC_WRITE_MASK);
2655 if (!slot)
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002656 return -1;
2657
Paolo Bonzinid9ef13c2015-05-19 16:01:50 +02002658 ret = gfn_to_page_many_atomic(slot, gfn, pages, end - start);
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002659 if (ret <= 0)
2660 return -1;
2661
Junaid Shahid43fdcda2019-01-03 16:22:21 -08002662 for (i = 0; i < ret; i++, gfn++, start++) {
Sean Christophersone88b8092020-09-23 11:37:35 -07002663 mmu_set_spte(vcpu, start, access, false, sp->role.level, gfn,
Takuya Yoshikawa029499b2015-11-20 17:44:05 +09002664 page_to_pfn(pages[i]), true, true);
Junaid Shahid43fdcda2019-01-03 16:22:21 -08002665 put_page(pages[i]);
2666 }
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002667
2668 return 0;
2669}
2670
2671static void __direct_pte_prefetch(struct kvm_vcpu *vcpu,
2672 struct kvm_mmu_page *sp, u64 *sptep)
2673{
2674 u64 *spte, *start = NULL;
2675 int i;
2676
2677 WARN_ON(!sp->role.direct);
2678
2679 i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
2680 spte = sp->spt + i;
2681
2682 for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
Xiao Guangrongc3707952011-07-12 03:28:04 +08002683 if (is_shadow_present_pte(*spte) || spte == sptep) {
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002684 if (!start)
2685 continue;
2686 if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0)
2687 break;
2688 start = NULL;
2689 } else if (!start)
2690 start = spte;
2691 }
2692}
2693
2694static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)
2695{
2696 struct kvm_mmu_page *sp;
2697
Sean Christopherson57354682020-06-22 13:20:33 -07002698 sp = sptep_to_sp(sptep);
Peter Feinerac8d57e2017-06-30 17:26:31 -07002699
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002700 /*
Peter Feinerac8d57e2017-06-30 17:26:31 -07002701 * Without accessed bits, there's no way to distinguish between
2702 * actually accessed translations and prefetched, so disable pte
2703 * prefetch if accessed bits aren't available.
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002704 */
Peter Feinerac8d57e2017-06-30 17:26:31 -07002705 if (sp_ad_disabled(sp))
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002706 return;
2707
Sean Christopherson3bae0452020-04-27 17:54:22 -07002708 if (sp->role.level > PG_LEVEL_4K)
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002709 return;
2710
David Stevens4a42d842021-02-22 11:45:22 +09002711 /*
2712 * If addresses are being invalidated, skip prefetching to avoid
2713 * accidentally prefetching those addresses.
2714 */
2715 if (unlikely(vcpu->kvm->mmu_notifier_count))
2716 return;
2717
Xiao Guangrong957ed9e2010-08-22 19:12:48 +08002718 __direct_pte_prefetch(vcpu, sp, sptep);
2719}
2720
Sean Christopherson1b6d9d92021-02-12 16:50:04 -08002721static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn,
Ben Gardon8ca6f062021-04-01 16:37:24 -07002722 const struct kvm_memory_slot *slot)
Sean Christophersondb543212020-01-08 12:24:41 -08002723{
Sean Christophersondb543212020-01-08 12:24:41 -08002724 unsigned long hva;
2725 pte_t *pte;
2726 int level;
2727
Sean Christophersone8512652020-01-08 12:24:48 -08002728 if (!PageCompound(pfn_to_page(pfn)) && !kvm_is_zone_device_pfn(pfn))
Sean Christopherson3bae0452020-04-27 17:54:22 -07002729 return PG_LEVEL_4K;
Sean Christophersondb543212020-01-08 12:24:41 -08002730
Sean Christopherson293e3062020-01-08 12:24:46 -08002731 /*
2732 * Note, using the already-retrieved memslot and __gfn_to_hva_memslot()
2733 * is not solely for performance, it's also necessary to avoid the
2734 * "writable" check in __gfn_to_hva_many(), which will always fail on
2735 * read-only memslots due to gfn_to_hva() assuming writes. Earlier
2736 * page fault steps have already verified the guest isn't writing a
2737 * read-only memslot.
2738 */
Sean Christophersondb543212020-01-08 12:24:41 -08002739 hva = __gfn_to_hva_memslot(slot, gfn);
2740
Sean Christopherson1b6d9d92021-02-12 16:50:04 -08002741 pte = lookup_address_in_mm(kvm->mm, hva, &level);
Sean Christophersondb543212020-01-08 12:24:41 -08002742 if (unlikely(!pte))
Sean Christopherson3bae0452020-04-27 17:54:22 -07002743 return PG_LEVEL_4K;
Sean Christophersondb543212020-01-08 12:24:41 -08002744
2745 return level;
2746}
2747
Ben Gardon8ca6f062021-04-01 16:37:24 -07002748int kvm_mmu_max_mapping_level(struct kvm *kvm,
2749 const struct kvm_memory_slot *slot, gfn_t gfn,
2750 kvm_pfn_t pfn, int max_level)
Sean Christopherson1b6d9d92021-02-12 16:50:04 -08002751{
2752 struct kvm_lpage_info *linfo;
2753
2754 max_level = min(max_level, max_huge_page_level);
2755 for ( ; max_level > PG_LEVEL_4K; max_level--) {
2756 linfo = lpage_info_slot(gfn, slot, max_level);
2757 if (!linfo->disallow_lpage)
2758 break;
2759 }
2760
2761 if (max_level == PG_LEVEL_4K)
2762 return PG_LEVEL_4K;
2763
2764 return host_pfn_mapping_level(kvm, gfn, pfn, slot);
2765}
2766
Ben Gardonbb188422020-10-14 11:26:50 -07002767int kvm_mmu_hugepage_adjust(struct kvm_vcpu *vcpu, gfn_t gfn,
2768 int max_level, kvm_pfn_t *pfnp,
2769 bool huge_page_disallowed, int *req_level)
Sean Christopherson08859042019-12-06 15:57:25 -08002770{
Sean Christopherson293e3062020-01-08 12:24:46 -08002771 struct kvm_memory_slot *slot;
Sean Christopherson08859042019-12-06 15:57:25 -08002772 kvm_pfn_t pfn = *pfnp;
Sean Christopherson17eff012020-01-08 12:24:40 -08002773 kvm_pfn_t mask;
Sean Christopherson83f06fa2020-01-08 12:24:43 -08002774 int level;
Sean Christopherson17eff012020-01-08 12:24:40 -08002775
Sean Christopherson3cf06612020-09-23 11:37:31 -07002776 *req_level = PG_LEVEL_4K;
2777
Sean Christopherson3bae0452020-04-27 17:54:22 -07002778 if (unlikely(max_level == PG_LEVEL_4K))
2779 return PG_LEVEL_4K;
Sean Christopherson17eff012020-01-08 12:24:40 -08002780
Sean Christophersone8512652020-01-08 12:24:48 -08002781 if (is_error_noslot_pfn(pfn) || kvm_is_reserved_pfn(pfn))
Sean Christopherson3bae0452020-04-27 17:54:22 -07002782 return PG_LEVEL_4K;
Sean Christopherson17eff012020-01-08 12:24:40 -08002783
Sean Christopherson293e3062020-01-08 12:24:46 -08002784 slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, true);
2785 if (!slot)
Sean Christopherson3bae0452020-04-27 17:54:22 -07002786 return PG_LEVEL_4K;
Sean Christopherson293e3062020-01-08 12:24:46 -08002787
Sean Christopherson1b6d9d92021-02-12 16:50:04 -08002788 level = kvm_mmu_max_mapping_level(vcpu->kvm, slot, gfn, pfn, max_level);
Sean Christopherson3bae0452020-04-27 17:54:22 -07002789 if (level == PG_LEVEL_4K)
Sean Christopherson83f06fa2020-01-08 12:24:43 -08002790 return level;
Sean Christopherson17eff012020-01-08 12:24:40 -08002791
Sean Christopherson3cf06612020-09-23 11:37:31 -07002792 *req_level = level = min(level, max_level);
2793
2794 /*
2795 * Enforce the iTLB multihit workaround after capturing the requested
2796 * level, which will be used to do precise, accurate accounting.
2797 */
2798 if (huge_page_disallowed)
2799 return PG_LEVEL_4K;
Sean Christopherson08859042019-12-06 15:57:25 -08002800
2801 /*
Sean Christopherson17eff012020-01-08 12:24:40 -08002802 * mmu_notifier_retry() was successful and mmu_lock is held, so
2803 * the pmd can't be split from under us.
Sean Christopherson08859042019-12-06 15:57:25 -08002804 */
Sean Christopherson17eff012020-01-08 12:24:40 -08002805 mask = KVM_PAGES_PER_HPAGE(level) - 1;
2806 VM_BUG_ON((gfn & mask) != (pfn & mask));
2807 *pfnp = pfn & ~mask;
Sean Christopherson83f06fa2020-01-08 12:24:43 -08002808
2809 return level;
Sean Christopherson08859042019-12-06 15:57:25 -08002810}
2811
Ben Gardonbb188422020-10-14 11:26:50 -07002812void disallowed_hugepage_adjust(u64 spte, gfn_t gfn, int cur_level,
2813 kvm_pfn_t *pfnp, int *goal_levelp)
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01002814{
Ben Gardonbb188422020-10-14 11:26:50 -07002815 int level = *goal_levelp;
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01002816
Ben Gardon7d945312020-10-14 11:26:49 -07002817 if (cur_level == level && level > PG_LEVEL_4K &&
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01002818 is_shadow_present_pte(spte) &&
2819 !is_large_pte(spte)) {
2820 /*
2821 * A small SPTE exists for this pfn, but FNAME(fetch)
2822 * and __direct_map would like to create a large PTE
2823 * instead: just force them to go down another level,
2824 * patching back for them into pfn the next 9 bits of
2825 * the address.
2826 */
Ben Gardon7d945312020-10-14 11:26:49 -07002827 u64 page_mask = KVM_PAGES_PER_HPAGE(level) -
2828 KVM_PAGES_PER_HPAGE(level - 1);
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01002829 *pfnp |= gfn & page_mask;
Ben Gardonbb188422020-10-14 11:26:50 -07002830 (*goal_levelp)--;
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01002831 }
2832}
2833
Sean Christopherson6c2fd342020-09-23 11:37:30 -07002834static int __direct_map(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code,
Sean Christopherson83f06fa2020-01-08 12:24:43 -08002835 int map_writable, int max_level, kvm_pfn_t pfn,
Sean Christopherson6c2fd342020-09-23 11:37:30 -07002836 bool prefault, bool is_tdp)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002837{
Sean Christopherson6c2fd342020-09-23 11:37:30 -07002838 bool nx_huge_page_workaround_enabled = is_nx_huge_page_enabled();
2839 bool write = error_code & PFERR_WRITE_MASK;
2840 bool exec = error_code & PFERR_FETCH_MASK;
2841 bool huge_page_disallowed = exec && nx_huge_page_workaround_enabled;
Paolo Bonzini3fcf2d12019-06-24 13:06:21 +02002842 struct kvm_shadow_walk_iterator it;
Avi Kivity9f652d212008-12-25 14:54:25 +02002843 struct kvm_mmu_page *sp;
Sean Christopherson3cf06612020-09-23 11:37:31 -07002844 int level, req_level, ret;
Paolo Bonzini3fcf2d12019-06-24 13:06:21 +02002845 gfn_t gfn = gpa >> PAGE_SHIFT;
2846 gfn_t base_gfn = gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002847
Sean Christopherson0c7a98e2019-12-06 15:57:28 -08002848 if (WARN_ON(!VALID_PAGE(vcpu->arch.mmu->root_hpa)))
Paolo Bonzini3fcf2d12019-06-24 13:06:21 +02002849 return RET_PF_RETRY;
Marcelo Tosatti989c6b32013-12-19 15:28:51 -02002850
Sean Christopherson3cf06612020-09-23 11:37:31 -07002851 level = kvm_mmu_hugepage_adjust(vcpu, gfn, max_level, &pfn,
2852 huge_page_disallowed, &req_level);
Sean Christopherson4cd071d2019-12-06 15:57:26 -08002853
Paolo Bonzini335e1922019-07-01 06:22:57 -04002854 trace_kvm_mmu_spte_requested(gpa, level, pfn);
Paolo Bonzini3fcf2d12019-06-24 13:06:21 +02002855 for_each_shadow_entry(vcpu, gpa, it) {
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01002856 /*
2857 * We cannot overwrite existing page tables with an NX
2858 * large page, as the leaf could be executable.
2859 */
Sean Christophersondcc70652020-09-23 11:37:34 -07002860 if (nx_huge_page_workaround_enabled)
Ben Gardon7d945312020-10-14 11:26:49 -07002861 disallowed_hugepage_adjust(*it.sptep, gfn, it.level,
2862 &pfn, &level);
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01002863
Paolo Bonzini3fcf2d12019-06-24 13:06:21 +02002864 base_gfn = gfn & ~(KVM_PAGES_PER_HPAGE(it.level) - 1);
2865 if (it.level == level)
Avi Kivity9f652d212008-12-25 14:54:25 +02002866 break;
Avi Kivity9f652d212008-12-25 14:54:25 +02002867
Paolo Bonzini3fcf2d12019-06-24 13:06:21 +02002868 drop_large_spte(vcpu, it.sptep);
2869 if (!is_shadow_present_pte(*it.sptep)) {
2870 sp = kvm_mmu_get_page(vcpu, base_gfn, it.addr,
2871 it.level - 1, true, ACC_ALL);
Lai Jiangshanc9fa0b32010-05-26 16:48:25 +08002872
Paolo Bonzini3fcf2d12019-06-24 13:06:21 +02002873 link_shadow_page(vcpu, it.sptep, sp);
Sean Christopherson5bcaf3e2020-09-23 11:37:32 -07002874 if (is_tdp && huge_page_disallowed &&
2875 req_level >= it.level)
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01002876 account_huge_nx_page(vcpu->kvm, sp);
Avi Kivity9f652d212008-12-25 14:54:25 +02002877 }
2878 }
Paolo Bonzini3fcf2d12019-06-24 13:06:21 +02002879
2880 ret = mmu_set_spte(vcpu, it.sptep, ACC_ALL,
2881 write, level, base_gfn, pfn, prefault,
2882 map_writable);
Sean Christopherson12703752020-09-23 15:04:25 -07002883 if (ret == RET_PF_SPURIOUS)
2884 return ret;
2885
Paolo Bonzini3fcf2d12019-06-24 13:06:21 +02002886 direct_pte_prefetch(vcpu, it.sptep);
2887 ++vcpu->stat.pf_fixed;
2888 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002889}
2890
Huang Ying77db5cb2010-10-08 16:24:15 +08002891static void kvm_send_hwpoison_signal(unsigned long address, struct task_struct *tsk)
Huang Yingbf998152010-05-31 14:28:19 +08002892{
Eric W. Biederman585a8b92018-04-16 14:23:27 -05002893 send_sig_mceerr(BUS_MCEERR_AR, (void __user *)address, PAGE_SHIFT, tsk);
Huang Yingbf998152010-05-31 14:28:19 +08002894}
2895
Dan Williamsba049e92016-01-15 16:56:11 -08002896static int kvm_handle_bad_page(struct kvm_vcpu *vcpu, gfn_t gfn, kvm_pfn_t pfn)
Huang Yingbf998152010-05-31 14:28:19 +08002897{
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08002898 /*
2899 * Do not cache the mmio info caused by writing the readonly gfn
2900 * into the spte otherwise read access on readonly gfn also can
2901 * caused mmio page fault and treat it as mmio access.
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08002902 */
2903 if (pfn == KVM_PFN_ERR_RO_FAULT)
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02002904 return RET_PF_EMULATE;
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08002905
Xiao Guangronge6c15022012-08-03 15:38:36 +08002906 if (pfn == KVM_PFN_ERR_HWPOISON) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002907 kvm_send_hwpoison_signal(kvm_vcpu_gfn_to_hva(vcpu, gfn), current);
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02002908 return RET_PF_RETRY;
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002909 }
Gleb Natapovedba23e2010-07-07 20:16:45 +03002910
Sean Christopherson2c151b22018-03-29 14:48:30 -07002911 return -EFAULT;
Huang Yingbf998152010-05-31 14:28:19 +08002912}
2913
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002914static bool handle_abnormal_pfn(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn,
Ben Gardon0a2b64c2020-02-03 15:09:09 -08002915 kvm_pfn_t pfn, unsigned int access,
2916 int *ret_val)
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002917{
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002918 /* The pfn is invalid, report the error! */
Xiao Guangrong81c52c52012-10-16 20:10:59 +08002919 if (unlikely(is_error_pfn(pfn))) {
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002920 *ret_val = kvm_handle_bad_page(vcpu, gfn, pfn);
Paolo Bonzini798e88b2016-02-23 15:28:51 +01002921 return true;
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002922 }
2923
Sean Christopherson30ab5902021-02-25 12:47:31 -08002924 if (unlikely(is_noslot_pfn(pfn))) {
Sean Christopherson4af77152019-08-01 13:35:22 -07002925 vcpu_cache_mmio_info(vcpu, gva, gfn,
2926 access & shadow_mmio_access_mask);
Sean Christopherson30ab5902021-02-25 12:47:31 -08002927 /*
2928 * If MMIO caching is disabled, emulate immediately without
2929 * touching the shadow page tables as attempting to install an
2930 * MMIO SPTE will just be an expensive nop.
2931 */
2932 if (unlikely(!shadow_mmio_value)) {
2933 *ret_val = RET_PF_EMULATE;
2934 return true;
2935 }
2936 }
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002937
Paolo Bonzini798e88b2016-02-23 15:28:51 +01002938 return false;
Xiao Guangrongd7c55202011-07-12 03:29:38 +08002939}
2940
Xiao Guangronge5552fd2013-07-30 21:01:59 +08002941static bool page_fault_can_be_fast(u32 error_code)
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002942{
2943 /*
Xiao Guangrong1c118b82013-07-18 12:52:37 +08002944 * Do not fix the mmio spte with invalid generation number which
2945 * need to be updated by slow page fault path.
2946 */
2947 if (unlikely(error_code & PFERR_RSVD_MASK))
2948 return false;
2949
Junaid Shahidf160c7b2016-12-06 16:46:16 -08002950 /* See if the page fault is due to an NX violation */
2951 if (unlikely(((error_code & (PFERR_FETCH_MASK | PFERR_PRESENT_MASK))
2952 == (PFERR_FETCH_MASK | PFERR_PRESENT_MASK))))
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002953 return false;
2954
Junaid Shahidf160c7b2016-12-06 16:46:16 -08002955 /*
2956 * #PF can be fast if:
2957 * 1. The shadow page table entry is not present, which could mean that
2958 * the fault is potentially caused by access tracking (if enabled).
2959 * 2. The shadow page table entry is present and the fault
2960 * is caused by write-protect, that means we just need change the W
2961 * bit of the spte which can be done out of mmu-lock.
2962 *
2963 * However, if access tracking is disabled we know that a non-present
2964 * page must be a genuine page fault where we have to create a new SPTE.
2965 * So, if access tracking is disabled, we return true only for write
2966 * accesses to a present page.
2967 */
2968
2969 return shadow_acc_track_mask != 0 ||
2970 ((error_code & (PFERR_WRITE_MASK | PFERR_PRESENT_MASK))
2971 == (PFERR_WRITE_MASK | PFERR_PRESENT_MASK));
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002972}
2973
Junaid Shahid97dceba2016-12-06 16:46:12 -08002974/*
2975 * Returns true if the SPTE was fixed successfully. Otherwise,
2976 * someone else modified the SPTE from its original value.
2977 */
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002978static bool
Xiao Guangrong92a476c2014-04-17 17:06:13 +08002979fast_pf_fix_direct_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
Junaid Shahidd3e328f22016-12-21 20:29:32 -08002980 u64 *sptep, u64 old_spte, u64 new_spte)
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002981{
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08002982 gfn_t gfn;
2983
2984 WARN_ON(!sp->role.direct);
2985
Kai Huang9b51a632015-01-28 10:54:25 +08002986 /*
2987 * Theoretically we could also set dirty bit (and flush TLB) here in
2988 * order to eliminate unnecessary PML logging. See comments in
2989 * set_spte. But fast_page_fault is very unlikely to happen with PML
2990 * enabled, so we do not do this. This might result in the same GPA
2991 * to be logged in PML buffer again when the write really happens, and
2992 * eventually to be called by mark_page_dirty twice. But it's also no
2993 * harm. This also avoids the TLB flush needed after setting dirty bit
2994 * so non-PML cases won't be impacted.
2995 *
2996 * Compare with set_spte where instead shadow_dirty_mask is set.
2997 */
Junaid Shahidf160c7b2016-12-06 16:46:16 -08002998 if (cmpxchg64(sptep, old_spte, new_spte) != old_spte)
Junaid Shahid97dceba2016-12-06 16:46:12 -08002999 return false;
3000
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003001 if (is_writable_pte(new_spte) && !is_writable_pte(old_spte)) {
Junaid Shahidf160c7b2016-12-06 16:46:16 -08003002 /*
3003 * The gfn of direct spte is stable since it is
3004 * calculated by sp->gfn.
3005 */
3006 gfn = kvm_mmu_page_get_gfn(sp, sptep - sp->spt);
3007 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3008 }
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003009
3010 return true;
3011}
3012
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003013static bool is_access_allowed(u32 fault_err_code, u64 spte)
3014{
3015 if (fault_err_code & PFERR_FETCH_MASK)
3016 return is_executable_pte(spte);
3017
3018 if (fault_err_code & PFERR_WRITE_MASK)
3019 return is_writable_pte(spte);
3020
3021 /* Fault was on Read access */
3022 return spte & PT_PRESENT_MASK;
3023}
3024
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003025/*
Sean Christophersonc4371c22020-09-23 15:04:24 -07003026 * Returns one of RET_PF_INVALID, RET_PF_FIXED or RET_PF_SPURIOUS.
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003027 */
Sean Christophersonc4371c22020-09-23 15:04:24 -07003028static int fast_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
3029 u32 error_code)
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003030{
3031 struct kvm_shadow_walk_iterator iterator;
Xiao Guangrong92a476c2014-04-17 17:06:13 +08003032 struct kvm_mmu_page *sp;
Sean Christophersonc4371c22020-09-23 15:04:24 -07003033 int ret = RET_PF_INVALID;
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003034 u64 spte = 0ull;
Junaid Shahid97dceba2016-12-06 16:46:12 -08003035 uint retry_count = 0;
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003036
Xiao Guangronge5552fd2013-07-30 21:01:59 +08003037 if (!page_fault_can_be_fast(error_code))
Sean Christophersonc4371c22020-09-23 15:04:24 -07003038 return ret;
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003039
3040 walk_shadow_page_lockless_begin(vcpu);
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003041
Junaid Shahid97dceba2016-12-06 16:46:12 -08003042 do {
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003043 u64 new_spte;
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003044
Sean Christopherson736c2912019-12-06 15:57:14 -08003045 for_each_shadow_entry_lockless(vcpu, cr2_or_gpa, iterator, spte)
Sean Christophersonf9fa2502020-01-08 12:24:42 -08003046 if (!is_shadow_present_pte(spte))
Junaid Shahidd162f302016-12-21 20:29:30 -08003047 break;
3048
Sean Christophersonec89e642021-02-25 12:47:28 -08003049 if (!is_shadow_present_pte(spte))
3050 break;
3051
Sean Christopherson57354682020-06-22 13:20:33 -07003052 sp = sptep_to_sp(iterator.sptep);
Junaid Shahid97dceba2016-12-06 16:46:12 -08003053 if (!is_last_spte(spte, sp->role.level))
3054 break;
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003055
Junaid Shahid97dceba2016-12-06 16:46:12 -08003056 /*
Junaid Shahidf160c7b2016-12-06 16:46:16 -08003057 * Check whether the memory access that caused the fault would
3058 * still cause it if it were to be performed right now. If not,
3059 * then this is a spurious fault caused by TLB lazily flushed,
3060 * or some other CPU has already fixed the PTE after the
3061 * current CPU took the fault.
Junaid Shahid97dceba2016-12-06 16:46:12 -08003062 *
3063 * Need not check the access of upper level table entries since
3064 * they are always ACC_ALL.
3065 */
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003066 if (is_access_allowed(error_code, spte)) {
Sean Christophersonc4371c22020-09-23 15:04:24 -07003067 ret = RET_PF_SPURIOUS;
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003068 break;
Junaid Shahid97dceba2016-12-06 16:46:12 -08003069 }
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003070
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003071 new_spte = spte;
Junaid Shahidf160c7b2016-12-06 16:46:16 -08003072
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003073 if (is_access_track_spte(spte))
3074 new_spte = restore_acc_track_spte(new_spte);
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003075
Junaid Shahid97dceba2016-12-06 16:46:12 -08003076 /*
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003077 * Currently, to simplify the code, write-protection can
3078 * be removed in the fast path only if the SPTE was
3079 * write-protected for dirty-logging or access tracking.
Junaid Shahid97dceba2016-12-06 16:46:12 -08003080 */
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003081 if ((error_code & PFERR_WRITE_MASK) &&
Miaohe Line6302692020-02-15 10:44:22 +08003082 spte_can_locklessly_be_made_writable(spte)) {
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003083 new_spte |= PT_WRITABLE_MASK;
3084
3085 /*
3086 * Do not fix write-permission on the large spte. Since
3087 * we only dirty the first page into the dirty-bitmap in
3088 * fast_pf_fix_direct_spte(), other pages are missed
3089 * if its slot has dirty logging enabled.
3090 *
3091 * Instead, we let the slow page fault path create a
3092 * normal spte to fix the access.
3093 *
3094 * See the comments in kvm_arch_commit_memory_region().
3095 */
Sean Christopherson3bae0452020-04-27 17:54:22 -07003096 if (sp->role.level > PG_LEVEL_4K)
Junaid Shahidd3e328f22016-12-21 20:29:32 -08003097 break;
3098 }
3099
3100 /* Verify that the fault can be handled in the fast path */
3101 if (new_spte == spte ||
3102 !is_access_allowed(error_code, new_spte))
Junaid Shahid97dceba2016-12-06 16:46:12 -08003103 break;
Xiao Guangrongc126d942014-04-17 17:06:14 +08003104
Junaid Shahid97dceba2016-12-06 16:46:12 -08003105 /*
3106 * Currently, fast page fault only works for direct mapping
3107 * since the gfn is not stable for indirect shadow page. See
Mauro Carvalho Chehab3ecad8c2020-04-14 18:48:36 +02003108 * Documentation/virt/kvm/locking.rst to get more detail.
Junaid Shahid97dceba2016-12-06 16:46:12 -08003109 */
Sean Christophersonc4371c22020-09-23 15:04:24 -07003110 if (fast_pf_fix_direct_spte(vcpu, sp, iterator.sptep, spte,
3111 new_spte)) {
3112 ret = RET_PF_FIXED;
Junaid Shahid97dceba2016-12-06 16:46:12 -08003113 break;
Sean Christophersonc4371c22020-09-23 15:04:24 -07003114 }
Junaid Shahid97dceba2016-12-06 16:46:12 -08003115
3116 if (++retry_count > 4) {
3117 printk_once(KERN_WARNING
3118 "kvm: Fast #PF retrying more than 4 times.\n");
3119 break;
3120 }
3121
Junaid Shahid97dceba2016-12-06 16:46:12 -08003122 } while (true);
3123
Sean Christopherson736c2912019-12-06 15:57:14 -08003124 trace_fast_page_fault(vcpu, cr2_or_gpa, error_code, iterator.sptep,
Sean Christophersonc4371c22020-09-23 15:04:24 -07003125 spte, ret);
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003126 walk_shadow_page_lockless_end(vcpu);
3127
Sean Christophersonc4371c22020-09-23 15:04:24 -07003128 return ret;
Xiao Guangrongc7ba5b42012-06-20 15:59:18 +08003129}
3130
Junaid Shahid74b566e2018-05-04 11:37:11 -07003131static void mmu_free_root_page(struct kvm *kvm, hpa_t *root_hpa,
3132 struct list_head *invalid_list)
3133{
3134 struct kvm_mmu_page *sp;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003135
Junaid Shahid74b566e2018-05-04 11:37:11 -07003136 if (!VALID_PAGE(*root_hpa))
3137 return;
3138
Sean Christophersone47c4ae2020-06-22 13:20:34 -07003139 sp = to_shadow_page(*root_hpa & PT64_BASE_ADDR_MASK);
Ben Gardon02c00b32020-10-14 20:26:44 +02003140
Ben Gardon2bdb3d82021-04-01 16:37:27 -07003141 if (is_tdp_mmu_page(sp))
Ben Gardon6103bc02021-04-01 16:37:32 -07003142 kvm_tdp_mmu_put_root(kvm, sp, false);
Ben Gardon76eb54e2021-04-01 16:37:25 -07003143 else if (!--sp->root_count && sp->role.invalid)
3144 kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
Junaid Shahid74b566e2018-05-04 11:37:11 -07003145
3146 *root_hpa = INVALID_PAGE;
3147}
3148
Junaid Shahid08fb59d2018-06-27 14:59:17 -07003149/* roots_to_free must be some combination of the KVM_MMU_ROOT_* flags */
Vitaly Kuznetsov6a82cd12018-10-08 21:28:07 +02003150void kvm_mmu_free_roots(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
3151 ulong roots_to_free)
Avi Kivity17ac10a2007-01-05 16:36:40 -08003152{
Sean Christopherson4d710de2020-09-23 12:12:04 -07003153 struct kvm *kvm = vcpu->kvm;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003154 int i;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08003155 LIST_HEAD(invalid_list);
Junaid Shahid08fb59d2018-06-27 14:59:17 -07003156 bool free_active_root = roots_to_free & KVM_MMU_ROOT_CURRENT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003157
Junaid Shahidb94742c2018-06-27 14:59:20 -07003158 BUILD_BUG_ON(KVM_MMU_NUM_PREV_ROOTS >= BITS_PER_LONG);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003159
Junaid Shahid08fb59d2018-06-27 14:59:17 -07003160 /* Before acquiring the MMU lock, see if we need to do any real work. */
Junaid Shahidb94742c2018-06-27 14:59:20 -07003161 if (!(free_active_root && VALID_PAGE(mmu->root_hpa))) {
3162 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
3163 if ((roots_to_free & KVM_MMU_ROOT_PREVIOUS(i)) &&
3164 VALID_PAGE(mmu->prev_roots[i].hpa))
3165 break;
3166
3167 if (i == KVM_MMU_NUM_PREV_ROOTS)
3168 return;
3169 }
Gleb Natapov35af5772013-05-16 11:55:51 +03003170
Ben Gardon531810c2021-02-02 10:57:24 -08003171 write_lock(&kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003172
Junaid Shahidb94742c2018-06-27 14:59:20 -07003173 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
3174 if (roots_to_free & KVM_MMU_ROOT_PREVIOUS(i))
Sean Christopherson4d710de2020-09-23 12:12:04 -07003175 mmu_free_root_page(kvm, &mmu->prev_roots[i].hpa,
Junaid Shahidb94742c2018-06-27 14:59:20 -07003176 &invalid_list);
Junaid Shahid7c390d32018-06-27 14:59:06 -07003177
Junaid Shahid08fb59d2018-06-27 14:59:17 -07003178 if (free_active_root) {
3179 if (mmu->shadow_root_level >= PT64_ROOT_4LEVEL &&
3180 (mmu->root_level >= PT64_ROOT_4LEVEL || mmu->direct_map)) {
Sean Christopherson4d710de2020-09-23 12:12:04 -07003181 mmu_free_root_page(kvm, &mmu->root_hpa, &invalid_list);
Sean Christopherson04d45552021-03-04 17:10:46 -08003182 } else if (mmu->pae_root) {
Sean Christophersonc834e5e42021-03-09 14:42:06 -08003183 for (i = 0; i < 4; ++i) {
3184 if (!IS_VALID_PAE_ROOT(mmu->pae_root[i]))
3185 continue;
3186
3187 mmu_free_root_page(kvm, &mmu->pae_root[i],
3188 &invalid_list);
3189 mmu->pae_root[i] = INVALID_PAE_ROOT;
3190 }
Junaid Shahid08fb59d2018-06-27 14:59:17 -07003191 }
Sean Christopherson04d45552021-03-04 17:10:46 -08003192 mmu->root_hpa = INVALID_PAGE;
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003193 mmu->root_pgd = 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003194 }
Junaid Shahid74b566e2018-05-04 11:37:11 -07003195
Sean Christopherson4d710de2020-09-23 12:12:04 -07003196 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Ben Gardon531810c2021-02-02 10:57:24 -08003197 write_unlock(&kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003198}
Junaid Shahid74b566e2018-05-04 11:37:11 -07003199EXPORT_SYMBOL_GPL(kvm_mmu_free_roots);
Avi Kivity17ac10a2007-01-05 16:36:40 -08003200
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003201static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
3202{
3203 int ret = 0;
3204
Vitaly Kuznetsov995decb2020-07-08 16:00:23 +02003205 if (!kvm_vcpu_is_visible_gfn(vcpu, root_gfn)) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03003206 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003207 ret = 1;
3208 }
3209
3210 return ret;
3211}
3212
Sean Christopherson8123f262020-04-27 19:37:14 -07003213static hpa_t mmu_alloc_root(struct kvm_vcpu *vcpu, gfn_t gfn, gva_t gva,
3214 u8 level, bool direct)
Joerg Roedel651dd372010-09-10 17:30:59 +02003215{
3216 struct kvm_mmu_page *sp;
Sean Christopherson8123f262020-04-27 19:37:14 -07003217
Sean Christopherson8123f262020-04-27 19:37:14 -07003218 sp = kvm_mmu_get_page(vcpu, gfn, gva, level, direct, ACC_ALL);
3219 ++sp->root_count;
3220
Sean Christopherson8123f262020-04-27 19:37:14 -07003221 return __pa(sp->spt);
3222}
3223
3224static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu)
3225{
Sean Christophersonb37233c2021-03-04 17:10:47 -08003226 struct kvm_mmu *mmu = vcpu->arch.mmu;
3227 u8 shadow_root_level = mmu->shadow_root_level;
Sean Christopherson8123f262020-04-27 19:37:14 -07003228 hpa_t root;
Avi Kivity7ebaf152010-10-03 18:51:39 +02003229 unsigned i;
Paolo Bonzini4a381622021-04-08 08:10:25 -04003230 int r;
3231
3232 write_lock(&vcpu->kvm->mmu_lock);
3233 r = make_mmu_pages_available(vcpu);
3234 if (r < 0)
3235 goto out_unlock;
Joerg Roedel651dd372010-09-10 17:30:59 +02003236
Paolo Bonzini897218f2021-02-06 09:53:33 -05003237 if (is_tdp_mmu_enabled(vcpu->kvm)) {
Ben Gardon02c00b32020-10-14 20:26:44 +02003238 root = kvm_tdp_mmu_get_vcpu_root_hpa(vcpu);
Sean Christophersonb37233c2021-03-04 17:10:47 -08003239 mmu->root_hpa = root;
Ben Gardon02c00b32020-10-14 20:26:44 +02003240 } else if (shadow_root_level >= PT64_ROOT_4LEVEL) {
Sean Christopherson6e6ec582021-03-04 17:10:50 -08003241 root = mmu_alloc_root(vcpu, 0, 0, shadow_root_level, true);
Sean Christophersonb37233c2021-03-04 17:10:47 -08003242 mmu->root_hpa = root;
Sean Christopherson8123f262020-04-27 19:37:14 -07003243 } else if (shadow_root_level == PT32E_ROOT_LEVEL) {
Paolo Bonzini4a381622021-04-08 08:10:25 -04003244 if (WARN_ON_ONCE(!mmu->pae_root)) {
3245 r = -EIO;
3246 goto out_unlock;
3247 }
Sean Christopherson73ad1602021-03-04 17:11:01 -08003248
Joerg Roedel651dd372010-09-10 17:30:59 +02003249 for (i = 0; i < 4; ++i) {
Sean Christophersonc834e5e42021-03-09 14:42:06 -08003250 WARN_ON_ONCE(IS_VALID_PAE_ROOT(mmu->pae_root[i]));
Joerg Roedel651dd372010-09-10 17:30:59 +02003251
Sean Christopherson8123f262020-04-27 19:37:14 -07003252 root = mmu_alloc_root(vcpu, i << (30 - PAGE_SHIFT),
3253 i << 30, PT32_ROOT_LEVEL, true);
Sean Christopherson17e368d2021-03-04 17:10:54 -08003254 mmu->pae_root[i] = root | PT_PRESENT_MASK |
3255 shadow_me_mask;
Joerg Roedel651dd372010-09-10 17:30:59 +02003256 }
Sean Christophersonb37233c2021-03-04 17:10:47 -08003257 mmu->root_hpa = __pa(mmu->pae_root);
Sean Christopherson73ad1602021-03-04 17:11:01 -08003258 } else {
3259 WARN_ONCE(1, "Bad TDP root level = %d\n", shadow_root_level);
Paolo Bonzini4a381622021-04-08 08:10:25 -04003260 r = -EIO;
3261 goto out_unlock;
Sean Christopherson73ad1602021-03-04 17:11:01 -08003262 }
Sean Christopherson3651c7f2020-02-28 14:52:39 -08003263
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003264 /* root_pgd is ignored for direct MMUs. */
Sean Christophersonb37233c2021-03-04 17:10:47 -08003265 mmu->root_pgd = 0;
Paolo Bonzini4a381622021-04-08 08:10:25 -04003266out_unlock:
3267 write_unlock(&vcpu->kvm->mmu_lock);
3268 return r;
Joerg Roedel651dd372010-09-10 17:30:59 +02003269}
3270
3271static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08003272{
Sean Christophersonb37233c2021-03-04 17:10:47 -08003273 struct kvm_mmu *mmu = vcpu->arch.mmu;
Sean Christopherson6e0918a2021-03-04 17:10:51 -08003274 u64 pdptrs[4], pm_mask;
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003275 gfn_t root_gfn, root_pgd;
Sean Christopherson8123f262020-04-27 19:37:14 -07003276 hpa_t root;
Paolo Bonzini4a381622021-04-08 08:10:25 -04003277 unsigned i;
3278 int r;
Avi Kivity3bb65a22007-01-05 16:36:51 -08003279
Sean Christophersonb37233c2021-03-04 17:10:47 -08003280 root_pgd = mmu->get_guest_pgd(vcpu);
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003281 root_gfn = root_pgd >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003282
Joerg Roedel651dd372010-09-10 17:30:59 +02003283 if (mmu_check_root(vcpu, root_gfn))
3284 return 1;
3285
3286 /*
Paolo Bonzini4a381622021-04-08 08:10:25 -04003287 * On SVM, reading PDPTRs might access guest memory, which might fault
3288 * and thus might sleep. Grab the PDPTRs before acquiring mmu_lock.
3289 */
Sean Christopherson6e0918a2021-03-04 17:10:51 -08003290 if (mmu->root_level == PT32E_ROOT_LEVEL) {
3291 for (i = 0; i < 4; ++i) {
3292 pdptrs[i] = mmu->get_pdptr(vcpu, i);
3293 if (!(pdptrs[i] & PT_PRESENT_MASK))
3294 continue;
3295
3296 if (mmu_check_root(vcpu, pdptrs[i] >> PAGE_SHIFT))
3297 return 1;
3298 }
3299 }
3300
Paolo Bonzini4a381622021-04-08 08:10:25 -04003301 write_lock(&vcpu->kvm->mmu_lock);
3302 r = make_mmu_pages_available(vcpu);
3303 if (r < 0)
3304 goto out_unlock;
3305
Joerg Roedel651dd372010-09-10 17:30:59 +02003306 /*
3307 * Do we shadow a long mode page table? If so we need to
3308 * write-protect the guests page table root.
3309 */
Sean Christophersonb37233c2021-03-04 17:10:47 -08003310 if (mmu->root_level >= PT64_ROOT_4LEVEL) {
Sean Christopherson8123f262020-04-27 19:37:14 -07003311 root = mmu_alloc_root(vcpu, root_gfn, 0,
Sean Christophersonb37233c2021-03-04 17:10:47 -08003312 mmu->shadow_root_level, false);
Sean Christophersonb37233c2021-03-04 17:10:47 -08003313 mmu->root_hpa = root;
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003314 goto set_root_pgd;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003315 }
Joerg Roedelf87f9282010-09-02 17:29:45 +02003316
Paolo Bonzini4a381622021-04-08 08:10:25 -04003317 if (WARN_ON_ONCE(!mmu->pae_root)) {
3318 r = -EIO;
3319 goto out_unlock;
3320 }
Sean Christopherson73ad1602021-03-04 17:11:01 -08003321
Joerg Roedel651dd372010-09-10 17:30:59 +02003322 /*
3323 * We shadow a 32 bit page table. This may be a legacy 2-level
Joerg Roedel81407ca2010-09-10 17:31:00 +02003324 * or a PAE 3-level page table. In either case we need to be aware that
3325 * the shadow page table may be a PAE or a long mode page table.
Joerg Roedel651dd372010-09-10 17:30:59 +02003326 */
Sean Christopherson17e368d2021-03-04 17:10:54 -08003327 pm_mask = PT_PRESENT_MASK | shadow_me_mask;
Sean Christopherson748e52b2021-03-04 17:10:49 -08003328 if (mmu->shadow_root_level == PT64_ROOT_4LEVEL) {
Joerg Roedel81407ca2010-09-10 17:31:00 +02003329 pm_mask |= PT_ACCESSED_MASK | PT_WRITABLE_MASK | PT_USER_MASK;
3330
Sean Christopherson03ca4582021-05-05 13:42:21 -07003331 if (WARN_ON_ONCE(!mmu->pml4_root)) {
Paolo Bonzini4a381622021-04-08 08:10:25 -04003332 r = -EIO;
3333 goto out_unlock;
3334 }
Sean Christopherson73ad1602021-03-04 17:11:01 -08003335
Sean Christopherson03ca4582021-05-05 13:42:21 -07003336 mmu->pml4_root[0] = __pa(mmu->pae_root) | pm_mask;
Sean Christopherson04d45552021-03-04 17:10:46 -08003337 }
3338
Avi Kivity17ac10a2007-01-05 16:36:40 -08003339 for (i = 0; i < 4; ++i) {
Sean Christophersonc834e5e42021-03-09 14:42:06 -08003340 WARN_ON_ONCE(IS_VALID_PAE_ROOT(mmu->pae_root[i]));
Sean Christopherson6e6ec582021-03-04 17:10:50 -08003341
Sean Christophersonb37233c2021-03-04 17:10:47 -08003342 if (mmu->root_level == PT32E_ROOT_LEVEL) {
Sean Christopherson6e0918a2021-03-04 17:10:51 -08003343 if (!(pdptrs[i] & PT_PRESENT_MASK)) {
Sean Christophersonc834e5e42021-03-09 14:42:06 -08003344 mmu->pae_root[i] = INVALID_PAE_ROOT;
Avi Kivity417726a2007-04-12 17:35:58 +03003345 continue;
3346 }
Sean Christopherson6e0918a2021-03-04 17:10:51 -08003347 root_gfn = pdptrs[i] >> PAGE_SHIFT;
Eric Northup5a7388c2010-04-26 17:00:05 -07003348 }
Avi Kivity8facbbf2010-05-04 12:58:32 +03003349
Sean Christopherson8123f262020-04-27 19:37:14 -07003350 root = mmu_alloc_root(vcpu, root_gfn, i << 30,
3351 PT32_ROOT_LEVEL, false);
Sean Christophersonb37233c2021-03-04 17:10:47 -08003352 mmu->pae_root[i] = root | pm_mask;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003353 }
Joerg Roedel81407ca2010-09-10 17:31:00 +02003354
Sean Christophersonba0a1942021-03-04 17:10:48 -08003355 if (mmu->shadow_root_level == PT64_ROOT_4LEVEL)
Sean Christopherson03ca4582021-05-05 13:42:21 -07003356 mmu->root_hpa = __pa(mmu->pml4_root);
Sean Christophersonba0a1942021-03-04 17:10:48 -08003357 else
3358 mmu->root_hpa = __pa(mmu->pae_root);
Joerg Roedel81407ca2010-09-10 17:31:00 +02003359
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003360set_root_pgd:
Sean Christophersonb37233c2021-03-04 17:10:47 -08003361 mmu->root_pgd = root_pgd;
Paolo Bonzini4a381622021-04-08 08:10:25 -04003362out_unlock:
3363 write_unlock(&vcpu->kvm->mmu_lock);
Vitaly Kuznetsovad7dc692019-02-22 17:45:01 +01003364
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03003365 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08003366}
3367
Sean Christopherson748e52b2021-03-04 17:10:49 -08003368static int mmu_alloc_special_roots(struct kvm_vcpu *vcpu)
Joerg Roedel651dd372010-09-10 17:30:59 +02003369{
Sean Christopherson748e52b2021-03-04 17:10:49 -08003370 struct kvm_mmu *mmu = vcpu->arch.mmu;
Sean Christopherson03ca4582021-05-05 13:42:21 -07003371 u64 *pml4_root, *pae_root;
Sean Christopherson748e52b2021-03-04 17:10:49 -08003372
3373 /*
3374 * When shadowing 32-bit or PAE NPT with 64-bit NPT, the PML4 and PDP
3375 * tables are allocated and initialized at root creation as there is no
3376 * equivalent level in the guest's NPT to shadow. Allocate the tables
3377 * on demand, as running a 32-bit L1 VMM on 64-bit KVM is very rare.
3378 */
3379 if (mmu->direct_map || mmu->root_level >= PT64_ROOT_4LEVEL ||
3380 mmu->shadow_root_level < PT64_ROOT_4LEVEL)
3381 return 0;
3382
3383 /*
3384 * This mess only works with 4-level paging and needs to be updated to
3385 * work with 5-level paging.
3386 */
3387 if (WARN_ON_ONCE(mmu->shadow_root_level != PT64_ROOT_4LEVEL))
3388 return -EIO;
3389
Sean Christopherson03ca4582021-05-05 13:42:21 -07003390 if (mmu->pae_root && mmu->pml4_root)
Sean Christopherson748e52b2021-03-04 17:10:49 -08003391 return 0;
3392
3393 /*
3394 * The special roots should always be allocated in concert. Yell and
3395 * bail if KVM ends up in a state where only one of the roots is valid.
3396 */
Sean Christopherson03ca4582021-05-05 13:42:21 -07003397 if (WARN_ON_ONCE(!tdp_enabled || mmu->pae_root || mmu->pml4_root))
Sean Christopherson748e52b2021-03-04 17:10:49 -08003398 return -EIO;
3399
Sean Christopherson4a986232021-03-09 14:42:07 -08003400 /*
3401 * Unlike 32-bit NPT, the PDP table doesn't need to be in low mem, and
3402 * doesn't need to be decrypted.
3403 */
Sean Christopherson748e52b2021-03-04 17:10:49 -08003404 pae_root = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
3405 if (!pae_root)
3406 return -ENOMEM;
3407
Sean Christopherson03ca4582021-05-05 13:42:21 -07003408 pml4_root = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
3409 if (!pml4_root) {
Sean Christopherson748e52b2021-03-04 17:10:49 -08003410 free_page((unsigned long)pae_root);
3411 return -ENOMEM;
3412 }
3413
3414 mmu->pae_root = pae_root;
Sean Christopherson03ca4582021-05-05 13:42:21 -07003415 mmu->pml4_root = pml4_root;
Sean Christopherson748e52b2021-03-04 17:10:49 -08003416
3417 return 0;
Joerg Roedel651dd372010-09-10 17:30:59 +02003418}
3419
Junaid Shahid578e1c42018-06-27 14:59:05 -07003420void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003421{
3422 int i;
3423 struct kvm_mmu_page *sp;
3424
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003425 if (vcpu->arch.mmu->direct_map)
Joerg Roedel81407ca2010-09-10 17:31:00 +02003426 return;
3427
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003428 if (!VALID_PAGE(vcpu->arch.mmu->root_hpa))
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003429 return;
Xiao Guangrong69030742010-09-27 18:09:29 +08003430
David Matlack56f17dd2014-08-18 15:46:07 -07003431 vcpu_clear_mmio_info(vcpu, MMIO_GVA_ANY);
Junaid Shahid578e1c42018-06-27 14:59:05 -07003432
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003433 if (vcpu->arch.mmu->root_level >= PT64_ROOT_4LEVEL) {
3434 hpa_t root = vcpu->arch.mmu->root_hpa;
Sean Christophersone47c4ae2020-06-22 13:20:34 -07003435 sp = to_shadow_page(root);
Junaid Shahid578e1c42018-06-27 14:59:05 -07003436
3437 /*
3438 * Even if another CPU was marking the SP as unsync-ed
3439 * simultaneously, any guest page table changes are not
3440 * guaranteed to be visible anyway until this VCPU issues a TLB
3441 * flush strictly after those changes are made. We only need to
3442 * ensure that the other CPU sets these flags before any actual
3443 * changes to the page tables are made. The comments in
3444 * mmu_need_write_protect() describe what could go wrong if this
3445 * requirement isn't satisfied.
3446 */
3447 if (!smp_load_acquire(&sp->unsync) &&
3448 !smp_load_acquire(&sp->unsync_children))
3449 return;
3450
Ben Gardon531810c2021-02-02 10:57:24 -08003451 write_lock(&vcpu->kvm->mmu_lock);
Junaid Shahid578e1c42018-06-27 14:59:05 -07003452 kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC);
3453
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003454 mmu_sync_children(vcpu, sp);
Junaid Shahid578e1c42018-06-27 14:59:05 -07003455
Xiao Guangrong0375f7f2011-11-28 20:41:00 +08003456 kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
Ben Gardon531810c2021-02-02 10:57:24 -08003457 write_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003458 return;
3459 }
Junaid Shahid578e1c42018-06-27 14:59:05 -07003460
Ben Gardon531810c2021-02-02 10:57:24 -08003461 write_lock(&vcpu->kvm->mmu_lock);
Junaid Shahid578e1c42018-06-27 14:59:05 -07003462 kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC);
3463
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003464 for (i = 0; i < 4; ++i) {
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003465 hpa_t root = vcpu->arch.mmu->pae_root[i];
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003466
Sean Christophersonc834e5e42021-03-09 14:42:06 -08003467 if (IS_VALID_PAE_ROOT(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003468 root &= PT64_BASE_ADDR_MASK;
Sean Christophersone47c4ae2020-06-22 13:20:34 -07003469 sp = to_shadow_page(root);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003470 mmu_sync_children(vcpu, sp);
3471 }
3472 }
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003473
Junaid Shahid578e1c42018-06-27 14:59:05 -07003474 kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
Ben Gardon531810c2021-02-02 10:57:24 -08003475 write_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03003476}
3477
Sean Christopherson736c2912019-12-06 15:57:14 -08003478static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gpa_t vaddr,
Avi Kivityab9ae312010-11-22 17:53:26 +02003479 u32 access, struct x86_exception *exception)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003480{
Avi Kivityab9ae312010-11-22 17:53:26 +02003481 if (exception)
3482 exception->error_code = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003483 return vaddr;
3484}
3485
Sean Christopherson736c2912019-12-06 15:57:14 -08003486static gpa_t nonpaging_gva_to_gpa_nested(struct kvm_vcpu *vcpu, gpa_t vaddr,
Avi Kivityab9ae312010-11-22 17:53:26 +02003487 u32 access,
3488 struct x86_exception *exception)
Joerg Roedel6539e732010-09-10 17:30:50 +02003489{
Avi Kivityab9ae312010-11-22 17:53:26 +02003490 if (exception)
3491 exception->error_code = 0;
Paolo Bonzini54987b72014-09-02 13:23:06 +02003492 return vcpu->arch.nested_mmu.translate_gpa(vcpu, vaddr, access, exception);
Joerg Roedel6539e732010-09-10 17:30:50 +02003493}
3494
Xiao Guangrongd625b152015-08-05 12:04:25 +08003495static bool
3496__is_rsvd_bits_set(struct rsvd_bits_validate *rsvd_check, u64 pte, int level)
3497{
Sean Christophersonb5c3c1b2020-01-09 15:06:40 -08003498 int bit7 = (pte >> 7) & 1;
Xiao Guangrongd625b152015-08-05 12:04:25 +08003499
Sean Christophersonb5c3c1b2020-01-09 15:06:40 -08003500 return pte & rsvd_check->rsvd_bits_mask[bit7][level-1];
Xiao Guangrongd625b152015-08-05 12:04:25 +08003501}
3502
Sean Christophersonb5c3c1b2020-01-09 15:06:40 -08003503static bool __is_bad_mt_xwr(struct rsvd_bits_validate *rsvd_check, u64 pte)
Xiao Guangrongd625b152015-08-05 12:04:25 +08003504{
Sean Christophersonb5c3c1b2020-01-09 15:06:40 -08003505 return rsvd_check->bad_mt_xwr & BIT_ULL(pte & 0x3f);
Xiao Guangrongd625b152015-08-05 12:04:25 +08003506}
3507
Takuya Yoshikawaded58742016-02-22 17:23:40 +09003508static bool mmio_info_in_cache(struct kvm_vcpu *vcpu, u64 addr, bool direct)
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003509{
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02003510 /*
3511 * A nested guest cannot use the MMIO cache if it is using nested
3512 * page tables, because cr2 is a nGPA while the cache stores GPAs.
3513 */
3514 if (mmu_is_nested(vcpu))
3515 return false;
3516
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003517 if (direct)
3518 return vcpu_match_mmio_gpa(vcpu, addr);
3519
3520 return vcpu_match_mmio_gva(vcpu, addr);
3521}
3522
Ben Gardon95fb5b02020-10-14 11:26:58 -07003523/*
3524 * Return the level of the lowest level SPTE added to sptes.
3525 * That SPTE may be non-present.
3526 */
Sean Christopherson39b4d432020-12-17 16:31:37 -08003527static int get_walk(struct kvm_vcpu *vcpu, u64 addr, u64 *sptes, int *root_level)
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003528{
3529 struct kvm_shadow_walk_iterator iterator;
Sean Christopherson2aa078932020-12-17 16:31:36 -08003530 int leaf = -1;
Ben Gardon95fb5b02020-10-14 11:26:58 -07003531 u64 spte;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003532
3533 walk_shadow_page_lockless_begin(vcpu);
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003534
Sean Christopherson39b4d432020-12-17 16:31:37 -08003535 for (shadow_walk_init(&iterator, vcpu, addr),
3536 *root_level = iterator.level;
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003537 shadow_walk_okay(&iterator);
3538 __shadow_walk_next(&iterator, spte)) {
Ben Gardon95fb5b02020-10-14 11:26:58 -07003539 leaf = iterator.level;
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003540 spte = mmu_spte_get_lockless(iterator.sptep);
3541
Sean Christophersondde81f92020-12-17 16:31:38 -08003542 sptes[leaf] = spte;
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003543
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003544 if (!is_shadow_present_pte(spte))
3545 break;
Ben Gardon95fb5b02020-10-14 11:26:58 -07003546 }
3547
3548 walk_shadow_page_lockless_end(vcpu);
3549
3550 return leaf;
3551}
3552
Sean Christopherson9aa41872020-12-17 16:31:39 -08003553/* return true if reserved bit(s) are detected on a valid, non-MMIO SPTE. */
Ben Gardon95fb5b02020-10-14 11:26:58 -07003554static bool get_mmio_spte(struct kvm_vcpu *vcpu, u64 addr, u64 *sptep)
3555{
Sean Christophersondde81f92020-12-17 16:31:38 -08003556 u64 sptes[PT64_ROOT_MAX_LEVEL + 1];
Ben Gardon95fb5b02020-10-14 11:26:58 -07003557 struct rsvd_bits_validate *rsvd_check;
Sean Christopherson39b4d432020-12-17 16:31:37 -08003558 int root, leaf, level;
Ben Gardon95fb5b02020-10-14 11:26:58 -07003559 bool reserved = false;
3560
3561 if (!VALID_PAGE(vcpu->arch.mmu->root_hpa)) {
3562 *sptep = 0ull;
3563 return reserved;
3564 }
3565
3566 if (is_tdp_mmu_root(vcpu->kvm, vcpu->arch.mmu->root_hpa))
Sean Christopherson39b4d432020-12-17 16:31:37 -08003567 leaf = kvm_tdp_mmu_get_walk(vcpu, addr, sptes, &root);
Ben Gardon95fb5b02020-10-14 11:26:58 -07003568 else
Sean Christopherson39b4d432020-12-17 16:31:37 -08003569 leaf = get_walk(vcpu, addr, sptes, &root);
Ben Gardon95fb5b02020-10-14 11:26:58 -07003570
Sean Christopherson2aa078932020-12-17 16:31:36 -08003571 if (unlikely(leaf < 0)) {
3572 *sptep = 0ull;
3573 return reserved;
3574 }
3575
Sean Christopherson9aa41872020-12-17 16:31:39 -08003576 *sptep = sptes[leaf];
3577
3578 /*
3579 * Skip reserved bits checks on the terminal leaf if it's not a valid
3580 * SPTE. Note, this also (intentionally) skips MMIO SPTEs, which, by
3581 * design, always have reserved bits set. The purpose of the checks is
3582 * to detect reserved bits on non-MMIO SPTEs. i.e. buggy SPTEs.
3583 */
3584 if (!is_shadow_present_pte(sptes[leaf]))
3585 leaf++;
Ben Gardon95fb5b02020-10-14 11:26:58 -07003586
3587 rsvd_check = &vcpu->arch.mmu->shadow_zero_check;
3588
Sean Christopherson9aa41872020-12-17 16:31:39 -08003589 for (level = root; level >= leaf; level--)
Sean Christophersonb5c3c1b2020-01-09 15:06:40 -08003590 /*
3591 * Use a bitwise-OR instead of a logical-OR to aggregate the
3592 * reserved bit and EPT's invalid memtype/XWR checks to avoid
3593 * adding a Jcc in the loop.
3594 */
Sean Christophersondde81f92020-12-17 16:31:38 -08003595 reserved |= __is_bad_mt_xwr(rsvd_check, sptes[level]) |
3596 __is_rsvd_bits_set(rsvd_check, sptes[level], level);
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003597
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003598 if (reserved) {
Sean Christophersonbb4cdf32021-02-25 12:47:49 -08003599 pr_err("%s: reserved bits set on MMU-present spte, addr 0x%llx, hierarchy:\n",
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003600 __func__, addr);
Ben Gardon95fb5b02020-10-14 11:26:58 -07003601 for (level = root; level >= leaf; level--)
Sean Christophersonbb4cdf32021-02-25 12:47:49 -08003602 pr_err("------ spte = 0x%llx level = %d, rsvd bits = 0x%llx",
3603 sptes[level], level,
3604 rsvd_check->rsvd_bits_mask[(sptes[level] >> 7) & 1][level-1]);
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003605 }
Sean Christophersonddce6202019-12-06 15:57:27 -08003606
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003607 return reserved;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003608}
3609
Paolo Bonzinie08d26f2017-08-17 18:36:56 +02003610static int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr, bool direct)
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003611{
3612 u64 spte;
Xiao Guangrong47ab8752015-08-05 12:04:26 +08003613 bool reserved;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003614
Takuya Yoshikawaded58742016-02-22 17:23:40 +09003615 if (mmio_info_in_cache(vcpu, addr, direct))
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02003616 return RET_PF_EMULATE;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003617
Ben Gardon95fb5b02020-10-14 11:26:58 -07003618 reserved = get_mmio_spte(vcpu, addr, &spte);
Paolo Bonzini450869d2015-11-04 13:41:21 +01003619 if (WARN_ON(reserved))
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02003620 return -EINVAL;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003621
3622 if (is_mmio_spte(spte)) {
3623 gfn_t gfn = get_mmio_spte_gfn(spte);
Ben Gardon0a2b64c2020-02-03 15:09:09 -08003624 unsigned int access = get_mmio_spte_access(spte);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003625
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003626 if (!check_mmio_spte(vcpu, spte))
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02003627 return RET_PF_INVALID;
Xiao Guangrongf8f55942013-06-07 16:51:26 +08003628
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003629 if (direct)
3630 addr = 0;
Xiao Guangrong4f022642011-07-12 03:34:24 +08003631
3632 trace_handle_mmio_page_fault(addr, gfn, access);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003633 vcpu_cache_mmio_info(vcpu, addr, gfn, access);
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02003634 return RET_PF_EMULATE;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003635 }
3636
3637 /*
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003638 * If the page table is zapped by other cpus, let CPU fault again on
3639 * the address.
3640 */
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02003641 return RET_PF_RETRY;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003642}
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003643
Xiao Guangrong3d0c27a2016-02-24 17:51:11 +08003644static bool page_fault_handle_page_track(struct kvm_vcpu *vcpu,
3645 u32 error_code, gfn_t gfn)
3646{
3647 if (unlikely(error_code & PFERR_RSVD_MASK))
3648 return false;
3649
3650 if (!(error_code & PFERR_PRESENT_MASK) ||
3651 !(error_code & PFERR_WRITE_MASK))
3652 return false;
3653
3654 /*
3655 * guest is writing the page which is write tracked which can
3656 * not be fixed by page fault handler.
3657 */
3658 if (kvm_page_track_is_active(vcpu, gfn, KVM_PAGE_TRACK_WRITE))
3659 return true;
3660
3661 return false;
3662}
3663
Xiao Guangronge5691a82016-02-24 17:51:12 +08003664static void shadow_page_table_clear_flood(struct kvm_vcpu *vcpu, gva_t addr)
3665{
3666 struct kvm_shadow_walk_iterator iterator;
3667 u64 spte;
3668
Xiao Guangronge5691a82016-02-24 17:51:12 +08003669 walk_shadow_page_lockless_begin(vcpu);
3670 for_each_shadow_entry_lockless(vcpu, addr, iterator, spte) {
3671 clear_sp_write_flooding_count(iterator.sptep);
3672 if (!is_shadow_present_pte(spte))
3673 break;
3674 }
3675 walk_shadow_page_lockless_end(vcpu);
3676}
3677
Vitaly Kuznetsove8c22262020-06-15 14:13:34 +02003678static bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
3679 gfn_t gfn)
Gleb Natapovaf585b92010-10-14 11:22:46 +02003680{
3681 struct kvm_arch_async_pf arch;
Xiao Guangrongfb67e142010-12-07 10:35:25 +08003682
Gleb Natapov7c907052010-10-14 11:22:53 +02003683 arch.token = (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id;
Gleb Natapovaf585b92010-10-14 11:22:46 +02003684 arch.gfn = gfn;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003685 arch.direct_map = vcpu->arch.mmu->direct_map;
Sean Christophersond8dd54e2020-03-02 18:02:39 -08003686 arch.cr3 = vcpu->arch.mmu->get_guest_pgd(vcpu);
Gleb Natapovaf585b92010-10-14 11:22:46 +02003687
Sean Christopherson9f1a8522019-12-06 15:57:17 -08003688 return kvm_setup_async_pf(vcpu, cr2_or_gpa,
3689 kvm_vcpu_gfn_to_hva(vcpu, gfn), &arch);
Gleb Natapovaf585b92010-10-14 11:22:46 +02003690}
3691
Xiao Guangrong78b2c542010-12-07 10:48:06 +08003692static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
David Stevens4a42d842021-02-22 11:45:22 +09003693 gpa_t cr2_or_gpa, kvm_pfn_t *pfn, hva_t *hva,
3694 bool write, bool *writable)
Gleb Natapovaf585b92010-10-14 11:22:46 +02003695{
Paolo Bonzinic36b7152020-04-16 09:48:07 -04003696 struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
Gleb Natapovaf585b92010-10-14 11:22:46 +02003697 bool async;
3698
Sean Christophersone0c37862021-02-25 12:47:30 -08003699 /*
3700 * Retry the page fault if the gfn hit a memslot that is being deleted
3701 * or moved. This ensures any existing SPTEs for the old memslot will
3702 * be zapped before KVM inserts a new MMIO SPTE for the gfn.
3703 */
3704 if (slot && (slot->flags & KVM_MEMSLOT_INVALID))
3705 return true;
3706
Paolo Bonzinic36b7152020-04-16 09:48:07 -04003707 /* Don't expose private memslots to L2. */
3708 if (is_guest_mode(vcpu) && !kvm_is_visible_memslot(slot)) {
Jim Mattson3a2936d2018-05-09 17:02:05 -04003709 *pfn = KVM_PFN_NOSLOT;
Sean Christophersonc583eed2020-04-15 14:44:13 -07003710 *writable = false;
Jim Mattson3a2936d2018-05-09 17:02:05 -04003711 return false;
3712 }
3713
Paolo Bonzini35204692015-04-02 11:20:48 +02003714 async = false;
David Stevens4a42d842021-02-22 11:45:22 +09003715 *pfn = __gfn_to_pfn_memslot(slot, gfn, false, &async,
3716 write, writable, hva);
Gleb Natapovaf585b92010-10-14 11:22:46 +02003717 if (!async)
3718 return false; /* *pfn has correct page already */
3719
Wanpeng Li9bc1f092017-06-08 20:13:40 -07003720 if (!prefault && kvm_can_do_async_pf(vcpu)) {
Sean Christopherson9f1a8522019-12-06 15:57:17 -08003721 trace_kvm_try_async_get_page(cr2_or_gpa, gfn);
Gleb Natapovaf585b92010-10-14 11:22:46 +02003722 if (kvm_find_async_pf_gfn(vcpu, gfn)) {
Sean Christopherson9f1a8522019-12-06 15:57:17 -08003723 trace_kvm_async_pf_doublefault(cr2_or_gpa, gfn);
Gleb Natapovaf585b92010-10-14 11:22:46 +02003724 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
3725 return true;
Sean Christopherson9f1a8522019-12-06 15:57:17 -08003726 } else if (kvm_arch_setup_async_pf(vcpu, cr2_or_gpa, gfn))
Gleb Natapovaf585b92010-10-14 11:22:46 +02003727 return true;
3728 }
3729
David Stevens4a42d842021-02-22 11:45:22 +09003730 *pfn = __gfn_to_pfn_memslot(slot, gfn, false, NULL,
3731 write, writable, hva);
Gleb Natapovaf585b92010-10-14 11:22:46 +02003732 return false;
3733}
3734
Sean Christopherson0f90e1c2019-12-06 15:57:24 -08003735static int direct_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code,
3736 bool prefault, int max_level, bool is_tdp)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003737{
Sean Christopherson367fd792019-12-06 15:57:16 -08003738 bool write = error_code & PFERR_WRITE_MASK;
Sean Christopherson0f90e1c2019-12-06 15:57:24 -08003739 bool map_writable;
Avi Kivityebeace82007-01-05 16:36:47 -08003740
Sean Christopherson0f90e1c2019-12-06 15:57:24 -08003741 gfn_t gfn = gpa >> PAGE_SHIFT;
3742 unsigned long mmu_seq;
3743 kvm_pfn_t pfn;
David Stevens4a42d842021-02-22 11:45:22 +09003744 hva_t hva;
Sean Christopherson83f06fa2020-01-08 12:24:43 -08003745 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003746
3747 if (page_fault_handle_page_track(vcpu, error_code, gfn))
3748 return RET_PF_EMULATE;
3749
Ben Gardonbb188422020-10-14 11:26:50 -07003750 if (!is_tdp_mmu_root(vcpu->kvm, vcpu->arch.mmu->root_hpa)) {
3751 r = fast_page_fault(vcpu, gpa, error_code);
3752 if (r != RET_PF_INVALID)
3753 return r;
3754 }
Sean Christopherson83291442020-07-02 19:35:30 -07003755
Sean Christopherson378f5cd2020-07-02 19:35:36 -07003756 r = mmu_topup_memory_caches(vcpu, false);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003757 if (r)
3758 return r;
3759
Sean Christopherson367fd792019-12-06 15:57:16 -08003760 mmu_seq = vcpu->kvm->mmu_notifier_seq;
3761 smp_rmb();
3762
David Stevens4a42d842021-02-22 11:45:22 +09003763 if (try_async_pf(vcpu, prefault, gfn, gpa, &pfn, &hva,
3764 write, &map_writable))
Sean Christopherson367fd792019-12-06 15:57:16 -08003765 return RET_PF_RETRY;
3766
Sean Christopherson0f90e1c2019-12-06 15:57:24 -08003767 if (handle_abnormal_pfn(vcpu, is_tdp ? 0 : gpa, gfn, pfn, ACC_ALL, &r))
Sean Christopherson367fd792019-12-06 15:57:16 -08003768 return r;
3769
3770 r = RET_PF_RETRY;
Ben Gardona2855af2021-02-02 10:57:29 -08003771
3772 if (is_tdp_mmu_root(vcpu->kvm, vcpu->arch.mmu->root_hpa))
3773 read_lock(&vcpu->kvm->mmu_lock);
3774 else
3775 write_lock(&vcpu->kvm->mmu_lock);
3776
David Stevens4a42d842021-02-22 11:45:22 +09003777 if (!is_noslot_pfn(pfn) && mmu_notifier_retry_hva(vcpu->kvm, mmu_seq, hva))
Sean Christopherson367fd792019-12-06 15:57:16 -08003778 goto out_unlock;
Sean Christopherson7bd7ded2020-06-23 12:35:42 -07003779 r = make_mmu_pages_available(vcpu);
3780 if (r)
Sean Christopherson367fd792019-12-06 15:57:16 -08003781 goto out_unlock;
Ben Gardonbb188422020-10-14 11:26:50 -07003782
3783 if (is_tdp_mmu_root(vcpu->kvm, vcpu->arch.mmu->root_hpa))
3784 r = kvm_tdp_mmu_map(vcpu, gpa, error_code, map_writable, max_level,
3785 pfn, prefault);
3786 else
3787 r = __direct_map(vcpu, gpa, error_code, map_writable, max_level, pfn,
3788 prefault, is_tdp);
Sean Christopherson0f90e1c2019-12-06 15:57:24 -08003789
Sean Christopherson367fd792019-12-06 15:57:16 -08003790out_unlock:
Ben Gardona2855af2021-02-02 10:57:29 -08003791 if (is_tdp_mmu_root(vcpu->kvm, vcpu->arch.mmu->root_hpa))
3792 read_unlock(&vcpu->kvm->mmu_lock);
3793 else
3794 write_unlock(&vcpu->kvm->mmu_lock);
Sean Christopherson367fd792019-12-06 15:57:16 -08003795 kvm_release_pfn_clean(pfn);
3796 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003797}
3798
Sean Christopherson0f90e1c2019-12-06 15:57:24 -08003799static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa,
3800 u32 error_code, bool prefault)
3801{
3802 pgprintk("%s: gva %lx error %x\n", __func__, gpa, error_code);
3803
3804 /* This path builds a PAE pagetable, we can map 2mb pages at maximum. */
3805 return direct_page_fault(vcpu, gpa & PAGE_MASK, error_code, prefault,
Sean Christopherson3bae0452020-04-27 17:54:22 -07003806 PG_LEVEL_2M, false);
Sean Christopherson0f90e1c2019-12-06 15:57:24 -08003807}
3808
Wanpeng Li1261bfa2017-07-13 18:30:40 -07003809int kvm_handle_page_fault(struct kvm_vcpu *vcpu, u64 error_code,
Paolo Bonzinid0006532017-08-11 18:36:43 +02003810 u64 fault_address, char *insn, int insn_len)
Wanpeng Li1261bfa2017-07-13 18:30:40 -07003811{
3812 int r = 1;
Vitaly Kuznetsov9ce372b2020-05-07 16:36:02 +02003813 u32 flags = vcpu->arch.apf.host_apf_flags;
Wanpeng Li1261bfa2017-07-13 18:30:40 -07003814
Sean Christopherson736c2912019-12-06 15:57:14 -08003815#ifndef CONFIG_X86_64
3816 /* A 64-bit CR2 should be impossible on 32-bit KVM. */
3817 if (WARN_ON_ONCE(fault_address >> 32))
3818 return -EFAULT;
3819#endif
3820
Paolo Bonzinic595cee2018-07-02 13:07:14 +02003821 vcpu->arch.l1tf_flush_l1d = true;
Vitaly Kuznetsov9ce372b2020-05-07 16:36:02 +02003822 if (!flags) {
Wanpeng Li1261bfa2017-07-13 18:30:40 -07003823 trace_kvm_page_fault(fault_address, error_code);
3824
Paolo Bonzinid0006532017-08-11 18:36:43 +02003825 if (kvm_event_needs_reinjection(vcpu))
Wanpeng Li1261bfa2017-07-13 18:30:40 -07003826 kvm_mmu_unprotect_page_virt(vcpu, fault_address);
3827 r = kvm_mmu_page_fault(vcpu, fault_address, error_code, insn,
3828 insn_len);
Vitaly Kuznetsov9ce372b2020-05-07 16:36:02 +02003829 } else if (flags & KVM_PV_REASON_PAGE_NOT_PRESENT) {
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02003830 vcpu->arch.apf.host_apf_flags = 0;
Wanpeng Li1261bfa2017-07-13 18:30:40 -07003831 local_irq_disable();
Thomas Gleixner6bca69a2020-03-07 00:42:06 +01003832 kvm_async_pf_task_wait_schedule(fault_address);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07003833 local_irq_enable();
Vitaly Kuznetsov9ce372b2020-05-07 16:36:02 +02003834 } else {
3835 WARN_ONCE(1, "Unexpected host async PF flags: %x\n", flags);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07003836 }
Vitaly Kuznetsov9ce372b2020-05-07 16:36:02 +02003837
Wanpeng Li1261bfa2017-07-13 18:30:40 -07003838 return r;
3839}
3840EXPORT_SYMBOL_GPL(kvm_handle_page_fault);
3841
Sean Christopherson7a026742020-02-06 14:14:34 -08003842int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code,
3843 bool prefault)
Joerg Roedelfb72d162008-02-07 13:47:44 +01003844{
Sean Christophersoncb9b88c2019-12-06 15:57:18 -08003845 int max_level;
Joerg Roedelfb72d162008-02-07 13:47:44 +01003846
Sean Christophersone662ec32020-04-27 17:54:21 -07003847 for (max_level = KVM_MAX_HUGEPAGE_LEVEL;
Sean Christopherson3bae0452020-04-27 17:54:22 -07003848 max_level > PG_LEVEL_4K;
Sean Christophersoncb9b88c2019-12-06 15:57:18 -08003849 max_level--) {
3850 int page_num = KVM_PAGES_PER_HPAGE(max_level);
Sean Christopherson0f90e1c2019-12-06 15:57:24 -08003851 gfn_t base = (gpa >> PAGE_SHIFT) & ~(page_num - 1);
Joerg Roedelfb72d162008-02-07 13:47:44 +01003852
Sean Christophersoncb9b88c2019-12-06 15:57:18 -08003853 if (kvm_mtrr_check_gfn_range_consistency(vcpu, base, page_num))
3854 break;
Takuya Yoshikawafd136902015-10-16 17:06:02 +09003855 }
Joerg Roedel852e3c12009-07-27 16:30:44 +02003856
Sean Christopherson0f90e1c2019-12-06 15:57:24 -08003857 return direct_page_fault(vcpu, gpa, error_code, prefault,
3858 max_level, true);
Joerg Roedelfb72d162008-02-07 13:47:44 +01003859}
3860
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02003861static void nonpaging_init_context(struct kvm_vcpu *vcpu,
3862 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003863{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003864 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003865 context->gva_to_gpa = nonpaging_gva_to_gpa;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03003866 context->sync_page = nonpaging_sync_page;
Paolo Bonzini5efac072020-03-23 20:42:57 -04003867 context->invlpg = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08003868 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003869 context->shadow_root_level = PT32E_ROOT_LEVEL;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02003870 context->direct_map = true;
Joerg Roedel2d48a982010-09-10 17:31:01 +02003871 context->nx = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003872}
3873
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003874static inline bool is_root_usable(struct kvm_mmu_root_info *root, gpa_t pgd,
Sean Christopherson0be44352020-02-28 14:52:40 -08003875 union kvm_mmu_page_role role)
3876{
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003877 return (role.direct || pgd == root->pgd) &&
Sean Christophersone47c4ae2020-06-22 13:20:34 -07003878 VALID_PAGE(root->hpa) && to_shadow_page(root->hpa) &&
3879 role.word == to_shadow_page(root->hpa)->role.word;
Sean Christopherson0be44352020-02-28 14:52:40 -08003880}
3881
Junaid Shahidb94742c2018-06-27 14:59:20 -07003882/*
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003883 * Find out if a previously cached root matching the new pgd/role is available.
Junaid Shahidb94742c2018-06-27 14:59:20 -07003884 * The current root is also inserted into the cache.
3885 * If a matching root was found, it is assigned to kvm_mmu->root_hpa and true is
3886 * returned.
3887 * Otherwise, the LRU root from the cache is assigned to kvm_mmu->root_hpa and
3888 * false is returned. This root should now be freed by the caller.
3889 */
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003890static bool cached_root_available(struct kvm_vcpu *vcpu, gpa_t new_pgd,
Junaid Shahidb94742c2018-06-27 14:59:20 -07003891 union kvm_mmu_page_role new_role)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003892{
Junaid Shahidb94742c2018-06-27 14:59:20 -07003893 uint i;
3894 struct kvm_mmu_root_info root;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003895 struct kvm_mmu *mmu = vcpu->arch.mmu;
Junaid Shahidb94742c2018-06-27 14:59:20 -07003896
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003897 root.pgd = mmu->root_pgd;
Junaid Shahidb94742c2018-06-27 14:59:20 -07003898 root.hpa = mmu->root_hpa;
3899
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003900 if (is_root_usable(&root, new_pgd, new_role))
Sean Christopherson0be44352020-02-28 14:52:40 -08003901 return true;
3902
Junaid Shahidb94742c2018-06-27 14:59:20 -07003903 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
3904 swap(root, mmu->prev_roots[i]);
3905
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003906 if (is_root_usable(&root, new_pgd, new_role))
Junaid Shahidb94742c2018-06-27 14:59:20 -07003907 break;
3908 }
3909
3910 mmu->root_hpa = root.hpa;
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003911 mmu->root_pgd = root.pgd;
Junaid Shahidb94742c2018-06-27 14:59:20 -07003912
3913 return i < KVM_MMU_NUM_PREV_ROOTS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003914}
3915
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003916static bool fast_pgd_switch(struct kvm_vcpu *vcpu, gpa_t new_pgd,
Sean Christophersonb8698552020-03-20 14:28:26 -07003917 union kvm_mmu_page_role new_role)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003918{
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003919 struct kvm_mmu *mmu = vcpu->arch.mmu;
Junaid Shahid7c390d32018-06-27 14:59:06 -07003920
3921 /*
3922 * For now, limit the fast switch to 64-bit hosts+VMs in order to avoid
3923 * having to deal with PDPTEs. We may add support for 32-bit hosts/VMs
3924 * later if necessary.
3925 */
3926 if (mmu->shadow_root_level >= PT64_ROOT_4LEVEL &&
Sean Christophersonb8698552020-03-20 14:28:26 -07003927 mmu->root_level >= PT64_ROOT_4LEVEL)
Vitaly Kuznetsovfe9304d2020-07-10 16:11:57 +02003928 return cached_root_available(vcpu, new_pgd, new_role);
Junaid Shahid7c390d32018-06-27 14:59:06 -07003929
3930 return false;
3931}
3932
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003933static void __kvm_mmu_new_pgd(struct kvm_vcpu *vcpu, gpa_t new_pgd,
Junaid Shahidade61e22018-06-27 14:59:15 -07003934 union kvm_mmu_page_role new_role,
Sean Christopherson4a632ac2020-03-20 14:28:27 -07003935 bool skip_tlb_flush, bool skip_mmu_sync)
Junaid Shahid0aab33e2018-06-27 14:59:09 -07003936{
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003937 if (!fast_pgd_switch(vcpu, new_pgd, new_role)) {
Sean Christophersonb8698552020-03-20 14:28:26 -07003938 kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, KVM_MMU_ROOT_CURRENT);
3939 return;
3940 }
3941
3942 /*
3943 * It's possible that the cached previous root page is obsolete because
3944 * of a change in the MMU generation number. However, changing the
3945 * generation number is accompanied by KVM_REQ_MMU_RELOAD, which will
3946 * free the root set here and allocate a new one.
3947 */
3948 kvm_make_request(KVM_REQ_LOAD_MMU_PGD, vcpu);
3949
Sean Christopherson71fe7012020-03-20 14:28:28 -07003950 if (!skip_mmu_sync || force_flush_and_sync_on_reuse)
Sean Christophersonb8698552020-03-20 14:28:26 -07003951 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
Sean Christopherson71fe7012020-03-20 14:28:28 -07003952 if (!skip_tlb_flush || force_flush_and_sync_on_reuse)
Sean Christophersonb8698552020-03-20 14:28:26 -07003953 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Sean Christophersonb8698552020-03-20 14:28:26 -07003954
3955 /*
3956 * The last MMIO access's GVA and GPA are cached in the VCPU. When
3957 * switching to a new CR3, that GVA->GPA mapping may no longer be
3958 * valid. So clear any cached MMIO info even when we don't need to sync
3959 * the shadow page tables.
3960 */
3961 vcpu_clear_mmio_info(vcpu, MMIO_GVA_ANY);
3962
Ben Gardondaa5b6c2020-10-14 11:26:59 -07003963 /*
3964 * If this is a direct root page, it doesn't have a write flooding
3965 * count. Otherwise, clear the write flooding count.
3966 */
3967 if (!new_role.direct)
3968 __clear_sp_write_flooding_count(
3969 to_shadow_page(vcpu->arch.mmu->root_hpa));
Junaid Shahid0aab33e2018-06-27 14:59:09 -07003970}
3971
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003972void kvm_mmu_new_pgd(struct kvm_vcpu *vcpu, gpa_t new_pgd, bool skip_tlb_flush,
Sean Christopherson4a632ac2020-03-20 14:28:27 -07003973 bool skip_mmu_sync)
Junaid Shahid7c390d32018-06-27 14:59:06 -07003974{
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003975 __kvm_mmu_new_pgd(vcpu, new_pgd, kvm_mmu_calc_root_page_role(vcpu),
Sean Christopherson4a632ac2020-03-20 14:28:27 -07003976 skip_tlb_flush, skip_mmu_sync);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003977}
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07003978EXPORT_SYMBOL_GPL(kvm_mmu_new_pgd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003979
Joerg Roedel5777ed32010-09-10 17:30:42 +02003980static unsigned long get_cr3(struct kvm_vcpu *vcpu)
3981{
Avi Kivity9f8fe502010-12-05 17:30:00 +02003982 return kvm_read_cr3(vcpu);
Joerg Roedel5777ed32010-09-10 17:30:42 +02003983}
3984
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003985static bool sync_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, gfn_t gfn,
Ben Gardon0a2b64c2020-02-03 15:09:09 -08003986 unsigned int access, int *nr_present)
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003987{
3988 if (unlikely(is_mmio_spte(*sptep))) {
3989 if (gfn != get_mmio_spte_gfn(*sptep)) {
3990 mmu_spte_clear_no_track(sptep);
3991 return true;
3992 }
3993
3994 (*nr_present)++;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003995 mark_mmio_spte(vcpu, sptep, gfn, access);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003996 return true;
3997 }
3998
3999 return false;
4000}
4001
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004002static inline bool is_last_gpte(struct kvm_mmu *mmu,
4003 unsigned level, unsigned gpte)
Avi Kivity6fd01b72012-09-12 20:46:56 +03004004{
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004005 /*
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004006 * The RHS has bit 7 set iff level < mmu->last_nonleaf_level.
4007 * If it is clear, there are no large pages at this level, so clear
4008 * PT_PAGE_SIZE_MASK in gpte if that is the case.
4009 */
4010 gpte &= level - mmu->last_nonleaf_level;
4011
Ladi Prosek829ee272017-10-05 11:10:23 +02004012 /*
Sean Christopherson3bae0452020-04-27 17:54:22 -07004013 * PG_LEVEL_4K always terminates. The RHS has bit 7 set
4014 * iff level <= PG_LEVEL_4K, which for our purpose means
4015 * level == PG_LEVEL_4K; set PT_PAGE_SIZE_MASK in gpte then.
Ladi Prosek829ee272017-10-05 11:10:23 +02004016 */
Sean Christopherson3bae0452020-04-27 17:54:22 -07004017 gpte |= level - PG_LEVEL_4K - 1;
Ladi Prosek829ee272017-10-05 11:10:23 +02004018
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004019 return gpte & PT_PAGE_SIZE_MASK;
Avi Kivity6fd01b72012-09-12 20:46:56 +03004020}
4021
Nadav Har'El37406aa2013-08-05 11:07:12 +03004022#define PTTYPE_EPT 18 /* arbitrary */
4023#define PTTYPE PTTYPE_EPT
4024#include "paging_tmpl.h"
4025#undef PTTYPE
4026
Avi Kivity6aa8b732006-12-10 02:21:36 -08004027#define PTTYPE 64
4028#include "paging_tmpl.h"
4029#undef PTTYPE
4030
4031#define PTTYPE 32
4032#include "paging_tmpl.h"
4033#undef PTTYPE
4034
Xiao Guangrong6dc98b82015-08-05 12:04:22 +08004035static void
4036__reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
4037 struct rsvd_bits_validate *rsvd_check,
Sean Christopherson5b7f5752021-02-03 16:01:13 -08004038 u64 pa_bits_rsvd, int level, bool nx, bool gbpages,
Paolo Bonzini6fec2142015-09-22 23:02:14 +02004039 bool pse, bool amd)
Dong, Eddie82725b22009-03-30 16:21:08 +08004040{
Nadav Amit5f7dde72014-05-07 15:32:50 +03004041 u64 gbpages_bit_rsvd = 0;
Paolo Bonzinia0c0feb2014-09-02 13:24:12 +02004042 u64 nonleaf_bit8_rsvd = 0;
Sean Christopherson5b7f5752021-02-03 16:01:13 -08004043 u64 high_bits_rsvd;
Dong, Eddie82725b22009-03-30 16:21:08 +08004044
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004045 rsvd_check->bad_mt_xwr = 0;
Yang Zhang25d92082013-08-06 12:00:32 +03004046
Xiao Guangrong6dc98b82015-08-05 12:04:22 +08004047 if (!gbpages)
Nadav Amit5f7dde72014-05-07 15:32:50 +03004048 gbpages_bit_rsvd = rsvd_bits(7, 7);
Paolo Bonzinia0c0feb2014-09-02 13:24:12 +02004049
Sean Christopherson5b7f5752021-02-03 16:01:13 -08004050 if (level == PT32E_ROOT_LEVEL)
4051 high_bits_rsvd = pa_bits_rsvd & rsvd_bits(0, 62);
4052 else
4053 high_bits_rsvd = pa_bits_rsvd & rsvd_bits(0, 51);
4054
4055 /* Note, NX doesn't exist in PDPTEs, this is handled below. */
4056 if (!nx)
4057 high_bits_rsvd |= rsvd_bits(63, 63);
4058
Paolo Bonzinia0c0feb2014-09-02 13:24:12 +02004059 /*
4060 * Non-leaf PML4Es and PDPEs reserve bit 8 (which would be the G bit for
4061 * leaf entries) on AMD CPUs only.
4062 */
Paolo Bonzini6fec2142015-09-22 23:02:14 +02004063 if (amd)
Paolo Bonzinia0c0feb2014-09-02 13:24:12 +02004064 nonleaf_bit8_rsvd = rsvd_bits(8, 8);
4065
Xiao Guangrong6dc98b82015-08-05 12:04:22 +08004066 switch (level) {
Dong, Eddie82725b22009-03-30 16:21:08 +08004067 case PT32_ROOT_LEVEL:
4068 /* no rsvd bits for 2 level 4K page table entries */
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004069 rsvd_check->rsvd_bits_mask[0][1] = 0;
4070 rsvd_check->rsvd_bits_mask[0][0] = 0;
4071 rsvd_check->rsvd_bits_mask[1][0] =
4072 rsvd_check->rsvd_bits_mask[0][0];
Xiao Guangrongf815bce2010-03-19 17:58:53 +08004073
Xiao Guangrong6dc98b82015-08-05 12:04:22 +08004074 if (!pse) {
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004075 rsvd_check->rsvd_bits_mask[1][1] = 0;
Xiao Guangrongf815bce2010-03-19 17:58:53 +08004076 break;
4077 }
4078
Dong, Eddie82725b22009-03-30 16:21:08 +08004079 if (is_cpuid_PSE36())
4080 /* 36bits PSE 4MB page */
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004081 rsvd_check->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08004082 else
4083 /* 32 bits PSE 4MB page */
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004084 rsvd_check->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Dong, Eddie82725b22009-03-30 16:21:08 +08004085 break;
4086 case PT32E_ROOT_LEVEL:
Sean Christopherson5b7f5752021-02-03 16:01:13 -08004087 rsvd_check->rsvd_bits_mask[0][2] = rsvd_bits(63, 63) |
4088 high_bits_rsvd |
4089 rsvd_bits(5, 8) |
4090 rsvd_bits(1, 2); /* PDPTE */
4091 rsvd_check->rsvd_bits_mask[0][1] = high_bits_rsvd; /* PDE */
4092 rsvd_check->rsvd_bits_mask[0][0] = high_bits_rsvd; /* PTE */
4093 rsvd_check->rsvd_bits_mask[1][1] = high_bits_rsvd |
4094 rsvd_bits(13, 20); /* large page */
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004095 rsvd_check->rsvd_bits_mask[1][0] =
4096 rsvd_check->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08004097 break;
Yu Zhang855feb62017-08-24 20:27:55 +08004098 case PT64_ROOT_5LEVEL:
Sean Christopherson5b7f5752021-02-03 16:01:13 -08004099 rsvd_check->rsvd_bits_mask[0][4] = high_bits_rsvd |
4100 nonleaf_bit8_rsvd |
4101 rsvd_bits(7, 7);
Yu Zhang855feb62017-08-24 20:27:55 +08004102 rsvd_check->rsvd_bits_mask[1][4] =
4103 rsvd_check->rsvd_bits_mask[0][4];
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004104 fallthrough;
Yu Zhang2a7266a2017-08-24 20:27:54 +08004105 case PT64_ROOT_4LEVEL:
Sean Christopherson5b7f5752021-02-03 16:01:13 -08004106 rsvd_check->rsvd_bits_mask[0][3] = high_bits_rsvd |
4107 nonleaf_bit8_rsvd |
4108 rsvd_bits(7, 7);
4109 rsvd_check->rsvd_bits_mask[0][2] = high_bits_rsvd |
4110 gbpages_bit_rsvd;
4111 rsvd_check->rsvd_bits_mask[0][1] = high_bits_rsvd;
4112 rsvd_check->rsvd_bits_mask[0][0] = high_bits_rsvd;
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004113 rsvd_check->rsvd_bits_mask[1][3] =
4114 rsvd_check->rsvd_bits_mask[0][3];
Sean Christopherson5b7f5752021-02-03 16:01:13 -08004115 rsvd_check->rsvd_bits_mask[1][2] = high_bits_rsvd |
4116 gbpages_bit_rsvd |
4117 rsvd_bits(13, 29);
4118 rsvd_check->rsvd_bits_mask[1][1] = high_bits_rsvd |
4119 rsvd_bits(13, 20); /* large page */
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004120 rsvd_check->rsvd_bits_mask[1][0] =
4121 rsvd_check->rsvd_bits_mask[0][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08004122 break;
4123 }
4124}
4125
Xiao Guangrong6dc98b82015-08-05 12:04:22 +08004126static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
4127 struct kvm_mmu *context)
4128{
4129 __reset_rsvds_bits_mask(vcpu, &context->guest_rsvd_check,
Sean Christopherson5b7f5752021-02-03 16:01:13 -08004130 vcpu->arch.reserved_gpa_bits,
4131 context->root_level, context->nx,
Radim Krčmářd6321d42017-08-05 00:12:49 +02004132 guest_cpuid_has(vcpu, X86_FEATURE_GBPAGES),
Sean Christopherson23493d02020-03-04 17:34:33 -08004133 is_pse(vcpu),
4134 guest_cpuid_is_amd_or_hygon(vcpu));
Xiao Guangrong6dc98b82015-08-05 12:04:22 +08004135}
4136
Xiao Guangrong81b8eeb2015-08-05 12:04:23 +08004137static void
4138__reset_rsvds_bits_mask_ept(struct rsvd_bits_validate *rsvd_check,
Sean Christopherson5b7f5752021-02-03 16:01:13 -08004139 u64 pa_bits_rsvd, bool execonly)
Yang Zhang25d92082013-08-06 12:00:32 +03004140{
Sean Christopherson5b7f5752021-02-03 16:01:13 -08004141 u64 high_bits_rsvd = pa_bits_rsvd & rsvd_bits(0, 51);
Paolo Bonzini951f9fd2015-09-23 10:34:26 +02004142 u64 bad_mt_xwr;
Yang Zhang25d92082013-08-06 12:00:32 +03004143
Sean Christopherson5b7f5752021-02-03 16:01:13 -08004144 rsvd_check->rsvd_bits_mask[0][4] = high_bits_rsvd | rsvd_bits(3, 7);
4145 rsvd_check->rsvd_bits_mask[0][3] = high_bits_rsvd | rsvd_bits(3, 7);
4146 rsvd_check->rsvd_bits_mask[0][2] = high_bits_rsvd | rsvd_bits(3, 6);
4147 rsvd_check->rsvd_bits_mask[0][1] = high_bits_rsvd | rsvd_bits(3, 6);
4148 rsvd_check->rsvd_bits_mask[0][0] = high_bits_rsvd;
Yang Zhang25d92082013-08-06 12:00:32 +03004149
4150 /* large page */
Yu Zhang855feb62017-08-24 20:27:55 +08004151 rsvd_check->rsvd_bits_mask[1][4] = rsvd_check->rsvd_bits_mask[0][4];
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004152 rsvd_check->rsvd_bits_mask[1][3] = rsvd_check->rsvd_bits_mask[0][3];
Sean Christopherson5b7f5752021-02-03 16:01:13 -08004153 rsvd_check->rsvd_bits_mask[1][2] = high_bits_rsvd | rsvd_bits(12, 29);
4154 rsvd_check->rsvd_bits_mask[1][1] = high_bits_rsvd | rsvd_bits(12, 20);
Xiao Guangronga0a64f52015-08-05 12:04:21 +08004155 rsvd_check->rsvd_bits_mask[1][0] = rsvd_check->rsvd_bits_mask[0][0];
Yang Zhang25d92082013-08-06 12:00:32 +03004156
Paolo Bonzini951f9fd2015-09-23 10:34:26 +02004157 bad_mt_xwr = 0xFFull << (2 * 8); /* bits 3..5 must not be 2 */
4158 bad_mt_xwr |= 0xFFull << (3 * 8); /* bits 3..5 must not be 3 */
4159 bad_mt_xwr |= 0xFFull << (7 * 8); /* bits 3..5 must not be 7 */
4160 bad_mt_xwr |= REPEAT_BYTE(1ull << 2); /* bits 0..2 must not be 010 */
4161 bad_mt_xwr |= REPEAT_BYTE(1ull << 6); /* bits 0..2 must not be 110 */
4162 if (!execonly) {
4163 /* bits 0..2 must not be 100 unless VMX capabilities allow it */
4164 bad_mt_xwr |= REPEAT_BYTE(1ull << 4);
Yang Zhang25d92082013-08-06 12:00:32 +03004165 }
Paolo Bonzini951f9fd2015-09-23 10:34:26 +02004166 rsvd_check->bad_mt_xwr = bad_mt_xwr;
Yang Zhang25d92082013-08-06 12:00:32 +03004167}
4168
Xiao Guangrong81b8eeb2015-08-05 12:04:23 +08004169static void reset_rsvds_bits_mask_ept(struct kvm_vcpu *vcpu,
4170 struct kvm_mmu *context, bool execonly)
4171{
4172 __reset_rsvds_bits_mask_ept(&context->guest_rsvd_check,
Sean Christopherson5b7f5752021-02-03 16:01:13 -08004173 vcpu->arch.reserved_gpa_bits, execonly);
Xiao Guangrong81b8eeb2015-08-05 12:04:23 +08004174}
4175
Sean Christopherson6f8e65a2021-02-03 16:01:14 -08004176static inline u64 reserved_hpa_bits(void)
4177{
4178 return rsvd_bits(shadow_phys_bits, 63);
4179}
4180
Xiao Guangrongc258b622015-08-05 12:04:24 +08004181/*
4182 * the page table on host is the shadow page table for the page
4183 * table in guest or amd nested guest, its mmu features completely
4184 * follow the features in guest.
4185 */
4186void
4187reset_shadow_zero_bits_mask(struct kvm_vcpu *vcpu, struct kvm_mmu *context)
4188{
Vitaly Kuznetsov36d9594d2018-10-08 21:28:10 +02004189 bool uses_nx = context->nx ||
4190 context->mmu_role.base.smep_andnot_wp;
Brijesh Singhea2800d2017-08-25 15:55:40 -05004191 struct rsvd_bits_validate *shadow_zero_check;
4192 int i;
Paolo Bonzini5f0b8192016-03-09 14:28:02 +01004193
Paolo Bonzini6fec2142015-09-22 23:02:14 +02004194 /*
4195 * Passing "true" to the last argument is okay; it adds a check
4196 * on bit 8 of the SPTEs which KVM doesn't use anyway.
4197 */
Brijesh Singhea2800d2017-08-25 15:55:40 -05004198 shadow_zero_check = &context->shadow_zero_check;
4199 __reset_rsvds_bits_mask(vcpu, shadow_zero_check,
Sean Christopherson6f8e65a2021-02-03 16:01:14 -08004200 reserved_hpa_bits(),
Paolo Bonzini5f0b8192016-03-09 14:28:02 +01004201 context->shadow_root_level, uses_nx,
Radim Krčmářd6321d42017-08-05 00:12:49 +02004202 guest_cpuid_has(vcpu, X86_FEATURE_GBPAGES),
4203 is_pse(vcpu), true);
Brijesh Singhea2800d2017-08-25 15:55:40 -05004204
4205 if (!shadow_me_mask)
4206 return;
4207
4208 for (i = context->shadow_root_level; --i >= 0;) {
4209 shadow_zero_check->rsvd_bits_mask[0][i] &= ~shadow_me_mask;
4210 shadow_zero_check->rsvd_bits_mask[1][i] &= ~shadow_me_mask;
4211 }
4212
Xiao Guangrongc258b622015-08-05 12:04:24 +08004213}
4214EXPORT_SYMBOL_GPL(reset_shadow_zero_bits_mask);
4215
Paolo Bonzini6fec2142015-09-22 23:02:14 +02004216static inline bool boot_cpu_is_amd(void)
4217{
4218 WARN_ON_ONCE(!tdp_enabled);
4219 return shadow_x_mask == 0;
4220}
4221
Xiao Guangrongc258b622015-08-05 12:04:24 +08004222/*
4223 * the direct page table on host, use as much mmu features as
4224 * possible, however, kvm currently does not do execution-protection.
4225 */
4226static void
4227reset_tdp_shadow_zero_bits_mask(struct kvm_vcpu *vcpu,
4228 struct kvm_mmu *context)
4229{
Brijesh Singhea2800d2017-08-25 15:55:40 -05004230 struct rsvd_bits_validate *shadow_zero_check;
4231 int i;
4232
4233 shadow_zero_check = &context->shadow_zero_check;
4234
Paolo Bonzini6fec2142015-09-22 23:02:14 +02004235 if (boot_cpu_is_amd())
Brijesh Singhea2800d2017-08-25 15:55:40 -05004236 __reset_rsvds_bits_mask(vcpu, shadow_zero_check,
Sean Christopherson6f8e65a2021-02-03 16:01:14 -08004237 reserved_hpa_bits(),
Xiao Guangrongc258b622015-08-05 12:04:24 +08004238 context->shadow_root_level, false,
Borislav Petkovb8291adc2016-03-29 17:41:58 +02004239 boot_cpu_has(X86_FEATURE_GBPAGES),
4240 true, true);
Xiao Guangrongc258b622015-08-05 12:04:24 +08004241 else
Brijesh Singhea2800d2017-08-25 15:55:40 -05004242 __reset_rsvds_bits_mask_ept(shadow_zero_check,
Sean Christopherson6f8e65a2021-02-03 16:01:14 -08004243 reserved_hpa_bits(), false);
Xiao Guangrongc258b622015-08-05 12:04:24 +08004244
Brijesh Singhea2800d2017-08-25 15:55:40 -05004245 if (!shadow_me_mask)
4246 return;
4247
4248 for (i = context->shadow_root_level; --i >= 0;) {
4249 shadow_zero_check->rsvd_bits_mask[0][i] &= ~shadow_me_mask;
4250 shadow_zero_check->rsvd_bits_mask[1][i] &= ~shadow_me_mask;
4251 }
Xiao Guangrongc258b622015-08-05 12:04:24 +08004252}
4253
4254/*
4255 * as the comments in reset_shadow_zero_bits_mask() except it
4256 * is the shadow page table for intel nested guest.
4257 */
4258static void
4259reset_ept_shadow_zero_bits_mask(struct kvm_vcpu *vcpu,
4260 struct kvm_mmu *context, bool execonly)
4261{
4262 __reset_rsvds_bits_mask_ept(&context->shadow_zero_check,
Sean Christopherson6f8e65a2021-02-03 16:01:14 -08004263 reserved_hpa_bits(), execonly);
Xiao Guangrongc258b622015-08-05 12:04:24 +08004264}
4265
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004266#define BYTE_MASK(access) \
4267 ((1 & (access) ? 2 : 0) | \
4268 (2 & (access) ? 4 : 0) | \
4269 (3 & (access) ? 8 : 0) | \
4270 (4 & (access) ? 16 : 0) | \
4271 (5 & (access) ? 32 : 0) | \
4272 (6 & (access) ? 64 : 0) | \
4273 (7 & (access) ? 128 : 0))
4274
4275
Xiao Guangrongedc90b72015-05-11 22:55:21 +08004276static void update_permission_bitmask(struct kvm_vcpu *vcpu,
4277 struct kvm_mmu *mmu, bool ept)
Avi Kivity97d64b72012-09-12 14:52:00 +03004278{
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004279 unsigned byte;
Avi Kivity97d64b72012-09-12 14:52:00 +03004280
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004281 const u8 x = BYTE_MASK(ACC_EXEC_MASK);
4282 const u8 w = BYTE_MASK(ACC_WRITE_MASK);
4283 const u8 u = BYTE_MASK(ACC_USER_MASK);
4284
4285 bool cr4_smep = kvm_read_cr4_bits(vcpu, X86_CR4_SMEP) != 0;
4286 bool cr4_smap = kvm_read_cr4_bits(vcpu, X86_CR4_SMAP) != 0;
4287 bool cr0_wp = is_write_protection(vcpu);
4288
Avi Kivity97d64b72012-09-12 14:52:00 +03004289 for (byte = 0; byte < ARRAY_SIZE(mmu->permissions); ++byte) {
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004290 unsigned pfec = byte << 1;
4291
Feng Wu97ec8c02014-04-01 17:46:34 +08004292 /*
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004293 * Each "*f" variable has a 1 bit for each UWX value
4294 * that causes a fault with the given PFEC.
Feng Wu97ec8c02014-04-01 17:46:34 +08004295 */
Avi Kivity97d64b72012-09-12 14:52:00 +03004296
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004297 /* Faults from writes to non-writable pages */
Arnd Bergmanna6a6d3b2019-07-12 11:12:30 +02004298 u8 wf = (pfec & PFERR_WRITE_MASK) ? (u8)~w : 0;
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004299 /* Faults from user mode accesses to supervisor pages */
Arnd Bergmanna6a6d3b2019-07-12 11:12:30 +02004300 u8 uf = (pfec & PFERR_USER_MASK) ? (u8)~u : 0;
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004301 /* Faults from fetches of non-executable pages*/
Arnd Bergmanna6a6d3b2019-07-12 11:12:30 +02004302 u8 ff = (pfec & PFERR_FETCH_MASK) ? (u8)~x : 0;
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004303 /* Faults from kernel mode fetches of user pages */
4304 u8 smepf = 0;
4305 /* Faults from kernel mode accesses of user pages */
4306 u8 smapf = 0;
Feng Wu97ec8c02014-04-01 17:46:34 +08004307
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004308 if (!ept) {
4309 /* Faults from kernel mode accesses to user pages */
4310 u8 kf = (pfec & PFERR_USER_MASK) ? 0 : u;
Avi Kivity97d64b72012-09-12 14:52:00 +03004311
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004312 /* Not really needed: !nx will cause pte.nx to fault */
4313 if (!mmu->nx)
4314 ff = 0;
4315
4316 /* Allow supervisor writes if !cr0.wp */
4317 if (!cr0_wp)
4318 wf = (pfec & PFERR_USER_MASK) ? wf : 0;
4319
4320 /* Disallow supervisor fetches of user code if cr4.smep */
4321 if (cr4_smep)
4322 smepf = (pfec & PFERR_FETCH_MASK) ? kf : 0;
4323
4324 /*
4325 * SMAP:kernel-mode data accesses from user-mode
4326 * mappings should fault. A fault is considered
4327 * as a SMAP violation if all of the following
Peng Hao39337ad2018-10-04 11:45:00 -04004328 * conditions are true:
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004329 * - X86_CR4_SMAP is set in CR4
4330 * - A user page is accessed
4331 * - The access is not a fetch
4332 * - Page fault in kernel mode
4333 * - if CPL = 3 or X86_EFLAGS_AC is clear
4334 *
4335 * Here, we cover the first three conditions.
4336 * The fourth is computed dynamically in permission_fault();
4337 * PFERR_RSVD_MASK bit will be set in PFEC if the access is
4338 * *not* subject to SMAP restrictions.
4339 */
4340 if (cr4_smap)
4341 smapf = (pfec & (PFERR_RSVD_MASK|PFERR_FETCH_MASK)) ? 0 : kf;
Avi Kivity97d64b72012-09-12 14:52:00 +03004342 }
Paolo Bonzini09f037a2017-08-24 17:37:25 +02004343
4344 mmu->permissions[byte] = ff | uf | wf | smepf | smapf;
Avi Kivity97d64b72012-09-12 14:52:00 +03004345 }
4346}
4347
Huaitong Han2d344102016-03-22 16:51:19 +08004348/*
4349* PKU is an additional mechanism by which the paging controls access to
4350* user-mode addresses based on the value in the PKRU register. Protection
4351* key violations are reported through a bit in the page fault error code.
4352* Unlike other bits of the error code, the PK bit is not known at the
4353* call site of e.g. gva_to_gpa; it must be computed directly in
4354* permission_fault based on two bits of PKRU, on some machine state (CR4,
4355* CR0, EFER, CPL), and on other bits of the error code and the page tables.
4356*
4357* In particular the following conditions come from the error code, the
4358* page tables and the machine state:
4359* - PK is always zero unless CR4.PKE=1 and EFER.LMA=1
4360* - PK is always zero if RSVD=1 (reserved bit set) or F=1 (instruction fetch)
4361* - PK is always zero if U=0 in the page tables
4362* - PKRU.WD is ignored if CR0.WP=0 and the access is a supervisor access.
4363*
4364* The PKRU bitmask caches the result of these four conditions. The error
4365* code (minus the P bit) and the page table's U bit form an index into the
4366* PKRU bitmask. Two bits of the PKRU bitmask are then extracted and ANDed
4367* with the two bits of the PKRU register corresponding to the protection key.
4368* For the first three conditions above the bits will be 00, thus masking
4369* away both AD and WD. For all reads or if the last condition holds, WD
4370* only will be masked away.
4371*/
4372static void update_pkru_bitmask(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
4373 bool ept)
4374{
4375 unsigned bit;
4376 bool wp;
4377
4378 if (ept) {
4379 mmu->pkru_mask = 0;
4380 return;
4381 }
4382
4383 /* PKEY is enabled only if CR4.PKE and EFER.LMA are both set. */
4384 if (!kvm_read_cr4_bits(vcpu, X86_CR4_PKE) || !is_long_mode(vcpu)) {
4385 mmu->pkru_mask = 0;
4386 return;
4387 }
4388
4389 wp = is_write_protection(vcpu);
4390
4391 for (bit = 0; bit < ARRAY_SIZE(mmu->permissions); ++bit) {
4392 unsigned pfec, pkey_bits;
4393 bool check_pkey, check_write, ff, uf, wf, pte_user;
4394
4395 pfec = bit << 1;
4396 ff = pfec & PFERR_FETCH_MASK;
4397 uf = pfec & PFERR_USER_MASK;
4398 wf = pfec & PFERR_WRITE_MASK;
4399
4400 /* PFEC.RSVD is replaced by ACC_USER_MASK. */
4401 pte_user = pfec & PFERR_RSVD_MASK;
4402
4403 /*
4404 * Only need to check the access which is not an
4405 * instruction fetch and is to a user page.
4406 */
4407 check_pkey = (!ff && pte_user);
4408 /*
4409 * write access is controlled by PKRU if it is a
4410 * user access or CR0.WP = 1.
4411 */
4412 check_write = check_pkey && wf && (uf || wp);
4413
4414 /* PKRU.AD stops both read and write access. */
4415 pkey_bits = !!check_pkey;
4416 /* PKRU.WD stops write access. */
4417 pkey_bits |= (!!check_write) << 1;
4418
4419 mmu->pkru_mask |= (pkey_bits & 3) << pfec;
4420 }
4421}
4422
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004423static void update_last_nonleaf_level(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu)
Avi Kivity6fd01b72012-09-12 20:46:56 +03004424{
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004425 unsigned root_level = mmu->root_level;
Avi Kivity6fd01b72012-09-12 20:46:56 +03004426
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004427 mmu->last_nonleaf_level = root_level;
4428 if (root_level == PT32_ROOT_LEVEL && is_pse(vcpu))
4429 mmu->last_nonleaf_level++;
Avi Kivity6fd01b72012-09-12 20:46:56 +03004430}
4431
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004432static void paging64_init_context_common(struct kvm_vcpu *vcpu,
4433 struct kvm_mmu *context,
4434 int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004435{
Joerg Roedel2d48a982010-09-10 17:31:01 +02004436 context->nx = is_nx(vcpu);
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004437 context->root_level = level;
Joerg Roedel2d48a982010-09-10 17:31:01 +02004438
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004439 reset_rsvds_bits_mask(vcpu, context);
Yang Zhang25d92082013-08-06 12:00:32 +03004440 update_permission_bitmask(vcpu, context, false);
Huaitong Han2d344102016-03-22 16:51:19 +08004441 update_pkru_bitmask(vcpu, context, false);
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004442 update_last_nonleaf_level(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004443
Paolo Bonzinifa4a2c02013-10-02 16:56:16 +02004444 MMU_WARN_ON(!is_pae(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004445 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004446 context->gva_to_gpa = paging64_gva_to_gpa;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03004447 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03004448 context->invlpg = paging64_invlpg;
Avi Kivity17ac10a2007-01-05 16:36:40 -08004449 context->shadow_root_level = level;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02004450 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004451}
4452
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004453static void paging64_init_context(struct kvm_vcpu *vcpu,
4454 struct kvm_mmu *context)
Avi Kivity17ac10a2007-01-05 16:36:40 -08004455{
Yu Zhang855feb62017-08-24 20:27:55 +08004456 int root_level = is_la57_mode(vcpu) ?
4457 PT64_ROOT_5LEVEL : PT64_ROOT_4LEVEL;
4458
4459 paging64_init_context_common(vcpu, context, root_level);
Avi Kivity17ac10a2007-01-05 16:36:40 -08004460}
4461
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004462static void paging32_init_context(struct kvm_vcpu *vcpu,
4463 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004464{
Joerg Roedel2d48a982010-09-10 17:31:01 +02004465 context->nx = false;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004466 context->root_level = PT32_ROOT_LEVEL;
Joerg Roedel2d48a982010-09-10 17:31:01 +02004467
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004468 reset_rsvds_bits_mask(vcpu, context);
Yang Zhang25d92082013-08-06 12:00:32 +03004469 update_permission_bitmask(vcpu, context, false);
Huaitong Han2d344102016-03-22 16:51:19 +08004470 update_pkru_bitmask(vcpu, context, false);
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004471 update_last_nonleaf_level(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004472
Avi Kivity6aa8b732006-12-10 02:21:36 -08004473 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004474 context->gva_to_gpa = paging32_gva_to_gpa;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03004475 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03004476 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004477 context->shadow_root_level = PT32E_ROOT_LEVEL;
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02004478 context->direct_map = false;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004479}
4480
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004481static void paging32E_init_context(struct kvm_vcpu *vcpu,
4482 struct kvm_mmu *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004483{
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004484 paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004485}
4486
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004487static union kvm_mmu_extended_role kvm_calc_mmu_role_ext(struct kvm_vcpu *vcpu)
Junaid Shahid9fa72112018-06-27 14:59:07 -07004488{
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004489 union kvm_mmu_extended_role ext = {0};
Junaid Shahid9fa72112018-06-27 14:59:07 -07004490
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004491 ext.cr0_pg = !!is_paging(vcpu);
Vitaly Kuznetsov0699c642019-04-30 19:33:26 +02004492 ext.cr4_pae = !!is_pae(vcpu);
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004493 ext.cr4_smep = !!kvm_read_cr4_bits(vcpu, X86_CR4_SMEP);
4494 ext.cr4_smap = !!kvm_read_cr4_bits(vcpu, X86_CR4_SMAP);
4495 ext.cr4_pse = !!is_pse(vcpu);
4496 ext.cr4_pke = !!kvm_read_cr4_bits(vcpu, X86_CR4_PKE);
Yu Zhangde3ccd22019-02-01 00:09:23 +08004497 ext.maxphyaddr = cpuid_maxphyaddr(vcpu);
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004498
4499 ext.valid = 1;
4500
4501 return ext;
4502}
4503
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004504static union kvm_mmu_role kvm_calc_mmu_role_common(struct kvm_vcpu *vcpu,
4505 bool base_only)
Junaid Shahid9fa72112018-06-27 14:59:07 -07004506{
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004507 union kvm_mmu_role role = {0};
Junaid Shahid9fa72112018-06-27 14:59:07 -07004508
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004509 role.base.access = ACC_ALL;
4510 role.base.nxe = !!is_nx(vcpu);
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004511 role.base.cr0_wp = is_write_protection(vcpu);
4512 role.base.smm = is_smm(vcpu);
4513 role.base.guest_mode = is_guest_mode(vcpu);
4514
4515 if (base_only)
4516 return role;
4517
4518 role.ext = kvm_calc_mmu_role_ext(vcpu);
4519
4520 return role;
4521}
4522
Sean Christophersond468d942020-07-15 20:41:20 -07004523static inline int kvm_mmu_get_tdp_level(struct kvm_vcpu *vcpu)
4524{
4525 /* Use 5-level TDP if and only if it's useful/necessary. */
Sean Christopherson83013052020-07-15 20:41:22 -07004526 if (max_tdp_level == 5 && cpuid_maxphyaddr(vcpu) <= 48)
Sean Christophersond468d942020-07-15 20:41:20 -07004527 return 4;
4528
Sean Christopherson83013052020-07-15 20:41:22 -07004529 return max_tdp_level;
Sean Christophersond468d942020-07-15 20:41:20 -07004530}
4531
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004532static union kvm_mmu_role
4533kvm_calc_tdp_mmu_root_page_role(struct kvm_vcpu *vcpu, bool base_only)
4534{
4535 union kvm_mmu_role role = kvm_calc_mmu_role_common(vcpu, base_only);
4536
4537 role.base.ad_disabled = (shadow_accessed_mask == 0);
Sean Christophersond468d942020-07-15 20:41:20 -07004538 role.base.level = kvm_mmu_get_tdp_level(vcpu);
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004539 role.base.direct = true;
Sean Christopherson47c42e62019-03-07 15:27:44 -08004540 role.base.gpte_is_8_bytes = true;
Junaid Shahid9fa72112018-06-27 14:59:07 -07004541
4542 return role;
4543}
4544
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004545static void init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
Joerg Roedelfb72d162008-02-07 13:47:44 +01004546{
Paolo Bonzini8c008652020-07-10 16:11:50 +02004547 struct kvm_mmu *context = &vcpu->arch.root_mmu;
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004548 union kvm_mmu_role new_role =
4549 kvm_calc_tdp_mmu_root_page_role(vcpu, false);
Joerg Roedelfb72d162008-02-07 13:47:44 +01004550
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004551 if (new_role.as_u64 == context->mmu_role.as_u64)
4552 return;
4553
4554 context->mmu_role.as_u64 = new_role.as_u64;
Sean Christopherson7a026742020-02-06 14:14:34 -08004555 context->page_fault = kvm_tdp_page_fault;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03004556 context->sync_page = nonpaging_sync_page;
Paolo Bonzini5efac072020-03-23 20:42:57 -04004557 context->invlpg = NULL;
Sean Christophersond468d942020-07-15 20:41:20 -07004558 context->shadow_root_level = kvm_mmu_get_tdp_level(vcpu);
Joerg Roedelc5a78f2b2010-09-10 17:30:39 +02004559 context->direct_map = true;
Sean Christophersond8dd54e2020-03-02 18:02:39 -08004560 context->get_guest_pgd = get_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03004561 context->get_pdptr = kvm_pdptr_read;
Joerg Roedelcb659db2010-09-10 17:30:43 +02004562 context->inject_page_fault = kvm_inject_page_fault;
Joerg Roedelfb72d162008-02-07 13:47:44 +01004563
4564 if (!is_paging(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02004565 context->nx = false;
Joerg Roedelfb72d162008-02-07 13:47:44 +01004566 context->gva_to_gpa = nonpaging_gva_to_gpa;
4567 context->root_level = 0;
4568 } else if (is_long_mode(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02004569 context->nx = is_nx(vcpu);
Yu Zhang855feb62017-08-24 20:27:55 +08004570 context->root_level = is_la57_mode(vcpu) ?
4571 PT64_ROOT_5LEVEL : PT64_ROOT_4LEVEL;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004572 reset_rsvds_bits_mask(vcpu, context);
4573 context->gva_to_gpa = paging64_gva_to_gpa;
Joerg Roedelfb72d162008-02-07 13:47:44 +01004574 } else if (is_pae(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02004575 context->nx = is_nx(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01004576 context->root_level = PT32E_ROOT_LEVEL;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004577 reset_rsvds_bits_mask(vcpu, context);
4578 context->gva_to_gpa = paging64_gva_to_gpa;
Joerg Roedelfb72d162008-02-07 13:47:44 +01004579 } else {
Joerg Roedel2d48a982010-09-10 17:31:01 +02004580 context->nx = false;
Joerg Roedelfb72d162008-02-07 13:47:44 +01004581 context->root_level = PT32_ROOT_LEVEL;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004582 reset_rsvds_bits_mask(vcpu, context);
4583 context->gva_to_gpa = paging32_gva_to_gpa;
Joerg Roedelfb72d162008-02-07 13:47:44 +01004584 }
4585
Yang Zhang25d92082013-08-06 12:00:32 +03004586 update_permission_bitmask(vcpu, context, false);
Huaitong Han2d344102016-03-22 16:51:19 +08004587 update_pkru_bitmask(vcpu, context, false);
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004588 update_last_nonleaf_level(vcpu, context);
Xiao Guangrongc258b622015-08-05 12:04:24 +08004589 reset_tdp_shadow_zero_bits_mask(vcpu, context);
Joerg Roedelfb72d162008-02-07 13:47:44 +01004590}
4591
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004592static union kvm_mmu_role
Sean Christopherson59505b52020-07-15 20:41:15 -07004593kvm_calc_shadow_root_page_role_common(struct kvm_vcpu *vcpu, bool base_only)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004594{
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004595 union kvm_mmu_role role = kvm_calc_mmu_role_common(vcpu, base_only);
Paolo Bonziniad896af2013-10-02 16:56:14 +02004596
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004597 role.base.smep_andnot_wp = role.ext.cr4_smep &&
4598 !is_write_protection(vcpu);
4599 role.base.smap_andnot_wp = role.ext.cr4_smap &&
4600 !is_write_protection(vcpu);
Sean Christopherson47c42e62019-03-07 15:27:44 -08004601 role.base.gpte_is_8_bytes = !!is_pae(vcpu);
Junaid Shahid9fa72112018-06-27 14:59:07 -07004602
Sean Christopherson59505b52020-07-15 20:41:15 -07004603 return role;
4604}
4605
4606static union kvm_mmu_role
4607kvm_calc_shadow_mmu_root_page_role(struct kvm_vcpu *vcpu, bool base_only)
4608{
4609 union kvm_mmu_role role =
4610 kvm_calc_shadow_root_page_role_common(vcpu, base_only);
4611
4612 role.base.direct = !is_paging(vcpu);
4613
Junaid Shahid9fa72112018-06-27 14:59:07 -07004614 if (!is_long_mode(vcpu))
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004615 role.base.level = PT32E_ROOT_LEVEL;
Junaid Shahid9fa72112018-06-27 14:59:07 -07004616 else if (is_la57_mode(vcpu))
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004617 role.base.level = PT64_ROOT_5LEVEL;
Junaid Shahid9fa72112018-06-27 14:59:07 -07004618 else
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004619 role.base.level = PT64_ROOT_4LEVEL;
Junaid Shahid9fa72112018-06-27 14:59:07 -07004620
4621 return role;
4622}
4623
Paolo Bonzini8c008652020-07-10 16:11:50 +02004624static void shadow_mmu_init_context(struct kvm_vcpu *vcpu, struct kvm_mmu *context,
4625 u32 cr0, u32 cr4, u32 efer,
4626 union kvm_mmu_role new_role)
Junaid Shahid9fa72112018-06-27 14:59:07 -07004627{
Paolo Bonzini929d1cf2020-05-19 06:18:31 -04004628 if (!(cr0 & X86_CR0_PG))
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004629 nonpaging_init_context(vcpu, context);
Paolo Bonzini929d1cf2020-05-19 06:18:31 -04004630 else if (efer & EFER_LMA)
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004631 paging64_init_context(vcpu, context);
Paolo Bonzini929d1cf2020-05-19 06:18:31 -04004632 else if (cr4 & X86_CR4_PAE)
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004633 paging32E_init_context(vcpu, context);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004634 else
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004635 paging32_init_context(vcpu, context);
Avi Kivitya770f6f2008-12-21 19:20:09 +02004636
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004637 context->mmu_role.as_u64 = new_role.as_u64;
Xiao Guangrongc258b622015-08-05 12:04:24 +08004638 reset_shadow_zero_bits_mask(vcpu, context);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02004639}
Vitaly Kuznetsov0f04a2a2020-07-10 16:11:49 +02004640
4641static void kvm_init_shadow_mmu(struct kvm_vcpu *vcpu, u32 cr0, u32 cr4, u32 efer)
4642{
Paolo Bonzini8c008652020-07-10 16:11:50 +02004643 struct kvm_mmu *context = &vcpu->arch.root_mmu;
Vitaly Kuznetsov0f04a2a2020-07-10 16:11:49 +02004644 union kvm_mmu_role new_role =
4645 kvm_calc_shadow_mmu_root_page_role(vcpu, false);
4646
4647 if (new_role.as_u64 != context->mmu_role.as_u64)
Paolo Bonzini8c008652020-07-10 16:11:50 +02004648 shadow_mmu_init_context(vcpu, context, cr0, cr4, efer, new_role);
Vitaly Kuznetsov0f04a2a2020-07-10 16:11:49 +02004649}
4650
Sean Christopherson59505b52020-07-15 20:41:15 -07004651static union kvm_mmu_role
4652kvm_calc_shadow_npt_root_page_role(struct kvm_vcpu *vcpu)
4653{
4654 union kvm_mmu_role role =
4655 kvm_calc_shadow_root_page_role_common(vcpu, false);
4656
4657 role.base.direct = false;
Sean Christophersond468d942020-07-15 20:41:20 -07004658 role.base.level = kvm_mmu_get_tdp_level(vcpu);
Sean Christopherson59505b52020-07-15 20:41:15 -07004659
4660 return role;
4661}
4662
Vitaly Kuznetsov0f04a2a2020-07-10 16:11:49 +02004663void kvm_init_shadow_npt_mmu(struct kvm_vcpu *vcpu, u32 cr0, u32 cr4, u32 efer,
4664 gpa_t nested_cr3)
4665{
Paolo Bonzini8c008652020-07-10 16:11:50 +02004666 struct kvm_mmu *context = &vcpu->arch.guest_mmu;
Sean Christopherson59505b52020-07-15 20:41:15 -07004667 union kvm_mmu_role new_role = kvm_calc_shadow_npt_root_page_role(vcpu);
Vitaly Kuznetsov0f04a2a2020-07-10 16:11:49 +02004668
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +02004669 __kvm_mmu_new_pgd(vcpu, nested_cr3, new_role.base, false, false);
4670
Sean Christophersona3322d52021-03-04 17:10:45 -08004671 if (new_role.as_u64 != context->mmu_role.as_u64) {
Paolo Bonzini8c008652020-07-10 16:11:50 +02004672 shadow_mmu_init_context(vcpu, context, cr0, cr4, efer, new_role);
Sean Christophersona3322d52021-03-04 17:10:45 -08004673
4674 /*
4675 * Override the level set by the common init helper, nested TDP
4676 * always uses the host's TDP configuration.
4677 */
4678 context->shadow_root_level = new_role.base.level;
4679 }
Vitaly Kuznetsov0f04a2a2020-07-10 16:11:49 +02004680}
4681EXPORT_SYMBOL_GPL(kvm_init_shadow_npt_mmu);
Joerg Roedel52fde8d2010-09-10 17:30:44 +02004682
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004683static union kvm_mmu_role
4684kvm_calc_shadow_ept_root_page_role(struct kvm_vcpu *vcpu, bool accessed_dirty,
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08004685 bool execonly, u8 level)
Junaid Shahid9fa72112018-06-27 14:59:07 -07004686{
Sean Christopherson552c69b12019-03-07 15:27:43 -08004687 union kvm_mmu_role role = {0};
Junaid Shahid9fa72112018-06-27 14:59:07 -07004688
Sean Christopherson47c42e62019-03-07 15:27:44 -08004689 /* SMM flag is inherited from root_mmu */
4690 role.base.smm = vcpu->arch.root_mmu.mmu_role.base.smm;
Junaid Shahid9fa72112018-06-27 14:59:07 -07004691
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08004692 role.base.level = level;
Sean Christopherson47c42e62019-03-07 15:27:44 -08004693 role.base.gpte_is_8_bytes = true;
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004694 role.base.direct = false;
4695 role.base.ad_disabled = !accessed_dirty;
4696 role.base.guest_mode = true;
4697 role.base.access = ACC_ALL;
4698
Sean Christopherson47c42e62019-03-07 15:27:44 -08004699 /*
4700 * WP=1 and NOT_WP=1 is an impossible combination, use WP and the
4701 * SMAP variation to denote shadow EPT entries.
4702 */
4703 role.base.cr0_wp = true;
4704 role.base.smap_andnot_wp = true;
4705
Sean Christopherson552c69b12019-03-07 15:27:43 -08004706 role.ext = kvm_calc_mmu_role_ext(vcpu);
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004707 role.ext.execonly = execonly;
Junaid Shahid9fa72112018-06-27 14:59:07 -07004708
4709 return role;
4710}
4711
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02004712void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly,
Junaid Shahid50c28f22018-06-27 14:59:11 -07004713 bool accessed_dirty, gpa_t new_eptp)
Nadav Har'El155a97a2013-08-05 11:07:16 +03004714{
Paolo Bonzini8c008652020-07-10 16:11:50 +02004715 struct kvm_mmu *context = &vcpu->arch.guest_mmu;
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08004716 u8 level = vmx_eptp_page_walk_level(new_eptp);
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004717 union kvm_mmu_role new_role =
4718 kvm_calc_shadow_ept_root_page_role(vcpu, accessed_dirty,
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08004719 execonly, level);
Paolo Bonziniad896af2013-10-02 16:56:14 +02004720
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07004721 __kvm_mmu_new_pgd(vcpu, new_eptp, new_role.base, true, true);
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004722
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004723 if (new_role.as_u64 == context->mmu_role.as_u64)
4724 return;
4725
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08004726 context->shadow_root_level = level;
Nadav Har'El155a97a2013-08-05 11:07:16 +03004727
4728 context->nx = true;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02004729 context->ept_ad = accessed_dirty;
Nadav Har'El155a97a2013-08-05 11:07:16 +03004730 context->page_fault = ept_page_fault;
4731 context->gva_to_gpa = ept_gva_to_gpa;
4732 context->sync_page = ept_sync_page;
4733 context->invlpg = ept_invlpg;
Sean Christophersonbb1fcc72020-03-02 18:02:36 -08004734 context->root_level = level;
Nadav Har'El155a97a2013-08-05 11:07:16 +03004735 context->direct_map = false;
Vitaly Kuznetsova3362822018-10-08 21:28:11 +02004736 context->mmu_role.as_u64 = new_role.as_u64;
Vitaly Kuznetsov3dc773e2018-10-08 21:28:06 +02004737
Nadav Har'El155a97a2013-08-05 11:07:16 +03004738 update_permission_bitmask(vcpu, context, true);
Huaitong Han2d344102016-03-22 16:51:19 +08004739 update_pkru_bitmask(vcpu, context, true);
Ladi Prosekfd19d3b42017-10-05 11:10:22 +02004740 update_last_nonleaf_level(vcpu, context);
Nadav Har'El155a97a2013-08-05 11:07:16 +03004741 reset_rsvds_bits_mask_ept(vcpu, context, execonly);
Xiao Guangrongc258b622015-08-05 12:04:24 +08004742 reset_ept_shadow_zero_bits_mask(vcpu, context, execonly);
Nadav Har'El155a97a2013-08-05 11:07:16 +03004743}
4744EXPORT_SYMBOL_GPL(kvm_init_shadow_ept_mmu);
4745
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004746static void init_kvm_softmmu(struct kvm_vcpu *vcpu)
Joerg Roedel52fde8d2010-09-10 17:30:44 +02004747{
Paolo Bonzini8c008652020-07-10 16:11:50 +02004748 struct kvm_mmu *context = &vcpu->arch.root_mmu;
Paolo Bonziniad896af2013-10-02 16:56:14 +02004749
Paolo Bonzini929d1cf2020-05-19 06:18:31 -04004750 kvm_init_shadow_mmu(vcpu,
4751 kvm_read_cr0_bits(vcpu, X86_CR0_PG),
4752 kvm_read_cr4_bits(vcpu, X86_CR4_PAE),
4753 vcpu->arch.efer);
4754
Sean Christophersond8dd54e2020-03-02 18:02:39 -08004755 context->get_guest_pgd = get_cr3;
Paolo Bonziniad896af2013-10-02 16:56:14 +02004756 context->get_pdptr = kvm_pdptr_read;
4757 context->inject_page_fault = kvm_inject_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004758}
4759
Sean Christopherson654430ef2021-06-10 15:00:26 -07004760static union kvm_mmu_role kvm_calc_nested_mmu_role(struct kvm_vcpu *vcpu)
4761{
4762 union kvm_mmu_role role = kvm_calc_shadow_root_page_role_common(vcpu, false);
4763
4764 /*
4765 * Nested MMUs are used only for walking L2's gva->gpa, they never have
4766 * shadow pages of their own and so "direct" has no meaning. Set it
4767 * to "true" to try to detect bogus usage of the nested MMU.
4768 */
4769 role.base.direct = true;
4770
4771 if (!is_paging(vcpu))
4772 role.base.level = 0;
4773 else if (is_long_mode(vcpu))
4774 role.base.level = is_la57_mode(vcpu) ? PT64_ROOT_5LEVEL :
4775 PT64_ROOT_4LEVEL;
4776 else if (is_pae(vcpu))
4777 role.base.level = PT32E_ROOT_LEVEL;
4778 else
4779 role.base.level = PT32_ROOT_LEVEL;
4780
4781 return role;
4782}
4783
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004784static void init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004785{
Sean Christopherson654430ef2021-06-10 15:00:26 -07004786 union kvm_mmu_role new_role = kvm_calc_nested_mmu_role(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004787 struct kvm_mmu *g_context = &vcpu->arch.nested_mmu;
4788
Vitaly Kuznetsovbf627a92018-10-08 21:28:13 +02004789 if (new_role.as_u64 == g_context->mmu_role.as_u64)
4790 return;
4791
4792 g_context->mmu_role.as_u64 = new_role.as_u64;
Sean Christophersond8dd54e2020-03-02 18:02:39 -08004793 g_context->get_guest_pgd = get_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03004794 g_context->get_pdptr = kvm_pdptr_read;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004795 g_context->inject_page_fault = kvm_inject_page_fault;
4796
4797 /*
Paolo Bonzini5efac072020-03-23 20:42:57 -04004798 * L2 page tables are never shadowed, so there is no need to sync
4799 * SPTEs.
4800 */
4801 g_context->invlpg = NULL;
4802
4803 /*
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02004804 * Note that arch.mmu->gva_to_gpa translates l2_gpa to l1_gpa using
David Matlack0af25932015-12-30 08:26:17 -08004805 * L1's nested page tables (e.g. EPT12). The nested translation
4806 * of l2_gva to l1_gpa is done by arch.nested_mmu.gva_to_gpa using
4807 * L2's page tables as the first level of translation and L1's
4808 * nested page tables as the second level of translation. Basically
4809 * the gva_to_gpa functions between mmu and nested_mmu are swapped.
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004810 */
4811 if (!is_paging(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02004812 g_context->nx = false;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004813 g_context->root_level = 0;
4814 g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested;
4815 } else if (is_long_mode(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02004816 g_context->nx = is_nx(vcpu);
Yu Zhang855feb62017-08-24 20:27:55 +08004817 g_context->root_level = is_la57_mode(vcpu) ?
4818 PT64_ROOT_5LEVEL : PT64_ROOT_4LEVEL;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004819 reset_rsvds_bits_mask(vcpu, g_context);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004820 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
4821 } else if (is_pae(vcpu)) {
Joerg Roedel2d48a982010-09-10 17:31:01 +02004822 g_context->nx = is_nx(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004823 g_context->root_level = PT32E_ROOT_LEVEL;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004824 reset_rsvds_bits_mask(vcpu, g_context);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004825 g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
4826 } else {
Joerg Roedel2d48a982010-09-10 17:31:01 +02004827 g_context->nx = false;
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004828 g_context->root_level = PT32_ROOT_LEVEL;
Davidlohr Bueso4d6931c2012-03-05 16:53:06 +01004829 reset_rsvds_bits_mask(vcpu, g_context);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004830 g_context->gva_to_gpa = paging32_gva_to_gpa_nested;
4831 }
4832
Yang Zhang25d92082013-08-06 12:00:32 +03004833 update_permission_bitmask(vcpu, g_context, false);
Huaitong Han2d344102016-03-22 16:51:19 +08004834 update_pkru_bitmask(vcpu, g_context, false);
Paolo Bonzini6bb69c92016-02-23 12:51:19 +01004835 update_last_nonleaf_level(vcpu, g_context);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004836}
4837
Junaid Shahid1c53da32018-06-27 14:59:10 -07004838void kvm_init_mmu(struct kvm_vcpu *vcpu, bool reset_roots)
Joerg Roedelfb72d162008-02-07 13:47:44 +01004839{
Junaid Shahid1c53da32018-06-27 14:59:10 -07004840 if (reset_roots) {
Junaid Shahidb94742c2018-06-27 14:59:20 -07004841 uint i;
4842
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02004843 vcpu->arch.mmu->root_hpa = INVALID_PAGE;
Junaid Shahidb94742c2018-06-27 14:59:20 -07004844
4845 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02004846 vcpu->arch.mmu->prev_roots[i] = KVM_MMU_ROOT_INFO_INVALID;
Junaid Shahid1c53da32018-06-27 14:59:10 -07004847 }
4848
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004849 if (mmu_is_nested(vcpu))
Paolo Bonzinie0c6db32014-12-23 13:39:46 +01004850 init_kvm_nested_mmu(vcpu);
Joerg Roedel02f59dc2010-09-10 17:30:54 +02004851 else if (tdp_enabled)
Paolo Bonzinie0c6db32014-12-23 13:39:46 +01004852 init_kvm_tdp_mmu(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01004853 else
Paolo Bonzinie0c6db32014-12-23 13:39:46 +01004854 init_kvm_softmmu(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01004855}
Junaid Shahid1c53da32018-06-27 14:59:10 -07004856EXPORT_SYMBOL_GPL(kvm_init_mmu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01004857
Junaid Shahid9fa72112018-06-27 14:59:07 -07004858static union kvm_mmu_page_role
4859kvm_mmu_calc_root_page_role(struct kvm_vcpu *vcpu)
4860{
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004861 union kvm_mmu_role role;
4862
Junaid Shahid9fa72112018-06-27 14:59:07 -07004863 if (tdp_enabled)
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004864 role = kvm_calc_tdp_mmu_root_page_role(vcpu, true);
Junaid Shahid9fa72112018-06-27 14:59:07 -07004865 else
Vitaly Kuznetsov7dcd5752018-10-08 21:28:12 +02004866 role = kvm_calc_shadow_mmu_root_page_role(vcpu, true);
4867
4868 return role.base;
Junaid Shahid9fa72112018-06-27 14:59:07 -07004869}
Dong, Eddie489f1d62008-01-07 11:14:20 +02004870
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02004871void kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
Avi Kivity17c3ba92007-06-04 15:58:30 +03004872{
Paolo Bonzini95f93af2013-10-02 16:56:12 +02004873 kvm_mmu_unload(vcpu);
Junaid Shahid1c53da32018-06-27 14:59:10 -07004874 kvm_init_mmu(vcpu, true);
Eddie Dong8668a3c2007-10-10 14:26:45 +08004875}
Avi Kivity17c3ba92007-06-04 15:58:30 +03004876EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
4877
4878int kvm_mmu_load(struct kvm_vcpu *vcpu)
Avi Kivity714b93d2007-01-05 16:36:53 -08004879{
4880 int r;
Avi Kivitye2dec932007-01-05 16:36:54 -08004881
Sean Christopherson378f5cd2020-07-02 19:35:36 -07004882 r = mmu_topup_memory_caches(vcpu, !vcpu->arch.mmu->direct_map);
Avi Kivity17c3ba92007-06-04 15:58:30 +03004883 if (r)
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05004884 goto out;
Sean Christopherson748e52b2021-03-04 17:10:49 -08004885 r = mmu_alloc_special_roots(vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03004886 if (r)
4887 goto out;
Paolo Bonzini4a381622021-04-08 08:10:25 -04004888 if (vcpu->arch.mmu->direct_map)
Sean Christopherson6e6ec582021-03-04 17:10:50 -08004889 r = mmu_alloc_direct_roots(vcpu);
4890 else
4891 r = mmu_alloc_shadow_roots(vcpu);
Avi Kivityac1b7142007-03-08 17:13:32 +02004892 if (r)
4893 goto out;
Sean Christophersona91f3872021-03-04 17:11:00 -08004894
4895 kvm_mmu_sync_roots(vcpu);
4896
Paolo Bonzini727a7e22020-03-05 03:52:50 -05004897 kvm_mmu_load_pgd(vcpu);
Jason Baronb36464772021-01-14 22:27:56 -05004898 static_call(kvm_x86_tlb_flush_current)(vcpu);
Avi Kivity9b7a0322007-01-05 16:36:45 -08004899out:
4900 return r;
4901}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004902
4903void kvm_mmu_unload(struct kvm_vcpu *vcpu)
4904{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02004905 kvm_mmu_free_roots(vcpu, &vcpu->arch.root_mmu, KVM_MMU_ROOTS_ALL);
4906 WARN_ON(VALID_PAGE(vcpu->arch.root_mmu.root_hpa));
4907 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
4908 WARN_ON(VALID_PAGE(vcpu->arch.guest_mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004909}
Avi Kivityac1b7142007-03-08 17:13:32 +02004910
Avi Kivity79539ce2007-11-21 02:06:21 +02004911static bool need_remote_flush(u64 old, u64 new)
4912{
4913 if (!is_shadow_present_pte(old))
4914 return false;
4915 if (!is_shadow_present_pte(new))
4916 return true;
4917 if ((old ^ new) & PT64_BASE_ADDR_MASK)
4918 return true;
Gleb Natapov53166222013-08-05 11:07:14 +03004919 old ^= shadow_nx_mask;
4920 new ^= shadow_nx_mask;
Avi Kivity79539ce2007-11-21 02:06:21 +02004921 return (old & ~new & PT64_PERM_MASK) != 0;
4922}
4923
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004924static u64 mmu_pte_write_fetch_gpte(struct kvm_vcpu *vcpu, gpa_t *gpa,
Junaid Shahid0e0fee52018-10-31 14:53:57 -07004925 int *bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08004926{
Junaid Shahid0e0fee52018-10-31 14:53:57 -07004927 u64 gentry = 0;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004928 int r;
Avi Kivity72016f32010-03-15 13:59:53 +02004929
Avi Kivity08e850c2010-03-15 13:59:57 +02004930 /*
4931 * Assume that the pte write on a page table of the same type
Xiao Guangrong49b26e22011-03-04 19:00:00 +08004932 * as the current vcpu paging mode since we update the sptes only
4933 * when they have the same mode.
Avi Kivity08e850c2010-03-15 13:59:57 +02004934 */
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004935 if (is_pae(vcpu) && *bytes == 4) {
Avi Kivity08e850c2010-03-15 13:59:57 +02004936 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004937 *gpa &= ~(gpa_t)7;
4938 *bytes = 8;
Avi Kivity08e850c2010-03-15 13:59:57 +02004939 }
4940
Junaid Shahid0e0fee52018-10-31 14:53:57 -07004941 if (*bytes == 4 || *bytes == 8) {
4942 r = kvm_vcpu_read_guest_atomic(vcpu, *gpa, &gentry, *bytes);
4943 if (r)
4944 gentry = 0;
Avi Kivity72016f32010-03-15 13:59:53 +02004945 }
4946
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004947 return gentry;
4948}
4949
4950/*
4951 * If we're seeing too many writes to a page, it may no longer be a page table,
4952 * or we may be forking, in which case it is better to unmap the page.
4953 */
Xiao Guangronga138fe72011-12-16 18:18:10 +08004954static bool detect_write_flooding(struct kvm_mmu_page *sp)
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004955{
Xiao Guangronga30f47c2011-09-22 16:58:36 +08004956 /*
4957 * Skip write-flooding detected for the sp whose level is 1, because
4958 * it can become unsync, then the guest page is not write-protected.
4959 */
Sean Christopherson3bae0452020-04-27 17:54:22 -07004960 if (sp->role.level == PG_LEVEL_4K)
Xiao Guangronga30f47c2011-09-22 16:58:36 +08004961 return false;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004962
Xiao Guangronge5691a82016-02-24 17:51:12 +08004963 atomic_inc(&sp->write_flooding_count);
4964 return atomic_read(&sp->write_flooding_count) >= 3;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004965}
4966
4967/*
4968 * Misaligned accesses are too much trouble to fix up; also, they usually
4969 * indicate a page is not used as a page table.
4970 */
4971static bool detect_write_misaligned(struct kvm_mmu_page *sp, gpa_t gpa,
4972 int bytes)
4973{
4974 unsigned offset, pte_size, misaligned;
4975
4976 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
4977 gpa, bytes, sp->role.word);
4978
4979 offset = offset_in_page(gpa);
Sean Christopherson47c42e62019-03-07 15:27:44 -08004980 pte_size = sp->role.gpte_is_8_bytes ? 8 : 4;
Xiao Guangrong5d9ca302011-09-22 16:57:55 +08004981
4982 /*
4983 * Sometimes, the OS only writes the last one bytes to update status
4984 * bits, for example, in linux, andb instruction is used in clear_bit().
4985 */
4986 if (!(offset & (pte_size - 1)) && bytes == 1)
4987 return false;
4988
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08004989 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
4990 misaligned |= bytes < 4;
4991
4992 return misaligned;
4993}
4994
4995static u64 *get_written_sptes(struct kvm_mmu_page *sp, gpa_t gpa, int *nspte)
4996{
4997 unsigned page_offset, quadrant;
4998 u64 *spte;
4999 int level;
5000
5001 page_offset = offset_in_page(gpa);
5002 level = sp->role.level;
5003 *nspte = 1;
Sean Christopherson47c42e62019-03-07 15:27:44 -08005004 if (!sp->role.gpte_is_8_bytes) {
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005005 page_offset <<= 1; /* 32->64 */
5006 /*
5007 * A 32-bit pde maps 4MB while the shadow pdes map
5008 * only 2MB. So we need to double the offset again
5009 * and zap two pdes instead of one.
5010 */
5011 if (level == PT32_ROOT_LEVEL) {
5012 page_offset &= ~7; /* kill rounding error */
5013 page_offset <<= 1;
5014 *nspte = 2;
5015 }
5016 quadrant = page_offset >> PAGE_SHIFT;
5017 page_offset &= ~PAGE_MASK;
5018 if (quadrant != sp->role.quadrant)
5019 return NULL;
5020 }
5021
5022 spte = &sp->spt[page_offset / sizeof(*spte)];
5023 return spte;
5024}
5025
Xiao Guangrong13d268c2016-02-24 17:51:16 +08005026static void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Jike Songd1263632016-10-25 15:50:42 +08005027 const u8 *new, int bytes,
5028 struct kvm_page_track_notifier_node *node)
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005029{
5030 gfn_t gfn = gpa >> PAGE_SHIFT;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005031 struct kvm_mmu_page *sp;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005032 LIST_HEAD(invalid_list);
5033 u64 entry, gentry, *spte;
5034 int npte;
Paolo Bonzinib8c67b72016-02-24 11:21:55 +01005035 bool remote_flush, local_flush;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005036
5037 /*
5038 * If we don't have indirect shadow pages, it means no page is
5039 * write-protected, so we can exit simply.
5040 */
Mark Rutland6aa7de02017-10-23 14:07:29 -07005041 if (!READ_ONCE(vcpu->kvm->arch.indirect_shadow_pages))
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005042 return;
5043
Paolo Bonzinib8c67b72016-02-24 11:21:55 +01005044 remote_flush = local_flush = false;
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005045
5046 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
5047
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005048 /*
5049 * No need to care whether allocation memory is successful
Ingo Molnard9f6e122021-03-18 15:28:01 +01005050 * or not since pte prefetch is skipped if it does not have
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005051 * enough objects in the cache.
5052 */
Sean Christopherson378f5cd2020-07-02 19:35:36 -07005053 mmu_topup_memory_caches(vcpu, true);
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005054
Ben Gardon531810c2021-02-02 10:57:24 -08005055 write_lock(&vcpu->kvm->mmu_lock);
Junaid Shahid0e0fee52018-10-31 14:53:57 -07005056
5057 gentry = mmu_pte_write_fetch_gpte(vcpu, &gpa, &bytes);
5058
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005059 ++vcpu->kvm->stat.mmu_pte_write;
Xiao Guangrong0375f7f2011-11-28 20:41:00 +08005060 kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE);
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005061
Sasha Levinb67bfe02013-02-27 17:06:00 -08005062 for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn) {
Xiao Guangronga30f47c2011-09-22 16:58:36 +08005063 if (detect_write_misaligned(sp, gpa, bytes) ||
Xiao Guangronga138fe72011-12-16 18:18:10 +08005064 detect_write_flooding(sp)) {
Paolo Bonzinib8c67b72016-02-24 11:21:55 +01005065 kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
Avi Kivity4cee5762007-11-18 16:37:07 +02005066 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08005067 continue;
5068 }
Xiao Guangrong889e5cb2011-09-22 16:57:23 +08005069
5070 spte = get_written_sptes(sp, gpa, &npte);
5071 if (!spte)
5072 continue;
5073
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08005074 local_flush = true;
Avi Kivityac1b7142007-03-08 17:13:32 +02005075 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02005076 entry = *spte;
Ben Gardon2de40852020-09-23 15:14:06 -07005077 mmu_page_zap_pte(vcpu->kvm, sp, spte, NULL);
Sean Christophersonc5e21842021-01-14 16:40:51 -08005078 if (gentry && sp->role.level != PG_LEVEL_4K)
5079 ++vcpu->kvm->stat.mmu_pde_zapped;
Gleb Natapov9bb4f6b2013-01-30 16:45:01 +02005080 if (need_remote_flush(entry, *spte))
Xiao Guangrong0671a8e2010-06-04 21:56:59 +08005081 remote_flush = true;
Avi Kivityac1b7142007-03-08 17:13:32 +02005082 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08005083 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08005084 }
Paolo Bonzinib8c67b72016-02-24 11:21:55 +01005085 kvm_mmu_flush_or_zap(vcpu, &invalid_list, remote_flush, local_flush);
Xiao Guangrong0375f7f2011-11-28 20:41:00 +08005086 kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE);
Ben Gardon531810c2021-02-02 10:57:24 -08005087 write_unlock(&vcpu->kvm->mmu_lock);
Avi Kivityda4a00f2007-01-05 16:36:44 -08005088}
5089
Sean Christopherson736c2912019-12-06 15:57:14 -08005090int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code,
Andre Przywaradc25e892010-12-21 11:12:07 +01005091 void *insn, int insn_len)
Avi Kivity30677142007-10-28 18:48:59 +02005092{
Sean Christopherson92daa482020-02-18 15:03:08 -08005093 int r, emulation_type = EMULTYPE_PF;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005094 bool direct = vcpu->arch.mmu->direct_map;
Avi Kivity30677142007-10-28 18:48:59 +02005095
Sean Christopherson69481992019-12-06 15:57:29 -08005096 if (WARN_ON(!VALID_PAGE(vcpu->arch.mmu->root_hpa)))
Sean Christophersonddce6202019-12-06 15:57:27 -08005097 return RET_PF_RETRY;
5098
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02005099 r = RET_PF_INVALID;
Takuya Yoshikawae9ee9562016-02-22 17:23:41 +09005100 if (unlikely(error_code & PFERR_RSVD_MASK)) {
Sean Christopherson736c2912019-12-06 15:57:14 -08005101 r = handle_mmio_page_fault(vcpu, cr2_or_gpa, direct);
Sean Christopherson472faff2018-08-23 13:56:50 -07005102 if (r == RET_PF_EMULATE)
Takuya Yoshikawae9ee9562016-02-22 17:23:41 +09005103 goto emulate;
Takuya Yoshikawae9ee9562016-02-22 17:23:41 +09005104 }
Avi Kivity30677142007-10-28 18:48:59 +02005105
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02005106 if (r == RET_PF_INVALID) {
Sean Christopherson7a026742020-02-06 14:14:34 -08005107 r = kvm_mmu_do_page_fault(vcpu, cr2_or_gpa,
5108 lower_32_bits(error_code), false);
Sean Christopherson7b367bc2020-09-23 15:04:22 -07005109 if (WARN_ON_ONCE(r == RET_PF_INVALID))
5110 return -EIO;
Paolo Bonzini9b8ebbd2017-08-17 15:03:32 +02005111 }
5112
Avi Kivity30677142007-10-28 18:48:59 +02005113 if (r < 0)
Takuya Yoshikawae9ee9562016-02-22 17:23:41 +09005114 return r;
Sean Christopherson83a2ba42020-09-23 15:04:23 -07005115 if (r != RET_PF_EMULATE)
5116 return 1;
Avi Kivity30677142007-10-28 18:48:59 +02005117
Tom Lendacky14727752016-11-23 12:01:38 -05005118 /*
5119 * Before emulating the instruction, check if the error code
5120 * was due to a RO violation while translating the guest page.
5121 * This can occur when using nested virtualization with nested
5122 * paging in both guests. If true, we simply unprotect the page
5123 * and resume the guest.
Tom Lendacky14727752016-11-23 12:01:38 -05005124 */
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005125 if (vcpu->arch.mmu->direct_map &&
Paolo Bonzinieebed242016-11-28 14:39:58 +01005126 (error_code & PFERR_NESTED_GUEST_PAGE) == PFERR_NESTED_GUEST_PAGE) {
Sean Christopherson736c2912019-12-06 15:57:14 -08005127 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(cr2_or_gpa));
Tom Lendacky14727752016-11-23 12:01:38 -05005128 return 1;
5129 }
5130
Sean Christopherson472faff2018-08-23 13:56:50 -07005131 /*
5132 * vcpu->arch.mmu.page_fault returned RET_PF_EMULATE, but we can still
5133 * optimistically try to just unprotect the page and let the processor
5134 * re-execute the instruction that caused the page fault. Do not allow
5135 * retrying MMIO emulation, as it's not only pointless but could also
5136 * cause us to enter an infinite loop because the processor will keep
Sean Christopherson6c3dfeb2018-08-23 13:56:51 -07005137 * faulting on the non-existent MMIO address. Retrying an instruction
5138 * from a nested guest is also pointless and dangerous as we are only
5139 * explicitly shadowing L1's page tables, i.e. unprotecting something
5140 * for L1 isn't going to magically fix whatever issue cause L2 to fail.
Sean Christopherson472faff2018-08-23 13:56:50 -07005141 */
Sean Christopherson736c2912019-12-06 15:57:14 -08005142 if (!mmio_info_in_cache(vcpu, cr2_or_gpa, direct) && !is_guest_mode(vcpu))
Sean Christopherson92daa482020-02-18 15:03:08 -08005143 emulation_type |= EMULTYPE_ALLOW_RETRY_PF;
Takuya Yoshikawae9ee9562016-02-22 17:23:41 +09005144emulate:
Sean Christopherson736c2912019-12-06 15:57:14 -08005145 return x86_emulate_instruction(vcpu, cr2_or_gpa, emulation_type, insn,
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005146 insn_len);
Avi Kivity30677142007-10-28 18:48:59 +02005147}
5148EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
5149
Paolo Bonzini5efac072020-03-23 20:42:57 -04005150void kvm_mmu_invalidate_gva(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
5151 gva_t gva, hpa_t root_hpa)
Marcelo Tosattia7052892008-09-23 13:18:35 -03005152{
Junaid Shahidb94742c2018-06-27 14:59:20 -07005153 int i;
Junaid Shahid7eb77e92018-06-27 14:59:16 -07005154
Paolo Bonzini5efac072020-03-23 20:42:57 -04005155 /* It's actually a GPA for vcpu->arch.guest_mmu. */
5156 if (mmu != &vcpu->arch.guest_mmu) {
5157 /* INVLPG on a non-canonical address is a NOP according to the SDM. */
5158 if (is_noncanonical_address(gva, vcpu))
5159 return;
5160
Jason Baronb36464772021-01-14 22:27:56 -05005161 static_call(kvm_x86_tlb_flush_gva)(vcpu, gva);
Paolo Bonzini5efac072020-03-23 20:42:57 -04005162 }
5163
5164 if (!mmu->invlpg)
Junaid Shahidfaff8752018-06-29 13:10:05 -07005165 return;
5166
Paolo Bonzini5efac072020-03-23 20:42:57 -04005167 if (root_hpa == INVALID_PAGE) {
5168 mmu->invlpg(vcpu, gva, mmu->root_hpa);
Junaid Shahid956bf352018-06-27 14:59:18 -07005169
Paolo Bonzini5efac072020-03-23 20:42:57 -04005170 /*
5171 * INVLPG is required to invalidate any global mappings for the VA,
5172 * irrespective of PCID. Since it would take us roughly similar amount
5173 * of work to determine whether any of the prev_root mappings of the VA
5174 * is marked global, or to just sync it blindly, so we might as well
5175 * just always sync it.
5176 *
5177 * Mappings not reachable via the current cr3 or the prev_roots will be
5178 * synced when switching to that cr3, so nothing needs to be done here
5179 * for them.
5180 */
5181 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
5182 if (VALID_PAGE(mmu->prev_roots[i].hpa))
5183 mmu->invlpg(vcpu, gva, mmu->prev_roots[i].hpa);
5184 } else {
5185 mmu->invlpg(vcpu, gva, root_hpa);
5186 }
5187}
Junaid Shahid956bf352018-06-27 14:59:18 -07005188
Paolo Bonzini5efac072020-03-23 20:42:57 -04005189void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
5190{
5191 kvm_mmu_invalidate_gva(vcpu, vcpu->arch.mmu, gva, INVALID_PAGE);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005192 ++vcpu->stat.invlpg;
5193}
5194EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
5195
Paolo Bonzini5efac072020-03-23 20:42:57 -04005196
Junaid Shahideb4b2482018-06-27 14:59:14 -07005197void kvm_mmu_invpcid_gva(struct kvm_vcpu *vcpu, gva_t gva, unsigned long pcid)
5198{
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005199 struct kvm_mmu *mmu = vcpu->arch.mmu;
Junaid Shahidfaff8752018-06-29 13:10:05 -07005200 bool tlb_flush = false;
Junaid Shahidb94742c2018-06-27 14:59:20 -07005201 uint i;
Junaid Shahideb4b2482018-06-27 14:59:14 -07005202
5203 if (pcid == kvm_get_active_pcid(vcpu)) {
Junaid Shahid7eb77e92018-06-27 14:59:16 -07005204 mmu->invlpg(vcpu, gva, mmu->root_hpa);
Junaid Shahidfaff8752018-06-29 13:10:05 -07005205 tlb_flush = true;
Junaid Shahideb4b2482018-06-27 14:59:14 -07005206 }
5207
Junaid Shahidb94742c2018-06-27 14:59:20 -07005208 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
5209 if (VALID_PAGE(mmu->prev_roots[i].hpa) &&
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07005210 pcid == kvm_get_pcid(vcpu, mmu->prev_roots[i].pgd)) {
Junaid Shahidb94742c2018-06-27 14:59:20 -07005211 mmu->invlpg(vcpu, gva, mmu->prev_roots[i].hpa);
5212 tlb_flush = true;
5213 }
Junaid Shahid956bf352018-06-27 14:59:18 -07005214 }
Junaid Shahidade61e22018-06-27 14:59:15 -07005215
Junaid Shahidfaff8752018-06-29 13:10:05 -07005216 if (tlb_flush)
Jason Baronb36464772021-01-14 22:27:56 -05005217 static_call(kvm_x86_tlb_flush_gva)(vcpu, gva);
Junaid Shahidfaff8752018-06-29 13:10:05 -07005218
Junaid Shahideb4b2482018-06-27 14:59:14 -07005219 ++vcpu->stat.invlpg;
5220
5221 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07005222 * Mappings not reachable via the current cr3 or the prev_roots will be
5223 * synced when switching to that cr3, so nothing needs to be done here
5224 * for them.
Junaid Shahideb4b2482018-06-27 14:59:14 -07005225 */
5226}
Junaid Shahideb4b2482018-06-27 14:59:14 -07005227
Sean Christopherson83013052020-07-15 20:41:22 -07005228void kvm_configure_mmu(bool enable_tdp, int tdp_max_root_level,
5229 int tdp_huge_page_level)
Joerg Roedel18552672008-02-07 13:47:41 +01005230{
Sean Christophersonbde77232020-03-02 15:57:02 -08005231 tdp_enabled = enable_tdp;
Sean Christopherson83013052020-07-15 20:41:22 -07005232 max_tdp_level = tdp_max_root_level;
Sean Christopherson703c3352020-03-02 15:57:03 -08005233
5234 /*
Sean Christopherson1d92d2e2020-07-15 20:41:21 -07005235 * max_huge_page_level reflects KVM's MMU capabilities irrespective
Sean Christopherson703c3352020-03-02 15:57:03 -08005236 * of kernel support, e.g. KVM may be capable of using 1GB pages when
5237 * the kernel is not. But, KVM never creates a page size greater than
5238 * what is used by the kernel for any given HVA, i.e. the kernel's
5239 * capabilities are ultimately consulted by kvm_mmu_hugepage_adjust().
5240 */
5241 if (tdp_enabled)
Sean Christopherson1d92d2e2020-07-15 20:41:21 -07005242 max_huge_page_level = tdp_huge_page_level;
Sean Christopherson703c3352020-03-02 15:57:03 -08005243 else if (boot_cpu_has(X86_FEATURE_GBPAGES))
Sean Christopherson1d92d2e2020-07-15 20:41:21 -07005244 max_huge_page_level = PG_LEVEL_1G;
Sean Christopherson703c3352020-03-02 15:57:03 -08005245 else
Sean Christopherson1d92d2e2020-07-15 20:41:21 -07005246 max_huge_page_level = PG_LEVEL_2M;
Joerg Roedel18552672008-02-07 13:47:41 +01005247}
Sean Christophersonbde77232020-03-02 15:57:02 -08005248EXPORT_SYMBOL_GPL(kvm_configure_mmu);
Xiao Guangrong13d268c2016-02-24 17:51:16 +08005249
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005250/* The return value indicates if tlb flush on all vcpus is needed. */
Sean Christopherson0a234f52021-02-12 16:50:05 -08005251typedef bool (*slot_level_handler) (struct kvm *kvm, struct kvm_rmap_head *rmap_head,
5252 struct kvm_memory_slot *slot);
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005253
5254/* The caller should hold mmu-lock before calling this function. */
David Woodhouse928a4c32018-02-10 23:39:24 +00005255static __always_inline bool
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005256slot_handle_level_range(struct kvm *kvm, struct kvm_memory_slot *memslot,
5257 slot_level_handler fn, int start_level, int end_level,
Sean Christopherson1a61b7d2021-03-25 19:19:43 -07005258 gfn_t start_gfn, gfn_t end_gfn, bool flush_on_yield,
5259 bool flush)
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005260{
5261 struct slot_rmap_walk_iterator iterator;
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005262
5263 for_each_slot_rmap_range(memslot, start_level, end_level, start_gfn,
5264 end_gfn, &iterator) {
5265 if (iterator.rmap)
Sean Christopherson0a234f52021-02-12 16:50:05 -08005266 flush |= fn(kvm, iterator.rmap, memslot);
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005267
Ben Gardon531810c2021-02-02 10:57:24 -08005268 if (need_resched() || rwlock_needbreak(&kvm->mmu_lock)) {
Sean Christopherson302695a2021-03-25 19:19:41 -07005269 if (flush && flush_on_yield) {
Ben Gardonf285c632019-03-12 11:45:59 -07005270 kvm_flush_remote_tlbs_with_address(kvm,
5271 start_gfn,
5272 iterator.gfn - start_gfn + 1);
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005273 flush = false;
5274 }
Ben Gardon531810c2021-02-02 10:57:24 -08005275 cond_resched_rwlock_write(&kvm->mmu_lock);
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005276 }
5277 }
5278
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005279 return flush;
5280}
5281
David Woodhouse928a4c32018-02-10 23:39:24 +00005282static __always_inline bool
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005283slot_handle_level(struct kvm *kvm, struct kvm_memory_slot *memslot,
5284 slot_level_handler fn, int start_level, int end_level,
Sean Christopherson302695a2021-03-25 19:19:41 -07005285 bool flush_on_yield)
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005286{
5287 return slot_handle_level_range(kvm, memslot, fn, start_level,
5288 end_level, memslot->base_gfn,
5289 memslot->base_gfn + memslot->npages - 1,
Sean Christopherson1a61b7d2021-03-25 19:19:43 -07005290 flush_on_yield, false);
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005291}
5292
David Woodhouse928a4c32018-02-10 23:39:24 +00005293static __always_inline bool
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005294slot_handle_leaf(struct kvm *kvm, struct kvm_memory_slot *memslot,
Sean Christopherson302695a2021-03-25 19:19:41 -07005295 slot_level_handler fn, bool flush_on_yield)
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005296{
Sean Christopherson3bae0452020-04-27 17:54:22 -07005297 return slot_handle_level(kvm, memslot, fn, PG_LEVEL_4K,
Sean Christopherson302695a2021-03-25 19:19:41 -07005298 PG_LEVEL_4K, flush_on_yield);
Xiao Guangrong1bad2b22015-05-13 14:42:23 +08005299}
5300
Jiří Paleček1cfff4d2019-06-22 19:42:04 +02005301static void free_mmu_pages(struct kvm_mmu *mmu)
Takuya Yoshikawab99db1d2013-01-08 19:44:48 +09005302{
Sean Christopherson4a986232021-03-09 14:42:07 -08005303 if (!tdp_enabled && mmu->pae_root)
5304 set_memory_encrypted((unsigned long)mmu->pae_root, 1);
Jiří Paleček1cfff4d2019-06-22 19:42:04 +02005305 free_page((unsigned long)mmu->pae_root);
Sean Christopherson03ca4582021-05-05 13:42:21 -07005306 free_page((unsigned long)mmu->pml4_root);
Takuya Yoshikawa6b81b052013-01-08 19:47:33 +09005307}
Xiao Guangrong198c74f2014-04-17 17:06:16 +08005308
Sean Christopherson04d28e32020-09-23 09:33:14 -07005309static int __kvm_mmu_create(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu)
Avi Kivity8234b222010-12-27 12:08:45 +02005310{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005311 struct page *page;
Takuya Yoshikawab99db1d2013-01-08 19:44:48 +09005312 int i;
Takuya Yoshikawa9d1beef2013-01-08 19:46:48 +09005313
Sean Christopherson04d28e32020-09-23 09:33:14 -07005314 mmu->root_hpa = INVALID_PAGE;
5315 mmu->root_pgd = 0;
5316 mmu->translate_gpa = translate_gpa;
5317 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
5318 mmu->prev_roots[i] = KVM_MMU_ROOT_INFO_INVALID;
5319
Sean Christophersonb6b80c72019-06-13 10:22:23 -07005320 /*
5321 * When using PAE paging, the four PDPTEs are treated as 'root' pages,
5322 * while the PDP table is a per-vCPU construct that's allocated at MMU
5323 * creation. When emulating 32-bit mode, cr3 is only 32 bits even on
5324 * x86_64. Therefore we need to allocate the PDP table in the first
Sean Christopherson04d45552021-03-04 17:10:46 -08005325 * 4GB of memory, which happens to fit the DMA32 zone. TDP paging
5326 * generally doesn't use PAE paging and can skip allocating the PDP
5327 * table. The main exception, handled here, is SVM's 32-bit NPT. The
5328 * other exception is for shadowing L1's 32-bit or PAE NPT on 64-bit
5329 * KVM; that horror is handled on-demand by mmu_alloc_shadow_roots().
Sean Christophersonb6b80c72019-06-13 10:22:23 -07005330 */
Sean Christophersond468d942020-07-15 20:41:20 -07005331 if (tdp_enabled && kvm_mmu_get_tdp_level(vcpu) > PT32E_ROOT_LEVEL)
Xiao Guangrong198c74f2014-04-17 17:06:16 +08005332 return 0;
5333
Ben Gardon254272c2019-02-11 11:02:50 -08005334 page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_DMA32);
Xiao Guangrong198c74f2014-04-17 17:06:16 +08005335 if (!page)
5336 return -ENOMEM;
5337
Jiří Paleček1cfff4d2019-06-22 19:42:04 +02005338 mmu->pae_root = page_address(page);
Sean Christopherson4a986232021-03-09 14:42:07 -08005339
5340 /*
5341 * CR3 is only 32 bits when PAE paging is used, thus it's impossible to
5342 * get the CPU to treat the PDPTEs as encrypted. Decrypt the page so
5343 * that KVM's writes and the CPU's reads get along. Note, this is
5344 * only necessary when using shadow paging, as 64-bit NPT can get at
5345 * the C-bit even when shadowing 32-bit NPT, and SME isn't supported
5346 * by 32-bit kernels (when KVM itself uses 32-bit NPT).
5347 */
5348 if (!tdp_enabled)
5349 set_memory_decrypted((unsigned long)mmu->pae_root, 1);
5350 else
5351 WARN_ON_ONCE(shadow_me_mask);
5352
Xiao Guangrong198c74f2014-04-17 17:06:16 +08005353 for (i = 0; i < 4; ++i)
Sean Christophersonc834e5e42021-03-09 14:42:06 -08005354 mmu->pae_root[i] = INVALID_PAE_ROOT;
Xiao Guangrong198c74f2014-04-17 17:06:16 +08005355
5356 return 0;
5357}
5358
Kai Huangd91ffee2015-01-12 15:28:54 +08005359int kvm_mmu_create(struct kvm_vcpu *vcpu)
5360{
Jiří Paleček1cfff4d2019-06-22 19:42:04 +02005361 int ret;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08005362
Sean Christopherson5962bfb2020-07-02 19:35:25 -07005363 vcpu->arch.mmu_pte_list_desc_cache.kmem_cache = pte_list_desc_cache;
Sean Christopherson5f6078f2020-07-02 19:35:34 -07005364 vcpu->arch.mmu_pte_list_desc_cache.gfp_zero = __GFP_ZERO;
5365
Sean Christopherson5962bfb2020-07-02 19:35:25 -07005366 vcpu->arch.mmu_page_header_cache.kmem_cache = mmu_page_header_cache;
Sean Christopherson5f6078f2020-07-02 19:35:34 -07005367 vcpu->arch.mmu_page_header_cache.gfp_zero = __GFP_ZERO;
Sean Christopherson5962bfb2020-07-02 19:35:25 -07005368
Sean Christopherson96880882020-07-02 19:35:35 -07005369 vcpu->arch.mmu_shadow_page_cache.gfp_zero = __GFP_ZERO;
5370
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005371 vcpu->arch.mmu = &vcpu->arch.root_mmu;
5372 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
5373
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005374 vcpu->arch.nested_mmu.translate_gpa = translate_nested_gpa;
Jiří Paleček1cfff4d2019-06-22 19:42:04 +02005375
Sean Christopherson04d28e32020-09-23 09:33:14 -07005376 ret = __kvm_mmu_create(vcpu, &vcpu->arch.guest_mmu);
Jiří Paleček1cfff4d2019-06-22 19:42:04 +02005377 if (ret)
5378 return ret;
5379
Sean Christopherson04d28e32020-09-23 09:33:14 -07005380 ret = __kvm_mmu_create(vcpu, &vcpu->arch.root_mmu);
Jiří Paleček1cfff4d2019-06-22 19:42:04 +02005381 if (ret)
5382 goto fail_allocate_root;
5383
5384 return ret;
5385 fail_allocate_root:
5386 free_mmu_pages(&vcpu->arch.guest_mmu);
5387 return ret;
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005388}
5389
Sean Christophersonfbb158c2019-09-12 19:46:07 -07005390#define BATCH_ZAP_PAGES 10
Sean Christopherson002c5f72019-09-12 19:46:02 -07005391static void kvm_zap_obsolete_pages(struct kvm *kvm)
5392{
5393 struct kvm_mmu_page *sp, *node;
Sean Christophersonfbb158c2019-09-12 19:46:07 -07005394 int nr_zapped, batch = 0;
Sean Christopherson002c5f72019-09-12 19:46:02 -07005395
5396restart:
5397 list_for_each_entry_safe_reverse(sp, node,
5398 &kvm->arch.active_mmu_pages, link) {
5399 /*
5400 * No obsolete valid page exists before a newly created page
5401 * since active_mmu_pages is a FIFO list.
5402 */
5403 if (!is_obsolete_sp(kvm, sp))
5404 break;
5405
5406 /*
Sean Christophersonf95eec92020-06-23 12:35:39 -07005407 * Invalid pages should never land back on the list of active
5408 * pages. Skip the bogus page, otherwise we'll get stuck in an
5409 * infinite loop if the page gets put back on the list (again).
Sean Christopherson002c5f72019-09-12 19:46:02 -07005410 */
Sean Christophersonf95eec92020-06-23 12:35:39 -07005411 if (WARN_ON(sp->role.invalid))
Sean Christopherson002c5f72019-09-12 19:46:02 -07005412 continue;
5413
Sean Christopherson4506ecf2019-09-12 19:46:08 -07005414 /*
5415 * No need to flush the TLB since we're only zapping shadow
5416 * pages with an obsolete generation number and all vCPUS have
5417 * loaded a new root, i.e. the shadow pages being zapped cannot
5418 * be in active use by the guest.
5419 */
Sean Christophersonfbb158c2019-09-12 19:46:07 -07005420 if (batch >= BATCH_ZAP_PAGES &&
Ben Gardon531810c2021-02-02 10:57:24 -08005421 cond_resched_rwlock_write(&kvm->mmu_lock)) {
Sean Christophersonfbb158c2019-09-12 19:46:07 -07005422 batch = 0;
Sean Christopherson002c5f72019-09-12 19:46:02 -07005423 goto restart;
5424 }
5425
Sean Christopherson10605202019-09-12 19:46:10 -07005426 if (__kvm_mmu_prepare_zap_page(kvm, sp,
5427 &kvm->arch.zapped_obsolete_pages, &nr_zapped)) {
Sean Christophersonfbb158c2019-09-12 19:46:07 -07005428 batch += nr_zapped;
Sean Christopherson002c5f72019-09-12 19:46:02 -07005429 goto restart;
Sean Christophersonfbb158c2019-09-12 19:46:07 -07005430 }
Sean Christopherson002c5f72019-09-12 19:46:02 -07005431 }
5432
Sean Christopherson4506ecf2019-09-12 19:46:08 -07005433 /*
5434 * Trigger a remote TLB flush before freeing the page tables to ensure
5435 * KVM is not in the middle of a lockless shadow page table walk, which
5436 * may reference the pages.
5437 */
Sean Christopherson10605202019-09-12 19:46:10 -07005438 kvm_mmu_commit_zap_page(kvm, &kvm->arch.zapped_obsolete_pages);
Sean Christopherson002c5f72019-09-12 19:46:02 -07005439}
5440
5441/*
5442 * Fast invalidate all shadow pages and use lock-break technique
5443 * to zap obsolete pages.
5444 *
5445 * It's required when memslot is being deleted or VM is being
5446 * destroyed, in these cases, we should ensure that KVM MMU does
5447 * not use any resource of the being-deleted slot or all slots
5448 * after calling the function.
5449 */
5450static void kvm_mmu_zap_all_fast(struct kvm *kvm)
5451{
Sean Christophersonca333ad2019-09-12 19:46:11 -07005452 lockdep_assert_held(&kvm->slots_lock);
5453
Ben Gardon531810c2021-02-02 10:57:24 -08005454 write_lock(&kvm->mmu_lock);
Sean Christopherson14a3c4f2019-09-12 19:46:06 -07005455 trace_kvm_mmu_zap_all_fast(kvm);
Sean Christophersonca333ad2019-09-12 19:46:11 -07005456
5457 /*
5458 * Toggle mmu_valid_gen between '0' and '1'. Because slots_lock is
5459 * held for the entire duration of zapping obsolete pages, it's
5460 * impossible for there to be multiple invalid generations associated
5461 * with *valid* shadow pages at any given time, i.e. there is exactly
5462 * one valid generation and (at most) one invalid generation.
5463 */
5464 kvm->arch.mmu_valid_gen = kvm->arch.mmu_valid_gen ? 0 : 1;
Sean Christopherson002c5f72019-09-12 19:46:02 -07005465
Ben Gardonb7cccd392021-04-01 16:37:35 -07005466 /* In order to ensure all threads see this change when
5467 * handling the MMU reload signal, this must happen in the
5468 * same critical section as kvm_reload_remote_mmus, and
5469 * before kvm_zap_obsolete_pages as kvm_zap_obsolete_pages
5470 * could drop the MMU lock and yield.
5471 */
5472 if (is_tdp_mmu_enabled(kvm))
5473 kvm_tdp_mmu_invalidate_all_roots(kvm);
5474
Sean Christopherson4506ecf2019-09-12 19:46:08 -07005475 /*
5476 * Notify all vcpus to reload its shadow page table and flush TLB.
5477 * Then all vcpus will switch to new shadow page table with the new
5478 * mmu_valid_gen.
5479 *
5480 * Note: we need to do this under the protection of mmu_lock,
5481 * otherwise, vcpu would purge shadow page but miss tlb flush.
5482 */
5483 kvm_reload_remote_mmus(kvm);
5484
Sean Christopherson002c5f72019-09-12 19:46:02 -07005485 kvm_zap_obsolete_pages(kvm);
Ben Gardonfaaf05b02020-10-14 11:26:47 -07005486
Ben Gardon531810c2021-02-02 10:57:24 -08005487 write_unlock(&kvm->mmu_lock);
Ben Gardon4c6654b2021-04-01 16:37:36 -07005488
5489 if (is_tdp_mmu_enabled(kvm)) {
5490 read_lock(&kvm->mmu_lock);
5491 kvm_tdp_mmu_zap_invalidated_roots(kvm);
5492 read_unlock(&kvm->mmu_lock);
5493 }
Sean Christopherson002c5f72019-09-12 19:46:02 -07005494}
5495
Sean Christopherson10605202019-09-12 19:46:10 -07005496static bool kvm_has_zapped_obsolete_pages(struct kvm *kvm)
5497{
5498 return unlikely(!list_empty_careful(&kvm->arch.zapped_obsolete_pages));
5499}
5500
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005501static void kvm_mmu_invalidate_zap_pages_in_memslot(struct kvm *kvm,
5502 struct kvm_memory_slot *slot,
5503 struct kvm_page_track_notifier_node *node)
Xiao Guangrongdecf6332015-04-14 12:04:10 +08005504{
Sean Christopherson002c5f72019-09-12 19:46:02 -07005505 kvm_mmu_zap_all_fast(kvm);
Xiao Guangrongdecf6332015-04-14 12:04:10 +08005506}
5507
Xiao Guangrongefdfe532015-05-13 14:42:27 +08005508void kvm_mmu_init_vm(struct kvm *kvm)
5509{
5510 struct kvm_page_track_notifier_node *node = &kvm->arch.mmu_sp_tracker;
5511
Ben Gardonfe5db272020-10-14 11:26:43 -07005512 kvm_mmu_init_tdp_mmu(kvm);
5513
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02005514 node->track_write = kvm_mmu_pte_write;
Xiao Guangrongefdfe532015-05-13 14:42:27 +08005515 node->track_flush_slot = kvm_mmu_invalidate_zap_pages_in_memslot;
5516 kvm_page_track_register_notifier(kvm, node);
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02005517}
5518
5519void kvm_mmu_uninit_vm(struct kvm *kvm)
5520{
Xiao Guangrongefdfe532015-05-13 14:42:27 +08005521 struct kvm_page_track_notifier_node *node = &kvm->arch.mmu_sp_tracker;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02005522
5523 kvm_page_track_unregister_notifier(kvm, node);
Ben Gardonfe5db272020-10-14 11:26:43 -07005524
5525 kvm_mmu_uninit_tdp_mmu(kvm);
Xiao Guangrongefdfe532015-05-13 14:42:27 +08005526}
5527
Xiao Guangrongdecf6332015-04-14 12:04:10 +08005528void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end)
Xiao Guangrongefdfe532015-05-13 14:42:27 +08005529{
5530 struct kvm_memslots *slots;
5531 struct kvm_memory_slot *memslot;
5532 int i;
Sean Christopherson1a61b7d2021-03-25 19:19:43 -07005533 bool flush = false;
Xiao Guangrongefdfe532015-05-13 14:42:27 +08005534
Ben Gardon531810c2021-02-02 10:57:24 -08005535 write_lock(&kvm->mmu_lock);
Xiao Guangrongefdfe532015-05-13 14:42:27 +08005536 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
5537 slots = __kvm_memslots(kvm, i);
5538 kvm_for_each_memslot(memslot, slots) {
5539 gfn_t start, end;
5540
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02005541 start = max(gfn_start, memslot->base_gfn);
5542 end = min(gfn_end, memslot->base_gfn + memslot->npages);
5543 if (start >= end)
5544 continue;
Xiao Guangrongefdfe532015-05-13 14:42:27 +08005545
Sean Christopherson302695a2021-03-25 19:19:41 -07005546 flush = slot_handle_level_range(kvm, memslot, kvm_zap_rmapp,
5547 PG_LEVEL_4K,
5548 KVM_MAX_HUGEPAGE_LEVEL,
Sean Christopherson1a61b7d2021-03-25 19:19:43 -07005549 start, end - 1, true, flush);
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02005550 }
Xiao Guangrongefdfe532015-05-13 14:42:27 +08005551 }
5552
Sean Christopherson1a61b7d2021-03-25 19:19:43 -07005553 if (flush)
5554 kvm_flush_remote_tlbs_with_address(kvm, gfn_start, gfn_end);
Ben Gardonfaaf05b02020-10-14 11:26:47 -07005555
Ben Gardon531810c2021-02-02 10:57:24 -08005556 write_unlock(&kvm->mmu_lock);
Ben Gardon6103bc02021-04-01 16:37:32 -07005557
5558 if (is_tdp_mmu_enabled(kvm)) {
5559 flush = false;
5560
5561 read_lock(&kvm->mmu_lock);
5562 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++)
5563 flush = kvm_tdp_mmu_zap_gfn_range(kvm, i, gfn_start,
5564 gfn_end, flush, true);
5565 if (flush)
5566 kvm_flush_remote_tlbs_with_address(kvm, gfn_start,
5567 gfn_end);
5568
5569 read_unlock(&kvm->mmu_lock);
5570 }
Xiao Guangrongefdfe532015-05-13 14:42:27 +08005571}
5572
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09005573static bool slot_rmap_write_protect(struct kvm *kvm,
Sean Christopherson0a234f52021-02-12 16:50:05 -08005574 struct kvm_rmap_head *rmap_head,
5575 struct kvm_memory_slot *slot)
Xiao Guangrongd77aa732015-05-13 14:42:24 +08005576{
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09005577 return __rmap_write_protect(kvm, rmap_head, false);
Xiao Guangrongd77aa732015-05-13 14:42:24 +08005578}
5579
Dor Laore0fa8262007-03-30 13:06:33 +03005580void kvm_mmu_slot_remove_write_access(struct kvm *kvm,
Jay Zhou3c9bd402020-02-27 09:32:27 +08005581 struct kvm_memory_slot *memslot,
5582 int start_level)
Xiao Guangrongd98ba052010-06-04 21:55:29 +08005583{
Xiao Guangrongd77aa732015-05-13 14:42:24 +08005584 bool flush;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08005585
Ben Gardon531810c2021-02-02 10:57:24 -08005586 write_lock(&kvm->mmu_lock);
Jay Zhou3c9bd402020-02-27 09:32:27 +08005587 flush = slot_handle_level(kvm, memslot, slot_rmap_write_protect,
Sean Christophersone662ec32020-04-27 17:54:21 -07005588 start_level, KVM_MAX_HUGEPAGE_LEVEL, false);
Ben Gardon531810c2021-02-02 10:57:24 -08005589 write_unlock(&kvm->mmu_lock);
Paul Mundt20c2df82007-07-20 10:11:58 +09005590
Ben Gardon24ae4cf2021-04-01 16:37:34 -07005591 if (is_tdp_mmu_enabled(kvm)) {
5592 read_lock(&kvm->mmu_lock);
5593 flush |= kvm_tdp_mmu_wrprot_slot(kvm, memslot, start_level);
5594 read_unlock(&kvm->mmu_lock);
5595 }
5596
Avi Kivityb5a33a72007-04-15 16:31:09 +03005597 /*
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08005598 * We can flush all the TLBs out of the mmu lock without TLB
5599 * corruption since we just change the spte from writable to
Xiao Guangronge7d11c72013-05-31 08:36:27 +08005600 * readonly so that we only need to care the case of changing
5601 * spte from present to present (changing the spte from present
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08005602 * to nonpresent will flush all the TLBs immediately), in other
5603 * words, the only case we care is mmu_spte_update() where we
Sean Christopherson5fc34242021-02-25 12:47:43 -08005604 * have checked Host-writable | MMU-writable instead of
5605 * PT_WRITABLE_MASK, that means it does not depend on PT_WRITABLE_MASK
5606 * anymore.
Avi Kivityb5a33a72007-04-15 16:31:09 +03005607 */
5608 if (flush)
Sean Christopherson7f42aa72020-02-18 13:07:36 -08005609 kvm_arch_flush_remote_tlbs_memslot(kvm, memslot);
Avi Kivityb5a33a72007-04-15 16:31:09 +03005610}
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005611
5612static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm,
Sean Christopherson0a234f52021-02-12 16:50:05 -08005613 struct kvm_rmap_head *rmap_head,
5614 struct kvm_memory_slot *slot)
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005615{
5616 u64 *sptep;
5617 struct rmap_iterator iter;
5618 int need_tlb_flush = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005619 kvm_pfn_t pfn;
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005620 struct kvm_mmu_page *sp;
5621
5622restart:
Takuya Yoshikawa018aabb52015-11-20 17:41:28 +09005623 for_each_rmap_spte(rmap_head, &iter, sptep) {
Sean Christopherson57354682020-06-22 13:20:33 -07005624 sp = sptep_to_sp(sptep);
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005625 pfn = spte_to_pfn(*sptep);
5626
5627 /*
Xiao Guangrongdecf6332015-04-14 12:04:10 +08005628 * We cannot do huge page mapping for indirect shadow pages,
5629 * which are found on the last rmap (level = 1) when not using
5630 * tdp; such shadow pages are synced with the page table in
5631 * the guest, and the guest page table is using 4K page size
5632 * mapping if the indirect sp has level = 1.
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005633 */
Sean Christophersona78986a2019-11-11 14:12:27 -08005634 if (sp->role.direct && !kvm_is_reserved_pfn(pfn) &&
Sean Christopherson9eba50f2021-02-12 16:50:06 -08005635 sp->role.level < kvm_mmu_max_mapping_level(kvm, slot, sp->gfn,
5636 pfn, PG_LEVEL_NUM)) {
Wei Yange7912382018-10-04 10:04:23 +08005637 pte_list_remove(rmap_head, sptep);
Lan Tianyu40ef75a2018-12-06 21:21:08 +08005638
5639 if (kvm_available_flush_tlb_with_range())
5640 kvm_flush_remote_tlbs_with_address(kvm, sp->gfn,
5641 KVM_PAGES_PER_HPAGE(sp->role.level));
5642 else
5643 need_tlb_flush = 1;
5644
Xiao Guangrong0d536792015-05-13 14:42:20 +08005645 goto restart;
5646 }
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005647 }
5648
5649 return need_tlb_flush;
5650}
5651
5652void kvm_mmu_zap_collapsible_sptes(struct kvm *kvm,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02005653 const struct kvm_memory_slot *memslot)
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005654{
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02005655 /* FIXME: const-ify all uses of struct kvm_memory_slot. */
Sean Christopherson9eba50f2021-02-12 16:50:06 -08005656 struct kvm_memory_slot *slot = (struct kvm_memory_slot *)memslot;
Sean Christopherson302695a2021-03-25 19:19:41 -07005657 bool flush;
Sean Christopherson9eba50f2021-02-12 16:50:06 -08005658
Ben Gardon531810c2021-02-02 10:57:24 -08005659 write_lock(&kvm->mmu_lock);
Sean Christopherson302695a2021-03-25 19:19:41 -07005660 flush = slot_handle_leaf(kvm, slot, kvm_mmu_zap_collapsible_spte, true);
Ben Gardon14881992020-10-14 11:26:56 -07005661
Sean Christopherson302695a2021-03-25 19:19:41 -07005662 if (flush)
5663 kvm_arch_flush_remote_tlbs_memslot(kvm, slot);
Ben Gardon531810c2021-02-02 10:57:24 -08005664 write_unlock(&kvm->mmu_lock);
Ben Gardon2db6f772021-04-01 16:37:33 -07005665
5666 if (is_tdp_mmu_enabled(kvm)) {
5667 flush = false;
5668
5669 read_lock(&kvm->mmu_lock);
5670 flush = kvm_tdp_mmu_zap_collapsible_sptes(kvm, slot, flush);
5671 if (flush)
5672 kvm_arch_flush_remote_tlbs_memslot(kvm, slot);
5673 read_unlock(&kvm->mmu_lock);
5674 }
Wanpeng Li3ea3b7f2015-04-03 15:40:25 +08005675}
5676
Sean Christophersonb3594ff2020-02-18 13:07:34 -08005677void kvm_arch_flush_remote_tlbs_memslot(struct kvm *kvm,
Paolo Bonzini6c9dd6d2021-04-02 17:53:09 +02005678 const struct kvm_memory_slot *memslot)
Sean Christophersonb3594ff2020-02-18 13:07:34 -08005679{
5680 /*
Sean Christopherson7f42aa72020-02-18 13:07:36 -08005681 * All current use cases for flushing the TLBs for a specific memslot
Sean Christopherson302695a2021-03-25 19:19:41 -07005682 * related to dirty logging, and many do the TLB flush out of mmu_lock.
Sean Christopherson7f42aa72020-02-18 13:07:36 -08005683 * The interaction between the various operations on memslot must be
5684 * serialized by slots_locks to ensure the TLB flush from one operation
5685 * is observed by any other operation on the same memslot.
Sean Christophersonb3594ff2020-02-18 13:07:34 -08005686 */
5687 lockdep_assert_held(&kvm->slots_lock);
Sean Christophersoncec37642020-02-18 13:07:35 -08005688 kvm_flush_remote_tlbs_with_address(kvm, memslot->base_gfn,
5689 memslot->npages);
Sean Christophersonb3594ff2020-02-18 13:07:34 -08005690}
5691
Kai Huangf4b4b182015-01-28 10:54:24 +08005692void kvm_mmu_slot_leaf_clear_dirty(struct kvm *kvm,
5693 struct kvm_memory_slot *memslot)
5694{
Xiao Guangrongd77aa732015-05-13 14:42:24 +08005695 bool flush;
Kai Huangf4b4b182015-01-28 10:54:24 +08005696
Ben Gardon531810c2021-02-02 10:57:24 -08005697 write_lock(&kvm->mmu_lock);
Xiao Guangrongd77aa732015-05-13 14:42:24 +08005698 flush = slot_handle_leaf(kvm, memslot, __rmap_clear_dirty, false);
Ben Gardon531810c2021-02-02 10:57:24 -08005699 write_unlock(&kvm->mmu_lock);
Kai Huangf4b4b182015-01-28 10:54:24 +08005700
Ben Gardon24ae4cf2021-04-01 16:37:34 -07005701 if (is_tdp_mmu_enabled(kvm)) {
5702 read_lock(&kvm->mmu_lock);
5703 flush |= kvm_tdp_mmu_clear_dirty_slot(kvm, memslot);
5704 read_unlock(&kvm->mmu_lock);
5705 }
5706
Kai Huangf4b4b182015-01-28 10:54:24 +08005707 /*
5708 * It's also safe to flush TLBs out of mmu lock here as currently this
5709 * function is only used for dirty logging, in which case flushing TLB
5710 * out of mmu lock also guarantees no dirty pages will be lost in
5711 * dirty_bitmap.
5712 */
5713 if (flush)
Sean Christopherson7f42aa72020-02-18 13:07:36 -08005714 kvm_arch_flush_remote_tlbs_memslot(kvm, memslot);
Kai Huangf4b4b182015-01-28 10:54:24 +08005715}
Kai Huangf4b4b182015-01-28 10:54:24 +08005716
Sean Christopherson92f58b52019-09-12 19:46:04 -07005717void kvm_mmu_zap_all(struct kvm *kvm)
Avi Kivityb5a33a72007-04-15 16:31:09 +03005718{
5719 struct kvm_mmu_page *sp, *node;
Sean Christopherson7390de12019-02-05 13:01:31 -08005720 LIST_HEAD(invalid_list);
Sean Christopherson83cdb562019-02-05 13:01:35 -08005721 int ign;
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08005722
Ben Gardon531810c2021-02-02 10:57:24 -08005723 write_lock(&kvm->mmu_lock);
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08005724restart:
Sean Christopherson8a674ad2019-02-05 13:01:32 -08005725 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link) {
Sean Christophersonf95eec92020-06-23 12:35:39 -07005726 if (WARN_ON(sp->role.invalid))
Sean Christopherson8a674ad2019-02-05 13:01:32 -08005727 continue;
Sean Christopherson92f58b52019-09-12 19:46:04 -07005728 if (__kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list, &ign))
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08005729 goto restart;
Ben Gardon531810c2021-02-02 10:57:24 -08005730 if (cond_resched_rwlock_write(&kvm->mmu_lock))
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08005731 goto restart;
5732 }
5733
Sean Christopherson47714502019-02-05 13:01:23 -08005734 kvm_mmu_commit_zap_page(kvm, &invalid_list);
Ben Gardonfaaf05b02020-10-14 11:26:47 -07005735
Paolo Bonzini897218f2021-02-06 09:53:33 -05005736 if (is_tdp_mmu_enabled(kvm))
Ben Gardonfaaf05b02020-10-14 11:26:47 -07005737 kvm_tdp_mmu_zap_all(kvm);
5738
Ben Gardon531810c2021-02-02 10:57:24 -08005739 write_unlock(&kvm->mmu_lock);
Xiao Guangrong5304b8d2013-05-31 08:36:22 +08005740}
5741
Sean Christopherson15248252019-02-05 12:54:17 -08005742void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm, u64 gen)
Xiao Guangrongf8f55942013-06-07 16:51:26 +08005743{
Sean Christopherson164bf7e2019-02-05 13:01:18 -08005744 WARN_ON(gen & KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS);
Sean Christophersone1359e22019-02-05 13:01:12 -08005745
Sean Christopherson164bf7e2019-02-05 13:01:18 -08005746 gen &= MMIO_SPTE_GEN_MASK;
Sean Christophersone1359e22019-02-05 13:01:12 -08005747
Xiao Guangrongf8f55942013-06-07 16:51:26 +08005748 /*
Sean Christophersone1359e22019-02-05 13:01:12 -08005749 * Generation numbers are incremented in multiples of the number of
5750 * address spaces in order to provide unique generations across all
5751 * address spaces. Strip what is effectively the address space
5752 * modifier prior to checking for a wrap of the MMIO generation so
5753 * that a wrap in any address space is detected.
5754 */
5755 gen &= ~((u64)KVM_ADDRESS_SPACE_NUM - 1);
5756
5757 /*
5758 * The very rare case: if the MMIO generation number has wrapped,
Xiao Guangrongf8f55942013-06-07 16:51:26 +08005759 * zap all shadow pages.
Xiao Guangrongf8f55942013-06-07 16:51:26 +08005760 */
Sean Christophersone1359e22019-02-05 13:01:12 -08005761 if (unlikely(gen == 0)) {
Bandan Dasae0f5492016-11-15 01:36:18 -05005762 kvm_debug_ratelimited("kvm: zapping shadow pages for mmio generation wraparound\n");
Sean Christopherson92f58b52019-09-12 19:46:04 -07005763 kvm_mmu_zap_all_fast(kvm);
Takuya Yoshikawa7a2e8aa2013-06-21 01:34:31 +09005764 }
Xiao Guangrongf8f55942013-06-07 16:51:26 +08005765}
5766
Dave Chinner70534a72013-08-28 10:18:14 +10005767static unsigned long
5768mmu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
Izik Eidus3ee16c82008-03-30 15:17:21 +03005769{
5770 struct kvm *kvm;
Ying Han1495f232011-05-24 17:12:27 -07005771 int nr_to_scan = sc->nr_to_scan;
Dave Chinner70534a72013-08-28 10:18:14 +10005772 unsigned long freed = 0;
Izik Eidus3ee16c82008-03-30 15:17:21 +03005773
Junaid Shahid0d9ce162019-01-03 17:14:28 -08005774 mutex_lock(&kvm_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03005775
5776 list_for_each_entry(kvm, &vm_list, vm_list) {
Jan Kiszka3d56cbd2011-12-02 18:35:24 +01005777 int idx;
Xiao Guangrongd98ba052010-06-04 21:55:29 +08005778 LIST_HEAD(invalid_list);
Izik Eidus3ee16c82008-03-30 15:17:21 +03005779
Gleb Natapov19526392012-06-04 14:53:23 +03005780 /*
Takuya Yoshikawa35f2d162012-08-20 18:35:39 +09005781 * Never scan more than sc->nr_to_scan VM instances.
5782 * Will not hit this condition practically since we do not try
5783 * to shrink more than one VM and it is very unlikely to see
5784 * !n_used_mmu_pages so many times.
5785 */
5786 if (!nr_to_scan--)
5787 break;
5788 /*
Gleb Natapov19526392012-06-04 14:53:23 +03005789 * n_used_mmu_pages is accessed without holding kvm->mmu_lock
5790 * here. We may skip a VM instance errorneosly, but we do not
5791 * want to shrink a VM that only started to populate its MMU
5792 * anyway.
5793 */
Sean Christopherson10605202019-09-12 19:46:10 -07005794 if (!kvm->arch.n_used_mmu_pages &&
5795 !kvm_has_zapped_obsolete_pages(kvm))
Gleb Natapov19526392012-06-04 14:53:23 +03005796 continue;
Gleb Natapov19526392012-06-04 14:53:23 +03005797
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005798 idx = srcu_read_lock(&kvm->srcu);
Ben Gardon531810c2021-02-02 10:57:24 -08005799 write_lock(&kvm->mmu_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03005800
Sean Christopherson10605202019-09-12 19:46:10 -07005801 if (kvm_has_zapped_obsolete_pages(kvm)) {
5802 kvm_mmu_commit_zap_page(kvm,
5803 &kvm->arch.zapped_obsolete_pages);
5804 goto unlock;
5805 }
5806
Sean Christophersonebdb2922020-06-23 12:35:41 -07005807 freed = kvm_mmu_zap_oldest_mmu_pages(kvm, sc->nr_to_scan);
Gleb Natapov19526392012-06-04 14:53:23 +03005808
Sean Christopherson10605202019-09-12 19:46:10 -07005809unlock:
Ben Gardon531810c2021-02-02 10:57:24 -08005810 write_unlock(&kvm->mmu_lock);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005811 srcu_read_unlock(&kvm->srcu, idx);
Gleb Natapov19526392012-06-04 14:53:23 +03005812
Dave Chinner70534a72013-08-28 10:18:14 +10005813 /*
5814 * unfair on small ones
5815 * per-vm shrinkers cry out
5816 * sadness comes quickly
5817 */
Gleb Natapov19526392012-06-04 14:53:23 +03005818 list_move_tail(&kvm->vm_list, &vm_list);
5819 break;
Izik Eidus3ee16c82008-03-30 15:17:21 +03005820 }
Izik Eidus3ee16c82008-03-30 15:17:21 +03005821
Junaid Shahid0d9ce162019-01-03 17:14:28 -08005822 mutex_unlock(&kvm_lock);
Dave Chinner70534a72013-08-28 10:18:14 +10005823 return freed;
Dave Chinner70534a72013-08-28 10:18:14 +10005824}
5825
5826static unsigned long
5827mmu_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
5828{
Dave Hansen45221ab2010-08-19 18:11:37 -07005829 return percpu_counter_read_positive(&kvm_total_used_mmu_pages);
Izik Eidus3ee16c82008-03-30 15:17:21 +03005830}
5831
5832static struct shrinker mmu_shrinker = {
Dave Chinner70534a72013-08-28 10:18:14 +10005833 .count_objects = mmu_shrink_count,
5834 .scan_objects = mmu_shrink_scan,
Izik Eidus3ee16c82008-03-30 15:17:21 +03005835 .seeks = DEFAULT_SEEKS * 10,
5836};
5837
Ingo Molnar2ddfd202008-05-22 10:37:48 +02005838static void mmu_destroy_caches(void)
Dor Laore0fa8262007-03-30 13:06:33 +03005839{
Tim Hansenc1bd7432017-10-07 23:15:23 -04005840 kmem_cache_destroy(pte_list_desc_cache);
5841 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03005842}
5843
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01005844static bool get_nx_auto_mode(void)
5845{
5846 /* Return true when CPU has the bug, and mitigations are ON */
5847 return boot_cpu_has_bug(X86_BUG_ITLB_MULTIHIT) && !cpu_mitigations_off();
5848}
5849
5850static void __set_nx_huge_pages(bool val)
5851{
5852 nx_huge_pages = itlb_multihit_kvm_mitigation = val;
5853}
5854
5855static int set_nx_huge_pages(const char *val, const struct kernel_param *kp)
5856{
5857 bool old_val = nx_huge_pages;
5858 bool new_val;
5859
5860 /* In "auto" mode deploy workaround only if CPU has the bug. */
5861 if (sysfs_streq(val, "off"))
5862 new_val = 0;
5863 else if (sysfs_streq(val, "force"))
5864 new_val = 1;
5865 else if (sysfs_streq(val, "auto"))
5866 new_val = get_nx_auto_mode();
5867 else if (strtobool(val, &new_val) < 0)
5868 return -EINVAL;
5869
5870 __set_nx_huge_pages(new_val);
5871
5872 if (new_val != old_val) {
5873 struct kvm *kvm;
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01005874
5875 mutex_lock(&kvm_lock);
5876
5877 list_for_each_entry(kvm, &vm_list, vm_list) {
Sean Christophersoned69a6c2019-11-13 11:30:32 -08005878 mutex_lock(&kvm->slots_lock);
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01005879 kvm_mmu_zap_all_fast(kvm);
Sean Christophersoned69a6c2019-11-13 11:30:32 -08005880 mutex_unlock(&kvm->slots_lock);
Junaid Shahid1aa9b952019-11-04 20:26:00 +01005881
5882 wake_up_process(kvm->arch.nx_lpage_recovery_thread);
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01005883 }
5884 mutex_unlock(&kvm_lock);
5885 }
5886
5887 return 0;
5888}
5889
Avi Kivityb5a33a72007-04-15 16:31:09 +03005890int kvm_mmu_module_init(void)
5891{
Arnd Bergmannab271bd2018-01-10 17:26:59 +01005892 int ret = -ENOMEM;
5893
Paolo Bonzinib8e8c832019-11-04 12:22:02 +01005894 if (nx_huge_pages == -1)
5895 __set_nx_huge_pages(get_nx_auto_mode());
5896
Vitaly Kuznetsov36d9594d2018-10-08 21:28:10 +02005897 /*
5898 * MMU roles use union aliasing which is, generally speaking, an
5899 * undefined behavior. However, we supposedly know how compilers behave
5900 * and the current status quo is unlikely to change. Guardians below are
5901 * supposed to let us know if the assumption becomes false.
5902 */
5903 BUILD_BUG_ON(sizeof(union kvm_mmu_page_role) != sizeof(u32));
5904 BUILD_BUG_ON(sizeof(union kvm_mmu_extended_role) != sizeof(u32));
5905 BUILD_BUG_ON(sizeof(union kvm_mmu_role) != sizeof(u64));
5906
Junaid Shahid28a1f3a2018-08-14 10:15:34 -07005907 kvm_mmu_reset_all_pte_masks();
Junaid Shahidf160c7b2016-12-06 16:46:16 -08005908
Xiao Guangrong53c07b12011-05-15 23:26:20 +08005909 pte_list_desc_cache = kmem_cache_create("pte_list_desc",
5910 sizeof(struct pte_list_desc),
Shakeel Butt46bea482017-10-05 18:07:24 -07005911 0, SLAB_ACCOUNT, NULL);
Xiao Guangrong53c07b12011-05-15 23:26:20 +08005912 if (!pte_list_desc_cache)
Arnd Bergmannab271bd2018-01-10 17:26:59 +01005913 goto out;
Avi Kivityb5a33a72007-04-15 16:31:09 +03005914
Avi Kivityd3d25b02007-05-30 12:34:53 +03005915 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
5916 sizeof(struct kvm_mmu_page),
Shakeel Butt46bea482017-10-05 18:07:24 -07005917 0, SLAB_ACCOUNT, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03005918 if (!mmu_page_header_cache)
Arnd Bergmannab271bd2018-01-10 17:26:59 +01005919 goto out;
Avi Kivityd3d25b02007-05-30 12:34:53 +03005920
Tejun Heo908c7f12014-09-08 09:51:29 +09005921 if (percpu_counter_init(&kvm_total_used_mmu_pages, 0, GFP_KERNEL))
Arnd Bergmannab271bd2018-01-10 17:26:59 +01005922 goto out;
Wei Yongjun45bf21a2010-08-23 16:13:15 +08005923
Arnd Bergmannab271bd2018-01-10 17:26:59 +01005924 ret = register_shrinker(&mmu_shrinker);
5925 if (ret)
5926 goto out;
Izik Eidus3ee16c82008-03-30 15:17:21 +03005927
Avi Kivityb5a33a72007-04-15 16:31:09 +03005928 return 0;
5929
Arnd Bergmannab271bd2018-01-10 17:26:59 +01005930out:
Izik Eidus3ee16c82008-03-30 15:17:21 +03005931 mmu_destroy_caches();
Arnd Bergmannab271bd2018-01-10 17:26:59 +01005932 return ret;
Avi Kivityb5a33a72007-04-15 16:31:09 +03005933}
5934
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08005935/*
Peng Hao39337ad2018-10-04 11:45:00 -04005936 * Calculate mmu pages needed for kvm.
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08005937 */
Ben Gardonbc8a3d82019-04-08 11:07:30 -07005938unsigned long kvm_mmu_calculate_default_mmu_pages(struct kvm *kvm)
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08005939{
Ben Gardonbc8a3d82019-04-08 11:07:30 -07005940 unsigned long nr_mmu_pages;
5941 unsigned long nr_pages = 0;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02005942 struct kvm_memslots *slots;
Xiao Guangrongbe6ba0f2011-11-24 17:39:18 +08005943 struct kvm_memory_slot *memslot;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02005944 int i;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08005945
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02005946 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
5947 slots = __kvm_memslots(kvm, i);
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08005948
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02005949 kvm_for_each_memslot(memslot, slots)
5950 nr_pages += memslot->npages;
5951 }
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08005952
5953 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
Ben Gardonbc8a3d82019-04-08 11:07:30 -07005954 nr_mmu_pages = max(nr_mmu_pages, KVM_MIN_ALLOC_MMU_PAGES);
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08005955
5956 return nr_mmu_pages;
5957}
5958
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08005959void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
5960{
Paolo Bonzini95f93af2013-10-02 16:56:12 +02005961 kvm_mmu_unload(vcpu);
Jiří Paleček1cfff4d2019-06-22 19:42:04 +02005962 free_mmu_pages(&vcpu->arch.root_mmu);
5963 free_mmu_pages(&vcpu->arch.guest_mmu);
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08005964 mmu_free_memory_caches(vcpu);
Xiao Guangrongb034cf02010-12-23 16:08:35 +08005965}
5966
Xiao Guangrongb034cf02010-12-23 16:08:35 +08005967void kvm_mmu_module_exit(void)
5968{
5969 mmu_destroy_caches();
5970 percpu_counter_destroy(&kvm_total_used_mmu_pages);
5971 unregister_shrinker(&mmu_shrinker);
Xiao Guangrongc42fffe2010-09-27 18:07:07 +08005972 mmu_audit_disable();
5973}
Junaid Shahid1aa9b952019-11-04 20:26:00 +01005974
5975static int set_nx_huge_pages_recovery_ratio(const char *val, const struct kernel_param *kp)
5976{
5977 unsigned int old_val;
5978 int err;
5979
5980 old_val = nx_huge_pages_recovery_ratio;
5981 err = param_set_uint(val, kp);
5982 if (err)
5983 return err;
5984
5985 if (READ_ONCE(nx_huge_pages) &&
5986 !old_val && nx_huge_pages_recovery_ratio) {
5987 struct kvm *kvm;
5988
5989 mutex_lock(&kvm_lock);
5990
5991 list_for_each_entry(kvm, &vm_list, vm_list)
5992 wake_up_process(kvm->arch.nx_lpage_recovery_thread);
5993
5994 mutex_unlock(&kvm_lock);
5995 }
5996
5997 return err;
5998}
5999
6000static void kvm_recover_nx_lpages(struct kvm *kvm)
6001{
6002 int rcu_idx;
6003 struct kvm_mmu_page *sp;
6004 unsigned int ratio;
6005 LIST_HEAD(invalid_list);
Sean Christopherson048f4982021-03-25 13:01:18 -07006006 bool flush = false;
Junaid Shahid1aa9b952019-11-04 20:26:00 +01006007 ulong to_zap;
6008
6009 rcu_idx = srcu_read_lock(&kvm->srcu);
Ben Gardon531810c2021-02-02 10:57:24 -08006010 write_lock(&kvm->mmu_lock);
Junaid Shahid1aa9b952019-11-04 20:26:00 +01006011
6012 ratio = READ_ONCE(nx_huge_pages_recovery_ratio);
6013 to_zap = ratio ? DIV_ROUND_UP(kvm->stat.nx_lpage_splits, ratio) : 0;
Sean Christopherson7d919c72020-09-23 11:37:29 -07006014 for ( ; to_zap; --to_zap) {
6015 if (list_empty(&kvm->arch.lpage_disallowed_mmu_pages))
6016 break;
6017
Junaid Shahid1aa9b952019-11-04 20:26:00 +01006018 /*
6019 * We use a separate list instead of just using active_mmu_pages
6020 * because the number of lpage_disallowed pages is expected to
6021 * be relatively small compared to the total.
6022 */
6023 sp = list_first_entry(&kvm->arch.lpage_disallowed_mmu_pages,
6024 struct kvm_mmu_page,
6025 lpage_disallowed_link);
6026 WARN_ON_ONCE(!sp->lpage_disallowed);
Paolo Bonzini897218f2021-02-06 09:53:33 -05006027 if (is_tdp_mmu_page(sp)) {
Paolo Bonzini315f02c2021-04-06 11:08:51 -04006028 flush |= kvm_tdp_mmu_zap_sp(kvm, sp);
Ben Gardon8d1a1822021-02-02 10:57:15 -08006029 } else {
Ben Gardon29cf0f52020-10-14 11:27:00 -07006030 kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
6031 WARN_ON_ONCE(sp->lpage_disallowed);
6032 }
Junaid Shahid1aa9b952019-11-04 20:26:00 +01006033
Ben Gardon531810c2021-02-02 10:57:24 -08006034 if (need_resched() || rwlock_needbreak(&kvm->mmu_lock)) {
Sean Christopherson048f4982021-03-25 13:01:18 -07006035 kvm_mmu_remote_flush_or_zap(kvm, &invalid_list, flush);
Ben Gardon531810c2021-02-02 10:57:24 -08006036 cond_resched_rwlock_write(&kvm->mmu_lock);
Sean Christopherson048f4982021-03-25 13:01:18 -07006037 flush = false;
Junaid Shahid1aa9b952019-11-04 20:26:00 +01006038 }
6039 }
Sean Christopherson048f4982021-03-25 13:01:18 -07006040 kvm_mmu_remote_flush_or_zap(kvm, &invalid_list, flush);
Junaid Shahid1aa9b952019-11-04 20:26:00 +01006041
Ben Gardon531810c2021-02-02 10:57:24 -08006042 write_unlock(&kvm->mmu_lock);
Junaid Shahid1aa9b952019-11-04 20:26:00 +01006043 srcu_read_unlock(&kvm->srcu, rcu_idx);
6044}
6045
6046static long get_nx_lpage_recovery_timeout(u64 start_time)
6047{
6048 return READ_ONCE(nx_huge_pages) && READ_ONCE(nx_huge_pages_recovery_ratio)
6049 ? start_time + 60 * HZ - get_jiffies_64()
6050 : MAX_SCHEDULE_TIMEOUT;
6051}
6052
6053static int kvm_nx_lpage_recovery_worker(struct kvm *kvm, uintptr_t data)
6054{
6055 u64 start_time;
6056 long remaining_time;
6057
6058 while (true) {
6059 start_time = get_jiffies_64();
6060 remaining_time = get_nx_lpage_recovery_timeout(start_time);
6061
6062 set_current_state(TASK_INTERRUPTIBLE);
6063 while (!kthread_should_stop() && remaining_time > 0) {
6064 schedule_timeout(remaining_time);
6065 remaining_time = get_nx_lpage_recovery_timeout(start_time);
6066 set_current_state(TASK_INTERRUPTIBLE);
6067 }
6068
6069 set_current_state(TASK_RUNNING);
6070
6071 if (kthread_should_stop())
6072 return 0;
6073
6074 kvm_recover_nx_lpages(kvm);
6075 }
6076}
6077
6078int kvm_mmu_post_init_vm(struct kvm *kvm)
6079{
6080 int err;
6081
6082 err = kvm_vm_create_worker_thread(kvm, kvm_nx_lpage_recovery_worker, 0,
6083 "kvm-nx-lpage-recovery",
6084 &kvm->arch.nx_lpage_recovery_thread);
6085 if (!err)
6086 kthread_unpark(kvm->arch.nx_lpage_recovery_thread);
6087
6088 return err;
6089}
6090
6091void kvm_mmu_pre_destroy_vm(struct kvm *kvm)
6092{
6093 if (kvm->arch.nx_lpage_recovery_thread)
6094 kthread_stop(kvm->arch.nx_lpage_recovery_thread);
6095}