Skip to contentSkip to conversation

hw/usb/hcd-dwc2: Handle invalid address access in read and write functions

PATCH Applied Zheyu Ma 4 participants 6 messages Jun 18, 2024 – Jun 20, 2024

lore
Share & tools
lore

Zheyu Ma

author
#0 lore raw

PATCH 1/1 hw/usb/hcd-dwc2: Handle invalid address access in read and write functions

This commit modifies the dwc2_hsotg_read() and dwc2_hsotg_write() functions to handle invalid address access gracefully. Instead of using g_assert_not_reached(), which causes the program to abort, the functions now log an error message and return a default value for reads or do nothing for writes.

This change prevents the program from aborting and provides clear log messages indicating when an invalid memory address is accessed.

Reproducer:
cat << EOF | qemu-system-aarch64 -display none \
-machine accel=qtest, -m 512M -machine raspi2b -m 1G -nodefaults \
-usb -drive file=null-co://,if=none,format=raw,id=disk0 -device \
usb-storage,port=1,drive=disk0 -qtest stdio
readl 0x3f980dfb
EOF

---
 hw/usb/hcd-dwc2.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
hw/usb/hcd-dwc2.c +7 -2
index 8cac9c0a06..b4f0652c7d 100644
@@ -1128,7 +1128,10 @@ static uint64_t dwc2_hsotg_read(void *ptr, hwaddr addr, unsigned size)
         val = dwc2_pcgreg_read(ptr, addr, (addr - HSOTG_REG(0xe00)) >> 2, size);
         break;
     default:
-        g_assert_not_reached();
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%"HWADDR_PRIx"\n",
+                      __func__, addr);
+        val = 0;
+        break;
     }
 
     return val;
@@ -1160,7 +1163,9 @@ static void dwc2_hsotg_write(void *ptr, hwaddr addr, uint64_t val,
         dwc2_pcgreg_write(ptr, addr, (addr - HSOTG_REG(0xe00)) >> 2, val, size);
         break;
     default:
-        g_assert_not_reached();
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%"HWADDR_PRIx"\n",
+                      __func__, addr);
+        break;
     }
 }
 
Signature · 1 line
2.34.1
#1 lore raw

Cc'ing Paul.

Quoted 49 lines · Zheyu Ma

This commit modifies the dwc2_hsotg_read() and dwc2_hsotg_write() functions to handle invalid address access gracefully. Instead of using g_assert_not_reached(), which causes the program to abort, the functions now log an error message and return a default value for reads or do nothing for writes.

This change prevents the program from aborting and provides clear log messages indicating when an invalid memory address is accessed.

Reproducer:
cat << EOF | qemu-system-aarch64 -display none \

(quoted diff) +0 -2
-machine accel=qtest, -m 512M -machine raspi2b -m 1G -nodefaults \
-usb -drive file=null-co://,if=none,format=raw,id=disk0 -device \

usb-storage,port=1,drive=disk0 -qtest stdio
readl 0x3f980dfb
EOF

---
  hw/usb/hcd-dwc2.c | 9 +++++++--
  1 file changed, 7 insertions(+), 2 deletions(-)
hw/usb/hcd-dwc2.c +7 -2
index 8cac9c0a06..b4f0652c7d 100644
@@ -1128,7 +1128,10 @@ static uint64_t dwc2_hsotg_read(void *ptr, hwaddr addr, unsigned size)
          val = dwc2_pcgreg_read(ptr, addr, (addr - HSOTG_REG(0xe00)) >> 2, size);
          break;
      default:
-        g_assert_not_reached();
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%"HWADDR_PRIx"\n",
+                      __func__, addr);
+        val = 0;
+        break;
      }
 
      return val;
@@ -1160,7 +1163,9 @@ static void dwc2_hsotg_write(void *ptr, hwaddr addr, uint64_t val,
          dwc2_pcgreg_write(ptr, addr, (addr - HSOTG_REG(0xe00)) >> 2, val, size);
          break;
      default:
-        g_assert_not_reached();
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%"HWADDR_PRIx"\n",
+                      __func__, addr);
+        break;
      }
  }
 
Reviewed-by on v1 1/1
#2 lore raw
In reply to Zheyu Ma
Quoted 2 lines · Zheyu Ma

This commit modifies the dwc2_hsotg_read() and dwc2_hsotg_write()

functions

