diff mbox series

[v1,7/8] xen/console: introduce console_set_focus()

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

Commit Message

Denis Mukhin March 18, 2025, 11:37 p.m. UTC
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>
---
 xen/drivers/char/console.c | 81 ++++++++++++++++----------------------
 1 file changed, 34 insertions(+), 47 deletions(-)

Comments

Denis Mukhin March 19, 2025, 7:53 p.m. UTC | #1
On Tuesday, March 18th, 2025 at 4:37 PM, dmkhn@proton.me <dmkhn@proton.me> wrote:

> 
> 
> 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
> 
> ---
> xen/drivers/char/console.c | 81 ++++++++++++++++----------------------
> 1 file changed, 34 insertions(+), 47 deletions(-)
> 
> diff --git a/xen/drivers/char/console.c b/xen/drivers/char/console.c
> index 3d538510f4..0e2349a868 100644
> --- a/xen/drivers/char/console.c
> +++ b/xen/drivers/char/console.c
> @@ -462,14 +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 owner domain identifier. /
> -static unsigned int __read_mostly console_focus = 0;
> -
> -#define max_console_rx (domid_top + 1)
> +static domid_t __read_mostly console_focus = DOMID_XEN;
> 
> static struct domain console_get_domain_by_id(domid_t domid)
> {
> @@ -488,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)
> @@ -508,42 +504,41 @@ static bool console_check_focus_by_id(domid_t domid)
> return !!d;
> }
> 
> -static void console_switch_focus(void)
> +static int console_set_focus(domid_t domid)
> {
> - unsigned int next_rx = console_focus;
> + if ( domid == DOMID_XEN )
> + printk(" Serial input to Xen");
> + else if ( console_check_focus_by_id(domid) )
> + printk("* Serial input to DOM%u", domid);
> + else
> + return -ENOENT;
> 
> - /*
> - * 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_check_focus_by_id(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 0;
> +}
> +
> +/*
> + * Switch console focus.
> + * Rotates input focus among Xen, dom0 and boot-time created domUs while
> + * skipping switching serial input to non existing domains.
> + */
> +static void console_switch_focus(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;
> +
> + console_set_focus(DOMID_XEN);
> }
> 
> static void __serial_rx(char c)
> @@ -551,7 +546,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();
> @@ -1141,14 +1136,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,
> @@ -1158,8 +1145,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_focus();
> + if ( opt_conswitch[1] != 'x' )
> + console_set_focus( get_initial_domain_id() );

Forgot to drop extra spaces around the function parameter.

> }
> 
> int __init console_has(const char *device)
> --
> 2.34.1
diff mbox series

Patch

diff --git a/xen/drivers/char/console.c b/xen/drivers/char/console.c
index 3d538510f4..0e2349a868 100644
--- a/xen/drivers/char/console.c
+++ b/xen/drivers/char/console.c
@@ -462,14 +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 owner domain identifier. */
-static unsigned int __read_mostly console_focus = 0;
-
-#define max_console_rx (domid_top + 1)
+static domid_t __read_mostly console_focus = DOMID_XEN;
 
 static struct domain *console_get_domain_by_id(domid_t domid)
 {
@@ -488,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)
@@ -508,42 +504,41 @@  static bool console_check_focus_by_id(domid_t domid)
     return !!d;
 }
 
-static void console_switch_focus(void)
+static int console_set_focus(domid_t domid)
 {
-    unsigned int next_rx = console_focus;
+    if ( domid == DOMID_XEN )
+        printk("*** Serial input to Xen");
+    else if ( console_check_focus_by_id(domid) )
+        printk("*** Serial input to DOM%u", domid);
+    else
+        return -ENOENT;
 
-    /*
-     * 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_check_focus_by_id(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 0;
+}
+
+/*
+ * Switch console focus.
+ * Rotates input focus among Xen, dom0 and boot-time created domUs while
+ * skipping switching serial input to non existing domains.
+ */
+static void console_switch_focus(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;
+
+    console_set_focus(DOMID_XEN);
 }
 
 static void __serial_rx(char c)
@@ -551,7 +546,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();
@@ -1141,14 +1136,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,
@@ -1158,8 +1145,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_focus();
+    if ( opt_conswitch[1] != 'x' )
+        console_set_focus( get_initial_domain_id() );
 }
 
 int __init console_has(const char *device)