Skip to contentSkip to conversation

x86: introduce NS16550-compatible UART emulator

PATCH In review [email protected] 5 participants 54 messages Nov 11 – Nov 20 RISC-V

lore
Share & tools
lore
#0 lore raw

v8 cover x86: introduce NS16550-compatible UART emulator

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/[email protected]/

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/[email protected]/

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/[email protected]/

Changes since v5:
- Split THR/RBR into two separate patches.
- Addressed feedback from v5.
- Link to v5: https://lore.kernel.org/xen-devel/[email protected]/

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/[email protected]/

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/[email protected]/

- Link to v3: https://lore.kernel.org/xen-devel/[email protected]/

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/[email protected]/

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/[email protected]

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
Signature · 1 line
2.56.0
#1 lore raw

PATCH v8 1/26 emul/vuart: introduce framework for UART emulators

From: Denis Mukhin <[email protected]>

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.

---
 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
xen/arch/arm/xen.lds.S +1 -0
index 613511390ff4..423b8e717539 100644
@@ -58,6 +58,7 @@ SECTIONS
        *(.rodata)
        *(.rodata.*)
        VPCI_ARRAY
+       VUART_ARRAY
        *(.data.rel.ro)
        *(.data.rel.ro.*)
 
xen/arch/ppc/xen.lds.S +1 -0
index 37256c8865a9..228dbe0055c1 100644
@@ -52,6 +52,7 @@ SECTIONS
         *(.rodata)
         *(.rodata.*)
         VPCI_ARRAY
+        VUART_ARRAY
         *(.data.rel.ro)
         *(.data.rel.ro.*)
 
xen/arch/riscv/xen.lds.S +1 -0
index d9375a961697..ca34974f5adf 100644
@@ -49,6 +49,7 @@ SECTIONS
         *(.rodata)
         *(.rodata.*)
         VPCI_ARRAY
+        VUART_ARRAY
         *(.data.rel.ro)
         *(.data.rel.ro.*)
 
xen/arch/x86/xen.lds.S +1 -0
index 8f943e11ead4..e57ab6ff74f3 100644
@@ -123,6 +123,7 @@ SECTIONS
        *(.rodata)
        *(.rodata.*)
        VPCI_ARRAY
+       VUART_ARRAY
        *(.data.rel.ro)
        *(.data.rel.ro.*)
 
xen/common/Kconfig +2 -0
index 5b289e444fa5..7f102a5ab3fc 100644
@@ -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
xen/common/Makefile +1 -0
index f69d47d18934..60f2d9087589 100644
@@ -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
xen/common/emul/Kconfig new file +6 -0
new file mode 100644
index 000000000000..7c6764d1756b
@@ -0,0 +1,6 @@
+menu "Domain Emulation Features"
+	visible if EXPERT
+
+source "common/emul/vuart/Kconfig"
+
+endmenu
xen/common/emul/Makefile new file +1 -0
new file mode 100644
index 000000000000..ae0b575c3901
@@ -0,0 +1 @@
+obj-$(CONFIG_VUART_FRAMEWORK) += vuart/
xen/common/emul/vuart/Kconfig new file +6 -0
new file mode 100644
index 000000000000..ce1b976b7da7
@@ -0,0 +1,6 @@
+config VUART_FRAMEWORK
+	bool
+
+menu "UART Emulation"
+
+endmenu
xen/common/emul/vuart/Makefile new file +1 -0
new file mode 100644
index 000000000000..97f792dc6641
@@ -0,0 +1 @@
+obj-y += vuart.o
xen/common/emul/vuart/ns165x50.c new file +0 -0
new file mode 100644
index 000000000000..e69de29bb2d1
xen/common/emul/vuart/vuart.c new file +173 -0

173 lines not on this page · raw message

xen/common/keyhandler.c +3 -0
index 7103bf20a7c9..b5012c9cb26d 100644
@@ -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 )
xen/drivers/char/console.c +5 -1
index e4df0bbc2eec..62ca32e5392a 100644
@@ -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. */
xen/include/xen/sched.h +5 -0
index ce1fa636db36..4fa7f62490ca 100644
@@ -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 {
xen/include/xen/serial.h +5 -0
index 3a71da767dd7..ec481893fede 100644
@@ -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 {
xen/include/xen/vuart.h new file +115 -0

115 lines not on this page · raw message

xen/include/xen/xen.lds.h +10 -0
index 958f8256b0fa..5e630d371471 100644
@@ -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 = .;   \
Signature · 1 line
2.56.0
#2 lore raw

Re: [PATCH v8 01/26] emul/vuart: introduce framework for UART emulators

Quoted 4 lines · On 11/11/26 8:47 PM, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Introduce a driver framework to abstract UART emulators in the hypervisor.

Quoted 8 lines

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.

Quoted 16 lines

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.

Quoted 12 lines
---
  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.

Quoted 16 lines
  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

[...]

xen/common/Makefile obj-$(CONFIG_DEVICE_TREE_PARSE) += device-tree/ · line 15

Quoted 9 lines
xen/common/Makefile +1 -0
index f69d47d18934..60f2d9087589 100644
@@ -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.

[...]

xen/common/emul/vuart/ns165x50.c

Quoted 3 lines
xen/common/emul/vuart/ns165x50.c new file +0 -0
new file mode 100644
index 000000000000..e69de29bb2d1

This adds an empty file whose name looks like a typo of ns16x50.c. Please drop it.

xen/common/emul/vuart/vuart.c line 4

Quoted 10 lines
xen/common/emul/vuart/vuart.c new file +4 -0
new file mode 100644
index 000000000000..bd3b756fb4d6
@@ -0,0 +1,173 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * UART emulator framework.
+ *

Is this line with only one start really needed?

xen/common/emul/vuart/vuart.c line 7

Quoted 3 lines
xen/common/emul/vuart/vuart.c new file +3 -0
+ */
+
+#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.

Quoted 13 lines
xen/common/emul/vuart/vuart.c new file +13 -0
+
+#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[];
+

xen/common/emul/vuart/vuart.c line 30

Quoted 10 lines
xen/common/emul/vuart/vuart.c new file +10 -0
+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.

xen/common/emul/vuart/vuart.c line 35

Quoted 5 lines
xen/common/emul/vuart/vuart.c new file +5 -0
+
+    return NULL;
+}
+
+const static struct vuart *

Nit: static const.

Quoted 12 lines
xen/common/emul/vuart/vuart.c new file +12 -0
+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,

xen/common/emul/vuart/vuart.c line 57

Quoted 10 lines
xen/common/emul/vuart/vuart.c new file +10 -0
+                                     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.

Quoted 21 lines
xen/common/emul/vuart/vuart.c new file +21 -0
+
+    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));

xen/common/emul/vuart/vuart.c line 88

Quoted 10 lines
xen/common/emul/vuart/vuart.c new file +10 -0
+    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().

xen/common/emul/vuart/vuart.c line 101

Quoted 13 lines
xen/common/emul/vuart/vuart.c new file +13 -0
+
+    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?

Quoted 6 lines
xen/common/emul/vuart/vuart.c new file +6 -0
+
+    return 0;
+
+ err_out2:
+    xvfree(vuart->info);
+ err_out1:

xen/common/emul/vuart/vuart.c line 117

Quoted 10 lines
xen/common/emul/vuart/vuart.c new file +10 -0
+    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()/ ?

