2 * Kernel-based Virtual Machine driver for Linux
6 * Copyright (C) 2006 Qumranet, Inc.
7 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
10 * Yaniv Kamay <yaniv@qumranet.com>
11 * Avi Kivity <avi@qumranet.com>
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
17 #include <linux/kvm_host.h>
21 #include "kvm_cache_regs.h"
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/vmalloc.h>
27 #include <linux/highmem.h>
28 #include <linux/sched.h>
29 #include <linux/ftrace_event.h>
30 #include <linux/slab.h>
32 #include <asm/tlbflush.h>
34 #include <asm/kvm_para.h>
36 #include <asm/virtext.h>
39 #define __ex(x) __kvm_handle_fault_on_reboot(x)
41 MODULE_AUTHOR("Qumranet");
42 MODULE_LICENSE("GPL");
44 #define IOPM_ALLOC_ORDER 2
45 #define MSRPM_ALLOC_ORDER 1
47 #define SEG_TYPE_LDT 2
48 #define SEG_TYPE_BUSY_TSS16 3
50 #define SVM_FEATURE_NPT (1 << 0)
51 #define SVM_FEATURE_LBRV (1 << 1)
52 #define SVM_FEATURE_SVML (1 << 2)
53 #define SVM_FEATURE_NRIP (1 << 3)
54 #define SVM_FEATURE_PAUSE_FILTER (1 << 10)
56 #define NESTED_EXIT_HOST 0 /* Exit handled on host level */
57 #define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
58 #define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
60 #define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
62 static bool erratum_383_found __read_mostly
;
64 static const u32 host_save_user_msrs
[] = {
66 MSR_STAR
, MSR_LSTAR
, MSR_CSTAR
, MSR_SYSCALL_MASK
, MSR_KERNEL_GS_BASE
,
69 MSR_IA32_SYSENTER_CS
, MSR_IA32_SYSENTER_ESP
, MSR_IA32_SYSENTER_EIP
,
72 #define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
82 /* These are the merged vectors */
85 /* gpa pointers to the real vectors */
89 /* A VMEXIT is required but not yet emulated */
93 * If we vmexit during an instruction emulation we need this to restore
94 * the l1 guest rip after the emulation
96 unsigned long vmexit_rip
;
97 unsigned long vmexit_rsp
;
98 unsigned long vmexit_rax
;
100 /* cache for intercepts of the guest */
103 u32 intercept_exceptions
;
106 /* Nested Paging related state */
110 #define MSRPM_OFFSETS 16
111 static u32 msrpm_offsets
[MSRPM_OFFSETS
] __read_mostly
;
114 struct kvm_vcpu vcpu
;
116 unsigned long vmcb_pa
;
117 struct svm_cpu_data
*svm_data
;
118 uint64_t asid_generation
;
119 uint64_t sysenter_esp
;
120 uint64_t sysenter_eip
;
124 u64 host_user_msrs
[NR_HOST_SAVE_USER_MSRS
];
134 struct nested_state nested
;
138 unsigned int3_injected
;
139 unsigned long int3_rip
;
143 #define MSR_INVALID 0xffffffffU
145 static struct svm_direct_access_msrs
{
146 u32 index
; /* Index of the MSR */
147 bool always
; /* True if intercept is always on */
148 } direct_access_msrs
[] = {
149 { .index
= MSR_STAR
, .always
= true },
150 { .index
= MSR_IA32_SYSENTER_CS
, .always
= true },
152 { .index
= MSR_GS_BASE
, .always
= true },
153 { .index
= MSR_FS_BASE
, .always
= true },
154 { .index
= MSR_KERNEL_GS_BASE
, .always
= true },
155 { .index
= MSR_LSTAR
, .always
= true },
156 { .index
= MSR_CSTAR
, .always
= true },
157 { .index
= MSR_SYSCALL_MASK
, .always
= true },
159 { .index
= MSR_IA32_LASTBRANCHFROMIP
, .always
= false },
160 { .index
= MSR_IA32_LASTBRANCHTOIP
, .always
= false },
161 { .index
= MSR_IA32_LASTINTFROMIP
, .always
= false },
162 { .index
= MSR_IA32_LASTINTTOIP
, .always
= false },
163 { .index
= MSR_INVALID
, .always
= false },
166 /* enable NPT for AMD64 and X86 with PAE */
167 #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
168 static bool npt_enabled
= true;
170 static bool npt_enabled
;
174 module_param(npt
, int, S_IRUGO
);
176 static int nested
= 1;
177 module_param(nested
, int, S_IRUGO
);
179 static void svm_flush_tlb(struct kvm_vcpu
*vcpu
);
180 static void svm_complete_interrupts(struct vcpu_svm
*svm
);
182 static int nested_svm_exit_handled(struct vcpu_svm
*svm
);
183 static int nested_svm_intercept(struct vcpu_svm
*svm
);
184 static int nested_svm_vmexit(struct vcpu_svm
*svm
);
185 static int nested_svm_check_exception(struct vcpu_svm
*svm
, unsigned nr
,
186 bool has_error_code
, u32 error_code
);
189 VMCB_INTERCEPTS
, /* Intercept vectors, TSC offset,
190 pause filter count */
191 VMCB_PERM_MAP
, /* IOPM Base and MSRPM Base */
195 #define VMCB_ALWAYS_DIRTY_MASK 0U
197 static inline void mark_all_dirty(struct vmcb
*vmcb
)
199 vmcb
->control
.clean
= 0;
202 static inline void mark_all_clean(struct vmcb
*vmcb
)
204 vmcb
->control
.clean
= ((1 << VMCB_DIRTY_MAX
) - 1)
205 & ~VMCB_ALWAYS_DIRTY_MASK
;
208 static inline void mark_dirty(struct vmcb
*vmcb
, int bit
)
210 vmcb
->control
.clean
&= ~(1 << bit
);
213 static inline struct vcpu_svm
*to_svm(struct kvm_vcpu
*vcpu
)
215 return container_of(vcpu
, struct vcpu_svm
, vcpu
);
218 static void recalc_intercepts(struct vcpu_svm
*svm
)
220 struct vmcb_control_area
*c
, *h
;
221 struct nested_state
*g
;
223 mark_dirty(svm
->vmcb
, VMCB_INTERCEPTS
);
225 if (!is_guest_mode(&svm
->vcpu
))
228 c
= &svm
->vmcb
->control
;
229 h
= &svm
->nested
.hsave
->control
;
232 c
->intercept_cr
= h
->intercept_cr
| g
->intercept_cr
;
233 c
->intercept_dr
= h
->intercept_dr
| g
->intercept_dr
;
234 c
->intercept_exceptions
= h
->intercept_exceptions
| g
->intercept_exceptions
;
235 c
->intercept
= h
->intercept
| g
->intercept
;
238 static inline struct vmcb
*get_host_vmcb(struct vcpu_svm
*svm
)
240 if (is_guest_mode(&svm
->vcpu
))
241 return svm
->nested
.hsave
;
246 static inline void set_cr_intercept(struct vcpu_svm
*svm
, int bit
)
248 struct vmcb
*vmcb
= get_host_vmcb(svm
);
250 vmcb
->control
.intercept_cr
|= (1U << bit
);
252 recalc_intercepts(svm
);
255 static inline void clr_cr_intercept(struct vcpu_svm
*svm
, int bit
)
257 struct vmcb
*vmcb
= get_host_vmcb(svm
);
259 vmcb
->control
.intercept_cr
&= ~(1U << bit
);
261 recalc_intercepts(svm
);
264 static inline bool is_cr_intercept(struct vcpu_svm
*svm
, int bit
)
266 struct vmcb
*vmcb
= get_host_vmcb(svm
);
268 return vmcb
->control
.intercept_cr
& (1U << bit
);
271 static inline void set_dr_intercept(struct vcpu_svm
*svm
, int bit
)
273 struct vmcb
*vmcb
= get_host_vmcb(svm
);
275 vmcb
->control
.intercept_dr
|= (1U << bit
);
277 recalc_intercepts(svm
);
280 static inline void clr_dr_intercept(struct vcpu_svm
*svm
, int bit
)
282 struct vmcb
*vmcb
= get_host_vmcb(svm
);
284 vmcb
->control
.intercept_dr
&= ~(1U << bit
);
286 recalc_intercepts(svm
);
289 static inline void set_exception_intercept(struct vcpu_svm
*svm
, int bit
)
291 struct vmcb
*vmcb
= get_host_vmcb(svm
);
293 vmcb
->control
.intercept_exceptions
|= (1U << bit
);
295 recalc_intercepts(svm
);
298 static inline void clr_exception_intercept(struct vcpu_svm
*svm
, int bit
)
300 struct vmcb
*vmcb
= get_host_vmcb(svm
);
302 vmcb
->control
.intercept_exceptions
&= ~(1U << bit
);
304 recalc_intercepts(svm
);
307 static inline void set_intercept(struct vcpu_svm
*svm
, int bit
)
309 struct vmcb
*vmcb
= get_host_vmcb(svm
);
311 vmcb
->control
.intercept
|= (1ULL << bit
);
313 recalc_intercepts(svm
);
316 static inline void clr_intercept(struct vcpu_svm
*svm
, int bit
)
318 struct vmcb
*vmcb
= get_host_vmcb(svm
);
320 vmcb
->control
.intercept
&= ~(1ULL << bit
);
322 recalc_intercepts(svm
);
325 static inline void enable_gif(struct vcpu_svm
*svm
)
327 svm
->vcpu
.arch
.hflags
|= HF_GIF_MASK
;
330 static inline void disable_gif(struct vcpu_svm
*svm
)
332 svm
->vcpu
.arch
.hflags
&= ~HF_GIF_MASK
;
335 static inline bool gif_set(struct vcpu_svm
*svm
)
337 return !!(svm
->vcpu
.arch
.hflags
& HF_GIF_MASK
);
340 static unsigned long iopm_base
;
342 struct kvm_ldttss_desc
{
345 unsigned base1
:8, type
:5, dpl
:2, p
:1;
346 unsigned limit1
:4, zero0
:3, g
:1, base2
:8;
349 } __attribute__((packed
));
351 struct svm_cpu_data
{
357 struct kvm_ldttss_desc
*tss_desc
;
359 struct page
*save_area
;
362 static DEFINE_PER_CPU(struct svm_cpu_data
*, svm_data
);
363 static uint32_t svm_features
;
365 struct svm_init_data
{
370 static u32 msrpm_ranges
[] = {0, 0xc0000000, 0xc0010000};
372 #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
373 #define MSRS_RANGE_SIZE 2048
374 #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
376 static u32
svm_msrpm_offset(u32 msr
)
381 for (i
= 0; i
< NUM_MSR_MAPS
; i
++) {
382 if (msr
< msrpm_ranges
[i
] ||
383 msr
>= msrpm_ranges
[i
] + MSRS_IN_RANGE
)
386 offset
= (msr
- msrpm_ranges
[i
]) / 4; /* 4 msrs per u8 */
387 offset
+= (i
* MSRS_RANGE_SIZE
); /* add range offset */
389 /* Now we have the u8 offset - but need the u32 offset */
393 /* MSR not in any range */
397 #define MAX_INST_SIZE 15
399 static inline void clgi(void)
401 asm volatile (__ex(SVM_CLGI
));
404 static inline void stgi(void)
406 asm volatile (__ex(SVM_STGI
));
409 static inline void invlpga(unsigned long addr
, u32 asid
)
411 asm volatile (__ex(SVM_INVLPGA
) : : "a"(addr
), "c"(asid
));
414 static inline void force_new_asid(struct kvm_vcpu
*vcpu
)
416 to_svm(vcpu
)->asid_generation
--;
419 static inline void flush_guest_tlb(struct kvm_vcpu
*vcpu
)
421 force_new_asid(vcpu
);
424 static int get_npt_level(void)
427 return PT64_ROOT_LEVEL
;
429 return PT32E_ROOT_LEVEL
;
433 static void svm_set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
435 vcpu
->arch
.efer
= efer
;
436 if (!npt_enabled
&& !(efer
& EFER_LMA
))
439 to_svm(vcpu
)->vmcb
->save
.efer
= efer
| EFER_SVME
;
442 static int is_external_interrupt(u32 info
)
444 info
&= SVM_EVTINJ_TYPE_MASK
| SVM_EVTINJ_VALID
;
445 return info
== (SVM_EVTINJ_VALID
| SVM_EVTINJ_TYPE_INTR
);
448 static u32
svm_get_interrupt_shadow(struct kvm_vcpu
*vcpu
, int mask
)
450 struct vcpu_svm
*svm
= to_svm(vcpu
);
453 if (svm
->vmcb
->control
.int_state
& SVM_INTERRUPT_SHADOW_MASK
)
454 ret
|= KVM_X86_SHADOW_INT_STI
| KVM_X86_SHADOW_INT_MOV_SS
;
458 static void svm_set_interrupt_shadow(struct kvm_vcpu
*vcpu
, int mask
)
460 struct vcpu_svm
*svm
= to_svm(vcpu
);
463 svm
->vmcb
->control
.int_state
&= ~SVM_INTERRUPT_SHADOW_MASK
;
465 svm
->vmcb
->control
.int_state
|= SVM_INTERRUPT_SHADOW_MASK
;
469 static void skip_emulated_instruction(struct kvm_vcpu
*vcpu
)
471 struct vcpu_svm
*svm
= to_svm(vcpu
);
473 if (svm
->vmcb
->control
.next_rip
!= 0)
474 svm
->next_rip
= svm
->vmcb
->control
.next_rip
;
476 if (!svm
->next_rip
) {
477 if (emulate_instruction(vcpu
, 0, 0, EMULTYPE_SKIP
) !=
479 printk(KERN_DEBUG
"%s: NOP\n", __func__
);
482 if (svm
->next_rip
- kvm_rip_read(vcpu
) > MAX_INST_SIZE
)
483 printk(KERN_ERR
"%s: ip 0x%lx next 0x%llx\n",
484 __func__
, kvm_rip_read(vcpu
), svm
->next_rip
);
486 kvm_rip_write(vcpu
, svm
->next_rip
);
487 svm_set_interrupt_shadow(vcpu
, 0);
490 static void svm_queue_exception(struct kvm_vcpu
*vcpu
, unsigned nr
,
491 bool has_error_code
, u32 error_code
,
494 struct vcpu_svm
*svm
= to_svm(vcpu
);
497 * If we are within a nested VM we'd better #VMEXIT and let the guest
498 * handle the exception
501 nested_svm_check_exception(svm
, nr
, has_error_code
, error_code
))
504 if (nr
== BP_VECTOR
&& !static_cpu_has(X86_FEATURE_NRIPS
)) {
505 unsigned long rip
, old_rip
= kvm_rip_read(&svm
->vcpu
);
508 * For guest debugging where we have to reinject #BP if some
509 * INT3 is guest-owned:
510 * Emulate nRIP by moving RIP forward. Will fail if injection
511 * raises a fault that is not intercepted. Still better than
512 * failing in all cases.
514 skip_emulated_instruction(&svm
->vcpu
);
515 rip
= kvm_rip_read(&svm
->vcpu
);
516 svm
->int3_rip
= rip
+ svm
->vmcb
->save
.cs
.base
;
517 svm
->int3_injected
= rip
- old_rip
;
520 svm
->vmcb
->control
.event_inj
= nr
522 | (has_error_code
? SVM_EVTINJ_VALID_ERR
: 0)
523 | SVM_EVTINJ_TYPE_EXEPT
;
524 svm
->vmcb
->control
.event_inj_err
= error_code
;
527 static void svm_init_erratum_383(void)
533 if (!cpu_has_amd_erratum(amd_erratum_383
))
536 /* Use _safe variants to not break nested virtualization */
537 val
= native_read_msr_safe(MSR_AMD64_DC_CFG
, &err
);
543 low
= lower_32_bits(val
);
544 high
= upper_32_bits(val
);
546 native_write_msr_safe(MSR_AMD64_DC_CFG
, low
, high
);
548 erratum_383_found
= true;
551 static int has_svm(void)
555 if (!cpu_has_svm(&msg
)) {
556 printk(KERN_INFO
"has_svm: %s\n", msg
);
563 static void svm_hardware_disable(void *garbage
)
568 static int svm_hardware_enable(void *garbage
)
571 struct svm_cpu_data
*sd
;
573 struct desc_ptr gdt_descr
;
574 struct desc_struct
*gdt
;
575 int me
= raw_smp_processor_id();
577 rdmsrl(MSR_EFER
, efer
);
578 if (efer
& EFER_SVME
)
582 printk(KERN_ERR
"svm_hardware_enable: err EOPNOTSUPP on %d\n",
586 sd
= per_cpu(svm_data
, me
);
589 printk(KERN_ERR
"svm_hardware_enable: svm_data is NULL on %d\n",
594 sd
->asid_generation
= 1;
595 sd
->max_asid
= cpuid_ebx(SVM_CPUID_FUNC
) - 1;
596 sd
->next_asid
= sd
->max_asid
+ 1;
598 native_store_gdt(&gdt_descr
);
599 gdt
= (struct desc_struct
*)gdt_descr
.address
;
600 sd
->tss_desc
= (struct kvm_ldttss_desc
*)(gdt
+ GDT_ENTRY_TSS
);
602 wrmsrl(MSR_EFER
, efer
| EFER_SVME
);
604 wrmsrl(MSR_VM_HSAVE_PA
, page_to_pfn(sd
->save_area
) << PAGE_SHIFT
);
606 svm_init_erratum_383();
611 static void svm_cpu_uninit(int cpu
)
613 struct svm_cpu_data
*sd
= per_cpu(svm_data
, raw_smp_processor_id());
618 per_cpu(svm_data
, raw_smp_processor_id()) = NULL
;
619 __free_page(sd
->save_area
);
623 static int svm_cpu_init(int cpu
)
625 struct svm_cpu_data
*sd
;
628 sd
= kzalloc(sizeof(struct svm_cpu_data
), GFP_KERNEL
);
632 sd
->save_area
= alloc_page(GFP_KERNEL
);
637 per_cpu(svm_data
, cpu
) = sd
;
647 static bool valid_msr_intercept(u32 index
)
651 for (i
= 0; direct_access_msrs
[i
].index
!= MSR_INVALID
; i
++)
652 if (direct_access_msrs
[i
].index
== index
)
658 static void set_msr_interception(u32
*msrpm
, unsigned msr
,
661 u8 bit_read
, bit_write
;
666 * If this warning triggers extend the direct_access_msrs list at the
667 * beginning of the file
669 WARN_ON(!valid_msr_intercept(msr
));
671 offset
= svm_msrpm_offset(msr
);
672 bit_read
= 2 * (msr
& 0x0f);
673 bit_write
= 2 * (msr
& 0x0f) + 1;
676 BUG_ON(offset
== MSR_INVALID
);
678 read
? clear_bit(bit_read
, &tmp
) : set_bit(bit_read
, &tmp
);
679 write
? clear_bit(bit_write
, &tmp
) : set_bit(bit_write
, &tmp
);
684 static void svm_vcpu_init_msrpm(u32
*msrpm
)
688 memset(msrpm
, 0xff, PAGE_SIZE
* (1 << MSRPM_ALLOC_ORDER
));
690 for (i
= 0; direct_access_msrs
[i
].index
!= MSR_INVALID
; i
++) {
691 if (!direct_access_msrs
[i
].always
)
694 set_msr_interception(msrpm
, direct_access_msrs
[i
].index
, 1, 1);
698 static void add_msr_offset(u32 offset
)
702 for (i
= 0; i
< MSRPM_OFFSETS
; ++i
) {
704 /* Offset already in list? */
705 if (msrpm_offsets
[i
] == offset
)
708 /* Slot used by another offset? */
709 if (msrpm_offsets
[i
] != MSR_INVALID
)
712 /* Add offset to list */
713 msrpm_offsets
[i
] = offset
;
719 * If this BUG triggers the msrpm_offsets table has an overflow. Just
720 * increase MSRPM_OFFSETS in this case.
725 static void init_msrpm_offsets(void)
729 memset(msrpm_offsets
, 0xff, sizeof(msrpm_offsets
));
731 for (i
= 0; direct_access_msrs
[i
].index
!= MSR_INVALID
; i
++) {
734 offset
= svm_msrpm_offset(direct_access_msrs
[i
].index
);
735 BUG_ON(offset
== MSR_INVALID
);
737 add_msr_offset(offset
);
741 static void svm_enable_lbrv(struct vcpu_svm
*svm
)
743 u32
*msrpm
= svm
->msrpm
;
745 svm
->vmcb
->control
.lbr_ctl
= 1;
746 set_msr_interception(msrpm
, MSR_IA32_LASTBRANCHFROMIP
, 1, 1);
747 set_msr_interception(msrpm
, MSR_IA32_LASTBRANCHTOIP
, 1, 1);
748 set_msr_interception(msrpm
, MSR_IA32_LASTINTFROMIP
, 1, 1);
749 set_msr_interception(msrpm
, MSR_IA32_LASTINTTOIP
, 1, 1);
752 static void svm_disable_lbrv(struct vcpu_svm
*svm
)
754 u32
*msrpm
= svm
->msrpm
;
756 svm
->vmcb
->control
.lbr_ctl
= 0;
757 set_msr_interception(msrpm
, MSR_IA32_LASTBRANCHFROMIP
, 0, 0);
758 set_msr_interception(msrpm
, MSR_IA32_LASTBRANCHTOIP
, 0, 0);
759 set_msr_interception(msrpm
, MSR_IA32_LASTINTFROMIP
, 0, 0);
760 set_msr_interception(msrpm
, MSR_IA32_LASTINTTOIP
, 0, 0);
763 static __init
int svm_hardware_setup(void)
766 struct page
*iopm_pages
;
770 iopm_pages
= alloc_pages(GFP_KERNEL
, IOPM_ALLOC_ORDER
);
775 iopm_va
= page_address(iopm_pages
);
776 memset(iopm_va
, 0xff, PAGE_SIZE
* (1 << IOPM_ALLOC_ORDER
));
777 iopm_base
= page_to_pfn(iopm_pages
) << PAGE_SHIFT
;
779 init_msrpm_offsets();
781 if (boot_cpu_has(X86_FEATURE_NX
))
782 kvm_enable_efer_bits(EFER_NX
);
784 if (boot_cpu_has(X86_FEATURE_FXSR_OPT
))
785 kvm_enable_efer_bits(EFER_FFXSR
);
788 printk(KERN_INFO
"kvm: Nested Virtualization enabled\n");
789 kvm_enable_efer_bits(EFER_SVME
| EFER_LMSLE
);
792 for_each_possible_cpu(cpu
) {
793 r
= svm_cpu_init(cpu
);
798 svm_features
= cpuid_edx(SVM_CPUID_FUNC
);
800 if (!boot_cpu_has(X86_FEATURE_NPT
))
803 if (npt_enabled
&& !npt
) {
804 printk(KERN_INFO
"kvm: Nested Paging disabled\n");
809 printk(KERN_INFO
"kvm: Nested Paging enabled\n");
817 __free_pages(iopm_pages
, IOPM_ALLOC_ORDER
);
822 static __exit
void svm_hardware_unsetup(void)
826 for_each_possible_cpu(cpu
)
829 __free_pages(pfn_to_page(iopm_base
>> PAGE_SHIFT
), IOPM_ALLOC_ORDER
);
833 static void init_seg(struct vmcb_seg
*seg
)
836 seg
->attrib
= SVM_SELECTOR_P_MASK
| SVM_SELECTOR_S_MASK
|
837 SVM_SELECTOR_WRITE_MASK
; /* Read/Write Data Segment */
842 static void init_sys_seg(struct vmcb_seg
*seg
, uint32_t type
)
845 seg
->attrib
= SVM_SELECTOR_P_MASK
| type
;
850 static void svm_write_tsc_offset(struct kvm_vcpu
*vcpu
, u64 offset
)
852 struct vcpu_svm
*svm
= to_svm(vcpu
);
853 u64 g_tsc_offset
= 0;
855 if (is_guest_mode(vcpu
)) {
856 g_tsc_offset
= svm
->vmcb
->control
.tsc_offset
-
857 svm
->nested
.hsave
->control
.tsc_offset
;
858 svm
->nested
.hsave
->control
.tsc_offset
= offset
;
861 svm
->vmcb
->control
.tsc_offset
= offset
+ g_tsc_offset
;
863 mark_dirty(svm
->vmcb
, VMCB_INTERCEPTS
);
866 static void svm_adjust_tsc_offset(struct kvm_vcpu
*vcpu
, s64 adjustment
)
868 struct vcpu_svm
*svm
= to_svm(vcpu
);
870 svm
->vmcb
->control
.tsc_offset
+= adjustment
;
871 if (is_guest_mode(vcpu
))
872 svm
->nested
.hsave
->control
.tsc_offset
+= adjustment
;
873 mark_dirty(svm
->vmcb
, VMCB_INTERCEPTS
);
876 static void init_vmcb(struct vcpu_svm
*svm
)
878 struct vmcb_control_area
*control
= &svm
->vmcb
->control
;
879 struct vmcb_save_area
*save
= &svm
->vmcb
->save
;
881 svm
->vcpu
.fpu_active
= 1;
882 svm
->vcpu
.arch
.hflags
= 0;
884 set_cr_intercept(svm
, INTERCEPT_CR0_READ
);
885 set_cr_intercept(svm
, INTERCEPT_CR3_READ
);
886 set_cr_intercept(svm
, INTERCEPT_CR4_READ
);
887 set_cr_intercept(svm
, INTERCEPT_CR0_WRITE
);
888 set_cr_intercept(svm
, INTERCEPT_CR3_WRITE
);
889 set_cr_intercept(svm
, INTERCEPT_CR4_WRITE
);
890 set_cr_intercept(svm
, INTERCEPT_CR8_WRITE
);
892 set_dr_intercept(svm
, INTERCEPT_DR0_READ
);
893 set_dr_intercept(svm
, INTERCEPT_DR1_READ
);
894 set_dr_intercept(svm
, INTERCEPT_DR2_READ
);
895 set_dr_intercept(svm
, INTERCEPT_DR3_READ
);
896 set_dr_intercept(svm
, INTERCEPT_DR4_READ
);
897 set_dr_intercept(svm
, INTERCEPT_DR5_READ
);
898 set_dr_intercept(svm
, INTERCEPT_DR6_READ
);
899 set_dr_intercept(svm
, INTERCEPT_DR7_READ
);
901 set_dr_intercept(svm
, INTERCEPT_DR0_WRITE
);
902 set_dr_intercept(svm
, INTERCEPT_DR1_WRITE
);
903 set_dr_intercept(svm
, INTERCEPT_DR2_WRITE
);
904 set_dr_intercept(svm
, INTERCEPT_DR3_WRITE
);
905 set_dr_intercept(svm
, INTERCEPT_DR4_WRITE
);
906 set_dr_intercept(svm
, INTERCEPT_DR5_WRITE
);
907 set_dr_intercept(svm
, INTERCEPT_DR6_WRITE
);
908 set_dr_intercept(svm
, INTERCEPT_DR7_WRITE
);
910 set_exception_intercept(svm
, PF_VECTOR
);
911 set_exception_intercept(svm
, UD_VECTOR
);
912 set_exception_intercept(svm
, MC_VECTOR
);
914 set_intercept(svm
, INTERCEPT_INTR
);
915 set_intercept(svm
, INTERCEPT_NMI
);
916 set_intercept(svm
, INTERCEPT_SMI
);
917 set_intercept(svm
, INTERCEPT_SELECTIVE_CR0
);
918 set_intercept(svm
, INTERCEPT_CPUID
);
919 set_intercept(svm
, INTERCEPT_INVD
);
920 set_intercept(svm
, INTERCEPT_HLT
);
921 set_intercept(svm
, INTERCEPT_INVLPG
);
922 set_intercept(svm
, INTERCEPT_INVLPGA
);
923 set_intercept(svm
, INTERCEPT_IOIO_PROT
);
924 set_intercept(svm
, INTERCEPT_MSR_PROT
);
925 set_intercept(svm
, INTERCEPT_TASK_SWITCH
);
926 set_intercept(svm
, INTERCEPT_SHUTDOWN
);
927 set_intercept(svm
, INTERCEPT_VMRUN
);
928 set_intercept(svm
, INTERCEPT_VMMCALL
);
929 set_intercept(svm
, INTERCEPT_VMLOAD
);
930 set_intercept(svm
, INTERCEPT_VMSAVE
);
931 set_intercept(svm
, INTERCEPT_STGI
);
932 set_intercept(svm
, INTERCEPT_CLGI
);
933 set_intercept(svm
, INTERCEPT_SKINIT
);
934 set_intercept(svm
, INTERCEPT_WBINVD
);
935 set_intercept(svm
, INTERCEPT_MONITOR
);
936 set_intercept(svm
, INTERCEPT_MWAIT
);
938 control
->iopm_base_pa
= iopm_base
;
939 control
->msrpm_base_pa
= __pa(svm
->msrpm
);
940 control
->int_ctl
= V_INTR_MASKING_MASK
;
948 save
->cs
.selector
= 0xf000;
949 /* Executable/Readable Code Segment */
950 save
->cs
.attrib
= SVM_SELECTOR_READ_MASK
| SVM_SELECTOR_P_MASK
|
951 SVM_SELECTOR_S_MASK
| SVM_SELECTOR_CODE_MASK
;
952 save
->cs
.limit
= 0xffff;
954 * cs.base should really be 0xffff0000, but vmx can't handle that, so
955 * be consistent with it.
957 * Replace when we have real mode working for vmx.
959 save
->cs
.base
= 0xf0000;
961 save
->gdtr
.limit
= 0xffff;
962 save
->idtr
.limit
= 0xffff;
964 init_sys_seg(&save
->ldtr
, SEG_TYPE_LDT
);
965 init_sys_seg(&save
->tr
, SEG_TYPE_BUSY_TSS16
);
967 svm_set_efer(&svm
->vcpu
, 0);
968 save
->dr6
= 0xffff0ff0;
971 save
->rip
= 0x0000fff0;
972 svm
->vcpu
.arch
.regs
[VCPU_REGS_RIP
] = save
->rip
;
975 * This is the guest-visible cr0 value.
976 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
978 svm
->vcpu
.arch
.cr0
= 0;
979 (void)kvm_set_cr0(&svm
->vcpu
, X86_CR0_NW
| X86_CR0_CD
| X86_CR0_ET
);
981 save
->cr4
= X86_CR4_PAE
;
985 /* Setup VMCB for Nested Paging */
986 control
->nested_ctl
= 1;
987 clr_intercept(svm
, INTERCEPT_TASK_SWITCH
);
988 clr_intercept(svm
, INTERCEPT_INVLPG
);
989 clr_exception_intercept(svm
, PF_VECTOR
);
990 clr_cr_intercept(svm
, INTERCEPT_CR3_READ
);
991 clr_cr_intercept(svm
, INTERCEPT_CR3_WRITE
);
992 save
->g_pat
= 0x0007040600070406ULL
;
996 force_new_asid(&svm
->vcpu
);
998 svm
->nested
.vmcb
= 0;
999 svm
->vcpu
.arch
.hflags
= 0;
1001 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER
)) {
1002 control
->pause_filter_count
= 3000;
1003 set_intercept(svm
, INTERCEPT_PAUSE
);
1006 mark_all_dirty(svm
->vmcb
);
1011 static int svm_vcpu_reset(struct kvm_vcpu
*vcpu
)
1013 struct vcpu_svm
*svm
= to_svm(vcpu
);
1017 if (!kvm_vcpu_is_bsp(vcpu
)) {
1018 kvm_rip_write(vcpu
, 0);
1019 svm
->vmcb
->save
.cs
.base
= svm
->vcpu
.arch
.sipi_vector
<< 12;
1020 svm
->vmcb
->save
.cs
.selector
= svm
->vcpu
.arch
.sipi_vector
<< 8;
1022 vcpu
->arch
.regs_avail
= ~0;
1023 vcpu
->arch
.regs_dirty
= ~0;
1028 static struct kvm_vcpu
*svm_create_vcpu(struct kvm
*kvm
, unsigned int id
)
1030 struct vcpu_svm
*svm
;
1032 struct page
*msrpm_pages
;
1033 struct page
*hsave_page
;
1034 struct page
*nested_msrpm_pages
;
1037 svm
= kmem_cache_zalloc(kvm_vcpu_cache
, GFP_KERNEL
);
1043 err
= kvm_vcpu_init(&svm
->vcpu
, kvm
, id
);
1048 page
= alloc_page(GFP_KERNEL
);
1052 msrpm_pages
= alloc_pages(GFP_KERNEL
, MSRPM_ALLOC_ORDER
);
1056 nested_msrpm_pages
= alloc_pages(GFP_KERNEL
, MSRPM_ALLOC_ORDER
);
1057 if (!nested_msrpm_pages
)
1060 hsave_page
= alloc_page(GFP_KERNEL
);
1064 svm
->nested
.hsave
= page_address(hsave_page
);
1066 svm
->msrpm
= page_address(msrpm_pages
);
1067 svm_vcpu_init_msrpm(svm
->msrpm
);
1069 svm
->nested
.msrpm
= page_address(nested_msrpm_pages
);
1070 svm_vcpu_init_msrpm(svm
->nested
.msrpm
);
1072 svm
->vmcb
= page_address(page
);
1073 clear_page(svm
->vmcb
);
1074 svm
->vmcb_pa
= page_to_pfn(page
) << PAGE_SHIFT
;
1075 svm
->asid_generation
= 0;
1077 kvm_write_tsc(&svm
->vcpu
, 0);
1079 err
= fx_init(&svm
->vcpu
);
1083 svm
->vcpu
.arch
.apic_base
= 0xfee00000 | MSR_IA32_APICBASE_ENABLE
;
1084 if (kvm_vcpu_is_bsp(&svm
->vcpu
))
1085 svm
->vcpu
.arch
.apic_base
|= MSR_IA32_APICBASE_BSP
;
1090 __free_page(hsave_page
);
1092 __free_pages(nested_msrpm_pages
, MSRPM_ALLOC_ORDER
);
1094 __free_pages(msrpm_pages
, MSRPM_ALLOC_ORDER
);
1098 kvm_vcpu_uninit(&svm
->vcpu
);
1100 kmem_cache_free(kvm_vcpu_cache
, svm
);
1102 return ERR_PTR(err
);
1105 static void svm_free_vcpu(struct kvm_vcpu
*vcpu
)
1107 struct vcpu_svm
*svm
= to_svm(vcpu
);
1109 __free_page(pfn_to_page(svm
->vmcb_pa
>> PAGE_SHIFT
));
1110 __free_pages(virt_to_page(svm
->msrpm
), MSRPM_ALLOC_ORDER
);
1111 __free_page(virt_to_page(svm
->nested
.hsave
));
1112 __free_pages(virt_to_page(svm
->nested
.msrpm
), MSRPM_ALLOC_ORDER
);
1113 kvm_vcpu_uninit(vcpu
);
1114 kmem_cache_free(kvm_vcpu_cache
, svm
);
1117 static void svm_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
1119 struct vcpu_svm
*svm
= to_svm(vcpu
);
1122 if (unlikely(cpu
!= vcpu
->cpu
)) {
1123 svm
->asid_generation
= 0;
1124 mark_all_dirty(svm
->vmcb
);
1127 #ifdef CONFIG_X86_64
1128 rdmsrl(MSR_GS_BASE
, to_svm(vcpu
)->host
.gs_base
);
1130 savesegment(fs
, svm
->host
.fs
);
1131 savesegment(gs
, svm
->host
.gs
);
1132 svm
->host
.ldt
= kvm_read_ldt();
1134 for (i
= 0; i
< NR_HOST_SAVE_USER_MSRS
; i
++)
1135 rdmsrl(host_save_user_msrs
[i
], svm
->host_user_msrs
[i
]);
1138 static void svm_vcpu_put(struct kvm_vcpu
*vcpu
)
1140 struct vcpu_svm
*svm
= to_svm(vcpu
);
1143 ++vcpu
->stat
.host_state_reload
;
1144 kvm_load_ldt(svm
->host
.ldt
);
1145 #ifdef CONFIG_X86_64
1146 loadsegment(fs
, svm
->host
.fs
);
1147 load_gs_index(svm
->host
.gs
);
1148 wrmsrl(MSR_KERNEL_GS_BASE
, current
->thread
.gs
);
1150 loadsegment(gs
, svm
->host
.gs
);
1152 for (i
= 0; i
< NR_HOST_SAVE_USER_MSRS
; i
++)
1153 wrmsrl(host_save_user_msrs
[i
], svm
->host_user_msrs
[i
]);
1156 static unsigned long svm_get_rflags(struct kvm_vcpu
*vcpu
)
1158 return to_svm(vcpu
)->vmcb
->save
.rflags
;
1161 static void svm_set_rflags(struct kvm_vcpu
*vcpu
, unsigned long rflags
)
1163 to_svm(vcpu
)->vmcb
->save
.rflags
= rflags
;
1166 static void svm_cache_reg(struct kvm_vcpu
*vcpu
, enum kvm_reg reg
)
1169 case VCPU_EXREG_PDPTR
:
1170 BUG_ON(!npt_enabled
);
1171 load_pdptrs(vcpu
, vcpu
->arch
.walk_mmu
, vcpu
->arch
.cr3
);
1178 static void svm_set_vintr(struct vcpu_svm
*svm
)
1180 set_intercept(svm
, INTERCEPT_VINTR
);
1183 static void svm_clear_vintr(struct vcpu_svm
*svm
)
1185 clr_intercept(svm
, INTERCEPT_VINTR
);
1188 static struct vmcb_seg
*svm_seg(struct kvm_vcpu
*vcpu
, int seg
)
1190 struct vmcb_save_area
*save
= &to_svm(vcpu
)->vmcb
->save
;
1193 case VCPU_SREG_CS
: return &save
->cs
;
1194 case VCPU_SREG_DS
: return &save
->ds
;
1195 case VCPU_SREG_ES
: return &save
->es
;
1196 case VCPU_SREG_FS
: return &save
->fs
;
1197 case VCPU_SREG_GS
: return &save
->gs
;
1198 case VCPU_SREG_SS
: return &save
->ss
;
1199 case VCPU_SREG_TR
: return &save
->tr
;
1200 case VCPU_SREG_LDTR
: return &save
->ldtr
;
1206 static u64
svm_get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
1208 struct vmcb_seg
*s
= svm_seg(vcpu
, seg
);
1213 static void svm_get_segment(struct kvm_vcpu
*vcpu
,
1214 struct kvm_segment
*var
, int seg
)
1216 struct vmcb_seg
*s
= svm_seg(vcpu
, seg
);
1218 var
->base
= s
->base
;
1219 var
->limit
= s
->limit
;
1220 var
->selector
= s
->selector
;
1221 var
->type
= s
->attrib
& SVM_SELECTOR_TYPE_MASK
;
1222 var
->s
= (s
->attrib
>> SVM_SELECTOR_S_SHIFT
) & 1;
1223 var
->dpl
= (s
->attrib
>> SVM_SELECTOR_DPL_SHIFT
) & 3;
1224 var
->present
= (s
->attrib
>> SVM_SELECTOR_P_SHIFT
) & 1;
1225 var
->avl
= (s
->attrib
>> SVM_SELECTOR_AVL_SHIFT
) & 1;
1226 var
->l
= (s
->attrib
>> SVM_SELECTOR_L_SHIFT
) & 1;
1227 var
->db
= (s
->attrib
>> SVM_SELECTOR_DB_SHIFT
) & 1;
1228 var
->g
= (s
->attrib
>> SVM_SELECTOR_G_SHIFT
) & 1;
1231 * AMD's VMCB does not have an explicit unusable field, so emulate it
1232 * for cross vendor migration purposes by "not present"
1234 var
->unusable
= !var
->present
|| (var
->type
== 0);
1239 * SVM always stores 0 for the 'G' bit in the CS selector in
1240 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1241 * Intel's VMENTRY has a check on the 'G' bit.
1243 var
->g
= s
->limit
> 0xfffff;
1247 * Work around a bug where the busy flag in the tr selector
1257 * The accessed bit must always be set in the segment
1258 * descriptor cache, although it can be cleared in the
1259 * descriptor, the cached bit always remains at 1. Since
1260 * Intel has a check on this, set it here to support
1261 * cross-vendor migration.
1268 * On AMD CPUs sometimes the DB bit in the segment
1269 * descriptor is left as 1, although the whole segment has
1270 * been made unusable. Clear it here to pass an Intel VMX
1271 * entry check when cross vendor migrating.
1279 static int svm_get_cpl(struct kvm_vcpu
*vcpu
)
1281 struct vmcb_save_area
*save
= &to_svm(vcpu
)->vmcb
->save
;
1286 static void svm_get_idt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
1288 struct vcpu_svm
*svm
= to_svm(vcpu
);
1290 dt
->size
= svm
->vmcb
->save
.idtr
.limit
;
1291 dt
->address
= svm
->vmcb
->save
.idtr
.base
;
1294 static void svm_set_idt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
1296 struct vcpu_svm
*svm
= to_svm(vcpu
);
1298 svm
->vmcb
->save
.idtr
.limit
= dt
->size
;
1299 svm
->vmcb
->save
.idtr
.base
= dt
->address
;
1302 static void svm_get_gdt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
1304 struct vcpu_svm
*svm
= to_svm(vcpu
);
1306 dt
->size
= svm
->vmcb
->save
.gdtr
.limit
;
1307 dt
->address
= svm
->vmcb
->save
.gdtr
.base
;
1310 static void svm_set_gdt(struct kvm_vcpu
*vcpu
, struct desc_ptr
*dt
)
1312 struct vcpu_svm
*svm
= to_svm(vcpu
);
1314 svm
->vmcb
->save
.gdtr
.limit
= dt
->size
;
1315 svm
->vmcb
->save
.gdtr
.base
= dt
->address
;
1318 static void svm_decache_cr0_guest_bits(struct kvm_vcpu
*vcpu
)
1322 static void svm_decache_cr4_guest_bits(struct kvm_vcpu
*vcpu
)
1326 static void update_cr0_intercept(struct vcpu_svm
*svm
)
1328 ulong gcr0
= svm
->vcpu
.arch
.cr0
;
1329 u64
*hcr0
= &svm
->vmcb
->save
.cr0
;
1331 if (!svm
->vcpu
.fpu_active
)
1332 *hcr0
|= SVM_CR0_SELECTIVE_MASK
;
1334 *hcr0
= (*hcr0
& ~SVM_CR0_SELECTIVE_MASK
)
1335 | (gcr0
& SVM_CR0_SELECTIVE_MASK
);
1338 if (gcr0
== *hcr0
&& svm
->vcpu
.fpu_active
) {
1339 clr_cr_intercept(svm
, INTERCEPT_CR0_READ
);
1340 clr_cr_intercept(svm
, INTERCEPT_CR0_WRITE
);
1342 set_cr_intercept(svm
, INTERCEPT_CR0_READ
);
1343 set_cr_intercept(svm
, INTERCEPT_CR0_WRITE
);
1347 static void svm_set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
1349 struct vcpu_svm
*svm
= to_svm(vcpu
);
1351 if (is_guest_mode(vcpu
)) {
1353 * We are here because we run in nested mode, the host kvm
1354 * intercepts cr0 writes but the l1 hypervisor does not.
1355 * But the L1 hypervisor may intercept selective cr0 writes.
1356 * This needs to be checked here.
1358 unsigned long old
, new;
1360 /* Remove bits that would trigger a real cr0 write intercept */
1361 old
= vcpu
->arch
.cr0
& SVM_CR0_SELECTIVE_MASK
;
1362 new = cr0
& SVM_CR0_SELECTIVE_MASK
;
1365 /* cr0 write with ts and mp unchanged */
1366 svm
->vmcb
->control
.exit_code
= SVM_EXIT_CR0_SEL_WRITE
;
1367 if (nested_svm_exit_handled(svm
) == NESTED_EXIT_DONE
) {
1368 svm
->nested
.vmexit_rip
= kvm_rip_read(vcpu
);
1369 svm
->nested
.vmexit_rsp
= kvm_register_read(vcpu
, VCPU_REGS_RSP
);
1370 svm
->nested
.vmexit_rax
= kvm_register_read(vcpu
, VCPU_REGS_RAX
);
1376 #ifdef CONFIG_X86_64
1377 if (vcpu
->arch
.efer
& EFER_LME
) {
1378 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
)) {
1379 vcpu
->arch
.efer
|= EFER_LMA
;
1380 svm
->vmcb
->save
.efer
|= EFER_LMA
| EFER_LME
;
1383 if (is_paging(vcpu
) && !(cr0
& X86_CR0_PG
)) {
1384 vcpu
->arch
.efer
&= ~EFER_LMA
;
1385 svm
->vmcb
->save
.efer
&= ~(EFER_LMA
| EFER_LME
);
1389 vcpu
->arch
.cr0
= cr0
;
1392 cr0
|= X86_CR0_PG
| X86_CR0_WP
;
1394 if (!vcpu
->fpu_active
)
1397 * re-enable caching here because the QEMU bios
1398 * does not do it - this results in some delay at
1401 cr0
&= ~(X86_CR0_CD
| X86_CR0_NW
);
1402 svm
->vmcb
->save
.cr0
= cr0
;
1403 update_cr0_intercept(svm
);
1406 static void svm_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
1408 unsigned long host_cr4_mce
= read_cr4() & X86_CR4_MCE
;
1409 unsigned long old_cr4
= to_svm(vcpu
)->vmcb
->save
.cr4
;
1411 if (npt_enabled
&& ((old_cr4
^ cr4
) & X86_CR4_PGE
))
1412 force_new_asid(vcpu
);
1414 vcpu
->arch
.cr4
= cr4
;
1417 cr4
|= host_cr4_mce
;
1418 to_svm(vcpu
)->vmcb
->save
.cr4
= cr4
;
1421 static void svm_set_segment(struct kvm_vcpu
*vcpu
,
1422 struct kvm_segment
*var
, int seg
)
1424 struct vcpu_svm
*svm
= to_svm(vcpu
);
1425 struct vmcb_seg
*s
= svm_seg(vcpu
, seg
);
1427 s
->base
= var
->base
;
1428 s
->limit
= var
->limit
;
1429 s
->selector
= var
->selector
;
1433 s
->attrib
= (var
->type
& SVM_SELECTOR_TYPE_MASK
);
1434 s
->attrib
|= (var
->s
& 1) << SVM_SELECTOR_S_SHIFT
;
1435 s
->attrib
|= (var
->dpl
& 3) << SVM_SELECTOR_DPL_SHIFT
;
1436 s
->attrib
|= (var
->present
& 1) << SVM_SELECTOR_P_SHIFT
;
1437 s
->attrib
|= (var
->avl
& 1) << SVM_SELECTOR_AVL_SHIFT
;
1438 s
->attrib
|= (var
->l
& 1) << SVM_SELECTOR_L_SHIFT
;
1439 s
->attrib
|= (var
->db
& 1) << SVM_SELECTOR_DB_SHIFT
;
1440 s
->attrib
|= (var
->g
& 1) << SVM_SELECTOR_G_SHIFT
;
1442 if (seg
== VCPU_SREG_CS
)
1444 = (svm
->vmcb
->save
.cs
.attrib
1445 >> SVM_SELECTOR_DPL_SHIFT
) & 3;
1449 static void update_db_intercept(struct kvm_vcpu
*vcpu
)
1451 struct vcpu_svm
*svm
= to_svm(vcpu
);
1453 clr_exception_intercept(svm
, DB_VECTOR
);
1454 clr_exception_intercept(svm
, BP_VECTOR
);
1456 if (svm
->nmi_singlestep
)
1457 set_exception_intercept(svm
, DB_VECTOR
);
1459 if (vcpu
->guest_debug
& KVM_GUESTDBG_ENABLE
) {
1460 if (vcpu
->guest_debug
&
1461 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
))
1462 set_exception_intercept(svm
, DB_VECTOR
);
1463 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_SW_BP
)
1464 set_exception_intercept(svm
, BP_VECTOR
);
1466 vcpu
->guest_debug
= 0;
1469 static void svm_guest_debug(struct kvm_vcpu
*vcpu
, struct kvm_guest_debug
*dbg
)
1471 struct vcpu_svm
*svm
= to_svm(vcpu
);
1473 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
)
1474 svm
->vmcb
->save
.dr7
= dbg
->arch
.debugreg
[7];
1476 svm
->vmcb
->save
.dr7
= vcpu
->arch
.dr7
;
1478 update_db_intercept(vcpu
);
1481 static void new_asid(struct vcpu_svm
*svm
, struct svm_cpu_data
*sd
)
1483 if (sd
->next_asid
> sd
->max_asid
) {
1484 ++sd
->asid_generation
;
1486 svm
->vmcb
->control
.tlb_ctl
= TLB_CONTROL_FLUSH_ALL_ASID
;
1489 svm
->asid_generation
= sd
->asid_generation
;
1490 svm
->vmcb
->control
.asid
= sd
->next_asid
++;
1493 static void svm_set_dr7(struct kvm_vcpu
*vcpu
, unsigned long value
)
1495 struct vcpu_svm
*svm
= to_svm(vcpu
);
1497 svm
->vmcb
->save
.dr7
= value
;
1500 static int pf_interception(struct vcpu_svm
*svm
)
1502 u64 fault_address
= svm
->vmcb
->control
.exit_info_2
;
1506 switch (svm
->apf_reason
) {
1508 error_code
= svm
->vmcb
->control
.exit_info_1
;
1510 trace_kvm_page_fault(fault_address
, error_code
);
1511 if (!npt_enabled
&& kvm_event_needs_reinjection(&svm
->vcpu
))
1512 kvm_mmu_unprotect_page_virt(&svm
->vcpu
, fault_address
);
1513 r
= kvm_mmu_page_fault(&svm
->vcpu
, fault_address
, error_code
);
1515 case KVM_PV_REASON_PAGE_NOT_PRESENT
:
1516 svm
->apf_reason
= 0;
1517 local_irq_disable();
1518 kvm_async_pf_task_wait(fault_address
);
1521 case KVM_PV_REASON_PAGE_READY
:
1522 svm
->apf_reason
= 0;
1523 local_irq_disable();
1524 kvm_async_pf_task_wake(fault_address
);
1531 static int db_interception(struct vcpu_svm
*svm
)
1533 struct kvm_run
*kvm_run
= svm
->vcpu
.run
;
1535 if (!(svm
->vcpu
.guest_debug
&
1536 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
)) &&
1537 !svm
->nmi_singlestep
) {
1538 kvm_queue_exception(&svm
->vcpu
, DB_VECTOR
);
1542 if (svm
->nmi_singlestep
) {
1543 svm
->nmi_singlestep
= false;
1544 if (!(svm
->vcpu
.guest_debug
& KVM_GUESTDBG_SINGLESTEP
))
1545 svm
->vmcb
->save
.rflags
&=
1546 ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
1547 update_db_intercept(&svm
->vcpu
);
1550 if (svm
->vcpu
.guest_debug
&
1551 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
)) {
1552 kvm_run
->exit_reason
= KVM_EXIT_DEBUG
;
1553 kvm_run
->debug
.arch
.pc
=
1554 svm
->vmcb
->save
.cs
.base
+ svm
->vmcb
->save
.rip
;
1555 kvm_run
->debug
.arch
.exception
= DB_VECTOR
;
1562 static int bp_interception(struct vcpu_svm
*svm
)
1564 struct kvm_run
*kvm_run
= svm
->vcpu
.run
;
1566 kvm_run
->exit_reason
= KVM_EXIT_DEBUG
;
1567 kvm_run
->debug
.arch
.pc
= svm
->vmcb
->save
.cs
.base
+ svm
->vmcb
->save
.rip
;
1568 kvm_run
->debug
.arch
.exception
= BP_VECTOR
;
1572 static int ud_interception(struct vcpu_svm
*svm
)
1576 er
= emulate_instruction(&svm
->vcpu
, 0, 0, EMULTYPE_TRAP_UD
);
1577 if (er
!= EMULATE_DONE
)
1578 kvm_queue_exception(&svm
->vcpu
, UD_VECTOR
);
1582 static void svm_fpu_activate(struct kvm_vcpu
*vcpu
)
1584 struct vcpu_svm
*svm
= to_svm(vcpu
);
1586 clr_exception_intercept(svm
, NM_VECTOR
);
1588 svm
->vcpu
.fpu_active
= 1;
1589 update_cr0_intercept(svm
);
1592 static int nm_interception(struct vcpu_svm
*svm
)
1594 svm_fpu_activate(&svm
->vcpu
);
1598 static bool is_erratum_383(void)
1603 if (!erratum_383_found
)
1606 value
= native_read_msr_safe(MSR_IA32_MC0_STATUS
, &err
);
1610 /* Bit 62 may or may not be set for this mce */
1611 value
&= ~(1ULL << 62);
1613 if (value
!= 0xb600000000010015ULL
)
1616 /* Clear MCi_STATUS registers */
1617 for (i
= 0; i
< 6; ++i
)
1618 native_write_msr_safe(MSR_IA32_MCx_STATUS(i
), 0, 0);
1620 value
= native_read_msr_safe(MSR_IA32_MCG_STATUS
, &err
);
1624 value
&= ~(1ULL << 2);
1625 low
= lower_32_bits(value
);
1626 high
= upper_32_bits(value
);
1628 native_write_msr_safe(MSR_IA32_MCG_STATUS
, low
, high
);
1631 /* Flush tlb to evict multi-match entries */
1637 static void svm_handle_mce(struct vcpu_svm
*svm
)
1639 if (is_erratum_383()) {
1641 * Erratum 383 triggered. Guest state is corrupt so kill the
1644 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1646 kvm_make_request(KVM_REQ_TRIPLE_FAULT
, &svm
->vcpu
);
1652 * On an #MC intercept the MCE handler is not called automatically in
1653 * the host. So do it by hand here.
1657 /* not sure if we ever come back to this point */
1662 static int mc_interception(struct vcpu_svm
*svm
)
1667 static int shutdown_interception(struct vcpu_svm
*svm
)
1669 struct kvm_run
*kvm_run
= svm
->vcpu
.run
;
1672 * VMCB is undefined after a SHUTDOWN intercept
1673 * so reinitialize it.
1675 clear_page(svm
->vmcb
);
1678 kvm_run
->exit_reason
= KVM_EXIT_SHUTDOWN
;
1682 static int io_interception(struct vcpu_svm
*svm
)
1684 struct kvm_vcpu
*vcpu
= &svm
->vcpu
;
1685 u32 io_info
= svm
->vmcb
->control
.exit_info_1
; /* address size bug? */
1686 int size
, in
, string
;
1689 ++svm
->vcpu
.stat
.io_exits
;
1690 string
= (io_info
& SVM_IOIO_STR_MASK
) != 0;
1691 in
= (io_info
& SVM_IOIO_TYPE_MASK
) != 0;
1693 return emulate_instruction(vcpu
, 0, 0, 0) == EMULATE_DONE
;
1695 port
= io_info
>> 16;
1696 size
= (io_info
& SVM_IOIO_SIZE_MASK
) >> SVM_IOIO_SIZE_SHIFT
;
1697 svm
->next_rip
= svm
->vmcb
->control
.exit_info_2
;
1698 skip_emulated_instruction(&svm
->vcpu
);
1700 return kvm_fast_pio_out(vcpu
, size
, port
);
1703 static int nmi_interception(struct vcpu_svm
*svm
)
1708 static int intr_interception(struct vcpu_svm
*svm
)
1710 ++svm
->vcpu
.stat
.irq_exits
;
1714 static int nop_on_interception(struct vcpu_svm
*svm
)
1719 static int halt_interception(struct vcpu_svm
*svm
)
1721 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 1;
1722 skip_emulated_instruction(&svm
->vcpu
);
1723 return kvm_emulate_halt(&svm
->vcpu
);
1726 static int vmmcall_interception(struct vcpu_svm
*svm
)
1728 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
1729 skip_emulated_instruction(&svm
->vcpu
);
1730 kvm_emulate_hypercall(&svm
->vcpu
);
1734 static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu
*vcpu
)
1736 struct vcpu_svm
*svm
= to_svm(vcpu
);
1738 return svm
->nested
.nested_cr3
;
1741 static void nested_svm_set_tdp_cr3(struct kvm_vcpu
*vcpu
,
1744 struct vcpu_svm
*svm
= to_svm(vcpu
);
1746 svm
->vmcb
->control
.nested_cr3
= root
;
1747 force_new_asid(vcpu
);
1750 static void nested_svm_inject_npf_exit(struct kvm_vcpu
*vcpu
,
1751 struct x86_exception
*fault
)
1753 struct vcpu_svm
*svm
= to_svm(vcpu
);
1755 svm
->vmcb
->control
.exit_code
= SVM_EXIT_NPF
;
1756 svm
->vmcb
->control
.exit_code_hi
= 0;
1757 svm
->vmcb
->control
.exit_info_1
= fault
->error_code
;
1758 svm
->vmcb
->control
.exit_info_2
= fault
->address
;
1760 nested_svm_vmexit(svm
);
1763 static int nested_svm_init_mmu_context(struct kvm_vcpu
*vcpu
)
1767 r
= kvm_init_shadow_mmu(vcpu
, &vcpu
->arch
.mmu
);
1769 vcpu
->arch
.mmu
.set_cr3
= nested_svm_set_tdp_cr3
;
1770 vcpu
->arch
.mmu
.get_cr3
= nested_svm_get_tdp_cr3
;
1771 vcpu
->arch
.mmu
.inject_page_fault
= nested_svm_inject_npf_exit
;
1772 vcpu
->arch
.mmu
.shadow_root_level
= get_npt_level();
1773 vcpu
->arch
.walk_mmu
= &vcpu
->arch
.nested_mmu
;
1778 static void nested_svm_uninit_mmu_context(struct kvm_vcpu
*vcpu
)
1780 vcpu
->arch
.walk_mmu
= &vcpu
->arch
.mmu
;
1783 static int nested_svm_check_permissions(struct vcpu_svm
*svm
)
1785 if (!(svm
->vcpu
.arch
.efer
& EFER_SVME
)
1786 || !is_paging(&svm
->vcpu
)) {
1787 kvm_queue_exception(&svm
->vcpu
, UD_VECTOR
);
1791 if (svm
->vmcb
->save
.cpl
) {
1792 kvm_inject_gp(&svm
->vcpu
, 0);
1799 static int nested_svm_check_exception(struct vcpu_svm
*svm
, unsigned nr
,
1800 bool has_error_code
, u32 error_code
)
1804 if (!is_guest_mode(&svm
->vcpu
))
1807 svm
->vmcb
->control
.exit_code
= SVM_EXIT_EXCP_BASE
+ nr
;
1808 svm
->vmcb
->control
.exit_code_hi
= 0;
1809 svm
->vmcb
->control
.exit_info_1
= error_code
;
1810 svm
->vmcb
->control
.exit_info_2
= svm
->vcpu
.arch
.cr2
;
1812 vmexit
= nested_svm_intercept(svm
);
1813 if (vmexit
== NESTED_EXIT_DONE
)
1814 svm
->nested
.exit_required
= true;
1819 /* This function returns true if it is save to enable the irq window */
1820 static inline bool nested_svm_intr(struct vcpu_svm
*svm
)
1822 if (!is_guest_mode(&svm
->vcpu
))
1825 if (!(svm
->vcpu
.arch
.hflags
& HF_VINTR_MASK
))
1828 if (!(svm
->vcpu
.arch
.hflags
& HF_HIF_MASK
))
1832 * if vmexit was already requested (by intercepted exception
1833 * for instance) do not overwrite it with "external interrupt"
1836 if (svm
->nested
.exit_required
)
1839 svm
->vmcb
->control
.exit_code
= SVM_EXIT_INTR
;
1840 svm
->vmcb
->control
.exit_info_1
= 0;
1841 svm
->vmcb
->control
.exit_info_2
= 0;
1843 if (svm
->nested
.intercept
& 1ULL) {
1845 * The #vmexit can't be emulated here directly because this
1846 * code path runs with irqs and preemtion disabled. A
1847 * #vmexit emulation might sleep. Only signal request for
1850 svm
->nested
.exit_required
= true;
1851 trace_kvm_nested_intr_vmexit(svm
->vmcb
->save
.rip
);
1858 /* This function returns true if it is save to enable the nmi window */
1859 static inline bool nested_svm_nmi(struct vcpu_svm
*svm
)
1861 if (!is_guest_mode(&svm
->vcpu
))
1864 if (!(svm
->nested
.intercept
& (1ULL << INTERCEPT_NMI
)))
1867 svm
->vmcb
->control
.exit_code
= SVM_EXIT_NMI
;
1868 svm
->nested
.exit_required
= true;
1873 static void *nested_svm_map(struct vcpu_svm
*svm
, u64 gpa
, struct page
**_page
)
1879 page
= gfn_to_page(svm
->vcpu
.kvm
, gpa
>> PAGE_SHIFT
);
1880 if (is_error_page(page
))
1888 kvm_release_page_clean(page
);
1889 kvm_inject_gp(&svm
->vcpu
, 0);
1894 static void nested_svm_unmap(struct page
*page
)
1897 kvm_release_page_dirty(page
);
1900 static int nested_svm_intercept_ioio(struct vcpu_svm
*svm
)
1906 if (!(svm
->nested
.intercept
& (1ULL << INTERCEPT_IOIO_PROT
)))
1907 return NESTED_EXIT_HOST
;
1909 port
= svm
->vmcb
->control
.exit_info_1
>> 16;
1910 gpa
= svm
->nested
.vmcb_iopm
+ (port
/ 8);
1914 if (kvm_read_guest(svm
->vcpu
.kvm
, gpa
, &val
, 1))
1917 return val
? NESTED_EXIT_DONE
: NESTED_EXIT_HOST
;
1920 static int nested_svm_exit_handled_msr(struct vcpu_svm
*svm
)
1922 u32 offset
, msr
, value
;
1925 if (!(svm
->nested
.intercept
& (1ULL << INTERCEPT_MSR_PROT
)))
1926 return NESTED_EXIT_HOST
;
1928 msr
= svm
->vcpu
.arch
.regs
[VCPU_REGS_RCX
];
1929 offset
= svm_msrpm_offset(msr
);
1930 write
= svm
->vmcb
->control
.exit_info_1
& 1;
1931 mask
= 1 << ((2 * (msr
& 0xf)) + write
);
1933 if (offset
== MSR_INVALID
)
1934 return NESTED_EXIT_DONE
;
1936 /* Offset is in 32 bit units but need in 8 bit units */
1939 if (kvm_read_guest(svm
->vcpu
.kvm
, svm
->nested
.vmcb_msrpm
+ offset
, &value
, 4))
1940 return NESTED_EXIT_DONE
;
1942 return (value
& mask
) ? NESTED_EXIT_DONE
: NESTED_EXIT_HOST
;
1945 static int nested_svm_exit_special(struct vcpu_svm
*svm
)
1947 u32 exit_code
= svm
->vmcb
->control
.exit_code
;
1949 switch (exit_code
) {
1952 case SVM_EXIT_EXCP_BASE
+ MC_VECTOR
:
1953 return NESTED_EXIT_HOST
;
1955 /* For now we are always handling NPFs when using them */
1957 return NESTED_EXIT_HOST
;
1959 case SVM_EXIT_EXCP_BASE
+ PF_VECTOR
:
1960 /* When we're shadowing, trap PFs, but not async PF */
1961 if (!npt_enabled
&& svm
->apf_reason
== 0)
1962 return NESTED_EXIT_HOST
;
1964 case SVM_EXIT_EXCP_BASE
+ NM_VECTOR
:
1965 nm_interception(svm
);
1971 return NESTED_EXIT_CONTINUE
;
1975 * If this function returns true, this #vmexit was already handled
1977 static int nested_svm_intercept(struct vcpu_svm
*svm
)
1979 u32 exit_code
= svm
->vmcb
->control
.exit_code
;
1980 int vmexit
= NESTED_EXIT_HOST
;
1982 switch (exit_code
) {
1984 vmexit
= nested_svm_exit_handled_msr(svm
);
1987 vmexit
= nested_svm_intercept_ioio(svm
);
1989 case SVM_EXIT_READ_CR0
... SVM_EXIT_WRITE_CR8
: {
1990 u32 bit
= 1U << (exit_code
- SVM_EXIT_READ_CR0
);
1991 if (svm
->nested
.intercept_cr
& bit
)
1992 vmexit
= NESTED_EXIT_DONE
;
1995 case SVM_EXIT_READ_DR0
... SVM_EXIT_WRITE_DR7
: {
1996 u32 bit
= 1U << (exit_code
- SVM_EXIT_READ_DR0
);
1997 if (svm
->nested
.intercept_dr
& bit
)
1998 vmexit
= NESTED_EXIT_DONE
;
2001 case SVM_EXIT_EXCP_BASE
... SVM_EXIT_EXCP_BASE
+ 0x1f: {
2002 u32 excp_bits
= 1 << (exit_code
- SVM_EXIT_EXCP_BASE
);
2003 if (svm
->nested
.intercept_exceptions
& excp_bits
)
2004 vmexit
= NESTED_EXIT_DONE
;
2005 /* async page fault always cause vmexit */
2006 else if ((exit_code
== SVM_EXIT_EXCP_BASE
+ PF_VECTOR
) &&
2007 svm
->apf_reason
!= 0)
2008 vmexit
= NESTED_EXIT_DONE
;
2011 case SVM_EXIT_ERR
: {
2012 vmexit
= NESTED_EXIT_DONE
;
2016 u64 exit_bits
= 1ULL << (exit_code
- SVM_EXIT_INTR
);
2017 if (svm
->nested
.intercept
& exit_bits
)
2018 vmexit
= NESTED_EXIT_DONE
;
2025 static int nested_svm_exit_handled(struct vcpu_svm
*svm
)
2029 vmexit
= nested_svm_intercept(svm
);
2031 if (vmexit
== NESTED_EXIT_DONE
)
2032 nested_svm_vmexit(svm
);
2037 static inline void copy_vmcb_control_area(struct vmcb
*dst_vmcb
, struct vmcb
*from_vmcb
)
2039 struct vmcb_control_area
*dst
= &dst_vmcb
->control
;
2040 struct vmcb_control_area
*from
= &from_vmcb
->control
;
2042 dst
->intercept_cr
= from
->intercept_cr
;
2043 dst
->intercept_dr
= from
->intercept_dr
;
2044 dst
->intercept_exceptions
= from
->intercept_exceptions
;
2045 dst
->intercept
= from
->intercept
;
2046 dst
->iopm_base_pa
= from
->iopm_base_pa
;
2047 dst
->msrpm_base_pa
= from
->msrpm_base_pa
;
2048 dst
->tsc_offset
= from
->tsc_offset
;
2049 dst
->asid
= from
->asid
;
2050 dst
->tlb_ctl
= from
->tlb_ctl
;
2051 dst
->int_ctl
= from
->int_ctl
;
2052 dst
->int_vector
= from
->int_vector
;
2053 dst
->int_state
= from
->int_state
;
2054 dst
->exit_code
= from
->exit_code
;
2055 dst
->exit_code_hi
= from
->exit_code_hi
;
2056 dst
->exit_info_1
= from
->exit_info_1
;
2057 dst
->exit_info_2
= from
->exit_info_2
;
2058 dst
->exit_int_info
= from
->exit_int_info
;
2059 dst
->exit_int_info_err
= from
->exit_int_info_err
;
2060 dst
->nested_ctl
= from
->nested_ctl
;
2061 dst
->event_inj
= from
->event_inj
;
2062 dst
->event_inj_err
= from
->event_inj_err
;
2063 dst
->nested_cr3
= from
->nested_cr3
;
2064 dst
->lbr_ctl
= from
->lbr_ctl
;
2067 static int nested_svm_vmexit(struct vcpu_svm
*svm
)
2069 struct vmcb
*nested_vmcb
;
2070 struct vmcb
*hsave
= svm
->nested
.hsave
;
2071 struct vmcb
*vmcb
= svm
->vmcb
;
2074 trace_kvm_nested_vmexit_inject(vmcb
->control
.exit_code
,
2075 vmcb
->control
.exit_info_1
,
2076 vmcb
->control
.exit_info_2
,
2077 vmcb
->control
.exit_int_info
,
2078 vmcb
->control
.exit_int_info_err
);
2080 nested_vmcb
= nested_svm_map(svm
, svm
->nested
.vmcb
, &page
);
2084 /* Exit Guest-Mode */
2085 leave_guest_mode(&svm
->vcpu
);
2086 svm
->nested
.vmcb
= 0;
2088 /* Give the current vmcb to the guest */
2091 nested_vmcb
->save
.es
= vmcb
->save
.es
;
2092 nested_vmcb
->save
.cs
= vmcb
->save
.cs
;
2093 nested_vmcb
->save
.ss
= vmcb
->save
.ss
;
2094 nested_vmcb
->save
.ds
= vmcb
->save
.ds
;
2095 nested_vmcb
->save
.gdtr
= vmcb
->save
.gdtr
;
2096 nested_vmcb
->save
.idtr
= vmcb
->save
.idtr
;
2097 nested_vmcb
->save
.efer
= svm
->vcpu
.arch
.efer
;
2098 nested_vmcb
->save
.cr0
= kvm_read_cr0(&svm
->vcpu
);
2099 nested_vmcb
->save
.cr3
= svm
->vcpu
.arch
.cr3
;
2100 nested_vmcb
->save
.cr2
= vmcb
->save
.cr2
;
2101 nested_vmcb
->save
.cr4
= svm
->vcpu
.arch
.cr4
;
2102 nested_vmcb
->save
.rflags
= vmcb
->save
.rflags
;
2103 nested_vmcb
->save
.rip
= vmcb
->save
.rip
;
2104 nested_vmcb
->save
.rsp
= vmcb
->save
.rsp
;
2105 nested_vmcb
->save
.rax
= vmcb
->save
.rax
;
2106 nested_vmcb
->save
.dr7
= vmcb
->save
.dr7
;
2107 nested_vmcb
->save
.dr6
= vmcb
->save
.dr6
;
2108 nested_vmcb
->save
.cpl
= vmcb
->save
.cpl
;
2110 nested_vmcb
->control
.int_ctl
= vmcb
->control
.int_ctl
;
2111 nested_vmcb
->control
.int_vector
= vmcb
->control
.int_vector
;
2112 nested_vmcb
->control
.int_state
= vmcb
->control
.int_state
;
2113 nested_vmcb
->control
.exit_code
= vmcb
->control
.exit_code
;
2114 nested_vmcb
->control
.exit_code_hi
= vmcb
->control
.exit_code_hi
;
2115 nested_vmcb
->control
.exit_info_1
= vmcb
->control
.exit_info_1
;
2116 nested_vmcb
->control
.exit_info_2
= vmcb
->control
.exit_info_2
;
2117 nested_vmcb
->control
.exit_int_info
= vmcb
->control
.exit_int_info
;
2118 nested_vmcb
->control
.exit_int_info_err
= vmcb
->control
.exit_int_info_err
;
2119 nested_vmcb
->control
.next_rip
= vmcb
->control
.next_rip
;
2122 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2123 * to make sure that we do not lose injected events. So check event_inj
2124 * here and copy it to exit_int_info if it is valid.
2125 * Exit_int_info and event_inj can't be both valid because the case
2126 * below only happens on a VMRUN instruction intercept which has
2127 * no valid exit_int_info set.
2129 if (vmcb
->control
.event_inj
& SVM_EVTINJ_VALID
) {
2130 struct vmcb_control_area
*nc
= &nested_vmcb
->control
;
2132 nc
->exit_int_info
= vmcb
->control
.event_inj
;
2133 nc
->exit_int_info_err
= vmcb
->control
.event_inj_err
;
2136 nested_vmcb
->control
.tlb_ctl
= 0;
2137 nested_vmcb
->control
.event_inj
= 0;
2138 nested_vmcb
->control
.event_inj_err
= 0;
2140 /* We always set V_INTR_MASKING and remember the old value in hflags */
2141 if (!(svm
->vcpu
.arch
.hflags
& HF_VINTR_MASK
))
2142 nested_vmcb
->control
.int_ctl
&= ~V_INTR_MASKING_MASK
;
2144 /* Restore the original control entries */
2145 copy_vmcb_control_area(vmcb
, hsave
);
2147 kvm_clear_exception_queue(&svm
->vcpu
);
2148 kvm_clear_interrupt_queue(&svm
->vcpu
);
2150 svm
->nested
.nested_cr3
= 0;
2152 /* Restore selected save entries */
2153 svm
->vmcb
->save
.es
= hsave
->save
.es
;
2154 svm
->vmcb
->save
.cs
= hsave
->save
.cs
;
2155 svm
->vmcb
->save
.ss
= hsave
->save
.ss
;
2156 svm
->vmcb
->save
.ds
= hsave
->save
.ds
;
2157 svm
->vmcb
->save
.gdtr
= hsave
->save
.gdtr
;
2158 svm
->vmcb
->save
.idtr
= hsave
->save
.idtr
;
2159 svm
->vmcb
->save
.rflags
= hsave
->save
.rflags
;
2160 svm_set_efer(&svm
->vcpu
, hsave
->save
.efer
);
2161 svm_set_cr0(&svm
->vcpu
, hsave
->save
.cr0
| X86_CR0_PE
);
2162 svm_set_cr4(&svm
->vcpu
, hsave
->save
.cr4
);
2164 svm
->vmcb
->save
.cr3
= hsave
->save
.cr3
;
2165 svm
->vcpu
.arch
.cr3
= hsave
->save
.cr3
;
2167 (void)kvm_set_cr3(&svm
->vcpu
, hsave
->save
.cr3
);
2169 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RAX
, hsave
->save
.rax
);
2170 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RSP
, hsave
->save
.rsp
);
2171 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RIP
, hsave
->save
.rip
);
2172 svm
->vmcb
->save
.dr7
= 0;
2173 svm
->vmcb
->save
.cpl
= 0;
2174 svm
->vmcb
->control
.exit_int_info
= 0;
2176 mark_all_dirty(svm
->vmcb
);
2178 nested_svm_unmap(page
);
2180 nested_svm_uninit_mmu_context(&svm
->vcpu
);
2181 kvm_mmu_reset_context(&svm
->vcpu
);
2182 kvm_mmu_load(&svm
->vcpu
);
2187 static bool nested_svm_vmrun_msrpm(struct vcpu_svm
*svm
)
2190 * This function merges the msr permission bitmaps of kvm and the
2191 * nested vmcb. It is omptimized in that it only merges the parts where
2192 * the kvm msr permission bitmap may contain zero bits
2196 if (!(svm
->nested
.intercept
& (1ULL << INTERCEPT_MSR_PROT
)))
2199 for (i
= 0; i
< MSRPM_OFFSETS
; i
++) {
2203 if (msrpm_offsets
[i
] == 0xffffffff)
2206 p
= msrpm_offsets
[i
];
2207 offset
= svm
->nested
.vmcb_msrpm
+ (p
* 4);
2209 if (kvm_read_guest(svm
->vcpu
.kvm
, offset
, &value
, 4))
2212 svm
->nested
.msrpm
[p
] = svm
->msrpm
[p
] | value
;
2215 svm
->vmcb
->control
.msrpm_base_pa
= __pa(svm
->nested
.msrpm
);
2220 static bool nested_vmcb_checks(struct vmcb
*vmcb
)
2222 if ((vmcb
->control
.intercept
& (1ULL << INTERCEPT_VMRUN
)) == 0)
2225 if (vmcb
->control
.asid
== 0)
2228 if (vmcb
->control
.nested_ctl
&& !npt_enabled
)
2234 static bool nested_svm_vmrun(struct vcpu_svm
*svm
)
2236 struct vmcb
*nested_vmcb
;
2237 struct vmcb
*hsave
= svm
->nested
.hsave
;
2238 struct vmcb
*vmcb
= svm
->vmcb
;
2242 vmcb_gpa
= svm
->vmcb
->save
.rax
;
2244 nested_vmcb
= nested_svm_map(svm
, svm
->vmcb
->save
.rax
, &page
);
2248 if (!nested_vmcb_checks(nested_vmcb
)) {
2249 nested_vmcb
->control
.exit_code
= SVM_EXIT_ERR
;
2250 nested_vmcb
->control
.exit_code_hi
= 0;
2251 nested_vmcb
->control
.exit_info_1
= 0;
2252 nested_vmcb
->control
.exit_info_2
= 0;
2254 nested_svm_unmap(page
);
2259 trace_kvm_nested_vmrun(svm
->vmcb
->save
.rip
, vmcb_gpa
,
2260 nested_vmcb
->save
.rip
,
2261 nested_vmcb
->control
.int_ctl
,
2262 nested_vmcb
->control
.event_inj
,
2263 nested_vmcb
->control
.nested_ctl
);
2265 trace_kvm_nested_intercepts(nested_vmcb
->control
.intercept_cr
& 0xffff,
2266 nested_vmcb
->control
.intercept_cr
>> 16,
2267 nested_vmcb
->control
.intercept_exceptions
,
2268 nested_vmcb
->control
.intercept
);
2270 /* Clear internal status */
2271 kvm_clear_exception_queue(&svm
->vcpu
);
2272 kvm_clear_interrupt_queue(&svm
->vcpu
);
2275 * Save the old vmcb, so we don't need to pick what we save, but can
2276 * restore everything when a VMEXIT occurs
2278 hsave
->save
.es
= vmcb
->save
.es
;
2279 hsave
->save
.cs
= vmcb
->save
.cs
;
2280 hsave
->save
.ss
= vmcb
->save
.ss
;
2281 hsave
->save
.ds
= vmcb
->save
.ds
;
2282 hsave
->save
.gdtr
= vmcb
->save
.gdtr
;
2283 hsave
->save
.idtr
= vmcb
->save
.idtr
;
2284 hsave
->save
.efer
= svm
->vcpu
.arch
.efer
;
2285 hsave
->save
.cr0
= kvm_read_cr0(&svm
->vcpu
);
2286 hsave
->save
.cr4
= svm
->vcpu
.arch
.cr4
;
2287 hsave
->save
.rflags
= vmcb
->save
.rflags
;
2288 hsave
->save
.rip
= kvm_rip_read(&svm
->vcpu
);
2289 hsave
->save
.rsp
= vmcb
->save
.rsp
;
2290 hsave
->save
.rax
= vmcb
->save
.rax
;
2292 hsave
->save
.cr3
= vmcb
->save
.cr3
;
2294 hsave
->save
.cr3
= svm
->vcpu
.arch
.cr3
;
2296 copy_vmcb_control_area(hsave
, vmcb
);
2298 if (svm
->vmcb
->save
.rflags
& X86_EFLAGS_IF
)
2299 svm
->vcpu
.arch
.hflags
|= HF_HIF_MASK
;
2301 svm
->vcpu
.arch
.hflags
&= ~HF_HIF_MASK
;
2303 if (nested_vmcb
->control
.nested_ctl
) {
2304 kvm_mmu_unload(&svm
->vcpu
);
2305 svm
->nested
.nested_cr3
= nested_vmcb
->control
.nested_cr3
;
2306 nested_svm_init_mmu_context(&svm
->vcpu
);
2309 /* Load the nested guest state */
2310 svm
->vmcb
->save
.es
= nested_vmcb
->save
.es
;
2311 svm
->vmcb
->save
.cs
= nested_vmcb
->save
.cs
;
2312 svm
->vmcb
->save
.ss
= nested_vmcb
->save
.ss
;
2313 svm
->vmcb
->save
.ds
= nested_vmcb
->save
.ds
;
2314 svm
->vmcb
->save
.gdtr
= nested_vmcb
->save
.gdtr
;
2315 svm
->vmcb
->save
.idtr
= nested_vmcb
->save
.idtr
;
2316 svm
->vmcb
->save
.rflags
= nested_vmcb
->save
.rflags
;
2317 svm_set_efer(&svm
->vcpu
, nested_vmcb
->save
.efer
);
2318 svm_set_cr0(&svm
->vcpu
, nested_vmcb
->save
.cr0
);
2319 svm_set_cr4(&svm
->vcpu
, nested_vmcb
->save
.cr4
);
2321 svm
->vmcb
->save
.cr3
= nested_vmcb
->save
.cr3
;
2322 svm
->vcpu
.arch
.cr3
= nested_vmcb
->save
.cr3
;
2324 (void)kvm_set_cr3(&svm
->vcpu
, nested_vmcb
->save
.cr3
);
2326 /* Guest paging mode is active - reset mmu */
2327 kvm_mmu_reset_context(&svm
->vcpu
);
2329 svm
->vmcb
->save
.cr2
= svm
->vcpu
.arch
.cr2
= nested_vmcb
->save
.cr2
;
2330 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RAX
, nested_vmcb
->save
.rax
);
2331 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RSP
, nested_vmcb
->save
.rsp
);
2332 kvm_register_write(&svm
->vcpu
, VCPU_REGS_RIP
, nested_vmcb
->save
.rip
);
2334 /* In case we don't even reach vcpu_run, the fields are not updated */
2335 svm
->vmcb
->save
.rax
= nested_vmcb
->save
.rax
;
2336 svm
->vmcb
->save
.rsp
= nested_vmcb
->save
.rsp
;
2337 svm
->vmcb
->save
.rip
= nested_vmcb
->save
.rip
;
2338 svm
->vmcb
->save
.dr7
= nested_vmcb
->save
.dr7
;
2339 svm
->vmcb
->save
.dr6
= nested_vmcb
->save
.dr6
;
2340 svm
->vmcb
->save
.cpl
= nested_vmcb
->save
.cpl
;
2342 svm
->nested
.vmcb_msrpm
= nested_vmcb
->control
.msrpm_base_pa
& ~0x0fffULL
;
2343 svm
->nested
.vmcb_iopm
= nested_vmcb
->control
.iopm_base_pa
& ~0x0fffULL
;
2345 /* cache intercepts */
2346 svm
->nested
.intercept_cr
= nested_vmcb
->control
.intercept_cr
;
2347 svm
->nested
.intercept_dr
= nested_vmcb
->control
.intercept_dr
;
2348 svm
->nested
.intercept_exceptions
= nested_vmcb
->control
.intercept_exceptions
;
2349 svm
->nested
.intercept
= nested_vmcb
->control
.intercept
;
2351 force_new_asid(&svm
->vcpu
);
2352 svm
->vmcb
->control
.int_ctl
= nested_vmcb
->control
.int_ctl
| V_INTR_MASKING_MASK
;
2353 if (nested_vmcb
->control
.int_ctl
& V_INTR_MASKING_MASK
)
2354 svm
->vcpu
.arch
.hflags
|= HF_VINTR_MASK
;
2356 svm
->vcpu
.arch
.hflags
&= ~HF_VINTR_MASK
;
2358 if (svm
->vcpu
.arch
.hflags
& HF_VINTR_MASK
) {
2359 /* We only want the cr8 intercept bits of the guest */
2360 clr_cr_intercept(svm
, INTERCEPT_CR8_READ
);
2361 clr_cr_intercept(svm
, INTERCEPT_CR8_WRITE
);
2364 /* We don't want to see VMMCALLs from a nested guest */
2365 clr_intercept(svm
, INTERCEPT_VMMCALL
);
2367 svm
->vmcb
->control
.lbr_ctl
= nested_vmcb
->control
.lbr_ctl
;
2368 svm
->vmcb
->control
.int_vector
= nested_vmcb
->control
.int_vector
;
2369 svm
->vmcb
->control
.int_state
= nested_vmcb
->control
.int_state
;
2370 svm
->vmcb
->control
.tsc_offset
+= nested_vmcb
->control
.tsc_offset
;
2371 svm
->vmcb
->control
.event_inj
= nested_vmcb
->control
.event_inj
;
2372 svm
->vmcb
->control
.event_inj_err
= nested_vmcb
->control
.event_inj_err
;
2374 nested_svm_unmap(page
);
2376 /* Enter Guest-Mode */
2377 enter_guest_mode(&svm
->vcpu
);
2380 * Merge guest and host intercepts - must be called with vcpu in
2381 * guest-mode to take affect here
2383 recalc_intercepts(svm
);
2385 svm
->nested
.vmcb
= vmcb_gpa
;
2389 mark_all_dirty(svm
->vmcb
);
2394 static void nested_svm_vmloadsave(struct vmcb
*from_vmcb
, struct vmcb
*to_vmcb
)
2396 to_vmcb
->save
.fs
= from_vmcb
->save
.fs
;
2397 to_vmcb
->save
.gs
= from_vmcb
->save
.gs
;
2398 to_vmcb
->save
.tr
= from_vmcb
->save
.tr
;
2399 to_vmcb
->save
.ldtr
= from_vmcb
->save
.ldtr
;
2400 to_vmcb
->save
.kernel_gs_base
= from_vmcb
->save
.kernel_gs_base
;
2401 to_vmcb
->save
.star
= from_vmcb
->save
.star
;
2402 to_vmcb
->save
.lstar
= from_vmcb
->save
.lstar
;
2403 to_vmcb
->save
.cstar
= from_vmcb
->save
.cstar
;
2404 to_vmcb
->save
.sfmask
= from_vmcb
->save
.sfmask
;
2405 to_vmcb
->save
.sysenter_cs
= from_vmcb
->save
.sysenter_cs
;
2406 to_vmcb
->save
.sysenter_esp
= from_vmcb
->save
.sysenter_esp
;
2407 to_vmcb
->save
.sysenter_eip
= from_vmcb
->save
.sysenter_eip
;
2410 static int vmload_interception(struct vcpu_svm
*svm
)
2412 struct vmcb
*nested_vmcb
;
2415 if (nested_svm_check_permissions(svm
))
2418 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
2419 skip_emulated_instruction(&svm
->vcpu
);
2421 nested_vmcb
= nested_svm_map(svm
, svm
->vmcb
->save
.rax
, &page
);
2425 nested_svm_vmloadsave(nested_vmcb
, svm
->vmcb
);
2426 nested_svm_unmap(page
);
2431 static int vmsave_interception(struct vcpu_svm
*svm
)
2433 struct vmcb
*nested_vmcb
;
2436 if (nested_svm_check_permissions(svm
))
2439 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
2440 skip_emulated_instruction(&svm
->vcpu
);
2442 nested_vmcb
= nested_svm_map(svm
, svm
->vmcb
->save
.rax
, &page
);
2446 nested_svm_vmloadsave(svm
->vmcb
, nested_vmcb
);
2447 nested_svm_unmap(page
);
2452 static int vmrun_interception(struct vcpu_svm
*svm
)
2454 if (nested_svm_check_permissions(svm
))
2457 /* Save rip after vmrun instruction */
2458 kvm_rip_write(&svm
->vcpu
, kvm_rip_read(&svm
->vcpu
) + 3);
2460 if (!nested_svm_vmrun(svm
))
2463 if (!nested_svm_vmrun_msrpm(svm
))
2470 svm
->vmcb
->control
.exit_code
= SVM_EXIT_ERR
;
2471 svm
->vmcb
->control
.exit_code_hi
= 0;
2472 svm
->vmcb
->control
.exit_info_1
= 0;
2473 svm
->vmcb
->control
.exit_info_2
= 0;
2475 nested_svm_vmexit(svm
);
2480 static int stgi_interception(struct vcpu_svm
*svm
)
2482 if (nested_svm_check_permissions(svm
))
2485 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
2486 skip_emulated_instruction(&svm
->vcpu
);
2487 kvm_make_request(KVM_REQ_EVENT
, &svm
->vcpu
);
2494 static int clgi_interception(struct vcpu_svm
*svm
)
2496 if (nested_svm_check_permissions(svm
))
2499 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
2500 skip_emulated_instruction(&svm
->vcpu
);
2504 /* After a CLGI no interrupts should come */
2505 svm_clear_vintr(svm
);
2506 svm
->vmcb
->control
.int_ctl
&= ~V_IRQ_MASK
;
2511 static int invlpga_interception(struct vcpu_svm
*svm
)
2513 struct kvm_vcpu
*vcpu
= &svm
->vcpu
;
2515 trace_kvm_invlpga(svm
->vmcb
->save
.rip
, vcpu
->arch
.regs
[VCPU_REGS_RCX
],
2516 vcpu
->arch
.regs
[VCPU_REGS_RAX
]);
2518 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2519 kvm_mmu_invlpg(vcpu
, vcpu
->arch
.regs
[VCPU_REGS_RAX
]);
2521 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 3;
2522 skip_emulated_instruction(&svm
->vcpu
);
2526 static int skinit_interception(struct vcpu_svm
*svm
)
2528 trace_kvm_skinit(svm
->vmcb
->save
.rip
, svm
->vcpu
.arch
.regs
[VCPU_REGS_RAX
]);
2530 kvm_queue_exception(&svm
->vcpu
, UD_VECTOR
);
2534 static int invalid_op_interception(struct vcpu_svm
*svm
)
2536 kvm_queue_exception(&svm
->vcpu
, UD_VECTOR
);
2540 static int task_switch_interception(struct vcpu_svm
*svm
)
2544 int int_type
= svm
->vmcb
->control
.exit_int_info
&
2545 SVM_EXITINTINFO_TYPE_MASK
;
2546 int int_vec
= svm
->vmcb
->control
.exit_int_info
& SVM_EVTINJ_VEC_MASK
;
2548 svm
->vmcb
->control
.exit_int_info
& SVM_EXITINTINFO_TYPE_MASK
;
2550 svm
->vmcb
->control
.exit_int_info
& SVM_EXITINTINFO_VALID
;
2551 bool has_error_code
= false;
2554 tss_selector
= (u16
)svm
->vmcb
->control
.exit_info_1
;
2556 if (svm
->vmcb
->control
.exit_info_2
&
2557 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET
))
2558 reason
= TASK_SWITCH_IRET
;
2559 else if (svm
->vmcb
->control
.exit_info_2
&
2560 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP
))
2561 reason
= TASK_SWITCH_JMP
;
2563 reason
= TASK_SWITCH_GATE
;
2565 reason
= TASK_SWITCH_CALL
;
2567 if (reason
== TASK_SWITCH_GATE
) {
2569 case SVM_EXITINTINFO_TYPE_NMI
:
2570 svm
->vcpu
.arch
.nmi_injected
= false;
2572 case SVM_EXITINTINFO_TYPE_EXEPT
:
2573 if (svm
->vmcb
->control
.exit_info_2
&
2574 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE
)) {
2575 has_error_code
= true;
2577 (u32
)svm
->vmcb
->control
.exit_info_2
;
2579 kvm_clear_exception_queue(&svm
->vcpu
);
2581 case SVM_EXITINTINFO_TYPE_INTR
:
2582 kvm_clear_interrupt_queue(&svm
->vcpu
);
2589 if (reason
!= TASK_SWITCH_GATE
||
2590 int_type
== SVM_EXITINTINFO_TYPE_SOFT
||
2591 (int_type
== SVM_EXITINTINFO_TYPE_EXEPT
&&
2592 (int_vec
== OF_VECTOR
|| int_vec
== BP_VECTOR
)))
2593 skip_emulated_instruction(&svm
->vcpu
);
2595 if (kvm_task_switch(&svm
->vcpu
, tss_selector
, reason
,
2596 has_error_code
, error_code
) == EMULATE_FAIL
) {
2597 svm
->vcpu
.run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
2598 svm
->vcpu
.run
->internal
.suberror
= KVM_INTERNAL_ERROR_EMULATION
;
2599 svm
->vcpu
.run
->internal
.ndata
= 0;
2605 static int cpuid_interception(struct vcpu_svm
*svm
)
2607 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 2;
2608 kvm_emulate_cpuid(&svm
->vcpu
);
2612 static int iret_interception(struct vcpu_svm
*svm
)
2614 ++svm
->vcpu
.stat
.nmi_window_exits
;
2615 clr_intercept(svm
, INTERCEPT_IRET
);
2616 svm
->vcpu
.arch
.hflags
|= HF_IRET_MASK
;
2620 static int invlpg_interception(struct vcpu_svm
*svm
)
2622 return emulate_instruction(&svm
->vcpu
, 0, 0, 0) == EMULATE_DONE
;
2625 static int emulate_on_interception(struct vcpu_svm
*svm
)
2627 return emulate_instruction(&svm
->vcpu
, 0, 0, 0) == EMULATE_DONE
;
2630 static int cr0_write_interception(struct vcpu_svm
*svm
)
2632 struct kvm_vcpu
*vcpu
= &svm
->vcpu
;
2635 r
= emulate_instruction(&svm
->vcpu
, 0, 0, 0);
2637 if (svm
->nested
.vmexit_rip
) {
2638 kvm_register_write(vcpu
, VCPU_REGS_RIP
, svm
->nested
.vmexit_rip
);
2639 kvm_register_write(vcpu
, VCPU_REGS_RSP
, svm
->nested
.vmexit_rsp
);
2640 kvm_register_write(vcpu
, VCPU_REGS_RAX
, svm
->nested
.vmexit_rax
);
2641 svm
->nested
.vmexit_rip
= 0;
2644 return r
== EMULATE_DONE
;
2647 static int cr8_write_interception(struct vcpu_svm
*svm
)
2649 struct kvm_run
*kvm_run
= svm
->vcpu
.run
;
2651 u8 cr8_prev
= kvm_get_cr8(&svm
->vcpu
);
2652 /* instruction emulation calls kvm_set_cr8() */
2653 emulate_instruction(&svm
->vcpu
, 0, 0, 0);
2654 if (irqchip_in_kernel(svm
->vcpu
.kvm
)) {
2655 clr_cr_intercept(svm
, INTERCEPT_CR8_WRITE
);
2658 if (cr8_prev
<= kvm_get_cr8(&svm
->vcpu
))
2660 kvm_run
->exit_reason
= KVM_EXIT_SET_TPR
;
2664 static int svm_get_msr(struct kvm_vcpu
*vcpu
, unsigned ecx
, u64
*data
)
2666 struct vcpu_svm
*svm
= to_svm(vcpu
);
2669 case MSR_IA32_TSC
: {
2670 struct vmcb
*vmcb
= get_host_vmcb(svm
);
2672 *data
= vmcb
->control
.tsc_offset
+ native_read_tsc();
2676 *data
= svm
->vmcb
->save
.star
;
2678 #ifdef CONFIG_X86_64
2680 *data
= svm
->vmcb
->save
.lstar
;
2683 *data
= svm
->vmcb
->save
.cstar
;
2685 case MSR_KERNEL_GS_BASE
:
2686 *data
= svm
->vmcb
->save
.kernel_gs_base
;
2688 case MSR_SYSCALL_MASK
:
2689 *data
= svm
->vmcb
->save
.sfmask
;
2692 case MSR_IA32_SYSENTER_CS
:
2693 *data
= svm
->vmcb
->save
.sysenter_cs
;
2695 case MSR_IA32_SYSENTER_EIP
:
2696 *data
= svm
->sysenter_eip
;
2698 case MSR_IA32_SYSENTER_ESP
:
2699 *data
= svm
->sysenter_esp
;
2702 * Nobody will change the following 5 values in the VMCB so we can
2703 * safely return them on rdmsr. They will always be 0 until LBRV is
2706 case MSR_IA32_DEBUGCTLMSR
:
2707 *data
= svm
->vmcb
->save
.dbgctl
;
2709 case MSR_IA32_LASTBRANCHFROMIP
:
2710 *data
= svm
->vmcb
->save
.br_from
;
2712 case MSR_IA32_LASTBRANCHTOIP
:
2713 *data
= svm
->vmcb
->save
.br_to
;
2715 case MSR_IA32_LASTINTFROMIP
:
2716 *data
= svm
->vmcb
->save
.last_excp_from
;
2718 case MSR_IA32_LASTINTTOIP
:
2719 *data
= svm
->vmcb
->save
.last_excp_to
;
2721 case MSR_VM_HSAVE_PA
:
2722 *data
= svm
->nested
.hsave_msr
;
2725 *data
= svm
->nested
.vm_cr_msr
;
2727 case MSR_IA32_UCODE_REV
:
2731 return kvm_get_msr_common(vcpu
, ecx
, data
);
2736 static int rdmsr_interception(struct vcpu_svm
*svm
)
2738 u32 ecx
= svm
->vcpu
.arch
.regs
[VCPU_REGS_RCX
];
2741 if (svm_get_msr(&svm
->vcpu
, ecx
, &data
)) {
2742 trace_kvm_msr_read_ex(ecx
);
2743 kvm_inject_gp(&svm
->vcpu
, 0);
2745 trace_kvm_msr_read(ecx
, data
);
2747 svm
->vcpu
.arch
.regs
[VCPU_REGS_RAX
] = data
& 0xffffffff;
2748 svm
->vcpu
.arch
.regs
[VCPU_REGS_RDX
] = data
>> 32;
2749 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 2;
2750 skip_emulated_instruction(&svm
->vcpu
);
2755 static int svm_set_vm_cr(struct kvm_vcpu
*vcpu
, u64 data
)
2757 struct vcpu_svm
*svm
= to_svm(vcpu
);
2758 int svm_dis
, chg_mask
;
2760 if (data
& ~SVM_VM_CR_VALID_MASK
)
2763 chg_mask
= SVM_VM_CR_VALID_MASK
;
2765 if (svm
->nested
.vm_cr_msr
& SVM_VM_CR_SVM_DIS_MASK
)
2766 chg_mask
&= ~(SVM_VM_CR_SVM_LOCK_MASK
| SVM_VM_CR_SVM_DIS_MASK
);
2768 svm
->nested
.vm_cr_msr
&= ~chg_mask
;
2769 svm
->nested
.vm_cr_msr
|= (data
& chg_mask
);
2771 svm_dis
= svm
->nested
.vm_cr_msr
& SVM_VM_CR_SVM_DIS_MASK
;
2773 /* check for svm_disable while efer.svme is set */
2774 if (svm_dis
&& (vcpu
->arch
.efer
& EFER_SVME
))
2780 static int svm_set_msr(struct kvm_vcpu
*vcpu
, unsigned ecx
, u64 data
)
2782 struct vcpu_svm
*svm
= to_svm(vcpu
);
2786 kvm_write_tsc(vcpu
, data
);
2789 svm
->vmcb
->save
.star
= data
;
2791 #ifdef CONFIG_X86_64
2793 svm
->vmcb
->save
.lstar
= data
;
2796 svm
->vmcb
->save
.cstar
= data
;
2798 case MSR_KERNEL_GS_BASE
:
2799 svm
->vmcb
->save
.kernel_gs_base
= data
;
2801 case MSR_SYSCALL_MASK
:
2802 svm
->vmcb
->save
.sfmask
= data
;
2805 case MSR_IA32_SYSENTER_CS
:
2806 svm
->vmcb
->save
.sysenter_cs
= data
;
2808 case MSR_IA32_SYSENTER_EIP
:
2809 svm
->sysenter_eip
= data
;
2810 svm
->vmcb
->save
.sysenter_eip
= data
;
2812 case MSR_IA32_SYSENTER_ESP
:
2813 svm
->sysenter_esp
= data
;
2814 svm
->vmcb
->save
.sysenter_esp
= data
;
2816 case MSR_IA32_DEBUGCTLMSR
:
2817 if (!boot_cpu_has(X86_FEATURE_LBRV
)) {
2818 pr_unimpl(vcpu
, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
2822 if (data
& DEBUGCTL_RESERVED_BITS
)
2825 svm
->vmcb
->save
.dbgctl
= data
;
2826 if (data
& (1ULL<<0))
2827 svm_enable_lbrv(svm
);
2829 svm_disable_lbrv(svm
);
2831 case MSR_VM_HSAVE_PA
:
2832 svm
->nested
.hsave_msr
= data
;
2835 return svm_set_vm_cr(vcpu
, data
);
2837 pr_unimpl(vcpu
, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx
, data
);
2840 return kvm_set_msr_common(vcpu
, ecx
, data
);
2845 static int wrmsr_interception(struct vcpu_svm
*svm
)
2847 u32 ecx
= svm
->vcpu
.arch
.regs
[VCPU_REGS_RCX
];
2848 u64 data
= (svm
->vcpu
.arch
.regs
[VCPU_REGS_RAX
] & -1u)
2849 | ((u64
)(svm
->vcpu
.arch
.regs
[VCPU_REGS_RDX
] & -1u) << 32);
2852 svm
->next_rip
= kvm_rip_read(&svm
->vcpu
) + 2;
2853 if (svm_set_msr(&svm
->vcpu
, ecx
, data
)) {
2854 trace_kvm_msr_write_ex(ecx
, data
);
2855 kvm_inject_gp(&svm
->vcpu
, 0);
2857 trace_kvm_msr_write(ecx
, data
);
2858 skip_emulated_instruction(&svm
->vcpu
);
2863 static int msr_interception(struct vcpu_svm
*svm
)
2865 if (svm
->vmcb
->control
.exit_info_1
)
2866 return wrmsr_interception(svm
);
2868 return rdmsr_interception(svm
);
2871 static int interrupt_window_interception(struct vcpu_svm
*svm
)
2873 struct kvm_run
*kvm_run
= svm
->vcpu
.run
;
2875 kvm_make_request(KVM_REQ_EVENT
, &svm
->vcpu
);
2876 svm_clear_vintr(svm
);
2877 svm
->vmcb
->control
.int_ctl
&= ~V_IRQ_MASK
;
2879 * If the user space waits to inject interrupts, exit as soon as
2882 if (!irqchip_in_kernel(svm
->vcpu
.kvm
) &&
2883 kvm_run
->request_interrupt_window
&&
2884 !kvm_cpu_has_interrupt(&svm
->vcpu
)) {
2885 ++svm
->vcpu
.stat
.irq_window_exits
;
2886 kvm_run
->exit_reason
= KVM_EXIT_IRQ_WINDOW_OPEN
;
2893 static int pause_interception(struct vcpu_svm
*svm
)
2895 kvm_vcpu_on_spin(&(svm
->vcpu
));
2899 static int (*svm_exit_handlers
[])(struct vcpu_svm
*svm
) = {
2900 [SVM_EXIT_READ_CR0
] = emulate_on_interception
,
2901 [SVM_EXIT_READ_CR3
] = emulate_on_interception
,
2902 [SVM_EXIT_READ_CR4
] = emulate_on_interception
,
2903 [SVM_EXIT_READ_CR8
] = emulate_on_interception
,
2904 [SVM_EXIT_CR0_SEL_WRITE
] = emulate_on_interception
,
2905 [SVM_EXIT_WRITE_CR0
] = cr0_write_interception
,
2906 [SVM_EXIT_WRITE_CR3
] = emulate_on_interception
,
2907 [SVM_EXIT_WRITE_CR4
] = emulate_on_interception
,
2908 [SVM_EXIT_WRITE_CR8
] = cr8_write_interception
,
2909 [SVM_EXIT_READ_DR0
] = emulate_on_interception
,
2910 [SVM_EXIT_READ_DR1
] = emulate_on_interception
,
2911 [SVM_EXIT_READ_DR2
] = emulate_on_interception
,
2912 [SVM_EXIT_READ_DR3
] = emulate_on_interception
,
2913 [SVM_EXIT_READ_DR4
] = emulate_on_interception
,
2914 [SVM_EXIT_READ_DR5
] = emulate_on_interception
,
2915 [SVM_EXIT_READ_DR6
] = emulate_on_interception
,
2916 [SVM_EXIT_READ_DR7
] = emulate_on_interception
,
2917 [SVM_EXIT_WRITE_DR0
] = emulate_on_interception
,
2918 [SVM_EXIT_WRITE_DR1
] = emulate_on_interception
,
2919 [SVM_EXIT_WRITE_DR2
] = emulate_on_interception
,
2920 [SVM_EXIT_WRITE_DR3
] = emulate_on_interception
,
2921 [SVM_EXIT_WRITE_DR4
] = emulate_on_interception
,
2922 [SVM_EXIT_WRITE_DR5
] = emulate_on_interception
,
2923 [SVM_EXIT_WRITE_DR6
] = emulate_on_interception
,
2924 [SVM_EXIT_WRITE_DR7
] = emulate_on_interception
,
2925 [SVM_EXIT_EXCP_BASE
+ DB_VECTOR
] = db_interception
,
2926 [SVM_EXIT_EXCP_BASE
+ BP_VECTOR
] = bp_interception
,
2927 [SVM_EXIT_EXCP_BASE
+ UD_VECTOR
] = ud_interception
,
2928 [SVM_EXIT_EXCP_BASE
+ PF_VECTOR
] = pf_interception
,
2929 [SVM_EXIT_EXCP_BASE
+ NM_VECTOR
] = nm_interception
,
2930 [SVM_EXIT_EXCP_BASE
+ MC_VECTOR
] = mc_interception
,
2931 [SVM_EXIT_INTR
] = intr_interception
,
2932 [SVM_EXIT_NMI
] = nmi_interception
,
2933 [SVM_EXIT_SMI
] = nop_on_interception
,
2934 [SVM_EXIT_INIT
] = nop_on_interception
,
2935 [SVM_EXIT_VINTR
] = interrupt_window_interception
,
2936 [SVM_EXIT_CPUID
] = cpuid_interception
,
2937 [SVM_EXIT_IRET
] = iret_interception
,
2938 [SVM_EXIT_INVD
] = emulate_on_interception
,
2939 [SVM_EXIT_PAUSE
] = pause_interception
,
2940 [SVM_EXIT_HLT
] = halt_interception
,
2941 [SVM_EXIT_INVLPG
] = invlpg_interception
,
2942 [SVM_EXIT_INVLPGA
] = invlpga_interception
,
2943 [SVM_EXIT_IOIO
] = io_interception
,
2944 [SVM_EXIT_MSR
] = msr_interception
,
2945 [SVM_EXIT_TASK_SWITCH
] = task_switch_interception
,
2946 [SVM_EXIT_SHUTDOWN
] = shutdown_interception
,
2947 [SVM_EXIT_VMRUN
] = vmrun_interception
,
2948 [SVM_EXIT_VMMCALL
] = vmmcall_interception
,
2949 [SVM_EXIT_VMLOAD
] = vmload_interception
,
2950 [SVM_EXIT_VMSAVE
] = vmsave_interception
,
2951 [SVM_EXIT_STGI
] = stgi_interception
,
2952 [SVM_EXIT_CLGI
] = clgi_interception
,
2953 [SVM_EXIT_SKINIT
] = skinit_interception
,
2954 [SVM_EXIT_WBINVD
] = emulate_on_interception
,
2955 [SVM_EXIT_MONITOR
] = invalid_op_interception
,
2956 [SVM_EXIT_MWAIT
] = invalid_op_interception
,
2957 [SVM_EXIT_NPF
] = pf_interception
,
2960 void dump_vmcb(struct kvm_vcpu
*vcpu
)
2962 struct vcpu_svm
*svm
= to_svm(vcpu
);
2963 struct vmcb_control_area
*control
= &svm
->vmcb
->control
;
2964 struct vmcb_save_area
*save
= &svm
->vmcb
->save
;
2966 pr_err("VMCB Control Area:\n");
2967 pr_err("cr_read: %04x\n", control
->intercept_cr
& 0xffff);
2968 pr_err("cr_write: %04x\n", control
->intercept_cr
>> 16);
2969 pr_err("dr_read: %04x\n", control
->intercept_dr
& 0xffff);
2970 pr_err("dr_write: %04x\n", control
->intercept_dr
>> 16);
2971 pr_err("exceptions: %08x\n", control
->intercept_exceptions
);
2972 pr_err("intercepts: %016llx\n", control
->intercept
);
2973 pr_err("pause filter count: %d\n", control
->pause_filter_count
);
2974 pr_err("iopm_base_pa: %016llx\n", control
->iopm_base_pa
);
2975 pr_err("msrpm_base_pa: %016llx\n", control
->msrpm_base_pa
);
2976 pr_err("tsc_offset: %016llx\n", control
->tsc_offset
);
2977 pr_err("asid: %d\n", control
->asid
);
2978 pr_err("tlb_ctl: %d\n", control
->tlb_ctl
);
2979 pr_err("int_ctl: %08x\n", control
->int_ctl
);
2980 pr_err("int_vector: %08x\n", control
->int_vector
);
2981 pr_err("int_state: %08x\n", control
->int_state
);
2982 pr_err("exit_code: %08x\n", control
->exit_code
);
2983 pr_err("exit_info1: %016llx\n", control
->exit_info_1
);
2984 pr_err("exit_info2: %016llx\n", control
->exit_info_2
);
2985 pr_err("exit_int_info: %08x\n", control
->exit_int_info
);
2986 pr_err("exit_int_info_err: %08x\n", control
->exit_int_info_err
);
2987 pr_err("nested_ctl: %lld\n", control
->nested_ctl
);
2988 pr_err("nested_cr3: %016llx\n", control
->nested_cr3
);
2989 pr_err("event_inj: %08x\n", control
->event_inj
);
2990 pr_err("event_inj_err: %08x\n", control
->event_inj_err
);
2991 pr_err("lbr_ctl: %lld\n", control
->lbr_ctl
);
2992 pr_err("next_rip: %016llx\n", control
->next_rip
);
2993 pr_err("VMCB State Save Area:\n");
2994 pr_err("es: s: %04x a: %04x l: %08x b: %016llx\n",
2995 save
->es
.selector
, save
->es
.attrib
,
2996 save
->es
.limit
, save
->es
.base
);
2997 pr_err("cs: s: %04x a: %04x l: %08x b: %016llx\n",
2998 save
->cs
.selector
, save
->cs
.attrib
,
2999 save
->cs
.limit
, save
->cs
.base
);
3000 pr_err("ss: s: %04x a: %04x l: %08x b: %016llx\n",
3001 save
->ss
.selector
, save
->ss
.attrib
,
3002 save
->ss
.limit
, save
->ss
.base
);
3003 pr_err("ds: s: %04x a: %04x l: %08x b: %016llx\n",
3004 save
->ds
.selector
, save
->ds
.attrib
,
3005 save
->ds
.limit
, save
->ds
.base
);
3006 pr_err("fs: s: %04x a: %04x l: %08x b: %016llx\n",
3007 save
->fs
.selector
, save
->fs
.attrib
,
3008 save
->fs
.limit
, save
->fs
.base
);
3009 pr_err("gs: s: %04x a: %04x l: %08x b: %016llx\n",
3010 save
->gs
.selector
, save
->gs
.attrib
,
3011 save
->gs
.limit
, save
->gs
.base
);
3012 pr_err("gdtr: s: %04x a: %04x l: %08x b: %016llx\n",
3013 save
->gdtr
.selector
, save
->gdtr
.attrib
,
3014 save
->gdtr
.limit
, save
->gdtr
.base
);
3015 pr_err("ldtr: s: %04x a: %04x l: %08x b: %016llx\n",
3016 save
->ldtr
.selector
, save
->ldtr
.attrib
,
3017 save
->ldtr
.limit
, save
->ldtr
.base
);
3018 pr_err("idtr: s: %04x a: %04x l: %08x b: %016llx\n",
3019 save
->idtr
.selector
, save
->idtr
.attrib
,
3020 save
->idtr
.limit
, save
->idtr
.base
);
3021 pr_err("tr: s: %04x a: %04x l: %08x b: %016llx\n",
3022 save
->tr
.selector
, save
->tr
.attrib
,
3023 save
->tr
.limit
, save
->tr
.base
);
3024 pr_err("cpl: %d efer: %016llx\n",
3025 save
->cpl
, save
->efer
);
3026 pr_err("cr0: %016llx cr2: %016llx\n",
3027 save
->cr0
, save
->cr2
);
3028 pr_err("cr3: %016llx cr4: %016llx\n",
3029 save
->cr3
, save
->cr4
);
3030 pr_err("dr6: %016llx dr7: %016llx\n",
3031 save
->dr6
, save
->dr7
);
3032 pr_err("rip: %016llx rflags: %016llx\n",
3033 save
->rip
, save
->rflags
);
3034 pr_err("rsp: %016llx rax: %016llx\n",
3035 save
->rsp
, save
->rax
);
3036 pr_err("star: %016llx lstar: %016llx\n",
3037 save
->star
, save
->lstar
);
3038 pr_err("cstar: %016llx sfmask: %016llx\n",
3039 save
->cstar
, save
->sfmask
);
3040 pr_err("kernel_gs_base: %016llx sysenter_cs: %016llx\n",
3041 save
->kernel_gs_base
, save
->sysenter_cs
);
3042 pr_err("sysenter_esp: %016llx sysenter_eip: %016llx\n",
3043 save
->sysenter_esp
, save
->sysenter_eip
);
3044 pr_err("gpat: %016llx dbgctl: %016llx\n",
3045 save
->g_pat
, save
->dbgctl
);
3046 pr_err("br_from: %016llx br_to: %016llx\n",
3047 save
->br_from
, save
->br_to
);
3048 pr_err("excp_from: %016llx excp_to: %016llx\n",
3049 save
->last_excp_from
, save
->last_excp_to
);
3053 static void svm_get_exit_info(struct kvm_vcpu
*vcpu
, u64
*info1
, u64
*info2
)
3055 struct vmcb_control_area
*control
= &to_svm(vcpu
)->vmcb
->control
;
3057 *info1
= control
->exit_info_1
;
3058 *info2
= control
->exit_info_2
;
3061 static int handle_exit(struct kvm_vcpu
*vcpu
)
3063 struct vcpu_svm
*svm
= to_svm(vcpu
);
3064 struct kvm_run
*kvm_run
= vcpu
->run
;
3065 u32 exit_code
= svm
->vmcb
->control
.exit_code
;
3067 trace_kvm_exit(exit_code
, vcpu
, KVM_ISA_SVM
);
3069 if (!is_cr_intercept(svm
, INTERCEPT_CR0_WRITE
))
3070 vcpu
->arch
.cr0
= svm
->vmcb
->save
.cr0
;
3072 vcpu
->arch
.cr3
= svm
->vmcb
->save
.cr3
;
3074 if (unlikely(svm
->nested
.exit_required
)) {
3075 nested_svm_vmexit(svm
);
3076 svm
->nested
.exit_required
= false;
3081 if (is_guest_mode(vcpu
)) {
3084 trace_kvm_nested_vmexit(svm
->vmcb
->save
.rip
, exit_code
,
3085 svm
->vmcb
->control
.exit_info_1
,
3086 svm
->vmcb
->control
.exit_info_2
,
3087 svm
->vmcb
->control
.exit_int_info
,
3088 svm
->vmcb
->control
.exit_int_info_err
);
3090 vmexit
= nested_svm_exit_special(svm
);
3092 if (vmexit
== NESTED_EXIT_CONTINUE
)
3093 vmexit
= nested_svm_exit_handled(svm
);
3095 if (vmexit
== NESTED_EXIT_DONE
)
3099 svm_complete_interrupts(svm
);
3101 if (svm
->vmcb
->control
.exit_code
== SVM_EXIT_ERR
) {
3102 kvm_run
->exit_reason
= KVM_EXIT_FAIL_ENTRY
;
3103 kvm_run
->fail_entry
.hardware_entry_failure_reason
3104 = svm
->vmcb
->control
.exit_code
;
3105 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
3110 if (is_external_interrupt(svm
->vmcb
->control
.exit_int_info
) &&
3111 exit_code
!= SVM_EXIT_EXCP_BASE
+ PF_VECTOR
&&
3112 exit_code
!= SVM_EXIT_NPF
&& exit_code
!= SVM_EXIT_TASK_SWITCH
&&
3113 exit_code
!= SVM_EXIT_INTR
&& exit_code
!= SVM_EXIT_NMI
)
3114 printk(KERN_ERR
"%s: unexpected exit_ini_info 0x%x "
3116 __func__
, svm
->vmcb
->control
.exit_int_info
,
3119 if (exit_code
>= ARRAY_SIZE(svm_exit_handlers
)
3120 || !svm_exit_handlers
[exit_code
]) {
3121 kvm_run
->exit_reason
= KVM_EXIT_UNKNOWN
;
3122 kvm_run
->hw
.hardware_exit_reason
= exit_code
;
3126 return svm_exit_handlers
[exit_code
](svm
);
3129 static void reload_tss(struct kvm_vcpu
*vcpu
)
3131 int cpu
= raw_smp_processor_id();
3133 struct svm_cpu_data
*sd
= per_cpu(svm_data
, cpu
);
3134 sd
->tss_desc
->type
= 9; /* available 32/64-bit TSS */
3138 static void pre_svm_run(struct vcpu_svm
*svm
)
3140 int cpu
= raw_smp_processor_id();
3142 struct svm_cpu_data
*sd
= per_cpu(svm_data
, cpu
);
3144 svm
->vmcb
->control
.tlb_ctl
= TLB_CONTROL_DO_NOTHING
;
3145 /* FIXME: handle wraparound of asid_generation */
3146 if (svm
->asid_generation
!= sd
->asid_generation
)
3150 static void svm_inject_nmi(struct kvm_vcpu
*vcpu
)
3152 struct vcpu_svm
*svm
= to_svm(vcpu
);
3154 svm
->vmcb
->control
.event_inj
= SVM_EVTINJ_VALID
| SVM_EVTINJ_TYPE_NMI
;
3155 vcpu
->arch
.hflags
|= HF_NMI_MASK
;
3156 set_intercept(svm
, INTERCEPT_IRET
);
3157 ++vcpu
->stat
.nmi_injections
;
3160 static inline void svm_inject_irq(struct vcpu_svm
*svm
, int irq
)
3162 struct vmcb_control_area
*control
;
3164 control
= &svm
->vmcb
->control
;
3165 control
->int_vector
= irq
;
3166 control
->int_ctl
&= ~V_INTR_PRIO_MASK
;
3167 control
->int_ctl
|= V_IRQ_MASK
|
3168 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT
);
3171 static void svm_set_irq(struct kvm_vcpu
*vcpu
)
3173 struct vcpu_svm
*svm
= to_svm(vcpu
);
3175 BUG_ON(!(gif_set(svm
)));
3177 trace_kvm_inj_virq(vcpu
->arch
.interrupt
.nr
);
3178 ++vcpu
->stat
.irq_injections
;
3180 svm
->vmcb
->control
.event_inj
= vcpu
->arch
.interrupt
.nr
|
3181 SVM_EVTINJ_VALID
| SVM_EVTINJ_TYPE_INTR
;
3184 static void update_cr8_intercept(struct kvm_vcpu
*vcpu
, int tpr
, int irr
)
3186 struct vcpu_svm
*svm
= to_svm(vcpu
);
3188 if (is_guest_mode(vcpu
) && (vcpu
->arch
.hflags
& HF_VINTR_MASK
))
3195 set_cr_intercept(svm
, INTERCEPT_CR8_WRITE
);
3198 static int svm_nmi_allowed(struct kvm_vcpu
*vcpu
)
3200 struct vcpu_svm
*svm
= to_svm(vcpu
);
3201 struct vmcb
*vmcb
= svm
->vmcb
;
3203 ret
= !(vmcb
->control
.int_state
& SVM_INTERRUPT_SHADOW_MASK
) &&
3204 !(svm
->vcpu
.arch
.hflags
& HF_NMI_MASK
);
3205 ret
= ret
&& gif_set(svm
) && nested_svm_nmi(svm
);
3210 static bool svm_get_nmi_mask(struct kvm_vcpu
*vcpu
)
3212 struct vcpu_svm
*svm
= to_svm(vcpu
);
3214 return !!(svm
->vcpu
.arch
.hflags
& HF_NMI_MASK
);
3217 static void svm_set_nmi_mask(struct kvm_vcpu
*vcpu
, bool masked
)
3219 struct vcpu_svm
*svm
= to_svm(vcpu
);
3222 svm
->vcpu
.arch
.hflags
|= HF_NMI_MASK
;
3223 set_intercept(svm
, INTERCEPT_IRET
);
3225 svm
->vcpu
.arch
.hflags
&= ~HF_NMI_MASK
;
3226 clr_intercept(svm
, INTERCEPT_IRET
);
3230 static int svm_interrupt_allowed(struct kvm_vcpu
*vcpu
)
3232 struct vcpu_svm
*svm
= to_svm(vcpu
);
3233 struct vmcb
*vmcb
= svm
->vmcb
;
3236 if (!gif_set(svm
) ||
3237 (vmcb
->control
.int_state
& SVM_INTERRUPT_SHADOW_MASK
))
3240 ret
= !!(vmcb
->save
.rflags
& X86_EFLAGS_IF
);
3242 if (is_guest_mode(vcpu
))
3243 return ret
&& !(svm
->vcpu
.arch
.hflags
& HF_VINTR_MASK
);
3248 static void enable_irq_window(struct kvm_vcpu
*vcpu
)
3250 struct vcpu_svm
*svm
= to_svm(vcpu
);
3253 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3254 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3255 * get that intercept, this function will be called again though and
3256 * we'll get the vintr intercept.
3258 if (gif_set(svm
) && nested_svm_intr(svm
)) {
3260 svm_inject_irq(svm
, 0x0);
3264 static void enable_nmi_window(struct kvm_vcpu
*vcpu
)
3266 struct vcpu_svm
*svm
= to_svm(vcpu
);
3268 if ((svm
->vcpu
.arch
.hflags
& (HF_NMI_MASK
| HF_IRET_MASK
))
3270 return; /* IRET will cause a vm exit */
3273 * Something prevents NMI from been injected. Single step over possible
3274 * problem (IRET or exception injection or interrupt shadow)
3276 svm
->nmi_singlestep
= true;
3277 svm
->vmcb
->save
.rflags
|= (X86_EFLAGS_TF
| X86_EFLAGS_RF
);
3278 update_db_intercept(vcpu
);
3281 static int svm_set_tss_addr(struct kvm
*kvm
, unsigned int addr
)
3286 static void svm_flush_tlb(struct kvm_vcpu
*vcpu
)
3288 force_new_asid(vcpu
);
3291 static void svm_prepare_guest_switch(struct kvm_vcpu
*vcpu
)
3295 static inline void sync_cr8_to_lapic(struct kvm_vcpu
*vcpu
)
3297 struct vcpu_svm
*svm
= to_svm(vcpu
);
3299 if (is_guest_mode(vcpu
) && (vcpu
->arch
.hflags
& HF_VINTR_MASK
))
3302 if (!is_cr_intercept(svm
, INTERCEPT_CR8_WRITE
)) {
3303 int cr8
= svm
->vmcb
->control
.int_ctl
& V_TPR_MASK
;
3304 kvm_set_cr8(vcpu
, cr8
);
3308 static inline void sync_lapic_to_cr8(struct kvm_vcpu
*vcpu
)
3310 struct vcpu_svm
*svm
= to_svm(vcpu
);
3313 if (is_guest_mode(vcpu
) && (vcpu
->arch
.hflags
& HF_VINTR_MASK
))
3316 cr8
= kvm_get_cr8(vcpu
);
3317 svm
->vmcb
->control
.int_ctl
&= ~V_TPR_MASK
;
3318 svm
->vmcb
->control
.int_ctl
|= cr8
& V_TPR_MASK
;
3321 static void svm_complete_interrupts(struct vcpu_svm
*svm
)
3325 u32 exitintinfo
= svm
->vmcb
->control
.exit_int_info
;
3326 unsigned int3_injected
= svm
->int3_injected
;
3328 svm
->int3_injected
= 0;
3330 if (svm
->vcpu
.arch
.hflags
& HF_IRET_MASK
) {
3331 svm
->vcpu
.arch
.hflags
&= ~(HF_NMI_MASK
| HF_IRET_MASK
);
3332 kvm_make_request(KVM_REQ_EVENT
, &svm
->vcpu
);
3335 svm
->vcpu
.arch
.nmi_injected
= false;
3336 kvm_clear_exception_queue(&svm
->vcpu
);
3337 kvm_clear_interrupt_queue(&svm
->vcpu
);
3339 if (!(exitintinfo
& SVM_EXITINTINFO_VALID
))
3342 kvm_make_request(KVM_REQ_EVENT
, &svm
->vcpu
);
3344 vector
= exitintinfo
& SVM_EXITINTINFO_VEC_MASK
;
3345 type
= exitintinfo
& SVM_EXITINTINFO_TYPE_MASK
;
3348 case SVM_EXITINTINFO_TYPE_NMI
:
3349 svm
->vcpu
.arch
.nmi_injected
= true;
3351 case SVM_EXITINTINFO_TYPE_EXEPT
:
3353 * In case of software exceptions, do not reinject the vector,
3354 * but re-execute the instruction instead. Rewind RIP first
3355 * if we emulated INT3 before.
3357 if (kvm_exception_is_soft(vector
)) {
3358 if (vector
== BP_VECTOR
&& int3_injected
&&
3359 kvm_is_linear_rip(&svm
->vcpu
, svm
->int3_rip
))
3360 kvm_rip_write(&svm
->vcpu
,
3361 kvm_rip_read(&svm
->vcpu
) -
3365 if (exitintinfo
& SVM_EXITINTINFO_VALID_ERR
) {
3366 u32 err
= svm
->vmcb
->control
.exit_int_info_err
;
3367 kvm_requeue_exception_e(&svm
->vcpu
, vector
, err
);
3370 kvm_requeue_exception(&svm
->vcpu
, vector
);
3372 case SVM_EXITINTINFO_TYPE_INTR
:
3373 kvm_queue_interrupt(&svm
->vcpu
, vector
, false);
3380 static void svm_cancel_injection(struct kvm_vcpu
*vcpu
)
3382 struct vcpu_svm
*svm
= to_svm(vcpu
);
3383 struct vmcb_control_area
*control
= &svm
->vmcb
->control
;
3385 control
->exit_int_info
= control
->event_inj
;
3386 control
->exit_int_info_err
= control
->event_inj_err
;
3387 control
->event_inj
= 0;
3388 svm_complete_interrupts(svm
);
3391 #ifdef CONFIG_X86_64
3397 static void svm_vcpu_run(struct kvm_vcpu
*vcpu
)
3399 struct vcpu_svm
*svm
= to_svm(vcpu
);
3401 svm
->vmcb
->save
.rax
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
3402 svm
->vmcb
->save
.rsp
= vcpu
->arch
.regs
[VCPU_REGS_RSP
];
3403 svm
->vmcb
->save
.rip
= vcpu
->arch
.regs
[VCPU_REGS_RIP
];
3406 * A vmexit emulation is required before the vcpu can be executed
3409 if (unlikely(svm
->nested
.exit_required
))
3414 sync_lapic_to_cr8(vcpu
);
3416 svm
->vmcb
->save
.cr2
= vcpu
->arch
.cr2
;
3423 "push %%"R
"bp; \n\t"
3424 "mov %c[rbx](%[svm]), %%"R
"bx \n\t"
3425 "mov %c[rcx](%[svm]), %%"R
"cx \n\t"
3426 "mov %c[rdx](%[svm]), %%"R
"dx \n\t"
3427 "mov %c[rsi](%[svm]), %%"R
"si \n\t"
3428 "mov %c[rdi](%[svm]), %%"R
"di \n\t"
3429 "mov %c[rbp](%[svm]), %%"R
"bp \n\t"
3430 #ifdef CONFIG_X86_64
3431 "mov %c[r8](%[svm]), %%r8 \n\t"
3432 "mov %c[r9](%[svm]), %%r9 \n\t"
3433 "mov %c[r10](%[svm]), %%r10 \n\t"
3434 "mov %c[r11](%[svm]), %%r11 \n\t"
3435 "mov %c[r12](%[svm]), %%r12 \n\t"
3436 "mov %c[r13](%[svm]), %%r13 \n\t"
3437 "mov %c[r14](%[svm]), %%r14 \n\t"
3438 "mov %c[r15](%[svm]), %%r15 \n\t"
3441 /* Enter guest mode */
3443 "mov %c[vmcb](%[svm]), %%"R
"ax \n\t"
3444 __ex(SVM_VMLOAD
) "\n\t"
3445 __ex(SVM_VMRUN
) "\n\t"
3446 __ex(SVM_VMSAVE
) "\n\t"
3449 /* Save guest registers, load host registers */
3450 "mov %%"R
"bx, %c[rbx](%[svm]) \n\t"
3451 "mov %%"R
"cx, %c[rcx](%[svm]) \n\t"
3452 "mov %%"R
"dx, %c[rdx](%[svm]) \n\t"
3453 "mov %%"R
"si, %c[rsi](%[svm]) \n\t"
3454 "mov %%"R
"di, %c[rdi](%[svm]) \n\t"
3455 "mov %%"R
"bp, %c[rbp](%[svm]) \n\t"
3456 #ifdef CONFIG_X86_64
3457 "mov %%r8, %c[r8](%[svm]) \n\t"
3458 "mov %%r9, %c[r9](%[svm]) \n\t"
3459 "mov %%r10, %c[r10](%[svm]) \n\t"
3460 "mov %%r11, %c[r11](%[svm]) \n\t"
3461 "mov %%r12, %c[r12](%[svm]) \n\t"
3462 "mov %%r13, %c[r13](%[svm]) \n\t"
3463 "mov %%r14, %c[r14](%[svm]) \n\t"
3464 "mov %%r15, %c[r15](%[svm]) \n\t"
3469 [vmcb
]"i"(offsetof(struct vcpu_svm
, vmcb_pa
)),
3470 [rbx
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RBX
])),
3471 [rcx
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RCX
])),
3472 [rdx
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RDX
])),
3473 [rsi
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RSI
])),
3474 [rdi
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RDI
])),
3475 [rbp
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_RBP
]))
3476 #ifdef CONFIG_X86_64
3477 , [r8
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R8
])),
3478 [r9
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R9
])),
3479 [r10
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R10
])),
3480 [r11
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R11
])),
3481 [r12
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R12
])),
3482 [r13
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R13
])),
3483 [r14
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R14
])),
3484 [r15
]"i"(offsetof(struct vcpu_svm
, vcpu
.arch
.regs
[VCPU_REGS_R15
]))
3487 , R
"bx", R
"cx", R
"dx", R
"si", R
"di"
3488 #ifdef CONFIG_X86_64
3489 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3493 #ifdef CONFIG_X86_64
3494 wrmsrl(MSR_GS_BASE
, svm
->host
.gs_base
);
3496 loadsegment(fs
, svm
->host
.fs
);
3501 local_irq_disable();
3505 vcpu
->arch
.cr2
= svm
->vmcb
->save
.cr2
;
3506 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = svm
->vmcb
->save
.rax
;
3507 vcpu
->arch
.regs
[VCPU_REGS_RSP
] = svm
->vmcb
->save
.rsp
;
3508 vcpu
->arch
.regs
[VCPU_REGS_RIP
] = svm
->vmcb
->save
.rip
;
3510 sync_cr8_to_lapic(vcpu
);
3514 /* if exit due to PF check for async PF */
3515 if (svm
->vmcb
->control
.exit_code
== SVM_EXIT_EXCP_BASE
+ PF_VECTOR
)
3516 svm
->apf_reason
= kvm_read_and_reset_pf_reason();
3519 vcpu
->arch
.regs_avail
&= ~(1 << VCPU_EXREG_PDPTR
);
3520 vcpu
->arch
.regs_dirty
&= ~(1 << VCPU_EXREG_PDPTR
);
3524 * We need to handle MC intercepts here before the vcpu has a chance to
3525 * change the physical cpu
3527 if (unlikely(svm
->vmcb
->control
.exit_code
==
3528 SVM_EXIT_EXCP_BASE
+ MC_VECTOR
))
3529 svm_handle_mce(svm
);
3531 mark_all_clean(svm
->vmcb
);
3536 static void svm_set_cr3(struct kvm_vcpu
*vcpu
, unsigned long root
)
3538 struct vcpu_svm
*svm
= to_svm(vcpu
);
3540 svm
->vmcb
->save
.cr3
= root
;
3541 force_new_asid(vcpu
);
3544 static void set_tdp_cr3(struct kvm_vcpu
*vcpu
, unsigned long root
)
3546 struct vcpu_svm
*svm
= to_svm(vcpu
);
3548 svm
->vmcb
->control
.nested_cr3
= root
;
3550 /* Also sync guest cr3 here in case we live migrate */
3551 svm
->vmcb
->save
.cr3
= vcpu
->arch
.cr3
;
3553 force_new_asid(vcpu
);
3556 static int is_disabled(void)
3560 rdmsrl(MSR_VM_CR
, vm_cr
);
3561 if (vm_cr
& (1 << SVM_VM_CR_SVM_DISABLE
))
3568 svm_patch_hypercall(struct kvm_vcpu
*vcpu
, unsigned char *hypercall
)
3571 * Patch in the VMMCALL instruction:
3573 hypercall
[0] = 0x0f;
3574 hypercall
[1] = 0x01;
3575 hypercall
[2] = 0xd9;
3578 static void svm_check_processor_compat(void *rtn
)
3583 static bool svm_cpu_has_accelerated_tpr(void)
3588 static u64
svm_get_mt_mask(struct kvm_vcpu
*vcpu
, gfn_t gfn
, bool is_mmio
)
3593 static void svm_cpuid_update(struct kvm_vcpu
*vcpu
)
3597 static void svm_set_supported_cpuid(u32 func
, struct kvm_cpuid_entry2
*entry
)
3601 /* Mask out xsave bit as long as it is not supported by SVM */
3602 entry
->ecx
&= ~(bit(X86_FEATURE_XSAVE
));
3606 entry
->ecx
|= (1 << 2); /* Set SVM bit */
3609 entry
->eax
= 1; /* SVM revision 1 */
3610 entry
->ebx
= 8; /* Lets support 8 ASIDs in case we add proper
3611 ASID emulation to nested SVM */
3612 entry
->ecx
= 0; /* Reserved */
3613 entry
->edx
= 0; /* Per default do not support any
3614 additional features */
3616 /* Support next_rip if host supports it */
3617 if (boot_cpu_has(X86_FEATURE_NRIPS
))
3618 entry
->edx
|= SVM_FEATURE_NRIP
;
3620 /* Support NPT for the guest if enabled */
3622 entry
->edx
|= SVM_FEATURE_NPT
;
3628 static const struct trace_print_flags svm_exit_reasons_str
[] = {
3629 { SVM_EXIT_READ_CR0
, "read_cr0" },
3630 { SVM_EXIT_READ_CR3
, "read_cr3" },
3631 { SVM_EXIT_READ_CR4
, "read_cr4" },
3632 { SVM_EXIT_READ_CR8
, "read_cr8" },
3633 { SVM_EXIT_WRITE_CR0
, "write_cr0" },
3634 { SVM_EXIT_WRITE_CR3
, "write_cr3" },
3635 { SVM_EXIT_WRITE_CR4
, "write_cr4" },
3636 { SVM_EXIT_WRITE_CR8
, "write_cr8" },
3637 { SVM_EXIT_READ_DR0
, "read_dr0" },
3638 { SVM_EXIT_READ_DR1
, "read_dr1" },
3639 { SVM_EXIT_READ_DR2
, "read_dr2" },
3640 { SVM_EXIT_READ_DR3
, "read_dr3" },
3641 { SVM_EXIT_WRITE_DR0
, "write_dr0" },
3642 { SVM_EXIT_WRITE_DR1
, "write_dr1" },
3643 { SVM_EXIT_WRITE_DR2
, "write_dr2" },
3644 { SVM_EXIT_WRITE_DR3
, "write_dr3" },
3645 { SVM_EXIT_WRITE_DR5
, "write_dr5" },
3646 { SVM_EXIT_WRITE_DR7
, "write_dr7" },
3647 { SVM_EXIT_EXCP_BASE
+ DB_VECTOR
, "DB excp" },
3648 { SVM_EXIT_EXCP_BASE
+ BP_VECTOR
, "BP excp" },
3649 { SVM_EXIT_EXCP_BASE
+ UD_VECTOR
, "UD excp" },
3650 { SVM_EXIT_EXCP_BASE
+ PF_VECTOR
, "PF excp" },
3651 { SVM_EXIT_EXCP_BASE
+ NM_VECTOR
, "NM excp" },
3652 { SVM_EXIT_EXCP_BASE
+ MC_VECTOR
, "MC excp" },
3653 { SVM_EXIT_INTR
, "interrupt" },
3654 { SVM_EXIT_NMI
, "nmi" },
3655 { SVM_EXIT_SMI
, "smi" },
3656 { SVM_EXIT_INIT
, "init" },
3657 { SVM_EXIT_VINTR
, "vintr" },
3658 { SVM_EXIT_CPUID
, "cpuid" },
3659 { SVM_EXIT_INVD
, "invd" },
3660 { SVM_EXIT_HLT
, "hlt" },
3661 { SVM_EXIT_INVLPG
, "invlpg" },
3662 { SVM_EXIT_INVLPGA
, "invlpga" },
3663 { SVM_EXIT_IOIO
, "io" },
3664 { SVM_EXIT_MSR
, "msr" },
3665 { SVM_EXIT_TASK_SWITCH
, "task_switch" },
3666 { SVM_EXIT_SHUTDOWN
, "shutdown" },
3667 { SVM_EXIT_VMRUN
, "vmrun" },
3668 { SVM_EXIT_VMMCALL
, "hypercall" },
3669 { SVM_EXIT_VMLOAD
, "vmload" },
3670 { SVM_EXIT_VMSAVE
, "vmsave" },
3671 { SVM_EXIT_STGI
, "stgi" },
3672 { SVM_EXIT_CLGI
, "clgi" },
3673 { SVM_EXIT_SKINIT
, "skinit" },
3674 { SVM_EXIT_WBINVD
, "wbinvd" },
3675 { SVM_EXIT_MONITOR
, "monitor" },
3676 { SVM_EXIT_MWAIT
, "mwait" },
3677 { SVM_EXIT_NPF
, "npf" },
3681 static int svm_get_lpage_level(void)
3683 return PT_PDPE_LEVEL
;
3686 static bool svm_rdtscp_supported(void)
3691 static bool svm_has_wbinvd_exit(void)
3696 static void svm_fpu_deactivate(struct kvm_vcpu
*vcpu
)
3698 struct vcpu_svm
*svm
= to_svm(vcpu
);
3700 set_exception_intercept(svm
, NM_VECTOR
);
3701 update_cr0_intercept(svm
);
3704 static struct kvm_x86_ops svm_x86_ops
= {
3705 .cpu_has_kvm_support
= has_svm
,
3706 .disabled_by_bios
= is_disabled
,
3707 .hardware_setup
= svm_hardware_setup
,
3708 .hardware_unsetup
= svm_hardware_unsetup
,
3709 .check_processor_compatibility
= svm_check_processor_compat
,
3710 .hardware_enable
= svm_hardware_enable
,
3711 .hardware_disable
= svm_hardware_disable
,
3712 .cpu_has_accelerated_tpr
= svm_cpu_has_accelerated_tpr
,
3714 .vcpu_create
= svm_create_vcpu
,
3715 .vcpu_free
= svm_free_vcpu
,
3716 .vcpu_reset
= svm_vcpu_reset
,
3718 .prepare_guest_switch
= svm_prepare_guest_switch
,
3719 .vcpu_load
= svm_vcpu_load
,
3720 .vcpu_put
= svm_vcpu_put
,
3722 .set_guest_debug
= svm_guest_debug
,
3723 .get_msr
= svm_get_msr
,
3724 .set_msr
= svm_set_msr
,
3725 .get_segment_base
= svm_get_segment_base
,
3726 .get_segment
= svm_get_segment
,
3727 .set_segment
= svm_set_segment
,
3728 .get_cpl
= svm_get_cpl
,
3729 .get_cs_db_l_bits
= kvm_get_cs_db_l_bits
,
3730 .decache_cr0_guest_bits
= svm_decache_cr0_guest_bits
,
3731 .decache_cr4_guest_bits
= svm_decache_cr4_guest_bits
,
3732 .set_cr0
= svm_set_cr0
,
3733 .set_cr3
= svm_set_cr3
,
3734 .set_cr4
= svm_set_cr4
,
3735 .set_efer
= svm_set_efer
,
3736 .get_idt
= svm_get_idt
,
3737 .set_idt
= svm_set_idt
,
3738 .get_gdt
= svm_get_gdt
,
3739 .set_gdt
= svm_set_gdt
,
3740 .set_dr7
= svm_set_dr7
,
3741 .cache_reg
= svm_cache_reg
,
3742 .get_rflags
= svm_get_rflags
,
3743 .set_rflags
= svm_set_rflags
,
3744 .fpu_activate
= svm_fpu_activate
,
3745 .fpu_deactivate
= svm_fpu_deactivate
,
3747 .tlb_flush
= svm_flush_tlb
,
3749 .run
= svm_vcpu_run
,
3750 .handle_exit
= handle_exit
,
3751 .skip_emulated_instruction
= skip_emulated_instruction
,
3752 .set_interrupt_shadow
= svm_set_interrupt_shadow
,
3753 .get_interrupt_shadow
= svm_get_interrupt_shadow
,
3754 .patch_hypercall
= svm_patch_hypercall
,
3755 .set_irq
= svm_set_irq
,
3756 .set_nmi
= svm_inject_nmi
,
3757 .queue_exception
= svm_queue_exception
,
3758 .cancel_injection
= svm_cancel_injection
,
3759 .interrupt_allowed
= svm_interrupt_allowed
,
3760 .nmi_allowed
= svm_nmi_allowed
,
3761 .get_nmi_mask
= svm_get_nmi_mask
,
3762 .set_nmi_mask
= svm_set_nmi_mask
,
3763 .enable_nmi_window
= enable_nmi_window
,
3764 .enable_irq_window
= enable_irq_window
,
3765 .update_cr8_intercept
= update_cr8_intercept
,
3767 .set_tss_addr
= svm_set_tss_addr
,
3768 .get_tdp_level
= get_npt_level
,
3769 .get_mt_mask
= svm_get_mt_mask
,
3771 .get_exit_info
= svm_get_exit_info
,
3772 .exit_reasons_str
= svm_exit_reasons_str
,
3774 .get_lpage_level
= svm_get_lpage_level
,
3776 .cpuid_update
= svm_cpuid_update
,
3778 .rdtscp_supported
= svm_rdtscp_supported
,
3780 .set_supported_cpuid
= svm_set_supported_cpuid
,
3782 .has_wbinvd_exit
= svm_has_wbinvd_exit
,
3784 .write_tsc_offset
= svm_write_tsc_offset
,
3785 .adjust_tsc_offset
= svm_adjust_tsc_offset
,
3787 .set_tdp_cr3
= set_tdp_cr3
,
3790 static int __init
svm_init(void)
3792 return kvm_init(&svm_x86_ops
, sizeof(struct vcpu_svm
),
3793 __alignof__(struct vcpu_svm
), THIS_MODULE
);
3796 static void __exit
svm_exit(void)
3801 module_init(svm_init
)
3802 module_exit(svm_exit
)