System Instructions

Environment interaction

There are two instructions to interact with the operating system:

  • ecall for system calls
  • ebreak for calling a debugger

Control and Status Registers (CSRs)

Control and Status Registers (CSRs) provide a general facility for system control and I/O. There is a CSR address space for up to 212 registers.

The instructions to modify CSRs are:

instr using rs1instr using immdescription
csrrw rd, csr, rs1csrrwi rd, csr, immatomically copy a value from csr to rd and
overwrite csr with the value in rs1 or imm
csrrc rd, csr, rs1csrrci rd, csr, immatomically copy a value from csr to rd and
clear bits in csr 1
csrrs rd, csr, rs1csrrsi rd, csr, immatomically copy a value from csr to rd and
set bits in a csr 1

Note: csrrs x1, csr, x0 can be used to read from csr without modifying it. It is abbreviated as a pseudoinstruciton csrr rd, csr.

Mandatory user-readable CSRs

CSRatdescription
cycle0xC00cycle counter
cycleh0xC80upper 32 bit of cycle counter
time0xC01real-time clock
timeh0xC81upper 32 bit of real-time clock
instret0xC02instructions retired counter
instreth0xC82upper 32 bit of instret

Encoding

All of the following are in I-format:

imm[11:0]rs1funct3rdopcode
ecall0000 0000 0000000000000000011 100 11
ebreak0000 0000 0001000000000000011 100 11
csrrw00111 100 11
csrrs01011 100 11
csrrc01111 100 11
csrrwi10111 100 11
csrrci11011 100 11
csrrsi11111 100 11

1 TODO: according to the mask in rs1/imm ?