Quoted 8 lines
xen/common/emul/vuart/vuart.c new file +8 -0
+ */
+void vuart_deinit(struct domain *d)
+{
+    struct vuart *vuart = d->console->vuart;
+
+    if ( vuart )
+    {
+        vuart->emulator->free(vuart->vdev);

xen/common/emul/vuart/vuart.c line 135

Quoted 10 lines
xen/common/emul/vuart/vuart.c new file +10 -0
+        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.

[...]

Quoted 11 lines
xen/drivers/char/console.c +1 -0
@@ -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>

xen/drivers/char/console.c static void __serial_rx(char c) · line 648

Quoted 10 lines
xen/drivers/char/console.c +2 -1
@@ -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 ().

xen/drivers/char/console.c static void __serial_rx(char c) · line 660

Quoted 9 lines
xen/drivers/char/console.c +2 -0
           */
          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?

xen/include/xen/sched.h line 27

Quoted 12 lines
xen/drivers/char/console.c +0 -0
  #ifdef CONFIG_SBSA_VUART_CONSOLE
      else
          /* Deliver input to the emulated UART. */
xen/include/xen/sched.h +1 -0
index ce1fa636db36..4fa7f62490ca 100644
@@ -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.

Quoted 22 lines
xen/include/xen/sched.h +4 -0
  #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 {
xen/include/xen/serial.h +0 -0
index 3a71da767dd7..ec481893fede 100644
@@ -32,10 +32,15 @@ enum serial_port_state {
 
  struct vuart_info {
      paddr_t base_addr;          /* Base address of the UART */

xen/include/xen/serial.h enum serial_port_state { · line 44

Quoted 10 lines
xen/include/xen/serial.h +5 -0
+    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.

xen/include/xen/vuart.h line 4

Quoted 12 lines
xen/include/xen/serial.h +0 -0
  struct serial_port {
xen/include/xen/vuart.h new file +4 -0
new file mode 100644
index 000000000000..b5785b3c6b8f
@@ -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?

Quoted 10 lines
xen/include/xen/vuart.h new file +10 -0
+ */
+
+#ifndef XEN_VUART_H
+#define XEN_VUART_H
+
+#include <xen/serial.h>
+#include <public/xen.h>
+
+struct vuart_emulator;
+

xen/include/xen/vuart.h line 24

Quoted 10 lines
xen/include/xen/vuart.h new file +10 -0
+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.

Quoted 42 lines
xen/include/xen/vuart.h new file +42 -0
+    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 \

xen/include/xen/vuart.h line 76

Quoted 10 lines
xen/include/xen/vuart.h new file +10 -0
+        __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().

Quoted 29 lines
xen/include/xen/vuart.h new file +29 -0
+
+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 */

xen/include/xen/vuart.h line 115

Quoted 10 lines
xen/include/xen/vuart.h new file +10 -0
+
+/*
+ * 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.

Quoted 5 lines
xen/include/xen/xen.lds.h +0 -0
index 958f8256b0fa..5e630d371471 100644

xen/include/xen/xen.lds.h line 239

Quoted 9 lines
xen/include/xen/xen.lds.h +6 -0
@@ -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

#3 lore raw

PATCH v8 2/26 xen/8250-uart: update definitions

In reply to [email protected]

From: Denis Mukhin <[email protected]>

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)".

---
 xen/drivers/char/ns16550.c  | 16 ++++++++--------
 xen/include/xen/8250-uart.h | 30 ++++++++++++++++++++++++++++--
 2 files changed, 36 insertions(+), 10 deletions(-)
xen/drivers/char/ns16550.c +8 -8
index be388c8e6582..4313071e6cfc 100644
@@ -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
xen/include/xen/8250-uart.h +28 -2
index d13352940c13..15cc4be5ff81 100644
@@ -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           */
Signature · 1 line
2.56.0
#4 lore raw

Re: [PATCH v8 02/26] xen/8250-uart: update definitions

Quoted 5 lines · On 11.11.2026 20:47, [email protected] wrote:

From: Denis Mukhin <[email protected]>

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?

Quoted 3 lines

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.

Quoted 23 lines
xen/include/xen/8250-uart.h +3 -0
@@ -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) */

xen/include/xen/8250-uart.h line 65

Quoted 10 lines
xen/include/xen/8250-uart.h +4 -1
+#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 ...

xen/include/xen/8250-uart.h line 115

Quoted 11 lines
xen/include/xen/8250-uart.h +4 -1
@@ -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

#5 lore raw

Re: [PATCH v8 02/26] xen/8250-uart: update definitions

In reply to [email protected]

On 11/11/26 8:47 PM, [email protected] wrote:

[...]

Quoted 25 lines
xen/include/xen/8250-uart.h +3 -0
index d13352940c13..15cc4be5ff81 100644
@@ -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) */

xen/include/xen/8250-uart.h line 65

Quoted 10 lines
xen/include/xen/8250-uart.h +4 -1
+#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?

xen/include/xen/8250-uart.h line 68

Quoted 3 lines
xen/include/xen/8250-uart.h +3 -0
+#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?

Quoted 4 lines
xen/include/xen/8250-uart.h +0 -0
  #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   */

xen/include/xen/8250-uart.h line 115

Quoted 10 lines
xen/include/xen/8250-uart.h +4 -1
@@ -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?

xen/include/xen/8250-uart.h line 125

Quoted 10 lines
xen/include/xen/8250-uart.h +10 -0
+
+/* 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.

xen/include/xen/8250-uart.h line 132

Quoted 7 lines
xen/include/xen/8250-uart.h +4 -0
+#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

#6 lore raw

PATCH v8 3/26 x86: fixup dependencies for asm/x86_emulate.h

In reply to [email protected]

From: Denis Mukhin <[email protected]>

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")

---
 xen/arch/x86/include/asm/x86_emulate.h | 1 +
 1 file changed, 1 insertion(+)
xen/arch/x86/include/asm/x86_emulate.h +1 -0
index 2b75f7f8e215..209d1e54e03f 100644
@@ -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>
Signature · 1 line
2.56.0
#7 lore raw

Re: [PATCH v8 03/26] x86: fixup dependencies for asm/x86_emulate.h

Quoted 4 lines · On 11/11/26 8:47 PM, [email protected] wrote:

From: Denis Mukhin <[email protected]>

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?

Quoted 2 lines

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.

Quoted 10 lines
---
  xen/arch/x86/include/asm/x86_emulate.h | 1 +
  1 file changed, 1 insertion(+)
xen/arch/x86/include/asm/x86_emulate.h +0 -0
index 2b75f7f8e215..209d1e54e03f 100644

xen/arch/x86/include/asm/x86_emulate.h line 16

Quoted 4 lines
xen/arch/x86/include/asm/x86_emulate.h +1 -0
@@ -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).

xen/arch/x86/include/asm/x86_emulate.h line 19

Quoted 3 lines
xen/arch/x86/include/asm/x86_emulate.h +0 -0
  #include <xen/lib.h>
  #include <asm/regs.h>
  #include <asm/x86-defns.h>

~ Oleksii

#8 lore raw

Re: [PATCH v8 03/26] x86: fixup dependencies for asm/x86_emulate.h

Quoted 19 lines · Oleksii Kurochko
Quoted 4 lines · On 11/11/26 8:47 PM, [email protected] wrote:

From: Denis Mukhin <[email protected]>

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?

Quoted 2 lines

Amends: 0a249bcb0279 ("x86: reduce dependencies on x86_emulate/x86_emulate.h")

Quoted 8 lines

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).

Quoted 9 lines

xen/arch/x86/include/asm/x86_emulate.h line 16

Quoted 7 lines
xen/arch/x86/include/asm/x86_emulate.h +1 -0
@@ -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 ...

Quoted 3 lines

xen/arch/x86/include/asm/x86_emulate.h line 19

Quoted 3 lines
xen/arch/x86/include/asm/x86_emulate.h +0 -0
  #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

#9 lore raw

PATCH v8 4/26 emul/ns16x50: implement emulator stub

In reply to [email protected]

From: Denis Mukhin <[email protected]>

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.

---
 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
xen/common/emul/vuart/Makefile +1 -0
index 97f792dc6641..fe904f6cb65d 100644
@@ -1 +1,2 @@
 obj-y += vuart.o
+obj-$(CONFIG_VUART_NS16X50) += ns16x50.o
xen/common/emul/vuart/ns16x50.c new file +417 -0

417 lines not on this page · raw message

Signature · 1 line
2.56.0
#10 lore raw

Re: [PATCH v8 04/26] emul/ns16x50: implement emulator stub

Quoted 19 lines · On 11/11/26 8:47 PM, [email protected] wrote:

From: Denis Mukhin <[email protected]>

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.

---
  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

xen/common/emul/vuart/Makefile +0 -0
index 97f792dc6641..fe904f6cb65d 100644

xen/common/emul/vuart/Makefile line 2

Quoted 2 lines
xen/common/emul/vuart/Makefile +1 -0
@@ -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.

Quoted 9 lines
xen/common/emul/vuart/ns16x50.c new file +3 -0
new file mode 100644
index 000000000000..6e1cfecbaa55
@@ -0,0 +1,417 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * NS16550-compatible UART Emulator.

xen/common/emul/vuart/ns16x50.c line 13

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c new file +10 -0
+ *
+ * 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.".

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c new file +10 -0
+ *
+ * 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

xen/common/emul/vuart/ns16x50.c line 33

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c new file +10 -0
+ *   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.

xen/common/emul/vuart/ns16x50.c line 38

Quoted 5 lines
xen/common/emul/vuart/ns16x50.c new file +5 -0
+#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.

xen/common/emul/vuart/ns16x50.c line 42

Quoted 4 lines
xen/common/emul/vuart/ns16x50.c new file +4 -0
+
+#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.

Quoted 13 lines
xen/common/emul/vuart/ns16x50.c new file +13 -0
+#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); \

xen/common/emul/vuart/ns16x50.c line 65

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c new file +10 -0
+} 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.

Quoted 14 lines
xen/common/emul/vuart/ns16x50.c new file +14 -0
+
+/*
+ * 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)

xen/common/emul/vuart/ns16x50.c line 89

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c new file +10 -0
+
+/*
+ * 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?

Quoted 15 lines
xen/common/emul/vuart/ns16x50.c new file +15 -0
+    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,

xen/common/emul/vuart/ns16x50.c line 114

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c new file +10 -0
+                                   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.

xen/common/emul/vuart/ns16x50.c line 118

Quoted 4 lines
xen/common/emul/vuart/ns16x50.c new file +4 -0
+
+/*
+ * 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.

Quoted 8 lines
xen/common/emul/vuart/ns16x50.c new file +8 -0
+static int ns16x50_io_write8(
+    struct vuart_ns16x50 *vdev, uint32_t reg, uint8_t *data)
+{
+    int rc = 0;
+
+    return rc;
+}
+

[ ... ]

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c new file +10 -0
+
+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;

xen/common/emul/vuart/ns16x50.c line 238

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c new file +10 -0
+    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.

Quoted 37 lines
xen/common/emul/vuart/ns16x50.c new file +37 -0
+
+        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",

xen/common/emul/vuart/ns16x50.c line 285

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c new file +10 -0
+                    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.

Quoted 13 lines
xen/common/emul/vuart/ns16x50.c new file +13 -0
+    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);
+}
+

xen/common/emul/vuart/ns16x50.c line 308

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c new file +10 -0
+/*
+ * 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.

Quoted 16 lines
xen/common/emul/vuart/ns16x50.c new file +16 -0
+}
+
+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;

xen/common/emul/vuart/ns16x50.c line 334

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c new file +10 -0
+    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.

xen/common/emul/vuart/ns16x50.c line 341

Quoted 7 lines
xen/common/emul/vuart/ns16x50.c new file +7 -0
+    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().

xen/common/emul/vuart/ns16x50.c line 343

Quoted 2 lines
xen/common/emul/vuart/ns16x50.c new file +2 -0
+
+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).

Quoted 5 lines
xen/common/emul/vuart/ns16x50.c new file +5 -0
+{
+    struct vuart_ns16x50 *vdev = arg;
+
+    ASSERT(vdev);
+}

xen/common/emul/vuart/ns16x50.c line 358

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c new file +10 -0
+
+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.

xen/common/emul/vuart/ns16x50.c line 366

Quoted 8 lines
xen/common/emul/vuart/ns16x50.c new file +8 -0
+    }
+
+    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?

xen/common/emul/vuart/ns16x50.c line 379

Quoted 13 lines
xen/common/emul/vuart/ns16x50.c new file +13 -0
+
+    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.

xen/common/emul/vuart/ns16x50.c line 390

Quoted 11 lines
xen/common/emul/vuart/ns16x50.c new file +11 -0
+
+    rc = ns16x50_init(vdev);
+    if ( rc )
+    {
+        xvfree(vdev);
+        return ERR_PTR(rc);
+    }
+
+    return vdev;
+}
+

[ ... ]

~ Oleksii

#11 lore raw

PATCH v8 5/26 emul/ns16x50: implement DLL/DLM registers

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Add DLL/DLM registers emulation.

DLL/DLM registers report hardcoded 115200 baud rate to the guest OS.

---
 xen/common/emul/vuart/ns16x50.c | 82 +++++++++++++++++++++++++++++++++
 1 file changed, 82 insertions(+)
xen/common/emul/vuart/ns16x50.c +82 -0
index 6e1cfecbaa55..80805ee6ecc6 100644
@@ -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)
Signature · 1 line
2.56.0
#12 lore raw

Re: [PATCH v8 05/26] emul/ns16x50: implement DLL/DLM registers

Quoted 5 lines · On 11/11/26 8:47 PM, [email protected] wrote:

From: Denis Mukhin <[email protected]>

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?

Quoted 11 lines
---
  xen/common/emul/vuart/ns16x50.c | 82 +++++++++++++++++++++++++++++++++
  1 file changed, 82 insertions(+)
xen/common/emul/vuart/ns16x50.c +0 -0
index 6e1cfecbaa55..80805ee6ecc6 100644

xen/common/emul/vuart/ns16x50.c static inline void ns16x50_unlock(struct vuart_ns16x50 *vdev, · line 128

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c +5 -0
@@ -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.

Quoted 6 lines
xen/common/emul/vuart/ns16x50.c +6 -0
+        /* DLAB=1 */
+        if ( regs[UART_LCR] & UART_LCR_DLAB )
+        {
+            regs[NS16X50_REGS_NUM + UART_DLL] = val;
+            break;
+        }

xen/common/emul/vuart/ns16x50.c static inline void ns16x50_unlock(struct vuart_ns16x50 *vdev, · line 144

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c +10 -0
+
+        /* 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).

Quoted 13 lines
xen/common/emul/vuart/ns16x50.c +8 -0
+        /* DLAB=0 */
+        break;
+
+    default:
+        rc = -EINVAL;
+        break;
+    }
+
      return rc;
  }
 
@@ -131,8 +160,17 @@ static int ns16x50_io_write8(
  static int ns16x50_io_write16(

xen/common/emul/vuart/ns16x50.c static int ns16x50_io_write8( · line 170

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c +6 -0
      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.

Quoted 65 lines
xen/common/emul/vuart/ns16x50.c +41 -0
+        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;

xen/common/emul/vuart/ns16x50.c static int cf_check ns16x50_io_handle( · line 389

Quoted 4 lines
xen/common/emul/vuart/ns16x50.c +1 -0
@@ -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.

xen/common/emul/vuart/ns16x50.c static int cf_check ns16x50_io_handle( · line 398

Quoted 9 lines
xen/common/emul/vuart/ns16x50.c +5 -0
+
      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

#13 lore raw

PATCH v8 6/26 emul/ns16x50: implement SCR register

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Add SCR register emulation to the I/O port handler. Firmware (e.g. OVMF) may use SCR during the guest OS boot.

---
 xen/common/emul/vuart/ns16x50.c | 9 +++++++++
 1 file changed, 9 insertions(+)
xen/common/emul/vuart/ns16x50.c +9 -0
index 80805ee6ecc6..2371aa32d03f 100644
@@ -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;
Signature · 1 line
2.56.0
#14 lore raw

Re: [PATCH v8 06/26] emul/ns16x50: implement SCR register

Quoted 15 lines · On 11/11/26 8:47 PM, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Add SCR register emulation to the I/O port handler. Firmware (e.g. OVMF) may use SCR during the guest OS boot.

---
  xen/common/emul/vuart/ns16x50.c | 9 +++++++++
  1 file changed, 9 insertions(+)
xen/common/emul/vuart/ns16x50.c +0 -0
index 80805ee6ecc6..2371aa32d03f 100644

xen/common/emul/vuart/ns16x50.c static int ns16x50_io_write8( · line 148

Quoted 4 lines
xen/common/emul/vuart/ns16x50.c +1 -0
@@ -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.

Quoted 8 lines
xen/common/emul/vuart/ns16x50.c +4 -0
+    case UART_SCR:
+        regs[UART_SCR] = val;
+        break;
+
      default:
          rc = -EINVAL;
          break;

xen/common/emul/vuart/ns16x50.c static int ns16x50_io_read8( · line 248

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c +4 -0
@@ -234,6 +239,10 @@ static int ns16x50_io_read8(
          /* DLAB=0 */
          break;
 
+    case UART_SCR:
+        val = regs[UART_SCR];
+        break;
+
      default:
          rc = -EINVAL;
          break;

~ Oleksii

#15 lore raw

PATCH v8 7/26 emul/ns16x50: implement IER/IIR registers

In reply to [email protected]

From: Denis Mukhin <[email protected]>

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.

---
 xen/common/emul/vuart/ns16x50.c | 112 ++++++++++++++++++++++++++++++++
 1 file changed, 112 insertions(+)
xen/common/emul/vuart/ns16x50.c +112 -0
index 2371aa32d03f..7c5cdc99cfa7 100644
@@ -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;
 }
 
Signature · 1 line
2.56.0
#16 lore raw

Re: [PATCH v8 07/26] emul/ns16x50: implement IER/IIR registers

Quoted 10 lines · On 11/11/26 8:47 PM, [email protected] wrote:

From: Denis Mukhin <[email protected]>

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"

Quoted 54 lines
---
  xen/common/emul/vuart/ns16x50.c | 112 ++++++++++++++++++++++++++++++++
  1 file changed, 112 insertions(+)
xen/common/emul/vuart/ns16x50.c +33 -0
index 2371aa32d03f..7c5cdc99cfa7 100644
@@ -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.
+     */

xen/common/emul/vuart/ns16x50.c static inline void ns16x50_unlock(struct vuart_ns16x50 *vdev, · line 158

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c +10 -0
+    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...

xen/common/emul/vuart/ns16x50.c static inline void ns16x50_unlock(struct vuart_ns16x50 *vdev, · line 169

Quoted 11 lines
xen/common/emul/vuart/ns16x50.c +11 -0
+    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.

Quoted 4 lines
xen/common/emul/vuart/ns16x50.c +4 -0
+    if ( i == ARRAY_SIZE(iir_by_prio) )
+        iir |= UART_IIR_NOINT;
+    else
+        iir |= iir_by_prio[i].iir;

xen/common/emul/vuart/ns16x50.c static inline void ns16x50_unlock(struct vuart_ns16x50 *vdev, · line 183

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c +10 -0
+
+    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.

Quoted 6 lines
xen/common/emul/vuart/ns16x50.c +6 -0
+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);

xen/common/emul/vuart/ns16x50.c static inline void ns16x50_unlock(struct vuart_ns16x50 *vdev, · line 199

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c +10 -0
+
+    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.

xen/common/emul/vuart/ns16x50.c static inline void ns16x50_unlock(struct vuart_ns16x50 *vdev, · line 204

Quoted 5 lines
xen/common/emul/vuart/ns16x50.c +5 -0
+        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.

xen/common/emul/vuart/ns16x50.c static inline void ns16x50_unlock(struct vuart_ns16x50 *vdev, · line 206

Quoted 2 lines
xen/common/emul/vuart/ns16x50.c +2 -0
+
+    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.

Quoted 9 lines
xen/common/emul/vuart/ns16x50.c +5 -0
+                  info->irq,
+                  iir,
+                  asserted ? "assert" : "deassert");
+}
+
  /*
   * Emulate 8-bit write access to ns16x50 register.
   */

xen/common/emul/vuart/ns16x50.c static int ns16x50_io_write8( · line 245

Quoted 7 lines
xen/common/emul/vuart/ns16x50.c +4 -0
@@ -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.

Quoted 12 lines
xen/common/emul/vuart/ns16x50.c +2 -0
          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;
  }
 

[...]

Quoted 9 lines
xen/common/emul/vuart/ns16x50.c +1 -0
@@ -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;

xen/common/emul/vuart/ns16x50.c static int ns16x50_init(void *arg) · line 542

Quoted 5 lines
xen/common/emul/vuart/ns16x50.c +2 -0
@@ -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.

xen/common/emul/vuart/ns16x50.c static int ns16x50_init(void *arg) · line 545

Quoted 3 lines
xen/common/emul/vuart/ns16x50.c +0 -0
      return 0;
  }
 

~ Oleksii

#17 lore raw

PATCH v8 8/26 emul/ns16x50: implement LCR/LSR registers

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Add LCR/LSR registers implementation to the I/O port handler.

Add implementation of ns16x50_dlab_get() and ns16x50_iir_check_lsi().

---
 xen/common/emul/vuart/ns16x50.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
xen/common/emul/vuart/ns16x50.c +22 -1
index 7c5cdc99cfa7..e80815ccf6e2 100644
@@ -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;
Signature · 1 line
2.56.0
#18 lore raw

Re: [PATCH v8 08/26] emul/ns16x50: implement LCR/LSR registers

Quoted 5 lines · On 11/11/26 8:47 PM, [email protected] wrote:

From: Denis Mukhin <[email protected]>

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).

Quoted 11 lines
---
  xen/common/emul/vuart/ns16x50.c | 23 ++++++++++++++++++++++-
  1 file changed, 22 insertions(+), 1 deletion(-)
xen/common/emul/vuart/ns16x50.c +0 -0
index 7c5cdc99cfa7..e80815ccf6e2 100644

xen/common/emul/vuart/ns16x50.c do { \ · line 69

Quoted 6 lines
xen/common/emul/vuart/ns16x50.c +3 -0
@@ -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 /* ... */

xen/common/emul/vuart/ns16x50.c do { \ · line 70

Quoted 1 line
xen/common/emul/vuart/ns16x50.c +1 -0
+#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?

xen/common/emul/vuart/ns16x50.c do { \ · line 74

Quoted 4 lines
xen/common/emul/vuart/ns16x50.c +1 -0
+
  /*
   * Number of supported registers in the UART.
   */

Nit: Single line comment should be enough here so please use /* ... */

Quoted 14 lines
xen/common/emul/vuart/ns16x50.c +2 -1
@@ -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:

xen/common/emul/vuart/ns16x50.c static int ns16x50_io_write8( · line 263

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c +4 -0
+        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().

Quoted 19 lines
xen/common/emul/vuart/ns16x50.c +9 -0
          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;

xen/common/emul/vuart/ns16x50.c static void ns16x50_reset(struct vuart_ns16x50 *vdev) · line 532

Quoted 5 lines
xen/common/emul/vuart/ns16x50.c +2 -0
@@ -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).

xen/common/emul/vuart/ns16x50.c static void ns16x50_reset(struct vuart_ns16x50 *vdev) · line 535

Quoted 3 lines
xen/common/emul/vuart/ns16x50.c +0 -0
      /* 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

#19 lore raw

PATCH v8 9/26 emul/ns16x50: implement MCR/MSR registers

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Add MCR/MSR registers emulation to the I/O port handler.

Add implementation of ns16x50_iir_check_msi().

---
 xen/common/emul/vuart/ns16x50.c | 76 ++++++++++++++++++++++++++++++++-
 1 file changed, 75 insertions(+), 1 deletion(-)
xen/common/emul/vuart/ns16x50.c +75 -1
index e80815ccf6e2..9f530e589495 100644
@@ -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;
Signature · 1 line
2.56.0
#20 lore raw

Re: [PATCH v8 09/26] emul/ns16x50: implement MCR/MSR registers

Quoted 50 lines · On 11/11/26 8:47 PM, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Add MCR/MSR registers emulation to the I/O port handler.

Add implementation of ns16x50_iir_check_msi().

---
  xen/common/emul/vuart/ns16x50.c | 76 ++++++++++++++++++++++++++++++++-
  1 file changed, 75 insertions(+), 1 deletion(-)
xen/common/emul/vuart/ns16x50.c +12 -1
index e80815ccf6e2..9f530e589495 100644
@@ -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;
 

xen/common/emul/vuart/ns16x50.c static int ns16x50_io_write8( · line 268

Quoted 4 lines
xen/common/emul/vuart/ns16x50.c +1 -0
@@ -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.

Quoted 8 lines
xen/common/emul/vuart/ns16x50.c +8 -0
+        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",

xen/common/emul/vuart/ns16x50.c static int ns16x50_io_write8( · line 286

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c +10 -0
+                         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 )".

xen/common/emul/vuart/ns16x50.c static int ns16x50_io_write8( · line 289

Quoted 3 lines
xen/common/emul/vuart/ns16x50.c +3 -0
+        /* 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.

Quoted 9 lines
xen/common/emul/vuart/ns16x50.c +9 -0
+            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;
+        }

xen/common/emul/vuart/ns16x50.c static int ns16x50_io_write8( · line 308

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c +10 -0
+        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) )

xen/common/emul/vuart/ns16x50.c static int ns16x50_io_write8( · line 313

Quoted 5 lines
xen/common/emul/vuart/ns16x50.c +5 -0
+        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);ч

xen/common/emul/vuart/ns16x50.c static int ns16x50_io_write8( · line 317

Quoted 4 lines
xen/common/emul/vuart/ns16x50.c +4 -0
+
+        break;
+    }
+

~ Oleksii

#21 lore raw

PATCH v8 10/26 emul/ns16x50: implement RBR register

In reply to [email protected]

From: Denis Mukhin <[email protected]>

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.

---
 xen/common/emul/vuart/ns16x50.c | 132 +++++++++++++++++++++++++++++++-
 1 file changed, 130 insertions(+), 2 deletions(-)
xen/common/emul/vuart/ns16x50.c +130 -2
index 9f530e589495..d04caf2baf2c 100644
@@ -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);
Signature · 1 line
2.56.0
#22 lore raw

Re: [PATCH v8 10/26] emul/ns16x50: implement RBR register

Quoted 3 lines · On 11/11/26 8:47 PM, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Add RBR register emulation to the I/O port handlder.

Nit: s/handlder/handler/ (also in 11/26).

Quoted 75 lines

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.

---
  xen/common/emul/vuart/ns16x50.c | 132 +++++++++++++++++++++++++++++++-
  1 file changed, 130 insertions(+), 2 deletions(-)
xen/common/emul/vuart/ns16x50.c +43 -0
index 9f530e589495..d04caf2baf2c 100644
@@ -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;
+

xen/common/emul/vuart/ns16x50.c static inline void ns16x50_unlock(struct vuart_ns16x50 *vdev, · line 178

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c +10 -0
+    /*
+     * 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;
+    }

On overrun this throws away the whole 1 KiB of buffered input and keeps the new character. A 16550 in FIFO mode does the opposite (SLLS177I, 7.7.8, LSR bit 1): "an overrun error occurs only after the FIFO is full and the next character has been completely received in the shift register. [...] The character in the shift register is overwritten, but it is not transferred to the FIFO." I.e. set OE and drop the new character. Also, the comment talks about THR where RBR is meant.

Also, the comment is mentioning FIFO-less but here we are implementing FIFO-based UART. Don't we won't then separate such cases? (Another question will it be really totally wrong to still have FIFO-based emulation for FIFO-less UARTs which are mentioned in the comment?)

xen/common/emul/vuart/ns16x50.c static inline void ns16x50_unlock(struct vuart_ns16x50 *vdev, · line 190

Quoted 12 lines
xen/common/emul/vuart/ns16x50.c +12 -0
+    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);

Nit: s/tranceiver/transceiver/. Also, all callers check DLAB first, but it's guest-controlled state on a guest-reachable path; ASSERT() seems enough.

Quoted 18 lines
xen/common/emul/vuart/ns16x50.c +6 -1
+
+    /* 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)

xen/common/emul/vuart/ns16x50.c static int ns16x50_io_read8( · line 477

Quoted 8 lines
xen/common/emul/vuart/ns16x50.c +5 -0
@@ -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;

OE is cleared by reading LSR, not RBR: "OE is cleared every time the CPU reads the contents of the LSR" (same section), which 08/26 already implements. Clearing it here means a driver that reads RBR before LSR never sees the overrun.

xen/common/emul/vuart/ns16x50.c static int ns16x50_io_read8( · line 483

Quoted 6 lines
xen/common/emul/vuart/ns16x50.c +6 -0
+
+        if ( ns16x50_fifo_rx_getchar(vdev, &val) )
+            regs[UART_LSR] &= ~UART_LSR_DR;
+        else
+            regs[UART_LSR] |= UART_LSR_DR;
+

LSR.DR is just "RX FIFO not empty", which ns16x50_iir_check_rda() already computes from the FIFO. Storing it in regs[UART_LSR] as well means four places have to update it by hand: the RBR read here, put_rx(), the FCR RX clear (12/26) and THR in loopback mode (11/26). They're all correct today, but the two copies can drift apart with the next change. Computing DR from the FIFO when LSR is read would remove those updates and keep LSR and IIR consistent by construction:

    case UART_LSR:
        val = regs[UART_LSR] & ~UART_LSR_DR;
        if ( !ns16x50_fifo_rx_empty(vdev) )
            val |= UART_LSR_DR;
        regs[UART_LSR] &= ~UART_LSR_MASK;
        break;

Something like that?

Quoted 7 lines
xen/common/emul/vuart/ns16x50.c +0 -0
          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);
  }
 

xen/common/emul/vuart/ns16x50.c static void cf_check ns16x50_free(void *arg) · line 787

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c +10 -0
+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);

Don't we have the issue with having a such lock here?

This is reached from __serial_rx(), and on x86, when the physical UART
uses its IRQ, that is hard IRQ context: ns16550_interrupt() ->
serial_rx_interrupt() -> serial_rx() -> __serial_rx() -> vuart_put_rx().
vdev->lock is otherwise taken by ns16x50_access() with IRQs enabled, so
a serial IRQ arriving on a pCPU whose vCPU is inside ns16x50_access()
spins forever, and CONFIG_DEBUG_LOCKS' check_lock() should trip on the
mixed IRQ safety. The same applies to d->arch.hvm.irq_lock, taken under
it via ns16x50_irq_check() -> hvm_isa_irq_assert(). Making vdev->lock
IRQ-safe isn't enough because of the latter. If I didn't mistake something then deferring the delivery out of IRQ context (e.g. queue the character and let a tasklet update LSR/IIR and the IRQ line) would avoid both.

xen/common/emul/vuart/ns16x50.c static void cf_check ns16x50_free(void *arg) · line 792

Quoted 5 lines
xen/common/emul/vuart/ns16x50.c +5 -0
+
+    regs = vdev->regs;
+
+    if ( vdev->regs[UART_LCR] & UART_LCR_DLAB )
+        ns16x50_debug(vdev, "THR/RBR access disabled: DLAB=1\n");

DLAB only selects what offsets 0 and 1 decode to; reception continues, and the character is readable from RBR once DLAB is cleared. Dropping input is right for loopback (SIN is disconnected) and arguably for DLL == 0, but not here. (Each dropped character also produces a "failed to process console input" warning, see my reply to 01/26.)

(ns16x50_is_tranceiver_enabled() would then need to lose its BUG_ON():
it only looks at the stored DLL, which doesn't depend on DLAB.)

Quoted 5 lines
xen/common/emul/vuart/ns16x50.c +5 -0
+    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

xen/common/emul/vuart/ns16x50.c static void cf_check ns16x50_free(void *arg) · line 807

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c +10 -0
+    {
+        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);

The guest's tty already echoes the input, so every typed character appears twice on the Xen console, and the two streams interleave with guest output. Worse, the guest can't turn this echo off: when the guest disables echo (e.g. login prompting for a password), the characters are still printed by Xen. I'd drop it. Also, "iff Xen console input is owned by ns16x50 domain" is always true here, as __serial_rx() only calls this for the domain owning the input.

Quoted 6 lines
xen/common/emul/vuart/ns16x50.c +6 -0
+
+        if ( ns16x50_fifo_rx_putchar(vdev, ch) )
+            regs[UART_LSR] |= UART_LSR_OE;
+
+        regs[UART_LSR] |= UART_LSR_DR;
+

xen/common/emul/vuart/ns16x50.c static void cf_check ns16x50_free(void *arg) · line 824

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c +10 -0
+        /* TODO: check FCR when to fire an interrupt */
+        ns16x50_irq_check(vdev);> +
+        rc = 0;
+    }
+
+    ns16x50_unlock(vdev, &flags);
+
+    return rc;
+}
+

~ Oleksii

#23 lore raw

PATCH v8 11/26 emul/ns16x50: implement THR register

In reply to [email protected]

From: Denis Mukhin <[email protected]>

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.

---
 xen/common/emul/vuart/ns16x50.c | 121 +++++++++++++++++++++++++++++++-
 1 file changed, 120 insertions(+), 1 deletion(-)
xen/common/emul/vuart/ns16x50.c +120 -1
index d04caf2baf2c..8d3845a8a4cd 100644
@@ -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)
Signature · 1 line
2.56.0
#24 lore raw

Re: [PATCH v8 11/26] emul/ns16x50: implement THR register

Quoted 7 lines · On 11/11/26 8:47 PM, [email protected] wrote:

From: Denis Mukhin <[email protected]>

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

Nit: s/handlder/handler/, s/FIFOs is/FIFO is/.

Quoted 25 lines

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.

---
  xen/common/emul/vuart/ns16x50.c | 121 +++++++++++++++++++++++++++++++-
  1 file changed, 120 insertions(+), 1 deletion(-)
xen/common/emul/vuart/ns16x50.c +0 -0
index d04caf2baf2c..8d3845a8a4cd 100644
@@ -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)

xen/common/emul/vuart/ns16x50.c static int ns16x50_fifo_rx_putchar(struct vuart_ns16x50 *vdev, char c) · line 194

Quoted 9 lines
xen/common/emul/vuart/ns16x50.c +6 -0
@@ -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);
+}

Sorry, for noticing that only now but I think it is necessary to raise this question. Is it okay to use xencons_interface for FIFOs? xencons_interface covers 1024 bytes (sizeof(cons->in)) but real UART is covering 16 bytes so overrun happens only after overflow of 1024 (not 16). I can assume that for emulator it could be fine but such behaviour differs from the datasheet, so, at least, it could/should be mentioned in the comment (where cons member of struct vurat_ns16x50 is declared) that such behaviour is okay (if it is really okay?).

xen/common/emul/vuart/ns16x50.c static int ns16x50_fifo_rx_putchar(struct vuart_ns16x50 *vdev, char c) · line 205

Quoted 11 lines
xen/common/emul/vuart/ns16x50.c +11 -0
+
+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.
+ */

Nit: the comment here doesn't require to be multiline comment.

Quoted 7 lines
xen/common/emul/vuart/ns16x50.c +7 -0
+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));

xen/common/emul/vuart/ns16x50.c static int ns16x50_fifo_rx_putchar(struct vuart_ns16x50 *vdev, char c) · line 222

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c +10 -0
+    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;

Nit: out_cons is where the oldest data starts, and the second part is the wrapped-around remainder at the start of the array; "Tail" and "Head" read the other way round. Maybe "Up to the end of the buffer" / "Wrapped-around part".

Quoted 20 lines
xen/common/emul/vuart/ns16x50.c +20 -0
+
+    /* 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) )

xen/common/emul/vuart/ns16x50.c static int ns16x50_fifo_rx_putchar(struct vuart_ns16x50 *vdev, char c) · line 252

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c +10 -0
+        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);

Output is only printed once a line is complete. Nothing is lost, but
anything not followed by a newline shows up late: the "login: "
prompt, a shell prompt, and the guest's echo of typed characters all
appear only once the next newline arrives, i.e. after Enter. I suspect this is what the echo in ns16x50_put_rx() (10/26) compensates for, at the cost of printing every typed character twice and of printing input the guest deliberately doesn't echo (passwords).

vpl011_write_data_xen() on Arm handles this by printing each character right away while the domain owns the console input, and line-buffering only otherwise. Doing the same here would make the console interactive and let the echo in put_rx() go.

Also, ch == '\0' can't be true here, as is_console_printable() has
already filtered it out. And ns16x50_fifo_tx_full() is called twice to check the same so shouldn't this if be combined with 'else' branch of previous if () statetment?

Quoted 15 lines
xen/common/emul/vuart/ns16x50.c +3 -1
+}
+
  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)

xen/common/emul/vuart/ns16x50.c static int ns16x50_io_write8( · line 391

Quoted 9 lines
xen/common/emul/vuart/ns16x50.c +6 -0
@@ -316,6 +383,38 @@ static int ns16x50_io_write8(
          }
 
          /* DLAB=0 */
+        if ( !ns16x50_is_tranceiver_enabled(vdev) )
+            break;
+
+        /*
+         * THR is no longer empty.
+         */

Nit: the comment here doesn't require to be multiline comment.

Quoted 30 lines
xen/common/emul/vuart/ns16x50.c +26 -0
+        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 */

xen/common/emul/vuart/ns16x50.c static int ns16x50_io_write8( · line 435

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c +6 -0
@@ -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) );

Nit: no spaces inside the outer parentheses.

xen/common/emul/vuart/ns16x50.c static int ns16x50_io_write8( · line 438

Quoted 3 lines
xen/common/emul/vuart/ns16x50.c +0 -0
          }
          break;
 

[...]

~ Oleksii

#25 lore raw

PATCH v8 12/26 emul/ns16x50: implement FCR register (write-only)

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Add emulation logic for FCR register.

Note, that does not hook FIFO interrupt moderation to the FIFO management code for simplicity.

---
 xen/common/emul/vuart/ns16x50.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
xen/common/emul/vuart/ns16x50.c +33 -0
index 8d3845a8a4cd..404e12311090 100644
@@ -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;
Signature · 1 line
2.56.0
#26 lore raw

Re: [PATCH v8 12/26] emul/ns16x50: implement FCR register (write-only)

Quoted 17 lines · On 11/11/26 8:47 PM, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Add emulation logic for FCR register.

Note, that does not hook FIFO interrupt moderation to the FIFO management code for simplicity.

---
  xen/common/emul/vuart/ns16x50.c | 33 +++++++++++++++++++++++++++++++++
  1 file changed, 33 insertions(+)
xen/common/emul/vuart/ns16x50.c +0 -0
index 8d3845a8a4cd..404e12311090 100644

xen/common/emul/vuart/ns16x50.c static int ns16x50_io_write8( · line 442

Quoted 7 lines
xen/common/emul/vuart/ns16x50.c +4 -0
@@ -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);

Bits 4 and 5 are indeed reserved on the TL16C550C ("reserved for future use"), so warning is fine; with a local mask (02/26) the two checks collapse into one. Nit: %#x, as in the MCR warnings.

xen/common/emul/vuart/ns16x50.c static int ns16x50_io_write8( · line 452

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c +10 -0
+
+        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;
+        }

One of the places that keeps LSR.DR in sync by hand, see 10/26.

xen/common/emul/vuart/ns16x50.c static int ns16x50_io_write8( · line 456

Quoted 4 lines
xen/common/emul/vuart/ns16x50.c +4 -0
+
+        if ( val & UART_FCR_CLTX )
+        {
+            ns16x50_fifo_tx_reset(vdev);

The TX "FIFO" here is the line buffer of 11/26: everything in it has already been reported to the guest as transmitted (THRE and TEMT are set again right after each THR write), it just hasn't been printed yet because no newline arrived. Resetting it discards output the guest considers sent, e.g. a prompt without a trailing newline, whenever the guest clears the TX FIFO. Linux does that in serial8250_do_startup() and serial8250_do_shutdown(), via serial8250_clear_fifos(). Since TEMT says the transmitter is empty, there is nothing left to clear from the guest's point of view; I'd flush the buffer here instead of dropping it (or leave it alone).

xen/common/emul/vuart/ns16x50.c static int ns16x50_io_write8( · line 466

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c +10 -0
+
+            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;

The datasheet (SLLS177I, 7.7.2) also says for bit 0: "Changing this bit clears the FIFOs", which isn't emulated: toggling FCR.ENABLE without CLRX/CLTX leaves stale RX data in place. QEMU handles it by forcing both clear bits when the enable bit changes:

    if ((val ^ s->fcr) & UART_FCR_FE) {
        val |= UART_FCR_XFR | UART_FCR_RFR;
    }

(and with the above, the TX part would then be a flush rather than a drop.)

As for the mask, UART_FCR_RTB0 | UART_FCR_RTB1 is UART_FCR_TRG14, see my reply to 02/26.

xen/common/emul/vuart/ns16x50.c static int ns16x50_io_write8( · line 474

Quoted 8 lines
xen/common/emul/vuart/ns16x50.c +5 -0
+
+        regs[UART_FCR] = val;
+
+        break;
+
      case UART_LCR:
          regs[UART_LCR] = val;
          break;

~ Oleksii

#27 lore raw

PATCH v8 13/26 emul/ns16550: implement dump_state() hook

In reply to [email protected]

From: Denis Mukhin <[email protected]>

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.

---
 xen/common/emul/vuart/ns16x50.c | 64 ++++++++++++++++++++++++++++++++-
 1 file changed, 63 insertions(+), 1 deletion(-)
xen/common/emul/vuart/ns16x50.c +63 -1
index 404e12311090..cda21c15cf78 100644
@@ -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,       \
 }
 
Signature · 1 line
2.56.0
#28 lore raw

Re: [PATCH v8 13/26] emul/ns16550: implement dump_state() hook

Quoted 1 line · On 11/11/26 8:47 PM, [email protected] wrote:

From: Denis Mukhin <[email protected]>

At the commit subject:

Nit: emul/ns16x50: like the rest of the series.

Quoted 15 lines

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.

---
  xen/common/emul/vuart/ns16x50.c | 64 ++++++++++++++++++++++++++++++++-
  1 file changed, 63 insertions(+), 1 deletion(-)
xen/common/emul/vuart/ns16x50.c +0 -0
index 404e12311090..cda21c15cf78 100644

xen/common/emul/vuart/ns16x50.c static int ns16x50_io_read( · line 733

Quoted 6 lines
xen/common/emul/vuart/ns16x50.c +3 -0
@@ -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

With the whole body under CONFIG_VUART_NS16X50_DEBUG, the 'q' keyhandler prints nothing for the vUART on a normal build, which is where a state dump is most useful. The function doesn't cost anything until someone presses 'q'; I'd drop the #ifdef (or, if it must stay, leave .dump_state NULL instead of registering an empty hook).

xen/common/emul/vuart/ns16x50.c static int ns16x50_io_read( · line 740

Quoted 7 lines
xen/common/emul/vuart/ns16x50.c +7 -0
+    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";

As mentioned in my reply to the cover letter, info is used before it is assigned; gcc rejects this with -Werror=uninitialized, so the series doesn't build with CONFIG_VUART_NS16X50_DEBUG=y. Computing as after info = vdev->info fixes it.

Quoted 5 lines
xen/common/emul/vuart/ns16x50.c +5 -0
+    unsigned long flags;
+
+    ASSERT(vdev);
+
+    /* Allow printing state in case of a deadlock. */

xen/common/emul/vuart/ns16x50.c static int ns16x50_io_read( · line 755

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c +10 -0
+    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);

Nits on the format strings: PRIpaddr for base_addr, %u for irq and the XENCONS_RING_IDX values, %zu for ARRAY_SIZE(), and PRIx8 for UART_RBR and friends, which are plain ints. Also the continuation lines are indented one column too far.

Quoted 5 lines
xen/common/emul/vuart/ns16x50.c +5 -0
+
+    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);
+

xen/common/emul/vuart/ns16x50.c static int ns16x50_io_read( · line 770

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c +10 -0
+    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]);

in_prod and out_prod point at the next free slot, so these print whatever stale byte happens to be there. For RBR, the character the guest will read next is in[in_cons] (only meaningful if the FIFO isn't empty); for THR, the last character written is out[out_prod - 1].

Quoted 20 lines
xen/common/emul/vuart/ns16x50.c +16 -0
+
+    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)

xen/common/emul/vuart/ns16x50.c static void ns16x50_access(int dir, unsigned int type, · line 854

Quoted 5 lines
xen/common/emul/vuart/ns16x50.c +2 -0
@@ -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);

ns16x50_log_level is at most 2, so this never runs (see 04/26). If it
did, it would run with vdev->lock held, so the trylock in
ns16x50_dump_state() would fail and print nothing. I'd drop both
call sites added here and keep dump_state() for the keyhandler only. Another call site is here ...

xen/common/emul/vuart/ns16x50.c static int cf_check ns16x50_put_rx(void *arg, char ch) · line 1034

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c +3 -0
+
      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);

... ^.

xen/common/emul/vuart/ns16x50.c static int cf_check ns16x50_put_rx(void *arg, char ch) · line 1047

Quoted 13 lines
xen/common/emul/vuart/ns16x50.c +2 -1
+
      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,       \
  }
 

~ Oleksii

#29 lore raw

PATCH v8 14/26 emul/vuart: add global UART emulator configuration knob

In reply to [email protected]

From: Denis Mukhin <[email protected]>

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.

---
 xen/common/emul/vuart/vuart.c | 85 +++++++++++++++++++++++++++++++++++
 1 file changed, 85 insertions(+)
xen/common/emul/vuart/vuart.c +85 -0
index bd3b756fb4d6..0df29540b1b6 100644
@@ -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)
 {
Signature · 1 line
2.56.0
#30 lore raw

Re: [PATCH v8 14/26] emul/vuart: add global UART emulator configuration knob

Quoted 4 lines · On 11/11/26 8:47 PM, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Introduce new command line setting vuart=domid[:compatible,name] for configuring UART emulator per given domain.

A new command line option needs an entry in docs/misc/xen-command-line.pandoc; I don't see one in the series.

Quoted 11 lines

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

Is name really optional? Look my reply below ([A]).

Quoted 14 lines

values will be chosen.

Note, documentation update is not covered in this patch.

---
  xen/common/emul/vuart/vuart.c | 85 +++++++++++++++++++++++++++++++++++
  1 file changed, 85 insertions(+)
xen/common/emul/vuart/vuart.c +0 -0
index bd3b756fb4d6..0df29540b1b6 100644

xen/common/emul/vuart/vuart.c line 12

Quoted 7 lines
xen/common/emul/vuart/vuart.c +2 -0
@@ -6,8 +6,10 @@
 
  #include <asm/types.h>
 
+#include <xen/ctype.h>
  #include <xen/err.h>
  #include <xen/resource.h>
+#include <xen/param.h>

Inclusion of headers should be sorted in alphabetical order so please put xen/param.h before xen/resouce.h.

xen/common/emul/vuart/vuart.c line 24

Quoted 9 lines
xen/common/emul/vuart/vuart.c +2 -0
  #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;

Only a single domain can be given a vUART, and a later vuart= silently replaces an earlier one. If that's the intended scope, the documentation should say so.

Quoted 7 lines
xen/common/emul/vuart/vuart.c +7 -0
+
+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, \

xen/common/emul/vuart/vuart.c line 41

Quoted 10 lines
xen/common/emul/vuart/vuart.c +10 -0
+    .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 */

COM3 and COM4 are at 0x3e8 and 0x2e8 (e.g. Linux's arch/x86/include/asm/serial.h). 0x3fe and 0x2fe fall inside the COM1 and COM2 ranges (0x3fe is COM1's MSR), so a guest wouldn't find com3/com4 where it looks for them, and the emulated range would overlap the neighbouring ports.

Also, this table describes legacy PC COM ports, i.e. it's x86-specific,
but it lives in the arch-independent framework. For the RISC-V port I
had to limit it to CONFIG_X86. Could it move to x86 code, or into ns16x50.c, with the framework only parsing the option?

Quoted 10 lines
xen/common/emul/vuart/vuart.c +10 -0
+#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;

xen/common/emul/vuart/vuart.c line 61

Quoted 10 lines
xen/common/emul/vuart/vuart.c +10 -0
+    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;

The value is truncated to domid_t before the range check, so e.g. vuart=65537 is accepted as domain 1. Also, base 0 makes vuart=010 mean domain 8. I'd parse into an unsigned long with base 10 and check that before assigning:

    unsigned long val = simple_strtoul(s, &end, 10);

    if ( end == s || val >= DOMID_FIRST_RESERVED )
        return -EINVAL;

    domid = val;

xen/common/emul/vuart/vuart.c line 74

Quoted 13 lines
xen/common/emul/vuart/vuart.c +13 -0
+
+    if ( *end == '\0' )
+        goto match_defaults;
+
+    if ( *end != ':' )
+        return -EINVAL;
+
+    compat = end + 1;
+
+    while ( *end && *end != ',' )
+        end++;
+    if ( *end != ',' )
+        return -EINVAL;

[A] So once ":compatible" is given, ",name" is mandatory: vuart=1:ns16550 is rejected. The Kconfig help in 16/26 documents 'vuart=<domid>:<compatible>[,com[1-4]]', i.e. the name as optional;
one of the two needs adjusting.

Also, the comment then should be adjusted too.

Quoted 5 lines
xen/common/emul/vuart/vuart.c +5 -0
+
+    compat_len = end - compat;
+    if ( !compat_len )
+        return -EINVAL;
+

xen/common/emul/vuart/vuart.c line 89

Quoted 10 lines
xen/common/emul/vuart/vuart.c +10 -0
+    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) )

Nit: this matches the compatible exactly, while vuart_match_by_compatible() (01/26) matches a prefix; it would be good to use one rule for both.

~ Oleksii

#31 lore raw

PATCH v8 15/26 emul/ns16x50: enable emulator

In reply to [email protected]

From: Denis Mukhin <[email protected]>

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.

---
 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(+)
xen/arch/x86/hvm/hvm.c +9 -0
index ef9c7cd05777..f4d2fbe75fc6 100644
@@ -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);
xen/common/emul/vuart/vuart.c +8 -0
index 0df29540b1b6..b2680711ad09 100644
@@ -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;
xen/include/xen/vuart.h +6 -0
index b5785b3c6b8f..43c6759c1f97 100644
@@ -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;
Signature · 1 line
2.56.0
#32 lore raw

Re: [PATCH v8 15/26] emul/ns16x50: enable emulator

Quoted 1 line · On 11/11/26 8:47 PM, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Commit subject: "emul/ns16x50: enable emulator"

Nit: the patch doesn't touch ns16x50.c; it wires the framework into x86 HVM domain creation, so "x86/hvm:" (or "emul/vuart:") seems more fitting as the prefix.

Quoted 37 lines

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.

---
  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(+)
xen/arch/x86/hvm/hvm.c +5 -0
index ef9c7cd05777..f4d2fbe75fc6 100644
@@ -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);

xen/arch/x86/hvm/hvm.c int hvm_domain_initialise(struct domain *d, · line 731

Quoted 5 lines
xen/arch/x86/hvm/hvm.c +2 -0
@@ -722,6 +727,8 @@ int hvm_domain_initialise(struct domain *d,
      return 0;
 
   fail2:
+    vuart_deinit(d);
+ out_vioapic_deinit:

Nit: the new label doesn't follow the failN naming used around it.

~ Oleksii

#33 lore raw

PATCH v8 16/26 emul/ns16x50: add Kconfig options

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Add initial Kconfig options configure NS16550-capable emulator.

---
 xen/common/emul/vuart/Kconfig | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
xen/common/emul/vuart/Kconfig +20 -0
index ce1b976b7da7..353a6325180e 100644
@@ -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
Signature · 1 line
2.56.0
#34 lore raw

Re: [PATCH v8 16/26] emul/ns16x50: add Kconfig options

Quoted 3 lines · On 11/11/26 8:47 PM, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Add initial Kconfig options configure NS16550-capable emulator.

Nit: "options to configure the NS16550-compatible emulator".

As said in my reply to the cover letter, 04/26 already makes ns16x50.o depend on CONFIG_VUART_NS16X50, so nothing added by 04..15 is compiled until this patch; that's how the build failure from 13/26 went unnoticed. Introducing VUART_NS16X50 (default n) in 04/26 would avoid that.

Quoted 14 lines
---
  xen/common/emul/vuart/Kconfig | 20 ++++++++++++++++++++
  1 file changed, 20 insertions(+)
xen/common/emul/vuart/Kconfig +0 -0
index ce1b976b7da7..353a6325180e 100644
@@ -3,4 +3,24 @@ config VUART_FRAMEWORK
 
  menu "UART Emulation"
 

xen/common/emul/vuart/Kconfig config VUART_FRAMEWORK · line 15

Quoted 10 lines
xen/common/emul/vuart/Kconfig +10 -0
+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).

The syntax here doesn't match the parser in 14/26, which requires the name once a compatible is given (see my reply there).

xen/common/emul/vuart/Kconfig config VUART_FRAMEWORK · line 21

Quoted 6 lines
xen/common/emul/vuart/Kconfig +6 -0
+
+	  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"

Is it really useful to have "Development: " here?

xen/common/emul/vuart/Kconfig config VUART_FRAMEWORK · line 24

Quoted 3 lines
xen/common/emul/vuart/Kconfig +3 -0
+	depends on VUART_NS16X50 && DEBUG
+	help
+	  Enable development debugging.

Please describe what it does. Besides enabling the emulator's warning/info messages and the body of dump_state(), it prefixes every line of guest output with "FROM GUEST ", i.e. it changes what the guest console looks like. That's surprising for a debug option: anything matching the "(dN) " output, such as test scripts, breaks once it's enabled. Note also that ns16x50_debug() stays silent even with it (see 04/26).

xen/common/emul/vuart/Kconfig config VUART_FRAMEWORK · line 26

Quoted 2 lines
xen/common/emul/vuart/Kconfig +1 -0
+
  endmenu

~ Oleksii

#35 lore raw

PATCH v8 17/26 x86/domain: enable per-domain I/O port bitmaps

In reply to [email protected]

From: Denis Mukhin <[email protected]>

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.

---
 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
xen/arch/x86/Makefile +1 -0
index 2be90ea09880..749b70444bb6 100644
@@ -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
xen/arch/x86/dom0_build.c +3 -108

134 lines not on this page · raw message

xen/arch/x86/hvm/hvm.c +10 -30
index f4d2fbe75fc6..9f0e0f6d8c3d 100644
@@ -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)
xen/arch/x86/hvm/nestedhvm.c +4 -4
index 3205277e815b..5278b7223b5f 100644
@@ -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)
xen/arch/x86/hvm/quirks.c +0 -3
index 9202f5a47fe9..f4d95441fcff 100644
@@ -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);
xen/arch/x86/hvm/svm/nestedsvm.c +1 -1
index d970869cfd96..4fff8e65242e 100644
@@ -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;
xen/arch/x86/hvm/vmx/vvmx.c +2 -2
index 29e48387a110..ef40d28ee874 100644
@@ -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 */
xen/arch/x86/include/asm/hvm/nestedhvm.h +2 -1
index 8a2a0fbb3c38..5452e9e46b76 100644
@@ -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))
xen/arch/x86/include/asm/hvm/support.h +0 -2
index 9e9fa6295567..9aae0f4012a2 100644
@@ -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,
xen/arch/x86/include/asm/iocap.h +3 -0
index f948b7186e95..3328b65345fb 100644
@@ -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)
 {
xen/arch/x86/ioport.c new file +168 -0

168 lines not on this page · raw message

xen/arch/x86/pv/dom0_build.c +4 -0
index 24f2fa0b7f63..385802c65abc 100644
@@ -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);
 
