summary refs log tree commit diff stats
path: root/monitor/hmp.c
diff options
context:
space:
mode:
authorPaolo Bonzini <pbonzini@redhat.com>2023-03-03 13:32:13 +0100
committerPaolo Bonzini <pbonzini@redhat.com>2023-05-25 10:18:33 +0200
commit6ee7c82d0df9bb6e972a8ea689b935df3ba37486 (patch)
treef99a5786fb54b9072069e498313d4336bd9aaa6a /monitor/hmp.c
parent4cb96b974265f97a9902b4458e50d01082572a16 (diff)
downloadfocaccia-qemu-6ee7c82d0df9bb6e972a8ea689b935df3ba37486.tar.gz
focaccia-qemu-6ee7c82d0df9bb6e972a8ea689b935df3ba37486.zip
monitor: do not use mb_read/mb_set for suspend_cnt
Clean up monitor_event to just use monitor_suspend/monitor_resume,
using mon->mux_out to protect against incorrect nesting (especially
on startup).

The only remaining case of reading suspend_cnt is in the can_read
callback, which is just advisory and can use qatomic_read.

As an extra benefit, mux_out is now simply protected by mon_lock.
Also, moving the prompt to the beginning of the main loop removes
it from the output in some error cases where QEMU does not actually
start successfully.  It is not a full fix and it would be nice to
also remove the monitor heading, but this is already a small (though
unintentional) improvement.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'monitor/hmp.c')
-rw-r--r--monitor/hmp.c35
1 files changed, 16 insertions, 19 deletions
diff --git a/monitor/hmp.c b/monitor/hmp.c
index 5cab56d355..69c1b7e98a 100644
--- a/monitor/hmp.c
+++ b/monitor/hmp.c
@@ -1401,45 +1401,42 @@ static void monitor_read(void *opaque, const uint8_t *buf, int size)
 static void monitor_event(void *opaque, QEMUChrEvent event)
 {
     Monitor *mon = opaque;
-    MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
 
     switch (event) {
     case CHR_EVENT_MUX_IN:
         qemu_mutex_lock(&mon->mon_lock);
-        mon->mux_out = 0;
-        qemu_mutex_unlock(&mon->mon_lock);
-        if (mon->reset_seen) {
-            readline_restart(hmp_mon->rs);
+        if (mon->mux_out) {
+            mon->mux_out = 0;
             monitor_resume(mon);
-            monitor_flush(mon);
-        } else {
-            qatomic_mb_set(&mon->suspend_cnt, 0);
         }
+        qemu_mutex_unlock(&mon->mon_lock);
         break;
 
     case CHR_EVENT_MUX_OUT:
-        if (mon->reset_seen) {
-            if (qatomic_mb_read(&mon->suspend_cnt) == 0) {
-                monitor_printf(mon, "\n");
+        qemu_mutex_lock(&mon->mon_lock);
+        if (!mon->mux_out) {
+            if (mon->reset_seen && !mon->suspend_cnt) {
+                monitor_puts_locked(mon, "\n");
+            } else {
+                monitor_flush_locked(mon);
             }
-            monitor_flush(mon);
             monitor_suspend(mon);
-        } else {
-            qatomic_inc(&mon->suspend_cnt);
+            mon->mux_out = 1;
         }
-        qemu_mutex_lock(&mon->mon_lock);
-        mon->mux_out = 1;
         qemu_mutex_unlock(&mon->mon_lock);
         break;
 
     case CHR_EVENT_OPENED:
         monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
                        "information\n", QEMU_VERSION);
+        qemu_mutex_lock(&mon->mon_lock);
+        mon->reset_seen = 1;
         if (!mon->mux_out) {
-            readline_restart(hmp_mon->rs);
-            readline_show_prompt(hmp_mon->rs);
+            /* Suspend-resume forces the prompt to be printed.  */
+            monitor_suspend(mon);
+            monitor_resume(mon);
         }
-        mon->reset_seen = 1;
+        qemu_mutex_unlock(&mon->mon_lock);
         mon_refcount++;
         break;