sifive / freedom-metal

Bare Metal Compatibility Library for the Freedom Platform
Other
152 stars 47 forks source link

Is `_metal_trap` interrupt-safe? #434

Closed RoccoMatano closed 4 months ago

RoccoMatano commented 2 years ago

Looking at the code of _metal_trap, I think it is not interrupt-safe. That is because the code is setting up mepc and mcause first, before disabling interrupts by writing to mstatus. If an interrupt occurs after setting up mepc and mcause but before writing to mcause, then the values in mepc and mcause will be overwritten and I think they will not be restored by the interrupt handling code.

By the way: I don't understand the calculation of the value for mepc. I guess addi t0, ra, -1 is used to rewind mepc to the instruction in front of the one where execution should continue. But how can applying -1 lead to the correct value, when all RISC-V instructions have to start at an even address and compressed and uncompressed instructions can be mixed?

RoccoMatano commented 4 months ago

nobody cares...