#include "mmu.h"
#include "debug_rom/debug_rom.h"
+#include "debug_rom/debug_rom_defines.h"
#if 1
# define D(x) x
# define D(x)
#endif
-///////////////////////// debug_module_data_t
-
-debug_module_data_t::debug_module_data_t()
-{
- memset(data, 0, sizeof(data));
-}
-
-bool debug_module_data_t::load(reg_t addr, size_t len, uint8_t* bytes)
-{
- if (addr + len < sizeof(data)) {
- memcpy(bytes, data + addr, len);
- return true;
- }
-
- fprintf(stderr, "ERROR: invalid load from debug_module_data_t: %zd bytes at 0x%016"
- PRIx64 "\n", len, addr);
-
- return false;
-}
-
-bool debug_module_data_t::store(reg_t addr, size_t len, const uint8_t* bytes)
-{
- D(fprintf(stderr, "debug_module_data_t store 0x%lx bytes at 0x%lx\n", len,
- addr));
-
- if (addr + len < sizeof(data)) {
- memcpy(data + addr, bytes, len);
- return true;
- }
-
- fprintf(stderr, "ERROR: invalid store to debug_module_data_t: %zd bytes at 0x%016"
- PRIx64 "\n", len, addr);
- return false;
-}
-
-uint32_t debug_module_data_t::read32(reg_t addr) const
-{
- assert(addr + 4 <= sizeof(data));
- return data[addr] |
- (data[addr + 1] << 8) |
- (data[addr + 2] << 16) |
- (data[addr + 3] << 24);
-}
-
-void debug_module_data_t::write32(reg_t addr, uint32_t value)
-{
- fprintf(stderr, "debug_module_data_t::write32(0x%lx, 0x%x)\n", addr, value);
- assert(addr + 4 <= sizeof(data));
- data[addr] = value & 0xff;
- data[addr + 1] = (value >> 8) & 0xff;
- data[addr + 2] = (value >> 16) & 0xff;
- data[addr + 3] = (value >> 24) & 0xff;
-}
-
///////////////////////// debug_module_t
-debug_module_t::debug_module_t(sim_t *sim) : sim(sim),
- next_action(jal(ZERO, 0)),
- action_executed(false)
+debug_module_t::debug_module_t(sim_t *sim) : sim(sim)
{
dmcontrol = {0};
abstractauto = {0};
- for (unsigned i = 0; i < DEBUG_ROM_ENTRY_SIZE / 4; i++) {
- write32(debug_rom_entry, i, jal(ZERO, 0));
- halted[i] = false;
- }
-
+ memset(halted, 0, sizeof(halted));
+ memset(debug_rom_flags, 0, sizeof(debug_rom_flags));
+ memset(resumeack, 0, sizeof(resumeack));
memset(program_buffer, 0, sizeof(program_buffer));
+ memset(dmdata, 0, sizeof(dmdata));
+
+ write32(debug_rom_whereto, 0,
+ jal(ZERO, DEBUG_ABSTRACT_START - DEBUG_ROM_WHERETO));
+
+ memset(debug_rom_abstract, 0, sizeof(debug_rom_abstract));
+
}
void debug_module_t::reset()
dmstatus.versionlo = 2;
abstractcs = {0};
- abstractcs.datacount = sizeof(dmdata.data) / 4;
+ abstractcs.datacount = sizeof(dmdata) / 4;
abstractcs.progsize = progsize;
abstractauto = {0};
void debug_module_t::add_device(bus_t *bus) {
bus->add_device(DEBUG_START, this);
- bus->add_device(DEBUG_EXCHANGE, &dmdata);
}
bool debug_module_t::load(reg_t addr, size_t len, uint8_t* bytes)
addr = DEBUG_START + addr;
if (addr >= DEBUG_ROM_ENTRY &&
- addr < DEBUG_ROM_ENTRY + DEBUG_ROM_ENTRY_SIZE) {
-
- if (read32(debug_rom_entry, dmcontrol.hartsel) == jal(ZERO, 0)) {
- // We're here in an infinite loop. That means that whatever abstract
- // command has complete.
- abstractcs.busy = false;
- }
-
- action_executed = true;
-
- halted[(addr - DEBUG_ROM_ENTRY) / 4] = true;
- memcpy(bytes, debug_rom_entry + addr - DEBUG_ROM_ENTRY, len);
+ addr < DEBUG_ROM_ENTRY + debug_rom_raw_len) {
+ memcpy(bytes, debug_rom_raw + addr - DEBUG_ROM_ENTRY, len);
return true;
}
- if (action_executed) {
- // Restore the jump-to-self loop.
- write32(debug_rom_entry, dmcontrol.hartsel, next_action);
- next_action = jal(ZERO, 0);
- action_executed = false;
+ if (addr >= DEBUG_ROM_WHERETO && addr < DEBUG_ROM_WHERETO + 4) {
+ memcpy(bytes, debug_rom_whereto + addr - DEBUG_ROM_WHERETO, len);
+ return true;
}
- if (addr >= DEBUG_ROM_CODE &&
- addr < DEBUG_ROM_CODE + DEBUG_ROM_CODE_SIZE) {
-
- if (read32(debug_rom_code, 0) == dret()) {
- abstractcs.busy = false;
- halted[dmcontrol.hartsel] = false;
- }
-
- memcpy(bytes, debug_rom_code + addr - DEBUG_ROM_CODE, len);
+ if (addr >= DEBUG_ROM_FLAGS && addr < DEBUG_ROM_FLAGS + 1024) {
+ memcpy(bytes, debug_rom_flags + addr - DEBUG_ROM_FLAGS, len);
return true;
}
- if (addr >= DEBUG_RAM_START && addr < DEBUG_RAM_END) {
- memcpy(bytes, program_buffer + addr - DEBUG_RAM_START, len);
+ if (addr >= DEBUG_ABSTRACT_START && addr < DEBUG_ABSTRACT_END) {
+ memcpy(bytes, debug_rom_abstract + addr - DEBUG_ABSTRACT_START, len);
return true;
}
- if (addr >= DEBUG_ROM_EXCEPTION &&
- addr < DEBUG_ROM_EXCEPTION + DEBUG_ROM_EXCEPTION_SIZE) {
- memcpy(bytes, debug_rom_exception + addr - DEBUG_ROM_EXCEPTION, len);
- if (abstractcs.cmderr == abstractcs.CMDERR_NONE) {
- abstractcs.cmderr = abstractcs.CMDERR_EXCEPTION;
- }
+ if (addr >= DEBUG_DATA_START && addr < DEBUG_DATA_END) {
+ memcpy(bytes, dmdata + addr - DEBUG_DATA_START, len);
+ return true;
+ }
+
+ if (addr >= DEBUG_PROGBUF_START && addr < DEBUG_PROGBUF_END) {
+ memcpy(bytes, program_buffer + addr - DEBUG_PROGBUF_START, len);
return true;
}
bool debug_module_t::store(reg_t addr, size_t len, const uint8_t* bytes)
{
+
+ uint8_t id_bytes[4];
+ uint32_t id = 0;
+ if (len == 4) {
+ memcpy(id_bytes, bytes, 4);
+ id = read32(id_bytes, 0);
+ }
+
addr = DEBUG_START + addr;
+
+ if (addr >= DEBUG_DATA_START && addr < DEBUG_DATA_END) {
+ memcpy(dmdata + addr - DEBUG_DATA_START, bytes, len);
+ return true;
+ }
+
+ if (addr >= DEBUG_PROGBUF_START && addr < DEBUG_PROGBUF_END) {
+ memcpy(program_buffer + addr - DEBUG_PROGBUF_START, bytes, len);
+ return true;
+ }
+
+ if (addr == DEBUG_ROM_HALTED) {
+ assert (len == 4);
+ halted[id] = true;
+ if (dmcontrol.hartsel == id) {
+ if (0 == (debug_rom_flags[id] & (1 << DEBUG_ROM_FLAG_GO))){
+ if (dmcontrol.hartsel == id) {
+ abstractcs.busy = false;
+ }
+ }
+ }
+ return true;
+ }
- if (addr >= DEBUG_RAM_START && addr < DEBUG_RAM_END) {
- memcpy(program_buffer + addr - DEBUG_RAM_START, bytes, len);
+ if (addr == DEBUG_ROM_GOING) {
+ debug_rom_flags[dmcontrol.hartsel] &= ~(1 << DEBUG_ROM_FLAG_GO);
+ return true;
+ }
+
+ if (addr == DEBUG_ROM_RESUMING) {
+ assert (len == 4);
+ halted[id] = false;
+ resumeack[id] = true;
+ debug_rom_flags[id] &= ~(1 << DEBUG_ROM_FLAG_RESUME);
+ return true;
+ }
+
+ if (addr == DEBUG_ROM_EXCEPTION) {
+ if (abstractcs.cmderr == CMDERR_NONE) {
+ abstractcs.cmderr = CMDERR_EXCEPTION;
+ }
return true;
}
D(fprintf(stderr, "dmi_read(0x%x) -> ", address));
if (address >= DMI_DATA0 && address < DMI_DATA0 + abstractcs.datacount) {
unsigned i = address - DMI_DATA0;
- result = dmdata.read32(4 * i);
+ result = read32(dmdata, i);
- if (abstractcs.busy && abstractcs.cmderr == abstractcs.CMDERR_NONE) {
- abstractcs.cmderr = abstractcs.CMDERR_BUSY;
+ if (abstractcs.busy && abstractcs.cmderr == CMDERR_NONE) {
+ abstractcs.cmderr = CMDERR_BUSY;
}
- if ((abstractauto.autoexecdata >> i) & 1)
+ if ((abstractauto.autoexecdata >> i) & 1){
perform_abstract_command();
+ }
} else if (address >= DMI_PROGBUF0 && address < DMI_PROGBUF0 + progsize) {
- result = read32(program_buffer, address - DMI_PROGBUF0);
+ unsigned i = address = DMI_PROGBUF0;
+ result = read32(program_buffer, i);
+ if ((abstractauto.autoexecprogbuf >> i) & 1) {
+ perform_abstract_command();
+ }
+
} else {
switch (address) {
case DMI_DMCONTROL:
dmstatus.allunavail = false;
dmstatus.allrunning = false;
dmstatus.allhalted = false;
+ dmstatus.allresumeack = false;
if (proc) {
if (halted[dmcontrol.hartsel]) {
dmstatus.allhalted = true;
dmstatus.anyunavail = dmstatus.allunavail;
dmstatus.anyrunning = dmstatus.allrunning;
dmstatus.anyhalted = dmstatus.allhalted;
-
+ if (proc) {
+ if (resumeack[dmcontrol.hartsel]) {
+ dmstatus.allresumeack = true;
+ } else {
+ dmstatus.allresumeack = false;
+ }
+ } else {
+ dmstatus.allresumeack = false;
+ }
+
result = set_field(result, DMI_DMSTATUS_ALLNONEXISTENT, dmstatus.allnonexistant);
result = set_field(result, DMI_DMSTATUS_ALLUNAVAIL, dmstatus.allunavail);
result = set_field(result, DMI_DMSTATUS_ALLRUNNING, dmstatus.allrunning);
result = set_field(result, DMI_DMSTATUS_ALLHALTED, dmstatus.allhalted);
+ result = set_field(result, DMI_DMSTATUS_ALLRESUMEACK, dmstatus.allresumeack);
result = set_field(result, DMI_DMSTATUS_ANYNONEXISTENT, dmstatus.anynonexistant);
result = set_field(result, DMI_DMSTATUS_ANYUNAVAIL, dmstatus.anyunavail);
result = set_field(result, DMI_DMSTATUS_ANYRUNNING, dmstatus.anyrunning);
result = set_field(result, DMI_DMSTATUS_ANYHALTED, dmstatus.anyhalted);
+ result = set_field(result, DMI_DMSTATUS_ANYRESUMEACK, dmstatus.anyresumeack);
result = set_field(result, DMI_DMSTATUS_AUTHENTICATED, dmstatus.authenticated);
result = set_field(result, DMI_DMSTATUS_AUTHBUSY, dmstatus.authbusy);
result = set_field(result, DMI_DMSTATUS_VERSIONHI, dmstatus.versionhi);
case DMI_COMMAND:
result = 0;
break;
+ case DMI_HARTINFO:
+ result = set_field(result, DMI_HARTINFO_NSCRATCH, 1);
+ result = set_field(result, DMI_HARTINFO_DATAACCESS, 1);
+ result = set_field(result, DMI_HARTINFO_DATASIZE, abstractcs.datacount);
+ result = set_field(result, DMI_HARTINFO_DATAADDR, DEBUG_DATA_START);
+ break;
default:
- D(fprintf(stderr, "error\n"));
+ result = 0;
+ D(fprintf(stderr, "Unexpected. Returning Error."));
return false;
}
}
bool debug_module_t::perform_abstract_command()
{
- if (abstractcs.cmderr != abstractcs.CMDERR_NONE)
+ if (abstractcs.cmderr != CMDERR_NONE)
return true;
if (abstractcs.busy) {
- abstractcs.cmderr = abstractcs.CMDERR_BUSY;
+ abstractcs.cmderr = CMDERR_BUSY;
return true;
}
bool write = get_field(command, AC_ACCESS_REGISTER_WRITE);
unsigned regno = get_field(command, AC_ACCESS_REGISTER_REGNO);
- if (regno < 0x1000 || regno >= 0x1020) {
- abstractcs.cmderr = abstractcs.CMDERR_NOTSUP;
+ if (!halted[dmcontrol.hartsel]) {
+ abstractcs.cmderr = CMDERR_HALTRESUME;
return true;
}
- unsigned regnum = regno - 0x1000;
+ if (get_field(command, AC_ACCESS_REGISTER_TRANSFER)) {
- if (!halted[dmcontrol.hartsel]) {
- abstractcs.cmderr = abstractcs.CMDERR_HALTRESUME;
- return true;
- }
+ if (regno < 0x1000 || regno >= 0x1020) {
+ abstractcs.cmderr = CMDERR_NOTSUP;
+ return true;
+ }
+
+ unsigned regnum = regno - 0x1000;
- switch (size) {
+ switch (size) {
case 2:
if (write)
- write32(debug_rom_code, 0, lw(regnum, ZERO, DEBUG_EXCHANGE));
+ write32(debug_rom_abstract, 0, lw(regnum, ZERO, DEBUG_DATA_START));
else
- write32(debug_rom_code, 0, sw(regnum, ZERO, DEBUG_EXCHANGE));
+ write32(debug_rom_abstract, 0, sw(regnum, ZERO, DEBUG_DATA_START));
break;
case 3:
if (write)
- write32(debug_rom_code, 0, ld(regnum, ZERO, DEBUG_EXCHANGE));
- else
- write32(debug_rom_code, 0, sd(regnum, ZERO, DEBUG_EXCHANGE));
- break;
- /*
- case 4:
- if (write)
- write32(debug_rom_code, 0, lq(regnum, ZERO, DEBUG_EXCHANGE));
+ write32(debug_rom_abstract, 0, ld(regnum, ZERO, DEBUG_DATA_START));
else
- write32(debug_rom_code, 0, sq(regnum, ZERO, DEBUG_EXCHANGE));
+ write32(debug_rom_abstract, 0, sd(regnum, ZERO, DEBUG_DATA_START));
break;
+ /*
+ case 4:
+ if (write)
+ write32(debug_rom_code, 0, lq(regnum, ZERO, DEBUG_DATA_START));
+ else
+ write32(debug_rom_code, 0, sq(regnum, ZERO, DEBUG_DATA_START));
+ break;
*/
default:
- abstractcs.cmderr = abstractcs.CMDERR_NOTSUP;
+ abstractcs.cmderr = CMDERR_NOTSUP;
return true;
- }
- if (get_field(command, AC_ACCESS_REGISTER_POSTEXEC)) {
- write32(debug_rom_code, 1, jal(ZERO, DEBUG_RAM_START - DEBUG_ROM_CODE - 4));
+ }
} else {
- write32(debug_rom_code, 1, ebreak());
+ // Should be a NOP. Store DEBUG_DATA to x0.
+ write32(debug_rom_abstract, 0, sw(ZERO, ZERO, DEBUG_DATA_START));
}
-
- if (get_field(command, AC_ACCESS_REGISTER_PREEXEC)) {
- write32(debug_rom_entry, dmcontrol.hartsel,
- jal(ZERO, DEBUG_RAM_START - (DEBUG_ROM_ENTRY + 4 * dmcontrol.hartsel)));
- next_action =
- jal(ZERO, DEBUG_ROM_CODE - (DEBUG_ROM_ENTRY + 4 * dmcontrol.hartsel));
+
+ if (get_field(command, AC_ACCESS_REGISTER_POSTEXEC)) {
+ write32(debug_rom_abstract, 1, jal(ZERO, DEBUG_PROGBUF_START - DEBUG_ABSTRACT_START));
} else {
- write32(debug_rom_entry, dmcontrol.hartsel,
- jal(ZERO, DEBUG_ROM_CODE - (DEBUG_ROM_ENTRY + 4 * dmcontrol.hartsel)));
+ write32(debug_rom_abstract, 1, ebreak());
}
- write32(debug_rom_exception, dmcontrol.hartsel,
- jal(ZERO, (DEBUG_ROM_ENTRY + 4 * dmcontrol.hartsel) - DEBUG_ROM_EXCEPTION));
+ debug_rom_flags[dmcontrol.hartsel] |= 1 << DEBUG_ROM_FLAG_GO;
+
abstractcs.busy = true;
} else {
- abstractcs.cmderr = abstractcs.CMDERR_NOTSUP;
+ abstractcs.cmderr = CMDERR_NOTSUP;
}
return true;
}
D(fprintf(stderr, "dmi_write(0x%x, 0x%x)\n", address, value));
if (address >= DMI_DATA0 && address < DMI_DATA0 + abstractcs.datacount) {
unsigned i = address - DMI_DATA0;
- dmdata.write32(4 * i, value);
+ write32(dmdata, address - DMI_DATA0, value);
- if (abstractcs.busy && abstractcs.cmderr == abstractcs.CMDERR_NONE) {
- abstractcs.cmderr = abstractcs.CMDERR_BUSY;
+ if (abstractcs.busy && abstractcs.cmderr == CMDERR_NONE) {
+ abstractcs.cmderr = CMDERR_BUSY;
}
if ((abstractauto.autoexecdata >> i) & 1)
if (proc) {
proc->halt_request = dmcontrol.haltreq;
if (dmcontrol.resumereq) {
- write32(debug_rom_code, 0, dret());
- write32(debug_rom_entry, dmcontrol.hartsel,
- jal(ZERO, DEBUG_ROM_CODE - (DEBUG_ROM_ENTRY + 4 * dmcontrol.hartsel)));
- abstractcs.busy = true;
+ debug_rom_flags[dmcontrol.hartsel] |= (1 << DEBUG_ROM_FLAG_RESUME);
+ resumeack[dmcontrol.hartsel] = false;
}
}
}
return perform_abstract_command();
case DMI_ABSTRACTCS:
- if (get_field(value, DMI_ABSTRACTCS_CMDERR) == abstractcs.CMDERR_NONE) {
- abstractcs.cmderr = abstractcs.CMDERR_NONE;
- }
+ abstractcs.cmderr = (cmderr_t) (((uint32_t) (abstractcs.cmderr)) & (~(uint32_t)(get_field(value, DMI_ABSTRACTCS_CMDERR))));
return true;
case DMI_ABSTRACTAUTO: