All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/5] xen/console: cleanup console input switch logic
@ 2025-05-19 20:12 dmkhn
  2025-05-19 20:12 ` [PATCH v3 1/5] xen/console: rename switch_serial_input() to console_switch_input() dmkhn
                   ` (4 more replies)
  0 siblings, 5 replies; 10+ messages in thread
From: dmkhn @ 2025-05-19 20:12 UTC (permalink / raw)
  To: xen-devel
  Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
	roger.pau, sstabellini, dmukhin

The patch series originates from the NS16550 UART emulator series [1] (x86)
which requires ability to switch physical console input to a PVH/HVM domain
with an emulated UART.

Currently, on x86, console input can be rotated in round-robin manner only
between dom0, PV shim, and Xen itself. On Arm the input rotation can include
domUs with vpl011.

The main idea of this series is introducing a per-domain permission flag that
is set during domain initialization and used by the console driver to switch
the input across permitted domains.

Patch 1 performs rename of switch_serial_input() to fit the console driver
notation.

Patch 2 simplifies the existing vUART code by using newly introduced API.

Patch 3 introduces a new domain permission flag to mark ownership of the
console input for the console driver.

Patch 4 cleans up the console input switch logic by removing dependency
on max_init_domid. Depends on patches 1, 3 and [2].

Patch 5 performs rename of console_rx to console_domid to match the usage
of the symbol in the code.

[1] https://lore.kernel.org/xen-devel/20250103-vuart-ns8250-v3-v1-0-c5d36b31d66c@ford.com/
[2] https://lore.kernel.org/xen-devel/20250519192306.1364471-1-dmukhin@ford.com/
[3] Link to v2: https://lore.kernel.org/xen-devel/20250331230508.440198-1-dmukhin@ford.com/
[4] Link to CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/1825448327

Denis Mukhin (5):
  xen/console: rename switch_serial_input() to console_switch_input()
  xen/console: introduce console_get_domid()
  xen/console: introduce console input permission
  xen/console: remove max_init_domid dependency
  xen/console: rename console_rx to console_domid

 xen/arch/arm/include/asm/setup.h        |   2 -
 xen/arch/arm/setup.c                    |   2 -
 xen/arch/arm/vpl011.c                   |   7 +-
 xen/arch/ppc/include/asm/setup.h        |   2 -
 xen/arch/riscv/include/asm/setup.h      |   2 -
 xen/arch/x86/include/asm/setup.h        |   2 -
 xen/arch/x86/pv/shim.c                  |   2 +
 xen/common/device-tree/dom0less-build.c |   2 -
 xen/common/domain.c                     |  31 +++++++
 xen/drivers/char/console.c              | 105 +++++++++++-------------
 xen/include/xen/console.h               |   3 +-
 xen/include/xen/domain.h                |   1 +
 xen/include/xen/sched.h                 |   8 +-
 13 files changed, 94 insertions(+), 75 deletions(-)

-- 
2.34.1




^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH v3 1/5] xen/console: rename switch_serial_input() to console_switch_input()
  2025-05-19 20:12 [PATCH v3 0/5] xen/console: cleanup console input switch logic dmkhn
@ 2025-05-19 20:12 ` dmkhn
  2025-05-20  6:07   ` Jan Beulich
  2025-05-19 20:12 ` [PATCH v3 2/5] xen/console: introduce console_get_domid() dmkhn
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 10+ messages in thread
From: dmkhn @ 2025-05-19 20:12 UTC (permalink / raw)
  To: xen-devel
  Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
	roger.pau, sstabellini, dmukhin

From: Denis Mukhin <dmukhin@ford.com>

Update the function name as per naming notation in the console driver.

No functional change.

Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
Changes since v2:
- rebased
- Link to v2: https://lore.kernel.org/xen-devel/20250331230508.440198-5-dmukhin@ford.com/
---
 xen/drivers/char/console.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/xen/drivers/char/console.c b/xen/drivers/char/console.c
index c3150fbdb7..c8dde38376 100644
--- a/xen/drivers/char/console.c
+++ b/xen/drivers/char/console.c
@@ -487,7 +487,7 @@ void console_put_domain(struct domain *d)
         rcu_unlock_domain(d);
 }
 
-static void switch_serial_input(void)
+static void console_switch_input(void)
 {
     unsigned int next_rx = console_rx;
 
@@ -581,7 +581,7 @@ static void cf_check serial_rx(char c)
         /* We eat CTRL-<switch_char> in groups of 3 to switch console input. */
         if ( ++switch_code_count == 3 )
         {
-            switch_serial_input();
+            console_switch_input();
             switch_code_count = 0;
         }
         return;
@@ -1125,7 +1125,7 @@ void __init console_endboot(void)
                             "toggle host/guest log level adjustment", 0);
 
     /* Serial input is directed to DOM0 by default. */
-    switch_serial_input();
+    console_switch_input();
 }
 
 int __init console_has(const char *device)
-- 
2.34.1




^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v3 2/5] xen/console: introduce console_get_domid()
  2025-05-19 20:12 [PATCH v3 0/5] xen/console: cleanup console input switch logic dmkhn
  2025-05-19 20:12 ` [PATCH v3 1/5] xen/console: rename switch_serial_input() to console_switch_input() dmkhn
