opentitan icon indicating copy to clipboard operation
opentitan copied to clipboard

[dv] Backdoor loading of scrambled memories from a Verilator simulation

Open GregAC opened this issue 5 years ago • 12 comments

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_pkg to build with Verilator (probably dropping all use of dynamic arrays)
  • Implement functionality from sram_scrambler_pkg in C
    • Could drop sram_scrambler_pkg in SV form altogether and have both mem_blkdir_if and memutil use it
  • 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.

GregAC avatar May 21 '21 11:05 GregAC

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.

rswarbrick avatar May 21 '21 11:05 rswarbrick

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?

imphil avatar May 21 '21 11:05 imphil

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.

GregAC avatar May 21 '21 11:05 GregAC

Agree - both 2 and 3 sound viable. I'm not sure if the dynamic arrays are really needed here?

tomeroberts avatar May 21 '21 11:05 tomeroberts

@udinator FYI

sriyerg avatar May 21 '21 19:05 sriyerg

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.

udinator avatar May 21 '21 19:05 udinator

+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.

sriyerg avatar May 21 '21 23:05 sriyerg

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.

GregAC avatar Jun 17 '21 10:06 GregAC

Hi @GregAC marking as M2 for further triage.

moidx avatar Aug 19 '22 15:08 moidx

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.

GregAC avatar Aug 26 '22 08:08 GregAC

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.

GregAC avatar Feb 24 '23 16:02 GregAC

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.

msfschaffner avatar Oct 06 '23 22:10 msfschaffner