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
, unsigned progsize
) :
21 program_buffer_bytes(4 + 4*progsize
),
22 debug_progbuf_start(debug_data_start
- program_buffer_bytes
),
23 debug_abstract_start(debug_progbuf_start
- debug_abstract_size
*4),
29 dmstatus
.authenticated
= 1;
30 dmstatus
.versionlo
= 2;
33 abstractcs
.progsize
= progsize
;
37 program_buffer
= new uint8_t[program_buffer_bytes
];
39 memset(halted
, 0, sizeof(halted
));
40 memset(debug_rom_flags
, 0, sizeof(debug_rom_flags
));
41 memset(resumeack
, 0, sizeof(resumeack
));
42 memset(program_buffer
, 0, program_buffer_bytes
);
43 program_buffer
[progsize
] = ebreak();
44 memset(dmdata
, 0, sizeof(dmdata
));
46 write32(debug_rom_whereto
, 0,
47 jal(ZERO
, debug_abstract_start
- DEBUG_ROM_WHERETO
));
49 memset(debug_abstract
, 0, sizeof(debug_abstract
));
52 debug_module_t::~debug_module_t()
54 delete[] program_buffer
;
57 void debug_module_t::reset()
59 for (unsigned i
= 0; i
< sim
->nprocs(); i
++) {
60 processor_t
*proc
= sim
->get_core(i
);
62 proc
->halt_request
= false;
68 dmstatus
.authenticated
= 1;
69 dmstatus
.versionlo
= 2;
72 abstractcs
.datacount
= sizeof(dmdata
) / 4;
73 abstractcs
.progsize
= progsize
;
78 void debug_module_t::add_device(bus_t
*bus
) {
79 bus
->add_device(DEBUG_START
, this);
82 bool debug_module_t::load(reg_t addr
, size_t len
, uint8_t* bytes
)
84 addr
= DEBUG_START
+ addr
;
86 if (addr
>= DEBUG_ROM_ENTRY
&&
87 (addr
+ len
) <= (DEBUG_ROM_ENTRY
+ debug_rom_raw_len
)) {
88 memcpy(bytes
, debug_rom_raw
+ addr
- DEBUG_ROM_ENTRY
, len
);
92 if (addr
>= DEBUG_ROM_WHERETO
&& (addr
+ len
) <= (DEBUG_ROM_WHERETO
+ 4)) {
93 memcpy(bytes
, debug_rom_whereto
+ addr
- DEBUG_ROM_WHERETO
, len
);
97 if (addr
>= DEBUG_ROM_FLAGS
&& ((addr
+ len
) <= DEBUG_ROM_FLAGS
+ 1024)) {
98 memcpy(bytes
, debug_rom_flags
+ addr
- DEBUG_ROM_FLAGS
, len
);
102 if (addr
>= debug_abstract_start
&& ((addr
+ len
) <= (debug_abstract_start
+ sizeof(debug_abstract
)))) {
103 memcpy(bytes
, debug_abstract
+ addr
- debug_abstract_start
, len
);
107 if (addr
>= debug_data_start
&& (addr
+ len
) <= (debug_data_start
+ sizeof(dmdata
))) {
108 memcpy(bytes
, dmdata
+ addr
- debug_data_start
, len
);
112 if (addr
>= debug_progbuf_start
&& ((addr
+ len
) <= (debug_progbuf_start
+ program_buffer_bytes
))) {
113 memcpy(bytes
, program_buffer
+ addr
- debug_progbuf_start
, len
);
117 fprintf(stderr
, "ERROR: invalid load from debug module: %zd bytes at 0x%016"
118 PRIx64
"\n", len
, addr
);
123 bool debug_module_t::store(reg_t addr
, size_t len
, const uint8_t* bytes
)
128 fprintf(stderr
, "store(addr=0x%lx, len=%d, bytes=0x%08x); "
129 "hartsel=0x%x\n", addr
, (unsigned) len
, *(uint32_t *) bytes
,
133 fprintf(stderr
, "store(addr=0x%lx, len=%d, bytes=...); "
134 "hartsel=0x%x\n", addr
, (unsigned) len
, dmcontrol
.hartsel
);
142 memcpy(id_bytes
, bytes
, 4);
143 id
= read32(id_bytes
, 0);
146 addr
= DEBUG_START
+ addr
;
148 if (addr
>= debug_data_start
&& (addr
+ len
) <= (debug_data_start
+ sizeof(dmdata
))) {
149 memcpy(dmdata
+ addr
- debug_data_start
, bytes
, len
);
153 if (addr
>= debug_progbuf_start
&& ((addr
+ len
) <= (debug_progbuf_start
+ program_buffer_bytes
))) {
154 memcpy(program_buffer
+ addr
- debug_progbuf_start
, bytes
, len
);
159 if (addr
== DEBUG_ROM_HALTED
) {
162 if (dmcontrol
.hartsel
== id
) {
163 if (0 == (debug_rom_flags
[id
] & (1 << DEBUG_ROM_FLAG_GO
))){
164 if (dmcontrol
.hartsel
== id
) {
165 abstractcs
.busy
= false;
172 if (addr
== DEBUG_ROM_GOING
) {
173 debug_rom_flags
[dmcontrol
.hartsel
] &= ~(1 << DEBUG_ROM_FLAG_GO
);
177 if (addr
== DEBUG_ROM_RESUMING
) {
180 resumeack
[id
] = true;
181 debug_rom_flags
[id
] &= ~(1 << DEBUG_ROM_FLAG_RESUME
);
185 if (addr
== DEBUG_ROM_EXCEPTION
) {
186 if (abstractcs
.cmderr
== CMDERR_NONE
) {
187 abstractcs
.cmderr
= CMDERR_EXCEPTION
;
192 fprintf(stderr
, "ERROR: invalid store to debug module: %zd bytes at 0x%016"
193 PRIx64
"\n", len
, addr
);
197 void debug_module_t::write32(uint8_t *memory
, unsigned int index
, uint32_t value
)
199 uint8_t* base
= memory
+ index
* 4;
200 base
[0] = value
& 0xff;
201 base
[1] = (value
>> 8) & 0xff;
202 base
[2] = (value
>> 16) & 0xff;
203 base
[3] = (value
>> 24) & 0xff;
206 uint32_t debug_module_t::read32(uint8_t *memory
, unsigned int index
)
208 uint8_t* base
= memory
+ index
* 4;
209 uint32_t value
= ((uint32_t) base
[0]) |
210 (((uint32_t) base
[1]) << 8) |
211 (((uint32_t) base
[2]) << 16) |
212 (((uint32_t) base
[3]) << 24);
216 processor_t
*debug_module_t::current_proc() const
218 processor_t
*proc
= NULL
;
220 proc
= sim
->get_core(dmcontrol
.hartsel
);
221 } catch (const std::out_of_range
&) {
226 bool debug_module_t::dmi_read(unsigned address
, uint32_t *value
)
229 D(fprintf(stderr
, "dmi_read(0x%x) -> ", address
));
230 if (address
>= DMI_DATA0
&& address
< DMI_DATA0
+ abstractcs
.datacount
) {
231 unsigned i
= address
- DMI_DATA0
;
232 result
= read32(dmdata
, i
);
233 if (abstractcs
.busy
) {
235 fprintf(stderr
, "\ndmi_read(0x%02x (data[%d]) -> -1 because abstractcs.busy==true\n", address
, i
);
238 if (abstractcs
.busy
&& abstractcs
.cmderr
== CMDERR_NONE
) {
239 abstractcs
.cmderr
= CMDERR_BUSY
;
242 if (!abstractcs
.busy
&& ((abstractauto
.autoexecdata
>> i
) & 1)) {
243 perform_abstract_command();
245 } else if (address
>= DMI_PROGBUF0
&& address
< DMI_PROGBUF0
+ progsize
) {
246 unsigned i
= address
- DMI_PROGBUF0
;
247 result
= read32(program_buffer
, i
);
248 if (abstractcs
.busy
) {
250 fprintf(stderr
, "\ndmi_read(0x%02x (progbuf[%d]) -> -1 because abstractcs.busy==true\n", address
, i
);
252 if (!abstractcs
.busy
&& ((abstractauto
.autoexecprogbuf
>> i
) & 1)) {
253 perform_abstract_command();
260 processor_t
*proc
= current_proc();
262 dmcontrol
.haltreq
= proc
->halt_request
;
264 result
= set_field(result
, DMI_DMCONTROL_HALTREQ
, dmcontrol
.haltreq
);
265 result
= set_field(result
, DMI_DMCONTROL_RESUMEREQ
, dmcontrol
.resumereq
);
266 result
= set_field(result
, DMI_DMCONTROL_HARTSEL
, dmcontrol
.hartsel
);
267 result
= set_field(result
, DMI_DMCONTROL_HARTRESET
, dmcontrol
.hartreset
);
268 result
= set_field(result
, DMI_DMCONTROL_NDMRESET
, dmcontrol
.ndmreset
);
269 result
= set_field(result
, DMI_DMCONTROL_DMACTIVE
, dmcontrol
.dmactive
);
274 processor_t
*proc
= current_proc();
276 dmstatus
.allnonexistant
= false;
277 dmstatus
.allunavail
= false;
278 dmstatus
.allrunning
= false;
279 dmstatus
.allhalted
= false;
280 dmstatus
.allresumeack
= false;
282 if (halted
[dmcontrol
.hartsel
]) {
283 dmstatus
.allhalted
= true;
285 dmstatus
.allrunning
= true;
288 dmstatus
.allnonexistant
= true;
290 dmstatus
.anynonexistant
= dmstatus
.allnonexistant
;
291 dmstatus
.anyunavail
= dmstatus
.allunavail
;
292 dmstatus
.anyrunning
= dmstatus
.allrunning
;
293 dmstatus
.anyhalted
= dmstatus
.allhalted
;
295 if (resumeack
[dmcontrol
.hartsel
]) {
296 dmstatus
.allresumeack
= true;
298 dmstatus
.allresumeack
= false;
301 dmstatus
.allresumeack
= false;
304 result
= set_field(result
, DMI_DMSTATUS_ALLNONEXISTENT
, dmstatus
.allnonexistant
);
305 result
= set_field(result
, DMI_DMSTATUS_ALLUNAVAIL
, dmstatus
.allunavail
);
306 result
= set_field(result
, DMI_DMSTATUS_ALLRUNNING
, dmstatus
.allrunning
);
307 result
= set_field(result
, DMI_DMSTATUS_ALLHALTED
, dmstatus
.allhalted
);
308 result
= set_field(result
, DMI_DMSTATUS_ALLRESUMEACK
, dmstatus
.allresumeack
);
309 result
= set_field(result
, DMI_DMSTATUS_ANYNONEXISTENT
, dmstatus
.anynonexistant
);
310 result
= set_field(result
, DMI_DMSTATUS_ANYUNAVAIL
, dmstatus
.anyunavail
);
311 result
= set_field(result
, DMI_DMSTATUS_ANYRUNNING
, dmstatus
.anyrunning
);
312 result
= set_field(result
, DMI_DMSTATUS_ANYHALTED
, dmstatus
.anyhalted
);
313 result
= set_field(result
, DMI_DMSTATUS_ANYRESUMEACK
, dmstatus
.anyresumeack
);
314 result
= set_field(result
, DMI_DMSTATUS_AUTHENTICATED
, dmstatus
.authenticated
);
315 result
= set_field(result
, DMI_DMSTATUS_AUTHBUSY
, dmstatus
.authbusy
);
316 result
= set_field(result
, DMI_DMSTATUS_VERSIONHI
, dmstatus
.versionhi
);
317 result
= set_field(result
, DMI_DMSTATUS_VERSIONLO
, dmstatus
.versionlo
);
321 result
= set_field(result
, DMI_ABSTRACTCS_CMDERR
, abstractcs
.cmderr
);
322 result
= set_field(result
, DMI_ABSTRACTCS_BUSY
, abstractcs
.busy
);
323 result
= set_field(result
, DMI_ABSTRACTCS_DATACOUNT
, abstractcs
.datacount
);
324 result
= set_field(result
, DMI_ABSTRACTCS_PROGSIZE
, abstractcs
.progsize
);
326 case DMI_ABSTRACTAUTO
:
327 result
= set_field(result
, DMI_ABSTRACTAUTO_AUTOEXECPROGBUF
, abstractauto
.autoexecprogbuf
);
328 result
= set_field(result
, DMI_ABSTRACTAUTO_AUTOEXECDATA
, abstractauto
.autoexecdata
);
334 result
= set_field(result
, DMI_HARTINFO_NSCRATCH
, 1);
335 result
= set_field(result
, DMI_HARTINFO_DATAACCESS
, 1);
336 result
= set_field(result
, DMI_HARTINFO_DATASIZE
, abstractcs
.datacount
);
337 result
= set_field(result
, DMI_HARTINFO_DATAADDR
, debug_data_start
);
341 D(fprintf(stderr
, "Unexpected. Returning Error."));
345 D(fprintf(stderr
, "0x%x\n", result
));
350 bool debug_module_t::perform_abstract_command()
352 if (abstractcs
.cmderr
!= CMDERR_NONE
)
354 if (abstractcs
.busy
) {
355 abstractcs
.cmderr
= CMDERR_BUSY
;
359 if ((command
>> 24) == 0) {
361 unsigned size
= get_field(command
, AC_ACCESS_REGISTER_SIZE
);
362 bool write
= get_field(command
, AC_ACCESS_REGISTER_WRITE
);
363 unsigned regno
= get_field(command
, AC_ACCESS_REGISTER_REGNO
);
365 if (!halted
[dmcontrol
.hartsel
]) {
366 abstractcs
.cmderr
= CMDERR_HALTRESUME
;
370 if (get_field(command
, AC_ACCESS_REGISTER_TRANSFER
)) {
372 if (regno
< 0x1000 || regno
>= 0x1020) {
373 abstractcs
.cmderr
= CMDERR_NOTSUP
;
377 unsigned regnum
= regno
- 0x1000;
382 write32(debug_abstract
, 0, lw(regnum
, ZERO
, debug_data_start
));
384 write32(debug_abstract
, 0, sw(regnum
, ZERO
, debug_data_start
));
388 write32(debug_abstract
, 0, ld(regnum
, ZERO
, debug_data_start
));
390 write32(debug_abstract
, 0, sd(regnum
, ZERO
, debug_data_start
));
395 write32(debug_rom_code, 0, lq(regnum, ZERO, debug_data_start));
397 write32(debug_rom_code, 0, sq(regnum, ZERO, debug_data_start));
401 abstractcs
.cmderr
= CMDERR_NOTSUP
;
406 write32(debug_abstract
, 0, addi(ZERO
, ZERO
, 0));
409 if (get_field(command
, AC_ACCESS_REGISTER_POSTEXEC
)) {
410 // Since the next instruction is what we will use, just use nother NOP
412 write32(debug_abstract
, 1, addi(ZERO
, ZERO
, 0));
414 write32(debug_abstract
, 1, ebreak());
417 debug_rom_flags
[dmcontrol
.hartsel
] |= 1 << DEBUG_ROM_FLAG_GO
;
419 abstractcs
.busy
= true;
421 abstractcs
.cmderr
= CMDERR_NOTSUP
;
426 bool debug_module_t::dmi_write(unsigned address
, uint32_t value
)
428 D(fprintf(stderr
, "dmi_write(0x%x, 0x%x)\n", address
, value
));
429 if (address
>= DMI_DATA0
&& address
< DMI_DATA0
+ abstractcs
.datacount
) {
430 unsigned i
= address
- DMI_DATA0
;
431 if (!abstractcs
.busy
)
432 write32(dmdata
, address
- DMI_DATA0
, value
);
434 if (abstractcs
.busy
&& abstractcs
.cmderr
== CMDERR_NONE
) {
435 abstractcs
.cmderr
= CMDERR_BUSY
;
438 if (!abstractcs
.busy
&& ((abstractauto
.autoexecdata
>> i
) & 1)) {
439 perform_abstract_command();
443 } else if (address
>= DMI_PROGBUF0
&& address
< DMI_PROGBUF0
+ progsize
) {
444 unsigned i
= address
- DMI_PROGBUF0
;
446 if (!abstractcs
.busy
)
447 write32(program_buffer
, i
, value
);
449 if (!abstractcs
.busy
&& ((abstractauto
.autoexecprogbuf
>> i
) & 1)) {
450 perform_abstract_command();
458 dmcontrol
.dmactive
= get_field(value
, DMI_DMCONTROL_DMACTIVE
);
459 if (dmcontrol
.dmactive
) {
460 dmcontrol
.haltreq
= get_field(value
, DMI_DMCONTROL_HALTREQ
);
461 dmcontrol
.resumereq
= get_field(value
, DMI_DMCONTROL_RESUMEREQ
);
462 dmcontrol
.hartreset
= get_field(value
, DMI_DMCONTROL_HARTRESET
);
463 dmcontrol
.ndmreset
= get_field(value
, DMI_DMCONTROL_NDMRESET
);
464 dmcontrol
.hartsel
= get_field(value
, DMI_DMCONTROL_HARTSEL
);
468 processor_t
*proc
= current_proc();
470 proc
->halt_request
= dmcontrol
.haltreq
;
471 if (dmcontrol
.resumereq
) {
472 debug_rom_flags
[dmcontrol
.hartsel
] |= (1 << DEBUG_ROM_FLAG_RESUME
);
473 resumeack
[dmcontrol
.hartsel
] = false;
475 if (dmcontrol
.hartreset
) {
479 if (dmcontrol
.ndmreset
) {
480 for (size_t i
= 0; i
< sim
->nprocs(); i
++) {
481 proc
= sim
->get_core(i
);
490 return perform_abstract_command();
493 abstractcs
.cmderr
= (cmderr_t
) (((uint32_t) (abstractcs
.cmderr
)) & (~(uint32_t)(get_field(value
, DMI_ABSTRACTCS_CMDERR
))));
496 case DMI_ABSTRACTAUTO
:
497 abstractauto
.autoexecprogbuf
= get_field(value
,
498 DMI_ABSTRACTAUTO_AUTOEXECPROGBUF
);
499 abstractauto
.autoexecdata
= get_field(value
,
500 DMI_ABSTRACTAUTO_AUTOEXECDATA
);