[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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





On 11/11/26 8:47 PM, dmukhin@xxxxxxxx wrote:
From: Denis Mukhin <dmukhin@xxxxxxxx>

Add DLL/DLM registers emulation.

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

They are initialised to the divisor for 115200, but guest writes are
stored and read back, so the guest sees whatever it programmed. Maybe
"DLL/DLM are reset to the divisor for 115200 baud; the divisor doesn't
affect emulation otherwise".

Also, LCR only becomes writable in 08/26, so until then DLAB is always
0 and none of the code added here is reachable. Would it make sense to
order the LCR patch first?


Signed-off-by: Denis Mukhin <dmukhin@xxxxxxxx>
---
  xen/common/emul/vuart/ns16x50.c | 82 +++++++++++++++++++++++++++++++++
  1 file changed, 82 insertions(+)

diff --git a/xen/common/emul/vuart/ns16x50.c b/xen/common/emul/vuart/ns16x50.c
index 6e1cfecbaa55..80805ee6ecc6 100644
--- a/xen/common/emul/vuart/ns16x50.c
+++ b/xen/common/emul/vuart/ns16x50.c
@@ -119,8 +119,37 @@ static inline void ns16x50_unlock(struct vuart_ns16x50 
*vdev,
  static int ns16x50_io_write8(
      struct vuart_ns16x50 *vdev, uint32_t reg, uint8_t *data)
  {
+    uint8_t *regs = vdev->regs;
+    uint8_t val = *data;
      int rc = 0;
+ switch ( reg )
+    {
+    case UART_THR: /* DLAB=0 --> UART_RBR, DLAB=1 --> UART_DLL */

On the write side, offset 0 with DLAB=0 is THR, not RBR.

+        /* DLAB=1 */
+        if ( regs[UART_LCR] & UART_LCR_DLAB )
+        {
+            regs[NS16X50_REGS_NUM + UART_DLL] = val;
+            break;
+        }
+
+        /* DLAB=0 */
+        break;
+
+    case UART_IER: /* DLAB=0 --> UART_IER, DLAB=1 --> UART_DLM */
+        /* DLAB=1 */
+        if ( regs[UART_LCR] & UART_LCR_DLAB )
+        {
+            regs[NS16X50_REGS_NUM + UART_DLM] = val;
+        }

Nit: no braces around a single statement (and it's inconsistent with
the read side, which uses break inside the if).

+        /* DLAB=0 */
+        break;
+
+    default:
+        rc = -EINVAL;
+        break;
+    }
+
      return rc;
  }
@@ -131,8 +160,17 @@ static int ns16x50_io_write8(
  static int ns16x50_io_write16(
      struct vuart_ns16x50 *vdev, uint32_t reg, uint16_t *data)
  {
+    uint8_t *regs = vdev->regs;
+    uint16_t val = *data;
      int rc = -EINVAL;
+ if ( (regs[UART_LCR] & UART_LCR_DLAB) && reg == UART_DLL )
+    {
+        vdev->regs[NS16X50_REGS_NUM + UART_DLL] = val & UINT8_MAX;
+        vdev->regs[NS16X50_REGS_NUM + UART_DLM] = (val >> 8) & UINT8_MAX;

Nit: regs is declared but vdev->regs is used right below (same in
ns16x50_io_read16()).

More importantly, special-casing a 16-bit access to DLL with DLAB=1
covers just one case. E.g. QEMU's 16550 model accepts 2- and 4-byte
accesses but implements byte accesses only, so an outw() to base+0 ends
up as byte accesses to base+0 and base+1, each decoded according to the
current DLAB (serial_io_ops in hw/char/serial.c, with the splitting done
by access_with_adjusted_size() in system/memory.c). As far as I know,
that's also what happens on real hardware for an 8-bit device behind
ISA/LPC. Am I missing somethings? if no doing the same here would cover all registers and drop the ns16x50_io_{read,write}16() helpers, something like:

    for ( i = 0; i < size; i++ )
    {
        uint8_t byte = *data >> (i * 8);

        rc = ns16x50_io_write8(vdev, reg + i, &byte);
        if ( rc )
            break;
    }

(and the reverse for reads, assembling *data from the bytes). As it
stands, e.g. an outw() to THR/IER with DLAB=0 is rejected and logged as
an error.

+        rc = 0;
+    }
+
      return rc;
  }
@@ -168,9 +206,39 @@ static int ns16x50_io_write(
  static int ns16x50_io_read8(
      struct vuart_ns16x50 *vdev, uint32_t reg, uint8_t *data)
  {
+    uint8_t *regs = vdev->regs;
      uint8_t val = UINT8_MAX;
      int rc = 0;
+ switch ( reg )
+    {
+    case UART_RBR: /* DLAB=0 --> UART_RBR, DLAB=1 --> UART_DLL */
+        /* DLAB=1 */
+        if ( regs[UART_LCR] & UART_LCR_DLAB )
+        {
+            val = regs[NS16X50_REGS_NUM + UART_DLL];
+            break;
+        }
+
+        /* DLAB=0 */
+        break;
+
+    case UART_IER: /* DLAB=0 --> UART_IER, DLAB=1 --> UART_DLM */
+        /* DLAB=1 */
+        if ( regs[UART_LCR] & UART_LCR_DLAB )
+        {
+            val = regs[NS16X50_REGS_NUM + UART_DLM];
+            break;
+        }
+
+        /* DLAB=0 */
+        break;
+
+    default:
+        rc = -EINVAL;
+        break;
+    }
+
      *data = val;
return rc;
@@ -182,9 +250,17 @@ static int ns16x50_io_read8(
  static int ns16x50_io_read16(
      struct vuart_ns16x50 *vdev, uint32_t reg, uint16_t *data)
  {
+    uint8_t *regs = vdev->regs;
      uint16_t val = UINT16_MAX;
      int rc = -EINVAL;
+ if ( (regs[UART_LCR] & UART_LCR_DLAB) && reg == UART_DLL )
+    {
+        val = vdev->regs[NS16X50_REGS_NUM + UART_DLM] << 8 |
+              vdev->regs[NS16X50_REGS_NUM + UART_DLL];
+        rc = 0;
+    }
+
      *data = val;
return rc;
@@ -310,7 +386,13 @@ static int cf_check ns16x50_io_handle(
static void ns16x50_reset(struct vuart_ns16x50 *vdev)
  {
+    const uint16_t divisor = (UART_CLOCK_HZ / 115200) >> 4;

Nit: UART_CLOCK_HZ / (16 * 115200) reads more directly as the divisor
formula. A comment on why the divisor is preset at all would also help:
a real 16550 doesn't reset DLL/DLM, so this is a choice made for guests
that never program the baud rate.

+
      memset(vdev->regs, 0, sizeof(vdev->regs));
+
+    /* NB: report 115200 baud rate. */
+    vdev->regs[NS16X50_REGS_NUM + UART_DLL] = divisor & UINT8_MAX;
+    vdev->regs[NS16X50_REGS_NUM + UART_DLM] = (divisor >> 8) & UINT8_MAX;
  }
static int ns16x50_init(void *arg)

~ Oleksii



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.