qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] system/memory: use ldn_he_p/stn_he_p
@ 2023-11-16 16:36 Patrick Venture
  2023-11-17  8:18 ` Philippe Mathieu-Daudé
  2023-11-17  8:43 ` David Hildenbrand
  0 siblings, 2 replies; 6+ messages in thread
From: Patrick Venture @ 2023-11-16 16:36 UTC (permalink / raw)
  To: pbonzini, peterx, david, philmd, peter.maydell, richard.henderson
  Cc: qemu-devel, qemu-stable, Patrick Venture, Chris Rauer,
	Peter Foley

Using direct pointer dereferencing can allow for unaligned accesses,
which was seen during execution with sanitizers enabled.

Reviewed-by: Chris Rauer <crauer@google.com>
Reviewed-by: Peter Foley <pefoley@google.com>
Signed-off-by: Patrick Venture <venture@google.com>
Cc: qemu-stable@nongnu.org
---
v2: changed commit mesage to be more accurate and switched from using
memcpy to using the endian appropriate assignment load and store.
---
 system/memory.c | 32 ++------------------------------
 1 file changed, 2 insertions(+), 30 deletions(-)

diff --git a/system/memory.c b/system/memory.c
index 304fa843ea..affc7ea83c 100644
--- a/system/memory.c
+++ b/system/memory.c
@@ -1339,22 +1339,7 @@ static uint64_t memory_region_ram_device_read(void *opaque,
                                               hwaddr addr, unsigned size)
 {
     MemoryRegion *mr = opaque;
-    uint64_t data = (uint64_t)~0;
-
-    switch (size) {
-    case 1:
-        data = *(uint8_t *)(mr->ram_block->host + addr);
-        break;
-    case 2:
-        data = *(uint16_t *)(mr->ram_block->host + addr);
-        break;
-    case 4:
-        data = *(uint32_t *)(mr->ram_block->host + addr);
-        break;
-    case 8:
-        data = *(uint64_t *)(mr->ram_block->host + addr);
-        break;
-    }
+    uint64_t data = ldn_he_p(mr->ram_block->host + addr, size);
 
     trace_memory_region_ram_device_read(get_cpu_index(), mr, addr, data, size);
 
@@ -1368,20 +1353,7 @@ static void memory_region_ram_device_write(void *opaque, hwaddr addr,
 
     trace_memory_region_ram_device_write(get_cpu_index(), mr, addr, data, size);
 
-    switch (size) {
-    case 1:
-        *(uint8_t *)(mr->ram_block->host + addr) = (uint8_t)data;
-        break;
-    case 2:
-        *(uint16_t *)(mr->ram_block->host + addr) = (uint16_t)data;
-        break;
-    case 4:
-        *(uint32_t *)(mr->ram_block->host + addr) = (uint32_t)data;
-        break;
-    case 8:
-        *(uint64_t *)(mr->ram_block->host + addr) = data;
-        break;
-    }
+    stn_he_p(mr->ram_block->host + addr, size, data);
 }
 
 static const MemoryRegionOps ram_device_mem_ops = {
-- 
2.43.0.rc0.421.g78406f8d94-goog



^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-12-04 17:42 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-16 16:36 [PATCH v2] system/memory: use ldn_he_p/stn_he_p Patrick Venture
2023-11-17  8:18 ` Philippe Mathieu-Daudé
2023-11-17  8:43 ` David Hildenbrand
2023-12-03 15:42   ` Patrick Venture
2023-12-04 11:24     ` Philippe Mathieu-Daudé
2023-12-04 17:40       ` Patrick Venture

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).