KVM: qemu patches for few KVM features I developed
v2 cover KVM: qemu patches for few KVM features I developed
These patches implement the qemu side logic to support the KVM features I developed recently.
All 3 patches are for features that are accepted upstream in KVM.
V2: rebased and fixed patch 2 to compile without kvm
Best regards,
Maxim Levitsky
Maxim Levitsky (3):
KVM: use KVM_{GET|SET}_SREGS2 when supported.
gdbstub: implement NOIRQ support for single step on KVM
KVM: SVM: add migration support for nested TSC scaling
accel/kvm/kvm-all.c | 30 +++++++++++ gdbstub.c | 62 +++++++++++++++++---- include/sysemu/kvm.h | 19 +++++++ target/i386/cpu.c | 5 ++ target/i386/cpu.h | 7 +++ target/i386/kvm/kvm.c | 122 +++++++++++++++++++++++++++++++++++++++++- target/i386/machine.c | 52 ++++++++++++++++++ 7 files changed, 284 insertions(+), 13 deletions(-)
Signature · 1 line
2.26.3
PATCH v2 1/3 KVM: use KVM_{GET|SET}_SREGS2 when supported.
This allows to make PDPTRs part of the migration stream and thus not reload them after migration which is against X86 spec.
--- accel/kvm/kvm-all.c | 5 ++ include/sysemu/kvm.h | 4 ++ target/i386/cpu.h | 3 ++ target/i386/kvm/kvm.c | 107 +++++++++++++++++++++++++++++++++++++++++- target/i386/machine.c | 29 ++++++++++++ 5 files changed, 146 insertions(+), 2 deletions(-)
accel/kvm/kvm-all.c +5 -0
@@ -163,6 +163,7 @@ bool kvm_msi_via_irqfd_allowed;bool kvm_gsi_routing_allowed;bool kvm_gsi_direct_mapping;bool kvm_allowed; +bool kvm_sregs2;bool kvm_readonly_mem_allowed;bool kvm_vm_attributes_allowed;bool kvm_direct_msi_allowed;
@@ -2557,6 +2558,10 @@ static int kvm_init(MachineState *ms) kvm_ioeventfd_any_length_allowed = (kvm_check_extension (s,KVM_CAP_IOEVENTFD_ANY_LENGTH ) >0 ); + + kvm_sregs2 = + (kvm_check_extension (s,KVM_CAP_SREGS2 ) >0 ); + kvm_state = s; ret =kvm_arch_init (ms, s);
include/sysemu/kvm.h +4 -0
@@ -32,6 +32,7 @@#ifdef CONFIG_KVM_IS_POSSIBLE extern bool kvm_allowed; +extern bool kvm_sregs2;extern bool kvm_kernel_irqchip;extern bool kvm_split_irqchip;extern bool kvm_async_interrupts_allowed;
@@ -139,6 +140,9 @@ extern bool kvm_msi_use_devid;*/ #define kvm_gsi_direct_mapping () (kvm_gsi_direct_mapping) + +#define kvm_supports_sregs2 () (kvm_sregs2) +/** * kvm_readonly_mem_enabled: *
target/i386/cpu.h +3 -0
@@ -1452,6 +1452,9 @@ typedef struct CPUX86State {SegmentCache idt;/* only base and limit are used */ target_ulong cr[5 ];/* NOTE: cr1 is unused */ + +bool pdptrs_valid; +uint64_t pdptrs[4 ];int32_t a20_mask;BNDReg bnd_regs[4 ];
target/i386/kvm/kvm.c +105 -2
131 lines not on this page · raw message
target/i386/machine.c +29 -0
@@ -1430,6 +1430,34 @@ static const VMStateDescription vmstate_msr_intel_sgx = { .needed = intel_sgx_msrs_needed, .fields = (VMStateField []) {VMSTATE_UINT64_ARRAY (env.msr_ia32_sgxlepubkeyhash,X86CPU ,4 ), +VMSTATE_END_OF_LIST () + } + }; + +static bool pdptrs_needed (void *opaque) +{ +X86CPU *cpu = opaque; +CPUX86State *env = &cpu->env; +return env->pdptrs_valid; +} + +static int pdptrs_post_load (void *opaque,int version_id) +{ +X86CPU *cpu = opaque; +CPUX86State *env = &cpu->env; + env->pdptrs_valid = true; +return 0 ; +} + + +static const VMStateDescription vmstate_pdptrs = { + .name ="cpu/pdptrs" , + .version_id =1 , + .minimum_version_id =1 , + .needed = pdptrs_needed, + .post_load = pdptrs_post_load, + .fields = (VMStateField []) { +VMSTATE_UINT64_ARRAY (env.pdptrs,X86CPU ,4 ),VMSTATE_END_OF_LIST () } };
@@ -1571,6 +1599,7 @@ const VMStateDescription vmstate_x86_cpu = {#endif &vmstate_msr_tsx_ctrl, &vmstate_msr_intel_sgx, + &vmstate_pdptrs, NULL } };
Signature · 1 line
2.26.3
PATCH v2 2/3 gdbstub: implement NOIRQ support for single step on KVM
In reply to Maxim Levitsky--- accel/kvm/kvm-all.c | 25 ++++++++++++++++++ gdbstub.c | 62 ++++++++++++++++++++++++++++++++++++-------- include/sysemu/kvm.h | 15 +++++++++++ 3 files changed, 91 insertions(+), 11 deletions(-)
accel/kvm/kvm-all.c +25 -0
@@ -169,6 +169,8 @@ bool kvm_vm_attributes_allowed;bool kvm_direct_msi_allowed;bool kvm_ioeventfd_any_length_allowed;bool kvm_msi_use_devid; +bool kvm_has_guest_debug; +int kvm_sstep_flags;static bool kvm_immediate_exit;static hwaddr kvm_max_slot_size = ~0 ;
@@ -2562,6 +2564,25 @@ static int kvm_init(MachineState *ms) kvm_sregs2 = (kvm_check_extension (s,KVM_CAP_SREGS2 ) >0 ); + kvm_has_guest_debug = + (kvm_check_extension (s,KVM_CAP_SET_GUEST_DEBUG ) >0 ); + + kvm_sstep_flags =0 ; + +if (kvm_has_guest_debug) { +/* Assume that single stepping is supported */ + kvm_sstep_flags =SSTEP_ENABLE ; + +int guest_debug_flags = +kvm_check_extension (s,KVM_CAP_SET_GUEST_DEBUG2 ); + +if (guest_debug_flags >0 ) { +if (guest_debug_flags &KVM_GUESTDBG_BLOCKIRQ ) { + kvm_sstep_flags |=SSTEP_NOIRQ ; + } + } + } + kvm_state = s; ret =kvm_arch_init (ms, s);
@@ -3191,6 +3212,10 @@ int kvm_update_guest_debug(CPUState *cpu, unsigned long reinject_trap)if (cpu->singlestep_enabled) { data.dbg.control |=KVM_GUESTDBG_ENABLE |KVM_GUESTDBG_SINGLESTEP ; + +if (cpu->singlestep_enabled &SSTEP_NOIRQ ) { + data.dbg.control |=KVM_GUESTDBG_BLOCKIRQ ; + } }kvm_arch_update_guest_debug (cpu, &data.dbg);
gdbstub.c +51 -11
117 lines not on this page · raw message
include/sysemu/kvm.h +15 -0
@@ -47,6 +47,8 @@ extern bool kvm_readonly_mem_allowed;extern bool kvm_direct_msi_allowed;extern bool kvm_ioeventfd_any_length_allowed;extern bool kvm_msi_use_devid; +extern bool kvm_has_guest_debug; +extern int kvm_sstep_flags;#define kvm_enabled () (kvm_allowed)/**
@@ -171,6 +173,17 @@ extern bool kvm_msi_use_devid;*/ #define kvm_msi_devid_required () (kvm_msi_use_devid) +/* +* Does KVM support guest debugging +*/ +#define kvm_supports_guest_debug () (kvm_has_guest_debug) + +/* +* kvm_supported_sstep_flags +* Returns: SSTEP_* flags that KVM supports for guest debug +*/ +#define kvm_get_supported_sstep_flags () (kvm_sstep_flags) +#else #define kvm_enabled () (0)
@@ -188,6 +201,8 @@ extern bool kvm_msi_use_devid;#define kvm_direct_msi_enabled () (false)#define kvm_ioeventfd_any_length_enabled () (false)#define kvm_msi_devid_required () (false) +#define kvm_supports_guest_debug () (false) +#define kvm_get_supported_sstep_flags () (0)#endif /* CONFIG_KVM_IS_POSSIBLE */
Signature · 1 line
2.26.3
PATCH v2 3/3 KVM: SVM: add migration support for nested TSC scaling
In reply to Maxim Levitsky--- target/i386/cpu.c | 5 +++++ target/i386/cpu.h | 4 ++++ target/i386/kvm/kvm.c | 15 +++++++++++++++ target/i386/machine.c | 23 +++++++++++++++++++++++ 4 files changed, 47 insertions(+)
target/i386/cpu.c +5 -0
@@ -5928,6 +5928,11 @@ static void x86_cpu_reset(DeviceState *dev) }x86_cpu_set_sgxlepubkeyhash (env); + +if (env->features[FEAT_SVM ] &CPUID_SVM_TSCSCALE ) { + env->amd_tsc_scale_msr =MSR_AMD64_TSC_RATIO_DEFAULT ; + } +#endif }
target/i386/cpu.h +4 -0
@@ -499,6 +499,9 @@ typedef enum X86Seg {#define MSR_GSBASE 0xc0000101#define MSR_KERNELGSBASE 0xc0000102#define MSR_TSC_AUX 0xc0000103 +#define MSR_AMD64_TSC_RATIO 0xc0000104 + +#define MSR_AMD64_TSC_RATIO_DEFAULT 0x100000000ULL#define MSR_VM_HSAVE_PA 0xc0010117
@@ -1539,6 +1542,7 @@ typedef struct CPUX86State {uint32_t tsx_ctrl;uint64_t spec_ctrl; +uint64_t amd_tsc_scale_msr;uint64_t virt_ssbd;/* End of state preserved by INIT (dummy marker). */
target/i386/kvm/kvm.c +15 -0
@@ -105,6 +105,7 @@ static bool has_msr_hv_reenlightenment;static bool has_msr_xss;static bool has_msr_umwait;static bool has_msr_spec_ctrl; +static bool has_tsc_scale_msr;static bool has_msr_tsx_ctrl;static bool has_msr_virt_ssbd;static bool has_msr_smi_count;
@@ -2216,6 +2217,9 @@ static int kvm_get_supported_msrs(KVMState *s)case MSR_IA32_SPEC_CTRL : has_msr_spec_ctrl = true;break ; +case MSR_AMD64_TSC_RATIO : + has_tsc_scale_msr = true; +break ;case MSR_IA32_TSX_CTRL : has_msr_tsx_ctrl = true;break ;
@@ -3027,6 +3031,10 @@ static int kvm_put_msrs(X86CPU *cpu, int level)if (has_msr_spec_ctrl) {kvm_msr_entry_add (cpu,MSR_IA32_SPEC_CTRL , env->spec_ctrl); } +if (has_tsc_scale_msr) { +kvm_msr_entry_add (cpu,MSR_AMD64_TSC_RATIO , env->amd_tsc_scale_msr); + } +if (has_msr_tsx_ctrl) {kvm_msr_entry_add (cpu,MSR_IA32_TSX_CTRL , env->tsx_ctrl); }
@@ -3479,6 +3487,10 @@ static int kvm_get_msrs(X86CPU *cpu)if (has_msr_spec_ctrl) {kvm_msr_entry_add (cpu,MSR_IA32_SPEC_CTRL ,0 ); } +if (has_tsc_scale_msr) { +kvm_msr_entry_add (cpu,MSR_AMD64_TSC_RATIO ,0 ); + } +if (has_msr_tsx_ctrl) {kvm_msr_entry_add (cpu,MSR_IA32_TSX_CTRL ,0 ); }
@@ -3890,6 +3902,9 @@ static int kvm_get_msrs(X86CPU *cpu)case MSR_IA32_SPEC_CTRL : env->spec_ctrl = msrs[i].data;break ; +case MSR_AMD64_TSC_RATIO : + env->amd_tsc_scale_msr = msrs[i].data; +break ;case MSR_IA32_TSX_CTRL : env->tsx_ctrl = msrs[i].data;break ;
target/i386/machine.c +23 -0
@@ -1280,6 +1280,28 @@ static const VMStateDescription vmstate_spec_ctrl = { } }; + +static bool amd_tsc_scale_msr_needed (void *opaque) +{ +X86CPU *cpu = opaque; +CPUX86State *env = &cpu->env; + +return env->amd_tsc_scale_msr && + env->amd_tsc_scale_msr !=MSR_AMD64_TSC_RATIO_DEFAULT ; +} + +static const VMStateDescription amd_tsc_scale_msr_ctrl = { + .name ="cpu/amd_tsc_scale_msr" , + .version_id =1 , + .minimum_version_id =1 , + .needed = amd_tsc_scale_msr_needed, + .fields = (VMStateField []){ +VMSTATE_UINT64 (env.amd_tsc_scale_msr,X86CPU ), +VMSTATE_END_OF_LIST () + } +}; + +static bool intel_pt_enable_needed (void *opaque) {X86CPU *cpu = opaque;
@@ -1586,6 +1608,7 @@ const VMStateDescription vmstate_x86_cpu = { &vmstate_pkru, &vmstate_pkrs, &vmstate_spec_ctrl, + &amd_tsc_scale_msr_ctrl, &vmstate_mcg_ext_ctl, &vmstate_msr_intel_pt, &vmstate_msr_virt_ssbd,
Signature · 1 line
2.26.3
Re: [PATCH v2 3/3] KVM: SVM: add migration support for nested TSC scaling
Quoted 7 lines · Maxim Levitsky
Signed-off-by Maxim Levitsky <[email protected]>--- target/i386/cpu.c | 5 +++++ target/i386/cpu.h | 4 ++++ target/i386/kvm/kvm.c | 15 +++++++++++++++ target/i386/machine.c | 23 +++++++++++++++++++++++ 4 files changed, 47 insertions(+)
It's easier to migrate it unconditionally:
target/i386/machine.c +1 -2
@@ -1286,8 +1286,7 @@ static bool amd_tsc_scale_msr_needed(void *opaque)X86CPU *cpu = opaque;CPUX86State *env = &cpu->env; -return env->amd_tsc_scale_msr && - env->amd_tsc_scale_msr !=MSR_AMD64_TSC_RATIO_DEFAULT ; +return (env->features[FEAT_SVM ] &CPUID_SVM_TSCSCALE ); }static const VMStateDescription amd_tsc_scale_msr_ctrl = {
Review
Quoted 3 lines
(quoted diff) +3
-0+ if (env->features[FEAT_SVM] & CPUID_SVM_TSCSCALE) { + env->amd_tsc_scale_msr = MSR_AMD64_TSC_RATIO_DEFAULT; + }
and also set it unconditionally here, so that it's always passed correctly to KVM.
I queued patches 2 and 3, for (1) I need to think more about migration to older QEMU versions.
Paolo