Skip to content
Permalink
Browse files
riscv: enable TIME CSR in U mode
After ad5d112 we let the user mode to use rdtime directly for time
access. This works if the hardware does not implement the TIME CSR
and traps to the firmware. The spec however does allow a hardware
implementation to redirect the CSR access to the memory-mapped MTIME
CSR, and if this is performed it will check {M,S}COUNTEREN CSR to
see if it is allowed.

To prevent unhandled illegal instruction fault on these platforms,
we can request these CSRs to be enabled in U-mode. For platforms
that does not support the TIME CSR to MTIME CSR conversion, they
will hardwire corresponding bit in COUNTEREN to zero and this would
be a no-op.

Signed-off-by: Gary Guo <gary@garyguo.net>
  • Loading branch information
nbdd0121 committed Dec 15, 2021
1 parent 8bb7eca commit 4481ee1b02354fde9960268a3fd8d638124aba1a
Showing 1 changed file with 6 additions and 0 deletions.
@@ -228,13 +228,19 @@ ENTRY(_start_kernel)
.align 2
pmp_done:

/* Allow user-mode to access time CSR */
csrw, CSR_MCOUNTEREN, 2

/*
* The hartid in a0 is expected later on, and we have no firmware
* to hand it to us.
*/
csrr a0, CSR_MHARTID
#endif /* CONFIG_RISCV_M_MODE */

/* Allow user-mode to access time CSR */
csrw CSR_SCOUNTEREN, 2

/* Load the global pointer */
.option push
.option norelax

0 comments on commit 4481ee1

Please sign in to comment.