Fix multicore debug.
[riscv-isa-sim.git] / riscv / debug_module.cc
index e9619c47825b01aa08df81458ce794849ebd6530..d828b72ff1a7da53df762143a53871c7f1d23695 100644 (file)
@@ -6,6 +6,7 @@
 #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_t
+
 debug_module_t::debug_module_t(sim_t *sim) : sim(sim)
 {
   dmcontrol = {0};
-  dmcontrol.version = 1;
 
-  for (unsigned i = 0; i < 1024; i++) {
-    write32(debug_rom_entry, i, jal(0, 0));
-    halted[i] = false;
-  }
+  dmstatus = {0};
+  dmstatus.authenticated = 1;
+  dmstatus.versionlo = 2;
 
-  for (unsigned i = 0; i < datacount; i++) {
-    data[i] = 0;
-  }
+  abstractcs = {0};
+  abstractcs.progsize = progsize;
 
-  for (unsigned i = 0; i < progsize; i++) {
-    ibuf[i] = 0;
-  }
+  abstractauto = {0};
+
+  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_abstract, 0, sizeof(debug_abstract));
 
 }
 
@@ -41,24 +50,55 @@ void debug_module_t::reset()
       proc->halt_request = false;
   }
 
-  dmcontrol.haltreq = 0;
-  dmcontrol.reset = 0;
-  dmcontrol.dmactive = 0;
-  dmcontrol.hartsel = 0;
-  dmcontrol.authenticated = 1;
-  dmcontrol.version = 1;
-  dmcontrol.authbusy = 0;
-  dmcontrol.authtype = dmcontrol.AUTHTYPE_NOAUTH;
-  abstractcs = datacount << DMI_ABSTRACTCS_DATACOUNT_OFFSET;
+  dmcontrol = {0};
+
+  dmstatus = {0};
+  dmstatus.authenticated = 1;
+  dmstatus.versionlo = 2;
+
+  abstractcs = {0};
+  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);
 }
 
 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_CODE) {
-    halted[(addr - DEBUG_ROM_ENTRY) / 4] = true;
-    memcpy(bytes, debug_rom_entry + addr - DEBUG_ROM_ENTRY, len);
+  if (addr >= DEBUG_ROM_ENTRY &&
+      (addr + len) <= (DEBUG_ROM_ENTRY + debug_rom_raw_len)) {
+    memcpy(bytes, debug_rom_raw + addr - DEBUG_ROM_ENTRY, len);
+    return true;
+  }
+
+  if (addr >= DEBUG_ROM_WHERETO && (addr + len) <= (DEBUG_ROM_WHERETO + 4)) {
+    memcpy(bytes, debug_rom_whereto + addr - DEBUG_ROM_WHERETO, len);
+    return true;
+  }
+
+  if (addr >= DEBUG_ROM_FLAGS && ((addr + len) <= DEBUG_ROM_FLAGS + 1024)) {
+    memcpy(bytes, debug_rom_flags + addr - DEBUG_ROM_FLAGS, len);
+    return true;
+  }
+
+  if (addr >= debug_abstract_start && ((addr + len) <= (debug_abstract_start + sizeof(debug_abstract)))) {
+    memcpy(bytes, debug_abstract + addr - debug_abstract_start, len);
+    return true;
+  }
+
+  if (addr >= debug_data_start && (addr + len) <= (debug_data_start + sizeof(dmdata))) {
+    memcpy(bytes, dmdata + addr - debug_data_start, len);
+    return true;
+  }
+
+  if (addr >= debug_progbuf_start && ((addr + len) <= (debug_progbuf_start + sizeof(program_buffer)))) {
+    memcpy(bytes, program_buffer + addr - debug_progbuf_start, len);
     return true;
   }
 
@@ -70,15 +110,60 @@ bool debug_module_t::load(reg_t addr, size_t len, uint8_t* bytes)
 
 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 & (len-1)) {
-    fprintf(stderr, "ERROR: unaligned store to debug module: %zd bytes at 0x%016"
-            PRIx64 "\n", len, addr);
-    return false;
+  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)))) {
+    fprintf(stderr, "Successful write to program buffer %d bytes at %x\n", (int) len, (int) addr);
+    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_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;
   }
 
-  // memcpy(debug_ram + addr - DEBUG_RAM_START, bytes, len);
+  if (addr == DEBUG_ROM_EXCEPTION) {
+    if (abstractcs.cmderr == CMDERR_NONE) {
+      abstractcs.cmderr = CMDERR_EXCEPTION;
+    }
+    return true;
+  }
 
   fprintf(stderr, "ERROR: invalid store to debug module: %zd bytes at 0x%016"
           PRIx64 "\n", len, addr);
