Webcentive RISC-V Reference

dret

Return from Debug Mode

Extension
Sdext
XLEN
RV32 and RV64
Assembly (riscv-unified-db)
dret
Length
32-bit

Encoding

3120191514121176210
01111011001000000000000001110011

Match 0x7b200073, mask 0xffffffff. Source: dret 11..7=0 19..15=0 31..20=0x7b2 14..12=0 6..2=0x1C 1..0=3 in riscv-opcodes/extensions/rv_sdext.

Description

Resumes execution after Debug Mode. dret is only legal in Debug Mode; executing it in any other mode raises an Illegal Instruction exception.

When executed in Debug Mode, dret:

  1. Sets the PC to the value in dpc.
  2. Changes the privilege and virtualization mode to those held in dcsr.PRV and dcsr.V.
  3. If the new privilege mode is less privileged than M-mode, clears mstatus.MPRV.
  4. If Smdbltrp is implemented and the new privilege mode is less privileged than M-mode, clears mstatus.MDT.
  5. If Ssdbltrp is implemented and the new privilege mode is U, VS, or VU, clears sstatus.SDT; if the new mode is VU, also clears vsstatus.SDT.
  6. If Zicfilp is implemented, sets ELP to dcsr.PELP if Zicfilp is enabled (xLPE=1) in the new privilege mode, otherwise to NO_LP_EXPECTED, and then clears dcsr.PELP.
  7. Leaves Debug Mode.

Description from riscv-unified-db, reformatted.

Operation

# UDB does not yet model Debug Mode state, so the hart can never be in Debug
# Mode and dret is always illegal, regardless of privilege mode.
# TODO: once Debug Mode is modeled, raise this only when the hart is not in
# Debug Mode.
raise (ExceptionCode::IllegalInstruction, mode(), $encoding);

# determine the resume mode from dcsr.PRV/dcsr.V
PrivilegeMode new_mode = PrivilegeMode::M;
if (CSR[dcsr].PRV == 2'b11) {
  new_mode = PrivilegeMode::M;
} else if (CSR[dcsr].PRV == 2'b01) {
  new_mode = PrivilegeMode::S;
  if (implemented?(ExtensionName::H)) {
    if (CSR[dcsr].V == 1'b1) {
      new_mode = PrivilegeMode::VS;
    }
  }
} else if (CSR[dcsr].PRV == 2'b00) {
  new_mode = PrivilegeMode::U;
  if (implemented?(ExtensionName::H)) {
    if (CSR[dcsr].V == 1'b1) {
      new_mode = PrivilegeMode::VU;
    }
  }
} else {
  unpredictable("dcsr.PRV holds a reserved encoding; the hart may resume in any supported privilege mode");
}

if (new_mode != PrivilegeMode::M) {
  if (implemented?(ExtensionName::U)) {
    CSR[mstatus].MPRV = 0;
  }
  if (implemented?(ExtensionName::Smdbltrp)) {
    CSR[mstatus].MDT = 1'b0;
  }
}

# TODO: Ssdbltrp: if new_mode is U, VS, or VU, clear sstatus.SDT; if new_mode
# is VU, also clear vsstatus.SDT. UDB does not yet define the SDT fields.

if (implemented?(ExtensionName::Zicfilp)) {
  # TODO: set ELP to dcsr.PELP if xLPE is enabled for new_mode, otherwise to
  # NO_LP_EXPECTED. UDB does not yet model the ELP state, so this restore
  # must happen before dcsr.PELP is cleared below once it is added.
  CSR[dcsr].PELP = 1'b0;
}

set_mode(new_mode);
$pc = $bits(CSR[dpc]);

Pseudocode in riscv-unified-db’s IDL, quoted unchanged.

Specification and related pages