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 enable_gif(struct vcpu_svm
*svm
)
264 svm
->vcpu
.arch
.hflags
|= HF_GIF_MASK
;
267 static inline void disable_gif(struct vcpu_svm
*svm
)
269 svm
->vcpu
.arch
.hflags
&= ~HF_GIF_MASK
;
272 static inline bool gif_set(struct vcpu_svm
*svm
)
274 return !!(svm
->vcpu
.arch
.hflags
& HF_GIF_MASK
);
277 static unsigned long iopm_base
;
279 struct kvm_ldttss_desc
{
282 unsigned base1
:8, type
:5, dpl
:2, p
:1;
283 unsigned limit1
:4, zero0
:3, g
:1, base2
:8;
286 } __attribute__((packed
));
288 struct svm_cpu_data
{
294 struct kvm_ldttss_desc
*tss_desc
;
296 struct page
*save_area
;
299 static DEFINE_PER_CPU(struct svm_cpu_data
*, svm_data
);
300 static uint32_t svm_features
;
302 struct svm_init_data
{
307 static u32 msrpm_ranges
[] = {0, 0xc0000000, 0xc0010000};
309 #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
310 #define MSRS_RANGE_SIZE 2048
311 #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
313 static u32
svm_msrpm_offset(u32 msr
)
318 for (i
= 0; i
< NUM_MSR_MAPS
; i
++) {
319 if (msr
< msrpm_ranges
[i
] ||
320 msr
>= msrpm_ranges
[i
] + MSRS_IN_RANGE
)
323 offset
= (msr
- msrpm_ranges
[i
]) / 4; /* 4 msrs per u8 */
324 offset
+= (i
* MSRS_RANGE_SIZE
); /* add range offset */
326 /* Now we have the u8 offset - but need the u32 offset */
330 /* MSR not in any range */
334 #define MAX_INST_SIZE 15
336 static inline void clgi(void)
338 asm volatile (__ex(SVM_CLGI
));
341 static inline void stgi(void)
343 asm volatile (__ex(SVM_STGI
));
346 static inline void invlpga(unsigned long addr
, u32 asid
)
348 asm volatile (__ex(SVM_INVLPGA
) : : "a"(addr
), "c"(asid
));
351 static inline void force_new_asid(struct kvm_vcpu
*vcpu
)
353 to_svm(vcpu
)->asid_generation
--;
356 static inline void flush_guest_tlb(struct kvm_vcpu
*vcpu
)
358 force_new_asid(vcpu
);
361 static int get_npt_level(void)
364 return PT64_ROOT_LEVEL
;
366 return PT32E_ROOT_LEVEL
;
370 static void svm_set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
372 vcpu
->arch
.efer
= efer
;
373 if (!npt_enabled
&& !(efer
& EFER_LMA
))
376 to_svm(vcpu
)->vmcb
->save
.efer
= efer
| EFER_SVME
;
379 static int is_external_interrupt(u32 info
)
381 info
&= SVM_EVTINJ_TYPE_MASK
| SVM_EVTINJ_VALID
;
382 return info
== (SVM_EVTINJ_VALID
| SVM_EVTINJ_TYPE_INTR
);
385 static u32
svm_get_interrupt_shadow(struct kvm_vcpu
*vcpu
, int mask
)
387 struct vcpu_svm
*svm
= to_svm(vcpu
);
390 if (svm
->vmcb
->control
.int_state
& SVM_INTERRUPT_SHADOW_MASK
)
391 ret
|= KVM_X86_SHADOW_INT_STI
| KVM_X86_SHADOW_INT_MOV_SS
;
395 static void svm_set_interrupt_shadow(struct kvm_vcpu
*vcpu
, int mask
)
397 struct vcpu_svm
*svm
= to_svm(vcpu
);
400 svm
->vmcb
->control
.int_state
&= ~SVM_INTERRUPT_SHADOW_MASK
;
402 svm
->vmcb
->control
.int_state
|= SVM_INTERRUPT_SHADOW_MASK
;
406 static void skip_emulated_instruction(struct kvm_vcpu
*vcpu
)
408 struct vcpu_svm
*svm
= to_svm(vcpu
);
410 if (svm
->vmcb
->control
.next_rip
!= 0)
411 svm
->next_rip
= svm
->vmcb
->control
.next_rip
;
413 if (!svm
->next_rip
) {
414 if (emulate_instruction(vcpu
, 0, 0, EMULTYPE_SKIP
) !=
416 printk(KERN_DEBUG
"%s: NOP\n", __func__
);
419 if (svm
->next_rip
- kvm_rip_read(vcpu
) > MAX_INST_SIZE
)
420 printk(KERN_ERR
"%s: ip 0x%lx next 0x%llx\n",
421 __func__
, kvm_rip_read(vcpu
), svm
->next_rip
);
423 kvm_rip_write(vcpu
, svm
->next_rip
);
424 svm_set_interrupt_shadow(vcpu
, 0);
427 static void svm_queue_exception(struct kvm_vcpu
*vcpu
, unsigned nr
,
428 bool has_error_code
, u32 error_code
,
431 struct vcpu_svm
*svm
= to_svm(vcpu
);
434 * If we are within a nested VM we'd better #VMEXIT and let the guest
435 * handle the exception
438 nested_svm_check_exception(svm
, nr
, has_error_code
, error_code
))
441 if (nr
== BP_VECTOR
&& !static_cpu_has(X86_FEATURE_NRIPS
)) {
442 unsigned long rip
, old_rip
= kvm_rip_read(&svm
->vcpu
);
445 * For guest debugging where we have to reinject #BP if some
446 * INT3 is guest-owned:
447 * Emulate nRIP by moving RIP forward. Will fail if injection
448 * raises a fault that is not intercepted. Still better than
449 * failing in all cases.
451 skip_emulated_instruction(&svm
->vcpu
);
452 rip
= kvm_rip_read(&svm
->vcpu
);
453 svm
->int3_rip
= rip
+ svm
->vmcb
->save
.cs
.base
;
454 svm
->int3_injected
= rip
- old_rip
;
457 svm
->vmcb
->control
.event_inj
= nr
459 | (has_error_code
? SVM_EVTINJ_VALID_ERR
: 0)
460 | SVM_EVTINJ_TYPE_EXEPT
;
461 svm
->vmcb
->control
.event_inj_err
= error_code
;
464 static void svm_init_erratum_383(void)
470 if (!cpu_has_amd_erratum(amd_erratum_383
))
473 /* Use _safe variants to not break nested virtualization */
474 val
= native_read_msr_safe(MSR_AMD64_DC_CFG
, &err
);
480 low
= lower_32_bits(val
);
481 high
= upper_32_bits(val
);
483 native_write_msr_safe(MSR_AMD64_DC_CFG
, low
, high
);
485 erratum_383_found
= true;
488 static int has_svm(void)
492 if (!cpu_has_svm(&msg
)) {
493 printk(KERN_INFO
"has_svm: %s\n", msg
);
500 static void svm_hardware_disable(void *garbage
)
505 static int svm_hardware_enable(void *garbage
)
508 struct svm_cpu_data
*sd
;
510 struct desc_ptr gdt_descr
;
511 struct desc_struct
*gdt
;
512 int me
= raw_smp_processor_id();
514 rdmsrl(MSR_EFER
, efer
);
515 if (efer
& EFER_SVME
)
519 printk(KERN_ERR
"svm_hardware_enable: err EOPNOTSUPP on %d\n",
523 sd
= per_cpu(svm_data
, me
);
526 printk(KERN_ERR
"svm_hardware_enable: svm_data is NULL on %d\n",
531 sd
->asid_generation
= 1;
532 sd
->max_asid
= cpuid_ebx(SVM_CPUID_FUNC
) - 1;
533 sd
->next_asid
= sd
->max_asid
+ 1;
535 native_store_gdt(&gdt_descr
);
536 gdt
= (struct desc_struct
*)gdt_descr
.address
;
537 sd
->tss_desc
= (struct kvm_ldttss_desc
*)(gdt
+ GDT_ENTRY_TSS
);
539 wrmsrl(MSR_EFER
, efer
| EFER_SVME
);
541 wrmsrl(MSR_VM_HSAVE_PA
, page_to_pfn(sd
->save_area
) << PAGE_SHIFT
);
543 svm_init_erratum_383();
548 static void svm_cpu_uninit(int cpu
)
550 struct svm_cpu_data
*sd
= per_cpu(svm_data
, raw_smp_processor_id());
555 per_cpu(svm_data
, raw_smp_processor_id()) = NULL
;
556 __free_page(sd
->save_area
);
560 static int svm_cpu_init(int cpu
)
562 struct svm_cpu_data
*sd
;
565 sd
= kzalloc(sizeof(struct svm_cpu_data
), GFP_KERNEL
);
569 sd
->save_area
= alloc_page(GFP_KERNEL
);
574 per_cpu(svm_data
, cpu
) = sd
;
584 static bool valid_msr_intercept(u32 index
)
588 for (i
= 0; direct_access_msrs
[i
].index
!= MSR_INVALID
; i
++)
589 if (direct_access_msrs
[i
].index
== index
)
595 static void set_msr_interception(u32
*msrpm
, unsigned msr
,
598 u8 bit_read
, bit_write
;
603 * If this warning triggers extend the direct_access_msrs list at the
604 * beginning of the file
606 WARN_ON(!valid_msr_intercept(msr
));
608 offset
= svm_msrpm_offset(msr
);
609 bit_read
= 2 * (msr
& 0x0f);
610 bit_write
= 2 * (msr
& 0x0f) + 1;
613 BUG_ON(offset
== MSR_INVALID
);
615 read
? clear_bit(bit_read
, &tmp
) : set_bit(bit_read
, &tmp
);
616 write
? clear_bit(bit_write
, &tmp
) : set_bit(bit_write
, &tmp
);
621 static void svm_vcpu_init_msrpm(u32
*msrpm
)
625 memset(msrpm
, 0xff, PAGE_SIZE
* (1 << MSRPM_ALLOC_ORDER
));
627 for (i
= 0; direct_access_msrs
[i
].index
!= MSR_INVALID
; i
++) {
628 if (!direct_access_msrs
[i
].always
)
631 set_msr_interception(msrpm
, direct_access_msrs
[i
].index
, 1, 1);
635 static void add_msr_offset(u32 offset
)
639 for (i
= 0; i
< MSRPM_OFFSETS
; ++i
) {
641 /* Offset already in list? */
642 if (msrpm_offsets
[i
] == offset
)
645 /* Slot used by another offset? */
646 if (msrpm_offsets
[i
] != MSR_INVALID
)
649 /* Add offset to list */
650 msrpm_offsets
[i
] = offset
;
656 * If this BUG triggers the msrpm_offsets table has an overflow. Just
657 * increase MSRPM_OFFSETS in this case.
662 static void init_msrpm_offsets(void)
666 memset(msrpm_offsets
, 0xff, sizeof(msrpm_offsets
));
668 for (i
= 0; direct_access_msrs
[i
].index
!= MSR_INVALID
; i
++) {
671 offset
= svm_msrpm_offset(direct_access_msrs
[i
].index
);
672 BUG_ON(offset
== MSR_INVALID
);
674 add_msr_offset(offset
);
678 static void svm_enable_lbrv(struct vcpu_svm
*svm
)
680 u32
*msrpm
= svm
->msrpm
;
682 svm
->vmcb
->control
.lbr_ctl
= 1;
683 set_msr_interception(msrpm
, MSR_IA32_LASTBRANCHFROMIP
, 1, 1);
684 set_msr_interception(msrpm
, MSR_IA32_LASTBRANCHTOIP
, 1, 1);
685 set_msr_interception(msrpm
, MSR_IA32_LASTINTFROMIP
, 1, 1);
686 set_msr_interception(msrpm
, MSR_IA32_LASTINTTOIP
, 1, 1);
689 static void svm_disable_lbrv(struct vcpu_svm
*svm
)
691 u32
*msrpm
= svm
->msrpm
;
693 svm
->vmcb
->control
.lbr_ctl
= 0;
694 set_msr_interception(msrpm
, MSR_IA32_LASTBRANCHFROMIP
, 0, 0);
695 set_msr_interception(msrpm
, MSR_IA32_LASTBRANCHTOIP
, 0, 0);
696 set_msr_interception(msrpm
, MSR_IA32_LASTINTFROMIP
, 0, 0);
697 set_msr_interception(msrpm
, MSR_IA32_LASTINTTOIP
, 0, 0);
700 static __init
int svm_hardware_setup(void)
703 struct page
*iopm_pages
;
707 iopm_pages
= alloc_pages(GFP_KERNEL
, IOPM_ALLOC_ORDER
);
712 iopm_va
= page_address(iopm_pages
);
713 memset(iopm_va
, 0xff, PAGE_SIZE
* (1 << IOPM_ALLOC_ORDER
));
714 iopm_base
= page_to_pfn(iopm_pages
) << PAGE_SHIFT
;
716 init_msrpm_offsets();
718 if (boot_cpu_has(X86_FEATURE_NX
))
719 kvm_enable_efer_bits(EFER_NX
);
721 if (boot_cpu_has(X86_FEATURE_FXSR_OPT
))
722 kvm_enable_efer_bits(EFER_FFXSR
);
725 printk(KERN_INFO
"kvm: Nested Virtualization enabled\n");
726 kvm_enable_efer_bits(EFER_SVME
| EFER_LMSLE
);
729 for_each_possible_cpu(cpu
) {
730 r
= svm_cpu_init(cpu
);
735 svm_features
= cpuid_edx(SVM_CPUID_FUNC
);
737 if (!boot_cpu_has(X86_FEATURE_NPT
))
740 if (npt_enabled
&& !npt
) {
741 printk(KERN_INFO
"kvm: Nested Paging disabled\n");
746 printk(KERN_INFO
"kvm: Nested Paging enabled\n");
754 __free_pages(iopm_pages
, IOPM_ALLOC_ORDER
);
759 static __exit
void svm_hardware_unsetup(void)
763 for_each_possible_cpu(cpu
)
766 __free_pages(pfn_to_page(iopm_base
>> PAGE_SHIFT
), IOPM_ALLOC_ORDER
);
770 static void init_seg(struct vmcb_seg
*seg
)
773 seg
->attrib
= SVM_SELECTOR_P_MASK
| SVM_SELECTOR_S_MASK
|
774 SVM_SELECTOR_WRITE_MASK
; /* Read/Write Data Segment */
779 static void init_sys_seg(struct vmcb_seg
*seg
, uint32_t type
)
782 seg
->attrib
= SVM_SELECTOR_P_MASK
| type
;
787 static void svm_write_tsc_offset(struct kvm_vcpu
*vcpu
, u64 offset
)
789 struct vcpu_svm
*svm
= to_svm(vcpu
);
790 u64 g_tsc_offset
= 0;
792 if (is_guest_mode(vcpu
)) {
793 g_tsc_offset
= svm
->vmcb
->control
.tsc_offset
-
794 svm
->nested
.hsave
->control
.tsc_offset
;
795 svm
->nested
.hsave
->control
.tsc_offset
= offset
;
798 svm
->vmcb
->control
.tsc_offset
= offset
+ g_tsc_offset
;
801 static void svm_adjust_tsc_offset(struct kvm_vcpu
*vcpu
, s64 adjustment
)
803 struct vcpu_svm
*svm
= to_svm(vcpu
);
805 svm
->vmcb
->control
.tsc_offset
+= adjustment
;
806 if (is_guest_mode(vcpu
))
807 svm
->nested
.hsave
->control
.tsc_offset
+= adjustment
;
810 static void init_vmcb(struct vcpu_svm
*svm
)
812 struct vmcb_control_area
*control
= &svm
->vmcb
->control
;
813 struct vmcb_save_area
*save
= &svm
->vmcb
->save
;
815 svm
->vcpu
.fpu_active
= 1;
816 svm
->vcpu
.arch
.hflags
= 0;
818 set_cr_intercept(svm
, INTERCEPT_CR0_READ
);
819 set_cr_intercept(svm
, INTERCEPT_CR3_READ
);
820 set_cr_intercept(svm
, INTERCEPT_CR4_READ
);
821 set_cr_intercept(svm
, INTERCEPT_CR0_WRITE
);
822 set_cr_intercept(svm
, INTERCEPT_CR3_WRITE
);
823 set_cr_intercept(svm
, INTERCEPT_CR4_WRITE
);
824 set_cr_intercept(svm
, INTERCEPT_CR8_WRITE
);
826 set_dr_intercept(svm
, INTERCEPT_DR0_READ
);
827 set_dr_intercept(svm
, INTERCEPT_DR1_READ
);
828 set_dr_intercept(svm
, INTERCEPT_DR2_READ
);
829 set_dr_intercept(svm
, INTERCEPT_DR3_READ
);
830 set_dr_intercept(svm
, INTERCEPT_DR4_READ
);
831 set_dr_intercept(svm
, INTERCEPT_DR5_READ
);
832 set_dr_intercept(svm
, INTERCEPT_DR6_READ
);
833 set_dr_intercept(svm
, INTERCEPT_DR7_READ
);
835 set_dr_intercept(svm
, INTERCEPT_DR0_WRITE
);
836 set_dr_intercept(svm
, INTERCEPT_DR1_WRITE
);
837 set_dr_intercept(svm
, INTERCEPT_DR2_WRITE
);
838 set_dr_intercept(svm
, INTERCEPT_DR3_WRITE
);
839 set_dr_intercept(svm
, INTERCEPT_DR4_WRITE
);
840 set_dr_intercept(svm
, INTERCEPT_DR5_WRITE
);
841 set_dr_intercept(svm
, INTERCEPT_DR6_WRITE
);
842 set_dr_intercept(svm
, INTERCEPT_DR7_WRITE
);
844 control
->intercept_exceptions
= (1 << PF_VECTOR
) |
849 control
->intercept
= (1ULL << INTERCEPT_INTR
) |
850 (1ULL << INTERCEPT_NMI
) |
851 (1ULL << INTERCEPT_SMI
) |
852 (1ULL << INTERCEPT_SELECTIVE_CR0
) |
853 (1ULL << INTERCEPT_CPUID
) |
854 (1ULL << INTERCEPT_INVD
) |
855 (1ULL << INTERCEPT_HLT
) |
856 (1ULL << INTERCEPT_INVLPG
) |
857 (1ULL << INTERCEPT_INVLPGA
) |
858 (1ULL << INTERCEPT_IOIO_PROT
) |
859 (1ULL << INTERCEPT_MSR_PROT
) |
860 (1ULL << INTERCEPT_TASK_SWITCH
) |
861 (1ULL << INTERCEPT_SHUTDOWN
) |
862 (1ULL << INTERCEPT_VMRUN
) |
863 (1ULL << INTERCEPT_VMMCALL
) |
864 (1ULL << INTERCEPT_VMLOAD
) |
865 (1ULL << INTERCEPT_VMSAVE
) |
866 (1ULL << INTERCEPT_STGI
) |
867 (1ULL << INTERCEPT_CLGI
) |
868 (1ULL << INTERCEPT_SKINIT
) |
869 (1ULL << INTERCEPT_WBINVD
) |
870 (1ULL << INTERCEPT_MONITOR
) |
871 (1ULL << INTERCEPT_MWAIT
);
873 control
->iopm_base_pa
= iopm_base
;
874 control
->msrpm_base_pa
= __pa(svm
->msrpm
);
875 control
->int_ctl
= V_INTR_MASKING_MASK
;
883 save
->cs
.selector
= 0xf000;
884 /* Executable/Readable Code Segment */
885 save
->cs
.attrib
= SVM_SELECTOR_READ_MASK
| SVM_SELECTOR_P_MASK
|
886 SVM_SELECTOR_S_MASK
| SVM_SELECTOR_CODE_MASK
;
887 save
->cs
.limit
= 0xffff;
889 * cs.base should really be 0xffff0000, but vmx can't handle that, so
890 * be consistent with it.
892 * Replace when we have real mode working for vmx.
894 save
->cs
.base
= 0xf0000;
896 save
->gdtr
.limit
= 0xffff;
897 save
->idtr
.limit
= 0xffff;
899 init_sys_seg(&save
->ldtr
, SEG_TYPE_LDT
);
900 init_sys_seg(&save
->tr
, SEG_TYPE_BUSY_TSS16
);
902 svm_set_efer(&svm
->vcpu
, 0);
903 save
->dr6
= 0xffff0ff0;
906 save
->rip
= 0x0000fff0;
907 svm
->vcpu
.arch
.regs
[VCPU_REGS_RIP
] = save
->rip
;
910 * This is the guest-visible cr0 value.
911 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
913 svm
->vcpu
.arch
.cr0
= 0;
914 (void)kvm_set_cr0(&svm
->vcpu
, X86_CR0_NW
| X86_CR0_CD
| X86_CR0_ET
);
916 save
->cr4
= X86_CR4_PAE
;
920 /* Setup VMCB for Nested Paging */
921 control
->nested_ctl
= 1;
922 control
->intercept
&= ~((1ULL << INTERCEPT_TASK_SWITCH
) |
923 (1ULL << INTERCEPT_INVLPG
));
924 control
->intercept_exceptions
&= ~(1 << PF_VECTOR
);
925 clr_cr_intercept(svm
, INTERCEPT_CR3_READ
);
926 clr_cr_intercept(svm
, INTERCEPT_CR3_WRITE
);
927 save
->g_pat
= 0x0007040600070406ULL
;
931 force_new_asid(&svm
->vcpu
);
933 svm
->nested
.vmcb
= 0;
934 svm
->vcpu
.arch
.hflags
= 0;
936 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER
)) {
937 control
->pause_filter_count
= 3000;
938 control
->intercept
|= (1ULL << INTERCEPT_PAUSE
);
944 static int svm_vcpu_reset(struct kvm_vcpu
*vcpu
)
946 struct vcpu_svm
*svm
= to_svm(vcpu
);
950 if (!kvm_vcpu_is_bsp(vcpu
)) {
951 kvm_rip_write(vcpu
, 0);
952 svm
->vmcb
->save
.cs
.base
= svm
->vcpu
.arch
.sipi_vector
<< 12;
953 svm
->vmcb
->save
.cs
.selector
= svm
->vcpu
.arch
.sipi_vector
<< 8;
955 vcpu
->arch
.regs_avail
= ~0;
956 vcpu
->arch
.regs_dirty
= ~0;
961 static struct kvm_vcpu
*svm_create_vcpu(struct kvm
*kvm
, unsigned int id
)
963 struct vcpu_svm
*svm
;
965 struct page
*msrpm_pages
;
966 struct page
*hsave_page
;
967 struct page
*nested_msrpm_pages
;
970 svm
= kmem_cache_zalloc(kvm_vcpu_cache
, GFP_KERNEL
);
976 err
= kvm_vcpu_init(&svm
->vcpu
, kvm
, id
);
981 page
= alloc_page(GFP_KERNEL
);
985 msrpm_pages
= alloc_pages(GFP_KERNEL
, MSRPM_ALLOC_ORDER
);
989 nested_msrpm_pages
= alloc_pages(GFP_KERNEL
, MSRPM_ALLOC_ORDER
);
990 if (!nested_msrpm_pages
)
993 hsave_page
= alloc_page(GFP_KERNEL
);
997 svm
->nested
.hsave
= page_address(hsave_page
);
999 svm
->msrpm
= page_address(msrpm_pages
);
1000 svm_vcpu_init_msrpm(svm
->msrpm
);
1002 svm
->nested
.msrpm
= page_address(nested_msrpm_pages
);
1003 svm_vcpu_init_msrpm(svm
->nested
.msrpm
);
1005 svm
->vmcb
= page_address(page
);
1006 clear_page(svm
->vmcb
);
1007 svm
->vmcb_pa
= page_to_pfn(page
) << PAGE_SHIFT
;
1008 svm
->asid_generation
= 0;
1010 kvm_write_tsc(&svm
->vcpu
, 0);
1012 err
= fx_init(&svm
->vcpu
);
1016 svm
->vcpu
.arch
.apic_base
= 0xfee00000 | MSR_IA32_APICBASE_ENABLE
;
1017 if (kvm_vcpu_is_bsp(&svm
->vcpu
))
1018 svm
->vcpu
.arch
.apic_base
|= MSR_IA32_APICBASE_BSP
;
1023 __free_page(hsave_page
);
1025 __free_pages(nested_msrpm_pages
, MSRPM_ALLOC_ORDER
);
1027 __free_pages(msrpm_pages
, MSRPM_ALLOC_ORDER
);
1031 kvm_vcpu_uninit(&svm
->vcpu
);
1033 kmem_cache_free(kvm_vcpu_cache
, svm
);
1035 return ERR_PTR(err
);
1038 static void svm_free_vcpu(struct kvm_vcpu
*vcpu
)
1040 struct vcpu_svm
*svm
= to_svm(vcpu
);
1042 __free_page(pfn_to_page(svm
->vmcb_pa
>> PAGE_SHIFT
));
1043 __free_pages(virt_to_page(svm
->msrpm
), MSRPM_ALLOC_ORDER
);
1044 __free_page(virt_to_page(svm
->nested
.hsave
));
1045 __free_pages(virt_to_page(svm
->nested
.msrpm
), MSRPM_ALLOC_ORDER
);
1046 kvm_vcpu_uninit(vcpu
);
1047 kmem_cache_free(kvm_vcpu_cache
, svm
);
1050 static void svm_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
1052 struct vcpu_svm
*svm
= to_svm(vcpu
);
1055 if (unlikely(cpu
!= vcpu
->cpu
)) {
1056 svm
->asid_generation
= 0;
1059 #ifdef CONFIG_X86_64
1060 rdmsrl(MSR_GS_BASE
, to_svm(vcpu
)->host
.gs_base
);
1062 savesegment(fs
, svm
->host
.fs
);
1063 savesegment(gs
, svm
->host
.gs
);
1064 svm
->host
.ldt
= kvm_read_ldt();
1066 for (i
= 0; i
< NR_HOST_SAVE_USER_MSRS
; i
++)
1067 rdmsrl(host_save_user_msrs
[i
], svm
->host_user_msrs
[i
]);
1070 static void svm_vcpu_put(struct kvm_vcpu
*vcpu
)
1072 struct vcpu_svm
*svm
= to_svm(vcpu
);
1075 ++vcpu
->stat
.host_state_reload
;
1076 kvm_load_ldt(svm
->host
.ldt
);
1077 #ifdef CONFIG_X86_64
1078 loadsegment(fs
, svm
->host
.fs
);
1079 load_gs_index(svm
->host
.gs
);
1080 wrmsrl(MSR_KERNEL_GS_BASE
, current
->thread
.gs
);
1082 loadsegment(gs
, svm
->host
.gs
);
1084 for (i
= 0; i
< NR_HOST_SAVE_USER_MSRS
; i
++)
1085 wrmsrl(host_save_user_msrs
[i
], svm
->host_user_msrs
[i
]);
1088 static unsigned long svm_get_rflags(struct kvm_vcpu
*vcpu
)
1090 return to_svm(vcpu
)->vmcb
->save
.rflags
;
1093 static void svm_set_rflags(struct kvm_vcpu
*vcpu
, unsigned long rflags
)
1095 to_svm(vcpu
)->vmcb
->save
.rflags
= rflags
;
1098 static void svm_cache_reg(struct kvm_vcpu
*vcpu
, enum kvm_reg reg
)
1101 case VCPU_EXREG_PDPTR
:
1102 BUG_ON(!npt_enabled
);
1103 load_pdptrs(vcpu
, vcpu
->arch
.walk_mmu
, vcpu
->arch
.cr3
);
1110 static void svm_set_vintr(struct vcpu_svm
*svm
)
1112 svm
->vmcb
->control
.intercept
|= 1ULL << INTERCEPT_VINTR
;
1115 static void svm_clear_vintr(struct vcpu_svm
*svm
)
1117 svm
->vmcb
->control
.intercept
&= ~(1ULL << INTERCEPT_VINTR
);
1120 static struct vmcb_seg
*svm_seg(struct kvm_vcpu
*vcpu
, int seg
)
1122 struct vmcb_save_area
*save
= &to_svm(vcpu
)->vmcb
->save
;
1125 case VCPU_SREG_CS
: return &save
->cs
;
1126 case VCPU_SREG_DS
: return &save
->ds
;
1127 case VCPU_SREG_ES
: return &save
->es
;
1128 case VCPU_SREG_FS
: return &save
->fs
;
1129 case VCPU_SREG_GS
: return &save
->gs
;
1130 case VCPU_SREG_SS
: return &save
->ss
;
1131 case VCPU_SREG_TR
: return &save
->tr
;
1132 case VCPU_SREG_LDTR
: return &save
->ldtr
;
1138 static u64
svm_get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
1140 struct vmcb_seg
*s
= svm_seg(vcpu
, seg
);
1145 static void svm_get_segment(struct kvm_vcpu
*vcpu
,
1146 struct kvm_segment
*var
, int seg
)
1148 struct vmcb_seg
*s
= svm_seg(vcpu
, seg
);
1150 var
->base
= s
->base
;
1151 var
->limit
= s
->limit
;
1152 var
->selector
= s
->selector
;
1153 var
->type
= s
->attrib
& SVM_SELECTOR_TYPE_MASK
;
1154 var
->s
= (s
->attrib
>> SVM_SELECTOR_S_SHIFT
) & 1;
1155 var
->dpl
= (s
->attrib
>> SVM_SELECTOR_DPL_SHIFT
) & 3;
1156 var
->present
= (s
->attrib
>> SVM_SELECTOR_P_SHIFT
) & 1;
1157 var
->avl
= (s
->attrib
>> SVM_SELECTOR_AVL_SHIFT
) & 1;
1158 var
->l
= (s
->attrib
>> SVM_SELECTOR_L_SHIFT
) & 1;
1159 var
->db
= (s
->attrib
>> SVM_SELECTOR_DB_SHIFT
) & 1;
1160 var
->g
= (s
->attrib
>> SVM_SELECTOR_G_SHIFT
) & 1;
1163 * AMD's VMCB does not have an explicit unusable field, so emulate it
1164 * for cross vendor migration purposes by "not present"
1166 var
->unusable
= !var
->present
|| (var
->type
== 0);
1171 * SVM always stores 0 for the 'G' bit in the CS selector in
1172 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1173 * Intel's VMENTRY has a check on the 'G' bit.
1175 var
->g
= s
->limit
> 0xfffff;
1179 * Work around a bug where the busy flag in the tr selector
1189 * The accessed bit must always be set in the segment
1190 * descriptor cache, although it can be cleared in the
1191 * descriptor, the cached bit always remains at 1. Since
1192 * Intel has a check on this, set it here to support
1193 * cross-vendor migration.
1200 * On AMD CPUs sometimes the DB bit in the segment
1201 * descriptor is left as 1, although the whole segment has
1202 * been made unusable. Clear it here to pass an Intel VMX
1203 * entry check when cross vendor migrating.
1211 static int svm_get_cpl(struct kvm_vcpu
*vcpu
)
1213 struct vmcb_save_area
*save
= &to_svm(vcpu
)->vmcb
->save
;
1218 static void svm_get_idt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
1220 struct vcpu_svm
*svm
= to_svm(vcpu
);
1222 dt
->size
= svm
->vmcb
->save
.idtr
.limit
;
1223 dt
->address
= svm
->vmcb
->save
.idtr
.base
;
1226 static void svm_set_idt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
1228 struct vcpu_svm
*svm
= to_svm(vcpu
);
1230 svm
->vmcb
->save
.idtr
.limit
= dt
->size
;
1231 svm
->vmcb
->save
.idtr
.base
= dt
->address
;
1234 static void svm_get_gdt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
1236 struct vcpu_svm
*svm
= to_svm(vcpu
);
1238 dt
->size
= svm
->vmcb
->save
.gdtr
.limit
;
1239 dt
->address
= svm
->vmcb
->save
.gdtr
.base
;
1242 static void svm_set_gdt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
1244 struct vcpu_svm
*svm
= to_svm(vcpu
);
1246 svm
->vmcb
->save
.gdtr
.limit
= dt
->size
;
1247 svm
->vmcb
->save
.gdtr
.base
= dt
->address
;
1250 static void svm_decache_cr0_guest_bits(struct kvm_vcpu
*vcpu
)
1254 static void svm_decache_cr4_guest_bits(struct kvm_vcpu
*vcpu
)
1258 static void update_cr0_intercept(struct vcpu_svm
*svm
)
1260 ulong gcr0
= svm
->vcpu
.arch
.cr0
;
1261 u64
*hcr0
= &svm
->vmcb
->save
.cr0
;
1263 if (!svm
->vcpu
.fpu_active
)
1264 *hcr0
|= SVM_CR0_SELECTIVE_MASK
;
1266 *hcr0
= (*hcr0
& ~SVM_CR0_SELECTIVE_MASK
)
1267 | (gcr0
& SVM_CR0_SELECTIVE_MASK
);
1270 if (gcr0
== *hcr0
&& svm
->vcpu
.fpu_active
) {
1271 clr_cr_intercept(svm
, INTERCEPT_CR0_READ
);
1272 clr_cr_intercept(svm
, INTERCEPT_CR0_WRITE
);
1274 set_cr_intercept(svm
, INTERCEPT_CR0_READ
);
1275 set_cr_intercept(svm
, INTERCEPT_CR0_WRITE
);
1279 static void svm_set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
1281 struct vcpu_svm
*svm
= to_svm(vcpu
);
1283 if (is_guest_mode(vcpu
)) {
1285 * We are here because we run in nested mode, the host kvm
1286 * intercepts cr0 writes but the l1 hypervisor does not.
1287 * But the L1 hypervisor may intercept selective cr0 writes.
1288 * This needs to be checked here.
1290 unsigned long old
, new;
1292 /* Remove bits that would trigger a real cr0 write intercept */
1293 old
= vcpu
->arch
.cr0
& SVM_CR0_SELECTIVE_MASK
;
1294 new = cr0
& SVM_CR0_SELECTIVE_MASK
;
1297 /* cr0 write with ts and mp unchanged */
1298 svm
->vmcb
->control
.exit_code
= SVM_EXIT_CR0_SEL_WRITE
;
1299 if (nested_svm_exit_handled(svm
) == NESTED_EXIT_DONE
) {
1300 svm
->nested
.vmexit_rip
= kvm_rip_read(vcpu
);
1301 svm
->nested
.vmexit_rsp
= kvm_register_read(vcpu
, VCPU_REGS_RSP
);
1302 svm
->nested
.vmexit_rax
= kvm_register_read(vcpu
, VCPU_REGS_RAX
);
1308 #ifdef CONFIG_X86_64
1309 if (vcpu
->arch
.efer
& EFER_LME
) {
1310 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
)) {
1311 vcpu
->arch
.efer
|= EFER_LMA
;
1312 svm
->vmcb
->save
.efer
|= EFER_LMA
| EFER_LME
;
1315 if (is_paging(vcpu
) && !(cr0
& X86_CR0_PG
)) {
1316 vcpu
->arch
.efer
&= ~EFER_LMA
;
1317 svm
->vmcb
->save
.efer
&= ~(EFER_LMA
| EFER_LME
);
1321 vcpu
->arch
.cr0
= cr0
;
1324 cr0
|= X86_CR0_PG
| X86_CR0_WP
;
1326 if (!vcpu
->fpu_active
)
1329 * re-enable caching here because the QEMU bios
1330 * does not do it - this results in some delay at
1333 cr0
&= ~(X86_CR0_CD
| X86_CR0_NW
);
1334 svm
->vmcb
->save
.cr0
= cr0
;
1335 update_cr0_intercept(svm
);
1338 static void svm_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
1340 unsigned long host_cr4_mce
= read_cr4() & X86_CR4_MCE
;
1341 unsigned long old_cr4
= to_svm(vcpu
)->vmcb
->save
.cr4
;
1343 if (npt_enabled
&& ((old_cr4
^ cr4
) & X86_CR4_PGE
))
1344 force_new_asid(vcpu
);
1346 vcpu
->arch
.cr4
= cr4
;
1349 cr4
|= host_cr4_mce
;
1350 to_svm(vcpu
)->vmcb
->save
.cr4
= cr4
;
1353 static void svm_set_segment(struct kvm_vcpu
*vcpu
,
1354 struct kvm_segment
*var
, int seg
)
1356 struct vcpu_svm
*svm
= to_svm(vcpu
);
1357 struct vmcb_seg
*s
= svm_seg(vcpu
, seg
);
1359 s
->base
= var
->base
;
1360 s
->limit
= var
->limit
;
1361 s
->selector
= var
->selector
;
1365 s
->attrib
= (var
->type
& SVM_SELECTOR_TYPE_MASK
);
1366 s
->attrib
|= (var
->s
& 1) << SVM_SELECTOR_S_SHIFT
;
1367 s
->attrib
|= (var
->dpl
& 3) << SVM_SELECTOR_DPL_SHIFT
;
1368 s
->attrib
|= (var
->present
& 1) << SVM_SELECTOR_P_SHIFT
;
1369 s
->attrib
|= (var
->avl
& 1) << SVM_SELECTOR_AVL_SHIFT
;
1370 s
->attrib
|= (var
->l
& 1) << SVM_SELECTOR_L_SHIFT
;
1371 s
->attrib
|= (var
->db
& 1) << SVM_SELECTOR_DB_SHIFT
;
1372 s
->attrib
|= (var
->g
& 1) << SVM_SELECTOR_G_SHIFT
;
1374 if (seg
== VCPU_SREG_CS
)
1376 = (svm
->vmcb
->save
.cs
.attrib
1377 >> SVM_SELECTOR_DPL_SHIFT
) & 3;
1381 static void update_db_intercept(struct kvm_vcpu
*vcpu
)
1383 struct vcpu_svm
*svm
= to_svm(vcpu
);
1385 svm
->vmcb
->control
.intercept_exceptions
&=
1386 ~((1 << DB_VECTOR
) | (1 << BP_VECTOR
));
1388 if (svm
->nmi_singlestep
)
1389 svm
->vmcb
->control
.intercept_exceptions
|= (1 << DB_VECTOR
);
1391 if (vcpu
->guest_debug
& KVM_GUESTDBG_ENABLE
) {
1392 if (vcpu
->guest_debug
&
1393 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
))
1394 svm
->vmcb
->control
.intercept_exceptions
|=
1396 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_SW_BP
)
1397 svm
->vmcb
->control
.intercept_exceptions
|=
1400 vcpu
->guest_debug
= 0;
1403 static void svm_guest_debug(struct kvm_vcpu
*vcpu
, struct kvm_guest_debug
*dbg
)
1405 struct vcpu_svm
*svm
= to_svm(vcpu
);
1407 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
)
1408 svm
->vmcb
->save
.dr7
= dbg
->arch
.debugreg
[7];
1410 svm
->vmcb
->save
.dr7
= vcpu
->arch
.dr7
;
1412 update_db_intercept(vcpu
);
1415 static void new_asid(struct vcpu_svm
*svm
, struct svm_cpu_data
*sd
)
1417 if (sd
->next_asid
> sd
->max_asid
) {
1418 ++sd
->asid_generation
;
1420 svm
->vmcb
->control
.tlb_ctl
= TLB_CONTROL_FLUSH_ALL_ASID
;
1423 svm
->asid_generation
= sd
->asid_generation
;
1424 svm
->vmcb
->control
.asid
= sd
->next_asid
++;
1427 static void svm_set_dr7(struct kvm_vcpu
*vcpu
, unsigned long value
)
1429 struct vcpu_svm
*svm
= to_svm(vcpu
);
1431 svm
->vmcb
->save
.dr7
= value
;
1434 static int pf_interception(struct vcpu_svm
*svm
)
1436 u64 fault_address
= svm
->vmcb
->control
.exit_info_2
;
1440 switch (svm
->apf_reason
) {
1442 error_code
= svm
->vmcb
->control
.exit_info_1
;
1444 trace_kvm_page_fault(fault_address
, error_code
);
1445 if (!npt_enabled
&& kvm_event_needs_reinjection(&svm
->vcpu
))
1446 kvm_mmu_unprotect_page_virt(&svm
->vcpu
, fault_address
);
1447 r
= kvm_mmu_page_fault(&svm
->vcpu
, fault_address
, error_code
);
1449 case KVM_PV_REASON_PAGE_NOT_PRESENT
:
1450 svm
->apf_reason
= 0;
1451 local_irq_disable();
1452 kvm_async_pf_task_wait(fault_address
);
1455 case KVM_PV_REASON_PAGE_READY
:
1456 svm
->apf_reason
= 0;
1457 local_irq_disable();
1458 kvm_async_pf_task_wake(fault_address
);
1465 static int db_interception(struct vcpu_svm
*svm
)
1467 struct kvm_run
*kvm_run
= svm
->vcpu
.run
;
1469 if (!(svm
->vcpu
.guest_debug
&
1470 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
)) &&
1471 !svm
->nmi_singlestep
) {
1472 kvm_queue_exception(&svm
->vcpu
, DB_VECTOR
);
1476 if (svm
->nmi_singlestep
) {
1477 svm
->nmi_singlestep
= false;
1478 if (!(svm
->vcpu
.guest_debug
& KVM_GUESTDBG_SINGLESTEP
))
1479 svm
->vmcb
->save
.rflags
&=
1480 ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
1481 update_db_intercept(&svm
->vcpu
);
1484 if (svm
->vcpu
.guest_debug
&
1485 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
)) {
1486 kvm_run
->exit_reason
= KVM_EXIT_DEBUG
;
1487 kvm_run
->debug
.arch
.pc
=
1488 svm
->vmcb
->save
.cs
.base
+ svm
->vmcb
->save
.rip
;
1489 kvm_run
->debug
.arch
.exception
= DB_VECTOR
;
1496 static int bp_interception(struct vcpu_svm
*svm
)
1498 struct kvm_run
*kvm_run
= svm
->vcpu
.run
;
1500 kvm_run
->exit_reason
= KVM_EXIT_DEBUG
;
1501 kvm_run
->debug
.arch
.pc
= svm
->vmcb
->save
.cs
.base
+ svm
->vmcb
->save
.rip
;
1502 kvm_run
->debug
.arch
.exception
= BP_VECTOR
;
1506 static int ud_interception(struct vcpu_svm
*svm
)
1510 er
= emulate_instruction(&svm
->vcpu
, 0, 0, EMULTYPE_TRAP_UD
);
1511 if (er
!= EMULATE_DONE
)
1512 kvm_queue_exception(&svm
->vcpu
, UD_VECTOR
);
1516 static void svm_fpu_activate(struct kvm_vcpu
*vcpu
)
1518 struct vcpu_svm
*svm
= to_svm(vcpu
);
1521 if (is_guest_mode(vcpu
)) {
1524 h_excp
= svm
->nested
.hsave
->control
.intercept_exceptions
;
1525 n_excp
= svm
->nested
.intercept_exceptions
;
1526 h_excp
&= ~(1 << NM_VECTOR
);
1527 excp
= h_excp
| n_excp
;
1529 excp
= svm
->vmcb
->control
.intercept_exceptions
;
1530 excp
&= ~(1 << NM_VECTOR
);
1533 svm
->vmcb
->control
.intercept_exceptions
= excp
;
1535 svm
->vcpu
.fpu_active
= 1;
1536 update_cr0_intercept(svm
);
1539 static int nm_interception(struct vcpu_svm
*svm
)
1541 svm_fpu_activate(&svm
->vcpu
);
1545 static bool is_erratum_383(void)
1550 if (!erratum_383_found
)
1553 value
= native_read_msr_safe(MSR_IA32_MC0_STATUS
, &err
);
1557 /* Bit 62 may or may not be set for this mce */
1558 value
&= ~(1ULL << 62);
1560 if (value
!= 0xb600000000010015ULL
)
1563 /* Clear MCi_STATUS registers */
1564 for (i
= 0; i
< 6; ++i
)
1565 native_write_msr_safe(MSR_IA32_MCx_STATUS(i
), 0, 0);
1567 value
= native_read_msr_safe(MSR_IA32_MCG_STATUS
, &err
);
1571 value
&= ~(1ULL << 2);
1572 low
= lower_32_bits(value
);
1573 high
= upper_32_bits(value
);
1575 native_write_msr_safe(MSR_IA32_MCG_STATUS
, low
, high
);
1578 /* Flush tlb to evict multi-match entries */
1584 static void svm_handle_mce(struct vcpu_svm
*svm
)
1586 if (is_erratum_383()) {
1588 * Erratum 383 triggered. Guest state is corrupt so kill the
1591 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1593 kvm_make_request(KVM_REQ_TRIPLE_FAULT
, &svm
->vcpu
);
1599 * On an #MC intercept the MCE handler is not called automatically in
1600 * the host. So do it by hand here.
1604 /* not sure if we ever come back to this point */
1609 static int mc_interception(struct vcpu_svm
*svm
)
1614 static int shutdown_interception(struct vcpu_svm
*svm
)
1616 struct kvm_run
*kvm_run
= svm
->vcpu
.run
;
1619 * VMCB is undefined after a SHUTDOWN intercept
1620 * so reinitialize it.
1622 clear_page(svm
->vmcb
);
1625 kvm_run
->exit_reason
= KVM_EXIT_SHUTDOWN
;
1629 static int io_interception(struct vcpu_svm
*svm
)
1631 struct kvm_vcpu
*vcpu
= &svm
->vcpu
;
1632 u32 io_info
= svm
->vmcb
->control
.exit_info_1
; /* address size bug? */
1633 int size
, in
, string
;
1636 ++svm
->vcpu
.stat
.io_exits
;
1637 string
= (io_info
& SVM_IOIO_STR_MASK
) != 0;
1638 in
= (io_info
& SVM_IOIO_TYPE_MASK
) != 0;
1640 return emulate_instruction(vcpu
, 0, 0, 0) == EMULATE_DONE
;
1642 port
= io_info
>> 16;
1643 size
= (io_info
& SVM_IOIO_SIZE_MASK
) >> SVM_IOIO_SIZE_SHIFT
;
1644 svm
->next_rip
= svm
->vmcb
->control
.exit_info_2
;
1645 skip_emulated_instruction(&svm
->vcpu
);
1647 return kvm_fast_pio_out(vcpu
, size
, port
);
1650 static int nmi_interception(struct vcpu_svm
*svm
)
1655 static int intr_interception(struct vcpu_svm
*svm
)
1657 ++svm
->vcpu
.stat
.irq_exits
;
1661 static int nop_on_interception(struct vcpu_svm
*svm
)
1666 static int halt_interception(struct vcpu_svm
*svm
)
1668 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 1;
1669 skip_emulated_instruction(&svm
->vcpu
);
1670 return kvm_emulate_halt(&svm
->vcpu
);
1673 static int vmmcall_interception(struct vcpu_svm
*svm
)
1675 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
1676 skip_emulated_instruction(&svm
->vcpu
);
1677 kvm_emulate_hypercall(&svm
->vcpu
);
1681 static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu
*vcpu
)
1683 struct vcpu_svm
*svm
= to_svm(vcpu
);
1685 return svm
->nested
.nested_cr3
;
1688 static void nested_svm_set_tdp_cr3(struct kvm_vcpu
*vcpu
,
1691 struct vcpu_svm
*svm
= to_svm(vcpu
);
1693 svm
->vmcb
->control
.nested_cr3
= root
;
1694 force_new_asid(vcpu
);
1697 static void nested_svm_inject_npf_exit(struct kvm_vcpu
*vcpu
,
1698 struct x86_exception
*fault
)
1700 struct vcpu_svm
*svm
= to_svm(vcpu
);
1702 svm
->vmcb
->control
.exit_code
= SVM_EXIT_NPF
;
1703 svm
->vmcb
->control
.exit_code_hi
= 0;
1704 svm
->vmcb
->control
.exit_info_1
= fault
->error_code
;
1705 svm
->vmcb
->control
.exit_info_2
= fault
->address
;
1707 nested_svm_vmexit(svm
);
1710 static int nested_svm_init_mmu_context(struct kvm_vcpu
*vcpu
)
1714 r
= kvm_init_shadow_mmu(vcpu
, &vcpu
->arch
.mmu
);
1716 vcpu
->arch
.mmu
.set_cr3
= nested_svm_set_tdp_cr3
;
1717 vcpu
->arch
.mmu
.get_cr3
= nested_svm_get_tdp_cr3
;
1718 vcpu
->arch
.mmu
.inject_page_fault
= nested_svm_inject_npf_exit
;
1719 vcpu
->arch
.mmu
.shadow_root_level
= get_npt_level();
1720 vcpu
->arch
.walk_mmu
= &vcpu
->arch
.nested_mmu
;
1725 static void nested_svm_uninit_mmu_context(struct kvm_vcpu
*vcpu
)
1727 vcpu
->arch
.walk_mmu
= &vcpu
->arch
.mmu
;
1730 static int nested_svm_check_permissions(struct vcpu_svm
*svm
)
1732 if (!(svm
->vcpu
.arch
.efer
& EFER_SVME
)
1733 || !is_paging(&svm
->vcpu
)) {
1734 kvm_queue_exception(&svm
->vcpu
, UD_VECTOR
);
1738 if (svm
->vmcb
->save
.cpl
) {
1739 kvm_inject_gp(&svm
->vcpu
, 0);
1746 static int nested_svm_check_exception(struct vcpu_svm
*svm
, unsigned nr
,
1747 bool has_error_code
, u32 error_code
)
1751 if (!is_guest_mode(&svm
->vcpu
))
1754 svm
->vmcb
->control
.exit_code
= SVM_EXIT_EXCP_BASE
+ nr
;
1755 svm
->vmcb
->control
.exit_code_hi
= 0;
1756 svm
->vmcb
->control
.exit_info_1
= error_code
;
1757 svm
->vmcb
->control
.exit_info_2
= svm
->vcpu
.arch
.cr2
;
1759 vmexit
= nested_svm_intercept(svm
);
1760 if (vmexit
== NESTED_EXIT_DONE
)
1761 svm
->nested
.exit_required
= true;
1766 /* This function returns true if it is save to enable the irq window */
1767 static inline bool nested_svm_intr(struct vcpu_svm
*svm
)
1769 if (!is_guest_mode(&svm
->vcpu
))
1772 if (!(svm
->vcpu
.arch
.hflags
& HF_VINTR_MASK
))
1775 if (!(svm
->vcpu
.arch
.hflags
& HF_HIF_MASK
))
1779 * if vmexit was already requested (by intercepted exception
1780 * for instance) do not overwrite it with "external interrupt"
1783 if (svm
->nested
.exit_required
)
1786 svm
->vmcb
->control
.exit_code
= SVM_EXIT_INTR
;
1787 svm
->vmcb
->control
.exit_info_1
= 0;
1788 svm
->vmcb
->control
.exit_info_2
= 0;
1790 if (svm
->nested
.intercept
& 1ULL) {
1792 * The #vmexit can't be emulated here directly because this
1793 * code path runs with irqs and preemtion disabled. A
1794 * #vmexit emulation might sleep. Only signal request for
1797 svm
->nested
.exit_required
= true;
1798 trace_kvm_nested_intr_vmexit(svm
->vmcb
->save
.rip
);
1805 /* This function returns true if it is save to enable the nmi window */
1806 static inline bool nested_svm_nmi(struct vcpu_svm
*svm
)
1808 if (!is_guest_mode(&svm
->vcpu
))
1811 if (!(svm
->nested
.intercept
& (1ULL << INTERCEPT_NMI
)))
1814 svm
->vmcb
->control
.exit_code
= SVM_EXIT_NMI
;
1815 svm
->nested
.exit_required
= true;
1820 static void *nested_svm_map(struct vcpu_svm
*svm
, u64 gpa
, struct page
**_page
)
1826 page
= gfn_to_page(svm
->vcpu
.kvm
, gpa
>> PAGE_SHIFT
);
1827 if (is_error_page(page
))
1835 kvm_release_page_clean(page
);
1836 kvm_inject_gp(&svm
->vcpu
, 0);
1841 static void nested_svm_unmap(struct page
*page
)
1844 kvm_release_page_dirty(page
);
1847 static int nested_svm_intercept_ioio(struct vcpu_svm
*svm
)
1853 if (!(svm
->nested
.intercept
& (1ULL << INTERCEPT_IOIO_PROT
)))
1854 return NESTED_EXIT_HOST
;
1856 port
= svm
->vmcb
->control
.exit_info_1
>> 16;
1857 gpa
= svm
->nested
.vmcb_iopm
+ (port
/ 8);
1861 if (kvm_read_guest(svm
->vcpu
.kvm
, gpa
, &val
, 1))
1864 return val
? NESTED_EXIT_DONE
: NESTED_EXIT_HOST
;
1867 static int nested_svm_exit_handled_msr(struct vcpu_svm
*svm
)
1869 u32 offset
, msr
, value
;
1872 if (!(svm
->nested
.intercept
& (1ULL << INTERCEPT_MSR_PROT
)))
1873 return NESTED_EXIT_HOST
;
1875 msr
= svm
->vcpu
.arch
.regs
[VCPU_REGS_RCX
];
1876 offset
= svm_msrpm_offset(msr
);
1877 write
= svm
->vmcb
->control
.exit_info_1
& 1;
1878 mask
= 1 << ((2 * (msr
& 0xf)) + write
);
1880 if (offset
== MSR_INVALID
)
1881 return NESTED_EXIT_DONE
;
1883 /* Offset is in 32 bit units but need in 8 bit units */
1886 if (kvm_read_guest(svm
->vcpu
.kvm
, svm
->nested
.vmcb_msrpm
+ offset
, &value
, 4))
1887 return NESTED_EXIT_DONE
;
1889 return (value
& mask
) ? NESTED_EXIT_DONE
: NESTED_EXIT_HOST
;
1892 static int nested_svm_exit_special(struct vcpu_svm
*svm
)
1894 u32 exit_code
= svm
->vmcb
->control
.exit_code
;
1896 switch (exit_code
) {
1899 case SVM_EXIT_EXCP_BASE
+ MC_VECTOR
:
1900 return NESTED_EXIT_HOST
;
1902 /* For now we are always handling NPFs when using them */
1904 return NESTED_EXIT_HOST
;
1906 case SVM_EXIT_EXCP_BASE
+ PF_VECTOR
:
1907 /* When we're shadowing, trap PFs, but not async PF */
1908 if (!npt_enabled
&& svm
->apf_reason
== 0)
1909 return NESTED_EXIT_HOST
;
1911 case SVM_EXIT_EXCP_BASE
+ NM_VECTOR
:
1912 nm_interception(svm
);
1918 return NESTED_EXIT_CONTINUE
;
1922 * If this function returns true, this #vmexit was already handled
1924 static int nested_svm_intercept(struct vcpu_svm
*svm
)
1926 u32 exit_code
= svm
->vmcb
->control
.exit_code
;
1927 int vmexit
= NESTED_EXIT_HOST
;
1929 switch (exit_code
) {
1931 vmexit
= nested_svm_exit_handled_msr(svm
);
1934 vmexit
= nested_svm_intercept_ioio(svm
);
1936 case SVM_EXIT_READ_CR0
... SVM_EXIT_WRITE_CR8
: {
1937 u32 bit
= 1U << (exit_code
- SVM_EXIT_READ_CR0
);
1938 if (svm
->nested
.intercept_cr
& bit
)
1939 vmexit
= NESTED_EXIT_DONE
;
1942 case SVM_EXIT_READ_DR0
... SVM_EXIT_WRITE_DR7
: {
1943 u32 bit
= 1U << (exit_code
- SVM_EXIT_READ_DR0
);
1944 if (svm
->nested
.intercept_dr
& bit
)
1945 vmexit
= NESTED_EXIT_DONE
;
1948 case SVM_EXIT_EXCP_BASE
... SVM_EXIT_EXCP_BASE
+ 0x1f: {
1949 u32 excp_bits
= 1 << (exit_code
- SVM_EXIT_EXCP_BASE
);
1950 if (svm
->nested
.intercept_exceptions
& excp_bits
)
1951 vmexit
= NESTED_EXIT_DONE
;
1952 /* async page fault always cause vmexit */
1953 else if ((exit_code
== SVM_EXIT_EXCP_BASE
+ PF_VECTOR
) &&
1954 svm
->apf_reason
!= 0)
1955 vmexit
= NESTED_EXIT_DONE
;
1958 case SVM_EXIT_ERR
: {
1959 vmexit
= NESTED_EXIT_DONE
;
1963 u64 exit_bits
= 1ULL << (exit_code
- SVM_EXIT_INTR
);
1964 if (svm
->nested
.intercept
& exit_bits
)
1965 vmexit
= NESTED_EXIT_DONE
;
1972 static int nested_svm_exit_handled(struct vcpu_svm
*svm
)
1976 vmexit
= nested_svm_intercept(svm
);
1978 if (vmexit
== NESTED_EXIT_DONE
)
1979 nested_svm_vmexit(svm
);
1984 static inline void copy_vmcb_control_area(struct vmcb
*dst_vmcb
, struct vmcb
*from_vmcb
)
1986 struct vmcb_control_area
*dst
= &dst_vmcb
->control
;
1987 struct vmcb_control_area
*from
= &from_vmcb
->control
;
1989 dst
->intercept_cr
= from
->intercept_cr
;
1990 dst
->intercept_dr
= from
->intercept_dr
;
1991 dst
->intercept_exceptions
= from
->intercept_exceptions
;
1992 dst
->intercept
= from
->intercept
;
1993 dst
->iopm_base_pa
= from
->iopm_base_pa
;
1994 dst
->msrpm_base_pa
= from
->msrpm_base_pa
;
1995 dst
->tsc_offset
= from
->tsc_offset
;
1996 dst
->asid
= from
->asid
;
1997 dst
->tlb_ctl
= from
->tlb_ctl
;
1998 dst
->int_ctl
= from
->int_ctl
;
1999 dst
->int_vector
= from
->int_vector
;
2000 dst
->int_state
= from
->int_state
;
2001 dst
->exit_code
= from
->exit_code
;
2002 dst
->exit_code_hi
= from
->exit_code_hi
;
2003 dst
->exit_info_1
= from
->exit_info_1
;
2004 dst
->exit_info_2
= from
->exit_info_2
;
2005 dst
->exit_int_info
= from
->exit_int_info
;
2006 dst
->exit_int_info_err
= from
->exit_int_info_err
;
2007 dst
->nested_ctl
= from
->nested_ctl
;
2008 dst
->event_inj
= from
->event_inj
;
2009 dst
->event_inj_err
= from
->event_inj_err
;
2010 dst
->nested_cr3
= from
->nested_cr3
;
2011 dst
->lbr_ctl
= from
->lbr_ctl
;
2014 static int nested_svm_vmexit(struct vcpu_svm
*svm
)
2016 struct vmcb
*nested_vmcb
;
2017 struct vmcb
*hsave
= svm
->nested
.hsave
;
2018 struct vmcb
*vmcb
= svm
->vmcb
;
2021 trace_kvm_nested_vmexit_inject(vmcb
->control
.exit_code
,
2022 vmcb
->control
.exit_info_1
,
2023 vmcb
->control
.exit_info_2
,
2024 vmcb
->control
.exit_int_info
,
2025 vmcb
->control
.exit_int_info_err
);
2027 nested_vmcb
= nested_svm_map(svm
, svm
->nested
.vmcb
, &page
);
2031 /* Exit Guest-Mode */
2032 leave_guest_mode(&svm
->vcpu
);
2033 svm
->nested
.vmcb
= 0;
2035 /* Give the current vmcb to the guest */
2038 nested_vmcb
->save
.es
= vmcb
->save
.es
;
2039 nested_vmcb
->save
.cs
= vmcb
->save
.cs
;
2040 nested_vmcb
->save
.ss
= vmcb
->save
.ss
;
2041 nested_vmcb
->save
.ds
= vmcb
->save
.ds
;
2042 nested_vmcb
->save
.gdtr
= vmcb
->save
.gdtr
;
2043 nested_vmcb
->save
.idtr
= vmcb
->save
.idtr
;
2044 nested_vmcb
->save
.efer
= svm
->vcpu
.arch
.efer
;
2045 nested_vmcb
->save
.cr0
= kvm_read_cr0(&svm
->vcpu
);
2046 nested_vmcb
->save
.cr3
= svm
->vcpu
.arch
.cr3
;
2047 nested_vmcb
->save
.cr2
= vmcb
->save
.cr2
;
2048 nested_vmcb
->save
.cr4
= svm
->vcpu
.arch
.cr4
;
2049 nested_vmcb
->save
.rflags
= vmcb
->save
.rflags
;
2050 nested_vmcb
->save
.rip
= vmcb
->save
.rip
;
2051 nested_vmcb
->save
.rsp
= vmcb
->save
.rsp
;
2052 nested_vmcb
->save
.rax
= vmcb
->save
.rax
;
2053 nested_vmcb
->save
.dr7
= vmcb
->save
.dr7
;
2054 nested_vmcb
->save
.dr6
= vmcb
->save
.dr6
;
2055 nested_vmcb
->save
.cpl
= vmcb
->save
.cpl
;
2057 nested_vmcb
->control
.int_ctl
= vmcb
->control
.int_ctl
;
2058 nested_vmcb
->control
.int_vector
= vmcb
->control
.int_vector
;
2059 nested_vmcb
->control
.int_state
= vmcb
->control
.int_state
;
2060 nested_vmcb
->control
.exit_code
= vmcb
->control
.exit_code
;
2061 nested_vmcb
->control
.exit_code_hi
= vmcb
->control
.exit_code_hi
;
2062 nested_vmcb
->control
.exit_info_1
= vmcb
->control
.exit_info_1
;
2063 nested_vmcb
->control
.exit_info_2
= vmcb
->control
.exit_info_2
;
2064 nested_vmcb
->control
.exit_int_info
= vmcb
->control
.exit_int_info
;
2065 nested_vmcb
->control
.exit_int_info_err
= vmcb
->control
.exit_int_info_err
;
2066 nested_vmcb
->control
.next_rip
= vmcb
->control
.next_rip
;
2069 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2070 * to make sure that we do not lose injected events. So check event_inj
2071 * here and copy it to exit_int_info if it is valid.
2072 * Exit_int_info and event_inj can't be both valid because the case
2073 * below only happens on a VMRUN instruction intercept which has
2074 * no valid exit_int_info set.
2076 if (vmcb
->control
.event_inj
& SVM_EVTINJ_VALID
) {
2077 struct vmcb_control_area
*nc
= &nested_vmcb
->control
;
2079 nc
->exit_int_info
= vmcb
->control
.event_inj
;
2080 nc
->exit_int_info_err
= vmcb
->control
.event_inj_err
;
2083 nested_vmcb
->control
.tlb_ctl
= 0;
2084 nested_vmcb
->control
.event_inj
= 0;
2085 nested_vmcb
->control
.event_inj_err
= 0;
2087 /* We always set V_INTR_MASKING and remember the old value in hflags */
2088 if (!(svm
->vcpu
.arch
.hflags
& HF_VINTR_MASK
))
2089 nested_vmcb
->control
.int_ctl
&= ~V_INTR_MASKING_MASK
;
2091 /* Restore the original control entries */
2092 copy_vmcb_control_area(vmcb
, hsave
);
2094 kvm_clear_exception_queue(&svm
->vcpu
);
2095 kvm_clear_interrupt_queue(&svm
->vcpu
);
2097 svm
->nested
.nested_cr3
= 0;
2099 /* Restore selected save entries */
2100 svm
->vmcb
->save
.es
= hsave
->save
.es
;
2101 svm
->vmcb
->save
.cs
= hsave
->save
.cs
;
2102 svm
->vmcb
->save
.ss
= hsave
->save
.ss
;
2103 svm
->vmcb
->save
.ds
= hsave
->save
.ds
;
2104 svm
->vmcb
->save
.gdtr
= hsave
->save
.gdtr
;
2105 svm
->vmcb
->save
.idtr
= hsave
->save
.idtr
;
2106 svm
->vmcb
->save
.rflags
= hsave
->save
.rflags
;
2107 svm_set_efer(&svm
->vcpu
, hsave
->save
.efer
);
2108 svm_set_cr0(&svm
->vcpu
, hsave
->save
.cr0
| X86_CR0_PE
);
2109 svm_set_cr4(&svm
->vcpu
, hsave
->save
.cr4
);
2111 svm
->vmcb
->save
.cr3
= hsave
->save
.cr3
;
2112 svm
->vcpu
.arch
.cr3
= hsave
->save
.cr3
;
2114 (void)kvm_set_cr3(&svm
->vcpu
, hsave
->save
.cr3
);
2116 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RAX
, hsave
->save
.rax
);
2117 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RSP
, hsave
->save
.rsp
);
2118 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RIP
, hsave
->save
.rip
);
2119 svm
->vmcb
->save
.dr7
= 0;
2120 svm
->vmcb
->save
.cpl
= 0;
2121 svm
->vmcb
->control
.exit_int_info
= 0;
2123 nested_svm_unmap(page
);
2125 nested_svm_uninit_mmu_context(&svm
->vcpu
);
2126 kvm_mmu_reset_context(&svm
->vcpu
);
2127 kvm_mmu_load(&svm
->vcpu
);
2132 static bool nested_svm_vmrun_msrpm(struct vcpu_svm
*svm
)
2135 * This function merges the msr permission bitmaps of kvm and the
2136 * nested vmcb. It is omptimized in that it only merges the parts where
2137 * the kvm msr permission bitmap may contain zero bits
2141 if (!(svm
->nested
.intercept
& (1ULL << INTERCEPT_MSR_PROT
)))
2144 for (i
= 0; i
< MSRPM_OFFSETS
; i
++) {
2148 if (msrpm_offsets
[i
] == 0xffffffff)
2151 p
= msrpm_offsets
[i
];
2152 offset
= svm
->nested
.vmcb_msrpm
+ (p
* 4);
2154 if (kvm_read_guest(svm
->vcpu
.kvm
, offset
, &value
, 4))
2157 svm
->nested
.msrpm
[p
] = svm
->msrpm
[p
] | value
;
2160 svm
->vmcb
->control
.msrpm_base_pa
= __pa(svm
->nested
.msrpm
);
2165 static bool nested_vmcb_checks(struct vmcb
*vmcb
)
2167 if ((vmcb
->control
.intercept
& (1ULL << INTERCEPT_VMRUN
)) == 0)
2170 if (vmcb
->control
.asid
== 0)
2173 if (vmcb
->control
.nested_ctl
&& !npt_enabled
)
2179 static bool nested_svm_vmrun(struct vcpu_svm
*svm
)
2181 struct vmcb
*nested_vmcb
;
2182 struct vmcb
*hsave
= svm
->nested
.hsave
;
2183 struct vmcb
*vmcb
= svm
->vmcb
;
2187 vmcb_gpa
= svm
->vmcb
->save
.rax
;
2189 nested_vmcb
= nested_svm_map(svm
, svm
->vmcb
->save
.rax
, &page
);
2193 if (!nested_vmcb_checks(nested_vmcb
)) {
2194 nested_vmcb
->control
.exit_code
= SVM_EXIT_ERR
;
2195 nested_vmcb
->control
.exit_code_hi
= 0;
2196 nested_vmcb
->control
.exit_info_1
= 0;
2197 nested_vmcb
->control
.exit_info_2
= 0;
2199 nested_svm_unmap(page
);
2204 trace_kvm_nested_vmrun(svm
->vmcb
->save
.rip
, vmcb_gpa
,
2205 nested_vmcb
->save
.rip
,
2206 nested_vmcb
->control
.int_ctl
,
2207 nested_vmcb
->control
.event_inj
,
2208 nested_vmcb
->control
.nested_ctl
);
2210 trace_kvm_nested_intercepts(nested_vmcb
->control
.intercept_cr
& 0xffff,
2211 nested_vmcb
->control
.intercept_cr
>> 16,
2212 nested_vmcb
->control
.intercept_exceptions
,
2213 nested_vmcb
->control
.intercept
);
2215 /* Clear internal status */
2216 kvm_clear_exception_queue(&svm
->vcpu
);
2217 kvm_clear_interrupt_queue(&svm
->vcpu
);
2220 * Save the old vmcb, so we don't need to pick what we save, but can
2221 * restore everything when a VMEXIT occurs
2223 hsave
->save
.es
= vmcb
->save
.es
;
2224 hsave
->save
.cs
= vmcb
->save
.cs
;
2225 hsave
->save
.ss
= vmcb
->save
.ss
;
2226 hsave
->save
.ds
= vmcb
->save
.ds
;
2227 hsave
->save
.gdtr
= vmcb
->save
.gdtr
;
2228 hsave
->save
.idtr
= vmcb
->save
.idtr
;
2229 hsave
->save
.efer
= svm
->vcpu
.arch
.efer
;
2230 hsave
->save
.cr0
= kvm_read_cr0(&svm
->vcpu
);
2231 hsave
->save
.cr4
= svm
->vcpu
.arch
.cr4
;
2232 hsave
->save
.rflags
= vmcb
->save
.rflags
;
2233 hsave
->save
.rip
= kvm_rip_read(&svm
->vcpu
);
2234 hsave
->save
.rsp
= vmcb
->save
.rsp
;
2235 hsave
->save
.rax
= vmcb
->save
.rax
;
2237 hsave
->save
.cr3
= vmcb
->save
.cr3
;
2239 hsave
->save
.cr3
= svm
->vcpu
.arch
.cr3
;
2241 copy_vmcb_control_area(hsave
, vmcb
);
2243 if (svm
->vmcb
->save
.rflags
& X86_EFLAGS_IF
)
2244 svm
->vcpu
.arch
.hflags
|= HF_HIF_MASK
;
2246 svm
->vcpu
.arch
.hflags
&= ~HF_HIF_MASK
;
2248 if (nested_vmcb
->control
.nested_ctl
) {
2249 kvm_mmu_unload(&svm
->vcpu
);
2250 svm
->nested
.nested_cr3
= nested_vmcb
->control
.nested_cr3
;
2251 nested_svm_init_mmu_context(&svm
->vcpu
);
2254 /* Load the nested guest state */
2255 svm
->vmcb
->save
.es
= nested_vmcb
->save
.es
;
2256 svm
->vmcb
->save
.cs
= nested_vmcb
->save
.cs
;
2257 svm
->vmcb
->save
.ss
= nested_vmcb
->save
.ss
;
2258 svm
->vmcb
->save
.ds
= nested_vmcb
->save
.ds
;
2259 svm
->vmcb
->save
.gdtr
= nested_vmcb
->save
.gdtr
;
2260 svm
->vmcb
->save
.idtr
= nested_vmcb
->save
.idtr
;
2261 svm
->vmcb
->save
.rflags
= nested_vmcb
->save
.rflags
;
2262 svm_set_efer(&svm
->vcpu
, nested_vmcb
->save
.efer
);
2263 svm_set_cr0(&svm
->vcpu
, nested_vmcb
->save
.cr0
);
2264 svm_set_cr4(&svm
->vcpu
, nested_vmcb
->save
.cr4
);
2266 svm
->vmcb
->save
.cr3
= nested_vmcb
->save
.cr3
;
2267 svm
->vcpu
.arch
.cr3
= nested_vmcb
->save
.cr3
;
2269 (void)kvm_set_cr3(&svm
->vcpu
, nested_vmcb
->save
.cr3
);
2271 /* Guest paging mode is active - reset mmu */
2272 kvm_mmu_reset_context(&svm
->vcpu
);
2274 svm
->vmcb
->save
.cr2
= svm
->vcpu
.arch
.cr2
= nested_vmcb
->save
.cr2
;
2275 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RAX
, nested_vmcb
->save
.rax
);
2276 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RSP
, nested_vmcb
->save
.rsp
);
2277 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RIP
, nested_vmcb
->save
.rip
);
2279 /* In case we don't even reach vcpu_run, the fields are not updated */
2280 svm
->vmcb
->save
.rax
= nested_vmcb
->save
.rax
;
2281 svm
->vmcb
->save
.rsp
= nested_vmcb
->save
.rsp
;
2282 svm
->vmcb
->save
.rip
= nested_vmcb
->save
.rip
;
2283 svm
->vmcb
->save
.dr7
= nested_vmcb
->save
.dr7
;
2284 svm
->vmcb
->save
.dr6
= nested_vmcb
->save
.dr6
;
2285 svm
->vmcb
->save
.cpl
= nested_vmcb
->save
.cpl
;
2287 svm
->nested
.vmcb_msrpm
= nested_vmcb
->control
.msrpm_base_pa
& ~0x0fffULL
;
2288 svm
->nested
.vmcb_iopm
= nested_vmcb
->control
.iopm_base_pa
& ~0x0fffULL
;
2290 /* cache intercepts */
2291 svm
->nested
.intercept_cr
= nested_vmcb
->control
.intercept_cr
;
2292 svm
->nested
.intercept_dr
= nested_vmcb
->control
.intercept_dr
;
2293 svm
->nested
.intercept_exceptions
= nested_vmcb
->control
.intercept_exceptions
;
2294 svm
->nested
.intercept
= nested_vmcb
->control
.intercept
;
2296 force_new_asid(&svm
->vcpu
);
2297 svm
->vmcb
->control
.int_ctl
= nested_vmcb
->control
.int_ctl
| V_INTR_MASKING_MASK
;
2298 if (nested_vmcb
->control
.int_ctl
& V_INTR_MASKING_MASK
)
2299 svm
->vcpu
.arch
.hflags
|= HF_VINTR_MASK
;
2301 svm
->vcpu
.arch
.hflags
&= ~HF_VINTR_MASK
;
2303 if (svm
->vcpu
.arch
.hflags
& HF_VINTR_MASK
) {
2304 /* We only want the cr8 intercept bits of the guest */
2305 clr_cr_intercept(svm
, INTERCEPT_CR8_READ
);
2306 clr_cr_intercept(svm
, INTERCEPT_CR8_WRITE
);
2309 /* We don't want to see VMMCALLs from a nested guest */
2310 svm
->vmcb
->control
.intercept
&= ~(1ULL << INTERCEPT_VMMCALL
);
2312 svm
->vmcb
->control
.lbr_ctl
= nested_vmcb
->control
.lbr_ctl
;
2313 svm
->vmcb
->control
.int_vector
= nested_vmcb
->control
.int_vector
;
2314 svm
->vmcb
->control
.int_state
= nested_vmcb
->control
.int_state
;
2315 svm
->vmcb
->control
.tsc_offset
+= nested_vmcb
->control
.tsc_offset
;
2316 svm
->vmcb
->control
.event_inj
= nested_vmcb
->control
.event_inj
;
2317 svm
->vmcb
->control
.event_inj_err
= nested_vmcb
->control
.event_inj_err
;
2319 nested_svm_unmap(page
);
2321 /* Enter Guest-Mode */
2322 enter_guest_mode(&svm
->vcpu
);
2325 * Merge guest and host intercepts - must be called with vcpu in
2326 * guest-mode to take affect here
2328 recalc_intercepts(svm
);
2330 svm
->nested
.vmcb
= vmcb_gpa
;
2337 static void nested_svm_vmloadsave(struct vmcb
*from_vmcb
, struct vmcb
*to_vmcb
)
2339 to_vmcb
->save
.fs
= from_vmcb
->save
.fs
;
2340 to_vmcb
->save
.gs
= from_vmcb
->save
.gs
;
2341 to_vmcb
->save
.tr
= from_vmcb
->save
.tr
;
2342 to_vmcb
->save
.ldtr
= from_vmcb
->save
.ldtr
;
2343 to_vmcb
->save
.kernel_gs_base
= from_vmcb
->save
.kernel_gs_base
;
2344 to_vmcb
->save
.star
= from_vmcb
->save
.star
;
2345 to_vmcb
->save
.lstar
= from_vmcb
->save
.lstar
;
2346 to_vmcb
->save
.cstar
= from_vmcb
->save
.cstar
;
2347 to_vmcb
->save
.sfmask
= from_vmcb
->save
.sfmask
;
2348 to_vmcb
->save
.sysenter_cs
= from_vmcb
->save
.sysenter_cs
;
2349 to_vmcb
->save
.sysenter_esp
= from_vmcb
->save
.sysenter_esp
;
2350 to_vmcb
->save
.sysenter_eip
= from_vmcb
->save
.sysenter_eip
;
2353 static int vmload_interception(struct vcpu_svm
*svm
)
2355 struct vmcb
*nested_vmcb
;
2358 if (nested_svm_check_permissions(svm
))
2361 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
2362 skip_emulated_instruction(&svm
->vcpu
);
2364 nested_vmcb
= nested_svm_map(svm
, svm
->vmcb
->save
.rax
, &page
);
2368 nested_svm_vmloadsave(nested_vmcb
, svm
->vmcb
);
2369 nested_svm_unmap(page
);
2374 static int vmsave_interception(struct vcpu_svm
*svm
)
2376 struct vmcb
*nested_vmcb
;
2379 if (nested_svm_check_permissions(svm
))
2382 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
2383 skip_emulated_instruction(&svm
->vcpu
);
2385 nested_vmcb
= nested_svm_map(svm
, svm
->vmcb
->save
.rax
, &page
);
2389 nested_svm_vmloadsave(svm
->vmcb
, nested_vmcb
);
2390 nested_svm_unmap(page
);
2395 static int vmrun_interception(struct vcpu_svm
*svm
)
2397 if (nested_svm_check_permissions(svm
))
2400 /* Save rip after vmrun instruction */
2401 kvm_rip_write(&svm
->vcpu
, kvm_rip_read(&svm
->vcpu
) + 3);
2403 if (!nested_svm_vmrun(svm
))
2406 if (!nested_svm_vmrun_msrpm(svm
))
2413 svm
->vmcb
->control
.exit_code
= SVM_EXIT_ERR
;
2414 svm
->vmcb
->control
.exit_code_hi
= 0;
2415 svm
->vmcb
->control
.exit_info_1
= 0;
2416 svm
->vmcb
->control
.exit_info_2
= 0;
2418 nested_svm_vmexit(svm
);
2423 static int stgi_interception(struct vcpu_svm
*svm
)
2425 if (nested_svm_check_permissions(svm
))
2428 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
2429 skip_emulated_instruction(&svm
->vcpu
);
2430 kvm_make_request(KVM_REQ_EVENT
, &svm
->vcpu
);
2437 static int clgi_interception(struct vcpu_svm
*svm
)
2439 if (nested_svm_check_permissions(svm
))
2442 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
2443 skip_emulated_instruction(&svm
->vcpu
);
2447 /* After a CLGI no interrupts should come */
2448 svm_clear_vintr(svm
);
2449 svm
->vmcb
->control
.int_ctl
&= ~V_IRQ_MASK
;
2454 static int invlpga_interception(struct vcpu_svm
*svm
)
2456 struct kvm_vcpu
*vcpu
= &svm
->vcpu
;
2458 trace_kvm_invlpga(svm
->vmcb
->save
.rip
, vcpu
->arch
.regs
[VCPU_REGS_RCX
],
2459 vcpu
->arch
.regs
[VCPU_REGS_RAX
]);
2461 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2462 kvm_mmu_invlpg(vcpu
, vcpu
->arch
.regs
[VCPU_REGS_RAX
]);
2464 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
2465 skip_emulated_instruction(&svm
->vcpu
);
2469 static int skinit_interception(struct vcpu_svm
*svm
)
2471 trace_kvm_skinit(svm
->vmcb
->save
.rip
, svm
->vcpu
.arch
.regs
[VCPU_REGS_RAX
]);
2473 kvm_queue_exception(&svm
->vcpu
, UD_VECTOR
);
2477 static int invalid_op_interception(struct vcpu_svm
*svm
)
2479 kvm_queue_exception(&svm
->vcpu
, UD_VECTOR
);
2483 static int task_switch_interception(struct vcpu_svm
*svm
)
2487 int int_type
= svm
->vmcb
->control
.exit_int_info
&
2488 SVM_EXITINTINFO_TYPE_MASK
;
2489 int int_vec
= svm
->vmcb
->control
.exit_int_info
& SVM_EVTINJ_VEC_MASK
;
2491 svm
->vmcb
->control
.exit_int_info
& SVM_EXITINTINFO_TYPE_MASK
;
2493 svm
->vmcb
->control
.exit_int_info
& SVM_EXITINTINFO_VALID
;
2494 bool has_error_code
= false;
2497 tss_selector
= (u16
)svm
->vmcb
->control
.exit_info_1
;
2499 if (svm
->vmcb
->control
.exit_info_2
&
2500 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET
))
2501 reason
= TASK_SWITCH_IRET
;
2502 else if (svm
->vmcb
->control
.exit_info_2
&
2503 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP
))
2504 reason
= TASK_SWITCH_JMP
;
2506 reason
= TASK_SWITCH_GATE
;
2508 reason
= TASK_SWITCH_CALL
;
2510 if (reason
== TASK_SWITCH_GATE
) {
2512 case SVM_EXITINTINFO_TYPE_NMI
:
2513 svm
->vcpu
.arch
.nmi_injected
= false;
2515 case SVM_EXITINTINFO_TYPE_EXEPT
:
2516 if (svm
->vmcb
->control
.exit_info_2
&
2517 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE
)) {
2518 has_error_code
= true;
2520 (u32
)svm
->vmcb
->control
.exit_info_2
;
2522 kvm_clear_exception_queue(&svm
->vcpu
);
2524 case SVM_EXITINTINFO_TYPE_INTR
:
2525 kvm_clear_interrupt_queue(&svm
->vcpu
);
2532 if (reason
!= TASK_SWITCH_GATE
||
2533 int_type
== SVM_EXITINTINFO_TYPE_SOFT
||
2534 (int_type
== SVM_EXITINTINFO_TYPE_EXEPT
&&
2535 (int_vec
== OF_VECTOR
|| int_vec
== BP_VECTOR
)))
2536 skip_emulated_instruction(&svm
->vcpu
);
2538 if (kvm_task_switch(&svm
->vcpu
, tss_selector
, reason
,
2539 has_error_code
, error_code
) == EMULATE_FAIL
) {
2540 svm
->vcpu
.run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
2541 svm
->vcpu
.run
->internal
.suberror
= KVM_INTERNAL_ERROR_EMULATION
;
2542 svm
->vcpu
.run
->internal
.ndata
= 0;
2548 static int cpuid_interception(struct vcpu_svm
*svm
)
2550 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 2;
2551 kvm_emulate_cpuid(&svm
->vcpu
);
2555 static int iret_interception(struct vcpu_svm
*svm
)
2557 ++svm
->vcpu
.stat
.nmi_window_exits
;
2558 svm
->vmcb
->control
.intercept
&= ~(1ULL << INTERCEPT_IRET
);
2559 svm
->vcpu
.arch
.hflags
|= HF_IRET_MASK
;
2563 static int invlpg_interception(struct vcpu_svm
*svm
)
2565 return emulate_instruction(&svm
->vcpu
, 0, 0, 0) == EMULATE_DONE
;
2568 static int emulate_on_interception(struct vcpu_svm
*svm
)
2570 return emulate_instruction(&svm
->vcpu
, 0, 0, 0) == EMULATE_DONE
;
2573 static int cr0_write_interception(struct vcpu_svm
*svm
)
2575 struct kvm_vcpu
*vcpu
= &svm
->vcpu
;
2578 r
= emulate_instruction(&svm
->vcpu
, 0, 0, 0);
2580 if (svm
->nested
.vmexit_rip
) {
2581 kvm_register_write(vcpu
, VCPU_REGS_RIP
, svm
->nested
.vmexit_rip
);
2582 kvm_register_write(vcpu
, VCPU_REGS_RSP
, svm
->nested
.vmexit_rsp
);
2583 kvm_register_write(vcpu
, VCPU_REGS_RAX
, svm
->nested
.vmexit_rax
);
2584 svm
->nested
.vmexit_rip
= 0;
2587 return r
== EMULATE_DONE
;
2590 static int cr8_write_interception(struct vcpu_svm
*svm
)
2592 struct kvm_run
*kvm_run
= svm
->vcpu
.run
;
2594 u8 cr8_prev
= kvm_get_cr8(&svm
->vcpu
);
2595 /* instruction emulation calls kvm_set_cr8() */
2596 emulate_instruction(&svm
->vcpu
, 0, 0, 0);
2597 if (irqchip_in_kernel(svm
->vcpu
.kvm
)) {
2598 clr_cr_intercept(svm
, INTERCEPT_CR8_WRITE
);
2601 if (cr8_prev
<= kvm_get_cr8(&svm
->vcpu
))
2603 kvm_run
->exit_reason
= KVM_EXIT_SET_TPR
;
2607 static int svm_get_msr(struct kvm_vcpu
*vcpu
, unsigned ecx
, u64
*data
)
2609 struct vcpu_svm
*svm
= to_svm(vcpu
);
2612 case MSR_IA32_TSC
: {
2615 if (is_guest_mode(vcpu
))
2616 tsc_offset
= svm
->nested
.hsave
->control
.tsc_offset
;
2618 tsc_offset
= svm
->vmcb
->control
.tsc_offset
;
2620 *data
= tsc_offset
+ native_read_tsc();
2624 *data
= svm
->vmcb
->save
.star
;
2626 #ifdef CONFIG_X86_64
2628 *data
= svm
->vmcb
->save
.lstar
;
2631 *data
= svm
->vmcb
->save
.cstar
;
2633 case MSR_KERNEL_GS_BASE
:
2634 *data
= svm
->vmcb
->save
.kernel_gs_base
;
2636 case MSR_SYSCALL_MASK
:
2637 *data
= svm
->vmcb
->save
.sfmask
;
2640 case MSR_IA32_SYSENTER_CS
:
2641 *data
= svm
->vmcb
->save
.sysenter_cs
;
2643 case MSR_IA32_SYSENTER_EIP
:
2644 *data
= svm
->sysenter_eip
;
2646 case MSR_IA32_SYSENTER_ESP
:
2647 *data
= svm
->sysenter_esp
;
2650 * Nobody will change the following 5 values in the VMCB so we can
2651 * safely return them on rdmsr. They will always be 0 until LBRV is
2654 case MSR_IA32_DEBUGCTLMSR
:
2655 *data
= svm
->vmcb
->save
.dbgctl
;
2657 case MSR_IA32_LASTBRANCHFROMIP
:
2658 *data
= svm
->vmcb
->save
.br_from
;
2660 case MSR_IA32_LASTBRANCHTOIP
:
2661 *data
= svm
->vmcb
->save
.br_to
;
2663 case MSR_IA32_LASTINTFROMIP
:
2664 *data
= svm
->vmcb
->save
.last_excp_from
;
2666 case MSR_IA32_LASTINTTOIP
:
2667 *data
= svm
->vmcb
->save
.last_excp_to
;
2669 case MSR_VM_HSAVE_PA
:
2670 *data
= svm
->nested
.hsave_msr
;
2673 *data
= svm
->nested
.vm_cr_msr
;
2675 case MSR_IA32_UCODE_REV
:
2679 return kvm_get_msr_common(vcpu
, ecx
, data
);
2684 static int rdmsr_interception(struct vcpu_svm
*svm
)
2686 u32 ecx
= svm
->vcpu
.arch
.regs
[VCPU_REGS_RCX
];
2689 if (svm_get_msr(&svm
->vcpu
, ecx
, &data
)) {
2690 trace_kvm_msr_read_ex(ecx
);
2691 kvm_inject_gp(&svm
->vcpu
, 0);
2693 trace_kvm_msr_read(ecx
, data
);
2695 svm
->vcpu
.arch
.regs
[VCPU_REGS_RAX
] = data
& 0xffffffff;
2696 svm
->vcpu
.arch
.regs
[VCPU_REGS_RDX
] = data
>> 32;
2697 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 2;
2698 skip_emulated_instruction(&svm
->vcpu
);
2703 static int svm_set_vm_cr(struct kvm_vcpu
*vcpu
, u64 data
)
2705 struct vcpu_svm
*svm
= to_svm(vcpu
);
2706 int svm_dis
, chg_mask
;
2708 if (data
& ~SVM_VM_CR_VALID_MASK
)
2711 chg_mask
= SVM_VM_CR_VALID_MASK
;
2713 if (svm
->nested
.vm_cr_msr
& SVM_VM_CR_SVM_DIS_MASK
)
2714 chg_mask
&= ~(SVM_VM_CR_SVM_LOCK_MASK
| SVM_VM_CR_SVM_DIS_MASK
);
2716 svm
->nested
.vm_cr_msr
&= ~chg_mask
;
2717 svm
->nested
.vm_cr_msr
|= (data
& chg_mask
);
2719 svm_dis
= svm
->nested
.vm_cr_msr
& SVM_VM_CR_SVM_DIS_MASK
;
2721 /* check for svm_disable while efer.svme is set */
2722 if (svm_dis
&& (vcpu
->arch
.efer
& EFER_SVME
))
2728 static int svm_set_msr(struct kvm_vcpu
*vcpu
, unsigned ecx
, u64 data
)
2730 struct vcpu_svm
*svm
= to_svm(vcpu
);
2734 kvm_write_tsc(vcpu
, data
);
2737 svm
->vmcb
->save
.star
= data
;
2739 #ifdef CONFIG_X86_64
2741 svm
->vmcb
->save
.lstar
= data
;
2744 svm
->vmcb
->save
.cstar
= data
;
2746 case MSR_KERNEL_GS_BASE
:
2747 svm
->vmcb
->save
.kernel_gs_base
= data
;
2749 case MSR_SYSCALL_MASK
:
2750 svm
->vmcb
->save
.sfmask
= data
;
2753 case MSR_IA32_SYSENTER_CS
:
2754 svm
->vmcb
->save
.sysenter_cs
= data
;
2756 case MSR_IA32_SYSENTER_EIP
:
2757 svm
->sysenter_eip
= data
;
2758 svm
->vmcb
->save
.sysenter_eip
= data
;
2760 case MSR_IA32_SYSENTER_ESP
:
2761 svm
->sysenter_esp
= data
;
2762 svm
->vmcb
->save
.sysenter_esp
= data
;
2764 case MSR_IA32_DEBUGCTLMSR
:
2765 if (!boot_cpu_has(X86_FEATURE_LBRV
)) {
2766 pr_unimpl(vcpu
, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
2770 if (data
& DEBUGCTL_RESERVED_BITS
)
2773 svm
->vmcb
->save
.dbgctl
= data
;
2774 if (data
& (1ULL<<0))
2775 svm_enable_lbrv(svm
);
2777 svm_disable_lbrv(svm
);
2779 case MSR_VM_HSAVE_PA
:
2780 svm
->nested
.hsave_msr
= data
;
2783 return svm_set_vm_cr(vcpu
, data
);
2785 pr_unimpl(vcpu
, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx
, data
);
2788 return kvm_set_msr_common(vcpu
, ecx
, data
);
2793 static int wrmsr_interception(struct vcpu_svm
*svm
)
2795 u32 ecx
= svm
->vcpu
.arch
.regs
[VCPU_REGS_RCX
];
2796 u64 data
= (svm
->vcpu
.arch
.regs
[VCPU_REGS_RAX
] & -1u)
2797 | ((u64
)(svm
->vcpu
.arch
.regs
[VCPU_REGS_RDX
] & -1u) << 32);
2800 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 2;
2801 if (svm_set_msr(&svm
->vcpu
, ecx
, data
)) {
2802 trace_kvm_msr_write_ex(ecx
, data
);
2803 kvm_inject_gp(&svm
->vcpu
, 0);
2805 trace_kvm_msr_write(ecx
, data
);
2806 skip_emulated_instruction(&svm
->vcpu
);
2811 static int msr_interception(struct vcpu_svm
*svm
)
2813 if (svm
->vmcb
->control
.exit_info_1
)
2814 return wrmsr_interception(svm
);
2816 return rdmsr_interception(svm
);
2819 static int interrupt_window_interception(struct vcpu_svm
*svm
)
2821 struct kvm_run
*kvm_run
= svm
->vcpu
.run
;
2823 kvm_make_request(KVM_REQ_EVENT
, &svm
->vcpu
);
2824 svm_clear_vintr(svm
);
2825 svm
->vmcb
->control
.int_ctl
&= ~V_IRQ_MASK
;
2827 * If the user space waits to inject interrupts, exit as soon as
2830 if (!irqchip_in_kernel(svm
->vcpu
.kvm
) &&
2831 kvm_run
->request_interrupt_window
&&
2832 !kvm_cpu_has_interrupt(&svm
->vcpu
)) {
2833 ++svm
->vcpu
.stat
.irq_window_exits
;
2834 kvm_run
->exit_reason
= KVM_EXIT_IRQ_WINDOW_OPEN
;
2841 static int pause_interception(struct vcpu_svm
*svm
)
2843 kvm_vcpu_on_spin(&(svm
->vcpu
));
2847 static int (*svm_exit_handlers
[])(struct vcpu_svm
*svm
) = {
2848 [SVM_EXIT_READ_CR0
] = emulate_on_interception
,
2849 [SVM_EXIT_READ_CR3
] = emulate_on_interception
,
2850 [SVM_EXIT_READ_CR4
] = emulate_on_interception
,
2851 [SVM_EXIT_READ_CR8
] = emulate_on_interception
,
2852 [SVM_EXIT_CR0_SEL_WRITE
] = emulate_on_interception
,
2853 [SVM_EXIT_WRITE_CR0
] = cr0_write_interception
,
2854 [SVM_EXIT_WRITE_CR3
] = emulate_on_interception
,
2855 [SVM_EXIT_WRITE_CR4
] = emulate_on_interception
,
2856 [SVM_EXIT_WRITE_CR8
] = cr8_write_interception
,
2857 [SVM_EXIT_READ_DR0
] = emulate_on_interception
,
2858 [SVM_EXIT_READ_DR1
] = emulate_on_interception
,
2859 [SVM_EXIT_READ_DR2
] = emulate_on_interception
,
2860 [SVM_EXIT_READ_DR3
] = emulate_on_interception
,
2861 [SVM_EXIT_READ_DR4
] = emulate_on_interception
,
2862 [SVM_EXIT_READ_DR5
] = emulate_on_interception
,
2863 [SVM_EXIT_READ_DR6
] = emulate_on_interception
,
2864 [SVM_EXIT_READ_DR7
] = emulate_on_interception
,
2865 [SVM_EXIT_WRITE_DR0
] = emulate_on_interception
,
2866 [SVM_EXIT_WRITE_DR1
] = emulate_on_interception
,
2867 [SVM_EXIT_WRITE_DR2
] = emulate_on_interception
,
2868 [SVM_EXIT_WRITE_DR3
] = emulate_on_interception
,
2869 [SVM_EXIT_WRITE_DR4
] = emulate_on_interception
,
2870 [SVM_EXIT_WRITE_DR5
] = emulate_on_interception
,
2871 [SVM_EXIT_WRITE_DR6
] = emulate_on_interception
,
2872 [SVM_EXIT_WRITE_DR7
] = emulate_on_interception
,
2873 [SVM_EXIT_EXCP_BASE
+ DB_VECTOR
] = db_interception
,
2874 [SVM_EXIT_EXCP_BASE
+ BP_VECTOR
] = bp_interception
,
2875 [SVM_EXIT_EXCP_BASE
+ UD_VECTOR
] = ud_interception
,
2876 [SVM_EXIT_EXCP_BASE
+ PF_VECTOR
] = pf_interception
,
2877 [SVM_EXIT_EXCP_BASE
+ NM_VECTOR
] = nm_interception
,
2878 [SVM_EXIT_EXCP_BASE
+ MC_VECTOR
] = mc_interception
,
2879 [SVM_EXIT_INTR
] = intr_interception
,
2880 [SVM_EXIT_NMI
] = nmi_interception
,
2881 [SVM_EXIT_SMI
] = nop_on_interception
,
2882 [SVM_EXIT_INIT
] = nop_on_interception
,
2883 [SVM_EXIT_VINTR
] = interrupt_window_interception
,
2884 [SVM_EXIT_CPUID
] = cpuid_interception
,
2885 [SVM_EXIT_IRET
] = iret_interception
,
2886 [SVM_EXIT_INVD
] = emulate_on_interception
,
2887 [SVM_EXIT_PAUSE
] = pause_interception
,
2888 [SVM_EXIT_HLT
] = halt_interception
,
2889 [SVM_EXIT_INVLPG
] = invlpg_interception
,
2890 [SVM_EXIT_INVLPGA
] = invlpga_interception
,
2891 [SVM_EXIT_IOIO
] = io_interception
,
2892 [SVM_EXIT_MSR
] = msr_interception
,
2893 [SVM_EXIT_TASK_SWITCH
] = task_switch_interception
,
2894 [SVM_EXIT_SHUTDOWN
] = shutdown_interception
,
2895 [SVM_EXIT_VMRUN
] = vmrun_interception
,
2896 [SVM_EXIT_VMMCALL
] = vmmcall_interception
,
2897 [SVM_EXIT_VMLOAD
] = vmload_interception
,
2898 [SVM_EXIT_VMSAVE
] = vmsave_interception
,
2899 [SVM_EXIT_STGI
] = stgi_interception
,
2900 [SVM_EXIT_CLGI
] = clgi_interception
,
2901 [SVM_EXIT_SKINIT
] = skinit_interception
,
2902 [SVM_EXIT_WBINVD
] = emulate_on_interception
,
2903 [SVM_EXIT_MONITOR
] = invalid_op_interception
,
2904 [SVM_EXIT_MWAIT
] = invalid_op_interception
,
2905 [SVM_EXIT_NPF
] = pf_interception
,
2908 void dump_vmcb(struct kvm_vcpu
*vcpu
)
2910 struct vcpu_svm
*svm
= to_svm(vcpu
);
2911 struct vmcb_control_area
*control
= &svm
->vmcb
->control
;
2912 struct vmcb_save_area
*save
= &svm
->vmcb
->save
;
2914 pr_err("VMCB Control Area:\n");
2915 pr_err("cr_read: %04x\n", control
->intercept_cr
& 0xffff);
2916 pr_err("cr_write: %04x\n", control
->intercept_cr
>> 16);
2917 pr_err("dr_read: %04x\n", control
->intercept_dr
& 0xffff);
2918 pr_err("dr_write: %04x\n", control
->intercept_dr
>> 16);
2919 pr_err("exceptions: %08x\n", control
->intercept_exceptions
);
2920 pr_err("intercepts: %016llx\n", control
->intercept
);
2921 pr_err("pause filter count: %d\n", control
->pause_filter_count
);
2922 pr_err("iopm_base_pa: %016llx\n", control
->iopm_base_pa
);
2923 pr_err("msrpm_base_pa: %016llx\n", control
->msrpm_base_pa
);
2924 pr_err("tsc_offset: %016llx\n", control
->tsc_offset
);
2925 pr_err("asid: %d\n", control
->asid
);
2926 pr_err("tlb_ctl: %d\n", control
->tlb_ctl
);
2927 pr_err("int_ctl: %08x\n", control
->int_ctl
);
2928 pr_err("int_vector: %08x\n", control
->int_vector
);
2929 pr_err("int_state: %08x\n", control
->int_state
);
2930 pr_err("exit_code: %08x\n", control
->exit_code
);
2931 pr_err("exit_info1: %016llx\n", control
->exit_info_1
);
2932 pr_err("exit_info2: %016llx\n", control
->exit_info_2
);
2933 pr_err("exit_int_info: %08x\n", control
->exit_int_info
);
2934 pr_err("exit_int_info_err: %08x\n", control
->exit_int_info_err
);
2935 pr_err("nested_ctl: %lld\n", control
->nested_ctl
);
2936 pr_err("nested_cr3: %016llx\n", control
->nested_cr3
);
2937 pr_err("event_inj: %08x\n", control
->event_inj
);
2938 pr_err("event_inj_err: %08x\n", control
->event_inj_err
);
2939 pr_err("lbr_ctl: %lld\n", control
->lbr_ctl
);
2940 pr_err("next_rip: %016llx\n", control
->next_rip
);
2941 pr_err("VMCB State Save Area:\n");
2942 pr_err("es: s: %04x a: %04x l: %08x b: %016llx\n",
2943 save
->es
.selector
, save
->es
.attrib
,
2944 save
->es
.limit
, save
->es
.base
);
2945 pr_err("cs: s: %04x a: %04x l: %08x b: %016llx\n",
2946 save
->cs
.selector
, save
->cs
.attrib
,
2947 save
->cs
.limit
, save
->cs
.base
);
2948 pr_err("ss: s: %04x a: %04x l: %08x b: %016llx\n",
2949 save
->ss
.selector
, save
->ss
.attrib
,
2950 save
->ss
.limit
, save
->ss
.base
);
2951 pr_err("ds: s: %04x a: %04x l: %08x b: %016llx\n",
2952 save
->ds
.selector
, save
->ds
.attrib
,
2953 save
->ds
.limit
, save
->ds
.base
);
2954 pr_err("fs: s: %04x a: %04x l: %08x b: %016llx\n",
2955 save
->fs
.selector
, save
->fs
.attrib
,
2956 save
->fs
.limit
, save
->fs
.base
);
2957 pr_err("gs: s: %04x a: %04x l: %08x b: %016llx\n",
2958 save
->gs
.selector
, save
->gs
.attrib
,
2959 save
->gs
.limit
, save
->gs
.base
);
2960 pr_err("gdtr: s: %04x a: %04x l: %08x b: %016llx\n",
2961 save
->gdtr
.selector
, save
->gdtr
.attrib
,
2962 save
->gdtr
.limit
, save
->gdtr
.base
);
2963 pr_err("ldtr: s: %04x a: %04x l: %08x b: %016llx\n",
2964 save
->ldtr
.selector
, save
->ldtr
.attrib
,
2965 save
->ldtr
.limit
, save
->ldtr
.base
);
2966 pr_err("idtr: s: %04x a: %04x l: %08x b: %016llx\n",
2967 save
->idtr
.selector
, save
->idtr
.attrib
,
2968 save
->idtr
.limit
, save
->idtr
.base
);
2969 pr_err("tr: s: %04x a: %04x l: %08x b: %016llx\n",
2970 save
->tr
.selector
, save
->tr
.attrib
,
2971 save
->tr
.limit
, save
->tr
.base
);
2972 pr_err("cpl: %d efer: %016llx\n",
2973 save
->cpl
, save
->efer
);
2974 pr_err("cr0: %016llx cr2: %016llx\n",
2975 save
->cr0
, save
->cr2
);
2976 pr_err("cr3: %016llx cr4: %016llx\n",
2977 save
->cr3
, save
->cr4
);
2978 pr_err("dr6: %016llx dr7: %016llx\n",
2979 save
->dr6
, save
->dr7
);
2980 pr_err("rip: %016llx rflags: %016llx\n",
2981 save
->rip
, save
->rflags
);
2982 pr_err("rsp: %016llx rax: %016llx\n",
2983 save
->rsp
, save
->rax
);
2984 pr_err("star: %016llx lstar: %016llx\n",
2985 save
->star
, save
->lstar
);
2986 pr_err("cstar: %016llx sfmask: %016llx\n",
2987 save
->cstar
, save
->sfmask
);
2988 pr_err("kernel_gs_base: %016llx sysenter_cs: %016llx\n",
2989 save
->kernel_gs_base
, save
->sysenter_cs
);
2990 pr_err("sysenter_esp: %016llx sysenter_eip: %016llx\n",
2991 save
->sysenter_esp
, save
->sysenter_eip
);
2992 pr_err("gpat: %016llx dbgctl: %016llx\n",
2993 save
->g_pat
, save
->dbgctl
);
2994 pr_err("br_from: %016llx br_to: %016llx\n",
2995 save
->br_from
, save
->br_to
);
2996 pr_err("excp_from: %016llx excp_to: %016llx\n",
2997 save
->last_excp_from
, save
->last_excp_to
);
3001 static void svm_get_exit_info(struct kvm_vcpu
*vcpu
, u64
*info1
, u64
*info2
)
3003 struct vmcb_control_area
*control
= &to_svm(vcpu
)->vmcb
->control
;
3005 *info1
= control
->exit_info_1
;
3006 *info2
= control
->exit_info_2
;
3009 static int handle_exit(struct kvm_vcpu
*vcpu
)
3011 struct vcpu_svm
*svm
= to_svm(vcpu
);
3012 struct kvm_run
*kvm_run
= vcpu
->run
;
3013 u32 exit_code
= svm
->vmcb
->control
.exit_code
;
3015 trace_kvm_exit(exit_code
, vcpu
, KVM_ISA_SVM
);
3017 if (!is_cr_intercept(svm
, INTERCEPT_CR0_WRITE
))
3018 vcpu
->arch
.cr0
= svm
->vmcb
->save
.cr0
;
3020 vcpu
->arch
.cr3
= svm
->vmcb
->save
.cr3
;
3022 if (unlikely(svm
->nested
.exit_required
)) {
3023 nested_svm_vmexit(svm
);
3024 svm
->nested
.exit_required
= false;
3029 if (is_guest_mode(vcpu
)) {
3032 trace_kvm_nested_vmexit(svm
->vmcb
->save
.rip
, exit_code
,
3033 svm
->vmcb
->control
.exit_info_1
,
3034 svm
->vmcb
->control
.exit_info_2
,
3035 svm
->vmcb
->control
.exit_int_info
,
3036 svm
->vmcb
->control
.exit_int_info_err
);
3038 vmexit
= nested_svm_exit_special(svm
);
3040 if (vmexit
== NESTED_EXIT_CONTINUE
)
3041 vmexit
= nested_svm_exit_handled(svm
);
3043 if (vmexit
== NESTED_EXIT_DONE
)
3047 svm_complete_interrupts(svm
);
3049 if (svm
->vmcb
->control
.exit_code
== SVM_EXIT_ERR
) {
3050 kvm_run
->exit_reason
= KVM_EXIT_FAIL_ENTRY
;
3051 kvm_run
->fail_entry
.hardware_entry_failure_reason
3052 = svm
->vmcb
->control
.exit_code
;
3053 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
3058 if (is_external_interrupt(svm
->vmcb
->control
.exit_int_info
) &&
3059 exit_code
!= SVM_EXIT_EXCP_BASE
+ PF_VECTOR
&&
3060 exit_code
!= SVM_EXIT_NPF
&& exit_code
!= SVM_EXIT_TASK_SWITCH
&&
3061 exit_code
!= SVM_EXIT_INTR
&& exit_code
!= SVM_EXIT_NMI
)
3062 printk(KERN_ERR
"%s: unexpected exit_ini_info 0x%x "
3064 __func__
, svm
->vmcb
->control
.exit_int_info
,
3067 if (exit_code
>= ARRAY_SIZE(svm_exit_handlers
)
3068 || !svm_exit_handlers
[exit_code
]) {
3069 kvm_run
->exit_reason
= KVM_EXIT_UNKNOWN
;
3070 kvm_run
->hw
.hardware_exit_reason
= exit_code
;
3074 return svm_exit_handlers
[exit_code
](svm
);
3077 static void reload_tss(struct kvm_vcpu
*vcpu
)
3079 int cpu
= raw_smp_processor_id();
3081 struct svm_cpu_data
*sd
= per_cpu(svm_data
, cpu
);
3082 sd
->tss_desc
->type
= 9; /* available 32/64-bit TSS */
3086 static void pre_svm_run(struct vcpu_svm
*svm
)
3088 int cpu
= raw_smp_processor_id();
3090 struct svm_cpu_data
*sd
= per_cpu(svm_data
, cpu
);
3092 svm
->vmcb
->control
.tlb_ctl
= TLB_CONTROL_DO_NOTHING
;
3093 /* FIXME: handle wraparound of asid_generation */
3094 if (svm
->asid_generation
!= sd
->asid_generation
)
3098 static void svm_inject_nmi(struct kvm_vcpu
*vcpu
)
3100 struct vcpu_svm
*svm
= to_svm(vcpu
);
3102 svm
->vmcb
->control
.event_inj
= SVM_EVTINJ_VALID
| SVM_EVTINJ_TYPE_NMI
;
3103 vcpu
->arch
.hflags
|= HF_NMI_MASK
;
3104 svm
->vmcb
->control
.intercept
|= (1ULL << INTERCEPT_IRET
);
3105 ++vcpu
->stat
.nmi_injections
;
3108 static inline void svm_inject_irq(struct vcpu_svm
*svm
, int irq
)
3110 struct vmcb_control_area
*control
;
3112 control
= &svm
->vmcb
->control
;
3113 control
->int_vector
= irq
;
3114 control
->int_ctl
&= ~V_INTR_PRIO_MASK
;
3115 control
->int_ctl
|= V_IRQ_MASK
|
3116 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT
);
3119 static void svm_set_irq(struct kvm_vcpu
*vcpu
)
3121 struct vcpu_svm
*svm
= to_svm(vcpu
);
3123 BUG_ON(!(gif_set(svm
)));
3125 trace_kvm_inj_virq(vcpu
->arch
.interrupt
.nr
);
3126 ++vcpu
->stat
.irq_injections
;
3128 svm
->vmcb
->control
.event_inj
= vcpu
->arch
.interrupt
.nr
|
3129 SVM_EVTINJ_VALID
| SVM_EVTINJ_TYPE_INTR
;
3132 static void update_cr8_intercept(struct kvm_vcpu
*vcpu
, int tpr
, int irr
)
3134 struct vcpu_svm
*svm
= to_svm(vcpu
);
3136 if (is_guest_mode(vcpu
) && (vcpu
->arch
.hflags
& HF_VINTR_MASK
))
3143 set_cr_intercept(svm
, INTERCEPT_CR8_WRITE
);
3146 static int svm_nmi_allowed(struct kvm_vcpu
*vcpu
)
3148 struct vcpu_svm
*svm
= to_svm(vcpu
);
3149 struct vmcb
*vmcb
= svm
->vmcb
;
3151 ret
= !(vmcb
->control
.int_state
& SVM_INTERRUPT_SHADOW_MASK
) &&
3152 !(svm
->vcpu
.arch
.hflags
& HF_NMI_MASK
);
3153 ret
= ret
&& gif_set(svm
) && nested_svm_nmi(svm
);
3158 static bool svm_get_nmi_mask(struct kvm_vcpu
*vcpu
)
3160 struct vcpu_svm
*svm
= to_svm(vcpu
);
3162 return !!(svm
->vcpu
.arch
.hflags
& HF_NMI_MASK
);
3165 static void svm_set_nmi_mask(struct kvm_vcpu
*vcpu
, bool masked
)
3167 struct vcpu_svm
*svm
= to_svm(vcpu
);
3170 svm
->vcpu
.arch
.hflags
|= HF_NMI_MASK
;
3171 svm
->vmcb
->control
.intercept
|= (1ULL << INTERCEPT_IRET
);
3173 svm
->vcpu
.arch
.hflags
&= ~HF_NMI_MASK
;
3174 svm
->vmcb
->control
.intercept
&= ~(1ULL << INTERCEPT_IRET
);
3178 static int svm_interrupt_allowed(struct kvm_vcpu
*vcpu
)
3180 struct vcpu_svm
*svm
= to_svm(vcpu
);
3181 struct vmcb
*vmcb
= svm
->vmcb
;
3184 if (!gif_set(svm
) ||
3185 (vmcb
->control
.int_state
& SVM_INTERRUPT_SHADOW_MASK
))
3188 ret
= !!(vmcb
->save
.rflags
& X86_EFLAGS_IF
);
3190 if (is_guest_mode(vcpu
))
3191 return ret
&& !(svm
->vcpu
.arch
.hflags
& HF_VINTR_MASK
);
3196 static void enable_irq_window(struct kvm_vcpu
*vcpu
)
3198 struct vcpu_svm
*svm
= to_svm(vcpu
);
3201 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3202 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3203 * get that intercept, this function will be called again though and
3204 * we'll get the vintr intercept.
3206 if (gif_set(svm
) && nested_svm_intr(svm
)) {
3208 svm_inject_irq(svm
, 0x0);
3212 static void enable_nmi_window(struct kvm_vcpu
*vcpu
)
3214 struct vcpu_svm
*svm
= to_svm(vcpu
);
3216 if ((svm
->vcpu
.arch
.hflags
& (HF_NMI_MASK
| HF_IRET_MASK
))
3218 return; /* IRET will cause a vm exit */
3221 * Something prevents NMI from been injected. Single step over possible
3222 * problem (IRET or exception injection or interrupt shadow)
3224 svm
->nmi_singlestep
= true;
3225 svm
->vmcb
->save
.rflags
|= (X86_EFLAGS_TF
| X86_EFLAGS_RF
);
3226 update_db_intercept(vcpu
);
3229 static int svm_set_tss_addr(struct kvm
*kvm
, unsigned int addr
)
3234 static void svm_flush_tlb(struct kvm_vcpu
*vcpu
)
3236 force_new_asid(vcpu
);
3239 static void svm_prepare_guest_switch(struct kvm_vcpu
*vcpu
)
3243 static inline void sync_cr8_to_lapic(struct kvm_vcpu
*vcpu
)
3245 struct vcpu_svm
*svm
= to_svm(vcpu
);
3247 if (is_guest_mode(vcpu
) && (vcpu
->arch
.hflags
& HF_VINTR_MASK
))
3250 if (!is_cr_intercept(svm
, INTERCEPT_CR8_WRITE
)) {
3251 int cr8
= svm
->vmcb
->control
.int_ctl
& V_TPR_MASK
;
3252 kvm_set_cr8(vcpu
, cr8
);
3256 static inline void sync_lapic_to_cr8(struct kvm_vcpu
*vcpu
)
3258 struct vcpu_svm
*svm
= to_svm(vcpu
);
3261 if (is_guest_mode(vcpu
) && (vcpu
->arch
.hflags
& HF_VINTR_MASK
))
3264 cr8
= kvm_get_cr8(vcpu
);
3265 svm
->vmcb
->control
.int_ctl
&= ~V_TPR_MASK
;
3266 svm
->vmcb
->control
.int_ctl
|= cr8
& V_TPR_MASK
;
3269 static void svm_complete_interrupts(struct vcpu_svm
*svm
)
3273 u32 exitintinfo
= svm
->vmcb
->control
.exit_int_info
;
3274 unsigned int3_injected
= svm
->int3_injected
;
3276 svm
->int3_injected
= 0;
3278 if (svm
->vcpu
.arch
.hflags
& HF_IRET_MASK
) {
3279 svm
->vcpu
.arch
.hflags
&= ~(HF_NMI_MASK
| HF_IRET_MASK
);
3280 kvm_make_request(KVM_REQ_EVENT
, &svm
->vcpu
);
3283 svm
->vcpu
.arch
.nmi_injected
= false;
3284 kvm_clear_exception_queue(&svm
->vcpu
);
3285 kvm_clear_interrupt_queue(&svm
->vcpu
);
3287 if (!(exitintinfo
& SVM_EXITINTINFO_VALID
))
3290 kvm_make_request(KVM_REQ_EVENT
, &svm
->vcpu
);
3292 vector
= exitintinfo
& SVM_EXITINTINFO_VEC_MASK
;
3293 type
= exitintinfo
& SVM_EXITINTINFO_TYPE_MASK
;
3296 case SVM_EXITINTINFO_TYPE_NMI
:
3297 svm
->vcpu
.arch
.nmi_injected
= true;
3299 case SVM_EXITINTINFO_TYPE_EXEPT
:
3301 * In case of software exceptions, do not reinject the vector,
3302 * but re-execute the instruction instead. Rewind RIP first
3303 * if we emulated INT3 before.
3305 if (kvm_exception_is_soft(vector
)) {
3306 if (vector
== BP_VECTOR
&& int3_injected
&&
3307 kvm_is_linear_rip(&svm
->vcpu
, svm
->int3_rip
))
3308 kvm_rip_write(&svm
->vcpu
,
3309 kvm_rip_read(&svm
->vcpu
) -
3313 if (exitintinfo
& SVM_EXITINTINFO_VALID_ERR
) {
3314 u32 err
= svm
->vmcb
->control
.exit_int_info_err
;
3315 kvm_requeue_exception_e(&svm
->vcpu
, vector
, err
);
3318 kvm_requeue_exception(&svm
->vcpu
, vector
);
3320 case SVM_EXITINTINFO_TYPE_INTR
:
3321 kvm_queue_interrupt(&svm
->vcpu
, vector
, false);
3328 static void svm_cancel_injection(struct kvm_vcpu
*vcpu
)
3330 struct vcpu_svm
*svm
= to_svm(vcpu
);
3331 struct vmcb_control_area
*control
= &svm
->vmcb
->control
;
3333 control
->exit_int_info
= control
->event_inj
;
3334 control
->exit_int_info_err
= control
->event_inj_err
;
3335 control
->event_inj
= 0;
3336 svm_complete_interrupts(svm
);
3339 #ifdef CONFIG_X86_64
3345 static void svm_vcpu_run(struct kvm_vcpu
*vcpu
)
3347 struct vcpu_svm
*svm
= to_svm(vcpu
);
3349 svm
->vmcb
->save
.rax
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
3350 svm
->vmcb
->save
.rsp
= vcpu
->arch
.regs
[VCPU_REGS_RSP
];
3351 svm
->vmcb
->save
.rip
= vcpu
->arch
.regs
[VCPU_REGS_RIP
];
3354 * A vmexit emulation is required before the vcpu can be executed
3357 if (unlikely(svm
->nested
.exit_required
))
3362 sync_lapic_to_cr8(vcpu
);
3364 svm
->vmcb
->save
.cr2
= vcpu
->arch
.cr2
;
3371 "push %%"R
"bp; \n\t"
3372 "mov %c[rbx](%[svm]), %%"R
"bx \n\t"
3373 "mov %c[rcx](%[svm]), %%"R
"cx \n\t"
3374 "mov %c[rdx](%[svm]), %%"R
"dx \n\t"
3375 "mov %c[rsi](%[svm]), %%"R
"si \n\t"
3376 "mov %c[rdi](%[svm]), %%"R
"di \n\t"
3377 "mov %c[rbp](%[svm]), %%"R
"bp \n\t"
3378 #ifdef CONFIG_X86_64
3379 "mov %c[r8](%[svm]), %%r8 \n\t"
3380 "mov %c[r9](%[svm]), %%r9 \n\t"
3381 "mov %c[r10](%[svm]), %%r10 \n\t"
3382 "mov %c[r11](%[svm]), %%r11 \n\t"
3383 "mov %c[r12](%[svm]), %%r12 \n\t"
3384 "mov %c[r13](%[svm]), %%r13 \n\t"
3385 "mov %c[r14](%[svm]), %%r14 \n\t"
3386 "mov %c[r15](%[svm]), %%r15 \n\t"
3389 /* Enter guest mode */
3391 "mov %c[vmcb](%[svm]), %%"R
"ax \n\t"
3392 __ex(SVM_VMLOAD
) "\n\t"
3393 __ex(SVM_VMRUN
) "\n\t"
3394 __ex(SVM_VMSAVE
) "\n\t"
3397 /* Save guest registers, load host registers */
3398 "mov %%"R
"bx, %c[rbx](%[svm]) \n\t"
3399 "mov %%"R
"cx, %c[rcx](%[svm]) \n\t"
3400 "mov %%"R
"dx, %c[rdx](%[svm]) \n\t"
3401 "mov %%"R
"si, %c[rsi](%[svm]) \n\t"
3402 "mov %%"R
"di, %c[rdi](%[svm]) \n\t"
3403 "mov %%"R
"bp, %c[rbp](%[svm]) \n\t"
3404 #ifdef CONFIG_X86_64
3405 "mov %%r8, %c[r8](%[svm]) \n\t"
3406 "mov %%r9, %c[r9](%[svm]) \n\t"
3407 "mov %%r10, %c[r10](%[svm]) \n\t"
3408 "mov %%r11, %c[r11](%[svm]) \n\t"
3409 "mov %%r12, %c[r12](%[svm]) \n\t"
3410 "mov %%r13, %c[r13](%[svm]) \n\t"
3411 "mov %%r14, %c[r14](%[svm]) \n\t"
3412 "mov %%r15, %c[r15](%[svm]) \n\t"
3417 [vmcb
]"i"(offsetof(struct vcpu_svm
, vmcb_pa
)),
3418 [rbx
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RBX
])),
3419 [rcx
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RCX
])),
3420 [rdx
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RDX
])),
3421 [rsi
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RSI
])),
3422 [rdi
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RDI
])),
3423 [rbp
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RBP
]))
3424 #ifdef CONFIG_X86_64
3425 , [r8
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R8
])),
3426 [r9
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R9
])),
3427 [r10
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R10
])),
3428 [r11
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R11
])),
3429 [r12
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R12
])),
3430 [r13
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R13
])),
3431 [r14
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R14
])),
3432 [r15
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R15
]))
3435 , R
"bx", R
"cx", R
"dx", R
"si", R
"di"
3436 #ifdef CONFIG_X86_64
3437 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3441 #ifdef CONFIG_X86_64
3442 wrmsrl(MSR_GS_BASE
, svm
->host
.gs_base
);
3444 loadsegment(fs
, svm
->host
.fs
);
3449 local_irq_disable();
3453 vcpu
->arch
.cr2
= svm
->vmcb
->save
.cr2
;
3454 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = svm
->vmcb
->save
.rax
;
3455 vcpu
->arch
.regs
[VCPU_REGS_RSP
] = svm
->vmcb
->save
.rsp
;
3456 vcpu
->arch
.regs
[VCPU_REGS_RIP
] = svm
->vmcb
->save
.rip
;
3458 sync_cr8_to_lapic(vcpu
);
3462 /* if exit due to PF check for async PF */
3463 if (svm
->vmcb
->control
.exit_code
== SVM_EXIT_EXCP_BASE
+ PF_VECTOR
)
3464 svm
->apf_reason
= kvm_read_and_reset_pf_reason();
3467 vcpu
->arch
.regs_avail
&= ~(1 << VCPU_EXREG_PDPTR
);
3468 vcpu
->arch
.regs_dirty
&= ~(1 << VCPU_EXREG_PDPTR
);
3472 * We need to handle MC intercepts here before the vcpu has a chance to
3473 * change the physical cpu
3475 if (unlikely(svm
->vmcb
->control
.exit_code
==
3476 SVM_EXIT_EXCP_BASE
+ MC_VECTOR
))
3477 svm_handle_mce(svm
);
3482 static void svm_set_cr3(struct kvm_vcpu
*vcpu
, unsigned long root
)
3484 struct vcpu_svm
*svm
= to_svm(vcpu
);
3486 svm
->vmcb
->save
.cr3
= root
;
3487 force_new_asid(vcpu
);
3490 static void set_tdp_cr3(struct kvm_vcpu
*vcpu
, unsigned long root
)
3492 struct vcpu_svm
*svm
= to_svm(vcpu
);
3494 svm
->vmcb
->control
.nested_cr3
= root
;
3496 /* Also sync guest cr3 here in case we live migrate */
3497 svm
->vmcb
->save
.cr3
= vcpu
->arch
.cr3
;
3499 force_new_asid(vcpu
);
3502 static int is_disabled(void)
3506 rdmsrl(MSR_VM_CR
, vm_cr
);
3507 if (vm_cr
& (1 << SVM_VM_CR_SVM_DISABLE
))
3514 svm_patch_hypercall(struct kvm_vcpu
*vcpu
, unsigned char *hypercall
)
3517 * Patch in the VMMCALL instruction:
3519 hypercall
[0] = 0x0f;
3520 hypercall
[1] = 0x01;
3521 hypercall
[2] = 0xd9;
3524 static void svm_check_processor_compat(void *rtn
)
3529 static bool svm_cpu_has_accelerated_tpr(void)
3534 static u64
svm_get_mt_mask(struct kvm_vcpu
*vcpu
, gfn_t gfn
, bool is_mmio
)
3539 static void svm_cpuid_update(struct kvm_vcpu
*vcpu
)
3543 static void svm_set_supported_cpuid(u32 func
, struct kvm_cpuid_entry2
*entry
)
3547 /* Mask out xsave bit as long as it is not supported by SVM */
3548 entry
->ecx
&= ~(bit(X86_FEATURE_XSAVE
));
3552 entry
->ecx
|= (1 << 2); /* Set SVM bit */
3555 entry
->eax
= 1; /* SVM revision 1 */
3556 entry
->ebx
= 8; /* Lets support 8 ASIDs in case we add proper
3557 ASID emulation to nested SVM */
3558 entry
->ecx
= 0; /* Reserved */
3559 entry
->edx
= 0; /* Per default do not support any
3560 additional features */
3562 /* Support next_rip if host supports it */
3563 if (boot_cpu_has(X86_FEATURE_NRIPS
))
3564 entry
->edx
|= SVM_FEATURE_NRIP
;
3566 /* Support NPT for the guest if enabled */
3568 entry
->edx
|= SVM_FEATURE_NPT
;
3574 static const struct trace_print_flags svm_exit_reasons_str
[] = {
3575 { SVM_EXIT_READ_CR0
, "read_cr0" },
3576 { SVM_EXIT_READ_CR3
, "read_cr3" },
3577 { SVM_EXIT_READ_CR4
, "read_cr4" },
3578 { SVM_EXIT_READ_CR8
, "read_cr8" },
3579 { SVM_EXIT_WRITE_CR0
, "write_cr0" },
3580 { SVM_EXIT_WRITE_CR3
, "write_cr3" },
3581 { SVM_EXIT_WRITE_CR4
, "write_cr4" },
3582 { SVM_EXIT_WRITE_CR8
, "write_cr8" },
3583 { SVM_EXIT_READ_DR0
, "read_dr0" },
3584 { SVM_EXIT_READ_DR1
, "read_dr1" },
3585 { SVM_EXIT_READ_DR2
, "read_dr2" },
3586 { SVM_EXIT_READ_DR3
, "read_dr3" },
3587 { SVM_EXIT_WRITE_DR0
, "write_dr0" },
3588 { SVM_EXIT_WRITE_DR1
, "write_dr1" },
3589 { SVM_EXIT_WRITE_DR2
, "write_dr2" },
3590 { SVM_EXIT_WRITE_DR3
, "write_dr3" },
3591 { SVM_EXIT_WRITE_DR5
, "write_dr5" },
3592 { SVM_EXIT_WRITE_DR7
, "write_dr7" },
3593 { SVM_EXIT_EXCP_BASE
+ DB_VECTOR
, "DB excp" },
3594 { SVM_EXIT_EXCP_BASE
+ BP_VECTOR
, "BP excp" },
3595 { SVM_EXIT_EXCP_BASE
+ UD_VECTOR
, "UD excp" },
3596 { SVM_EXIT_EXCP_BASE
+ PF_VECTOR
, "PF excp" },
3597 { SVM_EXIT_EXCP_BASE
+ NM_VECTOR
, "NM excp" },
3598 { SVM_EXIT_EXCP_BASE
+ MC_VECTOR
, "MC excp" },
3599 { SVM_EXIT_INTR
, "interrupt" },
3600 { SVM_EXIT_NMI
, "nmi" },
3601 { SVM_EXIT_SMI
, "smi" },
3602 { SVM_EXIT_INIT
, "init" },
3603 { SVM_EXIT_VINTR
, "vintr" },
3604 { SVM_EXIT_CPUID
, "cpuid" },
3605 { SVM_EXIT_INVD
, "invd" },
3606 { SVM_EXIT_HLT
, "hlt" },
3607 { SVM_EXIT_INVLPG
, "invlpg" },
3608 { SVM_EXIT_INVLPGA
, "invlpga" },
3609 { SVM_EXIT_IOIO
, "io" },
3610 { SVM_EXIT_MSR
, "msr" },
3611 { SVM_EXIT_TASK_SWITCH
, "task_switch" },
3612 { SVM_EXIT_SHUTDOWN
, "shutdown" },
3613 { SVM_EXIT_VMRUN
, "vmrun" },
3614 { SVM_EXIT_VMMCALL
, "hypercall" },
3615 { SVM_EXIT_VMLOAD
, "vmload" },
3616 { SVM_EXIT_VMSAVE
, "vmsave" },
3617 { SVM_EXIT_STGI
, "stgi" },
3618 { SVM_EXIT_CLGI
, "clgi" },
3619 { SVM_EXIT_SKINIT
, "skinit" },
3620 { SVM_EXIT_WBINVD
, "wbinvd" },
3621 { SVM_EXIT_MONITOR
, "monitor" },
3622 { SVM_EXIT_MWAIT
, "mwait" },
3623 { SVM_EXIT_NPF
, "npf" },
3627 static int svm_get_lpage_level(void)
3629 return PT_PDPE_LEVEL
;
3632 static bool svm_rdtscp_supported(void)
3637 static bool svm_has_wbinvd_exit(void)
3642 static void svm_fpu_deactivate(struct kvm_vcpu
*vcpu
)
3644 struct vcpu_svm
*svm
= to_svm(vcpu
);
3646 svm
->vmcb
->control
.intercept_exceptions
|= 1 << NM_VECTOR
;
3647 if (is_guest_mode(vcpu
))
3648 svm
->nested
.hsave
->control
.intercept_exceptions
|= 1 << NM_VECTOR
;
3649 update_cr0_intercept(svm
);
3652 static struct kvm_x86_ops svm_x86_ops
= {
3653 .cpu_has_kvm_support
= has_svm
,
3654 .disabled_by_bios
= is_disabled
,
3655 .hardware_setup
= svm_hardware_setup
,
3656 .hardware_unsetup
= svm_hardware_unsetup
,
3657 .check_processor_compatibility
= svm_check_processor_compat
,
3658 .hardware_enable
= svm_hardware_enable
,
3659 .hardware_disable
= svm_hardware_disable
,
3660 .cpu_has_accelerated_tpr
= svm_cpu_has_accelerated_tpr
,
3662 .vcpu_create
= svm_create_vcpu
,
3663 .vcpu_free
= svm_free_vcpu
,
3664 .vcpu_reset
= svm_vcpu_reset
,
3666 .prepare_guest_switch
= svm_prepare_guest_switch
,
3667 .vcpu_load
= svm_vcpu_load
,
3668 .vcpu_put
= svm_vcpu_put
,
3670 .set_guest_debug
= svm_guest_debug
,
3671 .get_msr
= svm_get_msr
,
3672 .set_msr
= svm_set_msr
,
3673 .get_segment_base
= svm_get_segment_base
,
3674 .get_segment
= svm_get_segment
,
3675 .set_segment
= svm_set_segment
,
3676 .get_cpl
= svm_get_cpl
,
3677 .get_cs_db_l_bits
= kvm_get_cs_db_l_bits
,
3678 .decache_cr0_guest_bits
= svm_decache_cr0_guest_bits
,
3679 .decache_cr4_guest_bits
= svm_decache_cr4_guest_bits
,
3680 .set_cr0
= svm_set_cr0
,
3681 .set_cr3
= svm_set_cr3
,
3682 .set_cr4
= svm_set_cr4
,
3683 .set_efer
= svm_set_efer
,
3684 .get_idt
= svm_get_idt
,
3685 .set_idt
= svm_set_idt
,
3686 .get_gdt
= svm_get_gdt
,
3687 .set_gdt
= svm_set_gdt
,
3688 .set_dr7
= svm_set_dr7
,
3689 .cache_reg
= svm_cache_reg
,
3690 .get_rflags
= svm_get_rflags
,
3691 .set_rflags
= svm_set_rflags
,
3692 .fpu_activate
= svm_fpu_activate
,
3693 .fpu_deactivate
= svm_fpu_deactivate
,
3695 .tlb_flush
= svm_flush_tlb
,
3697 .run
= svm_vcpu_run
,
3698 .handle_exit
= handle_exit
,
3699 .skip_emulated_instruction
= skip_emulated_instruction
,
3700 .set_interrupt_shadow
= svm_set_interrupt_shadow
,
3701 .get_interrupt_shadow
= svm_get_interrupt_shadow
,
3702 .patch_hypercall
= svm_patch_hypercall
,
3703 .set_irq
= svm_set_irq
,
3704 .set_nmi
= svm_inject_nmi
,
3705 .queue_exception
= svm_queue_exception
,
3706 .cancel_injection
= svm_cancel_injection
,
3707 .interrupt_allowed
= svm_interrupt_allowed
,
3708 .nmi_allowed
= svm_nmi_allowed
,
3709 .get_nmi_mask
= svm_get_nmi_mask
,
3710 .set_nmi_mask
= svm_set_nmi_mask
,
3711 .enable_nmi_window
= enable_nmi_window
,
3712 .enable_irq_window
= enable_irq_window
,
3713 .update_cr8_intercept
= update_cr8_intercept
,
3715 .set_tss_addr
= svm_set_tss_addr
,
3716 .get_tdp_level
= get_npt_level
,
3717 .get_mt_mask
= svm_get_mt_mask
,
3719 .get_exit_info
= svm_get_exit_info
,
3720 .exit_reasons_str
= svm_exit_reasons_str
,
3722 .get_lpage_level
= svm_get_lpage_level
,
3724 .cpuid_update
= svm_cpuid_update
,
3726 .rdtscp_supported
= svm_rdtscp_supported
,
3728 .set_supported_cpuid
= svm_set_supported_cpuid
,
3730 .has_wbinvd_exit
= svm_has_wbinvd_exit
,
3732 .write_tsc_offset
= svm_write_tsc_offset
,
3733 .adjust_tsc_offset
= svm_adjust_tsc_offset
,
3735 .set_tdp_cr3
= set_tdp_cr3
,
3738 static int __init
svm_init(void)
3740 return kvm_init(&svm_x86_ops
, sizeof(struct vcpu_svm
),
3741 __alignof__(struct vcpu_svm
), THIS_MODULE
);
3744 static void __exit
svm_exit(void)
3749 module_init(svm_init
)
3750 module_exit(svm_exit
)