Skip to content

Commit 3a139b8

Browse files
committed
Fix data types around batch.{c,h}
Check that the value of dtmcs.abits is in the expected range. Make corrections of data types in batch.{c,h} and in related code. Some of the issues were found by activating "-Wconversion" in GCC, others by inspecting the code manually. This is an initial step towards being able to use "-Wconversion" on RISC-V target code, which will give us bit more confidence when refactoring or merging new patches. Changes made: - Check `dtmcs.abits` during examination. - DMI address is no larger than 32-bits per the debug spec. Changed address parameters of multiple functions from uint64_t to uint32_t. - The value passed to jtag_add_runtest() is now `unsigned int`, not `int`. No need for `assert(idle <= INT_MAX)` anymore. - `get_delay()` in batch.c can return an unsigned value. - Added few assertions around `field->num_bits` in batch.c. Signed-off-by: Jan Matyas <[email protected]>
1 parent f82c5a7 commit 3a139b8

File tree

5 files changed

+76
-40
lines changed

5 files changed

+76
-40
lines changed

src/target/riscv/batch.c

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
#include "riscv.h"
1111
#include "field_helpers.h"
1212

13+
// TODO: DTM_DMI_MAX_ADDRESS_LENGTH should be reduced to 32 (per the debug spec)
1314
#define DTM_DMI_MAX_ADDRESS_LENGTH ((1<<DTM_DTMCS_ABITS_LENGTH)-1)
1415
#define DMI_SCAN_MAX_BIT_LENGTH (DTM_DMI_MAX_ADDRESS_LENGTH + DTM_DMI_DATA_LENGTH + DTM_DMI_OP_LENGTH)
16+
1517
#define DMI_SCAN_BUF_SIZE (DIV_ROUND_UP(DMI_SCAN_MAX_BIT_LENGTH, 8))
1618

1719
/* Reserve extra room in the batch (needed for the last NOP operation) */
@@ -127,11 +129,10 @@ static void add_idle_before_batch(const struct riscv_batch *batch, size_t start_
127129
const unsigned int idle_change = new_delay - batch->last_scan_delay;
128130
LOG_TARGET_DEBUG(batch->target, "Adding %u idle cycles before the batch.",
129131
idle_change);
130-
assert(idle_change <= INT_MAX);
131132
jtag_add_runtest(idle_change, TAP_IDLE);
132133
}
133134

