[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH v8 11/26] emul/ns16x50: implement THR register
- To: dmukhin@xxxxxxxx, xen-devel@xxxxxxxxxxxxxxxxxxxx
- From: Oleksii Kurochko <oleksii.kurochko@xxxxxxxxx>
- Date: Thu, 19 Nov 2026 11:22:00 +0100
- Authentication-results: eu.smtp.expurgate.cloud; dkim=pass header.s=20260707 header.d=gmail.com header.i="@gmail.com" header.h="Content-Transfer-Encoding:Content-Type:In-Reply-To:From:Content-Language:References:Cc:To:Subject:User-Agent:MIME-Version:Date:Message-ID"
- Cc: andrew.cooper3@xxxxxxxxxx, anthony.perard@xxxxxxxxxx, jbeulich@xxxxxxxx, julien@xxxxxxx, michal.orzel@xxxxxxx, roger@xxxxxxxxxxxxxx, sstabellini@xxxxxxxxxx, xakep.amatop@xxxxxxxxx, dmukhin@xxxxxxx
- Delivery-date: Thu, 19 Nov 2026 10:22:11 +0000
- List-id: Xen developer discussion <xen-devel.lists.xenproject.org>
On 11/11/26 8:47 PM, dmukhin@xxxxxxxx wrote:
From: Denis Mukhin <dmukhin@xxxxxxxx>
Add THR register emulation to the I/O port handlder.
Add TX FIFO management code since THR depends on TX FIFO.
TX FIFOs is emulated by using xencons_interface which conveniently
Nit: s/handlder/handler/, s/FIFOs is/FIFO is/.
provides primitives for buffer management and later can be used for
inter-domain communication similarly to vpl011.
Account for DLL == 0: in this case, disable transmitter.
Add UART_IIR_THR interrupt reason handling since it depends on THR
register access.
Signed-off-by: Denis Mukhin <dmukhin@xxxxxxxx>
---
xen/common/emul/vuart/ns16x50.c | 121 +++++++++++++++++++++++++++++++-
1 file changed, 120 insertions(+), 1 deletion(-)
diff --git a/xen/common/emul/vuart/ns16x50.c b/xen/common/emul/vuart/ns16x50.c
index d04caf2baf2c..8d3845a8a4cd 100644
--- a/xen/common/emul/vuart/ns16x50.c
+++ b/xen/common/emul/vuart/ns16x50.c
@@ -98,6 +98,7 @@ struct vuart_ns16x50 {
spinlock_t lock; /* Protection */
struct xencons_interface cons; /* Emulated RX/TX FIFOs */
> bool irq_asserted; /* IRQ line status */> +
bool thr_pending; /* THR interrupt reason status */
};
static inline void ns16x50_lock_init(struct vuart_ns16x50 *vdev)
@@ -185,6 +186,72 @@ static int ns16x50_fifo_rx_putchar(struct vuart_ns16x50
*vdev, char c)
return rc;
}
+static bool ns16x50_fifo_tx_full(const struct vuart_ns16x50 *vdev)
+{
+ const struct xencons_interface *cons = &vdev->cons;
+
+ return cons->out_prod - cons->out_cons == ARRAY_SIZE(cons->out);
+}
Sorry, for noticing that only now but I think it is necessary to raise
this question. Is it okay to use xencons_interface for FIFOs?
xencons_interface covers 1024 bytes (sizeof(cons->in)) but real UART is
covering 16 bytes so overrun happens only after overflow of 1024 (not
16). I can assume that for emulator it could be fine but such behaviour
differs from the datasheet, so, at least, it could/should be mentioned
in the comment (where cons member of struct vurat_ns16x50 is declared)
that such behaviour is okay (if it is really okay?).
+
+static void ns16x50_fifo_tx_reset(struct vuart_ns16x50 *vdev)
+{
+ struct xencons_interface *cons = &vdev->cons;
+
+ cons->out_cons = cons->out_prod;
+}
+
+/*
+ * Flush cached output to Xen console.
+ */
Nit: the comment here doesn't require to be multiline comment.
+static void ns16x50_fifo_tx_flush(struct vuart_ns16x50 *vdev)
+{
+ struct xencons_interface *cons = &vdev->cons;
+ struct domain *d = vdev->owner;
+ XENCONS_RING_IDX i, j, n, m, len = cons->out_prod - cons->out_cons;
+
+ ASSERT(len <= ARRAY_SIZE(cons->out));
+ if ( !len )
+ return;
+
+ /* Tail of the FIFO */
+ i = MASK_XENCONS_IDX(cons->out_cons, cons->out);
+ n = min_t(XENCONS_RING_IDX, len, ARRAY_SIZE(cons->out) - i);
+
+ /* Head of the FIFO */
+ j = 0;
+ m = len - n;
Nit: out_cons is where the oldest data starts, and the second part is
the wrapped-around remainder at the start of the array; "Tail" and
"Head" read the other way round. Maybe "Up to the end of the buffer" /
"Wrapped-around part".
+
+ /* Linearize the FIFO */
+ if ( m )
+ guest_printk(d, guest_prefix "%.*s%.*s",
+ n, &cons->out[i],
+ m, &cons->out[j]);
+ else
+ guest_printk(d, guest_prefix "%.*s", n, &cons->out[i]);
+
+ cons->out_cons += len;
+}
+
+/*
+ * Accumulate guest OS output before sending to Xen console.
+ */
+static void ns16x50_fifo_tx_putchar(struct vuart_ns16x50 *vdev, char ch)
+{
+ struct xencons_interface *cons = &vdev->cons;
+
+ if ( !is_console_printable(ch) )
+ return;
+
+ if ( !ns16x50_fifo_tx_full(vdev) )
+ {
+ cons->out[MASK_XENCONS_IDX(cons->out_prod, cons->out)] = ch;
+ cons->out_prod++;
+ }
+
+ if ( ch == '\n' || ch == '\0' || ns16x50_fifo_tx_full(vdev) )
+ ns16x50_fifo_tx_flush(vdev);
Output is only printed once a line is complete. Nothing is lost, but
anything not followed by a newline shows up late: the "login: "
prompt, a shell prompt, and the guest's echo of typed characters all
appear only once the next newline arrives, i.e. after Enter. I suspect
this is what the echo in ns16x50_put_rx() (10/26) compensates for, at
the cost of printing every typed character twice and of printing input
the guest deliberately doesn't echo (passwords).
vpl011_write_data_xen() on Arm handles this by printing each character
right away while the domain owns the console input, and line-buffering
only otherwise. Doing the same here would make the console interactive
and let the echo in put_rx() go.
Also, ch == '\0' can't be true here, as is_console_printable() has
already filtered it out. And ns16x50_fifo_tx_full() is called twice to
check the same so shouldn't this if be combined with 'else' branch of
previous if () statetment?
+}
+
static bool ns16x50_is_tranceiver_enabled(const struct vuart_ns16x50 *vdev)
{
BUG_ON(vdev->regs[UART_LCR] & UART_LCR_DLAB);
@@ -205,7 +272,7 @@ static bool cf_check ns16x50_iir_check_rda(const struct
vuart_ns16x50 *vdev)
static bool cf_check ns16x50_iir_check_thr(const struct vuart_ns16x50 *vdev)
{
- return false;
+ return vdev->thr_pending;
}
static bool cf_check ns16x50_iir_check_msi(const struct vuart_ns16x50 *vdev)
@@ -316,6 +383,38 @@ static int ns16x50_io_write8(
}
/* DLAB=0 */
+ if ( !ns16x50_is_tranceiver_enabled(vdev) )
+ break;
+
+ /*
+ * THR is no longer empty.
+ */
Nit: the comment here doesn't require to be multiline comment.
+ vdev->thr_pending = false;
+ regs[UART_LSR] &= ~(UART_LSR_THRE | UART_LSR_TEMT);
+
+ /*
+ * Drop the IRQ line before completing this immediate
+ * transmission, if THRE was its only reason.
+ */
+ ns16x50_irq_check(vdev);
+
+ if ( regs[UART_MCR] & UART_MCR_LOOP )
+ {
+ if ( ns16x50_fifo_rx_putchar(vdev, val) )
+ regs[UART_LSR] |= UART_LSR_OE;
+
+ regs[UART_LSR] |= UART_LSR_DR;
+ }
+ else
+ ns16x50_fifo_tx_putchar(vdev, val);
+
+ /*
+ * Backend transmission is synchronous in this emulator,
+ * so THR is immediately empty again.
+ */
+ regs[UART_LSR] |= UART_LSR_THRE | UART_LSR_TEMT;
+ vdev->thr_pending = true;
+
break;
case UART_IER: /* DLAB=0 --> UART_IER, DLAB=1 --> UART_DLM */
@@ -327,7 +426,13 @@ static int ns16x50_io_write8(
/* DLAB=0 */
else
{
+ uint8_t old = regs[UART_IER];
+
regs[UART_IER] = val & UART_IER_ALL_INTR;
+
+ if ( (old ^ regs[UART_IER]) & UART_IER_ETHREI )
+ vdev->thr_pending = ( (regs[UART_IER] & UART_IER_ETHREI) &&
+ (regs[UART_LSR] & UART_LSR_THRE) );
Nit: no spaces inside the outer parentheses.
}
break;
[...]
~ Oleksii
|