@ 2025-05-19 20:12 ` dmkhn
  2025-05-20  6:20   ` Jan Beulich
  2025-05-19 20:12 ` [PATCH v3 3/5] xen/console: introduce console input permission dmkhn
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 10+ messages in thread
From: dmkhn @ 2025-05-19 20:12 UTC (permalink / raw)
  To: xen-devel
  Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
	roger.pau, sstabellini, dmukhin

From: Denis Mukhin <dmukhin@ford.com>

Add console_get_domid() to the console driver to retrieve the current console
owner domain ID.

Use the function in vpl011 emulator which leads to simpler code.

Make console_{get,put}_domain() private to the console driver.

No functional change intended.

Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
Changes since v2:
- changed commit subject line
- Link to v2: https://lore.kernel.org/xen-devel/20250331230508.440198-8-dmukhin@ford.com/
---
 xen/arch/arm/vpl011.c      |  5 +----
 xen/drivers/char/console.c | 11 +++++++++--
 xen/include/xen/console.h  |  3 +--
 3 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/xen/arch/arm/vpl011.c b/xen/arch/arm/vpl011.c
index 66047bf33c..0f58b2c900 100644
--- a/xen/arch/arm/vpl011.c
+++ b/xen/arch/arm/vpl011.c
@@ -78,12 +78,11 @@ static void vpl011_write_data_xen(struct domain *d, uint8_t data)
     unsigned long flags;
     struct vpl011 *vpl011 = &d->arch.vpl011;
     struct vpl011_xen_backend *intf = vpl011->backend.xen;
-    struct domain *input = console_get_domain();
 
     VPL011_LOCK(d, flags);
 
     intf->out[intf->out_prod++] = data;
-    if ( d == input )
+    if ( d->domain_id == console_get_domid() )
     {
         if ( intf->out_prod == 1 )
         {
@@ -123,8 +122,6 @@ static void vpl011_write_data_xen(struct domain *d, uint8_t data)
     vpl011_update_interrupt_status(d);
 
     VPL011_UNLOCK(d, flags);
-
-    console_put_domain(input);
 }
 
 /*
diff --git a/xen/drivers/char/console.c b/xen/drivers/char/console.c
index c8dde38376..86fd0b427d 100644
--- a/xen/drivers/char/console.c
+++ b/xen/drivers/char/console.c
@@ -474,19 +474,26 @@ static unsigned int __read_mostly console_rx = 0;
 
 #define max_console_rx (max_init_domid + 1)
 
-struct domain *console_get_domain(void)
+static struct domain *console_get_domain(void)
 {
     if ( console_rx == 0 )
             return NULL;
     return rcu_lock_domain_by_id(console_rx - 1);
 }
 
-void console_put_domain(struct domain *d)
+static void console_put_domain(struct domain *d)
 {
     if ( d )
         rcu_unlock_domain(d);
 }
 
+domid_t console_get_domid(void)
+{
+    if ( console_rx == 0 )
+        return DOMID_XEN;
+    return console_rx - 1;
+}
+
 static void console_switch_input(void)
 {
     unsigned int next_rx = console_rx;
diff --git a/xen/include/xen/console.h b/xen/include/xen/console.h
index 83cbc9fbda..c6f9d84d80 100644
--- a/xen/include/xen/console.h
+++ b/xen/include/xen/console.h
@@ -32,8 +32,7 @@ void console_end_sync(void);
 void console_start_log_everything(void);
 void console_end_log_everything(void);
 
-struct domain *console_get_domain(void);
-void console_put_domain(struct domain *d);
+domid_t console_get_domid(void);
 
 /*
  * Steal output from the console. Returns +ve identifier, else -ve error.
-- 
2.34.1




^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v3 3/5] xen/console: introduce console input permission
  2025-05-19 20:12 [PATCH v3 0/5] xen/console: cleanup console input switch logic dmkhn
  2025-05-19 20:12 ` [PATCH v3 1/5] xen/console: rename switch_serial_input() to console_switch_input() dmkhn
  2025-05-19 20:12 ` [PATCH v3 2/5] xen/console: introduce console_get_domid() dmkhn
@ 2025-05-19 20:12 ` dmkhn
  2025-05-19 20:12 ` [PATCH v3 4/5] xen/console: remove max_init_domid dependency dmkhn
  2025-05-19 20:12 ` [PATCH v3 5/5] xen/console: rename console_rx to console_domid dmkhn
  4 siblings, 0 replies; 10+ messages in thread
