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.
18 #include "x86_emulate.h"
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/vmalloc.h>
24 #include <linux/highmem.h>
25 #include <linux/sched.h>
29 MODULE_AUTHOR("Qumranet");
30 MODULE_LICENSE("GPL");
32 #define IOPM_ALLOC_ORDER 2
33 #define MSRPM_ALLOC_ORDER 1
39 #define DR7_GD_MASK (1 << 13)
40 #define DR6_BD_MASK (1 << 13)
42 #define SEG_TYPE_LDT 2
43 #define SEG_TYPE_BUSY_TSS16 3
45 #define SVM_FEATURE_NPT (1 << 0)
46 #define SVM_FEATURE_LBRV (1 << 1)
47 #define SVM_DEATURE_SVML (1 << 2)
49 static void kvm_reput_irq(struct vcpu_svm
*svm
);
51 static inline struct vcpu_svm
*to_svm(struct kvm_vcpu
*vcpu
)
53 return container_of(vcpu
, struct vcpu_svm
, vcpu
);
56 unsigned long iopm_base
;
57 unsigned long msrpm_base
;
59 struct kvm_ldttss_desc
{
62 unsigned base1
: 8, type
: 5, dpl
: 2, p
: 1;
63 unsigned limit1
: 4, zero0
: 3, g
: 1, base2
: 8;
66 } __attribute__((packed
));
74 struct kvm_ldttss_desc
*tss_desc
;
76 struct page
*save_area
;
79 static DEFINE_PER_CPU(struct svm_cpu_data
*, svm_data
);
80 static uint32_t svm_features
;
82 struct svm_init_data
{
87 static u32 msrpm_ranges
[] = {0, 0xc0000000, 0xc0010000};
89 #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
90 #define MSRS_RANGE_SIZE 2048
91 #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
93 #define MAX_INST_SIZE 15
95 static inline u32
svm_has(u32 feat
)
97 return svm_features
& feat
;
100 static inline u8
pop_irq(struct kvm_vcpu
*vcpu
)
102 int word_index
= __ffs(vcpu
->irq_summary
);
103 int bit_index
= __ffs(vcpu
->irq_pending
[word_index
]);
104 int irq
= word_index
* BITS_PER_LONG
+ bit_index
;
106 clear_bit(bit_index
, &vcpu
->irq_pending
[word_index
]);
107 if (!vcpu
->irq_pending
[word_index
])
108 clear_bit(word_index
, &vcpu
->irq_summary
);
112 static inline void push_irq(struct kvm_vcpu
*vcpu
, u8 irq
)
114 set_bit(irq
, vcpu
->irq_pending
);
115 set_bit(irq
/ BITS_PER_LONG
, &vcpu
->irq_summary
);
118 static inline void clgi(void)
120 asm volatile (SVM_CLGI
);
123 static inline void stgi(void)
125 asm volatile (SVM_STGI
);
128 static inline void invlpga(unsigned long addr
, u32 asid
)
130 asm volatile (SVM_INVLPGA :: "a"(addr
), "c"(asid
));
133 static inline unsigned long kvm_read_cr2(void)
137 asm volatile ("mov %%cr2, %0" : "=r" (cr2
));
141 static inline void kvm_write_cr2(unsigned long val
)
143 asm volatile ("mov %0, %%cr2" :: "r" (val
));
146 static inline unsigned long read_dr6(void)
150 asm volatile ("mov %%dr6, %0" : "=r" (dr6
));
154 static inline void write_dr6(unsigned long val
)
156 asm volatile ("mov %0, %%dr6" :: "r" (val
));
159 static inline unsigned long read_dr7(void)
163 asm volatile ("mov %%dr7, %0" : "=r" (dr7
));
167 static inline void write_dr7(unsigned long val
)
169 asm volatile ("mov %0, %%dr7" :: "r" (val
));
172 static inline void force_new_asid(struct kvm_vcpu
*vcpu
)
174 to_svm(vcpu
)->asid_generation
--;
177 static inline void flush_guest_tlb(struct kvm_vcpu
*vcpu
)
179 force_new_asid(vcpu
);
182 static void svm_set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
184 if (!(efer
& EFER_LMA
))
187 to_svm(vcpu
)->vmcb
->save
.efer
= efer
| MSR_EFER_SVME_MASK
;
188 vcpu
->shadow_efer
= efer
;
191 static void svm_queue_exception(struct kvm_vcpu
*vcpu
, unsigned nr
,
192 bool has_error_code
, u32 error_code
)
194 struct vcpu_svm
*svm
= to_svm(vcpu
);
196 svm
->vmcb
->control
.event_inj
= nr
198 | (has_error_code
? SVM_EVTINJ_VALID_ERR
: 0)
199 | SVM_EVTINJ_TYPE_EXEPT
;
200 svm
->vmcb
->control
.event_inj_err
= error_code
;
203 static bool svm_exception_injected(struct kvm_vcpu
*vcpu
)
205 struct vcpu_svm
*svm
= to_svm(vcpu
);
207 return !(svm
->vmcb
->control
.exit_int_info
& SVM_EXITINTINFO_VALID
);
210 static void svm_inject_gp(struct kvm_vcpu
*vcpu
, unsigned error_code
)
212 struct vcpu_svm
*svm
= to_svm(vcpu
);
214 svm
->vmcb
->control
.event_inj
= SVM_EVTINJ_VALID
|
215 SVM_EVTINJ_VALID_ERR
|
216 SVM_EVTINJ_TYPE_EXEPT
|
218 svm
->vmcb
->control
.event_inj_err
= error_code
;
221 static void inject_ud(struct kvm_vcpu
*vcpu
)
223 to_svm(vcpu
)->vmcb
->control
.event_inj
= SVM_EVTINJ_VALID
|
224 SVM_EVTINJ_TYPE_EXEPT
|
228 static int is_page_fault(uint32_t info
)
230 info
&= SVM_EVTINJ_VEC_MASK
| SVM_EVTINJ_TYPE_MASK
| SVM_EVTINJ_VALID
;
231 return info
== (PF_VECTOR
| SVM_EVTINJ_VALID
| SVM_EVTINJ_TYPE_EXEPT
);
234 static int is_external_interrupt(u32 info
)
236 info
&= SVM_EVTINJ_TYPE_MASK
| SVM_EVTINJ_VALID
;
237 return info
== (SVM_EVTINJ_VALID
| SVM_EVTINJ_TYPE_INTR
);
240 static void skip_emulated_instruction(struct kvm_vcpu
*vcpu
)
242 struct vcpu_svm
*svm
= to_svm(vcpu
);
244 if (!svm
->next_rip
) {
245 printk(KERN_DEBUG
"%s: NOP\n", __FUNCTION__
);
248 if (svm
->next_rip
- svm
->vmcb
->save
.rip
> MAX_INST_SIZE
)
249 printk(KERN_ERR
"%s: ip 0x%llx next 0x%llx\n",
254 vcpu
->rip
= svm
->vmcb
->save
.rip
= svm
->next_rip
;
255 svm
->vmcb
->control
.int_state
&= ~SVM_INTERRUPT_SHADOW_MASK
;
257 vcpu
->interrupt_window_open
= 1;
260 static int has_svm(void)
262 uint32_t eax
, ebx
, ecx
, edx
;
264 if (boot_cpu_data
.x86_vendor
!= X86_VENDOR_AMD
) {
265 printk(KERN_INFO
"has_svm: not amd\n");
269 cpuid(0x80000000, &eax
, &ebx
, &ecx
, &edx
);
270 if (eax
< SVM_CPUID_FUNC
) {
271 printk(KERN_INFO
"has_svm: can't execute cpuid_8000000a\n");
275 cpuid(0x80000001, &eax
, &ebx
, &ecx
, &edx
);
276 if (!(ecx
& (1 << SVM_CPUID_FEATURE_SHIFT
))) {
277 printk(KERN_DEBUG
"has_svm: svm not available\n");
283 static void svm_hardware_disable(void *garbage
)
285 struct svm_cpu_data
*svm_data
286 = per_cpu(svm_data
, raw_smp_processor_id());
291 wrmsrl(MSR_VM_HSAVE_PA
, 0);
292 rdmsrl(MSR_EFER
, efer
);
293 wrmsrl(MSR_EFER
, efer
& ~MSR_EFER_SVME_MASK
);
294 per_cpu(svm_data
, raw_smp_processor_id()) = NULL
;
295 __free_page(svm_data
->save_area
);
300 static void svm_hardware_enable(void *garbage
)
303 struct svm_cpu_data
*svm_data
;
306 struct desc_ptr gdt_descr
;
308 struct desc_ptr gdt_descr
;
310 struct desc_struct
*gdt
;
311 int me
= raw_smp_processor_id();
314 printk(KERN_ERR
"svm_cpu_init: err EOPNOTSUPP on %d\n", me
);
317 svm_data
= per_cpu(svm_data
, me
);
320 printk(KERN_ERR
"svm_cpu_init: svm_data is NULL on %d\n",
325 svm_data
->asid_generation
= 1;
326 svm_data
->max_asid
= cpuid_ebx(SVM_CPUID_FUNC
) - 1;
327 svm_data
->next_asid
= svm_data
->max_asid
+ 1;
328 svm_features
= cpuid_edx(SVM_CPUID_FUNC
);
330 asm volatile ("sgdt %0" : "=m"(gdt_descr
));
331 gdt
= (struct desc_struct
*)gdt_descr
.address
;
332 svm_data
->tss_desc
= (struct kvm_ldttss_desc
*)(gdt
+ GDT_ENTRY_TSS
);
334 rdmsrl(MSR_EFER
, efer
);
335 wrmsrl(MSR_EFER
, efer
| MSR_EFER_SVME_MASK
);
337 wrmsrl(MSR_VM_HSAVE_PA
,
338 page_to_pfn(svm_data
->save_area
) << PAGE_SHIFT
);
341 static int svm_cpu_init(int cpu
)
343 struct svm_cpu_data
*svm_data
;
346 svm_data
= kzalloc(sizeof(struct svm_cpu_data
), GFP_KERNEL
);
350 svm_data
->save_area
= alloc_page(GFP_KERNEL
);
352 if (!svm_data
->save_area
)
355 per_cpu(svm_data
, cpu
) = svm_data
;
365 static void set_msr_interception(u32
*msrpm
, unsigned msr
,
370 for (i
= 0; i
< NUM_MSR_MAPS
; i
++) {
371 if (msr
>= msrpm_ranges
[i
] &&
372 msr
< msrpm_ranges
[i
] + MSRS_IN_RANGE
) {
373 u32 msr_offset
= (i
* MSRS_IN_RANGE
+ msr
-
374 msrpm_ranges
[i
]) * 2;
376 u32
*base
= msrpm
+ (msr_offset
/ 32);
377 u32 msr_shift
= msr_offset
% 32;
378 u32 mask
= ((write
) ? 0 : 2) | ((read
) ? 0 : 1);
379 *base
= (*base
& ~(0x3 << msr_shift
)) |
387 static __init
int svm_hardware_setup(void)
390 struct page
*iopm_pages
;
391 struct page
*msrpm_pages
;
392 void *iopm_va
, *msrpm_va
;
395 iopm_pages
= alloc_pages(GFP_KERNEL
, IOPM_ALLOC_ORDER
);
400 iopm_va
= page_address(iopm_pages
);
401 memset(iopm_va
, 0xff, PAGE_SIZE
* (1 << IOPM_ALLOC_ORDER
));
402 clear_bit(0x80, iopm_va
); /* allow direct access to PC debug port */
403 iopm_base
= page_to_pfn(iopm_pages
) << PAGE_SHIFT
;
406 msrpm_pages
= alloc_pages(GFP_KERNEL
, MSRPM_ALLOC_ORDER
);
412 msrpm_va
= page_address(msrpm_pages
);
413 memset(msrpm_va
, 0xff, PAGE_SIZE
* (1 << MSRPM_ALLOC_ORDER
));
414 msrpm_base
= page_to_pfn(msrpm_pages
) << PAGE_SHIFT
;
417 set_msr_interception(msrpm_va
, MSR_GS_BASE
, 1, 1);
418 set_msr_interception(msrpm_va
, MSR_FS_BASE
, 1, 1);
419 set_msr_interception(msrpm_va
, MSR_KERNEL_GS_BASE
, 1, 1);
420 set_msr_interception(msrpm_va
, MSR_LSTAR
, 1, 1);
421 set_msr_interception(msrpm_va
, MSR_CSTAR
, 1, 1);
422 set_msr_interception(msrpm_va
, MSR_SYSCALL_MASK
, 1, 1);
424 set_msr_interception(msrpm_va
, MSR_K6_STAR
, 1, 1);
425 set_msr_interception(msrpm_va
, MSR_IA32_SYSENTER_CS
, 1, 1);
426 set_msr_interception(msrpm_va
, MSR_IA32_SYSENTER_ESP
, 1, 1);
427 set_msr_interception(msrpm_va
, MSR_IA32_SYSENTER_EIP
, 1, 1);
429 for_each_online_cpu(cpu
) {
430 r
= svm_cpu_init(cpu
);
437 __free_pages(msrpm_pages
, MSRPM_ALLOC_ORDER
);
440 __free_pages(iopm_pages
, IOPM_ALLOC_ORDER
);
445 static __exit
void svm_hardware_unsetup(void)
447 __free_pages(pfn_to_page(msrpm_base
>> PAGE_SHIFT
), MSRPM_ALLOC_ORDER
);
448 __free_pages(pfn_to_page(iopm_base
>> PAGE_SHIFT
), IOPM_ALLOC_ORDER
);
449 iopm_base
= msrpm_base
= 0;
452 static void init_seg(struct vmcb_seg
*seg
)
455 seg
->attrib
= SVM_SELECTOR_P_MASK
| SVM_SELECTOR_S_MASK
|
456 SVM_SELECTOR_WRITE_MASK
; /* Read/Write Data Segment */
461 static void init_sys_seg(struct vmcb_seg
*seg
, uint32_t type
)
464 seg
->attrib
= SVM_SELECTOR_P_MASK
| type
;
469 static void init_vmcb(struct vmcb
*vmcb
)
471 struct vmcb_control_area
*control
= &vmcb
->control
;
472 struct vmcb_save_area
*save
= &vmcb
->save
;
474 control
->intercept_cr_read
= INTERCEPT_CR0_MASK
|
478 control
->intercept_cr_write
= INTERCEPT_CR0_MASK
|
482 control
->intercept_dr_read
= INTERCEPT_DR0_MASK
|
487 control
->intercept_dr_write
= INTERCEPT_DR0_MASK
|
494 control
->intercept_exceptions
= (1 << PF_VECTOR
) |
498 control
->intercept
= (1ULL << INTERCEPT_INTR
) |
499 (1ULL << INTERCEPT_NMI
) |
500 (1ULL << INTERCEPT_SMI
) |
502 * selective cr0 intercept bug?
503 * 0: 0f 22 d8 mov %eax,%cr3
504 * 3: 0f 20 c0 mov %cr0,%eax
505 * 6: 0d 00 00 00 80 or $0x80000000,%eax
506 * b: 0f 22 c0 mov %eax,%cr0
507 * set cr3 ->interception
508 * get cr0 ->interception
509 * set cr0 -> no interception
511 /* (1ULL << INTERCEPT_SELECTIVE_CR0) | */
512 (1ULL << INTERCEPT_CPUID
) |
513 (1ULL << INTERCEPT_INVD
) |
514 (1ULL << INTERCEPT_HLT
) |
515 (1ULL << INTERCEPT_INVLPGA
) |
516 (1ULL << INTERCEPT_IOIO_PROT
) |
517 (1ULL << INTERCEPT_MSR_PROT
) |
518 (1ULL << INTERCEPT_TASK_SWITCH
) |
519 (1ULL << INTERCEPT_SHUTDOWN
) |
520 (1ULL << INTERCEPT_VMRUN
) |
521 (1ULL << INTERCEPT_VMMCALL
) |
522 (1ULL << INTERCEPT_VMLOAD
) |
523 (1ULL << INTERCEPT_VMSAVE
) |
524 (1ULL << INTERCEPT_STGI
) |
525 (1ULL << INTERCEPT_CLGI
) |
526 (1ULL << INTERCEPT_SKINIT
) |
527 (1ULL << INTERCEPT_WBINVD
) |
528 (1ULL << INTERCEPT_MONITOR
) |
529 (1ULL << INTERCEPT_MWAIT
);
531 control
->iopm_base_pa
= iopm_base
;
532 control
->msrpm_base_pa
= msrpm_base
;
533 control
->tsc_offset
= 0;
534 control
->int_ctl
= V_INTR_MASKING_MASK
;
542 save
->cs
.selector
= 0xf000;
543 /* Executable/Readable Code Segment */
544 save
->cs
.attrib
= SVM_SELECTOR_READ_MASK
| SVM_SELECTOR_P_MASK
|
545 SVM_SELECTOR_S_MASK
| SVM_SELECTOR_CODE_MASK
;
546 save
->cs
.limit
= 0xffff;
548 * cs.base should really be 0xffff0000, but vmx can't handle that, so
549 * be consistent with it.
551 * Replace when we have real mode working for vmx.
553 save
->cs
.base
= 0xf0000;
555 save
->gdtr
.limit
= 0xffff;
556 save
->idtr
.limit
= 0xffff;
558 init_sys_seg(&save
->ldtr
, SEG_TYPE_LDT
);
559 init_sys_seg(&save
->tr
, SEG_TYPE_BUSY_TSS16
);
561 save
->efer
= MSR_EFER_SVME_MASK
;
562 save
->dr6
= 0xffff0ff0;
565 save
->rip
= 0x0000fff0;
568 * cr0 val on cpu init should be 0x60000010, we enable cpu
569 * cache by default. the orderly way is to enable cache in bios.
571 save
->cr0
= 0x00000010 | X86_CR0_PG
| X86_CR0_WP
;
572 save
->cr4
= X86_CR4_PAE
;
576 static int svm_vcpu_reset(struct kvm_vcpu
*vcpu
)
578 struct vcpu_svm
*svm
= to_svm(vcpu
);
580 init_vmcb(svm
->vmcb
);
582 if (vcpu
->vcpu_id
!= 0) {
583 svm
->vmcb
->save
.rip
= 0;
584 svm
->vmcb
->save
.cs
.base
= svm
->vcpu
.sipi_vector
<< 12;
585 svm
->vmcb
->save
.cs
.selector
= svm
->vcpu
.sipi_vector
<< 8;
591 static struct kvm_vcpu
*svm_create_vcpu(struct kvm
*kvm
, unsigned int id
)
593 struct vcpu_svm
*svm
;
597 svm
= kmem_cache_zalloc(kvm_vcpu_cache
, GFP_KERNEL
);
603 err
= kvm_vcpu_init(&svm
->vcpu
, kvm
, id
);
607 page
= alloc_page(GFP_KERNEL
);
613 svm
->vmcb
= page_address(page
);
614 clear_page(svm
->vmcb
);
615 svm
->vmcb_pa
= page_to_pfn(page
) << PAGE_SHIFT
;
616 svm
->asid_generation
= 0;
617 memset(svm
->db_regs
, 0, sizeof(svm
->db_regs
));
618 init_vmcb(svm
->vmcb
);
621 svm
->vcpu
.fpu_active
= 1;
622 svm
->vcpu
.apic_base
= 0xfee00000 | MSR_IA32_APICBASE_ENABLE
;
623 if (svm
->vcpu
.vcpu_id
== 0)
624 svm
->vcpu
.apic_base
|= MSR_IA32_APICBASE_BSP
;
629 kvm_vcpu_uninit(&svm
->vcpu
);
631 kmem_cache_free(kvm_vcpu_cache
, svm
);
636 static void svm_free_vcpu(struct kvm_vcpu
*vcpu
)
638 struct vcpu_svm
*svm
= to_svm(vcpu
);
640 __free_page(pfn_to_page(svm
->vmcb_pa
>> PAGE_SHIFT
));
641 kvm_vcpu_uninit(vcpu
);
642 kmem_cache_free(kvm_vcpu_cache
, svm
);
645 static void svm_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
647 struct vcpu_svm
*svm
= to_svm(vcpu
);
650 if (unlikely(cpu
!= vcpu
->cpu
)) {
654 * Make sure that the guest sees a monotonically
658 delta
= vcpu
->host_tsc
- tsc_this
;
659 svm
->vmcb
->control
.tsc_offset
+= delta
;
661 kvm_migrate_apic_timer(vcpu
);
664 for (i
= 0; i
< NR_HOST_SAVE_USER_MSRS
; i
++)
665 rdmsrl(host_save_user_msrs
[i
], svm
->host_user_msrs
[i
]);
668 static void svm_vcpu_put(struct kvm_vcpu
*vcpu
)
670 struct vcpu_svm
*svm
= to_svm(vcpu
);
673 ++vcpu
->stat
.host_state_reload
;
674 for (i
= 0; i
< NR_HOST_SAVE_USER_MSRS
; i
++)
675 wrmsrl(host_save_user_msrs
[i
], svm
->host_user_msrs
[i
]);
677 rdtscll(vcpu
->host_tsc
);
680 static void svm_vcpu_decache(struct kvm_vcpu
*vcpu
)
684 static void svm_cache_regs(struct kvm_vcpu
*vcpu
)
686 struct vcpu_svm
*svm
= to_svm(vcpu
);
688 vcpu
->regs
[VCPU_REGS_RAX
] = svm
->vmcb
->save
.rax
;
689 vcpu
->regs
[VCPU_REGS_RSP
] = svm
->vmcb
->save
.rsp
;
690 vcpu
->rip
= svm
->vmcb
->save
.rip
;
693 static void svm_decache_regs(struct kvm_vcpu
*vcpu
)
695 struct vcpu_svm
*svm
= to_svm(vcpu
);
696 svm
->vmcb
->save
.rax
= vcpu
->regs
[VCPU_REGS_RAX
];
697 svm
->vmcb
->save
.rsp
= vcpu
->regs
[VCPU_REGS_RSP
];
698 svm
->vmcb
->save
.rip
= vcpu
->rip
;
701 static unsigned long svm_get_rflags(struct kvm_vcpu
*vcpu
)
703 return to_svm(vcpu
)->vmcb
->save
.rflags
;
706 static void svm_set_rflags(struct kvm_vcpu
*vcpu
, unsigned long rflags
)
708 to_svm(vcpu
)->vmcb
->save
.rflags
= rflags
;
711 static struct vmcb_seg
*svm_seg(struct kvm_vcpu
*vcpu
, int seg
)
713 struct vmcb_save_area
*save
= &to_svm(vcpu
)->vmcb
->save
;
716 case VCPU_SREG_CS
: return &save
->cs
;
717 case VCPU_SREG_DS
: return &save
->ds
;
718 case VCPU_SREG_ES
: return &save
->es
;
719 case VCPU_SREG_FS
: return &save
->fs
;
720 case VCPU_SREG_GS
: return &save
->gs
;
721 case VCPU_SREG_SS
: return &save
->ss
;
722 case VCPU_SREG_TR
: return &save
->tr
;
723 case VCPU_SREG_LDTR
: return &save
->ldtr
;
729 static u64
svm_get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
731 struct vmcb_seg
*s
= svm_seg(vcpu
, seg
);
736 static void svm_get_segment(struct kvm_vcpu
*vcpu
,
737 struct kvm_segment
*var
, int seg
)
739 struct vmcb_seg
*s
= svm_seg(vcpu
, seg
);
742 var
->limit
= s
->limit
;
743 var
->selector
= s
->selector
;
744 var
->type
= s
->attrib
& SVM_SELECTOR_TYPE_MASK
;
745 var
->s
= (s
->attrib
>> SVM_SELECTOR_S_SHIFT
) & 1;
746 var
->dpl
= (s
->attrib
>> SVM_SELECTOR_DPL_SHIFT
) & 3;
747 var
->present
= (s
->attrib
>> SVM_SELECTOR_P_SHIFT
) & 1;
748 var
->avl
= (s
->attrib
>> SVM_SELECTOR_AVL_SHIFT
) & 1;
749 var
->l
= (s
->attrib
>> SVM_SELECTOR_L_SHIFT
) & 1;
750 var
->db
= (s
->attrib
>> SVM_SELECTOR_DB_SHIFT
) & 1;
751 var
->g
= (s
->attrib
>> SVM_SELECTOR_G_SHIFT
) & 1;
752 var
->unusable
= !var
->present
;
755 static void svm_get_idt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
757 struct vcpu_svm
*svm
= to_svm(vcpu
);
759 dt
->limit
= svm
->vmcb
->save
.idtr
.limit
;
760 dt
->base
= svm
->vmcb
->save
.idtr
.base
;
763 static void svm_set_idt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
765 struct vcpu_svm
*svm
= to_svm(vcpu
);
767 svm
->vmcb
->save
.idtr
.limit
= dt
->limit
;
768 svm
->vmcb
->save
.idtr
.base
= dt
->base
;
771 static void svm_get_gdt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
773 struct vcpu_svm
*svm
= to_svm(vcpu
);
775 dt
->limit
= svm
->vmcb
->save
.gdtr
.limit
;
776 dt
->base
= svm
->vmcb
->save
.gdtr
.base
;
779 static void svm_set_gdt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
781 struct vcpu_svm
*svm
= to_svm(vcpu
);
783 svm
->vmcb
->save
.gdtr
.limit
= dt
->limit
;
784 svm
->vmcb
->save
.gdtr
.base
= dt
->base
;
787 static void svm_decache_cr4_guest_bits(struct kvm_vcpu
*vcpu
)
791 static void svm_set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
793 struct vcpu_svm
*svm
= to_svm(vcpu
);
796 if (vcpu
->shadow_efer
& EFER_LME
) {
797 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
)) {
798 vcpu
->shadow_efer
|= EFER_LMA
;
799 svm
->vmcb
->save
.efer
|= EFER_LMA
| EFER_LME
;
802 if (is_paging(vcpu
) && !(cr0
& X86_CR0_PG
)) {
803 vcpu
->shadow_efer
&= ~EFER_LMA
;
804 svm
->vmcb
->save
.efer
&= ~(EFER_LMA
| EFER_LME
);
808 if ((vcpu
->cr0
& X86_CR0_TS
) && !(cr0
& X86_CR0_TS
)) {
809 svm
->vmcb
->control
.intercept_exceptions
&= ~(1 << NM_VECTOR
);
810 vcpu
->fpu_active
= 1;
814 cr0
|= X86_CR0_PG
| X86_CR0_WP
;
815 cr0
&= ~(X86_CR0_CD
| X86_CR0_NW
);
816 svm
->vmcb
->save
.cr0
= cr0
;
819 static void svm_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
822 to_svm(vcpu
)->vmcb
->save
.cr4
= cr4
| X86_CR4_PAE
;
825 static void svm_set_segment(struct kvm_vcpu
*vcpu
,
826 struct kvm_segment
*var
, int seg
)
828 struct vcpu_svm
*svm
= to_svm(vcpu
);
829 struct vmcb_seg
*s
= svm_seg(vcpu
, seg
);
832 s
->limit
= var
->limit
;
833 s
->selector
= var
->selector
;
837 s
->attrib
= (var
->type
& SVM_SELECTOR_TYPE_MASK
);
838 s
->attrib
|= (var
->s
& 1) << SVM_SELECTOR_S_SHIFT
;
839 s
->attrib
|= (var
->dpl
& 3) << SVM_SELECTOR_DPL_SHIFT
;
840 s
->attrib
|= (var
->present
& 1) << SVM_SELECTOR_P_SHIFT
;
841 s
->attrib
|= (var
->avl
& 1) << SVM_SELECTOR_AVL_SHIFT
;
842 s
->attrib
|= (var
->l
& 1) << SVM_SELECTOR_L_SHIFT
;
843 s
->attrib
|= (var
->db
& 1) << SVM_SELECTOR_DB_SHIFT
;
844 s
->attrib
|= (var
->g
& 1) << SVM_SELECTOR_G_SHIFT
;
846 if (seg
== VCPU_SREG_CS
)
848 = (svm
->vmcb
->save
.cs
.attrib
849 >> SVM_SELECTOR_DPL_SHIFT
) & 3;
855 svm(vcpu)->vmcb->control.int_ctl &= ~V_TPR_MASK;
856 svm(vcpu)->vmcb->control.int_ctl |= (sregs->cr8 & V_TPR_MASK);
860 static int svm_guest_debug(struct kvm_vcpu
*vcpu
, struct kvm_debug_guest
*dbg
)
865 static int svm_get_irq(struct kvm_vcpu
*vcpu
)
867 struct vcpu_svm
*svm
= to_svm(vcpu
);
868 u32 exit_int_info
= svm
->vmcb
->control
.exit_int_info
;
870 if (is_external_interrupt(exit_int_info
))
871 return exit_int_info
& SVM_EVTINJ_VEC_MASK
;
875 static void load_host_msrs(struct kvm_vcpu
*vcpu
)
878 wrmsrl(MSR_GS_BASE
, to_svm(vcpu
)->host_gs_base
);
882 static void save_host_msrs(struct kvm_vcpu
*vcpu
)
885 rdmsrl(MSR_GS_BASE
, to_svm(vcpu
)->host_gs_base
);
889 static void new_asid(struct vcpu_svm
*svm
, struct svm_cpu_data
*svm_data
)
891 if (svm_data
->next_asid
> svm_data
->max_asid
) {
892 ++svm_data
->asid_generation
;
893 svm_data
->next_asid
= 1;
894 svm
->vmcb
->control
.tlb_ctl
= TLB_CONTROL_FLUSH_ALL_ASID
;
897 svm
->vcpu
.cpu
= svm_data
->cpu
;
898 svm
->asid_generation
= svm_data
->asid_generation
;
899 svm
->vmcb
->control
.asid
= svm_data
->next_asid
++;
902 static unsigned long svm_get_dr(struct kvm_vcpu
*vcpu
, int dr
)
904 return to_svm(vcpu
)->db_regs
[dr
];
907 static void svm_set_dr(struct kvm_vcpu
*vcpu
, int dr
, unsigned long value
,
910 struct vcpu_svm
*svm
= to_svm(vcpu
);
914 if (svm
->vmcb
->save
.dr7
& DR7_GD_MASK
) {
915 svm
->vmcb
->save
.dr7
&= ~DR7_GD_MASK
;
916 svm
->vmcb
->save
.dr6
|= DR6_BD_MASK
;
917 *exception
= DB_VECTOR
;
923 svm
->db_regs
[dr
] = value
;
926 if (vcpu
->cr4
& X86_CR4_DE
) {
927 *exception
= UD_VECTOR
;
931 if (value
& ~((1ULL << 32) - 1)) {
932 *exception
= GP_VECTOR
;
935 svm
->vmcb
->save
.dr7
= value
;
939 printk(KERN_DEBUG
"%s: unexpected dr %u\n",
941 *exception
= UD_VECTOR
;
946 static int pf_interception(struct vcpu_svm
*svm
, struct kvm_run
*kvm_run
)
948 u32 exit_int_info
= svm
->vmcb
->control
.exit_int_info
;
949 struct kvm
*kvm
= svm
->vcpu
.kvm
;
953 if (!irqchip_in_kernel(kvm
) &&
954 is_external_interrupt(exit_int_info
))
955 push_irq(&svm
->vcpu
, exit_int_info
& SVM_EVTINJ_VEC_MASK
);
957 fault_address
= svm
->vmcb
->control
.exit_info_2
;
958 error_code
= svm
->vmcb
->control
.exit_info_1
;
959 return kvm_mmu_page_fault(&svm
->vcpu
, fault_address
, error_code
);
962 static int ud_interception(struct vcpu_svm
*svm
, struct kvm_run
*kvm_run
)
966 er
= emulate_instruction(&svm
->vcpu
, kvm_run
, 0, 0, 0);
967 if (er
!= EMULATE_DONE
)
968 inject_ud(&svm
->vcpu
);
973 static int nm_interception(struct vcpu_svm
*svm
, struct kvm_run
*kvm_run
)
975 svm
->vmcb
->control
.intercept_exceptions
&= ~(1 << NM_VECTOR
);
976 if (!(svm
->vcpu
.cr0
& X86_CR0_TS
))
977 svm
->vmcb
->save
.cr0
&= ~X86_CR0_TS
;
978 svm
->vcpu
.fpu_active
= 1;
983 static int shutdown_interception(struct vcpu_svm
*svm
, struct kvm_run
*kvm_run
)
986 * VMCB is undefined after a SHUTDOWN intercept
987 * so reinitialize it.
989 clear_page(svm
->vmcb
);
990 init_vmcb(svm
->vmcb
);
992 kvm_run
->exit_reason
= KVM_EXIT_SHUTDOWN
;
996 static int io_interception(struct vcpu_svm
*svm
, struct kvm_run
*kvm_run
)
998 u32 io_info
= svm
->vmcb
->control
.exit_info_1
; /* address size bug? */
999 int size
, down
, in
, string
, rep
;
1002 ++svm
->vcpu
.stat
.io_exits
;
1004 svm
->next_rip
= svm
->vmcb
->control
.exit_info_2
;
1006 string
= (io_info
& SVM_IOIO_STR_MASK
) != 0;
1009 if (emulate_instruction(&svm
->vcpu
,
1010 kvm_run
, 0, 0, 0) == EMULATE_DO_MMIO
)
1015 in
= (io_info
& SVM_IOIO_TYPE_MASK
) != 0;
1016 port
= io_info
>> 16;
1017 size
= (io_info
& SVM_IOIO_SIZE_MASK
) >> SVM_IOIO_SIZE_SHIFT
;
1018 rep
= (io_info
& SVM_IOIO_REP_MASK
) != 0;
1019 down
= (svm
->vmcb
->save
.rflags
& X86_EFLAGS_DF
) != 0;
1021 return kvm_emulate_pio(&svm
->vcpu
, kvm_run
, in
, size
, port
);
1024 static int nop_on_interception(struct vcpu_svm
*svm
, struct kvm_run
*kvm_run
)
1029 static int halt_interception(struct vcpu_svm
*svm
, struct kvm_run
*kvm_run
)
1031 svm
->next_rip
= svm
->vmcb
->save
.rip
+ 1;
1032 skip_emulated_instruction(&svm
->vcpu
);
1033 return kvm_emulate_halt(&svm
->vcpu
);
1036 static int vmmcall_interception(struct vcpu_svm
*svm
, struct kvm_run
*kvm_run
)
1038 svm
->next_rip
= svm
->vmcb
->save
.rip
+ 3;
1039 skip_emulated_instruction(&svm
->vcpu
);
1040 kvm_emulate_hypercall(&svm
->vcpu
);
1044 static int invalid_op_interception(struct vcpu_svm
*svm
,
1045 struct kvm_run
*kvm_run
)
1047 inject_ud(&svm
->vcpu
);
1051 static int task_switch_interception(struct vcpu_svm
*svm
,
1052 struct kvm_run
*kvm_run
)
1054 pr_unimpl(&svm
->vcpu
, "%s: task switch is unsupported\n", __FUNCTION__
);
1055 kvm_run
->exit_reason
= KVM_EXIT_UNKNOWN
;
1059 static int cpuid_interception(struct vcpu_svm
*svm
, struct kvm_run
*kvm_run
)
1061 svm
->next_rip
= svm
->vmcb
->save
.rip
+ 2;
1062 kvm_emulate_cpuid(&svm
->vcpu
);
1066 static int emulate_on_interception(struct vcpu_svm
*svm
,
1067 struct kvm_run
*kvm_run
)
1069 if (emulate_instruction(&svm
->vcpu
, NULL
, 0, 0, 0) != EMULATE_DONE
)
1070 pr_unimpl(&svm
->vcpu
, "%s: failed\n", __FUNCTION__
);
1074 static int svm_get_msr(struct kvm_vcpu
*vcpu
, unsigned ecx
, u64
*data
)
1076 struct vcpu_svm
*svm
= to_svm(vcpu
);
1079 case MSR_IA32_TIME_STAMP_COUNTER
: {
1083 *data
= svm
->vmcb
->control
.tsc_offset
+ tsc
;
1087 *data
= svm
->vmcb
->save
.star
;
1089 #ifdef CONFIG_X86_64
1091 *data
= svm
->vmcb
->save
.lstar
;
1094 *data
= svm
->vmcb
->save
.cstar
;
1096 case MSR_KERNEL_GS_BASE
:
1097 *data
= svm
->vmcb
->save
.kernel_gs_base
;
1099 case MSR_SYSCALL_MASK
:
1100 *data
= svm
->vmcb
->save
.sfmask
;
1103 case MSR_IA32_SYSENTER_CS
:
1104 *data
= svm
->vmcb
->save
.sysenter_cs
;
1106 case MSR_IA32_SYSENTER_EIP
:
1107 *data
= svm
->vmcb
->save
.sysenter_eip
;
1109 case MSR_IA32_SYSENTER_ESP
:
1110 *data
= svm
->vmcb
->save
.sysenter_esp
;
1113 return kvm_get_msr_common(vcpu
, ecx
, data
);
1118 static int rdmsr_interception(struct vcpu_svm
*svm
, struct kvm_run
*kvm_run
)
1120 u32 ecx
= svm
->vcpu
.regs
[VCPU_REGS_RCX
];
1123 if (svm_get_msr(&svm
->vcpu
, ecx
, &data
))
1124 svm_inject_gp(&svm
->vcpu
, 0);
1126 svm
->vmcb
->save
.rax
= data
& 0xffffffff;
1127 svm
->vcpu
.regs
[VCPU_REGS_RDX
] = data
>> 32;
1128 svm
->next_rip
= svm
->vmcb
->save
.rip
+ 2;
1129 skip_emulated_instruction(&svm
->vcpu
);
1134 static int svm_set_msr(struct kvm_vcpu
*vcpu
, unsigned ecx
, u64 data
)
1136 struct vcpu_svm
*svm
= to_svm(vcpu
);
1139 case MSR_IA32_TIME_STAMP_COUNTER
: {
1143 svm
->vmcb
->control
.tsc_offset
= data
- tsc
;
1147 svm
->vmcb
->save
.star
= data
;
1149 #ifdef CONFIG_X86_64
1151 svm
->vmcb
->save
.lstar
= data
;
1154 svm
->vmcb
->save
.cstar
= data
;
1156 case MSR_KERNEL_GS_BASE
:
1157 svm
->vmcb
->save
.kernel_gs_base
= data
;
1159 case MSR_SYSCALL_MASK
:
1160 svm
->vmcb
->save
.sfmask
= data
;
1163 case MSR_IA32_SYSENTER_CS
:
1164 svm
->vmcb
->save
.sysenter_cs
= data
;
1166 case MSR_IA32_SYSENTER_EIP
:
1167 svm
->vmcb
->save
.sysenter_eip
= data
;
1169 case MSR_IA32_SYSENTER_ESP
:
1170 svm
->vmcb
->save
.sysenter_esp
= data
;
1173 return kvm_set_msr_common(vcpu
, ecx
, data
);
1178 static int wrmsr_interception(struct vcpu_svm
*svm
, struct kvm_run
*kvm_run
)
1180 u32 ecx
= svm
->vcpu
.regs
[VCPU_REGS_RCX
];
1181 u64 data
= (svm
->vmcb
->save
.rax
& -1u)
1182 | ((u64
)(svm
->vcpu
.regs
[VCPU_REGS_RDX
] & -1u) << 32);
1183 svm
->next_rip
= svm
->vmcb
->save
.rip
+ 2;
1184 if (svm_set_msr(&svm
->vcpu
, ecx
, data
))
1185 svm_inject_gp(&svm
->vcpu
, 0);
1187 skip_emulated_instruction(&svm
->vcpu
);
1191 static int msr_interception(struct vcpu_svm
*svm
, struct kvm_run
*kvm_run
)
1193 if (svm
->vmcb
->control
.exit_info_1
)
1194 return wrmsr_interception(svm
, kvm_run
);
1196 return rdmsr_interception(svm
, kvm_run
);
1199 static int interrupt_window_interception(struct vcpu_svm
*svm
,
1200 struct kvm_run
*kvm_run
)
1202 svm
->vmcb
->control
.intercept
&= ~(1ULL << INTERCEPT_VINTR
);
1203 svm
->vmcb
->control
.int_ctl
&= ~V_IRQ_MASK
;
1205 * If the user space waits to inject interrupts, exit as soon as
1208 if (kvm_run
->request_interrupt_window
&&
1209 !svm
->vcpu
.irq_summary
) {
1210 ++svm
->vcpu
.stat
.irq_window_exits
;
1211 kvm_run
->exit_reason
= KVM_EXIT_IRQ_WINDOW_OPEN
;
1218 static int (*svm_exit_handlers
[])(struct vcpu_svm
*svm
,
1219 struct kvm_run
*kvm_run
) = {
1220 [SVM_EXIT_READ_CR0
] = emulate_on_interception
,
1221 [SVM_EXIT_READ_CR3
] = emulate_on_interception
,
1222 [SVM_EXIT_READ_CR4
] = emulate_on_interception
,
1224 [SVM_EXIT_WRITE_CR0
] = emulate_on_interception
,
1225 [SVM_EXIT_WRITE_CR3
] = emulate_on_interception
,
1226 [SVM_EXIT_WRITE_CR4
] = emulate_on_interception
,
1227 [SVM_EXIT_READ_DR0
] = emulate_on_interception
,
1228 [SVM_EXIT_READ_DR1
] = emulate_on_interception
,
1229 [SVM_EXIT_READ_DR2
] = emulate_on_interception
,
1230 [SVM_EXIT_READ_DR3
] = emulate_on_interception
,
1231 [SVM_EXIT_WRITE_DR0
] = emulate_on_interception
,
1232 [SVM_EXIT_WRITE_DR1
] = emulate_on_interception
,
1233 [SVM_EXIT_WRITE_DR2
] = emulate_on_interception
,
1234 [SVM_EXIT_WRITE_DR3
] = emulate_on_interception
,
1235 [SVM_EXIT_WRITE_DR5
] = emulate_on_interception
,
1236 [SVM_EXIT_WRITE_DR7
] = emulate_on_interception
,
1237 [SVM_EXIT_EXCP_BASE
+ UD_VECTOR
] = ud_interception
,
1238 [SVM_EXIT_EXCP_BASE
+ PF_VECTOR
] = pf_interception
,
1239 [SVM_EXIT_EXCP_BASE
+ NM_VECTOR
] = nm_interception
,
1240 [SVM_EXIT_INTR
] = nop_on_interception
,
1241 [SVM_EXIT_NMI
] = nop_on_interception
,
1242 [SVM_EXIT_SMI
] = nop_on_interception
,
1243 [SVM_EXIT_INIT
] = nop_on_interception
,
1244 [SVM_EXIT_VINTR
] = interrupt_window_interception
,
1245 /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */
1246 [SVM_EXIT_CPUID
] = cpuid_interception
,
1247 [SVM_EXIT_INVD
] = emulate_on_interception
,
1248 [SVM_EXIT_HLT
] = halt_interception
,
1249 [SVM_EXIT_INVLPG
] = emulate_on_interception
,
1250 [SVM_EXIT_INVLPGA
] = invalid_op_interception
,
1251 [SVM_EXIT_IOIO
] = io_interception
,
1252 [SVM_EXIT_MSR
] = msr_interception
,
1253 [SVM_EXIT_TASK_SWITCH
] = task_switch_interception
,
1254 [SVM_EXIT_SHUTDOWN
] = shutdown_interception
,
1255 [SVM_EXIT_VMRUN
] = invalid_op_interception
,
1256 [SVM_EXIT_VMMCALL
] = vmmcall_interception
,
1257 [SVM_EXIT_VMLOAD
] = invalid_op_interception
,
1258 [SVM_EXIT_VMSAVE
] = invalid_op_interception
,
1259 [SVM_EXIT_STGI
] = invalid_op_interception
,
1260 [SVM_EXIT_CLGI
] = invalid_op_interception
,
1261 [SVM_EXIT_SKINIT
] = invalid_op_interception
,
1262 [SVM_EXIT_WBINVD
] = emulate_on_interception
,
1263 [SVM_EXIT_MONITOR
] = invalid_op_interception
,
1264 [SVM_EXIT_MWAIT
] = invalid_op_interception
,
1268 static int handle_exit(struct kvm_run
*kvm_run
, struct kvm_vcpu
*vcpu
)
1270 struct vcpu_svm
*svm
= to_svm(vcpu
);
1271 u32 exit_code
= svm
->vmcb
->control
.exit_code
;
1275 if (svm
->vmcb
->control
.exit_code
== SVM_EXIT_ERR
) {
1276 kvm_run
->exit_reason
= KVM_EXIT_FAIL_ENTRY
;
1277 kvm_run
->fail_entry
.hardware_entry_failure_reason
1278 = svm
->vmcb
->control
.exit_code
;
1282 if (is_external_interrupt(svm
->vmcb
->control
.exit_int_info
) &&
1283 exit_code
!= SVM_EXIT_EXCP_BASE
+ PF_VECTOR
)
1284 printk(KERN_ERR
"%s: unexpected exit_ini_info 0x%x "
1286 __FUNCTION__
, svm
->vmcb
->control
.exit_int_info
,
1289 if (exit_code
>= ARRAY_SIZE(svm_exit_handlers
)
1290 || !svm_exit_handlers
[exit_code
]) {
1291 kvm_run
->exit_reason
= KVM_EXIT_UNKNOWN
;
1292 kvm_run
->hw
.hardware_exit_reason
= exit_code
;
1296 return svm_exit_handlers
[exit_code
](svm
, kvm_run
);
1299 static void reload_tss(struct kvm_vcpu
*vcpu
)
1301 int cpu
= raw_smp_processor_id();
1303 struct svm_cpu_data
*svm_data
= per_cpu(svm_data
, cpu
);
1304 svm_data
->tss_desc
->type
= 9; /* available 32/64-bit TSS */
1308 static void pre_svm_run(struct vcpu_svm
*svm
)
1310 int cpu
= raw_smp_processor_id();
1312 struct svm_cpu_data
*svm_data
= per_cpu(svm_data
, cpu
);
1314 svm
->vmcb
->control
.tlb_ctl
= TLB_CONTROL_DO_NOTHING
;
1315 if (svm
->vcpu
.cpu
!= cpu
||
1316 svm
->asid_generation
!= svm_data
->asid_generation
)
1317 new_asid(svm
, svm_data
);
1321 static inline void svm_inject_irq(struct vcpu_svm
*svm
, int irq
)
1323 struct vmcb_control_area
*control
;
1325 control
= &svm
->vmcb
->control
;
1326 control
->int_vector
= irq
;
1327 control
->int_ctl
&= ~V_INTR_PRIO_MASK
;
1328 control
->int_ctl
|= V_IRQ_MASK
|
1329 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT
);
1332 static void svm_set_irq(struct kvm_vcpu
*vcpu
, int irq
)
1334 struct vcpu_svm
*svm
= to_svm(vcpu
);
1336 svm_inject_irq(svm
, irq
);
1339 static void svm_intr_assist(struct kvm_vcpu
*vcpu
)
1341 struct vcpu_svm
*svm
= to_svm(vcpu
);
1342 struct vmcb
*vmcb
= svm
->vmcb
;
1343 int intr_vector
= -1;
1345 if ((vmcb
->control
.exit_int_info
& SVM_EVTINJ_VALID
) &&
1346 ((vmcb
->control
.exit_int_info
& SVM_EVTINJ_TYPE_MASK
) == 0)) {
1347 intr_vector
= vmcb
->control
.exit_int_info
&
1348 SVM_EVTINJ_VEC_MASK
;
1349 vmcb
->control
.exit_int_info
= 0;
1350 svm_inject_irq(svm
, intr_vector
);
1354 if (vmcb
->control
.int_ctl
& V_IRQ_MASK
)
1357 if (!kvm_cpu_has_interrupt(vcpu
))
1360 if (!(vmcb
->save
.rflags
& X86_EFLAGS_IF
) ||
1361 (vmcb
->control
.int_state
& SVM_INTERRUPT_SHADOW_MASK
) ||
1362 (vmcb
->control
.event_inj
& SVM_EVTINJ_VALID
)) {
1363 /* unable to deliver irq, set pending irq */
1364 vmcb
->control
.intercept
|= (1ULL << INTERCEPT_VINTR
);
1365 svm_inject_irq(svm
, 0x0);
1368 /* Okay, we can deliver the interrupt: grab it and update PIC state. */
1369 intr_vector
= kvm_cpu_get_interrupt(vcpu
);
1370 svm_inject_irq(svm
, intr_vector
);
1371 kvm_timer_intr_post(vcpu
, intr_vector
);
1374 static void kvm_reput_irq(struct vcpu_svm
*svm
)
1376 struct vmcb_control_area
*control
= &svm
->vmcb
->control
;
1378 if ((control
->int_ctl
& V_IRQ_MASK
)
1379 && !irqchip_in_kernel(svm
->vcpu
.kvm
)) {
1380 control
->int_ctl
&= ~V_IRQ_MASK
;
1381 push_irq(&svm
->vcpu
, control
->int_vector
);
1384 svm
->vcpu
.interrupt_window_open
=
1385 !(control
->int_state
& SVM_INTERRUPT_SHADOW_MASK
);
1388 static void svm_do_inject_vector(struct vcpu_svm
*svm
)
1390 struct kvm_vcpu
*vcpu
= &svm
->vcpu
;
1391 int word_index
= __ffs(vcpu
->irq_summary
);
1392 int bit_index
= __ffs(vcpu
->irq_pending
[word_index
]);
1393 int irq
= word_index
* BITS_PER_LONG
+ bit_index
;
1395 clear_bit(bit_index
, &vcpu
->irq_pending
[word_index
]);
1396 if (!vcpu
->irq_pending
[word_index
])
1397 clear_bit(word_index
, &vcpu
->irq_summary
);
1398 svm_inject_irq(svm
, irq
);
1401 static void do_interrupt_requests(struct kvm_vcpu
*vcpu
,
1402 struct kvm_run
*kvm_run
)
1404 struct vcpu_svm
*svm
= to_svm(vcpu
);
1405 struct vmcb_control_area
*control
= &svm
->vmcb
->control
;
1407 svm
->vcpu
.interrupt_window_open
=
1408 (!(control
->int_state
& SVM_INTERRUPT_SHADOW_MASK
) &&
1409 (svm
->vmcb
->save
.rflags
& X86_EFLAGS_IF
));
1411 if (svm
->vcpu
.interrupt_window_open
&& svm
->vcpu
.irq_summary
)
1413 * If interrupts enabled, and not blocked by sti or mov ss. Good.
1415 svm_do_inject_vector(svm
);
1418 * Interrupts blocked. Wait for unblock.
1420 if (!svm
->vcpu
.interrupt_window_open
&&
1421 (svm
->vcpu
.irq_summary
|| kvm_run
->request_interrupt_window
))
1422 control
->intercept
|= 1ULL << INTERCEPT_VINTR
;
1424 control
->intercept
&= ~(1ULL << INTERCEPT_VINTR
);
1427 static int svm_set_tss_addr(struct kvm
*kvm
, unsigned int addr
)
1432 static void save_db_regs(unsigned long *db_regs
)
1434 asm volatile ("mov %%dr0, %0" : "=r"(db_regs
[0]));
1435 asm volatile ("mov %%dr1, %0" : "=r"(db_regs
[1]));
1436 asm volatile ("mov %%dr2, %0" : "=r"(db_regs
[2]));
1437 asm volatile ("mov %%dr3, %0" : "=r"(db_regs
[3]));
1440 static void load_db_regs(unsigned long *db_regs
)
1442 asm volatile ("mov %0, %%dr0" : : "r"(db_regs
[0]));
1443 asm volatile ("mov %0, %%dr1" : : "r"(db_regs
[1]));
1444 asm volatile ("mov %0, %%dr2" : : "r"(db_regs
[2]));
1445 asm volatile ("mov %0, %%dr3" : : "r"(db_regs
[3]));
1448 static void svm_flush_tlb(struct kvm_vcpu
*vcpu
)
1450 force_new_asid(vcpu
);
1453 static void svm_prepare_guest_switch(struct kvm_vcpu
*vcpu
)
1457 static void svm_vcpu_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1459 struct vcpu_svm
*svm
= to_svm(vcpu
);
1466 save_host_msrs(vcpu
);
1467 fs_selector
= read_fs();
1468 gs_selector
= read_gs();
1469 ldt_selector
= read_ldt();
1470 svm
->host_cr2
= kvm_read_cr2();
1471 svm
->host_dr6
= read_dr6();
1472 svm
->host_dr7
= read_dr7();
1473 svm
->vmcb
->save
.cr2
= vcpu
->cr2
;
1475 if (svm
->vmcb
->save
.dr7
& 0xff) {
1477 save_db_regs(svm
->host_db_regs
);
1478 load_db_regs(svm
->db_regs
);
1486 #ifdef CONFIG_X86_64
1492 #ifdef CONFIG_X86_64
1493 "mov %c[rbx](%[svm]), %%rbx \n\t"
1494 "mov %c[rcx](%[svm]), %%rcx \n\t"
1495 "mov %c[rdx](%[svm]), %%rdx \n\t"
1496 "mov %c[rsi](%[svm]), %%rsi \n\t"
1497 "mov %c[rdi](%[svm]), %%rdi \n\t"
1498 "mov %c[rbp](%[svm]), %%rbp \n\t"
1499 "mov %c[r8](%[svm]), %%r8 \n\t"
1500 "mov %c[r9](%[svm]), %%r9 \n\t"
1501 "mov %c[r10](%[svm]), %%r10 \n\t"
1502 "mov %c[r11](%[svm]), %%r11 \n\t"
1503 "mov %c[r12](%[svm]), %%r12 \n\t"
1504 "mov %c[r13](%[svm]), %%r13 \n\t"
1505 "mov %c[r14](%[svm]), %%r14 \n\t"
1506 "mov %c[r15](%[svm]), %%r15 \n\t"
1508 "mov %c[rbx](%[svm]), %%ebx \n\t"
1509 "mov %c[rcx](%[svm]), %%ecx \n\t"
1510 "mov %c[rdx](%[svm]), %%edx \n\t"
1511 "mov %c[rsi](%[svm]), %%esi \n\t"
1512 "mov %c[rdi](%[svm]), %%edi \n\t"
1513 "mov %c[rbp](%[svm]), %%ebp \n\t"
1516 #ifdef CONFIG_X86_64
1517 /* Enter guest mode */
1519 "mov %c[vmcb](%[svm]), %%rax \n\t"
1525 /* Enter guest mode */
1527 "mov %c[vmcb](%[svm]), %%eax \n\t"
1534 /* Save guest registers, load host registers */
1535 #ifdef CONFIG_X86_64
1536 "mov %%rbx, %c[rbx](%[svm]) \n\t"
1537 "mov %%rcx, %c[rcx](%[svm]) \n\t"
1538 "mov %%rdx, %c[rdx](%[svm]) \n\t"
1539 "mov %%rsi, %c[rsi](%[svm]) \n\t"
1540 "mov %%rdi, %c[rdi](%[svm]) \n\t"
1541 "mov %%rbp, %c[rbp](%[svm]) \n\t"
1542 "mov %%r8, %c[r8](%[svm]) \n\t"
1543 "mov %%r9, %c[r9](%[svm]) \n\t"
1544 "mov %%r10, %c[r10](%[svm]) \n\t"
1545 "mov %%r11, %c[r11](%[svm]) \n\t"
1546 "mov %%r12, %c[r12](%[svm]) \n\t"
1547 "mov %%r13, %c[r13](%[svm]) \n\t"
1548 "mov %%r14, %c[r14](%[svm]) \n\t"
1549 "mov %%r15, %c[r15](%[svm]) \n\t"
1553 "mov %%ebx, %c[rbx](%[svm]) \n\t"
1554 "mov %%ecx, %c[rcx](%[svm]) \n\t"
1555 "mov %%edx, %c[rdx](%[svm]) \n\t"
1556 "mov %%esi, %c[rsi](%[svm]) \n\t"
1557 "mov %%edi, %c[rdi](%[svm]) \n\t"
1558 "mov %%ebp, %c[rbp](%[svm]) \n\t"
1564 [vmcb
]"i"(offsetof(struct vcpu_svm
, vmcb_pa
)),
1565 [rbx
]"i"(offsetof(struct vcpu_svm
, vcpu
.regs
[VCPU_REGS_RBX
])),
1566 [rcx
]"i"(offsetof(struct vcpu_svm
, vcpu
.regs
[VCPU_REGS_RCX
])),
1567 [rdx
]"i"(offsetof(struct vcpu_svm
, vcpu
.regs
[VCPU_REGS_RDX
])),
1568 [rsi
]"i"(offsetof(struct vcpu_svm
, vcpu
.regs
[VCPU_REGS_RSI
])),
1569 [rdi
]"i"(offsetof(struct vcpu_svm
, vcpu
.regs
[VCPU_REGS_RDI
])),
1570 [rbp
]"i"(offsetof(struct vcpu_svm
, vcpu
.regs
[VCPU_REGS_RBP
]))
1571 #ifdef CONFIG_X86_64
1572 , [r8
]"i"(offsetof(struct vcpu_svm
, vcpu
.regs
[VCPU_REGS_R8
])),
1573 [r9
]"i"(offsetof(struct vcpu_svm
, vcpu
.regs
[VCPU_REGS_R9
])),
1574 [r10
]"i"(offsetof(struct vcpu_svm
, vcpu
.regs
[VCPU_REGS_R10
])),
1575 [r11
]"i"(offsetof(struct vcpu_svm
, vcpu
.regs
[VCPU_REGS_R11
])),
1576 [r12
]"i"(offsetof(struct vcpu_svm
, vcpu
.regs
[VCPU_REGS_R12
])),
1577 [r13
]"i"(offsetof(struct vcpu_svm
, vcpu
.regs
[VCPU_REGS_R13
])),
1578 [r14
]"i"(offsetof(struct vcpu_svm
, vcpu
.regs
[VCPU_REGS_R14
])),
1579 [r15
]"i"(offsetof(struct vcpu_svm
, vcpu
.regs
[VCPU_REGS_R15
]))
1582 #ifdef CONFIG_X86_64
1583 , "rbx", "rcx", "rdx", "rsi", "rdi"
1584 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
1586 , "ebx", "ecx", "edx" , "esi", "edi"
1590 if ((svm
->vmcb
->save
.dr7
& 0xff))
1591 load_db_regs(svm
->host_db_regs
);
1593 vcpu
->cr2
= svm
->vmcb
->save
.cr2
;
1595 write_dr6(svm
->host_dr6
);
1596 write_dr7(svm
->host_dr7
);
1597 kvm_write_cr2(svm
->host_cr2
);
1599 load_fs(fs_selector
);
1600 load_gs(gs_selector
);
1601 load_ldt(ldt_selector
);
1602 load_host_msrs(vcpu
);
1606 local_irq_disable();
1613 static void svm_set_cr3(struct kvm_vcpu
*vcpu
, unsigned long root
)
1615 struct vcpu_svm
*svm
= to_svm(vcpu
);
1617 svm
->vmcb
->save
.cr3
= root
;
1618 force_new_asid(vcpu
);
1620 if (vcpu
->fpu_active
) {
1621 svm
->vmcb
->control
.intercept_exceptions
|= (1 << NM_VECTOR
);
1622 svm
->vmcb
->save
.cr0
|= X86_CR0_TS
;
1623 vcpu
->fpu_active
= 0;
1627 static void svm_inject_page_fault(struct kvm_vcpu
*vcpu
,
1631 struct vcpu_svm
*svm
= to_svm(vcpu
);
1632 uint32_t exit_int_info
= svm
->vmcb
->control
.exit_int_info
;
1634 ++vcpu
->stat
.pf_guest
;
1636 if (is_page_fault(exit_int_info
)) {
1638 svm
->vmcb
->control
.event_inj_err
= 0;
1639 svm
->vmcb
->control
.event_inj
= SVM_EVTINJ_VALID
|
1640 SVM_EVTINJ_VALID_ERR
|
1641 SVM_EVTINJ_TYPE_EXEPT
|
1646 svm
->vmcb
->save
.cr2
= addr
;
1647 svm
->vmcb
->control
.event_inj
= SVM_EVTINJ_VALID
|
1648 SVM_EVTINJ_VALID_ERR
|
1649 SVM_EVTINJ_TYPE_EXEPT
|
1651 svm
->vmcb
->control
.event_inj_err
= err_code
;
1655 static int is_disabled(void)
1659 rdmsrl(MSR_VM_CR
, vm_cr
);
1660 if (vm_cr
& (1 << SVM_VM_CR_SVM_DISABLE
))
1667 svm_patch_hypercall(struct kvm_vcpu
*vcpu
, unsigned char *hypercall
)
1670 * Patch in the VMMCALL instruction:
1672 hypercall
[0] = 0x0f;
1673 hypercall
[1] = 0x01;
1674 hypercall
[2] = 0xd9;
1677 static void svm_check_processor_compat(void *rtn
)
1682 static struct kvm_x86_ops svm_x86_ops
= {
1683 .cpu_has_kvm_support
= has_svm
,
1684 .disabled_by_bios
= is_disabled
,
1685 .hardware_setup
= svm_hardware_setup
,
1686 .hardware_unsetup
= svm_hardware_unsetup
,
1687 .check_processor_compatibility
= svm_check_processor_compat
,
1688 .hardware_enable
= svm_hardware_enable
,
1689 .hardware_disable
= svm_hardware_disable
,
1691 .vcpu_create
= svm_create_vcpu
,
1692 .vcpu_free
= svm_free_vcpu
,
1693 .vcpu_reset
= svm_vcpu_reset
,
1695 .prepare_guest_switch
= svm_prepare_guest_switch
,
1696 .vcpu_load
= svm_vcpu_load
,
1697 .vcpu_put
= svm_vcpu_put
,
1698 .vcpu_decache
= svm_vcpu_decache
,
1700 .set_guest_debug
= svm_guest_debug
,
1701 .get_msr
= svm_get_msr
,
1702 .set_msr
= svm_set_msr
,
1703 .get_segment_base
= svm_get_segment_base
,
1704 .get_segment
= svm_get_segment
,
1705 .set_segment
= svm_set_segment
,
1706 .get_cs_db_l_bits
= kvm_get_cs_db_l_bits
,
1707 .decache_cr4_guest_bits
= svm_decache_cr4_guest_bits
,
1708 .set_cr0
= svm_set_cr0
,
1709 .set_cr3
= svm_set_cr3
,
1710 .set_cr4
= svm_set_cr4
,
1711 .set_efer
= svm_set_efer
,
1712 .get_idt
= svm_get_idt
,
1713 .set_idt
= svm_set_idt
,
1714 .get_gdt
= svm_get_gdt
,
1715 .set_gdt
= svm_set_gdt
,
1716 .get_dr
= svm_get_dr
,
1717 .set_dr
= svm_set_dr
,
1718 .cache_regs
= svm_cache_regs
,
1719 .decache_regs
= svm_decache_regs
,
1720 .get_rflags
= svm_get_rflags
,
1721 .set_rflags
= svm_set_rflags
,
1723 .tlb_flush
= svm_flush_tlb
,
1724 .inject_page_fault
= svm_inject_page_fault
,
1726 .inject_gp
= svm_inject_gp
,
1728 .run
= svm_vcpu_run
,
1729 .handle_exit
= handle_exit
,
1730 .skip_emulated_instruction
= skip_emulated_instruction
,
1731 .patch_hypercall
= svm_patch_hypercall
,
1732 .get_irq
= svm_get_irq
,
1733 .set_irq
= svm_set_irq
,
1734 .queue_exception
= svm_queue_exception
,
1735 .exception_injected
= svm_exception_injected
,
1736 .inject_pending_irq
= svm_intr_assist
,
1737 .inject_pending_vectors
= do_interrupt_requests
,
1739 .set_tss_addr
= svm_set_tss_addr
,
1742 static int __init
svm_init(void)
1744 return kvm_init(&svm_x86_ops
, sizeof(struct vcpu_svm
),
1748 static void __exit
svm_exit(void)
1753 module_init(svm_init
)
1754 module_exit(svm_exit
)