← Home

sret

Supervisor Mode Return from Trap

Returns from supervisor mode after handling a trap.

When `sret` is allowed to execute, its behavior depends on whether or not the current privilege mode is virtualized.

*When the current privilege mode is (H)S-mode or M-mode*

`sret` sets `hstatus.HPV` = 0, `mstatus.SPP` = 0, `mstatus.SIE` = `mstatus.SPIE`, and `mstatus.SPIE` = 1, changes the privilege mode according to the table below, and then jumps to the address in `sepc`.

.Next privilege mode following an `sret` in (H)S-mode or M-mode [.rotate]#`mstatus.SPP`# — [.rotate]#`hstatus.SPV`# .> — Mode after `sret`

0 — 0 — U-mode 0 — 1 — VU-mode 1 — 0 — (H)S-mode 1 — 1 — VS-mode

*When the current privilege mode is VS-mode*

`sret` sets `vsstatus.SPP` = 0, `vsstatus.SIE` = `vstatus.SPIE`, and `vsstatus.SPIE` = 1, changes the privilege mode according to the table below, and then jumps to the address in `vsepc`.

.Next privilege mode following an `sret` in (H)S-mode or M-mode [.rotate]#`vsstatus.SPP`# .> — Mode after `sret`

0 — VU-mode 1 — VS-mode

Assembly format

sret

Encoding

313029282726252423222120191817161514131211109876543210
00010000001000000000000001110011

Execution

# first, check access requirements
if (implemented?(ExtensionName::H)) {
  if (CSR[mstatus].TSR == 1'b0 && CSR[hstatus].VTSR == 1'b0) {
    if (mode() == PrivilegeMode::U) {
      raise (ExceptionCode::IllegalInstruction, mode(), $encoding);
    } else if (mode() == PrivilegeMode::VU) {
      raise (ExceptionCode::VirtualInstruction, mode(), $encoding);
    }
  } else if (CSR[mstatus].TSR == 1'b0 && CSR[hstatus].VTSR == 1'b1) {
    if (mode() == PrivilegeMode::U) {
      raise (ExceptionCode::IllegalInstruction, mode(), $encoding);
    } else if (mode() == PrivilegeMode::VU || mode() == PrivilegeMode::VS) {
      raise (ExceptionCode::VirtualInstruction, mode(), $encoding);
    }
  } else if (CSR[mstatus].TSR == 1'b1 && CSR[hstatus].VTSR == 1'b0) {
    if (mode() == PrivilegeMode::U || mode() == PrivilegeMode::S) {
      raise (ExceptionCode::IllegalInstruction, mode(), $encoding);
    } else if (mode() == PrivilegeMode::VU) {
      raise (ExceptionCode::VirtualInstruction, mode(), $encoding);
    }
  } else if (CSR[mstatus].TSR == 1'b1 && CSR[hstatus].VTSR == 1'b1) {
    if (mode() == PrivilegeMode::U || mode() == PrivilegeMode::S) {
      raise (ExceptionCode::IllegalInstruction, mode(), $encoding);
    } else if (mode() == PrivilegeMode::VU || mode() == PrivilegeMode::VS) {
      raise (ExceptionCode::VirtualInstruction, mode(), $encoding);
    }
  }
} else {
  if (mode() == PrivilegeMode::U) {
    raise (ExceptionCode::IllegalInstruction, mode(), $encoding);
  }
}

# now pop the interrupt stack and change modes
if (!virtual_mode?()) {
  if (implemented?(ExtensionName::H)) {
    if (CSR[hstatus].SPV == 1'b1) {
      if (CSR[mstatus].SPP == 1'b1) {
        set_mode(PrivilegeMode::VS);
      } else {
        set_mode(PrivilegeMode::VU);
      }
    } else {
      if (CSR[mstatus].SPP == 1'b1) {
        set_mode(PrivilegeMode::S);
      } else {
        set_mode(PrivilegeMode::U);
      }
    }
    CSR[hstatus].SPV = 0;
  } else {
    if (CSR[mstatus].SPP == 1'b1) {
      set_mode(PrivilegeMode::S);
    } else {
      set_mode(PrivilegeMode::U);
    }
  }
  CSR[mstatus].SIE = CSR[mstatus].SPIE;
  CSR[mstatus].SPIE = 1;
  CSR[mstatus].SPP = 2'b00;
  $pc = CSR[sepc].sw_read();
} else if (implemented?(ExtensionName::H)) {
  if (CSR[mstatus].TSR == 1'b1) {
    raise (ExceptionCode::IllegalInstruction, mode(), $encoding);
  }
  CSR[vsstatus].SPP = 0;
  CSR[vsstatus].SIE = CSR[vsstatus].SPIE;
  CSR[vsstatus].SPIE = 1;
  $pc = CSR[vsepc].sw_read();
} else {
  unreachable();
}

Defined by

S

Access

SUVSVU
SometimesNeverSometimesNever

Version af3eaf9+cheriot.f635218

Data from the RISC-V Unified Database, copyright RISC-V International and contributors.

CHERIoT extensions from the CHERIoT specification and Sail model, copyright Microsoft and the CHERIoT Platform contributors.

This site is provided as a community resource and is NOT affiliated with nor endorsed by RISC-V International.