blob: 5f45991edcda88c6092dac8820ad8ee5f50be41b [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
Sean Christophersond2e56012021-06-09 16:42:26 -0700383static void nested_svm_transition_tlb_flush(struct kvm_vcpu *vcpu)
384{
385 /*
386 * TODO: optimize unconditional TLB flush/MMU sync. A partial list of
387 * things to fix before this can be conditional:
388 *
389 * - Flush TLBs for both L1 and L2 remote TLB flush
390 * - Honor L1's request to flush an ASID on nested VMRUN
391 * - Sync nested NPT MMU on VMRUN that flushes L2's ASID[*]
392 * - Don't crush a pending TLB flush in vmcb02 on nested VMRUN
393 * - Flush L1's ASID on KVM_REQ_TLB_FLUSH_GUEST
394 *
395 * [*] Unlike nested EPT, SVM's ASID management can invalidate nested
396 * NPT guest-physical mappings on VMRUN.
397 */
398 kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
399 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
400}
401
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200402/*
Vitaly Kuznetsovd82aaef2020-07-10 16:11:56 +0200403 * Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true
404 * if we are emulating VM-Entry into a guest with NPT enabled.
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200405 */
406static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
Maxim Levitskyb222b0b2021-06-07 12:01:59 +0300407 bool nested_npt, bool reload_pdptrs)
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200408{
Sean Christopherson11f0cbf2021-02-03 16:01:17 -0800409 if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3)))
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200410 return -EINVAL;
411
Maxim Levitskyb222b0b2021-06-07 12:01:59 +0300412 if (reload_pdptrs && !nested_npt && is_pae_paging(vcpu) &&
Sean Christophersona36dbec62021-06-07 12:01:57 +0300413 CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)))
414 return -EINVAL;
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200415
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200416 if (!nested_npt)
Sean Christophersonb5129102021-06-09 16:42:27 -0700417 kvm_mmu_new_pgd(vcpu, cr3);
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200418
419 vcpu->arch.cr3 = cr3;
420 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
421
422 kvm_init_mmu(vcpu, false);
423
424 return 0;
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200425}
426
Cathy Avery4995a362021-01-13 07:07:52 -0500427void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm)
428{
429 if (!svm->nested.vmcb02.ptr)
430 return;
431
432 /* FIXME: merge g_pat from vmcb01 and vmcb12. */
433 svm->nested.vmcb02.ptr->save.g_pat = svm->vmcb01.ptr->save.g_pat;
434}
435
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500436static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100437{
Cathy Avery81733962021-03-01 15:08:44 -0500438 bool new_vmcb12 = false;
439
Cathy Avery4995a362021-01-13 07:07:52 -0500440 nested_vmcb02_compute_g_pat(svm);
441
Joerg Roedel883b0a92020-03-24 10:41:52 +0100442 /* Load the nested guest state */
Cathy Avery81733962021-03-01 15:08:44 -0500443 if (svm->nested.vmcb12_gpa != svm->nested.last_vmcb12_gpa) {
444 new_vmcb12 = true;
445 svm->nested.last_vmcb12_gpa = svm->nested.vmcb12_gpa;
446 }
447
448 if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_SEG))) {
449 svm->vmcb->save.es = vmcb12->save.es;
450 svm->vmcb->save.cs = vmcb12->save.cs;
451 svm->vmcb->save.ss = vmcb12->save.ss;
452 svm->vmcb->save.ds = vmcb12->save.ds;
453 svm->vmcb->save.cpl = vmcb12->save.cpl;
454 vmcb_mark_dirty(svm->vmcb, VMCB_SEG);
455 }
456
457 if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DT))) {
458 svm->vmcb->save.gdtr = vmcb12->save.gdtr;
459 svm->vmcb->save.idtr = vmcb12->save.idtr;
460 vmcb_mark_dirty(svm->vmcb, VMCB_DT);
461 }
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500462
Paolo Bonzini8cce12b2020-11-27 12:46:36 -0500463 kvm_set_rflags(&svm->vcpu, vmcb12->save.rflags | X86_EFLAGS_FIXED);
Paolo Bonzini3c346c02021-03-31 06:28:01 -0400464
465 /*
466 * Force-set EFER_SVME even though it is checked earlier on the
467 * VMCB12, because the guest can flip the bit between the check
468 * and now. Clearing EFER_SVME would call svm_free_nested.
469 */
470 svm_set_efer(&svm->vcpu, vmcb12->save.efer | EFER_SVME);
471
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300472 svm_set_cr0(&svm->vcpu, vmcb12->save.cr0);
473 svm_set_cr4(&svm->vcpu, vmcb12->save.cr4);
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500474
475 svm->vcpu.arch.cr2 = vmcb12->save.cr2;
Cathy Avery81733962021-03-01 15:08:44 -0500476
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300477 kvm_rax_write(&svm->vcpu, vmcb12->save.rax);
478 kvm_rsp_write(&svm->vcpu, vmcb12->save.rsp);
479 kvm_rip_write(&svm->vcpu, vmcb12->save.rip);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100480
481 /* In case we don't even reach vcpu_run, the fields are not updated */
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300482 svm->vmcb->save.rax = vmcb12->save.rax;
483 svm->vmcb->save.rsp = vmcb12->save.rsp;
484 svm->vmcb->save.rip = vmcb12->save.rip;
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500485
Cathy Avery81733962021-03-01 15:08:44 -0500486 /* These bits will be set properly on the first execution when new_vmc12 is true */
487 if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DR))) {
488 svm->vmcb->save.dr7 = vmcb12->save.dr7 | DR7_FIXED_1;
489 svm->vcpu.arch.dr6 = vmcb12->save.dr6 | DR6_ACTIVE_LOW;
490 vmcb_mark_dirty(svm->vmcb, VMCB_DR);
491 }
Paolo Bonzinif241d712020-05-18 10:56:43 -0400492}
Joerg Roedel883b0a92020-03-24 10:41:52 +0100493
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500494static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
Paolo Bonzinif241d712020-05-18 10:56:43 -0400495{
Paolo Bonzini91b71302020-05-22 12:28:52 -0400496 const u32 mask = V_INTR_MASKING_MASK | V_GIF_ENABLE_MASK | V_GIF_MASK;
Sean Christophersond2e56012021-06-09 16:42:26 -0700497 struct kvm_vcpu *vcpu = &svm->vcpu;
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200498
Paolo Bonzini7c3ecfc2020-11-16 06:13:15 -0500499 /*
500 * Filled at exit: exit_code, exit_code_hi, exit_info_1, exit_info_2,
501 * exit_int_info, exit_int_info_err, next_rip, insn_len, insn_bytes.
502 */
Cathy Avery4995a362021-01-13 07:07:52 -0500503
Paolo Bonzini7c3ecfc2020-11-16 06:13:15 -0500504 /*
505 * Also covers avic_vapic_bar, avic_backing_page, avic_logical_id,
506 * avic_physical_id.
507 */
508 WARN_ON(svm->vmcb01.ptr->control.int_ctl & AVIC_ENABLE_MASK);
509
510 /* Copied from vmcb01. msrpm_base can be overwritten later. */
511 svm->vmcb->control.nested_ctl = svm->vmcb01.ptr->control.nested_ctl;
512 svm->vmcb->control.iopm_base_pa = svm->vmcb01.ptr->control.iopm_base_pa;
513 svm->vmcb->control.msrpm_base_pa = svm->vmcb01.ptr->control.msrpm_base_pa;
514
515 /* Done at vmrun: asid. */
516
517 /* Also overwritten later if necessary. */
518 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
519
520 /* nested_cr3. */
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200521 if (nested_npt_enabled(svm))
Sean Christophersond2e56012021-06-09 16:42:26 -0700522 nested_svm_init_mmu_context(vcpu);
Paolo Bonzini69cb8772020-05-22 05:27:46 -0400523
Sean Christophersond2e56012021-06-09 16:42:26 -0700524 svm->vmcb->control.tsc_offset = vcpu->arch.tsc_offset =
525 vcpu->arch.l1_tsc_offset + svm->nested.ctl.tsc_offset;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100526
Paolo Bonzini91b71302020-05-22 12:28:52 -0400527 svm->vmcb->control.int_ctl =
528 (svm->nested.ctl.int_ctl & ~mask) |
Cathy Avery4995a362021-01-13 07:07:52 -0500529 (svm->vmcb01.ptr->control.int_ctl & mask);
Paolo Bonzini91b71302020-05-22 12:28:52 -0400530
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400531 svm->vmcb->control.virt_ext = svm->nested.ctl.virt_ext;
532 svm->vmcb->control.int_vector = svm->nested.ctl.int_vector;
533 svm->vmcb->control.int_state = svm->nested.ctl.int_state;
534 svm->vmcb->control.event_inj = svm->nested.ctl.event_inj;
535 svm->vmcb->control.event_inj_err = svm->nested.ctl.event_inj_err;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100536
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400537 svm->vmcb->control.pause_filter_count = svm->nested.ctl.pause_filter_count;
538 svm->vmcb->control.pause_filter_thresh = svm->nested.ctl.pause_filter_thresh;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100539
Sean Christophersond2e56012021-06-09 16:42:26 -0700540 nested_svm_transition_tlb_flush(vcpu);
541
Joerg Roedel883b0a92020-03-24 10:41:52 +0100542 /* Enter Guest-Mode */
Sean Christophersond2e56012021-06-09 16:42:26 -0700543 enter_guest_mode(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100544
545 /*
Paolo Bonzini4bb170a2020-11-16 06:38:19 -0500546 * Merge guest and host intercepts - must be called with vcpu in
547 * guest-mode to take effect.
Joerg Roedel883b0a92020-03-24 10:41:52 +0100548 */
549 recalc_intercepts(svm);
Paolo Bonzinif241d712020-05-18 10:56:43 -0400550}
551
Babu Mogerd00b99c2021-02-17 10:56:04 -0500552static void nested_svm_copy_common_state(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
553{
554 /*
555 * Some VMCB state is shared between L1 and L2 and thus has to be
556 * moved at the time of nested vmrun and vmexit.
557 *
558 * VMLOAD/VMSAVE state would also belong in this category, but KVM
559 * always performs VMLOAD and VMSAVE from the VMCB01.
560 */
561 to_vmcb->save.spec_ctrl = from_vmcb->save.spec_ctrl;
562}
563
Paolo Bonzini63129752021-03-02 14:40:39 -0500564int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa,
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300565 struct vmcb *vmcb12)
Paolo Bonzinif241d712020-05-18 10:56:43 -0400566{
Paolo Bonzini63129752021-03-02 14:40:39 -0500567 struct vcpu_svm *svm = to_svm(vcpu);
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200568 int ret;
569
Maxim Levitsky954f4192021-02-17 16:57:13 +0200570 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb12_gpa,
571 vmcb12->save.rip,
572 vmcb12->control.int_ctl,
573 vmcb12->control.event_inj,
574 vmcb12->control.nested_ctl);
575
576 trace_kvm_nested_intercepts(vmcb12->control.intercepts[INTERCEPT_CR] & 0xffff,
577 vmcb12->control.intercepts[INTERCEPT_CR] >> 16,
578 vmcb12->control.intercepts[INTERCEPT_EXCEPTION],
579 vmcb12->control.intercepts[INTERCEPT_WORD3],
580 vmcb12->control.intercepts[INTERCEPT_WORD4],
581 vmcb12->control.intercepts[INTERCEPT_WORD5]);
582
583
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300584 svm->nested.vmcb12_gpa = vmcb12_gpa;
Cathy Avery4995a362021-01-13 07:07:52 -0500585
586 WARN_ON(svm->vmcb == svm->nested.vmcb02.ptr);
587
Babu Mogerd00b99c2021-02-17 10:56:04 -0500588 nested_svm_copy_common_state(svm->vmcb01.ptr, svm->nested.vmcb02.ptr);
Cathy Avery4995a362021-01-13 07:07:52 -0500589
590 svm_switch_vmcb(svm, &svm->nested.vmcb02);
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500591 nested_vmcb02_prepare_control(svm);
592 nested_vmcb02_prepare_save(svm, vmcb12);
Paolo Bonzinif241d712020-05-18 10:56:43 -0400593
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300594 ret = nested_svm_load_cr3(&svm->vcpu, vmcb12->save.cr3,
Maxim Levitskyb222b0b2021-06-07 12:01:59 +0300595 nested_npt_enabled(svm), true);
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200596 if (ret)
597 return ret;
598
Paolo Bonzinia04aead2021-02-18 07:16:59 -0500599 if (!npt_enabled)
Paolo Bonzini63129752021-03-02 14:40:39 -0500600 vcpu->arch.mmu->inject_page_fault = svm_inject_page_fault_nested;
Paolo Bonzinia04aead2021-02-18 07:16:59 -0500601
Paolo Bonziniffdf7f92020-05-22 12:18:27 -0400602 svm_set_gif(svm, true);
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200603
604 return 0;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100605}
606
Paolo Bonzini63129752021-03-02 14:40:39 -0500607int nested_svm_vmrun(struct kvm_vcpu *vcpu)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100608{
Paolo Bonzini63129752021-03-02 14:40:39 -0500609 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100610 int ret;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300611 struct vmcb *vmcb12;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100612 struct kvm_host_map map;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300613 u64 vmcb12_gpa;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100614
Paolo Bonzini63129752021-03-02 14:40:39 -0500615 if (is_smm(vcpu)) {
616 kvm_queue_exception(vcpu, UD_VECTOR);
Paolo Bonzini7c67f5462020-04-23 10:52:48 -0400617 return 1;
618 }
Joerg Roedel883b0a92020-03-24 10:41:52 +0100619
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300620 vmcb12_gpa = svm->vmcb->save.rax;
Paolo Bonzini63129752021-03-02 14:40:39 -0500621 ret = kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100622 if (ret == -EINVAL) {
Paolo Bonzini63129752021-03-02 14:40:39 -0500623 kvm_inject_gp(vcpu, 0);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100624 return 1;
625 } else if (ret) {
Paolo Bonzini63129752021-03-02 14:40:39 -0500626 return kvm_skip_emulated_instruction(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100627 }
628
Paolo Bonzini63129752021-03-02 14:40:39 -0500629 ret = kvm_skip_emulated_instruction(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100630
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300631 vmcb12 = map.hva;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100632
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300633 if (WARN_ON_ONCE(!svm->nested.initialized))
634 return -EINVAL;
635
Paolo Bonzinicb9b6a12021-03-31 07:35:52 -0400636 nested_load_control_from_vmcb12(svm, &vmcb12->control);
637
638 if (!nested_vmcb_valid_sregs(vcpu, &vmcb12->save) ||
Krish Sadhukhanee695f22021-04-12 17:56:08 -0400639 !nested_vmcb_check_controls(vcpu, &svm->nested.ctl)) {
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300640 vmcb12->control.exit_code = SVM_EXIT_ERR;
641 vmcb12->control.exit_code_hi = 0;
642 vmcb12->control.exit_info_1 = 0;
643 vmcb12->control.exit_info_2 = 0;
Paolo Bonzini69c9dfa2020-05-13 12:57:26 -0400644 goto out;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100645 }
646
Joerg Roedel883b0a92020-03-24 10:41:52 +0100647
648 /* Clear internal status */
Paolo Bonzini63129752021-03-02 14:40:39 -0500649 kvm_clear_exception_queue(vcpu);
650 kvm_clear_interrupt_queue(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100651
652 /*
Cathy Avery4995a362021-01-13 07:07:52 -0500653 * Since vmcb01 is not in use, we can use it to store some of the L1
654 * state.
Joerg Roedel883b0a92020-03-24 10:41:52 +0100655 */
Paolo Bonzini63129752021-03-02 14:40:39 -0500656 svm->vmcb01.ptr->save.efer = vcpu->arch.efer;
657 svm->vmcb01.ptr->save.cr0 = kvm_read_cr0(vcpu);
658 svm->vmcb01.ptr->save.cr4 = vcpu->arch.cr4;
659 svm->vmcb01.ptr->save.rflags = kvm_get_rflags(vcpu);
660 svm->vmcb01.ptr->save.rip = kvm_rip_read(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100661
Cathy Avery4995a362021-01-13 07:07:52 -0500662 if (!npt_enabled)
Paolo Bonzini63129752021-03-02 14:40:39 -0500663 svm->vmcb01.ptr->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100664
Paolo Bonzinif74f9412020-04-23 13:22:27 -0400665 svm->nested.nested_run_pending = 1;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100666
Paolo Bonzini63129752021-03-02 14:40:39 -0500667 if (enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12))
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200668 goto out_exit_err;
Vitaly Kuznetsovebdb3db2020-07-10 16:11:51 +0200669
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200670 if (nested_svm_vmrun_msrpm(svm))
671 goto out;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100672
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200673out_exit_err:
674 svm->nested.nested_run_pending = 0;
675
676 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
677 svm->vmcb->control.exit_code_hi = 0;
678 svm->vmcb->control.exit_info_1 = 0;
679 svm->vmcb->control.exit_info_2 = 0;
680
681 nested_svm_vmexit(svm);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100682
Paolo Bonzini69c9dfa2020-05-13 12:57:26 -0400683out:
Paolo Bonzini63129752021-03-02 14:40:39 -0500684 kvm_vcpu_unmap(vcpu, &map, true);
Paolo Bonzini69c9dfa2020-05-13 12:57:26 -0400685
Joerg Roedel883b0a92020-03-24 10:41:52 +0100686 return ret;
687}
688
689void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
690{
691 to_vmcb->save.fs = from_vmcb->save.fs;
692 to_vmcb->save.gs = from_vmcb->save.gs;
693 to_vmcb->save.tr = from_vmcb->save.tr;
694 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
695 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
696 to_vmcb->save.star = from_vmcb->save.star;
697 to_vmcb->save.lstar = from_vmcb->save.lstar;
698 to_vmcb->save.cstar = from_vmcb->save.cstar;
699 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
700 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
701 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
702 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
703}
704
705int nested_svm_vmexit(struct vcpu_svm *svm)
706{
Paolo Bonzini63129752021-03-02 14:40:39 -0500707 struct kvm_vcpu *vcpu = &svm->vcpu;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300708 struct vmcb *vmcb12;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100709 struct vmcb *vmcb = svm->vmcb;
710 struct kvm_host_map map;
Paolo Bonzini63129752021-03-02 14:40:39 -0500711 int rc;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100712
Sean Christophersoncb6a32c2021-03-02 09:45:14 -0800713 /* Triple faults in L2 should never escape. */
714 WARN_ON_ONCE(kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu));
715
Paolo Bonzini63129752021-03-02 14:40:39 -0500716 rc = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.vmcb12_gpa), &map);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100717 if (rc) {
718 if (rc == -EINVAL)
Paolo Bonzini63129752021-03-02 14:40:39 -0500719 kvm_inject_gp(vcpu, 0);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100720 return 1;
721 }
722
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300723 vmcb12 = map.hva;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100724
725 /* Exit Guest-Mode */
Paolo Bonzini63129752021-03-02 14:40:39 -0500726 leave_guest_mode(vcpu);
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300727 svm->nested.vmcb12_gpa = 0;
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400728 WARN_ON_ONCE(svm->nested.nested_run_pending);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100729
Paolo Bonzini63129752021-03-02 14:40:39 -0500730 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Maxim Levitskyf2c7ef32021-01-07 11:38:51 +0200731
Paolo Bonzini38c0b192020-04-23 13:13:09 -0400732 /* in case we halted in L2 */
733 svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE;
734
Joerg Roedel883b0a92020-03-24 10:41:52 +0100735 /* Give the current vmcb to the guest */
Joerg Roedel883b0a92020-03-24 10:41:52 +0100736
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300737 vmcb12->save.es = vmcb->save.es;
738 vmcb12->save.cs = vmcb->save.cs;
739 vmcb12->save.ss = vmcb->save.ss;
740 vmcb12->save.ds = vmcb->save.ds;
741 vmcb12->save.gdtr = vmcb->save.gdtr;
742 vmcb12->save.idtr = vmcb->save.idtr;
743 vmcb12->save.efer = svm->vcpu.arch.efer;
Paolo Bonzini63129752021-03-02 14:40:39 -0500744 vmcb12->save.cr0 = kvm_read_cr0(vcpu);
745 vmcb12->save.cr3 = kvm_read_cr3(vcpu);
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300746 vmcb12->save.cr2 = vmcb->save.cr2;
747 vmcb12->save.cr4 = svm->vcpu.arch.cr4;
Paolo Bonzini63129752021-03-02 14:40:39 -0500748 vmcb12->save.rflags = kvm_get_rflags(vcpu);
749 vmcb12->save.rip = kvm_rip_read(vcpu);
750 vmcb12->save.rsp = kvm_rsp_read(vcpu);
751 vmcb12->save.rax = kvm_rax_read(vcpu);
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300752 vmcb12->save.dr7 = vmcb->save.dr7;
753 vmcb12->save.dr6 = svm->vcpu.arch.dr6;
754 vmcb12->save.cpl = vmcb->save.cpl;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100755
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300756 vmcb12->control.int_state = vmcb->control.int_state;
757 vmcb12->control.exit_code = vmcb->control.exit_code;
758 vmcb12->control.exit_code_hi = vmcb->control.exit_code_hi;
759 vmcb12->control.exit_info_1 = vmcb->control.exit_info_1;
760 vmcb12->control.exit_info_2 = vmcb->control.exit_info_2;
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400761
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300762 if (vmcb12->control.exit_code != SVM_EXIT_ERR)
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -0500763 nested_save_pending_event_to_vmcb12(svm, vmcb12);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100764
765 if (svm->nrips_enabled)
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300766 vmcb12->control.next_rip = vmcb->control.next_rip;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100767
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300768 vmcb12->control.int_ctl = svm->nested.ctl.int_ctl;
769 vmcb12->control.tlb_ctl = svm->nested.ctl.tlb_ctl;
770 vmcb12->control.event_inj = svm->nested.ctl.event_inj;
771 vmcb12->control.event_inj_err = svm->nested.ctl.event_inj_err;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100772
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300773 vmcb12->control.pause_filter_count =
Joerg Roedel883b0a92020-03-24 10:41:52 +0100774 svm->vmcb->control.pause_filter_count;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300775 vmcb12->control.pause_filter_thresh =
Joerg Roedel883b0a92020-03-24 10:41:52 +0100776 svm->vmcb->control.pause_filter_thresh;
777
Babu Mogerd00b99c2021-02-17 10:56:04 -0500778 nested_svm_copy_common_state(svm->nested.vmcb02.ptr, svm->vmcb01.ptr);
779
Cathy Avery4995a362021-01-13 07:07:52 -0500780 svm_switch_vmcb(svm, &svm->vmcb01);
781
782 /*
783 * On vmexit the GIF is set to false and
784 * no event can be injected in L1.
785 */
Maxim Levitsky98837642020-08-27 19:27:18 +0300786 svm_set_gif(svm, false);
Cathy Avery4995a362021-01-13 07:07:52 -0500787 svm->vmcb->control.exit_int_info = 0;
Maxim Levitsky98837642020-08-27 19:27:18 +0300788
Paolo Bonzini7ca62d12020-11-16 06:38:19 -0500789 svm->vcpu.arch.tsc_offset = svm->vcpu.arch.l1_tsc_offset;
790 if (svm->vmcb->control.tsc_offset != svm->vcpu.arch.tsc_offset) {
791 svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset;
792 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
793 }
Paolo Bonzini18fc6c52020-05-18 11:07:08 -0400794
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400795 svm->nested.ctl.nested_cr3 = 0;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100796
Cathy Avery4995a362021-01-13 07:07:52 -0500797 /*
798 * Restore processor state that had been saved in vmcb01
799 */
Paolo Bonzini63129752021-03-02 14:40:39 -0500800 kvm_set_rflags(vcpu, svm->vmcb->save.rflags);
801 svm_set_efer(vcpu, svm->vmcb->save.efer);
802 svm_set_cr0(vcpu, svm->vmcb->save.cr0 | X86_CR0_PE);
803 svm_set_cr4(vcpu, svm->vmcb->save.cr4);
804 kvm_rax_write(vcpu, svm->vmcb->save.rax);
805 kvm_rsp_write(vcpu, svm->vmcb->save.rsp);
806 kvm_rip_write(vcpu, svm->vmcb->save.rip);
Cathy Avery4995a362021-01-13 07:07:52 -0500807
808 svm->vcpu.arch.dr7 = DR7_FIXED_1;
809 kvm_update_dr7(&svm->vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100810
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300811 trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code,
812 vmcb12->control.exit_info_1,
813 vmcb12->control.exit_info_2,
814 vmcb12->control.exit_int_info,
815 vmcb12->control.exit_int_info_err,
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400816 KVM_ISA_SVM);
817
Paolo Bonzini63129752021-03-02 14:40:39 -0500818 kvm_vcpu_unmap(vcpu, &map, true);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100819
Sean Christophersond2e56012021-06-09 16:42:26 -0700820 nested_svm_transition_tlb_flush(vcpu);
821
Paolo Bonzini63129752021-03-02 14:40:39 -0500822 nested_svm_uninit_mmu_context(vcpu);
Vitaly Kuznetsovbf7dea42020-07-10 16:11:54 +0200823
Maxim Levitskyb222b0b2021-06-07 12:01:59 +0300824 rc = nested_svm_load_cr3(vcpu, svm->vmcb->save.cr3, false, true);
Vitaly Kuznetsovd82aaef2020-07-10 16:11:56 +0200825 if (rc)
826 return 1;
Vitaly Kuznetsovbf7dea42020-07-10 16:11:54 +0200827
Joerg Roedel883b0a92020-03-24 10:41:52 +0100828 /*
829 * Drop what we picked up for L2 via svm_complete_interrupts() so it
830 * doesn't end up in L1.
831 */
832 svm->vcpu.arch.nmi_injected = false;
Paolo Bonzini63129752021-03-02 14:40:39 -0500833 kvm_clear_exception_queue(vcpu);
834 kvm_clear_interrupt_queue(vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100835
Krish Sadhukhan9a7de6e2021-03-23 13:50:03 -0400836 /*
837 * If we are here following the completion of a VMRUN that
838 * is being single-stepped, queue the pending #DB intercept
839 * right now so that it an be accounted for before we execute
840 * L1's next instruction.
841 */
842 if (unlikely(svm->vmcb->save.rflags & X86_EFLAGS_TF))
843 kvm_queue_exception(&(svm->vcpu), DB_VECTOR);
844
Joerg Roedel883b0a92020-03-24 10:41:52 +0100845 return 0;
846}
847
Sean Christophersoncb6a32c2021-03-02 09:45:14 -0800848static void nested_svm_triple_fault(struct kvm_vcpu *vcpu)
849{
Sean Christopherson3a87c7e2021-03-02 09:45:15 -0800850 nested_svm_simple_vmexit(to_svm(vcpu), SVM_EXIT_SHUTDOWN);
Sean Christophersoncb6a32c2021-03-02 09:45:14 -0800851}
852
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300853int svm_allocate_nested(struct vcpu_svm *svm)
854{
Cathy Avery4995a362021-01-13 07:07:52 -0500855 struct page *vmcb02_page;
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300856
857 if (svm->nested.initialized)
858 return 0;
859
Cathy Avery4995a362021-01-13 07:07:52 -0500860 vmcb02_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
861 if (!vmcb02_page)
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300862 return -ENOMEM;
Cathy Avery4995a362021-01-13 07:07:52 -0500863 svm->nested.vmcb02.ptr = page_address(vmcb02_page);
864 svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300865
866 svm->nested.msrpm = svm_vcpu_alloc_msrpm();
867 if (!svm->nested.msrpm)
Cathy Avery4995a362021-01-13 07:07:52 -0500868 goto err_free_vmcb02;
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300869 svm_vcpu_init_msrpm(&svm->vcpu, svm->nested.msrpm);
870
871 svm->nested.initialized = true;
872 return 0;
873
Cathy Avery4995a362021-01-13 07:07:52 -0500874err_free_vmcb02:
875 __free_page(vmcb02_page);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300876 return -ENOMEM;
877}
878
879void svm_free_nested(struct vcpu_svm *svm)
880{
881 if (!svm->nested.initialized)
882 return;
883
884 svm_vcpu_free_msrpm(svm->nested.msrpm);
885 svm->nested.msrpm = NULL;
886
Cathy Avery4995a362021-01-13 07:07:52 -0500887 __free_page(virt_to_page(svm->nested.vmcb02.ptr));
888 svm->nested.vmcb02.ptr = NULL;
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300889
Maxim Levitskyc74ad082021-05-03 15:54:43 +0300890 /*
891 * When last_vmcb12_gpa matches the current vmcb12 gpa,
892 * some vmcb12 fields are not loaded if they are marked clean
893 * in the vmcb12, since in this case they are up to date already.
894 *
895 * When the vmcb02 is freed, this optimization becomes invalid.
896 */
897 svm->nested.last_vmcb12_gpa = INVALID_GPA;
898
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300899 svm->nested.initialized = false;
900}
901
Paolo Bonzinic513f482020-05-18 13:08:37 -0400902/*
903 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
904 */
905void svm_leave_nested(struct vcpu_svm *svm)
906{
Paolo Bonzini63129752021-03-02 14:40:39 -0500907 struct kvm_vcpu *vcpu = &svm->vcpu;
908
909 if (is_guest_mode(vcpu)) {
Paolo Bonzinic513f482020-05-18 13:08:37 -0400910 svm->nested.nested_run_pending = 0;
Maxim Levitskyc74ad082021-05-03 15:54:43 +0300911 svm->nested.vmcb12_gpa = INVALID_GPA;
912
Paolo Bonzini63129752021-03-02 14:40:39 -0500913 leave_guest_mode(vcpu);
Cathy Avery4995a362021-01-13 07:07:52 -0500914
Maxim Levitskydeee59b2021-05-03 15:54:42 +0300915 svm_switch_vmcb(svm, &svm->vmcb01);
Cathy Avery4995a362021-01-13 07:07:52 -0500916
Paolo Bonzini63129752021-03-02 14:40:39 -0500917 nested_svm_uninit_mmu_context(vcpu);
Maxim Levitsky56fe28d2021-01-07 11:38:54 +0200918 vmcb_mark_all_dirty(svm->vmcb);
Paolo Bonzinic513f482020-05-18 13:08:37 -0400919 }
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -0400920
Paolo Bonzini63129752021-03-02 14:40:39 -0500921 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Paolo Bonzinic513f482020-05-18 13:08:37 -0400922}
923
Joerg Roedel883b0a92020-03-24 10:41:52 +0100924static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
925{
926 u32 offset, msr, value;
927 int write, mask;
928
Babu Mogerc62e2e92020-09-11 14:28:28 -0500929 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100930 return NESTED_EXIT_HOST;
931
932 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
933 offset = svm_msrpm_offset(msr);
934 write = svm->vmcb->control.exit_info_1 & 1;
935 mask = 1 << ((2 * (msr & 0xf)) + write);
936
937 if (offset == MSR_INVALID)
938 return NESTED_EXIT_DONE;
939
940 /* Offset is in 32 bit units but need in 8 bit units */
941 offset *= 4;
942
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400943 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100944 return NESTED_EXIT_DONE;
945
946 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
947}
948
Joerg Roedel883b0a92020-03-24 10:41:52 +0100949static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
950{
951 unsigned port, size, iopm_len;
952 u16 val, mask;
953 u8 start_bit;
954 u64 gpa;
955
Babu Mogerc62e2e92020-09-11 14:28:28 -0500956 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_IOIO_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100957 return NESTED_EXIT_HOST;
958
959 port = svm->vmcb->control.exit_info_1 >> 16;
960 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
961 SVM_IOIO_SIZE_SHIFT;
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400962 gpa = svm->nested.ctl.iopm_base_pa + (port / 8);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100963 start_bit = port % 8;
964 iopm_len = (start_bit + size > 8) ? 2 : 1;
965 mask = (0xf >> (4 - size)) << start_bit;
966 val = 0;
967
968 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
969 return NESTED_EXIT_DONE;
970
971 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
972}
973
974static int nested_svm_intercept(struct vcpu_svm *svm)
975{
976 u32 exit_code = svm->vmcb->control.exit_code;
977 int vmexit = NESTED_EXIT_HOST;
978
979 switch (exit_code) {
980 case SVM_EXIT_MSR:
981 vmexit = nested_svm_exit_handled_msr(svm);
982 break;
983 case SVM_EXIT_IOIO:
984 vmexit = nested_svm_intercept_ioio(svm);
985 break;
986 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
Babu Moger03bfeeb2020-09-11 14:28:05 -0500987 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100988 vmexit = NESTED_EXIT_DONE;
989 break;
990 }
991 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
Babu Moger30abaa882020-09-11 14:28:12 -0500992 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100993 vmexit = NESTED_EXIT_DONE;
994 break;
995 }
996 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
Paolo Bonzini7c866632020-05-16 08:42:28 -0400997 /*
998 * Host-intercepted exceptions have been checked already in
999 * nested_svm_exit_special. There is nothing to do here,
1000 * the vmexit is injected by svm_check_nested_events.
1001 */
1002 vmexit = NESTED_EXIT_DONE;
Joerg Roedel883b0a92020-03-24 10:41:52 +01001003 break;
1004 }
1005 case SVM_EXIT_ERR: {
1006 vmexit = NESTED_EXIT_DONE;
1007 break;
1008 }
1009 default: {
Babu Mogerc62e2e92020-09-11 14:28:28 -05001010 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
Joerg Roedel883b0a92020-03-24 10:41:52 +01001011 vmexit = NESTED_EXIT_DONE;
1012 }
1013 }
1014
1015 return vmexit;
1016}
1017
1018int nested_svm_exit_handled(struct vcpu_svm *svm)
1019{
1020 int vmexit;
1021
1022 vmexit = nested_svm_intercept(svm);
1023
1024 if (vmexit == NESTED_EXIT_DONE)
1025 nested_svm_vmexit(svm);
1026
1027 return vmexit;
1028}
1029
Paolo Bonzini63129752021-03-02 14:40:39 -05001030int nested_svm_check_permissions(struct kvm_vcpu *vcpu)
Joerg Roedel883b0a92020-03-24 10:41:52 +01001031{
Paolo Bonzini63129752021-03-02 14:40:39 -05001032 if (!(vcpu->arch.efer & EFER_SVME) || !is_paging(vcpu)) {
1033 kvm_queue_exception(vcpu, UD_VECTOR);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001034 return 1;
1035 }
1036
Paolo Bonzini63129752021-03-02 14:40:39 -05001037 if (to_svm(vcpu)->vmcb->save.cpl) {
1038 kvm_inject_gp(vcpu, 0);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001039 return 1;
1040 }
1041
1042 return 0;
1043}
1044
Paolo Bonzini7c866632020-05-16 08:42:28 -04001045static bool nested_exit_on_exception(struct vcpu_svm *svm)
Joerg Roedel883b0a92020-03-24 10:41:52 +01001046{
Paolo Bonzini7c866632020-05-16 08:42:28 -04001047 unsigned int nr = svm->vcpu.arch.exception.nr;
Joerg Roedel883b0a92020-03-24 10:41:52 +01001048
Babu Moger9780d512020-09-11 14:28:20 -05001049 return (svm->nested.ctl.intercepts[INTERCEPT_EXCEPTION] & BIT(nr));
Paolo Bonzini7c866632020-05-16 08:42:28 -04001050}
Joerg Roedel883b0a92020-03-24 10:41:52 +01001051
Paolo Bonzini7c866632020-05-16 08:42:28 -04001052static void nested_svm_inject_exception_vmexit(struct vcpu_svm *svm)
1053{
1054 unsigned int nr = svm->vcpu.arch.exception.nr;
Joerg Roedel883b0a92020-03-24 10:41:52 +01001055
1056 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1057 svm->vmcb->control.exit_code_hi = 0;
Paolo Bonzini7c866632020-05-16 08:42:28 -04001058
1059 if (svm->vcpu.arch.exception.has_error_code)
1060 svm->vmcb->control.exit_info_1 = svm->vcpu.arch.exception.error_code;
Joerg Roedel883b0a92020-03-24 10:41:52 +01001061
1062 /*
1063 * EXITINFO2 is undefined for all exception intercepts other
1064 * than #PF.
1065 */
Paolo Bonzini7c866632020-05-16 08:42:28 -04001066 if (nr == PF_VECTOR) {
1067 if (svm->vcpu.arch.exception.nested_apf)
1068 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
1069 else if (svm->vcpu.arch.exception.has_payload)
1070 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload;
1071 else
1072 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1073 } else if (nr == DB_VECTOR) {
1074 /* See inject_pending_event. */
1075 kvm_deliver_exception_payload(&svm->vcpu);
1076 if (svm->vcpu.arch.dr7 & DR7_GD) {
1077 svm->vcpu.arch.dr7 &= ~DR7_GD;
1078 kvm_update_dr7(&svm->vcpu);
1079 }
1080 } else
1081 WARN_ON(svm->vcpu.arch.exception.has_payload);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001082
Paolo Bonzini7c866632020-05-16 08:42:28 -04001083 nested_svm_vmexit(svm);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001084}
1085
Paolo Bonzini5b6724082020-05-16 08:50:35 -04001086static inline bool nested_exit_on_init(struct vcpu_svm *svm)
1087{
Babu Mogerc62e2e92020-09-11 14:28:28 -05001088 return vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_INIT);
Paolo Bonzini5b6724082020-05-16 08:50:35 -04001089}
1090
Paolo Bonzini33b22172020-04-17 10:24:18 -04001091static int svm_check_nested_events(struct kvm_vcpu *vcpu)
Joerg Roedel883b0a92020-03-24 10:41:52 +01001092{
1093 struct vcpu_svm *svm = to_svm(vcpu);
1094 bool block_nested_events =
Paolo Bonzinibd279622020-05-16 08:46:00 -04001095 kvm_event_needs_reinjection(vcpu) || svm->nested.nested_run_pending;
Paolo Bonzini5b6724082020-05-16 08:50:35 -04001096 struct kvm_lapic *apic = vcpu->arch.apic;
1097
1098 if (lapic_in_kernel(vcpu) &&
1099 test_bit(KVM_APIC_INIT, &apic->pending_events)) {
1100 if (block_nested_events)
1101 return -EBUSY;
1102 if (!nested_exit_on_init(svm))
1103 return 0;
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08001104 nested_svm_simple_vmexit(svm, SVM_EXIT_INIT);
Paolo Bonzini5b6724082020-05-16 08:50:35 -04001105 return 0;
1106 }
Joerg Roedel883b0a92020-03-24 10:41:52 +01001107
Paolo Bonzini7c866632020-05-16 08:42:28 -04001108 if (vcpu->arch.exception.pending) {
Maxim Levitsky4020da32021-04-01 17:38:14 +03001109 /*
1110 * Only a pending nested run can block a pending exception.
1111 * Otherwise an injected NMI/interrupt should either be
1112 * lost or delivered to the nested hypervisor in the EXITINTINFO
1113 * vmcb field, while delivering the pending exception.
1114 */
1115 if (svm->nested.nested_run_pending)
Paolo Bonzini7c866632020-05-16 08:42:28 -04001116 return -EBUSY;
1117 if (!nested_exit_on_exception(svm))
1118 return 0;
1119 nested_svm_inject_exception_vmexit(svm);
1120 return 0;
1121 }
1122
Paolo Bonzini221e7612020-04-23 08:13:10 -04001123 if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) {
Paolo Bonzini55714cd2020-04-23 08:17:28 -04001124 if (block_nested_events)
1125 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -04001126 if (!nested_exit_on_smi(svm))
1127 return 0;
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08001128 nested_svm_simple_vmexit(svm, SVM_EXIT_SMI);
Paolo Bonzini55714cd2020-04-23 08:17:28 -04001129 return 0;
1130 }
1131
Paolo Bonzini221e7612020-04-23 08:13:10 -04001132 if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) {
Cathy Avery9c3d3702020-04-14 16:11:06 -04001133 if (block_nested_events)
1134 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -04001135 if (!nested_exit_on_nmi(svm))
1136 return 0;
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08001137 nested_svm_simple_vmexit(svm, SVM_EXIT_NMI);
Cathy Avery9c3d3702020-04-14 16:11:06 -04001138 return 0;
1139 }
1140
Paolo Bonzini221e7612020-04-23 08:13:10 -04001141 if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) {
Joerg Roedel883b0a92020-03-24 10:41:52 +01001142 if (block_nested_events)
1143 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -04001144 if (!nested_exit_on_intr(svm))
1145 return 0;
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08001146 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
1147 nested_svm_simple_vmexit(svm, SVM_EXIT_INTR);
Joerg Roedel883b0a92020-03-24 10:41:52 +01001148 return 0;
1149 }
1150
1151 return 0;
1152}
1153
1154int nested_svm_exit_special(struct vcpu_svm *svm)
1155{
1156 u32 exit_code = svm->vmcb->control.exit_code;
1157
1158 switch (exit_code) {
1159 case SVM_EXIT_INTR:
1160 case SVM_EXIT_NMI:
Joerg Roedel883b0a92020-03-24 10:41:52 +01001161 case SVM_EXIT_NPF:
Paolo Bonzini7c866632020-05-16 08:42:28 -04001162 return NESTED_EXIT_HOST;
1163 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1164 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
1165
Cathy Avery4995a362021-01-13 07:07:52 -05001166 if (svm->vmcb01.ptr->control.intercepts[INTERCEPT_EXCEPTION] &
1167 excp_bits)
Paolo Bonzini7c866632020-05-16 08:42:28 -04001168 return NESTED_EXIT_HOST;
1169 else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02001170 svm->vcpu.arch.apf.host_apf_flags)
Paolo Bonzini7c866632020-05-16 08:42:28 -04001171 /* Trap async PF even if not shadowing */
Joerg Roedel883b0a92020-03-24 10:41:52 +01001172 return NESTED_EXIT_HOST;
1173 break;
Paolo Bonzini7c866632020-05-16 08:42:28 -04001174 }
Joerg Roedel883b0a92020-03-24 10:41:52 +01001175 default:
1176 break;
1177 }
1178
1179 return NESTED_EXIT_CONTINUE;
1180}
Paolo Bonzini33b22172020-04-17 10:24:18 -04001181
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001182static int svm_get_nested_state(struct kvm_vcpu *vcpu,
1183 struct kvm_nested_state __user *user_kvm_nested_state,
1184 u32 user_data_size)
1185{
1186 struct vcpu_svm *svm;
1187 struct kvm_nested_state kvm_state = {
1188 .flags = 0,
1189 .format = KVM_STATE_NESTED_FORMAT_SVM,
1190 .size = sizeof(kvm_state),
1191 };
1192 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1193 &user_kvm_nested_state->data.svm[0];
1194
1195 if (!vcpu)
1196 return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE;
1197
1198 svm = to_svm(vcpu);
1199
1200 if (user_data_size < kvm_state.size)
1201 goto out;
1202
1203 /* First fill in the header and copy it out. */
1204 if (is_guest_mode(vcpu)) {
Maxim Levitsky0dd16b52020-08-27 20:11:39 +03001205 kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001206 kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE;
1207 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
1208
1209 if (svm->nested.nested_run_pending)
1210 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
1211 }
1212
1213 if (gif_set(svm))
1214 kvm_state.flags |= KVM_STATE_NESTED_GIF_SET;
1215
1216 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
1217 return -EFAULT;
1218
1219 if (!is_guest_mode(vcpu))
1220 goto out;
1221
1222 /*
1223 * Copy over the full size of the VMCB rather than just the size
1224 * of the structs.
1225 */
1226 if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE))
1227 return -EFAULT;
1228 if (copy_to_user(&user_vmcb->control, &svm->nested.ctl,
1229 sizeof(user_vmcb->control)))
1230 return -EFAULT;
Cathy Avery4995a362021-01-13 07:07:52 -05001231 if (copy_to_user(&user_vmcb->save, &svm->vmcb01.ptr->save,
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001232 sizeof(user_vmcb->save)))
1233 return -EFAULT;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001234out:
1235 return kvm_state.size;
1236}
1237
1238static int svm_set_nested_state(struct kvm_vcpu *vcpu,
1239 struct kvm_nested_state __user *user_kvm_nested_state,
1240 struct kvm_nested_state *kvm_state)
1241{
1242 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001243 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1244 &user_kvm_nested_state->data.svm[0];
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001245 struct vmcb_control_area *ctl;
1246 struct vmcb_save_area *save;
1247 int ret;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001248 u32 cr0;
1249
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001250 BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >
1251 KVM_STATE_NESTED_SVM_VMCB_SIZE);
1252
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001253 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM)
1254 return -EINVAL;
1255
1256 if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE |
1257 KVM_STATE_NESTED_RUN_PENDING |
1258 KVM_STATE_NESTED_GIF_SET))
1259 return -EINVAL;
1260
1261 /*
1262 * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
1263 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
1264 */
1265 if (!(vcpu->arch.efer & EFER_SVME)) {
1266 /* GIF=1 and no guest mode are required if SVME=0. */
1267 if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
1268 return -EINVAL;
1269 }
1270
1271 /* SMM temporarily disables SVM, so we cannot be in guest mode. */
1272 if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
1273 return -EINVAL;
1274
1275 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) {
1276 svm_leave_nested(svm);
Vitaly Kuznetsovd5cd6f32020-09-14 15:37:25 +02001277 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1278 return 0;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001279 }
1280
1281 if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa))
1282 return -EINVAL;
1283 if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)
1284 return -EINVAL;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001285
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001286 ret = -ENOMEM;
Sean Christophersoneba04b22021-03-30 19:30:25 -07001287 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL_ACCOUNT);
1288 save = kzalloc(sizeof(*save), GFP_KERNEL_ACCOUNT);
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001289 if (!ctl || !save)
1290 goto out_free;
1291
1292 ret = -EFAULT;
1293 if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl)))
1294 goto out_free;
1295 if (copy_from_user(save, &user_vmcb->save, sizeof(*save)))
1296 goto out_free;
1297
1298 ret = -EINVAL;
Krish Sadhukhanee695f22021-04-12 17:56:08 -04001299 if (!nested_vmcb_check_controls(vcpu, ctl))
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001300 goto out_free;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001301
1302 /*
1303 * Processor state contains L2 state. Check that it is
Paolo Bonzinicb9b6a12021-03-31 07:35:52 -04001304 * valid for guest mode (see nested_vmcb_check_save).
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001305 */
1306 cr0 = kvm_read_cr0(vcpu);
1307 if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001308 goto out_free;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001309
1310 /*
1311 * Validate host state saved from before VMRUN (see
1312 * nested_svm_check_permissions).
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001313 */
Krish Sadhukhan6906e062020-10-06 19:06:52 +00001314 if (!(save->cr0 & X86_CR0_PG) ||
1315 !(save->cr0 & X86_CR0_PE) ||
1316 (save->rflags & X86_EFLAGS_VM) ||
Paolo Bonzini63129752021-03-02 14:40:39 -05001317 !nested_vmcb_valid_sregs(vcpu, save))
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001318 goto out_free;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001319
1320 /*
Maxim Levitskyb222b0b2021-06-07 12:01:59 +03001321 * While the nested guest CR3 is already checked and set by
1322 * KVM_SET_SREGS, it was set when nested state was yet loaded,
1323 * thus MMU might not be initialized correctly.
1324 * Set it again to fix this.
1325 */
1326
1327 ret = nested_svm_load_cr3(&svm->vcpu, vcpu->arch.cr3,
1328 nested_npt_enabled(svm), false);
1329 if (WARN_ON_ONCE(ret))
1330 goto out_free;
1331
1332
1333 /*
Cathy Avery4995a362021-01-13 07:07:52 -05001334 * All checks done, we can enter guest mode. Userspace provides
1335 * vmcb12.control, which will be combined with L1 and stored into
1336 * vmcb02, and the L1 save state which we store in vmcb01.
1337 * L2 registers if needed are moved from the current VMCB to VMCB02.
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001338 */
Maxim Levitsky81f76ad2021-01-07 11:38:52 +02001339
Maxim Levitsky9d290e12021-05-03 15:54:44 +03001340 if (is_guest_mode(vcpu))
1341 svm_leave_nested(svm);
1342 else
1343 svm->nested.vmcb02.ptr->save = svm->vmcb01.ptr->save;
1344
Maxim Levitsky063ab162021-05-04 17:39:35 +03001345 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1346
Maxim Levitsky81f76ad2021-01-07 11:38:52 +02001347 svm->nested.nested_run_pending =
1348 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
1349
Maxim Levitsky0dd16b52020-08-27 20:11:39 +03001350 svm->nested.vmcb12_gpa = kvm_state->hdr.svm.vmcb_pa;
Paolo Bonzinic08f3902020-11-17 02:51:35 -05001351
1352 svm->vmcb01.ptr->save.es = save->es;
1353 svm->vmcb01.ptr->save.cs = save->cs;
1354 svm->vmcb01.ptr->save.ss = save->ss;
1355 svm->vmcb01.ptr->save.ds = save->ds;
1356 svm->vmcb01.ptr->save.gdtr = save->gdtr;
1357 svm->vmcb01.ptr->save.idtr = save->idtr;
1358 svm->vmcb01.ptr->save.rflags = save->rflags | X86_EFLAGS_FIXED;
1359 svm->vmcb01.ptr->save.efer = save->efer;
1360 svm->vmcb01.ptr->save.cr0 = save->cr0;
1361 svm->vmcb01.ptr->save.cr3 = save->cr3;
1362 svm->vmcb01.ptr->save.cr4 = save->cr4;
1363 svm->vmcb01.ptr->save.rax = save->rax;
1364 svm->vmcb01.ptr->save.rsp = save->rsp;
1365 svm->vmcb01.ptr->save.rip = save->rip;
1366 svm->vmcb01.ptr->save.cpl = 0;
1367
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05001368 nested_load_control_from_vmcb12(svm, ctl);
Cathy Avery4995a362021-01-13 07:07:52 -05001369
1370 svm_switch_vmcb(svm, &svm->nested.vmcb02);
1371
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05001372 nested_vmcb02_prepare_control(svm);
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001373
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -04001374 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001375 ret = 0;
1376out_free:
1377 kfree(save);
1378 kfree(ctl);
1379
1380 return ret;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001381}
1382
Maxim Levitsky232f75d2021-04-01 17:18:10 +03001383static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)
1384{
1385 struct vcpu_svm *svm = to_svm(vcpu);
1386
1387 if (WARN_ON(!is_guest_mode(vcpu)))
1388 return true;
1389
Maxim Levitsky158a48e2021-06-07 12:02:03 +03001390 if (!vcpu->arch.pdptrs_from_userspace &&
1391 !nested_npt_enabled(svm) && is_pae_paging(vcpu))
Maxim Levitskyb222b0b2021-06-07 12:01:59 +03001392 /*
1393 * Reload the guest's PDPTRs since after a migration
1394 * the guest CR3 might be restored prior to setting the nested
1395 * state which can lead to a load of wrong PDPTRs.
1396 */
1397 if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3)))
1398 return false;
Maxim Levitsky232f75d2021-04-01 17:18:10 +03001399
1400 if (!nested_svm_vmrun_msrpm(svm)) {
1401 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1402 vcpu->run->internal.suberror =
1403 KVM_INTERNAL_ERROR_EMULATION;
1404 vcpu->run->internal.ndata = 0;
1405 return false;
1406 }
1407
1408 return true;
1409}
1410
Paolo Bonzini33b22172020-04-17 10:24:18 -04001411struct kvm_x86_nested_ops svm_nested_ops = {
1412 .check_events = svm_check_nested_events,
Sean Christophersoncb6a32c2021-03-02 09:45:14 -08001413 .triple_fault = nested_svm_triple_fault,
Paolo Bonzinia7d5c7c2020-09-22 07:43:14 -04001414 .get_nested_state_pages = svm_get_nested_state_pages,
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001415 .get_state = svm_get_nested_state,
1416 .set_state = svm_set_nested_state,
Paolo Bonzini33b22172020-04-17 10:24:18 -04001417};