Skip to content

Commit 4bce85d

Browse files
aykevldeadprogram
authored andcommitted
riscv: define CSR constants and use them where possible
1 parent 8a73502 commit 4bce85d

File tree

4 files changed

+77
-19
lines changed

4 files changed

+77
-19
lines changed

src/device/riscv/csr.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,61 @@ const (
275275
DPC CSR = 0x7B1 // Debug PC.
276276
DSCRATCH CSR = 0x7B2 // Debug scratch register.
277277
)
278+
279+
// Bitfields for the CSR registers above.
280+
const (
281+
// MSTATUS (common bits between RV32 and RV64)
282+
MSTATUS_SIE = 1 << 1
283+
MSTATUS_MIE = 1 << 3
284+
MSTATUS_SPIE = 1 << 5
285+
MSTATUS_UBE = 1 << 6
286+
MSTATUS_MPIE = 1 << 7
287+
MSTATUS_SPP = 1 << 8
288+
MSTATUS_MPRV = 1 << 17
289+
MSTATUS_SUM = 1 << 18
290+
MSTATUS_MXR = 1 << 19
291+
MSTATUS_TVM = 1 << 20
292+
MSTATUS_TW = 1 << 21
293+
MSTATUS_TSR = 1 << 22
294+
295+
MIE_SSIE = 1 << 1
296+
MIE_MSIE = 1 << 3
297+
MIE_STIE = 1 << 5
298+
MIE_MTIE = 1 << 7
299+
MIE_SEIE = 1 << 9
300+
MIE_MEIE = 1 << 11
301+
302+
MIP_SSIP = 1 << 1
303+
MIP_MSIP = 1 << 3
304+
MIP_STIP = 1 << 5
305+
MIP_MTIP = 1 << 7
306+
MIP_SEIP = 1 << 9
307+
MIP_MEIP = 1 << 11
308+
)
309+
310+
// Interrupt constants
311+
const (
312+
// MCAUSE values with the topmost bit (interrupt bit) set.
313+
SupervisorSoftwareInterrupt = 1
314+
MachineSoftwareInterrupt = 3
315+
SupervisorTimerInterrupt = 5
316+
MachineTimerInterrupt = 7
317+
SupervisorExternalInterrupt = 9
318+
MachineExternalInterrupt = 11
319+
320+
// MCAUSE values with the topmost bit (interrupt bit) clear.
321+
InstructionAddressMisaligned = 0
322+
InstructionAccessFault = 1
323+
IllegalInstruction = 2
324+
Breakpoint = 3
325+
LoadAddressMisaligned = 4
326+
LoadAccessFault = 5
327+
StoreOrAMOAddressMisaligned = 6
328+
StoreOrAMOAccessFault = 7
329+
EnvironmentCallFromUMode = 8
330+
EnvironmentCallFromSMode = 9
331+
EnvironmentCallFromMMode = 11
332+
InstructionPageFault = 12
333+
LoadPageFault = 13
334+
StoreOrAMOPageFault = 15
335+
)

src/runtime/interrupt/interrupt_esp32c3.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,13 @@ func handleInterrupt() {
189189
}
190190

191191
// enable CPU interrupts
192-
riscv.MSTATUS.SetBits(1 << 3)
192+
riscv.MSTATUS.SetBits(riscv.MSTATUS_MIE)
193193

194194
// Call registered interrupt handler(s)
195195
callHandler(int(interruptNumber))
196196

197197
// disable CPU interrupts
198-
riscv.MSTATUS.ClearBits(1 << 3)
198+
riscv.MSTATUS.ClearBits(riscv.MSTATUS_MIE)
199199

