CSR Bit Fields

RISC-V mscratch CSR Register

Address 0x340Privilege MachineAccess RW / MXLENMachine status, trap, and interrupt CSRs

Machine-mode scratch register, an MXLEN-bit read/write CSR dedicated for M-mode software use.

Field Map

Understand mscratch By Bit Fields

1 key fields
MXLEN-1:0

VALUE

RW

Complete scratch value for M-mode use; the privileged architecture does not define internal bit fields for this value.

VALUE (bits MXLEN-1:0) — Complete scratch value for M-mode use; the privileged architecture does not define internal bit fields for this value.

What This Field Controls

  • - Complete scratch value for M-mode use; the privileged architecture does not define internal bit fields for this value.

Common Values

This field is better understood together with surrounding context than as a fixed memorized enumeration.

Official Basis & Search Notes

mscratch is the MXLEN-bit Machine-mode read/write scratch CSR at 0x340. It typically holds a hart-local context pointer; its contents are defined by M-mode software convention, not by hardware bit fields.

The official manual defines mscratch as an MXLEN-bit read/write register dedicated for machine mode.
A typical use is swapping it with a user register on entry to an M-mode trap handler to obtain a machine-mode hart-local context-space pointer.
mscratch has no standardized internal bit fields; the VALUE row represents the complete software-managed value.

What To Check First When Reading This CSR

  • - mscratch is an MXLEN-bit Machine-mode read/write CSR at 0x340.
  • - The meaning of the value is defined by M-mode software convention; hardware does not automatically interpret it for ordinary traps.
  • - Access with insufficient privilege or to an unimplemented CSR raises an illegal-instruction exception.

Risk Checks Before Writing

  • - Writing mscratch changes the scratch value that M-mode trap-entry software may rely on.
  • - If it is used as a hart-local context pointer, keep the same software convention across trap entry, nested traps, and return paths.

Put It Back Into A Real Flow

1

M-mode initialization writes the agreed scratch value for each hart.

2

Trap entry swaps or reads mscratch with CSR instructions and a general-purpose register.

3

Trap handling and return code restore or update the value according to the same M-mode software convention.

FAQ

Can mscratch be accessed from any privilege level?

Do not decide from the CSR name alone. The official CSR table gives the lowest access privilege; mscratch is at 0x340 with Machine as the lowest access level. Access with insufficient privilege or to an unimplemented CSR raises an illegal-instruction exception.

What is easiest to miss when writing mscratch?

Do not treat mscratch as a hardware-defined bit-field register. The written value is interpreted by M-mode trap software convention, and an accidental overwrite can break the context pointer or temporary value used by trap entry.