From: dmkhn @ 2025-05-19 20:12 UTC (permalink / raw)
  To: xen-devel
  Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
	roger.pau, sstabellini, dmukhin

From: Denis Mukhin <dmukhin@ford.com>

Add new flag to domain structure for marking permission to intercept
the physical console input by the domain.

Update console input switch logic accordingly.

No functional change intended.

Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
Changes since v2:
- rebased
- Link to v2: https://lore.kernel.org/xen-devel/20250331230508.440198-2-dmukhin@ford.com/
---
 xen/arch/arm/vpl011.c      |  2 ++
 xen/arch/x86/pv/shim.c     |  2 ++
 xen/common/domain.c        |  2 ++
 xen/drivers/char/console.c | 18 +++++++++++++++++-
 xen/include/xen/sched.h    |  8 +++++++-
 5 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/xen/arch/arm/vpl011.c b/xen/arch/arm/vpl011.c
index 0f58b2c900..d0e5504e9e 100644
--- a/xen/arch/arm/vpl011.c
+++ b/xen/arch/arm/vpl011.c
@@ -734,6 +734,8 @@ int domain_vpl011_init(struct domain *d, struct vpl011_init_info *info)
     register_mmio_handler(d, &vpl011_mmio_handler,
                           vpl011->base_addr, GUEST_PL011_SIZE, NULL);
 
+    d->console.input_allowed = true;
+
     return 0;
 
 out1:
diff --git a/xen/arch/x86/pv/shim.c b/xen/arch/x86/pv/shim.c
index c506cc0bec..bc2a7dd5fa 100644
--- a/xen/arch/x86/pv/shim.c
+++ b/xen/arch/x86/pv/shim.c
@@ -238,6 +238,8 @@ void __init pv_shim_setup_dom(struct domain *d, l4_pgentry_t *l4start,
      * guest from depleting the shim memory pool.
      */
     d->max_pages = domain_tot_pages(d);
+
+    d->console.input_allowed = true;
 }
 
 static void write_start_info(struct domain *d)
diff --git a/xen/common/domain.c b/xen/common/domain.c
index cb05156ff5..6a6cdd991b 100644
--- a/xen/common/domain.c
+++ b/xen/common/domain.c
@@ -836,6 +836,8 @@ struct domain *domain_create(domid_t domid,
         flags |= CDF_hardware;
         if ( old_hwdom )
             old_hwdom->cdf &= ~CDF_hardware;
+
+        d->console.input_allowed = true;
     }
 
     /* Holding CDF_* internal flags. */
diff --git a/xen/drivers/char/console.c b/xen/drivers/char/console.c
index 86fd0b427d..ccecda6523 100644
--- a/xen/drivers/char/console.c
+++ b/xen/drivers/char/console.c
@@ -476,9 +476,21 @@ static unsigned int __read_mostly console_rx = 0;
 
 static struct domain *console_get_domain(void)
 {
+    struct domain *d;
+
     if ( console_rx == 0 )
             return NULL;
-    return rcu_lock_domain_by_id(console_rx - 1);
+
+    d = rcu_lock_domain_by_id(console_rx - 1);
+    if ( !d )
+        return NULL;
+
+    if ( d->console.input_allowed )
+        return d;
+
+    rcu_unlock_domain(d);
+
+    return NULL;
 }
 
 static void console_put_domain(struct domain *d)
