From 10f1659f5ef048486acf8ec9925d2c2e2b87f1dd Mon Sep 17 00:00:00 2001 From: Farid Khaydari Date: Wed, 1 Oct 2025 04:15:33 +0300 Subject: [PATCH 1/2] target/riscv: fix bug in scratch_reserve This commit fixes comparison in scratch_reserve function Change-Id: If994b333282bf706736b953227a9739e52f08139 Signed-off-by: Farid Khaydari --- src/target/riscv/riscv-013.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/target/riscv/riscv-013.c b/src/target/riscv/riscv-013.c index 4fd041fbe..9b5ac1029 100644 --- a/src/target/riscv/riscv-013.c +++ b/src/target/riscv/riscv-013.c @@ -1208,7 +1208,7 @@ static int scratch_reserve(struct target *target, /* Align. */ scratch->hart_address = (scratch->hart_address + alignment - 1) & ~(alignment - 1); - if ((size_bytes + scratch->hart_address - info->dataaddr + 3) / 4 >= + if ((size_bytes + scratch->hart_address - info->dataaddr + 3) / 4 <= info->datasize) { scratch->memory_space = SPACE_DM_DATA; scratch->debug_address = (scratch->hart_address - info->dataaddr) / 4; @@ -1225,7 +1225,7 @@ static int scratch_reserve(struct target *target, scratch->hart_address = (info->progbuf_address + program_size + alignment - 1) & ~(alignment - 1); if ((info->progbuf_writable == YNM_YES) && - ((size_bytes + scratch->hart_address - info->progbuf_address + 3) / 4 >= + ((size_bytes + scratch->hart_address - info->progbuf_address + 3) / 4 <= info->progbufsize)) { scratch->memory_space = SPACE_DMI_PROGBUF; scratch->debug_address = (scratch->hart_address - info->progbuf_address) / 4; From 9d4c94e51fdaba3290a16825e48f393b9650a321 Mon Sep 17 00:00:00 2001 From: Farid Khaydari Date: Wed, 1 Oct 2025 13:14:30 +0300 Subject: [PATCH 2/2] target/riscv: refactor scratch_reserve() to use standardized macros for alignment and bit operations This commit refactors the scratch_reserve() - Replacing hardcoded bit manipulation with BIT() macro - Using GENMASK_ULL() instead of hardcoded masks for sign extension - Applying ALIGN_UP() macro for address alignment calculations - Using DIV_ROUND_UP() instead of manual division with addition Change-Id: Ic40923ef7d9ac5ca0ffb313c9d4fc6d1457d6bbb Signed-off-by: Farid Khaydari --- src/target/riscv/riscv-013.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/target/riscv/riscv-013.c b/src/target/riscv/riscv-013.c index 9b5ac1029..f2fb22f98 100644 --- a/src/target/riscv/riscv-013.c +++ b/src/target/riscv/riscv-013.c @@ -1203,12 +1203,13 @@ static int scratch_reserve(struct target *target, if (info->dataaccess == 1) { /* Sign extend dataaddr. */ scratch->hart_address = info->dataaddr; - if (info->dataaddr & (1<<11)) - scratch->hart_address |= 0xfffffffffffff000ULL; + if (info->dataaddr & BIT(DM_HARTINFO_DATAADDR_LENGTH - 1)) + scratch->hart_address |= + GENMASK_ULL(riscv_xlen(target) - 1, DM_HARTINFO_DATAADDR_LENGTH); /* Align. */ - scratch->hart_address = (scratch->hart_address + alignment - 1) & ~(alignment - 1); + scratch->hart_address = ALIGN_UP(scratch->hart_address, alignment); - if ((size_bytes + scratch->hart_address - info->dataaddr + 3) / 4 <= + if (DIV_ROUND_UP(size_bytes + scratch->hart_address - info->dataaddr, 4) <= info->datasize) { scratch->memory_space = SPACE_DM_DATA; scratch->debug_address = (scratch->hart_address - info->dataaddr) / 4; @@ -1222,10 +1223,9 @@ static int scratch_reserve(struct target *target, /* Allow for ebreak at the end of the program. */ unsigned int program_size = (program->instruction_count + 1) * 4; - scratch->hart_address = (info->progbuf_address + program_size + alignment - 1) & - ~(alignment - 1); + scratch->hart_address = ALIGN_UP(info->progbuf_address + program_size, alignment); if ((info->progbuf_writable == YNM_YES) && - ((size_bytes + scratch->hart_address - info->progbuf_address + 3) / 4 <= + (DIV_ROUND_UP(size_bytes + scratch->hart_address - info->progbuf_address, 4) <= info->progbufsize)) { scratch->memory_space = SPACE_DMI_PROGBUF; scratch->debug_address = (scratch->hart_address - info->progbuf_address) / 4; @@ -1235,8 +1235,7 @@ static int scratch_reserve(struct target *target, /* Option 3: User-configured memory area as scratch RAM */ if (target_alloc_working_area(target, size_bytes + alignment - 1, &scratch->area) == ERROR_OK) { - scratch->hart_address = (scratch->area->address + alignment - 1) & - ~(alignment - 1); + scratch->hart_address = ALIGN_UP(scratch->area->address, alignment); scratch->memory_space = SPACE_DMI_RAM; scratch->debug_address = scratch->hart_address; return ERROR_OK;