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