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

Implicit CSR writes are not logged #450

Open
allenjbaum opened this issue Apr 15, 2024 · 18 comments
Open

Implicit CSR writes are not logged #450

allenjbaum opened this issue Apr 15, 2024 · 18 comments

Comments

@allenjbaum
Copy link
Collaborator

Some implicit CSR updates (e.g. updated Mstatus when trapping into Mmode, or MTVAL, etc) are logged,
but most or not.

I would propose that no further CSRs be allowed to be merged unless implicit accesses
(if there are any - this occurs primarily for status fields, rather than control fields)
are logged (one line/CSR, prexise format TBD, but should be uniform)).

Eventually, this has to be extended already existing status CSR fields (as opposed to control and readonly fields)

@Timmmm
Copy link
Collaborator

Timmmm commented May 7, 2024

I think this is a duplicate of #184

@allenjbaum
Copy link
Collaborator Author

Yes, but using much stronger language. This is required for us to measure coverage, and is a fairly strong argument why we can't use the Sail model compared to other existing solutions.

@Alasdair
Copy link
Collaborator

Alasdair commented May 8, 2024

Do you have a description of the desired format you require?

@allenjbaum
Copy link
Collaborator Author

The format would be the same as what is currently logged : CSR {CSR_Name} <- {Hex-Value}

@Alasdair
Copy link
Collaborator

Alasdair commented May 8, 2024

Maybe the way to go would be to add a CSR attribute to the appropriate registers, then have Sail insert the logging statements. Would perhaps be less error prone than having to manually write them in the source, and shouldn't be too hard to implement.

@allenjbaum
Copy link
Collaborator Author

allenjbaum commented May 8, 2024 via email

@Alasdair
Copy link
Collaborator

Alasdair commented May 8, 2024

I mean if we know what registers are CSRs then rather than writing something like print("CSR " ^ reg_name(...) ^ " <- " ^ ...) in the spec, we can just automatically insert the correct logging statements. That way we never miss any.

@allenjbaum
Copy link
Collaborator Author

allenjbaum commented May 8, 2024 via email

@jrtc27
Copy link
Collaborator

jrtc27 commented May 8, 2024

That doesn't help you when the model, as it should for clarity, accesses the register directly by its language-level register, which has zero ties to any notion of CSR address space. They are indistinguishable from general-purpose registers in that regard.

@allenjbaum
Copy link
Collaborator Author

OK, I think I understand the context now.
So, how would you add the CSR attribute to (all, preferably) CSRs, and when you did,
how do you get Sail to automatically out[ut a logging string if any field of the CSR is updated?

@Timmmm
Copy link
Collaborator

Timmmm commented May 8, 2024

Maybe the way to go would be to add a CSR attribute to the appropriate registers, then have Sail insert the logging statements. Would perhaps be less error prone than having to manually write them in the source, and shouldn't be too hard to implement.

I don't think this would work unfortunately. E.g. we want to log writes to sstatus but that register doesn't explicitly exist in the model. Our current solution is this:

    writeCSR(csr, new_val);
    // Immediately call this after writing the CSR.
    track_writeCSR(csr);
// Add it to a list of CSR writes - though in retrospect I like the callback solution better.
function track_writeCSR(csr : csreg) -> unit = {
  track_csr_writes = struct {
    index = csr,
    value = readCSR(csr), // Note, that this is a bit janky since in theory CSR reads can have side effects. In practice they don't though.
  } :: track_csr_writes;
}

And then we add a handful of extra calls:

riscv_fdext_regs.sail:  track_writeCSR(csr_name_map("mstatus"));  // dirty_fd_context
riscv_fdext_regs.sail:  track_writeCSR(csr_name_map("fcsr")); // accrue_fflags
riscv_sdext_control.sail:  track_writeCSR(csr_name_map("mstatus")); // leave_debug_mode
riscv_sys_control.sail:      track_writeCSR(csr_name_map("mstatus")); // mret
riscv_sys_control.sail:      track_writeCSR(csr_name_map("mstatus")); // sret
riscv_sys_control.sail:      track_writeCSR(csr_name_map("mstatus")); // uret

As far as I know that covers all the implicit CSR writes (maybe not in Vector). Given there are so few I think it's fine to just do it manually. We should just do it as a callback, as suggested in #184.

@allenjbaum
Copy link
Collaborator Author

allenjbaum commented May 8, 2024 via email

@Timmmm
Copy link
Collaborator

Timmmm commented May 8, 2024

We're using this for model checking - not coverage - so it needs to exactly match what our DUT says. When you write to sstatus the instrumentation of our DUT reports it as a write to sstatus, not mstatus. Theoretically that could be changed but I think it would be much worse for debugging if we did, and I can't see any reason to given how easy it is to solve this issue manually.

I thought that side effects on reads to CSRs were forbidden (explicitly) in the spec.

It's not forbidden, it just happens to be the case that all existing standard CSRs don't have side effects on read (because that would be mad). But custom CSRs are allowed to:

Standard CSRs do not have any side effects on reads. Standard CSRs may have side effects on writes. Custom extensions might add CSRs for which accesses have side effects on either reads or writes.

So in practice it's not an issue for the model since we don't have any crazy CSRs like that. There's a comment in the existing code actually for one of these side-effect cases:

    let csr_val = readCSR(csr); /* could have side-effects, so technically shouldn't perform for CSRW[I] with rd == 0 */

But yeah it currently doesn't matter (and is unlikely to ever matter really).

@rsnikhil
Copy link
Collaborator

rsnikhil commented May 8, 2024

. I thought that side effects on reads to CSRs were forbidden (explicitly) in the spec.

In addition to the text cited by @Timmmm there is also this text, in the first para of 9.1 (Zicsr):

If rd=x0, then the instruction shall not read the CSR and shall
not cause any of the side effects that might occur on a CSR read.

@jrtc27
Copy link
Collaborator

jrtc27 commented May 8, 2024

Your model might want sstatus but someone else’s might want mstatus. Canonicalising makes things simpler to compare, and avoids confusion around whether state is aliasing or separate. I would argue mstatus should be reported since that is the actual underlying architectural state being changed. See also fflags+frm vs fcsr.

@Timmmm
Copy link
Collaborator

Timmmm commented May 9, 2024

Hmm I suppose there are two similar but subtly different things you might want to know:

  • What are the changes to the underlying architectural state? (mstatus changed)
  • What action did the instruction perform? (wrote to sstatus)

There's actually an even more annoying subtlety we ran into: what about implicit CSR updates that don't actually change the value? Especially with implicit updates to fflags, you have a few options for when to record an update:

  • Whenever it is written to (i.e. when any instruction that theoretically could change it is executed).
  • Whenever its actual value changes.
  • Whenever the flag result of an instruction is non-zero, even if ORing that into fflags wouldn't change its value.

That was a right pain to deal with.

Maybe the main issue with canonicalising is that it's going to be a lot more work than the 5 line change we have.

@allenjbaum
Copy link
Collaborator Author

allenjbaum commented May 11, 2024 via email

@allenjbaum
Copy link
Collaborator Author

allenjbaum commented May 11, 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