3 #include "debug_module.h"
4 #include "debug_defines.h"
8 #include "debug_rom/debug_rom.h"
9 #include "debug_rom/debug_rom_defines.h"
17 ///////////////////////// debug_module_t
19 debug_module_t::debug_module_t(sim_t
*sim
) : sim(sim
)
24 dmstatus
.authenticated
= 1;
25 dmstatus
.versionlo
= 2;
28 abstractcs
.progsize
= progsize
;
32 memset(halted
, 0, sizeof(halted
));
33 memset(debug_rom_flags
, 0, sizeof(debug_rom_flags
));
34 memset(resumeack
, 0, sizeof(resumeack
));
35 memset(program_buffer
, 0, sizeof(program_buffer
));
36 memset(dmdata
, 0, sizeof(dmdata
));
38 write32(debug_rom_whereto
, 0,
39 jal(ZERO
, debug_abstract_start
- DEBUG_ROM_WHERETO
));
41 memset(debug_abstract
, 0, sizeof(debug_abstract
));
45 void debug_module_t::reset()
47 for (unsigned i
= 0; i
< sim
->nprocs(); i
++) {
48 processor_t
*proc
= sim
->get_core(i
);
50 proc
->halt_request
= false;
56 dmstatus
.authenticated
= 1;
57 dmstatus
.versionlo
= 2;
60 abstractcs
.datacount
= sizeof(dmdata
) / 4;
61 abstractcs
.progsize
= progsize
;
66 void debug_module_t::add_device(bus_t
*bus
) {
67 bus
->add_device(DEBUG_START
, this);
70 bool debug_module_t::load(reg_t addr
, size_t len
, uint8_t* bytes
)
72 addr
= DEBUG_START
+ addr
;
74 if (addr
>= DEBUG_ROM_ENTRY
&&
75 (addr
+ len
) <= (DEBUG_ROM_ENTRY
+ debug_rom_raw_len
)) {
76 memcpy(bytes
, debug_rom_raw
+ addr
- DEBUG_ROM_ENTRY
, len
);
80 if (addr
>= DEBUG_ROM_WHERETO
&& (addr
+ len
) <= (DEBUG_ROM_WHERETO
+ 4)) {
81 memcpy(bytes
, debug_rom_whereto
+ addr
- DEBUG_ROM_WHERETO
, len
);
85 if (addr
>= DEBUG_ROM_FLAGS
&& ((addr
+ len
) <= DEBUG_ROM_FLAGS
+ 1024)) {
86 memcpy(bytes
, debug_rom_flags
+ addr
- DEBUG_ROM_FLAGS
, len
);
90 if (addr
>= debug_abstract_start
&& ((addr
+ len
) <= (debug_abstract_start
+ sizeof(debug_abstract
)))) {
91 memcpy(bytes
, debug_abstract
+ addr
- debug_abstract_start
, len
);
95 if (addr
>= debug_data_start
&& (addr
+ len
) <= (debug_data_start
+ sizeof(dmdata
))) {
96 memcpy(bytes
, dmdata
+ addr
- debug_data_start
, len
);
100 if (addr
>= debug_progbuf_start
&& ((addr
+ len
) <= (debug_progbuf_start
+ sizeof(program_buffer
)))) {
101 memcpy(bytes
, program_buffer
+ addr
- debug_progbuf_start
, len
);
105 fprintf(stderr
, "ERROR: invalid load from debug module: %zd bytes at 0x%016"
106 PRIx64
"\n", len
, addr
);
111 bool debug_module_t::store(reg_t addr
, size_t len
, const uint8_t* bytes
)
116 fprintf(stderr
, "store(addr=0x%lx, len=%d, bytes=0x%08x); "
117 "hartsel=0x%x\n", addr
, (unsigned) len
, *(uint32_t *) bytes
,
121 fprintf(stderr
, "store(addr=0x%lx, len=%d, bytes=...); "
122 "hartsel=0x%x\n", addr
, (unsigned) len
, dmcontrol
.hartsel
);
130 memcpy(id_bytes
, bytes
, 4);
131 id
= read32(id_bytes
, 0);
134 addr
= DEBUG_START
+ addr
;
136 if (addr
>= debug_data_start
&& (addr
+ len
) <= (debug_data_start
+ sizeof(dmdata
))) {
137 memcpy(dmdata
+ addr
- debug_data_start
, bytes
, len
);
141 if (addr
>= debug_progbuf_start
&& ((addr
+ len
) <= (debug_progbuf_start
+ sizeof(program_buffer
)))) {
142 memcpy(program_buffer
+ addr
- debug_progbuf_start
, bytes
, len
);
147 if (addr
== DEBUG_ROM_HALTED
) {
150 if (dmcontrol
.hartsel
== id
) {
151 if (0 == (debug_rom_flags
[id
] & (1 << DEBUG_ROM_FLAG_GO
))){
152 if (dmcontrol
.hartsel
== id
) {
153 abstractcs
.busy
= false;
160 if (addr
== DEBUG_ROM_GOING
) {
161 debug_rom_flags
[dmcontrol
.hartsel
] &= ~(1 << DEBUG_ROM_FLAG_GO
);
165 if (addr
== DEBUG_ROM_RESUMING
) {
168 resumeack
[id
] = true;
169 debug_rom_flags
[id
] &= ~(1 << DEBUG_ROM_FLAG_RESUME
);
173 if (addr
== DEBUG_ROM_EXCEPTION
) {
174 if (abstractcs
.cmderr
== CMDERR_NONE
) {
175 abstractcs
.cmderr
= CMDERR_EXCEPTION
;
180 fprintf(stderr
, "ERROR: invalid store to debug module: %zd bytes at 0x%016"
181 PRIx64
"\n", len
, addr
);
185 void debug_module_t::write32(uint8_t *memory
, unsigned int index
, uint32_t value
)
187 uint8_t* base
= memory
+ index
* 4;
188 base
[0] = value
& 0xff;
189 base
[1] = (value
>> 8) & 0xff;
190 base
[2] = (value
>> 16) & 0xff;
191 base
[3] = (value
>> 24) & 0xff;
194 uint32_t debug_module_t::read32(uint8_t *memory
, unsigned int index
)
196 uint8_t* base
= memory
+ index
* 4;
197 uint32_t value
= ((uint32_t) base
[0]) |
198 (((uint32_t) base
[1]) << 8) |
199 (((uint32_t) base
[2]) << 16) |
200 (((uint32_t) base
[3]) << 24);
204 processor_t
*debug_module_t::current_proc() const
206 processor_t
*proc
= NULL
;
208 proc
= sim
->get_core(dmcontrol
.hartsel
);
209 } catch (const std::out_of_range
&) {
214 bool debug_module_t::dmi_read(unsigned address
, uint32_t *value
)
217 D(fprintf(stderr
, "dmi_read(0x%x) -> ", address
));
218 if (address
>= DMI_DATA0
&& address
< DMI_DATA0
+ abstractcs
.datacount
) {
219 unsigned i
= address
- DMI_DATA0
;
220 result
= read32(dmdata
, i
);
221 if (abstractcs
.busy
) {
223 fprintf(stderr
, "\ndmi_read(0x%02x (data[%d]) -> -1 because abstractcs.busy==true\n", address
, i
);
226 if (abstractcs
.busy
&& abstractcs
.cmderr
== CMDERR_NONE
) {
227 abstractcs
.cmderr
= CMDERR_BUSY
;
230 if (!abstractcs
.busy
&& ((abstractauto
.autoexecdata
>> i
) & 1)) {
231 perform_abstract_command();
233 } else if (address
>= DMI_PROGBUF0
&& address
< DMI_PROGBUF0
+ progsize
) {
234 unsigned i
= address
- DMI_PROGBUF0
;
235 result
= read32(program_buffer
, i
);
236 if (abstractcs
.busy
) {
238 fprintf(stderr
, "\ndmi_read(0x%02x (progbuf[%d]) -> -1 because abstractcs.busy==true\n", address
, i
);
240 if (!abstractcs
.busy
&& ((abstractauto
.autoexecprogbuf
>> i
) & 1)) {
241 perform_abstract_command();
248 processor_t
*proc
= current_proc();
250 dmcontrol
.haltreq
= proc
->halt_request
;
252 result
= set_field(result
, DMI_DMCONTROL_HALTREQ
, dmcontrol
.haltreq
);
253 result
= set_field(result
, DMI_DMCONTROL_RESUMEREQ
, dmcontrol
.resumereq
);
254 result
= set_field(result
, DMI_DMCONTROL_HARTSEL
, dmcontrol
.hartsel
);
255 result
= set_field(result
, DMI_DMCONTROL_HARTRESET
, dmcontrol
.hartreset
);
256 result
= set_field(result
, DMI_DMCONTROL_NDMRESET
, dmcontrol
.ndmreset
);
257 result
= set_field(result
, DMI_DMCONTROL_DMACTIVE
, dmcontrol
.dmactive
);
262 processor_t
*proc
= current_proc();
264 dmstatus
.allnonexistant
= false;
265 dmstatus
.allunavail
= false;
266 dmstatus
.allrunning
= false;
267 dmstatus
.allhalted
= false;
268 dmstatus
.allresumeack
= false;
270 if (halted
[dmcontrol
.hartsel
]) {
271 dmstatus
.allhalted
= true;
273 dmstatus
.allrunning
= true;
276 dmstatus
.allnonexistant
= true;
278 dmstatus
.anynonexistant
= dmstatus
.allnonexistant
;
279 dmstatus
.anyunavail
= dmstatus
.allunavail
;
280 dmstatus
.anyrunning
= dmstatus
.allrunning
;
281 dmstatus
.anyhalted
= dmstatus
.allhalted
;
283 if (resumeack
[dmcontrol
.hartsel
]) {
284 dmstatus
.allresumeack
= true;
286 dmstatus
.allresumeack
= false;
289 dmstatus
.allresumeack
= false;
292 result
= set_field(result
, DMI_DMSTATUS_ALLNONEXISTENT
, dmstatus
.allnonexistant
);
293 result
= set_field(result
, DMI_DMSTATUS_ALLUNAVAIL
, dmstatus
.allunavail
);
294 result
= set_field(result
, DMI_DMSTATUS_ALLRUNNING
, dmstatus
.allrunning
);
295 result
= set_field(result
, DMI_DMSTATUS_ALLHALTED
, dmstatus
.allhalted
);
296 result
= set_field(result
, DMI_DMSTATUS_ALLRESUMEACK
, dmstatus
.allresumeack
);
297 result
= set_field(result
, DMI_DMSTATUS_ANYNONEXISTENT
, dmstatus
.anynonexistant
);
298 result
= set_field(result
, DMI_DMSTATUS_ANYUNAVAIL
, dmstatus
.anyunavail
);
299 result
= set_field(result
, DMI_DMSTATUS_ANYRUNNING
, dmstatus
.anyrunning
);
300 result
= set_field(result
, DMI_DMSTATUS_ANYHALTED
, dmstatus
.anyhalted
);
301 result
= set_field(result
, DMI_DMSTATUS_ANYRESUMEACK
, dmstatus
.anyresumeack
);
302 result
= set_field(result
, DMI_DMSTATUS_AUTHENTICATED
, dmstatus
.authenticated
);
303 result
= set_field(result
, DMI_DMSTATUS_AUTHBUSY
, dmstatus
.authbusy
);
304 result
= set_field(result
, DMI_DMSTATUS_VERSIONHI
, dmstatus
.versionhi
);
305 result
= set_field(result
, DMI_DMSTATUS_VERSIONLO
, dmstatus
.versionlo
);
309 result
= set_field(result
, DMI_ABSTRACTCS_CMDERR
, abstractcs
.cmderr
);
310 result
= set_field(result
, DMI_ABSTRACTCS_BUSY
, abstractcs
.busy
);
311 result
= set_field(result
, DMI_ABSTRACTCS_DATACOUNT
, abstractcs
.datacount
);
312 result
= set_field(result
, DMI_ABSTRACTCS_PROGSIZE
, abstractcs
.progsize
);
314 case DMI_ABSTRACTAUTO
:
315 result
= set_field(result
, DMI_ABSTRACTAUTO_AUTOEXECPROGBUF
, abstractauto
.autoexecprogbuf
);
316 result
= set_field(result
, DMI_ABSTRACTAUTO_AUTOEXECDATA
, abstractauto
.autoexecdata
);
322 result
= set_field(result
, DMI_HARTINFO_NSCRATCH
, 1);
323 result
= set_field(result
, DMI_HARTINFO_DATAACCESS
, 1);
324 result
= set_field(result
, DMI_HARTINFO_DATASIZE
, abstractcs
.datacount
);
325 result
= set_field(result
, DMI_HARTINFO_DATAADDR
, debug_data_start
);
329 D(fprintf(stderr
, "Unexpected. Returning Error."));
333 D(fprintf(stderr
, "0x%x\n", result
));
338 bool debug_module_t::perform_abstract_command()
340 if (abstractcs
.cmderr
!= CMDERR_NONE
)
342 if (abstractcs
.busy
) {
343 abstractcs
.cmderr
= CMDERR_BUSY
;
347 if ((command
>> 24) == 0) {
349 unsigned size
= get_field(command
, AC_ACCESS_REGISTER_SIZE
);
350 bool write
= get_field(command
, AC_ACCESS_REGISTER_WRITE
);
351 unsigned regno
= get_field(command
, AC_ACCESS_REGISTER_REGNO
);
353 if (!halted
[dmcontrol
.hartsel
]) {
354 abstractcs
.cmderr
= CMDERR_HALTRESUME
;
358 if (get_field(command
, AC_ACCESS_REGISTER_TRANSFER
)) {
360 if (regno
< 0x1000 || regno
>= 0x1020) {
361 abstractcs
.cmderr
= CMDERR_NOTSUP
;
365 unsigned regnum
= regno
- 0x1000;
370 write32(debug_abstract
, 0, lw(regnum
, ZERO
, debug_data_start
));
372 write32(debug_abstract
, 0, sw(regnum
, ZERO
, debug_data_start
));
376 write32(debug_abstract
, 0, ld(regnum
, ZERO
, debug_data_start
));
378 write32(debug_abstract
, 0, sd(regnum
, ZERO
, debug_data_start
));
383 write32(debug_rom_code, 0, lq(regnum, ZERO, debug_data_start));
385 write32(debug_rom_code, 0, sq(regnum, ZERO, debug_data_start));
389 abstractcs
.cmderr
= CMDERR_NOTSUP
;
394 write32(debug_abstract
, 0, addi(ZERO
, ZERO
, 0));
397 if (get_field(command
, AC_ACCESS_REGISTER_POSTEXEC
)) {
398 // Since the next instruction is what we will use, just use nother NOP
400 write32(debug_abstract
, 1, addi(ZERO
, ZERO
, 0));
402 write32(debug_abstract
, 1, ebreak());
405 debug_rom_flags
[dmcontrol
.hartsel
] |= 1 << DEBUG_ROM_FLAG_GO
;
407 abstractcs
.busy
= true;
409 abstractcs
.cmderr
= CMDERR_NOTSUP
;
414 bool debug_module_t::dmi_write(unsigned address
, uint32_t value
)
416 D(fprintf(stderr
, "dmi_write(0x%x, 0x%x)\n", address
, value
));
417 if (address
>= DMI_DATA0
&& address
< DMI_DATA0
+ abstractcs
.datacount
) {
418 unsigned i
= address
- DMI_DATA0
;
419 if (!abstractcs
.busy
)
420 write32(dmdata
, address
- DMI_DATA0
, value
);
422 if (abstractcs
.busy
&& abstractcs
.cmderr
== CMDERR_NONE
) {
423 abstractcs
.cmderr
= CMDERR_BUSY
;
426 if (!abstractcs
.busy
&& ((abstractauto
.autoexecdata
>> i
) & 1)) {
427 perform_abstract_command();
431 } else if (address
>= DMI_PROGBUF0
&& address
< DMI_PROGBUF0
+ progsize
) {
432 unsigned i
= address
- DMI_PROGBUF0
;
434 if (!abstractcs
.busy
)
435 write32(program_buffer
, i
, value
);
437 if (!abstractcs
.busy
&& ((abstractauto
.autoexecprogbuf
>> i
) & 1)) {
438 perform_abstract_command();
446 dmcontrol
.dmactive
= get_field(value
, DMI_DMCONTROL_DMACTIVE
);
447 if (dmcontrol
.dmactive
) {
448 dmcontrol
.haltreq
= get_field(value
, DMI_DMCONTROL_HALTREQ
);
449 dmcontrol
.resumereq
= get_field(value
, DMI_DMCONTROL_RESUMEREQ
);
450 dmcontrol
.hartreset
= get_field(value
, DMI_DMCONTROL_HARTRESET
);
451 dmcontrol
.ndmreset
= get_field(value
, DMI_DMCONTROL_NDMRESET
);
452 dmcontrol
.hartsel
= get_field(value
, DMI_DMCONTROL_HARTSEL
);
456 processor_t
*proc
= current_proc();
458 proc
->halt_request
= dmcontrol
.haltreq
;
459 if (dmcontrol
.resumereq
) {
460 debug_rom_flags
[dmcontrol
.hartsel
] |= (1 << DEBUG_ROM_FLAG_RESUME
);
461 resumeack
[dmcontrol
.hartsel
] = false;
463 if (dmcontrol
.hartreset
) {
467 if (dmcontrol
.ndmreset
) {
468 for (size_t i
= 0; i
< sim
->nprocs(); i
++) {
469 proc
= sim
->get_core(i
);
478 return perform_abstract_command();
481 abstractcs
.cmderr
= (cmderr_t
) (((uint32_t) (abstractcs
.cmderr
)) & (~(uint32_t)(get_field(value
, DMI_ABSTRACTCS_CMDERR
))));
484 case DMI_ABSTRACTAUTO
:
485 abstractauto
.autoexecprogbuf
= get_field(value
,
486 DMI_ABSTRACTAUTO_AUTOEXECPROGBUF
);
487 abstractauto
.autoexecdata
= get_field(value
,
488 DMI_ABSTRACTAUTO_AUTOEXECDATA
);