Skip to contentSkip to conversation

xen/console: cleanup console input switch logic

PATCH Superseded [email protected] 2 participants 12 messages Mar 31, 2025 – Apr 9, 2025 RISC-V

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover xen/console: cleanup console input switch logic

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 patch series introduces the concept of "console focus", which is defined as the ID of the domain that currently owns the physical console input.

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

The main idea 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 introduces a new domain permission flag to mark ownership of the console input for the console driver.

Patches 2, 3 prepare console driver to allow console input rotation across multiple domains based on the new permission flag. Patch 3 depends on patch 2.

Patches 4, 5 perform mechanical renames to fit the usage in the code.

Patch 6 cleans up the console input switch logic; depends on patches 3, 4, 5.

Patch 7 simplifies the existing vUART code by using newly introduced APIs, depends on patch 5.

[1] https://lore.kernel.org/xen-devel/[email protected]/
[2] Link to CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/1745537898
[3] Link to v1: https://lore.kernel.org/xen-devel/[email protected]/

Denis Mukhin (7):
xen/console: introduce console input permission xen/domain: introduce domid_alloc()
xen/domain: introduce domid_top()
xen/console: rename switch_serial_input() to console_switch_input() xen/console: rename console_rx to console_focus xen/console: introduce console_set_focus()
xen/console: introduce console_get_focus()

 xen/arch/arm/dom0less-build.c      |  15 ++--
 xen/arch/arm/domain_build.c        |  19 +++--
 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/arch/x86/setup.c               |   8 +-
 xen/common/domain.c                |  70 +++++++++++++++++
 xen/common/domctl.c                |  45 +----------
 xen/drivers/char/console.c         | 121 ++++++++++++++++-------------
 xen/include/xen/console.h          |   3 +-
 xen/include/xen/domain.h           |   4 +
 xen/include/xen/sched.h            |   8 +-
 16 files changed, 186 insertions(+), 126 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/7 xen/console: introduce console input permission

From: Denis Mukhin <[email protected]>

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

Update console input switch logic accordingly.

---
Changes since v1:
- dropped change in __serial_rx()

---
 xen/arch/arm/vpl011.c      |  2 ++
 xen/arch/x86/pv/shim.c     |  2 ++
 xen/common/domain.c        |  2 ++
 xen/drivers/char/console.c | 32 +++++++++++++++++++++++++++-----
 xen/include/xen/sched.h    |  8 +++++++-
 5 files changed, 40 insertions(+), 6 deletions(-)
xen/arch/arm/vpl011.c +2 -0
index 66047bf33c..147958eee8 100644
@@ -737,6 +737,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:
xen/arch/x86/pv/shim.c +2 -0
index c506cc0bec..bc2a7dd5fa 100644
@@ -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)
xen/common/domain.c +2 -0
index 585fd726a9..b9f549c617 100644
@@ -827,6 +827,8 @@ struct domain *domain_create(domid_t domid,
 
         old_hwdom = hardware_domain;
         hardware_domain = d;
+
+        d->console.input_allowed = true;
     }
 
     TRACE_TIME(TRC_DOM0_DOM_ADD, d->domain_id);
xen/drivers/char/console.c +27 -5
index c3150fbdb7..b312a1223e 100644
@@ -474,11 +474,26 @@ static unsigned int __read_mostly console_rx = 0;
 
 #define max_console_rx (max_init_domid + 1)
 
+static struct domain *console_get_domain_by_id(domid_t domid)
+{
+    struct domain *d = rcu_lock_domain_by_id(domid);
+
+    if ( !d )
+        return NULL;
+
+    if ( d->console.input_allowed )
+        return d;
+
+    rcu_unlock_domain(d);
+
+    return NULL;
+}
+
 struct domain *console_get_domain(void)
 {
     if ( console_rx == 0 )
             return NULL;
-    return rcu_lock_domain_by_id(console_rx - 1);
+    return console_get_domain_by_id(console_rx - 1);
 }
 
 void console_put_domain(struct domain *d)
@@ -487,6 +502,15 @@ void console_put_domain(struct domain *d)
         rcu_unlock_domain(d);
 }
 
