Skip to content
46 changes: 37 additions & 9 deletions packages/river_hdl/lib/src/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,31 @@ class RiverCore extends BridgeModule {
config.mxlen.size,
config.mxlen.size,
);
// The current D-cache is virtually addressed before translation and has
// no physical PMA check on hits. Keep its existing alignment traps. Likewise
// do not advertise this path with unimplemented PMP/PBMT or H/OoO contexts.
final l1 = config.l1cache;
// Keep unsupported execution/translation profiles on the legacy path.
// Scalar Sv39 checks translation and permission before physical cache hits.
final physicalCaches =
l1 != null &&
!dualDispatch &&
l1.d.lineSize <= 4096 &&
(l1.i == null || l1.i!.lineSize <= 4096) &&
config.mmu.hasPaging &&
!config.hasHypervisor &&
!config.speculativeFetch &&
config.executionMode == ExecutionMode.inOrder &&
config.mmu.pmp.entries == 0 &&
!config.mmu.hasPageBasedMemoryTypes &&
config.mmu.pagingModes.every(
(m) =>
m == RiscVPagingMode.bare ||
(config.mxlen == RiscVMxlen.rv64 && m == RiscVPagingMode.sv39),
) &&
wbConfig.dataWidth == config.mxlen.size &&
wbConfig.addressWidth == config.mxlen.size &&
wbConfig.effectiveSelWidth == config.mxlen.size ~/ 8;

