Fix multicore debug.
[riscv-isa-sim.git] / riscv / debug_module.cc
index 0f51f7b2f11797c8a700f8ef51f9d1f1195c398d..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
@@ -15,9 +16,7 @@
 
 ///////////////////////// 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};
 
@@ -30,12 +29,17 @@ debug_module_t::debug_module_t(sim_t *sim) : sim(sim),
 
   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_abstract, 0, sizeof(debug_abstract));
+
 }
 
 void debug_module_t::reset()
@@ -68,58 +72,33 @@ 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 + len) <= (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 + len) <= (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;
-      resumeack[dmcontrol.hartsel] = true;
-    }
-
-    fprintf(stderr, "returning the  debug rom code.\n");
-    memcpy(bytes, debug_rom_code + addr - DEBUG_ROM_CODE, len);
+  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_DATA_START && addr < DEBUG_DATA_END) {
-    memcpy(bytes, dmdata + addr - DEBUG_DATA_START, len);
+  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_PROGBUF_START && addr < DEBUG_PROGBUF_END) {
-    memcpy(bytes, program_buffer + addr - DEBUG_PROGBUF_START, len);
+
+  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_ROM_EXCEPTION &&
-      addr < DEBUG_ROM_EXCEPTION + DEBUG_ROM_EXCEPTION_SIZE) {
-    memcpy(bytes, debug_rom_exception + addr - DEBUG_ROM_EXCEPTION, len);
-    if (abstractcs.cmderr == CMDERR_NONE) {
-      abstractcs.cmderr = CMDERR_EXCEPTION;
-    }
+  if (addr >= debug_progbuf_start && ((addr + len) <= (debug_progbuf_start + sizeof(program_buffer)))) {
+    memcpy(bytes, program_buffer + addr - debug_progbuf_start, len);
     return true;
   }
 
@@ -132,15 +111,57 @@ 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 >= DEBUG_DATA_START && addr < DEBUG_DATA_END) {
-    memcpy(dmdata + addr - DEBUG_DATA_START, bytes, len);
+
+  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 < DEBUG_PROGBUF_END) {
-    memcpy(program_buffer + addr - DEBUG_PROGBUF_START, bytes, len);
+
+  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;
+  }
+
+  if (addr == DEBUG_ROM_EXCEPTION) {
+    if (abstractcs.cmderr == CMDERR_NONE) {
+      abstractcs.cmderr = CMDERR_EXCEPTION;
+    }
     return true;
   }
 
@@ -184,17 +205,30 @@ bool debug_module_t::dmi_read(unsigned address, uint32_t *value)
   D(fprintf(stderr, "dmi_read(0x%x) -> ", address));
   if (address >= DMI_DATA0 && address < DMI_DATA0 + abstractcs.datacount) {
     unsigned i = address - DMI_DATA0;
-    result = read32(dmdata, 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) {
-    // TODO : Autoexec progbuf.
-    result = read32(program_buffer, address - DMI_PROGBUF0);
+    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:
@@ -242,7 +276,7 @@ bool debug_module_t::dmi_read(unsigned address, uint32_t *value)
           } 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);
@@ -276,7 +310,7 @@ bool debug_module_t::dmi_read(unsigned address, uint32_t *value)
         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);
+        result = set_field(result, DMI_HARTINFO_DATAADDR, debug_data_start);
         break;
       default:
         result = 0;
@@ -304,38 +338,39 @@ bool debug_module_t::perform_abstract_command()
     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 = CMDERR_NOTSUP;
-      return true;
-    }
-
-    unsigned regnum = regno - 0x1000;
-
     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_rom_code, 0, lw(regnum, ZERO, DEBUG_DATA_START));
+          write32(debug_abstract, 0, lw(regnum, ZERO, debug_data_start));
         else
-          write32(debug_rom_code, 0, sw(regnum, ZERO, DEBUG_DATA_START));
+          write32(debug_abstract, 0, sw(regnum, ZERO, debug_data_start));
         break;
       case 3:
         if (write)
-          write32(debug_rom_code, 0, ld(regnum, ZERO, DEBUG_DATA_START));
+          write32(debug_abstract, 0, ld(regnum, ZERO, debug_data_start));
         else
-          write32(debug_rom_code, 0, sd(regnum, ZERO, DEBUG_DATA_START));
+          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));
+          write32(debug_rom_code, 0, lq(regnum, ZERO, debug_data_start));
           else
-          write32(debug_rom_code, 0, sq(regnum, ZERO, DEBUG_DATA_START));
+          write32(debug_rom_code, 0, sq(regnum, ZERO, debug_data_start));
           break;
         */
       default:
@@ -343,22 +378,20 @@ bool debug_module_t::perform_abstract_command()
         return true;
       }
     } else {
-      // Should be a NOP. Store DEBUG_DATA to x0.
-      write32(debug_rom_code, 0, sw(ZERO, ZERO, DEBUG_DATA_START));
+      //NOP
+      write32(debug_abstract, 0, addi(ZERO, ZERO, 0));
     }
-    
+
     if (get_field(command, AC_ACCESS_REGISTER_POSTEXEC)) {
-      write32(debug_rom_code, 1, jal(ZERO, DEBUG_PROGBUF_START - DEBUG_ROM_CODE - 4));
+      // 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_rom_code, 1, ebreak());
+      write32(debug_abstract, 1, ebreak());
     }
 
-    
-    write32(debug_rom_entry, dmcontrol.hartsel,
-            jal(ZERO, DEBUG_ROM_CODE - (DEBUG_ROM_ENTRY + 4 * dmcontrol.hartsel)));
-    
-    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 = CMDERR_NOTSUP;
@@ -371,19 +404,29 @@ 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 + 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) {
-    write32(program_buffer, address - DMI_PROGBUF0, value);
+    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:
@@ -401,12 +444,12 @@ bool debug_module_t::dmi_write(unsigned address, uint32_t value)
           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;
             }
+           if (dmcontrol.ndmreset) {
+             proc->reset();
+           }
           }
         }
         return true;
@@ -420,9 +463,11 @@ bool debug_module_t::dmi_write(unsigned address, uint32_t value)
         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;
     }
   }
   return false;