Signature · 1 line
2.56.0
#36 lore raw

PATCH v8 18/26 emul/ns16x50: resolve physical I/O port sharing

In reply to [email protected]

From: Denis Mukhin <[email protected]>

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).

---
 xen/common/emul/vuart/ns16x50.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
xen/common/emul/vuart/ns16x50.c +20 -0
index cda21c15cf78..97e12a99368a 100644
@@ -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)
Signature · 1 line
2.56.0
#37 lore raw

Re: [PATCH v8 18/26] emul/ns16x50: resolve physical I/O port sharing

Quoted 50 lines · On 11/11/26 8:47 PM, [email protected] wrote:

From: Denis Mukhin <[email protected]>

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).

---
  xen/common/emul/vuart/ns16x50.c | 20 ++++++++++++++++++++
  1 file changed, 20 insertions(+)
xen/common/emul/vuart/ns16x50.c +14 -0
index cda21c15cf78..97e12a99368a 100644
@@ -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);

xen/common/emul/vuart/ns16x50.c static int ns16x50_init(void *arg) · line 957

Quoted 10 lines
xen/common/emul/vuart/ns16x50.c +6 -0
 
+    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);

Two things. d isn't declared in ns16x50_deinit() until 21/26, so this patch, 19/26 and 20/26 don't build with CONFIG_VUART_NS16X50=y (see my reply to the cover letter). And the permit is unconditional: for a domain that never had access to the range (any domU), this grants it. As deinit only runs when the domain is going away, I'd drop the permit; otherwise it has to restore the state from before ns16x50_init() rather than assume access was there.

