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

Add autonomous Docker for compilation/simulation #2

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM ubuntu:noble AS builder

# Install Make, g++, verilator, the RISC-V toolchain and the picolibc library
RUN apt-get update

RUN apt-get install -qy --no-install-recommends make g++ bsdmainutils verilator gcc-riscv64-unknown-elf picolibc-riscv64-unknown-elf


# NOTE: the random forces a Docker cache miss to force
# the execution of the script
ADD "https://www.random.org/cgi-bin/randbyte?nbytes=10&format=h" skipcache

# Now compile and simulate
WORKDIR /build
COPY ./ /build

RUN USE_PICOLIBC=1 make prof 2>&1 | tee -a sloth_build.log
RUN tar -czvf docker_build.tar.gz sloth_build.log core_pc.log firmware.pmap firmware.bin firmware.elf firmware.hex _prof _build drv slh --transform 's,^,docker_build/,'

# Copy the produced artifacts to the host
FROM scratch AS artifacts
COPY --from=builder /build/docker_build.tar.gz ./docker_build.tar.gz
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ CCONF_H ?= config.h
XCHAIN ?= riscv64-unknown-elf-
CFLAGS = -O2 -Wall -g -mabi=ilp32 -march=rv32imc
CFLAGS += -include $(CCONF_H) -DNDEBUG
LDFLAGS = -Wl,-Bstatic,-T,flow/riscv.ld,--no-relax
# If we use picolibc, adapt the flags.
# NOTE: the local ld script mostly provides flash and RAM layout
# (in our case flash starts at 0 and is in fact part of RAM)
ifeq ($(USE_PICOLIBC),1)
CFLAGS += -specs=picolibc.specs
LDFLAGS = -Wl,-Bstatic,-T,flow/picolibc.ld,--no-relax
else
LDFLAGS = -Wl,-Bstatic,-T,flow/riscv.ld,--no-relax
endif
KATNUM ?= 10
CFLAGS += -DKATNUM=$(KATNUM)

Expand Down Expand Up @@ -136,6 +144,7 @@ clean:
$(RM) -f $(FW).* $(CCONF_H) $(OBJS) $(VVP)
$(RM) -rf $(PROF) $(BUILD)
$(RM) -f *.jou *.log *.bit
$(MAKE) -f Makefile.docker clean
cd slh && $(MAKE) clean
cd flow/yosys-syn && $(MAKE) clean

19 changes: 19 additions & 0 deletions Makefile.docker
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
ifeq ($(DOCKER_FORCE_REBUILD),1)
NOCACHE=--no-cache
RECREATE=--force-recreate
else
NOCACHE=
RECREATE=
endif
ifeq ($(DOCKER_VERBOSE),1)
BUILDKIT_PROGRESS=plain
endif

DOCKER_PLATFORM=--platform linux/amd64

all:
DOCKER_BUILDKIT=1 BUILDKIT_PROGRESS=$(BUILDKIT_PROGRESS) docker build --build-arg PLATFORMS="$(PLATFORMS)" --file Docker/Dockerfile -o type=local,dest=Docker/ .

clean:
# Remove build artifacts
@rm -f Docker/docker_build.tar.gz
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ See [slh/README.md](slh/README.md) for more information.

As a prerequisite for simulation, you'll need:

* [Verilator](https://github.com/verilator/verilator) verilog simulator.
* [Verilator](https://github.com/verilator/verilator) verilog simulator. This might be packaged on some Linux distros.
* A RISC-V cross-compiler that supports bare-metal targets. You can build a suitable [riscv-gnu-toolchain](https://github.com/riscv/riscv-gnu-toolchain)
with `./configure --enable-multilib` and `make newlib`.
with `./configure --enable-multilib` and `make newlib`. Under some Linux based distros (such as Debian), it is possible to use the packaged
`gcc-riscv64-unknown-elf` along with the packaged `picolibc-riscv64-unknown-elf`: note that in this case, you will have to export the `USE_PICOLIBC=1` environment
variable when compiling: `USE_PICOLIBC=1 make veri` (this is due to some divergence between `newlib` and `picolibc` C standard libraries linking scripts).

Both of these may be available as packages for Linux operating systems. The name of your toolchain is set in `XCHAIN` variable in the [Makefile](Makefile).
The name of your toolchain is set in `XCHAIN` variable in the [Makefile](Makefile).

To build and run a quick end-to-end test, try:
```
Expand Down Expand Up @@ -107,6 +109,16 @@ exit()
```
The readout from this particular execution of SLH-DSA-SHAKE-128f is that KeyGen was 204310 cycles, signing was 4943111 cycles, and verification was 434660 cycles. Furthermore, the self-tests were a PASS; the output matched the Known Answer Tests. Modify the end of `test_bench.c` to have broader test behavior.

## Using Docker

Alternatively, it is possible to use an Ubuntu based Docker container for the simulation (with preinstalled verilator and toolchain). Having Docker installed, simply execute:


```
DOCKER_VERBOSE=1 make -f Makefile.docker
```

This will put the compilation and profiling artifacts in the compressed `Docker/docker_build.tar.gz` file.

## Some other targets

Expand Down
35 changes: 35 additions & 0 deletions drv/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,36 @@ int test_sloth(); // test_sloth.c
int test_bench(); // test_bench.c
int test_leak(); // test_leak.c

#ifdef _PICOLIBC__
// XXX In case of Picolibc, redirect stdio related stuff to uart
// (see https://github.com/picolibc/picolibc/blob/main/doc/os.md)
// This allows to use printf family of functions
#include <stdio.h>
#include <stdlib.h>
static int sample_putc(char c, FILE *file)
{
(void) file; /* Not used in this function */
sio_putc(c); /* Defined by underlying system */
return c;
}

static int sample_getc(FILE *file)
{
unsigned char c;
(void) file; /* Not used in this function */
c = sio_getc(); /* Defined by underlying system */
return c;
}

FILE __stdio = FDEV_SETUP_STREAM(sample_putc,
sample_getc,
NULL,
_FDEV_SETUP_RW);

FILE *const stdin = &__stdio; __strong_reference(stdin, stdout); __strong_reference(stdin, stderr);
#endif


int main()
{
int fail = 0;
Expand Down Expand Up @@ -68,6 +98,11 @@ int main()
sio_putc(4); // translated to EOF
sio_putc(0);

#ifdef _PICOLIBC__
// XXX: in case of picolibc, explicitly exit as
// this is not performed at the return of main
exit(0);
#endif
return 0;
}

7 changes: 6 additions & 1 deletion flow/eprof.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ def cnt_str(cnt):
for rfn in lsc:
pfl=len(os.path.commonprefix([os.getcwd(), rfn]))
wfn = rfn[pfl:].replace("/","_");
try:
# Try to open the source file, if not possible skip it
fr = open(rfn, "r")
except:
print("[-] Skipping %s (not found)" % rfn)
continue
print("writing:", wfn)
fr = open(rfn, "r")
fw = open(wfn, "w")
ln = 0
for sf in fr:
Expand Down
6 changes: 6 additions & 0 deletions flow/picolibc.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# XXX: sizes to be fixed according to the (hardware) allocated RAM size of SLotH
__flash = 0x00000000;
__flash_size = 64k;
__ram = __flash + __flash_size;
__ram_size = 64k;
__stack_size = 8k;