hw/usb/hcd-dwc2.c static uint64_t dwc2_hsotg_read(void *ptr, hwaddr

Quoted 26 lines

to handle invalid address access gracefully. Instead of using g_assert_not_reached(), which causes the program to abort, the functions now log an error message and return a default value for reads or do nothing for writes.

This change prevents the program from aborting and provides clear log messages indicating when an invalid memory address is accessed.

Reproducer:
cat << EOF | qemu-system-aarch64 -display none \

(quoted diff) +0 -2
-machine accel=qtest, -m 512M -machine raspi2b -m 1G -nodefaults \
-usb -drive file=null-co://,if=none,format=raw,id=disk0 -device \

usb-storage,port=1,drive=disk0 -qtest stdio
readl 0x3f980dfb
EOF

---
 hw/usb/hcd-dwc2.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
hw/usb/hcd-dwc2.c +0 -0
index 8cac9c0a06..b4f0652c7d 100644
@@ -1128,7 +1128,10 @@ static uint64_t dwc2_hsotg_read(void *ptr, hwaddr

addr, unsigned size)

hw/usb/hcd-dwc2.c static uint64_t dwc2_hsotg_read(void *ptr, hwaddr · line 1128

Quoted 1 line
hw/usb/hcd-dwc2.c +0 -0
         val = dwc2_pcgreg_read(ptr, addr, (addr - HSOTG_REG(0xe00)) >>

2, size);

hw/usb/hcd-dwc2.c static uint64_t dwc2_hsotg_read(void *ptr, hwaddr addr, unsigned size)

Quoted 4 lines
hw/usb/hcd-dwc2.c +1 -1
         break;
     default:
-        g_assert_not_reached();
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset

0x%"HWADDR_PRIx"\n",

hw/usb/hcd-dwc2.c static void dwc2_hsotg_write(void *ptr, hwaddr

Quoted 7 lines
hw/usb/hcd-dwc2.c +3 -0
+                      __func__, addr);
+        val = 0;
+        break;
     }
 
     return val;
@@ -1160,7 +1163,9 @@ static void dwc2_hsotg_write(void *ptr, hwaddr

addr, uint64_t val,

hw/usb/hcd-dwc2.c static void dwc2_hsotg_write(void *ptr, hwaddr · line 1163

Quoted 1 line
hw/usb/hcd-dwc2.c +0 -0
         dwc2_pcgreg_write(ptr, addr, (addr - HSOTG_REG(0xe00)) >> 2,

val, size);

hw/usb/hcd-dwc2.c static uint64_t dwc2_hsotg_read(void *ptr, hwaddr addr, unsigned size)

Quoted 4 lines
hw/usb/hcd-dwc2.c +1 -1
         break;
     default:
-        g_assert_not_reached();
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset

0x%"HWADDR_PRIx"\n",

Quoted 7 lines
hw/usb/hcd-dwc2.c +2 -1
+                      __func__, addr);
+        break;
     }
 }
 
--

2.34.1

Looks good to me.

#3 lore raw

Hi Paul,

Quoted 59 lines · Paul Zimmerman
Quoted 2 lines · On Tue, Jun 18, 2024 at 6:56 AM Zheyu Ma <[email protected]…

This commit modifies the dwc2_hsotg_read() and dwc2_hsotg_write()

functions

Quoted 16 lines

to handle invalid address access gracefully. Instead of using g_assert_not_reached(), which causes the program to abort, the functions now log an error message and return a default value for reads or do nothing for writes.

This change prevents the program from aborting and provides clear log messages indicating when an invalid memory address is accessed.

Reproducer:
cat << EOF | qemu-system-aarch64 -display none \

(quoted diff) +0 -2
-machine accel=qtest, -m 512M -machine raspi2b -m 1G -nodefaults \
-usb -drive file=null-co://,if=none,format=raw,id=disk0 -device \

usb-storage,port=1,drive=disk0 -qtest stdio
readl 0x3f980dfb
EOF

Quoted 1 line

<mailto:[email protected]>>

