OSDN Git Service

Merge branch 'kvm-fix-svm-races' into HEAD
[tomoyo/tomoyo-test1.git] / arch / x86 / kvm / svm / nested.c
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  <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>
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 "cpuid.h"
29 #include "lapic.h"
30 #include "svm.h"
31
32 #define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK
33
34 static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
35                                        struct x86_exception *fault)
36 {
37         struct vcpu_svm *svm = to_svm(vcpu);
38
39         if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
40                 /*
41                  * TODO: track the cause of the nested page fault, and
42                  * correctly fill in the high bits of exit_info_1.
43                  */
44                 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
45                 svm->vmcb->control.exit_code_hi = 0;
46                 svm->vmcb->control.exit_info_1 = (1ULL << 32);
47                 svm->vmcb->control.exit_info_2 = fault->address;
48         }
49
50         svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
51         svm->vmcb->control.exit_info_1 |= fault->error_code;
52
53         nested_svm_vmexit(svm);
54 }
55
56 static void svm_inject_page_fault_nested(struct kvm_vcpu *vcpu, struct x86_exception *fault)
57 {
58        struct vcpu_svm *svm = to_svm(vcpu);
59        WARN_ON(!is_guest_mode(vcpu));
60
61        if (vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_EXCEPTION_OFFSET + PF_VECTOR) &&
62            !svm->nested.nested_run_pending) {
63                svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + PF_VECTOR;
64                svm->vmcb->control.exit_code_hi = 0;
65                svm->vmcb->control.exit_info_1 = fault->error_code;
66                svm->vmcb->control.exit_info_2 = fault->address;
67                nested_svm_vmexit(svm);
68        } else {
69                kvm_inject_page_fault(vcpu, fault);
70        }
71 }
72
73 static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
74 {
75         struct vcpu_svm *svm = to_svm(vcpu);
76         u64 cr3 = svm->nested.ctl.nested_cr3;
77         u64 pdpte;
78         int ret;
79
80         ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte,
81                                        offset_in_page(cr3) + index * 8, 8);
82         if (ret)
83                 return 0;
84         return pdpte;
85 }
86
87 static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
88 {
89         struct vcpu_svm *svm = to_svm(vcpu);
90
91         return svm->nested.ctl.nested_cr3;
92 }
93
94 static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
95 {
96         struct vcpu_svm *svm = to_svm(vcpu);
97
98         WARN_ON(mmu_is_nested(vcpu));
99
100         vcpu->arch.mmu = &vcpu->arch.guest_mmu;
101         kvm_init_shadow_npt_mmu(vcpu, X86_CR0_PG, svm->vmcb01.ptr->save.cr4,
102                                 svm->vmcb01.ptr->save.efer,
103                                 svm->nested.ctl.nested_cr3);
104         vcpu->arch.mmu->get_guest_pgd     = nested_svm_get_tdp_cr3;
105         vcpu->arch.mmu->get_pdptr         = nested_svm_get_tdp_pdptr;
106         vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit;
107         reset_shadow_zero_bits_mask(vcpu, vcpu->arch.mmu);
108         vcpu->arch.walk_mmu              = &vcpu->arch.nested_mmu;
109 }
110
111 static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
112 {
113         vcpu->arch.mmu = &vcpu->arch.root_mmu;
114         vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
115 }
116
117 void recalc_intercepts(struct vcpu_svm *svm)
118 {
119         struct vmcb_control_area *c, *h, *g;
120         unsigned int i;
121
122         vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
123
124         if (!is_guest_mode(&svm->vcpu))
125                 return;
126
127         c = &svm->vmcb->control;
128         h = &svm->vmcb01.ptr->control;
129         g = &svm->nested.ctl;
130
131         for (i = 0; i < MAX_INTERCEPT; i++)
132                 c->intercepts[i] = h->intercepts[i];
133
134         if (g->int_ctl & V_INTR_MASKING_MASK) {
135                 /* We only want the cr8 intercept bits of L1 */
136                 vmcb_clr_intercept(c, INTERCEPT_CR8_READ);
137                 vmcb_clr_intercept(c, INTERCEPT_CR8_WRITE);
138
139                 /*
140                  * Once running L2 with HF_VINTR_MASK, EFLAGS.IF does not
141                  * affect any interrupt we may want to inject; therefore,
142                  * interrupt window vmexits are irrelevant to L0.
143                  */
144                 vmcb_clr_intercept(c, INTERCEPT_VINTR);
145         }
146
147         /* We don't want to see VMMCALLs from a nested guest */
148         vmcb_clr_intercept(c, INTERCEPT_VMMCALL);
149
150         for (i = 0; i < MAX_INTERCEPT; i++)
151                 c->intercepts[i] |= g->intercepts[i];
152 }
153
154 static void copy_vmcb_control_area(struct vmcb_control_area *dst,
155                                    struct vmcb_control_area *from)
156 {
157         unsigned int i;
158
159         for (i = 0; i < MAX_INTERCEPT; i++)
160                 dst->intercepts[i] = from->intercepts[i];
161
162         dst->iopm_base_pa         = from->iopm_base_pa;
163         dst->msrpm_base_pa        = from->msrpm_base_pa;
164         dst->tsc_offset           = from->tsc_offset;
165         /* asid not copied, it is handled manually for svm->vmcb.  */
166         dst->tlb_ctl              = from->tlb_ctl;
167         dst->int_ctl              = from->int_ctl;
168         dst->int_vector           = from->int_vector;
169         dst->int_state            = from->int_state;
170         dst->exit_code            = from->exit_code;
171         dst->exit_code_hi         = from->exit_code_hi;
172         dst->exit_info_1          = from->exit_info_1;
173         dst->exit_info_2          = from->exit_info_2;
174         dst->exit_int_info        = from->exit_int_info;
175         dst->exit_int_info_err    = from->exit_int_info_err;
176         dst->nested_ctl           = from->nested_ctl;
177         dst->event_inj            = from->event_inj;
178         dst->event_inj_err        = from->event_inj_err;
179         dst->nested_cr3           = from->nested_cr3;
180         dst->virt_ext              = from->virt_ext;
181         dst->pause_filter_count   = from->pause_filter_count;
182         dst->pause_filter_thresh  = from->pause_filter_thresh;
183 }
184
185 static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
186 {
187         /*
188          * This function merges the msr permission bitmaps of kvm and the
189          * nested vmcb. It is optimized in that it only merges the parts where
190          * the kvm msr permission bitmap may contain zero bits
191          */
192         int i;
193
194         if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
195                 return true;
196
197         for (i = 0; i < MSRPM_OFFSETS; i++) {
198                 u32 value, p;
199                 u64 offset;
200
201                 if (msrpm_offsets[i] == 0xffffffff)
202                         break;
203
204                 p      = msrpm_offsets[i];
205                 offset = svm->nested.ctl.msrpm_base_pa + (p * 4);
206
207                 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
208                         return false;
209
210                 svm->nested.msrpm[p] = svm->msrpm[p] | value;
211         }
212
213         svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
214
215         return true;
216 }
217
218 static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)
219 {
220         struct vcpu_svm *svm = to_svm(vcpu);
221
222         if (WARN_ON(!is_guest_mode(vcpu)))
223                 return true;
224
225         if (!nested_svm_vmrun_msrpm(svm)) {
226                 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
227                 vcpu->run->internal.suberror =
228                         KVM_INTERNAL_ERROR_EMULATION;
229                 vcpu->run->internal.ndata = 0;
230                 return false;
231         }
232
233         return true;
234 }
235
236 static bool nested_vmcb_check_controls(struct vmcb_control_area *control)
237 {
238         if (CC(!vmcb_is_intercept(control, INTERCEPT_VMRUN)))
239                 return false;
240
241         if (CC(control->asid == 0))
242                 return false;
243
244         if (CC((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) && !npt_enabled))
245                 return false;
246
247         return true;
248 }
249
250 static bool nested_vmcb_check_cr3_cr4(struct kvm_vcpu *vcpu,
251                                       struct vmcb_save_area *save)
252 {
253         /*
254          * These checks are also performed by KVM_SET_SREGS,
255          * except that EFER.LMA is not checked by SVM against
256          * CR0.PG && EFER.LME.
257          */
258         if ((save->efer & EFER_LME) && (save->cr0 & X86_CR0_PG)) {
259                 if (CC(!(save->cr4 & X86_CR4_PAE)) ||
260                     CC(!(save->cr0 & X86_CR0_PE)) ||
261                     CC(kvm_vcpu_is_illegal_gpa(vcpu, save->cr3)))
262                         return false;
263         }
264
265         if (CC(!kvm_is_valid_cr4(vcpu, save->cr4)))
266                 return false;
267
268         return true;
269 }
270
271 /* Common checks that apply to both L1 and L2 state.  */
272 static bool nested_vmcb_valid_sregs(struct kvm_vcpu *vcpu,
273                                     struct vmcb_save_area *save)
274 {
275         /*
276          * FIXME: these should be done after copying the fields,
277          * to avoid TOC/TOU races.  For these save area checks
278          * the possible damage is limited since kvm_set_cr0 and
279          * kvm_set_cr4 handle failure; EFER_SVME is an exception
280          * so it is force-set later in nested_prepare_vmcb_save.
281          */
282         if (CC(!(save->efer & EFER_SVME)))
283                 return false;
284
285         if (CC((save->cr0 & X86_CR0_CD) == 0 && (save->cr0 & X86_CR0_NW)) ||
286             CC(save->cr0 & ~0xffffffffULL))
287                 return false;
288
289         if (CC(!kvm_dr6_valid(save->dr6)) || CC(!kvm_dr7_valid(save->dr7)))
290                 return false;
291
292         if (!nested_vmcb_check_cr3_cr4(vcpu, save))
293                 return false;
294
295         if (CC(!kvm_valid_efer(vcpu, save->efer)))
296                 return false;
297
298         return true;
299 }
300
301 static void nested_load_control_from_vmcb12(struct vcpu_svm *svm,
302                                             struct vmcb_control_area *control)
303 {
304         copy_vmcb_control_area(&svm->nested.ctl, control);
305
306         /* Copy it here because nested_svm_check_controls will check it.  */
307         svm->nested.ctl.asid           = control->asid;
308         svm->nested.ctl.msrpm_base_pa &= ~0x0fffULL;
309         svm->nested.ctl.iopm_base_pa  &= ~0x0fffULL;
310 }
311
312 /*
313  * Synchronize fields that are written by the processor, so that
314  * they can be copied back into the vmcb12.
315  */
316 void nested_sync_control_from_vmcb02(struct vcpu_svm *svm)
317 {
318         u32 mask;
319         svm->nested.ctl.event_inj      = svm->vmcb->control.event_inj;
320         svm->nested.ctl.event_inj_err  = svm->vmcb->control.event_inj_err;
321
322         /* Only a few fields of int_ctl are written by the processor.  */
323         mask = V_IRQ_MASK | V_TPR_MASK;
324         if (!(svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) &&
325             svm_is_intercept(svm, INTERCEPT_VINTR)) {
326                 /*
327                  * In order to request an interrupt window, L0 is usurping
328                  * svm->vmcb->control.int_ctl and possibly setting V_IRQ
329                  * even if it was clear in L1's VMCB.  Restoring it would be
330                  * wrong.  However, in this case V_IRQ will remain true until
331                  * interrupt_window_interception calls svm_clear_vintr and
332                  * restores int_ctl.  We can just leave it aside.
333                  */
334                 mask &= ~V_IRQ_MASK;
335         }
336         svm->nested.ctl.int_ctl        &= ~mask;
337         svm->nested.ctl.int_ctl        |= svm->vmcb->control.int_ctl & mask;
338 }
339
340 /*
341  * Transfer any event that L0 or L1 wanted to inject into L2 to
342  * EXIT_INT_INFO.
343  */
344 static void nested_save_pending_event_to_vmcb12(struct vcpu_svm *svm,
345                                                 struct vmcb *vmcb12)
346 {
347         struct kvm_vcpu *vcpu = &svm->vcpu;
348         u32 exit_int_info = 0;
349         unsigned int nr;
350
351         if (vcpu->arch.exception.injected) {
352                 nr = vcpu->arch.exception.nr;
353                 exit_int_info = nr | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT;
354
355                 if (vcpu->arch.exception.has_error_code) {
356                         exit_int_info |= SVM_EVTINJ_VALID_ERR;
357                         vmcb12->control.exit_int_info_err =
358                                 vcpu->arch.exception.error_code;
359                 }
360
361         } else if (vcpu->arch.nmi_injected) {
362                 exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
363
364         } else if (vcpu->arch.interrupt.injected) {
365                 nr = vcpu->arch.interrupt.nr;
366                 exit_int_info = nr | SVM_EVTINJ_VALID;
367
368                 if (vcpu->arch.interrupt.soft)
369                         exit_int_info |= SVM_EVTINJ_TYPE_SOFT;
370                 else
371                         exit_int_info |= SVM_EVTINJ_TYPE_INTR;
372         }
373
374         vmcb12->control.exit_int_info = exit_int_info;
375 }
376
377 static inline bool nested_npt_enabled(struct vcpu_svm *svm)
378 {
379         return svm->nested.ctl.nested_ctl & SVM_NESTED_CTL_NP_ENABLE;
380 }
381
382 /*
383  * Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true
384  * if we are emulating VM-Entry into a guest with NPT enabled.
385  */
386 static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
387                                bool nested_npt)
388 {
389         if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3)))
390                 return -EINVAL;
391
392         if (!nested_npt && is_pae_paging(vcpu) &&
393             (cr3 != kvm_read_cr3(vcpu) || pdptrs_changed(vcpu))) {
394                 if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)))
395                         return -EINVAL;
396         }
397
398         /*
399          * TODO: optimize unconditional TLB flush/MMU sync here and in
400          * kvm_init_shadow_npt_mmu().
401          */
402         if (!nested_npt)
403                 kvm_mmu_new_pgd(vcpu, cr3, false, false);
404
405         vcpu->arch.cr3 = cr3;
406         kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
407
408         kvm_init_mmu(vcpu, false);
409
410         return 0;
411 }
412
413 void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm)
414 {
415         if (!svm->nested.vmcb02.ptr)
416                 return;
417
418         /* FIXME: merge g_pat from vmcb01 and vmcb12.  */
419         svm->nested.vmcb02.ptr->save.g_pat = svm->vmcb01.ptr->save.g_pat;
420 }
421
422 static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12)
423 {
424         bool new_vmcb12 = false;
425
426         nested_vmcb02_compute_g_pat(svm);
427
428         /* Load the nested guest state */
429
430         if (svm->nested.vmcb12_gpa != svm->nested.last_vmcb12_gpa) {
431                 new_vmcb12 = true;
432                 svm->nested.last_vmcb12_gpa = svm->nested.vmcb12_gpa;
433         }
434
435         if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_SEG))) {
436                 svm->vmcb->save.es = vmcb12->save.es;
437                 svm->vmcb->save.cs = vmcb12->save.cs;
438                 svm->vmcb->save.ss = vmcb12->save.ss;
439                 svm->vmcb->save.ds = vmcb12->save.ds;
440                 svm->vmcb->save.cpl = vmcb12->save.cpl;
441                 vmcb_mark_dirty(svm->vmcb, VMCB_SEG);
442         }
443
444         if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DT))) {
445                 svm->vmcb->save.gdtr = vmcb12->save.gdtr;
446                 svm->vmcb->save.idtr = vmcb12->save.idtr;
447                 vmcb_mark_dirty(svm->vmcb, VMCB_DT);
448         }
449
450         kvm_set_rflags(&svm->vcpu, vmcb12->save.rflags | X86_EFLAGS_FIXED);
451
452         /*
453          * Force-set EFER_SVME even though it is checked earlier on the
454          * VMCB12, because the guest can flip the bit between the check
455          * and now.  Clearing EFER_SVME would call svm_free_nested.
456          */
457         svm_set_efer(&svm->vcpu, vmcb12->save.efer | EFER_SVME);
458
459         svm_set_cr0(&svm->vcpu, vmcb12->save.cr0);
460         svm_set_cr4(&svm->vcpu, vmcb12->save.cr4);
461
462         svm->vcpu.arch.cr2 = vmcb12->save.cr2;
463
464         kvm_rax_write(&svm->vcpu, vmcb12->save.rax);
465         kvm_rsp_write(&svm->vcpu, vmcb12->save.rsp);
466         kvm_rip_write(&svm->vcpu, vmcb12->save.rip);
467
468         /* In case we don't even reach vcpu_run, the fields are not updated */
469         svm->vmcb->save.rax = vmcb12->save.rax;
470         svm->vmcb->save.rsp = vmcb12->save.rsp;
471         svm->vmcb->save.rip = vmcb12->save.rip;
472
473         /* These bits will be set properly on the first execution when new_vmc12 is true */
474         if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DR))) {
475                 svm->vmcb->save.dr7 = vmcb12->save.dr7 | DR7_FIXED_1;
476                 svm->vcpu.arch.dr6  = vmcb12->save.dr6 | DR6_ACTIVE_LOW;
477                 vmcb_mark_dirty(svm->vmcb, VMCB_DR);
478         }
479 }
480
481 static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
482 {
483         const u32 mask = V_INTR_MASKING_MASK | V_GIF_ENABLE_MASK | V_GIF_MASK;
484
485         /*
486          * Filled at exit: exit_code, exit_code_hi, exit_info_1, exit_info_2,
487          * exit_int_info, exit_int_info_err, next_rip, insn_len, insn_bytes.
488          */
489
490         /*
491          * Also covers avic_vapic_bar, avic_backing_page, avic_logical_id,
492          * avic_physical_id.
493          */
494         WARN_ON(svm->vmcb01.ptr->control.int_ctl & AVIC_ENABLE_MASK);
495
496         /* Copied from vmcb01.  msrpm_base can be overwritten later.  */
497         svm->vmcb->control.nested_ctl = svm->vmcb01.ptr->control.nested_ctl;
498         svm->vmcb->control.iopm_base_pa = svm->vmcb01.ptr->control.iopm_base_pa;
499         svm->vmcb->control.msrpm_base_pa = svm->vmcb01.ptr->control.msrpm_base_pa;
500
501         /* Done at vmrun: asid.  */
502
503         /* Also overwritten later if necessary.  */
504         svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
505
506         /* nested_cr3.  */
507         if (nested_npt_enabled(svm))
508                 nested_svm_init_mmu_context(&svm->vcpu);
509
510         svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset =
511                 svm->vcpu.arch.l1_tsc_offset + svm->nested.ctl.tsc_offset;
512
513         svm->vmcb->control.int_ctl             =
514                 (svm->nested.ctl.int_ctl & ~mask) |
515                 (svm->vmcb01.ptr->control.int_ctl & mask);
516
517         svm->vmcb->control.virt_ext            = svm->nested.ctl.virt_ext;
518         svm->vmcb->control.int_vector          = svm->nested.ctl.int_vector;
519         svm->vmcb->control.int_state           = svm->nested.ctl.int_state;
520         svm->vmcb->control.event_inj           = svm->nested.ctl.event_inj;
521         svm->vmcb->control.event_inj_err       = svm->nested.ctl.event_inj_err;
522
523         svm->vmcb->control.pause_filter_count  = svm->nested.ctl.pause_filter_count;
524         svm->vmcb->control.pause_filter_thresh = svm->nested.ctl.pause_filter_thresh;
525
526         /* Enter Guest-Mode */
527         enter_guest_mode(&svm->vcpu);
528
529         /*
530          * Merge guest and host intercepts - must be called with vcpu in
531          * guest-mode to take effect.
532          */
533         recalc_intercepts(svm);
534 }
535
536 static void nested_svm_copy_common_state(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
537 {
538         /*
539          * Some VMCB state is shared between L1 and L2 and thus has to be
540          * moved at the time of nested vmrun and vmexit.
541          *
542          * VMLOAD/VMSAVE state would also belong in this category, but KVM
543          * always performs VMLOAD and VMSAVE from the VMCB01.
544          */
545         to_vmcb->save.spec_ctrl = from_vmcb->save.spec_ctrl;
546 }
547
548 int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa,
549                          struct vmcb *vmcb12)
550 {
551         struct vcpu_svm *svm = to_svm(vcpu);
552         int ret;
553
554         trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb12_gpa,
555                                vmcb12->save.rip,
556                                vmcb12->control.int_ctl,
557                                vmcb12->control.event_inj,
558                                vmcb12->control.nested_ctl);
559
560         trace_kvm_nested_intercepts(vmcb12->control.intercepts[INTERCEPT_CR] & 0xffff,
561                                     vmcb12->control.intercepts[INTERCEPT_CR] >> 16,
562                                     vmcb12->control.intercepts[INTERCEPT_EXCEPTION],
563                                     vmcb12->control.intercepts[INTERCEPT_WORD3],
564                                     vmcb12->control.intercepts[INTERCEPT_WORD4],
565                                     vmcb12->control.intercepts[INTERCEPT_WORD5]);
566
567
568         svm->nested.vmcb12_gpa = vmcb12_gpa;
569
570         WARN_ON(svm->vmcb == svm->nested.vmcb02.ptr);
571
572         nested_svm_copy_common_state(svm->vmcb01.ptr, svm->nested.vmcb02.ptr);
573
574         svm_switch_vmcb(svm, &svm->nested.vmcb02);
575         nested_vmcb02_prepare_control(svm);
576         nested_vmcb02_prepare_save(svm, vmcb12);
577
578         ret = nested_svm_load_cr3(&svm->vcpu, vmcb12->save.cr3,
579                                   nested_npt_enabled(svm));
580         if (ret)
581                 return ret;
582
583         if (!npt_enabled)
584                 vcpu->arch.mmu->inject_page_fault = svm_inject_page_fault_nested;
585
586         svm_set_gif(svm, true);
587
588         return 0;
589 }
590
591 int nested_svm_vmrun(struct kvm_vcpu *vcpu)
592 {
593         struct vcpu_svm *svm = to_svm(vcpu);
594         int ret;
595         struct vmcb *vmcb12;
596         struct kvm_host_map map;
597         u64 vmcb12_gpa;
598
599         ++vcpu->stat.nested_run;
600
601         if (is_smm(vcpu)) {
602                 kvm_queue_exception(vcpu, UD_VECTOR);
603                 return 1;
604         }
605
606         vmcb12_gpa = svm->vmcb->save.rax;
607         ret = kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map);
608         if (ret == -EINVAL) {
609                 kvm_inject_gp(vcpu, 0);
610                 return 1;
611         } else if (ret) {
612                 return kvm_skip_emulated_instruction(vcpu);
613         }
614
615         ret = kvm_skip_emulated_instruction(vcpu);
616
617         vmcb12 = map.hva;
618
619         if (WARN_ON_ONCE(!svm->nested.initialized))
620                 return -EINVAL;
621
622         nested_load_control_from_vmcb12(svm, &vmcb12->control);
623
624         if (!nested_vmcb_valid_sregs(vcpu, &vmcb12->save) ||
625             !nested_vmcb_check_controls(&svm->nested.ctl)) {
626                 vmcb12->control.exit_code    = SVM_EXIT_ERR;
627                 vmcb12->control.exit_code_hi = 0;
628                 vmcb12->control.exit_info_1  = 0;
629                 vmcb12->control.exit_info_2  = 0;
630                 goto out;
631         }
632
633
634         /* Clear internal status */
635         kvm_clear_exception_queue(vcpu);
636         kvm_clear_interrupt_queue(vcpu);
637
638         /*
639          * Since vmcb01 is not in use, we can use it to store some of the L1
640          * state.
641          */
642         svm->vmcb01.ptr->save.efer   = vcpu->arch.efer;
643         svm->vmcb01.ptr->save.cr0    = kvm_read_cr0(vcpu);
644         svm->vmcb01.ptr->save.cr4    = vcpu->arch.cr4;
645         svm->vmcb01.ptr->save.rflags = kvm_get_rflags(vcpu);
646         svm->vmcb01.ptr->save.rip    = kvm_rip_read(vcpu);
647
648         if (!npt_enabled)
649                 svm->vmcb01.ptr->save.cr3 = kvm_read_cr3(vcpu);
650
651         svm->nested.nested_run_pending = 1;
652
653         if (enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12))
654                 goto out_exit_err;
655
656         if (nested_svm_vmrun_msrpm(svm))
657                 goto out;
658
659 out_exit_err:
660         svm->nested.nested_run_pending = 0;
661
662         svm->vmcb->control.exit_code    = SVM_EXIT_ERR;
663         svm->vmcb->control.exit_code_hi = 0;
664         svm->vmcb->control.exit_info_1  = 0;
665         svm->vmcb->control.exit_info_2  = 0;
666
667         nested_svm_vmexit(svm);
668
669 out:
670         kvm_vcpu_unmap(vcpu, &map, true);
671
672         return ret;
673 }
674
675 void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
676 {
677         to_vmcb->save.fs = from_vmcb->save.fs;
678         to_vmcb->save.gs = from_vmcb->save.gs;
679         to_vmcb->save.tr = from_vmcb->save.tr;
680         to_vmcb->save.ldtr = from_vmcb->save.ldtr;
681         to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
682         to_vmcb->save.star = from_vmcb->save.star;
683         to_vmcb->save.lstar = from_vmcb->save.lstar;
684         to_vmcb->save.cstar = from_vmcb->save.cstar;
685         to_vmcb->save.sfmask = from_vmcb->save.sfmask;
686         to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
687         to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
688         to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
689 }
690
691 int nested_svm_vmexit(struct vcpu_svm *svm)
692 {
693         struct kvm_vcpu *vcpu = &svm->vcpu;
694         struct vmcb *vmcb12;
695         struct vmcb *vmcb = svm->vmcb;
696         struct kvm_host_map map;
697         int rc;
698
699         /* Triple faults in L2 should never escape. */
700         WARN_ON_ONCE(kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu));
701
702         rc = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.vmcb12_gpa), &map);
703         if (rc) {
704                 if (rc == -EINVAL)
705                         kvm_inject_gp(vcpu, 0);
706                 return 1;
707         }
708
709         vmcb12 = map.hva;
710
711         /* Exit Guest-Mode */
712         leave_guest_mode(vcpu);
713         svm->nested.vmcb12_gpa = 0;
714         WARN_ON_ONCE(svm->nested.nested_run_pending);
715
716         kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
717
718         /* in case we halted in L2 */
719         svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE;
720
721         /* Give the current vmcb to the guest */
722
723         vmcb12->save.es     = vmcb->save.es;
724         vmcb12->save.cs     = vmcb->save.cs;
725         vmcb12->save.ss     = vmcb->save.ss;
726         vmcb12->save.ds     = vmcb->save.ds;
727         vmcb12->save.gdtr   = vmcb->save.gdtr;
728         vmcb12->save.idtr   = vmcb->save.idtr;
729         vmcb12->save.efer   = svm->vcpu.arch.efer;
730         vmcb12->save.cr0    = kvm_read_cr0(vcpu);
731         vmcb12->save.cr3    = kvm_read_cr3(vcpu);
732         vmcb12->save.cr2    = vmcb->save.cr2;
733         vmcb12->save.cr4    = svm->vcpu.arch.cr4;
734         vmcb12->save.rflags = kvm_get_rflags(vcpu);
735         vmcb12->save.rip    = kvm_rip_read(vcpu);
736         vmcb12->save.rsp    = kvm_rsp_read(vcpu);
737         vmcb12->save.rax    = kvm_rax_read(vcpu);
738         vmcb12->save.dr7    = vmcb->save.dr7;
739         vmcb12->save.dr6    = svm->vcpu.arch.dr6;
740         vmcb12->save.cpl    = vmcb->save.cpl;
741
742         vmcb12->control.int_state         = vmcb->control.int_state;
743         vmcb12->control.exit_code         = vmcb->control.exit_code;
744         vmcb12->control.exit_code_hi      = vmcb->control.exit_code_hi;
745         vmcb12->control.exit_info_1       = vmcb->control.exit_info_1;
746         vmcb12->control.exit_info_2       = vmcb->control.exit_info_2;
747
748         if (vmcb12->control.exit_code != SVM_EXIT_ERR)
749                 nested_save_pending_event_to_vmcb12(svm, vmcb12);
750
751         if (svm->nrips_enabled)
752                 vmcb12->control.next_rip  = vmcb->control.next_rip;
753
754         vmcb12->control.int_ctl           = svm->nested.ctl.int_ctl;
755         vmcb12->control.tlb_ctl           = svm->nested.ctl.tlb_ctl;
756         vmcb12->control.event_inj         = svm->nested.ctl.event_inj;
757         vmcb12->control.event_inj_err     = svm->nested.ctl.event_inj_err;
758
759         vmcb12->control.pause_filter_count =
760                 svm->vmcb->control.pause_filter_count;
761         vmcb12->control.pause_filter_thresh =
762                 svm->vmcb->control.pause_filter_thresh;
763
764         nested_svm_copy_common_state(svm->nested.vmcb02.ptr, svm->vmcb01.ptr);
765
766         svm_switch_vmcb(svm, &svm->vmcb01);
767
768         /*
769          * On vmexit the  GIF is set to false and
770          * no event can be injected in L1.
771          */
772         svm_set_gif(svm, false);
773         svm->vmcb->control.exit_int_info = 0;
774
775         svm->vcpu.arch.tsc_offset = svm->vcpu.arch.l1_tsc_offset;
776         if (svm->vmcb->control.tsc_offset != svm->vcpu.arch.tsc_offset) {
777                 svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset;
778                 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
779         }
780
781         svm->nested.ctl.nested_cr3 = 0;
782
783         /*
784          * Restore processor state that had been saved in vmcb01
785          */
786         kvm_set_rflags(vcpu, svm->vmcb->save.rflags);
787         svm_set_efer(vcpu, svm->vmcb->save.efer);
788         svm_set_cr0(vcpu, svm->vmcb->save.cr0 | X86_CR0_PE);
789         svm_set_cr4(vcpu, svm->vmcb->save.cr4);
790         kvm_rax_write(vcpu, svm->vmcb->save.rax);
791         kvm_rsp_write(vcpu, svm->vmcb->save.rsp);
792         kvm_rip_write(vcpu, svm->vmcb->save.rip);
793
794         svm->vcpu.arch.dr7 = DR7_FIXED_1;
795         kvm_update_dr7(&svm->vcpu);
796
797         trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code,
798                                        vmcb12->control.exit_info_1,
799                                        vmcb12->control.exit_info_2,
800                                        vmcb12->control.exit_int_info,
801                                        vmcb12->control.exit_int_info_err,
802                                        KVM_ISA_SVM);
803
804         kvm_vcpu_unmap(vcpu, &map, true);
805
806         nested_svm_uninit_mmu_context(vcpu);
807
808         rc = nested_svm_load_cr3(vcpu, svm->vmcb->save.cr3, false);
809         if (rc)
810                 return 1;
811
812         /*
813          * Drop what we picked up for L2 via svm_complete_interrupts() so it
814          * doesn't end up in L1.
815          */
816         svm->vcpu.arch.nmi_injected = false;
817         kvm_clear_exception_queue(vcpu);
818         kvm_clear_interrupt_queue(vcpu);
819
820         return 0;
821 }
822
823 static void nested_svm_triple_fault(struct kvm_vcpu *vcpu)
824 {
825         nested_svm_simple_vmexit(to_svm(vcpu), SVM_EXIT_SHUTDOWN);
826 }
827
828 int svm_allocate_nested(struct vcpu_svm *svm)
829 {
830         struct page *vmcb02_page;
831
832         if (svm->nested.initialized)
833                 return 0;
834
835         vmcb02_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
836         if (!vmcb02_page)
837                 return -ENOMEM;
838         svm->nested.vmcb02.ptr = page_address(vmcb02_page);
839         svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT);
840
841         svm->nested.msrpm = svm_vcpu_alloc_msrpm();
842         if (!svm->nested.msrpm)
843                 goto err_free_vmcb02;
844         svm_vcpu_init_msrpm(&svm->vcpu, svm->nested.msrpm);
845
846         svm->nested.initialized = true;
847         return 0;
848
849 err_free_vmcb02:
850         __free_page(vmcb02_page);
851         return -ENOMEM;
852 }
853
854 void svm_free_nested(struct vcpu_svm *svm)
855 {
856         if (!svm->nested.initialized)
857                 return;
858
859         svm_vcpu_free_msrpm(svm->nested.msrpm);
860         svm->nested.msrpm = NULL;
861
862         __free_page(virt_to_page(svm->nested.vmcb02.ptr));
863         svm->nested.vmcb02.ptr = NULL;
864
865         svm->nested.initialized = false;
866 }
867
868 /*
869  * Forcibly leave nested mode in order to be able to reset the VCPU later on.
870  */
871 void svm_leave_nested(struct vcpu_svm *svm)
872 {
873         struct kvm_vcpu *vcpu = &svm->vcpu;
874
875         if (is_guest_mode(vcpu)) {
876                 svm->nested.nested_run_pending = 0;
877                 leave_guest_mode(vcpu);
878
879                 svm_switch_vmcb(svm, &svm->nested.vmcb02);
880
881                 nested_svm_uninit_mmu_context(vcpu);
882                 vmcb_mark_all_dirty(svm->vmcb);
883         }
884
885         kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
886 }
887
888 static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
889 {
890         u32 offset, msr, value;
891         int write, mask;
892
893         if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
894                 return NESTED_EXIT_HOST;
895
896         msr    = svm->vcpu.arch.regs[VCPU_REGS_RCX];
897         offset = svm_msrpm_offset(msr);
898         write  = svm->vmcb->control.exit_info_1 & 1;
899         mask   = 1 << ((2 * (msr & 0xf)) + write);
900
901         if (offset == MSR_INVALID)
902                 return NESTED_EXIT_DONE;
903
904         /* Offset is in 32 bit units but need in 8 bit units */
905         offset *= 4;
906
907         if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4))
908                 return NESTED_EXIT_DONE;
909
910         return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
911 }
912
913 static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
914 {
915         unsigned port, size, iopm_len;
916         u16 val, mask;
917         u8 start_bit;
918         u64 gpa;
919
920         if (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_IOIO_PROT)))
921                 return NESTED_EXIT_HOST;
922
923         port = svm->vmcb->control.exit_info_1 >> 16;
924         size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
925                 SVM_IOIO_SIZE_SHIFT;
926         gpa  = svm->nested.ctl.iopm_base_pa + (port / 8);
927         start_bit = port % 8;
928         iopm_len = (start_bit + size > 8) ? 2 : 1;
929         mask = (0xf >> (4 - size)) << start_bit;
930         val = 0;
931
932         if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
933                 return NESTED_EXIT_DONE;
934
935         return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
936 }
937
938 static int nested_svm_intercept(struct vcpu_svm *svm)
939 {
940         u32 exit_code = svm->vmcb->control.exit_code;
941         int vmexit = NESTED_EXIT_HOST;
942
943         switch (exit_code) {
944         case SVM_EXIT_MSR:
945                 vmexit = nested_svm_exit_handled_msr(svm);
946                 break;
947         case SVM_EXIT_IOIO:
948                 vmexit = nested_svm_intercept_ioio(svm);
949                 break;
950         case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
951                 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
952                         vmexit = NESTED_EXIT_DONE;
953                 break;
954         }
955         case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
956                 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
957                         vmexit = NESTED_EXIT_DONE;
958                 break;
959         }
960         case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
961                 /*
962                  * Host-intercepted exceptions have been checked already in
963                  * nested_svm_exit_special.  There is nothing to do here,
964                  * the vmexit is injected by svm_check_nested_events.
965                  */
966                 vmexit = NESTED_EXIT_DONE;
967                 break;
968         }
969         case SVM_EXIT_ERR: {
970                 vmexit = NESTED_EXIT_DONE;
971                 break;
972         }
973         default: {
974                 if (vmcb_is_intercept(&svm->nested.ctl, exit_code))
975                         vmexit = NESTED_EXIT_DONE;
976         }
977         }
978
979         return vmexit;
980 }
981
982 int nested_svm_exit_handled(struct vcpu_svm *svm)
983 {
984         int vmexit;
985
986         vmexit = nested_svm_intercept(svm);
987
988         if (vmexit == NESTED_EXIT_DONE)
989                 nested_svm_vmexit(svm);
990
991         return vmexit;
992 }
993
994 int nested_svm_check_permissions(struct kvm_vcpu *vcpu)
995 {
996         if (!(vcpu->arch.efer & EFER_SVME) || !is_paging(vcpu)) {
997                 kvm_queue_exception(vcpu, UD_VECTOR);
998                 return 1;
999         }
1000
1001         if (to_svm(vcpu)->vmcb->save.cpl) {
1002                 kvm_inject_gp(vcpu, 0);
1003                 return 1;
1004         }
1005
1006         return 0;
1007 }
1008
1009 static bool nested_exit_on_exception(struct vcpu_svm *svm)
1010 {
1011         unsigned int nr = svm->vcpu.arch.exception.nr;
1012
1013         return (svm->nested.ctl.intercepts[INTERCEPT_EXCEPTION] & BIT(nr));
1014 }
1015
1016 static void nested_svm_inject_exception_vmexit(struct vcpu_svm *svm)
1017 {
1018         unsigned int nr = svm->vcpu.arch.exception.nr;
1019
1020         svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1021         svm->vmcb->control.exit_code_hi = 0;
1022
1023         if (svm->vcpu.arch.exception.has_error_code)
1024                 svm->vmcb->control.exit_info_1 = svm->vcpu.arch.exception.error_code;
1025
1026         /*
1027          * EXITINFO2 is undefined for all exception intercepts other
1028          * than #PF.
1029          */
1030         if (nr == PF_VECTOR) {
1031                 if (svm->vcpu.arch.exception.nested_apf)
1032                         svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
1033                 else if (svm->vcpu.arch.exception.has_payload)
1034                         svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload;
1035                 else
1036                         svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1037         } else if (nr == DB_VECTOR) {
1038                 /* See inject_pending_event.  */
1039                 kvm_deliver_exception_payload(&svm->vcpu);
1040                 if (svm->vcpu.arch.dr7 & DR7_GD) {
1041                         svm->vcpu.arch.dr7 &= ~DR7_GD;
1042                         kvm_update_dr7(&svm->vcpu);
1043                 }
1044         } else
1045                 WARN_ON(svm->vcpu.arch.exception.has_payload);
1046
1047         nested_svm_vmexit(svm);
1048 }
1049
1050 static inline bool nested_exit_on_init(struct vcpu_svm *svm)
1051 {
1052         return vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_INIT);
1053 }
1054
1055 static int svm_check_nested_events(struct kvm_vcpu *vcpu)
1056 {
1057         struct vcpu_svm *svm = to_svm(vcpu);
1058         bool block_nested_events =
1059                 kvm_event_needs_reinjection(vcpu) || svm->nested.nested_run_pending;
1060         struct kvm_lapic *apic = vcpu->arch.apic;
1061
1062         if (lapic_in_kernel(vcpu) &&
1063             test_bit(KVM_APIC_INIT, &apic->pending_events)) {
1064                 if (block_nested_events)
1065                         return -EBUSY;
1066                 if (!nested_exit_on_init(svm))
1067                         return 0;
1068                 nested_svm_simple_vmexit(svm, SVM_EXIT_INIT);
1069                 return 0;
1070         }
1071
1072         if (vcpu->arch.exception.pending) {
1073                 if (block_nested_events)
1074                         return -EBUSY;
1075                 if (!nested_exit_on_exception(svm))
1076                         return 0;
1077                 nested_svm_inject_exception_vmexit(svm);
1078                 return 0;
1079         }
1080
1081         if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) {
1082                 if (block_nested_events)
1083                         return -EBUSY;
1084                 if (!nested_exit_on_smi(svm))
1085                         return 0;
1086                 nested_svm_simple_vmexit(svm, SVM_EXIT_SMI);
1087                 return 0;
1088         }
1089
1090         if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) {
1091                 if (block_nested_events)
1092                         return -EBUSY;
1093                 if (!nested_exit_on_nmi(svm))
1094                         return 0;
1095                 nested_svm_simple_vmexit(svm, SVM_EXIT_NMI);
1096                 return 0;
1097         }
1098
1099         if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) {
1100                 if (block_nested_events)
1101                         return -EBUSY;
1102                 if (!nested_exit_on_intr(svm))
1103                         return 0;
1104                 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
1105                 nested_svm_simple_vmexit(svm, SVM_EXIT_INTR);
1106                 return 0;
1107         }
1108
1109         return 0;
1110 }
1111
1112 int nested_svm_exit_special(struct vcpu_svm *svm)
1113 {
1114         u32 exit_code = svm->vmcb->control.exit_code;
1115
1116         switch (exit_code) {
1117         case SVM_EXIT_INTR:
1118         case SVM_EXIT_NMI:
1119         case SVM_EXIT_NPF:
1120                 return NESTED_EXIT_HOST;
1121         case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1122                 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
1123
1124                 if (svm->vmcb01.ptr->control.intercepts[INTERCEPT_EXCEPTION] &
1125                     excp_bits)
1126                         return NESTED_EXIT_HOST;
1127                 else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR &&
1128                          svm->vcpu.arch.apf.host_apf_flags)
1129                         /* Trap async PF even if not shadowing */
1130                         return NESTED_EXIT_HOST;
1131                 break;
1132         }
1133         default:
1134                 break;
1135         }
1136
1137         return NESTED_EXIT_CONTINUE;
1138 }
1139
1140 static int svm_get_nested_state(struct kvm_vcpu *vcpu,
1141                                 struct kvm_nested_state __user *user_kvm_nested_state,
1142                                 u32 user_data_size)
1143 {
1144         struct vcpu_svm *svm;
1145         struct kvm_nested_state kvm_state = {
1146                 .flags = 0,
1147                 .format = KVM_STATE_NESTED_FORMAT_SVM,
1148                 .size = sizeof(kvm_state),
1149         };
1150         struct vmcb __user *user_vmcb = (struct vmcb __user *)
1151                 &user_kvm_nested_state->data.svm[0];
1152
1153         if (!vcpu)
1154                 return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE;
1155
1156         svm = to_svm(vcpu);
1157
1158         if (user_data_size < kvm_state.size)
1159                 goto out;
1160
1161         /* First fill in the header and copy it out.  */
1162         if (is_guest_mode(vcpu)) {
1163                 kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa;
1164                 kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE;
1165                 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
1166
1167                 if (svm->nested.nested_run_pending)
1168                         kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
1169         }
1170
1171         if (gif_set(svm))
1172                 kvm_state.flags |= KVM_STATE_NESTED_GIF_SET;
1173
1174         if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
1175                 return -EFAULT;
1176
1177         if (!is_guest_mode(vcpu))
1178                 goto out;
1179
1180         /*
1181          * Copy over the full size of the VMCB rather than just the size
1182          * of the structs.
1183          */
1184         if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE))
1185                 return -EFAULT;
1186         if (copy_to_user(&user_vmcb->control, &svm->nested.ctl,
1187                          sizeof(user_vmcb->control)))
1188                 return -EFAULT;
1189         if (copy_to_user(&user_vmcb->save, &svm->vmcb01.ptr->save,
1190                          sizeof(user_vmcb->save)))
1191                 return -EFAULT;
1192 out:
1193         return kvm_state.size;
1194 }
1195
1196 static int svm_set_nested_state(struct kvm_vcpu *vcpu,
1197                                 struct kvm_nested_state __user *user_kvm_nested_state,
1198                                 struct kvm_nested_state *kvm_state)
1199 {
1200         struct vcpu_svm *svm = to_svm(vcpu);
1201         struct vmcb __user *user_vmcb = (struct vmcb __user *)
1202                 &user_kvm_nested_state->data.svm[0];
1203         struct vmcb_control_area *ctl;
1204         struct vmcb_save_area *save;
1205         int ret;
1206         u32 cr0;
1207
1208         BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >
1209                      KVM_STATE_NESTED_SVM_VMCB_SIZE);
1210
1211         if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM)
1212                 return -EINVAL;
1213
1214         if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE |
1215                                  KVM_STATE_NESTED_RUN_PENDING |
1216                                  KVM_STATE_NESTED_GIF_SET))
1217                 return -EINVAL;
1218
1219         /*
1220          * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
1221          * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
1222          */
1223         if (!(vcpu->arch.efer & EFER_SVME)) {
1224                 /* GIF=1 and no guest mode are required if SVME=0.  */
1225                 if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
1226                         return -EINVAL;
1227         }
1228
1229         /* SMM temporarily disables SVM, so we cannot be in guest mode.  */
1230         if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
1231                 return -EINVAL;
1232
1233         if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) {
1234                 svm_leave_nested(svm);
1235                 svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
1236                 return 0;
1237         }
1238
1239         if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa))
1240                 return -EINVAL;
1241         if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)
1242                 return -EINVAL;
1243
1244         ret  = -ENOMEM;
1245         ctl  = kzalloc(sizeof(*ctl),  GFP_KERNEL);
1246         save = kzalloc(sizeof(*save), GFP_KERNEL);
1247         if (!ctl || !save)
1248                 goto out_free;
1249
1250         ret = -EFAULT;
1251         if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl)))
1252                 goto out_free;
1253         if (copy_from_user(save, &user_vmcb->save, sizeof(*save)))
1254                 goto out_free;
1255
1256         ret = -EINVAL;
1257         if (!nested_vmcb_check_controls(ctl))
1258                 goto out_free;
1259
1260         /*
1261          * Processor state contains L2 state.  Check that it is
1262          * valid for guest mode (see nested_vmcb_check_save).
1263          */
1264         cr0 = kvm_read_cr0(vcpu);
1265         if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
1266                 goto out_free;
1267
1268         /*
1269          * Validate host state saved from before VMRUN (see
1270          * nested_svm_check_permissions).
1271          */
1272         if (!(save->cr0 & X86_CR0_PG) ||
1273             !(save->cr0 & X86_CR0_PE) ||
1274             (save->rflags & X86_EFLAGS_VM) ||
1275             !nested_vmcb_valid_sregs(vcpu, save))
1276                 goto out_free;
1277
1278         /*
1279          * All checks done, we can enter guest mode. Userspace provides
1280          * vmcb12.control, which will be combined with L1 and stored into
1281          * vmcb02, and the L1 save state which we store in vmcb01.
1282          * L2 registers if needed are moved from the current VMCB to VMCB02.
1283          */
1284
1285         svm->nested.nested_run_pending =
1286                 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
1287
1288         svm->nested.vmcb12_gpa = kvm_state->hdr.svm.vmcb_pa;
1289         if (svm->current_vmcb == &svm->vmcb01)
1290                 svm->nested.vmcb02.ptr->save = svm->vmcb01.ptr->save;
1291
1292         svm->vmcb01.ptr->save.es = save->es;
1293         svm->vmcb01.ptr->save.cs = save->cs;
1294         svm->vmcb01.ptr->save.ss = save->ss;
1295         svm->vmcb01.ptr->save.ds = save->ds;
1296         svm->vmcb01.ptr->save.gdtr = save->gdtr;
1297         svm->vmcb01.ptr->save.idtr = save->idtr;
1298         svm->vmcb01.ptr->save.rflags = save->rflags | X86_EFLAGS_FIXED;
1299         svm->vmcb01.ptr->save.efer = save->efer;
1300         svm->vmcb01.ptr->save.cr0 = save->cr0;
1301         svm->vmcb01.ptr->save.cr3 = save->cr3;
1302         svm->vmcb01.ptr->save.cr4 = save->cr4;
1303         svm->vmcb01.ptr->save.rax = save->rax;
1304         svm->vmcb01.ptr->save.rsp = save->rsp;
1305         svm->vmcb01.ptr->save.rip = save->rip;
1306         svm->vmcb01.ptr->save.cpl = 0;
1307
1308         nested_load_control_from_vmcb12(svm, ctl);
1309
1310         svm_switch_vmcb(svm, &svm->nested.vmcb02);
1311
1312         nested_vmcb02_prepare_control(svm);
1313
1314         kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
1315         ret = 0;
1316 out_free:
1317         kfree(save);
1318         kfree(ctl);
1319
1320         return ret;
1321 }
1322
1323 struct kvm_x86_nested_ops svm_nested_ops = {
1324         .check_events = svm_check_nested_events,
1325         .triple_fault = nested_svm_triple_fault,
1326         .get_nested_state_pages = svm_get_nested_state_pages,
1327         .get_state = svm_get_nested_state,
1328         .set_state = svm_set_nested_state,
1329 };