From cd29d6a7219a5ac62cbed46f14762d378664c1d7 Mon Sep 17 00:00:00 2001 From: Mark Zhuang Date: Mon, 14 Oct 2024 00:18:11 +0800 Subject: [PATCH] target/riscv: fix cross-page misaligned access when mmu not enabled When mmu is disabled, simply call the physical read/write function --- src/target/riscv/riscv.c | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/target/riscv/riscv.c b/src/target/riscv/riscv.c index 876835543..0354469fc 100644 --- a/src/target/riscv/riscv.c +++ b/src/target/riscv/riscv.c @@ -3061,19 +3061,13 @@ static int riscv_virt2phys(struct target *target, target_addr_t virtual, target_ virtual, physical); } -static int check_access_request(struct target *target, target_addr_t address, +static int check_virt_memory_access(struct target *target, target_addr_t address, uint32_t size, uint32_t count) { bool crosses_page_boundary = RISCV_PGBASE(address + size * count - 1) != RISCV_PGBASE(address); if (address % size && crosses_page_boundary) { - int mmu_enabled; - int result = riscv_mmu(target, &mmu_enabled); - if (result != ERROR_OK) - return result; - if (mmu_enabled) { - LOG_TARGET_ERROR(target, "Accessing an element across a page boundary is not supported."); - return ERROR_FAIL; - } + LOG_TARGET_ERROR(target, "Accessing an element across a page boundary is not supported."); + return ERROR_FAIL; } return ERROR_OK; } @@ -3093,11 +3087,20 @@ static int riscv_read_memory(struct target *target, target_addr_t address, return ERROR_OK; } - int result = check_access_request(target, address, size, count); + int mmu_enabled; + int result = riscv_mmu(target, &mmu_enabled); if (result != ERROR_OK) return result; RISCV_INFO(r); + + if (!mmu_enabled) + return r->read_memory(target, address, size, count, buffer, size); + + result = check_virt_memory_access(target, address, size, count); + if (result != ERROR_OK) + return result; + uint32_t current_count = 0; while (current_count < count) { target_addr_t physical_addr; @@ -3135,7 +3138,8 @@ static int riscv_write_memory(struct target *target, target_addr_t address, return ERROR_OK; } - int result = check_access_request(target, address, size, count); + int mmu_enabled; + int result = riscv_mmu(target, &mmu_enabled); if (result != ERROR_OK) return result; @@ -3143,6 +3147,13 @@ static int riscv_write_memory(struct target *target, target_addr_t address, if (!tt) return ERROR_FAIL; + if (!mmu_enabled) + return tt->write_memory(target, address, size, count, buffer); + + result = check_virt_memory_access(target, address, size, count); + if (result != ERROR_OK) + return result; + uint32_t current_count = 0; while (current_count < count) { target_addr_t physical_addr;