From 294a0572c47128564078ab78eb84bce5bdb80a79 Mon Sep 17 00:00:00 2001 From: Tim Newsome Date: Mon, 13 Feb 2017 21:29:26 -0800 Subject: [PATCH] Implement program buffer preexec/postexec. I only tested preexec. --- riscv/debug_defines.h | 779 ++++++++++++++++++++++++++++++++++++++++-- riscv/debug_module.cc | 56 ++- riscv/debug_module.h | 11 +- 3 files changed, 802 insertions(+), 44 deletions(-) diff --git a/riscv/debug_defines.h b/riscv/debug_defines.h index 5666f46..59fa344 100644 --- a/riscv/debug_defines.h +++ b/riscv/debug_defines.h @@ -1,16 +1,45 @@ #define AC_ACCESS_REGISTER None +/* +* 2: Access the lowest 32 bits of the register. +* +* 3: Access the lowest 64 bits of the register. +* +* 4: Access the lowest 128 bits of the register. +* +* If \Fsize specifies a size larger than the register is, then the +* access must fail. If a register is accessible, then \Fsize matching +* the register's actual size must be supported. + */ #define AC_ACCESS_REGISTER_SIZE_OFFSET 19 #define AC_ACCESS_REGISTER_SIZE_LENGTH 3 #define AC_ACCESS_REGISTER_SIZE (0x7 << AC_ACCESS_REGISTER_SIZE_OFFSET) +/* +* When 1, execute the program in the Program Buffer exactly once +* before performing the read/write. + */ #define AC_ACCESS_REGISTER_PREEXEC_OFFSET 18 #define AC_ACCESS_REGISTER_PREEXEC_LENGTH 1 #define AC_ACCESS_REGISTER_PREEXEC (0x1 << AC_ACCESS_REGISTER_PREEXEC_OFFSET) +/* +* When 1, execute the program in the Program Buffer exactly once +* after performing the read/write. + */ #define AC_ACCESS_REGISTER_POSTEXEC_OFFSET 17 #define AC_ACCESS_REGISTER_POSTEXEC_LENGTH 1 #define AC_ACCESS_REGISTER_POSTEXEC (0x1 << AC_ACCESS_REGISTER_POSTEXEC_OFFSET) +/* +* 0: Copy data from \Rdatazero into the specified register. +* +* 1: Copy data from the specified register into \Rdatazero. +* +* (If XLEN is greater than 32, more {\tt data} registers are involved.) + */ #define AC_ACCESS_REGISTER_WRITE_OFFSET 16 #define AC_ACCESS_REGISTER_WRITE_LENGTH 1 #define AC_ACCESS_REGISTER_WRITE (0x1 << AC_ACCESS_REGISTER_WRITE_OFFSET) +/* +* Number of the register to access, as described in Table~\ref{tab:regno}. + */ #define AC_ACCESS_REGISTER_REGNO_OFFSET 0 #define AC_ACCESS_REGISTER_REGNO_LENGTH 16 #define AC_ACCESS_REGISTER_REGNO (0xffff << AC_ACCESS_REGISTER_REGNO_OFFSET) @@ -19,33 +48,106 @@ #define AC_QUICK_ACCESS_1_LENGTH 8 #define AC_QUICK_ACCESS_1 (0xff << AC_QUICK_ACCESS_1_OFFSET) #define CSR_DCSR 0x7b0 +/* +* 0: There is no external debug support. +* +* 1: External debug support exists as it is described in this document. +* +* Other values are reserved for future standards. + */ #define CSR_DCSR_XDEBUGVER_OFFSET 30 #define CSR_DCSR_XDEBUGVER_LENGTH 2 #define CSR_DCSR_XDEBUGVER (0x3 << CSR_DCSR_XDEBUGVER_OFFSET) +/* +* When 1, {\tt ebreak} instructions in Machine Mode enter Halt Mode. + */ #define CSR_DCSR_EBREAKM_OFFSET 15 #define CSR_DCSR_EBREAKM_LENGTH 1 #define CSR_DCSR_EBREAKM (0x1 << CSR_DCSR_EBREAKM_OFFSET) +/* +* When 1, {\tt ebreak} instructions in Hypervisor Mode enter Halt Mode. + */ #define CSR_DCSR_EBREAKH_OFFSET 14 #define CSR_DCSR_EBREAKH_LENGTH 1 #define CSR_DCSR_EBREAKH (0x1 << CSR_DCSR_EBREAKH_OFFSET) +/* +* When 1, {\tt ebreak} instructions in Supervisor Mode enter Halt Mode. + */ #define CSR_DCSR_EBREAKS_OFFSET 13 #define CSR_DCSR_EBREAKS_LENGTH 1 #define CSR_DCSR_EBREAKS (0x1 << CSR_DCSR_EBREAKS_OFFSET) +/* +* When 1, {\tt ebreak} instructions in User/Application Mode enter +* Halt Mode. + */ #define CSR_DCSR_EBREAKU_OFFSET 12 #define CSR_DCSR_EBREAKU_LENGTH 1 #define CSR_DCSR_EBREAKU (0x1 << CSR_DCSR_EBREAKU_OFFSET) +/* +* Controls the behavior of any counters while the component is in +* Halt Mode. This includes the {\tt cycle} and {\tt instret} CSRs. +* When 1, counters are stopped when the component is in Halt Mode. +* Otherwise, the counters continue to run. +* +* An implementation may choose not to support writing to this bit. +* The debugger must read back the value it writes to check whether +* the feature is supported. + */ #define CSR_DCSR_STOPCYCLE_OFFSET 10 #define CSR_DCSR_STOPCYCLE_LENGTH 1 #define CSR_DCSR_STOPCYCLE (0x1 << CSR_DCSR_STOPCYCLE_OFFSET) +/* +* Controls the behavior of any timers while the component is in Debug +* Mode. This includes the {\tt time} and {tt timeh} CSRs. When 1, +* timers are stopped when the component is in Halt Mode. Otherwise, +* the timers continue to run. +* +* An implementation may choose not to support writing to this bit. +* The debugger must read back the value it writes to check whether +* the feature is supported. + */ #define CSR_DCSR_STOPTIME_OFFSET 9 #define CSR_DCSR_STOPTIME_LENGTH 1 #define CSR_DCSR_STOPTIME (0x1 << CSR_DCSR_STOPTIME_OFFSET) +/* +* Explains why Halt Mode was entered. +* +* When there are multiple reasons to enter Halt Mode in a single +* cycle, the cause with the highest priority is the one written. +* +* 1: A software breakpoint was hit. (priority 3) +* +* 2: The Trigger Module caused a halt. (priority 4) +* +* 3: The debug interrupt was asserted by the Debug Module. (priority 2) +* +* 4: The hart single stepped because \Fstep was set. (priority 1) +* +* 5: \Fhaltreq was set. (priority 0) +* +* Other values are reserved for future use. + */ #define CSR_DCSR_CAUSE_OFFSET 6 #define CSR_DCSR_CAUSE_LENGTH 3 #define CSR_DCSR_CAUSE (0x7 << CSR_DCSR_CAUSE_OFFSET) +/* +* When set and not in Halt Mode, the hart will only execute a single +* instruction, and then enter Halt Mode. Interrupts are disabled +* when this bit is set. + */ #define CSR_DCSR_STEP_OFFSET 2 #define CSR_DCSR_STEP_LENGTH 1 #define CSR_DCSR_STEP (0x1 << CSR_DCSR_STEP_OFFSET) +/* +* Contains the privilege level the hart was operating in when Debug +* Mode was entered. The encoding is describe in Table +* \ref{tab:privlevel}. A debugger can change this value to change +* the hart's privilege level when exiting Halt Mode. +* +* Not all privilege levels are supported on all harts. If the +* encoding written is not supported or the debugger is not allowed to +* change to it, the hart may change to any supported privilege level. + */ #define CSR_DCSR_PRV_OFFSET 0 #define CSR_DCSR_PRV_LENGTH 2 #define CSR_DCSR_PRV (0x3 << CSR_DCSR_PRV_OFFSET) @@ -56,44 +158,139 @@ #define CSR_DSCRATCH0 0x7b2 #define CSR_DSCRATCH1 0x7b3 #define CSR_PRIV virtual +/* +* Contains the privilege level the hart was operating in when Debug +* Mode was entered. The encoding is describe in Table +* \ref{tab:privlevel}. A user can write this value to change the +* hart's privilege level when exiting Halt Mode. + */ #define CSR_PRIV_PRV_OFFSET 0 #define CSR_PRIV_PRV_LENGTH 2 #define CSR_PRIV_PRV (0x3 << CSR_PRIV_PRV_OFFSET) #define DMI_DMCONTROL 0x00 +/* +* Halt request signal for the hart selected by \Fhartsel. Writes +* apply to the new value of \Fhartsel. + */ #define DMI_DMCONTROL_HALTREQ_OFFSET 31 #define DMI_DMCONTROL_HALTREQ_LENGTH 1 #define DMI_DMCONTROL_HALTREQ (0x1 << DMI_DMCONTROL_HALTREQ_OFFSET) +/* +* This bit controls the reset signal from the DM to the rest of the +* system. To perform a reset the debugger writes 1, and then writes 0 +* to deassert the reset. + */ #define DMI_DMCONTROL_RESET_OFFSET 30 #define DMI_DMCONTROL_RESET_LENGTH 1 #define DMI_DMCONTROL_RESET (0x1 << DMI_DMCONTROL_RESET_OFFSET) +/* +* This bit serves as a reset signal for the Debug Module itself. +* When 0, the module is held in reset. When 1, it functions normally. +* No other mechanism should exist that may result in resetting the +* Debug Module after power up, including the platform's system reset +* or Debug Transport reset signals. +* +* A debugger should pulse this bit low to ensure that the Debug +* Module is fully reset and ready to use. +* +* Implementations may use this bit to aid debugging, for example by +* preventing the Debug Module from being power gated while debugging +* is active. + */ #define DMI_DMCONTROL_DMACTIVE_OFFSET 29 #define DMI_DMCONTROL_DMACTIVE_LENGTH 1 #define DMI_DMCONTROL_DMACTIVE (0x1 << DMI_DMCONTROL_DMACTIVE_OFFSET) +/* +* The status of the currently selected hart. +* +* 0: Halted. +* +* 1: Running. +* +* 2: Unavailable (eg. powered down, held in reset). +* +* 3: \Fhartsel specifies a hart that does not exist in this system. + */ #define DMI_DMCONTROL_HARTSTATUS_OFFSET 26 #define DMI_DMCONTROL_HARTSTATUS_LENGTH 2 #define DMI_DMCONTROL_HARTSTATUS (0x3 << DMI_DMCONTROL_HARTSTATUS_OFFSET) +/* +* The DM-specific index of the hart to select. + */ #define DMI_DMCONTROL_HARTSEL_OFFSET 16 #define DMI_DMCONTROL_HARTSEL_LENGTH 10 #define DMI_DMCONTROL_HARTSEL (0x3ff << DMI_DMCONTROL_HARTSEL_OFFSET) +/* +* 0 when authentication is required before using the DM. 1 when the +* authentication check has passed. On components that don't implement +* authentication, this bit must be preset as 1. + */ #define DMI_DMCONTROL_AUTHENTICATED_OFFSET 7 #define DMI_DMCONTROL_AUTHENTICATED_LENGTH 1 #define DMI_DMCONTROL_AUTHENTICATED (0x1 << DMI_DMCONTROL_AUTHENTICATED_OFFSET) +/* +* While 1, writes to \Rauthdatazero and \Rauthdataone may be ignored +* or may result in authentication failing. Authentication mechanisms +* that are slow (or intentionally delayed) must set this bit when +* they're not ready to process another write. + */ #define DMI_DMCONTROL_AUTHBUSY_OFFSET 6 #define DMI_DMCONTROL_AUTHBUSY_LENGTH 1 #define DMI_DMCONTROL_AUTHBUSY (0x1 << DMI_DMCONTROL_AUTHBUSY_OFFSET) +/* +* Defines the kind of authentication required to use this DM. +* +* 0: No authentication is required. +* +* 1: A password is required. +* +* 2: A challenge-response mechanism is in place. +* +* 3: Reserved for future use. + */ #define DMI_DMCONTROL_AUTHTYPE_OFFSET 4 #define DMI_DMCONTROL_AUTHTYPE_LENGTH 2 #define DMI_DMCONTROL_AUTHTYPE (0x3 << DMI_DMCONTROL_AUTHTYPE_OFFSET) +/* +* 0: There is no Debug Module present. +* +* 1: There is a Debug Module and it conforms to version 0.12 of this +* specification. +* +* Other values are reserved for future use. + */ #define DMI_DMCONTROL_VERSION_OFFSET 0 #define DMI_DMCONTROL_VERSION_LENGTH 4 #define DMI_DMCONTROL_VERSION (0xf << DMI_DMCONTROL_VERSION_OFFSET) #define DMI_HARTINFO 0x01 +/* +* 0: The {\tt data} registers are shadowed in the hart by CSR +* registers. Each CSR register is XLEN bits in size, and corresponds +* to a single argument, per Table~\ref{tab:datareg}. +* +* 1: The {\tt data} registers are shadowed in the hart's memory map. +* Each register takes up 4 bytes in the memory map. + */ #define DMI_HARTINFO_DATAACCESS_OFFSET 16 #define DMI_HARTINFO_DATAACCESS_LENGTH 1 #define DMI_HARTINFO_DATAACCESS (0x1 << DMI_HARTINFO_DATAACCESS_OFFSET) +/* +* If \Fdataaccess is 0: Number of CSR registers dedicated to +* shadowing the {\tt data} registers. +* +* If \Fdataaccess is 1: Number of 32-bit words in the memory map +* dedicated to shadowing the {\tt data} registers. + */ #define DMI_HARTINFO_DATASIZE_OFFSET 12 #define DMI_HARTINFO_DATASIZE_LENGTH 4 #define DMI_HARTINFO_DATASIZE (0xf << DMI_HARTINFO_DATASIZE_OFFSET) +/* +* If \Fdataaccess is 0: The number of the first CSR dedicated to +* shadowing the {\tt data} registers. +* +* If \Fdataaccess is 1: Signed address of RAM where the {\tt data} +* registers are shadowed. + */ #define DMI_HARTINFO_DATAADDR_OFFSET 0 #define DMI_HARTINFO_DATAADDR_LENGTH 12 #define DMI_HARTINFO_DATAADDR (0xfff << DMI_HARTINFO_DATAADDR_OFFSET) @@ -195,67 +392,164 @@ #define DMI_HALTSUM_HALT31_0_LENGTH 1 #define DMI_HALTSUM_HALT31_0 (0x1 << DMI_HALTSUM_HALT31_0_OFFSET) #define DMI_SBCS 0x03 +/* +* When a 1 is written here, triggers a read at the address in {\tt +* sbaddress} using the access size set by \Fsbaccess. + */ #define DMI_SBCS_SBSINGLEREAD_OFFSET 20 #define DMI_SBCS_SBSINGLEREAD_LENGTH 1 #define DMI_SBCS_SBSINGLEREAD (0x1 << DMI_SBCS_SBSINGLEREAD_OFFSET) +/* +* Select the access size to use for system bus accesses triggered by +* writes to the {\tt sbaddress} registers or \Rsbdatazero. +* +* 0: 8-bit +* +* 1: 16-bit +* +* 2: 32-bit +* +* 3: 64-bit +* +* 4: 128-bit +* +* If an unsupported system bus access size is written here, +* the DM may not +* perform the access, or may perform the access with any access size + */ #define DMI_SBCS_SBACCESS_OFFSET 17 #define DMI_SBCS_SBACCESS_LENGTH 3 #define DMI_SBCS_SBACCESS (0x7 << DMI_SBCS_SBACCESS_OFFSET) +/* +* When 1, the internal address value (used by the system bus master) +* is incremented by the access size (in bytes) selected in \Fsbaccess +* after every system bus access. + */ #define DMI_SBCS_SBAUTOINCREMENT_OFFSET 16 #define DMI_SBCS_SBAUTOINCREMENT_LENGTH 1 #define DMI_SBCS_SBAUTOINCREMENT (0x1 << DMI_SBCS_SBAUTOINCREMENT_OFFSET) +/* +* When 1, every read from \Rsbdatazero automatically triggers a system +* bus read at the new address. + */ #define DMI_SBCS_SBAUTOREAD_OFFSET 15 #define DMI_SBCS_SBAUTOREAD_LENGTH 1 #define DMI_SBCS_SBAUTOREAD (0x1 << DMI_SBCS_SBAUTOREAD_OFFSET) +/* +* When the debug module's system bus +* master causes a bus error, this field gets set. +* It remains set until 0 is written to any bit in this field. Until +* that happens, the system bus master is busy and no more accesses can be +* initiated by the debug module. +* +* 0: There was no bus error. +* +* 1: There was a timeout. +* +* 2: A bad address was accessed. +* +* 3: There was some other error (eg. alignment). +* +* 4: The system bus master was busy when a one of the {\tt sbaddress} or +* {\tt sbdata} registers was written. + */ #define DMI_SBCS_SBERROR_OFFSET 12 #define DMI_SBCS_SBERROR_LENGTH 3 #define DMI_SBCS_SBERROR (0x7 << DMI_SBCS_SBERROR_OFFSET) +/* +* Width of system bus addresses in bits. (0 indicates there is no bus +* access support.) + */ #define DMI_SBCS_SBASIZE_OFFSET 5 #define DMI_SBCS_SBASIZE_LENGTH 7 #define DMI_SBCS_SBASIZE (0x7f << DMI_SBCS_SBASIZE_OFFSET) +/* +* 1 when 128-bit system bus accesses are supported. + */ #define DMI_SBCS_SBACCESS128_OFFSET 4 #define DMI_SBCS_SBACCESS128_LENGTH 1 #define DMI_SBCS_SBACCESS128 (0x1 << DMI_SBCS_SBACCESS128_OFFSET) +/* +* 1 when 64-bit system bus accesses are supported. + */ #define DMI_SBCS_SBACCESS64_OFFSET 3 #define DMI_SBCS_SBACCESS64_LENGTH 1 #define DMI_SBCS_SBACCESS64 (0x1 << DMI_SBCS_SBACCESS64_OFFSET) +/* +* 1 when 32-bit system bus accesses are supported. + */ #define DMI_SBCS_SBACCESS32_OFFSET 2 #define DMI_SBCS_SBACCESS32_LENGTH 1 #define DMI_SBCS_SBACCESS32 (0x1 << DMI_SBCS_SBACCESS32_OFFSET) +/* +* 1 when 16-bit system bus accesses are supported. + */ #define DMI_SBCS_SBACCESS16_OFFSET 1 #define DMI_SBCS_SBACCESS16_LENGTH 1 #define DMI_SBCS_SBACCESS16 (0x1 << DMI_SBCS_SBACCESS16_OFFSET) +/* +* 1 when 8-bit system bus accesses are supported. + */ #define DMI_SBCS_SBACCESS8_OFFSET 0 #define DMI_SBCS_SBACCESS8_LENGTH 1 #define DMI_SBCS_SBACCESS8 (0x1 << DMI_SBCS_SBACCESS8_OFFSET) #define DMI_SBADDRESS0 0x04 +/* +* Accesses bits 31:0 of the internal address. + */ #define DMI_SBADDRESS0_ADDRESS_OFFSET 0 #define DMI_SBADDRESS0_ADDRESS_LENGTH 32 #define DMI_SBADDRESS0_ADDRESS (0xffffffff << DMI_SBADDRESS0_ADDRESS_OFFSET) #define DMI_SBADDRESS1 0x05 +/* +* Accesses bits 63:32 of the internal address (if the system address +* bus is that wide). + */ #define DMI_SBADDRESS1_ADDRESS_OFFSET 0 #define DMI_SBADDRESS1_ADDRESS_LENGTH 32 #define DMI_SBADDRESS1_ADDRESS (0xffffffff << DMI_SBADDRESS1_ADDRESS_OFFSET) #define DMI_SBADDRESS2 0x06 +/* +* The same as \Fbusy in \Rsbaddresszero. + */ #define DMI_SBADDRESS2_BUSY_OFFSET 31 #define DMI_SBADDRESS2_BUSY_LENGTH 1 #define DMI_SBADDRESS2_BUSY (0x1 << DMI_SBADDRESS2_BUSY_OFFSET) +/* +* Accesses bits 91:61 of the internal address (if the system address +* bus is that wide). + */ #define DMI_SBADDRESS2_ADDRESS_OFFSET 0 #define DMI_SBADDRESS2_ADDRESS_LENGTH 31 #define DMI_SBADDRESS2_ADDRESS (0x7fffffff << DMI_SBADDRESS2_ADDRESS_OFFSET) #define DMI_SBDATA0 0x07 +/* +* Accesses bits 31:0 of the internal data. + */ #define DMI_SBDATA0_DATA_OFFSET 0 #define DMI_SBDATA0_DATA_LENGTH 32 #define DMI_SBDATA0_DATA (0xffffffff << DMI_SBDATA0_DATA_OFFSET) #define DMI_SBDATA1 0x08 +/* +* Accesses bits 63:32 of the internal data (if the system bus is +* that wide). + */ #define DMI_SBDATA1_DATA_OFFSET 0 #define DMI_SBDATA1_DATA_LENGTH 32 #define DMI_SBDATA1_DATA (0xffffffff << DMI_SBDATA1_DATA_OFFSET) #define DMI_SBDATA2 0x09 +/* +* Accesses bits 95:64 of the internal data (if the system bus is +* that wide). + */ #define DMI_SBDATA2_DATA_OFFSET 0 #define DMI_SBDATA2_DATA_LENGTH 32 #define DMI_SBDATA2_DATA (0xffffffff << DMI_SBDATA2_DATA_OFFSET) #define DMI_SBDATA3 0x0a +/* +* Accesses bits 127:96 of the internal data (if the system bus is +* that wide). + */ #define DMI_SBDATA3_DATA_OFFSET 0 #define DMI_SBDATA3_DATA_LENGTH 32 #define DMI_SBDATA3_DATA (0xffffffff << DMI_SBDATA3_DATA_OFFSET) @@ -289,15 +583,54 @@ #define DMI_ABSTRACTCS_AUTOEXEC1_OFFSET 9 #define DMI_ABSTRACTCS_AUTOEXEC1_LENGTH 1 #define DMI_ABSTRACTCS_AUTOEXEC1 (0x1 << DMI_ABSTRACTCS_AUTOEXEC1_OFFSET) +/* +* When 1, accesses to \Rdatazero cause the command in \Rcommand to be +* executed again. +* +* The same is true for other other autoexec bits: When 1, accesses to +* {\tt data}N cause the command in \Rcommand to be executed again. + */ #define DMI_ABSTRACTCS_AUTOEXEC0_OFFSET 8 #define DMI_ABSTRACTCS_AUTOEXEC0_LENGTH 1 #define DMI_ABSTRACTCS_AUTOEXEC0 (0x1 << DMI_ABSTRACTCS_AUTOEXEC0_OFFSET) +/* +* Gets set if an abstract command fails. No abstract command is +* started until the value is reset to 0. +* +* 0 (none): No error. +* +* 1 (busy): An abstract command was executing while \Rcommand or one +* of the {\tt data} registers was accessed. +* +* 2 (not supported): The requested command is not supported. A +* command that is not supported while the hart is running may be +* supported when it is halted. +* +* 3 (exception): An exception occurred while executing the command +* (eg. while executing the Program Buffer). +* +* 4 (halt/resume): An abstract command couldn't execute because the +* hart wasn't in the expected state (running/halted). +* +* 7 (other): The command failed for another reason. + */ #define DMI_ABSTRACTCS_CMDERR_OFFSET 5 #define DMI_ABSTRACTCS_CMDERR_LENGTH 3 #define DMI_ABSTRACTCS_CMDERR (0x7 << DMI_ABSTRACTCS_CMDERR_OFFSET) +/* +* 1: An abstract command is currently being executed. +* +* This bit is set as soon as \Rcommand is written, and isn't cleared +* until that command has completed. + */ #define DMI_ABSTRACTCS_BUSY_OFFSET 4 #define DMI_ABSTRACTCS_BUSY_LENGTH 1 #define DMI_ABSTRACTCS_BUSY (0x1 << DMI_ABSTRACTCS_BUSY_OFFSET) +/* +* Number of {\tt data} registers that are implemented as part of the +* abstract command interface. If it's 0 then no abstract interface is +* implemented at all. + */ #define DMI_ABSTRACTCS_DATACOUNT_OFFSET 0 #define DMI_ABSTRACTCS_DATACOUNT_LENGTH 4 #define DMI_ABSTRACTCS_DATACOUNT (0xf << DMI_ABSTRACTCS_DATACOUNT_OFFSET) @@ -325,9 +658,15 @@ #define DMI_SERDATA_DATA_LENGTH 32 #define DMI_SERDATA_DATA (0xffffffff << DMI_SERDATA_DATA_OFFSET) #define DMI_SERSTATUS 0x1d +/* +* Number of supported serial ports. + */ #define DMI_SERSTATUS_SERIALCOUNT_OFFSET 28 #define DMI_SERSTATUS_SERIALCOUNT_LENGTH 4 #define DMI_SERSTATUS_SERIALCOUNT (0xf << DMI_SERSTATUS_SERIALCOUNT_OFFSET) +/* +* Select which serial port is accessed by \Rserdata. + */ #define DMI_SERSTATUS_SERIAL_OFFSET 16 #define DMI_SERSTATUS_SERIAL_LENGTH 3 #define DMI_SERSTATUS_SERIAL (0x7 << DMI_SERSTATUS_SERIAL_OFFSET) @@ -373,13 +712,30 @@ #define DMI_SERSTATUS_FULL_OVERFLOW1_OFFSET 2 #define DMI_SERSTATUS_FULL_OVERFLOW1_LENGTH 1 #define DMI_SERSTATUS_FULL_OVERFLOW1 (0x1 << DMI_SERSTATUS_FULL_OVERFLOW1_OFFSET) +/* +* 1 when the core-to-debugger queue for serial port 0 is not empty. + */ #define DMI_SERSTATUS_VALID0_OFFSET 1 #define DMI_SERSTATUS_VALID0_LENGTH 1 #define DMI_SERSTATUS_VALID0 (0x1 << DMI_SERSTATUS_VALID0_OFFSET) +/* +* 1 when the debugger-to-core queue for serial port 0 is either full, +* or has overflowed. Overflow state is sticky, and can be reset by +* writing 0 to this bit. + */ #define DMI_SERSTATUS_FULL_OVERFLOW0_OFFSET 0 #define DMI_SERSTATUS_FULL_OVERFLOW0_LENGTH 1 #define DMI_SERSTATUS_FULL_OVERFLOW0 (0x1 << DMI_SERSTATUS_FULL_OVERFLOW0_OFFSET) #define DMI_ACCESSCS 0x1f +/* +* Size of the Program Buffer, in 32-bit words. Valid sizes are 0 - 12. +* +* A debugger must not access any Instruction Buffer locations that +* fall outside the range specified here. +* +* TODO: Explain what can be done with each size of the buffer, to suggest +* why you would want more or less words. + */ #define DMI_ACCESSCS_PROGSIZE_OFFSET 0 #define DMI_ACCESSCS_PROGSIZE_LENGTH 4 #define DMI_ACCESSCS_PROGSIZE (0xf << DMI_ACCESSCS_PROGSIZE_OFFSET) @@ -399,36 +755,68 @@ #define DMI_IBUF10 0x2a #define DMI_IBUF11 0x2b #define SERINFO 0x110 +/* +* Like \Fserialzero. + */ #define SERINFO_SERIAL7_OFFSET 7 #define SERINFO_SERIAL7_LENGTH 1 #define SERINFO_SERIAL7 (0x1 << SERINFO_SERIAL7_OFFSET) +/* +* Like \Fserialzero. + */ #define SERINFO_SERIAL6_OFFSET 6 #define SERINFO_SERIAL6_LENGTH 1 #define SERINFO_SERIAL6 (0x1 << SERINFO_SERIAL6_OFFSET) +/* +* Like \Fserialzero. + */ #define SERINFO_SERIAL5_OFFSET 5 #define SERINFO_SERIAL5_LENGTH 1 #define SERINFO_SERIAL5 (0x1 << SERINFO_SERIAL5_OFFSET) +/* +* Like \Fserialzero. + */ #define SERINFO_SERIAL4_OFFSET 4 #define SERINFO_SERIAL4_LENGTH 1 #define SERINFO_SERIAL4 (0x1 << SERINFO_SERIAL4_OFFSET) +/* +* Like \Fserialzero. + */ #define SERINFO_SERIAL3_OFFSET 3 #define SERINFO_SERIAL3_LENGTH 1 #define SERINFO_SERIAL3 (0x1 << SERINFO_SERIAL3_OFFSET) +/* +* Like \Fserialzero. + */ #define SERINFO_SERIAL2_OFFSET 2 #define SERINFO_SERIAL2_LENGTH 1 #define SERINFO_SERIAL2 (0x1 << SERINFO_SERIAL2_OFFSET) +/* +* Like \Fserialzero. + */ #define SERINFO_SERIAL1_OFFSET 1 #define SERINFO_SERIAL1_LENGTH 1 #define SERINFO_SERIAL1 (0x1 << SERINFO_SERIAL1_OFFSET) +/* +* 1 means serial interface 0 is supported. + */ #define SERINFO_SERIAL0_OFFSET 0 #define SERINFO_SERIAL0_LENGTH 1 #define SERINFO_SERIAL0 (0x1 << SERINFO_SERIAL0_OFFSET) #define SERSEND0 0x200 #define SERRECV0 0x204 #define SERSTAT0 0x208 +/* +* Send ready. 1 when the core-to-debugger queue is not full. 0 +* otherwise. + */ #define SERSTAT0_SENDR_OFFSET 1 #define SERSTAT0_SENDR_LENGTH 1 #define SERSTAT0_SENDR (0x1 << SERSTAT0_SENDR_OFFSET) +/* +* Receive ready. 1 when the debugger-to-core queue is not empty. 0 +* otherwise. + */ #define SERSTAT0_RECVR_OFFSET 0 #define SERSTAT0_RECVR_LENGTH 1 #define SERSTAT0_RECVR (0x1 << SERSTAT0_RECVR_OFFSET) @@ -458,12 +846,39 @@ #define CSR_TSELECT_INDEX_LENGTH XLEN #define CSR_TSELECT_INDEX (((1L<= DEBUG_ROM_ENTRY && addr < DEBUG_ROM_ENTRY + DEBUG_ROM_ENTRY_SIZE) { - halted[(addr - DEBUG_ROM_ENTRY) / 4] = true; - memcpy(bytes, debug_rom_entry + addr - DEBUG_ROM_ENTRY, len); 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); return true; } - // Restore the jump-to-self loop. - write32(debug_rom_entry, dmcontrol.hartsel, jal(ZERO, 0)); + 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_CODE && addr < DEBUG_ROM_CODE + DEBUG_ROM_CODE_SIZE) { @@ -138,6 +144,11 @@ bool debug_module_t::load(reg_t addr, size_t len, uint8_t* bytes) return true; } + if (addr >= DEBUG_RAM_START && addr < DEBUG_RAM_END) { + memcpy(bytes, program_buffer + addr - DEBUG_RAM_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); @@ -157,6 +168,11 @@ bool debug_module_t::store(reg_t addr, size_t len, const uint8_t* bytes) { addr = DEBUG_START + addr; + if (addr >= DEBUG_RAM_START && addr < DEBUG_RAM_END) { + memcpy(program_buffer + addr - DEBUG_RAM_START, bytes, len); + return true; + } + fprintf(stderr, "ERROR: invalid store to debug module: %zd bytes at 0x%016" PRIx64 "\n", len, addr); return false; @@ -198,7 +214,7 @@ bool debug_module_t::dmi_read(unsigned address, uint32_t *value) if (address >= DMI_DATA0 && address < DMI_DATA0 + abstractcs.datacount) { result = dmdata.read32(4 * (address - DMI_DATA0)); } else if (address >= DMI_IBUF0 && address < DMI_IBUF0 + progsize) { - result = ibuf[address - DMI_IBUF0]; + result = read32(program_buffer, address - DMI_IBUF0); } else { switch (address) { case DMI_DMCONTROL: @@ -306,10 +322,22 @@ bool debug_module_t::perform_abstract_command(uint32_t command) abstractcs.cmderr = abstractcs.CMDERR_NOTSUP; return true; } - write32(debug_rom_code, 1, ebreak()); + 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()); + } + + 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)); + } else { + write32(debug_rom_entry, dmcontrol.hartsel, + jal(ZERO, DEBUG_ROM_CODE - (DEBUG_ROM_ENTRY + 4 * dmcontrol.hartsel))); + } - 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)); abstractcs.busy = true; @@ -326,7 +354,7 @@ bool debug_module_t::dmi_write(unsigned address, uint32_t value) dmdata.write32(4 * (address - DMI_DATA0), value); return true; } else if (address >= DMI_IBUF0 && address < DMI_IBUF0 + progsize) { - ibuf[address - DMI_IBUF0] = value; + write32(program_buffer, address - DMI_IBUF0, value); return true; } else { switch (address) { diff --git a/riscv/debug_module.h b/riscv/debug_module.h index 15ff66c..fdeaaac 100644 --- a/riscv/debug_module.h +++ b/riscv/debug_module.h @@ -101,25 +101,30 @@ class debug_module_t : public abstract_device_t bool dmi_write(unsigned address, uint32_t value); private: + static const unsigned progsize = 8; + sim_t *sim; // Track which interrupts from module to debugger are set. std::set interrupt; // Track which halt notifications from debugger to module are set. std::set halt_notification; + uint8_t debug_rom_entry[DEBUG_ROM_ENTRY_SIZE]; uint8_t debug_rom_code[DEBUG_ROM_CODE_SIZE]; uint8_t debug_rom_exception[DEBUG_ROM_EXCEPTION_SIZE]; + uint8_t program_buffer[progsize * 4]; bool halted[1024]; debug_module_data_t dmdata; + // Instruction that will be placed at the current hart's ROM entry address + // after the current action has completed. + uint32_t next_action; + bool action_executed; void write32(uint8_t *rom, unsigned int index, uint32_t value); uint32_t read32(uint8_t *rom, unsigned int index); - static const unsigned progsize = 8; - dmcontrol_t dmcontrol; abstractcs_t abstractcs; - uint32_t ibuf[progsize]; processor_t *current_proc() const; void reset(); -- 2.30.2