hw/usb/hcd-dwc2.c static uint64_t dwc2_hsotg_read(void *ptr,

Quoted 9 lines

---
 hw/usb/hcd-dwc2.c | 9 +++++++--

 1 file changed, 7 insertions(+), 2 deletions(-)
hw/usb/hcd-dwc2.c +0 -0
index 8cac9c0a06..b4f0652c7d 100644
@@ -1128,7 +1128,10 @@ static uint64_t dwc2_hsotg_read(void *ptr, 

hwaddr addr, unsigned size)

Quoted 6 lines
hw/usb/hcd-dwc2.c +0 -0
          val = dwc2_pcgreg_read(ptr, addr, (addr - HSOTG_REG(0xe00)) 
 > 2, size);
          break;

     default:
-        g_assert_not_reached();
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset

0x%"HWADDR_PRIx"\n",

hw/usb/hcd-dwc2.c static void dwc2_hsotg_write(void *ptr, hwaddr

Quoted 7 lines

+                      __func__, addr);
+        val = 0;
+        break;
     }

     return val;

hw/usb/hcd-dwc2.c +0 -0
@@ -1160,7 +1163,9 @@ static void dwc2_hsotg_write(void *ptr, hwaddr 

addr, uint64_t val,

hw/usb/hcd-dwc2.c static void dwc2_hsotg_write(void *ptr, hwaddr · line 1163

Quoted 1 line
hw/usb/hcd-dwc2.c +0 -0
          dwc2_pcgreg_write(ptr, addr, (addr - HSOTG_REG(0xe00)) >> 2, 

val, size);

Quoted 4 lines
hw/usb/hcd-dwc2.c +0 -0
          break;

     default:
-        g_assert_not_reached();
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset

0x%"HWADDR_PRIx"\n",

Quoted 5 lines

+                      __func__, addr);
+        break;
     }
 }

Quoted 7 lines
Quoted 2 lines

--
2.34.1

Looks good to me.

Reviewed-by Paul Zimmerman <[email protected] <mailto:[email protected]>>

Does that mean on real HW the access to unassigned registers are silently ignored as RAZ/WI like this patch? (I don't have access to the specs -- IIRC you don't neither, but you might have real HW to test).

Thanks,

Phil.

#4 lore raw
Quoted 69 lines · Philippe Mathieu-Daudé

Hi Paul,

Quoted 7 lines · Paul Zimmerman
Quoted 2 lines · On Tue, Jun 18, 2024 at 6:56 AM Zheyu Ma <[email protected]…

This commit modifies the dwc2_hsotg_read() and dwc2_hsotg_write()

functions

Quoted 2 lines

to handle invalid address access gracefully. Instead of using g_assert_not_reached(), which causes the program to abort, the

functions

Quoted 20 lines
Quoted 14 lines

now log an error message and return a default value for reads or do nothing for writes.

This change prevents the program from aborting and provides clear log messages indicating when an invalid memory address is accessed.

Reproducer:
cat << EOF | qemu-system-aarch64 -display none \

(quoted diff) +0 -2
-machine accel=qtest, -m 512M -machine raspi2b -m 1G -nodefaults \
-usb -drive file=null-co://,if=none,format=raw,id=disk0 -device \

usb-storage,port=1,drive=disk0 -qtest stdio
readl 0x3f980dfb
EOF

Quoted 1 line

<mailto:[email protected]>>

Quoted 4 lines
---
 hw/usb/hcd-dwc2.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
Quoted 5 lines

hw/usb/hcd-dwc2.c static uint64_t dwc2_hsotg_read(void *ptr,

Quoted 5 lines
hw/usb/hcd-dwc2.c +0 -0
index 8cac9c0a06..b4f0652c7d 100644
@@ -1128,7 +1128,10 @@ static uint64_t dwc2_hsotg_read(void *ptr,

hwaddr addr, unsigned size)

Quoted 6 lines

hw/usb/hcd-dwc2.c static uint64_t dwc2_hsotg_read(void *ptr, hwaddr addr, unsigned size)

Quoted 6 lines
hw/usb/hcd-dwc2.c +1 -1
         val = dwc2_pcgreg_read(ptr, addr, (addr - HSOTG_REG(0xe00))
 2, size);
         break;
     default:
-        g_assert_not_reached();
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset

0x%"HWADDR_PRIx"\n",

Quoted 7 lines

hw/usb/hcd-dwc2.c static void dwc2_hsotg_write(void *ptr, hwaddr

Quoted 7 lines
hw/usb/hcd-dwc2.c +3 -0
+                      __func__, addr);
+        val = 0;
+        break;
     }
 
     return val;
