Thomas Gleixner | 20c8ccb | 2019-06-04 10:11:32 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2 | /* |
| 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 Kaiser | 9611c18 | 2010-10-06 14:23:22 +0200 | [diff] [blame] | 11 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12 | * |
| 13 | * Authors: |
| 14 | * Yaniv Kamay <yaniv@qumranet.com> |
| 15 | * Avi Kivity <avi@qumranet.com> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 16 | */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 17 | |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 18 | #include "irq.h" |
彭浩(Richard) | 88197e6 | 2020-05-21 05:57:49 +0000 | [diff] [blame] | 19 | #include "ioapic.h" |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 20 | #include "mmu.h" |
Sean Christopherson | 6ca9a6f | 2020-06-22 13:20:31 -0700 | [diff] [blame] | 21 | #include "mmu_internal.h" |
Ben Gardon | fe5db27 | 2020-10-14 11:26:43 -0700 | [diff] [blame] | 22 | #include "tdp_mmu.h" |
Avi Kivity | 836a1b3 | 2010-01-21 15:31:49 +0200 | [diff] [blame] | 23 | #include "x86.h" |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 24 | #include "kvm_cache_regs.h" |
Sean Christopherson | 2f728d6 | 2020-02-18 15:29:49 -0800 | [diff] [blame] | 25 | #include "kvm_emulate.h" |
Nadav Amit | 5f7dde7 | 2014-05-07 15:32:50 +0300 | [diff] [blame] | 26 | #include "cpuid.h" |
Paolo Bonzini | 5a9624a | 2020-10-16 10:29:37 -0400 | [diff] [blame] | 27 | #include "spte.h" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 28 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 29 | #include <linux/kvm_host.h> |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 30 | #include <linux/types.h> |
| 31 | #include <linux/string.h> |
| 32 | #include <linux/mm.h> |
| 33 | #include <linux/highmem.h> |
Paul Gortmaker | 1767e93 | 2016-07-13 20:19:00 -0400 | [diff] [blame] | 34 | #include <linux/moduleparam.h> |
| 35 | #include <linux/export.h> |
Izik Eidus | 448353c | 2007-11-26 14:08:14 +0200 | [diff] [blame] | 36 | #include <linux/swap.h> |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 37 | #include <linux/hugetlb.h> |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 38 | #include <linux/compiler.h> |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 39 | #include <linux/srcu.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 40 | #include <linux/slab.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 41 | #include <linux/sched/signal.h> |
Huang Ying | bf99815 | 2010-05-31 14:28:19 +0800 | [diff] [blame] | 42 | #include <linux/uaccess.h> |
David Matlack | 114df30 | 2016-12-19 13:58:25 -0800 | [diff] [blame] | 43 | #include <linux/hash.h> |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 44 | #include <linux/kern_levels.h> |
Junaid Shahid | 1aa9b95 | 2019-11-04 20:26:00 +0100 | [diff] [blame] | 45 | #include <linux/kthread.h> |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 46 | |
| 47 | #include <asm/page.h> |
Ingo Molnar | eb243d1 | 2019-11-20 15:33:57 +0100 | [diff] [blame] | 48 | #include <asm/memtype.h> |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 49 | #include <asm/cmpxchg.h> |
Avi Kivity | 4e54237 | 2007-11-21 14:08:40 +0200 | [diff] [blame] | 50 | #include <asm/io.h> |
Sean Christopherson | 4a98623 | 2021-03-09 14:42:07 -0800 | [diff] [blame] | 51 | #include <asm/set_memory.h> |
Eduardo Habkost | 13673a9 | 2008-11-17 19:03:13 -0200 | [diff] [blame] | 52 | #include <asm/vmx.h> |
Xiao Guangrong | 3d0c27a | 2016-02-24 17:51:11 +0800 | [diff] [blame] | 53 | #include <asm/kvm_page_track.h> |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 54 | #include "trace.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 55 | |
Paolo Bonzini | b8e8c83 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 56 | extern bool itlb_multihit_kvm_mitigation; |
| 57 | |
Shaokun Zhang | a9d6496 | 2021-05-27 15:57:51 +0800 | [diff] [blame] | 58 | int __read_mostly nx_huge_pages = -1; |
Paolo Bonzini | 13fb592 | 2019-11-13 15:47:06 +0100 | [diff] [blame] | 59 | #ifdef CONFIG_PREEMPT_RT |
| 60 | /* Recovery can cause latency spikes, disable it for PREEMPT_RT. */ |
| 61 | static uint __read_mostly nx_huge_pages_recovery_ratio = 0; |
| 62 | #else |
Junaid Shahid | 1aa9b95 | 2019-11-04 20:26:00 +0100 | [diff] [blame] | 63 | static uint __read_mostly nx_huge_pages_recovery_ratio = 60; |
Paolo Bonzini | 13fb592 | 2019-11-13 15:47:06 +0100 | [diff] [blame] | 64 | #endif |
Paolo Bonzini | b8e8c83 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 65 | |
| 66 | static int set_nx_huge_pages(const char *val, const struct kernel_param *kp); |
Junaid Shahid | 1aa9b95 | 2019-11-04 20:26:00 +0100 | [diff] [blame] | 67 | static int set_nx_huge_pages_recovery_ratio(const char *val, const struct kernel_param *kp); |
Paolo Bonzini | b8e8c83 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 68 | |
Joe Perches | d5d6c18 | 2020-10-03 17:18:07 -0700 | [diff] [blame] | 69 | static const struct kernel_param_ops nx_huge_pages_ops = { |
Paolo Bonzini | b8e8c83 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 70 | .set = set_nx_huge_pages, |
| 71 | .get = param_get_bool, |
| 72 | }; |
| 73 | |
Joe Perches | d5d6c18 | 2020-10-03 17:18:07 -0700 | [diff] [blame] | 74 | static const struct kernel_param_ops nx_huge_pages_recovery_ratio_ops = { |
Junaid Shahid | 1aa9b95 | 2019-11-04 20:26:00 +0100 | [diff] [blame] | 75 | .set = set_nx_huge_pages_recovery_ratio, |
| 76 | .get = param_get_uint, |
| 77 | }; |
| 78 | |
Paolo Bonzini | b8e8c83 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 79 | module_param_cb(nx_huge_pages, &nx_huge_pages_ops, &nx_huge_pages, 0644); |
| 80 | __MODULE_PARM_TYPE(nx_huge_pages, "bool"); |
Junaid Shahid | 1aa9b95 | 2019-11-04 20:26:00 +0100 | [diff] [blame] | 81 | module_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 Bonzini | b8e8c83 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 84 | |
Sean Christopherson | 71fe701 | 2020-03-20 14:28:28 -0700 | [diff] [blame] | 85 | static bool __read_mostly force_flush_and_sync_on_reuse; |
| 86 | module_param_named(flush_on_reuse, force_flush_and_sync_on_reuse, bool, 0644); |
| 87 | |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 88 | /* |
| 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 Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 95 | bool tdp_enabled = false; |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 96 | |
Sean Christopherson | 1d92d2e | 2020-07-15 20:41:21 -0700 | [diff] [blame] | 97 | static int max_huge_page_level __read_mostly; |
Sean Christopherson | 8301305 | 2020-07-15 20:41:22 -0700 | [diff] [blame] | 98 | static int max_tdp_level __read_mostly; |
Sean Christopherson | 703c335 | 2020-03-02 15:57:03 -0800 | [diff] [blame] | 99 | |
Xiao Guangrong | 8b1fe17 | 2010-08-30 18:22:53 +0800 | [diff] [blame] | 100 | enum { |
| 101 | AUDIT_PRE_PAGE_FAULT, |
| 102 | AUDIT_POST_PAGE_FAULT, |
| 103 | AUDIT_PRE_PTE_WRITE, |
Xiao Guangrong | 6903074 | 2010-09-27 18:09:29 +0800 | [diff] [blame] | 104 | AUDIT_POST_PTE_WRITE, |
| 105 | AUDIT_PRE_SYNC, |
| 106 | AUDIT_POST_SYNC |
Xiao Guangrong | 8b1fe17 | 2010-08-30 18:22:53 +0800 | [diff] [blame] | 107 | }; |
| 108 | |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 109 | #ifdef MMU_DEBUG |
Paolo Bonzini | 5a9624a | 2020-10-16 10:29:37 -0400 | [diff] [blame] | 110 | bool dbg = 0; |
Paolo Bonzini | fa4a2c0 | 2013-10-02 16:56:16 +0200 | [diff] [blame] | 111 | module_param(dbg, bool, 0644); |
Yaozu Dong | d6c69ee | 2007-04-25 14:17:25 +0800 | [diff] [blame] | 112 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 113 | |
Xiao Guangrong | 957ed9e | 2010-08-22 19:12:48 +0800 | [diff] [blame] | 114 | #define PTE_PREFETCH_NUM 8 |
| 115 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 116 | #define PT32_LEVEL_BITS 10 |
| 117 | |
| 118 | #define PT32_LEVEL_SHIFT(level) \ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 119 | (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 120 | |
Joerg Roedel | e04da98 | 2009-07-27 16:30:45 +0200 | [diff] [blame] | 121 | #define PT32_LVL_OFFSET_MASK(level) \ |
| 122 | (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \ |
| 123 | * PT32_LEVEL_BITS))) - 1)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 124 | |
| 125 | #define PT32_INDEX(address, level)\ |
| 126 | (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1)) |
| 127 | |
| 128 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 129 | #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 Roedel | e04da98 | 2009-07-27 16:30:45 +0200 | [diff] [blame] | 132 | #define PT32_LVL_ADDR_MASK(level) \ |
| 133 | (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \ |
| 134 | * PT32_LEVEL_BITS))) - 1)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 135 | |
Avi Kivity | 90bb6fc | 2009-12-31 12:10:16 +0200 | [diff] [blame] | 136 | #include <trace/events/kvm.h> |
| 137 | |
Takuya Yoshikawa | 220f773 | 2012-03-21 23:49:39 +0900 | [diff] [blame] | 138 | /* make pte_list_desc fit well in cache line */ |
| 139 | #define PTE_LIST_EXT 3 |
| 140 | |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 141 | struct pte_list_desc { |
| 142 | u64 *sptes[PTE_LIST_EXT]; |
| 143 | struct pte_list_desc *more; |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 144 | }; |
| 145 | |
Avi Kivity | 2d11123 | 2008-12-25 14:39:47 +0200 | [diff] [blame] | 146 | struct kvm_shadow_walk_iterator { |
| 147 | u64 addr; |
| 148 | hpa_t shadow_addr; |
Avi Kivity | 2d11123 | 2008-12-25 14:39:47 +0200 | [diff] [blame] | 149 | u64 *sptep; |
Xiao Guangrong | dd3bfd5 | 2011-07-12 03:32:54 +0800 | [diff] [blame] | 150 | int level; |
Avi Kivity | 2d11123 | 2008-12-25 14:39:47 +0200 | [diff] [blame] | 151 | unsigned index; |
| 152 | }; |
| 153 | |
Junaid Shahid | 7eb77e9 | 2018-06-27 14:59:16 -0700 | [diff] [blame] | 154 | #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 Kivity | 2d11123 | 2008-12-25 14:39:47 +0200 | [diff] [blame] | 161 | for (shadow_walk_init(&(_walker), _vcpu, _addr); \ |
| 162 | shadow_walk_okay(&(_walker)); \ |
| 163 | shadow_walk_next(&(_walker))) |
| 164 | |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 165 | #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 Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 171 | static struct kmem_cache *pte_list_desc_cache; |
Ben Gardon | 02c00b3 | 2020-10-14 20:26:44 +0200 | [diff] [blame] | 172 | struct kmem_cache *mmu_page_header_cache; |
Dave Hansen | 45221ab | 2010-08-19 18:11:37 -0700 | [diff] [blame] | 173 | static struct percpu_counter kvm_total_used_mmu_pages; |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 174 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 175 | static void mmu_spte_set(u64 *sptep, u64 spte); |
Junaid Shahid | 9fa7211 | 2018-06-27 14:59:07 -0700 | [diff] [blame] | 176 | static union kvm_mmu_page_role |
| 177 | kvm_mmu_calc_root_page_role(struct kvm_vcpu *vcpu); |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 178 | |
Paolo Bonzini | 335e192 | 2019-07-01 06:22:57 -0400 | [diff] [blame] | 179 | #define CREATE_TRACE_POINTS |
| 180 | #include "mmutrace.h" |
| 181 | |
Lan Tianyu | 40ef75a | 2018-12-06 21:21:08 +0800 | [diff] [blame] | 182 | |
| 183 | static inline bool kvm_available_flush_tlb_with_range(void) |
| 184 | { |
Sean Christopherson | afaf0b2 | 2020-03-21 13:26:00 -0700 | [diff] [blame] | 185 | return kvm_x86_ops.tlb_remote_flush_with_range; |
Lan Tianyu | 40ef75a | 2018-12-06 21:21:08 +0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static void kvm_flush_remote_tlbs_with_range(struct kvm *kvm, |
| 189 | struct kvm_tlb_range *range) |
| 190 | { |
| 191 | int ret = -ENOTSUPP; |
| 192 | |
Sean Christopherson | afaf0b2 | 2020-03-21 13:26:00 -0700 | [diff] [blame] | 193 | if (range && kvm_x86_ops.tlb_remote_flush_with_range) |
Jason Baron | b3646477 | 2021-01-14 22:27:56 -0500 | [diff] [blame] | 194 | ret = static_call(kvm_x86_tlb_remote_flush_with_range)(kvm, range); |
Lan Tianyu | 40ef75a | 2018-12-06 21:21:08 +0800 | [diff] [blame] | 195 | |
| 196 | if (ret) |
| 197 | kvm_flush_remote_tlbs(kvm); |
| 198 | } |
| 199 | |
Ben Gardon | 2f2fad0 | 2020-10-14 20:26:45 +0200 | [diff] [blame] | 200 | void kvm_flush_remote_tlbs_with_address(struct kvm *kvm, |
Lan Tianyu | 40ef75a | 2018-12-06 21:21:08 +0800 | [diff] [blame] | 201 | 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 Gardon | 8f79b06 | 2020-02-03 15:09:10 -0800 | [diff] [blame] | 211 | static void mark_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, u64 gfn, |
| 212 | unsigned int access) |
| 213 | { |
Sean Christopherson | c236d96 | 2021-02-25 12:47:34 -0800 | [diff] [blame] | 214 | u64 spte = make_mmio_spte(vcpu, gfn, access); |
Ben Gardon | 8f79b06 | 2020-02-03 15:09:10 -0800 | [diff] [blame] | 215 | |
Sean Christopherson | c236d96 | 2021-02-25 12:47:34 -0800 | [diff] [blame] | 216 | trace_mark_mmio_spte(sptep, gfn, spte); |
| 217 | mmu_spte_set(sptep, spte); |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 218 | } |
| 219 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 220 | static gfn_t get_mmio_spte_gfn(u64 spte) |
| 221 | { |
Sean Christopherson | daa07cb | 2018-09-25 13:20:00 -0700 | [diff] [blame] | 222 | u64 gpa = spte & shadow_nonpresent_or_rsvd_lower_gfn_mask; |
Junaid Shahid | 28a1f3a | 2018-08-14 10:15:34 -0700 | [diff] [blame] | 223 | |
Paolo Bonzini | 8a967d6 | 2020-10-30 13:39:55 -0400 | [diff] [blame] | 224 | gpa |= (spte >> SHADOW_NONPRESENT_OR_RSVD_MASK_LEN) |
Junaid Shahid | 28a1f3a | 2018-08-14 10:15:34 -0700 | [diff] [blame] | 225 | & shadow_nonpresent_or_rsvd_mask; |
| 226 | |
| 227 | return gpa >> PAGE_SHIFT; |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static unsigned get_mmio_spte_access(u64 spte) |
| 231 | { |
Sean Christopherson | 4af7715 | 2019-08-01 13:35:22 -0700 | [diff] [blame] | 232 | return spte & shadow_mmio_access_mask; |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 233 | } |
| 234 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 235 | static bool check_mmio_spte(struct kvm_vcpu *vcpu, u64 spte) |
Xiao Guangrong | f8f5594 | 2013-06-07 16:51:26 +0800 | [diff] [blame] | 236 | { |
Sean Christopherson | cae7ed3 | 2019-02-05 13:01:16 -0800 | [diff] [blame] | 237 | u64 kvm_gen, spte_gen, gen; |
Xiao Guangrong | 089504c | 2013-06-07 16:51:27 +0800 | [diff] [blame] | 238 | |
Sean Christopherson | cae7ed3 | 2019-02-05 13:01:16 -0800 | [diff] [blame] | 239 | 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 Guangrong | 089504c | 2013-06-07 16:51:27 +0800 | [diff] [blame] | 244 | 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 Guangrong | f8f5594 | 2013-06-07 16:51:26 +0800 | [diff] [blame] | 248 | } |
| 249 | |
Mohammed Gamal | cd31356 | 2020-07-10 17:48:04 +0200 | [diff] [blame] | 250 | static gpa_t translate_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access, |
| 251 | struct x86_exception *exception) |
| 252 | { |
Mohammed Gamal | ec7771a | 2020-07-10 17:48:05 +0200 | [diff] [blame] | 253 | /* Check if guest physical address doesn't exceed guest maximum */ |
Sean Christopherson | dc46515 | 2020-09-24 12:42:49 -0700 | [diff] [blame] | 254 | if (kvm_vcpu_is_illegal_gpa(vcpu, gpa)) { |
Mohammed Gamal | ec7771a | 2020-07-10 17:48:05 +0200 | [diff] [blame] | 255 | exception->error_code |= PFERR_RSVD_MASK; |
| 256 | return UNMAPPED_GVA; |
| 257 | } |
| 258 | |
Mohammed Gamal | cd31356 | 2020-07-10 17:48:04 +0200 | [diff] [blame] | 259 | return gpa; |
| 260 | } |
| 261 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 262 | static int is_cpuid_PSE36(void) |
| 263 | { |
| 264 | return 1; |
| 265 | } |
| 266 | |
Avi Kivity | 73b1087 | 2007-01-26 00:56:41 -0800 | [diff] [blame] | 267 | static int is_nx(struct kvm_vcpu *vcpu) |
| 268 | { |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 269 | return vcpu->arch.efer & EFER_NX; |
Avi Kivity | 73b1087 | 2007-01-26 00:56:41 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Avi Kivity | da928521 | 2007-11-21 13:54:47 +0200 | [diff] [blame] | 272 | static 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 Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 279 | #ifdef CONFIG_X86_64 |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 280 | static void __set_spte(u64 *sptep, u64 spte) |
Avi Kivity | e663ee6 | 2007-05-31 15:46:04 +0300 | [diff] [blame] | 281 | { |
Nadav Amit | b19ee2f | 2016-05-11 08:04:29 -0700 | [diff] [blame] | 282 | WRITE_ONCE(*sptep, spte); |
Avi Kivity | e663ee6 | 2007-05-31 15:46:04 +0300 | [diff] [blame] | 283 | } |
| 284 | |
Xiao Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 285 | static void __update_clear_spte_fast(u64 *sptep, u64 spte) |
Avi Kivity | a9221dd | 2010-06-06 14:48:06 +0300 | [diff] [blame] | 286 | { |
Nadav Amit | b19ee2f | 2016-05-11 08:04:29 -0700 | [diff] [blame] | 287 | WRITE_ONCE(*sptep, spte); |
Avi Kivity | a9221dd | 2010-06-06 14:48:06 +0300 | [diff] [blame] | 288 | } |
| 289 | |
Xiao Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 290 | static u64 __update_clear_spte_slow(u64 *sptep, u64 spte) |
| 291 | { |
| 292 | return xchg(sptep, spte); |
| 293 | } |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 294 | |
| 295 | static u64 __get_spte_lockless(u64 *sptep) |
| 296 | { |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 297 | return READ_ONCE(*sptep); |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 298 | } |
Xiao Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 299 | #else |
| 300 | union split_spte { |
| 301 | struct { |
| 302 | u32 spte_low; |
| 303 | u32 spte_high; |
| 304 | }; |
| 305 | u64 spte; |
| 306 | }; |
| 307 | |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 308 | static void count_spte_clear(u64 *sptep, u64 spte) |
| 309 | { |
Sean Christopherson | 5735468 | 2020-06-22 13:20:33 -0700 | [diff] [blame] | 310 | struct kvm_mmu_page *sp = sptep_to_sp(sptep); |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 311 | |
| 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 Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 320 | static 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 Amit | b19ee2f | 2016-05-11 08:04:29 -0700 | [diff] [blame] | 336 | WRITE_ONCE(ssptep->spte_low, sspte.spte_low); |
Xiao Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | static 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 Amit | b19ee2f | 2016-05-11 08:04:29 -0700 | [diff] [blame] | 346 | WRITE_ONCE(ssptep->spte_low, sspte.spte_low); |
Xiao Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 347 | |
| 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 Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 355 | count_spte_clear(sptep, spte); |
Xiao Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | static 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 Jin | 41bc318 | 2011-09-19 12:19:51 +0800 | [diff] [blame] | 367 | orig.spte_high = ssptep->spte_high; |
| 368 | ssptep->spte_high = sspte.spte_high; |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 369 | count_spte_clear(sptep, spte); |
Xiao Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 370 | |
| 371 | return orig.spte; |
| 372 | } |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 373 | |
| 374 | /* |
| 375 | * The idea using the light way get the spte on x86_32 guest is from |
Christoph Hellwig | 39656e8 | 2019-07-11 20:56:49 -0700 | [diff] [blame] | 376 | * gup_get_pte (mm/gup.c). |
Xiao Guangrong | accaefe | 2013-06-19 17:09:20 +0800 | [diff] [blame] | 377 | * |
| 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 Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 391 | */ |
| 392 | static u64 __get_spte_lockless(u64 *sptep) |
| 393 | { |
Sean Christopherson | 5735468 | 2020-06-22 13:20:33 -0700 | [diff] [blame] | 394 | struct kvm_mmu_page *sp = sptep_to_sp(sptep); |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 395 | union split_spte spte, *orig = (union split_spte *)sptep; |
| 396 | int count; |
| 397 | |
| 398 | retry: |
| 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 Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 414 | #endif |
| 415 | |
Xiao Guangrong | 8672b72 | 2010-08-02 16:14:04 +0800 | [diff] [blame] | 416 | static bool spte_has_volatile_bits(u64 spte) |
| 417 | { |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 418 | if (!is_shadow_present_pte(spte)) |
| 419 | return false; |
| 420 | |
Xiao Guangrong | c7ba5b4 | 2012-06-20 15:59:18 +0800 | [diff] [blame] | 421 | /* |
Adam Buchbinder | 6a6256f | 2016-02-23 15:34:30 -0800 | [diff] [blame] | 422 | * Always atomically update spte if it can be updated |
Xiao Guangrong | c7ba5b4 | 2012-06-20 15:59:18 +0800 | [diff] [blame] | 423 | * 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 Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 427 | if (spte_can_locklessly_be_made_writable(spte) || |
| 428 | is_access_track_spte(spte)) |
Xiao Guangrong | c7ba5b4 | 2012-06-20 15:59:18 +0800 | [diff] [blame] | 429 | return true; |
| 430 | |
Peter Feiner | ac8d57e | 2017-06-30 17:26:31 -0700 | [diff] [blame] | 431 | if (spte_ad_enabled(spte)) { |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 432 | if ((spte & shadow_accessed_mask) == 0 || |
| 433 | (is_writable_pte(spte) && (spte & shadow_dirty_mask) == 0)) |
| 434 | return true; |
| 435 | } |
Xiao Guangrong | 8672b72 | 2010-08-02 16:14:04 +0800 | [diff] [blame] | 436 | |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 437 | return false; |
Xiao Guangrong | 8672b72 | 2010-08-02 16:14:04 +0800 | [diff] [blame] | 438 | } |
| 439 | |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 440 | /* 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 | */ |
| 446 | static 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 Shahid | f39a058 | 2016-12-06 16:46:14 -0800 | [diff] [blame] | 452 | /* |
| 453 | * Update the SPTE (excluding the PFN), but do not track changes in its |
| 454 | * accessed/dirty status. |
| 455 | */ |
| 456 | static 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 Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 477 | /* Rules for using mmu_spte_update: |
Andrea Gelmini | bb3541f | 2016-05-21 14:14:44 +0200 | [diff] [blame] | 478 | * Update the state bits, it means the mapped pfn is not changed. |
Xiao Guangrong | 6e7d035 | 2012-06-20 15:58:33 +0800 | [diff] [blame] | 479 | * |
| 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 Shahid | 83ef6c8 | 2016-12-06 16:46:13 -0800 | [diff] [blame] | 485 | * |
| 486 | * Returns true if the TLB needs to be flushed |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 487 | */ |
Xiao Guangrong | 6e7d035 | 2012-06-20 15:58:33 +0800 | [diff] [blame] | 488 | static bool mmu_spte_update(u64 *sptep, u64 new_spte) |
Avi Kivity | b79b93f | 2010-06-06 15:46:44 +0300 | [diff] [blame] | 489 | { |
Junaid Shahid | 83ef6c8 | 2016-12-06 16:46:13 -0800 | [diff] [blame] | 490 | bool flush = false; |
Junaid Shahid | f39a058 | 2016-12-06 16:46:14 -0800 | [diff] [blame] | 491 | u64 old_spte = mmu_spte_update_no_track(sptep, new_spte); |
Avi Kivity | b79b93f | 2010-06-06 15:46:44 +0300 | [diff] [blame] | 492 | |
Junaid Shahid | f39a058 | 2016-12-06 16:46:14 -0800 | [diff] [blame] | 493 | if (!is_shadow_present_pte(old_spte)) |
| 494 | return false; |
Junaid Shahid | 83ef6c8 | 2016-12-06 16:46:13 -0800 | [diff] [blame] | 495 | |
Xiao Guangrong | c7ba5b4 | 2012-06-20 15:59:18 +0800 | [diff] [blame] | 496 | /* |
| 497 | * For the spte updated out of mmu-lock is safe, since |
Adam Buchbinder | 6a6256f | 2016-02-23 15:34:30 -0800 | [diff] [blame] | 498 | * we always atomically update it, see the comments in |
Xiao Guangrong | c7ba5b4 | 2012-06-20 15:59:18 +0800 | [diff] [blame] | 499 | * spte_has_volatile_bits(). |
| 500 | */ |
Junaid Shahid | ea4114b | 2016-12-06 16:46:11 -0800 | [diff] [blame] | 501 | if (spte_can_locklessly_be_made_writable(old_spte) && |
Xiao Guangrong | 7f31c95 | 2014-04-17 17:06:15 +0800 | [diff] [blame] | 502 | !is_writable_pte(new_spte)) |
Junaid Shahid | 83ef6c8 | 2016-12-06 16:46:13 -0800 | [diff] [blame] | 503 | flush = true; |
Xiao Guangrong | 4132779 | 2010-08-02 16:15:08 +0800 | [diff] [blame] | 504 | |
Kai Huang | 7e71a59 | 2015-01-09 16:44:30 +0800 | [diff] [blame] | 505 | /* |
Junaid Shahid | 83ef6c8 | 2016-12-06 16:46:13 -0800 | [diff] [blame] | 506 | * Flush TLB when accessed/dirty states are changed in the page tables, |
Kai Huang | 7e71a59 | 2015-01-09 16:44:30 +0800 | [diff] [blame] | 507 | * to guarantee consistency between TLB and page tables. |
| 508 | */ |
Kai Huang | 7e71a59 | 2015-01-09 16:44:30 +0800 | [diff] [blame] | 509 | |
Junaid Shahid | 83ef6c8 | 2016-12-06 16:46:13 -0800 | [diff] [blame] | 510 | if (is_accessed_spte(old_spte) && !is_accessed_spte(new_spte)) { |
| 511 | flush = true; |
Xiao Guangrong | 4132779 | 2010-08-02 16:15:08 +0800 | [diff] [blame] | 512 | kvm_set_pfn_accessed(spte_to_pfn(old_spte)); |
Junaid Shahid | 83ef6c8 | 2016-12-06 16:46:13 -0800 | [diff] [blame] | 513 | } |
Xiao Guangrong | 6e7d035 | 2012-06-20 15:58:33 +0800 | [diff] [blame] | 514 | |
Junaid Shahid | 83ef6c8 | 2016-12-06 16:46:13 -0800 | [diff] [blame] | 515 | 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 Kivity | b79b93f | 2010-06-06 15:46:44 +0300 | [diff] [blame] | 521 | } |
| 522 | |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 523 | /* |
| 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 Shahid | 83ef6c8 | 2016-12-06 16:46:13 -0800 | [diff] [blame] | 527 | * Returns non-zero if the PTE was previously valid. |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 528 | */ |
| 529 | static int mmu_spte_clear_track_bits(u64 *sptep) |
| 530 | { |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 531 | kvm_pfn_t pfn; |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 532 | u64 old_spte = *sptep; |
| 533 | |
| 534 | if (!spte_has_volatile_bits(old_spte)) |
Xiao Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 535 | __update_clear_spte_fast(sptep, 0ull); |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 536 | else |
Xiao Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 537 | old_spte = __update_clear_spte_slow(sptep, 0ull); |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 538 | |
Takuya Yoshikawa | afd28fe | 2015-11-20 17:44:55 +0900 | [diff] [blame] | 539 | if (!is_shadow_present_pte(old_spte)) |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 540 | return 0; |
| 541 | |
| 542 | pfn = spte_to_pfn(old_spte); |
Xiao Guangrong | 86fde74 | 2012-07-17 21:52:52 +0800 | [diff] [blame] | 543 | |
| 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 Biesheuvel | bf4bea8 | 2014-11-10 08:33:56 +0000 | [diff] [blame] | 549 | WARN_ON(!kvm_is_reserved_pfn(pfn) && !page_count(pfn_to_page(pfn))); |
Xiao Guangrong | 86fde74 | 2012-07-17 21:52:52 +0800 | [diff] [blame] | 550 | |
Junaid Shahid | 83ef6c8 | 2016-12-06 16:46:13 -0800 | [diff] [blame] | 551 | if (is_accessed_spte(old_spte)) |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 552 | kvm_set_pfn_accessed(pfn); |
Junaid Shahid | 83ef6c8 | 2016-12-06 16:46:13 -0800 | [diff] [blame] | 553 | |
| 554 | if (is_dirty_spte(old_spte)) |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 555 | kvm_set_pfn_dirty(pfn); |
Junaid Shahid | 83ef6c8 | 2016-12-06 16:46:13 -0800 | [diff] [blame] | 556 | |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 557 | 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 | */ |
| 565 | static void mmu_spte_clear_no_track(u64 *sptep) |
| 566 | { |
Xiao Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 567 | __update_clear_spte_fast(sptep, 0ull); |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 568 | } |
| 569 | |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 570 | static u64 mmu_spte_get_lockless(u64 *sptep) |
| 571 | { |
| 572 | return __get_spte_lockless(sptep); |
| 573 | } |
| 574 | |
Junaid Shahid | d3e328f2 | 2016-12-21 20:29:32 -0800 | [diff] [blame] | 575 | /* Restore an acc-track PTE back to a regular PTE */ |
| 576 | static u64 restore_acc_track_spte(u64 spte) |
| 577 | { |
| 578 | u64 new_spte = spte; |
Paolo Bonzini | 8a967d6 | 2020-10-30 13:39:55 -0400 | [diff] [blame] | 579 | u64 saved_bits = (spte >> SHADOW_ACC_TRACK_SAVED_BITS_SHIFT) |
| 580 | & SHADOW_ACC_TRACK_SAVED_BITS_MASK; |
Junaid Shahid | d3e328f2 | 2016-12-21 20:29:32 -0800 | [diff] [blame] | 581 | |
Peter Feiner | ac8d57e | 2017-06-30 17:26:31 -0700 | [diff] [blame] | 582 | WARN_ON_ONCE(spte_ad_enabled(spte)); |
Junaid Shahid | d3e328f2 | 2016-12-21 20:29:32 -0800 | [diff] [blame] | 583 | WARN_ON_ONCE(!is_access_track_spte(spte)); |
| 584 | |
| 585 | new_spte &= ~shadow_acc_track_mask; |
Paolo Bonzini | 8a967d6 | 2020-10-30 13:39:55 -0400 | [diff] [blame] | 586 | new_spte &= ~(SHADOW_ACC_TRACK_SAVED_BITS_MASK << |
| 587 | SHADOW_ACC_TRACK_SAVED_BITS_SHIFT); |
Junaid Shahid | d3e328f2 | 2016-12-21 20:29:32 -0800 | [diff] [blame] | 588 | new_spte |= saved_bits; |
| 589 | |
| 590 | return new_spte; |
| 591 | } |
| 592 | |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 593 | /* Returns the Accessed status of the PTE and resets it at the same time. */ |
| 594 | static 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 Feiner | ac8d57e | 2017-06-30 17:26:31 -0700 | [diff] [blame] | 601 | if (spte_ad_enabled(spte)) { |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 602 | 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 Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 619 | static void walk_shadow_page_lockless_begin(struct kvm_vcpu *vcpu) |
| 620 | { |
Avi Kivity | c142786 | 2012-05-14 15:44:06 +0300 | [diff] [blame] | 621 | /* |
| 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 Tianyu | 36ca7e0 | 2016-03-13 11:10:25 +0800 | [diff] [blame] | 626 | |
Avi Kivity | c142786 | 2012-05-14 15:44:06 +0300 | [diff] [blame] | 627 | /* |
| 628 | * Make sure a following spte read is not reordered ahead of the write |
| 629 | * to vcpu->mode. |
| 630 | */ |
Lan Tianyu | 36ca7e0 | 2016-03-13 11:10:25 +0800 | [diff] [blame] | 631 | smp_store_mb(vcpu->mode, READING_SHADOW_PAGE_TABLES); |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | static void walk_shadow_page_lockless_end(struct kvm_vcpu *vcpu) |
| 635 | { |
Avi Kivity | c142786 | 2012-05-14 15:44:06 +0300 | [diff] [blame] | 636 | /* |
| 637 | * Make sure the write to vcpu->mode is not reordered in front of |
Tianyu Lan | 9a98458 | 2018-09-07 05:45:02 +0000 | [diff] [blame] | 638 | * reads to sptes. If it does, kvm_mmu_commit_zap_page() can see us |
Avi Kivity | c142786 | 2012-05-14 15:44:06 +0300 | [diff] [blame] | 639 | * OUTSIDE_GUEST_MODE and proceed to free the shadow page table. |
| 640 | */ |
Lan Tianyu | 36ca7e0 | 2016-03-13 11:10:25 +0800 | [diff] [blame] | 641 | smp_store_release(&vcpu->mode, OUTSIDE_GUEST_MODE); |
Avi Kivity | c142786 | 2012-05-14 15:44:06 +0300 | [diff] [blame] | 642 | local_irq_enable(); |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 643 | } |
| 644 | |
Sean Christopherson | 378f5cd | 2020-07-02 19:35:36 -0700 | [diff] [blame] | 645 | static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu, bool maybe_indirect) |
Avi Kivity | 8c43850 | 2007-04-16 11:53:17 +0300 | [diff] [blame] | 646 | { |
| 647 | int r; |
| 648 | |
Sean Christopherson | 531281a | 2020-07-02 19:35:32 -0700 | [diff] [blame] | 649 | /* 1 rmap, 1 parent PTE per level, and the prefetched rmaps. */ |
Sean Christopherson | 94ce87e | 2020-07-02 19:35:37 -0700 | [diff] [blame] | 650 | r = kvm_mmu_topup_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache, |
| 651 | 1 + PT64_ROOT_MAX_LEVEL + PTE_PREFETCH_NUM); |
Avi Kivity | 2e3e588 | 2007-09-10 11:28:17 +0300 | [diff] [blame] | 652 | if (r) |
Sean Christopherson | 284aa86 | 2020-07-02 19:35:28 -0700 | [diff] [blame] | 653 | return r; |
Sean Christopherson | 94ce87e | 2020-07-02 19:35:37 -0700 | [diff] [blame] | 654 | r = kvm_mmu_topup_memory_cache(&vcpu->arch.mmu_shadow_page_cache, |
| 655 | PT64_ROOT_MAX_LEVEL); |
Avi Kivity | 2e3e588 | 2007-09-10 11:28:17 +0300 | [diff] [blame] | 656 | if (r) |
Sean Christopherson | 171a90d | 2020-07-02 19:35:33 -0700 | [diff] [blame] | 657 | return r; |
Sean Christopherson | 378f5cd | 2020-07-02 19:35:36 -0700 | [diff] [blame] | 658 | if (maybe_indirect) { |
Sean Christopherson | 94ce87e | 2020-07-02 19:35:37 -0700 | [diff] [blame] | 659 | r = kvm_mmu_topup_memory_cache(&vcpu->arch.mmu_gfn_array_cache, |
| 660 | PT64_ROOT_MAX_LEVEL); |
Sean Christopherson | 378f5cd | 2020-07-02 19:35:36 -0700 | [diff] [blame] | 661 | if (r) |
| 662 | return r; |
| 663 | } |
Sean Christopherson | 94ce87e | 2020-07-02 19:35:37 -0700 | [diff] [blame] | 664 | return kvm_mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache, |
| 665 | PT64_ROOT_MAX_LEVEL); |
Avi Kivity | 8c43850 | 2007-04-16 11:53:17 +0300 | [diff] [blame] | 666 | } |
| 667 | |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 668 | static void mmu_free_memory_caches(struct kvm_vcpu *vcpu) |
| 669 | { |
Sean Christopherson | 94ce87e | 2020-07-02 19:35:37 -0700 | [diff] [blame] | 670 | 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 Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 674 | } |
| 675 | |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 676 | static struct pte_list_desc *mmu_alloc_pte_list_desc(struct kvm_vcpu *vcpu) |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 677 | { |
Sean Christopherson | 94ce87e | 2020-07-02 19:35:37 -0700 | [diff] [blame] | 678 | return kvm_mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_list_desc_cache); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 679 | } |
| 680 | |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 681 | static void mmu_free_pte_list_desc(struct pte_list_desc *pte_list_desc) |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 682 | { |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 683 | kmem_cache_free(pte_list_desc_cache, pte_list_desc); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 684 | } |
| 685 | |
Lai Jiangshan | 2032a93 | 2010-05-26 16:49:59 +0800 | [diff] [blame] | 686 | static 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 | |
| 694 | static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn) |
| 695 | { |
Paolo Bonzini | e9f2a76 | 2019-06-30 08:36:21 -0400 | [diff] [blame] | 696 | if (!sp->role.direct) { |
Lai Jiangshan | 2032a93 | 2010-05-26 16:49:59 +0800 | [diff] [blame] | 697 | sp->gfns[index] = gfn; |
Paolo Bonzini | e9f2a76 | 2019-06-30 08:36:21 -0400 | [diff] [blame] | 698 | 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 Jiangshan | 2032a93 | 2010-05-26 16:49:59 +0800 | [diff] [blame] | 706 | } |
| 707 | |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 708 | /* |
Takuya Yoshikawa | d4dbf47 | 2010-12-07 12:59:07 +0900 | [diff] [blame] | 709 | * Return the pointer to the large page information for a given gfn, |
| 710 | * handling slots that are not large page aligned. |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 711 | */ |
Takuya Yoshikawa | d4dbf47 | 2010-12-07 12:59:07 +0900 | [diff] [blame] | 712 | static struct kvm_lpage_info *lpage_info_slot(gfn_t gfn, |
Ben Gardon | 8ca6f06 | 2021-04-01 16:37:24 -0700 | [diff] [blame] | 713 | const struct kvm_memory_slot *slot, int level) |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 714 | { |
| 715 | unsigned long idx; |
| 716 | |
Takuya Yoshikawa | fb03cb6 | 2012-02-08 12:59:10 +0900 | [diff] [blame] | 717 | idx = gfn_to_index(gfn, slot->base_gfn, level); |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 718 | return &slot->arch.lpage_info[level - 2][idx]; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 719 | } |
| 720 | |
Xiao Guangrong | 547ffae | 2016-02-24 17:51:07 +0800 | [diff] [blame] | 721 | static 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 Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 727 | for (i = PG_LEVEL_2M; i <= KVM_MAX_HUGEPAGE_LEVEL; ++i) { |
Xiao Guangrong | 547ffae | 2016-02-24 17:51:07 +0800 | [diff] [blame] | 728 | linfo = lpage_info_slot(gfn, slot, i); |
| 729 | linfo->disallow_lpage += count; |
| 730 | WARN_ON(linfo->disallow_lpage < 0); |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | void 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 | |
| 739 | void 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 Bonzini | 3ed1a47 | 2015-05-19 16:29:22 +0200 | [diff] [blame] | 744 | static void account_shadowed(struct kvm *kvm, struct kvm_mmu_page *sp) |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 745 | { |
Paolo Bonzini | 699023e | 2015-05-18 15:03:39 +0200 | [diff] [blame] | 746 | struct kvm_memslots *slots; |
Joerg Roedel | d25797b | 2009-07-27 16:30:43 +0200 | [diff] [blame] | 747 | struct kvm_memory_slot *slot; |
Paolo Bonzini | 3ed1a47 | 2015-05-19 16:29:22 +0200 | [diff] [blame] | 748 | gfn_t gfn; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 749 | |
Xiao Guangrong | 56ca57f | 2016-02-24 17:51:14 +0800 | [diff] [blame] | 750 | kvm->arch.indirect_shadow_pages++; |
Paolo Bonzini | 3ed1a47 | 2015-05-19 16:29:22 +0200 | [diff] [blame] | 751 | gfn = sp->gfn; |
Paolo Bonzini | 699023e | 2015-05-18 15:03:39 +0200 | [diff] [blame] | 752 | slots = kvm_memslots_for_spte_role(kvm, sp->role); |
| 753 | slot = __gfn_to_memslot(slots, gfn); |
Xiao Guangrong | 56ca57f | 2016-02-24 17:51:14 +0800 | [diff] [blame] | 754 | |
| 755 | /* the non-leaf shadow pages are keeping readonly. */ |
Sean Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 756 | if (sp->role.level > PG_LEVEL_4K) |
Xiao Guangrong | 56ca57f | 2016-02-24 17:51:14 +0800 | [diff] [blame] | 757 | return kvm_slot_page_track_add_page(kvm, slot, gfn, |
| 758 | KVM_PAGE_TRACK_WRITE); |
| 759 | |
Xiao Guangrong | 547ffae | 2016-02-24 17:51:07 +0800 | [diff] [blame] | 760 | kvm_mmu_gfn_disallow_lpage(slot, gfn); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 761 | } |
| 762 | |
Ben Gardon | 29cf0f5 | 2020-10-14 11:27:00 -0700 | [diff] [blame] | 763 | void account_huge_nx_page(struct kvm *kvm, struct kvm_mmu_page *sp) |
Paolo Bonzini | b8e8c83 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 764 | { |
| 765 | if (sp->lpage_disallowed) |
| 766 | return; |
| 767 | |
| 768 | ++kvm->stat.nx_lpage_splits; |
Junaid Shahid | 1aa9b95 | 2019-11-04 20:26:00 +0100 | [diff] [blame] | 769 | list_add_tail(&sp->lpage_disallowed_link, |
| 770 | &kvm->arch.lpage_disallowed_mmu_pages); |
Paolo Bonzini | b8e8c83 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 771 | sp->lpage_disallowed = true; |
| 772 | } |
| 773 | |
Paolo Bonzini | 3ed1a47 | 2015-05-19 16:29:22 +0200 | [diff] [blame] | 774 | static void unaccount_shadowed(struct kvm *kvm, struct kvm_mmu_page *sp) |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 775 | { |
Paolo Bonzini | 699023e | 2015-05-18 15:03:39 +0200 | [diff] [blame] | 776 | struct kvm_memslots *slots; |
Joerg Roedel | d25797b | 2009-07-27 16:30:43 +0200 | [diff] [blame] | 777 | struct kvm_memory_slot *slot; |
Paolo Bonzini | 3ed1a47 | 2015-05-19 16:29:22 +0200 | [diff] [blame] | 778 | gfn_t gfn; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 779 | |
Xiao Guangrong | 56ca57f | 2016-02-24 17:51:14 +0800 | [diff] [blame] | 780 | kvm->arch.indirect_shadow_pages--; |
Paolo Bonzini | 3ed1a47 | 2015-05-19 16:29:22 +0200 | [diff] [blame] | 781 | gfn = sp->gfn; |
Paolo Bonzini | 699023e | 2015-05-18 15:03:39 +0200 | [diff] [blame] | 782 | slots = kvm_memslots_for_spte_role(kvm, sp->role); |
| 783 | slot = __gfn_to_memslot(slots, gfn); |
Sean Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 784 | if (sp->role.level > PG_LEVEL_4K) |
Xiao Guangrong | 56ca57f | 2016-02-24 17:51:14 +0800 | [diff] [blame] | 785 | return kvm_slot_page_track_remove_page(kvm, slot, gfn, |
| 786 | KVM_PAGE_TRACK_WRITE); |
| 787 | |
Xiao Guangrong | 547ffae | 2016-02-24 17:51:07 +0800 | [diff] [blame] | 788 | kvm_mmu_gfn_allow_lpage(slot, gfn); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 789 | } |
| 790 | |
Ben Gardon | 29cf0f5 | 2020-10-14 11:27:00 -0700 | [diff] [blame] | 791 | void unaccount_huge_nx_page(struct kvm *kvm, struct kvm_mmu_page *sp) |
Paolo Bonzini | b8e8c83 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 792 | { |
| 793 | --kvm->stat.nx_lpage_splits; |
| 794 | sp->lpage_disallowed = false; |
Junaid Shahid | 1aa9b95 | 2019-11-04 20:26:00 +0100 | [diff] [blame] | 795 | list_del(&sp->lpage_disallowed_link); |
Paolo Bonzini | b8e8c83 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 796 | } |
| 797 | |
Xiao Guangrong | 5d163b1 | 2011-03-09 15:43:00 +0800 | [diff] [blame] | 798 | static struct kvm_memory_slot * |
| 799 | gfn_to_memslot_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t gfn, |
| 800 | bool no_dirty_log) |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 801 | { |
| 802 | struct kvm_memory_slot *slot; |
Xiao Guangrong | 5d163b1 | 2011-03-09 15:43:00 +0800 | [diff] [blame] | 803 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 804 | slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); |
Paolo Bonzini | 91b0d26 | 2020-01-21 16:16:32 +0100 | [diff] [blame] | 805 | if (!slot || slot->flags & KVM_MEMSLOT_INVALID) |
| 806 | return NULL; |
Peter Xu | 044c59c | 2020-09-30 21:22:26 -0400 | [diff] [blame] | 807 | if (no_dirty_log && kvm_slot_dirty_track_enabled(slot)) |
Paolo Bonzini | 91b0d26 | 2020-01-21 16:16:32 +0100 | [diff] [blame] | 808 | return NULL; |
Xiao Guangrong | 5d163b1 | 2011-03-09 15:43:00 +0800 | [diff] [blame] | 809 | |
| 810 | return slot; |
| 811 | } |
| 812 | |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 813 | /* |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 814 | * About rmap_head encoding: |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 815 | * |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 816 | * 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 Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 818 | * pte_list_desc containing more mappings. |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 819 | */ |
| 820 | |
| 821 | /* |
| 822 | * Returns the number of pointers in the rmap chain, not counting the new one. |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 823 | */ |
| 824 | static int pte_list_add(struct kvm_vcpu *vcpu, u64 *spte, |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 825 | struct kvm_rmap_head *rmap_head) |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 826 | { |
| 827 | struct pte_list_desc *desc; |
| 828 | int i, count = 0; |
| 829 | |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 830 | if (!rmap_head->val) { |
Stephen Zhang | 805a0f8 | 2021-01-27 10:08:45 +0800 | [diff] [blame] | 831 | rmap_printk("%p %llx 0->1\n", spte, *spte); |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 832 | rmap_head->val = (unsigned long)spte; |
| 833 | } else if (!(rmap_head->val & 1)) { |
Stephen Zhang | 805a0f8 | 2021-01-27 10:08:45 +0800 | [diff] [blame] | 834 | rmap_printk("%p %llx 1->many\n", spte, *spte); |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 835 | desc = mmu_alloc_pte_list_desc(vcpu); |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 836 | desc->sptes[0] = (u64 *)rmap_head->val; |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 837 | desc->sptes[1] = spte; |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 838 | rmap_head->val = (unsigned long)desc | 1; |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 839 | ++count; |
| 840 | } else { |
Stephen Zhang | 805a0f8 | 2021-01-27 10:08:45 +0800 | [diff] [blame] | 841 | rmap_printk("%p %llx many->many\n", spte, *spte); |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 842 | desc = (struct pte_list_desc *)(rmap_head->val & ~1ul); |
Li RongQing | c6c4f96 | 2020-09-27 16:44:57 +0800 | [diff] [blame] | 843 | while (desc->sptes[PTE_LIST_EXT-1]) { |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 844 | count += PTE_LIST_EXT; |
Li RongQing | c6c4f96 | 2020-09-27 16:44:57 +0800 | [diff] [blame] | 845 | |
| 846 | if (!desc->more) { |
| 847 | desc->more = mmu_alloc_pte_list_desc(vcpu); |
| 848 | desc = desc->more; |
| 849 | break; |
| 850 | } |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 851 | 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 Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 860 | static void |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 861 | pte_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 Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 864 | { |
| 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 Lin | fe3c2b4 | 2019-12-05 11:40:16 +0800 | [diff] [blame] | 874 | rmap_head->val = 0; |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 875 | else |
| 876 | if (prev_desc) |
| 877 | prev_desc->more = desc->more; |
| 878 | else |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 879 | rmap_head->val = (unsigned long)desc->more | 1; |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 880 | mmu_free_pte_list_desc(desc); |
| 881 | } |
| 882 | |
Wei Yang | 8daf346 | 2018-10-04 10:04:22 +0800 | [diff] [blame] | 883 | static void __pte_list_remove(u64 *spte, struct kvm_rmap_head *rmap_head) |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 884 | { |
| 885 | struct pte_list_desc *desc; |
| 886 | struct pte_list_desc *prev_desc; |
| 887 | int i; |
| 888 | |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 889 | if (!rmap_head->val) { |
Wei Yang | 8daf346 | 2018-10-04 10:04:22 +0800 | [diff] [blame] | 890 | pr_err("%s: %p 0->BUG\n", __func__, spte); |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 891 | BUG(); |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 892 | } else if (!(rmap_head->val & 1)) { |
Stephen Zhang | 805a0f8 | 2021-01-27 10:08:45 +0800 | [diff] [blame] | 893 | rmap_printk("%p 1->0\n", spte); |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 894 | if ((u64 *)rmap_head->val != spte) { |
Wei Yang | 8daf346 | 2018-10-04 10:04:22 +0800 | [diff] [blame] | 895 | pr_err("%s: %p 1->BUG\n", __func__, spte); |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 896 | BUG(); |
| 897 | } |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 898 | rmap_head->val = 0; |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 899 | } else { |
Stephen Zhang | 805a0f8 | 2021-01-27 10:08:45 +0800 | [diff] [blame] | 900 | rmap_printk("%p many->many\n", spte); |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 901 | desc = (struct pte_list_desc *)(rmap_head->val & ~1ul); |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 902 | prev_desc = NULL; |
| 903 | while (desc) { |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 904 | for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i) { |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 905 | if (desc->sptes[i] == spte) { |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 906 | pte_list_desc_remove_entry(rmap_head, |
| 907 | desc, i, prev_desc); |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 908 | return; |
| 909 | } |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 910 | } |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 911 | prev_desc = desc; |
| 912 | desc = desc->more; |
| 913 | } |
Wei Yang | 8daf346 | 2018-10-04 10:04:22 +0800 | [diff] [blame] | 914 | pr_err("%s: %p many->many\n", __func__, spte); |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 915 | BUG(); |
| 916 | } |
| 917 | } |
| 918 | |
Wei Yang | e791238 | 2018-10-04 10:04:23 +0800 | [diff] [blame] | 919 | static 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 Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 925 | static struct kvm_rmap_head *__gfn_to_rmap(gfn_t gfn, int level, |
| 926 | struct kvm_memory_slot *slot) |
Takuya Yoshikawa | 9b9b149 | 2011-11-14 18:22:28 +0900 | [diff] [blame] | 927 | { |
Takuya Yoshikawa | 77d1130 | 2012-07-02 17:57:17 +0900 | [diff] [blame] | 928 | unsigned long idx; |
Takuya Yoshikawa | 9b9b149 | 2011-11-14 18:22:28 +0900 | [diff] [blame] | 929 | |
Takuya Yoshikawa | 77d1130 | 2012-07-02 17:57:17 +0900 | [diff] [blame] | 930 | idx = gfn_to_index(gfn, slot->base_gfn, level); |
Sean Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 931 | return &slot->arch.rmap[level - PG_LEVEL_4K][idx]; |
Takuya Yoshikawa | 9b9b149 | 2011-11-14 18:22:28 +0900 | [diff] [blame] | 932 | } |
| 933 | |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 934 | static struct kvm_rmap_head *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, |
| 935 | struct kvm_mmu_page *sp) |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 936 | { |
Paolo Bonzini | 699023e | 2015-05-18 15:03:39 +0200 | [diff] [blame] | 937 | struct kvm_memslots *slots; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 938 | struct kvm_memory_slot *slot; |
| 939 | |
Paolo Bonzini | 699023e | 2015-05-18 15:03:39 +0200 | [diff] [blame] | 940 | slots = kvm_memslots_for_spte_role(kvm, sp->role); |
| 941 | slot = __gfn_to_memslot(slots, gfn); |
Paolo Bonzini | e4cd1da | 2015-05-18 15:11:46 +0200 | [diff] [blame] | 942 | return __gfn_to_rmap(gfn, sp->role.level, slot); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 943 | } |
| 944 | |
Xiao Guangrong | f759e2b | 2011-09-22 16:53:17 +0800 | [diff] [blame] | 945 | static bool rmap_can_add(struct kvm_vcpu *vcpu) |
| 946 | { |
Sean Christopherson | 356ec69 | 2020-07-02 19:35:27 -0700 | [diff] [blame] | 947 | struct kvm_mmu_memory_cache *mc; |
Xiao Guangrong | f759e2b | 2011-09-22 16:53:17 +0800 | [diff] [blame] | 948 | |
Sean Christopherson | 356ec69 | 2020-07-02 19:35:27 -0700 | [diff] [blame] | 949 | mc = &vcpu->arch.mmu_pte_list_desc_cache; |
Sean Christopherson | 94ce87e | 2020-07-02 19:35:37 -0700 | [diff] [blame] | 950 | return kvm_mmu_memory_cache_nr_free_objects(mc); |
Xiao Guangrong | f759e2b | 2011-09-22 16:53:17 +0800 | [diff] [blame] | 951 | } |
| 952 | |
Joerg Roedel | 44ad994 | 2009-07-27 16:30:42 +0200 | [diff] [blame] | 953 | static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn) |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 954 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 955 | struct kvm_mmu_page *sp; |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 956 | struct kvm_rmap_head *rmap_head; |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 957 | |
Sean Christopherson | 5735468 | 2020-06-22 13:20:33 -0700 | [diff] [blame] | 958 | sp = sptep_to_sp(spte); |
Lai Jiangshan | 2032a93 | 2010-05-26 16:49:59 +0800 | [diff] [blame] | 959 | kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn); |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 960 | rmap_head = gfn_to_rmap(vcpu->kvm, gfn, sp); |
| 961 | return pte_list_add(vcpu, spte, rmap_head); |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 962 | } |
| 963 | |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 964 | static void rmap_remove(struct kvm *kvm, u64 *spte) |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 965 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 966 | struct kvm_mmu_page *sp; |
Lai Jiangshan | 2032a93 | 2010-05-26 16:49:59 +0800 | [diff] [blame] | 967 | gfn_t gfn; |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 968 | struct kvm_rmap_head *rmap_head; |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 969 | |
Sean Christopherson | 5735468 | 2020-06-22 13:20:33 -0700 | [diff] [blame] | 970 | sp = sptep_to_sp(spte); |
Lai Jiangshan | 2032a93 | 2010-05-26 16:49:59 +0800 | [diff] [blame] | 971 | gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt); |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 972 | rmap_head = gfn_to_rmap(kvm, gfn, sp); |
Wei Yang | 8daf346 | 2018-10-04 10:04:22 +0800 | [diff] [blame] | 973 | __pte_list_remove(spte, rmap_head); |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 974 | } |
| 975 | |
Takuya Yoshikawa | 1e3f42f | 2012-03-21 23:50:34 +0900 | [diff] [blame] | 976 | /* |
| 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 | */ |
| 980 | struct 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 Lin | 0a03cbd | 2019-12-06 16:20:18 +0800 | [diff] [blame] | 989 | * information in the iterator may not be valid. |
Takuya Yoshikawa | 1e3f42f | 2012-03-21 23:50:34 +0900 | [diff] [blame] | 990 | * |
| 991 | * Returns sptep if found, NULL otherwise. |
| 992 | */ |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 993 | static u64 *rmap_get_first(struct kvm_rmap_head *rmap_head, |
| 994 | struct rmap_iterator *iter) |
Takuya Yoshikawa | 1e3f42f | 2012-03-21 23:50:34 +0900 | [diff] [blame] | 995 | { |
Takuya Yoshikawa | 77fbbbd | 2015-11-20 17:45:44 +0900 | [diff] [blame] | 996 | u64 *sptep; |
| 997 | |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 998 | if (!rmap_head->val) |
Takuya Yoshikawa | 1e3f42f | 2012-03-21 23:50:34 +0900 | [diff] [blame] | 999 | return NULL; |
| 1000 | |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 1001 | if (!(rmap_head->val & 1)) { |
Takuya Yoshikawa | 1e3f42f | 2012-03-21 23:50:34 +0900 | [diff] [blame] | 1002 | iter->desc = NULL; |
Takuya Yoshikawa | 77fbbbd | 2015-11-20 17:45:44 +0900 | [diff] [blame] | 1003 | sptep = (u64 *)rmap_head->val; |
| 1004 | goto out; |
Takuya Yoshikawa | 1e3f42f | 2012-03-21 23:50:34 +0900 | [diff] [blame] | 1005 | } |
| 1006 | |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 1007 | iter->desc = (struct pte_list_desc *)(rmap_head->val & ~1ul); |
Takuya Yoshikawa | 1e3f42f | 2012-03-21 23:50:34 +0900 | [diff] [blame] | 1008 | iter->pos = 0; |
Takuya Yoshikawa | 77fbbbd | 2015-11-20 17:45:44 +0900 | [diff] [blame] | 1009 | sptep = iter->desc->sptes[iter->pos]; |
| 1010 | out: |
| 1011 | BUG_ON(!is_shadow_present_pte(*sptep)); |
| 1012 | return sptep; |
Takuya Yoshikawa | 1e3f42f | 2012-03-21 23:50:34 +0900 | [diff] [blame] | 1013 | } |
| 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 | */ |
| 1020 | static u64 *rmap_get_next(struct rmap_iterator *iter) |
| 1021 | { |
Takuya Yoshikawa | 77fbbbd | 2015-11-20 17:45:44 +0900 | [diff] [blame] | 1022 | u64 *sptep; |
| 1023 | |
Takuya Yoshikawa | 1e3f42f | 2012-03-21 23:50:34 +0900 | [diff] [blame] | 1024 | if (iter->desc) { |
| 1025 | if (iter->pos < PTE_LIST_EXT - 1) { |
Takuya Yoshikawa | 1e3f42f | 2012-03-21 23:50:34 +0900 | [diff] [blame] | 1026 | ++iter->pos; |
| 1027 | sptep = iter->desc->sptes[iter->pos]; |
| 1028 | if (sptep) |
Takuya Yoshikawa | 77fbbbd | 2015-11-20 17:45:44 +0900 | [diff] [blame] | 1029 | goto out; |
Takuya Yoshikawa | 1e3f42f | 2012-03-21 23:50:34 +0900 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | iter->desc = iter->desc->more; |
| 1033 | |
| 1034 | if (iter->desc) { |
| 1035 | iter->pos = 0; |
| 1036 | /* desc->sptes[0] cannot be NULL */ |
Takuya Yoshikawa | 77fbbbd | 2015-11-20 17:45:44 +0900 | [diff] [blame] | 1037 | sptep = iter->desc->sptes[iter->pos]; |
| 1038 | goto out; |
Takuya Yoshikawa | 1e3f42f | 2012-03-21 23:50:34 +0900 | [diff] [blame] | 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | return NULL; |
Takuya Yoshikawa | 77fbbbd | 2015-11-20 17:45:44 +0900 | [diff] [blame] | 1043 | out: |
| 1044 | BUG_ON(!is_shadow_present_pte(*sptep)); |
| 1045 | return sptep; |
Takuya Yoshikawa | 1e3f42f | 2012-03-21 23:50:34 +0900 | [diff] [blame] | 1046 | } |
| 1047 | |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 1048 | #define for_each_rmap_spte(_rmap_head_, _iter_, _spte_) \ |
| 1049 | for (_spte_ = rmap_get_first(_rmap_head_, _iter_); \ |
Takuya Yoshikawa | 77fbbbd | 2015-11-20 17:45:44 +0900 | [diff] [blame] | 1050 | _spte_; _spte_ = rmap_get_next(_iter_)) |
Xiao Guangrong | 0d53679 | 2015-05-13 14:42:20 +0800 | [diff] [blame] | 1051 | |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 1052 | static void drop_spte(struct kvm *kvm, u64 *sptep) |
Xiao Guangrong | e4b502e | 2010-07-16 11:28:09 +0800 | [diff] [blame] | 1053 | { |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 1054 | if (mmu_spte_clear_track_bits(sptep)) |
Marcelo Tosatti | eb45fda | 2010-10-25 11:58:22 -0200 | [diff] [blame] | 1055 | rmap_remove(kvm, sptep); |
Avi Kivity | be38d27 | 2010-06-06 14:31:27 +0300 | [diff] [blame] | 1056 | } |
| 1057 | |
Xiao Guangrong | 8e22f95 | 2012-06-20 15:57:39 +0800 | [diff] [blame] | 1058 | |
| 1059 | static bool __drop_large_spte(struct kvm *kvm, u64 *sptep) |
| 1060 | { |
| 1061 | if (is_large_pte(*sptep)) { |
Sean Christopherson | 5735468 | 2020-06-22 13:20:33 -0700 | [diff] [blame] | 1062 | WARN_ON(sptep_to_sp(sptep)->role.level == PG_LEVEL_4K); |
Xiao Guangrong | 8e22f95 | 2012-06-20 15:57:39 +0800 | [diff] [blame] | 1063 | drop_spte(kvm, sptep); |
| 1064 | --kvm->stat.lpages; |
| 1065 | return true; |
| 1066 | } |
| 1067 | |
| 1068 | return false; |
| 1069 | } |
| 1070 | |
| 1071 | static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep) |
| 1072 | { |
Lan Tianyu | c3134ce | 2018-12-06 21:21:09 +0800 | [diff] [blame] | 1073 | if (__drop_large_spte(vcpu->kvm, sptep)) { |
Sean Christopherson | 5735468 | 2020-06-22 13:20:33 -0700 | [diff] [blame] | 1074 | struct kvm_mmu_page *sp = sptep_to_sp(sptep); |
Lan Tianyu | c3134ce | 2018-12-06 21:21:09 +0800 | [diff] [blame] | 1075 | |
| 1076 | kvm_flush_remote_tlbs_with_address(vcpu->kvm, sp->gfn, |
| 1077 | KVM_PAGES_PER_HPAGE(sp->role.level)); |
| 1078 | } |
Xiao Guangrong | 8e22f95 | 2012-06-20 15:57:39 +0800 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | /* |
Xiao Guangrong | 49fde34 | 2012-06-20 15:58:58 +0800 | [diff] [blame] | 1082 | * Write-protect on the specified @sptep, @pt_protect indicates whether |
Xiao Guangrong | c126d94 | 2014-04-17 17:06:14 +0800 | [diff] [blame] | 1083 | * spte write-protection is caused by protecting shadow page table. |
Xiao Guangrong | 49fde34 | 2012-06-20 15:58:58 +0800 | [diff] [blame] | 1084 | * |
Tiejun Chen | b461966 | 2014-09-22 10:31:38 +0800 | [diff] [blame] | 1085 | * Note: write protection is difference between dirty logging and spte |
Xiao Guangrong | 49fde34 | 2012-06-20 15:58:58 +0800 | [diff] [blame] | 1086 | * 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 Guangrong | 8e22f95 | 2012-06-20 15:57:39 +0800 | [diff] [blame] | 1091 | * |
Xiao Guangrong | c126d94 | 2014-04-17 17:06:14 +0800 | [diff] [blame] | 1092 | * Return true if tlb need be flushed. |
Xiao Guangrong | 8e22f95 | 2012-06-20 15:57:39 +0800 | [diff] [blame] | 1093 | */ |
Bandan Das | c4f138b | 2016-08-02 16:32:37 -0400 | [diff] [blame] | 1094 | static bool spte_write_protect(u64 *sptep, bool pt_protect) |
Xiao Guangrong | d13bc5b | 2012-06-20 15:57:15 +0800 | [diff] [blame] | 1095 | { |
| 1096 | u64 spte = *sptep; |
| 1097 | |
Xiao Guangrong | 49fde34 | 2012-06-20 15:58:58 +0800 | [diff] [blame] | 1098 | if (!is_writable_pte(spte) && |
Junaid Shahid | ea4114b | 2016-12-06 16:46:11 -0800 | [diff] [blame] | 1099 | !(pt_protect && spte_can_locklessly_be_made_writable(spte))) |
Xiao Guangrong | d13bc5b | 2012-06-20 15:57:15 +0800 | [diff] [blame] | 1100 | return false; |
| 1101 | |
Stephen Zhang | 805a0f8 | 2021-01-27 10:08:45 +0800 | [diff] [blame] | 1102 | rmap_printk("spte %p %llx\n", sptep, *sptep); |
Xiao Guangrong | d13bc5b | 2012-06-20 15:57:15 +0800 | [diff] [blame] | 1103 | |
Xiao Guangrong | 49fde34 | 2012-06-20 15:58:58 +0800 | [diff] [blame] | 1104 | if (pt_protect) |
Sean Christopherson | 5fc3424 | 2021-02-25 12:47:43 -0800 | [diff] [blame] | 1105 | spte &= ~shadow_mmu_writable_mask; |
Xiao Guangrong | d13bc5b | 2012-06-20 15:57:15 +0800 | [diff] [blame] | 1106 | spte = spte & ~PT_WRITABLE_MASK; |
Xiao Guangrong | 49fde34 | 2012-06-20 15:58:58 +0800 | [diff] [blame] | 1107 | |
Xiao Guangrong | c126d94 | 2014-04-17 17:06:14 +0800 | [diff] [blame] | 1108 | return mmu_spte_update(sptep, spte); |
Xiao Guangrong | d13bc5b | 2012-06-20 15:57:15 +0800 | [diff] [blame] | 1109 | } |
| 1110 | |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 1111 | static bool __rmap_write_protect(struct kvm *kvm, |
| 1112 | struct kvm_rmap_head *rmap_head, |
Takuya Yoshikawa | 245c391 | 2013-01-08 19:44:09 +0900 | [diff] [blame] | 1113 | bool pt_protect) |
Takuya Yoshikawa | a0ed460 | 2012-03-01 19:31:22 +0900 | [diff] [blame] | 1114 | { |
Takuya Yoshikawa | 1e3f42f | 2012-03-21 23:50:34 +0900 | [diff] [blame] | 1115 | u64 *sptep; |
| 1116 | struct rmap_iterator iter; |
Xiao Guangrong | d13bc5b | 2012-06-20 15:57:15 +0800 | [diff] [blame] | 1117 | bool flush = false; |
Takuya Yoshikawa | a0ed460 | 2012-03-01 19:31:22 +0900 | [diff] [blame] | 1118 | |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 1119 | for_each_rmap_spte(rmap_head, &iter, sptep) |
Bandan Das | c4f138b | 2016-08-02 16:32:37 -0400 | [diff] [blame] | 1120 | flush |= spte_write_protect(sptep, pt_protect); |
Takuya Yoshikawa | a0ed460 | 2012-03-01 19:31:22 +0900 | [diff] [blame] | 1121 | |
Xiao Guangrong | d13bc5b | 2012-06-20 15:57:15 +0800 | [diff] [blame] | 1122 | return flush; |
Takuya Yoshikawa | a0ed460 | 2012-03-01 19:31:22 +0900 | [diff] [blame] | 1123 | } |
| 1124 | |
Bandan Das | c4f138b | 2016-08-02 16:32:37 -0400 | [diff] [blame] | 1125 | static bool spte_clear_dirty(u64 *sptep) |
Kai Huang | f4b4b18 | 2015-01-28 10:54:24 +0800 | [diff] [blame] | 1126 | { |
| 1127 | u64 spte = *sptep; |
| 1128 | |
Stephen Zhang | 805a0f8 | 2021-01-27 10:08:45 +0800 | [diff] [blame] | 1129 | rmap_printk("spte %p %llx\n", sptep, *sptep); |
Kai Huang | f4b4b18 | 2015-01-28 10:54:24 +0800 | [diff] [blame] | 1130 | |
Paolo Bonzini | 1f4e5fc | 2019-09-26 18:47:59 +0200 | [diff] [blame] | 1131 | MMU_WARN_ON(!spte_ad_enabled(spte)); |
Kai Huang | f4b4b18 | 2015-01-28 10:54:24 +0800 | [diff] [blame] | 1132 | spte &= ~shadow_dirty_mask; |
Kai Huang | f4b4b18 | 2015-01-28 10:54:24 +0800 | [diff] [blame] | 1133 | return mmu_spte_update(sptep, spte); |
| 1134 | } |
| 1135 | |
Paolo Bonzini | 1f4e5fc | 2019-09-26 18:47:59 +0200 | [diff] [blame] | 1136 | static bool spte_wrprot_for_clear_dirty(u64 *sptep) |
Peter Feiner | ac8d57e | 2017-06-30 17:26:31 -0700 | [diff] [blame] | 1137 | { |
| 1138 | bool was_writable = test_and_clear_bit(PT_WRITABLE_SHIFT, |
| 1139 | (unsigned long *)sptep); |
Paolo Bonzini | 1f4e5fc | 2019-09-26 18:47:59 +0200 | [diff] [blame] | 1140 | if (was_writable && !spte_ad_enabled(*sptep)) |
Peter Feiner | ac8d57e | 2017-06-30 17:26:31 -0700 | [diff] [blame] | 1141 | 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 Christopherson | 0a234f5 | 2021-02-12 16:50:05 -0800 | [diff] [blame] | 1152 | static bool __rmap_clear_dirty(struct kvm *kvm, struct kvm_rmap_head *rmap_head, |
| 1153 | struct kvm_memory_slot *slot) |
Kai Huang | f4b4b18 | 2015-01-28 10:54:24 +0800 | [diff] [blame] | 1154 | { |
| 1155 | u64 *sptep; |
| 1156 | struct rmap_iterator iter; |
| 1157 | bool flush = false; |
| 1158 | |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 1159 | for_each_rmap_spte(rmap_head, &iter, sptep) |
Paolo Bonzini | 1f4e5fc | 2019-09-26 18:47:59 +0200 | [diff] [blame] | 1160 | if (spte_ad_need_write_protect(*sptep)) |
| 1161 | flush |= spte_wrprot_for_clear_dirty(sptep); |
Peter Feiner | ac8d57e | 2017-06-30 17:26:31 -0700 | [diff] [blame] | 1162 | else |
Paolo Bonzini | 1f4e5fc | 2019-09-26 18:47:59 +0200 | [diff] [blame] | 1163 | flush |= spte_clear_dirty(sptep); |
Kai Huang | f4b4b18 | 2015-01-28 10:54:24 +0800 | [diff] [blame] | 1164 | |
| 1165 | return flush; |
| 1166 | } |
| 1167 | |
Takuya Yoshikawa | 5dc99b23 | 2012-03-01 19:32:16 +0900 | [diff] [blame] | 1168 | /** |
Kai Huang | 3b0f1d0 | 2015-01-28 10:54:23 +0800 | [diff] [blame] | 1169 | * kvm_mmu_write_protect_pt_masked - write protect selected PT level pages |
Takuya Yoshikawa | 5dc99b23 | 2012-03-01 19:32:16 +0900 | [diff] [blame] | 1170 | * @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 Zhu | 8921291 | 2021-04-29 11:41:15 +0800 | [diff] [blame^] | 1175 | * Used when we do not need to care about huge page mappings. |
Takuya Yoshikawa | 5dc99b23 | 2012-03-01 19:32:16 +0900 | [diff] [blame] | 1176 | */ |
Kai Huang | 3b0f1d0 | 2015-01-28 10:54:23 +0800 | [diff] [blame] | 1177 | static void kvm_mmu_write_protect_pt_masked(struct kvm *kvm, |
Takuya Yoshikawa | 5dc99b23 | 2012-03-01 19:32:16 +0900 | [diff] [blame] | 1178 | struct kvm_memory_slot *slot, |
| 1179 | gfn_t gfn_offset, unsigned long mask) |
Izik Eidus | 98348e9 | 2007-10-16 14:42:30 +0200 | [diff] [blame] | 1180 | { |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 1181 | struct kvm_rmap_head *rmap_head; |
Takuya Yoshikawa | 5dc99b23 | 2012-03-01 19:32:16 +0900 | [diff] [blame] | 1182 | |
Paolo Bonzini | 897218f | 2021-02-06 09:53:33 -0500 | [diff] [blame] | 1183 | if (is_tdp_mmu_enabled(kvm)) |
Ben Gardon | a6a0b05 | 2020-10-14 11:26:55 -0700 | [diff] [blame] | 1184 | kvm_tdp_mmu_clear_dirty_pt_masked(kvm, slot, |
| 1185 | slot->base_gfn + gfn_offset, mask, true); |
Takuya Yoshikawa | 5dc99b23 | 2012-03-01 19:32:16 +0900 | [diff] [blame] | 1186 | while (mask) { |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 1187 | rmap_head = __gfn_to_rmap(slot->base_gfn + gfn_offset + __ffs(mask), |
Sean Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 1188 | PG_LEVEL_4K, slot); |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 1189 | __rmap_write_protect(kvm, rmap_head, false); |
Takuya Yoshikawa | 5dc99b23 | 2012-03-01 19:32:16 +0900 | [diff] [blame] | 1190 | |
| 1191 | /* clear the first set bit */ |
| 1192 | mask &= mask - 1; |
| 1193 | } |
| 1194 | } |
| 1195 | |
Kai Huang | 3b0f1d0 | 2015-01-28 10:54:23 +0800 | [diff] [blame] | 1196 | /** |
Peter Feiner | ac8d57e | 2017-06-30 17:26:31 -0700 | [diff] [blame] | 1197 | * 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 Huang | f4b4b18 | 2015-01-28 10:54:24 +0800 | [diff] [blame] | 1199 | * @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 Christopherson | a018eba | 2021-02-12 16:50:10 -0800 | [diff] [blame] | 1206 | static 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 Huang | f4b4b18 | 2015-01-28 10:54:24 +0800 | [diff] [blame] | 1209 | { |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 1210 | struct kvm_rmap_head *rmap_head; |
Kai Huang | f4b4b18 | 2015-01-28 10:54:24 +0800 | [diff] [blame] | 1211 | |
Paolo Bonzini | 897218f | 2021-02-06 09:53:33 -0500 | [diff] [blame] | 1212 | if (is_tdp_mmu_enabled(kvm)) |
Ben Gardon | a6a0b05 | 2020-10-14 11:26:55 -0700 | [diff] [blame] | 1213 | kvm_tdp_mmu_clear_dirty_pt_masked(kvm, slot, |
| 1214 | slot->base_gfn + gfn_offset, mask, false); |
Kai Huang | f4b4b18 | 2015-01-28 10:54:24 +0800 | [diff] [blame] | 1215 | while (mask) { |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 1216 | rmap_head = __gfn_to_rmap(slot->base_gfn + gfn_offset + __ffs(mask), |
Sean Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 1217 | PG_LEVEL_4K, slot); |
Sean Christopherson | 0a234f5 | 2021-02-12 16:50:05 -0800 | [diff] [blame] | 1218 | __rmap_clear_dirty(kvm, rmap_head, slot); |
Kai Huang | f4b4b18 | 2015-01-28 10:54:24 +0800 | [diff] [blame] | 1219 | |
| 1220 | /* clear the first set bit */ |
| 1221 | mask &= mask - 1; |
| 1222 | } |
| 1223 | } |
Kai Huang | f4b4b18 | 2015-01-28 10:54:24 +0800 | [diff] [blame] | 1224 | |
| 1225 | /** |
Kai Huang | 3b0f1d0 | 2015-01-28 10:54:23 +0800 | [diff] [blame] | 1226 | * 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 Zhu | 8921291 | 2021-04-29 11:41:15 +0800 | [diff] [blame^] | 1232 | * We need to care about huge page mappings: e.g. during dirty logging we may |
| 1233 | * have such mappings. |
Kai Huang | 3b0f1d0 | 2015-01-28 10:54:23 +0800 | [diff] [blame] | 1234 | */ |
| 1235 | void 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 Zhu | 8921291 | 2021-04-29 11:41:15 +0800 | [diff] [blame^] | 1239 | /* |
| 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 Christopherson | a018eba | 2021-02-12 16:50:10 -0800 | [diff] [blame] | 1262 | if (kvm_x86_ops.cpu_dirty_log_size) |
| 1263 | kvm_mmu_clear_dirty_pt_masked(kvm, slot, gfn_offset, mask); |
Kai Huang | 88178fd | 2015-01-28 10:54:27 +0800 | [diff] [blame] | 1264 | else |
| 1265 | kvm_mmu_write_protect_pt_masked(kvm, slot, gfn_offset, mask); |
Kai Huang | 3b0f1d0 | 2015-01-28 10:54:23 +0800 | [diff] [blame] | 1266 | } |
| 1267 | |
Peter Xu | fb04a1e | 2020-09-30 21:22:22 -0400 | [diff] [blame] | 1268 | int kvm_cpu_dirty_log_size(void) |
| 1269 | { |
Sean Christopherson | 6dd0380 | 2021-02-12 16:50:09 -0800 | [diff] [blame] | 1270 | return kvm_x86_ops.cpu_dirty_log_size; |
Peter Xu | fb04a1e | 2020-09-30 21:22:22 -0400 | [diff] [blame] | 1271 | } |
| 1272 | |
Xiao Guangrong | aeecee2 | 2016-02-24 17:51:08 +0800 | [diff] [blame] | 1273 | bool kvm_mmu_slot_gfn_write_protect(struct kvm *kvm, |
Keqian Zhu | 3ad9356 | 2021-04-29 11:41:14 +0800 | [diff] [blame] | 1274 | struct kvm_memory_slot *slot, u64 gfn, |
| 1275 | int min_level) |
Takuya Yoshikawa | 5dc99b23 | 2012-03-01 19:32:16 +0900 | [diff] [blame] | 1276 | { |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 1277 | struct kvm_rmap_head *rmap_head; |
Takuya Yoshikawa | 5dc99b23 | 2012-03-01 19:32:16 +0900 | [diff] [blame] | 1278 | int i; |
Xiao Guangrong | 2f84569 | 2012-06-20 15:56:53 +0800 | [diff] [blame] | 1279 | bool write_protected = false; |
Takuya Yoshikawa | 5dc99b23 | 2012-03-01 19:32:16 +0900 | [diff] [blame] | 1280 | |
Keqian Zhu | 3ad9356 | 2021-04-29 11:41:14 +0800 | [diff] [blame] | 1281 | for (i = min_level; i <= KVM_MAX_HUGEPAGE_LEVEL; ++i) { |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 1282 | rmap_head = __gfn_to_rmap(gfn, i, slot); |
Xiao Guangrong | aeecee2 | 2016-02-24 17:51:08 +0800 | [diff] [blame] | 1283 | write_protected |= __rmap_write_protect(kvm, rmap_head, true); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1284 | } |
| 1285 | |
Paolo Bonzini | 897218f | 2021-02-06 09:53:33 -0500 | [diff] [blame] | 1286 | if (is_tdp_mmu_enabled(kvm)) |
Ben Gardon | 46044f7 | 2020-10-14 11:26:57 -0700 | [diff] [blame] | 1287 | write_protected |= |
Keqian Zhu | 3ad9356 | 2021-04-29 11:41:14 +0800 | [diff] [blame] | 1288 | kvm_tdp_mmu_write_protect_gfn(kvm, slot, gfn, min_level); |
Ben Gardon | 46044f7 | 2020-10-14 11:26:57 -0700 | [diff] [blame] | 1289 | |
Marcelo Tosatti | b1a3682 | 2008-12-01 22:32:03 -0200 | [diff] [blame] | 1290 | return write_protected; |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1291 | } |
| 1292 | |
Xiao Guangrong | aeecee2 | 2016-02-24 17:51:08 +0800 | [diff] [blame] | 1293 | static 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 Zhu | 3ad9356 | 2021-04-29 11:41:14 +0800 | [diff] [blame] | 1298 | return kvm_mmu_slot_gfn_write_protect(vcpu->kvm, slot, gfn, PG_LEVEL_4K); |
Xiao Guangrong | aeecee2 | 2016-02-24 17:51:08 +0800 | [diff] [blame] | 1299 | } |
| 1300 | |
Sean Christopherson | 0a234f5 | 2021-02-12 16:50:05 -0800 | [diff] [blame] | 1301 | static bool kvm_zap_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head, |
| 1302 | struct kvm_memory_slot *slot) |
Xiao Guangrong | 6a49f85 | 2015-05-13 14:42:25 +0800 | [diff] [blame] | 1303 | { |
| 1304 | u64 *sptep; |
| 1305 | struct rmap_iterator iter; |
| 1306 | bool flush = false; |
| 1307 | |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 1308 | while ((sptep = rmap_get_first(rmap_head, &iter))) { |
Stephen Zhang | 805a0f8 | 2021-01-27 10:08:45 +0800 | [diff] [blame] | 1309 | rmap_printk("spte %p %llx.\n", sptep, *sptep); |
Xiao Guangrong | 6a49f85 | 2015-05-13 14:42:25 +0800 | [diff] [blame] | 1310 | |
Wei Yang | e791238 | 2018-10-04 10:04:23 +0800 | [diff] [blame] | 1311 | pte_list_remove(rmap_head, sptep); |
Xiao Guangrong | 6a49f85 | 2015-05-13 14:42:25 +0800 | [diff] [blame] | 1312 | flush = true; |
| 1313 | } |
| 1314 | |
| 1315 | return flush; |
| 1316 | } |
| 1317 | |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1318 | static 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 Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1321 | { |
Sean Christopherson | 0a234f5 | 2021-02-12 16:50:05 -0800 | [diff] [blame] | 1322 | return kvm_zap_rmapp(kvm, rmap_head, slot); |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1323 | } |
| 1324 | |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1325 | static 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 Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 1328 | { |
Takuya Yoshikawa | 1e3f42f | 2012-03-21 23:50:34 +0900 | [diff] [blame] | 1329 | u64 *sptep; |
| 1330 | struct rmap_iterator iter; |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 1331 | int need_flush = 0; |
Takuya Yoshikawa | 1e3f42f | 2012-03-21 23:50:34 +0900 | [diff] [blame] | 1332 | u64 new_spte; |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 1333 | kvm_pfn_t new_pfn; |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 1334 | |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1335 | WARN_ON(pte_huge(pte)); |
| 1336 | new_pfn = pte_pfn(pte); |
Takuya Yoshikawa | 1e3f42f | 2012-03-21 23:50:34 +0900 | [diff] [blame] | 1337 | |
Xiao Guangrong | 0d53679 | 2015-05-13 14:42:20 +0800 | [diff] [blame] | 1338 | restart: |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 1339 | for_each_rmap_spte(rmap_head, &iter, sptep) { |
Stephen Zhang | 805a0f8 | 2021-01-27 10:08:45 +0800 | [diff] [blame] | 1340 | rmap_printk("spte %p %llx gfn %llx (%d)\n", |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 1341 | sptep, *sptep, gfn, level); |
Takuya Yoshikawa | 1e3f42f | 2012-03-21 23:50:34 +0900 | [diff] [blame] | 1342 | |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 1343 | need_flush = 1; |
Takuya Yoshikawa | 1e3f42f | 2012-03-21 23:50:34 +0900 | [diff] [blame] | 1344 | |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1345 | if (pte_write(pte)) { |
Wei Yang | e791238 | 2018-10-04 10:04:23 +0800 | [diff] [blame] | 1346 | pte_list_remove(rmap_head, sptep); |
Xiao Guangrong | 0d53679 | 2015-05-13 14:42:20 +0800 | [diff] [blame] | 1347 | goto restart; |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 1348 | } else { |
Paolo Bonzini | cb3eeda | 2020-09-28 10:17:17 -0400 | [diff] [blame] | 1349 | new_spte = kvm_mmu_changed_pte_notifier_make_spte( |
| 1350 | *sptep, new_pfn); |
Takuya Yoshikawa | 1e3f42f | 2012-03-21 23:50:34 +0900 | [diff] [blame] | 1351 | |
| 1352 | mmu_spte_clear_track_bits(sptep); |
| 1353 | mmu_spte_set(sptep, new_spte); |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 1354 | } |
| 1355 | } |
Takuya Yoshikawa | 1e3f42f | 2012-03-21 23:50:34 +0900 | [diff] [blame] | 1356 | |
Lan Tianyu | 3cc5ea9 | 2018-12-06 21:21:12 +0800 | [diff] [blame] | 1357 | 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 Tianyu | 0cf853c | 2018-12-06 21:21:11 +0800 | [diff] [blame] | 1362 | return need_flush; |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 1363 | } |
| 1364 | |
Xiao Guangrong | 6ce1f4e | 2015-05-13 14:42:22 +0800 | [diff] [blame] | 1365 | struct 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 Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 1375 | struct kvm_rmap_head *rmap; |
Xiao Guangrong | 6ce1f4e | 2015-05-13 14:42:22 +0800 | [diff] [blame] | 1376 | int level; |
| 1377 | |
| 1378 | /* private field. */ |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 1379 | struct kvm_rmap_head *end_rmap; |
Xiao Guangrong | 6ce1f4e | 2015-05-13 14:42:22 +0800 | [diff] [blame] | 1380 | }; |
| 1381 | |
| 1382 | static void |
| 1383 | rmap_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 | |
| 1392 | static void |
| 1393 | slot_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 | |
| 1406 | static bool slot_rmap_walk_okay(struct slot_rmap_walk_iterator *iterator) |
| 1407 | { |
| 1408 | return !!iterator->rmap; |
| 1409 | } |
| 1410 | |
| 1411 | static 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 Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1433 | typedef 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 Christopherson | c1b9149 | 2021-02-25 17:03:28 -0800 | [diff] [blame] | 1436 | |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1437 | static __always_inline bool kvm_handle_gfn_range(struct kvm *kvm, |
| 1438 | struct kvm_gfn_range *range, |
| 1439 | rmap_handler_t handler) |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1440 | { |
Xiao Guangrong | 6ce1f4e | 2015-05-13 14:42:22 +0800 | [diff] [blame] | 1441 | struct slot_rmap_walk_iterator iterator; |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1442 | bool ret = false; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1443 | |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1444 | 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 Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1448 | |
Takuya Yoshikawa | f395302 | 2012-07-02 17:58:48 +0900 | [diff] [blame] | 1449 | return ret; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1450 | } |
| 1451 | |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1452 | bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range) |
Takuya Yoshikawa | 84504ef | 2012-07-02 17:55:48 +0900 | [diff] [blame] | 1453 | { |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1454 | bool flush; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1455 | |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1456 | flush = kvm_handle_gfn_range(kvm, range, kvm_unmap_rmapp); |
Ben Gardon | 063afac | 2020-10-14 11:26:52 -0700 | [diff] [blame] | 1457 | |
Paolo Bonzini | 897218f | 2021-02-06 09:53:33 -0500 | [diff] [blame] | 1458 | if (is_tdp_mmu_enabled(kvm)) |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1459 | flush |= kvm_tdp_mmu_unmap_gfn_range(kvm, range, flush); |
Ben Gardon | 063afac | 2020-10-14 11:26:52 -0700 | [diff] [blame] | 1460 | |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1461 | return flush; |
Takuya Yoshikawa | b3ae209 | 2012-07-02 17:56:33 +0900 | [diff] [blame] | 1462 | } |
| 1463 | |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1464 | bool kvm_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range) |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 1465 | { |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1466 | bool flush; |
Ben Gardon | 1d8dd6b | 2020-10-14 11:26:54 -0700 | [diff] [blame] | 1467 | |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1468 | flush = kvm_handle_gfn_range(kvm, range, kvm_set_pte_rmapp); |
Ben Gardon | 1d8dd6b | 2020-10-14 11:26:54 -0700 | [diff] [blame] | 1469 | |
Paolo Bonzini | 897218f | 2021-02-06 09:53:33 -0500 | [diff] [blame] | 1470 | if (is_tdp_mmu_enabled(kvm)) |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1471 | flush |= kvm_tdp_mmu_set_spte_gfn(kvm, range); |
Ben Gardon | 1d8dd6b | 2020-10-14 11:26:54 -0700 | [diff] [blame] | 1472 | |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1473 | return flush; |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 1474 | } |
| 1475 | |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1476 | static 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 Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1479 | { |
Takuya Yoshikawa | 1e3f42f | 2012-03-21 23:50:34 +0900 | [diff] [blame] | 1480 | u64 *sptep; |
Kees Cook | 3f649ab | 2020-06-03 13:09:38 -0700 | [diff] [blame] | 1481 | struct rmap_iterator iter; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1482 | int young = 0; |
| 1483 | |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 1484 | for_each_rmap_spte(rmap_head, &iter, sptep) |
| 1485 | young |= mmu_spte_age(sptep); |
Xiao Guangrong | 0d53679 | 2015-05-13 14:42:20 +0800 | [diff] [blame] | 1486 | |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1487 | return young; |
| 1488 | } |
| 1489 | |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1490 | static 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 Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 1493 | { |
Takuya Yoshikawa | 1e3f42f | 2012-03-21 23:50:34 +0900 | [diff] [blame] | 1494 | u64 *sptep; |
| 1495 | struct rmap_iterator iter; |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 1496 | |
Junaid Shahid | 83ef6c8 | 2016-12-06 16:46:13 -0800 | [diff] [blame] | 1497 | for_each_rmap_spte(rmap_head, &iter, sptep) |
| 1498 | if (is_accessed_spte(*sptep)) |
| 1499 | return 1; |
Junaid Shahid | 83ef6c8 | 2016-12-06 16:46:13 -0800 | [diff] [blame] | 1500 | return 0; |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 1501 | } |
| 1502 | |
Marcelo Tosatti | 53a27b3 | 2009-08-05 15:43:58 -0300 | [diff] [blame] | 1503 | #define RMAP_RECYCLE_THRESHOLD 1000 |
| 1504 | |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 1505 | static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn) |
Marcelo Tosatti | 53a27b3 | 2009-08-05 15:43:58 -0300 | [diff] [blame] | 1506 | { |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 1507 | struct kvm_rmap_head *rmap_head; |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 1508 | struct kvm_mmu_page *sp; |
| 1509 | |
Sean Christopherson | 5735468 | 2020-06-22 13:20:33 -0700 | [diff] [blame] | 1510 | sp = sptep_to_sp(spte); |
Marcelo Tosatti | 53a27b3 | 2009-08-05 15:43:58 -0300 | [diff] [blame] | 1511 | |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 1512 | rmap_head = gfn_to_rmap(vcpu->kvm, gfn, sp); |
Marcelo Tosatti | 53a27b3 | 2009-08-05 15:43:58 -0300 | [diff] [blame] | 1513 | |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1514 | kvm_unmap_rmapp(vcpu->kvm, rmap_head, NULL, gfn, sp->role.level, __pte(0)); |
Lan Tianyu | c3134ce | 2018-12-06 21:21:09 +0800 | [diff] [blame] | 1515 | kvm_flush_remote_tlbs_with_address(vcpu->kvm, sp->gfn, |
| 1516 | KVM_PAGES_PER_HPAGE(sp->role.level)); |
Marcelo Tosatti | 53a27b3 | 2009-08-05 15:43:58 -0300 | [diff] [blame] | 1517 | } |
| 1518 | |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1519 | bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1520 | { |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1521 | bool young; |
Ben Gardon | f8e1449 | 2020-10-14 11:26:53 -0700 | [diff] [blame] | 1522 | |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1523 | young = kvm_handle_gfn_range(kvm, range, kvm_age_rmapp); |
| 1524 | |
Paolo Bonzini | 897218f | 2021-02-06 09:53:33 -0500 | [diff] [blame] | 1525 | if (is_tdp_mmu_enabled(kvm)) |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1526 | young |= kvm_tdp_mmu_age_gfn_range(kvm, range); |
Ben Gardon | f8e1449 | 2020-10-14 11:26:53 -0700 | [diff] [blame] | 1527 | |
| 1528 | return young; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1529 | } |
| 1530 | |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1531 | bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 1532 | { |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1533 | bool young; |
Ben Gardon | f8e1449 | 2020-10-14 11:26:53 -0700 | [diff] [blame] | 1534 | |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1535 | young = kvm_handle_gfn_range(kvm, range, kvm_test_age_rmapp); |
| 1536 | |
Paolo Bonzini | 897218f | 2021-02-06 09:53:33 -0500 | [diff] [blame] | 1537 | if (is_tdp_mmu_enabled(kvm)) |
Sean Christopherson | 3039bcc | 2021-04-01 17:56:50 -0700 | [diff] [blame] | 1538 | young |= kvm_tdp_mmu_test_age_gfn(kvm, range); |
Ben Gardon | f8e1449 | 2020-10-14 11:26:53 -0700 | [diff] [blame] | 1539 | |
| 1540 | return young; |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 1541 | } |
| 1542 | |
Yaozu Dong | d6c69ee | 2007-04-25 14:17:25 +0800 | [diff] [blame] | 1543 | #ifdef MMU_DEBUG |
Avi Kivity | 47ad8e6 | 2007-05-06 15:50:58 +0300 | [diff] [blame] | 1544 | static int is_empty_shadow_page(u64 *spt) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1545 | { |
Avi Kivity | 139bdb2 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 1546 | u64 *pos; |
| 1547 | u64 *end; |
| 1548 | |
Avi Kivity | 47ad8e6 | 2007-05-06 15:50:58 +0300 | [diff] [blame] | 1549 | for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++) |
Avi Kivity | 3c91551 | 2008-05-20 16:21:13 +0300 | [diff] [blame] | 1550 | if (is_shadow_present_pte(*pos)) { |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1551 | printk(KERN_ERR "%s: %p %llx\n", __func__, |
Avi Kivity | 139bdb2 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 1552 | pos, *pos); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1553 | return 0; |
Avi Kivity | 139bdb2 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 1554 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1555 | return 1; |
| 1556 | } |
Yaozu Dong | d6c69ee | 2007-04-25 14:17:25 +0800 | [diff] [blame] | 1557 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1558 | |
Dave Hansen | 45221ab | 2010-08-19 18:11:37 -0700 | [diff] [blame] | 1559 | /* |
| 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 Gardon | bc8a3d8 | 2019-04-08 11:07:30 -0700 | [diff] [blame] | 1565 | static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, unsigned long nr) |
Dave Hansen | 45221ab | 2010-08-19 18:11:37 -0700 | [diff] [blame] | 1566 | { |
| 1567 | kvm->arch.n_used_mmu_pages += nr; |
| 1568 | percpu_counter_add(&kvm_total_used_mmu_pages, nr); |
| 1569 | } |
| 1570 | |
Gleb Natapov | 834be0d | 2013-01-30 16:45:05 +0200 | [diff] [blame] | 1571 | static void kvm_mmu_free_page(struct kvm_mmu_page *sp) |
Avi Kivity | 260746c | 2007-01-05 16:36:49 -0800 | [diff] [blame] | 1572 | { |
Paolo Bonzini | fa4a2c0 | 2013-10-02 16:56:16 +0200 | [diff] [blame] | 1573 | MMU_WARN_ON(!is_empty_shadow_page(sp->spt)); |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 1574 | hlist_del(&sp->hash_link); |
Xiao Guangrong | bd4c86e | 2011-07-12 03:27:14 +0800 | [diff] [blame] | 1575 | list_del(&sp->link); |
| 1576 | free_page((unsigned long)sp->spt); |
Gleb Natapov | 834be0d | 2013-01-30 16:45:05 +0200 | [diff] [blame] | 1577 | if (!sp->role.direct) |
| 1578 | free_page((unsigned long)sp->gfns); |
Xiao Guangrong | e8ad9a7 | 2010-05-13 10:06:02 +0800 | [diff] [blame] | 1579 | kmem_cache_free(mmu_page_header_cache, sp); |
Avi Kivity | 260746c | 2007-01-05 16:36:49 -0800 | [diff] [blame] | 1580 | } |
| 1581 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1582 | static unsigned kvm_page_table_hashfn(gfn_t gfn) |
| 1583 | { |
David Matlack | 114df30 | 2016-12-19 13:58:25 -0800 | [diff] [blame] | 1584 | return hash_64(gfn, KVM_MMU_HASH_SHIFT); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1585 | } |
| 1586 | |
Xiao Guangrong | 67052b3 | 2011-05-15 23:27:08 +0800 | [diff] [blame] | 1587 | static 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 | |
| 1596 | static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp, |
| 1597 | u64 *parent_pte) |
| 1598 | { |
Wei Yang | 8daf346 | 2018-10-04 10:04:22 +0800 | [diff] [blame] | 1599 | __pte_list_remove(parent_pte, &sp->parent_ptes); |
Xiao Guangrong | 67052b3 | 2011-05-15 23:27:08 +0800 | [diff] [blame] | 1600 | } |
| 1601 | |
Xiao Guangrong | bcdd9a9 | 2011-05-15 23:28:29 +0800 | [diff] [blame] | 1602 | static void drop_parent_pte(struct kvm_mmu_page *sp, |
| 1603 | u64 *parent_pte) |
| 1604 | { |
| 1605 | mmu_page_remove_parent_pte(sp, parent_pte); |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 1606 | mmu_spte_clear_no_track(parent_pte); |
Xiao Guangrong | bcdd9a9 | 2011-05-15 23:28:29 +0800 | [diff] [blame] | 1607 | } |
| 1608 | |
Takuya Yoshikawa | 4700579 | 2015-11-20 17:46:29 +0900 | [diff] [blame] | 1609 | static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, int direct) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1610 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1611 | struct kvm_mmu_page *sp; |
Takuya Yoshikawa | 7ddca7e | 2013-03-21 19:33:43 +0900 | [diff] [blame] | 1612 | |
Sean Christopherson | 94ce87e | 2020-07-02 19:35:37 -0700 | [diff] [blame] | 1613 | 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 Jiangshan | 2032a93 | 2010-05-26 16:49:59 +0800 | [diff] [blame] | 1615 | if (!direct) |
Sean Christopherson | 94ce87e | 2020-07-02 19:35:37 -0700 | [diff] [blame] | 1616 | sp->gfns = kvm_mmu_memory_cache_alloc(&vcpu->arch.mmu_gfn_array_cache); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1617 | set_page_private(virt_to_page(sp->spt), (unsigned long)sp); |
Sean Christopherson | 002c5f7 | 2019-09-12 19:46:02 -0700 | [diff] [blame] | 1618 | |
| 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 Christopherson | ca333ad | 2019-09-12 19:46:11 -0700 | [diff] [blame] | 1624 | sp->mmu_valid_gen = vcpu->kvm->arch.mmu_valid_gen; |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 1625 | list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages); |
Dave Hansen | 45221ab | 2010-08-19 18:11:37 -0700 | [diff] [blame] | 1626 | kvm_mod_used_mmu_pages(vcpu->kvm, +1); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1627 | return sp; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1628 | } |
| 1629 | |
Xiao Guangrong | 67052b3 | 2011-05-15 23:27:08 +0800 | [diff] [blame] | 1630 | static void mark_unsync(u64 *spte); |
Xiao Guangrong | 6b18493 | 2010-04-16 21:29:17 +0800 | [diff] [blame] | 1631 | static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp) |
Marcelo Tosatti | 0074ff6 | 2008-09-23 13:18:40 -0300 | [diff] [blame] | 1632 | { |
Takuya Yoshikawa | 74c4e63 | 2015-11-26 21:15:38 +0900 | [diff] [blame] | 1633 | u64 *sptep; |
| 1634 | struct rmap_iterator iter; |
| 1635 | |
| 1636 | for_each_rmap_spte(&sp->parent_ptes, &iter, sptep) { |
| 1637 | mark_unsync(sptep); |
| 1638 | } |
Xiao Guangrong | 1047df1 | 2010-06-11 21:35:15 +0800 | [diff] [blame] | 1639 | } |
| 1640 | |
Xiao Guangrong | 67052b3 | 2011-05-15 23:27:08 +0800 | [diff] [blame] | 1641 | static void mark_unsync(u64 *spte) |
Xiao Guangrong | 1047df1 | 2010-06-11 21:35:15 +0800 | [diff] [blame] | 1642 | { |
Xiao Guangrong | 67052b3 | 2011-05-15 23:27:08 +0800 | [diff] [blame] | 1643 | struct kvm_mmu_page *sp; |
Xiao Guangrong | 1047df1 | 2010-06-11 21:35:15 +0800 | [diff] [blame] | 1644 | unsigned int index; |
| 1645 | |
Sean Christopherson | 5735468 | 2020-06-22 13:20:33 -0700 | [diff] [blame] | 1646 | sp = sptep_to_sp(spte); |
Xiao Guangrong | 1047df1 | 2010-06-11 21:35:15 +0800 | [diff] [blame] | 1647 | 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 Tosatti | 0074ff6 | 2008-09-23 13:18:40 -0300 | [diff] [blame] | 1653 | } |
| 1654 | |
Marcelo Tosatti | e8bc217 | 2008-09-23 13:18:33 -0300 | [diff] [blame] | 1655 | static int nonpaging_sync_page(struct kvm_vcpu *vcpu, |
Xiao Guangrong | a4a8e6f | 2010-11-19 17:04:03 +0800 | [diff] [blame] | 1656 | struct kvm_mmu_page *sp) |
Marcelo Tosatti | e8bc217 | 2008-09-23 13:18:33 -0300 | [diff] [blame] | 1657 | { |
Paolo Bonzini | 1f50f1b | 2016-02-24 11:07:14 +0100 | [diff] [blame] | 1658 | return 0; |
Marcelo Tosatti | e8bc217 | 2008-09-23 13:18:33 -0300 | [diff] [blame] | 1659 | } |
| 1660 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1661 | #define KVM_PAGE_ARRAY_NR 16 |
| 1662 | |
| 1663 | struct 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 Eder | cded19f | 2009-02-21 02:19:13 +0100 | [diff] [blame] | 1671 | static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp, |
| 1672 | int idx) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1673 | { |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1674 | int i; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1675 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1676 | 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 Yoshikawa | fd95145 | 2015-11-20 17:43:13 +0900 | [diff] [blame] | 1687 | static 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 Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1694 | static 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 Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1698 | |
Takuya Yoshikawa | 37178b8 | 2011-11-29 14:02:45 +0900 | [diff] [blame] | 1699 | for_each_set_bit(i, sp->unsync_child_bitmap, 512) { |
Xiao Guangrong | 7a8f1a7 | 2010-06-11 21:34:04 +0800 | [diff] [blame] | 1700 | struct kvm_mmu_page *child; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1701 | u64 ent = sp->spt[i]; |
| 1702 | |
Takuya Yoshikawa | fd95145 | 2015-11-20 17:43:13 +0900 | [diff] [blame] | 1703 | if (!is_shadow_present_pte(ent) || is_large_pte(ent)) { |
| 1704 | clear_unsync_child_bit(sp, i); |
| 1705 | continue; |
| 1706 | } |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1707 | |
Sean Christopherson | e47c4ae | 2020-06-22 13:20:34 -0700 | [diff] [blame] | 1708 | child = to_shadow_page(ent & PT64_BASE_ADDR_MASK); |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1709 | |
Xiao Guangrong | 7a8f1a7 | 2010-06-11 21:34:04 +0800 | [diff] [blame] | 1710 | if (child->unsync_children) { |
| 1711 | if (mmu_pages_add(pvec, child, i)) |
| 1712 | return -ENOSPC; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1713 | |
Xiao Guangrong | 7a8f1a7 | 2010-06-11 21:34:04 +0800 | [diff] [blame] | 1714 | ret = __mmu_unsync_walk(child, pvec); |
Takuya Yoshikawa | fd95145 | 2015-11-20 17:43:13 +0900 | [diff] [blame] | 1715 | if (!ret) { |
| 1716 | clear_unsync_child_bit(sp, i); |
| 1717 | continue; |
| 1718 | } else if (ret > 0) { |
Xiao Guangrong | 7a8f1a7 | 2010-06-11 21:34:04 +0800 | [diff] [blame] | 1719 | nr_unsync_leaf += ret; |
Takuya Yoshikawa | fd95145 | 2015-11-20 17:43:13 +0900 | [diff] [blame] | 1720 | } else |
Xiao Guangrong | 7a8f1a7 | 2010-06-11 21:34:04 +0800 | [diff] [blame] | 1721 | 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 Yoshikawa | fd95145 | 2015-11-20 17:43:13 +0900 | [diff] [blame] | 1727 | clear_unsync_child_bit(sp, i); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1728 | } |
| 1729 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1730 | return nr_unsync_leaf; |
| 1731 | } |
| 1732 | |
Xiao Guangrong | e23d3fe | 2016-02-24 09:46:06 +0100 | [diff] [blame] | 1733 | #define INVALID_INDEX (-1) |
| 1734 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1735 | static int mmu_unsync_walk(struct kvm_mmu_page *sp, |
| 1736 | struct kvm_mmu_pages *pvec) |
| 1737 | { |
Paolo Bonzini | 0a47cd8 | 2016-02-23 13:54:25 +0100 | [diff] [blame] | 1738 | pvec->nr = 0; |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1739 | if (!sp->unsync_children) |
| 1740 | return 0; |
| 1741 | |
Xiao Guangrong | e23d3fe | 2016-02-24 09:46:06 +0100 | [diff] [blame] | 1742 | mmu_pages_add(pvec, sp, INVALID_INDEX); |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1743 | return __mmu_unsync_walk(sp, pvec); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1744 | } |
| 1745 | |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1746 | static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp) |
| 1747 | { |
| 1748 | WARN_ON(!sp->unsync); |
Xiao Guangrong | 5e1b3dd | 2010-04-28 11:55:06 +0800 | [diff] [blame] | 1749 | trace_kvm_mmu_sync_page(sp); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1750 | sp->unsync = 0; |
| 1751 | --kvm->stat.mmu_unsync; |
| 1752 | } |
| 1753 | |
Sean Christopherson | 83cdb56 | 2019-02-05 13:01:35 -0800 | [diff] [blame] | 1754 | static bool kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp, |
| 1755 | struct list_head *invalid_list); |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 1756 | static void kvm_mmu_commit_zap_page(struct kvm *kvm, |
| 1757 | struct list_head *invalid_list); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1758 | |
Sean Christopherson | ac101b7 | 2020-06-23 12:40:26 -0700 | [diff] [blame] | 1759 | #define for_each_valid_sp(_kvm, _sp, _list) \ |
| 1760 | hlist_for_each_entry(_sp, _list, hash_link) \ |
Sean Christopherson | fac026d | 2019-09-12 19:46:03 -0700 | [diff] [blame] | 1761 | if (is_obsolete_sp((_kvm), (_sp))) { \ |
David Matlack | f3414bc | 2016-12-20 15:25:57 -0800 | [diff] [blame] | 1762 | } else |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 1763 | |
Takuya Yoshikawa | 1044b03 | 2013-03-06 16:05:07 +0900 | [diff] [blame] | 1764 | #define for_each_gfn_indirect_valid_sp(_kvm, _sp, _gfn) \ |
Sean Christopherson | ac101b7 | 2020-06-23 12:40:26 -0700 | [diff] [blame] | 1765 | for_each_valid_sp(_kvm, _sp, \ |
| 1766 | &(_kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(_gfn)]) \ |
David Matlack | f3414bc | 2016-12-20 15:25:57 -0800 | [diff] [blame] | 1767 | if ((_sp)->gfn != (_gfn) || (_sp)->role.direct) {} else |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 1768 | |
Sean Christopherson | 47c42e6 | 2019-03-07 15:27:44 -0800 | [diff] [blame] | 1769 | static 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 Guangrong | f918b44 | 2010-06-11 21:30:36 +0800 | [diff] [blame] | 1774 | /* @sp->gfn should be write-protected at the call site */ |
Paolo Bonzini | 1f50f1b | 2016-02-24 11:07:14 +0100 | [diff] [blame] | 1775 | static bool __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, |
| 1776 | struct list_head *invalid_list) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1777 | { |
Sean Christopherson | 47c42e6 | 2019-03-07 15:27:44 -0800 | [diff] [blame] | 1778 | if ((!is_ept_sp(sp) && sp->role.gpte_is_8_bytes != !!is_pae(vcpu)) || |
| 1779 | vcpu->arch.mmu->sync_page(vcpu, sp) == 0) { |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 1780 | kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list); |
Paolo Bonzini | 1f50f1b | 2016-02-24 11:07:14 +0100 | [diff] [blame] | 1781 | return false; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1782 | } |
| 1783 | |
Paolo Bonzini | 1f50f1b | 2016-02-24 11:07:14 +0100 | [diff] [blame] | 1784 | return true; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1785 | } |
| 1786 | |
Sean Christopherson | a211363 | 2019-02-05 13:01:20 -0800 | [diff] [blame] | 1787 | static bool kvm_mmu_remote_flush_or_zap(struct kvm *kvm, |
| 1788 | struct list_head *invalid_list, |
| 1789 | bool remote_flush) |
| 1790 | { |
Sean Christopherson | cfd32ac | 2019-04-12 19:55:41 -0700 | [diff] [blame] | 1791 | if (!remote_flush && list_empty(invalid_list)) |
Sean Christopherson | a211363 | 2019-02-05 13:01:20 -0800 | [diff] [blame] | 1792 | 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 Bonzini | 35a7051 | 2016-02-24 10:03:27 +0100 | [diff] [blame] | 1801 | static void kvm_mmu_flush_or_zap(struct kvm_vcpu *vcpu, |
| 1802 | struct list_head *invalid_list, |
| 1803 | bool remote_flush, bool local_flush) |
Xiao Guangrong | 1d9dc7e | 2010-05-15 18:51:24 +0800 | [diff] [blame] | 1804 | { |
Sean Christopherson | a211363 | 2019-02-05 13:01:20 -0800 | [diff] [blame] | 1805 | if (kvm_mmu_remote_flush_or_zap(vcpu->kvm, invalid_list, remote_flush)) |
Paolo Bonzini | 35a7051 | 2016-02-24 10:03:27 +0100 | [diff] [blame] | 1806 | return; |
Xiao Guangrong | 1d9dc7e | 2010-05-15 18:51:24 +0800 | [diff] [blame] | 1807 | |
Sean Christopherson | a211363 | 2019-02-05 13:01:20 -0800 | [diff] [blame] | 1808 | if (local_flush) |
Sean Christopherson | 8c8560b | 2020-03-20 14:28:21 -0700 | [diff] [blame] | 1809 | kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); |
Xiao Guangrong | 1d9dc7e | 2010-05-15 18:51:24 +0800 | [diff] [blame] | 1810 | } |
| 1811 | |
Xiao Guangrong | e37fa78 | 2011-11-30 17:43:24 +0800 | [diff] [blame] | 1812 | #ifdef CONFIG_KVM_MMU_AUDIT |
| 1813 | #include "mmu_audit.c" |
| 1814 | #else |
| 1815 | static void kvm_mmu_audit(struct kvm_vcpu *vcpu, int point) { } |
| 1816 | static void mmu_audit_disable(void) { } |
| 1817 | #endif |
| 1818 | |
Sean Christopherson | 002c5f7 | 2019-09-12 19:46:02 -0700 | [diff] [blame] | 1819 | static bool is_obsolete_sp(struct kvm *kvm, struct kvm_mmu_page *sp) |
| 1820 | { |
Sean Christopherson | fac026d | 2019-09-12 19:46:03 -0700 | [diff] [blame] | 1821 | return sp->role.invalid || |
| 1822 | unlikely(sp->mmu_valid_gen != kvm->arch.mmu_valid_gen); |
Sean Christopherson | 002c5f7 | 2019-09-12 19:46:02 -0700 | [diff] [blame] | 1823 | } |
| 1824 | |
Paolo Bonzini | 1f50f1b | 2016-02-24 11:07:14 +0100 | [diff] [blame] | 1825 | static bool kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 1826 | struct list_head *invalid_list) |
Xiao Guangrong | 1d9dc7e | 2010-05-15 18:51:24 +0800 | [diff] [blame] | 1827 | { |
Paolo Bonzini | 9a43c5d | 2016-02-24 10:28:01 +0100 | [diff] [blame] | 1828 | kvm_unlink_unsync_page(vcpu->kvm, sp); |
| 1829 | return __kvm_sync_page(vcpu, sp, invalid_list); |
Xiao Guangrong | 1d9dc7e | 2010-05-15 18:51:24 +0800 | [diff] [blame] | 1830 | } |
| 1831 | |
Xiao Guangrong | 9f1a122 | 2010-05-24 15:41:33 +0800 | [diff] [blame] | 1832 | /* @gfn should be write-protected at the call site */ |
Paolo Bonzini | 2a74003a | 2016-02-24 11:26:10 +0100 | [diff] [blame] | 1833 | static bool kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn, |
| 1834 | struct list_head *invalid_list) |
Xiao Guangrong | 9f1a122 | 2010-05-24 15:41:33 +0800 | [diff] [blame] | 1835 | { |
Xiao Guangrong | 9f1a122 | 2010-05-24 15:41:33 +0800 | [diff] [blame] | 1836 | struct kvm_mmu_page *s; |
Paolo Bonzini | 2a74003a | 2016-02-24 11:26:10 +0100 | [diff] [blame] | 1837 | bool ret = false; |
Xiao Guangrong | 9f1a122 | 2010-05-24 15:41:33 +0800 | [diff] [blame] | 1838 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1839 | for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn) { |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 1840 | if (!s->unsync) |
Xiao Guangrong | 9f1a122 | 2010-05-24 15:41:33 +0800 | [diff] [blame] | 1841 | continue; |
| 1842 | |
Sean Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 1843 | WARN_ON(s->role.level != PG_LEVEL_4K); |
Paolo Bonzini | 2a74003a | 2016-02-24 11:26:10 +0100 | [diff] [blame] | 1844 | ret |= kvm_sync_page(vcpu, s, invalid_list); |
Xiao Guangrong | 9f1a122 | 2010-05-24 15:41:33 +0800 | [diff] [blame] | 1845 | } |
| 1846 | |
Paolo Bonzini | 2a74003a | 2016-02-24 11:26:10 +0100 | [diff] [blame] | 1847 | return ret; |
Xiao Guangrong | 9f1a122 | 2010-05-24 15:41:33 +0800 | [diff] [blame] | 1848 | } |
| 1849 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1850 | struct mmu_page_path { |
Yu Zhang | 2a7266a | 2017-08-24 20:27:54 +0800 | [diff] [blame] | 1851 | struct kvm_mmu_page *parent[PT64_ROOT_MAX_LEVEL]; |
| 1852 | unsigned int idx[PT64_ROOT_MAX_LEVEL]; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1853 | }; |
| 1854 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1855 | #define for_each_sp(pvec, sp, parents, i) \ |
Paolo Bonzini | 0a47cd8 | 2016-02-23 13:54:25 +0100 | [diff] [blame] | 1856 | for (i = mmu_pages_first(&pvec, &parents); \ |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1857 | i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \ |
| 1858 | i = mmu_pages_next(&pvec, &parents, i)) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1859 | |
Hannes Eder | cded19f | 2009-02-21 02:19:13 +0100 | [diff] [blame] | 1860 | static int mmu_pages_next(struct kvm_mmu_pages *pvec, |
| 1861 | struct mmu_page_path *parents, |
| 1862 | int i) |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1863 | { |
| 1864 | int n; |
| 1865 | |
| 1866 | for (n = i+1; n < pvec->nr; n++) { |
| 1867 | struct kvm_mmu_page *sp = pvec->page[n].sp; |
Paolo Bonzini | 0a47cd8 | 2016-02-23 13:54:25 +0100 | [diff] [blame] | 1868 | unsigned idx = pvec->page[n].idx; |
| 1869 | int level = sp->role.level; |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1870 | |
Paolo Bonzini | 0a47cd8 | 2016-02-23 13:54:25 +0100 | [diff] [blame] | 1871 | parents->idx[level-1] = idx; |
Sean Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 1872 | if (level == PG_LEVEL_4K) |
Paolo Bonzini | 0a47cd8 | 2016-02-23 13:54:25 +0100 | [diff] [blame] | 1873 | break; |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1874 | |
Paolo Bonzini | 0a47cd8 | 2016-02-23 13:54:25 +0100 | [diff] [blame] | 1875 | parents->parent[level-2] = sp; |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1876 | } |
| 1877 | |
| 1878 | return n; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1879 | } |
| 1880 | |
Paolo Bonzini | 0a47cd8 | 2016-02-23 13:54:25 +0100 | [diff] [blame] | 1881 | static 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 Guangrong | e23d3fe | 2016-02-24 09:46:06 +0100 | [diff] [blame] | 1890 | WARN_ON(pvec->page[0].idx != INVALID_INDEX); |
| 1891 | |
Paolo Bonzini | 0a47cd8 | 2016-02-23 13:54:25 +0100 | [diff] [blame] | 1892 | sp = pvec->page[0].sp; |
| 1893 | level = sp->role.level; |
Sean Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 1894 | WARN_ON(level == PG_LEVEL_4K); |
Paolo Bonzini | 0a47cd8 | 2016-02-23 13:54:25 +0100 | [diff] [blame] | 1895 | |
| 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 Eder | cded19f | 2009-02-21 02:19:13 +0100 | [diff] [blame] | 1905 | static void mmu_pages_clear_parents(struct mmu_page_path *parents) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1906 | { |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1907 | struct kvm_mmu_page *sp; |
| 1908 | unsigned int level = 0; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1909 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1910 | do { |
| 1911 | unsigned int idx = parents->idx[level]; |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1912 | sp = parents->parent[level]; |
| 1913 | if (!sp) |
| 1914 | return; |
| 1915 | |
Xiao Guangrong | e23d3fe | 2016-02-24 09:46:06 +0100 | [diff] [blame] | 1916 | WARN_ON(idx == INVALID_INDEX); |
Takuya Yoshikawa | fd95145 | 2015-11-20 17:43:13 +0900 | [diff] [blame] | 1917 | clear_unsync_child_bit(sp, idx); |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1918 | level++; |
Paolo Bonzini | 0a47cd8 | 2016-02-23 13:54:25 +0100 | [diff] [blame] | 1919 | } while (!sp->unsync_children); |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | static 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 Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 1929 | LIST_HEAD(invalid_list); |
Paolo Bonzini | 50c9e6f | 2016-02-25 10:47:38 +0100 | [diff] [blame] | 1930 | bool flush = false; |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1931 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1932 | while (mmu_unsync_walk(parent, &pages)) { |
Xiao Guangrong | 2f84569 | 2012-06-20 15:56:53 +0800 | [diff] [blame] | 1933 | bool protected = false; |
Marcelo Tosatti | b1a3682 | 2008-12-01 22:32:03 -0200 | [diff] [blame] | 1934 | |
| 1935 | for_each_sp(pages, sp, parents, i) |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 1936 | protected |= rmap_write_protect(vcpu, sp->gfn); |
Marcelo Tosatti | b1a3682 | 2008-12-01 22:32:03 -0200 | [diff] [blame] | 1937 | |
Paolo Bonzini | 50c9e6f | 2016-02-25 10:47:38 +0100 | [diff] [blame] | 1938 | if (protected) { |
Marcelo Tosatti | b1a3682 | 2008-12-01 22:32:03 -0200 | [diff] [blame] | 1939 | kvm_flush_remote_tlbs(vcpu->kvm); |
Paolo Bonzini | 50c9e6f | 2016-02-25 10:47:38 +0100 | [diff] [blame] | 1940 | flush = false; |
| 1941 | } |
Marcelo Tosatti | b1a3682 | 2008-12-01 22:32:03 -0200 | [diff] [blame] | 1942 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1943 | for_each_sp(pages, sp, parents, i) { |
Paolo Bonzini | 1f50f1b | 2016-02-24 11:07:14 +0100 | [diff] [blame] | 1944 | flush |= kvm_sync_page(vcpu, sp, &invalid_list); |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1945 | mmu_pages_clear_parents(&parents); |
| 1946 | } |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 1947 | if (need_resched() || rwlock_needbreak(&vcpu->kvm->mmu_lock)) { |
Paolo Bonzini | 50c9e6f | 2016-02-25 10:47:38 +0100 | [diff] [blame] | 1948 | kvm_mmu_flush_or_zap(vcpu, &invalid_list, false, flush); |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 1949 | cond_resched_rwlock_write(&vcpu->kvm->mmu_lock); |
Paolo Bonzini | 50c9e6f | 2016-02-25 10:47:38 +0100 | [diff] [blame] | 1950 | flush = false; |
| 1951 | } |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1952 | } |
Paolo Bonzini | 50c9e6f | 2016-02-25 10:47:38 +0100 | [diff] [blame] | 1953 | |
| 1954 | kvm_mmu_flush_or_zap(vcpu, &invalid_list, false, flush); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1955 | } |
| 1956 | |
Xiao Guangrong | a30f47c | 2011-09-22 16:58:36 +0800 | [diff] [blame] | 1957 | static void __clear_sp_write_flooding_count(struct kvm_mmu_page *sp) |
| 1958 | { |
Xiao Guangrong | e5691a8 | 2016-02-24 17:51:12 +0800 | [diff] [blame] | 1959 | atomic_set(&sp->write_flooding_count, 0); |
Xiao Guangrong | a30f47c | 2011-09-22 16:58:36 +0800 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | static void clear_sp_write_flooding_count(u64 *spte) |
| 1963 | { |
Sean Christopherson | 5735468 | 2020-06-22 13:20:33 -0700 | [diff] [blame] | 1964 | __clear_sp_write_flooding_count(sptep_to_sp(spte)); |
Xiao Guangrong | a30f47c | 2011-09-22 16:58:36 +0800 | [diff] [blame] | 1965 | } |
| 1966 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1967 | static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, |
| 1968 | gfn_t gfn, |
| 1969 | gva_t gaddr, |
| 1970 | unsigned level, |
Avi Kivity | f6e2c02b | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 1971 | int direct, |
Ben Gardon | 0a2b64c | 2020-02-03 15:09:09 -0800 | [diff] [blame] | 1972 | unsigned int access) |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1973 | { |
Sean Christopherson | fb58a9c | 2020-06-23 12:40:27 -0700 | [diff] [blame] | 1974 | bool direct_mmu = vcpu->arch.mmu->direct_map; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1975 | union kvm_mmu_page_role role; |
Sean Christopherson | ac101b7 | 2020-06-23 12:40:26 -0700 | [diff] [blame] | 1976 | struct hlist_head *sp_list; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1977 | unsigned quadrant; |
Xiao Guangrong | 9f1a122 | 2010-05-24 15:41:33 +0800 | [diff] [blame] | 1978 | struct kvm_mmu_page *sp; |
Xiao Guangrong | 9f1a122 | 2010-05-24 15:41:33 +0800 | [diff] [blame] | 1979 | bool need_sync = false; |
Paolo Bonzini | 2a74003a | 2016-02-24 11:26:10 +0100 | [diff] [blame] | 1980 | bool flush = false; |
David Matlack | f3414bc | 2016-12-20 15:25:57 -0800 | [diff] [blame] | 1981 | int collisions = 0; |
Paolo Bonzini | 2a74003a | 2016-02-24 11:26:10 +0100 | [diff] [blame] | 1982 | LIST_HEAD(invalid_list); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1983 | |
Vitaly Kuznetsov | 36d9594d | 2018-10-08 21:28:10 +0200 | [diff] [blame] | 1984 | role = vcpu->arch.mmu->mmu_role.base; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1985 | role.level = level; |
Avi Kivity | f6e2c02b | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 1986 | role.direct = direct; |
Avi Kivity | 84b0c8c | 2010-03-14 10:16:40 +0200 | [diff] [blame] | 1987 | if (role.direct) |
Sean Christopherson | 47c42e6 | 2019-03-07 15:27:44 -0800 | [diff] [blame] | 1988 | role.gpte_is_8_bytes = true; |
Avi Kivity | 41074d0 | 2007-12-09 17:00:02 +0200 | [diff] [blame] | 1989 | role.access = access; |
Sean Christopherson | fb58a9c | 2020-06-23 12:40:27 -0700 | [diff] [blame] | 1990 | if (!direct_mmu && vcpu->arch.mmu->root_level <= PT32_ROOT_LEVEL) { |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1991 | 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 Christopherson | ac101b7 | 2020-06-23 12:40:26 -0700 | [diff] [blame] | 1995 | |
| 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 Matlack | f3414bc | 2016-12-20 15:25:57 -0800 | [diff] [blame] | 1998 | if (sp->gfn != gfn) { |
| 1999 | collisions++; |
| 2000 | continue; |
| 2001 | } |
| 2002 | |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 2003 | if (!need_sync && sp->unsync) |
| 2004 | need_sync = true; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 2005 | |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 2006 | if (sp->role.word != role.word) |
| 2007 | continue; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 2008 | |
Sean Christopherson | fb58a9c | 2020-06-23 12:40:27 -0700 | [diff] [blame] | 2009 | if (direct_mmu) |
| 2010 | goto trace_get_page; |
| 2011 | |
Paolo Bonzini | 2a74003a | 2016-02-24 11:26:10 +0100 | [diff] [blame] | 2012 | 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 Christopherson | 8c8560b | 2020-03-20 14:28:21 -0700 | [diff] [blame] | 2020 | kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); |
Paolo Bonzini | 2a74003a | 2016-02-24 11:26:10 +0100 | [diff] [blame] | 2021 | } |
Xiao Guangrong | e02aa90 | 2010-05-15 18:52:34 +0800 | [diff] [blame] | 2022 | |
Takuya Yoshikawa | 98bba23 | 2015-11-26 21:14:34 +0900 | [diff] [blame] | 2023 | if (sp->unsync_children) |
Lai Jiangshan | f6f6195 | 2020-09-02 21:54:21 +0800 | [diff] [blame] | 2024 | kvm_make_request(KVM_REQ_MMU_SYNC, vcpu); |
Xiao Guangrong | e02aa90 | 2010-05-15 18:52:34 +0800 | [diff] [blame] | 2025 | |
Xiao Guangrong | a30f47c | 2011-09-22 16:58:36 +0800 | [diff] [blame] | 2026 | __clear_sp_write_flooding_count(sp); |
Sean Christopherson | fb58a9c | 2020-06-23 12:40:27 -0700 | [diff] [blame] | 2027 | |
| 2028 | trace_get_page: |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 2029 | trace_kvm_mmu_get_page(sp, false); |
David Matlack | f3414bc | 2016-12-20 15:25:57 -0800 | [diff] [blame] | 2030 | goto out; |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 2031 | } |
Takuya Yoshikawa | 4700579 | 2015-11-20 17:46:29 +0900 | [diff] [blame] | 2032 | |
Avi Kivity | dfc5aa0 | 2007-12-18 19:47:18 +0200 | [diff] [blame] | 2033 | ++vcpu->kvm->stat.mmu_cache_miss; |
Takuya Yoshikawa | 4700579 | 2015-11-20 17:46:29 +0900 | [diff] [blame] | 2034 | |
| 2035 | sp = kvm_mmu_alloc_page(vcpu, direct); |
| 2036 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2037 | sp->gfn = gfn; |
| 2038 | sp->role = role; |
Sean Christopherson | ac101b7 | 2020-06-23 12:40:26 -0700 | [diff] [blame] | 2039 | hlist_add_head(&sp->hash_link, sp_list); |
Avi Kivity | f6e2c02b | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 2040 | if (!direct) { |
Xiao Guangrong | 56ca57f | 2016-02-24 17:51:14 +0800 | [diff] [blame] | 2041 | /* |
| 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 Bonzini | 3ed1a47 | 2015-05-19 16:29:22 +0200 | [diff] [blame] | 2046 | account_shadowed(vcpu->kvm, sp); |
Sean Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 2047 | if (level == PG_LEVEL_4K && rmap_write_protect(vcpu, gfn)) |
Lan Tianyu | c3134ce | 2018-12-06 21:21:09 +0800 | [diff] [blame] | 2048 | kvm_flush_remote_tlbs_with_address(vcpu->kvm, gfn, 1); |
Xiao Guangrong | 56ca57f | 2016-02-24 17:51:14 +0800 | [diff] [blame] | 2049 | |
Sean Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 2050 | if (level > PG_LEVEL_4K && need_sync) |
Paolo Bonzini | 2a74003a | 2016-02-24 11:26:10 +0100 | [diff] [blame] | 2051 | flush |= kvm_sync_pages(vcpu, gfn, &invalid_list); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 2052 | } |
Avi Kivity | f691fe1 | 2009-07-06 15:58:14 +0300 | [diff] [blame] | 2053 | trace_kvm_mmu_get_page(sp, true); |
Paolo Bonzini | 2a74003a | 2016-02-24 11:26:10 +0100 | [diff] [blame] | 2054 | |
| 2055 | kvm_mmu_flush_or_zap(vcpu, &invalid_list, false, flush); |
David Matlack | f3414bc | 2016-12-20 15:25:57 -0800 | [diff] [blame] | 2056 | out: |
| 2057 | if (collisions > vcpu->kvm->stat.max_mmu_page_hash_collisions) |
| 2058 | vcpu->kvm->stat.max_mmu_page_hash_collisions = collisions; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2059 | return sp; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 2060 | } |
| 2061 | |
Junaid Shahid | 7eb77e9 | 2018-06-27 14:59:16 -0700 | [diff] [blame] | 2062 | static void shadow_walk_init_using_root(struct kvm_shadow_walk_iterator *iterator, |
| 2063 | struct kvm_vcpu *vcpu, hpa_t root, |
| 2064 | u64 addr) |
Avi Kivity | 2d11123 | 2008-12-25 14:39:47 +0200 | [diff] [blame] | 2065 | { |
| 2066 | iterator->addr = addr; |
Junaid Shahid | 7eb77e9 | 2018-06-27 14:59:16 -0700 | [diff] [blame] | 2067 | iterator->shadow_addr = root; |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 2068 | iterator->level = vcpu->arch.mmu->shadow_root_level; |
Joerg Roedel | 81407ca | 2010-09-10 17:31:00 +0200 | [diff] [blame] | 2069 | |
Yu Zhang | 2a7266a | 2017-08-24 20:27:54 +0800 | [diff] [blame] | 2070 | if (iterator->level == PT64_ROOT_4LEVEL && |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 2071 | vcpu->arch.mmu->root_level < PT64_ROOT_4LEVEL && |
| 2072 | !vcpu->arch.mmu->direct_map) |
Joerg Roedel | 81407ca | 2010-09-10 17:31:00 +0200 | [diff] [blame] | 2073 | --iterator->level; |
| 2074 | |
Avi Kivity | 2d11123 | 2008-12-25 14:39:47 +0200 | [diff] [blame] | 2075 | if (iterator->level == PT32E_ROOT_LEVEL) { |
Junaid Shahid | 7eb77e9 | 2018-06-27 14:59:16 -0700 | [diff] [blame] | 2076 | /* |
| 2077 | * prev_root is currently only used for 64-bit hosts. So only |
| 2078 | * the active root_hpa is valid here. |
| 2079 | */ |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 2080 | BUG_ON(root != vcpu->arch.mmu->root_hpa); |
Junaid Shahid | 7eb77e9 | 2018-06-27 14:59:16 -0700 | [diff] [blame] | 2081 | |
Avi Kivity | 2d11123 | 2008-12-25 14:39:47 +0200 | [diff] [blame] | 2082 | iterator->shadow_addr |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 2083 | = vcpu->arch.mmu->pae_root[(addr >> 30) & 3]; |
Avi Kivity | 2d11123 | 2008-12-25 14:39:47 +0200 | [diff] [blame] | 2084 | iterator->shadow_addr &= PT64_BASE_ADDR_MASK; |
| 2085 | --iterator->level; |
| 2086 | if (!iterator->shadow_addr) |
| 2087 | iterator->level = 0; |
| 2088 | } |
| 2089 | } |
| 2090 | |
Junaid Shahid | 7eb77e9 | 2018-06-27 14:59:16 -0700 | [diff] [blame] | 2091 | static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator, |
| 2092 | struct kvm_vcpu *vcpu, u64 addr) |
| 2093 | { |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 2094 | shadow_walk_init_using_root(iterator, vcpu, vcpu->arch.mmu->root_hpa, |
Junaid Shahid | 7eb77e9 | 2018-06-27 14:59:16 -0700 | [diff] [blame] | 2095 | addr); |
| 2096 | } |
| 2097 | |
Avi Kivity | 2d11123 | 2008-12-25 14:39:47 +0200 | [diff] [blame] | 2098 | static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator) |
| 2099 | { |
Sean Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 2100 | if (iterator->level < PG_LEVEL_4K) |
Avi Kivity | 2d11123 | 2008-12-25 14:39:47 +0200 | [diff] [blame] | 2101 | return false; |
Marcelo Tosatti | 4d88954 | 2009-06-11 12:07:41 -0300 | [diff] [blame] | 2102 | |
Avi Kivity | 2d11123 | 2008-12-25 14:39:47 +0200 | [diff] [blame] | 2103 | 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 Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 2108 | static void __shadow_walk_next(struct kvm_shadow_walk_iterator *iterator, |
| 2109 | u64 spte) |
Avi Kivity | 2d11123 | 2008-12-25 14:39:47 +0200 | [diff] [blame] | 2110 | { |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 2111 | if (is_last_spte(spte, iterator->level)) { |
Xiao Guangrong | 052331b | 2011-07-12 03:21:17 +0800 | [diff] [blame] | 2112 | iterator->level = 0; |
| 2113 | return; |
| 2114 | } |
| 2115 | |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 2116 | iterator->shadow_addr = spte & PT64_BASE_ADDR_MASK; |
Avi Kivity | 2d11123 | 2008-12-25 14:39:47 +0200 | [diff] [blame] | 2117 | --iterator->level; |
| 2118 | } |
| 2119 | |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 2120 | static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator) |
| 2121 | { |
David Hildenbrand | bb606a9 | 2017-08-24 20:51:23 +0200 | [diff] [blame] | 2122 | __shadow_walk_next(iterator, *iterator->sptep); |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 2123 | } |
| 2124 | |
Takuya Yoshikawa | 98bba23 | 2015-11-26 21:14:34 +0900 | [diff] [blame] | 2125 | static void link_shadow_page(struct kvm_vcpu *vcpu, u64 *sptep, |
| 2126 | struct kvm_mmu_page *sp) |
Avi Kivity | 32ef26a | 2010-07-13 14:27:04 +0300 | [diff] [blame] | 2127 | { |
| 2128 | u64 spte; |
| 2129 | |
Bandan Das | ffb128c | 2016-07-12 18:18:49 -0400 | [diff] [blame] | 2130 | BUILD_BUG_ON(VMX_EPT_WRITABLE_MASK != PT_WRITABLE_MASK); |
Yang Zhang | 7a1638c | 2013-08-05 11:07:13 +0300 | [diff] [blame] | 2131 | |
Ben Gardon | cc4674d | 2020-09-25 14:22:48 -0700 | [diff] [blame] | 2132 | spte = make_nonleaf_spte(sp->spt, sp_ad_disabled(sp)); |
Xiao Guangrong | 24db273 | 2013-02-05 15:28:02 +0800 | [diff] [blame] | 2133 | |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 2134 | mmu_spte_set(sptep, spte); |
Takuya Yoshikawa | 98bba23 | 2015-11-26 21:14:34 +0900 | [diff] [blame] | 2135 | |
| 2136 | mmu_page_add_parent_pte(vcpu, sp, sptep); |
| 2137 | |
| 2138 | if (sp->unsync_children || sp->unsync) |
| 2139 | mark_unsync(sptep); |
Avi Kivity | 32ef26a | 2010-07-13 14:27:04 +0300 | [diff] [blame] | 2140 | } |
| 2141 | |
Avi Kivity | a357bd2 | 2010-07-13 14:27:07 +0300 | [diff] [blame] | 2142 | static 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 Christopherson | e47c4ae | 2020-06-22 13:20:34 -0700 | [diff] [blame] | 2155 | child = to_shadow_page(*sptep & PT64_BASE_ADDR_MASK); |
Avi Kivity | a357bd2 | 2010-07-13 14:27:07 +0300 | [diff] [blame] | 2156 | if (child->role.access == direct_access) |
| 2157 | return; |
| 2158 | |
Xiao Guangrong | bcdd9a9 | 2011-05-15 23:28:29 +0800 | [diff] [blame] | 2159 | drop_parent_pte(child, sptep); |
Lan Tianyu | c3134ce | 2018-12-06 21:21:09 +0800 | [diff] [blame] | 2160 | kvm_flush_remote_tlbs_with_address(vcpu->kvm, child->gfn, 1); |
Avi Kivity | a357bd2 | 2010-07-13 14:27:07 +0300 | [diff] [blame] | 2161 | } |
| 2162 | } |
| 2163 | |
Ben Gardon | 2de4085 | 2020-09-23 15:14:06 -0700 | [diff] [blame] | 2164 | /* Returns the number of zapped non-leaf child shadow pages. */ |
| 2165 | static int mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp, |
| 2166 | u64 *spte, struct list_head *invalid_list) |
Xiao Guangrong | 38e3b2b | 2011-05-15 23:27:52 +0800 | [diff] [blame] | 2167 | { |
| 2168 | u64 pte; |
| 2169 | struct kvm_mmu_page *child; |
| 2170 | |
| 2171 | pte = *spte; |
| 2172 | if (is_shadow_present_pte(pte)) { |
Xiao Guangrong | 505aef8 | 2011-09-22 16:56:06 +0800 | [diff] [blame] | 2173 | if (is_last_spte(pte, sp->role.level)) { |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 2174 | drop_spte(kvm, spte); |
Xiao Guangrong | 505aef8 | 2011-09-22 16:56:06 +0800 | [diff] [blame] | 2175 | if (is_large_pte(pte)) |
| 2176 | --kvm->stat.lpages; |
| 2177 | } else { |
Sean Christopherson | e47c4ae | 2020-06-22 13:20:34 -0700 | [diff] [blame] | 2178 | child = to_shadow_page(pte & PT64_BASE_ADDR_MASK); |
Xiao Guangrong | bcdd9a9 | 2011-05-15 23:28:29 +0800 | [diff] [blame] | 2179 | drop_parent_pte(child, spte); |
Ben Gardon | 2de4085 | 2020-09-23 15:14:06 -0700 | [diff] [blame] | 2180 | |
| 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 Guangrong | 38e3b2b | 2011-05-15 23:27:52 +0800 | [diff] [blame] | 2190 | } |
Sean Christopherson | ace569e | 2020-09-23 15:14:05 -0700 | [diff] [blame] | 2191 | } else if (is_mmio_spte(pte)) { |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 2192 | mmu_spte_clear_no_track(spte); |
Sean Christopherson | ace569e | 2020-09-23 15:14:05 -0700 | [diff] [blame] | 2193 | } |
Ben Gardon | 2de4085 | 2020-09-23 15:14:06 -0700 | [diff] [blame] | 2194 | return 0; |
Xiao Guangrong | 38e3b2b | 2011-05-15 23:27:52 +0800 | [diff] [blame] | 2195 | } |
| 2196 | |
Ben Gardon | 2de4085 | 2020-09-23 15:14:06 -0700 | [diff] [blame] | 2197 | static int kvm_mmu_page_unlink_children(struct kvm *kvm, |
| 2198 | struct kvm_mmu_page *sp, |
| 2199 | struct list_head *invalid_list) |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2200 | { |
Ben Gardon | 2de4085 | 2020-09-23 15:14:06 -0700 | [diff] [blame] | 2201 | int zapped = 0; |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 2202 | unsigned i; |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 2203 | |
Xiao Guangrong | 38e3b2b | 2011-05-15 23:27:52 +0800 | [diff] [blame] | 2204 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) |
Ben Gardon | 2de4085 | 2020-09-23 15:14:06 -0700 | [diff] [blame] | 2205 | zapped += mmu_page_zap_pte(kvm, sp, sp->spt + i, invalid_list); |
| 2206 | |
| 2207 | return zapped; |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2208 | } |
| 2209 | |
Avi Kivity | 31aa2b4 | 2008-07-11 17:59:46 +0300 | [diff] [blame] | 2210 | static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp) |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2211 | { |
Takuya Yoshikawa | 1e3f42f | 2012-03-21 23:50:34 +0900 | [diff] [blame] | 2212 | u64 *sptep; |
| 2213 | struct rmap_iterator iter; |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2214 | |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 2215 | while ((sptep = rmap_get_first(&sp->parent_ptes, &iter))) |
Takuya Yoshikawa | 1e3f42f | 2012-03-21 23:50:34 +0900 | [diff] [blame] | 2216 | drop_parent_pte(sp, sptep); |
Avi Kivity | 31aa2b4 | 2008-07-11 17:59:46 +0300 | [diff] [blame] | 2217 | } |
| 2218 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 2219 | static int mmu_zap_unsync_children(struct kvm *kvm, |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 2220 | struct kvm_mmu_page *parent, |
| 2221 | struct list_head *invalid_list) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 2222 | { |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 2223 | int i, zapped = 0; |
| 2224 | struct mmu_page_path parents; |
| 2225 | struct kvm_mmu_pages pages; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 2226 | |
Sean Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 2227 | if (parent->role.level == PG_LEVEL_4K) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 2228 | return 0; |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 2229 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 2230 | while (mmu_unsync_walk(parent, &pages)) { |
| 2231 | struct kvm_mmu_page *sp; |
| 2232 | |
| 2233 | for_each_sp(pages, sp, parents, i) { |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 2234 | kvm_mmu_prepare_zap_page(kvm, sp, invalid_list); |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 2235 | mmu_pages_clear_parents(&parents); |
Xiao Guangrong | 77662e0 | 2010-04-16 16:34:42 +0800 | [diff] [blame] | 2236 | zapped++; |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 2237 | } |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 2238 | } |
| 2239 | |
| 2240 | return zapped; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 2241 | } |
| 2242 | |
Sean Christopherson | 83cdb56 | 2019-02-05 13:01:35 -0800 | [diff] [blame] | 2243 | static 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 Kivity | 31aa2b4 | 2008-07-11 17:59:46 +0300 | [diff] [blame] | 2247 | { |
Sean Christopherson | 83cdb56 | 2019-02-05 13:01:35 -0800 | [diff] [blame] | 2248 | bool list_unstable; |
Avi Kivity | f691fe1 | 2009-07-06 15:58:14 +0300 | [diff] [blame] | 2249 | |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 2250 | trace_kvm_mmu_prepare_zap_page(sp); |
Avi Kivity | 31aa2b4 | 2008-07-11 17:59:46 +0300 | [diff] [blame] | 2251 | ++kvm->stat.mmu_shadow_zapped; |
Sean Christopherson | 83cdb56 | 2019-02-05 13:01:35 -0800 | [diff] [blame] | 2252 | *nr_zapped = mmu_zap_unsync_children(kvm, sp, invalid_list); |
Ben Gardon | 2de4085 | 2020-09-23 15:14:06 -0700 | [diff] [blame] | 2253 | *nr_zapped += kvm_mmu_page_unlink_children(kvm, sp, invalid_list); |
Avi Kivity | 31aa2b4 | 2008-07-11 17:59:46 +0300 | [diff] [blame] | 2254 | kvm_mmu_unlink_parents(kvm, sp); |
Xiao Guangrong | 5304b8d | 2013-05-31 08:36:22 +0800 | [diff] [blame] | 2255 | |
Sean Christopherson | 83cdb56 | 2019-02-05 13:01:35 -0800 | [diff] [blame] | 2256 | /* Zapping children means active_mmu_pages has become unstable. */ |
| 2257 | list_unstable = *nr_zapped; |
| 2258 | |
Avi Kivity | f6e2c02b | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 2259 | if (!sp->role.invalid && !sp->role.direct) |
Paolo Bonzini | 3ed1a47 | 2015-05-19 16:29:22 +0200 | [diff] [blame] | 2260 | unaccount_shadowed(kvm, sp); |
Xiao Guangrong | 5304b8d | 2013-05-31 08:36:22 +0800 | [diff] [blame] | 2261 | |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 2262 | if (sp->unsync) |
| 2263 | kvm_unlink_unsync_page(kvm, sp); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2264 | if (!sp->root_count) { |
Gui Jianfeng | 54a4f02 | 2010-05-05 09:03:49 +0800 | [diff] [blame] | 2265 | /* Count self */ |
Sean Christopherson | 83cdb56 | 2019-02-05 13:01:35 -0800 | [diff] [blame] | 2266 | (*nr_zapped)++; |
Sean Christopherson | f95eec9 | 2020-06-23 12:35:39 -0700 | [diff] [blame] | 2267 | |
| 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 Guangrong | aa6bd18 | 2011-07-12 03:26:40 +0800 | [diff] [blame] | 2277 | kvm_mod_used_mmu_pages(kvm, -1); |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 2278 | } else { |
Sean Christopherson | f95eec9 | 2020-06-23 12:35:39 -0700 | [diff] [blame] | 2279 | /* |
| 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 Natapov | 05988d7 | 2013-05-31 08:36:30 +0800 | [diff] [blame] | 2284 | |
Sean Christopherson | 1060520 | 2019-09-12 19:46:10 -0700 | [diff] [blame] | 2285 | /* |
| 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 Natapov | 05988d7 | 2013-05-31 08:36:30 +0800 | [diff] [blame] | 2291 | kvm_reload_remote_mmus(kvm); |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 2292 | } |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 2293 | |
Paolo Bonzini | b8e8c83 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 2294 | if (sp->lpage_disallowed) |
| 2295 | unaccount_huge_nx_page(kvm, sp); |
| 2296 | |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 2297 | sp->role.invalid = 1; |
Sean Christopherson | 83cdb56 | 2019-02-05 13:01:35 -0800 | [diff] [blame] | 2298 | return list_unstable; |
| 2299 | } |
| 2300 | |
| 2301 | static 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 Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2308 | } |
| 2309 | |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 2310 | static void kvm_mmu_commit_zap_page(struct kvm *kvm, |
| 2311 | struct list_head *invalid_list) |
| 2312 | { |
Takuya Yoshikawa | 945315b | 2013-03-06 16:05:52 +0900 | [diff] [blame] | 2313 | struct kvm_mmu_page *sp, *nsp; |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 2314 | |
| 2315 | if (list_empty(invalid_list)) |
| 2316 | return; |
| 2317 | |
Avi Kivity | c142786 | 2012-05-14 15:44:06 +0300 | [diff] [blame] | 2318 | /* |
Lan Tianyu | 9753f52 | 2016-03-13 11:10:24 +0800 | [diff] [blame] | 2319 | * 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 Kivity | c142786 | 2012-05-14 15:44:06 +0300 | [diff] [blame] | 2326 | */ |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 2327 | kvm_flush_remote_tlbs(kvm); |
| 2328 | |
Takuya Yoshikawa | 945315b | 2013-03-06 16:05:52 +0900 | [diff] [blame] | 2329 | list_for_each_entry_safe(sp, nsp, invalid_list, link) { |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 2330 | WARN_ON(!sp->role.invalid || sp->root_count); |
Xiao Guangrong | aa6bd18 | 2011-07-12 03:26:40 +0800 | [diff] [blame] | 2331 | kvm_mmu_free_page(sp); |
Takuya Yoshikawa | 945315b | 2013-03-06 16:05:52 +0900 | [diff] [blame] | 2332 | } |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 2333 | } |
| 2334 | |
Sean Christopherson | 6b82ef2 | 2020-06-23 12:35:40 -0700 | [diff] [blame] | 2335 | static unsigned long kvm_mmu_zap_oldest_mmu_pages(struct kvm *kvm, |
| 2336 | unsigned long nr_to_zap) |
Takuya Yoshikawa | 5da5960 | 2013-03-06 16:06:58 +0900 | [diff] [blame] | 2337 | { |
Sean Christopherson | 6b82ef2 | 2020-06-23 12:35:40 -0700 | [diff] [blame] | 2338 | unsigned long total_zapped = 0; |
| 2339 | struct kvm_mmu_page *sp, *tmp; |
Sean Christopherson | ba7888d | 2019-12-06 15:57:15 -0800 | [diff] [blame] | 2340 | LIST_HEAD(invalid_list); |
Sean Christopherson | 6b82ef2 | 2020-06-23 12:35:40 -0700 | [diff] [blame] | 2341 | bool unstable; |
| 2342 | int nr_zapped; |
Takuya Yoshikawa | 5da5960 | 2013-03-06 16:06:58 +0900 | [diff] [blame] | 2343 | |
| 2344 | if (list_empty(&kvm->arch.active_mmu_pages)) |
Sean Christopherson | ba7888d | 2019-12-06 15:57:15 -0800 | [diff] [blame] | 2345 | return 0; |
Takuya Yoshikawa | 5da5960 | 2013-03-06 16:06:58 +0900 | [diff] [blame] | 2346 | |
Sean Christopherson | 6b82ef2 | 2020-06-23 12:35:40 -0700 | [diff] [blame] | 2347 | restart: |
Sean Christopherson | 8fc5172 | 2021-01-13 12:50:30 -0800 | [diff] [blame] | 2348 | list_for_each_entry_safe_reverse(sp, tmp, &kvm->arch.active_mmu_pages, link) { |
Sean Christopherson | 6b82ef2 | 2020-06-23 12:35:40 -0700 | [diff] [blame] | 2349 | /* |
| 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 Christopherson | ba7888d | 2019-12-06 15:57:15 -0800 | [diff] [blame] | 2360 | break; |
| 2361 | |
Sean Christopherson | 6b82ef2 | 2020-06-23 12:35:40 -0700 | [diff] [blame] | 2362 | if (unstable) |
| 2363 | goto restart; |
Sean Christopherson | ba7888d | 2019-12-06 15:57:15 -0800 | [diff] [blame] | 2364 | } |
Sean Christopherson | 6b82ef2 | 2020-06-23 12:35:40 -0700 | [diff] [blame] | 2365 | |
| 2366 | kvm_mmu_commit_zap_page(kvm, &invalid_list); |
| 2367 | |
| 2368 | kvm->stat.mmu_recycled += total_zapped; |
| 2369 | return total_zapped; |
| 2370 | } |
| 2371 | |
Sean Christopherson | afe8d7e | 2020-06-22 13:20:30 -0700 | [diff] [blame] | 2372 | static 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 Yoshikawa | 5da5960 | 2013-03-06 16:06:58 +0900 | [diff] [blame] | 2379 | } |
| 2380 | |
Sean Christopherson | ba7888d | 2019-12-06 15:57:15 -0800 | [diff] [blame] | 2381 | static int make_mmu_pages_available(struct kvm_vcpu *vcpu) |
| 2382 | { |
Sean Christopherson | 6b82ef2 | 2020-06-23 12:35:40 -0700 | [diff] [blame] | 2383 | unsigned long avail = kvm_mmu_available_pages(vcpu->kvm); |
Sean Christopherson | ba7888d | 2019-12-06 15:57:15 -0800 | [diff] [blame] | 2384 | |
Sean Christopherson | 6b82ef2 | 2020-06-23 12:35:40 -0700 | [diff] [blame] | 2385 | if (likely(avail >= KVM_MIN_FREE_MMU_PAGES)) |
Sean Christopherson | ba7888d | 2019-12-06 15:57:15 -0800 | [diff] [blame] | 2386 | return 0; |
| 2387 | |
Sean Christopherson | 6b82ef2 | 2020-06-23 12:35:40 -0700 | [diff] [blame] | 2388 | kvm_mmu_zap_oldest_mmu_pages(vcpu->kvm, KVM_REFILL_PAGES - avail); |
Sean Christopherson | ba7888d | 2019-12-06 15:57:15 -0800 | [diff] [blame] | 2389 | |
Sean Christopherson | 6e6ec58 | 2021-03-04 17:10:50 -0800 | [diff] [blame] | 2390 | /* |
| 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 Christopherson | ba7888d | 2019-12-06 15:57:15 -0800 | [diff] [blame] | 2399 | if (!kvm_mmu_available_pages(vcpu->kvm)) |
| 2400 | return -ENOSPC; |
| 2401 | return 0; |
| 2402 | } |
| 2403 | |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 2404 | /* |
| 2405 | * Changing the number of mmu pages allocated to the vm |
Dave Hansen | 49d5ca2 | 2010-08-19 18:11:28 -0700 | [diff] [blame] | 2406 | * Note: if goal_nr_mmu_pages is too small, you will get dead lock |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 2407 | */ |
Ben Gardon | bc8a3d8 | 2019-04-08 11:07:30 -0700 | [diff] [blame] | 2408 | void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned long goal_nr_mmu_pages) |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 2409 | { |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 2410 | write_lock(&kvm->mmu_lock); |
Takuya Yoshikawa | b34cb59 | 2013-01-08 19:46:07 +0900 | [diff] [blame] | 2411 | |
Dave Hansen | 49d5ca2 | 2010-08-19 18:11:28 -0700 | [diff] [blame] | 2412 | if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) { |
Sean Christopherson | 6b82ef2 | 2020-06-23 12:35:40 -0700 | [diff] [blame] | 2413 | kvm_mmu_zap_oldest_mmu_pages(kvm, kvm->arch.n_used_mmu_pages - |
| 2414 | goal_nr_mmu_pages); |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 2415 | |
Dave Hansen | 49d5ca2 | 2010-08-19 18:11:28 -0700 | [diff] [blame] | 2416 | goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages; |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 2417 | } |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 2418 | |
Dave Hansen | 49d5ca2 | 2010-08-19 18:11:28 -0700 | [diff] [blame] | 2419 | kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages; |
Takuya Yoshikawa | b34cb59 | 2013-01-08 19:46:07 +0900 | [diff] [blame] | 2420 | |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 2421 | write_unlock(&kvm->mmu_lock); |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 2422 | } |
| 2423 | |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 2424 | int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn) |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2425 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2426 | struct kvm_mmu_page *sp; |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 2427 | LIST_HEAD(invalid_list); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2428 | int r; |
| 2429 | |
Xiao Guangrong | 9ad17b10 | 2010-08-28 19:19:42 +0800 | [diff] [blame] | 2430 | pgprintk("%s: looking for gfn %llx\n", __func__, gfn); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2431 | r = 0; |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 2432 | write_lock(&kvm->mmu_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 2433 | for_each_gfn_indirect_valid_sp(kvm, sp, gfn) { |
Xiao Guangrong | 9ad17b10 | 2010-08-28 19:19:42 +0800 | [diff] [blame] | 2434 | pgprintk("%s: gfn %llx role %x\n", __func__, gfn, |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 2435 | sp->role.word); |
| 2436 | r = 1; |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 2437 | kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list); |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 2438 | } |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 2439 | kvm_mmu_commit_zap_page(kvm, &invalid_list); |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 2440 | write_unlock(&kvm->mmu_lock); |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 2441 | |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2442 | return r; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 2443 | } |
Sean Christopherson | 96ad91a | 2021-02-12 16:50:15 -0800 | [diff] [blame] | 2444 | |
| 2445 | static 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 Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 2459 | |
Xiao Guangrong | 5c520e9 | 2016-02-24 17:51:15 +0800 | [diff] [blame] | 2460 | static void kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 2461 | { |
Xiao Guangrong | 5e1b3dd | 2010-04-28 11:55:06 +0800 | [diff] [blame] | 2462 | trace_kvm_mmu_unsync_page(sp); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 2463 | ++vcpu->kvm->stat.mmu_unsync; |
| 2464 | sp->unsync = 1; |
Marcelo Tosatti | 6cffe8c | 2008-12-01 22:32:04 -0200 | [diff] [blame] | 2465 | |
Xiao Guangrong | 6b18493 | 2010-04-16 21:29:17 +0800 | [diff] [blame] | 2466 | kvm_mmu_mark_parents_unsync(sp); |
Xiao Guangrong | 9cf5cf5 | 2010-05-24 15:40:07 +0800 | [diff] [blame] | 2467 | } |
| 2468 | |
Paolo Bonzini | 5a9624a | 2020-10-16 10:29:37 -0400 | [diff] [blame] | 2469 | bool mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn, |
| 2470 | bool can_unsync) |
Xiao Guangrong | 9cf5cf5 | 2010-05-24 15:40:07 +0800 | [diff] [blame] | 2471 | { |
Xiao Guangrong | 5c520e9 | 2016-02-24 17:51:15 +0800 | [diff] [blame] | 2472 | struct kvm_mmu_page *sp; |
Xiao Guangrong | 9cf5cf5 | 2010-05-24 15:40:07 +0800 | [diff] [blame] | 2473 | |
Xiao Guangrong | 3d0c27a | 2016-02-24 17:51:11 +0800 | [diff] [blame] | 2474 | if (kvm_page_track_is_active(vcpu, gfn, KVM_PAGE_TRACK_WRITE)) |
| 2475 | return true; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 2476 | |
Xiao Guangrong | 5c520e9 | 2016-02-24 17:51:15 +0800 | [diff] [blame] | 2477 | for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn) { |
Xiao Guangrong | 36a2e67 | 2010-06-30 16:02:02 +0800 | [diff] [blame] | 2478 | if (!can_unsync) |
Xiao Guangrong | 3d0c27a | 2016-02-24 17:51:11 +0800 | [diff] [blame] | 2479 | return true; |
Xiao Guangrong | 36a2e67 | 2010-06-30 16:02:02 +0800 | [diff] [blame] | 2480 | |
Xiao Guangrong | 5c520e9 | 2016-02-24 17:51:15 +0800 | [diff] [blame] | 2481 | if (sp->unsync) |
| 2482 | continue; |
Xiao Guangrong | 9cf5cf5 | 2010-05-24 15:40:07 +0800 | [diff] [blame] | 2483 | |
Sean Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 2484 | WARN_ON(sp->role.level != PG_LEVEL_4K); |
Xiao Guangrong | 5c520e9 | 2016-02-24 17:51:15 +0800 | [diff] [blame] | 2485 | kvm_unsync_page(vcpu, sp); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 2486 | } |
Xiao Guangrong | 3d0c27a | 2016-02-24 17:51:11 +0800 | [diff] [blame] | 2487 | |
Junaid Shahid | 578e1c4 | 2018-06-27 14:59:05 -0700 | [diff] [blame] | 2488 | /* |
| 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 Guangrong | 3d0c27a | 2016-02-24 17:51:11 +0800 | [diff] [blame] | 2527 | return false; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 2528 | } |
| 2529 | |
Ben Gardon | 799a419 | 2020-10-14 20:26:41 +0200 | [diff] [blame] | 2530 | static 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 Gardon | 799a419 | 2020-10-14 20:26:41 +0200 | [diff] [blame] | 2539 | 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 Christopherson | 1270375 | 2020-09-23 15:04:25 -0700 | [diff] [blame] | 2547 | if (*sptep == spte) |
| 2548 | ret |= SET_SPTE_SPURIOUS; |
| 2549 | else if (mmu_spte_update(sptep, spte)) |
Junaid Shahid | 5ce4786 | 2018-06-27 14:59:04 -0700 | [diff] [blame] | 2550 | ret |= SET_SPTE_NEED_REMOTE_TLB_FLUSH; |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2551 | return ret; |
| 2552 | } |
| 2553 | |
Ben Gardon | 0a2b64c | 2020-02-03 15:09:09 -0800 | [diff] [blame] | 2554 | static int mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep, |
Sean Christopherson | e88b809 | 2020-09-23 11:37:35 -0700 | [diff] [blame] | 2555 | unsigned int pte_access, bool write_fault, int level, |
Ben Gardon | 0a2b64c | 2020-02-03 15:09:09 -0800 | [diff] [blame] | 2556 | gfn_t gfn, kvm_pfn_t pfn, bool speculative, |
| 2557 | bool host_writable) |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2558 | { |
| 2559 | int was_rmapped = 0; |
Marcelo Tosatti | 53a27b3 | 2009-08-05 15:43:58 -0300 | [diff] [blame] | 2560 | int rmap_count; |
Junaid Shahid | 5ce4786 | 2018-06-27 14:59:04 -0700 | [diff] [blame] | 2561 | int set_spte_ret; |
Sean Christopherson | c4371c2 | 2020-09-23 15:04:24 -0700 | [diff] [blame] | 2562 | int ret = RET_PF_FIXED; |
Tianyu Lan | c2a4ead | 2018-07-24 08:17:07 +0000 | [diff] [blame] | 2563 | bool flush = false; |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2564 | |
Xiao Guangrong | f761620 | 2013-02-05 15:27:27 +0800 | [diff] [blame] | 2565 | pgprintk("%s: spte %llx write_fault %d gfn %llx\n", __func__, |
| 2566 | *sptep, write_fault, gfn); |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2567 | |
Sean Christopherson | a54aa15 | 2021-02-25 12:47:32 -0800 | [diff] [blame] | 2568 | if (unlikely(is_noslot_pfn(pfn))) { |
| 2569 | mark_mmio_spte(vcpu, sptep, gfn, pte_access); |
| 2570 | return RET_PF_EMULATE; |
| 2571 | } |
| 2572 | |
Takuya Yoshikawa | afd28fe | 2015-11-20 17:44:55 +0900 | [diff] [blame] | 2573 | if (is_shadow_present_pte(*sptep)) { |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2574 | /* |
| 2575 | * If we overwrite a PTE page pointer with a 2MB PMD, unlink |
| 2576 | * the parent of the now unreachable PTE. |
| 2577 | */ |
Sean Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 2578 | if (level > PG_LEVEL_4K && !is_large_pte(*sptep)) { |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2579 | struct kvm_mmu_page *child; |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2580 | u64 pte = *sptep; |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2581 | |
Sean Christopherson | e47c4ae | 2020-06-22 13:20:34 -0700 | [diff] [blame] | 2582 | child = to_shadow_page(pte & PT64_BASE_ADDR_MASK); |
Xiao Guangrong | bcdd9a9 | 2011-05-15 23:28:29 +0800 | [diff] [blame] | 2583 | drop_parent_pte(child, sptep); |
Tianyu Lan | c2a4ead | 2018-07-24 08:17:07 +0000 | [diff] [blame] | 2584 | flush = true; |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2585 | } else if (pfn != spte_to_pfn(*sptep)) { |
Xiao Guangrong | 9ad17b10 | 2010-08-28 19:19:42 +0800 | [diff] [blame] | 2586 | pgprintk("hfn old %llx new %llx\n", |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2587 | spte_to_pfn(*sptep), pfn); |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 2588 | drop_spte(vcpu->kvm, sptep); |
Tianyu Lan | c2a4ead | 2018-07-24 08:17:07 +0000 | [diff] [blame] | 2589 | flush = true; |
Joerg Roedel | 6bed6b9 | 2009-02-18 14:08:59 +0100 | [diff] [blame] | 2590 | } else |
| 2591 | was_rmapped = 1; |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2592 | } |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 2593 | |
Junaid Shahid | 5ce4786 | 2018-06-27 14:59:04 -0700 | [diff] [blame] | 2594 | 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 Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2597 | if (write_fault) |
Paolo Bonzini | 9b8ebbd | 2017-08-17 15:03:32 +0200 | [diff] [blame] | 2598 | ret = RET_PF_EMULATE; |
Sean Christopherson | 8c8560b | 2020-03-20 14:28:21 -0700 | [diff] [blame] | 2599 | kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); |
Marcelo Tosatti | a378b4e | 2008-09-23 13:18:31 -0300 | [diff] [blame] | 2600 | } |
Lan Tianyu | c3134ce | 2018-12-06 21:21:09 +0800 | [diff] [blame] | 2601 | |
Tianyu Lan | c2a4ead | 2018-07-24 08:17:07 +0000 | [diff] [blame] | 2602 | if (set_spte_ret & SET_SPTE_NEED_REMOTE_TLB_FLUSH || flush) |
Lan Tianyu | c3134ce | 2018-12-06 21:21:09 +0800 | [diff] [blame] | 2603 | kvm_flush_remote_tlbs_with_address(vcpu->kvm, gfn, |
| 2604 | KVM_PAGES_PER_HPAGE(level)); |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2605 | |
Sean Christopherson | 1270375 | 2020-09-23 15:04:25 -0700 | [diff] [blame] | 2606 | /* |
| 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 Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2615 | pgprintk("%s: setting spte %llx\n", __func__, *sptep); |
Paolo Bonzini | 335e192 | 2019-07-01 06:22:57 -0400 | [diff] [blame] | 2616 | trace_kvm_mmu_set_spte(level, gfn, sptep); |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2617 | if (!was_rmapped && is_large_pte(*sptep)) |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 2618 | ++vcpu->kvm->stat.lpages; |
| 2619 | |
Xiao Guangrong | ffb61bb | 2011-07-12 03:22:01 +0800 | [diff] [blame] | 2620 | if (is_shadow_present_pte(*sptep)) { |
Xiao Guangrong | ffb61bb | 2011-07-12 03:22:01 +0800 | [diff] [blame] | 2621 | 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 Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 2626 | } |
Xiao Guangrong | cb9aaa3 | 2012-08-03 15:42:10 +0800 | [diff] [blame] | 2627 | |
Paolo Bonzini | 9b8ebbd | 2017-08-17 15:03:32 +0200 | [diff] [blame] | 2628 | return ret; |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 2629 | } |
| 2630 | |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 2631 | static kvm_pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn, |
Xiao Guangrong | 957ed9e | 2010-08-22 19:12:48 +0800 | [diff] [blame] | 2632 | bool no_dirty_log) |
| 2633 | { |
| 2634 | struct kvm_memory_slot *slot; |
Xiao Guangrong | 957ed9e | 2010-08-22 19:12:48 +0800 | [diff] [blame] | 2635 | |
Xiao Guangrong | 5d163b1 | 2011-03-09 15:43:00 +0800 | [diff] [blame] | 2636 | slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, no_dirty_log); |
Xiao Guangrong | 903816f | 2012-07-17 21:54:11 +0800 | [diff] [blame] | 2637 | if (!slot) |
Xiao Guangrong | 6c8ee57 | 2012-08-03 15:37:54 +0800 | [diff] [blame] | 2638 | return KVM_PFN_ERR_FAULT; |
Xiao Guangrong | 957ed9e | 2010-08-22 19:12:48 +0800 | [diff] [blame] | 2639 | |
Xiao Guangrong | 037d92d | 2012-08-21 10:59:12 +0800 | [diff] [blame] | 2640 | return gfn_to_pfn_memslot_atomic(slot, gfn); |
Xiao Guangrong | 957ed9e | 2010-08-22 19:12:48 +0800 | [diff] [blame] | 2641 | } |
| 2642 | |
| 2643 | static 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 Bonzini | d9ef13c | 2015-05-19 16:01:50 +0200 | [diff] [blame] | 2648 | struct kvm_memory_slot *slot; |
Ben Gardon | 0a2b64c | 2020-02-03 15:09:09 -0800 | [diff] [blame] | 2649 | unsigned int access = sp->role.access; |
Xiao Guangrong | 957ed9e | 2010-08-22 19:12:48 +0800 | [diff] [blame] | 2650 | int i, ret; |
| 2651 | gfn_t gfn; |
| 2652 | |
| 2653 | gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt); |
Paolo Bonzini | d9ef13c | 2015-05-19 16:01:50 +0200 | [diff] [blame] | 2654 | slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, access & ACC_WRITE_MASK); |
| 2655 | if (!slot) |
Xiao Guangrong | 957ed9e | 2010-08-22 19:12:48 +0800 | [diff] [blame] | 2656 | return -1; |
| 2657 | |
Paolo Bonzini | d9ef13c | 2015-05-19 16:01:50 +0200 | [diff] [blame] | 2658 | ret = gfn_to_page_many_atomic(slot, gfn, pages, end - start); |
Xiao Guangrong | 957ed9e | 2010-08-22 19:12:48 +0800 | [diff] [blame] | 2659 | if (ret <= 0) |
| 2660 | return -1; |
| 2661 | |
Junaid Shahid | 43fdcda | 2019-01-03 16:22:21 -0800 | [diff] [blame] | 2662 | for (i = 0; i < ret; i++, gfn++, start++) { |
Sean Christopherson | e88b809 | 2020-09-23 11:37:35 -0700 | [diff] [blame] | 2663 | mmu_set_spte(vcpu, start, access, false, sp->role.level, gfn, |
Takuya Yoshikawa | 029499b | 2015-11-20 17:44:05 +0900 | [diff] [blame] | 2664 | page_to_pfn(pages[i]), true, true); |
Junaid Shahid | 43fdcda | 2019-01-03 16:22:21 -0800 | [diff] [blame] | 2665 | put_page(pages[i]); |
| 2666 | } |
Xiao Guangrong | 957ed9e | 2010-08-22 19:12:48 +0800 | [diff] [blame] | 2667 | |
| 2668 | return 0; |
| 2669 | } |
| 2670 | |
| 2671 | static 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 Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 2683 | if (is_shadow_present_pte(*spte) || spte == sptep) { |
Xiao Guangrong | 957ed9e | 2010-08-22 19:12:48 +0800 | [diff] [blame] | 2684 | 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 | |
| 2694 | static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep) |
| 2695 | { |
| 2696 | struct kvm_mmu_page *sp; |
| 2697 | |
Sean Christopherson | 5735468 | 2020-06-22 13:20:33 -0700 | [diff] [blame] | 2698 | sp = sptep_to_sp(sptep); |
Peter Feiner | ac8d57e | 2017-06-30 17:26:31 -0700 | [diff] [blame] | 2699 | |
Xiao Guangrong | 957ed9e | 2010-08-22 19:12:48 +0800 | [diff] [blame] | 2700 | /* |
Peter Feiner | ac8d57e | 2017-06-30 17:26:31 -0700 | [diff] [blame] | 2701 | * 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 Guangrong | 957ed9e | 2010-08-22 19:12:48 +0800 | [diff] [blame] | 2704 | */ |
Peter Feiner | ac8d57e | 2017-06-30 17:26:31 -0700 | [diff] [blame] | 2705 | if (sp_ad_disabled(sp)) |
Xiao Guangrong | 957ed9e | 2010-08-22 19:12:48 +0800 | [diff] [blame] | 2706 | return; |
| 2707 | |
Sean Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 2708 | if (sp->role.level > PG_LEVEL_4K) |
Xiao Guangrong | 957ed9e | 2010-08-22 19:12:48 +0800 | [diff] [blame] | 2709 | return; |
| 2710 | |
David Stevens | 4a42d84 | 2021-02-22 11:45:22 +0900 | [diff] [blame] | 2711 | /* |
| 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 Guangrong | 957ed9e | 2010-08-22 19:12:48 +0800 | [diff] [blame] | 2718 | __direct_pte_prefetch(vcpu, sp, sptep); |
| 2719 | } |
| 2720 | |
Sean Christopherson | 1b6d9d9 | 2021-02-12 16:50:04 -0800 | [diff] [blame] | 2721 | static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, |
Ben Gardon | 8ca6f06 | 2021-04-01 16:37:24 -0700 | [diff] [blame] | 2722 | const struct kvm_memory_slot *slot) |
Sean Christopherson | db54321 | 2020-01-08 12:24:41 -0800 | [diff] [blame] | 2723 | { |
Sean Christopherson | db54321 | 2020-01-08 12:24:41 -0800 | [diff] [blame] | 2724 | unsigned long hva; |
| 2725 | pte_t *pte; |
| 2726 | int level; |
| 2727 | |
Sean Christopherson | e851265 | 2020-01-08 12:24:48 -0800 | [diff] [blame] | 2728 | if (!PageCompound(pfn_to_page(pfn)) && !kvm_is_zone_device_pfn(pfn)) |
Sean Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 2729 | return PG_LEVEL_4K; |
Sean Christopherson | db54321 | 2020-01-08 12:24:41 -0800 | [diff] [blame] | 2730 | |
Sean Christopherson | 293e306 | 2020-01-08 12:24:46 -0800 | [diff] [blame] | 2731 | /* |
| 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 Christopherson | db54321 | 2020-01-08 12:24:41 -0800 | [diff] [blame] | 2739 | hva = __gfn_to_hva_memslot(slot, gfn); |
| 2740 | |
Sean Christopherson | 1b6d9d9 | 2021-02-12 16:50:04 -0800 | [diff] [blame] | 2741 | pte = lookup_address_in_mm(kvm->mm, hva, &level); |
Sean Christopherson | db54321 | 2020-01-08 12:24:41 -0800 | [diff] [blame] | 2742 | if (unlikely(!pte)) |
Sean Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 2743 | return PG_LEVEL_4K; |
Sean Christopherson | db54321 | 2020-01-08 12:24:41 -0800 | [diff] [blame] | 2744 | |
| 2745 | return level; |
| 2746 | } |
| 2747 | |
Ben Gardon | 8ca6f06 | 2021-04-01 16:37:24 -0700 | [diff] [blame] | 2748 | int 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 Christopherson | 1b6d9d9 | 2021-02-12 16:50:04 -0800 | [diff] [blame] | 2751 | { |
| 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 Gardon | bb18842 | 2020-10-14 11:26:50 -0700 | [diff] [blame] | 2767 | int 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 Christopherson | 0885904 | 2019-12-06 15:57:25 -0800 | [diff] [blame] | 2770 | { |
Sean Christopherson | 293e306 | 2020-01-08 12:24:46 -0800 | [diff] [blame] | 2771 | struct kvm_memory_slot *slot; |
Sean Christopherson | 0885904 | 2019-12-06 15:57:25 -0800 | [diff] [blame] | 2772 | kvm_pfn_t pfn = *pfnp; |
Sean Christopherson | 17eff01 | 2020-01-08 12:24:40 -0800 | [diff] [blame] | 2773 | kvm_pfn_t mask; |
Sean Christopherson | 83f06fa | 2020-01-08 12:24:43 -0800 | [diff] [blame] | 2774 | int level; |
Sean Christopherson | 17eff01 | 2020-01-08 12:24:40 -0800 | [diff] [blame] | 2775 | |
Sean Christopherson | 3cf0661 | 2020-09-23 11:37:31 -0700 | [diff] [blame] | 2776 | *req_level = PG_LEVEL_4K; |
| 2777 | |
Sean Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 2778 | if (unlikely(max_level == PG_LEVEL_4K)) |
| 2779 | return PG_LEVEL_4K; |
Sean Christopherson | 17eff01 | 2020-01-08 12:24:40 -0800 | [diff] [blame] | 2780 | |
Sean Christopherson | e851265 | 2020-01-08 12:24:48 -0800 | [diff] [blame] | 2781 | if (is_error_noslot_pfn(pfn) || kvm_is_reserved_pfn(pfn)) |
Sean Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 2782 | return PG_LEVEL_4K; |
Sean Christopherson | 17eff01 | 2020-01-08 12:24:40 -0800 | [diff] [blame] | 2783 | |
Sean Christopherson | 293e306 | 2020-01-08 12:24:46 -0800 | [diff] [blame] | 2784 | slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, true); |
| 2785 | if (!slot) |
Sean Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 2786 | return PG_LEVEL_4K; |
Sean Christopherson | 293e306 | 2020-01-08 12:24:46 -0800 | [diff] [blame] | 2787 | |
Sean Christopherson | 1b6d9d9 | 2021-02-12 16:50:04 -0800 | [diff] [blame] | 2788 | level = kvm_mmu_max_mapping_level(vcpu->kvm, slot, gfn, pfn, max_level); |
Sean Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 2789 | if (level == PG_LEVEL_4K) |
Sean Christopherson | 83f06fa | 2020-01-08 12:24:43 -0800 | [diff] [blame] | 2790 | return level; |
Sean Christopherson | 17eff01 | 2020-01-08 12:24:40 -0800 | [diff] [blame] | 2791 | |
Sean Christopherson | 3cf0661 | 2020-09-23 11:37:31 -0700 | [diff] [blame] | 2792 | *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 Christopherson | 0885904 | 2019-12-06 15:57:25 -0800 | [diff] [blame] | 2800 | |
| 2801 | /* |
Sean Christopherson | 17eff01 | 2020-01-08 12:24:40 -0800 | [diff] [blame] | 2802 | * mmu_notifier_retry() was successful and mmu_lock is held, so |
| 2803 | * the pmd can't be split from under us. |
Sean Christopherson | 0885904 | 2019-12-06 15:57:25 -0800 | [diff] [blame] | 2804 | */ |
Sean Christopherson | 17eff01 | 2020-01-08 12:24:40 -0800 | [diff] [blame] | 2805 | mask = KVM_PAGES_PER_HPAGE(level) - 1; |
| 2806 | VM_BUG_ON((gfn & mask) != (pfn & mask)); |
| 2807 | *pfnp = pfn & ~mask; |
Sean Christopherson | 83f06fa | 2020-01-08 12:24:43 -0800 | [diff] [blame] | 2808 | |
| 2809 | return level; |
Sean Christopherson | 0885904 | 2019-12-06 15:57:25 -0800 | [diff] [blame] | 2810 | } |
| 2811 | |
Ben Gardon | bb18842 | 2020-10-14 11:26:50 -0700 | [diff] [blame] | 2812 | void disallowed_hugepage_adjust(u64 spte, gfn_t gfn, int cur_level, |
| 2813 | kvm_pfn_t *pfnp, int *goal_levelp) |
Paolo Bonzini | b8e8c83 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 2814 | { |
Ben Gardon | bb18842 | 2020-10-14 11:26:50 -0700 | [diff] [blame] | 2815 | int level = *goal_levelp; |
Paolo Bonzini | b8e8c83 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 2816 | |
Ben Gardon | 7d94531 | 2020-10-14 11:26:49 -0700 | [diff] [blame] | 2817 | if (cur_level == level && level > PG_LEVEL_4K && |
Paolo Bonzini | b8e8c83 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 2818 | 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 Gardon | 7d94531 | 2020-10-14 11:26:49 -0700 | [diff] [blame] | 2827 | u64 page_mask = KVM_PAGES_PER_HPAGE(level) - |
| 2828 | KVM_PAGES_PER_HPAGE(level - 1); |
Paolo Bonzini | b8e8c83 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 2829 | *pfnp |= gfn & page_mask; |
Ben Gardon | bb18842 | 2020-10-14 11:26:50 -0700 | [diff] [blame] | 2830 | (*goal_levelp)--; |
Paolo Bonzini | b8e8c83 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 2831 | } |
| 2832 | } |
| 2833 | |
Sean Christopherson | 6c2fd34 | 2020-09-23 11:37:30 -0700 | [diff] [blame] | 2834 | static int __direct_map(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, |
Sean Christopherson | 83f06fa | 2020-01-08 12:24:43 -0800 | [diff] [blame] | 2835 | int map_writable, int max_level, kvm_pfn_t pfn, |
Sean Christopherson | 6c2fd34 | 2020-09-23 11:37:30 -0700 | [diff] [blame] | 2836 | bool prefault, bool is_tdp) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2837 | { |
Sean Christopherson | 6c2fd34 | 2020-09-23 11:37:30 -0700 | [diff] [blame] | 2838 | 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 Bonzini | 3fcf2d1 | 2019-06-24 13:06:21 +0200 | [diff] [blame] | 2842 | struct kvm_shadow_walk_iterator it; |
Avi Kivity | 9f652d21 | 2008-12-25 14:54:25 +0200 | [diff] [blame] | 2843 | struct kvm_mmu_page *sp; |
Sean Christopherson | 3cf0661 | 2020-09-23 11:37:31 -0700 | [diff] [blame] | 2844 | int level, req_level, ret; |
Paolo Bonzini | 3fcf2d1 | 2019-06-24 13:06:21 +0200 | [diff] [blame] | 2845 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 2846 | gfn_t base_gfn = gfn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2847 | |
Sean Christopherson | 0c7a98e | 2019-12-06 15:57:28 -0800 | [diff] [blame] | 2848 | if (WARN_ON(!VALID_PAGE(vcpu->arch.mmu->root_hpa))) |
Paolo Bonzini | 3fcf2d1 | 2019-06-24 13:06:21 +0200 | [diff] [blame] | 2849 | return RET_PF_RETRY; |
Marcelo Tosatti | 989c6b3 | 2013-12-19 15:28:51 -0200 | [diff] [blame] | 2850 | |
Sean Christopherson | 3cf0661 | 2020-09-23 11:37:31 -0700 | [diff] [blame] | 2851 | level = kvm_mmu_hugepage_adjust(vcpu, gfn, max_level, &pfn, |
| 2852 | huge_page_disallowed, &req_level); |
Sean Christopherson | 4cd071d | 2019-12-06 15:57:26 -0800 | [diff] [blame] | 2853 | |
Paolo Bonzini | 335e192 | 2019-07-01 06:22:57 -0400 | [diff] [blame] | 2854 | trace_kvm_mmu_spte_requested(gpa, level, pfn); |
Paolo Bonzini | 3fcf2d1 | 2019-06-24 13:06:21 +0200 | [diff] [blame] | 2855 | for_each_shadow_entry(vcpu, gpa, it) { |
Paolo Bonzini | b8e8c83 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 2856 | /* |
| 2857 | * We cannot overwrite existing page tables with an NX |
| 2858 | * large page, as the leaf could be executable. |
| 2859 | */ |
Sean Christopherson | dcc7065 | 2020-09-23 11:37:34 -0700 | [diff] [blame] | 2860 | if (nx_huge_page_workaround_enabled) |
Ben Gardon | 7d94531 | 2020-10-14 11:26:49 -0700 | [diff] [blame] | 2861 | disallowed_hugepage_adjust(*it.sptep, gfn, it.level, |
| 2862 | &pfn, &level); |
Paolo Bonzini | b8e8c83 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 2863 | |
Paolo Bonzini | 3fcf2d1 | 2019-06-24 13:06:21 +0200 | [diff] [blame] | 2864 | base_gfn = gfn & ~(KVM_PAGES_PER_HPAGE(it.level) - 1); |
| 2865 | if (it.level == level) |
Avi Kivity | 9f652d21 | 2008-12-25 14:54:25 +0200 | [diff] [blame] | 2866 | break; |
Avi Kivity | 9f652d21 | 2008-12-25 14:54:25 +0200 | [diff] [blame] | 2867 | |
Paolo Bonzini | 3fcf2d1 | 2019-06-24 13:06:21 +0200 | [diff] [blame] | 2868 | 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 Jiangshan | c9fa0b3 | 2010-05-26 16:48:25 +0800 | [diff] [blame] | 2872 | |
Paolo Bonzini | 3fcf2d1 | 2019-06-24 13:06:21 +0200 | [diff] [blame] | 2873 | link_shadow_page(vcpu, it.sptep, sp); |
Sean Christopherson | 5bcaf3e | 2020-09-23 11:37:32 -0700 | [diff] [blame] | 2874 | if (is_tdp && huge_page_disallowed && |
| 2875 | req_level >= it.level) |
Paolo Bonzini | b8e8c83 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 2876 | account_huge_nx_page(vcpu->kvm, sp); |
Avi Kivity | 9f652d21 | 2008-12-25 14:54:25 +0200 | [diff] [blame] | 2877 | } |
| 2878 | } |
Paolo Bonzini | 3fcf2d1 | 2019-06-24 13:06:21 +0200 | [diff] [blame] | 2879 | |
| 2880 | ret = mmu_set_spte(vcpu, it.sptep, ACC_ALL, |
| 2881 | write, level, base_gfn, pfn, prefault, |
| 2882 | map_writable); |
Sean Christopherson | 1270375 | 2020-09-23 15:04:25 -0700 | [diff] [blame] | 2883 | if (ret == RET_PF_SPURIOUS) |
| 2884 | return ret; |
| 2885 | |
Paolo Bonzini | 3fcf2d1 | 2019-06-24 13:06:21 +0200 | [diff] [blame] | 2886 | direct_pte_prefetch(vcpu, it.sptep); |
| 2887 | ++vcpu->stat.pf_fixed; |
| 2888 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2889 | } |
| 2890 | |
Huang Ying | 77db5cb | 2010-10-08 16:24:15 +0800 | [diff] [blame] | 2891 | static void kvm_send_hwpoison_signal(unsigned long address, struct task_struct *tsk) |
Huang Ying | bf99815 | 2010-05-31 14:28:19 +0800 | [diff] [blame] | 2892 | { |
Eric W. Biederman | 585a8b9 | 2018-04-16 14:23:27 -0500 | [diff] [blame] | 2893 | send_sig_mceerr(BUS_MCEERR_AR, (void __user *)address, PAGE_SHIFT, tsk); |
Huang Ying | bf99815 | 2010-05-31 14:28:19 +0800 | [diff] [blame] | 2894 | } |
| 2895 | |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 2896 | static int kvm_handle_bad_page(struct kvm_vcpu *vcpu, gfn_t gfn, kvm_pfn_t pfn) |
Huang Ying | bf99815 | 2010-05-31 14:28:19 +0800 | [diff] [blame] | 2897 | { |
Xiao Guangrong | 4d8b81a | 2012-08-21 11:02:51 +0800 | [diff] [blame] | 2898 | /* |
| 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 Guangrong | 4d8b81a | 2012-08-21 11:02:51 +0800 | [diff] [blame] | 2902 | */ |
| 2903 | if (pfn == KVM_PFN_ERR_RO_FAULT) |
Paolo Bonzini | 9b8ebbd | 2017-08-17 15:03:32 +0200 | [diff] [blame] | 2904 | return RET_PF_EMULATE; |
Xiao Guangrong | 4d8b81a | 2012-08-21 11:02:51 +0800 | [diff] [blame] | 2905 | |
Xiao Guangrong | e6c1502 | 2012-08-03 15:38:36 +0800 | [diff] [blame] | 2906 | if (pfn == KVM_PFN_ERR_HWPOISON) { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 2907 | kvm_send_hwpoison_signal(kvm_vcpu_gfn_to_hva(vcpu, gfn), current); |
Paolo Bonzini | 9b8ebbd | 2017-08-17 15:03:32 +0200 | [diff] [blame] | 2908 | return RET_PF_RETRY; |
Xiao Guangrong | d7c5520 | 2011-07-12 03:29:38 +0800 | [diff] [blame] | 2909 | } |
Gleb Natapov | edba23e | 2010-07-07 20:16:45 +0300 | [diff] [blame] | 2910 | |
Sean Christopherson | 2c151b2 | 2018-03-29 14:48:30 -0700 | [diff] [blame] | 2911 | return -EFAULT; |
Huang Ying | bf99815 | 2010-05-31 14:28:19 +0800 | [diff] [blame] | 2912 | } |
| 2913 | |
Xiao Guangrong | d7c5520 | 2011-07-12 03:29:38 +0800 | [diff] [blame] | 2914 | static bool handle_abnormal_pfn(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn, |
Ben Gardon | 0a2b64c | 2020-02-03 15:09:09 -0800 | [diff] [blame] | 2915 | kvm_pfn_t pfn, unsigned int access, |
| 2916 | int *ret_val) |
Xiao Guangrong | d7c5520 | 2011-07-12 03:29:38 +0800 | [diff] [blame] | 2917 | { |
Xiao Guangrong | d7c5520 | 2011-07-12 03:29:38 +0800 | [diff] [blame] | 2918 | /* The pfn is invalid, report the error! */ |
Xiao Guangrong | 81c52c5 | 2012-10-16 20:10:59 +0800 | [diff] [blame] | 2919 | if (unlikely(is_error_pfn(pfn))) { |
Xiao Guangrong | d7c5520 | 2011-07-12 03:29:38 +0800 | [diff] [blame] | 2920 | *ret_val = kvm_handle_bad_page(vcpu, gfn, pfn); |
Paolo Bonzini | 798e88b | 2016-02-23 15:28:51 +0100 | [diff] [blame] | 2921 | return true; |
Xiao Guangrong | d7c5520 | 2011-07-12 03:29:38 +0800 | [diff] [blame] | 2922 | } |
| 2923 | |
Sean Christopherson | 30ab590 | 2021-02-25 12:47:31 -0800 | [diff] [blame] | 2924 | if (unlikely(is_noslot_pfn(pfn))) { |
Sean Christopherson | 4af7715 | 2019-08-01 13:35:22 -0700 | [diff] [blame] | 2925 | vcpu_cache_mmio_info(vcpu, gva, gfn, |
| 2926 | access & shadow_mmio_access_mask); |
Sean Christopherson | 30ab590 | 2021-02-25 12:47:31 -0800 | [diff] [blame] | 2927 | /* |
| 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 Guangrong | d7c5520 | 2011-07-12 03:29:38 +0800 | [diff] [blame] | 2937 | |
Paolo Bonzini | 798e88b | 2016-02-23 15:28:51 +0100 | [diff] [blame] | 2938 | return false; |
Xiao Guangrong | d7c5520 | 2011-07-12 03:29:38 +0800 | [diff] [blame] | 2939 | } |
| 2940 | |
Xiao Guangrong | e5552fd | 2013-07-30 21:01:59 +0800 | [diff] [blame] | 2941 | static bool page_fault_can_be_fast(u32 error_code) |
Xiao Guangrong | c7ba5b4 | 2012-06-20 15:59:18 +0800 | [diff] [blame] | 2942 | { |
| 2943 | /* |
Xiao Guangrong | 1c118b8 | 2013-07-18 12:52:37 +0800 | [diff] [blame] | 2944 | * 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 Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 2950 | /* 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 Guangrong | c7ba5b4 | 2012-06-20 15:59:18 +0800 | [diff] [blame] | 2953 | return false; |
| 2954 | |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 2955 | /* |
| 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 Guangrong | c7ba5b4 | 2012-06-20 15:59:18 +0800 | [diff] [blame] | 2972 | } |
| 2973 | |
Junaid Shahid | 97dceba | 2016-12-06 16:46:12 -0800 | [diff] [blame] | 2974 | /* |
| 2975 | * Returns true if the SPTE was fixed successfully. Otherwise, |
| 2976 | * someone else modified the SPTE from its original value. |
| 2977 | */ |
Xiao Guangrong | c7ba5b4 | 2012-06-20 15:59:18 +0800 | [diff] [blame] | 2978 | static bool |
Xiao Guangrong | 92a476c | 2014-04-17 17:06:13 +0800 | [diff] [blame] | 2979 | fast_pf_fix_direct_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, |
Junaid Shahid | d3e328f2 | 2016-12-21 20:29:32 -0800 | [diff] [blame] | 2980 | u64 *sptep, u64 old_spte, u64 new_spte) |
Xiao Guangrong | c7ba5b4 | 2012-06-20 15:59:18 +0800 | [diff] [blame] | 2981 | { |
Xiao Guangrong | c7ba5b4 | 2012-06-20 15:59:18 +0800 | [diff] [blame] | 2982 | gfn_t gfn; |
| 2983 | |
| 2984 | WARN_ON(!sp->role.direct); |
| 2985 | |
Kai Huang | 9b51a63 | 2015-01-28 10:54:25 +0800 | [diff] [blame] | 2986 | /* |
| 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 Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 2998 | if (cmpxchg64(sptep, old_spte, new_spte) != old_spte) |
Junaid Shahid | 97dceba | 2016-12-06 16:46:12 -0800 | [diff] [blame] | 2999 | return false; |
| 3000 | |
Junaid Shahid | d3e328f2 | 2016-12-21 20:29:32 -0800 | [diff] [blame] | 3001 | if (is_writable_pte(new_spte) && !is_writable_pte(old_spte)) { |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 3002 | /* |
| 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 Guangrong | c7ba5b4 | 2012-06-20 15:59:18 +0800 | [diff] [blame] | 3009 | |
| 3010 | return true; |
| 3011 | } |
| 3012 | |
Junaid Shahid | d3e328f2 | 2016-12-21 20:29:32 -0800 | [diff] [blame] | 3013 | static 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 Guangrong | c7ba5b4 | 2012-06-20 15:59:18 +0800 | [diff] [blame] | 3025 | /* |
Sean Christopherson | c4371c2 | 2020-09-23 15:04:24 -0700 | [diff] [blame] | 3026 | * Returns one of RET_PF_INVALID, RET_PF_FIXED or RET_PF_SPURIOUS. |
Xiao Guangrong | c7ba5b4 | 2012-06-20 15:59:18 +0800 | [diff] [blame] | 3027 | */ |
Sean Christopherson | c4371c2 | 2020-09-23 15:04:24 -0700 | [diff] [blame] | 3028 | static int fast_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, |
| 3029 | u32 error_code) |
Xiao Guangrong | c7ba5b4 | 2012-06-20 15:59:18 +0800 | [diff] [blame] | 3030 | { |
| 3031 | struct kvm_shadow_walk_iterator iterator; |
Xiao Guangrong | 92a476c | 2014-04-17 17:06:13 +0800 | [diff] [blame] | 3032 | struct kvm_mmu_page *sp; |
Sean Christopherson | c4371c2 | 2020-09-23 15:04:24 -0700 | [diff] [blame] | 3033 | int ret = RET_PF_INVALID; |
Xiao Guangrong | c7ba5b4 | 2012-06-20 15:59:18 +0800 | [diff] [blame] | 3034 | u64 spte = 0ull; |
Junaid Shahid | 97dceba | 2016-12-06 16:46:12 -0800 | [diff] [blame] | 3035 | uint retry_count = 0; |
Xiao Guangrong | c7ba5b4 | 2012-06-20 15:59:18 +0800 | [diff] [blame] | 3036 | |
Xiao Guangrong | e5552fd | 2013-07-30 21:01:59 +0800 | [diff] [blame] | 3037 | if (!page_fault_can_be_fast(error_code)) |
Sean Christopherson | c4371c2 | 2020-09-23 15:04:24 -0700 | [diff] [blame] | 3038 | return ret; |
Xiao Guangrong | c7ba5b4 | 2012-06-20 15:59:18 +0800 | [diff] [blame] | 3039 | |
| 3040 | walk_shadow_page_lockless_begin(vcpu); |
Xiao Guangrong | c7ba5b4 | 2012-06-20 15:59:18 +0800 | [diff] [blame] | 3041 | |
Junaid Shahid | 97dceba | 2016-12-06 16:46:12 -0800 | [diff] [blame] | 3042 | do { |
Junaid Shahid | d3e328f2 | 2016-12-21 20:29:32 -0800 | [diff] [blame] | 3043 | u64 new_spte; |
Xiao Guangrong | c7ba5b4 | 2012-06-20 15:59:18 +0800 | [diff] [blame] | 3044 | |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 3045 | for_each_shadow_entry_lockless(vcpu, cr2_or_gpa, iterator, spte) |
Sean Christopherson | f9fa250 | 2020-01-08 12:24:42 -0800 | [diff] [blame] | 3046 | if (!is_shadow_present_pte(spte)) |
Junaid Shahid | d162f30 | 2016-12-21 20:29:30 -0800 | [diff] [blame] | 3047 | break; |
| 3048 | |
Sean Christopherson | ec89e64 | 2021-02-25 12:47:28 -0800 | [diff] [blame] | 3049 | if (!is_shadow_present_pte(spte)) |
| 3050 | break; |
| 3051 | |
Sean Christopherson | 5735468 | 2020-06-22 13:20:33 -0700 | [diff] [blame] | 3052 | sp = sptep_to_sp(iterator.sptep); |
Junaid Shahid | 97dceba | 2016-12-06 16:46:12 -0800 | [diff] [blame] | 3053 | if (!is_last_spte(spte, sp->role.level)) |
| 3054 | break; |
Xiao Guangrong | c7ba5b4 | 2012-06-20 15:59:18 +0800 | [diff] [blame] | 3055 | |
Junaid Shahid | 97dceba | 2016-12-06 16:46:12 -0800 | [diff] [blame] | 3056 | /* |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 3057 | * 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 Shahid | 97dceba | 2016-12-06 16:46:12 -0800 | [diff] [blame] | 3062 | * |
| 3063 | * Need not check the access of upper level table entries since |
| 3064 | * they are always ACC_ALL. |
| 3065 | */ |
Junaid Shahid | d3e328f2 | 2016-12-21 20:29:32 -0800 | [diff] [blame] | 3066 | if (is_access_allowed(error_code, spte)) { |
Sean Christopherson | c4371c2 | 2020-09-23 15:04:24 -0700 | [diff] [blame] | 3067 | ret = RET_PF_SPURIOUS; |
Junaid Shahid | d3e328f2 | 2016-12-21 20:29:32 -0800 | [diff] [blame] | 3068 | break; |
Junaid Shahid | 97dceba | 2016-12-06 16:46:12 -0800 | [diff] [blame] | 3069 | } |
Xiao Guangrong | c7ba5b4 | 2012-06-20 15:59:18 +0800 | [diff] [blame] | 3070 | |
Junaid Shahid | d3e328f2 | 2016-12-21 20:29:32 -0800 | [diff] [blame] | 3071 | new_spte = spte; |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 3072 | |
Junaid Shahid | d3e328f2 | 2016-12-21 20:29:32 -0800 | [diff] [blame] | 3073 | if (is_access_track_spte(spte)) |
| 3074 | new_spte = restore_acc_track_spte(new_spte); |
Xiao Guangrong | c7ba5b4 | 2012-06-20 15:59:18 +0800 | [diff] [blame] | 3075 | |
Junaid Shahid | 97dceba | 2016-12-06 16:46:12 -0800 | [diff] [blame] | 3076 | /* |
Junaid Shahid | d3e328f2 | 2016-12-21 20:29:32 -0800 | [diff] [blame] | 3077 | * 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 Shahid | 97dceba | 2016-12-06 16:46:12 -0800 | [diff] [blame] | 3080 | */ |
Junaid Shahid | d3e328f2 | 2016-12-21 20:29:32 -0800 | [diff] [blame] | 3081 | if ((error_code & PFERR_WRITE_MASK) && |
Miaohe Lin | e630269 | 2020-02-15 10:44:22 +0800 | [diff] [blame] | 3082 | spte_can_locklessly_be_made_writable(spte)) { |
Junaid Shahid | d3e328f2 | 2016-12-21 20:29:32 -0800 | [diff] [blame] | 3083 | 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 Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 3096 | if (sp->role.level > PG_LEVEL_4K) |
Junaid Shahid | d3e328f2 | 2016-12-21 20:29:32 -0800 | [diff] [blame] | 3097 | 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 Shahid | 97dceba | 2016-12-06 16:46:12 -0800 | [diff] [blame] | 3103 | break; |
Xiao Guangrong | c126d94 | 2014-04-17 17:06:14 +0800 | [diff] [blame] | 3104 | |
Junaid Shahid | 97dceba | 2016-12-06 16:46:12 -0800 | [diff] [blame] | 3105 | /* |
| 3106 | * Currently, fast page fault only works for direct mapping |
| 3107 | * since the gfn is not stable for indirect shadow page. See |
Mauro Carvalho Chehab | 3ecad8c | 2020-04-14 18:48:36 +0200 | [diff] [blame] | 3108 | * Documentation/virt/kvm/locking.rst to get more detail. |
Junaid Shahid | 97dceba | 2016-12-06 16:46:12 -0800 | [diff] [blame] | 3109 | */ |
Sean Christopherson | c4371c2 | 2020-09-23 15:04:24 -0700 | [diff] [blame] | 3110 | if (fast_pf_fix_direct_spte(vcpu, sp, iterator.sptep, spte, |
| 3111 | new_spte)) { |
| 3112 | ret = RET_PF_FIXED; |
Junaid Shahid | 97dceba | 2016-12-06 16:46:12 -0800 | [diff] [blame] | 3113 | break; |
Sean Christopherson | c4371c2 | 2020-09-23 15:04:24 -0700 | [diff] [blame] | 3114 | } |
Junaid Shahid | 97dceba | 2016-12-06 16:46:12 -0800 | [diff] [blame] | 3115 | |
| 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 Shahid | 97dceba | 2016-12-06 16:46:12 -0800 | [diff] [blame] | 3122 | } while (true); |
| 3123 | |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 3124 | trace_fast_page_fault(vcpu, cr2_or_gpa, error_code, iterator.sptep, |
Sean Christopherson | c4371c2 | 2020-09-23 15:04:24 -0700 | [diff] [blame] | 3125 | spte, ret); |
Xiao Guangrong | c7ba5b4 | 2012-06-20 15:59:18 +0800 | [diff] [blame] | 3126 | walk_shadow_page_lockless_end(vcpu); |
| 3127 | |
Sean Christopherson | c4371c2 | 2020-09-23 15:04:24 -0700 | [diff] [blame] | 3128 | return ret; |
Xiao Guangrong | c7ba5b4 | 2012-06-20 15:59:18 +0800 | [diff] [blame] | 3129 | } |
| 3130 | |
Junaid Shahid | 74b566e | 2018-05-04 11:37:11 -0700 | [diff] [blame] | 3131 | static 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 Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3135 | |
Junaid Shahid | 74b566e | 2018-05-04 11:37:11 -0700 | [diff] [blame] | 3136 | if (!VALID_PAGE(*root_hpa)) |
| 3137 | return; |
| 3138 | |
Sean Christopherson | e47c4ae | 2020-06-22 13:20:34 -0700 | [diff] [blame] | 3139 | sp = to_shadow_page(*root_hpa & PT64_BASE_ADDR_MASK); |
Ben Gardon | 02c00b3 | 2020-10-14 20:26:44 +0200 | [diff] [blame] | 3140 | |
Ben Gardon | 2bdb3d8 | 2021-04-01 16:37:27 -0700 | [diff] [blame] | 3141 | if (is_tdp_mmu_page(sp)) |
Ben Gardon | 6103bc0 | 2021-04-01 16:37:32 -0700 | [diff] [blame] | 3142 | kvm_tdp_mmu_put_root(kvm, sp, false); |
Ben Gardon | 76eb54e | 2021-04-01 16:37:25 -0700 | [diff] [blame] | 3143 | else if (!--sp->root_count && sp->role.invalid) |
| 3144 | kvm_mmu_prepare_zap_page(kvm, sp, invalid_list); |
Junaid Shahid | 74b566e | 2018-05-04 11:37:11 -0700 | [diff] [blame] | 3145 | |
| 3146 | *root_hpa = INVALID_PAGE; |
| 3147 | } |
| 3148 | |
Junaid Shahid | 08fb59d | 2018-06-27 14:59:17 -0700 | [diff] [blame] | 3149 | /* roots_to_free must be some combination of the KVM_MMU_ROOT_* flags */ |
Vitaly Kuznetsov | 6a82cd1 | 2018-10-08 21:28:07 +0200 | [diff] [blame] | 3150 | void kvm_mmu_free_roots(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, |
| 3151 | ulong roots_to_free) |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 3152 | { |
Sean Christopherson | 4d710de | 2020-09-23 12:12:04 -0700 | [diff] [blame] | 3153 | struct kvm *kvm = vcpu->kvm; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 3154 | int i; |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 3155 | LIST_HEAD(invalid_list); |
Junaid Shahid | 08fb59d | 2018-06-27 14:59:17 -0700 | [diff] [blame] | 3156 | bool free_active_root = roots_to_free & KVM_MMU_ROOT_CURRENT; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 3157 | |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 3158 | BUILD_BUG_ON(KVM_MMU_NUM_PREV_ROOTS >= BITS_PER_LONG); |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 3159 | |
Junaid Shahid | 08fb59d | 2018-06-27 14:59:17 -0700 | [diff] [blame] | 3160 | /* Before acquiring the MMU lock, see if we need to do any real work. */ |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 3161 | 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 Natapov | 35af577 | 2013-05-16 11:55:51 +0300 | [diff] [blame] | 3170 | |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 3171 | write_lock(&kvm->mmu_lock); |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 3172 | |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 3173 | for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) |
| 3174 | if (roots_to_free & KVM_MMU_ROOT_PREVIOUS(i)) |
Sean Christopherson | 4d710de | 2020-09-23 12:12:04 -0700 | [diff] [blame] | 3175 | mmu_free_root_page(kvm, &mmu->prev_roots[i].hpa, |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 3176 | &invalid_list); |
Junaid Shahid | 7c390d3 | 2018-06-27 14:59:06 -0700 | [diff] [blame] | 3177 | |
Junaid Shahid | 08fb59d | 2018-06-27 14:59:17 -0700 | [diff] [blame] | 3178 | if (free_active_root) { |
| 3179 | if (mmu->shadow_root_level >= PT64_ROOT_4LEVEL && |
| 3180 | (mmu->root_level >= PT64_ROOT_4LEVEL || mmu->direct_map)) { |
Sean Christopherson | 4d710de | 2020-09-23 12:12:04 -0700 | [diff] [blame] | 3181 | mmu_free_root_page(kvm, &mmu->root_hpa, &invalid_list); |
Sean Christopherson | 04d4555 | 2021-03-04 17:10:46 -0800 | [diff] [blame] | 3182 | } else if (mmu->pae_root) { |
Sean Christopherson | c834e5e4 | 2021-03-09 14:42:06 -0800 | [diff] [blame] | 3183 | 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 Shahid | 08fb59d | 2018-06-27 14:59:17 -0700 | [diff] [blame] | 3191 | } |
Sean Christopherson | 04d4555 | 2021-03-04 17:10:46 -0800 | [diff] [blame] | 3192 | mmu->root_hpa = INVALID_PAGE; |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 3193 | mmu->root_pgd = 0; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 3194 | } |
Junaid Shahid | 74b566e | 2018-05-04 11:37:11 -0700 | [diff] [blame] | 3195 | |
Sean Christopherson | 4d710de | 2020-09-23 12:12:04 -0700 | [diff] [blame] | 3196 | kvm_mmu_commit_zap_page(kvm, &invalid_list); |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 3197 | write_unlock(&kvm->mmu_lock); |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 3198 | } |
Junaid Shahid | 74b566e | 2018-05-04 11:37:11 -0700 | [diff] [blame] | 3199 | EXPORT_SYMBOL_GPL(kvm_mmu_free_roots); |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 3200 | |
Marcelo Tosatti | 8986ecc | 2009-05-12 18:55:45 -0300 | [diff] [blame] | 3201 | static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn) |
| 3202 | { |
| 3203 | int ret = 0; |
| 3204 | |
Vitaly Kuznetsov | 995decb | 2020-07-08 16:00:23 +0200 | [diff] [blame] | 3205 | if (!kvm_vcpu_is_visible_gfn(vcpu, root_gfn)) { |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 3206 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Marcelo Tosatti | 8986ecc | 2009-05-12 18:55:45 -0300 | [diff] [blame] | 3207 | ret = 1; |
| 3208 | } |
| 3209 | |
| 3210 | return ret; |
| 3211 | } |
| 3212 | |
Sean Christopherson | 8123f26 | 2020-04-27 19:37:14 -0700 | [diff] [blame] | 3213 | static hpa_t mmu_alloc_root(struct kvm_vcpu *vcpu, gfn_t gfn, gva_t gva, |
| 3214 | u8 level, bool direct) |
Joerg Roedel | 651dd37 | 2010-09-10 17:30:59 +0200 | [diff] [blame] | 3215 | { |
| 3216 | struct kvm_mmu_page *sp; |
Sean Christopherson | 8123f26 | 2020-04-27 19:37:14 -0700 | [diff] [blame] | 3217 | |
Sean Christopherson | 8123f26 | 2020-04-27 19:37:14 -0700 | [diff] [blame] | 3218 | sp = kvm_mmu_get_page(vcpu, gfn, gva, level, direct, ACC_ALL); |
| 3219 | ++sp->root_count; |
| 3220 | |
Sean Christopherson | 8123f26 | 2020-04-27 19:37:14 -0700 | [diff] [blame] | 3221 | return __pa(sp->spt); |
| 3222 | } |
| 3223 | |
| 3224 | static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) |
| 3225 | { |
Sean Christopherson | b37233c | 2021-03-04 17:10:47 -0800 | [diff] [blame] | 3226 | struct kvm_mmu *mmu = vcpu->arch.mmu; |
| 3227 | u8 shadow_root_level = mmu->shadow_root_level; |
Sean Christopherson | 8123f26 | 2020-04-27 19:37:14 -0700 | [diff] [blame] | 3228 | hpa_t root; |
Avi Kivity | 7ebaf15 | 2010-10-03 18:51:39 +0200 | [diff] [blame] | 3229 | unsigned i; |
Paolo Bonzini | 4a38162 | 2021-04-08 08:10:25 -0400 | [diff] [blame] | 3230 | 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 Roedel | 651dd37 | 2010-09-10 17:30:59 +0200 | [diff] [blame] | 3236 | |
Paolo Bonzini | 897218f | 2021-02-06 09:53:33 -0500 | [diff] [blame] | 3237 | if (is_tdp_mmu_enabled(vcpu->kvm)) { |
Ben Gardon | 02c00b3 | 2020-10-14 20:26:44 +0200 | [diff] [blame] | 3238 | root = kvm_tdp_mmu_get_vcpu_root_hpa(vcpu); |
Sean Christopherson | b37233c | 2021-03-04 17:10:47 -0800 | [diff] [blame] | 3239 | mmu->root_hpa = root; |
Ben Gardon | 02c00b3 | 2020-10-14 20:26:44 +0200 | [diff] [blame] | 3240 | } else if (shadow_root_level >= PT64_ROOT_4LEVEL) { |
Sean Christopherson | 6e6ec58 | 2021-03-04 17:10:50 -0800 | [diff] [blame] | 3241 | root = mmu_alloc_root(vcpu, 0, 0, shadow_root_level, true); |
Sean Christopherson | b37233c | 2021-03-04 17:10:47 -0800 | [diff] [blame] | 3242 | mmu->root_hpa = root; |
Sean Christopherson | 8123f26 | 2020-04-27 19:37:14 -0700 | [diff] [blame] | 3243 | } else if (shadow_root_level == PT32E_ROOT_LEVEL) { |
Paolo Bonzini | 4a38162 | 2021-04-08 08:10:25 -0400 | [diff] [blame] | 3244 | if (WARN_ON_ONCE(!mmu->pae_root)) { |
| 3245 | r = -EIO; |
| 3246 | goto out_unlock; |
| 3247 | } |
Sean Christopherson | 73ad160 | 2021-03-04 17:11:01 -0800 | [diff] [blame] | 3248 | |
Joerg Roedel | 651dd37 | 2010-09-10 17:30:59 +0200 | [diff] [blame] | 3249 | for (i = 0; i < 4; ++i) { |
Sean Christopherson | c834e5e4 | 2021-03-09 14:42:06 -0800 | [diff] [blame] | 3250 | WARN_ON_ONCE(IS_VALID_PAE_ROOT(mmu->pae_root[i])); |
Joerg Roedel | 651dd37 | 2010-09-10 17:30:59 +0200 | [diff] [blame] | 3251 | |
Sean Christopherson | 8123f26 | 2020-04-27 19:37:14 -0700 | [diff] [blame] | 3252 | root = mmu_alloc_root(vcpu, i << (30 - PAGE_SHIFT), |
| 3253 | i << 30, PT32_ROOT_LEVEL, true); |
Sean Christopherson | 17e368d | 2021-03-04 17:10:54 -0800 | [diff] [blame] | 3254 | mmu->pae_root[i] = root | PT_PRESENT_MASK | |
| 3255 | shadow_me_mask; |
Joerg Roedel | 651dd37 | 2010-09-10 17:30:59 +0200 | [diff] [blame] | 3256 | } |
Sean Christopherson | b37233c | 2021-03-04 17:10:47 -0800 | [diff] [blame] | 3257 | mmu->root_hpa = __pa(mmu->pae_root); |
Sean Christopherson | 73ad160 | 2021-03-04 17:11:01 -0800 | [diff] [blame] | 3258 | } else { |
| 3259 | WARN_ONCE(1, "Bad TDP root level = %d\n", shadow_root_level); |
Paolo Bonzini | 4a38162 | 2021-04-08 08:10:25 -0400 | [diff] [blame] | 3260 | r = -EIO; |
| 3261 | goto out_unlock; |
Sean Christopherson | 73ad160 | 2021-03-04 17:11:01 -0800 | [diff] [blame] | 3262 | } |
Sean Christopherson | 3651c7f | 2020-02-28 14:52:39 -0800 | [diff] [blame] | 3263 | |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 3264 | /* root_pgd is ignored for direct MMUs. */ |
Sean Christopherson | b37233c | 2021-03-04 17:10:47 -0800 | [diff] [blame] | 3265 | mmu->root_pgd = 0; |
Paolo Bonzini | 4a38162 | 2021-04-08 08:10:25 -0400 | [diff] [blame] | 3266 | out_unlock: |
| 3267 | write_unlock(&vcpu->kvm->mmu_lock); |
| 3268 | return r; |
Joerg Roedel | 651dd37 | 2010-09-10 17:30:59 +0200 | [diff] [blame] | 3269 | } |
| 3270 | |
| 3271 | static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu) |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 3272 | { |
Sean Christopherson | b37233c | 2021-03-04 17:10:47 -0800 | [diff] [blame] | 3273 | struct kvm_mmu *mmu = vcpu->arch.mmu; |
Sean Christopherson | 6e0918a | 2021-03-04 17:10:51 -0800 | [diff] [blame] | 3274 | u64 pdptrs[4], pm_mask; |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 3275 | gfn_t root_gfn, root_pgd; |
Sean Christopherson | 8123f26 | 2020-04-27 19:37:14 -0700 | [diff] [blame] | 3276 | hpa_t root; |
Paolo Bonzini | 4a38162 | 2021-04-08 08:10:25 -0400 | [diff] [blame] | 3277 | unsigned i; |
| 3278 | int r; |
Avi Kivity | 3bb65a2 | 2007-01-05 16:36:51 -0800 | [diff] [blame] | 3279 | |
Sean Christopherson | b37233c | 2021-03-04 17:10:47 -0800 | [diff] [blame] | 3280 | root_pgd = mmu->get_guest_pgd(vcpu); |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 3281 | root_gfn = root_pgd >> PAGE_SHIFT; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 3282 | |
Joerg Roedel | 651dd37 | 2010-09-10 17:30:59 +0200 | [diff] [blame] | 3283 | if (mmu_check_root(vcpu, root_gfn)) |
| 3284 | return 1; |
| 3285 | |
| 3286 | /* |
Paolo Bonzini | 4a38162 | 2021-04-08 08:10:25 -0400 | [diff] [blame] | 3287 | * 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 Christopherson | 6e0918a | 2021-03-04 17:10:51 -0800 | [diff] [blame] | 3290 | 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 Bonzini | 4a38162 | 2021-04-08 08:10:25 -0400 | [diff] [blame] | 3301 | write_lock(&vcpu->kvm->mmu_lock); |
| 3302 | r = make_mmu_pages_available(vcpu); |
| 3303 | if (r < 0) |
| 3304 | goto out_unlock; |
| 3305 | |
Joerg Roedel | 651dd37 | 2010-09-10 17:30:59 +0200 | [diff] [blame] | 3306 | /* |
| 3307 | * Do we shadow a long mode page table? If so we need to |
| 3308 | * write-protect the guests page table root. |
| 3309 | */ |
Sean Christopherson | b37233c | 2021-03-04 17:10:47 -0800 | [diff] [blame] | 3310 | if (mmu->root_level >= PT64_ROOT_4LEVEL) { |
Sean Christopherson | 8123f26 | 2020-04-27 19:37:14 -0700 | [diff] [blame] | 3311 | root = mmu_alloc_root(vcpu, root_gfn, 0, |
Sean Christopherson | b37233c | 2021-03-04 17:10:47 -0800 | [diff] [blame] | 3312 | mmu->shadow_root_level, false); |
Sean Christopherson | b37233c | 2021-03-04 17:10:47 -0800 | [diff] [blame] | 3313 | mmu->root_hpa = root; |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 3314 | goto set_root_pgd; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 3315 | } |
Joerg Roedel | f87f928 | 2010-09-02 17:29:45 +0200 | [diff] [blame] | 3316 | |
Paolo Bonzini | 4a38162 | 2021-04-08 08:10:25 -0400 | [diff] [blame] | 3317 | if (WARN_ON_ONCE(!mmu->pae_root)) { |
| 3318 | r = -EIO; |
| 3319 | goto out_unlock; |
| 3320 | } |
Sean Christopherson | 73ad160 | 2021-03-04 17:11:01 -0800 | [diff] [blame] | 3321 | |
Joerg Roedel | 651dd37 | 2010-09-10 17:30:59 +0200 | [diff] [blame] | 3322 | /* |
| 3323 | * We shadow a 32 bit page table. This may be a legacy 2-level |
Joerg Roedel | 81407ca | 2010-09-10 17:31:00 +0200 | [diff] [blame] | 3324 | * 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 Roedel | 651dd37 | 2010-09-10 17:30:59 +0200 | [diff] [blame] | 3326 | */ |
Sean Christopherson | 17e368d | 2021-03-04 17:10:54 -0800 | [diff] [blame] | 3327 | pm_mask = PT_PRESENT_MASK | shadow_me_mask; |
Sean Christopherson | 748e52b | 2021-03-04 17:10:49 -0800 | [diff] [blame] | 3328 | if (mmu->shadow_root_level == PT64_ROOT_4LEVEL) { |
Joerg Roedel | 81407ca | 2010-09-10 17:31:00 +0200 | [diff] [blame] | 3329 | pm_mask |= PT_ACCESSED_MASK | PT_WRITABLE_MASK | PT_USER_MASK; |
| 3330 | |
Sean Christopherson | 03ca458 | 2021-05-05 13:42:21 -0700 | [diff] [blame] | 3331 | if (WARN_ON_ONCE(!mmu->pml4_root)) { |
Paolo Bonzini | 4a38162 | 2021-04-08 08:10:25 -0400 | [diff] [blame] | 3332 | r = -EIO; |
| 3333 | goto out_unlock; |
| 3334 | } |
Sean Christopherson | 73ad160 | 2021-03-04 17:11:01 -0800 | [diff] [blame] | 3335 | |
Sean Christopherson | 03ca458 | 2021-05-05 13:42:21 -0700 | [diff] [blame] | 3336 | mmu->pml4_root[0] = __pa(mmu->pae_root) | pm_mask; |
Sean Christopherson | 04d4555 | 2021-03-04 17:10:46 -0800 | [diff] [blame] | 3337 | } |
| 3338 | |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 3339 | for (i = 0; i < 4; ++i) { |
Sean Christopherson | c834e5e4 | 2021-03-09 14:42:06 -0800 | [diff] [blame] | 3340 | WARN_ON_ONCE(IS_VALID_PAE_ROOT(mmu->pae_root[i])); |
Sean Christopherson | 6e6ec58 | 2021-03-04 17:10:50 -0800 | [diff] [blame] | 3341 | |
Sean Christopherson | b37233c | 2021-03-04 17:10:47 -0800 | [diff] [blame] | 3342 | if (mmu->root_level == PT32E_ROOT_LEVEL) { |
Sean Christopherson | 6e0918a | 2021-03-04 17:10:51 -0800 | [diff] [blame] | 3343 | if (!(pdptrs[i] & PT_PRESENT_MASK)) { |
Sean Christopherson | c834e5e4 | 2021-03-09 14:42:06 -0800 | [diff] [blame] | 3344 | mmu->pae_root[i] = INVALID_PAE_ROOT; |
Avi Kivity | 417726a | 2007-04-12 17:35:58 +0300 | [diff] [blame] | 3345 | continue; |
| 3346 | } |
Sean Christopherson | 6e0918a | 2021-03-04 17:10:51 -0800 | [diff] [blame] | 3347 | root_gfn = pdptrs[i] >> PAGE_SHIFT; |
Eric Northup | 5a7388c | 2010-04-26 17:00:05 -0700 | [diff] [blame] | 3348 | } |
Avi Kivity | 8facbbf | 2010-05-04 12:58:32 +0300 | [diff] [blame] | 3349 | |
Sean Christopherson | 8123f26 | 2020-04-27 19:37:14 -0700 | [diff] [blame] | 3350 | root = mmu_alloc_root(vcpu, root_gfn, i << 30, |
| 3351 | PT32_ROOT_LEVEL, false); |
Sean Christopherson | b37233c | 2021-03-04 17:10:47 -0800 | [diff] [blame] | 3352 | mmu->pae_root[i] = root | pm_mask; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 3353 | } |
Joerg Roedel | 81407ca | 2010-09-10 17:31:00 +0200 | [diff] [blame] | 3354 | |
Sean Christopherson | ba0a194 | 2021-03-04 17:10:48 -0800 | [diff] [blame] | 3355 | if (mmu->shadow_root_level == PT64_ROOT_4LEVEL) |
Sean Christopherson | 03ca458 | 2021-05-05 13:42:21 -0700 | [diff] [blame] | 3356 | mmu->root_hpa = __pa(mmu->pml4_root); |
Sean Christopherson | ba0a194 | 2021-03-04 17:10:48 -0800 | [diff] [blame] | 3357 | else |
| 3358 | mmu->root_hpa = __pa(mmu->pae_root); |
Joerg Roedel | 81407ca | 2010-09-10 17:31:00 +0200 | [diff] [blame] | 3359 | |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 3360 | set_root_pgd: |
Sean Christopherson | b37233c | 2021-03-04 17:10:47 -0800 | [diff] [blame] | 3361 | mmu->root_pgd = root_pgd; |
Paolo Bonzini | 4a38162 | 2021-04-08 08:10:25 -0400 | [diff] [blame] | 3362 | out_unlock: |
| 3363 | write_unlock(&vcpu->kvm->mmu_lock); |
Vitaly Kuznetsov | ad7dc69 | 2019-02-22 17:45:01 +0100 | [diff] [blame] | 3364 | |
Marcelo Tosatti | 8986ecc | 2009-05-12 18:55:45 -0300 | [diff] [blame] | 3365 | return 0; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 3366 | } |
| 3367 | |
Sean Christopherson | 748e52b | 2021-03-04 17:10:49 -0800 | [diff] [blame] | 3368 | static int mmu_alloc_special_roots(struct kvm_vcpu *vcpu) |
Joerg Roedel | 651dd37 | 2010-09-10 17:30:59 +0200 | [diff] [blame] | 3369 | { |
Sean Christopherson | 748e52b | 2021-03-04 17:10:49 -0800 | [diff] [blame] | 3370 | struct kvm_mmu *mmu = vcpu->arch.mmu; |
Sean Christopherson | 03ca458 | 2021-05-05 13:42:21 -0700 | [diff] [blame] | 3371 | u64 *pml4_root, *pae_root; |
Sean Christopherson | 748e52b | 2021-03-04 17:10:49 -0800 | [diff] [blame] | 3372 | |
| 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 Christopherson | 03ca458 | 2021-05-05 13:42:21 -0700 | [diff] [blame] | 3390 | if (mmu->pae_root && mmu->pml4_root) |
Sean Christopherson | 748e52b | 2021-03-04 17:10:49 -0800 | [diff] [blame] | 3391 | 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 Christopherson | 03ca458 | 2021-05-05 13:42:21 -0700 | [diff] [blame] | 3397 | if (WARN_ON_ONCE(!tdp_enabled || mmu->pae_root || mmu->pml4_root)) |
Sean Christopherson | 748e52b | 2021-03-04 17:10:49 -0800 | [diff] [blame] | 3398 | return -EIO; |
| 3399 | |
Sean Christopherson | 4a98623 | 2021-03-09 14:42:07 -0800 | [diff] [blame] | 3400 | /* |
| 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 Christopherson | 748e52b | 2021-03-04 17:10:49 -0800 | [diff] [blame] | 3404 | pae_root = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT); |
| 3405 | if (!pae_root) |
| 3406 | return -ENOMEM; |
| 3407 | |
Sean Christopherson | 03ca458 | 2021-05-05 13:42:21 -0700 | [diff] [blame] | 3408 | pml4_root = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT); |
| 3409 | if (!pml4_root) { |
Sean Christopherson | 748e52b | 2021-03-04 17:10:49 -0800 | [diff] [blame] | 3410 | free_page((unsigned long)pae_root); |
| 3411 | return -ENOMEM; |
| 3412 | } |
| 3413 | |
| 3414 | mmu->pae_root = pae_root; |
Sean Christopherson | 03ca458 | 2021-05-05 13:42:21 -0700 | [diff] [blame] | 3415 | mmu->pml4_root = pml4_root; |
Sean Christopherson | 748e52b | 2021-03-04 17:10:49 -0800 | [diff] [blame] | 3416 | |
| 3417 | return 0; |
Joerg Roedel | 651dd37 | 2010-09-10 17:30:59 +0200 | [diff] [blame] | 3418 | } |
| 3419 | |
Junaid Shahid | 578e1c4 | 2018-06-27 14:59:05 -0700 | [diff] [blame] | 3420 | void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 3421 | { |
| 3422 | int i; |
| 3423 | struct kvm_mmu_page *sp; |
| 3424 | |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 3425 | if (vcpu->arch.mmu->direct_map) |
Joerg Roedel | 81407ca | 2010-09-10 17:31:00 +0200 | [diff] [blame] | 3426 | return; |
| 3427 | |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 3428 | if (!VALID_PAGE(vcpu->arch.mmu->root_hpa)) |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 3429 | return; |
Xiao Guangrong | 6903074 | 2010-09-27 18:09:29 +0800 | [diff] [blame] | 3430 | |
David Matlack | 56f17dd | 2014-08-18 15:46:07 -0700 | [diff] [blame] | 3431 | vcpu_clear_mmio_info(vcpu, MMIO_GVA_ANY); |
Junaid Shahid | 578e1c4 | 2018-06-27 14:59:05 -0700 | [diff] [blame] | 3432 | |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 3433 | if (vcpu->arch.mmu->root_level >= PT64_ROOT_4LEVEL) { |
| 3434 | hpa_t root = vcpu->arch.mmu->root_hpa; |
Sean Christopherson | e47c4ae | 2020-06-22 13:20:34 -0700 | [diff] [blame] | 3435 | sp = to_shadow_page(root); |
Junaid Shahid | 578e1c4 | 2018-06-27 14:59:05 -0700 | [diff] [blame] | 3436 | |
| 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 Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 3451 | write_lock(&vcpu->kvm->mmu_lock); |
Junaid Shahid | 578e1c4 | 2018-06-27 14:59:05 -0700 | [diff] [blame] | 3452 | kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC); |
| 3453 | |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 3454 | mmu_sync_children(vcpu, sp); |
Junaid Shahid | 578e1c4 | 2018-06-27 14:59:05 -0700 | [diff] [blame] | 3455 | |
Xiao Guangrong | 0375f7f | 2011-11-28 20:41:00 +0800 | [diff] [blame] | 3456 | kvm_mmu_audit(vcpu, AUDIT_POST_SYNC); |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 3457 | write_unlock(&vcpu->kvm->mmu_lock); |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 3458 | return; |
| 3459 | } |
Junaid Shahid | 578e1c4 | 2018-06-27 14:59:05 -0700 | [diff] [blame] | 3460 | |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 3461 | write_lock(&vcpu->kvm->mmu_lock); |
Junaid Shahid | 578e1c4 | 2018-06-27 14:59:05 -0700 | [diff] [blame] | 3462 | kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC); |
| 3463 | |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 3464 | for (i = 0; i < 4; ++i) { |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 3465 | hpa_t root = vcpu->arch.mmu->pae_root[i]; |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 3466 | |
Sean Christopherson | c834e5e4 | 2021-03-09 14:42:06 -0800 | [diff] [blame] | 3467 | if (IS_VALID_PAE_ROOT(root)) { |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 3468 | root &= PT64_BASE_ADDR_MASK; |
Sean Christopherson | e47c4ae | 2020-06-22 13:20:34 -0700 | [diff] [blame] | 3469 | sp = to_shadow_page(root); |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 3470 | mmu_sync_children(vcpu, sp); |
| 3471 | } |
| 3472 | } |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 3473 | |
Junaid Shahid | 578e1c4 | 2018-06-27 14:59:05 -0700 | [diff] [blame] | 3474 | kvm_mmu_audit(vcpu, AUDIT_POST_SYNC); |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 3475 | write_unlock(&vcpu->kvm->mmu_lock); |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 3476 | } |
| 3477 | |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 3478 | static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gpa_t vaddr, |
Avi Kivity | ab9ae31 | 2010-11-22 17:53:26 +0200 | [diff] [blame] | 3479 | u32 access, struct x86_exception *exception) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3480 | { |
Avi Kivity | ab9ae31 | 2010-11-22 17:53:26 +0200 | [diff] [blame] | 3481 | if (exception) |
| 3482 | exception->error_code = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3483 | return vaddr; |
| 3484 | } |
| 3485 | |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 3486 | static gpa_t nonpaging_gva_to_gpa_nested(struct kvm_vcpu *vcpu, gpa_t vaddr, |
Avi Kivity | ab9ae31 | 2010-11-22 17:53:26 +0200 | [diff] [blame] | 3487 | u32 access, |
| 3488 | struct x86_exception *exception) |
Joerg Roedel | 6539e73 | 2010-09-10 17:30:50 +0200 | [diff] [blame] | 3489 | { |
Avi Kivity | ab9ae31 | 2010-11-22 17:53:26 +0200 | [diff] [blame] | 3490 | if (exception) |
| 3491 | exception->error_code = 0; |
Paolo Bonzini | 54987b7 | 2014-09-02 13:23:06 +0200 | [diff] [blame] | 3492 | return vcpu->arch.nested_mmu.translate_gpa(vcpu, vaddr, access, exception); |
Joerg Roedel | 6539e73 | 2010-09-10 17:30:50 +0200 | [diff] [blame] | 3493 | } |
| 3494 | |
Xiao Guangrong | d625b15 | 2015-08-05 12:04:25 +0800 | [diff] [blame] | 3495 | static bool |
| 3496 | __is_rsvd_bits_set(struct rsvd_bits_validate *rsvd_check, u64 pte, int level) |
| 3497 | { |
Sean Christopherson | b5c3c1b | 2020-01-09 15:06:40 -0800 | [diff] [blame] | 3498 | int bit7 = (pte >> 7) & 1; |
Xiao Guangrong | d625b15 | 2015-08-05 12:04:25 +0800 | [diff] [blame] | 3499 | |
Sean Christopherson | b5c3c1b | 2020-01-09 15:06:40 -0800 | [diff] [blame] | 3500 | return pte & rsvd_check->rsvd_bits_mask[bit7][level-1]; |
Xiao Guangrong | d625b15 | 2015-08-05 12:04:25 +0800 | [diff] [blame] | 3501 | } |
| 3502 | |
Sean Christopherson | b5c3c1b | 2020-01-09 15:06:40 -0800 | [diff] [blame] | 3503 | static bool __is_bad_mt_xwr(struct rsvd_bits_validate *rsvd_check, u64 pte) |
Xiao Guangrong | d625b15 | 2015-08-05 12:04:25 +0800 | [diff] [blame] | 3504 | { |
Sean Christopherson | b5c3c1b | 2020-01-09 15:06:40 -0800 | [diff] [blame] | 3505 | return rsvd_check->bad_mt_xwr & BIT_ULL(pte & 0x3f); |
Xiao Guangrong | d625b15 | 2015-08-05 12:04:25 +0800 | [diff] [blame] | 3506 | } |
| 3507 | |
Takuya Yoshikawa | ded5874 | 2016-02-22 17:23:40 +0900 | [diff] [blame] | 3508 | static bool mmio_info_in_cache(struct kvm_vcpu *vcpu, u64 addr, bool direct) |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3509 | { |
Paolo Bonzini | 9034e6e | 2017-08-17 18:36:58 +0200 | [diff] [blame] | 3510 | /* |
| 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 Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3517 | if (direct) |
| 3518 | return vcpu_match_mmio_gpa(vcpu, addr); |
| 3519 | |
| 3520 | return vcpu_match_mmio_gva(vcpu, addr); |
| 3521 | } |
| 3522 | |
Ben Gardon | 95fb5b0 | 2020-10-14 11:26:58 -0700 | [diff] [blame] | 3523 | /* |
| 3524 | * Return the level of the lowest level SPTE added to sptes. |
| 3525 | * That SPTE may be non-present. |
| 3526 | */ |
Sean Christopherson | 39b4d43 | 2020-12-17 16:31:37 -0800 | [diff] [blame] | 3527 | static int get_walk(struct kvm_vcpu *vcpu, u64 addr, u64 *sptes, int *root_level) |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3528 | { |
| 3529 | struct kvm_shadow_walk_iterator iterator; |
Sean Christopherson | 2aa07893 | 2020-12-17 16:31:36 -0800 | [diff] [blame] | 3530 | int leaf = -1; |
Ben Gardon | 95fb5b0 | 2020-10-14 11:26:58 -0700 | [diff] [blame] | 3531 | u64 spte; |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3532 | |
| 3533 | walk_shadow_page_lockless_begin(vcpu); |
Xiao Guangrong | 47ab875 | 2015-08-05 12:04:26 +0800 | [diff] [blame] | 3534 | |
Sean Christopherson | 39b4d43 | 2020-12-17 16:31:37 -0800 | [diff] [blame] | 3535 | for (shadow_walk_init(&iterator, vcpu, addr), |
| 3536 | *root_level = iterator.level; |
Xiao Guangrong | 47ab875 | 2015-08-05 12:04:26 +0800 | [diff] [blame] | 3537 | shadow_walk_okay(&iterator); |
| 3538 | __shadow_walk_next(&iterator, spte)) { |
Ben Gardon | 95fb5b0 | 2020-10-14 11:26:58 -0700 | [diff] [blame] | 3539 | leaf = iterator.level; |
Xiao Guangrong | 47ab875 | 2015-08-05 12:04:26 +0800 | [diff] [blame] | 3540 | spte = mmu_spte_get_lockless(iterator.sptep); |
| 3541 | |
Sean Christopherson | dde81f9 | 2020-12-17 16:31:38 -0800 | [diff] [blame] | 3542 | sptes[leaf] = spte; |
Xiao Guangrong | 47ab875 | 2015-08-05 12:04:26 +0800 | [diff] [blame] | 3543 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3544 | if (!is_shadow_present_pte(spte)) |
| 3545 | break; |
Ben Gardon | 95fb5b0 | 2020-10-14 11:26:58 -0700 | [diff] [blame] | 3546 | } |
| 3547 | |
| 3548 | walk_shadow_page_lockless_end(vcpu); |
| 3549 | |
| 3550 | return leaf; |
| 3551 | } |
| 3552 | |
Sean Christopherson | 9aa4187 | 2020-12-17 16:31:39 -0800 | [diff] [blame] | 3553 | /* return true if reserved bit(s) are detected on a valid, non-MMIO SPTE. */ |
Ben Gardon | 95fb5b0 | 2020-10-14 11:26:58 -0700 | [diff] [blame] | 3554 | static bool get_mmio_spte(struct kvm_vcpu *vcpu, u64 addr, u64 *sptep) |
| 3555 | { |
Sean Christopherson | dde81f9 | 2020-12-17 16:31:38 -0800 | [diff] [blame] | 3556 | u64 sptes[PT64_ROOT_MAX_LEVEL + 1]; |
Ben Gardon | 95fb5b0 | 2020-10-14 11:26:58 -0700 | [diff] [blame] | 3557 | struct rsvd_bits_validate *rsvd_check; |
Sean Christopherson | 39b4d43 | 2020-12-17 16:31:37 -0800 | [diff] [blame] | 3558 | int root, leaf, level; |
Ben Gardon | 95fb5b0 | 2020-10-14 11:26:58 -0700 | [diff] [blame] | 3559 | 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 Christopherson | 39b4d43 | 2020-12-17 16:31:37 -0800 | [diff] [blame] | 3567 | leaf = kvm_tdp_mmu_get_walk(vcpu, addr, sptes, &root); |
Ben Gardon | 95fb5b0 | 2020-10-14 11:26:58 -0700 | [diff] [blame] | 3568 | else |
Sean Christopherson | 39b4d43 | 2020-12-17 16:31:37 -0800 | [diff] [blame] | 3569 | leaf = get_walk(vcpu, addr, sptes, &root); |
Ben Gardon | 95fb5b0 | 2020-10-14 11:26:58 -0700 | [diff] [blame] | 3570 | |
Sean Christopherson | 2aa07893 | 2020-12-17 16:31:36 -0800 | [diff] [blame] | 3571 | if (unlikely(leaf < 0)) { |
| 3572 | *sptep = 0ull; |
| 3573 | return reserved; |
| 3574 | } |
| 3575 | |
Sean Christopherson | 9aa4187 | 2020-12-17 16:31:39 -0800 | [diff] [blame] | 3576 | *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 Gardon | 95fb5b0 | 2020-10-14 11:26:58 -0700 | [diff] [blame] | 3586 | |
| 3587 | rsvd_check = &vcpu->arch.mmu->shadow_zero_check; |
| 3588 | |
Sean Christopherson | 9aa4187 | 2020-12-17 16:31:39 -0800 | [diff] [blame] | 3589 | for (level = root; level >= leaf; level--) |
Sean Christopherson | b5c3c1b | 2020-01-09 15:06:40 -0800 | [diff] [blame] | 3590 | /* |
| 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 Christopherson | dde81f9 | 2020-12-17 16:31:38 -0800 | [diff] [blame] | 3595 | reserved |= __is_bad_mt_xwr(rsvd_check, sptes[level]) | |
| 3596 | __is_rsvd_bits_set(rsvd_check, sptes[level], level); |
Xiao Guangrong | 47ab875 | 2015-08-05 12:04:26 +0800 | [diff] [blame] | 3597 | |
Xiao Guangrong | 47ab875 | 2015-08-05 12:04:26 +0800 | [diff] [blame] | 3598 | if (reserved) { |
Sean Christopherson | bb4cdf3 | 2021-02-25 12:47:49 -0800 | [diff] [blame] | 3599 | pr_err("%s: reserved bits set on MMU-present spte, addr 0x%llx, hierarchy:\n", |
Xiao Guangrong | 47ab875 | 2015-08-05 12:04:26 +0800 | [diff] [blame] | 3600 | __func__, addr); |
Ben Gardon | 95fb5b0 | 2020-10-14 11:26:58 -0700 | [diff] [blame] | 3601 | for (level = root; level >= leaf; level--) |
Sean Christopherson | bb4cdf3 | 2021-02-25 12:47:49 -0800 | [diff] [blame] | 3602 | 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 Guangrong | 47ab875 | 2015-08-05 12:04:26 +0800 | [diff] [blame] | 3605 | } |
Sean Christopherson | ddce620 | 2019-12-06 15:57:27 -0800 | [diff] [blame] | 3606 | |
Xiao Guangrong | 47ab875 | 2015-08-05 12:04:26 +0800 | [diff] [blame] | 3607 | return reserved; |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3608 | } |
| 3609 | |
Paolo Bonzini | e08d26f | 2017-08-17 18:36:56 +0200 | [diff] [blame] | 3610 | static int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr, bool direct) |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3611 | { |
| 3612 | u64 spte; |
Xiao Guangrong | 47ab875 | 2015-08-05 12:04:26 +0800 | [diff] [blame] | 3613 | bool reserved; |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3614 | |
Takuya Yoshikawa | ded5874 | 2016-02-22 17:23:40 +0900 | [diff] [blame] | 3615 | if (mmio_info_in_cache(vcpu, addr, direct)) |
Paolo Bonzini | 9b8ebbd | 2017-08-17 15:03:32 +0200 | [diff] [blame] | 3616 | return RET_PF_EMULATE; |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3617 | |
Ben Gardon | 95fb5b0 | 2020-10-14 11:26:58 -0700 | [diff] [blame] | 3618 | reserved = get_mmio_spte(vcpu, addr, &spte); |
Paolo Bonzini | 450869d | 2015-11-04 13:41:21 +0100 | [diff] [blame] | 3619 | if (WARN_ON(reserved)) |
Paolo Bonzini | 9b8ebbd | 2017-08-17 15:03:32 +0200 | [diff] [blame] | 3620 | return -EINVAL; |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3621 | |
| 3622 | if (is_mmio_spte(spte)) { |
| 3623 | gfn_t gfn = get_mmio_spte_gfn(spte); |
Ben Gardon | 0a2b64c | 2020-02-03 15:09:09 -0800 | [diff] [blame] | 3624 | unsigned int access = get_mmio_spte_access(spte); |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3625 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 3626 | if (!check_mmio_spte(vcpu, spte)) |
Paolo Bonzini | 9b8ebbd | 2017-08-17 15:03:32 +0200 | [diff] [blame] | 3627 | return RET_PF_INVALID; |
Xiao Guangrong | f8f5594 | 2013-06-07 16:51:26 +0800 | [diff] [blame] | 3628 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3629 | if (direct) |
| 3630 | addr = 0; |
Xiao Guangrong | 4f02264 | 2011-07-12 03:34:24 +0800 | [diff] [blame] | 3631 | |
| 3632 | trace_handle_mmio_page_fault(addr, gfn, access); |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3633 | vcpu_cache_mmio_info(vcpu, addr, gfn, access); |
Paolo Bonzini | 9b8ebbd | 2017-08-17 15:03:32 +0200 | [diff] [blame] | 3634 | return RET_PF_EMULATE; |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3635 | } |
| 3636 | |
| 3637 | /* |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3638 | * If the page table is zapped by other cpus, let CPU fault again on |
| 3639 | * the address. |
| 3640 | */ |
Paolo Bonzini | 9b8ebbd | 2017-08-17 15:03:32 +0200 | [diff] [blame] | 3641 | return RET_PF_RETRY; |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3642 | } |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3643 | |
Xiao Guangrong | 3d0c27a | 2016-02-24 17:51:11 +0800 | [diff] [blame] | 3644 | static 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 Guangrong | e5691a8 | 2016-02-24 17:51:12 +0800 | [diff] [blame] | 3664 | static 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 Guangrong | e5691a8 | 2016-02-24 17:51:12 +0800 | [diff] [blame] | 3669 | 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 Kuznetsov | e8c2226 | 2020-06-15 14:13:34 +0200 | [diff] [blame] | 3678 | static bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, |
| 3679 | gfn_t gfn) |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 3680 | { |
| 3681 | struct kvm_arch_async_pf arch; |
Xiao Guangrong | fb67e14 | 2010-12-07 10:35:25 +0800 | [diff] [blame] | 3682 | |
Gleb Natapov | 7c90705 | 2010-10-14 11:22:53 +0200 | [diff] [blame] | 3683 | arch.token = (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id; |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 3684 | arch.gfn = gfn; |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 3685 | arch.direct_map = vcpu->arch.mmu->direct_map; |
Sean Christopherson | d8dd54e | 2020-03-02 18:02:39 -0800 | [diff] [blame] | 3686 | arch.cr3 = vcpu->arch.mmu->get_guest_pgd(vcpu); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 3687 | |
Sean Christopherson | 9f1a852 | 2019-12-06 15:57:17 -0800 | [diff] [blame] | 3688 | return kvm_setup_async_pf(vcpu, cr2_or_gpa, |
| 3689 | kvm_vcpu_gfn_to_hva(vcpu, gfn), &arch); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 3690 | } |
| 3691 | |
Xiao Guangrong | 78b2c54 | 2010-12-07 10:48:06 +0800 | [diff] [blame] | 3692 | static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn, |
David Stevens | 4a42d84 | 2021-02-22 11:45:22 +0900 | [diff] [blame] | 3693 | gpa_t cr2_or_gpa, kvm_pfn_t *pfn, hva_t *hva, |
| 3694 | bool write, bool *writable) |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 3695 | { |
Paolo Bonzini | c36b715 | 2020-04-16 09:48:07 -0400 | [diff] [blame] | 3696 | struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 3697 | bool async; |
| 3698 | |
Sean Christopherson | e0c3786 | 2021-02-25 12:47:30 -0800 | [diff] [blame] | 3699 | /* |
| 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 Bonzini | c36b715 | 2020-04-16 09:48:07 -0400 | [diff] [blame] | 3707 | /* Don't expose private memslots to L2. */ |
| 3708 | if (is_guest_mode(vcpu) && !kvm_is_visible_memslot(slot)) { |
Jim Mattson | 3a2936d | 2018-05-09 17:02:05 -0400 | [diff] [blame] | 3709 | *pfn = KVM_PFN_NOSLOT; |
Sean Christopherson | c583eed | 2020-04-15 14:44:13 -0700 | [diff] [blame] | 3710 | *writable = false; |
Jim Mattson | 3a2936d | 2018-05-09 17:02:05 -0400 | [diff] [blame] | 3711 | return false; |
| 3712 | } |
| 3713 | |
Paolo Bonzini | 3520469 | 2015-04-02 11:20:48 +0200 | [diff] [blame] | 3714 | async = false; |
David Stevens | 4a42d84 | 2021-02-22 11:45:22 +0900 | [diff] [blame] | 3715 | *pfn = __gfn_to_pfn_memslot(slot, gfn, false, &async, |
| 3716 | write, writable, hva); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 3717 | if (!async) |
| 3718 | return false; /* *pfn has correct page already */ |
| 3719 | |
Wanpeng Li | 9bc1f09 | 2017-06-08 20:13:40 -0700 | [diff] [blame] | 3720 | if (!prefault && kvm_can_do_async_pf(vcpu)) { |
Sean Christopherson | 9f1a852 | 2019-12-06 15:57:17 -0800 | [diff] [blame] | 3721 | trace_kvm_try_async_get_page(cr2_or_gpa, gfn); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 3722 | if (kvm_find_async_pf_gfn(vcpu, gfn)) { |
Sean Christopherson | 9f1a852 | 2019-12-06 15:57:17 -0800 | [diff] [blame] | 3723 | trace_kvm_async_pf_doublefault(cr2_or_gpa, gfn); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 3724 | kvm_make_request(KVM_REQ_APF_HALT, vcpu); |
| 3725 | return true; |
Sean Christopherson | 9f1a852 | 2019-12-06 15:57:17 -0800 | [diff] [blame] | 3726 | } else if (kvm_arch_setup_async_pf(vcpu, cr2_or_gpa, gfn)) |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 3727 | return true; |
| 3728 | } |
| 3729 | |
David Stevens | 4a42d84 | 2021-02-22 11:45:22 +0900 | [diff] [blame] | 3730 | *pfn = __gfn_to_pfn_memslot(slot, gfn, false, NULL, |
| 3731 | write, writable, hva); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 3732 | return false; |
| 3733 | } |
| 3734 | |
Sean Christopherson | 0f90e1c | 2019-12-06 15:57:24 -0800 | [diff] [blame] | 3735 | static int direct_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, |
| 3736 | bool prefault, int max_level, bool is_tdp) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3737 | { |
Sean Christopherson | 367fd79 | 2019-12-06 15:57:16 -0800 | [diff] [blame] | 3738 | bool write = error_code & PFERR_WRITE_MASK; |
Sean Christopherson | 0f90e1c | 2019-12-06 15:57:24 -0800 | [diff] [blame] | 3739 | bool map_writable; |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 3740 | |
Sean Christopherson | 0f90e1c | 2019-12-06 15:57:24 -0800 | [diff] [blame] | 3741 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 3742 | unsigned long mmu_seq; |
| 3743 | kvm_pfn_t pfn; |
David Stevens | 4a42d84 | 2021-02-22 11:45:22 +0900 | [diff] [blame] | 3744 | hva_t hva; |
Sean Christopherson | 83f06fa | 2020-01-08 12:24:43 -0800 | [diff] [blame] | 3745 | int r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3746 | |
| 3747 | if (page_fault_handle_page_track(vcpu, error_code, gfn)) |
| 3748 | return RET_PF_EMULATE; |
| 3749 | |
Ben Gardon | bb18842 | 2020-10-14 11:26:50 -0700 | [diff] [blame] | 3750 | 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 Christopherson | 8329144 | 2020-07-02 19:35:30 -0700 | [diff] [blame] | 3755 | |
Sean Christopherson | 378f5cd | 2020-07-02 19:35:36 -0700 | [diff] [blame] | 3756 | r = mmu_topup_memory_caches(vcpu, false); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3757 | if (r) |
| 3758 | return r; |
| 3759 | |
Sean Christopherson | 367fd79 | 2019-12-06 15:57:16 -0800 | [diff] [blame] | 3760 | mmu_seq = vcpu->kvm->mmu_notifier_seq; |
| 3761 | smp_rmb(); |
| 3762 | |
David Stevens | 4a42d84 | 2021-02-22 11:45:22 +0900 | [diff] [blame] | 3763 | if (try_async_pf(vcpu, prefault, gfn, gpa, &pfn, &hva, |
| 3764 | write, &map_writable)) |
Sean Christopherson | 367fd79 | 2019-12-06 15:57:16 -0800 | [diff] [blame] | 3765 | return RET_PF_RETRY; |
| 3766 | |
Sean Christopherson | 0f90e1c | 2019-12-06 15:57:24 -0800 | [diff] [blame] | 3767 | if (handle_abnormal_pfn(vcpu, is_tdp ? 0 : gpa, gfn, pfn, ACC_ALL, &r)) |
Sean Christopherson | 367fd79 | 2019-12-06 15:57:16 -0800 | [diff] [blame] | 3768 | return r; |
| 3769 | |
| 3770 | r = RET_PF_RETRY; |
Ben Gardon | a2855af | 2021-02-02 10:57:29 -0800 | [diff] [blame] | 3771 | |
| 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 Stevens | 4a42d84 | 2021-02-22 11:45:22 +0900 | [diff] [blame] | 3777 | if (!is_noslot_pfn(pfn) && mmu_notifier_retry_hva(vcpu->kvm, mmu_seq, hva)) |
Sean Christopherson | 367fd79 | 2019-12-06 15:57:16 -0800 | [diff] [blame] | 3778 | goto out_unlock; |
Sean Christopherson | 7bd7ded | 2020-06-23 12:35:42 -0700 | [diff] [blame] | 3779 | r = make_mmu_pages_available(vcpu); |
| 3780 | if (r) |
Sean Christopherson | 367fd79 | 2019-12-06 15:57:16 -0800 | [diff] [blame] | 3781 | goto out_unlock; |
Ben Gardon | bb18842 | 2020-10-14 11:26:50 -0700 | [diff] [blame] | 3782 | |
| 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 Christopherson | 0f90e1c | 2019-12-06 15:57:24 -0800 | [diff] [blame] | 3789 | |
Sean Christopherson | 367fd79 | 2019-12-06 15:57:16 -0800 | [diff] [blame] | 3790 | out_unlock: |
Ben Gardon | a2855af | 2021-02-02 10:57:29 -0800 | [diff] [blame] | 3791 | 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 Christopherson | 367fd79 | 2019-12-06 15:57:16 -0800 | [diff] [blame] | 3795 | kvm_release_pfn_clean(pfn); |
| 3796 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3797 | } |
| 3798 | |
Sean Christopherson | 0f90e1c | 2019-12-06 15:57:24 -0800 | [diff] [blame] | 3799 | static 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 Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 3806 | PG_LEVEL_2M, false); |
Sean Christopherson | 0f90e1c | 2019-12-06 15:57:24 -0800 | [diff] [blame] | 3807 | } |
| 3808 | |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 3809 | int kvm_handle_page_fault(struct kvm_vcpu *vcpu, u64 error_code, |
Paolo Bonzini | d000653 | 2017-08-11 18:36:43 +0200 | [diff] [blame] | 3810 | u64 fault_address, char *insn, int insn_len) |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 3811 | { |
| 3812 | int r = 1; |
Vitaly Kuznetsov | 9ce372b | 2020-05-07 16:36:02 +0200 | [diff] [blame] | 3813 | u32 flags = vcpu->arch.apf.host_apf_flags; |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 3814 | |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 3815 | #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 Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 3821 | vcpu->arch.l1tf_flush_l1d = true; |
Vitaly Kuznetsov | 9ce372b | 2020-05-07 16:36:02 +0200 | [diff] [blame] | 3822 | if (!flags) { |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 3823 | trace_kvm_page_fault(fault_address, error_code); |
| 3824 | |
Paolo Bonzini | d000653 | 2017-08-11 18:36:43 +0200 | [diff] [blame] | 3825 | if (kvm_event_needs_reinjection(vcpu)) |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 3826 | kvm_mmu_unprotect_page_virt(vcpu, fault_address); |
| 3827 | r = kvm_mmu_page_fault(vcpu, fault_address, error_code, insn, |
| 3828 | insn_len); |
Vitaly Kuznetsov | 9ce372b | 2020-05-07 16:36:02 +0200 | [diff] [blame] | 3829 | } else if (flags & KVM_PV_REASON_PAGE_NOT_PRESENT) { |
Vitaly Kuznetsov | 68fd66f | 2020-05-25 16:41:17 +0200 | [diff] [blame] | 3830 | vcpu->arch.apf.host_apf_flags = 0; |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 3831 | local_irq_disable(); |
Thomas Gleixner | 6bca69a | 2020-03-07 00:42:06 +0100 | [diff] [blame] | 3832 | kvm_async_pf_task_wait_schedule(fault_address); |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 3833 | local_irq_enable(); |
Vitaly Kuznetsov | 9ce372b | 2020-05-07 16:36:02 +0200 | [diff] [blame] | 3834 | } else { |
| 3835 | WARN_ONCE(1, "Unexpected host async PF flags: %x\n", flags); |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 3836 | } |
Vitaly Kuznetsov | 9ce372b | 2020-05-07 16:36:02 +0200 | [diff] [blame] | 3837 | |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 3838 | return r; |
| 3839 | } |
| 3840 | EXPORT_SYMBOL_GPL(kvm_handle_page_fault); |
| 3841 | |
Sean Christopherson | 7a02674 | 2020-02-06 14:14:34 -0800 | [diff] [blame] | 3842 | int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, |
| 3843 | bool prefault) |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 3844 | { |
Sean Christopherson | cb9b88c | 2019-12-06 15:57:18 -0800 | [diff] [blame] | 3845 | int max_level; |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 3846 | |
Sean Christopherson | e662ec3 | 2020-04-27 17:54:21 -0700 | [diff] [blame] | 3847 | for (max_level = KVM_MAX_HUGEPAGE_LEVEL; |
Sean Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 3848 | max_level > PG_LEVEL_4K; |
Sean Christopherson | cb9b88c | 2019-12-06 15:57:18 -0800 | [diff] [blame] | 3849 | max_level--) { |
| 3850 | int page_num = KVM_PAGES_PER_HPAGE(max_level); |
Sean Christopherson | 0f90e1c | 2019-12-06 15:57:24 -0800 | [diff] [blame] | 3851 | gfn_t base = (gpa >> PAGE_SHIFT) & ~(page_num - 1); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 3852 | |
Sean Christopherson | cb9b88c | 2019-12-06 15:57:18 -0800 | [diff] [blame] | 3853 | if (kvm_mtrr_check_gfn_range_consistency(vcpu, base, page_num)) |
| 3854 | break; |
Takuya Yoshikawa | fd13690 | 2015-10-16 17:06:02 +0900 | [diff] [blame] | 3855 | } |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 3856 | |
Sean Christopherson | 0f90e1c | 2019-12-06 15:57:24 -0800 | [diff] [blame] | 3857 | return direct_page_fault(vcpu, gpa, error_code, prefault, |
| 3858 | max_level, true); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 3859 | } |
| 3860 | |
Paolo Bonzini | 8a3c1a33 | 2013-10-02 16:56:13 +0200 | [diff] [blame] | 3861 | static void nonpaging_init_context(struct kvm_vcpu *vcpu, |
| 3862 | struct kvm_mmu *context) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3863 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3864 | context->page_fault = nonpaging_page_fault; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3865 | context->gva_to_gpa = nonpaging_gva_to_gpa; |
Marcelo Tosatti | e8bc217 | 2008-09-23 13:18:33 -0300 | [diff] [blame] | 3866 | context->sync_page = nonpaging_sync_page; |
Paolo Bonzini | 5efac07 | 2020-03-23 20:42:57 -0400 | [diff] [blame] | 3867 | context->invlpg = NULL; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 3868 | context->root_level = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3869 | context->shadow_root_level = PT32E_ROOT_LEVEL; |
Joerg Roedel | c5a78f2b | 2010-09-10 17:30:39 +0200 | [diff] [blame] | 3870 | context->direct_map = true; |
Joerg Roedel | 2d48a98 | 2010-09-10 17:31:01 +0200 | [diff] [blame] | 3871 | context->nx = false; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3872 | } |
| 3873 | |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 3874 | static inline bool is_root_usable(struct kvm_mmu_root_info *root, gpa_t pgd, |
Sean Christopherson | 0be4435 | 2020-02-28 14:52:40 -0800 | [diff] [blame] | 3875 | union kvm_mmu_page_role role) |
| 3876 | { |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 3877 | return (role.direct || pgd == root->pgd) && |
Sean Christopherson | e47c4ae | 2020-06-22 13:20:34 -0700 | [diff] [blame] | 3878 | VALID_PAGE(root->hpa) && to_shadow_page(root->hpa) && |
| 3879 | role.word == to_shadow_page(root->hpa)->role.word; |
Sean Christopherson | 0be4435 | 2020-02-28 14:52:40 -0800 | [diff] [blame] | 3880 | } |
| 3881 | |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 3882 | /* |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 3883 | * Find out if a previously cached root matching the new pgd/role is available. |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 3884 | * 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 Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 3890 | static bool cached_root_available(struct kvm_vcpu *vcpu, gpa_t new_pgd, |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 3891 | union kvm_mmu_page_role new_role) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3892 | { |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 3893 | uint i; |
| 3894 | struct kvm_mmu_root_info root; |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 3895 | struct kvm_mmu *mmu = vcpu->arch.mmu; |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 3896 | |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 3897 | root.pgd = mmu->root_pgd; |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 3898 | root.hpa = mmu->root_hpa; |
| 3899 | |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 3900 | if (is_root_usable(&root, new_pgd, new_role)) |
Sean Christopherson | 0be4435 | 2020-02-28 14:52:40 -0800 | [diff] [blame] | 3901 | return true; |
| 3902 | |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 3903 | for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) { |
| 3904 | swap(root, mmu->prev_roots[i]); |
| 3905 | |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 3906 | if (is_root_usable(&root, new_pgd, new_role)) |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 3907 | break; |
| 3908 | } |
| 3909 | |
| 3910 | mmu->root_hpa = root.hpa; |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 3911 | mmu->root_pgd = root.pgd; |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 3912 | |
| 3913 | return i < KVM_MMU_NUM_PREV_ROOTS; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3914 | } |
| 3915 | |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 3916 | static bool fast_pgd_switch(struct kvm_vcpu *vcpu, gpa_t new_pgd, |
Sean Christopherson | b869855 | 2020-03-20 14:28:26 -0700 | [diff] [blame] | 3917 | union kvm_mmu_page_role new_role) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3918 | { |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 3919 | struct kvm_mmu *mmu = vcpu->arch.mmu; |
Junaid Shahid | 7c390d3 | 2018-06-27 14:59:06 -0700 | [diff] [blame] | 3920 | |
| 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 Christopherson | b869855 | 2020-03-20 14:28:26 -0700 | [diff] [blame] | 3927 | mmu->root_level >= PT64_ROOT_4LEVEL) |
Vitaly Kuznetsov | fe9304d | 2020-07-10 16:11:57 +0200 | [diff] [blame] | 3928 | return cached_root_available(vcpu, new_pgd, new_role); |
Junaid Shahid | 7c390d3 | 2018-06-27 14:59:06 -0700 | [diff] [blame] | 3929 | |
| 3930 | return false; |
| 3931 | } |
| 3932 | |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 3933 | static void __kvm_mmu_new_pgd(struct kvm_vcpu *vcpu, gpa_t new_pgd, |
Junaid Shahid | ade61e2 | 2018-06-27 14:59:15 -0700 | [diff] [blame] | 3934 | union kvm_mmu_page_role new_role, |
Sean Christopherson | 4a632ac | 2020-03-20 14:28:27 -0700 | [diff] [blame] | 3935 | bool skip_tlb_flush, bool skip_mmu_sync) |
Junaid Shahid | 0aab33e | 2018-06-27 14:59:09 -0700 | [diff] [blame] | 3936 | { |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 3937 | if (!fast_pgd_switch(vcpu, new_pgd, new_role)) { |
Sean Christopherson | b869855 | 2020-03-20 14:28:26 -0700 | [diff] [blame] | 3938 | 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 Christopherson | 71fe701 | 2020-03-20 14:28:28 -0700 | [diff] [blame] | 3950 | if (!skip_mmu_sync || force_flush_and_sync_on_reuse) |
Sean Christopherson | b869855 | 2020-03-20 14:28:26 -0700 | [diff] [blame] | 3951 | kvm_make_request(KVM_REQ_MMU_SYNC, vcpu); |
Sean Christopherson | 71fe701 | 2020-03-20 14:28:28 -0700 | [diff] [blame] | 3952 | if (!skip_tlb_flush || force_flush_and_sync_on_reuse) |
Sean Christopherson | b869855 | 2020-03-20 14:28:26 -0700 | [diff] [blame] | 3953 | kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); |
Sean Christopherson | b869855 | 2020-03-20 14:28:26 -0700 | [diff] [blame] | 3954 | |
| 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 Gardon | daa5b6c | 2020-10-14 11:26:59 -0700 | [diff] [blame] | 3963 | /* |
| 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 Shahid | 0aab33e | 2018-06-27 14:59:09 -0700 | [diff] [blame] | 3970 | } |
| 3971 | |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 3972 | void kvm_mmu_new_pgd(struct kvm_vcpu *vcpu, gpa_t new_pgd, bool skip_tlb_flush, |
Sean Christopherson | 4a632ac | 2020-03-20 14:28:27 -0700 | [diff] [blame] | 3973 | bool skip_mmu_sync) |
Junaid Shahid | 7c390d3 | 2018-06-27 14:59:06 -0700 | [diff] [blame] | 3974 | { |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 3975 | __kvm_mmu_new_pgd(vcpu, new_pgd, kvm_mmu_calc_root_page_role(vcpu), |
Sean Christopherson | 4a632ac | 2020-03-20 14:28:27 -0700 | [diff] [blame] | 3976 | skip_tlb_flush, skip_mmu_sync); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3977 | } |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 3978 | EXPORT_SYMBOL_GPL(kvm_mmu_new_pgd); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3979 | |
Joerg Roedel | 5777ed3 | 2010-09-10 17:30:42 +0200 | [diff] [blame] | 3980 | static unsigned long get_cr3(struct kvm_vcpu *vcpu) |
| 3981 | { |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 3982 | return kvm_read_cr3(vcpu); |
Joerg Roedel | 5777ed3 | 2010-09-10 17:30:42 +0200 | [diff] [blame] | 3983 | } |
| 3984 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 3985 | static bool sync_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, gfn_t gfn, |
Ben Gardon | 0a2b64c | 2020-02-03 15:09:09 -0800 | [diff] [blame] | 3986 | unsigned int access, int *nr_present) |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3987 | { |
| 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 Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 3995 | mark_mmio_spte(vcpu, sptep, gfn, access); |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3996 | return true; |
| 3997 | } |
| 3998 | |
| 3999 | return false; |
| 4000 | } |
| 4001 | |
Paolo Bonzini | 6bb69c9 | 2016-02-23 12:51:19 +0100 | [diff] [blame] | 4002 | static inline bool is_last_gpte(struct kvm_mmu *mmu, |
| 4003 | unsigned level, unsigned gpte) |
Avi Kivity | 6fd01b7 | 2012-09-12 20:46:56 +0300 | [diff] [blame] | 4004 | { |
Paolo Bonzini | 6bb69c9 | 2016-02-23 12:51:19 +0100 | [diff] [blame] | 4005 | /* |
Paolo Bonzini | 6bb69c9 | 2016-02-23 12:51:19 +0100 | [diff] [blame] | 4006 | * 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 Prosek | 829ee27 | 2017-10-05 11:10:23 +0200 | [diff] [blame] | 4012 | /* |
Sean Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 4013 | * 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 Prosek | 829ee27 | 2017-10-05 11:10:23 +0200 | [diff] [blame] | 4016 | */ |
Sean Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 4017 | gpte |= level - PG_LEVEL_4K - 1; |
Ladi Prosek | 829ee27 | 2017-10-05 11:10:23 +0200 | [diff] [blame] | 4018 | |
Paolo Bonzini | 6bb69c9 | 2016-02-23 12:51:19 +0100 | [diff] [blame] | 4019 | return gpte & PT_PAGE_SIZE_MASK; |
Avi Kivity | 6fd01b7 | 2012-09-12 20:46:56 +0300 | [diff] [blame] | 4020 | } |
| 4021 | |
Nadav Har'El | 37406aa | 2013-08-05 11:07:12 +0300 | [diff] [blame] | 4022 | #define PTTYPE_EPT 18 /* arbitrary */ |
| 4023 | #define PTTYPE PTTYPE_EPT |
| 4024 | #include "paging_tmpl.h" |
| 4025 | #undef PTTYPE |
| 4026 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4027 | #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 Guangrong | 6dc98b8 | 2015-08-05 12:04:22 +0800 | [diff] [blame] | 4035 | static void |
| 4036 | __reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, |
| 4037 | struct rsvd_bits_validate *rsvd_check, |
Sean Christopherson | 5b7f575 | 2021-02-03 16:01:13 -0800 | [diff] [blame] | 4038 | u64 pa_bits_rsvd, int level, bool nx, bool gbpages, |
Paolo Bonzini | 6fec214 | 2015-09-22 23:02:14 +0200 | [diff] [blame] | 4039 | bool pse, bool amd) |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 4040 | { |
Nadav Amit | 5f7dde7 | 2014-05-07 15:32:50 +0300 | [diff] [blame] | 4041 | u64 gbpages_bit_rsvd = 0; |
Paolo Bonzini | a0c0feb | 2014-09-02 13:24:12 +0200 | [diff] [blame] | 4042 | u64 nonleaf_bit8_rsvd = 0; |
Sean Christopherson | 5b7f575 | 2021-02-03 16:01:13 -0800 | [diff] [blame] | 4043 | u64 high_bits_rsvd; |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 4044 | |
Xiao Guangrong | a0a64f5 | 2015-08-05 12:04:21 +0800 | [diff] [blame] | 4045 | rsvd_check->bad_mt_xwr = 0; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 4046 | |
Xiao Guangrong | 6dc98b8 | 2015-08-05 12:04:22 +0800 | [diff] [blame] | 4047 | if (!gbpages) |
Nadav Amit | 5f7dde7 | 2014-05-07 15:32:50 +0300 | [diff] [blame] | 4048 | gbpages_bit_rsvd = rsvd_bits(7, 7); |
Paolo Bonzini | a0c0feb | 2014-09-02 13:24:12 +0200 | [diff] [blame] | 4049 | |
Sean Christopherson | 5b7f575 | 2021-02-03 16:01:13 -0800 | [diff] [blame] | 4050 | 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 Bonzini | a0c0feb | 2014-09-02 13:24:12 +0200 | [diff] [blame] | 4059 | /* |
| 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 Bonzini | 6fec214 | 2015-09-22 23:02:14 +0200 | [diff] [blame] | 4063 | if (amd) |
Paolo Bonzini | a0c0feb | 2014-09-02 13:24:12 +0200 | [diff] [blame] | 4064 | nonleaf_bit8_rsvd = rsvd_bits(8, 8); |
| 4065 | |
Xiao Guangrong | 6dc98b8 | 2015-08-05 12:04:22 +0800 | [diff] [blame] | 4066 | switch (level) { |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 4067 | case PT32_ROOT_LEVEL: |
| 4068 | /* no rsvd bits for 2 level 4K page table entries */ |
Xiao Guangrong | a0a64f5 | 2015-08-05 12:04:21 +0800 | [diff] [blame] | 4069 | 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 Guangrong | f815bce | 2010-03-19 17:58:53 +0800 | [diff] [blame] | 4073 | |
Xiao Guangrong | 6dc98b8 | 2015-08-05 12:04:22 +0800 | [diff] [blame] | 4074 | if (!pse) { |
Xiao Guangrong | a0a64f5 | 2015-08-05 12:04:21 +0800 | [diff] [blame] | 4075 | rsvd_check->rsvd_bits_mask[1][1] = 0; |
Xiao Guangrong | f815bce | 2010-03-19 17:58:53 +0800 | [diff] [blame] | 4076 | break; |
| 4077 | } |
| 4078 | |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 4079 | if (is_cpuid_PSE36()) |
| 4080 | /* 36bits PSE 4MB page */ |
Xiao Guangrong | a0a64f5 | 2015-08-05 12:04:21 +0800 | [diff] [blame] | 4081 | rsvd_check->rsvd_bits_mask[1][1] = rsvd_bits(17, 21); |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 4082 | else |
| 4083 | /* 32 bits PSE 4MB page */ |
Xiao Guangrong | a0a64f5 | 2015-08-05 12:04:21 +0800 | [diff] [blame] | 4084 | rsvd_check->rsvd_bits_mask[1][1] = rsvd_bits(13, 21); |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 4085 | break; |
| 4086 | case PT32E_ROOT_LEVEL: |
Sean Christopherson | 5b7f575 | 2021-02-03 16:01:13 -0800 | [diff] [blame] | 4087 | 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 Guangrong | a0a64f5 | 2015-08-05 12:04:21 +0800 | [diff] [blame] | 4095 | rsvd_check->rsvd_bits_mask[1][0] = |
| 4096 | rsvd_check->rsvd_bits_mask[0][0]; |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 4097 | break; |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 4098 | case PT64_ROOT_5LEVEL: |
Sean Christopherson | 5b7f575 | 2021-02-03 16:01:13 -0800 | [diff] [blame] | 4099 | rsvd_check->rsvd_bits_mask[0][4] = high_bits_rsvd | |
| 4100 | nonleaf_bit8_rsvd | |
| 4101 | rsvd_bits(7, 7); |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 4102 | rsvd_check->rsvd_bits_mask[1][4] = |
| 4103 | rsvd_check->rsvd_bits_mask[0][4]; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 4104 | fallthrough; |
Yu Zhang | 2a7266a | 2017-08-24 20:27:54 +0800 | [diff] [blame] | 4105 | case PT64_ROOT_4LEVEL: |
Sean Christopherson | 5b7f575 | 2021-02-03 16:01:13 -0800 | [diff] [blame] | 4106 | 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 Guangrong | a0a64f5 | 2015-08-05 12:04:21 +0800 | [diff] [blame] | 4113 | rsvd_check->rsvd_bits_mask[1][3] = |
| 4114 | rsvd_check->rsvd_bits_mask[0][3]; |
Sean Christopherson | 5b7f575 | 2021-02-03 16:01:13 -0800 | [diff] [blame] | 4115 | 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 Guangrong | a0a64f5 | 2015-08-05 12:04:21 +0800 | [diff] [blame] | 4120 | rsvd_check->rsvd_bits_mask[1][0] = |
| 4121 | rsvd_check->rsvd_bits_mask[0][0]; |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 4122 | break; |
| 4123 | } |
| 4124 | } |
| 4125 | |
Xiao Guangrong | 6dc98b8 | 2015-08-05 12:04:22 +0800 | [diff] [blame] | 4126 | static 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 Christopherson | 5b7f575 | 2021-02-03 16:01:13 -0800 | [diff] [blame] | 4130 | vcpu->arch.reserved_gpa_bits, |
| 4131 | context->root_level, context->nx, |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 4132 | guest_cpuid_has(vcpu, X86_FEATURE_GBPAGES), |
Sean Christopherson | 23493d0 | 2020-03-04 17:34:33 -0800 | [diff] [blame] | 4133 | is_pse(vcpu), |
| 4134 | guest_cpuid_is_amd_or_hygon(vcpu)); |
Xiao Guangrong | 6dc98b8 | 2015-08-05 12:04:22 +0800 | [diff] [blame] | 4135 | } |
| 4136 | |
Xiao Guangrong | 81b8eeb | 2015-08-05 12:04:23 +0800 | [diff] [blame] | 4137 | static void |
| 4138 | __reset_rsvds_bits_mask_ept(struct rsvd_bits_validate *rsvd_check, |
Sean Christopherson | 5b7f575 | 2021-02-03 16:01:13 -0800 | [diff] [blame] | 4139 | u64 pa_bits_rsvd, bool execonly) |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 4140 | { |
Sean Christopherson | 5b7f575 | 2021-02-03 16:01:13 -0800 | [diff] [blame] | 4141 | u64 high_bits_rsvd = pa_bits_rsvd & rsvd_bits(0, 51); |
Paolo Bonzini | 951f9fd | 2015-09-23 10:34:26 +0200 | [diff] [blame] | 4142 | u64 bad_mt_xwr; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 4143 | |
Sean Christopherson | 5b7f575 | 2021-02-03 16:01:13 -0800 | [diff] [blame] | 4144 | 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 Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 4149 | |
| 4150 | /* large page */ |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 4151 | rsvd_check->rsvd_bits_mask[1][4] = rsvd_check->rsvd_bits_mask[0][4]; |
Xiao Guangrong | a0a64f5 | 2015-08-05 12:04:21 +0800 | [diff] [blame] | 4152 | rsvd_check->rsvd_bits_mask[1][3] = rsvd_check->rsvd_bits_mask[0][3]; |
Sean Christopherson | 5b7f575 | 2021-02-03 16:01:13 -0800 | [diff] [blame] | 4153 | 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 Guangrong | a0a64f5 | 2015-08-05 12:04:21 +0800 | [diff] [blame] | 4155 | rsvd_check->rsvd_bits_mask[1][0] = rsvd_check->rsvd_bits_mask[0][0]; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 4156 | |
Paolo Bonzini | 951f9fd | 2015-09-23 10:34:26 +0200 | [diff] [blame] | 4157 | 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 Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 4165 | } |
Paolo Bonzini | 951f9fd | 2015-09-23 10:34:26 +0200 | [diff] [blame] | 4166 | rsvd_check->bad_mt_xwr = bad_mt_xwr; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 4167 | } |
| 4168 | |
Xiao Guangrong | 81b8eeb | 2015-08-05 12:04:23 +0800 | [diff] [blame] | 4169 | static 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 Christopherson | 5b7f575 | 2021-02-03 16:01:13 -0800 | [diff] [blame] | 4173 | vcpu->arch.reserved_gpa_bits, execonly); |
Xiao Guangrong | 81b8eeb | 2015-08-05 12:04:23 +0800 | [diff] [blame] | 4174 | } |
| 4175 | |
Sean Christopherson | 6f8e65a | 2021-02-03 16:01:14 -0800 | [diff] [blame] | 4176 | static inline u64 reserved_hpa_bits(void) |
| 4177 | { |
| 4178 | return rsvd_bits(shadow_phys_bits, 63); |
| 4179 | } |
| 4180 | |
Xiao Guangrong | c258b62 | 2015-08-05 12:04:24 +0800 | [diff] [blame] | 4181 | /* |
| 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 | */ |
| 4186 | void |
| 4187 | reset_shadow_zero_bits_mask(struct kvm_vcpu *vcpu, struct kvm_mmu *context) |
| 4188 | { |
Vitaly Kuznetsov | 36d9594d | 2018-10-08 21:28:10 +0200 | [diff] [blame] | 4189 | bool uses_nx = context->nx || |
| 4190 | context->mmu_role.base.smep_andnot_wp; |
Brijesh Singh | ea2800d | 2017-08-25 15:55:40 -0500 | [diff] [blame] | 4191 | struct rsvd_bits_validate *shadow_zero_check; |
| 4192 | int i; |
Paolo Bonzini | 5f0b819 | 2016-03-09 14:28:02 +0100 | [diff] [blame] | 4193 | |
Paolo Bonzini | 6fec214 | 2015-09-22 23:02:14 +0200 | [diff] [blame] | 4194 | /* |
| 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 Singh | ea2800d | 2017-08-25 15:55:40 -0500 | [diff] [blame] | 4198 | shadow_zero_check = &context->shadow_zero_check; |
| 4199 | __reset_rsvds_bits_mask(vcpu, shadow_zero_check, |
Sean Christopherson | 6f8e65a | 2021-02-03 16:01:14 -0800 | [diff] [blame] | 4200 | reserved_hpa_bits(), |
Paolo Bonzini | 5f0b819 | 2016-03-09 14:28:02 +0100 | [diff] [blame] | 4201 | context->shadow_root_level, uses_nx, |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 4202 | guest_cpuid_has(vcpu, X86_FEATURE_GBPAGES), |
| 4203 | is_pse(vcpu), true); |
Brijesh Singh | ea2800d | 2017-08-25 15:55:40 -0500 | [diff] [blame] | 4204 | |
| 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 Guangrong | c258b62 | 2015-08-05 12:04:24 +0800 | [diff] [blame] | 4213 | } |
| 4214 | EXPORT_SYMBOL_GPL(reset_shadow_zero_bits_mask); |
| 4215 | |
Paolo Bonzini | 6fec214 | 2015-09-22 23:02:14 +0200 | [diff] [blame] | 4216 | static inline bool boot_cpu_is_amd(void) |
| 4217 | { |
| 4218 | WARN_ON_ONCE(!tdp_enabled); |
| 4219 | return shadow_x_mask == 0; |
| 4220 | } |
| 4221 | |
Xiao Guangrong | c258b62 | 2015-08-05 12:04:24 +0800 | [diff] [blame] | 4222 | /* |
| 4223 | * the direct page table on host, use as much mmu features as |
| 4224 | * possible, however, kvm currently does not do execution-protection. |
| 4225 | */ |
| 4226 | static void |
| 4227 | reset_tdp_shadow_zero_bits_mask(struct kvm_vcpu *vcpu, |
| 4228 | struct kvm_mmu *context) |
| 4229 | { |
Brijesh Singh | ea2800d | 2017-08-25 15:55:40 -0500 | [diff] [blame] | 4230 | struct rsvd_bits_validate *shadow_zero_check; |
| 4231 | int i; |
| 4232 | |
| 4233 | shadow_zero_check = &context->shadow_zero_check; |
| 4234 | |
Paolo Bonzini | 6fec214 | 2015-09-22 23:02:14 +0200 | [diff] [blame] | 4235 | if (boot_cpu_is_amd()) |
Brijesh Singh | ea2800d | 2017-08-25 15:55:40 -0500 | [diff] [blame] | 4236 | __reset_rsvds_bits_mask(vcpu, shadow_zero_check, |
Sean Christopherson | 6f8e65a | 2021-02-03 16:01:14 -0800 | [diff] [blame] | 4237 | reserved_hpa_bits(), |
Xiao Guangrong | c258b62 | 2015-08-05 12:04:24 +0800 | [diff] [blame] | 4238 | context->shadow_root_level, false, |
Borislav Petkov | b8291adc | 2016-03-29 17:41:58 +0200 | [diff] [blame] | 4239 | boot_cpu_has(X86_FEATURE_GBPAGES), |
| 4240 | true, true); |
Xiao Guangrong | c258b62 | 2015-08-05 12:04:24 +0800 | [diff] [blame] | 4241 | else |
Brijesh Singh | ea2800d | 2017-08-25 15:55:40 -0500 | [diff] [blame] | 4242 | __reset_rsvds_bits_mask_ept(shadow_zero_check, |
Sean Christopherson | 6f8e65a | 2021-02-03 16:01:14 -0800 | [diff] [blame] | 4243 | reserved_hpa_bits(), false); |
Xiao Guangrong | c258b62 | 2015-08-05 12:04:24 +0800 | [diff] [blame] | 4244 | |
Brijesh Singh | ea2800d | 2017-08-25 15:55:40 -0500 | [diff] [blame] | 4245 | 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 Guangrong | c258b62 | 2015-08-05 12:04:24 +0800 | [diff] [blame] | 4252 | } |
| 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 | */ |
| 4258 | static void |
| 4259 | reset_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 Christopherson | 6f8e65a | 2021-02-03 16:01:14 -0800 | [diff] [blame] | 4263 | reserved_hpa_bits(), execonly); |
Xiao Guangrong | c258b62 | 2015-08-05 12:04:24 +0800 | [diff] [blame] | 4264 | } |
| 4265 | |
Paolo Bonzini | 09f037a | 2017-08-24 17:37:25 +0200 | [diff] [blame] | 4266 | #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 Guangrong | edc90b7 | 2015-05-11 22:55:21 +0800 | [diff] [blame] | 4276 | static void update_permission_bitmask(struct kvm_vcpu *vcpu, |
| 4277 | struct kvm_mmu *mmu, bool ept) |
Avi Kivity | 97d64b7 | 2012-09-12 14:52:00 +0300 | [diff] [blame] | 4278 | { |
Paolo Bonzini | 09f037a | 2017-08-24 17:37:25 +0200 | [diff] [blame] | 4279 | unsigned byte; |
Avi Kivity | 97d64b7 | 2012-09-12 14:52:00 +0300 | [diff] [blame] | 4280 | |
Paolo Bonzini | 09f037a | 2017-08-24 17:37:25 +0200 | [diff] [blame] | 4281 | 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 Kivity | 97d64b7 | 2012-09-12 14:52:00 +0300 | [diff] [blame] | 4289 | for (byte = 0; byte < ARRAY_SIZE(mmu->permissions); ++byte) { |
Paolo Bonzini | 09f037a | 2017-08-24 17:37:25 +0200 | [diff] [blame] | 4290 | unsigned pfec = byte << 1; |
| 4291 | |
Feng Wu | 97ec8c0 | 2014-04-01 17:46:34 +0800 | [diff] [blame] | 4292 | /* |
Paolo Bonzini | 09f037a | 2017-08-24 17:37:25 +0200 | [diff] [blame] | 4293 | * Each "*f" variable has a 1 bit for each UWX value |
| 4294 | * that causes a fault with the given PFEC. |
Feng Wu | 97ec8c0 | 2014-04-01 17:46:34 +0800 | [diff] [blame] | 4295 | */ |
Avi Kivity | 97d64b7 | 2012-09-12 14:52:00 +0300 | [diff] [blame] | 4296 | |
Paolo Bonzini | 09f037a | 2017-08-24 17:37:25 +0200 | [diff] [blame] | 4297 | /* Faults from writes to non-writable pages */ |
Arnd Bergmann | a6a6d3b | 2019-07-12 11:12:30 +0200 | [diff] [blame] | 4298 | u8 wf = (pfec & PFERR_WRITE_MASK) ? (u8)~w : 0; |
Paolo Bonzini | 09f037a | 2017-08-24 17:37:25 +0200 | [diff] [blame] | 4299 | /* Faults from user mode accesses to supervisor pages */ |
Arnd Bergmann | a6a6d3b | 2019-07-12 11:12:30 +0200 | [diff] [blame] | 4300 | u8 uf = (pfec & PFERR_USER_MASK) ? (u8)~u : 0; |
Paolo Bonzini | 09f037a | 2017-08-24 17:37:25 +0200 | [diff] [blame] | 4301 | /* Faults from fetches of non-executable pages*/ |
Arnd Bergmann | a6a6d3b | 2019-07-12 11:12:30 +0200 | [diff] [blame] | 4302 | u8 ff = (pfec & PFERR_FETCH_MASK) ? (u8)~x : 0; |
Paolo Bonzini | 09f037a | 2017-08-24 17:37:25 +0200 | [diff] [blame] | 4303 | /* 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 Wu | 97ec8c0 | 2014-04-01 17:46:34 +0800 | [diff] [blame] | 4307 | |
Paolo Bonzini | 09f037a | 2017-08-24 17:37:25 +0200 | [diff] [blame] | 4308 | if (!ept) { |
| 4309 | /* Faults from kernel mode accesses to user pages */ |
| 4310 | u8 kf = (pfec & PFERR_USER_MASK) ? 0 : u; |
Avi Kivity | 97d64b7 | 2012-09-12 14:52:00 +0300 | [diff] [blame] | 4311 | |
Paolo Bonzini | 09f037a | 2017-08-24 17:37:25 +0200 | [diff] [blame] | 4312 | /* 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 Hao | 39337ad | 2018-10-04 11:45:00 -0400 | [diff] [blame] | 4328 | * conditions are true: |
Paolo Bonzini | 09f037a | 2017-08-24 17:37:25 +0200 | [diff] [blame] | 4329 | * - 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 Kivity | 97d64b7 | 2012-09-12 14:52:00 +0300 | [diff] [blame] | 4342 | } |
Paolo Bonzini | 09f037a | 2017-08-24 17:37:25 +0200 | [diff] [blame] | 4343 | |
| 4344 | mmu->permissions[byte] = ff | uf | wf | smepf | smapf; |
Avi Kivity | 97d64b7 | 2012-09-12 14:52:00 +0300 | [diff] [blame] | 4345 | } |
| 4346 | } |
| 4347 | |
Huaitong Han | 2d34410 | 2016-03-22 16:51:19 +0800 | [diff] [blame] | 4348 | /* |
| 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 | */ |
| 4372 | static 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 Bonzini | 6bb69c9 | 2016-02-23 12:51:19 +0100 | [diff] [blame] | 4423 | static void update_last_nonleaf_level(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu) |
Avi Kivity | 6fd01b7 | 2012-09-12 20:46:56 +0300 | [diff] [blame] | 4424 | { |
Paolo Bonzini | 6bb69c9 | 2016-02-23 12:51:19 +0100 | [diff] [blame] | 4425 | unsigned root_level = mmu->root_level; |
Avi Kivity | 6fd01b7 | 2012-09-12 20:46:56 +0300 | [diff] [blame] | 4426 | |
Paolo Bonzini | 6bb69c9 | 2016-02-23 12:51:19 +0100 | [diff] [blame] | 4427 | mmu->last_nonleaf_level = root_level; |
| 4428 | if (root_level == PT32_ROOT_LEVEL && is_pse(vcpu)) |
| 4429 | mmu->last_nonleaf_level++; |
Avi Kivity | 6fd01b7 | 2012-09-12 20:46:56 +0300 | [diff] [blame] | 4430 | } |
| 4431 | |
Paolo Bonzini | 8a3c1a33 | 2013-10-02 16:56:13 +0200 | [diff] [blame] | 4432 | static void paging64_init_context_common(struct kvm_vcpu *vcpu, |
| 4433 | struct kvm_mmu *context, |
| 4434 | int level) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4435 | { |
Joerg Roedel | 2d48a98 | 2010-09-10 17:31:01 +0200 | [diff] [blame] | 4436 | context->nx = is_nx(vcpu); |
Davidlohr Bueso | 4d6931c | 2012-03-05 16:53:06 +0100 | [diff] [blame] | 4437 | context->root_level = level; |
Joerg Roedel | 2d48a98 | 2010-09-10 17:31:01 +0200 | [diff] [blame] | 4438 | |
Davidlohr Bueso | 4d6931c | 2012-03-05 16:53:06 +0100 | [diff] [blame] | 4439 | reset_rsvds_bits_mask(vcpu, context); |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 4440 | update_permission_bitmask(vcpu, context, false); |
Huaitong Han | 2d34410 | 2016-03-22 16:51:19 +0800 | [diff] [blame] | 4441 | update_pkru_bitmask(vcpu, context, false); |
Paolo Bonzini | 6bb69c9 | 2016-02-23 12:51:19 +0100 | [diff] [blame] | 4442 | update_last_nonleaf_level(vcpu, context); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4443 | |
Paolo Bonzini | fa4a2c0 | 2013-10-02 16:56:16 +0200 | [diff] [blame] | 4444 | MMU_WARN_ON(!is_pae(vcpu)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4445 | context->page_fault = paging64_page_fault; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4446 | context->gva_to_gpa = paging64_gva_to_gpa; |
Marcelo Tosatti | e8bc217 | 2008-09-23 13:18:33 -0300 | [diff] [blame] | 4447 | context->sync_page = paging64_sync_page; |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4448 | context->invlpg = paging64_invlpg; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 4449 | context->shadow_root_level = level; |
Joerg Roedel | c5a78f2b | 2010-09-10 17:30:39 +0200 | [diff] [blame] | 4450 | context->direct_map = false; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4451 | } |
| 4452 | |
Paolo Bonzini | 8a3c1a33 | 2013-10-02 16:56:13 +0200 | [diff] [blame] | 4453 | static void paging64_init_context(struct kvm_vcpu *vcpu, |
| 4454 | struct kvm_mmu *context) |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 4455 | { |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 4456 | 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 Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 4460 | } |
| 4461 | |
Paolo Bonzini | 8a3c1a33 | 2013-10-02 16:56:13 +0200 | [diff] [blame] | 4462 | static void paging32_init_context(struct kvm_vcpu *vcpu, |
| 4463 | struct kvm_mmu *context) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4464 | { |
Joerg Roedel | 2d48a98 | 2010-09-10 17:31:01 +0200 | [diff] [blame] | 4465 | context->nx = false; |
Davidlohr Bueso | 4d6931c | 2012-03-05 16:53:06 +0100 | [diff] [blame] | 4466 | context->root_level = PT32_ROOT_LEVEL; |
Joerg Roedel | 2d48a98 | 2010-09-10 17:31:01 +0200 | [diff] [blame] | 4467 | |
Davidlohr Bueso | 4d6931c | 2012-03-05 16:53:06 +0100 | [diff] [blame] | 4468 | reset_rsvds_bits_mask(vcpu, context); |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 4469 | update_permission_bitmask(vcpu, context, false); |
Huaitong Han | 2d34410 | 2016-03-22 16:51:19 +0800 | [diff] [blame] | 4470 | update_pkru_bitmask(vcpu, context, false); |
Paolo Bonzini | 6bb69c9 | 2016-02-23 12:51:19 +0100 | [diff] [blame] | 4471 | update_last_nonleaf_level(vcpu, context); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4472 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4473 | context->page_fault = paging32_page_fault; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4474 | context->gva_to_gpa = paging32_gva_to_gpa; |
Marcelo Tosatti | e8bc217 | 2008-09-23 13:18:33 -0300 | [diff] [blame] | 4475 | context->sync_page = paging32_sync_page; |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4476 | context->invlpg = paging32_invlpg; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4477 | context->shadow_root_level = PT32E_ROOT_LEVEL; |
Joerg Roedel | c5a78f2b | 2010-09-10 17:30:39 +0200 | [diff] [blame] | 4478 | context->direct_map = false; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4479 | } |
| 4480 | |
Paolo Bonzini | 8a3c1a33 | 2013-10-02 16:56:13 +0200 | [diff] [blame] | 4481 | static void paging32E_init_context(struct kvm_vcpu *vcpu, |
| 4482 | struct kvm_mmu *context) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4483 | { |
Paolo Bonzini | 8a3c1a33 | 2013-10-02 16:56:13 +0200 | [diff] [blame] | 4484 | paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4485 | } |
| 4486 | |
Vitaly Kuznetsov | a336282 | 2018-10-08 21:28:11 +0200 | [diff] [blame] | 4487 | static union kvm_mmu_extended_role kvm_calc_mmu_role_ext(struct kvm_vcpu *vcpu) |
Junaid Shahid | 9fa7211 | 2018-06-27 14:59:07 -0700 | [diff] [blame] | 4488 | { |
Vitaly Kuznetsov | a336282 | 2018-10-08 21:28:11 +0200 | [diff] [blame] | 4489 | union kvm_mmu_extended_role ext = {0}; |
Junaid Shahid | 9fa7211 | 2018-06-27 14:59:07 -0700 | [diff] [blame] | 4490 | |
Vitaly Kuznetsov | 7dcd575 | 2018-10-08 21:28:12 +0200 | [diff] [blame] | 4491 | ext.cr0_pg = !!is_paging(vcpu); |
Vitaly Kuznetsov | 0699c64 | 2019-04-30 19:33:26 +0200 | [diff] [blame] | 4492 | ext.cr4_pae = !!is_pae(vcpu); |
Vitaly Kuznetsov | a336282 | 2018-10-08 21:28:11 +0200 | [diff] [blame] | 4493 | 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 Zhang | de3ccd2 | 2019-02-01 00:09:23 +0800 | [diff] [blame] | 4497 | ext.maxphyaddr = cpuid_maxphyaddr(vcpu); |
Vitaly Kuznetsov | a336282 | 2018-10-08 21:28:11 +0200 | [diff] [blame] | 4498 | |
| 4499 | ext.valid = 1; |
| 4500 | |
| 4501 | return ext; |
| 4502 | } |
| 4503 | |
Vitaly Kuznetsov | 7dcd575 | 2018-10-08 21:28:12 +0200 | [diff] [blame] | 4504 | static union kvm_mmu_role kvm_calc_mmu_role_common(struct kvm_vcpu *vcpu, |
| 4505 | bool base_only) |
Junaid Shahid | 9fa7211 | 2018-06-27 14:59:07 -0700 | [diff] [blame] | 4506 | { |
Vitaly Kuznetsov | 7dcd575 | 2018-10-08 21:28:12 +0200 | [diff] [blame] | 4507 | union kvm_mmu_role role = {0}; |
Junaid Shahid | 9fa7211 | 2018-06-27 14:59:07 -0700 | [diff] [blame] | 4508 | |
Vitaly Kuznetsov | 7dcd575 | 2018-10-08 21:28:12 +0200 | [diff] [blame] | 4509 | role.base.access = ACC_ALL; |
| 4510 | role.base.nxe = !!is_nx(vcpu); |
Vitaly Kuznetsov | 7dcd575 | 2018-10-08 21:28:12 +0200 | [diff] [blame] | 4511 | 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 Christopherson | d468d94 | 2020-07-15 20:41:20 -0700 | [diff] [blame] | 4523 | static 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 Christopherson | 8301305 | 2020-07-15 20:41:22 -0700 | [diff] [blame] | 4526 | if (max_tdp_level == 5 && cpuid_maxphyaddr(vcpu) <= 48) |
Sean Christopherson | d468d94 | 2020-07-15 20:41:20 -0700 | [diff] [blame] | 4527 | return 4; |
| 4528 | |
Sean Christopherson | 8301305 | 2020-07-15 20:41:22 -0700 | [diff] [blame] | 4529 | return max_tdp_level; |
Sean Christopherson | d468d94 | 2020-07-15 20:41:20 -0700 | [diff] [blame] | 4530 | } |
| 4531 | |
Vitaly Kuznetsov | 7dcd575 | 2018-10-08 21:28:12 +0200 | [diff] [blame] | 4532 | static union kvm_mmu_role |
| 4533 | kvm_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 Christopherson | d468d94 | 2020-07-15 20:41:20 -0700 | [diff] [blame] | 4538 | role.base.level = kvm_mmu_get_tdp_level(vcpu); |
Vitaly Kuznetsov | 7dcd575 | 2018-10-08 21:28:12 +0200 | [diff] [blame] | 4539 | role.base.direct = true; |
Sean Christopherson | 47c42e6 | 2019-03-07 15:27:44 -0800 | [diff] [blame] | 4540 | role.base.gpte_is_8_bytes = true; |
Junaid Shahid | 9fa7211 | 2018-06-27 14:59:07 -0700 | [diff] [blame] | 4541 | |
| 4542 | return role; |
| 4543 | } |
| 4544 | |
Paolo Bonzini | 8a3c1a33 | 2013-10-02 16:56:13 +0200 | [diff] [blame] | 4545 | static void init_kvm_tdp_mmu(struct kvm_vcpu *vcpu) |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 4546 | { |
Paolo Bonzini | 8c00865 | 2020-07-10 16:11:50 +0200 | [diff] [blame] | 4547 | struct kvm_mmu *context = &vcpu->arch.root_mmu; |
Vitaly Kuznetsov | 7dcd575 | 2018-10-08 21:28:12 +0200 | [diff] [blame] | 4548 | union kvm_mmu_role new_role = |
| 4549 | kvm_calc_tdp_mmu_root_page_role(vcpu, false); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 4550 | |
Vitaly Kuznetsov | 7dcd575 | 2018-10-08 21:28:12 +0200 | [diff] [blame] | 4551 | if (new_role.as_u64 == context->mmu_role.as_u64) |
| 4552 | return; |
| 4553 | |
| 4554 | context->mmu_role.as_u64 = new_role.as_u64; |
Sean Christopherson | 7a02674 | 2020-02-06 14:14:34 -0800 | [diff] [blame] | 4555 | context->page_fault = kvm_tdp_page_fault; |
Marcelo Tosatti | e8bc217 | 2008-09-23 13:18:33 -0300 | [diff] [blame] | 4556 | context->sync_page = nonpaging_sync_page; |
Paolo Bonzini | 5efac07 | 2020-03-23 20:42:57 -0400 | [diff] [blame] | 4557 | context->invlpg = NULL; |
Sean Christopherson | d468d94 | 2020-07-15 20:41:20 -0700 | [diff] [blame] | 4558 | context->shadow_root_level = kvm_mmu_get_tdp_level(vcpu); |
Joerg Roedel | c5a78f2b | 2010-09-10 17:30:39 +0200 | [diff] [blame] | 4559 | context->direct_map = true; |
Sean Christopherson | d8dd54e | 2020-03-02 18:02:39 -0800 | [diff] [blame] | 4560 | context->get_guest_pgd = get_cr3; |
Avi Kivity | e4e517b | 2011-07-28 11:36:17 +0300 | [diff] [blame] | 4561 | context->get_pdptr = kvm_pdptr_read; |
Joerg Roedel | cb659db | 2010-09-10 17:30:43 +0200 | [diff] [blame] | 4562 | context->inject_page_fault = kvm_inject_page_fault; |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 4563 | |
| 4564 | if (!is_paging(vcpu)) { |
Joerg Roedel | 2d48a98 | 2010-09-10 17:31:01 +0200 | [diff] [blame] | 4565 | context->nx = false; |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 4566 | context->gva_to_gpa = nonpaging_gva_to_gpa; |
| 4567 | context->root_level = 0; |
| 4568 | } else if (is_long_mode(vcpu)) { |
Joerg Roedel | 2d48a98 | 2010-09-10 17:31:01 +0200 | [diff] [blame] | 4569 | context->nx = is_nx(vcpu); |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 4570 | context->root_level = is_la57_mode(vcpu) ? |
| 4571 | PT64_ROOT_5LEVEL : PT64_ROOT_4LEVEL; |
Davidlohr Bueso | 4d6931c | 2012-03-05 16:53:06 +0100 | [diff] [blame] | 4572 | reset_rsvds_bits_mask(vcpu, context); |
| 4573 | context->gva_to_gpa = paging64_gva_to_gpa; |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 4574 | } else if (is_pae(vcpu)) { |
Joerg Roedel | 2d48a98 | 2010-09-10 17:31:01 +0200 | [diff] [blame] | 4575 | context->nx = is_nx(vcpu); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 4576 | context->root_level = PT32E_ROOT_LEVEL; |
Davidlohr Bueso | 4d6931c | 2012-03-05 16:53:06 +0100 | [diff] [blame] | 4577 | reset_rsvds_bits_mask(vcpu, context); |
| 4578 | context->gva_to_gpa = paging64_gva_to_gpa; |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 4579 | } else { |
Joerg Roedel | 2d48a98 | 2010-09-10 17:31:01 +0200 | [diff] [blame] | 4580 | context->nx = false; |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 4581 | context->root_level = PT32_ROOT_LEVEL; |
Davidlohr Bueso | 4d6931c | 2012-03-05 16:53:06 +0100 | [diff] [blame] | 4582 | reset_rsvds_bits_mask(vcpu, context); |
| 4583 | context->gva_to_gpa = paging32_gva_to_gpa; |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 4584 | } |
| 4585 | |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 4586 | update_permission_bitmask(vcpu, context, false); |
Huaitong Han | 2d34410 | 2016-03-22 16:51:19 +0800 | [diff] [blame] | 4587 | update_pkru_bitmask(vcpu, context, false); |
Paolo Bonzini | 6bb69c9 | 2016-02-23 12:51:19 +0100 | [diff] [blame] | 4588 | update_last_nonleaf_level(vcpu, context); |
Xiao Guangrong | c258b62 | 2015-08-05 12:04:24 +0800 | [diff] [blame] | 4589 | reset_tdp_shadow_zero_bits_mask(vcpu, context); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 4590 | } |
| 4591 | |
Vitaly Kuznetsov | 7dcd575 | 2018-10-08 21:28:12 +0200 | [diff] [blame] | 4592 | static union kvm_mmu_role |
Sean Christopherson | 59505b5 | 2020-07-15 20:41:15 -0700 | [diff] [blame] | 4593 | kvm_calc_shadow_root_page_role_common(struct kvm_vcpu *vcpu, bool base_only) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4594 | { |
Vitaly Kuznetsov | 7dcd575 | 2018-10-08 21:28:12 +0200 | [diff] [blame] | 4595 | union kvm_mmu_role role = kvm_calc_mmu_role_common(vcpu, base_only); |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 4596 | |
Vitaly Kuznetsov | 7dcd575 | 2018-10-08 21:28:12 +0200 | [diff] [blame] | 4597 | 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 Christopherson | 47c42e6 | 2019-03-07 15:27:44 -0800 | [diff] [blame] | 4601 | role.base.gpte_is_8_bytes = !!is_pae(vcpu); |
Junaid Shahid | 9fa7211 | 2018-06-27 14:59:07 -0700 | [diff] [blame] | 4602 | |
Sean Christopherson | 59505b5 | 2020-07-15 20:41:15 -0700 | [diff] [blame] | 4603 | return role; |
| 4604 | } |
| 4605 | |
| 4606 | static union kvm_mmu_role |
| 4607 | kvm_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 Shahid | 9fa7211 | 2018-06-27 14:59:07 -0700 | [diff] [blame] | 4614 | if (!is_long_mode(vcpu)) |
Vitaly Kuznetsov | 7dcd575 | 2018-10-08 21:28:12 +0200 | [diff] [blame] | 4615 | role.base.level = PT32E_ROOT_LEVEL; |
Junaid Shahid | 9fa7211 | 2018-06-27 14:59:07 -0700 | [diff] [blame] | 4616 | else if (is_la57_mode(vcpu)) |
Vitaly Kuznetsov | 7dcd575 | 2018-10-08 21:28:12 +0200 | [diff] [blame] | 4617 | role.base.level = PT64_ROOT_5LEVEL; |
Junaid Shahid | 9fa7211 | 2018-06-27 14:59:07 -0700 | [diff] [blame] | 4618 | else |
Vitaly Kuznetsov | 7dcd575 | 2018-10-08 21:28:12 +0200 | [diff] [blame] | 4619 | role.base.level = PT64_ROOT_4LEVEL; |
Junaid Shahid | 9fa7211 | 2018-06-27 14:59:07 -0700 | [diff] [blame] | 4620 | |
| 4621 | return role; |
| 4622 | } |
| 4623 | |
Paolo Bonzini | 8c00865 | 2020-07-10 16:11:50 +0200 | [diff] [blame] | 4624 | static 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 Shahid | 9fa7211 | 2018-06-27 14:59:07 -0700 | [diff] [blame] | 4627 | { |
Paolo Bonzini | 929d1cf | 2020-05-19 06:18:31 -0400 | [diff] [blame] | 4628 | if (!(cr0 & X86_CR0_PG)) |
Paolo Bonzini | 8a3c1a33 | 2013-10-02 16:56:13 +0200 | [diff] [blame] | 4629 | nonpaging_init_context(vcpu, context); |
Paolo Bonzini | 929d1cf | 2020-05-19 06:18:31 -0400 | [diff] [blame] | 4630 | else if (efer & EFER_LMA) |
Paolo Bonzini | 8a3c1a33 | 2013-10-02 16:56:13 +0200 | [diff] [blame] | 4631 | paging64_init_context(vcpu, context); |
Paolo Bonzini | 929d1cf | 2020-05-19 06:18:31 -0400 | [diff] [blame] | 4632 | else if (cr4 & X86_CR4_PAE) |
Paolo Bonzini | 8a3c1a33 | 2013-10-02 16:56:13 +0200 | [diff] [blame] | 4633 | paging32E_init_context(vcpu, context); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4634 | else |
Paolo Bonzini | 8a3c1a33 | 2013-10-02 16:56:13 +0200 | [diff] [blame] | 4635 | paging32_init_context(vcpu, context); |
Avi Kivity | a770f6f | 2008-12-21 19:20:09 +0200 | [diff] [blame] | 4636 | |
Vitaly Kuznetsov | 7dcd575 | 2018-10-08 21:28:12 +0200 | [diff] [blame] | 4637 | context->mmu_role.as_u64 = new_role.as_u64; |
Xiao Guangrong | c258b62 | 2015-08-05 12:04:24 +0800 | [diff] [blame] | 4638 | reset_shadow_zero_bits_mask(vcpu, context); |
Joerg Roedel | 52fde8d | 2010-09-10 17:30:44 +0200 | [diff] [blame] | 4639 | } |
Vitaly Kuznetsov | 0f04a2a | 2020-07-10 16:11:49 +0200 | [diff] [blame] | 4640 | |
| 4641 | static void kvm_init_shadow_mmu(struct kvm_vcpu *vcpu, u32 cr0, u32 cr4, u32 efer) |
| 4642 | { |
Paolo Bonzini | 8c00865 | 2020-07-10 16:11:50 +0200 | [diff] [blame] | 4643 | struct kvm_mmu *context = &vcpu->arch.root_mmu; |
Vitaly Kuznetsov | 0f04a2a | 2020-07-10 16:11:49 +0200 | [diff] [blame] | 4644 | 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 Bonzini | 8c00865 | 2020-07-10 16:11:50 +0200 | [diff] [blame] | 4648 | shadow_mmu_init_context(vcpu, context, cr0, cr4, efer, new_role); |
Vitaly Kuznetsov | 0f04a2a | 2020-07-10 16:11:49 +0200 | [diff] [blame] | 4649 | } |
| 4650 | |
Sean Christopherson | 59505b5 | 2020-07-15 20:41:15 -0700 | [diff] [blame] | 4651 | static union kvm_mmu_role |
| 4652 | kvm_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 Christopherson | d468d94 | 2020-07-15 20:41:20 -0700 | [diff] [blame] | 4658 | role.base.level = kvm_mmu_get_tdp_level(vcpu); |
Sean Christopherson | 59505b5 | 2020-07-15 20:41:15 -0700 | [diff] [blame] | 4659 | |
| 4660 | return role; |
| 4661 | } |
| 4662 | |
Vitaly Kuznetsov | 0f04a2a | 2020-07-10 16:11:49 +0200 | [diff] [blame] | 4663 | void kvm_init_shadow_npt_mmu(struct kvm_vcpu *vcpu, u32 cr0, u32 cr4, u32 efer, |
| 4664 | gpa_t nested_cr3) |
| 4665 | { |
Paolo Bonzini | 8c00865 | 2020-07-10 16:11:50 +0200 | [diff] [blame] | 4666 | struct kvm_mmu *context = &vcpu->arch.guest_mmu; |
Sean Christopherson | 59505b5 | 2020-07-15 20:41:15 -0700 | [diff] [blame] | 4667 | union kvm_mmu_role new_role = kvm_calc_shadow_npt_root_page_role(vcpu); |
Vitaly Kuznetsov | 0f04a2a | 2020-07-10 16:11:49 +0200 | [diff] [blame] | 4668 | |
Vitaly Kuznetsov | a506fdd | 2020-07-10 16:11:55 +0200 | [diff] [blame] | 4669 | __kvm_mmu_new_pgd(vcpu, nested_cr3, new_role.base, false, false); |
| 4670 | |
Sean Christopherson | a3322d5 | 2021-03-04 17:10:45 -0800 | [diff] [blame] | 4671 | if (new_role.as_u64 != context->mmu_role.as_u64) { |
Paolo Bonzini | 8c00865 | 2020-07-10 16:11:50 +0200 | [diff] [blame] | 4672 | shadow_mmu_init_context(vcpu, context, cr0, cr4, efer, new_role); |
Sean Christopherson | a3322d5 | 2021-03-04 17:10:45 -0800 | [diff] [blame] | 4673 | |
| 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 Kuznetsov | 0f04a2a | 2020-07-10 16:11:49 +0200 | [diff] [blame] | 4680 | } |
| 4681 | EXPORT_SYMBOL_GPL(kvm_init_shadow_npt_mmu); |
Joerg Roedel | 52fde8d | 2010-09-10 17:30:44 +0200 | [diff] [blame] | 4682 | |
Vitaly Kuznetsov | a336282 | 2018-10-08 21:28:11 +0200 | [diff] [blame] | 4683 | static union kvm_mmu_role |
| 4684 | kvm_calc_shadow_ept_root_page_role(struct kvm_vcpu *vcpu, bool accessed_dirty, |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 4685 | bool execonly, u8 level) |
Junaid Shahid | 9fa7211 | 2018-06-27 14:59:07 -0700 | [diff] [blame] | 4686 | { |
Sean Christopherson | 552c69b1 | 2019-03-07 15:27:43 -0800 | [diff] [blame] | 4687 | union kvm_mmu_role role = {0}; |
Junaid Shahid | 9fa7211 | 2018-06-27 14:59:07 -0700 | [diff] [blame] | 4688 | |
Sean Christopherson | 47c42e6 | 2019-03-07 15:27:44 -0800 | [diff] [blame] | 4689 | /* SMM flag is inherited from root_mmu */ |
| 4690 | role.base.smm = vcpu->arch.root_mmu.mmu_role.base.smm; |
Junaid Shahid | 9fa7211 | 2018-06-27 14:59:07 -0700 | [diff] [blame] | 4691 | |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 4692 | role.base.level = level; |
Sean Christopherson | 47c42e6 | 2019-03-07 15:27:44 -0800 | [diff] [blame] | 4693 | role.base.gpte_is_8_bytes = true; |
Vitaly Kuznetsov | a336282 | 2018-10-08 21:28:11 +0200 | [diff] [blame] | 4694 | 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 Christopherson | 47c42e6 | 2019-03-07 15:27:44 -0800 | [diff] [blame] | 4699 | /* |
| 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 Christopherson | 552c69b1 | 2019-03-07 15:27:43 -0800 | [diff] [blame] | 4706 | role.ext = kvm_calc_mmu_role_ext(vcpu); |
Vitaly Kuznetsov | a336282 | 2018-10-08 21:28:11 +0200 | [diff] [blame] | 4707 | role.ext.execonly = execonly; |
Junaid Shahid | 9fa7211 | 2018-06-27 14:59:07 -0700 | [diff] [blame] | 4708 | |
| 4709 | return role; |
| 4710 | } |
| 4711 | |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 4712 | void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly, |
Junaid Shahid | 50c28f2 | 2018-06-27 14:59:11 -0700 | [diff] [blame] | 4713 | bool accessed_dirty, gpa_t new_eptp) |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 4714 | { |
Paolo Bonzini | 8c00865 | 2020-07-10 16:11:50 +0200 | [diff] [blame] | 4715 | struct kvm_mmu *context = &vcpu->arch.guest_mmu; |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 4716 | u8 level = vmx_eptp_page_walk_level(new_eptp); |
Vitaly Kuznetsov | a336282 | 2018-10-08 21:28:11 +0200 | [diff] [blame] | 4717 | union kvm_mmu_role new_role = |
| 4718 | kvm_calc_shadow_ept_root_page_role(vcpu, accessed_dirty, |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 4719 | execonly, level); |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 4720 | |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 4721 | __kvm_mmu_new_pgd(vcpu, new_eptp, new_role.base, true, true); |
Vitaly Kuznetsov | a336282 | 2018-10-08 21:28:11 +0200 | [diff] [blame] | 4722 | |
Vitaly Kuznetsov | a336282 | 2018-10-08 21:28:11 +0200 | [diff] [blame] | 4723 | if (new_role.as_u64 == context->mmu_role.as_u64) |
| 4724 | return; |
| 4725 | |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 4726 | context->shadow_root_level = level; |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 4727 | |
| 4728 | context->nx = true; |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 4729 | context->ept_ad = accessed_dirty; |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 4730 | 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 Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 4734 | context->root_level = level; |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 4735 | context->direct_map = false; |
Vitaly Kuznetsov | a336282 | 2018-10-08 21:28:11 +0200 | [diff] [blame] | 4736 | context->mmu_role.as_u64 = new_role.as_u64; |
Vitaly Kuznetsov | 3dc773e | 2018-10-08 21:28:06 +0200 | [diff] [blame] | 4737 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 4738 | update_permission_bitmask(vcpu, context, true); |
Huaitong Han | 2d34410 | 2016-03-22 16:51:19 +0800 | [diff] [blame] | 4739 | update_pkru_bitmask(vcpu, context, true); |
Ladi Prosek | fd19d3b4 | 2017-10-05 11:10:22 +0200 | [diff] [blame] | 4740 | update_last_nonleaf_level(vcpu, context); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 4741 | reset_rsvds_bits_mask_ept(vcpu, context, execonly); |
Xiao Guangrong | c258b62 | 2015-08-05 12:04:24 +0800 | [diff] [blame] | 4742 | reset_ept_shadow_zero_bits_mask(vcpu, context, execonly); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 4743 | } |
| 4744 | EXPORT_SYMBOL_GPL(kvm_init_shadow_ept_mmu); |
| 4745 | |
Paolo Bonzini | 8a3c1a33 | 2013-10-02 16:56:13 +0200 | [diff] [blame] | 4746 | static void init_kvm_softmmu(struct kvm_vcpu *vcpu) |
Joerg Roedel | 52fde8d | 2010-09-10 17:30:44 +0200 | [diff] [blame] | 4747 | { |
Paolo Bonzini | 8c00865 | 2020-07-10 16:11:50 +0200 | [diff] [blame] | 4748 | struct kvm_mmu *context = &vcpu->arch.root_mmu; |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 4749 | |
Paolo Bonzini | 929d1cf | 2020-05-19 06:18:31 -0400 | [diff] [blame] | 4750 | 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 Christopherson | d8dd54e | 2020-03-02 18:02:39 -0800 | [diff] [blame] | 4755 | context->get_guest_pgd = get_cr3; |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 4756 | context->get_pdptr = kvm_pdptr_read; |
| 4757 | context->inject_page_fault = kvm_inject_page_fault; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4758 | } |
| 4759 | |
Sean Christopherson | 654430ef | 2021-06-10 15:00:26 -0700 | [diff] [blame] | 4760 | static 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 Bonzini | 8a3c1a33 | 2013-10-02 16:56:13 +0200 | [diff] [blame] | 4784 | static void init_kvm_nested_mmu(struct kvm_vcpu *vcpu) |
Joerg Roedel | 02f59dc | 2010-09-10 17:30:54 +0200 | [diff] [blame] | 4785 | { |
Sean Christopherson | 654430ef | 2021-06-10 15:00:26 -0700 | [diff] [blame] | 4786 | union kvm_mmu_role new_role = kvm_calc_nested_mmu_role(vcpu); |
Joerg Roedel | 02f59dc | 2010-09-10 17:30:54 +0200 | [diff] [blame] | 4787 | struct kvm_mmu *g_context = &vcpu->arch.nested_mmu; |
| 4788 | |
Vitaly Kuznetsov | bf627a9 | 2018-10-08 21:28:13 +0200 | [diff] [blame] | 4789 | 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 Christopherson | d8dd54e | 2020-03-02 18:02:39 -0800 | [diff] [blame] | 4793 | g_context->get_guest_pgd = get_cr3; |
Avi Kivity | e4e517b | 2011-07-28 11:36:17 +0300 | [diff] [blame] | 4794 | g_context->get_pdptr = kvm_pdptr_read; |
Joerg Roedel | 02f59dc | 2010-09-10 17:30:54 +0200 | [diff] [blame] | 4795 | g_context->inject_page_fault = kvm_inject_page_fault; |
| 4796 | |
| 4797 | /* |
Paolo Bonzini | 5efac07 | 2020-03-23 20:42:57 -0400 | [diff] [blame] | 4798 | * L2 page tables are never shadowed, so there is no need to sync |
| 4799 | * SPTEs. |
| 4800 | */ |
| 4801 | g_context->invlpg = NULL; |
| 4802 | |
| 4803 | /* |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 4804 | * Note that arch.mmu->gva_to_gpa translates l2_gpa to l1_gpa using |
David Matlack | 0af2593 | 2015-12-30 08:26:17 -0800 | [diff] [blame] | 4805 | * 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 Roedel | 02f59dc | 2010-09-10 17:30:54 +0200 | [diff] [blame] | 4810 | */ |
| 4811 | if (!is_paging(vcpu)) { |
Joerg Roedel | 2d48a98 | 2010-09-10 17:31:01 +0200 | [diff] [blame] | 4812 | g_context->nx = false; |
Joerg Roedel | 02f59dc | 2010-09-10 17:30:54 +0200 | [diff] [blame] | 4813 | g_context->root_level = 0; |
| 4814 | g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested; |
| 4815 | } else if (is_long_mode(vcpu)) { |
Joerg Roedel | 2d48a98 | 2010-09-10 17:31:01 +0200 | [diff] [blame] | 4816 | g_context->nx = is_nx(vcpu); |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 4817 | g_context->root_level = is_la57_mode(vcpu) ? |
| 4818 | PT64_ROOT_5LEVEL : PT64_ROOT_4LEVEL; |
Davidlohr Bueso | 4d6931c | 2012-03-05 16:53:06 +0100 | [diff] [blame] | 4819 | reset_rsvds_bits_mask(vcpu, g_context); |
Joerg Roedel | 02f59dc | 2010-09-10 17:30:54 +0200 | [diff] [blame] | 4820 | g_context->gva_to_gpa = paging64_gva_to_gpa_nested; |
| 4821 | } else if (is_pae(vcpu)) { |
Joerg Roedel | 2d48a98 | 2010-09-10 17:31:01 +0200 | [diff] [blame] | 4822 | g_context->nx = is_nx(vcpu); |
Joerg Roedel | 02f59dc | 2010-09-10 17:30:54 +0200 | [diff] [blame] | 4823 | g_context->root_level = PT32E_ROOT_LEVEL; |
Davidlohr Bueso | 4d6931c | 2012-03-05 16:53:06 +0100 | [diff] [blame] | 4824 | reset_rsvds_bits_mask(vcpu, g_context); |
Joerg Roedel | 02f59dc | 2010-09-10 17:30:54 +0200 | [diff] [blame] | 4825 | g_context->gva_to_gpa = paging64_gva_to_gpa_nested; |
| 4826 | } else { |
Joerg Roedel | 2d48a98 | 2010-09-10 17:31:01 +0200 | [diff] [blame] | 4827 | g_context->nx = false; |
Joerg Roedel | 02f59dc | 2010-09-10 17:30:54 +0200 | [diff] [blame] | 4828 | g_context->root_level = PT32_ROOT_LEVEL; |
Davidlohr Bueso | 4d6931c | 2012-03-05 16:53:06 +0100 | [diff] [blame] | 4829 | reset_rsvds_bits_mask(vcpu, g_context); |
Joerg Roedel | 02f59dc | 2010-09-10 17:30:54 +0200 | [diff] [blame] | 4830 | g_context->gva_to_gpa = paging32_gva_to_gpa_nested; |
| 4831 | } |
| 4832 | |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 4833 | update_permission_bitmask(vcpu, g_context, false); |
Huaitong Han | 2d34410 | 2016-03-22 16:51:19 +0800 | [diff] [blame] | 4834 | update_pkru_bitmask(vcpu, g_context, false); |
Paolo Bonzini | 6bb69c9 | 2016-02-23 12:51:19 +0100 | [diff] [blame] | 4835 | update_last_nonleaf_level(vcpu, g_context); |
Joerg Roedel | 02f59dc | 2010-09-10 17:30:54 +0200 | [diff] [blame] | 4836 | } |
| 4837 | |
Junaid Shahid | 1c53da3 | 2018-06-27 14:59:10 -0700 | [diff] [blame] | 4838 | void kvm_init_mmu(struct kvm_vcpu *vcpu, bool reset_roots) |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 4839 | { |
Junaid Shahid | 1c53da3 | 2018-06-27 14:59:10 -0700 | [diff] [blame] | 4840 | if (reset_roots) { |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 4841 | uint i; |
| 4842 | |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 4843 | vcpu->arch.mmu->root_hpa = INVALID_PAGE; |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 4844 | |
| 4845 | for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 4846 | vcpu->arch.mmu->prev_roots[i] = KVM_MMU_ROOT_INFO_INVALID; |
Junaid Shahid | 1c53da3 | 2018-06-27 14:59:10 -0700 | [diff] [blame] | 4847 | } |
| 4848 | |
Joerg Roedel | 02f59dc | 2010-09-10 17:30:54 +0200 | [diff] [blame] | 4849 | if (mmu_is_nested(vcpu)) |
Paolo Bonzini | e0c6db3 | 2014-12-23 13:39:46 +0100 | [diff] [blame] | 4850 | init_kvm_nested_mmu(vcpu); |
Joerg Roedel | 02f59dc | 2010-09-10 17:30:54 +0200 | [diff] [blame] | 4851 | else if (tdp_enabled) |
Paolo Bonzini | e0c6db3 | 2014-12-23 13:39:46 +0100 | [diff] [blame] | 4852 | init_kvm_tdp_mmu(vcpu); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 4853 | else |
Paolo Bonzini | e0c6db3 | 2014-12-23 13:39:46 +0100 | [diff] [blame] | 4854 | init_kvm_softmmu(vcpu); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 4855 | } |
Junaid Shahid | 1c53da3 | 2018-06-27 14:59:10 -0700 | [diff] [blame] | 4856 | EXPORT_SYMBOL_GPL(kvm_init_mmu); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 4857 | |
Junaid Shahid | 9fa7211 | 2018-06-27 14:59:07 -0700 | [diff] [blame] | 4858 | static union kvm_mmu_page_role |
| 4859 | kvm_mmu_calc_root_page_role(struct kvm_vcpu *vcpu) |
| 4860 | { |
Vitaly Kuznetsov | 7dcd575 | 2018-10-08 21:28:12 +0200 | [diff] [blame] | 4861 | union kvm_mmu_role role; |
| 4862 | |
Junaid Shahid | 9fa7211 | 2018-06-27 14:59:07 -0700 | [diff] [blame] | 4863 | if (tdp_enabled) |
Vitaly Kuznetsov | 7dcd575 | 2018-10-08 21:28:12 +0200 | [diff] [blame] | 4864 | role = kvm_calc_tdp_mmu_root_page_role(vcpu, true); |
Junaid Shahid | 9fa7211 | 2018-06-27 14:59:07 -0700 | [diff] [blame] | 4865 | else |
Vitaly Kuznetsov | 7dcd575 | 2018-10-08 21:28:12 +0200 | [diff] [blame] | 4866 | role = kvm_calc_shadow_mmu_root_page_role(vcpu, true); |
| 4867 | |
| 4868 | return role.base; |
Junaid Shahid | 9fa7211 | 2018-06-27 14:59:07 -0700 | [diff] [blame] | 4869 | } |
Dong, Eddie | 489f1d6 | 2008-01-07 11:14:20 +0200 | [diff] [blame] | 4870 | |
Paolo Bonzini | 8a3c1a33 | 2013-10-02 16:56:13 +0200 | [diff] [blame] | 4871 | void kvm_mmu_reset_context(struct kvm_vcpu *vcpu) |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 4872 | { |
Paolo Bonzini | 95f93af | 2013-10-02 16:56:12 +0200 | [diff] [blame] | 4873 | kvm_mmu_unload(vcpu); |
Junaid Shahid | 1c53da3 | 2018-06-27 14:59:10 -0700 | [diff] [blame] | 4874 | kvm_init_mmu(vcpu, true); |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 4875 | } |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 4876 | EXPORT_SYMBOL_GPL(kvm_mmu_reset_context); |
| 4877 | |
| 4878 | int kvm_mmu_load(struct kvm_vcpu *vcpu) |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 4879 | { |
| 4880 | int r; |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 4881 | |
Sean Christopherson | 378f5cd | 2020-07-02 19:35:36 -0700 | [diff] [blame] | 4882 | r = mmu_topup_memory_caches(vcpu, !vcpu->arch.mmu->direct_map); |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 4883 | if (r) |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 4884 | goto out; |
Sean Christopherson | 748e52b | 2021-03-04 17:10:49 -0800 | [diff] [blame] | 4885 | r = mmu_alloc_special_roots(vcpu); |
Marcelo Tosatti | 8986ecc | 2009-05-12 18:55:45 -0300 | [diff] [blame] | 4886 | if (r) |
| 4887 | goto out; |
Paolo Bonzini | 4a38162 | 2021-04-08 08:10:25 -0400 | [diff] [blame] | 4888 | if (vcpu->arch.mmu->direct_map) |
Sean Christopherson | 6e6ec58 | 2021-03-04 17:10:50 -0800 | [diff] [blame] | 4889 | r = mmu_alloc_direct_roots(vcpu); |
| 4890 | else |
| 4891 | r = mmu_alloc_shadow_roots(vcpu); |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 4892 | if (r) |
| 4893 | goto out; |
Sean Christopherson | a91f387 | 2021-03-04 17:11:00 -0800 | [diff] [blame] | 4894 | |
| 4895 | kvm_mmu_sync_roots(vcpu); |
| 4896 | |
Paolo Bonzini | 727a7e2 | 2020-03-05 03:52:50 -0500 | [diff] [blame] | 4897 | kvm_mmu_load_pgd(vcpu); |
Jason Baron | b3646477 | 2021-01-14 22:27:56 -0500 | [diff] [blame] | 4898 | static_call(kvm_x86_tlb_flush_current)(vcpu); |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 4899 | out: |
| 4900 | return r; |
| 4901 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4902 | |
| 4903 | void kvm_mmu_unload(struct kvm_vcpu *vcpu) |
| 4904 | { |
Vitaly Kuznetsov | 14c07ad | 2018-10-08 21:28:08 +0200 | [diff] [blame] | 4905 | 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 Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4909 | } |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 4910 | |
Avi Kivity | 79539ce | 2007-11-21 02:06:21 +0200 | [diff] [blame] | 4911 | static 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 Natapov | 5316622 | 2013-08-05 11:07:14 +0300 | [diff] [blame] | 4919 | old ^= shadow_nx_mask; |
| 4920 | new ^= shadow_nx_mask; |
Avi Kivity | 79539ce | 2007-11-21 02:06:21 +0200 | [diff] [blame] | 4921 | return (old & ~new & PT64_PERM_MASK) != 0; |
| 4922 | } |
| 4923 | |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 4924 | static u64 mmu_pte_write_fetch_gpte(struct kvm_vcpu *vcpu, gpa_t *gpa, |
Junaid Shahid | 0e0fee5 | 2018-10-31 14:53:57 -0700 | [diff] [blame] | 4925 | int *bytes) |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 4926 | { |
Junaid Shahid | 0e0fee5 | 2018-10-31 14:53:57 -0700 | [diff] [blame] | 4927 | u64 gentry = 0; |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 4928 | int r; |
Avi Kivity | 72016f3 | 2010-03-15 13:59:53 +0200 | [diff] [blame] | 4929 | |
Avi Kivity | 08e850c | 2010-03-15 13:59:57 +0200 | [diff] [blame] | 4930 | /* |
| 4931 | * Assume that the pte write on a page table of the same type |
Xiao Guangrong | 49b26e2 | 2011-03-04 19:00:00 +0800 | [diff] [blame] | 4932 | * as the current vcpu paging mode since we update the sptes only |
| 4933 | * when they have the same mode. |
Avi Kivity | 08e850c | 2010-03-15 13:59:57 +0200 | [diff] [blame] | 4934 | */ |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 4935 | if (is_pae(vcpu) && *bytes == 4) { |
Avi Kivity | 08e850c | 2010-03-15 13:59:57 +0200 | [diff] [blame] | 4936 | /* Handle a 32-bit guest writing two halves of a 64-bit gpte */ |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 4937 | *gpa &= ~(gpa_t)7; |
| 4938 | *bytes = 8; |
Avi Kivity | 08e850c | 2010-03-15 13:59:57 +0200 | [diff] [blame] | 4939 | } |
| 4940 | |
Junaid Shahid | 0e0fee5 | 2018-10-31 14:53:57 -0700 | [diff] [blame] | 4941 | if (*bytes == 4 || *bytes == 8) { |
| 4942 | r = kvm_vcpu_read_guest_atomic(vcpu, *gpa, &gentry, *bytes); |
| 4943 | if (r) |
| 4944 | gentry = 0; |
Avi Kivity | 72016f3 | 2010-03-15 13:59:53 +0200 | [diff] [blame] | 4945 | } |
| 4946 | |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 4947 | 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 Guangrong | a138fe7 | 2011-12-16 18:18:10 +0800 | [diff] [blame] | 4954 | static bool detect_write_flooding(struct kvm_mmu_page *sp) |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 4955 | { |
Xiao Guangrong | a30f47c | 2011-09-22 16:58:36 +0800 | [diff] [blame] | 4956 | /* |
| 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 Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 4960 | if (sp->role.level == PG_LEVEL_4K) |
Xiao Guangrong | a30f47c | 2011-09-22 16:58:36 +0800 | [diff] [blame] | 4961 | return false; |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 4962 | |
Xiao Guangrong | e5691a8 | 2016-02-24 17:51:12 +0800 | [diff] [blame] | 4963 | atomic_inc(&sp->write_flooding_count); |
| 4964 | return atomic_read(&sp->write_flooding_count) >= 3; |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 4965 | } |
| 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 | */ |
| 4971 | static 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 Christopherson | 47c42e6 | 2019-03-07 15:27:44 -0800 | [diff] [blame] | 4980 | pte_size = sp->role.gpte_is_8_bytes ? 8 : 4; |
Xiao Guangrong | 5d9ca30 | 2011-09-22 16:57:55 +0800 | [diff] [blame] | 4981 | |
| 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 Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 4989 | misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1); |
| 4990 | misaligned |= bytes < 4; |
| 4991 | |
| 4992 | return misaligned; |
| 4993 | } |
| 4994 | |
| 4995 | static 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 Christopherson | 47c42e6 | 2019-03-07 15:27:44 -0800 | [diff] [blame] | 5004 | if (!sp->role.gpte_is_8_bytes) { |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 5005 | 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 Guangrong | 13d268c | 2016-02-24 17:51:16 +0800 | [diff] [blame] | 5026 | static void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, |
Jike Song | d126363 | 2016-10-25 15:50:42 +0800 | [diff] [blame] | 5027 | const u8 *new, int bytes, |
| 5028 | struct kvm_page_track_notifier_node *node) |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 5029 | { |
| 5030 | gfn_t gfn = gpa >> PAGE_SHIFT; |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 5031 | struct kvm_mmu_page *sp; |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 5032 | LIST_HEAD(invalid_list); |
| 5033 | u64 entry, gentry, *spte; |
| 5034 | int npte; |
Paolo Bonzini | b8c67b7 | 2016-02-24 11:21:55 +0100 | [diff] [blame] | 5035 | bool remote_flush, local_flush; |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 5036 | |
| 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 Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 5041 | if (!READ_ONCE(vcpu->kvm->arch.indirect_shadow_pages)) |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 5042 | return; |
| 5043 | |
Paolo Bonzini | b8c67b7 | 2016-02-24 11:21:55 +0100 | [diff] [blame] | 5044 | remote_flush = local_flush = false; |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 5045 | |
| 5046 | pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes); |
| 5047 | |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 5048 | /* |
| 5049 | * No need to care whether allocation memory is successful |
Ingo Molnar | d9f6e12 | 2021-03-18 15:28:01 +0100 | [diff] [blame] | 5050 | * or not since pte prefetch is skipped if it does not have |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 5051 | * enough objects in the cache. |
| 5052 | */ |
Sean Christopherson | 378f5cd | 2020-07-02 19:35:36 -0700 | [diff] [blame] | 5053 | mmu_topup_memory_caches(vcpu, true); |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 5054 | |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 5055 | write_lock(&vcpu->kvm->mmu_lock); |
Junaid Shahid | 0e0fee5 | 2018-10-31 14:53:57 -0700 | [diff] [blame] | 5056 | |
| 5057 | gentry = mmu_pte_write_fetch_gpte(vcpu, &gpa, &bytes); |
| 5058 | |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 5059 | ++vcpu->kvm->stat.mmu_pte_write; |
Xiao Guangrong | 0375f7f | 2011-11-28 20:41:00 +0800 | [diff] [blame] | 5060 | kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE); |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 5061 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5062 | for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn) { |
Xiao Guangrong | a30f47c | 2011-09-22 16:58:36 +0800 | [diff] [blame] | 5063 | if (detect_write_misaligned(sp, gpa, bytes) || |
Xiao Guangrong | a138fe7 | 2011-12-16 18:18:10 +0800 | [diff] [blame] | 5064 | detect_write_flooding(sp)) { |
Paolo Bonzini | b8c67b7 | 2016-02-24 11:21:55 +0100 | [diff] [blame] | 5065 | kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list); |
Avi Kivity | 4cee576 | 2007-11-18 16:37:07 +0200 | [diff] [blame] | 5066 | ++vcpu->kvm->stat.mmu_flooded; |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 5067 | continue; |
| 5068 | } |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 5069 | |
| 5070 | spte = get_written_sptes(sp, gpa, &npte); |
| 5071 | if (!spte) |
| 5072 | continue; |
| 5073 | |
Xiao Guangrong | 0671a8e | 2010-06-04 21:56:59 +0800 | [diff] [blame] | 5074 | local_flush = true; |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 5075 | while (npte--) { |
Avi Kivity | 79539ce | 2007-11-21 02:06:21 +0200 | [diff] [blame] | 5076 | entry = *spte; |
Ben Gardon | 2de4085 | 2020-09-23 15:14:06 -0700 | [diff] [blame] | 5077 | mmu_page_zap_pte(vcpu->kvm, sp, spte, NULL); |
Sean Christopherson | c5e2184 | 2021-01-14 16:40:51 -0800 | [diff] [blame] | 5078 | if (gentry && sp->role.level != PG_LEVEL_4K) |
| 5079 | ++vcpu->kvm->stat.mmu_pde_zapped; |
Gleb Natapov | 9bb4f6b | 2013-01-30 16:45:01 +0200 | [diff] [blame] | 5080 | if (need_remote_flush(entry, *spte)) |
Xiao Guangrong | 0671a8e | 2010-06-04 21:56:59 +0800 | [diff] [blame] | 5081 | remote_flush = true; |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 5082 | ++spte; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 5083 | } |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 5084 | } |
Paolo Bonzini | b8c67b7 | 2016-02-24 11:21:55 +0100 | [diff] [blame] | 5085 | kvm_mmu_flush_or_zap(vcpu, &invalid_list, remote_flush, local_flush); |
Xiao Guangrong | 0375f7f | 2011-11-28 20:41:00 +0800 | [diff] [blame] | 5086 | kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE); |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 5087 | write_unlock(&vcpu->kvm->mmu_lock); |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 5088 | } |
| 5089 | |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 5090 | int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code, |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 5091 | void *insn, int insn_len) |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 5092 | { |
Sean Christopherson | 92daa48 | 2020-02-18 15:03:08 -0800 | [diff] [blame] | 5093 | int r, emulation_type = EMULTYPE_PF; |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 5094 | bool direct = vcpu->arch.mmu->direct_map; |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 5095 | |
Sean Christopherson | 6948199 | 2019-12-06 15:57:29 -0800 | [diff] [blame] | 5096 | if (WARN_ON(!VALID_PAGE(vcpu->arch.mmu->root_hpa))) |
Sean Christopherson | ddce620 | 2019-12-06 15:57:27 -0800 | [diff] [blame] | 5097 | return RET_PF_RETRY; |
| 5098 | |
Paolo Bonzini | 9b8ebbd | 2017-08-17 15:03:32 +0200 | [diff] [blame] | 5099 | r = RET_PF_INVALID; |
Takuya Yoshikawa | e9ee956 | 2016-02-22 17:23:41 +0900 | [diff] [blame] | 5100 | if (unlikely(error_code & PFERR_RSVD_MASK)) { |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 5101 | r = handle_mmio_page_fault(vcpu, cr2_or_gpa, direct); |
Sean Christopherson | 472faff | 2018-08-23 13:56:50 -0700 | [diff] [blame] | 5102 | if (r == RET_PF_EMULATE) |
Takuya Yoshikawa | e9ee956 | 2016-02-22 17:23:41 +0900 | [diff] [blame] | 5103 | goto emulate; |
Takuya Yoshikawa | e9ee956 | 2016-02-22 17:23:41 +0900 | [diff] [blame] | 5104 | } |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 5105 | |
Paolo Bonzini | 9b8ebbd | 2017-08-17 15:03:32 +0200 | [diff] [blame] | 5106 | if (r == RET_PF_INVALID) { |
Sean Christopherson | 7a02674 | 2020-02-06 14:14:34 -0800 | [diff] [blame] | 5107 | r = kvm_mmu_do_page_fault(vcpu, cr2_or_gpa, |
| 5108 | lower_32_bits(error_code), false); |
Sean Christopherson | 7b367bc | 2020-09-23 15:04:22 -0700 | [diff] [blame] | 5109 | if (WARN_ON_ONCE(r == RET_PF_INVALID)) |
| 5110 | return -EIO; |
Paolo Bonzini | 9b8ebbd | 2017-08-17 15:03:32 +0200 | [diff] [blame] | 5111 | } |
| 5112 | |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 5113 | if (r < 0) |
Takuya Yoshikawa | e9ee956 | 2016-02-22 17:23:41 +0900 | [diff] [blame] | 5114 | return r; |
Sean Christopherson | 83a2ba4 | 2020-09-23 15:04:23 -0700 | [diff] [blame] | 5115 | if (r != RET_PF_EMULATE) |
| 5116 | return 1; |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 5117 | |
Tom Lendacky | 1472775 | 2016-11-23 12:01:38 -0500 | [diff] [blame] | 5118 | /* |
| 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 Lendacky | 1472775 | 2016-11-23 12:01:38 -0500 | [diff] [blame] | 5124 | */ |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 5125 | if (vcpu->arch.mmu->direct_map && |
Paolo Bonzini | eebed24 | 2016-11-28 14:39:58 +0100 | [diff] [blame] | 5126 | (error_code & PFERR_NESTED_GUEST_PAGE) == PFERR_NESTED_GUEST_PAGE) { |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 5127 | kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(cr2_or_gpa)); |
Tom Lendacky | 1472775 | 2016-11-23 12:01:38 -0500 | [diff] [blame] | 5128 | return 1; |
| 5129 | } |
| 5130 | |
Sean Christopherson | 472faff | 2018-08-23 13:56:50 -0700 | [diff] [blame] | 5131 | /* |
| 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 Christopherson | 6c3dfeb | 2018-08-23 13:56:51 -0700 | [diff] [blame] | 5137 | * 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 Christopherson | 472faff | 2018-08-23 13:56:50 -0700 | [diff] [blame] | 5141 | */ |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 5142 | if (!mmio_info_in_cache(vcpu, cr2_or_gpa, direct) && !is_guest_mode(vcpu)) |
Sean Christopherson | 92daa48 | 2020-02-18 15:03:08 -0800 | [diff] [blame] | 5143 | emulation_type |= EMULTYPE_ALLOW_RETRY_PF; |
Takuya Yoshikawa | e9ee956 | 2016-02-22 17:23:41 +0900 | [diff] [blame] | 5144 | emulate: |
Sean Christopherson | 736c291 | 2019-12-06 15:57:14 -0800 | [diff] [blame] | 5145 | return x86_emulate_instruction(vcpu, cr2_or_gpa, emulation_type, insn, |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 5146 | insn_len); |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 5147 | } |
| 5148 | EXPORT_SYMBOL_GPL(kvm_mmu_page_fault); |
| 5149 | |
Paolo Bonzini | 5efac07 | 2020-03-23 20:42:57 -0400 | [diff] [blame] | 5150 | void kvm_mmu_invalidate_gva(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, |
| 5151 | gva_t gva, hpa_t root_hpa) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 5152 | { |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 5153 | int i; |
Junaid Shahid | 7eb77e9 | 2018-06-27 14:59:16 -0700 | [diff] [blame] | 5154 | |
Paolo Bonzini | 5efac07 | 2020-03-23 20:42:57 -0400 | [diff] [blame] | 5155 | /* 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 Baron | b3646477 | 2021-01-14 22:27:56 -0500 | [diff] [blame] | 5161 | static_call(kvm_x86_tlb_flush_gva)(vcpu, gva); |
Paolo Bonzini | 5efac07 | 2020-03-23 20:42:57 -0400 | [diff] [blame] | 5162 | } |
| 5163 | |
| 5164 | if (!mmu->invlpg) |
Junaid Shahid | faff875 | 2018-06-29 13:10:05 -0700 | [diff] [blame] | 5165 | return; |
| 5166 | |
Paolo Bonzini | 5efac07 | 2020-03-23 20:42:57 -0400 | [diff] [blame] | 5167 | if (root_hpa == INVALID_PAGE) { |
| 5168 | mmu->invlpg(vcpu, gva, mmu->root_hpa); |
Junaid Shahid | 956bf35 | 2018-06-27 14:59:18 -0700 | [diff] [blame] | 5169 | |
Paolo Bonzini | 5efac07 | 2020-03-23 20:42:57 -0400 | [diff] [blame] | 5170 | /* |
| 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 Shahid | 956bf35 | 2018-06-27 14:59:18 -0700 | [diff] [blame] | 5188 | |
Paolo Bonzini | 5efac07 | 2020-03-23 20:42:57 -0400 | [diff] [blame] | 5189 | void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva) |
| 5190 | { |
| 5191 | kvm_mmu_invalidate_gva(vcpu, vcpu->arch.mmu, gva, INVALID_PAGE); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 5192 | ++vcpu->stat.invlpg; |
| 5193 | } |
| 5194 | EXPORT_SYMBOL_GPL(kvm_mmu_invlpg); |
| 5195 | |
Paolo Bonzini | 5efac07 | 2020-03-23 20:42:57 -0400 | [diff] [blame] | 5196 | |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 5197 | void kvm_mmu_invpcid_gva(struct kvm_vcpu *vcpu, gva_t gva, unsigned long pcid) |
| 5198 | { |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 5199 | struct kvm_mmu *mmu = vcpu->arch.mmu; |
Junaid Shahid | faff875 | 2018-06-29 13:10:05 -0700 | [diff] [blame] | 5200 | bool tlb_flush = false; |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 5201 | uint i; |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 5202 | |
| 5203 | if (pcid == kvm_get_active_pcid(vcpu)) { |
Junaid Shahid | 7eb77e9 | 2018-06-27 14:59:16 -0700 | [diff] [blame] | 5204 | mmu->invlpg(vcpu, gva, mmu->root_hpa); |
Junaid Shahid | faff875 | 2018-06-29 13:10:05 -0700 | [diff] [blame] | 5205 | tlb_flush = true; |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 5206 | } |
| 5207 | |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 5208 | for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) { |
| 5209 | if (VALID_PAGE(mmu->prev_roots[i].hpa) && |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 5210 | pcid == kvm_get_pcid(vcpu, mmu->prev_roots[i].pgd)) { |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 5211 | mmu->invlpg(vcpu, gva, mmu->prev_roots[i].hpa); |
| 5212 | tlb_flush = true; |
| 5213 | } |
Junaid Shahid | 956bf35 | 2018-06-27 14:59:18 -0700 | [diff] [blame] | 5214 | } |
Junaid Shahid | ade61e2 | 2018-06-27 14:59:15 -0700 | [diff] [blame] | 5215 | |
Junaid Shahid | faff875 | 2018-06-29 13:10:05 -0700 | [diff] [blame] | 5216 | if (tlb_flush) |
Jason Baron | b3646477 | 2021-01-14 22:27:56 -0500 | [diff] [blame] | 5217 | static_call(kvm_x86_tlb_flush_gva)(vcpu, gva); |
Junaid Shahid | faff875 | 2018-06-29 13:10:05 -0700 | [diff] [blame] | 5218 | |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 5219 | ++vcpu->stat.invlpg; |
| 5220 | |
| 5221 | /* |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 5222 | * 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 Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 5225 | */ |
| 5226 | } |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 5227 | |
Sean Christopherson | 8301305 | 2020-07-15 20:41:22 -0700 | [diff] [blame] | 5228 | void kvm_configure_mmu(bool enable_tdp, int tdp_max_root_level, |
| 5229 | int tdp_huge_page_level) |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 5230 | { |
Sean Christopherson | bde7723 | 2020-03-02 15:57:02 -0800 | [diff] [blame] | 5231 | tdp_enabled = enable_tdp; |
Sean Christopherson | 8301305 | 2020-07-15 20:41:22 -0700 | [diff] [blame] | 5232 | max_tdp_level = tdp_max_root_level; |
Sean Christopherson | 703c335 | 2020-03-02 15:57:03 -0800 | [diff] [blame] | 5233 | |
| 5234 | /* |
Sean Christopherson | 1d92d2e | 2020-07-15 20:41:21 -0700 | [diff] [blame] | 5235 | * max_huge_page_level reflects KVM's MMU capabilities irrespective |
Sean Christopherson | 703c335 | 2020-03-02 15:57:03 -0800 | [diff] [blame] | 5236 | * 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 Christopherson | 1d92d2e | 2020-07-15 20:41:21 -0700 | [diff] [blame] | 5242 | max_huge_page_level = tdp_huge_page_level; |
Sean Christopherson | 703c335 | 2020-03-02 15:57:03 -0800 | [diff] [blame] | 5243 | else if (boot_cpu_has(X86_FEATURE_GBPAGES)) |
Sean Christopherson | 1d92d2e | 2020-07-15 20:41:21 -0700 | [diff] [blame] | 5244 | max_huge_page_level = PG_LEVEL_1G; |
Sean Christopherson | 703c335 | 2020-03-02 15:57:03 -0800 | [diff] [blame] | 5245 | else |
Sean Christopherson | 1d92d2e | 2020-07-15 20:41:21 -0700 | [diff] [blame] | 5246 | max_huge_page_level = PG_LEVEL_2M; |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 5247 | } |
Sean Christopherson | bde7723 | 2020-03-02 15:57:02 -0800 | [diff] [blame] | 5248 | EXPORT_SYMBOL_GPL(kvm_configure_mmu); |
Xiao Guangrong | 13d268c | 2016-02-24 17:51:16 +0800 | [diff] [blame] | 5249 | |
Xiao Guangrong | 1bad2b2 | 2015-05-13 14:42:23 +0800 | [diff] [blame] | 5250 | /* The return value indicates if tlb flush on all vcpus is needed. */ |
Sean Christopherson | 0a234f5 | 2021-02-12 16:50:05 -0800 | [diff] [blame] | 5251 | typedef bool (*slot_level_handler) (struct kvm *kvm, struct kvm_rmap_head *rmap_head, |
| 5252 | struct kvm_memory_slot *slot); |
Xiao Guangrong | 1bad2b2 | 2015-05-13 14:42:23 +0800 | [diff] [blame] | 5253 | |
| 5254 | /* The caller should hold mmu-lock before calling this function. */ |
David Woodhouse | 928a4c3 | 2018-02-10 23:39:24 +0000 | [diff] [blame] | 5255 | static __always_inline bool |
Xiao Guangrong | 1bad2b2 | 2015-05-13 14:42:23 +0800 | [diff] [blame] | 5256 | slot_handle_level_range(struct kvm *kvm, struct kvm_memory_slot *memslot, |
| 5257 | slot_level_handler fn, int start_level, int end_level, |
Sean Christopherson | 1a61b7d | 2021-03-25 19:19:43 -0700 | [diff] [blame] | 5258 | gfn_t start_gfn, gfn_t end_gfn, bool flush_on_yield, |
| 5259 | bool flush) |
Xiao Guangrong | 1bad2b2 | 2015-05-13 14:42:23 +0800 | [diff] [blame] | 5260 | { |
| 5261 | struct slot_rmap_walk_iterator iterator; |
Xiao Guangrong | 1bad2b2 | 2015-05-13 14:42:23 +0800 | [diff] [blame] | 5262 | |
| 5263 | for_each_slot_rmap_range(memslot, start_level, end_level, start_gfn, |
| 5264 | end_gfn, &iterator) { |
| 5265 | if (iterator.rmap) |
Sean Christopherson | 0a234f5 | 2021-02-12 16:50:05 -0800 | [diff] [blame] | 5266 | flush |= fn(kvm, iterator.rmap, memslot); |
Xiao Guangrong | 1bad2b2 | 2015-05-13 14:42:23 +0800 | [diff] [blame] | 5267 | |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 5268 | if (need_resched() || rwlock_needbreak(&kvm->mmu_lock)) { |
Sean Christopherson | 302695a | 2021-03-25 19:19:41 -0700 | [diff] [blame] | 5269 | if (flush && flush_on_yield) { |
Ben Gardon | f285c63 | 2019-03-12 11:45:59 -0700 | [diff] [blame] | 5270 | kvm_flush_remote_tlbs_with_address(kvm, |
| 5271 | start_gfn, |
| 5272 | iterator.gfn - start_gfn + 1); |
Xiao Guangrong | 1bad2b2 | 2015-05-13 14:42:23 +0800 | [diff] [blame] | 5273 | flush = false; |
| 5274 | } |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 5275 | cond_resched_rwlock_write(&kvm->mmu_lock); |
Xiao Guangrong | 1bad2b2 | 2015-05-13 14:42:23 +0800 | [diff] [blame] | 5276 | } |
| 5277 | } |
| 5278 | |
Xiao Guangrong | 1bad2b2 | 2015-05-13 14:42:23 +0800 | [diff] [blame] | 5279 | return flush; |
| 5280 | } |
| 5281 | |
David Woodhouse | 928a4c3 | 2018-02-10 23:39:24 +0000 | [diff] [blame] | 5282 | static __always_inline bool |
Xiao Guangrong | 1bad2b2 | 2015-05-13 14:42:23 +0800 | [diff] [blame] | 5283 | slot_handle_level(struct kvm *kvm, struct kvm_memory_slot *memslot, |
| 5284 | slot_level_handler fn, int start_level, int end_level, |
Sean Christopherson | 302695a | 2021-03-25 19:19:41 -0700 | [diff] [blame] | 5285 | bool flush_on_yield) |
Xiao Guangrong | 1bad2b2 | 2015-05-13 14:42:23 +0800 | [diff] [blame] | 5286 | { |
| 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 Christopherson | 1a61b7d | 2021-03-25 19:19:43 -0700 | [diff] [blame] | 5290 | flush_on_yield, false); |
Xiao Guangrong | 1bad2b2 | 2015-05-13 14:42:23 +0800 | [diff] [blame] | 5291 | } |
| 5292 | |
David Woodhouse | 928a4c3 | 2018-02-10 23:39:24 +0000 | [diff] [blame] | 5293 | static __always_inline bool |
Xiao Guangrong | 1bad2b2 | 2015-05-13 14:42:23 +0800 | [diff] [blame] | 5294 | slot_handle_leaf(struct kvm *kvm, struct kvm_memory_slot *memslot, |
Sean Christopherson | 302695a | 2021-03-25 19:19:41 -0700 | [diff] [blame] | 5295 | slot_level_handler fn, bool flush_on_yield) |
Xiao Guangrong | 1bad2b2 | 2015-05-13 14:42:23 +0800 | [diff] [blame] | 5296 | { |
Sean Christopherson | 3bae045 | 2020-04-27 17:54:22 -0700 | [diff] [blame] | 5297 | return slot_handle_level(kvm, memslot, fn, PG_LEVEL_4K, |
Sean Christopherson | 302695a | 2021-03-25 19:19:41 -0700 | [diff] [blame] | 5298 | PG_LEVEL_4K, flush_on_yield); |
Xiao Guangrong | 1bad2b2 | 2015-05-13 14:42:23 +0800 | [diff] [blame] | 5299 | } |
| 5300 | |
Jiří Paleček | 1cfff4d | 2019-06-22 19:42:04 +0200 | [diff] [blame] | 5301 | static void free_mmu_pages(struct kvm_mmu *mmu) |
Takuya Yoshikawa | b99db1d | 2013-01-08 19:44:48 +0900 | [diff] [blame] | 5302 | { |
Sean Christopherson | 4a98623 | 2021-03-09 14:42:07 -0800 | [diff] [blame] | 5303 | if (!tdp_enabled && mmu->pae_root) |
| 5304 | set_memory_encrypted((unsigned long)mmu->pae_root, 1); |
Jiří Paleček | 1cfff4d | 2019-06-22 19:42:04 +0200 | [diff] [blame] | 5305 | free_page((unsigned long)mmu->pae_root); |
Sean Christopherson | 03ca458 | 2021-05-05 13:42:21 -0700 | [diff] [blame] | 5306 | free_page((unsigned long)mmu->pml4_root); |
Takuya Yoshikawa | 6b81b05 | 2013-01-08 19:47:33 +0900 | [diff] [blame] | 5307 | } |
Xiao Guangrong | 198c74f | 2014-04-17 17:06:16 +0800 | [diff] [blame] | 5308 | |
Sean Christopherson | 04d28e3 | 2020-09-23 09:33:14 -0700 | [diff] [blame] | 5309 | static int __kvm_mmu_create(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu) |
Avi Kivity | 8234b22 | 2010-12-27 12:08:45 +0200 | [diff] [blame] | 5310 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5311 | struct page *page; |
Takuya Yoshikawa | b99db1d | 2013-01-08 19:44:48 +0900 | [diff] [blame] | 5312 | int i; |
Takuya Yoshikawa | 9d1beef | 2013-01-08 19:46:48 +0900 | [diff] [blame] | 5313 | |
Sean Christopherson | 04d28e3 | 2020-09-23 09:33:14 -0700 | [diff] [blame] | 5314 | 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 Christopherson | b6b80c7 | 2019-06-13 10:22:23 -0700 | [diff] [blame] | 5320 | /* |
| 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 Christopherson | 04d4555 | 2021-03-04 17:10:46 -0800 | [diff] [blame] | 5325 | * 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 Christopherson | b6b80c7 | 2019-06-13 10:22:23 -0700 | [diff] [blame] | 5330 | */ |
Sean Christopherson | d468d94 | 2020-07-15 20:41:20 -0700 | [diff] [blame] | 5331 | if (tdp_enabled && kvm_mmu_get_tdp_level(vcpu) > PT32E_ROOT_LEVEL) |
Xiao Guangrong | 198c74f | 2014-04-17 17:06:16 +0800 | [diff] [blame] | 5332 | return 0; |
| 5333 | |
Ben Gardon | 254272c | 2019-02-11 11:02:50 -0800 | [diff] [blame] | 5334 | page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_DMA32); |
Xiao Guangrong | 198c74f | 2014-04-17 17:06:16 +0800 | [diff] [blame] | 5335 | if (!page) |
| 5336 | return -ENOMEM; |
| 5337 | |
Jiří Paleček | 1cfff4d | 2019-06-22 19:42:04 +0200 | [diff] [blame] | 5338 | mmu->pae_root = page_address(page); |
Sean Christopherson | 4a98623 | 2021-03-09 14:42:07 -0800 | [diff] [blame] | 5339 | |
| 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 Guangrong | 198c74f | 2014-04-17 17:06:16 +0800 | [diff] [blame] | 5353 | for (i = 0; i < 4; ++i) |
Sean Christopherson | c834e5e4 | 2021-03-09 14:42:06 -0800 | [diff] [blame] | 5354 | mmu->pae_root[i] = INVALID_PAE_ROOT; |
Xiao Guangrong | 198c74f | 2014-04-17 17:06:16 +0800 | [diff] [blame] | 5355 | |
| 5356 | return 0; |
| 5357 | } |
| 5358 | |
Kai Huang | d91ffee | 2015-01-12 15:28:54 +0800 | [diff] [blame] | 5359 | int kvm_mmu_create(struct kvm_vcpu *vcpu) |
| 5360 | { |
Jiří Paleček | 1cfff4d | 2019-06-22 19:42:04 +0200 | [diff] [blame] | 5361 | int ret; |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 5362 | |
Sean Christopherson | 5962bfb | 2020-07-02 19:35:25 -0700 | [diff] [blame] | 5363 | vcpu->arch.mmu_pte_list_desc_cache.kmem_cache = pte_list_desc_cache; |
Sean Christopherson | 5f6078f | 2020-07-02 19:35:34 -0700 | [diff] [blame] | 5364 | vcpu->arch.mmu_pte_list_desc_cache.gfp_zero = __GFP_ZERO; |
| 5365 | |
Sean Christopherson | 5962bfb | 2020-07-02 19:35:25 -0700 | [diff] [blame] | 5366 | vcpu->arch.mmu_page_header_cache.kmem_cache = mmu_page_header_cache; |
Sean Christopherson | 5f6078f | 2020-07-02 19:35:34 -0700 | [diff] [blame] | 5367 | vcpu->arch.mmu_page_header_cache.gfp_zero = __GFP_ZERO; |
Sean Christopherson | 5962bfb | 2020-07-02 19:35:25 -0700 | [diff] [blame] | 5368 | |
Sean Christopherson | 9688088 | 2020-07-02 19:35:35 -0700 | [diff] [blame] | 5369 | vcpu->arch.mmu_shadow_page_cache.gfp_zero = __GFP_ZERO; |
| 5370 | |
Wanpeng Li | 3ea3b7f | 2015-04-03 15:40:25 +0800 | [diff] [blame] | 5371 | vcpu->arch.mmu = &vcpu->arch.root_mmu; |
| 5372 | vcpu->arch.walk_mmu = &vcpu->arch.root_mmu; |
| 5373 | |
Wanpeng Li | 3ea3b7f | 2015-04-03 15:40:25 +0800 | [diff] [blame] | 5374 | vcpu->arch.nested_mmu.translate_gpa = translate_nested_gpa; |
Jiří Paleček | 1cfff4d | 2019-06-22 19:42:04 +0200 | [diff] [blame] | 5375 | |
Sean Christopherson | 04d28e3 | 2020-09-23 09:33:14 -0700 | [diff] [blame] | 5376 | ret = __kvm_mmu_create(vcpu, &vcpu->arch.guest_mmu); |
Jiří Paleček | 1cfff4d | 2019-06-22 19:42:04 +0200 | [diff] [blame] | 5377 | if (ret) |
| 5378 | return ret; |
| 5379 | |
Sean Christopherson | 04d28e3 | 2020-09-23 09:33:14 -0700 | [diff] [blame] | 5380 | ret = __kvm_mmu_create(vcpu, &vcpu->arch.root_mmu); |
Jiří Paleček | 1cfff4d | 2019-06-22 19:42:04 +0200 | [diff] [blame] | 5381 | 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 Li | 3ea3b7f | 2015-04-03 15:40:25 +0800 | [diff] [blame] | 5388 | } |
| 5389 | |
Sean Christopherson | fbb158c | 2019-09-12 19:46:07 -0700 | [diff] [blame] | 5390 | #define BATCH_ZAP_PAGES 10 |
Sean Christopherson | 002c5f7 | 2019-09-12 19:46:02 -0700 | [diff] [blame] | 5391 | static void kvm_zap_obsolete_pages(struct kvm *kvm) |
| 5392 | { |
| 5393 | struct kvm_mmu_page *sp, *node; |
Sean Christopherson | fbb158c | 2019-09-12 19:46:07 -0700 | [diff] [blame] | 5394 | int nr_zapped, batch = 0; |
Sean Christopherson | 002c5f7 | 2019-09-12 19:46:02 -0700 | [diff] [blame] | 5395 | |
| 5396 | restart: |
| 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 Christopherson | f95eec9 | 2020-06-23 12:35:39 -0700 | [diff] [blame] | 5407 | * 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 Christopherson | 002c5f7 | 2019-09-12 19:46:02 -0700 | [diff] [blame] | 5410 | */ |
Sean Christopherson | f95eec9 | 2020-06-23 12:35:39 -0700 | [diff] [blame] | 5411 | if (WARN_ON(sp->role.invalid)) |
Sean Christopherson | 002c5f7 | 2019-09-12 19:46:02 -0700 | [diff] [blame] | 5412 | continue; |
| 5413 | |
Sean Christopherson | 4506ecf | 2019-09-12 19:46:08 -0700 | [diff] [blame] | 5414 | /* |
| 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 Christopherson | fbb158c | 2019-09-12 19:46:07 -0700 | [diff] [blame] | 5420 | if (batch >= BATCH_ZAP_PAGES && |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 5421 | cond_resched_rwlock_write(&kvm->mmu_lock)) { |
Sean Christopherson | fbb158c | 2019-09-12 19:46:07 -0700 | [diff] [blame] | 5422 | batch = 0; |
Sean Christopherson | 002c5f7 | 2019-09-12 19:46:02 -0700 | [diff] [blame] | 5423 | goto restart; |
| 5424 | } |
| 5425 | |
Sean Christopherson | 1060520 | 2019-09-12 19:46:10 -0700 | [diff] [blame] | 5426 | if (__kvm_mmu_prepare_zap_page(kvm, sp, |
| 5427 | &kvm->arch.zapped_obsolete_pages, &nr_zapped)) { |
Sean Christopherson | fbb158c | 2019-09-12 19:46:07 -0700 | [diff] [blame] | 5428 | batch += nr_zapped; |
Sean Christopherson | 002c5f7 | 2019-09-12 19:46:02 -0700 | [diff] [blame] | 5429 | goto restart; |
Sean Christopherson | fbb158c | 2019-09-12 19:46:07 -0700 | [diff] [blame] | 5430 | } |
Sean Christopherson | 002c5f7 | 2019-09-12 19:46:02 -0700 | [diff] [blame] | 5431 | } |
| 5432 | |
Sean Christopherson | 4506ecf | 2019-09-12 19:46:08 -0700 | [diff] [blame] | 5433 | /* |
| 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 Christopherson | 1060520 | 2019-09-12 19:46:10 -0700 | [diff] [blame] | 5438 | kvm_mmu_commit_zap_page(kvm, &kvm->arch.zapped_obsolete_pages); |
Sean Christopherson | 002c5f7 | 2019-09-12 19:46:02 -0700 | [diff] [blame] | 5439 | } |
| 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 | */ |
| 5450 | static void kvm_mmu_zap_all_fast(struct kvm *kvm) |
| 5451 | { |
Sean Christopherson | ca333ad | 2019-09-12 19:46:11 -0700 | [diff] [blame] | 5452 | lockdep_assert_held(&kvm->slots_lock); |
| 5453 | |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 5454 | write_lock(&kvm->mmu_lock); |
Sean Christopherson | 14a3c4f | 2019-09-12 19:46:06 -0700 | [diff] [blame] | 5455 | trace_kvm_mmu_zap_all_fast(kvm); |
Sean Christopherson | ca333ad | 2019-09-12 19:46:11 -0700 | [diff] [blame] | 5456 | |
| 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 Christopherson | 002c5f7 | 2019-09-12 19:46:02 -0700 | [diff] [blame] | 5465 | |
Ben Gardon | b7cccd39 | 2021-04-01 16:37:35 -0700 | [diff] [blame] | 5466 | /* 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 Christopherson | 4506ecf | 2019-09-12 19:46:08 -0700 | [diff] [blame] | 5475 | /* |
| 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 Christopherson | 002c5f7 | 2019-09-12 19:46:02 -0700 | [diff] [blame] | 5485 | kvm_zap_obsolete_pages(kvm); |
Ben Gardon | faaf05b0 | 2020-10-14 11:26:47 -0700 | [diff] [blame] | 5486 | |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 5487 | write_unlock(&kvm->mmu_lock); |
Ben Gardon | 4c6654b | 2021-04-01 16:37:36 -0700 | [diff] [blame] | 5488 | |
| 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 Christopherson | 002c5f7 | 2019-09-12 19:46:02 -0700 | [diff] [blame] | 5494 | } |
| 5495 | |
Sean Christopherson | 1060520 | 2019-09-12 19:46:10 -0700 | [diff] [blame] | 5496 | static bool kvm_has_zapped_obsolete_pages(struct kvm *kvm) |
| 5497 | { |
| 5498 | return unlikely(!list_empty_careful(&kvm->arch.zapped_obsolete_pages)); |
| 5499 | } |
| 5500 | |
Wanpeng Li | 3ea3b7f | 2015-04-03 15:40:25 +0800 | [diff] [blame] | 5501 | static 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 Guangrong | decf633 | 2015-04-14 12:04:10 +0800 | [diff] [blame] | 5504 | { |
Sean Christopherson | 002c5f7 | 2019-09-12 19:46:02 -0700 | [diff] [blame] | 5505 | kvm_mmu_zap_all_fast(kvm); |
Xiao Guangrong | decf633 | 2015-04-14 12:04:10 +0800 | [diff] [blame] | 5506 | } |
| 5507 | |
Xiao Guangrong | efdfe53 | 2015-05-13 14:42:27 +0800 | [diff] [blame] | 5508 | void kvm_mmu_init_vm(struct kvm *kvm) |
| 5509 | { |
| 5510 | struct kvm_page_track_notifier_node *node = &kvm->arch.mmu_sp_tracker; |
| 5511 | |
Ben Gardon | fe5db27 | 2020-10-14 11:26:43 -0700 | [diff] [blame] | 5512 | kvm_mmu_init_tdp_mmu(kvm); |
| 5513 | |
Paolo Bonzini | 9da0e4d | 2015-05-18 13:33:16 +0200 | [diff] [blame] | 5514 | node->track_write = kvm_mmu_pte_write; |
Xiao Guangrong | efdfe53 | 2015-05-13 14:42:27 +0800 | [diff] [blame] | 5515 | node->track_flush_slot = kvm_mmu_invalidate_zap_pages_in_memslot; |
| 5516 | kvm_page_track_register_notifier(kvm, node); |
Paolo Bonzini | 9da0e4d | 2015-05-18 13:33:16 +0200 | [diff] [blame] | 5517 | } |
| 5518 | |
| 5519 | void kvm_mmu_uninit_vm(struct kvm *kvm) |
| 5520 | { |
Xiao Guangrong | efdfe53 | 2015-05-13 14:42:27 +0800 | [diff] [blame] | 5521 | struct kvm_page_track_notifier_node *node = &kvm->arch.mmu_sp_tracker; |
Paolo Bonzini | 9da0e4d | 2015-05-18 13:33:16 +0200 | [diff] [blame] | 5522 | |
| 5523 | kvm_page_track_unregister_notifier(kvm, node); |
Ben Gardon | fe5db27 | 2020-10-14 11:26:43 -0700 | [diff] [blame] | 5524 | |
| 5525 | kvm_mmu_uninit_tdp_mmu(kvm); |
Xiao Guangrong | efdfe53 | 2015-05-13 14:42:27 +0800 | [diff] [blame] | 5526 | } |
| 5527 | |
Xiao Guangrong | decf633 | 2015-04-14 12:04:10 +0800 | [diff] [blame] | 5528 | void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end) |
Xiao Guangrong | efdfe53 | 2015-05-13 14:42:27 +0800 | [diff] [blame] | 5529 | { |
| 5530 | struct kvm_memslots *slots; |
| 5531 | struct kvm_memory_slot *memslot; |
| 5532 | int i; |
Sean Christopherson | 1a61b7d | 2021-03-25 19:19:43 -0700 | [diff] [blame] | 5533 | bool flush = false; |
Xiao Guangrong | efdfe53 | 2015-05-13 14:42:27 +0800 | [diff] [blame] | 5534 | |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 5535 | write_lock(&kvm->mmu_lock); |
Xiao Guangrong | efdfe53 | 2015-05-13 14:42:27 +0800 | [diff] [blame] | 5536 | 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 Bonzini | 9da0e4d | 2015-05-18 13:33:16 +0200 | [diff] [blame] | 5541 | start = max(gfn_start, memslot->base_gfn); |
| 5542 | end = min(gfn_end, memslot->base_gfn + memslot->npages); |
| 5543 | if (start >= end) |
| 5544 | continue; |
Xiao Guangrong | efdfe53 | 2015-05-13 14:42:27 +0800 | [diff] [blame] | 5545 | |
Sean Christopherson | 302695a | 2021-03-25 19:19:41 -0700 | [diff] [blame] | 5546 | flush = slot_handle_level_range(kvm, memslot, kvm_zap_rmapp, |
| 5547 | PG_LEVEL_4K, |
| 5548 | KVM_MAX_HUGEPAGE_LEVEL, |
Sean Christopherson | 1a61b7d | 2021-03-25 19:19:43 -0700 | [diff] [blame] | 5549 | start, end - 1, true, flush); |
Paolo Bonzini | 9da0e4d | 2015-05-18 13:33:16 +0200 | [diff] [blame] | 5550 | } |
Xiao Guangrong | efdfe53 | 2015-05-13 14:42:27 +0800 | [diff] [blame] | 5551 | } |
| 5552 | |
Sean Christopherson | 1a61b7d | 2021-03-25 19:19:43 -0700 | [diff] [blame] | 5553 | if (flush) |
| 5554 | kvm_flush_remote_tlbs_with_address(kvm, gfn_start, gfn_end); |
Ben Gardon | faaf05b0 | 2020-10-14 11:26:47 -0700 | [diff] [blame] | 5555 | |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 5556 | write_unlock(&kvm->mmu_lock); |
Ben Gardon | 6103bc0 | 2021-04-01 16:37:32 -0700 | [diff] [blame] | 5557 | |
| 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 Guangrong | efdfe53 | 2015-05-13 14:42:27 +0800 | [diff] [blame] | 5571 | } |
| 5572 | |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 5573 | static bool slot_rmap_write_protect(struct kvm *kvm, |
Sean Christopherson | 0a234f5 | 2021-02-12 16:50:05 -0800 | [diff] [blame] | 5574 | struct kvm_rmap_head *rmap_head, |
| 5575 | struct kvm_memory_slot *slot) |
Xiao Guangrong | d77aa73 | 2015-05-13 14:42:24 +0800 | [diff] [blame] | 5576 | { |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 5577 | return __rmap_write_protect(kvm, rmap_head, false); |
Xiao Guangrong | d77aa73 | 2015-05-13 14:42:24 +0800 | [diff] [blame] | 5578 | } |
| 5579 | |
Dor Laor | e0fa826 | 2007-03-30 13:06:33 +0300 | [diff] [blame] | 5580 | void kvm_mmu_slot_remove_write_access(struct kvm *kvm, |
Jay Zhou | 3c9bd40 | 2020-02-27 09:32:27 +0800 | [diff] [blame] | 5581 | struct kvm_memory_slot *memslot, |
| 5582 | int start_level) |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 5583 | { |
Xiao Guangrong | d77aa73 | 2015-05-13 14:42:24 +0800 | [diff] [blame] | 5584 | bool flush; |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 5585 | |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 5586 | write_lock(&kvm->mmu_lock); |
Jay Zhou | 3c9bd40 | 2020-02-27 09:32:27 +0800 | [diff] [blame] | 5587 | flush = slot_handle_level(kvm, memslot, slot_rmap_write_protect, |
Sean Christopherson | e662ec3 | 2020-04-27 17:54:21 -0700 | [diff] [blame] | 5588 | start_level, KVM_MAX_HUGEPAGE_LEVEL, false); |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 5589 | write_unlock(&kvm->mmu_lock); |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 5590 | |
Ben Gardon | 24ae4cf | 2021-04-01 16:37:34 -0700 | [diff] [blame] | 5591 | 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 Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 5597 | /* |
Xiao Guangrong | 5304b8d | 2013-05-31 08:36:22 +0800 | [diff] [blame] | 5598 | * 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 Guangrong | e7d11c7 | 2013-05-31 08:36:27 +0800 | [diff] [blame] | 5600 | * readonly so that we only need to care the case of changing |
| 5601 | * spte from present to present (changing the spte from present |
Xiao Guangrong | 5304b8d | 2013-05-31 08:36:22 +0800 | [diff] [blame] | 5602 | * to nonpresent will flush all the TLBs immediately), in other |
| 5603 | * words, the only case we care is mmu_spte_update() where we |
Sean Christopherson | 5fc3424 | 2021-02-25 12:47:43 -0800 | [diff] [blame] | 5604 | * 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 Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 5607 | */ |
| 5608 | if (flush) |
Sean Christopherson | 7f42aa7 | 2020-02-18 13:07:36 -0800 | [diff] [blame] | 5609 | kvm_arch_flush_remote_tlbs_memslot(kvm, memslot); |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 5610 | } |
Wanpeng Li | 3ea3b7f | 2015-04-03 15:40:25 +0800 | [diff] [blame] | 5611 | |
| 5612 | static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm, |
Sean Christopherson | 0a234f5 | 2021-02-12 16:50:05 -0800 | [diff] [blame] | 5613 | struct kvm_rmap_head *rmap_head, |
| 5614 | struct kvm_memory_slot *slot) |
Wanpeng Li | 3ea3b7f | 2015-04-03 15:40:25 +0800 | [diff] [blame] | 5615 | { |
| 5616 | u64 *sptep; |
| 5617 | struct rmap_iterator iter; |
| 5618 | int need_tlb_flush = 0; |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 5619 | kvm_pfn_t pfn; |
Wanpeng Li | 3ea3b7f | 2015-04-03 15:40:25 +0800 | [diff] [blame] | 5620 | struct kvm_mmu_page *sp; |
| 5621 | |
| 5622 | restart: |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 5623 | for_each_rmap_spte(rmap_head, &iter, sptep) { |
Sean Christopherson | 5735468 | 2020-06-22 13:20:33 -0700 | [diff] [blame] | 5624 | sp = sptep_to_sp(sptep); |
Wanpeng Li | 3ea3b7f | 2015-04-03 15:40:25 +0800 | [diff] [blame] | 5625 | pfn = spte_to_pfn(*sptep); |
| 5626 | |
| 5627 | /* |
Xiao Guangrong | decf633 | 2015-04-14 12:04:10 +0800 | [diff] [blame] | 5628 | * 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 Li | 3ea3b7f | 2015-04-03 15:40:25 +0800 | [diff] [blame] | 5633 | */ |
Sean Christopherson | a78986a | 2019-11-11 14:12:27 -0800 | [diff] [blame] | 5634 | if (sp->role.direct && !kvm_is_reserved_pfn(pfn) && |
Sean Christopherson | 9eba50f | 2021-02-12 16:50:06 -0800 | [diff] [blame] | 5635 | sp->role.level < kvm_mmu_max_mapping_level(kvm, slot, sp->gfn, |
| 5636 | pfn, PG_LEVEL_NUM)) { |
Wei Yang | e791238 | 2018-10-04 10:04:23 +0800 | [diff] [blame] | 5637 | pte_list_remove(rmap_head, sptep); |
Lan Tianyu | 40ef75a | 2018-12-06 21:21:08 +0800 | [diff] [blame] | 5638 | |
| 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 Guangrong | 0d53679 | 2015-05-13 14:42:20 +0800 | [diff] [blame] | 5645 | goto restart; |
| 5646 | } |
Wanpeng Li | 3ea3b7f | 2015-04-03 15:40:25 +0800 | [diff] [blame] | 5647 | } |
| 5648 | |
| 5649 | return need_tlb_flush; |
| 5650 | } |
| 5651 | |
| 5652 | void kvm_mmu_zap_collapsible_sptes(struct kvm *kvm, |
Paolo Bonzini | f36f3f2 | 2015-05-18 13:20:23 +0200 | [diff] [blame] | 5653 | const struct kvm_memory_slot *memslot) |
Wanpeng Li | 3ea3b7f | 2015-04-03 15:40:25 +0800 | [diff] [blame] | 5654 | { |
Paolo Bonzini | f36f3f2 | 2015-05-18 13:20:23 +0200 | [diff] [blame] | 5655 | /* FIXME: const-ify all uses of struct kvm_memory_slot. */ |
Sean Christopherson | 9eba50f | 2021-02-12 16:50:06 -0800 | [diff] [blame] | 5656 | struct kvm_memory_slot *slot = (struct kvm_memory_slot *)memslot; |
Sean Christopherson | 302695a | 2021-03-25 19:19:41 -0700 | [diff] [blame] | 5657 | bool flush; |
Sean Christopherson | 9eba50f | 2021-02-12 16:50:06 -0800 | [diff] [blame] | 5658 | |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 5659 | write_lock(&kvm->mmu_lock); |
Sean Christopherson | 302695a | 2021-03-25 19:19:41 -0700 | [diff] [blame] | 5660 | flush = slot_handle_leaf(kvm, slot, kvm_mmu_zap_collapsible_spte, true); |
Ben Gardon | 1488199 | 2020-10-14 11:26:56 -0700 | [diff] [blame] | 5661 | |
Sean Christopherson | 302695a | 2021-03-25 19:19:41 -0700 | [diff] [blame] | 5662 | if (flush) |
| 5663 | kvm_arch_flush_remote_tlbs_memslot(kvm, slot); |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 5664 | write_unlock(&kvm->mmu_lock); |
Ben Gardon | 2db6f77 | 2021-04-01 16:37:33 -0700 | [diff] [blame] | 5665 | |
| 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 Li | 3ea3b7f | 2015-04-03 15:40:25 +0800 | [diff] [blame] | 5675 | } |
| 5676 | |
Sean Christopherson | b3594ff | 2020-02-18 13:07:34 -0800 | [diff] [blame] | 5677 | void kvm_arch_flush_remote_tlbs_memslot(struct kvm *kvm, |
Paolo Bonzini | 6c9dd6d | 2021-04-02 17:53:09 +0200 | [diff] [blame] | 5678 | const struct kvm_memory_slot *memslot) |
Sean Christopherson | b3594ff | 2020-02-18 13:07:34 -0800 | [diff] [blame] | 5679 | { |
| 5680 | /* |
Sean Christopherson | 7f42aa7 | 2020-02-18 13:07:36 -0800 | [diff] [blame] | 5681 | * All current use cases for flushing the TLBs for a specific memslot |
Sean Christopherson | 302695a | 2021-03-25 19:19:41 -0700 | [diff] [blame] | 5682 | * related to dirty logging, and many do the TLB flush out of mmu_lock. |
Sean Christopherson | 7f42aa7 | 2020-02-18 13:07:36 -0800 | [diff] [blame] | 5683 | * 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 Christopherson | b3594ff | 2020-02-18 13:07:34 -0800 | [diff] [blame] | 5686 | */ |
| 5687 | lockdep_assert_held(&kvm->slots_lock); |
Sean Christopherson | cec3764 | 2020-02-18 13:07:35 -0800 | [diff] [blame] | 5688 | kvm_flush_remote_tlbs_with_address(kvm, memslot->base_gfn, |
| 5689 | memslot->npages); |
Sean Christopherson | b3594ff | 2020-02-18 13:07:34 -0800 | [diff] [blame] | 5690 | } |
| 5691 | |
Kai Huang | f4b4b18 | 2015-01-28 10:54:24 +0800 | [diff] [blame] | 5692 | void kvm_mmu_slot_leaf_clear_dirty(struct kvm *kvm, |
| 5693 | struct kvm_memory_slot *memslot) |
| 5694 | { |
Xiao Guangrong | d77aa73 | 2015-05-13 14:42:24 +0800 | [diff] [blame] | 5695 | bool flush; |
Kai Huang | f4b4b18 | 2015-01-28 10:54:24 +0800 | [diff] [blame] | 5696 | |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 5697 | write_lock(&kvm->mmu_lock); |
Xiao Guangrong | d77aa73 | 2015-05-13 14:42:24 +0800 | [diff] [blame] | 5698 | flush = slot_handle_leaf(kvm, memslot, __rmap_clear_dirty, false); |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 5699 | write_unlock(&kvm->mmu_lock); |
Kai Huang | f4b4b18 | 2015-01-28 10:54:24 +0800 | [diff] [blame] | 5700 | |
Ben Gardon | 24ae4cf | 2021-04-01 16:37:34 -0700 | [diff] [blame] | 5701 | 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 Huang | f4b4b18 | 2015-01-28 10:54:24 +0800 | [diff] [blame] | 5707 | /* |
| 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 Christopherson | 7f42aa7 | 2020-02-18 13:07:36 -0800 | [diff] [blame] | 5714 | kvm_arch_flush_remote_tlbs_memslot(kvm, memslot); |
Kai Huang | f4b4b18 | 2015-01-28 10:54:24 +0800 | [diff] [blame] | 5715 | } |
Kai Huang | f4b4b18 | 2015-01-28 10:54:24 +0800 | [diff] [blame] | 5716 | |
Sean Christopherson | 92f58b5 | 2019-09-12 19:46:04 -0700 | [diff] [blame] | 5717 | void kvm_mmu_zap_all(struct kvm *kvm) |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 5718 | { |
| 5719 | struct kvm_mmu_page *sp, *node; |
Sean Christopherson | 7390de1 | 2019-02-05 13:01:31 -0800 | [diff] [blame] | 5720 | LIST_HEAD(invalid_list); |
Sean Christopherson | 83cdb56 | 2019-02-05 13:01:35 -0800 | [diff] [blame] | 5721 | int ign; |
Xiao Guangrong | 5304b8d | 2013-05-31 08:36:22 +0800 | [diff] [blame] | 5722 | |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 5723 | write_lock(&kvm->mmu_lock); |
Xiao Guangrong | 5304b8d | 2013-05-31 08:36:22 +0800 | [diff] [blame] | 5724 | restart: |
Sean Christopherson | 8a674ad | 2019-02-05 13:01:32 -0800 | [diff] [blame] | 5725 | list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link) { |
Sean Christopherson | f95eec9 | 2020-06-23 12:35:39 -0700 | [diff] [blame] | 5726 | if (WARN_ON(sp->role.invalid)) |
Sean Christopherson | 8a674ad | 2019-02-05 13:01:32 -0800 | [diff] [blame] | 5727 | continue; |
Sean Christopherson | 92f58b5 | 2019-09-12 19:46:04 -0700 | [diff] [blame] | 5728 | if (__kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list, &ign)) |
Xiao Guangrong | 5304b8d | 2013-05-31 08:36:22 +0800 | [diff] [blame] | 5729 | goto restart; |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 5730 | if (cond_resched_rwlock_write(&kvm->mmu_lock)) |
Xiao Guangrong | 5304b8d | 2013-05-31 08:36:22 +0800 | [diff] [blame] | 5731 | goto restart; |
| 5732 | } |
| 5733 | |
Sean Christopherson | 4771450 | 2019-02-05 13:01:23 -0800 | [diff] [blame] | 5734 | kvm_mmu_commit_zap_page(kvm, &invalid_list); |
Ben Gardon | faaf05b0 | 2020-10-14 11:26:47 -0700 | [diff] [blame] | 5735 | |
Paolo Bonzini | 897218f | 2021-02-06 09:53:33 -0500 | [diff] [blame] | 5736 | if (is_tdp_mmu_enabled(kvm)) |
Ben Gardon | faaf05b0 | 2020-10-14 11:26:47 -0700 | [diff] [blame] | 5737 | kvm_tdp_mmu_zap_all(kvm); |
| 5738 | |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 5739 | write_unlock(&kvm->mmu_lock); |
Xiao Guangrong | 5304b8d | 2013-05-31 08:36:22 +0800 | [diff] [blame] | 5740 | } |
| 5741 | |
Sean Christopherson | 1524825 | 2019-02-05 12:54:17 -0800 | [diff] [blame] | 5742 | void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm, u64 gen) |
Xiao Guangrong | f8f5594 | 2013-06-07 16:51:26 +0800 | [diff] [blame] | 5743 | { |
Sean Christopherson | 164bf7e | 2019-02-05 13:01:18 -0800 | [diff] [blame] | 5744 | WARN_ON(gen & KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS); |
Sean Christopherson | e1359e2 | 2019-02-05 13:01:12 -0800 | [diff] [blame] | 5745 | |
Sean Christopherson | 164bf7e | 2019-02-05 13:01:18 -0800 | [diff] [blame] | 5746 | gen &= MMIO_SPTE_GEN_MASK; |
Sean Christopherson | e1359e2 | 2019-02-05 13:01:12 -0800 | [diff] [blame] | 5747 | |
Xiao Guangrong | f8f5594 | 2013-06-07 16:51:26 +0800 | [diff] [blame] | 5748 | /* |
Sean Christopherson | e1359e2 | 2019-02-05 13:01:12 -0800 | [diff] [blame] | 5749 | * 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 Guangrong | f8f5594 | 2013-06-07 16:51:26 +0800 | [diff] [blame] | 5759 | * zap all shadow pages. |
Xiao Guangrong | f8f5594 | 2013-06-07 16:51:26 +0800 | [diff] [blame] | 5760 | */ |
Sean Christopherson | e1359e2 | 2019-02-05 13:01:12 -0800 | [diff] [blame] | 5761 | if (unlikely(gen == 0)) { |
Bandan Das | ae0f549 | 2016-11-15 01:36:18 -0500 | [diff] [blame] | 5762 | kvm_debug_ratelimited("kvm: zapping shadow pages for mmio generation wraparound\n"); |
Sean Christopherson | 92f58b5 | 2019-09-12 19:46:04 -0700 | [diff] [blame] | 5763 | kvm_mmu_zap_all_fast(kvm); |
Takuya Yoshikawa | 7a2e8aa | 2013-06-21 01:34:31 +0900 | [diff] [blame] | 5764 | } |
Xiao Guangrong | f8f5594 | 2013-06-07 16:51:26 +0800 | [diff] [blame] | 5765 | } |
| 5766 | |
Dave Chinner | 70534a7 | 2013-08-28 10:18:14 +1000 | [diff] [blame] | 5767 | static unsigned long |
| 5768 | mmu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc) |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 5769 | { |
| 5770 | struct kvm *kvm; |
Ying Han | 1495f23 | 2011-05-24 17:12:27 -0700 | [diff] [blame] | 5771 | int nr_to_scan = sc->nr_to_scan; |
Dave Chinner | 70534a7 | 2013-08-28 10:18:14 +1000 | [diff] [blame] | 5772 | unsigned long freed = 0; |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 5773 | |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 5774 | mutex_lock(&kvm_lock); |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 5775 | |
| 5776 | list_for_each_entry(kvm, &vm_list, vm_list) { |
Jan Kiszka | 3d56cbd | 2011-12-02 18:35:24 +0100 | [diff] [blame] | 5777 | int idx; |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 5778 | LIST_HEAD(invalid_list); |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 5779 | |
Gleb Natapov | 1952639 | 2012-06-04 14:53:23 +0300 | [diff] [blame] | 5780 | /* |
Takuya Yoshikawa | 35f2d16 | 2012-08-20 18:35:39 +0900 | [diff] [blame] | 5781 | * 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 Natapov | 1952639 | 2012-06-04 14:53:23 +0300 | [diff] [blame] | 5789 | * 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 Christopherson | 1060520 | 2019-09-12 19:46:10 -0700 | [diff] [blame] | 5794 | if (!kvm->arch.n_used_mmu_pages && |
| 5795 | !kvm_has_zapped_obsolete_pages(kvm)) |
Gleb Natapov | 1952639 | 2012-06-04 14:53:23 +0300 | [diff] [blame] | 5796 | continue; |
Gleb Natapov | 1952639 | 2012-06-04 14:53:23 +0300 | [diff] [blame] | 5797 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5798 | idx = srcu_read_lock(&kvm->srcu); |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 5799 | write_lock(&kvm->mmu_lock); |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 5800 | |
Sean Christopherson | 1060520 | 2019-09-12 19:46:10 -0700 | [diff] [blame] | 5801 | 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 Christopherson | ebdb292 | 2020-06-23 12:35:41 -0700 | [diff] [blame] | 5807 | freed = kvm_mmu_zap_oldest_mmu_pages(kvm, sc->nr_to_scan); |
Gleb Natapov | 1952639 | 2012-06-04 14:53:23 +0300 | [diff] [blame] | 5808 | |
Sean Christopherson | 1060520 | 2019-09-12 19:46:10 -0700 | [diff] [blame] | 5809 | unlock: |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 5810 | write_unlock(&kvm->mmu_lock); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5811 | srcu_read_unlock(&kvm->srcu, idx); |
Gleb Natapov | 1952639 | 2012-06-04 14:53:23 +0300 | [diff] [blame] | 5812 | |
Dave Chinner | 70534a7 | 2013-08-28 10:18:14 +1000 | [diff] [blame] | 5813 | /* |
| 5814 | * unfair on small ones |
| 5815 | * per-vm shrinkers cry out |
| 5816 | * sadness comes quickly |
| 5817 | */ |
Gleb Natapov | 1952639 | 2012-06-04 14:53:23 +0300 | [diff] [blame] | 5818 | list_move_tail(&kvm->vm_list, &vm_list); |
| 5819 | break; |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 5820 | } |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 5821 | |
Junaid Shahid | 0d9ce16 | 2019-01-03 17:14:28 -0800 | [diff] [blame] | 5822 | mutex_unlock(&kvm_lock); |
Dave Chinner | 70534a7 | 2013-08-28 10:18:14 +1000 | [diff] [blame] | 5823 | return freed; |
Dave Chinner | 70534a7 | 2013-08-28 10:18:14 +1000 | [diff] [blame] | 5824 | } |
| 5825 | |
| 5826 | static unsigned long |
| 5827 | mmu_shrink_count(struct shrinker *shrink, struct shrink_control *sc) |
| 5828 | { |
Dave Hansen | 45221ab | 2010-08-19 18:11:37 -0700 | [diff] [blame] | 5829 | return percpu_counter_read_positive(&kvm_total_used_mmu_pages); |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 5830 | } |
| 5831 | |
| 5832 | static struct shrinker mmu_shrinker = { |
Dave Chinner | 70534a7 | 2013-08-28 10:18:14 +1000 | [diff] [blame] | 5833 | .count_objects = mmu_shrink_count, |
| 5834 | .scan_objects = mmu_shrink_scan, |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 5835 | .seeks = DEFAULT_SEEKS * 10, |
| 5836 | }; |
| 5837 | |
Ingo Molnar | 2ddfd20 | 2008-05-22 10:37:48 +0200 | [diff] [blame] | 5838 | static void mmu_destroy_caches(void) |
Dor Laor | e0fa826 | 2007-03-30 13:06:33 +0300 | [diff] [blame] | 5839 | { |
Tim Hansen | c1bd743 | 2017-10-07 23:15:23 -0400 | [diff] [blame] | 5840 | kmem_cache_destroy(pte_list_desc_cache); |
| 5841 | kmem_cache_destroy(mmu_page_header_cache); |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 5842 | } |
| 5843 | |
Paolo Bonzini | b8e8c83 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 5844 | static 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 | |
| 5850 | static void __set_nx_huge_pages(bool val) |
| 5851 | { |
| 5852 | nx_huge_pages = itlb_multihit_kvm_mitigation = val; |
| 5853 | } |
| 5854 | |
| 5855 | static 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 Bonzini | b8e8c83 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 5874 | |
| 5875 | mutex_lock(&kvm_lock); |
| 5876 | |
| 5877 | list_for_each_entry(kvm, &vm_list, vm_list) { |
Sean Christopherson | ed69a6c | 2019-11-13 11:30:32 -0800 | [diff] [blame] | 5878 | mutex_lock(&kvm->slots_lock); |
Paolo Bonzini | b8e8c83 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 5879 | kvm_mmu_zap_all_fast(kvm); |
Sean Christopherson | ed69a6c | 2019-11-13 11:30:32 -0800 | [diff] [blame] | 5880 | mutex_unlock(&kvm->slots_lock); |
Junaid Shahid | 1aa9b95 | 2019-11-04 20:26:00 +0100 | [diff] [blame] | 5881 | |
| 5882 | wake_up_process(kvm->arch.nx_lpage_recovery_thread); |
Paolo Bonzini | b8e8c83 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 5883 | } |
| 5884 | mutex_unlock(&kvm_lock); |
| 5885 | } |
| 5886 | |
| 5887 | return 0; |
| 5888 | } |
| 5889 | |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 5890 | int kvm_mmu_module_init(void) |
| 5891 | { |
Arnd Bergmann | ab271bd | 2018-01-10 17:26:59 +0100 | [diff] [blame] | 5892 | int ret = -ENOMEM; |
| 5893 | |
Paolo Bonzini | b8e8c83 | 2019-11-04 12:22:02 +0100 | [diff] [blame] | 5894 | if (nx_huge_pages == -1) |
| 5895 | __set_nx_huge_pages(get_nx_auto_mode()); |
| 5896 | |
Vitaly Kuznetsov | 36d9594d | 2018-10-08 21:28:10 +0200 | [diff] [blame] | 5897 | /* |
| 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 Shahid | 28a1f3a | 2018-08-14 10:15:34 -0700 | [diff] [blame] | 5907 | kvm_mmu_reset_all_pte_masks(); |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 5908 | |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 5909 | pte_list_desc_cache = kmem_cache_create("pte_list_desc", |
| 5910 | sizeof(struct pte_list_desc), |
Shakeel Butt | 46bea48 | 2017-10-05 18:07:24 -0700 | [diff] [blame] | 5911 | 0, SLAB_ACCOUNT, NULL); |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 5912 | if (!pte_list_desc_cache) |
Arnd Bergmann | ab271bd | 2018-01-10 17:26:59 +0100 | [diff] [blame] | 5913 | goto out; |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 5914 | |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 5915 | mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header", |
| 5916 | sizeof(struct kvm_mmu_page), |
Shakeel Butt | 46bea48 | 2017-10-05 18:07:24 -0700 | [diff] [blame] | 5917 | 0, SLAB_ACCOUNT, NULL); |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 5918 | if (!mmu_page_header_cache) |
Arnd Bergmann | ab271bd | 2018-01-10 17:26:59 +0100 | [diff] [blame] | 5919 | goto out; |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 5920 | |
Tejun Heo | 908c7f1 | 2014-09-08 09:51:29 +0900 | [diff] [blame] | 5921 | if (percpu_counter_init(&kvm_total_used_mmu_pages, 0, GFP_KERNEL)) |
Arnd Bergmann | ab271bd | 2018-01-10 17:26:59 +0100 | [diff] [blame] | 5922 | goto out; |
Wei Yongjun | 45bf21a | 2010-08-23 16:13:15 +0800 | [diff] [blame] | 5923 | |
Arnd Bergmann | ab271bd | 2018-01-10 17:26:59 +0100 | [diff] [blame] | 5924 | ret = register_shrinker(&mmu_shrinker); |
| 5925 | if (ret) |
| 5926 | goto out; |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 5927 | |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 5928 | return 0; |
| 5929 | |
Arnd Bergmann | ab271bd | 2018-01-10 17:26:59 +0100 | [diff] [blame] | 5930 | out: |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 5931 | mmu_destroy_caches(); |
Arnd Bergmann | ab271bd | 2018-01-10 17:26:59 +0100 | [diff] [blame] | 5932 | return ret; |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 5933 | } |
| 5934 | |
Zhang Xiantao | 3ad82a7 | 2007-11-20 13:11:38 +0800 | [diff] [blame] | 5935 | /* |
Peng Hao | 39337ad | 2018-10-04 11:45:00 -0400 | [diff] [blame] | 5936 | * Calculate mmu pages needed for kvm. |
Zhang Xiantao | 3ad82a7 | 2007-11-20 13:11:38 +0800 | [diff] [blame] | 5937 | */ |
Ben Gardon | bc8a3d8 | 2019-04-08 11:07:30 -0700 | [diff] [blame] | 5938 | unsigned long kvm_mmu_calculate_default_mmu_pages(struct kvm *kvm) |
Zhang Xiantao | 3ad82a7 | 2007-11-20 13:11:38 +0800 | [diff] [blame] | 5939 | { |
Ben Gardon | bc8a3d8 | 2019-04-08 11:07:30 -0700 | [diff] [blame] | 5940 | unsigned long nr_mmu_pages; |
| 5941 | unsigned long nr_pages = 0; |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 5942 | struct kvm_memslots *slots; |
Xiao Guangrong | be6ba0f | 2011-11-24 17:39:18 +0800 | [diff] [blame] | 5943 | struct kvm_memory_slot *memslot; |
Paolo Bonzini | 9da0e4d | 2015-05-18 13:33:16 +0200 | [diff] [blame] | 5944 | int i; |
Zhang Xiantao | 3ad82a7 | 2007-11-20 13:11:38 +0800 | [diff] [blame] | 5945 | |
Paolo Bonzini | 9da0e4d | 2015-05-18 13:33:16 +0200 | [diff] [blame] | 5946 | for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { |
| 5947 | slots = __kvm_memslots(kvm, i); |
Lai Jiangshan | 90d83dc | 2010-04-19 17:41:23 +0800 | [diff] [blame] | 5948 | |
Paolo Bonzini | 9da0e4d | 2015-05-18 13:33:16 +0200 | [diff] [blame] | 5949 | kvm_for_each_memslot(memslot, slots) |
| 5950 | nr_pages += memslot->npages; |
| 5951 | } |
Zhang Xiantao | 3ad82a7 | 2007-11-20 13:11:38 +0800 | [diff] [blame] | 5952 | |
| 5953 | nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000; |
Ben Gardon | bc8a3d8 | 2019-04-08 11:07:30 -0700 | [diff] [blame] | 5954 | nr_mmu_pages = max(nr_mmu_pages, KVM_MIN_ALLOC_MMU_PAGES); |
Zhang Xiantao | 3ad82a7 | 2007-11-20 13:11:38 +0800 | [diff] [blame] | 5955 | |
| 5956 | return nr_mmu_pages; |
| 5957 | } |
| 5958 | |
Xiao Guangrong | c42fffe | 2010-09-27 18:07:07 +0800 | [diff] [blame] | 5959 | void kvm_mmu_destroy(struct kvm_vcpu *vcpu) |
| 5960 | { |
Paolo Bonzini | 95f93af | 2013-10-02 16:56:12 +0200 | [diff] [blame] | 5961 | kvm_mmu_unload(vcpu); |
Jiří Paleček | 1cfff4d | 2019-06-22 19:42:04 +0200 | [diff] [blame] | 5962 | free_mmu_pages(&vcpu->arch.root_mmu); |
| 5963 | free_mmu_pages(&vcpu->arch.guest_mmu); |
Xiao Guangrong | c42fffe | 2010-09-27 18:07:07 +0800 | [diff] [blame] | 5964 | mmu_free_memory_caches(vcpu); |
Xiao Guangrong | b034cf0 | 2010-12-23 16:08:35 +0800 | [diff] [blame] | 5965 | } |
| 5966 | |
Xiao Guangrong | b034cf0 | 2010-12-23 16:08:35 +0800 | [diff] [blame] | 5967 | void 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 Guangrong | c42fffe | 2010-09-27 18:07:07 +0800 | [diff] [blame] | 5972 | mmu_audit_disable(); |
| 5973 | } |
Junaid Shahid | 1aa9b95 | 2019-11-04 20:26:00 +0100 | [diff] [blame] | 5974 | |
| 5975 | static 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 | |
| 6000 | static 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 Christopherson | 048f498 | 2021-03-25 13:01:18 -0700 | [diff] [blame] | 6006 | bool flush = false; |
Junaid Shahid | 1aa9b95 | 2019-11-04 20:26:00 +0100 | [diff] [blame] | 6007 | ulong to_zap; |
| 6008 | |
| 6009 | rcu_idx = srcu_read_lock(&kvm->srcu); |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 6010 | write_lock(&kvm->mmu_lock); |
Junaid Shahid | 1aa9b95 | 2019-11-04 20:26:00 +0100 | [diff] [blame] | 6011 | |
| 6012 | ratio = READ_ONCE(nx_huge_pages_recovery_ratio); |
| 6013 | to_zap = ratio ? DIV_ROUND_UP(kvm->stat.nx_lpage_splits, ratio) : 0; |
Sean Christopherson | 7d919c7 | 2020-09-23 11:37:29 -0700 | [diff] [blame] | 6014 | for ( ; to_zap; --to_zap) { |
| 6015 | if (list_empty(&kvm->arch.lpage_disallowed_mmu_pages)) |
| 6016 | break; |
| 6017 | |
Junaid Shahid | 1aa9b95 | 2019-11-04 20:26:00 +0100 | [diff] [blame] | 6018 | /* |
| 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 Bonzini | 897218f | 2021-02-06 09:53:33 -0500 | [diff] [blame] | 6027 | if (is_tdp_mmu_page(sp)) { |
Paolo Bonzini | 315f02c | 2021-04-06 11:08:51 -0400 | [diff] [blame] | 6028 | flush |= kvm_tdp_mmu_zap_sp(kvm, sp); |
Ben Gardon | 8d1a182 | 2021-02-02 10:57:15 -0800 | [diff] [blame] | 6029 | } else { |
Ben Gardon | 29cf0f5 | 2020-10-14 11:27:00 -0700 | [diff] [blame] | 6030 | kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list); |
| 6031 | WARN_ON_ONCE(sp->lpage_disallowed); |
| 6032 | } |
Junaid Shahid | 1aa9b95 | 2019-11-04 20:26:00 +0100 | [diff] [blame] | 6033 | |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 6034 | if (need_resched() || rwlock_needbreak(&kvm->mmu_lock)) { |
Sean Christopherson | 048f498 | 2021-03-25 13:01:18 -0700 | [diff] [blame] | 6035 | kvm_mmu_remote_flush_or_zap(kvm, &invalid_list, flush); |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 6036 | cond_resched_rwlock_write(&kvm->mmu_lock); |
Sean Christopherson | 048f498 | 2021-03-25 13:01:18 -0700 | [diff] [blame] | 6037 | flush = false; |
Junaid Shahid | 1aa9b95 | 2019-11-04 20:26:00 +0100 | [diff] [blame] | 6038 | } |
| 6039 | } |
Sean Christopherson | 048f498 | 2021-03-25 13:01:18 -0700 | [diff] [blame] | 6040 | kvm_mmu_remote_flush_or_zap(kvm, &invalid_list, flush); |
Junaid Shahid | 1aa9b95 | 2019-11-04 20:26:00 +0100 | [diff] [blame] | 6041 | |
Ben Gardon | 531810c | 2021-02-02 10:57:24 -0800 | [diff] [blame] | 6042 | write_unlock(&kvm->mmu_lock); |
Junaid Shahid | 1aa9b95 | 2019-11-04 20:26:00 +0100 | [diff] [blame] | 6043 | srcu_read_unlock(&kvm->srcu, rcu_idx); |
| 6044 | } |
| 6045 | |
| 6046 | static 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 | |
| 6053 | static 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 | |
| 6078 | int 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 | |
| 6091 | void 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 | } |