Skip to contentSkip to conversation

system/memory: use ldn_he_p/stn_he_p

PATCH In review Patrick Venture 3 participants 6 messages Nov 16, 2023 – Dec 4, 2023

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 system/memory: use ldn_he_p/stn_he_p

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

---
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(-)
system/memory.c +2 -30
index 304fa843ea..affc7ea83c 100644
@@ -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 = {
Signature · 1 line
2.43.0.rc0.421.g78406f8d94-goog
Reviewed-by on v2 1/1
#1 lore raw
Quoted 7 lines · Patrick Venture

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

Quoted 6 lines

---
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(-)
Reviewed-by on v2 1/1
#2 lore raw
In reply to Patrick Venture
Quoted 7 lines · Patrick Venture

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

Signature · 3 lines
Cheers,

David / dhildenb
#3 lore raw
Quoted 11 lines · David Hildenbrand
Quoted 7 lines · Patrick Venture

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

Quoted 7 lines

--
Cheers,

David / dhildenb

Friendly ping? Is this going to be applied or do I need to add another CC or? I do think it should go into stable.

#4 lore raw

Hi Patrick,

Quoted 1 line · Patrick Venture

Friendly ping? Is this going to be applied or do I need to add another CC or?  I do think it should go into stable.

I'll send a PR with this patch included.

Regards,

Phil.

#5 lore raw
Quoted 9 lines · Philippe Mathieu-Daudé

Hi Patrick,

Quoted 2 lines · Patrick Venture

Friendly ping? Is this going to be applied or do I need to add another CC or? I do think it should go into stable.

I'll send a PR with this patch included.

Thanks!

Quoted 5 lines

Regards,

Phil.