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