Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/arch/x86/kvm/svm/nested.c
26471 views
1
// 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 <[email protected]>
12
* Avi Kivity <[email protected]>
13
*/
14
15
#define pr_fmt(fmt) KBUILD_MODNAME ": " 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>
22
#include <asm/debugreg.h>
23
24
#include "kvm_emulate.h"
25
#include "trace.h"
26
#include "mmu.h"
27
#include "x86.h"
28
#include "smm.h"
29
#include "cpuid.h"
30
#include "lapic.h"
31
#include "svm.h"
32
#include "hyperv.h"
33
34
#define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK
35
36
static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
37
struct x86_exception *fault)
38
{
39
struct vcpu_svm *svm = to_svm(vcpu);
40
struct vmcb *vmcb = svm->vmcb;
41
42
if (vmcb->control.exit_code != SVM_EXIT_NPF) {
43
/*
44
* TODO: track the cause of the nested page fault, and
45
* correctly fill in the high bits of exit_info_1.
46
*/
47
vmcb->control.exit_code = SVM_EXIT_NPF;
48
vmcb->control.exit_code_hi = 0;
49
vmcb->control.exit_info_1 = (1ULL << 32);
50
vmcb->control.exit_info_2 = fault->address;
51
}
52
53
vmcb->control.exit_info_1 &= ~0xffffffffULL;
54
vmcb->control.exit_info_1 |= fault->error_code;
55
56
nested_svm_vmexit(svm);
57
}
58
59
static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
60
{
61
struct vcpu_svm *svm = to_svm(vcpu);
62
u64 cr3 = svm->nested.ctl.nested_cr3;
63
u64 pdpte;
64
int ret;
65
66
/*
67
* Note, nCR3 is "assumed" to be 32-byte aligned, i.e. the CPU ignores
68
* nCR3[4:0] when loading PDPTEs from memory.
69
*/
70
ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte,
71
(cr3 & GENMASK(11, 5)) + index * 8, 8);
72
if (ret)
73
return 0;
74
return pdpte;
75
}
76
77
static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
78
{
79
struct vcpu_svm *svm = to_svm(vcpu);
80
81
return svm->nested.ctl.nested_cr3;
82
}
83
84
static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
85
{
86
struct vcpu_svm *svm = to_svm(vcpu);
87
88
WARN_ON(mmu_is_nested(vcpu));
89
90
vcpu->arch.mmu = &vcpu->arch.guest_mmu;
91
92
/*
93
* The NPT format depends on L1's CR4 and EFER, which is in vmcb01. Note,
94
* when called via KVM_SET_NESTED_STATE, that state may _not_ match current
95
* vCPU state. CR0.WP is explicitly ignored, while CR0.PG is required.
96
*/
97
kvm_init_shadow_npt_mmu(vcpu, X86_CR0_PG, svm->vmcb01.ptr->save.cr4,
98
svm->vmcb01.ptr->save.efer,
99
svm->nested.ctl.nested_cr3);
100
vcpu->arch.mmu->get_guest_pgd = nested_svm_get_tdp_cr3;
101
vcpu->arch.mmu->get_pdptr = nested_svm_get_tdp_pdptr;
102
vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit;
103
vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
104
}
105
106
static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
107
{
108
vcpu->arch.mmu = &vcpu->arch.root_mmu;
109
vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
110
}
111
112
static bool nested_vmcb_needs_vls_intercept(struct vcpu_svm *svm)
113
{
114
if (!guest_cpu_cap_has(&svm->vcpu, X86_FEATURE_V_VMSAVE_VMLOAD))
115
return true;
116
117
if (!nested_npt_enabled(svm))
118
return true;
119
120
if (!(svm->nested.ctl.virt_ext & VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK))
121
return true;
122
123
return false;
124
}
125
126
void recalc_intercepts(struct vcpu_svm *svm)
127
{
128
struct vmcb_control_area *c, *h;
129
struct vmcb_ctrl_area_cached *g;
130
unsigned int i;
131
132
vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
133
134
if (!is_guest_mode(&svm->vcpu))
135
return;
136
137
c = &svm->vmcb->control;
138
h = &svm->vmcb01.ptr->control;
139
g = &svm->nested.ctl;
140
141
for (i = 0; i < MAX_INTERCEPT; i++)
142
c->intercepts[i] = h->intercepts[i];
143
144
if (g->int_ctl & V_INTR_MASKING_MASK) {
145
/*
146
* If L2 is active and V_INTR_MASKING is enabled in vmcb12,
147
* disable intercept of CR8 writes as L2's CR8 does not affect
148
* any interrupt KVM may want to inject.
149
*
150
* Similarly, disable intercept of virtual interrupts (used to
151
* detect interrupt windows) if the saved RFLAGS.IF is '0', as
152
* the effective RFLAGS.IF for L1 interrupts will never be set
153
* while L2 is running (L2's RFLAGS.IF doesn't affect L1 IRQs).
154
*/
155
vmcb_clr_intercept(c, INTERCEPT_CR8_WRITE);
156
if (!(svm->vmcb01.ptr->save.rflags & X86_EFLAGS_IF))
157
vmcb_clr_intercept(c, INTERCEPT_VINTR);
158
}
159
160
/*
161
* We want to see VMMCALLs from a nested guest only when Hyper-V L2 TLB
162
* flush feature is enabled.
163
*/
164
if (!nested_svm_l2_tlb_flush_enabled(&svm->vcpu))
165
vmcb_clr_intercept(c, INTERCEPT_VMMCALL);
166
167
for (i = 0; i < MAX_INTERCEPT; i++)
168
c->intercepts[i] |= g->intercepts[i];
169
170
/* If SMI is not intercepted, ignore guest SMI intercept as well */
171
if (!intercept_smi)
172
vmcb_clr_intercept(c, INTERCEPT_SMI);
173
174
if (nested_vmcb_needs_vls_intercept(svm)) {
175
/*
176
* If the virtual VMLOAD/VMSAVE is not enabled for the L2,
177
* we must intercept these instructions to correctly
178
* emulate them in case L1 doesn't intercept them.
179
*/
180
vmcb_set_intercept(c, INTERCEPT_VMLOAD);
181
vmcb_set_intercept(c, INTERCEPT_VMSAVE);
182
} else {
183
WARN_ON(!(c->virt_ext & VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK));
184
}
185
}
186
187
/*
188
* This array (and its actual size) holds the set of offsets (indexing by chunk
189
* size) to process when merging vmcb12's MSRPM with vmcb01's MSRPM. Note, the
190
* set of MSRs for which interception is disabled in vmcb01 is per-vCPU, e.g.
191
* based on CPUID features. This array only tracks MSRs that *might* be passed
192
* through to the guest.
193
*
194
* Hardcode the capacity of the array based on the maximum number of _offsets_.
195
* MSRs are batched together, so there are fewer offsets than MSRs.
196
*/
197
static int nested_svm_msrpm_merge_offsets[7] __ro_after_init;
198
static int nested_svm_nr_msrpm_merge_offsets __ro_after_init;
199
typedef unsigned long nsvm_msrpm_merge_t;
200
201
int __init nested_svm_init_msrpm_merge_offsets(void)
202
{
203
static const u32 merge_msrs[] __initconst = {
204
MSR_STAR,
205
MSR_IA32_SYSENTER_CS,
206
MSR_IA32_SYSENTER_EIP,
207
MSR_IA32_SYSENTER_ESP,
208
#ifdef CONFIG_X86_64
209
MSR_GS_BASE,
210
MSR_FS_BASE,
211
MSR_KERNEL_GS_BASE,
212
MSR_LSTAR,
213
MSR_CSTAR,
214
MSR_SYSCALL_MASK,
215
#endif
216
MSR_IA32_SPEC_CTRL,
217
MSR_IA32_PRED_CMD,
218
MSR_IA32_FLUSH_CMD,
219
MSR_IA32_APERF,
220
MSR_IA32_MPERF,
221
MSR_IA32_LASTBRANCHFROMIP,
222
MSR_IA32_LASTBRANCHTOIP,
223
MSR_IA32_LASTINTFROMIP,
224
MSR_IA32_LASTINTTOIP,
225
};
226
int i, j;
227
228
for (i = 0; i < ARRAY_SIZE(merge_msrs); i++) {
229
int bit_nr = svm_msrpm_bit_nr(merge_msrs[i]);
230
u32 offset;
231
232
if (WARN_ON(bit_nr < 0))
233
return -EIO;
234
235
/*
236
* Merging is done in chunks to reduce the number of accesses
237
* to L1's bitmap.
238
*/
239
offset = bit_nr / BITS_PER_BYTE / sizeof(nsvm_msrpm_merge_t);
240
241
for (j = 0; j < nested_svm_nr_msrpm_merge_offsets; j++) {
242
if (nested_svm_msrpm_merge_offsets[j] == offset)
243
break;
244
}
245
246
if (j < nested_svm_nr_msrpm_merge_offsets)
247
continue;
248
249
if (WARN_ON(j >= ARRAY_SIZE(nested_svm_msrpm_merge_offsets)))
250
return -EIO;
251
252
nested_svm_msrpm_merge_offsets[j] = offset;
253
nested_svm_nr_msrpm_merge_offsets++;
254
}
255
256
return 0;
257
}
258
259
/*
260
* Merge L0's (KVM) and L1's (Nested VMCB) MSR permission bitmaps. The function
261
* is optimized in that it only merges the parts where KVM MSR permission bitmap
262
* may contain zero bits.
263
*/
264
static bool nested_svm_merge_msrpm(struct kvm_vcpu *vcpu)
265
{
266
struct vcpu_svm *svm = to_svm(vcpu);
267
nsvm_msrpm_merge_t *msrpm02 = svm->nested.msrpm;
268
nsvm_msrpm_merge_t *msrpm01 = svm->msrpm;
269
int i;
270
271
/*
272
* MSR bitmap update can be skipped when:
273
* - MSR bitmap for L1 hasn't changed.
274
* - Nested hypervisor (L1) is attempting to launch the same L2 as
275
* before.
276
* - Nested hypervisor (L1) is using Hyper-V emulation interface and
277
* tells KVM (L0) there were no changes in MSR bitmap for L2.
278
*/
279
#ifdef CONFIG_KVM_HYPERV
280
if (!svm->nested.force_msr_bitmap_recalc) {
281
struct hv_vmcb_enlightenments *hve = &svm->nested.ctl.hv_enlightenments;
282
283
if (kvm_hv_hypercall_enabled(vcpu) &&
284
hve->hv_enlightenments_control.msr_bitmap &&
285
(svm->nested.ctl.clean & BIT(HV_VMCB_NESTED_ENLIGHTENMENTS)))
286
goto set_msrpm_base_pa;
287
}
288
#endif
289
290
if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
291
return true;
292
293
for (i = 0; i < nested_svm_nr_msrpm_merge_offsets; i++) {
294
const int p = nested_svm_msrpm_merge_offsets[i];
295
nsvm_msrpm_merge_t l1_val;
296
gpa_t gpa;
297
298
gpa = svm->nested.ctl.msrpm_base_pa + (p * sizeof(l1_val));
299
300
if (kvm_vcpu_read_guest(vcpu, gpa, &l1_val, sizeof(l1_val)))
301
return false;
302
303
msrpm02[p] = msrpm01[p] | l1_val;
304
}
305
306
svm->nested.force_msr_bitmap_recalc = false;
307
308
#ifdef CONFIG_KVM_HYPERV
309
set_msrpm_base_pa:
310
#endif
311
svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
312
313
return true;
314
}
315
316
/*
317
* Bits 11:0 of bitmap address are ignored by hardware
318
*/
319
static bool nested_svm_check_bitmap_pa(struct kvm_vcpu *vcpu, u64 pa, u32 size)
320
{
321
u64 addr = PAGE_ALIGN(pa);
322
323
return kvm_vcpu_is_legal_gpa(vcpu, addr) &&
324
kvm_vcpu_is_legal_gpa(vcpu, addr + size - 1);
325
}
326
327
static bool __nested_vmcb_check_controls(struct kvm_vcpu *vcpu,
328
struct vmcb_ctrl_area_cached *control)
329
{
330
if (CC(!vmcb12_is_intercept(control, INTERCEPT_VMRUN)))
331
return false;
332
333
if (CC(control->asid == 0))
334
return false;
335
336
if (CC((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) && !npt_enabled))
337
return false;
338
339
if (CC(!nested_svm_check_bitmap_pa(vcpu, control->msrpm_base_pa,
340
MSRPM_SIZE)))
341
return false;
342
if (CC(!nested_svm_check_bitmap_pa(vcpu, control->iopm_base_pa,
343
IOPM_SIZE)))
344
return false;
345
346
if (CC((control->int_ctl & V_NMI_ENABLE_MASK) &&
347
!vmcb12_is_intercept(control, INTERCEPT_NMI))) {
348
return false;
349
}
350
351
return true;
352
}
353
354
/* Common checks that apply to both L1 and L2 state. */
355
static bool __nested_vmcb_check_save(struct kvm_vcpu *vcpu,
356
struct vmcb_save_area_cached *save)
357
{
358
if (CC(!(save->efer & EFER_SVME)))
359
return false;
360
361
if (CC((save->cr0 & X86_CR0_CD) == 0 && (save->cr0 & X86_CR0_NW)) ||
362
CC(save->cr0 & ~0xffffffffULL))
363
return false;
364
365
if (CC(!kvm_dr6_valid(save->dr6)) || CC(!kvm_dr7_valid(save->dr7)))
366
return false;
367
368
/*
369
* These checks are also performed by KVM_SET_SREGS,
370
* except that EFER.LMA is not checked by SVM against
371
* CR0.PG && EFER.LME.
372
*/
373
if ((save->efer & EFER_LME) && (save->cr0 & X86_CR0_PG)) {
374
if (CC(!(save->cr4 & X86_CR4_PAE)) ||
375
CC(!(save->cr0 & X86_CR0_PE)) ||
376
CC(!kvm_vcpu_is_legal_cr3(vcpu, save->cr3)))
377
return false;
378
}
379
380
/* Note, SVM doesn't have any additional restrictions on CR4. */
381
if (CC(!__kvm_is_valid_cr4(vcpu, save->cr4)))
382
return false;
383
384
if (CC(!kvm_valid_efer(vcpu, save->efer)))
385
return false;
386
387
return true;
388
}
389
390
static bool nested_vmcb_check_save(struct kvm_vcpu *vcpu)
391
{
392
struct vcpu_svm *svm = to_svm(vcpu);
393
struct vmcb_save_area_cached *save = &svm->nested.save;
394
395
return __nested_vmcb_check_save(vcpu, save);
396
}
397
398
static bool nested_vmcb_check_controls(struct kvm_vcpu *vcpu)
399
{
400
struct vcpu_svm *svm = to_svm(vcpu);
401
struct vmcb_ctrl_area_cached *ctl = &svm->nested.ctl;
402
403
return __nested_vmcb_check_controls(vcpu, ctl);
404
}
405
406
static
407
void __nested_copy_vmcb_control_to_cache(struct kvm_vcpu *vcpu,
408
struct vmcb_ctrl_area_cached *to,
409
struct vmcb_control_area *from)
410
{
411
unsigned int i;
412
413
for (i = 0; i < MAX_INTERCEPT; i++)
414
to->intercepts[i] = from->intercepts[i];
415
416
to->iopm_base_pa = from->iopm_base_pa;
417
to->msrpm_base_pa = from->msrpm_base_pa;
418
to->tsc_offset = from->tsc_offset;
419
to->tlb_ctl = from->tlb_ctl;
420
to->int_ctl = from->int_ctl;
421
to->int_vector = from->int_vector;
422
to->int_state = from->int_state;
423
to->exit_code = from->exit_code;
424
to->exit_code_hi = from->exit_code_hi;
425
to->exit_info_1 = from->exit_info_1;
426
to->exit_info_2 = from->exit_info_2;
427
to->exit_int_info = from->exit_int_info;
428
to->exit_int_info_err = from->exit_int_info_err;
429
to->nested_ctl = from->nested_ctl;
430
to->event_inj = from->event_inj;
431
to->event_inj_err = from->event_inj_err;
432
to->next_rip = from->next_rip;
433
to->nested_cr3 = from->nested_cr3;
434
to->virt_ext = from->virt_ext;
435
to->pause_filter_count = from->pause_filter_count;
436
to->pause_filter_thresh = from->pause_filter_thresh;
437
438
/* Copy asid here because nested_vmcb_check_controls will check it. */
439
to->asid = from->asid;
440
to->msrpm_base_pa &= ~0x0fffULL;
441
to->iopm_base_pa &= ~0x0fffULL;
442
443
#ifdef CONFIG_KVM_HYPERV
444
/* Hyper-V extensions (Enlightened VMCB) */
445
if (kvm_hv_hypercall_enabled(vcpu)) {
446
to->clean = from->clean;
447
memcpy(&to->hv_enlightenments, &from->hv_enlightenments,
448
sizeof(to->hv_enlightenments));
449
}
450
#endif
451
}
452
453
void nested_copy_vmcb_control_to_cache(struct vcpu_svm *svm,
454
struct vmcb_control_area *control)
455
{
456
__nested_copy_vmcb_control_to_cache(&svm->vcpu, &svm->nested.ctl, control);
457
}
458
459
static void __nested_copy_vmcb_save_to_cache(struct vmcb_save_area_cached *to,
460
struct vmcb_save_area *from)
461
{
462
/*
463
* Copy only fields that are validated, as we need them
464
* to avoid TOC/TOU races.
465
*/
466
to->efer = from->efer;
467
to->cr0 = from->cr0;
468
to->cr3 = from->cr3;
469
to->cr4 = from->cr4;
470
471
to->dr6 = from->dr6;
472
to->dr7 = from->dr7;
473
}
474
475
void nested_copy_vmcb_save_to_cache(struct vcpu_svm *svm,
476
struct vmcb_save_area *save)
477
{
478
__nested_copy_vmcb_save_to_cache(&svm->nested.save, save);
479
}
480
481
/*
482
* Synchronize fields that are written by the processor, so that
483
* they can be copied back into the vmcb12.
484
*/
485
void nested_sync_control_from_vmcb02(struct vcpu_svm *svm)
486
{
487
u32 mask;
488
svm->nested.ctl.event_inj = svm->vmcb->control.event_inj;
489
svm->nested.ctl.event_inj_err = svm->vmcb->control.event_inj_err;
490
491
/* Only a few fields of int_ctl are written by the processor. */
492
mask = V_IRQ_MASK | V_TPR_MASK;
493
/*
494
* Don't sync vmcb02 V_IRQ back to vmcb12 if KVM (L0) is intercepting
495
* virtual interrupts in order to request an interrupt window, as KVM
496
* has usurped vmcb02's int_ctl. If an interrupt window opens before
497
* the next VM-Exit, svm_clear_vintr() will restore vmcb12's int_ctl.
498
* If no window opens, V_IRQ will be correctly preserved in vmcb12's
499
* int_ctl (because it was never recognized while L2 was running).
500
*/
501
if (svm_is_intercept(svm, INTERCEPT_VINTR) &&
502
!test_bit(INTERCEPT_VINTR, (unsigned long *)svm->nested.ctl.intercepts))
503
mask &= ~V_IRQ_MASK;
504
505
if (nested_vgif_enabled(svm))
506
mask |= V_GIF_MASK;
507
508
if (nested_vnmi_enabled(svm))
509
mask |= V_NMI_BLOCKING_MASK | V_NMI_PENDING_MASK;
510
511
svm->nested.ctl.int_ctl &= ~mask;
512
svm->nested.ctl.int_ctl |= svm->vmcb->control.int_ctl & mask;
513
}
514
515
/*
516
* Transfer any event that L0 or L1 wanted to inject into L2 to
517
* EXIT_INT_INFO.
518
*/
519
static void nested_save_pending_event_to_vmcb12(struct vcpu_svm *svm,
520
struct vmcb *vmcb12)
521
{
522
struct kvm_vcpu *vcpu = &svm->vcpu;
523
u32 exit_int_info = 0;
524
unsigned int nr;
525
526
if (vcpu->arch.exception.injected) {
527
nr = vcpu->arch.exception.vector;
528
exit_int_info = nr | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT;
529
530
if (vcpu->arch.exception.has_error_code) {
531
exit_int_info |= SVM_EVTINJ_VALID_ERR;
532
vmcb12->control.exit_int_info_err =
533
vcpu->arch.exception.error_code;
534
}
535
536
} else if (vcpu->arch.nmi_injected) {
537
exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
538
539
} else if (vcpu->arch.interrupt.injected) {
540
nr = vcpu->arch.interrupt.nr;
541
exit_int_info = nr | SVM_EVTINJ_VALID;
542
543
if (vcpu->arch.interrupt.soft)
544
exit_int_info |= SVM_EVTINJ_TYPE_SOFT;
545
else
546
exit_int_info |= SVM_EVTINJ_TYPE_INTR;
547
}
548
549
vmcb12->control.exit_int_info = exit_int_info;
550
}
551
552
static void nested_svm_transition_tlb_flush(struct kvm_vcpu *vcpu)
553
{
554
/* Handle pending Hyper-V TLB flush requests */
555
kvm_hv_nested_transtion_tlb_flush(vcpu, npt_enabled);
556
557
/*
558
* TODO: optimize unconditional TLB flush/MMU sync. A partial list of
559
* things to fix before this can be conditional:
560
*
561
* - Flush TLBs for both L1 and L2 remote TLB flush
562
* - Honor L1's request to flush an ASID on nested VMRUN
563
* - Sync nested NPT MMU on VMRUN that flushes L2's ASID[*]
564
* - Don't crush a pending TLB flush in vmcb02 on nested VMRUN
565
* - Flush L1's ASID on KVM_REQ_TLB_FLUSH_GUEST
566
*
567
* [*] Unlike nested EPT, SVM's ASID management can invalidate nested
568
* NPT guest-physical mappings on VMRUN.
569
*/
570
kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
571
kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
572
}
573
574
/*
575
* Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true
576
* if we are emulating VM-Entry into a guest with NPT enabled.
577
*/
578
static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
579
bool nested_npt, bool reload_pdptrs)
580
{
581
if (CC(!kvm_vcpu_is_legal_cr3(vcpu, cr3)))
582
return -EINVAL;
583
584
if (reload_pdptrs && !nested_npt && is_pae_paging(vcpu) &&
585
CC(!load_pdptrs(vcpu, cr3)))
586
return -EINVAL;
587
588
vcpu->arch.cr3 = cr3;
589
590
/* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */
591
kvm_init_mmu(vcpu);
592
593
if (!nested_npt)
594
kvm_mmu_new_pgd(vcpu, cr3);
595
596
return 0;
597
}
598
599
void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm)
600
{
601
if (!svm->nested.vmcb02.ptr)
602
return;
603
604
/* FIXME: merge g_pat from vmcb01 and vmcb12. */
605
svm->nested.vmcb02.ptr->save.g_pat = svm->vmcb01.ptr->save.g_pat;
606
}
607
608
static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12)
609
{
610
bool new_vmcb12 = false;
611
struct vmcb *vmcb01 = svm->vmcb01.ptr;
612
struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
613
struct kvm_vcpu *vcpu = &svm->vcpu;
614
615
nested_vmcb02_compute_g_pat(svm);
616
617
/* Load the nested guest state */
618
if (svm->nested.vmcb12_gpa != svm->nested.last_vmcb12_gpa) {
619
new_vmcb12 = true;
620
svm->nested.last_vmcb12_gpa = svm->nested.vmcb12_gpa;
621
svm->nested.force_msr_bitmap_recalc = true;
622
}
623
624
if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_SEG))) {
625
vmcb02->save.es = vmcb12->save.es;
626
vmcb02->save.cs = vmcb12->save.cs;
627
vmcb02->save.ss = vmcb12->save.ss;
628
vmcb02->save.ds = vmcb12->save.ds;
629
vmcb02->save.cpl = vmcb12->save.cpl;
630
vmcb_mark_dirty(vmcb02, VMCB_SEG);
631
}
632
633
if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DT))) {
634
vmcb02->save.gdtr = vmcb12->save.gdtr;
635
vmcb02->save.idtr = vmcb12->save.idtr;
636
vmcb_mark_dirty(vmcb02, VMCB_DT);
637
}
638
639
kvm_set_rflags(vcpu, vmcb12->save.rflags | X86_EFLAGS_FIXED);
640
641
svm_set_efer(vcpu, svm->nested.save.efer);
642
643
svm_set_cr0(vcpu, svm->nested.save.cr0);
644
svm_set_cr4(vcpu, svm->nested.save.cr4);
645
646
svm->vcpu.arch.cr2 = vmcb12->save.cr2;
647
648
kvm_rax_write(vcpu, vmcb12->save.rax);
649
kvm_rsp_write(vcpu, vmcb12->save.rsp);
650
kvm_rip_write(vcpu, vmcb12->save.rip);
651
652
/* In case we don't even reach vcpu_run, the fields are not updated */
653
vmcb02->save.rax = vmcb12->save.rax;
654
vmcb02->save.rsp = vmcb12->save.rsp;
655
vmcb02->save.rip = vmcb12->save.rip;
656
657
/* These bits will be set properly on the first execution when new_vmc12 is true */
658
if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DR))) {
659
vmcb02->save.dr7 = svm->nested.save.dr7 | DR7_FIXED_1;
660
svm->vcpu.arch.dr6 = svm->nested.save.dr6 | DR6_ACTIVE_LOW;
661
vmcb_mark_dirty(vmcb02, VMCB_DR);
662
}
663
664
if (unlikely(guest_cpu_cap_has(vcpu, X86_FEATURE_LBRV) &&
665
(svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) {
666
/*
667
* Reserved bits of DEBUGCTL are ignored. Be consistent with
668
* svm_set_msr's definition of reserved bits.
669
*/
670
svm_copy_lbrs(vmcb02, vmcb12);
671
vmcb02->save.dbgctl &= ~DEBUGCTL_RESERVED_BITS;
672
svm_update_lbrv(&svm->vcpu);
673
674
} else if (unlikely(vmcb01->control.virt_ext & LBR_CTL_ENABLE_MASK)) {
675
svm_copy_lbrs(vmcb02, vmcb01);
676
}
677
}
678
679
static inline bool is_evtinj_soft(u32 evtinj)
680
{
681
u32 type = evtinj & SVM_EVTINJ_TYPE_MASK;
682
u8 vector = evtinj & SVM_EVTINJ_VEC_MASK;
683
684
if (!(evtinj & SVM_EVTINJ_VALID))
685
return false;
686
687
if (type == SVM_EVTINJ_TYPE_SOFT)
688
return true;
689
690
return type == SVM_EVTINJ_TYPE_EXEPT && kvm_exception_is_soft(vector);
691
}
692
693
static bool is_evtinj_nmi(u32 evtinj)
694
{
695
u32 type = evtinj & SVM_EVTINJ_TYPE_MASK;
696
697
if (!(evtinj & SVM_EVTINJ_VALID))
698
return false;
699
700
return type == SVM_EVTINJ_TYPE_NMI;
701
}
702
703
static void nested_vmcb02_prepare_control(struct vcpu_svm *svm,
704
unsigned long vmcb12_rip,
705
unsigned long vmcb12_csbase)
706
{
707
u32 int_ctl_vmcb01_bits = V_INTR_MASKING_MASK;
708
u32 int_ctl_vmcb12_bits = V_TPR_MASK | V_IRQ_INJECTION_BITS_MASK;
709
710
struct kvm_vcpu *vcpu = &svm->vcpu;
711
struct vmcb *vmcb01 = svm->vmcb01.ptr;
712
struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
713
u32 pause_count12;
714
u32 pause_thresh12;
715
716
nested_svm_transition_tlb_flush(vcpu);
717
718
/* Enter Guest-Mode */
719
enter_guest_mode(vcpu);
720
721
/*
722
* Filled at exit: exit_code, exit_code_hi, exit_info_1, exit_info_2,
723
* exit_int_info, exit_int_info_err, next_rip, insn_len, insn_bytes.
724
*/
725
726
if (guest_cpu_cap_has(vcpu, X86_FEATURE_VGIF) &&
727
(svm->nested.ctl.int_ctl & V_GIF_ENABLE_MASK))
728
int_ctl_vmcb12_bits |= (V_GIF_MASK | V_GIF_ENABLE_MASK);
729
else
730
int_ctl_vmcb01_bits |= (V_GIF_MASK | V_GIF_ENABLE_MASK);
731
732
if (vnmi) {
733
if (vmcb01->control.int_ctl & V_NMI_PENDING_MASK) {
734
svm->vcpu.arch.nmi_pending++;
735
kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
736
}
737
if (nested_vnmi_enabled(svm))
738
int_ctl_vmcb12_bits |= (V_NMI_PENDING_MASK |
739
V_NMI_ENABLE_MASK |
740
V_NMI_BLOCKING_MASK);
741
}
742
743
/* Copied from vmcb01. msrpm_base can be overwritten later. */
744
vmcb02->control.nested_ctl = vmcb01->control.nested_ctl;
745
vmcb02->control.iopm_base_pa = vmcb01->control.iopm_base_pa;
746
vmcb02->control.msrpm_base_pa = vmcb01->control.msrpm_base_pa;
747
748
/*
749
* Stash vmcb02's counter if the guest hasn't moved past the guilty
750
* instruction; otherwise, reset the counter to '0'.
751
*
752
* In order to detect if L2 has made forward progress or not, track the
753
* RIP at which a bus lock has occurred on a per-vmcb12 basis. If RIP
754
* is changed, guest has clearly made forward progress, bus_lock_counter
755
* still remained '1', so reset bus_lock_counter to '0'. Eg. In the
756
* scenario, where a buslock happened in L1 before VMRUN, the bus lock
757
* firmly happened on an instruction in the past. Even if vmcb01's
758
* counter is still '1', (because the guilty instruction got patched),
759
* the vCPU has clearly made forward progress and so KVM should reset
760
* vmcb02's counter to '0'.
761
*
762
* If the RIP hasn't changed, stash the bus lock counter at nested VMRUN
763
* to prevent the same guilty instruction from triggering a VM-Exit. Eg.
764
* if userspace rate-limits the vCPU, then it's entirely possible that
765
* L1's tick interrupt is pending by the time userspace re-runs the
766
* vCPU. If KVM unconditionally clears the counter on VMRUN, then when
767
* L1 re-enters L2, the same instruction will trigger a VM-Exit and the
768
* entire cycle start over.
769
*/
770
if (vmcb02->save.rip && (svm->nested.ctl.bus_lock_rip == vmcb02->save.rip))
771
vmcb02->control.bus_lock_counter = 1;
772
else
773
vmcb02->control.bus_lock_counter = 0;
774
775
/* Done at vmrun: asid. */
776
777
/* Also overwritten later if necessary. */
778
vmcb02->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
779
780
/* nested_cr3. */
781
if (nested_npt_enabled(svm))
782
nested_svm_init_mmu_context(vcpu);
783
784
vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset(
785
vcpu->arch.l1_tsc_offset,
786
svm->nested.ctl.tsc_offset,
787
svm->tsc_ratio_msr);
788
789
vmcb02->control.tsc_offset = vcpu->arch.tsc_offset;
790
791
if (guest_cpu_cap_has(vcpu, X86_FEATURE_TSCRATEMSR) &&
792
svm->tsc_ratio_msr != kvm_caps.default_tsc_scaling_ratio)
793
nested_svm_update_tsc_ratio_msr(vcpu);
794
795
vmcb02->control.int_ctl =
796
(svm->nested.ctl.int_ctl & int_ctl_vmcb12_bits) |
797
(vmcb01->control.int_ctl & int_ctl_vmcb01_bits);
798
799
vmcb02->control.int_vector = svm->nested.ctl.int_vector;
800
vmcb02->control.int_state = svm->nested.ctl.int_state;
801
vmcb02->control.event_inj = svm->nested.ctl.event_inj;
802
vmcb02->control.event_inj_err = svm->nested.ctl.event_inj_err;
803
804
/*
805
* next_rip is consumed on VMRUN as the return address pushed on the
806
* stack for injected soft exceptions/interrupts. If nrips is exposed
807
* to L1, take it verbatim from vmcb12. If nrips is supported in
808
* hardware but not exposed to L1, stuff the actual L2 RIP to emulate
809
* what a nrips=0 CPU would do (L1 is responsible for advancing RIP
810
* prior to injecting the event).
811
*/
812
if (guest_cpu_cap_has(vcpu, X86_FEATURE_NRIPS))
813
vmcb02->control.next_rip = svm->nested.ctl.next_rip;
814
else if (boot_cpu_has(X86_FEATURE_NRIPS))
815
vmcb02->control.next_rip = vmcb12_rip;
816
817
svm->nmi_l1_to_l2 = is_evtinj_nmi(vmcb02->control.event_inj);
818
if (is_evtinj_soft(vmcb02->control.event_inj)) {
819
svm->soft_int_injected = true;
820
svm->soft_int_csbase = vmcb12_csbase;
821
svm->soft_int_old_rip = vmcb12_rip;
822
if (guest_cpu_cap_has(vcpu, X86_FEATURE_NRIPS))
823
svm->soft_int_next_rip = svm->nested.ctl.next_rip;
824
else
825
svm->soft_int_next_rip = vmcb12_rip;
826
}
827
828
vmcb02->control.virt_ext = vmcb01->control.virt_ext &
829
LBR_CTL_ENABLE_MASK;
830
if (guest_cpu_cap_has(vcpu, X86_FEATURE_LBRV))
831
vmcb02->control.virt_ext |=
832
(svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK);
833
834
if (!nested_vmcb_needs_vls_intercept(svm))
835
vmcb02->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
836
837
if (guest_cpu_cap_has(vcpu, X86_FEATURE_PAUSEFILTER))
838
pause_count12 = svm->nested.ctl.pause_filter_count;
839
else
840
pause_count12 = 0;
841
if (guest_cpu_cap_has(vcpu, X86_FEATURE_PFTHRESHOLD))
842
pause_thresh12 = svm->nested.ctl.pause_filter_thresh;
843
else
844
pause_thresh12 = 0;
845
if (kvm_pause_in_guest(svm->vcpu.kvm)) {
846
/* use guest values since host doesn't intercept PAUSE */
847
vmcb02->control.pause_filter_count = pause_count12;
848
vmcb02->control.pause_filter_thresh = pause_thresh12;
849
850
} else {
851
/* start from host values otherwise */
852
vmcb02->control.pause_filter_count = vmcb01->control.pause_filter_count;
853
vmcb02->control.pause_filter_thresh = vmcb01->control.pause_filter_thresh;
854
855
/* ... but ensure filtering is disabled if so requested. */
856
if (vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_PAUSE)) {
857
if (!pause_count12)
858
vmcb02->control.pause_filter_count = 0;
859
if (!pause_thresh12)
860
vmcb02->control.pause_filter_thresh = 0;
861
}
862
}
863
864
/*
865
* Merge guest and host intercepts - must be called with vcpu in
866
* guest-mode to take effect.
867
*/
868
recalc_intercepts(svm);
869
}
870
871
static void nested_svm_copy_common_state(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
872
{
873
/*
874
* Some VMCB state is shared between L1 and L2 and thus has to be
875
* moved at the time of nested vmrun and vmexit.
876
*
877
* VMLOAD/VMSAVE state would also belong in this category, but KVM
878
* always performs VMLOAD and VMSAVE from the VMCB01.
879
*/
880
to_vmcb->save.spec_ctrl = from_vmcb->save.spec_ctrl;
881
}
882
883
int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa,
884
struct vmcb *vmcb12, bool from_vmrun)
885
{
886
struct vcpu_svm *svm = to_svm(vcpu);
887
int ret;
888
889
trace_kvm_nested_vmenter(svm->vmcb->save.rip,
890
vmcb12_gpa,
891
vmcb12->save.rip,
892
vmcb12->control.int_ctl,
893
vmcb12->control.event_inj,
894
vmcb12->control.nested_ctl,
895
vmcb12->control.nested_cr3,
896
vmcb12->save.cr3,
897
KVM_ISA_SVM);
898
899
trace_kvm_nested_intercepts(vmcb12->control.intercepts[INTERCEPT_CR] & 0xffff,
900
vmcb12->control.intercepts[INTERCEPT_CR] >> 16,
901
vmcb12->control.intercepts[INTERCEPT_EXCEPTION],
902
vmcb12->control.intercepts[INTERCEPT_WORD3],
903
vmcb12->control.intercepts[INTERCEPT_WORD4],
904
vmcb12->control.intercepts[INTERCEPT_WORD5]);
905
906
907
svm->nested.vmcb12_gpa = vmcb12_gpa;
908
909
WARN_ON(svm->vmcb == svm->nested.vmcb02.ptr);
910
911
nested_svm_copy_common_state(svm->vmcb01.ptr, svm->nested.vmcb02.ptr);
912
913
svm_switch_vmcb(svm, &svm->nested.vmcb02);
914
nested_vmcb02_prepare_control(svm, vmcb12->save.rip, vmcb12->save.cs.base);
915
nested_vmcb02_prepare_save(svm, vmcb12);
916
917
ret = nested_svm_load_cr3(&svm->vcpu, svm->nested.save.cr3,
918
nested_npt_enabled(svm), from_vmrun);
919
if (ret)
920
return ret;
921
922
if (!from_vmrun)
923
kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
924
925
svm_set_gif(svm, true);
926
927
if (kvm_vcpu_apicv_active(vcpu))
928
kvm_make_request(KVM_REQ_APICV_UPDATE, vcpu);
929
930
nested_svm_hv_update_vm_vp_ids(vcpu);
931
932
return 0;
933
}
934
935
int nested_svm_vmrun(struct kvm_vcpu *vcpu)
936
{
937
struct vcpu_svm *svm = to_svm(vcpu);
938
int ret;
939
struct vmcb *vmcb12;
940
struct kvm_host_map map;
941
u64 vmcb12_gpa;
942
struct vmcb *vmcb01 = svm->vmcb01.ptr;
943
944
if (!svm->nested.hsave_msr) {
945
kvm_inject_gp(vcpu, 0);
946
return 1;
947
}
948
949
if (is_smm(vcpu)) {
950
kvm_queue_exception(vcpu, UD_VECTOR);
951
return 1;
952
}
953
954
/* This fails when VP assist page is enabled but the supplied GPA is bogus */
955
ret = kvm_hv_verify_vp_assist(vcpu);
956
if (ret) {
957
kvm_inject_gp(vcpu, 0);
958
return ret;
959
}
960
961
vmcb12_gpa = svm->vmcb->save.rax;
962
ret = kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map);
963
if (ret == -EINVAL) {
964
kvm_inject_gp(vcpu, 0);
965
return 1;
966
} else if (ret) {
967
return kvm_skip_emulated_instruction(vcpu);
968
}
969
970
ret = kvm_skip_emulated_instruction(vcpu);
971
972
vmcb12 = map.hva;
973
974
if (WARN_ON_ONCE(!svm->nested.initialized))
975
return -EINVAL;
976
977
nested_copy_vmcb_control_to_cache(svm, &vmcb12->control);
978
nested_copy_vmcb_save_to_cache(svm, &vmcb12->save);
979
980
if (!nested_vmcb_check_save(vcpu) ||
981
!nested_vmcb_check_controls(vcpu)) {
982
vmcb12->control.exit_code = SVM_EXIT_ERR;
983
vmcb12->control.exit_code_hi = 0;
984
vmcb12->control.exit_info_1 = 0;
985
vmcb12->control.exit_info_2 = 0;
986
goto out;
987
}
988
989
/*
990
* Since vmcb01 is not in use, we can use it to store some of the L1
991
* state.
992
*/
993
vmcb01->save.efer = vcpu->arch.efer;
994
vmcb01->save.cr0 = kvm_read_cr0(vcpu);
995
vmcb01->save.cr4 = vcpu->arch.cr4;
996
vmcb01->save.rflags = kvm_get_rflags(vcpu);
997
vmcb01->save.rip = kvm_rip_read(vcpu);
998
999
if (!npt_enabled)
1000
vmcb01->save.cr3 = kvm_read_cr3(vcpu);
1001
1002
svm->nested.nested_run_pending = 1;
1003
1004
if (enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12, true))
1005
goto out_exit_err;
1006
1007
if (nested_svm_merge_msrpm(vcpu))
1008
goto out;
1009
1010
out_exit_err:
1011
svm->nested.nested_run_pending = 0;
1012
svm->nmi_l1_to_l2 = false;
1013
svm->soft_int_injected = false;
1014
1015
svm->vmcb->control.exit_code = SVM_EXIT_ERR;
1016
svm->vmcb->control.exit_code_hi = 0;
1017
svm->vmcb->control.exit_info_1 = 0;
1018
svm->vmcb->control.exit_info_2 = 0;
1019
1020
nested_svm_vmexit(svm);
1021
1022
out:
1023
kvm_vcpu_unmap(vcpu, &map);
1024
1025
return ret;
1026
}
1027
1028
/* Copy state save area fields which are handled by VMRUN */
1029
void svm_copy_vmrun_state(struct vmcb_save_area *to_save,
1030
struct vmcb_save_area *from_save)
1031
{
1032
to_save->es = from_save->es;
1033
to_save->cs = from_save->cs;
1034
to_save->ss = from_save->ss;
1035
to_save->ds = from_save->ds;
1036
to_save->gdtr = from_save->gdtr;
1037
to_save->idtr = from_save->idtr;
1038
to_save->rflags = from_save->rflags | X86_EFLAGS_FIXED;
1039
to_save->efer = from_save->efer;
1040
to_save->cr0 = from_save->cr0;
1041
to_save->cr3 = from_save->cr3;
1042
to_save->cr4 = from_save->cr4;
1043
to_save->rax = from_save->rax;
1044
to_save->rsp = from_save->rsp;
1045
to_save->rip = from_save->rip;
1046
to_save->cpl = 0;
1047
}
1048
1049
void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmcb)
1050
{
1051
to_vmcb->save.fs = from_vmcb->save.fs;
1052
to_vmcb->save.gs = from_vmcb->save.gs;
1053
to_vmcb->save.tr = from_vmcb->save.tr;
1054
to_vmcb->save.ldtr = from_vmcb->save.ldtr;
1055
to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
1056
to_vmcb->save.star = from_vmcb->save.star;
1057
to_vmcb->save.lstar = from_vmcb->save.lstar;
1058
to_vmcb->save.cstar = from_vmcb->save.cstar;
1059
to_vmcb->save.sfmask = from_vmcb->save.sfmask;
1060
to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
1061
to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
1062
to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
1063
}
1064
1065
int nested_svm_vmexit(struct vcpu_svm *svm)
1066
{
1067
struct kvm_vcpu *vcpu = &svm->vcpu;
1068
struct vmcb *vmcb01 = svm->vmcb01.ptr;
1069
struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
1070
struct vmcb *vmcb12;
1071
struct kvm_host_map map;
1072
int rc;
1073
1074
rc = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.vmcb12_gpa), &map);
1075
if (rc) {
1076
if (rc == -EINVAL)
1077
kvm_inject_gp(vcpu, 0);
1078
return 1;
1079
}
1080
1081
vmcb12 = map.hva;
1082
1083
/* Exit Guest-Mode */
1084
leave_guest_mode(vcpu);
1085
svm->nested.vmcb12_gpa = 0;
1086
WARN_ON_ONCE(svm->nested.nested_run_pending);
1087
1088
kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
1089
1090
/* in case we halted in L2 */
1091
kvm_set_mp_state(vcpu, KVM_MP_STATE_RUNNABLE);
1092
1093
/* Give the current vmcb to the guest */
1094
1095
vmcb12->save.es = vmcb02->save.es;
1096
vmcb12->save.cs = vmcb02->save.cs;
1097
vmcb12->save.ss = vmcb02->save.ss;
1098
vmcb12->save.ds = vmcb02->save.ds;
1099
vmcb12->save.gdtr = vmcb02->save.gdtr;
1100
vmcb12->save.idtr = vmcb02->save.idtr;
1101
vmcb12->save.efer = svm->vcpu.arch.efer;
1102
vmcb12->save.cr0 = kvm_read_cr0(vcpu);
1103
vmcb12->save.cr3 = kvm_read_cr3(vcpu);
1104
vmcb12->save.cr2 = vmcb02->save.cr2;
1105
vmcb12->save.cr4 = svm->vcpu.arch.cr4;
1106
vmcb12->save.rflags = kvm_get_rflags(vcpu);
1107
vmcb12->save.rip = kvm_rip_read(vcpu);
1108
vmcb12->save.rsp = kvm_rsp_read(vcpu);
1109
vmcb12->save.rax = kvm_rax_read(vcpu);
1110
vmcb12->save.dr7 = vmcb02->save.dr7;
1111
vmcb12->save.dr6 = svm->vcpu.arch.dr6;
1112
vmcb12->save.cpl = vmcb02->save.cpl;
1113
1114
vmcb12->control.int_state = vmcb02->control.int_state;
1115
vmcb12->control.exit_code = vmcb02->control.exit_code;
1116
vmcb12->control.exit_code_hi = vmcb02->control.exit_code_hi;
1117
vmcb12->control.exit_info_1 = vmcb02->control.exit_info_1;
1118
vmcb12->control.exit_info_2 = vmcb02->control.exit_info_2;
1119
1120
if (vmcb12->control.exit_code != SVM_EXIT_ERR)
1121
nested_save_pending_event_to_vmcb12(svm, vmcb12);
1122
1123
if (guest_cpu_cap_has(vcpu, X86_FEATURE_NRIPS))
1124
vmcb12->control.next_rip = vmcb02->control.next_rip;
1125
1126
vmcb12->control.int_ctl = svm->nested.ctl.int_ctl;
1127
vmcb12->control.event_inj = svm->nested.ctl.event_inj;
1128
vmcb12->control.event_inj_err = svm->nested.ctl.event_inj_err;
1129
1130
if (!kvm_pause_in_guest(vcpu->kvm)) {
1131
vmcb01->control.pause_filter_count = vmcb02->control.pause_filter_count;
1132
vmcb_mark_dirty(vmcb01, VMCB_INTERCEPTS);
1133
1134
}
1135
1136
/*
1137
* Invalidate bus_lock_rip unless KVM is still waiting for the guest
1138
* to make forward progress before re-enabling bus lock detection.
1139
*/
1140
if (!vmcb02->control.bus_lock_counter)
1141
svm->nested.ctl.bus_lock_rip = INVALID_GPA;
1142
1143
nested_svm_copy_common_state(svm->nested.vmcb02.ptr, svm->vmcb01.ptr);
1144
1145
kvm_nested_vmexit_handle_ibrs(vcpu);
1146
1147
svm_switch_vmcb(svm, &svm->vmcb01);
1148
1149
/*
1150
* Rules for synchronizing int_ctl bits from vmcb02 to vmcb01:
1151
*
1152
* V_IRQ, V_IRQ_VECTOR, V_INTR_PRIO_MASK, V_IGN_TPR: If L1 doesn't
1153
* intercept interrupts, then KVM will use vmcb02's V_IRQ (and related
1154
* flags) to detect interrupt windows for L1 IRQs (even if L1 uses
1155
* virtual interrupt masking). Raise KVM_REQ_EVENT to ensure that
1156
* KVM re-requests an interrupt window if necessary, which implicitly
1157
* copies this bits from vmcb02 to vmcb01.
1158
*
1159
* V_TPR: If L1 doesn't use virtual interrupt masking, then L1's vTPR
1160
* is stored in vmcb02, but its value doesn't need to be copied from/to
1161
* vmcb01 because it is copied from/to the virtual APIC's TPR register
1162
* on each VM entry/exit.
1163
*
1164
* V_GIF: If nested vGIF is not used, KVM uses vmcb02's V_GIF for L1's
1165
* V_GIF. However, GIF is architecturally clear on each VM exit, thus
1166
* there is no need to copy V_GIF from vmcb02 to vmcb01.
1167
*/
1168
if (!nested_exit_on_intr(svm))
1169
kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
1170
1171
if (unlikely(guest_cpu_cap_has(vcpu, X86_FEATURE_LBRV) &&
1172
(svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) {
1173
svm_copy_lbrs(vmcb12, vmcb02);
1174
svm_update_lbrv(vcpu);
1175
} else if (unlikely(vmcb01->control.virt_ext & LBR_CTL_ENABLE_MASK)) {
1176
svm_copy_lbrs(vmcb01, vmcb02);
1177
svm_update_lbrv(vcpu);
1178
}
1179
1180
if (vnmi) {
1181
if (vmcb02->control.int_ctl & V_NMI_BLOCKING_MASK)
1182
vmcb01->control.int_ctl |= V_NMI_BLOCKING_MASK;
1183
else
1184
vmcb01->control.int_ctl &= ~V_NMI_BLOCKING_MASK;
1185
1186
if (vcpu->arch.nmi_pending) {
1187
vcpu->arch.nmi_pending--;
1188
vmcb01->control.int_ctl |= V_NMI_PENDING_MASK;
1189
} else {
1190
vmcb01->control.int_ctl &= ~V_NMI_PENDING_MASK;
1191
}
1192
}
1193
1194
/*
1195
* On vmexit the GIF is set to false and
1196
* no event can be injected in L1.
1197
*/
1198
svm_set_gif(svm, false);
1199
vmcb01->control.exit_int_info = 0;
1200
1201
svm->vcpu.arch.tsc_offset = svm->vcpu.arch.l1_tsc_offset;
1202
if (vmcb01->control.tsc_offset != svm->vcpu.arch.tsc_offset) {
1203
vmcb01->control.tsc_offset = svm->vcpu.arch.tsc_offset;
1204
vmcb_mark_dirty(vmcb01, VMCB_INTERCEPTS);
1205
}
1206
1207
if (kvm_caps.has_tsc_control &&
1208
vcpu->arch.tsc_scaling_ratio != vcpu->arch.l1_tsc_scaling_ratio) {
1209
vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio;
1210
svm_write_tsc_multiplier(vcpu);
1211
}
1212
1213
svm->nested.ctl.nested_cr3 = 0;
1214
1215
/*
1216
* Restore processor state that had been saved in vmcb01
1217
*/
1218
kvm_set_rflags(vcpu, vmcb01->save.rflags);
1219
svm_set_efer(vcpu, vmcb01->save.efer);
1220
svm_set_cr0(vcpu, vmcb01->save.cr0 | X86_CR0_PE);
1221
svm_set_cr4(vcpu, vmcb01->save.cr4);
1222
kvm_rax_write(vcpu, vmcb01->save.rax);
1223
kvm_rsp_write(vcpu, vmcb01->save.rsp);
1224
kvm_rip_write(vcpu, vmcb01->save.rip);
1225
1226
svm->vcpu.arch.dr7 = DR7_FIXED_1;
1227
kvm_update_dr7(&svm->vcpu);
1228
1229
trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code,
1230
vmcb12->control.exit_info_1,
1231
vmcb12->control.exit_info_2,
1232
vmcb12->control.exit_int_info,
1233
vmcb12->control.exit_int_info_err,
1234
KVM_ISA_SVM);
1235
1236
kvm_vcpu_unmap(vcpu, &map);
1237
1238
nested_svm_transition_tlb_flush(vcpu);
1239
1240
nested_svm_uninit_mmu_context(vcpu);
1241
1242
rc = nested_svm_load_cr3(vcpu, vmcb01->save.cr3, false, true);
1243
if (rc)
1244
return 1;
1245
1246
/*
1247
* Drop what we picked up for L2 via svm_complete_interrupts() so it
1248
* doesn't end up in L1.
1249
*/
1250
svm->vcpu.arch.nmi_injected = false;
1251
kvm_clear_exception_queue(vcpu);
1252
kvm_clear_interrupt_queue(vcpu);
1253
1254
/*
1255
* If we are here following the completion of a VMRUN that
1256
* is being single-stepped, queue the pending #DB intercept
1257
* right now so that it an be accounted for before we execute
1258
* L1's next instruction.
1259
*/
1260
if (unlikely(vmcb01->save.rflags & X86_EFLAGS_TF))
1261
kvm_queue_exception(&(svm->vcpu), DB_VECTOR);
1262
1263
/*
1264
* Un-inhibit the AVIC right away, so that other vCPUs can start
1265
* to benefit from it right away.
1266
*/
1267
if (kvm_apicv_activated(vcpu->kvm))
1268
__kvm_vcpu_update_apicv(vcpu);
1269
1270
return 0;
1271
}
1272
1273
static void nested_svm_triple_fault(struct kvm_vcpu *vcpu)
1274
{
1275
struct vcpu_svm *svm = to_svm(vcpu);
1276
1277
if (!vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_SHUTDOWN))
1278
return;
1279
1280
kvm_clear_request(KVM_REQ_TRIPLE_FAULT, vcpu);
1281
nested_svm_simple_vmexit(to_svm(vcpu), SVM_EXIT_SHUTDOWN);
1282
}
1283
1284
int svm_allocate_nested(struct vcpu_svm *svm)
1285
{
1286
struct page *vmcb02_page;
1287
1288
if (svm->nested.initialized)
1289
return 0;
1290
1291
vmcb02_page = snp_safe_alloc_page();
1292
if (!vmcb02_page)
1293
return -ENOMEM;
1294
svm->nested.vmcb02.ptr = page_address(vmcb02_page);
1295
svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT);
1296
1297
svm->nested.msrpm = svm_vcpu_alloc_msrpm();
1298
if (!svm->nested.msrpm)
1299
goto err_free_vmcb02;
1300
1301
svm->nested.initialized = true;
1302
return 0;
1303
1304
err_free_vmcb02:
1305
__free_page(vmcb02_page);
1306
return -ENOMEM;
1307
}
1308
1309
void svm_free_nested(struct vcpu_svm *svm)
1310
{
1311
if (!svm->nested.initialized)
1312
return;
1313
1314
if (WARN_ON_ONCE(svm->vmcb != svm->vmcb01.ptr))
1315
svm_switch_vmcb(svm, &svm->vmcb01);
1316
1317
svm_vcpu_free_msrpm(svm->nested.msrpm);
1318
svm->nested.msrpm = NULL;
1319
1320
__free_page(virt_to_page(svm->nested.vmcb02.ptr));
1321
svm->nested.vmcb02.ptr = NULL;
1322
1323
/*
1324
* When last_vmcb12_gpa matches the current vmcb12 gpa,
1325
* some vmcb12 fields are not loaded if they are marked clean
1326
* in the vmcb12, since in this case they are up to date already.
1327
*
1328
* When the vmcb02 is freed, this optimization becomes invalid.
1329
*/
1330
svm->nested.last_vmcb12_gpa = INVALID_GPA;
1331
1332
svm->nested.initialized = false;
1333
}
1334
1335
void svm_leave_nested(struct kvm_vcpu *vcpu)
1336
{
1337
struct vcpu_svm *svm = to_svm(vcpu);
1338
1339
if (is_guest_mode(vcpu)) {
1340
svm->nested.nested_run_pending = 0;
1341
svm->nested.vmcb12_gpa = INVALID_GPA;
1342
1343
leave_guest_mode(vcpu);
1344
1345
svm_switch_vmcb(svm, &svm->vmcb01);
1346
1347
nested_svm_uninit_mmu_context(vcpu);
1348
vmcb_mark_all_dirty(svm->vmcb);
1349
1350
if (kvm_apicv_activated(vcpu->kvm))
1351
kvm_make_request(KVM_REQ_APICV_UPDATE, vcpu);
1352
}
1353
1354
kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
1355
}
1356
1357
static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
1358
{
1359
gpa_t base = svm->nested.ctl.msrpm_base_pa;
1360
int write, bit_nr;
1361
u8 value, mask;
1362
u32 msr;
1363
1364
if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
1365
return NESTED_EXIT_HOST;
1366
1367
msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1368
bit_nr = svm_msrpm_bit_nr(msr);
1369
write = svm->vmcb->control.exit_info_1 & 1;
1370
1371
if (bit_nr < 0)
1372
return NESTED_EXIT_DONE;
1373
1374
if (kvm_vcpu_read_guest(&svm->vcpu, base + bit_nr / BITS_PER_BYTE,
1375
&value, sizeof(value)))
1376
return NESTED_EXIT_DONE;
1377
1378
mask = BIT(write) << (bit_nr & (BITS_PER_BYTE - 1));
1379
return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1380
}
1381
1382
static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
1383
{
1384
unsigned port, size, iopm_len;
1385
u16 val, mask;
1386
u8 start_bit;
1387
u64 gpa;
1388
1389
if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_IOIO_PROT)))
1390
return NESTED_EXIT_HOST;
1391
1392
port = svm->vmcb->control.exit_info_1 >> 16;
1393
size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
1394
SVM_IOIO_SIZE_SHIFT;
1395
gpa = svm->nested.ctl.iopm_base_pa + (port / 8);
1396
start_bit = port % 8;
1397
iopm_len = (start_bit + size > 8) ? 2 : 1;
1398
mask = (0xf >> (4 - size)) << start_bit;
1399
val = 0;
1400
1401
if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
1402
return NESTED_EXIT_DONE;
1403
1404
return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1405
}
1406
1407
static int nested_svm_intercept(struct vcpu_svm *svm)
1408
{
1409
u32 exit_code = svm->vmcb->control.exit_code;
1410
int vmexit = NESTED_EXIT_HOST;
1411
1412
switch (exit_code) {
1413
case SVM_EXIT_MSR:
1414
vmexit = nested_svm_exit_handled_msr(svm);
1415
break;
1416
case SVM_EXIT_IOIO:
1417
vmexit = nested_svm_intercept_ioio(svm);
1418
break;
1419
case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
1420
if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
1421
vmexit = NESTED_EXIT_DONE;
1422
break;
1423
}
1424
case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
1425
if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
1426
vmexit = NESTED_EXIT_DONE;
1427
break;
1428
}
1429
case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1430
/*
1431
* Host-intercepted exceptions have been checked already in
1432
* nested_svm_exit_special. There is nothing to do here,
1433
* the vmexit is injected by svm_check_nested_events.
1434
*/
1435
vmexit = NESTED_EXIT_DONE;
1436
break;
1437
}
1438
case SVM_EXIT_ERR: {
1439
vmexit = NESTED_EXIT_DONE;
1440
break;
1441
}
1442
default: {
1443
if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
1444
vmexit = NESTED_EXIT_DONE;
1445
}
1446
}
1447
1448
return vmexit;
1449
}
1450
1451
int nested_svm_exit_handled(struct vcpu_svm *svm)
1452
{
1453
int vmexit;
1454
1455
vmexit = nested_svm_intercept(svm);
1456
1457
if (vmexit == NESTED_EXIT_DONE)
1458
nested_svm_vmexit(svm);
1459
1460
return vmexit;
1461
}
1462
1463
int nested_svm_check_permissions(struct kvm_vcpu *vcpu)
1464
{
1465
if (!(vcpu->arch.efer & EFER_SVME) || !is_paging(vcpu)) {
1466
kvm_queue_exception(vcpu, UD_VECTOR);
1467
return 1;
1468
}
1469
1470
if (to_svm(vcpu)->vmcb->save.cpl) {
1471
kvm_inject_gp(vcpu, 0);
1472
return 1;
1473
}
1474
1475
return 0;
1476
}
1477
1478
static bool nested_svm_is_exception_vmexit(struct kvm_vcpu *vcpu, u8 vector,
1479
u32 error_code)
1480
{
1481
struct vcpu_svm *svm = to_svm(vcpu);
1482
1483
return (svm->nested.ctl.intercepts[INTERCEPT_EXCEPTION] & BIT(vector));
1484
}
1485
1486
static void nested_svm_inject_exception_vmexit(struct kvm_vcpu *vcpu)
1487
{
1488
struct kvm_queued_exception *ex = &vcpu->arch.exception_vmexit;
1489
struct vcpu_svm *svm = to_svm(vcpu);
1490
struct vmcb *vmcb = svm->vmcb;
1491
1492
vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + ex->vector;
1493
vmcb->control.exit_code_hi = 0;
1494
1495
if (ex->has_error_code)
1496
vmcb->control.exit_info_1 = ex->error_code;
1497
1498
/*
1499
* EXITINFO2 is undefined for all exception intercepts other
1500
* than #PF.
1501
*/
1502
if (ex->vector == PF_VECTOR) {
1503
if (ex->has_payload)
1504
vmcb->control.exit_info_2 = ex->payload;
1505
else
1506
vmcb->control.exit_info_2 = vcpu->arch.cr2;
1507
} else if (ex->vector == DB_VECTOR) {
1508
/* See kvm_check_and_inject_events(). */
1509
kvm_deliver_exception_payload(vcpu, ex);
1510
1511
if (vcpu->arch.dr7 & DR7_GD) {
1512
vcpu->arch.dr7 &= ~DR7_GD;
1513
kvm_update_dr7(vcpu);
1514
}
1515
} else {
1516
WARN_ON(ex->has_payload);
1517
}
1518
1519
nested_svm_vmexit(svm);
1520
}
1521
1522
static inline bool nested_exit_on_init(struct vcpu_svm *svm)
1523
{
1524
return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_INIT);
1525
}
1526
1527
static int svm_check_nested_events(struct kvm_vcpu *vcpu)
1528
{
1529
struct kvm_lapic *apic = vcpu->arch.apic;
1530
struct vcpu_svm *svm = to_svm(vcpu);
1531
/*
1532
* Only a pending nested run blocks a pending exception. If there is a
1533
* previously injected event, the pending exception occurred while said
1534
* event was being delivered and thus needs to be handled.
1535
*/
1536
bool block_nested_exceptions = svm->nested.nested_run_pending;
1537
/*
1538
* New events (not exceptions) are only recognized at instruction
1539
* boundaries. If an event needs reinjection, then KVM is handling a
1540
* VM-Exit that occurred _during_ instruction execution; new events are
1541
* blocked until the instruction completes.
1542
*/
1543
bool block_nested_events = block_nested_exceptions ||
1544
kvm_event_needs_reinjection(vcpu);
1545
1546
if (lapic_in_kernel(vcpu) &&
1547
test_bit(KVM_APIC_INIT, &apic->pending_events)) {
1548
if (block_nested_events)
1549
return -EBUSY;
1550
if (!nested_exit_on_init(svm))
1551
return 0;
1552
nested_svm_simple_vmexit(svm, SVM_EXIT_INIT);
1553
return 0;
1554
}
1555
1556
if (vcpu->arch.exception_vmexit.pending) {
1557
if (block_nested_exceptions)
1558
return -EBUSY;
1559
nested_svm_inject_exception_vmexit(vcpu);
1560
return 0;
1561
}
1562
1563
if (vcpu->arch.exception.pending) {
1564
if (block_nested_exceptions)
1565
return -EBUSY;
1566
return 0;
1567
}
1568
1569
#ifdef CONFIG_KVM_SMM
1570
if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) {
1571
if (block_nested_events)
1572
return -EBUSY;
1573
if (!nested_exit_on_smi(svm))
1574
return 0;
1575
nested_svm_simple_vmexit(svm, SVM_EXIT_SMI);
1576
return 0;
1577
}
1578
#endif
1579
1580
if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) {
1581
if (block_nested_events)
1582
return -EBUSY;
1583
if (!nested_exit_on_nmi(svm))
1584
return 0;
1585
nested_svm_simple_vmexit(svm, SVM_EXIT_NMI);
1586
return 0;
1587
}
1588
1589
if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) {
1590
if (block_nested_events)
1591
return -EBUSY;
1592
if (!nested_exit_on_intr(svm))
1593
return 0;
1594
trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
1595
nested_svm_simple_vmexit(svm, SVM_EXIT_INTR);
1596
return 0;
1597
}
1598
1599
return 0;
1600
}
1601
1602
int nested_svm_exit_special(struct vcpu_svm *svm)
1603
{
1604
u32 exit_code = svm->vmcb->control.exit_code;
1605
struct kvm_vcpu *vcpu = &svm->vcpu;
1606
1607
switch (exit_code) {
1608
case SVM_EXIT_INTR:
1609
case SVM_EXIT_NMI:
1610
case SVM_EXIT_NPF:
1611
return NESTED_EXIT_HOST;
1612
case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1613
u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
1614
1615
if (svm->vmcb01.ptr->control.intercepts[INTERCEPT_EXCEPTION] &
1616
excp_bits)
1617
return NESTED_EXIT_HOST;
1618
else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR &&
1619
svm->vcpu.arch.apf.host_apf_flags)
1620
/* Trap async PF even if not shadowing */
1621
return NESTED_EXIT_HOST;
1622
break;
1623
}
1624
case SVM_EXIT_VMMCALL:
1625
/* Hyper-V L2 TLB flush hypercall is handled by L0 */
1626
if (guest_hv_cpuid_has_l2_tlb_flush(vcpu) &&
1627
nested_svm_l2_tlb_flush_enabled(vcpu) &&
1628
kvm_hv_is_tlb_flush_hcall(vcpu))
1629
return NESTED_EXIT_HOST;
1630
break;
1631
default:
1632
break;
1633
}
1634
1635
return NESTED_EXIT_CONTINUE;
1636
}
1637
1638
void nested_svm_update_tsc_ratio_msr(struct kvm_vcpu *vcpu)
1639
{
1640
struct vcpu_svm *svm = to_svm(vcpu);
1641
1642
vcpu->arch.tsc_scaling_ratio =
1643
kvm_calc_nested_tsc_multiplier(vcpu->arch.l1_tsc_scaling_ratio,
1644
svm->tsc_ratio_msr);
1645
svm_write_tsc_multiplier(vcpu);
1646
}
1647
1648
/* Inverse operation of nested_copy_vmcb_control_to_cache(). asid is copied too. */
1649
static void nested_copy_vmcb_cache_to_control(struct vmcb_control_area *dst,
1650
struct vmcb_ctrl_area_cached *from)
1651
{
1652
unsigned int i;
1653
1654
memset(dst, 0, sizeof(struct vmcb_control_area));
1655
1656
for (i = 0; i < MAX_INTERCEPT; i++)
1657
dst->intercepts[i] = from->intercepts[i];
1658
1659
dst->iopm_base_pa = from->iopm_base_pa;
1660
dst->msrpm_base_pa = from->msrpm_base_pa;
1661
dst->tsc_offset = from->tsc_offset;
1662
dst->asid = from->asid;
1663
dst->tlb_ctl = from->tlb_ctl;
1664
dst->int_ctl = from->int_ctl;
1665
dst->int_vector = from->int_vector;
1666
dst->int_state = from->int_state;
1667
dst->exit_code = from->exit_code;
1668
dst->exit_code_hi = from->exit_code_hi;
1669
dst->exit_info_1 = from->exit_info_1;
1670
dst->exit_info_2 = from->exit_info_2;
1671
dst->exit_int_info = from->exit_int_info;
1672
dst->exit_int_info_err = from->exit_int_info_err;
1673
dst->nested_ctl = from->nested_ctl;
1674
dst->event_inj = from->event_inj;
1675
dst->event_inj_err = from->event_inj_err;
1676
dst->next_rip = from->next_rip;
1677
dst->nested_cr3 = from->nested_cr3;
1678
dst->virt_ext = from->virt_ext;
1679
dst->pause_filter_count = from->pause_filter_count;
1680
dst->pause_filter_thresh = from->pause_filter_thresh;
1681
/* 'clean' and 'hv_enlightenments' are not changed by KVM */
1682
}
1683
1684
static int svm_get_nested_state(struct kvm_vcpu *vcpu,
1685
struct kvm_nested_state __user *user_kvm_nested_state,
1686
u32 user_data_size)
1687
{
1688
struct vcpu_svm *svm;
1689
struct vmcb_control_area *ctl;
1690
unsigned long r;
1691
struct kvm_nested_state kvm_state = {
1692
.flags = 0,
1693
.format = KVM_STATE_NESTED_FORMAT_SVM,
1694
.size = sizeof(kvm_state),
1695
};
1696
struct vmcb __user *user_vmcb = (struct vmcb __user *)
1697
&user_kvm_nested_state->data.svm[0];
1698
1699
if (!vcpu)
1700
return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE;
1701
1702
svm = to_svm(vcpu);
1703
1704
if (user_data_size < kvm_state.size)
1705
goto out;
1706
1707
/* First fill in the header and copy it out. */
1708
if (is_guest_mode(vcpu)) {
1709
kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa;
1710
kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE;
1711
kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
1712
1713
if (svm->nested.nested_run_pending)
1714
kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
1715
}
1716
1717
if (gif_set(svm))
1718
kvm_state.flags |= KVM_STATE_NESTED_GIF_SET;
1719
1720
if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
1721
return -EFAULT;
1722
1723
if (!is_guest_mode(vcpu))
1724
goto out;
1725
1726
/*
1727
* Copy over the full size of the VMCB rather than just the size
1728
* of the structs.
1729
*/
1730
if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE))
1731
return -EFAULT;
1732
1733
ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
1734
if (!ctl)
1735
return -ENOMEM;
1736
1737
nested_copy_vmcb_cache_to_control(ctl, &svm->nested.ctl);
1738
r = copy_to_user(&user_vmcb->control, ctl,
1739
sizeof(user_vmcb->control));
1740
kfree(ctl);
1741
if (r)
1742
return -EFAULT;
1743
1744
if (copy_to_user(&user_vmcb->save, &svm->vmcb01.ptr->save,
1745
sizeof(user_vmcb->save)))
1746
return -EFAULT;
1747
out:
1748
return kvm_state.size;
1749
}
1750
1751
static int svm_set_nested_state(struct kvm_vcpu *vcpu,
1752
struct kvm_nested_state __user *user_kvm_nested_state,
1753
struct kvm_nested_state *kvm_state)
1754
{
1755
struct vcpu_svm *svm = to_svm(vcpu);
1756
struct vmcb __user *user_vmcb = (struct vmcb __user *)
1757
&user_kvm_nested_state->data.svm[0];
1758
struct vmcb_control_area *ctl;
1759
struct vmcb_save_area *save;
1760
struct vmcb_save_area_cached save_cached;
1761
struct vmcb_ctrl_area_cached ctl_cached;
1762
unsigned long cr0;
1763
int ret;
1764
1765
BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >
1766
KVM_STATE_NESTED_SVM_VMCB_SIZE);
1767
1768
if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM)
1769
return -EINVAL;
1770
1771
if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE |
1772
KVM_STATE_NESTED_RUN_PENDING |
1773
KVM_STATE_NESTED_GIF_SET))
1774
return -EINVAL;
1775
1776
/*
1777
* If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
1778
* EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
1779
*/
1780
if (!(vcpu->arch.efer & EFER_SVME)) {
1781
/* GIF=1 and no guest mode are required if SVME=0. */
1782
if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
1783
return -EINVAL;
1784
}
1785
1786
/* SMM temporarily disables SVM, so we cannot be in guest mode. */
1787
if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
1788
return -EINVAL;
1789
1790
if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) {
1791
svm_leave_nested(vcpu);
1792
svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1793
return 0;
1794
}
1795
1796
if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa))
1797
return -EINVAL;
1798
if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)
1799
return -EINVAL;
1800
1801
ret = -ENOMEM;
1802
ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
1803
save = kzalloc(sizeof(*save), GFP_KERNEL);
1804
if (!ctl || !save)
1805
goto out_free;
1806
1807
ret = -EFAULT;
1808
if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl)))
1809
goto out_free;
1810
if (copy_from_user(save, &user_vmcb->save, sizeof(*save)))
1811
goto out_free;
1812
1813
ret = -EINVAL;
1814
__nested_copy_vmcb_control_to_cache(vcpu, &ctl_cached, ctl);
1815
if (!__nested_vmcb_check_controls(vcpu, &ctl_cached))
1816
goto out_free;
1817
1818
/*
1819
* Processor state contains L2 state. Check that it is
1820
* valid for guest mode (see nested_vmcb_check_save).
1821
*/
1822
cr0 = kvm_read_cr0(vcpu);
1823
if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
1824
goto out_free;
1825
1826
/*
1827
* Validate host state saved from before VMRUN (see
1828
* nested_svm_check_permissions).
1829
*/
1830
__nested_copy_vmcb_save_to_cache(&save_cached, save);
1831
if (!(save->cr0 & X86_CR0_PG) ||
1832
!(save->cr0 & X86_CR0_PE) ||
1833
(save->rflags & X86_EFLAGS_VM) ||
1834
!__nested_vmcb_check_save(vcpu, &save_cached))
1835
goto out_free;
1836
1837
1838
/*
1839
* All checks done, we can enter guest mode. Userspace provides
1840
* vmcb12.control, which will be combined with L1 and stored into
1841
* vmcb02, and the L1 save state which we store in vmcb01.
1842
* L2 registers if needed are moved from the current VMCB to VMCB02.
1843
*/
1844
1845
if (is_guest_mode(vcpu))
1846
svm_leave_nested(vcpu);
1847
else
1848
svm->nested.vmcb02.ptr->save = svm->vmcb01.ptr->save;
1849
1850
svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1851
1852
svm->nested.nested_run_pending =
1853
!!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
1854
1855
svm->nested.vmcb12_gpa = kvm_state->hdr.svm.vmcb_pa;
1856
1857
svm_copy_vmrun_state(&svm->vmcb01.ptr->save, save);
1858
nested_copy_vmcb_control_to_cache(svm, ctl);
1859
1860
svm_switch_vmcb(svm, &svm->nested.vmcb02);
1861
nested_vmcb02_prepare_control(svm, svm->vmcb->save.rip, svm->vmcb->save.cs.base);
1862
1863
/*
1864
* While the nested guest CR3 is already checked and set by
1865
* KVM_SET_SREGS, it was set when nested state was yet loaded,
1866
* thus MMU might not be initialized correctly.
1867
* Set it again to fix this.
1868
*/
1869
1870
ret = nested_svm_load_cr3(&svm->vcpu, vcpu->arch.cr3,
1871
nested_npt_enabled(svm), false);
1872
if (WARN_ON_ONCE(ret))
1873
goto out_free;
1874
1875
svm->nested.force_msr_bitmap_recalc = true;
1876
1877
kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
1878
ret = 0;
1879
out_free:
1880
kfree(save);
1881
kfree(ctl);
1882
1883
return ret;
1884
}
1885
1886
static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)
1887
{
1888
if (WARN_ON(!is_guest_mode(vcpu)))
1889
return true;
1890
1891
if (!vcpu->arch.pdptrs_from_userspace &&
1892
!nested_npt_enabled(to_svm(vcpu)) && is_pae_paging(vcpu))
1893
/*
1894
* Reload the guest's PDPTRs since after a migration
1895
* the guest CR3 might be restored prior to setting the nested
1896
* state which can lead to a load of wrong PDPTRs.
1897
*/
1898
if (CC(!load_pdptrs(vcpu, vcpu->arch.cr3)))
1899
return false;
1900
1901
if (!nested_svm_merge_msrpm(vcpu)) {
1902
vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1903
vcpu->run->internal.suberror =
1904
KVM_INTERNAL_ERROR_EMULATION;
1905
vcpu->run->internal.ndata = 0;
1906
return false;
1907
}
1908
1909
if (kvm_hv_verify_vp_assist(vcpu))
1910
return false;
1911
1912
return true;
1913
}
1914
1915
struct kvm_x86_nested_ops svm_nested_ops = {
1916
.leave_nested = svm_leave_nested,
1917
.is_exception_vmexit = nested_svm_is_exception_vmexit,
1918
.check_events = svm_check_nested_events,
1919
.triple_fault = nested_svm_triple_fault,
1920
.get_nested_state_pages = svm_get_nested_state_pages,
1921
.get_state = svm_get_nested_state,
1922
.set_state = svm_set_nested_state,
1923
.hv_inject_synthetic_vmexit_post_tlb_flush = svm_hv_inject_synthetic_vmexit_post_tlb_flush,
1924
};
1925
1926