Skip to contentSkip to conversation

hw/misc/bcm2835_rng: Specify valid memory access sizes

PATCH In review Peter Maydell 2 participants 4 messages May 1 – May 5

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/misc/bcm2835_rng: Specify valid memory access sizes

The BCM2835 RNG has 32-bit registers only; specify this in the MemoryRegionOps so wrong-sized accesses are rejected rather than getting to the assertions in the read and write functions.

Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3394

Fixes 54a5ba13a9f ("target-arm: Implement BCM2835 hardware RNG")
---
 hw/misc/bcm2835_rng.c | 2 ++
 1 file changed, 2 insertions(+)
hw/misc/bcm2835_rng.c +2 -0
index e4d2c224c8..bd4acade5b 100644
@@ -93,6 +93,8 @@ static const MemoryRegionOps bcm2835_rng_ops = {
     .read = bcm2835_rng_read,
     .write = bcm2835_rng_write,
     .endianness = DEVICE_NATIVE_ENDIAN,
+    .valid.min_access_size = 4,
+    .valid.max_access_size = 4,
 };
 
 static const VMStateDescription vmstate_bcm2835_rng = {
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw

Hi Peter,

Quoted 17 lines · Peter Maydell

The BCM2835 RNG has 32-bit registers only; specify this in the MemoryRegionOps so wrong-sized accesses are rejected rather than getting to the assertions in the read and write functions.

Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3394

Fixes 54a5ba13a9f ("target-arm: Implement BCM2835 hardware RNG")
---
  hw/misc/bcm2835_rng.c | 2 ++
  1 file changed, 2 insertions(+)
hw/misc/bcm2835_rng.c +0 -0
index e4d2c224c8..bd4acade5b 100644

hw/misc/bcm2835_rng.c static const MemoryRegionOps bcm2835_rng_ops = { · line 97

Quoted 5 lines
hw/misc/bcm2835_rng.c +2 -0
@@ -93,6 +93,8 @@ static const MemoryRegionOps bcm2835_rng_ops = {
      .read = bcm2835_rng_read,
      .write = bcm2835_rng_write,
      .endianness = DEVICE_NATIVE_ENDIAN,
+    .valid.min_access_size = 4,
+    .valid.max_access_size = 4,

.impl is clearly 4 (why not set it? I tend to think the default values -- where unset -- come from adapting pre-MemoryRegionOps models, and today better be explicit and avoid implicit defaults here).

What is the rationale for max_access_size=4, that the ARM core is a 32-bit CPU? I guess remember these peripherals are on an AXI bus accessible by the videocore as 64-bit accesses. But QEMU doesn't model that, and other peripherals are also limited to 32-bit max. So good enough for now I suppose.

Quoted 3 lines
hw/misc/bcm2835_rng.c +0 -0
  };
 
  static const VMStateDescription vmstate_bcm2835_rng = {
#2 lore raw
Quoted 31 lines · Philippe Mathieu-Daudé

Hi Peter,

Quoted 17 lines · Peter Maydell

The BCM2835 RNG has 32-bit registers only; specify this in the MemoryRegionOps so wrong-sized accesses are rejected rather than getting to the assertions in the read and write functions.

Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3394

Fixes 54a5ba13a9f ("target-arm: Implement BCM2835 hardware RNG")
---
  hw/misc/bcm2835_rng.c | 2 ++
  1 file changed, 2 insertions(+)
hw/misc/bcm2835_rng.c +0 -0
index e4d2c224c8..bd4acade5b 100644

hw/misc/bcm2835_rng.c static const MemoryRegionOps bcm2835_rng_ops = { · line 97

Quoted 5 lines
hw/misc/bcm2835_rng.c +2 -0
@@ -93,6 +93,8 @@ static const MemoryRegionOps bcm2835_rng_ops = {
      .read = bcm2835_rng_read,
      .write = bcm2835_rng_write,
      .endianness = DEVICE_NATIVE_ENDIAN,
+    .valid.min_access_size = 4,
+    .valid.max_access_size = 4,

.impl is clearly 4 (why not set it? I tend to think the default values -- where unset -- come from adapting pre-MemoryRegionOps models, and today better be explicit and avoid implicit defaults here).

Quoted 5 lines

What is the rationale for max_access_size=4, that the ARM core is a 32-bit CPU? I guess remember these peripherals are on an AXI bus accessible by the videocore as 64-bit accesses. But QEMU doesn't model that, and other peripherals are also limited to 32-bit max. So good enough for now I suppose.

The rationale is that the registers are all 32 bit, and we're already asserting that size == 4, so we know there's no code out there in the wild that's trying to do accesses at different sizes.

Adding

+ .impl.min_access_size = 4,
+ .impl.max_access_size = 4,

is probably a good idea here, I agree.

-- PMM

#3 lore raw
Quoted 38 lines · Peter Maydell
Quoted 31 lines · Philippe Mathieu-Daudé

Hi Peter,

Quoted 17 lines · Peter Maydell

The BCM2835 RNG has 32-bit registers only; specify this in the MemoryRegionOps so wrong-sized accesses are rejected rather than getting to the assertions in the read and write functions.

Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3394

Fixes 54a5ba13a9f ("target-arm: Implement BCM2835 hardware RNG")
---
   hw/misc/bcm2835_rng.c | 2 ++
   1 file changed, 2 insertions(+)
hw/misc/bcm2835_rng.c +0 -0
index e4d2c224c8..bd4acade5b 100644

hw/misc/bcm2835_rng.c static const MemoryRegionOps bcm2835_rng_ops = { · line 97

Quoted 5 lines
hw/misc/bcm2835_rng.c +2 -0
@@ -93,6 +93,8 @@ static const MemoryRegionOps bcm2835_rng_ops = {
       .read = bcm2835_rng_read,
       .write = bcm2835_rng_write,
       .endianness = DEVICE_NATIVE_ENDIAN,
+    .valid.min_access_size = 4,
+    .valid.max_access_size = 4,

.impl is clearly 4 (why not set it? I tend to think the default values -- where unset -- come from adapting pre-MemoryRegionOps models, and today better be explicit and avoid implicit defaults here).

Quoted 5 lines

What is the rationale for max_access_size=4, that the ARM core is a 32-bit CPU? I guess remember these peripherals are on an AXI bus accessible by the videocore as 64-bit accesses. But QEMU doesn't model that, and other peripherals are also limited to 32-bit max. So good enough for now I suppose.

Quoted 4 lines

The rationale is that the registers are all 32 bit, and we're already asserting that size == 4, so we know there's no code out there in the wild that's trying to do accesses at different sizes.

My worry is about someone extending the model, allowing different access sizes.

Quoted 7 lines

Adding

+ .impl.min_access_size = 4,
+ .impl.max_access_size = 4,

is probably a good idea here, I agree.

Thanks,

Phil.