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
);
188 static inline struct vcpu_svm
*to_svm(struct kvm_vcpu
*vcpu
)
190 return container_of(vcpu
, struct vcpu_svm
, vcpu
);
193 static void recalc_intercepts(struct vcpu_svm
*svm
)
195 struct vmcb_control_area
*c
, *h
;
196 struct nested_state
*g
;
198 if (!is_guest_mode(&svm
->vcpu
))
201 c
= &svm
->vmcb
->control
;
202 h
= &svm
->nested
.hsave
->control
;
205 c
->intercept_cr
= h
->intercept_cr
| g
->intercept_cr
;
206 c
->intercept_dr
= h
->intercept_dr
| g
->intercept_dr
;
207 c
->intercept_exceptions
= h
->intercept_exceptions
| g
->intercept_exceptions
;
208 c
->intercept
= h
->intercept
| g
->intercept
;
211 static inline struct vmcb
*get_host_vmcb(struct vcpu_svm
*svm
)
213 if (is_guest_mode(&svm
->vcpu
))
214 return svm
->nested
.hsave
;
219 static inline void set_cr_intercept(struct vcpu_svm
*svm
, int bit
)
221 struct vmcb
*vmcb
= get_host_vmcb(svm
);
223 vmcb
->control
.intercept_cr
|= (1U << bit
);
225 recalc_intercepts(svm
);
228 static inline void clr_cr_intercept(struct vcpu_svm
*svm
, int bit
)
230 struct vmcb
*vmcb
= get_host_vmcb(svm
);
232 vmcb
->control
.intercept_cr
&= ~(1U << bit
);
234 recalc_intercepts(svm
);
237 static inline bool is_cr_intercept(struct vcpu_svm
*svm
, int bit
)
239 struct vmcb
*vmcb
= get_host_vmcb(svm
);
241 return vmcb
->control
.intercept_cr
& (1U << bit
);
244 static inline void set_dr_intercept(struct vcpu_svm
*svm
, int bit
)
246 struct vmcb
*vmcb
= get_host_vmcb(svm
);
248 vmcb
->control
.intercept_dr
|= (1U << bit
);
250 recalc_intercepts(svm
);
253 static inline void clr_dr_intercept(struct vcpu_svm
*svm
, int bit
)
255 struct vmcb
*vmcb
= get_host_vmcb(svm
);
257 vmcb
->control
.intercept_dr
&= ~(1U << bit
);
259 recalc_intercepts(svm
);
262 static inline void set_exception_intercept(struct vcpu_svm
*svm
, int bit
)
264 struct vmcb
*vmcb
= get_host_vmcb(svm
);
266 vmcb
->control
.intercept_exceptions
|= (1U << bit
);
268 recalc_intercepts(svm
);
271 static inline void clr_exception_intercept(struct vcpu_svm
*svm
, int bit
)
273 struct vmcb
*vmcb
= get_host_vmcb(svm
);
275 vmcb
->control
.intercept_exceptions
&= ~(1U << bit
);
277 recalc_intercepts(svm
);
280 static inline void set_intercept(struct vcpu_svm
*svm
, int bit
)
282 struct vmcb
*vmcb
= get_host_vmcb(svm
);
284 vmcb
->control
.intercept
|= (1ULL << bit
);
286 recalc_intercepts(svm
);
289 static inline void clr_intercept(struct vcpu_svm
*svm
, int bit
)
291 struct vmcb
*vmcb
= get_host_vmcb(svm
);
293 vmcb
->control
.intercept
&= ~(1ULL << bit
);
295 recalc_intercepts(svm
);
298 static inline void enable_gif(struct vcpu_svm
*svm
)
300 svm
->vcpu
.arch
.hflags
|= HF_GIF_MASK
;
303 static inline void disable_gif(struct vcpu_svm
*svm
)
305 svm
->vcpu
.arch
.hflags
&= ~HF_GIF_MASK
;
308 static inline bool gif_set(struct vcpu_svm
*svm
)
310 return !!(svm
->vcpu
.arch
.hflags
& HF_GIF_MASK
);
313 static unsigned long iopm_base
;
315 struct kvm_ldttss_desc
{
318 unsigned base1
:8, type
:5, dpl
:2, p
:1;
319 unsigned limit1
:4, zero0
:3, g
:1, base2
:8;
322 } __attribute__((packed
));
324 struct svm_cpu_data
{
330 struct kvm_ldttss_desc
*tss_desc
;
332 struct page
*save_area
;
335 static DEFINE_PER_CPU(struct svm_cpu_data
*, svm_data
);
336 static uint32_t svm_features
;
338 struct svm_init_data
{
343 static u32 msrpm_ranges
[] = {0, 0xc0000000, 0xc0010000};
345 #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
346 #define MSRS_RANGE_SIZE 2048
347 #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
349 static u32
svm_msrpm_offset(u32 msr
)
354 for (i
= 0; i
< NUM_MSR_MAPS
; i
++) {
355 if (msr
< msrpm_ranges
[i
] ||
356 msr
>= msrpm_ranges
[i
] + MSRS_IN_RANGE
)
359 offset
= (msr
- msrpm_ranges
[i
]) / 4; /* 4 msrs per u8 */
360 offset
+= (i
* MSRS_RANGE_SIZE
); /* add range offset */
362 /* Now we have the u8 offset - but need the u32 offset */
366 /* MSR not in any range */
370 #define MAX_INST_SIZE 15
372 static inline void clgi(void)
374 asm volatile (__ex(SVM_CLGI
));
377 static inline void stgi(void)
379 asm volatile (__ex(SVM_STGI
));
382 static inline void invlpga(unsigned long addr
, u32 asid
)
384 asm volatile (__ex(SVM_INVLPGA
) : : "a"(addr
), "c"(asid
));
387 static inline void force_new_asid(struct kvm_vcpu
*vcpu
)
389 to_svm(vcpu
)->asid_generation
--;
392 static inline void flush_guest_tlb(struct kvm_vcpu
*vcpu
)
394 force_new_asid(vcpu
);
397 static int get_npt_level(void)
400 return PT64_ROOT_LEVEL
;
402 return PT32E_ROOT_LEVEL
;
406 static void svm_set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
408 vcpu
->arch
.efer
= efer
;
409 if (!npt_enabled
&& !(efer
& EFER_LMA
))
412 to_svm(vcpu
)->vmcb
->save
.efer
= efer
| EFER_SVME
;
415 static int is_external_interrupt(u32 info
)
417 info
&= SVM_EVTINJ_TYPE_MASK
| SVM_EVTINJ_VALID
;
418 return info
== (SVM_EVTINJ_VALID
| SVM_EVTINJ_TYPE_INTR
);
421 static u32
svm_get_interrupt_shadow(struct kvm_vcpu
*vcpu
, int mask
)
423 struct vcpu_svm
*svm
= to_svm(vcpu
);
426 if (svm
->vmcb
->control
.int_state
& SVM_INTERRUPT_SHADOW_MASK
)
427 ret
|= KVM_X86_SHADOW_INT_STI
| KVM_X86_SHADOW_INT_MOV_SS
;
431 static void svm_set_interrupt_shadow(struct kvm_vcpu
*vcpu
, int mask
)
433 struct vcpu_svm
*svm
= to_svm(vcpu
);
436 svm
->vmcb
->control
.int_state
&= ~SVM_INTERRUPT_SHADOW_MASK
;
438 svm
->vmcb
->control
.int_state
|= SVM_INTERRUPT_SHADOW_MASK
;
442 static void skip_emulated_instruction(struct kvm_vcpu
*vcpu
)
444 struct vcpu_svm
*svm
= to_svm(vcpu
);
446 if (svm
->vmcb
->control
.next_rip
!= 0)
447 svm
->next_rip
= svm
->vmcb
->control
.next_rip
;
449 if (!svm
->next_rip
) {
450 if (emulate_instruction(vcpu
, 0, 0, EMULTYPE_SKIP
) !=
452 printk(KERN_DEBUG
"%s: NOP\n", __func__
);
455 if (svm
->next_rip
- kvm_rip_read(vcpu
) > MAX_INST_SIZE
)
456 printk(KERN_ERR
"%s: ip 0x%lx next 0x%llx\n",
457 __func__
, kvm_rip_read(vcpu
), svm
->next_rip
);
459 kvm_rip_write(vcpu
, svm
->next_rip
);
460 svm_set_interrupt_shadow(vcpu
, 0);
463 static void svm_queue_exception(struct kvm_vcpu
*vcpu
, unsigned nr
,
464 bool has_error_code
, u32 error_code
,
467 struct vcpu_svm
*svm
= to_svm(vcpu
);
470 * If we are within a nested VM we'd better #VMEXIT and let the guest
471 * handle the exception
474 nested_svm_check_exception(svm
, nr
, has_error_code
, error_code
))
477 if (nr
== BP_VECTOR
&& !static_cpu_has(X86_FEATURE_NRIPS
)) {
478 unsigned long rip
, old_rip
= kvm_rip_read(&svm
->vcpu
);
481 * For guest debugging where we have to reinject #BP if some
482 * INT3 is guest-owned:
483 * Emulate nRIP by moving RIP forward. Will fail if injection
484 * raises a fault that is not intercepted. Still better than
485 * failing in all cases.
487 skip_emulated_instruction(&svm
->vcpu
);
488 rip
= kvm_rip_read(&svm
->vcpu
);
489 svm
->int3_rip
= rip
+ svm
->vmcb
->save
.cs
.base
;
490 svm
->int3_injected
= rip
- old_rip
;
493 svm
->vmcb
->control
.event_inj
= nr
495 | (has_error_code
? SVM_EVTINJ_VALID_ERR
: 0)
496 | SVM_EVTINJ_TYPE_EXEPT
;
497 svm
->vmcb
->control
.event_inj_err
= error_code
;
500 static void svm_init_erratum_383(void)
506 if (!cpu_has_amd_erratum(amd_erratum_383
))
509 /* Use _safe variants to not break nested virtualization */
510 val
= native_read_msr_safe(MSR_AMD64_DC_CFG
, &err
);
516 low
= lower_32_bits(val
);
517 high
= upper_32_bits(val
);
519 native_write_msr_safe(MSR_AMD64_DC_CFG
, low
, high
);
521 erratum_383_found
= true;
524 static int has_svm(void)
528 if (!cpu_has_svm(&msg
)) {
529 printk(KERN_INFO
"has_svm: %s\n", msg
);
536 static void svm_hardware_disable(void *garbage
)
541 static int svm_hardware_enable(void *garbage
)
544 struct svm_cpu_data
*sd
;
546 struct desc_ptr gdt_descr
;
547 struct desc_struct
*gdt
;
548 int me
= raw_smp_processor_id();
550 rdmsrl(MSR_EFER
, efer
);
551 if (efer
& EFER_SVME
)
555 printk(KERN_ERR
"svm_hardware_enable: err EOPNOTSUPP on %d\n",
559 sd
= per_cpu(svm_data
, me
);
562 printk(KERN_ERR
"svm_hardware_enable: svm_data is NULL on %d\n",
567 sd
->asid_generation
= 1;
568 sd
->max_asid
= cpuid_ebx(SVM_CPUID_FUNC
) - 1;
569 sd
->next_asid
= sd
->max_asid
+ 1;
571 native_store_gdt(&gdt_descr
);
572 gdt
= (struct desc_struct
*)gdt_descr
.address
;
573 sd
->tss_desc
= (struct kvm_ldttss_desc
*)(gdt
+ GDT_ENTRY_TSS
);
575 wrmsrl(MSR_EFER
, efer
| EFER_SVME
);
577 wrmsrl(MSR_VM_HSAVE_PA
, page_to_pfn(sd
->save_area
) << PAGE_SHIFT
);
579 svm_init_erratum_383();
584 static void svm_cpu_uninit(int cpu
)
586 struct svm_cpu_data
*sd
= per_cpu(svm_data
, raw_smp_processor_id());
591 per_cpu(svm_data
, raw_smp_processor_id()) = NULL
;
592 __free_page(sd
->save_area
);
596 static int svm_cpu_init(int cpu
)
598 struct svm_cpu_data
*sd
;
601 sd
= kzalloc(sizeof(struct svm_cpu_data
), GFP_KERNEL
);
605 sd
->save_area
= alloc_page(GFP_KERNEL
);
610 per_cpu(svm_data
, cpu
) = sd
;
620 static bool valid_msr_intercept(u32 index
)
624 for (i
= 0; direct_access_msrs
[i
].index
!= MSR_INVALID
; i
++)
625 if (direct_access_msrs
[i
].index
== index
)
631 static void set_msr_interception(u32
*msrpm
, unsigned msr
,
634 u8 bit_read
, bit_write
;
639 * If this warning triggers extend the direct_access_msrs list at the
640 * beginning of the file
642 WARN_ON(!valid_msr_intercept(msr
));
644 offset
= svm_msrpm_offset(msr
);
645 bit_read
= 2 * (msr
& 0x0f);
646 bit_write
= 2 * (msr
& 0x0f) + 1;
649 BUG_ON(offset
== MSR_INVALID
);
651 read
? clear_bit(bit_read
, &tmp
) : set_bit(bit_read
, &tmp
);
652 write
? clear_bit(bit_write
, &tmp
) : set_bit(bit_write
, &tmp
);
657 static void svm_vcpu_init_msrpm(u32
*msrpm
)
661 memset(msrpm
, 0xff, PAGE_SIZE
* (1 << MSRPM_ALLOC_ORDER
));
663 for (i
= 0; direct_access_msrs
[i
].index
!= MSR_INVALID
; i
++) {
664 if (!direct_access_msrs
[i
].always
)
667 set_msr_interception(msrpm
, direct_access_msrs
[i
].index
, 1, 1);
671 static void add_msr_offset(u32 offset
)
675 for (i
= 0; i
< MSRPM_OFFSETS
; ++i
) {
677 /* Offset already in list? */
678 if (msrpm_offsets
[i
] == offset
)
681 /* Slot used by another offset? */
682 if (msrpm_offsets
[i
] != MSR_INVALID
)
685 /* Add offset to list */
686 msrpm_offsets
[i
] = offset
;
692 * If this BUG triggers the msrpm_offsets table has an overflow. Just
693 * increase MSRPM_OFFSETS in this case.
698 static void init_msrpm_offsets(void)
702 memset(msrpm_offsets
, 0xff, sizeof(msrpm_offsets
));
704 for (i
= 0; direct_access_msrs
[i
].index
!= MSR_INVALID
; i
++) {
707 offset
= svm_msrpm_offset(direct_access_msrs
[i
].index
);
708 BUG_ON(offset
== MSR_INVALID
);
710 add_msr_offset(offset
);
714 static void svm_enable_lbrv(struct vcpu_svm
*svm
)
716 u32
*msrpm
= svm
->msrpm
;
718 svm
->vmcb
->control
.lbr_ctl
= 1;
719 set_msr_interception(msrpm
, MSR_IA32_LASTBRANCHFROMIP
, 1, 1);
720 set_msr_interception(msrpm
, MSR_IA32_LASTBRANCHTOIP
, 1, 1);
721 set_msr_interception(msrpm
, MSR_IA32_LASTINTFROMIP
, 1, 1);
722 set_msr_interception(msrpm
, MSR_IA32_LASTINTTOIP
, 1, 1);
725 static void svm_disable_lbrv(struct vcpu_svm
*svm
)
727 u32
*msrpm
= svm
->msrpm
;
729 svm
->vmcb
->control
.lbr_ctl
= 0;
730 set_msr_interception(msrpm
, MSR_IA32_LASTBRANCHFROMIP
, 0, 0);
731 set_msr_interception(msrpm
, MSR_IA32_LASTBRANCHTOIP
, 0, 0);
732 set_msr_interception(msrpm
, MSR_IA32_LASTINTFROMIP
, 0, 0);
733 set_msr_interception(msrpm
, MSR_IA32_LASTINTTOIP
, 0, 0);
736 static __init
int svm_hardware_setup(void)
739 struct page
*iopm_pages
;
743 iopm_pages
= alloc_pages(GFP_KERNEL
, IOPM_ALLOC_ORDER
);
748 iopm_va
= page_address(iopm_pages
);
749 memset(iopm_va
, 0xff, PAGE_SIZE
* (1 << IOPM_ALLOC_ORDER
));
750 iopm_base
= page_to_pfn(iopm_pages
) << PAGE_SHIFT
;
752 init_msrpm_offsets();
754 if (boot_cpu_has(X86_FEATURE_NX
))
755 kvm_enable_efer_bits(EFER_NX
);
757 if (boot_cpu_has(X86_FEATURE_FXSR_OPT
))
758 kvm_enable_efer_bits(EFER_FFXSR
);
761 printk(KERN_INFO
"kvm: Nested Virtualization enabled\n");
762 kvm_enable_efer_bits(EFER_SVME
| EFER_LMSLE
);
765 for_each_possible_cpu(cpu
) {
766 r
= svm_cpu_init(cpu
);
771 svm_features
= cpuid_edx(SVM_CPUID_FUNC
);
773 if (!boot_cpu_has(X86_FEATURE_NPT
))
776 if (npt_enabled
&& !npt
) {
777 printk(KERN_INFO
"kvm: Nested Paging disabled\n");
782 printk(KERN_INFO
"kvm: Nested Paging enabled\n");
790 __free_pages(iopm_pages
, IOPM_ALLOC_ORDER
);
795 static __exit
void svm_hardware_unsetup(void)
799 for_each_possible_cpu(cpu
)
802 __free_pages(pfn_to_page(iopm_base
>> PAGE_SHIFT
), IOPM_ALLOC_ORDER
);
806 static void init_seg(struct vmcb_seg
*seg
)
809 seg
->attrib
= SVM_SELECTOR_P_MASK
| SVM_SELECTOR_S_MASK
|
810 SVM_SELECTOR_WRITE_MASK
; /* Read/Write Data Segment */
815 static void init_sys_seg(struct vmcb_seg
*seg
, uint32_t type
)
818 seg
->attrib
= SVM_SELECTOR_P_MASK
| type
;
823 static void svm_write_tsc_offset(struct kvm_vcpu
*vcpu
, u64 offset
)
825 struct vcpu_svm
*svm
= to_svm(vcpu
);
826 u64 g_tsc_offset
= 0;
828 if (is_guest_mode(vcpu
)) {
829 g_tsc_offset
= svm
->vmcb
->control
.tsc_offset
-
830 svm
->nested
.hsave
->control
.tsc_offset
;
831 svm
->nested
.hsave
->control
.tsc_offset
= offset
;
834 svm
->vmcb
->control
.tsc_offset
= offset
+ g_tsc_offset
;
837 static void svm_adjust_tsc_offset(struct kvm_vcpu
*vcpu
, s64 adjustment
)
839 struct vcpu_svm
*svm
= to_svm(vcpu
);
841 svm
->vmcb
->control
.tsc_offset
+= adjustment
;
842 if (is_guest_mode(vcpu
))
843 svm
->nested
.hsave
->control
.tsc_offset
+= adjustment
;
846 static void init_vmcb(struct vcpu_svm
*svm
)
848 struct vmcb_control_area
*control
= &svm
->vmcb
->control
;
849 struct vmcb_save_area
*save
= &svm
->vmcb
->save
;
851 svm
->vcpu
.fpu_active
= 1;
852 svm
->vcpu
.arch
.hflags
= 0;
854 set_cr_intercept(svm
, INTERCEPT_CR0_READ
);
855 set_cr_intercept(svm
, INTERCEPT_CR3_READ
);
856 set_cr_intercept(svm
, INTERCEPT_CR4_READ
);
857 set_cr_intercept(svm
, INTERCEPT_CR0_WRITE
);
858 set_cr_intercept(svm
, INTERCEPT_CR3_WRITE
);
859 set_cr_intercept(svm
, INTERCEPT_CR4_WRITE
);
860 set_cr_intercept(svm
, INTERCEPT_CR8_WRITE
);
862 set_dr_intercept(svm
, INTERCEPT_DR0_READ
);
863 set_dr_intercept(svm
, INTERCEPT_DR1_READ
);
864 set_dr_intercept(svm
, INTERCEPT_DR2_READ
);
865 set_dr_intercept(svm
, INTERCEPT_DR3_READ
);
866 set_dr_intercept(svm
, INTERCEPT_DR4_READ
);
867 set_dr_intercept(svm
, INTERCEPT_DR5_READ
);
868 set_dr_intercept(svm
, INTERCEPT_DR6_READ
);
869 set_dr_intercept(svm
, INTERCEPT_DR7_READ
);
871 set_dr_intercept(svm
, INTERCEPT_DR0_WRITE
);
872 set_dr_intercept(svm
, INTERCEPT_DR1_WRITE
);
873 set_dr_intercept(svm
, INTERCEPT_DR2_WRITE
);
874 set_dr_intercept(svm
, INTERCEPT_DR3_WRITE
);
875 set_dr_intercept(svm
, INTERCEPT_DR4_WRITE
);
876 set_dr_intercept(svm
, INTERCEPT_DR5_WRITE
);
877 set_dr_intercept(svm
, INTERCEPT_DR6_WRITE
);
878 set_dr_intercept(svm
, INTERCEPT_DR7_WRITE
);
880 set_exception_intercept(svm
, PF_VECTOR
);
881 set_exception_intercept(svm
, UD_VECTOR
);
882 set_exception_intercept(svm
, MC_VECTOR
);
884 set_intercept(svm
, INTERCEPT_INTR
);
885 set_intercept(svm
, INTERCEPT_NMI
);
886 set_intercept(svm
, INTERCEPT_SMI
);
887 set_intercept(svm
, INTERCEPT_SELECTIVE_CR0
);
888 set_intercept(svm
, INTERCEPT_CPUID
);
889 set_intercept(svm
, INTERCEPT_INVD
);
890 set_intercept(svm
, INTERCEPT_HLT
);
891 set_intercept(svm
, INTERCEPT_INVLPG
);
892 set_intercept(svm
, INTERCEPT_INVLPGA
);
893 set_intercept(svm
, INTERCEPT_IOIO_PROT
);
894 set_intercept(svm
, INTERCEPT_MSR_PROT
);
895 set_intercept(svm
, INTERCEPT_TASK_SWITCH
);
896 set_intercept(svm
, INTERCEPT_SHUTDOWN
);
897 set_intercept(svm
, INTERCEPT_VMRUN
);
898 set_intercept(svm
, INTERCEPT_VMMCALL
);
899 set_intercept(svm
, INTERCEPT_VMLOAD
);
900 set_intercept(svm
, INTERCEPT_VMSAVE
);
901 set_intercept(svm
, INTERCEPT_STGI
);
902 set_intercept(svm
, INTERCEPT_CLGI
);
903 set_intercept(svm
, INTERCEPT_SKINIT
);
904 set_intercept(svm
, INTERCEPT_WBINVD
);
905 set_intercept(svm
, INTERCEPT_MONITOR
);
906 set_intercept(svm
, INTERCEPT_MWAIT
);
908 control
->iopm_base_pa
= iopm_base
;
909 control
->msrpm_base_pa
= __pa(svm
->msrpm
);
910 control
->int_ctl
= V_INTR_MASKING_MASK
;
918 save
->cs
.selector
= 0xf000;
919 /* Executable/Readable Code Segment */
920 save
->cs
.attrib
= SVM_SELECTOR_READ_MASK
| SVM_SELECTOR_P_MASK
|
921 SVM_SELECTOR_S_MASK
| SVM_SELECTOR_CODE_MASK
;
922 save
->cs
.limit
= 0xffff;
924 * cs.base should really be 0xffff0000, but vmx can't handle that, so
925 * be consistent with it.
927 * Replace when we have real mode working for vmx.
929 save
->cs
.base
= 0xf0000;
931 save
->gdtr
.limit
= 0xffff;
932 save
->idtr
.limit
= 0xffff;
934 init_sys_seg(&save
->ldtr
, SEG_TYPE_LDT
);
935 init_sys_seg(&save
->tr
, SEG_TYPE_BUSY_TSS16
);
937 svm_set_efer(&svm
->vcpu
, 0);
938 save
->dr6
= 0xffff0ff0;
941 save
->rip
= 0x0000fff0;
942 svm
->vcpu
.arch
.regs
[VCPU_REGS_RIP
] = save
->rip
;
945 * This is the guest-visible cr0 value.
946 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
948 svm
->vcpu
.arch
.cr0
= 0;
949 (void)kvm_set_cr0(&svm
->vcpu
, X86_CR0_NW
| X86_CR0_CD
| X86_CR0_ET
);
951 save
->cr4
= X86_CR4_PAE
;
955 /* Setup VMCB for Nested Paging */
956 control
->nested_ctl
= 1;
957 clr_intercept(svm
, INTERCEPT_TASK_SWITCH
);
958 clr_intercept(svm
, INTERCEPT_INVLPG
);
959 clr_exception_intercept(svm
, PF_VECTOR
);
960 clr_cr_intercept(svm
, INTERCEPT_CR3_READ
);
961 clr_cr_intercept(svm
, INTERCEPT_CR3_WRITE
);
962 save
->g_pat
= 0x0007040600070406ULL
;
966 force_new_asid(&svm
->vcpu
);
968 svm
->nested
.vmcb
= 0;
969 svm
->vcpu
.arch
.hflags
= 0;
971 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER
)) {
972 control
->pause_filter_count
= 3000;
973 set_intercept(svm
, INTERCEPT_PAUSE
);
979 static int svm_vcpu_reset(struct kvm_vcpu
*vcpu
)
981 struct vcpu_svm
*svm
= to_svm(vcpu
);
985 if (!kvm_vcpu_is_bsp(vcpu
)) {
986 kvm_rip_write(vcpu
, 0);
987 svm
->vmcb
->save
.cs
.base
= svm
->vcpu
.arch
.sipi_vector
<< 12;
988 svm
->vmcb
->save
.cs
.selector
= svm
->vcpu
.arch
.sipi_vector
<< 8;
990 vcpu
->arch
.regs_avail
= ~0;
991 vcpu
->arch
.regs_dirty
= ~0;
996 static struct kvm_vcpu
*svm_create_vcpu(struct kvm
*kvm
, unsigned int id
)
998 struct vcpu_svm
*svm
;
1000 struct page
*msrpm_pages
;
1001 struct page
*hsave_page
;
1002 struct page
*nested_msrpm_pages
;
1005 svm
= kmem_cache_zalloc(kvm_vcpu_cache
, GFP_KERNEL
);
1011 err
= kvm_vcpu_init(&svm
->vcpu
, kvm
, id
);
1016 page
= alloc_page(GFP_KERNEL
);
1020 msrpm_pages
= alloc_pages(GFP_KERNEL
, MSRPM_ALLOC_ORDER
);
1024 nested_msrpm_pages
= alloc_pages(GFP_KERNEL
, MSRPM_ALLOC_ORDER
);
1025 if (!nested_msrpm_pages
)
1028 hsave_page
= alloc_page(GFP_KERNEL
);
1032 svm
->nested
.hsave
= page_address(hsave_page
);
1034 svm
->msrpm
= page_address(msrpm_pages
);
1035 svm_vcpu_init_msrpm(svm
->msrpm
);
1037 svm
->nested
.msrpm
= page_address(nested_msrpm_pages
);
1038 svm_vcpu_init_msrpm(svm
->nested
.msrpm
);
1040 svm
->vmcb
= page_address(page
);
1041 clear_page(svm
->vmcb
);
1042 svm
->vmcb_pa
= page_to_pfn(page
) << PAGE_SHIFT
;
1043 svm
->asid_generation
= 0;
1045 kvm_write_tsc(&svm
->vcpu
, 0);
1047 err
= fx_init(&svm
->vcpu
);
1051 svm
->vcpu
.arch
.apic_base
= 0xfee00000 | MSR_IA32_APICBASE_ENABLE
;
1052 if (kvm_vcpu_is_bsp(&svm
->vcpu
))
1053 svm
->vcpu
.arch
.apic_base
|= MSR_IA32_APICBASE_BSP
;
1058 __free_page(hsave_page
);
1060 __free_pages(nested_msrpm_pages
, MSRPM_ALLOC_ORDER
);
1062 __free_pages(msrpm_pages
, MSRPM_ALLOC_ORDER
);
1066 kvm_vcpu_uninit(&svm
->vcpu
);
1068 kmem_cache_free(kvm_vcpu_cache
, svm
);
1070 return ERR_PTR(err
);
1073 static void svm_free_vcpu(struct kvm_vcpu
*vcpu
)
1075 struct vcpu_svm
*svm
= to_svm(vcpu
);
1077 __free_page(pfn_to_page(svm
->vmcb_pa
>> PAGE_SHIFT
));
1078 __free_pages(virt_to_page(svm
->msrpm
), MSRPM_ALLOC_ORDER
);
1079 __free_page(virt_to_page(svm
->nested
.hsave
));
1080 __free_pages(virt_to_page(svm
->nested
.msrpm
), MSRPM_ALLOC_ORDER
);
1081 kvm_vcpu_uninit(vcpu
);
1082 kmem_cache_free(kvm_vcpu_cache
, svm
);
1085 static void svm_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
1087 struct vcpu_svm
*svm
= to_svm(vcpu
);
1090 if (unlikely(cpu
!= vcpu
->cpu
)) {
1091 svm
->asid_generation
= 0;
1094 #ifdef CONFIG_X86_64
1095 rdmsrl(MSR_GS_BASE
, to_svm(vcpu
)->host
.gs_base
);
1097 savesegment(fs
, svm
->host
.fs
);
1098 savesegment(gs
, svm
->host
.gs
);
1099 svm
->host
.ldt
= kvm_read_ldt();
1101 for (i
= 0; i
< NR_HOST_SAVE_USER_MSRS
; i
++)
1102 rdmsrl(host_save_user_msrs
[i
], svm
->host_user_msrs
[i
]);
1105 static void svm_vcpu_put(struct kvm_vcpu
*vcpu
)
1107 struct vcpu_svm
*svm
= to_svm(vcpu
);
1110 ++vcpu
->stat
.host_state_reload
;
1111 kvm_load_ldt(svm
->host
.ldt
);
1112 #ifdef CONFIG_X86_64
1113 loadsegment(fs
, svm
->host
.fs
);
1114 load_gs_index(svm
->host
.gs
);
1115 wrmsrl(MSR_KERNEL_GS_BASE
, current
->thread
.gs
);
1117 loadsegment(gs
, svm
->host
.gs
);
1119 for (i
= 0; i
< NR_HOST_SAVE_USER_MSRS
; i
++)
1120 wrmsrl(host_save_user_msrs
[i
], svm
->host_user_msrs
[i
]);
1123 static unsigned long svm_get_rflags(struct kvm_vcpu
*vcpu
)
1125 return to_svm(vcpu
)->vmcb
->save
.rflags
;
1128 static void svm_set_rflags(struct kvm_vcpu
*vcpu
, unsigned long rflags
)
1130 to_svm(vcpu
)->vmcb
->save
.rflags
= rflags
;
1133 static void svm_cache_reg(struct kvm_vcpu
*vcpu
, enum kvm_reg reg
)
1136 case VCPU_EXREG_PDPTR
:
1137 BUG_ON(!npt_enabled
);
1138 load_pdptrs(vcpu
, vcpu
->arch
.walk_mmu
, vcpu
->arch
.cr3
);
1145 static void svm_set_vintr(struct vcpu_svm
*svm
)
1147 set_intercept(svm
, INTERCEPT_VINTR
);
1150 static void svm_clear_vintr(struct vcpu_svm
*svm
)
1152 clr_intercept(svm
, INTERCEPT_VINTR
);
1155 static struct vmcb_seg
*svm_seg(struct kvm_vcpu
*vcpu
, int seg
)
1157 struct vmcb_save_area
*save
= &to_svm(vcpu
)->vmcb
->save
;
1160 case VCPU_SREG_CS
: return &save
->cs
;
1161 case VCPU_SREG_DS
: return &save
->ds
;
1162 case VCPU_SREG_ES
: return &save
->es
;
1163 case VCPU_SREG_FS
: return &save
->fs
;
1164 case VCPU_SREG_GS
: return &save
->gs
;
1165 case VCPU_SREG_SS
: return &save
->ss
;
1166 case VCPU_SREG_TR
: return &save
->tr
;
1167 case VCPU_SREG_LDTR
: return &save
->ldtr
;
1173 static u64
svm_get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
1175 struct vmcb_seg
*s
= svm_seg(vcpu
, seg
);
1180 static void svm_get_segment(struct kvm_vcpu
*vcpu
,
1181 struct kvm_segment
*var
, int seg
)
1183 struct vmcb_seg
*s
= svm_seg(vcpu
, seg
);
1185 var
->base
= s
->base
;
1186 var
->limit
= s
->limit
;
1187 var
->selector
= s
->selector
;
1188 var
->type
= s
->attrib
& SVM_SELECTOR_TYPE_MASK
;
1189 var
->s
= (s
->attrib
>> SVM_SELECTOR_S_SHIFT
) & 1;
1190 var
->dpl
= (s
->attrib
>> SVM_SELECTOR_DPL_SHIFT
) & 3;
1191 var
->present
= (s
->attrib
>> SVM_SELECTOR_P_SHIFT
) & 1;
1192 var
->avl
= (s
->attrib
>> SVM_SELECTOR_AVL_SHIFT
) & 1;
1193 var
->l
= (s
->attrib
>> SVM_SELECTOR_L_SHIFT
) & 1;
1194 var
->db
= (s
->attrib
>> SVM_SELECTOR_DB_SHIFT
) & 1;
1195 var
->g
= (s
->attrib
>> SVM_SELECTOR_G_SHIFT
) & 1;
1198 * AMD's VMCB does not have an explicit unusable field, so emulate it
1199 * for cross vendor migration purposes by "not present"
1201 var
->unusable
= !var
->present
|| (var
->type
== 0);
1206 * SVM always stores 0 for the 'G' bit in the CS selector in
1207 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1208 * Intel's VMENTRY has a check on the 'G' bit.
1210 var
->g
= s
->limit
> 0xfffff;
1214 * Work around a bug where the busy flag in the tr selector
1224 * The accessed bit must always be set in the segment
1225 * descriptor cache, although it can be cleared in the
1226 * descriptor, the cached bit always remains at 1. Since
1227 * Intel has a check on this, set it here to support
1228 * cross-vendor migration.
1235 * On AMD CPUs sometimes the DB bit in the segment
1236 * descriptor is left as 1, although the whole segment has
1237 * been made unusable. Clear it here to pass an Intel VMX
1238 * entry check when cross vendor migrating.
1246 static int svm_get_cpl(struct kvm_vcpu
*vcpu
)
1248 struct vmcb_save_area
*save
= &to_svm(vcpu
)->vmcb
->save
;
1253 static void svm_get_idt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
1255 struct vcpu_svm
*svm
= to_svm(vcpu
);
1257 dt
->size
= svm
->vmcb
->save
.idtr
.limit
;
1258 dt
->address
= svm
->vmcb
->save
.idtr
.base
;
1261 static void svm_set_idt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
1263 struct vcpu_svm
*svm
= to_svm(vcpu
);
1265 svm
->vmcb
->save
.idtr
.limit
= dt
->size
;
1266 svm
->vmcb
->save
.idtr
.base
= dt
->address
;
1269 static void svm_get_gdt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
1271 struct vcpu_svm
*svm
= to_svm(vcpu
);
1273 dt
->size
= svm
->vmcb
->save
.gdtr
.limit
;
1274 dt
->address
= svm
->vmcb
->save
.gdtr
.base
;
1277 static void svm_set_gdt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
1279 struct vcpu_svm
*svm
= to_svm(vcpu
);
1281 svm
->vmcb
->save
.gdtr
.limit
= dt
->size
;
1282 svm
->vmcb
->save
.gdtr
.base
= dt
->address
;
1285 static void svm_decache_cr0_guest_bits(struct kvm_vcpu
*vcpu
)
1289 static void svm_decache_cr4_guest_bits(struct kvm_vcpu
*vcpu
)
1293 static void update_cr0_intercept(struct vcpu_svm
*svm
)
1295 ulong gcr0
= svm
->vcpu
.arch
.cr0
;
1296 u64
*hcr0
= &svm
->vmcb
->save
.cr0
;
1298 if (!svm
->vcpu
.fpu_active
)
1299 *hcr0
|= SVM_CR0_SELECTIVE_MASK
;
1301 *hcr0
= (*hcr0
& ~SVM_CR0_SELECTIVE_MASK
)
1302 | (gcr0
& SVM_CR0_SELECTIVE_MASK
);
1305 if (gcr0
== *hcr0
&& svm
->vcpu
.fpu_active
) {
1306 clr_cr_intercept(svm
, INTERCEPT_CR0_READ
);
1307 clr_cr_intercept(svm
, INTERCEPT_CR0_WRITE
);
1309 set_cr_intercept(svm
, INTERCEPT_CR0_READ
);
1310 set_cr_intercept(svm
, INTERCEPT_CR0_WRITE
);
1314 static void svm_set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
1316 struct vcpu_svm
*svm
= to_svm(vcpu
);
1318 if (is_guest_mode(vcpu
)) {
1320 * We are here because we run in nested mode, the host kvm
1321 * intercepts cr0 writes but the l1 hypervisor does not.
1322 * But the L1 hypervisor may intercept selective cr0 writes.
1323 * This needs to be checked here.
1325 unsigned long old
, new;
1327 /* Remove bits that would trigger a real cr0 write intercept */
1328 old
= vcpu
->arch
.cr0
& SVM_CR0_SELECTIVE_MASK
;
1329 new = cr0
& SVM_CR0_SELECTIVE_MASK
;
1332 /* cr0 write with ts and mp unchanged */
1333 svm
->vmcb
->control
.exit_code
= SVM_EXIT_CR0_SEL_WRITE
;
1334 if (nested_svm_exit_handled(svm
) == NESTED_EXIT_DONE
) {
1335 svm
->nested
.vmexit_rip
= kvm_rip_read(vcpu
);
1336 svm
->nested
.vmexit_rsp
= kvm_register_read(vcpu
, VCPU_REGS_RSP
);
1337 svm
->nested
.vmexit_rax
= kvm_register_read(vcpu
, VCPU_REGS_RAX
);
1343 #ifdef CONFIG_X86_64
1344 if (vcpu
->arch
.efer
& EFER_LME
) {
1345 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
)) {
1346 vcpu
->arch
.efer
|= EFER_LMA
;
1347 svm
->vmcb
->save
.efer
|= EFER_LMA
| EFER_LME
;
1350 if (is_paging(vcpu
) && !(cr0
& X86_CR0_PG
)) {
1351 vcpu
->arch
.efer
&= ~EFER_LMA
;
1352 svm
->vmcb
->save
.efer
&= ~(EFER_LMA
| EFER_LME
);
1356 vcpu
->arch
.cr0
= cr0
;
1359 cr0
|= X86_CR0_PG
| X86_CR0_WP
;
1361 if (!vcpu
->fpu_active
)
1364 * re-enable caching here because the QEMU bios
1365 * does not do it - this results in some delay at
1368 cr0
&= ~(X86_CR0_CD
| X86_CR0_NW
);
1369 svm
->vmcb
->save
.cr0
= cr0
;
1370 update_cr0_intercept(svm
);
1373 static void svm_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
1375 unsigned long host_cr4_mce
= read_cr4() & X86_CR4_MCE
;
1376 unsigned long old_cr4
= to_svm(vcpu
)->vmcb
->save
.cr4
;
1378 if (npt_enabled
&& ((old_cr4
^ cr4
) & X86_CR4_PGE
))
1379 force_new_asid(vcpu
);
1381 vcpu
->arch
.cr4
= cr4
;
1384 cr4
|= host_cr4_mce
;
1385 to_svm(vcpu
)->vmcb
->save
.cr4
= cr4
;
1388 static void svm_set_segment(struct kvm_vcpu
*vcpu
,
1389 struct kvm_segment
*var
, int seg
)
1391 struct vcpu_svm
*svm
= to_svm(vcpu
);
1392 struct vmcb_seg
*s
= svm_seg(vcpu
, seg
);
1394 s
->base
= var
->base
;
1395 s
->limit
= var
->limit
;
1396 s
->selector
= var
->selector
;
1400 s
->attrib
= (var
->type
& SVM_SELECTOR_TYPE_MASK
);
1401 s
->attrib
|= (var
->s
& 1) << SVM_SELECTOR_S_SHIFT
;
1402 s
->attrib
|= (var
->dpl
& 3) << SVM_SELECTOR_DPL_SHIFT
;
1403 s
->attrib
|= (var
->present
& 1) << SVM_SELECTOR_P_SHIFT
;
1404 s
->attrib
|= (var
->avl
& 1) << SVM_SELECTOR_AVL_SHIFT
;
1405 s
->attrib
|= (var
->l
& 1) << SVM_SELECTOR_L_SHIFT
;
1406 s
->attrib
|= (var
->db
& 1) << SVM_SELECTOR_DB_SHIFT
;
1407 s
->attrib
|= (var
->g
& 1) << SVM_SELECTOR_G_SHIFT
;
1409 if (seg
== VCPU_SREG_CS
)
1411 = (svm
->vmcb
->save
.cs
.attrib
1412 >> SVM_SELECTOR_DPL_SHIFT
) & 3;
1416 static void update_db_intercept(struct kvm_vcpu
*vcpu
)
1418 struct vcpu_svm
*svm
= to_svm(vcpu
);
1420 clr_exception_intercept(svm
, DB_VECTOR
);
1421 clr_exception_intercept(svm
, BP_VECTOR
);
1423 if (svm
->nmi_singlestep
)
1424 set_exception_intercept(svm
, DB_VECTOR
);
1426 if (vcpu
->guest_debug
& KVM_GUESTDBG_ENABLE
) {
1427 if (vcpu
->guest_debug
&
1428 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
))
1429 set_exception_intercept(svm
, DB_VECTOR
);
1430 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_SW_BP
)
1431 set_exception_intercept(svm
, BP_VECTOR
);
1433 vcpu
->guest_debug
= 0;
1436 static void svm_guest_debug(struct kvm_vcpu
*vcpu
, struct kvm_guest_debug
*dbg
)
1438 struct vcpu_svm
*svm
= to_svm(vcpu
);
1440 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
)
1441 svm
->vmcb
->save
.dr7
= dbg
->arch
.debugreg
[7];
1443 svm
->vmcb
->save
.dr7
= vcpu
->arch
.dr7
;
1445 update_db_intercept(vcpu
);
1448 static void new_asid(struct vcpu_svm
*svm
, struct svm_cpu_data
*sd
)
1450 if (sd
->next_asid
> sd
->max_asid
) {
1451 ++sd
->asid_generation
;
1453 svm
->vmcb
->control
.tlb_ctl
= TLB_CONTROL_FLUSH_ALL_ASID
;
1456 svm
->asid_generation
= sd
->asid_generation
;
1457 svm
->vmcb
->control
.asid
= sd
->next_asid
++;
1460 static void svm_set_dr7(struct kvm_vcpu
*vcpu
, unsigned long value
)
1462 struct vcpu_svm
*svm
= to_svm(vcpu
);
1464 svm
->vmcb
->save
.dr7
= value
;
1467 static int pf_interception(struct vcpu_svm
*svm
)
1469 u64 fault_address
= svm
->vmcb
->control
.exit_info_2
;
1473 switch (svm
->apf_reason
) {
1475 error_code
= svm
->vmcb
->control
.exit_info_1
;
1477 trace_kvm_page_fault(fault_address
, error_code
);
1478 if (!npt_enabled
&& kvm_event_needs_reinjection(&svm
->vcpu
))
1479 kvm_mmu_unprotect_page_virt(&svm
->vcpu
, fault_address
);
1480 r
= kvm_mmu_page_fault(&svm
->vcpu
, fault_address
, error_code
);
1482 case KVM_PV_REASON_PAGE_NOT_PRESENT
:
1483 svm
->apf_reason
= 0;
1484 local_irq_disable();
1485 kvm_async_pf_task_wait(fault_address
);
1488 case KVM_PV_REASON_PAGE_READY
:
1489 svm
->apf_reason
= 0;
1490 local_irq_disable();
1491 kvm_async_pf_task_wake(fault_address
);
1498 static int db_interception(struct vcpu_svm
*svm
)
1500 struct kvm_run
*kvm_run
= svm
->vcpu
.run
;
1502 if (!(svm
->vcpu
.guest_debug
&
1503 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
)) &&
1504 !svm
->nmi_singlestep
) {
1505 kvm_queue_exception(&svm
->vcpu
, DB_VECTOR
);
1509 if (svm
->nmi_singlestep
) {
1510 svm
->nmi_singlestep
= false;
1511 if (!(svm
->vcpu
.guest_debug
& KVM_GUESTDBG_SINGLESTEP
))
1512 svm
->vmcb
->save
.rflags
&=
1513 ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
1514 update_db_intercept(&svm
->vcpu
);
1517 if (svm
->vcpu
.guest_debug
&
1518 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
)) {
1519 kvm_run
->exit_reason
= KVM_EXIT_DEBUG
;
1520 kvm_run
->debug
.arch
.pc
=
1521 svm
->vmcb
->save
.cs
.base
+ svm
->vmcb
->save
.rip
;
1522 kvm_run
->debug
.arch
.exception
= DB_VECTOR
;
1529 static int bp_interception(struct vcpu_svm
*svm
)
1531 struct kvm_run
*kvm_run
= svm
->vcpu
.run
;
1533 kvm_run
->exit_reason
= KVM_EXIT_DEBUG
;
1534 kvm_run
->debug
.arch
.pc
= svm
->vmcb
->save
.cs
.base
+ svm
->vmcb
->save
.rip
;
1535 kvm_run
->debug
.arch
.exception
= BP_VECTOR
;
1539 static int ud_interception(struct vcpu_svm
*svm
)
1543 er
= emulate_instruction(&svm
->vcpu
, 0, 0, EMULTYPE_TRAP_UD
);
1544 if (er
!= EMULATE_DONE
)
1545 kvm_queue_exception(&svm
->vcpu
, UD_VECTOR
);
1549 static void svm_fpu_activate(struct kvm_vcpu
*vcpu
)
1551 struct vcpu_svm
*svm
= to_svm(vcpu
);
1553 clr_exception_intercept(svm
, NM_VECTOR
);
1555 svm
->vcpu
.fpu_active
= 1;
1556 update_cr0_intercept(svm
);
1559 static int nm_interception(struct vcpu_svm
*svm
)
1561 svm_fpu_activate(&svm
->vcpu
);
1565 static bool is_erratum_383(void)
1570 if (!erratum_383_found
)
1573 value
= native_read_msr_safe(MSR_IA32_MC0_STATUS
, &err
);
1577 /* Bit 62 may or may not be set for this mce */
1578 value
&= ~(1ULL << 62);
1580 if (value
!= 0xb600000000010015ULL
)
1583 /* Clear MCi_STATUS registers */
1584 for (i
= 0; i
< 6; ++i
)
1585 native_write_msr_safe(MSR_IA32_MCx_STATUS(i
), 0, 0);
1587 value
= native_read_msr_safe(MSR_IA32_MCG_STATUS
, &err
);
1591 value
&= ~(1ULL << 2);
1592 low
= lower_32_bits(value
);
1593 high
= upper_32_bits(value
);
1595 native_write_msr_safe(MSR_IA32_MCG_STATUS
, low
, high
);
1598 /* Flush tlb to evict multi-match entries */
1604 static void svm_handle_mce(struct vcpu_svm
*svm
)
1606 if (is_erratum_383()) {
1608 * Erratum 383 triggered. Guest state is corrupt so kill the
1611 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1613 kvm_make_request(KVM_REQ_TRIPLE_FAULT
, &svm
->vcpu
);
1619 * On an #MC intercept the MCE handler is not called automatically in
1620 * the host. So do it by hand here.
1624 /* not sure if we ever come back to this point */
1629 static int mc_interception(struct vcpu_svm
*svm
)
1634 static int shutdown_interception(struct vcpu_svm
*svm
)
1636 struct kvm_run
*kvm_run
= svm
->vcpu
.run
;
1639 * VMCB is undefined after a SHUTDOWN intercept
1640 * so reinitialize it.
1642 clear_page(svm
->vmcb
);
1645 kvm_run
->exit_reason
= KVM_EXIT_SHUTDOWN
;
1649 static int io_interception(struct vcpu_svm
*svm
)
1651 struct kvm_vcpu
*vcpu
= &svm
->vcpu
;
1652 u32 io_info
= svm
->vmcb
->control
.exit_info_1
; /* address size bug? */
1653 int size
, in
, string
;
1656 ++svm
->vcpu
.stat
.io_exits
;
1657 string
= (io_info
& SVM_IOIO_STR_MASK
) != 0;
1658 in
= (io_info
& SVM_IOIO_TYPE_MASK
) != 0;
1660 return emulate_instruction(vcpu
, 0, 0, 0) == EMULATE_DONE
;
1662 port
= io_info
>> 16;
1663 size
= (io_info
& SVM_IOIO_SIZE_MASK
) >> SVM_IOIO_SIZE_SHIFT
;
1664 svm
->next_rip
= svm
->vmcb
->control
.exit_info_2
;
1665 skip_emulated_instruction(&svm
->vcpu
);
1667 return kvm_fast_pio_out(vcpu
, size
, port
);
1670 static int nmi_interception(struct vcpu_svm
*svm
)
1675 static int intr_interception(struct vcpu_svm
*svm
)
1677 ++svm
->vcpu
.stat
.irq_exits
;
1681 static int nop_on_interception(struct vcpu_svm
*svm
)
1686 static int halt_interception(struct vcpu_svm
*svm
)
1688 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 1;
1689 skip_emulated_instruction(&svm
->vcpu
);
1690 return kvm_emulate_halt(&svm
->vcpu
);
1693 static int vmmcall_interception(struct vcpu_svm
*svm
)
1695 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
1696 skip_emulated_instruction(&svm
->vcpu
);
1697 kvm_emulate_hypercall(&svm
->vcpu
);
1701 static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu
*vcpu
)
1703 struct vcpu_svm
*svm
= to_svm(vcpu
);
1705 return svm
->nested
.nested_cr3
;
1708 static void nested_svm_set_tdp_cr3(struct kvm_vcpu
*vcpu
,
1711 struct vcpu_svm
*svm
= to_svm(vcpu
);
1713 svm
->vmcb
->control
.nested_cr3
= root
;
1714 force_new_asid(vcpu
);
1717 static void nested_svm_inject_npf_exit(struct kvm_vcpu
*vcpu
,
1718 struct x86_exception
*fault
)
1720 struct vcpu_svm
*svm
= to_svm(vcpu
);
1722 svm
->vmcb
->control
.exit_code
= SVM_EXIT_NPF
;
1723 svm
->vmcb
->control
.exit_code_hi
= 0;
1724 svm
->vmcb
->control
.exit_info_1
= fault
->error_code
;
1725 svm
->vmcb
->control
.exit_info_2
= fault
->address
;
1727 nested_svm_vmexit(svm
);
1730 static int nested_svm_init_mmu_context(struct kvm_vcpu
*vcpu
)
1734 r
= kvm_init_shadow_mmu(vcpu
, &vcpu
->arch
.mmu
);
1736 vcpu
->arch
.mmu
.set_cr3
= nested_svm_set_tdp_cr3
;
1737 vcpu
->arch
.mmu
.get_cr3
= nested_svm_get_tdp_cr3
;
1738 vcpu
->arch
.mmu
.inject_page_fault
= nested_svm_inject_npf_exit
;
1739 vcpu
->arch
.mmu
.shadow_root_level
= get_npt_level();
1740 vcpu
->arch
.walk_mmu
= &vcpu
->arch
.nested_mmu
;
1745 static void nested_svm_uninit_mmu_context(struct kvm_vcpu
*vcpu
)
1747 vcpu
->arch
.walk_mmu
= &vcpu
->arch
.mmu
;
1750 static int nested_svm_check_permissions(struct vcpu_svm
*svm
)
1752 if (!(svm
->vcpu
.arch
.efer
& EFER_SVME
)
1753 || !is_paging(&svm
->vcpu
)) {
1754 kvm_queue_exception(&svm
->vcpu
, UD_VECTOR
);
1758 if (svm
->vmcb
->save
.cpl
) {
1759 kvm_inject_gp(&svm
->vcpu
, 0);
1766 static int nested_svm_check_exception(struct vcpu_svm
*svm
, unsigned nr
,
1767 bool has_error_code
, u32 error_code
)
1771 if (!is_guest_mode(&svm
->vcpu
))
1774 svm
->vmcb
->control
.exit_code
= SVM_EXIT_EXCP_BASE
+ nr
;
1775 svm
->vmcb
->control
.exit_code_hi
= 0;
1776 svm
->vmcb
->control
.exit_info_1
= error_code
;
1777 svm
->vmcb
->control
.exit_info_2
= svm
->vcpu
.arch
.cr2
;
1779 vmexit
= nested_svm_intercept(svm
);
1780 if (vmexit
== NESTED_EXIT_DONE
)
1781 svm
->nested
.exit_required
= true;
1786 /* This function returns true if it is save to enable the irq window */
1787 static inline bool nested_svm_intr(struct vcpu_svm
*svm
)
1789 if (!is_guest_mode(&svm
->vcpu
))
1792 if (!(svm
->vcpu
.arch
.hflags
& HF_VINTR_MASK
))
1795 if (!(svm
->vcpu
.arch
.hflags
& HF_HIF_MASK
))
1799 * if vmexit was already requested (by intercepted exception
1800 * for instance) do not overwrite it with "external interrupt"
1803 if (svm
->nested
.exit_required
)
1806 svm
->vmcb
->control
.exit_code
= SVM_EXIT_INTR
;
1807 svm
->vmcb
->control
.exit_info_1
= 0;
1808 svm
->vmcb
->control
.exit_info_2
= 0;
1810 if (svm
->nested
.intercept
& 1ULL) {
1812 * The #vmexit can't be emulated here directly because this
1813 * code path runs with irqs and preemtion disabled. A
1814 * #vmexit emulation might sleep. Only signal request for
1817 svm
->nested
.exit_required
= true;
1818 trace_kvm_nested_intr_vmexit(svm
->vmcb
->save
.rip
);
1825 /* This function returns true if it is save to enable the nmi window */
1826 static inline bool nested_svm_nmi(struct vcpu_svm
*svm
)
1828 if (!is_guest_mode(&svm
->vcpu
))
1831 if (!(svm
->nested
.intercept
& (1ULL << INTERCEPT_NMI
)))
1834 svm
->vmcb
->control
.exit_code
= SVM_EXIT_NMI
;
1835 svm
->nested
.exit_required
= true;
1840 static void *nested_svm_map(struct vcpu_svm
*svm
, u64 gpa
, struct page
**_page
)
1846 page
= gfn_to_page(svm
->vcpu
.kvm
, gpa
>> PAGE_SHIFT
);
1847 if (is_error_page(page
))
1855 kvm_release_page_clean(page
);
1856 kvm_inject_gp(&svm
->vcpu
, 0);
1861 static void nested_svm_unmap(struct page
*page
)
1864 kvm_release_page_dirty(page
);
1867 static int nested_svm_intercept_ioio(struct vcpu_svm
*svm
)
1873 if (!(svm
->nested
.intercept
& (1ULL << INTERCEPT_IOIO_PROT
)))
1874 return NESTED_EXIT_HOST
;
1876 port
= svm
->vmcb
->control
.exit_info_1
>> 16;
1877 gpa
= svm
->nested
.vmcb_iopm
+ (port
/ 8);
1881 if (kvm_read_guest(svm
->vcpu
.kvm
, gpa
, &val
, 1))
1884 return val
? NESTED_EXIT_DONE
: NESTED_EXIT_HOST
;
1887 static int nested_svm_exit_handled_msr(struct vcpu_svm
*svm
)
1889 u32 offset
, msr
, value
;
1892 if (!(svm
->nested
.intercept
& (1ULL << INTERCEPT_MSR_PROT
)))
1893 return NESTED_EXIT_HOST
;
1895 msr
= svm
->vcpu
.arch
.regs
[VCPU_REGS_RCX
];
1896 offset
= svm_msrpm_offset(msr
);
1897 write
= svm
->vmcb
->control
.exit_info_1
& 1;
1898 mask
= 1 << ((2 * (msr
& 0xf)) + write
);
1900 if (offset
== MSR_INVALID
)
1901 return NESTED_EXIT_DONE
;
1903 /* Offset is in 32 bit units but need in 8 bit units */
1906 if (kvm_read_guest(svm
->vcpu
.kvm
, svm
->nested
.vmcb_msrpm
+ offset
, &value
, 4))
1907 return NESTED_EXIT_DONE
;
1909 return (value
& mask
) ? NESTED_EXIT_DONE
: NESTED_EXIT_HOST
;
1912 static int nested_svm_exit_special(struct vcpu_svm
*svm
)
1914 u32 exit_code
= svm
->vmcb
->control
.exit_code
;
1916 switch (exit_code
) {
1919 case SVM_EXIT_EXCP_BASE
+ MC_VECTOR
:
1920 return NESTED_EXIT_HOST
;
1922 /* For now we are always handling NPFs when using them */
1924 return NESTED_EXIT_HOST
;
1926 case SVM_EXIT_EXCP_BASE
+ PF_VECTOR
:
1927 /* When we're shadowing, trap PFs, but not async PF */
1928 if (!npt_enabled
&& svm
->apf_reason
== 0)
1929 return NESTED_EXIT_HOST
;
1931 case SVM_EXIT_EXCP_BASE
+ NM_VECTOR
:
1932 nm_interception(svm
);
1938 return NESTED_EXIT_CONTINUE
;
1942 * If this function returns true, this #vmexit was already handled
1944 static int nested_svm_intercept(struct vcpu_svm
*svm
)
1946 u32 exit_code
= svm
->vmcb
->control
.exit_code
;
1947 int vmexit
= NESTED_EXIT_HOST
;
1949 switch (exit_code
) {
1951 vmexit
= nested_svm_exit_handled_msr(svm
);
1954 vmexit
= nested_svm_intercept_ioio(svm
);
1956 case SVM_EXIT_READ_CR0
... SVM_EXIT_WRITE_CR8
: {
1957 u32 bit
= 1U << (exit_code
- SVM_EXIT_READ_CR0
);
1958 if (svm
->nested
.intercept_cr
& bit
)
1959 vmexit
= NESTED_EXIT_DONE
;
1962 case SVM_EXIT_READ_DR0
... SVM_EXIT_WRITE_DR7
: {
1963 u32 bit
= 1U << (exit_code
- SVM_EXIT_READ_DR0
);
1964 if (svm
->nested
.intercept_dr
& bit
)
1965 vmexit
= NESTED_EXIT_DONE
;
1968 case SVM_EXIT_EXCP_BASE
... SVM_EXIT_EXCP_BASE
+ 0x1f: {
1969 u32 excp_bits
= 1 << (exit_code
- SVM_EXIT_EXCP_BASE
);
1970 if (svm
->nested
.intercept_exceptions
& excp_bits
)
1971 vmexit
= NESTED_EXIT_DONE
;
1972 /* async page fault always cause vmexit */
1973 else if ((exit_code
== SVM_EXIT_EXCP_BASE
+ PF_VECTOR
) &&
1974 svm
->apf_reason
!= 0)
1975 vmexit
= NESTED_EXIT_DONE
;
1978 case SVM_EXIT_ERR
: {
1979 vmexit
= NESTED_EXIT_DONE
;
1983 u64 exit_bits
= 1ULL << (exit_code
- SVM_EXIT_INTR
);
1984 if (svm
->nested
.intercept
& exit_bits
)
1985 vmexit
= NESTED_EXIT_DONE
;
1992 static int nested_svm_exit_handled(struct vcpu_svm
*svm
)
1996 vmexit
= nested_svm_intercept(svm
);
1998 if (vmexit
== NESTED_EXIT_DONE
)
1999 nested_svm_vmexit(svm
);
2004 static inline void copy_vmcb_control_area(struct vmcb
*dst_vmcb
, struct vmcb
*from_vmcb
)
2006 struct vmcb_control_area
*dst
= &dst_vmcb
->control
;
2007 struct vmcb_control_area
*from
= &from_vmcb
->control
;
2009 dst
->intercept_cr
= from
->intercept_cr
;
2010 dst
->intercept_dr
= from
->intercept_dr
;
2011 dst
->intercept_exceptions
= from
->intercept_exceptions
;
2012 dst
->intercept
= from
->intercept
;
2013 dst
->iopm_base_pa
= from
->iopm_base_pa
;
2014 dst
->msrpm_base_pa
= from
->msrpm_base_pa
;
2015 dst
->tsc_offset
= from
->tsc_offset
;
2016 dst
->asid
= from
->asid
;
2017 dst
->tlb_ctl
= from
->tlb_ctl
;
2018 dst
->int_ctl
= from
->int_ctl
;
2019 dst
->int_vector
= from
->int_vector
;
2020 dst
->int_state
= from
->int_state
;
2021 dst
->exit_code
= from
->exit_code
;
2022 dst
->exit_code_hi
= from
->exit_code_hi
;
2023 dst
->exit_info_1
= from
->exit_info_1
;
2024 dst
->exit_info_2
= from
->exit_info_2
;
2025 dst
->exit_int_info
= from
->exit_int_info
;
2026 dst
->exit_int_info_err
= from
->exit_int_info_err
;
2027 dst
->nested_ctl
= from
->nested_ctl
;
2028 dst
->event_inj
= from
->event_inj
;
2029 dst
->event_inj_err
= from
->event_inj_err
;
2030 dst
->nested_cr3
= from
->nested_cr3
;
2031 dst
->lbr_ctl
= from
->lbr_ctl
;
2034 static int nested_svm_vmexit(struct vcpu_svm
*svm
)
2036 struct vmcb
*nested_vmcb
;
2037 struct vmcb
*hsave
= svm
->nested
.hsave
;
2038 struct vmcb
*vmcb
= svm
->vmcb
;
2041 trace_kvm_nested_vmexit_inject(vmcb
->control
.exit_code
,
2042 vmcb
->control
.exit_info_1
,
2043 vmcb
->control
.exit_info_2
,
2044 vmcb
->control
.exit_int_info
,
2045 vmcb
->control
.exit_int_info_err
);
2047 nested_vmcb
= nested_svm_map(svm
, svm
->nested
.vmcb
, &page
);
2051 /* Exit Guest-Mode */
2052 leave_guest_mode(&svm
->vcpu
);
2053 svm
->nested
.vmcb
= 0;
2055 /* Give the current vmcb to the guest */
2058 nested_vmcb
->save
.es
= vmcb
->save
.es
;
2059 nested_vmcb
->save
.cs
= vmcb
->save
.cs
;
2060 nested_vmcb
->save
.ss
= vmcb
->save
.ss
;
2061 nested_vmcb
->save
.ds
= vmcb
->save
.ds
;
2062 nested_vmcb
->save
.gdtr
= vmcb
->save
.gdtr
;
2063 nested_vmcb
->save
.idtr
= vmcb
->save
.idtr
;
2064 nested_vmcb
->save
.efer
= svm
->vcpu
.arch
.efer
;
2065 nested_vmcb
->save
.cr0
= kvm_read_cr0(&svm
->vcpu
);
2066 nested_vmcb
->save
.cr3
= svm
->vcpu
.arch
.cr3
;
2067 nested_vmcb
->save
.cr2
= vmcb
->save
.cr2
;
2068 nested_vmcb
->save
.cr4
= svm
->vcpu
.arch
.cr4
;
2069 nested_vmcb
->save
.rflags
= vmcb
->save
.rflags
;
2070 nested_vmcb
->save
.rip
= vmcb
->save
.rip
;
2071 nested_vmcb
->save
.rsp
= vmcb
->save
.rsp
;
2072 nested_vmcb
->save
.rax
= vmcb
->save
.rax
;
2073 nested_vmcb
->save
.dr7
= vmcb
->save
.dr7
;
2074 nested_vmcb
->save
.dr6
= vmcb
->save
.dr6
;
2075 nested_vmcb
->save
.cpl
= vmcb
->save
.cpl
;
2077 nested_vmcb
->control
.int_ctl
= vmcb
->control
.int_ctl
;
2078 nested_vmcb
->control
.int_vector
= vmcb
->control
.int_vector
;
2079 nested_vmcb
->control
.int_state
= vmcb
->control
.int_state
;
2080 nested_vmcb
->control
.exit_code
= vmcb
->control
.exit_code
;
2081 nested_vmcb
->control
.exit_code_hi
= vmcb
->control
.exit_code_hi
;
2082 nested_vmcb
->control
.exit_info_1
= vmcb
->control
.exit_info_1
;
2083 nested_vmcb
->control
.exit_info_2
= vmcb
->control
.exit_info_2
;
2084 nested_vmcb
->control
.exit_int_info
= vmcb
->control
.exit_int_info
;
2085 nested_vmcb
->control
.exit_int_info_err
= vmcb
->control
.exit_int_info_err
;
2086 nested_vmcb
->control
.next_rip
= vmcb
->control
.next_rip
;
2089 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2090 * to make sure that we do not lose injected events. So check event_inj
2091 * here and copy it to exit_int_info if it is valid.
2092 * Exit_int_info and event_inj can't be both valid because the case
2093 * below only happens on a VMRUN instruction intercept which has
2094 * no valid exit_int_info set.
2096 if (vmcb
->control
.event_inj
& SVM_EVTINJ_VALID
) {
2097 struct vmcb_control_area
*nc
= &nested_vmcb
->control
;
2099 nc
->exit_int_info
= vmcb
->control
.event_inj
;
2100 nc
->exit_int_info_err
= vmcb
->control
.event_inj_err
;
2103 nested_vmcb
->control
.tlb_ctl
= 0;
2104 nested_vmcb
->control
.event_inj
= 0;
2105 nested_vmcb
->control
.event_inj_err
= 0;
2107 /* We always set V_INTR_MASKING and remember the old value in hflags */
2108 if (!(svm
->vcpu
.arch
.hflags
& HF_VINTR_MASK
))
2109 nested_vmcb
->control
.int_ctl
&= ~V_INTR_MASKING_MASK
;
2111 /* Restore the original control entries */
2112 copy_vmcb_control_area(vmcb
, hsave
);
2114 kvm_clear_exception_queue(&svm
->vcpu
);
2115 kvm_clear_interrupt_queue(&svm
->vcpu
);
2117 svm
->nested
.nested_cr3
= 0;
2119 /* Restore selected save entries */
2120 svm
->vmcb
->save
.es
= hsave
->save
.es
;
2121 svm
->vmcb
->save
.cs
= hsave
->save
.cs
;
2122 svm
->vmcb
->save
.ss
= hsave
->save
.ss
;
2123 svm
->vmcb
->save
.ds
= hsave
->save
.ds
;
2124 svm
->vmcb
->save
.gdtr
= hsave
->save
.gdtr
;
2125 svm
->vmcb
->save
.idtr
= hsave
->save
.idtr
;
2126 svm
->vmcb
->save
.rflags
= hsave
->save
.rflags
;
2127 svm_set_efer(&svm
->vcpu
, hsave
->save
.efer
);
2128 svm_set_cr0(&svm
->vcpu
, hsave
->save
.cr0
| X86_CR0_PE
);
2129 svm_set_cr4(&svm
->vcpu
, hsave
->save
.cr4
);
2131 svm
->vmcb
->save
.cr3
= hsave
->save
.cr3
;
2132 svm
->vcpu
.arch
.cr3
= hsave
->save
.cr3
;
2134 (void)kvm_set_cr3(&svm
->vcpu
, hsave
->save
.cr3
);
2136 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RAX
, hsave
->save
.rax
);
2137 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RSP
, hsave
->save
.rsp
);
2138 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RIP
, hsave
->save
.rip
);
2139 svm
->vmcb
->save
.dr7
= 0;
2140 svm
->vmcb
->save
.cpl
= 0;
2141 svm
->vmcb
->control
.exit_int_info
= 0;
2143 nested_svm_unmap(page
);
2145 nested_svm_uninit_mmu_context(&svm
->vcpu
);
2146 kvm_mmu_reset_context(&svm
->vcpu
);
2147 kvm_mmu_load(&svm
->vcpu
);
2152 static bool nested_svm_vmrun_msrpm(struct vcpu_svm
*svm
)
2155 * This function merges the msr permission bitmaps of kvm and the
2156 * nested vmcb. It is omptimized in that it only merges the parts where
2157 * the kvm msr permission bitmap may contain zero bits
2161 if (!(svm
->nested
.intercept
& (1ULL << INTERCEPT_MSR_PROT
)))
2164 for (i
= 0; i
< MSRPM_OFFSETS
; i
++) {
2168 if (msrpm_offsets
[i
] == 0xffffffff)
2171 p
= msrpm_offsets
[i
];
2172 offset
= svm
->nested
.vmcb_msrpm
+ (p
* 4);
2174 if (kvm_read_guest(svm
->vcpu
.kvm
, offset
, &value
, 4))
2177 svm
->nested
.msrpm
[p
] = svm
->msrpm
[p
] | value
;
2180 svm
->vmcb
->control
.msrpm_base_pa
= __pa(svm
->nested
.msrpm
);
2185 static bool nested_vmcb_checks(struct vmcb
*vmcb
)
2187 if ((vmcb
->control
.intercept
& (1ULL << INTERCEPT_VMRUN
)) == 0)
2190 if (vmcb
->control
.asid
== 0)
2193 if (vmcb
->control
.nested_ctl
&& !npt_enabled
)
2199 static bool nested_svm_vmrun(struct vcpu_svm
*svm
)
2201 struct vmcb
*nested_vmcb
;
2202 struct vmcb
*hsave
= svm
->nested
.hsave
;
2203 struct vmcb
*vmcb
= svm
->vmcb
;
2207 vmcb_gpa
= svm
->vmcb
->save
.rax
;
2209 nested_vmcb
= nested_svm_map(svm
, svm
->vmcb
->save
.rax
, &page
);
2213 if (!nested_vmcb_checks(nested_vmcb
)) {
2214 nested_vmcb
->control
.exit_code
= SVM_EXIT_ERR
;
2215 nested_vmcb
->control
.exit_code_hi
= 0;
2216 nested_vmcb
->control
.exit_info_1
= 0;
2217 nested_vmcb
->control
.exit_info_2
= 0;
2219 nested_svm_unmap(page
);
2224 trace_kvm_nested_vmrun(svm
->vmcb
->save
.rip
, vmcb_gpa
,
2225 nested_vmcb
->save
.rip
,
2226 nested_vmcb
->control
.int_ctl
,
2227 nested_vmcb
->control
.event_inj
,
2228 nested_vmcb
->control
.nested_ctl
);
2230 trace_kvm_nested_intercepts(nested_vmcb
->control
.intercept_cr
& 0xffff,
2231 nested_vmcb
->control
.intercept_cr
>> 16,
2232 nested_vmcb
->control
.intercept_exceptions
,
2233 nested_vmcb
->control
.intercept
);
2235 /* Clear internal status */
2236 kvm_clear_exception_queue(&svm
->vcpu
);
2237 kvm_clear_interrupt_queue(&svm
->vcpu
);
2240 * Save the old vmcb, so we don't need to pick what we save, but can
2241 * restore everything when a VMEXIT occurs
2243 hsave
->save
.es
= vmcb
->save
.es
;
2244 hsave
->save
.cs
= vmcb
->save
.cs
;
2245 hsave
->save
.ss
= vmcb
->save
.ss
;
2246 hsave
->save
.ds
= vmcb
->save
.ds
;
2247 hsave
->save
.gdtr
= vmcb
->save
.gdtr
;
2248 hsave
->save
.idtr
= vmcb
->save
.idtr
;
2249 hsave
->save
.efer
= svm
->vcpu
.arch
.efer
;
2250 hsave
->save
.cr0
= kvm_read_cr0(&svm
->vcpu
);
2251 hsave
->save
.cr4
= svm
->vcpu
.arch
.cr4
;
2252 hsave
->save
.rflags
= vmcb
->save
.rflags
;
2253 hsave
->save
.rip
= kvm_rip_read(&svm
->vcpu
);
2254 hsave
->save
.rsp
= vmcb
->save
.rsp
;
2255 hsave
->save
.rax
= vmcb
->save
.rax
;
2257 hsave
->save
.cr3
= vmcb
->save
.cr3
;
2259 hsave
->save
.cr3
= svm
->vcpu
.arch
.cr3
;
2261 copy_vmcb_control_area(hsave
, vmcb
);
2263 if (svm
->vmcb
->save
.rflags
& X86_EFLAGS_IF
)
2264 svm
->vcpu
.arch
.hflags
|= HF_HIF_MASK
;
2266 svm
->vcpu
.arch
.hflags
&= ~HF_HIF_MASK
;
2268 if (nested_vmcb
->control
.nested_ctl
) {
2269 kvm_mmu_unload(&svm
->vcpu
);
2270 svm
->nested
.nested_cr3
= nested_vmcb
->control
.nested_cr3
;
2271 nested_svm_init_mmu_context(&svm
->vcpu
);
2274 /* Load the nested guest state */
2275 svm
->vmcb
->save
.es
= nested_vmcb
->save
.es
;
2276 svm
->vmcb
->save
.cs
= nested_vmcb
->save
.cs
;
2277 svm
->vmcb
->save
.ss
= nested_vmcb
->save
.ss
;
2278 svm
->vmcb
->save
.ds
= nested_vmcb
->save
.ds
;
2279 svm
->vmcb
->save
.gdtr
= nested_vmcb
->save
.gdtr
;
2280 svm
->vmcb
->save
.idtr
= nested_vmcb
->save
.idtr
;
2281 svm
->vmcb
->save
.rflags
= nested_vmcb
->save
.rflags
;
2282 svm_set_efer(&svm
->vcpu
, nested_vmcb
->save
.efer
);
2283 svm_set_cr0(&svm
->vcpu
, nested_vmcb
->save
.cr0
);
2284 svm_set_cr4(&svm
->vcpu
, nested_vmcb
->save
.cr4
);
2286 svm
->vmcb
->save
.cr3
= nested_vmcb
->save
.cr3
;
2287 svm
->vcpu
.arch
.cr3
= nested_vmcb
->save
.cr3
;
2289 (void)kvm_set_cr3(&svm
->vcpu
, nested_vmcb
->save
.cr3
);
2291 /* Guest paging mode is active - reset mmu */
2292 kvm_mmu_reset_context(&svm
->vcpu
);
2294 svm
->vmcb
->save
.cr2
= svm
->vcpu
.arch
.cr2
= nested_vmcb
->save
.cr2
;
2295 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RAX
, nested_vmcb
->save
.rax
);
2296 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RSP
, nested_vmcb
->save
.rsp
);
2297 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RIP
, nested_vmcb
->save
.rip
);
2299 /* In case we don't even reach vcpu_run, the fields are not updated */
2300 svm
->vmcb
->save
.rax
= nested_vmcb
->save
.rax
;
2301 svm
->vmcb
->save
.rsp
= nested_vmcb
->save
.rsp
;
2302 svm
->vmcb
->save
.rip
= nested_vmcb
->save
.rip
;
2303 svm
->vmcb
->save
.dr7
= nested_vmcb
->save
.dr7
;
2304 svm
->vmcb
->save
.dr6
= nested_vmcb
->save
.dr6
;
2305 svm
->vmcb
->save
.cpl
= nested_vmcb
->save
.cpl
;
2307 svm
->nested
.vmcb_msrpm
= nested_vmcb
->control
.msrpm_base_pa
& ~0x0fffULL
;
2308 svm
->nested
.vmcb_iopm
= nested_vmcb
->control
.iopm_base_pa
& ~0x0fffULL
;
2310 /* cache intercepts */
2311 svm
->nested
.intercept_cr
= nested_vmcb
->control
.intercept_cr
;
2312 svm
->nested
.intercept_dr
= nested_vmcb
->control
.intercept_dr
;
2313 svm
->nested
.intercept_exceptions
= nested_vmcb
->control
.intercept_exceptions
;
2314 svm
->nested
.intercept
= nested_vmcb
->control
.intercept
;
2316 force_new_asid(&svm
->vcpu
);
2317 svm
->vmcb
->control
.int_ctl
= nested_vmcb
->control
.int_ctl
| V_INTR_MASKING_MASK
;
2318 if (nested_vmcb
->control
.int_ctl
& V_INTR_MASKING_MASK
)
2319 svm
->vcpu
.arch
.hflags
|= HF_VINTR_MASK
;
2321 svm
->vcpu
.arch
.hflags
&= ~HF_VINTR_MASK
;
2323 if (svm
->vcpu
.arch
.hflags
& HF_VINTR_MASK
) {
2324 /* We only want the cr8 intercept bits of the guest */
2325 clr_cr_intercept(svm
, INTERCEPT_CR8_READ
);
2326 clr_cr_intercept(svm
, INTERCEPT_CR8_WRITE
);
2329 /* We don't want to see VMMCALLs from a nested guest */
2330 clr_intercept(svm
, INTERCEPT_VMMCALL
);
2332 svm
->vmcb
->control
.lbr_ctl
= nested_vmcb
->control
.lbr_ctl
;
2333 svm
->vmcb
->control
.int_vector
= nested_vmcb
->control
.int_vector
;
2334 svm
->vmcb
->control
.int_state
= nested_vmcb
->control
.int_state
;
2335 svm
->vmcb
->control
.tsc_offset
+= nested_vmcb
->control
.tsc_offset
;
2336 svm
->vmcb
->control
.event_inj
= nested_vmcb
->control
.event_inj
;
2337 svm
->vmcb
->control
.event_inj_err
= nested_vmcb
->control
.event_inj_err
;
2339 nested_svm_unmap(page
);
2341 /* Enter Guest-Mode */
2342 enter_guest_mode(&svm
->vcpu
);
2345 * Merge guest and host intercepts - must be called with vcpu in
2346 * guest-mode to take affect here
2348 recalc_intercepts(svm
);
2350 svm
->nested
.vmcb
= vmcb_gpa
;
2357 static void nested_svm_vmloadsave(struct vmcb
*from_vmcb
, struct vmcb
*to_vmcb
)
2359 to_vmcb
->save
.fs
= from_vmcb
->save
.fs
;
2360 to_vmcb
->save
.gs
= from_vmcb
->save
.gs
;
2361 to_vmcb
->save
.tr
= from_vmcb
->save
.tr
;
2362 to_vmcb
->save
.ldtr
= from_vmcb
->save
.ldtr
;
2363 to_vmcb
->save
.kernel_gs_base
= from_vmcb
->save
.kernel_gs_base
;
2364 to_vmcb
->save
.star
= from_vmcb
->save
.star
;
2365 to_vmcb
->save
.lstar
= from_vmcb
->save
.lstar
;
2366 to_vmcb
->save
.cstar
= from_vmcb
->save
.cstar
;
2367 to_vmcb
->save
.sfmask
= from_vmcb
->save
.sfmask
;
2368 to_vmcb
->save
.sysenter_cs
= from_vmcb
->save
.sysenter_cs
;
2369 to_vmcb
->save
.sysenter_esp
= from_vmcb
->save
.sysenter_esp
;
2370 to_vmcb
->save
.sysenter_eip
= from_vmcb
->save
.sysenter_eip
;
2373 static int vmload_interception(struct vcpu_svm
*svm
)
2375 struct vmcb
*nested_vmcb
;
2378 if (nested_svm_check_permissions(svm
))
2381 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
2382 skip_emulated_instruction(&svm
->vcpu
);
2384 nested_vmcb
= nested_svm_map(svm
, svm
->vmcb
->save
.rax
, &page
);
2388 nested_svm_vmloadsave(nested_vmcb
, svm
->vmcb
);
2389 nested_svm_unmap(page
);
2394 static int vmsave_interception(struct vcpu_svm
*svm
)
2396 struct vmcb
*nested_vmcb
;
2399 if (nested_svm_check_permissions(svm
))
2402 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
2403 skip_emulated_instruction(&svm
->vcpu
);
2405 nested_vmcb
= nested_svm_map(svm
, svm
->vmcb
->save
.rax
, &page
);
2409 nested_svm_vmloadsave(svm
->vmcb
, nested_vmcb
);
2410 nested_svm_unmap(page
);
2415 static int vmrun_interception(struct vcpu_svm
*svm
)
2417 if (nested_svm_check_permissions(svm
))
2420 /* Save rip after vmrun instruction */
2421 kvm_rip_write(&svm
->vcpu
, kvm_rip_read(&svm
->vcpu
) + 3);
2423 if (!nested_svm_vmrun(svm
))
2426 if (!nested_svm_vmrun_msrpm(svm
))
2433 svm
->vmcb
->control
.exit_code
= SVM_EXIT_ERR
;
2434 svm
->vmcb
->control
.exit_code_hi
= 0;
2435 svm
->vmcb
->control
.exit_info_1
= 0;
2436 svm
->vmcb
->control
.exit_info_2
= 0;
2438 nested_svm_vmexit(svm
);
2443 static int stgi_interception(struct vcpu_svm
*svm
)
2445 if (nested_svm_check_permissions(svm
))
2448 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
2449 skip_emulated_instruction(&svm
->vcpu
);
2450 kvm_make_request(KVM_REQ_EVENT
, &svm
->vcpu
);
2457 static int clgi_interception(struct vcpu_svm
*svm
)
2459 if (nested_svm_check_permissions(svm
))
2462 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
2463 skip_emulated_instruction(&svm
->vcpu
);
2467 /* After a CLGI no interrupts should come */
2468 svm_clear_vintr(svm
);
2469 svm
->vmcb
->control
.int_ctl
&= ~V_IRQ_MASK
;
2474 static int invlpga_interception(struct vcpu_svm
*svm
)
2476 struct kvm_vcpu
*vcpu
= &svm
->vcpu
;
2478 trace_kvm_invlpga(svm
->vmcb
->save
.rip
, vcpu
->arch
.regs
[VCPU_REGS_RCX
],
2479 vcpu
->arch
.regs
[VCPU_REGS_RAX
]);
2481 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2482 kvm_mmu_invlpg(vcpu
, vcpu
->arch
.regs
[VCPU_REGS_RAX
]);
2484 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
2485 skip_emulated_instruction(&svm
->vcpu
);
2489 static int skinit_interception(struct vcpu_svm
*svm
)
2491 trace_kvm_skinit(svm
->vmcb
->save
.rip
, svm
->vcpu
.arch
.regs
[VCPU_REGS_RAX
]);
2493 kvm_queue_exception(&svm
->vcpu
, UD_VECTOR
);
2497 static int invalid_op_interception(struct vcpu_svm
*svm
)
2499 kvm_queue_exception(&svm
->vcpu
, UD_VECTOR
);
2503 static int task_switch_interception(struct vcpu_svm
*svm
)
2507 int int_type
= svm
->vmcb
->control
.exit_int_info
&
2508 SVM_EXITINTINFO_TYPE_MASK
;
2509 int int_vec
= svm
->vmcb
->control
.exit_int_info
& SVM_EVTINJ_VEC_MASK
;
2511 svm
->vmcb
->control
.exit_int_info
& SVM_EXITINTINFO_TYPE_MASK
;
2513 svm
->vmcb
->control
.exit_int_info
& SVM_EXITINTINFO_VALID
;
2514 bool has_error_code
= false;
2517 tss_selector
= (u16
)svm
->vmcb
->control
.exit_info_1
;
2519 if (svm
->vmcb
->control
.exit_info_2
&
2520 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET
))
2521 reason
= TASK_SWITCH_IRET
;
2522 else if (svm
->vmcb
->control
.exit_info_2
&
2523 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP
))
2524 reason
= TASK_SWITCH_JMP
;
2526 reason
= TASK_SWITCH_GATE
;
2528 reason
= TASK_SWITCH_CALL
;
2530 if (reason
== TASK_SWITCH_GATE
) {
2532 case SVM_EXITINTINFO_TYPE_NMI
:
2533 svm
->vcpu
.arch
.nmi_injected
= false;
2535 case SVM_EXITINTINFO_TYPE_EXEPT
:
2536 if (svm
->vmcb
->control
.exit_info_2
&
2537 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE
)) {
2538 has_error_code
= true;
2540 (u32
)svm
->vmcb
->control
.exit_info_2
;
2542 kvm_clear_exception_queue(&svm
->vcpu
);
2544 case SVM_EXITINTINFO_TYPE_INTR
:
2545 kvm_clear_interrupt_queue(&svm
->vcpu
);
2552 if (reason
!= TASK_SWITCH_GATE
||
2553 int_type
== SVM_EXITINTINFO_TYPE_SOFT
||
2554 (int_type
== SVM_EXITINTINFO_TYPE_EXEPT
&&
2555 (int_vec
== OF_VECTOR
|| int_vec
== BP_VECTOR
)))
2556 skip_emulated_instruction(&svm
->vcpu
);
2558 if (kvm_task_switch(&svm
->vcpu
, tss_selector
, reason
,
2559 has_error_code
, error_code
) == EMULATE_FAIL
) {
2560 svm
->vcpu
.run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
2561 svm
->vcpu
.run
->internal
.suberror
= KVM_INTERNAL_ERROR_EMULATION
;
2562 svm
->vcpu
.run
->internal
.ndata
= 0;
2568 static int cpuid_interception(struct vcpu_svm
*svm
)
2570 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 2;
2571 kvm_emulate_cpuid(&svm
->vcpu
);
2575 static int iret_interception(struct vcpu_svm
*svm
)
2577 ++svm
->vcpu
.stat
.nmi_window_exits
;
2578 clr_intercept(svm
, INTERCEPT_IRET
);
2579 svm
->vcpu
.arch
.hflags
|= HF_IRET_MASK
;
2583 static int invlpg_interception(struct vcpu_svm
*svm
)
2585 return emulate_instruction(&svm
->vcpu
, 0, 0, 0) == EMULATE_DONE
;
2588 static int emulate_on_interception(struct vcpu_svm
*svm
)
2590 return emulate_instruction(&svm
->vcpu
, 0, 0, 0) == EMULATE_DONE
;
2593 static int cr0_write_interception(struct vcpu_svm
*svm
)
2595 struct kvm_vcpu
*vcpu
= &svm
->vcpu
;
2598 r
= emulate_instruction(&svm
->vcpu
, 0, 0, 0);
2600 if (svm
->nested
.vmexit_rip
) {
2601 kvm_register_write(vcpu
, VCPU_REGS_RIP
, svm
->nested
.vmexit_rip
);
2602 kvm_register_write(vcpu
, VCPU_REGS_RSP
, svm
->nested
.vmexit_rsp
);
2603 kvm_register_write(vcpu
, VCPU_REGS_RAX
, svm
->nested
.vmexit_rax
);
2604 svm
->nested
.vmexit_rip
= 0;
2607 return r
== EMULATE_DONE
;
2610 static int cr8_write_interception(struct vcpu_svm
*svm
)
2612 struct kvm_run
*kvm_run
= svm
->vcpu
.run
;
2614 u8 cr8_prev
= kvm_get_cr8(&svm
->vcpu
);
2615 /* instruction emulation calls kvm_set_cr8() */
2616 emulate_instruction(&svm
->vcpu
, 0, 0, 0);
2617 if (irqchip_in_kernel(svm
->vcpu
.kvm
)) {
2618 clr_cr_intercept(svm
, INTERCEPT_CR8_WRITE
);
2621 if (cr8_prev
<= kvm_get_cr8(&svm
->vcpu
))
2623 kvm_run
->exit_reason
= KVM_EXIT_SET_TPR
;
2627 static int svm_get_msr(struct kvm_vcpu
*vcpu
, unsigned ecx
, u64
*data
)
2629 struct vcpu_svm
*svm
= to_svm(vcpu
);
2632 case MSR_IA32_TSC
: {
2633 struct vmcb
*vmcb
= get_host_vmcb(svm
);
2635 *data
= vmcb
->control
.tsc_offset
+ native_read_tsc();
2639 *data
= svm
->vmcb
->save
.star
;
2641 #ifdef CONFIG_X86_64
2643 *data
= svm
->vmcb
->save
.lstar
;
2646 *data
= svm
->vmcb
->save
.cstar
;
2648 case MSR_KERNEL_GS_BASE
:
2649 *data
= svm
->vmcb
->save
.kernel_gs_base
;
2651 case MSR_SYSCALL_MASK
:
2652 *data
= svm
->vmcb
->save
.sfmask
;
2655 case MSR_IA32_SYSENTER_CS
:
2656 *data
= svm
->vmcb
->save
.sysenter_cs
;
2658 case MSR_IA32_SYSENTER_EIP
:
2659 *data
= svm
->sysenter_eip
;
2661 case MSR_IA32_SYSENTER_ESP
:
2662 *data
= svm
->sysenter_esp
;
2665 * Nobody will change the following 5 values in the VMCB so we can
2666 * safely return them on rdmsr. They will always be 0 until LBRV is
2669 case MSR_IA32_DEBUGCTLMSR
:
2670 *data
= svm
->vmcb
->save
.dbgctl
;
2672 case MSR_IA32_LASTBRANCHFROMIP
:
2673 *data
= svm
->vmcb
->save
.br_from
;
2675 case MSR_IA32_LASTBRANCHTOIP
:
2676 *data
= svm
->vmcb
->save
.br_to
;
2678 case MSR_IA32_LASTINTFROMIP
:
2679 *data
= svm
->vmcb
->save
.last_excp_from
;
2681 case MSR_IA32_LASTINTTOIP
:
2682 *data
= svm
->vmcb
->save
.last_excp_to
;
2684 case MSR_VM_HSAVE_PA
:
2685 *data
= svm
->nested
.hsave_msr
;
2688 *data
= svm
->nested
.vm_cr_msr
;
2690 case MSR_IA32_UCODE_REV
:
2694 return kvm_get_msr_common(vcpu
, ecx
, data
);
2699 static int rdmsr_interception(struct vcpu_svm
*svm
)
2701 u32 ecx
= svm
->vcpu
.arch
.regs
[VCPU_REGS_RCX
];
2704 if (svm_get_msr(&svm
->vcpu
, ecx
, &data
)) {
2705 trace_kvm_msr_read_ex(ecx
);
2706 kvm_inject_gp(&svm
->vcpu
, 0);
2708 trace_kvm_msr_read(ecx
, data
);
2710 svm
->vcpu
.arch
.regs
[VCPU_REGS_RAX
] = data
& 0xffffffff;
2711 svm
->vcpu
.arch
.regs
[VCPU_REGS_RDX
] = data
>> 32;
2712 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 2;
2713 skip_emulated_instruction(&svm
->vcpu
);
2718 static int svm_set_vm_cr(struct kvm_vcpu
*vcpu
, u64 data
)
2720 struct vcpu_svm
*svm
= to_svm(vcpu
);
2721 int svm_dis
, chg_mask
;
2723 if (data
& ~SVM_VM_CR_VALID_MASK
)
2726 chg_mask
= SVM_VM_CR_VALID_MASK
;
2728 if (svm
->nested
.vm_cr_msr
& SVM_VM_CR_SVM_DIS_MASK
)
2729 chg_mask
&= ~(SVM_VM_CR_SVM_LOCK_MASK
| SVM_VM_CR_SVM_DIS_MASK
);
2731 svm
->nested
.vm_cr_msr
&= ~chg_mask
;
2732 svm
->nested
.vm_cr_msr
|= (data
& chg_mask
);
2734 svm_dis
= svm
->nested
.vm_cr_msr
& SVM_VM_CR_SVM_DIS_MASK
;
2736 /* check for svm_disable while efer.svme is set */
2737 if (svm_dis
&& (vcpu
->arch
.efer
& EFER_SVME
))
2743 static int svm_set_msr(struct kvm_vcpu
*vcpu
, unsigned ecx
, u64 data
)
2745 struct vcpu_svm
*svm
= to_svm(vcpu
);
2749 kvm_write_tsc(vcpu
, data
);
2752 svm
->vmcb
->save
.star
= data
;
2754 #ifdef CONFIG_X86_64
2756 svm
->vmcb
->save
.lstar
= data
;
2759 svm
->vmcb
->save
.cstar
= data
;
2761 case MSR_KERNEL_GS_BASE
:
2762 svm
->vmcb
->save
.kernel_gs_base
= data
;
2764 case MSR_SYSCALL_MASK
:
2765 svm
->vmcb
->save
.sfmask
= data
;
2768 case MSR_IA32_SYSENTER_CS
:
2769 svm
->vmcb
->save
.sysenter_cs
= data
;
2771 case MSR_IA32_SYSENTER_EIP
:
2772 svm
->sysenter_eip
= data
;
2773 svm
->vmcb
->save
.sysenter_eip
= data
;
2775 case MSR_IA32_SYSENTER_ESP
:
2776 svm
->sysenter_esp
= data
;
2777 svm
->vmcb
->save
.sysenter_esp
= data
;
2779 case MSR_IA32_DEBUGCTLMSR
:
2780 if (!boot_cpu_has(X86_FEATURE_LBRV
)) {
2781 pr_unimpl(vcpu
, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
2785 if (data
& DEBUGCTL_RESERVED_BITS
)
2788 svm
->vmcb
->save
.dbgctl
= data
;
2789 if (data
& (1ULL<<0))
2790 svm_enable_lbrv(svm
);
2792 svm_disable_lbrv(svm
);
2794 case MSR_VM_HSAVE_PA
:
2795 svm
->nested
.hsave_msr
= data
;
2798 return svm_set_vm_cr(vcpu
, data
);
2800 pr_unimpl(vcpu
, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx
, data
);
2803 return kvm_set_msr_common(vcpu
, ecx
, data
);
2808 static int wrmsr_interception(struct vcpu_svm
*svm
)
2810 u32 ecx
= svm
->vcpu
.arch
.regs
[VCPU_REGS_RCX
];
2811 u64 data
= (svm
->vcpu
.arch
.regs
[VCPU_REGS_RAX
] & -1u)
2812 | ((u64
)(svm
->vcpu
.arch
.regs
[VCPU_REGS_RDX
] & -1u) << 32);
2815 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 2;
2816 if (svm_set_msr(&svm
->vcpu
, ecx
, data
)) {
2817 trace_kvm_msr_write_ex(ecx
, data
);
2818 kvm_inject_gp(&svm
->vcpu
, 0);
2820 trace_kvm_msr_write(ecx
, data
);
2821 skip_emulated_instruction(&svm
->vcpu
);
2826 static int msr_interception(struct vcpu_svm
*svm
)
2828 if (svm
->vmcb
->control
.exit_info_1
)
2829 return wrmsr_interception(svm
);
2831 return rdmsr_interception(svm
);
2834 static int interrupt_window_interception(struct vcpu_svm
*svm
)
2836 struct kvm_run
*kvm_run
= svm
->vcpu
.run
;
2838 kvm_make_request(KVM_REQ_EVENT
, &svm
->vcpu
);
2839 svm_clear_vintr(svm
);
2840 svm
->vmcb
->control
.int_ctl
&= ~V_IRQ_MASK
;
2842 * If the user space waits to inject interrupts, exit as soon as
2845 if (!irqchip_in_kernel(svm
->vcpu
.kvm
) &&
2846 kvm_run
->request_interrupt_window
&&
2847 !kvm_cpu_has_interrupt(&svm
->vcpu
)) {
2848 ++svm
->vcpu
.stat
.irq_window_exits
;
2849 kvm_run
->exit_reason
= KVM_EXIT_IRQ_WINDOW_OPEN
;
2856 static int pause_interception(struct vcpu_svm
*svm
)
2858 kvm_vcpu_on_spin(&(svm
->vcpu
));
2862 static int (*svm_exit_handlers
[])(struct vcpu_svm
*svm
) = {
2863 [SVM_EXIT_READ_CR0
] = emulate_on_interception
,
2864 [SVM_EXIT_READ_CR3
] = emulate_on_interception
,
2865 [SVM_EXIT_READ_CR4
] = emulate_on_interception
,
2866 [SVM_EXIT_READ_CR8
] = emulate_on_interception
,
2867 [SVM_EXIT_CR0_SEL_WRITE
] = emulate_on_interception
,
2868 [SVM_EXIT_WRITE_CR0
] = cr0_write_interception
,
2869 [SVM_EXIT_WRITE_CR3
] = emulate_on_interception
,
2870 [SVM_EXIT_WRITE_CR4
] = emulate_on_interception
,
2871 [SVM_EXIT_WRITE_CR8
] = cr8_write_interception
,
2872 [SVM_EXIT_READ_DR0
] = emulate_on_interception
,
2873 [SVM_EXIT_READ_DR1
] = emulate_on_interception
,
2874 [SVM_EXIT_READ_DR2
] = emulate_on_interception
,
2875 [SVM_EXIT_READ_DR3
] = emulate_on_interception
,
2876 [SVM_EXIT_READ_DR4
] = emulate_on_interception
,
2877 [SVM_EXIT_READ_DR5
] = emulate_on_interception
,
2878 [SVM_EXIT_READ_DR6
] = emulate_on_interception
,
2879 [SVM_EXIT_READ_DR7
] = emulate_on_interception
,
2880 [SVM_EXIT_WRITE_DR0
] = emulate_on_interception
,
2881 [SVM_EXIT_WRITE_DR1
] = emulate_on_interception
,
2882 [SVM_EXIT_WRITE_DR2
] = emulate_on_interception
,
2883 [SVM_EXIT_WRITE_DR3
] = emulate_on_interception
,
2884 [SVM_EXIT_WRITE_DR4
] = emulate_on_interception
,
2885 [SVM_EXIT_WRITE_DR5
] = emulate_on_interception
,
2886 [SVM_EXIT_WRITE_DR6
] = emulate_on_interception
,
2887 [SVM_EXIT_WRITE_DR7
] = emulate_on_interception
,
2888 [SVM_EXIT_EXCP_BASE
+ DB_VECTOR
] = db_interception
,
2889 [SVM_EXIT_EXCP_BASE
+ BP_VECTOR
] = bp_interception
,
2890 [SVM_EXIT_EXCP_BASE
+ UD_VECTOR
] = ud_interception
,
2891 [SVM_EXIT_EXCP_BASE
+ PF_VECTOR
] = pf_interception
,
2892 [SVM_EXIT_EXCP_BASE
+ NM_VECTOR
] = nm_interception
,
2893 [SVM_EXIT_EXCP_BASE
+ MC_VECTOR
] = mc_interception
,
2894 [SVM_EXIT_INTR
] = intr_interception
,
2895 [SVM_EXIT_NMI
] = nmi_interception
,
2896 [SVM_EXIT_SMI
] = nop_on_interception
,
2897 [SVM_EXIT_INIT
] = nop_on_interception
,
2898 [SVM_EXIT_VINTR
] = interrupt_window_interception
,
2899 [SVM_EXIT_CPUID
] = cpuid_interception
,
2900 [SVM_EXIT_IRET
] = iret_interception
,
2901 [SVM_EXIT_INVD
] = emulate_on_interception
,
2902 [SVM_EXIT_PAUSE
] = pause_interception
,
2903 [SVM_EXIT_HLT
] = halt_interception
,
2904 [SVM_EXIT_INVLPG
] = invlpg_interception
,
2905 [SVM_EXIT_INVLPGA
] = invlpga_interception
,
2906 [SVM_EXIT_IOIO
] = io_interception
,
2907 [SVM_EXIT_MSR
] = msr_interception
,
2908 [SVM_EXIT_TASK_SWITCH
] = task_switch_interception
,
2909 [SVM_EXIT_SHUTDOWN
] = shutdown_interception
,
2910 [SVM_EXIT_VMRUN
] = vmrun_interception
,
2911 [SVM_EXIT_VMMCALL
] = vmmcall_interception
,
2912 [SVM_EXIT_VMLOAD
] = vmload_interception
,
2913 [SVM_EXIT_VMSAVE
] = vmsave_interception
,
2914 [SVM_EXIT_STGI
] = stgi_interception
,
2915 [SVM_EXIT_CLGI
] = clgi_interception
,
2916 [SVM_EXIT_SKINIT
] = skinit_interception
,
2917 [SVM_EXIT_WBINVD
] = emulate_on_interception
,
2918 [SVM_EXIT_MONITOR
] = invalid_op_interception
,
2919 [SVM_EXIT_MWAIT
] = invalid_op_interception
,
2920 [SVM_EXIT_NPF
] = pf_interception
,
2923 void dump_vmcb(struct kvm_vcpu
*vcpu
)
2925 struct vcpu_svm
*svm
= to_svm(vcpu
);
2926 struct vmcb_control_area
*control
= &svm
->vmcb
->control
;
2927 struct vmcb_save_area
*save
= &svm
->vmcb
->save
;
2929 pr_err("VMCB Control Area:\n");
2930 pr_err("cr_read: %04x\n", control
->intercept_cr
& 0xffff);
2931 pr_err("cr_write: %04x\n", control
->intercept_cr
>> 16);
2932 pr_err("dr_read: %04x\n", control
->intercept_dr
& 0xffff);
2933 pr_err("dr_write: %04x\n", control
->intercept_dr
>> 16);
2934 pr_err("exceptions: %08x\n", control
->intercept_exceptions
);
2935 pr_err("intercepts: %016llx\n", control
->intercept
);
2936 pr_err("pause filter count: %d\n", control
->pause_filter_count
);
2937 pr_err("iopm_base_pa: %016llx\n", control
->iopm_base_pa
);
2938 pr_err("msrpm_base_pa: %016llx\n", control
->msrpm_base_pa
);
2939 pr_err("tsc_offset: %016llx\n", control
->tsc_offset
);
2940 pr_err("asid: %d\n", control
->asid
);
2941 pr_err("tlb_ctl: %d\n", control
->tlb_ctl
);
2942 pr_err("int_ctl: %08x\n", control
->int_ctl
);
2943 pr_err("int_vector: %08x\n", control
->int_vector
);
2944 pr_err("int_state: %08x\n", control
->int_state
);
2945 pr_err("exit_code: %08x\n", control
->exit_code
);
2946 pr_err("exit_info1: %016llx\n", control
->exit_info_1
);
2947 pr_err("exit_info2: %016llx\n", control
->exit_info_2
);
2948 pr_err("exit_int_info: %08x\n", control
->exit_int_info
);
2949 pr_err("exit_int_info_err: %08x\n", control
->exit_int_info_err
);
2950 pr_err("nested_ctl: %lld\n", control
->nested_ctl
);
2951 pr_err("nested_cr3: %016llx\n", control
->nested_cr3
);
2952 pr_err("event_inj: %08x\n", control
->event_inj
);
2953 pr_err("event_inj_err: %08x\n", control
->event_inj_err
);
2954 pr_err("lbr_ctl: %lld\n", control
->lbr_ctl
);
2955 pr_err("next_rip: %016llx\n", control
->next_rip
);
2956 pr_err("VMCB State Save Area:\n");
2957 pr_err("es: s: %04x a: %04x l: %08x b: %016llx\n",
2958 save
->es
.selector
, save
->es
.attrib
,
2959 save
->es
.limit
, save
->es
.base
);
2960 pr_err("cs: s: %04x a: %04x l: %08x b: %016llx\n",
2961 save
->cs
.selector
, save
->cs
.attrib
,
2962 save
->cs
.limit
, save
->cs
.base
);
2963 pr_err("ss: s: %04x a: %04x l: %08x b: %016llx\n",
2964 save
->ss
.selector
, save
->ss
.attrib
,
2965 save
->ss
.limit
, save
->ss
.base
);
2966 pr_err("ds: s: %04x a: %04x l: %08x b: %016llx\n",
2967 save
->ds
.selector
, save
->ds
.attrib
,
2968 save
->ds
.limit
, save
->ds
.base
);
2969 pr_err("fs: s: %04x a: %04x l: %08x b: %016llx\n",
2970 save
->fs
.selector
, save
->fs
.attrib
,
2971 save
->fs
.limit
, save
->fs
.base
);
2972 pr_err("gs: s: %04x a: %04x l: %08x b: %016llx\n",
2973 save
->gs
.selector
, save
->gs
.attrib
,
2974 save
->gs
.limit
, save
->gs
.base
);
2975 pr_err("gdtr: s: %04x a: %04x l: %08x b: %016llx\n",
2976 save
->gdtr
.selector
, save
->gdtr
.attrib
,
2977 save
->gdtr
.limit
, save
->gdtr
.base
);
2978 pr_err("ldtr: s: %04x a: %04x l: %08x b: %016llx\n",
2979 save
->ldtr
.selector
, save
->ldtr
.attrib
,
2980 save
->ldtr
.limit
, save
->ldtr
.base
);
2981 pr_err("idtr: s: %04x a: %04x l: %08x b: %016llx\n",
2982 save
->idtr
.selector
, save
->idtr
.attrib
,
2983 save
->idtr
.limit
, save
->idtr
.base
);
2984 pr_err("tr: s: %04x a: %04x l: %08x b: %016llx\n",
2985 save
->tr
.selector
, save
->tr
.attrib
,
2986 save
->tr
.limit
, save
->tr
.base
);
2987 pr_err("cpl: %d efer: %016llx\n",
2988 save
->cpl
, save
->efer
);
2989 pr_err("cr0: %016llx cr2: %016llx\n",
2990 save
->cr0
, save
->cr2
);
2991 pr_err("cr3: %016llx cr4: %016llx\n",
2992 save
->cr3
, save
->cr4
);
2993 pr_err("dr6: %016llx dr7: %016llx\n",
2994 save
->dr6
, save
->dr7
);
2995 pr_err("rip: %016llx rflags: %016llx\n",
2996 save
->rip
, save
->rflags
);
2997 pr_err("rsp: %016llx rax: %016llx\n",
2998 save
->rsp
, save
->rax
);
2999 pr_err("star: %016llx lstar: %016llx\n",
3000 save
->star
, save
->lstar
);
3001 pr_err("cstar: %016llx sfmask: %016llx\n",
3002 save
->cstar
, save
->sfmask
);
3003 pr_err("kernel_gs_base: %016llx sysenter_cs: %016llx\n",
3004 save
->kernel_gs_base
, save
->sysenter_cs
);
3005 pr_err("sysenter_esp: %016llx sysenter_eip: %016llx\n",
3006 save
->sysenter_esp
, save
->sysenter_eip
);
3007 pr_err("gpat: %016llx dbgctl: %016llx\n",
3008 save
->g_pat
, save
->dbgctl
);
3009 pr_err("br_from: %016llx br_to: %016llx\n",
3010 save
->br_from
, save
->br_to
);
3011 pr_err("excp_from: %016llx excp_to: %016llx\n",
3012 save
->last_excp_from
, save
->last_excp_to
);
3016 static void svm_get_exit_info(struct kvm_vcpu
*vcpu
, u64
*info1
, u64
*info2
)
3018 struct vmcb_control_area
*control
= &to_svm(vcpu
)->vmcb
->control
;
3020 *info1
= control
->exit_info_1
;
3021 *info2
= control
->exit_info_2
;
3024 static int handle_exit(struct kvm_vcpu
*vcpu
)
3026 struct vcpu_svm
*svm
= to_svm(vcpu
);
3027 struct kvm_run
*kvm_run
= vcpu
->run
;
3028 u32 exit_code
= svm
->vmcb
->control
.exit_code
;
3030 trace_kvm_exit(exit_code
, vcpu
, KVM_ISA_SVM
);
3032 if (!is_cr_intercept(svm
, INTERCEPT_CR0_WRITE
))
3033 vcpu
->arch
.cr0
= svm
->vmcb
->save
.cr0
;
3035 vcpu
->arch
.cr3
= svm
->vmcb
->save
.cr3
;
3037 if (unlikely(svm
->nested
.exit_required
)) {
3038 nested_svm_vmexit(svm
);
3039 svm
->nested
.exit_required
= false;
3044 if (is_guest_mode(vcpu
)) {
3047 trace_kvm_nested_vmexit(svm
->vmcb
->save
.rip
, exit_code
,
3048 svm
->vmcb
->control
.exit_info_1
,
3049 svm
->vmcb
->control
.exit_info_2
,
3050 svm
->vmcb
->control
.exit_int_info
,
3051 svm
->vmcb
->control
.exit_int_info_err
);
3053 vmexit
= nested_svm_exit_special(svm
);
3055 if (vmexit
== NESTED_EXIT_CONTINUE
)
3056 vmexit
= nested_svm_exit_handled(svm
);
3058 if (vmexit
== NESTED_EXIT_DONE
)
3062 svm_complete_interrupts(svm
);
3064 if (svm
->vmcb
->control
.exit_code
== SVM_EXIT_ERR
) {
3065 kvm_run
->exit_reason
= KVM_EXIT_FAIL_ENTRY
;
3066 kvm_run
->fail_entry
.hardware_entry_failure_reason
3067 = svm
->vmcb
->control
.exit_code
;
3068 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
3073 if (is_external_interrupt(svm
->vmcb
->control
.exit_int_info
) &&
3074 exit_code
!= SVM_EXIT_EXCP_BASE
+ PF_VECTOR
&&
3075 exit_code
!= SVM_EXIT_NPF
&& exit_code
!= SVM_EXIT_TASK_SWITCH
&&
3076 exit_code
!= SVM_EXIT_INTR
&& exit_code
!= SVM_EXIT_NMI
)
3077 printk(KERN_ERR
"%s: unexpected exit_ini_info 0x%x "
3079 __func__
, svm
->vmcb
->control
.exit_int_info
,
3082 if (exit_code
>= ARRAY_SIZE(svm_exit_handlers
)
3083 || !svm_exit_handlers
[exit_code
]) {
3084 kvm_run
->exit_reason
= KVM_EXIT_UNKNOWN
;
3085 kvm_run
->hw
.hardware_exit_reason
= exit_code
;
3089 return svm_exit_handlers
[exit_code
](svm
);
3092 static void reload_tss(struct kvm_vcpu
*vcpu
)
3094 int cpu
= raw_smp_processor_id();
3096 struct svm_cpu_data
*sd
= per_cpu(svm_data
, cpu
);
3097 sd
->tss_desc
->type
= 9; /* available 32/64-bit TSS */
3101 static void pre_svm_run(struct vcpu_svm
*svm
)
3103 int cpu
= raw_smp_processor_id();
3105 struct svm_cpu_data
*sd
= per_cpu(svm_data
, cpu
);
3107 svm
->vmcb
->control
.tlb_ctl
= TLB_CONTROL_DO_NOTHING
;
3108 /* FIXME: handle wraparound of asid_generation */
3109 if (svm
->asid_generation
!= sd
->asid_generation
)
3113 static void svm_inject_nmi(struct kvm_vcpu
*vcpu
)
3115 struct vcpu_svm
*svm
= to_svm(vcpu
);
3117 svm
->vmcb
->control
.event_inj
= SVM_EVTINJ_VALID
| SVM_EVTINJ_TYPE_NMI
;
3118 vcpu
->arch
.hflags
|= HF_NMI_MASK
;
3119 set_intercept(svm
, INTERCEPT_IRET
);
3120 ++vcpu
->stat
.nmi_injections
;
3123 static inline void svm_inject_irq(struct vcpu_svm
*svm
, int irq
)
3125 struct vmcb_control_area
*control
;
3127 control
= &svm
->vmcb
->control
;
3128 control
->int_vector
= irq
;
3129 control
->int_ctl
&= ~V_INTR_PRIO_MASK
;
3130 control
->int_ctl
|= V_IRQ_MASK
|
3131 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT
);
3134 static void svm_set_irq(struct kvm_vcpu
*vcpu
)
3136 struct vcpu_svm
*svm
= to_svm(vcpu
);
3138 BUG_ON(!(gif_set(svm
)));
3140 trace_kvm_inj_virq(vcpu
->arch
.interrupt
.nr
);
3141 ++vcpu
->stat
.irq_injections
;
3143 svm
->vmcb
->control
.event_inj
= vcpu
->arch
.interrupt
.nr
|
3144 SVM_EVTINJ_VALID
| SVM_EVTINJ_TYPE_INTR
;
3147 static void update_cr8_intercept(struct kvm_vcpu
*vcpu
, int tpr
, int irr
)
3149 struct vcpu_svm
*svm
= to_svm(vcpu
);
3151 if (is_guest_mode(vcpu
) && (vcpu
->arch
.hflags
& HF_VINTR_MASK
))
3158 set_cr_intercept(svm
, INTERCEPT_CR8_WRITE
);
3161 static int svm_nmi_allowed(struct kvm_vcpu
*vcpu
)
3163 struct vcpu_svm
*svm
= to_svm(vcpu
);
3164 struct vmcb
*vmcb
= svm
->vmcb
;
3166 ret
= !(vmcb
->control
.int_state
& SVM_INTERRUPT_SHADOW_MASK
) &&
3167 !(svm
->vcpu
.arch
.hflags
& HF_NMI_MASK
);
3168 ret
= ret
&& gif_set(svm
) && nested_svm_nmi(svm
);
3173 static bool svm_get_nmi_mask(struct kvm_vcpu
*vcpu
)
3175 struct vcpu_svm
*svm
= to_svm(vcpu
);
3177 return !!(svm
->vcpu
.arch
.hflags
& HF_NMI_MASK
);
3180 static void svm_set_nmi_mask(struct kvm_vcpu
*vcpu
, bool masked
)
3182 struct vcpu_svm
*svm
= to_svm(vcpu
);
3185 svm
->vcpu
.arch
.hflags
|= HF_NMI_MASK
;
3186 set_intercept(svm
, INTERCEPT_IRET
);
3188 svm
->vcpu
.arch
.hflags
&= ~HF_NMI_MASK
;
3189 clr_intercept(svm
, INTERCEPT_IRET
);
3193 static int svm_interrupt_allowed(struct kvm_vcpu
*vcpu
)
3195 struct vcpu_svm
*svm
= to_svm(vcpu
);
3196 struct vmcb
*vmcb
= svm
->vmcb
;
3199 if (!gif_set(svm
) ||
3200 (vmcb
->control
.int_state
& SVM_INTERRUPT_SHADOW_MASK
))
3203 ret
= !!(vmcb
->save
.rflags
& X86_EFLAGS_IF
);
3205 if (is_guest_mode(vcpu
))
3206 return ret
&& !(svm
->vcpu
.arch
.hflags
& HF_VINTR_MASK
);
3211 static void enable_irq_window(struct kvm_vcpu
*vcpu
)
3213 struct vcpu_svm
*svm
= to_svm(vcpu
);
3216 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3217 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3218 * get that intercept, this function will be called again though and
3219 * we'll get the vintr intercept.
3221 if (gif_set(svm
) && nested_svm_intr(svm
)) {
3223 svm_inject_irq(svm
, 0x0);
3227 static void enable_nmi_window(struct kvm_vcpu
*vcpu
)
3229 struct vcpu_svm
*svm
= to_svm(vcpu
);
3231 if ((svm
->vcpu
.arch
.hflags
& (HF_NMI_MASK
| HF_IRET_MASK
))
3233 return; /* IRET will cause a vm exit */
3236 * Something prevents NMI from been injected. Single step over possible
3237 * problem (IRET or exception injection or interrupt shadow)
3239 svm
->nmi_singlestep
= true;
3240 svm
->vmcb
->save
.rflags
|= (X86_EFLAGS_TF
| X86_EFLAGS_RF
);
3241 update_db_intercept(vcpu
);
3244 static int svm_set_tss_addr(struct kvm
*kvm
, unsigned int addr
)
3249 static void svm_flush_tlb(struct kvm_vcpu
*vcpu
)
3251 force_new_asid(vcpu
);
3254 static void svm_prepare_guest_switch(struct kvm_vcpu
*vcpu
)
3258 static inline void sync_cr8_to_lapic(struct kvm_vcpu
*vcpu
)
3260 struct vcpu_svm
*svm
= to_svm(vcpu
);
3262 if (is_guest_mode(vcpu
) && (vcpu
->arch
.hflags
& HF_VINTR_MASK
))
3265 if (!is_cr_intercept(svm
, INTERCEPT_CR8_WRITE
)) {
3266 int cr8
= svm
->vmcb
->control
.int_ctl
& V_TPR_MASK
;
3267 kvm_set_cr8(vcpu
, cr8
);
3271 static inline void sync_lapic_to_cr8(struct kvm_vcpu
*vcpu
)
3273 struct vcpu_svm
*svm
= to_svm(vcpu
);
3276 if (is_guest_mode(vcpu
) && (vcpu
->arch
.hflags
& HF_VINTR_MASK
))
3279 cr8
= kvm_get_cr8(vcpu
);
3280 svm
->vmcb
->control
.int_ctl
&= ~V_TPR_MASK
;
3281 svm
->vmcb
->control
.int_ctl
|= cr8
& V_TPR_MASK
;
3284 static void svm_complete_interrupts(struct vcpu_svm
*svm
)
3288 u32 exitintinfo
= svm
->vmcb
->control
.exit_int_info
;
3289 unsigned int3_injected
= svm
->int3_injected
;
3291 svm
->int3_injected
= 0;
3293 if (svm
->vcpu
.arch
.hflags
& HF_IRET_MASK
) {
3294 svm
->vcpu
.arch
.hflags
&= ~(HF_NMI_MASK
| HF_IRET_MASK
);
3295 kvm_make_request(KVM_REQ_EVENT
, &svm
->vcpu
);
3298 svm
->vcpu
.arch
.nmi_injected
= false;
3299 kvm_clear_exception_queue(&svm
->vcpu
);
3300 kvm_clear_interrupt_queue(&svm
->vcpu
);
3302 if (!(exitintinfo
& SVM_EXITINTINFO_VALID
))
3305 kvm_make_request(KVM_REQ_EVENT
, &svm
->vcpu
);
3307 vector
= exitintinfo
& SVM_EXITINTINFO_VEC_MASK
;
3308 type
= exitintinfo
& SVM_EXITINTINFO_TYPE_MASK
;
3311 case SVM_EXITINTINFO_TYPE_NMI
:
3312 svm
->vcpu
.arch
.nmi_injected
= true;
3314 case SVM_EXITINTINFO_TYPE_EXEPT
:
3316 * In case of software exceptions, do not reinject the vector,
3317 * but re-execute the instruction instead. Rewind RIP first
3318 * if we emulated INT3 before.
3320 if (kvm_exception_is_soft(vector
)) {
3321 if (vector
== BP_VECTOR
&& int3_injected
&&
3322 kvm_is_linear_rip(&svm
->vcpu
, svm
->int3_rip
))
3323 kvm_rip_write(&svm
->vcpu
,
3324 kvm_rip_read(&svm
->vcpu
) -
3328 if (exitintinfo
& SVM_EXITINTINFO_VALID_ERR
) {
3329 u32 err
= svm
->vmcb
->control
.exit_int_info_err
;
3330 kvm_requeue_exception_e(&svm
->vcpu
, vector
, err
);
3333 kvm_requeue_exception(&svm
->vcpu
, vector
);
3335 case SVM_EXITINTINFO_TYPE_INTR
:
3336 kvm_queue_interrupt(&svm
->vcpu
, vector
, false);
3343 static void svm_cancel_injection(struct kvm_vcpu
*vcpu
)
3345 struct vcpu_svm
*svm
= to_svm(vcpu
);
3346 struct vmcb_control_area
*control
= &svm
->vmcb
->control
;
3348 control
->exit_int_info
= control
->event_inj
;
3349 control
->exit_int_info_err
= control
->event_inj_err
;
3350 control
->event_inj
= 0;
3351 svm_complete_interrupts(svm
);
3354 #ifdef CONFIG_X86_64
3360 static void svm_vcpu_run(struct kvm_vcpu
*vcpu
)
3362 struct vcpu_svm
*svm
= to_svm(vcpu
);
3364 svm
->vmcb
->save
.rax
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
3365 svm
->vmcb
->save
.rsp
= vcpu
->arch
.regs
[VCPU_REGS_RSP
];
3366 svm
->vmcb
->save
.rip
= vcpu
->arch
.regs
[VCPU_REGS_RIP
];
3369 * A vmexit emulation is required before the vcpu can be executed
3372 if (unlikely(svm
->nested
.exit_required
))
3377 sync_lapic_to_cr8(vcpu
);
3379 svm
->vmcb
->save
.cr2
= vcpu
->arch
.cr2
;
3386 "push %%"R
"bp; \n\t"
3387 "mov %c[rbx](%[svm]), %%"R
"bx \n\t"
3388 "mov %c[rcx](%[svm]), %%"R
"cx \n\t"
3389 "mov %c[rdx](%[svm]), %%"R
"dx \n\t"
3390 "mov %c[rsi](%[svm]), %%"R
"si \n\t"
3391 "mov %c[rdi](%[svm]), %%"R
"di \n\t"
3392 "mov %c[rbp](%[svm]), %%"R
"bp \n\t"
3393 #ifdef CONFIG_X86_64
3394 "mov %c[r8](%[svm]), %%r8 \n\t"
3395 "mov %c[r9](%[svm]), %%r9 \n\t"
3396 "mov %c[r10](%[svm]), %%r10 \n\t"
3397 "mov %c[r11](%[svm]), %%r11 \n\t"
3398 "mov %c[r12](%[svm]), %%r12 \n\t"
3399 "mov %c[r13](%[svm]), %%r13 \n\t"
3400 "mov %c[r14](%[svm]), %%r14 \n\t"
3401 "mov %c[r15](%[svm]), %%r15 \n\t"
3404 /* Enter guest mode */
3406 "mov %c[vmcb](%[svm]), %%"R
"ax \n\t"
3407 __ex(SVM_VMLOAD
) "\n\t"
3408 __ex(SVM_VMRUN
) "\n\t"
3409 __ex(SVM_VMSAVE
) "\n\t"
3412 /* Save guest registers, load host registers */
3413 "mov %%"R
"bx, %c[rbx](%[svm]) \n\t"
3414 "mov %%"R
"cx, %c[rcx](%[svm]) \n\t"
3415 "mov %%"R
"dx, %c[rdx](%[svm]) \n\t"
3416 "mov %%"R
"si, %c[rsi](%[svm]) \n\t"
3417 "mov %%"R
"di, %c[rdi](%[svm]) \n\t"
3418 "mov %%"R
"bp, %c[rbp](%[svm]) \n\t"
3419 #ifdef CONFIG_X86_64
3420 "mov %%r8, %c[r8](%[svm]) \n\t"
3421 "mov %%r9, %c[r9](%[svm]) \n\t"
3422 "mov %%r10, %c[r10](%[svm]) \n\t"
3423 "mov %%r11, %c[r11](%[svm]) \n\t"
3424 "mov %%r12, %c[r12](%[svm]) \n\t"
3425 "mov %%r13, %c[r13](%[svm]) \n\t"
3426 "mov %%r14, %c[r14](%[svm]) \n\t"
3427 "mov %%r15, %c[r15](%[svm]) \n\t"
3432 [vmcb
]"i"(offsetof(struct vcpu_svm
, vmcb_pa
)),
3433 [rbx
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RBX
])),
3434 [rcx
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RCX
])),
3435 [rdx
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RDX
])),
3436 [rsi
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RSI
])),
3437 [rdi
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RDI
])),
3438 [rbp
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RBP
]))
3439 #ifdef CONFIG_X86_64
3440 , [r8
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R8
])),
3441 [r9
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R9
])),
3442 [r10
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R10
])),
3443 [r11
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R11
])),
3444 [r12
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R12
])),
3445 [r13
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R13
])),
3446 [r14
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R14
])),
3447 [r15
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R15
]))
3450 , R
"bx", R
"cx", R
"dx", R
"si", R
"di"
3451 #ifdef CONFIG_X86_64
3452 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3456 #ifdef CONFIG_X86_64
3457 wrmsrl(MSR_GS_BASE
, svm
->host
.gs_base
);
3459 loadsegment(fs
, svm
->host
.fs
);
3464 local_irq_disable();
3468 vcpu
->arch
.cr2
= svm
->vmcb
->save
.cr2
;
3469 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = svm
->vmcb
->save
.rax
;
3470 vcpu
->arch
.regs
[VCPU_REGS_RSP
] = svm
->vmcb
->save
.rsp
;
3471 vcpu
->arch
.regs
[VCPU_REGS_RIP
] = svm
->vmcb
->save
.rip
;
3473 sync_cr8_to_lapic(vcpu
);
3477 /* if exit due to PF check for async PF */
3478 if (svm
->vmcb
->control
.exit_code
== SVM_EXIT_EXCP_BASE
+ PF_VECTOR
)
3479 svm
->apf_reason
= kvm_read_and_reset_pf_reason();
3482 vcpu
->arch
.regs_avail
&= ~(1 << VCPU_EXREG_PDPTR
);
3483 vcpu
->arch
.regs_dirty
&= ~(1 << VCPU_EXREG_PDPTR
);
3487 * We need to handle MC intercepts here before the vcpu has a chance to
3488 * change the physical cpu
3490 if (unlikely(svm
->vmcb
->control
.exit_code
==
3491 SVM_EXIT_EXCP_BASE
+ MC_VECTOR
))
3492 svm_handle_mce(svm
);
3497 static void svm_set_cr3(struct kvm_vcpu
*vcpu
, unsigned long root
)
3499 struct vcpu_svm
*svm
= to_svm(vcpu
);
3501 svm
->vmcb
->save
.cr3
= root
;
3502 force_new_asid(vcpu
);
3505 static void set_tdp_cr3(struct kvm_vcpu
*vcpu
, unsigned long root
)
3507 struct vcpu_svm
*svm
= to_svm(vcpu
);
3509 svm
->vmcb
->control
.nested_cr3
= root
;
3511 /* Also sync guest cr3 here in case we live migrate */
3512 svm
->vmcb
->save
.cr3
= vcpu
->arch
.cr3
;
3514 force_new_asid(vcpu
);
3517 static int is_disabled(void)
3521 rdmsrl(MSR_VM_CR
, vm_cr
);
3522 if (vm_cr
& (1 << SVM_VM_CR_SVM_DISABLE
))
3529 svm_patch_hypercall(struct kvm_vcpu
*vcpu
, unsigned char *hypercall
)
3532 * Patch in the VMMCALL instruction:
3534 hypercall
[0] = 0x0f;
3535 hypercall
[1] = 0x01;
3536 hypercall
[2] = 0xd9;
3539 static void svm_check_processor_compat(void *rtn
)
3544 static bool svm_cpu_has_accelerated_tpr(void)
3549 static u64
svm_get_mt_mask(struct kvm_vcpu
*vcpu
, gfn_t gfn
, bool is_mmio
)
3554 static void svm_cpuid_update(struct kvm_vcpu
*vcpu
)
3558 static void svm_set_supported_cpuid(u32 func
, struct kvm_cpuid_entry2
*entry
)
3562 /* Mask out xsave bit as long as it is not supported by SVM */
3563 entry
->ecx
&= ~(bit(X86_FEATURE_XSAVE
));
3567 entry
->ecx
|= (1 << 2); /* Set SVM bit */
3570 entry
->eax
= 1; /* SVM revision 1 */
3571 entry
->ebx
= 8; /* Lets support 8 ASIDs in case we add proper
3572 ASID emulation to nested SVM */
3573 entry
->ecx
= 0; /* Reserved */
3574 entry
->edx
= 0; /* Per default do not support any
3575 additional features */
3577 /* Support next_rip if host supports it */
3578 if (boot_cpu_has(X86_FEATURE_NRIPS
))
3579 entry
->edx
|= SVM_FEATURE_NRIP
;
3581 /* Support NPT for the guest if enabled */
3583 entry
->edx
|= SVM_FEATURE_NPT
;
3589 static const struct trace_print_flags svm_exit_reasons_str
[] = {
3590 { SVM_EXIT_READ_CR0
, "read_cr0" },
3591 { SVM_EXIT_READ_CR3
, "read_cr3" },
3592 { SVM_EXIT_READ_CR4
, "read_cr4" },
3593 { SVM_EXIT_READ_CR8
, "read_cr8" },
3594 { SVM_EXIT_WRITE_CR0
, "write_cr0" },
3595 { SVM_EXIT_WRITE_CR3
, "write_cr3" },
3596 { SVM_EXIT_WRITE_CR4
, "write_cr4" },
3597 { SVM_EXIT_WRITE_CR8
, "write_cr8" },
3598 { SVM_EXIT_READ_DR0
, "read_dr0" },
3599 { SVM_EXIT_READ_DR1
, "read_dr1" },
3600 { SVM_EXIT_READ_DR2
, "read_dr2" },
3601 { SVM_EXIT_READ_DR3
, "read_dr3" },
3602 { SVM_EXIT_WRITE_DR0
, "write_dr0" },
3603 { SVM_EXIT_WRITE_DR1
, "write_dr1" },
3604 { SVM_EXIT_WRITE_DR2
, "write_dr2" },
3605 { SVM_EXIT_WRITE_DR3
, "write_dr3" },
3606 { SVM_EXIT_WRITE_DR5
, "write_dr5" },
3607 { SVM_EXIT_WRITE_DR7
, "write_dr7" },
3608 { SVM_EXIT_EXCP_BASE
+ DB_VECTOR
, "DB excp" },
3609 { SVM_EXIT_EXCP_BASE
+ BP_VECTOR
, "BP excp" },
3610 { SVM_EXIT_EXCP_BASE
+ UD_VECTOR
, "UD excp" },
3611 { SVM_EXIT_EXCP_BASE
+ PF_VECTOR
, "PF excp" },
3612 { SVM_EXIT_EXCP_BASE
+ NM_VECTOR
, "NM excp" },
3613 { SVM_EXIT_EXCP_BASE
+ MC_VECTOR
, "MC excp" },
3614 { SVM_EXIT_INTR
, "interrupt" },
3615 { SVM_EXIT_NMI
, "nmi" },
3616 { SVM_EXIT_SMI
, "smi" },
3617 { SVM_EXIT_INIT
, "init" },
3618 { SVM_EXIT_VINTR
, "vintr" },
3619 { SVM_EXIT_CPUID
, "cpuid" },
3620 { SVM_EXIT_INVD
, "invd" },
3621 { SVM_EXIT_HLT
, "hlt" },
3622 { SVM_EXIT_INVLPG
, "invlpg" },
3623 { SVM_EXIT_INVLPGA
, "invlpga" },
3624 { SVM_EXIT_IOIO
, "io" },
3625 { SVM_EXIT_MSR
, "msr" },
3626 { SVM_EXIT_TASK_SWITCH
, "task_switch" },
3627 { SVM_EXIT_SHUTDOWN
, "shutdown" },
3628 { SVM_EXIT_VMRUN
, "vmrun" },
3629 { SVM_EXIT_VMMCALL
, "hypercall" },
3630 { SVM_EXIT_VMLOAD
, "vmload" },
3631 { SVM_EXIT_VMSAVE
, "vmsave" },
3632 { SVM_EXIT_STGI
, "stgi" },
3633 { SVM_EXIT_CLGI
, "clgi" },
3634 { SVM_EXIT_SKINIT
, "skinit" },
3635 { SVM_EXIT_WBINVD
, "wbinvd" },
3636 { SVM_EXIT_MONITOR
, "monitor" },
3637 { SVM_EXIT_MWAIT
, "mwait" },
3638 { SVM_EXIT_NPF
, "npf" },
3642 static int svm_get_lpage_level(void)
3644 return PT_PDPE_LEVEL
;
3647 static bool svm_rdtscp_supported(void)
3652 static bool svm_has_wbinvd_exit(void)
3657 static void svm_fpu_deactivate(struct kvm_vcpu
*vcpu
)
3659 struct vcpu_svm
*svm
= to_svm(vcpu
);
3661 set_exception_intercept(svm
, NM_VECTOR
);
3662 update_cr0_intercept(svm
);
3665 static struct kvm_x86_ops svm_x86_ops
= {
3666 .cpu_has_kvm_support
= has_svm
,
3667 .disabled_by_bios
= is_disabled
,
3668 .hardware_setup
= svm_hardware_setup
,
3669 .hardware_unsetup
= svm_hardware_unsetup
,
3670 .check_processor_compatibility
= svm_check_processor_compat
,
3671 .hardware_enable
= svm_hardware_enable
,
3672 .hardware_disable
= svm_hardware_disable
,
3673 .cpu_has_accelerated_tpr
= svm_cpu_has_accelerated_tpr
,
3675 .vcpu_create
= svm_create_vcpu
,
3676 .vcpu_free
= svm_free_vcpu
,
3677 .vcpu_reset
= svm_vcpu_reset
,
3679 .prepare_guest_switch
= svm_prepare_guest_switch
,
3680 .vcpu_load
= svm_vcpu_load
,
3681 .vcpu_put
= svm_vcpu_put
,
3683 .set_guest_debug
= svm_guest_debug
,
3684 .get_msr
= svm_get_msr
,
3685 .set_msr
= svm_set_msr
,
3686 .get_segment_base
= svm_get_segment_base
,
3687 .get_segment
= svm_get_segment
,
3688 .set_segment
= svm_set_segment
,
3689 .get_cpl
= svm_get_cpl
,
3690 .get_cs_db_l_bits
= kvm_get_cs_db_l_bits
,
3691 .decache_cr0_guest_bits
= svm_decache_cr0_guest_bits
,
3692 .decache_cr4_guest_bits
= svm_decache_cr4_guest_bits
,
3693 .set_cr0
= svm_set_cr0
,
3694 .set_cr3
= svm_set_cr3
,
3695 .set_cr4
= svm_set_cr4
,
3696 .set_efer
= svm_set_efer
,
3697 .get_idt
= svm_get_idt
,
3698 .set_idt
= svm_set_idt
,
3699 .get_gdt
= svm_get_gdt
,
3700 .set_gdt
= svm_set_gdt
,
3701 .set_dr7
= svm_set_dr7
,
3702 .cache_reg
= svm_cache_reg
,
3703 .get_rflags
= svm_get_rflags
,
3704 .set_rflags
= svm_set_rflags
,
3705 .fpu_activate
= svm_fpu_activate
,
3706 .fpu_deactivate
= svm_fpu_deactivate
,
3708 .tlb_flush
= svm_flush_tlb
,
3710 .run
= svm_vcpu_run
,
3711 .handle_exit
= handle_exit
,
3712 .skip_emulated_instruction
= skip_emulated_instruction
,
3713 .set_interrupt_shadow
= svm_set_interrupt_shadow
,
3714 .get_interrupt_shadow
= svm_get_interrupt_shadow
,
3715 .patch_hypercall
= svm_patch_hypercall
,
3716 .set_irq
= svm_set_irq
,
3717 .set_nmi
= svm_inject_nmi
,
3718 .queue_exception
= svm_queue_exception
,
3719 .cancel_injection
= svm_cancel_injection
,
3720 .interrupt_allowed
= svm_interrupt_allowed
,
3721 .nmi_allowed
= svm_nmi_allowed
,
3722 .get_nmi_mask
= svm_get_nmi_mask
,
3723 .set_nmi_mask
= svm_set_nmi_mask
,
3724 .enable_nmi_window
= enable_nmi_window
,
3725 .enable_irq_window
= enable_irq_window
,
3726 .update_cr8_intercept
= update_cr8_intercept
,
3728 .set_tss_addr
= svm_set_tss_addr
,
3729 .get_tdp_level
= get_npt_level
,
3730 .get_mt_mask
= svm_get_mt_mask
,
3732 .get_exit_info
= svm_get_exit_info
,
3733 .exit_reasons_str
= svm_exit_reasons_str
,
3735 .get_lpage_level
= svm_get_lpage_level
,
3737 .cpuid_update
= svm_cpuid_update
,
3739 .rdtscp_supported
= svm_rdtscp_supported
,
3741 .set_supported_cpuid
= svm_set_supported_cpuid
,
3743 .has_wbinvd_exit
= svm_has_wbinvd_exit
,
3745 .write_tsc_offset
= svm_write_tsc_offset
,
3746 .adjust_tsc_offset
= svm_adjust_tsc_offset
,
3748 .set_tdp_cr3
= set_tdp_cr3
,
3751 static int __init
svm_init(void)
3753 return kvm_init(&svm_x86_ops
, sizeof(struct vcpu_svm
),
3754 __alignof__(struct vcpu_svm
), THIS_MODULE
);
3757 static void __exit
svm_exit(void)
3762 module_init(svm_init
)
3763 module_exit(svm_exit
)