@@ -118,44 +203,118 @@ bool debug_module_t::dmi_read(unsigned address, uint32_t *value)
 {
   uint32_t result = 0;
   D(fprintf(stderr, "dmi_read(0x%x) -> ", address));
-  if (address >= DMI_DATA0 && address < DMI_DATA0 + datacount) {
-    result = data[address - DMI_DATA0];
-  } else if (address >= DMI_IBUF0 && address < DMI_IBUF0 + progsize) {
-    result = ibuf[address - DMI_IBUF0];
+  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 (!abstractcs.busy && ((abstractauto.autoexecdata >> i) & 1)) {
+      perform_abstract_command();
+    }
+  } else if (address >= DMI_PROGBUF0 && address < DMI_PROGBUF0 + progsize) {
+    unsigned i = address - DMI_PROGBUF0;
+    result = read32(program_buffer, i);
+    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();
+    }
+
   } else {
     switch (address) {
       case DMI_DMCONTROL:
         {
           processor_t *proc = current_proc();
+          if (proc)
+            dmcontrol.haltreq = proc->halt_request;
+
+          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, DMI_DMCONTROL_HARTRESET, dmcontrol.hartreset);
+         result = set_field(result, DMI_DMCONTROL_NDMRESET, dmcontrol.ndmreset);
+          result = set_field(result, DMI_DMCONTROL_DMACTIVE, dmcontrol.dmactive);
+        }
+        break;
+      case DMI_DMSTATUS:
+        {
+          processor_t *proc = current_proc();
+
+         dmstatus.allnonexistant = false;
+         dmstatus.allunavail = false;
+         dmstatus.allrunning = false;
+         dmstatus.allhalted = false;
+          dmstatus.allresumeack = false;
           if (proc) {
             if (halted[dmcontrol.hartsel]) {
-              dmcontrol.hartstatus = dmcontrol.HARTSTATUS_HALTED;
+              dmstatus.allhalted = true;
             } else {
-              dmcontrol.hartstatus = dmcontrol.HARTSTATUS_RUNNING;
+              dmstatus.allrunning = true;
             }
-            dmcontrol.haltreq = proc->halt_request;
           } else {
-            dmcontrol.hartstatus = dmcontrol.HARTSTATUS_NOTEXIST;
+           dmstatus.allnonexistant = true;
           }
-          result = set_field(result, DMI_DMCONTROL_HALTREQ, dmcontrol.haltreq);
-          result = set_field(result, DMI_DMCONTROL_RESET, dmcontrol.reset);
-          result = set_field(result, DMI_DMCONTROL_DMACTIVE, dmcontrol.dmactive);
-          result = set_field(result, DMI_DMCONTROL_HARTSTATUS, dmcontrol.hartstatus);
-          result = set_field(result, DMI_DMCONTROL_HARTSEL, dmcontrol.hartsel);
-          result = set_field(result, DMI_DMCONTROL_AUTHENTICATED, dmcontrol.authenticated);
-          result = set_field(result, DMI_DMCONTROL_AUTHBUSY, dmcontrol.authbusy);
-          result = set_field(result, DMI_DMCONTROL_AUTHTYPE, dmcontrol.authtype);
-          result = set_field(result, DMI_DMCONTROL_VERSION, dmcontrol.version);
+         dmstatus.anynonexistant = dmstatus.allnonexistant;
+         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);
+          result = set_field(result, DMI_DMSTATUS_VERSIONLO, dmstatus.versionlo);
         }
-        break;
+       break;
       case DMI_ABSTRACTCS:
-        result = 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);
+        break;
+      case DMI_ABSTRACTAUTO:
+        result = set_field(result, DMI_ABSTRACTAUTO_AUTOEXECPROGBUF, abstractauto.autoexecprogbuf);
+        result = set_field(result, DMI_ABSTRACTAUTO_AUTOEXECDATA, abstractauto.autoexecdata);
         break;
-      case DMI_ACCESSCS:
-        result = progsize << DMI_ACCESSCS_PROGSIZE_OFFSET;
+      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;
     }
   }
@@ -164,20 +323,110 @@ bool debug_module_t::dmi_read(unsigned address, uint32_t *value)
   return true;
 }
 
