All of lore.kernel.org
 help / color / mirror / Atom feed
* [XTF PATCH v1 0/6] x86: add NS16550-compatible UART emulator test
@ 2026-11-11 19:51 dmukhin
  2026-11-11 19:51 ` [XTF PATCH v1 1/6] .gitignore: exclude tags dmukhin
                   ` (5 more replies)
  0 siblings, 6 replies; 13+ messages in thread
From: dmukhin @ 2026-11-11 19:51 UTC (permalink / raw)
  To: xen-devel
  Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
	roger, sstabellini, dmukhin, Denis Mukhin

Series introduces a test for NS16550-compatible UART.

Patches 1-3 are preliminary cleanups and fixups.

Patch 4 allows emulator to be the only registered console in
case console hypercall is not available.

Patch 5 adds minimally functional NS16550-compatible UART
driver.

Patch 6 adds a new test.

CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/3055921295
Link to XTF branch: https://gitlab.com/xen-project/people/dmukhin/xtf/-/tree/emul-16x50?ref_type=heads
Link to emualator: https://gitlab.com/xen-project/people/dmukhin/xen/-/commits/vuart-ns8250-v8

Denis Mukhin (6):
  .gitignore: exclude tags
  x86: introduce cpu_relax()
  x86: isolate FEP in setup_fep()
  x86: skip registering unavailable Xen console
  common: add simplistic NS16550-compatible UART driver
  x86: add NS16550-compatible emulator test

 .gitignore                        |   1 +
 arch/x86/include/arch/processor.h |   3 +
 arch/x86/setup.c                  |  87 ++--
 build/files.mk                    |   1 +
 common/console.c                  |   3 +-
 common/ns16550.c                  | 153 +++++++
 include/xtf/console.h             |  15 +-
 include/xtf/hypercall.h           |   4 +-
 include/xtf/ns16550.h             | 117 ++++++
 tests/emul-ns16x50/.gitignore     |   3 +
 tests/emul-ns16x50/Makefile       |   9 +
 tests/emul-ns16x50/main.c         | 644 ++++++++++++++++++++++++++++++
 tests/emul-ns16x50/patterns.c     |  65 +++
 13 files changed, 1056 insertions(+), 49 deletions(-)
 create mode 100644 common/ns16550.c
 create mode 100644 include/xtf/ns16550.h
 create mode 100644 tests/emul-ns16x50/.gitignore
 create mode 100644 tests/emul-ns16x50/Makefile
 create mode 100644 tests/emul-ns16x50/main.c
 create mode 100644 tests/emul-ns16x50/patterns.c

-- 
2.56.0



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

* [XTF PATCH v1 1/6] .gitignore: exclude tags
  2026-11-11 19:51 [XTF PATCH v1 0/6] x86: add NS16550-compatible UART emulator test dmukhin
@ 2026-11-11 19:51 ` dmukhin
  2026-12-03  0:12   ` Stefano Stabellini
  2026-11-11 19:51 ` [XTF PATCH v1 2/6] x86: introduce cpu_relax() dmukhin
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 13+ messages in thread
From: dmukhin @ 2026-11-11 19:51 UTC (permalink / raw)
  To: xen-devel
  Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
	roger, sstabellini, dmukhin, Denis Mukhin

From: Denis Mukhin <dmukhin@ford.com>

Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
 .gitignore | 1 +
 1 file changed, 1 insertion(+)

diff --git a/.gitignore b/.gitignore
index dcacdf3b2d6c..b16635db3409 100644
--- a/.gitignore
+++ b/.gitignore
@@ -15,4 +15,5 @@
 /GRTAGS
 /GTAGS
 Makefile.local
+tags
 .vimrc
-- 
2.56.0



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

* [XTF PATCH v1 2/6] x86: introduce cpu_relax()
  2026-11-11 19:51 [XTF PATCH v1 0/6] x86: add NS16550-compatible UART emulator test dmukhin
  2026-11-11 19:51 ` [XTF PATCH v1 1/6] .gitignore: exclude tags dmukhin
@ 2026-11-11 19:51 ` dmukhin
  2026-12-03  0:13   ` Stefano Stabellini
  2026-11-11 19:51 ` [XTF PATCH v1 3/6] x86: isolate FEP in setup_fep() dmukhin
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 13+ messages in thread
From: dmukhin @ 2026-11-11 19:51 UTC (permalink / raw)
  To: xen-devel
  Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
	roger, sstabellini, dmukhin, Denis Mukhin

From: Denis Mukhin <dmukhin@ford.com>

Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
 arch/x86/include/arch/processor.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/arch/x86/include/arch/processor.h b/arch/x86/include/arch/processor.h
index 9cf2fe08669f..9db352f18c7c 100644
--- a/arch/x86/include/arch/processor.h
+++ b/arch/x86/include/arch/processor.h
@@ -190,6 +190,9 @@
 #define X86_SEL_GDT       0
 #define X86_SEL_LDT       X86_SEL_TI
 
+/* Pause for busy-wait loops. */
+#define cpu_relax()       asm volatile ( "pause" ::: "memory" )
+
 #endif /* XTF_X86_PROCESSOR_H */
 
 /*
-- 
2.56.0



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

* [XTF PATCH v1 3/6] x86: isolate FEP in setup_fep()
  2026-11-11 19:51 [XTF PATCH v1 0/6] x86: add NS16550-compatible UART emulator test dmukhin
  2026-11-11 19:51 ` [XTF PATCH v1 1/6] .gitignore: exclude tags dmukhin
  2026-11-11 19:51 ` [XTF PATCH v1 2/6] x86: introduce cpu_relax() dmukhin
@ 2026-11-11 19:51 ` dmukhin
  2026-12-03  0:21   ` Stefano Stabellini
  2026-11-11 19:51 ` [XTF PATCH v1 4/6] x86: skip registering unavailable Xen console dmukhin
                   ` (2 subsequent siblings)
  5 siblings, 1 reply; 13+ messages in thread
From: dmukhin @ 2026-11-11 19:51 UTC (permalink / raw)
  To: xen-devel
  Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
	roger, sstabellini, dmukhin, Denis Mukhin

From: Denis Mukhin <dmukhin@ford.com>

Keep weak symbol test_setup() for per-test definition.

Isolate FEP into a new setup_fep() call which is executed on a common x86
path.

Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
 arch/x86/setup.c | 80 +++++++++++++++++++++++++-----------------------
 1 file changed, 41 insertions(+), 39 deletions(-)

diff --git a/arch/x86/setup.c b/arch/x86/setup.c
index ba6f9c320843..8fea6495de1c 100644
--- a/arch/x86/setup.c
+++ b/arch/x86/setup.c
@@ -253,6 +253,45 @@ static void xen_console_write(const char *buf, size_t len)
     hypercall_console_write(buf, len);
 }
 
+/*
+ * Common test setup:
+ *
+ * xtf_has_* indicates the availabiliy of options which require runtime
+ * detection.
+ */
+bool xtf_has_fep = false;
+
+/*
+ * Default weak settings.
+ *
+ * test_wants_* indicates default settings for the framework, which may be
+ * overridden by individual tests by providing non-weak variables.
+ *
+ * test_needs_* indicates options required by the test, without which the test
+ * isn't worth running.
+ */
+bool __weak test_needs_fep = false;
+
+static void setup_fep(void)
+{
+    /*
+     * Attempt to use the forced emulation prefix to set the value of
+     * xtf_has_fep to the value of 1.  Use the exception table to compensate
+     * for the #UD exception if FEP is not available.
+     */
+    asm volatile ("1:" _ASM_XEN_FEP "mov $1, %[fep];"
+                  "2:"
+                  _ASM_EXTABLE(1b, 2b)
+                  : [fep] "=q" (xtf_has_fep)
+                  : "0" (false));
+
+    if ( test_needs_fep && !xtf_has_fep )
+    {
+        printk("FEP unavailable, but needed by test. (Is Xen support\n");
+        return xtf_skip("compiled in, and booted with 'hvm_fep'?)\n");
+    }
+}
+
 void arch_setup(void)
 {
     if ( IS_DEFINED(CONFIG_HVM) && !pvh_start_info )
@@ -275,6 +314,8 @@ void arch_setup(void)
     }
 
     map_shared_info();
+
+    setup_fep();
 }
 
 int arch_get_domid(void)
@@ -293,45 +334,6 @@ int arch_get_domid(void)
     return -1;
 }
 
-/*
- * Common test setup:
- *
- * xtf_has_* indicates the availabiliy of options which require runtime
- * detection.
- */
-bool xtf_has_fep = false;
-
-/*
- * Default weak settings.
- *
- * test_wants_* indicates default settings for the framework, which may be
- * overridden by individual tests by providing non-weak variables.
- *
- * test_needs_* indicates options required by the test, without which the test
- * isn't worth running.
- */
-bool __weak test_needs_fep = false;
-
-void test_setup(void)
-{
-    /*
-     * Attempt to use the forced emulation prefix to set the value of
-     * xtf_has_fep to the value of 1.  Use the exception table to compensate
-     * for the #UD exception if FEP is not available.
-     */
-    asm volatile ("1:" _ASM_XEN_FEP "mov $1, %[fep];"
-                  "2:"
-                  _ASM_EXTABLE(1b, 2b)
-                  : [fep] "=q" (xtf_has_fep)
-                  : "0" (false));
-
-    if ( test_needs_fep && !xtf_has_fep )
-    {
-        printk("FEP unavailable, but needed by test. (Is Xen support\n");
-        return xtf_skip("compiled in, and booted with 'hvm_fep'?)\n");
-    }
-}
-
 /*
  * Local variables:
  * mode: C
-- 
2.56.0



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

* [XTF PATCH v1 4/6] x86: skip registering unavailable Xen console
  2026-11-11 19:51 [XTF PATCH v1 0/6] x86: add NS16550-compatible UART emulator test dmukhin
                   ` (2 preceding siblings ...)
  2026-11-11 19:51 ` [XTF PATCH v1 3/6] x86: isolate FEP in setup_fep() dmukhin
@ 2026-11-11 19:51 ` dmukhin
  2026-12-03  0:14   ` Stefano Stabellini
  2026-11-11 19:51 ` [XTF PATCH v1 5/6] common: add simplistic NS16550-compatible UART driver dmukhin
  2026-11-11 19:51 ` [XTF PATCH v1 6/6] x86: add NS16550-compatible emulator test dmukhin
  5 siblings, 1 reply; 13+ messages in thread
From: dmukhin @ 2026-11-11 19:51 UTC (permalink / raw)
  To: xen-devel
  Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
	roger, sstabellini, dmukhin, Denis Mukhin

From: Denis Mukhin <dmukhin@ford.com>

Do not register Xen console for the test in case HYPERCALL_console_io
is not available.

Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
 arch/x86/setup.c        | 5 +++--
 include/xtf/hypercall.h | 4 ++--
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/arch/x86/setup.c b/arch/x86/setup.c
index 8fea6495de1c..725abe289a05 100644
--- a/arch/x86/setup.c
+++ b/arch/x86/setup.c
@@ -250,7 +250,7 @@ static void qemu_console_write(const char *buf, size_t len)
 
 static void xen_console_write(const char *buf, size_t len)
 {
-    hypercall_console_write(buf, len);
+    (void)hypercall_console_write(buf, len);
 }
 
 /*
@@ -297,7 +297,8 @@ void arch_setup(void)
     if ( IS_DEFINED(CONFIG_HVM) && !pvh_start_info )
         register_console_callback(qemu_console_write);
 
-    register_console_callback(xen_console_write);
+    if ( !hypercall_console_write("\0", 1) )
+        register_console_callback(xen_console_write);
 
     collect_cpuid(IS_DEFINED(CONFIG_PV) ? pv_cpuid_count : cpuid_count);
 
diff --git a/include/xtf/hypercall.h b/include/xtf/hypercall.h
index 68b3da111d85..475eb771d472 100644
--- a/include/xtf/hypercall.h
+++ b/include/xtf/hypercall.h
@@ -194,9 +194,9 @@ static inline long hypercall_argo_op(unsigned int cmd, void *arg1, void *arg2,
 /*
  * Higher level hypercall helpers
  */
-static inline void hypercall_console_write(const char *buf, unsigned long count)
+static inline long hypercall_console_write(const char *buf, unsigned long count)
 {
-    (void)HYPERCALL3(long, __HYPERVISOR_console_io, CONSOLEIO_write, count, buf);
+    return HYPERCALL3(long, __HYPERVISOR_console_io, CONSOLEIO_write, count, buf);
 }
 
 static inline long hypercall_shutdown(unsigned int reason)
-- 
2.56.0



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

* [XTF PATCH v1 5/6] common: add simplistic NS16550-compatible UART driver
  2026-11-11 19:51 [XTF PATCH v1 0/6] x86: add NS16550-compatible UART emulator test dmukhin
                   ` (3 preceding siblings ...)
  2026-11-11 19:51 ` [XTF PATCH v1 4/6] x86: skip registering unavailable Xen console dmukhin
@ 2026-11-11 19:51 ` dmukhin
  2026-12-03  0:25   ` Stefano Stabellini
  2026-11-11 19:51 ` [XTF PATCH v1 6/6] x86: add NS16550-compatible emulator test dmukhin
  5 siblings, 1 reply; 13+ messages in thread
From: dmukhin @ 2026-11-11 19:51 UTC (permalink / raw)
  To: xen-devel
  Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
	roger, sstabellini, dmukhin, Denis Mukhin

From: Denis Mukhin <dmukhin@ford.com>

Add a miniature NS16550-compatible driver for hypervisor validation.

Implement polling mode and putchar only.

Place the code in a common place since NS16550 hardware is not limited to
x86 systems (although the current driver uses few x86-ishms like I/O ports).

Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
 build/files.mk        |   1 +
 common/console.c      |   3 +-
 common/ns16550.c      | 153 ++++++++++++++++++++++++++++++++++++++++++
 include/xtf/console.h |  15 +++--
 include/xtf/ns16550.h | 117 ++++++++++++++++++++++++++++++++
 5 files changed, 284 insertions(+), 5 deletions(-)
 create mode 100644 common/ns16550.c
 create mode 100644 include/xtf/ns16550.h

diff --git a/build/files.mk b/build/files.mk
index 11624a89a21c..841ea9ebc70a 100644
--- a/build/files.mk
+++ b/build/files.mk
@@ -34,6 +34,7 @@ obj-hvm += $(ROOT)/arch/x86/hvm/head.o
 obj-hvm += $(ROOT)/arch/x86/hvm/pagetables.o
 obj-hvm += $(ROOT)/arch/x86/hvm/traps.o
 obj-hvm += $(ROOT)/arch/x86/io-apic.o
+obj-hvm += $(ROOT)/common/ns16550.o
 
 # Arguably common objects, but PV guests will have no interest in them.
 obj-hvm += $(ROOT)/arch/x86/vmx.o
diff --git a/common/console.c b/common/console.c
index b3e89473a7b4..02ff90c5428d 100644
--- a/common/console.c
+++ b/common/console.c
@@ -13,8 +13,9 @@
  * - Xen hypervisor console
  * - PV console
  * - Qemu debug console
+ * - NS16550-compatible emulator
  */
-static cons_output_cb output_fns[3];
+static cons_output_cb output_fns[4];
 static unsigned int nr_cons_cb;
 
 /* Guest PV console details. */
diff --git a/common/ns16550.c b/common/ns16550.c
new file mode 100644
index 000000000000..fa2bae5ebf98
--- /dev/null
+++ b/common/ns16550.c
@@ -0,0 +1,153 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/**
+ * Simple NS16550-compatible device driver.
+ */
+
+#include <arch/config.h>
+#include <arch/traps.h>
+
+#include <xtf/console.h>
+#include <xtf/ns16550.h>
+#include <xtf/types.h>
+
+static const struct ns16550 *ns16550;
+
+static inline unsigned long
+ns16550_reg(const struct ns16550 *uart, unsigned int reg)
+{
+    return uart->base + (reg << uart->reg_shift);
+}
+
+uint8_t ns16550_inb(const struct ns16550 *uart, unsigned int reg)
+{
+    unsigned long addr = ns16550_reg(uart, reg);
+
+    return inb(addr);
+}
+
+void ns16550_outb(const struct ns16550 *uart, unsigned int reg,
+                  unsigned char val)
+{
+    unsigned long addr = ns16550_reg(uart, reg);
+
+    outb(val, addr);
+}
+
+void ns16550_wait_tx(const struct ns16550 *uart)
+{
+    while ( !(ns16550_inb(uart, UART_LSR) & UART_LSR_THRE) )
+        cpu_relax();
+}
+
+void ns16550_drain_rx(const struct ns16550 *uart)
+{
+    unsigned int i;
+
+    for ( i = 0; i < 128 && (ns16550_inb(uart, UART_LSR) & UART_LSR_DR); i++ )
+        (void)ns16550_inb(uart, UART_RBR);
+}
+
+void ns16550_putc(const struct ns16550 *uart, char c)
+{
+    ns16550_wait_tx(uart);
+
+    /* Inject carriage return. */
+    if ( c == '\n' )
+    {
+        ns16550_outb(uart, UART_THR, '\r');
+        ns16550_wait_tx(uart);
+    }
+
+    ns16550_outb(uart, UART_THR, c);
+}
+
+static void ns16550_write(const struct ns16550 *uart, const char *buf,
+                          size_t len)
+{
+    size_t i;
+
+    for ( i = 0; i < len; i++ )
+        ns16550_putc(uart, buf[i]);
+}
+
+void ns16550_puts(const struct ns16550 *uart, const char *buf)
+{
+    ns16550_write(uart, buf, strlen(buf));
+}
+
+int ns16550_init(const struct ns16550 *uart)
+{
+    unsigned int divisor = uart->clk_freq / (16 * uart->baud);
+
+    if ( !divisor )
+        divisor = 1;
+
+    /* Disable interrupts. */
+    ns16550_outb(uart, UART_IER, 0x00);
+
+    /* Set baud rate. */
+    ns16550_outb(uart, UART_LCR, UART_LCR_DLAB);
+    ns16550_outb(uart, UART_DLL, divisor & 0xff);
+    ns16550_outb(uart, UART_DLM, (divisor >> 8) & 0xff);
+
+    /* Support only 8 data bits, no parity, 1 stop bit. */
+    ns16550_outb(uart, UART_LCR, UART_LCR_8N1); /* DLAB=0 */
+
+    /* Reset FIFOs. */
+    ns16550_outb(uart, UART_FCR,
+                 UART_FCR_ENABLE | UART_FCR_CLRX |
+                 UART_FCR_CLTX   | UART_FCR_TRG14);
+
+    /* Assert modem control lines. */
+    ns16550_outb(uart, UART_MCR,
+                 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
+
+    /* Drain any pending status/data to avoid stale state. */
+    (void)ns16550_inb(uart, UART_LSR);
+    (void)ns16550_inb(uart, UART_RBR);
+    (void)ns16550_inb(uart, UART_IIR);
+    (void)ns16550_inb(uart, UART_MSR);
+
+    return 0;
+}
+
+/*
+ * Console driver.
+ */
+
+static void ns16550_console_write(const char *buf, size_t len)
+{
+    if (ns16550)
+        ns16550_write(ns16550, buf, len);
+}
+
+void ns16550_console_puts(const char *buf)
+{
+    ns16550_console_write(buf, strlen(buf));
+}
+
+int ns16550_console_register(const struct ns16550 *uart)
+{
+    if ( ns16550_init(uart) )
+        return -1;
+
+    register_console_callback(ns16550_console_write);
+    ns16550 = uart;
+
+    return 0;
+}
+
+void ns16550_console_unregister(void)
+{
+    ns16550 = NULL;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/include/xtf/console.h b/include/xtf/console.h
index 16a6a23e3f56..4830e511b080 100644
--- a/include/xtf/console.h
+++ b/include/xtf/console.h
@@ -3,6 +3,7 @@
 
 #include <xtf/libc.h>
 #include <xtf/compiler.h>
+#include <xtf/ns16550.h>
 
 #include <xen/event_channel.h>
 #include <xen/io/console.h>
@@ -16,17 +17,23 @@ typedef void (*cons_output_cb)(const char *buf, size_t len);
  */
 void register_console_callback(cons_output_cb cb);
 
+void vprintk(const char *fmt, va_list args) __printf(1, 0);
+void printk(const char *fmt, ...) __printf(1, 2);
+
 /*
  * Initialise the PV console.  Will register a callback.
  */
 void init_pv_console(xencons_interface_t *ring,
                      evtchn_port_t port);
-
-void vprintk(const char *fmt, va_list args) __printf(1, 0);
-void printk(const char *fmt, ...) __printf(1, 2);
-
 size_t pv_console_read_some(char *buf, size_t len);
 
+/*
+ * NS16550-backed console.
+ */
+int ns16550_console_register(const struct ns16550 *uart);
+void ns16550_console_unregister(void);
+void ns16550_console_puts(const char *buf);
+
 #endif /* XTF_CONSOLE_H */
 
 /*
diff --git a/include/xtf/ns16550.h b/include/xtf/ns16550.h
new file mode 100644
index 000000000000..5db515b7fb91
--- /dev/null
+++ b/include/xtf/ns16550.h
@@ -0,0 +1,117 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/**
+ * Simple console driver for NS16550-compatible UART.
+ */
+
+#ifndef XTF_NS16550_H
+#define XTF_NS16550_H
+
+#define UART_RBR            0x00
+#define UART_THR            0x00
+#define UART_DLL            0x00
+#define UART_IER            0x01
+#define   UART_IER_ERDAI    0x01
+#define   UART_IER_ETHREI   0x02
+#define   UART_IER_ELSI     0x04
+#define   UART_IER_EMSI     0x08
+#define   UART_IER_MASK     0x0f
+#define   UART_IER_RSRVD    0xf0
+#define UART_DLM            0x01
+#define UART_IIR            0x02
+#define   UART_IIR_NOINT    0x01
+#define   UART_IIR_IMA      0x06
+#define   UART_IIR_LSI      0x06
+#define   UART_IIR_RDA      0x04
+#define   UART_IIR_THR      0x02
+#define   UART_IIR_MSI      0x00
+#define   UART_IIR_BSY      0x07
+#define   UART_IIR_FE       0xc0
+#define   UART_IIR_MASK     0x0f
+#define UART_FCR            0x02
+#define   UART_FCR_ENABLE   0x01
+#define   UART_FCR_CLRX     0x02
+#define   UART_FCR_CLTX     0x04
+#define   UART_FCR_DMA      0x08
+#define   UART_FCR_TRG1     0x00
+#define   UART_FCR_TRG4     0x40
+#define   UART_FCR_TRG8     0x80
+#define   UART_FCR_TRG14    0xc0
+#define UART_LCR            0x03
+#define   UART_LCR_WLEN5    0x00
+#define   UART_LCR_WLEN6    0x01
+#define   UART_LCR_WLEN7    0x02
+#define   UART_LCR_WLEN8    0x03
+#define   UART_LCR_STB      0x04
+#define   UART_LCR_PE       0x08
+#define   UART_LCR_EP       0x10
+#define   UART_LCR_SP       0x20
+#define   UART_LCR_SB       0x40
+#define   UART_LCR_DLAB     0x80
+#define   UART_LCR_7N2      (UART_LCR_WLEN7 | UART_LCR_STB)
+#define   UART_LCR_8N1      (UART_LCR_WLEN8)
+#define   UART_LSR_ERRORS   (UART_LSR_OE | UART_LSR_PE | \
+                             UART_LSR_FE | UART_LSR_BI)
+#define UART_MCR            0x04
+#define   UART_MCR_DTR      0x01
+#define   UART_MCR_RTS      0x02
+#define   UART_MCR_OUT1     0x04
+#define   UART_MCR_OUT2     0x08
+#define   UART_MCR_LOOP     0x10
+#define   UART_MCR_RSRVD0   0x20
+#define   UART_MCR_TCRTLR   0x40
+#define   UART_MCR_RSRVD1   0x80
+#define   UART_MCR_MASK     (UART_MCR_DTR | UART_MCR_RTS | \
+                             UART_MCR_OUT1 | UART_MCR_OUT2 | \
+                             UART_MCR_LOOP)
+#define UART_LSR            0x05
+#define   UART_LSR_DR       0x01
+#define   UART_LSR_OE       0x02
+#define   UART_LSR_PE       0x04
+#define   UART_LSR_FE       0x08
+#define   UART_LSR_BI       0x10
+#define   UART_LSR_THRE     0x20
+#define   UART_LSR_TEMT     0x40
+#define   UART_LSR_ERR      0x80
+#define UART_MSR            0x06
+#define   UART_MSR_DCTS     0x01
+#define   UART_MSR_DDSR     0x02
+#define   UART_MSR_TERI     0x04
+#define   UART_MSR_DDCD     0x08
+#define   UART_MSR_CTS      0x10
+#define   UART_MSR_DSR      0x20
+#define   UART_MSR_RI       0x40
+#define   UART_MSR_DCD      0x80
+#define UART_SCR            0x07
+
+struct ns16550 {
+    const char *name;
+    unsigned long base;
+    unsigned int reg_shift;
+    unsigned int reg_width;
+    unsigned int clk_freq;
+    unsigned int baud;
+};
+
+uint8_t ns16550_inb(const struct ns16550 *uart, unsigned int reg);
+void ns16550_outb(const struct ns16550 *uart, unsigned int reg,
+                  unsigned char val);
+
+void ns16550_wait_tx(const struct ns16550 *uart);
+void ns16550_drain_rx(const struct ns16550 *uart);
+
+void ns16550_putc(const struct ns16550 *uart, char c);
+void ns16550_puts(const struct ns16550 *uart, const char *buf);
+
+int ns16550_init(const struct ns16550 *uart);
+
+#endif /* XTF_NS16550_H */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
-- 
2.56.0



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

* [XTF PATCH v1 6/6] x86: add NS16550-compatible emulator test
  2026-11-11 19:51 [XTF PATCH v1 0/6] x86: add NS16550-compatible UART emulator test dmukhin
                   ` (4 preceding siblings ...)
  2026-11-11 19:51 ` [XTF PATCH v1 5/6] common: add simplistic NS16550-compatible UART driver dmukhin
@ 2026-11-11 19:51 ` dmukhin
  2026-12-03  0:35   ` Stefano Stabellini
  5 siblings, 1 reply; 13+ messages in thread
From: dmukhin @ 2026-11-11 19:51 UTC (permalink / raw)
  To: xen-devel
  Cc: andrew.cooper3, anthony.perard, jbeulich, julien, michal.orzel,
	roger, sstabellini, dmukhin, Denis Mukhin

From: Denis Mukhin <dmukhin@ford.com>

Add initial test suite for NS16550-compatible emulator.

Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
 tests/emul-ns16x50/.gitignore |   3 +
 tests/emul-ns16x50/Makefile   |   9 +
 tests/emul-ns16x50/main.c     | 644 ++++++++++++++++++++++++++++++++++
 tests/emul-ns16x50/patterns.c |  65 ++++
 4 files changed, 721 insertions(+)
 create mode 100644 tests/emul-ns16x50/.gitignore
 create mode 100644 tests/emul-ns16x50/Makefile
 create mode 100644 tests/emul-ns16x50/main.c
 create mode 100644 tests/emul-ns16x50/patterns.c

