|
| 1 | +[[sec:core_debug]] |
| 2 | +== Sdext (ISA Extension) |
| 3 | + |
| 4 | +This chapter describes the Sdext ISA extension. It must be implemented |
| 5 | +to make external debug work, and is only useful in conjunction with |
| 6 | +external debug. |
| 7 | + |
| 8 | +Modifications to the RISC-V core to support debug are kept to a minimum. |
| 9 | +There is a special execution mode (Debug Mode) and a few extra CSRs. The |
| 10 | +DM takes care of the rest. |
| 11 | + |
| 12 | +In order to be compatible with this specification an implementation must |
| 13 | +implement everything described in this chapter that is not explicitly |
| 14 | +listed as optional. |
| 15 | + |
| 16 | +If Sdext is implemented and Sdtrig is not implemented, then accessing |
| 17 | +any of the Sdtrig CSRs must raise an illegal instruction exception. |
| 18 | + |
| 19 | +[[debugmode]] |
| 20 | +=== Debug Mode |
| 21 | + |
| 22 | +Debug Mode is a special processor mode used only when a hart is halted |
| 23 | +for external debugging. Because the hart is halted, there is no forward |
| 24 | +progress in the normal instruction stream. How Debug Mode is implemented |
| 25 | +is not specified here. |
| 26 | + |
| 27 | +When executing code due to an abstract command, the hart stays in Debug |
| 28 | +Mode and the following apply: |
| 29 | + |
| 30 | +All implemented instructions operate just as they do in M-mode, unless |
| 31 | +an exception is mentioned in this list. |
| 32 | + |
| 33 | +All operations are executed with machine mode privilege, except that |
| 34 | +additional Debug Mode CSRs are accessible and in may be ignored |
| 35 | +according to . Full permission checks, or a relaxed set of permission |
| 36 | +checks, will apply according to . |
| 37 | + |
| 38 | +All interrupts (including NMI) are masked. |
| 39 | + |
| 40 | +Traps don’t take place. Instead, they end execution of the program |
| 41 | +buffer and the hart remains in Debug Mode. Because they do not trap to |
| 42 | +M-mode, they do not update registers such as , `mepc`, `mcause`, |
| 43 | +` mtval`, `mtval2`, and `mtinst`. The same is true for the equivalent |
| 44 | +privileged registers that are updated when trapping to other modes. |
| 45 | +Registers that may be updated as part of execution before the exception |
| 46 | +are allowed to be updated. For example, vector load/store instructions |
| 47 | +which raise exceptions may partially update the destination register and |
| 48 | +set `vstart` appropriately. |
| 49 | + |
| 50 | +Triggers don’t match or fire. |
| 51 | + |
| 52 | +If is 0 then counters continue. If it is 1 then counters are stopped. |
| 53 | + |
| 54 | +If is 0 then continues to update. If it is 1 then will not update. It |
| 55 | +will resynchronize with after leaving Debug Mode. |
| 56 | + |
| 57 | +Instructions that place the hart into a stalled state act as a `nop`. |
| 58 | +This includes `wfi`, `wrs.sto`, and `wrs.nto`. |
| 59 | + |
| 60 | +Almost all instructions that change the privilege mode have behavior. |
| 61 | +This includes `ecall`, `mret`, `sret`, and `uret`. (To change the |
| 62 | +privilege mode, the debugger can write and in ). The only exception is |
| 63 | +`ebreak`, which ends execution of the Program Buffer when executed. |
| 64 | + |
| 65 | +All control transfer instructions may act as illegal instructions if |
| 66 | +their destination is in the Program Buffer. If one such instruction acts |
| 67 | +as an illegal instruction, all such instructions must act as illegal |
| 68 | +instructions. |
| 69 | + |
| 70 | +All control transfer instructions may act as illegal instructions if |
| 71 | +their destination is outside the Program Buffer. If one such instruction |
| 72 | +acts as an illegal instruction, all such instructions must act as |
| 73 | +illegal instructions. |
| 74 | + |
| 75 | +Instructions that depend on the value of the PC (e.g. `auipc`) may act |
| 76 | +as illegal instructions. |
| 77 | + |
| 78 | +Effective XLEN is DXLEN. |
| 79 | + |
| 80 | +Forward progress is guaranteed. |
| 81 | + |
| 82 | +When , the external debugger can set MPRV and MPP appropriately to have |
| 83 | +hardware perform memory accesses with the appropriate endianness, |
| 84 | +address translation, permission checks, and PMP/PMA checks (subject to |
| 85 | +). This is also the only way to access all of physical memory when |
| 86 | +34-bit physical addresses are supported on a Sv32 hart. If hardware ties |
| 87 | +to 0 then the external debugger is expected to simulate all the effects |
| 88 | +of MPRV, including any extensions that affect memory accesses. For these |
| 89 | +reasons it is recommended to tie to 1. |
| 90 | + |
| 91 | +=== Load-Reserved/Store-Conditional Instructions |
| 92 | + |
| 93 | +The reservation registered by an `lr` instruction on a memory address |
| 94 | +may be lost when entering Debug Mode or while in Debug Mode. This means |
| 95 | +that there may be no forward progress if Debug Mode is entered between |
| 96 | +`lr` and ` sc` pairs. |
| 97 | + |
| 98 | +This is a behavior that debug users must be aware of. If they have a |
| 99 | +breakpoint set between a `lr` and `sc` pair, or are stepping through |
| 100 | +such code, the `sc` may never succeed. Fortunately in general use there |
| 101 | +will be very few instructions in such a sequence, and anybody debugging |
| 102 | +it will quickly notice that the reservation is not occurring. The |
| 103 | +solution in that case is to set a breakpoint on the first instruction |
| 104 | +after the `sc` and run to it. A higher level debugger may choose to |
| 105 | +automate this. |
| 106 | + |
| 107 | +=== Wait for Interrupt Instruction |
| 108 | + |
| 109 | +If halt is requested while `wfi` is executing, then the hart must leave |
| 110 | +the stalled state, completing this instruction’s execution, and then |
| 111 | +enter Debug Mode. |
| 112 | + |
| 113 | +=== Wait-on-Reservation-Set Instructions |
| 114 | + |
| 115 | +If halt is requested while `wrs.sto` or `wrs.nto` is executing, then the |
| 116 | +hart must leave the stalled state, completing this instruction’s |
| 117 | +execution, and then enter Debug Mode. |
| 118 | + |
| 119 | +=== Single Step |
| 120 | + |
| 121 | +[[stepBit]] |
| 122 | +==== Step Bit In Dcsr |
| 123 | + |
| 124 | +This method is only available to external debuggers, and is the |
| 125 | +preferred way to single step. |
| 126 | + |
| 127 | +An external debugger can cause a halted hart to execute a single |
| 128 | +instruction or trap and then re-enter Debug Mode by setting before |
| 129 | +resuming. If is set when a hart resumes then it will single step, |
| 130 | +regardless of the reason for resuming. |
| 131 | + |
| 132 | +If control is transferred to a trap handler while executing the |
| 133 | +instruction, then Debug Mode is re-entered immediately after the PC is |
| 134 | +changed to the trap handler, and the appropriate `tval` and `cause` |
| 135 | +registers are updated. In this case none of the trap handler is |
| 136 | +executed, and if the cause was a pending interrupt no instructions might |
| 137 | +be executed at all. |
| 138 | + |
| 139 | +If executing or fetching the instruction causes a trigger to fire with |
| 140 | +action=1, Debug Mode is re-entered immediately after that trigger has |
| 141 | +fired. In that case is set to 2 (trigger) instead of 4 (single step). |
| 142 | +Whether the instruction is executed or not depends on the specific |
| 143 | +configuration of the trigger. |
| 144 | + |
| 145 | +If the instruction that is executed causes the PC to change to an |
| 146 | +address where an instruction fetch causes an exception, that exception |
| 147 | +does not occur until the next time the hart is resumed. Similarly, a |
| 148 | +trigger at the new address does not fire until the hart actually |
| 149 | +attempts to execute that instruction. |
| 150 | + |
| 151 | +If the instruction being stepped over would normally stall the hart, |
| 152 | +then instead the instruction is treated as a `nop`. This includes `wfi`, |
| 153 | +`wrs.sto`, and `wrs.nto`. |
| 154 | + |
| 155 | +[[stepIcount]] |
| 156 | +==== Icount Trigger |
| 157 | + |
| 158 | +Native debuggers won’t have access to , but can use the trigger by |
| 159 | +setting to 1. |
| 160 | + |
| 161 | +This approach does have some limitations: |
| 162 | + |
| 163 | +Interrupts will fire as usual. Debuggers that want to disable interrupts |
| 164 | +while stepping must disable them by changing , and specially handle |
| 165 | +instructions that read . |
| 166 | + |
| 167 | +`wfi` instructions are not treated specially and might take a very long |
| 168 | +time to complete. |
| 169 | + |
| 170 | +This mechanism cleanly supports a system which supports multiple |
| 171 | +privilege levels, where the OS or a debug stub runs in M-Mode while the |
| 172 | +program being debugged runs in a less privileged mode. Systems that only |
| 173 | +support M-Mode can use as well, but count must be able to count several |
| 174 | +instructions (depending on the software implementation). See Section |
| 175 | +link:#nativestep[[nativestep]]. |
| 176 | + |
| 177 | +=== Reset |
| 178 | + |
| 179 | +If the halt signal (driven by the hart’s halt request bit in the Debug |
| 180 | +Module) or are asserted when a hart comes out of reset, the hart must |
| 181 | +enter Debug Mode before executing any instructions, but after performing |
| 182 | +any initialization that would usually happen before the first |
| 183 | +instruction is executed. |
| 184 | + |
| 185 | +=== Halt |
| 186 | + |
| 187 | +When a hart halts: |
| 188 | + |
| 189 | +is updated. |
| 190 | + |
| 191 | +and are set to reflect current privilege mode. |
| 192 | + |
| 193 | +is set to the next instruction that should be executed. |
| 194 | + |
| 195 | +If the current instruction can be partially executed and should be |
| 196 | +restarted to complete, then the relevant state for that is updated. E.g. |
| 197 | +if a halt occurs during a partially executed vector instruction, then |
| 198 | +`vstart` is updated, and is updated to the address of the partially |
| 199 | +executed instruction. This is analogous to how vector instructions |
| 200 | +behave for exceptions. |
| 201 | + |
| 202 | +The hart enters Debug Mode. |
| 203 | + |
| 204 | +=== Resume |
| 205 | + |
| 206 | +When a hart resumes: |
| 207 | + |
| 208 | +changes to the value stored in . |
| 209 | + |
| 210 | +The current privilege mode and virtualization mode are changed to that |
| 211 | +specified by and . |
| 212 | + |
| 213 | +If the new privilege mode is less privileged than M-mode, in is cleared. |
| 214 | + |
| 215 | +The hart is no longer in debug mode. |
| 216 | + |
| 217 | +=== XLEN |
| 218 | + |
| 219 | +While in Debug Mode, XLEN is DXLEN. It is up to the debugger to |
| 220 | +determine the XLEN during normal program execution (by looking at ) and |
| 221 | +to clearly communicate this to the user. |
| 222 | + |
| 223 | +[[debreg]] |
| 224 | +=== Core Debug Registers |
| 225 | + |
| 226 | +The supported Core Debug Registers must be implemented for each hart |
| 227 | +that can be debugged. They are CSRs, accessible using the RISC-V `csr` |
| 228 | +opcodes and optionally also using abstract debug commands. |
| 229 | + |
| 230 | +Attempts to access an unimplemented Core Debug Register raise an illegal |
| 231 | +instruction exception. |
| 232 | + |
| 233 | +[[virtreg]] |
| 234 | +=== Virtual Debug Registers |
0 commit comments