Skip to content

Commit eb01c63

Browse files
authored
Merge pull request #1295 from fk-sc/fk-sc/fix-scratch-reserve
Fix bug in scratch_reserve
2 parents 608ba43 + 9d4c94e commit eb01c63

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/target/riscv/riscv-013.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,12 +1203,13 @@ static int scratch_reserve(struct target *target,
12031203
if (info->dataaccess == 1) {
12041204
/* Sign extend dataaddr. */
12051205
scratch->hart_address = info->dataaddr;
1206-
if (info->dataaddr & (1<<11))
1207-
scratch->hart_address |= 0xfffffffffffff000ULL;
1206+
if (info->dataaddr & BIT(DM_HARTINFO_DATAADDR_LENGTH - 1))
1207+
scratch->hart_address |=
1208+
GENMASK_ULL(riscv_xlen(target) - 1, DM_HARTINFO_DATAADDR_LENGTH);
12081209
/* Align. */
1209-
scratch->hart_address = (scratch->hart_address + alignment - 1) & ~(alignment - 1);
1210+
scratch->hart_address = ALIGN_UP(scratch->hart_address, alignment);
12101211

1211-
if ((size_bytes + scratch->hart_address - info->dataaddr + 3) / 4 >=
1212+
if (DIV_ROUND_UP(size_bytes + scratch->hart_address - info->dataaddr, 4) <=
12121213
info->datasize) {
12131214
scratch->memory_space = SPACE_DM_DATA;
12141215
scratch->debug_address = (scratch->hart_address - info->dataaddr) / 4;
@@ -1222,10 +1223,9 @@ static int scratch_reserve(struct target *target,
12221223

12231224
/* Allow for ebreak at the end of the program. */
12241225
unsigned int program_size = (program->instruction_count + 1) * 4;
1225-
scratch->hart_address = (info->progbuf_address + program_size + alignment - 1) &
1226-
~(alignment - 1);
1226+
scratch->hart_address = ALIGN_UP(info->progbuf_address + program_size, alignment);
12271227
if ((info->progbuf_writable == YNM_YES) &&
1228-
((size_bytes + scratch->hart_address - info->progbuf_address + 3) / 4 >=
1228+
(DIV_ROUND_UP(size_bytes + scratch->hart_address - info->progbuf_address, 4) <=
12291229
info->progbufsize)) {
12301230
scratch->memory_space = SPACE_DMI_PROGBUF;
12311231
scratch->debug_address = (scratch->hart_address - info->progbuf_address) / 4;
@@ -1235,8 +1235,7 @@ static int scratch_reserve(struct target *target,
12351235
/* Option 3: User-configured memory area as scratch RAM */
12361236
if (target_alloc_working_area(target, size_bytes + alignment - 1,
12371237
&scratch->area) == ERROR_OK) {
1238-
scratch->hart_address = (scratch->area->address + alignment - 1) &
1239-
~(alignment - 1);
1238+
scratch->hart_address = ALIGN_UP(scratch->area->address, alignment);
12401239
scratch->memory_space = SPACE_DMI_RAM;
12411240
scratch->debug_address = scratch->hart_address;
12421241
return ERROR_OK;

0 commit comments

Comments
 (0)