Skip to contentSkip to conversation

hw/char/pl011: Use correct masks for IBRD and FBRD

PATCH In review Peter Maydell 4 participants 4 messages Oct 7, 2024 – Oct 9, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/char/pl011: Use correct masks for IBRD and FBRD

In commit b88cfee90268cad we defined masks for the IBRD and FBRD integer and fractional baud rate divider registers, to prevent the guest from writing invalid values which could cause division-by-zero. Unfortunately we got the mask values the wrong way around: the FBRD register is six bits and the IBRD register is 16 bits, not vice-versa.

You would only run into this bug if you programmed the UART to a baud rate of less than 9600, because for 9600 baud and above the IBRD value will fit into 6 bits, as per the table in
https://developer.arm.com/documentation/ddi0183/g/programmers-model/register-descriptions/fractional-baud-rate-register--uartfbrd

The only visible effects would be that the value read back from the register by the guest would be truncated, and we would print an incorrect baud rate in the debug logs.

Fixes b88cfee90268 ("hw/char/pl011: Avoid division-by-zero in pl011_get_baudrate()")

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2610

---
 hw/char/pl011.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/char/pl011.c +2 -2
index 15df7c1e1ca..0fd1334fab4 100644
@@ -90,10 +90,10 @@ DeviceState *pl011_create(hwaddr addr, qemu_irq irq, Chardev *chr)
 #define CR_UARTEN   (1 << 0)
 
 /* Integer Baud Rate Divider, UARTIBRD */
-#define IBRD_MASK 0x3f
+#define IBRD_MASK 0xffff
 
 /* Fractional Baud Rate Divider, UARTFBRD */
-#define FBRD_MASK 0xffff
+#define FBRD_MASK 0x3f
 
 static const unsigned char pl011_id_arm[8] =
   { 0x11, 0x10, 0x14, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Peter Maydell

In commit b88cfee90268cad we defined masks for the IBRD and FBRD integer and fractional baud rate divider registers, to prevent the guest from writing invalid values which could cause division-by-zero. Unfortunately we got the mask values the wrong way around: the FBRD register is six bits and the IBRD register is 16 bits, not vice-versa.

You would only run into this bug if you programmed the UART to a baud rate of less than 9600, because for 9600 baud and above the IBRD value will fit into 6 bits, as per the table in
https://developer.arm.com/documentation/ddi0183/g/programmers-model/register-descriptions/fractional-baud-rate-register--uartfbrd

Quoted 8 lines

The only visible effects would be that the value read back from the register by the guest would be truncated, and we would print an incorrect baud rate in the debug logs.

Fixes b88cfee90268 ("hw/char/pl011: Avoid division-by-zero in pl011_get_baudrate()")

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2610

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
Reviewed-by on v1 1/1
#2 lore raw
In reply to Peter Maydell
Quoted 6 lines · Peter Maydell

In commit b88cfee90268cad we defined masks for the IBRD and FBRD integer and fractional baud rate divider registers, to prevent the guest from writing invalid values which could cause division-by-zero. Unfortunately we got the mask values the wrong way around: the FBRD register is six bits and the IBRD register is 16 bits, not vice-versa.

Oops indeed, I didn't check this closely enough.

Quoted 16 lines

You would only run into this bug if you programmed the UART to a baud rate of less than 9600, because for 9600 baud and above the IBRD value will fit into 6 bits, as per the table in
https://developer.arm.com/documentation/ddi0183/g/programmers-model/register-descriptions/fractional-baud-rate-register--uartfbrd

The only visible effects would be that the value read back from the register by the guest would be truncated, and we would print an incorrect baud rate in the debug logs.

Fixes b88cfee90268 ("hw/char/pl011: Avoid division-by-zero in pl011_get_baudrate()")

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2610

---
  hw/char/pl011.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
Reviewed-by on v1 1/1
#3 lore raw
In reply to Peter Maydell
Quoted 16 lines · Peter Maydell

In commit b88cfee90268cad we defined masks for the IBRD and FBRD integer and fractional baud rate divider registers, to prevent the guest from writing invalid values which could cause division-by-zero. Unfortunately we got the mask values the wrong way around: the FBRD register is six bits and the IBRD register is 16 bits, not vice-versa.

You would only run into this bug if you programmed the UART to a baud rate of less than 9600, because for 9600 baud and above the IBRD value will fit into 6 bits, as per the table in
https://developer.arm.com/documentation/ddi0183/g/programmers-model/register-descriptions/fractional-baud-rate-register--uartfbrd

The only visible effects would be that the value read back from the register by the guest would be truncated, and we would print an incorrect baud rate in the debug logs.

Quoted 8 lines
Fixes b88cfee90268 ("hw/char/pl011: Avoid division-by-zero in pl011_get_baudrate()")

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2610

---
  hw/char/pl011.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)