Webcentive RISC-V Reference

lr.d

Load Reserved Doubleword

Extension
A
XLEN
RV64 only
Assembly (riscv-unified-db)
lr.d xd, (xs1)
Length
32-bit

Encoding

3129282726252420191514121176210
00010aqrl00000rs1011rd0101111

Match 0x1000302f, mask 0xf9f0707f. Source: lr.d rd rs1 24..20=0 aq rl 31..29=0 28..27=2 14..12=3 6..2=0x0B 1..0=3 in riscv-opcodes/extensions/rv64_a.

FieldBitsriscv-opcodes name
aq26aq
rl25rl
rs119:15rs1
rd11:7rd

Description

Loads a doubleword from the address in xs1, places the value in xd, and registers a reservation set -- a set of bytes that subsumes the bytes in the addressed doubleword.

The address in xs1 must be 8-byte aligned.

If the address is not naturally aligned, a LoadAddressMisaligned exception or an LoadAccessFault exception will be generated. The access-fault exception can be generated for a memory access that would otherwise be able to complete except for the misalignment, if the misaligned access should not be emulated.

An implementation can register an arbitrarily large reservation set on each LR, provided the reservation set includes all bytes of the addressed data word or doubleword. An SC can only pair with the most recent LR in program order. An SC may succeed only if no store from another hart to the reservation set can be observed to have occurred between the LR and the SC, and if there is no other SC between the LR and itself in program order. An SC may succeed only if no write from a device other than a hart to the bytes accessed by the LR instruction can be observed to have occurred between the LR and SC. Note this LR might have had a different effective address and data size, but reserved the SC's address as part of the reservation set.

Following this model, in systems with memory translation, an SC is allowed to succeed if the
earlier LR reserved the same location using an alias with a different virtual address, but is
also allowed to fail if the virtual address is different.

To accommodate legacy devices and buses, writes from devices other than RISC-V harts are only
required to invalidate reservations when they overlap the bytes accessed by the LR.
These writes are not required to invalidate the reservation when they access other bytes in
the reservation set.

Software should not set the rl bit on an LR instruction unless the aq bit is also set. LR.rl and SC.aq instructions are not guaranteed to provide any stronger ordering than those with both bits clear, but may result in lower performance.

Description from riscv-unified-db, reformatted.

Operation

XReg virtual_address = X[xs1];

if (!is_naturally_aligned(64, virtual_address)) {
  # can raise either LoadAddressMisaligned *or* LoadAccessFault
  #
  # from the spec:
  #   If the address is not naturally aligned, an address-misaligned exception or
  #   an access-fault exception will be generated. The access-fault exception can
  #   be generated for a memory access that would otherwise be able to complete except
  #   for the misalignment, if the misaligned access should not be emulated.

  if (LRSC_MISALIGNED_BEHAVIOR == "always raise misaligned exception") {
    raise(ExceptionCode::LoadAddressMisaligned, effective_ldst_mode(), virtual_address);
  } else if (LRSC_MISALIGNED_BEHAVIOR == "always raise access fault") {
    raise(ExceptionCode::LoadAccessFault, effective_ldst_mode(), virtual_address);
  } else {
    unpredictable("Implementations may raise either a LoadAddressMisaligned or a LoadAccessFault when an LR/SC address is misaligned");
  }
}

XReg load_value = load_reserved(64, virtual_address, aq, rl, $encoding);
X[xd] = load_value;

Pseudocode in riscv-unified-db’s IDL, quoted unchanged.

Specification and related pages