2 * Kernel-based Virtual Machine driver for Linux
6 * Copyright (C) 2006 Qumranet, Inc.
7 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
10 * Yaniv Kamay <yaniv@qumranet.com>
11 * Avi Kivity <avi@qumranet.com>
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
17 #include <linux/kvm_host.h>
21 #include "kvm_cache_regs.h"
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/vmalloc.h>
27 #include <linux/highmem.h>
28 #include <linux/sched.h>
29 #include <linux/ftrace_event.h>
30 #include <linux/slab.h>
32 #include <asm/tlbflush.h>
34 #include <asm/kvm_para.h>
36 #include <asm/virtext.h>
39 #define __ex(x) __kvm_handle_fault_on_reboot(x)
41 MODULE_AUTHOR("Qumranet");
42 MODULE_LICENSE("GPL");
44 #define IOPM_ALLOC_ORDER 2
45 #define MSRPM_ALLOC_ORDER 1
47 #define SEG_TYPE_LDT 2
48 #define SEG_TYPE_BUSY_TSS16 3
50 #define SVM_FEATURE_NPT (1 << 0)
51 #define SVM_FEATURE_LBRV (1 << 1)
52 #define SVM_FEATURE_SVML (1 << 2)
53 #define SVM_FEATURE_NRIP (1 << 3)
54 #define SVM_FEATURE_PAUSE_FILTER (1 << 10)
56 #define NESTED_EXIT_HOST 0 /* Exit handled on host level */
57 #define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
58 #define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
60 #define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
62 static bool erratum_383_found __read_mostly
;
64 static const u32 host_save_user_msrs
[] = {
66 MSR_STAR
, MSR_LSTAR
, MSR_CSTAR
, MSR_SYSCALL_MASK
, MSR_KERNEL_GS_BASE
,
69 MSR_IA32_SYSENTER_CS
, MSR_IA32_SYSENTER_ESP
, MSR_IA32_SYSENTER_EIP
,
72 #define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
82 /* These are the merged vectors */
85 /* gpa pointers to the real vectors */
89 /* A VMEXIT is required but not yet emulated */
93 * If we vmexit during an instruction emulation we need this to restore
94 * the l1 guest rip after the emulation
96 unsigned long vmexit_rip
;
97 unsigned long vmexit_rsp
;
98 unsigned long vmexit_rax
;
100 /* cache for intercepts of the guest */
103 u32 intercept_exceptions
;
106 /* Nested Paging related state */
110 #define MSRPM_OFFSETS 16
111 static u32 msrpm_offsets
[MSRPM_OFFSETS
] __read_mostly
;
114 struct kvm_vcpu vcpu
;
116 unsigned long vmcb_pa
;
117 struct svm_cpu_data
*svm_data
;
118 uint64_t asid_generation
;
119 uint64_t sysenter_esp
;
120 uint64_t sysenter_eip
;
124 u64 host_user_msrs
[NR_HOST_SAVE_USER_MSRS
];
134 struct nested_state nested
;
138 unsigned int3_injected
;
139 unsigned long int3_rip
;
143 #define MSR_INVALID 0xffffffffU
145 static struct svm_direct_access_msrs
{
146 u32 index
; /* Index of the MSR */
147 bool always
; /* True if intercept is always on */
148 } direct_access_msrs
[] = {
149 { .index
= MSR_STAR
, .always
= true },
150 { .index
= MSR_IA32_SYSENTER_CS
, .always
= true },
152 { .index
= MSR_GS_BASE
, .always
= true },
153 { .index
= MSR_FS_BASE
, .always
= true },
154 { .index
= MSR_KERNEL_GS_BASE
, .always
= true },
155 { .index
= MSR_LSTAR
, .always
= true },
156 { .index
= MSR_CSTAR
, .always
= true },
157 { .index
= MSR_SYSCALL_MASK
, .always
= true },
159 { .index
= MSR_IA32_LASTBRANCHFROMIP
, .always
= false },
160 { .index
= MSR_IA32_LASTBRANCHTOIP
, .always
= false },
161 { .index
= MSR_IA32_LASTINTFROMIP
, .always
= false },
162 { .index
= MSR_IA32_LASTINTTOIP
, .always
= false },
163 { .index
= MSR_INVALID
, .always
= false },
166 /* enable NPT for AMD64 and X86 with PAE */
167 #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
168 static bool npt_enabled
= true;
170 static bool npt_enabled
;
174 module_param(npt
, int, S_IRUGO
);
176 static int nested
= 1;
177 module_param(nested
, int, S_IRUGO
);
179 static void svm_flush_tlb(struct kvm_vcpu
*vcpu
);
180 static void svm_complete_interrupts(struct vcpu_svm
*svm
);
182 static int nested_svm_exit_handled(struct vcpu_svm
*svm
);
183 static int nested_svm_intercept(struct vcpu_svm
*svm
);
184 static int nested_svm_vmexit(struct vcpu_svm
*svm
);
185 static int nested_svm_check_exception(struct vcpu_svm
*svm
, unsigned nr
,
186 bool has_error_code
, u32 error_code
);
189 VMCB_INTERCEPTS
, /* Intercept vectors, TSC offset,
190 pause filter count */
194 #define VMCB_ALWAYS_DIRTY_MASK 0U
196 static inline void mark_all_dirty(struct vmcb
*vmcb
)
198 vmcb
->control
.clean
= 0;
201 static inline void mark_all_clean(struct vmcb
*vmcb
)
203 vmcb
->control
.clean
= ((1 << VMCB_DIRTY_MAX
) - 1)
204 & ~VMCB_ALWAYS_DIRTY_MASK
;
207 static inline void mark_dirty(struct vmcb
*vmcb
, int bit
)
209 vmcb
->control
.clean
&= ~(1 << bit
);
212 static inline struct vcpu_svm
*to_svm(struct kvm_vcpu
*vcpu
)
214 return container_of(vcpu
, struct vcpu_svm
, vcpu
);
217 static void recalc_intercepts(struct vcpu_svm
*svm
)
219 struct vmcb_control_area
*c
, *h
;
220 struct nested_state
*g
;
222 mark_dirty(svm
->vmcb
, VMCB_INTERCEPTS
);
224 if (!is_guest_mode(&svm
->vcpu
))
227 c
= &svm
->vmcb
->control
;
228 h
= &svm
->nested
.hsave
->control
;
231 c
->intercept_cr
= h
->intercept_cr
| g
->intercept_cr
;
232 c
->intercept_dr
= h
->intercept_dr
| g
->intercept_dr
;
233 c
->intercept_exceptions
= h
->intercept_exceptions
| g
->intercept_exceptions
;
234 c
->intercept
= h
->intercept
| g
->intercept
;
237 static inline struct vmcb
*get_host_vmcb(struct vcpu_svm
*svm
)
239 if (is_guest_mode(&svm
->vcpu
))
240 return svm
->nested
.hsave
;
245 static inline void set_cr_intercept(struct vcpu_svm
*svm
, int bit
)
247 struct vmcb
*vmcb
= get_host_vmcb(svm
);
249 vmcb
->control
.intercept_cr
|= (1U << bit
);
251 recalc_intercepts(svm
);
254 static inline void clr_cr_intercept(struct vcpu_svm
*svm
, int bit
)
256 struct vmcb
*vmcb
= get_host_vmcb(svm
);
258 vmcb
->control
.intercept_cr
&= ~(1U << bit
);
260 recalc_intercepts(svm
);
263 static inline bool is_cr_intercept(struct vcpu_svm
*svm
, int bit
)
265 struct vmcb
*vmcb
= get_host_vmcb(svm
);
267 return vmcb
->control
.intercept_cr
& (1U << bit
);
270 static inline void set_dr_intercept(struct vcpu_svm
*svm
, int bit
)
272 struct vmcb
*vmcb
= get_host_vmcb(svm
);
274 vmcb
->control
.intercept_dr
|= (1U << bit
);
276 recalc_intercepts(svm
);
279 static inline void clr_dr_intercept(struct vcpu_svm
*svm
, int bit
)
281 struct vmcb
*vmcb
= get_host_vmcb(svm
);
283 vmcb
->control
.intercept_dr
&= ~(1U << bit
);
285 recalc_intercepts(svm
);
288 static inline void set_exception_intercept(struct vcpu_svm
*svm
, int bit
)
290 struct vmcb
*vmcb
= get_host_vmcb(svm
);
292 vmcb
->control
.intercept_exceptions
|= (1U << bit
);
294 recalc_intercepts(svm
);
297 static inline void clr_exception_intercept(struct vcpu_svm
*svm
, int bit
)
299 struct vmcb
*vmcb
= get_host_vmcb(svm
);
301 vmcb
->control
.intercept_exceptions
&= ~(1U << bit
);
303 recalc_intercepts(svm
);
306 static inline void set_intercept(struct vcpu_svm
*svm
, int bit
)
308 struct vmcb
*vmcb
= get_host_vmcb(svm
);
310 vmcb
->control
.intercept
|= (1ULL << bit
);
312 recalc_intercepts(svm
);
315 static inline void clr_intercept(struct vcpu_svm
*svm
, int bit
)
317 struct vmcb
*vmcb
= get_host_vmcb(svm
);
319 vmcb
->control
.intercept
&= ~(1ULL << bit
);
321 recalc_intercepts(svm
);
324 static inline void enable_gif(struct vcpu_svm
*svm
)
326 svm
->vcpu
.arch
.hflags
|= HF_GIF_MASK
;
329 static inline void disable_gif(struct vcpu_svm
*svm
)
331 svm
->vcpu
.arch
.hflags
&= ~HF_GIF_MASK
;
334 static inline bool gif_set(struct vcpu_svm
*svm
)
336 return !!(svm
->vcpu
.arch
.hflags
& HF_GIF_MASK
);
339 static unsigned long iopm_base
;
341 struct kvm_ldttss_desc
{
344 unsigned base1
:8, type
:5, dpl
:2, p
:1;
345 unsigned limit1
:4, zero0
:3, g
:1, base2
:8;
348 } __attribute__((packed
));
350 struct svm_cpu_data
{
356 struct kvm_ldttss_desc
*tss_desc
;
358 struct page
*save_area
;
361 static DEFINE_PER_CPU(struct svm_cpu_data
*, svm_data
);
362 static uint32_t svm_features
;
364 struct svm_init_data
{
369 static u32 msrpm_ranges
[] = {0, 0xc0000000, 0xc0010000};
371 #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
372 #define MSRS_RANGE_SIZE 2048
373 #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
375 static u32
svm_msrpm_offset(u32 msr
)
380 for (i
= 0; i
< NUM_MSR_MAPS
; i
++) {
381 if (msr
< msrpm_ranges
[i
] ||
382 msr
>= msrpm_ranges
[i
] + MSRS_IN_RANGE
)
385 offset
= (msr
- msrpm_ranges
[i
]) / 4; /* 4 msrs per u8 */
386 offset
+= (i
* MSRS_RANGE_SIZE
); /* add range offset */
388 /* Now we have the u8 offset - but need the u32 offset */
392 /* MSR not in any range */
396 #define MAX_INST_SIZE 15
398 static inline void clgi(void)
400 asm volatile (__ex(SVM_CLGI
));
403 static inline void stgi(void)
405 asm volatile (__ex(SVM_STGI
));
408 static inline void invlpga(unsigned long addr
, u32 asid
)
410 asm volatile (__ex(SVM_INVLPGA
) : : "a"(addr
), "c"(asid
));
413 static inline void force_new_asid(struct kvm_vcpu
*vcpu
)
415 to_svm(vcpu
)->asid_generation
--;
418 static inline void flush_guest_tlb(struct kvm_vcpu
*vcpu
)
420 force_new_asid(vcpu
);
423 static int get_npt_level(void)
426 return PT64_ROOT_LEVEL
;
428 return PT32E_ROOT_LEVEL
;
432 static void svm_set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
434 vcpu
->arch
.efer
= efer
;
435 if (!npt_enabled
&& !(efer
& EFER_LMA
))
438 to_svm(vcpu
)->vmcb
->save
.efer
= efer
| EFER_SVME
;
441 static int is_external_interrupt(u32 info
)
443 info
&= SVM_EVTINJ_TYPE_MASK
| SVM_EVTINJ_VALID
;
444 return info
== (SVM_EVTINJ_VALID
| SVM_EVTINJ_TYPE_INTR
);
447 static u32
svm_get_interrupt_shadow(struct kvm_vcpu
*vcpu
, int mask
)
449 struct vcpu_svm
*svm
= to_svm(vcpu
);
452 if (svm
->vmcb
->control
.int_state
& SVM_INTERRUPT_SHADOW_MASK
)
453 ret
|= KVM_X86_SHADOW_INT_STI
| KVM_X86_SHADOW_INT_MOV_SS
;
457 static void svm_set_interrupt_shadow(struct kvm_vcpu
*vcpu
, int mask
)
459 struct vcpu_svm
*svm
= to_svm(vcpu
);
462 svm
->vmcb
->control
.int_state
&= ~SVM_INTERRUPT_SHADOW_MASK
;
464 svm
->vmcb
->control
.int_state
|= SVM_INTERRUPT_SHADOW_MASK
;
468 static void skip_emulated_instruction(struct kvm_vcpu
*vcpu
)
470 struct vcpu_svm
*svm
= to_svm(vcpu
);
472 if (svm
->vmcb
->control
.next_rip
!= 0)
473 svm
->next_rip
= svm
->vmcb
->control
.next_rip
;
475 if (!svm
->next_rip
) {
476 if (emulate_instruction(vcpu
, 0, 0, EMULTYPE_SKIP
) !=
478 printk(KERN_DEBUG
"%s: NOP\n", __func__
);
481 if (svm
->next_rip
- kvm_rip_read(vcpu
) > MAX_INST_SIZE
)
482 printk(KERN_ERR
"%s: ip 0x%lx next 0x%llx\n",
483 __func__
, kvm_rip_read(vcpu
), svm
->next_rip
);
485 kvm_rip_write(vcpu
, svm
->next_rip
);
486 svm_set_interrupt_shadow(vcpu
, 0);
489 static void svm_queue_exception(struct kvm_vcpu
*vcpu
, unsigned nr
,
490 bool has_error_code
, u32 error_code
,
493 struct vcpu_svm
*svm
= to_svm(vcpu
);
496 * If we are within a nested VM we'd better #VMEXIT and let the guest
497 * handle the exception
500 nested_svm_check_exception(svm
, nr
, has_error_code
, error_code
))
503 if (nr
== BP_VECTOR
&& !static_cpu_has(X86_FEATURE_NRIPS
)) {
504 unsigned long rip
, old_rip
= kvm_rip_read(&svm
->vcpu
);
507 * For guest debugging where we have to reinject #BP if some
508 * INT3 is guest-owned:
509 * Emulate nRIP by moving RIP forward. Will fail if injection
510 * raises a fault that is not intercepted. Still better than
511 * failing in all cases.
513 skip_emulated_instruction(&svm
->vcpu
);
514 rip
= kvm_rip_read(&svm
->vcpu
);
515 svm
->int3_rip
= rip
+ svm
->vmcb
->save
.cs
.base
;
516 svm
->int3_injected
= rip
- old_rip
;
519 svm
->vmcb
->control
.event_inj
= nr
521 | (has_error_code
? SVM_EVTINJ_VALID_ERR
: 0)
522 | SVM_EVTINJ_TYPE_EXEPT
;
523 svm
->vmcb
->control
.event_inj_err
= error_code
;
526 static void svm_init_erratum_383(void)
532 if (!cpu_has_amd_erratum(amd_erratum_383
))
535 /* Use _safe variants to not break nested virtualization */
536 val
= native_read_msr_safe(MSR_AMD64_DC_CFG
, &err
);
542 low
= lower_32_bits(val
);
543 high
= upper_32_bits(val
);
545 native_write_msr_safe(MSR_AMD64_DC_CFG
, low
, high
);
547 erratum_383_found
= true;
550 static int has_svm(void)
554 if (!cpu_has_svm(&msg
)) {
555 printk(KERN_INFO
"has_svm: %s\n", msg
);
562 static void svm_hardware_disable(void *garbage
)
567 static int svm_hardware_enable(void *garbage
)
570 struct svm_cpu_data
*sd
;
572 struct desc_ptr gdt_descr
;
573 struct desc_struct
*gdt
;
574 int me
= raw_smp_processor_id();
576 rdmsrl(MSR_EFER
, efer
);
577 if (efer
& EFER_SVME
)
581 printk(KERN_ERR
"svm_hardware_enable: err EOPNOTSUPP on %d\n",
585 sd
= per_cpu(svm_data
, me
);
588 printk(KERN_ERR
"svm_hardware_enable: svm_data is NULL on %d\n",
593 sd
->asid_generation
= 1;
594 sd
->max_asid
= cpuid_ebx(SVM_CPUID_FUNC
) - 1;
595 sd
->next_asid
= sd
->max_asid
+ 1;
597 native_store_gdt(&gdt_descr
);
598 gdt
= (struct desc_struct
*)gdt_descr
.address
;
599 sd
->tss_desc
= (struct kvm_ldttss_desc
*)(gdt
+ GDT_ENTRY_TSS
);
601 wrmsrl(MSR_EFER
, efer
| EFER_SVME
);
603 wrmsrl(MSR_VM_HSAVE_PA
, page_to_pfn(sd
->save_area
) << PAGE_SHIFT
);
605 svm_init_erratum_383();
610 static void svm_cpu_uninit(int cpu
)
612 struct svm_cpu_data
*sd
= per_cpu(svm_data
, raw_smp_processor_id());
617 per_cpu(svm_data
, raw_smp_processor_id()) = NULL
;
618 __free_page(sd
->save_area
);
622 static int svm_cpu_init(int cpu
)
624 struct svm_cpu_data
*sd
;
627 sd
= kzalloc(sizeof(struct svm_cpu_data
), GFP_KERNEL
);
631 sd
->save_area
= alloc_page(GFP_KERNEL
);
636 per_cpu(svm_data
, cpu
) = sd
;
646 static bool valid_msr_intercept(u32 index
)
650 for (i
= 0; direct_access_msrs
[i
].index
!= MSR_INVALID
; i
++)
651 if (direct_access_msrs
[i
].index
== index
)
657 static void set_msr_interception(u32
*msrpm
, unsigned msr
,
660 u8 bit_read
, bit_write
;
665 * If this warning triggers extend the direct_access_msrs list at the
666 * beginning of the file
668 WARN_ON(!valid_msr_intercept(msr
));
670 offset
= svm_msrpm_offset(msr
);
671 bit_read
= 2 * (msr
& 0x0f);
672 bit_write
= 2 * (msr
& 0x0f) + 1;
675 BUG_ON(offset
== MSR_INVALID
);
677 read
? clear_bit(bit_read
, &tmp
) : set_bit(bit_read
, &tmp
);
678 write
? clear_bit(bit_write
, &tmp
) : set_bit(bit_write
, &tmp
);
683 static void svm_vcpu_init_msrpm(u32
*msrpm
)
687 memset(msrpm
, 0xff, PAGE_SIZE
* (1 << MSRPM_ALLOC_ORDER
));
689 for (i
= 0; direct_access_msrs
[i
].index
!= MSR_INVALID
; i
++) {
690 if (!direct_access_msrs
[i
].always
)
693 set_msr_interception(msrpm
, direct_access_msrs
[i
].index
, 1, 1);
697 static void add_msr_offset(u32 offset
)
701 for (i
= 0; i
< MSRPM_OFFSETS
; ++i
) {
703 /* Offset already in list? */
704 if (msrpm_offsets
[i
] == offset
)
707 /* Slot used by another offset? */
708 if (msrpm_offsets
[i
] != MSR_INVALID
)
711 /* Add offset to list */
712 msrpm_offsets
[i
] = offset
;
718 * If this BUG triggers the msrpm_offsets table has an overflow. Just
719 * increase MSRPM_OFFSETS in this case.
724 static void init_msrpm_offsets(void)
728 memset(msrpm_offsets
, 0xff, sizeof(msrpm_offsets
));
730 for (i
= 0; direct_access_msrs
[i
].index
!= MSR_INVALID
; i
++) {
733 offset
= svm_msrpm_offset(direct_access_msrs
[i
].index
);
734 BUG_ON(offset
== MSR_INVALID
);
736 add_msr_offset(offset
);
740 static void svm_enable_lbrv(struct vcpu_svm
*svm
)
742 u32
*msrpm
= svm
->msrpm
;
744 svm
->vmcb
->control
.lbr_ctl
= 1;
745 set_msr_interception(msrpm
, MSR_IA32_LASTBRANCHFROMIP
, 1, 1);
746 set_msr_interception(msrpm
, MSR_IA32_LASTBRANCHTOIP
, 1, 1);
747 set_msr_interception(msrpm
, MSR_IA32_LASTINTFROMIP
, 1, 1);
748 set_msr_interception(msrpm
, MSR_IA32_LASTINTTOIP
, 1, 1);
751 static void svm_disable_lbrv(struct vcpu_svm
*svm
)
753 u32
*msrpm
= svm
->msrpm
;
755 svm
->vmcb
->control
.lbr_ctl
= 0;
756 set_msr_interception(msrpm
, MSR_IA32_LASTBRANCHFROMIP
, 0, 0);
757 set_msr_interception(msrpm
, MSR_IA32_LASTBRANCHTOIP
, 0, 0);
758 set_msr_interception(msrpm
, MSR_IA32_LASTINTFROMIP
, 0, 0);
759 set_msr_interception(msrpm
, MSR_IA32_LASTINTTOIP
, 0, 0);
762 static __init
int svm_hardware_setup(void)
765 struct page
*iopm_pages
;
769 iopm_pages
= alloc_pages(GFP_KERNEL
, IOPM_ALLOC_ORDER
);
774 iopm_va
= page_address(iopm_pages
);
775 memset(iopm_va
, 0xff, PAGE_SIZE
* (1 << IOPM_ALLOC_ORDER
));
776 iopm_base
= page_to_pfn(iopm_pages
) << PAGE_SHIFT
;
778 init_msrpm_offsets();
780 if (boot_cpu_has(X86_FEATURE_NX
))
781 kvm_enable_efer_bits(EFER_NX
);
783 if (boot_cpu_has(X86_FEATURE_FXSR_OPT
))
784 kvm_enable_efer_bits(EFER_FFXSR
);
787 printk(KERN_INFO
"kvm: Nested Virtualization enabled\n");
788 kvm_enable_efer_bits(EFER_SVME
| EFER_LMSLE
);
791 for_each_possible_cpu(cpu
) {
792 r
= svm_cpu_init(cpu
);
797 svm_features
= cpuid_edx(SVM_CPUID_FUNC
);
799 if (!boot_cpu_has(X86_FEATURE_NPT
))
802 if (npt_enabled
&& !npt
) {
803 printk(KERN_INFO
"kvm: Nested Paging disabled\n");
808 printk(KERN_INFO
"kvm: Nested Paging enabled\n");
816 __free_pages(iopm_pages
, IOPM_ALLOC_ORDER
);
821 static __exit
void svm_hardware_unsetup(void)
825 for_each_possible_cpu(cpu
)
828 __free_pages(pfn_to_page(iopm_base
>> PAGE_SHIFT
), IOPM_ALLOC_ORDER
);
832 static void init_seg(struct vmcb_seg
*seg
)
835 seg
->attrib
= SVM_SELECTOR_P_MASK
| SVM_SELECTOR_S_MASK
|
836 SVM_SELECTOR_WRITE_MASK
; /* Read/Write Data Segment */
841 static void init_sys_seg(struct vmcb_seg
*seg
, uint32_t type
)
844 seg
->attrib
= SVM_SELECTOR_P_MASK
| type
;
849 static void svm_write_tsc_offset(struct kvm_vcpu
*vcpu
, u64 offset
)
851 struct vcpu_svm
*svm
= to_svm(vcpu
);
852 u64 g_tsc_offset
= 0;
854 if (is_guest_mode(vcpu
)) {
855 g_tsc_offset
= svm
->vmcb
->control
.tsc_offset
-
856 svm
->nested
.hsave
->control
.tsc_offset
;
857 svm
->nested
.hsave
->control
.tsc_offset
= offset
;
860 svm
->vmcb
->control
.tsc_offset
= offset
+ g_tsc_offset
;
862 mark_dirty(svm
->vmcb
, VMCB_INTERCEPTS
);
865 static void svm_adjust_tsc_offset(struct kvm_vcpu
*vcpu
, s64 adjustment
)
867 struct vcpu_svm
*svm
= to_svm(vcpu
);
869 svm
->vmcb
->control
.tsc_offset
+= adjustment
;
870 if (is_guest_mode(vcpu
))
871 svm
->nested
.hsave
->control
.tsc_offset
+= adjustment
;
872 mark_dirty(svm
->vmcb
, VMCB_INTERCEPTS
);
875 static void init_vmcb(struct vcpu_svm
*svm
)
877 struct vmcb_control_area
*control
= &svm
->vmcb
->control
;
878 struct vmcb_save_area
*save
= &svm
->vmcb
->save
;
880 svm
->vcpu
.fpu_active
= 1;
881 svm
->vcpu
.arch
.hflags
= 0;
883 set_cr_intercept(svm
, INTERCEPT_CR0_READ
);
884 set_cr_intercept(svm
, INTERCEPT_CR3_READ
);
885 set_cr_intercept(svm
, INTERCEPT_CR4_READ
);
886 set_cr_intercept(svm
, INTERCEPT_CR0_WRITE
);
887 set_cr_intercept(svm
, INTERCEPT_CR3_WRITE
);
888 set_cr_intercept(svm
, INTERCEPT_CR4_WRITE
);
889 set_cr_intercept(svm
, INTERCEPT_CR8_WRITE
);
891 set_dr_intercept(svm
, INTERCEPT_DR0_READ
);
892 set_dr_intercept(svm
, INTERCEPT_DR1_READ
);
893 set_dr_intercept(svm
, INTERCEPT_DR2_READ
);
894 set_dr_intercept(svm
, INTERCEPT_DR3_READ
);
895 set_dr_intercept(svm
, INTERCEPT_DR4_READ
);
896 set_dr_intercept(svm
, INTERCEPT_DR5_READ
);
897 set_dr_intercept(svm
, INTERCEPT_DR6_READ
);
898 set_dr_intercept(svm
, INTERCEPT_DR7_READ
);
900 set_dr_intercept(svm
, INTERCEPT_DR0_WRITE
);
901 set_dr_intercept(svm
, INTERCEPT_DR1_WRITE
);
902 set_dr_intercept(svm
, INTERCEPT_DR2_WRITE
);
903 set_dr_intercept(svm
, INTERCEPT_DR3_WRITE
);
904 set_dr_intercept(svm
, INTERCEPT_DR4_WRITE
);
905 set_dr_intercept(svm
, INTERCEPT_DR5_WRITE
);
906 set_dr_intercept(svm
, INTERCEPT_DR6_WRITE
);
907 set_dr_intercept(svm
, INTERCEPT_DR7_WRITE
);
909 set_exception_intercept(svm
, PF_VECTOR
);
910 set_exception_intercept(svm
, UD_VECTOR
);
911 set_exception_intercept(svm
, MC_VECTOR
);
913 set_intercept(svm
, INTERCEPT_INTR
);
914 set_intercept(svm
, INTERCEPT_NMI
);
915 set_intercept(svm
, INTERCEPT_SMI
);
916 set_intercept(svm
, INTERCEPT_SELECTIVE_CR0
);
917 set_intercept(svm
, INTERCEPT_CPUID
);
918 set_intercept(svm
, INTERCEPT_INVD
);
919 set_intercept(svm
, INTERCEPT_HLT
);
920 set_intercept(svm
, INTERCEPT_INVLPG
);
921 set_intercept(svm
, INTERCEPT_INVLPGA
);
922 set_intercept(svm
, INTERCEPT_IOIO_PROT
);
923 set_intercept(svm
, INTERCEPT_MSR_PROT
);
924 set_intercept(svm
, INTERCEPT_TASK_SWITCH
);
925 set_intercept(svm
, INTERCEPT_SHUTDOWN
);
926 set_intercept(svm
, INTERCEPT_VMRUN
);
927 set_intercept(svm
, INTERCEPT_VMMCALL
);
928 set_intercept(svm
, INTERCEPT_VMLOAD
);
929 set_intercept(svm
, INTERCEPT_VMSAVE
);
930 set_intercept(svm
, INTERCEPT_STGI
);
931 set_intercept(svm
, INTERCEPT_CLGI
);
932 set_intercept(svm
, INTERCEPT_SKINIT
);
933 set_intercept(svm
, INTERCEPT_WBINVD
);
934 set_intercept(svm
, INTERCEPT_MONITOR
);
935 set_intercept(svm
, INTERCEPT_MWAIT
);
937 control
->iopm_base_pa
= iopm_base
;
938 control
->msrpm_base_pa
= __pa(svm
->msrpm
);
939 control
->int_ctl
= V_INTR_MASKING_MASK
;
947 save
->cs
.selector
= 0xf000;
948 /* Executable/Readable Code Segment */
949 save
->cs
.attrib
= SVM_SELECTOR_READ_MASK
| SVM_SELECTOR_P_MASK
|
950 SVM_SELECTOR_S_MASK
| SVM_SELECTOR_CODE_MASK
;
951 save
->cs
.limit
= 0xffff;
953 * cs.base should really be 0xffff0000, but vmx can't handle that, so
954 * be consistent with it.
956 * Replace when we have real mode working for vmx.
958 save
->cs
.base
= 0xf0000;
960 save
->gdtr
.limit
= 0xffff;
961 save
->idtr
.limit
= 0xffff;
963 init_sys_seg(&save
->ldtr
, SEG_TYPE_LDT
);
964 init_sys_seg(&save
->tr
, SEG_TYPE_BUSY_TSS16
);
966 svm_set_efer(&svm
->vcpu
, 0);
967 save
->dr6
= 0xffff0ff0;
970 save
->rip
= 0x0000fff0;
971 svm
->vcpu
.arch
.regs
[VCPU_REGS_RIP
] = save
->rip
;
974 * This is the guest-visible cr0 value.
975 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
977 svm
->vcpu
.arch
.cr0
= 0;
978 (void)kvm_set_cr0(&svm
->vcpu
, X86_CR0_NW
| X86_CR0_CD
| X86_CR0_ET
);
980 save
->cr4
= X86_CR4_PAE
;
984 /* Setup VMCB for Nested Paging */
985 control
->nested_ctl
= 1;
986 clr_intercept(svm
, INTERCEPT_TASK_SWITCH
);
987 clr_intercept(svm
, INTERCEPT_INVLPG
);
988 clr_exception_intercept(svm
, PF_VECTOR
);
989 clr_cr_intercept(svm
, INTERCEPT_CR3_READ
);
990 clr_cr_intercept(svm
, INTERCEPT_CR3_WRITE
);
991 save
->g_pat
= 0x0007040600070406ULL
;
995 force_new_asid(&svm
->vcpu
);
997 svm
->nested
.vmcb
= 0;
998 svm
->vcpu
.arch
.hflags
= 0;
1000 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER
)) {
1001 control
->pause_filter_count
= 3000;
1002 set_intercept(svm
, INTERCEPT_PAUSE
);
1005 mark_all_dirty(svm
->vmcb
);
1010 static int svm_vcpu_reset(struct kvm_vcpu
*vcpu
)
1012 struct vcpu_svm
*svm
= to_svm(vcpu
);
1016 if (!kvm_vcpu_is_bsp(vcpu
)) {
1017 kvm_rip_write(vcpu
, 0);
1018 svm
->vmcb
->save
.cs
.base
= svm
->vcpu
.arch
.sipi_vector
<< 12;
1019 svm
->vmcb
->save
.cs
.selector
= svm
->vcpu
.arch
.sipi_vector
<< 8;
1021 vcpu
->arch
.regs_avail
= ~0;
1022 vcpu
->arch
.regs_dirty
= ~0;
1027 static struct kvm_vcpu
*svm_create_vcpu(struct kvm
*kvm
, unsigned int id
)
1029 struct vcpu_svm
*svm
;
1031 struct page
*msrpm_pages
;
1032 struct page
*hsave_page
;
1033 struct page
*nested_msrpm_pages
;
1036 svm
= kmem_cache_zalloc(kvm_vcpu_cache
, GFP_KERNEL
);
1042 err
= kvm_vcpu_init(&svm
->vcpu
, kvm
, id
);
1047 page
= alloc_page(GFP_KERNEL
);
1051 msrpm_pages
= alloc_pages(GFP_KERNEL
, MSRPM_ALLOC_ORDER
);
1055 nested_msrpm_pages
= alloc_pages(GFP_KERNEL
, MSRPM_ALLOC_ORDER
);
1056 if (!nested_msrpm_pages
)
1059 hsave_page
= alloc_page(GFP_KERNEL
);
1063 svm
->nested
.hsave
= page_address(hsave_page
);
1065 svm
->msrpm
= page_address(msrpm_pages
);
1066 svm_vcpu_init_msrpm(svm
->msrpm
);
1068 svm
->nested
.msrpm
= page_address(nested_msrpm_pages
);
1069 svm_vcpu_init_msrpm(svm
->nested
.msrpm
);
1071 svm
->vmcb
= page_address(page
);
1072 clear_page(svm
->vmcb
);
1073 svm
->vmcb_pa
= page_to_pfn(page
) << PAGE_SHIFT
;
1074 svm
->asid_generation
= 0;
1076 kvm_write_tsc(&svm
->vcpu
, 0);
1078 err
= fx_init(&svm
->vcpu
);
1082 svm
->vcpu
.arch
.apic_base
= 0xfee00000 | MSR_IA32_APICBASE_ENABLE
;
1083 if (kvm_vcpu_is_bsp(&svm
->vcpu
))
1084 svm
->vcpu
.arch
.apic_base
|= MSR_IA32_APICBASE_BSP
;
1089 __free_page(hsave_page
);
1091 __free_pages(nested_msrpm_pages
, MSRPM_ALLOC_ORDER
);
1093 __free_pages(msrpm_pages
, MSRPM_ALLOC_ORDER
);
1097 kvm_vcpu_uninit(&svm
->vcpu
);
1099 kmem_cache_free(kvm_vcpu_cache
, svm
);
1101 return ERR_PTR(err
);
1104 static void svm_free_vcpu(struct kvm_vcpu
*vcpu
)
1106 struct vcpu_svm
*svm
= to_svm(vcpu
);
1108 __free_page(pfn_to_page(svm
->vmcb_pa
>> PAGE_SHIFT
));
1109 __free_pages(virt_to_page(svm
->msrpm
), MSRPM_ALLOC_ORDER
);
1110 __free_page(virt_to_page(svm
->nested
.hsave
));
1111 __free_pages(virt_to_page(svm
->nested
.msrpm
), MSRPM_ALLOC_ORDER
);
1112 kvm_vcpu_uninit(vcpu
);
1113 kmem_cache_free(kvm_vcpu_cache
, svm
);
1116 static void svm_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
1118 struct vcpu_svm
*svm
= to_svm(vcpu
);
1121 if (unlikely(cpu
!= vcpu
->cpu
)) {
1122 svm
->asid_generation
= 0;
1123 mark_all_dirty(svm
->vmcb
);
1126 #ifdef CONFIG_X86_64
1127 rdmsrl(MSR_GS_BASE
, to_svm(vcpu
)->host
.gs_base
);
1129 savesegment(fs
, svm
->host
.fs
);
1130 savesegment(gs
, svm
->host
.gs
);
1131 svm
->host
.ldt
= kvm_read_ldt();
1133 for (i
= 0; i
< NR_HOST_SAVE_USER_MSRS
; i
++)
1134 rdmsrl(host_save_user_msrs
[i
], svm
->host_user_msrs
[i
]);
1137 static void svm_vcpu_put(struct kvm_vcpu
*vcpu
)
1139 struct vcpu_svm
*svm
= to_svm(vcpu
);
1142 ++vcpu
->stat
.host_state_reload
;
1143 kvm_load_ldt(svm
->host
.ldt
);
1144 #ifdef CONFIG_X86_64
1145 loadsegment(fs
, svm
->host
.fs
);
1146 load_gs_index(svm
->host
.gs
);
1147 wrmsrl(MSR_KERNEL_GS_BASE
, current
->thread
.gs
);
1149 loadsegment(gs
, svm
->host
.gs
);
1151 for (i
= 0; i
< NR_HOST_SAVE_USER_MSRS
; i
++)
1152 wrmsrl(host_save_user_msrs
[i
], svm
->host_user_msrs
[i
]);
1155 static unsigned long svm_get_rflags(struct kvm_vcpu
*vcpu
)
1157 return to_svm(vcpu
)->vmcb
->save
.rflags
;
1160 static void svm_set_rflags(struct kvm_vcpu
*vcpu
, unsigned long rflags
)
1162 to_svm(vcpu
)->vmcb
->save
.rflags
= rflags
;
1165 static void svm_cache_reg(struct kvm_vcpu
*vcpu
, enum kvm_reg reg
)
1168 case VCPU_EXREG_PDPTR
:
1169 BUG_ON(!npt_enabled
);
1170 load_pdptrs(vcpu
, vcpu
->arch
.walk_mmu
, vcpu
->arch
.cr3
);
1177 static void svm_set_vintr(struct vcpu_svm
*svm
)
1179 set_intercept(svm
, INTERCEPT_VINTR
);
1182 static void svm_clear_vintr(struct vcpu_svm
*svm
)
1184 clr_intercept(svm
, INTERCEPT_VINTR
);
1187 static struct vmcb_seg
*svm_seg(struct kvm_vcpu
*vcpu
, int seg
)
1189 struct vmcb_save_area
*save
= &to_svm(vcpu
)->vmcb
->save
;
1192 case VCPU_SREG_CS
: return &save
->cs
;
1193 case VCPU_SREG_DS
: return &save
->ds
;
1194 case VCPU_SREG_ES
: return &save
->es
;
1195 case VCPU_SREG_FS
: return &save
->fs
;
1196 case VCPU_SREG_GS
: return &save
->gs
;
1197 case VCPU_SREG_SS
: return &save
->ss
;
1198 case VCPU_SREG_TR
: return &save
->tr
;
1199 case VCPU_SREG_LDTR
: return &save
->ldtr
;
1205 static u64
svm_get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
1207 struct vmcb_seg
*s
= svm_seg(vcpu
, seg
);
1212 static void svm_get_segment(struct kvm_vcpu
*vcpu
,
1213 struct kvm_segment
*var
, int seg
)
1215 struct vmcb_seg
*s
= svm_seg(vcpu
, seg
);
1217 var
->base
= s
->base
;
1218 var
->limit
= s
->limit
;
1219 var
->selector
= s
->selector
;
1220 var
->type
= s
->attrib
& SVM_SELECTOR_TYPE_MASK
;
1221 var
->s
= (s
->attrib
>> SVM_SELECTOR_S_SHIFT
) & 1;
1222 var
->dpl
= (s
->attrib
>> SVM_SELECTOR_DPL_SHIFT
) & 3;
1223 var
->present
= (s
->attrib
>> SVM_SELECTOR_P_SHIFT
) & 1;
1224 var
->avl
= (s
->attrib
>> SVM_SELECTOR_AVL_SHIFT
) & 1;
1225 var
->l
= (s
->attrib
>> SVM_SELECTOR_L_SHIFT
) & 1;
1226 var
->db
= (s
->attrib
>> SVM_SELECTOR_DB_SHIFT
) & 1;
1227 var
->g
= (s
->attrib
>> SVM_SELECTOR_G_SHIFT
) & 1;
1230 * AMD's VMCB does not have an explicit unusable field, so emulate it
1231 * for cross vendor migration purposes by "not present"
1233 var
->unusable
= !var
->present
|| (var
->type
== 0);
1238 * SVM always stores 0 for the 'G' bit in the CS selector in
1239 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1240 * Intel's VMENTRY has a check on the 'G' bit.
1242 var
->g
= s
->limit
> 0xfffff;
1246 * Work around a bug where the busy flag in the tr selector
1256 * The accessed bit must always be set in the segment
1257 * descriptor cache, although it can be cleared in the
1258 * descriptor, the cached bit always remains at 1. Since
1259 * Intel has a check on this, set it here to support
1260 * cross-vendor migration.
1267 * On AMD CPUs sometimes the DB bit in the segment
1268 * descriptor is left as 1, although the whole segment has
1269 * been made unusable. Clear it here to pass an Intel VMX
1270 * entry check when cross vendor migrating.
1278 static int svm_get_cpl(struct kvm_vcpu
*vcpu
)
1280 struct vmcb_save_area
*save
= &to_svm(vcpu
)->vmcb
->save
;
1285 static void svm_get_idt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
1287 struct vcpu_svm
*svm
= to_svm(vcpu
);
1289 dt
->size
= svm
->vmcb
->save
.idtr
.limit
;
1290 dt
->address
= svm
->vmcb
->save
.idtr
.base
;
1293 static void svm_set_idt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
1295 struct vcpu_svm
*svm
= to_svm(vcpu
);
1297 svm
->vmcb
->save
.idtr
.limit
= dt
->size
;
1298 svm
->vmcb
->save
.idtr
.base
= dt
->address
;
1301 static void svm_get_gdt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
1303 struct vcpu_svm
*svm
= to_svm(vcpu
);
1305 dt
->size
= svm
->vmcb
->save
.gdtr
.limit
;
1306 dt
->address
= svm
->vmcb
->save
.gdtr
.base
;
1309 static void svm_set_gdt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
1311 struct vcpu_svm
*svm
= to_svm(vcpu
);
1313 svm
->vmcb
->save
.gdtr
.limit
= dt
->size
;
1314 svm
->vmcb
->save
.gdtr
.base
= dt
->address
;
1317 static void svm_decache_cr0_guest_bits(struct kvm_vcpu
*vcpu
)
1321 static void svm_decache_cr4_guest_bits(struct kvm_vcpu
*vcpu
)
1325 static void update_cr0_intercept(struct vcpu_svm
*svm
)
1327 ulong gcr0
= svm
->vcpu
.arch
.cr0
;
1328 u64
*hcr0
= &svm
->vmcb
->save
.cr0
;
1330 if (!svm
->vcpu
.fpu_active
)
1331 *hcr0
|= SVM_CR0_SELECTIVE_MASK
;
1333 *hcr0
= (*hcr0
& ~SVM_CR0_SELECTIVE_MASK
)
1334 | (gcr0
& SVM_CR0_SELECTIVE_MASK
);
1337 if (gcr0
== *hcr0
&& svm
->vcpu
.fpu_active
) {
1338 clr_cr_intercept(svm
, INTERCEPT_CR0_READ
);
1339 clr_cr_intercept(svm
, INTERCEPT_CR0_WRITE
);
1341 set_cr_intercept(svm
, INTERCEPT_CR0_READ
);
1342 set_cr_intercept(svm
, INTERCEPT_CR0_WRITE
);
1346 static void svm_set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
1348 struct vcpu_svm
*svm
= to_svm(vcpu
);
1350 if (is_guest_mode(vcpu
)) {
1352 * We are here because we run in nested mode, the host kvm
1353 * intercepts cr0 writes but the l1 hypervisor does not.
1354 * But the L1 hypervisor may intercept selective cr0 writes.
1355 * This needs to be checked here.
1357 unsigned long old
, new;
1359 /* Remove bits that would trigger a real cr0 write intercept */
1360 old
= vcpu
->arch
.cr0
& SVM_CR0_SELECTIVE_MASK
;
1361 new = cr0
& SVM_CR0_SELECTIVE_MASK
;
1364 /* cr0 write with ts and mp unchanged */
1365 svm
->vmcb
->control
.exit_code
= SVM_EXIT_CR0_SEL_WRITE
;
1366 if (nested_svm_exit_handled(svm
) == NESTED_EXIT_DONE
) {
1367 svm
->nested
.vmexit_rip
= kvm_rip_read(vcpu
);
1368 svm
->nested
.vmexit_rsp
= kvm_register_read(vcpu
, VCPU_REGS_RSP
);
1369 svm
->nested
.vmexit_rax
= kvm_register_read(vcpu
, VCPU_REGS_RAX
);
1375 #ifdef CONFIG_X86_64
1376 if (vcpu
->arch
.efer
& EFER_LME
) {
1377 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
)) {
1378 vcpu
->arch
.efer
|= EFER_LMA
;
1379 svm
->vmcb
->save
.efer
|= EFER_LMA
| EFER_LME
;
1382 if (is_paging(vcpu
) && !(cr0
& X86_CR0_PG
)) {
1383 vcpu
->arch
.efer
&= ~EFER_LMA
;
1384 svm
->vmcb
->save
.efer
&= ~(EFER_LMA
| EFER_LME
);
1388 vcpu
->arch
.cr0
= cr0
;
1391 cr0
|= X86_CR0_PG
| X86_CR0_WP
;
1393 if (!vcpu
->fpu_active
)
1396 * re-enable caching here because the QEMU bios
1397 * does not do it - this results in some delay at
1400 cr0
&= ~(X86_CR0_CD
| X86_CR0_NW
);
1401 svm
->vmcb
->save
.cr0
= cr0
;
1402 update_cr0_intercept(svm
);
1405 static void svm_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
1407 unsigned long host_cr4_mce
= read_cr4() & X86_CR4_MCE
;
1408 unsigned long old_cr4
= to_svm(vcpu
)->vmcb
->save
.cr4
;
1410 if (npt_enabled
&& ((old_cr4
^ cr4
) & X86_CR4_PGE
))
1411 force_new_asid(vcpu
);
1413 vcpu
->arch
.cr4
= cr4
;
1416 cr4
|= host_cr4_mce
;
1417 to_svm(vcpu
)->vmcb
->save
.cr4
= cr4
;
1420 static void svm_set_segment(struct kvm_vcpu
*vcpu
,
1421 struct kvm_segment
*var
, int seg
)
1423 struct vcpu_svm
*svm
= to_svm(vcpu
);
1424 struct vmcb_seg
*s
= svm_seg(vcpu
, seg
);
1426 s
->base
= var
->base
;
1427 s
->limit
= var
->limit
;
1428 s
->selector
= var
->selector
;
1432 s
->attrib
= (var
->type
& SVM_SELECTOR_TYPE_MASK
);
1433 s
->attrib
|= (var
->s
& 1) << SVM_SELECTOR_S_SHIFT
;
1434 s
->attrib
|= (var
->dpl
& 3) << SVM_SELECTOR_DPL_SHIFT
;
1435 s
->attrib
|= (var
->present
& 1) << SVM_SELECTOR_P_SHIFT
;
1436 s
->attrib
|= (var
->avl
& 1) << SVM_SELECTOR_AVL_SHIFT
;
1437 s
->attrib
|= (var
->l
& 1) << SVM_SELECTOR_L_SHIFT
;
1438 s
->attrib
|= (var
->db
& 1) << SVM_SELECTOR_DB_SHIFT
;
1439 s
->attrib
|= (var
->g
& 1) << SVM_SELECTOR_G_SHIFT
;
1441 if (seg
== VCPU_SREG_CS
)
1443 = (svm
->vmcb
->save
.cs
.attrib
1444 >> SVM_SELECTOR_DPL_SHIFT
) & 3;
1448 static void update_db_intercept(struct kvm_vcpu
*vcpu
)
1450 struct vcpu_svm
*svm
= to_svm(vcpu
);
1452 clr_exception_intercept(svm
, DB_VECTOR
);
1453 clr_exception_intercept(svm
, BP_VECTOR
);
1455 if (svm
->nmi_singlestep
)
1456 set_exception_intercept(svm
, DB_VECTOR
);
1458 if (vcpu
->guest_debug
& KVM_GUESTDBG_ENABLE
) {
1459 if (vcpu
->guest_debug
&
1460 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
))
1461 set_exception_intercept(svm
, DB_VECTOR
);
1462 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_SW_BP
)
1463 set_exception_intercept(svm
, BP_VECTOR
);
1465 vcpu
->guest_debug
= 0;
1468 static void svm_guest_debug(struct kvm_vcpu
*vcpu
, struct kvm_guest_debug
*dbg
)
1470 struct vcpu_svm
*svm
= to_svm(vcpu
);
1472 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
)
1473 svm
->vmcb
->save
.dr7
= dbg
->arch
.debugreg
[7];
1475 svm
->vmcb
->save
.dr7
= vcpu
->arch
.dr7
;
1477 update_db_intercept(vcpu
);
1480 static void new_asid(struct vcpu_svm
*svm
, struct svm_cpu_data
*sd
)
1482 if (sd
->next_asid
> sd
->max_asid
) {
1483 ++sd
->asid_generation
;
1485 svm
->vmcb
->control
.tlb_ctl
= TLB_CONTROL_FLUSH_ALL_ASID
;
1488 svm
->asid_generation
= sd
->asid_generation
;
1489 svm
->vmcb
->control
.asid
= sd
->next_asid
++;
1492 static void svm_set_dr7(struct kvm_vcpu
*vcpu
, unsigned long value
)
1494 struct vcpu_svm
*svm
= to_svm(vcpu
);
1496 svm
->vmcb
->save
.dr7
= value
;
1499 static int pf_interception(struct vcpu_svm
*svm
)
1501 u64 fault_address
= svm
->vmcb
->control
.exit_info_2
;
1505 switch (svm
->apf_reason
) {
1507 error_code
= svm
->vmcb
->control
.exit_info_1
;
1509 trace_kvm_page_fault(fault_address
, error_code
);
1510 if (!npt_enabled
&& kvm_event_needs_reinjection(&svm
->vcpu
))
1511 kvm_mmu_unprotect_page_virt(&svm
->vcpu
, fault_address
);
1512 r
= kvm_mmu_page_fault(&svm
->vcpu
, fault_address
, error_code
);
1514 case KVM_PV_REASON_PAGE_NOT_PRESENT
:
1515 svm
->apf_reason
= 0;
1516 local_irq_disable();
1517 kvm_async_pf_task_wait(fault_address
);
1520 case KVM_PV_REASON_PAGE_READY
:
1521 svm
->apf_reason
= 0;
1522 local_irq_disable();
1523 kvm_async_pf_task_wake(fault_address
);
1530 static int db_interception(struct vcpu_svm
*svm
)
1532 struct kvm_run
*kvm_run
= svm
->vcpu
.run
;
1534 if (!(svm
->vcpu
.guest_debug
&
1535 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
)) &&
1536 !svm
->nmi_singlestep
) {
1537 kvm_queue_exception(&svm
->vcpu
, DB_VECTOR
);
1541 if (svm
->nmi_singlestep
) {
1542 svm
->nmi_singlestep
= false;
1543 if (!(svm
->vcpu
.guest_debug
& KVM_GUESTDBG_SINGLESTEP
))
1544 svm
->vmcb
->save
.rflags
&=
1545 ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
1546 update_db_intercept(&svm
->vcpu
);
1549 if (svm
->vcpu
.guest_debug
&
1550 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
)) {
1551 kvm_run
->exit_reason
= KVM_EXIT_DEBUG
;
1552 kvm_run
->debug
.arch
.pc
=
1553 svm
->vmcb
->save
.cs
.base
+ svm
->vmcb
->save
.rip
;
1554 kvm_run
->debug
.arch
.exception
= DB_VECTOR
;
1561 static int bp_interception(struct vcpu_svm
*svm
)
1563 struct kvm_run
*kvm_run
= svm
->vcpu
.run
;
1565 kvm_run
->exit_reason
= KVM_EXIT_DEBUG
;
1566 kvm_run
->debug
.arch
.pc
= svm
->vmcb
->save
.cs
.base
+ svm
->vmcb
->save
.rip
;
1567 kvm_run
->debug
.arch
.exception
= BP_VECTOR
;
1571 static int ud_interception(struct vcpu_svm
*svm
)
1575 er
= emulate_instruction(&svm
->vcpu
, 0, 0, EMULTYPE_TRAP_UD
);
1576 if (er
!= EMULATE_DONE
)
1577 kvm_queue_exception(&svm
->vcpu
, UD_VECTOR
);
1581 static void svm_fpu_activate(struct kvm_vcpu
*vcpu
)
1583 struct vcpu_svm
*svm
= to_svm(vcpu
);
1585 clr_exception_intercept(svm
, NM_VECTOR
);
1587 svm
->vcpu
.fpu_active
= 1;
1588 update_cr0_intercept(svm
);
1591 static int nm_interception(struct vcpu_svm
*svm
)
1593 svm_fpu_activate(&svm
->vcpu
);
1597 static bool is_erratum_383(void)
1602 if (!erratum_383_found
)
1605 value
= native_read_msr_safe(MSR_IA32_MC0_STATUS
, &err
);
1609 /* Bit 62 may or may not be set for this mce */
1610 value
&= ~(1ULL << 62);
1612 if (value
!= 0xb600000000010015ULL
)
1615 /* Clear MCi_STATUS registers */
1616 for (i
= 0; i
< 6; ++i
)
1617 native_write_msr_safe(MSR_IA32_MCx_STATUS(i
), 0, 0);
1619 value
= native_read_msr_safe(MSR_IA32_MCG_STATUS
, &err
);
1623 value
&= ~(1ULL << 2);
1624 low
= lower_32_bits(value
);
1625 high
= upper_32_bits(value
);
1627 native_write_msr_safe(MSR_IA32_MCG_STATUS
, low
, high
);
1630 /* Flush tlb to evict multi-match entries */
1636 static void svm_handle_mce(struct vcpu_svm
*svm
)
1638 if (is_erratum_383()) {
1640 * Erratum 383 triggered. Guest state is corrupt so kill the
1643 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1645 kvm_make_request(KVM_REQ_TRIPLE_FAULT
, &svm
->vcpu
);
1651 * On an #MC intercept the MCE handler is not called automatically in
1652 * the host. So do it by hand here.
1656 /* not sure if we ever come back to this point */
1661 static int mc_interception(struct vcpu_svm
*svm
)
1666 static int shutdown_interception(struct vcpu_svm
*svm
)
1668 struct kvm_run
*kvm_run
= svm
->vcpu
.run
;
1671 * VMCB is undefined after a SHUTDOWN intercept
1672 * so reinitialize it.
1674 clear_page(svm
->vmcb
);
1677 kvm_run
->exit_reason
= KVM_EXIT_SHUTDOWN
;
1681 static int io_interception(struct vcpu_svm
*svm
)
1683 struct kvm_vcpu
*vcpu
= &svm
->vcpu
;
1684 u32 io_info
= svm
->vmcb
->control
.exit_info_1
; /* address size bug? */
1685 int size
, in
, string
;
1688 ++svm
->vcpu
.stat
.io_exits
;
1689 string
= (io_info
& SVM_IOIO_STR_MASK
) != 0;
1690 in
= (io_info
& SVM_IOIO_TYPE_MASK
) != 0;
1692 return emulate_instruction(vcpu
, 0, 0, 0) == EMULATE_DONE
;
1694 port
= io_info
>> 16;
1695 size
= (io_info
& SVM_IOIO_SIZE_MASK
) >> SVM_IOIO_SIZE_SHIFT
;
1696 svm
->next_rip
= svm
->vmcb
->control
.exit_info_2
;
1697 skip_emulated_instruction(&svm
->vcpu
);
1699 return kvm_fast_pio_out(vcpu
, size
, port
);
1702 static int nmi_interception(struct vcpu_svm
*svm
)
1707 static int intr_interception(struct vcpu_svm
*svm
)
1709 ++svm
->vcpu
.stat
.irq_exits
;
1713 static int nop_on_interception(struct vcpu_svm
*svm
)
1718 static int halt_interception(struct vcpu_svm
*svm
)
1720 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 1;
1721 skip_emulated_instruction(&svm
->vcpu
);
1722 return kvm_emulate_halt(&svm
->vcpu
);
1725 static int vmmcall_interception(struct vcpu_svm
*svm
)
1727 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
1728 skip_emulated_instruction(&svm
->vcpu
);
1729 kvm_emulate_hypercall(&svm
->vcpu
);
1733 static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu
*vcpu
)
1735 struct vcpu_svm
*svm
= to_svm(vcpu
);
1737 return svm
->nested
.nested_cr3
;
1740 static void nested_svm_set_tdp_cr3(struct kvm_vcpu
*vcpu
,
1743 struct vcpu_svm
*svm
= to_svm(vcpu
);
1745 svm
->vmcb
->control
.nested_cr3
= root
;
1746 force_new_asid(vcpu
);
1749 static void nested_svm_inject_npf_exit(struct kvm_vcpu
*vcpu
,
1750 struct x86_exception
*fault
)
1752 struct vcpu_svm
*svm
= to_svm(vcpu
);
1754 svm
->vmcb
->control
.exit_code
= SVM_EXIT_NPF
;
1755 svm
->vmcb
->control
.exit_code_hi
= 0;
1756 svm
->vmcb
->control
.exit_info_1
= fault
->error_code
;
1757 svm
->vmcb
->control
.exit_info_2
= fault
->address
;
1759 nested_svm_vmexit(svm
);
1762 static int nested_svm_init_mmu_context(struct kvm_vcpu
*vcpu
)
1766 r
= kvm_init_shadow_mmu(vcpu
, &vcpu
->arch
.mmu
);
1768 vcpu
->arch
.mmu
.set_cr3
= nested_svm_set_tdp_cr3
;
1769 vcpu
->arch
.mmu
.get_cr3
= nested_svm_get_tdp_cr3
;
1770 vcpu
->arch
.mmu
.inject_page_fault
= nested_svm_inject_npf_exit
;
1771 vcpu
->arch
.mmu
.shadow_root_level
= get_npt_level();
1772 vcpu
->arch
.walk_mmu
= &vcpu
->arch
.nested_mmu
;
1777 static void nested_svm_uninit_mmu_context(struct kvm_vcpu
*vcpu
)
1779 vcpu
->arch
.walk_mmu
= &vcpu
->arch
.mmu
;
1782 static int nested_svm_check_permissions(struct vcpu_svm
*svm
)
1784 if (!(svm
->vcpu
.arch
.efer
& EFER_SVME
)
1785 || !is_paging(&svm
->vcpu
)) {
1786 kvm_queue_exception(&svm
->vcpu
, UD_VECTOR
);
1790 if (svm
->vmcb
->save
.cpl
) {
1791 kvm_inject_gp(&svm
->vcpu
, 0);
1798 static int nested_svm_check_exception(struct vcpu_svm
*svm
, unsigned nr
,
1799 bool has_error_code
, u32 error_code
)
1803 if (!is_guest_mode(&svm
->vcpu
))
1806 svm
->vmcb
->control
.exit_code
= SVM_EXIT_EXCP_BASE
+ nr
;
1807 svm
->vmcb
->control
.exit_code_hi
= 0;
1808 svm
->vmcb
->control
.exit_info_1
= error_code
;
1809 svm
->vmcb
->control
.exit_info_2
= svm
->vcpu
.arch
.cr2
;
1811 vmexit
= nested_svm_intercept(svm
);
1812 if (vmexit
== NESTED_EXIT_DONE
)
1813 svm
->nested
.exit_required
= true;
1818 /* This function returns true if it is save to enable the irq window */
1819 static inline bool nested_svm_intr(struct vcpu_svm
*svm
)
1821 if (!is_guest_mode(&svm
->vcpu
))
1824 if (!(svm
->vcpu
.arch
.hflags
& HF_VINTR_MASK
))
1827 if (!(svm
->vcpu
.arch
.hflags
& HF_HIF_MASK
))
1831 * if vmexit was already requested (by intercepted exception
1832 * for instance) do not overwrite it with "external interrupt"
1835 if (svm
->nested
.exit_required
)
1838 svm
->vmcb
->control
.exit_code
= SVM_EXIT_INTR
;
1839 svm
->vmcb
->control
.exit_info_1
= 0;
1840 svm
->vmcb
->control
.exit_info_2
= 0;
1842 if (svm
->nested
.intercept
& 1ULL) {
1844 * The #vmexit can't be emulated here directly because this
1845 * code path runs with irqs and preemtion disabled. A
1846 * #vmexit emulation might sleep. Only signal request for
1849 svm
->nested
.exit_required
= true;
1850 trace_kvm_nested_intr_vmexit(svm
->vmcb
->save
.rip
);
1857 /* This function returns true if it is save to enable the nmi window */
1858 static inline bool nested_svm_nmi(struct vcpu_svm
*svm
)
1860 if (!is_guest_mode(&svm
->vcpu
))
1863 if (!(svm
->nested
.intercept
& (1ULL << INTERCEPT_NMI
)))
1866 svm
->vmcb
->control
.exit_code
= SVM_EXIT_NMI
;
1867 svm
->nested
.exit_required
= true;
1872 static void *nested_svm_map(struct vcpu_svm
*svm
, u64 gpa
, struct page
**_page
)
1878 page
= gfn_to_page(svm
->vcpu
.kvm
, gpa
>> PAGE_SHIFT
);
1879 if (is_error_page(page
))
1887 kvm_release_page_clean(page
);
1888 kvm_inject_gp(&svm
->vcpu
, 0);
1893 static void nested_svm_unmap(struct page
*page
)
1896 kvm_release_page_dirty(page
);
1899 static int nested_svm_intercept_ioio(struct vcpu_svm
*svm
)
1905 if (!(svm
->nested
.intercept
& (1ULL << INTERCEPT_IOIO_PROT
)))
1906 return NESTED_EXIT_HOST
;
1908 port
= svm
->vmcb
->control
.exit_info_1
>> 16;
1909 gpa
= svm
->nested
.vmcb_iopm
+ (port
/ 8);
1913 if (kvm_read_guest(svm
->vcpu
.kvm
, gpa
, &val
, 1))
1916 return val
? NESTED_EXIT_DONE
: NESTED_EXIT_HOST
;
1919 static int nested_svm_exit_handled_msr(struct vcpu_svm
*svm
)
1921 u32 offset
, msr
, value
;
1924 if (!(svm
->nested
.intercept
& (1ULL << INTERCEPT_MSR_PROT
)))
1925 return NESTED_EXIT_HOST
;
1927 msr
= svm
->vcpu
.arch
.regs
[VCPU_REGS_RCX
];
1928 offset
= svm_msrpm_offset(msr
);
1929 write
= svm
->vmcb
->control
.exit_info_1
& 1;
1930 mask
= 1 << ((2 * (msr
& 0xf)) + write
);
1932 if (offset
== MSR_INVALID
)
1933 return NESTED_EXIT_DONE
;
1935 /* Offset is in 32 bit units but need in 8 bit units */
1938 if (kvm_read_guest(svm
->vcpu
.kvm
, svm
->nested
.vmcb_msrpm
+ offset
, &value
, 4))
1939 return NESTED_EXIT_DONE
;
1941 return (value
& mask
) ? NESTED_EXIT_DONE
: NESTED_EXIT_HOST
;
1944 static int nested_svm_exit_special(struct vcpu_svm
*svm
)
1946 u32 exit_code
= svm
->vmcb
->control
.exit_code
;
1948 switch (exit_code
) {
1951 case SVM_EXIT_EXCP_BASE
+ MC_VECTOR
:
1952 return NESTED_EXIT_HOST
;
1954 /* For now we are always handling NPFs when using them */
1956 return NESTED_EXIT_HOST
;
1958 case SVM_EXIT_EXCP_BASE
+ PF_VECTOR
:
1959 /* When we're shadowing, trap PFs, but not async PF */
1960 if (!npt_enabled
&& svm
->apf_reason
== 0)
1961 return NESTED_EXIT_HOST
;
1963 case SVM_EXIT_EXCP_BASE
+ NM_VECTOR
:
1964 nm_interception(svm
);
1970 return NESTED_EXIT_CONTINUE
;
1974 * If this function returns true, this #vmexit was already handled
1976 static int nested_svm_intercept(struct vcpu_svm
*svm
)
1978 u32 exit_code
= svm
->vmcb
->control
.exit_code
;
1979 int vmexit
= NESTED_EXIT_HOST
;
1981 switch (exit_code
) {
1983 vmexit
= nested_svm_exit_handled_msr(svm
);
1986 vmexit
= nested_svm_intercept_ioio(svm
);
1988 case SVM_EXIT_READ_CR0
... SVM_EXIT_WRITE_CR8
: {
1989 u32 bit
= 1U << (exit_code
- SVM_EXIT_READ_CR0
);
1990 if (svm
->nested
.intercept_cr
& bit
)
1991 vmexit
= NESTED_EXIT_DONE
;
1994 case SVM_EXIT_READ_DR0
... SVM_EXIT_WRITE_DR7
: {
1995 u32 bit
= 1U << (exit_code
- SVM_EXIT_READ_DR0
);
1996 if (svm
->nested
.intercept_dr
& bit
)
1997 vmexit
= NESTED_EXIT_DONE
;
2000 case SVM_EXIT_EXCP_BASE
... SVM_EXIT_EXCP_BASE
+ 0x1f: {
2001 u32 excp_bits
= 1 << (exit_code
- SVM_EXIT_EXCP_BASE
);
2002 if (svm
->nested
.intercept_exceptions
& excp_bits
)
2003 vmexit
= NESTED_EXIT_DONE
;
2004 /* async page fault always cause vmexit */
2005 else if ((exit_code
== SVM_EXIT_EXCP_BASE
+ PF_VECTOR
) &&
2006 svm
->apf_reason
!= 0)
2007 vmexit
= NESTED_EXIT_DONE
;
2010 case SVM_EXIT_ERR
: {
2011 vmexit
= NESTED_EXIT_DONE
;
2015 u64 exit_bits
= 1ULL << (exit_code
- SVM_EXIT_INTR
);
2016 if (svm
->nested
.intercept
& exit_bits
)
2017 vmexit
= NESTED_EXIT_DONE
;
2024 static int nested_svm_exit_handled(struct vcpu_svm
*svm
)
2028 vmexit
= nested_svm_intercept(svm
);
2030 if (vmexit
== NESTED_EXIT_DONE
)
2031 nested_svm_vmexit(svm
);
2036 static inline void copy_vmcb_control_area(struct vmcb
*dst_vmcb
, struct vmcb
*from_vmcb
)
2038 struct vmcb_control_area
*dst
= &dst_vmcb
->control
;
2039 struct vmcb_control_area
*from
= &from_vmcb
->control
;
2041 dst
->intercept_cr
= from
->intercept_cr
;
2042 dst
->intercept_dr
= from
->intercept_dr
;
2043 dst
->intercept_exceptions
= from
->intercept_exceptions
;
2044 dst
->intercept
= from
->intercept
;
2045 dst
->iopm_base_pa
= from
->iopm_base_pa
;
2046 dst
->msrpm_base_pa
= from
->msrpm_base_pa
;
2047 dst
->tsc_offset
= from
->tsc_offset
;
2048 dst
->asid
= from
->asid
;
2049 dst
->tlb_ctl
= from
->tlb_ctl
;
2050 dst
->int_ctl
= from
->int_ctl
;
2051 dst
->int_vector
= from
->int_vector
;
2052 dst
->int_state
= from
->int_state
;
2053 dst
->exit_code
= from
->exit_code
;
2054 dst
->exit_code_hi
= from
->exit_code_hi
;
2055 dst
->exit_info_1
= from
->exit_info_1
;
2056 dst
->exit_info_2
= from
->exit_info_2
;
2057 dst
->exit_int_info
= from
->exit_int_info
;
2058 dst
->exit_int_info_err
= from
->exit_int_info_err
;
2059 dst
->nested_ctl
= from
->nested_ctl
;
2060 dst
->event_inj
= from
->event_inj
;
2061 dst
->event_inj_err
= from
->event_inj_err
;
2062 dst
->nested_cr3
= from
->nested_cr3
;
2063 dst
->lbr_ctl
= from
->lbr_ctl
;
2066 static int nested_svm_vmexit(struct vcpu_svm
*svm
)
2068 struct vmcb
*nested_vmcb
;
2069 struct vmcb
*hsave
= svm
->nested
.hsave
;
2070 struct vmcb
*vmcb
= svm
->vmcb
;
2073 trace_kvm_nested_vmexit_inject(vmcb
->control
.exit_code
,
2074 vmcb
->control
.exit_info_1
,
2075 vmcb
->control
.exit_info_2
,
2076 vmcb
->control
.exit_int_info
,
2077 vmcb
->control
.exit_int_info_err
);
2079 nested_vmcb
= nested_svm_map(svm
, svm
->nested
.vmcb
, &page
);
2083 /* Exit Guest-Mode */
2084 leave_guest_mode(&svm
->vcpu
);
2085 svm
->nested
.vmcb
= 0;
2087 /* Give the current vmcb to the guest */
2090 nested_vmcb
->save
.es
= vmcb
->save
.es
;
2091 nested_vmcb
->save
.cs
= vmcb
->save
.cs
;
2092 nested_vmcb
->save
.ss
= vmcb
->save
.ss
;
2093 nested_vmcb
->save
.ds
= vmcb
->save
.ds
;
2094 nested_vmcb
->save
.gdtr
= vmcb
->save
.gdtr
;
2095 nested_vmcb
->save
.idtr
= vmcb
->save
.idtr
;
2096 nested_vmcb
->save
.efer
= svm
->vcpu
.arch
.efer
;
2097 nested_vmcb
->save
.cr0
= kvm_read_cr0(&svm
->vcpu
);
2098 nested_vmcb
->save
.cr3
= svm
->vcpu
.arch
.cr3
;
2099 nested_vmcb
->save
.cr2
= vmcb
->save
.cr2
;
2100 nested_vmcb
->save
.cr4
= svm
->vcpu
.arch
.cr4
;
2101 nested_vmcb
->save
.rflags
= vmcb
->save
.rflags
;
2102 nested_vmcb
->save
.rip
= vmcb
->save
.rip
;
2103 nested_vmcb
->save
.rsp
= vmcb
->save
.rsp
;
2104 nested_vmcb
->save
.rax
= vmcb
->save
.rax
;
2105 nested_vmcb
->save
.dr7
= vmcb
->save
.dr7
;
2106 nested_vmcb
->save
.dr6
= vmcb
->save
.dr6
;
2107 nested_vmcb
->save
.cpl
= vmcb
->save
.cpl
;
2109 nested_vmcb
->control
.int_ctl
= vmcb
->control
.int_ctl
;
2110 nested_vmcb
->control
.int_vector
= vmcb
->control
.int_vector
;
2111 nested_vmcb
->control
.int_state
= vmcb
->control
.int_state
;
2112 nested_vmcb
->control
.exit_code
= vmcb
->control
.exit_code
;
2113 nested_vmcb
->control
.exit_code_hi
= vmcb
->control
.exit_code_hi
;
2114 nested_vmcb
->control
.exit_info_1
= vmcb
->control
.exit_info_1
;
2115 nested_vmcb
->control
.exit_info_2
= vmcb
->control
.exit_info_2
;
2116 nested_vmcb
->control
.exit_int_info
= vmcb
->control
.exit_int_info
;
2117 nested_vmcb
->control
.exit_int_info_err
= vmcb
->control
.exit_int_info_err
;
2118 nested_vmcb
->control
.next_rip
= vmcb
->control
.next_rip
;
2121 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2122 * to make sure that we do not lose injected events. So check event_inj
2123 * here and copy it to exit_int_info if it is valid.
2124 * Exit_int_info and event_inj can't be both valid because the case
2125 * below only happens on a VMRUN instruction intercept which has
2126 * no valid exit_int_info set.
2128 if (vmcb
->control
.event_inj
& SVM_EVTINJ_VALID
) {
2129 struct vmcb_control_area
*nc
= &nested_vmcb
->control
;
2131 nc
->exit_int_info
= vmcb
->control
.event_inj
;
2132 nc
->exit_int_info_err
= vmcb
->control
.event_inj_err
;
2135 nested_vmcb
->control
.tlb_ctl
= 0;
2136 nested_vmcb
->control
.event_inj
= 0;
2137 nested_vmcb
->control
.event_inj_err
= 0;
2139 /* We always set V_INTR_MASKING and remember the old value in hflags */
2140 if (!(svm
->vcpu
.arch
.hflags
& HF_VINTR_MASK
))
2141 nested_vmcb
->control
.int_ctl
&= ~V_INTR_MASKING_MASK
;
2143 /* Restore the original control entries */
2144 copy_vmcb_control_area(vmcb
, hsave
);
2146 kvm_clear_exception_queue(&svm
->vcpu
);
2147 kvm_clear_interrupt_queue(&svm
->vcpu
);
2149 svm
->nested
.nested_cr3
= 0;
2151 /* Restore selected save entries */
2152 svm
->vmcb
->save
.es
= hsave
->save
.es
;
2153 svm
->vmcb
->save
.cs
= hsave
->save
.cs
;
2154 svm
->vmcb
->save
.ss
= hsave
->save
.ss
;
2155 svm
->vmcb
->save
.ds
= hsave
->save
.ds
;
2156 svm
->vmcb
->save
.gdtr
= hsave
->save
.gdtr
;
2157 svm
->vmcb
->save
.idtr
= hsave
->save
.idtr
;
2158 svm
->vmcb
->save
.rflags
= hsave
->save
.rflags
;
2159 svm_set_efer(&svm
->vcpu
, hsave
->save
.efer
);
2160 svm_set_cr0(&svm
->vcpu
, hsave
->save
.cr0
| X86_CR0_PE
);
2161 svm_set_cr4(&svm
->vcpu
, hsave
->save
.cr4
);
2163 svm
->vmcb
->save
.cr3
= hsave
->save
.cr3
;
2164 svm
->vcpu
.arch
.cr3
= hsave
->save
.cr3
;
2166 (void)kvm_set_cr3(&svm
->vcpu
, hsave
->save
.cr3
);
2168 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RAX
, hsave
->save
.rax
);
2169 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RSP
, hsave
->save
.rsp
);
2170 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RIP
, hsave
->save
.rip
);
2171 svm
->vmcb
->save
.dr7
= 0;
2172 svm
->vmcb
->save
.cpl
= 0;
2173 svm
->vmcb
->control
.exit_int_info
= 0;
2175 mark_all_dirty(svm
->vmcb
);
2177 nested_svm_unmap(page
);
2179 nested_svm_uninit_mmu_context(&svm
->vcpu
);
2180 kvm_mmu_reset_context(&svm
->vcpu
);
2181 kvm_mmu_load(&svm
->vcpu
);
2186 static bool nested_svm_vmrun_msrpm(struct vcpu_svm
*svm
)
2189 * This function merges the msr permission bitmaps of kvm and the
2190 * nested vmcb. It is omptimized in that it only merges the parts where
2191 * the kvm msr permission bitmap may contain zero bits
2195 if (!(svm
->nested
.intercept
& (1ULL << INTERCEPT_MSR_PROT
)))
2198 for (i
= 0; i
< MSRPM_OFFSETS
; i
++) {
2202 if (msrpm_offsets
[i
] == 0xffffffff)
2205 p
= msrpm_offsets
[i
];
2206 offset
= svm
->nested
.vmcb_msrpm
+ (p
* 4);
2208 if (kvm_read_guest(svm
->vcpu
.kvm
, offset
, &value
, 4))
2211 svm
->nested
.msrpm
[p
] = svm
->msrpm
[p
] | value
;
2214 svm
->vmcb
->control
.msrpm_base_pa
= __pa(svm
->nested
.msrpm
);
2219 static bool nested_vmcb_checks(struct vmcb
*vmcb
)
2221 if ((vmcb
->control
.intercept
& (1ULL << INTERCEPT_VMRUN
)) == 0)
2224 if (vmcb
->control
.asid
== 0)
2227 if (vmcb
->control
.nested_ctl
&& !npt_enabled
)
2233 static bool nested_svm_vmrun(struct vcpu_svm
*svm
)
2235 struct vmcb
*nested_vmcb
;
2236 struct vmcb
*hsave
= svm
->nested
.hsave
;
2237 struct vmcb
*vmcb
= svm
->vmcb
;
2241 vmcb_gpa
= svm
->vmcb
->save
.rax
;
2243 nested_vmcb
= nested_svm_map(svm
, svm
->vmcb
->save
.rax
, &page
);
2247 if (!nested_vmcb_checks(nested_vmcb
)) {
2248 nested_vmcb
->control
.exit_code
= SVM_EXIT_ERR
;
2249 nested_vmcb
->control
.exit_code_hi
= 0;
2250 nested_vmcb
->control
.exit_info_1
= 0;
2251 nested_vmcb
->control
.exit_info_2
= 0;
2253 nested_svm_unmap(page
);
2258 trace_kvm_nested_vmrun(svm
->vmcb
->save
.rip
, vmcb_gpa
,
2259 nested_vmcb
->save
.rip
,
2260 nested_vmcb
->control
.int_ctl
,
2261 nested_vmcb
->control
.event_inj
,
2262 nested_vmcb
->control
.nested_ctl
);
2264 trace_kvm_nested_intercepts(nested_vmcb
->control
.intercept_cr
& 0xffff,
2265 nested_vmcb
->control
.intercept_cr
>> 16,
2266 nested_vmcb
->control
.intercept_exceptions
,
2267 nested_vmcb
->control
.intercept
);
2269 /* Clear internal status */
2270 kvm_clear_exception_queue(&svm
->vcpu
);
2271 kvm_clear_interrupt_queue(&svm
->vcpu
);
2274 * Save the old vmcb, so we don't need to pick what we save, but can
2275 * restore everything when a VMEXIT occurs
2277 hsave
->save
.es
= vmcb
->save
.es
;
2278 hsave
->save
.cs
= vmcb
->save
.cs
;
2279 hsave
->save
.ss
= vmcb
->save
.ss
;
2280 hsave
->save
.ds
= vmcb
->save
.ds
;
2281 hsave
->save
.gdtr
= vmcb
->save
.gdtr
;
2282 hsave
->save
.idtr
= vmcb
->save
.idtr
;
2283 hsave
->save
.efer
= svm
->vcpu
.arch
.efer
;
2284 hsave
->save
.cr0
= kvm_read_cr0(&svm
->vcpu
);
2285 hsave
->save
.cr4
= svm
->vcpu
.arch
.cr4
;
2286 hsave
->save
.rflags
= vmcb
->save
.rflags
;
2287 hsave
->save
.rip
= kvm_rip_read(&svm
->vcpu
);
2288 hsave
->save
.rsp
= vmcb
->save
.rsp
;
2289 hsave
->save
.rax
= vmcb
->save
.rax
;
2291 hsave
->save
.cr3
= vmcb
->save
.cr3
;
2293 hsave
->save
.cr3
= svm
->vcpu
.arch
.cr3
;
2295 copy_vmcb_control_area(hsave
, vmcb
);
2297 if (svm
->vmcb
->save
.rflags
& X86_EFLAGS_IF
)
2298 svm
->vcpu
.arch
.hflags
|= HF_HIF_MASK
;
2300 svm
->vcpu
.arch
.hflags
&= ~HF_HIF_MASK
;
2302 if (nested_vmcb
->control
.nested_ctl
) {
2303 kvm_mmu_unload(&svm
->vcpu
);
2304 svm
->nested
.nested_cr3
= nested_vmcb
->control
.nested_cr3
;
2305 nested_svm_init_mmu_context(&svm
->vcpu
);
2308 /* Load the nested guest state */
2309 svm
->vmcb
->save
.es
= nested_vmcb
->save
.es
;
2310 svm
->vmcb
->save
.cs
= nested_vmcb
->save
.cs
;
2311 svm
->vmcb
->save
.ss
= nested_vmcb
->save
.ss
;
2312 svm
->vmcb
->save
.ds
= nested_vmcb
->save
.ds
;
2313 svm
->vmcb
->save
.gdtr
= nested_vmcb
->save
.gdtr
;
2314 svm
->vmcb
->save
.idtr
= nested_vmcb
->save
.idtr
;
2315 svm
->vmcb
->save
.rflags
= nested_vmcb
->save
.rflags
;
2316 svm_set_efer(&svm
->vcpu
, nested_vmcb
->save
.efer
);
2317 svm_set_cr0(&svm
->vcpu
, nested_vmcb
->save
.cr0
);
2318 svm_set_cr4(&svm
->vcpu
, nested_vmcb
->save
.cr4
);
2320 svm
->vmcb
->save
.cr3
= nested_vmcb
->save
.cr3
;
2321 svm
->vcpu
.arch
.cr3
= nested_vmcb
->save
.cr3
;
2323 (void)kvm_set_cr3(&svm
->vcpu
, nested_vmcb
->save
.cr3
);
2325 /* Guest paging mode is active - reset mmu */
2326 kvm_mmu_reset_context(&svm
->vcpu
);
2328 svm
->vmcb
->save
.cr2
= svm
->vcpu
.arch
.cr2
= nested_vmcb
->save
.cr2
;
2329 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RAX
, nested_vmcb
->save
.rax
);
2330 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RSP
, nested_vmcb
->save
.rsp
);
2331 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RIP
, nested_vmcb
->save
.rip
);
2333 /* In case we don't even reach vcpu_run, the fields are not updated */
2334 svm
->vmcb
->save
.rax
= nested_vmcb
->save
.rax
;
2335 svm
->vmcb
->save
.rsp
= nested_vmcb
->save
.rsp
;
2336 svm
->vmcb
->save
.rip
= nested_vmcb
->save
.rip
;
2337 svm
->vmcb
->save
.dr7
= nested_vmcb
->save
.dr7
;
2338 svm
->vmcb
->save
.dr6
= nested_vmcb
->save
.dr6
;
2339 svm
->vmcb
->save
.cpl
= nested_vmcb
->save
.cpl
;
2341 svm
->nested
.vmcb_msrpm
= nested_vmcb
->control
.msrpm_base_pa
& ~0x0fffULL
;
2342 svm
->nested
.vmcb_iopm
= nested_vmcb
->control
.iopm_base_pa
& ~0x0fffULL
;
2344 /* cache intercepts */
2345 svm
->nested
.intercept_cr
= nested_vmcb
->control
.intercept_cr
;
2346 svm
->nested
.intercept_dr
= nested_vmcb
->control
.intercept_dr
;
2347 svm
->nested
.intercept_exceptions
= nested_vmcb
->control
.intercept_exceptions
;
2348 svm
->nested
.intercept
= nested_vmcb
->control
.intercept
;
2350 force_new_asid(&svm
->vcpu
);
2351 svm
->vmcb
->control
.int_ctl
= nested_vmcb
->control
.int_ctl
| V_INTR_MASKING_MASK
;
2352 if (nested_vmcb
->control
.int_ctl
& V_INTR_MASKING_MASK
)
2353 svm
->vcpu
.arch
.hflags
|= HF_VINTR_MASK
;
2355 svm
->vcpu
.arch
.hflags
&= ~HF_VINTR_MASK
;
2357 if (svm
->vcpu
.arch
.hflags
& HF_VINTR_MASK
) {
2358 /* We only want the cr8 intercept bits of the guest */
2359 clr_cr_intercept(svm
, INTERCEPT_CR8_READ
);
2360 clr_cr_intercept(svm
, INTERCEPT_CR8_WRITE
);
2363 /* We don't want to see VMMCALLs from a nested guest */
2364 clr_intercept(svm
, INTERCEPT_VMMCALL
);
2366 svm
->vmcb
->control
.lbr_ctl
= nested_vmcb
->control
.lbr_ctl
;
2367 svm
->vmcb
->control
.int_vector
= nested_vmcb
->control
.int_vector
;
2368 svm
->vmcb
->control
.int_state
= nested_vmcb
->control
.int_state
;
2369 svm
->vmcb
->control
.tsc_offset
+= nested_vmcb
->control
.tsc_offset
;
2370 svm
->vmcb
->control
.event_inj
= nested_vmcb
->control
.event_inj
;
2371 svm
->vmcb
->control
.event_inj_err
= nested_vmcb
->control
.event_inj_err
;
2373 nested_svm_unmap(page
);
2375 /* Enter Guest-Mode */
2376 enter_guest_mode(&svm
->vcpu
);
2379 * Merge guest and host intercepts - must be called with vcpu in
2380 * guest-mode to take affect here
2382 recalc_intercepts(svm
);
2384 svm
->nested
.vmcb
= vmcb_gpa
;
2388 mark_all_dirty(svm
->vmcb
);
2393 static void nested_svm_vmloadsave(struct vmcb
*from_vmcb
, struct vmcb
*to_vmcb
)
2395 to_vmcb
->save
.fs
= from_vmcb
->save
.fs
;
2396 to_vmcb
->save
.gs
= from_vmcb
->save
.gs
;
2397 to_vmcb
->save
.tr
= from_vmcb
->save
.tr
;
2398 to_vmcb
->save
.ldtr
= from_vmcb
->save
.ldtr
;
2399 to_vmcb
->save
.kernel_gs_base
= from_vmcb
->save
.kernel_gs_base
;
2400 to_vmcb
->save
.star
= from_vmcb
->save
.star
;
2401 to_vmcb
->save
.lstar
= from_vmcb
->save
.lstar
;
2402 to_vmcb
->save
.cstar
= from_vmcb
->save
.cstar
;
2403 to_vmcb
->save
.sfmask
= from_vmcb
->save
.sfmask
;
2404 to_vmcb
->save
.sysenter_cs
= from_vmcb
->save
.sysenter_cs
;
2405 to_vmcb
->save
.sysenter_esp
= from_vmcb
->save
.sysenter_esp
;
2406 to_vmcb
->save
.sysenter_eip
= from_vmcb
->save
.sysenter_eip
;
2409 static int vmload_interception(struct vcpu_svm
*svm
)
2411 struct vmcb
*nested_vmcb
;
2414 if (nested_svm_check_permissions(svm
))
2417 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
2418 skip_emulated_instruction(&svm
->vcpu
);
2420 nested_vmcb
= nested_svm_map(svm
, svm
->vmcb
->save
.rax
, &page
);
2424 nested_svm_vmloadsave(nested_vmcb
, svm
->vmcb
);
2425 nested_svm_unmap(page
);
2430 static int vmsave_interception(struct vcpu_svm
*svm
)
2432 struct vmcb
*nested_vmcb
;
2435 if (nested_svm_check_permissions(svm
))
2438 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
2439 skip_emulated_instruction(&svm
->vcpu
);
2441 nested_vmcb
= nested_svm_map(svm
, svm
->vmcb
->save
.rax
, &page
);
2445 nested_svm_vmloadsave(svm
->vmcb
, nested_vmcb
);
2446 nested_svm_unmap(page
);
2451 static int vmrun_interception(struct vcpu_svm
*svm
)
2453 if (nested_svm_check_permissions(svm
))
2456 /* Save rip after vmrun instruction */
2457 kvm_rip_write(&svm
->vcpu
, kvm_rip_read(&svm
->vcpu
) + 3);
2459 if (!nested_svm_vmrun(svm
))
2462 if (!nested_svm_vmrun_msrpm(svm
))
2469 svm
->vmcb
->control
.exit_code
= SVM_EXIT_ERR
;
2470 svm
->vmcb
->control
.exit_code_hi
= 0;
2471 svm
->vmcb
->control
.exit_info_1
= 0;
2472 svm
->vmcb
->control
.exit_info_2
= 0;
2474 nested_svm_vmexit(svm
);
2479 static int stgi_interception(struct vcpu_svm
*svm
)
2481 if (nested_svm_check_permissions(svm
))
2484 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
2485 skip_emulated_instruction(&svm
->vcpu
);
2486 kvm_make_request(KVM_REQ_EVENT
, &svm
->vcpu
);
2493 static int clgi_interception(struct vcpu_svm
*svm
)
2495 if (nested_svm_check_permissions(svm
))
2498 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
2499 skip_emulated_instruction(&svm
->vcpu
);
2503 /* After a CLGI no interrupts should come */
2504 svm_clear_vintr(svm
);
2505 svm
->vmcb
->control
.int_ctl
&= ~V_IRQ_MASK
;
2510 static int invlpga_interception(struct vcpu_svm
*svm
)
2512 struct kvm_vcpu
*vcpu
= &svm
->vcpu
;
2514 trace_kvm_invlpga(svm
->vmcb
->save
.rip
, vcpu
->arch
.regs
[VCPU_REGS_RCX
],
2515 vcpu
->arch
.regs
[VCPU_REGS_RAX
]);
2517 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2518 kvm_mmu_invlpg(vcpu
, vcpu
->arch
.regs
[VCPU_REGS_RAX
]);
2520 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
2521 skip_emulated_instruction(&svm
->vcpu
);
2525 static int skinit_interception(struct vcpu_svm
*svm
)
2527 trace_kvm_skinit(svm
->vmcb
->save
.rip
, svm
->vcpu
.arch
.regs
[VCPU_REGS_RAX
]);
2529 kvm_queue_exception(&svm
->vcpu
, UD_VECTOR
);
2533 static int invalid_op_interception(struct vcpu_svm
*svm
)
2535 kvm_queue_exception(&svm
->vcpu
, UD_VECTOR
);
2539 static int task_switch_interception(struct vcpu_svm
*svm
)
2543 int int_type
= svm
->vmcb
->control
.exit_int_info
&
2544 SVM_EXITINTINFO_TYPE_MASK
;
2545 int int_vec
= svm
->vmcb
->control
.exit_int_info
& SVM_EVTINJ_VEC_MASK
;
2547 svm
->vmcb
->control
.exit_int_info
& SVM_EXITINTINFO_TYPE_MASK
;
2549 svm
->vmcb
->control
.exit_int_info
& SVM_EXITINTINFO_VALID
;
2550 bool has_error_code
= false;
2553 tss_selector
= (u16
)svm
->vmcb
->control
.exit_info_1
;
2555 if (svm
->vmcb
->control
.exit_info_2
&
2556 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET
))
2557 reason
= TASK_SWITCH_IRET
;
2558 else if (svm
->vmcb
->control
.exit_info_2
&
2559 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP
))
2560 reason
= TASK_SWITCH_JMP
;
2562 reason
= TASK_SWITCH_GATE
;
2564 reason
= TASK_SWITCH_CALL
;
2566 if (reason
== TASK_SWITCH_GATE
) {
2568 case SVM_EXITINTINFO_TYPE_NMI
:
2569 svm
->vcpu
.arch
.nmi_injected
= false;
2571 case SVM_EXITINTINFO_TYPE_EXEPT
:
2572 if (svm
->vmcb
->control
.exit_info_2
&
2573 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE
)) {
2574 has_error_code
= true;
2576 (u32
)svm
->vmcb
->control
.exit_info_2
;
2578 kvm_clear_exception_queue(&svm
->vcpu
);
2580 case SVM_EXITINTINFO_TYPE_INTR
:
2581 kvm_clear_interrupt_queue(&svm
->vcpu
);
2588 if (reason
!= TASK_SWITCH_GATE
||
2589 int_type
== SVM_EXITINTINFO_TYPE_SOFT
||
2590 (int_type
== SVM_EXITINTINFO_TYPE_EXEPT
&&
2591 (int_vec
== OF_VECTOR
|| int_vec
== BP_VECTOR
)))
2592 skip_emulated_instruction(&svm
->vcpu
);
2594 if (kvm_task_switch(&svm
->vcpu
, tss_selector
, reason
,
2595 has_error_code
, error_code
) == EMULATE_FAIL
) {
2596 svm
->vcpu
.run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
2597 svm
->vcpu
.run
->internal
.suberror
= KVM_INTERNAL_ERROR_EMULATION
;
2598 svm
->vcpu
.run
->internal
.ndata
= 0;
2604 static int cpuid_interception(struct vcpu_svm
*svm
)
2606 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 2;
2607 kvm_emulate_cpuid(&svm
->vcpu
);
2611 static int iret_interception(struct vcpu_svm
*svm
)
2613 ++svm
->vcpu
.stat
.nmi_window_exits
;
2614 clr_intercept(svm
, INTERCEPT_IRET
);
2615 svm
->vcpu
.arch
.hflags
|= HF_IRET_MASK
;
2619 static int invlpg_interception(struct vcpu_svm
*svm
)
2621 return emulate_instruction(&svm
->vcpu
, 0, 0, 0) == EMULATE_DONE
;
2624 static int emulate_on_interception(struct vcpu_svm
*svm
)
2626 return emulate_instruction(&svm
->vcpu
, 0, 0, 0) == EMULATE_DONE
;
2629 static int cr0_write_interception(struct vcpu_svm
*svm
)
2631 struct kvm_vcpu
*vcpu
= &svm
->vcpu
;
2634 r
= emulate_instruction(&svm
->vcpu
, 0, 0, 0);
2636 if (svm
->nested
.vmexit_rip
) {
2637 kvm_register_write(vcpu
, VCPU_REGS_RIP
, svm
->nested
.vmexit_rip
);
2638 kvm_register_write(vcpu
, VCPU_REGS_RSP
, svm
->nested
.vmexit_rsp
);
2639 kvm_register_write(vcpu
, VCPU_REGS_RAX
, svm
->nested
.vmexit_rax
);
2640 svm
->nested
.vmexit_rip
= 0;
2643 return r
== EMULATE_DONE
;
2646 static int cr8_write_interception(struct vcpu_svm
*svm
)
2648 struct kvm_run
*kvm_run
= svm
->vcpu
.run
;
2650 u8 cr8_prev
= kvm_get_cr8(&svm
->vcpu
);
2651 /* instruction emulation calls kvm_set_cr8() */
2652 emulate_instruction(&svm
->vcpu
, 0, 0, 0);
2653 if (irqchip_in_kernel(svm
->vcpu
.kvm
)) {
2654 clr_cr_intercept(svm
, INTERCEPT_CR8_WRITE
);
2657 if (cr8_prev
<= kvm_get_cr8(&svm
->vcpu
))
2659 kvm_run
->exit_reason
= KVM_EXIT_SET_TPR
;
2663 static int svm_get_msr(struct kvm_vcpu
*vcpu
, unsigned ecx
, u64
*data
)
2665 struct vcpu_svm
*svm
= to_svm(vcpu
);
2668 case MSR_IA32_TSC
: {
2669 struct vmcb
*vmcb
= get_host_vmcb(svm
);
2671 *data
= vmcb
->control
.tsc_offset
+ native_read_tsc();
2675 *data
= svm
->vmcb
->save
.star
;
2677 #ifdef CONFIG_X86_64
2679 *data
= svm
->vmcb
->save
.lstar
;
2682 *data
= svm
->vmcb
->save
.cstar
;
2684 case MSR_KERNEL_GS_BASE
:
2685 *data
= svm
->vmcb
->save
.kernel_gs_base
;
2687 case MSR_SYSCALL_MASK
:
2688 *data
= svm
->vmcb
->save
.sfmask
;
2691 case MSR_IA32_SYSENTER_CS
:
2692 *data
= svm
->vmcb
->save
.sysenter_cs
;
2694 case MSR_IA32_SYSENTER_EIP
:
2695 *data
= svm
->sysenter_eip
;
2697 case MSR_IA32_SYSENTER_ESP
:
2698 *data
= svm
->sysenter_esp
;
2701 * Nobody will change the following 5 values in the VMCB so we can
2702 * safely return them on rdmsr. They will always be 0 until LBRV is
2705 case MSR_IA32_DEBUGCTLMSR
:
2706 *data
= svm
->vmcb
->save
.dbgctl
;
2708 case MSR_IA32_LASTBRANCHFROMIP
:
2709 *data
= svm
->vmcb
->save
.br_from
;
2711 case MSR_IA32_LASTBRANCHTOIP
:
2712 *data
= svm
->vmcb
->save
.br_to
;
2714 case MSR_IA32_LASTINTFROMIP
:
2715 *data
= svm
->vmcb
->save
.last_excp_from
;
2717 case MSR_IA32_LASTINTTOIP
:
2718 *data
= svm
->vmcb
->save
.last_excp_to
;
2720 case MSR_VM_HSAVE_PA
:
2721 *data
= svm
->nested
.hsave_msr
;
2724 *data
= svm
->nested
.vm_cr_msr
;
2726 case MSR_IA32_UCODE_REV
:
2730 return kvm_get_msr_common(vcpu
, ecx
, data
);
2735 static int rdmsr_interception(struct vcpu_svm
*svm
)
2737 u32 ecx
= svm
->vcpu
.arch
.regs
[VCPU_REGS_RCX
];
2740 if (svm_get_msr(&svm
->vcpu
, ecx
, &data
)) {
2741 trace_kvm_msr_read_ex(ecx
);
2742 kvm_inject_gp(&svm
->vcpu
, 0);
2744 trace_kvm_msr_read(ecx
, data
);
2746 svm
->vcpu
.arch
.regs
[VCPU_REGS_RAX
] = data
& 0xffffffff;
2747 svm
->vcpu
.arch
.regs
[VCPU_REGS_RDX
] = data
>> 32;
2748 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 2;
2749 skip_emulated_instruction(&svm
->vcpu
);
2754 static int svm_set_vm_cr(struct kvm_vcpu
*vcpu
, u64 data
)
2756 struct vcpu_svm
*svm
= to_svm(vcpu
);
2757 int svm_dis
, chg_mask
;
2759 if (data
& ~SVM_VM_CR_VALID_MASK
)
2762 chg_mask
= SVM_VM_CR_VALID_MASK
;
2764 if (svm
->nested
.vm_cr_msr
& SVM_VM_CR_SVM_DIS_MASK
)
2765 chg_mask
&= ~(SVM_VM_CR_SVM_LOCK_MASK
| SVM_VM_CR_SVM_DIS_MASK
);
2767 svm
->nested
.vm_cr_msr
&= ~chg_mask
;
2768 svm
->nested
.vm_cr_msr
|= (data
& chg_mask
);
2770 svm_dis
= svm
->nested
.vm_cr_msr
& SVM_VM_CR_SVM_DIS_MASK
;
2772 /* check for svm_disable while efer.svme is set */
2773 if (svm_dis
&& (vcpu
->arch
.efer
& EFER_SVME
))
2779 static int svm_set_msr(struct kvm_vcpu
*vcpu
, unsigned ecx
, u64 data
)
2781 struct vcpu_svm
*svm
= to_svm(vcpu
);
2785 kvm_write_tsc(vcpu
, data
);
2788 svm
->vmcb
->save
.star
= data
;
2790 #ifdef CONFIG_X86_64
2792 svm
->vmcb
->save
.lstar
= data
;
2795 svm
->vmcb
->save
.cstar
= data
;
2797 case MSR_KERNEL_GS_BASE
:
2798 svm
->vmcb
->save
.kernel_gs_base
= data
;
2800 case MSR_SYSCALL_MASK
:
2801 svm
->vmcb
->save
.sfmask
= data
;
2804 case MSR_IA32_SYSENTER_CS
:
2805 svm
->vmcb
->save
.sysenter_cs
= data
;
2807 case MSR_IA32_SYSENTER_EIP
:
2808 svm
->sysenter_eip
= data
;
2809 svm
->vmcb
->save
.sysenter_eip
= data
;
2811 case MSR_IA32_SYSENTER_ESP
:
2812 svm
->sysenter_esp
= data
;
2813 svm
->vmcb
->save
.sysenter_esp
= data
;
2815 case MSR_IA32_DEBUGCTLMSR
:
2816 if (!boot_cpu_has(X86_FEATURE_LBRV
)) {
2817 pr_unimpl(vcpu
, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
2821 if (data
& DEBUGCTL_RESERVED_BITS
)
2824 svm
->vmcb
->save
.dbgctl
= data
;
2825 if (data
& (1ULL<<0))
2826 svm_enable_lbrv(svm
);
2828 svm_disable_lbrv(svm
);
2830 case MSR_VM_HSAVE_PA
:
2831 svm
->nested
.hsave_msr
= data
;
2834 return svm_set_vm_cr(vcpu
, data
);
2836 pr_unimpl(vcpu
, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx
, data
);
2839 return kvm_set_msr_common(vcpu
, ecx
, data
);
2844 static int wrmsr_interception(struct vcpu_svm
*svm
)
2846 u32 ecx
= svm
->vcpu
.arch
.regs
[VCPU_REGS_RCX
];
2847 u64 data
= (svm
->vcpu
.arch
.regs
[VCPU_REGS_RAX
] & -1u)
2848 | ((u64
)(svm
->vcpu
.arch
.regs
[VCPU_REGS_RDX
] & -1u) << 32);
2851 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 2;
2852 if (svm_set_msr(&svm
->vcpu
, ecx
, data
)) {
2853 trace_kvm_msr_write_ex(ecx
, data
);
2854 kvm_inject_gp(&svm
->vcpu
, 0);
2856 trace_kvm_msr_write(ecx
, data
);
2857 skip_emulated_instruction(&svm
->vcpu
);
2862 static int msr_interception(struct vcpu_svm
*svm
)
2864 if (svm
->vmcb
->control
.exit_info_1
)
2865 return wrmsr_interception(svm
);
2867 return rdmsr_interception(svm
);
2870 static int interrupt_window_interception(struct vcpu_svm
*svm
)
2872 struct kvm_run
*kvm_run
= svm
->vcpu
.run
;
2874 kvm_make_request(KVM_REQ_EVENT
, &svm
->vcpu
);
2875 svm_clear_vintr(svm
);
2876 svm
->vmcb
->control
.int_ctl
&= ~V_IRQ_MASK
;
2878 * If the user space waits to inject interrupts, exit as soon as
2881 if (!irqchip_in_kernel(svm
->vcpu
.kvm
) &&
2882 kvm_run
->request_interrupt_window
&&
2883 !kvm_cpu_has_interrupt(&svm
->vcpu
)) {
2884 ++svm
->vcpu
.stat
.irq_window_exits
;
2885 kvm_run
->exit_reason
= KVM_EXIT_IRQ_WINDOW_OPEN
;
2892 static int pause_interception(struct vcpu_svm
*svm
)
2894 kvm_vcpu_on_spin(&(svm
->vcpu
));
2898 static int (*svm_exit_handlers
[])(struct vcpu_svm
*svm
) = {
2899 [SVM_EXIT_READ_CR0
] = emulate_on_interception
,
2900 [SVM_EXIT_READ_CR3
] = emulate_on_interception
,
2901 [SVM_EXIT_READ_CR4
] = emulate_on_interception
,
2902 [SVM_EXIT_READ_CR8
] = emulate_on_interception
,
2903 [SVM_EXIT_CR0_SEL_WRITE
] = emulate_on_interception
,
2904 [SVM_EXIT_WRITE_CR0
] = cr0_write_interception
,
2905 [SVM_EXIT_WRITE_CR3
] = emulate_on_interception
,
2906 [SVM_EXIT_WRITE_CR4
] = emulate_on_interception
,
2907 [SVM_EXIT_WRITE_CR8
] = cr8_write_interception
,
2908 [SVM_EXIT_READ_DR0
] = emulate_on_interception
,
2909 [SVM_EXIT_READ_DR1
] = emulate_on_interception
,
2910 [SVM_EXIT_READ_DR2
] = emulate_on_interception
,
2911 [SVM_EXIT_READ_DR3
] = emulate_on_interception
,
2912 [SVM_EXIT_READ_DR4
] = emulate_on_interception
,
2913 [SVM_EXIT_READ_DR5
] = emulate_on_interception
,
2914 [SVM_EXIT_READ_DR6
] = emulate_on_interception
,
2915 [SVM_EXIT_READ_DR7
] = emulate_on_interception
,
2916 [SVM_EXIT_WRITE_DR0
] = emulate_on_interception
,
2917 [SVM_EXIT_WRITE_DR1
] = emulate_on_interception
,
2918 [SVM_EXIT_WRITE_DR2
] = emulate_on_interception
,
2919 [SVM_EXIT_WRITE_DR3
] = emulate_on_interception
,
2920 [SVM_EXIT_WRITE_DR4
] = emulate_on_interception
,
2921 [SVM_EXIT_WRITE_DR5
] = emulate_on_interception
,
2922 [SVM_EXIT_WRITE_DR6
] = emulate_on_interception
,
2923 [SVM_EXIT_WRITE_DR7
] = emulate_on_interception
,
2924 [SVM_EXIT_EXCP_BASE
+ DB_VECTOR
] = db_interception
,
2925 [SVM_EXIT_EXCP_BASE
+ BP_VECTOR
] = bp_interception
,
2926 [SVM_EXIT_EXCP_BASE
+ UD_VECTOR
] = ud_interception
,
2927 [SVM_EXIT_EXCP_BASE
+ PF_VECTOR
] = pf_interception
,
2928 [SVM_EXIT_EXCP_BASE
+ NM_VECTOR
] = nm_interception
,
2929 [SVM_EXIT_EXCP_BASE
+ MC_VECTOR
] = mc_interception
,
2930 [SVM_EXIT_INTR
] = intr_interception
,
2931 [SVM_EXIT_NMI
] = nmi_interception
,
2932 [SVM_EXIT_SMI
] = nop_on_interception
,
2933 [SVM_EXIT_INIT
] = nop_on_interception
,
2934 [SVM_EXIT_VINTR
] = interrupt_window_interception
,
2935 [SVM_EXIT_CPUID
] = cpuid_interception
,
2936 [SVM_EXIT_IRET
] = iret_interception
,
2937 [SVM_EXIT_INVD
] = emulate_on_interception
,
2938 [SVM_EXIT_PAUSE
] = pause_interception
,
2939 [SVM_EXIT_HLT
] = halt_interception
,
2940 [SVM_EXIT_INVLPG
] = invlpg_interception
,
2941 [SVM_EXIT_INVLPGA
] = invlpga_interception
,
2942 [SVM_EXIT_IOIO
] = io_interception
,
2943 [SVM_EXIT_MSR
] = msr_interception
,
2944 [SVM_EXIT_TASK_SWITCH
] = task_switch_interception
,
2945 [SVM_EXIT_SHUTDOWN
] = shutdown_interception
,
2946 [SVM_EXIT_VMRUN
] = vmrun_interception
,
2947 [SVM_EXIT_VMMCALL
] = vmmcall_interception
,
2948 [SVM_EXIT_VMLOAD
] = vmload_interception
,
2949 [SVM_EXIT_VMSAVE
] = vmsave_interception
,
2950 [SVM_EXIT_STGI
] = stgi_interception
,
2951 [SVM_EXIT_CLGI
] = clgi_interception
,
2952 [SVM_EXIT_SKINIT
] = skinit_interception
,
2953 [SVM_EXIT_WBINVD
] = emulate_on_interception
,
2954 [SVM_EXIT_MONITOR
] = invalid_op_interception
,
2955 [SVM_EXIT_MWAIT
] = invalid_op_interception
,
2956 [SVM_EXIT_NPF
] = pf_interception
,
2959 void dump_vmcb(struct kvm_vcpu
*vcpu
)
2961 struct vcpu_svm
*svm
= to_svm(vcpu
);
2962 struct vmcb_control_area
*control
= &svm
->vmcb
->control
;
2963 struct vmcb_save_area
*save
= &svm
->vmcb
->save
;
2965 pr_err("VMCB Control Area:\n");
2966 pr_err("cr_read: %04x\n", control
->intercept_cr
& 0xffff);
2967 pr_err("cr_write: %04x\n", control
->intercept_cr
>> 16);
2968 pr_err("dr_read: %04x\n", control
->intercept_dr
& 0xffff);
2969 pr_err("dr_write: %04x\n", control
->intercept_dr
>> 16);
2970 pr_err("exceptions: %08x\n", control
->intercept_exceptions
);
2971 pr_err("intercepts: %016llx\n", control
->intercept
);
2972 pr_err("pause filter count: %d\n", control
->pause_filter_count
);
2973 pr_err("iopm_base_pa: %016llx\n", control
->iopm_base_pa
);
2974 pr_err("msrpm_base_pa: %016llx\n", control
->msrpm_base_pa
);
2975 pr_err("tsc_offset: %016llx\n", control
->tsc_offset
);
2976 pr_err("asid: %d\n", control
->asid
);
2977 pr_err("tlb_ctl: %d\n", control
->tlb_ctl
);
2978 pr_err("int_ctl: %08x\n", control
->int_ctl
);
2979 pr_err("int_vector: %08x\n", control
->int_vector
);
2980 pr_err("int_state: %08x\n", control
->int_state
);
2981 pr_err("exit_code: %08x\n", control
->exit_code
);
2982 pr_err("exit_info1: %016llx\n", control
->exit_info_1
);
2983 pr_err("exit_info2: %016llx\n", control
->exit_info_2
);
2984 pr_err("exit_int_info: %08x\n", control
->exit_int_info
);
2985 pr_err("exit_int_info_err: %08x\n", control
->exit_int_info_err
);
2986 pr_err("nested_ctl: %lld\n", control
->nested_ctl
);
2987 pr_err("nested_cr3: %016llx\n", control
->nested_cr3
);
2988 pr_err("event_inj: %08x\n", control
->event_inj
);
2989 pr_err("event_inj_err: %08x\n", control
->event_inj_err
);
2990 pr_err("lbr_ctl: %lld\n", control
->lbr_ctl
);
2991 pr_err("next_rip: %016llx\n", control
->next_rip
);
2992 pr_err("VMCB State Save Area:\n");
2993 pr_err("es: s: %04x a: %04x l: %08x b: %016llx\n",
2994 save
->es
.selector
, save
->es
.attrib
,
2995 save
->es
.limit
, save
->es
.base
);
2996 pr_err("cs: s: %04x a: %04x l: %08x b: %016llx\n",
2997 save
->cs
.selector
, save
->cs
.attrib
,
2998 save
->cs
.limit
, save
->cs
.base
);
2999 pr_err("ss: s: %04x a: %04x l: %08x b: %016llx\n",
3000 save
->ss
.selector
, save
->ss
.attrib
,
3001 save
->ss
.limit
, save
->ss
.base
);
3002 pr_err("ds: s: %04x a: %04x l: %08x b: %016llx\n",
3003 save
->ds
.selector
, save
->ds
.attrib
,
3004 save
->ds
.limit
, save
->ds
.base
);
3005 pr_err("fs: s: %04x a: %04x l: %08x b: %016llx\n",
3006 save
->fs
.selector
, save
->fs
.attrib
,
3007 save
->fs
.limit
, save
->fs
.base
);
3008 pr_err("gs: s: %04x a: %04x l: %08x b: %016llx\n",
3009 save
->gs
.selector
, save
->gs
.attrib
,
3010 save
->gs
.limit
, save
->gs
.base
);
3011 pr_err("gdtr: s: %04x a: %04x l: %08x b: %016llx\n",
3012 save
->gdtr
.selector
, save
->gdtr
.attrib
,
3013 save
->gdtr
.limit
, save
->gdtr
.base
);
3014 pr_err("ldtr: s: %04x a: %04x l: %08x b: %016llx\n",
3015 save
->ldtr
.selector
, save
->ldtr
.attrib
,
3016 save
->ldtr
.limit
, save
->ldtr
.base
);
3017 pr_err("idtr: s: %04x a: %04x l: %08x b: %016llx\n",
3018 save
->idtr
.selector
, save
->idtr
.attrib
,
3019 save
->idtr
.limit
, save
->idtr
.base
);
3020 pr_err("tr: s: %04x a: %04x l: %08x b: %016llx\n",
3021 save
->tr
.selector
, save
->tr
.attrib
,
3022 save
->tr
.limit
, save
->tr
.base
);
3023 pr_err("cpl: %d efer: %016llx\n",
3024 save
->cpl
, save
->efer
);
3025 pr_err("cr0: %016llx cr2: %016llx\n",
3026 save
->cr0
, save
->cr2
);
3027 pr_err("cr3: %016llx cr4: %016llx\n",
3028 save
->cr3
, save
->cr4
);
3029 pr_err("dr6: %016llx dr7: %016llx\n",
3030 save
->dr6
, save
->dr7
);
3031 pr_err("rip: %016llx rflags: %016llx\n",
3032 save
->rip
, save
->rflags
);
3033 pr_err("rsp: %016llx rax: %016llx\n",
3034 save
->rsp
, save
->rax
);
3035 pr_err("star: %016llx lstar: %016llx\n",
3036 save
->star
, save
->lstar
);
3037 pr_err("cstar: %016llx sfmask: %016llx\n",
3038 save
->cstar
, save
->sfmask
);
3039 pr_err("kernel_gs_base: %016llx sysenter_cs: %016llx\n",
3040 save
->kernel_gs_base
, save
->sysenter_cs
);
3041 pr_err("sysenter_esp: %016llx sysenter_eip: %016llx\n",
3042 save
->sysenter_esp
, save
->sysenter_eip
);
3043 pr_err("gpat: %016llx dbgctl: %016llx\n",
3044 save
->g_pat
, save
->dbgctl
);
3045 pr_err("br_from: %016llx br_to: %016llx\n",
3046 save
->br_from
, save
->br_to
);
3047 pr_err("excp_from: %016llx excp_to: %016llx\n",
3048 save
->last_excp_from
, save
->last_excp_to
);
3052 static void svm_get_exit_info(struct kvm_vcpu
*vcpu
, u64
*info1
, u64
*info2
)
3054 struct vmcb_control_area
*control
= &to_svm(vcpu
)->vmcb
->control
;
3056 *info1
= control
->exit_info_1
;
3057 *info2
= control
->exit_info_2
;
3060 static int handle_exit(struct kvm_vcpu
*vcpu
)
3062 struct vcpu_svm
*svm
= to_svm(vcpu
);
3063 struct kvm_run
*kvm_run
= vcpu
->run
;
3064 u32 exit_code
= svm
->vmcb
->control
.exit_code
;
3066 trace_kvm_exit(exit_code
, vcpu
, KVM_ISA_SVM
);
3068 if (!is_cr_intercept(svm
, INTERCEPT_CR0_WRITE
))
3069 vcpu
->arch
.cr0
= svm
->vmcb
->save
.cr0
;
3071 vcpu
->arch
.cr3
= svm
->vmcb
->save
.cr3
;
3073 if (unlikely(svm
->nested
.exit_required
)) {
3074 nested_svm_vmexit(svm
);
3075 svm
->nested
.exit_required
= false;
3080 if (is_guest_mode(vcpu
)) {
3083 trace_kvm_nested_vmexit(svm
->vmcb
->save
.rip
, exit_code
,
3084 svm
->vmcb
->control
.exit_info_1
,
3085 svm
->vmcb
->control
.exit_info_2
,
3086 svm
->vmcb
->control
.exit_int_info
,
3087 svm
->vmcb
->control
.exit_int_info_err
);
3089 vmexit
= nested_svm_exit_special(svm
);
3091 if (vmexit
== NESTED_EXIT_CONTINUE
)
3092 vmexit
= nested_svm_exit_handled(svm
);
3094 if (vmexit
== NESTED_EXIT_DONE
)
3098 svm_complete_interrupts(svm
);
3100 if (svm
->vmcb
->control
.exit_code
== SVM_EXIT_ERR
) {
3101 kvm_run
->exit_reason
= KVM_EXIT_FAIL_ENTRY
;
3102 kvm_run
->fail_entry
.hardware_entry_failure_reason
3103 = svm
->vmcb
->control
.exit_code
;
3104 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
3109 if (is_external_interrupt(svm
->vmcb
->control
.exit_int_info
) &&
3110 exit_code
!= SVM_EXIT_EXCP_BASE
+ PF_VECTOR
&&
3111 exit_code
!= SVM_EXIT_NPF
&& exit_code
!= SVM_EXIT_TASK_SWITCH
&&
3112 exit_code
!= SVM_EXIT_INTR
&& exit_code
!= SVM_EXIT_NMI
)
3113 printk(KERN_ERR
"%s: unexpected exit_ini_info 0x%x "
3115 __func__
, svm
->vmcb
->control
.exit_int_info
,
3118 if (exit_code
>= ARRAY_SIZE(svm_exit_handlers
)
3119 || !svm_exit_handlers
[exit_code
]) {
3120 kvm_run
->exit_reason
= KVM_EXIT_UNKNOWN
;
3121 kvm_run
->hw
.hardware_exit_reason
= exit_code
;
3125 return svm_exit_handlers
[exit_code
](svm
);
3128 static void reload_tss(struct kvm_vcpu
*vcpu
)
3130 int cpu
= raw_smp_processor_id();
3132 struct svm_cpu_data
*sd
= per_cpu(svm_data
, cpu
);
3133 sd
->tss_desc
->type
= 9; /* available 32/64-bit TSS */
3137 static void pre_svm_run(struct vcpu_svm
*svm
)
3139 int cpu
= raw_smp_processor_id();
3141 struct svm_cpu_data
*sd
= per_cpu(svm_data
, cpu
);
3143 svm
->vmcb
->control
.tlb_ctl
= TLB_CONTROL_DO_NOTHING
;
3144 /* FIXME: handle wraparound of asid_generation */
3145 if (svm
->asid_generation
!= sd
->asid_generation
)
3149 static void svm_inject_nmi(struct kvm_vcpu
*vcpu
)
3151 struct vcpu_svm
*svm
= to_svm(vcpu
);
3153 svm
->vmcb
->control
.event_inj
= SVM_EVTINJ_VALID
| SVM_EVTINJ_TYPE_NMI
;
3154 vcpu
->arch
.hflags
|= HF_NMI_MASK
;
3155 set_intercept(svm
, INTERCEPT_IRET
);
3156 ++vcpu
->stat
.nmi_injections
;
3159 static inline void svm_inject_irq(struct vcpu_svm
*svm
, int irq
)
3161 struct vmcb_control_area
*control
;
3163 control
= &svm
->vmcb
->control
;
3164 control
->int_vector
= irq
;
3165 control
->int_ctl
&= ~V_INTR_PRIO_MASK
;
3166 control
->int_ctl
|= V_IRQ_MASK
|
3167 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT
);
3170 static void svm_set_irq(struct kvm_vcpu
*vcpu
)
3172 struct vcpu_svm
*svm
= to_svm(vcpu
);
3174 BUG_ON(!(gif_set(svm
)));
3176 trace_kvm_inj_virq(vcpu
->arch
.interrupt
.nr
);
3177 ++vcpu
->stat
.irq_injections
;
3179 svm
->vmcb
->control
.event_inj
= vcpu
->arch
.interrupt
.nr
|
3180 SVM_EVTINJ_VALID
| SVM_EVTINJ_TYPE_INTR
;
3183 static void update_cr8_intercept(struct kvm_vcpu
*vcpu
, int tpr
, int irr
)
3185 struct vcpu_svm
*svm
= to_svm(vcpu
);
3187 if (is_guest_mode(vcpu
) && (vcpu
->arch
.hflags
& HF_VINTR_MASK
))
3194 set_cr_intercept(svm
, INTERCEPT_CR8_WRITE
);
3197 static int svm_nmi_allowed(struct kvm_vcpu
*vcpu
)
3199 struct vcpu_svm
*svm
= to_svm(vcpu
);
3200 struct vmcb
*vmcb
= svm
->vmcb
;
3202 ret
= !(vmcb
->control
.int_state
& SVM_INTERRUPT_SHADOW_MASK
) &&
3203 !(svm
->vcpu
.arch
.hflags
& HF_NMI_MASK
);
3204 ret
= ret
&& gif_set(svm
) && nested_svm_nmi(svm
);
3209 static bool svm_get_nmi_mask(struct kvm_vcpu
*vcpu
)
3211 struct vcpu_svm
*svm
= to_svm(vcpu
);
3213 return !!(svm
->vcpu
.arch
.hflags
& HF_NMI_MASK
);
3216 static void svm_set_nmi_mask(struct kvm_vcpu
*vcpu
, bool masked
)
3218 struct vcpu_svm
*svm
= to_svm(vcpu
);
3221 svm
->vcpu
.arch
.hflags
|= HF_NMI_MASK
;
3222 set_intercept(svm
, INTERCEPT_IRET
);
3224 svm
->vcpu
.arch
.hflags
&= ~HF_NMI_MASK
;
3225 clr_intercept(svm
, INTERCEPT_IRET
);
3229 static int svm_interrupt_allowed(struct kvm_vcpu
*vcpu
)
3231 struct vcpu_svm
*svm
= to_svm(vcpu
);
3232 struct vmcb
*vmcb
= svm
->vmcb
;
3235 if (!gif_set(svm
) ||
3236 (vmcb
->control
.int_state
& SVM_INTERRUPT_SHADOW_MASK
))
3239 ret
= !!(vmcb
->save
.rflags
& X86_EFLAGS_IF
);
3241 if (is_guest_mode(vcpu
))
3242 return ret
&& !(svm
->vcpu
.arch
.hflags
& HF_VINTR_MASK
);
3247 static void enable_irq_window(struct kvm_vcpu
*vcpu
)
3249 struct vcpu_svm
*svm
= to_svm(vcpu
);
3252 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3253 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3254 * get that intercept, this function will be called again though and
3255 * we'll get the vintr intercept.
3257 if (gif_set(svm
) && nested_svm_intr(svm
)) {
3259 svm_inject_irq(svm
, 0x0);
3263 static void enable_nmi_window(struct kvm_vcpu
*vcpu
)
3265 struct vcpu_svm
*svm
= to_svm(vcpu
);
3267 if ((svm
->vcpu
.arch
.hflags
& (HF_NMI_MASK
| HF_IRET_MASK
))
3269 return; /* IRET will cause a vm exit */
3272 * Something prevents NMI from been injected. Single step over possible
3273 * problem (IRET or exception injection or interrupt shadow)
3275 svm
->nmi_singlestep
= true;
3276 svm
->vmcb
->save
.rflags
|= (X86_EFLAGS_TF
| X86_EFLAGS_RF
);
3277 update_db_intercept(vcpu
);
3280 static int svm_set_tss_addr(struct kvm
*kvm
, unsigned int addr
)
3285 static void svm_flush_tlb(struct kvm_vcpu
*vcpu
)
3287 force_new_asid(vcpu
);
3290 static void svm_prepare_guest_switch(struct kvm_vcpu
*vcpu
)
3294 static inline void sync_cr8_to_lapic(struct kvm_vcpu
*vcpu
)
3296 struct vcpu_svm
*svm
= to_svm(vcpu
);
3298 if (is_guest_mode(vcpu
) && (vcpu
->arch
.hflags
& HF_VINTR_MASK
))
3301 if (!is_cr_intercept(svm
, INTERCEPT_CR8_WRITE
)) {
3302 int cr8
= svm
->vmcb
->control
.int_ctl
& V_TPR_MASK
;
3303 kvm_set_cr8(vcpu
, cr8
);
3307 static inline void sync_lapic_to_cr8(struct kvm_vcpu
*vcpu
)
3309 struct vcpu_svm
*svm
= to_svm(vcpu
);
3312 if (is_guest_mode(vcpu
) && (vcpu
->arch
.hflags
& HF_VINTR_MASK
))
3315 cr8
= kvm_get_cr8(vcpu
);
3316 svm
->vmcb
->control
.int_ctl
&= ~V_TPR_MASK
;
3317 svm
->vmcb
->control
.int_ctl
|= cr8
& V_TPR_MASK
;
3320 static void svm_complete_interrupts(struct vcpu_svm
*svm
)
3324 u32 exitintinfo
= svm
->vmcb
->control
.exit_int_info
;
3325 unsigned int3_injected
= svm
->int3_injected
;
3327 svm
->int3_injected
= 0;
3329 if (svm
->vcpu
.arch
.hflags
& HF_IRET_MASK
) {
3330 svm
->vcpu
.arch
.hflags
&= ~(HF_NMI_MASK
| HF_IRET_MASK
);
3331 kvm_make_request(KVM_REQ_EVENT
, &svm
->vcpu
);
3334 svm
->vcpu
.arch
.nmi_injected
= false;
3335 kvm_clear_exception_queue(&svm
->vcpu
);
3336 kvm_clear_interrupt_queue(&svm
->vcpu
);
3338 if (!(exitintinfo
& SVM_EXITINTINFO_VALID
))
3341 kvm_make_request(KVM_REQ_EVENT
, &svm
->vcpu
);
3343 vector
= exitintinfo
& SVM_EXITINTINFO_VEC_MASK
;
3344 type
= exitintinfo
& SVM_EXITINTINFO_TYPE_MASK
;
3347 case SVM_EXITINTINFO_TYPE_NMI
:
3348 svm
->vcpu
.arch
.nmi_injected
= true;
3350 case SVM_EXITINTINFO_TYPE_EXEPT
:
3352 * In case of software exceptions, do not reinject the vector,
3353 * but re-execute the instruction instead. Rewind RIP first
3354 * if we emulated INT3 before.
3356 if (kvm_exception_is_soft(vector
)) {
3357 if (vector
== BP_VECTOR
&& int3_injected
&&
3358 kvm_is_linear_rip(&svm
->vcpu
, svm
->int3_rip
))
3359 kvm_rip_write(&svm
->vcpu
,
3360 kvm_rip_read(&svm
->vcpu
) -
3364 if (exitintinfo
& SVM_EXITINTINFO_VALID_ERR
) {
3365 u32 err
= svm
->vmcb
->control
.exit_int_info_err
;
3366 kvm_requeue_exception_e(&svm
->vcpu
, vector
, err
);
3369 kvm_requeue_exception(&svm
->vcpu
, vector
);
3371 case SVM_EXITINTINFO_TYPE_INTR
:
3372 kvm_queue_interrupt(&svm
->vcpu
, vector
, false);
3379 static void svm_cancel_injection(struct kvm_vcpu
*vcpu
)
3381 struct vcpu_svm
*svm
= to_svm(vcpu
);
3382 struct vmcb_control_area
*control
= &svm
->vmcb
->control
;
3384 control
->exit_int_info
= control
->event_inj
;
3385 control
->exit_int_info_err
= control
->event_inj_err
;
3386 control
->event_inj
= 0;
3387 svm_complete_interrupts(svm
);
3390 #ifdef CONFIG_X86_64
3396 static void svm_vcpu_run(struct kvm_vcpu
*vcpu
)
3398 struct vcpu_svm
*svm
= to_svm(vcpu
);
3400 svm
->vmcb
->save
.rax
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
3401 svm
->vmcb
->save
.rsp
= vcpu
->arch
.regs
[VCPU_REGS_RSP
];
3402 svm
->vmcb
->save
.rip
= vcpu
->arch
.regs
[VCPU_REGS_RIP
];
3405 * A vmexit emulation is required before the vcpu can be executed
3408 if (unlikely(svm
->nested
.exit_required
))
3413 sync_lapic_to_cr8(vcpu
);
3415 svm
->vmcb
->save
.cr2
= vcpu
->arch
.cr2
;
3422 "push %%"R
"bp; \n\t"
3423 "mov %c[rbx](%[svm]), %%"R
"bx \n\t"
3424 "mov %c[rcx](%[svm]), %%"R
"cx \n\t"
3425 "mov %c[rdx](%[svm]), %%"R
"dx \n\t"
3426 "mov %c[rsi](%[svm]), %%"R
"si \n\t"
3427 "mov %c[rdi](%[svm]), %%"R
"di \n\t"
3428 "mov %c[rbp](%[svm]), %%"R
"bp \n\t"
3429 #ifdef CONFIG_X86_64
3430 "mov %c[r8](%[svm]), %%r8 \n\t"
3431 "mov %c[r9](%[svm]), %%r9 \n\t"
3432 "mov %c[r10](%[svm]), %%r10 \n\t"
3433 "mov %c[r11](%[svm]), %%r11 \n\t"
3434 "mov %c[r12](%[svm]), %%r12 \n\t"
3435 "mov %c[r13](%[svm]), %%r13 \n\t"
3436 "mov %c[r14](%[svm]), %%r14 \n\t"
3437 "mov %c[r15](%[svm]), %%r15 \n\t"
3440 /* Enter guest mode */
3442 "mov %c[vmcb](%[svm]), %%"R
"ax \n\t"
3443 __ex(SVM_VMLOAD
) "\n\t"
3444 __ex(SVM_VMRUN
) "\n\t"
3445 __ex(SVM_VMSAVE
) "\n\t"
3448 /* Save guest registers, load host registers */
3449 "mov %%"R
"bx, %c[rbx](%[svm]) \n\t"
3450 "mov %%"R
"cx, %c[rcx](%[svm]) \n\t"
3451 "mov %%"R
"dx, %c[rdx](%[svm]) \n\t"
3452 "mov %%"R
"si, %c[rsi](%[svm]) \n\t"
3453 "mov %%"R
"di, %c[rdi](%[svm]) \n\t"
3454 "mov %%"R
"bp, %c[rbp](%[svm]) \n\t"
3455 #ifdef CONFIG_X86_64
3456 "mov %%r8, %c[r8](%[svm]) \n\t"
3457 "mov %%r9, %c[r9](%[svm]) \n\t"
3458 "mov %%r10, %c[r10](%[svm]) \n\t"
3459 "mov %%r11, %c[r11](%[svm]) \n\t"
3460 "mov %%r12, %c[r12](%[svm]) \n\t"
3461 "mov %%r13, %c[r13](%[svm]) \n\t"
3462 "mov %%r14, %c[r14](%[svm]) \n\t"
3463 "mov %%r15, %c[r15](%[svm]) \n\t"
3468 [vmcb
]"i"(offsetof(struct vcpu_svm
, vmcb_pa
)),
3469 [rbx
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RBX
])),
3470 [rcx
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RCX
])),
3471 [rdx
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RDX
])),
3472 [rsi
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RSI
])),
3473 [rdi
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RDI
])),
3474 [rbp
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RBP
]))
3475 #ifdef CONFIG_X86_64
3476 , [r8
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R8
])),
3477 [r9
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R9
])),
3478 [r10
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R10
])),
3479 [r11
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R11
])),
3480 [r12
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R12
])),
3481 [r13
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R13
])),
3482 [r14
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R14
])),
3483 [r15
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R15
]))
3486 , R
"bx", R
"cx", R
"dx", R
"si", R
"di"
3487 #ifdef CONFIG_X86_64
3488 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3492 #ifdef CONFIG_X86_64
3493 wrmsrl(MSR_GS_BASE
, svm
->host
.gs_base
);
3495 loadsegment(fs
, svm
->host
.fs
);
3500 local_irq_disable();
3504 vcpu
->arch
.cr2
= svm
->vmcb
->save
.cr2
;
3505 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = svm
->vmcb
->save
.rax
;
3506 vcpu
->arch
.regs
[VCPU_REGS_RSP
] = svm
->vmcb
->save
.rsp
;
3507 vcpu
->arch
.regs
[VCPU_REGS_RIP
] = svm
->vmcb
->save
.rip
;
3509 sync_cr8_to_lapic(vcpu
);
3513 /* if exit due to PF check for async PF */
3514 if (svm
->vmcb
->control
.exit_code
== SVM_EXIT_EXCP_BASE
+ PF_VECTOR
)
3515 svm
->apf_reason
= kvm_read_and_reset_pf_reason();
3518 vcpu
->arch
.regs_avail
&= ~(1 << VCPU_EXREG_PDPTR
);
3519 vcpu
->arch
.regs_dirty
&= ~(1 << VCPU_EXREG_PDPTR
);
3523 * We need to handle MC intercepts here before the vcpu has a chance to
3524 * change the physical cpu
3526 if (unlikely(svm
->vmcb
->control
.exit_code
==
3527 SVM_EXIT_EXCP_BASE
+ MC_VECTOR
))
3528 svm_handle_mce(svm
);
3530 mark_all_clean(svm
->vmcb
);
3535 static void svm_set_cr3(struct kvm_vcpu
*vcpu
, unsigned long root
)
3537 struct vcpu_svm
*svm
= to_svm(vcpu
);
3539 svm
->vmcb
->save
.cr3
= root
;
3540 force_new_asid(vcpu
);
3543 static void set_tdp_cr3(struct kvm_vcpu
*vcpu
, unsigned long root
)
3545 struct vcpu_svm
*svm
= to_svm(vcpu
);
3547 svm
->vmcb
->control
.nested_cr3
= root
;
3549 /* Also sync guest cr3 here in case we live migrate */
3550 svm
->vmcb
->save
.cr3
= vcpu
->arch
.cr3
;
3552 force_new_asid(vcpu
);
3555 static int is_disabled(void)
3559 rdmsrl(MSR_VM_CR
, vm_cr
);
3560 if (vm_cr
& (1 << SVM_VM_CR_SVM_DISABLE
))
3567 svm_patch_hypercall(struct kvm_vcpu
*vcpu
, unsigned char *hypercall
)
3570 * Patch in the VMMCALL instruction:
3572 hypercall
[0] = 0x0f;
3573 hypercall
[1] = 0x01;
3574 hypercall
[2] = 0xd9;
3577 static void svm_check_processor_compat(void *rtn
)
3582 static bool svm_cpu_has_accelerated_tpr(void)
3587 static u64
svm_get_mt_mask(struct kvm_vcpu
*vcpu
, gfn_t gfn
, bool is_mmio
)
3592 static void svm_cpuid_update(struct kvm_vcpu
*vcpu
)
3596 static void svm_set_supported_cpuid(u32 func
, struct kvm_cpuid_entry2
*entry
)
3600 /* Mask out xsave bit as long as it is not supported by SVM */
3601 entry
->ecx
&= ~(bit(X86_FEATURE_XSAVE
));
3605 entry
->ecx
|= (1 << 2); /* Set SVM bit */
3608 entry
->eax
= 1; /* SVM revision 1 */
3609 entry
->ebx
= 8; /* Lets support 8 ASIDs in case we add proper
3610 ASID emulation to nested SVM */
3611 entry
->ecx
= 0; /* Reserved */
3612 entry
->edx
= 0; /* Per default do not support any
3613 additional features */
3615 /* Support next_rip if host supports it */
3616 if (boot_cpu_has(X86_FEATURE_NRIPS
))
3617 entry
->edx
|= SVM_FEATURE_NRIP
;
3619 /* Support NPT for the guest if enabled */
3621 entry
->edx
|= SVM_FEATURE_NPT
;
3627 static const struct trace_print_flags svm_exit_reasons_str
[] = {
3628 { SVM_EXIT_READ_CR0
, "read_cr0" },
3629 { SVM_EXIT_READ_CR3
, "read_cr3" },
3630 { SVM_EXIT_READ_CR4
, "read_cr4" },
3631 { SVM_EXIT_READ_CR8
, "read_cr8" },
3632 { SVM_EXIT_WRITE_CR0
, "write_cr0" },
3633 { SVM_EXIT_WRITE_CR3
, "write_cr3" },
3634 { SVM_EXIT_WRITE_CR4
, "write_cr4" },
3635 { SVM_EXIT_WRITE_CR8
, "write_cr8" },
3636 { SVM_EXIT_READ_DR0
, "read_dr0" },
3637 { SVM_EXIT_READ_DR1
, "read_dr1" },
3638 { SVM_EXIT_READ_DR2
, "read_dr2" },
3639 { SVM_EXIT_READ_DR3
, "read_dr3" },
3640 { SVM_EXIT_WRITE_DR0
, "write_dr0" },
3641 { SVM_EXIT_WRITE_DR1
, "write_dr1" },
3642 { SVM_EXIT_WRITE_DR2
, "write_dr2" },
3643 { SVM_EXIT_WRITE_DR3
, "write_dr3" },
3644 { SVM_EXIT_WRITE_DR5
, "write_dr5" },
3645 { SVM_EXIT_WRITE_DR7
, "write_dr7" },
3646 { SVM_EXIT_EXCP_BASE
+ DB_VECTOR
, "DB excp" },
3647 { SVM_EXIT_EXCP_BASE
+ BP_VECTOR
, "BP excp" },
3648 { SVM_EXIT_EXCP_BASE
+ UD_VECTOR
, "UD excp" },
3649 { SVM_EXIT_EXCP_BASE
+ PF_VECTOR
, "PF excp" },
3650 { SVM_EXIT_EXCP_BASE
+ NM_VECTOR
, "NM excp" },
3651 { SVM_EXIT_EXCP_BASE
+ MC_VECTOR
, "MC excp" },
3652 { SVM_EXIT_INTR
, "interrupt" },
3653 { SVM_EXIT_NMI
, "nmi" },
3654 { SVM_EXIT_SMI
, "smi" },
3655 { SVM_EXIT_INIT
, "init" },
3656 { SVM_EXIT_VINTR
, "vintr" },
3657 { SVM_EXIT_CPUID
, "cpuid" },
3658 { SVM_EXIT_INVD
, "invd" },
3659 { SVM_EXIT_HLT
, "hlt" },
3660 { SVM_EXIT_INVLPG
, "invlpg" },
3661 { SVM_EXIT_INVLPGA
, "invlpga" },
3662 { SVM_EXIT_IOIO
, "io" },
3663 { SVM_EXIT_MSR
, "msr" },
3664 { SVM_EXIT_TASK_SWITCH
, "task_switch" },
3665 { SVM_EXIT_SHUTDOWN
, "shutdown" },
3666 { SVM_EXIT_VMRUN
, "vmrun" },
3667 { SVM_EXIT_VMMCALL
, "hypercall" },
3668 { SVM_EXIT_VMLOAD
, "vmload" },
3669 { SVM_EXIT_VMSAVE
, "vmsave" },
3670 { SVM_EXIT_STGI
, "stgi" },
3671 { SVM_EXIT_CLGI
, "clgi" },
3672 { SVM_EXIT_SKINIT
, "skinit" },
3673 { SVM_EXIT_WBINVD
, "wbinvd" },
3674 { SVM_EXIT_MONITOR
, "monitor" },
3675 { SVM_EXIT_MWAIT
, "mwait" },
3676 { SVM_EXIT_NPF
, "npf" },
3680 static int svm_get_lpage_level(void)
3682 return PT_PDPE_LEVEL
;
3685 static bool svm_rdtscp_supported(void)
3690 static bool svm_has_wbinvd_exit(void)
3695 static void svm_fpu_deactivate(struct kvm_vcpu
*vcpu
)
3697 struct vcpu_svm
*svm
= to_svm(vcpu
);
3699 set_exception_intercept(svm
, NM_VECTOR
);
3700 update_cr0_intercept(svm
);
3703 static struct kvm_x86_ops svm_x86_ops
= {
3704 .cpu_has_kvm_support
= has_svm
,
3705 .disabled_by_bios
= is_disabled
,
3706 .hardware_setup
= svm_hardware_setup
,
3707 .hardware_unsetup
= svm_hardware_unsetup
,
3708 .check_processor_compatibility
= svm_check_processor_compat
,
3709 .hardware_enable
= svm_hardware_enable
,
3710 .hardware_disable
= svm_hardware_disable
,
3711 .cpu_has_accelerated_tpr
= svm_cpu_has_accelerated_tpr
,
3713 .vcpu_create
= svm_create_vcpu
,
3714 .vcpu_free
= svm_free_vcpu
,
3715 .vcpu_reset
= svm_vcpu_reset
,
3717 .prepare_guest_switch
= svm_prepare_guest_switch
,
3718 .vcpu_load
= svm_vcpu_load
,
3719 .vcpu_put
= svm_vcpu_put
,
3721 .set_guest_debug
= svm_guest_debug
,
3722 .get_msr
= svm_get_msr
,
3723 .set_msr
= svm_set_msr
,
3724 .get_segment_base
= svm_get_segment_base
,
3725 .get_segment
= svm_get_segment
,
3726 .set_segment
= svm_set_segment
,
3727 .get_cpl
= svm_get_cpl
,
3728 .get_cs_db_l_bits
= kvm_get_cs_db_l_bits
,
3729 .decache_cr0_guest_bits
= svm_decache_cr0_guest_bits
,
3730 .decache_cr4_guest_bits
= svm_decache_cr4_guest_bits
,
3731 .set_cr0
= svm_set_cr0
,
3732 .set_cr3
= svm_set_cr3
,
3733 .set_cr4
= svm_set_cr4
,
3734 .set_efer
= svm_set_efer
,
3735 .get_idt
= svm_get_idt
,
3736 .set_idt
= svm_set_idt
,
3737 .get_gdt
= svm_get_gdt
,
3738 .set_gdt
= svm_set_gdt
,
3739 .set_dr7
= svm_set_dr7
,
3740 .cache_reg
= svm_cache_reg
,
3741 .get_rflags
= svm_get_rflags
,
3742 .set_rflags
= svm_set_rflags
,
3743 .fpu_activate
= svm_fpu_activate
,
3744 .fpu_deactivate
= svm_fpu_deactivate
,
3746 .tlb_flush
= svm_flush_tlb
,
3748 .run
= svm_vcpu_run
,
3749 .handle_exit
= handle_exit
,
3750 .skip_emulated_instruction
= skip_emulated_instruction
,
3751 .set_interrupt_shadow
= svm_set_interrupt_shadow
,
3752 .get_interrupt_shadow
= svm_get_interrupt_shadow
,
3753 .patch_hypercall
= svm_patch_hypercall
,
3754 .set_irq
= svm_set_irq
,
3755 .set_nmi
= svm_inject_nmi
,
3756 .queue_exception
= svm_queue_exception
,
3757 .cancel_injection
= svm_cancel_injection
,
3758 .interrupt_allowed
= svm_interrupt_allowed
,
3759 .nmi_allowed
= svm_nmi_allowed
,
3760 .get_nmi_mask
= svm_get_nmi_mask
,
3761 .set_nmi_mask
= svm_set_nmi_mask
,
3762 .enable_nmi_window
= enable_nmi_window
,
3763 .enable_irq_window
= enable_irq_window
,
3764 .update_cr8_intercept
= update_cr8_intercept
,
3766 .set_tss_addr
= svm_set_tss_addr
,
3767 .get_tdp_level
= get_npt_level
,
3768 .get_mt_mask
= svm_get_mt_mask
,
3770 .get_exit_info
= svm_get_exit_info
,
3771 .exit_reasons_str
= svm_exit_reasons_str
,
3773 .get_lpage_level
= svm_get_lpage_level
,
3775 .cpuid_update
= svm_cpuid_update
,
3777 .rdtscp_supported
= svm_rdtscp_supported
,
3779 .set_supported_cpuid
= svm_set_supported_cpuid
,
3781 .has_wbinvd_exit
= svm_has_wbinvd_exit
,
3783 .write_tsc_offset
= svm_write_tsc_offset
,
3784 .adjust_tsc_offset
= svm_adjust_tsc_offset
,
3786 .set_tdp_cr3
= set_tdp_cr3
,
3789 static int __init
svm_init(void)
3791 return kvm_init(&svm_x86_ops
, sizeof(struct vcpu_svm
),
3792 __alignof__(struct vcpu_svm
), THIS_MODULE
);
3795 static void __exit
svm_exit(void)
3800 module_init(svm_init
)
3801 module_exit(svm_exit
)