200200
// restore interrupt threshold to enable interrupt again
201201
reg.Set(thresholdSave)
@@ -207,7 +207,7 @@ func handleInterrupt() {
207207

208208
// do not enable CPU interrupts now
209209
// the 'MRET' in src/device/riscv/handleinterrupt.S will copies the state of MPIE back into MIE, and subsequently clears MPIE.
210-
// riscv.MSTATUS.SetBits(0x8)
210+
// riscv.MSTATUS.SetBits(riscv.MSTATUS_MIE)
211211
} else {
212212
// Topmost bit is clear, so it is an exception of some sort.
213213
// We could implement support for unsupported instructions here (such as
@@ -221,13 +221,13 @@ func handleException(mcause uintptr) {
221221
println("*** Exception: code:", uint32(mcause&0x1f))
222222
println("*** Exception: mcause:", mcause)
223223
switch uint32(mcause & 0x1f) {
224-
case 1:
224+
case riscv.InstructionAccessFault:
225225
println("*** virtual address:", riscv.MTVAL.Get())
226-
case 2:
226+
case riscv.IllegalInstruction:
227227
println("*** opcode:", riscv.MTVAL.Get())
228-
case 5:
228+
case riscv.LoadAccessFault:
229229
println("*** read address:", riscv.MTVAL.Get())
230-
case 7:
230+
case riscv.StoreOrAMOAccessFault:
231231
println("*** write address:", riscv.MTVAL.Get())
232232
}
233233
for {

src/runtime/runtime_fe310.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ func main() {
3838

3939
// Reset the MIE register and enable external interrupts.
4040
// It must be reset here because it not zeroed at startup.
41-
riscv.MIE.Set(1 << 11) // bit 11 is for machine external interrupts
41+
riscv.MIE.Set(riscv.MIE_MEIE)
4242

4343
// Enable global interrupts now that they've been set up.
44-
riscv.MSTATUS.SetBits(1 << 3) // MIE
44+
riscv.MSTATUS.SetBits(riscv.MSTATUS_MIE) // MIE: machine external interrupts
4545

4646
preinit()
4747
initPeripherals()
@@ -59,13 +59,13 @@ func handleInterrupt() {
5959
if cause&(1<<31) != 0 {
6060
// Topmost bit is set, which means that it is an interrupt.
6161
switch code {
62-
case 7: // Machine timer interrupt
62+
case riscv.MachineTimerInterrupt:
6363
// Signal timeout.
6464
timerWakeup.Set(1)
6565
// Disable the timer, to avoid triggering the interrupt right after
6666
// this interrupt returns.
67-
riscv.MIE.ClearBits(1 << 7) // MTIE bit
68-
case 11: // Machine external interrupt
67+
riscv.MIE.ClearBits(riscv.MIE_MTIE)
68+
case riscv.MachineExternalInterrupt:
6969
// Claim this interrupt.
7070
id := sifive.PLIC.CLAIM.Get()
7171
// Call the interrupt handler, if any is registered for this ID.
@@ -143,7 +143,7 @@ func sleepTicks(d timeUnit) {
143143
target := uint64(ticks() + d)
144144
sifive.CLINT.MTIMECMPH.Set(uint32(target >> 32))
145145
sifive.CLINT.MTIMECMP.Set(uint32(target))
146-
riscv.MIE.SetBits(1 << 7) // MTIE
146+
riscv.MIE.SetBits(riscv.MIE_MTIE)
147147
for {
148148
if timerWakeup.Get() != 0 {
149149
timerWakeup.Set(0)

src/runtime/runtime_k210.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ func main() {
4444

4545
// Reset the MIE register and enable external interrupts.
4646
// It must be reset here because it not zeroed at startup.
47-
riscv.MIE.Set(1 << 11) // bit 11 is for machine external interrupts
47+
riscv.MIE.Set(riscv.MIE_MEIE) // MEIE is for machine external interrupts
4848

4949
// Enable global interrupts now that they've been set up.
50-
riscv.MSTATUS.SetBits(1 << 3) // MIE
50+
riscv.MSTATUS.SetBits(riscv.MSTATUS_MIE)
5151

5252
preinit()
5353
initPeripherals()
@@ -77,13 +77,13 @@ func handleInterrupt() {
7777
if cause&(1<<63) != 0 {
7878
// Topmost bit is set, which means that it is an interrupt.
7979
switch code {
80-
case 7: // Machine timer interrupt
80+
case riscv.MachineTimerInterrupt:
8181
// Signal timeout.
8282
timerWakeup.Set(1)
8383
// Disable the timer, to avoid triggering the interrupt right after
8484
// this interrupt returns.
85-
riscv.MIE.ClearBits(1 << 7) // MTIE bit
86-
case 11: // Machine external interrupt
85+
riscv.MIE.ClearBits(riscv.MIE_MTIE)
86+
case riscv.MachineExternalInterrupt:
8787
hartId := riscv.MHARTID.Get()
8888

8989
// Claim this interrupt.
@@ -149,7 +149,7 @@ func ticks() timeUnit {
149149
func sleepTicks(d timeUnit) {
150150
target := uint64(ticks() + d)
151151
kendryte.CLINT.MTIMECMP[0].Set(target)
152-
riscv.MIE.SetBits(1 << 7) // MTIE
152+
riscv.MIE.SetBits(riscv.MIE_MTIE)
153153
for {
154154
if timerWakeup.Get() != 0 {
155155
timerWakeup.Set(0)

0 commit comments

Comments
 (0)