blob: 32400cba608d4298868d516effdb9fd0c0a6e3c3 [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
61 if (vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_EXCEPTION_OFFSET + PF_VECTOR) &&
62 !svm->nested.nested_run_pending) {
63 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + PF_VECTOR;
64 svm->vmcb->control.exit_code_hi = 0;
65 svm->vmcb->control.exit_info_1 = fault->error_code;
66 svm->vmcb->control.exit_info_2 = fault->address;
67 nested_svm_vmexit(svm);
68 } else {
69 kvm_inject_page_fault(vcpu, fault);
70 }
71}
72
Joerg Roedel883b0a92020-03-24 10:41:52 +010073static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
74{
75 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzinie670bf62020-05-13 13:16:12 -040076 u64 cr3 = svm->nested.ctl.nested_cr3;
Joerg Roedel883b0a92020-03-24 10:41:52 +010077 u64 pdpte;
78 int ret;
79
Sean Christopherson2732be92021-02-03 16:01:07 -080080 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte,
Joerg Roedel883b0a92020-03-24 10:41:52 +010081 offset_in_page(cr3) + index * 8, 8);
82 if (ret)
83 return 0;
84 return pdpte;
85}
86
87static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
88{
89 struct vcpu_svm *svm = to_svm(vcpu);
90
Paolo Bonzinie670bf62020-05-13 13:16:12 -040091 return svm->nested.ctl.nested_cr3;
Joerg Roedel883b0a92020-03-24 10:41:52 +010092}
93
94static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
95{
Paolo Bonzini929d1cf2020-05-19 06:18:31 -040096 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzini929d1cf2020-05-19 06:18:31 -040097
Joerg Roedel883b0a92020-03-24 10:41:52 +010098 WARN_ON(mmu_is_nested(vcpu));
99
100 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
Cathy Avery4995a362021-01-13 07:07:52 -0500101 kvm_init_shadow_npt_mmu(vcpu, X86_CR0_PG, svm->vmcb01.ptr->save.cr4,
102 svm->vmcb01.ptr->save.efer,
Vitaly Kuznetsov0f04a2a2020-07-10 16:11:49 +0200103 svm->nested.ctl.nested_cr3);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100104 vcpu->arch.mmu->get_guest_pgd = nested_svm_get_tdp_cr3;
105 vcpu->arch.mmu->get_pdptr = nested_svm_get_tdp_pdptr;
106 vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100107 reset_shadow_zero_bits_mask(vcpu, vcpu->arch.mmu);
108 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
109}
110
111static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
112{
113 vcpu->arch.mmu = &vcpu->arch.root_mmu;
114 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
115}
116
117void recalc_intercepts(struct vcpu_svm *svm)
118{
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400119 struct vmcb_control_area *c, *h, *g;
Babu Mogerc45ad722020-09-11 14:27:58 -0500120 unsigned int i;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100121
Joerg Roedel06e78522020-06-25 10:03:23 +0200122 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100123
124 if (!is_guest_mode(&svm->vcpu))
125 return;
126
127 c = &svm->vmcb->control;
Cathy Avery4995a362021-01-13 07:07:52 -0500128 h = &svm->vmcb01.ptr->control;
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400129 g = &svm->nested.ctl;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100130
Babu Mogerc45ad722020-09-11 14:27:58 -0500131 for (i = 0; i < MAX_INTERCEPT; i++)
132 c->intercepts[i] = h->intercepts[i];
133
Paolo Bonzinie9fd7612020-05-13 13:28:23 -0400134 if (g->int_ctl & V_INTR_MASKING_MASK) {
Joerg Roedel883b0a92020-03-24 10:41:52 +0100135 /* We only want the cr8 intercept bits of L1 */
Babu Moger03bfeeb2020-09-11 14:28:05 -0500136 vmcb_clr_intercept(c, INTERCEPT_CR8_READ);
137 vmcb_clr_intercept(c, INTERCEPT_CR8_WRITE);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100138
139 /*
140 * Once running L2 with HF_VINTR_MASK, EFLAGS.IF does not
141 * affect any interrupt we may want to inject; therefore,
142 * interrupt window vmexits are irrelevant to L0.
143 */
Babu Mogerc62e2e92020-09-11 14:28:28 -0500144 vmcb_clr_intercept(c, INTERCEPT_VINTR);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100145 }
146
147 /* We don't want to see VMMCALLs from a nested guest */
Babu Mogerc62e2e92020-09-11 14:28:28 -0500148 vmcb_clr_intercept(c, INTERCEPT_VMMCALL);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100149
Babu Mogerc45ad722020-09-11 14:27:58 -0500150 for (i = 0; i < MAX_INTERCEPT; i++)
151 c->intercepts[i] |= g->intercepts[i];
Joerg Roedel883b0a92020-03-24 10:41:52 +0100152}
153
Paolo Bonzini2f675912020-05-18 15:21:22 -0400154static void copy_vmcb_control_area(struct vmcb_control_area *dst,
155 struct vmcb_control_area *from)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100156{
Babu Mogerc45ad722020-09-11 14:27:58 -0500157 unsigned int i;
158
159 for (i = 0; i < MAX_INTERCEPT; i++)
160 dst->intercepts[i] = from->intercepts[i];
161
Joerg Roedel883b0a92020-03-24 10:41:52 +0100162 dst->iopm_base_pa = from->iopm_base_pa;
163 dst->msrpm_base_pa = from->msrpm_base_pa;
164 dst->tsc_offset = from->tsc_offset;
Paolo Bonzini6c0238c2020-05-20 08:02:17 -0400165 /* asid not copied, it is handled manually for svm->vmcb. */
Joerg Roedel883b0a92020-03-24 10:41:52 +0100166 dst->tlb_ctl = from->tlb_ctl;
167 dst->int_ctl = from->int_ctl;
168 dst->int_vector = from->int_vector;
169 dst->int_state = from->int_state;
170 dst->exit_code = from->exit_code;
171 dst->exit_code_hi = from->exit_code_hi;
172 dst->exit_info_1 = from->exit_info_1;
173 dst->exit_info_2 = from->exit_info_2;
174 dst->exit_int_info = from->exit_int_info;
175 dst->exit_int_info_err = from->exit_int_info_err;
176 dst->nested_ctl = from->nested_ctl;
177 dst->event_inj = from->event_inj;
178 dst->event_inj_err = from->event_inj_err;
179 dst->nested_cr3 = from->nested_cr3;
180 dst->virt_ext = from->virt_ext;
181 dst->pause_filter_count = from->pause_filter_count;
182 dst->pause_filter_thresh = from->pause_filter_thresh;
183}
184
185static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
186{
187 /*
188 * This function merges the msr permission bitmaps of kvm and the
189 * nested vmcb. It is optimized in that it only merges the parts where
190 * the kvm msr permission bitmap may contain zero bits
191 */
192 int i;
193
Babu Mogerc62e2e92020-09-11 14:28:28 -0500194 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100195 return true;
196
197 for (i = 0; i < MSRPM_OFFSETS; i++) {
198 u32 value, p;
199 u64 offset;
200
201 if (msrpm_offsets[i] == 0xffffffff)
202 break;
203
204 p = msrpm_offsets[i];
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400205 offset = svm->nested.ctl.msrpm_base_pa + (p * 4);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100206
207 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
208 return false;
209
210 svm->nested.msrpm[p] = svm->msrpm[p] | value;
211 }
212
213 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
214
215 return true;
216}
217
Krish Sadhukhanee695f22021-04-12 17:56:08 -0400218/*
219 * Bits 11:0 of bitmap address are ignored by hardware
220 */
221static bool nested_svm_check_bitmap_pa(struct kvm_vcpu *vcpu, u64 pa, u32 size)
222{
223 u64 addr = PAGE_ALIGN(pa);
224
225 return kvm_vcpu_is_legal_gpa(vcpu, addr) &&
226 kvm_vcpu_is_legal_gpa(vcpu, addr + size - 1);
227}
228
229static bool nested_vmcb_check_controls(struct kvm_vcpu *vcpu,
230 struct vmcb_control_area *control)
Paolo Bonzinica46d732020-05-18 13:02:15 -0400231{
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800232 if (CC(!vmcb_is_intercept(control, INTERCEPT_VMRUN)))
Paolo Bonzinica46d732020-05-18 13:02:15 -0400233 return false;
234
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800235 if (CC(control->asid == 0))
Paolo Bonzinica46d732020-05-18 13:02:15 -0400236 return false;
237
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800238 if (CC((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) && !npt_enabled))
Paolo Bonzinica46d732020-05-18 13:02:15 -0400239 return false;
240
Krish Sadhukhanee695f22021-04-12 17:56:08 -0400241 if (CC(!nested_svm_check_bitmap_pa(vcpu, control->msrpm_base_pa,
242 MSRPM_SIZE)))
243 return false;
244 if (CC(!nested_svm_check_bitmap_pa(vcpu, control->iopm_base_pa,
245 IOPM_SIZE)))
246 return false;
247
Paolo Bonzinica46d732020-05-18 13:02:15 -0400248 return true;
249}
250
Paolo Bonzini63129752021-03-02 14:40:39 -0500251static bool nested_vmcb_check_cr3_cr4(struct kvm_vcpu *vcpu,
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000252 struct vmcb_save_area *save)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100253{
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000254 /*
255 * These checks are also performed by KVM_SET_SREGS,
256 * except that EFER.LMA is not checked by SVM against
257 * CR0.PG && EFER.LME.
258 */
259 if ((save->efer & EFER_LME) && (save->cr0 & X86_CR0_PG)) {
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800260 if (CC(!(save->cr4 & X86_CR4_PAE)) ||
261 CC(!(save->cr0 & X86_CR0_PE)) ||
262 CC(kvm_vcpu_is_illegal_gpa(vcpu, save->cr3)))
Krish Sadhukhan761e4162020-07-08 00:39:56 +0000263 return false;
264 }
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000265
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800266 if (CC(!kvm_is_valid_cr4(vcpu, save->cr4)))
267 return false;
268
269 return true;
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000270}
271
272/* Common checks that apply to both L1 and L2 state. */
Paolo Bonzini63129752021-03-02 14:40:39 -0500273static bool nested_vmcb_valid_sregs(struct kvm_vcpu *vcpu,
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000274 struct vmcb_save_area *save)
275{
Paolo Bonzini3c346c02021-03-31 06:28:01 -0400276 /*
277 * FIXME: these should be done after copying the fields,
278 * to avoid TOC/TOU races. For these save area checks
279 * the possible damage is limited since kvm_set_cr0 and
280 * kvm_set_cr4 handle failure; EFER_SVME is an exception
281 * so it is force-set later in nested_prepare_vmcb_save.
282 */
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800283 if (CC(!(save->efer & EFER_SVME)))
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000284 return false;
285
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800286 if (CC((save->cr0 & X86_CR0_CD) == 0 && (save->cr0 & X86_CR0_NW)) ||
287 CC(save->cr0 & ~0xffffffffULL))
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000288 return false;
289
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800290 if (CC(!kvm_dr6_valid(save->dr6)) || CC(!kvm_dr7_valid(save->dr7)))
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000291 return false;
292
Paolo Bonzini63129752021-03-02 14:40:39 -0500293 if (!nested_vmcb_check_cr3_cr4(vcpu, save))
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000294 return false;
295
Paolo Bonzini63129752021-03-02 14:40:39 -0500296 if (CC(!kvm_valid_efer(vcpu, save->efer)))
Krish Sadhukhan6906e062020-10-06 19:06:52 +0000297 return false;
298
299 return true;
300}
301
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500302static void nested_load_control_from_vmcb12(struct vcpu_svm *svm,
303 struct vmcb_control_area *control)
Paolo Bonzini3e06f012020-05-13 13:07:26 -0400304{
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400305 copy_vmcb_control_area(&svm->nested.ctl, control);
Paolo Bonzini3e06f012020-05-13 13:07:26 -0400306
Paolo Bonzinicc440cd2020-05-13 13:36:32 -0400307 /* Copy it here because nested_svm_check_controls will check it. */
308 svm->nested.ctl.asid = control->asid;
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400309 svm->nested.ctl.msrpm_base_pa &= ~0x0fffULL;
310 svm->nested.ctl.iopm_base_pa &= ~0x0fffULL;
Paolo Bonzini3e06f012020-05-13 13:07:26 -0400311}
312
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400313/*
314 * Synchronize fields that are written by the processor, so that
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500315 * they can be copied back into the vmcb12.
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400316 */
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500317void nested_sync_control_from_vmcb02(struct vcpu_svm *svm)
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400318{
319 u32 mask;
320 svm->nested.ctl.event_inj = svm->vmcb->control.event_inj;
321 svm->nested.ctl.event_inj_err = svm->vmcb->control.event_inj_err;
322
323 /* Only a few fields of int_ctl are written by the processor. */
324 mask = V_IRQ_MASK | V_TPR_MASK;
325 if (!(svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) &&
Joerg Roedela284ba52020-06-25 10:03:24 +0200326 svm_is_intercept(svm, INTERCEPT_VINTR)) {
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400327 /*
328 * In order to request an interrupt window, L0 is usurping
329 * svm->vmcb->control.int_ctl and possibly setting V_IRQ
330 * even if it was clear in L1's VMCB. Restoring it would be
331 * wrong. However, in this case V_IRQ will remain true until
332 * interrupt_window_interception calls svm_clear_vintr and
333 * restores int_ctl. We can just leave it aside.
334 */
335 mask &= ~V_IRQ_MASK;
336 }
337 svm->nested.ctl.int_ctl &= ~mask;
338 svm->nested.ctl.int_ctl |= svm->vmcb->control.int_ctl & mask;
339}
340
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400341/*
342 * Transfer any event that L0 or L1 wanted to inject into L2 to
343 * EXIT_INT_INFO.
344 */
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500345static void nested_save_pending_event_to_vmcb12(struct vcpu_svm *svm,
346 struct vmcb *vmcb12)
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400347{
348 struct kvm_vcpu *vcpu = &svm->vcpu;
349 u32 exit_int_info = 0;
350 unsigned int nr;
351
352 if (vcpu->arch.exception.injected) {
353 nr = vcpu->arch.exception.nr;
354 exit_int_info = nr | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT;
355
356 if (vcpu->arch.exception.has_error_code) {
357 exit_int_info |= SVM_EVTINJ_VALID_ERR;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300358 vmcb12->control.exit_int_info_err =
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400359 vcpu->arch.exception.error_code;
360 }
361
362 } else if (vcpu->arch.nmi_injected) {
363 exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
364
365 } else if (vcpu->arch.interrupt.injected) {
366 nr = vcpu->arch.interrupt.nr;
367 exit_int_info = nr | SVM_EVTINJ_VALID;
368
369 if (vcpu->arch.interrupt.soft)
370 exit_int_info |= SVM_EVTINJ_TYPE_SOFT;
371 else
372 exit_int_info |= SVM_EVTINJ_TYPE_INTR;
373 }
374
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300375 vmcb12->control.exit_int_info = exit_int_info;
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400376}
377
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200378static inline bool nested_npt_enabled(struct vcpu_svm *svm)
379{
380 return svm->nested.ctl.nested_ctl & SVM_NESTED_CTL_NP_ENABLE;
381}
382
383/*
Vitaly Kuznetsovd82aaef2020-07-10 16:11:56 +0200384 * Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true
385 * if we are emulating VM-Entry into a guest with NPT enabled.
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200386 */
387static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
388 bool nested_npt)
389{
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800390 if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3)))
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200391 return -EINVAL;
392
393 if (!nested_npt && is_pae_paging(vcpu) &&
394 (cr3 != kvm_read_cr3(vcpu) || pdptrs_changed(vcpu))) {
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800395 if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)))
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200396 return -EINVAL;
397 }
398
399 /*
400 * TODO: optimize unconditional TLB flush/MMU sync here and in
401 * kvm_init_shadow_npt_mmu().
402 */
403 if (!nested_npt)
404 kvm_mmu_new_pgd(vcpu, cr3, false, false);
405
406 vcpu->arch.cr3 = cr3;
407 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
408
409 kvm_init_mmu(vcpu, false);
410
411 return 0;
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200412}
413
Cathy Avery4995a362021-01-13 07:07:52 -0500414void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm)
415{
416 if (!svm->nested.vmcb02.ptr)
417 return;
418
419 /* FIXME: merge g_pat from vmcb01 and vmcb12. */
420 svm->nested.vmcb02.ptr->save.g_pat = svm->vmcb01.ptr->save.g_pat;
421}
422
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500423static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100424{
Cathy Avery81733962021-03-01 15:08:44 -0500425 bool new_vmcb12 = false;
426
Cathy Avery4995a362021-01-13 07:07:52 -0500427 nested_vmcb02_compute_g_pat(svm);
428
Joerg Roedel883b0a92020-03-24 10:41:52 +0100429 /* Load the nested guest state */
Cathy Avery81733962021-03-01 15:08:44 -0500430 if (svm->nested.vmcb12_gpa != svm->nested.last_vmcb12_gpa) {
431 new_vmcb12 = true;
432 svm->nested.last_vmcb12_gpa = svm->nested.vmcb12_gpa;
433 }
434
435 if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_SEG))) {
436 svm->vmcb->save.es = vmcb12->save.es;
437 svm->vmcb->save.cs = vmcb12->save.cs;
438 svm->vmcb->save.ss = vmcb12->save.ss;
439 svm->vmcb->save.ds = vmcb12->save.ds;
440 svm->vmcb->save.cpl = vmcb12->save.cpl;
441 vmcb_mark_dirty(svm->vmcb, VMCB_SEG);
442 }
443
444 if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DT))) {
445 svm->vmcb->save.gdtr = vmcb12->save.gdtr;
446 svm->vmcb->save.idtr = vmcb12->save.idtr;
447 vmcb_mark_dirty(svm->vmcb, VMCB_DT);
448 }
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500449
Paolo Bonzini8cce12b2020-11-27 12:46:36 -0500450 kvm_set_rflags(&svm->vcpu, vmcb12->save.rflags | X86_EFLAGS_FIXED);
Paolo Bonzini3c346c02021-03-31 06:28:01 -0400451
452 /*
453 * Force-set EFER_SVME even though it is checked earlier on the
454 * VMCB12, because the guest can flip the bit between the check
455 * and now. Clearing EFER_SVME would call svm_free_nested.
456 */
457 svm_set_efer(&svm->vcpu, vmcb12->save.efer | EFER_SVME);
458
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300459 svm_set_cr0(&svm->vcpu, vmcb12->save.cr0);
460 svm_set_cr4(&svm->vcpu, vmcb12->save.cr4);
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500461
462 svm->vcpu.arch.cr2 = vmcb12->save.cr2;
Cathy Avery81733962021-03-01 15:08:44 -0500463
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300464 kvm_rax_write(&svm->vcpu, vmcb12->save.rax);
465 kvm_rsp_write(&svm->vcpu, vmcb12->save.rsp);
466 kvm_rip_write(&svm->vcpu, vmcb12->save.rip);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100467
468 /* In case we don't even reach vcpu_run, the fields are not updated */
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300469 svm->vmcb->save.rax = vmcb12->save.rax;
470 svm->vmcb->save.rsp = vmcb12->save.rsp;
471 svm->vmcb->save.rip = vmcb12->save.rip;
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500472
Cathy Avery81733962021-03-01 15:08:44 -0500473 /* These bits will be set properly on the first execution when new_vmc12 is true */
474 if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DR))) {
475 svm->vmcb->save.dr7 = vmcb12->save.dr7 | DR7_FIXED_1;
476 svm->vcpu.arch.dr6 = vmcb12->save.dr6 | DR6_ACTIVE_LOW;
477 vmcb_mark_dirty(svm->vmcb, VMCB_DR);
478 }
Paolo Bonzinif241d712020-05-18 10:56:43 -0400479}
Joerg Roedel883b0a92020-03-24 10:41:52 +0100480
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500481static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
Paolo Bonzinif241d712020-05-18 10:56:43 -0400482{
Paolo Bonzini91b71302020-05-22 12:28:52 -0400483 const u32 mask = V_INTR_MASKING_MASK | V_GIF_ENABLE_MASK | V_GIF_MASK;
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200484
Paolo Bonzini7c3ecfc2020-11-16 06:13:15 -0500485 /*
486 * Filled at exit: exit_code, exit_code_hi, exit_info_1, exit_info_2,
487 * exit_int_info, exit_int_info_err, next_rip, insn_len, insn_bytes.
488 */
Cathy Avery4995a362021-01-13 07:07:52 -0500489
Paolo Bonzini7c3ecfc2020-11-16 06:13:15 -0500490 /*
491 * Also covers avic_vapic_bar, avic_backing_page, avic_logical_id,
492 * avic_physical_id.
493 */
494 WARN_ON(svm->vmcb01.ptr->control.int_ctl & AVIC_ENABLE_MASK);
495
496 /* Copied from vmcb01. msrpm_base can be overwritten later. */
497 svm->vmcb->control.nested_ctl = svm->vmcb01.ptr->control.nested_ctl;
498 svm->vmcb->control.iopm_base_pa = svm->vmcb01.ptr->control.iopm_base_pa;
499 svm->vmcb->control.msrpm_base_pa = svm->vmcb01.ptr->control.msrpm_base_pa;
500
501 /* Done at vmrun: asid. */
502
503 /* Also overwritten later if necessary. */
504 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
505
506 /* nested_cr3. */
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200507 if (nested_npt_enabled(svm))
Paolo Bonzini69cb8772020-05-22 05:27:46 -0400508 nested_svm_init_mmu_context(&svm->vcpu);
509
Paolo Bonzini18fc6c52020-05-18 11:07:08 -0400510 svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset =
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400511 svm->vcpu.arch.l1_tsc_offset + svm->nested.ctl.tsc_offset;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100512
Paolo Bonzini91b71302020-05-22 12:28:52 -0400513 svm->vmcb->control.int_ctl =
514 (svm->nested.ctl.int_ctl & ~mask) |
Cathy Avery4995a362021-01-13 07:07:52 -0500515 (svm->vmcb01.ptr->control.int_ctl & mask);
Paolo Bonzini91b71302020-05-22 12:28:52 -0400516
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400517 svm->vmcb->control.virt_ext = svm->nested.ctl.virt_ext;
518 svm->vmcb->control.int_vector = svm->nested.ctl.int_vector;
519 svm->vmcb->control.int_state = svm->nested.ctl.int_state;
520 svm->vmcb->control.event_inj = svm->nested.ctl.event_inj;
521 svm->vmcb->control.event_inj_err = svm->nested.ctl.event_inj_err;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100522
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400523 svm->vmcb->control.pause_filter_count = svm->nested.ctl.pause_filter_count;
524 svm->vmcb->control.pause_filter_thresh = svm->nested.ctl.pause_filter_thresh;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100525
Joerg Roedel883b0a92020-03-24 10:41:52 +0100526 /* Enter Guest-Mode */
527 enter_guest_mode(&svm->vcpu);
528
529 /*
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500530 * Merge guest and host intercepts - must be called with vcpu in
531 * guest-mode to take effect.
Joerg Roedel883b0a92020-03-24 10:41:52 +0100532 */
533 recalc_intercepts(svm);
Paolo Bonzinif241d712020-05-18 10:56:43 -0400534}
535
Babu Mogerd00b99c2021-02-17 10:56:04 -0500536static void nested_svm_copy_common_state(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
537{
538 /*
539 * Some VMCB state is shared between L1 and L2 and thus has to be
540 * moved at the time of nested vmrun and vmexit.
541 *
542 * VMLOAD/VMSAVE state would also belong in this category, but KVM
543 * always performs VMLOAD and VMSAVE from the VMCB01.
544 */
545 to_vmcb->save.spec_ctrl = from_vmcb->save.spec_ctrl;
546}
547
Paolo Bonzini63129752021-03-02 14:40:39 -0500548int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa,
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300549 struct vmcb *vmcb12)
Paolo Bonzinif241d712020-05-18 10:56:43 -0400550{
Paolo Bonzini63129752021-03-02 14:40:39 -0500551 struct vcpu_svm *svm = to_svm(vcpu);
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200552 int ret;
553
Maxim Levitsky954f4192021-02-17 16:57:13 +0200554 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb12_gpa,
555 vmcb12->save.rip,
556 vmcb12->control.int_ctl,
557 vmcb12->control.event_inj,
558 vmcb12->control.nested_ctl);
559
560 trace_kvm_nested_intercepts(vmcb12->control.intercepts[INTERCEPT_CR] & 0xffff,
561 vmcb12->control.intercepts[INTERCEPT_CR] >> 16,
562 vmcb12->control.intercepts[INTERCEPT_EXCEPTION],
563 vmcb12->control.intercepts[INTERCEPT_WORD3],
564 vmcb12->control.intercepts[INTERCEPT_WORD4],
565 vmcb12->control.intercepts[INTERCEPT_WORD5]);
566
567
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300568 svm->nested.vmcb12_gpa = vmcb12_gpa;
Cathy Avery4995a362021-01-13 07:07:52 -0500569
570 WARN_ON(svm->vmcb == svm->nested.vmcb02.ptr);
571
Babu Mogerd00b99c2021-02-17 10:56:04 -0500572 nested_svm_copy_common_state(svm->vmcb01.ptr, svm->nested.vmcb02.ptr);
Cathy Avery4995a362021-01-13 07:07:52 -0500573
574 svm_switch_vmcb(svm, &svm->nested.vmcb02);
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500575 nested_vmcb02_prepare_control(svm);
576 nested_vmcb02_prepare_save(svm, vmcb12);
Paolo Bonzinif241d712020-05-18 10:56:43 -0400577
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300578 ret = nested_svm_load_cr3(&svm->vcpu, vmcb12->save.cr3,
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200579 nested_npt_enabled(svm));
580 if (ret)
581 return ret;
582
Paolo Bonzinia04aead2021-02-18 07:16:59 -0500583 if (!npt_enabled)
Paolo Bonzini63129752021-03-02 14:40:39 -0500584 vcpu->arch.mmu->inject_page_fault = svm_inject_page_fault_nested;
Paolo Bonzinia04aead2021-02-18 07:16:59 -0500585
Paolo Bonziniffdf7f92020-05-22 12:18:27 -0400586 svm_set_gif(svm, true);
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200587
588 return 0;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100589}
590
Paolo Bonzini63129752021-03-02 14:40:39 -0500591int nested_svm_vmrun(struct kvm_vcpu *vcpu)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100592{
Paolo Bonzini63129752021-03-02 14:40:39 -0500593 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100594 int ret;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300595 struct vmcb *vmcb12;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100596 struct kvm_host_map map;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300597 u64 vmcb12_gpa;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100598
Paolo Bonzini63129752021-03-02 14:40:39 -0500599 ++vcpu->stat.nested_run;
Dongli Zhang43c11d92021-03-05 14:57:47 -0800600
Paolo Bonzini63129752021-03-02 14:40:39 -0500601 if (is_smm(vcpu)) {
602 kvm_queue_exception(vcpu, UD_VECTOR);
Paolo Bonzini7c67f5462020-04-23 10:52:48 -0400603 return 1;
604 }
Joerg Roedel883b0a92020-03-24 10:41:52 +0100605
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300606 vmcb12_gpa = svm->vmcb->save.rax;
Paolo Bonzini63129752021-03-02 14:40:39 -0500607 ret = kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100608 if (ret == -EINVAL) {
Paolo Bonzini63129752021-03-02 14:40:39 -0500609 kvm_inject_gp(vcpu, 0);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100610 return 1;
611 } else if (ret) {
Paolo Bonzini63129752021-03-02 14:40:39 -0500612 return kvm_skip_emulated_instruction(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100613 }
614
Paolo Bonzini63129752021-03-02 14:40:39 -0500615 ret = kvm_skip_emulated_instruction(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100616
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300617 vmcb12 = map.hva;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100618
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300619 if (WARN_ON_ONCE(!svm->nested.initialized))
620 return -EINVAL;
621
Paolo Bonzinicb9b6a12021-03-31 07:35:52 -0400622 nested_load_control_from_vmcb12(svm, &vmcb12->control);
623
624 if (!nested_vmcb_valid_sregs(vcpu, &vmcb12->save) ||
Krish Sadhukhanee695f22021-04-12 17:56:08 -0400625 !nested_vmcb_check_controls(vcpu, &svm->nested.ctl)) {
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300626 vmcb12->control.exit_code = SVM_EXIT_ERR;
627 vmcb12->control.exit_code_hi = 0;
628 vmcb12->control.exit_info_1 = 0;
629 vmcb12->control.exit_info_2 = 0;
Paolo Bonzini69c9dfa2020-05-13 12:57:26 -0400630 goto out;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100631 }
632
Joerg Roedel883b0a92020-03-24 10:41:52 +0100633
634 /* Clear internal status */
Paolo Bonzini63129752021-03-02 14:40:39 -0500635 kvm_clear_exception_queue(vcpu);
636 kvm_clear_interrupt_queue(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100637
638 /*
Cathy Avery4995a362021-01-13 07:07:52 -0500639 * Since vmcb01 is not in use, we can use it to store some of the L1
640 * state.
Joerg Roedel883b0a92020-03-24 10:41:52 +0100641 */
Paolo Bonzini63129752021-03-02 14:40:39 -0500642 svm->vmcb01.ptr->save.efer = vcpu->arch.efer;
643 svm->vmcb01.ptr->save.cr0 = kvm_read_cr0(vcpu);
644 svm->vmcb01.ptr->save.cr4 = vcpu->arch.cr4;
645 svm->vmcb01.ptr->save.rflags = kvm_get_rflags(vcpu);
646 svm->vmcb01.ptr->save.rip = kvm_rip_read(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100647
Cathy Avery4995a362021-01-13 07:07:52 -0500648 if (!npt_enabled)
Paolo Bonzini63129752021-03-02 14:40:39 -0500649 svm->vmcb01.ptr->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100650
Paolo Bonzinif74f9412020-04-23 13:22:27 -0400651 svm->nested.nested_run_pending = 1;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100652
Paolo Bonzini63129752021-03-02 14:40:39 -0500653 if (enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12))
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200654 goto out_exit_err;
Vitaly Kuznetsovebdb3db2020-07-10 16:11:51 +0200655
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200656 if (nested_svm_vmrun_msrpm(svm))
657 goto out;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100658
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200659out_exit_err:
660 svm->nested.nested_run_pending = 0;
661
662 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
663 svm->vmcb->control.exit_code_hi = 0;
664 svm->vmcb->control.exit_info_1 = 0;
665 svm->vmcb->control.exit_info_2 = 0;
666
667 nested_svm_vmexit(svm);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100668
Paolo Bonzini69c9dfa2020-05-13 12:57:26 -0400669out:
Paolo Bonzini63129752021-03-02 14:40:39 -0500670 kvm_vcpu_unmap(vcpu, &map, true);
Paolo Bonzini69c9dfa2020-05-13 12:57:26 -0400671
Joerg Roedel883b0a92020-03-24 10:41:52 +0100672 return ret;
673}
674
675void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
676{
677 to_vmcb->save.fs = from_vmcb->save.fs;
678 to_vmcb->save.gs = from_vmcb->save.gs;
679 to_vmcb->save.tr = from_vmcb->save.tr;
680 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
681 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
682 to_vmcb->save.star = from_vmcb->save.star;
683 to_vmcb->save.lstar = from_vmcb->save.lstar;
684 to_vmcb->save.cstar = from_vmcb->save.cstar;
685 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
686 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
687 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
688 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
689}
690
691int nested_svm_vmexit(struct vcpu_svm *svm)
692{
Paolo Bonzini63129752021-03-02 14:40:39 -0500693 struct kvm_vcpu *vcpu = &svm->vcpu;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300694 struct vmcb *vmcb12;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100695 struct vmcb *vmcb = svm->vmcb;
696 struct kvm_host_map map;
Paolo Bonzini63129752021-03-02 14:40:39 -0500697 int rc;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100698
Sean Christophersoncb6a32c2021-03-02 09:45:14 -0800699 /* Triple faults in L2 should never escape. */
700 WARN_ON_ONCE(kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu));
701
Paolo Bonzini63129752021-03-02 14:40:39 -0500702 rc = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.vmcb12_gpa), &map);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100703 if (rc) {
704 if (rc == -EINVAL)
Paolo Bonzini63129752021-03-02 14:40:39 -0500705 kvm_inject_gp(vcpu, 0);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100706 return 1;
707 }
708
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300709 vmcb12 = map.hva;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100710
711 /* Exit Guest-Mode */
Paolo Bonzini63129752021-03-02 14:40:39 -0500712 leave_guest_mode(vcpu);
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300713 svm->nested.vmcb12_gpa = 0;
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400714 WARN_ON_ONCE(svm->nested.nested_run_pending);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100715
Paolo Bonzini63129752021-03-02 14:40:39 -0500716 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Maxim Levitskyf2c7ef32021-01-07 11:38:51 +0200717
Paolo Bonzini38c0b192020-04-23 13:13:09 -0400718 /* in case we halted in L2 */
719 svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE;
720
Joerg Roedel883b0a92020-03-24 10:41:52 +0100721 /* Give the current vmcb to the guest */
Joerg Roedel883b0a92020-03-24 10:41:52 +0100722
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300723 vmcb12->save.es = vmcb->save.es;
724 vmcb12->save.cs = vmcb->save.cs;
725 vmcb12->save.ss = vmcb->save.ss;
726 vmcb12->save.ds = vmcb->save.ds;
727 vmcb12->save.gdtr = vmcb->save.gdtr;
728 vmcb12->save.idtr = vmcb->save.idtr;
729 vmcb12->save.efer = svm->vcpu.arch.efer;
Paolo Bonzini63129752021-03-02 14:40:39 -0500730 vmcb12->save.cr0 = kvm_read_cr0(vcpu);
731 vmcb12->save.cr3 = kvm_read_cr3(vcpu);
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300732 vmcb12->save.cr2 = vmcb->save.cr2;
733 vmcb12->save.cr4 = svm->vcpu.arch.cr4;
Paolo Bonzini63129752021-03-02 14:40:39 -0500734 vmcb12->save.rflags = kvm_get_rflags(vcpu);
735 vmcb12->save.rip = kvm_rip_read(vcpu);
736 vmcb12->save.rsp = kvm_rsp_read(vcpu);
737 vmcb12->save.rax = kvm_rax_read(vcpu);
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300738 vmcb12->save.dr7 = vmcb->save.dr7;
739 vmcb12->save.dr6 = svm->vcpu.arch.dr6;
740 vmcb12->save.cpl = vmcb->save.cpl;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100741
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300742 vmcb12->control.int_state = vmcb->control.int_state;
743 vmcb12->control.exit_code = vmcb->control.exit_code;
744 vmcb12->control.exit_code_hi = vmcb->control.exit_code_hi;
745 vmcb12->control.exit_info_1 = vmcb->control.exit_info_1;
746 vmcb12->control.exit_info_2 = vmcb->control.exit_info_2;
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400747
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300748 if (vmcb12->control.exit_code != SVM_EXIT_ERR)
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500749 nested_save_pending_event_to_vmcb12(svm, vmcb12);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100750
751 if (svm->nrips_enabled)
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300752 vmcb12->control.next_rip = vmcb->control.next_rip;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100753
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300754 vmcb12->control.int_ctl = svm->nested.ctl.int_ctl;
755 vmcb12->control.tlb_ctl = svm->nested.ctl.tlb_ctl;
756 vmcb12->control.event_inj = svm->nested.ctl.event_inj;
757 vmcb12->control.event_inj_err = svm->nested.ctl.event_inj_err;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100758
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300759 vmcb12->control.pause_filter_count =
Joerg Roedel883b0a92020-03-24 10:41:52 +0100760 svm->vmcb->control.pause_filter_count;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300761 vmcb12->control.pause_filter_thresh =
Joerg Roedel883b0a92020-03-24 10:41:52 +0100762 svm->vmcb->control.pause_filter_thresh;
763
Babu Mogerd00b99c2021-02-17 10:56:04 -0500764 nested_svm_copy_common_state(svm->nested.vmcb02.ptr, svm->vmcb01.ptr);
765
Cathy Avery4995a362021-01-13 07:07:52 -0500766 svm_switch_vmcb(svm, &svm->vmcb01);
Krish Sadhukhan9a7de6e2021-03-23 13:50:03 -0400767 WARN_ON_ONCE(svm->vmcb->control.exit_code != SVM_EXIT_VMRUN);
Cathy Avery4995a362021-01-13 07:07:52 -0500768
769 /*
770 * On vmexit the GIF is set to false and
771 * no event can be injected in L1.
772 */
Maxim Levitsky98837642020-08-27 19:27:18 +0300773 svm_set_gif(svm, false);
Cathy Avery4995a362021-01-13 07:07:52 -0500774 svm->vmcb->control.exit_int_info = 0;
Maxim Levitsky98837642020-08-27 19:27:18 +0300775
Paolo Bonzini7ca62d12020-11-16 06:38:19 -0500776 svm->vcpu.arch.tsc_offset = svm->vcpu.arch.l1_tsc_offset;
777 if (svm->vmcb->control.tsc_offset != svm->vcpu.arch.tsc_offset) {
778 svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset;
779 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
780 }
Paolo Bonzini18fc6c52020-05-18 11:07:08 -0400781
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400782 svm->nested.ctl.nested_cr3 = 0;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100783
Cathy Avery4995a362021-01-13 07:07:52 -0500784 /*
785 * Restore processor state that had been saved in vmcb01
786 */
Paolo Bonzini63129752021-03-02 14:40:39 -0500787 kvm_set_rflags(vcpu, svm->vmcb->save.rflags);
788 svm_set_efer(vcpu, svm->vmcb->save.efer);
789 svm_set_cr0(vcpu, svm->vmcb->save.cr0 | X86_CR0_PE);
790 svm_set_cr4(vcpu, svm->vmcb->save.cr4);
791 kvm_rax_write(vcpu, svm->vmcb->save.rax);
792 kvm_rsp_write(vcpu, svm->vmcb->save.rsp);
793 kvm_rip_write(vcpu, svm->vmcb->save.rip);
Cathy Avery4995a362021-01-13 07:07:52 -0500794
795 svm->vcpu.arch.dr7 = DR7_FIXED_1;
796 kvm_update_dr7(&svm->vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100797
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300798 trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code,
799 vmcb12->control.exit_info_1,
800 vmcb12->control.exit_info_2,
801 vmcb12->control.exit_int_info,
802 vmcb12->control.exit_int_info_err,
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400803 KVM_ISA_SVM);
804
Paolo Bonzini63129752021-03-02 14:40:39 -0500805 kvm_vcpu_unmap(vcpu, &map, true);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100806
Paolo Bonzini63129752021-03-02 14:40:39 -0500807 nested_svm_uninit_mmu_context(vcpu);
Vitaly Kuznetsovbf7dea42020-07-10 16:11:54 +0200808
Paolo Bonzini63129752021-03-02 14:40:39 -0500809 rc = nested_svm_load_cr3(vcpu, svm->vmcb->save.cr3, false);
Vitaly Kuznetsovd82aaef2020-07-10 16:11:56 +0200810 if (rc)
811 return 1;
Vitaly Kuznetsovbf7dea42020-07-10 16:11:54 +0200812
Joerg Roedel883b0a92020-03-24 10:41:52 +0100813 /*
814 * Drop what we picked up for L2 via svm_complete_interrupts() so it
815 * doesn't end up in L1.
816 */
817 svm->vcpu.arch.nmi_injected = false;
Paolo Bonzini63129752021-03-02 14:40:39 -0500818 kvm_clear_exception_queue(vcpu);
819 kvm_clear_interrupt_queue(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100820
Krish Sadhukhan9a7de6e2021-03-23 13:50:03 -0400821 /*
822 * If we are here following the completion of a VMRUN that
823 * is being single-stepped, queue the pending #DB intercept
824 * right now so that it an be accounted for before we execute
825 * L1's next instruction.
826 */
827 if (unlikely(svm->vmcb->save.rflags & X86_EFLAGS_TF))
828 kvm_queue_exception(&(svm->vcpu), DB_VECTOR);
829
Joerg Roedel883b0a92020-03-24 10:41:52 +0100830 return 0;
831}
832
Sean Christophersoncb6a32c2021-03-02 09:45:14 -0800833static void nested_svm_triple_fault(struct kvm_vcpu *vcpu)
834{
Sean Christopherson3a87c7e2021-03-02 09:45:15 -0800835 nested_svm_simple_vmexit(to_svm(vcpu), SVM_EXIT_SHUTDOWN);
Sean Christophersoncb6a32c2021-03-02 09:45:14 -0800836}
837
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300838int svm_allocate_nested(struct vcpu_svm *svm)
839{
Cathy Avery4995a362021-01-13 07:07:52 -0500840 struct page *vmcb02_page;
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300841
842 if (svm->nested.initialized)
843 return 0;
844
Cathy Avery4995a362021-01-13 07:07:52 -0500845 vmcb02_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
846 if (!vmcb02_page)
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300847 return -ENOMEM;
Cathy Avery4995a362021-01-13 07:07:52 -0500848 svm->nested.vmcb02.ptr = page_address(vmcb02_page);
849 svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300850
851 svm->nested.msrpm = svm_vcpu_alloc_msrpm();
852 if (!svm->nested.msrpm)
Cathy Avery4995a362021-01-13 07:07:52 -0500853 goto err_free_vmcb02;
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300854 svm_vcpu_init_msrpm(&svm->vcpu, svm->nested.msrpm);
855
856 svm->nested.initialized = true;
857 return 0;
858
Cathy Avery4995a362021-01-13 07:07:52 -0500859err_free_vmcb02:
860 __free_page(vmcb02_page);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300861 return -ENOMEM;
862}
863
864void svm_free_nested(struct vcpu_svm *svm)
865{
866 if (!svm->nested.initialized)
867 return;
868
869 svm_vcpu_free_msrpm(svm->nested.msrpm);
870 svm->nested.msrpm = NULL;
871
Cathy Avery4995a362021-01-13 07:07:52 -0500872 __free_page(virt_to_page(svm->nested.vmcb02.ptr));
873 svm->nested.vmcb02.ptr = NULL;
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300874
Maxim Levitskyc74ad082021-05-03 15:54:43 +0300875 /*
876 * When last_vmcb12_gpa matches the current vmcb12 gpa,
877 * some vmcb12 fields are not loaded if they are marked clean
878 * in the vmcb12, since in this case they are up to date already.
879 *
880 * When the vmcb02 is freed, this optimization becomes invalid.
881 */
882 svm->nested.last_vmcb12_gpa = INVALID_GPA;
883
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300884 svm->nested.initialized = false;
885}
886
Paolo Bonzinic513f482020-05-18 13:08:37 -0400887/*
888 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
889 */
890void svm_leave_nested(struct vcpu_svm *svm)
891{
Paolo Bonzini63129752021-03-02 14:40:39 -0500892 struct kvm_vcpu *vcpu = &svm->vcpu;
893
894 if (is_guest_mode(vcpu)) {
Paolo Bonzinic513f482020-05-18 13:08:37 -0400895 svm->nested.nested_run_pending = 0;
Maxim Levitskyc74ad082021-05-03 15:54:43 +0300896 svm->nested.vmcb12_gpa = INVALID_GPA;
897
Paolo Bonzini63129752021-03-02 14:40:39 -0500898 leave_guest_mode(vcpu);
Cathy Avery4995a362021-01-13 07:07:52 -0500899
Maxim Levitskydeee59b2021-05-03 15:54:42 +0300900 svm_switch_vmcb(svm, &svm->vmcb01);
Cathy Avery4995a362021-01-13 07:07:52 -0500901
Paolo Bonzini63129752021-03-02 14:40:39 -0500902 nested_svm_uninit_mmu_context(vcpu);
Maxim Levitsky56fe28d2021-01-07 11:38:54 +0200903 vmcb_mark_all_dirty(svm->vmcb);
Paolo Bonzinic513f482020-05-18 13:08:37 -0400904 }
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -0400905
Paolo Bonzini63129752021-03-02 14:40:39 -0500906 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Paolo Bonzinic513f482020-05-18 13:08:37 -0400907}
908
Joerg Roedel883b0a92020-03-24 10:41:52 +0100909static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
910{
911 u32 offset, msr, value;
912 int write, mask;
913
Babu Mogerc62e2e92020-09-11 14:28:28 -0500914 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100915 return NESTED_EXIT_HOST;
916
917 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
918 offset = svm_msrpm_offset(msr);
919 write = svm->vmcb->control.exit_info_1 & 1;
920 mask = 1 << ((2 * (msr & 0xf)) + write);
921
922 if (offset == MSR_INVALID)
923 return NESTED_EXIT_DONE;
924
925 /* Offset is in 32 bit units but need in 8 bit units */
926 offset *= 4;
927
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400928 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100929 return NESTED_EXIT_DONE;
930
931 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
932}
933
Joerg Roedel883b0a92020-03-24 10:41:52 +0100934static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
935{
936 unsigned port, size, iopm_len;
937 u16 val, mask;
938 u8 start_bit;
939 u64 gpa;
940
Babu Mogerc62e2e92020-09-11 14:28:28 -0500941 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_IOIO_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100942 return NESTED_EXIT_HOST;
943
944 port = svm->vmcb->control.exit_info_1 >> 16;
945 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
946 SVM_IOIO_SIZE_SHIFT;
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400947 gpa = svm->nested.ctl.iopm_base_pa + (port / 8);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100948 start_bit = port % 8;
949 iopm_len = (start_bit + size > 8) ? 2 : 1;
950 mask = (0xf >> (4 - size)) << start_bit;
951 val = 0;
952
953 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
954 return NESTED_EXIT_DONE;
955
956 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
957}
958
959static int nested_svm_intercept(struct vcpu_svm *svm)
960{
961 u32 exit_code = svm->vmcb->control.exit_code;
962 int vmexit = NESTED_EXIT_HOST;
963
964 switch (exit_code) {
965 case SVM_EXIT_MSR:
966 vmexit = nested_svm_exit_handled_msr(svm);
967 break;
968 case SVM_EXIT_IOIO:
969 vmexit = nested_svm_intercept_ioio(svm);
970 break;
971 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
Babu Moger03bfeeb2020-09-11 14:28:05 -0500972 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100973 vmexit = NESTED_EXIT_DONE;
974 break;
975 }
976 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
Babu Moger30abaa882020-09-11 14:28:12 -0500977 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100978 vmexit = NESTED_EXIT_DONE;
979 break;
980 }
981 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
Paolo Bonzini7c866632020-05-16 08:42:28 -0400982 /*
983 * Host-intercepted exceptions have been checked already in
984 * nested_svm_exit_special. There is nothing to do here,
985 * the vmexit is injected by svm_check_nested_events.
986 */
987 vmexit = NESTED_EXIT_DONE;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100988 break;
989 }
990 case SVM_EXIT_ERR: {
991 vmexit = NESTED_EXIT_DONE;
992 break;
993 }
994 default: {
Babu Mogerc62e2e92020-09-11 14:28:28 -0500995 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100996 vmexit = NESTED_EXIT_DONE;
997 }
998 }
999
1000 return vmexit;
1001}
1002
1003int nested_svm_exit_handled(struct vcpu_svm *svm)
1004{
1005 int vmexit;
1006
1007 vmexit = nested_svm_intercept(svm);
1008
1009 if (vmexit == NESTED_EXIT_DONE)
1010 nested_svm_vmexit(svm);
1011
1012 return vmexit;
1013}
1014
Paolo Bonzini63129752021-03-02 14:40:39 -05001015int nested_svm_check_permissions(struct kvm_vcpu *vcpu)
Joerg Roedel883b0a92020-03-24 10:41:52 +01001016{
Paolo Bonzini63129752021-03-02 14:40:39 -05001017 if (!(vcpu->arch.efer & EFER_SVME) || !is_paging(vcpu)) {
1018 kvm_queue_exception(vcpu, UD_VECTOR);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001019 return 1;
1020 }
1021
Paolo Bonzini63129752021-03-02 14:40:39 -05001022 if (to_svm(vcpu)->vmcb->save.cpl) {
1023 kvm_inject_gp(vcpu, 0);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001024 return 1;
1025 }
1026
1027 return 0;
1028}
1029
Paolo Bonzini7c866632020-05-16 08:42:28 -04001030static bool nested_exit_on_exception(struct vcpu_svm *svm)
Joerg Roedel883b0a92020-03-24 10:41:52 +01001031{
Paolo Bonzini7c866632020-05-16 08:42:28 -04001032 unsigned int nr = svm->vcpu.arch.exception.nr;
Joerg Roedel883b0a92020-03-24 10:41:52 +01001033
Babu Moger9780d512020-09-11 14:28:20 -05001034 return (svm->nested.ctl.intercepts[INTERCEPT_EXCEPTION] & BIT(nr));
Paolo Bonzini7c866632020-05-16 08:42:28 -04001035}
Joerg Roedel883b0a92020-03-24 10:41:52 +01001036
Paolo Bonzini7c866632020-05-16 08:42:28 -04001037static void nested_svm_inject_exception_vmexit(struct vcpu_svm *svm)
1038{
1039 unsigned int nr = svm->vcpu.arch.exception.nr;
Joerg Roedel883b0a92020-03-24 10:41:52 +01001040
1041 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1042 svm->vmcb->control.exit_code_hi = 0;
Paolo Bonzini7c866632020-05-16 08:42:28 -04001043
1044 if (svm->vcpu.arch.exception.has_error_code)
1045 svm->vmcb->control.exit_info_1 = svm->vcpu.arch.exception.error_code;
Joerg Roedel883b0a92020-03-24 10:41:52 +01001046
1047 /*
1048 * EXITINFO2 is undefined for all exception intercepts other
1049 * than #PF.
1050 */
Paolo Bonzini7c866632020-05-16 08:42:28 -04001051 if (nr == PF_VECTOR) {
1052 if (svm->vcpu.arch.exception.nested_apf)
1053 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
1054 else if (svm->vcpu.arch.exception.has_payload)
1055 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload;
1056 else
1057 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1058 } else if (nr == DB_VECTOR) {
1059 /* See inject_pending_event. */
1060 kvm_deliver_exception_payload(&svm->vcpu);
1061 if (svm->vcpu.arch.dr7 & DR7_GD) {
1062 svm->vcpu.arch.dr7 &= ~DR7_GD;
1063 kvm_update_dr7(&svm->vcpu);
1064 }
1065 } else
1066 WARN_ON(svm->vcpu.arch.exception.has_payload);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001067
Paolo Bonzini7c866632020-05-16 08:42:28 -04001068 nested_svm_vmexit(svm);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001069}
1070
Paolo Bonzini5b6724082020-05-16 08:50:35 -04001071static inline bool nested_exit_on_init(struct vcpu_svm *svm)
1072{
Babu Mogerc62e2e92020-09-11 14:28:28 -05001073 return vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_INIT);
Paolo Bonzini5b6724082020-05-16 08:50:35 -04001074}
1075
Paolo Bonzini33b22172020-04-17 10:24:18 -04001076static int svm_check_nested_events(struct kvm_vcpu *vcpu)
Joerg Roedel883b0a92020-03-24 10:41:52 +01001077{
1078 struct vcpu_svm *svm = to_svm(vcpu);
1079 bool block_nested_events =
Paolo Bonzinibd279622020-05-16 08:46:00 -04001080 kvm_event_needs_reinjection(vcpu) || svm->nested.nested_run_pending;
Paolo Bonzini5b6724082020-05-16 08:50:35 -04001081 struct kvm_lapic *apic = vcpu->arch.apic;
1082
1083 if (lapic_in_kernel(vcpu) &&
1084 test_bit(KVM_APIC_INIT, &apic->pending_events)) {
1085 if (block_nested_events)
1086 return -EBUSY;
1087 if (!nested_exit_on_init(svm))
1088 return 0;
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08001089 nested_svm_simple_vmexit(svm, SVM_EXIT_INIT);
Paolo Bonzini5b6724082020-05-16 08:50:35 -04001090 return 0;
1091 }
Joerg Roedel883b0a92020-03-24 10:41:52 +01001092
Paolo Bonzini7c866632020-05-16 08:42:28 -04001093 if (vcpu->arch.exception.pending) {
Maxim Levitsky4020da32021-04-01 17:38:14 +03001094 /*
1095 * Only a pending nested run can block a pending exception.
1096 * Otherwise an injected NMI/interrupt should either be
1097 * lost or delivered to the nested hypervisor in the EXITINTINFO
1098 * vmcb field, while delivering the pending exception.
1099 */
1100 if (svm->nested.nested_run_pending)
Paolo Bonzini7c866632020-05-16 08:42:28 -04001101 return -EBUSY;
1102 if (!nested_exit_on_exception(svm))
1103 return 0;
1104 nested_svm_inject_exception_vmexit(svm);
1105 return 0;
1106 }
1107
Paolo Bonzini221e7612020-04-23 08:13:10 -04001108 if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) {
Paolo Bonzini55714cd2020-04-23 08:17:28 -04001109 if (block_nested_events)
1110 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -04001111 if (!nested_exit_on_smi(svm))
1112 return 0;
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08001113 nested_svm_simple_vmexit(svm, SVM_EXIT_SMI);
Paolo Bonzini55714cd2020-04-23 08:17:28 -04001114 return 0;
1115 }
1116
Paolo Bonzini221e7612020-04-23 08:13:10 -04001117 if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) {
Cathy Avery9c3d3702020-04-14 16:11:06 -04001118 if (block_nested_events)
1119 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -04001120 if (!nested_exit_on_nmi(svm))
1121 return 0;
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08001122 nested_svm_simple_vmexit(svm, SVM_EXIT_NMI);
Cathy Avery9c3d3702020-04-14 16:11:06 -04001123 return 0;
1124 }
1125
Paolo Bonzini221e7612020-04-23 08:13:10 -04001126 if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) {
Joerg Roedel883b0a92020-03-24 10:41:52 +01001127 if (block_nested_events)
1128 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -04001129 if (!nested_exit_on_intr(svm))
1130 return 0;
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08001131 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
1132 nested_svm_simple_vmexit(svm, SVM_EXIT_INTR);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001133 return 0;
1134 }
1135
1136 return 0;
1137}
1138
1139int nested_svm_exit_special(struct vcpu_svm *svm)
1140{
1141 u32 exit_code = svm->vmcb->control.exit_code;
1142
1143 switch (exit_code) {
1144 case SVM_EXIT_INTR:
1145 case SVM_EXIT_NMI:
Joerg Roedel883b0a92020-03-24 10:41:52 +01001146 case SVM_EXIT_NPF:
Paolo Bonzini7c866632020-05-16 08:42:28 -04001147 return NESTED_EXIT_HOST;
1148 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1149 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
1150
Cathy Avery4995a362021-01-13 07:07:52 -05001151 if (svm->vmcb01.ptr->control.intercepts[INTERCEPT_EXCEPTION] &
1152 excp_bits)
Paolo Bonzini7c866632020-05-16 08:42:28 -04001153 return NESTED_EXIT_HOST;
1154 else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02001155 svm->vcpu.arch.apf.host_apf_flags)
Paolo Bonzini7c866632020-05-16 08:42:28 -04001156 /* Trap async PF even if not shadowing */
Joerg Roedel883b0a92020-03-24 10:41:52 +01001157 return NESTED_EXIT_HOST;
1158 break;
Paolo Bonzini7c866632020-05-16 08:42:28 -04001159 }
Joerg Roedel883b0a92020-03-24 10:41:52 +01001160 default:
1161 break;
1162 }
1163
1164 return NESTED_EXIT_CONTINUE;
1165}
Paolo Bonzini33b22172020-04-17 10:24:18 -04001166
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001167static int svm_get_nested_state(struct kvm_vcpu *vcpu,
1168 struct kvm_nested_state __user *user_kvm_nested_state,
1169 u32 user_data_size)
1170{
1171 struct vcpu_svm *svm;
1172 struct kvm_nested_state kvm_state = {
1173 .flags = 0,
1174 .format = KVM_STATE_NESTED_FORMAT_SVM,
1175 .size = sizeof(kvm_state),
1176 };
1177 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1178 &user_kvm_nested_state->data.svm[0];
1179
1180 if (!vcpu)
1181 return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE;
1182
1183 svm = to_svm(vcpu);
1184
1185 if (user_data_size < kvm_state.size)
1186 goto out;
1187
1188 /* First fill in the header and copy it out. */
1189 if (is_guest_mode(vcpu)) {
Maxim Levitsky0dd16b52020-08-27 20:11:39 +03001190 kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001191 kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE;
1192 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
1193
1194 if (svm->nested.nested_run_pending)
1195 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
1196 }
1197
1198 if (gif_set(svm))
1199 kvm_state.flags |= KVM_STATE_NESTED_GIF_SET;
1200
1201 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
1202 return -EFAULT;
1203
1204 if (!is_guest_mode(vcpu))
1205 goto out;
1206
1207 /*
1208 * Copy over the full size of the VMCB rather than just the size
1209 * of the structs.
1210 */
1211 if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE))
1212 return -EFAULT;
1213 if (copy_to_user(&user_vmcb->control, &svm->nested.ctl,
1214 sizeof(user_vmcb->control)))
1215 return -EFAULT;
Cathy Avery4995a362021-01-13 07:07:52 -05001216 if (copy_to_user(&user_vmcb->save, &svm->vmcb01.ptr->save,
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001217 sizeof(user_vmcb->save)))
1218 return -EFAULT;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001219out:
1220 return kvm_state.size;
1221}
1222
1223static int svm_set_nested_state(struct kvm_vcpu *vcpu,
1224 struct kvm_nested_state __user *user_kvm_nested_state,
1225 struct kvm_nested_state *kvm_state)
1226{
1227 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001228 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1229 &user_kvm_nested_state->data.svm[0];
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001230 struct vmcb_control_area *ctl;
1231 struct vmcb_save_area *save;
1232 int ret;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001233 u32 cr0;
1234
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001235 BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >
1236 KVM_STATE_NESTED_SVM_VMCB_SIZE);
1237
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001238 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM)
1239 return -EINVAL;
1240
1241 if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE |
1242 KVM_STATE_NESTED_RUN_PENDING |
1243 KVM_STATE_NESTED_GIF_SET))
1244 return -EINVAL;
1245
1246 /*
1247 * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
1248 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
1249 */
1250 if (!(vcpu->arch.efer & EFER_SVME)) {
1251 /* GIF=1 and no guest mode are required if SVME=0. */
1252 if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
1253 return -EINVAL;
1254 }
1255
1256 /* SMM temporarily disables SVM, so we cannot be in guest mode. */
1257 if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
1258 return -EINVAL;
1259
1260 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) {
1261 svm_leave_nested(svm);
Vitaly Kuznetsovd5cd6f32020-09-14 15:37:25 +02001262 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1263 return 0;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001264 }
1265
1266 if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa))
1267 return -EINVAL;
1268 if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)
1269 return -EINVAL;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001270
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001271 ret = -ENOMEM;
Sean Christophersoneba04b22021-03-30 19:30:25 -07001272 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL_ACCOUNT);
1273 save = kzalloc(sizeof(*save), GFP_KERNEL_ACCOUNT);
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001274 if (!ctl || !save)
1275 goto out_free;
1276
1277 ret = -EFAULT;
1278 if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl)))
1279 goto out_free;
1280 if (copy_from_user(save, &user_vmcb->save, sizeof(*save)))
1281 goto out_free;
1282
1283 ret = -EINVAL;
Krish Sadhukhanee695f22021-04-12 17:56:08 -04001284 if (!nested_vmcb_check_controls(vcpu, ctl))
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001285 goto out_free;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001286
1287 /*
1288 * Processor state contains L2 state. Check that it is
Paolo Bonzinicb9b6a12021-03-31 07:35:52 -04001289 * valid for guest mode (see nested_vmcb_check_save).
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001290 */
1291 cr0 = kvm_read_cr0(vcpu);
1292 if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001293 goto out_free;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001294
1295 /*
1296 * Validate host state saved from before VMRUN (see
1297 * nested_svm_check_permissions).
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001298 */
Krish Sadhukhan6906e062020-10-06 19:06:52 +00001299 if (!(save->cr0 & X86_CR0_PG) ||
1300 !(save->cr0 & X86_CR0_PE) ||
1301 (save->rflags & X86_EFLAGS_VM) ||
Paolo Bonzini63129752021-03-02 14:40:39 -05001302 !nested_vmcb_valid_sregs(vcpu, save))
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001303 goto out_free;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001304
1305 /*
Cathy Avery4995a362021-01-13 07:07:52 -05001306 * All checks done, we can enter guest mode. Userspace provides
1307 * vmcb12.control, which will be combined with L1 and stored into
1308 * vmcb02, and the L1 save state which we store in vmcb01.
1309 * L2 registers if needed are moved from the current VMCB to VMCB02.
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001310 */
Maxim Levitsky81f76ad2021-01-07 11:38:52 +02001311
Maxim Levitsky9d290e12021-05-03 15:54:44 +03001312 if (is_guest_mode(vcpu))
1313 svm_leave_nested(svm);
1314 else
1315 svm->nested.vmcb02.ptr->save = svm->vmcb01.ptr->save;
1316
Maxim Levitsky81f76ad2021-01-07 11:38:52 +02001317 svm->nested.nested_run_pending =
1318 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
1319
Maxim Levitsky0dd16b52020-08-27 20:11:39 +03001320 svm->nested.vmcb12_gpa = kvm_state->hdr.svm.vmcb_pa;
Paolo Bonzinic08f3902020-11-17 02:51:35 -05001321
1322 svm->vmcb01.ptr->save.es = save->es;
1323 svm->vmcb01.ptr->save.cs = save->cs;
1324 svm->vmcb01.ptr->save.ss = save->ss;
1325 svm->vmcb01.ptr->save.ds = save->ds;
1326 svm->vmcb01.ptr->save.gdtr = save->gdtr;
1327 svm->vmcb01.ptr->save.idtr = save->idtr;
1328 svm->vmcb01.ptr->save.rflags = save->rflags | X86_EFLAGS_FIXED;
1329 svm->vmcb01.ptr->save.efer = save->efer;
1330 svm->vmcb01.ptr->save.cr0 = save->cr0;
1331 svm->vmcb01.ptr->save.cr3 = save->cr3;
1332 svm->vmcb01.ptr->save.cr4 = save->cr4;
1333 svm->vmcb01.ptr->save.rax = save->rax;
1334 svm->vmcb01.ptr->save.rsp = save->rsp;
1335 svm->vmcb01.ptr->save.rip = save->rip;
1336 svm->vmcb01.ptr->save.cpl = 0;
1337
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05001338 nested_load_control_from_vmcb12(svm, ctl);
Cathy Avery4995a362021-01-13 07:07:52 -05001339
1340 svm_switch_vmcb(svm, &svm->nested.vmcb02);
1341
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05001342 nested_vmcb02_prepare_control(svm);
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001343
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -04001344 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001345 ret = 0;
1346out_free:
1347 kfree(save);
1348 kfree(ctl);
1349
1350 return ret;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001351}
1352
Maxim Levitsky232f75d2021-04-01 17:18:10 +03001353static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)
1354{
1355 struct vcpu_svm *svm = to_svm(vcpu);
1356
1357 if (WARN_ON(!is_guest_mode(vcpu)))
1358 return true;
1359
1360 if (nested_svm_load_cr3(&svm->vcpu, vcpu->arch.cr3,
1361 nested_npt_enabled(svm)))
1362 return false;
1363
1364 if (!nested_svm_vmrun_msrpm(svm)) {
1365 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1366 vcpu->run->internal.suberror =
1367 KVM_INTERNAL_ERROR_EMULATION;
1368 vcpu->run->internal.ndata = 0;
1369 return false;
1370 }
1371
1372 return true;
1373}
1374
Paolo Bonzini33b22172020-04-17 10:24:18 -04001375struct kvm_x86_nested_ops svm_nested_ops = {
1376 .check_events = svm_check_nested_events,
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08001377 .triple_fault = nested_svm_triple_fault,
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -04001378 .get_nested_state_pages = svm_get_nested_state_pages,
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001379 .get_state = svm_get_nested_state,
1380 .set_state = svm_set_nested_state,
Paolo Bonzini33b22172020-04-17 10:24:18 -04001381};