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