4 * Copyright (C) 2006-2008 Qumranet Technologies
5 * Copyright IBM, Corp. 2008
8 * Anthony Liguori <aliguori@us.ibm.com>
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
15 #include <sys/types.h>
16 #include <sys/ioctl.h>
18 #include <sys/utsname.h>
20 #include <linux/kvm.h>
21 #include <linux/kvm_para.h>
23 #include "qemu-common.h"
24 #include "sysemu/sysemu.h"
25 #include "sysemu/kvm.h"
28 #include "exec/gdbstub.h"
29 #include "qemu/host-utils.h"
30 #include "qemu/config-file.h"
31 #include "hw/i386/pc.h"
32 #include "hw/i386/apic.h"
33 #include "exec/ioport.h"
34 #include <asm/hyperv.h>
35 #include "hw/pci/pci.h"
40 #define DPRINTF(fmt, ...) \
41 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
43 #define DPRINTF(fmt, ...) \
47 #define MSR_KVM_WALL_CLOCK 0x11
48 #define MSR_KVM_SYSTEM_TIME 0x12
51 #define BUS_MCEERR_AR 4
54 #define BUS_MCEERR_AO 5
57 const KVMCapabilityInfo kvm_arch_required_capabilities
[] = {
58 KVM_CAP_INFO(SET_TSS_ADDR
),
59 KVM_CAP_INFO(EXT_CPUID
),
60 KVM_CAP_INFO(MP_STATE
),
64 static bool has_msr_star
;
65 static bool has_msr_hsave_pa
;
66 static bool has_msr_tsc_adjust
;
67 static bool has_msr_tsc_deadline
;
68 static bool has_msr_feature_control
;
69 static bool has_msr_async_pf_en
;
70 static bool has_msr_pv_eoi_en
;
71 static bool has_msr_misc_enable
;
72 static bool has_msr_bndcfgs
;
73 static bool has_msr_kvm_steal_time
;
74 static int lm_capable_kernel
;
75 static bool has_msr_hv_hypercall
;
76 static bool has_msr_hv_vapic
;
77 static bool has_msr_hv_tsc
;
79 static bool has_msr_architectural_pmu
;
80 static uint32_t num_architectural_pmu_counters
;
82 bool kvm_allows_irq0_override(void)
84 return !kvm_irqchip_in_kernel() || kvm_has_gsi_routing();
87 static struct kvm_cpuid2
*try_get_cpuid(KVMState
*s
, int max
)
89 struct kvm_cpuid2
*cpuid
;
92 size
= sizeof(*cpuid
) + max
* sizeof(*cpuid
->entries
);
93 cpuid
= (struct kvm_cpuid2
*)g_malloc0(size
);
95 r
= kvm_ioctl(s
, KVM_GET_SUPPORTED_CPUID
, cpuid
);
96 if (r
== 0 && cpuid
->nent
>= max
) {
104 fprintf(stderr
, "KVM_GET_SUPPORTED_CPUID failed: %s\n",
112 /* Run KVM_GET_SUPPORTED_CPUID ioctl(), allocating a buffer large enough
115 static struct kvm_cpuid2
*get_supported_cpuid(KVMState
*s
)
117 struct kvm_cpuid2
*cpuid
;
119 while ((cpuid
= try_get_cpuid(s
, max
)) == NULL
) {
125 static const struct kvm_para_features
{
128 } para_features
[] = {
129 { KVM_CAP_CLOCKSOURCE
, KVM_FEATURE_CLOCKSOURCE
},
130 { KVM_CAP_NOP_IO_DELAY
, KVM_FEATURE_NOP_IO_DELAY
},
131 { KVM_CAP_PV_MMU
, KVM_FEATURE_MMU_OP
},
132 { KVM_CAP_ASYNC_PF
, KVM_FEATURE_ASYNC_PF
},
136 static int get_para_features(KVMState
*s
)
140 for (i
= 0; i
< ARRAY_SIZE(para_features
) - 1; i
++) {
141 if (kvm_check_extension(s
, para_features
[i
].cap
)) {
142 features
|= (1 << para_features
[i
].feature
);
150 /* Returns the value for a specific register on the cpuid entry
152 static uint32_t cpuid_entry_get_reg(struct kvm_cpuid_entry2
*entry
, int reg
)
172 /* Find matching entry for function/index on kvm_cpuid2 struct
174 static struct kvm_cpuid_entry2
*cpuid_find_entry(struct kvm_cpuid2
*cpuid
,
179 for (i
= 0; i
< cpuid
->nent
; ++i
) {
180 if (cpuid
->entries
[i
].function
== function
&&
181 cpuid
->entries
[i
].index
== index
) {
182 return &cpuid
->entries
[i
];
189 uint32_t kvm_arch_get_supported_cpuid(KVMState
*s
, uint32_t function
,
190 uint32_t index
, int reg
)
192 struct kvm_cpuid2
*cpuid
;
194 uint32_t cpuid_1_edx
;
197 cpuid
= get_supported_cpuid(s
);
199 struct kvm_cpuid_entry2
*entry
= cpuid_find_entry(cpuid
, function
, index
);
202 ret
= cpuid_entry_get_reg(entry
, reg
);
205 /* Fixups for the data returned by KVM, below */
207 if (function
== 1 && reg
== R_EDX
) {
208 /* KVM before 2.6.30 misreports the following features */
209 ret
|= CPUID_MTRR
| CPUID_PAT
| CPUID_MCE
| CPUID_MCA
;
210 } else if (function
== 1 && reg
== R_ECX
) {
211 /* We can set the hypervisor flag, even if KVM does not return it on
212 * GET_SUPPORTED_CPUID
214 ret
|= CPUID_EXT_HYPERVISOR
;
215 /* tsc-deadline flag is not returned by GET_SUPPORTED_CPUID, but it
216 * can be enabled if the kernel has KVM_CAP_TSC_DEADLINE_TIMER,
217 * and the irqchip is in the kernel.
219 if (kvm_irqchip_in_kernel() &&
220 kvm_check_extension(s
, KVM_CAP_TSC_DEADLINE_TIMER
)) {
221 ret
|= CPUID_EXT_TSC_DEADLINE_TIMER
;
224 /* x2apic is reported by GET_SUPPORTED_CPUID, but it can't be enabled
225 * without the in-kernel irqchip
227 if (!kvm_irqchip_in_kernel()) {
228 ret
&= ~CPUID_EXT_X2APIC
;
230 } else if (function
== 0x80000001 && reg
== R_EDX
) {
231 /* On Intel, kvm returns cpuid according to the Intel spec,
232 * so add missing bits according to the AMD spec:
234 cpuid_1_edx
= kvm_arch_get_supported_cpuid(s
, 1, 0, R_EDX
);
235 ret
|= cpuid_1_edx
& CPUID_EXT2_AMD_ALIASES
;
240 /* fallback for older kernels */
241 if ((function
== KVM_CPUID_FEATURES
) && !found
) {
242 ret
= get_para_features(s
);
248 typedef struct HWPoisonPage
{
250 QLIST_ENTRY(HWPoisonPage
) list
;
253 static QLIST_HEAD(, HWPoisonPage
) hwpoison_page_list
=
254 QLIST_HEAD_INITIALIZER(hwpoison_page_list
);
256 static void kvm_unpoison_all(void *param
)
258 HWPoisonPage
*page
, *next_page
;
260 QLIST_FOREACH_SAFE(page
, &hwpoison_page_list
, list
, next_page
) {
261 QLIST_REMOVE(page
, list
);
262 qemu_ram_remap(page
->ram_addr
, TARGET_PAGE_SIZE
);
267 static void kvm_hwpoison_page_add(ram_addr_t ram_addr
)
271 QLIST_FOREACH(page
, &hwpoison_page_list
, list
) {
272 if (page
->ram_addr
== ram_addr
) {
276 page
= g_malloc(sizeof(HWPoisonPage
));
277 page
->ram_addr
= ram_addr
;
278 QLIST_INSERT_HEAD(&hwpoison_page_list
, page
, list
);
281 static int kvm_get_mce_cap_supported(KVMState
*s
, uint64_t *mce_cap
,
286 r
= kvm_check_extension(s
, KVM_CAP_MCE
);
289 return kvm_ioctl(s
, KVM_X86_GET_MCE_CAP_SUPPORTED
, mce_cap
);
294 static void kvm_mce_inject(X86CPU
*cpu
, hwaddr paddr
, int code
)
296 CPUX86State
*env
= &cpu
->env
;
297 uint64_t status
= MCI_STATUS_VAL
| MCI_STATUS_UC
| MCI_STATUS_EN
|
298 MCI_STATUS_MISCV
| MCI_STATUS_ADDRV
| MCI_STATUS_S
;
299 uint64_t mcg_status
= MCG_STATUS_MCIP
;
301 if (code
== BUS_MCEERR_AR
) {
302 status
|= MCI_STATUS_AR
| 0x134;
303 mcg_status
|= MCG_STATUS_EIPV
;
306 mcg_status
|= MCG_STATUS_RIPV
;
308 cpu_x86_inject_mce(NULL
, cpu
, 9, status
, mcg_status
, paddr
,
309 (MCM_ADDR_PHYS
<< 6) | 0xc,
310 cpu_x86_support_mca_broadcast(env
) ?
311 MCE_INJECT_BROADCAST
: 0);
314 static void hardware_memory_error(void)
316 fprintf(stderr
, "Hardware memory error!\n");
320 int kvm_arch_on_sigbus_vcpu(CPUState
*c
, int code
, void *addr
)
322 X86CPU
*cpu
= X86_CPU(c
);
323 CPUX86State
*env
= &cpu
->env
;
327 if ((env
->mcg_cap
& MCG_SER_P
) && addr
328 && (code
== BUS_MCEERR_AR
|| code
== BUS_MCEERR_AO
)) {
329 if (qemu_ram_addr_from_host(addr
, &ram_addr
) == NULL
||
330 !kvm_physical_memory_addr_from_host(c
->kvm_state
, addr
, &paddr
)) {
331 fprintf(stderr
, "Hardware memory error for memory used by "
332 "QEMU itself instead of guest system!\n");
333 /* Hope we are lucky for AO MCE */
334 if (code
== BUS_MCEERR_AO
) {
337 hardware_memory_error();
340 kvm_hwpoison_page_add(ram_addr
);
341 kvm_mce_inject(cpu
, paddr
, code
);
343 if (code
== BUS_MCEERR_AO
) {
345 } else if (code
== BUS_MCEERR_AR
) {
346 hardware_memory_error();
354 int kvm_arch_on_sigbus(int code
, void *addr
)
356 X86CPU
*cpu
= X86_CPU(first_cpu
);
358 if ((cpu
->env
.mcg_cap
& MCG_SER_P
) && addr
&& code
== BUS_MCEERR_AO
) {
362 /* Hope we are lucky for AO MCE */
363 if (qemu_ram_addr_from_host(addr
, &ram_addr
) == NULL
||
364 !kvm_physical_memory_addr_from_host(first_cpu
->kvm_state
,
366 fprintf(stderr
, "Hardware memory error for memory used by "
367 "QEMU itself instead of guest system!: %p\n", addr
);
370 kvm_hwpoison_page_add(ram_addr
);
371 kvm_mce_inject(X86_CPU(first_cpu
), paddr
, code
);
373 if (code
== BUS_MCEERR_AO
) {
375 } else if (code
== BUS_MCEERR_AR
) {
376 hardware_memory_error();
384 static int kvm_inject_mce_oldstyle(X86CPU
*cpu
)
386 CPUX86State
*env
= &cpu
->env
;
388 if (!kvm_has_vcpu_events() && env
->exception_injected
== EXCP12_MCHK
) {
389 unsigned int bank
, bank_num
= env
->mcg_cap
& 0xff;
390 struct kvm_x86_mce mce
;
392 env
->exception_injected
= -1;
395 * There must be at least one bank in use if an MCE is pending.
396 * Find it and use its values for the event injection.
398 for (bank
= 0; bank
< bank_num
; bank
++) {
399 if (env
->mce_banks
[bank
* 4 + 1] & MCI_STATUS_VAL
) {
403 assert(bank
< bank_num
);
406 mce
.status
= env
->mce_banks
[bank
* 4 + 1];
407 mce
.mcg_status
= env
->mcg_status
;
408 mce
.addr
= env
->mce_banks
[bank
* 4 + 2];
409 mce
.misc
= env
->mce_banks
[bank
* 4 + 3];
411 return kvm_vcpu_ioctl(CPU(cpu
), KVM_X86_SET_MCE
, &mce
);
416 static void cpu_update_state(void *opaque
, int running
, RunState state
)
418 CPUX86State
*env
= opaque
;
421 env
->tsc_valid
= false;
425 unsigned long kvm_arch_vcpu_id(CPUState
*cs
)
427 X86CPU
*cpu
= X86_CPU(cs
);
428 return cpu
->env
.cpuid_apic_id
;
431 #ifndef KVM_CPUID_SIGNATURE_NEXT
432 #define KVM_CPUID_SIGNATURE_NEXT 0x40000100
435 static bool hyperv_hypercall_available(X86CPU
*cpu
)
437 return cpu
->hyperv_vapic
||
438 (cpu
->hyperv_spinlock_attempts
!= HYPERV_SPINLOCK_NEVER_RETRY
);
441 static bool hyperv_enabled(X86CPU
*cpu
)
443 CPUState
*cs
= CPU(cpu
);
444 return kvm_check_extension(cs
->kvm_state
, KVM_CAP_HYPERV
) > 0 &&
445 (hyperv_hypercall_available(cpu
) ||
447 cpu
->hyperv_relaxed_timing
);
450 #define KVM_MAX_CPUID_ENTRIES 100
452 int kvm_arch_init_vcpu(CPUState
*cs
)
455 struct kvm_cpuid2 cpuid
;
456 struct kvm_cpuid_entry2 entries
[KVM_MAX_CPUID_ENTRIES
];
457 } QEMU_PACKED cpuid_data
;
458 X86CPU
*cpu
= X86_CPU(cs
);
459 CPUX86State
*env
= &cpu
->env
;
460 uint32_t limit
, i
, j
, cpuid_i
;
462 struct kvm_cpuid_entry2
*c
;
463 uint32_t signature
[3];
464 int kvm_base
= KVM_CPUID_SIGNATURE
;
467 memset(&cpuid_data
, 0, sizeof(cpuid_data
));
471 /* Paravirtualization CPUIDs */
472 if (hyperv_enabled(cpu
)) {
473 c
= &cpuid_data
.entries
[cpuid_i
++];
474 c
->function
= HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS
;
475 memcpy(signature
, "Microsoft Hv", 12);
476 c
->eax
= HYPERV_CPUID_MIN
;
477 c
->ebx
= signature
[0];
478 c
->ecx
= signature
[1];
479 c
->edx
= signature
[2];
481 c
= &cpuid_data
.entries
[cpuid_i
++];
482 c
->function
= HYPERV_CPUID_INTERFACE
;
483 memcpy(signature
, "Hv#1\0\0\0\0\0\0\0\0", 12);
484 c
->eax
= signature
[0];
489 c
= &cpuid_data
.entries
[cpuid_i
++];
490 c
->function
= HYPERV_CPUID_VERSION
;
494 c
= &cpuid_data
.entries
[cpuid_i
++];
495 c
->function
= HYPERV_CPUID_FEATURES
;
496 if (cpu
->hyperv_relaxed_timing
) {
497 c
->eax
|= HV_X64_MSR_HYPERCALL_AVAILABLE
;
499 if (cpu
->hyperv_vapic
) {
500 c
->eax
|= HV_X64_MSR_HYPERCALL_AVAILABLE
;
501 c
->eax
|= HV_X64_MSR_APIC_ACCESS_AVAILABLE
;
502 has_msr_hv_vapic
= true;
504 if (cpu
->hyperv_time
&&
505 kvm_check_extension(cs
->kvm_state
, KVM_CAP_HYPERV_TIME
) > 0) {
506 c
->eax
|= HV_X64_MSR_HYPERCALL_AVAILABLE
;
507 c
->eax
|= HV_X64_MSR_TIME_REF_COUNT_AVAILABLE
;
509 has_msr_hv_tsc
= true;
511 c
= &cpuid_data
.entries
[cpuid_i
++];
512 c
->function
= HYPERV_CPUID_ENLIGHTMENT_INFO
;
513 if (cpu
->hyperv_relaxed_timing
) {
514 c
->eax
|= HV_X64_RELAXED_TIMING_RECOMMENDED
;
516 if (has_msr_hv_vapic
) {
517 c
->eax
|= HV_X64_APIC_ACCESS_RECOMMENDED
;
519 c
->ebx
= cpu
->hyperv_spinlock_attempts
;
521 c
= &cpuid_data
.entries
[cpuid_i
++];
522 c
->function
= HYPERV_CPUID_IMPLEMENT_LIMITS
;
526 kvm_base
= KVM_CPUID_SIGNATURE_NEXT
;
527 has_msr_hv_hypercall
= true;
530 memcpy(signature
, "KVMKVMKVM\0\0\0", 12);
531 c
= &cpuid_data
.entries
[cpuid_i
++];
532 c
->function
= KVM_CPUID_SIGNATURE
| kvm_base
;
534 c
->ebx
= signature
[0];
535 c
->ecx
= signature
[1];
536 c
->edx
= signature
[2];
538 c
= &cpuid_data
.entries
[cpuid_i
++];
539 c
->function
= KVM_CPUID_FEATURES
| kvm_base
;
540 c
->eax
= env
->features
[FEAT_KVM
];
542 has_msr_async_pf_en
= c
->eax
& (1 << KVM_FEATURE_ASYNC_PF
);
544 has_msr_pv_eoi_en
= c
->eax
& (1 << KVM_FEATURE_PV_EOI
);
546 has_msr_kvm_steal_time
= c
->eax
& (1 << KVM_FEATURE_STEAL_TIME
);
548 cpu_x86_cpuid(env
, 0, 0, &limit
, &unused
, &unused
, &unused
);
550 for (i
= 0; i
<= limit
; i
++) {
551 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
552 fprintf(stderr
, "unsupported level value: 0x%x\n", limit
);
555 c
= &cpuid_data
.entries
[cpuid_i
++];
559 /* Keep reading function 2 till all the input is received */
563 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
|
564 KVM_CPUID_FLAG_STATE_READ_NEXT
;
565 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
566 times
= c
->eax
& 0xff;
568 for (j
= 1; j
< times
; ++j
) {
569 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
570 fprintf(stderr
, "cpuid_data is full, no space for "
571 "cpuid(eax:2):eax & 0xf = 0x%x\n", times
);
574 c
= &cpuid_data
.entries
[cpuid_i
++];
576 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
;
577 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
585 if (i
== 0xd && j
== 64) {
589 c
->flags
= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
591 cpu_x86_cpuid(env
, i
, j
, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
593 if (i
== 4 && c
->eax
== 0) {
596 if (i
== 0xb && !(c
->ecx
& 0xff00)) {
599 if (i
== 0xd && c
->eax
== 0) {
602 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
603 fprintf(stderr
, "cpuid_data is full, no space for "
604 "cpuid(eax:0x%x,ecx:0x%x)\n", i
, j
);
607 c
= &cpuid_data
.entries
[cpuid_i
++];
613 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
621 cpu_x86_cpuid(env
, 0x0a, 0, &ver
, &unused
, &unused
, &unused
);
622 if ((ver
& 0xff) > 0) {
623 has_msr_architectural_pmu
= true;
624 num_architectural_pmu_counters
= (ver
& 0xff00) >> 8;
626 /* Shouldn't be more than 32, since that's the number of bits
627 * available in EBX to tell us _which_ counters are available.
630 if (num_architectural_pmu_counters
> MAX_GP_COUNTERS
) {
631 num_architectural_pmu_counters
= MAX_GP_COUNTERS
;
636 cpu_x86_cpuid(env
, 0x80000000, 0, &limit
, &unused
, &unused
, &unused
);
638 for (i
= 0x80000000; i
<= limit
; i
++) {
639 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
640 fprintf(stderr
, "unsupported xlevel value: 0x%x\n", limit
);
643 c
= &cpuid_data
.entries
[cpuid_i
++];
647 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
650 /* Call Centaur's CPUID instructions they are supported. */
651 if (env
->cpuid_xlevel2
> 0) {
652 cpu_x86_cpuid(env
, 0xC0000000, 0, &limit
, &unused
, &unused
, &unused
);
654 for (i
= 0xC0000000; i
<= limit
; i
++) {
655 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
656 fprintf(stderr
, "unsupported xlevel2 value: 0x%x\n", limit
);
659 c
= &cpuid_data
.entries
[cpuid_i
++];
663 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
667 cpuid_data
.cpuid
.nent
= cpuid_i
;
669 if (((env
->cpuid_version
>> 8)&0xF) >= 6
670 && (env
->features
[FEAT_1_EDX
] & (CPUID_MCE
| CPUID_MCA
)) ==
671 (CPUID_MCE
| CPUID_MCA
)
672 && kvm_check_extension(cs
->kvm_state
, KVM_CAP_MCE
) > 0) {
677 ret
= kvm_get_mce_cap_supported(cs
->kvm_state
, &mcg_cap
, &banks
);
679 fprintf(stderr
, "kvm_get_mce_cap_supported: %s", strerror(-ret
));
683 if (banks
> MCE_BANKS_DEF
) {
684 banks
= MCE_BANKS_DEF
;
686 mcg_cap
&= MCE_CAP_DEF
;
688 ret
= kvm_vcpu_ioctl(cs
, KVM_X86_SETUP_MCE
, &mcg_cap
);
690 fprintf(stderr
, "KVM_X86_SETUP_MCE: %s", strerror(-ret
));
694 env
->mcg_cap
= mcg_cap
;
697 qemu_add_vm_change_state_handler(cpu_update_state
, env
);
699 c
= cpuid_find_entry(&cpuid_data
.cpuid
, 1, 0);
701 has_msr_feature_control
= !!(c
->ecx
& CPUID_EXT_VMX
) ||
702 !!(c
->ecx
& CPUID_EXT_SMX
);
705 cpuid_data
.cpuid
.padding
= 0;
706 r
= kvm_vcpu_ioctl(cs
, KVM_SET_CPUID2
, &cpuid_data
);
711 r
= kvm_check_extension(cs
->kvm_state
, KVM_CAP_TSC_CONTROL
);
712 if (r
&& env
->tsc_khz
) {
713 r
= kvm_vcpu_ioctl(cs
, KVM_SET_TSC_KHZ
, env
->tsc_khz
);
715 fprintf(stderr
, "KVM_SET_TSC_KHZ failed\n");
720 if (kvm_has_xsave()) {
721 env
->kvm_xsave_buf
= qemu_memalign(4096, sizeof(struct kvm_xsave
));
727 void kvm_arch_reset_vcpu(CPUState
*cs
)
729 X86CPU
*cpu
= X86_CPU(cs
);
730 CPUX86State
*env
= &cpu
->env
;
732 env
->exception_injected
= -1;
733 env
->interrupt_injected
= -1;
735 if (kvm_irqchip_in_kernel()) {
736 env
->mp_state
= cpu_is_bsp(cpu
) ? KVM_MP_STATE_RUNNABLE
:
737 KVM_MP_STATE_UNINITIALIZED
;
739 env
->mp_state
= KVM_MP_STATE_RUNNABLE
;
743 static int kvm_get_supported_msrs(KVMState
*s
)
745 static int kvm_supported_msrs
;
749 if (kvm_supported_msrs
== 0) {
750 struct kvm_msr_list msr_list
, *kvm_msr_list
;
752 kvm_supported_msrs
= -1;
754 /* Obtain MSR list from KVM. These are the MSRs that we must
757 ret
= kvm_ioctl(s
, KVM_GET_MSR_INDEX_LIST
, &msr_list
);
758 if (ret
< 0 && ret
!= -E2BIG
) {
761 /* Old kernel modules had a bug and could write beyond the provided
762 memory. Allocate at least a safe amount of 1K. */
763 kvm_msr_list
= g_malloc0(MAX(1024, sizeof(msr_list
) +
765 sizeof(msr_list
.indices
[0])));
767 kvm_msr_list
->nmsrs
= msr_list
.nmsrs
;
768 ret
= kvm_ioctl(s
, KVM_GET_MSR_INDEX_LIST
, kvm_msr_list
);
772 for (i
= 0; i
< kvm_msr_list
->nmsrs
; i
++) {
773 if (kvm_msr_list
->indices
[i
] == MSR_STAR
) {
777 if (kvm_msr_list
->indices
[i
] == MSR_VM_HSAVE_PA
) {
778 has_msr_hsave_pa
= true;
781 if (kvm_msr_list
->indices
[i
] == MSR_TSC_ADJUST
) {
782 has_msr_tsc_adjust
= true;
785 if (kvm_msr_list
->indices
[i
] == MSR_IA32_TSCDEADLINE
) {
786 has_msr_tsc_deadline
= true;
789 if (kvm_msr_list
->indices
[i
] == MSR_IA32_MISC_ENABLE
) {
790 has_msr_misc_enable
= true;
793 if (kvm_msr_list
->indices
[i
] == MSR_IA32_BNDCFGS
) {
794 has_msr_bndcfgs
= true;
800 g_free(kvm_msr_list
);
806 int kvm_arch_init(KVMState
*s
)
808 uint64_t identity_base
= 0xfffbc000;
811 struct utsname utsname
;
813 ret
= kvm_get_supported_msrs(s
);
819 lm_capable_kernel
= strcmp(utsname
.machine
, "x86_64") == 0;
822 * On older Intel CPUs, KVM uses vm86 mode to emulate 16-bit code directly.
823 * In order to use vm86 mode, an EPT identity map and a TSS are needed.
824 * Since these must be part of guest physical memory, we need to allocate
825 * them, both by setting their start addresses in the kernel and by
826 * creating a corresponding e820 entry. We need 4 pages before the BIOS.
828 * Older KVM versions may not support setting the identity map base. In
829 * that case we need to stick with the default, i.e. a 256K maximum BIOS
832 if (kvm_check_extension(s
, KVM_CAP_SET_IDENTITY_MAP_ADDR
)) {
833 /* Allows up to 16M BIOSes. */
834 identity_base
= 0xfeffc000;
836 ret
= kvm_vm_ioctl(s
, KVM_SET_IDENTITY_MAP_ADDR
, &identity_base
);
842 /* Set TSS base one page after EPT identity map. */
843 ret
= kvm_vm_ioctl(s
, KVM_SET_TSS_ADDR
, identity_base
+ 0x1000);
848 /* Tell fw_cfg to notify the BIOS to reserve the range. */
849 ret
= e820_add_entry(identity_base
, 0x4000, E820_RESERVED
);
851 fprintf(stderr
, "e820_add_entry() table is full\n");
854 qemu_register_reset(kvm_unpoison_all
, NULL
);
856 shadow_mem
= qemu_opt_get_size(qemu_get_machine_opts(),
857 "kvm_shadow_mem", -1);
858 if (shadow_mem
!= -1) {
860 ret
= kvm_vm_ioctl(s
, KVM_SET_NR_MMU_PAGES
, shadow_mem
);
868 static void set_v8086_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
870 lhs
->selector
= rhs
->selector
;
871 lhs
->base
= rhs
->base
;
872 lhs
->limit
= rhs
->limit
;
884 static void set_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
886 unsigned flags
= rhs
->flags
;
887 lhs
->selector
= rhs
->selector
;
888 lhs
->base
= rhs
->base
;
889 lhs
->limit
= rhs
->limit
;
890 lhs
->type
= (flags
>> DESC_TYPE_SHIFT
) & 15;
891 lhs
->present
= (flags
& DESC_P_MASK
) != 0;
892 lhs
->dpl
= (flags
>> DESC_DPL_SHIFT
) & 3;
893 lhs
->db
= (flags
>> DESC_B_SHIFT
) & 1;
894 lhs
->s
= (flags
& DESC_S_MASK
) != 0;
895 lhs
->l
= (flags
>> DESC_L_SHIFT
) & 1;
896 lhs
->g
= (flags
& DESC_G_MASK
) != 0;
897 lhs
->avl
= (flags
& DESC_AVL_MASK
) != 0;
902 static void get_seg(SegmentCache
*lhs
, const struct kvm_segment
*rhs
)
904 lhs
->selector
= rhs
->selector
;
905 lhs
->base
= rhs
->base
;
906 lhs
->limit
= rhs
->limit
;
907 lhs
->flags
= (rhs
->type
<< DESC_TYPE_SHIFT
) |
908 (rhs
->present
* DESC_P_MASK
) |
909 (rhs
->dpl
<< DESC_DPL_SHIFT
) |
910 (rhs
->db
<< DESC_B_SHIFT
) |
911 (rhs
->s
* DESC_S_MASK
) |
912 (rhs
->l
<< DESC_L_SHIFT
) |
913 (rhs
->g
* DESC_G_MASK
) |
914 (rhs
->avl
* DESC_AVL_MASK
);
917 static void kvm_getput_reg(__u64
*kvm_reg
, target_ulong
*qemu_reg
, int set
)
920 *kvm_reg
= *qemu_reg
;
922 *qemu_reg
= *kvm_reg
;
926 static int kvm_getput_regs(X86CPU
*cpu
, int set
)
928 CPUX86State
*env
= &cpu
->env
;
929 struct kvm_regs regs
;
933 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_REGS
, ®s
);
939 kvm_getput_reg(®s
.rax
, &env
->regs
[R_EAX
], set
);
940 kvm_getput_reg(®s
.rbx
, &env
->regs
[R_EBX
], set
);
941 kvm_getput_reg(®s
.rcx
, &env
->regs
[R_ECX
], set
);
942 kvm_getput_reg(®s
.rdx
, &env
->regs
[R_EDX
], set
);
943 kvm_getput_reg(®s
.rsi
, &env
->regs
[R_ESI
], set
);
944 kvm_getput_reg(®s
.rdi
, &env
->regs
[R_EDI
], set
);
945 kvm_getput_reg(®s
.rsp
, &env
->regs
[R_ESP
], set
);
946 kvm_getput_reg(®s
.rbp
, &env
->regs
[R_EBP
], set
);
948 kvm_getput_reg(®s
.r8
, &env
->regs
[8], set
);
949 kvm_getput_reg(®s
.r9
, &env
->regs
[9], set
);
950 kvm_getput_reg(®s
.r10
, &env
->regs
[10], set
);
951 kvm_getput_reg(®s
.r11
, &env
->regs
[11], set
);
952 kvm_getput_reg(®s
.r12
, &env
->regs
[12], set
);
953 kvm_getput_reg(®s
.r13
, &env
->regs
[13], set
);
954 kvm_getput_reg(®s
.r14
, &env
->regs
[14], set
);
955 kvm_getput_reg(®s
.r15
, &env
->regs
[15], set
);
958 kvm_getput_reg(®s
.rflags
, &env
->eflags
, set
);
959 kvm_getput_reg(®s
.rip
, &env
->eip
, set
);
962 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_REGS
, ®s
);
968 static int kvm_put_fpu(X86CPU
*cpu
)
970 CPUX86State
*env
= &cpu
->env
;
974 memset(&fpu
, 0, sizeof fpu
);
975 fpu
.fsw
= env
->fpus
& ~(7 << 11);
976 fpu
.fsw
|= (env
->fpstt
& 7) << 11;
978 fpu
.last_opcode
= env
->fpop
;
979 fpu
.last_ip
= env
->fpip
;
980 fpu
.last_dp
= env
->fpdp
;
981 for (i
= 0; i
< 8; ++i
) {
982 fpu
.ftwx
|= (!env
->fptags
[i
]) << i
;
984 memcpy(fpu
.fpr
, env
->fpregs
, sizeof env
->fpregs
);
985 memcpy(fpu
.xmm
, env
->xmm_regs
, sizeof env
->xmm_regs
);
986 fpu
.mxcsr
= env
->mxcsr
;
988 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_FPU
, &fpu
);
991 #define XSAVE_FCW_FSW 0
992 #define XSAVE_FTW_FOP 1
993 #define XSAVE_CWD_RIP 2
994 #define XSAVE_CWD_RDP 4
995 #define XSAVE_MXCSR 6
996 #define XSAVE_ST_SPACE 8
997 #define XSAVE_XMM_SPACE 40
998 #define XSAVE_XSTATE_BV 128
999 #define XSAVE_YMMH_SPACE 144
1000 #define XSAVE_BNDREGS 240
1001 #define XSAVE_BNDCSR 256
1003 static int kvm_put_xsave(X86CPU
*cpu
)
1005 CPUX86State
*env
= &cpu
->env
;
1006 struct kvm_xsave
* xsave
= env
->kvm_xsave_buf
;
1007 uint16_t cwd
, swd
, twd
;
1010 if (!kvm_has_xsave()) {
1011 return kvm_put_fpu(cpu
);
1014 memset(xsave
, 0, sizeof(struct kvm_xsave
));
1016 swd
= env
->fpus
& ~(7 << 11);
1017 swd
|= (env
->fpstt
& 7) << 11;
1019 for (i
= 0; i
< 8; ++i
) {
1020 twd
|= (!env
->fptags
[i
]) << i
;
1022 xsave
->region
[XSAVE_FCW_FSW
] = (uint32_t)(swd
<< 16) + cwd
;
1023 xsave
->region
[XSAVE_FTW_FOP
] = (uint32_t)(env
->fpop
<< 16) + twd
;
1024 memcpy(&xsave
->region
[XSAVE_CWD_RIP
], &env
->fpip
, sizeof(env
->fpip
));
1025 memcpy(&xsave
->region
[XSAVE_CWD_RDP
], &env
->fpdp
, sizeof(env
->fpdp
));
1026 memcpy(&xsave
->region
[XSAVE_ST_SPACE
], env
->fpregs
,
1027 sizeof env
->fpregs
);
1028 memcpy(&xsave
->region
[XSAVE_XMM_SPACE
], env
->xmm_regs
,
1029 sizeof env
->xmm_regs
);
1030 xsave
->region
[XSAVE_MXCSR
] = env
->mxcsr
;
1031 *(uint64_t *)&xsave
->region
[XSAVE_XSTATE_BV
] = env
->xstate_bv
;
1032 memcpy(&xsave
->region
[XSAVE_YMMH_SPACE
], env
->ymmh_regs
,
1033 sizeof env
->ymmh_regs
);
1034 memcpy(&xsave
->region
[XSAVE_BNDREGS
], env
->bnd_regs
,
1035 sizeof env
->bnd_regs
);
1036 memcpy(&xsave
->region
[XSAVE_BNDCSR
], &env
->bndcs_regs
,
1037 sizeof(env
->bndcs_regs
));
1038 r
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_XSAVE
, xsave
);
1042 static int kvm_put_xcrs(X86CPU
*cpu
)
1044 CPUX86State
*env
= &cpu
->env
;
1045 struct kvm_xcrs xcrs
;
1047 if (!kvm_has_xcrs()) {
1053 xcrs
.xcrs
[0].xcr
= 0;
1054 xcrs
.xcrs
[0].value
= env
->xcr0
;
1055 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_XCRS
, &xcrs
);
1058 static int kvm_put_sregs(X86CPU
*cpu
)
1060 CPUX86State
*env
= &cpu
->env
;
1061 struct kvm_sregs sregs
;
1063 memset(sregs
.interrupt_bitmap
, 0, sizeof(sregs
.interrupt_bitmap
));
1064 if (env
->interrupt_injected
>= 0) {
1065 sregs
.interrupt_bitmap
[env
->interrupt_injected
/ 64] |=
1066 (uint64_t)1 << (env
->interrupt_injected
% 64);
1069 if ((env
->eflags
& VM_MASK
)) {
1070 set_v8086_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
1071 set_v8086_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
1072 set_v8086_seg(&sregs
.es
, &env
->segs
[R_ES
]);
1073 set_v8086_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
1074 set_v8086_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
1075 set_v8086_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
1077 set_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
1078 set_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
1079 set_seg(&sregs
.es
, &env
->segs
[R_ES
]);
1080 set_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
1081 set_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
1082 set_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
1085 set_seg(&sregs
.tr
, &env
->tr
);
1086 set_seg(&sregs
.ldt
, &env
->ldt
);
1088 sregs
.idt
.limit
= env
->idt
.limit
;
1089 sregs
.idt
.base
= env
->idt
.base
;
1090 memset(sregs
.idt
.padding
, 0, sizeof sregs
.idt
.padding
);
1091 sregs
.gdt
.limit
= env
->gdt
.limit
;
1092 sregs
.gdt
.base
= env
->gdt
.base
;
1093 memset(sregs
.gdt
.padding
, 0, sizeof sregs
.gdt
.padding
);
1095 sregs
.cr0
= env
->cr
[0];
1096 sregs
.cr2
= env
->cr
[2];
1097 sregs
.cr3
= env
->cr
[3];
1098 sregs
.cr4
= env
->cr
[4];
1100 sregs
.cr8
= cpu_get_apic_tpr(cpu
->apic_state
);
1101 sregs
.apic_base
= cpu_get_apic_base(cpu
->apic_state
);
1103 sregs
.efer
= env
->efer
;
1105 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_SREGS
, &sregs
);
1108 static void kvm_msr_entry_set(struct kvm_msr_entry
*entry
,
1109 uint32_t index
, uint64_t value
)
1111 entry
->index
= index
;
1112 entry
->data
= value
;
1115 static int kvm_put_tscdeadline_msr(X86CPU
*cpu
)
1117 CPUX86State
*env
= &cpu
->env
;
1119 struct kvm_msrs info
;
1120 struct kvm_msr_entry entries
[1];
1122 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
1124 if (!has_msr_tsc_deadline
) {
1128 kvm_msr_entry_set(&msrs
[0], MSR_IA32_TSCDEADLINE
, env
->tsc_deadline
);
1130 msr_data
.info
.nmsrs
= 1;
1132 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MSRS
, &msr_data
);
1136 * Provide a separate write service for the feature control MSR in order to
1137 * kick the VCPU out of VMXON or even guest mode on reset. This has to be done
1138 * before writing any other state because forcibly leaving nested mode
1139 * invalidates the VCPU state.
1141 static int kvm_put_msr_feature_control(X86CPU
*cpu
)
1144 struct kvm_msrs info
;
1145 struct kvm_msr_entry entry
;
1148 kvm_msr_entry_set(&msr_data
.entry
, MSR_IA32_FEATURE_CONTROL
,
1149 cpu
->env
.msr_ia32_feature_control
);
1150 msr_data
.info
.nmsrs
= 1;
1151 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MSRS
, &msr_data
);
1154 static int kvm_put_msrs(X86CPU
*cpu
, int level
)
1156 CPUX86State
*env
= &cpu
->env
;
1158 struct kvm_msrs info
;
1159 struct kvm_msr_entry entries
[100];
1161 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
1164 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_CS
, env
->sysenter_cs
);
1165 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_ESP
, env
->sysenter_esp
);
1166 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_EIP
, env
->sysenter_eip
);
1167 kvm_msr_entry_set(&msrs
[n
++], MSR_PAT
, env
->pat
);
1169 kvm_msr_entry_set(&msrs
[n
++], MSR_STAR
, env
->star
);
1171 if (has_msr_hsave_pa
) {
1172 kvm_msr_entry_set(&msrs
[n
++], MSR_VM_HSAVE_PA
, env
->vm_hsave
);
1174 if (has_msr_tsc_adjust
) {
1175 kvm_msr_entry_set(&msrs
[n
++], MSR_TSC_ADJUST
, env
->tsc_adjust
);
1177 if (has_msr_misc_enable
) {
1178 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_MISC_ENABLE
,
1179 env
->msr_ia32_misc_enable
);
1181 if (has_msr_bndcfgs
) {
1182 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_BNDCFGS
, env
->msr_bndcfgs
);
1184 #ifdef TARGET_X86_64
1185 if (lm_capable_kernel
) {
1186 kvm_msr_entry_set(&msrs
[n
++], MSR_CSTAR
, env
->cstar
);
1187 kvm_msr_entry_set(&msrs
[n
++], MSR_KERNELGSBASE
, env
->kernelgsbase
);
1188 kvm_msr_entry_set(&msrs
[n
++], MSR_FMASK
, env
->fmask
);
1189 kvm_msr_entry_set(&msrs
[n
++], MSR_LSTAR
, env
->lstar
);
1193 * The following MSRs have side effects on the guest or are too heavy
1194 * for normal writeback. Limit them to reset or full state updates.
1196 if (level
>= KVM_PUT_RESET_STATE
) {
1197 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_TSC
, env
->tsc
);
1198 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_SYSTEM_TIME
,
1199 env
->system_time_msr
);
1200 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_WALL_CLOCK
, env
->wall_clock_msr
);
1201 if (has_msr_async_pf_en
) {
1202 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_ASYNC_PF_EN
,
1203 env
->async_pf_en_msr
);
1205 if (has_msr_pv_eoi_en
) {
1206 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_PV_EOI_EN
,
1207 env
->pv_eoi_en_msr
);
1209 if (has_msr_kvm_steal_time
) {
1210 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_STEAL_TIME
,
1211 env
->steal_time_msr
);
1213 if (has_msr_architectural_pmu
) {
1214 /* Stop the counter. */
1215 kvm_msr_entry_set(&msrs
[n
++], MSR_CORE_PERF_FIXED_CTR_CTRL
, 0);
1216 kvm_msr_entry_set(&msrs
[n
++], MSR_CORE_PERF_GLOBAL_CTRL
, 0);
1218 /* Set the counter values. */
1219 for (i
= 0; i
< MAX_FIXED_COUNTERS
; i
++) {
1220 kvm_msr_entry_set(&msrs
[n
++], MSR_CORE_PERF_FIXED_CTR0
+ i
,
1221 env
->msr_fixed_counters
[i
]);
1223 for (i
= 0; i
< num_architectural_pmu_counters
; i
++) {
1224 kvm_msr_entry_set(&msrs
[n
++], MSR_P6_PERFCTR0
+ i
,
1225 env
->msr_gp_counters
[i
]);
1226 kvm_msr_entry_set(&msrs
[n
++], MSR_P6_EVNTSEL0
+ i
,
1227 env
->msr_gp_evtsel
[i
]);
1229 kvm_msr_entry_set(&msrs
[n
++], MSR_CORE_PERF_GLOBAL_STATUS
,
1230 env
->msr_global_status
);
1231 kvm_msr_entry_set(&msrs
[n
++], MSR_CORE_PERF_GLOBAL_OVF_CTRL
,
1232 env
->msr_global_ovf_ctrl
);
1234 /* Now start the PMU. */
1235 kvm_msr_entry_set(&msrs
[n
++], MSR_CORE_PERF_FIXED_CTR_CTRL
,
1236 env
->msr_fixed_ctr_ctrl
);
1237 kvm_msr_entry_set(&msrs
[n
++], MSR_CORE_PERF_GLOBAL_CTRL
,
1238 env
->msr_global_ctrl
);
1240 if (has_msr_hv_hypercall
) {
1241 kvm_msr_entry_set(&msrs
[n
++], HV_X64_MSR_GUEST_OS_ID
,
1242 env
->msr_hv_guest_os_id
);
1243 kvm_msr_entry_set(&msrs
[n
++], HV_X64_MSR_HYPERCALL
,
1244 env
->msr_hv_hypercall
);
1246 if (has_msr_hv_vapic
) {
1247 kvm_msr_entry_set(&msrs
[n
++], HV_X64_MSR_APIC_ASSIST_PAGE
,
1250 if (has_msr_hv_tsc
) {
1251 kvm_msr_entry_set(&msrs
[n
++], HV_X64_MSR_REFERENCE_TSC
,
1255 /* Note: MSR_IA32_FEATURE_CONTROL is written separately, see
1256 * kvm_put_msr_feature_control. */
1261 kvm_msr_entry_set(&msrs
[n
++], MSR_MCG_STATUS
, env
->mcg_status
);
1262 kvm_msr_entry_set(&msrs
[n
++], MSR_MCG_CTL
, env
->mcg_ctl
);
1263 for (i
= 0; i
< (env
->mcg_cap
& 0xff) * 4; i
++) {
1264 kvm_msr_entry_set(&msrs
[n
++], MSR_MC0_CTL
+ i
, env
->mce_banks
[i
]);
1268 msr_data
.info
.nmsrs
= n
;
1270 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MSRS
, &msr_data
);
1275 static int kvm_get_fpu(X86CPU
*cpu
)
1277 CPUX86State
*env
= &cpu
->env
;
1281 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_FPU
, &fpu
);
1286 env
->fpstt
= (fpu
.fsw
>> 11) & 7;
1287 env
->fpus
= fpu
.fsw
;
1288 env
->fpuc
= fpu
.fcw
;
1289 env
->fpop
= fpu
.last_opcode
;
1290 env
->fpip
= fpu
.last_ip
;
1291 env
->fpdp
= fpu
.last_dp
;
1292 for (i
= 0; i
< 8; ++i
) {
1293 env
->fptags
[i
] = !((fpu
.ftwx
>> i
) & 1);
1295 memcpy(env
->fpregs
, fpu
.fpr
, sizeof env
->fpregs
);
1296 memcpy(env
->xmm_regs
, fpu
.xmm
, sizeof env
->xmm_regs
);
1297 env
->mxcsr
= fpu
.mxcsr
;
1302 static int kvm_get_xsave(X86CPU
*cpu
)
1304 CPUX86State
*env
= &cpu
->env
;
1305 struct kvm_xsave
* xsave
= env
->kvm_xsave_buf
;
1307 uint16_t cwd
, swd
, twd
;
1309 if (!kvm_has_xsave()) {
1310 return kvm_get_fpu(cpu
);
1313 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_XSAVE
, xsave
);
1318 cwd
= (uint16_t)xsave
->region
[XSAVE_FCW_FSW
];
1319 swd
= (uint16_t)(xsave
->region
[XSAVE_FCW_FSW
] >> 16);
1320 twd
= (uint16_t)xsave
->region
[XSAVE_FTW_FOP
];
1321 env
->fpop
= (uint16_t)(xsave
->region
[XSAVE_FTW_FOP
] >> 16);
1322 env
->fpstt
= (swd
>> 11) & 7;
1325 for (i
= 0; i
< 8; ++i
) {
1326 env
->fptags
[i
] = !((twd
>> i
) & 1);
1328 memcpy(&env
->fpip
, &xsave
->region
[XSAVE_CWD_RIP
], sizeof(env
->fpip
));
1329 memcpy(&env
->fpdp
, &xsave
->region
[XSAVE_CWD_RDP
], sizeof(env
->fpdp
));
1330 env
->mxcsr
= xsave
->region
[XSAVE_MXCSR
];
1331 memcpy(env
->fpregs
, &xsave
->region
[XSAVE_ST_SPACE
],
1332 sizeof env
->fpregs
);
1333 memcpy(env
->xmm_regs
, &xsave
->region
[XSAVE_XMM_SPACE
],
1334 sizeof env
->xmm_regs
);
1335 env
->xstate_bv
= *(uint64_t *)&xsave
->region
[XSAVE_XSTATE_BV
];
1336 memcpy(env
->ymmh_regs
, &xsave
->region
[XSAVE_YMMH_SPACE
],
1337 sizeof env
->ymmh_regs
);
1338 memcpy(env
->bnd_regs
, &xsave
->region
[XSAVE_BNDREGS
],
1339 sizeof env
->bnd_regs
);
1340 memcpy(&env
->bndcs_regs
, &xsave
->region
[XSAVE_BNDCSR
],
1341 sizeof(env
->bndcs_regs
));
1345 static int kvm_get_xcrs(X86CPU
*cpu
)
1347 CPUX86State
*env
= &cpu
->env
;
1349 struct kvm_xcrs xcrs
;
1351 if (!kvm_has_xcrs()) {
1355 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_XCRS
, &xcrs
);
1360 for (i
= 0; i
< xcrs
.nr_xcrs
; i
++) {
1361 /* Only support xcr0 now */
1362 if (xcrs
.xcrs
[i
].xcr
== 0) {
1363 env
->xcr0
= xcrs
.xcrs
[i
].value
;
1370 static int kvm_get_sregs(X86CPU
*cpu
)
1372 CPUX86State
*env
= &cpu
->env
;
1373 struct kvm_sregs sregs
;
1377 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_SREGS
, &sregs
);
1382 /* There can only be one pending IRQ set in the bitmap at a time, so try
1383 to find it and save its number instead (-1 for none). */
1384 env
->interrupt_injected
= -1;
1385 for (i
= 0; i
< ARRAY_SIZE(sregs
.interrupt_bitmap
); i
++) {
1386 if (sregs
.interrupt_bitmap
[i
]) {
1387 bit
= ctz64(sregs
.interrupt_bitmap
[i
]);
1388 env
->interrupt_injected
= i
* 64 + bit
;
1393 get_seg(&env
->segs
[R_CS
], &sregs
.cs
);
1394 get_seg(&env
->segs
[R_DS
], &sregs
.ds
);
1395 get_seg(&env
->segs
[R_ES
], &sregs
.es
);
1396 get_seg(&env
->segs
[R_FS
], &sregs
.fs
);
1397 get_seg(&env
->segs
[R_GS
], &sregs
.gs
);
1398 get_seg(&env
->segs
[R_SS
], &sregs
.ss
);
1400 get_seg(&env
->tr
, &sregs
.tr
);
1401 get_seg(&env
->ldt
, &sregs
.ldt
);
1403 env
->idt
.limit
= sregs
.idt
.limit
;
1404 env
->idt
.base
= sregs
.idt
.base
;
1405 env
->gdt
.limit
= sregs
.gdt
.limit
;
1406 env
->gdt
.base
= sregs
.gdt
.base
;
1408 env
->cr
[0] = sregs
.cr0
;
1409 env
->cr
[2] = sregs
.cr2
;
1410 env
->cr
[3] = sregs
.cr3
;
1411 env
->cr
[4] = sregs
.cr4
;
1413 env
->efer
= sregs
.efer
;
1415 /* changes to apic base and cr8/tpr are read back via kvm_arch_post_run */
1417 #define HFLAG_COPY_MASK \
1418 ~( HF_CPL_MASK | HF_PE_MASK | HF_MP_MASK | HF_EM_MASK | \
1419 HF_TS_MASK | HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK | \
1420 HF_OSFXSR_MASK | HF_LMA_MASK | HF_CS32_MASK | \
1421 HF_SS32_MASK | HF_CS64_MASK | HF_ADDSEG_MASK)
1423 hflags
= (env
->segs
[R_CS
].flags
>> DESC_DPL_SHIFT
) & HF_CPL_MASK
;
1424 hflags
|= (env
->cr
[0] & CR0_PE_MASK
) << (HF_PE_SHIFT
- CR0_PE_SHIFT
);
1425 hflags
|= (env
->cr
[0] << (HF_MP_SHIFT
- CR0_MP_SHIFT
)) &
1426 (HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
);
1427 hflags
|= (env
->eflags
& (HF_TF_MASK
| HF_VM_MASK
| HF_IOPL_MASK
));
1428 hflags
|= (env
->cr
[4] & CR4_OSFXSR_MASK
) <<
1429 (HF_OSFXSR_SHIFT
- CR4_OSFXSR_SHIFT
);
1431 if (env
->efer
& MSR_EFER_LMA
) {
1432 hflags
|= HF_LMA_MASK
;
1435 if ((hflags
& HF_LMA_MASK
) && (env
->segs
[R_CS
].flags
& DESC_L_MASK
)) {
1436 hflags
|= HF_CS32_MASK
| HF_SS32_MASK
| HF_CS64_MASK
;
1438 hflags
|= (env
->segs
[R_CS
].flags
& DESC_B_MASK
) >>
1439 (DESC_B_SHIFT
- HF_CS32_SHIFT
);
1440 hflags
|= (env
->segs
[R_SS
].flags
& DESC_B_MASK
) >>
1441 (DESC_B_SHIFT
- HF_SS32_SHIFT
);
1442 if (!(env
->cr
[0] & CR0_PE_MASK
) || (env
->eflags
& VM_MASK
) ||
1443 !(hflags
& HF_CS32_MASK
)) {
1444 hflags
|= HF_ADDSEG_MASK
;
1446 hflags
|= ((env
->segs
[R_DS
].base
| env
->segs
[R_ES
].base
|
1447 env
->segs
[R_SS
].base
) != 0) << HF_ADDSEG_SHIFT
;
1450 env
->hflags
= (env
->hflags
& HFLAG_COPY_MASK
) | hflags
;
1455 static int kvm_get_msrs(X86CPU
*cpu
)
1457 CPUX86State
*env
= &cpu
->env
;
1459 struct kvm_msrs info
;
1460 struct kvm_msr_entry entries
[100];
1462 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
1466 msrs
[n
++].index
= MSR_IA32_SYSENTER_CS
;
1467 msrs
[n
++].index
= MSR_IA32_SYSENTER_ESP
;
1468 msrs
[n
++].index
= MSR_IA32_SYSENTER_EIP
;
1469 msrs
[n
++].index
= MSR_PAT
;
1471 msrs
[n
++].index
= MSR_STAR
;
1473 if (has_msr_hsave_pa
) {
1474 msrs
[n
++].index
= MSR_VM_HSAVE_PA
;
1476 if (has_msr_tsc_adjust
) {
1477 msrs
[n
++].index
= MSR_TSC_ADJUST
;
1479 if (has_msr_tsc_deadline
) {
1480 msrs
[n
++].index
= MSR_IA32_TSCDEADLINE
;
1482 if (has_msr_misc_enable
) {
1483 msrs
[n
++].index
= MSR_IA32_MISC_ENABLE
;
1485 if (has_msr_feature_control
) {
1486 msrs
[n
++].index
= MSR_IA32_FEATURE_CONTROL
;
1488 if (has_msr_bndcfgs
) {
1489 msrs
[n
++].index
= MSR_IA32_BNDCFGS
;
1492 if (!env
->tsc_valid
) {
1493 msrs
[n
++].index
= MSR_IA32_TSC
;
1494 env
->tsc_valid
= !runstate_is_running();
1497 #ifdef TARGET_X86_64
1498 if (lm_capable_kernel
) {
1499 msrs
[n
++].index
= MSR_CSTAR
;
1500 msrs
[n
++].index
= MSR_KERNELGSBASE
;
1501 msrs
[n
++].index
= MSR_FMASK
;
1502 msrs
[n
++].index
= MSR_LSTAR
;
1505 msrs
[n
++].index
= MSR_KVM_SYSTEM_TIME
;
1506 msrs
[n
++].index
= MSR_KVM_WALL_CLOCK
;
1507 if (has_msr_async_pf_en
) {
1508 msrs
[n
++].index
= MSR_KVM_ASYNC_PF_EN
;
1510 if (has_msr_pv_eoi_en
) {
1511 msrs
[n
++].index
= MSR_KVM_PV_EOI_EN
;
1513 if (has_msr_kvm_steal_time
) {
1514 msrs
[n
++].index
= MSR_KVM_STEAL_TIME
;
1516 if (has_msr_architectural_pmu
) {
1517 msrs
[n
++].index
= MSR_CORE_PERF_FIXED_CTR_CTRL
;
1518 msrs
[n
++].index
= MSR_CORE_PERF_GLOBAL_CTRL
;
1519 msrs
[n
++].index
= MSR_CORE_PERF_GLOBAL_STATUS
;
1520 msrs
[n
++].index
= MSR_CORE_PERF_GLOBAL_OVF_CTRL
;
1521 for (i
= 0; i
< MAX_FIXED_COUNTERS
; i
++) {
1522 msrs
[n
++].index
= MSR_CORE_PERF_FIXED_CTR0
+ i
;
1524 for (i
= 0; i
< num_architectural_pmu_counters
; i
++) {
1525 msrs
[n
++].index
= MSR_P6_PERFCTR0
+ i
;
1526 msrs
[n
++].index
= MSR_P6_EVNTSEL0
+ i
;
1531 msrs
[n
++].index
= MSR_MCG_STATUS
;
1532 msrs
[n
++].index
= MSR_MCG_CTL
;
1533 for (i
= 0; i
< (env
->mcg_cap
& 0xff) * 4; i
++) {
1534 msrs
[n
++].index
= MSR_MC0_CTL
+ i
;
1538 if (has_msr_hv_hypercall
) {
1539 msrs
[n
++].index
= HV_X64_MSR_HYPERCALL
;
1540 msrs
[n
++].index
= HV_X64_MSR_GUEST_OS_ID
;
1542 if (has_msr_hv_vapic
) {
1543 msrs
[n
++].index
= HV_X64_MSR_APIC_ASSIST_PAGE
;
1545 if (has_msr_hv_tsc
) {
1546 msrs
[n
++].index
= HV_X64_MSR_REFERENCE_TSC
;
1549 msr_data
.info
.nmsrs
= n
;
1550 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_MSRS
, &msr_data
);
1555 for (i
= 0; i
< ret
; i
++) {
1556 uint32_t index
= msrs
[i
].index
;
1558 case MSR_IA32_SYSENTER_CS
:
1559 env
->sysenter_cs
= msrs
[i
].data
;
1561 case MSR_IA32_SYSENTER_ESP
:
1562 env
->sysenter_esp
= msrs
[i
].data
;
1564 case MSR_IA32_SYSENTER_EIP
:
1565 env
->sysenter_eip
= msrs
[i
].data
;
1568 env
->pat
= msrs
[i
].data
;
1571 env
->star
= msrs
[i
].data
;
1573 #ifdef TARGET_X86_64
1575 env
->cstar
= msrs
[i
].data
;
1577 case MSR_KERNELGSBASE
:
1578 env
->kernelgsbase
= msrs
[i
].data
;
1581 env
->fmask
= msrs
[i
].data
;
1584 env
->lstar
= msrs
[i
].data
;
1588 env
->tsc
= msrs
[i
].data
;
1590 case MSR_TSC_ADJUST
:
1591 env
->tsc_adjust
= msrs
[i
].data
;
1593 case MSR_IA32_TSCDEADLINE
:
1594 env
->tsc_deadline
= msrs
[i
].data
;
1596 case MSR_VM_HSAVE_PA
:
1597 env
->vm_hsave
= msrs
[i
].data
;
1599 case MSR_KVM_SYSTEM_TIME
:
1600 env
->system_time_msr
= msrs
[i
].data
;
1602 case MSR_KVM_WALL_CLOCK
:
1603 env
->wall_clock_msr
= msrs
[i
].data
;
1605 case MSR_MCG_STATUS
:
1606 env
->mcg_status
= msrs
[i
].data
;
1609 env
->mcg_ctl
= msrs
[i
].data
;
1611 case MSR_IA32_MISC_ENABLE
:
1612 env
->msr_ia32_misc_enable
= msrs
[i
].data
;
1614 case MSR_IA32_FEATURE_CONTROL
:
1615 env
->msr_ia32_feature_control
= msrs
[i
].data
;
1617 case MSR_IA32_BNDCFGS
:
1618 env
->msr_bndcfgs
= msrs
[i
].data
;
1621 if (msrs
[i
].index
>= MSR_MC0_CTL
&&
1622 msrs
[i
].index
< MSR_MC0_CTL
+ (env
->mcg_cap
& 0xff) * 4) {
1623 env
->mce_banks
[msrs
[i
].index
- MSR_MC0_CTL
] = msrs
[i
].data
;
1626 case MSR_KVM_ASYNC_PF_EN
:
1627 env
->async_pf_en_msr
= msrs
[i
].data
;
1629 case MSR_KVM_PV_EOI_EN
:
1630 env
->pv_eoi_en_msr
= msrs
[i
].data
;
1632 case MSR_KVM_STEAL_TIME
:
1633 env
->steal_time_msr
= msrs
[i
].data
;
1635 case MSR_CORE_PERF_FIXED_CTR_CTRL
:
1636 env
->msr_fixed_ctr_ctrl
= msrs
[i
].data
;
1638 case MSR_CORE_PERF_GLOBAL_CTRL
:
1639 env
->msr_global_ctrl
= msrs
[i
].data
;
1641 case MSR_CORE_PERF_GLOBAL_STATUS
:
1642 env
->msr_global_status
= msrs
[i
].data
;
1644 case MSR_CORE_PERF_GLOBAL_OVF_CTRL
:
1645 env
->msr_global_ovf_ctrl
= msrs
[i
].data
;
1647 case MSR_CORE_PERF_FIXED_CTR0
... MSR_CORE_PERF_FIXED_CTR0
+ MAX_FIXED_COUNTERS
- 1:
1648 env
->msr_fixed_counters
[index
- MSR_CORE_PERF_FIXED_CTR0
] = msrs
[i
].data
;
1650 case MSR_P6_PERFCTR0
... MSR_P6_PERFCTR0
+ MAX_GP_COUNTERS
- 1:
1651 env
->msr_gp_counters
[index
- MSR_P6_PERFCTR0
] = msrs
[i
].data
;
1653 case MSR_P6_EVNTSEL0
... MSR_P6_EVNTSEL0
+ MAX_GP_COUNTERS
- 1:
1654 env
->msr_gp_evtsel
[index
- MSR_P6_EVNTSEL0
] = msrs
[i
].data
;
1656 case HV_X64_MSR_HYPERCALL
:
1657 env
->msr_hv_hypercall
= msrs
[i
].data
;
1659 case HV_X64_MSR_GUEST_OS_ID
:
1660 env
->msr_hv_guest_os_id
= msrs
[i
].data
;
1662 case HV_X64_MSR_APIC_ASSIST_PAGE
:
1663 env
->msr_hv_vapic
= msrs
[i
].data
;
1665 case HV_X64_MSR_REFERENCE_TSC
:
1666 env
->msr_hv_tsc
= msrs
[i
].data
;
1674 static int kvm_put_mp_state(X86CPU
*cpu
)
1676 struct kvm_mp_state mp_state
= { .mp_state
= cpu
->env
.mp_state
};
1678 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MP_STATE
, &mp_state
);
1681 static int kvm_get_mp_state(X86CPU
*cpu
)
1683 CPUState
*cs
= CPU(cpu
);
1684 CPUX86State
*env
= &cpu
->env
;
1685 struct kvm_mp_state mp_state
;
1688 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_MP_STATE
, &mp_state
);
1692 env
->mp_state
= mp_state
.mp_state
;
1693 if (kvm_irqchip_in_kernel()) {
1694 cs
->halted
= (mp_state
.mp_state
== KVM_MP_STATE_HALTED
);
1699 static int kvm_get_apic(X86CPU
*cpu
)
1701 DeviceState
*apic
= cpu
->apic_state
;
1702 struct kvm_lapic_state kapic
;
1705 if (apic
&& kvm_irqchip_in_kernel()) {
1706 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_LAPIC
, &kapic
);
1711 kvm_get_apic_state(apic
, &kapic
);
1716 static int kvm_put_apic(X86CPU
*cpu
)
1718 DeviceState
*apic
= cpu
->apic_state
;
1719 struct kvm_lapic_state kapic
;
1721 if (apic
&& kvm_irqchip_in_kernel()) {
1722 kvm_put_apic_state(apic
, &kapic
);
1724 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_LAPIC
, &kapic
);
1729 static int kvm_put_vcpu_events(X86CPU
*cpu
, int level
)
1731 CPUX86State
*env
= &cpu
->env
;
1732 struct kvm_vcpu_events events
;
1734 if (!kvm_has_vcpu_events()) {
1738 events
.exception
.injected
= (env
->exception_injected
>= 0);
1739 events
.exception
.nr
= env
->exception_injected
;
1740 events
.exception
.has_error_code
= env
->has_error_code
;
1741 events
.exception
.error_code
= env
->error_code
;
1742 events
.exception
.pad
= 0;
1744 events
.interrupt
.injected
= (env
->interrupt_injected
>= 0);
1745 events
.interrupt
.nr
= env
->interrupt_injected
;
1746 events
.interrupt
.soft
= env
->soft_interrupt
;
1748 events
.nmi
.injected
= env
->nmi_injected
;
1749 events
.nmi
.pending
= env
->nmi_pending
;
1750 events
.nmi
.masked
= !!(env
->hflags2
& HF2_NMI_MASK
);
1753 events
.sipi_vector
= env
->sipi_vector
;
1756 if (level
>= KVM_PUT_RESET_STATE
) {
1758 KVM_VCPUEVENT_VALID_NMI_PENDING
| KVM_VCPUEVENT_VALID_SIPI_VECTOR
;
1761 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_VCPU_EVENTS
, &events
);
1764 static int kvm_get_vcpu_events(X86CPU
*cpu
)
1766 CPUX86State
*env
= &cpu
->env
;
1767 struct kvm_vcpu_events events
;
1770 if (!kvm_has_vcpu_events()) {
1774 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_VCPU_EVENTS
, &events
);
1778 env
->exception_injected
=
1779 events
.exception
.injected
? events
.exception
.nr
: -1;
1780 env
->has_error_code
= events
.exception
.has_error_code
;
1781 env
->error_code
= events
.exception
.error_code
;
1783 env
->interrupt_injected
=
1784 events
.interrupt
.injected
? events
.interrupt
.nr
: -1;
1785 env
->soft_interrupt
= events
.interrupt
.soft
;
1787 env
->nmi_injected
= events
.nmi
.injected
;
1788 env
->nmi_pending
= events
.nmi
.pending
;
1789 if (events
.nmi
.masked
) {
1790 env
->hflags2
|= HF2_NMI_MASK
;
1792 env
->hflags2
&= ~HF2_NMI_MASK
;
1795 env
->sipi_vector
= events
.sipi_vector
;
1800 static int kvm_guest_debug_workarounds(X86CPU
*cpu
)
1802 CPUState
*cs
= CPU(cpu
);
1803 CPUX86State
*env
= &cpu
->env
;
1805 unsigned long reinject_trap
= 0;
1807 if (!kvm_has_vcpu_events()) {
1808 if (env
->exception_injected
== 1) {
1809 reinject_trap
= KVM_GUESTDBG_INJECT_DB
;
1810 } else if (env
->exception_injected
== 3) {
1811 reinject_trap
= KVM_GUESTDBG_INJECT_BP
;
1813 env
->exception_injected
= -1;
1817 * Kernels before KVM_CAP_X86_ROBUST_SINGLESTEP overwrote flags.TF
1818 * injected via SET_GUEST_DEBUG while updating GP regs. Work around this
1819 * by updating the debug state once again if single-stepping is on.
1820 * Another reason to call kvm_update_guest_debug here is a pending debug
1821 * trap raise by the guest. On kernels without SET_VCPU_EVENTS we have to
1822 * reinject them via SET_GUEST_DEBUG.
1824 if (reinject_trap
||
1825 (!kvm_has_robust_singlestep() && cs
->singlestep_enabled
)) {
1826 ret
= kvm_update_guest_debug(cs
, reinject_trap
);
1831 static int kvm_put_debugregs(X86CPU
*cpu
)
1833 CPUX86State
*env
= &cpu
->env
;
1834 struct kvm_debugregs dbgregs
;
1837 if (!kvm_has_debugregs()) {
1841 for (i
= 0; i
< 4; i
++) {
1842 dbgregs
.db
[i
] = env
->dr
[i
];
1844 dbgregs
.dr6
= env
->dr
[6];
1845 dbgregs
.dr7
= env
->dr
[7];
1848 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_DEBUGREGS
, &dbgregs
);
1851 static int kvm_get_debugregs(X86CPU
*cpu
)
1853 CPUX86State
*env
= &cpu
->env
;
1854 struct kvm_debugregs dbgregs
;
1857 if (!kvm_has_debugregs()) {
1861 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_DEBUGREGS
, &dbgregs
);
1865 for (i
= 0; i
< 4; i
++) {
1866 env
->dr
[i
] = dbgregs
.db
[i
];
1868 env
->dr
[4] = env
->dr
[6] = dbgregs
.dr6
;
1869 env
->dr
[5] = env
->dr
[7] = dbgregs
.dr7
;
1874 int kvm_arch_put_registers(CPUState
*cpu
, int level
)
1876 X86CPU
*x86_cpu
= X86_CPU(cpu
);
1879 assert(cpu_is_stopped(cpu
) || qemu_cpu_is_self(cpu
));
1881 if (level
>= KVM_PUT_RESET_STATE
&& has_msr_feature_control
) {
1882 ret
= kvm_put_msr_feature_control(x86_cpu
);
1888 ret
= kvm_getput_regs(x86_cpu
, 1);
1892 ret
= kvm_put_xsave(x86_cpu
);
1896 ret
= kvm_put_xcrs(x86_cpu
);
1900 ret
= kvm_put_sregs(x86_cpu
);
1904 /* must be before kvm_put_msrs */
1905 ret
= kvm_inject_mce_oldstyle(x86_cpu
);
1909 ret
= kvm_put_msrs(x86_cpu
, level
);
1913 if (level
>= KVM_PUT_RESET_STATE
) {
1914 ret
= kvm_put_mp_state(x86_cpu
);
1918 ret
= kvm_put_apic(x86_cpu
);
1924 ret
= kvm_put_tscdeadline_msr(x86_cpu
);
1929 ret
= kvm_put_vcpu_events(x86_cpu
, level
);
1933 ret
= kvm_put_debugregs(x86_cpu
);
1938 ret
= kvm_guest_debug_workarounds(x86_cpu
);
1945 int kvm_arch_get_registers(CPUState
*cs
)
1947 X86CPU
*cpu
= X86_CPU(cs
);
1950 assert(cpu_is_stopped(cs
) || qemu_cpu_is_self(cs
));
1952 ret
= kvm_getput_regs(cpu
, 0);
1956 ret
= kvm_get_xsave(cpu
);
1960 ret
= kvm_get_xcrs(cpu
);
1964 ret
= kvm_get_sregs(cpu
);
1968 ret
= kvm_get_msrs(cpu
);
1972 ret
= kvm_get_mp_state(cpu
);
1976 ret
= kvm_get_apic(cpu
);
1980 ret
= kvm_get_vcpu_events(cpu
);
1984 ret
= kvm_get_debugregs(cpu
);
1991 void kvm_arch_pre_run(CPUState
*cpu
, struct kvm_run
*run
)
1993 X86CPU
*x86_cpu
= X86_CPU(cpu
);
1994 CPUX86State
*env
= &x86_cpu
->env
;
1998 if (cpu
->interrupt_request
& CPU_INTERRUPT_NMI
) {
1999 cpu
->interrupt_request
&= ~CPU_INTERRUPT_NMI
;
2000 DPRINTF("injected NMI\n");
2001 ret
= kvm_vcpu_ioctl(cpu
, KVM_NMI
);
2003 fprintf(stderr
, "KVM: injection failed, NMI lost (%s)\n",
2008 if (!kvm_irqchip_in_kernel()) {
2009 /* Force the VCPU out of its inner loop to process any INIT requests
2010 * or pending TPR access reports. */
2011 if (cpu
->interrupt_request
&
2012 (CPU_INTERRUPT_INIT
| CPU_INTERRUPT_TPR
)) {
2013 cpu
->exit_request
= 1;
2016 /* Try to inject an interrupt if the guest can accept it */
2017 if (run
->ready_for_interrupt_injection
&&
2018 (cpu
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
2019 (env
->eflags
& IF_MASK
)) {
2022 cpu
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
2023 irq
= cpu_get_pic_interrupt(env
);
2025 struct kvm_interrupt intr
;
2028 DPRINTF("injected interrupt %d\n", irq
);
2029 ret
= kvm_vcpu_ioctl(cpu
, KVM_INTERRUPT
, &intr
);
2032 "KVM: injection failed, interrupt lost (%s)\n",
2038 /* If we have an interrupt but the guest is not ready to receive an
2039 * interrupt, request an interrupt window exit. This will
2040 * cause a return to userspace as soon as the guest is ready to
2041 * receive interrupts. */
2042 if ((cpu
->interrupt_request
& CPU_INTERRUPT_HARD
)) {
2043 run
->request_interrupt_window
= 1;
2045 run
->request_interrupt_window
= 0;
2048 DPRINTF("setting tpr\n");
2049 run
->cr8
= cpu_get_apic_tpr(x86_cpu
->apic_state
);
2053 void kvm_arch_post_run(CPUState
*cpu
, struct kvm_run
*run
)
2055 X86CPU
*x86_cpu
= X86_CPU(cpu
);
2056 CPUX86State
*env
= &x86_cpu
->env
;
2059 env
->eflags
|= IF_MASK
;
2061 env
->eflags
&= ~IF_MASK
;
2063 cpu_set_apic_tpr(x86_cpu
->apic_state
, run
->cr8
);
2064 cpu_set_apic_base(x86_cpu
->apic_state
, run
->apic_base
);
2067 int kvm_arch_process_async_events(CPUState
*cs
)
2069 X86CPU
*cpu
= X86_CPU(cs
);
2070 CPUX86State
*env
= &cpu
->env
;
2072 if (cs
->interrupt_request
& CPU_INTERRUPT_MCE
) {
2073 /* We must not raise CPU_INTERRUPT_MCE if it's not supported. */
2074 assert(env
->mcg_cap
);
2076 cs
->interrupt_request
&= ~CPU_INTERRUPT_MCE
;
2078 kvm_cpu_synchronize_state(cs
);
2080 if (env
->exception_injected
== EXCP08_DBLE
) {
2081 /* this means triple fault */
2082 qemu_system_reset_request();
2083 cs
->exit_request
= 1;
2086 env
->exception_injected
= EXCP12_MCHK
;
2087 env
->has_error_code
= 0;
2090 if (kvm_irqchip_in_kernel() && env
->mp_state
== KVM_MP_STATE_HALTED
) {
2091 env
->mp_state
= KVM_MP_STATE_RUNNABLE
;
2095 if (kvm_irqchip_in_kernel()) {
2099 if (cs
->interrupt_request
& CPU_INTERRUPT_POLL
) {
2100 cs
->interrupt_request
&= ~CPU_INTERRUPT_POLL
;
2101 apic_poll_irq(cpu
->apic_state
);
2103 if (((cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
2104 (env
->eflags
& IF_MASK
)) ||
2105 (cs
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
2108 if (cs
->interrupt_request
& CPU_INTERRUPT_INIT
) {
2109 kvm_cpu_synchronize_state(cs
);
2112 if (cs
->interrupt_request
& CPU_INTERRUPT_SIPI
) {
2113 kvm_cpu_synchronize_state(cs
);
2116 if (cs
->interrupt_request
& CPU_INTERRUPT_TPR
) {
2117 cs
->interrupt_request
&= ~CPU_INTERRUPT_TPR
;
2118 kvm_cpu_synchronize_state(cs
);
2119 apic_handle_tpr_access_report(cpu
->apic_state
, env
->eip
,
2120 env
->tpr_access_type
);
2126 static int kvm_handle_halt(X86CPU
*cpu
)
2128 CPUState
*cs
= CPU(cpu
);
2129 CPUX86State
*env
= &cpu
->env
;
2131 if (!((cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
2132 (env
->eflags
& IF_MASK
)) &&
2133 !(cs
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
2141 static int kvm_handle_tpr_access(X86CPU
*cpu
)
2143 CPUState
*cs
= CPU(cpu
);
2144 struct kvm_run
*run
= cs
->kvm_run
;
2146 apic_handle_tpr_access_report(cpu
->apic_state
, run
->tpr_access
.rip
,
2147 run
->tpr_access
.is_write
? TPR_ACCESS_WRITE
2152 int kvm_arch_insert_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
2154 static const uint8_t int3
= 0xcc;
2156 if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 0) ||
2157 cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&int3
, 1, 1)) {
2163 int kvm_arch_remove_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
2167 if (cpu_memory_rw_debug(cs
, bp
->pc
, &int3
, 1, 0) || int3
!= 0xcc ||
2168 cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 1)) {
2180 static int nb_hw_breakpoint
;
2182 static int find_hw_breakpoint(target_ulong addr
, int len
, int type
)
2186 for (n
= 0; n
< nb_hw_breakpoint
; n
++) {
2187 if (hw_breakpoint
[n
].addr
== addr
&& hw_breakpoint
[n
].type
== type
&&
2188 (hw_breakpoint
[n
].len
== len
|| len
== -1)) {
2195 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
2196 target_ulong len
, int type
)
2199 case GDB_BREAKPOINT_HW
:
2202 case GDB_WATCHPOINT_WRITE
:
2203 case GDB_WATCHPOINT_ACCESS
:
2210 if (addr
& (len
- 1)) {
2222 if (nb_hw_breakpoint
== 4) {
2225 if (find_hw_breakpoint(addr
, len
, type
) >= 0) {
2228 hw_breakpoint
[nb_hw_breakpoint
].addr
= addr
;
2229 hw_breakpoint
[nb_hw_breakpoint
].len
= len
;
2230 hw_breakpoint
[nb_hw_breakpoint
].type
= type
;
2236 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
2237 target_ulong len
, int type
)
2241 n
= find_hw_breakpoint(addr
, (type
== GDB_BREAKPOINT_HW
) ? 1 : len
, type
);
2246 hw_breakpoint
[n
] = hw_breakpoint
[nb_hw_breakpoint
];
2251 void kvm_arch_remove_all_hw_breakpoints(void)
2253 nb_hw_breakpoint
= 0;
2256 static CPUWatchpoint hw_watchpoint
;
2258 static int kvm_handle_debug(X86CPU
*cpu
,
2259 struct kvm_debug_exit_arch
*arch_info
)
2261 CPUState
*cs
= CPU(cpu
);
2262 CPUX86State
*env
= &cpu
->env
;
2266 if (arch_info
->exception
== 1) {
2267 if (arch_info
->dr6
& (1 << 14)) {
2268 if (cs
->singlestep_enabled
) {
2272 for (n
= 0; n
< 4; n
++) {
2273 if (arch_info
->dr6
& (1 << n
)) {
2274 switch ((arch_info
->dr7
>> (16 + n
*4)) & 0x3) {
2280 cs
->watchpoint_hit
= &hw_watchpoint
;
2281 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
2282 hw_watchpoint
.flags
= BP_MEM_WRITE
;
2286 cs
->watchpoint_hit
= &hw_watchpoint
;
2287 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
2288 hw_watchpoint
.flags
= BP_MEM_ACCESS
;
2294 } else if (kvm_find_sw_breakpoint(cs
, arch_info
->pc
)) {
2298 cpu_synchronize_state(cs
);
2299 assert(env
->exception_injected
== -1);
2302 env
->exception_injected
= arch_info
->exception
;
2303 env
->has_error_code
= 0;
2309 void kvm_arch_update_guest_debug(CPUState
*cpu
, struct kvm_guest_debug
*dbg
)
2311 const uint8_t type_code
[] = {
2312 [GDB_BREAKPOINT_HW
] = 0x0,
2313 [GDB_WATCHPOINT_WRITE
] = 0x1,
2314 [GDB_WATCHPOINT_ACCESS
] = 0x3
2316 const uint8_t len_code
[] = {
2317 [1] = 0x0, [2] = 0x1, [4] = 0x3, [8] = 0x2
2321 if (kvm_sw_breakpoints_active(cpu
)) {
2322 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_SW_BP
;
2324 if (nb_hw_breakpoint
> 0) {
2325 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
;
2326 dbg
->arch
.debugreg
[7] = 0x0600;
2327 for (n
= 0; n
< nb_hw_breakpoint
; n
++) {
2328 dbg
->arch
.debugreg
[n
] = hw_breakpoint
[n
].addr
;
2329 dbg
->arch
.debugreg
[7] |= (2 << (n
* 2)) |
2330 (type_code
[hw_breakpoint
[n
].type
] << (16 + n
*4)) |
2331 ((uint32_t)len_code
[hw_breakpoint
[n
].len
] << (18 + n
*4));
2336 static bool host_supports_vmx(void)
2338 uint32_t ecx
, unused
;
2340 host_cpuid(1, 0, &unused
, &unused
, &ecx
, &unused
);
2341 return ecx
& CPUID_EXT_VMX
;
2344 #define VMX_INVALID_GUEST_STATE 0x80000021
2346 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
2348 X86CPU
*cpu
= X86_CPU(cs
);
2352 switch (run
->exit_reason
) {
2354 DPRINTF("handle_hlt\n");
2355 ret
= kvm_handle_halt(cpu
);
2357 case KVM_EXIT_SET_TPR
:
2360 case KVM_EXIT_TPR_ACCESS
:
2361 ret
= kvm_handle_tpr_access(cpu
);
2363 case KVM_EXIT_FAIL_ENTRY
:
2364 code
= run
->fail_entry
.hardware_entry_failure_reason
;
2365 fprintf(stderr
, "KVM: entry failed, hardware error 0x%" PRIx64
"\n",
2367 if (host_supports_vmx() && code
== VMX_INVALID_GUEST_STATE
) {
2369 "\nIf you're running a guest on an Intel machine without "
2370 "unrestricted mode\n"
2371 "support, the failure can be most likely due to the guest "
2372 "entering an invalid\n"
2373 "state for Intel VT. For example, the guest maybe running "
2374 "in big real mode\n"
2375 "which is not supported on less recent Intel processors."
2380 case KVM_EXIT_EXCEPTION
:
2381 fprintf(stderr
, "KVM: exception %d exit (error code 0x%x)\n",
2382 run
->ex
.exception
, run
->ex
.error_code
);
2385 case KVM_EXIT_DEBUG
:
2386 DPRINTF("kvm_exit_debug\n");
2387 ret
= kvm_handle_debug(cpu
, &run
->debug
.arch
);
2390 fprintf(stderr
, "KVM: unknown exit reason %d\n", run
->exit_reason
);
2398 bool kvm_arch_stop_on_emulation_error(CPUState
*cs
)
2400 X86CPU
*cpu
= X86_CPU(cs
);
2401 CPUX86State
*env
= &cpu
->env
;
2403 kvm_cpu_synchronize_state(cs
);
2404 return !(env
->cr
[0] & CR0_PE_MASK
) ||
2405 ((env
->segs
[R_CS
].selector
& 3) != 3);
2408 void kvm_arch_init_irq_routing(KVMState
*s
)
2410 if (!kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
)) {
2411 /* If kernel can't do irq routing, interrupt source
2412 * override 0->2 cannot be set up as required by HPET.
2413 * So we have to disable it.
2417 /* We know at this point that we're using the in-kernel
2418 * irqchip, so we can use irqfds, and on x86 we know
2419 * we can use msi via irqfd and GSI routing.
2421 kvm_irqfds_allowed
= true;
2422 kvm_msi_via_irqfd_allowed
= true;
2423 kvm_gsi_routing_allowed
= true;
2426 /* Classic KVM device assignment interface. Will remain x86 only. */
2427 int kvm_device_pci_assign(KVMState
*s
, PCIHostDeviceAddress
*dev_addr
,
2428 uint32_t flags
, uint32_t *dev_id
)
2430 struct kvm_assigned_pci_dev dev_data
= {
2431 .segnr
= dev_addr
->domain
,
2432 .busnr
= dev_addr
->bus
,
2433 .devfn
= PCI_DEVFN(dev_addr
->slot
, dev_addr
->function
),
2438 dev_data
.assigned_dev_id
=
2439 (dev_addr
->domain
<< 16) | (dev_addr
->bus
<< 8) | dev_data
.devfn
;
2441 ret
= kvm_vm_ioctl(s
, KVM_ASSIGN_PCI_DEVICE
, &dev_data
);
2446 *dev_id
= dev_data
.assigned_dev_id
;
2451 int kvm_device_pci_deassign(KVMState
*s
, uint32_t dev_id
)
2453 struct kvm_assigned_pci_dev dev_data
= {
2454 .assigned_dev_id
= dev_id
,
2457 return kvm_vm_ioctl(s
, KVM_DEASSIGN_PCI_DEVICE
, &dev_data
);
2460 static int kvm_assign_irq_internal(KVMState
*s
, uint32_t dev_id
,
2461 uint32_t irq_type
, uint32_t guest_irq
)
2463 struct kvm_assigned_irq assigned_irq
= {
2464 .assigned_dev_id
= dev_id
,
2465 .guest_irq
= guest_irq
,
2469 if (kvm_check_extension(s
, KVM_CAP_ASSIGN_DEV_IRQ
)) {
2470 return kvm_vm_ioctl(s
, KVM_ASSIGN_DEV_IRQ
, &assigned_irq
);
2472 return kvm_vm_ioctl(s
, KVM_ASSIGN_IRQ
, &assigned_irq
);
2476 int kvm_device_intx_assign(KVMState
*s
, uint32_t dev_id
, bool use_host_msi
,
2479 uint32_t irq_type
= KVM_DEV_IRQ_GUEST_INTX
|
2480 (use_host_msi
? KVM_DEV_IRQ_HOST_MSI
: KVM_DEV_IRQ_HOST_INTX
);
2482 return kvm_assign_irq_internal(s
, dev_id
, irq_type
, guest_irq
);
2485 int kvm_device_intx_set_mask(KVMState
*s
, uint32_t dev_id
, bool masked
)
2487 struct kvm_assigned_pci_dev dev_data
= {
2488 .assigned_dev_id
= dev_id
,
2489 .flags
= masked
? KVM_DEV_ASSIGN_MASK_INTX
: 0,
2492 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_INTX_MASK
, &dev_data
);
2495 static int kvm_deassign_irq_internal(KVMState
*s
, uint32_t dev_id
,
2498 struct kvm_assigned_irq assigned_irq
= {
2499 .assigned_dev_id
= dev_id
,
2503 return kvm_vm_ioctl(s
, KVM_DEASSIGN_DEV_IRQ
, &assigned_irq
);
2506 int kvm_device_intx_deassign(KVMState
*s
, uint32_t dev_id
, bool use_host_msi
)
2508 return kvm_deassign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_GUEST_INTX
|
2509 (use_host_msi
? KVM_DEV_IRQ_HOST_MSI
: KVM_DEV_IRQ_HOST_INTX
));
2512 int kvm_device_msi_assign(KVMState
*s
, uint32_t dev_id
, int virq
)
2514 return kvm_assign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_HOST_MSI
|
2515 KVM_DEV_IRQ_GUEST_MSI
, virq
);
2518 int kvm_device_msi_deassign(KVMState
*s
, uint32_t dev_id
)
2520 return kvm_deassign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_GUEST_MSI
|
2521 KVM_DEV_IRQ_HOST_MSI
);
2524 bool kvm_device_msix_supported(KVMState
*s
)
2526 /* The kernel lacks a corresponding KVM_CAP, so we probe by calling
2527 * KVM_ASSIGN_SET_MSIX_NR with an invalid parameter. */
2528 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_NR
, NULL
) == -EFAULT
;
2531 int kvm_device_msix_init_vectors(KVMState
*s
, uint32_t dev_id
,
2532 uint32_t nr_vectors
)
2534 struct kvm_assigned_msix_nr msix_nr
= {
2535 .assigned_dev_id
= dev_id
,
2536 .entry_nr
= nr_vectors
,
2539 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_NR
, &msix_nr
);
2542 int kvm_device_msix_set_vector(KVMState
*s
, uint32_t dev_id
, uint32_t vector
,
2545 struct kvm_assigned_msix_entry msix_entry
= {
2546 .assigned_dev_id
= dev_id
,
2551 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_ENTRY
, &msix_entry
);
2554 int kvm_device_msix_assign(KVMState
*s
, uint32_t dev_id
)
2556 return kvm_assign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_HOST_MSIX
|
2557 KVM_DEV_IRQ_GUEST_MSIX
, 0);
2560 int kvm_device_msix_deassign(KVMState
*s
, uint32_t dev_id
)
2562 return kvm_deassign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_GUEST_MSIX
|
2563 KVM_DEV_IRQ_HOST_MSIX
);