blob: cc0985c0e09e6d79421fb876acf60859543f08be [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
32static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
33 struct x86_exception *fault)
34{
35 struct vcpu_svm *svm = to_svm(vcpu);
36
37 if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
38 /*
39 * TODO: track the cause of the nested page fault, and
40 * correctly fill in the high bits of exit_info_1.
41 */
42 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
43 svm->vmcb->control.exit_code_hi = 0;
44 svm->vmcb->control.exit_info_1 = (1ULL << 32);
45 svm->vmcb->control.exit_info_2 = fault->address;
46 }
47
48 svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
49 svm->vmcb->control.exit_info_1 |= fault->error_code;
50
Joerg Roedel883b0a92020-03-24 10:41:52 +010051 nested_svm_vmexit(svm);
52}
53
54static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
55{
56 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzinie670bf62020-05-13 13:16:12 -040057 u64 cr3 = svm->nested.ctl.nested_cr3;
Joerg Roedel883b0a92020-03-24 10:41:52 +010058 u64 pdpte;
59 int ret;
60
61 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(__sme_clr(cr3)), &pdpte,
62 offset_in_page(cr3) + index * 8, 8);
63 if (ret)
64 return 0;
65 return pdpte;
66}
67
68static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
69{
70 struct vcpu_svm *svm = to_svm(vcpu);
71
Paolo Bonzinie670bf62020-05-13 13:16:12 -040072 return svm->nested.ctl.nested_cr3;
Joerg Roedel883b0a92020-03-24 10:41:52 +010073}
74
75static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
76{
Paolo Bonzini929d1cf2020-05-19 06:18:31 -040077 struct vcpu_svm *svm = to_svm(vcpu);
78 struct vmcb *hsave = svm->nested.hsave;
79
Joerg Roedel883b0a92020-03-24 10:41:52 +010080 WARN_ON(mmu_is_nested(vcpu));
81
82 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
Vitaly Kuznetsov0f04a2a2020-07-10 16:11:49 +020083 kvm_init_shadow_npt_mmu(vcpu, X86_CR0_PG, hsave->save.cr4, hsave->save.efer,
84 svm->nested.ctl.nested_cr3);
Joerg Roedel883b0a92020-03-24 10:41:52 +010085 vcpu->arch.mmu->get_guest_pgd = nested_svm_get_tdp_cr3;
86 vcpu->arch.mmu->get_pdptr = nested_svm_get_tdp_pdptr;
87 vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit;
Joerg Roedel883b0a92020-03-24 10:41:52 +010088 reset_shadow_zero_bits_mask(vcpu, vcpu->arch.mmu);
89 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
90}
91
92static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
93{
94 vcpu->arch.mmu = &vcpu->arch.root_mmu;
95 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
96}
97
98void recalc_intercepts(struct vcpu_svm *svm)
99{
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400100 struct vmcb_control_area *c, *h, *g;
Babu Mogerc45ad722020-09-11 14:27:58 -0500101 unsigned int i;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100102
Joerg Roedel06e78522020-06-25 10:03:23 +0200103 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100104
105 if (!is_guest_mode(&svm->vcpu))
106 return;
107
108 c = &svm->vmcb->control;
109 h = &svm->nested.hsave->control;
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400110 g = &svm->nested.ctl;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100111
Babu Mogerc45ad722020-09-11 14:27:58 -0500112 for (i = 0; i < MAX_INTERCEPT; i++)
113 c->intercepts[i] = h->intercepts[i];
114
Paolo Bonzinie9fd7612020-05-13 13:28:23 -0400115 if (g->int_ctl & V_INTR_MASKING_MASK) {
Joerg Roedel883b0a92020-03-24 10:41:52 +0100116 /* We only want the cr8 intercept bits of L1 */
Babu Moger03bfeeb2020-09-11 14:28:05 -0500117 vmcb_clr_intercept(c, INTERCEPT_CR8_READ);
118 vmcb_clr_intercept(c, INTERCEPT_CR8_WRITE);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100119
120 /*
121 * Once running L2 with HF_VINTR_MASK, EFLAGS.IF does not
122 * affect any interrupt we may want to inject; therefore,
123 * interrupt window vmexits are irrelevant to L0.
124 */
Babu Mogerc62e2e92020-09-11 14:28:28 -0500125 vmcb_clr_intercept(c, INTERCEPT_VINTR);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100126 }
127
128 /* We don't want to see VMMCALLs from a nested guest */
Babu Mogerc62e2e92020-09-11 14:28:28 -0500129 vmcb_clr_intercept(c, INTERCEPT_VMMCALL);
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] |= g->intercepts[i];
Joerg Roedel883b0a92020-03-24 10:41:52 +0100133}
134
Paolo Bonzini2f675912020-05-18 15:21:22 -0400135static void copy_vmcb_control_area(struct vmcb_control_area *dst,
136 struct vmcb_control_area *from)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100137{
Babu Mogerc45ad722020-09-11 14:27:58 -0500138 unsigned int i;
139
140 for (i = 0; i < MAX_INTERCEPT; i++)
141 dst->intercepts[i] = from->intercepts[i];
142
Joerg Roedel883b0a92020-03-24 10:41:52 +0100143 dst->iopm_base_pa = from->iopm_base_pa;
144 dst->msrpm_base_pa = from->msrpm_base_pa;
145 dst->tsc_offset = from->tsc_offset;
Paolo Bonzini6c0238c2020-05-20 08:02:17 -0400146 /* asid not copied, it is handled manually for svm->vmcb. */
Joerg Roedel883b0a92020-03-24 10:41:52 +0100147 dst->tlb_ctl = from->tlb_ctl;
148 dst->int_ctl = from->int_ctl;
149 dst->int_vector = from->int_vector;
150 dst->int_state = from->int_state;
151 dst->exit_code = from->exit_code;
152 dst->exit_code_hi = from->exit_code_hi;
153 dst->exit_info_1 = from->exit_info_1;
154 dst->exit_info_2 = from->exit_info_2;
155 dst->exit_int_info = from->exit_int_info;
156 dst->exit_int_info_err = from->exit_int_info_err;
157 dst->nested_ctl = from->nested_ctl;
158 dst->event_inj = from->event_inj;
159 dst->event_inj_err = from->event_inj_err;
160 dst->nested_cr3 = from->nested_cr3;
161 dst->virt_ext = from->virt_ext;
162 dst->pause_filter_count = from->pause_filter_count;
163 dst->pause_filter_thresh = from->pause_filter_thresh;
164}
165
166static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
167{
168 /*
169 * This function merges the msr permission bitmaps of kvm and the
170 * nested vmcb. It is optimized in that it only merges the parts where
171 * the kvm msr permission bitmap may contain zero bits
172 */
173 int i;
174
Babu Mogerc62e2e92020-09-11 14:28:28 -0500175 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100176 return true;
177
178 for (i = 0; i < MSRPM_OFFSETS; i++) {
179 u32 value, p;
180 u64 offset;
181
182 if (msrpm_offsets[i] == 0xffffffff)
183 break;
184
185 p = msrpm_offsets[i];
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400186 offset = svm->nested.ctl.msrpm_base_pa + (p * 4);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100187
188 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
189 return false;
190
191 svm->nested.msrpm[p] = svm->msrpm[p] | value;
192 }
193
194 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
195
196 return true;
197}
198
Paolo Bonzinica46d732020-05-18 13:02:15 -0400199static bool nested_vmcb_check_controls(struct vmcb_control_area *control)
200{
Babu Mogerc62e2e92020-09-11 14:28:28 -0500201 if ((vmcb_is_intercept(control, INTERCEPT_VMRUN)) == 0)
Paolo Bonzinica46d732020-05-18 13:02:15 -0400202 return false;
203
204 if (control->asid == 0)
205 return false;
206
207 if ((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) &&
208 !npt_enabled)
209 return false;
210
211 return true;
212}
213
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300214static bool nested_vmcb_checks(struct vcpu_svm *svm, struct vmcb *vmcb12)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100215{
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300216 bool vmcb12_lma;
217
218 if ((vmcb12->save.efer & EFER_SVME) == 0)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100219 return false;
220
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300221 if (((vmcb12->save.cr0 & X86_CR0_CD) == 0) && (vmcb12->save.cr0 & X86_CR0_NW))
Krish Sadhukhan4f233372020-04-09 16:50:33 -0400222 return false;
223
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300224 if (!kvm_dr6_valid(vmcb12->save.dr6) || !kvm_dr7_valid(vmcb12->save.dr7))
Krish Sadhukhan1aef8162020-05-22 18:19:52 -0400225 return false;
226
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300227 vmcb12_lma = (vmcb12->save.efer & EFER_LME) && (vmcb12->save.cr0 & X86_CR0_PG);
Krish Sadhukhan761e4162020-07-08 00:39:56 +0000228
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300229 if (!vmcb12_lma) {
230 if (vmcb12->save.cr4 & X86_CR4_PAE) {
231 if (vmcb12->save.cr3 & MSR_CR3_LEGACY_PAE_RESERVED_MASK)
Krish Sadhukhan761e4162020-07-08 00:39:56 +0000232 return false;
233 } else {
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300234 if (vmcb12->save.cr3 & MSR_CR3_LEGACY_RESERVED_MASK)
Krish Sadhukhan761e4162020-07-08 00:39:56 +0000235 return false;
236 }
237 } else {
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300238 if (!(vmcb12->save.cr4 & X86_CR4_PAE) ||
239 !(vmcb12->save.cr0 & X86_CR0_PE) ||
240 (vmcb12->save.cr3 & MSR_CR3_LONG_RESERVED_MASK))
Krish Sadhukhan761e4162020-07-08 00:39:56 +0000241 return false;
242 }
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300243 if (kvm_valid_cr4(&svm->vcpu, vmcb12->save.cr4))
Krish Sadhukhan761e4162020-07-08 00:39:56 +0000244 return false;
245
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300246 return nested_vmcb_check_controls(&vmcb12->control);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100247}
248
Paolo Bonzini3e06f012020-05-13 13:07:26 -0400249static void load_nested_vmcb_control(struct vcpu_svm *svm,
250 struct vmcb_control_area *control)
251{
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400252 copy_vmcb_control_area(&svm->nested.ctl, control);
Paolo Bonzini3e06f012020-05-13 13:07:26 -0400253
Paolo Bonzinicc440cd2020-05-13 13:36:32 -0400254 /* Copy it here because nested_svm_check_controls will check it. */
255 svm->nested.ctl.asid = control->asid;
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400256 svm->nested.ctl.msrpm_base_pa &= ~0x0fffULL;
257 svm->nested.ctl.iopm_base_pa &= ~0x0fffULL;
Paolo Bonzini3e06f012020-05-13 13:07:26 -0400258}
259
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400260/*
261 * Synchronize fields that are written by the processor, so that
262 * they can be copied back into the nested_vmcb.
263 */
264void sync_nested_vmcb_control(struct vcpu_svm *svm)
265{
266 u32 mask;
267 svm->nested.ctl.event_inj = svm->vmcb->control.event_inj;
268 svm->nested.ctl.event_inj_err = svm->vmcb->control.event_inj_err;
269
270 /* Only a few fields of int_ctl are written by the processor. */
271 mask = V_IRQ_MASK | V_TPR_MASK;
272 if (!(svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) &&
Joerg Roedela284ba52020-06-25 10:03:24 +0200273 svm_is_intercept(svm, INTERCEPT_VINTR)) {
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400274 /*
275 * In order to request an interrupt window, L0 is usurping
276 * svm->vmcb->control.int_ctl and possibly setting V_IRQ
277 * even if it was clear in L1's VMCB. Restoring it would be
278 * wrong. However, in this case V_IRQ will remain true until
279 * interrupt_window_interception calls svm_clear_vintr and
280 * restores int_ctl. We can just leave it aside.
281 */
282 mask &= ~V_IRQ_MASK;
283 }
284 svm->nested.ctl.int_ctl &= ~mask;
285 svm->nested.ctl.int_ctl |= svm->vmcb->control.int_ctl & mask;
286}
287
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400288/*
289 * Transfer any event that L0 or L1 wanted to inject into L2 to
290 * EXIT_INT_INFO.
291 */
292static void nested_vmcb_save_pending_event(struct vcpu_svm *svm,
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300293 struct vmcb *vmcb12)
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400294{
295 struct kvm_vcpu *vcpu = &svm->vcpu;
296 u32 exit_int_info = 0;
297 unsigned int nr;
298
299 if (vcpu->arch.exception.injected) {
300 nr = vcpu->arch.exception.nr;
301 exit_int_info = nr | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT;
302
303 if (vcpu->arch.exception.has_error_code) {
304 exit_int_info |= SVM_EVTINJ_VALID_ERR;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300305 vmcb12->control.exit_int_info_err =
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400306 vcpu->arch.exception.error_code;
307 }
308
309 } else if (vcpu->arch.nmi_injected) {
310 exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
311
312 } else if (vcpu->arch.interrupt.injected) {
313 nr = vcpu->arch.interrupt.nr;
314 exit_int_info = nr | SVM_EVTINJ_VALID;
315
316 if (vcpu->arch.interrupt.soft)
317 exit_int_info |= SVM_EVTINJ_TYPE_SOFT;
318 else
319 exit_int_info |= SVM_EVTINJ_TYPE_INTR;
320 }
321
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300322 vmcb12->control.exit_int_info = exit_int_info;
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400323}
324
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200325static inline bool nested_npt_enabled(struct vcpu_svm *svm)
326{
327 return svm->nested.ctl.nested_ctl & SVM_NESTED_CTL_NP_ENABLE;
328}
329
330/*
Vitaly Kuznetsovd82aaef2020-07-10 16:11:56 +0200331 * Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true
332 * if we are emulating VM-Entry into a guest with NPT enabled.
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200333 */
334static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
335 bool nested_npt)
336{
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200337 if (cr3 & rsvd_bits(cpuid_maxphyaddr(vcpu), 63))
338 return -EINVAL;
339
340 if (!nested_npt && is_pae_paging(vcpu) &&
341 (cr3 != kvm_read_cr3(vcpu) || pdptrs_changed(vcpu))) {
342 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
343 return -EINVAL;
344 }
345
346 /*
347 * TODO: optimize unconditional TLB flush/MMU sync here and in
348 * kvm_init_shadow_npt_mmu().
349 */
350 if (!nested_npt)
351 kvm_mmu_new_pgd(vcpu, cr3, false, false);
352
353 vcpu->arch.cr3 = cr3;
354 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
355
356 kvm_init_mmu(vcpu, false);
357
358 return 0;
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200359}
360
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300361static void nested_prepare_vmcb_save(struct vcpu_svm *svm, struct vmcb *vmcb12)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100362{
Joerg Roedel883b0a92020-03-24 10:41:52 +0100363 /* Load the nested guest state */
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300364 svm->vmcb->save.es = vmcb12->save.es;
365 svm->vmcb->save.cs = vmcb12->save.cs;
366 svm->vmcb->save.ss = vmcb12->save.ss;
367 svm->vmcb->save.ds = vmcb12->save.ds;
368 svm->vmcb->save.gdtr = vmcb12->save.gdtr;
369 svm->vmcb->save.idtr = vmcb12->save.idtr;
370 kvm_set_rflags(&svm->vcpu, vmcb12->save.rflags);
371 svm_set_efer(&svm->vcpu, vmcb12->save.efer);
372 svm_set_cr0(&svm->vcpu, vmcb12->save.cr0);
373 svm_set_cr4(&svm->vcpu, vmcb12->save.cr4);
374 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = vmcb12->save.cr2;
375 kvm_rax_write(&svm->vcpu, vmcb12->save.rax);
376 kvm_rsp_write(&svm->vcpu, vmcb12->save.rsp);
377 kvm_rip_write(&svm->vcpu, vmcb12->save.rip);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100378
379 /* In case we don't even reach vcpu_run, the fields are not updated */
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300380 svm->vmcb->save.rax = vmcb12->save.rax;
381 svm->vmcb->save.rsp = vmcb12->save.rsp;
382 svm->vmcb->save.rip = vmcb12->save.rip;
383 svm->vmcb->save.dr7 = vmcb12->save.dr7;
384 svm->vcpu.arch.dr6 = vmcb12->save.dr6;
385 svm->vmcb->save.cpl = vmcb12->save.cpl;
Paolo Bonzinif241d712020-05-18 10:56:43 -0400386}
Joerg Roedel883b0a92020-03-24 10:41:52 +0100387
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400388static void nested_prepare_vmcb_control(struct vcpu_svm *svm)
Paolo Bonzinif241d712020-05-18 10:56:43 -0400389{
Paolo Bonzini91b71302020-05-22 12:28:52 -0400390 const u32 mask = V_INTR_MASKING_MASK | V_GIF_ENABLE_MASK | V_GIF_MASK;
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200391
392 if (nested_npt_enabled(svm))
Paolo Bonzini69cb8772020-05-22 05:27:46 -0400393 nested_svm_init_mmu_context(&svm->vcpu);
394
Paolo Bonzini18fc6c52020-05-18 11:07:08 -0400395 svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset =
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400396 svm->vcpu.arch.l1_tsc_offset + svm->nested.ctl.tsc_offset;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100397
Paolo Bonzini91b71302020-05-22 12:28:52 -0400398 svm->vmcb->control.int_ctl =
399 (svm->nested.ctl.int_ctl & ~mask) |
400 (svm->nested.hsave->control.int_ctl & mask);
401
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400402 svm->vmcb->control.virt_ext = svm->nested.ctl.virt_ext;
403 svm->vmcb->control.int_vector = svm->nested.ctl.int_vector;
404 svm->vmcb->control.int_state = svm->nested.ctl.int_state;
405 svm->vmcb->control.event_inj = svm->nested.ctl.event_inj;
406 svm->vmcb->control.event_inj_err = svm->nested.ctl.event_inj_err;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100407
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400408 svm->vmcb->control.pause_filter_count = svm->nested.ctl.pause_filter_count;
409 svm->vmcb->control.pause_filter_thresh = svm->nested.ctl.pause_filter_thresh;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100410
Joerg Roedel883b0a92020-03-24 10:41:52 +0100411 /* Enter Guest-Mode */
412 enter_guest_mode(&svm->vcpu);
413
414 /*
415 * Merge guest and host intercepts - must be called with vcpu in
416 * guest-mode to take affect here
417 */
418 recalc_intercepts(svm);
419
Joerg Roedel06e78522020-06-25 10:03:23 +0200420 vmcb_mark_all_dirty(svm->vmcb);
Paolo Bonzinif241d712020-05-18 10:56:43 -0400421}
422
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300423int enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb12_gpa,
424 struct vmcb *vmcb12)
Paolo Bonzinif241d712020-05-18 10:56:43 -0400425{
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200426 int ret;
427
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300428 svm->nested.vmcb12_gpa = vmcb12_gpa;
429 load_nested_vmcb_control(svm, &vmcb12->control);
430 nested_prepare_vmcb_save(svm, vmcb12);
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400431 nested_prepare_vmcb_control(svm);
Paolo Bonzinif241d712020-05-18 10:56:43 -0400432
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300433 ret = nested_svm_load_cr3(&svm->vcpu, vmcb12->save.cr3,
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200434 nested_npt_enabled(svm));
435 if (ret)
436 return ret;
437
Paolo Bonziniffdf7f92020-05-22 12:18:27 -0400438 svm_set_gif(svm, true);
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200439
440 return 0;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100441}
442
443int nested_svm_vmrun(struct vcpu_svm *svm)
444{
445 int ret;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300446 struct vmcb *vmcb12;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100447 struct vmcb *hsave = svm->nested.hsave;
448 struct vmcb *vmcb = svm->vmcb;
449 struct kvm_host_map map;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300450 u64 vmcb12_gpa;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100451
Paolo Bonzini7c67f5462020-04-23 10:52:48 -0400452 if (is_smm(&svm->vcpu)) {
453 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
454 return 1;
455 }
Joerg Roedel883b0a92020-03-24 10:41:52 +0100456
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300457 vmcb12_gpa = svm->vmcb->save.rax;
458 ret = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(vmcb12_gpa), &map);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100459 if (ret == -EINVAL) {
460 kvm_inject_gp(&svm->vcpu, 0);
461 return 1;
462 } else if (ret) {
463 return kvm_skip_emulated_instruction(&svm->vcpu);
464 }
465
466 ret = kvm_skip_emulated_instruction(&svm->vcpu);
467
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300468 vmcb12 = map.hva;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100469
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300470 if (!nested_vmcb_checks(svm, vmcb12)) {
471 vmcb12->control.exit_code = SVM_EXIT_ERR;
472 vmcb12->control.exit_code_hi = 0;
473 vmcb12->control.exit_info_1 = 0;
474 vmcb12->control.exit_info_2 = 0;
Paolo Bonzini69c9dfa2020-05-13 12:57:26 -0400475 goto out;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100476 }
477
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300478 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb12_gpa,
479 vmcb12->save.rip,
480 vmcb12->control.int_ctl,
481 vmcb12->control.event_inj,
482 vmcb12->control.nested_ctl);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100483
Babu Moger03bfeeb2020-09-11 14:28:05 -0500484 trace_kvm_nested_intercepts(vmcb12->control.intercepts[INTERCEPT_CR] & 0xffff,
485 vmcb12->control.intercepts[INTERCEPT_CR] >> 16,
Babu Moger9780d512020-09-11 14:28:20 -0500486 vmcb12->control.intercepts[INTERCEPT_EXCEPTION],
Babu Mogerc62e2e92020-09-11 14:28:28 -0500487 vmcb12->control.intercepts[INTERCEPT_WORD3],
488 vmcb12->control.intercepts[INTERCEPT_WORD4]);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100489
490 /* Clear internal status */
491 kvm_clear_exception_queue(&svm->vcpu);
492 kvm_clear_interrupt_queue(&svm->vcpu);
493
494 /*
495 * Save the old vmcb, so we don't need to pick what we save, but can
496 * restore everything when a VMEXIT occurs
497 */
498 hsave->save.es = vmcb->save.es;
499 hsave->save.cs = vmcb->save.cs;
500 hsave->save.ss = vmcb->save.ss;
501 hsave->save.ds = vmcb->save.ds;
502 hsave->save.gdtr = vmcb->save.gdtr;
503 hsave->save.idtr = vmcb->save.idtr;
504 hsave->save.efer = svm->vcpu.arch.efer;
505 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
506 hsave->save.cr4 = svm->vcpu.arch.cr4;
507 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
508 hsave->save.rip = kvm_rip_read(&svm->vcpu);
509 hsave->save.rsp = vmcb->save.rsp;
510 hsave->save.rax = vmcb->save.rax;
511 if (npt_enabled)
512 hsave->save.cr3 = vmcb->save.cr3;
513 else
514 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
515
Paolo Bonzini2f675912020-05-18 15:21:22 -0400516 copy_vmcb_control_area(&hsave->control, &vmcb->control);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100517
Paolo Bonzinif74f9412020-04-23 13:22:27 -0400518 svm->nested.nested_run_pending = 1;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100519
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300520 if (enter_svm_guest_mode(svm, vmcb12_gpa, vmcb12))
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200521 goto out_exit_err;
Vitaly Kuznetsovebdb3db2020-07-10 16:11:51 +0200522
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200523 if (nested_svm_vmrun_msrpm(svm))
524 goto out;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100525
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200526out_exit_err:
527 svm->nested.nested_run_pending = 0;
528
529 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
530 svm->vmcb->control.exit_code_hi = 0;
531 svm->vmcb->control.exit_info_1 = 0;
532 svm->vmcb->control.exit_info_2 = 0;
533
534 nested_svm_vmexit(svm);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100535
Paolo Bonzini69c9dfa2020-05-13 12:57:26 -0400536out:
537 kvm_vcpu_unmap(&svm->vcpu, &map, true);
538
Joerg Roedel883b0a92020-03-24 10:41:52 +0100539 return ret;
540}
541
542void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
543{
544 to_vmcb->save.fs = from_vmcb->save.fs;
545 to_vmcb->save.gs = from_vmcb->save.gs;
546 to_vmcb->save.tr = from_vmcb->save.tr;
547 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
548 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
549 to_vmcb->save.star = from_vmcb->save.star;
550 to_vmcb->save.lstar = from_vmcb->save.lstar;
551 to_vmcb->save.cstar = from_vmcb->save.cstar;
552 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
553 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
554 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
555 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
556}
557
558int nested_svm_vmexit(struct vcpu_svm *svm)
559{
560 int rc;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300561 struct vmcb *vmcb12;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100562 struct vmcb *hsave = svm->nested.hsave;
563 struct vmcb *vmcb = svm->vmcb;
564 struct kvm_host_map map;
565
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300566 rc = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(svm->nested.vmcb12_gpa), &map);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100567 if (rc) {
568 if (rc == -EINVAL)
569 kvm_inject_gp(&svm->vcpu, 0);
570 return 1;
571 }
572
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300573 vmcb12 = map.hva;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100574
575 /* Exit Guest-Mode */
576 leave_guest_mode(&svm->vcpu);
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300577 svm->nested.vmcb12_gpa = 0;
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400578 WARN_ON_ONCE(svm->nested.nested_run_pending);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100579
Paolo Bonzini38c0b192020-04-23 13:13:09 -0400580 /* in case we halted in L2 */
581 svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE;
582
Joerg Roedel883b0a92020-03-24 10:41:52 +0100583 /* Give the current vmcb to the guest */
Joerg Roedel883b0a92020-03-24 10:41:52 +0100584
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300585 vmcb12->save.es = vmcb->save.es;
586 vmcb12->save.cs = vmcb->save.cs;
587 vmcb12->save.ss = vmcb->save.ss;
588 vmcb12->save.ds = vmcb->save.ds;
589 vmcb12->save.gdtr = vmcb->save.gdtr;
590 vmcb12->save.idtr = vmcb->save.idtr;
591 vmcb12->save.efer = svm->vcpu.arch.efer;
592 vmcb12->save.cr0 = kvm_read_cr0(&svm->vcpu);
593 vmcb12->save.cr3 = kvm_read_cr3(&svm->vcpu);
594 vmcb12->save.cr2 = vmcb->save.cr2;
595 vmcb12->save.cr4 = svm->vcpu.arch.cr4;
596 vmcb12->save.rflags = kvm_get_rflags(&svm->vcpu);
597 vmcb12->save.rip = kvm_rip_read(&svm->vcpu);
598 vmcb12->save.rsp = kvm_rsp_read(&svm->vcpu);
599 vmcb12->save.rax = kvm_rax_read(&svm->vcpu);
600 vmcb12->save.dr7 = vmcb->save.dr7;
601 vmcb12->save.dr6 = svm->vcpu.arch.dr6;
602 vmcb12->save.cpl = vmcb->save.cpl;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100603
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300604 vmcb12->control.int_state = vmcb->control.int_state;
605 vmcb12->control.exit_code = vmcb->control.exit_code;
606 vmcb12->control.exit_code_hi = vmcb->control.exit_code_hi;
607 vmcb12->control.exit_info_1 = vmcb->control.exit_info_1;
608 vmcb12->control.exit_info_2 = vmcb->control.exit_info_2;
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400609
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300610 if (vmcb12->control.exit_code != SVM_EXIT_ERR)
611 nested_vmcb_save_pending_event(svm, vmcb12);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100612
613 if (svm->nrips_enabled)
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300614 vmcb12->control.next_rip = vmcb->control.next_rip;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100615
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300616 vmcb12->control.int_ctl = svm->nested.ctl.int_ctl;
617 vmcb12->control.tlb_ctl = svm->nested.ctl.tlb_ctl;
618 vmcb12->control.event_inj = svm->nested.ctl.event_inj;
619 vmcb12->control.event_inj_err = svm->nested.ctl.event_inj_err;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100620
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300621 vmcb12->control.pause_filter_count =
Joerg Roedel883b0a92020-03-24 10:41:52 +0100622 svm->vmcb->control.pause_filter_count;
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300623 vmcb12->control.pause_filter_thresh =
Joerg Roedel883b0a92020-03-24 10:41:52 +0100624 svm->vmcb->control.pause_filter_thresh;
625
Joerg Roedel883b0a92020-03-24 10:41:52 +0100626 /* Restore the original control entries */
Paolo Bonzini2f675912020-05-18 15:21:22 -0400627 copy_vmcb_control_area(&vmcb->control, &hsave->control);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100628
Maxim Levitsky98837642020-08-27 19:27:18 +0300629 /* On vmexit the GIF is set to false */
630 svm_set_gif(svm, false);
631
Paolo Bonzini18fc6c52020-05-18 11:07:08 -0400632 svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset =
633 svm->vcpu.arch.l1_tsc_offset;
634
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400635 svm->nested.ctl.nested_cr3 = 0;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100636
637 /* Restore selected save entries */
638 svm->vmcb->save.es = hsave->save.es;
639 svm->vmcb->save.cs = hsave->save.cs;
640 svm->vmcb->save.ss = hsave->save.ss;
641 svm->vmcb->save.ds = hsave->save.ds;
642 svm->vmcb->save.gdtr = hsave->save.gdtr;
643 svm->vmcb->save.idtr = hsave->save.idtr;
644 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
645 svm_set_efer(&svm->vcpu, hsave->save.efer);
646 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
647 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100648 kvm_rax_write(&svm->vcpu, hsave->save.rax);
649 kvm_rsp_write(&svm->vcpu, hsave->save.rsp);
650 kvm_rip_write(&svm->vcpu, hsave->save.rip);
651 svm->vmcb->save.dr7 = 0;
652 svm->vmcb->save.cpl = 0;
653 svm->vmcb->control.exit_int_info = 0;
654
Joerg Roedel06e78522020-06-25 10:03:23 +0200655 vmcb_mark_all_dirty(svm->vmcb);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100656
Maxim Levitsky0dd16b52020-08-27 20:11:39 +0300657 trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code,
658 vmcb12->control.exit_info_1,
659 vmcb12->control.exit_info_2,
660 vmcb12->control.exit_int_info,
661 vmcb12->control.exit_int_info_err,
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400662 KVM_ISA_SVM);
663
Joerg Roedel883b0a92020-03-24 10:41:52 +0100664 kvm_vcpu_unmap(&svm->vcpu, &map, true);
665
666 nested_svm_uninit_mmu_context(&svm->vcpu);
Vitaly Kuznetsovbf7dea42020-07-10 16:11:54 +0200667
Vitaly Kuznetsovd82aaef2020-07-10 16:11:56 +0200668 rc = nested_svm_load_cr3(&svm->vcpu, hsave->save.cr3, false);
669 if (rc)
670 return 1;
Vitaly Kuznetsovbf7dea42020-07-10 16:11:54 +0200671
Vitaly Kuznetsovd82aaef2020-07-10 16:11:56 +0200672 if (npt_enabled)
673 svm->vmcb->save.cr3 = hsave->save.cr3;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100674
675 /*
676 * Drop what we picked up for L2 via svm_complete_interrupts() so it
677 * doesn't end up in L1.
678 */
679 svm->vcpu.arch.nmi_injected = false;
680 kvm_clear_exception_queue(&svm->vcpu);
681 kvm_clear_interrupt_queue(&svm->vcpu);
682
683 return 0;
684}
685
Paolo Bonzinic513f482020-05-18 13:08:37 -0400686/*
687 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
688 */
689void svm_leave_nested(struct vcpu_svm *svm)
690{
691 if (is_guest_mode(&svm->vcpu)) {
692 struct vmcb *hsave = svm->nested.hsave;
693 struct vmcb *vmcb = svm->vmcb;
694
695 svm->nested.nested_run_pending = 0;
696 leave_guest_mode(&svm->vcpu);
697 copy_vmcb_control_area(&vmcb->control, &hsave->control);
698 nested_svm_uninit_mmu_context(&svm->vcpu);
699 }
700}
701
Joerg Roedel883b0a92020-03-24 10:41:52 +0100702static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
703{
704 u32 offset, msr, value;
705 int write, mask;
706
Babu Mogerc62e2e92020-09-11 14:28:28 -0500707 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100708 return NESTED_EXIT_HOST;
709
710 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
711 offset = svm_msrpm_offset(msr);
712 write = svm->vmcb->control.exit_info_1 & 1;
713 mask = 1 << ((2 * (msr & 0xf)) + write);
714
715 if (offset == MSR_INVALID)
716 return NESTED_EXIT_DONE;
717
718 /* Offset is in 32 bit units but need in 8 bit units */
719 offset *= 4;
720
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400721 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100722 return NESTED_EXIT_DONE;
723
724 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
725}
726
Joerg Roedel883b0a92020-03-24 10:41:52 +0100727static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
728{
729 unsigned port, size, iopm_len;
730 u16 val, mask;
731 u8 start_bit;
732 u64 gpa;
733
Babu Mogerc62e2e92020-09-11 14:28:28 -0500734 if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_IOIO_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100735 return NESTED_EXIT_HOST;
736
737 port = svm->vmcb->control.exit_info_1 >> 16;
738 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
739 SVM_IOIO_SIZE_SHIFT;
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400740 gpa = svm->nested.ctl.iopm_base_pa + (port / 8);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100741 start_bit = port % 8;
742 iopm_len = (start_bit + size > 8) ? 2 : 1;
743 mask = (0xf >> (4 - size)) << start_bit;
744 val = 0;
745
746 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
747 return NESTED_EXIT_DONE;
748
749 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
750}
751
752static int nested_svm_intercept(struct vcpu_svm *svm)
753{
754 u32 exit_code = svm->vmcb->control.exit_code;
755 int vmexit = NESTED_EXIT_HOST;
756
757 switch (exit_code) {
758 case SVM_EXIT_MSR:
759 vmexit = nested_svm_exit_handled_msr(svm);
760 break;
761 case SVM_EXIT_IOIO:
762 vmexit = nested_svm_intercept_ioio(svm);
763 break;
764 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
Babu Moger03bfeeb2020-09-11 14:28:05 -0500765 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100766 vmexit = NESTED_EXIT_DONE;
767 break;
768 }
769 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
Babu Moger30abaa882020-09-11 14:28:12 -0500770 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100771 vmexit = NESTED_EXIT_DONE;
772 break;
773 }
774 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
Paolo Bonzini7c866632020-05-16 08:42:28 -0400775 /*
776 * Host-intercepted exceptions have been checked already in
777 * nested_svm_exit_special. There is nothing to do here,
778 * the vmexit is injected by svm_check_nested_events.
779 */
780 vmexit = NESTED_EXIT_DONE;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100781 break;
782 }
783 case SVM_EXIT_ERR: {
784 vmexit = NESTED_EXIT_DONE;
785 break;
786 }
787 default: {
Babu Mogerc62e2e92020-09-11 14:28:28 -0500788 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100789 vmexit = NESTED_EXIT_DONE;
790 }
791 }
792
793 return vmexit;
794}
795
796int nested_svm_exit_handled(struct vcpu_svm *svm)
797{
798 int vmexit;
799
800 vmexit = nested_svm_intercept(svm);
801
802 if (vmexit == NESTED_EXIT_DONE)
803 nested_svm_vmexit(svm);
804
805 return vmexit;
806}
807
808int nested_svm_check_permissions(struct vcpu_svm *svm)
809{
810 if (!(svm->vcpu.arch.efer & EFER_SVME) ||
811 !is_paging(&svm->vcpu)) {
812 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
813 return 1;
814 }
815
816 if (svm->vmcb->save.cpl) {
817 kvm_inject_gp(&svm->vcpu, 0);
818 return 1;
819 }
820
821 return 0;
822}
823
Paolo Bonzini7c866632020-05-16 08:42:28 -0400824static bool nested_exit_on_exception(struct vcpu_svm *svm)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100825{
Paolo Bonzini7c866632020-05-16 08:42:28 -0400826 unsigned int nr = svm->vcpu.arch.exception.nr;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100827
Babu Moger9780d512020-09-11 14:28:20 -0500828 return (svm->nested.ctl.intercepts[INTERCEPT_EXCEPTION] & BIT(nr));
Paolo Bonzini7c866632020-05-16 08:42:28 -0400829}
Joerg Roedel883b0a92020-03-24 10:41:52 +0100830
Paolo Bonzini7c866632020-05-16 08:42:28 -0400831static void nested_svm_inject_exception_vmexit(struct vcpu_svm *svm)
832{
833 unsigned int nr = svm->vcpu.arch.exception.nr;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100834
835 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
836 svm->vmcb->control.exit_code_hi = 0;
Paolo Bonzini7c866632020-05-16 08:42:28 -0400837
838 if (svm->vcpu.arch.exception.has_error_code)
839 svm->vmcb->control.exit_info_1 = svm->vcpu.arch.exception.error_code;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100840
841 /*
842 * EXITINFO2 is undefined for all exception intercepts other
843 * than #PF.
844 */
Paolo Bonzini7c866632020-05-16 08:42:28 -0400845 if (nr == PF_VECTOR) {
846 if (svm->vcpu.arch.exception.nested_apf)
847 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
848 else if (svm->vcpu.arch.exception.has_payload)
849 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload;
850 else
851 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
852 } else if (nr == DB_VECTOR) {
853 /* See inject_pending_event. */
854 kvm_deliver_exception_payload(&svm->vcpu);
855 if (svm->vcpu.arch.dr7 & DR7_GD) {
856 svm->vcpu.arch.dr7 &= ~DR7_GD;
857 kvm_update_dr7(&svm->vcpu);
858 }
859 } else
860 WARN_ON(svm->vcpu.arch.exception.has_payload);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100861
Paolo Bonzini7c866632020-05-16 08:42:28 -0400862 nested_svm_vmexit(svm);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100863}
864
Paolo Bonzini55714cd2020-04-23 08:17:28 -0400865static void nested_svm_smi(struct vcpu_svm *svm)
866{
867 svm->vmcb->control.exit_code = SVM_EXIT_SMI;
868 svm->vmcb->control.exit_info_1 = 0;
869 svm->vmcb->control.exit_info_2 = 0;
870
871 nested_svm_vmexit(svm);
872}
873
Cathy Avery9c3d3702020-04-14 16:11:06 -0400874static void nested_svm_nmi(struct vcpu_svm *svm)
875{
876 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
877 svm->vmcb->control.exit_info_1 = 0;
878 svm->vmcb->control.exit_info_2 = 0;
879
880 nested_svm_vmexit(svm);
881}
882
Joerg Roedel883b0a92020-03-24 10:41:52 +0100883static void nested_svm_intr(struct vcpu_svm *svm)
884{
Paolo Bonzini6e085cb2020-04-23 13:15:33 -0400885 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
886
Joerg Roedel883b0a92020-03-24 10:41:52 +0100887 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
888 svm->vmcb->control.exit_info_1 = 0;
889 svm->vmcb->control.exit_info_2 = 0;
890
Paolo Bonzini6e085cb2020-04-23 13:15:33 -0400891 nested_svm_vmexit(svm);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100892}
893
Paolo Bonzini5b6724082020-05-16 08:50:35 -0400894static inline bool nested_exit_on_init(struct vcpu_svm *svm)
895{
Babu Mogerc62e2e92020-09-11 14:28:28 -0500896 return vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_INIT);
Paolo Bonzini5b6724082020-05-16 08:50:35 -0400897}
898
899static void nested_svm_init(struct vcpu_svm *svm)
900{
901 svm->vmcb->control.exit_code = SVM_EXIT_INIT;
902 svm->vmcb->control.exit_info_1 = 0;
903 svm->vmcb->control.exit_info_2 = 0;
904
905 nested_svm_vmexit(svm);
906}
907
908
Paolo Bonzini33b22172020-04-17 10:24:18 -0400909static int svm_check_nested_events(struct kvm_vcpu *vcpu)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100910{
911 struct vcpu_svm *svm = to_svm(vcpu);
912 bool block_nested_events =
Paolo Bonzinibd279622020-05-16 08:46:00 -0400913 kvm_event_needs_reinjection(vcpu) || svm->nested.nested_run_pending;
Paolo Bonzini5b6724082020-05-16 08:50:35 -0400914 struct kvm_lapic *apic = vcpu->arch.apic;
915
916 if (lapic_in_kernel(vcpu) &&
917 test_bit(KVM_APIC_INIT, &apic->pending_events)) {
918 if (block_nested_events)
919 return -EBUSY;
920 if (!nested_exit_on_init(svm))
921 return 0;
922 nested_svm_init(svm);
923 return 0;
924 }
Joerg Roedel883b0a92020-03-24 10:41:52 +0100925
Paolo Bonzini7c866632020-05-16 08:42:28 -0400926 if (vcpu->arch.exception.pending) {
927 if (block_nested_events)
928 return -EBUSY;
929 if (!nested_exit_on_exception(svm))
930 return 0;
931 nested_svm_inject_exception_vmexit(svm);
932 return 0;
933 }
934
Paolo Bonzini221e7612020-04-23 08:13:10 -0400935 if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) {
Paolo Bonzini55714cd2020-04-23 08:17:28 -0400936 if (block_nested_events)
937 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -0400938 if (!nested_exit_on_smi(svm))
939 return 0;
Paolo Bonzini55714cd2020-04-23 08:17:28 -0400940 nested_svm_smi(svm);
941 return 0;
942 }
943
Paolo Bonzini221e7612020-04-23 08:13:10 -0400944 if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) {
Cathy Avery9c3d3702020-04-14 16:11:06 -0400945 if (block_nested_events)
946 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -0400947 if (!nested_exit_on_nmi(svm))
948 return 0;
Cathy Avery9c3d3702020-04-14 16:11:06 -0400949 nested_svm_nmi(svm);
950 return 0;
951 }
952
Paolo Bonzini221e7612020-04-23 08:13:10 -0400953 if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) {
Joerg Roedel883b0a92020-03-24 10:41:52 +0100954 if (block_nested_events)
955 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -0400956 if (!nested_exit_on_intr(svm))
957 return 0;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100958 nested_svm_intr(svm);
959 return 0;
960 }
961
962 return 0;
963}
964
965int nested_svm_exit_special(struct vcpu_svm *svm)
966{
967 u32 exit_code = svm->vmcb->control.exit_code;
968
969 switch (exit_code) {
970 case SVM_EXIT_INTR:
971 case SVM_EXIT_NMI:
Joerg Roedel883b0a92020-03-24 10:41:52 +0100972 case SVM_EXIT_NPF:
Paolo Bonzini7c866632020-05-16 08:42:28 -0400973 return NESTED_EXIT_HOST;
974 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
975 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
976
Babu Moger9780d512020-09-11 14:28:20 -0500977 if (get_host_vmcb(svm)->control.intercepts[INTERCEPT_EXCEPTION] &
978 excp_bits)
Paolo Bonzini7c866632020-05-16 08:42:28 -0400979 return NESTED_EXIT_HOST;
980 else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +0200981 svm->vcpu.arch.apf.host_apf_flags)
Paolo Bonzini7c866632020-05-16 08:42:28 -0400982 /* Trap async PF even if not shadowing */
Joerg Roedel883b0a92020-03-24 10:41:52 +0100983 return NESTED_EXIT_HOST;
984 break;
Paolo Bonzini7c866632020-05-16 08:42:28 -0400985 }
Joerg Roedel883b0a92020-03-24 10:41:52 +0100986 default:
987 break;
988 }
989
990 return NESTED_EXIT_CONTINUE;
991}
Paolo Bonzini33b22172020-04-17 10:24:18 -0400992
Paolo Bonzinicc440cd2020-05-13 13:36:32 -0400993static int svm_get_nested_state(struct kvm_vcpu *vcpu,
994 struct kvm_nested_state __user *user_kvm_nested_state,
995 u32 user_data_size)
996{
997 struct vcpu_svm *svm;
998 struct kvm_nested_state kvm_state = {
999 .flags = 0,
1000 .format = KVM_STATE_NESTED_FORMAT_SVM,
1001 .size = sizeof(kvm_state),
1002 };
1003 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1004 &user_kvm_nested_state->data.svm[0];
1005
1006 if (!vcpu)
1007 return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE;
1008
1009 svm = to_svm(vcpu);
1010
1011 if (user_data_size < kvm_state.size)
1012 goto out;
1013
1014 /* First fill in the header and copy it out. */
1015 if (is_guest_mode(vcpu)) {
Maxim Levitsky0dd16b52020-08-27 20:11:39 +03001016 kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001017 kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE;
1018 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
1019
1020 if (svm->nested.nested_run_pending)
1021 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
1022 }
1023
1024 if (gif_set(svm))
1025 kvm_state.flags |= KVM_STATE_NESTED_GIF_SET;
1026
1027 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
1028 return -EFAULT;
1029
1030 if (!is_guest_mode(vcpu))
1031 goto out;
1032
1033 /*
1034 * Copy over the full size of the VMCB rather than just the size
1035 * of the structs.
1036 */
1037 if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE))
1038 return -EFAULT;
1039 if (copy_to_user(&user_vmcb->control, &svm->nested.ctl,
1040 sizeof(user_vmcb->control)))
1041 return -EFAULT;
1042 if (copy_to_user(&user_vmcb->save, &svm->nested.hsave->save,
1043 sizeof(user_vmcb->save)))
1044 return -EFAULT;
1045
1046out:
1047 return kvm_state.size;
1048}
1049
1050static int svm_set_nested_state(struct kvm_vcpu *vcpu,
1051 struct kvm_nested_state __user *user_kvm_nested_state,
1052 struct kvm_nested_state *kvm_state)
1053{
1054 struct vcpu_svm *svm = to_svm(vcpu);
1055 struct vmcb *hsave = svm->nested.hsave;
1056 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1057 &user_kvm_nested_state->data.svm[0];
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001058 struct vmcb_control_area *ctl;
1059 struct vmcb_save_area *save;
1060 int ret;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001061 u32 cr0;
1062
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001063 BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >
1064 KVM_STATE_NESTED_SVM_VMCB_SIZE);
1065
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001066 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM)
1067 return -EINVAL;
1068
1069 if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE |
1070 KVM_STATE_NESTED_RUN_PENDING |
1071 KVM_STATE_NESTED_GIF_SET))
1072 return -EINVAL;
1073
1074 /*
1075 * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
1076 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
1077 */
1078 if (!(vcpu->arch.efer & EFER_SVME)) {
1079 /* GIF=1 and no guest mode are required if SVME=0. */
1080 if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
1081 return -EINVAL;
1082 }
1083
1084 /* SMM temporarily disables SVM, so we cannot be in guest mode. */
1085 if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
1086 return -EINVAL;
1087
1088 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) {
1089 svm_leave_nested(svm);
Vitaly Kuznetsovd5cd6f32020-09-14 15:37:25 +02001090 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1091 return 0;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001092 }
1093
1094 if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa))
1095 return -EINVAL;
1096 if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)
1097 return -EINVAL;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001098
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001099 ret = -ENOMEM;
1100 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
1101 save = kzalloc(sizeof(*save), GFP_KERNEL);
1102 if (!ctl || !save)
1103 goto out_free;
1104
1105 ret = -EFAULT;
1106 if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl)))
1107 goto out_free;
1108 if (copy_from_user(save, &user_vmcb->save, sizeof(*save)))
1109 goto out_free;
1110
1111 ret = -EINVAL;
1112 if (!nested_vmcb_check_controls(ctl))
1113 goto out_free;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001114
1115 /*
1116 * Processor state contains L2 state. Check that it is
1117 * valid for guest mode (see nested_vmcb_checks).
1118 */
1119 cr0 = kvm_read_cr0(vcpu);
1120 if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001121 goto out_free;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001122
1123 /*
1124 * Validate host state saved from before VMRUN (see
1125 * nested_svm_check_permissions).
1126 * TODO: validate reserved bits for all saved state.
1127 */
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001128 if (!(save->cr0 & X86_CR0_PG))
1129 goto out_free;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001130
1131 /*
1132 * All checks done, we can enter guest mode. L1 control fields
1133 * come from the nested save state. Guest state is already
1134 * in the registers, the save area of the nested state instead
1135 * contains saved L1 state.
1136 */
1137 copy_vmcb_control_area(&hsave->control, &svm->vmcb->control);
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001138 hsave->save = *save;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001139
Maxim Levitsky0dd16b52020-08-27 20:11:39 +03001140 svm->nested.vmcb12_gpa = kvm_state->hdr.svm.vmcb_pa;
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001141 load_nested_vmcb_control(svm, ctl);
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001142 nested_prepare_vmcb_control(svm);
1143
Maxim Levitsky772b81b2020-08-27 19:27:19 +03001144 if (!nested_svm_vmrun_msrpm(svm))
Vitaly Kuznetsovd5cd6f32020-09-14 15:37:25 +02001145 goto out_free;
Joerg Roedel6ccbd292020-09-07 15:15:02 +02001146
1147 ret = 0;
1148out_free:
1149 kfree(save);
1150 kfree(ctl);
1151
1152 return ret;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001153}
1154
Paolo Bonzini33b22172020-04-17 10:24:18 -04001155struct kvm_x86_nested_ops svm_nested_ops = {
1156 .check_events = svm_check_nested_events,
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001157 .get_state = svm_get_nested_state,
1158 .set_state = svm_set_nested_state,
Paolo Bonzini33b22172020-04-17 10:24:18 -04001159};