wfi

Wait for interrupt

This instruction is defined by:

  • Sm, version >= 0

This instruction is included in the following profiles:

Encoding

svg

Assembly format

`wfi `

Synopsis

Can causes the processor to enter a low-power state until the next interrupt occurs.

<%- if ext?(:H) -%> The behavior of wfi is affected by the mstatus.TW and hstatus.VTW bits, as summarized below.

mstatus.TW

hstatus.VTW

wfi behavior

HS-mode

U-mode

VS-mode

in VU-mode

0

0

Wait

Trap (I)

Wait

Trap (V)

0

1

Wait

Trap (I)

Trap (V)

Trap (V)

1

-

Trap (I)

Trap (I)

Trap (I)

Trap (I)

Trap (I) - Trap with Illegal Instruction code
Trap (V) - Trap with Virtual Instruction code

<%- else -%> The wfi instruction is also affected by mstatus.TW, as shown below:

mstatus.TW

wfi behavior

S-mode

U-mode

0

Wait

Trap (I)

1

Trap (I)

Trap (I)

Trap (I) - Trap with Illegal Instruction code

<%- end -%>

When wfi is marked as causing a trap above, the implementation is allowed to wait for an unspecified period of time to see if an interrupt occurs before raising the trap. That period of time can be zero (i.e., wfi always causes a trap in the cases identified above).

Access

M HS U VS VU

Always

Sometimes

Sometimes

Sometimes

Sometimes

<%- if ext?(:H) -%> The behavior of wfi is affected by the mstatus.TW and hstatus.VTW bits, as summarized below.

mstatus.TW

hstatus.VTW

wfi behavior

HS-mode

U-mode

VS-mode

in VU-mode

0

0

Wait

Trap (I)

Wait

Trap (V)

0

1

Wait

Trap (I)

Trap (V)

Trap (V)

1

-

Trap (I)

Trap (I)

Trap (I)

Trap (I)

Trap (I) - Trap with Illegal Instruction code
Trap (V) - Trap with Virtual Instruction code

<%- else -%> The wfi instruction is also affected by mstatus.TW, as shown below:

mstatus.TW

wfi behavior

S-mode

U-mode

0

Wait

Trap (I)

1

Trap (I)

Trap (I)

Trap (I) - Trap with Illegal Instruction code

<%- end -%>

Decode Variables

Execution

  • IDL

  • Sail

if (mode() == PrivilegeMode::U) {
  raise(ExceptionCode::IllegalInstruction, mode(), $encoding);
}
if ((CSR[misa].S == 1) && (CSR[mstatus].TW == 1'b1)) {
  if (mode() != PrivilegeMode::M) {
    raise(ExceptionCode::IllegalInstruction, mode(), $encoding);
  }
}
if (CSR[misa].H == 1) {
  if (CSR[hstatus].VTW == 1'b0) {
    if (mode() == PrivilegeMode::VU) {
      raise(ExceptionCode::VirtualInstruction, mode(), $encoding);
    }
  } else if (CSR[hstatus].VTW == 1'b1) {
    if ((mode() == PrivilegeMode::VS) || (mode() == PrivilegeMode::VU)) {
      raise(ExceptionCode::VirtualInstruction, mode(), $encoding);
    }
  }
}
wfi();
match cur_privilege {
    Machine    => { platform_wfi(); RETIRE_SUCCESS },
    Supervisor => if   mstatus.TW() == 0b1
                  then { handle_illegal(); RETIRE_FAIL }
                  else { platform_wfi(); RETIRE_SUCCESS },
    User       => { handle_illegal(); RETIRE_FAIL }
  }

Exceptions

This instruction may result in the following synchronous exceptions:

  • IllegalInstruction

  • VirtualInstruction