Also, on the error paths of ns16x50_init() that follow (20/26, 21/26), the range stays denied and the portio handler stays registered. That's harmless only because the domain then fails to be created; worth a comment at least.

Another one question I would like to rise is this is x86-specific (I/O ports, ioport_caps, the PVH hwdom I/O bitmap) in otherwise arch-independent code, and it isn't specific to an NS16550 either: any emulated device handed to a domain at a resource the domain could otherwise reach directly needs the same.

I'd do it in the framework, through a per-arch hook called by vuart_init() before emulator->alloc() (together with the IRQ part from 20/26), rather than in ns16x50_init(). That's also where an MMIO-based variant would use iomem_deny_access() instead.

~ Oleksii

#38 lore raw

PATCH v8 19/26 xen/domain: allocate d->irq_caps before arch-specific initialization

In reply to [email protected]

From: Denis Mukhin <[email protected]>

To do that, move per-domain interrupt rangeset allocation before arch-specific code. Add irqs_setup_access() to setup the initial rangeset.

---
 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(-)
xen/arch/x86/dom0_build.c +0 -1
index e8f783d533af..035d2db72788 100644
@@ -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 )
xen/arch/x86/hvm/dom0_build.c +7 -0
index f536f5dcc444..5708caf752cb 100644
@@ -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.
xen/arch/x86/include/asm/irq.h +2 -0
index 7315150b66b4..d26cb5c1daba 100644
@@ -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 */
xen/arch/x86/irq.c +8 -0
index 32a75f77a46d..3fcff671c27f 100644
@@ -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;
+}
xen/arch/x86/pv/dom0_build.c +3 -0
index 385802c65abc..50c311f0a490 100644
@@ -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);
 
