Skip to contentSkip to conversation

char: cadence: check divider against baud rate

PATCH Superseded P J P 4 participants 12 messages Oct 18, 2016 – Oct 25, 2016

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

P J P

author
#0 lore raw

PATCH 1/1 char: cadence: check divider against baud rate

From: Prasad J Pandit <[email protected]>

The Cadence UART device emulator calculates speed by dividing the baud rate by a divider. If this divider was to be zero or if baud rate was to be lesser than the divider, it could lead to a divide by zero error. Add check to avoid it.

---
 hw/char/cadence_uart.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/char/cadence_uart.c +4 -0
index e3bc52f..b18dd7f 100644
@@ -170,6 +170,10 @@ static void uart_parameters_setup(CadenceUARTState *s)
     baud_rate = (s->r[R_MR] & UART_MR_CLKS) ?
             UART_INPUT_CLK / 8 : UART_INPUT_CLK;
 
+    if (!s->r[R_BRGR] || !(s->r[R_BDIV] + 1)
+        || baud_rate < (s->r[R_BRGR] * (s->r[R_BDIV] + 1))) {
+        return;
+    }
     ssp.speed = baud_rate / (s->r[R_BRGR] * (s->r[R_BDIV] + 1));
     packet_size = 1;
 
Signature · 1 line
2.7.4
#1 lore raw
Quoted 18 lines · P J P

From: Prasad J Pandit <[email protected]>

The Cadence UART device emulator calculates speed by dividing the baud rate by a divider. If this divider was to be zero or if baud rate was to be lesser than the divider, it could lead to a divide by zero error. Add check to avoid it.

---
 hw/char/cadence_uart.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/char/cadence_uart.c +0 -0
index e3bc52f..b18dd7f 100644

hw/char/cadence_uart.c static void uart_parameters_setup(CadenceUARTState *s) · line 178

Quoted 9 lines
hw/char/cadence_uart.c +4 -0
@@ -170,6 +170,10 @@ static void uart_parameters_setup(CadenceUARTState *s)
     baud_rate = (s->r[R_MR] & UART_MR_CLKS) ?
             UART_INPUT_CLK / 8 : UART_INPUT_CLK;
 
+    if (!s->r[R_BRGR] || !(s->r[R_BDIV] + 1)
+        || baud_rate < (s->r[R_BRGR] * (s->r[R_BDIV] + 1))) {
+        return;
+    }
     ssp.speed = baud_rate / (s->r[R_BRGR] * (s->r[R_BDIV] + 1));
     packet_size = 1;

It seems really unlikely that early return here is the correct thing, since it will result in our not correctly setting a bunch of the other stuff done later in this function that's unrelated to baud rate. What does the datasheet for this UART specify for this situation?

thanks
-- PMM

author
#2 lore raw
+-- On Tue, 18 Oct 2016, Peter Maydell wrote --+
| > +    if (!s->r[R_BRGR] || !(s->r[R_BDIV] + 1)
| > +        || baud_rate < (s->r[R_BRGR] * (s->r[R_BDIV] + 1))) {
| > +        return;
| > +    }
| >      ssp.speed = baud_rate / (s->r[R_BRGR] * (s->r[R_BDIV] + 1));
| >      packet_size = 1;
| 
| It seems really unlikely that early return here is the correct thing, since
| it will result in our not correctly setting a bunch of the other
| stuff done later in this function that's unrelated to baud rate.
| What does the datasheet for this UART specify for this situation?

@Alistair.. wdyt?
--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F

#3 lore raw
Quoted 13 lines · P J P
+-- On Tue, 18 Oct 2016, Peter Maydell wrote --+
| > +    if (!s->r[R_BRGR] || !(s->r[R_BDIV] + 1)
| > +        || baud_rate < (s->r[R_BRGR] * (s->r[R_BDIV] + 1))) {
| > +        return;
| > +    }
| >      ssp.speed = baud_rate / (s->r[R_BRGR] * (s->r[R_BDIV] + 1));
| >      packet_size = 1;
|
| It seems really unlikely that early return here is the correct thing, since
| it will result in our not correctly setting a bunch of the other
| stuff done later in this function that's unrelated to baud rate.
| What does the datasheet for this UART specify for this situation?
Quoted 1 line

@Alistair.. wdyt?

I'm traveling this week, so I won't have a chance to check the data sheet until next week.

Generally these types of IP just say 'undefined' in invalid situations like this. If that is the case we shouldn't return but just set some default value and allow the other functions in the setup to continue on.

Thanks,

Alistair

Quoted 4 lines

--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F

author
#4 lore raw

Hello Alistair,

+-- On Wed, 19 Oct 2016, Alistair Francis wrote --+
| Generally these types of IP just say 'undefined' in invalid situations
| like this. If that is the case we shouldn't return but just set some
| default value and allow the other functions in the setup to continue
| on.

Could you please point me to its data sheet? I went through quite a few that
I came across, but none was helpful.

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F

