diff mbox series

[v2,6/7] xen/console: introduce console_set_focus()

Message ID 20250331230508.440198-7-dmukhin@ford.com (mailing list archive)
State New
Headers show
Series xen/console: cleanup console input switch logic | expand

Commit Message

Denis Mukhin March 31, 2025, 11:06 p.m. UTC
From: Denis Mukhin <dmukhin@ford.com>

Switch console_focus address space from integers mapped to domain IDs to
direct domain IDs, simplifying the console input switching code.

Introduce console_set_focus() to set the console owner domain identifier.

Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
Changes since v1:
- update console_focus description
---
 xen/drivers/char/console.c | 88 ++++++++++++++++----------------------
 1 file changed, 37 insertions(+), 51 deletions(-)
diff mbox series

Patch

diff --git a/xen/drivers/char/console.c b/xen/drivers/char/console.c
index 42974c0af7..9905ffd6b9 100644
--- a/xen/drivers/char/console.c
+++ b/xen/drivers/char/console.c
@@ -462,17 +462,12 @@  static void cf_check dump_console_ring_key(unsigned char key)
 
 /*
  * CTRL-<switch_char> changes input direction, rotating among Xen, Dom0,
- * and the DomUs started from Xen at boot.
+ * and the DomUs.
  */
 #define switch_code (opt_conswitch[0]-'a'+1)
-/*
- * console_rx=0 => input to xen
- * console_rx=1 => input to dom0 (or the sole shim domain)
- * console_rx=N => input to dom(N-1)
- */
-static unsigned int __read_mostly console_focus = 0;
 
-#define max_console_rx (domid_top() + 1)
+/* Console owner domain identifier. */
+static domid_t __read_mostly console_focus = DOMID_XEN;
 
 static struct domain *console_get_domain_by_id(domid_t domid)
 {
@@ -491,9 +486,7 @@  static struct domain *console_get_domain_by_id(domid_t domid)
 
 struct domain *console_get_domain(void)
 {
-    if ( console_focus == 0 )
-            return NULL;
-    return console_get_domain_by_id(console_focus - 1);
+    return console_get_domain_by_id(console_focus);
 }
 
 void console_put_domain(struct domain *d)
@@ -511,42 +504,43 @@  static bool console_is_input_allowed(domid_t domid)
     return !!d;
 }
 
-static void console_switch_input(void)
+/*
+ * Set owner of the physical console input.
+ */
+static bool cf_check console_set_focus(domid_t domid)
 {
-    unsigned int next_rx = console_focus;
+    if ( domid == DOMID_XEN )
+        printk("*** Serial input to Xen");
+    else if ( console_is_input_allowed(domid) )
+        printk("*** Serial input to DOM%u", domid);
+    else
+        return false;
 
-    /*
-     * Rotate among Xen, dom0 and boot-time created domUs while skipping
-     * switching serial input to non existing domains.
-     */
-    for ( ; ; )
-    {
-        domid_t domid;
-
-        if ( next_rx++ >= max_console_rx )
-        {
-            console_focus = 0;
-            printk("*** Serial input to Xen");
-            break;
-        }
-
-        if ( consoled_is_enabled() && next_rx == 1 )
-            domid = get_initial_domain_id();
-        else
-            domid = next_rx - 1;
-
-        if ( console_is_input_allowed(domid) )
-        {
-            console_focus = next_rx;
-            printk("*** Serial input to DOM%u", domid);
-            break;
-        }
-    }
+    console_focus = domid;
 
     if ( switch_code )
         printk(" (type 'CTRL-%c' three times to switch input)",
                opt_conswitch[0]);
     printk("\n");
+
+    return true;
+}
+
+/*
+ * Switch console focus.
+ * Rotates input focus among Xen and domains with console input permission.
+ */
+static void console_switch_input(void)
+{
+    const domid_t n = domid_top() + 1;
+    domid_t i = ( console_focus == DOMID_XEN )
+                ? get_initial_domain_id() : console_focus + 1;
+
+    for ( ; i < n; i++ )
+        if ( console_set_focus(i) )
+            return;
+
+    ASSERT(console_set_focus(DOMID_XEN));
 }
 
 static void __serial_rx(char c)
@@ -554,7 +548,7 @@  static void __serial_rx(char c)
     struct domain *d;
     int rc = 0;
 
-    if ( console_focus == 0 )
+    if ( console_focus == DOMID_XEN )
         return handle_keypress(c, false);
 
     d = console_get_domain();
@@ -1129,14 +1123,6 @@  void __init console_endboot(void)
 
     video_endboot();
 
-    /*
-     * If user specifies so, we fool the switch routine to redirect input
-     * straight back to Xen. I use this convoluted method so we still print
-     * a useful 'how to switch' message.
-     */
-    if ( opt_conswitch[1] == 'x' )
-        console_focus = max_console_rx;
-
     register_keyhandler('w', dump_console_ring_key,
                         "synchronously dump console ring buffer (dmesg)", 0);
     register_irq_keyhandler('+', &do_inc_thresh,
@@ -1146,8 +1132,8 @@  void __init console_endboot(void)
     register_irq_keyhandler('G', &do_toggle_guest,
                             "toggle host/guest log level adjustment", 0);
 
-    /* Serial input is directed to DOM0 by default. */
-    console_switch_input();
+    if ( opt_conswitch[1] != 'x' )
+        (void)console_set_focus(get_initial_domain_id());
 }
 
 int __init console_has(const char *device)