blob: 1218b5a342fc8574f3da905a190fce0713291438 [file] [log] [blame]
Joerg Roedel883b0a92020-03-24 10:41:52 +01001// 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 Bonzini5679b802020-05-04 11:28:25 -040022#include <asm/debugreg.h>
Joerg Roedel883b0a92020-03-24 10:41:52 +010023
24#include "kvm_emulate.h"
25#include "trace.h"
26#include "mmu.h"
27#include "x86.h"
Paolo Bonzinicc440cd2020-05-13 13:36:32 -040028#include "cpuid.h"
Paolo Bonzini5b6724082020-05-16 08:50:35 -040029#include "lapic.h"
Joerg Roedel883b0a92020-03-24 10:41:52 +010030#include "svm.h"
31
Sean Christopherson11f0cbf2021-02-03 16:01:17 -080032#define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK
33
Joerg Roedel883b0a92020-03-24 10:41:52 +010034static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
35 struct x86_exception *fault)
36{
37 struct vcpu_svm *svm = to_svm(vcpu);
38
39 if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
40 /*
41 * TODO: track the cause of the nested page fault, and
42 * correctly fill in the high bits of exit_info_1.
43 */
44 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
45 svm->vmcb->control.exit_code_hi = 0;
46 svm->vmcb->control.exit_info_1 = (1ULL << 32);
47 svm->vmcb->control.exit_info_2 = fault->address;
48 }
49
50 svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
51 svm->vmcb->control.exit_info_1 |= fault->error_code;
52
Joerg Roedel883b0a92020-03-24 10:41:52 +010053 nested_svm_vmexit(svm);
54}
55
Paolo Bonzinia04aead2021-02-18 07:16:59 -050056static void svm_inject_page_fault_nested(struct kvm_vcpu *vcpu, struct x86_exception *fault)
57{
58 struct vcpu_svm *svm = to_svm(vcpu);
59 WARN_ON(!is_guest_mode(vcpu));
60
Emanuele Giuseppe Esposito8fc78902021-11-03 10:05:26 -040061 if (vmcb12_is_intercept(&svm->nested.ctl,
62 INTERCEPT_EXCEPTION_OFFSET + PF_VECTOR) &&
63 !svm->nested.nested_run_pending) {
Paolo Bonzinia04aead2021-02-18 07:16:59 -050064 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + PF_VECTOR;
65 svm->vmcb->control.exit_code_hi = 0;
66 svm->vmcb->control.exit_info_1 = fault->error_code;
67 svm->vmcb->control.exit_info_2 = fault->address;
68 nested_svm_vmexit(svm);
69 } else {
70 kvm_inject_page_fault(vcpu, fault);
71 }
72}
73
Joerg Roedel883b0a92020-03-24 10:41:52 +010074static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
75{
76 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzinie670bf62020-05-13 13:16:12 -040077 u64 cr3 = svm->nested.ctl.nested_cr3;
Joerg Roedel883b0a92020-03-24 10:41:52 +010078 u64 pdpte;
79 int ret;
80
Sean Christopherson2732be92021-02-03 16:01:07 -080081 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte,
Joerg Roedel883b0a92020-03-24 10:41:52 +010082 offset_in_page(cr3) + index * 8, 8);
83 if (ret)
84 return 0;
85 return pdpte;
86}
87
88static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
89{
90 struct vcpu_svm *svm = to_svm(vcpu);
91
Paolo Bonzinie670bf62020-05-13 13:16:12 -040092 return svm->nested.ctl.nested_cr3;
Joerg Roedel883b0a92020-03-24 10:41:52 +010093}
94
95static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
96{
Paolo Bonzini929d1cf2020-05-19 06:18:31 -040097 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzini929d1cf2020-05-19 06:18:31 -040098
Joerg Roedel883b0a92020-03-24 10:41:52 +010099 WARN_ON(mmu_is_nested(vcpu));
100
101 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
Sean Christopherson31e96bc2021-06-22 10:57:00 -0700102
103 /*
104 * The NPT format depends on L1's CR4 and EFER, which is in vmcb01. Note,
105 * when called via KVM_SET_NESTED_STATE, that state may _not_ match current
106 * vCPU state. CR0.WP is explicitly ignored, while CR0.PG is required.
107 */
Cathy Avery4995a362021-01-13 07:07:52 -0500108 kvm_init_shadow_npt_mmu(vcpu, X86_CR0_PG, svm->vmcb01.ptr->save.cr4,
109 svm->vmcb01.ptr->save.efer,
Vitaly Kuznetsov0f04a2a2020-07-10 16:11:49 +0200110 svm->nested.ctl.nested_cr3);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100111 vcpu->arch.mmu->get_guest_pgd = nested_svm_get_tdp_cr3;
112 vcpu->arch.mmu->get_pdptr = nested_svm_get_tdp_pdptr;
113 vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100114 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
115}
116
117static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
118{
119 vcpu->arch.mmu = &vcpu->arch.root_mmu;
120 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
121}
122
123void recalc_intercepts(struct vcpu_svm *svm)
124{
Emanuele Giuseppe Esposito8fc78902021-11-03 10:05:26 -0400125 struct vmcb_control_area *c, *h;
126 struct vmcb_ctrl_area_cached *g;
Babu Mogerc45ad722020-09-11 14:27:58 -0500127 unsigned int i;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100128
Joerg Roedel06e78522020-06-25 10:03:23 +0200129 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100130
131 if (!is_guest_mode(&svm->vcpu))
132 return;
133
134 c = &svm->vmcb->control;
Cathy Avery4995a362021-01-13 07:07:52 -0500135 h = &svm->vmcb01.ptr->control;
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400136 g = &svm->nested.ctl;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100137
Babu Mogerc45ad722020-09-11 14:27:58 -0500138 for (i = 0; i < MAX_INTERCEPT; i++)
139 c->intercepts[i] = h->intercepts[i];
140
Paolo Bonzinie9fd7612020-05-13 13:28:23 -0400141 if (g->int_ctl & V_INTR_MASKING_MASK) {
Joerg Roedel883b0a92020-03-24 10:41:52 +0100142 /* We only want the cr8 intercept bits of L1 */
Babu Moger03bfeeb2020-09-11 14:28:05 -0500143 vmcb_clr_intercept(c, INTERCEPT_CR8_READ);
144 vmcb_clr_intercept(c, INTERCEPT_CR8_WRITE);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100145
146 /*
147 * Once running L2 with HF_VINTR_MASK, EFLAGS.IF does not
148 * affect any interrupt we may want to inject; therefore,
149 * interrupt window vmexits are irrelevant to L0.
150 */
Babu Mogerc62e2e92020-09-11 14:28:28 -0500151 vmcb_clr_intercept(c, INTERCEPT_VINTR);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100152 }
153
154 /* We don't want to see VMMCALLs from a nested guest */
Babu Mogerc62e2e92020-09-11 14:28:28 -0500155 vmcb_clr_intercept(c, INTERCEPT_VMMCALL);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100156
Babu Mogerc45ad722020-09-11 14:27:58 -0500157 for (i = 0; i < MAX_INTERCEPT; i++)
158 c->intercepts[i] |= g->intercepts[i];
Maxim Levitsky4b639a92021-07-07 15:51:00 +0300159
160 /* If SMI is not intercepted, ignore guest SMI intercept as well */
161 if (!intercept_smi)
162 vmcb_clr_intercept(c, INTERCEPT_SMI);
Maxim Levitskyc7dfa402021-07-19 16:05:00 +0300163
164 vmcb_set_intercept(c, INTERCEPT_VMLOAD);
165 vmcb_set_intercept(c, INTERCEPT_VMSAVE);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100166}
167
Joerg Roedel883b0a92020-03-24 10:41:52 +0100168static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
169{
170 /*
171 * This function merges the msr permission bitmaps of kvm and the
172 * nested vmcb. It is optimized in that it only merges the parts where
173 * the kvm msr permission bitmap may contain zero bits
174 */
175 int i;
176
Emanuele Giuseppe Esposito8fc78902021-11-03 10:05:26 -0400177 if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100178 return true;
179
180 for (i = 0; i < MSRPM_OFFSETS; i++) {
181 u32 value, p;
182 u64 offset;
183
184 if (msrpm_offsets[i] == 0xffffffff)
185 break;
186
187 p = msrpm_offsets[i];
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400188 offset = svm->nested.ctl.msrpm_base_pa + (p * 4);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100189
190 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
191 return false;
192
193 svm->nested.msrpm[p] = svm->msrpm[p] | value;
194 }
195
196 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
197
198 return true;
199}
200
Krish Sadhukhanee695f22021-04-12 17:56:08 -0400201/*
202 * Bits 11:0 of bitmap address are ignored by hardware
203 */
204static bool nested_svm_check_bitmap_pa(struct kvm_vcpu *vcpu, u64 pa, u32 size)
205{
206 u64 addr = PAGE_ALIGN(pa);
207
208 return kvm_vcpu_is_legal_gpa(vcpu, addr) &&
209 kvm_vcpu_is_legal_gpa(vcpu, addr + size - 1);
210}
211
Krish Sadhukhan174a9212021-09-20 19:51:31 -0400212static bool nested_svm_check_tlb_ctl(struct kvm_vcpu *vcpu, u8 tlb_ctl)
213{
214 /* Nested FLUSHBYASID is not supported yet. */
215 switch(tlb_ctl) {
216 case TLB_CONTROL_DO_NOTHING:
217 case TLB_CONTROL_FLUSH_ALL_ASID:
218 return true;
219 default:
220 return false;
221 }
222}
223
Paolo Bonzinibd959262021-11-11 09:14:08 -0500224static bool __nested_vmcb_check_controls(struct kvm_vcpu *vcpu,
Emanuele Giuseppe Esposito8fc78902021-11-03 10:05:26 -0400225 struct vmcb_ctrl_area_cached *control)
Paolo Bonzinica46d732020-05-18 13:02:15 -0400226{
Emanuele Giuseppe Esposito8fc78902021-11-03 10:05:26 -0400227 if (CC(!vmcb12_is_intercept(control, INTERCEPT_VMRUN)))
Paolo Bonzinica46d732020-05-18 13:02:15 -0400228 return false;
229
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800230 if (CC(control->asid == 0))
Paolo Bonzinica46d732020-05-18 13:02:15 -0400231 return false;
232
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800233 if (CC((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) && !npt_enabled))
Paolo Bonzinica46d732020-05-18 13:02:15 -0400234 return false;
235
Krish Sadhukhanee695f22021-04-12 17:56:08 -0400236 if (CC(!nested_svm_check_bitmap_pa(vcpu, control->msrpm_base_pa,
237 MSRPM_SIZE)))
238 return false;
239 if (CC(!nested_svm_check_bitmap_pa(vcpu, control->iopm_base_pa,
240 IOPM_SIZE)))
241 return false;
242
Krish Sadhukhan174a9212021-09-20 19:51:31 -0400243 if (CC(!nested_svm_check_tlb_ctl(vcpu, control->tlb_ctl)))
244 return false;
245
Paolo Bonzinica46d732020-05-18 13:02:15 -0400246 return true;
247}
248
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000249/* Common checks that apply to both L1 and L2 state. */
Emanuele Giuseppe Espositob7a3d8b2021-11-03 10:05:24 -0400250static bool __nested_vmcb_check_save(struct kvm_vcpu *vcpu,
251 struct vmcb_save_area_cached *save)
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000252{
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800253 if (CC(!(save->efer & EFER_SVME)))
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000254 return false;
255
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800256 if (CC((save->cr0 & X86_CR0_CD) == 0 && (save->cr0 & X86_CR0_NW)) ||
257 CC(save->cr0 & ~0xffffffffULL))
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000258 return false;
259
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800260 if (CC(!kvm_dr6_valid(save->dr6)) || CC(!kvm_dr7_valid(save->dr7)))
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000261 return false;
262
Emanuele Giuseppe Esposito907afa42021-11-03 10:05:21 -0400263 /*
264 * These checks are also performed by KVM_SET_SREGS,
265 * except that EFER.LMA is not checked by SVM against
266 * CR0.PG && EFER.LME.
267 */
268 if ((save->efer & EFER_LME) && (save->cr0 & X86_CR0_PG)) {
269 if (CC(!(save->cr4 & X86_CR4_PAE)) ||
270 CC(!(save->cr0 & X86_CR0_PE)) ||
271 CC(kvm_vcpu_is_illegal_gpa(vcpu, save->cr3)))
272 return false;
273 }
274
275 if (CC(!kvm_is_valid_cr4(vcpu, save->cr4)))
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000276 return false;
277
Paolo Bonzini63129752021-03-02 14:40:39 -0500278 if (CC(!kvm_valid_efer(vcpu, save->efer)))
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000279 return false;
280
281 return true;
282}
283
Emanuele Giuseppe Espositob7a3d8b2021-11-03 10:05:24 -0400284static bool nested_vmcb_check_save(struct kvm_vcpu *vcpu)
285{
286 struct vcpu_svm *svm = to_svm(vcpu);
287 struct vmcb_save_area_cached *save = &svm->nested.save;
288
289 return __nested_vmcb_check_save(vcpu, save);
290}
291
Paolo Bonzinibd959262021-11-11 09:14:08 -0500292static bool nested_vmcb_check_controls(struct kvm_vcpu *vcpu)
293{
294 struct vcpu_svm *svm = to_svm(vcpu);
Emanuele Giuseppe Esposito8fc78902021-11-03 10:05:26 -0400295 struct vmcb_ctrl_area_cached *ctl = &svm->nested.ctl;
Paolo Bonzinibd959262021-11-11 09:14:08 -0500296
297 return __nested_vmcb_check_controls(vcpu, ctl);
298}
299
Emanuele Giuseppe Esposito79071602021-11-03 10:05:23 -0400300static
Emanuele Giuseppe Esposito8fc78902021-11-03 10:05:26 -0400301void __nested_copy_vmcb_control_to_cache(struct vmcb_ctrl_area_cached *to,
Emanuele Giuseppe Esposito79071602021-11-03 10:05:23 -0400302 struct vmcb_control_area *from)
Paolo Bonzini3e06f012020-05-13 13:07:26 -0400303{
Emanuele Giuseppe Esposito79071602021-11-03 10:05:23 -0400304 unsigned int i;
Paolo Bonzini3e06f012020-05-13 13:07:26 -0400305
Emanuele Giuseppe Esposito79071602021-11-03 10:05:23 -0400306 for (i = 0; i < MAX_INTERCEPT; i++)
307 to->intercepts[i] = from->intercepts[i];
308
309 to->iopm_base_pa = from->iopm_base_pa;
310 to->msrpm_base_pa = from->msrpm_base_pa;
311 to->tsc_offset = from->tsc_offset;
312 to->tlb_ctl = from->tlb_ctl;
313 to->int_ctl = from->int_ctl;
314 to->int_vector = from->int_vector;
315 to->int_state = from->int_state;
316 to->exit_code = from->exit_code;
317 to->exit_code_hi = from->exit_code_hi;
318 to->exit_info_1 = from->exit_info_1;
319 to->exit_info_2 = from->exit_info_2;
320 to->exit_int_info = from->exit_int_info;
321 to->exit_int_info_err = from->exit_int_info_err;
322 to->nested_ctl = from->nested_ctl;
323 to->event_inj = from->event_inj;
324 to->event_inj_err = from->event_inj_err;
325 to->nested_cr3 = from->nested_cr3;
326 to->virt_ext = from->virt_ext;
327 to->pause_filter_count = from->pause_filter_count;
328 to->pause_filter_thresh = from->pause_filter_thresh;
329
330 /* Copy asid here because nested_vmcb_check_controls will check it. */
331 to->asid = from->asid;
332 to->msrpm_base_pa &= ~0x0fffULL;
333 to->iopm_base_pa &= ~0x0fffULL;
334}
335
336void nested_copy_vmcb_control_to_cache(struct vcpu_svm *svm,
337 struct vmcb_control_area *control)
338{
339 __nested_copy_vmcb_control_to_cache(&svm->nested.ctl, control);
Paolo Bonzini3e06f012020-05-13 13:07:26 -0400340}
341
Emanuele Giuseppe Espositof2740a82021-11-03 10:05:22 -0400342static void __nested_copy_vmcb_save_to_cache(struct vmcb_save_area_cached *to,
343 struct vmcb_save_area *from)
344{
345 /*
346 * Copy only fields that are validated, as we need them
347 * to avoid TOC/TOU races.
348 */
349 to->efer = from->efer;
350 to->cr0 = from->cr0;
351 to->cr3 = from->cr3;
352 to->cr4 = from->cr4;
353
354 to->dr6 = from->dr6;
355 to->dr7 = from->dr7;
356}
357
358void nested_copy_vmcb_save_to_cache(struct vcpu_svm *svm,
359 struct vmcb_save_area *save)
360{
361 __nested_copy_vmcb_save_to_cache(&svm->nested.save, save);
362}
363
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400364/*
365 * Synchronize fields that are written by the processor, so that
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500366 * they can be copied back into the vmcb12.
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400367 */
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500368void nested_sync_control_from_vmcb02(struct vcpu_svm *svm)
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400369{
370 u32 mask;
371 svm->nested.ctl.event_inj = svm->vmcb->control.event_inj;
372 svm->nested.ctl.event_inj_err = svm->vmcb->control.event_inj_err;
373
374 /* Only a few fields of int_ctl are written by the processor. */
375 mask = V_IRQ_MASK | V_TPR_MASK;
376 if (!(svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) &&
Joerg Roedela284ba52020-06-25 10:03:24 +0200377 svm_is_intercept(svm, INTERCEPT_VINTR)) {
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400378 /*
379 * In order to request an interrupt window, L0 is usurping
380 * svm->vmcb->control.int_ctl and possibly setting V_IRQ
381 * even if it was clear in L1's VMCB. Restoring it would be
382 * wrong. However, in this case V_IRQ will remain true until
383 * interrupt_window_interception calls svm_clear_vintr and
384 * restores int_ctl. We can just leave it aside.
385 */
386 mask &= ~V_IRQ_MASK;
387 }
388 svm->nested.ctl.int_ctl &= ~mask;
389 svm->nested.ctl.int_ctl |= svm->vmcb->control.int_ctl & mask;
390}
391
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400392/*
393 * Transfer any event that L0 or L1 wanted to inject into L2 to
394 * EXIT_INT_INFO.
395 */
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500396static void nested_save_pending_event_to_vmcb12(struct vcpu_svm *svm,
397 struct vmcb *vmcb12)
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400398{
399 struct kvm_vcpu *vcpu = &svm->vcpu;
400 u32 exit_int_info = 0;
401 unsigned int nr;
402
403 if (vcpu->arch.exception.injected) {
404 nr = vcpu->arch.exception.nr;
405 exit_int_info = nr | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT;
406
407 if (vcpu->arch.exception.has_error_code) {
408 exit_int_info |= SVM_EVTINJ_VALID_ERR;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300409 vmcb12->control.exit_int_info_err =
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400410 vcpu->arch.exception.error_code;
411 }
412
413 } else if (vcpu->arch.nmi_injected) {
414 exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
415
416 } else if (vcpu->arch.interrupt.injected) {
417 nr = vcpu->arch.interrupt.nr;
418 exit_int_info = nr | SVM_EVTINJ_VALID;
419
420 if (vcpu->arch.interrupt.soft)
421 exit_int_info |= SVM_EVTINJ_TYPE_SOFT;
422 else
423 exit_int_info |= SVM_EVTINJ_TYPE_INTR;
424 }
425
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300426 vmcb12->control.exit_int_info = exit_int_info;
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400427}
428
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200429static inline bool nested_npt_enabled(struct vcpu_svm *svm)
430{
431 return svm->nested.ctl.nested_ctl & SVM_NESTED_CTL_NP_ENABLE;
432}
433
Sean Christophersond2e56012021-06-09 16:42:26 -0700434static void nested_svm_transition_tlb_flush(struct kvm_vcpu *vcpu)
435{
436 /*
437 * TODO: optimize unconditional TLB flush/MMU sync. A partial list of
438 * things to fix before this can be conditional:
439 *
440 * - Flush TLBs for both L1 and L2 remote TLB flush
441 * - Honor L1's request to flush an ASID on nested VMRUN
442 * - Sync nested NPT MMU on VMRUN that flushes L2's ASID[*]
443 * - Don't crush a pending TLB flush in vmcb02 on nested VMRUN
444 * - Flush L1's ASID on KVM_REQ_TLB_FLUSH_GUEST
445 *
446 * [*] Unlike nested EPT, SVM's ASID management can invalidate nested
447 * NPT guest-physical mappings on VMRUN.
448 */
449 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
450 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
451}
452
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200453/*
Vitaly Kuznetsovd82aaef2020-07-10 16:11:56 +0200454 * Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true
455 * if we are emulating VM-Entry into a guest with NPT enabled.
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200456 */
457static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
Maxim Levitskyb222b0b2021-06-07 12:01:59 +0300458 bool nested_npt, bool reload_pdptrs)
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200459{
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800460 if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3)))
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200461 return -EINVAL;
462
Maxim Levitskyb222b0b2021-06-07 12:01:59 +0300463 if (reload_pdptrs && !nested_npt && is_pae_paging(vcpu) &&
Lai Jiangshan2df4a5e2021-11-24 20:20:52 +0800464 CC(!load_pdptrs(vcpu, cr3)))
Sean Christophersona36dbec62021-06-07 12:01:57 +0300465 return -EINVAL;
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200466
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200467 if (!nested_npt)
Sean Christophersonb5129102021-06-09 16:42:27 -0700468 kvm_mmu_new_pgd(vcpu, cr3);
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200469
470 vcpu->arch.cr3 = cr3;
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200471
Sean Christopherson616007c2021-06-22 10:57:34 -0700472 /* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */
Sean Christophersonc9060662021-06-09 16:42:33 -0700473 kvm_init_mmu(vcpu);
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200474
475 return 0;
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200476}
477
Cathy Avery4995a362021-01-13 07:07:52 -0500478void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm)
479{
480 if (!svm->nested.vmcb02.ptr)
481 return;
482
483 /* FIXME: merge g_pat from vmcb01 and vmcb12. */
484 svm->nested.vmcb02.ptr->save.g_pat = svm->vmcb01.ptr->save.g_pat;
485}
486
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500487static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100488{
Cathy Avery81733962021-03-01 15:08:44 -0500489 bool new_vmcb12 = false;
490
Cathy Avery4995a362021-01-13 07:07:52 -0500491 nested_vmcb02_compute_g_pat(svm);
492
Joerg Roedel883b0a92020-03-24 10:41:52 +0100493 /* Load the nested guest state */
Cathy Avery81733962021-03-01 15:08:44 -0500494 if (svm->nested.vmcb12_gpa != svm->nested.last_vmcb12_gpa) {
495 new_vmcb12 = true;
496 svm->nested.last_vmcb12_gpa = svm->nested.vmcb12_gpa;
497 }
498
499 if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_SEG))) {
500 svm->vmcb->save.es = vmcb12->save.es;
501 svm->vmcb->save.cs = vmcb12->save.cs;
502 svm->vmcb->save.ss = vmcb12->save.ss;
503 svm->vmcb->save.ds = vmcb12->save.ds;
504 svm->vmcb->save.cpl = vmcb12->save.cpl;
505 vmcb_mark_dirty(svm->vmcb, VMCB_SEG);
506 }
507
508 if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DT))) {
509 svm->vmcb->save.gdtr = vmcb12->save.gdtr;
510 svm->vmcb->save.idtr = vmcb12->save.idtr;
511 vmcb_mark_dirty(svm->vmcb, VMCB_DT);
512 }
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500513
Paolo Bonzini8cce12b2020-11-27 12:46:36 -0500514 kvm_set_rflags(&svm->vcpu, vmcb12->save.rflags | X86_EFLAGS_FIXED);
Paolo Bonzini3c346c02021-03-31 06:28:01 -0400515
Emanuele Giuseppe Esposito355d0472021-11-03 10:05:25 -0400516 svm_set_efer(&svm->vcpu, svm->nested.save.efer);
Paolo Bonzini3c346c02021-03-31 06:28:01 -0400517
Emanuele Giuseppe Esposito355d0472021-11-03 10:05:25 -0400518 svm_set_cr0(&svm->vcpu, svm->nested.save.cr0);
519 svm_set_cr4(&svm->vcpu, svm->nested.save.cr4);
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500520
521 svm->vcpu.arch.cr2 = vmcb12->save.cr2;
Cathy Avery81733962021-03-01 15:08:44 -0500522
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300523 kvm_rax_write(&svm->vcpu, vmcb12->save.rax);
524 kvm_rsp_write(&svm->vcpu, vmcb12->save.rsp);
525 kvm_rip_write(&svm->vcpu, vmcb12->save.rip);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100526
527 /* In case we don't even reach vcpu_run, the fields are not updated */
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300528 svm->vmcb->save.rax = vmcb12->save.rax;
529 svm->vmcb->save.rsp = vmcb12->save.rsp;
530 svm->vmcb->save.rip = vmcb12->save.rip;
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500531
Cathy Avery81733962021-03-01 15:08:44 -0500532 /* These bits will be set properly on the first execution when new_vmc12 is true */
533 if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DR))) {
Emanuele Giuseppe Esposito355d0472021-11-03 10:05:25 -0400534 svm->vmcb->save.dr7 = svm->nested.save.dr7 | DR7_FIXED_1;
535 svm->vcpu.arch.dr6 = svm->nested.save.dr6 | DR6_ACTIVE_LOW;
Cathy Avery81733962021-03-01 15:08:44 -0500536 vmcb_mark_dirty(svm->vmcb, VMCB_DR);
537 }
Paolo Bonzinif241d712020-05-18 10:56:43 -0400538}
Joerg Roedel883b0a92020-03-24 10:41:52 +0100539
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500540static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
Paolo Bonzinif241d712020-05-18 10:56:43 -0400541{
Maxim Levitsky0f923e02021-07-15 01:56:24 +0300542 const u32 int_ctl_vmcb01_bits =
543 V_INTR_MASKING_MASK | V_GIF_MASK | V_GIF_ENABLE_MASK;
544
545 const u32 int_ctl_vmcb12_bits = V_TPR_MASK | V_IRQ_INJECTION_BITS_MASK;
546
Sean Christophersond2e56012021-06-09 16:42:26 -0700547 struct kvm_vcpu *vcpu = &svm->vcpu;
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200548
Paolo Bonzini7c3ecfc2020-11-16 06:13:15 -0500549 /*
550 * Filled at exit: exit_code, exit_code_hi, exit_info_1, exit_info_2,
551 * exit_int_info, exit_int_info_err, next_rip, insn_len, insn_bytes.
552 */
Cathy Avery4995a362021-01-13 07:07:52 -0500553
Paolo Bonzini7c3ecfc2020-11-16 06:13:15 -0500554 /*
555 * Also covers avic_vapic_bar, avic_backing_page, avic_logical_id,
556 * avic_physical_id.
557 */
Maxim Levitskyfeea0132021-07-13 17:20:17 +0300558 WARN_ON(kvm_apicv_activated(svm->vcpu.kvm));
Paolo Bonzini7c3ecfc2020-11-16 06:13:15 -0500559
560 /* Copied from vmcb01. msrpm_base can be overwritten later. */
561 svm->vmcb->control.nested_ctl = svm->vmcb01.ptr->control.nested_ctl;
562 svm->vmcb->control.iopm_base_pa = svm->vmcb01.ptr->control.iopm_base_pa;
563 svm->vmcb->control.msrpm_base_pa = svm->vmcb01.ptr->control.msrpm_base_pa;
564
565 /* Done at vmrun: asid. */
566
567 /* Also overwritten later if necessary. */
568 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
569
570 /* nested_cr3. */
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200571 if (nested_npt_enabled(svm))
Sean Christophersond2e56012021-06-09 16:42:26 -0700572 nested_svm_init_mmu_context(vcpu);
Paolo Bonzini69cb8772020-05-22 05:27:46 -0400573
Maxim Levitsky5228eb92021-09-14 18:48:24 +0300574 vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset(
575 vcpu->arch.l1_tsc_offset,
576 svm->nested.ctl.tsc_offset,
577 svm->tsc_ratio_msr);
578
579 svm->vmcb->control.tsc_offset = vcpu->arch.tsc_offset;
580
581 if (svm->tsc_ratio_msr != kvm_default_tsc_scaling_ratio) {
582 WARN_ON(!svm->tsc_scaling_enabled);
583 nested_svm_update_tsc_ratio_msr(vcpu);
584 }
Joerg Roedel883b0a92020-03-24 10:41:52 +0100585
Paolo Bonzini91b71302020-05-22 12:28:52 -0400586 svm->vmcb->control.int_ctl =
Maxim Levitsky0f923e02021-07-15 01:56:24 +0300587 (svm->nested.ctl.int_ctl & int_ctl_vmcb12_bits) |
588 (svm->vmcb01.ptr->control.int_ctl & int_ctl_vmcb01_bits);
Paolo Bonzini91b71302020-05-22 12:28:52 -0400589
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400590 svm->vmcb->control.int_vector = svm->nested.ctl.int_vector;
591 svm->vmcb->control.int_state = svm->nested.ctl.int_state;
592 svm->vmcb->control.event_inj = svm->nested.ctl.event_inj;
593 svm->vmcb->control.event_inj_err = svm->nested.ctl.event_inj_err;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100594
Sean Christophersond2e56012021-06-09 16:42:26 -0700595 nested_svm_transition_tlb_flush(vcpu);
596
Joerg Roedel883b0a92020-03-24 10:41:52 +0100597 /* Enter Guest-Mode */
Sean Christophersond2e56012021-06-09 16:42:26 -0700598 enter_guest_mode(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100599
600 /*
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500601 * Merge guest and host intercepts - must be called with vcpu in
602 * guest-mode to take effect.
Joerg Roedel883b0a92020-03-24 10:41:52 +0100603 */
604 recalc_intercepts(svm);
Paolo Bonzinif241d712020-05-18 10:56:43 -0400605}
606
Babu Mogerd00b99c2021-02-17 10:56:04 -0500607static void nested_svm_copy_common_state(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
608{
609 /*
610 * Some VMCB state is shared between L1 and L2 and thus has to be
611 * moved at the time of nested vmrun and vmexit.
612 *
613 * VMLOAD/VMSAVE state would also belong in this category, but KVM
614 * always performs VMLOAD and VMSAVE from the VMCB01.
615 */
616 to_vmcb->save.spec_ctrl = from_vmcb->save.spec_ctrl;
617}
618
Paolo Bonzini63129752021-03-02 14:40:39 -0500619int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa,
Maxim Levitskye85d3e72021-09-13 17:09:51 +0300620 struct vmcb *vmcb12, bool from_vmrun)
Paolo Bonzinif241d712020-05-18 10:56:43 -0400621{
Paolo Bonzini63129752021-03-02 14:40:39 -0500622 struct vcpu_svm *svm = to_svm(vcpu);
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200623 int ret;
624
Maxim Levitsky954f4192021-02-17 16:57:13 +0200625 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb12_gpa,
626 vmcb12->save.rip,
627 vmcb12->control.int_ctl,
628 vmcb12->control.event_inj,
629 vmcb12->control.nested_ctl);
630
631 trace_kvm_nested_intercepts(vmcb12->control.intercepts[INTERCEPT_CR] & 0xffff,
632 vmcb12->control.intercepts[INTERCEPT_CR] >> 16,
633 vmcb12->control.intercepts[INTERCEPT_EXCEPTION],
634 vmcb12->control.intercepts[INTERCEPT_WORD3],
635 vmcb12->control.intercepts[INTERCEPT_WORD4],
636 vmcb12->control.intercepts[INTERCEPT_WORD5]);
637
638
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300639 svm->nested.vmcb12_gpa = vmcb12_gpa;
Cathy Avery4995a362021-01-13 07:07:52 -0500640
641 WARN_ON(svm->vmcb == svm->nested.vmcb02.ptr);
642
Babu Mogerd00b99c2021-02-17 10:56:04 -0500643 nested_svm_copy_common_state(svm->vmcb01.ptr, svm->nested.vmcb02.ptr);
Cathy Avery4995a362021-01-13 07:07:52 -0500644
645 svm_switch_vmcb(svm, &svm->nested.vmcb02);
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500646 nested_vmcb02_prepare_control(svm);
647 nested_vmcb02_prepare_save(svm, vmcb12);
Paolo Bonzinif241d712020-05-18 10:56:43 -0400648
Emanuele Giuseppe Esposito355d0472021-11-03 10:05:25 -0400649 ret = nested_svm_load_cr3(&svm->vcpu, svm->nested.save.cr3,
Maxim Levitskye85d3e72021-09-13 17:09:51 +0300650 nested_npt_enabled(svm), from_vmrun);
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200651 if (ret)
652 return ret;
653
Paolo Bonzinia04aead2021-02-18 07:16:59 -0500654 if (!npt_enabled)
Paolo Bonzini63129752021-03-02 14:40:39 -0500655 vcpu->arch.mmu->inject_page_fault = svm_inject_page_fault_nested;
Paolo Bonzinia04aead2021-02-18 07:16:59 -0500656
Maxim Levitskye85d3e72021-09-13 17:09:51 +0300657 if (!from_vmrun)
658 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
659
Paolo Bonziniffdf7f92020-05-22 12:18:27 -0400660 svm_set_gif(svm, true);
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200661
662 return 0;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100663}
664
Paolo Bonzini63129752021-03-02 14:40:39 -0500665int nested_svm_vmrun(struct kvm_vcpu *vcpu)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100666{
Paolo Bonzini63129752021-03-02 14:40:39 -0500667 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100668 int ret;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300669 struct vmcb *vmcb12;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100670 struct kvm_host_map map;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300671 u64 vmcb12_gpa;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100672
Vitaly Kuznetsovfb79f562021-06-28 12:44:21 +0200673 if (!svm->nested.hsave_msr) {
674 kvm_inject_gp(vcpu, 0);
675 return 1;
676 }
677
Paolo Bonzini63129752021-03-02 14:40:39 -0500678 if (is_smm(vcpu)) {
679 kvm_queue_exception(vcpu, UD_VECTOR);
Paolo Bonzini7c67f5462020-04-23 10:52:48 -0400680 return 1;
681 }
Joerg Roedel883b0a92020-03-24 10:41:52 +0100682
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300683 vmcb12_gpa = svm->vmcb->save.rax;
Paolo Bonzini63129752021-03-02 14:40:39 -0500684 ret = kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100685 if (ret == -EINVAL) {
Paolo Bonzini63129752021-03-02 14:40:39 -0500686 kvm_inject_gp(vcpu, 0);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100687 return 1;
688 } else if (ret) {
Paolo Bonzini63129752021-03-02 14:40:39 -0500689 return kvm_skip_emulated_instruction(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100690 }
691
Paolo Bonzini63129752021-03-02 14:40:39 -0500692 ret = kvm_skip_emulated_instruction(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100693
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300694 vmcb12 = map.hva;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100695
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300696 if (WARN_ON_ONCE(!svm->nested.initialized))
697 return -EINVAL;
698
Emanuele Giuseppe Esposito79071602021-11-03 10:05:23 -0400699 nested_copy_vmcb_control_to_cache(svm, &vmcb12->control);
Emanuele Giuseppe Espositof2740a82021-11-03 10:05:22 -0400700 nested_copy_vmcb_save_to_cache(svm, &vmcb12->save);
Paolo Bonzinicb9b6a12021-03-31 07:35:52 -0400701
Emanuele Giuseppe Espositob7a3d8b2021-11-03 10:05:24 -0400702 if (!nested_vmcb_check_save(vcpu) ||
Paolo Bonzinibd959262021-11-11 09:14:08 -0500703 !nested_vmcb_check_controls(vcpu)) {
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300704 vmcb12->control.exit_code = SVM_EXIT_ERR;
705 vmcb12->control.exit_code_hi = 0;
706 vmcb12->control.exit_info_1 = 0;
707 vmcb12->control.exit_info_2 = 0;
Paolo Bonzini69c9dfa2020-05-13 12:57:26 -0400708 goto out;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100709 }
710
Joerg Roedel883b0a92020-03-24 10:41:52 +0100711 /*
Cathy Avery4995a362021-01-13 07:07:52 -0500712 * Since vmcb01 is not in use, we can use it to store some of the L1
713 * state.
Joerg Roedel883b0a92020-03-24 10:41:52 +0100714 */
Paolo Bonzini63129752021-03-02 14:40:39 -0500715 svm->vmcb01.ptr->save.efer = vcpu->arch.efer;
716 svm->vmcb01.ptr->save.cr0 = kvm_read_cr0(vcpu);
717 svm->vmcb01.ptr->save.cr4 = vcpu->arch.cr4;
718 svm->vmcb01.ptr->save.rflags = kvm_get_rflags(vcpu);
719 svm->vmcb01.ptr->save.rip = kvm_rip_read(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100720
Cathy Avery4995a362021-01-13 07:07:52 -0500721 if (!npt_enabled)
Paolo Bonzini63129752021-03-02 14:40:39 -0500722 svm->vmcb01.ptr->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100723
Paolo Bonzinif74f9412020-04-23 13:22:27 -0400724 svm->nested.nested_run_pending = 1;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100725
Maxim Levitskye85d3e72021-09-13 17:09:51 +0300726 if (enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12, true))
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200727 goto out_exit_err;
Vitaly Kuznetsovebdb3db2020-07-10 16:11:51 +0200728
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200729 if (nested_svm_vmrun_msrpm(svm))
730 goto out;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100731
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200732out_exit_err:
733 svm->nested.nested_run_pending = 0;
734
735 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
736 svm->vmcb->control.exit_code_hi = 0;
737 svm->vmcb->control.exit_info_1 = 0;
738 svm->vmcb->control.exit_info_2 = 0;
739
740 nested_svm_vmexit(svm);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100741
Paolo Bonzini69c9dfa2020-05-13 12:57:26 -0400742out:
Paolo Bonzini63129752021-03-02 14:40:39 -0500743 kvm_vcpu_unmap(vcpu, &map, true);
Paolo Bonzini69c9dfa2020-05-13 12:57:26 -0400744
Joerg Roedel883b0a92020-03-24 10:41:52 +0100745 return ret;
746}
747
Vitaly Kuznetsov0a758292021-06-28 12:44:22 +0200748/* Copy state save area fields which are handled by VMRUN */
Vitaly Kuznetsov2bb16be2021-07-19 11:03:22 +0200749void svm_copy_vmrun_state(struct vmcb_save_area *to_save,
750 struct vmcb_save_area *from_save)
Vitaly Kuznetsov0a758292021-06-28 12:44:22 +0200751{
752 to_save->es = from_save->es;
753 to_save->cs = from_save->cs;
754 to_save->ss = from_save->ss;
755 to_save->ds = from_save->ds;
756 to_save->gdtr = from_save->gdtr;
757 to_save->idtr = from_save->idtr;
758 to_save->rflags = from_save->rflags | X86_EFLAGS_FIXED;
759 to_save->efer = from_save->efer;
760 to_save->cr0 = from_save->cr0;
761 to_save->cr3 = from_save->cr3;
762 to_save->cr4 = from_save->cr4;
763 to_save->rax = from_save->rax;
764 to_save->rsp = from_save->rsp;
765 to_save->rip = from_save->rip;
766 to_save->cpl = 0;
767}
768
Vitaly Kuznetsov2bb16be2021-07-19 11:03:22 +0200769void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmcb)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100770{
771 to_vmcb->save.fs = from_vmcb->save.fs;
772 to_vmcb->save.gs = from_vmcb->save.gs;
773 to_vmcb->save.tr = from_vmcb->save.tr;
774 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
775 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
776 to_vmcb->save.star = from_vmcb->save.star;
777 to_vmcb->save.lstar = from_vmcb->save.lstar;
778 to_vmcb->save.cstar = from_vmcb->save.cstar;
779 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
780 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
781 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
782 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
783}
784
785int nested_svm_vmexit(struct vcpu_svm *svm)
786{
Paolo Bonzini63129752021-03-02 14:40:39 -0500787 struct kvm_vcpu *vcpu = &svm->vcpu;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300788 struct vmcb *vmcb12;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100789 struct vmcb *vmcb = svm->vmcb;
790 struct kvm_host_map map;
Paolo Bonzini63129752021-03-02 14:40:39 -0500791 int rc;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100792
Sean Christophersoncb6a32c2021-03-02 09:45:14 -0800793 /* Triple faults in L2 should never escape. */
794 WARN_ON_ONCE(kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu));
795
Paolo Bonzini63129752021-03-02 14:40:39 -0500796 rc = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.vmcb12_gpa), &map);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100797 if (rc) {
798 if (rc == -EINVAL)
Paolo Bonzini63129752021-03-02 14:40:39 -0500799 kvm_inject_gp(vcpu, 0);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100800 return 1;
801 }
802
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300803 vmcb12 = map.hva;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100804
805 /* Exit Guest-Mode */
Paolo Bonzini63129752021-03-02 14:40:39 -0500806 leave_guest_mode(vcpu);
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300807 svm->nested.vmcb12_gpa = 0;
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400808 WARN_ON_ONCE(svm->nested.nested_run_pending);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100809
Paolo Bonzini63129752021-03-02 14:40:39 -0500810 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Maxim Levitskyf2c7ef32021-01-07 11:38:51 +0200811
Paolo Bonzini38c0b192020-04-23 13:13:09 -0400812 /* in case we halted in L2 */
813 svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE;
814
Joerg Roedel883b0a92020-03-24 10:41:52 +0100815 /* Give the current vmcb to the guest */
Joerg Roedel883b0a92020-03-24 10:41:52 +0100816
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300817 vmcb12->save.es = vmcb->save.es;
818 vmcb12->save.cs = vmcb->save.cs;
819 vmcb12->save.ss = vmcb->save.ss;
820 vmcb12->save.ds = vmcb->save.ds;
821 vmcb12->save.gdtr = vmcb->save.gdtr;
822 vmcb12->save.idtr = vmcb->save.idtr;
823 vmcb12->save.efer = svm->vcpu.arch.efer;
Paolo Bonzini63129752021-03-02 14:40:39 -0500824 vmcb12->save.cr0 = kvm_read_cr0(vcpu);
825 vmcb12->save.cr3 = kvm_read_cr3(vcpu);
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300826 vmcb12->save.cr2 = vmcb->save.cr2;
827 vmcb12->save.cr4 = svm->vcpu.arch.cr4;
Paolo Bonzini63129752021-03-02 14:40:39 -0500828 vmcb12->save.rflags = kvm_get_rflags(vcpu);
829 vmcb12->save.rip = kvm_rip_read(vcpu);
830 vmcb12->save.rsp = kvm_rsp_read(vcpu);
831 vmcb12->save.rax = kvm_rax_read(vcpu);
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300832 vmcb12->save.dr7 = vmcb->save.dr7;
833 vmcb12->save.dr6 = svm->vcpu.arch.dr6;
834 vmcb12->save.cpl = vmcb->save.cpl;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100835
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300836 vmcb12->control.int_state = vmcb->control.int_state;
837 vmcb12->control.exit_code = vmcb->control.exit_code;
838 vmcb12->control.exit_code_hi = vmcb->control.exit_code_hi;
839 vmcb12->control.exit_info_1 = vmcb->control.exit_info_1;
840 vmcb12->control.exit_info_2 = vmcb->control.exit_info_2;
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400841
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300842 if (vmcb12->control.exit_code != SVM_EXIT_ERR)
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500843 nested_save_pending_event_to_vmcb12(svm, vmcb12);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100844
845 if (svm->nrips_enabled)
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300846 vmcb12->control.next_rip = vmcb->control.next_rip;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100847
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300848 vmcb12->control.int_ctl = svm->nested.ctl.int_ctl;
849 vmcb12->control.tlb_ctl = svm->nested.ctl.tlb_ctl;
850 vmcb12->control.event_inj = svm->nested.ctl.event_inj;
851 vmcb12->control.event_inj_err = svm->nested.ctl.event_inj_err;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100852
Babu Mogerd00b99c2021-02-17 10:56:04 -0500853 nested_svm_copy_common_state(svm->nested.vmcb02.ptr, svm->vmcb01.ptr);
854
Cathy Avery4995a362021-01-13 07:07:52 -0500855 svm_switch_vmcb(svm, &svm->vmcb01);
856
857 /*
858 * On vmexit the GIF is set to false and
859 * no event can be injected in L1.
860 */
Maxim Levitsky98837642020-08-27 19:27:18 +0300861 svm_set_gif(svm, false);
Cathy Avery4995a362021-01-13 07:07:52 -0500862 svm->vmcb->control.exit_int_info = 0;
Maxim Levitsky98837642020-08-27 19:27:18 +0300863
Paolo Bonzini7ca62d12020-11-16 06:38:19 -0500864 svm->vcpu.arch.tsc_offset = svm->vcpu.arch.l1_tsc_offset;
865 if (svm->vmcb->control.tsc_offset != svm->vcpu.arch.tsc_offset) {
866 svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset;
867 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
868 }
Paolo Bonzini18fc6c52020-05-18 11:07:08 -0400869
Maxim Levitsky5228eb92021-09-14 18:48:24 +0300870 if (svm->tsc_ratio_msr != kvm_default_tsc_scaling_ratio) {
871 WARN_ON(!svm->tsc_scaling_enabled);
872 vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio;
873 svm_write_tsc_multiplier(vcpu, vcpu->arch.tsc_scaling_ratio);
874 }
875
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400876 svm->nested.ctl.nested_cr3 = 0;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100877
Cathy Avery4995a362021-01-13 07:07:52 -0500878 /*
879 * Restore processor state that had been saved in vmcb01
880 */
Paolo Bonzini63129752021-03-02 14:40:39 -0500881 kvm_set_rflags(vcpu, svm->vmcb->save.rflags);
882 svm_set_efer(vcpu, svm->vmcb->save.efer);
883 svm_set_cr0(vcpu, svm->vmcb->save.cr0 | X86_CR0_PE);
884 svm_set_cr4(vcpu, svm->vmcb->save.cr4);
885 kvm_rax_write(vcpu, svm->vmcb->save.rax);
886 kvm_rsp_write(vcpu, svm->vmcb->save.rsp);
887 kvm_rip_write(vcpu, svm->vmcb->save.rip);
Cathy Avery4995a362021-01-13 07:07:52 -0500888
889 svm->vcpu.arch.dr7 = DR7_FIXED_1;
890 kvm_update_dr7(&svm->vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100891
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300892 trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code,
893 vmcb12->control.exit_info_1,
894 vmcb12->control.exit_info_2,
895 vmcb12->control.exit_int_info,
896 vmcb12->control.exit_int_info_err,
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400897 KVM_ISA_SVM);
898
Paolo Bonzini63129752021-03-02 14:40:39 -0500899 kvm_vcpu_unmap(vcpu, &map, true);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100900
Sean Christophersond2e56012021-06-09 16:42:26 -0700901 nested_svm_transition_tlb_flush(vcpu);
902
Paolo Bonzini63129752021-03-02 14:40:39 -0500903 nested_svm_uninit_mmu_context(vcpu);
Vitaly Kuznetsovbf7dea42020-07-10 16:11:54 +0200904
Maxim Levitskyb222b0b2021-06-07 12:01:59 +0300905 rc = nested_svm_load_cr3(vcpu, svm->vmcb->save.cr3, false, true);
Vitaly Kuznetsovd82aaef2020-07-10 16:11:56 +0200906 if (rc)
907 return 1;
Vitaly Kuznetsovbf7dea42020-07-10 16:11:54 +0200908
Joerg Roedel883b0a92020-03-24 10:41:52 +0100909 /*
910 * Drop what we picked up for L2 via svm_complete_interrupts() so it
911 * doesn't end up in L1.
912 */
913 svm->vcpu.arch.nmi_injected = false;
Paolo Bonzini63129752021-03-02 14:40:39 -0500914 kvm_clear_exception_queue(vcpu);
915 kvm_clear_interrupt_queue(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100916
Krish Sadhukhan9a7de6e2021-03-23 13:50:03 -0400917 /*
918 * If we are here following the completion of a VMRUN that
919 * is being single-stepped, queue the pending #DB intercept
920 * right now so that it an be accounted for before we execute
921 * L1's next instruction.
922 */
923 if (unlikely(svm->vmcb->save.rflags & X86_EFLAGS_TF))
924 kvm_queue_exception(&(svm->vcpu), DB_VECTOR);
925
Joerg Roedel883b0a92020-03-24 10:41:52 +0100926 return 0;
927}
928
Sean Christophersoncb6a32c2021-03-02 09:45:14 -0800929static void nested_svm_triple_fault(struct kvm_vcpu *vcpu)
930{
Sean Christopherson3a87c7e2021-03-02 09:45:15 -0800931 nested_svm_simple_vmexit(to_svm(vcpu), SVM_EXIT_SHUTDOWN);
Sean Christophersoncb6a32c2021-03-02 09:45:14 -0800932}
933
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300934int svm_allocate_nested(struct vcpu_svm *svm)
935{
Cathy Avery4995a362021-01-13 07:07:52 -0500936 struct page *vmcb02_page;
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300937
938 if (svm->nested.initialized)
939 return 0;
940
Cathy Avery4995a362021-01-13 07:07:52 -0500941 vmcb02_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
942 if (!vmcb02_page)
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300943 return -ENOMEM;
Cathy Avery4995a362021-01-13 07:07:52 -0500944 svm->nested.vmcb02.ptr = page_address(vmcb02_page);
945 svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300946
947 svm->nested.msrpm = svm_vcpu_alloc_msrpm();
948 if (!svm->nested.msrpm)
Cathy Avery4995a362021-01-13 07:07:52 -0500949 goto err_free_vmcb02;
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300950 svm_vcpu_init_msrpm(&svm->vcpu, svm->nested.msrpm);
951
952 svm->nested.initialized = true;
953 return 0;
954
Cathy Avery4995a362021-01-13 07:07:52 -0500955err_free_vmcb02:
956 __free_page(vmcb02_page);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300957 return -ENOMEM;
958}
959
960void svm_free_nested(struct vcpu_svm *svm)
961{
962 if (!svm->nested.initialized)
963 return;
964
965 svm_vcpu_free_msrpm(svm->nested.msrpm);
966 svm->nested.msrpm = NULL;
967
Cathy Avery4995a362021-01-13 07:07:52 -0500968 __free_page(virt_to_page(svm->nested.vmcb02.ptr));
969 svm->nested.vmcb02.ptr = NULL;
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300970
Maxim Levitskyc74ad082021-05-03 15:54:43 +0300971 /*
972 * When last_vmcb12_gpa matches the current vmcb12 gpa,
973 * some vmcb12 fields are not loaded if they are marked clean
974 * in the vmcb12, since in this case they are up to date already.
975 *
976 * When the vmcb02 is freed, this optimization becomes invalid.
977 */
978 svm->nested.last_vmcb12_gpa = INVALID_GPA;
979
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300980 svm->nested.initialized = false;
981}
982
Paolo Bonzinic513f482020-05-18 13:08:37 -0400983/*
984 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
985 */
Sean Christophersonf7e57072022-01-25 22:03:58 +0000986void svm_leave_nested(struct kvm_vcpu *vcpu)
Paolo Bonzinic513f482020-05-18 13:08:37 -0400987{
Sean Christophersonf7e57072022-01-25 22:03:58 +0000988 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzini63129752021-03-02 14:40:39 -0500989
990 if (is_guest_mode(vcpu)) {
Paolo Bonzinic513f482020-05-18 13:08:37 -0400991 svm->nested.nested_run_pending = 0;
Maxim Levitskyc74ad082021-05-03 15:54:43 +0300992 svm->nested.vmcb12_gpa = INVALID_GPA;
993
Paolo Bonzini63129752021-03-02 14:40:39 -0500994 leave_guest_mode(vcpu);
Cathy Avery4995a362021-01-13 07:07:52 -0500995
Maxim Levitskydeee59b2021-05-03 15:54:42 +0300996 svm_switch_vmcb(svm, &svm->vmcb01);
Cathy Avery4995a362021-01-13 07:07:52 -0500997
Paolo Bonzini63129752021-03-02 14:40:39 -0500998 nested_svm_uninit_mmu_context(vcpu);
Maxim Levitsky56fe28d2021-01-07 11:38:54 +0200999 vmcb_mark_all_dirty(svm->vmcb);
Paolo Bonzinic513f482020-05-18 13:08:37 -04001000 }
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -04001001
Paolo Bonzini63129752021-03-02 14:40:39 -05001002 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Paolo Bonzinic513f482020-05-18 13:08:37 -04001003}
1004
Joerg Roedel883b0a92020-03-24 10:41:52 +01001005static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
1006{
1007 u32 offset, msr, value;
1008 int write, mask;
1009
Emanuele Giuseppe Esposito8fc78902021-11-03 10:05:26 -04001010 if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +01001011 return NESTED_EXIT_HOST;
1012
1013 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1014 offset = svm_msrpm_offset(msr);
1015 write = svm->vmcb->control.exit_info_1 & 1;
1016 mask = 1 << ((2 * (msr & 0xf)) + write);
1017
1018 if (offset == MSR_INVALID)
1019 return NESTED_EXIT_DONE;
1020
1021 /* Offset is in 32 bit units but need in 8 bit units */
1022 offset *= 4;
1023
Paolo Bonzinie670bf62020-05-13 13:16:12 -04001024 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4))
Joerg Roedel883b0a92020-03-24 10:41:52 +01001025 return NESTED_EXIT_DONE;
1026
1027 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1028}
1029
Joerg Roedel883b0a92020-03-24 10:41:52 +01001030static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
1031{
1032 unsigned port, size, iopm_len;
1033 u16 val, mask;
1034 u8 start_bit;
1035 u64 gpa;
1036
Emanuele Giuseppe Esposito8fc78902021-11-03 10:05:26 -04001037 if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_IOIO_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +01001038 return NESTED_EXIT_HOST;
1039
1040 port = svm->vmcb->control.exit_info_1 >> 16;
1041 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
1042 SVM_IOIO_SIZE_SHIFT;
Paolo Bonzinie670bf62020-05-13 13:16:12 -04001043 gpa = svm->nested.ctl.iopm_base_pa + (port / 8);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001044 start_bit = port % 8;
1045 iopm_len = (start_bit + size > 8) ? 2 : 1;
1046 mask = (0xf >> (4 - size)) << start_bit;
1047 val = 0;
1048
1049 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
1050 return NESTED_EXIT_DONE;
1051
1052 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1053}
1054
1055static int nested_svm_intercept(struct vcpu_svm *svm)
1056{
1057 u32 exit_code = svm->vmcb->control.exit_code;
1058 int vmexit = NESTED_EXIT_HOST;
1059
1060 switch (exit_code) {
1061 case SVM_EXIT_MSR:
1062 vmexit = nested_svm_exit_handled_msr(svm);
1063 break;
1064 case SVM_EXIT_IOIO:
1065 vmexit = nested_svm_intercept_ioio(svm);
1066 break;
1067 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
Emanuele Giuseppe Esposito8fc78902021-11-03 10:05:26 -04001068 if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
Joerg Roedel883b0a92020-03-24 10:41:52 +01001069 vmexit = NESTED_EXIT_DONE;
1070 break;
1071 }
1072 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
Emanuele Giuseppe Esposito8fc78902021-11-03 10:05:26 -04001073 if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
Joerg Roedel883b0a92020-03-24 10:41:52 +01001074 vmexit = NESTED_EXIT_DONE;
1075 break;
1076 }
1077 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
Paolo Bonzini7c866632020-05-16 08:42:28 -04001078 /*
1079 * Host-intercepted exceptions have been checked already in
1080 * nested_svm_exit_special. There is nothing to do here,
1081 * the vmexit is injected by svm_check_nested_events.
1082 */
1083 vmexit = NESTED_EXIT_DONE;
Joerg Roedel883b0a92020-03-24 10:41:52 +01001084 break;
1085 }
1086 case SVM_EXIT_ERR: {
1087 vmexit = NESTED_EXIT_DONE;
1088 break;
1089 }
1090 default: {
Emanuele Giuseppe Esposito8fc78902021-11-03 10:05:26 -04001091 if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
Joerg Roedel883b0a92020-03-24 10:41:52 +01001092 vmexit = NESTED_EXIT_DONE;
1093 }
1094 }
1095
1096 return vmexit;
1097}
1098
1099int nested_svm_exit_handled(struct vcpu_svm *svm)
1100{
1101 int vmexit;
1102
1103 vmexit = nested_svm_intercept(svm);
1104
1105 if (vmexit == NESTED_EXIT_DONE)
1106 nested_svm_vmexit(svm);
1107
1108 return vmexit;
1109}
1110
Paolo Bonzini63129752021-03-02 14:40:39 -05001111int nested_svm_check_permissions(struct kvm_vcpu *vcpu)
Joerg Roedel883b0a92020-03-24 10:41:52 +01001112{
Paolo Bonzini63129752021-03-02 14:40:39 -05001113 if (!(vcpu->arch.efer & EFER_SVME) || !is_paging(vcpu)) {
1114 kvm_queue_exception(vcpu, UD_VECTOR);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001115 return 1;
1116 }
1117
Paolo Bonzini63129752021-03-02 14:40:39 -05001118 if (to_svm(vcpu)->vmcb->save.cpl) {
1119 kvm_inject_gp(vcpu, 0);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001120 return 1;
1121 }
1122
1123 return 0;
1124}
1125
Paolo Bonzini7c866632020-05-16 08:42:28 -04001126static bool nested_exit_on_exception(struct vcpu_svm *svm)
Joerg Roedel883b0a92020-03-24 10:41:52 +01001127{
Paolo Bonzini7c866632020-05-16 08:42:28 -04001128 unsigned int nr = svm->vcpu.arch.exception.nr;
Joerg Roedel883b0a92020-03-24 10:41:52 +01001129
Babu Moger9780d512020-09-11 14:28:20 -05001130 return (svm->nested.ctl.intercepts[INTERCEPT_EXCEPTION] & BIT(nr));
Paolo Bonzini7c866632020-05-16 08:42:28 -04001131}
Joerg Roedel883b0a92020-03-24 10:41:52 +01001132
Paolo Bonzini7c866632020-05-16 08:42:28 -04001133static void nested_svm_inject_exception_vmexit(struct vcpu_svm *svm)
1134{
1135 unsigned int nr = svm->vcpu.arch.exception.nr;
Joerg Roedel883b0a92020-03-24 10:41:52 +01001136
1137 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1138 svm->vmcb->control.exit_code_hi = 0;
Paolo Bonzini7c866632020-05-16 08:42:28 -04001139
1140 if (svm->vcpu.arch.exception.has_error_code)
1141 svm->vmcb->control.exit_info_1 = svm->vcpu.arch.exception.error_code;
Joerg Roedel883b0a92020-03-24 10:41:52 +01001142
1143 /*
1144 * EXITINFO2 is undefined for all exception intercepts other
1145 * than #PF.
1146 */
Paolo Bonzini7c866632020-05-16 08:42:28 -04001147 if (nr == PF_VECTOR) {
1148 if (svm->vcpu.arch.exception.nested_apf)
1149 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
1150 else if (svm->vcpu.arch.exception.has_payload)
1151 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload;
1152 else
1153 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1154 } else if (nr == DB_VECTOR) {
1155 /* See inject_pending_event. */
1156 kvm_deliver_exception_payload(&svm->vcpu);
1157 if (svm->vcpu.arch.dr7 & DR7_GD) {
1158 svm->vcpu.arch.dr7 &= ~DR7_GD;
1159 kvm_update_dr7(&svm->vcpu);
1160 }
1161 } else
1162 WARN_ON(svm->vcpu.arch.exception.has_payload);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001163
Paolo Bonzini7c866632020-05-16 08:42:28 -04001164 nested_svm_vmexit(svm);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001165}
1166
Paolo Bonzini5b6724082020-05-16 08:50:35 -04001167static inline bool nested_exit_on_init(struct vcpu_svm *svm)
1168{
Emanuele Giuseppe Esposito8fc78902021-11-03 10:05:26 -04001169 return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_INIT);
Paolo Bonzini5b6724082020-05-16 08:50:35 -04001170}
1171
Paolo Bonzini33b22172020-04-17 10:24:18 -04001172static int svm_check_nested_events(struct kvm_vcpu *vcpu)
Joerg Roedel883b0a92020-03-24 10:41:52 +01001173{
1174 struct vcpu_svm *svm = to_svm(vcpu);
1175 bool block_nested_events =
Paolo Bonzinibd279622020-05-16 08:46:00 -04001176 kvm_event_needs_reinjection(vcpu) || svm->nested.nested_run_pending;
Paolo Bonzini5b6724082020-05-16 08:50:35 -04001177 struct kvm_lapic *apic = vcpu->arch.apic;
1178
1179 if (lapic_in_kernel(vcpu) &&
1180 test_bit(KVM_APIC_INIT, &apic->pending_events)) {
1181 if (block_nested_events)
1182 return -EBUSY;
1183 if (!nested_exit_on_init(svm))
1184 return 0;
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08001185 nested_svm_simple_vmexit(svm, SVM_EXIT_INIT);
Paolo Bonzini5b6724082020-05-16 08:50:35 -04001186 return 0;
1187 }
Joerg Roedel883b0a92020-03-24 10:41:52 +01001188
Paolo Bonzini7c866632020-05-16 08:42:28 -04001189 if (vcpu->arch.exception.pending) {
Maxim Levitsky4020da32021-04-01 17:38:14 +03001190 /*
1191 * Only a pending nested run can block a pending exception.
1192 * Otherwise an injected NMI/interrupt should either be
1193 * lost or delivered to the nested hypervisor in the EXITINTINFO
1194 * vmcb field, while delivering the pending exception.
1195 */
1196 if (svm->nested.nested_run_pending)
Paolo Bonzini7c866632020-05-16 08:42:28 -04001197 return -EBUSY;
1198 if (!nested_exit_on_exception(svm))
1199 return 0;
1200 nested_svm_inject_exception_vmexit(svm);
1201 return 0;
1202 }
1203
Paolo Bonzini221e7612020-04-23 08:13:10 -04001204 if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) {
Paolo Bonzini55714cd2020-04-23 08:17:28 -04001205 if (block_nested_events)
1206 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -04001207 if (!nested_exit_on_smi(svm))
1208 return 0;
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08001209 nested_svm_simple_vmexit(svm, SVM_EXIT_SMI);
Paolo Bonzini55714cd2020-04-23 08:17:28 -04001210 return 0;
1211 }
1212
Paolo Bonzini221e7612020-04-23 08:13:10 -04001213 if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) {
Cathy Avery9c3d3702020-04-14 16:11:06 -04001214 if (block_nested_events)
1215 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -04001216 if (!nested_exit_on_nmi(svm))
1217 return 0;
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08001218 nested_svm_simple_vmexit(svm, SVM_EXIT_NMI);
Cathy Avery9c3d3702020-04-14 16:11:06 -04001219 return 0;
1220 }
1221
Paolo Bonzini221e7612020-04-23 08:13:10 -04001222 if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) {
Joerg Roedel883b0a92020-03-24 10:41:52 +01001223 if (block_nested_events)
1224 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -04001225 if (!nested_exit_on_intr(svm))
1226 return 0;
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08001227 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
1228 nested_svm_simple_vmexit(svm, SVM_EXIT_INTR);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001229 return 0;
1230 }
1231
1232 return 0;
1233}
1234
1235int nested_svm_exit_special(struct vcpu_svm *svm)
1236{
1237 u32 exit_code = svm->vmcb->control.exit_code;
1238
1239 switch (exit_code) {
1240 case SVM_EXIT_INTR:
1241 case SVM_EXIT_NMI:
Joerg Roedel883b0a92020-03-24 10:41:52 +01001242 case SVM_EXIT_NPF:
Paolo Bonzini7c866632020-05-16 08:42:28 -04001243 return NESTED_EXIT_HOST;
1244 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1245 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
1246
Cathy Avery4995a362021-01-13 07:07:52 -05001247 if (svm->vmcb01.ptr->control.intercepts[INTERCEPT_EXCEPTION] &
1248 excp_bits)
Paolo Bonzini7c866632020-05-16 08:42:28 -04001249 return NESTED_EXIT_HOST;
1250 else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02001251 svm->vcpu.arch.apf.host_apf_flags)
Paolo Bonzini7c866632020-05-16 08:42:28 -04001252 /* Trap async PF even if not shadowing */
Joerg Roedel883b0a92020-03-24 10:41:52 +01001253 return NESTED_EXIT_HOST;
1254 break;
Paolo Bonzini7c866632020-05-16 08:42:28 -04001255 }
Joerg Roedel883b0a92020-03-24 10:41:52 +01001256 default:
1257 break;
1258 }
1259
1260 return NESTED_EXIT_CONTINUE;
1261}
Paolo Bonzini33b22172020-04-17 10:24:18 -04001262
Maxim Levitsky5228eb92021-09-14 18:48:24 +03001263void nested_svm_update_tsc_ratio_msr(struct kvm_vcpu *vcpu)
1264{
1265 struct vcpu_svm *svm = to_svm(vcpu);
1266
1267 vcpu->arch.tsc_scaling_ratio =
1268 kvm_calc_nested_tsc_multiplier(vcpu->arch.l1_tsc_scaling_ratio,
1269 svm->tsc_ratio_msr);
1270 svm_write_tsc_multiplier(vcpu, vcpu->arch.tsc_scaling_ratio);
1271}
1272
Emanuele Giuseppe Esposito8fc78902021-11-03 10:05:26 -04001273/* Inverse operation of nested_copy_vmcb_control_to_cache(). asid is copied too. */
1274static void nested_copy_vmcb_cache_to_control(struct vmcb_control_area *dst,
1275 struct vmcb_ctrl_area_cached *from)
1276{
1277 unsigned int i;
1278
1279 memset(dst, 0, sizeof(struct vmcb_control_area));
1280
1281 for (i = 0; i < MAX_INTERCEPT; i++)
1282 dst->intercepts[i] = from->intercepts[i];
1283
1284 dst->iopm_base_pa = from->iopm_base_pa;
1285 dst->msrpm_base_pa = from->msrpm_base_pa;
1286 dst->tsc_offset = from->tsc_offset;
1287 dst->asid = from->asid;
1288 dst->tlb_ctl = from->tlb_ctl;
1289 dst->int_ctl = from->int_ctl;
1290 dst->int_vector = from->int_vector;
1291 dst->int_state = from->int_state;
1292 dst->exit_code = from->exit_code;
1293 dst->exit_code_hi = from->exit_code_hi;
1294 dst->exit_info_1 = from->exit_info_1;
1295 dst->exit_info_2 = from->exit_info_2;
1296 dst->exit_int_info = from->exit_int_info;
1297 dst->exit_int_info_err = from->exit_int_info_err;
1298 dst->nested_ctl = from->nested_ctl;
1299 dst->event_inj = from->event_inj;
1300 dst->event_inj_err = from->event_inj_err;
1301 dst->nested_cr3 = from->nested_cr3;
1302 dst->virt_ext = from->virt_ext;
1303 dst->pause_filter_count = from->pause_filter_count;
1304 dst->pause_filter_thresh = from->pause_filter_thresh;
1305}
1306
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001307static int svm_get_nested_state(struct kvm_vcpu *vcpu,
1308 struct kvm_nested_state __user *user_kvm_nested_state,
1309 u32 user_data_size)
1310{
1311 struct vcpu_svm *svm;
Emanuele Giuseppe Esposito8fc78902021-11-03 10:05:26 -04001312 struct vmcb_control_area *ctl;
1313 unsigned long r;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001314 struct kvm_nested_state kvm_state = {
1315 .flags = 0,
1316 .format = KVM_STATE_NESTED_FORMAT_SVM,
1317 .size = sizeof(kvm_state),
1318 };
1319 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1320 &user_kvm_nested_state->data.svm[0];
1321
1322 if (!vcpu)
1323 return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE;
1324
1325 svm = to_svm(vcpu);
1326
1327 if (user_data_size < kvm_state.size)
1328 goto out;
1329
1330 /* First fill in the header and copy it out. */
1331 if (is_guest_mode(vcpu)) {
Maxim Levitsky0dd16b52020-08-27 20:11:39 +03001332 kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001333 kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE;
1334 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
1335
1336 if (svm->nested.nested_run_pending)
1337 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
1338 }
1339
1340 if (gif_set(svm))
1341 kvm_state.flags |= KVM_STATE_NESTED_GIF_SET;
1342
1343 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
1344 return -EFAULT;
1345
1346 if (!is_guest_mode(vcpu))
1347 goto out;
1348
1349 /*
1350 * Copy over the full size of the VMCB rather than just the size
1351 * of the structs.
1352 */
1353 if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE))
1354 return -EFAULT;
Emanuele Giuseppe Esposito8fc78902021-11-03 10:05:26 -04001355
1356 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
1357 if (!ctl)
1358 return -ENOMEM;
1359
1360 nested_copy_vmcb_cache_to_control(ctl, &svm->nested.ctl);
1361 r = copy_to_user(&user_vmcb->control, ctl,
1362 sizeof(user_vmcb->control));
1363 kfree(ctl);
1364 if (r)
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001365 return -EFAULT;
Emanuele Giuseppe Esposito8fc78902021-11-03 10:05:26 -04001366
Cathy Avery4995a362021-01-13 07:07:52 -05001367 if (copy_to_user(&user_vmcb->save, &svm->vmcb01.ptr->save,
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001368 sizeof(user_vmcb->save)))
1369 return -EFAULT;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001370out:
1371 return kvm_state.size;
1372}
1373
1374static int svm_set_nested_state(struct kvm_vcpu *vcpu,
1375 struct kvm_nested_state __user *user_kvm_nested_state,
1376 struct kvm_nested_state *kvm_state)
1377{
1378 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001379 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1380 &user_kvm_nested_state->data.svm[0];
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001381 struct vmcb_control_area *ctl;
1382 struct vmcb_save_area *save;
Emanuele Giuseppe Espositob7a3d8b2021-11-03 10:05:24 -04001383 struct vmcb_save_area_cached save_cached;
Emanuele Giuseppe Esposito8fc78902021-11-03 10:05:26 -04001384 struct vmcb_ctrl_area_cached ctl_cached;
Sean Christophersondbc47392021-06-22 10:56:59 -07001385 unsigned long cr0;
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001386 int ret;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001387
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001388 BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >
1389 KVM_STATE_NESTED_SVM_VMCB_SIZE);
1390
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001391 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM)
1392 return -EINVAL;
1393
1394 if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE |
1395 KVM_STATE_NESTED_RUN_PENDING |
1396 KVM_STATE_NESTED_GIF_SET))
1397 return -EINVAL;
1398
1399 /*
1400 * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
1401 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
1402 */
1403 if (!(vcpu->arch.efer & EFER_SVME)) {
1404 /* GIF=1 and no guest mode are required if SVME=0. */
1405 if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
1406 return -EINVAL;
1407 }
1408
1409 /* SMM temporarily disables SVM, so we cannot be in guest mode. */
1410 if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
1411 return -EINVAL;
1412
1413 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) {
Sean Christophersonf7e57072022-01-25 22:03:58 +00001414 svm_leave_nested(vcpu);
Vitaly Kuznetsovd5cd6f32020-09-14 15:37:25 +02001415 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1416 return 0;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001417 }
1418
1419 if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa))
1420 return -EINVAL;
1421 if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)
1422 return -EINVAL;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001423
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001424 ret = -ENOMEM;
Sean Christophersoneba04b22021-03-30 19:30:25 -07001425 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL_ACCOUNT);
1426 save = kzalloc(sizeof(*save), GFP_KERNEL_ACCOUNT);
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001427 if (!ctl || !save)
1428 goto out_free;
1429
1430 ret = -EFAULT;
1431 if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl)))
1432 goto out_free;
1433 if (copy_from_user(save, &user_vmcb->save, sizeof(*save)))
1434 goto out_free;
1435
1436 ret = -EINVAL;
Emanuele Giuseppe Esposito8fc78902021-11-03 10:05:26 -04001437 __nested_copy_vmcb_control_to_cache(&ctl_cached, ctl);
1438 if (!__nested_vmcb_check_controls(vcpu, &ctl_cached))
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001439 goto out_free;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001440
1441 /*
1442 * Processor state contains L2 state. Check that it is
Paolo Bonzinicb9b6a12021-03-31 07:35:52 -04001443 * valid for guest mode (see nested_vmcb_check_save).
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001444 */
1445 cr0 = kvm_read_cr0(vcpu);
1446 if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001447 goto out_free;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001448
1449 /*
1450 * Validate host state saved from before VMRUN (see
1451 * nested_svm_check_permissions).
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001452 */
Emanuele Giuseppe Espositob7a3d8b2021-11-03 10:05:24 -04001453 __nested_copy_vmcb_save_to_cache(&save_cached, save);
Krish Sadhukhan6906e062020-10-06 19:06:52 +00001454 if (!(save->cr0 & X86_CR0_PG) ||
1455 !(save->cr0 & X86_CR0_PE) ||
1456 (save->rflags & X86_EFLAGS_VM) ||
Emanuele Giuseppe Espositob7a3d8b2021-11-03 10:05:24 -04001457 !__nested_vmcb_check_save(vcpu, &save_cached))
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001458 goto out_free;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001459
1460 /*
Maxim Levitskyb222b0b2021-06-07 12:01:59 +03001461 * While the nested guest CR3 is already checked and set by
1462 * KVM_SET_SREGS, it was set when nested state was yet loaded,
1463 * thus MMU might not be initialized correctly.
1464 * Set it again to fix this.
1465 */
1466
1467 ret = nested_svm_load_cr3(&svm->vcpu, vcpu->arch.cr3,
1468 nested_npt_enabled(svm), false);
1469 if (WARN_ON_ONCE(ret))
1470 goto out_free;
1471
1472
1473 /*
Cathy Avery4995a362021-01-13 07:07:52 -05001474 * All checks done, we can enter guest mode. Userspace provides
1475 * vmcb12.control, which will be combined with L1 and stored into
1476 * vmcb02, and the L1 save state which we store in vmcb01.
1477 * L2 registers if needed are moved from the current VMCB to VMCB02.
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001478 */
Maxim Levitsky81f76ad2021-01-07 11:38:52 +02001479
Maxim Levitsky9d290e12021-05-03 15:54:44 +03001480 if (is_guest_mode(vcpu))
Sean Christophersonf7e57072022-01-25 22:03:58 +00001481 svm_leave_nested(vcpu);
Maxim Levitsky9d290e12021-05-03 15:54:44 +03001482 else
1483 svm->nested.vmcb02.ptr->save = svm->vmcb01.ptr->save;
1484
Maxim Levitsky063ab162021-05-04 17:39:35 +03001485 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1486
Maxim Levitsky81f76ad2021-01-07 11:38:52 +02001487 svm->nested.nested_run_pending =
1488 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
1489
Maxim Levitsky0dd16b52020-08-27 20:11:39 +03001490 svm->nested.vmcb12_gpa = kvm_state->hdr.svm.vmcb_pa;
Paolo Bonzinic08f3902020-11-17 02:51:35 -05001491
Vitaly Kuznetsov2bb16be2021-07-19 11:03:22 +02001492 svm_copy_vmrun_state(&svm->vmcb01.ptr->save, save);
Emanuele Giuseppe Esposito79071602021-11-03 10:05:23 -04001493 nested_copy_vmcb_control_to_cache(svm, ctl);
Cathy Avery4995a362021-01-13 07:07:52 -05001494
1495 svm_switch_vmcb(svm, &svm->nested.vmcb02);
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05001496 nested_vmcb02_prepare_control(svm);
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -04001497 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001498 ret = 0;
1499out_free:
1500 kfree(save);
1501 kfree(ctl);
1502
1503 return ret;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001504}
1505
Maxim Levitsky232f75d2021-04-01 17:18:10 +03001506static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)
1507{
1508 struct vcpu_svm *svm = to_svm(vcpu);
1509
1510 if (WARN_ON(!is_guest_mode(vcpu)))
1511 return true;
1512
Maxim Levitsky158a48e2021-06-07 12:02:03 +03001513 if (!vcpu->arch.pdptrs_from_userspace &&
1514 !nested_npt_enabled(svm) && is_pae_paging(vcpu))
Maxim Levitskyb222b0b2021-06-07 12:01:59 +03001515 /*
1516 * Reload the guest's PDPTRs since after a migration
1517 * the guest CR3 might be restored prior to setting the nested
1518 * state which can lead to a load of wrong PDPTRs.
1519 */
Lai Jiangshan2df4a5e2021-11-24 20:20:52 +08001520 if (CC(!load_pdptrs(vcpu, vcpu->arch.cr3)))
Maxim Levitskyb222b0b2021-06-07 12:01:59 +03001521 return false;
Maxim Levitsky232f75d2021-04-01 17:18:10 +03001522
1523 if (!nested_svm_vmrun_msrpm(svm)) {
1524 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1525 vcpu->run->internal.suberror =
1526 KVM_INTERNAL_ERROR_EMULATION;
1527 vcpu->run->internal.ndata = 0;
1528 return false;
1529 }
1530
1531 return true;
1532}
1533
Paolo Bonzini33b22172020-04-17 10:24:18 -04001534struct kvm_x86_nested_ops svm_nested_ops = {
Sean Christophersonf7e57072022-01-25 22:03:58 +00001535 .leave_nested = svm_leave_nested,
Paolo Bonzini33b22172020-04-17 10:24:18 -04001536 .check_events = svm_check_nested_events,
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08001537 .triple_fault = nested_svm_triple_fault,
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -04001538 .get_nested_state_pages = svm_get_nested_state_pages,
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001539 .get_state = svm_get_nested_state,
1540 .set_state = svm_set_nested_state,
Paolo Bonzini33b22172020-04-17 10:24:18 -04001541};