* [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator
@ 2026-11-11 19:47 dmukhin
2026-11-11 19:47 ` [PATCH v8 01/26] emul/vuart: introduce framework for UART emulators dmukhin
` (26 more replies)
0 siblings, 27 replies; 39+ messages in thread
From: dmukhin @ 2026-11-11 19:47 UTC (permalink / raw)
To: xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin, Denis Mukhin
x86 port of Xen lacks vUART facility similar to Arm's vpl011 to support x86
guest OS bring up in the embedded setups.
This patch series introduces initial in-hypervisor emulator for
NS8250/NS16x50-compatible UARTs under CONFIG_VUART_NS16X50.
In parallel domain creation scenario (hyperlaunch), NS16550 emulator helps
early guest firmware and OS bringup debugging, because it eliminates
dependency on the external emulator (qemu) being operational by the time
domains are created.
The emulator also allows to forward the physical console input to the x86
domain which is useful when a system has only one physical UART for early
debugging and this UART is owned by Xen.
By default, CONFIG_VUART_NS16X50 enables emulation of NS16550 at I/O port
0x3f8, IRQ#4 in guest OS (legacy COM1). Legacy COM resources can be selected
at run-time via temporary and undocumented switch 'vuart='.
CONFIG_VUART_NS16X50_DEBUG enables some extra debugging facilities useful
for NS16550 emulator development/debugging (disabled by default).
The NS16550 emulator is disabled in default x86 configuration and goes under
CONFIG_EXPERT in Kconfig.
Note, there's a follow on work for RISC-V which is based on that series:
https://gitlab.com/xen-project/people/olkur/xen/-/tree/riscv-vuart-support-wip?ref_type=heads
Limitations
===========
- Only x86;
- Only legacy COM resources, custom I/O ports/IRQs are not supported;
- Only Xen console as a backend, no inter-domain communication (similar to
vpl011 on Arm);
- Only 8n1 emulation (8-bit data, no parity, 1 stop bit);
- No toolstack integration;
- No baud rate emulation (reports 115200 baud to the guest OS);
- No FIFO-less mode emulation;
- No RX FIFO interrupt moderation (FCR) emulation;
- No integration w/ VM snapshotting (HVM_REGISTER_SAVE_RESTORE() and
friends);
- No MMIO-based UART emulation.
Series
======
Patch 1 introduces the new vUART framework, that is the code originally
posted here:
https://lore.kernel.org/xen-devel/20250624035443.344099-16-dmukhin@ford.com/
Required for emulator.
Patch 2 adds missing NS16550 definitions, required for emulator.
Patches 3-4 introduces the basic emulator skeleton - state machine
initialization stubs, I/O port handler stub, logging, etc.
Patches 5-12 incrementally populate the minimal NS16550 register emulation.
Patch 13 hooks vUART state debugging (disabled by default).
Patch 14-16 adds Kconfig for enabling minimal NS16550 UART (disabled by default).
Patches 17-21 introduce necessary changes to enable NS16550 on dom0 (and PVH).
Patches 22-23 documentation updates. Note, that vuart= setting is not
documented since it will likely change with hyperlanch landing on mainline.
Patches 24-26 tests updates.
Link to CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/3055921295
Link to XTF branch: https://gitlab.com/xen-project/people/dmukhin/xtf/-/commits/emul-16x50
Link to Xen branch: https://gitlab.com/xen-project/people/dmukhin/xen/-/tree/vuart-ns8250-v8?ref_type=heads
Testing
=======
```shell
echo CONFIG_EXPERT=y >> .config
echo CONFIG_VUART_NS16X50=y >> .config
make olddefconfig
```
To test w/ virtual COM2, the guest kernel parameters should contain
something like the following:
earlycon=uart,io,0x2f8,115200n8 console=uart,io,0x2f8,115200n8
Xen command line should have:
vuart=<DOMID>:ns16550,com2
HVM
---
Tested only boot of HVM linux guest with OVMF as the virtual firmware.
SeaBIOS as a virtual firmware is not tested.
Also, HVM is tested via new XTF test:
https://gitlab.com/xen-project/people/dmukhin/xtf/-/commits/emul-16x50
PVH dom0 or domU
----------------
Xen is able to forward physical console input to the domain with virtual
NS16550. To switch the console focus press Ctrl+aaa.
Console switch is limited on x86 to dom0 and Xen.
It is possible to run QEMU-based dom0 + domU test:
- dom0 with emulator:
./automation/scripts/qemu-alpine-x86_64.sh --dom0-type=pvh --dom0-console=ttyS1
- domU with emulator:
./automation/scripts/qemu-alpine-x86_64.sh --dom0-type=pvh --domU-type=pvh --domU-console=ttyS1
Changes since v7:
- Addressed feedback from v7
- Added QEMU tests for PVH guests
- Added XTF tests
- Added initial set of vUART requirements
- Link to v7: https://lore.kernel.org/xen-devel/20250908211149.279143-1-dmukhin@ford.com/
Changes since v6:
- Addressed feedback from v6, mainly DLL, THRE interrupt reason handling and
fixups in vuart.c and ns16x50.c. Also plumbed new temporary swicth 'vuart='
for ease of testing (CI unit test jobs are pending).
- Link to v6: https://lore.kernel.org/xen-devel/20250905232715.440758-1-dmukhin@ford.com/
Changes since v5:
- Split THR/RBR into two separate patches.
- Addressed feedback from v5.
- Link to v5: https://lore.kernel.org/xen-devel/20250828235409.2835815-1-dmukhin@ford.com/
Changes since v4:
- Split the series to make it simpler to review.
- Addressed feedback from v4.
- Dropped xl changes, which I will submit separately.
- Link to v4: https://lore.kernel.org/xen-devel/20250731192130.3948419-1-dmukhin@ford.com/
Changes since v3:
- Reduced the blast radius of the series, thanks to reviews, individual
aspects (like console focus) touched in v3 moved to separate threads.
- Kept the UART emulator framework since I need to redo some of emulator code
and there's more-or-less agreement on it (where to place, naming, scope).
- Applied the feedback from
https://lore.kernel.org/xen-devel/20250624035443.344099-1-dmukhin@ford.com/
- Link to v3: https://lore.kernel.org/xen-devel/20250103-vuart-ns8250-v3-v1-0-c5d36b31d66c@ford.com/
Changes since v2:
- renamed emulator s/NS8250/NS16550/g
- reduced the patch series after addressing v2 feedback
- introduced driver framework for UART emulators
- unified guest OS printouts across all available UART emulators
- Link to v2: https://lore.kernel.org/xen-devel/20241205-vuart-ns8250-v1-0-e9aa923127eb@ford.com/
Changes since v1:
- dropped kmalloc/kfree aliases
- fixed ECLAIR jobs (thanks Andrew Cooper)
- addressed console forwarding on arm32 and arm64 (thanks to Luca Fancellu)
- moved NS8250 debugging stubs into its own patch
- added fix for https://gitlab.com/xen-project/xen/-/issues/184
- Link to v1: https://lore.kernel.org/r/20241126-vuart-ns8250-v1-v1-0-87b9a8375b7a@ford.com
Ayan Kumar Halder (1):
docs/fusa: add NS16x50 UART requirements for x86
Denis Mukhin (25):
emul/vuart: introduce framework for UART emulators
xen/8250-uart: update definitions
x86: fixup dependencies for asm/x86_emulate.h
emul/ns16x50: implement emulator stub
emul/ns16x50: implement DLL/DLM registers
emul/ns16x50: implement SCR register
emul/ns16x50: implement IER/IIR registers
emul/ns16x50: implement LCR/LSR registers
emul/ns16x50: implement MCR/MSR registers
emul/ns16x50: implement RBR register
emul/ns16x50: implement THR register
emul/ns16x50: implement FCR register (write-only)
emul/ns16550: implement dump_state() hook
emul/vuart: add global UART emulator configuration knob
emul/ns16x50: enable emulator
emul/ns16x50: add Kconfig options
x86/domain: enable per-domain I/O port bitmaps
emul/ns16x50: resolve physical I/O port sharing
xen/domain: allocate d->irq_caps before arch-specific initialization
emul/ns16x50: resolve physical IRQ sharing
emul/ns16x50: implement IRQ emulation via vIOAPIC
MAINTAINERS: add myself as vUART framework maintainer
ci: add initial NS16550-compatible XTF test
ci: add smoke NS16550-compatible PVH Linux test
ci: do not merge: test
MAINTAINERS | 15 +
automation/gitlab-ci/build.yaml | 2 +
automation/gitlab-ci/test.yaml | 37 +-
automation/scripts/include/xtf-runner | 2 +
automation/scripts/include/xtf-x86_64 | 4 +-
automation/scripts/qemu-alpine-x86_64.sh | 325 ++++--
docs/fusa/reqs/design-reqs/x86/ns16x50.rst | 386 +++++++
docs/fusa/reqs/index.rst | 2 +
docs/fusa/reqs/product-reqs/x86/reqs.rst | 24 +
xen/arch/arm/xen.lds.S | 1 +
xen/arch/ppc/xen.lds.S | 1 +
xen/arch/riscv/xen.lds.S | 1 +
xen/arch/x86/Makefile | 1 +
xen/arch/x86/dom0_build.c | 112 +-
xen/arch/x86/hvm/dom0_build.c | 7 +
xen/arch/x86/hvm/hvm.c | 49 +-
xen/arch/x86/hvm/nestedhvm.c | 8 +-
xen/arch/x86/hvm/quirks.c | 3 -
xen/arch/x86/hvm/svm/nestedsvm.c | 2 +-
xen/arch/x86/hvm/vioapic.c | 10 +
xen/arch/x86/hvm/vmx/vvmx.c | 4 +-
xen/arch/x86/include/asm/hvm/nestedhvm.h | 3 +-
xen/arch/x86/include/asm/hvm/support.h | 2 -
xen/arch/x86/include/asm/iocap.h | 3 +
xen/arch/x86/include/asm/irq.h | 8 +
xen/arch/x86/include/asm/x86_emulate.h | 1 +
xen/arch/x86/ioport.c | 168 +++
xen/arch/x86/irq.c | 10 +-
xen/arch/x86/pv/dom0_build.c | 7 +
xen/arch/x86/xen.lds.S | 1 +
xen/common/Kconfig | 2 +
xen/common/Makefile | 1 +
xen/common/domain.c | 8 +-
xen/common/emul/Kconfig | 6 +
xen/common/emul/Makefile | 1 +
xen/common/emul/vuart/Kconfig | 26 +
xen/common/emul/vuart/Makefile | 2 +
xen/common/emul/vuart/ns165x50.c | 0
xen/common/emul/vuart/ns16x50.c | 1135 ++++++++++++++++++++
xen/common/emul/vuart/vuart.c | 266 +++++
xen/common/keyhandler.c | 3 +
xen/drivers/char/console.c | 6 +-
xen/drivers/char/ns16550.c | 16 +-
xen/drivers/passthrough/x86/hvm.c | 11 +-
xen/include/xen/8250-uart.h | 30 +-
xen/include/xen/sched.h | 5 +
xen/include/xen/serial.h | 5 +
xen/include/xen/vuart.h | 121 +++
xen/include/xen/xen.lds.h | 10 +
49 files changed, 2612 insertions(+), 241 deletions(-)
create mode 100644 docs/fusa/reqs/design-reqs/x86/ns16x50.rst
create mode 100644 docs/fusa/reqs/product-reqs/x86/reqs.rst
create mode 100644 xen/arch/x86/ioport.c
create mode 100644 xen/common/emul/Kconfig
create mode 100644 xen/common/emul/Makefile
create mode 100644 xen/common/emul/vuart/Kconfig
create mode 100644 xen/common/emul/vuart/Makefile
create mode 100644 xen/common/emul/vuart/ns165x50.c
create mode 100644 xen/common/emul/vuart/ns16x50.c
create mode 100644 xen/common/emul/vuart/vuart.c
create mode 100644 xen/include/xen/vuart.h
--
2.56.0
^ permalink raw reply [flat|nested] 39+ messages in thread
* [PATCH v8 01/26] emul/vuart: introduce framework for UART emulators
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
@ 2026-11-11 19:47 ` dmukhin
2026-11-12 15:35 ` Oleksii Kurochko
2026-11-11 19:47 ` [PATCH v8 02/26] xen/8250-uart: update definitions dmukhin
` (25 subsequent siblings)
26 siblings, 1 reply; 39+ messages in thread
From: dmukhin @ 2026-11-11 19:47 UTC (permalink / raw)
To: xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin, Denis Mukhin
From: Denis Mukhin <dmukhin@ford.com>
Introduce a driver framework to abstract UART emulators in the hypervisor.
That allows for architecture-independent handling of virtual UARTs in
the console driver and simplifies enabling new UART emulators.
The framework is built under CONFIG_VUART_FRAMEWORK, which will be
automatically enabled once the user enables any UART emulator.
Current implementation supports maximum of one vUART of each kind per
domain.
Use new domain_has_vuart() in the console driver code to check whether
to forward console input to the domain using vUART.
Enable console forwarding over vUART for hardware domains with a vUART.
That enables console forwarding to dom0 on x86, since console can be
forwarded only to Xen, dom0 and pvshim on x86 as of now.
Note: existing vUARTs are deliberately *not* hooked to the new framework
to minimize the scope of the patch: vpl011 (i.e. SBSA) emulator and
"vuart" (i.e. minimalistic MMIO-mapped dtuart for hwdoms on Arm) are
kept unmodified.
No functional changes for non-x86 architectures.
Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
xen/arch/arm/xen.lds.S | 1 +
xen/arch/ppc/xen.lds.S | 1 +
xen/arch/riscv/xen.lds.S | 1 +
xen/arch/x86/xen.lds.S | 1 +
xen/common/Kconfig | 2 +
xen/common/Makefile | 1 +
xen/common/emul/Kconfig | 6 ++
xen/common/emul/Makefile | 1 +
xen/common/emul/vuart/Kconfig | 6 ++
xen/common/emul/vuart/Makefile | 1 +
xen/common/emul/vuart/ns165x50.c | 0
xen/common/emul/vuart/vuart.c | 173 +++++++++++++++++++++++++++++++
xen/common/keyhandler.c | 3 +
xen/drivers/char/console.c | 6 +-
xen/include/xen/sched.h | 5 +
xen/include/xen/serial.h | 5 +
xen/include/xen/vuart.h | 115 ++++++++++++++++++++
xen/include/xen/xen.lds.h | 10 ++
18 files changed, 337 insertions(+), 1 deletion(-)
create mode 100644 xen/common/emul/Kconfig
create mode 100644 xen/common/emul/Makefile
create mode 100644 xen/common/emul/vuart/Kconfig
create mode 100644 xen/common/emul/vuart/Makefile
create mode 100644 xen/common/emul/vuart/ns165x50.c
create mode 100644 xen/common/emul/vuart/vuart.c
create mode 100644 xen/include/xen/vuart.h
diff --git a/xen/arch/arm/xen.lds.S b/xen/arch/arm/xen.lds.S
index 613511390ff4..423b8e717539 100644
--- a/xen/arch/arm/xen.lds.S
+++ b/xen/arch/arm/xen.lds.S
@@ -58,6 +58,7 @@ SECTIONS
*(.rodata)
*(.rodata.*)
VPCI_ARRAY
+ VUART_ARRAY
*(.data.rel.ro)
*(.data.rel.ro.*)
diff --git a/xen/arch/ppc/xen.lds.S b/xen/arch/ppc/xen.lds.S
index 37256c8865a9..228dbe0055c1 100644
--- a/xen/arch/ppc/xen.lds.S
+++ b/xen/arch/ppc/xen.lds.S
@@ -52,6 +52,7 @@ SECTIONS
*(.rodata)
*(.rodata.*)
VPCI_ARRAY
+ VUART_ARRAY
*(.data.rel.ro)
*(.data.rel.ro.*)
diff --git a/xen/arch/riscv/xen.lds.S b/xen/arch/riscv/xen.lds.S
index d9375a961697..ca34974f5adf 100644
--- a/xen/arch/riscv/xen.lds.S
+++ b/xen/arch/riscv/xen.lds.S
@@ -49,6 +49,7 @@ SECTIONS
*(.rodata)
*(.rodata.*)
VPCI_ARRAY
+ VUART_ARRAY
*(.data.rel.ro)
*(.data.rel.ro.*)
diff --git a/xen/arch/x86/xen.lds.S b/xen/arch/x86/xen.lds.S
index 8f943e11ead4..e57ab6ff74f3 100644
--- a/xen/arch/x86/xen.lds.S
+++ b/xen/arch/x86/xen.lds.S
@@ -123,6 +123,7 @@ SECTIONS
*(.rodata)
*(.rodata.*)
VPCI_ARRAY
+ VUART_ARRAY
*(.data.rel.ro)
*(.data.rel.ro.*)
diff --git a/xen/common/Kconfig b/xen/common/Kconfig
index 5b289e444fa5..7f102a5ab3fc 100644
--- a/xen/common/Kconfig
+++ b/xen/common/Kconfig
@@ -675,4 +675,6 @@ config PM_STATS
Enable collection of performance management statistics to aid in
analyzing and tuning power/performance characteristics of the system
+source "common/emul/Kconfig"
+
endmenu
diff --git a/xen/common/Makefile b/xen/common/Makefile
index f69d47d18934..60f2d9087589 100644
--- a/xen/common/Makefile
+++ b/xen/common/Makefile
@@ -12,6 +12,7 @@ obj-$(CONFIG_DEVICE_TREE_PARSE) += device-tree/
obj-$(CONFIG_IOREQ_SERVER) += dm.o
obj-y += domain.o
obj-y += domid.o
+obj-y += emul/
obj-$(CONFIG_HAS_SHARED_INFO) += event_2l.o
obj-y += event_channel.o
obj-$(CONFIG_EVTCHN_FIFO) += event_fifo.o
diff --git a/xen/common/emul/Kconfig b/xen/common/emul/Kconfig
new file mode 100644
index 000000000000..7c6764d1756b
--- /dev/null
+++ b/xen/common/emul/Kconfig
@@ -0,0 +1,6 @@
+menu "Domain Emulation Features"
+ visible if EXPERT
+
+source "common/emul/vuart/Kconfig"
+
+endmenu
diff --git a/xen/common/emul/Makefile b/xen/common/emul/Makefile
new file mode 100644
index 000000000000..ae0b575c3901
--- /dev/null
+++ b/xen/common/emul/Makefile
@@ -0,0 +1 @@
+obj-$(CONFIG_VUART_FRAMEWORK) += vuart/
diff --git a/xen/common/emul/vuart/Kconfig b/xen/common/emul/vuart/Kconfig
new file mode 100644
index 000000000000..ce1b976b7da7
--- /dev/null
+++ b/xen/common/emul/vuart/Kconfig
@@ -0,0 +1,6 @@
+config VUART_FRAMEWORK
+ bool
+
+menu "UART Emulation"
+
+endmenu
diff --git a/xen/common/emul/vuart/Makefile b/xen/common/emul/vuart/Makefile
new file mode 100644
index 000000000000..97f792dc6641
--- /dev/null
+++ b/xen/common/emul/vuart/Makefile
@@ -0,0 +1 @@
+obj-y += vuart.o
diff --git a/xen/common/emul/vuart/ns165x50.c b/xen/common/emul/vuart/ns165x50.c
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/xen/common/emul/vuart/vuart.c b/xen/common/emul/vuart/vuart.c
new file mode 100644
index 000000000000..bd3b756fb4d6
--- /dev/null
+++ b/xen/common/emul/vuart/vuart.c
@@ -0,0 +1,173 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * UART emulator framework.
+ *
+ */
+
+#include <asm/types.h>
+
+#include <xen/err.h>
+#include <xen/resource.h>
+#include <xen/sched.h>
+#include <xen/vuart.h>
+#include <xen/xvmalloc.h>
+
+#define for_each_emulator(e) \
+ for ( e = vuart_array_start; e < vuart_array_end; e++ )
+
+extern const struct vuart_emulator vuart_array_start[];
+extern const struct vuart_emulator vuart_array_end[];
+
+static const struct vuart_emulator *
+vuart_match_by_compatible(const struct domain *d, const char *compat)
+{
+ const struct vuart_emulator *emulator;
+
+ for_each_emulator(emulator)
+ if ( emulator->compatible &&
+ !strncmp(compat, emulator->compatible,
+ strlen(emulator->compatible)) )
+ return emulator;
+
+ return NULL;
+}
+
+const static struct vuart *
+vuart_find_by_flags(const struct domain *d, unsigned int flags)
+{
+ const struct vuart *vuart = d->console->vuart;
+
+ if ( vuart && (vuart->info->flags & flags) )
+ return vuart;
+
+ return NULL;
+}
+
+struct vuart *vuart_find_by_resource(const struct domain *d,
+ unsigned int type,
+ unsigned long addr,
+ unsigned long size)
+{
+ struct vuart *vuart = d->console->vuart;
+
+ if ( vuart &&
+ (type & vuart->info->base_type) &&
+ addr >= vuart->info->base_addr &&
+ addr + size <= vuart->info->base_addr + vuart->info->size )
+ return vuart;
+
+ return NULL;
+}
+
+int vuart_init(struct domain *d, const struct vuart_info *info)
+{
+ const struct vuart_emulator *emulator;
+ struct vuart *vuart;
+ int rc;
+
+ if ( !info )
+ return 0; /* Nothing to do */
+
+ if ( d->console->vuart )
+ return -EBUSY;
+
+ emulator = vuart_match_by_compatible(d, info->compatible);
+ if ( !emulator )
+ return -ENODEV;
+
+ vuart = xzalloc(typeof(*vuart));
+ if ( !vuart )
+ return -ENOMEM;
+
+ vuart->info = xvzalloc(typeof(*vuart->info));
+ if ( !vuart->info )
+ {
+ rc = -ENOMEM;
+ goto err_out1;
+ }
+ memcpy(vuart->info, info, sizeof(*info));
+
+ vuart->vdev = emulator->alloc(d, vuart->info);
+ if ( IS_ERR(vuart->vdev) )
+ {
+ rc = PTR_ERR(vuart->vdev);
+ goto err_out2;
+ }
+
+ vuart->emulator = emulator;
+ vuart->owner = d;
+
+ d->console->input_allowed = true;
+ d->console->vuart = vuart;
+
+ return 0;
+
+ err_out2:
+ xvfree(vuart->info);
+ err_out1:
+ xvfree(vuart);
+
+ return rc;
+}
+
+/*
+ * Release any resources taken by UART emulators.
+ *
+ * NB: no flags are cleared, since currently exit() is called only during
+ * domain destroy.
+ */
+void vuart_deinit(struct domain *d)
+{
+ struct vuart *vuart = d->console->vuart;
+
+ if ( vuart )
+ {
+ vuart->emulator->free(vuart->vdev);
+ xvfree(vuart->info);
+ }
+
+ XVFREE(d->console->vuart);
+}
+
+/*
+ * Print emulated UART state on the console.
+ *
+ * Must be called under rcu_lock_domain().
+ */
+void vuart_dump_state(const struct domain *d)
+{
+ struct vuart *vuart = d->console->vuart;
+
+ if ( vuart && vuart->emulator->dump_state )
+ vuart->emulator->dump_state(vuart->vdev);
+}
+
+/*
+ * Put character to the first emulated UART's FIFO with the physical console
+ * forwarding enabled.
+ *
+ * Must be called under rcu_lock_domain().
+ */
+int vuart_put_rx(const struct domain *d, char c)
+{
+ const struct vuart *vuart = vuart_find_by_flags(d, VUART_CONSOLE_INPUT);
+
+ if ( vuart && vuart->emulator->put_rx )
+ return vuart->emulator->put_rx(vuart->vdev, c);
+
+ return -ENODEV;
+}
+
+bool domain_has_vuart(const struct domain *d)
+{
+ return vuart_find_by_flags(d, VUART_CONSOLE_INPUT);
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/xen/common/keyhandler.c b/xen/common/keyhandler.c
index 7103bf20a7c9..b5012c9cb26d 100644
--- a/xen/common/keyhandler.c
+++ b/xen/common/keyhandler.c
@@ -22,6 +22,7 @@
#include <xen/mm.h>
#include <xen/watchdog.h>
#include <xen/init.h>
+#include <xen/vuart.h>
#include <asm/div64.h>
static unsigned char keypress_key;
@@ -360,6 +361,8 @@ static void cf_check dump_domains(unsigned char key)
v->periodic_period / 1000000);
}
}
+
+ vuart_dump_state(d);
}
for_each_domain ( d )
diff --git a/xen/drivers/char/console.c b/xen/drivers/char/console.c
index e4df0bbc2eec..62ca32e5392a 100644
--- a/xen/drivers/char/console.c
+++ b/xen/drivers/char/console.c
@@ -33,6 +33,7 @@
#include <xen/dom0less-build.h>
#include <xen/sections.h>
#include <xen/consoled.h>
+#include <xen/vuart.h>
#ifdef CONFIG_X86
#include <asm/guest.h>
@@ -639,11 +640,12 @@ static void __serial_rx(char c)
if ( !d )
return;
- if ( is_hardware_domain(d) )
+ if ( is_hardware_domain(d) && !domain_has_vuart(d) )
{
/*
* Deliver input to the hardware domain buffer, unless it is
* already full.
+ * NB: must be the first check: hardware domain may have emulated UART.
*/
if ( (serial_rx_prod - serial_rx_cons) != SERIAL_RX_SIZE )
serial_rx_ring[SERIAL_RX_MASK(serial_rx_prod++)] = c;
@@ -654,6 +656,8 @@ static void __serial_rx(char c)
*/
send_global_virq(VIRQ_CONSOLE);
}
+ else if ( domain_has_vuart(d) )
+ rc = vuart_put_rx(d, c);
#ifdef CONFIG_SBSA_VUART_CONSOLE
else
/* Deliver input to the emulated UART. */
diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h
index ce1fa636db36..4fa7f62490ca 100644
--- a/xen/include/xen/sched.h
+++ b/xen/include/xen/sched.h
@@ -24,6 +24,7 @@
#include <asm/atomic.h>
#include <asm/current.h>
#include <xen/vpci.h>
+#include <xen/vuart.h>
#include <xen/wait.h>
#include <public/xen.h>
#include <public/domctl.h>
@@ -382,6 +383,10 @@ struct domain_console {
unsigned int idx;
spinlock_t lock;
char buf[256];
+
+#ifdef CONFIG_VUART_FRAMEWORK
+ struct vuart *vuart;
+#endif
};
enum __packed domain_shutdown_state {
diff --git a/xen/include/xen/serial.h b/xen/include/xen/serial.h
index 3a71da767dd7..ec481893fede 100644
--- a/xen/include/xen/serial.h
+++ b/xen/include/xen/serial.h
@@ -32,10 +32,15 @@ enum serial_port_state {
struct vuart_info {
paddr_t base_addr; /* Base address of the UART */
+ unsigned int base_type; /* Base I/O resource type */
unsigned long size; /* Size of the memory region */
unsigned long data_off; /* Data register offset */
unsigned long status_off; /* Status register offset */
unsigned long status; /* Ready status value */
+ unsigned long flags; /* Emulation flags */
+ unsigned int irq; /* Emulated IRQ # */
+ char compatible[16]; /* Compatible string */
+ char name[16]; /* User-friendly name */
};
struct serial_port {
diff --git a/xen/include/xen/vuart.h b/xen/include/xen/vuart.h
new file mode 100644
index 000000000000..b5785b3c6b8f
--- /dev/null
+++ b/xen/include/xen/vuart.h
@@ -0,0 +1,115 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * UART emulator framework.
+ *
+ */
+
+#ifndef XEN_VUART_H
+#define XEN_VUART_H
+
+#include <xen/serial.h>
+#include <public/xen.h>
+
+struct vuart_emulator;
+
+enum {
+ VUART_CONSOLE_INPUT = BIT(0, U), /* Physical console input forwarding. */
+};
+
+/*
+ * FIXME: #ifdef is temporary to avoid clash with
+ * arch/arm/include/asm/domain.h
+ */
+#ifdef CONFIG_VUART_FRAMEWORK
+struct vuart {
+ const struct vuart_emulator *emulator;
+ struct vuart_info *info;
+ struct domain *owner;
+ void *vdev;
+};
+#endif
+
+struct vuart_emulator {
+ /* UART compatible string. Cannot be NULL or empty. */
+ const char *compatible;
+
+ /*
+ * Allocate emulated UART state (RX/TX FIFOs, locks, initialize registers,
+ * hook I/O handlers, etc.)
+ * Cannot be NULL.
+ */
+ void *(*alloc)(struct domain *d, const struct vuart_info *info);
+
+ /*
+ * Release resources used to emulate UART state (flush RX/TX FIFOs, unhook
+ * I/O handlers, etc.).
+ * Cannot be NULL.
+ */
+ void (*free)(void *arg);
+
+ /*
+ * Print emulated UART state (e.g. emulated registers) on the diagnostic
+ * console.
+ * Can be NULL.
+ */
+ void (*dump_state)(void *arg);
+
+ /*
+ * Place character to the emulated RX FIFO.
+ * Used to forward physical console input to the guest OS.
+ * Can be NULL.
+ */
+ int (*put_rx)(void *arg, char c);
+};
+
+#define VUART_REGISTER(name, x) \
+ static const struct vuart_emulator name##_entry \
+ __used_section(".data.rel.ro.vuart") = x
+
+struct vuart *vuart_find_by_resource(const struct domain *d,
+ unsigned int type,
+ unsigned long addr,
+ unsigned long size);
+
+int vuart_put_rx(const struct domain *d, char c);
+
+#ifdef CONFIG_VUART_FRAMEWORK
+
+int vuart_init(struct domain *d, const struct vuart_info *info);
+void vuart_deinit(struct domain *d);
+void vuart_dump_state(const struct domain *d);
+bool domain_has_vuart(const struct domain *d);
+
+#else
+
+static inline int vuart_init(struct domain *d, const struct vuart_info *info)
+{
+ return 0;
+}
+
+static inline void vuart_deinit(struct domain *d)
+{
+}
+
+static inline void vuart_dump_state(const struct domain *d)
+{
+}
+
+static inline bool domain_has_vuart(const struct domain *d)
+{
+ return false;
+}
+
+#endif /* CONFIG_VUART_FRAMEWORK */
+
+#endif /* XEN_VUART_H */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
+
diff --git a/xen/include/xen/xen.lds.h b/xen/include/xen/xen.lds.h
index 958f8256b0fa..5e630d371471 100644
--- a/xen/include/xen/xen.lds.h
+++ b/xen/include/xen/xen.lds.h
@@ -231,6 +231,16 @@
#define VPCI_ARRAY
#endif
+#ifdef CONFIG_VUART_FRAMEWORK
+#define VUART_ARRAY \
+ . = ALIGN(POINTER_ALIGN); \
+ vuart_array_start = .; \
+ *(.data.rel.ro.vuart) \
+ vuart_array_end = .;
+#else
+#define VUART_ARRAY
+#endif
+
#define EX_TABLE \
. = ALIGN(4); \
__start___ex_table = .; \
--
2.56.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [PATCH v8 02/26] xen/8250-uart: update definitions
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
2026-11-11 19:47 ` [PATCH v8 01/26] emul/vuart: introduce framework for UART emulators dmukhin
@ 2026-11-11 19:47 ` dmukhin
2026-11-12 14:49 ` Jan Beulich
2026-11-12 16:34 ` Oleksii Kurochko
2026-11-11 19:47 ` [PATCH v8 03/26] x86: fixup dependencies for asm/x86_emulate.h dmukhin
` (24 subsequent siblings)
26 siblings, 2 replies; 39+ messages in thread
From: dmukhin @ 2026-11-11 19:47 UTC (permalink / raw)
To: xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin, Denis Mukhin
From: Denis Mukhin <dmukhin@ford.com>
Added missing definitions needed for NS16550 UART emulator.
Newly introduced MSR definitions re-used in the existing ns16550 driver.
Also, corrected FCR DMA definition bit#3 (0x08) as per:
https://www.ti.com/lit/ds/symlink/tl16c550c.pdf
See "7.7.2 FIFO Control Register (FCR)".
Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
xen/drivers/char/ns16550.c | 16 ++++++++--------
xen/include/xen/8250-uart.h | 30 ++++++++++++++++++++++++++++--
2 files changed, 36 insertions(+), 10 deletions(-)
diff --git a/xen/drivers/char/ns16550.c b/xen/drivers/char/ns16550.c
index be388c8e6582..4313071e6cfc 100644
--- a/xen/drivers/char/ns16550.c
+++ b/xen/drivers/char/ns16550.c
@@ -422,7 +422,7 @@ static void __init cf_check ns16550_init_preirq(struct serial_port *port)
/* Check this really is a 16550+. Otherwise we have no FIFOs. */
if ( uart->fifo_size <= 1 &&
- ((ns_read_reg(uart, UART_IIR) & 0xc0) == 0xc0) &&
+ ((ns_read_reg(uart, UART_IIR) & UART_IIR_FE) == UART_IIR_FE) &&
((ns_read_reg(uart, UART_FCR) & UART_FCR_TRG14) == UART_FCR_TRG14) )
uart->fifo_size = 16;
}
@@ -755,20 +755,20 @@ static int __init check_existence(struct ns16550 *uart)
* Mask out IER[7:4] bits for test as some UARTs (e.g. TL
* 16C754B) allow only to modify them if an EFR bit is set.
*/
- scratch2 = ns_read_reg(uart, UART_IER) & 0x0f;
- ns_write_reg(uart,UART_IER, 0x0F);
- scratch3 = ns_read_reg(uart, UART_IER) & 0x0f;
+ scratch2 = ns_read_reg(uart, UART_IER) & UART_IER_ALL_INTR;
+ ns_write_reg(uart, UART_IER, UART_IER_ALL_INTR);
+ scratch3 = ns_read_reg(uart, UART_IER) & UART_IER_ALL_INTR;
ns_write_reg(uart, UART_IER, scratch);
- if ( (scratch2 != 0) || (scratch3 != 0x0F) )
+ if ( (scratch2 != 0) || (scratch3 != UART_IER_ALL_INTR) )
return 0;
/*
* Check to see if a UART is really there.
* Use loopback test mode.
*/
- ns_write_reg(uart, UART_MCR, UART_MCR_LOOP | 0x0A);
- status = ns_read_reg(uart, UART_MSR) & 0xF0;
- return (status == 0x90);
+ ns_write_reg(uart, UART_MCR, UART_MCR_LOOP | UART_MCR_RTS | UART_MCR_OUT2);
+ status = ns_read_reg(uart, UART_MSR) & UART_MSR_STATUS;
+ return (status == (UART_MSR_CTS | UART_MSR_DCD));
}
#ifdef CONFIG_HAS_PCI
diff --git a/xen/include/xen/8250-uart.h b/xen/include/xen/8250-uart.h
index d13352940c13..15cc4be5ff81 100644
--- a/xen/include/xen/8250-uart.h
+++ b/xen/include/xen/8250-uart.h
@@ -32,6 +32,7 @@
#define UART_MCR 0x04 /* Modem control */
#define UART_LSR 0x05 /* line status */
#define UART_MSR 0x06 /* Modem status */
+#define UART_SCR 0x07 /* Scratch pad */
#define UART_USR 0x1f /* Status register (DW) */
#define UART_DLL 0x00 /* divisor latch (ls) (DLAB=1) */
#define UART_DLM 0x01 /* divisor latch (ms) (DLAB=1) */
@@ -42,6 +43,8 @@
#define UART_IER_ETHREI 0x02 /* tx reg. empty */
#define UART_IER_ELSI 0x04 /* rx line status */
#define UART_IER_EMSI 0x08 /* MODEM status */
+#define UART_IER_ALL_INTR \
+ (UART_IER_ERDAI | UART_IER_ETHREI | UART_IER_ELSI | UART_IER_EMSI)
/* Interrupt Identification Register */
#define UART_IIR_NOINT 0x01 /* no interrupt pending */
@@ -51,12 +54,18 @@
#define UART_IIR_THR 0x02 /* - tx reg. empty */
#define UART_IIR_MSI 0x00 /* - MODEM status */
#define UART_IIR_BSY 0x07 /* - busy detect (DW) */
+#define UART_IIR_FE 0xc0 /* FIFO enabled (2 bits) */
/* FIFO Control Register */
#define UART_FCR_ENABLE 0x01 /* enable FIFO */
#define UART_FCR_CLRX 0x02 /* clear Rx FIFO */
#define UART_FCR_CLTX 0x04 /* clear Tx FIFO */
-#define UART_FCR_DMA 0x10 /* enter DMA mode */
+#define UART_FCR_DMA 0x08 /* enter DMA mode */
+#define UART_FCR_RSRVD0 0x10 /* reserved; always 0 */
+#define UART_FCR_RSRVD1 0x20 /* reserved; always 0 */
+#define UART_FCR_RTB0 0x40 /* receiver trigger bit #0 */
+#define UART_FCR_RTB1 0x80 /* receiver trigger bit #1 */
+
#define UART_FCR_TRG1 0x00 /* Rx FIFO trig lev 1 */
#define UART_FCR_TRG4 0x40 /* Rx FIFO trig lev 4 */
#define UART_FCR_TRG8 0x80 /* Rx FIFO trig lev 8 */
@@ -98,9 +107,26 @@
/* Modem Control Register */
#define UART_MCR_DTR 0x01 /* Data Terminal Ready */
#define UART_MCR_RTS 0x02 /* Request to Send */
-#define UART_MCR_OUT2 0x08 /* OUT2: interrupt mask */
+#define UART_MCR_OUT1 0x04 /* Output #1 */
+#define UART_MCR_OUT2 0x08 /* Output #2 */
#define UART_MCR_LOOP 0x10 /* Enable loopback test mode */
+#define UART_MCR_RSRVD0 0x20 /* Reserved #0 */
#define UART_MCR_TCRTLR 0x40 /* Access TCR/TLR (TI16C752, EFR[4]=1) */
+#define UART_MCR_RSRVD1 0x80 /* Reserved #1 */
+
+/* Modem Status Register */
+#define UART_MSR_DCTS 0x01 /* Change in CTS */
+#define UART_MSR_DDSR 0x02 /* Change in DSR */
+#define UART_MSR_TERI 0x04 /* Change in RI */
+#define UART_MSR_DDCD 0x08 /* Change in DCD */
+#define UART_MSR_CTS 0x10
+#define UART_MSR_DSR 0x20
+#define UART_MSR_RI 0x40
+#define UART_MSR_DCD 0x80
+#define UART_MSR_CHANGE \
+ (UART_MSR_DCTS | UART_MSR_DDSR | UART_MSR_TERI | UART_MSR_DDCD)
+#define UART_MSR_STATUS \
+ (UART_MSR_CTS | UART_MSR_DSR | UART_MSR_RI | UART_MSR_DCD)
/* Line Status Register */
#define UART_LSR_DR 0x01 /* Data ready */
--
2.56.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [PATCH v8 03/26] x86: fixup dependencies for asm/x86_emulate.h
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
2026-11-11 19:47 ` [PATCH v8 01/26] emul/vuart: introduce framework for UART emulators dmukhin
2026-11-11 19:47 ` [PATCH v8 02/26] xen/8250-uart: update definitions dmukhin
@ 2026-11-11 19:47 ` dmukhin
2026-11-12 20:25 ` Oleksii Kurochko
2026-11-11 19:47 ` [PATCH v8 04/26] emul/ns16x50: implement emulator stub dmukhin
` (23 subsequent siblings)
26 siblings, 1 reply; 39+ messages in thread
From: dmukhin @ 2026-11-11 19:47 UTC (permalink / raw)
To: xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin, Denis Mukhin
From: Denis Mukhin <dmukhin@ford.com>
Add xen/bug.h so that asm/x86_emulate.h can be cleanly used if listed as
the first include in translation unit.
Amends: 0a249bcb0279 ("x86: reduce dependencies on x86_emulate/x86_emulate.h")
Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
xen/arch/x86/include/asm/x86_emulate.h | 1 +
1 file changed, 1 insertion(+)
diff --git a/xen/arch/x86/include/asm/x86_emulate.h b/xen/arch/x86/include/asm/x86_emulate.h
index 2b75f7f8e215..209d1e54e03f 100644
--- a/xen/arch/x86/include/asm/x86_emulate.h
+++ b/xen/arch/x86/include/asm/x86_emulate.h
@@ -13,6 +13,7 @@
#define __ASM_X86_X86_EMULATE_H__
#include <xen/types.h>
+#include <xen/bug.h>
#include <xen/lib.h>
#include <asm/regs.h>
#include <asm/x86-defns.h>
--
2.56.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [PATCH v8 04/26] emul/ns16x50: implement emulator stub
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
` (2 preceding siblings ...)
2026-11-11 19:47 ` [PATCH v8 03/26] x86: fixup dependencies for asm/x86_emulate.h dmukhin
@ 2026-11-11 19:47 ` dmukhin
2026-11-13 11:48 ` Oleksii Kurochko
2026-11-11 19:47 ` [PATCH v8 05/26] emul/ns16x50: implement DLL/DLM registers dmukhin
` (22 subsequent siblings)
26 siblings, 1 reply; 39+ messages in thread
From: dmukhin @ 2026-11-11 19:47 UTC (permalink / raw)
To: xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin, Denis Mukhin
From: Denis Mukhin <dmukhin@ford.com>
Define the placeholder for NS16550 UART emulator.
Implement alloc/free vUART hooks, add stub for I/O port handler,
initialize emulator state and define debug logging facilities.
Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
xen/common/emul/vuart/Makefile | 1 +
xen/common/emul/vuart/ns16x50.c | 417 ++++++++++++++++++++++++++++++++
2 files changed, 418 insertions(+)
create mode 100644 xen/common/emul/vuart/ns16x50.c
diff --git a/xen/common/emul/vuart/Makefile b/xen/common/emul/vuart/Makefile
index 97f792dc6641..fe904f6cb65d 100644
--- a/xen/common/emul/vuart/Makefile
+++ b/xen/common/emul/vuart/Makefile
@@ -1 +1,2 @@
obj-y += vuart.o
+obj-$(CONFIG_VUART_NS16X50) += ns16x50.o
diff --git a/xen/common/emul/vuart/ns16x50.c b/xen/common/emul/vuart/ns16x50.c
new file mode 100644
index 000000000000..6e1cfecbaa55
--- /dev/null
+++ b/xen/common/emul/vuart/ns16x50.c
@@ -0,0 +1,417 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * NS16550-compatible UART Emulator.
+ *
+ * See:
+ * - Serial and UART Tutorial:
+ * https://download.freebsd.org/doc/en/articles/serial-uart/serial-uart_en.pdf
+ * - UART w/ 16 byte FIFO:
+ * https://www.ti.com/lit/ds/symlink/tl16c550c.pdf
+ * - UART w/ 64 byte FIFO:
+ * https://www.ti.com/lit/ds/symlink/tl16c750.pdf
+ * - DesignWare DW_apb_uart Databook, v4.02a:
+ * https://iccircle.com/static/upload/img20240313113905.pdf
+ *
+ * Limitations:
+ * - Only x86;
+ * - Only Xen console as a backend, no inter-domain communication (similar to
+ * vpl011 on Arm);
+ * - Only 8n1 emulation (8-bit data, no parity, 1 stop bit);
+ * - No baud rate emulation (reports 115200 baud to the guest OS);
+ * - No FIFO-less mode emulation;
+ * - No RX FIFO interrupt moderation (FCR) emulation;
+ * - No integration w/ VM snapshotting (HVM_REGISTER_SAVE_RESTORE() and
+ * friends);
+ * - No ISA IRQ sharing allowed;
+ * - No MMIO-based UART emulation.
+ */
+
+#define pr_prefix "ns16x50"
+#define pr_fmt(fmt) pr_prefix ": " fmt
+
+#ifdef CONFIG_VUART_NS16X50_DEBUG
+#define guest_prefix "FROM GUEST "
+#define ns16x50_log_level 2
+#else
+#define guest_prefix ""
+#define ns16x50_log_level 0
+#endif
+
+#include <asm/x86_emulate.h>
+
+#include <xen/8250-uart.h>
+#include <xen/console.h>
+#include <xen/err.h>
+#include <xen/iocap.h>
+#include <xen/resource.h>
+#include <xen/vuart.h>
+#include <xen/xvmalloc.h>
+
+#include <public/io/console.h>
+
+#define ns16x50_log(n, lvl, vdev, fmt, args...) \
+do { \
+ if ( ns16x50_log_level >= n ) \
+ gprintk(lvl, pr_fmt("%s: " fmt), (vdev)->name, ## args); \
+} while (0)
+
+#define ns16x50_err(vdev, fmt, args...) \
+ ns16x50_log(0, XENLOG_ERR, vdev, fmt, ## args)
+#define ns16x50_warn(vdev, fmt, args...) \
+ ns16x50_log(1, XENLOG_WARNING, vdev, fmt, ## args)
+#define ns16x50_info(vdev, fmt, args...) \
+ ns16x50_log(2, XENLOG_INFO, vdev, fmt, ## args)
+#define ns16x50_debug(vdev, fmt, args...) \
+ ns16x50_log(3, XENLOG_DEBUG, vdev, fmt, ## args)
+
+/*
+ * Number of supported registers in the UART.
+ */
+#define NS16X50_REGS_NUM (UART_SCR + 1)
+
+/*
+ * Number of emulated registers.
+ *
+ * - Emulated registers [0..NS16X50_REGS_NUM] are R/W registers for DLAB=0.
+ * - DLAB=1, R/W, DLL = (NS16X50_REGS_NUM + 0)
+ * - DLAB=1, R/W, DLM = (NS16X50_REGS_NUM + 1)
+ */
+#define NS16X50_EMU_REGS_NUM (NS16X50_REGS_NUM + 2)
+
+/*
+ * Virtual ns16x50 device state.
+ */
+struct vuart_ns16x50 {
+ uint8_t regs[NS16X50_EMU_REGS_NUM]; /* Emulated registers */
+ const struct vuart_info *info; /* UART description */
+ struct domain *owner; /* Owner domain */
+ const char *name; /* Device name */
+ spinlock_t lock; /* Protection */
+ struct xencons_interface cons; /* Emulated RX/TX FIFOs */
+};
+
+static inline void ns16x50_lock_init(struct vuart_ns16x50 *vdev)
+{
+ spin_lock_init(&vdev->lock);
+}
+
+static inline void ns16x50_lock(struct vuart_ns16x50 *vdev,
+ unsigned long *flags)
+{
+ spin_lock(&vdev->lock);
+}
+
+static inline bool ns16x50_trylock(struct vuart_ns16x50 *vdev,
+ unsigned long *flags)
+{
+ return spin_trylock(&vdev->lock);
+}
+
+static inline void ns16x50_unlock(struct vuart_ns16x50 *vdev,
+ unsigned long *flags)
+{
+ spin_unlock(&vdev->lock);
+}
+
+/*
+ * Emulate 8-bit write access to ns16x50 register.
+ */
+static int ns16x50_io_write8(
+ struct vuart_ns16x50 *vdev, uint32_t reg, uint8_t *data)
+{
+ int rc = 0;
+
+ return rc;
+}
+
+/*
+ * Emulate 16-bit write access to ns16x50 register.
+ * NB: some guest OSes use outw() to access UART_DLL.
+ */
+static int ns16x50_io_write16(
+ struct vuart_ns16x50 *vdev, uint32_t reg, uint16_t *data)
+{
+ int rc = -EINVAL;
+
+ return rc;
+}
+
+/*
+ * Emulate write access to ns16x50 register.
+ */
+static int ns16x50_io_write(
+ struct vuart_ns16x50 *vdev, uint8_t reg, uint32_t size, uint32_t *data)
+{
+ int rc;
+
+ switch ( size )
+ {
+ case 1:
+ rc = ns16x50_io_write8(vdev, reg, (uint8_t *)data);
+ break;
+
+ case 2:
+ rc = ns16x50_io_write16(vdev, reg, (uint16_t *)data);
+ break;
+
+ default:
+ rc = -EINVAL;
+ break;
+ }
+
+ return rc;
+}
+
+/*
+ * Emulate 8-bit read access to ns16x50 register.
+ */
+static int ns16x50_io_read8(
+ struct vuart_ns16x50 *vdev, uint32_t reg, uint8_t *data)
+{
+ uint8_t val = UINT8_MAX;
+ int rc = 0;
+
+ *data = val;
+
+ return rc;
+}
+
+/*
+ * Emulate 16-bit read access to ns16x50 register.
+ */
+static int ns16x50_io_read16(
+ struct vuart_ns16x50 *vdev, uint32_t reg, uint16_t *data)
+{
+ uint16_t val = UINT16_MAX;
+ int rc = -EINVAL;
+
+ *data = val;
+
+ return rc;
+}
+
+/*
+ * Emulate read access to ns16x50 register.
+ */
+static int ns16x50_io_read(
+ struct vuart_ns16x50 *vdev, uint8_t reg, uint32_t size, uint32_t *data)
+{
+ int rc;
+
+ switch ( size )
+ {
+ case 1:
+ rc = ns16x50_io_read8(vdev, reg, (uint8_t *)data);
+ break;
+
+ case 2:
+ rc = ns16x50_io_read16(vdev, reg, (uint16_t *)data);
+ break;
+
+ default:
+ *data = UINT32_MAX;
+ rc = -EINVAL;
+ break;
+ }
+
+ return rc;
+}
+
+static void ns16x50_access(int dir, unsigned int type,
+ unsigned long addr, unsigned int size,
+ uint32_t *data)
+{
+ const char op = (dir == IOREQ_WRITE) ? 'W' : 'R';
+ struct domain *d = rcu_lock_current_domain();
+ struct vuart *vuart = vuart_find_by_resource(d, type, addr, size);
+ struct vuart_ns16x50 *vdev = NULL;
+ const struct domain *owner;
+ const struct vuart_info *info;
+ const char *as;
+ uint32_t reg;
+ unsigned long flags;
+ int rc;
+
+ if ( !vuart )
+ {
+ printk(XENLOG_ERR "%c @ %#"PRIx64" %d: not initialized\n",
+ op, addr, size);
+
+ ASSERT_UNREACHABLE();
+ goto out;
+ }
+
+ vdev = vuart->vdev;
+ ASSERT(vdev);
+
+ ns16x50_lock(vdev, &flags);
+
+ owner = vuart->owner;
+ ASSERT(owner);
+
+ if ( d != owner )
+ {
+ ns16x50_err(vdev, "%c @ %#"PRIx64" %d: does not match current domain %pd\n",
+ op, addr, size, d);
+
+ ASSERT_UNREACHABLE();
+ goto out_unlock;
+ }
+
+ info = vuart->info;
+ ASSERT(info);
+ as = (info->base_type & IORESOURCE_IO) ? "ioport": "iomem";
+
+ reg = addr - info->base_addr;
+ if ( !IS_ALIGNED(reg, size) )
+ {
+ ns16x50_err(vdev, "%c %s %#"PRIx64" %d: unaligned access\n",
+ op, as, addr, size);
+ goto out_unlock;
+ }
+
+ if ( reg >= NS16X50_REGS_NUM )
+ {
+ ns16x50_err(vdev, "%c %s %#"PRIx64" %d: LCR=%#02x %#02x %#x: not implemented\n",
+ op, as, addr, size, vdev->regs[UART_LCR], reg, *data);
+ goto out_unlock;
+ }
+
+ if ( dir == IOREQ_WRITE )
+ rc = ns16x50_io_write(vdev, reg, size, data);
+ else
+ rc = ns16x50_io_read(vdev, reg, size, data);
+
+
+ if ( rc == 0 )
+ ns16x50_debug(vdev, "%c %s %#"PRIx64" %d: LCR=%#02x %#02x %#08x\n",
+ op, as, addr, size, vdev->regs[UART_LCR], reg, *data);
+ else
+ ns16x50_err(vdev, "%c %s %#"PRIx64" %d: LCR=%#02x %#02x %#08x: unsupported access\n",
+ op, as, addr, size, vdev->regs[UART_LCR], reg, *data);
+
+out_unlock:
+ ns16x50_unlock(vdev, &flags);
+out:
+ rcu_unlock_domain(d);
+}
+
+/*
+ * Emulate I/O access to ns16x50 register.
+ * Note, emulation always returns X86EMUL_OKAY, once I/O port trap is enabled.
+ */
+static int cf_check ns16x50_io_handle(
+ int dir, unsigned int addr, unsigned int size, uint32_t *data)
+{
+ ns16x50_access(dir, IORESOURCE_IO, addr, size, data);
+
+ return X86EMUL_OKAY;
+}
+
+static void ns16x50_reset(struct vuart_ns16x50 *vdev)
+{
+ memset(vdev->regs, 0, sizeof(vdev->regs));
+}
+
+static int ns16x50_init(void *arg)
+{
+ struct vuart_ns16x50 *vdev = arg;
+ const struct vuart_info *info;
+ struct domain *d;
+
+ ASSERT(vdev);
+
+ d = vdev->owner;
+ info = vdev->info;
+
+ if ( info->base_type & IORESOURCE_IO )
+ {
+ register_portio_handler(d, info->base_addr, info->size, ns16x50_io_handle);
+ }
+ else if ( info->base_type & IORESOURCE_MEM )
+ {
+ BUG_ON("unimplemented");
+ }
+ else
+ ASSERT_UNREACHABLE();
+
+ ns16x50_reset(vdev);
+
+ return 0;
+}
+
+static void cf_check ns16x50_deinit(void *arg)
+{
+ struct vuart_ns16x50 *vdev = arg;
+
+ ASSERT(vdev);
+}
+
+static void * cf_check ns16x50_alloc(struct domain *d, const struct vuart_info *info)
+{
+ struct vuart_ns16x50 *vdev;
+ int rc;
+
+ if ( !is_hvm_domain(d) )
+ {
+ ns16x50_err(info, "not an HVM domain\n");
+ return ERR_PTR(-ENOSYS);
+ }
+
+ if ( vuart_find_by_resource(d, info->base_type, info->base_addr,
+ info->size) )
+ {
+ ns16x50_err(info, "already registered\n");
+ return ERR_PTR(-EBUSY);
+ }
+
+ vdev = xvzalloc(typeof(*vdev));
+ if ( !vdev )
+ {
+ ns16x50_err(info, "failed to allocate memory\n");
+ return ERR_PTR(-ENOMEM);
+ }
+
+ ns16x50_lock_init(vdev);
+
+ vdev->name = info->name;
+ vdev->owner = d;
+ vdev->info = info;
+
+ rc = ns16x50_init(vdev);
+ if ( rc )
+ {
+ xvfree(vdev);
+ return ERR_PTR(rc);
+ }
+
+ return vdev;
+}
+
+static void cf_check ns16x50_free(void *arg)
+{
+ if ( arg )
+ ns16x50_deinit(arg);
+
+ xvfree(arg);
+}
+
+#define ns16x50_emulator \
+{ \
+ .compatible = "ns16550", \
+ .alloc = ns16x50_alloc, \
+ .free = ns16x50_free, \
+ .dump_state = NULL, \
+ .put_rx = NULL, \
+}
+
+VUART_REGISTER(ns16x50, ns16x50_emulator);
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
--
2.56.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [PATCH v8 05/26] emul/ns16x50: implement DLL/DLM registers
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
` (3 preceding siblings ...)
2026-11-11 19:47 ` [PATCH v8 04/26] emul/ns16x50: implement emulator stub dmukhin
@ 2026-11-11 19:47 ` dmukhin
2026-11-16 10:36 ` Oleksii Kurochko
2026-11-11 19:47 ` [PATCH v8 06/26] emul/ns16x50: implement SCR register dmukhin
` (21 subsequent siblings)
26 siblings, 1 reply; 39+ messages in thread
From: dmukhin @ 2026-11-11 19:47 UTC (permalink / raw)
To: xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin, Denis Mukhin
From: Denis Mukhin <dmukhin@ford.com>
Add DLL/DLM registers emulation.
DLL/DLM registers report hardcoded 115200 baud rate to the guest OS.
Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
xen/common/emul/vuart/ns16x50.c | 82 +++++++++++++++++++++++++++++++++
1 file changed, 82 insertions(+)
diff --git a/xen/common/emul/vuart/ns16x50.c b/xen/common/emul/vuart/ns16x50.c
index 6e1cfecbaa55..80805ee6ecc6 100644
--- a/xen/common/emul/vuart/ns16x50.c
+++ b/xen/common/emul/vuart/ns16x50.c
@@ -119,8 +119,37 @@ static inline void ns16x50_unlock(struct vuart_ns16x50 *vdev,
static int ns16x50_io_write8(
struct vuart_ns16x50 *vdev, uint32_t reg, uint8_t *data)
{
+ uint8_t *regs = vdev->regs;
+ uint8_t val = *data;
int rc = 0;
+ switch ( reg )
+ {
+ case UART_THR: /* DLAB=0 --> UART_RBR, DLAB=1 --> UART_DLL */
+ /* DLAB=1 */
+ if ( regs[UART_LCR] & UART_LCR_DLAB )
+ {
+ regs[NS16X50_REGS_NUM + UART_DLL] = val;
+ break;
+ }
+
+ /* DLAB=0 */
+ break;
+
+ case UART_IER: /* DLAB=0 --> UART_IER, DLAB=1 --> UART_DLM */
+ /* DLAB=1 */
+ if ( regs[UART_LCR] & UART_LCR_DLAB )
+ {
+ regs[NS16X50_REGS_NUM + UART_DLM] = val;
+ }
+ /* DLAB=0 */
+ break;
+
+ default:
+ rc = -EINVAL;
+ break;
+ }
+
return rc;
}
@@ -131,8 +160,17 @@ static int ns16x50_io_write8(
static int ns16x50_io_write16(
struct vuart_ns16x50 *vdev, uint32_t reg, uint16_t *data)
{
+ uint8_t *regs = vdev->regs;
+ uint16_t val = *data;
int rc = -EINVAL;
+ if ( (regs[UART_LCR] & UART_LCR_DLAB) && reg == UART_DLL )
+ {
+ vdev->regs[NS16X50_REGS_NUM + UART_DLL] = val & UINT8_MAX;
+ vdev->regs[NS16X50_REGS_NUM + UART_DLM] = (val >> 8) & UINT8_MAX;
+ rc = 0;
+ }
+
return rc;
}
@@ -168,9 +206,39 @@ static int ns16x50_io_write(
static int ns16x50_io_read8(
struct vuart_ns16x50 *vdev, uint32_t reg, uint8_t *data)
{
+ uint8_t *regs = vdev->regs;
uint8_t val = UINT8_MAX;
int rc = 0;
+ switch ( reg )
+ {
+ case UART_RBR: /* DLAB=0 --> UART_RBR, DLAB=1 --> UART_DLL */
+ /* DLAB=1 */
+ if ( regs[UART_LCR] & UART_LCR_DLAB )
+ {
+ val = regs[NS16X50_REGS_NUM + UART_DLL];
+ break;
+ }
+
+ /* DLAB=0 */
+ break;
+
+ case UART_IER: /* DLAB=0 --> UART_IER, DLAB=1 --> UART_DLM */
+ /* DLAB=1 */
+ if ( regs[UART_LCR] & UART_LCR_DLAB )
+ {
+ val = regs[NS16X50_REGS_NUM + UART_DLM];
+ break;
+ }
+
+ /* DLAB=0 */
+ break;
+
+ default:
+ rc = -EINVAL;
+ break;
+ }
+
*data = val;
return rc;
@@ -182,9 +250,17 @@ static int ns16x50_io_read8(
static int ns16x50_io_read16(
struct vuart_ns16x50 *vdev, uint32_t reg, uint16_t *data)
{
+ uint8_t *regs = vdev->regs;
uint16_t val = UINT16_MAX;
int rc = -EINVAL;
+ if ( (regs[UART_LCR] & UART_LCR_DLAB) && reg == UART_DLL )
+ {
+ val = vdev->regs[NS16X50_REGS_NUM + UART_DLM] << 8 |
+ vdev->regs[NS16X50_REGS_NUM + UART_DLL];
+ rc = 0;
+ }
+
*data = val;
return rc;
@@ -310,7 +386,13 @@ static int cf_check ns16x50_io_handle(
static void ns16x50_reset(struct vuart_ns16x50 *vdev)
{
+ const uint16_t divisor = (UART_CLOCK_HZ / 115200) >> 4;
+
memset(vdev->regs, 0, sizeof(vdev->regs));
+
+ /* NB: report 115200 baud rate. */
+ vdev->regs[NS16X50_REGS_NUM + UART_DLL] = divisor & UINT8_MAX;
+ vdev->regs[NS16X50_REGS_NUM + UART_DLM] = (divisor >> 8) & UINT8_MAX;
}
static int ns16x50_init(void *arg)
--
2.56.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [PATCH v8 06/26] emul/ns16x50: implement SCR register
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
` (4 preceding siblings ...)
2026-11-11 19:47 ` [PATCH v8 05/26] emul/ns16x50: implement DLL/DLM registers dmukhin
@ 2026-11-11 19:47 ` dmukhin
2026-11-16 10:42 ` Oleksii Kurochko
2026-11-11 19:47 ` [PATCH v8 07/26] emul/ns16x50: implement IER/IIR registers dmukhin
` (20 subsequent siblings)
26 siblings, 1 reply; 39+ messages in thread
From: dmukhin @ 2026-11-11 19:47 UTC (permalink / raw)
To: xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin, Denis Mukhin
From: Denis Mukhin <dmukhin@ford.com>
Add SCR register emulation to the I/O port handler.
Firmware (e.g. OVMF) may use SCR during the guest OS boot.
Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
xen/common/emul/vuart/ns16x50.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/xen/common/emul/vuart/ns16x50.c b/xen/common/emul/vuart/ns16x50.c
index 80805ee6ecc6..2371aa32d03f 100644
--- a/xen/common/emul/vuart/ns16x50.c
+++ b/xen/common/emul/vuart/ns16x50.c
@@ -145,6 +145,11 @@ static int ns16x50_io_write8(
/* DLAB=0 */
break;
+ /* NB: Firmware (e.g. OVMF) may rely on SCR presence. */
+ case UART_SCR:
+ regs[UART_SCR] = val;
+ break;
+
default:
rc = -EINVAL;
break;
@@ -234,6 +239,10 @@ static int ns16x50_io_read8(
/* DLAB=0 */
break;
+ case UART_SCR:
+ val = regs[UART_SCR];
+ break;
+
default:
rc = -EINVAL;
break;
--
2.56.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [PATCH v8 07/26] emul/ns16x50: implement IER/IIR registers
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
` (5 preceding siblings ...)
2026-11-11 19:47 ` [PATCH v8 06/26] emul/ns16x50: implement SCR register dmukhin
@ 2026-11-11 19:47 ` dmukhin
2026-11-16 16:59 ` Oleksii Kurochko
2026-11-11 19:47 ` [PATCH v8 08/26] emul/ns16x50: implement LCR/LSR registers dmukhin
` (19 subsequent siblings)
26 siblings, 1 reply; 39+ messages in thread
From: dmukhin @ 2026-11-11 19:47 UTC (permalink / raw)
To: xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin, Denis Mukhin
From: Denis Mukhin <dmukhin@ford.com>
Add interrupt enable register emulation (IER) and interrupt identity
reason (IIR) register emulation to the I/O port handler.
Also add routines for asserting/deasserting the virtual ns16x50
interrupt line as a dependent on IIR code. Add vPIC handling (HVM).
Latch the interrupt line state to avoid poke ns16x50_irq_check() on
every I/O register access.
Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
xen/common/emul/vuart/ns16x50.c | 112 ++++++++++++++++++++++++++++++++
1 file changed, 112 insertions(+)
diff --git a/xen/common/emul/vuart/ns16x50.c b/xen/common/emul/vuart/ns16x50.c
index 2371aa32d03f..7c5cdc99cfa7 100644
--- a/xen/common/emul/vuart/ns16x50.c
+++ b/xen/common/emul/vuart/ns16x50.c
@@ -88,6 +88,7 @@ struct vuart_ns16x50 {
const char *name; /* Device name */
spinlock_t lock; /* Protection */
struct xencons_interface cons; /* Emulated RX/TX FIFOs */
+ bool irq_asserted; /* IRQ line status */
};
static inline void ns16x50_lock_init(struct vuart_ns16x50 *vdev)
@@ -113,6 +114,101 @@ static inline void ns16x50_unlock(struct vuart_ns16x50 *vdev,
spin_unlock(&vdev->lock);
}
+static bool cf_check ns16x50_iir_check_lsi(const struct vuart_ns16x50 *vdev)
+{
+ return false;
+}
+
+static bool cf_check ns16x50_iir_check_rda(const struct vuart_ns16x50 *vdev)
+{
+ return false;
+}
+
+static bool cf_check ns16x50_iir_check_thr(const struct vuart_ns16x50 *vdev)
+{
+ return false;
+}
+
+static bool cf_check ns16x50_iir_check_msi(const struct vuart_ns16x50 *vdev)
+{
+ return false;
+}
+
+/*
+ * Get the interrupt identity reason.
+ *
+ * IIR is re-calculated once called, because ns16x50 always reports high
+ * priority events first.
+ */
+static uint8_t ns16x50_iir_get(const struct vuart_ns16x50 *vdev)
+{
+ /*
+ * Interrupt identity reasons by priority.
+ * NB: high priority are at lower indexes below.
+ */
+ static const struct {
+ bool (*check)(const struct vuart_ns16x50 *vdev);
+ uint8_t ier;
+ uint8_t iir;
+ } iir_by_prio[] = {
+ [0] = { ns16x50_iir_check_lsi, UART_IER_ELSI, UART_IIR_LSI },
+ [1] = { ns16x50_iir_check_rda, UART_IER_ERDAI, UART_IIR_RDA },
+ [2] = { ns16x50_iir_check_thr, UART_IER_ETHREI, UART_IIR_THR },
+ [3] = { ns16x50_iir_check_msi, UART_IER_EMSI, UART_IIR_MSI },
+ };
+ const uint8_t *regs = vdev->regs;
+ uint8_t iir = 0;
+ unsigned int i;
+
+ for ( i = 0; i < ARRAY_SIZE(iir_by_prio); i++ )
+ {
+ if ( (regs[UART_IER] & iir_by_prio[i].ier) &&
+ iir_by_prio[i].check(vdev) )
+ break;
+
+ }
+ if ( i == ARRAY_SIZE(iir_by_prio) )
+ iir |= UART_IIR_NOINT;
+ else
+ iir |= iir_by_prio[i].iir;
+
+ if ( regs[UART_FCR] & UART_FCR_ENABLE )
+ iir |= UART_IIR_FE;
+
+ return iir;
+}
+
+/*
+ * Assert/deassert virtual ns16x50 interrupt line.
+ */
+static void ns16x50_irq_check(struct vuart_ns16x50 *vdev)
+{
+ const struct vuart_info *info = vdev->info;
+ struct domain *d = vdev->owner;
+ uint8_t iir = ns16x50_iir_get(vdev);
+ bool asserted = !(iir & UART_IIR_NOINT);
+
+ if ( asserted == vdev->irq_asserted )
+ return;
+
+ vdev->irq_asserted = asserted;
+
+ if ( has_vpic(d) )
+ {
+ if ( asserted )
+ hvm_isa_irq_assert(d, info->irq, vioapic_get_vector);
+ else
+ hvm_isa_irq_deassert(d, info->irq);
+ }
+ else
+ ASSERT_UNREACHABLE();
+
+ ns16x50_debug(vdev, "IRQ#%d IIR %#02x %s\n",
+ info->irq,
+ iir,
+ asserted ? "assert" : "deassert");
+}
+
/*
* Emulate 8-bit write access to ns16x50 register.
*/
@@ -143,6 +239,10 @@ static int ns16x50_io_write8(
regs[NS16X50_REGS_NUM + UART_DLM] = val;
}
/* DLAB=0 */
+ else
+ {
+ regs[UART_IER] = val & UART_IER_ALL_INTR;
+ }
break;
/* NB: Firmware (e.g. OVMF) may rely on SCR presence. */
@@ -155,6 +255,8 @@ static int ns16x50_io_write8(
break;
}
+ ns16x50_irq_check(vdev);
+
return rc;
}
@@ -237,6 +339,11 @@ static int ns16x50_io_read8(
}
/* DLAB=0 */
+ val = regs[UART_IER];
+ break;
+
+ case UART_IIR: /* RO */
+ val = ns16x50_iir_get(vdev);
break;
case UART_SCR:
@@ -250,6 +357,8 @@ static int ns16x50_io_read8(
*data = val;
+ ns16x50_irq_check(vdev);
+
return rc;
}
@@ -398,6 +507,7 @@ static void ns16x50_reset(struct vuart_ns16x50 *vdev)
const uint16_t divisor = (UART_CLOCK_HZ / 115200) >> 4;
memset(vdev->regs, 0, sizeof(vdev->regs));
+ vdev->irq_asserted = false;
/* NB: report 115200 baud rate. */
vdev->regs[NS16X50_REGS_NUM + UART_DLL] = divisor & UINT8_MAX;
@@ -428,6 +538,8 @@ static int ns16x50_init(void *arg)
ns16x50_reset(vdev);
+ ns16x50_irq_check(vdev);
+
return 0;
}
--
2.56.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [PATCH v8 08/26] emul/ns16x50: implement LCR/LSR registers
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
` (6 preceding siblings ...)
2026-11-11 19:47 ` [PATCH v8 07/26] emul/ns16x50: implement IER/IIR registers dmukhin
@ 2026-11-11 19:47 ` dmukhin
2026-11-17 16:33 ` Oleksii Kurochko
2026-11-11 19:47 ` [PATCH v8 09/26] emul/ns16x50: implement MCR/MSR registers dmukhin
` (18 subsequent siblings)
26 siblings, 1 reply; 39+ messages in thread
From: dmukhin @ 2026-11-11 19:47 UTC (permalink / raw)
To: xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin, Denis Mukhin
From: Denis Mukhin <dmukhin@ford.com>
Add LCR/LSR registers implementation to the I/O port handler.
Add implementation of ns16x50_dlab_get() and ns16x50_iir_check_lsi().
Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
xen/common/emul/vuart/ns16x50.c | 23 ++++++++++++++++++++++-
1 file changed, 22 insertions(+), 1 deletion(-)
diff --git a/xen/common/emul/vuart/ns16x50.c b/xen/common/emul/vuart/ns16x50.c
index 7c5cdc99cfa7..e80815ccf6e2 100644
--- a/xen/common/emul/vuart/ns16x50.c
+++ b/xen/common/emul/vuart/ns16x50.c
@@ -64,6 +64,11 @@ do { \
#define ns16x50_debug(vdev, fmt, args...) \
ns16x50_log(3, XENLOG_DEBUG, vdev, fmt, ## args)
+/*
+ * Helper register definitions.
+ */
+#define UART_LSR_MASK (UART_LSR_OE | UART_LSR_BI)
+
/*
* Number of supported registers in the UART.
*/
@@ -116,7 +121,7 @@ static inline void ns16x50_unlock(struct vuart_ns16x50 *vdev,
static bool cf_check ns16x50_iir_check_lsi(const struct vuart_ns16x50 *vdev)
{
- return false;
+ return vdev->regs[UART_LSR] & UART_LSR_MASK;
}
static bool cf_check ns16x50_iir_check_rda(const struct vuart_ns16x50 *vdev)
@@ -245,11 +250,16 @@ static int ns16x50_io_write8(
}
break;
+ case UART_LCR:
+ regs[UART_LCR] = val;
+ break;
+
/* NB: Firmware (e.g. OVMF) may rely on SCR presence. */
case UART_SCR:
regs[UART_SCR] = val;
break;
+ case UART_LSR: /* RO */
default:
rc = -EINVAL;
break;
@@ -346,6 +356,15 @@ static int ns16x50_io_read8(
val = ns16x50_iir_get(vdev);
break;
+ case UART_LCR:
+ val = regs[UART_LCR];
+ break;
+
+ case UART_LSR:
+ val = regs[UART_LSR];
+ regs[UART_LSR] = val & ~UART_LSR_MASK;
+ break;
+
case UART_SCR:
val = regs[UART_SCR];
break;
@@ -509,6 +528,8 @@ static void ns16x50_reset(struct vuart_ns16x50 *vdev)
memset(vdev->regs, 0, sizeof(vdev->regs));
vdev->irq_asserted = false;
+ vdev->regs[UART_LSR] = UART_LSR_THRE | UART_LSR_TEMT;
+
/* NB: report 115200 baud rate. */
vdev->regs[NS16X50_REGS_NUM + UART_DLL] = divisor & UINT8_MAX;
vdev->regs[NS16X50_REGS_NUM + UART_DLM] = (divisor >> 8) & UINT8_MAX;
--
2.56.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [PATCH v8 09/26] emul/ns16x50: implement MCR/MSR registers
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
` (7 preceding siblings ...)
2026-11-11 19:47 ` [PATCH v8 08/26] emul/ns16x50: implement LCR/LSR registers dmukhin
@ 2026-11-11 19:47 ` dmukhin
2026-11-17 18:32 ` Oleksii Kurochko
2026-11-11 19:47 ` [PATCH v8 10/26] emul/ns16x50: implement RBR register dmukhin
` (17 subsequent siblings)
26 siblings, 1 reply; 39+ messages in thread
From: dmukhin @ 2026-11-11 19:47 UTC (permalink / raw)
To: xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin, Denis Mukhin
From: Denis Mukhin <dmukhin@ford.com>
Add MCR/MSR registers emulation to the I/O port handler.
Add implementation of ns16x50_iir_check_msi().
Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
xen/common/emul/vuart/ns16x50.c | 76 ++++++++++++++++++++++++++++++++-
1 file changed, 75 insertions(+), 1 deletion(-)
diff --git a/xen/common/emul/vuart/ns16x50.c b/xen/common/emul/vuart/ns16x50.c
index e80815ccf6e2..9f530e589495 100644
--- a/xen/common/emul/vuart/ns16x50.c
+++ b/xen/common/emul/vuart/ns16x50.c
@@ -69,6 +69,10 @@ do { \
*/
#define UART_LSR_MASK (UART_LSR_OE | UART_LSR_BI)
+#define UART_MCR_MASK (UART_MCR_DTR | UART_MCR_RTS | \
+ UART_MCR_OUT1 | UART_MCR_OUT2 | \
+ UART_MCR_LOOP)
+
/*
* Number of supported registers in the UART.
*/
@@ -136,7 +140,7 @@ static bool cf_check ns16x50_iir_check_thr(const struct vuart_ns16x50 *vdev)
static bool cf_check ns16x50_iir_check_msi(const struct vuart_ns16x50 *vdev)
{
- return false;
+ return vdev->regs[UART_MSR] & UART_MSR_CHANGE;
}
/*
@@ -193,6 +197,13 @@ static void ns16x50_irq_check(struct vuart_ns16x50 *vdev)
uint8_t iir = ns16x50_iir_get(vdev);
bool asserted = !(iir & UART_IIR_NOINT);
+ /*
+ * On a PC-compatible UART OUT2 gates the ISA interrupt.
+ * Linux'es autoconfig_irq() expects when it toggles MCR.OUT2 during IRQ
+ * probing.
+ */
+ asserted &= !!(vdev->regs[UART_MCR] & UART_MCR_OUT2);
+
if ( asserted == vdev->irq_asserted )
return;
@@ -254,12 +265,63 @@ static int ns16x50_io_write8(
regs[UART_LCR] = val;
break;
+ case UART_MCR: {
+ uint8_t msr_curr, msr_next, msr_delta;
+
+ msr_curr = regs[UART_MSR];
+ msr_next = 0;
+ msr_delta = 0;
+
+ if ( val & UART_MCR_RSRVD0 )
+ ns16x50_warn(vdev, "MCR: attempt to set reserved bit: %#x\n",
+ UART_MCR_RSRVD0);
+
+ if ( val & UART_MCR_TCRTLR )
+ ns16x50_warn(vdev, "MCR: not supported: %#x\n",
+ UART_MCR_TCRTLR);
+
+ if ( val & UART_MCR_RSRVD1 )
+ ns16x50_warn(vdev, "MCR: attempt to set reserved bit: %#x\n",
+ UART_MCR_RSRVD1);
+
+ /* Set modem status */
+ if ( val & UART_MCR_LOOP )
+ {
+ if ( val & UART_MCR_DTR )
+ msr_next |= UART_MSR_DSR;
+ if ( val & UART_MCR_RTS )
+ msr_next |= UART_MSR_CTS;
+ if ( val & UART_MCR_OUT1 )
+ msr_next |= UART_MSR_RI;
+ if ( val & UART_MCR_OUT2 )
+ msr_next |= UART_MSR_DCD;
+ }
+ else
+ msr_next |= UART_MSR_DCD | UART_MSR_DSR | UART_MSR_CTS;
+
+ /* Calculate changes in modem status */
+ if ( (msr_curr & UART_MSR_CTS) ^ (msr_next & UART_MSR_CTS) )
+ msr_delta |= UART_MSR_DCTS;
+ if ( (msr_curr & UART_MSR_DSR) ^ (msr_next & UART_MSR_DSR) )
+ msr_delta |= UART_MSR_DDSR;
+ if ( !(msr_curr & UART_MSR_RI) && (msr_next & UART_MSR_RI) )
+ msr_delta |= UART_MSR_TERI;
+ if ( (msr_curr & UART_MSR_DCD) ^ (msr_next & UART_MSR_DCD) )
+ msr_delta |= UART_MSR_DDCD;
+
+ regs[UART_MCR] = val & UART_MCR_MASK;
+ regs[UART_MSR] = msr_next | msr_delta;
+
+ break;
+ }
+
/* NB: Firmware (e.g. OVMF) may rely on SCR presence. */
case UART_SCR:
regs[UART_SCR] = val;
break;
case UART_LSR: /* RO */
+ case UART_MSR: /* RO */
default:
rc = -EINVAL;
break;
@@ -360,11 +422,20 @@ static int ns16x50_io_read8(
val = regs[UART_LCR];
break;
+ case UART_MCR:
+ val = regs[UART_MCR];
+ break;
+
case UART_LSR:
val = regs[UART_LSR];
regs[UART_LSR] = val & ~UART_LSR_MASK;
break;
+ case UART_MSR:
+ val = regs[UART_MSR];
+ regs[UART_MSR] &= ~UART_MSR_CHANGE;
+ break;
+
case UART_SCR:
val = regs[UART_SCR];
break;
@@ -529,6 +600,9 @@ static void ns16x50_reset(struct vuart_ns16x50 *vdev)
vdev->irq_asserted = false;
vdev->regs[UART_LSR] = UART_LSR_THRE | UART_LSR_TEMT;
+ vdev->regs[UART_MSR] = UART_MSR_DCD |
+ UART_MSR_DSR |
+ UART_MSR_CTS;
/* NB: report 115200 baud rate. */
vdev->regs[NS16X50_REGS_NUM + UART_DLL] = divisor & UINT8_MAX;
--
2.56.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [PATCH v8 10/26] emul/ns16x50: implement RBR register
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
` (8 preceding siblings ...)
2026-11-11 19:47 ` [PATCH v8 09/26] emul/ns16x50: implement MCR/MSR registers dmukhin
@ 2026-11-11 19:47 ` dmukhin
2026-11-11 19:47 ` [PATCH v8 11/26] emul/ns16x50: implement THR register dmukhin
` (16 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: dmukhin @ 2026-11-11 19:47 UTC (permalink / raw)
To: xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin, Denis Mukhin
From: Denis Mukhin <dmukhin@ford.com>
Add RBR register emulation to the I/O port handlder.
Add RX FIFO management code since RBR depends on RX FIFO.
Emulator does not emulate NS8250 (no FIFO), NS16550a (16 bytes)
or NS16750 (64 bytes) precisely for simplicity. No baud rate
emulation either.
RX FIFO is emulated by means of using xencons_interface which
conveniently provides primitives for buffer management and later can be
used for inter-domain communication similarly to vpl011.
Account for DLL == 0: in this case, disable receiver.
Add UART_LSR_DR handling since it depends on RBR register access.
Implement put_rx() vUART hook for placing a character into the
emulated RX FIFO from console driver. That implements physical console
forwarding to the guest OS over emulated NS16550.
Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
xen/common/emul/vuart/ns16x50.c | 132 +++++++++++++++++++++++++++++++-
1 file changed, 130 insertions(+), 2 deletions(-)
diff --git a/xen/common/emul/vuart/ns16x50.c b/xen/common/emul/vuart/ns16x50.c
index 9f530e589495..d04caf2baf2c 100644
--- a/xen/common/emul/vuart/ns16x50.c
+++ b/xen/common/emul/vuart/ns16x50.c
@@ -123,6 +123,76 @@ static inline void ns16x50_unlock(struct vuart_ns16x50 *vdev,
spin_unlock(&vdev->lock);
}
+static bool ns16x50_fifo_rx_empty(const struct vuart_ns16x50 *vdev)
+{
+ const struct xencons_interface *cons = &vdev->cons;
+
+ return cons->in_prod == cons->in_cons;
+}
+
+static bool ns16x50_fifo_rx_full(const struct vuart_ns16x50 *vdev)
+{
+ const struct xencons_interface *cons = &vdev->cons;
+
+ return cons->in_prod - cons->in_cons == ARRAY_SIZE(cons->in);
+}
+
+static void ns16x50_fifo_rx_reset(struct vuart_ns16x50 *vdev)
+{
+ struct xencons_interface *cons = &vdev->cons;
+
+ cons->in_cons = cons->in_prod;
+}
+
+/*
+ * Transfer character from RX FIFO and return the RX FIFO status after the
+ * transfer.
+ */
+static int ns16x50_fifo_rx_getchar(struct vuart_ns16x50 *vdev, uint8_t *ptr)
+{
+ struct xencons_interface *cons = &vdev->cons;
+
+ if ( ns16x50_fifo_rx_empty(vdev) )
+ return -ENODATA;
+
+ *ptr = cons->in[MASK_XENCONS_IDX(cons->in_cons, cons->in)];
+ cons->in_cons++;
+
+ return ns16x50_fifo_rx_empty(vdev) ? -ENODATA : 0;
+}
+
+static int ns16x50_fifo_rx_putchar(struct vuart_ns16x50 *vdev, char c)
+{
+ struct xencons_interface *cons = &vdev->cons;
+ int rc;
+
+ /*
+ * FIFO-less 8250/16450 UARTs: newly arrived word overwrites the contents
+ * of the THR.
+ */
+ if ( ns16x50_fifo_rx_full(vdev) )
+ {
+ ns16x50_debug(vdev, "RX FIFO full; resetting\n");
+ ns16x50_fifo_rx_reset(vdev);
+ rc = -ENOSPC;
+ }
+ else
+ rc = 0;
+
+ cons->in[MASK_XENCONS_IDX(cons->in_prod, cons->in)] = c;
+ cons->in_prod++;
+
+ return rc;
+}
+
+static bool ns16x50_is_tranceiver_enabled(const struct vuart_ns16x50 *vdev)
+{
+ BUG_ON(vdev->regs[UART_LCR] & UART_LCR_DLAB);
+
+ /* DLL set to 0 disables serial communication. */
+ return vdev->regs[NS16X50_REGS_NUM + UART_DLL];
+}
+
static bool cf_check ns16x50_iir_check_lsi(const struct vuart_ns16x50 *vdev)
{
return vdev->regs[UART_LSR] & UART_LSR_MASK;
@@ -130,7 +200,7 @@ static bool cf_check ns16x50_iir_check_lsi(const struct vuart_ns16x50 *vdev)
static bool cf_check ns16x50_iir_check_rda(const struct vuart_ns16x50 *vdev)
{
- return false;
+ return !ns16x50_fifo_rx_empty(vdev);
}
static bool cf_check ns16x50_iir_check_thr(const struct vuart_ns16x50 *vdev)
@@ -400,6 +470,17 @@ static int ns16x50_io_read8(
}
/* DLAB=0 */
+ if ( !ns16x50_is_tranceiver_enabled(vdev) )
+ break;
+
+ /* NB: do not forget to clear overrun condition */
+ regs[UART_LSR] &= ~UART_LSR_OE;
+
+ if ( ns16x50_fifo_rx_getchar(vdev, &val) )
+ regs[UART_LSR] &= ~UART_LSR_DR;
+ else
+ regs[UART_LSR] |= UART_LSR_DR;
+
break;
case UART_IER: /* DLAB=0 --> UART_IER, DLAB=1 --> UART_DLM */
@@ -694,13 +775,60 @@ static void cf_check ns16x50_free(void *arg)
xvfree(arg);
}
+static int cf_check ns16x50_put_rx(void *arg, char ch)
+{
+ struct vuart_ns16x50 *vdev = arg;
+ unsigned long flags;
+ uint8_t *regs;
+ int rc = -EBUSY;
+
+ ASSERT(vdev);
+
+ ns16x50_lock(vdev, &flags);
+
+ regs = vdev->regs;
+
+ if ( vdev->regs[UART_LCR] & UART_LCR_DLAB )
+ ns16x50_debug(vdev, "THR/RBR access disabled: DLAB=1\n");
+ else if ( !ns16x50_is_tranceiver_enabled(vdev) )
+ ns16x50_debug(vdev, "THR/RBR access disabled: DLAB=0 DLL=0\n");
+ else if ( regs[UART_MCR] & UART_MCR_LOOP )
+ ns16x50_debug(vdev, "THR/RBR access disabled: DLAB=0 loopback mode\n");
+ else
+ {
+ const struct domain *d = vdev->owner;
+
+ /*
+ * Echo the user input on Xen console iff Xen console input is owned
+ * by ns16x50 domain.
+ * NB: use 'console_timestamps=none' to disable Xen timestamps.
+ */
+ if ( is_console_printable(ch) )
+ guest_printk(d, "%c", ch);
+
+ if ( ns16x50_fifo_rx_putchar(vdev, ch) )
+ regs[UART_LSR] |= UART_LSR_OE;
+
+ regs[UART_LSR] |= UART_LSR_DR;
+
+ /* TODO: check FCR when to fire an interrupt */
+ ns16x50_irq_check(vdev);
+
+ rc = 0;
+ }
+
+ ns16x50_unlock(vdev, &flags);
+
+ return rc;
+}
+
#define ns16x50_emulator \
{ \
.compatible = "ns16550", \
.alloc = ns16x50_alloc, \
.free = ns16x50_free, \
.dump_state = NULL, \
- .put_rx = NULL, \
+ .put_rx = ns16x50_put_rx, \
}
VUART_REGISTER(ns16x50, ns16x50_emulator);
--
2.56.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [PATCH v8 11/26] emul/ns16x50: implement THR register
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
` (9 preceding siblings ...)
2026-11-11 19:47 ` [PATCH v8 10/26] emul/ns16x50: implement RBR register dmukhin
@ 2026-11-11 19:47 ` dmukhin
2026-11-11 19:47 ` [PATCH v8 12/26] emul/ns16x50: implement FCR register (write-only) dmukhin
` (15 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: dmukhin @ 2026-11-11 19:47 UTC (permalink / raw)
To: xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin, Denis Mukhin
From: Denis Mukhin <dmukhin@ford.com>
Add THR register emulation to the I/O port handlder.
Add TX FIFO management code since THR depends on TX FIFO.
TX FIFOs is emulated by using xencons_interface which conveniently
provides primitives for buffer management and later can be used for
inter-domain communication similarly to vpl011.
Account for DLL == 0: in this case, disable transmitter.
Add UART_IIR_THR interrupt reason handling since it depends on THR
register access.
Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
xen/common/emul/vuart/ns16x50.c | 121 +++++++++++++++++++++++++++++++-
1 file changed, 120 insertions(+), 1 deletion(-)
diff --git a/xen/common/emul/vuart/ns16x50.c b/xen/common/emul/vuart/ns16x50.c
index d04caf2baf2c..8d3845a8a4cd 100644
--- a/xen/common/emul/vuart/ns16x50.c
+++ b/xen/common/emul/vuart/ns16x50.c
@@ -98,6 +98,7 @@ struct vuart_ns16x50 {
spinlock_t lock; /* Protection */
struct xencons_interface cons; /* Emulated RX/TX FIFOs */
bool irq_asserted; /* IRQ line status */
+ bool thr_pending; /* THR interrupt reason status */
};
static inline void ns16x50_lock_init(struct vuart_ns16x50 *vdev)
@@ -185,6 +186,72 @@ static int ns16x50_fifo_rx_putchar(struct vuart_ns16x50 *vdev, char c)
return rc;
}
+static bool ns16x50_fifo_tx_full(const struct vuart_ns16x50 *vdev)
+{
+ const struct xencons_interface *cons = &vdev->cons;
+
+ return cons->out_prod - cons->out_cons == ARRAY_SIZE(cons->out);
+}
+
+static void ns16x50_fifo_tx_reset(struct vuart_ns16x50 *vdev)
+{
+ struct xencons_interface *cons = &vdev->cons;
+
+ cons->out_cons = cons->out_prod;
+}
+
+/*
+ * Flush cached output to Xen console.
+ */
+static void ns16x50_fifo_tx_flush(struct vuart_ns16x50 *vdev)
+{
+ struct xencons_interface *cons = &vdev->cons;
+ struct domain *d = vdev->owner;
+ XENCONS_RING_IDX i, j, n, m, len = cons->out_prod - cons->out_cons;
+
+ ASSERT(len <= ARRAY_SIZE(cons->out));
+ if ( !len )
+ return;
+
+ /* Tail of the FIFO */
+ i = MASK_XENCONS_IDX(cons->out_cons, cons->out);
+ n = min_t(XENCONS_RING_IDX, len, ARRAY_SIZE(cons->out) - i);
+
+ /* Head of the FIFO */
+ j = 0;
+ m = len - n;
+
+ /* Linearize the FIFO */
+ if ( m )
+ guest_printk(d, guest_prefix "%.*s%.*s",
+ n, &cons->out[i],
+ m, &cons->out[j]);
+ else
+ guest_printk(d, guest_prefix "%.*s", n, &cons->out[i]);
+
+ cons->out_cons += len;
+}
+
+/*
+ * Accumulate guest OS output before sending to Xen console.
+ */
+static void ns16x50_fifo_tx_putchar(struct vuart_ns16x50 *vdev, char ch)
+{
+ struct xencons_interface *cons = &vdev->cons;
+
+ if ( !is_console_printable(ch) )
+ return;
+
+ if ( !ns16x50_fifo_tx_full(vdev) )
+ {
+ cons->out[MASK_XENCONS_IDX(cons->out_prod, cons->out)] = ch;
+ cons->out_prod++;
+ }
+
+ if ( ch == '\n' || ch == '\0' || ns16x50_fifo_tx_full(vdev) )
+ ns16x50_fifo_tx_flush(vdev);
+}
+
static bool ns16x50_is_tranceiver_enabled(const struct vuart_ns16x50 *vdev)
{
BUG_ON(vdev->regs[UART_LCR] & UART_LCR_DLAB);
@@ -205,7 +272,7 @@ static bool cf_check ns16x50_iir_check_rda(const struct vuart_ns16x50 *vdev)
static bool cf_check ns16x50_iir_check_thr(const struct vuart_ns16x50 *vdev)
{
- return false;
+ return vdev->thr_pending;
}
static bool cf_check ns16x50_iir_check_msi(const struct vuart_ns16x50 *vdev)
@@ -316,6 +383,38 @@ static int ns16x50_io_write8(
}
/* DLAB=0 */
+ if ( !ns16x50_is_tranceiver_enabled(vdev) )
+ break;
+
+ /*
+ * THR is no longer empty.
+ */
+ vdev->thr_pending = false;
+ regs[UART_LSR] &= ~(UART_LSR_THRE | UART_LSR_TEMT);
+
+ /*
+ * Drop the IRQ line before completing this immediate
+ * transmission, if THRE was its only reason.
+ */
+ ns16x50_irq_check(vdev);
+
+ if ( regs[UART_MCR] & UART_MCR_LOOP )
+ {
+ if ( ns16x50_fifo_rx_putchar(vdev, val) )
+ regs[UART_LSR] |= UART_LSR_OE;
+
+ regs[UART_LSR] |= UART_LSR_DR;
+ }
+ else
+ ns16x50_fifo_tx_putchar(vdev, val);
+
+ /*
+ * Backend transmission is synchronous in this emulator,
+ * so THR is immediately empty again.
+ */
+ regs[UART_LSR] |= UART_LSR_THRE | UART_LSR_TEMT;
+ vdev->thr_pending = true;
+
break;
case UART_IER: /* DLAB=0 --> UART_IER, DLAB=1 --> UART_DLM */
@@ -327,7 +426,13 @@ static int ns16x50_io_write8(
/* DLAB=0 */
else
{
+ uint8_t old = regs[UART_IER];
+
regs[UART_IER] = val & UART_IER_ALL_INTR;
+
+ if ( (old ^ regs[UART_IER]) & UART_IER_ETHREI )
+ vdev->thr_pending = ( (regs[UART_IER] & UART_IER_ETHREI) &&
+ (regs[UART_LSR] & UART_LSR_THRE) );
}
break;
@@ -497,6 +602,14 @@ static int ns16x50_io_read8(
case UART_IIR: /* RO */
val = ns16x50_iir_get(vdev);
+
+ /*
+ * Reading IIR when THRE is the reported reason clears the
+ * THRE interrupt latch.
+ */
+ if ( (val & UART_IIR_IMA) == UART_IIR_THR )
+ vdev->thr_pending = false;
+
break;
case UART_LCR:
@@ -679,6 +792,7 @@ static void ns16x50_reset(struct vuart_ns16x50 *vdev)
memset(vdev->regs, 0, sizeof(vdev->regs));
vdev->irq_asserted = false;
+ vdev->thr_pending = false;
vdev->regs[UART_LSR] = UART_LSR_THRE | UART_LSR_TEMT;
vdev->regs[UART_MSR] = UART_MSR_DCD |
@@ -722,8 +836,13 @@ static int ns16x50_init(void *arg)
static void cf_check ns16x50_deinit(void *arg)
{
struct vuart_ns16x50 *vdev = arg;
+ unsigned long flags;
ASSERT(vdev);
+
+ ns16x50_lock(vdev, &flags);
+ ns16x50_fifo_tx_flush(vdev);
+ ns16x50_unlock(vdev, &flags);
}
static void * cf_check ns16x50_alloc(struct domain *d, const struct vuart_info *info)
--
2.56.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [PATCH v8 12/26] emul/ns16x50: implement FCR register (write-only)
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
` (10 preceding siblings ...)
2026-11-11 19:47 ` [PATCH v8 11/26] emul/ns16x50: implement THR register dmukhin
@ 2026-11-11 19:47 ` dmukhin
2026-11-11 19:47 ` [PATCH v8 13/26] emul/ns16550: implement dump_state() hook dmukhin
` (14 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: dmukhin @ 2026-11-11 19:47 UTC (permalink / raw)
To: xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin, Denis Mukhin
From: Denis Mukhin <dmukhin@ford.com>
Add emulation logic for FCR register.
Note, that does not hook FIFO interrupt moderation to the FIFO
management code for simplicity.
Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
xen/common/emul/vuart/ns16x50.c | 33 +++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+)
diff --git a/xen/common/emul/vuart/ns16x50.c b/xen/common/emul/vuart/ns16x50.c
index 8d3845a8a4cd..404e12311090 100644
--- a/xen/common/emul/vuart/ns16x50.c
+++ b/xen/common/emul/vuart/ns16x50.c
@@ -436,6 +436,39 @@ static int ns16x50_io_write8(
}
break;
+ case UART_FCR: /* WO */
+ if ( val & UART_FCR_RSRVD0 )
+ ns16x50_warn(vdev, "FCR: attempt to set reserved bit: %x\n",
+ UART_FCR_RSRVD0);
+
+ if ( val & UART_FCR_RSRVD1 )
+ ns16x50_warn(vdev, "FCR: attempt to set reserved bit: %x\n",
+ UART_FCR_RSRVD1);
+
+ if ( val & UART_FCR_CLRX )
+ {
+ ns16x50_fifo_rx_reset(vdev);
+ regs[UART_LSR] &= ~UART_LSR_DR;
+ }
+
+ if ( val & UART_FCR_CLTX )
+ {
+ ns16x50_fifo_tx_reset(vdev);
+
+ regs[UART_LSR] |= UART_LSR_THRE | UART_LSR_TEMT;
+ vdev->thr_pending = true;
+ }
+
+ if ( val & UART_FCR_ENABLE )
+ val &= UART_FCR_ENABLE | UART_FCR_DMA |
+ UART_FCR_RTB0 | UART_FCR_RTB1;
+ else
+ val = 0;
+
+ regs[UART_FCR] = val;
+
+ break;
+
case UART_LCR:
regs[UART_LCR] = val;
break;
--
2.56.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [PATCH v8 13/26] emul/ns16550: implement dump_state() hook
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
` (11 preceding siblings ...)
2026-11-11 19:47 ` [PATCH v8 12/26] emul/ns16x50: implement FCR register (write-only) dmukhin
@ 2026-11-11 19:47 ` dmukhin
2026-11-11 19:47 ` [PATCH v8 14/26] emul/vuart: add global UART emulator configuration knob dmukhin
` (13 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: dmukhin @ 2026-11-11 19:47 UTC (permalink / raw)
To: xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin, Denis Mukhin
From: Denis Mukhin <dmukhin@ford.com>
Implement dump_state() vUART hook for debugging UART state machine over
Xen console. dump_state() prints state of all emulated registers
(including state-less IIR) and state of RX/TX FIFOs.
Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
xen/common/emul/vuart/ns16x50.c | 64 ++++++++++++++++++++++++++++++++-
1 file changed, 63 insertions(+), 1 deletion(-)
diff --git a/xen/common/emul/vuart/ns16x50.c b/xen/common/emul/vuart/ns16x50.c
index 404e12311090..cda21c15cf78 100644
--- a/xen/common/emul/vuart/ns16x50.c
+++ b/xen/common/emul/vuart/ns16x50.c
@@ -728,6 +728,62 @@ static int ns16x50_io_read(
return rc;
}
+static void cf_check ns16x50_dump_state(void *arg)
+{
+#ifdef CONFIG_VUART_NS16X50_DEBUG
+ struct vuart_ns16x50 *vdev = arg;
+ const struct domain *d;
+ const struct vuart_info *info;
+ const struct xencons_interface *cons;
+ const uint8_t *regs;
+ const char *as = (info->base_type & IORESOURCE_IO) ? "ioport":
+ "iomem";
+ unsigned long flags;
+
+ ASSERT(vdev);
+
+ /* Allow printing state in case of a deadlock. */
+ if ( !ns16x50_trylock(vdev, &flags) )
+ return;
+
+ d = vdev->owner;
+ info = vdev->info;
+ cons = &vdev->cons;
+ regs = &vdev->regs[0];
+
+ printk("Virtual " pr_prefix " (%s) %s %#"PRIx64" IRQ#%d owner %pd\n",
+ vdev->name, as, info->base_addr, info->irq, d);
+
+ printk(" RX FIFO size %ld in_prod %d in_cons %d used %d\n",
+ ARRAY_SIZE(cons->in), cons->in_prod, cons->in_cons,
+ cons->in_prod - cons->in_cons);
+
+ printk(" TX FIFO size %ld out_prod %d out_cons %d used %d\n",
+ ARRAY_SIZE(cons->out), cons->out_prod, cons->out_cons,
+ cons->out_prod - cons->out_cons);
+
+ printk(" %02"PRIx8" RBR %02"PRIx8" THR %02"PRIx8" DLL %02"PRIx8" DLM %02"PRIx8"\n",
+ UART_RBR,
+ cons->in[MASK_XENCONS_IDX(cons->in_prod, cons->in)],
+ cons->out[MASK_XENCONS_IDX(cons->out_prod, cons->out)],
+ regs[NS16X50_REGS_NUM + UART_DLL],
+ regs[NS16X50_REGS_NUM + UART_DLM]);
+
+ printk(" %02"PRIx8" IER %02"PRIx8"\n", UART_IER, regs[UART_IER]);
+
+ printk(" %02"PRIx8" FCR %02"PRIx8" IIR %02"PRIx8"\n",
+ UART_FCR, regs[UART_FCR], ns16x50_iir_get(vdev));
+
+ printk(" %02"PRIx8" LCR %02"PRIx8"\n", UART_LCR, regs[UART_LCR]);
+ printk(" %02"PRIx8" MCR %02"PRIx8"\n", UART_MCR, regs[UART_MCR]);
+ printk(" %02"PRIx8" LSR %02"PRIx8"\n", UART_LSR, regs[UART_LSR]);
+ printk(" %02"PRIx8" MSR %02"PRIx8"\n", UART_MSR, regs[UART_MSR]);
+ printk(" %02"PRIx8" SCR %02"PRIx8"\n", UART_SCR, regs[UART_SCR]);
+
+ ns16x50_unlock(vdev, &flags);
+#endif /* CONFIG_VUART_NS16X50_DEBUG */
+}
+
static void ns16x50_access(int dir, unsigned int type,
unsigned long addr, unsigned int size,
uint32_t *data)
@@ -794,6 +850,9 @@ static void ns16x50_access(int dir, unsigned int type,
rc = ns16x50_io_read(vdev, reg, size, data);
+ if ( ns16x50_log_level >= 3 )
+ ns16x50_dump_state(vdev);
+
if ( rc == 0 )
ns16x50_debug(vdev, "%c %s %#"PRIx64" %d: LCR=%#02x %#02x %#08x\n",
op, as, addr, size, vdev->regs[UART_LCR], reg, *data);
@@ -971,6 +1030,9 @@ static int cf_check ns16x50_put_rx(void *arg, char ch)
ns16x50_unlock(vdev, &flags);
+ if ( ns16x50_log_level >= 3 )
+ ns16x50_dump_state(vdev);
+
return rc;
}
@@ -979,7 +1041,7 @@ static int cf_check ns16x50_put_rx(void *arg, char ch)
.compatible = "ns16550", \
.alloc = ns16x50_alloc, \
.free = ns16x50_free, \
- .dump_state = NULL, \
+ .dump_state = ns16x50_dump_state, \
.put_rx = ns16x50_put_rx, \
}
--
2.56.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [PATCH v8 14/26] emul/vuart: add global UART emulator configuration knob
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
` (12 preceding siblings ...)
2026-11-11 19:47 ` [PATCH v8 13/26] emul/ns16550: implement dump_state() hook dmukhin
@ 2026-11-11 19:47 ` dmukhin
2026-11-11 19:47 ` [PATCH v8 15/26] emul/ns16x50: enable emulator dmukhin
` (12 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: dmukhin @ 2026-11-11 19:47 UTC (permalink / raw)
To: xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin, Denis Mukhin
From: Denis Mukhin <dmukhin@ford.com>
Introduce new command line setting `vuart=domid[:compatible,name]` for
configuring UART emulator per given domain.
Only one domain is supported for now.
Format:
domid - vUART owner domain ID
compatible - vUART compatible string, must match with one of the
registered emulators
name - vUART user-friendly name used for searching for one of
the pre-defined guest I/O resources
compatible string and name are optional, if skipped, platform default
values will be chosen.
Note, documentation update is not covered in this patch.
Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
xen/common/emul/vuart/vuart.c | 85 +++++++++++++++++++++++++++++++++++
1 file changed, 85 insertions(+)
diff --git a/xen/common/emul/vuart/vuart.c b/xen/common/emul/vuart/vuart.c
index bd3b756fb4d6..0df29540b1b6 100644
--- a/xen/common/emul/vuart/vuart.c
+++ b/xen/common/emul/vuart/vuart.c
@@ -6,8 +6,10 @@
#include <asm/types.h>
+#include <xen/ctype.h>
#include <xen/err.h>
#include <xen/resource.h>
+#include <xen/param.h>
#include <xen/sched.h>
#include <xen/vuart.h>
#include <xen/xvmalloc.h>
@@ -18,6 +20,89 @@
extern const struct vuart_emulator vuart_array_start[];
extern const struct vuart_emulator vuart_array_end[];
+static domid_t __read_mostly opt_vuart_domid = DOMID_INVALID;
+static const struct vuart_info * __read_mostly opt_vuart_info;
+
+static const struct vuart_info vuart_supported[] = {
+#define VUART(c,n,t,a,s,i) { \
+ .compatible = c, \
+ .name = n, \
+ .flags = VUART_CONSOLE_INPUT, \
+ .base_type = t, \
+ .base_addr = a, \
+ .size = s, \
+ .irq = i, \
+}
+#ifdef CONFIG_VUART_NS16X50
+ VUART("ns16550", "com1", IORESOURCE_IO, 0x3f8u, 8u, 4u),
+ VUART("ns16550", "com2", IORESOURCE_IO, 0x2f8u, 8u, 3u),
+ VUART("ns16550", "com3", IORESOURCE_IO, 0x3feu, 8u, 4u),
+ VUART("ns16550", "com4", IORESOURCE_IO, 0x2feu, 8u, 3u),
+#endif /* CONFIG_VUART_NS16X50 */
+#undef VUART
+};
+
+static int __init cf_check vuart_parse_cmdline(const char *s)
+{
+ const char *compat = "ns16550";
+ size_t compat_len = strlen(compat);
+ const char *name = "com1";
+ size_t name_len = strlen(name);
+ const char *end;
+ domid_t domid;
+ unsigned int i;
+
+ /* vuart=domid:compatible,name */
+ if ( !isdigit(*s) )
+ return -EINVAL;
+
+ domid = simple_strtoul(s, &end, 0);
+ if ( domid >= DOMID_FIRST_RESERVED )
+ return -EINVAL;
+
+ if ( *end == '\0' )
+ goto match_defaults;
+
+ if ( *end != ':' )
+ return -EINVAL;
+
+ compat = end + 1;
+
+ while ( *end && *end != ',' )
+ end++;
+ if ( *end != ',' )
+ return -EINVAL;
+
+ compat_len = end - compat;
+ if ( !compat_len )
+ return -EINVAL;
+
+ name = end + 1;
+ name_len = strlen(name);
+ if ( !name_len )
+ return -EINVAL;
+
+ match_defaults:
+ for ( i = 0; i < ARRAY_SIZE(vuart_supported); i++ )
+ {
+ if ( compat_len != strlen(vuart_supported[i].compatible) ||
+ strncmp(compat, vuart_supported[i].compatible, compat_len) )
+ continue;
+
+ if ( name_len != strlen(vuart_supported[i].name) ||
+ strncmp(name, vuart_supported[i].name, name_len) )
+ continue;
+
+ opt_vuart_info = &vuart_supported[i];
+ opt_vuart_domid = domid;
+
+ return 0;
+ }
+
+ return -EINVAL;
+}
+custom_param("vuart", vuart_parse_cmdline);
+
static const struct vuart_emulator *
vuart_match_by_compatible(const struct domain *d, const char *compat)
{
--
2.56.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [PATCH v8 15/26] emul/ns16x50: enable emulator
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
` (13 preceding siblings ...)
2026-11-11 19:47 ` [PATCH v8 14/26] emul/vuart: add global UART emulator configuration knob dmukhin
@ 2026-11-11 19:47 ` dmukhin
2026-11-11 19:47 ` [PATCH v8 16/26] emul/ns16x50: add Kconfig options dmukhin
` (11 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: dmukhin @ 2026-11-11 19:47 UTC (permalink / raw)
To: xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin, Denis Mukhin
From: Denis Mukhin <dmukhin@ford.com>
Wire emulator to domain creation code.
Introduce a helper `vuart_probe()` call to get the parsed vUART
configuration settings and use it while constructing domain with vUART.
Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
xen/arch/x86/hvm/hvm.c | 9 +++++++++
xen/common/emul/vuart/vuart.c | 8 ++++++++
xen/include/xen/vuart.h | 6 ++++++
3 files changed, 23 insertions(+)
diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c
index ef9c7cd05777..f4d2fbe75fc6 100644
--- a/xen/arch/x86/hvm/hvm.c
+++ b/xen/arch/x86/hvm/hvm.c
@@ -29,6 +29,7 @@
#include <xen/trace.h>
#include <xen/vm_event.h>
#include <xen/vpci.h>
+#include <xen/vuart.h>
#include <xen/wait.h>
#include <xen/warning.h>
@@ -699,6 +700,10 @@ int hvm_domain_initialise(struct domain *d,
if ( rc != 0 )
goto fail1;
+ rc = vuart_init(d, vuart_probe(d));
+ if ( rc )
+ goto out_vioapic_deinit;
+
stdvga_init(d);
rtc_init(d);
@@ -722,6 +727,8 @@ int hvm_domain_initialise(struct domain *d,
return 0;
fail2:
+ vuart_deinit(d);
+ out_vioapic_deinit:
vioapic_deinit(d);
fail1:
if ( is_hardware_domain(d) )
@@ -784,6 +791,8 @@ void hvm_domain_destroy(struct domain *d)
if ( hvm_funcs.domain_destroy )
alternative_vcall(hvm_funcs.domain_destroy, d);
+ vuart_deinit(d);
+
vioapic_deinit(d);
XFREE(d->arch.hvm.pl_time);
diff --git a/xen/common/emul/vuart/vuart.c b/xen/common/emul/vuart/vuart.c
index 0df29540b1b6..b2680711ad09 100644
--- a/xen/common/emul/vuart/vuart.c
+++ b/xen/common/emul/vuart/vuart.c
@@ -144,6 +144,14 @@ struct vuart *vuart_find_by_resource(const struct domain *d,
return NULL;
}
+const struct vuart_info *vuart_probe(const struct domain *d)
+{
+ if ( d->domain_id == opt_vuart_domid )
+ return opt_vuart_info;
+
+ return NULL;
+}
+
int vuart_init(struct domain *d, const struct vuart_info *info)
{
const struct vuart_emulator *emulator;
diff --git a/xen/include/xen/vuart.h b/xen/include/xen/vuart.h
index b5785b3c6b8f..43c6759c1f97 100644
--- a/xen/include/xen/vuart.h
+++ b/xen/include/xen/vuart.h
@@ -75,6 +75,7 @@ int vuart_put_rx(const struct domain *d, char c);
#ifdef CONFIG_VUART_FRAMEWORK
+const struct vuart_info *vuart_probe(const struct domain *d);
int vuart_init(struct domain *d, const struct vuart_info *info);
void vuart_deinit(struct domain *d);
void vuart_dump_state(const struct domain *d);
@@ -82,6 +83,11 @@ bool domain_has_vuart(const struct domain *d);
#else
+static inline const struct vuart_info *vuart_probe(const struct domain *d)
+{
+ return NULL;
+}
+
static inline int vuart_init(struct domain *d, const struct vuart_info *info)
{
return 0;
--
2.56.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [PATCH v8 16/26] emul/ns16x50: add Kconfig options
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
` (14 preceding siblings ...)
2026-11-11 19:47 ` [PATCH v8 15/26] emul/ns16x50: enable emulator dmukhin
@ 2026-11-11 19:47 ` dmukhin
2026-11-11 19:47 ` [PATCH v8 17/26] x86/domain: enable per-domain I/O port bitmaps dmukhin
` (10 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: dmukhin @ 2026-11-11 19:47 UTC (permalink / raw)
To: xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin, Denis Mukhin
From: Denis Mukhin <dmukhin@ford.com>
Add initial Kconfig options configure NS16550-capable emulator.
Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
xen/common/emul/vuart/Kconfig | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git a/xen/common/emul/vuart/Kconfig b/xen/common/emul/vuart/Kconfig
index ce1b976b7da7..353a6325180e 100644
--- a/xen/common/emul/vuart/Kconfig
+++ b/xen/common/emul/vuart/Kconfig
@@ -3,4 +3,24 @@ config VUART_FRAMEWORK
menu "UART Emulation"
+config VUART_NS16X50
+ bool "NS16550-compatible UART Emulator" if EXPERT
+ depends on X86 && HVM
+ select VUART_FRAMEWORK
+ help
+ In-hypervisor NS16550-compatible UART emulation.
+
+ On x86, only one legacy PC COM port is emulated for domain with a
+ certain ID (set via 'vuart=<domid>:<compatible>[,com[1-4]]' command line
+ setting).
+
+ This is strictly for testing purposes (such as early HVM guest console),
+ and not appropriate for use in production.
+
+config VUART_NS16X50_DEBUG
+ bool "Development: NS16550-compatible UART Emulator Debugging"
+ depends on VUART_NS16X50 && DEBUG
+ help
+ Enable development debugging.
+
endmenu
--
2.56.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [PATCH v8 17/26] x86/domain: enable per-domain I/O port bitmaps
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
` (15 preceding siblings ...)
2026-11-11 19:47 ` [PATCH v8 16/26] emul/ns16x50: add Kconfig options dmukhin
@ 2026-11-11 19:47 ` dmukhin
2026-11-11 19:47 ` [PATCH v8 18/26] emul/ns16x50: resolve physical I/O port sharing dmukhin
` (9 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: dmukhin @ 2026-11-11 19:47 UTC (permalink / raw)
To: xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin, Denis Mukhin
From: Denis Mukhin <dmukhin@ford.com>
Current design enables all HVM domains share the same I/O port bitmap.
It is necessary for domains crafting its own I/O port address space
depending on the user configuration.
No functional change intended.
Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
xen/arch/x86/Makefile | 1 +
xen/arch/x86/dom0_build.c | 111 +--------------
xen/arch/x86/hvm/hvm.c | 40 ++----
xen/arch/x86/hvm/nestedhvm.c | 8 +-
xen/arch/x86/hvm/quirks.c | 3 -
xen/arch/x86/hvm/svm/nestedsvm.c | 2 +-
xen/arch/x86/hvm/vmx/vvmx.c | 4 +-
xen/arch/x86/include/asm/hvm/nestedhvm.h | 3 +-
xen/arch/x86/include/asm/hvm/support.h | 2 -
xen/arch/x86/include/asm/iocap.h | 3 +
xen/arch/x86/ioport.c | 168 +++++++++++++++++++++++
xen/arch/x86/pv/dom0_build.c | 4 +
12 files changed, 198 insertions(+), 151 deletions(-)
create mode 100644 xen/arch/x86/ioport.c
diff --git a/xen/arch/x86/Makefile b/xen/arch/x86/Makefile
index 2be90ea09880..749b70444bb6 100644
--- a/xen/arch/x86/Makefile
+++ b/xen/arch/x86/Makefile
@@ -42,6 +42,7 @@ obj-y += i387.o
obj-y += i8259.o
obj-$(CONFIG_INDIRECT_THUNK) += indirect-thunk.o
obj-$(CONFIG_RETURN_THUNK) += indirect-thunk.o
+obj-y += ioport.o
obj-$(CONFIG_PV) += ioport_emulate.o
obj-y += io_apic.o
obj-y += irq.o
diff --git a/xen/arch/x86/dom0_build.c b/xen/arch/x86/dom0_build.c
index 80308ca9af46..e8f783d533af 100644
--- a/xen/arch/x86/dom0_build.c
+++ b/xen/arch/x86/dom0_build.c
@@ -298,9 +298,6 @@ int __init parse_arch_dom0_param(const char *s, const char *e)
return 0;
}
-static char __initdata opt_dom0_ioports_disable[200] = "";
-string_param("dom0_ioports_disable", opt_dom0_ioports_disable);
-
static bool __initdata ro_hpet = true;
boolean_param("ro-hpet", ro_hpet);
@@ -445,122 +442,20 @@ unsigned long __init dom0_compute_nr_pages(
return nr_pages;
}
-static void __init process_dom0_ioports_disable(struct domain *dom0)
-{
- unsigned long io_from, io_to;
- char *t, *s = opt_dom0_ioports_disable;
- const char *u;
-
- if ( *s == '\0' )
- return;
-
- while ( (t = strsep(&s, ",")) != NULL )
- {
- io_from = simple_strtoul(t, &u, 16);
- if ( u == t )
- {
- parse_error:
- printk("Invalid ioport range <%s> "
- "in dom0_ioports_disable, skipping\n", t);
- continue;
- }
-
- if ( *u == '\0' )
- io_to = io_from;
- else if ( *u == '-' )
- io_to = simple_strtoul(u + 1, &u, 16);
- else
- goto parse_error;
-
- if ( (*u != '\0') || (io_to < io_from) || (io_to >= 65536) )
- goto parse_error;
-
- printk("Disabling dom0 access to ioport range %04lx-%04lx\n",
- io_from, io_to);
-
- if ( ioports_deny_access(dom0, io_from, io_to) != 0 )
- BUG();
- }
-}
-
+/* Modify I/O memory access permissions. */
int __init dom0_setup_permissions(struct domain *d)
{
unsigned long mfn;
- unsigned int i, offs;
- int rc;
+ unsigned int i;
+ int rc = 0;
if ( pv_shim )
return 0;
- /* The hardware domain is initially permitted full I/O capabilities. */
- rc = ioports_permit_access(d, 0, 0xFFFF);
rc |= iomem_permit_access(d, 0UL,
PFN_DOWN(1UL << domain_max_paddr_bits(d)) - 1);
rc |= irqs_permit_access(d, 1, nr_irqs_gsi - 1);
- /* Modify I/O port access permissions. */
-
- for ( offs = 0, i = ISOLATE_LSB(i8259A_alias_mask) ?: 2;
- offs <= i8259A_alias_mask; offs += i )
- {
- if ( offs & ~i8259A_alias_mask )
- continue;
- /* Master Interrupt Controller (PIC). */
- rc |= ioports_deny_access(d, 0x20 + offs, 0x21 + offs);
- /* Slave Interrupt Controller (PIC). */
- rc |= ioports_deny_access(d, 0xA0 + offs, 0xA1 + offs);
- }
-
- /* ELCR of both PICs. */
- rc |= ioports_deny_access(d, 0x4D0, 0x4D1);
-
- /* Interval Timer (PIT). */
- for ( offs = 0, i = ISOLATE_LSB(pit_alias_mask) ?: 4;
- offs <= pit_alias_mask; offs += i )
- if ( !(offs & ~pit_alias_mask) )
- rc |= ioports_deny_access(d, PIT_CH0 + offs, PIT_MODE + offs);
-
- /* PIT Channel 2 / PC Speaker Control. */
- rc |= ioports_deny_access(d, 0x61, 0x61);
-
- /* INIT# and alternative A20M# control. */
- rc |= ioports_deny_access(d, 0x92, 0x92);
-
- /* IGNNE# control. */
- rc |= ioports_deny_access(d, 0xF0, 0xF0);
-
- /* ACPI PM Timer. */
- if ( pmtmr_ioport )
- rc |= ioports_deny_access(d, pmtmr_ioport, pmtmr_ioport + 3);
-
- /* Reset control. */
- rc |= ioports_deny_access(d, 0xCF9, 0xCF9);
-
- /* PCI configuration space (NB. 0xCF8 has special treatment). */
- rc |= ioports_deny_access(d, 0xCFC, 0xCFF);
-
-#ifdef CONFIG_HVM
- if ( is_hvm_domain(d) )
- {
- /* ISA DMA controller, channels 0-3 (incl possible aliases). */
- rc |= ioports_deny_access(d, 0x00, 0x1F);
- /* ISA DMA controller, page registers (incl various reserved ones). */
- rc |= ioports_deny_access(d, 0x80 + !!hvm_port80_allowed, 0x8F);
- /* ISA DMA controller, channels 4-7 (incl usual aliases). */
- rc |= ioports_deny_access(d, 0xC0, 0xDF);
-
- /* HVM debug console IO port. */
- rc |= ioports_deny_access(d, XEN_HVM_DEBUGCONS_IOPORT,
- XEN_HVM_DEBUGCONS_IOPORT);
- if ( amd_acpi_c1e_quirk )
- rc |= ioports_deny_access(d, acpi_smi_cmd, acpi_smi_cmd);
- }
-#endif
- /* Command-line I/O ranges. */
- process_dom0_ioports_disable(d);
-
- /* Modify I/O memory access permissions. */
-
/* Local APIC. */
if ( mp_lapic_addr != 0 )
{
diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c
index f4d2fbe75fc6..9f0e0f6d8c3d 100644
--- a/xen/arch/x86/hvm/hvm.c
+++ b/xen/arch/x86/hvm/hvm.c
@@ -51,6 +51,7 @@
#include <asm/hvm/vm_event.h>
#include <asm/hvm/vpt.h>
#include <asm/i387.h>
+#include <asm/iocap.h>
#include <asm/mc146818rtc.h>
#include <asm/mce.h>
#include <asm/mem_access.h>
@@ -82,14 +83,6 @@ integer_param("hvm_debug", opt_hvm_debug_level);
struct hvm_function_table __ro_after_init hvm_funcs;
-/*
- * The I/O permission bitmap is globally shared by all HVM guests except
- * the hardware domain which needs a more permissive one.
- */
-#define HVM_IOBITMAP_SIZE (3 * PAGE_SIZE)
-unsigned long __section(".bss.page_aligned") __aligned(PAGE_SIZE)
- hvm_io_bitmap[HVM_IOBITMAP_SIZE / BYTES_PER_LONG];
-
/* Xen command-line option to enable HAP */
static bool __initdata opt_hap_enabled = true;
boolean_param("hap", opt_hap_enabled);
@@ -206,15 +199,6 @@ static int __init cf_check hvm_enable(void)
if ( opt_hvm_fep )
warning_add(warning_hvm_fep);
- /*
- * Allow direct access to the PC debug ports 0x80 and 0xed (they are
- * often used for I/O delays, but the vmexits simply slow things down).
- */
- memset(hvm_io_bitmap, ~0, sizeof(hvm_io_bitmap));
- if ( hvm_port80_allowed )
- __clear_bit(0x80, hvm_io_bitmap);
- __clear_bit(0xed, hvm_io_bitmap);
-
register_cpu_notifier(&cpu_nfb);
return 0;
@@ -655,19 +639,12 @@ int hvm_domain_initialise(struct domain *d,
rwlock_init(&d->arch.hvm.pl_time->pt_migrate);
- /* Set the default IO Bitmap. */
- if ( is_hardware_domain(d) )
+ rc = ioports_setup_access(d);
+ if ( rc )
{
- d->arch.hvm.io_bitmap = _xmalloc(HVM_IOBITMAP_SIZE, PAGE_SIZE);
- if ( d->arch.hvm.io_bitmap == NULL )
- {
- rc = -ENOMEM;
- goto fail1;
- }
- memset(d->arch.hvm.io_bitmap, ~0, HVM_IOBITMAP_SIZE);
+ printk("%pd failed to setup I/O bitmap: %d\n", d, rc);
+ goto fail1;
}
- else
- d->arch.hvm.io_bitmap = hvm_io_bitmap;
register_g2m_portio_handler(d);
register_vpci_portio_handler(d);
@@ -694,6 +671,8 @@ int hvm_domain_initialise(struct domain *d,
break;
}
+ BUG_ON(!d->arch.ioport_caps);
+
vpic_init(d);
rc = vioapic_init(d);
@@ -731,8 +710,7 @@ int hvm_domain_initialise(struct domain *d,
out_vioapic_deinit:
vioapic_deinit(d);
fail1:
- if ( is_hardware_domain(d) )
- xfree(d->arch.hvm.io_bitmap);
+ ioports_release_access(d);
XFREE(d->arch.hvm.params);
XFREE(d->arch.hvm.irq);
fail0:
@@ -806,6 +784,8 @@ void hvm_domain_destroy(struct domain *d)
}
destroy_vpci_mmcfg(d);
+
+ ioports_release_access(d);
}
static int cf_check hvm_save_tsc_adjust(struct vcpu *v, hvm_domain_context_t *h)
diff --git a/xen/arch/x86/hvm/nestedhvm.c b/xen/arch/x86/hvm/nestedhvm.c
index 3205277e815b..5278b7223b5f 100644
--- a/xen/arch/x86/hvm/nestedhvm.c
+++ b/xen/arch/x86/hvm/nestedhvm.c
@@ -100,7 +100,7 @@ nestedhvm_vmcx_flushtlb(struct p2m_domain *p2m)
* The users of the bitmap patterns are in SVM/VMX specific code.
*
* bitmap port 0x80 port 0xed
- * hvm_io_bitmap cleared cleared
+ * hvm.io_bitmap cleared cleared
* iomap[0] cleared set
* iomap[1] set cleared
* iomap[2] set set
@@ -108,7 +108,7 @@ nestedhvm_vmcx_flushtlb(struct p2m_domain *p2m)
static int __init cf_check nestedhvm_setup(void)
{
- /* Same format and size as hvm_io_bitmap (Intel needs only 2 pages). */
+ /* Same format and size as hvm.io_bitmap (Intel needs only 2 pages). */
unsigned nr = cpu_has_vmx ? 2 : 3;
unsigned int i, order = get_order_from_pages(nr);
@@ -158,7 +158,7 @@ static int __init cf_check nestedhvm_setup(void)
__initcall(nestedhvm_setup);
unsigned long *
-nestedhvm_vcpu_iomap_get(bool ioport_80, bool ioport_ed)
+nestedhvm_vcpu_iomap_get(struct vcpu *v, bool ioport_80, bool ioport_ed)
{
int i;
@@ -167,7 +167,7 @@ nestedhvm_vcpu_iomap_get(bool ioport_80, bool ioport_ed)
if (ioport_80 == 0) {
if (ioport_ed == 0)
- return hvm_io_bitmap;
+ return v->domain->arch.hvm.io_bitmap;
i = 0;
} else {
if (ioport_ed == 0)
diff --git a/xen/arch/x86/hvm/quirks.c b/xen/arch/x86/hvm/quirks.c
index 9202f5a47fe9..f4d95441fcff 100644
--- a/xen/arch/x86/hvm/quirks.c
+++ b/xen/arch/x86/hvm/quirks.c
@@ -73,9 +73,6 @@ static int __init cf_check check_port80(void)
dmi_check_system(hvm_no_port80_dmi_table);
- if ( !hvm_port80_allowed )
- __set_bit(0x80, hvm_io_bitmap);
-
return 0;
}
__initcall(check_port80);
diff --git a/xen/arch/x86/hvm/svm/nestedsvm.c b/xen/arch/x86/hvm/svm/nestedsvm.c
index d970869cfd96..4fff8e65242e 100644
--- a/xen/arch/x86/hvm/svm/nestedsvm.c
+++ b/xen/arch/x86/hvm/svm/nestedsvm.c
@@ -315,7 +315,7 @@ static int nsvm_vmrun_permissionmap(struct vcpu *v)
hvm_unmap_guest_frame(ns_viomap, 0);
}
- svm->ns_iomap = nestedhvm_vcpu_iomap_get(ioport_80, ioport_ed);
+ svm->ns_iomap = nestedhvm_vcpu_iomap_get(v, ioport_80, ioport_ed);
nv->nv_ioport80 = ioport_80;
nv->nv_ioportED = ioport_ed;
diff --git a/xen/arch/x86/hvm/vmx/vvmx.c b/xen/arch/x86/hvm/vmx/vvmx.c
index 29e48387a110..ef40d28ee874 100644
--- a/xen/arch/x86/hvm/vmx/vvmx.c
+++ b/xen/arch/x86/hvm/vmx/vvmx.c
@@ -555,7 +555,7 @@ unsigned long *_shadow_io_bitmap(struct vcpu *v)
port80 = bitmap[0x80 >> 3] & (1 << (0x80 & 0x7)) ? 1 : 0;
portED = bitmap[0xed >> 3] & (1 << (0xed & 0x7)) ? 1 : 0;
- return nestedhvm_vcpu_iomap_get(port80, portED);
+ return nestedhvm_vcpu_iomap_get(v, port80, portED);
}
static void update_msrbitmap(struct vcpu *v, uint32_t shadow_ctrl)
@@ -623,7 +623,7 @@ void nvmx_update_exec_control(struct vcpu *v, u32 host_cntrl)
* L1 VMM doesn't intercept IO instruction.
* Use host configuration and reset IO_BITMAP
*/
- bitmap = hvm_io_bitmap;
+ bitmap = v->domain->arch.hvm.io_bitmap;
}
else {
/* use IO bitmap */
diff --git a/xen/arch/x86/include/asm/hvm/nestedhvm.h b/xen/arch/x86/include/asm/hvm/nestedhvm.h
index 8a2a0fbb3c38..5452e9e46b76 100644
--- a/xen/arch/x86/include/asm/hvm/nestedhvm.h
+++ b/xen/arch/x86/include/asm/hvm/nestedhvm.h
@@ -53,7 +53,8 @@ int nestedhvm_hap_nested_page_fault(struct vcpu *v, paddr_t *L2_gpa,
struct npfec npfec);
/* IO permission map */
-unsigned long *nestedhvm_vcpu_iomap_get(bool ioport_80, bool ioport_ed);
+unsigned long *nestedhvm_vcpu_iomap_get(struct vcpu *v,
+ bool ioport_80, bool ioport_ed);
/* Misc */
#define nestedhvm_paging_mode_hap(v) (!!nhvm_vmcx_hap_enabled(v))
diff --git a/xen/arch/x86/include/asm/hvm/support.h b/xen/arch/x86/include/asm/hvm/support.h
index 9e9fa6295567..9aae0f4012a2 100644
--- a/xen/arch/x86/include/asm/hvm/support.h
+++ b/xen/arch/x86/include/asm/hvm/support.h
@@ -41,8 +41,6 @@ extern unsigned int opt_hvm_debug_level;
#define HVM_DBG_LOG(level, _f, _a...) do {} while (0)
#endif
-extern unsigned long hvm_io_bitmap[];
-
enum hvm_translation_result {
HVMTRANS_okay,
HVMTRANS_bad_linear_to_gfn,
diff --git a/xen/arch/x86/include/asm/iocap.h b/xen/arch/x86/include/asm/iocap.h
index f948b7186e95..3328b65345fb 100644
--- a/xen/arch/x86/include/asm/iocap.h
+++ b/xen/arch/x86/include/asm/iocap.h
@@ -22,6 +22,9 @@
#define cache_flush_permitted(d) \
(has_arch_io_resources(d) || has_arch_pdevs(d))
+int ioports_setup_access(struct domain *d);
+void ioports_release_access(struct domain *d);
+
static inline int ioports_permit_access(struct domain *d, unsigned long s,
unsigned long e)
{
diff --git a/xen/arch/x86/ioport.c b/xen/arch/x86/ioport.c
new file mode 100644
index 000000000000..a4d046770bed
--- /dev/null
+++ b/xen/arch/x86/ioport.c
@@ -0,0 +1,168 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Guest I/O port address space configuration.
+ *
+ */
+
+#include <xen/domain.h>
+#include <xen/param.h>
+
+#include <asm/amd.h>
+#include <asm/acpi.h>
+#include <asm/io-ports.h>
+#include <asm/iocap.h>
+#include <asm/pv/shim.h>
+#include <asm/setup.h>
+
+static char __initdata opt_dom0_ioports_disable[256] = "";
+string_param("dom0_ioports_disable", opt_dom0_ioports_disable);
+
+/*
+ * The I/O permission bitmap size.
+ * See: comment in nestedhvm_setup()
+ */
+#define HVM_IOBITMAP_SIZE (3 * PAGE_SIZE)
+
+/* Hide user-defined I/O ports from the guest OS. */
+static void process_dom0_ioports_disable(struct domain *dom0)
+{
+ unsigned long io_from, io_to;
+ char *t, *s = opt_dom0_ioports_disable;
+ const char *u;
+
+ if ( *s == '\0' )
+ return;
+
+ while ( (t = strsep(&s, ",")) != NULL )
+ {
+ io_from = simple_strtoul(t, &u, 16);
+ if ( u == t )
+ {
+ parse_error:
+ printk("Invalid ioport range <%s> "
+ "in dom0_ioports_disable, skipping\n", t);
+ continue;
+ }
+
+ if ( *u == '\0' )
+ io_to = io_from;
+ else if ( *u == '-' )
+ io_to = simple_strtoul(u + 1, &u, 16);
+ else
+ goto parse_error;
+
+ if ( (*u != '\0') || (io_to < io_from) || (io_to >= 65536) )
+ goto parse_error;
+
+ printk("Disabling dom0 access to ioport range %04lx-%04lx\n",
+ io_from, io_to);
+
+ if ( ioports_deny_access(dom0, io_from, io_to) != 0 )
+ BUG();
+ }
+}
+
+/* Set the default IO Bitmap. */
+int ioports_setup_access(struct domain *d)
+{
+ unsigned int i, offs;
+ int rc;
+
+ if ( pv_shim )
+ return 0;
+
+#ifdef CONFIG_HVM
+ d->arch.hvm.io_bitmap = _xmalloc(HVM_IOBITMAP_SIZE, PAGE_SIZE);
+ if ( d->arch.hvm.io_bitmap == NULL )
+ return -ENOMEM;
+
+ memset(d->arch.hvm.io_bitmap, ~0, HVM_IOBITMAP_SIZE);
+
+ if ( !is_hardware_domain(d) )
+ {
+ /*
+ * Allow direct access to the PC debug ports 0x80 and 0xed (they are
+ * often used for I/O delays, but the vmexits simply slow things down).
+ */
+ if ( hvm_port80_allowed )
+ __clear_bit(0x80, d->arch.hvm.io_bitmap);
+
+ __clear_bit(0xed, d->arch.hvm.io_bitmap);
+
+ return 0;
+ }
+#endif
+
+ /* The hardware domain is initially permitted full I/O capabilities. */
+ rc = ioports_permit_access(d, 0, 0xFFFF);
+
+ /* Modify I/O port access permissions. */
+
+ for ( offs = 0, i = ISOLATE_LSB(i8259A_alias_mask) ?: 2;
+ offs <= i8259A_alias_mask; offs += i )
+ {
+ if ( offs & ~i8259A_alias_mask )
+ continue;
+ /* Master Interrupt Controller (PIC). */
+ rc |= ioports_deny_access(d, 0x20 + offs, 0x21 + offs);
+ /* Slave Interrupt Controller (PIC). */
+ rc |= ioports_deny_access(d, 0xA0 + offs, 0xA1 + offs);
+ }
+
+ /* ELCR of both PICs. */
+ rc |= ioports_deny_access(d, 0x4D0, 0x4D1);
+
+ /* Interval Timer (PIT). */
+ for ( offs = 0, i = ISOLATE_LSB(pit_alias_mask) ?: 4;
+ offs <= pit_alias_mask; offs += i )
+ if ( !(offs & ~pit_alias_mask) )
+ rc |= ioports_deny_access(d, PIT_CH0 + offs, PIT_MODE + offs);
+
+ /* PIT Channel 2 / PC Speaker Control. */
+ rc |= ioports_deny_access(d, 0x61, 0x61);
+
+ /* INIT# and alternative A20M# control. */
+ rc |= ioports_deny_access(d, 0x92, 0x92);
+
+ /* IGNNE# control. */
+ rc |= ioports_deny_access(d, 0xF0, 0xF0);
+
+ /* ACPI PM Timer. */
+ if ( pmtmr_ioport )
+ rc |= ioports_deny_access(d, pmtmr_ioport, pmtmr_ioport + 3);
+
+ /* Reset control. */
+ rc |= ioports_deny_access(d, 0xCF9, 0xCF9);
+
+ /* PCI configuration space (NB. 0xCF8 has special treatment). */
+ rc |= ioports_deny_access(d, 0xCFC, 0xCFF);
+
+#ifdef CONFIG_HVM
+ if ( is_hvm_domain(d) )
+ {
+ /* ISA DMA controller, channels 0-3 (incl possible aliases). */
+ rc |= ioports_deny_access(d, 0x00, 0x1F);
+ /* ISA DMA controller, page registers (incl various reserved ones). */
+ rc |= ioports_deny_access(d, 0x80 + !!hvm_port80_allowed, 0x8F);
+ /* ISA DMA controller, channels 4-7 (incl usual aliases). */
+ rc |= ioports_deny_access(d, 0xC0, 0xDF);
+
+ /* HVM debug console IO port. */
+ rc |= ioports_deny_access(d, XEN_HVM_DEBUGCONS_IOPORT,
+ XEN_HVM_DEBUGCONS_IOPORT);
+ if ( amd_acpi_c1e_quirk )
+ rc |= ioports_deny_access(d, acpi_smi_cmd, acpi_smi_cmd);
+ }
+#endif
+
+ /* Command-line I/O ranges. */
+ process_dom0_ioports_disable(d);
+
+ return rc;
+}
+
+void ioports_release_access(struct domain *d)
+{
+ if ( !pv_shim )
+ XFREE(d->arch.hvm.io_bitmap);
+}
diff --git a/xen/arch/x86/pv/dom0_build.c b/xen/arch/x86/pv/dom0_build.c
index 24f2fa0b7f63..385802c65abc 100644
--- a/xen/arch/x86/pv/dom0_build.c
+++ b/xen/arch/x86/pv/dom0_build.c
@@ -17,6 +17,7 @@
#include <asm/bootinfo.h>
#include <asm/bzimage.h>
#include <asm/dom0_build.h>
+#include <asm/iocap.h>
#include <asm/guest.h>
#include <asm/page.h>
#include <asm/pv/mm.h>
@@ -1025,6 +1026,9 @@ static int __init dom0_construct(const struct boot_domain *bd)
if ( test_bit(XENFEAT_supervisor_mode_kernel, parms.f_required) )
panic("Dom0 requires supervisor-mode execution\n");
+ rc = ioports_setup_access(d);
+ BUG_ON(rc != 0);
+
rc = dom0_setup_permissions(d);
BUG_ON(rc != 0);
--
2.56.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [PATCH v8 18/26] emul/ns16x50: resolve physical I/O port sharing
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
` (16 preceding siblings ...)
2026-11-11 19:47 ` [PATCH v8 17/26] x86/domain: enable per-domain I/O port bitmaps dmukhin
@ 2026-11-11 19:47 ` dmukhin
2026-11-11 19:47 ` [PATCH v8 19/26] xen/domain: allocate d->irq_caps before arch-specific initialization dmukhin
` (8 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: dmukhin @ 2026-11-11 19:47 UTC (permalink / raw)
To: xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin, Denis Mukhin
From: Denis Mukhin <dmukhin@ford.com>
Ensure NS16550 emulator does not share I/O ports with the physical I/O
ports, which is essential for emulation in PVH hwdom case (dom0).
Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
xen/common/emul/vuart/ns16x50.c | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git a/xen/common/emul/vuart/ns16x50.c b/xen/common/emul/vuart/ns16x50.c
index cda21c15cf78..97e12a99368a 100644
--- a/xen/common/emul/vuart/ns16x50.c
+++ b/xen/common/emul/vuart/ns16x50.c
@@ -901,14 +901,27 @@ static int ns16x50_init(void *arg)
struct vuart_ns16x50 *vdev = arg;
const struct vuart_info *info;
struct domain *d;
+ const char *as;
+ int rc;
ASSERT(vdev);
d = vdev->owner;
info = vdev->info;
+ as = (info->base_type & IORESOURCE_IO) ? "ioport": "iomem";
if ( info->base_type & IORESOURCE_IO )
{
+ /* Disallow sharing physical I/O port */
+ rc = ioports_deny_access(d, info->base_addr,
+ info->base_addr + info->size - 1);
+ if ( rc )
+ {
+ ns16x50_err(info, "virtual %s range [%#"PRIx64"..%#"PRIx64"]: conflict w/ physical range\n",
+ as, info->base_addr, info->base_addr + info->size - 1);
+ return rc;
+ }
+
register_portio_handler(d, info->base_addr, info->size, ns16x50_io_handle);
}
else if ( info->base_type & IORESOURCE_MEM )
@@ -928,13 +941,20 @@ static int ns16x50_init(void *arg)
static void cf_check ns16x50_deinit(void *arg)
{
struct vuart_ns16x50 *vdev = arg;
+ const struct vuart_info *info;
unsigned long flags;
ASSERT(vdev);
+ info = vdev->info;
+
ns16x50_lock(vdev, &flags);
ns16x50_fifo_tx_flush(vdev);
ns16x50_unlock(vdev, &flags);
+
+ if ( info->base_type & IORESOURCE_IO )
+ ioports_permit_access(d, info->base_addr,
+ info->base_addr + info->size - 1);
}
static void * cf_check ns16x50_alloc(struct domain *d, const struct vuart_info *info)
--
2.56.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [PATCH v8 19/26] xen/domain: allocate d->irq_caps before arch-specific initialization
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
` (17 preceding siblings ...)
2026-11-11 19:47 ` [PATCH v8 18/26] emul/ns16x50: resolve physical I/O port sharing dmukhin
@ 2026-11-11 19:47 ` dmukhin
2026-11-11 19:47 ` [PATCH v8 20/26] emul/ns16x50: resolve physical IRQ sharing dmukhin
` (7 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: dmukhin @ 2026-11-11 19:47 UTC (permalink / raw)
To: xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin, Denis Mukhin
From: Denis Mukhin <dmukhin@ford.com>
To do that, move per-domain interrupt rangeset allocation before
arch-specific code. Add irqs_setup_access() to setup the initial
rangeset.
Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
xen/arch/x86/dom0_build.c | 1 -
xen/arch/x86/hvm/dom0_build.c | 7 +++++++
xen/arch/x86/include/asm/irq.h | 2 ++
xen/arch/x86/irq.c | 8 ++++++++
xen/arch/x86/pv/dom0_build.c | 3 +++
xen/common/domain.c | 8 ++++++--
6 files changed, 26 insertions(+), 3 deletions(-)
diff --git a/xen/arch/x86/dom0_build.c b/xen/arch/x86/dom0_build.c
index e8f783d533af..035d2db72788 100644
--- a/xen/arch/x86/dom0_build.c
+++ b/xen/arch/x86/dom0_build.c
@@ -454,7 +454,6 @@ int __init dom0_setup_permissions(struct domain *d)
rc |= iomem_permit_access(d, 0UL,
PFN_DOWN(1UL << domain_max_paddr_bits(d)) - 1);
- rc |= irqs_permit_access(d, 1, nr_irqs_gsi - 1);
/* Local APIC. */
if ( mp_lapic_addr != 0 )
diff --git a/xen/arch/x86/hvm/dom0_build.c b/xen/arch/x86/hvm/dom0_build.c
index f536f5dcc444..5708caf752cb 100644
--- a/xen/arch/x86/hvm/dom0_build.c
+++ b/xen/arch/x86/hvm/dom0_build.c
@@ -1352,6 +1352,13 @@ int __init dom0_construct_pvh(const struct boot_domain *bd)
*/
pvh_setup_mmcfg(d);
+ rc = irqs_setup_access(d);
+ if ( rc )
+ {
+ printk("%pd unable to setup IRQ rangeset: %d\n", d, rc);
+ return rc;
+ }
+
/*
* Setup permissions early so that calls to add MMIO regions to the
* p2m as part of vPCI setup don't fail due to permission checks.
diff --git a/xen/arch/x86/include/asm/irq.h b/xen/arch/x86/include/asm/irq.h
index 7315150b66b4..d26cb5c1daba 100644
--- a/xen/arch/x86/include/asm/irq.h
+++ b/xen/arch/x86/include/asm/irq.h
@@ -232,4 +232,6 @@ int allocate_and_map_gsi_pirq(struct domain *d, int index, int *pirq_p);
int allocate_and_map_msi_pirq(struct domain *d, int index, int *pirq_p,
int type, struct msi_info *msi);
+int irqs_setup_access(struct domain *d);
+
#endif /* _ASM_HW_IRQ_H */
diff --git a/xen/arch/x86/irq.c b/xen/arch/x86/irq.c
index 32a75f77a46d..3fcff671c27f 100644
--- a/xen/arch/x86/irq.c
+++ b/xen/arch/x86/irq.c
@@ -3082,3 +3082,11 @@ int allocate_and_map_msi_pirq(struct domain *d, int index, int *pirq_p,
return ret;
}
+
+int irqs_setup_access(struct domain *d)
+{
+ if ( is_hardware_domain(d) )
+ return irqs_permit_access(d, 1, nr_irqs_gsi - 1);
+
+ return 0;
+}
diff --git a/xen/arch/x86/pv/dom0_build.c b/xen/arch/x86/pv/dom0_build.c
index 385802c65abc..50c311f0a490 100644
--- a/xen/arch/x86/pv/dom0_build.c
+++ b/xen/arch/x86/pv/dom0_build.c
@@ -1029,6 +1029,9 @@ static int __init dom0_construct(const struct boot_domain *bd)
rc = ioports_setup_access(d);
BUG_ON(rc != 0);
+ rc = irqs_setup_access(d);
+ BUG_ON(rc != 0);
+
rc = dom0_setup_permissions(d);
BUG_ON(rc != 0);
diff --git a/xen/common/domain.c b/xen/common/domain.c
index f3a2634d5f98..18c5674f6439 100644
--- a/xen/common/domain.c
+++ b/xen/common/domain.c
@@ -1031,6 +1031,11 @@ struct domain *domain_create(domid_t domid,
radix_tree_init(&d->pirq_tree);
#endif
+ err = -ENOMEM;
+ d->irq_caps = rangeset_new(d, "Interrupts", 0);
+ if ( !d->irq_caps )
+ goto fail;
+
if ( (err = arch_domain_create(d, config, flags)) != 0 )
goto fail;
init_status |= INIT_arch;
@@ -1040,8 +1045,7 @@ struct domain *domain_create(domid_t domid,
err = -ENOMEM;
d->iomem_caps = rangeset_new(d, "I/O Memory", RANGESETF_prettyprint_hex);
- d->irq_caps = rangeset_new(d, "Interrupts", 0);
- if ( !d->iomem_caps || !d->irq_caps )
+ if ( !d->iomem_caps )
goto fail;
if ( (err = xsm_domain_create(XSM_HOOK, d, config->ssidref)) != 0 )
--
2.56.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [PATCH v8 20/26] emul/ns16x50: resolve physical IRQ sharing
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
` (18 preceding siblings ...)
2026-11-11 19:47 ` [PATCH v8 19/26] xen/domain: allocate d->irq_caps before arch-specific initialization dmukhin
@ 2026-11-11 19:47 ` dmukhin
2026-11-11 19:47 ` [PATCH v8 21/26] emul/ns16x50: implement IRQ emulation via vIOAPIC dmukhin
` (6 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: dmukhin @ 2026-11-11 19:47 UTC (permalink / raw)
To: xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin, Denis Mukhin
From: Denis Mukhin <dmukhin@ford.com>
Make sure that NS16550 emulator does not share virtual device IRQ with
the physical one. This is needed for enabling NS16550 emulator for PVH
hwdom (dom0).
Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
xen/common/emul/vuart/ns16x50.c | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/xen/common/emul/vuart/ns16x50.c b/xen/common/emul/vuart/ns16x50.c
index 97e12a99368a..389c58f94198 100644
--- a/xen/common/emul/vuart/ns16x50.c
+++ b/xen/common/emul/vuart/ns16x50.c
@@ -931,6 +931,17 @@ static int ns16x50_init(void *arg)
else
ASSERT_UNREACHABLE();
+ /*
+ * Disallow sharing physical IRQ.
+ */
+ rc = irq_deny_access(d, info->irq);
+ if ( rc )
+ {
+ ns16x50_err(info, "virtual IRQ#%d: conflict w/ physical IRQ: %d\n",
+ info->irq, rc);
+ return rc;
+ }
+
ns16x50_reset(vdev);
ns16x50_irq_check(vdev);
--
2.56.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [PATCH v8 21/26] emul/ns16x50: implement IRQ emulation via vIOAPIC
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
` (19 preceding siblings ...)
2026-11-11 19:47 ` [PATCH v8 20/26] emul/ns16x50: resolve physical IRQ sharing dmukhin
@ 2026-11-11 19:47 ` dmukhin
2026-11-11 19:47 ` [PATCH v8 22/26] MAINTAINERS: add myself as vUART framework maintainer dmukhin
` (5 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: dmukhin @ 2026-11-11 19:47 UTC (permalink / raw)
To: xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin, Denis Mukhin
From: Denis Mukhin <dmukhin@ford.com>
PVH domains use vIOAPIC, not vPIC and NS16550 emulates ISA IRQs which
cannot be asserted on vIOAPIC.
{map,unmap}_domain_pirq_emuirq() infrastructure is modified by adding
new type of interrupt resources 'IRQ_EMU' which means 'emulated device
IRQ' (similarly to IRQ_MSI_EMU). That relaxes ASSERT() in
map_domain_emuirq_pirq() to account for IRQ_EMU case.
This is necessary to for IOAPIC emulation code to skip IRQ->PIRQ mapping
(vioapic_hwdom_map_gsi()) when guest OS unmasks vIOAPIC pin
corresponding to virtual device's IRQ.
Also, hvm_gsi_eoi() is modified to trigger assertion in
hvm_gsi_deassert() path for ISA IRQs.
Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
xen/arch/x86/hvm/vioapic.c | 10 +++++++
xen/arch/x86/include/asm/irq.h | 6 ++++
xen/arch/x86/irq.c | 2 +-
xen/common/emul/vuart/ns16x50.c | 47 +++++++++++++++++++++++++++++++
xen/drivers/passthrough/x86/hvm.c | 11 +++++++-
5 files changed, 74 insertions(+), 2 deletions(-)
diff --git a/xen/arch/x86/hvm/vioapic.c b/xen/arch/x86/hvm/vioapic.c
index 995fae7b3c0f..03a63c048ea6 100644
--- a/xen/arch/x86/hvm/vioapic.c
+++ b/xen/arch/x86/hvm/vioapic.c
@@ -178,6 +178,16 @@ static int vioapic_hwdom_map_gsi(unsigned int gsi, unsigned int trig,
ASSERT(is_hardware_domain(currd));
+ /*
+ * Interrupt is claimed by one of the platform virtual devices (e.g.
+ * NS16550); do nothing.
+ */
+ write_lock(&currd->event_lock);
+ ret = is_domain_emuirq_claimed(currd, gsi);
+ write_unlock(&currd->event_lock);
+ if ( ret )
+ return 0;
+
/* Interrupt has been unmasked, bind it now. */
ret = mp_register_gsi(gsi, trig, pol);
if ( ret == -EEXIST )
diff --git a/xen/arch/x86/include/asm/irq.h b/xen/arch/x86/include/asm/irq.h
index d26cb5c1daba..58f32026e033 100644
--- a/xen/arch/x86/include/asm/irq.h
+++ b/xen/arch/x86/include/asm/irq.h
@@ -169,6 +169,11 @@ void free_domain_pirqs(struct domain *d);
int map_domain_emuirq_pirq(struct domain *d, int pirq, int emuirq);
int unmap_domain_pirq_emuirq(struct domain *d, int pirq);
+#define domain_emuirq_claim(d, irq) map_domain_emuirq_pirq(d, irq, IRQ_EMU)
+#define domain_emuirq_unclaim(d, irq) unmap_domain_pirq_emuirq(d, irq)
+#define is_domain_emuirq_claimed(d, irq) \
+ (domain_pirq_to_emuirq(d, irq) != IRQ_UNBOUND)
+
/* Evacuate interrupts assigned to CPUs not present in the CPU online map. */
void fixup_irqs(void);
void fixup_eoi(void);
@@ -222,6 +227,7 @@ void cleanup_domain_irq_mapping(struct domain *d);
#define IRQ_UNBOUND (-1)
#define IRQ_PT (-2)
#define IRQ_MSI_EMU (-3)
+#define IRQ_EMU (-4)
bool cpu_has_pending_apic_eoi(void);
diff --git a/xen/arch/x86/irq.c b/xen/arch/x86/irq.c
index 3fcff671c27f..a1f0f3e05dac 100644
--- a/xen/arch/x86/irq.c
+++ b/xen/arch/x86/irq.c
@@ -2814,7 +2814,7 @@ int map_domain_emuirq_pirq(struct domain *d, int pirq, int emuirq)
if ( !is_hvm_domain(d) )
return -EINVAL;
- ASSERT(has_pirq(d));
+ ASSERT(has_pirq(d) || emuirq == IRQ_EMU);
if ( pirq < 0 || pirq >= d->nr_pirqs ||
emuirq == IRQ_UNBOUND || emuirq >= (int) nr_irqs )
diff --git a/xen/common/emul/vuart/ns16x50.c b/xen/common/emul/vuart/ns16x50.c
index 389c58f94198..17691dd2b5eb 100644
--- a/xen/common/emul/vuart/ns16x50.c
+++ b/xen/common/emul/vuart/ns16x50.c
@@ -353,6 +353,14 @@ static void ns16x50_irq_check(struct vuart_ns16x50 *vdev)
else
hvm_isa_irq_deassert(d, info->irq);
}
+ else if ( has_vioapic(d) )
+ {
+ /*
+ * Inject the positive edge only on a low -> high transition.
+ */
+ if ( asserted )
+ hvm_ioapic_assert(d, info->irq, false);
+ }
else
ASSERT_UNREACHABLE();
@@ -896,6 +904,36 @@ static void ns16x50_reset(struct vuart_ns16x50 *vdev)
vdev->regs[NS16X50_REGS_NUM + UART_DLM] = (divisor >> 8) & UINT8_MAX;
}
+static int ns16x50_claim_irq(struct vuart_ns16x50 *vdev, unsigned int irq)
+{
+ struct domain *d = vdev->owner;
+ int rc = 0;
+
+ write_lock(&d->event_lock);
+ rc = domain_emuirq_claim(d, irq);
+ write_unlock(&d->event_lock);
+
+ if ( rc )
+ ns16x50_err(vdev, "virtual IRQ#%d: cannot claim: %d\n",
+ irq, rc);
+
+ return rc;
+}
+
+static void ns16x50_unclaim_irq(struct vuart_ns16x50 *vdev, unsigned int irq)
+{
+ struct domain *d = vdev->owner;
+ int rc = 0;
+
+ write_lock(&d->event_lock);
+ rc = domain_emuirq_unclaim(d, irq);
+ write_unlock(&d->event_lock);
+
+ if ( rc )
+ ns16x50_err(vdev, "virtual IRQ#%d: cannot unclaim: %d\n",
+ irq, rc);
+}
+
static int ns16x50_init(void *arg)
{
struct vuart_ns16x50 *vdev = arg;
@@ -942,6 +980,11 @@ static int ns16x50_init(void *arg)
return rc;
}
+ /* Claim virtual IRQ */
+ rc = ns16x50_claim_irq(vdev, info->irq);
+ if ( rc )
+ return rc;
+
ns16x50_reset(vdev);
ns16x50_irq_check(vdev);
@@ -953,12 +996,16 @@ static void cf_check ns16x50_deinit(void *arg)
{
struct vuart_ns16x50 *vdev = arg;
const struct vuart_info *info;
+ struct domain *d;
unsigned long flags;
ASSERT(vdev);
+ d = vdev->owner;
info = vdev->info;
+ ns16x50_unclaim_irq(vdev, info->irq);
+
ns16x50_lock(vdev, &flags);
ns16x50_fifo_tx_flush(vdev);
ns16x50_unlock(vdev, &flags);
diff --git a/xen/drivers/passthrough/x86/hvm.c b/xen/drivers/passthrough/x86/hvm.c
index 9f8e6c8cd50f..e108f2c69a33 100644
--- a/xen/drivers/passthrough/x86/hvm.c
+++ b/xen/drivers/passthrough/x86/hvm.c
@@ -934,7 +934,16 @@ static void __hvm_dpci_eoi(struct domain *d,
static void hvm_gsi_eoi(struct domain *d, unsigned int gsi)
{
- struct pirq *pirq = pirq_info(d, gsi);
+ struct pirq *pirq;
+
+ /* Check if GSI is claimed by one of the virtual devices. */
+ if ( is_domain_emuirq_claimed(d, gsi) )
+ {
+ hvm_gsi_deassert(d, gsi);
+ return;
+ }
+
+ pirq = pirq_info(d, gsi);
/* Check if GSI is actually mapped. */
if ( !pirq_dpci(pirq) )
--
2.56.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [PATCH v8 22/26] MAINTAINERS: add myself as vUART framework maintainer
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
` (20 preceding siblings ...)
2026-11-11 19:47 ` [PATCH v8 21/26] emul/ns16x50: implement IRQ emulation via vIOAPIC dmukhin
@ 2026-11-11 19:47 ` dmukhin
2026-11-11 19:47 ` [PATCH v8 23/26] docs/fusa: add NS16x50 UART requirements for x86 dmukhin
` (4 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: dmukhin @ 2026-11-11 19:47 UTC (permalink / raw)
To: xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin, Denis Mukhin
From: Denis Mukhin <dmukhin@ford.com>
Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
MAINTAINERS | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/MAINTAINERS b/MAINTAINERS
index b804434d52d6..327adf4641a1 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -615,6 +615,14 @@ F: stubdom/vtpm/
F: stubdom/vtpmmgr/
F: docs/misc/vtpm-platforms.txt
+VUART
+M: Denis Mukhin <dmukhin@ford.com>
+S: Maintained
+F: common/emul/vuart/ns16x50.c
+F: common/emul/vuart/vuart.c
+F: docs/misc/console.txt
+F: xen/include/xen/vuart.h
+
X86 ARCHITECTURE
M: Jan Beulich <jbeulich@suse.com>
M: Andrew Cooper <andrew.cooper3@citrix.com>
--
2.56.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [PATCH v8 23/26] docs/fusa: add NS16x50 UART requirements for x86
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
` (21 preceding siblings ...)
2026-11-11 19:47 ` [PATCH v8 22/26] MAINTAINERS: add myself as vUART framework maintainer dmukhin
@ 2026-11-11 19:47 ` dmukhin
2026-11-11 19:47 ` [PATCH v8 24/26] ci: add initial NS16550-compatible XTF test dmukhin
` (3 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: dmukhin @ 2026-11-11 19:47 UTC (permalink / raw)
To: xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin, Denis Mukhin,
Ayan Kumar Halder
From: Ayan Kumar Halder <ayan.kumar.halder@amd.com>
Add the market-to-design traceability chain for the emulated
NS16550-compatible UART exposed to x86 domains.
The design requirements are written one per row of the test_suite[]
array in the XTF test tests/emul-ns16x50/main.c, so that each
requirement is covered by a test case.
Additionally, document the assumptions Xen makes on the domain in order
to provide the functionality.
Introduce XenProd~x86_emulated_uart~1 as the product requirement the
design requirements cover, and add the two new documents to the toctree.
Finally, fill in the maintainer of the FUSA (x86) entry and extend it to
cover the x86 product requirements.
Signed-off-by: Ayan Kumar Halder <ayan.kumar.halder@amd.com>
Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
MAINTAINERS | 7 +
docs/fusa/reqs/design-reqs/x86/ns16x50.rst | 386 +++++++++++++++++++++
docs/fusa/reqs/index.rst | 2 +
docs/fusa/reqs/product-reqs/x86/reqs.rst | 24 ++
4 files changed, 419 insertions(+)
create mode 100644 docs/fusa/reqs/design-reqs/x86/ns16x50.rst
create mode 100644 docs/fusa/reqs/product-reqs/x86/reqs.rst
diff --git a/MAINTAINERS b/MAINTAINERS
index 327adf4641a1..2c0eaaea2d24 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -335,6 +335,13 @@ F: xen/arch/x86/include/asm/x86_*/efi*.h
F: xen/common/efi/
F: xen/include/efi/
+FUSA (x86)
+M: Denis Mukhin <dmukhin@ford.com>
+R: Ayan Kumar Halder <ayan.kumar.halder@amd.com>
+S: Supported
+F: docs/fusa/reqs/design-reqs/x86
+F: docs/fusa/reqs/product-reqs/x86
+
FUSA
M: Stefano Stabellini <sstabellini@kernel.org>
M: Bertrand Marquis <bertrand.marquis@arm.com>
diff --git a/docs/fusa/reqs/design-reqs/x86/ns16x50.rst b/docs/fusa/reqs/design-reqs/x86/ns16x50.rst
new file mode 100644
index 000000000000..e3995dc9defc
--- /dev/null
+++ b/docs/fusa/reqs/design-reqs/x86/ns16x50.rst
@@ -0,0 +1,386 @@
+.. SPDX-License-Identifier: CC-BY-4.0
+
+NS16550-compatible UART Emulation
+=================================
+
+The following are the requirements related to NS16550-compatible UART emulated
+and exposed by Xen x86 domains.
+
+Access UART scratch register
+----------------------------
+
+`XenSwdgn~emul_ns16x50_scr~1`
+
+Description:
+Xen shall emulate a scratch register for read/write.
+
+Rationale:
+
+Comments:
+
+Covers:
+ - `XenProd~emul_ns16x50~1`
+
+Access Transmit Holding Register
+--------------------------------
+
+`XenSwdgn~emul_ns16x50_thr~1`
+
+Description:
+Xen shall emulate a transmit holding register.
+
+Rationale:
+
+Comments:
+
+Covers:
+ - `XenProd~emul_ns16x50~1`
+
+Access Line Control Register
+----------------------------
+
+`XenSwdgn~emul_ns16x50_lcr~1`
+
+Description:
+Xen shall emulate a line control register for read/write.
+
+Rationale:
+
+Comments:
+
+Covers:
+ - `XenProd~emul_ns16x50~1`
+
+Access Interrupt Enable Register
+--------------------------------
+
+`XenSwdgn~emul_ns16x50_ier~1`
+
+Description:
+Xen shall emulate an interrupt enable register for read/write.
+
+Rationale:
+
+Comments:
+
+Covers:
+ - `XenProd~emul_ns16x50~1`
+
+Access divisor latch registers to support various baud rates
+------------------------------------------------------------
+
+`XenSwdgn~emul_ns16x50_divisor_latch~1`
+
+Description:
+Xen shall emulate the divisor latch low and divisor latch high registers to
+support baud rates - 50, 75, 110, 134.5, 150, 300, 600, 1200, 1800, 2000,
+2400, 3600, 4800, 7200, 9600, 14400, 19200, 28800, 38400, 57600 and 115200.
+
+Rationale:
+
+Comments:
+
+Covers:
+ - `XenProd~emul_ns16x50~1`
+
+Access Modem Control Register
+-----------------------------
+
+`XenSwdgn~emul_ns16x50_mcr~1`
+
+Description:
+Xen shall emulate a modem control register for read/write.
+
+Rationale:
+
+Comments:
+
+Covers:
+ - `XenProd~emul_ns16x50~1`
+
+Enable and disable the FIFOs
+----------------------------
+
+`XenSwdgn~emul_ns16x50_fcr_enable~1`
+
+Description:
+Xen shall report the FIFO state in the interrupt identification register
+according to the FIFO enable bit of the FIFO control register.
+
+Rationale:
+
+Comments:
+
+Covers:
+ - `XenProd~emul_ns16x50~1`
+
+Select the receiver FIFO trigger level
+--------------------------------------
+
+`XenSwdgn~emul_ns16x50_fcr_trigger_level~1`
+
+Description:
+Xen shall accept a write of any receiver FIFO trigger level to the FIFO
+control register.
+
+Rationale:
+
+Comments:
+
+Covers:
+ - `XenProd~emul_ns16x50~1`
+
+Clear the FIFOs
+---------------
+
+`XenSwdgn~emul_ns16x50_fcr_clear~1`
+
+Description:
+Xen shall discard the buffered data of the receiver and the transmitter FIFOs
+when the corresponding clear bit of the FIFO control register is written.
+
+Rationale:
+
+Comments:
+
+Covers:
+ - `XenProd~emul_ns16x50~1`
+
+Transfer data in loopback mode
+------------------------------
+
+`XenSwdgn~emul_ns16x50_loopback_data~1`
+
+Description:
+Xen shall route a byte written to the transmit holding register back to the
+receiver buffer register when the loopback bit of the modem control register
+is set.
+
+Rationale:
+
+Comments:
+
+Covers:
+ - `XenProd~emul_ns16x50~1`
+
+Report modem status in loopback mode
+------------------------------------
+
+`XenSwdgn~emul_ns16x50_loopback_modem~1`
+
+Description:
+Xen shall reflect the modem control register outputs into the modem status
+register inputs when the loopback bit of the modem control register is set.
+
+Rationale:
+
+Comments:
+
+Covers:
+ - `XenProd~emul_ns16x50~1`
+
+Report no pending interrupt
+---------------------------
+
+`XenSwdgn~emul_ns16x50_iir_no_interrupt~1`
+
+Description:
+Xen shall report no pending interrupt in the interrupt identification register
+when no interrupt reason is enabled in the interrupt enable register.
+
+Rationale:
+
+Comments:
+
+Covers:
+ - `XenProd~emul_ns16x50~1`
+
+Report the transmit holding register empty interrupt reason
+-----------------------------------------------------------
+
+`XenSwdgn~emul_ns16x50_iir_thr~1`
+
+Description:
+Xen shall report the transmit holding register empty interrupt reason in the
+interrupt identification register while that reason is enabled and the
+transmit holding register is empty.
+
+Rationale:
+
+Comments:
+
+Covers:
+ - `XenProd~emul_ns16x50~1`
+
+Report the received data available interrupt reason
+---------------------------------------------------
+
+`XenSwdgn~emul_ns16x50_iir_rda~1`
+
+Description:
+Xen shall report the received data available interrupt reason in the interrupt
+identification register while that reason is enabled and unread data is
+pending in the receiver.
+
+Rationale:
+
+Comments:
+
+Covers:
+ - `XenProd~emul_ns16x50~1`
+
+Report the highest priority interrupt reason
+--------------------------------------------
+
+`XenSwdgn~emul_ns16x50_iir_priority~1`
+
+Description:
+Xen shall report the highest priority pending interrupt reason in the
+interrupt identification register when more than one enabled reason is
+pending.
+
+Rationale:
+
+Comments:
+
+Covers:
+ - `XenProd~emul_ns16x50~1`
+
+Assumption of Use on the Domain
+===============================
+
+Configure the UART for 8 data bits, no parity and 1 stop bit
+------------------------------------------------------------
+
+`XenSwdgn~emul_ns16x50_dom_configure_8n1~1`
+
+Description:
+The domain shall configure the UART for a character format of 8 data bits, no
+parity and 1 stop bit.
+
+Rationale:
+Xen stores the word length, parity and stop bit fields of the line control
+register but does not act on them. A domain which configures any other
+character format observes the 8n1 behaviour regardless.
+
+Comments:
+
+Covers:
+ - `XenProd~emul_ns16x50~1`
+
+Program a non-zero baud rate divisor
+------------------------------------
+
+`XenSwdgn~emul_ns16x50_dom_program_nonzero_divisor~1`
+
+Description:
+The domain shall program a non-zero value into the divisor latch registers
+before transferring data.
+
+Rationale:
+Xen treats a divisor of zero as the serial communication being disabled, and
+discards accesses to the transmit holding register and the receiver buffer
+register while the divisor is zero.
+
+Comments:
+Xen resets the divisor latch registers to a divisor corresponding to 115200
+baud, so a domain which does not program them at all is able to transfer data.
+
+Covers:
+ - `XenProd~emul_ns16x50~1`
+
+Clear the divisor latch access bit before transferring data
+-----------------------------------------------------------
+
+`XenSwdgn~emul_ns16x50_dom_clear_dlab~1`
+
+Description:
+The domain shall clear the divisor latch access bit of the line control
+register before accessing the receiver buffer register, the transmit holding
+register or the interrupt enable register.
+
+Rationale:
+The divisor latch registers share their I/O port offsets with the receiver
+buffer register, the transmit holding register and the interrupt enable
+register. Xen discards received characters while the divisor latch access bit
+is set.
+
+Comments:
+
+Covers:
+ - `XenProd~emul_ns16x50~1`
+
+Set the out2 bit of the modem control register to receive interrupts
+--------------------------------------------------------------------
+
+`XenSwdgn~emul_ns16x50_dom_set_out2~1`
+
+Description:
+The domain shall set the out2 bit of the modem control register in order to
+receive UART interrupts.
+
+Rationale:
+On a PC compatible UART the out2 output gates the interrupt line, and Xen
+emulates that gating. A domain which leaves out2 clear does not receive UART
+interrupts irrespective of the contents of the interrupt enable register.
+
+Comments:
+
+Covers:
+ - `XenProd~emul_ns16x50~1`
+
+Access the UART registers with the supported access widths
+-----------------------------------------------------------
+
+`XenSwdgn~emul_ns16x50_dom_access_width~1`
+
+Description:
+The domain shall access the UART registers with an 8-bit access, other than
+the divisor latch registers which may also be accessed with a naturally
+aligned 16-bit access.
+
+Rationale:
+Xen discards an access which is not naturally aligned, which is wider than
+16 bits, or which is a 16-bit access to a register other than the divisor
+latch registers.
+
+Comments:
+A discarded access does not raise an exception in the domain. A read returns
+an all ones value.
+
+Covers:
+ - `XenProd~emul_ns16x50~1`
+
+Do not rely on the reserved bits of the UART registers
+-------------------------------------------------------
+
+`XenSwdgn~emul_ns16x50_dom_reserved_bits~1`
+
+Description:
+The domain shall not rely on the value of the reserved bits of the interrupt
+enable register, the modem control register and the FIFO control register.
+
+Rationale:
+Xen discards the reserved bits on a write, so they read as zero.
+
+Comments:
+
+Covers:
+ - `XenProd~emul_ns16x50~1`
+
+Do not rely on the UART state being preserved across save and restore
+----------------------------------------------------------------------
+
+`XenSwdgn~emul_ns16x50_dom_no_save_restore~1`
+
+Description:
+The domain shall not rely on the state of the UART being preserved across a
+save and restore of the domain.
+
+Rationale:
+Xen does not include the emulated UART state in the domain save image.
+
+Comments:
+
+Covers:
+ - `XenProd~emul_ns16x50~1`
diff --git a/docs/fusa/reqs/index.rst b/docs/fusa/reqs/index.rst
index de19b0cda252..6c1249c9f18c 100644
--- a/docs/fusa/reqs/index.rst
+++ b/docs/fusa/reqs/index.rst
@@ -10,10 +10,12 @@ Requirements documentation
market-reqs/reqs
product-reqs/reqs
product-reqs/arm64/reqs
+ product-reqs/x86/reqs
product-reqs/version_hypercall
product-reqs/hypercall
design-reqs/arm64/generic-timer
design-reqs/arm64/sbsa-uart
design-reqs/arm64/hypercall
design-reqs/arm64/version_hypercall
+ design-reqs/x86/ns16x50
design-reqs/version_hypercall
diff --git a/docs/fusa/reqs/product-reqs/x86/reqs.rst b/docs/fusa/reqs/product-reqs/x86/reqs.rst
new file mode 100644
index 000000000000..ab517c02471f
--- /dev/null
+++ b/docs/fusa/reqs/product-reqs/x86/reqs.rst
@@ -0,0 +1,24 @@
+.. SPDX-License-Identifier: CC-BY-4.0
+
+Domain Creation And Runtime
+===========================
+
+Emulated UART
+-------------
+
+`XenProd~emul_ns16x50~1`
+
+Description:
+Xen shall provide an NS16550-compatible UART device to the domains.
+
+Rationale:
+
+Comments:
+The device is exposed to a domain as a set of I/O ports at one of the legacy
+PC COM port base addresses.
+
+Covers:
+ - `XenMkt~provide_console_domains~1`
+
+Needs:
+ - XenSwdgn
--
2.56.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [PATCH v8 24/26] ci: add initial NS16550-compatible XTF test
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
` (22 preceding siblings ...)
2026-11-11 19:47 ` [PATCH v8 23/26] docs/fusa: add NS16x50 UART requirements for x86 dmukhin
@ 2026-11-11 19:47 ` dmukhin
2026-11-11 19:47 ` [PATCH v8 25/26] ci: add smoke NS16550-compatible PVH Linux test dmukhin
` (2 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: dmukhin @ 2026-11-11 19:47 UTC (permalink / raw)
To: xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin, Denis Mukhin
From: Denis Mukhin <dmukhin@ford.com>
Enable simple functional test for NS16550-compatible emulator by adding
CONFIG_VUART_NS16X50=y
CONFIG_SHADOW_PAGING=y
for alpine-3.24-x86_64-gcc-debug Xen build.
Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
automation/gitlab-ci/build.yaml | 2 ++
automation/gitlab-ci/test.yaml | 10 ++++++++++
automation/scripts/include/xtf-runner | 2 ++
3 files changed, 14 insertions(+)
diff --git a/automation/gitlab-ci/build.yaml b/automation/gitlab-ci/build.yaml
index 3d37e0b572d0..f2072f6a9829 100644
--- a/automation/gitlab-ci/build.yaml
+++ b/automation/gitlab-ci/build.yaml
@@ -290,6 +290,8 @@ alpine-3.24-x86_64-gcc-debug:
CONFIG_UBSAN_FATAL=y
CONFIG_MICROCODE_SCAN_DEFAULT=y
CONFIG_XHCI=y
+ CONFIG_VUART_NS16X50=y
+ CONFIG_SHADOW_PAGING=y
debian-13-x86_64-gcc-debug:
extends: .gcc-x86_64-build-debug
diff --git a/automation/gitlab-ci/test.yaml b/automation/gitlab-ci/test.yaml
index 96127995d7be..4f4ce8aab9e9 100644
--- a/automation/gitlab-ci/test.yaml
+++ b/automation/gitlab-ci/test.yaml
@@ -785,6 +785,16 @@ qemu-xtf-argo-x86_64-gcc-debug:
needs:
- alpine-3.24-x86_64-gcc-debug
+qemu-xtf-emul-ns16x50-x86_64-gcc-debug:
+ extends: .qemu-smoke-x86_64
+ variables:
+ TEST_TIMEOUT_OVERRIDE: 60
+ BOOT_MSG: "NS16550-compatible emulator tests"
+ script:
+ - ./automation/scripts/qemu-xtf.sh x86_64 hvm64 emul-ns16x50 2>&1 | tee ${LOGFILE}
+ needs:
+ - alpine-3.24-x86_64-gcc-debug
+
qemu-smoke-riscv64-gcc:
extends: .qemu-riscv64
script:
diff --git a/automation/scripts/include/xtf-runner b/automation/scripts/include/xtf-runner
index d1ef888465c3..41deb49d1c5d 100644
--- a/automation/scripts/include/xtf-runner
+++ b/automation/scripts/include/xtf-runner
@@ -70,6 +70,8 @@ function xtf_build_cmdline()
declare -a cmdline=()
declare -A per_test_args=(
[argo]="argo=1,mac-permissive=1"
+ # QEMU emulates COM1 (0x3f8) by default, use COM2 for test
+ [emul-ns16x50]="vuart=0:ns16550,com2 -- console=com2"
)
cmdline+=("${XEN_CMDLINE}")
--
2.56.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [PATCH v8 25/26] ci: add smoke NS16550-compatible PVH Linux test
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
` (23 preceding siblings ...)
2026-11-11 19:47 ` [PATCH v8 24/26] ci: add initial NS16550-compatible XTF test dmukhin
@ 2026-11-11 19:47 ` dmukhin
2026-11-11 19:48 ` [PATCH v8 26/26] ci: do not merge: test dmukhin
2026-11-12 14:53 ` [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator Oleksii Kurochko
26 siblings, 0 replies; 39+ messages in thread
From: dmukhin @ 2026-11-11 19:47 UTC (permalink / raw)
To: xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin, Denis Mukhin
From: Denis Mukhin <dmukhin@ford.com>
Add smoke test for NS16550-compatible UART emulated for PVH dom0/domU.
The change also overhauls existing qemu-alpine-x86_64.sh by moving
stuff around so it would be possible to extend the functionality
easier in the future.
Note, that new jobs will be likely skipped in the public CI since
/dev/kvm is not exposed for a reason into docker environment. But this
is great for local testing.
Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
automation/gitlab-ci/test.yaml | 27 +-
automation/scripts/qemu-alpine-x86_64.sh | 325 ++++++++++++++++++-----
2 files changed, 280 insertions(+), 72 deletions(-)
diff --git a/automation/gitlab-ci/test.yaml b/automation/gitlab-ci/test.yaml
index 4f4ce8aab9e9..c4140e850b5f 100644
--- a/automation/gitlab-ci/test.yaml
+++ b/automation/gitlab-ci/test.yaml
@@ -736,7 +736,32 @@ qemu-smoke-dom0less-arm32-gcc-debug-earlyprintk:
qemu-alpine-x86_64-gcc:
extends: .qemu-x86_64
script:
- - ./automation/scripts/qemu-alpine-x86_64.sh 2>&1 | tee ${LOGFILE}
+ - ./automation/scripts/qemu-alpine-x86_64.sh --dom0-type=pv 2>&1 | tee ${LOGFILE}
+ variables:
+ TEST_TIMEOUT_OVERRIDE: 300
+ needs:
+ - *x86_64-test-needs
+ - alpine-3.24-x86_64-gcc
+
+qemu-alpine-x86_64-gcc-vuart-hwdom:
+ extends: .qemu-x86_64
+ script:
+ - if [[ "$(stat -c '%a' /dev/kvm)" != "666" ]]; then echo "/dev/kvm is missing. Skipping test."; exit 0; fi
+ - ./automation/scripts/qemu-alpine-x86_64.sh --dom0-type=pvh --dom0-console=ttyS1 2>&1 | tee ${LOGFILE}
+ variables:
+ TEST_TIMEOUT_OVERRIDE: 300
+ needs:
+ - *x86_64-test-needs
+ - alpine-3.24-x86_64-gcc
+
+qemu-alpine-x86_64-gcc-vuart-pvh:
+ extends: .qemu-x86_64
+ script:
+ - if [[ "$(stat -c '%a' /dev/kvm)" != "666" ]]; then echo "/dev/kvm is missing. Skipping test."; exit 0; fi
+ - ./automation/scripts/qemu-alpine-x86_64.sh --dom0-type=pvh --domU-type=pvh --domU-console=ttyS1 2>&1 | tee ${LOGFILE}
+ variables:
+ PASSED: "BusyBox"
+ TEST_TIMEOUT_OVERRIDE: 300
needs:
- *x86_64-test-needs
- alpine-3.24-x86_64-gcc
diff --git a/automation/scripts/qemu-alpine-x86_64.sh b/automation/scripts/qemu-alpine-x86_64.sh
index 242ffca693fe..5521a2e53e36 100755
--- a/automation/scripts/qemu-alpine-x86_64.sh
+++ b/automation/scripts/qemu-alpine-x86_64.sh
@@ -2,86 +2,269 @@
set -ex -o pipefail
-# DomU Busybox
-cd binaries
-mkdir -p initrd
-mkdir -p initrd/bin
-mkdir -p initrd/sbin
-mkdir -p initrd/etc
-mkdir -p initrd/dev
-mkdir -p initrd/proc
-mkdir -p initrd/sys
-mkdir -p initrd/lib
-mkdir -p initrd/var
-mkdir -p initrd/mnt
-cp /bin/busybox initrd/bin/busybox
-initrd/bin/busybox --install initrd/bin
-echo "#!/bin/sh
+xen_extra_args=""
+dom0_extra_args=""
+domU_extra_args=""
+domU_type=""
-mount -t proc proc /proc
-mount -t sysfs sysfs /sys
-mount -t devtmpfs devtmpfs /dev
-/bin/sh" > initrd/init
-chmod +x initrd/init
-# DomU rootfs
-cd initrd
-find . | cpio -R 0:0 -H newc -o | gzip > ../domU-rootfs.cpio.gz
-cd ..
+while [[ $# -gt 0 ]]; do
+ case "$1" in
+ --xen-extra-args=*)
+ xen_extra_args="${xen_extra_args} ${1#*=}"
+ shift
+ ;;
+ --dom0-extra-args=*)
+ dom0_extra_args="${1#*=}"
+ shift
+ ;;
+ --dom0-console=*)
+ dom0_console="${1#*=}"
+ shift
+ ;;
+ --dom0-type=*)
+ dom0_type="${1#*=}"
+ shift
+ ;;
+ --domU-extra-args=*)
+ domU_extra_args="${1#*=}"
+ shift
+ ;;
+ --domU-console=*)
+ domU_console="${1#*=}"
+ shift
+ ;;
+ --domU-type=*)
+ domU_type="${1#*=}"
+ shift
+ ;;
+ *)
+ echo "Unknown argument: $1"
+ exit 1
+ ;;
+ esac
+done
-# Dom0 rootfs
-cp rootfs.cpio.gz dom0-rootfs.cpio.gz
-cat xen-tools.cpio.gz >> dom0-rootfs.cpio.gz
+dom0_console="${dom0_console:-hvc0}"
+domU_console="${domU_console:-hvc0}"
-# test-local configuration
-mkdir -p rootfs
-cd rootfs
-mkdir -p root etc/local.d
-mv ../domU-rootfs.cpio.gz ./root
-cp ../bzImage ./root
-echo "name=\"domU\"
-memory=512
-vcpus=1
-kernel=\"/root/bzImage\"
-ramdisk=\"/root/domU-rootfs.cpio.gz\"
-extra=\"console=hvc0 root=/dev/ram0 rdinit=/bin/sh\"
-" > root/domU.cfg
-echo "#!/bin/bash
+dom0_type="${dom0_type:-pv}"
-set -x
-
-bash /etc/init.d/xencommons start
-
-xl list
-
-xl -vvv create -c /root/domU.cfg
-
-" > etc/local.d/xen.start
-chmod +x etc/local.d/xen.start
-find . | cpio -R 0:0 -H newc -o | gzip >> ../dom0-rootfs.cpio.gz
-cd ../..
-
-cat >> binaries/pxelinux.0 << EOF
+# iPXE boot configuration: xen + dom0
+function make_ipxe_config()
+{
+ cat > binaries/pxelinux.0 <<EOF
#!ipxe
-
-kernel xen console=com1 console_timestamps=boot
-module bzImage console=hvc0
+kernel xen console=com1 console_timestamps=boot dom0_mem=1500M dom0=${dom0_type} $xen_extra_args
+module bzImage $dom0_extra_args
module dom0-rootfs.cpio.gz
boot
EOF
+}
-# Run the test
-rm -f smoke.serial
-export TEST_CMD="qemu-system-x86_64 \
- -cpu qemu64,+svm \
- -m 2G -smp 2 \
- -monitor none -serial stdio \
- -nographic \
- -device virtio-net-pci,netdev=n0 \
- -netdev user,id=n0,tftp=binaries,bootfile=/pxelinux.0"
+# Detect the type IOMMU device to emulate.
+# PVH dom0 needs IOMMU.
+function host_iommu_device()
+{
+ if grep -q 'AuthenticAMD' /proc/cpuinfo; then
+ name="amd-iommu"
+ elif grep -q 'GenuineIntel' /proc/cpuinfo; then
+ name="intel-iommu"
+ else
+ name="unknown-iommu"
+ fi
+ printf "%s" "$name"
+}
-export TEST_LOG="smoke.serial"
-export BOOT_MSG="Latest ChangeSet: "
-export LOG_MSG="Domain-0"
-export PASSED="BusyBox"
+function make_qemu_config()
+{
+ local qemu_cmd=""
+
+ case "$dom0_type" in
+ pv)
+ export TEST_CMD="qemu-system-x86_64 \
+ -cpu qemu64,+svm \
+ -m 2G -smp 2 \
+ -monitor none -serial stdio \
+ -nographic \
+ -device virtio-net-pci,netdev=n0 \
+ -netdev user,id=n0,tftp=binaries,bootfile=/pxelinux.0"
+ ;;
+
+ pvh)
+ local iommu_device="$(host_iommu_device)"
+ export TEST_CMD="qemu-system-x86_64 \
+ -cpu host \
+ -accel kvm \
+ -machine q35,kernel-irqchip=split,sata=off,usb=off,graphics=off,smbus=off,vmport=off \
+ -device ${iommu_device},intremap=on,caching-mode=on \
+ -vga none \
+ -m 4G -smp 2 \
+ -monitor none \
+ -serial stdio \
+ -nographic \
+ -device virtio-net-pci,netdev=n0 \
+ -netdev user,id=n0,tftp=binaries,bootfile=/pxelinux.0"
+ ;;
+
+ *)
+ echo "Unsupported dom0 type '$dom0_type'"
+ exit 1
+ ;;
+ esac
+
+ export TEST_LOG="smoke.serial"
+ export BOOT_MSG="Latest ChangeSet: "
+ export PASSED="${PASSED:-Welcome to Alpine Linux}"
+}
+
+function add_udev_settle()
+{
+ mkdir -p etc/init.d
+
+ cat > etc/init.d/udev-settle <<EOF
+#!/sbin/openrc-run
+start()
+{
+ return 0
+}
+EOF
+
+ chmod +x etc/init.d/udev-settle
+}
+
+function add_console()
+{
+ local domid="$1"
+ local tty="$2"
+ local ioport=""
+ local ioname=""
+ local guest_cmdline=""
+
+ mkdir -p etc
+
+ gzip -dc ../rootfs.cpio.gz | cpio -i --to-stdout 'etc/inittab' > etc/inittab
+ # Ensure no getty on default consoles.
+ sed -i -E '/^(ttyS0|hvc0)::respawn:/d' etc/inittab
+ # Parametrize guest console.
+ echo "$tty::respawn:/sbin/getty -L $tty 115200 vt100" >> etc/inittab
+ echo $tty >> etc/securetty
+
+ case "$tty" in
+ ttyS0)
+ ioport="0x3f8"
+ ioname="com1"
+ ;;
+ ttyS1)
+ ioport="0x2f8"
+ ioname="com2"
+ ;;
+ ttyS2)
+ ioport="0x3e8"
+ ioname="com3"
+ ;;
+ ttyS3)
+ ioport="0x2e8"
+ ioname="com4"
+ ;;
+ *)
+ ;;
+ esac
+
+ if [ -z "$ioport" ]; then
+ guest_cmdline="console=${tty}"
+ else
+ # NB: ensure guest is not using Xen console
+ guest_cmdline="xen_console_io=off console=uart,io,${ioport},115200n8"
+ xen_extra_args="${xen_extra_args} vuart=$domid:ns16550,$ioname"
+ fi
+
+ if (( $domid > 0 )); then
+ domU_extra_args="${domU_extra_args} ${guest_cmdline}"
+ else
+ dom0_extra_args="${dom0_extra_args} ${guest_cmdline}"
+ fi
+}
+
+function make_domU_rootfs()
+{
+ mkdir -p initrd/{bin,sbin,etc,dev,proc,sys,lib,var,mnt}
+
+ cp /bin/busybox initrd/bin/busybox
+
+ initrd/bin/busybox --install initrd/bin
+ initrd/bin/busybox --install initrd/sbin
+
+ cat > initrd/init <<EOF
+#!/bin/sh
+mount -t proc proc /proc
+mount -t sysfs sysfs /sys
+mount -t devtmpfs devtmpfs /dev
+/bin/sh
+EOF
+ chmod +x initrd/init
+
+ pushd initrd
+ add_console 1 $domU_console
+ find . | cpio -R 0:0 -H newc -o | gzip > ../domU-rootfs.cpio.gz
+ popd
+
+ mkdir -p rootfs/root
+ mv domU-rootfs.cpio.gz rootfs/root
+ cp bzImage rootfs/root
+
+ cat > rootfs/root/domU.cfg <<EOF
+name="domU"
+type="$domU_type"
+memory=512
+vcpus=1
+kernel="/root/bzImage"
+ramdisk="/root/domU-rootfs.cpio.gz"
+extra="root=/dev/ram0 $domU_extra_args"
+EOF
+
+ mkdir -p rootfs/etc/local.d
+ cat > rootfs/etc/local.d/xen.start <<EOF
+#!/bin/bash
+set -x
+bash /etc/init.d/xencommons start
+xl list
+xl -vvv create -c /root/domU.cfg
+EOF
+
+ chmod +x rootfs/etc/local.d/xen.start
+}
+
+pushd binaries
+ rm -rf \
+ etc \
+ dom0-rootfs.cpio.gz \
+ domU-rootfs.cpio.gz \
+ initrd \
+ pxelinux.0 \
+ rootfs \
+ smoke.serial
+
+ cp rootfs.cpio.gz dom0-rootfs.cpio.gz
+ cat xen-tools.cpio.gz >> dom0-rootfs.cpio.gz
+
+ mkdir -p rootfs
+
+ pushd rootfs
+ add_console 0 $dom0_console
+ add_udev_settle
+ popd
+
+ if [ ! -z "${domU_type}" ]; then
+ make_domU_rootfs
+ fi
+
+ pushd rootfs
+ find . | cpio -R 0:0 -H newc -o | gzip >> ../dom0-rootfs.cpio.gz
+ popd
+popd
+
+make_ipxe_config
+
+make_qemu_config
./automation/scripts/console.exp |& sed 's/\r\+$//'
--
2.56.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* [PATCH v8 26/26] ci: do not merge: test
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
` (24 preceding siblings ...)
2026-11-11 19:47 ` [PATCH v8 25/26] ci: add smoke NS16550-compatible PVH Linux test dmukhin
@ 2026-11-11 19:48 ` dmukhin
2026-11-12 14:53 ` [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator Oleksii Kurochko
26 siblings, 0 replies; 39+ messages in thread
From: dmukhin @ 2026-11-11 19:48 UTC (permalink / raw)
To: xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin, Denis Mukhin
From: Denis Mukhin <dmukhin@ford.com>
This commit only enables test jobs in CI to demonstrate NS16550
emulator is functioning.
Note, qemu-alpine-x86_64-gcc-vuart-hwdom and
qemu-alpine-x86_64-gcc-vuart-pvh jobs likely will not even start in
public CI since there's no suitable QEMU runner with /dev/kvm in docker.
Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
automation/scripts/include/xtf-x86_64 | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/automation/scripts/include/xtf-x86_64 b/automation/scripts/include/xtf-x86_64
index 186f074bd8eb..e1e839d3c925 100644
--- a/automation/scripts/include/xtf-x86_64
+++ b/automation/scripts/include/xtf-x86_64
@@ -10,8 +10,8 @@ function xtf_arch_prepare()
export QEMU_PREFIX="${QEMU_PREFIX:-}"
export XEN_BINARY="${XEN_BINARY:-${WORKDIR}/xen}"
export XEN_CMDLINE="${XEN_CMDLINE:-loglvl=all noreboot console_timestamps=boot console=com1}"
- export XTF_SRC_BRANCH="${XTF_SRC_BRANCH:-master}"
- export XTF_SRC_URI="${XTF_SRC_URI:-https://xenbits.xen.org/git-http/xtf.git}"
+ export XTF_SRC_BRANCH="${XTF_SRC_BRANCH:-emul-16x50}"
+ export XTF_SRC_URI="${XTF_SRC_URI:-https://gitlab.com/xen-project/people/dmukhin/xtf.git}"
export XTF_SRC_VARIANTS="hvm64 pv64"
}
--
2.56.0
^ permalink raw reply related [flat|nested] 39+ messages in thread
* Re: [PATCH v8 02/26] xen/8250-uart: update definitions
2026-11-11 19:47 ` [PATCH v8 02/26] xen/8250-uart: update definitions dmukhin
@ 2026-11-12 14:49 ` Jan Beulich
2026-11-12 16:34 ` Oleksii Kurochko
1 sibling, 0 replies; 39+ messages in thread
From: Jan Beulich @ 2026-11-12 14:49 UTC (permalink / raw)
To: dmukhin
Cc: andrew.cooper3, anthony.perard, julien, michal.orzel, roger,
sstabellini, xakep.amatop, dmukhin, xen-devel
On 11.11.2026 20:47, dmukhin@ford.com wrote:
> From: Denis Mukhin <dmukhin@ford.com>
>
> Added missing definitions needed for NS16550 UART emulator.
>
> Newly introduced MSR definitions re-used in the existing ns16550 driver.
Why the restriction to MSR here? You also do this for IIR and IER. Simply
drop "MSR" from here?
> Also, corrected FCR DMA definition bit#3 (0x08) as per:
> https://www.ti.com/lit/ds/symlink/tl16c550c.pdf
> See "7.7.2 FIFO Control Register (FCR)".
Hmm, Ralph Brown's (once famous) Interrupt List doesn't describe the bit
like this. At the same time a different doc than yours that I have matches
what you say. So fine perhaps.
> --- a/xen/include/xen/8250-uart.h
> +++ b/xen/include/xen/8250-uart.h
> @@ -32,6 +32,7 @@
> #define UART_MCR 0x04 /* Modem control */
> #define UART_LSR 0x05 /* line status */
> #define UART_MSR 0x06 /* Modem status */
> +#define UART_SCR 0x07 /* Scratch pad */
> #define UART_USR 0x1f /* Status register (DW) */
> #define UART_DLL 0x00 /* divisor latch (ls) (DLAB=1) */
> #define UART_DLM 0x01 /* divisor latch (ms) (DLAB=1) */
> @@ -42,6 +43,8 @@
> #define UART_IER_ETHREI 0x02 /* tx reg. empty */
> #define UART_IER_ELSI 0x04 /* rx line status */
> #define UART_IER_EMSI 0x08 /* MODEM status */
> +#define UART_IER_ALL_INTR \
> + (UART_IER_ERDAI | UART_IER_ETHREI | UART_IER_ELSI | UART_IER_EMSI)
>
> /* Interrupt Identification Register */
> #define UART_IIR_NOINT 0x01 /* no interrupt pending */
> @@ -51,12 +54,18 @@
> #define UART_IIR_THR 0x02 /* - tx reg. empty */
> #define UART_IIR_MSI 0x00 /* - MODEM status */
> #define UART_IIR_BSY 0x07 /* - busy detect (DW) */
> +#define UART_IIR_FE 0xc0 /* FIFO enabled (2 bits) */
>
> /* FIFO Control Register */
> #define UART_FCR_ENABLE 0x01 /* enable FIFO */
> #define UART_FCR_CLRX 0x02 /* clear Rx FIFO */
> #define UART_FCR_CLTX 0x04 /* clear Tx FIFO */
> -#define UART_FCR_DMA 0x10 /* enter DMA mode */
> +#define UART_FCR_DMA 0x08 /* enter DMA mode */
> +#define UART_FCR_RSRVD0 0x10 /* reserved; always 0 */
> +#define UART_FCR_RSRVD1 0x20 /* reserved; always 0 */
Do you really need these two and ...
> @@ -98,9 +107,26 @@
> /* Modem Control Register */
> #define UART_MCR_DTR 0x01 /* Data Terminal Ready */
> #define UART_MCR_RTS 0x02 /* Request to Send */
> -#define UART_MCR_OUT2 0x08 /* OUT2: interrupt mask */
> +#define UART_MCR_OUT1 0x04 /* Output #1 */
> +#define UART_MCR_OUT2 0x08 /* Output #2 */
> #define UART_MCR_LOOP 0x10 /* Enable loopback test mode */
> +#define UART_MCR_RSRVD0 0x20 /* Reserved #0 */
> #define UART_MCR_TCRTLR 0x40 /* Access TCR/TLR (TI16C752, EFR[4]=1) */
> +#define UART_MCR_RSRVD1 0x80 /* Reserved #1 */
... these two reserved bits as separate masks each? The 0/1 nomenclature
won't make obvious (at use sites) which of each (respectively) is meant.
Jan
^ permalink raw reply [flat|nested] 39+ messages in thread
* Re: [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
` (25 preceding siblings ...)
2026-11-11 19:48 ` [PATCH v8 26/26] ci: do not merge: test dmukhin
@ 2026-11-12 14:53 ` Oleksii Kurochko
26 siblings, 0 replies; 39+ messages in thread
From: Oleksii Kurochko @ 2026-11-12 14:53 UTC (permalink / raw)
To: dmukhin, xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin
Hello Denis,
On 11/11/26 8:47 PM, dmukhin@ford.com wrote:
> x86 port of Xen lacks vUART facility similar to Arm's vpl011 to support x86
> guest OS bring up in the embedded setups.
>
> This patch series introduces initial in-hypervisor emulator for
> NS8250/NS16x50-compatible UARTs under CONFIG_VUART_NS16X50.
>
> In parallel domain creation scenario (hyperlaunch), NS16550 emulator helps
> early guest firmware and OS bringup debugging, because it eliminates
> dependency on the external emulator (qemu) being operational by the time
> domains are created.
>
> The emulator also allows to forward the physical console input to the x86
> domain which is useful when a system has only one physical UART for early
> debugging and this UART is owned by Xen.
>
> By default, CONFIG_VUART_NS16X50 enables emulation of NS16550 at I/O port
> 0x3f8, IRQ#4 in guest OS (legacy COM1). Legacy COM resources can be selected
> at run-time via temporary and undocumented switch 'vuart='.
>
> CONFIG_VUART_NS16X50_DEBUG enables some extra debugging facilities useful
> for NS16550 emulator development/debugging (disabled by default).
>
> The NS16550 emulator is disabled in default x86 configuration and goes under
> CONFIG_EXPERT in Kconfig.
>
> Note, there's a follow on work for RISC-V which is based on that series:
> https://gitlab.com/xen-project/people/olkur/xen/-/tree/riscv-vuart-support-wip?ref_type=heads
>
> Limitations
> ===========
> - Only x86;
> - Only legacy COM resources, custom I/O ports/IRQs are not supported;
> - Only Xen console as a backend, no inter-domain communication (similar to
> vpl011 on Arm);
> - Only 8n1 emulation (8-bit data, no parity, 1 stop bit);
> - No toolstack integration;
> - No baud rate emulation (reports 115200 baud to the guest OS);
> - No FIFO-less mode emulation;
> - No RX FIFO interrupt moderation (FCR) emulation;
> - No integration w/ VM snapshotting (HVM_REGISTER_SAVE_RESTORE() and
> friends);
> - No MMIO-based UART emulation.
>
> Series
> ======
>
> Patch 1 introduces the new vUART framework, that is the code originally
> posted here:
> https://lore.kernel.org/xen-devel/20250624035443.344099-16-dmukhin@ford.com/
> Required for emulator.
>
> Patch 2 adds missing NS16550 definitions, required for emulator.
>
> Patches 3-4 introduces the basic emulator skeleton - state machine
> initialization stubs, I/O port handler stub, logging, etc.
>
> Patches 5-12 incrementally populate the minimal NS16550 register emulation.
>
> Patch 13 hooks vUART state debugging (disabled by default).
>
> Patch 14-16 adds Kconfig for enabling minimal NS16550 UART (disabled by default).
>
> Patches 17-21 introduce necessary changes to enable NS16550 on dom0 (and PVH).
>
> Patches 22-23 documentation updates. Note, that vuart= setting is not
> documented since it will likely change with hyperlanch landing on mainline.
>
> Patches 24-26 tests updates.
>
> Link to CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/3055921295
> Link to XTF branch: https://gitlab.com/xen-project/people/dmukhin/xtf/-/commits/emul-16x50
> Link to Xen branch: https://gitlab.com/xen-project/people/dmukhin/xen/-/tree/vuart-ns8250-v8?ref_type=heads
>
I built every patch of the series on x86 (defconfig + EXPERT + DEBUG,
gcc 16.2) and a few of them don't build:
- 18/26, 19/26, 20/26 with CONFIG_VUART_NS16X50=y: ns16x50_deinit() uses
d, which is declared only in 21/26.
- 16/26 up to 26/26 with CONFIG_VUART_NS16X50_DEBUG=y: in
ns16x50_dump_state() (13/26) the initializer of as dereferences info
before info = vdev->info, so -Werror=uninitialized rejects it. It
shows up only from 16/26 on because nothing compiles ns16x50.c before
the Kconfig option is introduced.
~ Oleksii
^ permalink raw reply [flat|nested] 39+ messages in thread
* Re: [PATCH v8 01/26] emul/vuart: introduce framework for UART emulators
2026-11-11 19:47 ` [PATCH v8 01/26] emul/vuart: introduce framework for UART emulators dmukhin
@ 2026-11-12 15:35 ` Oleksii Kurochko
0 siblings, 0 replies; 39+ messages in thread
From: Oleksii Kurochko @ 2026-11-12 15:35 UTC (permalink / raw)
To: dmukhin, xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin
On 11/11/26 8:47 PM, dmukhin@ford.com wrote:
> From: Denis Mukhin <dmukhin@ford.com>
>
> Introduce a driver framework to abstract UART emulators in the hypervisor.
>
> That allows for architecture-independent handling of virtual UARTs in
> the console driver and simplifies enabling new UART emulators.
>
> The framework is built under CONFIG_VUART_FRAMEWORK, which will be
> automatically enabled once the user enables any UART emulator.
>
> Current implementation supports maximum of one vUART of each kind per
> domain.
d->console->vuart is a single pointer and vuart_init() returns -EBUSY
if it is already set, so it is one vUART per domain in total, not one
of each kind.
>
> Use new domain_has_vuart() in the console driver code to check whether
> to forward console input to the domain using vUART.
>
> Enable console forwarding over vUART for hardware domains with a vUART.
> That enables console forwarding to dom0 on x86, since console can be
> forwarded only to Xen, dom0 and pvshim on x86 as of now.
>
> Note: existing vUARTs are deliberately *not* hooked to the new framework
> to minimize the scope of the patch: vpl011 (i.e. SBSA) emulator and
> "vuart" (i.e. minimalistic MMIO-mapped dtuart for hwdoms on Arm) are
> kept unmodified.
>
> No functional changes for non-x86 architectures.
>
> Signed-off-by: Denis Mukhin <dmukhin@ford.com>
> ---
> xen/arch/arm/xen.lds.S | 1 +
> xen/arch/ppc/xen.lds.S | 1 +
> xen/arch/riscv/xen.lds.S | 1 +
> xen/arch/x86/xen.lds.S | 1 +
> xen/common/Kconfig | 2 +
> xen/common/Makefile | 1 +
> xen/common/emul/Kconfig | 6 ++
> xen/common/emul/Makefile | 1 +
> xen/common/emul/vuart/Kconfig | 6 ++
> xen/common/emul/vuart/Makefile | 1 +
> xen/common/emul/vuart/ns165x50.c | 0
This adds an empty file whose name looks like a typo of ns16x50.c.
Please drop it.
> xen/common/emul/vuart/vuart.c | 173 +++++++++++++++++++++++++++++++
> xen/common/keyhandler.c | 3 +
> xen/drivers/char/console.c | 6 +-
> xen/include/xen/sched.h | 5 +
> xen/include/xen/serial.h | 5 +
> xen/include/xen/vuart.h | 115 ++++++++++++++++++++
> xen/include/xen/xen.lds.h | 10 ++
> 18 files changed, 337 insertions(+), 1 deletion(-)
> create mode 100644 xen/common/emul/Kconfig
> create mode 100644 xen/common/emul/Makefile
> create mode 100644 xen/common/emul/vuart/Kconfig
> create mode 100644 xen/common/emul/vuart/Makefile
> create mode 100644 xen/common/emul/vuart/ns165x50.c
> create mode 100644 xen/common/emul/vuart/vuart.c
> create mode 100644 xen/include/xen/vuart.h
>
[...]
> diff --git a/xen/common/Makefile b/xen/common/Makefile
> index f69d47d18934..60f2d9087589 100644
> --- a/xen/common/Makefile
> +++ b/xen/common/Makefile
> @@ -12,6 +12,7 @@ obj-$(CONFIG_DEVICE_TREE_PARSE) += device-tree/
> obj-$(CONFIG_IOREQ_SERVER) += dm.o
> obj-y += domain.o
> obj-y += domid.o
> +obj-y += emul/
I think this doesn't make much difference at the moment, but it might be
useful to make this conditional so that architectures that don't need
any emulators can exclude them and save some space.
[...]
> diff --git a/xen/common/emul/vuart/ns165x50.c b/xen/common/emul/vuart/ns165x50.c
> new file mode 100644
> index 000000000000..e69de29bb2d1
This adds an empty file whose name looks like a typo of ns16x50.c.
Please drop it.
> diff --git a/xen/common/emul/vuart/vuart.c b/xen/common/emul/vuart/vuart.c
> new file mode 100644
> index 000000000000..bd3b756fb4d6
> --- /dev/null
> +++ b/xen/common/emul/vuart/vuart.c
> @@ -0,0 +1,173 @@
> +/* SPDX-License-Identifier: GPL-2.0-only */
> +/*
> + * UART emulator framework.
> + *
Is this line with only one start really needed?
> + */
> +
> +#include <asm/types.h>
Nit: xen/ headers usually come before asm/ ones.
Also, I think it will be better to use xen/types.h to avoid depenendcy
in common code from arch-specific headers.
> +
> +#include <xen/err.h>
> +#include <xen/resource.h>
> +#include <xen/sched.h>
> +#include <xen/vuart.h>
> +#include <xen/xvmalloc.h>
> +
> +#define for_each_emulator(e) \
> + for ( e = vuart_array_start; e < vuart_array_end; e++ )
> +
> +extern const struct vuart_emulator vuart_array_start[];
> +extern const struct vuart_emulator vuart_array_end[];
> +
> +static const struct vuart_emulator *
> +vuart_match_by_compatible(const struct domain *d, const char *compat)
> +{
> + const struct vuart_emulator *emulator;
> +
> + for_each_emulator(emulator)
> + if ( emulator->compatible &&
> + !strncmp(compat, emulator->compatible,
> + strlen(emulator->compatible)) )
> + return emulator;
This is a prefix match: "ns16550foo" matches "ns16550", and if one
emulator's compatible is a prefix of another's, whichever is linked
first wins. Is that intended? If not, a plain strcmp() would do.
Also, d is unused, and struct vuart_emulator documents compatible as
"Cannot be NULL or empty", so the NULL check either isn't needed or the
comment should say otherwise, or just ASSERT() will be enough here.
> +
> + return NULL;
> +}
> +
> +const static struct vuart *
Nit: static const.
> +vuart_find_by_flags(const struct domain *d, unsigned int flags)
> +{
> + const struct vuart *vuart = d->console->vuart;
> +
> + if ( vuart && (vuart->info->flags & flags) )
> + return vuart;
> +
> + return NULL;
> +}
> +
> +struct vuart *vuart_find_by_resource(const struct domain *d,
> + unsigned int type,
> + unsigned long addr,
> + unsigned long size)
> +{
> + struct vuart *vuart = d->console->vuart;
> +
> + if ( vuart &&
> + (type & vuart->info->base_type) &&
> + addr >= vuart->info->base_addr &&
> + addr + size <= vuart->info->base_addr + vuart->info->size )
> + return vuart;
This has no user in this patch; Wouldn't it better to introduce together
with its first user? Also, base_addr is paddr_t while addr/size are
unsigned long, and addr + size can wrap; checking "size <= base_addr +
vuart->info->size - addr" after the lower bound avoids that.
> +
> + return NULL;
> +}
> +
> +int vuart_init(struct domain *d, const struct vuart_info *info)
> +{
> + const struct vuart_emulator *emulator;
> + struct vuart *vuart;
> + int rc;
> +
> + if ( !info )
> + return 0; /* Nothing to do */
> +
> + if ( d->console->vuart )
> + return -EBUSY;
> +
> + emulator = vuart_match_by_compatible(d, info->compatible);
> + if ( !emulator )
> + return -ENODEV;
> +
> + vuart = xzalloc(typeof(*vuart));
> + if ( !vuart )
> + return -ENOMEM;
> +
> + vuart->info = xvzalloc(typeof(*vuart->info));
> + if ( !vuart->info )
> + {
> + rc = -ENOMEM;
> + goto err_out1;
> + }
> + memcpy(vuart->info, info, sizeof(*info));
vuart comes from xzalloc() but is released with xvfree() here and with
XVFREE() in vuart_deinit(). That works, since xvfree() falls back to
xfree(), but please pair the allocators. Also, why a separate
allocation for info? Embedding a struct vuart_info in struct vuart
would drop one allocation, one error path and the memcpy().
> +
> + vuart->vdev = emulator->alloc(d, vuart->info);
> + if ( IS_ERR(vuart->vdev) )
> + {
> + rc = PTR_ERR(vuart->vdev);
> + goto err_out2;
> + }
> +
> + vuart->emulator = emulator;
> + vuart->owner = d;
> +
> + d->console->input_allowed = true;
> + d->console->vuart = vuart;
input_allowed is set regardless of VUART_CONSOLE_INPUT, whereas
vuart_put_rx() and domain_has_vuart() act only on a vUART with that
flag. For a vUART without the flag, the domain becomes selectable with
CTRL-a, but all input to it is dropped silently. Should this be
conditional on info->flags & VUART_CONSOLE_INPUT?
> +
> + return 0;
> +
> + err_out2:
> + xvfree(vuart->info);
> + err_out1:
> + xvfree(vuart);
> +
> + return rc;
> +}
> +
> +/*
> + * Release any resources taken by UART emulators.
> + *
> + * NB: no flags are cleared, since currently exit() is called only during
> + * domain destroy.
s/exit()/vuart_deinit()/ ?
> + */
> +void vuart_deinit(struct domain *d)
> +{
> + struct vuart *vuart = d->console->vuart;
> +
> + if ( vuart )
> + {
> + vuart->emulator->free(vuart->vdev);
> + xvfree(vuart->info);
> + }
> +
> + XVFREE(d->console->vuart);
> +}
> +
> +/*
> + * Print emulated UART state on the console.
> + *
> + * Must be called under rcu_lock_domain().
The only caller, dump_domains(), holds rcu_read_lock(&domlist_read_lock)
rather than rcu_lock_domain(). That is fine, but the comment should
match.
[...]
> --- a/xen/drivers/char/console.c
> +++ b/xen/drivers/char/console.c
> @@ -33,6 +33,7 @@
> #include <xen/dom0less-build.h>
> #include <xen/sections.h>
> #include <xen/consoled.h>
> +#include <xen/vuart.h>
>
> #ifdef CONFIG_X86
> #include <asm/guest.h>
> @@ -639,11 +640,12 @@ static void __serial_rx(char c)
> if ( !d )
> return;
>
> - if ( is_hardware_domain(d) )
> + if ( is_hardware_domain(d) && !domain_has_vuart(d) )
> {
> /*
> * Deliver input to the hardware domain buffer, unless it is
> * already full.
> + * NB: must be the first check: hardware domain may have emulated UART.
The NB is now inside the block it refers to, which makes it hard to
follow. Since the condition itself excludes a hwdom with a vUART, does
the order still matter? If it does, I'd put the note above the if ().
> */
> if ( (serial_rx_prod - serial_rx_cons) != SERIAL_RX_SIZE )
> serial_rx_ring[SERIAL_RX_MASK(serial_rx_prod++)] = c;
> @@ -654,6 +656,8 @@ static void __serial_rx(char c)
> */
> send_global_virq(VIRQ_CONSOLE);
> }
> + else if ( domain_has_vuart(d) )
> + rc = vuart_put_rx(d, c);
ns16x50_put_rx() (10/26) returns -EBUSY while LCR.DLAB is set, DLL is 0
or MCR.LOOP is set, i.e. while the guest is programming the UART, so
every character typed in that window is dropped and the warning below
is printed for each of them. Are we okay with that spam even during a
short peroid?
> #ifdef CONFIG_SBSA_VUART_CONSOLE
> else
> /* Deliver input to the emulated UART. */
> diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h
> index ce1fa636db36..4fa7f62490ca 100644
> --- a/xen/include/xen/sched.h
> +++ b/xen/include/xen/sched.h
> @@ -24,6 +24,7 @@
> #include <asm/atomic.h>
> #include <asm/current.h>
> #include <xen/vpci.h>
> +#include <xen/vuart.h>
struct domain_console only needs a pointer, so a forward declaration
"struct vuart;" is enough.
> #include <xen/wait.h>
> #include <public/xen.h>
> #include <public/domctl.h>
> @@ -382,6 +383,10 @@ struct domain_console {
> unsigned int idx;
> spinlock_t lock;
> char buf[256];
> +
> +#ifdef CONFIG_VUART_FRAMEWORK
> + struct vuart *vuart;
> +#endif
> };
>
> enum __packed domain_shutdown_state {
> diff --git a/xen/include/xen/serial.h b/xen/include/xen/serial.h
> index 3a71da767dd7..ec481893fede 100644
> --- a/xen/include/xen/serial.h
> +++ b/xen/include/xen/serial.h
> @@ -32,10 +32,15 @@ enum serial_port_state {
>
> struct vuart_info {
> paddr_t base_addr; /* Base address of the UART */
> + unsigned int base_type; /* Base I/O resource type */
> unsigned long size; /* Size of the memory region */
> unsigned long data_off; /* Data register offset */
> unsigned long status_off; /* Status register offset */
> unsigned long status; /* Ready status value */
> + unsigned long flags; /* Emulation flags */
> + unsigned int irq; /* Emulated IRQ # */
> + char compatible[16]; /* Compatible string */
> + char name[16]; /* User-friendly name */
> };
Today struct vuart_info describes a *physical* UART: ns16550.c,
cadence-uart.c and mvebu-uart.c fill it in for Arm's hwdom vuart. With
this change it also becomes the configuration of an *emulated* one, so
every physical driver's instance grows fields that are meaningless for
it, and data_off/status_off/status are meaningless for an emulator. A
separate structure for the emulator configuration would keep the two
apart.
>
> struct serial_port {
> diff --git a/xen/include/xen/vuart.h b/xen/include/xen/vuart.h
> new file mode 100644
> index 000000000000..b5785b3c6b8f
> --- /dev/null
> +++ b/xen/include/xen/vuart.h
> @@ -0,0 +1,115 @@
> +/* SPDX-License-Identifier: GPL-2.0-only */
> +/*
> + * UART emulator framework.
> + *
It seems like this line with start should be dropped, shouldn't it?
> + */
> +
> +#ifndef XEN_VUART_H
> +#define XEN_VUART_H
> +
> +#include <xen/serial.h>
> +#include <public/xen.h>
> +
> +struct vuart_emulator;
> +
> +enum {
> + VUART_CONSOLE_INPUT = BIT(0, U), /* Physical console input forwarding. */
> +};
> +
> +/*
> + * FIXME: #ifdef is temporary to avoid clash with
> + * arch/arm/include/asm/domain.h
> + */
> +#ifdef CONFIG_VUART_FRAMEWORK
> +struct vuart {
As long as the name clashes with Arm's struct vuart, the framework
can't be enabled on Arm. Picking a non-clashing name now looks simpler
than carrying the FIXME.
> + const struct vuart_emulator *emulator;
> + struct vuart_info *info;
> + struct domain *owner;
> + void *vdev;
> +};
> +#endif
> +
> +struct vuart_emulator {
> + /* UART compatible string. Cannot be NULL or empty. */
> + const char *compatible;
> +
> + /*
> + * Allocate emulated UART state (RX/TX FIFOs, locks, initialize registers,
> + * hook I/O handlers, etc.)
> + * Cannot be NULL.
> + */
> + void *(*alloc)(struct domain *d, const struct vuart_info *info);
> +
> + /*
> + * Release resources used to emulate UART state (flush RX/TX FIFOs, unhook
> + * I/O handlers, etc.).
> + * Cannot be NULL.
> + */
> + void (*free)(void *arg);
> +
> + /*
> + * Print emulated UART state (e.g. emulated registers) on the diagnostic
> + * console.
> + * Can be NULL.
> + */
> + void (*dump_state)(void *arg);
> +
> + /*
> + * Place character to the emulated RX FIFO.
> + * Used to forward physical console input to the guest OS.
> + * Can be NULL.
> + */
> + int (*put_rx)(void *arg, char c);
> +};
> +
> +#define VUART_REGISTER(name, x) \
> + static const struct vuart_emulator name##_entry \
> + __used_section(".data.rel.ro.vuart") = x
> +
> +struct vuart *vuart_find_by_resource(const struct domain *d,
> + unsigned int type,
> + unsigned long addr,
> + unsigned long size);
> +
> +int vuart_put_rx(const struct domain *d, char c);
> +
> +#ifdef CONFIG_VUART_FRAMEWORK
These two are declared outside the #ifdef with no stubs. The console.c
call builds with the framework off only because domain_has_vuart() is
an inline returning false and the call gets eliminated. I'd move both
under the #ifdef, next to the other declarations, with a stub for
vuart_put_rx().
> +
> +int vuart_init(struct domain *d, const struct vuart_info *info);
> +void vuart_deinit(struct domain *d);
> +void vuart_dump_state(const struct domain *d);
> +bool domain_has_vuart(const struct domain *d);
> +
> +#else
> +
> +static inline int vuart_init(struct domain *d, const struct vuart_info *info)
> +{
> + return 0;
> +}
> +
> +static inline void vuart_deinit(struct domain *d)
> +{
> +}
> +
> +static inline void vuart_dump_state(const struct domain *d)
> +{
> +}
> +
> +static inline bool domain_has_vuart(const struct domain *d)
> +{
> + return false;
> +}
> +
> +#endif /* CONFIG_VUART_FRAMEWORK */
> +
> +#endif /* XEN_VUART_H */
> +
> +/*
> + * Local variables:
> + * mode: C
> + * c-file-style: "BSD"
> + * c-basic-offset: 4
> + * indent-tabs-mode: nil
> + * End:
> + */
> +
git reports "new blank line at EOF" here.
> diff --git a/xen/include/xen/xen.lds.h b/xen/include/xen/xen.lds.h
> index 958f8256b0fa..5e630d371471 100644
> --- a/xen/include/xen/xen.lds.h
> +++ b/xen/include/xen/xen.lds.h
> @@ -231,6 +231,16 @@
> #define VPCI_ARRAY
> #endif
>
> +#ifdef CONFIG_VUART_FRAMEWORK
> +#define VUART_ARRAY \
> + . = ALIGN(POINTER_ALIGN); \
> + vuart_array_start = .; \
> + *(.data.rel.ro.vuart) \
> + vuart_array_end = .;
Nit: VPCI_ARRAY right above uses __start_vpci_array/__end_vpci_array;
__start_vuart_array/__end_vuart_array would be consistent.
~ Oleksii
^ permalink raw reply [flat|nested] 39+ messages in thread
* Re: [PATCH v8 02/26] xen/8250-uart: update definitions
2026-11-11 19:47 ` [PATCH v8 02/26] xen/8250-uart: update definitions dmukhin
2026-11-12 14:49 ` Jan Beulich
@ 2026-11-12 16:34 ` Oleksii Kurochko
1 sibling, 0 replies; 39+ messages in thread
From: Oleksii Kurochko @ 2026-11-12 16:34 UTC (permalink / raw)
To: dmukhin, xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin
On 11/11/26 8:47 PM, dmukhin@ford.com wrote:
[...]
> diff --git a/xen/include/xen/8250-uart.h b/xen/include/xen/8250-uart.h
> index d13352940c13..15cc4be5ff81 100644
> --- a/xen/include/xen/8250-uart.h
> +++ b/xen/include/xen/8250-uart.h
> @@ -32,6 +32,7 @@
> #define UART_MCR 0x04 /* Modem control */
> #define UART_LSR 0x05 /* line status */
> #define UART_MSR 0x06 /* Modem status */
> +#define UART_SCR 0x07 /* Scratch pad */
> #define UART_USR 0x1f /* Status register (DW) */
> #define UART_DLL 0x00 /* divisor latch (ls) (DLAB=1) */
> #define UART_DLM 0x01 /* divisor latch (ms) (DLAB=1) */
> @@ -42,6 +43,8 @@
> #define UART_IER_ETHREI 0x02 /* tx reg. empty */
> #define UART_IER_ELSI 0x04 /* rx line status */
> #define UART_IER_EMSI 0x08 /* MODEM status */
> +#define UART_IER_ALL_INTR \
> + (UART_IER_ERDAI | UART_IER_ETHREI | UART_IER_ELSI | UART_IER_EMSI)
>
> /* Interrupt Identification Register */
> #define UART_IIR_NOINT 0x01 /* no interrupt pending */
> @@ -51,12 +54,18 @@
> #define UART_IIR_THR 0x02 /* - tx reg. empty */
> #define UART_IIR_MSI 0x00 /* - MODEM status */
> #define UART_IIR_BSY 0x07 /* - busy detect (DW) */
> +#define UART_IIR_FE 0xc0 /* FIFO enabled (2 bits) */
>
> /* FIFO Control Register */
> #define UART_FCR_ENABLE 0x01 /* enable FIFO */
> #define UART_FCR_CLRX 0x02 /* clear Rx FIFO */
> #define UART_FCR_CLTX 0x04 /* clear Tx FIFO */
> -#define UART_FCR_DMA 0x10 /* enter DMA mode */
> +#define UART_FCR_DMA 0x08 /* enter DMA mode */
> +#define UART_FCR_RSRVD0 0x10 /* reserved; always 0 */
> +#define UART_FCR_RSRVD1 0x20 /* reserved; always 0 */
These bits are reserved on a 16550, but this header isn't
16550-specific. A few lines below, the same header defines
UART_FCR_T_TRIG_* as bits 5:4 (TX trigger level on 16650/16654/16C752/
Tegra, see the table there in this header). "reserved; always 0"
contradicts that. Since only the emulator needs to know what *it* treats
as reserved, I'd keep these masks local to ns16x50.c. Am I missing
something?
> +#define UART_FCR_RTB0 0x40 /* receiver trigger bit #0 */
> +#define UART_FCR_RTB1 0x80 /* receiver trigger bit #1 */
> +
These duplicate UART_FCR_TRG4/UART_FCR_TRG8 and UART_FCR_R_TRIG_01/
UART_FCR_R_TRIG_10 right below. The emulator only uses them as the mask
UART_FCR_RTB0 | UART_FCR_RTB1, which is UART_FCR_TRG14
(UART_FCR_R_TRIG_11). Could one of the existing names be used, or a
single mask be introduced, instead of a third set of names for the same
two bits?
> #define UART_FCR_TRG1 0x00 /* Rx FIFO trig lev 1 */
> #define UART_FCR_TRG4 0x40 /* Rx FIFO trig lev 4 */
> #define UART_FCR_TRG8 0x80 /* Rx FIFO trig lev 8 */
> @@ -98,9 +107,26 @@
> /* Modem Control Register */
> #define UART_MCR_DTR 0x01 /* Data Terminal Ready */
> #define UART_MCR_RTS 0x02 /* Request to Send */
> -#define UART_MCR_OUT2 0x08 /* OUT2: interrupt mask */
> +#define UART_MCR_OUT1 0x04 /* Output #1 */
> +#define UART_MCR_OUT2 0x08 /* Output #2 */
> #define UART_MCR_LOOP 0x10 /* Enable loopback test mode */
> +#define UART_MCR_RSRVD0 0x20 /* Reserved #0 */
> #define UART_MCR_TCRTLR 0x40 /* Access TCR/TLR (TI16C752, EFR[4]=1) */
> +#define UART_MCR_RSRVD1 0x80 /* Reserved #1 */
Same concern as for FCR: MCR[5] is the auto flow control enable on the
16750 and on DW_apb_uart (which this header covers, see UART_USR and
UART_IIR_BSY), and MCR[7] is the clock prescaler select on the
TI16C752, whose TCRTLR bit is defined right between the two. Local to
the emulator then?
> +
> +/* Modem Status Register */
> +#define UART_MSR_DCTS 0x01 /* Change in CTS */
> +#define UART_MSR_DDSR 0x02 /* Change in DSR */
> +#define UART_MSR_TERI 0x04 /* Change in RI */
> +#define UART_MSR_DDCD 0x08 /* Change in DCD */
> +#define UART_MSR_CTS 0x10
> +#define UART_MSR_DSR 0x20
> +#define UART_MSR_RI 0x40
> +#define UART_MSR_DCD 0x80
Nit: the last four have no comments, unlike everything else in the
file. Also, TERI is "trailing edge of RI" rather than "change in RI",
which matters because the emulator implements exactly that
(0 -> 1 of RI in ns16x50_io_write8()).
Since ns16x50.c already has UART_MCR_MASK built from the header's
definitions, "reserved" from the emulator's point of view is just
~UART_MCR_MASK (and similarly for FCR with a local mask of ENABLE |
CLRX | CLTX | DMA | TRG14), so neither the RSRVD nor the RTB macros are
needed in the common header.
The ns16550.c conversions look equivalent to me: 0x0A == RTS | OUT2,
and 0x90 == CTS | DCD under UART_MSR_STATUS.
> +#define UART_MSR_CHANGE \
> + (UART_MSR_DCTS | UART_MSR_DDSR | UART_MSR_TERI | UART_MSR_DDCD)
> +#define UART_MSR_STATUS \
> + (UART_MSR_CTS | UART_MSR_DSR | UART_MSR_RI | UART_MSR_DCD)
>
> /* Line Status Register */
> #define UART_LSR_DR 0x01 /* Data ready */
~ Oleksii
^ permalink raw reply [flat|nested] 39+ messages in thread
* Re: [PATCH v8 03/26] x86: fixup dependencies for asm/x86_emulate.h
2026-11-11 19:47 ` [PATCH v8 03/26] x86: fixup dependencies for asm/x86_emulate.h dmukhin
@ 2026-11-12 20:25 ` Oleksii Kurochko
2026-11-13 7:20 ` Jan Beulich
0 siblings, 1 reply; 39+ messages in thread
From: Oleksii Kurochko @ 2026-11-12 20:25 UTC (permalink / raw)
To: dmukhin, xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin
On 11/11/26 8:47 PM, dmukhin@ford.com wrote:
> From: Denis Mukhin <dmukhin@ford.com>
>
> Add xen/bug.h so that asm/x86_emulate.h can be cleanly used if listed as
> the first include in translation unit.
It would help to say what is needed from it: x86emul_invpcid_aux() in
x86_emulate/x86_emulate.h uses ASSERT(), which lives in xen/bug.h, and
xen/lib.h no longer provides it.
I haven't checked by do we really need then inclusion of xen/lib.h in
x86_emulate.h? Won't it be enough to have only xen/bug.h as my
understanding that the only reason for xen/lib.h is (was) ASSERT?
>
> Amends: 0a249bcb0279 ("x86: reduce dependencies on x86_emulate/x86_emulate.h")
I don't think that commit is where this comes from. A translation unit
containing only #include <asm/x86_emulate.h> fails with "implicit
declaration of function 'ASSERT'" both at 0a249bcb0279 and at its
parent. The ASSERT() dependency got lost with 8de3afc0b402 ("xen: move
BUG_ON(), WARN_ON(), ASSERT(), ASSERT_UNREACHABLE() to xen/bug.h"),
which dropped xen/bug.h from xen/lib.h. Since the header wasn't
self-contained before that either, I'd rather drop the tag than point
it at 0a249bcb0279.
> Signed-off-by: Denis Mukhin <dmukhin@ford.com>
> ---
> xen/arch/x86/include/asm/x86_emulate.h | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/xen/arch/x86/include/asm/x86_emulate.h b/xen/arch/x86/include/asm/x86_emulate.h
> index 2b75f7f8e215..209d1e54e03f 100644
> --- a/xen/arch/x86/include/asm/x86_emulate.h
> +++ b/xen/arch/x86/include/asm/x86_emulate.h
> @@ -13,6 +13,7 @@
> #define __ASM_X86_X86_EMULATE_H__
>
> #include <xen/types.h>
> +#include <xen/bug.h>
Nit: alphabetical order (xen/bug.h first).
> #include <xen/lib.h>
> #include <asm/regs.h>
> #include <asm/x86-defns.h>
~ Oleksii
^ permalink raw reply [flat|nested] 39+ messages in thread
* Re: [PATCH v8 03/26] x86: fixup dependencies for asm/x86_emulate.h
2026-11-12 20:25 ` Oleksii Kurochko
@ 2026-11-13 7:20 ` Jan Beulich
0 siblings, 0 replies; 39+ messages in thread
From: Jan Beulich @ 2026-11-13 7:20 UTC (permalink / raw)
To: Oleksii Kurochko, dmukhin
Cc: andrew.cooper3, anthony.perard, julien, michal.orzel, roger,
sstabellini, xakep.amatop, dmukhin, xen-devel
On 12.11.2026 21:25, Oleksii Kurochko wrote:
>
>
> On 11/11/26 8:47 PM, dmukhin@ford.com wrote:
>> From: Denis Mukhin <dmukhin@ford.com>
>>
>> Add xen/bug.h so that asm/x86_emulate.h can be cleanly used if listed as
>> the first include in translation unit.
>
> It would help to say what is needed from it: x86emul_invpcid_aux() in
> x86_emulate/x86_emulate.h uses ASSERT(), which lives in xen/bug.h, and
> xen/lib.h no longer provides it.
>
> I haven't checked by do we really need then inclusion of xen/lib.h in
> x86_emulate.h? Won't it be enough to have only xen/bug.h as my
> understanding that the only reason for xen/lib.h is (was) ASSERT?
>
>>
>> Amends: 0a249bcb0279 ("x86: reduce dependencies on x86_emulate/x86_emulate.h")
>
> I don't think that commit is where this comes from. A translation unit
> containing only #include <asm/x86_emulate.h> fails with "implicit
> declaration of function 'ASSERT'" both at 0a249bcb0279 and at its
> parent. The ASSERT() dependency got lost with 8de3afc0b402 ("xen: move
> BUG_ON(), WARN_ON(), ASSERT(), ASSERT_UNREACHABLE() to xen/bug.h"),
> which dropped xen/bug.h from xen/lib.h. Since the header wasn't
> self-contained before that either, I'd rather drop the tag than point
> it at 0a249bcb0279.
+1
A Fixes: tag would imo be appropriate only if there was active breakage
in the present tree (possibly only in some exotic configuration).
>> --- a/xen/arch/x86/include/asm/x86_emulate.h
>> +++ b/xen/arch/x86/include/asm/x86_emulate.h
>> @@ -13,6 +13,7 @@
>> #define __ASM_X86_X86_EMULATE_H__
>>
>> #include <xen/types.h>
>> +#include <xen/bug.h>
>
> Nit: alphabetical order (xen/bug.h first).
And then, with xen/lib.h dropped and ...
>> #include <xen/lib.h>
>> #include <asm/regs.h>
>> #include <asm/x86-defns.h>
... a blank line inserted in its place, we'll be fully style-conforming
here.
Jan
^ permalink raw reply [flat|nested] 39+ messages in thread
* Re: [PATCH v8 04/26] emul/ns16x50: implement emulator stub
2026-11-11 19:47 ` [PATCH v8 04/26] emul/ns16x50: implement emulator stub dmukhin
@ 2026-11-13 11:48 ` Oleksii Kurochko
0 siblings, 0 replies; 39+ messages in thread
From: Oleksii Kurochko @ 2026-11-13 11:48 UTC (permalink / raw)
To: dmukhin, xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin
On 11/11/26 8:47 PM, dmukhin@ford.com wrote:
> From: Denis Mukhin <dmukhin@ford.com>
>
> Define the placeholder for NS16550 UART emulator.
>
> Implement alloc/free vUART hooks, add stub for I/O port handler,
> initialize emulator state and define debug logging facilities.
>
> Signed-off-by: Denis Mukhin <dmukhin@ford.com>
> ---
> xen/common/emul/vuart/Makefile | 1 +
> xen/common/emul/vuart/ns16x50.c | 417 ++++++++++++++++++++++++++++++++
> 2 files changed, 418 insertions(+)
> create mode 100644 xen/common/emul/vuart/ns16x50.c
>
> diff --git a/xen/common/emul/vuart/Makefile b/xen/common/emul/vuart/Makefile
> index 97f792dc6641..fe904f6cb65d 100644
> --- a/xen/common/emul/vuart/Makefile
> +++ b/xen/common/emul/vuart/Makefile
> @@ -1 +1,2 @@
> obj-y += vuart.o
> +obj-$(CONFIG_VUART_NS16X50) += ns16x50.o
CONFIG_VUART_NS16X50 appears only in 16/26, so this file and everything
added to it by 05..15 isn't compiled at all until then. Introducing the
Kconfig option (EXPERT, default n) in this patch would make each of the
following patches build-tested on its own.
> diff --git a/xen/common/emul/vuart/ns16x50.c b/xen/common/emul/vuart/ns16x50.c
> new file mode 100644
> index 000000000000..6e1cfecbaa55
> --- /dev/null
> +++ b/xen/common/emul/vuart/ns16x50.c
> @@ -0,0 +1,417 @@
> +/* SPDX-License-Identifier: GPL-2.0-only */
> +/*
> + * NS16550-compatible UART Emulator.
> + *
> + * See:
> + * - Serial and UART Tutorial:
> + * https://download.freebsd.org/doc/en/articles/serial-uart/serial-uart_en.pdf
> + * - UART w/ 16 byte FIFO:
> + * https://www.ti.com/lit/ds/symlink/tl16c550c.pdf
> + * - UART w/ 64 byte FIFO:
> + * https://www.ti.com/lit/ds/symlink/tl16c750.pdf
> + * - DesignWare DW_apb_uart Databook, v4.02a:
> + * https://iccircle.com/static/upload/img20240313113905.pdf
I can't open this link: "The requested URL was not found on this server.".
> + *
> + * Limitations:
> + * - Only x86;
> + * - Only Xen console as a backend, no inter-domain communication (similar to
> + * vpl011 on Arm);
> + * - Only 8n1 emulation (8-bit data, no parity, 1 stop bit);
> + * - No baud rate emulation (reports 115200 baud to the guest OS);
> + * - No FIFO-less mode emulation;
> + * - No RX FIFO interrupt moderation (FCR) emulation;
> + * - No integration w/ VM snapshotting (HVM_REGISTER_SAVE_RESTORE() and
> + * friends);
> + * - No ISA IRQ sharing allowed;
> + * - No MMIO-based UART emulation.
> + */
> +
> +#define pr_prefix "ns16x50"
> +#define pr_fmt(fmt) pr_prefix ": " fmt
> +
> +#ifdef CONFIG_VUART_NS16X50_DEBUG
> +#define guest_prefix "FROM GUEST "
Nit: unused until 11/26; I'd introduce it together with its user.
> +#define ns16x50_log_level 2
> +#else
> +#define guest_prefix ""
> +#define ns16x50_log_level 0
> +#endif
Are these defines used anywhere outside this file? I couldn't find any
usage outside this file, so please move them after the includes.
I'm not sure whether there is a specific CODING_STYLE rule for this, but
looking at other files, includes usually come before defines, except in
cases where there is a good reason to use the opposite order.
> +
> +#include <asm/x86_emulate.h>
> +
> +#include <xen/8250-uart.h>
This is common code, but it depends on x86 in several places:
X86EMUL_OKAY (and with it the whole x86 instruction emulator header),
register_portio_handler(), and later in the series vPIC/vIOAPIC,
ioports_deny_access() and the emuirq claim. The register model itself
(registers, FIFOs, IIR) is architecture-neutral, though. I'd separate
the two: keep ns16x50_access() and the register emulation common, and
put the transport (portio handler) and the IRQ line handling behind a
couple of per-arch hooks, either under #ifdef CONFIG_X86 or in a small
arch file. An MMIO-based variant would then only need another
transport.
Nit: xen/ headers usually come before asm/ ones.
> +#include <xen/console.h>
> +#include <xen/err.h>
> +#include <xen/iocap.h>
> +#include <xen/resource.h>
> +#include <xen/vuart.h>
> +#include <xen/xvmalloc.h>
> +
> +#include <public/io/console.h>
> +
> +#define ns16x50_log(n, lvl, vdev, fmt, args...) \
> +do { \
> + if ( ns16x50_log_level >= n ) \
> + gprintk(lvl, pr_fmt("%s: " fmt), (vdev)->name, ## args); \
> +} while (0)
> +
> +#define ns16x50_err(vdev, fmt, args...) \
> + ns16x50_log(0, XENLOG_ERR, vdev, fmt, ## args)
> +#define ns16x50_warn(vdev, fmt, args...) \
> + ns16x50_log(1, XENLOG_WARNING, vdev, fmt, ## args)
> +#define ns16x50_info(vdev, fmt, args...) \
> + ns16x50_log(2, XENLOG_INFO, vdev, fmt, ## args)
> +#define ns16x50_debug(vdev, fmt, args...) \
> + ns16x50_log(3, XENLOG_DEBUG, vdev, fmt, ## args)
ns16x50_log_level is at most 2, so ns16x50_debug() never prints, even
with CONFIG_VUART_NS16X50_DEBUG=y. Is that intended? The same applies
to the "ns16x50_log_level >= 3" checks added later in the series.
> +
> +/*
> + * Number of supported registers in the UART.
> + */
> +#define NS16X50_REGS_NUM (UART_SCR + 1)
> +
> +/*
> + * Number of emulated registers.
> + *
> + * - Emulated registers [0..NS16X50_REGS_NUM] are R/W registers for DLAB=0.
> + * - DLAB=1, R/W, DLL = (NS16X50_REGS_NUM + 0)
> + * - DLAB=1, R/W, DLM = (NS16X50_REGS_NUM + 1)
> + */
> +#define NS16X50_EMU_REGS_NUM (NS16X50_REGS_NUM + 2)
> +
> +/*
> + * Virtual ns16x50 device state.
> + */
> +struct vuart_ns16x50 {
> + uint8_t regs[NS16X50_EMU_REGS_NUM]; /* Emulated registers */
> + const struct vuart_info *info; /* UART description */
> + struct domain *owner; /* Owner domain */
> + const char *name; /* Device name */
> + spinlock_t lock; /* Protection */
The comment here isn't really useful. Could we clarify, at least, then
what it protects?
> + struct xencons_interface cons; /* Emulated RX/TX FIFOs */
> +};
> +
> +static inline void ns16x50_lock_init(struct vuart_ns16x50 *vdev)
> +{
> + spin_lock_init(&vdev->lock);
> +}
> +
> +static inline void ns16x50_lock(struct vuart_ns16x50 *vdev,
> + unsigned long *flags)
> +{
> + spin_lock(&vdev->lock);
> +}
> +
> +static inline bool ns16x50_trylock(struct vuart_ns16x50 *vdev,
> + unsigned long *flags)
> +{
> + return spin_trylock(&vdev->lock);
> +}
> +
> +static inline void ns16x50_unlock(struct vuart_ns16x50 *vdev,
> + unsigned long *flags)
> +{
> + spin_unlock(&vdev->lock);
> +}
Are these wrappers are really useful?
Also, flags is never used by any of the wrappers. If they are meant to
become spin_lock_irqsave() at some point, please say so; otherwise plain
spin_lock() calls would be simpler.
> +
> +/*
> + * Emulate 8-bit write access to ns16x50 register.
> + */
Will this comment be extended in later patches? if no, then it should be
/* ... */ style here. The similar could be applied then for other
similar functions.
> +static int ns16x50_io_write8(
> + struct vuart_ns16x50 *vdev, uint32_t reg, uint8_t *data)
> +{
> + int rc = 0;
> +
> + return rc;
> +}
> +
[ ... ]
> +
> +static void ns16x50_access(int dir, unsigned int type,
> + unsigned long addr, unsigned int size,
> + uint32_t *data)
> +{
> + const char op = (dir == IOREQ_WRITE) ? 'W' : 'R';
> + struct domain *d = rcu_lock_current_domain();
> + struct vuart *vuart = vuart_find_by_resource(d, type, addr, size);
> + struct vuart_ns16x50 *vdev = NULL;
> + const struct domain *owner;
> + const struct vuart_info *info;
> + const char *as;
> + uint32_t reg;
> + unsigned long flags;
> + int rc;
> +
> + if ( !vuart )
> + {
> + printk(XENLOG_ERR "%c @ %#"PRIx64" %d: not initialized\n",
> + op, addr, size);
addr is unsigned long and size is unsigned int: %#lx and %u.
PRIx64 only happens to work on x86-64, and this is common code. The
same goes for the other messages in this function.
> +
> + ASSERT_UNREACHABLE();
> + goto out;
> + }
> +
> + vdev = vuart->vdev;
> + ASSERT(vdev);
> +
> + ns16x50_lock(vdev, &flags);
> +
> + owner = vuart->owner;
> + ASSERT(owner);
> +
> + if ( d != owner )
> + {
> + ns16x50_err(vdev, "%c @ %#"PRIx64" %d: does not match current domain %pd\n",
> + op, addr, size, d);
> +
> + ASSERT_UNREACHABLE();
> + goto out_unlock;
> + }
> +
> + info = vuart->info;
> + ASSERT(info);
> + as = (info->base_type & IORESOURCE_IO) ? "ioport": "iomem";
> +
> + reg = addr - info->base_addr;
> + if ( !IS_ALIGNED(reg, size) )
> + {
> + ns16x50_err(vdev, "%c %s %#"PRIx64" %d: unaligned access\n",
> + op, as, addr, size);
> + goto out_unlock;
> + }
> +
> + if ( reg >= NS16X50_REGS_NUM )
> + {
> + ns16x50_err(vdev, "%c %s %#"PRIx64" %d: LCR=%#02x %#02x %#x: not implemented\n",
> + op, as, addr, size, vdev->regs[UART_LCR], reg, *data);
> + goto out_unlock;
> + }
> +
> + if ( dir == IOREQ_WRITE )
> + rc = ns16x50_io_write(vdev, reg, size, data);
> + else
> + rc = ns16x50_io_read(vdev, reg, size, data);
> +
> +
Nit: double blank line.
> + if ( rc == 0 )
> + ns16x50_debug(vdev, "%c %s %#"PRIx64" %d: LCR=%#02x %#02x %#08x\n",
> + op, as, addr, size, vdev->regs[UART_LCR], reg, *data);
> + else
> + ns16x50_err(vdev, "%c %s %#"PRIx64" %d: LCR=%#02x %#02x %#08x: unsupported access\n",
> + op, as, addr, size, vdev->regs[UART_LCR], reg, *data);
> +
> +out_unlock:
> + ns16x50_unlock(vdev, &flags);
> +out:
> + rcu_unlock_domain(d);
> +}
> +
> +/*
> + * Emulate I/O access to ns16x50 register.
> + * Note, emulation always returns X86EMUL_OKAY, once I/O port trap is enabled.
> + */
> +static int cf_check ns16x50_io_handle(
> + int dir, unsigned int addr, unsigned int size, uint32_t *data)
> +{
> + ns16x50_access(dir, IORESOURCE_IO, addr, size, data);
> +
> + return X86EMUL_OKAY;
As I mentioned above I am not sure that we should have x86-specific
things in common code.
> +}
> +
> +static void ns16x50_reset(struct vuart_ns16x50 *vdev)
> +{
> + memset(vdev->regs, 0, sizeof(vdev->regs));
> +}
> +
> +static int ns16x50_init(void *arg)
> +{
> + struct vuart_ns16x50 *vdev = arg;
> + const struct vuart_info *info;
> + struct domain *d;
> +
> + ASSERT(vdev);
> +
> + d = vdev->owner;
> + info = vdev->info;
> +
> + if ( info->base_type & IORESOURCE_IO )
> + {
> + register_portio_handler(d, info->base_addr, info->size, ns16x50_io_handle);
> + }
> + else if ( info->base_type & IORESOURCE_MEM )
> + {
> + BUG_ON("unimplemented");
> + }
Nit: no braces around single statements.
> + else
> + ASSERT_UNREACHABLE();
> +
> + ns16x50_reset(vdev);
> +
> + return 0;
> +}
Bringing the host down for an unsupported configuration is a bit harsh;
returning -EOPNOTSUPP would make domain creation fail instead. And in a
release build, the last branch returns success without any I/O handler
registered; it should return an error (e.g. -EINVAL) after the
ASSERT_UNREACHABLE().
> +
> +static void cf_check ns16x50_deinit(void *arg)
Nit: ns16x50_deinit() is only called directly, so cf_check isn't
needed (ns16x50_init() doesn't have it either).
> +{
> + struct vuart_ns16x50 *vdev = arg;
> +
> + ASSERT(vdev);
> +}
> +
> +static void * cf_check ns16x50_alloc(struct domain *d, const struct vuart_info *info)
> +{
> + struct vuart_ns16x50 *vdev;
> + int rc;
> +
> + if ( !is_hvm_domain(d) )
> + {
> + ns16x50_err(info, "not an HVM domain\n");
> + return ERR_PTR(-ENOSYS);
Why ENOSYS?
Also, ns16x50_err() is meant to take a struct vuart_ns16x50 and
dereferences (vdev)->name; passing a struct vuart_info only compiles
because both happen to have a name member. A separate macro (or passing
info->name explicitly) would make that less accidental.
The same is true for other use cases of nx16x50_err() below.
> + }
> +
> + if ( vuart_find_by_resource(d, info->base_type, info->base_addr,
> + info->size) )
> + {
> + ns16x50_err(info, "already registered\n");
> + return ERR_PTR(-EBUSY);
> + }
Can it be triggered?
vuart_find_by_resource() only looks at d->console->vuart, vuart_init()
returns -EBUSY before calling alloc() if that is set, and it assigns it
only after alloc() has returned. So inside alloc() it is always NULL.
Unless multiple vUARTs per domain are planned, I'd drop the check; the
-EBUSY in vuart_init() already covers it. Am I missing something?
> +
> + vdev = xvzalloc(typeof(*vdev));
> + if ( !vdev )
> + {
> + ns16x50_err(info, "failed to allocate memory\n");
> + return ERR_PTR(-ENOMEM);
> + }
> +
> + ns16x50_lock_init(vdev);
> +
> + vdev->name = info->name;
> + vdev->owner = d;
> + vdev->info = info;
Nit: vdev->name duplicates vdev->info->name.
> +
> + rc = ns16x50_init(vdev);
> + if ( rc )
> + {
> + xvfree(vdev);
> + return ERR_PTR(rc);
> + }
> +
> + return vdev;
> +}
> +
[ ... ]
~ Oleksii
^ permalink raw reply [flat|nested] 39+ messages in thread
* Re: [PATCH v8 05/26] emul/ns16x50: implement DLL/DLM registers
2026-11-11 19:47 ` [PATCH v8 05/26] emul/ns16x50: implement DLL/DLM registers dmukhin
@ 2026-11-16 10:36 ` Oleksii Kurochko
0 siblings, 0 replies; 39+ messages in thread
From: Oleksii Kurochko @ 2026-11-16 10:36 UTC (permalink / raw)
To: dmukhin, xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin
On 11/11/26 8:47 PM, dmukhin@ford.com wrote:
> From: Denis Mukhin <dmukhin@ford.com>
>
> Add DLL/DLM registers emulation.
>
> DLL/DLM registers report hardcoded 115200 baud rate to the guest OS.
They are initialised to the divisor for 115200, but guest writes are
stored and read back, so the guest sees whatever it programmed. Maybe
"DLL/DLM are reset to the divisor for 115200 baud; the divisor doesn't
affect emulation otherwise".
Also, LCR only becomes writable in 08/26, so until then DLAB is always
0 and none of the code added here is reachable. Would it make sense to
order the LCR patch first?
>
> Signed-off-by: Denis Mukhin <dmukhin@ford.com>
> ---
> xen/common/emul/vuart/ns16x50.c | 82 +++++++++++++++++++++++++++++++++
> 1 file changed, 82 insertions(+)
>
> diff --git a/xen/common/emul/vuart/ns16x50.c b/xen/common/emul/vuart/ns16x50.c
> index 6e1cfecbaa55..80805ee6ecc6 100644
> --- a/xen/common/emul/vuart/ns16x50.c
> +++ b/xen/common/emul/vuart/ns16x50.c
> @@ -119,8 +119,37 @@ static inline void ns16x50_unlock(struct vuart_ns16x50 *vdev,
> static int ns16x50_io_write8(
> struct vuart_ns16x50 *vdev, uint32_t reg, uint8_t *data)
> {
> + uint8_t *regs = vdev->regs;
> + uint8_t val = *data;
> int rc = 0;
>
> + switch ( reg )
> + {
> + case UART_THR: /* DLAB=0 --> UART_RBR, DLAB=1 --> UART_DLL */
On the write side, offset 0 with DLAB=0 is THR, not RBR.
> + /* DLAB=1 */
> + if ( regs[UART_LCR] & UART_LCR_DLAB )
> + {
> + regs[NS16X50_REGS_NUM + UART_DLL] = val;
> + break;
> + }
> +
> + /* DLAB=0 */
> + break;
> +
> + case UART_IER: /* DLAB=0 --> UART_IER, DLAB=1 --> UART_DLM */
> + /* DLAB=1 */
> + if ( regs[UART_LCR] & UART_LCR_DLAB )
> + {
> + regs[NS16X50_REGS_NUM + UART_DLM] = val;
> + }
Nit: no braces around a single statement (and it's inconsistent with
the read side, which uses break inside the if).
> + /* DLAB=0 */
> + break;
> +
> + default:
> + rc = -EINVAL;
> + break;
> + }
> +
> return rc;
> }
>
> @@ -131,8 +160,17 @@ static int ns16x50_io_write8(
> static int ns16x50_io_write16(
> struct vuart_ns16x50 *vdev, uint32_t reg, uint16_t *data)
> {
> + uint8_t *regs = vdev->regs;
> + uint16_t val = *data;
> int rc = -EINVAL;
>
> + if ( (regs[UART_LCR] & UART_LCR_DLAB) && reg == UART_DLL )
> + {
> + vdev->regs[NS16X50_REGS_NUM + UART_DLL] = val & UINT8_MAX;
> + vdev->regs[NS16X50_REGS_NUM + UART_DLM] = (val >> 8) & UINT8_MAX;
Nit: regs is declared but vdev->regs is used right below (same in
ns16x50_io_read16()).
More importantly, special-casing a 16-bit access to DLL with DLAB=1
covers just one case. E.g. QEMU's 16550 model accepts 2- and 4-byte
accesses but implements byte accesses only, so an outw() to base+0 ends
up as byte accesses to base+0 and base+1, each decoded according to the
current DLAB (serial_io_ops in hw/char/serial.c, with the splitting done
by access_with_adjusted_size() in system/memory.c). As far as I know,
that's also what happens on real hardware for an 8-bit device behind
ISA/LPC. Am I missing somethings? if no doing the same here would cover
all registers and drop the ns16x50_io_{read,write}16() helpers,
something like:
for ( i = 0; i < size; i++ )
{
uint8_t byte = *data >> (i * 8);
rc = ns16x50_io_write8(vdev, reg + i, &byte);
if ( rc )
break;
}
(and the reverse for reads, assembling *data from the bytes). As it
stands, e.g. an outw() to THR/IER with DLAB=0 is rejected and logged as
an error.
> + rc = 0;
> + }
> +
> return rc;
> }
>
> @@ -168,9 +206,39 @@ static int ns16x50_io_write(
> static int ns16x50_io_read8(
> struct vuart_ns16x50 *vdev, uint32_t reg, uint8_t *data)
> {
> + uint8_t *regs = vdev->regs;
> uint8_t val = UINT8_MAX;
> int rc = 0;
>
> + switch ( reg )
> + {
> + case UART_RBR: /* DLAB=0 --> UART_RBR, DLAB=1 --> UART_DLL */
> + /* DLAB=1 */
> + if ( regs[UART_LCR] & UART_LCR_DLAB )
> + {
> + val = regs[NS16X50_REGS_NUM + UART_DLL];
> + break;
> + }
> +
> + /* DLAB=0 */
> + break;
> +
> + case UART_IER: /* DLAB=0 --> UART_IER, DLAB=1 --> UART_DLM */
> + /* DLAB=1 */
> + if ( regs[UART_LCR] & UART_LCR_DLAB )
> + {
> + val = regs[NS16X50_REGS_NUM + UART_DLM];
> + break;
> + }
> +
> + /* DLAB=0 */
> + break;
> +
> + default:
> + rc = -EINVAL;
> + break;
> + }
> +
> *data = val;
>
> return rc;
> @@ -182,9 +250,17 @@ static int ns16x50_io_read8(
> static int ns16x50_io_read16(
> struct vuart_ns16x50 *vdev, uint32_t reg, uint16_t *data)
> {
> + uint8_t *regs = vdev->regs;
> uint16_t val = UINT16_MAX;
> int rc = -EINVAL;
>
> + if ( (regs[UART_LCR] & UART_LCR_DLAB) && reg == UART_DLL )
> + {
> + val = vdev->regs[NS16X50_REGS_NUM + UART_DLM] << 8 |
> + vdev->regs[NS16X50_REGS_NUM + UART_DLL];
> + rc = 0;
> + }
> +
> *data = val;
>
> return rc;
> @@ -310,7 +386,13 @@ static int cf_check ns16x50_io_handle(
>
> static void ns16x50_reset(struct vuart_ns16x50 *vdev)
> {
> + const uint16_t divisor = (UART_CLOCK_HZ / 115200) >> 4;
Nit: UART_CLOCK_HZ / (16 * 115200) reads more directly as the divisor
formula. A comment on why the divisor is preset at all would also help:
a real 16550 doesn't reset DLL/DLM, so this is a choice made for guests
that never program the baud rate.
> +
> memset(vdev->regs, 0, sizeof(vdev->regs));
> +
> + /* NB: report 115200 baud rate. */
> + vdev->regs[NS16X50_REGS_NUM + UART_DLL] = divisor & UINT8_MAX;
> + vdev->regs[NS16X50_REGS_NUM + UART_DLM] = (divisor >> 8) & UINT8_MAX;
> }
>
> static int ns16x50_init(void *arg)
~ Oleksii
^ permalink raw reply [flat|nested] 39+ messages in thread
* Re: [PATCH v8 06/26] emul/ns16x50: implement SCR register
2026-11-11 19:47 ` [PATCH v8 06/26] emul/ns16x50: implement SCR register dmukhin
@ 2026-11-16 10:42 ` Oleksii Kurochko
0 siblings, 0 replies; 39+ messages in thread
From: Oleksii Kurochko @ 2026-11-16 10:42 UTC (permalink / raw)
To: dmukhin, xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin
On 11/11/26 8:47 PM, dmukhin@ford.com wrote:
> From: Denis Mukhin <dmukhin@ford.com>
>
> Add SCR register emulation to the I/O port handler.
> Firmware (e.g. OVMF) may use SCR during the guest OS boot.
>
> Signed-off-by: Denis Mukhin <dmukhin@ford.com>
> ---
> xen/common/emul/vuart/ns16x50.c | 9 +++++++++
> 1 file changed, 9 insertions(+)
>
> diff --git a/xen/common/emul/vuart/ns16x50.c b/xen/common/emul/vuart/ns16x50.c
> index 80805ee6ecc6..2371aa32d03f 100644
> --- a/xen/common/emul/vuart/ns16x50.c
> +++ b/xen/common/emul/vuart/ns16x50.c
> @@ -145,6 +145,11 @@ static int ns16x50_io_write8(
> /* DLAB=0 */
> break;
>
> + /* NB: Firmware (e.g. OVMF) may rely on SCR presence. */
Nit: the comment sits only on the write side, and above the case label
rather than inside it like the other DLAB comments; with the commit
message saying the same, I'd drop it or move it next to the definition
of NS16X50_REGS_NUM, which is what makes SCR emulated at all.
> + case UART_SCR:
> + regs[UART_SCR] = val;
> + break;
> +
> default:
> rc = -EINVAL;
> break;
> @@ -234,6 +239,10 @@ static int ns16x50_io_read8(
> /* DLAB=0 */
> break;
>
> + case UART_SCR:
> + val = regs[UART_SCR];
> + break;
> +
> default:
> rc = -EINVAL;
> break;
Reviwed-by: Oleksii Kurochko <oleksii.kurochko@gmail.com>
~ Oleksii
^ permalink raw reply [flat|nested] 39+ messages in thread
* Re: [PATCH v8 07/26] emul/ns16x50: implement IER/IIR registers
2026-11-11 19:47 ` [PATCH v8 07/26] emul/ns16x50: implement IER/IIR registers dmukhin
@ 2026-11-16 16:59 ` Oleksii Kurochko
0 siblings, 0 replies; 39+ messages in thread
From: Oleksii Kurochko @ 2026-11-16 16:59 UTC (permalink / raw)
To: dmukhin, xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin
On 11/11/26 8:47 PM, dmukhin@ford.com wrote:
> From: Denis Mukhin <dmukhin@ford.com>
>
> Add interrupt enable register emulation (IER) and interrupt identity
> reason (IIR) register emulation to the I/O port handler.
>
> Also add routines for asserting/deasserting the virtual ns16x50
> interrupt line as a dependent on IIR code. Add vPIC handling (HVM).
>
> Latch the interrupt line state to avoid poke ns16x50_irq_check() on
> every I/O register access.
ns16x50_irq_check() is still called on every access; what the latch
avoids is calling hvm_isa_irq_{,de}assert() when the line level hasn't
changed. Maybe "Latch the line state, so that vPIC is only told about
level changes"
>
> Signed-off-by: Denis Mukhin <dmukhin@ford.com>
> ---
> xen/common/emul/vuart/ns16x50.c | 112 ++++++++++++++++++++++++++++++++
> 1 file changed, 112 insertions(+)
>
> diff --git a/xen/common/emul/vuart/ns16x50.c b/xen/common/emul/vuart/ns16x50.c
> index 2371aa32d03f..7c5cdc99cfa7 100644
> --- a/xen/common/emul/vuart/ns16x50.c
> +++ b/xen/common/emul/vuart/ns16x50.c
> @@ -88,6 +88,7 @@ struct vuart_ns16x50 {
> const char *name; /* Device name */
> spinlock_t lock; /* Protection */
> struct xencons_interface cons; /* Emulated RX/TX FIFOs */
> + bool irq_asserted; /* IRQ line status */
> };
>
> static inline void ns16x50_lock_init(struct vuart_ns16x50 *vdev)
> @@ -113,6 +114,101 @@ static inline void ns16x50_unlock(struct vuart_ns16x50 *vdev,
> spin_unlock(&vdev->lock);
> }
>
> +static bool cf_check ns16x50_iir_check_lsi(const struct vuart_ns16x50 *vdev)
> +{
> + return false;
> +}
> +
> +static bool cf_check ns16x50_iir_check_rda(const struct vuart_ns16x50 *vdev)
> +{
> + return false;
> +}
> +
> +static bool cf_check ns16x50_iir_check_thr(const struct vuart_ns16x50 *vdev)
> +{
> + return false;
> +}
> +
> +static bool cf_check ns16x50_iir_check_msi(const struct vuart_ns16x50 *vdev)
> +{
> + return false;
> +}
> +
> +/*
> + * Get the interrupt identity reason.
> + *
> + * IIR is re-calculated once called, because ns16x50 always reports high
> + * priority events first.
> + */
> +static uint8_t ns16x50_iir_get(const struct vuart_ns16x50 *vdev)
> +{
> + /*
> + * Interrupt identity reasons by priority.
> + * NB: high priority are at lower indexes below.
> + */
> + static const struct {
> + bool (*check)(const struct vuart_ns16x50 *vdev);
> + uint8_t ier;
> + uint8_t iir;
> + } iir_by_prio[] = {
> + [0] = { ns16x50_iir_check_lsi, UART_IER_ELSI, UART_IIR_LSI },
> + [1] = { ns16x50_iir_check_rda, UART_IER_ERDAI, UART_IIR_RDA },
> + [2] = { ns16x50_iir_check_thr, UART_IER_ETHREI, UART_IIR_THR },
> + [3] = { ns16x50_iir_check_msi, UART_IER_EMSI, UART_IIR_MSI },
> + };
The priority order matches the 16550 (LSI > RDA > THRE > MSI). This
costs up to four indirect calls on every register access, though, and
each check is a one-liner. A plain sequence of ifs would be shorter,
avoid the cf_check annotations, and read just as well:
if ( (ier & UART_IER_ELSI) && (regs[UART_LSR] & UART_LSR_MASK) )
iir = UART_IIR_LSI;
else if ( (ier & UART_IER_ERDAI) && !ns16x50_fifo_rx_empty(vdev) )
iir = UART_IIR_RDA;
...
I am not going to insist on that probably it isn't really big cost...
> + const uint8_t *regs = vdev->regs;
> + uint8_t iir = 0;
> + unsigned int i;
> +
> + for ( i = 0; i < ARRAY_SIZE(iir_by_prio); i++ )
> + {
> + if ( (regs[UART_IER] & iir_by_prio[i].ier) &&
> + iir_by_prio[i].check(vdev) )
> + break;
> +
> + }
Nit: stray blank line before the closing brace.
> + if ( i == ARRAY_SIZE(iir_by_prio) )
> + iir |= UART_IIR_NOINT;
> + else
> + iir |= iir_by_prio[i].iir;
> +
> + if ( regs[UART_FCR] & UART_FCR_ENABLE )
> + iir |= UART_IIR_FE;
> +
> + return iir;
> +}
> +
> +/*
> + * Assert/deassert virtual ns16x50 interrupt line.
> + */
Nit: the comment should be like /* ... */ here.
> +static void ns16x50_irq_check(struct vuart_ns16x50 *vdev)
> +{
> + const struct vuart_info *info = vdev->info;
> + struct domain *d = vdev->owner;
> + uint8_t iir = ns16x50_iir_get(vdev);
> + bool asserted = !(iir & UART_IIR_NOINT);
> +
> + if ( asserted == vdev->irq_asserted )
> + return;
> +
> + vdev->irq_asserted = asserted;
> +
> + if ( has_vpic(d) )
> + {
> + if ( asserted )
> + hvm_isa_irq_assert(d, info->irq, vioapic_get_vector);
The return value (the vector) is ignored, so the get_vector callback
isn't needed: the other callers that don't use it (rtc.c, pmtimer.c,
dm.c) pass NULL. But I am not x86 person so probably I am missing something.
> + else
> + hvm_isa_irq_deassert(d, info->irq);
> + }
> + else
> + ASSERT_UNREACHABLE();
More generally, everything below the latch is x86-specific (has_vpic(),
hvm_isa_irq_{,de}assert(), and in 21/26 has_vioapic() and the
edge-only hvm_ioapic_assert()), while this is common code. The common
part only needs to say "the line is now high/low"; I'd put that behind
a single per-arch hook, e.g.
void vuart_arch_set_irq_level(struct domain *d, unsigned int irq,
bool level);
implemented for x86 with the vPIC/vIOAPIC logic above.
> +
> + ns16x50_debug(vdev, "IRQ#%d IIR %#02x %s\n",
Nit: info->irq is unsigned (%u), and %#02x doesn't pad: the 0x prefix
counts towards the width, so it'd be %#04x.
> + info->irq,
> + iir,
> + asserted ? "assert" : "deassert");
> +}
> +
> /*
> * Emulate 8-bit write access to ns16x50 register.
> */
> @@ -143,6 +239,10 @@ static int ns16x50_io_write8(
> regs[NS16X50_REGS_NUM + UART_DLM] = val;
> }
> /* DLAB=0 */
> + else
> + {
> + regs[UART_IER] = val & UART_IER_ALL_INTR;
> + }
Nit: no braces around a single statement.
> break;
>
> /* NB: Firmware (e.g. OVMF) may rely on SCR presence. */
> @@ -155,6 +255,8 @@ static int ns16x50_io_write8(
> break;
> }
>
> + ns16x50_irq_check(vdev);
> +
> return rc;
> }
>
[...]
> @@ -398,6 +507,7 @@ static void ns16x50_reset(struct vuart_ns16x50 *vdev)
> const uint16_t divisor = (UART_CLOCK_HZ / 115200) >> 4;
>
> memset(vdev->regs, 0, sizeof(vdev->regs));
> + vdev->irq_asserted = false;
>
> /* NB: report 115200 baud rate. */
> vdev->regs[NS16X50_REGS_NUM + UART_DLL] = divisor & UINT8_MAX;
> @@ -428,6 +538,8 @@ static int ns16x50_init(void *arg)
>
> ns16x50_reset(vdev);
>
> + ns16x50_irq_check(vdev);
> +
After the reset IER is 0 and irq_asserted is false, so this returns
early. It's harmless, but it isn't needed.
> return 0;
> }
>
~ Oleksii
^ permalink raw reply [flat|nested] 39+ messages in thread
* Re: [PATCH v8 08/26] emul/ns16x50: implement LCR/LSR registers
2026-11-11 19:47 ` [PATCH v8 08/26] emul/ns16x50: implement LCR/LSR registers dmukhin
@ 2026-11-17 16:33 ` Oleksii Kurochko
0 siblings, 0 replies; 39+ messages in thread
From: Oleksii Kurochko @ 2026-11-17 16:33 UTC (permalink / raw)
To: dmukhin, xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin
On 11/11/26 8:47 PM, dmukhin@ford.com wrote:
> From: Denis Mukhin <dmukhin@ford.com>
>
> Add LCR/LSR registers implementation to the I/O port handler.
>
> Add implementation of ns16x50_dlab_get() and ns16x50_iir_check_lsi().
There's no ns16x50_dlab_get() in this patch, or anywhere in the series;
DLAB is tested open-coded as regs[UART_LCR] & UART_LCR_DLAB. Either the
sentence is stale or the helper got lost (a helper would indeed be nice,
as that expression appears four times by now).
>
> Signed-off-by: Denis Mukhin <dmukhin@ford.com>
> ---
> xen/common/emul/vuart/ns16x50.c | 23 ++++++++++++++++++++++-
> 1 file changed, 22 insertions(+), 1 deletion(-)
>
> diff --git a/xen/common/emul/vuart/ns16x50.c b/xen/common/emul/vuart/ns16x50.c
> index 7c5cdc99cfa7..e80815ccf6e2 100644
> --- a/xen/common/emul/vuart/ns16x50.c
> +++ b/xen/common/emul/vuart/ns16x50.c
> @@ -64,6 +64,11 @@ do { \
> #define ns16x50_debug(vdev, fmt, args...) \
> ns16x50_log(3, XENLOG_DEBUG, vdev, fmt, ## args)
>
> +/*
> + * Helper register definitions.
> + */
Nit: Single line comment should be enough here so please use /* ... */
> +#define UART_LSR_MASK (UART_LSR_OE | UART_LSR_BI)
Nit: this is local to the emulator, but uses the UART_ prefix of
xen/8250-uart.h, so it looks like a register definition from there. Also
"MASK" doesn't say what it is: these are the LSR bits that raise the LSI
and are cleared by reading LSR. On a 16550 that's all four error bits
(OE, PE, FE, BI); the emulator never sets PE and FE (nor BI, as far as I
can see), but including them would match the datasheet and cost
nothing. Maybe NS16X50_LSR_ERRORS?
> +
> /*
> * Number of supported registers in the UART.
> */
Nit: Single line comment should be enough here so please use /* ... */
> @@ -116,7 +121,7 @@ static inline void ns16x50_unlock(struct vuart_ns16x50 *vdev,
>
> static bool cf_check ns16x50_iir_check_lsi(const struct vuart_ns16x50 *vdev)
> {
> - return false;
> + return vdev->regs[UART_LSR] & UART_LSR_MASK;
> }
>
> static bool cf_check ns16x50_iir_check_rda(const struct vuart_ns16x50 *vdev)
> @@ -245,11 +250,16 @@ static int ns16x50_io_write8(
> }
> break;
>
> + case UART_LCR:
> + regs[UART_LCR] = val;
> + break;
> +
> /* NB: Firmware (e.g. OVMF) may rely on SCR presence. */
> case UART_SCR:
> regs[UART_SCR] = val;
> break;
>
> + case UART_LSR: /* RO */
> default:
A write to LSR (or later MSR) ends up in ns16x50_err(), i.e. a
guest-triggerable error message per access. Real 16550s ignore such
writes (the TL16C550C datasheet only says writing LSR isn't
recommended), so silently ignoring them, or at most a debug message,
seems more appropriate. The same applies to the other "unsupported
access" paths in ns16x50_access().
> rc = -EINVAL;
> break;
> @@ -346,6 +356,15 @@ static int ns16x50_io_read8(
> val = ns16x50_iir_get(vdev);
> break;
>
> + case UART_LCR:
> + val = regs[UART_LCR];
> + break;
> +
> + case UART_LSR:
> + val = regs[UART_LSR];
> + regs[UART_LSR] = val & ~UART_LSR_MASK;
> + break;
> +
> case UART_SCR:
> val = regs[UART_SCR];
> break;
> @@ -509,6 +528,8 @@ static void ns16x50_reset(struct vuart_ns16x50 *vdev)
> memset(vdev->regs, 0, sizeof(vdev->regs));
> vdev->irq_asserted = false;
>
> + vdev->regs[UART_LSR] = UART_LSR_THRE | UART_LSR_TEMT;
> +
Matches the 16550 reset value of LSR (0x60).
> /* NB: report 115200 baud rate. */
> vdev->regs[NS16X50_REGS_NUM + UART_DLL] = divisor & UINT8_MAX;
> vdev->regs[NS16X50_REGS_NUM + UART_DLM] = (divisor >> 8) & UINT8_MAX;
~ Oleksii
^ permalink raw reply [flat|nested] 39+ messages in thread
* Re: [PATCH v8 09/26] emul/ns16x50: implement MCR/MSR registers
2026-11-11 19:47 ` [PATCH v8 09/26] emul/ns16x50: implement MCR/MSR registers dmukhin
@ 2026-11-17 18:32 ` Oleksii Kurochko
0 siblings, 0 replies; 39+ messages in thread
From: Oleksii Kurochko @ 2026-11-17 18:32 UTC (permalink / raw)
To: dmukhin, xen-devel
Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
roger, sstabellini, xakep.amatop, dmukhin
On 11/11/26 8:47 PM, dmukhin@ford.com wrote:
> From: Denis Mukhin <dmukhin@ford.com>
>
> Add MCR/MSR registers emulation to the I/O port handler.
>
> Add implementation of ns16x50_iir_check_msi().
>
> Signed-off-by: Denis Mukhin <dmukhin@ford.com>
> ---
> xen/common/emul/vuart/ns16x50.c | 76 ++++++++++++++++++++++++++++++++-
> 1 file changed, 75 insertions(+), 1 deletion(-)
>
> diff --git a/xen/common/emul/vuart/ns16x50.c b/xen/common/emul/vuart/ns16x50.c
> index e80815ccf6e2..9f530e589495 100644
> --- a/xen/common/emul/vuart/ns16x50.c
> +++ b/xen/common/emul/vuart/ns16x50.c
> @@ -69,6 +69,10 @@ do { \
> */
> #define UART_LSR_MASK (UART_LSR_OE | UART_LSR_BI)
>
> +#define UART_MCR_MASK (UART_MCR_DTR | UART_MCR_RTS | \
> + UART_MCR_OUT1 | UART_MCR_OUT2 | \
> + UART_MCR_LOOP)
> +
> /*
> * Number of supported registers in the UART.
> */
> @@ -136,7 +140,7 @@ static bool cf_check ns16x50_iir_check_thr(const struct vuart_ns16x50 *vdev)
>
> static bool cf_check ns16x50_iir_check_msi(const struct vuart_ns16x50 *vdev)
> {
> - return false;
> + return vdev->regs[UART_MSR] & UART_MSR_CHANGE;
> }
>
> /*
> @@ -193,6 +197,13 @@ static void ns16x50_irq_check(struct vuart_ns16x50 *vdev)
> uint8_t iir = ns16x50_iir_get(vdev);
> bool asserted = !(iir & UART_IIR_NOINT);
>
> + /*
> + * On a PC-compatible UART OUT2 gates the ISA interrupt.
> + * Linux'es autoconfig_irq() expects when it toggles MCR.OUT2 during IRQ
> + * probing.
> + */
> + asserted &= !!(vdev->regs[UART_MCR] & UART_MCR_OUT2);
> +
> if ( asserted == vdev->irq_asserted )
> return;
>
> @@ -254,12 +265,63 @@ static int ns16x50_io_write8(
> regs[UART_LCR] = val;
> break;
>
> + case UART_MCR: {
Nit: I think '{' should be on the next line.
> + uint8_t msr_curr, msr_next, msr_delta;
> +
> + msr_curr = regs[UART_MSR];
> + msr_next = 0;
> + msr_delta = 0;
> +
> + if ( val & UART_MCR_RSRVD0 )
> + ns16x50_warn(vdev, "MCR: attempt to set reserved bit: %#x\n",
> + UART_MCR_RSRVD0);
> +
> + if ( val & UART_MCR_TCRTLR )
> + ns16x50_warn(vdev, "MCR: not supported: %#x\n",
> + UART_MCR_TCRTLR);
> +
> + if ( val & UART_MCR_RSRVD1 )
> + ns16x50_warn(vdev, "MCR: attempt to set reserved bit: %#x\n",
> + UART_MCR_RSRVD1);
> +
See my reply to 02/26: bit 5 is AFE on the TL16C550C itself. With a
local mask, these three checks become one
"if ( val & ~UART_MCR_MASK )".
> + /* Set modem status */
> + if ( val & UART_MCR_LOOP )
> + {
I am curious if loopback mode used somewhere except PC? I did a trace
for RISC-V for example and I see only the following writes to MCR:
0x00, 0x01, 0x09, 0x0b.
> + if ( val & UART_MCR_DTR )
> + msr_next |= UART_MSR_DSR;
> + if ( val & UART_MCR_RTS )
> + msr_next |= UART_MSR_CTS;
> + if ( val & UART_MCR_OUT1 )
> + msr_next |= UART_MSR_RI;
> + if ( val & UART_MCR_OUT2 )
> + msr_next |= UART_MSR_DCD;
> + }
> + else
> + msr_next |= UART_MSR_DCD | UART_MSR_DSR | UART_MSR_CTS;
> +
> + /* Calculate changes in modem status */
> + if ( (msr_curr & UART_MSR_CTS) ^ (msr_next & UART_MSR_CTS) )
> + msr_delta |= UART_MSR_DCTS;
> + if ( (msr_curr & UART_MSR_DSR) ^ (msr_next & UART_MSR_DSR) )
> + msr_delta |= UART_MSR_DDSR;
> + if ( !(msr_curr & UART_MSR_RI) && (msr_next & UART_MSR_RI) )
> + msr_delta |= UART_MSR_TERI;
This is the leading edge. MSR.RI is the complement of the RI input, and
TERI is set when the input goes from low to high (SLLS177I, 7.7.10, bits
2 and 6), i.e. when MSR.RI goes from 1 to 0. QEMU does the same
(hw/char/serial.c: "UART_MSR_TERI only if change was from 1 -> 0"). So:
if ( (msr_curr & UART_MSR_RI) && !(msr_next & UART_MSR_RI) )
> + if ( (msr_curr & UART_MSR_DCD) ^ (msr_next & UART_MSR_DCD) )
> + msr_delta |= UART_MSR_DDCD;
> +
> + regs[UART_MCR] = val & UART_MCR_MASK;
> + regs[UART_MSR] = msr_next | msr_delta;
This drops delta bits that are still pending. The change bits are only
cleared "when the CPU reads the MSR" (7.7.10), so e.g. with DTR and RTS
toggled by two separate MCR writes in loopback mode before the guest
reads MSR, ΔCTS from the first write is lost. Keeping them:
regs[UART_MSR] = msr_next | msr_delta |
(msr_curr & UART_MSR_CHANGE);ч
> +
> + break;
> + }
> +
~ Oleksii
^ permalink raw reply [flat|nested] 39+ messages in thread
end of thread, other threads:[~2026-11-17 18:32 UTC | newest]
Thread overview: 39+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
2026-11-11 19:47 ` [PATCH v8 01/26] emul/vuart: introduce framework for UART emulators dmukhin
2026-11-12 15:35 ` Oleksii Kurochko
2026-11-11 19:47 ` [PATCH v8 02/26] xen/8250-uart: update definitions dmukhin
2026-11-12 14:49 ` Jan Beulich
2026-11-12 16:34 ` Oleksii Kurochko
2026-11-11 19:47 ` [PATCH v8 03/26] x86: fixup dependencies for asm/x86_emulate.h dmukhin
2026-11-12 20:25 ` Oleksii Kurochko
2026-11-13 7:20 ` Jan Beulich
2026-11-11 19:47 ` [PATCH v8 04/26] emul/ns16x50: implement emulator stub dmukhin
2026-11-13 11:48 ` Oleksii Kurochko
2026-11-11 19:47 ` [PATCH v8 05/26] emul/ns16x50: implement DLL/DLM registers dmukhin
2026-11-16 10:36 ` Oleksii Kurochko
2026-11-11 19:47 ` [PATCH v8 06/26] emul/ns16x50: implement SCR register dmukhin
2026-11-16 10:42 ` Oleksii Kurochko
2026-11-11 19:47 ` [PATCH v8 07/26] emul/ns16x50: implement IER/IIR registers dmukhin
2026-11-16 16:59 ` Oleksii Kurochko
2026-11-11 19:47 ` [PATCH v8 08/26] emul/ns16x50: implement LCR/LSR registers dmukhin
2026-11-17 16:33 ` Oleksii Kurochko
2026-11-11 19:47 ` [PATCH v8 09/26] emul/ns16x50: implement MCR/MSR registers dmukhin
2026-11-17 18:32 ` Oleksii Kurochko
2026-11-11 19:47 ` [PATCH v8 10/26] emul/ns16x50: implement RBR register dmukhin
2026-11-11 19:47 ` [PATCH v8 11/26] emul/ns16x50: implement THR register dmukhin
2026-11-11 19:47 ` [PATCH v8 12/26] emul/ns16x50: implement FCR register (write-only) dmukhin
2026-11-11 19:47 ` [PATCH v8 13/26] emul/ns16550: implement dump_state() hook dmukhin
2026-11-11 19:47 ` [PATCH v8 14/26] emul/vuart: add global UART emulator configuration knob dmukhin
2026-11-11 19:47 ` [PATCH v8 15/26] emul/ns16x50: enable emulator dmukhin
2026-11-11 19:47 ` [PATCH v8 16/26] emul/ns16x50: add Kconfig options dmukhin
2026-11-11 19:47 ` [PATCH v8 17/26] x86/domain: enable per-domain I/O port bitmaps dmukhin
2026-11-11 19:47 ` [PATCH v8 18/26] emul/ns16x50: resolve physical I/O port sharing dmukhin
2026-11-11 19:47 ` [PATCH v8 19/26] xen/domain: allocate d->irq_caps before arch-specific initialization dmukhin
2026-11-11 19:47 ` [PATCH v8 20/26] emul/ns16x50: resolve physical IRQ sharing dmukhin
2026-11-11 19:47 ` [PATCH v8 21/26] emul/ns16x50: implement IRQ emulation via vIOAPIC dmukhin
2026-11-11 19:47 ` [PATCH v8 22/26] MAINTAINERS: add myself as vUART framework maintainer dmukhin
2026-11-11 19:47 ` [PATCH v8 23/26] docs/fusa: add NS16x50 UART requirements for x86 dmukhin
2026-11-11 19:47 ` [PATCH v8 24/26] ci: add initial NS16550-compatible XTF test dmukhin
2026-11-11 19:47 ` [PATCH v8 25/26] ci: add smoke NS16550-compatible PVH Linux test dmukhin
2026-11-11 19:48 ` [PATCH v8 26/26] ci: do not merge: test dmukhin
2026-11-12 14:53 ` [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator Oleksii Kurochko
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.