Home/Instructions/Machine-mode Return
MRET

RISC-V MRET Instruction Details

Instruction ManualI-type

Machine trap return: pc<-mepc, privilege<-mstatus.MPP, MIE<-MPIE.

Instruction Syntax

mret
Operand Breakdown
Destination rd: general-purpose register receiving the result.
Source rs1: register holding the first operand.
Immediate imm: 12-bit signed value, sign-extended before operation with rs1.
PrivilegedSystem & Privileged

Instruction Encoding

31..20
imm[11:0]
19..15
rs1
14..12
funct3
11..7
rd
6..0
opcode

MRET uses opcode 1110011 (0x73), funct3 000, funct12 001100000010. The rs1 field selects the source register, the 12-bit immediate provides the second operand, and rd selects the destination.

Format: I-type
opcode: 1110011 (0x73)
funct3: 000 (0x0)
funct12: 001100000010 (0x302)

Instruction Behavior

MRET returns from a machine-mode trap handler: pc is set to mepc, the current privilege mode is restored from mstatus.MPP, MIE is restored from MPIE, MPIE is set to 1, and MPP is set to the least-privileged supported mode. With the H extension, virtualization state is also restored according to MPV/MPP. MRET is legal only in M-mode; execution in a lower privilege mode raises an illegal-instruction exception.

Quick Understanding & Search Notes

MRET is a system-level instruction defined by the privileged architecture. This page covers architectural semantics only, not platform firmware policy.

Execution permission, trap type, and state restoration depend on privilege level, relevant CSR bits, and extension presence.
Executing at an unsupported privilege level or without the required extension raises the specified exception.

Common Usage Scenarios

M-mode handler return

Understand this scenario with real code like «mret # Return from M-mode trap».

Firmware/SEE return

Understand this scenario with real code like «mret # Return from M-mode trap».

Pre-Use Checklist

Syntax Check
  • Confirm the current instruction format is I-type.
  • Confirm the operand order matches the example.
Semantic Check
  • Ensure the destination register usage is compatible with the calling convention.
  • Confirm this is not the lower-level form of a pseudo-instruction expansion.

Pitfalls / Common Confusions

Legal only in M-mode; execution in S/U-mode raises an illegal-instruction exception.
xRET may clear an LR/SC reservation but is not required to; handlers should explicitly clear it if needed.
MRET restores the state saved in MPP/MPIE; set mepc and related CSRs for the target context before returning.

FAQ

Is it a normal user-mode instruction?

No. These instructions are constrained by privilege level and extension support; user-mode legality must follow the privileged rules.

Does it replace ordinary memory fences?

No. Address translation, instruction fetch, and data-memory ordering use different fence/invalidation instructions.