Skip to content

Commit

Permalink
target/riscv: fix read/write virtual memory across page boundaries
Browse files Browse the repository at this point in the history
When read/write virtual addresses cross page boundaries,
the physical addresses are not necessarily contiguous and
need to call virt2phys again.
  • Loading branch information
zqb-all committed Oct 13, 2024
1 parent a4020f1 commit 519ea7a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 12 deletions.
70 changes: 58 additions & 12 deletions src/target/riscv/riscv.c
Original file line number Diff line number Diff line change
Expand Up @@ -3061,6 +3061,23 @@ 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,
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;
}
}
return ERROR_OK;
}

static int riscv_read_phys_memory(struct target *target, target_addr_t phys_address,
uint32_t size, uint32_t count, uint8_t *buffer)
{
Expand All @@ -3076,15 +3093,29 @@ static int riscv_read_memory(struct target *target, target_addr_t address,
return ERROR_OK;
}

target_addr_t physical_addr;
int result = target->type->virt2phys(target, address, &physical_addr);
if (result != ERROR_OK) {
LOG_TARGET_ERROR(target, "Address translation failed.");
int result = check_access_request(target, address, size, count);
if (result != ERROR_OK)
return result;
}

RISCV_INFO(r);
return r->read_memory(target, physical_addr, size, count, buffer, size);
uint32_t current_count = 0;
while (current_count < count) {
target_addr_t physical_addr;
result = target->type->virt2phys(target, address, &physical_addr);
if (result != ERROR_OK) {
LOG_TARGET_ERROR(target, "Address translation failed.");
return result;
}

uint32_t chunk_count = MIN(count - current_count, (RISCV_PGSIZE - RISCV_PGOFFSET(address)) / size);
result = r->read_memory(target, physical_addr, size, chunk_count, buffer + current_count * size, size);
if (result != ERROR_OK)
return result;

current_count += chunk_count;
address += chunk_count * size;
}
return ERROR_OK;
}

static int riscv_write_phys_memory(struct target *target, target_addr_t phys_address,
Expand All @@ -3104,17 +3135,32 @@ static int riscv_write_memory(struct target *target, target_addr_t address,
return ERROR_OK;
}

target_addr_t physical_addr;
int result = target->type->virt2phys(target, address, &physical_addr);
if (result != ERROR_OK) {
LOG_TARGET_ERROR(target, "Address translation failed.");
int result = check_access_request(target, address, size, count);
if (result != ERROR_OK)
return result;
}

struct target_type *tt = get_target_type(target);
if (!tt)
return ERROR_FAIL;
return tt->write_memory(target, physical_addr, size, count, buffer);

uint32_t current_count = 0;
while (current_count < count) {
target_addr_t physical_addr;
result = target->type->virt2phys(target, address, &physical_addr);
if (result != ERROR_OK) {
LOG_TARGET_ERROR(target, "Address translation failed.");
return result;
}

uint32_t chunk_count = MIN(count - current_count, (RISCV_PGSIZE - RISCV_PGOFFSET(address)) / size);
result = tt->write_memory(target, physical_addr, size, chunk_count, buffer + current_count * size);
if (result != ERROR_OK)
return result;

current_count += chunk_count;
address += chunk_count * size;
}
return ERROR_OK;
}

static const char *riscv_get_gdb_arch(const struct target *target)
Expand Down
3 changes: 3 additions & 0 deletions src/target/riscv/riscv.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ struct riscv_program;
#define RISCV_HGATP_MODE(xlen) ((xlen) == 32 ? HGATP32_MODE : HGATP64_MODE)
#define RISCV_HGATP_PPN(xlen) ((xlen) == 32 ? HGATP32_PPN : HGATP64_PPN)
#define RISCV_PGSHIFT 12
#define RISCV_PGSIZE BIT(RISCV_PGSHIFT)
#define RISCV_PGBASE(addr) (addr & ~(RISCV_PGSIZE - 1))
#define RISCV_PGOFFSET(addr) (addr & (RISCV_PGSIZE - 1))

#define PG_MAX_LEVEL 5

Expand Down

0 comments on commit 519ea7a

Please sign in to comment.