-bool debug_module_t::perform_abstract_command(uint32_t command)
+bool debug_module_t::perform_abstract_command()
 {
-  return false;
+  if (abstractcs.cmderr != CMDERR_NONE)
+    return true;
+  if (abstractcs.busy) {
+    abstractcs.cmderr = CMDERR_BUSY;
+    return true;
+  }
+
+  if ((command >> 24) == 0) {
+    // register access
+    unsigned size = get_field(command, AC_ACCESS_REGISTER_SIZE);
+    bool write = get_field(command, AC_ACCESS_REGISTER_WRITE);
+    unsigned regno = get_field(command, AC_ACCESS_REGISTER_REGNO);
+
+    if (!halted[dmcontrol.hartsel]) {
+      abstractcs.cmderr = CMDERR_HALTRESUME;
+      return true;
+    }
+
+    if (get_field(command, AC_ACCESS_REGISTER_TRANSFER)) {
+
+      if (regno < 0x1000 || regno >= 0x1020) {
+        abstractcs.cmderr = CMDERR_NOTSUP;
+        return true;
+      }
+
+      unsigned regnum = regno - 0x1000;
+
+      switch (size) {
+      case 2:
+        if (write)
+          write32(debug_abstract, 0, lw(regnum, ZERO, debug_data_start));
+        else
+          write32(debug_abstract, 0, sw(regnum, ZERO, debug_data_start));
+        break;
+      case 3:
+        if (write)
+          write32(debug_abstract, 0, ld(regnum, ZERO, debug_data_start));
+        else
+          write32(debug_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 = CMDERR_NOTSUP;
+        return true;
+      }
+    } else {
+      //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.
+      write32(debug_abstract, 1, addi(ZERO, ZERO, 0));
+    } else {
+      write32(debug_abstract, 1, ebreak());
+    }
+
+    debug_rom_flags[dmcontrol.hartsel] |= 1 << DEBUG_ROM_FLAG_GO;
+
+    abstractcs.busy = true;
+  } else {
+    abstractcs.cmderr = CMDERR_NOTSUP;
+  }
+  return true;
 }
 
 bool debug_module_t::dmi_write(unsigned address, uint32_t value)
 {
   D(fprintf(stderr, "dmi_write(0x%x, 0x%x)\n", address, value));
-  if (address >= DMI_DATA0 && address < DMI_DATA0 + datacount) {
-    data[address - DMI_DATA0] = value;
+  if (address >= DMI_DATA0 && address < DMI_DATA0 + abstractcs.datacount) {
+    unsigned i = address - DMI_DATA0;
+    if (!abstractcs.busy)
+      write32(dmdata, address - DMI_DATA0, value);
+
+    if (abstractcs.busy && abstractcs.cmderr == CMDERR_NONE) {
+      abstractcs.cmderr = CMDERR_BUSY;
+    }
+
+    if (!abstractcs.busy && ((abstractauto.autoexecdata >> i) & 1)) {
+      perform_abstract_command();
+    }
     return true;
-  } else if (address >= DMI_IBUF0 && address < DMI_IBUF0 + progsize) {
-    ibuf[address - DMI_IBUF0] = value;
+
+  } else if (address >= DMI_PROGBUF0 && address < DMI_PROGBUF0 + progsize) {
+    unsigned i = address - DMI_PROGBUF0;
+
+    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:
@@ -185,7 +434,8 @@ bool debug_module_t::dmi_write(unsigned address, uint32_t value)
           dmcontrol.dmactive = get_field(value, DMI_DMCONTROL_DMACTIVE);
           if (dmcontrol.dmactive) {
             dmcontrol.haltreq = get_field(value, DMI_DMCONTROL_HALTREQ);
-            dmcontrol.reset = get_field(value, DMI_DMCONTROL_RESET);
+            dmcontrol.resumereq = get_field(value, DMI_DMCONTROL_RESUMEREQ);
+            dmcontrol.ndmreset = get_field(value, DMI_DMCONTROL_NDMRESET);
             dmcontrol.hartsel = get_field(value, DMI_DMCONTROL_HARTSEL);
           } else {
             reset();
@@ -193,12 +443,31 @@ bool debug_module_t::dmi_write(unsigned address, uint32_t value)
           processor_t *proc = current_proc();
           if (proc) {
             proc->halt_request = dmcontrol.haltreq;
+            if (dmcontrol.resumereq) {
+              debug_rom_flags[dmcontrol.hartsel] |= (1 << DEBUG_ROM_FLAG_RESUME);
+              resumeack[dmcontrol.hartsel] = false;
+            }
+           if (dmcontrol.ndmreset) {
+             proc->reset();
+           }
           }
         }
         return true;
 
+      case DMI_COMMAND:
+        command = value;
+        return perform_abstract_command();
+
       case DMI_ABSTRACTCS:
-        return perform_abstract_command(value);
+        abstractcs.cmderr = (cmderr_t) (((uint32_t) (abstractcs.cmderr)) & (~(uint32_t)(get_field(value, DMI_ABSTRACTCS_CMDERR))));
+        return true;
+
+      case DMI_ABSTRACTAUTO:
+        abstractauto.autoexecprogbuf = get_field(value,
+            DMI_ABSTRACTAUTO_AUTOEXECPROGBUF);
+        abstractauto.autoexecdata = get_field(value,
+            DMI_ABSTRACTAUTO_AUTOEXECDATA);
+        return true;
     }
   }
   return false;