1 /* SPDX-License-Identifier: GPL-2.0-or-later */
11 #include <helper/log.h>
12 #include <helper/time_support.h>
13 #include "target/target.h"
14 #include "target/algorithm.h"
15 #include "target/target_type.h"
16 #include "jtag/jtag.h"
17 #include "target/register.h"
18 #include "target/breakpoints.h"
21 #include "rtos/rtos.h"
22 #include "debug_defines.h"
23 #include <helper/bits.h>
25 #define get_field(reg, mask) (((reg) & (mask)) / ((mask) & ~((mask) << 1)))
26 #define set_field(reg, mask, val) (((reg) & ~(mask)) | (((val) * ((mask) & ~((mask) << 1))) & (mask)))
28 /* Constants for legacy SiFive hardware breakpoints. */
29 #define CSR_BPCONTROL_X (1<<0)
30 #define CSR_BPCONTROL_W (1<<1)
31 #define CSR_BPCONTROL_R (1<<2)
32 #define CSR_BPCONTROL_U (1<<3)
33 #define CSR_BPCONTROL_S (1<<4)
34 #define CSR_BPCONTROL_H (1<<5)
35 #define CSR_BPCONTROL_M (1<<6)
36 #define CSR_BPCONTROL_BPMATCH (0xf<<7)
37 #define CSR_BPCONTROL_BPACTION (0xff<<11)
39 #define DEBUG_ROM_START 0x800
40 #define DEBUG_ROM_RESUME (DEBUG_ROM_START + 4)
41 #define DEBUG_ROM_EXCEPTION (DEBUG_ROM_START + 8)
42 #define DEBUG_RAM_START 0x400
44 #define SETHALTNOT 0x10c
46 /*** JTAG registers. ***/
48 #define DTMCONTROL 0x10
49 #define DTMCONTROL_DBUS_RESET (1<<16)
50 #define DTMCONTROL_IDLE (7<<10)
51 #define DTMCONTROL_ADDRBITS (0xf<<4)
52 #define DTMCONTROL_VERSION (0xf)
55 #define DBUS_OP_START 0
56 #define DBUS_OP_SIZE 2
63 DBUS_STATUS_SUCCESS
= 0,
64 DBUS_STATUS_FAILED
= 2,
67 #define DBUS_DATA_START 2
68 #define DBUS_DATA_SIZE 34
69 #define DBUS_ADDRESS_START 36
77 /*** Debug Bus registers. ***/
79 #define DMCONTROL 0x10
80 #define DMCONTROL_INTERRUPT (((uint64_t)1)<<33)
81 #define DMCONTROL_HALTNOT (((uint64_t)1)<<32)
82 #define DMCONTROL_BUSERROR (7<<19)
83 #define DMCONTROL_SERIAL (3<<16)
84 #define DMCONTROL_AUTOINCREMENT (1<<15)
85 #define DMCONTROL_ACCESS (7<<12)
86 #define DMCONTROL_HARTID (0x3ff<<2)
87 #define DMCONTROL_NDRESET (1<<1)
88 #define DMCONTROL_FULLRESET 1
91 #define DMINFO_ABUSSIZE (0x7fU<<25)
92 #define DMINFO_SERIALCOUNT (0xf<<21)
93 #define DMINFO_ACCESS128 (1<<20)
94 #define DMINFO_ACCESS64 (1<<19)
95 #define DMINFO_ACCESS32 (1<<18)
96 #define DMINFO_ACCESS16 (1<<17)
97 #define DMINFO_ACCESS8 (1<<16)
98 #define DMINFO_DRAMSIZE (0x3f<<10)
99 #define DMINFO_AUTHENTICATED (1<<5)
100 #define DMINFO_AUTHBUSY (1<<4)
101 #define DMINFO_AUTHTYPE (3<<2)
102 #define DMINFO_VERSION 3
104 /*** Info about the core being debugged. ***/
106 #define DBUS_ADDRESS_UNKNOWN 0xffff
109 #define DRAM_CACHE_SIZE 16
111 uint8_t ir_dtmcontrol
[4] = {DTMCONTROL
};
112 struct scan_field select_dtmcontrol
= {
114 .out_value
= ir_dtmcontrol
116 uint8_t ir_dbus
[4] = {DBUS
};
117 struct scan_field select_dbus
= {
121 uint8_t ir_idcode
[4] = {0x1};
122 struct scan_field select_idcode
= {
124 .out_value
= ir_idcode
127 bscan_tunnel_type_t bscan_tunnel_type
;
128 int bscan_tunnel_ir_width
; /* if zero, then tunneling is not present/active */
130 static const uint8_t bscan_zero
[4] = {0};
131 static const uint8_t bscan_one
[4] = {1};
134 struct scan_field select_user4
= {
136 .out_value
= ir_user4
140 uint8_t bscan_tunneled_ir_width
[4] = {5}; /* overridden by assignment in riscv_init_target */
141 struct scan_field _bscan_tunnel_data_register_select_dmi
[] = {
144 .out_value
= bscan_zero
,
148 .num_bits
= 5, /* initialized in riscv_init_target to ir width of DM */
149 .out_value
= ir_dbus
,
154 .out_value
= bscan_tunneled_ir_width
,
159 .out_value
= bscan_zero
,
164 struct scan_field _bscan_tunnel_nested_tap_select_dmi
[] = {
167 .out_value
= bscan_zero
,
172 .out_value
= bscan_tunneled_ir_width
,
176 .num_bits
= 0, /* initialized in riscv_init_target to ir width of DM */
177 .out_value
= ir_dbus
,
182 .out_value
= bscan_zero
,
186 struct scan_field
*bscan_tunnel_nested_tap_select_dmi
= _bscan_tunnel_nested_tap_select_dmi
;
187 uint32_t bscan_tunnel_nested_tap_select_dmi_num_fields
= ARRAY_SIZE(_bscan_tunnel_nested_tap_select_dmi
);
189 struct scan_field
*bscan_tunnel_data_register_select_dmi
= _bscan_tunnel_data_register_select_dmi
;
190 uint32_t bscan_tunnel_data_register_select_dmi_num_fields
= ARRAY_SIZE(_bscan_tunnel_data_register_select_dmi
);
197 bool read
, write
, execute
;
201 /* Wall-clock timeout for a command/access. Settable via RISC-V Target commands.*/
202 int riscv_command_timeout_sec
= DEFAULT_COMMAND_TIMEOUT_SEC
;
204 /* Wall-clock timeout after reset. Settable via RISC-V Target commands.*/
205 int riscv_reset_timeout_sec
= DEFAULT_RESET_TIMEOUT_SEC
;
207 bool riscv_enable_virt2phys
= true;
208 bool riscv_ebreakm
= true;
209 bool riscv_ebreaks
= true;
210 bool riscv_ebreaku
= true;
212 bool riscv_enable_virtual
;
219 const virt2phys_info_t sv32
= {
224 .vpn_shift
= {12, 22},
225 .vpn_mask
= {0x3ff, 0x3ff},
226 .pte_ppn_shift
= {10, 20},
227 .pte_ppn_mask
= {0x3ff, 0xfff},
228 .pa_ppn_shift
= {12, 22},
229 .pa_ppn_mask
= {0x3ff, 0xfff},
232 const virt2phys_info_t sv39
= {
237 .vpn_shift
= {12, 21, 30},
238 .vpn_mask
= {0x1ff, 0x1ff, 0x1ff},
239 .pte_ppn_shift
= {10, 19, 28},
240 .pte_ppn_mask
= {0x1ff, 0x1ff, 0x3ffffff},
241 .pa_ppn_shift
= {12, 21, 30},
242 .pa_ppn_mask
= {0x1ff, 0x1ff, 0x3ffffff},
245 const virt2phys_info_t sv48
= {
250 .vpn_shift
= {12, 21, 30, 39},
251 .vpn_mask
= {0x1ff, 0x1ff, 0x1ff, 0x1ff},
252 .pte_ppn_shift
= {10, 19, 28, 37},
253 .pte_ppn_mask
= {0x1ff, 0x1ff, 0x1ff, 0x1ffff},
254 .pa_ppn_shift
= {12, 21, 30, 39},
255 .pa_ppn_mask
= {0x1ff, 0x1ff, 0x1ff, 0x1ffff},
258 void riscv_sample_buf_maybe_add_timestamp(struct target
*target
, bool before
)
261 uint32_t now
= timeval_ms() & 0xffffffff;
262 if (r
->sample_buf
.used
+ 5 < r
->sample_buf
.size
) {
264 r
->sample_buf
.buf
[r
->sample_buf
.used
++] = RISCV_SAMPLE_BUF_TIMESTAMP_BEFORE
;
266 r
->sample_buf
.buf
[r
->sample_buf
.used
++] = RISCV_SAMPLE_BUF_TIMESTAMP_AFTER
;
267 r
->sample_buf
.buf
[r
->sample_buf
.used
++] = now
& 0xff;
268 r
->sample_buf
.buf
[r
->sample_buf
.used
++] = (now
>> 8) & 0xff;
269 r
->sample_buf
.buf
[r
->sample_buf
.used
++] = (now
>> 16) & 0xff;
270 r
->sample_buf
.buf
[r
->sample_buf
.used
++] = (now
>> 24) & 0xff;
274 static int riscv_resume_go_all_harts(struct target
*target
);
276 void select_dmi_via_bscan(struct target
*target
)
278 jtag_add_ir_scan(target
->tap
, &select_user4
, TAP_IDLE
);
279 if (bscan_tunnel_type
== BSCAN_TUNNEL_DATA_REGISTER
)
280 jtag_add_dr_scan(target
->tap
, bscan_tunnel_data_register_select_dmi_num_fields
,
281 bscan_tunnel_data_register_select_dmi
, TAP_IDLE
);
282 else /* BSCAN_TUNNEL_NESTED_TAP */
283 jtag_add_dr_scan(target
->tap
, bscan_tunnel_nested_tap_select_dmi_num_fields
,
284 bscan_tunnel_nested_tap_select_dmi
, TAP_IDLE
);
287 uint32_t dtmcontrol_scan_via_bscan(struct target
*target
, uint32_t out
)
289 /* On BSCAN TAP: Select IR=USER4, issue tunneled IR scan via BSCAN TAP's DR */
290 uint8_t tunneled_ir_width
[4] = {bscan_tunnel_ir_width
};
291 uint8_t tunneled_dr_width
[4] = {32};
292 uint8_t out_value
[5] = {0};
293 uint8_t in_value
[5] = {0};
295 buf_set_u32(out_value
, 0, 32, out
);
296 struct scan_field tunneled_ir
[4] = {};
297 struct scan_field tunneled_dr
[4] = {};
299 if (bscan_tunnel_type
== BSCAN_TUNNEL_DATA_REGISTER
) {
300 tunneled_ir
[0].num_bits
= 3;
301 tunneled_ir
[0].out_value
= bscan_zero
;
302 tunneled_ir
[0].in_value
= NULL
;
303 tunneled_ir
[1].num_bits
= bscan_tunnel_ir_width
;
304 tunneled_ir
[1].out_value
= ir_dtmcontrol
;
305 tunneled_ir
[1].in_value
= NULL
;
306 tunneled_ir
[2].num_bits
= 7;
307 tunneled_ir
[2].out_value
= tunneled_ir_width
;
308 tunneled_ir
[2].in_value
= NULL
;
309 tunneled_ir
[3].num_bits
= 1;
310 tunneled_ir
[3].out_value
= bscan_zero
;
311 tunneled_ir
[3].in_value
= NULL
;
313 tunneled_dr
[0].num_bits
= 3;
314 tunneled_dr
[0].out_value
= bscan_zero
;
315 tunneled_dr
[0].in_value
= NULL
;
316 tunneled_dr
[1].num_bits
= 32 + 1;
317 tunneled_dr
[1].out_value
= out_value
;
318 tunneled_dr
[1].in_value
= in_value
;
319 tunneled_dr
[2].num_bits
= 7;
320 tunneled_dr
[2].out_value
= tunneled_dr_width
;
321 tunneled_dr
[2].in_value
= NULL
;
322 tunneled_dr
[3].num_bits
= 1;
323 tunneled_dr
[3].out_value
= bscan_one
;
324 tunneled_dr
[3].in_value
= NULL
;
326 /* BSCAN_TUNNEL_NESTED_TAP */
327 tunneled_ir
[3].num_bits
= 3;
328 tunneled_ir
[3].out_value
= bscan_zero
;
329 tunneled_ir
[3].in_value
= NULL
;
330 tunneled_ir
[2].num_bits
= bscan_tunnel_ir_width
;
331 tunneled_ir
[2].out_value
= ir_dtmcontrol
;
332 tunneled_ir
[1].in_value
= NULL
;
333 tunneled_ir
[1].num_bits
= 7;
334 tunneled_ir
[1].out_value
= tunneled_ir_width
;
335 tunneled_ir
[2].in_value
= NULL
;
336 tunneled_ir
[0].num_bits
= 1;
337 tunneled_ir
[0].out_value
= bscan_zero
;
338 tunneled_ir
[0].in_value
= NULL
;
340 tunneled_dr
[3].num_bits
= 3;
341 tunneled_dr
[3].out_value
= bscan_zero
;
342 tunneled_dr
[3].in_value
= NULL
;
343 tunneled_dr
[2].num_bits
= 32 + 1;
344 tunneled_dr
[2].out_value
= out_value
;
345 tunneled_dr
[2].in_value
= in_value
;
346 tunneled_dr
[1].num_bits
= 7;
347 tunneled_dr
[1].out_value
= tunneled_dr_width
;
348 tunneled_dr
[1].in_value
= NULL
;
349 tunneled_dr
[0].num_bits
= 1;
350 tunneled_dr
[0].out_value
= bscan_one
;
351 tunneled_dr
[0].in_value
= NULL
;
353 jtag_add_ir_scan(target
->tap
, &select_user4
, TAP_IDLE
);
354 jtag_add_dr_scan(target
->tap
, ARRAY_SIZE(tunneled_ir
), tunneled_ir
, TAP_IDLE
);
355 jtag_add_dr_scan(target
->tap
, ARRAY_SIZE(tunneled_dr
), tunneled_dr
, TAP_IDLE
);
356 select_dmi_via_bscan(target
);
358 int retval
= jtag_execute_queue();
359 if (retval
!= ERROR_OK
) {
360 LOG_ERROR("failed jtag scan: %d", retval
);
363 /* Note the starting offset is bit 1, not bit 0. In BSCAN tunnel, there is a one-bit TCK skew between
365 uint32_t in
= buf_get_u32(in_value
, 1, 32);
366 LOG_DEBUG("DTMCS: 0x%x -> 0x%x", out
, in
);
371 static uint32_t dtmcontrol_scan(struct target
*target
, uint32_t out
)
373 struct scan_field field
;
375 uint8_t out_value
[4] = { 0 };
377 if (bscan_tunnel_ir_width
!= 0)
378 return dtmcontrol_scan_via_bscan(target
, out
);
381 buf_set_u32(out_value
, 0, 32, out
);
383 jtag_add_ir_scan(target
->tap
, &select_dtmcontrol
, TAP_IDLE
);
386 field
.out_value
= out_value
;
387 field
.in_value
= in_value
;
388 jtag_add_dr_scan(target
->tap
, 1, &field
, TAP_IDLE
);
390 /* Always return to dbus. */
391 jtag_add_ir_scan(target
->tap
, &select_dbus
, TAP_IDLE
);
393 int retval
= jtag_execute_queue();
394 if (retval
!= ERROR_OK
) {
395 LOG_ERROR("failed jtag scan: %d", retval
);
399 uint32_t in
= buf_get_u32(field
.in_value
, 0, 32);
400 LOG_DEBUG("DTMCONTROL: 0x%x -> 0x%x", out
, in
);
405 static struct target_type
*get_target_type(struct target
*target
)
407 riscv_info_t
*info
= (riscv_info_t
*) target
->arch_info
;
410 LOG_ERROR("Target has not been initialized");
414 switch (info
->dtm_version
) {
416 return &riscv011_target
;
418 return &riscv013_target
;
420 LOG_ERROR("Unsupported DTM version: %d", info
->dtm_version
);
425 static int riscv_create_target(struct target
*target
, Jim_Interp
*interp
)
427 LOG_DEBUG("riscv_create_target()");
428 target
->arch_info
= calloc(1, sizeof(riscv_info_t
));
429 if (!target
->arch_info
) {
430 LOG_ERROR("Failed to allocate RISC-V target structure.");
433 riscv_info_init(target
, target
->arch_info
);
437 static int riscv_init_target(struct command_context
*cmd_ctx
,
438 struct target
*target
)
440 LOG_DEBUG("riscv_init_target()");
442 info
->cmd_ctx
= cmd_ctx
;
444 select_dtmcontrol
.num_bits
= target
->tap
->ir_length
;
445 select_dbus
.num_bits
= target
->tap
->ir_length
;
446 select_idcode
.num_bits
= target
->tap
->ir_length
;
448 if (bscan_tunnel_ir_width
!= 0) {
449 assert(target
->tap
->ir_length
>= 6);
450 uint32_t ir_user4_raw
= 0x23 << (target
->tap
->ir_length
- 6);
451 ir_user4
[0] = (uint8_t)ir_user4_raw
;
452 ir_user4
[1] = (uint8_t)(ir_user4_raw
>>= 8);
453 ir_user4
[2] = (uint8_t)(ir_user4_raw
>>= 8);
454 ir_user4
[3] = (uint8_t)(ir_user4_raw
>>= 8);
455 select_user4
.num_bits
= target
->tap
->ir_length
;
456 bscan_tunneled_ir_width
[0] = bscan_tunnel_ir_width
;
457 if (bscan_tunnel_type
== BSCAN_TUNNEL_DATA_REGISTER
)
458 bscan_tunnel_data_register_select_dmi
[1].num_bits
= bscan_tunnel_ir_width
;
459 else /* BSCAN_TUNNEL_NESTED_TAP */
460 bscan_tunnel_nested_tap_select_dmi
[2].num_bits
= bscan_tunnel_ir_width
;
463 riscv_semihosting_init(target
);
465 target
->debug_reason
= DBG_REASON_DBGRQ
;
470 static void riscv_free_registers(struct target
*target
)
472 /* Free the shared structure use for most registers. */
473 if (target
->reg_cache
) {
474 if (target
->reg_cache
->reg_list
) {
475 free(target
->reg_cache
->reg_list
[0].arch_info
);
476 /* Free the ones we allocated separately. */
477 for (unsigned i
= GDB_REGNO_COUNT
; i
< target
->reg_cache
->num_regs
; i
++)
478 free(target
->reg_cache
->reg_list
[i
].arch_info
);
479 free(target
->reg_cache
->reg_list
);
481 free(target
->reg_cache
);
485 static void riscv_deinit_target(struct target
*target
)
487 LOG_DEBUG("riscv_deinit_target()");
489 riscv_info_t
*info
= target
->arch_info
;
490 struct target_type
*tt
= get_target_type(target
);
492 if (tt
&& info
->version_specific
)
493 tt
->deinit_target(target
);
495 riscv_free_registers(target
);
497 range_list_t
*entry
, *tmp
;
498 list_for_each_entry_safe(entry
, tmp
, &info
->expose_csr
, list
) {
503 list_for_each_entry_safe(entry
, tmp
, &info
->expose_custom
, list
) {
508 free(info
->reg_names
);
509 free(target
->arch_info
);
511 target
->arch_info
= NULL
;
514 static void trigger_from_breakpoint(struct trigger
*trigger
,
515 const struct breakpoint
*breakpoint
)
517 trigger
->address
= breakpoint
->address
;
518 trigger
->length
= breakpoint
->length
;
519 trigger
->mask
= ~0LL;
520 trigger
->read
= false;
521 trigger
->write
= false;
522 trigger
->execute
= true;
523 /* unique_id is unique across both breakpoints and watchpoints. */
524 trigger
->unique_id
= breakpoint
->unique_id
;
527 static int maybe_add_trigger_t1(struct target
*target
,
528 struct trigger
*trigger
, uint64_t tdata1
)
532 const uint32_t bpcontrol_x
= 1<<0;
533 const uint32_t bpcontrol_w
= 1<<1;
534 const uint32_t bpcontrol_r
= 1<<2;
535 const uint32_t bpcontrol_u
= 1<<3;
536 const uint32_t bpcontrol_s
= 1<<4;
537 const uint32_t bpcontrol_h
= 1<<5;
538 const uint32_t bpcontrol_m
= 1<<6;
539 const uint32_t bpcontrol_bpmatch
= 0xf << 7;
540 const uint32_t bpcontrol_bpaction
= 0xff << 11;
542 if (tdata1
& (bpcontrol_r
| bpcontrol_w
| bpcontrol_x
)) {
543 /* Trigger is already in use, presumably by user code. */
544 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
547 tdata1
= set_field(tdata1
, bpcontrol_r
, trigger
->read
);
548 tdata1
= set_field(tdata1
, bpcontrol_w
, trigger
->write
);
549 tdata1
= set_field(tdata1
, bpcontrol_x
, trigger
->execute
);
550 tdata1
= set_field(tdata1
, bpcontrol_u
,
551 !!(r
->misa
& BIT('U' - 'A')));
552 tdata1
= set_field(tdata1
, bpcontrol_s
,
553 !!(r
->misa
& BIT('S' - 'A')));
554 tdata1
= set_field(tdata1
, bpcontrol_h
,
555 !!(r
->misa
& BIT('H' - 'A')));
556 tdata1
|= bpcontrol_m
;
557 tdata1
= set_field(tdata1
, bpcontrol_bpmatch
, 0); /* exact match */
558 tdata1
= set_field(tdata1
, bpcontrol_bpaction
, 0); /* cause bp exception */
560 riscv_set_register(target
, GDB_REGNO_TDATA1
, tdata1
);
562 riscv_reg_t tdata1_rb
;
563 if (riscv_get_register(target
, &tdata1_rb
, GDB_REGNO_TDATA1
) != ERROR_OK
)
565 LOG_DEBUG("tdata1=0x%" PRIx64
, tdata1_rb
);
567 if (tdata1
!= tdata1_rb
) {
568 LOG_DEBUG("Trigger doesn't support what we need; After writing 0x%"
569 PRIx64
" to tdata1 it contains 0x%" PRIx64
,
571 riscv_set_register(target
, GDB_REGNO_TDATA1
, 0);
572 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
575 riscv_set_register(target
, GDB_REGNO_TDATA2
, trigger
->address
);
580 static int maybe_add_trigger_t2(struct target
*target
,
581 struct trigger
*trigger
, uint64_t tdata1
)
585 /* tselect is already set */
586 if (tdata1
& (MCONTROL_EXECUTE
| MCONTROL_STORE
| MCONTROL_LOAD
)) {
587 /* Trigger is already in use, presumably by user code. */
588 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
591 /* address/data match trigger */
592 tdata1
|= MCONTROL_DMODE(riscv_xlen(target
));
593 tdata1
= set_field(tdata1
, MCONTROL_ACTION
,
594 MCONTROL_ACTION_DEBUG_MODE
);
595 tdata1
= set_field(tdata1
, MCONTROL_MATCH
, MCONTROL_MATCH_EQUAL
);
596 tdata1
|= MCONTROL_M
;
597 if (r
->misa
& (1 << ('S' - 'A')))
598 tdata1
|= MCONTROL_S
;
599 if (r
->misa
& (1 << ('U' - 'A')))
600 tdata1
|= MCONTROL_U
;
602 if (trigger
->execute
)
603 tdata1
|= MCONTROL_EXECUTE
;
605 tdata1
|= MCONTROL_LOAD
;
607 tdata1
|= MCONTROL_STORE
;
609 riscv_set_register(target
, GDB_REGNO_TDATA1
, tdata1
);
612 int result
= riscv_get_register(target
, &tdata1_rb
, GDB_REGNO_TDATA1
);
613 if (result
!= ERROR_OK
)
615 LOG_DEBUG("tdata1=0x%" PRIx64
, tdata1_rb
);
617 if (tdata1
!= tdata1_rb
) {
618 LOG_DEBUG("Trigger doesn't support what we need; After writing 0x%"
619 PRIx64
" to tdata1 it contains 0x%" PRIx64
,
621 riscv_set_register(target
, GDB_REGNO_TDATA1
, 0);
622 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
625 riscv_set_register(target
, GDB_REGNO_TDATA2
, trigger
->address
);
630 static int maybe_add_trigger_t6(struct target
*target
,
631 struct trigger
*trigger
, uint64_t tdata1
)
635 /* tselect is already set */
636 if (tdata1
& (CSR_MCONTROL6_EXECUTE
| CSR_MCONTROL6_STORE
| CSR_MCONTROL6_LOAD
)) {
637 /* Trigger is already in use, presumably by user code. */
638 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
641 /* address/data match trigger */
642 tdata1
|= MCONTROL_DMODE(riscv_xlen(target
));
643 tdata1
= set_field(tdata1
, CSR_MCONTROL6_ACTION
,
644 MCONTROL_ACTION_DEBUG_MODE
);
645 tdata1
= set_field(tdata1
, CSR_MCONTROL6_MATCH
, MCONTROL_MATCH_EQUAL
);
646 tdata1
|= CSR_MCONTROL6_M
;
647 if (r
->misa
& (1 << ('H' - 'A')))
648 tdata1
|= CSR_MCONTROL6_VS
| CSR_MCONTROL6_VU
;
649 if (r
->misa
& (1 << ('S' - 'A')))
650 tdata1
|= CSR_MCONTROL6_S
;
651 if (r
->misa
& (1 << ('U' - 'A')))
652 tdata1
|= CSR_MCONTROL6_U
;
654 if (trigger
->execute
)
655 tdata1
|= CSR_MCONTROL6_EXECUTE
;
657 tdata1
|= CSR_MCONTROL6_LOAD
;
659 tdata1
|= CSR_MCONTROL6_STORE
;
661 riscv_set_register(target
, GDB_REGNO_TDATA1
, tdata1
);
664 int result
= riscv_get_register(target
, &tdata1_rb
, GDB_REGNO_TDATA1
);
665 if (result
!= ERROR_OK
)
667 LOG_DEBUG("tdata1=0x%" PRIx64
, tdata1_rb
);
669 if (tdata1
!= tdata1_rb
) {
670 LOG_DEBUG("Trigger doesn't support what we need; After writing 0x%"
671 PRIx64
" to tdata1 it contains 0x%" PRIx64
,
673 riscv_set_register(target
, GDB_REGNO_TDATA1
, 0);
674 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
677 riscv_set_register(target
, GDB_REGNO_TDATA2
, trigger
->address
);
682 static int add_trigger(struct target
*target
, struct trigger
*trigger
)
686 if (riscv_enumerate_triggers(target
) != ERROR_OK
)
690 if (riscv_get_register(target
, &tselect
, GDB_REGNO_TSELECT
) != ERROR_OK
)
694 for (i
= 0; i
< r
->trigger_count
; i
++) {
695 if (r
->trigger_unique_id
[i
] != -1)
698 riscv_set_register(target
, GDB_REGNO_TSELECT
, i
);
701 int result
= riscv_get_register(target
, &tdata1
, GDB_REGNO_TDATA1
);
702 if (result
!= ERROR_OK
)
704 int type
= get_field(tdata1
, MCONTROL_TYPE(riscv_xlen(target
)));
709 result
= maybe_add_trigger_t1(target
, trigger
, tdata1
);
712 result
= maybe_add_trigger_t2(target
, trigger
, tdata1
);
715 result
= maybe_add_trigger_t6(target
, trigger
, tdata1
);
718 LOG_DEBUG("trigger %d has unknown type %d", i
, type
);
722 if (result
!= ERROR_OK
)
725 LOG_DEBUG("[%d] Using trigger %d (type %d) for bp %d", target
->coreid
,
726 i
, type
, trigger
->unique_id
);
727 r
->trigger_unique_id
[i
] = trigger
->unique_id
;
731 riscv_set_register(target
, GDB_REGNO_TSELECT
, tselect
);
733 if (i
>= r
->trigger_count
) {
734 LOG_ERROR("Couldn't find an available hardware trigger.");
735 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
742 * Write one memory item of given "size". Use memory access of given "access_size".
743 * Utilize read-modify-write, if needed.
745 static int write_by_given_size(struct target
*target
, target_addr_t address
,
746 uint32_t size
, uint8_t *buffer
, uint32_t access_size
)
748 assert(size
== 1 || size
== 2 || size
== 4 || size
== 8);
749 assert(access_size
== 1 || access_size
== 2 || access_size
== 4 || access_size
== 8);
751 if (access_size
<= size
&& address
% access_size
== 0)
752 /* Can do the memory access directly without a helper buffer. */
753 return target_write_memory(target
, address
, access_size
, size
/ access_size
, buffer
);
755 unsigned int offset_head
= address
% access_size
;
756 unsigned int n_blocks
= ((size
+ offset_head
) <= access_size
) ? 1 : 2;
757 uint8_t helper_buf
[n_blocks
* access_size
];
759 /* Read from memory */
760 if (target_read_memory(target
, address
- offset_head
, access_size
, n_blocks
, helper_buf
) != ERROR_OK
)
763 /* Modify and write back */
764 memcpy(helper_buf
+ offset_head
, buffer
, size
);
765 return target_write_memory(target
, address
- offset_head
, access_size
, n_blocks
, helper_buf
);
769 * Read one memory item of given "size". Use memory access of given "access_size".
770 * Read larger section of memory and pick out the required portion, if needed.
772 static int read_by_given_size(struct target
*target
, target_addr_t address
,
773 uint32_t size
, uint8_t *buffer
, uint32_t access_size
)
775 assert(size
== 1 || size
== 2 || size
== 4 || size
== 8);
776 assert(access_size
== 1 || access_size
== 2 || access_size
== 4 || access_size
== 8);
778 if (access_size
<= size
&& address
% access_size
== 0)
779 /* Can do the memory access directly without a helper buffer. */
780 return target_read_memory(target
, address
, access_size
, size
/ access_size
, buffer
);
782 unsigned int offset_head
= address
% access_size
;
783 unsigned int n_blocks
= ((size
+ offset_head
) <= access_size
) ? 1 : 2;
784 uint8_t helper_buf
[n_blocks
* access_size
];
786 /* Read from memory */
787 if (target_read_memory(target
, address
- offset_head
, access_size
, n_blocks
, helper_buf
) != ERROR_OK
)
790 /* Pick the requested portion from the buffer */
791 memcpy(buffer
, helper_buf
+ offset_head
, size
);
796 * Write one memory item using any memory access size that will work.
797 * Utilize read-modify-write, if needed.
799 int riscv_write_by_any_size(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
801 assert(size
== 1 || size
== 2 || size
== 4 || size
== 8);
803 /* Find access size that correspond to data size and the alignment. */
804 unsigned int preferred_size
= size
;
805 while (address
% preferred_size
!= 0)
808 /* First try the preferred (most natural) access size. */
809 if (write_by_given_size(target
, address
, size
, buffer
, preferred_size
) == ERROR_OK
)
812 /* On failure, try other access sizes.
813 Minimize the number of accesses by trying first the largest size. */
814 for (unsigned int access_size
= 8; access_size
> 0; access_size
/= 2) {
815 if (access_size
== preferred_size
)
816 /* Already tried this size. */
819 if (write_by_given_size(target
, address
, size
, buffer
, access_size
) == ERROR_OK
)
823 /* No access attempt succeeded. */
828 * Read one memory item using any memory access size that will work.
829 * Read larger section of memory and pick out the required portion, if needed.
831 int riscv_read_by_any_size(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
833 assert(size
== 1 || size
== 2 || size
== 4 || size
== 8);
835 /* Find access size that correspond to data size and the alignment. */
836 unsigned int preferred_size
= size
;
837 while (address
% preferred_size
!= 0)
840 /* First try the preferred (most natural) access size. */
841 if (read_by_given_size(target
, address
, size
, buffer
, preferred_size
) == ERROR_OK
)
844 /* On failure, try other access sizes.
845 Minimize the number of accesses by trying first the largest size. */
846 for (unsigned int access_size
= 8; access_size
> 0; access_size
/= 2) {
847 if (access_size
== preferred_size
)
848 /* Already tried this size. */
851 if (read_by_given_size(target
, address
, size
, buffer
, access_size
) == ERROR_OK
)
855 /* No access attempt succeeded. */
859 int riscv_add_breakpoint(struct target
*target
, struct breakpoint
*breakpoint
)
861 LOG_DEBUG("[%d] @0x%" TARGET_PRIxADDR
, target
->coreid
, breakpoint
->address
);
863 if (breakpoint
->type
== BKPT_SOFT
) {
864 /** @todo check RVC for size/alignment */
865 if (!(breakpoint
->length
== 4 || breakpoint
->length
== 2)) {
866 LOG_ERROR("Invalid breakpoint length %d", breakpoint
->length
);
870 if (0 != (breakpoint
->address
% 2)) {
871 LOG_ERROR("Invalid breakpoint alignment for address 0x%" TARGET_PRIxADDR
, breakpoint
->address
);
875 /* Read the original instruction. */
876 if (riscv_read_by_any_size(
877 target
, breakpoint
->address
, breakpoint
->length
, breakpoint
->orig_instr
) != ERROR_OK
) {
878 LOG_ERROR("Failed to read original instruction at 0x%" TARGET_PRIxADDR
,
879 breakpoint
->address
);
883 uint8_t buff
[4] = { 0 };
884 buf_set_u32(buff
, 0, breakpoint
->length
* CHAR_BIT
, breakpoint
->length
== 4 ? ebreak() : ebreak_c());
885 /* Write the ebreak instruction. */
886 if (riscv_write_by_any_size(target
, breakpoint
->address
, breakpoint
->length
, buff
) != ERROR_OK
) {
887 LOG_ERROR("Failed to write %d-byte breakpoint instruction at 0x%"
888 TARGET_PRIxADDR
, breakpoint
->length
, breakpoint
->address
);
892 } else if (breakpoint
->type
== BKPT_HARD
) {
893 struct trigger trigger
;
894 trigger_from_breakpoint(&trigger
, breakpoint
);
895 int const result
= add_trigger(target
, &trigger
);
896 if (result
!= ERROR_OK
)
899 LOG_INFO("OpenOCD only supports hardware and software breakpoints.");
900 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
903 breakpoint
->set
= true;
907 static int remove_trigger(struct target
*target
, struct trigger
*trigger
)
911 if (riscv_enumerate_triggers(target
) != ERROR_OK
)
915 for (i
= 0; i
< r
->trigger_count
; i
++) {
916 if (r
->trigger_unique_id
[i
] == trigger
->unique_id
)
919 if (i
>= r
->trigger_count
) {
920 LOG_ERROR("Couldn't find the hardware resources used by hardware "
924 LOG_DEBUG("[%d] Stop using resource %d for bp %d", target
->coreid
, i
,
928 int result
= riscv_get_register(target
, &tselect
, GDB_REGNO_TSELECT
);
929 if (result
!= ERROR_OK
)
931 riscv_set_register(target
, GDB_REGNO_TSELECT
, i
);
932 riscv_set_register(target
, GDB_REGNO_TDATA1
, 0);
933 riscv_set_register(target
, GDB_REGNO_TSELECT
, tselect
);
934 r
->trigger_unique_id
[i
] = -1;
939 int riscv_remove_breakpoint(struct target
*target
,
940 struct breakpoint
*breakpoint
)
942 if (breakpoint
->type
== BKPT_SOFT
) {
943 /* Write the original instruction. */
944 if (riscv_write_by_any_size(
945 target
, breakpoint
->address
, breakpoint
->length
, breakpoint
->orig_instr
) != ERROR_OK
) {
946 LOG_ERROR("Failed to restore instruction for %d-byte breakpoint at "
947 "0x%" TARGET_PRIxADDR
, breakpoint
->length
, breakpoint
->address
);
951 } else if (breakpoint
->type
== BKPT_HARD
) {
952 struct trigger trigger
;
953 trigger_from_breakpoint(&trigger
, breakpoint
);
954 int result
= remove_trigger(target
, &trigger
);
955 if (result
!= ERROR_OK
)
959 LOG_INFO("OpenOCD only supports hardware and software breakpoints.");
960 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
963 breakpoint
->set
= false;
968 static void trigger_from_watchpoint(struct trigger
*trigger
,
969 const struct watchpoint
*watchpoint
)
971 trigger
->address
= watchpoint
->address
;
972 trigger
->length
= watchpoint
->length
;
973 trigger
->mask
= watchpoint
->mask
;
974 trigger
->value
= watchpoint
->value
;
975 trigger
->read
= (watchpoint
->rw
== WPT_READ
|| watchpoint
->rw
== WPT_ACCESS
);
976 trigger
->write
= (watchpoint
->rw
== WPT_WRITE
|| watchpoint
->rw
== WPT_ACCESS
);
977 trigger
->execute
= false;
978 /* unique_id is unique across both breakpoints and watchpoints. */
979 trigger
->unique_id
= watchpoint
->unique_id
;
982 int riscv_add_watchpoint(struct target
*target
, struct watchpoint
*watchpoint
)
984 struct trigger trigger
;
985 trigger_from_watchpoint(&trigger
, watchpoint
);
987 int result
= add_trigger(target
, &trigger
);
988 if (result
!= ERROR_OK
)
990 watchpoint
->set
= true;
995 int riscv_remove_watchpoint(struct target
*target
,
996 struct watchpoint
*watchpoint
)
998 LOG_DEBUG("[%d] @0x%" TARGET_PRIxADDR
, target
->coreid
, watchpoint
->address
);
1000 struct trigger trigger
;
1001 trigger_from_watchpoint(&trigger
, watchpoint
);
1003 int result
= remove_trigger(target
, &trigger
);
1004 if (result
!= ERROR_OK
)
1006 watchpoint
->set
= false;
1011 /* Sets *hit_watchpoint to the first watchpoint identified as causing the
1014 * The GDB server uses this information to tell GDB what data address has
1015 * been hit, which enables GDB to print the hit variable along with its old
1017 int riscv_hit_watchpoint(struct target
*target
, struct watchpoint
**hit_watchpoint
)
1019 struct watchpoint
*wp
= target
->watchpoints
;
1021 LOG_DEBUG("Current hartid = %d", riscv_current_hartid(target
));
1023 /*TODO instead of disassembling the instruction that we think caused the
1024 * trigger, check the hit bit of each watchpoint first. The hit bit is
1025 * simpler and more reliable to check but as it is optional and relatively
1026 * new, not all hardware will implement it */
1028 riscv_get_register(target
, &dpc
, GDB_REGNO_DPC
);
1029 const uint8_t length
= 4;
1030 LOG_DEBUG("dpc is 0x%" PRIx64
, dpc
);
1032 /* fetch the instruction at dpc */
1033 uint8_t buffer
[length
];
1034 if (target_read_buffer(target
, dpc
, length
, buffer
) != ERROR_OK
) {
1035 LOG_ERROR("Failed to read instruction at dpc 0x%" PRIx64
, dpc
);
1039 uint32_t instruction
= 0;
1041 for (int i
= 0; i
< length
; i
++) {
1042 LOG_DEBUG("Next byte is %x", buffer
[i
]);
1043 instruction
+= (buffer
[i
] << 8 * i
);
1045 LOG_DEBUG("Full instruction is %x", instruction
);
1047 /* find out which memory address is accessed by the instruction at dpc */
1048 /* opcode is first 7 bits of the instruction */
1049 uint8_t opcode
= instruction
& 0x7F;
1052 riscv_reg_t mem_addr
;
1054 if (opcode
== MATCH_LB
|| opcode
== MATCH_SB
) {
1055 rs1
= (instruction
& 0xf8000) >> 15;
1056 riscv_get_register(target
, &mem_addr
, rs1
);
1058 if (opcode
== MATCH_SB
) {
1059 LOG_DEBUG("%x is store instruction", instruction
);
1060 imm
= ((instruction
& 0xf80) >> 7) | ((instruction
& 0xfe000000) >> 20);
1062 LOG_DEBUG("%x is load instruction", instruction
);
1063 imm
= (instruction
& 0xfff00000) >> 20;
1065 /* sign extend 12-bit imm to 16-bits */
1066 if (imm
& (1 << 11))
1069 LOG_DEBUG("memory address=0x%" PRIx64
, mem_addr
);
1071 LOG_DEBUG("%x is not a RV32I load or store", instruction
);
1076 /*TODO support length/mask */
1077 if (wp
->address
== mem_addr
) {
1078 *hit_watchpoint
= wp
;
1079 LOG_DEBUG("Hit address=%" TARGET_PRIxADDR
, wp
->address
);
1085 /* No match found - either we hit a watchpoint caused by an instruction that
1086 * this function does not yet disassemble, or we hit a breakpoint.
1088 * OpenOCD will behave as if this function had never been implemented i.e.
1089 * report the halt to GDB with no address information. */
1094 static int oldriscv_step(struct target
*target
, int current
, uint32_t address
,
1095 int handle_breakpoints
)
1097 struct target_type
*tt
= get_target_type(target
);
1098 return tt
->step(target
, current
, address
, handle_breakpoints
);
1101 static int old_or_new_riscv_step(struct target
*target
, int current
,
1102 target_addr_t address
, int handle_breakpoints
)
1105 LOG_DEBUG("handle_breakpoints=%d", handle_breakpoints
);
1107 return oldriscv_step(target
, current
, address
, handle_breakpoints
);
1109 return riscv_openocd_step(target
, current
, address
, handle_breakpoints
);
1113 static int riscv_examine(struct target
*target
)
1115 LOG_DEBUG("riscv_examine()");
1116 if (target_was_examined(target
)) {
1117 LOG_DEBUG("Target was already examined.");
1121 /* Don't need to select dbus, since the first thing we do is read dtmcontrol. */
1124 uint32_t dtmcontrol
= dtmcontrol_scan(target
, 0);
1125 LOG_DEBUG("dtmcontrol=0x%x", dtmcontrol
);
1126 info
->dtm_version
= get_field(dtmcontrol
, DTMCONTROL_VERSION
);
1127 LOG_DEBUG(" version=0x%x", info
->dtm_version
);
1129 struct target_type
*tt
= get_target_type(target
);
1133 int result
= tt
->init_target(info
->cmd_ctx
, target
);
1134 if (result
!= ERROR_OK
)
1137 return tt
->examine(target
);
1140 static int oldriscv_poll(struct target
*target
)
1142 struct target_type
*tt
= get_target_type(target
);
1143 return tt
->poll(target
);
1146 static int old_or_new_riscv_poll(struct target
*target
)
1150 return oldriscv_poll(target
);
1152 return riscv_openocd_poll(target
);
1155 int riscv_select_current_hart(struct target
*target
)
1157 return riscv_set_current_hartid(target
, target
->coreid
);
1160 int halt_prep(struct target
*target
)
1164 LOG_DEBUG("[%s] prep hart, debug_reason=%d", target_name(target
),
1165 target
->debug_reason
);
1166 if (riscv_select_current_hart(target
) != ERROR_OK
)
1168 if (riscv_is_halted(target
)) {
1169 LOG_DEBUG("[%s] Hart is already halted (reason=%d).",
1170 target_name(target
), target
->debug_reason
);
1172 if (r
->halt_prep(target
) != ERROR_OK
)
1180 int riscv_halt_go_all_harts(struct target
*target
)
1184 if (riscv_select_current_hart(target
) != ERROR_OK
)
1186 if (riscv_is_halted(target
)) {
1187 LOG_DEBUG("[%s] Hart is already halted.", target_name(target
));
1189 if (r
->halt_go(target
) != ERROR_OK
)
1193 riscv_invalidate_register_cache(target
);
1198 int halt_go(struct target
*target
)
1200 riscv_info_t
*r
= riscv_info(target
);
1202 if (!r
->is_halted
) {
1203 struct target_type
*tt
= get_target_type(target
);
1204 result
= tt
->halt(target
);
1206 result
= riscv_halt_go_all_harts(target
);
1208 target
->state
= TARGET_HALTED
;
1209 if (target
->debug_reason
== DBG_REASON_NOTHALTED
)
1210 target
->debug_reason
= DBG_REASON_DBGRQ
;
1215 static int halt_finish(struct target
*target
)
1217 return target_call_event_callbacks(target
, TARGET_EVENT_HALTED
);
1220 int riscv_halt(struct target
*target
)
1224 if (!r
->is_halted
) {
1225 struct target_type
*tt
= get_target_type(target
);
1226 return tt
->halt(target
);
1229 LOG_DEBUG("[%d] halting all harts", target
->coreid
);
1231 int result
= ERROR_OK
;
1233 for (struct target_list
*tlist
= target
->head
; tlist
; tlist
= tlist
->next
) {
1234 struct target
*t
= tlist
->target
;
1235 if (halt_prep(t
) != ERROR_OK
)
1236 result
= ERROR_FAIL
;
1239 for (struct target_list
*tlist
= target
->head
; tlist
; tlist
= tlist
->next
) {
1240 struct target
*t
= tlist
->target
;
1241 riscv_info_t
*i
= riscv_info(t
);
1243 if (halt_go(t
) != ERROR_OK
)
1244 result
= ERROR_FAIL
;
1248 for (struct target_list
*tlist
= target
->head
; tlist
; tlist
= tlist
->next
) {
1249 struct target
*t
= tlist
->target
;
1250 if (halt_finish(t
) != ERROR_OK
)
1255 if (halt_prep(target
) != ERROR_OK
)
1256 result
= ERROR_FAIL
;
1257 if (halt_go(target
) != ERROR_OK
)
1258 result
= ERROR_FAIL
;
1259 if (halt_finish(target
) != ERROR_OK
)
1266 static int riscv_assert_reset(struct target
*target
)
1268 LOG_DEBUG("[%d]", target
->coreid
);
1269 struct target_type
*tt
= get_target_type(target
);
1270 riscv_invalidate_register_cache(target
);
1271 return tt
->assert_reset(target
);
1274 static int riscv_deassert_reset(struct target
*target
)
1276 LOG_DEBUG("[%d]", target
->coreid
);
1277 struct target_type
*tt
= get_target_type(target
);
1278 return tt
->deassert_reset(target
);
1281 int riscv_resume_prep_all_harts(struct target
*target
)
1285 LOG_DEBUG("[%s] prep hart", target_name(target
));
1286 if (riscv_select_current_hart(target
) != ERROR_OK
)
1288 if (riscv_is_halted(target
)) {
1289 if (r
->resume_prep(target
) != ERROR_OK
)
1292 LOG_DEBUG("[%s] hart requested resume, but was already resumed",
1293 target_name(target
));
1296 LOG_DEBUG("[%s] mark as prepped", target_name(target
));
1302 /* state must be riscv_reg_t state[RISCV_MAX_HWBPS] = {0}; */
1303 static int disable_triggers(struct target
*target
, riscv_reg_t
*state
)
1307 LOG_DEBUG("deal with triggers");
1309 if (riscv_enumerate_triggers(target
) != ERROR_OK
)
1312 if (r
->manual_hwbp_set
) {
1313 /* Look at every trigger that may have been set. */
1314 riscv_reg_t tselect
;
1315 if (riscv_get_register(target
, &tselect
, GDB_REGNO_TSELECT
) != ERROR_OK
)
1317 for (unsigned int t
= 0; t
< r
->trigger_count
; t
++) {
1318 if (riscv_set_register(target
, GDB_REGNO_TSELECT
, t
) != ERROR_OK
)
1321 if (riscv_get_register(target
, &tdata1
, GDB_REGNO_TDATA1
) != ERROR_OK
)
1323 if (tdata1
& MCONTROL_DMODE(riscv_xlen(target
))) {
1325 if (riscv_set_register(target
, GDB_REGNO_TDATA1
, 0) != ERROR_OK
)
1329 if (riscv_set_register(target
, GDB_REGNO_TSELECT
, tselect
) != ERROR_OK
)
1333 /* Just go through the triggers we manage. */
1334 struct watchpoint
*watchpoint
= target
->watchpoints
;
1336 while (watchpoint
) {
1337 LOG_DEBUG("watchpoint %d: set=%d", i
, watchpoint
->set
);
1338 state
[i
] = watchpoint
->set
;
1339 if (watchpoint
->set
) {
1340 if (riscv_remove_watchpoint(target
, watchpoint
) != ERROR_OK
)
1343 watchpoint
= watchpoint
->next
;
1351 static int enable_triggers(struct target
*target
, riscv_reg_t
*state
)
1355 if (r
->manual_hwbp_set
) {
1356 /* Look at every trigger that may have been set. */
1357 riscv_reg_t tselect
;
1358 if (riscv_get_register(target
, &tselect
, GDB_REGNO_TSELECT
) != ERROR_OK
)
1360 for (unsigned int t
= 0; t
< r
->trigger_count
; t
++) {
1361 if (state
[t
] != 0) {
1362 if (riscv_set_register(target
, GDB_REGNO_TSELECT
, t
) != ERROR_OK
)
1364 if (riscv_set_register(target
, GDB_REGNO_TDATA1
, state
[t
]) != ERROR_OK
)
1368 if (riscv_set_register(target
, GDB_REGNO_TSELECT
, tselect
) != ERROR_OK
)
1372 struct watchpoint
*watchpoint
= target
->watchpoints
;
1374 while (watchpoint
) {
1375 LOG_DEBUG("watchpoint %d: cleared=%" PRId64
, i
, state
[i
]);
1377 if (riscv_add_watchpoint(target
, watchpoint
) != ERROR_OK
)
1380 watchpoint
= watchpoint
->next
;
1389 * Get everything ready to resume.
1391 static int resume_prep(struct target
*target
, int current
,
1392 target_addr_t address
, int handle_breakpoints
, int debug_execution
)
1395 LOG_DEBUG("[%d]", target
->coreid
);
1398 riscv_set_register(target
, GDB_REGNO_PC
, address
);
1400 if (target
->debug_reason
== DBG_REASON_WATCHPOINT
) {
1401 /* To be able to run off a trigger, disable all the triggers, step, and
1402 * then resume as usual. */
1403 riscv_reg_t trigger_state
[RISCV_MAX_HWBPS
] = {0};
1405 if (disable_triggers(target
, trigger_state
) != ERROR_OK
)
1408 if (old_or_new_riscv_step(target
, true, 0, false) != ERROR_OK
)
1411 if (enable_triggers(target
, trigger_state
) != ERROR_OK
)
1416 if (riscv_resume_prep_all_harts(target
) != ERROR_OK
)
1420 LOG_DEBUG("[%d] mark as prepped", target
->coreid
);
1427 * Resume all the harts that have been prepped, as close to instantaneous as
1430 static int resume_go(struct target
*target
, int current
,
1431 target_addr_t address
, int handle_breakpoints
, int debug_execution
)
1433 riscv_info_t
*r
= riscv_info(target
);
1435 if (!r
->is_halted
) {
1436 struct target_type
*tt
= get_target_type(target
);
1437 result
= tt
->resume(target
, current
, address
, handle_breakpoints
,
1440 result
= riscv_resume_go_all_harts(target
);
1446 static int resume_finish(struct target
*target
)
1448 register_cache_invalidate(target
->reg_cache
);
1450 target
->state
= TARGET_RUNNING
;
1451 target
->debug_reason
= DBG_REASON_NOTHALTED
;
1452 return target_call_event_callbacks(target
, TARGET_EVENT_RESUMED
);
1456 * @par single_hart When true, only resume a single hart even if SMP is
1457 * configured. This is used to run algorithms on just one hart.
1460 struct target
*target
,
1462 target_addr_t address
,
1463 int handle_breakpoints
,
1464 int debug_execution
,
1467 LOG_DEBUG("handle_breakpoints=%d", handle_breakpoints
);
1468 int result
= ERROR_OK
;
1469 if (target
->smp
&& !single_hart
) {
1470 for (struct target_list
*tlist
= target
->head
; tlist
; tlist
= tlist
->next
) {
1471 struct target
*t
= tlist
->target
;
1472 if (resume_prep(t
, current
, address
, handle_breakpoints
,
1473 debug_execution
) != ERROR_OK
)
1474 result
= ERROR_FAIL
;
1477 for (struct target_list
*tlist
= target
->head
; tlist
; tlist
= tlist
->next
) {
1478 struct target
*t
= tlist
->target
;
1479 riscv_info_t
*i
= riscv_info(t
);
1481 if (resume_go(t
, current
, address
, handle_breakpoints
,
1482 debug_execution
) != ERROR_OK
)
1483 result
= ERROR_FAIL
;
1487 for (struct target_list
*tlist
= target
->head
; tlist
; tlist
= tlist
->next
) {
1488 struct target
*t
= tlist
->target
;
1489 if (resume_finish(t
) != ERROR_OK
)
1494 if (resume_prep(target
, current
, address
, handle_breakpoints
,
1495 debug_execution
) != ERROR_OK
)
1496 result
= ERROR_FAIL
;
1497 if (resume_go(target
, current
, address
, handle_breakpoints
,
1498 debug_execution
) != ERROR_OK
)
1499 result
= ERROR_FAIL
;
1500 if (resume_finish(target
) != ERROR_OK
)
1507 static int riscv_target_resume(struct target
*target
, int current
, target_addr_t address
,
1508 int handle_breakpoints
, int debug_execution
)
1510 return riscv_resume(target
, current
, address
, handle_breakpoints
,
1511 debug_execution
, false);
1514 static int riscv_mmu(struct target
*target
, int *enabled
)
1516 if (!riscv_enable_virt2phys
) {
1521 /* Don't use MMU in explicit or effective M (machine) mode */
1523 if (riscv_get_register(target
, &priv
, GDB_REGNO_PRIV
) != ERROR_OK
) {
1524 LOG_ERROR("Failed to read priv register.");
1528 riscv_reg_t mstatus
;
1529 if (riscv_get_register(target
, &mstatus
, GDB_REGNO_MSTATUS
) != ERROR_OK
) {
1530 LOG_ERROR("Failed to read mstatus register.");
1534 if ((get_field(mstatus
, MSTATUS_MPRV
) ? get_field(mstatus
, MSTATUS_MPP
) : priv
) == PRV_M
) {
1535 LOG_DEBUG("SATP/MMU ignored in Machine mode (mstatus=0x%" PRIx64
").", mstatus
);
1541 if (riscv_get_register(target
, &satp
, GDB_REGNO_SATP
) != ERROR_OK
) {
1542 LOG_DEBUG("Couldn't read SATP.");
1543 /* If we can't read SATP, then there must not be an MMU. */
1548 if (get_field(satp
, RISCV_SATP_MODE(riscv_xlen(target
))) == SATP_MODE_OFF
) {
1549 LOG_DEBUG("MMU is disabled.");
1552 LOG_DEBUG("MMU is enabled.");
1559 static int riscv_address_translate(struct target
*target
,
1560 target_addr_t
virtual, target_addr_t
*physical
)
1563 riscv_reg_t satp_value
;
1566 target_addr_t table_address
;
1567 const virt2phys_info_t
*info
;
1571 int result
= riscv_get_register(target
, &satp_value
, GDB_REGNO_SATP
);
1572 if (result
!= ERROR_OK
)
1575 unsigned xlen
= riscv_xlen(target
);
1576 mode
= get_field(satp_value
, RISCV_SATP_MODE(xlen
));
1578 case SATP_MODE_SV32
:
1581 case SATP_MODE_SV39
:
1584 case SATP_MODE_SV48
:
1588 LOG_ERROR("No translation or protection." \
1589 " (satp: 0x%" PRIx64
")", satp_value
);
1592 LOG_ERROR("The translation mode is not supported." \
1593 " (satp: 0x%" PRIx64
")", satp_value
);
1596 LOG_DEBUG("virtual=0x%" TARGET_PRIxADDR
"; mode=%s", virtual, info
->name
);
1598 /* verify bits xlen-1:va_bits-1 are all equal */
1599 target_addr_t mask
= ((target_addr_t
)1 << (xlen
- (info
->va_bits
- 1))) - 1;
1600 target_addr_t masked_msbs
= (virtual >> (info
->va_bits
- 1)) & mask
;
1601 if (masked_msbs
!= 0 && masked_msbs
!= mask
) {
1602 LOG_ERROR("Virtual address 0x%" TARGET_PRIxADDR
" is not sign-extended "
1603 "for %s mode.", virtual, info
->name
);
1607 ppn_value
= get_field(satp_value
, RISCV_SATP_PPN(xlen
));
1608 table_address
= ppn_value
<< RISCV_PGSHIFT
;
1609 i
= info
->level
- 1;
1611 uint64_t vpn
= virtual >> info
->vpn_shift
[i
];
1612 vpn
&= info
->vpn_mask
[i
];
1613 target_addr_t pte_address
= table_address
+
1614 (vpn
<< info
->pte_shift
);
1616 assert(info
->pte_shift
<= 3);
1617 int retval
= r
->read_memory(target
, pte_address
,
1618 4, (1 << info
->pte_shift
) / 4, buffer
, 4);
1619 if (retval
!= ERROR_OK
)
1622 if (info
->pte_shift
== 2)
1623 pte
= buf_get_u32(buffer
, 0, 32);
1625 pte
= buf_get_u64(buffer
, 0, 64);
1627 LOG_DEBUG("i=%d; PTE @0x%" TARGET_PRIxADDR
" = 0x%" PRIx64
, i
,
1630 if (!(pte
& PTE_V
) || (!(pte
& PTE_R
) && (pte
& PTE_W
)))
1633 if ((pte
& PTE_R
) || (pte
& PTE_X
)) /* Found leaf PTE. */
1639 ppn_value
= pte
>> PTE_PPN_SHIFT
;
1640 table_address
= ppn_value
<< RISCV_PGSHIFT
;
1644 LOG_ERROR("Couldn't find the PTE.");
1648 /* Make sure to clear out the high bits that may be set. */
1649 *physical
= virtual & (((target_addr_t
)1 << info
->va_bits
) - 1);
1651 while (i
< info
->level
) {
1652 ppn_value
= pte
>> info
->pte_ppn_shift
[i
];
1653 ppn_value
&= info
->pte_ppn_mask
[i
];
1654 *physical
&= ~(((target_addr_t
)info
->pa_ppn_mask
[i
]) <<
1655 info
->pa_ppn_shift
[i
]);
1656 *physical
|= (ppn_value
<< info
->pa_ppn_shift
[i
]);
1659 LOG_DEBUG("0x%" TARGET_PRIxADDR
" -> 0x%" TARGET_PRIxADDR
, virtual,
1665 static int riscv_virt2phys(struct target
*target
, target_addr_t
virtual, target_addr_t
*physical
)
1668 if (riscv_mmu(target
, &enabled
) == ERROR_OK
) {
1672 if (riscv_address_translate(target
, virtual, physical
) == ERROR_OK
)
1679 static int riscv_read_phys_memory(struct target
*target
, target_addr_t phys_address
,
1680 uint32_t size
, uint32_t count
, uint8_t *buffer
)
1683 if (riscv_select_current_hart(target
) != ERROR_OK
)
1685 return r
->read_memory(target
, phys_address
, size
, count
, buffer
, size
);
1688 static int riscv_read_memory(struct target
*target
, target_addr_t address
,
1689 uint32_t size
, uint32_t count
, uint8_t *buffer
)
1692 LOG_WARNING("0-length read from 0x%" TARGET_PRIxADDR
, address
);
1696 if (riscv_select_current_hart(target
) != ERROR_OK
)
1699 target_addr_t physical_addr
;
1700 if (target
->type
->virt2phys(target
, address
, &physical_addr
) == ERROR_OK
)
1701 address
= physical_addr
;
1704 return r
->read_memory(target
, address
, size
, count
, buffer
, size
);
1707 static int riscv_write_phys_memory(struct target
*target
, target_addr_t phys_address
,
1708 uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1710 if (riscv_select_current_hart(target
) != ERROR_OK
)
1712 struct target_type
*tt
= get_target_type(target
);
1713 return tt
->write_memory(target
, phys_address
, size
, count
, buffer
);
1716 static int riscv_write_memory(struct target
*target
, target_addr_t address
,
1717 uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1720 LOG_WARNING("0-length write to 0x%" TARGET_PRIxADDR
, address
);
1724 if (riscv_select_current_hart(target
) != ERROR_OK
)
1727 target_addr_t physical_addr
;
1728 if (target
->type
->virt2phys(target
, address
, &physical_addr
) == ERROR_OK
)
1729 address
= physical_addr
;
1731 struct target_type
*tt
= get_target_type(target
);
1732 return tt
->write_memory(target
, address
, size
, count
, buffer
);
1735 const char *riscv_get_gdb_arch(struct target
*target
)
1737 switch (riscv_xlen(target
)) {
1739 return "riscv:rv32";
1741 return "riscv:rv64";
1743 LOG_ERROR("Unsupported xlen: %d", riscv_xlen(target
));
1747 static int riscv_get_gdb_reg_list_internal(struct target
*target
,
1748 struct reg
**reg_list
[], int *reg_list_size
,
1749 enum target_register_class reg_class
, bool read
)
1752 LOG_DEBUG("current_hartid=%d, reg_class=%d, read=%d",
1753 r
->current_hartid
, reg_class
, read
);
1755 if (!target
->reg_cache
) {
1756 LOG_ERROR("Target not initialized. Return ERROR_FAIL.");
1760 if (riscv_select_current_hart(target
) != ERROR_OK
)
1763 switch (reg_class
) {
1764 case REG_CLASS_GENERAL
:
1765 *reg_list_size
= 33;
1768 *reg_list_size
= target
->reg_cache
->num_regs
;
1771 LOG_ERROR("Unsupported reg_class: %d", reg_class
);
1775 *reg_list
= calloc(*reg_list_size
, sizeof(struct reg
*));
1779 for (int i
= 0; i
< *reg_list_size
; i
++) {
1780 assert(!target
->reg_cache
->reg_list
[i
].valid
||
1781 target
->reg_cache
->reg_list
[i
].size
> 0);
1782 (*reg_list
)[i
] = &target
->reg_cache
->reg_list
[i
];
1784 target
->reg_cache
->reg_list
[i
].exist
&&
1785 !target
->reg_cache
->reg_list
[i
].valid
) {
1786 if (target
->reg_cache
->reg_list
[i
].type
->get(
1787 &target
->reg_cache
->reg_list
[i
]) != ERROR_OK
)
1795 static int riscv_get_gdb_reg_list_noread(struct target
*target
,
1796 struct reg
**reg_list
[], int *reg_list_size
,
1797 enum target_register_class reg_class
)
1799 return riscv_get_gdb_reg_list_internal(target
, reg_list
, reg_list_size
,
1803 static int riscv_get_gdb_reg_list(struct target
*target
,
1804 struct reg
**reg_list
[], int *reg_list_size
,
1805 enum target_register_class reg_class
)
1807 return riscv_get_gdb_reg_list_internal(target
, reg_list
, reg_list_size
,
1811 static int riscv_arch_state(struct target
*target
)
1813 struct target_type
*tt
= get_target_type(target
);
1814 return tt
->arch_state(target
);
1817 /* Algorithm must end with a software breakpoint instruction. */
1818 static int riscv_run_algorithm(struct target
*target
, int num_mem_params
,
1819 struct mem_param
*mem_params
, int num_reg_params
,
1820 struct reg_param
*reg_params
, target_addr_t entry_point
,
1821 target_addr_t exit_point
, int timeout_ms
, void *arch_info
)
1825 if (num_mem_params
> 0) {
1826 LOG_ERROR("Memory parameters are not supported for RISC-V algorithms.");
1830 if (target
->state
!= TARGET_HALTED
) {
1831 LOG_WARNING("target not halted");
1832 return ERROR_TARGET_NOT_HALTED
;
1835 /* Save registers */
1836 struct reg
*reg_pc
= register_get_by_name(target
->reg_cache
, "pc", true);
1837 if (!reg_pc
|| reg_pc
->type
->get(reg_pc
) != ERROR_OK
)
1839 uint64_t saved_pc
= buf_get_u64(reg_pc
->value
, 0, reg_pc
->size
);
1840 LOG_DEBUG("saved_pc=0x%" PRIx64
, saved_pc
);
1842 uint64_t saved_regs
[32];
1843 for (int i
= 0; i
< num_reg_params
; i
++) {
1844 LOG_DEBUG("save %s", reg_params
[i
].reg_name
);
1845 struct reg
*r
= register_get_by_name(target
->reg_cache
, reg_params
[i
].reg_name
, false);
1847 LOG_ERROR("Couldn't find register named '%s'", reg_params
[i
].reg_name
);
1851 if (r
->size
!= reg_params
[i
].size
) {
1852 LOG_ERROR("Register %s is %d bits instead of %d bits.",
1853 reg_params
[i
].reg_name
, r
->size
, reg_params
[i
].size
);
1857 if (r
->number
> GDB_REGNO_XPR31
) {
1858 LOG_ERROR("Only GPRs can be use as argument registers.");
1862 if (r
->type
->get(r
) != ERROR_OK
)
1864 saved_regs
[r
->number
] = buf_get_u64(r
->value
, 0, r
->size
);
1866 if (reg_params
[i
].direction
== PARAM_OUT
|| reg_params
[i
].direction
== PARAM_IN_OUT
) {
1867 if (r
->type
->set(r
, reg_params
[i
].value
) != ERROR_OK
)
1873 /* Disable Interrupts before attempting to run the algorithm. */
1874 uint64_t current_mstatus
;
1875 uint8_t mstatus_bytes
[8] = { 0 };
1877 LOG_DEBUG("Disabling Interrupts");
1878 struct reg
*reg_mstatus
= register_get_by_name(target
->reg_cache
,
1881 LOG_ERROR("Couldn't find mstatus!");
1885 reg_mstatus
->type
->get(reg_mstatus
);
1886 current_mstatus
= buf_get_u64(reg_mstatus
->value
, 0, reg_mstatus
->size
);
1887 uint64_t ie_mask
= MSTATUS_MIE
| MSTATUS_HIE
| MSTATUS_SIE
| MSTATUS_UIE
;
1888 buf_set_u64(mstatus_bytes
, 0, info
->xlen
, set_field(current_mstatus
,
1891 reg_mstatus
->type
->set(reg_mstatus
, mstatus_bytes
);
1894 LOG_DEBUG("resume at 0x%" TARGET_PRIxADDR
, entry_point
);
1895 if (riscv_resume(target
, 0, entry_point
, 0, 0, true) != ERROR_OK
)
1898 int64_t start
= timeval_ms();
1899 while (target
->state
!= TARGET_HALTED
) {
1900 LOG_DEBUG("poll()");
1901 int64_t now
= timeval_ms();
1902 if (now
- start
> timeout_ms
) {
1903 LOG_ERROR("Algorithm timed out after %" PRId64
" ms.", now
- start
);
1905 old_or_new_riscv_poll(target
);
1906 enum gdb_regno regnums
[] = {
1907 GDB_REGNO_RA
, GDB_REGNO_SP
, GDB_REGNO_GP
, GDB_REGNO_TP
,
1908 GDB_REGNO_T0
, GDB_REGNO_T1
, GDB_REGNO_T2
, GDB_REGNO_FP
,
1909 GDB_REGNO_S1
, GDB_REGNO_A0
, GDB_REGNO_A1
, GDB_REGNO_A2
,
1910 GDB_REGNO_A3
, GDB_REGNO_A4
, GDB_REGNO_A5
, GDB_REGNO_A6
,
1911 GDB_REGNO_A7
, GDB_REGNO_S2
, GDB_REGNO_S3
, GDB_REGNO_S4
,
1912 GDB_REGNO_S5
, GDB_REGNO_S6
, GDB_REGNO_S7
, GDB_REGNO_S8
,
1913 GDB_REGNO_S9
, GDB_REGNO_S10
, GDB_REGNO_S11
, GDB_REGNO_T3
,
1914 GDB_REGNO_T4
, GDB_REGNO_T5
, GDB_REGNO_T6
,
1916 GDB_REGNO_MSTATUS
, GDB_REGNO_MEPC
, GDB_REGNO_MCAUSE
,
1918 for (unsigned i
= 0; i
< ARRAY_SIZE(regnums
); i
++) {
1919 enum gdb_regno regno
= regnums
[i
];
1920 riscv_reg_t reg_value
;
1921 if (riscv_get_register(target
, ®_value
, regno
) != ERROR_OK
)
1923 LOG_ERROR("%s = 0x%" PRIx64
, gdb_regno_name(regno
), reg_value
);
1925 return ERROR_TARGET_TIMEOUT
;
1928 int result
= old_or_new_riscv_poll(target
);
1929 if (result
!= ERROR_OK
)
1933 /* The current hart id might have been changed in poll(). */
1934 if (riscv_select_current_hart(target
) != ERROR_OK
)
1937 if (reg_pc
->type
->get(reg_pc
) != ERROR_OK
)
1939 uint64_t final_pc
= buf_get_u64(reg_pc
->value
, 0, reg_pc
->size
);
1940 if (exit_point
&& final_pc
!= exit_point
) {
1941 LOG_ERROR("PC ended up at 0x%" PRIx64
" instead of 0x%"
1942 TARGET_PRIxADDR
, final_pc
, exit_point
);
1946 /* Restore Interrupts */
1947 LOG_DEBUG("Restoring Interrupts");
1948 buf_set_u64(mstatus_bytes
, 0, info
->xlen
, current_mstatus
);
1949 reg_mstatus
->type
->set(reg_mstatus
, mstatus_bytes
);
1951 /* Restore registers */
1952 uint8_t buf
[8] = { 0 };
1953 buf_set_u64(buf
, 0, info
->xlen
, saved_pc
);
1954 if (reg_pc
->type
->set(reg_pc
, buf
) != ERROR_OK
)
1957 for (int i
= 0; i
< num_reg_params
; i
++) {
1958 if (reg_params
[i
].direction
== PARAM_IN
||
1959 reg_params
[i
].direction
== PARAM_IN_OUT
) {
1960 struct reg
*r
= register_get_by_name(target
->reg_cache
, reg_params
[i
].reg_name
, false);
1961 if (r
->type
->get(r
) != ERROR_OK
) {
1962 LOG_ERROR("get(%s) failed", r
->name
);
1965 buf_cpy(r
->value
, reg_params
[i
].value
, reg_params
[i
].size
);
1967 LOG_DEBUG("restore %s", reg_params
[i
].reg_name
);
1968 struct reg
*r
= register_get_by_name(target
->reg_cache
, reg_params
[i
].reg_name
, false);
1969 buf_set_u64(buf
, 0, info
->xlen
, saved_regs
[r
->number
]);
1970 if (r
->type
->set(r
, buf
) != ERROR_OK
) {
1971 LOG_ERROR("set(%s) failed", r
->name
);
1979 static int riscv_checksum_memory(struct target
*target
,
1980 target_addr_t address
, uint32_t count
,
1983 struct working_area
*crc_algorithm
;
1984 struct reg_param reg_params
[2];
1987 LOG_DEBUG("address=0x%" TARGET_PRIxADDR
"; count=0x%" PRIx32
, address
, count
);
1989 static const uint8_t riscv32_crc_code
[] = {
1990 #include "contrib/loaders/checksum/riscv32_crc.inc"
1992 static const uint8_t riscv64_crc_code
[] = {
1993 #include "contrib/loaders/checksum/riscv64_crc.inc"
1996 static const uint8_t *crc_code
;
1998 unsigned xlen
= riscv_xlen(target
);
1999 unsigned crc_code_size
;
2001 crc_code
= riscv32_crc_code
;
2002 crc_code_size
= sizeof(riscv32_crc_code
);
2004 crc_code
= riscv64_crc_code
;
2005 crc_code_size
= sizeof(riscv64_crc_code
);
2008 if (count
< crc_code_size
* 4) {
2009 /* Don't use the algorithm for relatively small buffers. It's faster
2010 * just to read the memory. target_checksum_memory() will take care of
2011 * that if we fail. */
2015 retval
= target_alloc_working_area(target
, crc_code_size
, &crc_algorithm
);
2016 if (retval
!= ERROR_OK
)
2019 if (crc_algorithm
->address
+ crc_algorithm
->size
> address
&&
2020 crc_algorithm
->address
< address
+ count
) {
2021 /* Region to checksum overlaps with the work area we've been assigned.
2022 * Bail. (Would be better to manually checksum what we read there, and
2023 * use the algorithm for the rest.) */
2024 target_free_working_area(target
, crc_algorithm
);
2028 retval
= target_write_buffer(target
, crc_algorithm
->address
, crc_code_size
,
2030 if (retval
!= ERROR_OK
) {
2031 LOG_ERROR("Failed to write code to " TARGET_ADDR_FMT
": %d",
2032 crc_algorithm
->address
, retval
);
2033 target_free_working_area(target
, crc_algorithm
);
2037 init_reg_param(®_params
[0], "a0", xlen
, PARAM_IN_OUT
);
2038 init_reg_param(®_params
[1], "a1", xlen
, PARAM_OUT
);
2039 buf_set_u64(reg_params
[0].value
, 0, xlen
, address
);
2040 buf_set_u64(reg_params
[1].value
, 0, xlen
, count
);
2042 /* 20 second timeout/megabyte */
2043 int timeout
= 20000 * (1 + (count
/ (1024 * 1024)));
2045 retval
= target_run_algorithm(target
, 0, NULL
, 2, reg_params
,
2046 crc_algorithm
->address
,
2047 0, /* Leave exit point unspecified because we don't know. */
2050 if (retval
== ERROR_OK
)
2051 *checksum
= buf_get_u32(reg_params
[0].value
, 0, 32);
2053 LOG_ERROR("error executing RISC-V CRC algorithm");
2055 destroy_reg_param(®_params
[0]);
2056 destroy_reg_param(®_params
[1]);
2058 target_free_working_area(target
, crc_algorithm
);
2060 LOG_DEBUG("checksum=0x%" PRIx32
", result=%d", *checksum
, retval
);
2065 /*** OpenOCD Helper Functions ***/
2067 enum riscv_poll_hart
{
2069 RPH_DISCOVERED_HALTED
,
2070 RPH_DISCOVERED_RUNNING
,
2073 static enum riscv_poll_hart
riscv_poll_hart(struct target
*target
, int hartid
)
2076 if (riscv_set_current_hartid(target
, hartid
) != ERROR_OK
)
2079 LOG_DEBUG("polling hart %d, target->state=%d", hartid
, target
->state
);
2081 /* If OpenOCD thinks we're running but this hart is halted then it's time
2082 * to raise an event. */
2083 bool halted
= riscv_is_halted(target
);
2084 if (target
->state
!= TARGET_HALTED
&& halted
) {
2085 LOG_DEBUG(" triggered a halt");
2087 return RPH_DISCOVERED_HALTED
;
2088 } else if (target
->state
!= TARGET_RUNNING
&& !halted
) {
2089 LOG_DEBUG(" triggered running");
2090 target
->state
= TARGET_RUNNING
;
2091 target
->debug_reason
= DBG_REASON_NOTHALTED
;
2092 return RPH_DISCOVERED_RUNNING
;
2095 return RPH_NO_CHANGE
;
2098 int set_debug_reason(struct target
*target
, enum riscv_halt_reason halt_reason
)
2100 switch (halt_reason
) {
2101 case RISCV_HALT_BREAKPOINT
:
2102 target
->debug_reason
= DBG_REASON_BREAKPOINT
;
2104 case RISCV_HALT_TRIGGER
:
2105 target
->debug_reason
= DBG_REASON_WATCHPOINT
;
2107 case RISCV_HALT_INTERRUPT
:
2108 case RISCV_HALT_GROUP
:
2109 target
->debug_reason
= DBG_REASON_DBGRQ
;
2111 case RISCV_HALT_SINGLESTEP
:
2112 target
->debug_reason
= DBG_REASON_SINGLESTEP
;
2114 case RISCV_HALT_UNKNOWN
:
2115 target
->debug_reason
= DBG_REASON_UNDEFINED
;
2117 case RISCV_HALT_ERROR
:
2120 LOG_DEBUG("[%s] debug_reason=%d", target_name(target
), target
->debug_reason
);
2124 int sample_memory(struct target
*target
)
2128 if (!r
->sample_buf
.buf
|| !r
->sample_config
.enabled
)
2131 LOG_DEBUG("buf used/size: %d/%d", r
->sample_buf
.used
, r
->sample_buf
.size
);
2133 uint64_t start
= timeval_ms();
2134 riscv_sample_buf_maybe_add_timestamp(target
, true);
2135 int result
= ERROR_OK
;
2136 if (r
->sample_memory
) {
2137 result
= r
->sample_memory(target
, &r
->sample_buf
, &r
->sample_config
,
2138 start
+ TARGET_DEFAULT_POLLING_INTERVAL
);
2139 if (result
!= ERROR_NOT_IMPLEMENTED
)
2143 /* Default slow path. */
2144 while (timeval_ms() - start
< TARGET_DEFAULT_POLLING_INTERVAL
) {
2145 for (unsigned int i
= 0; i
< ARRAY_SIZE(r
->sample_config
.bucket
); i
++) {
2146 if (r
->sample_config
.bucket
[i
].enabled
&&
2147 r
->sample_buf
.used
+ 1 + r
->sample_config
.bucket
[i
].size_bytes
< r
->sample_buf
.size
) {
2148 assert(i
< RISCV_SAMPLE_BUF_TIMESTAMP_BEFORE
);
2149 r
->sample_buf
.buf
[r
->sample_buf
.used
] = i
;
2150 result
= riscv_read_phys_memory(
2151 target
, r
->sample_config
.bucket
[i
].address
,
2152 r
->sample_config
.bucket
[i
].size_bytes
, 1,
2153 r
->sample_buf
.buf
+ r
->sample_buf
.used
+ 1);
2154 if (result
== ERROR_OK
)
2155 r
->sample_buf
.used
+= 1 + r
->sample_config
.bucket
[i
].size_bytes
;
2163 riscv_sample_buf_maybe_add_timestamp(target
, false);
2164 if (result
!= ERROR_OK
) {
2165 LOG_INFO("Turning off memory sampling because it failed.");
2166 r
->sample_config
.enabled
= false;
2171 /*** OpenOCD Interface ***/
2172 int riscv_openocd_poll(struct target
*target
)
2174 LOG_DEBUG("polling all harts");
2175 int halted_hart
= -1;
2178 unsigned halts_discovered
= 0;
2179 unsigned should_remain_halted
= 0;
2180 unsigned should_resume
= 0;
2182 for (struct target_list
*list
= target
->head
; list
;
2183 list
= list
->next
, i
++) {
2184 struct target
*t
= list
->target
;
2185 riscv_info_t
*r
= riscv_info(t
);
2186 enum riscv_poll_hart out
= riscv_poll_hart(t
, r
->current_hartid
);
2190 case RPH_DISCOVERED_RUNNING
:
2191 t
->state
= TARGET_RUNNING
;
2192 t
->debug_reason
= DBG_REASON_NOTHALTED
;
2194 case RPH_DISCOVERED_HALTED
:
2196 t
->state
= TARGET_HALTED
;
2197 enum riscv_halt_reason halt_reason
=
2198 riscv_halt_reason(t
, r
->current_hartid
);
2199 if (set_debug_reason(t
, halt_reason
) != ERROR_OK
)
2202 if (halt_reason
== RISCV_HALT_BREAKPOINT
) {
2204 switch (riscv_semihosting(t
, &retval
)) {
2207 /* This hart should remain halted. */
2208 should_remain_halted
++;
2211 /* This hart should be resumed, along with any other
2212 * harts that halted due to haltgroups. */
2218 } else if (halt_reason
!= RISCV_HALT_GROUP
) {
2219 should_remain_halted
++;
2228 LOG_DEBUG("should_remain_halted=%d, should_resume=%d",
2229 should_remain_halted
, should_resume
);
2230 if (should_remain_halted
&& should_resume
) {
2231 LOG_WARNING("%d harts should remain halted, and %d should resume.",
2232 should_remain_halted
, should_resume
);
2234 if (should_remain_halted
) {
2235 LOG_DEBUG("halt all");
2237 } else if (should_resume
) {
2238 LOG_DEBUG("resume all");
2239 riscv_resume(target
, true, 0, 0, 0, false);
2242 /* Sample memory if any target is running. */
2243 for (struct target_list
*list
= target
->head
; list
;
2244 list
= list
->next
, i
++) {
2245 struct target
*t
= list
->target
;
2246 if (t
->state
== TARGET_RUNNING
) {
2247 sample_memory(target
);
2255 enum riscv_poll_hart out
= riscv_poll_hart(target
,
2256 riscv_current_hartid(target
));
2257 if (out
== RPH_NO_CHANGE
|| out
== RPH_DISCOVERED_RUNNING
) {
2258 if (target
->state
== TARGET_RUNNING
)
2259 sample_memory(target
);
2261 } else if (out
== RPH_ERROR
) {
2265 halted_hart
= riscv_current_hartid(target
);
2266 LOG_DEBUG(" hart %d halted", halted_hart
);
2268 enum riscv_halt_reason halt_reason
= riscv_halt_reason(target
, halted_hart
);
2269 if (set_debug_reason(target
, halt_reason
) != ERROR_OK
)
2271 target
->state
= TARGET_HALTED
;
2274 if (target
->debug_reason
== DBG_REASON_BREAKPOINT
) {
2276 switch (riscv_semihosting(target
, &retval
)) {
2279 target_call_event_callbacks(target
, TARGET_EVENT_HALTED
);
2282 if (riscv_resume(target
, true, 0, 0, 0, false) != ERROR_OK
)
2289 target_call_event_callbacks(target
, TARGET_EVENT_HALTED
);
2295 int riscv_openocd_step(struct target
*target
, int current
,
2296 target_addr_t address
, int handle_breakpoints
)
2298 LOG_DEBUG("stepping rtos hart");
2301 riscv_set_register(target
, GDB_REGNO_PC
, address
);
2303 riscv_reg_t trigger_state
[RISCV_MAX_HWBPS
] = {0};
2304 if (disable_triggers(target
, trigger_state
) != ERROR_OK
)
2307 int out
= riscv_step_rtos_hart(target
);
2308 if (out
!= ERROR_OK
) {
2309 LOG_ERROR("unable to step rtos hart");
2313 register_cache_invalidate(target
->reg_cache
);
2315 if (enable_triggers(target
, trigger_state
) != ERROR_OK
)
2318 target
->state
= TARGET_RUNNING
;
2319 target_call_event_callbacks(target
, TARGET_EVENT_RESUMED
);
2320 target
->state
= TARGET_HALTED
;
2321 target
->debug_reason
= DBG_REASON_SINGLESTEP
;
2322 target_call_event_callbacks(target
, TARGET_EVENT_HALTED
);
2326 /* Command Handlers */
2327 COMMAND_HANDLER(riscv_set_command_timeout_sec
)
2329 if (CMD_ARGC
!= 1) {
2330 LOG_ERROR("Command takes exactly 1 parameter");
2331 return ERROR_COMMAND_SYNTAX_ERROR
;
2333 int timeout
= atoi(CMD_ARGV
[0]);
2335 LOG_ERROR("%s is not a valid integer argument for command.", CMD_ARGV
[0]);
2339 riscv_command_timeout_sec
= timeout
;
2344 COMMAND_HANDLER(riscv_set_reset_timeout_sec
)
2346 if (CMD_ARGC
!= 1) {
2347 LOG_ERROR("Command takes exactly 1 parameter");
2348 return ERROR_COMMAND_SYNTAX_ERROR
;
2350 int timeout
= atoi(CMD_ARGV
[0]);
2352 LOG_ERROR("%s is not a valid integer argument for command.", CMD_ARGV
[0]);
2356 riscv_reset_timeout_sec
= timeout
;
2360 COMMAND_HANDLER(riscv_set_prefer_sba
)
2362 struct target
*target
= get_current_target(CMD_CTX
);
2365 LOG_WARNING("`riscv set_prefer_sba` is deprecated. Please use `riscv set_mem_access` instead.");
2366 if (CMD_ARGC
!= 1) {
2367 LOG_ERROR("Command takes exactly 1 parameter");
2368 return ERROR_COMMAND_SYNTAX_ERROR
;
2370 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], prefer_sba
);
2372 /* Use system bus with highest priority */
2373 r
->mem_access_methods
[0] = RISCV_MEM_ACCESS_SYSBUS
;
2374 r
->mem_access_methods
[1] = RISCV_MEM_ACCESS_PROGBUF
;
2375 r
->mem_access_methods
[2] = RISCV_MEM_ACCESS_ABSTRACT
;
2377 /* Use progbuf with highest priority */
2378 r
->mem_access_methods
[0] = RISCV_MEM_ACCESS_PROGBUF
;
2379 r
->mem_access_methods
[1] = RISCV_MEM_ACCESS_SYSBUS
;
2380 r
->mem_access_methods
[2] = RISCV_MEM_ACCESS_ABSTRACT
;
2383 /* Reset warning flags */
2384 r
->mem_access_progbuf_warn
= true;
2385 r
->mem_access_sysbus_warn
= true;
2386 r
->mem_access_abstract_warn
= true;
2391 COMMAND_HANDLER(riscv_set_mem_access
)
2393 struct target
*target
= get_current_target(CMD_CTX
);
2395 int progbuf_cnt
= 0;
2397 int abstract_cnt
= 0;
2399 if (CMD_ARGC
< 1 || CMD_ARGC
> RISCV_NUM_MEM_ACCESS_METHODS
) {
2400 LOG_ERROR("Command takes 1 to %d parameters", RISCV_NUM_MEM_ACCESS_METHODS
);
2401 return ERROR_COMMAND_SYNTAX_ERROR
;
2404 /* Check argument validity */
2405 for (unsigned int i
= 0; i
< CMD_ARGC
; i
++) {
2406 if (strcmp("progbuf", CMD_ARGV
[i
]) == 0) {
2408 } else if (strcmp("sysbus", CMD_ARGV
[i
]) == 0) {
2410 } else if (strcmp("abstract", CMD_ARGV
[i
]) == 0) {
2413 LOG_ERROR("Unknown argument '%s'. "
2414 "Must be one of: 'progbuf', 'sysbus' or 'abstract'.", CMD_ARGV
[i
]);
2415 return ERROR_COMMAND_SYNTAX_ERROR
;
2418 if (progbuf_cnt
> 1 || sysbus_cnt
> 1 || abstract_cnt
> 1) {
2419 LOG_ERROR("Syntax error - duplicate arguments to `riscv set_mem_access`.");
2420 return ERROR_COMMAND_SYNTAX_ERROR
;
2423 /* Args are valid, store them */
2424 for (unsigned int i
= 0; i
< RISCV_NUM_MEM_ACCESS_METHODS
; i
++)
2425 r
->mem_access_methods
[i
] = RISCV_MEM_ACCESS_UNSPECIFIED
;
2426 for (unsigned int i
= 0; i
< CMD_ARGC
; i
++) {
2427 if (strcmp("progbuf", CMD_ARGV
[i
]) == 0)
2428 r
->mem_access_methods
[i
] = RISCV_MEM_ACCESS_PROGBUF
;
2429 else if (strcmp("sysbus", CMD_ARGV
[i
]) == 0)
2430 r
->mem_access_methods
[i
] = RISCV_MEM_ACCESS_SYSBUS
;
2431 else if (strcmp("abstract", CMD_ARGV
[i
]) == 0)
2432 r
->mem_access_methods
[i
] = RISCV_MEM_ACCESS_ABSTRACT
;
2435 /* Reset warning flags */
2436 r
->mem_access_progbuf_warn
= true;
2437 r
->mem_access_sysbus_warn
= true;
2438 r
->mem_access_abstract_warn
= true;
2443 COMMAND_HANDLER(riscv_set_enable_virtual
)
2445 if (CMD_ARGC
!= 1) {
2446 LOG_ERROR("Command takes exactly 1 parameter");
2447 return ERROR_COMMAND_SYNTAX_ERROR
;
2449 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], riscv_enable_virtual
);
2453 int parse_ranges(struct list_head
*ranges
, const char *tcl_arg
, const char *reg_type
, unsigned int max_val
)
2455 char *args
= strdup(tcl_arg
);
2459 /* For backward compatibility, allow multiple parameters within one TCL argument, separated by ',' */
2460 char *arg
= strtok(args
, ",");
2466 char *dash
= strchr(arg
, '-');
2467 char *equals
= strchr(arg
, '=');
2470 if (!dash
&& !equals
) {
2471 /* Expecting single register number. */
2472 if (sscanf(arg
, "%u%n", &low
, &pos
) != 1 || pos
!= strlen(arg
)) {
2473 LOG_ERROR("Failed to parse single register number from '%s'.", arg
);
2475 return ERROR_COMMAND_SYNTAX_ERROR
;
2477 } else if (dash
&& !equals
) {
2478 /* Expecting register range - two numbers separated by a dash: ##-## */
2481 if (sscanf(arg
, "%u%n", &low
, &pos
) != 1 || pos
!= strlen(arg
)) {
2482 LOG_ERROR("Failed to parse single register number from '%s'.", arg
);
2484 return ERROR_COMMAND_SYNTAX_ERROR
;
2486 if (sscanf(dash
, "%u%n", &high
, &pos
) != 1 || pos
!= strlen(dash
)) {
2487 LOG_ERROR("Failed to parse single register number from '%s'.", dash
);
2489 return ERROR_COMMAND_SYNTAX_ERROR
;
2492 LOG_ERROR("Incorrect range encountered [%u, %u].", low
, high
);
2496 } else if (!dash
&& equals
) {
2497 /* Expecting single register number with textual name specified: ##=name */
2500 if (sscanf(arg
, "%u%n", &low
, &pos
) != 1 || pos
!= strlen(arg
)) {
2501 LOG_ERROR("Failed to parse single register number from '%s'.", arg
);
2503 return ERROR_COMMAND_SYNTAX_ERROR
;
2506 name
= calloc(1, strlen(equals
) + strlen(reg_type
) + 2);
2508 LOG_ERROR("Failed to allocate register name.");
2513 /* Register prefix: "csr_" or "custom_" */
2514 strcpy(name
, reg_type
);
2515 name
[strlen(reg_type
)] = '_';
2517 if (sscanf(equals
, "%[_a-zA-Z0-9]%n", name
+ strlen(reg_type
) + 1, &pos
) != 1 || pos
!= strlen(equals
)) {
2518 LOG_ERROR("Failed to parse register name from '%s'.", equals
);
2521 return ERROR_COMMAND_SYNTAX_ERROR
;
2524 LOG_ERROR("Invalid argument '%s'.", arg
);
2526 return ERROR_COMMAND_SYNTAX_ERROR
;
2529 high
= high
> low
? high
: low
;
2531 if (high
> max_val
) {
2532 LOG_ERROR("Cannot expose %s register number %u, maximum allowed value is %u.", reg_type
, high
, max_val
);
2538 /* Check for overlap, name uniqueness. */
2539 range_list_t
*entry
;
2540 list_for_each_entry(entry
, ranges
, list
) {
2541 if ((entry
->low
<= high
) && (low
<= entry
->high
)) {
2543 LOG_WARNING("Duplicate %s register number - "
2544 "Register %u has already been exposed previously", reg_type
, low
);
2546 LOG_WARNING("Overlapping register ranges - Register range starting from %u overlaps "
2547 "with already exposed register/range at %u.", low
, entry
->low
);
2550 if (entry
->name
&& name
&& (strcasecmp(entry
->name
, name
) == 0)) {
2551 LOG_ERROR("Duplicate register name \"%s\" found.", name
);
2558 range_list_t
*range
= calloc(1, sizeof(range_list_t
));
2560 LOG_ERROR("Failed to allocate range list.");
2569 list_add(&range
->list
, ranges
);
2571 arg
= strtok(NULL
, ",");
2578 COMMAND_HANDLER(riscv_set_expose_csrs
)
2580 if (CMD_ARGC
== 0) {
2581 LOG_ERROR("Command expects parameters");
2582 return ERROR_COMMAND_SYNTAX_ERROR
;
2585 struct target
*target
= get_current_target(CMD_CTX
);
2589 for (unsigned int i
= 0; i
< CMD_ARGC
; i
++) {
2590 ret
= parse_ranges(&info
->expose_csr
, CMD_ARGV
[i
], "csr", 0xfff);
2591 if (ret
!= ERROR_OK
)
2598 COMMAND_HANDLER(riscv_set_expose_custom
)
2600 if (CMD_ARGC
== 0) {
2601 LOG_ERROR("Command expects parameters");
2602 return ERROR_COMMAND_SYNTAX_ERROR
;
2605 struct target
*target
= get_current_target(CMD_CTX
);
2609 for (unsigned int i
= 0; i
< CMD_ARGC
; i
++) {
2610 ret
= parse_ranges(&info
->expose_custom
, CMD_ARGV
[i
], "custom", 0x3fff);
2611 if (ret
!= ERROR_OK
)
2618 COMMAND_HANDLER(riscv_authdata_read
)
2620 unsigned int index
= 0;
2621 if (CMD_ARGC
== 0) {
2623 } else if (CMD_ARGC
== 1) {
2624 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], index
);
2626 LOG_ERROR("Command takes at most one parameter");
2627 return ERROR_COMMAND_SYNTAX_ERROR
;
2630 struct target
*target
= get_current_target(CMD_CTX
);
2632 LOG_ERROR("target is NULL!");
2638 LOG_ERROR("riscv_info is NULL!");
2642 if (r
->authdata_read
) {
2644 if (r
->authdata_read(target
, &value
, index
) != ERROR_OK
)
2646 command_print_sameline(CMD
, "0x%08" PRIx32
, value
);
2649 LOG_ERROR("authdata_read is not implemented for this target.");
2654 COMMAND_HANDLER(riscv_authdata_write
)
2657 unsigned int index
= 0;
2659 if (CMD_ARGC
== 0) {
2661 } else if (CMD_ARGC
== 1) {
2662 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], value
);
2663 } else if (CMD_ARGC
== 2) {
2664 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], index
);
2665 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2667 LOG_ERROR("Command takes at most 2 arguments");
2668 return ERROR_COMMAND_SYNTAX_ERROR
;
2671 struct target
*target
= get_current_target(CMD_CTX
);
2674 if (r
->authdata_write
) {
2675 return r
->authdata_write(target
, value
, index
);
2677 LOG_ERROR("authdata_write is not implemented for this target.");
2682 COMMAND_HANDLER(riscv_dmi_read
)
2684 if (CMD_ARGC
!= 1) {
2685 LOG_ERROR("Command takes 1 parameter");
2686 return ERROR_COMMAND_SYNTAX_ERROR
;
2689 struct target
*target
= get_current_target(CMD_CTX
);
2691 LOG_ERROR("target is NULL!");
2697 LOG_ERROR("riscv_info is NULL!");
2702 uint32_t address
, value
;
2703 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2704 if (r
->dmi_read(target
, &value
, address
) != ERROR_OK
)
2706 command_print(CMD
, "0x%" PRIx32
, value
);
2709 LOG_ERROR("dmi_read is not implemented for this target.");
2715 COMMAND_HANDLER(riscv_dmi_write
)
2717 if (CMD_ARGC
!= 2) {
2718 LOG_ERROR("Command takes exactly 2 arguments");
2719 return ERROR_COMMAND_SYNTAX_ERROR
;
2722 struct target
*target
= get_current_target(CMD_CTX
);
2725 uint32_t address
, value
;
2726 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2727 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2730 return r
->dmi_write(target
, address
, value
);
2732 LOG_ERROR("dmi_write is not implemented for this target.");
2737 COMMAND_HANDLER(riscv_test_sba_config_reg
)
2739 if (CMD_ARGC
!= 4) {
2740 LOG_ERROR("Command takes exactly 4 arguments");
2741 return ERROR_COMMAND_SYNTAX_ERROR
;
2744 struct target
*target
= get_current_target(CMD_CTX
);
2747 target_addr_t legal_address
;
2749 target_addr_t illegal_address
;
2750 bool run_sbbusyerror_test
;
2752 COMMAND_PARSE_NUMBER(target_addr
, CMD_ARGV
[0], legal_address
);
2753 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], num_words
);
2754 COMMAND_PARSE_NUMBER(target_addr
, CMD_ARGV
[2], illegal_address
);
2755 COMMAND_PARSE_ON_OFF(CMD_ARGV
[3], run_sbbusyerror_test
);
2757 if (r
->test_sba_config_reg
) {
2758 return r
->test_sba_config_reg(target
, legal_address
, num_words
,
2759 illegal_address
, run_sbbusyerror_test
);
2761 LOG_ERROR("test_sba_config_reg is not implemented for this target.");
2766 COMMAND_HANDLER(riscv_reset_delays
)
2771 LOG_ERROR("Command takes at most one argument");
2772 return ERROR_COMMAND_SYNTAX_ERROR
;
2776 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], wait
);
2778 struct target
*target
= get_current_target(CMD_CTX
);
2780 r
->reset_delays_wait
= wait
;
2784 COMMAND_HANDLER(riscv_set_ir
)
2786 if (CMD_ARGC
!= 2) {
2787 LOG_ERROR("Command takes exactly 2 arguments");
2788 return ERROR_COMMAND_SYNTAX_ERROR
;
2792 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2794 if (!strcmp(CMD_ARGV
[0], "idcode"))
2795 buf_set_u32(ir_idcode
, 0, 32, value
);
2796 else if (!strcmp(CMD_ARGV
[0], "dtmcs"))
2797 buf_set_u32(ir_dtmcontrol
, 0, 32, value
);
2798 else if (!strcmp(CMD_ARGV
[0], "dmi"))
2799 buf_set_u32(ir_dbus
, 0, 32, value
);
2806 COMMAND_HANDLER(riscv_resume_order
)
2809 LOG_ERROR("Command takes at most one argument");
2810 return ERROR_COMMAND_SYNTAX_ERROR
;
2813 if (!strcmp(CMD_ARGV
[0], "normal")) {
2814 resume_order
= RO_NORMAL
;
2815 } else if (!strcmp(CMD_ARGV
[0], "reversed")) {
2816 resume_order
= RO_REVERSED
;
2818 LOG_ERROR("Unsupported resume order: %s", CMD_ARGV
[0]);
2825 COMMAND_HANDLER(riscv_use_bscan_tunnel
)
2828 int tunnel_type
= BSCAN_TUNNEL_NESTED_TAP
;
2831 LOG_ERROR("Command takes at most two arguments");
2832 return ERROR_COMMAND_SYNTAX_ERROR
;
2833 } else if (CMD_ARGC
== 1) {
2834 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], irwidth
);
2835 } else if (CMD_ARGC
== 2) {
2836 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], irwidth
);
2837 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[1], tunnel_type
);
2839 if (tunnel_type
== BSCAN_TUNNEL_NESTED_TAP
)
2840 LOG_INFO("Nested Tap based Bscan Tunnel Selected");
2841 else if (tunnel_type
== BSCAN_TUNNEL_DATA_REGISTER
)
2842 LOG_INFO("Simple Register based Bscan Tunnel Selected");
2844 LOG_INFO("Invalid Tunnel type selected ! : selecting default Nested Tap Type");
2846 bscan_tunnel_type
= tunnel_type
;
2847 bscan_tunnel_ir_width
= irwidth
;
2851 COMMAND_HANDLER(riscv_set_enable_virt2phys
)
2853 if (CMD_ARGC
!= 1) {
2854 LOG_ERROR("Command takes exactly 1 parameter");
2855 return ERROR_COMMAND_SYNTAX_ERROR
;
2857 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], riscv_enable_virt2phys
);
2861 COMMAND_HANDLER(riscv_set_ebreakm
)
2863 if (CMD_ARGC
!= 1) {
2864 LOG_ERROR("Command takes exactly 1 parameter");
2865 return ERROR_COMMAND_SYNTAX_ERROR
;
2867 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], riscv_ebreakm
);
2871 COMMAND_HANDLER(riscv_set_ebreaks
)
2873 if (CMD_ARGC
!= 1) {
2874 LOG_ERROR("Command takes exactly 1 parameter");
2875 return ERROR_COMMAND_SYNTAX_ERROR
;
2877 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], riscv_ebreaks
);
2881 COMMAND_HANDLER(riscv_set_ebreaku
)
2883 if (CMD_ARGC
!= 1) {
2884 LOG_ERROR("Command takes exactly 1 parameter");
2885 return ERROR_COMMAND_SYNTAX_ERROR
;
2887 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], riscv_ebreaku
);
2891 COMMAND_HELPER(riscv_print_info_line
, const char *section
, const char *key
,
2895 snprintf(full_key
, sizeof(full_key
), "%s.%s", section
, key
);
2896 command_print(CMD
, "%-21s %3d", full_key
, value
);
2900 COMMAND_HANDLER(handle_info
)
2902 struct target
*target
= get_current_target(CMD_CTX
);
2905 /* This output format can be fed directly into TCL's "array set". */
2907 riscv_print_info_line(CMD
, "hart", "xlen", riscv_xlen(target
));
2908 riscv_enumerate_triggers(target
);
2909 riscv_print_info_line(CMD
, "hart", "trigger_count",
2913 return CALL_COMMAND_HANDLER(r
->print_info
, target
);
2918 static const struct command_registration riscv_exec_command_handlers
[] = {
2921 .handler
= handle_info
,
2922 .mode
= COMMAND_EXEC
,
2924 .help
= "Displays some information OpenOCD detected about the target."
2927 .name
= "set_command_timeout_sec",
2928 .handler
= riscv_set_command_timeout_sec
,
2929 .mode
= COMMAND_ANY
,
2931 .help
= "Set the wall-clock timeout (in seconds) for individual commands"
2934 .name
= "set_reset_timeout_sec",
2935 .handler
= riscv_set_reset_timeout_sec
,
2936 .mode
= COMMAND_ANY
,
2938 .help
= "Set the wall-clock timeout (in seconds) after reset is deasserted"
2941 .name
= "set_prefer_sba",
2942 .handler
= riscv_set_prefer_sba
,
2943 .mode
= COMMAND_ANY
,
2945 .help
= "When on, prefer to use System Bus Access to access memory. "
2946 "When off (default), prefer to use the Program Buffer to access memory."
2949 .name
= "set_mem_access",
2950 .handler
= riscv_set_mem_access
,
2951 .mode
= COMMAND_ANY
,
2952 .usage
= "method1 [method2] [method3]",
2953 .help
= "Set which memory access methods shall be used and in which order "
2954 "of priority. Method can be one of: 'progbuf', 'sysbus' or 'abstract'."
2957 .name
= "set_enable_virtual",
2958 .handler
= riscv_set_enable_virtual
,
2959 .mode
= COMMAND_ANY
,
2961 .help
= "When on, memory accesses are performed on physical or virtual "
2962 "memory depending on the current system configuration. "
2963 "When off (default), all memory accessses are performed on physical memory."
2966 .name
= "expose_csrs",
2967 .handler
= riscv_set_expose_csrs
,
2968 .mode
= COMMAND_CONFIG
,
2969 .usage
= "n0[-m0|=name0][,n1[-m1|=name1]]...",
2970 .help
= "Configure a list of inclusive ranges for CSRs to expose in "
2971 "addition to the standard ones. This must be executed before "
2975 .name
= "expose_custom",
2976 .handler
= riscv_set_expose_custom
,
2977 .mode
= COMMAND_CONFIG
,
2978 .usage
= "n0[-m0|=name0][,n1[-m1|=name1]]...",
2979 .help
= "Configure a list of inclusive ranges for custom registers to "
2980 "expose. custom0 is accessed as abstract register number 0xc000, "
2981 "etc. This must be executed before `init`."
2984 .name
= "authdata_read",
2985 .handler
= riscv_authdata_read
,
2987 .mode
= COMMAND_ANY
,
2988 .help
= "Return the 32-bit value read from authdata or authdata0 "
2989 "(index=0), or authdata1 (index=1)."
2992 .name
= "authdata_write",
2993 .handler
= riscv_authdata_write
,
2994 .mode
= COMMAND_ANY
,
2995 .usage
= "[index] value",
2996 .help
= "Write the 32-bit value to authdata or authdata0 (index=0), "
2997 "or authdata1 (index=1)."
3001 .handler
= riscv_dmi_read
,
3002 .mode
= COMMAND_ANY
,
3004 .help
= "Perform a 32-bit DMI read at address, returning the value."
3007 .name
= "dmi_write",
3008 .handler
= riscv_dmi_write
,
3009 .mode
= COMMAND_ANY
,
3010 .usage
= "address value",
3011 .help
= "Perform a 32-bit DMI write of value at address."
3014 .name
= "test_sba_config_reg",
3015 .handler
= riscv_test_sba_config_reg
,
3016 .mode
= COMMAND_ANY
,
3017 .usage
= "legal_address num_words "
3018 "illegal_address run_sbbusyerror_test[on/off]",
3019 .help
= "Perform a series of tests on the SBCS register. "
3020 "Inputs are a legal, 128-byte aligned address and a number of words to "
3021 "read/write starting at that address (i.e., address range [legal address, "
3022 "legal_address+word_size*num_words) must be legally readable/writable), "
3023 "an illegal, 128-byte aligned address for error flag/handling cases, "
3024 "and whether sbbusyerror test should be run."
3027 .name
= "reset_delays",
3028 .handler
= riscv_reset_delays
,
3029 .mode
= COMMAND_ANY
,
3031 .help
= "OpenOCD learns how many Run-Test/Idle cycles are required "
3032 "between scans to avoid encountering the target being busy. This "
3033 "command resets those learned values after `wait` scans. It's only "
3034 "useful for testing OpenOCD itself."
3037 .name
= "resume_order",
3038 .handler
= riscv_resume_order
,
3039 .mode
= COMMAND_ANY
,
3040 .usage
= "normal|reversed",
3041 .help
= "Choose the order that harts are resumed in when `hasel` is not "
3042 "supported. Normal order is from lowest hart index to highest. "
3043 "Reversed order is from highest hart index to lowest."
3047 .handler
= riscv_set_ir
,
3048 .mode
= COMMAND_ANY
,
3049 .usage
= "[idcode|dtmcs|dmi] value",
3050 .help
= "Set IR value for specified JTAG register."
3053 .name
= "use_bscan_tunnel",
3054 .handler
= riscv_use_bscan_tunnel
,
3055 .mode
= COMMAND_ANY
,
3056 .usage
= "value [type]",
3057 .help
= "Enable or disable use of a BSCAN tunnel to reach DM. Supply "
3058 "the width of the DM transport TAP's instruction register to "
3059 "enable. Supply a value of 0 to disable. Pass A second argument "
3060 "(optional) to indicate Bscan Tunnel Type {0:(default) NESTED_TAP , "
3064 .name
= "set_enable_virt2phys",
3065 .handler
= riscv_set_enable_virt2phys
,
3066 .mode
= COMMAND_ANY
,
3068 .help
= "When on (default), enable translation from virtual address to "
3072 .name
= "set_ebreakm",
3073 .handler
= riscv_set_ebreakm
,
3074 .mode
= COMMAND_ANY
,
3076 .help
= "Control dcsr.ebreakm. When off, M-mode ebreak instructions "
3077 "don't trap to OpenOCD. Defaults to on."
3080 .name
= "set_ebreaks",
3081 .handler
= riscv_set_ebreaks
,
3082 .mode
= COMMAND_ANY
,
3084 .help
= "Control dcsr.ebreaks. When off, S-mode ebreak instructions "
3085 "don't trap to OpenOCD. Defaults to on."
3088 .name
= "set_ebreaku",
3089 .handler
= riscv_set_ebreaku
,
3090 .mode
= COMMAND_ANY
,
3092 .help
= "Control dcsr.ebreaku. When off, U-mode ebreak instructions "
3093 "don't trap to OpenOCD. Defaults to on."
3095 COMMAND_REGISTRATION_DONE
3099 * To be noted that RISC-V targets use the same semihosting commands as
3102 * The main reason is compatibility with existing tools. For example the
3103 * Eclipse OpenOCD/SEGGER J-Link/QEMU plug-ins have several widgets to
3104 * configure semihosting, which generate commands like `arm semihosting
3106 * A secondary reason is the fact that the protocol used is exactly the
3107 * one specified by ARM. If RISC-V will ever define its own semihosting
3108 * protocol, then a command like `riscv semihosting enable` will make
3109 * sense, but for now all semihosting commands are prefixed with `arm`.
3111 extern const struct command_registration semihosting_common_handlers
[];
3113 const struct command_registration riscv_command_handlers
[] = {
3116 .mode
= COMMAND_ANY
,
3117 .help
= "RISC-V Command Group",
3119 .chain
= riscv_exec_command_handlers
3123 .mode
= COMMAND_ANY
,
3124 .help
= "ARM Command Group",
3126 .chain
= semihosting_common_handlers
3128 COMMAND_REGISTRATION_DONE
3131 static unsigned riscv_xlen_nonconst(struct target
*target
)
3133 return riscv_xlen(target
);
3136 static unsigned int riscv_data_bits(struct target
*target
)
3140 return r
->data_bits(target
);
3141 return riscv_xlen(target
);
3144 struct target_type riscv_target
= {
3147 .target_create
= riscv_create_target
,
3148 .init_target
= riscv_init_target
,
3149 .deinit_target
= riscv_deinit_target
,
3150 .examine
= riscv_examine
,
3152 /* poll current target status */
3153 .poll
= old_or_new_riscv_poll
,
3156 .resume
= riscv_target_resume
,
3157 .step
= old_or_new_riscv_step
,
3159 .assert_reset
= riscv_assert_reset
,
3160 .deassert_reset
= riscv_deassert_reset
,
3162 .read_memory
= riscv_read_memory
,
3163 .write_memory
= riscv_write_memory
,
3164 .read_phys_memory
= riscv_read_phys_memory
,
3165 .write_phys_memory
= riscv_write_phys_memory
,
3167 .checksum_memory
= riscv_checksum_memory
,
3170 .virt2phys
= riscv_virt2phys
,
3172 .get_gdb_arch
= riscv_get_gdb_arch
,
3173 .get_gdb_reg_list
= riscv_get_gdb_reg_list
,
3174 .get_gdb_reg_list_noread
= riscv_get_gdb_reg_list_noread
,
3176 .add_breakpoint
= riscv_add_breakpoint
,
3177 .remove_breakpoint
= riscv_remove_breakpoint
,
3179 .add_watchpoint
= riscv_add_watchpoint
,
3180 .remove_watchpoint
= riscv_remove_watchpoint
,
3181 .hit_watchpoint
= riscv_hit_watchpoint
,
3183 .arch_state
= riscv_arch_state
,
3185 .run_algorithm
= riscv_run_algorithm
,
3187 .commands
= riscv_command_handlers
,
3189 .address_bits
= riscv_xlen_nonconst
,
3190 .data_bits
= riscv_data_bits
3193 /*** RISC-V Interface ***/
3195 void riscv_info_init(struct target
*target
, riscv_info_t
*r
)
3197 memset(r
, 0, sizeof(*r
));
3199 r
->registers_initialized
= false;
3200 r
->current_hartid
= target
->coreid
;
3201 r
->version_specific
= NULL
;
3203 memset(r
->trigger_unique_id
, 0xff, sizeof(r
->trigger_unique_id
));
3207 r
->mem_access_methods
[0] = RISCV_MEM_ACCESS_PROGBUF
;
3208 r
->mem_access_methods
[1] = RISCV_MEM_ACCESS_SYSBUS
;
3209 r
->mem_access_methods
[2] = RISCV_MEM_ACCESS_ABSTRACT
;
3211 r
->mem_access_progbuf_warn
= true;
3212 r
->mem_access_sysbus_warn
= true;
3213 r
->mem_access_abstract_warn
= true;
3215 INIT_LIST_HEAD(&r
->expose_csr
);
3216 INIT_LIST_HEAD(&r
->expose_custom
);
3219 static int riscv_resume_go_all_harts(struct target
*target
)
3223 LOG_DEBUG("[%s] resuming hart", target_name(target
));
3224 if (riscv_select_current_hart(target
) != ERROR_OK
)
3226 if (riscv_is_halted(target
)) {
3227 if (r
->resume_go(target
) != ERROR_OK
)
3230 LOG_DEBUG("[%s] hart requested resume, but was already resumed",
3231 target_name(target
));
3234 riscv_invalidate_register_cache(target
);
3238 int riscv_step_rtos_hart(struct target
*target
)
3241 if (riscv_select_current_hart(target
) != ERROR_OK
)
3243 LOG_DEBUG("[%s] stepping", target_name(target
));
3245 if (!riscv_is_halted(target
)) {
3246 LOG_ERROR("Hart isn't halted before single step!");
3249 riscv_invalidate_register_cache(target
);
3251 if (r
->step_current_hart(target
) != ERROR_OK
)
3253 riscv_invalidate_register_cache(target
);
3255 if (!riscv_is_halted(target
)) {
3256 LOG_ERROR("Hart was not halted after single step!");
3262 bool riscv_supports_extension(struct target
*target
, char letter
)
3266 if (letter
>= 'a' && letter
<= 'z')
3268 else if (letter
>= 'A' && letter
<= 'Z')
3272 return r
->misa
& BIT(num
);
3275 unsigned riscv_xlen(const struct target
*target
)
3281 int riscv_set_current_hartid(struct target
*target
, int hartid
)
3284 if (!r
->select_current_hart
)
3287 int previous_hartid
= riscv_current_hartid(target
);
3288 r
->current_hartid
= hartid
;
3289 LOG_DEBUG("setting hartid to %d, was %d", hartid
, previous_hartid
);
3290 if (r
->select_current_hart(target
) != ERROR_OK
)
3296 void riscv_invalidate_register_cache(struct target
*target
)
3300 LOG_DEBUG("[%d]", target
->coreid
);
3301 register_cache_invalidate(target
->reg_cache
);
3302 for (size_t i
= 0; i
< target
->reg_cache
->num_regs
; ++i
) {
3303 struct reg
*reg
= &target
->reg_cache
->reg_list
[i
];
3307 r
->registers_initialized
= true;
3310 int riscv_current_hartid(const struct target
*target
)
3313 return r
->current_hartid
;
3316 int riscv_count_harts(struct target
*target
)
3321 if (!r
|| !r
->hart_count
)
3323 return r
->hart_count(target
);
3328 * return true iff we are guaranteed that the register will contain exactly
3329 * the value we just wrote when it's read.
3330 * If write is false:
3331 * return true iff we are guaranteed that the register will read the same
3332 * value in the future as the value we just read.
3334 static bool gdb_regno_cacheable(enum gdb_regno regno
, bool write
)
3336 /* GPRs, FPRs, vector registers are just normal data stores. */
3337 if (regno
<= GDB_REGNO_XPR31
||
3338 (regno
>= GDB_REGNO_FPR0
&& regno
<= GDB_REGNO_FPR31
) ||
3339 (regno
>= GDB_REGNO_V0
&& regno
<= GDB_REGNO_V31
))
3342 /* Most CSRs won't change value on us, but we can't assume it about arbitrary
3348 case GDB_REGNO_VSTART
:
3349 case GDB_REGNO_VXSAT
:
3350 case GDB_REGNO_VXRM
:
3351 case GDB_REGNO_VLENB
:
3353 case GDB_REGNO_VTYPE
:
3354 case GDB_REGNO_MISA
:
3355 case GDB_REGNO_DCSR
:
3356 case GDB_REGNO_DSCRATCH0
:
3357 case GDB_REGNO_MSTATUS
:
3358 case GDB_REGNO_MEPC
:
3359 case GDB_REGNO_MCAUSE
:
3360 case GDB_REGNO_SATP
:
3362 * WARL registers might not contain the value we just wrote, but
3363 * these ones won't spontaneously change their value either. *
3367 case GDB_REGNO_TSELECT
: /* I think this should be above, but then it doesn't work. */
3368 case GDB_REGNO_TDATA1
: /* Changes value when tselect is changed. */
3369 case GDB_REGNO_TDATA2
: /* Changse value when tselect is changed. */
3376 * This function is called when the debug user wants to change the value of a
3377 * register. The new value may be cached, and may not be written until the hart
3379 int riscv_set_register(struct target
*target
, enum gdb_regno regid
, riscv_reg_t value
)
3382 LOG_DEBUG("[%s] %s <- %" PRIx64
, target_name(target
), gdb_regno_name(regid
), value
);
3383 assert(r
->set_register
);
3387 /* TODO: Hack to deal with gdb that thinks these registers still exist. */
3388 if (regid
> GDB_REGNO_XPR15
&& regid
<= GDB_REGNO_XPR31
&& value
== 0 &&
3389 riscv_supports_extension(target
, 'E'))
3392 struct reg
*reg
= &target
->reg_cache
->reg_list
[regid
];
3393 buf_set_u64(reg
->value
, 0, reg
->size
, value
);
3395 int result
= r
->set_register(target
, regid
, value
);
3396 if (result
== ERROR_OK
)
3397 reg
->valid
= gdb_regno_cacheable(regid
, true);
3400 LOG_DEBUG("[%s] wrote 0x%" PRIx64
" to %s valid=%d",
3401 target_name(target
), value
, reg
->name
, reg
->valid
);
3405 int riscv_get_register(struct target
*target
, riscv_reg_t
*value
,
3406 enum gdb_regno regid
)
3412 struct reg
*reg
= &target
->reg_cache
->reg_list
[regid
];
3414 LOG_DEBUG("[%s] %s does not exist.",
3415 target_name(target
), gdb_regno_name(regid
));
3419 if (reg
&& reg
->valid
) {
3420 *value
= buf_get_u64(reg
->value
, 0, reg
->size
);
3421 LOG_DEBUG("[%s] %s: %" PRIx64
" (cached)", target_name(target
),
3422 gdb_regno_name(regid
), *value
);
3426 /* TODO: Hack to deal with gdb that thinks these registers still exist. */
3427 if (regid
> GDB_REGNO_XPR15
&& regid
<= GDB_REGNO_XPR31
&&
3428 riscv_supports_extension(target
, 'E')) {
3433 int result
= r
->get_register(target
, value
, regid
);
3435 if (result
== ERROR_OK
)
3436 reg
->valid
= gdb_regno_cacheable(regid
, false);
3438 LOG_DEBUG("[%s] %s: %" PRIx64
, target_name(target
),
3439 gdb_regno_name(regid
), *value
);
3443 bool riscv_is_halted(struct target
*target
)
3446 assert(r
->is_halted
);
3447 return r
->is_halted(target
);
3450 enum riscv_halt_reason
riscv_halt_reason(struct target
*target
, int hartid
)
3453 if (riscv_set_current_hartid(target
, hartid
) != ERROR_OK
)
3454 return RISCV_HALT_ERROR
;
3455 if (!riscv_is_halted(target
)) {
3456 LOG_ERROR("Hart is not halted!");
3457 return RISCV_HALT_UNKNOWN
;
3459 return r
->halt_reason(target
);
3462 size_t riscv_debug_buffer_size(struct target
*target
)
3465 return r
->debug_buffer_size
;
3468 int riscv_write_debug_buffer(struct target
*target
, int index
, riscv_insn_t insn
)
3471 r
->write_debug_buffer(target
, index
, insn
);
3475 riscv_insn_t
riscv_read_debug_buffer(struct target
*target
, int index
)
3478 return r
->read_debug_buffer(target
, index
);
3481 int riscv_execute_debug_buffer(struct target
*target
)
3484 return r
->execute_debug_buffer(target
);
3487 void riscv_fill_dmi_write_u64(struct target
*target
, char *buf
, int a
, uint64_t d
)
3490 r
->fill_dmi_write_u64(target
, buf
, a
, d
);
3493 void riscv_fill_dmi_read_u64(struct target
*target
, char *buf
, int a
)
3496 r
->fill_dmi_read_u64(target
, buf
, a
);
3499 void riscv_fill_dmi_nop_u64(struct target
*target
, char *buf
)
3502 r
->fill_dmi_nop_u64(target
, buf
);
3505 int riscv_dmi_write_u64_bits(struct target
*target
)
3508 return r
->dmi_write_u64_bits(target
);
3512 * Count triggers, and initialize trigger_count for each hart.
3513 * trigger_count is initialized even if this function fails to d