2 * Kernel-based Virtual Machine driver for Linux
6 * Copyright (C) 2006 Qumranet, Inc.
9 * Yaniv Kamay <yaniv@qumranet.com>
10 * Avi Kivity <avi@qumranet.com>
12 * This work is licensed under the terms of the GNU GPL, version 2. See
13 * the COPYING file in the top-level directory.
16 #include <linux/kvm_host.h>
20 #include "kvm_cache_regs.h"
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/vmalloc.h>
26 #include <linux/highmem.h>
27 #include <linux/sched.h>
28 #include <linux/ftrace_event.h>
29 #include <linux/slab.h>
33 #include <asm/virtext.h>
36 #define __ex(x) __kvm_handle_fault_on_reboot(x)
38 MODULE_AUTHOR("Qumranet");
39 MODULE_LICENSE("GPL");
41 #define IOPM_ALLOC_ORDER 2
42 #define MSRPM_ALLOC_ORDER 1
44 #define SEG_TYPE_LDT 2
45 #define SEG_TYPE_BUSY_TSS16 3
47 #define SVM_FEATURE_NPT (1 << 0)
48 #define SVM_FEATURE_LBRV (1 << 1)
49 #define SVM_FEATURE_SVML (1 << 2)
50 #define SVM_FEATURE_NRIP (1 << 3)
51 #define SVM_FEATURE_PAUSE_FILTER (1 << 10)
53 #define NESTED_EXIT_HOST 0 /* Exit handled on host level */
54 #define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
55 #define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
57 #define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
59 static const u32 host_save_user_msrs
[] = {
61 MSR_STAR
, MSR_LSTAR
, MSR_CSTAR
, MSR_SYSCALL_MASK
, MSR_KERNEL_GS_BASE
,
64 MSR_IA32_SYSENTER_CS
, MSR_IA32_SYSENTER_ESP
, MSR_IA32_SYSENTER_EIP
,
67 #define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
76 /* These are the merged vectors */
79 /* gpa pointers to the real vectors */
82 /* A VMEXIT is required but not yet emulated */
85 /* cache for intercepts of the guest */
86 u16 intercept_cr_read
;
87 u16 intercept_cr_write
;
88 u16 intercept_dr_read
;
89 u16 intercept_dr_write
;
90 u32 intercept_exceptions
;
98 unsigned long vmcb_pa
;
99 struct svm_cpu_data
*svm_data
;
100 uint64_t asid_generation
;
101 uint64_t sysenter_esp
;
102 uint64_t sysenter_eip
;
106 u64 host_user_msrs
[NR_HOST_SAVE_USER_MSRS
];
111 struct nested_state nested
;
115 unsigned int3_injected
;
116 unsigned long int3_rip
;
119 /* enable NPT for AMD64 and X86 with PAE */
120 #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
121 static bool npt_enabled
= true;
123 static bool npt_enabled
= false;
127 module_param(npt
, int, S_IRUGO
);
129 static int nested
= 1;
130 module_param(nested
, int, S_IRUGO
);
132 static void svm_flush_tlb(struct kvm_vcpu
*vcpu
);
133 static void svm_complete_interrupts(struct vcpu_svm
*svm
);
135 static int nested_svm_exit_handled(struct vcpu_svm
*svm
);
136 static int nested_svm_intercept(struct vcpu_svm
*svm
);
137 static int nested_svm_vmexit(struct vcpu_svm
*svm
);
138 static int nested_svm_check_exception(struct vcpu_svm
*svm
, unsigned nr
,
139 bool has_error_code
, u32 error_code
);
141 static inline struct vcpu_svm
*to_svm(struct kvm_vcpu
*vcpu
)
143 return container_of(vcpu
, struct vcpu_svm
, vcpu
);
146 static inline bool is_nested(struct vcpu_svm
*svm
)
148 return svm
->nested
.vmcb
;
151 static inline void enable_gif(struct vcpu_svm
*svm
)
153 svm
->vcpu
.arch
.hflags
|= HF_GIF_MASK
;
156 static inline void disable_gif(struct vcpu_svm
*svm
)
158 svm
->vcpu
.arch
.hflags
&= ~HF_GIF_MASK
;
161 static inline bool gif_set(struct vcpu_svm
*svm
)
163 return !!(svm
->vcpu
.arch
.hflags
& HF_GIF_MASK
);
166 static unsigned long iopm_base
;
168 struct kvm_ldttss_desc
{
171 unsigned base1
: 8, type
: 5, dpl
: 2, p
: 1;
172 unsigned limit1
: 4, zero0
: 3, g
: 1, base2
: 8;
175 } __attribute__((packed
));
177 struct svm_cpu_data
{
183 struct kvm_ldttss_desc
*tss_desc
;
185 struct page
*save_area
;
188 static DEFINE_PER_CPU(struct svm_cpu_data
*, svm_data
);
189 static uint32_t svm_features
;
191 struct svm_init_data
{
196 static u32 msrpm_ranges
[] = {0, 0xc0000000, 0xc0010000};
198 #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
199 #define MSRS_RANGE_SIZE 2048
200 #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
202 #define MAX_INST_SIZE 15
204 static inline u32
svm_has(u32 feat
)
206 return svm_features
& feat
;
209 static inline void clgi(void)
211 asm volatile (__ex(SVM_CLGI
));
214 static inline void stgi(void)
216 asm volatile (__ex(SVM_STGI
));
219 static inline void invlpga(unsigned long addr
, u32 asid
)
221 asm volatile (__ex(SVM_INVLPGA
) :: "a"(addr
), "c"(asid
));
224 static inline void force_new_asid(struct kvm_vcpu
*vcpu
)
226 to_svm(vcpu
)->asid_generation
--;
229 static inline void flush_guest_tlb(struct kvm_vcpu
*vcpu
)
231 force_new_asid(vcpu
);
234 static void svm_set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
236 if (!npt_enabled
&& !(efer
& EFER_LMA
))
239 to_svm(vcpu
)->vmcb
->save
.efer
= efer
| EFER_SVME
;
240 vcpu
->arch
.efer
= efer
;
243 static int is_external_interrupt(u32 info
)
245 info
&= SVM_EVTINJ_TYPE_MASK
| SVM_EVTINJ_VALID
;
246 return info
== (SVM_EVTINJ_VALID
| SVM_EVTINJ_TYPE_INTR
);
249 static u32
svm_get_interrupt_shadow(struct kvm_vcpu
*vcpu
, int mask
)
251 struct vcpu_svm
*svm
= to_svm(vcpu
);
254 if (svm
->vmcb
->control
.int_state
& SVM_INTERRUPT_SHADOW_MASK
)
255 ret
|= KVM_X86_SHADOW_INT_STI
| KVM_X86_SHADOW_INT_MOV_SS
;
259 static void svm_set_interrupt_shadow(struct kvm_vcpu
*vcpu
, int mask
)
261 struct vcpu_svm
*svm
= to_svm(vcpu
);
264 svm
->vmcb
->control
.int_state
&= ~SVM_INTERRUPT_SHADOW_MASK
;
266 svm
->vmcb
->control
.int_state
|= SVM_INTERRUPT_SHADOW_MASK
;
270 static void skip_emulated_instruction(struct kvm_vcpu
*vcpu
)
272 struct vcpu_svm
*svm
= to_svm(vcpu
);
274 if (!svm
->next_rip
) {
275 if (emulate_instruction(vcpu
, 0, 0, EMULTYPE_SKIP
) !=
277 printk(KERN_DEBUG
"%s: NOP\n", __func__
);
280 if (svm
->next_rip
- kvm_rip_read(vcpu
) > MAX_INST_SIZE
)
281 printk(KERN_ERR
"%s: ip 0x%lx next 0x%llx\n",
282 __func__
, kvm_rip_read(vcpu
), svm
->next_rip
);
284 kvm_rip_write(vcpu
, svm
->next_rip
);
285 svm_set_interrupt_shadow(vcpu
, 0);
288 static void svm_queue_exception(struct kvm_vcpu
*vcpu
, unsigned nr
,
289 bool has_error_code
, u32 error_code
)
291 struct vcpu_svm
*svm
= to_svm(vcpu
);
293 /* If we are within a nested VM we'd better #VMEXIT and let the
294 guest handle the exception */
295 if (nested_svm_check_exception(svm
, nr
, has_error_code
, error_code
))
298 if (nr
== BP_VECTOR
&& !svm_has(SVM_FEATURE_NRIP
)) {
299 unsigned long rip
, old_rip
= kvm_rip_read(&svm
->vcpu
);
302 * For guest debugging where we have to reinject #BP if some
303 * INT3 is guest-owned:
304 * Emulate nRIP by moving RIP forward. Will fail if injection
305 * raises a fault that is not intercepted. Still better than
306 * failing in all cases.
308 skip_emulated_instruction(&svm
->vcpu
);
309 rip
= kvm_rip_read(&svm
->vcpu
);
310 svm
->int3_rip
= rip
+ svm
->vmcb
->save
.cs
.base
;
311 svm
->int3_injected
= rip
- old_rip
;
314 svm
->vmcb
->control
.event_inj
= nr
316 | (has_error_code
? SVM_EVTINJ_VALID_ERR
: 0)
317 | SVM_EVTINJ_TYPE_EXEPT
;
318 svm
->vmcb
->control
.event_inj_err
= error_code
;
321 static int has_svm(void)
325 if (!cpu_has_svm(&msg
)) {
326 printk(KERN_INFO
"has_svm: %s\n", msg
);
333 static void svm_hardware_disable(void *garbage
)
338 static int svm_hardware_enable(void *garbage
)
341 struct svm_cpu_data
*sd
;
343 struct desc_ptr gdt_descr
;
344 struct desc_struct
*gdt
;
345 int me
= raw_smp_processor_id();
347 rdmsrl(MSR_EFER
, efer
);
348 if (efer
& EFER_SVME
)
352 printk(KERN_ERR
"svm_hardware_enable: err EOPNOTSUPP on %d\n",
356 sd
= per_cpu(svm_data
, me
);
359 printk(KERN_ERR
"svm_hardware_enable: svm_data is NULL on %d\n",
364 sd
->asid_generation
= 1;
365 sd
->max_asid
= cpuid_ebx(SVM_CPUID_FUNC
) - 1;
366 sd
->next_asid
= sd
->max_asid
+ 1;
368 kvm_get_gdt(&gdt_descr
);
369 gdt
= (struct desc_struct
*)gdt_descr
.address
;
370 sd
->tss_desc
= (struct kvm_ldttss_desc
*)(gdt
+ GDT_ENTRY_TSS
);
372 wrmsrl(MSR_EFER
, efer
| EFER_SVME
);
374 wrmsrl(MSR_VM_HSAVE_PA
, page_to_pfn(sd
->save_area
) << PAGE_SHIFT
);
379 static void svm_cpu_uninit(int cpu
)
381 struct svm_cpu_data
*sd
= per_cpu(svm_data
, raw_smp_processor_id());
386 per_cpu(svm_data
, raw_smp_processor_id()) = NULL
;
387 __free_page(sd
->save_area
);
391 static int svm_cpu_init(int cpu
)
393 struct svm_cpu_data
*sd
;
396 sd
= kzalloc(sizeof(struct svm_cpu_data
), GFP_KERNEL
);
400 sd
->save_area
= alloc_page(GFP_KERNEL
);
405 per_cpu(svm_data
, cpu
) = sd
;
415 static void set_msr_interception(u32
*msrpm
, unsigned msr
,
420 for (i
= 0; i
< NUM_MSR_MAPS
; i
++) {
421 if (msr
>= msrpm_ranges
[i
] &&
422 msr
< msrpm_ranges
[i
] + MSRS_IN_RANGE
) {
423 u32 msr_offset
= (i
* MSRS_IN_RANGE
+ msr
-
424 msrpm_ranges
[i
]) * 2;
426 u32
*base
= msrpm
+ (msr_offset
/ 32);
427 u32 msr_shift
= msr_offset
% 32;
428 u32 mask
= ((write
) ? 0 : 2) | ((read
) ? 0 : 1);
429 *base
= (*base
& ~(0x3 << msr_shift
)) |
437 static void svm_vcpu_init_msrpm(u32
*msrpm
)
439 memset(msrpm
, 0xff, PAGE_SIZE
* (1 << MSRPM_ALLOC_ORDER
));
442 set_msr_interception(msrpm
, MSR_GS_BASE
, 1, 1);
443 set_msr_interception(msrpm
, MSR_FS_BASE
, 1, 1);
444 set_msr_interception(msrpm
, MSR_KERNEL_GS_BASE
, 1, 1);
445 set_msr_interception(msrpm
, MSR_LSTAR
, 1, 1);
446 set_msr_interception(msrpm
, MSR_CSTAR
, 1, 1);
447 set_msr_interception(msrpm
, MSR_SYSCALL_MASK
, 1, 1);
449 set_msr_interception(msrpm
, MSR_K6_STAR
, 1, 1);
450 set_msr_interception(msrpm
, MSR_IA32_SYSENTER_CS
, 1, 1);
453 static void svm_enable_lbrv(struct vcpu_svm
*svm
)
455 u32
*msrpm
= svm
->msrpm
;
457 svm
->vmcb
->control
.lbr_ctl
= 1;
458 set_msr_interception(msrpm
, MSR_IA32_LASTBRANCHFROMIP
, 1, 1);
459 set_msr_interception(msrpm
, MSR_IA32_LASTBRANCHTOIP
, 1, 1);
460 set_msr_interception(msrpm
, MSR_IA32_LASTINTFROMIP
, 1, 1);
461 set_msr_interception(msrpm
, MSR_IA32_LASTINTTOIP
, 1, 1);
464 static void svm_disable_lbrv(struct vcpu_svm
*svm
)
466 u32
*msrpm
= svm
->msrpm
;
468 svm
->vmcb
->control
.lbr_ctl
= 0;
469 set_msr_interception(msrpm
, MSR_IA32_LASTBRANCHFROMIP
, 0, 0);
470 set_msr_interception(msrpm
, MSR_IA32_LASTBRANCHTOIP
, 0, 0);
471 set_msr_interception(msrpm
, MSR_IA32_LASTINTFROMIP
, 0, 0);
472 set_msr_interception(msrpm
, MSR_IA32_LASTINTTOIP
, 0, 0);
475 static __init
int svm_hardware_setup(void)
478 struct page
*iopm_pages
;
482 iopm_pages
= alloc_pages(GFP_KERNEL
, IOPM_ALLOC_ORDER
);
487 iopm_va
= page_address(iopm_pages
);
488 memset(iopm_va
, 0xff, PAGE_SIZE
* (1 << IOPM_ALLOC_ORDER
));
489 iopm_base
= page_to_pfn(iopm_pages
) << PAGE_SHIFT
;
491 if (boot_cpu_has(X86_FEATURE_NX
))
492 kvm_enable_efer_bits(EFER_NX
);
494 if (boot_cpu_has(X86_FEATURE_FXSR_OPT
))
495 kvm_enable_efer_bits(EFER_FFXSR
);
498 printk(KERN_INFO
"kvm: Nested Virtualization enabled\n");
499 kvm_enable_efer_bits(EFER_SVME
);
502 for_each_possible_cpu(cpu
) {
503 r
= svm_cpu_init(cpu
);
508 svm_features
= cpuid_edx(SVM_CPUID_FUNC
);
510 if (!svm_has(SVM_FEATURE_NPT
))
513 if (npt_enabled
&& !npt
) {
514 printk(KERN_INFO
"kvm: Nested Paging disabled\n");
519 printk(KERN_INFO
"kvm: Nested Paging enabled\n");
527 __free_pages(iopm_pages
, IOPM_ALLOC_ORDER
);
532 static __exit
void svm_hardware_unsetup(void)
536 for_each_possible_cpu(cpu
)
539 __free_pages(pfn_to_page(iopm_base
>> PAGE_SHIFT
), IOPM_ALLOC_ORDER
);
543 static void init_seg(struct vmcb_seg
*seg
)
546 seg
->attrib
= SVM_SELECTOR_P_MASK
| SVM_SELECTOR_S_MASK
|
547 SVM_SELECTOR_WRITE_MASK
; /* Read/Write Data Segment */
552 static void init_sys_seg(struct vmcb_seg
*seg
, uint32_t type
)
555 seg
->attrib
= SVM_SELECTOR_P_MASK
| type
;
560 static void init_vmcb(struct vcpu_svm
*svm
)
562 struct vmcb_control_area
*control
= &svm
->vmcb
->control
;
563 struct vmcb_save_area
*save
= &svm
->vmcb
->save
;
565 svm
->vcpu
.fpu_active
= 1;
567 control
->intercept_cr_read
= INTERCEPT_CR0_MASK
|
571 control
->intercept_cr_write
= INTERCEPT_CR0_MASK
|
576 control
->intercept_dr_read
= INTERCEPT_DR0_MASK
|
585 control
->intercept_dr_write
= INTERCEPT_DR0_MASK
|
594 control
->intercept_exceptions
= (1 << PF_VECTOR
) |
599 control
->intercept
= (1ULL << INTERCEPT_INTR
) |
600 (1ULL << INTERCEPT_NMI
) |
601 (1ULL << INTERCEPT_SMI
) |
602 (1ULL << INTERCEPT_SELECTIVE_CR0
) |
603 (1ULL << INTERCEPT_CPUID
) |
604 (1ULL << INTERCEPT_INVD
) |
605 (1ULL << INTERCEPT_HLT
) |
606 (1ULL << INTERCEPT_INVLPG
) |
607 (1ULL << INTERCEPT_INVLPGA
) |
608 (1ULL << INTERCEPT_IOIO_PROT
) |
609 (1ULL << INTERCEPT_MSR_PROT
) |
610 (1ULL << INTERCEPT_TASK_SWITCH
) |
611 (1ULL << INTERCEPT_SHUTDOWN
) |
612 (1ULL << INTERCEPT_VMRUN
) |
613 (1ULL << INTERCEPT_VMMCALL
) |
614 (1ULL << INTERCEPT_VMLOAD
) |
615 (1ULL << INTERCEPT_VMSAVE
) |
616 (1ULL << INTERCEPT_STGI
) |
617 (1ULL << INTERCEPT_CLGI
) |
618 (1ULL << INTERCEPT_SKINIT
) |
619 (1ULL << INTERCEPT_WBINVD
) |
620 (1ULL << INTERCEPT_MONITOR
) |
621 (1ULL << INTERCEPT_MWAIT
);
623 control
->iopm_base_pa
= iopm_base
;
624 control
->msrpm_base_pa
= __pa(svm
->msrpm
);
625 control
->tsc_offset
= 0;
626 control
->int_ctl
= V_INTR_MASKING_MASK
;
634 save
->cs
.selector
= 0xf000;
635 /* Executable/Readable Code Segment */
636 save
->cs
.attrib
= SVM_SELECTOR_READ_MASK
| SVM_SELECTOR_P_MASK
|
637 SVM_SELECTOR_S_MASK
| SVM_SELECTOR_CODE_MASK
;
638 save
->cs
.limit
= 0xffff;
640 * cs.base should really be 0xffff0000, but vmx can't handle that, so
641 * be consistent with it.
643 * Replace when we have real mode working for vmx.
645 save
->cs
.base
= 0xf0000;
647 save
->gdtr
.limit
= 0xffff;
648 save
->idtr
.limit
= 0xffff;
650 init_sys_seg(&save
->ldtr
, SEG_TYPE_LDT
);
651 init_sys_seg(&save
->tr
, SEG_TYPE_BUSY_TSS16
);
653 save
->efer
= EFER_SVME
;
654 save
->dr6
= 0xffff0ff0;
657 save
->rip
= 0x0000fff0;
658 svm
->vcpu
.arch
.regs
[VCPU_REGS_RIP
] = save
->rip
;
660 /* This is the guest-visible cr0 value.
661 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
663 svm
->vcpu
.arch
.cr0
= X86_CR0_NW
| X86_CR0_CD
| X86_CR0_ET
;
664 kvm_set_cr0(&svm
->vcpu
, svm
->vcpu
.arch
.cr0
);
666 save
->cr4
= X86_CR4_PAE
;
670 /* Setup VMCB for Nested Paging */
671 control
->nested_ctl
= 1;
672 control
->intercept
&= ~((1ULL << INTERCEPT_TASK_SWITCH
) |
673 (1ULL << INTERCEPT_INVLPG
));
674 control
->intercept_exceptions
&= ~(1 << PF_VECTOR
);
675 control
->intercept_cr_read
&= ~INTERCEPT_CR3_MASK
;
676 control
->intercept_cr_write
&= ~INTERCEPT_CR3_MASK
;
677 save
->g_pat
= 0x0007040600070406ULL
;
681 force_new_asid(&svm
->vcpu
);
683 svm
->nested
.vmcb
= 0;
684 svm
->vcpu
.arch
.hflags
= 0;
686 if (svm_has(SVM_FEATURE_PAUSE_FILTER
)) {
687 control
->pause_filter_count
= 3000;
688 control
->intercept
|= (1ULL << INTERCEPT_PAUSE
);
694 static int svm_vcpu_reset(struct kvm_vcpu
*vcpu
)
696 struct vcpu_svm
*svm
= to_svm(vcpu
);
700 if (!kvm_vcpu_is_bsp(vcpu
)) {
701 kvm_rip_write(vcpu
, 0);
702 svm
->vmcb
->save
.cs
.base
= svm
->vcpu
.arch
.sipi_vector
<< 12;
703 svm
->vmcb
->save
.cs
.selector
= svm
->vcpu
.arch
.sipi_vector
<< 8;
705 vcpu
->arch
.regs_avail
= ~0;
706 vcpu
->arch
.regs_dirty
= ~0;
711 static struct kvm_vcpu
*svm_create_vcpu(struct kvm
*kvm
, unsigned int id
)
713 struct vcpu_svm
*svm
;
715 struct page
*msrpm_pages
;
716 struct page
*hsave_page
;
717 struct page
*nested_msrpm_pages
;
720 svm
= kmem_cache_zalloc(kvm_vcpu_cache
, GFP_KERNEL
);
726 err
= kvm_vcpu_init(&svm
->vcpu
, kvm
, id
);
731 page
= alloc_page(GFP_KERNEL
);
735 msrpm_pages
= alloc_pages(GFP_KERNEL
, MSRPM_ALLOC_ORDER
);
739 nested_msrpm_pages
= alloc_pages(GFP_KERNEL
, MSRPM_ALLOC_ORDER
);
740 if (!nested_msrpm_pages
)
743 hsave_page
= alloc_page(GFP_KERNEL
);
747 svm
->nested
.hsave
= page_address(hsave_page
);
749 svm
->msrpm
= page_address(msrpm_pages
);
750 svm_vcpu_init_msrpm(svm
->msrpm
);
752 svm
->nested
.msrpm
= page_address(nested_msrpm_pages
);
754 svm
->vmcb
= page_address(page
);
755 clear_page(svm
->vmcb
);
756 svm
->vmcb_pa
= page_to_pfn(page
) << PAGE_SHIFT
;
757 svm
->asid_generation
= 0;
761 svm
->vcpu
.arch
.apic_base
= 0xfee00000 | MSR_IA32_APICBASE_ENABLE
;
762 if (kvm_vcpu_is_bsp(&svm
->vcpu
))
763 svm
->vcpu
.arch
.apic_base
|= MSR_IA32_APICBASE_BSP
;
768 __free_pages(nested_msrpm_pages
, MSRPM_ALLOC_ORDER
);
770 __free_pages(msrpm_pages
, MSRPM_ALLOC_ORDER
);
774 kvm_vcpu_uninit(&svm
->vcpu
);
776 kmem_cache_free(kvm_vcpu_cache
, svm
);
781 static void svm_free_vcpu(struct kvm_vcpu
*vcpu
)
783 struct vcpu_svm
*svm
= to_svm(vcpu
);
785 __free_page(pfn_to_page(svm
->vmcb_pa
>> PAGE_SHIFT
));
786 __free_pages(virt_to_page(svm
->msrpm
), MSRPM_ALLOC_ORDER
);
787 __free_page(virt_to_page(svm
->nested
.hsave
));
788 __free_pages(virt_to_page(svm
->nested
.msrpm
), MSRPM_ALLOC_ORDER
);
789 kvm_vcpu_uninit(vcpu
);
790 kmem_cache_free(kvm_vcpu_cache
, svm
);
793 static void svm_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
795 struct vcpu_svm
*svm
= to_svm(vcpu
);
798 if (unlikely(cpu
!= vcpu
->cpu
)) {
801 if (check_tsc_unstable()) {
803 * Make sure that the guest sees a monotonically
806 delta
= vcpu
->arch
.host_tsc
- native_read_tsc();
807 svm
->vmcb
->control
.tsc_offset
+= delta
;
809 svm
->nested
.hsave
->control
.tsc_offset
+= delta
;
812 kvm_migrate_timers(vcpu
);
813 svm
->asid_generation
= 0;
816 for (i
= 0; i
< NR_HOST_SAVE_USER_MSRS
; i
++)
817 rdmsrl(host_save_user_msrs
[i
], svm
->host_user_msrs
[i
]);
820 static void svm_vcpu_put(struct kvm_vcpu
*vcpu
)
822 struct vcpu_svm
*svm
= to_svm(vcpu
);
825 ++vcpu
->stat
.host_state_reload
;
826 for (i
= 0; i
< NR_HOST_SAVE_USER_MSRS
; i
++)
827 wrmsrl(host_save_user_msrs
[i
], svm
->host_user_msrs
[i
]);
829 vcpu
->arch
.host_tsc
= native_read_tsc();
832 static unsigned long svm_get_rflags(struct kvm_vcpu
*vcpu
)
834 return to_svm(vcpu
)->vmcb
->save
.rflags
;
837 static void svm_set_rflags(struct kvm_vcpu
*vcpu
, unsigned long rflags
)
839 to_svm(vcpu
)->vmcb
->save
.rflags
= rflags
;
842 static void svm_cache_reg(struct kvm_vcpu
*vcpu
, enum kvm_reg reg
)
845 case VCPU_EXREG_PDPTR
:
846 BUG_ON(!npt_enabled
);
847 load_pdptrs(vcpu
, vcpu
->arch
.cr3
);
854 static void svm_set_vintr(struct vcpu_svm
*svm
)
856 svm
->vmcb
->control
.intercept
|= 1ULL << INTERCEPT_VINTR
;
859 static void svm_clear_vintr(struct vcpu_svm
*svm
)
861 svm
->vmcb
->control
.intercept
&= ~(1ULL << INTERCEPT_VINTR
);
864 static struct vmcb_seg
*svm_seg(struct kvm_vcpu
*vcpu
, int seg
)
866 struct vmcb_save_area
*save
= &to_svm(vcpu
)->vmcb
->save
;
869 case VCPU_SREG_CS
: return &save
->cs
;
870 case VCPU_SREG_DS
: return &save
->ds
;
871 case VCPU_SREG_ES
: return &save
->es
;
872 case VCPU_SREG_FS
: return &save
->fs
;
873 case VCPU_SREG_GS
: return &save
->gs
;
874 case VCPU_SREG_SS
: return &save
->ss
;
875 case VCPU_SREG_TR
: return &save
->tr
;
876 case VCPU_SREG_LDTR
: return &save
->ldtr
;
882 static u64
svm_get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
884 struct vmcb_seg
*s
= svm_seg(vcpu
, seg
);
889 static void svm_get_segment(struct kvm_vcpu
*vcpu
,
890 struct kvm_segment
*var
, int seg
)
892 struct vmcb_seg
*s
= svm_seg(vcpu
, seg
);
895 var
->limit
= s
->limit
;
896 var
->selector
= s
->selector
;
897 var
->type
= s
->attrib
& SVM_SELECTOR_TYPE_MASK
;
898 var
->s
= (s
->attrib
>> SVM_SELECTOR_S_SHIFT
) & 1;
899 var
->dpl
= (s
->attrib
>> SVM_SELECTOR_DPL_SHIFT
) & 3;
900 var
->present
= (s
->attrib
>> SVM_SELECTOR_P_SHIFT
) & 1;
901 var
->avl
= (s
->attrib
>> SVM_SELECTOR_AVL_SHIFT
) & 1;
902 var
->l
= (s
->attrib
>> SVM_SELECTOR_L_SHIFT
) & 1;
903 var
->db
= (s
->attrib
>> SVM_SELECTOR_DB_SHIFT
) & 1;
904 var
->g
= (s
->attrib
>> SVM_SELECTOR_G_SHIFT
) & 1;
906 /* AMD's VMCB does not have an explicit unusable field, so emulate it
907 * for cross vendor migration purposes by "not present"
909 var
->unusable
= !var
->present
|| (var
->type
== 0);
914 * SVM always stores 0 for the 'G' bit in the CS selector in
915 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
916 * Intel's VMENTRY has a check on the 'G' bit.
918 var
->g
= s
->limit
> 0xfffff;
922 * Work around a bug where the busy flag in the tr selector
932 * The accessed bit must always be set in the segment
933 * descriptor cache, although it can be cleared in the
934 * descriptor, the cached bit always remains at 1. Since
935 * Intel has a check on this, set it here to support
936 * cross-vendor migration.
942 /* On AMD CPUs sometimes the DB bit in the segment
943 * descriptor is left as 1, although the whole segment has
944 * been made unusable. Clear it here to pass an Intel VMX
945 * entry check when cross vendor migrating.
953 static int svm_get_cpl(struct kvm_vcpu
*vcpu
)
955 struct vmcb_save_area
*save
= &to_svm(vcpu
)->vmcb
->save
;
960 static void svm_get_idt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
962 struct vcpu_svm
*svm
= to_svm(vcpu
);
964 dt
->size
= svm
->vmcb
->save
.idtr
.limit
;
965 dt
->address
= svm
->vmcb
->save
.idtr
.base
;
968 static void svm_set_idt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
970 struct vcpu_svm
*svm
= to_svm(vcpu
);
972 svm
->vmcb
->save
.idtr
.limit
= dt
->size
;
973 svm
->vmcb
->save
.idtr
.base
= dt
->address
;
976 static void svm_get_gdt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
978 struct vcpu_svm
*svm
= to_svm(vcpu
);
980 dt
->size
= svm
->vmcb
->save
.gdtr
.limit
;
981 dt
->address
= svm
->vmcb
->save
.gdtr
.base
;
984 static void svm_set_gdt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
986 struct vcpu_svm
*svm
= to_svm(vcpu
);
988 svm
->vmcb
->save
.gdtr
.limit
= dt
->size
;
989 svm
->vmcb
->save
.gdtr
.base
= dt
->address
;
992 static void svm_decache_cr0_guest_bits(struct kvm_vcpu
*vcpu
)
996 static void svm_decache_cr4_guest_bits(struct kvm_vcpu
*vcpu
)
1000 static void update_cr0_intercept(struct vcpu_svm
*svm
)
1002 struct vmcb
*vmcb
= svm
->vmcb
;
1003 ulong gcr0
= svm
->vcpu
.arch
.cr0
;
1004 u64
*hcr0
= &svm
->vmcb
->save
.cr0
;
1006 if (!svm
->vcpu
.fpu_active
)
1007 *hcr0
|= SVM_CR0_SELECTIVE_MASK
;
1009 *hcr0
= (*hcr0
& ~SVM_CR0_SELECTIVE_MASK
)
1010 | (gcr0
& SVM_CR0_SELECTIVE_MASK
);
1013 if (gcr0
== *hcr0
&& svm
->vcpu
.fpu_active
) {
1014 vmcb
->control
.intercept_cr_read
&= ~INTERCEPT_CR0_MASK
;
1015 vmcb
->control
.intercept_cr_write
&= ~INTERCEPT_CR0_MASK
;
1016 if (is_nested(svm
)) {
1017 struct vmcb
*hsave
= svm
->nested
.hsave
;
1019 hsave
->control
.intercept_cr_read
&= ~INTERCEPT_CR0_MASK
;
1020 hsave
->control
.intercept_cr_write
&= ~INTERCEPT_CR0_MASK
;
1021 vmcb
->control
.intercept_cr_read
|= svm
->nested
.intercept_cr_read
;
1022 vmcb
->control
.intercept_cr_write
|= svm
->nested
.intercept_cr_write
;
1025 svm
->vmcb
->control
.intercept_cr_read
|= INTERCEPT_CR0_MASK
;
1026 svm
->vmcb
->control
.intercept_cr_write
|= INTERCEPT_CR0_MASK
;
1027 if (is_nested(svm
)) {
1028 struct vmcb
*hsave
= svm
->nested
.hsave
;
1030 hsave
->control
.intercept_cr_read
|= INTERCEPT_CR0_MASK
;
1031 hsave
->control
.intercept_cr_write
|= INTERCEPT_CR0_MASK
;
1036 static void svm_set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
1038 struct vcpu_svm
*svm
= to_svm(vcpu
);
1040 #ifdef CONFIG_X86_64
1041 if (vcpu
->arch
.efer
& EFER_LME
) {
1042 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
)) {
1043 vcpu
->arch
.efer
|= EFER_LMA
;
1044 svm
->vmcb
->save
.efer
|= EFER_LMA
| EFER_LME
;
1047 if (is_paging(vcpu
) && !(cr0
& X86_CR0_PG
)) {
1048 vcpu
->arch
.efer
&= ~EFER_LMA
;
1049 svm
->vmcb
->save
.efer
&= ~(EFER_LMA
| EFER_LME
);
1053 vcpu
->arch
.cr0
= cr0
;
1056 cr0
|= X86_CR0_PG
| X86_CR0_WP
;
1058 if (!vcpu
->fpu_active
)
1061 * re-enable caching here because the QEMU bios
1062 * does not do it - this results in some delay at
1065 cr0
&= ~(X86_CR0_CD
| X86_CR0_NW
);
1066 svm
->vmcb
->save
.cr0
= cr0
;
1067 update_cr0_intercept(svm
);
1070 static void svm_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
1072 unsigned long host_cr4_mce
= read_cr4() & X86_CR4_MCE
;
1073 unsigned long old_cr4
= to_svm(vcpu
)->vmcb
->save
.cr4
;
1075 if (npt_enabled
&& ((old_cr4
^ cr4
) & X86_CR4_PGE
))
1076 force_new_asid(vcpu
);
1078 vcpu
->arch
.cr4
= cr4
;
1081 cr4
|= host_cr4_mce
;
1082 to_svm(vcpu
)->vmcb
->save
.cr4
= cr4
;
1085 static void svm_set_segment(struct kvm_vcpu
*vcpu
,
1086 struct kvm_segment
*var
, int seg
)
1088 struct vcpu_svm
*svm
= to_svm(vcpu
);
1089 struct vmcb_seg
*s
= svm_seg(vcpu
, seg
);
1091 s
->base
= var
->base
;
1092 s
->limit
= var
->limit
;
1093 s
->selector
= var
->selector
;
1097 s
->attrib
= (var
->type
& SVM_SELECTOR_TYPE_MASK
);
1098 s
->attrib
|= (var
->s
& 1) << SVM_SELECTOR_S_SHIFT
;
1099 s
->attrib
|= (var
->dpl
& 3) << SVM_SELECTOR_DPL_SHIFT
;
1100 s
->attrib
|= (var
->present
& 1) << SVM_SELECTOR_P_SHIFT
;
1101 s
->attrib
|= (var
->avl
& 1) << SVM_SELECTOR_AVL_SHIFT
;
1102 s
->attrib
|= (var
->l
& 1) << SVM_SELECTOR_L_SHIFT
;
1103 s
->attrib
|= (var
->db
& 1) << SVM_SELECTOR_DB_SHIFT
;
1104 s
->attrib
|= (var
->g
& 1) << SVM_SELECTOR_G_SHIFT
;
1106 if (seg
== VCPU_SREG_CS
)
1108 = (svm
->vmcb
->save
.cs
.attrib
1109 >> SVM_SELECTOR_DPL_SHIFT
) & 3;
1113 static void update_db_intercept(struct kvm_vcpu
*vcpu
)
1115 struct vcpu_svm
*svm
= to_svm(vcpu
);
1117 svm
->vmcb
->control
.intercept_exceptions
&=
1118 ~((1 << DB_VECTOR
) | (1 << BP_VECTOR
));
1120 if (svm
->nmi_singlestep
)
1121 svm
->vmcb
->control
.intercept_exceptions
|= (1 << DB_VECTOR
);
1123 if (vcpu
->guest_debug
& KVM_GUESTDBG_ENABLE
) {
1124 if (vcpu
->guest_debug
&
1125 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
))
1126 svm
->vmcb
->control
.intercept_exceptions
|=
1128 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_SW_BP
)
1129 svm
->vmcb
->control
.intercept_exceptions
|=
1132 vcpu
->guest_debug
= 0;
1135 static void svm_guest_debug(struct kvm_vcpu
*vcpu
, struct kvm_guest_debug
*dbg
)
1137 struct vcpu_svm
*svm
= to_svm(vcpu
);
1139 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
)
1140 svm
->vmcb
->save
.dr7
= dbg
->arch
.debugreg
[7];
1142 svm
->vmcb
->save
.dr7
= vcpu
->arch
.dr7
;
1144 update_db_intercept(vcpu
);
1147 static void load_host_msrs(struct kvm_vcpu
*vcpu
)
1149 #ifdef CONFIG_X86_64
1150 wrmsrl(MSR_GS_BASE
, to_svm(vcpu
)->host_gs_base
);
1154 static void save_host_msrs(struct kvm_vcpu
*vcpu
)
1156 #ifdef CONFIG_X86_64
1157 rdmsrl(MSR_GS_BASE
, to_svm(vcpu
)->host_gs_base
);
1161 static void new_asid(struct vcpu_svm
*svm
, struct svm_cpu_data
*sd
)
1163 if (sd
->next_asid
> sd
->max_asid
) {
1164 ++sd
->asid_generation
;
1166 svm
->vmcb
->control
.tlb_ctl
= TLB_CONTROL_FLUSH_ALL_ASID
;
1169 svm
->asid_generation
= sd
->asid_generation
;
1170 svm
->vmcb
->control
.asid
= sd
->next_asid
++;
1173 static int svm_get_dr(struct kvm_vcpu
*vcpu
, int dr
, unsigned long *dest
)
1175 struct vcpu_svm
*svm
= to_svm(vcpu
);
1179 *dest
= vcpu
->arch
.db
[dr
];
1182 if (kvm_read_cr4_bits(vcpu
, X86_CR4_DE
))
1183 return EMULATE_FAIL
; /* will re-inject UD */
1186 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
)
1187 *dest
= vcpu
->arch
.dr6
;
1189 *dest
= svm
->vmcb
->save
.dr6
;
1192 if (kvm_read_cr4_bits(vcpu
, X86_CR4_DE
))
1193 return EMULATE_FAIL
; /* will re-inject UD */
1196 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
)
1197 *dest
= vcpu
->arch
.dr7
;
1199 *dest
= svm
->vmcb
->save
.dr7
;
1203 return EMULATE_DONE
;
1206 static int svm_set_dr(struct kvm_vcpu
*vcpu
, int dr
, unsigned long value
)
1208 struct vcpu_svm
*svm
= to_svm(vcpu
);
1212 vcpu
->arch
.db
[dr
] = value
;
1213 if (!(vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
))
1214 vcpu
->arch
.eff_db
[dr
] = value
;
1217 if (kvm_read_cr4_bits(vcpu
, X86_CR4_DE
))
1218 return EMULATE_FAIL
; /* will re-inject UD */
1221 vcpu
->arch
.dr6
= (value
& DR6_VOLATILE
) | DR6_FIXED_1
;
1224 if (kvm_read_cr4_bits(vcpu
, X86_CR4_DE
))
1225 return EMULATE_FAIL
; /* will re-inject UD */
1228 vcpu
->arch
.dr7
= (value
& DR7_VOLATILE
) | DR7_FIXED_1
;
1229 if (!(vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
)) {
1230 svm
->vmcb
->save
.dr7
= vcpu
->arch
.dr7
;
1231 vcpu
->arch
.switch_db_regs
= (value
& DR7_BP_EN_MASK
);
1236 return EMULATE_DONE
;
1239 static int pf_interception(struct vcpu_svm
*svm
)
1244 fault_address
= svm
->vmcb
->control
.exit_info_2
;
1245 error_code
= svm
->vmcb
->control
.exit_info_1
;
1247 trace_kvm_page_fault(fault_address
, error_code
);
1248 if (!npt_enabled
&& kvm_event_needs_reinjection(&svm
->vcpu
))
1249 kvm_mmu_unprotect_page_virt(&svm
->vcpu
, fault_address
);
1250 return kvm_mmu_page_fault(&svm
->vcpu
, fault_address
, error_code
);
1253 static int db_interception(struct vcpu_svm
*svm
)
1255 struct kvm_run
*kvm_run
= svm
->vcpu
.run
;
1257 if (!(svm
->vcpu
.guest_debug
&
1258 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
)) &&
1259 !svm
->nmi_singlestep
) {
1260 kvm_queue_exception(&svm
->vcpu
, DB_VECTOR
);
1264 if (svm
->nmi_singlestep
) {
1265 svm
->nmi_singlestep
= false;
1266 if (!(svm
->vcpu
.guest_debug
& KVM_GUESTDBG_SINGLESTEP
))
1267 svm
->vmcb
->save
.rflags
&=
1268 ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
1269 update_db_intercept(&svm
->vcpu
);
1272 if (svm
->vcpu
.guest_debug
&
1273 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
)){
1274 kvm_run
->exit_reason
= KVM_EXIT_DEBUG
;
1275 kvm_run
->debug
.arch
.pc
=
1276 svm
->vmcb
->save
.cs
.base
+ svm
->vmcb
->save
.rip
;
1277 kvm_run
->debug
.arch
.exception
= DB_VECTOR
;
1284 static int bp_interception(struct vcpu_svm
*svm
)
1286 struct kvm_run
*kvm_run
= svm
->vcpu
.run
;
1288 kvm_run
->exit_reason
= KVM_EXIT_DEBUG
;
1289 kvm_run
->debug
.arch
.pc
= svm
->vmcb
->save
.cs
.base
+ svm
->vmcb
->save
.rip
;
1290 kvm_run
->debug
.arch
.exception
= BP_VECTOR
;
1294 static int ud_interception(struct vcpu_svm
*svm
)
1298 er
= emulate_instruction(&svm
->vcpu
, 0, 0, EMULTYPE_TRAP_UD
);
1299 if (er
!= EMULATE_DONE
)
1300 kvm_queue_exception(&svm
->vcpu
, UD_VECTOR
);
1304 static void svm_fpu_activate(struct kvm_vcpu
*vcpu
)
1306 struct vcpu_svm
*svm
= to_svm(vcpu
);
1309 if (is_nested(svm
)) {
1312 h_excp
= svm
->nested
.hsave
->control
.intercept_exceptions
;
1313 n_excp
= svm
->nested
.intercept_exceptions
;
1314 h_excp
&= ~(1 << NM_VECTOR
);
1315 excp
= h_excp
| n_excp
;
1317 excp
= svm
->vmcb
->control
.intercept_exceptions
;
1318 excp
&= ~(1 << NM_VECTOR
);
1321 svm
->vmcb
->control
.intercept_exceptions
= excp
;
1323 svm
->vcpu
.fpu_active
= 1;
1324 update_cr0_intercept(svm
);
1327 static int nm_interception(struct vcpu_svm
*svm
)
1329 svm_fpu_activate(&svm
->vcpu
);
1333 static int mc_interception(struct vcpu_svm
*svm
)
1336 * On an #MC intercept the MCE handler is not called automatically in
1337 * the host. So do it by hand here.
1341 /* not sure if we ever come back to this point */
1346 static int shutdown_interception(struct vcpu_svm
*svm
)
1348 struct kvm_run
*kvm_run
= svm
->vcpu
.run
;
1351 * VMCB is undefined after a SHUTDOWN intercept
1352 * so reinitialize it.
1354 clear_page(svm
->vmcb
);
1357 kvm_run
->exit_reason
= KVM_EXIT_SHUTDOWN
;
1361 static int io_interception(struct vcpu_svm
*svm
)
1363 u32 io_info
= svm
->vmcb
->control
.exit_info_1
; /* address size bug? */
1364 int size
, in
, string
;
1367 ++svm
->vcpu
.stat
.io_exits
;
1369 svm
->next_rip
= svm
->vmcb
->control
.exit_info_2
;
1371 string
= (io_info
& SVM_IOIO_STR_MASK
) != 0;
1374 if (emulate_instruction(&svm
->vcpu
,
1375 0, 0, 0) == EMULATE_DO_MMIO
)
1380 in
= (io_info
& SVM_IOIO_TYPE_MASK
) != 0;
1381 port
= io_info
>> 16;
1382 size
= (io_info
& SVM_IOIO_SIZE_MASK
) >> SVM_IOIO_SIZE_SHIFT
;
1384 skip_emulated_instruction(&svm
->vcpu
);
1385 return kvm_emulate_pio(&svm
->vcpu
, in
, size
, port
);
1388 static int nmi_interception(struct vcpu_svm
*svm
)
1393 static int intr_interception(struct vcpu_svm
*svm
)
1395 ++svm
->vcpu
.stat
.irq_exits
;
1399 static int nop_on_interception(struct vcpu_svm
*svm
)
1404 static int halt_interception(struct vcpu_svm
*svm
)
1406 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 1;
1407 skip_emulated_instruction(&svm
->vcpu
);
1408 return kvm_emulate_halt(&svm
->vcpu
);
1411 static int vmmcall_interception(struct vcpu_svm
*svm
)
1413 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
1414 skip_emulated_instruction(&svm
->vcpu
);
1415 kvm_emulate_hypercall(&svm
->vcpu
);
1419 static int nested_svm_check_permissions(struct vcpu_svm
*svm
)
1421 if (!(svm
->vcpu
.arch
.efer
& EFER_SVME
)
1422 || !is_paging(&svm
->vcpu
)) {
1423 kvm_queue_exception(&svm
->vcpu
, UD_VECTOR
);
1427 if (svm
->vmcb
->save
.cpl
) {
1428 kvm_inject_gp(&svm
->vcpu
, 0);
1435 static int nested_svm_check_exception(struct vcpu_svm
*svm
, unsigned nr
,
1436 bool has_error_code
, u32 error_code
)
1440 if (!is_nested(svm
))
1443 svm
->vmcb
->control
.exit_code
= SVM_EXIT_EXCP_BASE
+ nr
;
1444 svm
->vmcb
->control
.exit_code_hi
= 0;
1445 svm
->vmcb
->control
.exit_info_1
= error_code
;
1446 svm
->vmcb
->control
.exit_info_2
= svm
->vcpu
.arch
.cr2
;
1448 vmexit
= nested_svm_intercept(svm
);
1449 if (vmexit
== NESTED_EXIT_DONE
)
1450 svm
->nested
.exit_required
= true;
1455 /* This function returns true if it is save to enable the irq window */
1456 static inline bool nested_svm_intr(struct vcpu_svm
*svm
)
1458 if (!is_nested(svm
))
1461 if (!(svm
->vcpu
.arch
.hflags
& HF_VINTR_MASK
))
1464 if (!(svm
->vcpu
.arch
.hflags
& HF_HIF_MASK
))
1467 svm
->vmcb
->control
.exit_code
= SVM_EXIT_INTR
;
1469 if (svm
->nested
.intercept
& 1ULL) {
1471 * The #vmexit can't be emulated here directly because this
1472 * code path runs with irqs and preemtion disabled. A
1473 * #vmexit emulation might sleep. Only signal request for
1476 svm
->nested
.exit_required
= true;
1477 trace_kvm_nested_intr_vmexit(svm
->vmcb
->save
.rip
);
1484 static void *nested_svm_map(struct vcpu_svm
*svm
, u64 gpa
, struct page
**_page
)
1490 page
= gfn_to_page(svm
->vcpu
.kvm
, gpa
>> PAGE_SHIFT
);
1491 if (is_error_page(page
))
1499 kvm_release_page_clean(page
);
1500 kvm_inject_gp(&svm
->vcpu
, 0);
1505 static void nested_svm_unmap(struct page
*page
)
1508 kvm_release_page_dirty(page
);
1511 static bool nested_svm_exit_handled_msr(struct vcpu_svm
*svm
)
1513 u32 param
= svm
->vmcb
->control
.exit_info_1
& 1;
1514 u32 msr
= svm
->vcpu
.arch
.regs
[VCPU_REGS_RCX
];
1519 if (!(svm
->nested
.intercept
& (1ULL << INTERCEPT_MSR_PROT
)))
1527 case 0xc0000000 ... 0xc0001fff:
1528 t0
= (8192 + msr
- 0xc0000000) * 2;
1532 case 0xc0010000 ... 0xc0011fff:
1533 t0
= (16384 + msr
- 0xc0010000) * 2;
1542 if (!kvm_read_guest(svm
->vcpu
.kvm
, svm
->nested
.vmcb_msrpm
+ t1
, &val
, 1))
1543 ret
= val
& ((1 << param
) << t0
);
1549 static int nested_svm_exit_special(struct vcpu_svm
*svm
)
1551 u32 exit_code
= svm
->vmcb
->control
.exit_code
;
1553 switch (exit_code
) {
1556 return NESTED_EXIT_HOST
;
1557 /* For now we are always handling NPFs when using them */
1560 return NESTED_EXIT_HOST
;
1562 /* When we're shadowing, trap PFs */
1563 case SVM_EXIT_EXCP_BASE
+ PF_VECTOR
:
1565 return NESTED_EXIT_HOST
;
1567 case SVM_EXIT_EXCP_BASE
+ NM_VECTOR
:
1568 nm_interception(svm
);
1574 return NESTED_EXIT_CONTINUE
;
1578 * If this function returns true, this #vmexit was already handled
1580 static int nested_svm_intercept(struct vcpu_svm
*svm
)
1582 u32 exit_code
= svm
->vmcb
->control
.exit_code
;
1583 int vmexit
= NESTED_EXIT_HOST
;
1585 switch (exit_code
) {
1587 vmexit
= nested_svm_exit_handled_msr(svm
);
1589 case SVM_EXIT_READ_CR0
... SVM_EXIT_READ_CR8
: {
1590 u32 cr_bits
= 1 << (exit_code
- SVM_EXIT_READ_CR0
);
1591 if (svm
->nested
.intercept_cr_read
& cr_bits
)
1592 vmexit
= NESTED_EXIT_DONE
;
1595 case SVM_EXIT_WRITE_CR0
... SVM_EXIT_WRITE_CR8
: {
1596 u32 cr_bits
= 1 << (exit_code
- SVM_EXIT_WRITE_CR0
);
1597 if (svm
->nested
.intercept_cr_write
& cr_bits
)
1598 vmexit
= NESTED_EXIT_DONE
;
1601 case SVM_EXIT_READ_DR0
... SVM_EXIT_READ_DR7
: {
1602 u32 dr_bits
= 1 << (exit_code
- SVM_EXIT_READ_DR0
);
1603 if (svm
->nested
.intercept_dr_read
& dr_bits
)
1604 vmexit
= NESTED_EXIT_DONE
;
1607 case SVM_EXIT_WRITE_DR0
... SVM_EXIT_WRITE_DR7
: {
1608 u32 dr_bits
= 1 << (exit_code
- SVM_EXIT_WRITE_DR0
);
1609 if (svm
->nested
.intercept_dr_write
& dr_bits
)
1610 vmexit
= NESTED_EXIT_DONE
;
1613 case SVM_EXIT_EXCP_BASE
... SVM_EXIT_EXCP_BASE
+ 0x1f: {
1614 u32 excp_bits
= 1 << (exit_code
- SVM_EXIT_EXCP_BASE
);
1615 if (svm
->nested
.intercept_exceptions
& excp_bits
)
1616 vmexit
= NESTED_EXIT_DONE
;
1620 u64 exit_bits
= 1ULL << (exit_code
- SVM_EXIT_INTR
);
1621 if (svm
->nested
.intercept
& exit_bits
)
1622 vmexit
= NESTED_EXIT_DONE
;
1629 static int nested_svm_exit_handled(struct vcpu_svm
*svm
)
1633 vmexit
= nested_svm_intercept(svm
);
1635 if (vmexit
== NESTED_EXIT_DONE
)
1636 nested_svm_vmexit(svm
);
1641 static inline void copy_vmcb_control_area(struct vmcb
*dst_vmcb
, struct vmcb
*from_vmcb
)
1643 struct vmcb_control_area
*dst
= &dst_vmcb
->control
;
1644 struct vmcb_control_area
*from
= &from_vmcb
->control
;
1646 dst
->intercept_cr_read
= from
->intercept_cr_read
;
1647 dst
->intercept_cr_write
= from
->intercept_cr_write
;
1648 dst
->intercept_dr_read
= from
->intercept_dr_read
;
1649 dst
->intercept_dr_write
= from
->intercept_dr_write
;
1650 dst
->intercept_exceptions
= from
->intercept_exceptions
;
1651 dst
->intercept
= from
->intercept
;
1652 dst
->iopm_base_pa
= from
->iopm_base_pa
;
1653 dst
->msrpm_base_pa
= from
->msrpm_base_pa
;
1654 dst
->tsc_offset
= from
->tsc_offset
;
1655 dst
->asid
= from
->asid
;
1656 dst
->tlb_ctl
= from
->tlb_ctl
;
1657 dst
->int_ctl
= from
->int_ctl
;
1658 dst
->int_vector
= from
->int_vector
;
1659 dst
->int_state
= from
->int_state
;
1660 dst
->exit_code
= from
->exit_code
;
1661 dst
->exit_code_hi
= from
->exit_code_hi
;
1662 dst
->exit_info_1
= from
->exit_info_1
;
1663 dst
->exit_info_2
= from
->exit_info_2
;
1664 dst
->exit_int_info
= from
->exit_int_info
;
1665 dst
->exit_int_info_err
= from
->exit_int_info_err
;
1666 dst
->nested_ctl
= from
->nested_ctl
;
1667 dst
->event_inj
= from
->event_inj
;
1668 dst
->event_inj_err
= from
->event_inj_err
;
1669 dst
->nested_cr3
= from
->nested_cr3
;
1670 dst
->lbr_ctl
= from
->lbr_ctl
;
1673 static int nested_svm_vmexit(struct vcpu_svm
*svm
)
1675 struct vmcb
*nested_vmcb
;
1676 struct vmcb
*hsave
= svm
->nested
.hsave
;
1677 struct vmcb
*vmcb
= svm
->vmcb
;
1680 trace_kvm_nested_vmexit_inject(vmcb
->control
.exit_code
,
1681 vmcb
->control
.exit_info_1
,
1682 vmcb
->control
.exit_info_2
,
1683 vmcb
->control
.exit_int_info
,
1684 vmcb
->control
.exit_int_info_err
);
1686 nested_vmcb
= nested_svm_map(svm
, svm
->nested
.vmcb
, &page
);
1690 /* Exit nested SVM mode */
1691 svm
->nested
.vmcb
= 0;
1693 /* Give the current vmcb to the guest */
1696 nested_vmcb
->save
.es
= vmcb
->save
.es
;
1697 nested_vmcb
->save
.cs
= vmcb
->save
.cs
;
1698 nested_vmcb
->save
.ss
= vmcb
->save
.ss
;
1699 nested_vmcb
->save
.ds
= vmcb
->save
.ds
;
1700 nested_vmcb
->save
.gdtr
= vmcb
->save
.gdtr
;
1701 nested_vmcb
->save
.idtr
= vmcb
->save
.idtr
;
1702 nested_vmcb
->save
.cr0
= kvm_read_cr0(&svm
->vcpu
);
1704 nested_vmcb
->save
.cr3
= vmcb
->save
.cr3
;
1706 nested_vmcb
->save
.cr3
= svm
->vcpu
.arch
.cr3
;
1707 nested_vmcb
->save
.cr2
= vmcb
->save
.cr2
;
1708 nested_vmcb
->save
.cr4
= svm
->vcpu
.arch
.cr4
;
1709 nested_vmcb
->save
.rflags
= vmcb
->save
.rflags
;
1710 nested_vmcb
->save
.rip
= vmcb
->save
.rip
;
1711 nested_vmcb
->save
.rsp
= vmcb
->save
.rsp
;
1712 nested_vmcb
->save
.rax
= vmcb
->save
.rax
;
1713 nested_vmcb
->save
.dr7
= vmcb
->save
.dr7
;
1714 nested_vmcb
->save
.dr6
= vmcb
->save
.dr6
;
1715 nested_vmcb
->save
.cpl
= vmcb
->save
.cpl
;
1717 nested_vmcb
->control
.int_ctl
= vmcb
->control
.int_ctl
;
1718 nested_vmcb
->control
.int_vector
= vmcb
->control
.int_vector
;
1719 nested_vmcb
->control
.int_state
= vmcb
->control
.int_state
;
1720 nested_vmcb
->control
.exit_code
= vmcb
->control
.exit_code
;
1721 nested_vmcb
->control
.exit_code_hi
= vmcb
->control
.exit_code_hi
;
1722 nested_vmcb
->control
.exit_info_1
= vmcb
->control
.exit_info_1
;
1723 nested_vmcb
->control
.exit_info_2
= vmcb
->control
.exit_info_2
;
1724 nested_vmcb
->control
.exit_int_info
= vmcb
->control
.exit_int_info
;
1725 nested_vmcb
->control
.exit_int_info_err
= vmcb
->control
.exit_int_info_err
;
1728 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
1729 * to make sure that we do not lose injected events. So check event_inj
1730 * here and copy it to exit_int_info if it is valid.
1731 * Exit_int_info and event_inj can't be both valid because the case
1732 * below only happens on a VMRUN instruction intercept which has
1733 * no valid exit_int_info set.
1735 if (vmcb
->control
.event_inj
& SVM_EVTINJ_VALID
) {
1736 struct vmcb_control_area
*nc
= &nested_vmcb
->control
;
1738 nc
->exit_int_info
= vmcb
->control
.event_inj
;
1739 nc
->exit_int_info_err
= vmcb
->control
.event_inj_err
;
1742 nested_vmcb
->control
.tlb_ctl
= 0;
1743 nested_vmcb
->control
.event_inj
= 0;
1744 nested_vmcb
->control
.event_inj_err
= 0;
1746 /* We always set V_INTR_MASKING and remember the old value in hflags */
1747 if (!(svm
->vcpu
.arch
.hflags
& HF_VINTR_MASK
))
1748 nested_vmcb
->control
.int_ctl
&= ~V_INTR_MASKING_MASK
;
1750 /* Restore the original control entries */
1751 copy_vmcb_control_area(vmcb
, hsave
);
1753 kvm_clear_exception_queue(&svm
->vcpu
);
1754 kvm_clear_interrupt_queue(&svm
->vcpu
);
1756 /* Restore selected save entries */
1757 svm
->vmcb
->save
.es
= hsave
->save
.es
;
1758 svm
->vmcb
->save
.cs
= hsave
->save
.cs
;
1759 svm
->vmcb
->save
.ss
= hsave
->save
.ss
;
1760 svm
->vmcb
->save
.ds
= hsave
->save
.ds
;
1761 svm
->vmcb
->save
.gdtr
= hsave
->save
.gdtr
;
1762 svm
->vmcb
->save
.idtr
= hsave
->save
.idtr
;
1763 svm
->vmcb
->save
.rflags
= hsave
->save
.rflags
;
1764 svm_set_efer(&svm
->vcpu
, hsave
->save
.efer
);
1765 svm_set_cr0(&svm
->vcpu
, hsave
->save
.cr0
| X86_CR0_PE
);
1766 svm_set_cr4(&svm
->vcpu
, hsave
->save
.cr4
);
1768 svm
->vmcb
->save
.cr3
= hsave
->save
.cr3
;
1769 svm
->vcpu
.arch
.cr3
= hsave
->save
.cr3
;
1771 kvm_set_cr3(&svm
->vcpu
, hsave
->save
.cr3
);
1773 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RAX
, hsave
->save
.rax
);
1774 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RSP
, hsave
->save
.rsp
);
1775 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RIP
, hsave
->save
.rip
);
1776 svm
->vmcb
->save
.dr7
= 0;
1777 svm
->vmcb
->save
.cpl
= 0;
1778 svm
->vmcb
->control
.exit_int_info
= 0;
1780 nested_svm_unmap(page
);
1782 kvm_mmu_reset_context(&svm
->vcpu
);
1783 kvm_mmu_load(&svm
->vcpu
);
1788 static bool nested_svm_vmrun_msrpm(struct vcpu_svm
*svm
)
1794 nested_msrpm
= nested_svm_map(svm
, svm
->nested
.vmcb_msrpm
, &page
);
1798 for (i
=0; i
< PAGE_SIZE
* (1 << MSRPM_ALLOC_ORDER
) / 4; i
++)
1799 svm
->nested
.msrpm
[i
] = svm
->msrpm
[i
] | nested_msrpm
[i
];
1801 svm
->vmcb
->control
.msrpm_base_pa
= __pa(svm
->nested
.msrpm
);
1803 nested_svm_unmap(page
);
1808 static bool nested_svm_vmrun(struct vcpu_svm
*svm
)
1810 struct vmcb
*nested_vmcb
;
1811 struct vmcb
*hsave
= svm
->nested
.hsave
;
1812 struct vmcb
*vmcb
= svm
->vmcb
;
1816 vmcb_gpa
= svm
->vmcb
->save
.rax
;
1818 nested_vmcb
= nested_svm_map(svm
, svm
->vmcb
->save
.rax
, &page
);
1822 trace_kvm_nested_vmrun(svm
->vmcb
->save
.rip
- 3, svm
->nested
.vmcb
,
1823 nested_vmcb
->save
.rip
,
1824 nested_vmcb
->control
.int_ctl
,
1825 nested_vmcb
->control
.event_inj
,
1826 nested_vmcb
->control
.nested_ctl
);
1828 /* Clear internal status */
1829 kvm_clear_exception_queue(&svm
->vcpu
);
1830 kvm_clear_interrupt_queue(&svm
->vcpu
);
1832 /* Save the old vmcb, so we don't need to pick what we save, but
1833 can restore everything when a VMEXIT occurs */
1834 hsave
->save
.es
= vmcb
->save
.es
;
1835 hsave
->save
.cs
= vmcb
->save
.cs
;
1836 hsave
->save
.ss
= vmcb
->save
.ss
;
1837 hsave
->save
.ds
= vmcb
->save
.ds
;
1838 hsave
->save
.gdtr
= vmcb
->save
.gdtr
;
1839 hsave
->save
.idtr
= vmcb
->save
.idtr
;
1840 hsave
->save
.efer
= svm
->vcpu
.arch
.efer
;
1841 hsave
->save
.cr0
= kvm_read_cr0(&svm
->vcpu
);
1842 hsave
->save
.cr4
= svm
->vcpu
.arch
.cr4
;
1843 hsave
->save
.rflags
= vmcb
->save
.rflags
;
1844 hsave
->save
.rip
= svm
->next_rip
;
1845 hsave
->save
.rsp
= vmcb
->save
.rsp
;
1846 hsave
->save
.rax
= vmcb
->save
.rax
;
1848 hsave
->save
.cr3
= vmcb
->save
.cr3
;
1850 hsave
->save
.cr3
= svm
->vcpu
.arch
.cr3
;
1852 copy_vmcb_control_area(hsave
, vmcb
);
1854 if (svm
->vmcb
->save
.rflags
& X86_EFLAGS_IF
)
1855 svm
->vcpu
.arch
.hflags
|= HF_HIF_MASK
;
1857 svm
->vcpu
.arch
.hflags
&= ~HF_HIF_MASK
;
1859 /* Load the nested guest state */
1860 svm
->vmcb
->save
.es
= nested_vmcb
->save
.es
;
1861 svm
->vmcb
->save
.cs
= nested_vmcb
->save
.cs
;
1862 svm
->vmcb
->save
.ss
= nested_vmcb
->save
.ss
;
1863 svm
->vmcb
->save
.ds
= nested_vmcb
->save
.ds
;
1864 svm
->vmcb
->save
.gdtr
= nested_vmcb
->save
.gdtr
;
1865 svm
->vmcb
->save
.idtr
= nested_vmcb
->save
.idtr
;
1866 svm
->vmcb
->save
.rflags
= nested_vmcb
->save
.rflags
;
1867 svm_set_efer(&svm
->vcpu
, nested_vmcb
->save
.efer
);
1868 svm_set_cr0(&svm
->vcpu
, nested_vmcb
->save
.cr0
);
1869 svm_set_cr4(&svm
->vcpu
, nested_vmcb
->save
.cr4
);
1871 svm
->vmcb
->save
.cr3
= nested_vmcb
->save
.cr3
;
1872 svm
->vcpu
.arch
.cr3
= nested_vmcb
->save
.cr3
;
1874 kvm_set_cr3(&svm
->vcpu
, nested_vmcb
->save
.cr3
);
1875 kvm_mmu_reset_context(&svm
->vcpu
);
1877 svm
->vmcb
->save
.cr2
= svm
->vcpu
.arch
.cr2
= nested_vmcb
->save
.cr2
;
1878 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RAX
, nested_vmcb
->save
.rax
);
1879 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RSP
, nested_vmcb
->save
.rsp
);
1880 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RIP
, nested_vmcb
->save
.rip
);
1881 /* In case we don't even reach vcpu_run, the fields are not updated */
1882 svm
->vmcb
->save
.rax
= nested_vmcb
->save
.rax
;
1883 svm
->vmcb
->save
.rsp
= nested_vmcb
->save
.rsp
;
1884 svm
->vmcb
->save
.rip
= nested_vmcb
->save
.rip
;
1885 svm
->vmcb
->save
.dr7
= nested_vmcb
->save
.dr7
;
1886 svm
->vmcb
->save
.dr6
= nested_vmcb
->save
.dr6
;
1887 svm
->vmcb
->save
.cpl
= nested_vmcb
->save
.cpl
;
1889 svm
->nested
.vmcb_msrpm
= nested_vmcb
->control
.msrpm_base_pa
;
1891 /* cache intercepts */
1892 svm
->nested
.intercept_cr_read
= nested_vmcb
->control
.intercept_cr_read
;
1893 svm
->nested
.intercept_cr_write
= nested_vmcb
->control
.intercept_cr_write
;
1894 svm
->nested
.intercept_dr_read
= nested_vmcb
->control
.intercept_dr_read
;
1895 svm
->nested
.intercept_dr_write
= nested_vmcb
->control
.intercept_dr_write
;
1896 svm
->nested
.intercept_exceptions
= nested_vmcb
->control
.intercept_exceptions
;
1897 svm
->nested
.intercept
= nested_vmcb
->control
.intercept
;
1899 force_new_asid(&svm
->vcpu
);
1900 svm
->vmcb
->control
.int_ctl
= nested_vmcb
->control
.int_ctl
| V_INTR_MASKING_MASK
;
1901 if (nested_vmcb
->control
.int_ctl
& V_INTR_MASKING_MASK
)
1902 svm
->vcpu
.arch
.hflags
|= HF_VINTR_MASK
;
1904 svm
->vcpu
.arch
.hflags
&= ~HF_VINTR_MASK
;
1906 if (svm
->vcpu
.arch
.hflags
& HF_VINTR_MASK
) {
1907 /* We only want the cr8 intercept bits of the guest */
1908 svm
->vmcb
->control
.intercept_cr_read
&= ~INTERCEPT_CR8_MASK
;
1909 svm
->vmcb
->control
.intercept_cr_write
&= ~INTERCEPT_CR8_MASK
;
1912 /* We don't want a nested guest to be more powerful than the guest,
1913 so all intercepts are ORed */
1914 svm
->vmcb
->control
.intercept_cr_read
|=
1915 nested_vmcb
->control
.intercept_cr_read
;
1916 svm
->vmcb
->control
.intercept_cr_write
|=
1917 nested_vmcb
->control
.intercept_cr_write
;
1918 svm
->vmcb
->control
.intercept_dr_read
|=
1919 nested_vmcb
->control
.intercept_dr_read
;
1920 svm
->vmcb
->control
.intercept_dr_write
|=
1921 nested_vmcb
->control
.intercept_dr_write
;
1922 svm
->vmcb
->control
.intercept_exceptions
|=
1923 nested_vmcb
->control
.intercept_exceptions
;
1925 svm
->vmcb
->control
.intercept
|= nested_vmcb
->control
.intercept
;
1927 svm
->vmcb
->control
.lbr_ctl
= nested_vmcb
->control
.lbr_ctl
;
1928 svm
->vmcb
->control
.int_vector
= nested_vmcb
->control
.int_vector
;
1929 svm
->vmcb
->control
.int_state
= nested_vmcb
->control
.int_state
;
1930 svm
->vmcb
->control
.tsc_offset
+= nested_vmcb
->control
.tsc_offset
;
1931 svm
->vmcb
->control
.event_inj
= nested_vmcb
->control
.event_inj
;
1932 svm
->vmcb
->control
.event_inj_err
= nested_vmcb
->control
.event_inj_err
;
1934 nested_svm_unmap(page
);
1936 /* nested_vmcb is our indicator if nested SVM is activated */
1937 svm
->nested
.vmcb
= vmcb_gpa
;
1944 static void nested_svm_vmloadsave(struct vmcb
*from_vmcb
, struct vmcb
*to_vmcb
)
1946 to_vmcb
->save
.fs
= from_vmcb
->save
.fs
;
1947 to_vmcb
->save
.gs
= from_vmcb
->save
.gs
;
1948 to_vmcb
->save
.tr
= from_vmcb
->save
.tr
;
1949 to_vmcb
->save
.ldtr
= from_vmcb
->save
.ldtr
;
1950 to_vmcb
->save
.kernel_gs_base
= from_vmcb
->save
.kernel_gs_base
;
1951 to_vmcb
->save
.star
= from_vmcb
->save
.star
;
1952 to_vmcb
->save
.lstar
= from_vmcb
->save
.lstar
;
1953 to_vmcb
->save
.cstar
= from_vmcb
->save
.cstar
;
1954 to_vmcb
->save
.sfmask
= from_vmcb
->save
.sfmask
;
1955 to_vmcb
->save
.sysenter_cs
= from_vmcb
->save
.sysenter_cs
;
1956 to_vmcb
->save
.sysenter_esp
= from_vmcb
->save
.sysenter_esp
;
1957 to_vmcb
->save
.sysenter_eip
= from_vmcb
->save
.sysenter_eip
;
1960 static int vmload_interception(struct vcpu_svm
*svm
)
1962 struct vmcb
*nested_vmcb
;
1965 if (nested_svm_check_permissions(svm
))
1968 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
1969 skip_emulated_instruction(&svm
->vcpu
);
1971 nested_vmcb
= nested_svm_map(svm
, svm
->vmcb
->save
.rax
, &page
);
1975 nested_svm_vmloadsave(nested_vmcb
, svm
->vmcb
);
1976 nested_svm_unmap(page
);
1981 static int vmsave_interception(struct vcpu_svm
*svm
)
1983 struct vmcb
*nested_vmcb
;
1986 if (nested_svm_check_permissions(svm
))
1989 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
1990 skip_emulated_instruction(&svm
->vcpu
);
1992 nested_vmcb
= nested_svm_map(svm
, svm
->vmcb
->save
.rax
, &page
);
1996 nested_svm_vmloadsave(svm
->vmcb
, nested_vmcb
);
1997 nested_svm_unmap(page
);
2002 static int vmrun_interception(struct vcpu_svm
*svm
)
2004 if (nested_svm_check_permissions(svm
))
2007 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
2008 skip_emulated_instruction(&svm
->vcpu
);
2010 if (!nested_svm_vmrun(svm
))
2013 if (!nested_svm_vmrun_msrpm(svm
))
2020 svm
->vmcb
->control
.exit_code
= SVM_EXIT_ERR
;
2021 svm
->vmcb
->control
.exit_code_hi
= 0;
2022 svm
->vmcb
->control
.exit_info_1
= 0;
2023 svm
->vmcb
->control
.exit_info_2
= 0;
2025 nested_svm_vmexit(svm
);
2030 static int stgi_interception(struct vcpu_svm
*svm
)
2032 if (nested_svm_check_permissions(svm
))
2035 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
2036 skip_emulated_instruction(&svm
->vcpu
);
2043 static int clgi_interception(struct vcpu_svm
*svm
)
2045 if (nested_svm_check_permissions(svm
))
2048 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
2049 skip_emulated_instruction(&svm
->vcpu
);
2053 /* After a CLGI no interrupts should come */
2054 svm_clear_vintr(svm
);
2055 svm
->vmcb
->control
.int_ctl
&= ~V_IRQ_MASK
;
2060 static int invlpga_interception(struct vcpu_svm
*svm
)
2062 struct kvm_vcpu
*vcpu
= &svm
->vcpu
;
2064 trace_kvm_invlpga(svm
->vmcb
->save
.rip
, vcpu
->arch
.regs
[VCPU_REGS_RCX
],
2065 vcpu
->arch
.regs
[VCPU_REGS_RAX
]);
2067 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2068 kvm_mmu_invlpg(vcpu
, vcpu
->arch
.regs
[VCPU_REGS_RAX
]);
2070 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
2071 skip_emulated_instruction(&svm
->vcpu
);
2075 static int skinit_interception(struct vcpu_svm
*svm
)
2077 trace_kvm_skinit(svm
->vmcb
->save
.rip
, svm
->vcpu
.arch
.regs
[VCPU_REGS_RAX
]);
2079 kvm_queue_exception(&svm
->vcpu
, UD_VECTOR
);
2083 static int invalid_op_interception(struct vcpu_svm
*svm
)
2085 kvm_queue_exception(&svm
->vcpu
, UD_VECTOR
);
2089 static int task_switch_interception(struct vcpu_svm
*svm
)
2093 int int_type
= svm
->vmcb
->control
.exit_int_info
&
2094 SVM_EXITINTINFO_TYPE_MASK
;
2095 int int_vec
= svm
->vmcb
->control
.exit_int_info
& SVM_EVTINJ_VEC_MASK
;
2097 svm
->vmcb
->control
.exit_int_info
& SVM_EXITINTINFO_TYPE_MASK
;
2099 svm
->vmcb
->control
.exit_int_info
& SVM_EXITINTINFO_VALID
;
2101 tss_selector
= (u16
)svm
->vmcb
->control
.exit_info_1
;
2103 if (svm
->vmcb
->control
.exit_info_2
&
2104 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET
))
2105 reason
= TASK_SWITCH_IRET
;
2106 else if (svm
->vmcb
->control
.exit_info_2
&
2107 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP
))
2108 reason
= TASK_SWITCH_JMP
;
2110 reason
= TASK_SWITCH_GATE
;
2112 reason
= TASK_SWITCH_CALL
;
2114 if (reason
== TASK_SWITCH_GATE
) {
2116 case SVM_EXITINTINFO_TYPE_NMI
:
2117 svm
->vcpu
.arch
.nmi_injected
= false;
2119 case SVM_EXITINTINFO_TYPE_EXEPT
:
2120 kvm_clear_exception_queue(&svm
->vcpu
);
2122 case SVM_EXITINTINFO_TYPE_INTR
:
2123 kvm_clear_interrupt_queue(&svm
->vcpu
);
2130 if (reason
!= TASK_SWITCH_GATE
||
2131 int_type
== SVM_EXITINTINFO_TYPE_SOFT
||
2132 (int_type
== SVM_EXITINTINFO_TYPE_EXEPT
&&
2133 (int_vec
== OF_VECTOR
|| int_vec
== BP_VECTOR
)))
2134 skip_emulated_instruction(&svm
->vcpu
);
2136 return kvm_task_switch(&svm
->vcpu
, tss_selector
, reason
);
2139 static int cpuid_interception(struct vcpu_svm
*svm
)
2141 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 2;
2142 kvm_emulate_cpuid(&svm
->vcpu
);
2146 static int iret_interception(struct vcpu_svm
*svm
)
2148 ++svm
->vcpu
.stat
.nmi_window_exits
;
2149 svm
->vmcb
->control
.intercept
&= ~(1UL << INTERCEPT_IRET
);
2150 svm
->vcpu
.arch
.hflags
|= HF_IRET_MASK
;
2154 static int invlpg_interception(struct vcpu_svm
*svm
)
2156 if (emulate_instruction(&svm
->vcpu
, 0, 0, 0) != EMULATE_DONE
)
2157 pr_unimpl(&svm
->vcpu
, "%s: failed\n", __func__
);
2161 static int emulate_on_interception(struct vcpu_svm
*svm
)
2163 if (emulate_instruction(&svm
->vcpu
, 0, 0, 0) != EMULATE_DONE
)
2164 pr_unimpl(&svm
->vcpu
, "%s: failed\n", __func__
);
2168 static int cr8_write_interception(struct vcpu_svm
*svm
)
2170 struct kvm_run
*kvm_run
= svm
->vcpu
.run
;
2172 u8 cr8_prev
= kvm_get_cr8(&svm
->vcpu
);
2173 /* instruction emulation calls kvm_set_cr8() */
2174 emulate_instruction(&svm
->vcpu
, 0, 0, 0);
2175 if (irqchip_in_kernel(svm
->vcpu
.kvm
)) {
2176 svm
->vmcb
->control
.intercept_cr_write
&= ~INTERCEPT_CR8_MASK
;
2179 if (cr8_prev
<= kvm_get_cr8(&svm
->vcpu
))
2181 kvm_run
->exit_reason
= KVM_EXIT_SET_TPR
;
2185 static int svm_get_msr(struct kvm_vcpu
*vcpu
, unsigned ecx
, u64
*data
)
2187 struct vcpu_svm
*svm
= to_svm(vcpu
);
2190 case MSR_IA32_TSC
: {
2194 tsc_offset
= svm
->nested
.hsave
->control
.tsc_offset
;
2196 tsc_offset
= svm
->vmcb
->control
.tsc_offset
;
2198 *data
= tsc_offset
+ native_read_tsc();
2202 *data
= svm
->vmcb
->save
.star
;
2204 #ifdef CONFIG_X86_64
2206 *data
= svm
->vmcb
->save
.lstar
;
2209 *data
= svm
->vmcb
->save
.cstar
;
2211 case MSR_KERNEL_GS_BASE
:
2212 *data
= svm
->vmcb
->save
.kernel_gs_base
;
2214 case MSR_SYSCALL_MASK
:
2215 *data
= svm
->vmcb
->save
.sfmask
;
2218 case MSR_IA32_SYSENTER_CS
:
2219 *data
= svm
->vmcb
->save
.sysenter_cs
;
2221 case MSR_IA32_SYSENTER_EIP
:
2222 *data
= svm
->sysenter_eip
;
2224 case MSR_IA32_SYSENTER_ESP
:
2225 *data
= svm
->sysenter_esp
;
2227 /* Nobody will change the following 5 values in the VMCB so
2228 we can safely return them on rdmsr. They will always be 0
2229 until LBRV is implemented. */
2230 case MSR_IA32_DEBUGCTLMSR
:
2231 *data
= svm
->vmcb
->save
.dbgctl
;
2233 case MSR_IA32_LASTBRANCHFROMIP
:
2234 *data
= svm
->vmcb
->save
.br_from
;
2236 case MSR_IA32_LASTBRANCHTOIP
:
2237 *data
= svm
->vmcb
->save
.br_to
;
2239 case MSR_IA32_LASTINTFROMIP
:
2240 *data
= svm
->vmcb
->save
.last_excp_from
;
2242 case MSR_IA32_LASTINTTOIP
:
2243 *data
= svm
->vmcb
->save
.last_excp_to
;
2245 case MSR_VM_HSAVE_PA
:
2246 *data
= svm
->nested
.hsave_msr
;
2251 case MSR_IA32_UCODE_REV
:
2255 return kvm_get_msr_common(vcpu
, ecx
, data
);
2260 static int rdmsr_interception(struct vcpu_svm
*svm
)
2262 u32 ecx
= svm
->vcpu
.arch
.regs
[VCPU_REGS_RCX
];
2265 if (svm_get_msr(&svm
->vcpu
, ecx
, &data
)) {
2266 trace_kvm_msr_read_ex(ecx
);
2267 kvm_inject_gp(&svm
->vcpu
, 0);
2269 trace_kvm_msr_read(ecx
, data
);
2271 svm
->vcpu
.arch
.regs
[VCPU_REGS_RAX
] = data
& 0xffffffff;
2272 svm
->vcpu
.arch
.regs
[VCPU_REGS_RDX
] = data
>> 32;
2273 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 2;
2274 skip_emulated_instruction(&svm
->vcpu
);
2279 static int svm_set_msr(struct kvm_vcpu
*vcpu
, unsigned ecx
, u64 data
)
2281 struct vcpu_svm
*svm
= to_svm(vcpu
);
2284 case MSR_IA32_TSC
: {
2285 u64 tsc_offset
= data
- native_read_tsc();
2286 u64 g_tsc_offset
= 0;
2288 if (is_nested(svm
)) {
2289 g_tsc_offset
= svm
->vmcb
->control
.tsc_offset
-
2290 svm
->nested
.hsave
->control
.tsc_offset
;
2291 svm
->nested
.hsave
->control
.tsc_offset
= tsc_offset
;
2294 svm
->vmcb
->control
.tsc_offset
= tsc_offset
+ g_tsc_offset
;
2299 svm
->vmcb
->save
.star
= data
;
2301 #ifdef CONFIG_X86_64
2303 svm
->vmcb
->save
.lstar
= data
;
2306 svm
->vmcb
->save
.cstar
= data
;
2308 case MSR_KERNEL_GS_BASE
:
2309 svm
->vmcb
->save
.kernel_gs_base
= data
;
2311 case MSR_SYSCALL_MASK
:
2312 svm
->vmcb
->save
.sfmask
= data
;
2315 case MSR_IA32_SYSENTER_CS
:
2316 svm
->vmcb
->save
.sysenter_cs
= data
;
2318 case MSR_IA32_SYSENTER_EIP
:
2319 svm
->sysenter_eip
= data
;
2320 svm
->vmcb
->save
.sysenter_eip
= data
;
2322 case MSR_IA32_SYSENTER_ESP
:
2323 svm
->sysenter_esp
= data
;
2324 svm
->vmcb
->save
.sysenter_esp
= data
;
2326 case MSR_IA32_DEBUGCTLMSR
:
2327 if (!svm_has(SVM_FEATURE_LBRV
)) {
2328 pr_unimpl(vcpu
, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
2332 if (data
& DEBUGCTL_RESERVED_BITS
)
2335 svm
->vmcb
->save
.dbgctl
= data
;
2336 if (data
& (1ULL<<0))
2337 svm_enable_lbrv(svm
);
2339 svm_disable_lbrv(svm
);
2341 case MSR_VM_HSAVE_PA
:
2342 svm
->nested
.hsave_msr
= data
;
2346 pr_unimpl(vcpu
, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx
, data
);
2349 return kvm_set_msr_common(vcpu
, ecx
, data
);
2354 static int wrmsr_interception(struct vcpu_svm
*svm
)
2356 u32 ecx
= svm
->vcpu
.arch
.regs
[VCPU_REGS_RCX
];
2357 u64 data
= (svm
->vcpu
.arch
.regs
[VCPU_REGS_RAX
] & -1u)
2358 | ((u64
)(svm
->vcpu
.arch
.regs
[VCPU_REGS_RDX
] & -1u) << 32);
2361 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 2;
2362 if (svm_set_msr(&svm
->vcpu
, ecx
, data
)) {
2363 trace_kvm_msr_write_ex(ecx
, data
);
2364 kvm_inject_gp(&svm
->vcpu
, 0);
2366 trace_kvm_msr_write(ecx
, data
);
2367 skip_emulated_instruction(&svm
->vcpu
);
2372 static int msr_interception(struct vcpu_svm
*svm
)
2374 if (svm
->vmcb
->control
.exit_info_1
)
2375 return wrmsr_interception(svm
);
2377 return rdmsr_interception(svm
);
2380 static int interrupt_window_interception(struct vcpu_svm
*svm
)
2382 struct kvm_run
*kvm_run
= svm
->vcpu
.run
;
2384 svm_clear_vintr(svm
);
2385 svm
->vmcb
->control
.int_ctl
&= ~V_IRQ_MASK
;
2387 * If the user space waits to inject interrupts, exit as soon as
2390 if (!irqchip_in_kernel(svm
->vcpu
.kvm
) &&
2391 kvm_run
->request_interrupt_window
&&
2392 !kvm_cpu_has_interrupt(&svm
->vcpu
)) {
2393 ++svm
->vcpu
.stat
.irq_window_exits
;
2394 kvm_run
->exit_reason
= KVM_EXIT_IRQ_WINDOW_OPEN
;
2401 static int pause_interception(struct vcpu_svm
*svm
)
2403 kvm_vcpu_on_spin(&(svm
->vcpu
));
2407 static int (*svm_exit_handlers
[])(struct vcpu_svm
*svm
) = {
2408 [SVM_EXIT_READ_CR0
] = emulate_on_interception
,
2409 [SVM_EXIT_READ_CR3
] = emulate_on_interception
,
2410 [SVM_EXIT_READ_CR4
] = emulate_on_interception
,
2411 [SVM_EXIT_READ_CR8
] = emulate_on_interception
,
2412 [SVM_EXIT_CR0_SEL_WRITE
] = emulate_on_interception
,
2413 [SVM_EXIT_WRITE_CR0
] = emulate_on_interception
,
2414 [SVM_EXIT_WRITE_CR3
] = emulate_on_interception
,
2415 [SVM_EXIT_WRITE_CR4
] = emulate_on_interception
,
2416 [SVM_EXIT_WRITE_CR8
] = cr8_write_interception
,
2417 [SVM_EXIT_READ_DR0
] = emulate_on_interception
,
2418 [SVM_EXIT_READ_DR1
] = emulate_on_interception
,
2419 [SVM_EXIT_READ_DR2
] = emulate_on_interception
,
2420 [SVM_EXIT_READ_DR3
] = emulate_on_interception
,
2421 [SVM_EXIT_READ_DR4
] = emulate_on_interception
,
2422 [SVM_EXIT_READ_DR5
] = emulate_on_interception
,
2423 [SVM_EXIT_READ_DR6
] = emulate_on_interception
,
2424 [SVM_EXIT_READ_DR7
] = emulate_on_interception
,
2425 [SVM_EXIT_WRITE_DR0
] = emulate_on_interception
,
2426 [SVM_EXIT_WRITE_DR1
] = emulate_on_interception
,
2427 [SVM_EXIT_WRITE_DR2
] = emulate_on_interception
,
2428 [SVM_EXIT_WRITE_DR3
] = emulate_on_interception
,
2429 [SVM_EXIT_WRITE_DR4
] = emulate_on_interception
,
2430 [SVM_EXIT_WRITE_DR5
] = emulate_on_interception
,
2431 [SVM_EXIT_WRITE_DR6
] = emulate_on_interception
,
2432 [SVM_EXIT_WRITE_DR7
] = emulate_on_interception
,
2433 [SVM_EXIT_EXCP_BASE
+ DB_VECTOR
] = db_interception
,
2434 [SVM_EXIT_EXCP_BASE
+ BP_VECTOR
] = bp_interception
,
2435 [SVM_EXIT_EXCP_BASE
+ UD_VECTOR
] = ud_interception
,
2436 [SVM_EXIT_EXCP_BASE
+ PF_VECTOR
] = pf_interception
,
2437 [SVM_EXIT_EXCP_BASE
+ NM_VECTOR
] = nm_interception
,
2438 [SVM_EXIT_EXCP_BASE
+ MC_VECTOR
] = mc_interception
,
2439 [SVM_EXIT_INTR
] = intr_interception
,
2440 [SVM_EXIT_NMI
] = nmi_interception
,
2441 [SVM_EXIT_SMI
] = nop_on_interception
,
2442 [SVM_EXIT_INIT
] = nop_on_interception
,
2443 [SVM_EXIT_VINTR
] = interrupt_window_interception
,
2444 /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */
2445 [SVM_EXIT_CPUID
] = cpuid_interception
,
2446 [SVM_EXIT_IRET
] = iret_interception
,
2447 [SVM_EXIT_INVD
] = emulate_on_interception
,
2448 [SVM_EXIT_PAUSE
] = pause_interception
,
2449 [SVM_EXIT_HLT
] = halt_interception
,
2450 [SVM_EXIT_INVLPG
] = invlpg_interception
,
2451 [SVM_EXIT_INVLPGA
] = invlpga_interception
,
2452 [SVM_EXIT_IOIO
] = io_interception
,
2453 [SVM_EXIT_MSR
] = msr_interception
,
2454 [SVM_EXIT_TASK_SWITCH
] = task_switch_interception
,
2455 [SVM_EXIT_SHUTDOWN
] = shutdown_interception
,
2456 [SVM_EXIT_VMRUN
] = vmrun_interception
,
2457 [SVM_EXIT_VMMCALL
] = vmmcall_interception
,
2458 [SVM_EXIT_VMLOAD
] = vmload_interception
,
2459 [SVM_EXIT_VMSAVE
] = vmsave_interception
,
2460 [SVM_EXIT_STGI
] = stgi_interception
,
2461 [SVM_EXIT_CLGI
] = clgi_interception
,
2462 [SVM_EXIT_SKINIT
] = skinit_interception
,
2463 [SVM_EXIT_WBINVD
] = emulate_on_interception
,
2464 [SVM_EXIT_MONITOR
] = invalid_op_interception
,
2465 [SVM_EXIT_MWAIT
] = invalid_op_interception
,
2466 [SVM_EXIT_NPF
] = pf_interception
,
2469 static int handle_exit(struct kvm_vcpu
*vcpu
)
2471 struct vcpu_svm
*svm
= to_svm(vcpu
);
2472 struct kvm_run
*kvm_run
= vcpu
->run
;
2473 u32 exit_code
= svm
->vmcb
->control
.exit_code
;
2475 trace_kvm_exit(exit_code
, svm
->vmcb
->save
.rip
);
2477 if (unlikely(svm
->nested
.exit_required
)) {
2478 nested_svm_vmexit(svm
);
2479 svm
->nested
.exit_required
= false;
2484 if (is_nested(svm
)) {
2487 trace_kvm_nested_vmexit(svm
->vmcb
->save
.rip
, exit_code
,
2488 svm
->vmcb
->control
.exit_info_1
,
2489 svm
->vmcb
->control
.exit_info_2
,
2490 svm
->vmcb
->control
.exit_int_info
,
2491 svm
->vmcb
->control
.exit_int_info_err
);
2493 vmexit
= nested_svm_exit_special(svm
);
2495 if (vmexit
== NESTED_EXIT_CONTINUE
)
2496 vmexit
= nested_svm_exit_handled(svm
);
2498 if (vmexit
== NESTED_EXIT_DONE
)
2502 svm_complete_interrupts(svm
);
2504 if (!(svm
->vmcb
->control
.intercept_cr_write
& INTERCEPT_CR0_MASK
))
2505 vcpu
->arch
.cr0
= svm
->vmcb
->save
.cr0
;
2507 vcpu
->arch
.cr3
= svm
->vmcb
->save
.cr3
;
2509 if (svm
->vmcb
->control
.exit_code
== SVM_EXIT_ERR
) {
2510 kvm_run
->exit_reason
= KVM_EXIT_FAIL_ENTRY
;
2511 kvm_run
->fail_entry
.hardware_entry_failure_reason
2512 = svm
->vmcb
->control
.exit_code
;
2516 if (is_external_interrupt(svm
->vmcb
->control
.exit_int_info
) &&
2517 exit_code
!= SVM_EXIT_EXCP_BASE
+ PF_VECTOR
&&
2518 exit_code
!= SVM_EXIT_NPF
&& exit_code
!= SVM_EXIT_TASK_SWITCH
)
2519 printk(KERN_ERR
"%s: unexpected exit_ini_info 0x%x "
2521 __func__
, svm
->vmcb
->control
.exit_int_info
,
2524 if (exit_code
>= ARRAY_SIZE(svm_exit_handlers
)
2525 || !svm_exit_handlers
[exit_code
]) {
2526 kvm_run
->exit_reason
= KVM_EXIT_UNKNOWN
;
2527 kvm_run
->hw
.hardware_exit_reason
= exit_code
;
2531 return svm_exit_handlers
[exit_code
](svm
);
2534 static void reload_tss(struct kvm_vcpu
*vcpu
)
2536 int cpu
= raw_smp_processor_id();
2538 struct svm_cpu_data
*sd
= per_cpu(svm_data
, cpu
);
2539 sd
->tss_desc
->type
= 9; /* available 32/64-bit TSS */
2543 static void pre_svm_run(struct vcpu_svm
*svm
)
2545 int cpu
= raw_smp_processor_id();
2547 struct svm_cpu_data
*sd
= per_cpu(svm_data
, cpu
);
2549 svm
->vmcb
->control
.tlb_ctl
= TLB_CONTROL_DO_NOTHING
;
2550 /* FIXME: handle wraparound of asid_generation */
2551 if (svm
->asid_generation
!= sd
->asid_generation
)
2555 static void svm_inject_nmi(struct kvm_vcpu
*vcpu
)
2557 struct vcpu_svm
*svm
= to_svm(vcpu
);
2559 svm
->vmcb
->control
.event_inj
= SVM_EVTINJ_VALID
| SVM_EVTINJ_TYPE_NMI
;
2560 vcpu
->arch
.hflags
|= HF_NMI_MASK
;
2561 svm
->vmcb
->control
.intercept
|= (1UL << INTERCEPT_IRET
);
2562 ++vcpu
->stat
.nmi_injections
;
2565 static inline void svm_inject_irq(struct vcpu_svm
*svm
, int irq
)
2567 struct vmcb_control_area
*control
;
2569 trace_kvm_inj_virq(irq
);
2571 ++svm
->vcpu
.stat
.irq_injections
;
2572 control
= &svm
->vmcb
->control
;
2573 control
->int_vector
= irq
;
2574 control
->int_ctl
&= ~V_INTR_PRIO_MASK
;
2575 control
->int_ctl
|= V_IRQ_MASK
|
2576 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT
);
2579 static void svm_set_irq(struct kvm_vcpu
*vcpu
)
2581 struct vcpu_svm
*svm
= to_svm(vcpu
);
2583 BUG_ON(!(gif_set(svm
)));
2585 svm
->vmcb
->control
.event_inj
= vcpu
->arch
.interrupt
.nr
|
2586 SVM_EVTINJ_VALID
| SVM_EVTINJ_TYPE_INTR
;
2589 static void update_cr8_intercept(struct kvm_vcpu
*vcpu
, int tpr
, int irr
)
2591 struct vcpu_svm
*svm
= to_svm(vcpu
);
2593 if (is_nested(svm
) && (vcpu
->arch
.hflags
& HF_VINTR_MASK
))
2600 svm
->vmcb
->control
.intercept_cr_write
|= INTERCEPT_CR8_MASK
;
2603 static int svm_nmi_allowed(struct kvm_vcpu
*vcpu
)
2605 struct vcpu_svm
*svm
= to_svm(vcpu
);
2606 struct vmcb
*vmcb
= svm
->vmcb
;
2607 return !(vmcb
->control
.int_state
& SVM_INTERRUPT_SHADOW_MASK
) &&
2608 !(svm
->vcpu
.arch
.hflags
& HF_NMI_MASK
);
2611 static bool svm_get_nmi_mask(struct kvm_vcpu
*vcpu
)
2613 struct vcpu_svm
*svm
= to_svm(vcpu
);
2615 return !!(svm
->vcpu
.arch
.hflags
& HF_NMI_MASK
);
2618 static void svm_set_nmi_mask(struct kvm_vcpu
*vcpu
, bool masked
)
2620 struct vcpu_svm
*svm
= to_svm(vcpu
);
2623 svm
->vcpu
.arch
.hflags
|= HF_NMI_MASK
;
2624 svm
->vmcb
->control
.intercept
|= (1UL << INTERCEPT_IRET
);
2626 svm
->vcpu
.arch
.hflags
&= ~HF_NMI_MASK
;
2627 svm
->vmcb
->control
.intercept
&= ~(1UL << INTERCEPT_IRET
);
2631 static int svm_interrupt_allowed(struct kvm_vcpu
*vcpu
)
2633 struct vcpu_svm
*svm
= to_svm(vcpu
);
2634 struct vmcb
*vmcb
= svm
->vmcb
;
2637 if (!gif_set(svm
) ||
2638 (vmcb
->control
.int_state
& SVM_INTERRUPT_SHADOW_MASK
))
2641 ret
= !!(vmcb
->save
.rflags
& X86_EFLAGS_IF
);
2644 return ret
&& !(svm
->vcpu
.arch
.hflags
& HF_VINTR_MASK
);
2649 static void enable_irq_window(struct kvm_vcpu
*vcpu
)
2651 struct vcpu_svm
*svm
= to_svm(vcpu
);
2653 /* In case GIF=0 we can't rely on the CPU to tell us when
2654 * GIF becomes 1, because that's a separate STGI/VMRUN intercept.
2655 * The next time we get that intercept, this function will be
2656 * called again though and we'll get the vintr intercept. */
2657 if (gif_set(svm
) && nested_svm_intr(svm
)) {
2659 svm_inject_irq(svm
, 0x0);
2663 static void enable_nmi_window(struct kvm_vcpu
*vcpu
)
2665 struct vcpu_svm
*svm
= to_svm(vcpu
);
2667 if ((svm
->vcpu
.arch
.hflags
& (HF_NMI_MASK
| HF_IRET_MASK
))
2669 return; /* IRET will cause a vm exit */
2671 /* Something prevents NMI from been injected. Single step over
2672 possible problem (IRET or exception injection or interrupt
2674 svm
->nmi_singlestep
= true;
2675 svm
->vmcb
->save
.rflags
|= (X86_EFLAGS_TF
| X86_EFLAGS_RF
);
2676 update_db_intercept(vcpu
);
2679 static int svm_set_tss_addr(struct kvm
*kvm
, unsigned int addr
)
2684 static void svm_flush_tlb(struct kvm_vcpu
*vcpu
)
2686 force_new_asid(vcpu
);
2689 static void svm_prepare_guest_switch(struct kvm_vcpu
*vcpu
)
2693 static inline void sync_cr8_to_lapic(struct kvm_vcpu
*vcpu
)
2695 struct vcpu_svm
*svm
= to_svm(vcpu
);
2697 if (is_nested(svm
) && (vcpu
->arch
.hflags
& HF_VINTR_MASK
))
2700 if (!(svm
->vmcb
->control
.intercept_cr_write
& INTERCEPT_CR8_MASK
)) {
2701 int cr8
= svm
->vmcb
->control
.int_ctl
& V_TPR_MASK
;
2702 kvm_set_cr8(vcpu
, cr8
);
2706 static inline void sync_lapic_to_cr8(struct kvm_vcpu
*vcpu
)
2708 struct vcpu_svm
*svm
= to_svm(vcpu
);
2711 if (is_nested(svm
) && (vcpu
->arch
.hflags
& HF_VINTR_MASK
))
2714 cr8
= kvm_get_cr8(vcpu
);
2715 svm
->vmcb
->control
.int_ctl
&= ~V_TPR_MASK
;
2716 svm
->vmcb
->control
.int_ctl
|= cr8
& V_TPR_MASK
;
2719 static void svm_complete_interrupts(struct vcpu_svm
*svm
)
2723 u32 exitintinfo
= svm
->vmcb
->control
.exit_int_info
;
2724 unsigned int3_injected
= svm
->int3_injected
;
2726 svm
->int3_injected
= 0;
2728 if (svm
->vcpu
.arch
.hflags
& HF_IRET_MASK
)
2729 svm
->vcpu
.arch
.hflags
&= ~(HF_NMI_MASK
| HF_IRET_MASK
);
2731 svm
->vcpu
.arch
.nmi_injected
= false;
2732 kvm_clear_exception_queue(&svm
->vcpu
);
2733 kvm_clear_interrupt_queue(&svm
->vcpu
);
2735 if (!(exitintinfo
& SVM_EXITINTINFO_VALID
))
2738 vector
= exitintinfo
& SVM_EXITINTINFO_VEC_MASK
;
2739 type
= exitintinfo
& SVM_EXITINTINFO_TYPE_MASK
;
2742 case SVM_EXITINTINFO_TYPE_NMI
:
2743 svm
->vcpu
.arch
.nmi_injected
= true;
2745 case SVM_EXITINTINFO_TYPE_EXEPT
:
2749 * In case of software exceptions, do not reinject the vector,
2750 * but re-execute the instruction instead. Rewind RIP first
2751 * if we emulated INT3 before.
2753 if (kvm_exception_is_soft(vector
)) {
2754 if (vector
== BP_VECTOR
&& int3_injected
&&
2755 kvm_is_linear_rip(&svm
->vcpu
, svm
->int3_rip
))
2756 kvm_rip_write(&svm
->vcpu
,
2757 kvm_rip_read(&svm
->vcpu
) -
2761 if (exitintinfo
& SVM_EXITINTINFO_VALID_ERR
) {
2762 u32 err
= svm
->vmcb
->control
.exit_int_info_err
;
2763 kvm_queue_exception_e(&svm
->vcpu
, vector
, err
);
2766 kvm_queue_exception(&svm
->vcpu
, vector
);
2768 case SVM_EXITINTINFO_TYPE_INTR
:
2769 kvm_queue_interrupt(&svm
->vcpu
, vector
, false);
2776 #ifdef CONFIG_X86_64
2782 static void svm_vcpu_run(struct kvm_vcpu
*vcpu
)
2784 struct vcpu_svm
*svm
= to_svm(vcpu
);
2790 * A vmexit emulation is required before the vcpu can be executed
2793 if (unlikely(svm
->nested
.exit_required
))
2796 svm
->vmcb
->save
.rax
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
2797 svm
->vmcb
->save
.rsp
= vcpu
->arch
.regs
[VCPU_REGS_RSP
];
2798 svm
->vmcb
->save
.rip
= vcpu
->arch
.regs
[VCPU_REGS_RIP
];
2802 sync_lapic_to_cr8(vcpu
);
2804 save_host_msrs(vcpu
);
2805 fs_selector
= kvm_read_fs();
2806 gs_selector
= kvm_read_gs();
2807 ldt_selector
= kvm_read_ldt();
2808 svm
->vmcb
->save
.cr2
= vcpu
->arch
.cr2
;
2809 /* required for live migration with NPT */
2811 svm
->vmcb
->save
.cr3
= vcpu
->arch
.cr3
;
2818 "push %%"R
"bp; \n\t"
2819 "mov %c[rbx](%[svm]), %%"R
"bx \n\t"
2820 "mov %c[rcx](%[svm]), %%"R
"cx \n\t"
2821 "mov %c[rdx](%[svm]), %%"R
"dx \n\t"
2822 "mov %c[rsi](%[svm]), %%"R
"si \n\t"
2823 "mov %c[rdi](%[svm]), %%"R
"di \n\t"
2824 "mov %c[rbp](%[svm]), %%"R
"bp \n\t"
2825 #ifdef CONFIG_X86_64
2826 "mov %c[r8](%[svm]), %%r8 \n\t"
2827 "mov %c[r9](%[svm]), %%r9 \n\t"
2828 "mov %c[r10](%[svm]), %%r10 \n\t"
2829 "mov %c[r11](%[svm]), %%r11 \n\t"
2830 "mov %c[r12](%[svm]), %%r12 \n\t"
2831 "mov %c[r13](%[svm]), %%r13 \n\t"
2832 "mov %c[r14](%[svm]), %%r14 \n\t"
2833 "mov %c[r15](%[svm]), %%r15 \n\t"
2836 /* Enter guest mode */
2838 "mov %c[vmcb](%[svm]), %%"R
"ax \n\t"
2839 __ex(SVM_VMLOAD
) "\n\t"
2840 __ex(SVM_VMRUN
) "\n\t"
2841 __ex(SVM_VMSAVE
) "\n\t"
2844 /* Save guest registers, load host registers */
2845 "mov %%"R
"bx, %c[rbx](%[svm]) \n\t"
2846 "mov %%"R
"cx, %c[rcx](%[svm]) \n\t"
2847 "mov %%"R
"dx, %c[rdx](%[svm]) \n\t"
2848 "mov %%"R
"si, %c[rsi](%[svm]) \n\t"
2849 "mov %%"R
"di, %c[rdi](%[svm]) \n\t"
2850 "mov %%"R
"bp, %c[rbp](%[svm]) \n\t"
2851 #ifdef CONFIG_X86_64
2852 "mov %%r8, %c[r8](%[svm]) \n\t"
2853 "mov %%r9, %c[r9](%[svm]) \n\t"
2854 "mov %%r10, %c[r10](%[svm]) \n\t"
2855 "mov %%r11, %c[r11](%[svm]) \n\t"
2856 "mov %%r12, %c[r12](%[svm]) \n\t"
2857 "mov %%r13, %c[r13](%[svm]) \n\t"
2858 "mov %%r14, %c[r14](%[svm]) \n\t"
2859 "mov %%r15, %c[r15](%[svm]) \n\t"
2864 [vmcb
]"i"(offsetof(struct vcpu_svm
, vmcb_pa
)),
2865 [rbx
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RBX
])),
2866 [rcx
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RCX
])),
2867 [rdx
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RDX
])),
2868 [rsi
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RSI
])),
2869 [rdi
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RDI
])),
2870 [rbp
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RBP
]))
2871 #ifdef CONFIG_X86_64
2872 , [r8
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R8
])),
2873 [r9
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R9
])),
2874 [r10
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R10
])),
2875 [r11
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R11
])),
2876 [r12
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R12
])),
2877 [r13
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R13
])),
2878 [r14
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R14
])),
2879 [r15
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R15
]))
2882 , R
"bx", R
"cx", R
"dx", R
"si", R
"di"
2883 #ifdef CONFIG_X86_64
2884 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
2888 vcpu
->arch
.cr2
= svm
->vmcb
->save
.cr2
;
2889 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = svm
->vmcb
->save
.rax
;
2890 vcpu
->arch
.regs
[VCPU_REGS_RSP
] = svm
->vmcb
->save
.rsp
;
2891 vcpu
->arch
.regs
[VCPU_REGS_RIP
] = svm
->vmcb
->save
.rip
;
2893 kvm_load_fs(fs_selector
);
2894 kvm_load_gs(gs_selector
);
2895 kvm_load_ldt(ldt_selector
);
2896 load_host_msrs(vcpu
);
2900 local_irq_disable();
2904 sync_cr8_to_lapic(vcpu
);
2909 vcpu
->arch
.regs_avail
&= ~(1 << VCPU_EXREG_PDPTR
);
2910 vcpu
->arch
.regs_dirty
&= ~(1 << VCPU_EXREG_PDPTR
);
2916 static void svm_set_cr3(struct kvm_vcpu
*vcpu
, unsigned long root
)
2918 struct vcpu_svm
*svm
= to_svm(vcpu
);
2921 svm
->vmcb
->control
.nested_cr3
= root
;
2922 force_new_asid(vcpu
);
2926 svm
->vmcb
->save
.cr3
= root
;
2927 force_new_asid(vcpu
);
2930 static int is_disabled(void)
2934 rdmsrl(MSR_VM_CR
, vm_cr
);
2935 if (vm_cr
& (1 << SVM_VM_CR_SVM_DISABLE
))
2942 svm_patch_hypercall(struct kvm_vcpu
*vcpu
, unsigned char *hypercall
)
2945 * Patch in the VMMCALL instruction:
2947 hypercall
[0] = 0x0f;
2948 hypercall
[1] = 0x01;
2949 hypercall
[2] = 0xd9;
2952 static void svm_check_processor_compat(void *rtn
)
2957 static bool svm_cpu_has_accelerated_tpr(void)
2962 static int get_npt_level(void)
2964 #ifdef CONFIG_X86_64
2965 return PT64_ROOT_LEVEL
;
2967 return PT32E_ROOT_LEVEL
;
2971 static u64
svm_get_mt_mask(struct kvm_vcpu
*vcpu
, gfn_t gfn
, bool is_mmio
)
2976 static void svm_cpuid_update(struct kvm_vcpu
*vcpu
)
2980 static const struct trace_print_flags svm_exit_reasons_str
[] = {
2981 { SVM_EXIT_READ_CR0
, "read_cr0" },
2982 { SVM_EXIT_READ_CR3
, "read_cr3" },
2983 { SVM_EXIT_READ_CR4
, "read_cr4" },
2984 { SVM_EXIT_READ_CR8
, "read_cr8" },
2985 { SVM_EXIT_WRITE_CR0
, "write_cr0" },
2986 { SVM_EXIT_WRITE_CR3
, "write_cr3" },
2987 { SVM_EXIT_WRITE_CR4
, "write_cr4" },
2988 { SVM_EXIT_WRITE_CR8
, "write_cr8" },
2989 { SVM_EXIT_READ_DR0
, "read_dr0" },
2990 { SVM_EXIT_READ_DR1
, "read_dr1" },
2991 { SVM_EXIT_READ_DR2
, "read_dr2" },
2992 { SVM_EXIT_READ_DR3
, "read_dr3" },
2993 { SVM_EXIT_WRITE_DR0
, "write_dr0" },
2994 { SVM_EXIT_WRITE_DR1
, "write_dr1" },
2995 { SVM_EXIT_WRITE_DR2
, "write_dr2" },
2996 { SVM_EXIT_WRITE_DR3
, "write_dr3" },
2997 { SVM_EXIT_WRITE_DR5
, "write_dr5" },
2998 { SVM_EXIT_WRITE_DR7
, "write_dr7" },
2999 { SVM_EXIT_EXCP_BASE
+ DB_VECTOR
, "DB excp" },
3000 { SVM_EXIT_EXCP_BASE
+ BP_VECTOR
, "BP excp" },
3001 { SVM_EXIT_EXCP_BASE
+ UD_VECTOR
, "UD excp" },
3002 { SVM_EXIT_EXCP_BASE
+ PF_VECTOR
, "PF excp" },
3003 { SVM_EXIT_EXCP_BASE
+ NM_VECTOR
, "NM excp" },
3004 { SVM_EXIT_EXCP_BASE
+ MC_VECTOR
, "MC excp" },
3005 { SVM_EXIT_INTR
, "interrupt" },
3006 { SVM_EXIT_NMI
, "nmi" },
3007 { SVM_EXIT_SMI
, "smi" },
3008 { SVM_EXIT_INIT
, "init" },
3009 { SVM_EXIT_VINTR
, "vintr" },
3010 { SVM_EXIT_CPUID
, "cpuid" },
3011 { SVM_EXIT_INVD
, "invd" },
3012 { SVM_EXIT_HLT
, "hlt" },
3013 { SVM_EXIT_INVLPG
, "invlpg" },
3014 { SVM_EXIT_INVLPGA
, "invlpga" },
3015 { SVM_EXIT_IOIO
, "io" },
3016 { SVM_EXIT_MSR
, "msr" },
3017 { SVM_EXIT_TASK_SWITCH
, "task_switch" },
3018 { SVM_EXIT_SHUTDOWN
, "shutdown" },
3019 { SVM_EXIT_VMRUN
, "vmrun" },
3020 { SVM_EXIT_VMMCALL
, "hypercall" },
3021 { SVM_EXIT_VMLOAD
, "vmload" },
3022 { SVM_EXIT_VMSAVE
, "vmsave" },
3023 { SVM_EXIT_STGI
, "stgi" },
3024 { SVM_EXIT_CLGI
, "clgi" },
3025 { SVM_EXIT_SKINIT
, "skinit" },
3026 { SVM_EXIT_WBINVD
, "wbinvd" },
3027 { SVM_EXIT_MONITOR
, "monitor" },
3028 { SVM_EXIT_MWAIT
, "mwait" },
3029 { SVM_EXIT_NPF
, "npf" },
3033 static int svm_get_lpage_level(void)
3035 return PT_PDPE_LEVEL
;
3038 static bool svm_rdtscp_supported(void)
3043 static void svm_fpu_deactivate(struct kvm_vcpu
*vcpu
)
3045 struct vcpu_svm
*svm
= to_svm(vcpu
);
3047 svm
->vmcb
->control
.intercept_exceptions
|= 1 << NM_VECTOR
;
3049 svm
->nested
.hsave
->control
.intercept_exceptions
|= 1 << NM_VECTOR
;
3050 update_cr0_intercept(svm
);
3053 static struct kvm_x86_ops svm_x86_ops
= {
3054 .cpu_has_kvm_support
= has_svm
,
3055 .disabled_by_bios
= is_disabled
,
3056 .hardware_setup
= svm_hardware_setup
,
3057 .hardware_unsetup
= svm_hardware_unsetup
,
3058 .check_processor_compatibility
= svm_check_processor_compat
,
3059 .hardware_enable
= svm_hardware_enable
,
3060 .hardware_disable
= svm_hardware_disable
,
3061 .cpu_has_accelerated_tpr
= svm_cpu_has_accelerated_tpr
,
3063 .vcpu_create
= svm_create_vcpu
,
3064 .vcpu_free
= svm_free_vcpu
,
3065 .vcpu_reset
= svm_vcpu_reset
,
3067 .prepare_guest_switch
= svm_prepare_guest_switch
,
3068 .vcpu_load
= svm_vcpu_load
,
3069 .vcpu_put
= svm_vcpu_put
,
3071 .set_guest_debug
= svm_guest_debug
,
3072 .get_msr
= svm_get_msr
,
3073 .set_msr
= svm_set_msr
,
3074 .get_segment_base
= svm_get_segment_base
,
3075 .get_segment
= svm_get_segment
,
3076 .set_segment
= svm_set_segment
,
3077 .get_cpl
= svm_get_cpl
,
3078 .get_cs_db_l_bits
= kvm_get_cs_db_l_bits
,
3079 .decache_cr0_guest_bits
= svm_decache_cr0_guest_bits
,
3080 .decache_cr4_guest_bits
= svm_decache_cr4_guest_bits
,
3081 .set_cr0
= svm_set_cr0
,
3082 .set_cr3
= svm_set_cr3
,
3083 .set_cr4
= svm_set_cr4
,
3084 .set_efer
= svm_set_efer
,
3085 .get_idt
= svm_get_idt
,
3086 .set_idt
= svm_set_idt
,
3087 .get_gdt
= svm_get_gdt
,
3088 .set_gdt
= svm_set_gdt
,
3089 .get_dr
= svm_get_dr
,
3090 .set_dr
= svm_set_dr
,
3091 .cache_reg
= svm_cache_reg
,
3092 .get_rflags
= svm_get_rflags
,
3093 .set_rflags
= svm_set_rflags
,
3094 .fpu_activate
= svm_fpu_activate
,
3095 .fpu_deactivate
= svm_fpu_deactivate
,
3097 .tlb_flush
= svm_flush_tlb
,
3099 .run
= svm_vcpu_run
,
3100 .handle_exit
= handle_exit
,
3101 .skip_emulated_instruction
= skip_emulated_instruction
,
3102 .set_interrupt_shadow
= svm_set_interrupt_shadow
,
3103 .get_interrupt_shadow
= svm_get_interrupt_shadow
,
3104 .patch_hypercall
= svm_patch_hypercall
,
3105 .set_irq
= svm_set_irq
,
3106 .set_nmi
= svm_inject_nmi
,
3107 .queue_exception
= svm_queue_exception
,
3108 .interrupt_allowed
= svm_interrupt_allowed
,
3109 .nmi_allowed
= svm_nmi_allowed
,
3110 .get_nmi_mask
= svm_get_nmi_mask
,
3111 .set_nmi_mask
= svm_set_nmi_mask
,
3112 .enable_nmi_window
= enable_nmi_window
,
3113 .enable_irq_window
= enable_irq_window
,
3114 .update_cr8_intercept
= update_cr8_intercept
,
3116 .set_tss_addr
= svm_set_tss_addr
,
3117 .get_tdp_level
= get_npt_level
,
3118 .get_mt_mask
= svm_get_mt_mask
,
3120 .exit_reasons_str
= svm_exit_reasons_str
,
3121 .get_lpage_level
= svm_get_lpage_level
,
3123 .cpuid_update
= svm_cpuid_update
,
3125 .rdtscp_supported
= svm_rdtscp_supported
,
3128 static int __init
svm_init(void)
3130 return kvm_init(&svm_x86_ops
, sizeof(struct vcpu_svm
),
3134 static void __exit
svm_exit(void)
3139 module_init(svm_init
)
3140 module_exit(svm_exit
)