diff --git a/tests/emul-ns16x50/.gitignore b/tests/emul-ns16x50/.gitignore
new file mode 100644
index 000000000000..28fbd61de7b5
--- /dev/null
+++ b/tests/emul-ns16x50/.gitignore
@@ -0,0 +1,3 @@
+*.swo
+*.swp
+
diff --git a/tests/emul-ns16x50/Makefile b/tests/emul-ns16x50/Makefile
new file mode 100644
index 000000000000..11ddfc82fb74
--- /dev/null
+++ b/tests/emul-ns16x50/Makefile
@@ -0,0 +1,9 @@
+include $(ROOT)/build/common.mk
+
+NAME      := emul-ns16x50
+CATEGORY  := functional
+TEST-ENVS := hvm64
+
+obj-perenv += main.o patterns.o
+
+include $(ROOT)/build/gen.mk
diff --git a/tests/emul-ns16x50/main.c b/tests/emul-ns16x50/main.c
new file mode 100644
index 000000000000..39a566d2b1d5
--- /dev/null
+++ b/tests/emul-ns16x50/main.c
@@ -0,0 +1,644 @@
+/**
+ * Tests for NS16550-compatible emulator.
+ */
+
+#include <xtf.h>
+#include <xtf/ns16550.h>
+
+const char test_title[] = "NS16550-compatible emulator tests";
+
+extern const char *pattern_4096;
+
+#define COM_PORT(n,b) \
+{ \
+    .name = n, \
+    .base = b, \
+    .reg_shift = 0, \
+    .reg_width = 1, \
+    .clk_freq = 1843200U, \
+    .baud = 115200U, \
+}
+
+/*
+ * PC COM ports.
+ */
+static const struct ns16550 com_ports[] = {
+    COM_PORT("com1", 0x3f8U),
+    COM_PORT("com2", 0x2f8U),
+    COM_PORT("com3", 0x3e8U),
+    COM_PORT("com4", 0x2e8U),
+};
+
+static const struct ns16550 *uart;
+
+static int ns16550_parse_cmdline(const char *s)
+{
+    const char *console = "console=";
+    size_t console_len = strlen(console);
+    unsigned int idx;
+
+    if (!s)
+        return -1;
+
+    /* console=com2 */
+    while ( *s && *s == ' ' )
+        s++;
+
+    if ( strncmp(s, console, console_len) )
+        return -1;
+
+    s += console_len;
+
+    for ( idx = 0; idx < ARRAY_SIZE(com_ports); idx++ )
+        if ( !strncmp(s, com_ports[idx].name, strlen(com_ports[idx].name)) )
+            break;
+
+    if ( idx == ARRAY_SIZE(com_ports) )
+        return -1;
+
+    return idx;
+}
+
+void test_setup(void)
+{
+    const char *cmdline;
+    int idx;
+
+    if ( !IS_DEFINED(CONFIG_HVM) )
+        return xtf_skip("N/A\n");
+
+    cmdline = (const char *)(uintptr_t)pvh_start_info->cmdline_paddr;
+    idx = ns16550_parse_cmdline(cmdline);
+    if ( idx < 0 )
+        return xtf_failure("not configured\n");
+
+    uart = &com_ports[idx];
+    printk("Testing with %s (0x%lx)...\n", uart->name, uart->base);
+
+    if ( ns16550_init(uart) )
+        return xtf_failure("failed to initialize\n");
+}
+
+static void test_thr(const char *banner)
+{
+    uint8_t iir;
+
+    ns16550_outb(uart, UART_IER, UART_IER_ETHREI);
+
+    ns16550_puts(uart, pattern_4096);
+
+    iir = ns16550_inb(uart, UART_IIR);
+    if ( (iir & (UART_IIR_NOINT | UART_IIR_THR)) == UART_IIR_NOINT )
+        return xtf_failure("%s: expect UART_IIR_THR\n", banner);
+
+    ns16550_outb(uart, UART_IER, 0);
+}
+
+static void test_scr(const char *banner)
+{
+    unsigned int in, out;
+
+    /* Enable all interrupt reasons */
+    ns16550_outb(uart, UART_IER,
+                 UART_IER_ERDAI | UART_IER_ETHREI |
+                 UART_IER_ELSI  | UART_IER_EMSI);
+
+    for ( in = 0; in < 256; in++ )
+    {
+        ns16550_outb(uart, UART_SCR, in);
+        out = ns16550_inb(uart, UART_SCR);
+        if ( in != out )
+            return xtf_failure("%s: wrote 0x%02x, read 0x%02x\n",
+                               banner, in, out);
+    }
+
+    ns16550_outb(uart, UART_IER, 0);
+}
+
+static void test_lcr(const char *banner)
+{
+    static const uint8_t patterns[] = {
+        0,
+        UART_LCR_WLEN8,
+        UART_LCR_WLEN8 | UART_LCR_STB,
+        UART_LCR_WLEN7 | UART_LCR_PE | UART_LCR_EP,
+        UART_LCR_WLEN8 | UART_LCR_SB,
+        UART_LCR_WLEN8 | UART_LCR_DLAB,
+        0xff,
+    };
+    unsigned int i;
+
+    for ( i = 0; i < ARRAY_SIZE(patterns); i++ )
+    {
+        uint8_t r;
+
+        ns16550_outb(uart, UART_LCR, patterns[i]);
+        r = ns16550_inb(uart, UART_LCR);
+        if ( r != patterns[i] )
+            xtf_failure("%s: wrote %#x, read %#x\n",
+                        banner, patterns[i], r);
+    }
+
+    ns16550_outb(uart, UART_LCR, UART_LCR_8N1);
+}
+
+static void test_ier(const char *banner)
+{
+    uint8_t in, out;
+
+    /* Test valid permutations. */
+    for ( out = 0; out <= UART_IER_MASK; out++ )
+    {
+        ns16550_outb(uart, UART_IER, out);
+        in = ns16550_inb(uart, UART_IER);
+        if ( in != out )
+            xtf_failure("%s: wrote %#x, read %#x\n",
+                        banner, out, in);
+    }
+
+    /* Ensure reserved bits are not touched. */
+    out = 0xff;
+    ns16550_outb(uart, UART_IER, out);
+    in = ns16550_inb(uart, UART_IER);
+    if ( in != UART_IER_MASK )
+        xtf_failure("%s: wrote %x, read %#x (expected %x)\n",
+                    banner, out, in, UART_IER_MASK);
+}
+
+static void test_divisor_latch(const char *banner)
+{
+    /*
+     * divisor = 115200 / baud
+     * divisor 0 is invalid
+     *
+     * DLL = divisor & 0xff
+     * DLM = divisor >> 8
+     */
+    static const struct {
+        uint8_t dll;
+        uint8_t dlm;
+    } baud_rates[] = {
+        { 0x00, 0x00 },     /* unsupported */
+        { 0xff, 0xff },     /* unsupported */
+        { 0x00, 0x09 },     /* 50 baud */
+        { 0x00, 0x06 },     /* 75 baud */
+        { 0x17, 0x04 },     /* 110 baud (approx) */
+        { 0x59, 0x03 },     /* 134.5 baud (approx) */
+        { 0x00, 0x03 },     /* 150 baud */
+        { 0x80, 0x01 },     /* 300 baud */
+        { 0xc0, 0x00 },     /* 600 baud */
+        { 0x60, 0x00 },     /* 1200 baud */
+        { 0x40, 0x00 },     /* 1800 baud */
+        { 0x3a, 0x00 },     /* 2000 baud (approx) */
+        { 0x30, 0x00 },     /* 2400 baud */
+        { 0x20, 0x00 },     /* 3600 baud */
+        { 0x18, 0x00 },     /* 4800 baud */
+        { 0x10, 0x00 },     /* 7200 baud */
+        { 0x0c, 0x00 },     /* 9600 baud */
+        { 0x08, 0x00 },     /* 14400 baud */
+        { 0x06, 0x00 },     /* 19200 baud */
+        { 0x04, 0x00 },     /* 28800 baud */
+        { 0x03, 0x00 },     /* 38400 baud */
+        { 0x02, 0x00 },     /* 57600 baud */
+        { 0x01, 0x00 },     /* 115200 baud */
+    };
+    unsigned i, n;
+
+    /* DLAB=0 */
+    ns16550_outb(uart, UART_LCR, UART_LCR_8N1);
+    /* Plant IER - that will be asserted later */
+    ns16550_outb(uart, UART_IER, UART_IER_MASK);
+
+    for ( i = 0; i < ARRAY_SIZE(baud_rates); i++ )
+    {
+        uint8_t dll, dlm, r;
+
+        /* DLAB=1 */
+        ns16550_outb(uart, UART_LCR, UART_LCR_DLAB);
+
+        /* Pre-poison to catch any cross-write. */
+        ns16550_outb(uart, UART_DLL, ~baud_rates[i].dll);
+        ns16550_outb(uart, UART_DLM, ~baud_rates[i].dlm);
+
+        ns16550_outb(uart, UART_DLL, baud_rates[i].dll);
+        ns16550_outb(uart, UART_DLM, baud_rates[i].dlm);
+
+        dll = ns16550_inb(uart, UART_DLL);
+        dlm = ns16550_inb(uart, UART_DLM);
+
+        if ( dll != baud_rates[i].dll || dlm != baud_rates[i].dlm )
+            xtf_failure("%s: 1st attempt failed: wrote {%#x,%#x}, read {%#x,%#x}\n",
+                        banner,
+                        baud_rates[i].dll, baud_rates[i].dlm,
+                        dll, dlm);
+
+        /* Flip DLAB, plant THR, assert on planted IER */
+        for ( n = 0; n < 8; n++ )
+        {
+            ns16550_outb(uart, UART_LCR, UART_LCR_8N1);
+
+            /* THR offset is the same as DLL */
+            ns16550_outb(uart, UART_THR, '\0');
+
+            /* IER offset is the same as DLM */
+            r = ns16550_inb(uart, UART_IER);
+            if ( r != UART_IER_MASK )
+                xtf_failure("%s: IER not survided DLAB flip %d: read %#x expect %#x\n",
+                            banner, n, r, UART_IER_MASK);
+
+            ns16550_outb(uart, UART_LCR, UART_LCR_DLAB);
+        }
+
+        /* Check divisor latch survives */
+        dll = ns16550_inb(uart, UART_DLL);
+        dlm = ns16550_inb(uart, UART_DLM);
+
+        if ( dll != baud_rates[i].dll || dlm != baud_rates[i].dlm )
+            xtf_failure("%s: 2nd attempt failed: wrote {%#x,%#x}, read {%#x,%#x}\n",
+                        banner,
+                        baud_rates[i].dll, baud_rates[i].dlm,
+                        dll, dlm);
+
+        /* DLAB=0 */
+        ns16550_outb(uart, UART_LCR, UART_LCR_8N1);
+    }
+}
+
+static void test_mcr(const char *banner)
+{
+    uint8_t in, out;
+
+    /*
+     * Test combinations of DTR/RTS/OUT1/OUT2 except LOOP, which
+     * is exercised by a separate test.
+     */
+    for ( out = 0; out <= (UART_MCR_DTR | UART_MCR_RTS |
+                           UART_MCR_OUT1 | UART_MCR_OUT2); out++ )
+    {
+        ns16550_outb(uart, UART_MCR, out);
+        in = ns16550_inb(uart, UART_MCR);
+        if ( in != out )
+            xtf_failure("%s: wrote %#x, read %#x\n", banner, in, out);
+    }
+
+    /*
+     * Reserved bits must read as zero.
+     */
+    ns16550_outb(uart, UART_MCR, 0xff);
+    in = ns16550_inb(uart, UART_MCR);
+    if ( in & ~UART_MCR_MASK )
+        xtf_failure("%s: reserved bits readable: %#x\n", banner, in);
+
+    ns16550_outb(uart, UART_MCR, 0);
+}
+
+static void test_fcr_toggle(const char *banner)
+{
+    uint8_t iir;
+
+    ns16550_outb(uart, UART_FCR, 0);
+    iir = ns16550_inb(uart, UART_IIR);
+    if ( iir & UART_IIR_FE )
+        xtf_failure("%s: IIR FIFO bits set with UART_FCR_ENABLE clear: %#x\n",
+                    banner, iir);
+
+    ns16550_outb(uart, UART_FCR, UART_FCR_ENABLE);
+    iir = ns16550_inb(uart, UART_IIR);
+    if ( (iir & UART_IIR_FE) != UART_IIR_FE )
+        xtf_failure("%s: IIR FIFO bits not 0b11 with UART_FCR_ENABLE set: %#x\n",
+                    banner, iir);
+
+    ns16550_outb(uart, UART_FCR, 0);
+    iir = ns16550_inb(uart, UART_IIR);
+    if ( iir & UART_IIR_FE )
+        xtf_failure("%s: IIR FIFO bits stuck after UART_FCR_ENABLE clear: %#x\n",
+                    banner, iir);
+}
+
+static void test_fcr_trigger(const char *banner)
+{
+    static const uint8_t levels[] = {
+        UART_FCR_TRG1,
+        UART_FCR_TRG4,
+        UART_FCR_TRG8,
+        UART_FCR_TRG14,
+    };
+    unsigned int i;
+
+    for ( i = 0; i < ARRAY_SIZE(levels); i++ )
+    {
+        uint8_t iir;
+
+        ns16550_outb(uart, UART_FCR, UART_FCR_ENABLE | levels[i]);
+        iir = ns16550_inb(uart, UART_IIR);
+
+#ifdef STRICT
+        if ( !(iir & UART_IIR_NOINT) )
+            xtf_failure("%s: FCR trigger %#x raised an interrupt (IIR %#x)\n",
+                        banner, levels[i], iir);
+#endif
+        if ( (iir & UART_IIR_FE) != UART_IIR_FE )
+            xtf_failure("%s: FIFO indication lost at trigger %#x (IIR %#x)\n",
+                        banner, levels[i], iir);
+    }
+}
+
+static void test_fcr_clear(const char *banner)
+{
+    unsigned int i;
+    uint8_t lsr;
+
+    ns16550_outb(uart, UART_IER, UART_IER_MASK);
+
+    for ( i = 0; i < 16; i++ )
+    {
+        ns16550_outb(uart, UART_FCR,
+                     UART_FCR_ENABLE | UART_FCR_CLRX | UART_FCR_CLTX);
+    }
+
+    /* Plant a byte via loopback. */
+    ns16550_outb(uart, UART_MCR, UART_MCR_LOOP);
+    ns16550_outb(uart, UART_THR, 'A');
+
+    lsr = ns16550_inb(uart, UART_LSR);
+    if ( lsr & UART_LSR_DR )
+    {
+        ns16550_outb(uart, UART_FCR, UART_FCR_ENABLE | UART_FCR_CLRX);
+        lsr = ns16550_inb(uart, UART_LSR);
+        if ( lsr & UART_LSR_DR )
+            xtf_failure("%s: DR still set after UART_FCR_CLRX (LSR %#x)\n",
+                        banner, lsr);
+
+        ns16550_outb(uart, UART_MCR, 0);
+        ns16550_outb(uart, UART_FCR, 0);
+    }
+    else
+    {
+        return xtf_failure("%s: loopback byte not delivered (LSR %#x)\n",
+                           banner, lsr);
+    }
+}
+
+/*
+ * MCR.LOOP.  Data path first, then the modem path (in loopback the
+ * four MCR outputs route to the four MSR inputs: DTR->DSR, RTS->CTS,
+ * OUT1->RI, OUT2->DCD).
+ */
+static void test_loopback_data_path(const char *banner)
+{
+    unsigned int v;
+
+    ns16550_outb(uart, UART_LCR, UART_LCR_8N1);
+    ns16550_outb(uart, UART_IER, 0);
+    ns16550_outb(uart, UART_FCR, UART_FCR_CLRX | UART_FCR_CLTX);
+
+    /* Discard any stale delta state. */
+    ns16550_inb(uart, UART_MSR);
+
+    ns16550_outb(uart, UART_MCR, UART_MCR_LOOP);
+    if ( !(ns16550_inb(uart, UART_MCR) & UART_MCR_LOOP) )
+        return xtf_failure("%s: UART_MCR_LOOP did not stick\n",
+                           banner);
+
+    for ( v = 0; v < 256; v++ )
+    {
+        uint8_t lsr, r;
+
+        lsr = ns16550_inb(uart, UART_LSR);
+        if ( !(lsr & UART_LSR_THRE) )
+            return xtf_failure("%s: THRE clear before THR read %#x (LSR %#x)\n",
+                               banner, v, lsr);
+
+        ns16550_outb(uart, UART_THR, v);
+
+        lsr = ns16550_inb(uart, UART_LSR);
+        if ( !(lsr & UART_LSR_DR) )
+            return xtf_failure("%s: DR clear after THT read %#x (LSR %#x)\n",
+                               banner, v, lsr);
+        if ( lsr & UART_LSR_ERRORS )
+            return xtf_failure("%s: error status after THR read %#x (LSR %#x)\n",
+                               banner, v, lsr);
+
+        r = ns16550_inb(uart, UART_RBR);
+        if ( r != v )
+            return xtf_failure("%s: loopback corrupted byte: sent %#x, got %#x\n",
+                               banner, v, r);
+
+        lsr = ns16550_inb(uart, UART_LSR);
+        if ( lsr & UART_LSR_DR )
+            return xtf_failure("%s: DR still set after draining RBR (LSR %#x)\n",
+                               banner, lsr);
+    }
+}
+
+static void test_loopback_modem_path(const char *banner)
+{
+    uint8_t msr, mask;
+
+    ns16550_outb(uart, UART_LCR, UART_LCR_8N1);
+    ns16550_outb(uart, UART_IER, 0);
+    ns16550_outb(uart, UART_FCR, UART_FCR_CLRX | UART_FCR_CLTX);
+
+    /* Discard any stale delta state. */
+    ns16550_inb(uart, UART_MSR);
+    ns16550_outb(uart, UART_MCR, UART_MCR_LOOP | UART_MCR_DTR |
+                                 UART_MCR_RTS | UART_MCR_OUT1 |
+                                 UART_MCR_OUT2);
+
+    if ( !(ns16550_inb(uart, UART_MCR) & UART_MCR_LOOP) )
+        return xtf_failure("%s: UART_MCR_LOOP did not stick\n",
+                           banner);
+
+    msr = ns16550_inb(uart, UART_MSR);
+    mask = UART_MSR_CTS | UART_MSR_DSR | UART_MSR_RI | UART_MSR_DCD |
+           UART_MSR_DCTS | UART_MSR_DDSR | UART_MSR_TERI | UART_MSR_DDCD;
+    if ( msr != mask )
+        xtf_failure("%s: MSR after raising all outputs: %#x (expected %#x)\n",
+                    banner, msr, mask);
+
+    /* The read above consumed the deltas; lines persist. */
+    msr = ns16550_inb(uart, UART_MSR);
+    mask = UART_MSR_CTS | UART_MSR_DSR | UART_MSR_RI | UART_MSR_DCD;
+    if ( msr != mask )
+        xtf_failure("%s: MSR deltas not cleared by read: %#x (expected %#x)\n",
+                    banner, msr, mask);
+
+    /*
+     * Drop all outputs.  Lines fall, DCTS/DDSR/DDCD latch again, and
+     * this time no TERI.
+     */
+    ns16550_outb(uart, UART_MCR, UART_MCR_LOOP);
+
+    msr = ns16550_inb(uart, UART_MSR);
+    mask = UART_MSR_DCTS | UART_MSR_DDSR | UART_MSR_DDCD;
+    if ( msr != mask )
+        xtf_failure("%s: MSR after dropping all outputs: %#x (expected %#x)\n",
+                    banner, msr, mask);
+
+    msr = ns16550_inb(uart, UART_MSR);
+    if ( msr )
+        xtf_failure("%s: MSR not clear after delta consumption: %#x\n",
+                    banner, msr);
+}
+
+static void test_iir_noint(const char *banner)
+{
+    uint8_t iir;
+
+    ns16550_outb(uart, UART_FCR, UART_FCR_CLRX | UART_FCR_CLTX);
+    ns16550_outb(uart, UART_MCR, UART_MCR_LOOP);
+    ns16550_outb(uart, UART_IER, 0);
+    ns16550_outb(uart, UART_THR, '\0');
+
+    iir = ns16550_inb(uart, UART_IIR);
+    if ( !(iir & UART_IIR_NOINT) )
+        xtf_failure("%s: interrupt reported with IER == 0: IIR %#x\n",
+                    banner, iir);
+}
+
+static void test_iir_thr_source(const char *banner)
+{
+    uint8_t iir;
+
+    ns16550_outb(uart, UART_FCR, UART_FCR_CLRX | UART_FCR_CLTX);
+    ns16550_outb(uart, UART_MCR, UART_MCR_LOOP);
+
+    /*
+     * THR is already empty, so enabling ETHREI must immediately
+     * assert the THR-empty source.
+     */
+    ns16550_outb(uart, UART_IER, UART_IER_ETHREI);
+
+    iir = ns16550_inb(uart, UART_IIR);
+    if ( (iir & UART_IIR_MASK) != UART_IIR_THR )
+        xtf_failure("%s: THR source not asserted on ETHREI enable: IIR %#x\n",
+                    banner, iir);
+
+    ns16550_outb(uart, UART_THR, '\0');
+    ns16550_drain_rx(uart);
+
+    iir = ns16550_inb(uart, UART_IIR);
+    if ( (iir & UART_IIR_MASK) != UART_IIR_THR )
+        xtf_failure("%s: THR source not regenerated by THR write: IIR %#x\n",
+                    banner, iir);
+
+    /* Disabling ETHREI must retract the indication. */
+    ns16550_outb(uart, UART_IER, 0);
+    iir = ns16550_inb(uart, UART_IIR);
+    if ( !(iir & UART_IIR_NOINT) )
+        xtf_failure("%s: THR source survives ETHREI disable: IIR %#x\n",
+                    banner, iir);
+}
+
+static void test_iir_rda_source(const char *banner)
+{
+    uint8_t iir;
+
+    ns16550_outb(uart, UART_FCR, UART_FCR_CLRX | UART_FCR_CLTX);
+    ns16550_outb(uart, UART_MCR, UART_MCR_LOOP);
+    ns16550_outb(uart, UART_IER, UART_IER_ERDAI);
+
+    /* Nothing pending yet. */
+    iir = ns16550_inb(uart, UART_IIR);
+    if ( !(iir & UART_IIR_NOINT) )
+        xtf_failure("%s: RDA asserted with no Rx data: IIR %#x\n",
+                    banner, iir);
+
+    /* Plant a byte; RDA must assert. */
+    ns16550_outb(uart, UART_THR, '\0');
+    iir = ns16550_inb(uart, UART_IIR);
+    if ( (iir & UART_IIR_MASK) != UART_IIR_RDA )
+        xtf_failure("%s: RDA not asserted with Rx data pending: IIR %#x\n",
+                    banner, iir);
+
+    iir = ns16550_inb(uart, UART_IIR);
+    if ( (iir & UART_IIR_MASK) != UART_IIR_RDA )
+        xtf_failure("%s: RDA cleared by IIR read: IIR %#x\n",
+                    banner, iir);
+
+    ns16550_inb(uart, UART_RBR);
+    iir = ns16550_inb(uart, UART_IIR);
+    if ( !(iir & UART_IIR_NOINT) )
+        xtf_failure("%s: RDA not cleared by draining RBR: IIR %#x\n",
+                    banner, iir);
+}
+
+static void test_iir_priority(const char *banner)
+{
+    uint8_t iir;
+
+    ns16550_outb(uart, UART_IER, UART_IER_ERDAI | UART_IER_ETHREI);
+    ns16550_outb(uart, UART_THR, '\0');
+
+    /*
+     * Priority order is LSI > RDA > timeout > THR > MSI
+     */
+    iir = ns16550_inb(uart, UART_IIR);
+    if ( (iir & UART_IIR_MASK) != UART_IIR_RDA )
+        xtf_failure("%s: RDA does not outrank THR: IIR %#x\n",
+                    banner, iir);
+
+    /*
+     * Drain Rx; THR-empty is still outstanding (the THR write above
+     * re-armed it, and no IIR read has identified it as the source
+     * while it was the highest priority), so IIR falls back to THR.
+     */
+    ns16550_inb(uart, UART_RBR);
+    iir = ns16550_inb(uart, UART_IIR);
+    if ( (iir & UART_IIR_MASK) != UART_IIR_THR )
+        xtf_failure("%s: THR not reported after RDA drained: IIR %#x\n",
+                    banner, iir);
+}
+
+struct test_case {
+    const char *banner;
+    void (*run)(const char *banner);
+};
+
+#define TEST_CASE(b, r)     { .banner = (b), .run = (r) }
+
+static const struct test_case test_suite[] = {
+    TEST_CASE("THR",            test_thr),
+    TEST_CASE("SCR",            test_scr),
+    TEST_CASE("LCR",            test_lcr),
+    TEST_CASE("IER",            test_ier),
+    TEST_CASE("DLL/DLM",        test_divisor_latch),
+    TEST_CASE("MCR",            test_mcr),
+    TEST_CASE("FCR-togggle",    test_fcr_toggle),
+    TEST_CASE("FCR-trigger",    test_fcr_trigger),
+    TEST_CASE("FCR-clear",      test_fcr_clear),
+    TEST_CASE("LOOPBACK-data",  test_loopback_data_path),
+    TEST_CASE("LOOPBACK-modem", test_loopback_modem_path),
+    TEST_CASE("IIR-noint",      test_iir_noint),
+    TEST_CASE("IIR-thr",        test_iir_thr_source),
+    TEST_CASE("IIR-rda",        test_iir_rda_source),
+    TEST_CASE("IIR-prio",       test_iir_priority),
+};
+
+void test_main(void)
+{
+    unsigned i;
+
+    for ( i = 0; i < ARRAY_SIZE(test_suite); i++ )
+    {
+        const struct test_case *t = &test_suite[i];
+
+        printk("  Test: %s\n", t->banner);
+        t->run(t->banner);
+    }
+
+    if ( !xtf_status_reported() )
+        xtf_success(NULL);
+
+    xtf_report_status();
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/tests/emul-ns16x50/patterns.c b/tests/emul-ns16x50/patterns.c
new file mode 100644
index 000000000000..b47cb6345364
--- /dev/null
+++ b/tests/emul-ns16x50/patterns.c
@@ -0,0 +1,65 @@
+const char *pattern_4096 =
+    "The quick brown fox jumps over the lazy dog. The quick brown fox"
+    " jumps over the lazy dog. The quick brown fox jumps over the laz"
+    "y dog. The quick brown fox jumps over the lazy dog. The quick br"
+    "own fox jumps over the lazy dog. The quick brown fox jumps over "
+    "the lazy dog. The quick brown fox jumps over the lazy dog. The q"
+    "uick brown fox jumps over the lazy dog. The quick brown fox jump"
+    "s over the lazy dog. The quick brown fox jumps over the lazy dog"
+    ". The quick brown fox jumps over the lazy dog. The quick brown f"
+    "ox jumps over the lazy dog. The quick brown fox jumps over the l"
+    "azy dog. The quick brown fox jumps over the lazy dog. The quick "
+    "brown fox jumps over the lazy dog. The quick brown fox jumps ove"
+    "r the lazy dog. The quick brown fox jumps over the lazy dog. The"
+    " quick brown fox jumps over the lazy dog. The quick brown fox ju"
+    "mps over the lazy dog. The quick brown fox jumps over the lazy d"
+    "og. The quick brown fox jumps over the lazy dog. The quick brown"
+    " fox jumps over the lazy dog. The quick brown fox jumps over the"
+    " lazy dog. The quick brown fox jumps over the lazy dog. The quic"
+    "k brown fox jumps over the lazy dog. The quick brown fox jumps o"
+    "ver the lazy dog. The quick brown fox jumps over the lazy dog. T"
+    "he quick brown fox jumps over the lazy dog. The quick brown fox "
+    "jumps over the lazy dog. The quick brown fox jumps over the lazy"
+    " dog. The quick brown fox jumps over the lazy dog. The quick bro"
+    "wn fox jumps over the lazy dog. The quick brown fox jumps over t"
+    "he lazy dog. The quick brown fox jumps over the lazy dog. The qu"
+    "ick brown fox jumps over the lazy dog. The quick brown fox jumps"
+    " over the lazy dog. The quick brown fox jumps over the lazy dog."
+    " The quick brown fox jumps over the lazy dog. The quick brown fo"
+    "x jumps over the lazy dog. The quick brown fox jumps over the la"
+    "zy dog. The quick brown fox jumps over the lazy dog. The quick b"
+    "rown fox jumps over the lazy dog. The quick brown fox jumps over"
+    " the lazy dog. The quick brown fox jumps over the lazy dog. The "
+    "quick brown fox jumps over the lazy dog. The quick brown fox jum"
+    "ps over the lazy dog. The quick brown fox jumps over the lazy do"
+    "g. The quick brown fox jumps over the lazy dog. The quick brown "
+    "fox jumps over the lazy dog. The quick brown fox jumps over the "
+    "lazy dog. The quick brown fox jumps over the lazy dog. The quick"
+    " brown fox jumps over the lazy dog. The quick brown fox jumps ov"
+    "er the lazy dog. The quick brown fox jumps over the lazy dog. Th"
+    "e quick brown fox jumps over the lazy dog. The quick brown fox j"
+    "umps over the lazy dog. The quick brown fox jumps over the lazy "
+    "dog. The quick brown fox jumps over the lazy dog. The quick brow"
+    "n fox jumps over the lazy dog. The quick brown fox jumps over th"
+    "e lazy dog. The quick brown fox jumps over the lazy dog. The qui"
+    "ck brown fox jumps over the lazy dog. The quick brown fox jumps "
+    "over the lazy dog. The quick brown fox jumps over the lazy dog. "
+    "The quick brown fox jumps over the lazy dog. The quick brown fox"
+    " jumps over the lazy dog. The quick brown fox jumps over the laz"
+    "y dog. The quick brown fox jumps over the lazy dog. The quick br"
+    "own fox jumps over the lazy dog. The quick brown fox jumps over "
+    "the lazy dog. The quick brown fox jumps over the lazy dog. The q"
+    "uick brown fox jumps over the lazy dog. The quick brown fox jump"
+    "s over the lazy dog. The quick brown fox jumps over the lazy dog"
+    ". The quick brown fox jumps over the lazy dog. The quick brown f"
+    "ox jumps over the lazy dog. The quick brown fox jumps over the l"
+    "azy dog. The quick brown fox jumps over the lazy dog. The quick "
+    "brown fox jumps over the lazy dog. The quick brown fox jumps ove"
+    "r the lazy dog. The quick brown fox jumps over the lazy dog. The"
+    " quick brown fox jumps over the lazy dog. The quick brown fox ju"
+    "mps over the lazy dog. The quick brown fox jumps over the lazy d"
+    "og. The quick brown fox jumps over the lazy dog. The quick brown"
+    " fox jumps over the lazy dog. The quick brown fox jumps over the"
+    " lazy dog. The quick brown fox jumps over the lazy dog. The quic"
+    "k brown fox jumps over the lazy dog. The quick brown fox jumps o"
+    "ver the lazy dog. The quick brown fox jumps over the lazy dog.\n";
-- 
2.56.0



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

* Re: [XTF PATCH v1 1/6] .gitignore: exclude tags
  2026-11-11 19:51 ` [XTF PATCH v1 1/6] .gitignore: exclude tags dmukhin
@ 2026-12-03  0:12   ` Stefano Stabellini
  0 siblings, 0 replies; 13+ messages in thread
From: Stefano Stabellini @ 2026-12-03  0:12 UTC (permalink / raw)
  To: Denis Mukhin
  Cc: xen-devel, andrew.cooper3, anthony.perard, jbeulich, julien,
	michal.orzel, roger, sstabellini, dmukhin

On Wed, 11 Nov 2026, dmukhin@ford.com wrote:
> From: Denis Mukhin <dmukhin@ford.com>
> 
> Signed-off-by: Denis Mukhin <dmukhin@ford.com>

Reviewed-by: Stefano Stabellini <sstabellini@kernel.org>


> ---
>  .gitignore | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/.gitignore b/.gitignore
> index dcacdf3b2d6c..b16635db3409 100644
> --- a/.gitignore
> +++ b/.gitignore
> @@ -15,4 +15,5 @@
>  /GRTAGS
>  /GTAGS
>  Makefile.local
> +tags
>  .vimrc
> -- 
> 2.56.0
> 


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

* Re: [XTF PATCH v1 2/6] x86: introduce cpu_relax()
  2026-11-11 19:51 ` [XTF PATCH v1 2/6] x86: introduce cpu_relax() dmukhin
@ 2026-12-03  0:13   ` Stefano Stabellini
  0 siblings, 0 replies; 13+ messages in thread
From: Stefano Stabellini @ 2026-12-03  0:13 UTC (permalink / raw)
  To: Denis Mukhin
  Cc: xen-devel, andrew.cooper3, anthony.perard, jbeulich, julien,
	michal.orzel, roger, sstabellini, dmukhin

On Wed, 11 Nov 2026, dmukhin@ford.com wrote:
> From: Denis Mukhin <dmukhin@ford.com>
> 
> Signed-off-by: Denis Mukhin <dmukhin@ford.com>

Reviewed-by: Stefano Stabellini <sstabellini@kernel.org>


> ---
>  arch/x86/include/arch/processor.h | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/arch/x86/include/arch/processor.h b/arch/x86/include/arch/processor.h
> index 9cf2fe08669f..9db352f18c7c 100644
> --- a/arch/x86/include/arch/processor.h
> +++ b/arch/x86/include/arch/processor.h
> @@ -190,6 +190,9 @@
>  #define X86_SEL_GDT       0
>  #define X86_SEL_LDT       X86_SEL_TI
>  
> +/* Pause for busy-wait loops. */
> +#define cpu_relax()       asm volatile ( "pause" ::: "memory" )
> +
>  #endif /* XTF_X86_PROCESSOR_H */
>  
>  /*
> -- 
> 2.56.0
> 


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

* Re: [XTF PATCH v1 4/6] x86: skip registering unavailable Xen console
  2026-11-11 19:51 ` [XTF PATCH v1 4/6] x86: skip registering unavailable Xen console dmukhin
@ 2026-12-03  0:14   ` Stefano Stabellini
  0 siblings, 0 replies; 13+ messages in thread
From: Stefano Stabellini @ 2026-12-03  0:14 UTC (permalink / raw)
  To: Denis Mukhin
  Cc: xen-devel, andrew.cooper3, anthony.perard, jbeulich, julien,
	michal.orzel, roger, sstabellini, dmukhin

On Wed, 11 Nov 2026, dmukhin@ford.com wrote:
> From: Denis Mukhin <dmukhin@ford.com>
> 
> Do not register Xen console for the test in case HYPERCALL_console_io
> is not available.
> 
> Signed-off-by: Denis Mukhin <dmukhin@ford.com>

Reviewed-by: Stefano Stabellini <sstabellini@kernel.org>



> ---
>  arch/x86/setup.c        | 5 +++--
>  include/xtf/hypercall.h | 4 ++--
>  2 files changed, 5 insertions(+), 4 deletions(-)
> 
> diff --git a/arch/x86/setup.c b/arch/x86/setup.c
> index 8fea6495de1c..725abe289a05 100644
> --- a/arch/x86/setup.c
> +++ b/arch/x86/setup.c
> @@ -250,7 +250,7 @@ static void qemu_console_write(const char *buf, size_t len)
>  
>  static void xen_console_write(const char *buf, size_t len)
>  {
> -    hypercall_console_write(buf, len);
> +    (void)hypercall_console_write(buf, len);
>  }
>  
>  /*
> @@ -297,7 +297,8 @@ void arch_setup(void)
>      if ( IS_DEFINED(CONFIG_HVM) && !pvh_start_info )
>          register_console_callback(qemu_console_write);
>  
> -    register_console_callback(xen_console_write);
> +    if ( !hypercall_console_write("\0", 1) )
> +        register_console_callback(xen_console_write);
>  
>      collect_cpuid(IS_DEFINED(CONFIG_PV) ? pv_cpuid_count : cpuid_count);
>  
> diff --git a/include/xtf/hypercall.h b/include/xtf/hypercall.h
> index 68b3da111d85..475eb771d472 100644
> --- a/include/xtf/hypercall.h
> +++ b/include/xtf/hypercall.h
> @@ -194,9 +194,9 @@ static inline long hypercall_argo_op(unsigned int cmd, void *arg1, void *arg2,
>  /*
>   * Higher level hypercall helpers
>   */
> -static inline void hypercall_console_write(const char *buf, unsigned long count)
> +static inline long hypercall_console_write(const char *buf, unsigned long count)
>  {
> -    (void)HYPERCALL3(long, __HYPERVISOR_console_io, CONSOLEIO_write, count, buf);
> +    return HYPERCALL3(long, __HYPERVISOR_console_io, CONSOLEIO_write, count, buf);
>  }
>  
>  static inline long hypercall_shutdown(unsigned int reason)
> -- 
> 2.56.0
> 


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

* Re: [XTF PATCH v1 3/6] x86: isolate FEP in setup_fep()
  2026-11-11 19:51 ` [XTF PATCH v1 3/6] x86: isolate FEP in setup_fep() dmukhin
@ 2026-12-03  0:21   ` Stefano Stabellini
  0 siblings, 0 replies; 13+ messages in thread
From: Stefano Stabellini @ 2026-12-03  0:21 UTC (permalink / raw)
  To: Denis Mukhin
  Cc: xen-devel, andrew.cooper3, anthony.perard, jbeulich, julien,
	michal.orzel, roger, sstabellini, dmukhin

On Wed, 11 Nov 2026, dmukhin@ford.com wrote:
> From: Denis Mukhin <dmukhin@ford.com>
> 
> Keep weak symbol test_setup() for per-test definition.
> 
> Isolate FEP into a new setup_fep() call which is executed on a common x86
> path.
> 
> Signed-off-by: Denis Mukhin <dmukhin@ford.com>
> ---
>  arch/x86/setup.c | 80 +++++++++++++++++++++++++-----------------------
>  1 file changed, 41 insertions(+), 39 deletions(-)
> 
> diff --git a/arch/x86/setup.c b/arch/x86/setup.c
> index ba6f9c320843..8fea6495de1c 100644
> --- a/arch/x86/setup.c
> +++ b/arch/x86/setup.c
> @@ -253,6 +253,45 @@ static void xen_console_write(const char *buf, size_t len)
>      hypercall_console_write(buf, len);
>  }
>  
> +/*
> + * Common test setup:
> + *
> + * xtf_has_* indicates the availabiliy of options which require runtime
> + * detection.
> + */
> +bool xtf_has_fep = false;
> +
> +/*
> + * Default weak settings.
> + *
> + * test_wants_* indicates default settings for the framework, which may be
> + * overridden by individual tests by providing non-weak variables.
> + *
> + * test_needs_* indicates options required by the test, without which the test
> + * isn't worth running.
> + */
> +bool __weak test_needs_fep = false;
> +
> +static void setup_fep(void)
> +{
> +    /*
> +     * Attempt to use the forced emulation prefix to set the value of
> +     * xtf_has_fep to the value of 1.  Use the exception table to compensate
> +     * for the #UD exception if FEP is not available.
> +     */
> +    asm volatile ("1:" _ASM_XEN_FEP "mov $1, %[fep];"
> +                  "2:"
> +                  _ASM_EXTABLE(1b, 2b)
> +                  : [fep] "=q" (xtf_has_fep)
> +                  : "0" (false));
> +
> +    if ( test_needs_fep && !xtf_has_fep )
> +    {
> +        printk("FEP unavailable, but needed by test. (Is Xen support\n");
> +        return xtf_skip("compiled in, and booted with 'hvm_fep'?)\n");
> +    }
> +}
> +
>  void arch_setup(void)
>  {
>      if ( IS_DEFINED(CONFIG_HVM) && !pvh_start_info )
> @@ -275,6 +314,8 @@ void arch_setup(void)
>      }
>  
>      map_shared_info();
> +
> +    setup_fep();
>  }

The FEP skip now happens in arch_setup(), before the banner, and
xtf_main() still calls test_setup() unconditionally. So a test that
needs FEP and has its own test_setup() runs that setup even after it's
been skipped. If that setup fails, the SKIP turns into FAILURE.

Maybe we should guard test_setup() with !xtf_status_reported()

>  int arch_get_domid(void)
> @@ -293,45 +334,6 @@ int arch_get_domid(void)
>      return -1;
>  }
>  
> -/*
> - * Common test setup:
> - *
> - * xtf_has_* indicates the availabiliy of options which require runtime
> - * detection.
> - */
> -bool xtf_has_fep = false;
> -
> -/*
> - * Default weak settings.
> - *
> - * test_wants_* indicates default settings for the framework, which may be
> - * overridden by individual tests by providing non-weak variables.
> - *
> - * test_needs_* indicates options required by the test, without which the test
> - * isn't worth running.
> - */
> -bool __weak test_needs_fep = false;
> -
> -void test_setup(void)
> -{
> -    /*
> -     * Attempt to use the forced emulation prefix to set the value of
> -     * xtf_has_fep to the value of 1.  Use the exception table to compensate
> -     * for the #UD exception if FEP is not available.
> -     */
> -    asm volatile ("1:" _ASM_XEN_FEP "mov $1, %[fep];"
> -                  "2:"
> -                  _ASM_EXTABLE(1b, 2b)
> -                  : [fep] "=q" (xtf_has_fep)
> -                  : "0" (false));
> -
> -    if ( test_needs_fep && !xtf_has_fep )
> -    {
> -        printk("FEP unavailable, but needed by test. (Is Xen support\n");
> -        return xtf_skip("compiled in, and booted with 'hvm_fep'?)\n");
> -    }
> -}
> -
>  /*
>   * Local variables:
>   * mode: C
> -- 
> 2.56.0
> 


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

* Re: [XTF PATCH v1 5/6] common: add simplistic NS16550-compatible UART driver
  2026-11-11 19:51 ` [XTF PATCH v1 5/6] common: add simplistic NS16550-compatible UART driver dmukhin
@ 2026-12-03  0:25   ` Stefano Stabellini
  0 siblings, 0 replies; 13+ messages in thread
From: Stefano Stabellini @ 2026-12-03  0:25 UTC (permalink / raw)
  To: Denis Mukhin
  Cc: xen-devel, andrew.cooper3, anthony.perard, jbeulich, julien,
	michal.orzel, roger, sstabellini, dmukhin

On Wed, 11 Nov 2026, dmukhin@ford.com wrote:
> From: Denis Mukhin <dmukhin@ford.com>
> 
> Add a miniature NS16550-compatible driver for hypervisor validation.
> 
> Implement polling mode and putchar only.
> 
> Place the code in a common place since NS16550 hardware is not limited to
> x86 systems (although the current driver uses few x86-ishms like I/O ports).
> 
> Signed-off-by: Denis Mukhin <dmukhin@ford.com>
> ---
>  build/files.mk        |   1 +
>  common/console.c      |   3 +-
>  common/ns16550.c      | 153 ++++++++++++++++++++++++++++++++++++++++++
>  include/xtf/console.h |  15 +++--
>  include/xtf/ns16550.h | 117 ++++++++++++++++++++++++++++++++
>  5 files changed, 284 insertions(+), 5 deletions(-)
>  create mode 100644 common/ns16550.c
>  create mode 100644 include/xtf/ns16550.h
> 
> diff --git a/build/files.mk b/build/files.mk
> index 11624a89a21c..841ea9ebc70a 100644
> --- a/build/files.mk
> +++ b/build/files.mk
> @@ -34,6 +34,7 @@ obj-hvm += $(ROOT)/arch/x86/hvm/head.o
>  obj-hvm += $(ROOT)/arch/x86/hvm/pagetables.o
>  obj-hvm += $(ROOT)/arch/x86/hvm/traps.o
>  obj-hvm += $(ROOT)/arch/x86/io-apic.o
> +obj-hvm += $(ROOT)/common/ns16550.o
>  
>  # Arguably common objects, but PV guests will have no interest in them.
>  obj-hvm += $(ROOT)/arch/x86/vmx.o
> diff --git a/common/console.c b/common/console.c
> index b3e89473a7b4..02ff90c5428d 100644
> --- a/common/console.c
> +++ b/common/console.c
> @@ -13,8 +13,9 @@
>   * - Xen hypervisor console
>   * - PV console
>   * - Qemu debug console
> + * - NS16550-compatible emulator
>   */
> -static cons_output_cb output_fns[3];
> +static cons_output_cb output_fns[4];
>  static unsigned int nr_cons_cb;
>  
>  /* Guest PV console details. */
> diff --git a/common/ns16550.c b/common/ns16550.c
> new file mode 100644
> index 000000000000..fa2bae5ebf98
> --- /dev/null
> +++ b/common/ns16550.c
> @@ -0,0 +1,153 @@
> +// SPDX-License-Identifier: BSD-2-Clause
> +/**
> + * Simple NS16550-compatible device driver.
> + */
> +
> +#include <arch/config.h>
> +#include <arch/traps.h>
> +
> +#include <xtf/console.h>
> +#include <xtf/ns16550.h>
> +#include <xtf/types.h>
> +
> +static const struct ns16550 *ns16550;
> +
> +static inline unsigned long
> +ns16550_reg(const struct ns16550 *uart, unsigned int reg)
> +{
> +    return uart->base + (reg << uart->reg_shift);
> +}
> +
> +uint8_t ns16550_inb(const struct ns16550 *uart, unsigned int reg)
> +{
> +    unsigned long addr = ns16550_reg(uart, reg);
> +
> +    return inb(addr);
> +}
> +
> +void ns16550_outb(const struct ns16550 *uart, unsigned int reg,
> +                  unsigned char val)
> +{
> +    unsigned long addr = ns16550_reg(uart, reg);
> +
> +    outb(val, addr);
> +}
> +
> +void ns16550_wait_tx(const struct ns16550 *uart)
> +{
> +    while ( !(ns16550_inb(uart, UART_LSR) & UART_LSR_THRE) )
> +        cpu_relax();
> +}
> +
> +void ns16550_drain_rx(const struct ns16550 *uart)
> +{
> +    unsigned int i;
> +
> +    for ( i = 0; i < 128 && (ns16550_inb(uart, UART_LSR) & UART_LSR_DR); i++ )
> +        (void)ns16550_inb(uart, UART_RBR);
> +}
> +
> +void ns16550_putc(const struct ns16550 *uart, char c)
> +{
> +    ns16550_wait_tx(uart);
> +
> +    /* Inject carriage return. */
> +    if ( c == '\n' )
> +    {
> +        ns16550_outb(uart, UART_THR, '\r');
> +        ns16550_wait_tx(uart);
> +    }
> +
> +    ns16550_outb(uart, UART_THR, c);
> +}
> +
> +static void ns16550_write(const struct ns16550 *uart, const char *buf,
> +                          size_t len)
> +{
> +    size_t i;
> +
> +    for ( i = 0; i < len; i++ )
> +        ns16550_putc(uart, buf[i]);
> +}
> +
> +void ns16550_puts(const struct ns16550 *uart, const char *buf)
> +{
> +    ns16550_write(uart, buf, strlen(buf));
> +}
> +
> +int ns16550_init(const struct ns16550 *uart)
> +{
> +    unsigned int divisor = uart->clk_freq / (16 * uart->baud);

This divides by zero if baud == 0


> +    if ( !divisor )
> +        divisor = 1;
> +
> +    /* Disable interrupts. */
> +    ns16550_outb(uart, UART_IER, 0x00);
> +
> +    /* Set baud rate. */
> +    ns16550_outb(uart, UART_LCR, UART_LCR_DLAB);
> +    ns16550_outb(uart, UART_DLL, divisor & 0xff);
> +    ns16550_outb(uart, UART_DLM, (divisor >> 8) & 0xff);
> +
> +    /* Support only 8 data bits, no parity, 1 stop bit. */
> +    ns16550_outb(uart, UART_LCR, UART_LCR_8N1); /* DLAB=0 */
> +
> +    /* Reset FIFOs. */
> +    ns16550_outb(uart, UART_FCR,
> +                 UART_FCR_ENABLE | UART_FCR_CLRX |
> +                 UART_FCR_CLTX   | UART_FCR_TRG14);
> +
> +    /* Assert modem control lines. */
> +    ns16550_outb(uart, UART_MCR,
> +                 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
> +
> +    /* Drain any pending status/data to avoid stale state. */
> +    (void)ns16550_inb(uart, UART_LSR);
> +    (void)ns16550_inb(uart, UART_RBR);
> +    (void)ns16550_inb(uart, UART_IIR);
> +    (void)ns16550_inb(uart, UART_MSR);
> +
> +    return 0;
> +}
> +
> +/*
> + * Console driver.
> + */
> +
> +static void ns16550_console_write(const char *buf, size_t len)
> +{
> +    if (ns16550)

code style


> +        ns16550_write(ns16550, buf, len);
> +}
> +
> +void ns16550_console_puts(const char *buf)
> +{
> +    ns16550_console_write(buf, strlen(buf));
> +}
> +
> +int ns16550_console_register(const struct ns16550 *uart)
> +{
> +    if ( ns16550_init(uart) )
> +        return -1;
> +
> +    register_console_callback(ns16550_console_write);
> +    ns16550 = uart;
> +
> +    return 0;
> +}
> +
> +void ns16550_console_unregister(void)
> +{
> +    ns16550 = NULL;
> +}
> +
> +/*
> + * Local variables:
> + * mode: C
> + * c-file-style: "BSD"
> + * c-basic-offset: 4
> + * tab-width: 4
> + * indent-tabs-mode: nil
> + * End:
> + */
> diff --git a/include/xtf/console.h b/include/xtf/console.h
> index 16a6a23e3f56..4830e511b080 100644
> --- a/include/xtf/console.h
> +++ b/include/xtf/console.h
> @@ -3,6 +3,7 @@
>  
>  #include <xtf/libc.h>
>  #include <xtf/compiler.h>
> +#include <xtf/ns16550.h>

don't include this from console.h as it puts every UART_* define into
every test


>  #include <xen/event_channel.h>
>  #include <xen/io/console.h>
> @@ -16,17 +17,23 @@ typedef void (*cons_output_cb)(const char *buf, size_t len);
>   */
>  void register_console_callback(cons_output_cb cb);
>  
> +void vprintk(const char *fmt, va_list args) __printf(1, 0);
> +void printk(const char *fmt, ...) __printf(1, 2);
> +
>  /*
>   * Initialise the PV console.  Will register a callback.
>   */
>  void init_pv_console(xencons_interface_t *ring,
>                       evtchn_port_t port);
> -
> -void vprintk(const char *fmt, va_list args) __printf(1, 0);
> -void printk(const char *fmt, ...) __printf(1, 2);
> -
>  size_t pv_console_read_some(char *buf, size_t len);
>  
> +/*
> + * NS16550-backed console.
> + */
> +int ns16550_console_register(const struct ns16550 *uart);
> +void ns16550_console_unregister(void);
> +void ns16550_console_puts(const char *buf);
> +
>  #endif /* XTF_CONSOLE_H */
>  
>  /*
> diff --git a/include/xtf/ns16550.h b/include/xtf/ns16550.h
> new file mode 100644
> index 000000000000..5db515b7fb91
> --- /dev/null
> +++ b/include/xtf/ns16550.h
> @@ -0,0 +1,117 @@
> +// SPDX-License-Identifier: BSD-2-Clause
> +/**
> + * Simple console driver for NS16550-compatible UART.
> + */
> +
> +#ifndef XTF_NS16550_H
> +#define XTF_NS16550_H
> +
> +#define UART_RBR            0x00
> +#define UART_THR            0x00
> +#define UART_DLL            0x00
> +#define UART_IER            0x01
> +#define   UART_IER_ERDAI    0x01
> +#define   UART_IER_ETHREI   0x02
> +#define   UART_IER_ELSI     0x04
> +#define   UART_IER_EMSI     0x08
> +#define   UART_IER_MASK     0x0f
> +#define   UART_IER_RSRVD    0xf0
> +#define UART_DLM            0x01
> +#define UART_IIR            0x02
> +#define   UART_IIR_NOINT    0x01
> +#define   UART_IIR_IMA      0x06
> +#define   UART_IIR_LSI      0x06
> +#define   UART_IIR_RDA      0x04
> +#define   UART_IIR_THR      0x02
> +#define   UART_IIR_MSI      0x00
> +#define   UART_IIR_BSY      0x07
> +#define   UART_IIR_FE       0xc0
> +#define   UART_IIR_MASK     0x0f
> +#define UART_FCR            0x02
> +#define   UART_FCR_ENABLE   0x01
> +#define   UART_FCR_CLRX     0x02
> +#define   UART_FCR_CLTX     0x04
> +#define   UART_FCR_DMA      0x08
> +#define   UART_FCR_TRG1     0x00
> +#define   UART_FCR_TRG4     0x40
> +#define   UART_FCR_TRG8     0x80
> +#define   UART_FCR_TRG14    0xc0
> +#define UART_LCR            0x03
> +#define   UART_LCR_WLEN5    0x00
> +#define   UART_LCR_WLEN6    0x01
> +#define   UART_LCR_WLEN7    0x02
> +#define   UART_LCR_WLEN8    0x03
> +#define   UART_LCR_STB      0x04
> +#define   UART_LCR_PE       0x08
> +#define   UART_LCR_EP       0x10
> +#define   UART_LCR_SP       0x20
> +#define   UART_LCR_SB       0x40
> +#define   UART_LCR_DLAB     0x80
> +#define   UART_LCR_7N2      (UART_LCR_WLEN7 | UART_LCR_STB)
> +#define   UART_LCR_8N1      (UART_LCR_WLEN8)
> +#define   UART_LSR_ERRORS   (UART_LSR_OE | UART_LSR_PE | \
> +                             UART_LSR_FE | UART_LSR_BI)
> +#define UART_MCR            0x04
> +#define   UART_MCR_DTR      0x01
> +#define   UART_MCR_RTS      0x02
> +#define   UART_MCR_OUT1     0x04
> +#define   UART_MCR_OUT2     0x08
> +#define   UART_MCR_LOOP     0x10
> +#define   UART_MCR_RSRVD0   0x20
> +#define   UART_MCR_TCRTLR   0x40
> +#define   UART_MCR_RSRVD1   0x80
> +#define   UART_MCR_MASK     (UART_MCR_DTR | UART_MCR_RTS | \
> +                             UART_MCR_OUT1 | UART_MCR_OUT2 | \
> +                             UART_MCR_LOOP)
> +#define UART_LSR            0x05
> +#define   UART_LSR_DR       0x01
> +#define   UART_LSR_OE       0x02
> +#define   UART_LSR_PE       0x04
> +#define   UART_LSR_FE       0x08
> +#define   UART_LSR_BI       0x10
> +#define   UART_LSR_THRE     0x20
> +#define   UART_LSR_TEMT     0x40
> +#define   UART_LSR_ERR      0x80
> +#define UART_MSR            0x06
> +#define   UART_MSR_DCTS     0x01
> +#define   UART_MSR_DDSR     0x02
> +#define   UART_MSR_TERI     0x04
> +#define   UART_MSR_DDCD     0x08
> +#define   UART_MSR_CTS      0x10
> +#define   UART_MSR_DSR      0x20
> +#define   UART_MSR_RI       0x40
> +#define   UART_MSR_DCD      0x80
> +#define UART_SCR            0x07
> +
> +struct ns16550 {
> +    const char *name;
> +    unsigned long base;
> +    unsigned int reg_shift;
> +    unsigned int reg_width;
> +    unsigned int clk_freq;
> +    unsigned int baud;
> +};
> +
> +uint8_t ns16550_inb(const struct ns16550 *uart, unsigned int reg);
> +void ns16550_outb(const struct ns16550 *uart, unsigned int reg,
> +                  unsigned char val);
> +
> +void ns16550_wait_tx(const struct ns16550 *uart);
> +void ns16550_drain_rx(const struct ns16550 *uart);
> +
> +void ns16550_putc(const struct ns16550 *uart, char c);
> +void ns16550_puts(const struct ns16550 *uart, const char *buf);
> +
> +int ns16550_init(const struct ns16550 *uart);
> +
> +#endif /* XTF_NS16550_H */
> +
> +/*
> + * Local variables:
> + * mode: C
> + * c-file-style: "BSD"
> + * c-basic-offset: 4
> + * tab-width: 4
> + * indent-tabs-mode: nil
> + * End:
> + */
> -- 
> 2.56.0
> 


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

* Re: [XTF PATCH v1 6/6] x86: add NS16550-compatible emulator test
  2026-11-11 19:51 ` [XTF PATCH v1 6/6] x86: add NS16550-compatible emulator test dmukhin
@ 2026-12-03  0:35   ` Stefano Stabellini
  0 siblings, 0 replies; 13+ messages in thread
From: Stefano Stabellini @ 2026-12-03  0:35 UTC (permalink / raw)
  To: Denis Mukhin
  Cc: xen-devel, andrew.cooper3, anthony.perard, jbeulich, julien,
	michal.orzel, roger, sstabellini, dmukhin

On Wed, 11 Nov 2026, dmukhin@ford.com wrote:
> From: Denis Mukhin <dmukhin@ford.com>
> 
> Add initial test suite for NS16550-compatible emulator.
> 
> Signed-off-by: Denis Mukhin <dmukhin@ford.com>
> ---
>  tests/emul-ns16x50/.gitignore |   3 +
>  tests/emul-ns16x50/Makefile   |   9 +
>  tests/emul-ns16x50/main.c     | 644 ++++++++++++++++++++++++++++++++++
>  tests/emul-ns16x50/patterns.c |  65 ++++
>  4 files changed, 721 insertions(+)
>  create mode 100644 tests/emul-ns16x50/.gitignore
>  create mode 100644 tests/emul-ns16x50/Makefile
>  create mode 100644 tests/emul-ns16x50/main.c
>  create mode 100644 tests/emul-ns16x50/patterns.c
> 
> diff --git a/tests/emul-ns16x50/.gitignore b/tests/emul-ns16x50/.gitignore
> new file mode 100644
> index 000000000000..28fbd61de7b5
> --- /dev/null
> +++ b/tests/emul-ns16x50/.gitignore
> @@ -0,0 +1,3 @@
> +*.swo
> +*.swp
> +
> diff --git a/tests/emul-ns16x50/Makefile b/tests/emul-ns16x50/Makefile
> new file mode 100644
> index 000000000000..11ddfc82fb74
> --- /dev/null
> +++ b/tests/emul-ns16x50/Makefile
> @@ -0,0 +1,9 @@
> +include $(ROOT)/build/common.mk
> +
> +NAME      := emul-ns16x50
> +CATEGORY  := functional
> +TEST-ENVS := hvm64
> +
> +obj-perenv += main.o patterns.o
> +
> +include $(ROOT)/build/gen.mk
> diff --git a/tests/emul-ns16x50/main.c b/tests/emul-ns16x50/main.c
> new file mode 100644
> index 000000000000..39a566d2b1d5
> --- /dev/null
> +++ b/tests/emul-ns16x50/main.c
> @@ -0,0 +1,644 @@
> +/**
> + * Tests for NS16550-compatible emulator.
> + */
> +
> +#include <xtf.h>
> +#include <xtf/ns16550.h>
> +
> +const char test_title[] = "NS16550-compatible emulator tests";
> +
> +extern const char *pattern_4096;
> +
> +#define COM_PORT(n,b) \
> +{ \
> +    .name = n, \
> +    .base = b, \
> +    .reg_shift = 0, \
> +    .reg_width = 1, \
> +    .clk_freq = 1843200U, \
> +    .baud = 115200U, \
> +}
> +
> +/*
> + * PC COM ports.
> + */
> +static const struct ns16550 com_ports[] = {
> +    COM_PORT("com1", 0x3f8U),
> +    COM_PORT("com2", 0x2f8U),
> +    COM_PORT("com3", 0x3e8U),
> +    COM_PORT("com4", 0x2e8U),
> +};
> +
> +static const struct ns16550 *uart;
> +
> +static int ns16550_parse_cmdline(const char *s)
> +{
> +    const char *console = "console=";
> +    size_t console_len = strlen(console);
> +    unsigned int idx;
> +
> +    if (!s)
> +        return -1;
> +
> +    /* console=com2 */
> +    while ( *s && *s == ' ' )
> +        s++;
> +
> +    if ( strncmp(s, console, console_len) )
> +        return -1;
> +
> +    s += console_len;
> +
> +    for ( idx = 0; idx < ARRAY_SIZE(com_ports); idx++ )
> +        if ( !strncmp(s, com_ports[idx].name, strlen(com_ports[idx].name)) )
> +            break;
> +
> +    if ( idx == ARRAY_SIZE(com_ports) )
> +        return -1;
> +
> +    return idx;
> +}
> +
> +void test_setup(void)
> +{
> +    const char *cmdline;
> +    int idx;
> +
> +    if ( !IS_DEFINED(CONFIG_HVM) )
> +        return xtf_skip("N/A\n");
> +
> +    cmdline = (const char *)(uintptr_t)pvh_start_info->cmdline_paddr;

pvh_start_info can be NULL on HVM


> +    idx = ns16550_parse_cmdline(cmdline);
> +    if ( idx < 0 )
> +        return xtf_failure("not configured\n");
> +
> +    uart = &com_ports[idx];
> +    printk("Testing with %s (0x%lx)...\n", uart->name, uart->base);
> +
> +    if ( ns16550_init(uart) )
> +        return xtf_failure("failed to initialize\n");
> +}
> +
> +static void test_thr(const char *banner)
> +{
> +    uint8_t iir;
> +
> +    ns16550_outb(uart, UART_IER, UART_IER_ETHREI);
> +
> +    ns16550_puts(uart, pattern_4096);
> +
> +    iir = ns16550_inb(uart, UART_IIR);
> +    if ( (iir & (UART_IIR_NOINT | UART_IIR_THR)) == UART_IIR_NOINT )

should this be: (iir & UART_IIR_MASK) != UART_IIR_THR  ?


> +        return xtf_failure("%s: expect UART_IIR_THR\n", banner);
> +
> +    ns16550_outb(uart, UART_IER, 0);
> +}
> +
> +static void test_scr(const char *banner)
> +{
> +    unsigned int in, out;
> +
> +    /* Enable all interrupt reasons */
> +    ns16550_outb(uart, UART_IER,
> +                 UART_IER_ERDAI | UART_IER_ETHREI |
> +                 UART_IER_ELSI  | UART_IER_EMSI);
> +
> +    for ( in = 0; in < 256; in++ )
> +    {
> +        ns16550_outb(uart, UART_SCR, in);
> +        out = ns16550_inb(uart, UART_SCR);
> +        if ( in != out )
> +            return xtf_failure("%s: wrote 0x%02x, read 0x%02x\n",
> +                               banner, in, out);

in and out are swapped


> +    }
> +
> +    ns16550_outb(uart, UART_IER, 0);
> +}
> +
> +static void test_lcr(const char *banner)
> +{
> +    static const uint8_t patterns[] = {
> +        0,
> +        UART_LCR_WLEN8,
> +        UART_LCR_WLEN8 | UART_LCR_STB,
> +        UART_LCR_WLEN7 | UART_LCR_PE | UART_LCR_EP,
> +        UART_LCR_WLEN8 | UART_LCR_SB,
> +        UART_LCR_WLEN8 | UART_LCR_DLAB,
> +        0xff,
> +    };
> +    unsigned int i;
> +
> +    for ( i = 0; i < ARRAY_SIZE(patterns); i++ )
> +    {
> +        uint8_t r;
> +
> +        ns16550_outb(uart, UART_LCR, patterns[i]);
> +        r = ns16550_inb(uart, UART_LCR);
> +        if ( r != patterns[i] )
> +            xtf_failure("%s: wrote %#x, read %#x\n",
> +                        banner, patterns[i], r);
> +    }
> +
> +    ns16550_outb(uart, UART_LCR, UART_LCR_8N1);
> +}
> +
> +static void test_ier(const char *banner)
> +{
> +    uint8_t in, out;
> +
> +    /* Test valid permutations. */
> +    for ( out = 0; out <= UART_IER_MASK; out++ )
> +    {
> +        ns16550_outb(uart, UART_IER, out);
> +        in = ns16550_inb(uart, UART_IER);
> +        if ( in != out )
> +            xtf_failure("%s: wrote %#x, read %#x\n",
> +                        banner, out, in);
> +    }
> +
> +    /* Ensure reserved bits are not touched. */
> +    out = 0xff;
> +    ns16550_outb(uart, UART_IER, out);
> +    in = ns16550_inb(uart, UART_IER);
> +    if ( in != UART_IER_MASK )
> +        xtf_failure("%s: wrote %x, read %#x (expected %x)\n",
> +                    banner, out, in, UART_IER_MASK);
> +}
> +
> +static void test_divisor_latch(const char *banner)
> +{
> +    /*
> +     * divisor = 115200 / baud
> +     * divisor 0 is invalid
> +     *
> +     * DLL = divisor & 0xff
> +     * DLM = divisor >> 8
> +     */
> +    static const struct {
> +        uint8_t dll;
> +        uint8_t dlm;
> +    } baud_rates[] = {
> +        { 0x00, 0x00 },     /* unsupported */
> +        { 0xff, 0xff },     /* unsupported */
> +        { 0x00, 0x09 },     /* 50 baud */
> +        { 0x00, 0x06 },     /* 75 baud */
> +        { 0x17, 0x04 },     /* 110 baud (approx) */
> +        { 0x59, 0x03 },     /* 134.5 baud (approx) */
> +        { 0x00, 0x03 },     /* 150 baud */
> +        { 0x80, 0x01 },     /* 300 baud */
> +        { 0xc0, 0x00 },     /* 600 baud */
> +        { 0x60, 0x00 },     /* 1200 baud */
> +        { 0x40, 0x00 },     /* 1800 baud */
> +        { 0x3a, 0x00 },     /* 2000 baud (approx) */
> +        { 0x30, 0x00 },     /* 2400 baud */
> +        { 0x20, 0x00 },     /* 3600 baud */
> +        { 0x18, 0x00 },     /* 4800 baud */
> +        { 0x10, 0x00 },     /* 7200 baud */
> +        { 0x0c, 0x00 },     /* 9600 baud */
> +        { 0x08, 0x00 },     /* 14400 baud */
> +        { 0x06, 0x00 },     /* 19200 baud */
> +        { 0x04, 0x00 },     /* 28800 baud */
> +        { 0x03, 0x00 },     /* 38400 baud */
> +        { 0x02, 0x00 },     /* 57600 baud */
> +        { 0x01, 0x00 },     /* 115200 baud */
> +    };
> +    unsigned i, n;
> +
> +    /* DLAB=0 */
> +    ns16550_outb(uart, UART_LCR, UART_LCR_8N1);
> +    /* Plant IER - that will be asserted later */
> +    ns16550_outb(uart, UART_IER, UART_IER_MASK);
> +
> +    for ( i = 0; i < ARRAY_SIZE(baud_rates); i++ )
> +    {
> +        uint8_t dll, dlm, r;
> +
> +        /* DLAB=1 */
> +        ns16550_outb(uart, UART_LCR, UART_LCR_DLAB);
> +
> +        /* Pre-poison to catch any cross-write. */
> +        ns16550_outb(uart, UART_DLL, ~baud_rates[i].dll);
> +        ns16550_outb(uart, UART_DLM, ~baud_rates[i].dlm);
> +
> +        ns16550_outb(uart, UART_DLL, baud_rates[i].dll);
> +        ns16550_outb(uart, UART_DLM, baud_rates[i].dlm);
> +
> +        dll = ns16550_inb(uart, UART_DLL);
> +        dlm = ns16550_inb(uart, UART_DLM);
> +
> +        if ( dll != baud_rates[i].dll || dlm != baud_rates[i].dlm )
> +            xtf_failure("%s: 1st attempt failed: wrote {%#x,%#x}, read {%#x,%#x}\n",
> +                        banner,
> +                        baud_rates[i].dll, baud_rates[i].dlm,
> +                        dll, dlm);
> +
> +        /* Flip DLAB, plant THR, assert on planted IER */
> +        for ( n = 0; n < 8; n++ )
> +        {
> +            ns16550_outb(uart, UART_LCR, UART_LCR_8N1);
> +
> +            /* THR offset is the same as DLL */
> +            ns16550_outb(uart, UART_THR, '\0');
> +
> +            /* IER offset is the same as DLM */
> +            r = ns16550_inb(uart, UART_IER);
> +            if ( r != UART_IER_MASK )
> +                xtf_failure("%s: IER not survided DLAB flip %d: read %#x expect %#x\n",
> +                            banner, n, r, UART_IER_MASK);
> +
> +            ns16550_outb(uart, UART_LCR, UART_LCR_DLAB);
> +        }
> +
> +        /* Check divisor latch survives */
> +        dll = ns16550_inb(uart, UART_DLL);
> +        dlm = ns16550_inb(uart, UART_DLM);
> +
> +        if ( dll != baud_rates[i].dll || dlm != baud_rates[i].dlm )
> +            xtf_failure("%s: 2nd attempt failed: wrote {%#x,%#x}, read {%#x,%#x}\n",
> +                        banner,
> +                        baud_rates[i].dll, baud_rates[i].dlm,
> +                        dll, dlm);
> +
> +        /* DLAB=0 */
> +        ns16550_outb(uart, UART_LCR, UART_LCR_8N1);
> +    }
> +}
> +
> +static void test_mcr(const char *banner)
> +{
> +    uint8_t in, out;
> +
> +    /*
> +     * Test combinations of DTR/RTS/OUT1/OUT2 except LOOP, which
> +     * is exercised by a separate test.
> +     */
> +    for ( out = 0; out <= (UART_MCR_DTR | UART_MCR_RTS |
> +                           UART_MCR_OUT1 | UART_MCR_OUT2); out++ )
> +    {
> +        ns16550_outb(uart, UART_MCR, out);
> +        in = ns16550_inb(uart, UART_MCR);
> +        if ( in != out )
> +            xtf_failure("%s: wrote %#x, read %#x\n", banner, in, out);
> +    }
> +
> +    /*
> +     * Reserved bits must read as zero.
> +     */
> +    ns16550_outb(uart, UART_MCR, 0xff);
> +    in = ns16550_inb(uart, UART_MCR);
> +    if ( in & ~UART_MCR_MASK )
> +        xtf_failure("%s: reserved bits readable: %#x\n", banner, in);
> +
> +    ns16550_outb(uart, UART_MCR, 0);
> +}
> +
> +static void test_fcr_toggle(const char *banner)
> +{
> +    uint8_t iir;
> +
> +    ns16550_outb(uart, UART_FCR, 0);
> +    iir = ns16550_inb(uart, UART_IIR);
> +    if ( iir & UART_IIR_FE )
> +        xtf_failure("%s: IIR FIFO bits set with UART_FCR_ENABLE clear: %#x\n",
> +                    banner, iir);
> +
> +    ns16550_outb(uart, UART_FCR, UART_FCR_ENABLE);
> +    iir = ns16550_inb(uart, UART_IIR);
> +    if ( (iir & UART_IIR_FE) != UART_IIR_FE )
> +        xtf_failure("%s: IIR FIFO bits not 0b11 with UART_FCR_ENABLE set: %#x\n",
> +                    banner, iir);
> +
> +    ns16550_outb(uart, UART_FCR, 0);
> +    iir = ns16550_inb(uart, UART_IIR);
> +    if ( iir & UART_IIR_FE )
> +        xtf_failure("%s: IIR FIFO bits stuck after UART_FCR_ENABLE clear: %#x\n",
> +                    banner, iir);
> +}
> +
> +static void test_fcr_trigger(const char *banner)
> +{
> +    static const uint8_t levels[] = {
> +        UART_FCR_TRG1,
> +        UART_FCR_TRG4,
> +        UART_FCR_TRG8,
> +        UART_FCR_TRG14,
> +    };
> +    unsigned int i;
> +
> +    for ( i = 0; i < ARRAY_SIZE(levels); i++ )
> +    {
> +        uint8_t iir;
> +
> +        ns16550_outb(uart, UART_FCR, UART_FCR_ENABLE | levels[i]);
> +        iir = ns16550_inb(uart, UART_IIR);
> +
> +#ifdef STRICT

STRICT is not defined?


> +        if ( !(iir & UART_IIR_NOINT) )
> +            xtf_failure("%s: FCR trigger %#x raised an interrupt (IIR %#x)\n",
> +                        banner, levels[i], iir);
> +#endif
> +        if ( (iir & UART_IIR_FE) != UART_IIR_FE )
> +            xtf_failure("%s: FIFO indication lost at trigger %#x (IIR %#x)\n",
> +                        banner, levels[i], iir);
> +    }
> +}
> +
> +static void test_fcr_clear(const char *banner)
> +{
> +    unsigned int i;
> +    uint8_t lsr;
> +
> +    ns16550_outb(uart, UART_IER, UART_IER_MASK);
> +
> +    for ( i = 0; i < 16; i++ )
> +    {
> +        ns16550_outb(uart, UART_FCR,
> +                     UART_FCR_ENABLE | UART_FCR_CLRX | UART_FCR_CLTX);
> +    }
> +
> +    /* Plant a byte via loopback. */
> +    ns16550_outb(uart, UART_MCR, UART_MCR_LOOP);
> +    ns16550_outb(uart, UART_THR, 'A');
> +
> +    lsr = ns16550_inb(uart, UART_LSR);
> +    if ( lsr & UART_LSR_DR )
> +    {
> +        ns16550_outb(uart, UART_FCR, UART_FCR_ENABLE | UART_FCR_CLRX);
> +        lsr = ns16550_inb(uart, UART_LSR);
> +        if ( lsr & UART_LSR_DR )
> +            xtf_failure("%s: DR still set after UART_FCR_CLRX (LSR %#x)\n",
> +                        banner, lsr);
> +
> +        ns16550_outb(uart, UART_MCR, 0);
> +        ns16550_outb(uart, UART_FCR, 0);
> +    }
> +    else
> +    {
> +        return xtf_failure("%s: loopback byte not delivered (LSR %#x)\n",
> +                           banner, lsr);
> +    }
> +}
> +
> +/*
> + * MCR.LOOP.  Data path first, then the modem path (in loopback the
> + * four MCR outputs route to the four MSR inputs: DTR->DSR, RTS->CTS,
> + * OUT1->RI, OUT2->DCD).
> + */
> +static void test_loopback_data_path(const char *banner)
> +{
> +    unsigned int v;
> +
> +    ns16550_outb(uart, UART_LCR, UART_LCR_8N1);
> +    ns16550_outb(uart, UART_IER, 0);
> +    ns16550_outb(uart, UART_FCR, UART_FCR_CLRX | UART_FCR_CLTX);

Without FCR_ENABLE this doesn't clear anything on real hardware and the
FIFOs are already off at this point. Same in the other places.



> +    /* Discard any stale delta state. */
> +    ns16550_inb(uart, UART_MSR);
> +
> +    ns16550_outb(uart, UART_MCR, UART_MCR_LOOP);
> +    if ( !(ns16550_inb(uart, UART_MCR) & UART_MCR_LOOP) )
> +        return xtf_failure("%s: UART_MCR_LOOP did not stick\n",
> +                           banner);
> +
> +    for ( v = 0; v < 256; v++ )
> +    {
> +        uint8_t lsr, r;
> +
> +        lsr = ns16550_inb(uart, UART_LSR);
> +        if ( !(lsr & UART_LSR_THRE) )
> +            return xtf_failure("%s: THRE clear before THR read %#x (LSR %#x)\n",
> +                               banner, v, lsr);
> +
> +        ns16550_outb(uart, UART_THR, v);
> +
> +        lsr = ns16550_inb(uart, UART_LSR);
> +        if ( !(lsr & UART_LSR_DR) )
> +            return xtf_failure("%s: DR clear after THT read %#x (LSR %#x)\n",
> +                               banner, v, lsr);
> +        if ( lsr & UART_LSR_ERRORS )
> +            return xtf_failure("%s: error status after THR read %#x (LSR %#x)\n",
> +                               banner, v, lsr);
> +
> +        r = ns16550_inb(uart, UART_RBR);
> +        if ( r != v )
> +            return xtf_failure("%s: loopback corrupted byte: sent %#x, got %#x\n",
> +                               banner, v, r);
> +
> +        lsr = ns16550_inb(uart, UART_LSR);
> +        if ( lsr & UART_LSR_DR )
> +            return xtf_failure("%s: DR still set after draining RBR (LSR %#x)\n",
> +                               banner, lsr);
> +    }
> +}
> +
> +static void test_loopback_modem_path(const char *banner)
> +{
> +    uint8_t msr, mask;
> +
> +    ns16550_outb(uart, UART_LCR, UART_LCR_8N1);
> +    ns16550_outb(uart, UART_IER, 0);
> +    ns16550_outb(uart, UART_FCR, UART_FCR_CLRX | UART_FCR_CLTX);
> +
> +    /* Discard any stale delta state. */
> +    ns16550_inb(uart, UART_MSR);
> +    ns16550_outb(uart, UART_MCR, UART_MCR_LOOP | UART_MCR_DTR |
> +                                 UART_MCR_RTS | UART_MCR_OUT1 |
> +                                 UART_MCR_OUT2);
> +
> +    if ( !(ns16550_inb(uart, UART_MCR) & UART_MCR_LOOP) )
> +        return xtf_failure("%s: UART_MCR_LOOP did not stick\n",
> +                           banner);
> +
> +    msr = ns16550_inb(uart, UART_MSR);
> +    mask = UART_MSR_CTS | UART_MSR_DSR | UART_MSR_RI | UART_MSR_DCD |
> +           UART_MSR_DCTS | UART_MSR_DDSR | UART_MSR_TERI | UART_MSR_DDCD;

TERI is set on the trailing edge of RI not when it is raised


> +    if ( msr != mask )
> +        xtf_failure("%s: MSR after raising all outputs: %#x (expected %#x)\n",
> +                    banner, msr, mask);
> +
> +    /* The read above consumed the deltas; lines persist. */
> +    msr = ns16550_inb(uart, UART_MSR);
> +    mask = UART_MSR_CTS | UART_MSR_DSR | UART_MSR_RI | UART_MSR_DCD;
> +    if ( msr != mask )
> +        xtf_failure("%s: MSR deltas not cleared by read: %#x (expected %#x)\n",
> +                    banner, msr, mask);
> +
> +    /*
> +     * Drop all outputs.  Lines fall, DCTS/DDSR/DDCD latch again, and
> +     * this time no TERI.
> +     */
> +    ns16550_outb(uart, UART_MCR, UART_MCR_LOOP);
> +
> +    msr = ns16550_inb(uart, UART_MSR);
> +    mask = UART_MSR_DCTS | UART_MSR_DDSR | UART_MSR_DDCD;
> +    if ( msr != mask )
> +        xtf_failure("%s: MSR after dropping all outputs: %#x (expected %#x)\n",
> +                    banner, msr, mask);
> +
> +    msr = ns16550_inb(uart, UART_MSR);
> +    if ( msr )
> +        xtf_failure("%s: MSR not clear after delta consumption: %#x\n",
> +                    banner, msr);
> +}
> +
> +static void test_iir_noint(const char *banner)
> +{
> +    uint8_t iir;
> +
> +    ns16550_outb(uart, UART_FCR, UART_FCR_CLRX | UART_FCR_CLTX);
> +    ns16550_outb(uart, UART_MCR, UART_MCR_LOOP);
> +    ns16550_outb(uart, UART_IER, 0);
> +    ns16550_outb(uart, UART_THR, '\0');
> +
> +    iir = ns16550_inb(uart, UART_IIR);
> +    if ( !(iir & UART_IIR_NOINT) )
> +        xtf_failure("%s: interrupt reported with IER == 0: IIR %#x\n",
> +                    banner, iir);
> +}
> +
> +static void test_iir_thr_source(const char *banner)
> +{
> +    uint8_t iir;
> +
> +    ns16550_outb(uart, UART_FCR, UART_FCR_CLRX | UART_FCR_CLTX);
> +    ns16550_outb(uart, UART_MCR, UART_MCR_LOOP);
> +
> +    /*
> +     * THR is already empty, so enabling ETHREI must immediately
> +     * assert the THR-empty source.
> +     */
> +    ns16550_outb(uart, UART_IER, UART_IER_ETHREI);
> +
> +    iir = ns16550_inb(uart, UART_IIR);
> +    if ( (iir & UART_IIR_MASK) != UART_IIR_THR )
> +        xtf_failure("%s: THR source not asserted on ETHREI enable: IIR %#x\n",
> +                    banner, iir);
> +
> +    ns16550_outb(uart, UART_THR, '\0');
> +    ns16550_drain_rx(uart);
> +
> +    iir = ns16550_inb(uart, UART_IIR);
> +    if ( (iir & UART_IIR_MASK) != UART_IIR_THR )
> +        xtf_failure("%s: THR source not regenerated by THR write: IIR %#x\n",
> +                    banner, iir);
> +
> +    /* Disabling ETHREI must retract the indication. */
> +    ns16550_outb(uart, UART_IER, 0);
> +    iir = ns16550_inb(uart, UART_IIR);
> +    if ( !(iir & UART_IIR_NOINT) )
> +        xtf_failure("%s: THR source survives ETHREI disable: IIR %#x\n",
> +                    banner, iir);
> +}
> +
> +static void test_iir_rda_source(const char *banner)
> +{
> +    uint8_t iir;
> +
> +    ns16550_outb(uart, UART_FCR, UART_FCR_CLRX | UART_FCR_CLTX);
> +    ns16550_outb(uart, UART_MCR, UART_MCR_LOOP);
> +    ns16550_outb(uart, UART_IER, UART_IER_ERDAI);
> +
> +    /* Nothing pending yet. */
> +    iir = ns16550_inb(uart, UART_IIR);
> +    if ( !(iir & UART_IIR_NOINT) )
> +        xtf_failure("%s: RDA asserted with no Rx data: IIR %#x\n",
> +                    banner, iir);
> +
> +    /* Plant a byte; RDA must assert. */
> +    ns16550_outb(uart, UART_THR, '\0');
> +    iir = ns16550_inb(uart, UART_IIR);
> +    if ( (iir & UART_IIR_MASK) != UART_IIR_RDA )
> +        xtf_failure("%s: RDA not asserted with Rx data pending: IIR %#x\n",
> +                    banner, iir);
> +
> +    iir = ns16550_inb(uart, UART_IIR);
> +    if ( (iir & UART_IIR_MASK) != UART_IIR_RDA )
> +        xtf_failure("%s: RDA cleared by IIR read: IIR %#x\n",
> +                    banner, iir);
> +
> +    ns16550_inb(uart, UART_RBR);
> +    iir = ns16550_inb(uart, UART_IIR);
> +    if ( !(iir & UART_IIR_NOINT) )
> +        xtf_failure("%s: RDA not cleared by draining RBR: IIR %#x\n",
> +                    banner, iir);
> +}
> +
> +static void test_iir_priority(const char *banner)
> +{
> +    uint8_t iir;
> +
> +    ns16550_outb(uart, UART_IER, UART_IER_ERDAI | UART_IER_ETHREI);
> +    ns16550_outb(uart, UART_THR, '\0');
> +
> +    /*
> +     * Priority order is LSI > RDA > timeout > THR > MSI
> +     */
> +    iir = ns16550_inb(uart, UART_IIR);
> +    if ( (iir & UART_IIR_MASK) != UART_IIR_RDA )
> +        xtf_failure("%s: RDA does not outrank THR: IIR %#x\n",
> +                    banner, iir);
> +
> +    /*
> +     * Drain Rx; THR-empty is still outstanding (the THR write above
> +     * re-armed it, and no IIR read has identified it as the source
> +     * while it was the highest priority), so IIR falls back to THR.
> +     */
> +    ns16550_inb(uart, UART_RBR);
> +    iir = ns16550_inb(uart, UART_IIR);
> +    if ( (iir & UART_IIR_MASK) != UART_IIR_THR )
> +        xtf_failure("%s: THR not reported after RDA drained: IIR %#x\n",
> +                    banner, iir);
> +}
> +
> +struct test_case {
> +    const char *banner;
> +    void (*run)(const char *banner);
> +};
> +
> +#define TEST_CASE(b, r)     { .banner = (b), .run = (r) }
> +
> +static const struct test_case test_suite[] = {
> +    TEST_CASE("THR",            test_thr),
> +    TEST_CASE("SCR",            test_scr),
> +    TEST_CASE("LCR",            test_lcr),
> +    TEST_CASE("IER",            test_ier),
> +    TEST_CASE("DLL/DLM",        test_divisor_latch),
> +    TEST_CASE("MCR",            test_mcr),
> +    TEST_CASE("FCR-togggle",    test_fcr_toggle),
> +    TEST_CASE("FCR-trigger",    test_fcr_trigger),
> +    TEST_CASE("FCR-clear",      test_fcr_clear),
> +    TEST_CASE("LOOPBACK-data",  test_loopback_data_path),
> +    TEST_CASE("LOOPBACK-modem", test_loopback_modem_path),
> +    TEST_CASE("IIR-noint",      test_iir_noint),
> +    TEST_CASE("IIR-thr",        test_iir_thr_source),
> +    TEST_CASE("IIR-rda",        test_iir_rda_source),
> +    TEST_CASE("IIR-prio",       test_iir_priority),
> +};
> +
> +void test_main(void)
> +{
> +    unsigned i;
> +
> +    for ( i = 0; i < ARRAY_SIZE(test_suite); i++ )
> +    {
> +        const struct test_case *t = &test_suite[i];
> +
> +        printk("  Test: %s\n", t->banner);
> +        t->run(t->banner);
> +    }
> +
> +    if ( !xtf_status_reported() )
> +        xtf_success(NULL);
> +
> +    xtf_report_status();

xtf_exit already does it


> +}
> +
> +/*
> + * Local variables:
> + * mode: C
> + * c-file-style: "BSD"
> + * c-basic-offset: 4
> + * tab-width: 4
> + * indent-tabs-mode: nil
> + * End:
> + */
> diff --git a/tests/emul-ns16x50/patterns.c b/tests/emul-ns16x50/patterns.c
> new file mode 100644
> index 000000000000..b47cb6345364
> --- /dev/null
> +++ b/tests/emul-ns16x50/patterns.c
> @@ -0,0 +1,65 @@
> +const char *pattern_4096 =
> +    "The quick brown fox jumps over the lazy dog. The quick brown fox"
> +    " jumps over the lazy dog. The quick brown fox jumps over the laz"
> +    "y dog. The quick brown fox jumps over the lazy dog. The quick br"
> +    "own fox jumps over the lazy dog. The quick brown fox jumps over "
> +    "the lazy dog. The quick brown fox jumps over the lazy dog. The q"
> +    "uick brown fox jumps over the lazy dog. The quick brown fox jump"
> +    "s over the lazy dog. The quick brown fox jumps over the lazy dog"
> +    ". The quick brown fox jumps over the lazy dog. The quick brown f"
> +    "ox jumps over the lazy dog. The quick brown fox jumps over the l"
> +    "azy dog. The quick brown fox jumps over the lazy dog. The quick "
> +    "brown fox jumps over the lazy dog. The quick brown fox jumps ove"
> +    "r the lazy dog. The quick brown fox jumps over the lazy dog. The"
> +    " quick brown fox jumps over the lazy dog. The quick brown fox ju"
> +    "mps over the lazy dog. The quick brown fox jumps over the lazy d"
> +    "og. The quick brown fox jumps over the lazy dog. The quick brown"
> +    " fox jumps over the lazy dog. The quick brown fox jumps over the"
> +    " lazy dog. The quick brown fox jumps over the lazy dog. The quic"
> +    "k brown fox jumps over the lazy dog. The quick brown fox jumps o"
> +    "ver the lazy dog. The quick brown fox jumps over the lazy dog. T"
> +    "he quick brown fox jumps over the lazy dog. The quick brown fox "
> +    "jumps over the lazy dog. The quick brown fox jumps over the lazy"
> +    " dog. The quick brown fox jumps over the lazy dog. The quick bro"
> +    "wn fox jumps over the lazy dog. The quick brown fox jumps over t"
> +    "he lazy dog. The quick brown fox jumps over the lazy dog. The qu"
> +    "ick brown fox jumps over the lazy dog. The quick brown fox jumps"
> +    " over the lazy dog. The quick brown fox jumps over the lazy dog."
> +    " The quick brown fox jumps over the lazy dog. The quick brown fo"
> +    "x jumps over the lazy dog. The quick brown fox jumps over the la"
> +    "zy dog. The quick brown fox jumps over the lazy dog. The quick b"
> +    "rown fox jumps over the lazy dog. The quick brown fox jumps over"
> +    " the lazy dog. The quick brown fox jumps over the lazy dog. The "
> +    "quick brown fox jumps over the lazy dog. The quick brown fox jum"
> +    "ps over the lazy dog. The quick brown fox jumps over the lazy do"
> +    "g. The quick brown fox jumps over the lazy dog. The quick brown "
> +    "fox jumps over the lazy dog. The quick brown fox jumps over the "
> +    "lazy dog. The quick brown fox jumps over the lazy dog. The quick"
> +    " brown fox jumps over the lazy dog. The quick brown fox jumps ov"
> +    "er the lazy dog. The quick brown fox jumps over the lazy dog. Th"
> +    "e quick brown fox jumps over the lazy dog. The quick brown fox j"
> +    "umps over the lazy dog. The quick brown fox jumps over the lazy "
> +    "dog. The quick brown fox jumps over the lazy dog. The quick brow"
> +    "n fox jumps over the lazy dog. The quick brown fox jumps over th"
> +    "e lazy dog. The quick brown fox jumps over the lazy dog. The qui"
> +    "ck brown fox jumps over the lazy dog. The quick brown fox jumps "
> +    "over the lazy dog. The quick brown fox jumps over the lazy dog. "
> +    "The quick brown fox jumps over the lazy dog. The quick brown fox"
> +    " jumps over the lazy dog. The quick brown fox jumps over the laz"
> +    "y dog. The quick brown fox jumps over the lazy dog. The quick br"
> +    "own fox jumps over the lazy dog. The quick brown fox jumps over "
> +    "the lazy dog. The quick brown fox jumps over the lazy dog. The q"
> +    "uick brown fox jumps over the lazy dog. The quick brown fox jump"
> +    "s over the lazy dog. The quick brown fox jumps over the lazy dog"
> +    ". The quick brown fox jumps over the lazy dog. The quick brown f"
> +    "ox jumps over the lazy dog. The quick brown fox jumps over the l"
> +    "azy dog. The quick brown fox jumps over the lazy dog. The quick "
> +    "brown fox jumps over the lazy dog. The quick brown fox jumps ove"
> +    "r the lazy dog. The quick brown fox jumps over the lazy dog. The"
> +    " quick brown fox jumps over the lazy dog. The quick brown fox ju"
> +    "mps over the lazy dog. The quick brown fox jumps over the lazy d"
> +    "og. The quick brown fox jumps over the lazy dog. The quick brown"
> +    " fox jumps over the lazy dog. The quick brown fox jumps over the"
> +    " lazy dog. The quick brown fox jumps over the lazy dog. The quic"
> +    "k brown fox jumps over the lazy dog. The quick brown fox jumps o"
> +    "ver the lazy dog. The quick brown fox jumps over the lazy dog.\n";
> -- 
> 2.56.0
> 


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

end of thread, other threads:[~2026-12-03  0:35 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-11 19:51 [XTF PATCH v1 0/6] x86: add NS16550-compatible UART emulator test dmukhin
2026-11-11 19:51 ` [XTF PATCH v1 1/6] .gitignore: exclude tags dmukhin
2026-12-03  0:12   ` Stefano Stabellini
2026-11-11 19:51 ` [XTF PATCH v1 2/6] x86: introduce cpu_relax() dmukhin
2026-12-03  0:13   ` Stefano Stabellini
2026-11-11 19:51 ` [XTF PATCH v1 3/6] x86: isolate FEP in setup_fep() dmukhin
2026-12-03  0:21   ` Stefano Stabellini
2026-11-11 19:51 ` [XTF PATCH v1 4/6] x86: skip registering unavailable Xen console dmukhin
2026-12-03  0:14   ` Stefano Stabellini
2026-11-11 19:51 ` [XTF PATCH v1 5/6] common: add simplistic NS16550-compatible UART driver dmukhin
2026-12-03  0:25   ` Stefano Stabellini
2026-11-11 19:51 ` [XTF PATCH v1 6/6] x86: add NS16550-compatible emulator test dmukhin
2026-12-03  0:35   ` Stefano Stabellini

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