xen/common/domain.c +6 -2
index f3a2634d5f98..18c5674f6439 100644
@@ -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 )
Signature · 1 line
2.56.0
#39 lore raw

PATCH v8 20/26 emul/ns16x50: resolve physical IRQ sharing

In reply to [email protected]

From: Denis Mukhin <[email protected]>

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).

---
 xen/common/emul/vuart/ns16x50.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
xen/common/emul/vuart/ns16x50.c +11 -0
index 97e12a99368a..389c58f94198 100644
@@ -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);
Signature · 1 line
2.56.0
#40 lore raw

PATCH v8 21/26 emul/ns16x50: implement IRQ emulation via vIOAPIC

In reply to [email protected]

From: Denis Mukhin <[email protected]>

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.

---
 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(-)
xen/arch/x86/hvm/vioapic.c +10 -0
index 995fae7b3c0f..03a63c048ea6 100644
@@ -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 )
xen/arch/x86/include/asm/irq.h +6 -0
index d26cb5c1daba..58f32026e033 100644
@@ -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);
 
xen/arch/x86/irq.c +1 -1
index 3fcff671c27f..a1f0f3e05dac 100644
@@ -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 )
xen/common/emul/vuart/ns16x50.c +47 -0

77 lines not on this page · raw message

xen/drivers/passthrough/x86/hvm.c +10 -1
index 9f8e6c8cd50f..e108f2c69a33 100644
@@ -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) )
Signature · 1 line
2.56.0
#41 lore raw

