Implement callbacks for state-changing events#494
Conversation
Timmmm
left a comment
There was a problem hiding this comment.
This looks along the lines I was thinking, though I think we will still need to have RVFI support in the C emulator, so you still need the flags. It will just need to be implemented via the callbacks.
I probably wouldn't use __attribute__((weak)). If anyone is modifying the callbacks they'll have their own fork of the emulator anyway and can just delete the default ones. That's what we (Codasip) will do.
I'll get you some code to read lbits.
|
It's something like this in C++ (adapted from our code, so this exact code isn't tested). In crappy C you will have to deal with |
model/riscv_insts_vext_mem.sail
Outdated
| if i == 0 then { ext_handle_data_check_error(e); return RETIRE_FAIL } | ||
| else { | ||
| vl = to_bits(sizeof(xlen), i); | ||
| if rv_enable_callbacks then csr_update_callback("vl", vl); |
There was a problem hiding this comment.
Having to add these manually is rather error prone and makes the code less readable. Can we do some fancy sail overloading tricks to automatically call these functions for CSR writes (e.g. something like CSR(vl) = ... ?
There was a problem hiding this comment.
Various CSRs have a set_foo() function. We could put it in there. Though that doesn't significantly reduce the risk because people can always forget to use the setter.
I ran into a similar issue recently - trying to memoize/cache a value. Difficult to guarantee that all the code will use setter that updates the cache without some support for private.
IMO there are few enough of these that we can probably ignore this issue for the moment, though I agree it's not ideal.
There was a problem hiding this comment.
Perhaps it would be better to get Sail to generate the callbacks automatically?
There was a problem hiding this comment.
Yeah we discussed this before. Might be nice, but I'm not 100% sure it would do exactly what you want, e.g. you probably want distinct callbacks for writes to sstatus and mstatus, but under the hood they write to the same register. Also there are very few places this callback is needed so the manual burden is not that high, and I suspect Alasdair has enough on his plate already!
There was a problem hiding this comment.
I added the ext_notification_write_CSR (and ext_notification_read_CSR) function. It helps to simplify writing to a CSR with a call to a callback.
Actually reading this back, I wonder if |
|
I would like to point out that the callback functions are annotated as |
I would say if someone edits the code and changes the callback to be impure then it's up to them to also change the Sail code to mark them as impure. |
arichardson
left a comment
There was a problem hiding this comment.
Could you keep the RVFI-DII code around? Alternatively, a good demonstrator of the callbacks would be to implement the RVFI-DII socket interface using those callbacks?
|
Yeah to be clear I don't think these callbacks should be user defined. We should have hard-coded callbacks that support RVFI, logging, etc. |
10ae49b to
6e0f187
Compare
|
@arichardson, @Timmmm, what do you think of the current version? |
Timmmm
left a comment
There was a problem hiding this comment.
Yeah this is pretty much exactly what I was thinking. I put some minor comments but I it looks like the right direction to me.
arichardson
left a comment
There was a problem hiding this comment.
Overall I think this approach looks good, but I'd push all the rv_enable_callbacks into the callee to simplify the code. Since the variable is not a compile-time constant it shouldn't matter whether it's checked in the C code or sail code. It might be easier to keep this as an internal flag to the C/Ocaml emulators.
09acccc to
be696b8
Compare
98f3973 to
7dcbd56
Compare
arichardson
left a comment
There was a problem hiding this comment.
Not reviewed this in detail, but overall the new approach looks sensible to me.
779fbf7 to
232e5bd
Compare
|
@arichardson, @Timmmm, what do you think of the current version? |
|
Hi, sorry for not getting around to this. Just wanted to say it's still on my todo list and I will get to it! |
Timmmm
left a comment
There was a problem hiding this comment.
Finally got around to this again - sorry for the delay!
I think it's pretty close - the main thing is merging riscv_rvfi_callbacks.c into riscv_default_callbacks.c.
You could also rename it to just riscv_callbacks.c.
pmundkur
left a comment
There was a problem hiding this comment.
A nice improvement, moving most of RVFI handling out of the main Sail model.
756baa5 to
cd62eef
Compare
48ede59 to
b70da8f
Compare
pmundkur
left a comment
There was a problem hiding this comment.
Other than the c-preserve and pc_write_callback, looks great!
b70da8f to
8022eea
Compare
8022eea to
9812b22
Compare
Timmmm
left a comment
There was a problem hiding this comment.
LGTM. Just a couple of minor things and I think we should add some comments to either riscv_callbacks.h or riscv_callbacks.sail to say exactly what all the callbacks mean, because there are some subtleties. E.g. when exactly is trap_callback() called? Are addresses physical or virtual? How is mstatush etc. handled?
There are probably some other minor things we can improve but I think this is already a huge improvement so lets get it in!
9812b22 to
294f469
Compare
Timmmm
left a comment
There was a problem hiding this comment.
LGTM. We can add the comments later. I imagine the exact API may change slightly when we try to use it in anger anyway.
|
Oh you missed a |
1. Added callback functions for XRegs, FRegs, VRegs, PC, CSR and memory writes, and CSR and memory reads. 2. Added RVFI and standard trace logging implementation of callbacks. Co-authored-by: Tim Hutt <timothy.hutt@codasip.com>
294f469 to
ef2b1d1
Compare
|
Oops. Fixed. I added some comments to |
|
Nice work everyone! |
Add a callback API for register, PC and CSR updates, memory writes and traps. This is used for logging and RVFI instead of embedding that code in the model itself.