+static bool console_is_input_allowed(domid_t domid)
+{
+    struct domain *d = console_get_domain_by_id(domid);
+
+    console_put_domain(d);
+
+    return !!d;
+}
+
 static void switch_serial_input(void)
 {
     unsigned int next_rx = console_rx;
@@ -498,7 +522,6 @@ static void switch_serial_input(void)
     for ( ; ; )
     {
         domid_t domid;
-        struct domain *d;
 
         if ( next_rx++ >= max_console_rx )
         {
@@ -511,10 +534,9 @@ static void switch_serial_input(void)
             domid = get_initial_domain_id();
         else
             domid = next_rx - 1;
-        d = rcu_lock_domain_by_id(domid);
-        if ( d )
+
+        if ( console_is_input_allowed(domid) )
         {
-            rcu_unlock_domain(d);
             console_rx = next_rx;
             printk("*** Serial input to DOM%u", domid);
             break;
xen/include/xen/sched.h +7 -1
index 559d201e0c..292b1a91f9 100644
@@ -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 own physical console input. */
+        bool input_allowed;
+    } console;
 } __aligned(PAGE_SIZE);
 
 static inline struct page_list_head *page_to_list(
Signature · 1 line
2.34.1
#2 lore raw

PATCH v2 2/7 xen/domain: introduce domid_alloc()

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Move domain ID allocation during domain creation to a dedicated function domid_alloc().

Allocation algorithm:
- If an explicit domain ID is provided, verify its availability and use it if ID is unused;
- Otherwise, perform an exhaustive search for the first available ID within the [0..DOMID_FIRST_RESERVED) range, excluding hardware_domid.

This minimizes the use of max_init_domid in the code and, thus, is a prerequisite change for enabling console input rotation across domains with console input permission on x86 platforms (which currently is limited to dom0, PV shim and Xen).

---
Changes since v1:
- use domid_alloc() during dom0 creation on x86

---
 xen/arch/arm/dom0less-build.c | 15 ++++++------
 xen/arch/arm/domain_build.c   | 19 +++++++++++----
 xen/arch/x86/setup.c          |  8 ++++++-
 xen/common/domain.c           | 45 +++++++++++++++++++++++++++++++++++
 xen/common/domctl.c           | 45 ++++-------------------------------
 xen/include/xen/domain.h      |  3 +++
 6 files changed, 80 insertions(+), 55 deletions(-)
xen/arch/arm/dom0less-build.c +7 -8
index 573b0d25ae..4b9e22039e 100644
@@ -975,14 +975,18 @@ void __init create_domUs(void)
             .grant_opts = XEN_DOMCTL_GRANT_version(opt_gnttab_max_version),
         };
         unsigned int flags = 0U;
+        domid_t domid;
         uint32_t val;
         int rc;
 
         if ( !dt_device_is_compatible(node, "xen,domain") )
             continue;
 
-        if ( (max_init_domid + 1) >= DOMID_FIRST_RESERVED )
-            panic("No more domain IDs available\n");
+        rc = domid_alloc(DOMID_AUTO);
+        if ( rc < 0 )
+            panic("cannot allocate domain ID for domain %s (rc = %d)\n",
+                  dt_node_name(node), rc);
+        domid = rc;
 
         if ( dt_find_property(node, "xen,static-mem", NULL) )
         {
@@ -1107,12 +1111,7 @@ void __init create_domUs(void)
         if ( !llc_coloring_enabled && llc_colors_str )
             panic("'llc-colors' found, but LLC coloring is disabled\n");
 
-        /*
-         * The variable max_init_domid is initialized with zero, so here it's
-         * very important to use the pre-increment operator to call
-         * domain_create() with a domid > 0. (domid == 0 is reserved for Dom0)
-         */
-        d = domain_create(++max_init_domid, &d_cfg, flags);
+        d = domain_create(domid, &d_cfg, flags);
         if ( IS_ERR(d) )
             panic("Error creating domain %s (rc = %ld)\n",
                   dt_node_name(node), PTR_ERR(d));
xen/arch/arm/domain_build.c +14 -5
index 2b5b433183..2d8c2931d6 100644
@@ -2367,8 +2367,15 @@ void __init create_dom0(void)
         .grant_opts = XEN_DOMCTL_GRANT_version(opt_gnttab_max_version),
     };
     unsigned int flags = CDF_privileged;
+    domid_t domid;
     int rc;
 
+    rc = domid_alloc(get_initial_domain_id());
+    if ( rc < 0 )
+        panic("Cannot use domain ID %d (rc = %d)\n",
+              get_initial_domain_id(), rc);
+    domid = rc;
+
     /* The vGIC for DOM0 is exactly emulating the hardware GIC */
     dom0_cfg.arch.gic_version = XEN_DOMCTL_CONFIG_GIC_NATIVE;
     dom0_cfg.arch.nr_spis = VGIC_DEF_NR_SPIS;
@@ -2391,19 +2398,21 @@ void __init create_dom0(void)
     if ( !llc_coloring_enabled )
         flags |= CDF_directmap;
 
-    dom0 = domain_create(0, &dom0_cfg, flags);
+    dom0 = domain_create(domid, &dom0_cfg, flags);
     if ( IS_ERR(dom0) )
-        panic("Error creating domain 0 (rc = %ld)\n", PTR_ERR(dom0));
+        panic("Error creating domain %d (rc = %ld)\n", domid, PTR_ERR(dom0));
 
     if ( llc_coloring_enabled && (rc = dom0_set_llc_colors(dom0)) )
-        panic("Error initializing LLC coloring for domain 0 (rc = %d)\n", rc);
+        panic("Error initializing LLC coloring for domain %d (rc = %d)\n",
+              domid, rc);
 
     if ( alloc_dom0_vcpu0(dom0) == NULL )
-        panic("Error creating domain 0 vcpu0\n");
+        panic("Error creating domain %d vcpu0\n", domid);
 
     rc = construct_dom0(dom0);
     if ( rc )
-        panic("Could not set up DOM0 guest OS (rc = %d)\n", rc);
+        panic("Could not set up guest OS for domain %d (rc = %d)\n",
+              domid, rc);
 }
 
 /*
xen/arch/x86/setup.c +7 -1
index d70abb7e0c..ad349528ea 100644
@@ -996,6 +996,7 @@ static struct domain *__init create_dom0(struct boot_info *bi)
     domid_t domid;
     struct boot_module *image;
     unsigned int idx;
+    int rc;
 
     idx = first_boot_module_index(bi, BOOTMOD_KERNEL);
     if ( idx >= bi->nr_modules )
@@ -1003,6 +1004,12 @@ static struct domain *__init create_dom0(struct boot_info *bi)
 
     image = &bi->mods[idx];
 
+    rc = domid_alloc(get_initial_domain_id());
+    if ( rc < 0 )
+        panic("Cannot use domain ID %d (rc = %d)\n",
+              get_initial_domain_id(), rc);
+    domid = rc;
+
     if ( opt_dom0_pvh )
     {
         dom0_cfg.flags |= (XEN_DOMCTL_CDF_hvm |
@@ -1017,7 +1024,6 @@ static struct domain *__init create_dom0(struct boot_info *bi)
         dom0_cfg.flags |= XEN_DOMCTL_CDF_iommu;
 
     /* Create initial domain.  Not d0 for pvshim. */
-    domid = get_initial_domain_id();
     d = domain_create(domid, &dom0_cfg, pv_shim ? 0 : CDF_privileged);
     if ( IS_ERR(d) )
         panic("Error creating d%u: %ld\n", domid, PTR_ERR(d));
xen/common/domain.c +45 -0

51 lines not on this page · raw message

xen/common/domctl.c +4 -41

58 lines not on this page · raw message

xen/include/xen/domain.h +3 -0
index 83069de501..9b7159a743 100644
@@ -37,6 +37,9 @@ void arch_get_domain_info(const struct domain *d,
 
 domid_t get_initial_domain_id(void);
 
+#define DOMID_AUTO               (-1)
+int domid_alloc(int hint);
+
 /* CDF_* constant. Internal flags for domain creation. */
 /* Is this a privileged domain? */
 #define CDF_privileged           (1U << 0)
Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH v2 2/7] xen/domain: introduce domid_alloc()

Quoted 11 lines · On 01.04.2025 01:05, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Move domain ID allocation during domain creation to a dedicated function domid_alloc().

Allocation algorithm:
- If an explicit domain ID is provided, verify its availability and use it if ID is unused;
- Otherwise, perform an exhaustive search for the first available ID within the [0..DOMID_FIRST_RESERVED) range, excluding hardware_domid.

Quoted 4 lines

This minimizes the use of max_init_domid in the code and, thus, is a prerequisite change for enabling console input rotation across domains with console input permission on x86 platforms (which currently is limited to dom0, PV shim and Xen).

By removing the updating of max_init_domid you do - afaict - break the remaining use site(s) of the variable.

xen/arch/x86/setup.c static struct domain *__init create_dom0(struct boot_info *bi) · line 1015

Quoted 13 lines
xen/arch/x86/setup.c +6 -0
@@ -1003,6 +1004,12 @@ static struct domain *__init create_dom0(struct boot_info *bi)
 
     image = &bi->mods[idx];
 
+    rc = domid_alloc(get_initial_domain_id());
+    if ( rc < 0 )
+        panic("Cannot use domain ID %d (rc = %d)\n",
+              get_initial_domain_id(), rc);
+    domid = rc;
+
     if ( opt_dom0_pvh )
     {
         dom0_cfg.flags |= (XEN_DOMCTL_CDF_hvm |

Why does this need to move up, ...

xen/arch/x86/setup.c static struct domain *__init create_dom0(struct boot_info *bi) · line 1020

Quoted 5 lines
xen/arch/x86/setup.c +0 -1
@@ -1017,7 +1024,6 @@ static struct domain *__init create_dom0(struct boot_info *bi)
         dom0_cfg.flags |= XEN_DOMCTL_CDF_iommu;
 
     /* Create initial domain.  Not d0 for pvshim. */
-    domid = get_initial_domain_id();

... disconnecting the logic from the comment that is relevant there, ...

xen/arch/x86/setup.c static struct domain *__init create_dom0(struct boot_info *bi) · line 1027

Quoted 1 line
xen/arch/x86/setup.c +0 -0
     d = domain_create(domid, &dom0_cfg, pv_shim ? 0 : CDF_privileged);

... and not so much here?

xen/include/xen/domain.h void arch_get_domain_info(const struct domain *d, · line 41

Quoted 8 lines
xen/include/xen/domain.h +2 -0
@@ -37,6 +37,9 @@ void arch_get_domain_info(const struct domain *d,
 
 domid_t get_initial_domain_id(void);
 
+#define DOMID_AUTO               (-1)
+int domid_alloc(int hint);

Imo it would be better to use e.g. DOMID_INVALID as the "give me whatever is available" indicator, allowing the function parameter to properly be domid_t.

But first of all - can we please take a step back and re-evaluate whether all of this re-arrangement that you're doing (not just in the patch here) is really needed? It seems to me that it must be possible to do whatever you ultimately want to do without re-writing quite a few pretty central pieces that have been serving us fine for a long time. That is, rather than make our code fit your desires, make your plans fit within the code base we have.

Jan

#4 lore raw

Re: [PATCH v2 2/7] xen/domain: introduce domid_alloc()

Quoted 15 lines · Jan Beulich
Quoted 11 lines · On 01.04.2025 01:05, [email protected] wrote:

From: Denis Mukhin [email protected]

Move domain ID allocation during domain creation to a dedicated function domid_alloc().

Allocation algorithm:
- If an explicit domain ID is provided, verify its availability and use it if ID is unused;
- Otherwise, perform an exhaustive search for the first available ID within the [0..DOMID_FIRST_RESERVED) range, excluding hardware_domid.

Quoted 8 lines
Quoted 4 lines

This minimizes the use of max_init_domid in the code and, thus, is a prerequisite change for enabling console input rotation across domains with console input permission on x86 platforms (which currently is limited to dom0, PV shim and Xen).

By removing the updating of max_init_domid you do - afaict - break the remaining use site(s) of the variable.

Unfortunately, this patch should go together with the next patch in this series:
xen/domain: introduce domid_top()

I mentioned dependency in the cover letter.

Quoted 27 lines
Quoted 13 lines
xen/arch/x86/setup.c +6 -0
@@ -1003,6 +1004,12 @@ static struct domain *__init create_dom0(struct boot_info *bi)
 
 image = &bi->mods[idx];
 
+ rc = domid_alloc(get_initial_domain_id());
+ if ( rc < 0 )
+ panic("Cannot use domain ID %d (rc = %d)\n",
+ get_initial_domain_id(), rc);
+ domid = rc;
+
 if ( opt_dom0_pvh )
 {
Quoted 1 line

dom0_cfg.flags |= (XEN_DOMCTL_CDF_hvm |

Why does this need to move up, ...

xen/arch/x86/setup.c static struct domain *__init create_dom0(struct boot_info *bi) · line 1020

Quoted 5 lines
xen/arch/x86/setup.c +0 -1
@@ -1017,7 +1024,6 @@ static struct domain *__init create_dom0(struct boot_info *bi)
 dom0_cfg.flags |= XEN_DOMCTL_CDF_iommu;
 
 /* Create initial domain. Not d0 for pvshim. */
- domid = get_initial_domain_id();

... disconnecting the logic from the comment that is relevant there, ...

Quoted 4 lines

xen/arch/x86/setup.c static struct domain *__init create_dom0(struct boot_info *bi) · line 1027

Quoted 1 line
xen/arch/x86/setup.c +0 -0
 d = domain_create(domid, &dom0_cfg, pv_shim ? 0 : CDF_privileged);

... and not so much here?

So that in case domid_alloc() fails, create_dom0() will reach error state quickly. I will re-group it as suggested.

Quoted 7 lines
Quoted 7 lines
xen/include/xen/domain.h +0 -0
@@ -37,6 +37,9 @@ void arch_get_domain_info(const struct domain *d,
 
 domid_t get_initial_domain_id(void);
 
Quoted 7 lines

xen/include/xen/domain.h void arch_get_domain_info(const struct domain *d, · line 41

Quoted 2 lines
xen/include/xen/domain.h +2 -0
+#define DOMID_AUTO (-1)
+int domid_alloc(int hint);

Imo it would be better to use e.g. DOMID_INVALID as the "give me whatever is available" indicator, allowing the function parameter to properly be domid_t.

Ack.

Quoted 8 lines

But first of all - can we please take a step back and re-evaluate whether all of this re-arrangement that you're doing (not just in the patch here) is really needed? It seems to me that it must be possible to do whatever you ultimately want to do without re-writing quite a few pretty central pieces that have been serving us fine for a long time. That is, rather than make our code fit your desires, make your plans fit within the code base we have.

Thanks for the review and feedback!

My plan is to deliver the NS16550 emulator for x86 because that saves some time during bring up of an embedded system.

Current xen console driver allows physical input pass-through to the domain.

This is implemented differently between arm and x86. Currently, the logic of switching input depends on the global variable max_init_domid, which is advanced on arm only (and never decreased, which makes sense for embedded designs).

One of the planned emulator features is allowing guest OS to accept physical input over the emulated UART. On x86, that includes dom0, PV shim and, with emulator in place, also includes all domains with NS16550 emulator enabled.

So I need to solve the problem of switching console input to another domain on x86.

I see there are two ways to solve it:

(a) iterate through the list of known domains and check "UART emulator present" property. AFAIU, that will require maintaining "current console domain" pointer for a domain list walk.

(b) calculate the max known domain ID and then check whether domain ID corresponds to a domain with "UART emulator present" property by iterating through the range of domain IDs. This is current approach in the code.

So, this patch series solves problem of switching console input to another domain on x86 using (b); as looked as less changes to me comparing to maintaining a domain pointer.

re: code re-arrangements: while working on the emulator, getting more feedback, this and other console-related cleanup series appeared and I think the code which served fine for a long time, got a bit cleaner, easier to follow and, I hope, a bit easier to maintain.

Quoted 2 lines

Jan

Thanks,
Denis

#5 lore raw

Re: [PATCH v2 2/7] xen/domain: introduce domid_alloc()

Quoted 20 lines · Denis Mukhin
Quoted 19 lines · Jan Beulich
Quoted 15 lines · On 01.04.2025 01:05, [email protected] wrote:

From: Denis Mukhin [email protected]

Move domain ID allocation during domain creation to a dedicated function domid_alloc().

Allocation algorithm:
- If an explicit domain ID is provided, verify its availability and use it if ID is unused;
- Otherwise, perform an exhaustive search for the first available ID within the [0..DOMID_FIRST_RESERVED) range, excluding hardware_domid.

This minimizes the use of max_init_domid in the code and, thus, is a prerequisite change for enabling console input rotation across domains with console input permission on x86 platforms (which currently is limited to dom0, PV shim and Xen).

Quoted 8 lines
Quoted 2 lines

By removing the updating of max_init_domid you do - afaict - break the remaining use site(s) of the variable.

Unfortunately, this patch should go together with the next patch in this series:
xen/domain: introduce domid_top()

I mentioned dependency in the cover letter.

No, that would still break bisectability. Also such dependencies would better be mentioned in both the cover letter and the patch(es).

Going back to the cover letter, all I find is "Patch 3 depends on patch 2." That's the natural thing in a series.

Jan

#6 lore raw

PATCH v2 3/7 xen/domain: introduce domid_top()

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Introduce domid_top() in architecture-independent location to retrieve the highest non-system domain ID for use in console input switch logic.

Replace max_init_domid with domid_top(), which obsoletes max_init_domid in the code base.

---
Changes since v1:
- promote domid_top to a function which walks through the list of domains to identify the highest non-system domain ID which should be considered for console input rotation
- elimitate use of max_init_domid

---
 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/domain.c                | 23 +++++++++++++++++++++++
 xen/drivers/char/console.c         |  2 +-
 xen/include/xen/domain.h           |  1 +
 8 files changed, 25 insertions(+), 11 deletions(-)
xen/arch/arm/include/asm/setup.h +0 -2
index 6cf272c160..f107e8eebb 100644
@@ -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);
xen/arch/arm/setup.c +0 -2
index bf39d41e9b..143808f592 100644
@@ -60,8 +60,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;
xen/arch/ppc/include/asm/setup.h +0 -2
index e4f64879b6..956fa6985a 100644
@@ -1,6 +1,4 @@
 #ifndef __ASM_PPC_SETUP_H__
 #define __ASM_PPC_SETUP_H__
 
-#define max_init_domid (0)
-
 #endif /* __ASM_PPC_SETUP_H__ */
xen/arch/riscv/include/asm/setup.h +0 -2
index c9d69cdf51..d1fc64b673 100644
@@ -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);
xen/arch/x86/include/asm/setup.h +0 -2
index 5c2391a868..296348655b 100644
@@ -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
xen/common/domain.c +23 -0
index b07d70a7e3..8e8e784b46 100644
@@ -111,6 +111,29 @@ int domid_alloc(int hint)
     return domid;
 }
 
+/*
+ * Retrieve the highest known non-system domain ID.
+ */
+domid_t domid_top(void)
+{
+    struct domain *d;
+    domid_t i = 0;
+
+    spin_lock(&domlist_update_lock);
+
+    for ( d = domain_list;
+          d && (d->domain_id < DOMID_FIRST_RESERVED);
+          d = d->next_in_list )
+    {
+        if ( i < d->domain_id )
+            i = d->domain_id;
+    }
+
+    spin_unlock(&domlist_update_lock);
+
+    return i;
+}
+
 /*
  * Insert a domain into the domlist/hash.  This allows the domain to be looked
  * up by domid, and therefore to be the subject of hypercalls/etc.
xen/drivers/char/console.c +1 -1
index b312a1223e..54163e93fb 100644
@@ -472,7 +472,7 @@ static void cf_check dump_console_ring_key(unsigned char key)
  */
 static unsigned int __read_mostly console_rx = 0;
 
-#define max_console_rx (max_init_domid + 1)
+#define max_console_rx (domid_top() + 1)
 
 static struct domain *console_get_domain_by_id(domid_t domid)
 {
xen/include/xen/domain.h +1 -0
index 9b7159a743..c3dda8edf2 100644
@@ -35,6 +35,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_top(void);
 domid_t get_initial_domain_id(void);
 
 #define DOMID_AUTO               (-1)
Signature · 1 line
2.34.1
#7 lore raw

Re: [PATCH v2 3/7] xen/domain: introduce domid_top()

Quoted 8 lines · On 01.04.2025 01:05, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Introduce domid_top() in architecture-independent location to retrieve the highest non-system domain ID for use in console input switch logic.

Replace max_init_domid with domid_top(), which obsoletes max_init_domid in the code base.

Quoted 7 lines

---
Changes since v1:
- promote domid_top to a function which walks through the list of domains to identify the highest non-system domain ID which should be considered for console input rotation
- elimitate use of max_init_domid

I still don't get how this is functionally identical to what we had so far. In principle imo this change should come with a "No functional change" statement. Provided of course that's true in the first place.

xen/common/domain.c int domid_alloc(int hint) · line 119

Quoted 12 lines
xen/common/domain.c +6 -0
@@ -111,6 +111,29 @@ int domid_alloc(int hint)
     return domid;
 }
 
+/*
+ * Retrieve the highest known non-system domain ID.
+ */
+domid_t domid_top(void)
+{
+    struct domain *d;

const?

xen/common/domain.c int domid_alloc(int hint) · line 120

Quoted 1 line
xen/common/domain.c +1 -0
+    domid_t i = 0;

i is a pretty generic name for something of type domid_t.

xen/common/domain.c int domid_alloc(int hint) · line 125

Quoted 5 lines
xen/common/domain.c +5 -0
+
+    spin_lock(&domlist_update_lock);
+
+    for ( d = domain_list;
+          d && (d->domain_id < DOMID_FIRST_RESERVED);

May I ask which domains with ID >= DOMID_FIRST_RESERVED end up in this list?

xen/common/domain.c int domid_alloc(int hint) · line 129

Quoted 4 lines
xen/common/domain.c +4 -0
+          d = d->next_in_list )
+    {
+        if ( i < d->domain_id )
+            i = d->domain_id;

See the comment in domlist_insert() as to sorting of that list.

xen/drivers/char/console.c static void cf_check dump_console_ring_key(unsigned char key) · line 475

Quoted 8 lines
xen/drivers/char/console.c +1 -1
@@ -472,7 +472,7 @@ static void cf_check dump_console_ring_key(unsigned char key)
  */
 static unsigned int __read_mostly console_rx = 0;
 
-#define max_console_rx (max_init_domid + 1)
+#define max_console_rx (domid_top() + 1)

This is becoming quite a bit more expensive now, without it becoming clear whether it's really worth paying that price.

Jan

#8 lore raw

PATCH v2 4/7 xen/console: rename switch_serial_input() to console_switch_input()

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Update the name to emphasize the physical console input switch to a new owner domain following the naming notation in the console driver.

No functional change.

---
Changes since v1:
- rename console_switch_focus() to console_switch_input()

---
 xen/drivers/char/console.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
xen/drivers/char/console.c +3 -3
index 54163e93fb..48da4e5838 100644
@@ -511,7 +511,7 @@ static bool console_is_input_allowed(domid_t domid)
     return !!d;
 }
 
-static void switch_serial_input(void)
+static void console_switch_input(void)
 {
     unsigned int next_rx = console_rx;
 
@@ -603,7 +603,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;
@@ -1147,7 +1147,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)
Signature · 1 line
2.34.1
#9 lore raw

PATCH v2 5/7 xen/console: rename console_rx to console_focus

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Update the symbol name in preparation for the semantic change to the physical console input owner domain identifier.

No functional change.

---
Changes since v1:
- keep the original console_focus description

---
 xen/drivers/char/console.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
xen/drivers/char/console.c +8 -8
index 48da4e5838..42974c0af7 100644
@@ -470,7 +470,7 @@ static void cf_check dump_console_ring_key(unsigned char key)
  * 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;
+static unsigned int __read_mostly console_focus = 0;
 
 #define max_console_rx (domid_top() + 1)
 
@@ -491,9 +491,9 @@ static struct domain *console_get_domain_by_id(domid_t domid)
 
 struct domain *console_get_domain(void)
 {
-    if ( console_rx == 0 )
+    if ( console_focus == 0 )
             return NULL;
-    return console_get_domain_by_id(console_rx - 1);
+    return console_get_domain_by_id(console_focus - 1);
 }
 
 void console_put_domain(struct domain *d)
@@ -513,7 +513,7 @@ static bool console_is_input_allowed(domid_t domid)
 
 static void console_switch_input(void)
 {
-    unsigned int next_rx = console_rx;
+    unsigned int next_rx = console_focus;
 
     /*
      * Rotate among Xen, dom0 and boot-time created domUs while skipping
@@ -525,7 +525,7 @@ static void console_switch_input(void)
 
         if ( next_rx++ >= max_console_rx )
         {
-            console_rx = 0;
+            console_focus = 0;
             printk("*** Serial input to Xen");
             break;
         }
@@ -537,7 +537,7 @@ static void console_switch_input(void)
 
         if ( console_is_input_allowed(domid) )
         {
-            console_rx = next_rx;
+            console_focus = next_rx;
             printk("*** Serial input to DOM%u", domid);
             break;
         }
@@ -554,7 +554,7 @@ static void __serial_rx(char c)
     struct domain *d;
     int rc = 0;
 
-    if ( console_rx == 0 )
+    if ( console_focus == 0 )
         return handle_keypress(c, false);
 
     d = console_get_domain();
@@ -1135,7 +1135,7 @@ void __init console_endboot(void)
      * a useful 'how to switch' message.
      */
     if ( opt_conswitch[1] == 'x' )
-        console_rx = max_console_rx;
+        console_focus = max_console_rx;
 
     register_keyhandler('w', dump_console_ring_key,
                         "synchronously dump console ring buffer (dmesg)", 0);
Signature · 1 line
2.34.1
#10 lore raw

PATCH v2 6/7 xen/console: introduce console_set_focus()

In reply to [email protected]

From: Denis Mukhin <[email protected]>

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.

---
Changes since v1:
- update console_focus description

---
 xen/drivers/char/console.c | 88 ++++++++++++++++----------------------
 1 file changed, 37 insertions(+), 51 deletions(-)
xen/drivers/char/console.c +37 -51
index 42974c0af7..9905ffd6b9 100644
@@ -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)
Signature · 1 line
2.34.1
#11 lore raw

PATCH v2 7/7 xen/console: introduce console_get_focus()

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Add console_get_focus() as a console public API to the retrieve current console owner domain ID.

Make console_{get,put}_domain() private and simplify vpl011 code a bit.

---
 xen/arch/arm/vpl011.c      | 5 +----
 xen/drivers/char/console.c | 9 +++++++--
 xen/include/xen/console.h  | 3 +--
 3 files changed, 9 insertions(+), 8 deletions(-)
xen/arch/arm/vpl011.c +1 -4
index 147958eee8..9ce6151c2a 100644
@@ -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_focus() )
     {
         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);
 }
 
 /*
xen/drivers/char/console.c +7 -2
index 9905ffd6b9..888dabe8af 100644
@@ -484,12 +484,12 @@ static struct domain *console_get_domain_by_id(domid_t domid)
     return NULL;
 }
 
-struct domain *console_get_domain(void)
+static struct domain *console_get_domain(void)
 {
     return console_get_domain_by_id(console_focus);
 }
 
-void console_put_domain(struct domain *d)
+static void console_put_domain(struct domain *d)
 {
     if ( d )
         rcu_unlock_domain(d);
@@ -526,6 +526,11 @@ static bool cf_check console_set_focus(domid_t domid)
     return true;
 }
 
+domid_t console_get_focus(void)
+{
+    return console_focus;
+}
+
 /*
  * Switch console focus.
  * Rotates input focus among Xen and domains with console input permission.
xen/include/xen/console.h +1 -2
index 83cbc9fbda..19da2b755c 100644
@@ -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_focus(void);
 
 /*
  * Steal output from the console. Returns +ve identifier, else -ve error.
Signature · 1 line
2.34.1