Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Kernel-based Virtual Machine driver for Linux |
| 4 | * |
| 5 | * AMD SVM support |
| 6 | * |
| 7 | * Copyright (C) 2006 Qumranet, Inc. |
| 8 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
| 9 | * |
| 10 | * Authors: |
| 11 | * Yaniv Kamay <yaniv@qumranet.com> |
| 12 | * Avi Kivity <avi@qumranet.com> |
| 13 | */ |
| 14 | |
| 15 | #define pr_fmt(fmt) "SVM: " fmt |
| 16 | |
| 17 | #include <linux/kvm_types.h> |
| 18 | #include <linux/kvm_host.h> |
| 19 | #include <linux/kernel.h> |
| 20 | |
| 21 | #include <asm/msr-index.h> |
Paolo Bonzini | 5679b80 | 2020-05-04 11:28:25 -0400 | [diff] [blame] | 22 | #include <asm/debugreg.h> |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 23 | |
| 24 | #include "kvm_emulate.h" |
| 25 | #include "trace.h" |
| 26 | #include "mmu.h" |
| 27 | #include "x86.h" |
Paolo Bonzini | cc440cd | 2020-05-13 13:36:32 -0400 | [diff] [blame] | 28 | #include "cpuid.h" |
Paolo Bonzini | 5b672408 | 2020-05-16 08:50:35 -0400 | [diff] [blame] | 29 | #include "lapic.h" |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 30 | #include "svm.h" |
| 31 | |
Sean Christopherson | 11f0cbf | 2021-02-03 16:01:17 -0800 | [diff] [blame] | 32 | #define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK |
| 33 | |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 34 | static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu, |
| 35 | struct x86_exception *fault) |
| 36 | { |
| 37 | struct vcpu_svm *svm = to_svm(vcpu); |
| 38 | |
| 39 | if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) { |
| 40 | /* |
| 41 | * TODO: track the cause of the nested page fault, and |
| 42 | * correctly fill in the high bits of exit_info_1. |
| 43 | */ |
| 44 | svm->vmcb->control.exit_code = SVM_EXIT_NPF; |
| 45 | svm->vmcb->control.exit_code_hi = 0; |
| 46 | svm->vmcb->control.exit_info_1 = (1ULL << 32); |
| 47 | svm->vmcb->control.exit_info_2 = fault->address; |
| 48 | } |
| 49 | |
| 50 | svm->vmcb->control.exit_info_1 &= ~0xffffffffULL; |
| 51 | svm->vmcb->control.exit_info_1 |= fault->error_code; |
| 52 | |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 53 | nested_svm_vmexit(svm); |
| 54 | } |
| 55 | |
Paolo Bonzini | a04aead | 2021-02-18 07:16:59 -0500 | [diff] [blame] | 56 | static void svm_inject_page_fault_nested(struct kvm_vcpu *vcpu, struct x86_exception *fault) |
| 57 | { |
| 58 | struct vcpu_svm *svm = to_svm(vcpu); |
| 59 | WARN_ON(!is_guest_mode(vcpu)); |
| 60 | |
| 61 | if (vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_EXCEPTION_OFFSET + PF_VECTOR) && |
| 62 | !svm->nested.nested_run_pending) { |
| 63 | svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + PF_VECTOR; |
| 64 | svm->vmcb->control.exit_code_hi = 0; |
| 65 | svm->vmcb->control.exit_info_1 = fault->error_code; |
| 66 | svm->vmcb->control.exit_info_2 = fault->address; |
| 67 | nested_svm_vmexit(svm); |
| 68 | } else { |
| 69 | kvm_inject_page_fault(vcpu, fault); |
| 70 | } |
| 71 | } |
| 72 | |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 73 | static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index) |
| 74 | { |
| 75 | struct vcpu_svm *svm = to_svm(vcpu); |
Paolo Bonzini | e670bf6 | 2020-05-13 13:16:12 -0400 | [diff] [blame] | 76 | u64 cr3 = svm->nested.ctl.nested_cr3; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 77 | u64 pdpte; |
| 78 | int ret; |
| 79 | |
Sean Christopherson | 2732be9 | 2021-02-03 16:01:07 -0800 | [diff] [blame] | 80 | ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte, |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 81 | offset_in_page(cr3) + index * 8, 8); |
| 82 | if (ret) |
| 83 | return 0; |
| 84 | return pdpte; |
| 85 | } |
| 86 | |
| 87 | static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu) |
| 88 | { |
| 89 | struct vcpu_svm *svm = to_svm(vcpu); |
| 90 | |
Paolo Bonzini | e670bf6 | 2020-05-13 13:16:12 -0400 | [diff] [blame] | 91 | return svm->nested.ctl.nested_cr3; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu) |
| 95 | { |
Paolo Bonzini | 929d1cf | 2020-05-19 06:18:31 -0400 | [diff] [blame] | 96 | struct vcpu_svm *svm = to_svm(vcpu); |
Paolo Bonzini | 929d1cf | 2020-05-19 06:18:31 -0400 | [diff] [blame] | 97 | |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 98 | WARN_ON(mmu_is_nested(vcpu)); |
| 99 | |
| 100 | vcpu->arch.mmu = &vcpu->arch.guest_mmu; |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 101 | kvm_init_shadow_npt_mmu(vcpu, X86_CR0_PG, svm->vmcb01.ptr->save.cr4, |
| 102 | svm->vmcb01.ptr->save.efer, |
Vitaly Kuznetsov | 0f04a2a | 2020-07-10 16:11:49 +0200 | [diff] [blame] | 103 | svm->nested.ctl.nested_cr3); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 104 | vcpu->arch.mmu->get_guest_pgd = nested_svm_get_tdp_cr3; |
| 105 | vcpu->arch.mmu->get_pdptr = nested_svm_get_tdp_pdptr; |
| 106 | vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 107 | reset_shadow_zero_bits_mask(vcpu, vcpu->arch.mmu); |
| 108 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; |
| 109 | } |
| 110 | |
| 111 | static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu) |
| 112 | { |
| 113 | vcpu->arch.mmu = &vcpu->arch.root_mmu; |
| 114 | vcpu->arch.walk_mmu = &vcpu->arch.root_mmu; |
| 115 | } |
| 116 | |
| 117 | void recalc_intercepts(struct vcpu_svm *svm) |
| 118 | { |
Paolo Bonzini | e670bf6 | 2020-05-13 13:16:12 -0400 | [diff] [blame] | 119 | struct vmcb_control_area *c, *h, *g; |
Babu Moger | c45ad72 | 2020-09-11 14:27:58 -0500 | [diff] [blame] | 120 | unsigned int i; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 121 | |
Joerg Roedel | 06e7852 | 2020-06-25 10:03:23 +0200 | [diff] [blame] | 122 | vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 123 | |
| 124 | if (!is_guest_mode(&svm->vcpu)) |
| 125 | return; |
| 126 | |
| 127 | c = &svm->vmcb->control; |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 128 | h = &svm->vmcb01.ptr->control; |
Paolo Bonzini | e670bf6 | 2020-05-13 13:16:12 -0400 | [diff] [blame] | 129 | g = &svm->nested.ctl; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 130 | |
Babu Moger | c45ad72 | 2020-09-11 14:27:58 -0500 | [diff] [blame] | 131 | for (i = 0; i < MAX_INTERCEPT; i++) |
| 132 | c->intercepts[i] = h->intercepts[i]; |
| 133 | |
Paolo Bonzini | e9fd761 | 2020-05-13 13:28:23 -0400 | [diff] [blame] | 134 | if (g->int_ctl & V_INTR_MASKING_MASK) { |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 135 | /* We only want the cr8 intercept bits of L1 */ |
Babu Moger | 03bfeeb | 2020-09-11 14:28:05 -0500 | [diff] [blame] | 136 | vmcb_clr_intercept(c, INTERCEPT_CR8_READ); |
| 137 | vmcb_clr_intercept(c, INTERCEPT_CR8_WRITE); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 138 | |
| 139 | /* |
| 140 | * Once running L2 with HF_VINTR_MASK, EFLAGS.IF does not |
| 141 | * affect any interrupt we may want to inject; therefore, |
| 142 | * interrupt window vmexits are irrelevant to L0. |
| 143 | */ |
Babu Moger | c62e2e9 | 2020-09-11 14:28:28 -0500 | [diff] [blame] | 144 | vmcb_clr_intercept(c, INTERCEPT_VINTR); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /* We don't want to see VMMCALLs from a nested guest */ |
Babu Moger | c62e2e9 | 2020-09-11 14:28:28 -0500 | [diff] [blame] | 148 | vmcb_clr_intercept(c, INTERCEPT_VMMCALL); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 149 | |
Babu Moger | c45ad72 | 2020-09-11 14:27:58 -0500 | [diff] [blame] | 150 | for (i = 0; i < MAX_INTERCEPT; i++) |
| 151 | c->intercepts[i] |= g->intercepts[i]; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 152 | } |
| 153 | |
Paolo Bonzini | 2f67591 | 2020-05-18 15:21:22 -0400 | [diff] [blame] | 154 | static void copy_vmcb_control_area(struct vmcb_control_area *dst, |
| 155 | struct vmcb_control_area *from) |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 156 | { |
Babu Moger | c45ad72 | 2020-09-11 14:27:58 -0500 | [diff] [blame] | 157 | unsigned int i; |
| 158 | |
| 159 | for (i = 0; i < MAX_INTERCEPT; i++) |
| 160 | dst->intercepts[i] = from->intercepts[i]; |
| 161 | |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 162 | dst->iopm_base_pa = from->iopm_base_pa; |
| 163 | dst->msrpm_base_pa = from->msrpm_base_pa; |
| 164 | dst->tsc_offset = from->tsc_offset; |
Paolo Bonzini | 6c0238c | 2020-05-20 08:02:17 -0400 | [diff] [blame] | 165 | /* asid not copied, it is handled manually for svm->vmcb. */ |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 166 | dst->tlb_ctl = from->tlb_ctl; |
| 167 | dst->int_ctl = from->int_ctl; |
| 168 | dst->int_vector = from->int_vector; |
| 169 | dst->int_state = from->int_state; |
| 170 | dst->exit_code = from->exit_code; |
| 171 | dst->exit_code_hi = from->exit_code_hi; |
| 172 | dst->exit_info_1 = from->exit_info_1; |
| 173 | dst->exit_info_2 = from->exit_info_2; |
| 174 | dst->exit_int_info = from->exit_int_info; |
| 175 | dst->exit_int_info_err = from->exit_int_info_err; |
| 176 | dst->nested_ctl = from->nested_ctl; |
| 177 | dst->event_inj = from->event_inj; |
| 178 | dst->event_inj_err = from->event_inj_err; |
| 179 | dst->nested_cr3 = from->nested_cr3; |
| 180 | dst->virt_ext = from->virt_ext; |
| 181 | dst->pause_filter_count = from->pause_filter_count; |
| 182 | dst->pause_filter_thresh = from->pause_filter_thresh; |
| 183 | } |
| 184 | |
| 185 | static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm) |
| 186 | { |
| 187 | /* |
| 188 | * This function merges the msr permission bitmaps of kvm and the |
| 189 | * nested vmcb. It is optimized in that it only merges the parts where |
| 190 | * the kvm msr permission bitmap may contain zero bits |
| 191 | */ |
| 192 | int i; |
| 193 | |
Babu Moger | c62e2e9 | 2020-09-11 14:28:28 -0500 | [diff] [blame] | 194 | if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT))) |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 195 | return true; |
| 196 | |
| 197 | for (i = 0; i < MSRPM_OFFSETS; i++) { |
| 198 | u32 value, p; |
| 199 | u64 offset; |
| 200 | |
| 201 | if (msrpm_offsets[i] == 0xffffffff) |
| 202 | break; |
| 203 | |
| 204 | p = msrpm_offsets[i]; |
Paolo Bonzini | e670bf6 | 2020-05-13 13:16:12 -0400 | [diff] [blame] | 205 | offset = svm->nested.ctl.msrpm_base_pa + (p * 4); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 206 | |
| 207 | if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4)) |
| 208 | return false; |
| 209 | |
| 210 | svm->nested.msrpm[p] = svm->msrpm[p] | value; |
| 211 | } |
| 212 | |
| 213 | svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm)); |
| 214 | |
| 215 | return true; |
| 216 | } |
| 217 | |
Paolo Bonzini | a7d5c7c | 2020-09-22 07:43:14 -0400 | [diff] [blame] | 218 | static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu) |
| 219 | { |
| 220 | struct vcpu_svm *svm = to_svm(vcpu); |
Maxim Levitsky | f2c7ef3 | 2021-01-07 11:38:51 +0200 | [diff] [blame] | 221 | |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 222 | if (WARN_ON(!is_guest_mode(vcpu))) |
| 223 | return true; |
| 224 | |
Paolo Bonzini | a7d5c7c | 2020-09-22 07:43:14 -0400 | [diff] [blame] | 225 | if (!nested_svm_vmrun_msrpm(svm)) { |
| 226 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 227 | vcpu->run->internal.suberror = |
| 228 | KVM_INTERNAL_ERROR_EMULATION; |
| 229 | vcpu->run->internal.ndata = 0; |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | return true; |
| 234 | } |
| 235 | |
Paolo Bonzini | ca46d73 | 2020-05-18 13:02:15 -0400 | [diff] [blame] | 236 | static bool nested_vmcb_check_controls(struct vmcb_control_area *control) |
| 237 | { |
Sean Christopherson | 11f0cbf | 2021-02-03 16:01:17 -0800 | [diff] [blame] | 238 | if (CC(!vmcb_is_intercept(control, INTERCEPT_VMRUN))) |
Paolo Bonzini | ca46d73 | 2020-05-18 13:02:15 -0400 | [diff] [blame] | 239 | return false; |
| 240 | |
Sean Christopherson | 11f0cbf | 2021-02-03 16:01:17 -0800 | [diff] [blame] | 241 | if (CC(control->asid == 0)) |
Paolo Bonzini | ca46d73 | 2020-05-18 13:02:15 -0400 | [diff] [blame] | 242 | return false; |
| 243 | |
Sean Christopherson | 11f0cbf | 2021-02-03 16:01:17 -0800 | [diff] [blame] | 244 | if (CC((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) && !npt_enabled)) |
Paolo Bonzini | ca46d73 | 2020-05-18 13:02:15 -0400 | [diff] [blame] | 245 | return false; |
| 246 | |
| 247 | return true; |
| 248 | } |
| 249 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 250 | static bool nested_vmcb_check_cr3_cr4(struct kvm_vcpu *vcpu, |
Krish Sadhukhan | 6906e06 | 2020-10-06 19:06:52 +0000 | [diff] [blame] | 251 | struct vmcb_save_area *save) |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 252 | { |
Krish Sadhukhan | 6906e06 | 2020-10-06 19:06:52 +0000 | [diff] [blame] | 253 | /* |
| 254 | * These checks are also performed by KVM_SET_SREGS, |
| 255 | * except that EFER.LMA is not checked by SVM against |
| 256 | * CR0.PG && EFER.LME. |
| 257 | */ |
| 258 | if ((save->efer & EFER_LME) && (save->cr0 & X86_CR0_PG)) { |
Sean Christopherson | 11f0cbf | 2021-02-03 16:01:17 -0800 | [diff] [blame] | 259 | if (CC(!(save->cr4 & X86_CR4_PAE)) || |
| 260 | CC(!(save->cr0 & X86_CR0_PE)) || |
| 261 | CC(kvm_vcpu_is_illegal_gpa(vcpu, save->cr3))) |
Krish Sadhukhan | 761e416 | 2020-07-08 00:39:56 +0000 | [diff] [blame] | 262 | return false; |
| 263 | } |
Krish Sadhukhan | 6906e06 | 2020-10-06 19:06:52 +0000 | [diff] [blame] | 264 | |
Sean Christopherson | 11f0cbf | 2021-02-03 16:01:17 -0800 | [diff] [blame] | 265 | if (CC(!kvm_is_valid_cr4(vcpu, save->cr4))) |
| 266 | return false; |
| 267 | |
| 268 | return true; |
Krish Sadhukhan | 6906e06 | 2020-10-06 19:06:52 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* Common checks that apply to both L1 and L2 state. */ |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 272 | static bool nested_vmcb_valid_sregs(struct kvm_vcpu *vcpu, |
Krish Sadhukhan | 6906e06 | 2020-10-06 19:06:52 +0000 | [diff] [blame] | 273 | struct vmcb_save_area *save) |
| 274 | { |
Sean Christopherson | 11f0cbf | 2021-02-03 16:01:17 -0800 | [diff] [blame] | 275 | if (CC(!(save->efer & EFER_SVME))) |
Krish Sadhukhan | 6906e06 | 2020-10-06 19:06:52 +0000 | [diff] [blame] | 276 | return false; |
| 277 | |
Sean Christopherson | 11f0cbf | 2021-02-03 16:01:17 -0800 | [diff] [blame] | 278 | if (CC((save->cr0 & X86_CR0_CD) == 0 && (save->cr0 & X86_CR0_NW)) || |
| 279 | CC(save->cr0 & ~0xffffffffULL)) |
Krish Sadhukhan | 6906e06 | 2020-10-06 19:06:52 +0000 | [diff] [blame] | 280 | return false; |
| 281 | |
Sean Christopherson | 11f0cbf | 2021-02-03 16:01:17 -0800 | [diff] [blame] | 282 | if (CC(!kvm_dr6_valid(save->dr6)) || CC(!kvm_dr7_valid(save->dr7))) |
Krish Sadhukhan | 6906e06 | 2020-10-06 19:06:52 +0000 | [diff] [blame] | 283 | return false; |
| 284 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 285 | if (!nested_vmcb_check_cr3_cr4(vcpu, save)) |
Krish Sadhukhan | 6906e06 | 2020-10-06 19:06:52 +0000 | [diff] [blame] | 286 | return false; |
| 287 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 288 | if (CC(!kvm_valid_efer(vcpu, save->efer))) |
Krish Sadhukhan | 6906e06 | 2020-10-06 19:06:52 +0000 | [diff] [blame] | 289 | return false; |
| 290 | |
| 291 | return true; |
| 292 | } |
| 293 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 294 | static bool nested_vmcb_checks(struct kvm_vcpu *vcpu, struct vmcb *vmcb12) |
Krish Sadhukhan | 6906e06 | 2020-10-06 19:06:52 +0000 | [diff] [blame] | 295 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 296 | if (!nested_vmcb_valid_sregs(vcpu, &vmcb12->save)) |
Krish Sadhukhan | 761e416 | 2020-07-08 00:39:56 +0000 | [diff] [blame] | 297 | return false; |
| 298 | |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 299 | return nested_vmcb_check_controls(&vmcb12->control); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 300 | } |
| 301 | |
Paolo Bonzini | 9e8f0fb | 2020-11-17 05:15:41 -0500 | [diff] [blame] | 302 | static void nested_load_control_from_vmcb12(struct vcpu_svm *svm, |
| 303 | struct vmcb_control_area *control) |
Paolo Bonzini | 3e06f01 | 2020-05-13 13:07:26 -0400 | [diff] [blame] | 304 | { |
Paolo Bonzini | e670bf6 | 2020-05-13 13:16:12 -0400 | [diff] [blame] | 305 | copy_vmcb_control_area(&svm->nested.ctl, control); |
Paolo Bonzini | 3e06f01 | 2020-05-13 13:07:26 -0400 | [diff] [blame] | 306 | |
Paolo Bonzini | cc440cd | 2020-05-13 13:36:32 -0400 | [diff] [blame] | 307 | /* Copy it here because nested_svm_check_controls will check it. */ |
| 308 | svm->nested.ctl.asid = control->asid; |
Paolo Bonzini | e670bf6 | 2020-05-13 13:16:12 -0400 | [diff] [blame] | 309 | svm->nested.ctl.msrpm_base_pa &= ~0x0fffULL; |
| 310 | svm->nested.ctl.iopm_base_pa &= ~0x0fffULL; |
Paolo Bonzini | 3e06f01 | 2020-05-13 13:07:26 -0400 | [diff] [blame] | 311 | } |
| 312 | |
Paolo Bonzini | 2d8a42b | 2020-05-22 03:50:14 -0400 | [diff] [blame] | 313 | /* |
| 314 | * Synchronize fields that are written by the processor, so that |
Paolo Bonzini | 9e8f0fb | 2020-11-17 05:15:41 -0500 | [diff] [blame] | 315 | * they can be copied back into the vmcb12. |
Paolo Bonzini | 2d8a42b | 2020-05-22 03:50:14 -0400 | [diff] [blame] | 316 | */ |
Paolo Bonzini | 9e8f0fb | 2020-11-17 05:15:41 -0500 | [diff] [blame] | 317 | void nested_sync_control_from_vmcb02(struct vcpu_svm *svm) |
Paolo Bonzini | 2d8a42b | 2020-05-22 03:50:14 -0400 | [diff] [blame] | 318 | { |
| 319 | u32 mask; |
| 320 | svm->nested.ctl.event_inj = svm->vmcb->control.event_inj; |
| 321 | svm->nested.ctl.event_inj_err = svm->vmcb->control.event_inj_err; |
| 322 | |
| 323 | /* Only a few fields of int_ctl are written by the processor. */ |
| 324 | mask = V_IRQ_MASK | V_TPR_MASK; |
| 325 | if (!(svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) && |
Joerg Roedel | a284ba5 | 2020-06-25 10:03:24 +0200 | [diff] [blame] | 326 | svm_is_intercept(svm, INTERCEPT_VINTR)) { |
Paolo Bonzini | 2d8a42b | 2020-05-22 03:50:14 -0400 | [diff] [blame] | 327 | /* |
| 328 | * In order to request an interrupt window, L0 is usurping |
| 329 | * svm->vmcb->control.int_ctl and possibly setting V_IRQ |
| 330 | * even if it was clear in L1's VMCB. Restoring it would be |
| 331 | * wrong. However, in this case V_IRQ will remain true until |
| 332 | * interrupt_window_interception calls svm_clear_vintr and |
| 333 | * restores int_ctl. We can just leave it aside. |
| 334 | */ |
| 335 | mask &= ~V_IRQ_MASK; |
| 336 | } |
| 337 | svm->nested.ctl.int_ctl &= ~mask; |
| 338 | svm->nested.ctl.int_ctl |= svm->vmcb->control.int_ctl & mask; |
| 339 | } |
| 340 | |
Paolo Bonzini | 36e2e98 | 2020-05-22 06:04:57 -0400 | [diff] [blame] | 341 | /* |
| 342 | * Transfer any event that L0 or L1 wanted to inject into L2 to |
| 343 | * EXIT_INT_INFO. |
| 344 | */ |
Paolo Bonzini | 9e8f0fb | 2020-11-17 05:15:41 -0500 | [diff] [blame] | 345 | static void nested_save_pending_event_to_vmcb12(struct vcpu_svm *svm, |
| 346 | struct vmcb *vmcb12) |
Paolo Bonzini | 36e2e98 | 2020-05-22 06:04:57 -0400 | [diff] [blame] | 347 | { |
| 348 | struct kvm_vcpu *vcpu = &svm->vcpu; |
| 349 | u32 exit_int_info = 0; |
| 350 | unsigned int nr; |
| 351 | |
| 352 | if (vcpu->arch.exception.injected) { |
| 353 | nr = vcpu->arch.exception.nr; |
| 354 | exit_int_info = nr | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT; |
| 355 | |
| 356 | if (vcpu->arch.exception.has_error_code) { |
| 357 | exit_int_info |= SVM_EVTINJ_VALID_ERR; |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 358 | vmcb12->control.exit_int_info_err = |
Paolo Bonzini | 36e2e98 | 2020-05-22 06:04:57 -0400 | [diff] [blame] | 359 | vcpu->arch.exception.error_code; |
| 360 | } |
| 361 | |
| 362 | } else if (vcpu->arch.nmi_injected) { |
| 363 | exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI; |
| 364 | |
| 365 | } else if (vcpu->arch.interrupt.injected) { |
| 366 | nr = vcpu->arch.interrupt.nr; |
| 367 | exit_int_info = nr | SVM_EVTINJ_VALID; |
| 368 | |
| 369 | if (vcpu->arch.interrupt.soft) |
| 370 | exit_int_info |= SVM_EVTINJ_TYPE_SOFT; |
| 371 | else |
| 372 | exit_int_info |= SVM_EVTINJ_TYPE_INTR; |
| 373 | } |
| 374 | |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 375 | vmcb12->control.exit_int_info = exit_int_info; |
Paolo Bonzini | 36e2e98 | 2020-05-22 06:04:57 -0400 | [diff] [blame] | 376 | } |
| 377 | |
Vitaly Kuznetsov | 62156f6 | 2020-07-10 16:11:53 +0200 | [diff] [blame] | 378 | static inline bool nested_npt_enabled(struct vcpu_svm *svm) |
| 379 | { |
| 380 | return svm->nested.ctl.nested_ctl & SVM_NESTED_CTL_NP_ENABLE; |
| 381 | } |
| 382 | |
| 383 | /* |
Vitaly Kuznetsov | d82aaef | 2020-07-10 16:11:56 +0200 | [diff] [blame] | 384 | * Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true |
| 385 | * if we are emulating VM-Entry into a guest with NPT enabled. |
Vitaly Kuznetsov | 62156f6 | 2020-07-10 16:11:53 +0200 | [diff] [blame] | 386 | */ |
| 387 | static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, |
| 388 | bool nested_npt) |
| 389 | { |
Sean Christopherson | 11f0cbf | 2021-02-03 16:01:17 -0800 | [diff] [blame] | 390 | if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3))) |
Vitaly Kuznetsov | a506fdd | 2020-07-10 16:11:55 +0200 | [diff] [blame] | 391 | return -EINVAL; |
| 392 | |
| 393 | if (!nested_npt && is_pae_paging(vcpu) && |
| 394 | (cr3 != kvm_read_cr3(vcpu) || pdptrs_changed(vcpu))) { |
Sean Christopherson | 11f0cbf | 2021-02-03 16:01:17 -0800 | [diff] [blame] | 395 | if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))) |
Vitaly Kuznetsov | a506fdd | 2020-07-10 16:11:55 +0200 | [diff] [blame] | 396 | return -EINVAL; |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | * TODO: optimize unconditional TLB flush/MMU sync here and in |
| 401 | * kvm_init_shadow_npt_mmu(). |
| 402 | */ |
| 403 | if (!nested_npt) |
| 404 | kvm_mmu_new_pgd(vcpu, cr3, false, false); |
| 405 | |
| 406 | vcpu->arch.cr3 = cr3; |
| 407 | kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); |
| 408 | |
| 409 | kvm_init_mmu(vcpu, false); |
| 410 | |
| 411 | return 0; |
Vitaly Kuznetsov | 62156f6 | 2020-07-10 16:11:53 +0200 | [diff] [blame] | 412 | } |
| 413 | |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 414 | void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm) |
| 415 | { |
| 416 | if (!svm->nested.vmcb02.ptr) |
| 417 | return; |
| 418 | |
| 419 | /* FIXME: merge g_pat from vmcb01 and vmcb12. */ |
| 420 | svm->nested.vmcb02.ptr->save.g_pat = svm->vmcb01.ptr->save.g_pat; |
| 421 | } |
| 422 | |
Paolo Bonzini | 9e8f0fb | 2020-11-17 05:15:41 -0500 | [diff] [blame] | 423 | static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12) |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 424 | { |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 425 | nested_vmcb02_compute_g_pat(svm); |
| 426 | |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 427 | /* Load the nested guest state */ |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 428 | svm->vmcb->save.es = vmcb12->save.es; |
| 429 | svm->vmcb->save.cs = vmcb12->save.cs; |
| 430 | svm->vmcb->save.ss = vmcb12->save.ss; |
| 431 | svm->vmcb->save.ds = vmcb12->save.ds; |
Paolo Bonzini | 4bb170a | 2020-11-16 06:38:19 -0500 | [diff] [blame] | 432 | svm->vmcb->save.cpl = vmcb12->save.cpl; |
| 433 | vmcb_mark_dirty(svm->vmcb, VMCB_SEG); |
| 434 | |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 435 | svm->vmcb->save.gdtr = vmcb12->save.gdtr; |
| 436 | svm->vmcb->save.idtr = vmcb12->save.idtr; |
Paolo Bonzini | 4bb170a | 2020-11-16 06:38:19 -0500 | [diff] [blame] | 437 | vmcb_mark_dirty(svm->vmcb, VMCB_DT); |
| 438 | |
Paolo Bonzini | 8cce12b | 2020-11-27 12:46:36 -0500 | [diff] [blame] | 439 | kvm_set_rflags(&svm->vcpu, vmcb12->save.rflags | X86_EFLAGS_FIXED); |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 440 | svm_set_efer(&svm->vcpu, vmcb12->save.efer); |
| 441 | svm_set_cr0(&svm->vcpu, vmcb12->save.cr0); |
| 442 | svm_set_cr4(&svm->vcpu, vmcb12->save.cr4); |
Paolo Bonzini | 4bb170a | 2020-11-16 06:38:19 -0500 | [diff] [blame] | 443 | |
| 444 | svm->vcpu.arch.cr2 = vmcb12->save.cr2; |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 445 | kvm_rax_write(&svm->vcpu, vmcb12->save.rax); |
| 446 | kvm_rsp_write(&svm->vcpu, vmcb12->save.rsp); |
| 447 | kvm_rip_write(&svm->vcpu, vmcb12->save.rip); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 448 | |
| 449 | /* In case we don't even reach vcpu_run, the fields are not updated */ |
Paolo Bonzini | 4bb170a | 2020-11-16 06:38:19 -0500 | [diff] [blame] | 450 | svm->vmcb->save.cr2 = svm->vcpu.arch.cr2; |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 451 | svm->vmcb->save.rax = vmcb12->save.rax; |
| 452 | svm->vmcb->save.rsp = vmcb12->save.rsp; |
| 453 | svm->vmcb->save.rip = vmcb12->save.rip; |
Paolo Bonzini | 4bb170a | 2020-11-16 06:38:19 -0500 | [diff] [blame] | 454 | |
Paolo Bonzini | 8cce12b | 2020-11-27 12:46:36 -0500 | [diff] [blame] | 455 | svm->vmcb->save.dr7 = vmcb12->save.dr7 | DR7_FIXED_1; |
Chenyi Qiang | 9a3ecd5 | 2021-02-02 17:04:31 +0800 | [diff] [blame] | 456 | svm->vcpu.arch.dr6 = vmcb12->save.dr6 | DR6_ACTIVE_LOW; |
Paolo Bonzini | 4bb170a | 2020-11-16 06:38:19 -0500 | [diff] [blame] | 457 | vmcb_mark_dirty(svm->vmcb, VMCB_DR); |
Paolo Bonzini | f241d71 | 2020-05-18 10:56:43 -0400 | [diff] [blame] | 458 | } |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 459 | |
Paolo Bonzini | 9e8f0fb | 2020-11-17 05:15:41 -0500 | [diff] [blame] | 460 | static void nested_vmcb02_prepare_control(struct vcpu_svm *svm) |
Paolo Bonzini | f241d71 | 2020-05-18 10:56:43 -0400 | [diff] [blame] | 461 | { |
Paolo Bonzini | 91b7130 | 2020-05-22 12:28:52 -0400 | [diff] [blame] | 462 | const u32 mask = V_INTR_MASKING_MASK | V_GIF_ENABLE_MASK | V_GIF_MASK; |
Vitaly Kuznetsov | 62156f6 | 2020-07-10 16:11:53 +0200 | [diff] [blame] | 463 | |
Paolo Bonzini | 7c3ecfc | 2020-11-16 06:13:15 -0500 | [diff] [blame] | 464 | /* |
| 465 | * Filled at exit: exit_code, exit_code_hi, exit_info_1, exit_info_2, |
| 466 | * exit_int_info, exit_int_info_err, next_rip, insn_len, insn_bytes. |
| 467 | */ |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 468 | |
Paolo Bonzini | 7c3ecfc | 2020-11-16 06:13:15 -0500 | [diff] [blame] | 469 | /* |
| 470 | * Also covers avic_vapic_bar, avic_backing_page, avic_logical_id, |
| 471 | * avic_physical_id. |
| 472 | */ |
| 473 | WARN_ON(svm->vmcb01.ptr->control.int_ctl & AVIC_ENABLE_MASK); |
| 474 | |
| 475 | /* Copied from vmcb01. msrpm_base can be overwritten later. */ |
| 476 | svm->vmcb->control.nested_ctl = svm->vmcb01.ptr->control.nested_ctl; |
| 477 | svm->vmcb->control.iopm_base_pa = svm->vmcb01.ptr->control.iopm_base_pa; |
| 478 | svm->vmcb->control.msrpm_base_pa = svm->vmcb01.ptr->control.msrpm_base_pa; |
| 479 | |
| 480 | /* Done at vmrun: asid. */ |
| 481 | |
| 482 | /* Also overwritten later if necessary. */ |
| 483 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING; |
| 484 | |
| 485 | /* nested_cr3. */ |
Vitaly Kuznetsov | 62156f6 | 2020-07-10 16:11:53 +0200 | [diff] [blame] | 486 | if (nested_npt_enabled(svm)) |
Paolo Bonzini | 69cb877 | 2020-05-22 05:27:46 -0400 | [diff] [blame] | 487 | nested_svm_init_mmu_context(&svm->vcpu); |
| 488 | |
Paolo Bonzini | 18fc6c5 | 2020-05-18 11:07:08 -0400 | [diff] [blame] | 489 | svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset = |
Paolo Bonzini | e670bf6 | 2020-05-13 13:16:12 -0400 | [diff] [blame] | 490 | svm->vcpu.arch.l1_tsc_offset + svm->nested.ctl.tsc_offset; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 491 | |
Paolo Bonzini | 91b7130 | 2020-05-22 12:28:52 -0400 | [diff] [blame] | 492 | svm->vmcb->control.int_ctl = |
| 493 | (svm->nested.ctl.int_ctl & ~mask) | |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 494 | (svm->vmcb01.ptr->control.int_ctl & mask); |
Paolo Bonzini | 91b7130 | 2020-05-22 12:28:52 -0400 | [diff] [blame] | 495 | |
Paolo Bonzini | e670bf6 | 2020-05-13 13:16:12 -0400 | [diff] [blame] | 496 | svm->vmcb->control.virt_ext = svm->nested.ctl.virt_ext; |
| 497 | svm->vmcb->control.int_vector = svm->nested.ctl.int_vector; |
| 498 | svm->vmcb->control.int_state = svm->nested.ctl.int_state; |
| 499 | svm->vmcb->control.event_inj = svm->nested.ctl.event_inj; |
| 500 | svm->vmcb->control.event_inj_err = svm->nested.ctl.event_inj_err; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 501 | |
Paolo Bonzini | e670bf6 | 2020-05-13 13:16:12 -0400 | [diff] [blame] | 502 | svm->vmcb->control.pause_filter_count = svm->nested.ctl.pause_filter_count; |
| 503 | svm->vmcb->control.pause_filter_thresh = svm->nested.ctl.pause_filter_thresh; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 504 | |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 505 | /* Enter Guest-Mode */ |
| 506 | enter_guest_mode(&svm->vcpu); |
| 507 | |
| 508 | /* |
Paolo Bonzini | 4bb170a | 2020-11-16 06:38:19 -0500 | [diff] [blame] | 509 | * Merge guest and host intercepts - must be called with vcpu in |
| 510 | * guest-mode to take effect. |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 511 | */ |
| 512 | recalc_intercepts(svm); |
Paolo Bonzini | f241d71 | 2020-05-18 10:56:43 -0400 | [diff] [blame] | 513 | } |
| 514 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 515 | int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa, |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 516 | struct vmcb *vmcb12) |
Paolo Bonzini | f241d71 | 2020-05-18 10:56:43 -0400 | [diff] [blame] | 517 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 518 | struct vcpu_svm *svm = to_svm(vcpu); |
Vitaly Kuznetsov | a506fdd | 2020-07-10 16:11:55 +0200 | [diff] [blame] | 519 | int ret; |
| 520 | |
Maxim Levitsky | 954f419 | 2021-02-17 16:57:13 +0200 | [diff] [blame] | 521 | trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb12_gpa, |
| 522 | vmcb12->save.rip, |
| 523 | vmcb12->control.int_ctl, |
| 524 | vmcb12->control.event_inj, |
| 525 | vmcb12->control.nested_ctl); |
| 526 | |
| 527 | trace_kvm_nested_intercepts(vmcb12->control.intercepts[INTERCEPT_CR] & 0xffff, |
| 528 | vmcb12->control.intercepts[INTERCEPT_CR] >> 16, |
| 529 | vmcb12->control.intercepts[INTERCEPT_EXCEPTION], |
| 530 | vmcb12->control.intercepts[INTERCEPT_WORD3], |
| 531 | vmcb12->control.intercepts[INTERCEPT_WORD4], |
| 532 | vmcb12->control.intercepts[INTERCEPT_WORD5]); |
| 533 | |
| 534 | |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 535 | svm->nested.vmcb12_gpa = vmcb12_gpa; |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 536 | |
| 537 | WARN_ON(svm->vmcb == svm->nested.vmcb02.ptr); |
| 538 | |
Paolo Bonzini | 9e8f0fb | 2020-11-17 05:15:41 -0500 | [diff] [blame] | 539 | nested_load_control_from_vmcb12(svm, &vmcb12->control); |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 540 | |
| 541 | svm_switch_vmcb(svm, &svm->nested.vmcb02); |
Paolo Bonzini | 9e8f0fb | 2020-11-17 05:15:41 -0500 | [diff] [blame] | 542 | nested_vmcb02_prepare_control(svm); |
| 543 | nested_vmcb02_prepare_save(svm, vmcb12); |
Paolo Bonzini | f241d71 | 2020-05-18 10:56:43 -0400 | [diff] [blame] | 544 | |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 545 | ret = nested_svm_load_cr3(&svm->vcpu, vmcb12->save.cr3, |
Vitaly Kuznetsov | a506fdd | 2020-07-10 16:11:55 +0200 | [diff] [blame] | 546 | nested_npt_enabled(svm)); |
| 547 | if (ret) |
| 548 | return ret; |
| 549 | |
Paolo Bonzini | a04aead | 2021-02-18 07:16:59 -0500 | [diff] [blame] | 550 | if (!npt_enabled) |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 551 | vcpu->arch.mmu->inject_page_fault = svm_inject_page_fault_nested; |
Paolo Bonzini | a04aead | 2021-02-18 07:16:59 -0500 | [diff] [blame] | 552 | |
Paolo Bonzini | ffdf7f9 | 2020-05-22 12:18:27 -0400 | [diff] [blame] | 553 | svm_set_gif(svm, true); |
Vitaly Kuznetsov | 59cd9bc | 2020-07-10 16:11:52 +0200 | [diff] [blame] | 554 | |
| 555 | return 0; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 556 | } |
| 557 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 558 | int nested_svm_vmrun(struct kvm_vcpu *vcpu) |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 559 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 560 | struct vcpu_svm *svm = to_svm(vcpu); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 561 | int ret; |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 562 | struct vmcb *vmcb12; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 563 | struct kvm_host_map map; |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 564 | u64 vmcb12_gpa; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 565 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 566 | ++vcpu->stat.nested_run; |
Dongli Zhang | 43c11d9 | 2021-03-05 14:57:47 -0800 | [diff] [blame] | 567 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 568 | if (is_smm(vcpu)) { |
| 569 | kvm_queue_exception(vcpu, UD_VECTOR); |
Paolo Bonzini | 7c67f546 | 2020-04-23 10:52:48 -0400 | [diff] [blame] | 570 | return 1; |
| 571 | } |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 572 | |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 573 | vmcb12_gpa = svm->vmcb->save.rax; |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 574 | ret = kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 575 | if (ret == -EINVAL) { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 576 | kvm_inject_gp(vcpu, 0); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 577 | return 1; |
| 578 | } else if (ret) { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 579 | return kvm_skip_emulated_instruction(vcpu); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 580 | } |
| 581 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 582 | ret = kvm_skip_emulated_instruction(vcpu); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 583 | |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 584 | vmcb12 = map.hva; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 585 | |
Maxim Levitsky | 2fcf487 | 2020-10-01 14:29:54 +0300 | [diff] [blame] | 586 | if (WARN_ON_ONCE(!svm->nested.initialized)) |
| 587 | return -EINVAL; |
| 588 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 589 | if (!nested_vmcb_checks(vcpu, vmcb12)) { |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 590 | vmcb12->control.exit_code = SVM_EXIT_ERR; |
| 591 | vmcb12->control.exit_code_hi = 0; |
| 592 | vmcb12->control.exit_info_1 = 0; |
| 593 | vmcb12->control.exit_info_2 = 0; |
Paolo Bonzini | 69c9dfa | 2020-05-13 12:57:26 -0400 | [diff] [blame] | 594 | goto out; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 595 | } |
| 596 | |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 597 | |
| 598 | /* Clear internal status */ |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 599 | kvm_clear_exception_queue(vcpu); |
| 600 | kvm_clear_interrupt_queue(vcpu); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 601 | |
| 602 | /* |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 603 | * Since vmcb01 is not in use, we can use it to store some of the L1 |
| 604 | * state. |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 605 | */ |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 606 | svm->vmcb01.ptr->save.efer = vcpu->arch.efer; |
| 607 | svm->vmcb01.ptr->save.cr0 = kvm_read_cr0(vcpu); |
| 608 | svm->vmcb01.ptr->save.cr4 = vcpu->arch.cr4; |
| 609 | svm->vmcb01.ptr->save.rflags = kvm_get_rflags(vcpu); |
| 610 | svm->vmcb01.ptr->save.rip = kvm_rip_read(vcpu); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 611 | |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 612 | if (!npt_enabled) |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 613 | svm->vmcb01.ptr->save.cr3 = kvm_read_cr3(vcpu); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 614 | |
Paolo Bonzini | f74f941 | 2020-04-23 13:22:27 -0400 | [diff] [blame] | 615 | svm->nested.nested_run_pending = 1; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 616 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 617 | if (enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12)) |
Vitaly Kuznetsov | 59cd9bc | 2020-07-10 16:11:52 +0200 | [diff] [blame] | 618 | goto out_exit_err; |
Vitaly Kuznetsov | ebdb3db | 2020-07-10 16:11:51 +0200 | [diff] [blame] | 619 | |
Vitaly Kuznetsov | 59cd9bc | 2020-07-10 16:11:52 +0200 | [diff] [blame] | 620 | if (nested_svm_vmrun_msrpm(svm)) |
| 621 | goto out; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 622 | |
Vitaly Kuznetsov | 59cd9bc | 2020-07-10 16:11:52 +0200 | [diff] [blame] | 623 | out_exit_err: |
| 624 | svm->nested.nested_run_pending = 0; |
| 625 | |
| 626 | svm->vmcb->control.exit_code = SVM_EXIT_ERR; |
| 627 | svm->vmcb->control.exit_code_hi = 0; |
| 628 | svm->vmcb->control.exit_info_1 = 0; |
| 629 | svm->vmcb->control.exit_info_2 = 0; |
| 630 | |
| 631 | nested_svm_vmexit(svm); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 632 | |
Paolo Bonzini | 69c9dfa | 2020-05-13 12:57:26 -0400 | [diff] [blame] | 633 | out: |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 634 | kvm_vcpu_unmap(vcpu, &map, true); |
Paolo Bonzini | 69c9dfa | 2020-05-13 12:57:26 -0400 | [diff] [blame] | 635 | |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 636 | return ret; |
| 637 | } |
| 638 | |
| 639 | void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb) |
| 640 | { |
| 641 | to_vmcb->save.fs = from_vmcb->save.fs; |
| 642 | to_vmcb->save.gs = from_vmcb->save.gs; |
| 643 | to_vmcb->save.tr = from_vmcb->save.tr; |
| 644 | to_vmcb->save.ldtr = from_vmcb->save.ldtr; |
| 645 | to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base; |
| 646 | to_vmcb->save.star = from_vmcb->save.star; |
| 647 | to_vmcb->save.lstar = from_vmcb->save.lstar; |
| 648 | to_vmcb->save.cstar = from_vmcb->save.cstar; |
| 649 | to_vmcb->save.sfmask = from_vmcb->save.sfmask; |
| 650 | to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs; |
| 651 | to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp; |
| 652 | to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip; |
| 653 | } |
| 654 | |
| 655 | int nested_svm_vmexit(struct vcpu_svm *svm) |
| 656 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 657 | struct kvm_vcpu *vcpu = &svm->vcpu; |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 658 | struct vmcb *vmcb12; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 659 | struct vmcb *vmcb = svm->vmcb; |
| 660 | struct kvm_host_map map; |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 661 | int rc; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 662 | |
Sean Christopherson | cb6a32c | 2021-03-02 09:45:14 -0800 | [diff] [blame] | 663 | /* Triple faults in L2 should never escape. */ |
| 664 | WARN_ON_ONCE(kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)); |
| 665 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 666 | rc = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.vmcb12_gpa), &map); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 667 | if (rc) { |
| 668 | if (rc == -EINVAL) |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 669 | kvm_inject_gp(vcpu, 0); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 670 | return 1; |
| 671 | } |
| 672 | |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 673 | vmcb12 = map.hva; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 674 | |
| 675 | /* Exit Guest-Mode */ |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 676 | leave_guest_mode(vcpu); |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 677 | svm->nested.vmcb12_gpa = 0; |
Paolo Bonzini | 2d8a42b | 2020-05-22 03:50:14 -0400 | [diff] [blame] | 678 | WARN_ON_ONCE(svm->nested.nested_run_pending); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 679 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 680 | kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); |
Maxim Levitsky | f2c7ef3 | 2021-01-07 11:38:51 +0200 | [diff] [blame] | 681 | |
Paolo Bonzini | 38c0b19 | 2020-04-23 13:13:09 -0400 | [diff] [blame] | 682 | /* in case we halted in L2 */ |
| 683 | svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE; |
| 684 | |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 685 | /* Give the current vmcb to the guest */ |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 686 | |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 687 | vmcb12->save.es = vmcb->save.es; |
| 688 | vmcb12->save.cs = vmcb->save.cs; |
| 689 | vmcb12->save.ss = vmcb->save.ss; |
| 690 | vmcb12->save.ds = vmcb->save.ds; |
| 691 | vmcb12->save.gdtr = vmcb->save.gdtr; |
| 692 | vmcb12->save.idtr = vmcb->save.idtr; |
| 693 | vmcb12->save.efer = svm->vcpu.arch.efer; |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 694 | vmcb12->save.cr0 = kvm_read_cr0(vcpu); |
| 695 | vmcb12->save.cr3 = kvm_read_cr3(vcpu); |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 696 | vmcb12->save.cr2 = vmcb->save.cr2; |
| 697 | vmcb12->save.cr4 = svm->vcpu.arch.cr4; |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 698 | vmcb12->save.rflags = kvm_get_rflags(vcpu); |
| 699 | vmcb12->save.rip = kvm_rip_read(vcpu); |
| 700 | vmcb12->save.rsp = kvm_rsp_read(vcpu); |
| 701 | vmcb12->save.rax = kvm_rax_read(vcpu); |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 702 | vmcb12->save.dr7 = vmcb->save.dr7; |
| 703 | vmcb12->save.dr6 = svm->vcpu.arch.dr6; |
| 704 | vmcb12->save.cpl = vmcb->save.cpl; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 705 | |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 706 | vmcb12->control.int_state = vmcb->control.int_state; |
| 707 | vmcb12->control.exit_code = vmcb->control.exit_code; |
| 708 | vmcb12->control.exit_code_hi = vmcb->control.exit_code_hi; |
| 709 | vmcb12->control.exit_info_1 = vmcb->control.exit_info_1; |
| 710 | vmcb12->control.exit_info_2 = vmcb->control.exit_info_2; |
Paolo Bonzini | 36e2e98 | 2020-05-22 06:04:57 -0400 | [diff] [blame] | 711 | |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 712 | if (vmcb12->control.exit_code != SVM_EXIT_ERR) |
Paolo Bonzini | 9e8f0fb | 2020-11-17 05:15:41 -0500 | [diff] [blame] | 713 | nested_save_pending_event_to_vmcb12(svm, vmcb12); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 714 | |
| 715 | if (svm->nrips_enabled) |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 716 | vmcb12->control.next_rip = vmcb->control.next_rip; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 717 | |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 718 | vmcb12->control.int_ctl = svm->nested.ctl.int_ctl; |
| 719 | vmcb12->control.tlb_ctl = svm->nested.ctl.tlb_ctl; |
| 720 | vmcb12->control.event_inj = svm->nested.ctl.event_inj; |
| 721 | vmcb12->control.event_inj_err = svm->nested.ctl.event_inj_err; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 722 | |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 723 | vmcb12->control.pause_filter_count = |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 724 | svm->vmcb->control.pause_filter_count; |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 725 | vmcb12->control.pause_filter_thresh = |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 726 | svm->vmcb->control.pause_filter_thresh; |
| 727 | |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 728 | svm_switch_vmcb(svm, &svm->vmcb01); |
| 729 | |
| 730 | /* |
| 731 | * On vmexit the GIF is set to false and |
| 732 | * no event can be injected in L1. |
| 733 | */ |
Maxim Levitsky | 9883764 | 2020-08-27 19:27:18 +0300 | [diff] [blame] | 734 | svm_set_gif(svm, false); |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 735 | svm->vmcb->control.exit_int_info = 0; |
Maxim Levitsky | 9883764 | 2020-08-27 19:27:18 +0300 | [diff] [blame] | 736 | |
Paolo Bonzini | 7ca62d1 | 2020-11-16 06:38:19 -0500 | [diff] [blame] | 737 | svm->vcpu.arch.tsc_offset = svm->vcpu.arch.l1_tsc_offset; |
| 738 | if (svm->vmcb->control.tsc_offset != svm->vcpu.arch.tsc_offset) { |
| 739 | svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset; |
| 740 | vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS); |
| 741 | } |
Paolo Bonzini | 18fc6c5 | 2020-05-18 11:07:08 -0400 | [diff] [blame] | 742 | |
Paolo Bonzini | e670bf6 | 2020-05-13 13:16:12 -0400 | [diff] [blame] | 743 | svm->nested.ctl.nested_cr3 = 0; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 744 | |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 745 | /* |
| 746 | * Restore processor state that had been saved in vmcb01 |
| 747 | */ |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 748 | kvm_set_rflags(vcpu, svm->vmcb->save.rflags); |
| 749 | svm_set_efer(vcpu, svm->vmcb->save.efer); |
| 750 | svm_set_cr0(vcpu, svm->vmcb->save.cr0 | X86_CR0_PE); |
| 751 | svm_set_cr4(vcpu, svm->vmcb->save.cr4); |
| 752 | kvm_rax_write(vcpu, svm->vmcb->save.rax); |
| 753 | kvm_rsp_write(vcpu, svm->vmcb->save.rsp); |
| 754 | kvm_rip_write(vcpu, svm->vmcb->save.rip); |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 755 | |
| 756 | svm->vcpu.arch.dr7 = DR7_FIXED_1; |
| 757 | kvm_update_dr7(&svm->vcpu); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 758 | |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 759 | trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code, |
| 760 | vmcb12->control.exit_info_1, |
| 761 | vmcb12->control.exit_info_2, |
| 762 | vmcb12->control.exit_int_info, |
| 763 | vmcb12->control.exit_int_info_err, |
Paolo Bonzini | 36e2e98 | 2020-05-22 06:04:57 -0400 | [diff] [blame] | 764 | KVM_ISA_SVM); |
| 765 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 766 | kvm_vcpu_unmap(vcpu, &map, true); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 767 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 768 | nested_svm_uninit_mmu_context(vcpu); |
Vitaly Kuznetsov | bf7dea4 | 2020-07-10 16:11:54 +0200 | [diff] [blame] | 769 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 770 | rc = nested_svm_load_cr3(vcpu, svm->vmcb->save.cr3, false); |
Vitaly Kuznetsov | d82aaef | 2020-07-10 16:11:56 +0200 | [diff] [blame] | 771 | if (rc) |
| 772 | return 1; |
Vitaly Kuznetsov | bf7dea4 | 2020-07-10 16:11:54 +0200 | [diff] [blame] | 773 | |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 774 | /* |
| 775 | * Drop what we picked up for L2 via svm_complete_interrupts() so it |
| 776 | * doesn't end up in L1. |
| 777 | */ |
| 778 | svm->vcpu.arch.nmi_injected = false; |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 779 | kvm_clear_exception_queue(vcpu); |
| 780 | kvm_clear_interrupt_queue(vcpu); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 781 | |
| 782 | return 0; |
| 783 | } |
| 784 | |
Sean Christopherson | cb6a32c | 2021-03-02 09:45:14 -0800 | [diff] [blame] | 785 | static void nested_svm_triple_fault(struct kvm_vcpu *vcpu) |
| 786 | { |
Sean Christopherson | 3a87c7e | 2021-03-02 09:45:15 -0800 | [diff] [blame] | 787 | nested_svm_simple_vmexit(to_svm(vcpu), SVM_EXIT_SHUTDOWN); |
Sean Christopherson | cb6a32c | 2021-03-02 09:45:14 -0800 | [diff] [blame] | 788 | } |
| 789 | |
Maxim Levitsky | 2fcf487 | 2020-10-01 14:29:54 +0300 | [diff] [blame] | 790 | int svm_allocate_nested(struct vcpu_svm *svm) |
| 791 | { |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 792 | struct page *vmcb02_page; |
Maxim Levitsky | 2fcf487 | 2020-10-01 14:29:54 +0300 | [diff] [blame] | 793 | |
| 794 | if (svm->nested.initialized) |
| 795 | return 0; |
| 796 | |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 797 | vmcb02_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); |
| 798 | if (!vmcb02_page) |
Maxim Levitsky | 2fcf487 | 2020-10-01 14:29:54 +0300 | [diff] [blame] | 799 | return -ENOMEM; |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 800 | svm->nested.vmcb02.ptr = page_address(vmcb02_page); |
| 801 | svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT); |
Maxim Levitsky | 2fcf487 | 2020-10-01 14:29:54 +0300 | [diff] [blame] | 802 | |
| 803 | svm->nested.msrpm = svm_vcpu_alloc_msrpm(); |
| 804 | if (!svm->nested.msrpm) |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 805 | goto err_free_vmcb02; |
Maxim Levitsky | 2fcf487 | 2020-10-01 14:29:54 +0300 | [diff] [blame] | 806 | svm_vcpu_init_msrpm(&svm->vcpu, svm->nested.msrpm); |
| 807 | |
| 808 | svm->nested.initialized = true; |
| 809 | return 0; |
| 810 | |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 811 | err_free_vmcb02: |
| 812 | __free_page(vmcb02_page); |
Maxim Levitsky | 2fcf487 | 2020-10-01 14:29:54 +0300 | [diff] [blame] | 813 | return -ENOMEM; |
| 814 | } |
| 815 | |
| 816 | void svm_free_nested(struct vcpu_svm *svm) |
| 817 | { |
| 818 | if (!svm->nested.initialized) |
| 819 | return; |
| 820 | |
| 821 | svm_vcpu_free_msrpm(svm->nested.msrpm); |
| 822 | svm->nested.msrpm = NULL; |
| 823 | |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 824 | __free_page(virt_to_page(svm->nested.vmcb02.ptr)); |
| 825 | svm->nested.vmcb02.ptr = NULL; |
Maxim Levitsky | 2fcf487 | 2020-10-01 14:29:54 +0300 | [diff] [blame] | 826 | |
| 827 | svm->nested.initialized = false; |
| 828 | } |
| 829 | |
Paolo Bonzini | c513f48 | 2020-05-18 13:08:37 -0400 | [diff] [blame] | 830 | /* |
| 831 | * Forcibly leave nested mode in order to be able to reset the VCPU later on. |
| 832 | */ |
| 833 | void svm_leave_nested(struct vcpu_svm *svm) |
| 834 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 835 | struct kvm_vcpu *vcpu = &svm->vcpu; |
| 836 | |
| 837 | if (is_guest_mode(vcpu)) { |
Paolo Bonzini | c513f48 | 2020-05-18 13:08:37 -0400 | [diff] [blame] | 838 | svm->nested.nested_run_pending = 0; |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 839 | leave_guest_mode(vcpu); |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 840 | |
| 841 | svm_switch_vmcb(svm, &svm->nested.vmcb02); |
| 842 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 843 | nested_svm_uninit_mmu_context(vcpu); |
Maxim Levitsky | 56fe28d | 2021-01-07 11:38:54 +0200 | [diff] [blame] | 844 | vmcb_mark_all_dirty(svm->vmcb); |
Paolo Bonzini | c513f48 | 2020-05-18 13:08:37 -0400 | [diff] [blame] | 845 | } |
Paolo Bonzini | a7d5c7c | 2020-09-22 07:43:14 -0400 | [diff] [blame] | 846 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 847 | kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); |
Paolo Bonzini | c513f48 | 2020-05-18 13:08:37 -0400 | [diff] [blame] | 848 | } |
| 849 | |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 850 | static int nested_svm_exit_handled_msr(struct vcpu_svm *svm) |
| 851 | { |
| 852 | u32 offset, msr, value; |
| 853 | int write, mask; |
| 854 | |
Babu Moger | c62e2e9 | 2020-09-11 14:28:28 -0500 | [diff] [blame] | 855 | if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT))) |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 856 | return NESTED_EXIT_HOST; |
| 857 | |
| 858 | msr = svm->vcpu.arch.regs[VCPU_REGS_RCX]; |
| 859 | offset = svm_msrpm_offset(msr); |
| 860 | write = svm->vmcb->control.exit_info_1 & 1; |
| 861 | mask = 1 << ((2 * (msr & 0xf)) + write); |
| 862 | |
| 863 | if (offset == MSR_INVALID) |
| 864 | return NESTED_EXIT_DONE; |
| 865 | |
| 866 | /* Offset is in 32 bit units but need in 8 bit units */ |
| 867 | offset *= 4; |
| 868 | |
Paolo Bonzini | e670bf6 | 2020-05-13 13:16:12 -0400 | [diff] [blame] | 869 | if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4)) |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 870 | return NESTED_EXIT_DONE; |
| 871 | |
| 872 | return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST; |
| 873 | } |
| 874 | |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 875 | static int nested_svm_intercept_ioio(struct vcpu_svm *svm) |
| 876 | { |
| 877 | unsigned port, size, iopm_len; |
| 878 | u16 val, mask; |
| 879 | u8 start_bit; |
| 880 | u64 gpa; |
| 881 | |
Babu Moger | c62e2e9 | 2020-09-11 14:28:28 -0500 | [diff] [blame] | 882 | if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_IOIO_PROT))) |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 883 | return NESTED_EXIT_HOST; |
| 884 | |
| 885 | port = svm->vmcb->control.exit_info_1 >> 16; |
| 886 | size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >> |
| 887 | SVM_IOIO_SIZE_SHIFT; |
Paolo Bonzini | e670bf6 | 2020-05-13 13:16:12 -0400 | [diff] [blame] | 888 | gpa = svm->nested.ctl.iopm_base_pa + (port / 8); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 889 | start_bit = port % 8; |
| 890 | iopm_len = (start_bit + size > 8) ? 2 : 1; |
| 891 | mask = (0xf >> (4 - size)) << start_bit; |
| 892 | val = 0; |
| 893 | |
| 894 | if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len)) |
| 895 | return NESTED_EXIT_DONE; |
| 896 | |
| 897 | return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST; |
| 898 | } |
| 899 | |
| 900 | static int nested_svm_intercept(struct vcpu_svm *svm) |
| 901 | { |
| 902 | u32 exit_code = svm->vmcb->control.exit_code; |
| 903 | int vmexit = NESTED_EXIT_HOST; |
| 904 | |
| 905 | switch (exit_code) { |
| 906 | case SVM_EXIT_MSR: |
| 907 | vmexit = nested_svm_exit_handled_msr(svm); |
| 908 | break; |
| 909 | case SVM_EXIT_IOIO: |
| 910 | vmexit = nested_svm_intercept_ioio(svm); |
| 911 | break; |
| 912 | case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: { |
Babu Moger | 03bfeeb | 2020-09-11 14:28:05 -0500 | [diff] [blame] | 913 | if (vmcb_is_intercept(&svm->nested.ctl, exit_code)) |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 914 | vmexit = NESTED_EXIT_DONE; |
| 915 | break; |
| 916 | } |
| 917 | case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: { |
Babu Moger | 30abaa88 | 2020-09-11 14:28:12 -0500 | [diff] [blame] | 918 | if (vmcb_is_intercept(&svm->nested.ctl, exit_code)) |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 919 | vmexit = NESTED_EXIT_DONE; |
| 920 | break; |
| 921 | } |
| 922 | case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: { |
Paolo Bonzini | 7c86663 | 2020-05-16 08:42:28 -0400 | [diff] [blame] | 923 | /* |
| 924 | * Host-intercepted exceptions have been checked already in |
| 925 | * nested_svm_exit_special. There is nothing to do here, |
| 926 | * the vmexit is injected by svm_check_nested_events. |
| 927 | */ |
| 928 | vmexit = NESTED_EXIT_DONE; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 929 | break; |
| 930 | } |
| 931 | case SVM_EXIT_ERR: { |
| 932 | vmexit = NESTED_EXIT_DONE; |
| 933 | break; |
| 934 | } |
| 935 | default: { |
Babu Moger | c62e2e9 | 2020-09-11 14:28:28 -0500 | [diff] [blame] | 936 | if (vmcb_is_intercept(&svm->nested.ctl, exit_code)) |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 937 | vmexit = NESTED_EXIT_DONE; |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | return vmexit; |
| 942 | } |
| 943 | |
| 944 | int nested_svm_exit_handled(struct vcpu_svm *svm) |
| 945 | { |
| 946 | int vmexit; |
| 947 | |
| 948 | vmexit = nested_svm_intercept(svm); |
| 949 | |
| 950 | if (vmexit == NESTED_EXIT_DONE) |
| 951 | nested_svm_vmexit(svm); |
| 952 | |
| 953 | return vmexit; |
| 954 | } |
| 955 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 956 | int nested_svm_check_permissions(struct kvm_vcpu *vcpu) |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 957 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 958 | if (!(vcpu->arch.efer & EFER_SVME) || !is_paging(vcpu)) { |
| 959 | kvm_queue_exception(vcpu, UD_VECTOR); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 960 | return 1; |
| 961 | } |
| 962 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 963 | if (to_svm(vcpu)->vmcb->save.cpl) { |
| 964 | kvm_inject_gp(vcpu, 0); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 965 | return 1; |
| 966 | } |
| 967 | |
| 968 | return 0; |
| 969 | } |
| 970 | |
Paolo Bonzini | 7c86663 | 2020-05-16 08:42:28 -0400 | [diff] [blame] | 971 | static bool nested_exit_on_exception(struct vcpu_svm *svm) |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 972 | { |
Paolo Bonzini | 7c86663 | 2020-05-16 08:42:28 -0400 | [diff] [blame] | 973 | unsigned int nr = svm->vcpu.arch.exception.nr; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 974 | |
Babu Moger | 9780d51 | 2020-09-11 14:28:20 -0500 | [diff] [blame] | 975 | return (svm->nested.ctl.intercepts[INTERCEPT_EXCEPTION] & BIT(nr)); |
Paolo Bonzini | 7c86663 | 2020-05-16 08:42:28 -0400 | [diff] [blame] | 976 | } |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 977 | |
Paolo Bonzini | 7c86663 | 2020-05-16 08:42:28 -0400 | [diff] [blame] | 978 | static void nested_svm_inject_exception_vmexit(struct vcpu_svm *svm) |
| 979 | { |
| 980 | unsigned int nr = svm->vcpu.arch.exception.nr; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 981 | |
| 982 | svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr; |
| 983 | svm->vmcb->control.exit_code_hi = 0; |
Paolo Bonzini | 7c86663 | 2020-05-16 08:42:28 -0400 | [diff] [blame] | 984 | |
| 985 | if (svm->vcpu.arch.exception.has_error_code) |
| 986 | svm->vmcb->control.exit_info_1 = svm->vcpu.arch.exception.error_code; |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 987 | |
| 988 | /* |
| 989 | * EXITINFO2 is undefined for all exception intercepts other |
| 990 | * than #PF. |
| 991 | */ |
Paolo Bonzini | 7c86663 | 2020-05-16 08:42:28 -0400 | [diff] [blame] | 992 | if (nr == PF_VECTOR) { |
| 993 | if (svm->vcpu.arch.exception.nested_apf) |
| 994 | svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token; |
| 995 | else if (svm->vcpu.arch.exception.has_payload) |
| 996 | svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload; |
| 997 | else |
| 998 | svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2; |
| 999 | } else if (nr == DB_VECTOR) { |
| 1000 | /* See inject_pending_event. */ |
| 1001 | kvm_deliver_exception_payload(&svm->vcpu); |
| 1002 | if (svm->vcpu.arch.dr7 & DR7_GD) { |
| 1003 | svm->vcpu.arch.dr7 &= ~DR7_GD; |
| 1004 | kvm_update_dr7(&svm->vcpu); |
| 1005 | } |
| 1006 | } else |
| 1007 | WARN_ON(svm->vcpu.arch.exception.has_payload); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 1008 | |
Paolo Bonzini | 7c86663 | 2020-05-16 08:42:28 -0400 | [diff] [blame] | 1009 | nested_svm_vmexit(svm); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 1010 | } |
| 1011 | |
Paolo Bonzini | 5b672408 | 2020-05-16 08:50:35 -0400 | [diff] [blame] | 1012 | static inline bool nested_exit_on_init(struct vcpu_svm *svm) |
| 1013 | { |
Babu Moger | c62e2e9 | 2020-09-11 14:28:28 -0500 | [diff] [blame] | 1014 | return vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_INIT); |
Paolo Bonzini | 5b672408 | 2020-05-16 08:50:35 -0400 | [diff] [blame] | 1015 | } |
| 1016 | |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 1017 | static int svm_check_nested_events(struct kvm_vcpu *vcpu) |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 1018 | { |
| 1019 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1020 | bool block_nested_events = |
Paolo Bonzini | bd27962 | 2020-05-16 08:46:00 -0400 | [diff] [blame] | 1021 | kvm_event_needs_reinjection(vcpu) || svm->nested.nested_run_pending; |
Paolo Bonzini | 5b672408 | 2020-05-16 08:50:35 -0400 | [diff] [blame] | 1022 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 1023 | |
| 1024 | if (lapic_in_kernel(vcpu) && |
| 1025 | test_bit(KVM_APIC_INIT, &apic->pending_events)) { |
| 1026 | if (block_nested_events) |
| 1027 | return -EBUSY; |
| 1028 | if (!nested_exit_on_init(svm)) |
| 1029 | return 0; |
Sean Christopherson | 3a87c7e | 2021-03-02 09:45:15 -0800 | [diff] [blame] | 1030 | nested_svm_simple_vmexit(svm, SVM_EXIT_INIT); |
Paolo Bonzini | 5b672408 | 2020-05-16 08:50:35 -0400 | [diff] [blame] | 1031 | return 0; |
| 1032 | } |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 1033 | |
Paolo Bonzini | 7c86663 | 2020-05-16 08:42:28 -0400 | [diff] [blame] | 1034 | if (vcpu->arch.exception.pending) { |
| 1035 | if (block_nested_events) |
| 1036 | return -EBUSY; |
| 1037 | if (!nested_exit_on_exception(svm)) |
| 1038 | return 0; |
| 1039 | nested_svm_inject_exception_vmexit(svm); |
| 1040 | return 0; |
| 1041 | } |
| 1042 | |
Paolo Bonzini | 221e761 | 2020-04-23 08:13:10 -0400 | [diff] [blame] | 1043 | if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) { |
Paolo Bonzini | 55714cd | 2020-04-23 08:17:28 -0400 | [diff] [blame] | 1044 | if (block_nested_events) |
| 1045 | return -EBUSY; |
Paolo Bonzini | 221e761 | 2020-04-23 08:13:10 -0400 | [diff] [blame] | 1046 | if (!nested_exit_on_smi(svm)) |
| 1047 | return 0; |
Sean Christopherson | 3a87c7e | 2021-03-02 09:45:15 -0800 | [diff] [blame] | 1048 | nested_svm_simple_vmexit(svm, SVM_EXIT_SMI); |
Paolo Bonzini | 55714cd | 2020-04-23 08:17:28 -0400 | [diff] [blame] | 1049 | return 0; |
| 1050 | } |
| 1051 | |
Paolo Bonzini | 221e761 | 2020-04-23 08:13:10 -0400 | [diff] [blame] | 1052 | if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) { |
Cathy Avery | 9c3d370 | 2020-04-14 16:11:06 -0400 | [diff] [blame] | 1053 | if (block_nested_events) |
| 1054 | return -EBUSY; |
Paolo Bonzini | 221e761 | 2020-04-23 08:13:10 -0400 | [diff] [blame] | 1055 | if (!nested_exit_on_nmi(svm)) |
| 1056 | return 0; |
Sean Christopherson | 3a87c7e | 2021-03-02 09:45:15 -0800 | [diff] [blame] | 1057 | nested_svm_simple_vmexit(svm, SVM_EXIT_NMI); |
Cathy Avery | 9c3d370 | 2020-04-14 16:11:06 -0400 | [diff] [blame] | 1058 | return 0; |
| 1059 | } |
| 1060 | |
Paolo Bonzini | 221e761 | 2020-04-23 08:13:10 -0400 | [diff] [blame] | 1061 | if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) { |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 1062 | if (block_nested_events) |
| 1063 | return -EBUSY; |
Paolo Bonzini | 221e761 | 2020-04-23 08:13:10 -0400 | [diff] [blame] | 1064 | if (!nested_exit_on_intr(svm)) |
| 1065 | return 0; |
Sean Christopherson | 3a87c7e | 2021-03-02 09:45:15 -0800 | [diff] [blame] | 1066 | trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip); |
| 1067 | nested_svm_simple_vmexit(svm, SVM_EXIT_INTR); |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 1068 | return 0; |
| 1069 | } |
| 1070 | |
| 1071 | return 0; |
| 1072 | } |
| 1073 | |
| 1074 | int nested_svm_exit_special(struct vcpu_svm *svm) |
| 1075 | { |
| 1076 | u32 exit_code = svm->vmcb->control.exit_code; |
| 1077 | |
| 1078 | switch (exit_code) { |
| 1079 | case SVM_EXIT_INTR: |
| 1080 | case SVM_EXIT_NMI: |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 1081 | case SVM_EXIT_NPF: |
Paolo Bonzini | 7c86663 | 2020-05-16 08:42:28 -0400 | [diff] [blame] | 1082 | return NESTED_EXIT_HOST; |
| 1083 | case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: { |
| 1084 | u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE); |
| 1085 | |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 1086 | if (svm->vmcb01.ptr->control.intercepts[INTERCEPT_EXCEPTION] & |
| 1087 | excp_bits) |
Paolo Bonzini | 7c86663 | 2020-05-16 08:42:28 -0400 | [diff] [blame] | 1088 | return NESTED_EXIT_HOST; |
| 1089 | else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR && |
Vitaly Kuznetsov | 68fd66f | 2020-05-25 16:41:17 +0200 | [diff] [blame] | 1090 | svm->vcpu.arch.apf.host_apf_flags) |
Paolo Bonzini | 7c86663 | 2020-05-16 08:42:28 -0400 | [diff] [blame] | 1091 | /* Trap async PF even if not shadowing */ |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 1092 | return NESTED_EXIT_HOST; |
| 1093 | break; |
Paolo Bonzini | 7c86663 | 2020-05-16 08:42:28 -0400 | [diff] [blame] | 1094 | } |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 1095 | default: |
| 1096 | break; |
| 1097 | } |
| 1098 | |
| 1099 | return NESTED_EXIT_CONTINUE; |
| 1100 | } |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 1101 | |
Paolo Bonzini | cc440cd | 2020-05-13 13:36:32 -0400 | [diff] [blame] | 1102 | static int svm_get_nested_state(struct kvm_vcpu *vcpu, |
| 1103 | struct kvm_nested_state __user *user_kvm_nested_state, |
| 1104 | u32 user_data_size) |
| 1105 | { |
| 1106 | struct vcpu_svm *svm; |
| 1107 | struct kvm_nested_state kvm_state = { |
| 1108 | .flags = 0, |
| 1109 | .format = KVM_STATE_NESTED_FORMAT_SVM, |
| 1110 | .size = sizeof(kvm_state), |
| 1111 | }; |
| 1112 | struct vmcb __user *user_vmcb = (struct vmcb __user *) |
| 1113 | &user_kvm_nested_state->data.svm[0]; |
| 1114 | |
| 1115 | if (!vcpu) |
| 1116 | return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE; |
| 1117 | |
| 1118 | svm = to_svm(vcpu); |
| 1119 | |
| 1120 | if (user_data_size < kvm_state.size) |
| 1121 | goto out; |
| 1122 | |
| 1123 | /* First fill in the header and copy it out. */ |
| 1124 | if (is_guest_mode(vcpu)) { |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 1125 | kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa; |
Paolo Bonzini | cc440cd | 2020-05-13 13:36:32 -0400 | [diff] [blame] | 1126 | kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE; |
| 1127 | kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE; |
| 1128 | |
| 1129 | if (svm->nested.nested_run_pending) |
| 1130 | kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING; |
| 1131 | } |
| 1132 | |
| 1133 | if (gif_set(svm)) |
| 1134 | kvm_state.flags |= KVM_STATE_NESTED_GIF_SET; |
| 1135 | |
| 1136 | if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state))) |
| 1137 | return -EFAULT; |
| 1138 | |
| 1139 | if (!is_guest_mode(vcpu)) |
| 1140 | goto out; |
| 1141 | |
| 1142 | /* |
| 1143 | * Copy over the full size of the VMCB rather than just the size |
| 1144 | * of the structs. |
| 1145 | */ |
| 1146 | if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE)) |
| 1147 | return -EFAULT; |
| 1148 | if (copy_to_user(&user_vmcb->control, &svm->nested.ctl, |
| 1149 | sizeof(user_vmcb->control))) |
| 1150 | return -EFAULT; |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 1151 | if (copy_to_user(&user_vmcb->save, &svm->vmcb01.ptr->save, |
Paolo Bonzini | cc440cd | 2020-05-13 13:36:32 -0400 | [diff] [blame] | 1152 | sizeof(user_vmcb->save))) |
| 1153 | return -EFAULT; |
Paolo Bonzini | cc440cd | 2020-05-13 13:36:32 -0400 | [diff] [blame] | 1154 | out: |
| 1155 | return kvm_state.size; |
| 1156 | } |
| 1157 | |
| 1158 | static int svm_set_nested_state(struct kvm_vcpu *vcpu, |
| 1159 | struct kvm_nested_state __user *user_kvm_nested_state, |
| 1160 | struct kvm_nested_state *kvm_state) |
| 1161 | { |
| 1162 | struct vcpu_svm *svm = to_svm(vcpu); |
Paolo Bonzini | cc440cd | 2020-05-13 13:36:32 -0400 | [diff] [blame] | 1163 | struct vmcb __user *user_vmcb = (struct vmcb __user *) |
| 1164 | &user_kvm_nested_state->data.svm[0]; |
Joerg Roedel | 6ccbd29 | 2020-09-07 15:15:02 +0200 | [diff] [blame] | 1165 | struct vmcb_control_area *ctl; |
| 1166 | struct vmcb_save_area *save; |
| 1167 | int ret; |
Paolo Bonzini | cc440cd | 2020-05-13 13:36:32 -0400 | [diff] [blame] | 1168 | u32 cr0; |
| 1169 | |
Joerg Roedel | 6ccbd29 | 2020-09-07 15:15:02 +0200 | [diff] [blame] | 1170 | BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) > |
| 1171 | KVM_STATE_NESTED_SVM_VMCB_SIZE); |
| 1172 | |
Paolo Bonzini | cc440cd | 2020-05-13 13:36:32 -0400 | [diff] [blame] | 1173 | if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM) |
| 1174 | return -EINVAL; |
| 1175 | |
| 1176 | if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE | |
| 1177 | KVM_STATE_NESTED_RUN_PENDING | |
| 1178 | KVM_STATE_NESTED_GIF_SET)) |
| 1179 | return -EINVAL; |
| 1180 | |
| 1181 | /* |
| 1182 | * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's |
| 1183 | * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed. |
| 1184 | */ |
| 1185 | if (!(vcpu->arch.efer & EFER_SVME)) { |
| 1186 | /* GIF=1 and no guest mode are required if SVME=0. */ |
| 1187 | if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET) |
| 1188 | return -EINVAL; |
| 1189 | } |
| 1190 | |
| 1191 | /* SMM temporarily disables SVM, so we cannot be in guest mode. */ |
| 1192 | if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) |
| 1193 | return -EINVAL; |
| 1194 | |
| 1195 | if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) { |
| 1196 | svm_leave_nested(svm); |
Vitaly Kuznetsov | d5cd6f3 | 2020-09-14 15:37:25 +0200 | [diff] [blame] | 1197 | svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET)); |
| 1198 | return 0; |
Paolo Bonzini | cc440cd | 2020-05-13 13:36:32 -0400 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa)) |
| 1202 | return -EINVAL; |
| 1203 | if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE) |
| 1204 | return -EINVAL; |
Paolo Bonzini | cc440cd | 2020-05-13 13:36:32 -0400 | [diff] [blame] | 1205 | |
Joerg Roedel | 6ccbd29 | 2020-09-07 15:15:02 +0200 | [diff] [blame] | 1206 | ret = -ENOMEM; |
| 1207 | ctl = kzalloc(sizeof(*ctl), GFP_KERNEL); |
| 1208 | save = kzalloc(sizeof(*save), GFP_KERNEL); |
| 1209 | if (!ctl || !save) |
| 1210 | goto out_free; |
| 1211 | |
| 1212 | ret = -EFAULT; |
| 1213 | if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl))) |
| 1214 | goto out_free; |
| 1215 | if (copy_from_user(save, &user_vmcb->save, sizeof(*save))) |
| 1216 | goto out_free; |
| 1217 | |
| 1218 | ret = -EINVAL; |
| 1219 | if (!nested_vmcb_check_controls(ctl)) |
| 1220 | goto out_free; |
Paolo Bonzini | cc440cd | 2020-05-13 13:36:32 -0400 | [diff] [blame] | 1221 | |
| 1222 | /* |
| 1223 | * Processor state contains L2 state. Check that it is |
| 1224 | * valid for guest mode (see nested_vmcb_checks). |
| 1225 | */ |
| 1226 | cr0 = kvm_read_cr0(vcpu); |
| 1227 | if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW)) |
Joerg Roedel | 6ccbd29 | 2020-09-07 15:15:02 +0200 | [diff] [blame] | 1228 | goto out_free; |
Paolo Bonzini | cc440cd | 2020-05-13 13:36:32 -0400 | [diff] [blame] | 1229 | |
| 1230 | /* |
| 1231 | * Validate host state saved from before VMRUN (see |
| 1232 | * nested_svm_check_permissions). |
Paolo Bonzini | cc440cd | 2020-05-13 13:36:32 -0400 | [diff] [blame] | 1233 | */ |
Krish Sadhukhan | 6906e06 | 2020-10-06 19:06:52 +0000 | [diff] [blame] | 1234 | if (!(save->cr0 & X86_CR0_PG) || |
| 1235 | !(save->cr0 & X86_CR0_PE) || |
| 1236 | (save->rflags & X86_EFLAGS_VM) || |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1237 | !nested_vmcb_valid_sregs(vcpu, save)) |
Joerg Roedel | 6ccbd29 | 2020-09-07 15:15:02 +0200 | [diff] [blame] | 1238 | goto out_free; |
Paolo Bonzini | cc440cd | 2020-05-13 13:36:32 -0400 | [diff] [blame] | 1239 | |
| 1240 | /* |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 1241 | * All checks done, we can enter guest mode. Userspace provides |
| 1242 | * vmcb12.control, which will be combined with L1 and stored into |
| 1243 | * vmcb02, and the L1 save state which we store in vmcb01. |
| 1244 | * L2 registers if needed are moved from the current VMCB to VMCB02. |
Paolo Bonzini | cc440cd | 2020-05-13 13:36:32 -0400 | [diff] [blame] | 1245 | */ |
Maxim Levitsky | 81f76ad | 2021-01-07 11:38:52 +0200 | [diff] [blame] | 1246 | |
| 1247 | svm->nested.nested_run_pending = |
| 1248 | !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING); |
| 1249 | |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 1250 | svm->nested.vmcb12_gpa = kvm_state->hdr.svm.vmcb_pa; |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 1251 | if (svm->current_vmcb == &svm->vmcb01) |
| 1252 | svm->nested.vmcb02.ptr->save = svm->vmcb01.ptr->save; |
Paolo Bonzini | c08f390 | 2020-11-17 02:51:35 -0500 | [diff] [blame] | 1253 | |
| 1254 | svm->vmcb01.ptr->save.es = save->es; |
| 1255 | svm->vmcb01.ptr->save.cs = save->cs; |
| 1256 | svm->vmcb01.ptr->save.ss = save->ss; |
| 1257 | svm->vmcb01.ptr->save.ds = save->ds; |
| 1258 | svm->vmcb01.ptr->save.gdtr = save->gdtr; |
| 1259 | svm->vmcb01.ptr->save.idtr = save->idtr; |
| 1260 | svm->vmcb01.ptr->save.rflags = save->rflags | X86_EFLAGS_FIXED; |
| 1261 | svm->vmcb01.ptr->save.efer = save->efer; |
| 1262 | svm->vmcb01.ptr->save.cr0 = save->cr0; |
| 1263 | svm->vmcb01.ptr->save.cr3 = save->cr3; |
| 1264 | svm->vmcb01.ptr->save.cr4 = save->cr4; |
| 1265 | svm->vmcb01.ptr->save.rax = save->rax; |
| 1266 | svm->vmcb01.ptr->save.rsp = save->rsp; |
| 1267 | svm->vmcb01.ptr->save.rip = save->rip; |
| 1268 | svm->vmcb01.ptr->save.cpl = 0; |
| 1269 | |
Paolo Bonzini | 9e8f0fb | 2020-11-17 05:15:41 -0500 | [diff] [blame] | 1270 | nested_load_control_from_vmcb12(svm, ctl); |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 1271 | |
| 1272 | svm_switch_vmcb(svm, &svm->nested.vmcb02); |
| 1273 | |
Paolo Bonzini | 9e8f0fb | 2020-11-17 05:15:41 -0500 | [diff] [blame] | 1274 | nested_vmcb02_prepare_control(svm); |
Paolo Bonzini | cc440cd | 2020-05-13 13:36:32 -0400 | [diff] [blame] | 1275 | |
Paolo Bonzini | a7d5c7c | 2020-09-22 07:43:14 -0400 | [diff] [blame] | 1276 | kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); |
Joerg Roedel | 6ccbd29 | 2020-09-07 15:15:02 +0200 | [diff] [blame] | 1277 | ret = 0; |
| 1278 | out_free: |
| 1279 | kfree(save); |
| 1280 | kfree(ctl); |
| 1281 | |
| 1282 | return ret; |
Paolo Bonzini | cc440cd | 2020-05-13 13:36:32 -0400 | [diff] [blame] | 1283 | } |
| 1284 | |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 1285 | struct kvm_x86_nested_ops svm_nested_ops = { |
| 1286 | .check_events = svm_check_nested_events, |
Sean Christopherson | cb6a32c | 2021-03-02 09:45:14 -0800 | [diff] [blame] | 1287 | .triple_fault = nested_svm_triple_fault, |
Paolo Bonzini | a7d5c7c | 2020-09-22 07:43:14 -0400 | [diff] [blame] | 1288 | .get_nested_state_pages = svm_get_nested_state_pages, |
Paolo Bonzini | cc440cd | 2020-05-13 13:36:32 -0400 | [diff] [blame] | 1289 | .get_state = svm_get_nested_state, |
| 1290 | .set_state = svm_set_nested_state, |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 1291 | }; |