PATCH v8 22/26 MAINTAINERS: add myself as vUART framework maintainer

In reply to [email protected]

From: Denis Mukhin <[email protected]>

---
 MAINTAINERS | 8 ++++++++
 1 file changed, 8 insertions(+)
MAINTAINERS +8 -0
index b804434d52d6..327adf4641a1 100644
@@ -615,6 +615,14 @@ F:	stubdom/vtpm/
 F:	stubdom/vtpmmgr/
 F:	docs/misc/vtpm-platforms.txt
 
+VUART
+M:	Denis Mukhin <[email protected]>
+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 <[email protected]>
 M:	Andrew Cooper <[email protected]>
Signature · 1 line
2.56.0
#42 lore raw

Re: [PATCH v8 22/26] MAINTAINERS: add myself as vUART framework maintainer

Quoted 12 lines · On 11/11/26 8:47 PM, [email protected] wrote:

From: Denis Mukhin <[email protected]>

---
  MAINTAINERS | 8 ++++++++
  1 file changed, 8 insertions(+)
MAINTAINERS +0 -0
index b804434d52d6..327adf4641a1 100644

MAINTAINERS F: stubdom/vtpm/ · line 622

Quoted 8 lines
MAINTAINERS +5 -0
@@ -615,6 +615,14 @@ F:	stubdom/vtpm/
  F:	stubdom/vtpmmgr/
  F:	docs/misc/vtpm-platforms.txt
 
+VUART
+M:	Denis Mukhin <[email protected]>
+S:	Maintained
+F:	common/emul/vuart/ns16x50.c
+F:	common/emul/vuart/vuart.c

These two lack the xen/ prefix, unlike every other F: entry (including xen/include/xen/vuart.h below), so they don't match anything and get_maintainer.pl won't pick them up. A directory entry would also cover the Kconfig/Makefile and any future emulator:

F: xen/common/emul/vuart/

MAINTAINERS F: stubdom/vtpm/ · line 623

Quoted 1 line
MAINTAINERS +1 -0
+F:	docs/misc/console.txt

That file is the general PV console document (xenstore layout, xenconsoled, vpl011), not specific to the vUART framework, and the series doesn't touch it. Unless the plan is to document vUART there, I'd leave it out or a separate doc file will be needed to be introduced.

~ Oleksii

#43 lore raw

PATCH v8 23/26 docs/fusa: add NS16x50 UART requirements for x86

In reply to [email protected]

From: Ayan Kumar Halder <[email protected]>

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.

---
 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
MAINTAINERS +7 -0
index 327adf4641a1..2c0eaaea2d24 100644
@@ -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 <[email protected]>
+R:	Ayan Kumar Halder <[email protected]>
+S:	Supported
+F:	docs/fusa/reqs/design-reqs/x86
+F:	docs/fusa/reqs/product-reqs/x86
+
 FUSA
 M:	Stefano Stabellini <[email protected]>
 M:	Bertrand Marquis <[email protected]>
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +386 -0

386 lines not on this page · raw message

docs/fusa/reqs/index.rst +2 -0
index de19b0cda252..6c1249c9f18c 100644
@@ -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
docs/fusa/reqs/product-reqs/x86/reqs.rst new file +24 -0
new file mode 100644
index 000000000000..ab517c02471f
@@ -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
Signature · 1 line
2.56.0
Reviewed-by Ayan Kumar Halder on v8 23/26
#44 lore raw

Re: [PATCH v8 23/26] docs/fusa: add NS16x50 UART requirements for x86

Hi Denis,

The requirements look good. And it is great that you have written the tests as well.

So from a FuSa perspective, these requirements are 'covered'.

I want to confirm the tests for each of the requirements.

Refer "[XTF PATCH v1 6/6] x86: add NS16550-compatible emulator test"

Quoted 58 lines · On 11/11/2026 19:47, [email protected] wrote:

From: Ayan Kumar Halder<[email protected]>

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.

---
  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

MAINTAINERS +7 -0
index 327adf4641a1..2c0eaaea2d24 100644
@@ -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<[email protected]>
+R:	Ayan Kumar Halder<[email protected]>
+S:	Supported
+F:	docs/fusa/reqs/design-reqs/x86
+F:	docs/fusa/reqs/product-reqs/x86
+
  FUSA
  M:	Stefano Stabellini<[email protected]>
  M:	Bertrand Marquis<[email protected]>
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +5 -0
new file mode 100644
index 000000000000..e3995dc9defc
@@ -0,0 +1,386 @@
+.. SPDX-License-Identifier: CC-BY-4.0
+
+NS16550-compatible UART Emulation
+=================================
+

docs/fusa/reqs/design-reqs/x86/ns16x50.rst line 15

Quoted 10 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +10 -0
+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.

Is this tested by test_scr() ? And ..

Quoted 5 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +5 -0
+
+Rationale:
+
+Comments:
+

docs/fusa/reqs/design-reqs/x86/ns16x50.rst line 30

Quoted 10 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +10 -0
+Covers:
+ - `XenProd~emul_ns16x50~1`
+
+Access Transmit Holding Register
+--------------------------------
+
+`XenSwdgn~emul_ns16x50_thr~1`
+
+Description:
+Xen shall emulate a transmit holding register.

test_thr()

Quoted 5 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +5 -0
+
+Rationale:
+
+Comments:
+

docs/fusa/reqs/design-reqs/x86/ns16x50.rst line 45

Quoted 10 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +10 -0
+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.

test_lcr()

Quoted 5 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +5 -0
+
+Rationale:
+
+Comments:
+

docs/fusa/reqs/design-reqs/x86/ns16x50.rst line 60

Quoted 10 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +10 -0
+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.

test_ier()

Quoted 7 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +7 -0
+
+Rationale:
+
+Comments:
+
+Covers:
+ - `XenProd~emul_ns16x50~1`

docs/fusa/reqs/design-reqs/x86/ns16x50.rst line 77

Quoted 10 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +10 -0
+
+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.

test_divisor_latch()

Quoted 5 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +5 -0
+
+Rationale:
+
+Comments:
+

docs/fusa/reqs/design-reqs/x86/ns16x50.rst line 92

Quoted 10 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +10 -0
+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.

test_mcr()

Quoted 6 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +6 -0
+
+Rationale:
+
+Comments:
+
+Covers:

docs/fusa/reqs/design-reqs/x86/ns16x50.rst line 108

Quoted 10 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +10 -0
+ - `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.

test_fcr_toggle()

Quoted 6 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +6 -0
+
+Rationale:
+
+Comments:
+
+Covers:

docs/fusa/reqs/design-reqs/x86/ns16x50.rst line 124

Quoted 10 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +10 -0
+ - `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.

test_fcr_trigger()

Quoted 6 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +6 -0
+
+Rationale:
+
+Comments:
+
+Covers:

docs/fusa/reqs/design-reqs/x86/ns16x50.rst line 140

Quoted 10 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +10 -0
+ - `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.

test_fcr_clear()

Quoted 7 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +7 -0
+
+Rationale:
+
+Comments:
+
+Covers:
+ - `XenProd~emul_ns16x50~1`

docs/fusa/reqs/design-reqs/x86/ns16x50.rst line 157

Quoted 10 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +10 -0
+
+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.

test_loopback_data_path()

Quoted 6 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +6 -0
+
+Rationale:
+
+Comments:
+
+Covers:

docs/fusa/reqs/design-reqs/x86/ns16x50.rst line 173

Quoted 10 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +10 -0
+ - `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.

test_loopback_modem_path()

Quoted 6 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +6 -0
+
+Rationale:
+
+Comments:
+
+Covers:

docs/fusa/reqs/design-reqs/x86/ns16x50.rst line 189

Quoted 10 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +10 -0
+ - `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.

test_iir_noint()

Quoted 7 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +7 -0
+
+Rationale:
+
+Comments:
+
+Covers:
+ - `XenProd~emul_ns16x50~1`

docs/fusa/reqs/design-reqs/x86/ns16x50.rst line 206

Quoted 10 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +10 -0
+
+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.

test_iir_thr_source()

Quoted 7 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +7 -0
+
+Rationale:
+
+Comments:
+
+Covers:
+ - `XenProd~emul_ns16x50~1`

docs/fusa/reqs/design-reqs/x86/ns16x50.rst line 223

Quoted 10 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +10 -0
+
+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.

test_iir_rda_source()

Quoted 7 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +7 -0
+
+Rationale:
+
+Comments:
+
+Covers:
+ - `XenProd~emul_ns16x50~1`

docs/fusa/reqs/design-reqs/x86/ns16x50.rst line 240

Quoted 10 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +10 -0
+
+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.

test_iir_priority()

Quoted 183 lines
docs/fusa/reqs/design-reqs/x86/ns16x50.rst new file +146 -0
+
+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`
docs/fusa/reqs/index.rst +2 -0
index de19b0cda252..6c1249c9f18c 100644
@@ -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
docs/fusa/reqs/product-reqs/x86/reqs.rst new file +14 -0
new file mode 100644
index 000000000000..ab517c02471f
@@ -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:

