#include "debug_rom/debug_rom.h"
#include "debug_rom/debug_rom_defines.h"
-#if 1
+#if 0
# define D(x) x
#else
# define D(x)
///////////////////////// debug_module_t
-debug_module_t::debug_module_t(sim_t *sim) : sim(sim)
+debug_module_t::debug_module_t(sim_t *sim, unsigned progbufsize) :
+ progbufsize(progbufsize),
+ program_buffer_bytes(4 + 4*progbufsize),
+ debug_progbuf_start(debug_data_start - program_buffer_bytes),
+ debug_abstract_start(debug_progbuf_start - debug_abstract_size*4),
+ sim(sim)
{
- dmcontrol = {0};
-
- dmstatus = {0};
- dmstatus.authenticated = 1;
- dmstatus.versionlo = 2;
-
- abstractcs = {0};
- abstractcs.progsize = progsize;
-
- abstractauto = {0};
+ program_buffer = new uint8_t[program_buffer_bytes];
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(program_buffer, 0, program_buffer_bytes);
+ program_buffer[4*progbufsize] = ebreak();
+ program_buffer[4*progbufsize+1] = ebreak() >> 8;
+ program_buffer[4*progbufsize+2] = ebreak() >> 16;
+ program_buffer[4*progbufsize+3] = ebreak() >> 24;
memset(dmdata, 0, sizeof(dmdata));
write32(debug_rom_whereto, 0,
jal(ZERO, debug_abstract_start - DEBUG_ROM_WHERETO));
memset(debug_abstract, 0, sizeof(debug_abstract));
-
+
+ reset();
+}
+
+debug_module_t::~debug_module_t()
+{
+ delete[] program_buffer;
}
void debug_module_t::reset()
dmcontrol = {0};
dmstatus = {0};
+ dmstatus.impebreak = true;
dmstatus.authenticated = 1;
- dmstatus.versionlo = 2;
+ dmstatus.version = 2;
abstractcs = {0};
abstractcs.datacount = sizeof(dmdata) / 4;
- abstractcs.progsize = progsize;
+ abstractcs.progbufsize = progbufsize;
abstractauto = {0};
+
+ sbcs = {0};
+ sbcs.version = 1;
+ sbcs.access64 = true;
+ sbcs.access32 = true;
+ sbcs.access16 = true;
+ sbcs.access8 = true;
+ sbcs.asize = sizeof(reg_t) * 8;
}
void debug_module_t::add_device(bus_t *bus) {
memcpy(bytes, dmdata + addr - debug_data_start, len);
return true;
}
-
- if (addr >= debug_progbuf_start && ((addr + len) <= (debug_progbuf_start + sizeof(program_buffer)))) {
+
+ if (addr >= debug_progbuf_start && ((addr + len) <= (debug_progbuf_start + program_buffer_bytes))) {
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)
{
+ D(
+ switch (len) {
+ case 4:
+ fprintf(stderr, "store(addr=0x%lx, len=%d, bytes=0x%08x); "
+ "hartsel=0x%x\n", addr, (unsigned) len, *(uint32_t *) bytes,
+ dmcontrol.hartsel);
+ break;
+ default:
+ fprintf(stderr, "store(addr=0x%lx, len=%d, bytes=...); "
+ "hartsel=0x%x\n", addr, (unsigned) len, dmcontrol.hartsel);
+ break;
+ }
+ );
uint8_t id_bytes[4];
uint32_t id = 0;
}
addr = DEBUG_START + addr;
-
+
if (addr >= debug_data_start && (addr + len) <= (debug_data_start + sizeof(dmdata))) {
memcpy(dmdata + addr - debug_data_start, bytes, len);
return true;
}
-
- if (addr >= debug_progbuf_start && ((addr + len) <= (debug_progbuf_start + sizeof(program_buffer)))) {
+
+ if (addr >= debug_progbuf_start && ((addr + len) <= (debug_progbuf_start + program_buffer_bytes))) {
memcpy(program_buffer + addr - debug_progbuf_start, bytes, len);
+
return true;
}
return proc;
}
+unsigned debug_module_t::sb_access_bits()
+{
+ return 8 << sbcs.sbaccess;
+}
+
+void debug_module_t::sb_autoincrement()
+{
+ if (!sbcs.autoincrement)
+ return;
+
+ uint64_t value = sbaddress[0] + sb_access_bits() / 8;
+ sbaddress[0] = value;
+ uint32_t carry = value >> 32;
+
+ value = sbaddress[1] + carry;
+ sbaddress[1] = value;
+ carry = value >> 32;
+
+ value = sbaddress[2] + carry;
+ sbaddress[2] = value;
+ carry = value >> 32;
+
+ sbaddress[3] += carry;
+}
+
+void debug_module_t::sb_read()
+{
+ reg_t address = ((uint64_t) sbaddress[1] << 32) | sbaddress[0];
+ D(fprintf(stderr, "sb_read() @ 0x%lx\n", address));
+ try {
+ switch (sbcs.sbaccess) {
+ case 0:
+ sbdata[0] = sim->debug_mmu->load_uint8(address);
+ break;
+ case 1:
+ sbdata[0] = sim->debug_mmu->load_uint16(address);
+ break;
+ case 2:
+ sbdata[0] = sim->debug_mmu->load_uint32(address);
+ D(fprintf(stderr, " -> 0x%x\n", sbdata[0]));
+ break;
+ case 3:
+ {
+ uint64_t value = sim->debug_mmu->load_uint32(address);
+ sbdata[0] = value;
+ sbdata[1] = value >> 32;
+ break;
+ }
+ default:
+ sbcs.error = 3;
+ break;
+ }
+ } catch (trap_load_access_fault& t) {
+ sbcs.error = 2;
+ }
+}
+
+void debug_module_t::sb_write()
+{
+ reg_t address = ((uint64_t) sbaddress[1] << 32) | sbaddress[0];
+ D(fprintf(stderr, "sb_write() 0x%x @ 0x%lx\n", sbdata[0], address));
+ switch (sbcs.sbaccess) {
+ case 0:
+ sim->debug_mmu->store_uint8(address, sbdata[0]);
+ break;
+ case 1:
+ sim->debug_mmu->store_uint16(address, sbdata[0]);
+ break;
+ case 2:
+ sim->debug_mmu->store_uint32(address, sbdata[0]);
+ break;
+ case 3:
+ sim->debug_mmu->store_uint64(address,
+ (((uint64_t) sbdata[1]) << 32) | sbdata[0]);
+ break;
+ default:
+ sbcs.error = 3;
+ break;
+ }
+}
+
bool debug_module_t::dmi_read(unsigned address, uint32_t *value)
{
uint32_t result = 0;
if (address >= DMI_DATA0 && address < DMI_DATA0 + abstractcs.datacount) {
unsigned i = address - DMI_DATA0;
result = read32(dmdata, i);
+ if (abstractcs.busy) {
+ result = -1;
+ fprintf(stderr, "\ndmi_read(0x%02x (data[%d]) -> -1 because abstractcs.busy==true\n", address, i);
+ }
if (abstractcs.busy && abstractcs.cmderr == CMDERR_NONE) {
abstractcs.cmderr = CMDERR_BUSY;
}
- if ((abstractauto.autoexecdata >> i) & 1){
+ if (!abstractcs.busy && ((abstractauto.autoexecdata >> i) & 1)) {
perform_abstract_command();
}
- } else if (address >= DMI_PROGBUF0 && address < DMI_PROGBUF0 + progsize) {
- unsigned i = address = DMI_PROGBUF0;
+ } else if (address >= DMI_PROGBUF0 && address < DMI_PROGBUF0 + progbufsize) {
+ unsigned i = address - DMI_PROGBUF0;
result = read32(program_buffer, i);
- if ((abstractauto.autoexecprogbuf >> i) & 1) {
+ if (abstractcs.busy) {
+ result = -1;
+ fprintf(stderr, "\ndmi_read(0x%02x (progbuf[%d]) -> -1 because abstractcs.busy==true\n", address, i);
+ }
+ if (!abstractcs.busy && ((abstractauto.autoexecprogbuf >> i) & 1)) {
perform_abstract_command();
}
result = set_field(result, DMI_DMCONTROL_HALTREQ, dmcontrol.haltreq);
result = set_field(result, DMI_DMCONTROL_RESUMEREQ, dmcontrol.resumereq);
- result = set_field(result, DMI_DMCONTROL_HARTSEL, dmcontrol.hartsel);
+ result = set_field(result, ((1L<<hartsellen)-1) <<
+ DMI_DMCONTROL_HARTSEL_OFFSET, dmcontrol.hartsel);
result = set_field(result, DMI_DMCONTROL_HARTRESET, dmcontrol.hartreset);
result = set_field(result, DMI_DMCONTROL_NDMRESET, dmcontrol.ndmreset);
result = set_field(result, DMI_DMCONTROL_DMACTIVE, dmcontrol.dmactive);
} else {
dmstatus.allresumeack = false;
}
-
+
+ result = set_field(result, DMI_DMSTATUS_IMPEBREAK,
+ dmstatus.impebreak);
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_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);
- result = set_field(result, DMI_DMSTATUS_VERSIONLO, dmstatus.versionlo);
+ result = set_field(result, DMI_DMSTATUS_VERSION, dmstatus.version);
}
break;
case DMI_ABSTRACTCS:
result = set_field(result, DMI_ABSTRACTCS_CMDERR, abstractcs.cmderr);
result = set_field(result, DMI_ABSTRACTCS_BUSY, abstractcs.busy);
result = set_field(result, DMI_ABSTRACTCS_DATACOUNT, abstractcs.datacount);
- result = set_field(result, DMI_ABSTRACTCS_PROGSIZE, abstractcs.progsize);
+ result = set_field(result, DMI_ABSTRACTCS_PROGBUFSIZE,
+ abstractcs.progbufsize);
break;
case DMI_ABSTRACTAUTO:
result = set_field(result, DMI_ABSTRACTAUTO_AUTOEXECPROGBUF, abstractauto.autoexecprogbuf);
result = set_field(result, DMI_HARTINFO_DATASIZE, abstractcs.datacount);
result = set_field(result, DMI_HARTINFO_DATAADDR, debug_data_start);
break;
+ case DMI_SBCS:
+ result = set_field(result, DMI_SBCS_SBVERSION, sbcs.version);
+ result = set_field(result, DMI_SBCS_SBREADONADDR, sbcs.readonaddr);
+ result = set_field(result, DMI_SBCS_SBACCESS, sbcs.sbaccess);
+ result = set_field(result, DMI_SBCS_SBAUTOINCREMENT, sbcs.autoincrement);
+ result = set_field(result, DMI_SBCS_SBREADONDATA, sbcs.readondata);
+ result = set_field(result, DMI_SBCS_SBERROR, sbcs.error);
+ result = set_field(result, DMI_SBCS_SBASIZE, sbcs.asize);
+ result = set_field(result, DMI_SBCS_SBACCESS128, sbcs.access128);
+ result = set_field(result, DMI_SBCS_SBACCESS64, sbcs.access64);
+ result = set_field(result, DMI_SBCS_SBACCESS32, sbcs.access32);
+ result = set_field(result, DMI_SBCS_SBACCESS16, sbcs.access16);
+ result = set_field(result, DMI_SBCS_SBACCESS8, sbcs.access8);
+ break;
+ case DMI_SBADDRESS0:
+ result = sbaddress[0];
+ break;
+ case DMI_SBADDRESS1:
+ result = sbaddress[1];
+ break;
+ case DMI_SBADDRESS2:
+ result = sbaddress[2];
+ break;
+ case DMI_SBADDRESS3:
+ result = sbaddress[3];
+ break;
+ case DMI_SBDATA0:
+ result = sbdata[0];
+ if (sbcs.error == 0) {
+ sb_autoincrement();
+ if (sbcs.readondata) {
+ sb_read();
+ }
+ }
+ break;
+ case DMI_SBDATA1:
+ result = sbdata[1];
+ break;
+ case DMI_SBDATA2:
+ result = sbdata[2];
+ break;
+ case DMI_SBDATA3:
+ result = sbdata[3];
+ break;
default:
result = 0;
D(fprintf(stderr, "Unexpected. Returning Error."));
//NOP
write32(debug_abstract, 0, addi(ZERO, ZERO, 0));
}
-
+
if (get_field(command, AC_ACCESS_REGISTER_POSTEXEC)) {
// Since the next instruction is what we will use, just use nother NOP
// to get there.
}
debug_rom_flags[dmcontrol.hartsel] |= 1 << DEBUG_ROM_FLAG_GO;
-
+
abstractcs.busy = true;
} else {
abstractcs.cmderr = CMDERR_NOTSUP;
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;
- write32(dmdata, address - DMI_DATA0, value);
+ if (!abstractcs.busy)
+ write32(dmdata, address - DMI_DATA0, value);
if (abstractcs.busy && abstractcs.cmderr == CMDERR_NONE) {
abstractcs.cmderr = CMDERR_BUSY;
}
- if ((abstractauto.autoexecdata >> i) & 1) {
+ if (!abstractcs.busy && ((abstractauto.autoexecdata >> i) & 1)) {
perform_abstract_command();
}
return true;
- } else if (address >= DMI_PROGBUF0 && address < DMI_PROGBUF0 + progsize) {
+ } else if (address >= DMI_PROGBUF0 && address < DMI_PROGBUF0 + progbufsize) {
unsigned i = address - DMI_PROGBUF0;
-
- write32(program_buffer, i, value);
- if ((abstractauto.autoexecprogbuf >> i) & 1) {
+ if (!abstractcs.busy)
+ write32(program_buffer, i, value);
+
+ if (!abstractcs.busy && ((abstractauto.autoexecprogbuf >> i) & 1)) {
perform_abstract_command();
}
return true;
-
+
} else {
switch (address) {
case DMI_DMCONTROL:
{
+ if (!dmcontrol.dmactive && get_field(value, DMI_DMCONTROL_DMACTIVE))
+ reset();
dmcontrol.dmactive = get_field(value, DMI_DMCONTROL_DMACTIVE);
if (dmcontrol.dmactive) {
dmcontrol.haltreq = get_field(value, DMI_DMCONTROL_HALTREQ);
dmcontrol.resumereq = get_field(value, DMI_DMCONTROL_RESUMEREQ);
+ dmcontrol.hartreset = get_field(value, DMI_DMCONTROL_HARTRESET);
dmcontrol.ndmreset = get_field(value, DMI_DMCONTROL_NDMRESET);
- dmcontrol.hartsel = get_field(value, DMI_DMCONTROL_HARTSEL);
- } else {
- reset();
+ dmcontrol.hartsel = get_field(value, ((1L<<hartsellen)-1) <<
+ DMI_DMCONTROL_HARTSEL_OFFSET);
}
processor_t *proc = current_proc();
if (proc) {
debug_rom_flags[dmcontrol.hartsel] |= (1 << DEBUG_ROM_FLAG_RESUME);
resumeack[dmcontrol.hartsel] = false;
}
+ if (dmcontrol.hartreset) {
+ proc->reset();
+ }
+ }
+ if (dmcontrol.ndmreset) {
+ for (size_t i = 0; i < sim->nprocs(); i++) {
+ proc = sim->get_core(i);
+ proc->reset();
+ }
}
}
return true;
return true;
case DMI_ABSTRACTAUTO:
- abstractauto.autoexecprogbuf = get_field(value, DMI_ABSTRACTAUTO_AUTOEXECPROGBUF);
- abstractauto.autoexecdata = get_field(value, DMI_ABSTRACTAUTO_AUTOEXECDATA);
- break;
+ abstractauto.autoexecprogbuf = get_field(value,
+ DMI_ABSTRACTAUTO_AUTOEXECPROGBUF);
+ abstractauto.autoexecdata = get_field(value,
+ DMI_ABSTRACTAUTO_AUTOEXECDATA);
+ return true;
+ case DMI_SBCS:
+ sbcs.readonaddr = get_field(value, DMI_SBCS_SBREADONADDR);
+ sbcs.sbaccess = get_field(value, DMI_SBCS_SBACCESS);
+ sbcs.autoincrement = get_field(value, DMI_SBCS_SBAUTOINCREMENT);
+ sbcs.readondata = get_field(value, DMI_SBCS_SBREADONDATA);
+ sbcs.error &= ~get_field(value, DMI_SBCS_SBERROR);
+ return true;
+ case DMI_SBADDRESS0:
+ sbaddress[0] = value;
+ if (sbcs.error == 0 && sbcs.readonaddr) {
+ sb_read();
+ }
+ return true;
+ case DMI_SBADDRESS1:
+ sbaddress[1] = value;
+ return true;
+ case DMI_SBADDRESS2:
+ sbaddress[2] = value;
+ return true;
+ case DMI_SBADDRESS3:
+ sbaddress[3] = value;
+ return true;
+ case DMI_SBDATA0:
+ sbdata[0] = value;
+ if (sbcs.error == 0) {
+ sb_write();
+ if (sbcs.autoincrement && sbcs.error == 0) {
+ sb_autoincrement();
+ }
+ }
+ return true;
+ case DMI_SBDATA1:
+ sbdata[1] = value;
+ return true;
+ case DMI_SBDATA2:
+ sbdata[2] = value;
+ return true;
+ case DMI_SBDATA3:
+ sbdata[3] = value;
+ return true;
}
}
return false;