#5 lore raw
Quoted 10 lines · P J P

Hello Alistair,

+-- On Wed, 19 Oct 2016, Alistair Francis wrote --+
| Generally these types of IP just say 'undefined' in invalid situations
| like this. If that is the case we shouldn't return but just set some
| default value and allow the other functions in the setup to continue
| on.

Could you please point me to its data sheet? I went through quite a few that
I came across, but none was helpful.

I don't think the Cadence datasheets are public. If you can't find it from a simple Google search I don't think you have access.

Thanks,

Alistair

Quoted 6 lines

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F

author
#6 lore raw
+-- On Wed, 19 Oct 2016, Alistair Francis wrote --+
| I don't think the Cadence datasheets are public. If you can't find it
| from a simple Google search I don't think you have access.

I see, okay.

I think it'll greatly help if device emulator writers include a link to its specification/datasheet resources in the respective source file. At least if these are publicly available. That way one knows for sure which specifications to follow while debugging, instead of searching and sifting through multiple different versions. OR Maybe there could a field in the MAINTAINERS file which would point to respective reference resources. (just a thought)

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F

#7 lore raw
Quoted 4 lines · P J P
+-- On Wed, 19 Oct 2016, Alistair Francis wrote --+
| I don't think the Cadence datasheets are public. If you can't find it
| from a simple Google search I don't think you have access.
Quoted 8 lines

I see, okay.

I think it'll greatly help if device emulator writers include a link to its specification/datasheet resources in the respective source file. At least if these are publicly available. That way one knows for sure which specifications to follow while debugging, instead of searching and sifting through multiple different versions. OR Maybe there could a field in the MAINTAINERS file which would point to respective reference resources. (just a thought)

Hi,

The UART is described in the Zynq TRMs, I think you can just google for zynq TRM and a reference to UG585 and UG1085 will come up.

http://www.xilinx.com/support/documentation/user_guides/ug585-Zynq-7000-TRM.pdf
http://www.xilinx.com/support/documentation/user_guides/ug1085-zynq-ultrascale-trm.pdf

AFAIK, the ZynqMP has a newer version of the cadence UART with a superset of the feature set. I don't think QEMU models any of the differences though.

Cheers,
Edgar

Quoted 6 lines

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F

author
#8 lore raw
+-- On Thu, 20 Oct 2016, Edgar E. Iglesias wrote --+
| The UART is described in the Zynq TRMs, I think you can just
| google for zynq TRM and a reference to UG585 and UG1085 will come up.

Unlikely for a novice to think 'zynq TRM' as search term for cadence UART. I
went with Xilinx Cadence UART specificaiton.

| http://www.xilinx.com/support/documentation/user_guides/ug585-Zynq-7000-TRM.pdf
| http://www.xilinx.com/support/documentation/user_guides/ug1085-zynq-ultrascale-trm.pdf
| 
| AFAIK, the ZynqMP has a newer version of the cadence UART with a superset of
| the feature set. I don't think QEMU models any of the differences though.

I see, thank you for the details. I'll add these to the source file.

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F

#9 lore raw
Quoted 7 lines · P J P
+-- On Thu, 20 Oct 2016, Edgar E. Iglesias wrote --+
| The UART is described in the Zynq TRMs, I think you can just
| google for zynq TRM and a reference to UG585 and UG1085 will come up.

Unlikely for a novice to think 'zynq TRM' as search term for cadence UART. I
went with Xilinx Cadence UART specificaiton.

Quoted 7 lines
| http://www.xilinx.com/support/documentation/user_guides/ug585-Zynq-7000-TRM.pdf
| http://www.xilinx.com/support/documentation/user_guides/ug1085-zynq-ultrascale-trm.pdf
|
| AFAIK, the ZynqMP has a newer version of the cadence UART with a superset of
| the feature set. I don't think QEMU models any of the differences though.

I see, thank you for the details. I'll add these to the source file.

Did the TRM have enough detail for you to figure out how the hardware behaves?

Thanks,

Alistair

Quoted 6 lines

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F

author
#10 lore raw
+-- On Mon, 24 Oct 2016, Alistair Francis wrote --+
| > | http://www.xilinx.com/support/documentation/user_guides/ug585-Zynq-7000-TRM.pdf
| 
| Did the TRM have enough detail for you to figure out how the hardware behaves?

Yes, it defines range for 'Baud rate generator' and 'Baud rate divider'
register values and their default reset value. I've sent patch v2.

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F

#11 lore raw
Quoted 7 lines · P J P
+-- On Mon, 24 Oct 2016, Alistair Francis wrote --+
| > | http://www.xilinx.com/support/documentation/user_guides/ug585-Zynq-7000-TRM.pdf
|
| Did the TRM have enough detail for you to figure out how the hardware behaves?

Yes, it defines range for 'Baud rate generator' and 'Baud rate divider'
register values and their default reset value. I've sent patch v2.

Great!

Thanks,

Alistair

Quoted 6 lines

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F