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