Page MenuHomeFreeBSD

RISC-V: Add macros for reading performance counter CSRs
ClosedPublic

Authored by mhorne063_gmail.com on Nov 11 2018, 10:21 PM.
Tags
None
Referenced Files
Unknown Object (File)
Sun, Mar 31, 10:44 AM
Unknown Object (File)
Jan 26 2024, 12:03 AM
Unknown Object (File)
Jan 22 2024, 11:46 PM
Unknown Object (File)
Jan 17 2024, 2:29 PM
Unknown Object (File)
Dec 20 2023, 9:56 PM
Unknown Object (File)
Dec 20 2023, 12:28 AM
Unknown Object (File)
Dec 19 2023, 3:56 AM
Unknown Object (File)
Dec 13 2023, 12:05 AM
Subscribers

Details

Summary

The RISC-V spec defines several performance counter CSRs such as: cycle,
time, instret, hpmcounter(3...31). They are defined to be 64-bits wide
on all RISC-V architectures. On RV64 and RV128 they can be read from a
single CSR. On RV32, additional CSRs (given the suffix "h") are present
which contain the upper 32 bits of these counters, and must be read as
well. (See section 2.8 in the User ISA Spec for full details)

This change adds macros for reading these values safely on any RISC-V
ISA length. Obviously we aren't supporting anything other than RV64
at the moment, but this ensures we won't need to change how we read
these values if we ever do.

Diff Detail

Repository
rS FreeBSD src repository - subversion
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

Hi @markj @br, hope I can bother you to review a couple RISC-V patches. I know there's not a heavy amount of development here but you two appeared to be the most active committers.

I will be looking for further improvements that I can submit, so I appreciate your help and cooperation.

sys/riscv/include/riscvreg.h
234 ↗(On Diff #50292)
  1. Why do you spin here ?
  2. Why do you use t0 in a macro ? (what if t0 is used already outside macro ?)
mhorne063_gmail.com added inline comments.
sys/riscv/include/riscvreg.h
234 ↗(On Diff #50292)

The spin is to ensure that the whole value is read properly, as the thread could be preempted between CSR reads.

t0 is listed as a clobbered register, so the compiler is aware of its use.

This revision is now accepted and ready to land.Nov 12 2018, 7:10 PM
sys/riscv/include/riscvreg.h
242 ↗(On Diff #50292)

Out of an abundance of caution, it might be best to add an outer wrapper layer of parentheses around the value for this macro, that is:

((uint64_t)csr_read(csr))
mhorne063_gmail.com added inline comments.
sys/riscv/include/riscvreg.h
242 ↗(On Diff #50292)

Agreed, it can't hurt to have it. I can update the diff in a few hours.

This revision now requires review to proceed.Nov 13 2018, 3:39 AM
This revision was not accepted when it landed; it landed in state Needs Review.Nov 13 2018, 6:12 PM
This revision was automatically updated to reflect the committed changes.