@@ -522,6 +534,10 @@ static void console_switch_input(void)
         if ( d )
         {
             rcu_unlock_domain(d);
+
+            if ( !d->console.input_allowed )
+                break;
+
             console_rx = next_rx;
             printk("*** Serial input to DOM%u", domid);
             break;
diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h
index 559d201e0c..e91c99a8f3 100644
--- a/xen/include/xen/sched.h
+++ b/xen/include/xen/sched.h
@@ -512,7 +512,7 @@ struct domain
     bool             auto_node_affinity;
     /* Is this guest fully privileged (aka dom0)? */
     bool             is_privileged;
-    /* Can this guest access the Xen console? */
+    /* XSM: permission to use HYPERCALL_console_io hypercall */
     bool             is_console;
     /* Is this guest being debugged by dom0? */
     bool             debugger_attached;
@@ -651,6 +651,12 @@ struct domain
     unsigned int num_llc_colors;
     const unsigned int *llc_colors;
 #endif
+
+    /* Console settings. */
+    struct {
+        /* Permission to take ownership of the physical console input. */
+        bool input_allowed;
+    } console;
 } __aligned(PAGE_SIZE);
 
 static inline struct page_list_head *page_to_list(
-- 
2.34.1




^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v3 4/5] xen/console: remove max_init_domid dependency
  2025-05-19 20:12 [PATCH v3 0/5] xen/console: cleanup console input switch logic dmkhn
                   ` (2 preceding siblings ...)
  2025-05-19 20:12 ` [PATCH v3 3/5] xen/console: introduce console input permission dmkhn
