arm64/mem: Refactor guest memory access and made it endian aware

Refactors the core guest memory access subsystem (guest.h) to be safer
and portable accross host systems with different endianness. The
previous implementation used direct pointer casting, which is not endian
safe.

1. All read and write functions have been converted from unsafe pointer
   casts to memcpy(). This resolves alignment warning -Wcast-align.

2. The access functions no longer rely on asserts for error checking.
   They now perform explicit boundary and alignment checking and returns
   a guest_mem_access_result_t status code.

3. A new header (endian.h) provides cross platform byte swapping macros.
   The memory access functions use these macros to ensure that the guest
   always sees memory in the correct endian format, regardless of the
   host's native byte order. The host endianness is now automatically
   detected via CMake.

3. Asserts are now explicitly enabled in release builds to catch
   critical errors.

Signed-off-by: Ronald Caesar <github43132@proton.me>
This commit is contained in:
Ronald Caesar 2025-09-13 22:47:01 -04:00
parent 768355712d
commit 8b483849f4
9 changed files with 467 additions and 218 deletions

View file

@ -2,6 +2,7 @@
cmake_minimum_required(VERSION 3.22)
set(CMAKE_C_STANDARD 17)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_C_STANDARD_REQUIRED TRUE)
@ -12,6 +13,8 @@ if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
endif()
# Enable asserts in release mode.
string( REPLACE "/DNDEBUG" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
# Optimizations
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE ON)
@ -22,6 +25,7 @@ endif()
project(Pound)
find_package(fmt 10.2.1 CONFIG)
find_package(SDL3 3.2.10 CONFIG)
add_subdirectory(3rd_Party)
@ -38,6 +42,24 @@ add_subdirectory(src/frontend)
add_subdirectory(src/host)
add_subdirectory(src/targets/switch1/hardware)
# Detect Host System Endianness.
include(TestBigEndian)
TEST_BIG_ENDIAN(WORDS_BIGENDIAN)
if(WORDS_BIGENDIAN)
message(STATUS "Host Endianness: Big Endian")
else()
message(STATUS "Host Endianness: Little Endian")
endif()
# Configure Preprocessor Definitions based on Endianness.
if(WORDS_BIGENDIAN)
target_compile_definitions(Pound PRIVATE HOST_IS_BIG_ENDIAN=1)
target_compile_definitions(Pound PRIVATE HOST_IS_LITTLE_ENDIAN=0)
else()
target_compile_definitions(Pound PRIVATE HOST_IS_BIG_ENDIAN=0)
target_compile_definitions(Pound PRIVATE HOST_IS_LITTLE_ENDIAN=1)
endif()
target_compile_options(Pound PRIVATE -Wall -Wpedantic
-Wshadow
-Wpointer-arith