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 "hw/i386/apic_internal.h"
34 #include "hw/i386/apic-msidef.h"
35 #include "exec/ioport.h"
36 #include <asm/hyperv.h>
37 #include "hw/pci/pci.h"
38 #include "migration/migration.h"
39 #include "qapi/qmp/qerror.h"
44 #define DPRINTF(fmt, ...) \
45 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
47 #define DPRINTF(fmt, ...) \
51 #define MSR_KVM_WALL_CLOCK 0x11
52 #define MSR_KVM_SYSTEM_TIME 0x12
55 #define BUS_MCEERR_AR 4
58 #define BUS_MCEERR_AO 5
61 const KVMCapabilityInfo kvm_arch_required_capabilities
[] = {
62 KVM_CAP_INFO(SET_TSS_ADDR
),
63 KVM_CAP_INFO(EXT_CPUID
),
64 KVM_CAP_INFO(MP_STATE
),
68 static bool has_msr_star
;
69 static bool has_msr_hsave_pa
;
70 static bool has_msr_tsc_adjust
;
71 static bool has_msr_tsc_deadline
;
72 static bool has_msr_feature_control
;
73 static bool has_msr_async_pf_en
;
74 static bool has_msr_pv_eoi_en
;
75 static bool has_msr_misc_enable
;
76 static bool has_msr_bndcfgs
;
77 static bool has_msr_kvm_steal_time
;
78 static int lm_capable_kernel
;
79 static bool has_msr_hv_hypercall
;
80 static bool has_msr_hv_vapic
;
81 static bool has_msr_hv_tsc
;
83 static bool has_msr_architectural_pmu
;
84 static uint32_t num_architectural_pmu_counters
;
86 bool kvm_allows_irq0_override(void)
88 return !kvm_irqchip_in_kernel() || kvm_has_gsi_routing();
91 static struct kvm_cpuid2
*try_get_cpuid(KVMState
*s
, int max
)
93 struct kvm_cpuid2
*cpuid
;
96 size
= sizeof(*cpuid
) + max
* sizeof(*cpuid
->entries
);
97 cpuid
= (struct kvm_cpuid2
*)g_malloc0(size
);
99 r
= kvm_ioctl(s
, KVM_GET_SUPPORTED_CPUID
, cpuid
);
100 if (r
== 0 && cpuid
->nent
>= max
) {
108 fprintf(stderr
, "KVM_GET_SUPPORTED_CPUID failed: %s\n",
116 /* Run KVM_GET_SUPPORTED_CPUID ioctl(), allocating a buffer large enough
119 static struct kvm_cpuid2
*get_supported_cpuid(KVMState
*s
)
121 struct kvm_cpuid2
*cpuid
;
123 while ((cpuid
= try_get_cpuid(s
, max
)) == NULL
) {
129 static const struct kvm_para_features
{
132 } para_features
[] = {
133 { KVM_CAP_CLOCKSOURCE
, KVM_FEATURE_CLOCKSOURCE
},
134 { KVM_CAP_NOP_IO_DELAY
, KVM_FEATURE_NOP_IO_DELAY
},
135 { KVM_CAP_PV_MMU
, KVM_FEATURE_MMU_OP
},
136 { KVM_CAP_ASYNC_PF
, KVM_FEATURE_ASYNC_PF
},
139 static int get_para_features(KVMState
*s
)
143 for (i
= 0; i
< ARRAY_SIZE(para_features
); i
++) {
144 if (kvm_check_extension(s
, para_features
[i
].cap
)) {
145 features
|= (1 << para_features
[i
].feature
);
153 /* Returns the value for a specific register on the cpuid entry
155 static uint32_t cpuid_entry_get_reg(struct kvm_cpuid_entry2
*entry
, int reg
)
175 /* Find matching entry for function/index on kvm_cpuid2 struct
177 static struct kvm_cpuid_entry2
*cpuid_find_entry(struct kvm_cpuid2
*cpuid
,
182 for (i
= 0; i
< cpuid
->nent
; ++i
) {
183 if (cpuid
->entries
[i
].function
== function
&&
184 cpuid
->entries
[i
].index
== index
) {
185 return &cpuid
->entries
[i
];
192 uint32_t kvm_arch_get_supported_cpuid(KVMState
*s
, uint32_t function
,
193 uint32_t index
, int reg
)
195 struct kvm_cpuid2
*cpuid
;
197 uint32_t cpuid_1_edx
;
200 cpuid
= get_supported_cpuid(s
);
202 struct kvm_cpuid_entry2
*entry
= cpuid_find_entry(cpuid
, function
, index
);
205 ret
= cpuid_entry_get_reg(entry
, reg
);
208 /* Fixups for the data returned by KVM, below */
210 if (function
== 1 && reg
== R_EDX
) {
211 /* KVM before 2.6.30 misreports the following features */
212 ret
|= CPUID_MTRR
| CPUID_PAT
| CPUID_MCE
| CPUID_MCA
;
213 } else if (function
== 1 && reg
== R_ECX
) {
214 /* We can set the hypervisor flag, even if KVM does not return it on
215 * GET_SUPPORTED_CPUID
217 ret
|= CPUID_EXT_HYPERVISOR
;
218 /* tsc-deadline flag is not returned by GET_SUPPORTED_CPUID, but it
219 * can be enabled if the kernel has KVM_CAP_TSC_DEADLINE_TIMER,
220 * and the irqchip is in the kernel.
222 if (kvm_irqchip_in_kernel() &&
223 kvm_check_extension(s
, KVM_CAP_TSC_DEADLINE_TIMER
)) {
224 ret
|= CPUID_EXT_TSC_DEADLINE_TIMER
;
227 /* x2apic is reported by GET_SUPPORTED_CPUID, but it can't be enabled
228 * without the in-kernel irqchip
230 if (!kvm_irqchip_in_kernel()) {
231 ret
&= ~CPUID_EXT_X2APIC
;
233 } else if (function
== 0x80000001 && reg
== R_EDX
) {
234 /* On Intel, kvm returns cpuid according to the Intel spec,
235 * so add missing bits according to the AMD spec:
237 cpuid_1_edx
= kvm_arch_get_supported_cpuid(s
, 1, 0, R_EDX
);
238 ret
|= cpuid_1_edx
& CPUID_EXT2_AMD_ALIASES
;
243 /* fallback for older kernels */
244 if ((function
== KVM_CPUID_FEATURES
) && !found
) {
245 ret
= get_para_features(s
);
251 typedef struct HWPoisonPage
{
253 QLIST_ENTRY(HWPoisonPage
) list
;
256 static QLIST_HEAD(, HWPoisonPage
) hwpoison_page_list
=
257 QLIST_HEAD_INITIALIZER(hwpoison_page_list
);
259 static void kvm_unpoison_all(void *param
)
261 HWPoisonPage
*page
, *next_page
;
263 QLIST_FOREACH_SAFE(page
, &hwpoison_page_list
, list
, next_page
) {
264 QLIST_REMOVE(page
, list
);
265 qemu_ram_remap(page
->ram_addr
, TARGET_PAGE_SIZE
);
270 static void kvm_hwpoison_page_add(ram_addr_t ram_addr
)
274 QLIST_FOREACH(page
, &hwpoison_page_list
, list
) {
275 if (page
->ram_addr
== ram_addr
) {
279 page
= g_malloc(sizeof(HWPoisonPage
));
280 page
->ram_addr
= ram_addr
;
281 QLIST_INSERT_HEAD(&hwpoison_page_list
, page
, list
);
284 static int kvm_get_mce_cap_supported(KVMState
*s
, uint64_t *mce_cap
,
289 r
= kvm_check_extension(s
, KVM_CAP_MCE
);
292 return kvm_ioctl(s
, KVM_X86_GET_MCE_CAP_SUPPORTED
, mce_cap
);
297 static void kvm_mce_inject(X86CPU
*cpu
, hwaddr paddr
, int code
)
299 CPUX86State
*env
= &cpu
->env
;
300 uint64_t status
= MCI_STATUS_VAL
| MCI_STATUS_UC
| MCI_STATUS_EN
|
301 MCI_STATUS_MISCV
| MCI_STATUS_ADDRV
| MCI_STATUS_S
;
302 uint64_t mcg_status
= MCG_STATUS_MCIP
;
304 if (code
== BUS_MCEERR_AR
) {
305 status
|= MCI_STATUS_AR
| 0x134;
306 mcg_status
|= MCG_STATUS_EIPV
;
309 mcg_status
|= MCG_STATUS_RIPV
;
311 cpu_x86_inject_mce(NULL
, cpu
, 9, status
, mcg_status
, paddr
,
312 (MCM_ADDR_PHYS
<< 6) | 0xc,
313 cpu_x86_support_mca_broadcast(env
) ?
314 MCE_INJECT_BROADCAST
: 0);
317 static void hardware_memory_error(void)
319 fprintf(stderr
, "Hardware memory error!\n");
323 int kvm_arch_on_sigbus_vcpu(CPUState
*c
, int code
, void *addr
)
325 X86CPU
*cpu
= X86_CPU(c
);
326 CPUX86State
*env
= &cpu
->env
;
330 if ((env
->mcg_cap
& MCG_SER_P
) && addr
331 && (code
== BUS_MCEERR_AR
|| code
== BUS_MCEERR_AO
)) {
332 if (qemu_ram_addr_from_host(addr
, &ram_addr
) == NULL
||
333 !kvm_physical_memory_addr_from_host(c
->kvm_state
, addr
, &paddr
)) {
334 fprintf(stderr
, "Hardware memory error for memory used by "
335 "QEMU itself instead of guest system!\n");
336 /* Hope we are lucky for AO MCE */
337 if (code
== BUS_MCEERR_AO
) {
340 hardware_memory_error();
343 kvm_hwpoison_page_add(ram_addr
);
344 kvm_mce_inject(cpu
, paddr
, code
);
346 if (code
== BUS_MCEERR_AO
) {
348 } else if (code
== BUS_MCEERR_AR
) {
349 hardware_memory_error();
357 int kvm_arch_on_sigbus(int code
, void *addr
)
359 X86CPU
*cpu
= X86_CPU(first_cpu
);
361 if ((cpu
->env
.mcg_cap
& MCG_SER_P
) && addr
&& code
== BUS_MCEERR_AO
) {
365 /* Hope we are lucky for AO MCE */
366 if (qemu_ram_addr_from_host(addr
, &ram_addr
) == NULL
||
367 !kvm_physical_memory_addr_from_host(first_cpu
->kvm_state
,
369 fprintf(stderr
, "Hardware memory error for memory used by "
370 "QEMU itself instead of guest system!: %p\n", addr
);
373 kvm_hwpoison_page_add(ram_addr
);
374 kvm_mce_inject(X86_CPU(first_cpu
), paddr
, code
);
376 if (code
== BUS_MCEERR_AO
) {
378 } else if (code
== BUS_MCEERR_AR
) {
379 hardware_memory_error();
387 static int kvm_inject_mce_oldstyle(X86CPU
*cpu
)
389 CPUX86State
*env
= &cpu
->env
;
391 if (!kvm_has_vcpu_events() && env
->exception_injected
== EXCP12_MCHK
) {
392 unsigned int bank
, bank_num
= env
->mcg_cap
& 0xff;
393 struct kvm_x86_mce mce
;
395 env
->exception_injected
= -1;
398 * There must be at least one bank in use if an MCE is pending.
399 * Find it and use its values for the event injection.
401 for (bank
= 0; bank
< bank_num
; bank
++) {
402 if (env
->mce_banks
[bank
* 4 + 1] & MCI_STATUS_VAL
) {
406 assert(bank
< bank_num
);
409 mce
.status
= env
->mce_banks
[bank
* 4 + 1];
410 mce
.mcg_status
= env
->mcg_status
;
411 mce
.addr
= env
->mce_banks
[bank
* 4 + 2];
412 mce
.misc
= env
->mce_banks
[bank
* 4 + 3];
414 return kvm_vcpu_ioctl(CPU(cpu
), KVM_X86_SET_MCE
, &mce
);
419 static void cpu_update_state(void *opaque
, int running
, RunState state
)
421 CPUX86State
*env
= opaque
;
424 env
->tsc_valid
= false;
428 unsigned long kvm_arch_vcpu_id(CPUState
*cs
)
430 X86CPU
*cpu
= X86_CPU(cs
);
431 return cpu
->env
.cpuid_apic_id
;
434 #ifndef KVM_CPUID_SIGNATURE_NEXT
435 #define KVM_CPUID_SIGNATURE_NEXT 0x40000100
438 static bool hyperv_hypercall_available(X86CPU
*cpu
)
440 return cpu
->hyperv_vapic
||
441 (cpu
->hyperv_spinlock_attempts
!= HYPERV_SPINLOCK_NEVER_RETRY
);
444 static bool hyperv_enabled(X86CPU
*cpu
)
446 CPUState
*cs
= CPU(cpu
);
447 return kvm_check_extension(cs
->kvm_state
, KVM_CAP_HYPERV
) > 0 &&
448 (hyperv_hypercall_available(cpu
) ||
450 cpu
->hyperv_relaxed_timing
);
453 static Error
*invtsc_mig_blocker
;
455 #define KVM_MAX_CPUID_ENTRIES 100
457 int kvm_arch_init_vcpu(CPUState
*cs
)
460 struct kvm_cpuid2 cpuid
;
461 struct kvm_cpuid_entry2 entries
[KVM_MAX_CPUID_ENTRIES
];
462 } QEMU_PACKED cpuid_data
;
463 X86CPU
*cpu
= X86_CPU(cs
);
464 CPUX86State
*env
= &cpu
->env
;
465 uint32_t limit
, i
, j
, cpuid_i
;
467 struct kvm_cpuid_entry2
*c
;
468 uint32_t signature
[3];
469 int kvm_base
= KVM_CPUID_SIGNATURE
;
472 memset(&cpuid_data
, 0, sizeof(cpuid_data
));
476 /* Paravirtualization CPUIDs */
477 if (hyperv_enabled(cpu
)) {
478 c
= &cpuid_data
.entries
[cpuid_i
++];
479 c
->function
= HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS
;
480 memcpy(signature
, "Microsoft Hv", 12);
481 c
->eax
= HYPERV_CPUID_MIN
;
482 c
->ebx
= signature
[0];
483 c
->ecx
= signature
[1];
484 c
->edx
= signature
[2];
486 c
= &cpuid_data
.entries
[cpuid_i
++];
487 c
->function
= HYPERV_CPUID_INTERFACE
;
488 memcpy(signature
, "Hv#1\0\0\0\0\0\0\0\0", 12);
489 c
->eax
= signature
[0];
494 c
= &cpuid_data
.entries
[cpuid_i
++];
495 c
->function
= HYPERV_CPUID_VERSION
;
499 c
= &cpuid_data
.entries
[cpuid_i
++];
500 c
->function
= HYPERV_CPUID_FEATURES
;
501 if (cpu
->hyperv_relaxed_timing
) {
502 c
->eax
|= HV_X64_MSR_HYPERCALL_AVAILABLE
;
504 if (cpu
->hyperv_vapic
) {
505 c
->eax
|= HV_X64_MSR_HYPERCALL_AVAILABLE
;
506 c
->eax
|= HV_X64_MSR_APIC_ACCESS_AVAILABLE
;
507 has_msr_hv_vapic
= true;
509 if (cpu
->hyperv_time
&&
510 kvm_check_extension(cs
->kvm_state
, KVM_CAP_HYPERV_TIME
) > 0) {
511 c
->eax
|= HV_X64_MSR_HYPERCALL_AVAILABLE
;
512 c
->eax
|= HV_X64_MSR_TIME_REF_COUNT_AVAILABLE
;
514 has_msr_hv_tsc
= true;
516 c
= &cpuid_data
.entries
[cpuid_i
++];
517 c
->function
= HYPERV_CPUID_ENLIGHTMENT_INFO
;
518 if (cpu
->hyperv_relaxed_timing
) {
519 c
->eax
|= HV_X64_RELAXED_TIMING_RECOMMENDED
;
521 if (has_msr_hv_vapic
) {
522 c
->eax
|= HV_X64_APIC_ACCESS_RECOMMENDED
;
524 c
->ebx
= cpu
->hyperv_spinlock_attempts
;
526 c
= &cpuid_data
.entries
[cpuid_i
++];
527 c
->function
= HYPERV_CPUID_IMPLEMENT_LIMITS
;
531 kvm_base
= KVM_CPUID_SIGNATURE_NEXT
;
532 has_msr_hv_hypercall
= true;
535 if (cpu
->expose_kvm
) {
536 memcpy(signature
, "KVMKVMKVM\0\0\0", 12);
537 c
= &cpuid_data
.entries
[cpuid_i
++];
538 c
->function
= KVM_CPUID_SIGNATURE
| kvm_base
;
539 c
->eax
= KVM_CPUID_FEATURES
| kvm_base
;
540 c
->ebx
= signature
[0];
541 c
->ecx
= signature
[1];
542 c
->edx
= signature
[2];
544 c
= &cpuid_data
.entries
[cpuid_i
++];
545 c
->function
= KVM_CPUID_FEATURES
| kvm_base
;
546 c
->eax
= env
->features
[FEAT_KVM
];
548 has_msr_async_pf_en
= c
->eax
& (1 << KVM_FEATURE_ASYNC_PF
);
550 has_msr_pv_eoi_en
= c
->eax
& (1 << KVM_FEATURE_PV_EOI
);
552 has_msr_kvm_steal_time
= c
->eax
& (1 << KVM_FEATURE_STEAL_TIME
);
555 cpu_x86_cpuid(env
, 0, 0, &limit
, &unused
, &unused
, &unused
);
557 for (i
= 0; i
<= limit
; i
++) {
558 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
559 fprintf(stderr
, "unsupported level value: 0x%x\n", limit
);
562 c
= &cpuid_data
.entries
[cpuid_i
++];
566 /* Keep reading function 2 till all the input is received */
570 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
|
571 KVM_CPUID_FLAG_STATE_READ_NEXT
;
572 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
573 times
= c
->eax
& 0xff;
575 for (j
= 1; j
< times
; ++j
) {
576 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
577 fprintf(stderr
, "cpuid_data is full, no space for "
578 "cpuid(eax:2):eax & 0xf = 0x%x\n", times
);
581 c
= &cpuid_data
.entries
[cpuid_i
++];
583 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
;
584 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
592 if (i
== 0xd && j
== 64) {
596 c
->flags
= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
598 cpu_x86_cpuid(env
, i
, j
, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
600 if (i
== 4 && c
->eax
== 0) {
603 if (i
== 0xb && !(c
->ecx
& 0xff00)) {
606 if (i
== 0xd && c
->eax
== 0) {
609 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
610 fprintf(stderr
, "cpuid_data is full, no space for "
611 "cpuid(eax:0x%x,ecx:0x%x)\n", i
, j
);
614 c
= &cpuid_data
.entries
[cpuid_i
++];
620 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
628 cpu_x86_cpuid(env
, 0x0a, 0, &ver
, &unused
, &unused
, &unused
);
629 if ((ver
& 0xff) > 0) {
630 has_msr_architectural_pmu
= true;
631 num_architectural_pmu_counters
= (ver
& 0xff00) >> 8;
633 /* Shouldn't be more than 32, since that's the number of bits
634 * available in EBX to tell us _which_ counters are available.
637 if (num_architectural_pmu_counters
> MAX_GP_COUNTERS
) {
638 num_architectural_pmu_counters
= MAX_GP_COUNTERS
;
643 cpu_x86_cpuid(env
, 0x80000000, 0, &limit
, &unused
, &unused
, &unused
);
645 for (i
= 0x80000000; i
<= limit
; i
++) {
646 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
647 fprintf(stderr
, "unsupported xlevel value: 0x%x\n", limit
);
650 c
= &cpuid_data
.entries
[cpuid_i
++];
654 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
657 /* Call Centaur's CPUID instructions they are supported. */
658 if (env
->cpuid_xlevel2
> 0) {
659 cpu_x86_cpuid(env
, 0xC0000000, 0, &limit
, &unused
, &unused
, &unused
);
661 for (i
= 0xC0000000; i
<= limit
; i
++) {
662 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
663 fprintf(stderr
, "unsupported xlevel2 value: 0x%x\n", limit
);
666 c
= &cpuid_data
.entries
[cpuid_i
++];
670 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
674 cpuid_data
.cpuid
.nent
= cpuid_i
;
676 if (((env
->cpuid_version
>> 8)&0xF) >= 6
677 && (env
->features
[FEAT_1_EDX
] & (CPUID_MCE
| CPUID_MCA
)) ==
678 (CPUID_MCE
| CPUID_MCA
)
679 && kvm_check_extension(cs
->kvm_state
, KVM_CAP_MCE
) > 0) {
684 ret
= kvm_get_mce_cap_supported(cs
->kvm_state
, &mcg_cap
, &banks
);
686 fprintf(stderr
, "kvm_get_mce_cap_supported: %s", strerror(-ret
));
690 if (banks
> MCE_BANKS_DEF
) {
691 banks
= MCE_BANKS_DEF
;
693 mcg_cap
&= MCE_CAP_DEF
;
695 ret
= kvm_vcpu_ioctl(cs
, KVM_X86_SETUP_MCE
, &mcg_cap
);
697 fprintf(stderr
, "KVM_X86_SETUP_MCE: %s", strerror(-ret
));
701 env
->mcg_cap
= mcg_cap
;
704 qemu_add_vm_change_state_handler(cpu_update_state
, env
);
706 c
= cpuid_find_entry(&cpuid_data
.cpuid
, 1, 0);
708 has_msr_feature_control
= !!(c
->ecx
& CPUID_EXT_VMX
) ||
709 !!(c
->ecx
& CPUID_EXT_SMX
);
712 c
= cpuid_find_entry(&cpuid_data
.cpuid
, 0x80000007, 0);
713 if (c
&& (c
->edx
& 1<<8) && invtsc_mig_blocker
== NULL
) {
715 error_setg(&invtsc_mig_blocker
,
716 "State blocked by non-migratable CPU device"
718 migrate_add_blocker(invtsc_mig_blocker
);
720 vmstate_x86_cpu
.unmigratable
= 1;
723 cpuid_data
.cpuid
.padding
= 0;
724 r
= kvm_vcpu_ioctl(cs
, KVM_SET_CPUID2
, &cpuid_data
);
729 r
= kvm_check_extension(cs
->kvm_state
, KVM_CAP_TSC_CONTROL
);
730 if (r
&& env
->tsc_khz
) {
731 r
= kvm_vcpu_ioctl(cs
, KVM_SET_TSC_KHZ
, env
->tsc_khz
);
733 fprintf(stderr
, "KVM_SET_TSC_KHZ failed\n");
738 if (kvm_has_xsave()) {
739 env
->kvm_xsave_buf
= qemu_memalign(4096, sizeof(struct kvm_xsave
));
745 void kvm_arch_reset_vcpu(X86CPU
*cpu
)
747 CPUX86State
*env
= &cpu
->env
;
749 env
->exception_injected
= -1;
750 env
->interrupt_injected
= -1;
752 if (kvm_irqchip_in_kernel()) {
753 env
->mp_state
= cpu_is_bsp(cpu
) ? KVM_MP_STATE_RUNNABLE
:
754 KVM_MP_STATE_UNINITIALIZED
;
756 env
->mp_state
= KVM_MP_STATE_RUNNABLE
;
760 void kvm_arch_do_init_vcpu(X86CPU
*cpu
)
762 CPUX86State
*env
= &cpu
->env
;
764 /* APs get directly into wait-for-SIPI state. */
765 if (env
->mp_state
== KVM_MP_STATE_UNINITIALIZED
) {
766 env
->mp_state
= KVM_MP_STATE_INIT_RECEIVED
;
770 static int kvm_get_supported_msrs(KVMState
*s
)
772 static int kvm_supported_msrs
;
776 if (kvm_supported_msrs
== 0) {
777 struct kvm_msr_list msr_list
, *kvm_msr_list
;
779 kvm_supported_msrs
= -1;
781 /* Obtain MSR list from KVM. These are the MSRs that we must
784 ret
= kvm_ioctl(s
, KVM_GET_MSR_INDEX_LIST
, &msr_list
);
785 if (ret
< 0 && ret
!= -E2BIG
) {
788 /* Old kernel modules had a bug and could write beyond the provided
789 memory. Allocate at least a safe amount of 1K. */
790 kvm_msr_list
= g_malloc0(MAX(1024, sizeof(msr_list
) +
792 sizeof(msr_list
.indices
[0])));
794 kvm_msr_list
->nmsrs
= msr_list
.nmsrs
;
795 ret
= kvm_ioctl(s
, KVM_GET_MSR_INDEX_LIST
, kvm_msr_list
);
799 for (i
= 0; i
< kvm_msr_list
->nmsrs
; i
++) {
800 if (kvm_msr_list
->indices
[i
] == MSR_STAR
) {
804 if (kvm_msr_list
->indices
[i
] == MSR_VM_HSAVE_PA
) {
805 has_msr_hsave_pa
= true;
808 if (kvm_msr_list
->indices
[i
] == MSR_TSC_ADJUST
) {
809 has_msr_tsc_adjust
= true;
812 if (kvm_msr_list
->indices
[i
] == MSR_IA32_TSCDEADLINE
) {
813 has_msr_tsc_deadline
= true;
816 if (kvm_msr_list
->indices
[i
] == MSR_IA32_MISC_ENABLE
) {
817 has_msr_misc_enable
= true;
820 if (kvm_msr_list
->indices
[i
] == MSR_IA32_BNDCFGS
) {
821 has_msr_bndcfgs
= true;
827 g_free(kvm_msr_list
);
833 int kvm_arch_init(KVMState
*s
)
835 uint64_t identity_base
= 0xfffbc000;
838 struct utsname utsname
;
840 ret
= kvm_get_supported_msrs(s
);
846 lm_capable_kernel
= strcmp(utsname
.machine
, "x86_64") == 0;
849 * On older Intel CPUs, KVM uses vm86 mode to emulate 16-bit code directly.
850 * In order to use vm86 mode, an EPT identity map and a TSS are needed.
851 * Since these must be part of guest physical memory, we need to allocate
852 * them, both by setting their start addresses in the kernel and by
853 * creating a corresponding e820 entry. We need 4 pages before the BIOS.
855 * Older KVM versions may not support setting the identity map base. In
856 * that case we need to stick with the default, i.e. a 256K maximum BIOS
859 if (kvm_check_extension(s
, KVM_CAP_SET_IDENTITY_MAP_ADDR
)) {
860 /* Allows up to 16M BIOSes. */
861 identity_base
= 0xfeffc000;
863 ret
= kvm_vm_ioctl(s
, KVM_SET_IDENTITY_MAP_ADDR
, &identity_base
);
869 /* Set TSS base one page after EPT identity map. */
870 ret
= kvm_vm_ioctl(s
, KVM_SET_TSS_ADDR
, identity_base
+ 0x1000);
875 /* Tell fw_cfg to notify the BIOS to reserve the range. */
876 ret
= e820_add_entry(identity_base
, 0x4000, E820_RESERVED
);
878 fprintf(stderr
, "e820_add_entry() table is full\n");
881 qemu_register_reset(kvm_unpoison_all
, NULL
);
883 shadow_mem
= qemu_opt_get_size(qemu_get_machine_opts(),
884 "kvm_shadow_mem", -1);
885 if (shadow_mem
!= -1) {
887 ret
= kvm_vm_ioctl(s
, KVM_SET_NR_MMU_PAGES
, shadow_mem
);
895 static void set_v8086_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
897 lhs
->selector
= rhs
->selector
;
898 lhs
->base
= rhs
->base
;
899 lhs
->limit
= rhs
->limit
;
911 static void set_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
913 unsigned flags
= rhs
->flags
;
914 lhs
->selector
= rhs
->selector
;
915 lhs
->base
= rhs
->base
;
916 lhs
->limit
= rhs
->limit
;
917 lhs
->type
= (flags
>> DESC_TYPE_SHIFT
) & 15;
918 lhs
->present
= (flags
& DESC_P_MASK
) != 0;
919 lhs
->dpl
= (flags
>> DESC_DPL_SHIFT
) & 3;
920 lhs
->db
= (flags
>> DESC_B_SHIFT
) & 1;
921 lhs
->s
= (flags
& DESC_S_MASK
) != 0;
922 lhs
->l
= (flags
>> DESC_L_SHIFT
) & 1;
923 lhs
->g
= (flags
& DESC_G_MASK
) != 0;
924 lhs
->avl
= (flags
& DESC_AVL_MASK
) != 0;
929 static void get_seg(SegmentCache
*lhs
, const struct kvm_segment
*rhs
)
931 lhs
->selector
= rhs
->selector
;
932 lhs
->base
= rhs
->base
;
933 lhs
->limit
= rhs
->limit
;
934 lhs
->flags
= (rhs
->type
<< DESC_TYPE_SHIFT
) |
935 (rhs
->present
* DESC_P_MASK
) |
936 (rhs
->dpl
<< DESC_DPL_SHIFT
) |
937 (rhs
->db
<< DESC_B_SHIFT
) |
938 (rhs
->s
* DESC_S_MASK
) |
939 (rhs
->l
<< DESC_L_SHIFT
) |
940 (rhs
->g
* DESC_G_MASK
) |
941 (rhs
->avl
* DESC_AVL_MASK
);
944 static void kvm_getput_reg(__u64
*kvm_reg
, target_ulong
*qemu_reg
, int set
)
947 *kvm_reg
= *qemu_reg
;
949 *qemu_reg
= *kvm_reg
;
953 static int kvm_getput_regs(X86CPU
*cpu
, int set
)
955 CPUX86State
*env
= &cpu
->env
;
956 struct kvm_regs regs
;
960 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_REGS
, ®s
);
966 kvm_getput_reg(®s
.rax
, &env
->regs
[R_EAX
], set
);
967 kvm_getput_reg(®s
.rbx
, &env
->regs
[R_EBX
], set
);
968 kvm_getput_reg(®s
.rcx
, &env
->regs
[R_ECX
], set
);
969 kvm_getput_reg(®s
.rdx
, &env
->regs
[R_EDX
], set
);
970 kvm_getput_reg(®s
.rsi
, &env
->regs
[R_ESI
], set
);
971 kvm_getput_reg(®s
.rdi
, &env
->regs
[R_EDI
], set
);
972 kvm_getput_reg(®s
.rsp
, &env
->regs
[R_ESP
], set
);
973 kvm_getput_reg(®s
.rbp
, &env
->regs
[R_EBP
], set
);
975 kvm_getput_reg(®s
.r8
, &env
->regs
[8], set
);
976 kvm_getput_reg(®s
.r9
, &env
->regs
[9], set
);
977 kvm_getput_reg(®s
.r10
, &env
->regs
[10], set
);
978 kvm_getput_reg(®s
.r11
, &env
->regs
[11], set
);
979 kvm_getput_reg(®s
.r12
, &env
->regs
[12], set
);
980 kvm_getput_reg(®s
.r13
, &env
->regs
[13], set
);
981 kvm_getput_reg(®s
.r14
, &env
->regs
[14], set
);
982 kvm_getput_reg(®s
.r15
, &env
->regs
[15], set
);
985 kvm_getput_reg(®s
.rflags
, &env
->eflags
, set
);
986 kvm_getput_reg(®s
.rip
, &env
->eip
, set
);
989 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_REGS
, ®s
);
995 static int kvm_put_fpu(X86CPU
*cpu
)
997 CPUX86State
*env
= &cpu
->env
;
1001 memset(&fpu
, 0, sizeof fpu
);
1002 fpu
.fsw
= env
->fpus
& ~(7 << 11);
1003 fpu
.fsw
|= (env
->fpstt
& 7) << 11;
1004 fpu
.fcw
= env
->fpuc
;
1005 fpu
.last_opcode
= env
->fpop
;
1006 fpu
.last_ip
= env
->fpip
;
1007 fpu
.last_dp
= env
->fpdp
;
1008 for (i
= 0; i
< 8; ++i
) {
1009 fpu
.ftwx
|= (!env
->fptags
[i
]) << i
;
1011 memcpy(fpu
.fpr
, env
->fpregs
, sizeof env
->fpregs
);
1012 memcpy(fpu
.xmm
, env
->xmm_regs
, sizeof env
->xmm_regs
);
1013 fpu
.mxcsr
= env
->mxcsr
;
1015 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_FPU
, &fpu
);
1018 #define XSAVE_FCW_FSW 0
1019 #define XSAVE_FTW_FOP 1
1020 #define XSAVE_CWD_RIP 2
1021 #define XSAVE_CWD_RDP 4
1022 #define XSAVE_MXCSR 6
1023 #define XSAVE_ST_SPACE 8
1024 #define XSAVE_XMM_SPACE 40
1025 #define XSAVE_XSTATE_BV 128
1026 #define XSAVE_YMMH_SPACE 144
1027 #define XSAVE_BNDREGS 240
1028 #define XSAVE_BNDCSR 256
1030 static int kvm_put_xsave(X86CPU
*cpu
)
1032 CPUX86State
*env
= &cpu
->env
;
1033 struct kvm_xsave
* xsave
= env
->kvm_xsave_buf
;
1034 uint16_t cwd
, swd
, twd
;
1037 if (!kvm_has_xsave()) {
1038 return kvm_put_fpu(cpu
);
1041 memset(xsave
, 0, sizeof(struct kvm_xsave
));
1043 swd
= env
->fpus
& ~(7 << 11);
1044 swd
|= (env
->fpstt
& 7) << 11;
1046 for (i
= 0; i
< 8; ++i
) {
1047 twd
|= (!env
->fptags
[i
]) << i
;
1049 xsave
->region
[XSAVE_FCW_FSW
] = (uint32_t)(swd
<< 16) + cwd
;
1050 xsave
->region
[XSAVE_FTW_FOP
] = (uint32_t)(env
->fpop
<< 16) + twd
;
1051 memcpy(&xsave
->region
[XSAVE_CWD_RIP
], &env
->fpip
, sizeof(env
->fpip
));
1052 memcpy(&xsave
->region
[XSAVE_CWD_RDP
], &env
->fpdp
, sizeof(env
->fpdp
));
1053 memcpy(&xsave
->region
[XSAVE_ST_SPACE
], env
->fpregs
,
1054 sizeof env
->fpregs
);
1055 memcpy(&xsave
->region
[XSAVE_XMM_SPACE
], env
->xmm_regs
,
1056 sizeof env
->xmm_regs
);
1057 xsave
->region
[XSAVE_MXCSR
] = env
->mxcsr
;
1058 *(uint64_t *)&xsave
->region
[XSAVE_XSTATE_BV
] = env
->xstate_bv
;
1059 memcpy(&xsave
->region
[XSAVE_YMMH_SPACE
], env
->ymmh_regs
,
1060 sizeof env
->ymmh_regs
);
1061 memcpy(&xsave
->region
[XSAVE_BNDREGS
], env
->bnd_regs
,
1062 sizeof env
->bnd_regs
);
1063 memcpy(&xsave
->region
[XSAVE_BNDCSR
], &env
->bndcs_regs
,
1064 sizeof(env
->bndcs_regs
));
1065 r
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_XSAVE
, xsave
);
1069 static int kvm_put_xcrs(X86CPU
*cpu
)
1071 CPUX86State
*env
= &cpu
->env
;
1072 struct kvm_xcrs xcrs
;
1074 if (!kvm_has_xcrs()) {
1080 xcrs
.xcrs
[0].xcr
= 0;
1081 xcrs
.xcrs
[0].value
= env
->xcr0
;
1082 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_XCRS
, &xcrs
);
1085 static int kvm_put_sregs(X86CPU
*cpu
)
1087 CPUX86State
*env
= &cpu
->env
;
1088 struct kvm_sregs sregs
;
1090 memset(sregs
.interrupt_bitmap
, 0, sizeof(sregs
.interrupt_bitmap
));
1091 if (env
->interrupt_injected
>= 0) {
1092 sregs
.interrupt_bitmap
[env
->interrupt_injected
/ 64] |=
1093 (uint64_t)1 << (env
->interrupt_injected
% 64);
1096 if ((env
->eflags
& VM_MASK
)) {
1097 set_v8086_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
1098 set_v8086_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
1099 set_v8086_seg(&sregs
.es
, &env
->segs
[R_ES
]);
1100 set_v8086_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
1101 set_v8086_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
1102 set_v8086_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
1104 set_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
1105 set_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
1106 set_seg(&sregs
.es
, &env
->segs
[R_ES
]);
1107 set_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
1108 set_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
1109 set_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
1112 set_seg(&sregs
.tr
, &env
->tr
);
1113 set_seg(&sregs
.ldt
, &env
->ldt
);
1115 sregs
.idt
.limit
= env
->idt
.limit
;
1116 sregs
.idt
.base
= env
->idt
.base
;
1117 memset(sregs
.idt
.padding
, 0, sizeof sregs
.idt
.padding
);
1118 sregs
.gdt
.limit
= env
->gdt
.limit
;
1119 sregs
.gdt
.base
= env
->gdt
.base
;
1120 memset(sregs
.gdt
.padding
, 0, sizeof sregs
.gdt
.padding
);
1122 sregs
.cr0
= env
->cr
[0];
1123 sregs
.cr2
= env
->cr
[2];
1124 sregs
.cr3
= env
->cr
[3];
1125 sregs
.cr4
= env
->cr
[4];
1127 sregs
.cr8
= cpu_get_apic_tpr(cpu
->apic_state
);
1128 sregs
.apic_base
= cpu_get_apic_base(cpu
->apic_state
);
1130 sregs
.efer
= env
->efer
;
1132 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_SREGS
, &sregs
);
1135 static void kvm_msr_entry_set(struct kvm_msr_entry
*entry
,
1136 uint32_t index
, uint64_t value
)
1138 entry
->index
= index
;
1139 entry
->data
= value
;
1142 static int kvm_put_tscdeadline_msr(X86CPU
*cpu
)
1144 CPUX86State
*env
= &cpu
->env
;
1146 struct kvm_msrs info
;
1147 struct kvm_msr_entry entries
[1];
1149 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
1151 if (!has_msr_tsc_deadline
) {
1155 kvm_msr_entry_set(&msrs
[0], MSR_IA32_TSCDEADLINE
, env
->tsc_deadline
);
1157 msr_data
.info
.nmsrs
= 1;
1159 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MSRS
, &msr_data
);
1163 * Provide a separate write service for the feature control MSR in order to
1164 * kick the VCPU out of VMXON or even guest mode on reset. This has to be done
1165 * before writing any other state because forcibly leaving nested mode
1166 * invalidates the VCPU state.
1168 static int kvm_put_msr_feature_control(X86CPU
*cpu
)
1171 struct kvm_msrs info
;
1172 struct kvm_msr_entry entry
;
1175 kvm_msr_entry_set(&msr_data
.entry
, MSR_IA32_FEATURE_CONTROL
,
1176 cpu
->env
.msr_ia32_feature_control
);
1177 msr_data
.info
.nmsrs
= 1;
1178 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MSRS
, &msr_data
);
1181 static int kvm_put_msrs(X86CPU
*cpu
, int level
)
1183 CPUX86State
*env
= &cpu
->env
;
1185 struct kvm_msrs info
;
1186 struct kvm_msr_entry entries
[100];
1188 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
1191 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_CS
, env
->sysenter_cs
);
1192 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_ESP
, env
->sysenter_esp
);
1193 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_EIP
, env
->sysenter_eip
);
1194 kvm_msr_entry_set(&msrs
[n
++], MSR_PAT
, env
->pat
);
1196 kvm_msr_entry_set(&msrs
[n
++], MSR_STAR
, env
->star
);
1198 if (has_msr_hsave_pa
) {
1199 kvm_msr_entry_set(&msrs
[n
++], MSR_VM_HSAVE_PA
, env
->vm_hsave
);
1201 if (has_msr_tsc_adjust
) {
1202 kvm_msr_entry_set(&msrs
[n
++], MSR_TSC_ADJUST
, env
->tsc_adjust
);
1204 if (has_msr_misc_enable
) {
1205 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_MISC_ENABLE
,
1206 env
->msr_ia32_misc_enable
);
1208 if (has_msr_bndcfgs
) {
1209 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_BNDCFGS
, env
->msr_bndcfgs
);
1211 #ifdef TARGET_X86_64
1212 if (lm_capable_kernel
) {
1213 kvm_msr_entry_set(&msrs
[n
++], MSR_CSTAR
, env
->cstar
);
1214 kvm_msr_entry_set(&msrs
[n
++], MSR_KERNELGSBASE
, env
->kernelgsbase
);
1215 kvm_msr_entry_set(&msrs
[n
++], MSR_FMASK
, env
->fmask
);
1216 kvm_msr_entry_set(&msrs
[n
++], MSR_LSTAR
, env
->lstar
);
1220 * The following MSRs have side effects on the guest or are too heavy
1221 * for normal writeback. Limit them to reset or full state updates.
1223 if (level
>= KVM_PUT_RESET_STATE
) {
1224 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_TSC
, env
->tsc
);
1225 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_SYSTEM_TIME
,
1226 env
->system_time_msr
);
1227 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_WALL_CLOCK
, env
->wall_clock_msr
);
1228 if (has_msr_async_pf_en
) {
1229 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_ASYNC_PF_EN
,
1230 env
->async_pf_en_msr
);
1232 if (has_msr_pv_eoi_en
) {
1233 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_PV_EOI_EN
,
1234 env
->pv_eoi_en_msr
);
1236 if (has_msr_kvm_steal_time
) {
1237 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_STEAL_TIME
,
1238 env
->steal_time_msr
);
1240 if (has_msr_architectural_pmu
) {
1241 /* Stop the counter. */
1242 kvm_msr_entry_set(&msrs
[n
++], MSR_CORE_PERF_FIXED_CTR_CTRL
, 0);
1243 kvm_msr_entry_set(&msrs
[n
++], MSR_CORE_PERF_GLOBAL_CTRL
, 0);
1245 /* Set the counter values. */
1246 for (i
= 0; i
< MAX_FIXED_COUNTERS
; i
++) {
1247 kvm_msr_entry_set(&msrs
[n
++], MSR_CORE_PERF_FIXED_CTR0
+ i
,
1248 env
->msr_fixed_counters
[i
]);
1250 for (i
= 0; i
< num_architectural_pmu_counters
; i
++) {
1251 kvm_msr_entry_set(&msrs
[n
++], MSR_P6_PERFCTR0
+ i
,
1252 env
->msr_gp_counters
[i
]);
1253 kvm_msr_entry_set(&msrs
[n
++], MSR_P6_EVNTSEL0
+ i
,
1254 env
->msr_gp_evtsel
[i
]);
1256 kvm_msr_entry_set(&msrs
[n
++], MSR_CORE_PERF_GLOBAL_STATUS
,
1257 env
->msr_global_status
);
1258 kvm_msr_entry_set(&msrs
[n
++], MSR_CORE_PERF_GLOBAL_OVF_CTRL
,
1259 env
->msr_global_ovf_ctrl
);
1261 /* Now start the PMU. */
1262 kvm_msr_entry_set(&msrs
[n
++], MSR_CORE_PERF_FIXED_CTR_CTRL
,
1263 env
->msr_fixed_ctr_ctrl
);
1264 kvm_msr_entry_set(&msrs
[n
++], MSR_CORE_PERF_GLOBAL_CTRL
,
1265 env
->msr_global_ctrl
);
1267 if (has_msr_hv_hypercall
) {
1268 kvm_msr_entry_set(&msrs
[n
++], HV_X64_MSR_GUEST_OS_ID
,
1269 env
->msr_hv_guest_os_id
);
1270 kvm_msr_entry_set(&msrs
[n
++], HV_X64_MSR_HYPERCALL
,
1271 env
->msr_hv_hypercall
);
1273 if (has_msr_hv_vapic
) {
1274 kvm_msr_entry_set(&msrs
[n
++], HV_X64_MSR_APIC_ASSIST_PAGE
,
1277 if (has_msr_hv_tsc
) {
1278 kvm_msr_entry_set(&msrs
[n
++], HV_X64_MSR_REFERENCE_TSC
,
1282 /* Note: MSR_IA32_FEATURE_CONTROL is written separately, see
1283 * kvm_put_msr_feature_control. */
1288 kvm_msr_entry_set(&msrs
[n
++], MSR_MCG_STATUS
, env
->mcg_status
);
1289 kvm_msr_entry_set(&msrs
[n
++], MSR_MCG_CTL
, env
->mcg_ctl
);
1290 for (i
= 0; i
< (env
->mcg_cap
& 0xff) * 4; i
++) {
1291 kvm_msr_entry_set(&msrs
[n
++], MSR_MC0_CTL
+ i
, env
->mce_banks
[i
]);
1295 msr_data
.info
.nmsrs
= n
;
1297 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MSRS
, &msr_data
);
1302 static int kvm_get_fpu(X86CPU
*cpu
)
1304 CPUX86State
*env
= &cpu
->env
;
1308 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_FPU
, &fpu
);
1313 env
->fpstt
= (fpu
.fsw
>> 11) & 7;
1314 env
->fpus
= fpu
.fsw
;
1315 env
->fpuc
= fpu
.fcw
;
1316 env
->fpop
= fpu
.last_opcode
;
1317 env
->fpip
= fpu
.last_ip
;
1318 env
->fpdp
= fpu
.last_dp
;
1319 for (i
= 0; i
< 8; ++i
) {
1320 env
->fptags
[i
] = !((fpu
.ftwx
>> i
) & 1);
1322 memcpy(env
->fpregs
, fpu
.fpr
, sizeof env
->fpregs
);
1323 memcpy(env
->xmm_regs
, fpu
.xmm
, sizeof env
->xmm_regs
);
1324 env
->mxcsr
= fpu
.mxcsr
;
1329 static int kvm_get_xsave(X86CPU
*cpu
)
1331 CPUX86State
*env
= &cpu
->env
;
1332 struct kvm_xsave
* xsave
= env
->kvm_xsave_buf
;
1334 uint16_t cwd
, swd
, twd
;
1336 if (!kvm_has_xsave()) {
1337 return kvm_get_fpu(cpu
);
1340 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_XSAVE
, xsave
);
1345 cwd
= (uint16_t)xsave
->region
[XSAVE_FCW_FSW
];
1346 swd
= (uint16_t)(xsave
->region
[XSAVE_FCW_FSW
] >> 16);
1347 twd
= (uint16_t)xsave
->region
[XSAVE_FTW_FOP
];
1348 env
->fpop
= (uint16_t)(xsave
->region
[XSAVE_FTW_FOP
] >> 16);
1349 env
->fpstt
= (swd
>> 11) & 7;
1352 for (i
= 0; i
< 8; ++i
) {
1353 env
->fptags
[i
] = !((twd
>> i
) & 1);
1355 memcpy(&env
->fpip
, &xsave
->region
[XSAVE_CWD_RIP
], sizeof(env
->fpip
));
1356 memcpy(&env
->fpdp
, &xsave
->region
[XSAVE_CWD_RDP
], sizeof(env
->fpdp
));
1357 env
->mxcsr
= xsave
->region
[XSAVE_MXCSR
];
1358 memcpy(env
->fpregs
, &xsave
->region
[XSAVE_ST_SPACE
],
1359 sizeof env
->fpregs
);
1360 memcpy(env
->xmm_regs
, &xsave
->region
[XSAVE_XMM_SPACE
],
1361 sizeof env
->xmm_regs
);
1362 env
->xstate_bv
= *(uint64_t *)&xsave
->region
[XSAVE_XSTATE_BV
];
1363 memcpy(env
->ymmh_regs
, &xsave
->region
[XSAVE_YMMH_SPACE
],
1364 sizeof env
->ymmh_regs
);
1365 memcpy(env
->bnd_regs
, &xsave
->region
[XSAVE_BNDREGS
],
1366 sizeof env
->bnd_regs
);
1367 memcpy(&env
->bndcs_regs
, &xsave
->region
[XSAVE_BNDCSR
],
1368 sizeof(env
->bndcs_regs
));
1372 static int kvm_get_xcrs(X86CPU
*cpu
)
1374 CPUX86State
*env
= &cpu
->env
;
1376 struct kvm_xcrs xcrs
;
1378 if (!kvm_has_xcrs()) {
1382 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_XCRS
, &xcrs
);
1387 for (i
= 0; i
< xcrs
.nr_xcrs
; i
++) {
1388 /* Only support xcr0 now */
1389 if (xcrs
.xcrs
[i
].xcr
== 0) {
1390 env
->xcr0
= xcrs
.xcrs
[i
].value
;
1397 static int kvm_get_sregs(X86CPU
*cpu
)
1399 CPUX86State
*env
= &cpu
->env
;
1400 struct kvm_sregs sregs
;
1404 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_SREGS
, &sregs
);
1409 /* There can only be one pending IRQ set in the bitmap at a time, so try
1410 to find it and save its number instead (-1 for none). */
1411 env
->interrupt_injected
= -1;
1412 for (i
= 0; i
< ARRAY_SIZE(sregs
.interrupt_bitmap
); i
++) {
1413 if (sregs
.interrupt_bitmap
[i
]) {
1414 bit
= ctz64(sregs
.interrupt_bitmap
[i
]);
1415 env
->interrupt_injected
= i
* 64 + bit
;
1420 get_seg(&env
->segs
[R_CS
], &sregs
.cs
);
1421 get_seg(&env
->segs
[R_DS
], &sregs
.ds
);
1422 get_seg(&env
->segs
[R_ES
], &sregs
.es
);
1423 get_seg(&env
->segs
[R_FS
], &sregs
.fs
);
1424 get_seg(&env
->segs
[R_GS
], &sregs
.gs
);
1425 get_seg(&env
->segs
[R_SS
], &sregs
.ss
);
1427 get_seg(&env
->tr
, &sregs
.tr
);
1428 get_seg(&env
->ldt
, &sregs
.ldt
);
1430 env
->idt
.limit
= sregs
.idt
.limit
;
1431 env
->idt
.base
= sregs
.idt
.base
;
1432 env
->gdt
.limit
= sregs
.gdt
.limit
;
1433 env
->gdt
.base
= sregs
.gdt
.base
;
1435 env
->cr
[0] = sregs
.cr0
;
1436 env
->cr
[2] = sregs
.cr2
;
1437 env
->cr
[3] = sregs
.cr3
;
1438 env
->cr
[4] = sregs
.cr4
;
1440 env
->efer
= sregs
.efer
;
1442 /* changes to apic base and cr8/tpr are read back via kvm_arch_post_run */
1444 #define HFLAG_COPY_MASK \
1445 ~( HF_CPL_MASK | HF_PE_MASK | HF_MP_MASK | HF_EM_MASK | \
1446 HF_TS_MASK | HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK | \
1447 HF_OSFXSR_MASK | HF_LMA_MASK | HF_CS32_MASK | \
1448 HF_SS32_MASK | HF_CS64_MASK | HF_ADDSEG_MASK)
1450 hflags
= (env
->segs
[R_SS
].flags
>> DESC_DPL_SHIFT
) & HF_CPL_MASK
;
1451 hflags
|= (env
->cr
[0] & CR0_PE_MASK
) << (HF_PE_SHIFT
- CR0_PE_SHIFT
);
1452 hflags
|= (env
->cr
[0] << (HF_MP_SHIFT
- CR0_MP_SHIFT
)) &
1453 (HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
);
1454 hflags
|= (env
->eflags
& (HF_TF_MASK
| HF_VM_MASK
| HF_IOPL_MASK
));
1455 hflags
|= (env
->cr
[4] & CR4_OSFXSR_MASK
) <<
1456 (HF_OSFXSR_SHIFT
- CR4_OSFXSR_SHIFT
);
1458 if (env
->efer
& MSR_EFER_LMA
) {
1459 hflags
|= HF_LMA_MASK
;
1462 if ((hflags
& HF_LMA_MASK
) && (env
->segs
[R_CS
].flags
& DESC_L_MASK
)) {
1463 hflags
|= HF_CS32_MASK
| HF_SS32_MASK
| HF_CS64_MASK
;
1465 hflags
|= (env
->segs
[R_CS
].flags
& DESC_B_MASK
) >>
1466 (DESC_B_SHIFT
- HF_CS32_SHIFT
);
1467 hflags
|= (env
->segs
[R_SS
].flags
& DESC_B_MASK
) >>
1468 (DESC_B_SHIFT
- HF_SS32_SHIFT
);
1469 if (!(env
->cr
[0] & CR0_PE_MASK
) || (env
->eflags
& VM_MASK
) ||
1470 !(hflags
& HF_CS32_MASK
)) {
1471 hflags
|= HF_ADDSEG_MASK
;
1473 hflags
|= ((env
->segs
[R_DS
].base
| env
->segs
[R_ES
].base
|
1474 env
->segs
[R_SS
].base
) != 0) << HF_ADDSEG_SHIFT
;
1477 env
->hflags
= (env
->hflags
& HFLAG_COPY_MASK
) | hflags
;
1482 static int kvm_get_msrs(X86CPU
*cpu
)
1484 CPUX86State
*env
= &cpu
->env
;
1486 struct kvm_msrs info
;
1487 struct kvm_msr_entry entries
[100];
1489 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
1493 msrs
[n
++].index
= MSR_IA32_SYSENTER_CS
;
1494 msrs
[n
++].index
= MSR_IA32_SYSENTER_ESP
;
1495 msrs
[n
++].index
= MSR_IA32_SYSENTER_EIP
;
1496 msrs
[n
++].index
= MSR_PAT
;
1498 msrs
[n
++].index
= MSR_STAR
;
1500 if (has_msr_hsave_pa
) {
1501 msrs
[n
++].index
= MSR_VM_HSAVE_PA
;
1503 if (has_msr_tsc_adjust
) {
1504 msrs
[n
++].index
= MSR_TSC_ADJUST
;
1506 if (has_msr_tsc_deadline
) {
1507 msrs
[n
++].index
= MSR_IA32_TSCDEADLINE
;
1509 if (has_msr_misc_enable
) {
1510 msrs
[n
++].index
= MSR_IA32_MISC_ENABLE
;
1512 if (has_msr_feature_control
) {
1513 msrs
[n
++].index
= MSR_IA32_FEATURE_CONTROL
;
1515 if (has_msr_bndcfgs
) {
1516 msrs
[n
++].index
= MSR_IA32_BNDCFGS
;
1519 if (!env
->tsc_valid
) {
1520 msrs
[n
++].index
= MSR_IA32_TSC
;
1521 env
->tsc_valid
= !runstate_is_running();
1524 #ifdef TARGET_X86_64
1525 if (lm_capable_kernel
) {
1526 msrs
[n
++].index
= MSR_CSTAR
;
1527 msrs
[n
++].index
= MSR_KERNELGSBASE
;
1528 msrs
[n
++].index
= MSR_FMASK
;
1529 msrs
[n
++].index
= MSR_LSTAR
;
1532 msrs
[n
++].index
= MSR_KVM_SYSTEM_TIME
;
1533 msrs
[n
++].index
= MSR_KVM_WALL_CLOCK
;
1534 if (has_msr_async_pf_en
) {
1535 msrs
[n
++].index
= MSR_KVM_ASYNC_PF_EN
;
1537 if (has_msr_pv_eoi_en
) {
1538 msrs
[n
++].index
= MSR_KVM_PV_EOI_EN
;
1540 if (has_msr_kvm_steal_time
) {
1541 msrs
[n
++].index
= MSR_KVM_STEAL_TIME
;
1543 if (has_msr_architectural_pmu
) {
1544 msrs
[n
++].index
= MSR_CORE_PERF_FIXED_CTR_CTRL
;
1545 msrs
[n
++].index
= MSR_CORE_PERF_GLOBAL_CTRL
;
1546 msrs
[n
++].index
= MSR_CORE_PERF_GLOBAL_STATUS
;
1547 msrs
[n
++].index
= MSR_CORE_PERF_GLOBAL_OVF_CTRL
;
1548 for (i
= 0; i
< MAX_FIXED_COUNTERS
; i
++) {
1549 msrs
[n
++].index
= MSR_CORE_PERF_FIXED_CTR0
+ i
;
1551 for (i
= 0; i
< num_architectural_pmu_counters
; i
++) {
1552 msrs
[n
++].index
= MSR_P6_PERFCTR0
+ i
;
1553 msrs
[n
++].index
= MSR_P6_EVNTSEL0
+ i
;
1558 msrs
[n
++].index
= MSR_MCG_STATUS
;
1559 msrs
[n
++].index
= MSR_MCG_CTL
;
1560 for (i
= 0; i
< (env
->mcg_cap
& 0xff) * 4; i
++) {
1561 msrs
[n
++].index
= MSR_MC0_CTL
+ i
;
1565 if (has_msr_hv_hypercall
) {
1566 msrs
[n
++].index
= HV_X64_MSR_HYPERCALL
;
1567 msrs
[n
++].index
= HV_X64_MSR_GUEST_OS_ID
;
1569 if (has_msr_hv_vapic
) {
1570 msrs
[n
++].index
= HV_X64_MSR_APIC_ASSIST_PAGE
;
1572 if (has_msr_hv_tsc
) {
1573 msrs
[n
++].index
= HV_X64_MSR_REFERENCE_TSC
;
1576 msr_data
.info
.nmsrs
= n
;
1577 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_MSRS
, &msr_data
);
1582 for (i
= 0; i
< ret
; i
++) {
1583 uint32_t index
= msrs
[i
].index
;
1585 case MSR_IA32_SYSENTER_CS
:
1586 env
->sysenter_cs
= msrs
[i
].data
;
1588 case MSR_IA32_SYSENTER_ESP
:
1589 env
->sysenter_esp
= msrs
[i
].data
;
1591 case MSR_IA32_SYSENTER_EIP
:
1592 env
->sysenter_eip
= msrs
[i
].data
;
1595 env
->pat
= msrs
[i
].data
;
1598 env
->star
= msrs
[i
].data
;
1600 #ifdef TARGET_X86_64
1602 env
->cstar
= msrs
[i
].data
;
1604 case MSR_KERNELGSBASE
:
1605 env
->kernelgsbase
= msrs
[i
].data
;
1608 env
->fmask
= msrs
[i
].data
;
1611 env
->lstar
= msrs
[i
].data
;
1615 env
->tsc
= msrs
[i
].data
;
1617 case MSR_TSC_ADJUST
:
1618 env
->tsc_adjust
= msrs
[i
].data
;
1620 case MSR_IA32_TSCDEADLINE
:
1621 env
->tsc_deadline
= msrs
[i
].data
;
1623 case MSR_VM_HSAVE_PA
:
1624 env
->vm_hsave
= msrs
[i
].data
;
1626 case MSR_KVM_SYSTEM_TIME
:
1627 env
->system_time_msr
= msrs
[i
].data
;
1629 case MSR_KVM_WALL_CLOCK
:
1630 env
->wall_clock_msr
= msrs
[i
].data
;
1632 case MSR_MCG_STATUS
:
1633 env
->mcg_status
= msrs
[i
].data
;
1636 env
->mcg_ctl
= msrs
[i
].data
;
1638 case MSR_IA32_MISC_ENABLE
:
1639 env
->msr_ia32_misc_enable
= msrs
[i
].data
;
1641 case MSR_IA32_FEATURE_CONTROL
:
1642 env
->msr_ia32_feature_control
= msrs
[i
].data
;
1644 case MSR_IA32_BNDCFGS
:
1645 env
->msr_bndcfgs
= msrs
[i
].data
;
1648 if (msrs
[i
].index
>= MSR_MC0_CTL
&&
1649 msrs
[i
].index
< MSR_MC0_CTL
+ (env
->mcg_cap
& 0xff) * 4) {
1650 env
->mce_banks
[msrs
[i
].index
- MSR_MC0_CTL
] = msrs
[i
].data
;
1653 case MSR_KVM_ASYNC_PF_EN
:
1654 env
->async_pf_en_msr
= msrs
[i
].data
;
1656 case MSR_KVM_PV_EOI_EN
:
1657 env
->pv_eoi_en_msr
= msrs
[i
].data
;
1659 case MSR_KVM_STEAL_TIME
:
1660 env
->steal_time_msr
= msrs
[i
].data
;
1662 case MSR_CORE_PERF_FIXED_CTR_CTRL
:
1663 env
->msr_fixed_ctr_ctrl
= msrs
[i
].data
;
1665 case MSR_CORE_PERF_GLOBAL_CTRL
:
1666 env
->msr_global_ctrl
= msrs
[i
].data
;
1668 case MSR_CORE_PERF_GLOBAL_STATUS
:
1669 env
->msr_global_status
= msrs
[i
].data
;
1671 case MSR_CORE_PERF_GLOBAL_OVF_CTRL
:
1672 env
->msr_global_ovf_ctrl
= msrs
[i
].data
;
1674 case MSR_CORE_PERF_FIXED_CTR0
... MSR_CORE_PERF_FIXED_CTR0
+ MAX_FIXED_COUNTERS
- 1:
1675 env
->msr_fixed_counters
[index
- MSR_CORE_PERF_FIXED_CTR0
] = msrs
[i
].data
;
1677 case MSR_P6_PERFCTR0
... MSR_P6_PERFCTR0
+ MAX_GP_COUNTERS
- 1:
1678 env
->msr_gp_counters
[index
- MSR_P6_PERFCTR0
] = msrs
[i
].data
;
1680 case MSR_P6_EVNTSEL0
... MSR_P6_EVNTSEL0
+ MAX_GP_COUNTERS
- 1:
1681 env
->msr_gp_evtsel
[index
- MSR_P6_EVNTSEL0
] = msrs
[i
].data
;
1683 case HV_X64_MSR_HYPERCALL
:
1684 env
->msr_hv_hypercall
= msrs
[i
].data
;
1686 case HV_X64_MSR_GUEST_OS_ID
:
1687 env
->msr_hv_guest_os_id
= msrs
[i
].data
;
1689 case HV_X64_MSR_APIC_ASSIST_PAGE
:
1690 env
->msr_hv_vapic
= msrs
[i
].data
;
1692 case HV_X64_MSR_REFERENCE_TSC
:
1693 env
->msr_hv_tsc
= msrs
[i
].data
;
1701 static int kvm_put_mp_state(X86CPU
*cpu
)
1703 struct kvm_mp_state mp_state
= { .mp_state
= cpu
->env
.mp_state
};
1705 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MP_STATE
, &mp_state
);
1708 static int kvm_get_mp_state(X86CPU
*cpu
)
1710 CPUState
*cs
= CPU(cpu
);
1711 CPUX86State
*env
= &cpu
->env
;
1712 struct kvm_mp_state mp_state
;
1715 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_MP_STATE
, &mp_state
);
1719 env
->mp_state
= mp_state
.mp_state
;
1720 if (kvm_irqchip_in_kernel()) {
1721 cs
->halted
= (mp_state
.mp_state
== KVM_MP_STATE_HALTED
);
1726 static int kvm_get_apic(X86CPU
*cpu
)
1728 DeviceState
*apic
= cpu
->apic_state
;
1729 struct kvm_lapic_state kapic
;
1732 if (apic
&& kvm_irqchip_in_kernel()) {
1733 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_LAPIC
, &kapic
);
1738 kvm_get_apic_state(apic
, &kapic
);
1743 static int kvm_put_apic(X86CPU
*cpu
)
1745 DeviceState
*apic
= cpu
->apic_state
;
1746 struct kvm_lapic_state kapic
;
1748 if (apic
&& kvm_irqchip_in_kernel()) {
1749 kvm_put_apic_state(apic
, &kapic
);
1751 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_LAPIC
, &kapic
);
1756 static int kvm_put_vcpu_events(X86CPU
*cpu
, int level
)
1758 CPUX86State
*env
= &cpu
->env
;
1759 struct kvm_vcpu_events events
;
1761 if (!kvm_has_vcpu_events()) {
1765 events
.exception
.injected
= (env
->exception_injected
>= 0);
1766 events
.exception
.nr
= env
->exception_injected
;
1767 events
.exception
.has_error_code
= env
->has_error_code
;
1768 events
.exception
.error_code
= env
->error_code
;
1769 events
.exception
.pad
= 0;
1771 events
.interrupt
.injected
= (env
->interrupt_injected
>= 0);
1772 events
.interrupt
.nr
= env
->interrupt_injected
;
1773 events
.interrupt
.soft
= env
->soft_interrupt
;
1775 events
.nmi
.injected
= env
->nmi_injected
;
1776 events
.nmi
.pending
= env
->nmi_pending
;
1777 events
.nmi
.masked
= !!(env
->hflags2
& HF2_NMI_MASK
);
1780 events
.sipi_vector
= env
->sipi_vector
;
1783 if (level
>= KVM_PUT_RESET_STATE
) {
1785 KVM_VCPUEVENT_VALID_NMI_PENDING
| KVM_VCPUEVENT_VALID_SIPI_VECTOR
;
1788 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_VCPU_EVENTS
, &events
);
1791 static int kvm_get_vcpu_events(X86CPU
*cpu
)
1793 CPUX86State
*env
= &cpu
->env
;
1794 struct kvm_vcpu_events events
;
1797 if (!kvm_has_vcpu_events()) {
1801 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_VCPU_EVENTS
, &events
);
1805 env
->exception_injected
=
1806 events
.exception
.injected
? events
.exception
.nr
: -1;
1807 env
->has_error_code
= events
.exception
.has_error_code
;
1808 env
->error_code
= events
.exception
.error_code
;
1810 env
->interrupt_injected
=
1811 events
.interrupt
.injected
? events
.interrupt
.nr
: -1;
1812 env
->soft_interrupt
= events
.interrupt
.soft
;
1814 env
->nmi_injected
= events
.nmi
.injected
;
1815 env
->nmi_pending
= events
.nmi
.pending
;
1816 if (events
.nmi
.masked
) {
1817 env
->hflags2
|= HF2_NMI_MASK
;
1819 env
->hflags2
&= ~HF2_NMI_MASK
;
1822 env
->sipi_vector
= events
.sipi_vector
;
1827 static int kvm_guest_debug_workarounds(X86CPU
*cpu
)
1829 CPUState
*cs
= CPU(cpu
);
1830 CPUX86State
*env
= &cpu
->env
;
1832 unsigned long reinject_trap
= 0;
1834 if (!kvm_has_vcpu_events()) {
1835 if (env
->exception_injected
== 1) {
1836 reinject_trap
= KVM_GUESTDBG_INJECT_DB
;
1837 } else if (env
->exception_injected
== 3) {
1838 reinject_trap
= KVM_GUESTDBG_INJECT_BP
;
1840 env
->exception_injected
= -1;
1844 * Kernels before KVM_CAP_X86_ROBUST_SINGLESTEP overwrote flags.TF
1845 * injected via SET_GUEST_DEBUG while updating GP regs. Work around this
1846 * by updating the debug state once again if single-stepping is on.
1847 * Another reason to call kvm_update_guest_debug here is a pending debug
1848 * trap raise by the guest. On kernels without SET_VCPU_EVENTS we have to
1849 * reinject them via SET_GUEST_DEBUG.
1851 if (reinject_trap
||
1852 (!kvm_has_robust_singlestep() && cs
->singlestep_enabled
)) {
1853 ret
= kvm_update_guest_debug(cs
, reinject_trap
);
1858 static int kvm_put_debugregs(X86CPU
*cpu
)
1860 CPUX86State
*env
= &cpu
->env
;
1861 struct kvm_debugregs dbgregs
;
1864 if (!kvm_has_debugregs()) {
1868 for (i
= 0; i
< 4; i
++) {
1869 dbgregs
.db
[i
] = env
->dr
[i
];
1871 dbgregs
.dr6
= env
->dr
[6];
1872 dbgregs
.dr7
= env
->dr
[7];
1875 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_DEBUGREGS
, &dbgregs
);
1878 static int kvm_get_debugregs(X86CPU
*cpu
)
1880 CPUX86State
*env
= &cpu
->env
;
1881 struct kvm_debugregs dbgregs
;
1884 if (!kvm_has_debugregs()) {
1888 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_DEBUGREGS
, &dbgregs
);
1892 for (i
= 0; i
< 4; i
++) {
1893 env
->dr
[i
] = dbgregs
.db
[i
];
1895 env
->dr
[4] = env
->dr
[6] = dbgregs
.dr6
;
1896 env
->dr
[5] = env
->dr
[7] = dbgregs
.dr7
;
1901 int kvm_arch_put_registers(CPUState
*cpu
, int level
)
1903 X86CPU
*x86_cpu
= X86_CPU(cpu
);
1906 assert(cpu_is_stopped(cpu
) || qemu_cpu_is_self(cpu
));
1908 if (level
>= KVM_PUT_RESET_STATE
&& has_msr_feature_control
) {
1909 ret
= kvm_put_msr_feature_control(x86_cpu
);
1915 ret
= kvm_getput_regs(x86_cpu
, 1);
1919 ret
= kvm_put_xsave(x86_cpu
);
1923 ret
= kvm_put_xcrs(x86_cpu
);
1927 ret
= kvm_put_sregs(x86_cpu
);
1931 /* must be before kvm_put_msrs */
1932 ret
= kvm_inject_mce_oldstyle(x86_cpu
);
1936 ret
= kvm_put_msrs(x86_cpu
, level
);
1940 if (level
>= KVM_PUT_RESET_STATE
) {
1941 ret
= kvm_put_mp_state(x86_cpu
);
1945 ret
= kvm_put_apic(x86_cpu
);
1951 ret
= kvm_put_tscdeadline_msr(x86_cpu
);
1956 ret
= kvm_put_vcpu_events(x86_cpu
, level
);
1960 ret
= kvm_put_debugregs(x86_cpu
);
1965 ret
= kvm_guest_debug_workarounds(x86_cpu
);
1972 int kvm_arch_get_registers(CPUState
*cs
)
1974 X86CPU
*cpu
= X86_CPU(cs
);
1977 assert(cpu_is_stopped(cs
) || qemu_cpu_is_self(cs
));
1979 ret
= kvm_getput_regs(cpu
, 0);
1983 ret
= kvm_get_xsave(cpu
);
1987 ret
= kvm_get_xcrs(cpu
);
1991 ret
= kvm_get_sregs(cpu
);
1995 ret
= kvm_get_msrs(cpu
);
1999 ret
= kvm_get_mp_state(cpu
);
2003 ret
= kvm_get_apic(cpu
);
2007 ret
= kvm_get_vcpu_events(cpu
);
2011 ret
= kvm_get_debugregs(cpu
);
2018 void kvm_arch_pre_run(CPUState
*cpu
, struct kvm_run
*run
)
2020 X86CPU
*x86_cpu
= X86_CPU(cpu
);
2021 CPUX86State
*env
= &x86_cpu
->env
;
2025 if (cpu
->interrupt_request
& CPU_INTERRUPT_NMI
) {
2026 cpu
->interrupt_request
&= ~CPU_INTERRUPT_NMI
;
2027 DPRINTF("injected NMI\n");
2028 ret
= kvm_vcpu_ioctl(cpu
, KVM_NMI
);
2030 fprintf(stderr
, "KVM: injection failed, NMI lost (%s)\n",
2035 /* Force the VCPU out of its inner loop to process any INIT requests
2036 * or (for userspace APIC, but it is cheap to combine the checks here)
2037 * pending TPR access reports.
2039 if (cpu
->interrupt_request
& (CPU_INTERRUPT_INIT
| CPU_INTERRUPT_TPR
)) {
2040 cpu
->exit_request
= 1;
2043 if (!kvm_irqchip_in_kernel()) {
2044 /* Try to inject an interrupt if the guest can accept it */
2045 if (run
->ready_for_interrupt_injection
&&
2046 (cpu
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
2047 (env
->eflags
& IF_MASK
)) {
2050 cpu
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
2051 irq
= cpu_get_pic_interrupt(env
);
2053 struct kvm_interrupt intr
;
2056 DPRINTF("injected interrupt %d\n", irq
);
2057 ret
= kvm_vcpu_ioctl(cpu
, KVM_INTERRUPT
, &intr
);
2060 "KVM: injection failed, interrupt lost (%s)\n",
2066 /* If we have an interrupt but the guest is not ready to receive an
2067 * interrupt, request an interrupt window exit. This will
2068 * cause a return to userspace as soon as the guest is ready to
2069 * receive interrupts. */
2070 if ((cpu
->interrupt_request
& CPU_INTERRUPT_HARD
)) {
2071 run
->request_interrupt_window
= 1;
2073 run
->request_interrupt_window
= 0;
2076 DPRINTF("setting tpr\n");
2077 run
->cr8
= cpu_get_apic_tpr(x86_cpu
->apic_state
);
2081 void kvm_arch_post_run(CPUState
*cpu
, struct kvm_run
*run
)
2083 X86CPU
*x86_cpu
= X86_CPU(cpu
);
2084 CPUX86State
*env
= &x86_cpu
->env
;
2087 env
->eflags
|= IF_MASK
;
2089 env
->eflags
&= ~IF_MASK
;
2091 cpu_set_apic_tpr(x86_cpu
->apic_state
, run
->cr8
);
2092 cpu_set_apic_base(x86_cpu
->apic_state
, run
->apic_base
);
2095 int kvm_arch_process_async_events(CPUState
*cs
)
2097 X86CPU
*cpu
= X86_CPU(cs
);
2098 CPUX86State
*env
= &cpu
->env
;
2100 if (cs
->interrupt_request
& CPU_INTERRUPT_MCE
) {
2101 /* We must not raise CPU_INTERRUPT_MCE if it's not supported. */
2102 assert(env
->mcg_cap
);
2104 cs
->interrupt_request
&= ~CPU_INTERRUPT_MCE
;
2106 kvm_cpu_synchronize_state(cs
);
2108 if (env
->exception_injected
== EXCP08_DBLE
) {
2109 /* this means triple fault */
2110 qemu_system_reset_request();
2111 cs
->exit_request
= 1;
2114 env
->exception_injected
= EXCP12_MCHK
;
2115 env
->has_error_code
= 0;
2118 if (kvm_irqchip_in_kernel() && env
->mp_state
== KVM_MP_STATE_HALTED
) {
2119 env
->mp_state
= KVM_MP_STATE_RUNNABLE
;
2123 if (cs
->interrupt_request
& CPU_INTERRUPT_INIT
) {
2124 kvm_cpu_synchronize_state(cs
);
2128 if (kvm_irqchip_in_kernel()) {
2132 if (cs
->interrupt_request
& CPU_INTERRUPT_POLL
) {
2133 cs
->interrupt_request
&= ~CPU_INTERRUPT_POLL
;
2134 apic_poll_irq(cpu
->apic_state
);
2136 if (((cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
2137 (env
->eflags
& IF_MASK
)) ||
2138 (cs
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
2141 if (cs
->interrupt_request
& CPU_INTERRUPT_SIPI
) {
2142 kvm_cpu_synchronize_state(cs
);
2145 if (cs
->interrupt_request
& CPU_INTERRUPT_TPR
) {
2146 cs
->interrupt_request
&= ~CPU_INTERRUPT_TPR
;
2147 kvm_cpu_synchronize_state(cs
);
2148 apic_handle_tpr_access_report(cpu
->apic_state
, env
->eip
,
2149 env
->tpr_access_type
);
2155 static int kvm_handle_halt(X86CPU
*cpu
)
2157 CPUState
*cs
= CPU(cpu
);
2158 CPUX86State
*env
= &cpu
->env
;
2160 if (!((cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
2161 (env
->eflags
& IF_MASK
)) &&
2162 !(cs
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
2170 static int kvm_handle_tpr_access(X86CPU
*cpu
)
2172 CPUState
*cs
= CPU(cpu
);
2173 struct kvm_run
*run
= cs
->kvm_run
;
2175 apic_handle_tpr_access_report(cpu
->apic_state
, run
->tpr_access
.rip
,
2176 run
->tpr_access
.is_write
? TPR_ACCESS_WRITE
2181 int kvm_arch_insert_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
2183 static const uint8_t int3
= 0xcc;
2185 if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 0) ||
2186 cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&int3
, 1, 1)) {
2192 int kvm_arch_remove_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
2196 if (cpu_memory_rw_debug(cs
, bp
->pc
, &int3
, 1, 0) || int3
!= 0xcc ||
2197 cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 1)) {
2209 static int nb_hw_breakpoint
;
2211 static int find_hw_breakpoint(target_ulong addr
, int len
, int type
)
2215 for (n
= 0; n
< nb_hw_breakpoint
; n
++) {
2216 if (hw_breakpoint
[n
].addr
== addr
&& hw_breakpoint
[n
].type
== type
&&
2217 (hw_breakpoint
[n
].len
== len
|| len
== -1)) {
2224 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
2225 target_ulong len
, int type
)
2228 case GDB_BREAKPOINT_HW
:
2231 case GDB_WATCHPOINT_WRITE
:
2232 case GDB_WATCHPOINT_ACCESS
:
2239 if (addr
& (len
- 1)) {
2251 if (nb_hw_breakpoint
== 4) {
2254 if (find_hw_breakpoint(addr
, len
, type
) >= 0) {
2257 hw_breakpoint
[nb_hw_breakpoint
].addr
= addr
;
2258 hw_breakpoint
[nb_hw_breakpoint
].len
= len
;
2259 hw_breakpoint
[nb_hw_breakpoint
].type
= type
;
2265 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
2266 target_ulong len
, int type
)
2270 n
= find_hw_breakpoint(addr
, (type
== GDB_BREAKPOINT_HW
) ? 1 : len
, type
);
2275 hw_breakpoint
[n
] = hw_breakpoint
[nb_hw_breakpoint
];
2280 void kvm_arch_remove_all_hw_breakpoints(void)
2282 nb_hw_breakpoint
= 0;
2285 static CPUWatchpoint hw_watchpoint
;
2287 static int kvm_handle_debug(X86CPU
*cpu
,
2288 struct kvm_debug_exit_arch
*arch_info
)
2290 CPUState
*cs
= CPU(cpu
);
2291 CPUX86State
*env
= &cpu
->env
;
2295 if (arch_info
->exception
== 1) {
2296 if (arch_info
->dr6
& (1 << 14)) {
2297 if (cs
->singlestep_enabled
) {
2301 for (n
= 0; n
< 4; n
++) {
2302 if (arch_info
->dr6
& (1 << n
)) {
2303 switch ((arch_info
->dr7
>> (16 + n
*4)) & 0x3) {
2309 cs
->watchpoint_hit
= &hw_watchpoint
;
2310 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
2311 hw_watchpoint
.flags
= BP_MEM_WRITE
;
2315 cs
->watchpoint_hit
= &hw_watchpoint
;
2316 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
2317 hw_watchpoint
.flags
= BP_MEM_ACCESS
;
2323 } else if (kvm_find_sw_breakpoint(cs
, arch_info
->pc
)) {
2327 cpu_synchronize_state(cs
);
2328 assert(env
->exception_injected
== -1);
2331 env
->exception_injected
= arch_info
->exception
;
2332 env
->has_error_code
= 0;
2338 void kvm_arch_update_guest_debug(CPUState
*cpu
, struct kvm_guest_debug
*dbg
)
2340 const uint8_t type_code
[] = {
2341 [GDB_BREAKPOINT_HW
] = 0x0,
2342 [GDB_WATCHPOINT_WRITE
] = 0x1,
2343 [GDB_WATCHPOINT_ACCESS
] = 0x3
2345 const uint8_t len_code
[] = {
2346 [1] = 0x0, [2] = 0x1, [4] = 0x3, [8] = 0x2
2350 if (kvm_sw_breakpoints_active(cpu
)) {
2351 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_SW_BP
;
2353 if (nb_hw_breakpoint
> 0) {
2354 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
;
2355 dbg
->arch
.debugreg
[7] = 0x0600;
2356 for (n
= 0; n
< nb_hw_breakpoint
; n
++) {
2357 dbg
->arch
.debugreg
[n
] = hw_breakpoint
[n
].addr
;
2358 dbg
->arch
.debugreg
[7] |= (2 << (n
* 2)) |
2359 (type_code
[hw_breakpoint
[n
].type
] << (16 + n
*4)) |
2360 ((uint32_t)len_code
[hw_breakpoint
[n
].len
] << (18 + n
*4));
2365 static bool host_supports_vmx(void)
2367 uint32_t ecx
, unused
;
2369 host_cpuid(1, 0, &unused
, &unused
, &ecx
, &unused
);
2370 return ecx
& CPUID_EXT_VMX
;
2373 #define VMX_INVALID_GUEST_STATE 0x80000021
2375 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
2377 X86CPU
*cpu
= X86_CPU(cs
);
2381 switch (run
->exit_reason
) {
2383 DPRINTF("handle_hlt\n");
2384 ret
= kvm_handle_halt(cpu
);
2386 case KVM_EXIT_SET_TPR
:
2389 case KVM_EXIT_TPR_ACCESS
:
2390 ret
= kvm_handle_tpr_access(cpu
);
2392 case KVM_EXIT_FAIL_ENTRY
:
2393 code
= run
->fail_entry
.hardware_entry_failure_reason
;
2394 fprintf(stderr
, "KVM: entry failed, hardware error 0x%" PRIx64
"\n",
2396 if (host_supports_vmx() && code
== VMX_INVALID_GUEST_STATE
) {
2398 "\nIf you're running a guest on an Intel machine without "
2399 "unrestricted mode\n"
2400 "support, the failure can be most likely due to the guest "
2401 "entering an invalid\n"
2402 "state for Intel VT. For example, the guest maybe running "
2403 "in big real mode\n"
2404 "which is not supported on less recent Intel processors."
2409 case KVM_EXIT_EXCEPTION
:
2410 fprintf(stderr
, "KVM: exception %d exit (error code 0x%x)\n",
2411 run
->ex
.exception
, run
->ex
.error_code
);
2414 case KVM_EXIT_DEBUG
:
2415 DPRINTF("kvm_exit_debug\n");
2416 ret
= kvm_handle_debug(cpu
, &run
->debug
.arch
);
2419 fprintf(stderr
, "KVM: unknown exit reason %d\n", run
->exit_reason
);
2427 bool kvm_arch_stop_on_emulation_error(CPUState
*cs
)
2429 X86CPU
*cpu
= X86_CPU(cs
);
2430 CPUX86State
*env
= &cpu
->env
;
2432 kvm_cpu_synchronize_state(cs
);
2433 return !(env
->cr
[0] & CR0_PE_MASK
) ||
2434 ((env
->segs
[R_CS
].selector
& 3) != 3);
2437 void kvm_arch_init_irq_routing(KVMState
*s
)
2439 if (!kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
)) {
2440 /* If kernel can't do irq routing, interrupt source
2441 * override 0->2 cannot be set up as required by HPET.
2442 * So we have to disable it.
2446 /* We know at this point that we're using the in-kernel
2447 * irqchip, so we can use irqfds, and on x86 we know
2448 * we can use msi via irqfd and GSI routing.
2450 kvm_irqfds_allowed
= true;
2451 kvm_msi_via_irqfd_allowed
= true;
2452 kvm_gsi_routing_allowed
= true;
2455 /* Classic KVM device assignment interface. Will remain x86 only. */
2456 int kvm_device_pci_assign(KVMState
*s
, PCIHostDeviceAddress
*dev_addr
,
2457 uint32_t flags
, uint32_t *dev_id
)
2459 struct kvm_assigned_pci_dev dev_data
= {
2460 .segnr
= dev_addr
->domain
,
2461 .busnr
= dev_addr
->bus
,
2462 .devfn
= PCI_DEVFN(dev_addr
->slot
, dev_addr
->function
),
2467 dev_data
.assigned_dev_id
=
2468 (dev_addr
->domain
<< 16) | (dev_addr
->bus
<< 8) | dev_data
.devfn
;
2470 ret
= kvm_vm_ioctl(s
, KVM_ASSIGN_PCI_DEVICE
, &dev_data
);
2475 *dev_id
= dev_data
.assigned_dev_id
;
2480 int kvm_device_pci_deassign(KVMState
*s
, uint32_t dev_id
)
2482 struct kvm_assigned_pci_dev dev_data
= {
2483 .assigned_dev_id
= dev_id
,
2486 return kvm_vm_ioctl(s
, KVM_DEASSIGN_PCI_DEVICE
, &dev_data
);
2489 static int kvm_assign_irq_internal(KVMState
*s
, uint32_t dev_id
,
2490 uint32_t irq_type
, uint32_t guest_irq
)
2492 struct kvm_assigned_irq assigned_irq
= {
2493 .assigned_dev_id
= dev_id
,
2494 .guest_irq
= guest_irq
,
2498 if (kvm_check_extension(s
, KVM_CAP_ASSIGN_DEV_IRQ
)) {
2499 return kvm_vm_ioctl(s
, KVM_ASSIGN_DEV_IRQ
, &assigned_irq
);
2501 return kvm_vm_ioctl(s
, KVM_ASSIGN_IRQ
, &assigned_irq
);
2505 int kvm_device_intx_assign(KVMState
*s
, uint32_t dev_id
, bool use_host_msi
,
2508 uint32_t irq_type
= KVM_DEV_IRQ_GUEST_INTX
|
2509 (use_host_msi
? KVM_DEV_IRQ_HOST_MSI
: KVM_DEV_IRQ_HOST_INTX
);
2511 return kvm_assign_irq_internal(s
, dev_id
, irq_type
, guest_irq
);
2514 int kvm_device_intx_set_mask(KVMState
*s
, uint32_t dev_id
, bool masked
)
2516 struct kvm_assigned_pci_dev dev_data
= {
2517 .assigned_dev_id
= dev_id
,
2518 .flags
= masked
? KVM_DEV_ASSIGN_MASK_INTX
: 0,
2521 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_INTX_MASK
, &dev_data
);
2524 static int kvm_deassign_irq_internal(KVMState
*s
, uint32_t dev_id
,
2527 struct kvm_assigned_irq assigned_irq
= {
2528 .assigned_dev_id
= dev_id
,
2532 return kvm_vm_ioctl(s
, KVM_DEASSIGN_DEV_IRQ
, &assigned_irq
);
2535 int kvm_device_intx_deassign(KVMState
*s
, uint32_t dev_id
, bool use_host_msi
)
2537 return kvm_deassign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_GUEST_INTX
|
2538 (use_host_msi
? KVM_DEV_IRQ_HOST_MSI
: KVM_DEV_IRQ_HOST_INTX
));
2541 int kvm_device_msi_assign(KVMState
*s
, uint32_t dev_id
, int virq
)
2543 return kvm_assign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_HOST_MSI
|
2544 KVM_DEV_IRQ_GUEST_MSI
, virq
);
2547 int kvm_device_msi_deassign(KVMState
*s
, uint32_t dev_id
)
2549 return kvm_deassign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_GUEST_MSI
|
2550 KVM_DEV_IRQ_HOST_MSI
);
2553 bool kvm_device_msix_supported(KVMState
*s
)
2555 /* The kernel lacks a corresponding KVM_CAP, so we probe by calling
2556 * KVM_ASSIGN_SET_MSIX_NR with an invalid parameter. */
2557 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_NR
, NULL
) == -EFAULT
;
2560 int kvm_device_msix_init_vectors(KVMState
*s
, uint32_t dev_id
,
2561 uint32_t nr_vectors
)
2563 struct kvm_assigned_msix_nr msix_nr
= {
2564 .assigned_dev_id
= dev_id
,
2565 .entry_nr
= nr_vectors
,
2568 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_NR
, &msix_nr
);
2571 int kvm_device_msix_set_vector(KVMState
*s
, uint32_t dev_id
, uint32_t vector
,
2574 struct kvm_assigned_msix_entry msix_entry
= {
2575 .assigned_dev_id
= dev_id
,
2580 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_ENTRY
, &msix_entry
);
2583 int kvm_device_msix_assign(KVMState
*s
, uint32_t dev_id
)
2585 return kvm_assign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_HOST_MSIX
|
2586 KVM_DEV_IRQ_GUEST_MSIX
, 0);
2589 int kvm_device_msix_deassign(KVMState
*s
, uint32_t dev_id
)
2591 return kvm_deassign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_GUEST_MSIX
|
2592 KVM_DEV_IRQ_HOST_MSIX
);