Skip to content

Commit ff5b83b

Browse files
committed
Catch more fetch errors
1 parent e67c0bf commit ff5b83b

4 files changed

Lines changed: 30 additions & 1 deletion

File tree

src/main/scala/magicore/control/Machine.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ object FullMachineException {
159159
}
160160

161161
object MachineExceptionCode extends SpinalEnum(binarySequential) {
162-
val BRANCH_MISS, INSN_CACHE_MISS, DECODE_ERROR, DIVIDE_ERROR, SERIALIZE,
162+
val BRANCH_MISS, INSN_CACHE_MISS, INSN_MEMORY_ERROR, DECODE_ERROR, DIVIDE_ERROR, SERIALIZE,
163163
MEMORY_ERROR, INSN_CACHE_FLUSH, EXCEPTION_RETURN, INSN_ALIGNMENT_ERROR,
164164
LOAD_ALIGNMENT_ERROR, STORE_ALIGNMENT_ERROR, EXT_INTERRUPT, ENV_CALL, WFI,
165165
RETRY = newElement()

src/main/scala/magicore/frontend/FetchUnit.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ case class FetchPacket(hasInsn: Boolean = true)
4040
private val fspec = Machine.get[FrontendSpec]
4141
def parentObjects = Seq()
4242

43+
val cacheError = Bool()
44+
4345
val cacheMiss = Bool()
4446
val cacheMissOffset =
4547
if (fspec.compressed) UInt(log2Up(fspec.addrStep) bits) else null
@@ -231,6 +233,8 @@ case class FetchUnit() extends Area {
231233

232234
private val data = FetchPacket()
233235

236+
data.cacheError := icache.io.cpu.fetch.error
237+
234238
// `cacheMiss` can be `X` on the first cycle.
235239
// XXX: Review this.
236240
data.cacheMiss := icache.io.cpu.fetch.cacheMiss || icache.io.cpu.prefetch.haltIt
@@ -252,6 +256,9 @@ case class FetchUnit() extends Area {
252256
icache.io.cpu.fetch.isStuck := dataStream.isStall
253257
icache.io.cpu.fetch.isRemoved := False // ???
254258
icache.io.cpu.fetch.mmuRsp.physicalAddress := pcFetchStage.pc & fspec.addrMask
259+
icache.io.cpu.fetch.mmuRsp.exception := False
260+
icache.io.cpu.fetch.mmuRsp.allowExecute := True
261+
icache.io.cpu.fetch.mmuRsp.isPaging := False
255262

256263
val decompressor = Machine.tryGet[FetchDecompressor]
257264
if (decompressor.isDefined) {

src/main/scala/magicore/isa/riscv/RiscvDecoder.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,26 @@ case class RiscvDecoder(
333333
).asUInt =/= 0 || inputStream.payload.unsuppressRd)
334334
outPatched.assignUnassignedByName(out)
335335

336+
when(out.fetch.cacheError) {
337+
outPatched.fuTag := earlyExceptionPort
338+
outPatched.earlyExc.code := MachineExceptionCode.INSN_MEMORY_ERROR
339+
wantStall := True
340+
}
341+
336342
when(out.fetch.cacheMiss) {
337343
outPatched.fuTag := earlyExceptionPort
338344
outPatched.earlyExc.code := MachineExceptionCode.INSN_CACHE_MISS
339345
wantStall := True
340346
}
341347

348+
val pcMisaligned =
349+
if (compressed) out.fetch.pc(0) else out.fetch.pc(1 downto 0).orR
350+
when(pcMisaligned) {
351+
outPatched.fuTag := earlyExceptionPort
352+
outPatched.earlyExc.code := MachineExceptionCode.INSN_ALIGNMENT_ERROR
353+
wantStall := True
354+
}
355+
342356
// Suppress interrupts during micro-op sequences.
343357
when(intrSvc.trigger && !inputStream.payload.microOp) {
344358
outPatched.fuTag := earlyExceptionPort

src/main/scala/magicore/isa/riscv/RvCsr.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,14 @@ class RvCsr(staticTagData: => Data) extends FunctionUnit {
409409
// Load access fault
410410
restartIntoException(5, exc.memoryError_accessAddr.asUInt)
411411
}
412+
is(MachineExceptionCode.INSN_ALIGNMENT_ERROR) {
413+
// Instruction address misaligned
414+
restartIntoException(0, 0)
415+
}
416+
is(MachineExceptionCode.INSN_MEMORY_ERROR) {
417+
// Instruction access fault
418+
restartIntoException(1, 0)
419+
}
412420
is(MachineExceptionCode.LOAD_ALIGNMENT_ERROR) {
413421
// Load address misaligned
414422
restartIntoException(4, exc.memoryError_accessAddr.asUInt)

0 commit comments

Comments
 (0)