2 * Kernel-based Virtual Machine driver for Linux
6 * Copyright (C) 2006 Qumranet, Inc.
9 * Yaniv Kamay <yaniv@qumranet.com>
10 * Avi Kivity <avi@qumranet.com>
12 * This work is licensed under the terms of the GNU GPL, version 2. See
13 * the COPYING file in the top-level directory.
17 #include <linux/module.h>
18 #include <linux/vmalloc.h>
19 #include <linux/highmem.h>
20 #include <linux/profile.h>
24 #include "x86_emulate.h"
26 MODULE_AUTHOR("Qumranet");
27 MODULE_LICENSE("GPL");
29 #define IOPM_ALLOC_ORDER 2
30 #define MSRPM_ALLOC_ORDER 1
36 #define DR7_GD_MASK (1 << 13)
37 #define DR6_BD_MASK (1 << 13)
38 #define CR4_DE_MASK (1UL << 3)
40 #define SEG_TYPE_LDT 2
41 #define SEG_TYPE_BUSY_TSS16 3
43 #define KVM_EFER_LMA (1 << 10)
44 #define KVM_EFER_LME (1 << 8)
46 unsigned long iopm_base
;
47 unsigned long msrpm_base
;
49 struct kvm_ldttss_desc
{
52 unsigned base1
: 8, type
: 5, dpl
: 2, p
: 1;
53 unsigned limit1
: 4, zero0
: 3, g
: 1, base2
: 8;
56 } __attribute__((packed
));
61 uint64_t asid_generation
;
64 struct kvm_ldttss_desc
*tss_desc
;
66 struct page
*save_area
;
69 static DEFINE_PER_CPU(struct svm_cpu_data
*, svm_data
);
71 struct svm_init_data
{
76 static u32 msrpm_ranges
[] = {0, 0xc0000000, 0xc0010000};
78 #define NUM_MSR_MAPS (sizeof(msrpm_ranges) / sizeof(*msrpm_ranges))
79 #define MSRS_RANGE_SIZE 2048
80 #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
82 #define MAX_INST_SIZE 15
84 static unsigned get_addr_size(struct kvm_vcpu
*vcpu
)
86 struct vmcb_save_area
*sa
= &vcpu
->svm
->vmcb
->save
;
89 if (!(sa
->cr0
& CR0_PE_MASK
) || (sa
->rflags
& X86_EFLAGS_VM
))
92 cs_attrib
= sa
->cs
.attrib
;
94 return (cs_attrib
& SVM_SELECTOR_L_MASK
) ? 8 :
95 (cs_attrib
& SVM_SELECTOR_DB_MASK
) ? 4 : 2;
98 static inline u8
pop_irq(struct kvm_vcpu
*vcpu
)
100 int word_index
= __ffs(vcpu
->irq_summary
);
101 int bit_index
= __ffs(vcpu
->irq_pending
[word_index
]);
102 int irq
= word_index
* BITS_PER_LONG
+ bit_index
;
104 clear_bit(bit_index
, &vcpu
->irq_pending
[word_index
]);
105 if (!vcpu
->irq_pending
[word_index
])
106 clear_bit(word_index
, &vcpu
->irq_summary
);
110 static inline void push_irq(struct kvm_vcpu
*vcpu
, u8 irq
)
112 set_bit(irq
, vcpu
->irq_pending
);
113 set_bit(irq
/ BITS_PER_LONG
, &vcpu
->irq_summary
);
116 static inline void clgi(void)
118 asm volatile (SVM_CLGI
);
121 static inline void stgi(void)
123 asm volatile (SVM_STGI
);
126 static inline void invlpga(unsigned long addr
, u32 asid
)
128 asm volatile (SVM_INVLPGA :: "a"(addr
), "c"(asid
));
131 static inline unsigned long kvm_read_cr2(void)
135 asm volatile ("mov %%cr2, %0" : "=r" (cr2
));
139 static inline void kvm_write_cr2(unsigned long val
)
141 asm volatile ("mov %0, %%cr2" :: "r" (val
));
144 static inline unsigned long read_dr6(void)
148 asm volatile ("mov %%dr6, %0" : "=r" (dr6
));
152 static inline void write_dr6(unsigned long val
)
154 asm volatile ("mov %0, %%dr6" :: "r" (val
));
157 static inline unsigned long read_dr7(void)
161 asm volatile ("mov %%dr7, %0" : "=r" (dr7
));
165 static inline void write_dr7(unsigned long val
)
167 asm volatile ("mov %0, %%dr7" :: "r" (val
));
170 static inline void force_new_asid(struct kvm_vcpu
*vcpu
)
172 vcpu
->svm
->asid_generation
--;
175 static inline void flush_guest_tlb(struct kvm_vcpu
*vcpu
)
177 force_new_asid(vcpu
);
180 static void svm_set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
182 if (!(efer
& KVM_EFER_LMA
))
183 efer
&= ~KVM_EFER_LME
;
185 vcpu
->svm
->vmcb
->save
.efer
= efer
| MSR_EFER_SVME_MASK
;
186 vcpu
->shadow_efer
= efer
;
189 static void svm_inject_gp(struct kvm_vcpu
*vcpu
, unsigned error_code
)
191 vcpu
->svm
->vmcb
->control
.event_inj
= SVM_EVTINJ_VALID
|
192 SVM_EVTINJ_VALID_ERR
|
193 SVM_EVTINJ_TYPE_EXEPT
|
195 vcpu
->svm
->vmcb
->control
.event_inj_err
= error_code
;
198 static void inject_ud(struct kvm_vcpu
*vcpu
)
200 vcpu
->svm
->vmcb
->control
.event_inj
= SVM_EVTINJ_VALID
|
201 SVM_EVTINJ_TYPE_EXEPT
|
205 static void inject_db(struct kvm_vcpu
*vcpu
)
207 vcpu
->svm
->vmcb
->control
.event_inj
= SVM_EVTINJ_VALID
|
208 SVM_EVTINJ_TYPE_EXEPT
|
212 static int is_page_fault(uint32_t info
)
214 info
&= SVM_EVTINJ_VEC_MASK
| SVM_EVTINJ_TYPE_MASK
| SVM_EVTINJ_VALID
;
215 return info
== (PF_VECTOR
| SVM_EVTINJ_VALID
| SVM_EVTINJ_TYPE_EXEPT
);
218 static int is_external_interrupt(u32 info
)
220 info
&= SVM_EVTINJ_TYPE_MASK
| SVM_EVTINJ_VALID
;
221 return info
== (SVM_EVTINJ_VALID
| SVM_EVTINJ_TYPE_INTR
);
224 static void skip_emulated_instruction(struct kvm_vcpu
*vcpu
)
226 if (!vcpu
->svm
->next_rip
) {
227 printk(KERN_DEBUG
"%s: NOP\n", __FUNCTION__
);
230 if (vcpu
->svm
->next_rip
- vcpu
->svm
->vmcb
->save
.rip
> 15) {
231 printk(KERN_ERR
"%s: ip 0x%llx next 0x%llx\n",
233 vcpu
->svm
->vmcb
->save
.rip
,
234 vcpu
->svm
->next_rip
);
237 vcpu
->rip
= vcpu
->svm
->vmcb
->save
.rip
= vcpu
->svm
->next_rip
;
238 vcpu
->svm
->vmcb
->control
.int_state
&= ~SVM_INTERRUPT_SHADOW_MASK
;
240 vcpu
->interrupt_window_open
= 1;
243 static int has_svm(void)
245 uint32_t eax
, ebx
, ecx
, edx
;
247 if (boot_cpu_data
.x86_vendor
!= X86_VENDOR_AMD
) {
248 printk(KERN_INFO
"has_svm: not amd\n");
252 cpuid(0x80000000, &eax
, &ebx
, &ecx
, &edx
);
253 if (eax
< SVM_CPUID_FUNC
) {
254 printk(KERN_INFO
"has_svm: can't execute cpuid_8000000a\n");
258 cpuid(0x80000001, &eax
, &ebx
, &ecx
, &edx
);
259 if (!(ecx
& (1 << SVM_CPUID_FEATURE_SHIFT
))) {
260 printk(KERN_DEBUG
"has_svm: svm not available\n");
266 static void svm_hardware_disable(void *garbage
)
268 struct svm_cpu_data
*svm_data
269 = per_cpu(svm_data
, raw_smp_processor_id());
274 wrmsrl(MSR_VM_HSAVE_PA
, 0);
275 rdmsrl(MSR_EFER
, efer
);
276 wrmsrl(MSR_EFER
, efer
& ~MSR_EFER_SVME_MASK
);
277 per_cpu(svm_data
, raw_smp_processor_id()) = 0;
278 __free_page(svm_data
->save_area
);
283 static void svm_hardware_enable(void *garbage
)
286 struct svm_cpu_data
*svm_data
;
289 struct desc_ptr gdt_descr
;
291 struct Xgt_desc_struct gdt_descr
;
293 struct desc_struct
*gdt
;
294 int me
= raw_smp_processor_id();
297 printk(KERN_ERR
"svm_cpu_init: err EOPNOTSUPP on %d\n", me
);
300 svm_data
= per_cpu(svm_data
, me
);
303 printk(KERN_ERR
"svm_cpu_init: svm_data is NULL on %d\n",
308 svm_data
->asid_generation
= 1;
309 svm_data
->max_asid
= cpuid_ebx(SVM_CPUID_FUNC
) - 1;
310 svm_data
->next_asid
= svm_data
->max_asid
+ 1;
312 asm volatile ( "sgdt %0" : "=m"(gdt_descr
) );
313 gdt
= (struct desc_struct
*)gdt_descr
.address
;
314 svm_data
->tss_desc
= (struct kvm_ldttss_desc
*)(gdt
+ GDT_ENTRY_TSS
);
316 rdmsrl(MSR_EFER
, efer
);
317 wrmsrl(MSR_EFER
, efer
| MSR_EFER_SVME_MASK
);
319 wrmsrl(MSR_VM_HSAVE_PA
,
320 page_to_pfn(svm_data
->save_area
) << PAGE_SHIFT
);
323 static int svm_cpu_init(int cpu
)
325 struct svm_cpu_data
*svm_data
;
328 svm_data
= kzalloc(sizeof(struct svm_cpu_data
), GFP_KERNEL
);
332 svm_data
->save_area
= alloc_page(GFP_KERNEL
);
334 if (!svm_data
->save_area
)
337 per_cpu(svm_data
, cpu
) = svm_data
;
347 static int set_msr_interception(u32
*msrpm
, unsigned msr
,
352 for (i
= 0; i
< NUM_MSR_MAPS
; i
++) {
353 if (msr
>= msrpm_ranges
[i
] &&
354 msr
< msrpm_ranges
[i
] + MSRS_IN_RANGE
) {
355 u32 msr_offset
= (i
* MSRS_IN_RANGE
+ msr
-
356 msrpm_ranges
[i
]) * 2;
358 u32
*base
= msrpm
+ (msr_offset
/ 32);
359 u32 msr_shift
= msr_offset
% 32;
360 u32 mask
= ((write
) ? 0 : 2) | ((read
) ? 0 : 1);
361 *base
= (*base
& ~(0x3 << msr_shift
)) |
366 printk(KERN_DEBUG
"%s: not found 0x%x\n", __FUNCTION__
, msr
);
370 static __init
int svm_hardware_setup(void)
373 struct page
*iopm_pages
;
374 struct page
*msrpm_pages
;
378 kvm_emulator_want_group7_invlpg();
380 iopm_pages
= alloc_pages(GFP_KERNEL
, IOPM_ALLOC_ORDER
);
384 memset(page_address(iopm_pages
), 0xff,
385 PAGE_SIZE
* (1 << IOPM_ALLOC_ORDER
));
386 iopm_base
= page_to_pfn(iopm_pages
) << PAGE_SHIFT
;
389 msrpm_pages
= alloc_pages(GFP_KERNEL
, MSRPM_ALLOC_ORDER
);
395 msrpm_va
= page_address(msrpm_pages
);
396 memset(msrpm_va
, 0xff, PAGE_SIZE
* (1 << MSRPM_ALLOC_ORDER
));
397 msrpm_base
= page_to_pfn(msrpm_pages
) << PAGE_SHIFT
;
400 set_msr_interception(msrpm_va
, MSR_GS_BASE
, 1, 1);
401 set_msr_interception(msrpm_va
, MSR_FS_BASE
, 1, 1);
402 set_msr_interception(msrpm_va
, MSR_KERNEL_GS_BASE
, 1, 1);
403 set_msr_interception(msrpm_va
, MSR_LSTAR
, 1, 1);
404 set_msr_interception(msrpm_va
, MSR_CSTAR
, 1, 1);
405 set_msr_interception(msrpm_va
, MSR_SYSCALL_MASK
, 1, 1);
407 set_msr_interception(msrpm_va
, MSR_K6_STAR
, 1, 1);
408 set_msr_interception(msrpm_va
, MSR_IA32_SYSENTER_CS
, 1, 1);
409 set_msr_interception(msrpm_va
, MSR_IA32_SYSENTER_ESP
, 1, 1);
410 set_msr_interception(msrpm_va
, MSR_IA32_SYSENTER_EIP
, 1, 1);
412 for_each_online_cpu(cpu
) {
413 r
= svm_cpu_init(cpu
);
420 __free_pages(msrpm_pages
, MSRPM_ALLOC_ORDER
);
423 __free_pages(iopm_pages
, IOPM_ALLOC_ORDER
);
428 static __exit
void svm_hardware_unsetup(void)
430 __free_pages(pfn_to_page(msrpm_base
>> PAGE_SHIFT
), MSRPM_ALLOC_ORDER
);
431 __free_pages(pfn_to_page(iopm_base
>> PAGE_SHIFT
), IOPM_ALLOC_ORDER
);
432 iopm_base
= msrpm_base
= 0;
435 static void init_seg(struct vmcb_seg
*seg
)
438 seg
->attrib
= SVM_SELECTOR_P_MASK
| SVM_SELECTOR_S_MASK
|
439 SVM_SELECTOR_WRITE_MASK
; /* Read/Write Data Segment */
444 static void init_sys_seg(struct vmcb_seg
*seg
, uint32_t type
)
447 seg
->attrib
= SVM_SELECTOR_P_MASK
| type
;
452 static int svm_vcpu_setup(struct kvm_vcpu
*vcpu
)
457 static void init_vmcb(struct vmcb
*vmcb
)
459 struct vmcb_control_area
*control
= &vmcb
->control
;
460 struct vmcb_save_area
*save
= &vmcb
->save
;
463 control
->intercept_cr_read
= INTERCEPT_CR0_MASK
|
467 control
->intercept_cr_write
= INTERCEPT_CR0_MASK
|
471 control
->intercept_dr_read
= INTERCEPT_DR0_MASK
|
476 control
->intercept_dr_write
= INTERCEPT_DR0_MASK
|
483 control
->intercept_exceptions
= 1 << PF_VECTOR
;
486 control
->intercept
= (1ULL << INTERCEPT_INTR
) |
487 (1ULL << INTERCEPT_NMI
) |
489 * selective cr0 intercept bug?
490 * 0: 0f 22 d8 mov %eax,%cr3
491 * 3: 0f 20 c0 mov %cr0,%eax
492 * 6: 0d 00 00 00 80 or $0x80000000,%eax
493 * b: 0f 22 c0 mov %eax,%cr0
494 * set cr3 ->interception
495 * get cr0 ->interception
496 * set cr0 -> no interception
498 /* (1ULL << INTERCEPT_SELECTIVE_CR0) | */
499 (1ULL << INTERCEPT_CPUID
) |
500 (1ULL << INTERCEPT_HLT
) |
501 (1ULL << INTERCEPT_INVLPGA
) |
502 (1ULL << INTERCEPT_IOIO_PROT
) |
503 (1ULL << INTERCEPT_MSR_PROT
) |
504 (1ULL << INTERCEPT_TASK_SWITCH
) |
505 (1ULL << INTERCEPT_SHUTDOWN
) |
506 (1ULL << INTERCEPT_VMRUN
) |
507 (1ULL << INTERCEPT_VMMCALL
) |
508 (1ULL << INTERCEPT_VMLOAD
) |
509 (1ULL << INTERCEPT_VMSAVE
) |
510 (1ULL << INTERCEPT_STGI
) |
511 (1ULL << INTERCEPT_CLGI
) |
512 (1ULL << INTERCEPT_SKINIT
);
514 control
->iopm_base_pa
= iopm_base
;
515 control
->msrpm_base_pa
= msrpm_base
;
517 control
->tsc_offset
= -tsc
;
518 control
->int_ctl
= V_INTR_MASKING_MASK
;
526 save
->cs
.selector
= 0xf000;
527 /* Executable/Readable Code Segment */
528 save
->cs
.attrib
= SVM_SELECTOR_READ_MASK
| SVM_SELECTOR_P_MASK
|
529 SVM_SELECTOR_S_MASK
| SVM_SELECTOR_CODE_MASK
;
530 save
->cs
.limit
= 0xffff;
531 save
->cs
.base
= 0xffff0000;
533 save
->gdtr
.limit
= 0xffff;
534 save
->idtr
.limit
= 0xffff;
536 init_sys_seg(&save
->ldtr
, SEG_TYPE_LDT
);
537 init_sys_seg(&save
->tr
, SEG_TYPE_BUSY_TSS16
);
539 save
->efer
= MSR_EFER_SVME_MASK
;
541 save
->dr6
= 0xffff0ff0;
544 save
->rip
= 0x0000fff0;
547 * cr0 val on cpu init should be 0x60000010, we enable cpu
548 * cache by default. the orderly way is to enable cache in bios.
550 save
->cr0
= 0x00000010 | CR0_PG_MASK
;
551 save
->cr4
= CR4_PAE_MASK
;
555 static int svm_create_vcpu(struct kvm_vcpu
*vcpu
)
561 vcpu
->svm
= kzalloc(sizeof *vcpu
->svm
, GFP_KERNEL
);
564 page
= alloc_page(GFP_KERNEL
);
568 vcpu
->svm
->vmcb
= page_address(page
);
569 memset(vcpu
->svm
->vmcb
, 0, PAGE_SIZE
);
570 vcpu
->svm
->vmcb_pa
= page_to_pfn(page
) << PAGE_SHIFT
;
571 vcpu
->svm
->cr0
= 0x00000010;
572 vcpu
->svm
->asid_generation
= 0;
573 memset(vcpu
->svm
->db_regs
, 0, sizeof(vcpu
->svm
->db_regs
));
574 init_vmcb(vcpu
->svm
->vmcb
);
586 static void svm_free_vcpu(struct kvm_vcpu
*vcpu
)
591 __free_page(pfn_to_page(vcpu
->svm
->vmcb_pa
>> PAGE_SHIFT
));
595 static struct kvm_vcpu
*svm_vcpu_load(struct kvm_vcpu
*vcpu
)
601 static void svm_vcpu_put(struct kvm_vcpu
*vcpu
)
606 static void svm_cache_regs(struct kvm_vcpu
*vcpu
)
608 vcpu
->regs
[VCPU_REGS_RAX
] = vcpu
->svm
->vmcb
->save
.rax
;
609 vcpu
->regs
[VCPU_REGS_RSP
] = vcpu
->svm
->vmcb
->save
.rsp
;
610 vcpu
->rip
= vcpu
->svm
->vmcb
->save
.rip
;
613 static void svm_decache_regs(struct kvm_vcpu
*vcpu
)
615 vcpu
->svm
->vmcb
->save
.rax
= vcpu
->regs
[VCPU_REGS_RAX
];
616 vcpu
->svm
->vmcb
->save
.rsp
= vcpu
->regs
[VCPU_REGS_RSP
];
617 vcpu
->svm
->vmcb
->save
.rip
= vcpu
->rip
;
620 static unsigned long svm_get_rflags(struct kvm_vcpu
*vcpu
)
622 return vcpu
->svm
->vmcb
->save
.rflags
;
625 static void svm_set_rflags(struct kvm_vcpu
*vcpu
, unsigned long rflags
)
627 vcpu
->svm
->vmcb
->save
.rflags
= rflags
;
630 static struct vmcb_seg
*svm_seg(struct kvm_vcpu
*vcpu
, int seg
)
632 struct vmcb_save_area
*save
= &vcpu
->svm
->vmcb
->save
;
635 case VCPU_SREG_CS
: return &save
->cs
;
636 case VCPU_SREG_DS
: return &save
->ds
;
637 case VCPU_SREG_ES
: return &save
->es
;
638 case VCPU_SREG_FS
: return &save
->fs
;
639 case VCPU_SREG_GS
: return &save
->gs
;
640 case VCPU_SREG_SS
: return &save
->ss
;
641 case VCPU_SREG_TR
: return &save
->tr
;
642 case VCPU_SREG_LDTR
: return &save
->ldtr
;
648 static u64
svm_get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
650 struct vmcb_seg
*s
= svm_seg(vcpu
, seg
);
655 static void svm_get_segment(struct kvm_vcpu
*vcpu
,
656 struct kvm_segment
*var
, int seg
)
658 struct vmcb_seg
*s
= svm_seg(vcpu
, seg
);
661 var
->limit
= s
->limit
;
662 var
->selector
= s
->selector
;
663 var
->type
= s
->attrib
& SVM_SELECTOR_TYPE_MASK
;
664 var
->s
= (s
->attrib
>> SVM_SELECTOR_S_SHIFT
) & 1;
665 var
->dpl
= (s
->attrib
>> SVM_SELECTOR_DPL_SHIFT
) & 3;
666 var
->present
= (s
->attrib
>> SVM_SELECTOR_P_SHIFT
) & 1;
667 var
->avl
= (s
->attrib
>> SVM_SELECTOR_AVL_SHIFT
) & 1;
668 var
->l
= (s
->attrib
>> SVM_SELECTOR_L_SHIFT
) & 1;
669 var
->db
= (s
->attrib
>> SVM_SELECTOR_DB_SHIFT
) & 1;
670 var
->g
= (s
->attrib
>> SVM_SELECTOR_G_SHIFT
) & 1;
671 var
->unusable
= !var
->present
;
674 static void svm_get_cs_db_l_bits(struct kvm_vcpu
*vcpu
, int *db
, int *l
)
676 struct vmcb_seg
*s
= svm_seg(vcpu
, VCPU_SREG_CS
);
678 *db
= (s
->attrib
>> SVM_SELECTOR_DB_SHIFT
) & 1;
679 *l
= (s
->attrib
>> SVM_SELECTOR_L_SHIFT
) & 1;
682 static void svm_get_idt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
684 dt
->limit
= vcpu
->svm
->vmcb
->save
.idtr
.limit
;
685 dt
->base
= vcpu
->svm
->vmcb
->save
.idtr
.base
;
688 static void svm_set_idt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
690 vcpu
->svm
->vmcb
->save
.idtr
.limit
= dt
->limit
;
691 vcpu
->svm
->vmcb
->save
.idtr
.base
= dt
->base
;
694 static void svm_get_gdt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
696 dt
->limit
= vcpu
->svm
->vmcb
->save
.gdtr
.limit
;
697 dt
->base
= vcpu
->svm
->vmcb
->save
.gdtr
.base
;
700 static void svm_set_gdt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
702 vcpu
->svm
->vmcb
->save
.gdtr
.limit
= dt
->limit
;
703 vcpu
->svm
->vmcb
->save
.gdtr
.base
= dt
->base
;
706 static void svm_decache_cr0_cr4_guest_bits(struct kvm_vcpu
*vcpu
)
710 static void svm_set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
713 if (vcpu
->shadow_efer
& KVM_EFER_LME
) {
714 if (!is_paging(vcpu
) && (cr0
& CR0_PG_MASK
)) {
715 vcpu
->shadow_efer
|= KVM_EFER_LMA
;
716 vcpu
->svm
->vmcb
->save
.efer
|= KVM_EFER_LMA
| KVM_EFER_LME
;
719 if (is_paging(vcpu
) && !(cr0
& CR0_PG_MASK
) ) {
720 vcpu
->shadow_efer
&= ~KVM_EFER_LMA
;
721 vcpu
->svm
->vmcb
->save
.efer
&= ~(KVM_EFER_LMA
| KVM_EFER_LME
);
725 vcpu
->svm
->cr0
= cr0
;
726 vcpu
->svm
->vmcb
->save
.cr0
= cr0
| CR0_PG_MASK
;
730 static void svm_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
733 vcpu
->svm
->vmcb
->save
.cr4
= cr4
| CR4_PAE_MASK
;
736 static void svm_set_segment(struct kvm_vcpu
*vcpu
,
737 struct kvm_segment
*var
, int seg
)
739 struct vmcb_seg
*s
= svm_seg(vcpu
, seg
);
742 s
->limit
= var
->limit
;
743 s
->selector
= var
->selector
;
747 s
->attrib
= (var
->type
& SVM_SELECTOR_TYPE_MASK
);
748 s
->attrib
|= (var
->s
& 1) << SVM_SELECTOR_S_SHIFT
;
749 s
->attrib
|= (var
->dpl
& 3) << SVM_SELECTOR_DPL_SHIFT
;
750 s
->attrib
|= (var
->present
& 1) << SVM_SELECTOR_P_SHIFT
;
751 s
->attrib
|= (var
->avl
& 1) << SVM_SELECTOR_AVL_SHIFT
;
752 s
->attrib
|= (var
->l
& 1) << SVM_SELECTOR_L_SHIFT
;
753 s
->attrib
|= (var
->db
& 1) << SVM_SELECTOR_DB_SHIFT
;
754 s
->attrib
|= (var
->g
& 1) << SVM_SELECTOR_G_SHIFT
;
756 if (seg
== VCPU_SREG_CS
)
757 vcpu
->svm
->vmcb
->save
.cpl
758 = (vcpu
->svm
->vmcb
->save
.cs
.attrib
759 >> SVM_SELECTOR_DPL_SHIFT
) & 3;
765 vcpu->svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
766 vcpu->svm->vmcb->control.int_ctl |= (sregs->cr8 & V_TPR_MASK);
770 static int svm_guest_debug(struct kvm_vcpu
*vcpu
, struct kvm_debug_guest
*dbg
)
775 static void load_host_msrs(struct kvm_vcpu
*vcpu
)
779 for ( i
= 0; i
< NR_HOST_SAVE_MSRS
; i
++)
780 wrmsrl(host_save_msrs
[i
], vcpu
->svm
->host_msrs
[i
]);
783 static void save_host_msrs(struct kvm_vcpu
*vcpu
)
787 for ( i
= 0; i
< NR_HOST_SAVE_MSRS
; i
++)
788 rdmsrl(host_save_msrs
[i
], vcpu
->svm
->host_msrs
[i
]);
791 static void new_asid(struct kvm_vcpu
*vcpu
, struct svm_cpu_data
*svm_data
)
793 if (svm_data
->next_asid
> svm_data
->max_asid
) {
794 ++svm_data
->asid_generation
;
795 svm_data
->next_asid
= 1;
796 vcpu
->svm
->vmcb
->control
.tlb_ctl
= TLB_CONTROL_FLUSH_ALL_ASID
;
799 vcpu
->cpu
= svm_data
->cpu
;
800 vcpu
->svm
->asid_generation
= svm_data
->asid_generation
;
801 vcpu
->svm
->vmcb
->control
.asid
= svm_data
->next_asid
++;
804 static void svm_invlpg(struct kvm_vcpu
*vcpu
, gva_t address
)
806 invlpga(address
, vcpu
->svm
->vmcb
->control
.asid
); // is needed?
809 static unsigned long svm_get_dr(struct kvm_vcpu
*vcpu
, int dr
)
811 return vcpu
->svm
->db_regs
[dr
];
814 static void svm_set_dr(struct kvm_vcpu
*vcpu
, int dr
, unsigned long value
,
819 if (vcpu
->svm
->vmcb
->save
.dr7
& DR7_GD_MASK
) {
820 vcpu
->svm
->vmcb
->save
.dr7
&= ~DR7_GD_MASK
;
821 vcpu
->svm
->vmcb
->save
.dr6
|= DR6_BD_MASK
;
822 *exception
= DB_VECTOR
;
828 vcpu
->svm
->db_regs
[dr
] = value
;
831 if (vcpu
->cr4
& CR4_DE_MASK
) {
832 *exception
= UD_VECTOR
;
836 if (value
& ~((1ULL << 32) - 1)) {
837 *exception
= GP_VECTOR
;
840 vcpu
->svm
->vmcb
->save
.dr7
= value
;
844 printk(KERN_DEBUG
"%s: unexpected dr %u\n",
846 *exception
= UD_VECTOR
;
851 static int pf_interception(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
853 u32 exit_int_info
= vcpu
->svm
->vmcb
->control
.exit_int_info
;
856 enum emulation_result er
;
859 if (is_external_interrupt(exit_int_info
))
860 push_irq(vcpu
, exit_int_info
& SVM_EVTINJ_VEC_MASK
);
862 spin_lock(&vcpu
->kvm
->lock
);
864 fault_address
= vcpu
->svm
->vmcb
->control
.exit_info_2
;
865 error_code
= vcpu
->svm
->vmcb
->control
.exit_info_1
;
866 r
= kvm_mmu_page_fault(vcpu
, fault_address
, error_code
);
868 spin_unlock(&vcpu
->kvm
->lock
);
872 spin_unlock(&vcpu
->kvm
->lock
);
875 er
= emulate_instruction(vcpu
, kvm_run
, fault_address
, error_code
);
876 spin_unlock(&vcpu
->kvm
->lock
);
881 case EMULATE_DO_MMIO
:
882 ++kvm_stat
.mmio_exits
;
883 kvm_run
->exit_reason
= KVM_EXIT_MMIO
;
886 vcpu_printf(vcpu
, "%s: emulate fail\n", __FUNCTION__
);
892 kvm_run
->exit_reason
= KVM_EXIT_UNKNOWN
;
896 static int shutdown_interception(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
899 * VMCB is undefined after a SHUTDOWN intercept
900 * so reinitialize it.
902 memset(vcpu
->svm
->vmcb
, 0, PAGE_SIZE
);
903 init_vmcb(vcpu
->svm
->vmcb
);
905 kvm_run
->exit_reason
= KVM_EXIT_SHUTDOWN
;
909 static int io_get_override(struct kvm_vcpu
*vcpu
,
910 struct vmcb_seg
**seg
,
913 u8 inst
[MAX_INST_SIZE
];
918 rip
= vcpu
->svm
->vmcb
->save
.rip
;
919 ins_length
= vcpu
->svm
->next_rip
- rip
;
920 rip
+= vcpu
->svm
->vmcb
->save
.cs
.base
;
922 if (ins_length
> MAX_INST_SIZE
)
924 "%s: inst length err, cs base 0x%llx rip 0x%llx "
925 "next rip 0x%llx ins_length %u\n",
927 vcpu
->svm
->vmcb
->save
.cs
.base
,
928 vcpu
->svm
->vmcb
->save
.rip
,
929 vcpu
->svm
->vmcb
->control
.exit_info_2
,
932 if (kvm_read_guest(vcpu
, rip
, ins_length
, inst
) != ins_length
)
938 for (i
= 0; i
< ins_length
; i
++)
949 *seg
= &vcpu
->svm
->vmcb
->save
.cs
;
952 *seg
= &vcpu
->svm
->vmcb
->save
.ss
;
955 *seg
= &vcpu
->svm
->vmcb
->save
.ds
;
958 *seg
= &vcpu
->svm
->vmcb
->save
.es
;
961 *seg
= &vcpu
->svm
->vmcb
->save
.fs
;
964 *seg
= &vcpu
->svm
->vmcb
->save
.gs
;
969 printk(KERN_DEBUG
"%s: unexpected\n", __FUNCTION__
);
973 static unsigned long io_adress(struct kvm_vcpu
*vcpu
, int ins
, u64
*address
)
975 unsigned long addr_mask
;
977 struct vmcb_seg
*seg
;
979 struct vmcb_save_area
*save_area
= &vcpu
->svm
->vmcb
->save
;
980 u16 cs_attrib
= save_area
->cs
.attrib
;
981 unsigned addr_size
= get_addr_size(vcpu
);
983 if (!io_get_override(vcpu
, &seg
, &addr_override
))
987 addr_size
= (addr_size
== 2) ? 4: (addr_size
>> 1);
990 reg
= &vcpu
->regs
[VCPU_REGS_RDI
];
991 seg
= &vcpu
->svm
->vmcb
->save
.es
;
993 reg
= &vcpu
->regs
[VCPU_REGS_RSI
];
994 seg
= (seg
) ? seg
: &vcpu
->svm
->vmcb
->save
.ds
;
997 addr_mask
= ~0ULL >> (64 - (addr_size
* 8));
999 if ((cs_attrib
& SVM_SELECTOR_L_MASK
) &&
1000 !(vcpu
->svm
->vmcb
->save
.rflags
& X86_EFLAGS_VM
)) {
1001 *address
= (*reg
& addr_mask
);
1005 if (!(seg
->attrib
& SVM_SELECTOR_P_SHIFT
)) {
1006 svm_inject_gp(vcpu
, 0);
1010 *address
= (*reg
& addr_mask
) + seg
->base
;
1014 static int io_interception(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1016 u32 io_info
= vcpu
->svm
->vmcb
->control
.exit_info_1
; //address size bug?
1017 int _in
= io_info
& SVM_IOIO_TYPE_MASK
;
1019 ++kvm_stat
.io_exits
;
1021 vcpu
->svm
->next_rip
= vcpu
->svm
->vmcb
->control
.exit_info_2
;
1023 kvm_run
->exit_reason
= KVM_EXIT_IO
;
1024 kvm_run
->io
.port
= io_info
>> 16;
1025 kvm_run
->io
.direction
= (_in
) ? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
1026 kvm_run
->io
.size
= ((io_info
& SVM_IOIO_SIZE_MASK
) >> SVM_IOIO_SIZE_SHIFT
);
1027 kvm_run
->io
.string
= (io_info
& SVM_IOIO_STR_MASK
) != 0;
1028 kvm_run
->io
.rep
= (io_info
& SVM_IOIO_REP_MASK
) != 0;
1030 if (kvm_run
->io
.string
) {
1033 addr_mask
= io_adress(vcpu
, _in
, &kvm_run
->io
.address
);
1035 printk(KERN_DEBUG
"%s: get io address failed\n", __FUNCTION__
);
1039 if (kvm_run
->io
.rep
) {
1040 kvm_run
->io
.count
= vcpu
->regs
[VCPU_REGS_RCX
] & addr_mask
;
1041 kvm_run
->io
.string_down
= (vcpu
->svm
->vmcb
->save
.rflags
1042 & X86_EFLAGS_DF
) != 0;
1045 kvm_run
->io
.value
= vcpu
->svm
->vmcb
->save
.rax
;
1051 static int nop_on_interception(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1056 static int halt_interception(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1058 vcpu
->svm
->next_rip
= vcpu
->svm
->vmcb
->save
.rip
+ 1;
1059 skip_emulated_instruction(vcpu
);
1060 if (vcpu
->irq_summary
)
1063 kvm_run
->exit_reason
= KVM_EXIT_HLT
;
1064 ++kvm_stat
.halt_exits
;
1068 static int invalid_op_interception(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1074 static int task_switch_interception(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1076 printk(KERN_DEBUG
"%s: task swiche is unsupported\n", __FUNCTION__
);
1077 kvm_run
->exit_reason
= KVM_EXIT_UNKNOWN
;
1081 static int cpuid_interception(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1083 vcpu
->svm
->next_rip
= vcpu
->svm
->vmcb
->save
.rip
+ 2;
1084 kvm_run
->exit_reason
= KVM_EXIT_CPUID
;
1088 static int emulate_on_interception(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1090 if (emulate_instruction(vcpu
, 0, 0, 0) != EMULATE_DONE
)
1091 printk(KERN_ERR
"%s: failed\n", __FUNCTION__
);
1095 static int svm_get_msr(struct kvm_vcpu
*vcpu
, unsigned ecx
, u64
*data
)
1098 case MSR_IA32_TIME_STAMP_COUNTER
: {
1102 *data
= vcpu
->svm
->vmcb
->control
.tsc_offset
+ tsc
;
1106 *data
= vcpu
->svm
->vmcb
->save
.star
;
1108 #ifdef CONFIG_X86_64
1110 *data
= vcpu
->svm
->vmcb
->save
.lstar
;
1113 *data
= vcpu
->svm
->vmcb
->save
.cstar
;
1115 case MSR_KERNEL_GS_BASE
:
1116 *data
= vcpu
->svm
->vmcb
->save
.kernel_gs_base
;
1118 case MSR_SYSCALL_MASK
:
1119 *data
= vcpu
->svm
->vmcb
->save
.sfmask
;
1122 case MSR_IA32_SYSENTER_CS
:
1123 *data
= vcpu
->svm
->vmcb
->save
.sysenter_cs
;
1125 case MSR_IA32_SYSENTER_EIP
:
1126 *data
= vcpu
->svm
->vmcb
->save
.sysenter_eip
;
1128 case MSR_IA32_SYSENTER_ESP
:
1129 *data
= vcpu
->svm
->vmcb
->save
.sysenter_esp
;
1132 return kvm_get_msr_common(vcpu
, ecx
, data
);
1137 static int rdmsr_interception(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1139 u32 ecx
= vcpu
->regs
[VCPU_REGS_RCX
];
1142 if (svm_get_msr(vcpu
, ecx
, &data
))
1143 svm_inject_gp(vcpu
, 0);
1145 vcpu
->svm
->vmcb
->save
.rax
= data
& 0xffffffff;
1146 vcpu
->regs
[VCPU_REGS_RDX
] = data
>> 32;
1147 vcpu
->svm
->next_rip
= vcpu
->svm
->vmcb
->save
.rip
+ 2;
1148 skip_emulated_instruction(vcpu
);
1153 static int svm_set_msr(struct kvm_vcpu
*vcpu
, unsigned ecx
, u64 data
)
1156 case MSR_IA32_TIME_STAMP_COUNTER
: {
1160 vcpu
->svm
->vmcb
->control
.tsc_offset
= data
- tsc
;
1164 vcpu
->svm
->vmcb
->save
.star
= data
;
1166 #ifdef CONFIG_X86_64
1168 vcpu
->svm
->vmcb
->save
.lstar
= data
;
1171 vcpu
->svm
->vmcb
->save
.cstar
= data
;
1173 case MSR_KERNEL_GS_BASE
:
1174 vcpu
->svm
->vmcb
->save
.kernel_gs_base
= data
;
1176 case MSR_SYSCALL_MASK
:
1177 vcpu
->svm
->vmcb
->save
.sfmask
= data
;
1180 case MSR_IA32_SYSENTER_CS
:
1181 vcpu
->svm
->vmcb
->save
.sysenter_cs
= data
;
1183 case MSR_IA32_SYSENTER_EIP
:
1184 vcpu
->svm
->vmcb
->save
.sysenter_eip
= data
;
1186 case MSR_IA32_SYSENTER_ESP
:
1187 vcpu
->svm
->vmcb
->save
.sysenter_esp
= data
;
1190 return kvm_set_msr_common(vcpu
, ecx
, data
);
1195 static int wrmsr_interception(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1197 u32 ecx
= vcpu
->regs
[VCPU_REGS_RCX
];
1198 u64 data
= (vcpu
->svm
->vmcb
->save
.rax
& -1u)
1199 | ((u64
)(vcpu
->regs
[VCPU_REGS_RDX
] & -1u) << 32);
1200 vcpu
->svm
->next_rip
= vcpu
->svm
->vmcb
->save
.rip
+ 2;
1201 if (svm_set_msr(vcpu
, ecx
, data
))
1202 svm_inject_gp(vcpu
, 0);
1204 skip_emulated_instruction(vcpu
);
1208 static int msr_interception(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1210 if (vcpu
->svm
->vmcb
->control
.exit_info_1
)
1211 return wrmsr_interception(vcpu
, kvm_run
);
1213 return rdmsr_interception(vcpu
, kvm_run
);
1216 static int interrupt_window_interception(struct kvm_vcpu
*vcpu
,
1217 struct kvm_run
*kvm_run
)
1220 * If the user space waits to inject interrupts, exit as soon as
1223 if (kvm_run
->request_interrupt_window
&&
1224 !vcpu
->irq_summary
) {
1225 ++kvm_stat
.irq_window_exits
;
1226 kvm_run
->exit_reason
= KVM_EXIT_IRQ_WINDOW_OPEN
;
1233 static int (*svm_exit_handlers
[])(struct kvm_vcpu
*vcpu
,
1234 struct kvm_run
*kvm_run
) = {
1235 [SVM_EXIT_READ_CR0
] = emulate_on_interception
,
1236 [SVM_EXIT_READ_CR3
] = emulate_on_interception
,
1237 [SVM_EXIT_READ_CR4
] = emulate_on_interception
,
1239 [SVM_EXIT_WRITE_CR0
] = emulate_on_interception
,
1240 [SVM_EXIT_WRITE_CR3
] = emulate_on_interception
,
1241 [SVM_EXIT_WRITE_CR4
] = emulate_on_interception
,
1242 [SVM_EXIT_READ_DR0
] = emulate_on_interception
,
1243 [SVM_EXIT_READ_DR1
] = emulate_on_interception
,
1244 [SVM_EXIT_READ_DR2
] = emulate_on_interception
,
1245 [SVM_EXIT_READ_DR3
] = emulate_on_interception
,
1246 [SVM_EXIT_WRITE_DR0
] = emulate_on_interception
,
1247 [SVM_EXIT_WRITE_DR1
] = emulate_on_interception
,
1248 [SVM_EXIT_WRITE_DR2
] = emulate_on_interception
,
1249 [SVM_EXIT_WRITE_DR3
] = emulate_on_interception
,
1250 [SVM_EXIT_WRITE_DR5
] = emulate_on_interception
,
1251 [SVM_EXIT_WRITE_DR7
] = emulate_on_interception
,
1252 [SVM_EXIT_EXCP_BASE
+ PF_VECTOR
] = pf_interception
,
1253 [SVM_EXIT_INTR
] = nop_on_interception
,
1254 [SVM_EXIT_NMI
] = nop_on_interception
,
1255 [SVM_EXIT_SMI
] = nop_on_interception
,
1256 [SVM_EXIT_INIT
] = nop_on_interception
,
1257 [SVM_EXIT_VINTR
] = interrupt_window_interception
,
1258 /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */
1259 [SVM_EXIT_CPUID
] = cpuid_interception
,
1260 [SVM_EXIT_HLT
] = halt_interception
,
1261 [SVM_EXIT_INVLPG
] = emulate_on_interception
,
1262 [SVM_EXIT_INVLPGA
] = invalid_op_interception
,
1263 [SVM_EXIT_IOIO
] = io_interception
,
1264 [SVM_EXIT_MSR
] = msr_interception
,
1265 [SVM_EXIT_TASK_SWITCH
] = task_switch_interception
,
1266 [SVM_EXIT_SHUTDOWN
] = shutdown_interception
,
1267 [SVM_EXIT_VMRUN
] = invalid_op_interception
,
1268 [SVM_EXIT_VMMCALL
] = invalid_op_interception
,
1269 [SVM_EXIT_VMLOAD
] = invalid_op_interception
,
1270 [SVM_EXIT_VMSAVE
] = invalid_op_interception
,
1271 [SVM_EXIT_STGI
] = invalid_op_interception
,
1272 [SVM_EXIT_CLGI
] = invalid_op_interception
,
1273 [SVM_EXIT_SKINIT
] = invalid_op_interception
,
1277 static int handle_exit(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1279 u32 exit_code
= vcpu
->svm
->vmcb
->control
.exit_code
;
1281 kvm_run
->exit_type
= KVM_EXIT_TYPE_VM_EXIT
;
1283 if (is_external_interrupt(vcpu
->svm
->vmcb
->control
.exit_int_info
) &&
1284 exit_code
!= SVM_EXIT_EXCP_BASE
+ PF_VECTOR
)
1285 printk(KERN_ERR
"%s: unexpected exit_ini_info 0x%x "
1287 __FUNCTION__
, vcpu
->svm
->vmcb
->control
.exit_int_info
,
1290 if (exit_code
>= sizeof(svm_exit_handlers
) / sizeof(*svm_exit_handlers
)
1291 || svm_exit_handlers
[exit_code
] == 0) {
1292 kvm_run
->exit_reason
= KVM_EXIT_UNKNOWN
;
1293 printk(KERN_ERR
"%s: 0x%x @ 0x%llx cr0 0x%lx rflags 0x%llx\n",
1296 vcpu
->svm
->vmcb
->save
.rip
,
1298 vcpu
->svm
->vmcb
->save
.rflags
);
1302 return svm_exit_handlers
[exit_code
](vcpu
, kvm_run
);
1305 static void reload_tss(struct kvm_vcpu
*vcpu
)
1307 int cpu
= raw_smp_processor_id();
1309 struct svm_cpu_data
*svm_data
= per_cpu(svm_data
, cpu
);
1310 svm_data
->tss_desc
->type
= 9; //available 32/64-bit TSS
1314 static void pre_svm_run(struct kvm_vcpu
*vcpu
)
1316 int cpu
= raw_smp_processor_id();
1318 struct svm_cpu_data
*svm_data
= per_cpu(svm_data
, cpu
);
1320 vcpu
->svm
->vmcb
->control
.tlb_ctl
= TLB_CONTROL_DO_NOTHING
;
1321 if (vcpu
->cpu
!= cpu
||
1322 vcpu
->svm
->asid_generation
!= svm_data
->asid_generation
)
1323 new_asid(vcpu
, svm_data
);
1327 static inline void kvm_do_inject_irq(struct kvm_vcpu
*vcpu
)
1329 struct vmcb_control_area
*control
;
1331 control
= &vcpu
->svm
->vmcb
->control
;
1332 control
->int_vector
= pop_irq(vcpu
);
1333 control
->int_ctl
&= ~V_INTR_PRIO_MASK
;
1334 control
->int_ctl
|= V_IRQ_MASK
|
1335 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT
);
1338 static void kvm_reput_irq(struct kvm_vcpu
*vcpu
)
1340 struct vmcb_control_area
*control
= &vcpu
->svm
->vmcb
->control
;
1342 if (control
->int_ctl
& V_IRQ_MASK
) {
1343 control
->int_ctl
&= ~V_IRQ_MASK
;
1344 push_irq(vcpu
, control
->int_vector
);
1347 vcpu
->interrupt_window_open
=
1348 !(control
->int_state
& SVM_INTERRUPT_SHADOW_MASK
);
1351 static void do_interrupt_requests(struct kvm_vcpu
*vcpu
,
1352 struct kvm_run
*kvm_run
)
1354 struct vmcb_control_area
*control
= &vcpu
->svm
->vmcb
->control
;
1356 vcpu
->interrupt_window_open
=
1357 (!(control
->int_state
& SVM_INTERRUPT_SHADOW_MASK
) &&
1358 (vcpu
->svm
->vmcb
->save
.rflags
& X86_EFLAGS_IF
));
1360 if (vcpu
->interrupt_window_open
&& vcpu
->irq_summary
)
1362 * If interrupts enabled, and not blocked by sti or mov ss. Good.
1364 kvm_do_inject_irq(vcpu
);
1367 * Interrupts blocked. Wait for unblock.
1369 if (!vcpu
->interrupt_window_open
&&
1370 (vcpu
->irq_summary
|| kvm_run
->request_interrupt_window
)) {
1371 control
->intercept
|= 1ULL << INTERCEPT_VINTR
;
1373 control
->intercept
&= ~(1ULL << INTERCEPT_VINTR
);
1376 static void post_kvm_run_save(struct kvm_vcpu
*vcpu
,
1377 struct kvm_run
*kvm_run
)
1379 kvm_run
->ready_for_interrupt_injection
= (vcpu
->interrupt_window_open
&&
1380 vcpu
->irq_summary
== 0);
1381 kvm_run
->if_flag
= (vcpu
->svm
->vmcb
->save
.rflags
& X86_EFLAGS_IF
) != 0;
1382 kvm_run
->cr8
= vcpu
->cr8
;
1383 kvm_run
->apic_base
= vcpu
->apic_base
;
1387 * Check if userspace requested an interrupt window, and that the
1388 * interrupt window is open.
1390 * No need to exit to userspace if we already have an interrupt queued.
1392 static int dm_request_for_irq_injection(struct kvm_vcpu
*vcpu
,
1393 struct kvm_run
*kvm_run
)
1395 return (!vcpu
->irq_summary
&&
1396 kvm_run
->request_interrupt_window
&&
1397 vcpu
->interrupt_window_open
&&
1398 (vcpu
->svm
->vmcb
->save
.rflags
& X86_EFLAGS_IF
));
1401 static void save_db_regs(unsigned long *db_regs
)
1403 asm volatile ("mov %%dr0, %0" : "=r"(db_regs
[0]));
1404 asm volatile ("mov %%dr1, %0" : "=r"(db_regs
[1]));
1405 asm volatile ("mov %%dr2, %0" : "=r"(db_regs
[2]));
1406 asm volatile ("mov %%dr3, %0" : "=r"(db_regs
[3]));
1409 static void load_db_regs(unsigned long *db_regs
)
1411 asm volatile ("mov %0, %%dr0" : : "r"(db_regs
[0]));
1412 asm volatile ("mov %0, %%dr1" : : "r"(db_regs
[1]));
1413 asm volatile ("mov %0, %%dr2" : : "r"(db_regs
[2]));
1414 asm volatile ("mov %0, %%dr3" : : "r"(db_regs
[3]));
1417 static int svm_vcpu_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1425 if (!vcpu
->mmio_read_completed
)
1426 do_interrupt_requests(vcpu
, kvm_run
);
1432 save_host_msrs(vcpu
);
1433 fs_selector
= read_fs();
1434 gs_selector
= read_gs();
1435 ldt_selector
= read_ldt();
1436 vcpu
->svm
->host_cr2
= kvm_read_cr2();
1437 vcpu
->svm
->host_dr6
= read_dr6();
1438 vcpu
->svm
->host_dr7
= read_dr7();
1439 vcpu
->svm
->vmcb
->save
.cr2
= vcpu
->cr2
;
1441 if (vcpu
->svm
->vmcb
->save
.dr7
& 0xff) {
1443 save_db_regs(vcpu
->svm
->host_db_regs
);
1444 load_db_regs(vcpu
->svm
->db_regs
);
1447 fx_save(vcpu
->host_fx_image
);
1448 fx_restore(vcpu
->guest_fx_image
);
1451 #ifdef CONFIG_X86_64
1452 "push %%rbx; push %%rcx; push %%rdx;"
1453 "push %%rsi; push %%rdi; push %%rbp;"
1454 "push %%r8; push %%r9; push %%r10; push %%r11;"
1455 "push %%r12; push %%r13; push %%r14; push %%r15;"
1457 "push %%ebx; push %%ecx; push %%edx;"
1458 "push %%esi; push %%edi; push %%ebp;"
1461 #ifdef CONFIG_X86_64
1462 "mov %c[rbx](%[vcpu]), %%rbx \n\t"
1463 "mov %c[rcx](%[vcpu]), %%rcx \n\t"
1464 "mov %c[rdx](%[vcpu]), %%rdx \n\t"
1465 "mov %c[rsi](%[vcpu]), %%rsi \n\t"
1466 "mov %c[rdi](%[vcpu]), %%rdi \n\t"
1467 "mov %c[rbp](%[vcpu]), %%rbp \n\t"
1468 "mov %c[r8](%[vcpu]), %%r8 \n\t"
1469 "mov %c[r9](%[vcpu]), %%r9 \n\t"
1470 "mov %c[r10](%[vcpu]), %%r10 \n\t"
1471 "mov %c[r11](%[vcpu]), %%r11 \n\t"
1472 "mov %c[r12](%[vcpu]), %%r12 \n\t"
1473 "mov %c[r13](%[vcpu]), %%r13 \n\t"
1474 "mov %c[r14](%[vcpu]), %%r14 \n\t"
1475 "mov %c[r15](%[vcpu]), %%r15 \n\t"
1477 "mov %c[rbx](%[vcpu]), %%ebx \n\t"
1478 "mov %c[rcx](%[vcpu]), %%ecx \n\t"
1479 "mov %c[rdx](%[vcpu]), %%edx \n\t"
1480 "mov %c[rsi](%[vcpu]), %%esi \n\t"
1481 "mov %c[rdi](%[vcpu]), %%edi \n\t"
1482 "mov %c[rbp](%[vcpu]), %%ebp \n\t"
1485 #ifdef CONFIG_X86_64
1486 /* Enter guest mode */
1488 "mov %c[svm](%[vcpu]), %%rax \n\t"
1489 "mov %c[vmcb](%%rax), %%rax \n\t"
1495 /* Enter guest mode */
1497 "mov %c[svm](%[vcpu]), %%eax \n\t"
1498 "mov %c[vmcb](%%eax), %%eax \n\t"
1505 /* Save guest registers, load host registers */
1506 #ifdef CONFIG_X86_64
1507 "mov %%rbx, %c[rbx](%[vcpu]) \n\t"
1508 "mov %%rcx, %c[rcx](%[vcpu]) \n\t"
1509 "mov %%rdx, %c[rdx](%[vcpu]) \n\t"
1510 "mov %%rsi, %c[rsi](%[vcpu]) \n\t"
1511 "mov %%rdi, %c[rdi](%[vcpu]) \n\t"
1512 "mov %%rbp, %c[rbp](%[vcpu]) \n\t"
1513 "mov %%r8, %c[r8](%[vcpu]) \n\t"
1514 "mov %%r9, %c[r9](%[vcpu]) \n\t"
1515 "mov %%r10, %c[r10](%[vcpu]) \n\t"
1516 "mov %%r11, %c[r11](%[vcpu]) \n\t"
1517 "mov %%r12, %c[r12](%[vcpu]) \n\t"
1518 "mov %%r13, %c[r13](%[vcpu]) \n\t"
1519 "mov %%r14, %c[r14](%[vcpu]) \n\t"
1520 "mov %%r15, %c[r15](%[vcpu]) \n\t"
1522 "pop %%r15; pop %%r14; pop %%r13; pop %%r12;"
1523 "pop %%r11; pop %%r10; pop %%r9; pop %%r8;"
1524 "pop %%rbp; pop %%rdi; pop %%rsi;"
1525 "pop %%rdx; pop %%rcx; pop %%rbx; \n\t"
1527 "mov %%ebx, %c[rbx](%[vcpu]) \n\t"
1528 "mov %%ecx, %c[rcx](%[vcpu]) \n\t"
1529 "mov %%edx, %c[rdx](%[vcpu]) \n\t"
1530 "mov %%esi, %c[rsi](%[vcpu]) \n\t"
1531 "mov %%edi, %c[rdi](%[vcpu]) \n\t"
1532 "mov %%ebp, %c[rbp](%[vcpu]) \n\t"
1534 "pop %%ebp; pop %%edi; pop %%esi;"
1535 "pop %%edx; pop %%ecx; pop %%ebx; \n\t"
1539 [svm
]"i"(offsetof(struct kvm_vcpu
, svm
)),
1540 [vmcb
]"i"(offsetof(struct vcpu_svm
, vmcb_pa
)),
1541 [rbx
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_RBX
])),
1542 [rcx
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_RCX
])),
1543 [rdx
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_RDX
])),
1544 [rsi
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_RSI
])),
1545 [rdi
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_RDI
])),
1546 [rbp
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_RBP
]))
1547 #ifdef CONFIG_X86_64
1548 ,[r8
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_R8
])),
1549 [r9
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_R9
])),
1550 [r10
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_R10
])),
1551 [r11
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_R11
])),
1552 [r12
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_R12
])),
1553 [r13
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_R13
])),
1554 [r14
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_R14
])),
1555 [r15
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_R15
]))
1559 fx_save(vcpu
->guest_fx_image
);
1560 fx_restore(vcpu
->host_fx_image
);
1562 if ((vcpu
->svm
->vmcb
->save
.dr7
& 0xff))
1563 load_db_regs(vcpu
->svm
->host_db_regs
);
1565 vcpu
->cr2
= vcpu
->svm
->vmcb
->save
.cr2
;
1567 write_dr6(vcpu
->svm
->host_dr6
);
1568 write_dr7(vcpu
->svm
->host_dr7
);
1569 kvm_write_cr2(vcpu
->svm
->host_cr2
);
1571 load_fs(fs_selector
);
1572 load_gs(gs_selector
);
1573 load_ldt(ldt_selector
);
1574 load_host_msrs(vcpu
);
1579 * Profile KVM exit RIPs:
1581 if (unlikely(prof_on
== KVM_PROFILING
))
1582 profile_hit(KVM_PROFILING
,
1583 (void *)(unsigned long)vcpu
->svm
->vmcb
->save
.rip
);
1587 kvm_reput_irq(vcpu
);
1589 vcpu
->svm
->next_rip
= 0;
1591 if (vcpu
->svm
->vmcb
->control
.exit_code
== SVM_EXIT_ERR
) {
1592 kvm_run
->exit_type
= KVM_EXIT_TYPE_FAIL_ENTRY
;
1593 kvm_run
->exit_reason
= vcpu
->svm
->vmcb
->control
.exit_code
;
1594 post_kvm_run_save(vcpu
, kvm_run
);
1598 r
= handle_exit(vcpu
, kvm_run
);
1600 if (signal_pending(current
)) {
1601 ++kvm_stat
.signal_exits
;
1602 post_kvm_run_save(vcpu
, kvm_run
);
1606 if (dm_request_for_irq_injection(vcpu
, kvm_run
)) {
1607 ++kvm_stat
.request_irq_exits
;
1608 post_kvm_run_save(vcpu
, kvm_run
);
1614 post_kvm_run_save(vcpu
, kvm_run
);
1618 static void svm_flush_tlb(struct kvm_vcpu
*vcpu
)
1620 force_new_asid(vcpu
);
1623 static void svm_set_cr3(struct kvm_vcpu
*vcpu
, unsigned long root
)
1625 vcpu
->svm
->vmcb
->save
.cr3
= root
;
1626 force_new_asid(vcpu
);
1629 static void svm_inject_page_fault(struct kvm_vcpu
*vcpu
,
1633 uint32_t exit_int_info
= vcpu
->svm
->vmcb
->control
.exit_int_info
;
1635 ++kvm_stat
.pf_guest
;
1637 if (is_page_fault(exit_int_info
)) {
1639 vcpu
->svm
->vmcb
->control
.event_inj_err
= 0;
1640 vcpu
->svm
->vmcb
->control
.event_inj
= SVM_EVTINJ_VALID
|
1641 SVM_EVTINJ_VALID_ERR
|
1642 SVM_EVTINJ_TYPE_EXEPT
|
1647 vcpu
->svm
->vmcb
->save
.cr2
= addr
;
1648 vcpu
->svm
->vmcb
->control
.event_inj
= SVM_EVTINJ_VALID
|
1649 SVM_EVTINJ_VALID_ERR
|
1650 SVM_EVTINJ_TYPE_EXEPT
|
1652 vcpu
->svm
->vmcb
->control
.event_inj_err
= err_code
;
1656 static int is_disabled(void)
1661 static struct kvm_arch_ops svm_arch_ops
= {
1662 .cpu_has_kvm_support
= has_svm
,
1663 .disabled_by_bios
= is_disabled
,
1664 .hardware_setup
= svm_hardware_setup
,
1665 .hardware_unsetup
= svm_hardware_unsetup
,
1666 .hardware_enable
= svm_hardware_enable
,
1667 .hardware_disable
= svm_hardware_disable
,
1669 .vcpu_create
= svm_create_vcpu
,
1670 .vcpu_free
= svm_free_vcpu
,
1672 .vcpu_load
= svm_vcpu_load
,
1673 .vcpu_put
= svm_vcpu_put
,
1675 .set_guest_debug
= svm_guest_debug
,
1676 .get_msr
= svm_get_msr
,
1677 .set_msr
= svm_set_msr
,
1678 .get_segment_base
= svm_get_segment_base
,
1679 .get_segment
= svm_get_segment
,
1680 .set_segment
= svm_set_segment
,
1681 .get_cs_db_l_bits
= svm_get_cs_db_l_bits
,
1682 .decache_cr0_cr4_guest_bits
= svm_decache_cr0_cr4_guest_bits
,
1683 .set_cr0
= svm_set_cr0
,
1684 .set_cr0_no_modeswitch
= svm_set_cr0
,
1685 .set_cr3
= svm_set_cr3
,
1686 .set_cr4
= svm_set_cr4
,
1687 .set_efer
= svm_set_efer
,
1688 .get_idt
= svm_get_idt
,
1689 .set_idt
= svm_set_idt
,
1690 .get_gdt
= svm_get_gdt
,
1691 .set_gdt
= svm_set_gdt
,
1692 .get_dr
= svm_get_dr
,
1693 .set_dr
= svm_set_dr
,
1694 .cache_regs
= svm_cache_regs
,
1695 .decache_regs
= svm_decache_regs
,
1696 .get_rflags
= svm_get_rflags
,
1697 .set_rflags
= svm_set_rflags
,
1699 .invlpg
= svm_invlpg
,
1700 .tlb_flush
= svm_flush_tlb
,
1701 .inject_page_fault
= svm_inject_page_fault
,
1703 .inject_gp
= svm_inject_gp
,
1705 .run
= svm_vcpu_run
,
1706 .skip_emulated_instruction
= skip_emulated_instruction
,
1707 .vcpu_setup
= svm_vcpu_setup
,
1710 static int __init
svm_init(void)
1712 return kvm_init_arch(&svm_arch_ops
, THIS_MODULE
);
1715 static void __exit
svm_exit(void)
1720 module_init(svm_init
)
1721 module_exit(svm_exit
)