134-
static int get_delay(const struct riscv_batch *batch, size_t scan_idx,
135+
static unsigned int get_delay(const struct riscv_batch *batch, size_t scan_idx,
135136
const struct riscv_scan_delays *delays, bool resets_delays,
136137
size_t reset_delays_after)
137138
{
@@ -142,7 +143,6 @@ static int get_delay(const struct riscv_batch *batch, size_t scan_idx,
142143
const enum riscv_scan_delay_class delay_class =
143144
batch->delay_classes[scan_idx];
144145
const unsigned int delay = riscv_scan_get_delay(delays, delay_class);
145-
assert(delay <= INT_MAX);
146146
return delays_were_reset ? 0 : delay;
147147
}
148148

@@ -199,9 +199,10 @@ static void log_batch(const struct riscv_batch *batch, size_t start_idx,
199199
return;
200200

201201
const unsigned int scan_bits = batch->fields->num_bits;
202-
assert(scan_bits == (unsigned int)riscv_get_dmi_scan_length(batch->target));
203-
const unsigned int abits = scan_bits - DTM_DMI_OP_LENGTH
204-
- DTM_DMI_DATA_LENGTH;
202+
assert(scan_bits == riscv_get_dmi_scan_length(batch->target));
203+
assert(scan_bits <= DMI_SCAN_MAX_BIT_LENGTH);
204+
const unsigned int abits = (unsigned int)(scan_bits - DTM_DMI_OP_LENGTH
205+
- DTM_DMI_DATA_LENGTH);
205206

206207
/* Determine the "op" and "address" of the scan that preceded the first
207208
* executed scan.
@@ -211,7 +212,7 @@ static void log_batch(const struct riscv_batch *batch, size_t start_idx,
211212
* would be a more robust solution.
212213
*/
213214
bool last_scan_was_read = false;
214-
uint32_t last_scan_address = -1 /* to silence maybe-uninitialized */;
215+
uint32_t last_scan_address = (uint32_t)(-1) /* to silence maybe-uninitialized */;
215216
if (start_idx > 0) {
216217
const struct scan_field * const field = &batch->fields[start_idx - 1];
217218
assert(field->out_value);
@@ -224,7 +225,7 @@ static void log_batch(const struct riscv_batch *batch, size_t start_idx,
224225
/* Decode and log every executed scan */
225226
for (size_t i = start_idx; i < batch->used_scans; ++i) {
226227
static const char * const op_string[] = {"-", "r", "w", "?"};
227-
const int delay = get_delay(batch, i, delays, resets_delays,
228+
const unsigned int delay = get_delay(batch, i, delays, resets_delays,
228229
reset_delays_after);
229230
const struct scan_field * const field = &batch->fields[i];
230231

@@ -247,15 +248,15 @@ static void log_batch(const struct riscv_batch *batch, size_t start_idx,
247248
DTM_DMI_ADDRESS_OFFSET, abits);
248249

249250
LOG_DEBUG("%db %s %08" PRIx32 " @%02" PRIx32
250-
" -> %s %08" PRIx32 " @%02" PRIx32 "; %di",
251+
" -> %s %08" PRIx32 " @%02" PRIx32 "; %ui",
251252
field->num_bits, op_string[out_op], out_data, out_address,
252253
status_string[in_op], in_data, in_address, delay);
253254

254255
if (last_scan_was_read && in_op == DTM_DMI_OP_SUCCESS)
255256
log_dmi_decoded(batch, /*write*/ false,
256257
last_scan_address, in_data);
257258
} else {
258-
LOG_DEBUG("%db %s %08" PRIx32 " @%02" PRIx32 " -> ?; %di",
259+
LOG_DEBUG("%db %s %08" PRIx32 " @%02" PRIx32 " -> ?; %ui",
259260
field->num_bits, op_string[out_op], out_data, out_address,
260261
delay);
261262
}
@@ -321,12 +322,16 @@ int riscv_batch_run_from(struct riscv_batch *batch, size_t start_idx,
321322
return ERROR_OK;
322323
}
323324

324-
void riscv_batch_add_dmi_write(struct riscv_batch *batch, uint64_t address, uint32_t data,
325+
void riscv_batch_add_dmi_write(struct riscv_batch *batch, uint32_t address, uint32_t data,
325326
bool read_back, enum riscv_scan_delay_class delay_class)
326327
{
328+
// TODO: Check that the bit width of "address" is no more than dtmcs.abits,
329+
// otherwise return an error (during batch creation or when the batch is executed).
330+
327331
assert(batch->used_scans < batch->allocated_scans);
328332
struct scan_field *field = batch->fields + batch->used_scans;
329333
field->num_bits = riscv_get_dmi_scan_length(batch->target);
334+
assert(field->num_bits <= DMI_SCAN_MAX_BIT_LENGTH);
330335
field->out_value = (void *)(batch->data_out + batch->used_scans * DMI_SCAN_BUF_SIZE);
331336
riscv_fill_dmi_write(batch->target, (char *)field->out_value, address, data);
332337
if (read_back) {
@@ -340,12 +345,16 @@ void riscv_batch_add_dmi_write(struct riscv_batch *batch, uint64_t address, uint
340345
batch->used_scans++;
341346
}
342347

343-
size_t riscv_batch_add_dmi_read(struct riscv_batch *batch, uint64_t address,
348+
size_t riscv_batch_add_dmi_read(struct riscv_batch *batch, uint32_t address,
344349
enum riscv_scan_delay_class delay_class)
345350
{
351+
// TODO: Check that the bit width of "address" is no more than dtmcs.abits,
352+
// otherwise return an error (during batch creation or when the batch is executed).
353+
346354
assert(batch->used_scans < batch->allocated_scans);
347355
struct scan_field *field = batch->fields + batch->used_scans;
348356
field->num_bits = riscv_get_dmi_scan_length(batch->target);
357+
assert(field->num_bits <= DMI_SCAN_MAX_BIT_LENGTH);
349358
field->out_value = (void *)(batch->data_out + batch->used_scans * DMI_SCAN_BUF_SIZE);
350359
field->in_value = (void *)(batch->data_in + batch->used_scans * DMI_SCAN_BUF_SIZE);
351360
riscv_fill_dmi_read(batch->target, (char *)field->out_value, address);
@@ -383,6 +392,7 @@ void riscv_batch_add_nop(struct riscv_batch *batch)
383392
assert(batch->used_scans < batch->allocated_scans);
384393
struct scan_field *field = batch->fields + batch->used_scans;
385394
field->num_bits = riscv_get_dmi_scan_length(batch->target);
395+
assert(field->num_bits <= DMI_SCAN_MAX_BIT_LENGTH);
386396
field->out_value = (void *)(batch->data_out + batch->used_scans * DMI_SCAN_BUF_SIZE);
387397
field->in_value = (void *)(batch->data_in + batch->used_scans * DMI_SCAN_BUF_SIZE);
388398
riscv_fill_dm_nop(batch->target, (char *)field->out_value);

src/target/riscv/batch.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,11 @@ int riscv_batch_run_from(struct riscv_batch *batch, size_t start_idx,
190190
size_t riscv_batch_finished_scans(const struct riscv_batch *batch);
191191

192192
/* Adds a DM register write to this batch. */
193-
void riscv_batch_add_dmi_write(struct riscv_batch *batch, uint64_t address, uint32_t data,
193+
void riscv_batch_add_dmi_write(struct riscv_batch *batch, uint32_t address, uint32_t data,
194194
bool read_back, enum riscv_scan_delay_class delay_class);
195195

196196
static inline void
197-
riscv_batch_add_dm_write(struct riscv_batch *batch, uint64_t address, uint32_t data,
197+
riscv_batch_add_dm_write(struct riscv_batch *batch, uint32_t address, uint32_t data,
198198
bool read_back, enum riscv_scan_delay_class delay_type)
199199
{
200200
return riscv_batch_add_dmi_write(batch,
@@ -205,11 +205,11 @@ riscv_batch_add_dm_write(struct riscv_batch *batch, uint64_t address, uint32_t d
205205
/* DM register reads must be handled in two parts: the first one schedules a read and
206206
* provides a key, the second one actually obtains the result of the read -
207207
* status (op) and the actual data. */
208-
size_t riscv_batch_add_dmi_read(struct riscv_batch *batch, uint64_t address,
208+
size_t riscv_batch_add_dmi_read(struct riscv_batch *batch, uint32_t address,
209209
enum riscv_scan_delay_class delay_class);
210210

211211
static inline size_t
212-
riscv_batch_add_dm_read(struct riscv_batch *batch, uint64_t address,
212+
riscv_batch_add_dm_read(struct riscv_batch *batch, uint32_t address,
213213
enum riscv_scan_delay_class delay_type)
214214
{
215215
return riscv_batch_add_dmi_read(batch,

src/target/riscv/riscv-013.c

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ static riscv_insn_t riscv013_read_progbuf(struct target *target, unsigned int
5656
index);
5757
static int riscv013_invalidate_cached_progbuf(struct target *target);
5858
static int riscv013_execute_progbuf(struct target *target, uint32_t *cmderr);
59-
static void riscv013_fill_dmi_write(struct target *target, char *buf, uint64_t a, uint32_t d);
60-
static void riscv013_fill_dmi_read(struct target *target, char *buf, uint64_t a);
61-
static int riscv013_get_dmi_scan_length(struct target *target);
59+
static void riscv013_fill_dmi_write(struct target *target, char *buf, uint32_t a, uint32_t d);
60+
static void riscv013_fill_dmi_read(struct target *target, char *buf, uint32_t a);
61+
static unsigned int riscv013_get_dmi_scan_length(struct target *target);
6262
static void riscv013_fill_dm_nop(struct target *target, char *buf);
6363
static unsigned int register_size(struct target *target, enum gdb_regno number);
6464
static int register_read_direct(struct target *target, riscv_reg_t *value,
@@ -1941,6 +1941,29 @@ static int examine(struct target *target)
19411941
info->abits = get_field(dtmcontrol, DTM_DTMCS_ABITS);
19421942
info->dtmcs_idle = get_field(dtmcontrol, DTM_DTMCS_IDLE);
19431943

1944+
if (info->abits > RISCV013_DTMCS_ABITS_MAX) {
1945+
/* Max. address width given by the debug specification is exceeded */
1946+
LOG_TARGET_ERROR(target, "The target's debug bus (DMI) address width exceeds "
1947+
"the maximum:");
1948+
LOG_TARGET_ERROR(target, " found dtmcs.abits = %d; maximum is abits = %d.",
1949+
info->abits, RISCV013_DTMCS_ABITS_MAX);
1950+
return ERROR_FAIL;
1951+
}
1952+
1953+
if (info->abits < RISCV013_DTMCS_ABITS_MIN) {
1954+
/* The requirement for minimum DMI address width of 7 bits is part of
1955+
* the RISC-V Debug spec since Jan-20-2017 (commit 03df6ee7). However,
1956+
* implementations exist that implement narrower DMI address. For example
1957+
* Spike as of Q1/2025 uses dmi.abits = 6.
1958+
*
1959+
* For that reason, warn the user but continue.
1960+
*/
1961+
LOG_TARGET_WARNING(target, "The target's debug bus (DMI) address width is "
1962+
"lower than the minimum:");
1963+
LOG_TARGET_WARNING(target, " found dtmcs.abits = %d; minimum is abits = %d.",
1964+
info->abits, RISCV013_DTMCS_ABITS_MIN);
1965+
}
1966+
19441967
if (!check_dbgbase_exists(target)) {
19451968
LOG_TARGET_ERROR(target, "Could not find debug module with DMI base address (dbgbase) = 0x%x", target->dbgbase);
19461969
return ERROR_FAIL;
@@ -5390,31 +5413,31 @@ static int riscv013_execute_progbuf(struct target *target, uint32_t *cmderr)
53905413
return riscv013_execute_abstract_command(target, run_program, cmderr);
53915414
}
53925415

5393-
static void riscv013_fill_dmi_write(struct target *target, char *buf, uint64_t a, uint32_t d)
5416+
static void riscv013_fill_dmi_write(struct target *target, char *buf, uint32_t a, uint32_t d)
53945417
{
53955418
RISCV013_INFO(info);
5396-
buf_set_u64((unsigned char *)buf, DTM_DMI_OP_OFFSET, DTM_DMI_OP_LENGTH, DMI_OP_WRITE);
5397-
buf_set_u64((unsigned char *)buf, DTM_DMI_DATA_OFFSET, DTM_DMI_DATA_LENGTH, d);
5398-
buf_set_u64((unsigned char *)buf, DTM_DMI_ADDRESS_OFFSET, info->abits, a);
5419+
buf_set_u32((unsigned char *)buf, DTM_DMI_OP_OFFSET, DTM_DMI_OP_LENGTH, DMI_OP_WRITE);
5420+
buf_set_u32((unsigned char *)buf, DTM_DMI_DATA_OFFSET, DTM_DMI_DATA_LENGTH, d);
5421+
buf_set_u32((unsigned char *)buf, DTM_DMI_ADDRESS_OFFSET, info->abits, a);
53995422
}
54005423

5401-
static void riscv013_fill_dmi_read(struct target *target, char *buf, uint64_t a)
5424+
static void riscv013_fill_dmi_read(struct target *target, char *buf, uint32_t a)
54025425
{
54035426
RISCV013_INFO(info);
5404-
buf_set_u64((unsigned char *)buf, DTM_DMI_OP_OFFSET, DTM_DMI_OP_LENGTH, DMI_OP_READ);
5405-
buf_set_u64((unsigned char *)buf, DTM_DMI_DATA_OFFSET, DTM_DMI_DATA_LENGTH, 0);
5406-
buf_set_u64((unsigned char *)buf, DTM_DMI_ADDRESS_OFFSET, info->abits, a);
5427+
buf_set_u32((unsigned char *)buf, DTM_DMI_OP_OFFSET, DTM_DMI_OP_LENGTH, DMI_OP_READ);
5428+
buf_set_u32((unsigned char *)buf, DTM_DMI_DATA_OFFSET, DTM_DMI_DATA_LENGTH, 0);
5429+
buf_set_u32((unsigned char *)buf, DTM_DMI_ADDRESS_OFFSET, info->abits, a);
54075430
}
54085431

54095432
static void riscv013_fill_dm_nop(struct target *target, char *buf)
54105433
{
54115434
RISCV013_INFO(info);
5412-
buf_set_u64((unsigned char *)buf, DTM_DMI_OP_OFFSET, DTM_DMI_OP_LENGTH, DMI_OP_NOP);
5413-
buf_set_u64((unsigned char *)buf, DTM_DMI_DATA_OFFSET, DTM_DMI_DATA_LENGTH, 0);
5414-
buf_set_u64((unsigned char *)buf, DTM_DMI_ADDRESS_OFFSET, info->abits, 0);
5435+
buf_set_u32((unsigned char *)buf, DTM_DMI_OP_OFFSET, DTM_DMI_OP_LENGTH, DMI_OP_NOP);
5436+
buf_set_u32((unsigned char *)buf, DTM_DMI_DATA_OFFSET, DTM_DMI_DATA_LENGTH, 0);
5437+
buf_set_u32((unsigned char *)buf, DTM_DMI_ADDRESS_OFFSET, info->abits, 0);
54155438
}
54165439

5417-
static int riscv013_get_dmi_scan_length(struct target *target)
5440+
static unsigned int riscv013_get_dmi_scan_length(struct target *target)
54185441
{
54195442
RISCV013_INFO(info);
54205443
return info->abits + DTM_DMI_DATA_LENGTH + DTM_DMI_OP_LENGTH;

src/target/riscv/riscv.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5890,13 +5890,13 @@ int riscv_execute_progbuf(struct target *target, uint32_t *cmderr)
58905890
return r->execute_progbuf(target, cmderr);
58915891
}
58925892

5893-
void riscv_fill_dmi_write(struct target *target, char *buf, uint64_t a, uint32_t d)
5893+
void riscv_fill_dmi_write(struct target *target, char *buf, uint32_t a, uint32_t d)
58945894
{
58955895
RISCV_INFO(r);
58965896
r->fill_dmi_write(target, buf, a, d);
58975897
}
58985898

5899-
void riscv_fill_dmi_read(struct target *target, char *buf, uint64_t a)
5899+
void riscv_fill_dmi_read(struct target *target, char *buf, uint32_t a)
59005900
{
59015901
RISCV_INFO(r);
59025902
r->fill_dmi_read(target, buf, a);
@@ -5908,7 +5908,7 @@ void riscv_fill_dm_nop(struct target *target, char *buf)
59085908
r->fill_dm_nop(target, buf);
59095909
}
59105910

5911-
int riscv_get_dmi_scan_length(struct target *target)
5911+
unsigned int riscv_get_dmi_scan_length(struct target *target)
59125912
{
59135913
RISCV_INFO(r);
59145914
return r->get_dmi_scan_length(target);

src/target/riscv/riscv.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ typedef struct {
125125
#define DTM_DTMCS_VERSION_UNKNOWN ((unsigned int)-1)
126126
#define RISCV_TINFO_VERSION_UNKNOWN (-1)
127127

128+
#define RISCV013_DTMCS_ABITS_MIN 7
129+
#define RISCV013_DTMCS_ABITS_MAX 32
130+
128131
struct reg_name_table {
129132
unsigned int num_entries;
130133
char **reg_names;
@@ -275,9 +278,9 @@ struct riscv_info {
275278
riscv_insn_t (*read_progbuf)(struct target *target, unsigned int index);
276279
int (*execute_progbuf)(struct target *target, uint32_t *cmderr);
277280
int (*invalidate_cached_progbuf)(struct target *target);
278-
int (*get_dmi_scan_length)(struct target *target);
279-
void (*fill_dmi_write)(struct target *target, char *buf, uint64_t a, uint32_t d);
280-
void (*fill_dmi_read)(struct target *target, char *buf, uint64_t a);
281+
unsigned int (*get_dmi_scan_length)(struct target *target);
282+
void (*fill_dmi_write)(struct target *target, char *buf, uint32_t a, uint32_t d);
283+
void (*fill_dmi_read)(struct target *target, char *buf, uint32_t a);
281284
void (*fill_dm_nop)(struct target *target, char *buf);
282285

283286
int (*authdata_read)(struct target *target, uint32_t *value, unsigned int index);
@@ -463,9 +466,9 @@ int riscv_write_progbuf(struct target *target, int index, riscv_insn_t insn);
463466
int riscv_execute_progbuf(struct target *target, uint32_t *cmderr);
464467

465468
void riscv_fill_dm_nop(struct target *target, char *buf);
466-
void riscv_fill_dmi_write(struct target *target, char *buf, uint64_t a, uint32_t d);
467-
void riscv_fill_dmi_read(struct target *target, char *buf, uint64_t a);
468-
int riscv_get_dmi_scan_length(struct target *target);
469+
void riscv_fill_dmi_write(struct target *target, char *buf, uint32_t a, uint32_t d);
470+
void riscv_fill_dmi_read(struct target *target, char *buf, uint32_t a);
471+
unsigned int riscv_get_dmi_scan_length(struct target *target);
469472

470473
uint32_t riscv_get_dmi_address(const struct target *target, uint32_t dm_address);
471474

0 commit comments

Comments
 (0)