hw/usb/hcd-dwc2.c +0 -0
@@ -1160,7 +1163,9 @@ static void dwc2_hsotg_write(void *ptr, hwaddr

addr, uint64_t val,

Quoted 1 line

hw/usb/hcd-dwc2.c static void dwc2_hsotg_write(void *ptr, hwaddr · line 1163

Quoted 1 line
hw/usb/hcd-dwc2.c +0 -0
         dwc2_pcgreg_write(ptr, addr, (addr - HSOTG_REG(0xe00)) >> 2,

val, size);

Quoted 4 lines

hw/usb/hcd-dwc2.c static uint64_t dwc2_hsotg_read(void *ptr, hwaddr addr, unsigned size)

Quoted 4 lines
hw/usb/hcd-dwc2.c +1 -1
         break;
     default:
-        g_assert_not_reached();
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset

0x%"HWADDR_PRIx"\n",

Quoted 10 lines
Quoted 7 lines
hw/usb/hcd-dwc2.c +2 -1
+                      __func__, addr);
+        break;
     }
 }
 
--

2.34.1

Looks good to me.

Quoted 8 lines
Quoted 1 line

[email protected]>>

Quoted 1 line

Does that mean on real HW the access to unassigned registers are silently ignored as RAZ/WI like this patch? (I don't have access to the specs -- IIRC you don't neither, but you might have real HW to test).

Hi Phil,

I have an old raspi around somewhere I could probably dig up and test with, but I'm not familiar with qtest, so I don't know how I would reproduce the failure on real hw.

Besides, isn't it always better to fail and log an error than just crash?

Regards,
Paul

#5 lore raw
Quoted 73 lines · Paul Zimmerman
Quoted 63 lines · Philippe Mathieu-Daudé

Hi Paul,

Quoted 59 lines · Paul Zimmerman
Quoted 2 lines · On Tue, Jun 18, 2024 at 6:56 AM Zheyu Ma <[email protected]…

This commit modifies the dwc2_hsotg_read() and dwc2_hsotg_write()

functions

Quoted 16 lines

to handle invalid address access gracefully. Instead of using g_assert_not_reached(), which causes the program to abort, the functions now log an error message and return a default value for reads or do nothing for writes.

This change prevents the program from aborting and provides clear log messages indicating when an invalid memory address is accessed.

Reproducer:
cat << EOF | qemu-system-aarch64 -display none \

(quoted diff) +0 -2
-machine accel=qtest, -m 512M -machine raspi2b -m 1G -nodefaults \
-usb -drive file=null-co://,if=none,format=raw,id=disk0 -device \

usb-storage,port=1,drive=disk0 -qtest stdio
readl 0x3f980dfb
EOF

Quoted 1 line

<mailto:[email protected]>>

Quoted 32 lines
---
 hw/usb/hcd-dwc2.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
hw/usb/hcd-dwc2.c +7 -3
index 8cac9c0a06..b4f0652c7d 100644
@@ -1128,7 +1128,10 @@ static uint64_t dwc2_hsotg_read(void *ptr, hwaddr addr, unsigned size)
         val = dwc2_pcgreg_read(ptr, addr, (addr - HSOTG_REG(0xe00)) >> 2, size);
         break;
     default:
-        g_assert_not_reached();
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%"HWADDR_PRIx"\n",
+                      __func__, addr);
+        val = 0;
+        break;
     }
 
     return val;
@@ -1160,7 +1163,9 @@ static void dwc2_hsotg_write(void *ptr, hwaddr addr, uint64_t val,
         dwc2_pcgreg_write(ptr, addr, (addr - HSOTG_REG(0xe00)) >> 2, val, size);
         break;
     default:
-        g_assert_not_reached();
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%"HWADDR_PRIx"\n",
+                      __func__, addr);
+        break;
     }
 }
 
--
Quoted 1 line

2.34.1

Looks good to me.

Quoted 7 lines
Quoted 2 lines
Reviewed-by Paul Zimmerman <[email protected] <mailto:[email protected]>>

Does that mean on real HW the access to unassigned registers are silently ignored as RAZ/WI like this patch? (I don't have access to the specs -- IIRC you don't neither, but you might have real HW to test).

Quoted 5 lines

I have an old raspi around somewhere I could probably dig up and test with, but I'm not familiar with qtest, so I don't know how I would reproduce the failure on real hw.

Besides, isn't it always better to fail and log an error than just crash?

Yes, assert is definitely the wrong thing here. RAZ/WI and log a guest-error is what we typically do for devices where the spec doesn't give a behaviour for accesses to register offsets that aren't documented as having registers.

I've applied this to target-arm.next; thanks.

-- PMM