[dv] Backdoor loading of scrambled memories from a Verilator simulation
I'm in the process of adding scrambling to the OTBN memories and started looking at how to support backdoor memory loading. We already have support for backdoor loading with scrambling via mem_blkdir_if (https://github.com/lowRISC/opentitan/tree/master/hw/dv/sv/mem_bkdr_if) however it's been written to work with the DV UVM environment and can't be directly used by Verilator simulations.
The OTBN UVM testbench could use mem_blkdir_if though it's currently using memutil (https://github.com/lowRISC/opentitan/tree/master/hw/dv/verilator) to do backdoor loading which supports ELF loading and provides some consistency between the verilator and UVM testbenches as they can both use the same code.
We could just not scramble memories in the OTBN verilator or UVM testbench or switch the UVM one to use mem_blkdir_if but then we need a new elf loading solution. Plus our top-level earlgrey verilator will also need a way to load scrambled memories. So I think we need a Verilator solution for backdoor loading of scrambled memories.
We do have a C implementation of the PRINCE primitive, but we need a few extra bits on top of it, specifically the extra diffusion layers that do some substitutions and permutations. This is all available in sram_scrambler_pkg which doesn't include extra DV/UVM environment stuff so could be built in a Verilator sim using DPI to export the top level encrypt/decrypt methods though sadly Verilator doesn't build it, there's some internal issues around the use of dynamic arrays.
What's the best way forward?
- Submit a Verilator bug report to get issues fixed
- I will do this whatever, it's more a question of whether we'll get a fix that works for our purposes in good time
- Have yet to look into it in detail, these are certainly the kinds of features Verilator doesn't claim to have great support for so a fix could take a while or they'll be unwilling to fix in the short term if it's complex and eventually getting dealt with by the extra support Verilator is getting to allow use of UVM
- Rewrite
sram_scrambler_pkgto build with Verilator (probably dropping all use of dynamic arrays) - Implement functionality from
sram_scrambler_pkgin C- Could drop
sram_scrambler_pkgin SV form altogether and have bothmem_blkdir_ifand memutil use it
- Could drop
- Drop backdoor loading support from Verilator sims
- Not a big deal for the OTBN testbench, more problematic for the top-level earlgrey simulation
I might tend towards an implementation of sram_scrambler_pkg in C that everything uses. Otherwise it's a bit messy from the verilator side, DPI call to SV which in turn DPI calls into another C library.
I don't think that the required Verilator support will arrive before tapeout, so I think we can discount option (1). Option (4) seems lame: surely we can do better! Which leaves options (2) and (3), both of which seem fine to me.
though sadly Verilator doesn't build it, there's some internal issues around the use of dynamic arrays.
Do we know which features exactly are missing? https://github.com/verilator/verilator/issues/2314 maybe?
Do we know which features exactly are missing? verilator/verilator#2314 maybe?
That could be it, most of the dynamic arrays are a single dimension with a single bit logic as the base type but I think there's a couple that are more complex, could be the presence of those causing issues.
Agree - both 2 and 3 sound viable. I'm not sure if the dynamic arrays are really needed here?
@udinator FYI
Both solutions 2 and 3 sound ok to me as well - though I'd probably slightly prefer option 2.
I had to write the sram_scrambler_pkg using dynamic arrays to get around the limitation of not wanting to introduce more memory-size-related parameters in the interface.
I think @sriyerg is working on some enhancements to the bkdr_if also to parameterize it on the memory width and depth,
so once that change goes in it would be more straightforward to rewrite the sram scrambling logic using non-dynamic arrays.
However, @GregAC if the functionality is needed urgently, rewriting the scrambling in C and adding a DPI package to the mem_bkdr_if subdirectory would be a good solution.
+1 for having sram_scrambler_pkg written in C. Our tech debt is already high with duplication of efforts across DV and Verilator.
EDIT: or SV, whichever is easy as long as we can share the implementation.
I've introduced a C++ scrambling model here: https://github.com/lowRISC/opentitan/pull/7020 the plan would be to do a follow-up PR That would replace sram_scrambler_pkg with something using a DPI interface to that model.
Hi @GregAC marking as M2 for further triage.
This would be a useful tidy-up (we don't really want lots of different scrambling implementations everywhere). But certainly not a priority for M2. I will leave this open but I think this is a future release issue.
It's entirely possible the SV code that wouldn't build under verilator will now build but this issue remains low priority for a future release.
I am not sure we need to do this for PROD, hence not flagging as a candidate. Please relabel if this is needed for PROD.