|
2 | 2 |
|
3 | 3 | #include "mmu.h"
|
4 | 4 | #include "mmu_internal.h"
|
| 5 | +#include "tdp_iter.h" |
5 | 6 | #include "tdp_mmu.h"
|
6 | 7 | #include "spte.h"
|
7 | 8 |
|
@@ -130,3 +131,114 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu)
|
130 | 131 |
|
131 | 132 | return __pa(root->spt);
|
132 | 133 | }
|
| 134 | + |
| 135 | +static void handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn, |
| 136 | + u64 old_spte, u64 new_spte, int level); |
| 137 | + |
| 138 | +/** |
| 139 | + * handle_changed_spte - handle bookkeeping associated with an SPTE change |
| 140 | + * @kvm: kvm instance |
| 141 | + * @as_id: the address space of the paging structure the SPTE was a part of |
| 142 | + * @gfn: the base GFN that was mapped by the SPTE |
| 143 | + * @old_spte: The value of the SPTE before the change |
| 144 | + * @new_spte: The value of the SPTE after the change |
| 145 | + * @level: the level of the PT the SPTE is part of in the paging structure |
| 146 | + * |
| 147 | + * Handle bookkeeping that might result from the modification of a SPTE. |
| 148 | + * This function must be called for all TDP SPTE modifications. |
| 149 | + */ |
| 150 | +static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn, |
| 151 | + u64 old_spte, u64 new_spte, int level) |
| 152 | +{ |
| 153 | + bool was_present = is_shadow_present_pte(old_spte); |
| 154 | + bool is_present = is_shadow_present_pte(new_spte); |
| 155 | + bool was_leaf = was_present && is_last_spte(old_spte, level); |
| 156 | + bool is_leaf = is_present && is_last_spte(new_spte, level); |
| 157 | + bool pfn_changed = spte_to_pfn(old_spte) != spte_to_pfn(new_spte); |
| 158 | + u64 *pt; |
| 159 | + u64 old_child_spte; |
| 160 | + int i; |
| 161 | + |
| 162 | + WARN_ON(level > PT64_ROOT_MAX_LEVEL); |
| 163 | + WARN_ON(level < PG_LEVEL_4K); |
| 164 | + WARN_ON(gfn % KVM_PAGES_PER_HPAGE(level)); |
| 165 | + |
| 166 | + /* |
| 167 | + * If this warning were to trigger it would indicate that there was a |
| 168 | + * missing MMU notifier or a race with some notifier handler. |
| 169 | + * A present, leaf SPTE should never be directly replaced with another |
| 170 | + * present leaf SPTE pointing to a differnt PFN. A notifier handler |
| 171 | + * should be zapping the SPTE before the main MM's page table is |
| 172 | + * changed, or the SPTE should be zeroed, and the TLBs flushed by the |
| 173 | + * thread before replacement. |
| 174 | + */ |
| 175 | + if (was_leaf && is_leaf && pfn_changed) { |
| 176 | + pr_err("Invalid SPTE change: cannot replace a present leaf\n" |
| 177 | + "SPTE with another present leaf SPTE mapping a\n" |
| 178 | + "different PFN!\n" |
| 179 | + "as_id: %d gfn: %llx old_spte: %llx new_spte: %llx level: %d", |
| 180 | + as_id, gfn, old_spte, new_spte, level); |
| 181 | + |
| 182 | + /* |
| 183 | + * Crash the host to prevent error propagation and guest data |
| 184 | + * courruption. |
| 185 | + */ |
| 186 | + BUG(); |
| 187 | + } |
| 188 | + |
| 189 | + if (old_spte == new_spte) |
| 190 | + return; |
| 191 | + |
| 192 | + /* |
| 193 | + * The only times a SPTE should be changed from a non-present to |
| 194 | + * non-present state is when an MMIO entry is installed/modified/ |
| 195 | + * removed. In that case, there is nothing to do here. |
| 196 | + */ |
| 197 | + if (!was_present && !is_present) { |
| 198 | + /* |
| 199 | + * If this change does not involve a MMIO SPTE, it is |
| 200 | + * unexpected. Log the change, though it should not impact the |
| 201 | + * guest since both the former and current SPTEs are nonpresent. |
| 202 | + */ |
| 203 | + if (WARN_ON(!is_mmio_spte(old_spte) && !is_mmio_spte(new_spte))) |
| 204 | + pr_err("Unexpected SPTE change! Nonpresent SPTEs\n" |
| 205 | + "should not be replaced with another,\n" |
| 206 | + "different nonpresent SPTE, unless one or both\n" |
| 207 | + "are MMIO SPTEs.\n" |
| 208 | + "as_id: %d gfn: %llx old_spte: %llx new_spte: %llx level: %d", |
| 209 | + as_id, gfn, old_spte, new_spte, level); |
| 210 | + return; |
| 211 | + } |
| 212 | + |
| 213 | + |
| 214 | + if (was_leaf && is_dirty_spte(old_spte) && |
| 215 | + (!is_dirty_spte(new_spte) || pfn_changed)) |
| 216 | + kvm_set_pfn_dirty(spte_to_pfn(old_spte)); |
| 217 | + |
| 218 | + /* |
| 219 | + * Recursively handle child PTs if the change removed a subtree from |
| 220 | + * the paging structure. |
| 221 | + */ |
| 222 | + if (was_present && !was_leaf && (pfn_changed || !is_present)) { |
| 223 | + pt = spte_to_child_pt(old_spte, level); |
| 224 | + |
| 225 | + for (i = 0; i < PT64_ENT_PER_PAGE; i++) { |
| 226 | + old_child_spte = READ_ONCE(*(pt + i)); |
| 227 | + WRITE_ONCE(*(pt + i), 0); |
| 228 | + handle_changed_spte(kvm, as_id, |
| 229 | + gfn + (i * KVM_PAGES_PER_HPAGE(level - 1)), |
| 230 | + old_child_spte, 0, level - 1); |
| 231 | + } |
| 232 | + |
| 233 | + kvm_flush_remote_tlbs_with_address(kvm, gfn, |
| 234 | + KVM_PAGES_PER_HPAGE(level)); |
| 235 | + |
| 236 | + free_page((unsigned long)pt); |
| 237 | + } |
| 238 | +} |
| 239 | + |
| 240 | +static void handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn, |
| 241 | + u64 old_spte, u64 new_spte, int level) |
| 242 | +{ |
| 243 | + __handle_changed_spte(kvm, as_id, gfn, old_spte, new_spte, level); |
| 244 | +} |
0 commit comments