Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

instret, cycle, time outside of Zicntr (and hpmpcounterN outside Zihpm) #1734

Open
dhower-qc opened this issue Nov 22, 2024 · 10 comments
Open

Comments

@dhower-qc
Copy link

It's unclear if instret, cycle, and time can exist without implementing Zicntr.

If they do not exist outside Zicntr, the priv spec needs to be clear on that when mentioning those CSRs (currently, Zicntr is not mentioned at all).

If they can exist outside of Zicntr, the unpriv spec needs to be clear that Zicntr does not define the CSRs (and only defines pseudoinstructions).

<sed g/Zicntr/Ziphm/> # same for hpm

A protracted debate among CSC members shows there is no consensus based on the spec text.

@gfavor
Copy link
Collaborator

gfavor commented Nov 22, 2024

Priv 1.13 defines the mtime, mcycle, minstret counters and associated machine-level CSRs, and says that the user-level time, cycle, and instret CSRs (assuming they are implemented) are read-only shadows of the machine-level CSRs. (And note that these two sets of CSRs ARE distinct CSRs with different access permissions, with the Priv spec specifying how they are related.)

Then the Unpriv (i.e. user-level) Zicntr extension defines the time, cycle, and instret CSRs. This extension would typically also be implemented (as is recommended in the spec), but if it isn't, then that is fine. That just means that the user-level time, cycle, and instret CSRs are not implemented (and obviously then inaccessible to user-level).

@allenjbaum
Copy link

allenjbaum commented Nov 22, 2024 via email

@dhower-qc
Copy link
Author

Priv 1.13 defines the mtime, mcycle, minstret counters and associated machine-level CSRs, and says that the user-level time, cycle, and instret CSRs (assuming they are implemented) are read-only shadows of the machine-level CSRs. (And note that these two sets of CSRs ARE distinct CSRs with different access permissions, with the Priv spec specifying how they are related.)

Yes, but what it doesn't say is whether or not time, cycle, and instret can exist (can be read without causing a trap) independent of Zicntr.

Then the Unpriv (i.e. user-level) Zicntr extension defines the time, cycle, and instret CSRs. This extension would typically also be implemented (as is recommended in the spec), but if it isn't, then that is fine. That just means that the user-level time, cycle, and instret CSRs are not implemented (and obviously then inaccessible to user-level).

Like Allen said, I'm not sure the Unpriv spec actually says that (thus the confusion). And even if it does, it's confusing because "implemented" isn't well-defined. time, in particular, might be defined/implemented by Zicntr but will still always cause an IllegalInstruction exception (how is that different than not implemented?).

@gfavor
Copy link
Collaborator

gfavor commented Nov 22, 2024 via email

@dhower-qc
Copy link
Author

If standard numbers, then one has implemented a nonconforming extension.

So, to rephrase: Zicntr defines the standard CSR numbers for time, cycle, and instret. Reading any of them from M-mode will always trap in a conforming implementation if Zicntr is not implemented.

If I have that right, I think a manual update to clarify that is needed.

If one implements Zicntr (i.e. implements all the defined arch
functionality), then always trapping CSR accesses would not be complying
with the spec (i.e. a nonconforming implementation).

I think there is a disconnect in how we are thinking about this. From user-mode's perspective, you can't (without side channels) detect trap-and-emulate so this is moot. But, from M-mode's perspective (needed for certification), it's possible that some, all, or none of these do trap, and that is detectable. time in particular may always trap since its shadow, mtime, is an MMIO read.

@ved-rivos
Copy link
Collaborator

On some simple platforms, cycle count might represent a valid implementation of RDTIME, in which case RDTIME and RDCYCLE may return the same result. Is it the case that they simply count at the same rate?, or can they be shadows of each other (e.g. writing one will change the value of the other)?

I would consider such an implementation to be wrong. Many use cases require resetting/context-switching cycles but software expects time to be invariant and monotonic. When multiple harts exists, software also expects the time observed by harts to be within one-tick of each other. An implementation that aliases cycles may lead to unexpected/undesired behaviors such as time being observed to go backwards. Time may not be invariant or synchronized on such implementations.

@gfavor
Copy link
Collaborator

gfavor commented Nov 22, 2024 via email

@allenjbaum
Copy link

allenjbaum commented Nov 22, 2024 via email

@kdockser
Copy link
Collaborator

Why is this discussion limited to Zicntr?
In theory nothing needs to exist, at least not in user mode. Everything in user mode, CSR accesses, memory accesses, and even the instruction execution itself can take an invisible trap to M-mode. Is the intent for M-mode certification to require that user mode is tested with the option of each instruction and register not "existing"?

@dhower-qc
Copy link
Author

dhower-qc commented Nov 22, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants