[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH v8 10/26] emul/ns16x50: implement RBR register
- To: dmukhin@xxxxxxxx, xen-devel@xxxxxxxxxxxxxxxxxxxx
- From: Oleksii Kurochko <oleksii.kurochko@xxxxxxxxx>
- Date: Wed, 18 Nov 2026 17:28:36 +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: Wed, 18 Nov 2026 16:28:53 +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 RBR register emulation to the I/O port handlder.
Nit: s/handlder/handler/ (also in 11/26).
Add RX FIFO management code since RBR depends on RX FIFO.
Emulator does not emulate NS8250 (no FIFO), NS16550a (16 bytes)
or NS16750 (64 bytes) precisely for simplicity. No baud rate
emulation either.
RX FIFO is emulated by means of using xencons_interface which
conveniently provides primitives for buffer management and later can be
used for inter-domain communication similarly to vpl011.
Account for DLL == 0: in this case, disable receiver.
Add UART_LSR_DR handling since it depends on RBR register access.
Implement put_rx() vUART hook for placing a character into the
emulated RX FIFO from console driver. That implements physical console
forwarding to the guest OS over emulated NS16550.
Signed-off-by: Denis Mukhin <dmukhin@xxxxxxxx>
---
xen/common/emul/vuart/ns16x50.c | 132 +++++++++++++++++++++++++++++++-
1 file changed, 130 insertions(+), 2 deletions(-)
diff --git a/xen/common/emul/vuart/ns16x50.c b/xen/common/emul/vuart/ns16x50.c
index 9f530e589495..d04caf2baf2c 100644
--- a/xen/common/emul/vuart/ns16x50.c
+++ b/xen/common/emul/vuart/ns16x50.c
@@ -123,6 +123,76 @@ static inline void ns16x50_unlock(struct vuart_ns16x50
*vdev,
spin_unlock(&vdev->lock);
}
+static bool ns16x50_fifo_rx_empty(const struct vuart_ns16x50 *vdev)
+{
+ const struct xencons_interface *cons = &vdev->cons;
+
+ return cons->in_prod == cons->in_cons;
+}
+
+static bool ns16x50_fifo_rx_full(const struct vuart_ns16x50 *vdev)
+{
+ const struct xencons_interface *cons = &vdev->cons;
+
+ return cons->in_prod - cons->in_cons == ARRAY_SIZE(cons->in);
+}
+
+static void ns16x50_fifo_rx_reset(struct vuart_ns16x50 *vdev)
+{
+ struct xencons_interface *cons = &vdev->cons;
+
+ cons->in_cons = cons->in_prod;
+}
+
+/*
+ * Transfer character from RX FIFO and return the RX FIFO status after the
+ * transfer.
+ */
+static int ns16x50_fifo_rx_getchar(struct vuart_ns16x50 *vdev, uint8_t *ptr)
+{
+ struct xencons_interface *cons = &vdev->cons;
+
+ if ( ns16x50_fifo_rx_empty(vdev) )
+ return -ENODATA;
+
+ *ptr = cons->in[MASK_XENCONS_IDX(cons->in_cons, cons->in)];
+ cons->in_cons++;
+
+ return ns16x50_fifo_rx_empty(vdev) ? -ENODATA : 0;
+}
+
+static int ns16x50_fifo_rx_putchar(struct vuart_ns16x50 *vdev, char c)
+{
+ struct xencons_interface *cons = &vdev->cons;
+ int rc;
+
+ /*
+ * FIFO-less 8250/16450 UARTs: newly arrived word overwrites the contents
+ * of the THR.
+ */
+ if ( ns16x50_fifo_rx_full(vdev) )
+ {
+ ns16x50_debug(vdev, "RX FIFO full; resetting\n");
+ ns16x50_fifo_rx_reset(vdev);
+ rc = -ENOSPC;
+ }
On overrun this throws away the whole 1 KiB of buffered input and keeps
the new character. A 16550 in FIFO mode does the opposite (SLLS177I,
7.7.8, LSR bit 1): "an overrun error occurs only after the FIFO is full
and the next character has been completely received in the shift
register. [...] The character in the shift register is overwritten, but
it is not transferred to the FIFO." I.e. set OE and drop the new
character. Also, the comment talks about THR where RBR is meant.
Also, the comment is mentioning FIFO-less but here we are implementing
FIFO-based UART. Don't we won't then separate such cases? (Another
question will it be really totally wrong to still have FIFO-based
emulation for FIFO-less UARTs which are mentioned in the comment?)
+ else
+ rc = 0;
+
+ cons->in[MASK_XENCONS_IDX(cons->in_prod, cons->in)] = c;
+ cons->in_prod++;
+
+ return rc;
+}
+
+static bool ns16x50_is_tranceiver_enabled(const struct vuart_ns16x50 *vdev)
+{
+ BUG_ON(vdev->regs[UART_LCR] & UART_LCR_DLAB);
Nit: s/tranceiver/transceiver/. Also, all callers check DLAB first, but
it's guest-controlled state on a guest-reachable path; ASSERT() seems
enough.
+
+ /* DLL set to 0 disables serial communication. */
+ return vdev->regs[NS16X50_REGS_NUM + UART_DLL];
+}
+
static bool cf_check ns16x50_iir_check_lsi(const struct vuart_ns16x50 *vdev)
{
return vdev->regs[UART_LSR] & UART_LSR_MASK;
@@ -130,7 +200,7 @@ static bool cf_check ns16x50_iir_check_lsi(const struct
vuart_ns16x50 *vdev)
static bool cf_check ns16x50_iir_check_rda(const struct vuart_ns16x50 *vdev)
{
- return false;
+ return !ns16x50_fifo_rx_empty(vdev);
}
static bool cf_check ns16x50_iir_check_thr(const struct vuart_ns16x50 *vdev)
@@ -400,6 +470,17 @@ static int ns16x50_io_read8(
}
/* DLAB=0 */
+ if ( !ns16x50_is_tranceiver_enabled(vdev) )
+ break;
+
+ /* NB: do not forget to clear overrun condition */
+ regs[UART_LSR] &= ~UART_LSR_OE;
OE is cleared by reading LSR, not RBR: "OE is cleared every time the CPU
reads the contents of the LSR" (same section), which 08/26 already
implements. Clearing it here means a driver that reads RBR before LSR
never sees the overrun.
+
+ if ( ns16x50_fifo_rx_getchar(vdev, &val) )
+ regs[UART_LSR] &= ~UART_LSR_DR;
+ else
+ regs[UART_LSR] |= UART_LSR_DR;
+
LSR.DR is just "RX FIFO not empty", which ns16x50_iir_check_rda()
already computes from the FIFO. Storing it in regs[UART_LSR] as well
means four places have to update it by hand: the RBR read here,
put_rx(), the FCR RX clear (12/26) and THR in loopback mode (11/26).
They're all correct today, but the two copies can drift apart with the
next change. Computing DR from the FIFO when LSR is read would remove
those updates and keep LSR and IIR consistent by construction:
case UART_LSR:
val = regs[UART_LSR] & ~UART_LSR_DR;
if ( !ns16x50_fifo_rx_empty(vdev) )
val |= UART_LSR_DR;
regs[UART_LSR] &= ~UART_LSR_MASK;
break;
Something like that?
break;
case UART_IER: /* DLAB=0 --> UART_IER, DLAB=1 --> UART_DLM */
@@ -694,13 +775,60 @@ static void cf_check ns16x50_free(void *arg)
xvfree(arg);
}
+static int cf_check ns16x50_put_rx(void *arg, char ch)
+{
+ struct vuart_ns16x50 *vdev = arg;
+ unsigned long flags;
+ uint8_t *regs;
+ int rc = -EBUSY;
+
+ ASSERT(vdev);
+
+ ns16x50_lock(vdev, &flags);
Don't we have the issue with having a such lock here?
This is reached from __serial_rx(), and on x86, when the physical UART
uses its IRQ, that is hard IRQ context: ns16550_interrupt() ->
serial_rx_interrupt() -> serial_rx() -> __serial_rx() -> vuart_put_rx().
vdev->lock is otherwise taken by ns16x50_access() with IRQs enabled, so
a serial IRQ arriving on a pCPU whose vCPU is inside ns16x50_access()
spins forever, and CONFIG_DEBUG_LOCKS' check_lock() should trip on the
mixed IRQ safety. The same applies to d->arch.hvm.irq_lock, taken under
it via ns16x50_irq_check() -> hvm_isa_irq_assert(). Making vdev->lock
IRQ-safe isn't enough because of the latter. If I didn't mistake
something then deferring the delivery out of IRQ context (e.g. queue the
character and let a tasklet update LSR/IIR and the IRQ line) would avoid
both.
+
+ regs = vdev->regs;
+
+ if ( vdev->regs[UART_LCR] & UART_LCR_DLAB )
+ ns16x50_debug(vdev, "THR/RBR access disabled: DLAB=1\n");
DLAB only selects what offsets 0 and 1 decode to; reception continues,
and the character is readable from RBR once DLAB is cleared. Dropping
input is right for loopback (SIN is disconnected) and arguably for
DLL == 0, but not here. (Each dropped character also produces a
"failed to process console input" warning, see my reply to 01/26.)
(ns16x50_is_tranceiver_enabled() would then need to lose its BUG_ON():
it only looks at the stored DLL, which doesn't depend on DLAB.)
+ else if ( !ns16x50_is_tranceiver_enabled(vdev) )
+ ns16x50_debug(vdev, "THR/RBR access disabled: DLAB=0 DLL=0\n");
+ else if ( regs[UART_MCR] & UART_MCR_LOOP )
+ ns16x50_debug(vdev, "THR/RBR access disabled: DLAB=0 loopback mode\n");
+ else
+ {
+ const struct domain *d = vdev->owner;
+
+ /*
+ * Echo the user input on Xen console iff Xen console input is owned
+ * by ns16x50 domain.
+ * NB: use 'console_timestamps=none' to disable Xen timestamps.
+ */
+ if ( is_console_printable(ch) )
+ guest_printk(d, "%c", ch);
The guest's tty already echoes the input, so every typed character
appears twice on the Xen console, and the two streams interleave with
guest output. Worse, the guest can't turn this echo off: when the guest
disables echo (e.g. login prompting for a password), the characters are
still printed by Xen. I'd drop it. Also, "iff Xen console input is
owned by ns16x50 domain" is always true here, as __serial_rx() only
calls this for the domain owning the input.
+
+ if ( ns16x50_fifo_rx_putchar(vdev, ch) )
+ regs[UART_LSR] |= UART_LSR_OE;
+
+ regs[UART_LSR] |= UART_LSR_DR;
+
+ /* TODO: check FCR when to fire an interrupt */
+ ns16x50_irq_check(vdev);> +
+ rc = 0;
+ }
+
+ ns16x50_unlock(vdev, &flags);
+
+ return rc;
+}
+
~ Oleksii
|