2 * qemu/kvm integration, x86 specific code
4 * Copyright (C) 2006-2008 Qumranet Technologies
6 * Licensed under the terms of the GNU GPL version 2 or higher.
10 #include "config-host.h"
20 #include <sys/utsname.h>
21 #include <linux/kvm_para.h>
22 #include <sys/ioctl.h>
27 #define MSR_IA32_TSC 0x10
29 static struct kvm_msr_list
*kvm_msr_list
;
30 extern unsigned int kvm_shadow_memory
;
31 static int kvm_has_msr_star
;
32 static int kvm_has_vm_hsave_pa
;
34 static int lm_capable_kernel
;
36 int kvm_set_tss_addr(kvm_context_t kvm
, unsigned long addr
)
38 #ifdef KVM_CAP_SET_TSS_ADDR
41 r
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
, KVM_CAP_SET_TSS_ADDR
);
43 r
= kvm_vm_ioctl(kvm_state
, KVM_SET_TSS_ADDR
, addr
);
45 fprintf(stderr
, "kvm_set_tss_addr: %m\n");
54 static int kvm_init_tss(kvm_context_t kvm
)
56 #ifdef KVM_CAP_SET_TSS_ADDR
59 r
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
, KVM_CAP_SET_TSS_ADDR
);
62 * this address is 3 pages before the bios, and the bios should present
65 r
= kvm_set_tss_addr(kvm
, 0xfeffd000);
67 fprintf(stderr
, "kvm_init_tss: unable to set tss addr\n");
76 static int kvm_set_identity_map_addr(kvm_context_t kvm
, uint64_t addr
)
78 #ifdef KVM_CAP_SET_IDENTITY_MAP_ADDR
81 r
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
, KVM_CAP_SET_IDENTITY_MAP_ADDR
);
83 r
= kvm_vm_ioctl(kvm_state
, KVM_SET_IDENTITY_MAP_ADDR
, &addr
);
85 fprintf(stderr
, "kvm_set_identity_map_addr: %m\n");
94 static int kvm_init_identity_map_page(kvm_context_t kvm
)
96 #ifdef KVM_CAP_SET_IDENTITY_MAP_ADDR
99 r
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
, KVM_CAP_SET_IDENTITY_MAP_ADDR
);
102 * this address is 4 pages before the bios, and the bios should present
103 * as unavaible memory
105 r
= kvm_set_identity_map_addr(kvm
, 0xfeffc000);
107 fprintf(stderr
, "kvm_init_identity_map_page: "
108 "unable to set identity mapping addr\n");
117 static int kvm_create_pit(kvm_context_t kvm
)
122 kvm
->pit_in_kernel
= 0;
123 if (!kvm
->no_pit_creation
) {
124 r
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
, KVM_CAP_PIT
);
126 r
= kvm_vm_ioctl(kvm_state
, KVM_CREATE_PIT
);
128 kvm
->pit_in_kernel
= 1;
130 fprintf(stderr
, "Create kernel PIC irqchip failed\n");
139 int kvm_arch_create(kvm_context_t kvm
, unsigned long phys_mem_bytes
,
144 r
= kvm_init_tss(kvm
);
148 r
= kvm_init_identity_map_page(kvm
);
152 r
= kvm_create_pit(kvm
);
156 r
= kvm_init_coalesced_mmio(kvm
);
163 #ifdef KVM_EXIT_TPR_ACCESS
165 static int kvm_handle_tpr_access(CPUState
*env
)
167 struct kvm_run
*run
= env
->kvm_run
;
168 kvm_tpr_access_report(env
,
170 run
->tpr_access
.is_write
);
175 int kvm_enable_vapic(kvm_vcpu_context_t vcpu
, uint64_t vapic
)
178 struct kvm_vapic_addr va
= {
182 r
= ioctl(vcpu
->fd
, KVM_SET_VAPIC_ADDR
, &va
);
185 perror("kvm_enable_vapic");
193 int kvm_arch_run(CPUState
*env
)
196 struct kvm_run
*run
= env
->kvm_run
;
199 switch (run
->exit_reason
) {
200 #ifdef KVM_EXIT_SET_TPR
201 case KVM_EXIT_SET_TPR
:
204 #ifdef KVM_EXIT_TPR_ACCESS
205 case KVM_EXIT_TPR_ACCESS
:
206 r
= kvm_handle_tpr_access(env
);
217 #define MAX_ALIAS_SLOTS 4
221 } kvm_aliases
[MAX_ALIAS_SLOTS
];
223 static int get_alias_slot(uint64_t start
)
227 for (i
=0; i
<MAX_ALIAS_SLOTS
; i
++)
228 if (kvm_aliases
[i
].start
== start
)
232 static int get_free_alias_slot(void)
236 for (i
=0; i
<MAX_ALIAS_SLOTS
; i
++)
237 if (kvm_aliases
[i
].len
== 0)
242 static void register_alias(int slot
, uint64_t start
, uint64_t len
)
244 kvm_aliases
[slot
].start
= start
;
245 kvm_aliases
[slot
].len
= len
;
248 int kvm_create_memory_alias(kvm_context_t kvm
,
251 uint64_t target_phys
)
253 struct kvm_memory_alias alias
= {
255 .guest_phys_addr
= phys_start
,
257 .target_phys_addr
= target_phys
,
262 slot
= get_alias_slot(phys_start
);
264 slot
= get_free_alias_slot();
269 r
= kvm_vm_ioctl(kvm_state
, KVM_SET_MEMORY_ALIAS
, &alias
);
273 register_alias(slot
, phys_start
, len
);
277 int kvm_destroy_memory_alias(kvm_context_t kvm
, uint64_t phys_start
)
279 return kvm_create_memory_alias(kvm
, phys_start
, 0, 0);
282 #ifdef KVM_CAP_IRQCHIP
284 int kvm_get_lapic(kvm_vcpu_context_t vcpu
, struct kvm_lapic_state
*s
)
287 if (!kvm_irqchip_in_kernel())
289 r
= ioctl(vcpu
->fd
, KVM_GET_LAPIC
, s
);
292 perror("kvm_get_lapic");
297 int kvm_set_lapic(kvm_vcpu_context_t vcpu
, struct kvm_lapic_state
*s
)
300 if (!kvm_irqchip_in_kernel())
302 r
= ioctl(vcpu
->fd
, KVM_SET_LAPIC
, s
);
305 perror("kvm_set_lapic");
314 int kvm_get_pit(kvm_context_t kvm
, struct kvm_pit_state
*s
)
316 if (!kvm
->pit_in_kernel
)
318 return kvm_vm_ioctl(kvm_state
, KVM_GET_PIT
, s
);
321 int kvm_set_pit(kvm_context_t kvm
, struct kvm_pit_state
*s
)
323 if (!kvm
->pit_in_kernel
)
325 return kvm_vm_ioctl(kvm_state
, KVM_SET_PIT
, s
);
328 #ifdef KVM_CAP_PIT_STATE2
329 int kvm_get_pit2(kvm_context_t kvm
, struct kvm_pit_state2
*ps2
)
331 if (!kvm
->pit_in_kernel
)
333 return kvm_vm_ioctl(kvm_state
, KVM_GET_PIT2
, ps2
);
336 int kvm_set_pit2(kvm_context_t kvm
, struct kvm_pit_state2
*ps2
)
338 if (!kvm
->pit_in_kernel
)
340 return kvm_vm_ioctl(kvm_state
, KVM_SET_PIT2
, ps2
);
346 int kvm_has_pit_state2(kvm_context_t kvm
)
350 #ifdef KVM_CAP_PIT_STATE2
351 r
= kvm_check_extension(kvm_state
, KVM_CAP_PIT_STATE2
);
356 void kvm_show_code(kvm_vcpu_context_t vcpu
)
358 #define SHOW_CODE_LEN 50
360 struct kvm_regs regs
;
361 struct kvm_sregs sregs
;
365 char code_str
[SHOW_CODE_LEN
* 3 + 1];
368 r
= ioctl(fd
, KVM_GET_SREGS
, &sregs
);
370 perror("KVM_GET_SREGS");
373 r
= ioctl(fd
, KVM_GET_REGS
, ®s
);
375 perror("KVM_GET_REGS");
378 rip
= sregs
.cs
.base
+ regs
.rip
;
379 back_offset
= regs
.rip
;
380 if (back_offset
> 20)
383 for (n
= -back_offset
; n
< SHOW_CODE_LEN
-back_offset
; ++n
) {
385 strcat(code_str
, " -->");
386 cpu_physical_memory_rw(rip
+ n
, &code
, 1, 1);
387 sprintf(code_str
+ strlen(code_str
), " %02x", code
);
389 fprintf(stderr
, "code:%s\n", code_str
);
394 * Returns available msr list. User must free.
396 struct kvm_msr_list
*kvm_get_msr_list(kvm_context_t kvm
)
398 struct kvm_msr_list sizer
, *msrs
;
402 r
= kvm_ioctl(kvm_state
, KVM_GET_MSR_INDEX_LIST
, &sizer
);
403 if (r
< 0 && r
!= -E2BIG
)
405 /* Old kernel modules had a bug and could write beyond the provided
406 memory. Allocate at least a safe amount of 1K. */
407 msrs
= qemu_malloc(MAX(1024, sizeof(*msrs
) +
408 sizer
.nmsrs
* sizeof(*msrs
->indices
)));
410 msrs
->nmsrs
= sizer
.nmsrs
;
411 r
= kvm_ioctl(kvm_state
, KVM_GET_MSR_INDEX_LIST
, msrs
);
420 int kvm_get_msrs(kvm_vcpu_context_t vcpu
, struct kvm_msr_entry
*msrs
, int n
)
422 struct kvm_msrs
*kmsrs
= qemu_malloc(sizeof *kmsrs
+ n
* sizeof *msrs
);
426 memcpy(kmsrs
->entries
, msrs
, n
* sizeof *msrs
);
427 r
= ioctl(vcpu
->fd
, KVM_GET_MSRS
, kmsrs
);
429 memcpy(msrs
, kmsrs
->entries
, n
* sizeof *msrs
);
435 int kvm_set_msrs(kvm_vcpu_context_t vcpu
, struct kvm_msr_entry
*msrs
, int n
)
437 struct kvm_msrs
*kmsrs
= qemu_malloc(sizeof *kmsrs
+ n
* sizeof *msrs
);
441 memcpy(kmsrs
->entries
, msrs
, n
* sizeof *msrs
);
442 r
= ioctl(vcpu
->fd
, KVM_SET_MSRS
, kmsrs
);
449 int kvm_get_mce_cap_supported(kvm_context_t kvm
, uint64_t *mce_cap
,
455 r
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
, KVM_CAP_MCE
);
458 return kvm_ioctl(kvm_state
, KVM_X86_GET_MCE_CAP_SUPPORTED
, mce_cap
);
464 int kvm_setup_mce(kvm_vcpu_context_t vcpu
, uint64_t *mcg_cap
)
467 return ioctl(vcpu
->fd
, KVM_X86_SETUP_MCE
, mcg_cap
);
473 int kvm_set_mce(kvm_vcpu_context_t vcpu
, struct kvm_x86_mce
*m
)
476 return ioctl(vcpu
->fd
, KVM_X86_SET_MCE
, m
);
482 static void print_seg(FILE *file
, const char *name
, struct kvm_segment
*seg
)
485 "%s %04x (%08llx/%08x p %d dpl %d db %d s %d type %x l %d"
487 name
, seg
->selector
, seg
->base
, seg
->limit
, seg
->present
,
488 seg
->dpl
, seg
->db
, seg
->s
, seg
->type
, seg
->l
, seg
->g
,
492 static void print_dt(FILE *file
, const char *name
, struct kvm_dtable
*dt
)
494 fprintf(stderr
, "%s %llx/%x\n", name
, dt
->base
, dt
->limit
);
497 void kvm_show_regs(kvm_vcpu_context_t vcpu
)
500 struct kvm_regs regs
;
501 struct kvm_sregs sregs
;
504 r
= ioctl(fd
, KVM_GET_REGS
, ®s
);
506 perror("KVM_GET_REGS");
510 "rax %016llx rbx %016llx rcx %016llx rdx %016llx\n"
511 "rsi %016llx rdi %016llx rsp %016llx rbp %016llx\n"
512 "r8 %016llx r9 %016llx r10 %016llx r11 %016llx\n"
513 "r12 %016llx r13 %016llx r14 %016llx r15 %016llx\n"
514 "rip %016llx rflags %08llx\n",
515 regs
.rax
, regs
.rbx
, regs
.rcx
, regs
.rdx
,
516 regs
.rsi
, regs
.rdi
, regs
.rsp
, regs
.rbp
,
517 regs
.r8
, regs
.r9
, regs
.r10
, regs
.r11
,
518 regs
.r12
, regs
.r13
, regs
.r14
, regs
.r15
,
519 regs
.rip
, regs
.rflags
);
520 r
= ioctl(fd
, KVM_GET_SREGS
, &sregs
);
522 perror("KVM_GET_SREGS");
525 print_seg(stderr
, "cs", &sregs
.cs
);
526 print_seg(stderr
, "ds", &sregs
.ds
);
527 print_seg(stderr
, "es", &sregs
.es
);
528 print_seg(stderr
, "ss", &sregs
.ss
);
529 print_seg(stderr
, "fs", &sregs
.fs
);
530 print_seg(stderr
, "gs", &sregs
.gs
);
531 print_seg(stderr
, "tr", &sregs
.tr
);
532 print_seg(stderr
, "ldt", &sregs
.ldt
);
533 print_dt(stderr
, "gdt", &sregs
.gdt
);
534 print_dt(stderr
, "idt", &sregs
.idt
);
535 fprintf(stderr
, "cr0 %llx cr2 %llx cr3 %llx cr4 %llx cr8 %llx"
537 sregs
.cr0
, sregs
.cr2
, sregs
.cr3
, sregs
.cr4
, sregs
.cr8
,
541 static uint64_t kvm_get_apic_base(CPUState
*env
)
543 return env
->kvm_run
->apic_base
;
546 static void kvm_set_cr8(CPUState
*env
, uint64_t cr8
)
548 env
->kvm_run
->cr8
= cr8
;
551 static __u64
kvm_get_cr8(CPUState
*env
)
553 return env
->kvm_run
->cr8
;
556 int kvm_setup_cpuid(kvm_vcpu_context_t vcpu
, int nent
,
557 struct kvm_cpuid_entry
*entries
)
559 struct kvm_cpuid
*cpuid
;
562 cpuid
= qemu_malloc(sizeof(*cpuid
) + nent
* sizeof(*entries
));
565 memcpy(cpuid
->entries
, entries
, nent
* sizeof(*entries
));
566 r
= ioctl(vcpu
->fd
, KVM_SET_CPUID
, cpuid
);
572 int kvm_setup_cpuid2(kvm_vcpu_context_t vcpu
, int nent
,
573 struct kvm_cpuid_entry2
*entries
)
575 struct kvm_cpuid2
*cpuid
;
578 cpuid
= qemu_malloc(sizeof(*cpuid
) + nent
* sizeof(*entries
));
581 memcpy(cpuid
->entries
, entries
, nent
* sizeof(*entries
));
582 r
= ioctl(vcpu
->fd
, KVM_SET_CPUID2
, cpuid
);
584 fprintf(stderr
, "kvm_setup_cpuid2: %m\n");
591 int kvm_set_shadow_pages(kvm_context_t kvm
, unsigned int nrshadow_pages
)
593 #ifdef KVM_CAP_MMU_SHADOW_CACHE_CONTROL
596 r
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
,
597 KVM_CAP_MMU_SHADOW_CACHE_CONTROL
);
599 r
= kvm_vm_ioctl(kvm_state
, KVM_SET_NR_MMU_PAGES
, nrshadow_pages
);
601 fprintf(stderr
, "kvm_set_shadow_pages: %m\n");
610 int kvm_get_shadow_pages(kvm_context_t kvm
, unsigned int *nrshadow_pages
)
612 #ifdef KVM_CAP_MMU_SHADOW_CACHE_CONTROL
615 r
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
,
616 KVM_CAP_MMU_SHADOW_CACHE_CONTROL
);
618 *nrshadow_pages
= kvm_vm_ioctl(kvm_state
, KVM_GET_NR_MMU_PAGES
);
627 static int tpr_access_reporting(kvm_vcpu_context_t vcpu
, int enabled
)
630 struct kvm_tpr_access_ctl tac
= {
634 r
= kvm_ioctl(kvm_state
, KVM_CHECK_EXTENSION
, KVM_CAP_VAPIC
);
637 r
= ioctl(vcpu
->fd
, KVM_TPR_ACCESS_REPORTING
, &tac
);
640 perror("KVM_TPR_ACCESS_REPORTING");
646 int kvm_enable_tpr_access_reporting(kvm_vcpu_context_t vcpu
)
648 return tpr_access_reporting(vcpu
, 1);
651 int kvm_disable_tpr_access_reporting(kvm_vcpu_context_t vcpu
)
653 return tpr_access_reporting(vcpu
, 0);
658 #ifdef KVM_CAP_EXT_CPUID
660 static struct kvm_cpuid2
*try_get_cpuid(kvm_context_t kvm
, int max
)
662 struct kvm_cpuid2
*cpuid
;
665 size
= sizeof(*cpuid
) + max
* sizeof(*cpuid
->entries
);
666 cpuid
= qemu_malloc(size
);
668 r
= kvm_ioctl(kvm_state
, KVM_GET_SUPPORTED_CPUID
, cpuid
);
669 if (r
== 0 && cpuid
->nent
>= max
)
676 fprintf(stderr
, "KVM_GET_SUPPORTED_CPUID failed: %s\n",
693 uint32_t kvm_get_supported_cpuid(kvm_context_t kvm
, uint32_t function
, int reg
)
695 struct kvm_cpuid2
*cpuid
;
698 uint32_t cpuid_1_edx
;
700 if (!kvm_check_extension(kvm_state
, KVM_CAP_EXT_CPUID
)) {
705 while ((cpuid
= try_get_cpuid(kvm
, max
)) == NULL
) {
709 for (i
= 0; i
< cpuid
->nent
; ++i
) {
710 if (cpuid
->entries
[i
].function
== function
) {
713 ret
= cpuid
->entries
[i
].eax
;
716 ret
= cpuid
->entries
[i
].ebx
;
719 ret
= cpuid
->entries
[i
].ecx
;
722 ret
= cpuid
->entries
[i
].edx
;
724 /* kvm misreports the following features
726 ret
|= 1 << 12; /* MTRR */
727 ret
|= 1 << 16; /* PAT */
728 ret
|= 1 << 7; /* MCE */
729 ret
|= 1 << 14; /* MCA */
732 /* On Intel, kvm returns cpuid according to
733 * the Intel spec, so add missing bits
734 * according to the AMD spec:
736 if (function
== 0x80000001) {
737 cpuid_1_edx
= kvm_get_supported_cpuid(kvm
, 1, R_EDX
);
738 ret
|= cpuid_1_edx
& 0xdfeff7ff;
752 uint32_t kvm_get_supported_cpuid(kvm_context_t kvm
, uint32_t function
, int reg
)
758 int kvm_qemu_create_memory_alias(uint64_t phys_start
,
760 uint64_t target_phys
)
762 return kvm_create_memory_alias(kvm_context
, phys_start
, len
, target_phys
);
765 int kvm_qemu_destroy_memory_alias(uint64_t phys_start
)
767 return kvm_destroy_memory_alias(kvm_context
, phys_start
);
770 int kvm_arch_qemu_create_context(void)
773 struct utsname utsname
;
776 lm_capable_kernel
= strcmp(utsname
.machine
, "x86_64") == 0;
778 if (kvm_shadow_memory
)
779 kvm_set_shadow_pages(kvm_context
, kvm_shadow_memory
);
781 kvm_msr_list
= kvm_get_msr_list(kvm_context
);
784 for (i
= 0; i
< kvm_msr_list
->nmsrs
; ++i
) {
785 if (kvm_msr_list
->indices
[i
] == MSR_STAR
)
786 kvm_has_msr_star
= 1;
787 if (kvm_msr_list
->indices
[i
] == MSR_VM_HSAVE_PA
)
788 kvm_has_vm_hsave_pa
= 1;
794 static void set_msr_entry(struct kvm_msr_entry
*entry
, uint32_t index
,
797 entry
->index
= index
;
801 /* returns 0 on success, non-0 on failure */
802 static int get_msr_entry(struct kvm_msr_entry
*entry
, CPUState
*env
)
804 switch (entry
->index
) {
805 case MSR_IA32_SYSENTER_CS
:
806 env
->sysenter_cs
= entry
->data
;
808 case MSR_IA32_SYSENTER_ESP
:
809 env
->sysenter_esp
= entry
->data
;
811 case MSR_IA32_SYSENTER_EIP
:
812 env
->sysenter_eip
= entry
->data
;
815 env
->star
= entry
->data
;
819 env
->cstar
= entry
->data
;
821 case MSR_KERNELGSBASE
:
822 env
->kernelgsbase
= entry
->data
;
825 env
->fmask
= entry
->data
;
828 env
->lstar
= entry
->data
;
832 env
->tsc
= entry
->data
;
834 case MSR_VM_HSAVE_PA
:
835 env
->vm_hsave
= entry
->data
;
838 printf("Warning unknown msr index 0x%x\n", entry
->index
);
850 static void set_v8086_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
852 lhs
->selector
= rhs
->selector
;
853 lhs
->base
= rhs
->base
;
854 lhs
->limit
= rhs
->limit
;
866 static void set_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
868 unsigned flags
= rhs
->flags
;
869 lhs
->selector
= rhs
->selector
;
870 lhs
->base
= rhs
->base
;
871 lhs
->limit
= rhs
->limit
;
872 lhs
->type
= (flags
>> DESC_TYPE_SHIFT
) & 15;
873 lhs
->present
= (flags
& DESC_P_MASK
) != 0;
874 lhs
->dpl
= rhs
->selector
& 3;
875 lhs
->db
= (flags
>> DESC_B_SHIFT
) & 1;
876 lhs
->s
= (flags
& DESC_S_MASK
) != 0;
877 lhs
->l
= (flags
>> DESC_L_SHIFT
) & 1;
878 lhs
->g
= (flags
& DESC_G_MASK
) != 0;
879 lhs
->avl
= (flags
& DESC_AVL_MASK
) != 0;
883 static void get_seg(SegmentCache
*lhs
, const struct kvm_segment
*rhs
)
885 lhs
->selector
= rhs
->selector
;
886 lhs
->base
= rhs
->base
;
887 lhs
->limit
= rhs
->limit
;
889 (rhs
->type
<< DESC_TYPE_SHIFT
)
890 | (rhs
->present
* DESC_P_MASK
)
891 | (rhs
->dpl
<< DESC_DPL_SHIFT
)
892 | (rhs
->db
<< DESC_B_SHIFT
)
893 | (rhs
->s
* DESC_S_MASK
)
894 | (rhs
->l
<< DESC_L_SHIFT
)
895 | (rhs
->g
* DESC_G_MASK
)
896 | (rhs
->avl
* DESC_AVL_MASK
);
899 void kvm_arch_load_regs(CPUState
*env
)
901 struct kvm_regs regs
;
903 struct kvm_sregs sregs
;
904 struct kvm_msr_entry msrs
[MSR_COUNT
];
907 regs
.rax
= env
->regs
[R_EAX
];
908 regs
.rbx
= env
->regs
[R_EBX
];
909 regs
.rcx
= env
->regs
[R_ECX
];
910 regs
.rdx
= env
->regs
[R_EDX
];
911 regs
.rsi
= env
->regs
[R_ESI
];
912 regs
.rdi
= env
->regs
[R_EDI
];
913 regs
.rsp
= env
->regs
[R_ESP
];
914 regs
.rbp
= env
->regs
[R_EBP
];
916 regs
.r8
= env
->regs
[8];
917 regs
.r9
= env
->regs
[9];
918 regs
.r10
= env
->regs
[10];
919 regs
.r11
= env
->regs
[11];
920 regs
.r12
= env
->regs
[12];
921 regs
.r13
= env
->regs
[13];
922 regs
.r14
= env
->regs
[14];
923 regs
.r15
= env
->regs
[15];
926 regs
.rflags
= env
->eflags
;
929 kvm_set_regs(env
->kvm_cpu_state
.vcpu_ctx
, ®s
);
931 memset(&fpu
, 0, sizeof fpu
);
932 fpu
.fsw
= env
->fpus
& ~(7 << 11);
933 fpu
.fsw
|= (env
->fpstt
& 7) << 11;
935 for (i
= 0; i
< 8; ++i
)
936 fpu
.ftwx
|= (!env
->fptags
[i
]) << i
;
937 memcpy(fpu
.fpr
, env
->fpregs
, sizeof env
->fpregs
);
938 memcpy(fpu
.xmm
, env
->xmm_regs
, sizeof env
->xmm_regs
);
939 fpu
.mxcsr
= env
->mxcsr
;
940 kvm_set_fpu(env
->kvm_cpu_state
.vcpu_ctx
, &fpu
);
942 memcpy(sregs
.interrupt_bitmap
, env
->interrupt_bitmap
, sizeof(sregs
.interrupt_bitmap
));
944 if ((env
->eflags
& VM_MASK
)) {
945 set_v8086_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
946 set_v8086_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
947 set_v8086_seg(&sregs
.es
, &env
->segs
[R_ES
]);
948 set_v8086_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
949 set_v8086_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
950 set_v8086_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
952 set_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
953 set_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
954 set_seg(&sregs
.es
, &env
->segs
[R_ES
]);
955 set_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
956 set_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
957 set_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
959 if (env
->cr
[0] & CR0_PE_MASK
) {
960 /* force ss cpl to cs cpl */
961 sregs
.ss
.selector
= (sregs
.ss
.selector
& ~3) |
962 (sregs
.cs
.selector
& 3);
963 sregs
.ss
.dpl
= sregs
.ss
.selector
& 3;
967 set_seg(&sregs
.tr
, &env
->tr
);
968 set_seg(&sregs
.ldt
, &env
->ldt
);
970 sregs
.idt
.limit
= env
->idt
.limit
;
971 sregs
.idt
.base
= env
->idt
.base
;
972 sregs
.gdt
.limit
= env
->gdt
.limit
;
973 sregs
.gdt
.base
= env
->gdt
.base
;
975 sregs
.cr0
= env
->cr
[0];
976 sregs
.cr2
= env
->cr
[2];
977 sregs
.cr3
= env
->cr
[3];
978 sregs
.cr4
= env
->cr
[4];
980 sregs
.cr8
= cpu_get_apic_tpr(env
);
981 sregs
.apic_base
= cpu_get_apic_base(env
);
983 sregs
.efer
= env
->efer
;
985 kvm_set_sregs(env
->kvm_cpu_state
.vcpu_ctx
, &sregs
);
989 /* Remember to increase MSR_COUNT if you add new registers below */
990 set_msr_entry(&msrs
[n
++], MSR_IA32_SYSENTER_CS
, env
->sysenter_cs
);
991 set_msr_entry(&msrs
[n
++], MSR_IA32_SYSENTER_ESP
, env
->sysenter_esp
);
992 set_msr_entry(&msrs
[n
++], MSR_IA32_SYSENTER_EIP
, env
->sysenter_eip
);
993 if (kvm_has_msr_star
)
994 set_msr_entry(&msrs
[n
++], MSR_STAR
, env
->star
);
995 if (kvm_has_vm_hsave_pa
)
996 set_msr_entry(&msrs
[n
++], MSR_VM_HSAVE_PA
, env
->vm_hsave
);
998 if (lm_capable_kernel
) {
999 set_msr_entry(&msrs
[n
++], MSR_CSTAR
, env
->cstar
);
1000 set_msr_entry(&msrs
[n
++], MSR_KERNELGSBASE
, env
->kernelgsbase
);
1001 set_msr_entry(&msrs
[n
++], MSR_FMASK
, env
->fmask
);
1002 set_msr_entry(&msrs
[n
++], MSR_LSTAR
, env
->lstar
);
1006 rc
= kvm_set_msrs(env
->kvm_cpu_state
.vcpu_ctx
, msrs
, n
);
1008 perror("kvm_set_msrs FAILED");
1011 void kvm_load_tsc(CPUState
*env
)
1014 struct kvm_msr_entry msr
;
1016 set_msr_entry(&msr
, MSR_IA32_TSC
, env
->tsc
);
1018 rc
= kvm_set_msrs(env
->kvm_cpu_state
.vcpu_ctx
, &msr
, 1);
1020 perror("kvm_set_tsc FAILED.\n");
1023 void kvm_arch_save_mpstate(CPUState
*env
)
1025 #ifdef KVM_CAP_MP_STATE
1027 struct kvm_mp_state mp_state
;
1029 r
= kvm_get_mpstate(env
->kvm_cpu_state
.vcpu_ctx
, &mp_state
);
1033 env
->mp_state
= mp_state
.mp_state
;
1039 void kvm_arch_load_mpstate(CPUState
*env
)
1041 #ifdef KVM_CAP_MP_STATE
1042 struct kvm_mp_state mp_state
= { .mp_state
= env
->mp_state
};
1045 * -1 indicates that the host did not support GET_MP_STATE ioctl,
1046 * so don't touch it.
1048 if (env
->mp_state
!= -1)
1049 kvm_set_mpstate(env
->kvm_cpu_state
.vcpu_ctx
, &mp_state
);
1053 void kvm_arch_save_regs(CPUState
*env
)
1055 struct kvm_regs regs
;
1057 struct kvm_sregs sregs
;
1058 struct kvm_msr_entry msrs
[MSR_COUNT
];
1062 kvm_get_regs(env
->kvm_cpu_state
.vcpu_ctx
, ®s
);
1064 env
->regs
[R_EAX
] = regs
.rax
;
1065 env
->regs
[R_EBX
] = regs
.rbx
;
1066 env
->regs
[R_ECX
] = regs
.rcx
;
1067 env
->regs
[R_EDX
] = regs
.rdx
;
1068 env
->regs
[R_ESI
] = regs
.rsi
;
1069 env
->regs
[R_EDI
] = regs
.rdi
;
1070 env
->regs
[R_ESP
] = regs
.rsp
;
1071 env
->regs
[R_EBP
] = regs
.rbp
;
1072 #ifdef TARGET_X86_64
1073 env
->regs
[8] = regs
.r8
;
1074 env
->regs
[9] = regs
.r9
;
1075 env
->regs
[10] = regs
.r10
;
1076 env
->regs
[11] = regs
.r11
;
1077 env
->regs
[12] = regs
.r12
;
1078 env
->regs
[13] = regs
.r13
;
1079 env
->regs
[14] = regs
.r14
;
1080 env
->regs
[15] = regs
.r15
;
1083 env
->eflags
= regs
.rflags
;
1084 env
->eip
= regs
.rip
;
1086 kvm_get_fpu(env
->kvm_cpu_state
.vcpu_ctx
, &fpu
);
1087 env
->fpstt
= (fpu
.fsw
>> 11) & 7;
1088 env
->fpus
= fpu
.fsw
;
1089 env
->fpuc
= fpu
.fcw
;
1090 for (i
= 0; i
< 8; ++i
)
1091 env
->fptags
[i
] = !((fpu
.ftwx
>> i
) & 1);
1092 memcpy(env
->fpregs
, fpu
.fpr
, sizeof env
->fpregs
);
1093 memcpy(env
->xmm_regs
, fpu
.xmm
, sizeof env
->xmm_regs
);
1094 env
->mxcsr
= fpu
.mxcsr
;
1096 kvm_get_sregs(env
->kvm_cpu_state
.vcpu_ctx
, &sregs
);
1098 memcpy(env
->interrupt_bitmap
, sregs
.interrupt_bitmap
, sizeof(env
->interrupt_bitmap
));
1100 get_seg(&env
->segs
[R_CS
], &sregs
.cs
);
1101 get_seg(&env
->segs
[R_DS
], &sregs
.ds
);
1102 get_seg(&env
->segs
[R_ES
], &sregs
.es
);
1103 get_seg(&env
->segs
[R_FS
], &sregs
.fs
);
1104 get_seg(&env
->segs
[R_GS
], &sregs
.gs
);
1105 get_seg(&env
->segs
[R_SS
], &sregs
.ss
);
1107 get_seg(&env
->tr
, &sregs
.tr
);
1108 get_seg(&env
->ldt
, &sregs
.ldt
);
1110 env
->idt
.limit
= sregs
.idt
.limit
;
1111 env
->idt
.base
= sregs
.idt
.base
;
1112 env
->gdt
.limit
= sregs
.gdt
.limit
;
1113 env
->gdt
.base
= sregs
.gdt
.base
;
1115 env
->cr
[0] = sregs
.cr0
;
1116 env
->cr
[2] = sregs
.cr2
;
1117 env
->cr
[3] = sregs
.cr3
;
1118 env
->cr
[4] = sregs
.cr4
;
1120 cpu_set_apic_base(env
, sregs
.apic_base
);
1122 env
->efer
= sregs
.efer
;
1123 //cpu_set_apic_tpr(env, sregs.cr8);
1125 #define HFLAG_COPY_MASK ~( \
1126 HF_CPL_MASK | HF_PE_MASK | HF_MP_MASK | HF_EM_MASK | \
1127 HF_TS_MASK | HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK | \
1128 HF_OSFXSR_MASK | HF_LMA_MASK | HF_CS32_MASK | \
1129 HF_SS32_MASK | HF_CS64_MASK | HF_ADDSEG_MASK)
1133 hflags
= (env
->segs
[R_CS
].flags
>> DESC_DPL_SHIFT
) & HF_CPL_MASK
;
1134 hflags
|= (env
->cr
[0] & CR0_PE_MASK
) << (HF_PE_SHIFT
- CR0_PE_SHIFT
);
1135 hflags
|= (env
->cr
[0] << (HF_MP_SHIFT
- CR0_MP_SHIFT
)) &
1136 (HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
);
1137 hflags
|= (env
->eflags
& (HF_TF_MASK
| HF_VM_MASK
| HF_IOPL_MASK
));
1138 hflags
|= (env
->cr
[4] & CR4_OSFXSR_MASK
) <<
1139 (HF_OSFXSR_SHIFT
- CR4_OSFXSR_SHIFT
);
1141 if (env
->efer
& MSR_EFER_LMA
) {
1142 hflags
|= HF_LMA_MASK
;
1145 if ((hflags
& HF_LMA_MASK
) && (env
->segs
[R_CS
].flags
& DESC_L_MASK
)) {
1146 hflags
|= HF_CS32_MASK
| HF_SS32_MASK
| HF_CS64_MASK
;
1148 hflags
|= (env
->segs
[R_CS
].flags
& DESC_B_MASK
) >>
1149 (DESC_B_SHIFT
- HF_CS32_SHIFT
);
1150 hflags
|= (env
->segs
[R_SS
].flags
& DESC_B_MASK
) >>
1151 (DESC_B_SHIFT
- HF_SS32_SHIFT
);
1152 if (!(env
->cr
[0] & CR0_PE_MASK
) ||
1153 (env
->eflags
& VM_MASK
) ||
1154 !(hflags
& HF_CS32_MASK
)) {
1155 hflags
|= HF_ADDSEG_MASK
;
1157 hflags
|= ((env
->segs
[R_DS
].base
|
1158 env
->segs
[R_ES
].base
|
1159 env
->segs
[R_SS
].base
) != 0) <<
1163 env
->hflags
= (env
->hflags
& HFLAG_COPY_MASK
) | hflags
;
1167 /* Remember to increase MSR_COUNT if you add new registers below */
1168 msrs
[n
++].index
= MSR_IA32_SYSENTER_CS
;
1169 msrs
[n
++].index
= MSR_IA32_SYSENTER_ESP
;
1170 msrs
[n
++].index
= MSR_IA32_SYSENTER_EIP
;
1171 if (kvm_has_msr_star
)
1172 msrs
[n
++].index
= MSR_STAR
;
1173 msrs
[n
++].index
= MSR_IA32_TSC
;
1174 if (kvm_has_vm_hsave_pa
)
1175 msrs
[n
++].index
= MSR_VM_HSAVE_PA
;
1176 #ifdef TARGET_X86_64
1177 if (lm_capable_kernel
) {
1178 msrs
[n
++].index
= MSR_CSTAR
;
1179 msrs
[n
++].index
= MSR_KERNELGSBASE
;
1180 msrs
[n
++].index
= MSR_FMASK
;
1181 msrs
[n
++].index
= MSR_LSTAR
;
1184 rc
= kvm_get_msrs(env
->kvm_cpu_state
.vcpu_ctx
, msrs
, n
);
1186 perror("kvm_get_msrs FAILED");
1189 n
= rc
; /* actual number of MSRs */
1190 for (i
=0 ; i
<n
; i
++) {
1191 if (get_msr_entry(&msrs
[i
], env
))
1197 static void do_cpuid_ent(struct kvm_cpuid_entry2
*e
, uint32_t function
,
1198 uint32_t count
, CPUState
*env
)
1200 env
->regs
[R_EAX
] = function
;
1201 env
->regs
[R_ECX
] = count
;
1202 qemu_kvm_cpuid_on_env(env
);
1203 e
->function
= function
;
1206 e
->eax
= env
->regs
[R_EAX
];
1207 e
->ebx
= env
->regs
[R_EBX
];
1208 e
->ecx
= env
->regs
[R_ECX
];
1209 e
->edx
= env
->regs
[R_EDX
];
1212 struct kvm_para_features
{
1215 } para_features
[] = {
1216 #ifdef KVM_CAP_CLOCKSOURCE
1217 { KVM_CAP_CLOCKSOURCE
, KVM_FEATURE_CLOCKSOURCE
},
1219 #ifdef KVM_CAP_NOP_IO_DELAY
1220 { KVM_CAP_NOP_IO_DELAY
, KVM_FEATURE_NOP_IO_DELAY
},
1222 #ifdef KVM_CAP_PV_MMU
1223 { KVM_CAP_PV_MMU
, KVM_FEATURE_MMU_OP
},
1225 #ifdef KVM_CAP_CR3_CACHE
1226 { KVM_CAP_CR3_CACHE
, KVM_FEATURE_CR3_CACHE
},
1231 static int get_para_features(kvm_context_t kvm_context
)
1233 int i
, features
= 0;
1235 for (i
= 0; i
< ARRAY_SIZE(para_features
)-1; i
++) {
1236 if (kvm_check_extension(kvm_state
, para_features
[i
].cap
))
1237 features
|= (1 << para_features
[i
].feature
);
1243 static void kvm_trim_features(uint32_t *features
, uint32_t supported
)
1248 for (i
= 0; i
< 32; ++i
) {
1250 if ((*features
& mask
) && !(supported
& mask
)) {
1256 int kvm_arch_init_vcpu(CPUState
*cenv
)
1258 struct kvm_cpuid_entry2 cpuid_ent
[100];
1259 #ifdef KVM_CPUID_SIGNATURE
1260 struct kvm_cpuid_entry2
*pv_ent
;
1261 uint32_t signature
[3];
1265 uint32_t i
, j
, limit
;
1267 qemu_kvm_load_lapic(cenv
);
1270 #ifdef KVM_CPUID_SIGNATURE
1271 /* Paravirtualization CPUIDs */
1272 memcpy(signature
, "KVMKVMKVM\0\0\0", 12);
1273 pv_ent
= &cpuid_ent
[cpuid_nent
++];
1274 memset(pv_ent
, 0, sizeof(*pv_ent
));
1275 pv_ent
->function
= KVM_CPUID_SIGNATURE
;
1277 pv_ent
->ebx
= signature
[0];
1278 pv_ent
->ecx
= signature
[1];
1279 pv_ent
->edx
= signature
[2];
1281 pv_ent
= &cpuid_ent
[cpuid_nent
++];
1282 memset(pv_ent
, 0, sizeof(*pv_ent
));
1283 pv_ent
->function
= KVM_CPUID_FEATURES
;
1284 pv_ent
->eax
= get_para_features(kvm_context
);
1287 kvm_trim_features(&cenv
->cpuid_features
,
1288 kvm_arch_get_supported_cpuid(cenv
, 1, R_EDX
));
1290 /* prevent the hypervisor bit from being cleared by the kernel */
1291 i
= cenv
->cpuid_ext_features
& CPUID_EXT_HYPERVISOR
;
1292 kvm_trim_features(&cenv
->cpuid_ext_features
,
1293 kvm_arch_get_supported_cpuid(cenv
, 1, R_ECX
));
1294 cenv
->cpuid_ext_features
|= i
;
1296 kvm_trim_features(&cenv
->cpuid_ext2_features
,
1297 kvm_arch_get_supported_cpuid(cenv
, 0x80000001, R_EDX
));
1298 kvm_trim_features(&cenv
->cpuid_ext3_features
,
1299 kvm_arch_get_supported_cpuid(cenv
, 0x80000001, R_ECX
));
1303 copy
.regs
[R_EAX
] = 0;
1304 qemu_kvm_cpuid_on_env(©
);
1305 limit
= copy
.regs
[R_EAX
];
1307 for (i
= 0; i
<= limit
; ++i
) {
1308 if (i
== 4 || i
== 0xb || i
== 0xd) {
1309 for (j
= 0; ; ++j
) {
1310 do_cpuid_ent(&cpuid_ent
[cpuid_nent
], i
, j
, ©
);
1312 cpuid_ent
[cpuid_nent
].flags
= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1313 cpuid_ent
[cpuid_nent
].index
= j
;
1317 if (i
== 4 && copy
.regs
[R_EAX
] == 0)
1319 if (i
== 0xb && !(copy
.regs
[R_ECX
] & 0xff00))
1321 if (i
== 0xd && copy
.regs
[R_EAX
] == 0)
1325 do_cpuid_ent(&cpuid_ent
[cpuid_nent
++], i
, 0, ©
);
1328 copy
.regs
[R_EAX
] = 0x80000000;
1329 qemu_kvm_cpuid_on_env(©
);
1330 limit
= copy
.regs
[R_EAX
];
1332 for (i
= 0x80000000; i
<= limit
; ++i
)
1333 do_cpuid_ent(&cpuid_ent
[cpuid_nent
++], i
, 0, ©
);
1335 kvm_setup_cpuid2(cenv
->kvm_cpu_state
.vcpu_ctx
, cpuid_nent
, cpuid_ent
);
1338 if (((cenv
->cpuid_version
>> 8)&0xF) >= 6
1339 && (cenv
->cpuid_features
&(CPUID_MCE
|CPUID_MCA
)) == (CPUID_MCE
|CPUID_MCA
)
1340 && kvm_check_extension(kvm_state
, KVM_CAP_MCE
) > 0) {
1344 if (kvm_get_mce_cap_supported(kvm_context
, &mcg_cap
, &banks
))
1345 perror("kvm_get_mce_cap_supported FAILED");
1347 if (banks
> MCE_BANKS_DEF
)
1348 banks
= MCE_BANKS_DEF
;
1349 mcg_cap
&= MCE_CAP_DEF
;
1351 if (kvm_setup_mce(cenv
->kvm_cpu_state
.vcpu_ctx
, &mcg_cap
))
1352 perror("kvm_setup_mce FAILED");
1354 cenv
->mcg_cap
= mcg_cap
;
1362 int kvm_arch_halt(kvm_vcpu_context_t vcpu
)
1364 CPUState
*env
= cpu_single_env
;
1366 if (!((env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
1367 (env
->eflags
& IF_MASK
)) &&
1368 !(env
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
1374 void kvm_arch_pre_kvm_run(void *opaque
, CPUState
*env
)
1376 if (!kvm_irqchip_in_kernel())
1377 kvm_set_cr8(env
, cpu_get_apic_tpr(env
));
1380 void kvm_arch_post_kvm_run(void *opaque
, CPUState
*env
)
1382 cpu_single_env
= env
;
1384 env
->eflags
= kvm_get_interrupt_flag(env
)
1385 ? env
->eflags
| IF_MASK
: env
->eflags
& ~IF_MASK
;
1387 cpu_set_apic_tpr(env
, kvm_get_cr8(env
));
1388 cpu_set_apic_base(env
, kvm_get_apic_base(env
));
1391 int kvm_arch_has_work(CPUState
*env
)
1393 if (((env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
1394 (env
->eflags
& IF_MASK
)) ||
1395 (env
->interrupt_request
& CPU_INTERRUPT_NMI
))
1400 int kvm_arch_try_push_interrupts(void *opaque
)
1402 CPUState
*env
= cpu_single_env
;
1405 if (kvm_is_ready_for_interrupt_injection(env
) &&
1406 (env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
1407 (env
->eflags
& IF_MASK
)) {
1408 env
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
1409 irq
= cpu_get_pic_interrupt(env
);
1411 r
= kvm_inject_irq(env
->kvm_cpu_state
.vcpu_ctx
, irq
);
1413 printf("cpu %d fail inject %x\n", env
->cpu_index
, irq
);
1417 return (env
->interrupt_request
& CPU_INTERRUPT_HARD
) != 0;
1420 #ifdef KVM_CAP_USER_NMI
1421 void kvm_arch_push_nmi(void *opaque
)
1423 CPUState
*env
= cpu_single_env
;
1426 if (likely(!(env
->interrupt_request
& CPU_INTERRUPT_NMI
)))
1429 env
->interrupt_request
&= ~CPU_INTERRUPT_NMI
;
1430 r
= kvm_inject_nmi(env
->kvm_cpu_state
.vcpu_ctx
);
1432 printf("cpu %d fail inject NMI\n", env
->cpu_index
);
1434 #endif /* KVM_CAP_USER_NMI */
1436 void kvm_arch_cpu_reset(CPUState
*env
)
1438 kvm_arch_load_regs(env
);
1439 if (!cpu_is_bsp(env
)) {
1440 if (kvm_irqchip_in_kernel()) {
1441 #ifdef KVM_CAP_MP_STATE
1442 kvm_reset_mpstate(env
->kvm_cpu_state
.vcpu_ctx
);
1445 env
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
1451 int kvm_arch_insert_sw_breakpoint(CPUState
*env
, struct kvm_sw_breakpoint
*bp
)
1453 uint8_t int3
= 0xcc;
1455 if (cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 0) ||
1456 cpu_memory_rw_debug(env
, bp
->pc
, &int3
, 1, 1))
1461 int kvm_arch_remove_sw_breakpoint(CPUState
*env
, struct kvm_sw_breakpoint
*bp
)
1465 if (cpu_memory_rw_debug(env
, bp
->pc
, &int3
, 1, 0) || int3
!= 0xcc ||
1466 cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 1))
1471 #ifdef KVM_CAP_SET_GUEST_DEBUG
1478 static int nb_hw_breakpoint
;
1480 static int find_hw_breakpoint(target_ulong addr
, int len
, int type
)
1484 for (n
= 0; n
< nb_hw_breakpoint
; n
++)
1485 if (hw_breakpoint
[n
].addr
== addr
&& hw_breakpoint
[n
].type
== type
&&
1486 (hw_breakpoint
[n
].len
== len
|| len
== -1))
1491 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
1492 target_ulong len
, int type
)
1495 case GDB_BREAKPOINT_HW
:
1498 case GDB_WATCHPOINT_WRITE
:
1499 case GDB_WATCHPOINT_ACCESS
:
1506 if (addr
& (len
- 1))
1517 if (nb_hw_breakpoint
== 4)
1520 if (find_hw_breakpoint(addr
, len
, type
) >= 0)
1523 hw_breakpoint
[nb_hw_breakpoint
].addr
= addr
;
1524 hw_breakpoint
[nb_hw_breakpoint
].len
= len
;
1525 hw_breakpoint
[nb_hw_breakpoint
].type
= type
;
1531 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
1532 target_ulong len
, int type
)
1536 n
= find_hw_breakpoint(addr
, (type
== GDB_BREAKPOINT_HW
) ? 1 : len
, type
);
1541 hw_breakpoint
[n
] = hw_breakpoint
[nb_hw_breakpoint
];
1546 void kvm_arch_remove_all_hw_breakpoints(void)
1548 nb_hw_breakpoint
= 0;
1551 static CPUWatchpoint hw_watchpoint
;
1553 int kvm_arch_debug(struct kvm_debug_exit_arch
*arch_info
)
1558 if (arch_info
->exception
== 1) {
1559 if (arch_info
->dr6
& (1 << 14)) {
1560 if (cpu_single_env
->singlestep_enabled
)
1563 for (n
= 0; n
< 4; n
++)
1564 if (arch_info
->dr6
& (1 << n
))
1565 switch ((arch_info
->dr7
>> (16 + n
*4)) & 0x3) {
1571 cpu_single_env
->watchpoint_hit
= &hw_watchpoint
;
1572 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
1573 hw_watchpoint
.flags
= BP_MEM_WRITE
;
1577 cpu_single_env
->watchpoint_hit
= &hw_watchpoint
;
1578 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
1579 hw_watchpoint
.flags
= BP_MEM_ACCESS
;
1583 } else if (kvm_find_sw_breakpoint(cpu_single_env
, arch_info
->pc
))
1587 kvm_update_guest_debug(cpu_single_env
,
1588 (arch_info
->exception
== 1) ?
1589 KVM_GUESTDBG_INJECT_DB
: KVM_GUESTDBG_INJECT_BP
);
1594 void kvm_arch_update_guest_debug(CPUState
*env
, struct kvm_guest_debug
*dbg
)
1596 const uint8_t type_code
[] = {
1597 [GDB_BREAKPOINT_HW
] = 0x0,
1598 [GDB_WATCHPOINT_WRITE
] = 0x1,
1599 [GDB_WATCHPOINT_ACCESS
] = 0x3
1601 const uint8_t len_code
[] = {
1602 [1] = 0x0, [2] = 0x1, [4] = 0x3, [8] = 0x2
1606 if (kvm_sw_breakpoints_active(env
))
1607 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_SW_BP
;
1609 if (nb_hw_breakpoint
> 0) {
1610 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
;
1611 dbg
->arch
.debugreg
[7] = 0x0600;
1612 for (n
= 0; n
< nb_hw_breakpoint
; n
++) {
1613 dbg
->arch
.debugreg
[n
] = hw_breakpoint
[n
].addr
;
1614 dbg
->arch
.debugreg
[7] |= (2 << (n
* 2)) |
1615 (type_code
[hw_breakpoint
[n
].type
] << (16 + n
*4)) |
1616 (len_code
[hw_breakpoint
[n
].len
] << (18 + n
*4));
1622 #ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
1623 void kvm_arch_do_ioperm(void *_data
)
1625 struct ioperm_data
*data
= _data
;
1626 ioperm(data
->start_port
, data
->num
, data
->turn_on
);
1631 * Setup x86 specific IRQ routing
1633 int kvm_arch_init_irq_routing(void)
1637 if (kvm_irqchip
&& kvm_has_gsi_routing(kvm_context
)) {
1638 kvm_clear_gsi_routes(kvm_context
);
1639 for (i
= 0; i
< 8; ++i
) {
1642 r
= kvm_add_irq_route(kvm_context
, i
, KVM_IRQCHIP_PIC_MASTER
, i
);
1646 for (i
= 8; i
< 16; ++i
) {
1647 r
= kvm_add_irq_route(kvm_context
, i
, KVM_IRQCHIP_PIC_SLAVE
, i
- 8);
1651 for (i
= 0; i
< 24; ++i
) {
1653 r
= kvm_add_irq_route(kvm_context
, i
, KVM_IRQCHIP_IOAPIC
, 2);
1654 } else if (i
!= 2) {
1655 r
= kvm_add_irq_route(kvm_context
, i
, KVM_IRQCHIP_IOAPIC
, i
);
1660 kvm_commit_irq_routes(kvm_context
);
1665 uint32_t kvm_arch_get_supported_cpuid(CPUState
*env
, uint32_t function
,
1668 return kvm_get_supported_cpuid(kvm_context
, function
, reg
);
1671 void kvm_arch_process_irqchip_events(CPUState
*env
)
1673 if (env
->interrupt_request
& CPU_INTERRUPT_INIT
) {
1674 kvm_cpu_synchronize_state(env
);
1677 if (env
->interrupt_request
& CPU_INTERRUPT_SIPI
) {
1678 kvm_cpu_synchronize_state(env
);