@ 2025-05-19 20:12 ` dmkhn
  2025-05-20  6:28   ` Jan Beulich
  2025-05-19 20:12 ` [PATCH v3 5/5] xen/console: rename console_rx to console_domid dmkhn
  4 siblings, 1 reply; 10+ messages in thread
From: dmkhn @ 2025-05-19 20:12 UTC (permalink / raw)
  To: xen-devel
  Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
	roger.pau, sstabellini, dmukhin

From: Denis Mukhin <dmukhin@ford.com>

The physical console input rotation depends on max_init_domid symbol, which is
managed differently across architectures.

Instead of trying to manage max_init_domid in the arch-common code the console
input rotation code can be reworked by removing dependency on max_init_domid
entirely.

To do that, introduce domid_find_with_input_allowed() in arch-independent
location to find the ID of the next possible console owner domain. The IDs
are rotated across non-system domain IDs and DOMID_XEN.

Also, introduce helper console_set_domid() for updating identifier of the
current console input owner (points to Xen or domain).

Use domid_find_with_input_allowed() and console_set_domid() in
console_switch_input().

Remove uses of max_init_domid in the code.

Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
Changes since v2:
- reworked to traversing domain list directly
- Link to v2: https://lore.kernel.org/xen-devel/20250331230508.440198-7-dmukhin@ford.com/
---
 xen/arch/arm/include/asm/setup.h        |   2 -
 xen/arch/arm/setup.c                    |   2 -
 xen/arch/ppc/include/asm/setup.h        |   2 -
 xen/arch/riscv/include/asm/setup.h      |   2 -
 xen/arch/x86/include/asm/setup.h        |   2 -
 xen/common/device-tree/dom0less-build.c |   2 -
 xen/common/domain.c                     |  29 +++++++
 xen/drivers/char/console.c              | 102 +++++++++---------------
 xen/include/xen/domain.h                |   1 +
 9 files changed, 66 insertions(+), 78 deletions(-)

diff --git a/xen/arch/arm/include/asm/setup.h b/xen/arch/arm/include/asm/setup.h
index 6cf272c160..f107e8eebb 100644
--- a/xen/arch/arm/include/asm/setup.h
+++ b/xen/arch/arm/include/asm/setup.h
@@ -25,8 +25,6 @@ struct map_range_data
     struct rangeset *irq_ranges;
 };
 
-extern domid_t max_init_domid;
-
 void copy_from_paddr(void *dst, paddr_t paddr, unsigned long len);
 
 size_t estimate_efi_size(unsigned int mem_nr_banks);
diff --git a/xen/arch/arm/setup.c b/xen/arch/arm/setup.c
index 10b46d0684..53e2f8b537 100644
--- a/xen/arch/arm/setup.c
+++ b/xen/arch/arm/setup.c
@@ -61,8 +61,6 @@ struct cpuinfo_arm __read_mostly system_cpuinfo;
 bool __read_mostly acpi_disabled;
 #endif
 
-domid_t __read_mostly max_init_domid;
-
 static __used void init_done(void)
 {
     int rc;
diff --git a/xen/arch/ppc/include/asm/setup.h b/xen/arch/ppc/include/asm/setup.h
index e4f64879b6..956fa6985a 100644
--- a/xen/arch/ppc/include/asm/setup.h
+++ b/xen/arch/ppc/include/asm/setup.h
@@ -1,6 +1,4 @@
 #ifndef __ASM_PPC_SETUP_H__
 #define __ASM_PPC_SETUP_H__
 
-#define max_init_domid (0)
-
 #endif /* __ASM_PPC_SETUP_H__ */
diff --git a/xen/arch/riscv/include/asm/setup.h b/xen/arch/riscv/include/asm/setup.h
index c9d69cdf51..d1fc64b673 100644
--- a/xen/arch/riscv/include/asm/setup.h
+++ b/xen/arch/riscv/include/asm/setup.h
@@ -5,8 +5,6 @@
 
 #include <xen/types.h>
 
-#define max_init_domid (0)
-
 void setup_mm(void);
 
 void copy_from_paddr(void *dst, paddr_t paddr, unsigned long len);
diff --git a/xen/arch/x86/include/asm/setup.h b/xen/arch/x86/include/asm/setup.h
index ac34c69855..b67de8577f 100644
--- a/xen/arch/x86/include/asm/setup.h
+++ b/xen/arch/x86/include/asm/setup.h
@@ -69,6 +69,4 @@ extern bool opt_dom0_verbose;
 extern bool opt_dom0_cpuid_faulting;
 extern bool opt_dom0_msr_relaxed;
 
-#define max_init_domid (0)
-
 #endif
diff --git a/xen/common/device-tree/dom0less-build.c b/xen/common/device-tree/dom0less-build.c
index 8e38affd0c..4c050b9ded 100644
--- a/xen/common/device-tree/dom0less-build.c
+++ b/xen/common/device-tree/dom0less-build.c
@@ -977,8 +977,6 @@ void __init create_domUs(void)
         domid = domid_alloc(DOMID_INVALID);
         if ( domid == DOMID_INVALID )
             panic("Error allocating ID for domain %s\n", dt_node_name(node));
-        if ( max_init_domid < domid )
-            max_init_domid = domid;
 
         d = domain_create(domid, &d_cfg, flags);
         if ( IS_ERR(d) )
diff --git a/xen/common/domain.c b/xen/common/domain.c
index 6a6cdd991b..90bd42b633 100644
--- a/xen/common/domain.c
+++ b/xen/common/domain.c
@@ -2460,6 +2460,35 @@ void domid_free(domid_t domid)
     spin_unlock(&domid_lock);
 }
 
+/*
+ * Find the ID of the next possible console owner domain.
+ *
+ * @return Domain ID: DOMID_XEN or non-system domain IDs within
+ * the range of [0..CONFIG_MAX_DOMID-1].
+ */
+domid_t domid_find_with_input_allowed(domid_t hint)
+{
+    struct domain *d;
+    domid_t domid = DOMID_XEN;
+
+    spin_lock(&domlist_update_lock);
+
+    for ( d = domid_to_domain(hint);
+          d && likely(get_domain(d)) && (d->domain_id < CONFIG_MAX_DOMID);
+          d = d->next_in_list )
+    {
+        if ( d->console.input_allowed )
+        {
+            domid = d->domain_id;
+            break;
+        }
+    }
+
+    spin_unlock(&domlist_update_lock);
+
+    return domid;
+}
+
 /*
  * Local variables:
  * mode: C
diff --git a/xen/drivers/char/console.c b/xen/drivers/char/console.c
index ccecda6523..ff20706ac9 100644
--- a/xen/drivers/char/console.c
+++ b/xen/drivers/char/console.c
@@ -462,26 +462,17 @@ 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_rx = 0;
 
-#define max_console_rx (max_init_domid + 1)
+/* Console owner domain identifier. */
+static domid_t __read_mostly console_rx = DOMID_XEN;
 
 static struct domain *console_get_domain(void)
 {
-    struct domain *d;
+    struct domain *d = rcu_lock_domain_by_id(console_rx);
 
-    if ( console_rx == 0 )
-            return NULL;
-
-    d = rcu_lock_domain_by_id(console_rx - 1);
     if ( !d )
         return NULL;
 
@@ -499,50 +490,14 @@ static void console_put_domain(struct domain *d)
         rcu_unlock_domain(d);
 }
 
-domid_t console_get_domid(void)
+static void console_set_domid(domid_t domid)
 {
-    if ( console_rx == 0 )
-        return DOMID_XEN;
-    return console_rx - 1;
-}
+    if ( domid == DOMID_XEN )
+        printk("*** Serial input to Xen");
+    else
+        printk("*** Serial input to DOM%u", domid);
 
-static void console_switch_input(void)
-{
-    unsigned int next_rx = console_rx;
-
-    /*
-     * Rotate among Xen, dom0 and boot-time created domUs while skipping
-     * switching serial input to non existing domains.
-     */
-    for ( ; ; )
-    {
-        domid_t domid;
-        struct domain *d;
-
-        if ( next_rx++ >= max_console_rx )
-        {
-            console_rx = 0;
-            printk("*** Serial input to Xen");
-            break;
-        }
-
-        if ( consoled_is_enabled() && next_rx == 1 )
-            domid = get_initial_domain_id();
-        else
-            domid = next_rx - 1;
-        d = rcu_lock_domain_by_id(domid);
-        if ( d )
-        {
-            rcu_unlock_domain(d);
-
-            if ( !d->console.input_allowed )
-                break;
-
-            console_rx = next_rx;
-            printk("*** Serial input to DOM%u", domid);
-            break;
-        }
-    }
+    console_rx = domid;
 
     if ( switch_code )
         printk(" (type 'CTRL-%c' three times to switch input)",
@@ -550,12 +505,35 @@ static void console_switch_input(void)
     printk("\n");
 }
 
+domid_t console_get_domid(void)
+{
+    return console_rx;
+}
+
+/*
+ * Switch console focus.
+ * Rotates input focus among Xen and domains with console input permission.
+ */
+static void console_switch_input(void)
+{
+    domid_t hint;
+
+    if ( console_rx == DOMID_XEN )
+        hint = get_initial_domain_id();
+    else
+        hint = console_rx + 1;
+
+    hint = domid_find_with_input_allowed(hint);
+
+    console_set_domid(hint);
+}
+
 static void __serial_rx(char c)
 {
     struct domain *d;
     int rc = 0;
 
-    if ( console_rx == 0 )
+    if ( console_rx == DOMID_XEN )
         return handle_keypress(c, false);
 
     d = console_get_domain();
@@ -1130,14 +1108,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_rx = max_console_rx;
-
     register_keyhandler('w', dump_console_ring_key,
                         "synchronously dump console ring buffer (dmesg)", 0);
     register_irq_keyhandler('+', &do_inc_thresh,
@@ -1147,8 +1117,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_domid(get_initial_domain_id());
 }
 
 int __init console_has(const char *device)
diff --git a/xen/include/xen/domain.h b/xen/include/xen/domain.h
index 8aab05ae93..a88eb34f3f 100644
--- a/xen/include/xen/domain.h
+++ b/xen/include/xen/domain.h
@@ -36,6 +36,7 @@ void getdomaininfo(struct domain *d, struct xen_domctl_getdomaininfo *info);
 void arch_get_domain_info(const struct domain *d,
                           struct xen_domctl_getdomaininfo *info);
 
+domid_t domid_find_with_input_allowed(domid_t hint);
 domid_t get_initial_domain_id(void);
 
 domid_t domid_alloc(domid_t domid);
-- 
2.34.1




^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v3 5/5] xen/console: rename console_rx to console_domid
  2025-05-19 20:12 [PATCH v3 0/5] xen/console: cleanup console input switch logic dmkhn
                   ` (3 preceding siblings ...)
  2025-05-19 20:12 ` [PATCH v3 4/5] xen/console: remove max_init_domid dependency dmkhn
@ 2025-05-19 20:12 ` dmkhn
  4 siblings, 0 replies; 10+ messages in thread
