blob: fb68467e60496aa5bc2abd2f8fa5dfd85e257ebd [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;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100101
Joerg Roedel06e78522020-06-25 10:03:23 +0200102 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100103
104 if (!is_guest_mode(&svm->vcpu))
105 return;
106
107 c = &svm->vmcb->control;
108 h = &svm->nested.hsave->control;
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400109 g = &svm->nested.ctl;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100110
Paolo Bonzini7c866632020-05-16 08:42:28 -0400111 svm->nested.host_intercept_exceptions = h->intercept_exceptions;
112
Joerg Roedel883b0a92020-03-24 10:41:52 +0100113 c->intercept_cr = h->intercept_cr;
114 c->intercept_dr = h->intercept_dr;
115 c->intercept_exceptions = h->intercept_exceptions;
116 c->intercept = h->intercept;
117
Paolo Bonzinie9fd7612020-05-13 13:28:23 -0400118 if (g->int_ctl & V_INTR_MASKING_MASK) {
Joerg Roedel883b0a92020-03-24 10:41:52 +0100119 /* We only want the cr8 intercept bits of L1 */
120 c->intercept_cr &= ~(1U << INTERCEPT_CR8_READ);
121 c->intercept_cr &= ~(1U << INTERCEPT_CR8_WRITE);
122
123 /*
124 * Once running L2 with HF_VINTR_MASK, EFLAGS.IF does not
125 * affect any interrupt we may want to inject; therefore,
126 * interrupt window vmexits are irrelevant to L0.
127 */
128 c->intercept &= ~(1ULL << INTERCEPT_VINTR);
129 }
130
131 /* We don't want to see VMMCALLs from a nested guest */
132 c->intercept &= ~(1ULL << INTERCEPT_VMMCALL);
133
134 c->intercept_cr |= g->intercept_cr;
135 c->intercept_dr |= g->intercept_dr;
136 c->intercept_exceptions |= g->intercept_exceptions;
137 c->intercept |= g->intercept;
138}
139
Paolo Bonzini2f675912020-05-18 15:21:22 -0400140static void copy_vmcb_control_area(struct vmcb_control_area *dst,
141 struct vmcb_control_area *from)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100142{
Joerg Roedel883b0a92020-03-24 10:41:52 +0100143 dst->intercept_cr = from->intercept_cr;
144 dst->intercept_dr = from->intercept_dr;
145 dst->intercept_exceptions = from->intercept_exceptions;
146 dst->intercept = from->intercept;
147 dst->iopm_base_pa = from->iopm_base_pa;
148 dst->msrpm_base_pa = from->msrpm_base_pa;
149 dst->tsc_offset = from->tsc_offset;
Paolo Bonzini6c0238c2020-05-20 08:02:17 -0400150 /* asid not copied, it is handled manually for svm->vmcb. */
Joerg Roedel883b0a92020-03-24 10:41:52 +0100151 dst->tlb_ctl = from->tlb_ctl;
152 dst->int_ctl = from->int_ctl;
153 dst->int_vector = from->int_vector;
154 dst->int_state = from->int_state;
155 dst->exit_code = from->exit_code;
156 dst->exit_code_hi = from->exit_code_hi;
157 dst->exit_info_1 = from->exit_info_1;
158 dst->exit_info_2 = from->exit_info_2;
159 dst->exit_int_info = from->exit_int_info;
160 dst->exit_int_info_err = from->exit_int_info_err;
161 dst->nested_ctl = from->nested_ctl;
162 dst->event_inj = from->event_inj;
163 dst->event_inj_err = from->event_inj_err;
164 dst->nested_cr3 = from->nested_cr3;
165 dst->virt_ext = from->virt_ext;
166 dst->pause_filter_count = from->pause_filter_count;
167 dst->pause_filter_thresh = from->pause_filter_thresh;
168}
169
170static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
171{
172 /*
173 * This function merges the msr permission bitmaps of kvm and the
174 * nested vmcb. It is optimized in that it only merges the parts where
175 * the kvm msr permission bitmap may contain zero bits
176 */
177 int i;
178
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400179 if (!(svm->nested.ctl.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100180 return true;
181
182 for (i = 0; i < MSRPM_OFFSETS; i++) {
183 u32 value, p;
184 u64 offset;
185
186 if (msrpm_offsets[i] == 0xffffffff)
187 break;
188
189 p = msrpm_offsets[i];
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400190 offset = svm->nested.ctl.msrpm_base_pa + (p * 4);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100191
192 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
193 return false;
194
195 svm->nested.msrpm[p] = svm->msrpm[p] | value;
196 }
197
198 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
199
200 return true;
201}
202
Paolo Bonzinica46d732020-05-18 13:02:15 -0400203static bool nested_vmcb_check_controls(struct vmcb_control_area *control)
204{
205 if ((control->intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
206 return false;
207
208 if (control->asid == 0)
209 return false;
210
211 if ((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) &&
212 !npt_enabled)
213 return false;
214
215 return true;
216}
217
Krish Sadhukhan761e4162020-07-08 00:39:56 +0000218static bool nested_vmcb_checks(struct vcpu_svm *svm, struct vmcb *vmcb)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100219{
Krish Sadhukhan761e4162020-07-08 00:39:56 +0000220 bool nested_vmcb_lma;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100221 if ((vmcb->save.efer & EFER_SVME) == 0)
222 return false;
223
Krish Sadhukhan4f233372020-04-09 16:50:33 -0400224 if (((vmcb->save.cr0 & X86_CR0_CD) == 0) &&
225 (vmcb->save.cr0 & X86_CR0_NW))
226 return false;
227
Krish Sadhukhan1aef8162020-05-22 18:19:52 -0400228 if (!kvm_dr6_valid(vmcb->save.dr6) || !kvm_dr7_valid(vmcb->save.dr7))
229 return false;
230
Krish Sadhukhan761e4162020-07-08 00:39:56 +0000231 nested_vmcb_lma =
232 (vmcb->save.efer & EFER_LME) &&
233 (vmcb->save.cr0 & X86_CR0_PG);
234
235 if (!nested_vmcb_lma) {
236 if (vmcb->save.cr4 & X86_CR4_PAE) {
237 if (vmcb->save.cr3 & MSR_CR3_LEGACY_PAE_RESERVED_MASK)
238 return false;
239 } else {
240 if (vmcb->save.cr3 & MSR_CR3_LEGACY_RESERVED_MASK)
241 return false;
242 }
243 } else {
244 if (!(vmcb->save.cr4 & X86_CR4_PAE) ||
245 !(vmcb->save.cr0 & X86_CR0_PE) ||
246 (vmcb->save.cr3 & MSR_CR3_LONG_RESERVED_MASK))
247 return false;
248 }
249 if (kvm_valid_cr4(&svm->vcpu, vmcb->save.cr4))
250 return false;
251
Paolo Bonzinica46d732020-05-18 13:02:15 -0400252 return nested_vmcb_check_controls(&vmcb->control);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100253}
254
Paolo Bonzini3e06f012020-05-13 13:07:26 -0400255static void load_nested_vmcb_control(struct vcpu_svm *svm,
256 struct vmcb_control_area *control)
257{
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400258 copy_vmcb_control_area(&svm->nested.ctl, control);
Paolo Bonzini3e06f012020-05-13 13:07:26 -0400259
Paolo Bonzinicc440cd2020-05-13 13:36:32 -0400260 /* Copy it here because nested_svm_check_controls will check it. */
261 svm->nested.ctl.asid = control->asid;
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400262 svm->nested.ctl.msrpm_base_pa &= ~0x0fffULL;
263 svm->nested.ctl.iopm_base_pa &= ~0x0fffULL;
Paolo Bonzini3e06f012020-05-13 13:07:26 -0400264}
265
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400266/*
267 * Synchronize fields that are written by the processor, so that
268 * they can be copied back into the nested_vmcb.
269 */
270void sync_nested_vmcb_control(struct vcpu_svm *svm)
271{
272 u32 mask;
273 svm->nested.ctl.event_inj = svm->vmcb->control.event_inj;
274 svm->nested.ctl.event_inj_err = svm->vmcb->control.event_inj_err;
275
276 /* Only a few fields of int_ctl are written by the processor. */
277 mask = V_IRQ_MASK | V_TPR_MASK;
278 if (!(svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) &&
Joerg Roedela284ba52020-06-25 10:03:24 +0200279 svm_is_intercept(svm, INTERCEPT_VINTR)) {
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400280 /*
281 * In order to request an interrupt window, L0 is usurping
282 * svm->vmcb->control.int_ctl and possibly setting V_IRQ
283 * even if it was clear in L1's VMCB. Restoring it would be
284 * wrong. However, in this case V_IRQ will remain true until
285 * interrupt_window_interception calls svm_clear_vintr and
286 * restores int_ctl. We can just leave it aside.
287 */
288 mask &= ~V_IRQ_MASK;
289 }
290 svm->nested.ctl.int_ctl &= ~mask;
291 svm->nested.ctl.int_ctl |= svm->vmcb->control.int_ctl & mask;
292}
293
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400294/*
295 * Transfer any event that L0 or L1 wanted to inject into L2 to
296 * EXIT_INT_INFO.
297 */
298static void nested_vmcb_save_pending_event(struct vcpu_svm *svm,
299 struct vmcb *nested_vmcb)
300{
301 struct kvm_vcpu *vcpu = &svm->vcpu;
302 u32 exit_int_info = 0;
303 unsigned int nr;
304
305 if (vcpu->arch.exception.injected) {
306 nr = vcpu->arch.exception.nr;
307 exit_int_info = nr | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT;
308
309 if (vcpu->arch.exception.has_error_code) {
310 exit_int_info |= SVM_EVTINJ_VALID_ERR;
311 nested_vmcb->control.exit_int_info_err =
312 vcpu->arch.exception.error_code;
313 }
314
315 } else if (vcpu->arch.nmi_injected) {
316 exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
317
318 } else if (vcpu->arch.interrupt.injected) {
319 nr = vcpu->arch.interrupt.nr;
320 exit_int_info = nr | SVM_EVTINJ_VALID;
321
322 if (vcpu->arch.interrupt.soft)
323 exit_int_info |= SVM_EVTINJ_TYPE_SOFT;
324 else
325 exit_int_info |= SVM_EVTINJ_TYPE_INTR;
326 }
327
328 nested_vmcb->control.exit_int_info = exit_int_info;
329}
330
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200331static inline bool nested_npt_enabled(struct vcpu_svm *svm)
332{
333 return svm->nested.ctl.nested_ctl & SVM_NESTED_CTL_NP_ENABLE;
334}
335
336/*
Vitaly Kuznetsovd82aaef2020-07-10 16:11:56 +0200337 * Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true
338 * if we are emulating VM-Entry into a guest with NPT enabled.
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200339 */
340static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
341 bool nested_npt)
342{
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200343 if (cr3 & rsvd_bits(cpuid_maxphyaddr(vcpu), 63))
344 return -EINVAL;
345
346 if (!nested_npt && is_pae_paging(vcpu) &&
347 (cr3 != kvm_read_cr3(vcpu) || pdptrs_changed(vcpu))) {
348 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
349 return -EINVAL;
350 }
351
352 /*
353 * TODO: optimize unconditional TLB flush/MMU sync here and in
354 * kvm_init_shadow_npt_mmu().
355 */
356 if (!nested_npt)
357 kvm_mmu_new_pgd(vcpu, cr3, false, false);
358
359 vcpu->arch.cr3 = cr3;
360 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
361
362 kvm_init_mmu(vcpu, false);
363
364 return 0;
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200365}
366
Paolo Bonzinif241d712020-05-18 10:56:43 -0400367static void nested_prepare_vmcb_save(struct vcpu_svm *svm, struct vmcb *nested_vmcb)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100368{
Joerg Roedel883b0a92020-03-24 10:41:52 +0100369 /* Load the nested guest state */
370 svm->vmcb->save.es = nested_vmcb->save.es;
371 svm->vmcb->save.cs = nested_vmcb->save.cs;
372 svm->vmcb->save.ss = nested_vmcb->save.ss;
373 svm->vmcb->save.ds = nested_vmcb->save.ds;
374 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
375 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
376 kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
377 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
378 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
379 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100380 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
381 kvm_rax_write(&svm->vcpu, nested_vmcb->save.rax);
382 kvm_rsp_write(&svm->vcpu, nested_vmcb->save.rsp);
383 kvm_rip_write(&svm->vcpu, nested_vmcb->save.rip);
384
385 /* In case we don't even reach vcpu_run, the fields are not updated */
386 svm->vmcb->save.rax = nested_vmcb->save.rax;
387 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
388 svm->vmcb->save.rip = nested_vmcb->save.rip;
389 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
Paolo Bonzini5679b802020-05-04 11:28:25 -0400390 svm->vcpu.arch.dr6 = nested_vmcb->save.dr6;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100391 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
Paolo Bonzinif241d712020-05-18 10:56:43 -0400392}
Joerg Roedel883b0a92020-03-24 10:41:52 +0100393
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400394static void nested_prepare_vmcb_control(struct vcpu_svm *svm)
Paolo Bonzinif241d712020-05-18 10:56:43 -0400395{
Paolo Bonzini91b71302020-05-22 12:28:52 -0400396 const u32 mask = V_INTR_MASKING_MASK | V_GIF_ENABLE_MASK | V_GIF_MASK;
Vitaly Kuznetsov62156f62020-07-10 16:11:53 +0200397
398 if (nested_npt_enabled(svm))
Paolo Bonzini69cb8772020-05-22 05:27:46 -0400399 nested_svm_init_mmu_context(&svm->vcpu);
400
Paolo Bonzini18fc6c52020-05-18 11:07:08 -0400401 svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset =
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400402 svm->vcpu.arch.l1_tsc_offset + svm->nested.ctl.tsc_offset;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100403
Paolo Bonzini91b71302020-05-22 12:28:52 -0400404 svm->vmcb->control.int_ctl =
405 (svm->nested.ctl.int_ctl & ~mask) |
406 (svm->nested.hsave->control.int_ctl & mask);
407
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400408 svm->vmcb->control.virt_ext = svm->nested.ctl.virt_ext;
409 svm->vmcb->control.int_vector = svm->nested.ctl.int_vector;
410 svm->vmcb->control.int_state = svm->nested.ctl.int_state;
411 svm->vmcb->control.event_inj = svm->nested.ctl.event_inj;
412 svm->vmcb->control.event_inj_err = svm->nested.ctl.event_inj_err;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100413
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400414 svm->vmcb->control.pause_filter_count = svm->nested.ctl.pause_filter_count;
415 svm->vmcb->control.pause_filter_thresh = svm->nested.ctl.pause_filter_thresh;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100416
Joerg Roedel883b0a92020-03-24 10:41:52 +0100417 /* Enter Guest-Mode */
418 enter_guest_mode(&svm->vcpu);
419
420 /*
421 * Merge guest and host intercepts - must be called with vcpu in
422 * guest-mode to take affect here
423 */
424 recalc_intercepts(svm);
425
Joerg Roedel06e78522020-06-25 10:03:23 +0200426 vmcb_mark_all_dirty(svm->vmcb);
Paolo Bonzinif241d712020-05-18 10:56:43 -0400427}
428
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200429int enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa,
Paolo Bonzinif241d712020-05-18 10:56:43 -0400430 struct vmcb *nested_vmcb)
431{
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200432 int ret;
433
Paolo Bonzinif241d712020-05-18 10:56:43 -0400434 svm->nested.vmcb = vmcb_gpa;
Paolo Bonzinif241d712020-05-18 10:56:43 -0400435 load_nested_vmcb_control(svm, &nested_vmcb->control);
436 nested_prepare_vmcb_save(svm, nested_vmcb);
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400437 nested_prepare_vmcb_control(svm);
Paolo Bonzinif241d712020-05-18 10:56:43 -0400438
Vitaly Kuznetsova506fdd2020-07-10 16:11:55 +0200439 ret = nested_svm_load_cr3(&svm->vcpu, nested_vmcb->save.cr3,
440 nested_npt_enabled(svm));
441 if (ret)
442 return ret;
443
Paolo Bonziniffdf7f92020-05-22 12:18:27 -0400444 svm_set_gif(svm, true);
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200445
446 return 0;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100447}
448
449int nested_svm_vmrun(struct vcpu_svm *svm)
450{
451 int ret;
452 struct vmcb *nested_vmcb;
453 struct vmcb *hsave = svm->nested.hsave;
454 struct vmcb *vmcb = svm->vmcb;
455 struct kvm_host_map map;
456 u64 vmcb_gpa;
457
Paolo Bonzini7c67f5462020-04-23 10:52:48 -0400458 if (is_smm(&svm->vcpu)) {
459 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
460 return 1;
461 }
Joerg Roedel883b0a92020-03-24 10:41:52 +0100462
Paolo Bonzini7c67f5462020-04-23 10:52:48 -0400463 vmcb_gpa = svm->vmcb->save.rax;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100464 ret = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(vmcb_gpa), &map);
465 if (ret == -EINVAL) {
466 kvm_inject_gp(&svm->vcpu, 0);
467 return 1;
468 } else if (ret) {
469 return kvm_skip_emulated_instruction(&svm->vcpu);
470 }
471
472 ret = kvm_skip_emulated_instruction(&svm->vcpu);
473
474 nested_vmcb = map.hva;
475
Krish Sadhukhan761e4162020-07-08 00:39:56 +0000476 if (!nested_vmcb_checks(svm, nested_vmcb)) {
Joerg Roedel883b0a92020-03-24 10:41:52 +0100477 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
478 nested_vmcb->control.exit_code_hi = 0;
479 nested_vmcb->control.exit_info_1 = 0;
480 nested_vmcb->control.exit_info_2 = 0;
Paolo Bonzini69c9dfa2020-05-13 12:57:26 -0400481 goto out;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100482 }
483
484 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
485 nested_vmcb->save.rip,
486 nested_vmcb->control.int_ctl,
487 nested_vmcb->control.event_inj,
488 nested_vmcb->control.nested_ctl);
489
490 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
491 nested_vmcb->control.intercept_cr >> 16,
492 nested_vmcb->control.intercept_exceptions,
493 nested_vmcb->control.intercept);
494
495 /* Clear internal status */
496 kvm_clear_exception_queue(&svm->vcpu);
497 kvm_clear_interrupt_queue(&svm->vcpu);
498
499 /*
500 * Save the old vmcb, so we don't need to pick what we save, but can
501 * restore everything when a VMEXIT occurs
502 */
503 hsave->save.es = vmcb->save.es;
504 hsave->save.cs = vmcb->save.cs;
505 hsave->save.ss = vmcb->save.ss;
506 hsave->save.ds = vmcb->save.ds;
507 hsave->save.gdtr = vmcb->save.gdtr;
508 hsave->save.idtr = vmcb->save.idtr;
509 hsave->save.efer = svm->vcpu.arch.efer;
510 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
511 hsave->save.cr4 = svm->vcpu.arch.cr4;
512 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
513 hsave->save.rip = kvm_rip_read(&svm->vcpu);
514 hsave->save.rsp = vmcb->save.rsp;
515 hsave->save.rax = vmcb->save.rax;
516 if (npt_enabled)
517 hsave->save.cr3 = vmcb->save.cr3;
518 else
519 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
520
Paolo Bonzini2f675912020-05-18 15:21:22 -0400521 copy_vmcb_control_area(&hsave->control, &vmcb->control);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100522
Paolo Bonzinif74f9412020-04-23 13:22:27 -0400523 svm->nested.nested_run_pending = 1;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100524
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200525 if (enter_svm_guest_mode(svm, vmcb_gpa, nested_vmcb))
526 goto out_exit_err;
Vitaly Kuznetsovebdb3db2020-07-10 16:11:51 +0200527
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200528 if (nested_svm_vmrun_msrpm(svm))
529 goto out;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100530
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +0200531out_exit_err:
532 svm->nested.nested_run_pending = 0;
533
534 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
535 svm->vmcb->control.exit_code_hi = 0;
536 svm->vmcb->control.exit_info_1 = 0;
537 svm->vmcb->control.exit_info_2 = 0;
538
539 nested_svm_vmexit(svm);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100540
Paolo Bonzini69c9dfa2020-05-13 12:57:26 -0400541out:
542 kvm_vcpu_unmap(&svm->vcpu, &map, true);
543
Joerg Roedel883b0a92020-03-24 10:41:52 +0100544 return ret;
545}
546
547void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
548{
549 to_vmcb->save.fs = from_vmcb->save.fs;
550 to_vmcb->save.gs = from_vmcb->save.gs;
551 to_vmcb->save.tr = from_vmcb->save.tr;
552 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
553 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
554 to_vmcb->save.star = from_vmcb->save.star;
555 to_vmcb->save.lstar = from_vmcb->save.lstar;
556 to_vmcb->save.cstar = from_vmcb->save.cstar;
557 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
558 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
559 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
560 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
561}
562
563int nested_svm_vmexit(struct vcpu_svm *svm)
564{
565 int rc;
566 struct vmcb *nested_vmcb;
567 struct vmcb *hsave = svm->nested.hsave;
568 struct vmcb *vmcb = svm->vmcb;
569 struct kvm_host_map map;
570
Joerg Roedel883b0a92020-03-24 10:41:52 +0100571 rc = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(svm->nested.vmcb), &map);
572 if (rc) {
573 if (rc == -EINVAL)
574 kvm_inject_gp(&svm->vcpu, 0);
575 return 1;
576 }
577
578 nested_vmcb = map.hva;
579
580 /* Exit Guest-Mode */
581 leave_guest_mode(&svm->vcpu);
582 svm->nested.vmcb = 0;
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400583 WARN_ON_ONCE(svm->nested.nested_run_pending);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100584
Paolo Bonzini38c0b192020-04-23 13:13:09 -0400585 /* in case we halted in L2 */
586 svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE;
587
Joerg Roedel883b0a92020-03-24 10:41:52 +0100588 /* Give the current vmcb to the guest */
Paolo Bonziniffdf7f92020-05-22 12:18:27 -0400589 svm_set_gif(svm, false);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100590
591 nested_vmcb->save.es = vmcb->save.es;
592 nested_vmcb->save.cs = vmcb->save.cs;
593 nested_vmcb->save.ss = vmcb->save.ss;
594 nested_vmcb->save.ds = vmcb->save.ds;
595 nested_vmcb->save.gdtr = vmcb->save.gdtr;
596 nested_vmcb->save.idtr = vmcb->save.idtr;
597 nested_vmcb->save.efer = svm->vcpu.arch.efer;
598 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
599 nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
600 nested_vmcb->save.cr2 = vmcb->save.cr2;
601 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
602 nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
Vitaly Kuznetsovb6162e82020-05-27 11:01:02 +0200603 nested_vmcb->save.rip = kvm_rip_read(&svm->vcpu);
604 nested_vmcb->save.rsp = kvm_rsp_read(&svm->vcpu);
605 nested_vmcb->save.rax = kvm_rax_read(&svm->vcpu);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100606 nested_vmcb->save.dr7 = vmcb->save.dr7;
Paolo Bonzini5679b802020-05-04 11:28:25 -0400607 nested_vmcb->save.dr6 = svm->vcpu.arch.dr6;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100608 nested_vmcb->save.cpl = vmcb->save.cpl;
609
Joerg Roedel883b0a92020-03-24 10:41:52 +0100610 nested_vmcb->control.int_state = vmcb->control.int_state;
611 nested_vmcb->control.exit_code = vmcb->control.exit_code;
612 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
613 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
614 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400615
616 if (nested_vmcb->control.exit_code != SVM_EXIT_ERR)
617 nested_vmcb_save_pending_event(svm, nested_vmcb);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100618
619 if (svm->nrips_enabled)
620 nested_vmcb->control.next_rip = vmcb->control.next_rip;
621
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -0400622 nested_vmcb->control.int_ctl = svm->nested.ctl.int_ctl;
623 nested_vmcb->control.tlb_ctl = svm->nested.ctl.tlb_ctl;
624 nested_vmcb->control.event_inj = svm->nested.ctl.event_inj;
625 nested_vmcb->control.event_inj_err = svm->nested.ctl.event_inj_err;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100626
627 nested_vmcb->control.pause_filter_count =
628 svm->vmcb->control.pause_filter_count;
629 nested_vmcb->control.pause_filter_thresh =
630 svm->vmcb->control.pause_filter_thresh;
631
Joerg Roedel883b0a92020-03-24 10:41:52 +0100632 /* Restore the original control entries */
Paolo Bonzini2f675912020-05-18 15:21:22 -0400633 copy_vmcb_control_area(&vmcb->control, &hsave->control);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100634
Paolo Bonzini18fc6c52020-05-18 11:07:08 -0400635 svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset =
636 svm->vcpu.arch.l1_tsc_offset;
637
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400638 svm->nested.ctl.nested_cr3 = 0;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100639
640 /* Restore selected save entries */
641 svm->vmcb->save.es = hsave->save.es;
642 svm->vmcb->save.cs = hsave->save.cs;
643 svm->vmcb->save.ss = hsave->save.ss;
644 svm->vmcb->save.ds = hsave->save.ds;
645 svm->vmcb->save.gdtr = hsave->save.gdtr;
646 svm->vmcb->save.idtr = hsave->save.idtr;
647 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
648 svm_set_efer(&svm->vcpu, hsave->save.efer);
649 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
650 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100651 kvm_rax_write(&svm->vcpu, hsave->save.rax);
652 kvm_rsp_write(&svm->vcpu, hsave->save.rsp);
653 kvm_rip_write(&svm->vcpu, hsave->save.rip);
654 svm->vmcb->save.dr7 = 0;
655 svm->vmcb->save.cpl = 0;
656 svm->vmcb->control.exit_int_info = 0;
657
Joerg Roedel06e78522020-06-25 10:03:23 +0200658 vmcb_mark_all_dirty(svm->vmcb);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100659
Paolo Bonzini36e2e982020-05-22 06:04:57 -0400660 trace_kvm_nested_vmexit_inject(nested_vmcb->control.exit_code,
661 nested_vmcb->control.exit_info_1,
662 nested_vmcb->control.exit_info_2,
663 nested_vmcb->control.exit_int_info,
664 nested_vmcb->control.exit_int_info_err,
665 KVM_ISA_SVM);
666
Joerg Roedel883b0a92020-03-24 10:41:52 +0100667 kvm_vcpu_unmap(&svm->vcpu, &map, true);
668
669 nested_svm_uninit_mmu_context(&svm->vcpu);
Vitaly Kuznetsovbf7dea42020-07-10 16:11:54 +0200670
Vitaly Kuznetsovd82aaef2020-07-10 16:11:56 +0200671 rc = nested_svm_load_cr3(&svm->vcpu, hsave->save.cr3, false);
672 if (rc)
673 return 1;
Vitaly Kuznetsovbf7dea42020-07-10 16:11:54 +0200674
Vitaly Kuznetsovd82aaef2020-07-10 16:11:56 +0200675 if (npt_enabled)
676 svm->vmcb->save.cr3 = hsave->save.cr3;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100677
678 /*
679 * Drop what we picked up for L2 via svm_complete_interrupts() so it
680 * doesn't end up in L1.
681 */
682 svm->vcpu.arch.nmi_injected = false;
683 kvm_clear_exception_queue(&svm->vcpu);
684 kvm_clear_interrupt_queue(&svm->vcpu);
685
686 return 0;
687}
688
Paolo Bonzinic513f482020-05-18 13:08:37 -0400689/*
690 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
691 */
692void svm_leave_nested(struct vcpu_svm *svm)
693{
694 if (is_guest_mode(&svm->vcpu)) {
695 struct vmcb *hsave = svm->nested.hsave;
696 struct vmcb *vmcb = svm->vmcb;
697
698 svm->nested.nested_run_pending = 0;
699 leave_guest_mode(&svm->vcpu);
700 copy_vmcb_control_area(&vmcb->control, &hsave->control);
701 nested_svm_uninit_mmu_context(&svm->vcpu);
702 }
703}
704
Joerg Roedel883b0a92020-03-24 10:41:52 +0100705static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
706{
707 u32 offset, msr, value;
708 int write, mask;
709
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400710 if (!(svm->nested.ctl.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100711 return NESTED_EXIT_HOST;
712
713 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
714 offset = svm_msrpm_offset(msr);
715 write = svm->vmcb->control.exit_info_1 & 1;
716 mask = 1 << ((2 * (msr & 0xf)) + write);
717
718 if (offset == MSR_INVALID)
719 return NESTED_EXIT_DONE;
720
721 /* Offset is in 32 bit units but need in 8 bit units */
722 offset *= 4;
723
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400724 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100725 return NESTED_EXIT_DONE;
726
727 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
728}
729
Joerg Roedel883b0a92020-03-24 10:41:52 +0100730static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
731{
732 unsigned port, size, iopm_len;
733 u16 val, mask;
734 u8 start_bit;
735 u64 gpa;
736
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400737 if (!(svm->nested.ctl.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
Joerg Roedel883b0a92020-03-24 10:41:52 +0100738 return NESTED_EXIT_HOST;
739
740 port = svm->vmcb->control.exit_info_1 >> 16;
741 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
742 SVM_IOIO_SIZE_SHIFT;
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400743 gpa = svm->nested.ctl.iopm_base_pa + (port / 8);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100744 start_bit = port % 8;
745 iopm_len = (start_bit + size > 8) ? 2 : 1;
746 mask = (0xf >> (4 - size)) << start_bit;
747 val = 0;
748
749 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
750 return NESTED_EXIT_DONE;
751
752 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
753}
754
755static int nested_svm_intercept(struct vcpu_svm *svm)
756{
757 u32 exit_code = svm->vmcb->control.exit_code;
758 int vmexit = NESTED_EXIT_HOST;
759
760 switch (exit_code) {
761 case SVM_EXIT_MSR:
762 vmexit = nested_svm_exit_handled_msr(svm);
763 break;
764 case SVM_EXIT_IOIO:
765 vmexit = nested_svm_intercept_ioio(svm);
766 break;
767 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
768 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400769 if (svm->nested.ctl.intercept_cr & bit)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100770 vmexit = NESTED_EXIT_DONE;
771 break;
772 }
773 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
774 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400775 if (svm->nested.ctl.intercept_dr & bit)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100776 vmexit = NESTED_EXIT_DONE;
777 break;
778 }
779 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
Paolo Bonzini7c866632020-05-16 08:42:28 -0400780 /*
781 * Host-intercepted exceptions have been checked already in
782 * nested_svm_exit_special. There is nothing to do here,
783 * the vmexit is injected by svm_check_nested_events.
784 */
785 vmexit = NESTED_EXIT_DONE;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100786 break;
787 }
788 case SVM_EXIT_ERR: {
789 vmexit = NESTED_EXIT_DONE;
790 break;
791 }
792 default: {
793 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400794 if (svm->nested.ctl.intercept & exit_bits)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100795 vmexit = NESTED_EXIT_DONE;
796 }
797 }
798
799 return vmexit;
800}
801
802int nested_svm_exit_handled(struct vcpu_svm *svm)
803{
804 int vmexit;
805
806 vmexit = nested_svm_intercept(svm);
807
808 if (vmexit == NESTED_EXIT_DONE)
809 nested_svm_vmexit(svm);
810
811 return vmexit;
812}
813
814int nested_svm_check_permissions(struct vcpu_svm *svm)
815{
816 if (!(svm->vcpu.arch.efer & EFER_SVME) ||
817 !is_paging(&svm->vcpu)) {
818 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
819 return 1;
820 }
821
822 if (svm->vmcb->save.cpl) {
823 kvm_inject_gp(&svm->vcpu, 0);
824 return 1;
825 }
826
827 return 0;
828}
829
Paolo Bonzini7c866632020-05-16 08:42:28 -0400830static bool nested_exit_on_exception(struct vcpu_svm *svm)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100831{
Paolo Bonzini7c866632020-05-16 08:42:28 -0400832 unsigned int nr = svm->vcpu.arch.exception.nr;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100833
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400834 return (svm->nested.ctl.intercept_exceptions & (1 << nr));
Paolo Bonzini7c866632020-05-16 08:42:28 -0400835}
Joerg Roedel883b0a92020-03-24 10:41:52 +0100836
Paolo Bonzini7c866632020-05-16 08:42:28 -0400837static void nested_svm_inject_exception_vmexit(struct vcpu_svm *svm)
838{
839 unsigned int nr = svm->vcpu.arch.exception.nr;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100840
841 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
842 svm->vmcb->control.exit_code_hi = 0;
Paolo Bonzini7c866632020-05-16 08:42:28 -0400843
844 if (svm->vcpu.arch.exception.has_error_code)
845 svm->vmcb->control.exit_info_1 = svm->vcpu.arch.exception.error_code;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100846
847 /*
848 * EXITINFO2 is undefined for all exception intercepts other
849 * than #PF.
850 */
Paolo Bonzini7c866632020-05-16 08:42:28 -0400851 if (nr == PF_VECTOR) {
852 if (svm->vcpu.arch.exception.nested_apf)
853 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
854 else if (svm->vcpu.arch.exception.has_payload)
855 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload;
856 else
857 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
858 } else if (nr == DB_VECTOR) {
859 /* See inject_pending_event. */
860 kvm_deliver_exception_payload(&svm->vcpu);
861 if (svm->vcpu.arch.dr7 & DR7_GD) {
862 svm->vcpu.arch.dr7 &= ~DR7_GD;
863 kvm_update_dr7(&svm->vcpu);
864 }
865 } else
866 WARN_ON(svm->vcpu.arch.exception.has_payload);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100867
Paolo Bonzini7c866632020-05-16 08:42:28 -0400868 nested_svm_vmexit(svm);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100869}
870
Paolo Bonzini55714cd2020-04-23 08:17:28 -0400871static void nested_svm_smi(struct vcpu_svm *svm)
872{
873 svm->vmcb->control.exit_code = SVM_EXIT_SMI;
874 svm->vmcb->control.exit_info_1 = 0;
875 svm->vmcb->control.exit_info_2 = 0;
876
877 nested_svm_vmexit(svm);
878}
879
Cathy Avery9c3d3702020-04-14 16:11:06 -0400880static void nested_svm_nmi(struct vcpu_svm *svm)
881{
882 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
883 svm->vmcb->control.exit_info_1 = 0;
884 svm->vmcb->control.exit_info_2 = 0;
885
886 nested_svm_vmexit(svm);
887}
888
Joerg Roedel883b0a92020-03-24 10:41:52 +0100889static void nested_svm_intr(struct vcpu_svm *svm)
890{
Paolo Bonzini6e085cb2020-04-23 13:15:33 -0400891 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
892
Joerg Roedel883b0a92020-03-24 10:41:52 +0100893 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
894 svm->vmcb->control.exit_info_1 = 0;
895 svm->vmcb->control.exit_info_2 = 0;
896
Paolo Bonzini6e085cb2020-04-23 13:15:33 -0400897 nested_svm_vmexit(svm);
Joerg Roedel883b0a92020-03-24 10:41:52 +0100898}
899
Paolo Bonzini5b6724082020-05-16 08:50:35 -0400900static inline bool nested_exit_on_init(struct vcpu_svm *svm)
901{
Paolo Bonzinie670bf62020-05-13 13:16:12 -0400902 return (svm->nested.ctl.intercept & (1ULL << INTERCEPT_INIT));
Paolo Bonzini5b6724082020-05-16 08:50:35 -0400903}
904
905static void nested_svm_init(struct vcpu_svm *svm)
906{
907 svm->vmcb->control.exit_code = SVM_EXIT_INIT;
908 svm->vmcb->control.exit_info_1 = 0;
909 svm->vmcb->control.exit_info_2 = 0;
910
911 nested_svm_vmexit(svm);
912}
913
914
Paolo Bonzini33b22172020-04-17 10:24:18 -0400915static int svm_check_nested_events(struct kvm_vcpu *vcpu)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100916{
917 struct vcpu_svm *svm = to_svm(vcpu);
918 bool block_nested_events =
Paolo Bonzinibd279622020-05-16 08:46:00 -0400919 kvm_event_needs_reinjection(vcpu) || svm->nested.nested_run_pending;
Paolo Bonzini5b6724082020-05-16 08:50:35 -0400920 struct kvm_lapic *apic = vcpu->arch.apic;
921
922 if (lapic_in_kernel(vcpu) &&
923 test_bit(KVM_APIC_INIT, &apic->pending_events)) {
924 if (block_nested_events)
925 return -EBUSY;
926 if (!nested_exit_on_init(svm))
927 return 0;
928 nested_svm_init(svm);
929 return 0;
930 }
Joerg Roedel883b0a92020-03-24 10:41:52 +0100931
Paolo Bonzini7c866632020-05-16 08:42:28 -0400932 if (vcpu->arch.exception.pending) {
933 if (block_nested_events)
934 return -EBUSY;
935 if (!nested_exit_on_exception(svm))
936 return 0;
937 nested_svm_inject_exception_vmexit(svm);
938 return 0;
939 }
940
Paolo Bonzini221e7612020-04-23 08:13:10 -0400941 if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) {
Paolo Bonzini55714cd2020-04-23 08:17:28 -0400942 if (block_nested_events)
943 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -0400944 if (!nested_exit_on_smi(svm))
945 return 0;
Paolo Bonzini55714cd2020-04-23 08:17:28 -0400946 nested_svm_smi(svm);
947 return 0;
948 }
949
Paolo Bonzini221e7612020-04-23 08:13:10 -0400950 if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) {
Cathy Avery9c3d3702020-04-14 16:11:06 -0400951 if (block_nested_events)
952 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -0400953 if (!nested_exit_on_nmi(svm))
954 return 0;
Cathy Avery9c3d3702020-04-14 16:11:06 -0400955 nested_svm_nmi(svm);
956 return 0;
957 }
958
Paolo Bonzini221e7612020-04-23 08:13:10 -0400959 if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) {
Joerg Roedel883b0a92020-03-24 10:41:52 +0100960 if (block_nested_events)
961 return -EBUSY;
Paolo Bonzini221e7612020-04-23 08:13:10 -0400962 if (!nested_exit_on_intr(svm))
963 return 0;
Joerg Roedel883b0a92020-03-24 10:41:52 +0100964 nested_svm_intr(svm);
965 return 0;
966 }
967
968 return 0;
969}
970
971int nested_svm_exit_special(struct vcpu_svm *svm)
972{
973 u32 exit_code = svm->vmcb->control.exit_code;
974
975 switch (exit_code) {
976 case SVM_EXIT_INTR:
977 case SVM_EXIT_NMI:
Joerg Roedel883b0a92020-03-24 10:41:52 +0100978 case SVM_EXIT_NPF:
Paolo Bonzini7c866632020-05-16 08:42:28 -0400979 return NESTED_EXIT_HOST;
980 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
981 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
982
983 if (get_host_vmcb(svm)->control.intercept_exceptions & excp_bits)
984 return NESTED_EXIT_HOST;
985 else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +0200986 svm->vcpu.arch.apf.host_apf_flags)
Paolo Bonzini7c866632020-05-16 08:42:28 -0400987 /* Trap async PF even if not shadowing */
Joerg Roedel883b0a92020-03-24 10:41:52 +0100988 return NESTED_EXIT_HOST;
989 break;
Paolo Bonzini7c866632020-05-16 08:42:28 -0400990 }
Joerg Roedel883b0a92020-03-24 10:41:52 +0100991 default:
992 break;
993 }
994
995 return NESTED_EXIT_CONTINUE;
996}
Paolo Bonzini33b22172020-04-17 10:24:18 -0400997
Paolo Bonzinicc440cd2020-05-13 13:36:32 -0400998static int svm_get_nested_state(struct kvm_vcpu *vcpu,
999 struct kvm_nested_state __user *user_kvm_nested_state,
1000 u32 user_data_size)
1001{
1002 struct vcpu_svm *svm;
1003 struct kvm_nested_state kvm_state = {
1004 .flags = 0,
1005 .format = KVM_STATE_NESTED_FORMAT_SVM,
1006 .size = sizeof(kvm_state),
1007 };
1008 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1009 &user_kvm_nested_state->data.svm[0];
1010
1011 if (!vcpu)
1012 return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE;
1013
1014 svm = to_svm(vcpu);
1015
1016 if (user_data_size < kvm_state.size)
1017 goto out;
1018
1019 /* First fill in the header and copy it out. */
1020 if (is_guest_mode(vcpu)) {
1021 kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb;
1022 kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE;
1023 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
1024
1025 if (svm->nested.nested_run_pending)
1026 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
1027 }
1028
1029 if (gif_set(svm))
1030 kvm_state.flags |= KVM_STATE_NESTED_GIF_SET;
1031
1032 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
1033 return -EFAULT;
1034
1035 if (!is_guest_mode(vcpu))
1036 goto out;
1037
1038 /*
1039 * Copy over the full size of the VMCB rather than just the size
1040 * of the structs.
1041 */
1042 if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE))
1043 return -EFAULT;
1044 if (copy_to_user(&user_vmcb->control, &svm->nested.ctl,
1045 sizeof(user_vmcb->control)))
1046 return -EFAULT;
1047 if (copy_to_user(&user_vmcb->save, &svm->nested.hsave->save,
1048 sizeof(user_vmcb->save)))
1049 return -EFAULT;
1050
1051out:
1052 return kvm_state.size;
1053}
1054
1055static int svm_set_nested_state(struct kvm_vcpu *vcpu,
1056 struct kvm_nested_state __user *user_kvm_nested_state,
1057 struct kvm_nested_state *kvm_state)
1058{
1059 struct vcpu_svm *svm = to_svm(vcpu);
1060 struct vmcb *hsave = svm->nested.hsave;
1061 struct vmcb __user *user_vmcb = (struct vmcb __user *)
1062 &user_kvm_nested_state->data.svm[0];
1063 struct vmcb_control_area ctl;
1064 struct vmcb_save_area save;
1065 u32 cr0;
1066
1067 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM)
1068 return -EINVAL;
1069
1070 if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE |
1071 KVM_STATE_NESTED_RUN_PENDING |
1072 KVM_STATE_NESTED_GIF_SET))
1073 return -EINVAL;
1074
1075 /*
1076 * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
1077 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
1078 */
1079 if (!(vcpu->arch.efer & EFER_SVME)) {
1080 /* GIF=1 and no guest mode are required if SVME=0. */
1081 if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
1082 return -EINVAL;
1083 }
1084
1085 /* SMM temporarily disables SVM, so we cannot be in guest mode. */
1086 if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
1087 return -EINVAL;
1088
1089 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) {
1090 svm_leave_nested(svm);
1091 goto out_set_gif;
1092 }
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;
1098 if (copy_from_user(&ctl, &user_vmcb->control, sizeof(ctl)))
1099 return -EFAULT;
1100 if (copy_from_user(&save, &user_vmcb->save, sizeof(save)))
1101 return -EFAULT;
1102
1103 if (!nested_vmcb_check_controls(&ctl))
1104 return -EINVAL;
1105
1106 /*
1107 * Processor state contains L2 state. Check that it is
1108 * valid for guest mode (see nested_vmcb_checks).
1109 */
1110 cr0 = kvm_read_cr0(vcpu);
1111 if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
1112 return -EINVAL;
1113
1114 /*
1115 * Validate host state saved from before VMRUN (see
1116 * nested_svm_check_permissions).
1117 * TODO: validate reserved bits for all saved state.
1118 */
1119 if (!(save.cr0 & X86_CR0_PG))
1120 return -EINVAL;
1121
1122 /*
1123 * All checks done, we can enter guest mode. L1 control fields
1124 * come from the nested save state. Guest state is already
1125 * in the registers, the save area of the nested state instead
1126 * contains saved L1 state.
1127 */
1128 copy_vmcb_control_area(&hsave->control, &svm->vmcb->control);
1129 hsave->save = save;
1130
1131 svm->nested.vmcb = kvm_state->hdr.svm.vmcb_pa;
1132 load_nested_vmcb_control(svm, &ctl);
1133 nested_prepare_vmcb_control(svm);
1134
1135out_set_gif:
1136 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1137 return 0;
1138}
1139
Paolo Bonzini33b22172020-04-17 10:24:18 -04001140struct kvm_x86_nested_ops svm_nested_ops = {
1141 .check_events = svm_check_nested_events,
Paolo Bonzinicc440cd2020-05-13 13:36:32 -04001142 .get_state = svm_get_nested_state,
1143 .set_state = svm_set_nested_state,
Paolo Bonzini33b22172020-04-17 10:24:18 -04001144};