@samitouri / QOSamiQemu / commits / d6d6ed026b

target/i386: decouple cpu_x86_inject_mce() from Monitor

Replace monitor_printf() error reporting with the standard **errp pattern. Drop the Monitor *mon parameter and return bool to indicate success, improving the function usage from non-HMP contexts. Update the KVM MCE injection path and hmp_mce() accordingly. Notes: - we may want to print the error in KVM path too Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com> Message-ID: <20260828-qemu-no-hmp-v5-5-9227de146347@redhat.com>

Marc-André Lureau committed Aug 28, 2026 at 16:04 UTC d6d6ed026b6842bddd44d504d276a90b9bf9ed50
4 files changed +36 -27
target/i386/cpu.h
+2 -2
@@ -2859,9 +2859,9 @@ void do_cpu_init(X86CPU *cpu);
2859 #define MCE_INJECT_BROADCAST 1
2860 #define MCE_INJECT_UNCOND_AO 2
2861
2862 -void cpu_x86_inject_mce(Monitor *mon, X86CPU *cpu, int bank,
2862 +bool cpu_x86_inject_mce(X86CPU *cpu, int bank,
2863 uint64_t status, uint64_t mcg_status, uint64_t addr,
2864 - uint64_t misc, int flags);
2864 + uint64_t misc, int flags, Error **errp);
2865
2866 uint32_t cpu_cc_compute_all(CPUX86State *env1);
2867
target/i386/helper.c
+28 -21
@@ -18,6 +18,7 @@
18 */
19
20 #include "qemu/osdep.h"
21 +#include "qapi/error.h"
22 #include "qapi/qapi-events-run-state.h"
23 #include "cpu.h"
24 #include "exec/cputlb.h"
@@ -27,7 +28,6 @@
28 #ifndef CONFIG_USER_ONLY
29 #include "system/hw_accel.h"
30 #include "system/memory.h"
30 -#include "monitor/monitor.h"
31 #include "kvm/kvm_i386.h"
32 #endif
33 #include "qemu/log.h"
@@ -381,7 +381,7 @@ out:
381 }
382
383 typedef struct MCEInjectionParams {
384 - Monitor *mon;
384 + Error **errp;
385 int bank;
386 uint64_t status;
387 uint64_t mcg_status;
@@ -428,9 +428,9 @@ static void do_inject_x86_mce(CPUState *cs, run_on_cpu_data data)
428 * reporting is disabled
429 */
430 if ((cenv->mcg_cap & MCG_CTL_P) && cenv->mcg_ctl != ~(uint64_t)0) {
431 - monitor_printf(params->mon,
432 - "CPU %d: Uncorrected error reporting disabled\n",
433 - cs->cpu_index);
431 + error_setg(params->errp,
432 + "CPU %d: Uncorrected error reporting disabled",
433 + cs->cpu_index);
434 return;
435 }
436
@@ -439,10 +439,9 @@ static void do_inject_x86_mce(CPUState *cs, run_on_cpu_data data)
439 * reporting is disabled for the bank
440 */
441 if (banks[0] != ~(uint64_t)0) {
442 - monitor_printf(params->mon,
443 - "CPU %d: Uncorrected error reporting disabled for"
444 - " bank %d\n",
445 - cs->cpu_index, params->bank);
442 + error_setg(params->errp,
443 + "CPU %d: Uncorrected error reporting disabled for bank %d",
444 + cs->cpu_index, params->bank);
445 return;
446 }
447
@@ -459,7 +458,7 @@ static void do_inject_x86_mce(CPUState *cs, run_on_cpu_data data)
458 if (need_reset) {
459 emit_guest_memory_failure(MEMORY_FAILURE_ACTION_RESET, ar,
460 recursive);
462 - monitor_printf(params->mon, "%s", msg);
461 + error_setg(params->errp, "%s", msg);
462 qemu_log_mask(CPU_LOG_RESET, "%s\n", msg);
463 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
464 return;
@@ -488,14 +487,15 @@ static void do_inject_x86_mce(CPUState *cs, run_on_cpu_data data)
487 emit_guest_memory_failure(MEMORY_FAILURE_ACTION_INJECT, ar, recursive);
488 }
489
491 -void cpu_x86_inject_mce(Monitor *mon, X86CPU *cpu, int bank,
490 +bool cpu_x86_inject_mce(X86CPU *cpu, int bank,
491 uint64_t status, uint64_t mcg_status, uint64_t addr,
493 - uint64_t misc, int flags)
492 + uint64_t misc, int flags, Error **errp)
493 {
494 + ERRP_GUARD();
495 CPUState *cs = CPU(cpu);
496 CPUX86State *cenv = &cpu->env;
497 MCEInjectionParams params = {
498 - .mon = mon,
498 + .errp = errp,
499 .bank = bank,
500 .status = status,
501 .mcg_status = mcg_status,
@@ -506,24 +506,27 @@ void cpu_x86_inject_mce(Monitor *mon, X86CPU *cpu, int bank,
506 unsigned bank_num = cenv->mcg_cap & 0xff;
507
508 if (!cenv->mcg_cap) {
509 - monitor_printf(mon, "MCE injection not supported\n");
510 - return;
509 + error_setg(errp, "MCE injection not supported");
510 + return false;
511 }
512 if (bank >= bank_num) {
513 - monitor_printf(mon, "Invalid MCE bank number\n");
514 - return;
513 + error_setg(errp, "Invalid MCE bank number");
514 + return false;
515 }
516 if (!(status & MCI_STATUS_VAL)) {
517 - monitor_printf(mon, "Invalid MCE status code\n");
518 - return;
517 + error_setg(errp, "Invalid MCE status code");
518 + return false;
519 }
520 if ((flags & MCE_INJECT_BROADCAST)
521 && !cpu_x86_support_mca_broadcast(cenv)) {
522 - monitor_printf(mon, "Guest CPU does not support MCA broadcast\n");
523 - return;
522 + error_setg(errp, "Guest CPU does not support MCA broadcast");
523 + return false;
524 }
525
526 run_on_cpu(cs, do_inject_x86_mce, RUN_ON_CPU_HOST_PTR(&params));
527 + if (*errp) {
528 + return false;
529 + }
530 if (flags & MCE_INJECT_BROADCAST) {
531 CPUState *other_cs;
532
@@ -537,8 +540,12 @@ void cpu_x86_inject_mce(Monitor *mon, X86CPU *cpu, int bank,
540 continue;
541 }
542 run_on_cpu(other_cs, do_inject_x86_mce, RUN_ON_CPU_HOST_PTR(&params));
543 + if (*errp) {
544 + return false;
545 + }
546 }
547 }
548 + return true;
549 }
550
551 static inline target_ulong get_memio_eip(CPUX86State *env)
target/i386/kvm/kvm.c
+2 -2
@@ -748,8 +748,8 @@ static void kvm_mce_inject(X86CPU *cpu, hwaddr paddr, int code)
748 flags = 0;
749 }
750
751 - cpu_x86_inject_mce(NULL, cpu, 9, status, mcg_status, paddr,
752 - (MCM_ADDR_PHYS << 6) | 0xc, flags);
751 + cpu_x86_inject_mce(cpu, 9, status, mcg_status, paddr,
752 + (MCM_ADDR_PHYS << 6) | 0xc, flags, NULL);
753 }
754
755 static void emit_hypervisor_memory_failure(MemoryFailureAction action, bool ar)
target/i386/monitor.c
+4 -2
@@ -580,6 +580,7 @@ void hmp_mce(Monitor *mon, const QDict *qdict)
580 uint64_t addr = qdict_get_int(qdict, "addr");
581 uint64_t misc = qdict_get_int(qdict, "misc");
582 int flags = MCE_INJECT_UNCOND_AO;
583 + Error *err = NULL;
584
585 if (qdict_get_try_bool(qdict, "broadcast", false)) {
586 flags |= MCE_INJECT_BROADCAST;
@@ -587,7 +588,8 @@ void hmp_mce(Monitor *mon, const QDict *qdict)
588 cs = qemu_get_cpu(cpu_index);
589 if (cs != NULL) {
590 cpu = X86_CPU(cs);
590 - cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc,
591 - flags);
591 + cpu_x86_inject_mce(cpu, bank, status, mcg_status, addr, misc,
592 + flags, &err);
593 + hmp_handle_error(mon, err);
594 }
595 }