docs/fusa/reqs/product-reqs/x86/reqs.rst line 24

Quoted 10 lines
docs/fusa/reqs/product-reqs/x86/reqs.rst new file +10 -0
+
+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

LGTM

- Ayan

#45 lore raw

Re: [PATCH v8 23/26] docs/fusa: add NS16x50 UART requirements for x86

Quoted 46 lines · Ayan Kumar

Hi Denis,

The requirements look good. And it is great that you have written the tests as well.

So from a FuSa perspective, these requirements are 'covered'.

I want to confirm the tests for each of the requirements.

Refer "[XTF PATCH v1 6/6] x86: add NS16550-compatible emulator test"

On 11/11/2026 19:47, [email protected] wrote:

From: Ayan Kumar Halder <[email protected]>

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.

---
 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
MAINTAINERS +0 -0
index 327adf4641a1..2c0eaaea2d24 100644

MAINTAINERS F: xen/arch/x86/include/asm/x86_*/efi*.h · line 343

Quoted 9 lines
MAINTAINERS +6 -0
@@ -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 <[email protected]>
+R:	Ayan Kumar Halder <[email protected]>
+S:	Supported
+F:	docs/fusa/reqs/design-reqs/x86
+F:	docs/fusa/reqs/product-reqs/x86

Hi Ayan, Denis,

The MAINTAINERS file is not hierarchical, so with this change the x86 directories would no longer be covered by the maintainers listed under FUSA.

Could we have at least two maintainers listed? Ayan is already a reviewer; if he agrees, we could make him a maintainer so that we have two.

The rest of the patch appears to be acked, so I can make this change on commit if we all agree.

Quoted 3 lines
MAINTAINERS +0 -0
 FUSA
 M:	Stefano Stabellini <[email protected]>
 M:	Bertrand Marquis <[email protected]>
#46 lore raw

Re: [PATCH v8 23/26] docs/fusa: add NS16x50 UART requirements for x86

Hi Stefano,

Quoted 62 lines · Stefano Stabellini
Quoted 46 lines · Ayan Kumar

Hi Denis,

The requirements look good. And it is great that you have written the tests as well.

So from a FuSa perspective, these requirements are 'covered'.

I want to confirm the tests for each of the requirements.

Refer "[XTF PATCH v1 6/6] x86: add NS16550-compatible emulator test"

On 11/11/2026 19:47, [email protected] wrote:

From: Ayan Kumar Halder <[email protected]>

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.

---
  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

MAINTAINERS +0 -0
index 327adf4641a1..2c0eaaea2d24 100644

MAINTAINERS F: xen/arch/x86/include/asm/x86_*/efi*.h · line 343

Quoted 9 lines
MAINTAINERS +6 -0
@@ -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 <[email protected]>
+R:	Ayan Kumar Halder <[email protected]>
+S:	Supported
+F:	docs/fusa/reqs/design-reqs/x86
+F:	docs/fusa/reqs/product-reqs/x86

Hi Ayan, Denis,

The MAINTAINERS file is not hierarchical, so with this change the x86 directories would no longer be covered by the maintainers listed under FUSA.

Quoted 6 lines

Could we have at least two maintainers listed? Ayan is already a reviewer; if he agrees, we could make him a maintainer so that we have two.

The rest of the patch appears to be acked, so I can make this change on commit if we all agree.

I am ok with this.

- Ayan

#47 lore raw

Re: [PATCH v8 23/26] docs/fusa: add NS16x50 UART requirements for x86

In reply to [email protected]
Quoted 1 line · On 11/11/26 8:47 PM, [email protected] wrote:

From: Ayan Kumar Halder <[email protected]>

[...]

Quoted 5 lines
MAINTAINERS +0 -0
index 327adf4641a1..2c0eaaea2d24 100644

MAINTAINERS F: xen/arch/x86/include/asm/x86_*/efi*.h · line 344

Quoted 10 lines
MAINTAINERS +7 -0
@@ -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 <[email protected]>
+R:	Ayan Kumar Halder <[email protected]>
+S:	Supported
+F:	docs/fusa/reqs/design-reqs/x86
+F:	docs/fusa/reqs/product-reqs/x86
+

Not something to do for this patch but I would like to ask (mostly maintainers) won't be better then also update ...

MAINTAINERS F: xen/arch/x86/include/asm/x86_*/efi*.h · line 345

Quoted 1 line
MAINTAINERS +0 -0
  FUSA

... this name. It is a little bit unclear what is the difference between FUSA vs FUSA (x86).

MAINTAINERS F: xen/arch/x86/include/asm/x86_*/efi*.h · line 347

Quoted 2 lines
MAINTAINERS +0 -0
  M:	Stefano Stabellini <[email protected]>
  M:	Bertrand Marquis <[email protected]>

~ Oleksii

#48 lore raw

Re: [PATCH v8 23/26] docs/fusa: add NS16x50 UART requirements for x86

Hi Oleksii,

Quoted 24 lines · Oleksii Kurochko
Quoted 1 line · On 11/11/26 8:47 PM, [email protected] wrote:

From: Ayan Kumar Halder <[email protected]>

[...]

Quoted 8 lines
MAINTAINERS +0 -0
index 327adf4641a1..2c0eaaea2d24 100644
@@ -335,6 +335,13 @@ F:    xen/arch/x86/include/asm/x86_*/efi*.h

  F:    xen/common/efi/
  F:    xen/include/efi/
  +FUSA (x86)

MAINTAINERS F: xen/arch/x86/include/asm/x86_*/efi*.h · line 344

Quoted 6 lines
MAINTAINERS +6 -0
+M:    Denis Mukhin <[email protected]>
+R:    Ayan Kumar Halder <[email protected]>
+S:    Supported
+F:    docs/fusa/reqs/design-reqs/x86
+F:    docs/fusa/reqs/product-reqs/x86
+

Not something to do for this patch but I would like to ask (mostly maintainers) won't be better then also update ...

Quoted 3 lines
Quoted 1 line

  FUSA

... this name. It is a little bit unclear what is the difference between FUSA vs FUSA (x86).

FUSA (x86) - is a more granular maintainership under docs/fusa. It will cover only the x86 specific bits.

We want to have more granular maintainership so that the person knows which file/s to maintain.

FUSA - covers the rest under docs/fusa

- Ayan

#49 lore raw

PATCH v8 24/26 ci: add initial NS16550-compatible XTF test

In reply to [email protected]

From: Denis Mukhin <[email protected]>

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.

---
 automation/gitlab-ci/build.yaml       |  2 ++
 automation/gitlab-ci/test.yaml        | 10 ++++++++++
 automation/scripts/include/xtf-runner |  2 ++
 3 files changed, 14 insertions(+)
automation/gitlab-ci/build.yaml +2 -0
index 3d37e0b572d0..f2072f6a9829 100644
@@ -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
automation/gitlab-ci/test.yaml +10 -0
index 96127995d7be..4f4ce8aab9e9 100644
@@ -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:
automation/scripts/include/xtf-runner +2 -0
index d1ef888465c3..41deb49d1c5d 100644
@@ -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}")
Signature · 1 line
2.56.0
#50 lore raw

Re: [PATCH v8 24/26] ci: add initial NS16550-compatible XTF test

Quoted 5 lines · On 11/11/26 8:47 PM, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Enable simple functional test for NS16550-compatible emulator by adding
CONFIG_VUART_NS16X50=y
CONFIG_SHADOW_PAGING=y

It would help to say what the test checks, and why SHADOW_PAGING is
needed. If it is something obvious and there is no sense to put this infromation in commit message then could you please just for me explain why SHADOW_PAGING should be enabled?

Quoted 27 lines

for alpine-3.24-x86_64-gcc-debug Xen build.

---
  automation/gitlab-ci/build.yaml       |  2 ++
  automation/gitlab-ci/test.yaml        | 10 ++++++++++
  automation/scripts/include/xtf-runner |  2 ++
  3 files changed, 14 insertions(+)
automation/gitlab-ci/build.yaml +2 -0
index 3d37e0b572d0..f2072f6a9829 100644
@@ -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
automation/gitlab-ci/test.yaml +0 -0
index 96127995d7be..4f4ce8aab9e9 100644

automation/gitlab-ci/test.yaml qemu-xtf-argo-x86_64-gcc-debug: · line 794

Quoted 10 lines
automation/gitlab-ci/test.yaml +7 -0
@@ -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}

As far as I can see, emul-ns16x50 isn't in upstream XTF. With the
default XTF_SRC_URI/XTF_SRC_BRANCH (xenbits master) this job would fail
to find the test; only 26/26, marked "do not merge", points CI at your
XTF branch. Has the XTF test been posted? IIRC, it is posted. I think this patch has to wait until it's in XTF, and the cover letter should say so.

Quoted 11 lines
automation/gitlab-ci/test.yaml +3 -0
+  needs:
+    - alpine-3.24-x86_64-gcc-debug
+
  qemu-smoke-riscv64-gcc:
    extends: .qemu-riscv64
    script:
automation/scripts/include/xtf-runner +0 -0
index d1ef888465c3..41deb49d1c5d 100644

automation/scripts/include/xtf-runner function xtf_build_cmdline() · line 74

Quoted 5 lines
automation/scripts/include/xtf-runner +2 -0
@@ -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"

Could the comment say why COM1 isn't used? Xen's own console is on QEMU's COM1 (console=com1 in XEN_CMDLINE), but since the test domain's accesses to an emulated port are intercepted anyway, it's not obvious to me that an emulated COM1 wouldn't work as well.

Also, no CI build enables CONFIG_VUART_NS16X50_DEBUG, which is how the build failure in 13/26 went unnoticed. Adding it to one of the debug builds would cover that code.

~ Oleksii

#51 lore raw

PATCH v8 25/26 ci: add smoke NS16550-compatible PVH Linux test

In reply to [email protected]

From: Denis Mukhin <[email protected]>

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.

---
 automation/gitlab-ci/test.yaml           |  27 +-
 automation/scripts/qemu-alpine-x86_64.sh | 325 ++++++++++++++++++-----
 2 files changed, 280 insertions(+), 72 deletions(-)
automation/gitlab-ci/test.yaml +26 -1
index 4f4ce8aab9e9..c4140e850b5f 100644
@@ -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
automation/scripts/qemu-alpine-x86_64.sh +254 -71

340 lines not on this page · raw message

Signature · 1 line
2.56.0
#52 lore raw

PATCH v8 26/26 ci: do not merge: test

In reply to [email protected]

From: Denis Mukhin <[email protected]>

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.

---
 automation/scripts/include/xtf-x86_64 | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
automation/scripts/include/xtf-x86_64 +2 -2
index 186f074bd8eb..e1e839d3c925 100644
@@ -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"
 }
 
Signature · 1 line
2.56.0
#53 lore raw
In reply to [email protected]

Hello Denis,

Quoted 64 lines · On 11/11/26 8:47 PM, [email protected] 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/[email protected]/

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).

Quoted 9 lines

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