From: dmkhn @ 2025-05-19 20:12 UTC (permalink / raw)
  To: xen-devel
  Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
	roger.pau, sstabellini, dmukhin

From: Denis Mukhin <dmukhin@ford.com>

Update the symbol name to match the code better.

No functional change.

Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
Changes since v2:
- new patch
---
 xen/drivers/char/console.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/xen/drivers/char/console.c b/xen/drivers/char/console.c
index ff20706ac9..afcc6a236e 100644
--- a/xen/drivers/char/console.c
+++ b/xen/drivers/char/console.c
@@ -467,11 +467,11 @@ static void cf_check dump_console_ring_key(unsigned char key)
 #define switch_code (opt_conswitch[0]-'a'+1)
 
 /* Console owner domain identifier. */
-static domid_t __read_mostly console_rx = DOMID_XEN;
+static domid_t __read_mostly console_domid = DOMID_XEN;
 
 static struct domain *console_get_domain(void)
 {
-    struct domain *d = rcu_lock_domain_by_id(console_rx);
+    struct domain *d = rcu_lock_domain_by_id(console_domid);
 
     if ( !d )
         return NULL;
@@ -497,7 +497,7 @@ static void console_set_domid(domid_t domid)
     else
         printk("*** Serial input to DOM%u", domid);
 
-    console_rx = domid;
+    console_domid = domid;
 
     if ( switch_code )
         printk(" (type 'CTRL-%c' three times to switch input)",
@@ -507,7 +507,7 @@ static void console_set_domid(domid_t domid)
 
 domid_t console_get_domid(void)
 {
-    return console_rx;
+    return console_domid;
 }
 
 /*
@@ -518,10 +518,10 @@ static void console_switch_input(void)
 {
     domid_t hint;
 
-    if ( console_rx == DOMID_XEN )
+    if ( console_domid == DOMID_XEN )
         hint = get_initial_domain_id();
     else
-        hint = console_rx + 1;
+        hint = console_domid + 1;
 
     hint = domid_find_with_input_allowed(hint);
 
@@ -533,7 +533,7 @@ static void __serial_rx(char c)
     struct domain *d;
     int rc = 0;
 
-    if ( console_rx == DOMID_XEN )
+    if ( console_domid == DOMID_XEN )
         return handle_keypress(c, false);
 
     d = console_get_domain();
-- 
2.34.1




^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH v3 1/5] xen/console: rename switch_serial_input() to console_switch_input()
  2025-05-19 20:12 ` [PATCH v3 1/5] xen/console: rename switch_serial_input() to console_switch_input() dmkhn
@ 2025-05-20  6:07   ` Jan Beulich
  0 siblings, 0 replies; 10+ messages in thread
From: Jan Beulich @ 2025-05-20  6:07 UTC (permalink / raw)
  To: dmkhn
  Cc: andrew.cooper3, anthony.perard, julien, michal.orzel, roger.pau,
	sstabellini, dmukhin, xen-devel

On 19.05.2025 22:12, dmkhn@proton.me wrote:
> From: Denis Mukhin <dmukhin@ford.com>
> 
> Update the function name as per naming notation in the console driver.
> 
> No functional change.
> 
> Signed-off-by: Denis Mukhin <dmukhin@ford.com>

Acked-by: Jan Beulich <jbeulich@suse.com>



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v3 2/5] xen/console: introduce console_get_domid()
  2025-05-19 20:12 ` [PATCH v3 2/5] xen/console: introduce console_get_domid() dmkhn
@ 2025-05-20  6:20   ` Jan Beulich
  2025-05-22  0:30     ` dmkhn
  0 siblings, 1 reply; 10+ messages in thread
From: Jan Beulich @ 2025-05-20  6:20 UTC (permalink / raw)
  To: dmkhn
  Cc: andrew.cooper3, anthony.perard, julien, michal.orzel, roger.pau,
	sstabellini, dmukhin, xen-devel

On 19.05.2025 22:12, dmkhn@proton.me wrote:
> --- a/xen/arch/arm/vpl011.c
> +++ b/xen/arch/arm/vpl011.c
> @@ -78,12 +78,11 @@ static void vpl011_write_data_xen(struct domain *d, uint8_t data)
>      unsigned long flags;
>      struct vpl011 *vpl011 = &d->arch.vpl011;
>      struct vpl011_xen_backend *intf = vpl011->backend.xen;
> -    struct domain *input = console_get_domain();
>  
>      VPL011_LOCK(d, flags);
>  
>      intf->out[intf->out_prod++] = data;
> -    if ( d == input )
> +    if ( d->domain_id == console_get_domid() )

How do you know d isn't a domain different from the one that was the
console "owner" prior to being destroyed? Original code guaranteed this
up to ...

> @@ -123,8 +122,6 @@ static void vpl011_write_data_xen(struct domain *d, uint8_t data)
>      vpl011_update_interrupt_status(d);
>  
>      VPL011_UNLOCK(d, flags);
> -
> -    console_put_domain(input);

... here.

Jan


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v3 4/5] xen/console: remove max_init_domid dependency
  2025-05-19 20:12 ` [PATCH v3 4/5] xen/console: remove max_init_domid dependency dmkhn
@ 2025-05-20  6:28   ` Jan Beulich
  0 siblings, 0 replies; 10+ messages in thread
From: Jan Beulich @ 2025-05-20  6:28 UTC (permalink / raw)
  To: dmkhn
  Cc: andrew.cooper3, anthony.perard, julien, michal.orzel, roger.pau,
	sstabellini, dmukhin, xen-devel

On 19.05.2025 22:12, dmkhn@proton.me wrote:
> From: Denis Mukhin <dmukhin@ford.com>
> 
> The physical console input rotation depends on max_init_domid symbol, which is
> managed differently across architectures.
> 
> Instead of trying to manage max_init_domid in the arch-common code the console
> input rotation code can be reworked by removing dependency on max_init_domid
> entirely.

... at the expense of doing (worst case) 32k iterations just to find nothing
(else). Iirc it was to avoid this why max_init_domid was introduced.

> --- a/xen/common/domain.c
> +++ b/xen/common/domain.c
> @@ -2460,6 +2460,35 @@ void domid_free(domid_t domid)
>      spin_unlock(&domid_lock);
>  }
>  
> +/*
> + * Find the ID of the next possible console owner domain.
> + *
> + * @return Domain ID: DOMID_XEN or non-system domain IDs within
> + * the range of [0..CONFIG_MAX_DOMID-1].
> + */
> +domid_t domid_find_with_input_allowed(domid_t hint)
> +{
> +    struct domain *d;

const?

> +    domid_t domid = DOMID_XEN;
> +
> +    spin_lock(&domlist_update_lock);

Why this heavy lock? Other functions iterating the list just use the RCU
read lock.

Jan


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v3 2/5] xen/console: introduce console_get_domid()
  2025-05-20  6:20   ` Jan Beulich
@ 2025-05-22  0:30     ` dmkhn
  0 siblings, 0 replies; 10+ messages in thread
From: dmkhn @ 2025-05-22  0:30 UTC (permalink / raw)
  To: Jan Beulich
  Cc: andrew.cooper3, anthony.perard, julien, michal.orzel, roger.pau,
	sstabellini, dmukhin, xen-devel

On Tue, May 20, 2025 at 08:20:16AM +0200, Jan Beulich wrote:
> On 19.05.2025 22:12, dmkhn@proton.me wrote:
> > --- a/xen/arch/arm/vpl011.c
> > +++ b/xen/arch/arm/vpl011.c
> > @@ -78,12 +78,11 @@ static void vpl011_write_data_xen(struct domain *d, uint8_t data)
> >      unsigned long flags;
> >      struct vpl011 *vpl011 = &d->arch.vpl011;
> >      struct vpl011_xen_backend *intf = vpl011->backend.xen;
> > -    struct domain *input = console_get_domain();
> >
> >      VPL011_LOCK(d, flags);
> >
> >      intf->out[intf->out_prod++] = data;
> > -    if ( d == input )
> > +    if ( d->domain_id == console_get_domid() )
> 
> How do you know d isn't a domain different from the one that was the
> console "owner" prior to being destroyed? Original code guaranteed this
> up to ...
> 
> > @@ -123,8 +122,6 @@ static void vpl011_write_data_xen(struct domain *d, uint8_t data)
> >      vpl011_update_interrupt_status(d);
> >
> >      VPL011_UNLOCK(d, flags);
> > -
> > -    console_put_domain(input);
> 
> ... here.

Agreed; I will drop the code change in the next iteration.
Thanks.

> 
> Jan



^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2025-05-22  0:31 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-19 20:12 [PATCH v3 0/5] xen/console: cleanup console input switch logic dmkhn
2025-05-19 20:12 ` [PATCH v3 1/5] xen/console: rename switch_serial_input() to console_switch_input() dmkhn
2025-05-20  6:07   ` Jan Beulich
2025-05-19 20:12 ` [PATCH v3 2/5] xen/console: introduce console_get_domid() dmkhn
2025-05-20  6:20   ` Jan Beulich
2025-05-22  0:30     ` dmkhn
2025-05-19 20:12 ` [PATCH v3 3/5] xen/console: introduce console input permission dmkhn
2025-05-19 20:12 ` [PATCH v3 4/5] xen/console: remove max_init_domid dependency dmkhn
2025-05-20  6:28   ` Jan Beulich
2025-05-19 20:12 ` [PATCH v3 5/5] xen/console: rename console_rx to console_domid dmkhn

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.