// Cached misaligned loads remain disabled, including on the physical path.
// Do not advertise this path with unimplemented PMP/PBMT or H/OoO contexts.
// A platform opts in by describing safe, full-beat-readable RAM in its PMAs.
final enableMisalignedLoads =
config.l1cache == null &&
Expand Down Expand Up @@ -430,7 +452,7 @@ class RiverCore extends BridgeModule {
execWriteActive,
writeLog2Size,
loadUnit == null
? Const(2, width: 3)
? (physicalCaches ? loadSize : Const(2, width: 3))
: mux(
loadUnit.restricted,
Const((config.mxlen.size ~/ 8).bitLength - 1, width: 3),
Expand Down Expand Up @@ -471,6 +493,7 @@ class RiverCore extends BridgeModule {
? mux(grantP1Now, pipeFetchRead1!.addr, pipeFetchRead.addr)
: pipeFetchRead.addr;

// Legacy L1 wiring (for profiles not using physical caches below).
// Optional L1 instruction cache between the fetch unit(s) and the MMU ifetch
// port. Hits in one cycle; misses fill a line from the MMU (its miss port
// drives the MMU ifetch instead of the arbiter). Dual-dispatch serves both
Expand All @@ -479,11 +502,10 @@ class RiverCore extends BridgeModule {
// fills one paced word at a time, so the PHY only sees single reads it
// captures correctly, never a back-to-back burst. The D-cache extends the
// same pacing to data loads.
final l1 = config.l1cache;
final useICache = l1?.i != null;
final useDCache = l1 != null;
final useICache = l1?.i != null && !physicalCaches;
final useDCache = l1 != null && !physicalCaches;

// Permission context for the L1 tags. Both caches are in FRONT of the MMU,
// Permission context for legacy L1 tags. Those caches are in FRONT of the MMU,
// so a HIT never reaches the MMU and no permission check runs on it. Without
// this tag a line filled by one privilege mode stayed usable by a mode the
// page table forbids: user code read a supervisor-only page out of the
Expand Down Expand Up @@ -649,6 +671,9 @@ class RiverCore extends BridgeModule {
translateFetch: config.mmu.hasPaging,
tlbFlush: config.mmu.hasPaging ? mmuTlbFlush : null,
dtlbFlushOnPrivChange: config.mmu.hasPaging ? dtlbFlushOnPriv : null,
physicalL1: physicalCaches ? l1 : null,
cacheTarget: target,
cacheFlush: physicalCaches ? icFlush : null,
userProbe: userProbe,
);

Expand Down Expand Up @@ -1688,6 +1713,7 @@ class RiverCore extends BridgeModule {
hstateen0Se0: csrs?.hstateen0Se0,
memFaultGuest: config.hasHypervisor ? mmu.dportFaultGuest : null,
enableMisalignedLoads: enableMisalignedLoads,
exactMemoryReads: physicalCaches,
loadFaultTval: loadUnit?.faultAddress,
memAccessFault: !handleAccessFaults
? null
Expand Down Expand Up @@ -1728,7 +1754,7 @@ class RiverCore extends BridgeModule {
staticInstructions: staticInstructions,
);

// Both L1 caches sit in FRONT of the MMU, so they are indexed and tagged by
// Legacy L1 caches sit in FRONT of the MMU, so they are indexed and tagged by
// VIRTUAL address. Every event that can re-point a virtual address at
// different memory must drop their contents: fence.i, sfence.vma, AND a satp
// write. The exec unit pulses `fence` for all three (see the WriteCsr steps,
Expand All @@ -1744,6 +1770,8 @@ class RiverCore extends BridgeModule {
// a process fetched another process's instructions. On creek this could not
// show (bare mode, VA == PA, one address space); under Sv39 it corrupted the
// machine at random.
// Physical L1s use the same flush signal conservatively. Translation and
// permission checks still precede every physical-cache lookup.
icFlush <= pipeline.fence;
mmuTlbFlush <= pipeline.fence;
loadMisaligned <= pipeline.output('misalignedLoad');
Expand Down
87 changes: 65 additions & 22 deletions packages/river_hdl/lib/src/core/exec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,9 @@ abstract class ExecutionUnit extends Module {
final bool hasSupervisor;
final bool hasUser;
final bool enableMisalignedLoads;
// Byte-addressed, sized reads with lane-zero responses from the MMU.
// Legacy front-of-MMU caches retain their existing read convention.
final bool exactMemoryReads;
Logic get misalignedLoad => output('misalignedLoad');
Logic get loadSize => output('loadSize');
late final Logic _misalignedLoadAllowed;
Expand Down Expand Up @@ -607,6 +610,7 @@ abstract class ExecutionUnit extends Module {
this.hasSupervisor = false,
this.hasUser = false,
this.enableMisalignedLoads = false,
this.exactMemoryReads = false,
Logic? loadFaultTval,
this.useIterativeMul = true,
required this.microcode,
Expand Down Expand Up @@ -676,6 +680,8 @@ abstract class ExecutionUnit extends Module {
addOutput('loadSize', width: 3);
if (!enableMisalignedLoads) {
misalignedLoad <= Const(0);
}
if (!enableMisalignedLoads && !exactMemoryReads) {
loadSize <= Const(2, width: 3);
}
_fetchFaultTval = fetchFaultTval == null
Expand Down Expand Up @@ -1359,7 +1365,8 @@ abstract class ExecutionUnit extends Module {
mopStep < 0,
done < 0,
fpFlags < 0,
if (enableMisalignedLoads) ...[misalignedLoad < 0, loadSize < 2],
if (enableMisalignedLoads) misalignedLoad < 0,
if (enableMisalignedLoads || exactMemoryReads) loadSize < 2,
output('trap') < 0,
output('trapInterrupt') < 0,
output('trapEpc') < currentPc,
Expand Down Expand Up @@ -1569,7 +1576,8 @@ abstract class ExecutionUnit extends Module {
this.csrWrite!.data < 0,
],
fence < 0,
if (enableMisalignedLoads) ...[misalignedLoad < 0, loadSize < 2],
if (enableMisalignedLoads) misalignedLoad < 0,
if (enableMisalignedLoads || exactMemoryReads) loadSize < 2,
interruptHold < 0,
nextPc < currentPc,
nextSp < currentSp,
Expand Down Expand Up @@ -1888,6 +1896,7 @@ class DynamicExecutionUnit extends ExecutionUnit {
super.hasSupervisor,
super.hasUser,
super.enableMisalignedLoads,
super.exactMemoryReads,
super.loadFaultTval,
required super.microcode,
required super.mxlen,
Expand Down Expand Up @@ -3455,17 +3464,17 @@ class DynamicExecutionUnit extends ExecutionUnit {
~_misalignedLoadAllowed,
then: doTrap(Trap.misalignedLoad, opBase + imm),
orElse: [
if (enableMisalignedLoads) ...[
if (enableMisalignedLoads)
misalignedLoad <
((opBase + imm) &
Const(
size.bytes - 1,
width: mxlen.size,
))
.neq(0),
if (enableMisalignedLoads || exactMemoryReads)
loadSize <
Const(size.bytes.bitLength - 1, width: 3),
],
memRead.en < 1,
memRead.addr < (opBase + imm),
],
Expand Down Expand Up @@ -3521,7 +3530,16 @@ class DynamicExecutionUnit extends ExecutionUnit {
))
.neq(0),
then: doTrap(Trap.misalignedLoad, opBase),
orElse: [memRead.en < 1, memRead.addr < opBase],
orElse: [
if (exactMemoryReads)
loadSize <
Const(
size.bytes.bitLength - 1,
width: 3,
),
memRead.en < 1,
memRead.addr < opBase,
],
),
],
),
Expand Down Expand Up @@ -3626,7 +3644,16 @@ class DynamicExecutionUnit extends ExecutionUnit {
))
.neq(0),
then: doTrap(Trap.misalignedStore, opBase),
orElse: [memRead.en < 1, memRead.addr < opBase],
orElse: [
if (exactMemoryReads)
loadSize <
Const(
size.bytes.bitLength - 1,
width: 3,
),
memRead.en < 1,
memRead.addr < opBase,
],
),
],
),
Expand Down Expand Up @@ -4155,6 +4182,7 @@ class StaticExecutionUnit extends ExecutionUnit {
super.rdWrite, {
super.hasSupervisor = false,
super.enableMisalignedLoads,
super.exactMemoryReads,
super.loadFaultTval,
super.hasUser = false,
required super.microcode,
Expand Down Expand Up @@ -4489,6 +4517,8 @@ class StaticExecutionUnit extends ExecutionUnit {
steps.add(
CaseItem(Const(3, width: maxLen.bitLength), [
memRead.addr < regBaseL,
if (exactMemoryReads)
loadSize < Const(chunkBytes.bitLength - 1, width: 3),
memRead.en < 1,
mopStep < mopStep + 1,
]),
Expand All @@ -4501,6 +4531,8 @@ class StaticExecutionUnit extends ExecutionUnit {
rs1 < memRead.data, // chunk 0
memRead.addr <
(regBaseL + Const(chunkBytes, width: mxlen.size)),
if (exactMemoryReads)
loadSize < Const(chunkBytes.bitLength - 1, width: 3),
memRead.en < 1, // chunk 1
mopStep < mopStep + 1,
],
Expand Down Expand Up @@ -5509,39 +5541,44 @@ class StaticExecutionUnit extends ExecutionUnit {
final unaligned =
(addr & Const(mop.size.bytes - 1, width: mxlen.size)).neq(0);

// Sub-word loads: the memory returns the aligned bus-word, so
// select the addressed lane by shifting right by the byte
// offset before slicing (lb/lbu/lh/lhu and Zcb c.lbu/lhu/lh).
// Legacy reads return the aligned bus-word and select a lane
// here. Physical-cache reads send the exact byte address/size;
// their MMU response is already normalized to lane zero.
final busBytes = mxlen.size ~/ 8;
final alignedAddr =
addr & ~Const(busBytes - 1, width: mxlen.size);
final byteOff = addr & Const(busBytes - 1, width: mxlen.size);
final shifted =
memRead.data >> (byteOff * Const(8, width: mxlen.size));
final raw = mux(
unaligned & _misalignedLoadAllowed,
memRead.data,
shifted,
).slice(mop.size.bits - 1, 0);
final raw =
(exactMemoryReads
? memRead.data
: mux(
unaligned & _misalignedLoadAllowed,
memRead.data,
shifted,
))
.slice(mop.size.bits - 1, 0);

steps.add(
CaseItem(Const(i, width: maxLen.bitLength), [
If(
unaligned & ~_misalignedLoadAllowed,
then: doTrap(Trap.misalignedLoad, addr, '_${op.mnemonic}'),
orElse: [
if (enableMisalignedLoads) ...[
misalignedLoad < unaligned,
if (enableMisalignedLoads) misalignedLoad < unaligned,
if (enableMisalignedLoads || exactMemoryReads)
loadSize <
Const(mop.size.bytes.bitLength - 1, width: 3),
],
memRead.en < 1,
memRead.addr <
mux(
unaligned & _misalignedLoadAllowed,
addr,
alignedAddr,
),
(exactMemoryReads
? addr
: mux(
unaligned & _misalignedLoadAllowed,
addr,
alignedAddr,
)),
mopStep < mopStep + 1,
],
),
Expand Down Expand Up @@ -5806,6 +5843,9 @@ class StaticExecutionUnit extends ExecutionUnit {
unaligned,
then: doTrap(Trap.misalignedStore, addr, '_${op.mnemonic}'),
orElse: [
if (exactMemoryReads)
loadSize <
Const(mop.size.bytes.bitLength - 1, width: 3),
memRead.en < 1,
memRead.addr < addr,
// cas needs rd's VALUE as the compare operand: read it
Expand Down Expand Up @@ -5925,6 +5965,9 @@ class StaticExecutionUnit extends ExecutionUnit {
unaligned,
then: doTrap(Trap.misalignedLoad, addr, '_${op.mnemonic}'),
orElse: [
if (exactMemoryReads)
loadSize <
Const(mop.size.bytes.bitLength - 1, width: 3),
memRead.en < 1,
memRead.addr < addr,
mopStep < mopStep + 1,
Expand Down
Loading
Loading