kvm userspace: Add MCE simulation to kvm
PATCH 2/2 kvm userspace: Add MCE simulation to kvm
- MCE features are initialized when VCPU is initialized according to CPUID.
- A monitor command "mce" is added to inject a MCE.
ChangeLog:
v2:
- Use new kernel MCE capability exportion interface.
--- libkvm/libkvm-x86.c | 33 +++++++++++++++++++++++++++++++++ libkvm/libkvm.h | 4 ++++ qemu/monitor.c | 26 ++++++++++++++++++++++++++ qemu/qemu-kvm-x86.c | 33 +++++++++++++++++++++++++++++++++ qemu/qemu-kvm.c | 26 ++++++++++++++++++++++++++ qemu/qemu-kvm.h | 4 ++++ qemu/target-i386/cpu.h | 3 +++ 7 files changed, 129 insertions(+)
qemu/monitor.c +26 -0
@@ -1557,6 +1557,31 @@ static void do_info_status(Monitor *mon) } +#if defined(TARGET_I386 ) || defined(TARGET_X86_64 ) +static void do_inject_mce (Monitor *mon, +int cpu_index,int bank, +unsigned status_hi,unsigned status_lo, +unsigned mcg_status_hi,unsigned mcg_status_lo, +unsigned addr_hi,unsigned addr_lo, +unsigned misc_hi,unsigned misc_lo) +{ +CPUState *env; +struct kvm_x86_mce mce = { + .bank = bank, + .status = ((uint64_t )status_hi <<32 ) | status_lo, + .mcg_status = ((uint64_t )mcg_status_hi <<32 ) | mcg_status_lo, + .addr = ((uint64_t )addr_hi <<32 ) | addr_lo, + .misc = ((uint64_t )misc_hi <<32 ) | misc_lo, + }; + +for (env = first_cpu; env !=NULL ; env = env->next_cpu) +if (env->cpu_index == cpu_index && env->mcg_cap) { +kvm_inject_x86_mce (env, &mce); +break ; + } +} +#endif +static void do_balloon (Monitor *mon,int value) {ram_addr_t target = value;
@@ -1758,6 +1783,7 @@ static const mon_cmd_t mon_cmds[] = { "[tap,user,socket,vde] options", "add host VLAN client" }, { "host_net_remove", "is", net_host_device_remove, "vlan_id name", "remove host VLAN client" }, + { "mce", "iillll", do_inject_mce, "cpu bank status mcgstatus addr misc", "inject a MCE on the given CPU"}, #endif { "balloon", "i", do_balloon, "target", "request VM to change it's memory allocation (in MB)" },
libkvm/libkvm-x86.c +33 -0
@@ -379,6 +379,39 @@ int kvm_set_msrs(kvm_context_t kvm, intreturn r; } +int kvm_get_mce_cap_supported (kvm_context_t kvm,uint64_t *mce_cap, +int *max_banks) +{ +int r; + + r =ioctl (kvm->fd,KVM_CHECK_EXTENSION ,KVM_CAP_MCE ); +if (r >0 ) { + *max_banks = r; +return ioctl (kvm->fd,KVM_X86_GET_MCE_CAP_SUPPORTED , mce_cap); + } +return -ENOSYS ; +} + +int kvm_setup_mce (kvm_context_t kvm,int vcpu,uint64_t *mcg_cap) +{ +int r; + + r =ioctl (kvm->fd,KVM_CHECK_EXTENSION ,KVM_CAP_MCE ); +if (r >0 ) +return ioctl (kvm->vcpu_fd[vcpu],KVM_X86_SETUP_MCE , mcg_cap); +return -ENOSYS ; +} + +int kvm_set_mce (kvm_context_t kvm,int vcpu,struct kvm_x86_mce *m) +{ +int r; + + r =ioctl (kvm->fd,KVM_CHECK_EXTENSION ,KVM_CAP_MCE ); +if (r >0 ) +return ioctl (kvm->vcpu_fd[vcpu],KVM_X86_SET_MCE , m); +return -ENOSYS ; +} +static void print_seg (FILE *file,const char *name,struct kvm_segment *seg) { fprintf(stderr,
libkvm/libkvm.h +4 -0
@@ -27,6 +27,10 @@ typedef struct kvm_context *kvm_context_struct kvm_msr_list *kvm_get_msr_list (kvm_context_t );int kvm_get_msrs (kvm_context_t ,int vcpu,struct kvm_msr_entry *msrs,int n);int kvm_set_msrs (kvm_context_t ,int vcpu,struct kvm_msr_entry *msrs,int n); +int kvm_get_mce_cap_supported (kvm_context_t ,uint64_t *mce_cap,int *max_banks); +int kvm_setup_mce (kvm_context_t ,int vcpu,uint64_t *mcg_cap); +struct kvm_x86_mce ; +int kvm_set_mce (kvm_context_t ,int vcpu,struct kvm_x86_mce *mce);#endif /*!
qemu/qemu-kvm-x86.c +33 -0
@@ -457,6 +457,15 @@ void kvm_arch_save_regs(CPUState *env) } } +void kvm_arch_inject_x86_mce (CPUState *env,struct kvm_x86_mce *mce) +{ +int rc; + + rc =kvm_set_mce (kvm_context, env->cpu_index, mce); +if (rc <0 ) +perror ("kvm_set_mce FAILED" ); +} +static void do_cpuid_ent (struct kvm_cpuid_entry2 *e,uint32_t function,uint32_t count,CPUState *env) {
@@ -566,6 +575,30 @@ int kvm_arch_qemu_init_env(CPUState *cendo_cpuid_ent (&cpuid_ent[cpuid_nent++], i,0 , ©);kvm_setup_cpuid2 (kvm_context, cenv->cpu_index, cpuid_nent, cpuid_ent); + +if (((cenv->cpuid_version >>8 )&0xF ) >=6 + && (cenv->cpuid_features&(CPUID_MCE |CPUID_MCA )) == (CPUID_MCE |CPUID_MCA ) + &&kvm_check_extension (kvm_context,KVM_CAP_MCE ) >0 ) { +uint64_t mcg_cap; +int banks; + +#define MCE_CAP_DEF 0x100 +#define MCE_BANKS_DEF 4 + +if (kvm_get_mce_cap_supported (kvm_context, &mcg_cap, &banks)) +perror ("kvm_get_mce_cap_supported FAILED" ); +else { +if (banks >MCE_BANKS_DEF ) + banks =MCE_BANKS_DEF ; + mcg_cap &=MCE_CAP_DEF ; + mcg_cap |= banks; +if (kvm_setup_mce (kvm_context, cenv->cpu_index, &mcg_cap)) +perror ("kvm_setup_mce FAILED" ); +else + cenv->mcg_cap = mcg_cap; + } + } +return 0 ; }
qemu/qemu-kvm.h +4 -0
@@ -74,6 +74,8 @@ int kvm_arch_try_push_interrupts(void *ovoid kvm_arch_push_nmi (void *opaque);void kvm_arch_update_regs_for_sipi (CPUState *env);void kvm_arch_cpu_reset (CPUState *env); +struct kvm_x86_mce ; +void kvm_arch_inject_x86_mce (CPUState *env,struct kvm_x86_mce *mce);struct kvm_guest_debug ;struct kvm_debug_exit_arch ;
@@ -233,4 +235,6 @@ static inline void cpu_synchronize_state } } +void kvm_inject_x86_mce (CPUState *env,struct kvm_x86_mce *mce); +#endif
qemu/qemu-kvm.c +26 -0
@@ -198,6 +198,32 @@ void kvm_save_registers(CPUState *env)on_vcpu (env, kvm_do_save_registers, env); } +#if defined(TARGET_I386 ) || defined(TARGET_X86_64 ) +struct kvm_x86_mce_data +{ +CPUState *env; +struct kvm_x86_mce *mce; +}; + +static void kvm_do_inject_x86_mce (void *_data) +{ +struct kvm_x86_mce_data *data = _data; + +kvm_arch_inject_x86_mce (data->env, data->mce); +} + +void kvm_inject_x86_mce (CPUState *env,struct kvm_x86_mce *mce) +{ +struct kvm_x86_mce_data data; + +if (kvm_enabled ()) { + data.env = env; + data.mce = mce; +on_vcpu (env, kvm_do_inject_x86_mce, &data); + } +} +#endif +int kvm_cpu_exec (CPUState *env) {int r;
qemu/target-i386/cpu.h +3 -0
@@ -661,6 +661,9 @@ typedef struct CPUX86State {uint64_t mask; } mtrr_var[8 ]; +/* MCE capability */ +uint64_t mcg_cap; +#ifdef USE_KQEMU int kqemu_enabled;int last_io_time;
Hi Huang,
Quoted 43 lines · Huang Ying
- MCE features are initialized when VCPU is initialized according to CPUID.
- A monitor command "mce" is added to inject a MCE.ChangeLog:
v2:
- Use new kernel MCE capability exportion interface.
Signed-off-by Huang Ying <[email protected]>--- libkvm/libkvm-x86.c | 33 +++++++++++++++++++++++++++++++++ libkvm/libkvm.h | 4 ++++ qemu/monitor.c | 26 ++++++++++++++++++++++++++ qemu/qemu-kvm-x86.c | 33 +++++++++++++++++++++++++++++++++ qemu/qemu-kvm.c | 26 ++++++++++++++++++++++++++ qemu/qemu-kvm.h | 4 ++++ qemu/target-i386/cpu.h | 3 +++ 7 files changed, 129 insertions(+)qemu/monitor.c +14
-0@@ -1557,6 +1557,31 @@ static void do_info_status(Monitor *mon) } +#if defined(TARGET_I386) || defined(TARGET_X86_64) +static void do_inject_mce(Monitor *mon, + int cpu_index, int bank, + unsigned status_hi, unsigned status_lo, + unsigned mcg_status_hi, unsigned mcg_status_lo, + unsigned addr_hi, unsigned addr_lo, + unsigned misc_hi, unsigned misc_lo) +{ + CPUState *env; + struct kvm_x86_mce mce = { + .bank = bank, + .status = ((uint64_t)status_hi << 32) | status_lo, + .mcg_status = ((uint64_t)mcg_status_hi << 32) | mcg_status_lo, + .addr = ((uint64_t)addr_hi << 32) | addr_lo,
qemu/monitor.c static void do_info_status(Monitor *mon) · line 1583
Quoted 10 lines
qemu/monitor.c +10
-0+ .misc = ((uint64_t)misc_hi << 32) | misc_lo, + }; + +for (env = first_cpu; env !=NULL ; env = env->next_cpu) +if (env->cpu_index == cpu_index && env->mcg_cap) { +kvm_inject_x86_mce (env, &mce); +break ; + } +} +#endif
This will break compilation on hosts with older kernel headers. To ease the merge with QEMU, do_inject_mce should call qemu_inject_x86_mce, which does nothing for the case where KVM is disabled, or call KVM specific function otherwise (knowledge about struct kvm_x86_mce should be contained within KVM code).
Quoted 41 lines
qemu/monitor.c +2
-0+static void do_balloon (Monitor *mon,int value) {ram_addr_t target = value;@@ -1758,6 +1783,7 @@ static const mon_cmd_t mon_cmds[] = { "[tap,user,socket,vde] options", "add host VLAN client" }, { "host_net_remove", "is", net_host_device_remove, "vlan_id name", "remove host VLAN client" }, + { "mce", "iillll", do_inject_mce, "cpu bank status mcgstatus addr misc", "inject a MCE on the given CPU"}, #endif { "balloon", "i", do_balloon, "target", "request VM to change it's memory allocation (in MB)" },libkvm/libkvm-x86.c +22
-0@@ -379,6 +379,39 @@ int kvm_set_msrs(kvm_context_t kvm, intreturn r; } +int kvm_get_mce_cap_supported (kvm_context_t kvm,uint64_t *mce_cap, +int *max_banks) +{ +int r; + + r =ioctl (kvm->fd,KVM_CHECK_EXTENSION ,KVM_CAP_MCE ); +if (r >0 ) { + *max_banks = r; +return ioctl (kvm->fd,KVM_X86_GET_MCE_CAP_SUPPORTED , mce_cap); + } +return -ENOSYS ; +} + +int kvm_setup_mce (kvm_context_t kvm,int vcpu,uint64_t *mcg_cap) +{ +int r; + + r =ioctl (kvm->fd,KVM_CHECK_EXTENSION ,KVM_CAP_MCE ); +if (r >0 ) +return ioctl (kvm->vcpu_fd[vcpu],KVM_X86_SETUP_MCE , mcg_cap); +return -ENOSYS ; +}
libkvm/libkvm-x86.c int kvm_set_msrs(kvm_context_t kvm, int · line 414
Quoted 10 lines
libkvm/libkvm-x86.c +10
-0+ +int kvm_set_mce (kvm_context_t kvm,int vcpu,struct kvm_x86_mce *m) +{ +int r; + + r =ioctl (kvm->fd,KVM_CHECK_EXTENSION ,KVM_CAP_MCE ); +if (r >0 ) +return ioctl (kvm->vcpu_fd[vcpu],KVM_X86_SET_MCE , m); +return -ENOSYS ; +}
Have to #ifdef KVM_CAP_MCE where appropriate, otherwise compilation on hosts with older kernel headers fail.
Quoted 54 lines · Marcelo Tosatti
Hi Huang,
Quoted 51 lines · Huang Ying
- MCE features are initialized when VCPU is initialized according to CPUID.
- A monitor command "mce" is added to inject a MCE.ChangeLog:
v2:
- Use new kernel MCE capability exportion interface.
Signed-off-by Huang Ying <[email protected]>--- libkvm/libkvm-x86.c | 33 +++++++++++++++++++++++++++++++++ libkvm/libkvm.h | 4 ++++ qemu/monitor.c | 26 ++++++++++++++++++++++++++ qemu/qemu-kvm-x86.c | 33 +++++++++++++++++++++++++++++++++ qemu/qemu-kvm.c | 26 ++++++++++++++++++++++++++ qemu/qemu-kvm.h | 4 ++++ qemu/target-i386/cpu.h | 3 +++ 7 files changed, 129 insertions(+)qemu/monitor.c +22
-0@@ -1557,6 +1557,31 @@ static void do_info_status(Monitor *mon) } +#if defined(TARGET_I386 ) || defined(TARGET_X86_64 ) +static void do_inject_mce (Monitor *mon, +int cpu_index,int bank, +unsigned status_hi,unsigned status_lo, +unsigned mcg_status_hi,unsigned mcg_status_lo, +unsigned addr_hi,unsigned addr_lo, +unsigned misc_hi,unsigned misc_lo) +{ +CPUState *env; +struct kvm_x86_mce mce = { + .bank = bank, + .status = ((uint64_t )status_hi <<32 ) | status_lo, + .mcg_status = ((uint64_t )mcg_status_hi <<32 ) | mcg_status_lo, + .addr = ((uint64_t )addr_hi <<32 ) | addr_lo, + .misc = ((uint64_t )misc_hi <<32 ) | misc_lo, + }; + +for (env = first_cpu; env !=NULL ; env = env->next_cpu) +if (env->cpu_index == cpu_index && env->mcg_cap) { +kvm_inject_x86_mce (env, &mce); +break ; + }
Quoted 8 lines
qemu/monitor.c static void do_info_status(Monitor *mon) · line 1583
Quoted 2 lines
qemu/monitor.c +2
-0+} +#endif This will break compilation on hosts with older kernel headers. To ease the merge with QEMU,
do_inject_mceshould callqemu_inject_x86_mce, which does nothing for the case where KVM is disabled, or call KVM specific function otherwise (knowledge about structkvm_x86_mceshould be contained within KVM code).
Yes. I will fix this.
Quoted 46 lines
Quoted 46 lines
qemu/monitor.c +2
-0+static void do_balloon (Monitor *mon,int value) {ram_addr_t target = value;@@ -1758,6 +1783,7 @@ static const mon_cmd_t mon_cmds[] = { "[tap,user,socket,vde] options", "add host VLAN client" }, { "host_net_remove", "is", net_host_device_remove, "vlan_id name", "remove host VLAN client" }, + { "mce", "iillll", do_inject_mce, "cpu bank status mcgstatus addr misc", "inject a MCE on the given CPU"}, #endif { "balloon", "i", do_balloon, "target", "request VM to change it's memory allocation (in MB)" },libkvm/libkvm-x86.c +27
-0@@ -379,6 +379,39 @@ int kvm_set_msrs(kvm_context_t kvm, intreturn r; } +int kvm_get_mce_cap_supported (kvm_context_t kvm,uint64_t *mce_cap, +int *max_banks) +{ +int r; + + r =ioctl (kvm->fd,KVM_CHECK_EXTENSION ,KVM_CAP_MCE ); +if (r >0 ) { + *max_banks = r; +return ioctl (kvm->fd,KVM_X86_GET_MCE_CAP_SUPPORTED , mce_cap); + } +return -ENOSYS ; +} + +int kvm_setup_mce (kvm_context_t kvm,int vcpu,uint64_t *mcg_cap) +{ +int r; + + r =ioctl (kvm->fd,KVM_CHECK_EXTENSION ,KVM_CAP_MCE ); +if (r >0 ) +return ioctl (kvm->vcpu_fd[vcpu],KVM_X86_SETUP_MCE , mcg_cap); +return -ENOSYS ; +} + +int kvm_set_mce (kvm_context_t kvm,int vcpu,struct kvm_x86_mce *m) +{ +int r; +
Quoted 8 lines
libkvm/libkvm-x86.c int kvm_set_msrs(kvm_context_t kvm, int · line 413
Quoted 5 lines
libkvm/libkvm-x86.c +5
-0+ r =ioctl (kvm->fd,KVM_CHECK_EXTENSION ,KVM_CAP_MCE ); +if (r >0 ) +return ioctl (kvm->vcpu_fd[vcpu],KVM_X86_SET_MCE , m); +return -ENOSYS ; +}Have to #ifdef
KVM_CAP_MCEwhere appropriate, otherwise compilation on hosts with older kernel headers fail.
Yes. I will fix this.
Best Regards,
Huang Ying