diff --git a/.github/workflows/test-configs.yml b/.github/workflows/test-configs.yml index aef24ad2d9..40593e977c 100644 --- a/.github/workflows/test-configs.yml +++ b/.github/workflows/test-configs.yml @@ -739,6 +739,31 @@ jobs: config-file: ./config/examples/polarfire_mpfs250_m_qspi_ddr.config pre-build: sh tools/ci/gen_mpfs_libero_stub.sh tools/ci/mpfs_libero_stub make-args: LIBERO_FPGA_CONFIG_DIR=tools/ci/mpfs_libero_stub + # The M-mode test-app running wolfCrypt test and benchmark from DDR (the + # software build of docs/PolarFire.md; the Athena build needs the CAL). + microchip_mpfs250_m_wolfcrypt_test: + uses: ./.github/workflows/test-build-riscv.yml + with: + arch: riscv64 + config-file: ./config/examples/polarfire_mpfs250_m_qspi_ddr.config + pre-build: sed -i '/WOLFBOOT_MMODE_SMODE_BOOT/d' .config && sh tools/ci/gen_mpfs_libero_stub.sh tools/ci/mpfs_libero_stub + make-args: LIBERO_FPGA_CONFIG_DIR=tools/ci/mpfs_libero_stub NO_ASM=1 UART_QSPI_PROGRAM=1 WOLFCRYPT_TEST=1 WOLFCRYPT_BENCHMARK=1 MPFS_BENCH_SIZES=1024,4096 + # Athena F5200 offload, against the CAL from the public HSS release: the + # bootloader on its own, and the test-app driving the engine. + microchip_mpfs250_m_athena_test: + uses: ./.github/workflows/test-build-riscv.yml + with: + arch: riscv64 + config-file: ./config/examples/polarfire_mpfs250_m.config + pre-build: sh tools/ci/gen_mpfs_libero_stub.sh tools/ci/mpfs_libero_stub && sh tools/ci/fetch-mpfs-cal.sh + make-args: LIBERO_FPGA_CONFIG_DIR=tools/ci/mpfs_libero_stub MPFS_ATHENA=1 MPFS_ATHENA_CAL_DIR=tools/ci/mpfs_cal/services/crypto + microchip_mpfs250_m_athena_wolfcrypt_test: + uses: ./.github/workflows/test-build-riscv.yml + with: + arch: riscv64 + config-file: ./config/examples/polarfire_mpfs250_m_qspi_ddr.config + pre-build: sed -i '/WOLFBOOT_MMODE_SMODE_BOOT/d' .config && sh tools/ci/gen_mpfs_libero_stub.sh tools/ci/mpfs_libero_stub && sh tools/ci/fetch-mpfs-cal.sh + make-args: LIBERO_FPGA_CONFIG_DIR=tools/ci/mpfs_libero_stub NO_ASM=1 UART_QSPI_PROGRAM=1 WOLFCRYPT_TEST=1 WOLFCRYPT_BENCHMARK=1 MPFS_ATHENA=1 MPFS_ATHENA_CAL_DIR=tools/ci/mpfs_cal/services/crypto microchip_mpfs250_m_mldsa_test: uses: ./.github/workflows/test-build-riscv.yml with: diff --git a/.gitignore b/.gitignore index e58350d14b..7ed73d4d7e 100644 --- a/.gitignore +++ b/.gitignore @@ -520,6 +520,8 @@ port/stmicro/stm32h5-tz-wolfhsm/out/ # CI-only generated Libero settings stub (see tools/ci/gen_mpfs_libero_stub.sh) tools/ci/mpfs_libero_stub/ +# CI-only Microchip CAL checkout (see tools/ci/fetch-mpfs-cal.sh) +tools/ci/mpfs_cal/ # gnu-efi built for the AArch64 UEFI target and its cloned source tools/gnu-efi-aarch64/ tools/gnu-efi-src/ diff --git a/Makefile b/Makefile index e70ba6b552..92e58da67e 100644 --- a/Makefile +++ b/Makefile @@ -19,6 +19,11 @@ ifneq ($(LIBERO_FPGA_CONFIG_DIR),) override LIBERO_FPGA_CONFIG_DIR := $(abspath $(LIBERO_FPGA_CONFIG_DIR)) export LIBERO_FPGA_CONFIG_DIR endif +# Same for the Athena CAL directory (MPFS_ATHENA=1). +ifneq ($(MPFS_ATHENA_CAL_DIR),) + override MPFS_ATHENA_CAL_DIR := $(abspath $(MPFS_ATHENA_CAL_DIR)) + export MPFS_ATHENA_CAL_DIR +endif WOLFHSM_MICROCHIP_PIC32CZ ?= .. override WOLFHSM_MICROCHIP_PIC32CZ := $(abspath $(WOLFHSM_MICROCHIP_PIC32CZ)) @@ -671,7 +676,7 @@ endif @echo test-app/image.bin: wolfboot.elf - $(Q)$(MAKE) -C test-app WOLFBOOT_ROOT="$(WOLFBOOT_ROOT)" ELF_FLASH_SCATTER="$(ELF_FLASH_SCATTER)" LIBERO_FPGA_CONFIG_DIR="$(LIBERO_FPGA_CONFIG_DIR)" WOLFHSM_MICROCHIP_PIC32CZ="$(WOLFHSM_MICROCHIP_PIC32CZ)" + $(Q)$(MAKE) -C test-app WOLFBOOT_ROOT="$(WOLFBOOT_ROOT)" ELF_FLASH_SCATTER="$(ELF_FLASH_SCATTER)" LIBERO_FPGA_CONFIG_DIR="$(LIBERO_FPGA_CONFIG_DIR)" MPFS_ATHENA_CAL_DIR="$(MPFS_ATHENA_CAL_DIR)" WOLFHSM_MICROCHIP_PIC32CZ="$(WOLFHSM_MICROCHIP_PIC32CZ)" $(Q)$(SIZE) test-app/image.elf standalone: @@ -825,7 +830,7 @@ else endif test-app/image.elf: wolfboot.elf - $(Q)$(MAKE) -C test-app WOLFBOOT_ROOT="$(WOLFBOOT_ROOT)" ELF_FLASH_SCATTER="$(ELF_FLASH_SCATTER)" LIBERO_FPGA_CONFIG_DIR="$(LIBERO_FPGA_CONFIG_DIR)" WOLFHSM_MICROCHIP_PIC32CZ="$(WOLFHSM_MICROCHIP_PIC32CZ)" image.elf + $(Q)$(MAKE) -C test-app WOLFBOOT_ROOT="$(WOLFBOOT_ROOT)" ELF_FLASH_SCATTER="$(ELF_FLASH_SCATTER)" LIBERO_FPGA_CONFIG_DIR="$(LIBERO_FPGA_CONFIG_DIR)" MPFS_ATHENA_CAL_DIR="$(MPFS_ATHENA_CAL_DIR)" WOLFHSM_MICROCHIP_PIC32CZ="$(WOLFHSM_MICROCHIP_PIC32CZ)" image.elf ifneq (,$(filter 1,$(ELF_FLASH_SCATTER))$(filter ti_am64x_r5,$(TARGET))) $(Q)$(SIZE) test-app/image-orig.elf else diff --git a/docs/PolarFire.md b/docs/PolarFire.md index 7f80891813..0a63525290 100644 --- a/docs/PolarFire.md +++ b/docs/PolarFire.md @@ -1240,6 +1240,33 @@ The AES offload is worth about 4.2x on the decrypt. **The hash offload is about Athena also does not fit eNVM alongside the full feature set: with the manifest linked it needs both `GZIP` and `MPFS_SBI_SC_FLASH` dropped. Dropping `GZIP` costs nothing when the FIT's kernel is stored uncompressed. +The port also serves AES-ECB, which is how AES-XTS reaches the engine: wolfCrypt's XTS masks the whole buffer with the tweak chain and issues one bulk ECB call per XTS call (`HAVE_AES_ECB` required, distinct in/out buffers), while the tweak math stays in software. wolfBoot itself encrypts images with CTR and has no XTS use; the ECB path is for an application. Requests under `WC_MPFS_ATHENA_AES_MIN_SZ` (32 bytes) are declined to software, which keeps the XTS tweak and ciphertext-stealing blocks off the engine. Beyond what wolfBoot uses, the port offloads the rest of SHA-2, AES-CBC, AES-GCM, AES-CMAC and AES key wrap at every key size, the NRBG as a seed source, ECDSA, ECDH and EC key generation, and RSA. See `wolfcrypt/src/port/microchip/README.md` in wolfSSL. + +#### Measuring the offload bare-metal + +The test application doubles as a bare-metal wolfCrypt test and benchmark harness for the engine, in the same M-mode environment wolfBoot uses, so its figures compare with the software build of the same app and not with a Linux userspace benchmark: + +```sh +grep -v WOLFBOOT_MMODE_SMODE_BOOT config/examples/polarfire_mpfs250_m_qspi_ddr.config > .config +make clean +make NO_ASM=1 UART_QSPI_PROGRAM=1 STRIP_ELF=1 \ + LIBERO_FPGA_CONFIG_DIR=/path/to/hss/build/boards/mpfs-video-kit/fpga_design_config \ + MPFS_ATHENA=1 MPFS_ATHENA_CAL_DIR=/path/to/hart-software-services/services/crypto \ + WOLFCRYPT_TEST=1 WOLFCRYPT_BENCHMARK=1 +``` + +Load `test-app/image_v1_signed.bin` with `mpfs_qspi_prog.py` as for any M-mode test app. With the `-DWOLFBOOT_MMODE_SMODE_BOOT` line removed, the DDR configuration loads the signed app into DDR and enters it with the M-mode direct jump. `NO_ASM=1` keeps the software results on wolfCrypt's C implementations, which is what the software column below measures; without it the RISC-V AES and SHA assembly is built instead. The L2 scratchpad configuration (`polarfire_mpfs250_m_qspi.config`) has no room for the test or benchmark app. `test-app/RISCV64-mpfs250.ld` reserves 64 KB of stack for the benchmark's SP math frames, and 20 KB of heap for the Athena self-test, whose allocations `test-app/syscalls.c` never frees. `MPFS_ATHENA=1` makes the app bring the engine up itself, so the bootloader it runs under may be built with or without the offload. With `MPFS_ATHENA=1` the test and benchmark build also turns on SHA-224, AES key wrap and CMAC, and seeds wolfCrypt's DRBG from the engine's NRBG, so every offload is exercised; RSA is covered when the configuration signs with RSA. The app registers the port, runs its known-answer self-test and prints `Athena: self-test passed, engine served N call(s)`, or the failing test followed by `Athena: halted`, in which case it stops there; the stock `wolfcrypt_test()` and `benchmark_test()` then drive the engine through `WC_USE_DEVID`, with the benchmark labelling offloaded results `HW`. The benchmark runs once per call size in `MPFS_BENCH_SIZES` (1 KiB, 512 B and 4 KiB by default, or a comma-separated list on the make line such as `MPFS_BENCH_SIZES=1024,4096`; each needs twice that from `WOLFSSL_STATIC_MEMORY_TEST_SZ`), since one engine transfer per call amortises very differently at sector size and in bulk. The app ends with the total engine call count: a passing test or a plausible rate proves nothing about offload on its own, because every case the port declines runs in software. The same app built without `MPFS_ATHENA` is the software baseline. Object files are configuration-specific, so `make clean` between the two builds. + +Measured on the Video Kit with the DDR configuration (E51 at 600 MHz), the software column being wolfCrypt's default C AES for RISC-V: + +| Call size | AES-256-XTS software | AES-256-XTS Athena | AES-256-CTR Athena | SHA-384 software | SHA-384 Athena | +|---|---|---|---|---|---| +| 512 B | 48 KiB/s | 1.23 MiB/s | 12.9 MiB/s | 5.7 MiB/s | 11.4 MiB/s | +| 1 KiB | 49 KiB/s | 2.06 MiB/s | 14.6 MiB/s | 5.8 MiB/s | 13.2 MiB/s | +| 4 KiB | 49 KiB/s | 4.2 MiB/s | 16.1 MiB/s | 5.8 MiB/s | 14.8 MiB/s | + +The app keeps the five MSS watchdogs refreshed from a machine timer interrupt every 5 s, since a test or benchmark pass runs far longer than their window, and its M-mode trap handler prints the cause and halts instead of resuming at the faulting instruction. The RISC-V harts trap on misaligned accesses, so `user_settings.h` defines `WOLFSSL_USE_ALIGN` for `ARCH_RISCV64`; the AES-GCM tests and benchmark also need the wolfSSL change that takes the byte-wise GCM table multiply under that define on 64-bit builds. + ## Source files `hal/mpfs250.c` - Hardware abstraction layer (UART, QSPI, SD/eMMC, multi-hart) diff --git a/docs/Targets.md b/docs/Targets.md index e0f51d7ba3..ee7a92a682 100644 --- a/docs/Targets.md +++ b/docs/Targets.md @@ -982,7 +982,7 @@ Make variables, on the command line or in the `.config`. See [PolarFire.md](Pola | `MPFS_SD_EMMC_DEMUX_ADDR` | Fabric register that selects the SD slot or the eMMC on designs with the demux in the fabric (Icicle Kit: `0x4FFFFF00`); unset, the Video Kit's IOMUX override is used. | | `MPFS_SC_PROBE` | Read-only System Controller service survey at boot (device serial, TRNG nonce, PUF emulation): status and stability only, never a value. On by default for M-mode builds; `MPFS_SC_PROBE=0` disables it. | | `MPFS_DDR_MEMTEST` | `=1`: CPU pattern test over the DDR windows the design's SEG registers map, after training, reporting the error count per 64 MB chunk; independent of `DEBUG_DDR`. | -| `MPFS_ATHENA` (+ `MPFS_ATHENA_CAL_DIR`) | SHA-384 and AES-256-CTR offload to the Athena F5200 (TeraFire), with a mandatory self-check. | +| `MPFS_ATHENA` (+ `MPFS_ATHENA_CAL_DIR`) | SHA-384, AES-256-CTR and AES-256-ECB (AES-XTS) offload to the Athena F5200 (TeraFire), with a mandatory self-check. With `WOLFCRYPT_TEST=1` or `WOLFCRYPT_BENCHMARK=1` the test-app also drives the engine, across every algorithm the wolfSSL port offloads. | | `SNVM_KEYSTORE`, `SNVM_KEYSTORE_PROVISION`, `SNVM_KEYSTORE_REQUIRE_ROM` | Serve the verification keystore from secure NVM; the one-time writer; refuse to boot unless every keystore page is ROM-flagged. | | `SNVM_KEK`, `SNVM_ENCKEY_PROVISION` | PUF-derived KEK with RFC 3394 key-wrap for the sNVM-stored image-encryption key, and its one-time writer. | | `MPFS_SCB_SMODE_DENY` | Fence S-mode off the System Controller mailbox (PMP plus dtb node removal), so nothing after wolfBoot can reach the sNVM write service. | diff --git a/hal/mpfs250.c b/hal/mpfs250.c index bf9c8a83a2..5d8506c4fd 100644 --- a/hal/mpfs250.c +++ b/hal/mpfs250.c @@ -146,10 +146,6 @@ extern uint8_t _main_hart_hls; /* linker-provided address symbol; typed as uint8 /* CLINT MSIP register for IPI delivery */ #define CLINT_MSIP_REG(hart) (*(volatile uint32_t*)(CLINT_BASE + (hart) * 4)) -/* CLINT machine-timer comparator (per hart) and MTIME counter */ -#define CLINT_MTIMECMP_REG(hart) \ - (*(volatile uint64_t*)(CLINT_BASE + 0x4000UL + (hart) * 8UL)) -#define CLINT_MTIME_REG (*(volatile uint64_t*)(CLINT_BASE + 0xBFF8UL)) /* Signal secondary harts that E51 (main hart) is ready. */ static void mpfs_signal_main_hart_started(void) @@ -217,10 +213,10 @@ void secondary_hart_entry(unsigned long hartid, HLS_DATA* hls) } } -#if defined(MPFS_DDR_INIT) && defined(WOLFBOOT_MMODE_SMODE_BOOT) +#ifdef WOLFBOOT_RISCV_MMODE /* Enable the CLINT MTIME counter via the SYSREG RTC clock divider. Without it * MTIME never advances and an S-mode OS has no scheduler tick. */ -static void mpfs_enable_mtime(void) +void mpfs_enable_mtime(void) { volatile uint32_t *rtc_cr = (volatile uint32_t *)(SYSREG_BASE + 0x0CUL); uint32_t div; @@ -233,7 +229,9 @@ static void mpfs_enable_mtime(void) *rtc_cr |= (1UL << 16); /* enable RTC/time-base clock */ __asm__ volatile("fence iorw, iorw" ::: "memory"); } +#endif /* WOLFBOOT_RISCV_MMODE */ +#if defined(MPFS_DDR_INIT) && defined(WOLFBOOT_MMODE_SMODE_BOOT) /* SBI HSM hart_start backend: stage the target hart's mailbox and ring its * MSIP; it enters S-mode at saddr with a0=hartid, a1=opaque. */ int sbi_hal_hart_start(unsigned long hartid, unsigned long saddr, diff --git a/hal/mpfs250.h b/hal/mpfs250.h index 5cd790a227..867a278406 100644 --- a/hal/mpfs250.h +++ b/hal/mpfs250.h @@ -392,6 +392,10 @@ int mpfs_nonce(uint8_t *nonce); #ifndef CLINT_BASE #define CLINT_BASE 0x02000000UL #endif +/* Machine-timer comparator (per hart) and the MTIME counter. */ +#define CLINT_MTIMECMP_REG(hart) \ + (*(volatile uint64_t*)(CLINT_BASE + 0x4000UL + (hart) * 8UL)) +#define CLINT_MTIME_REG (*(volatile uint64_t*)(CLINT_BASE + 0xBFF8UL)) /* RTC Clock Frequency (1 MHz after divisor) */ #define RTC_CLOCK_FREQ 1000000UL @@ -476,6 +480,7 @@ void uart_write(const char* buf, unsigned int sz); #endif #ifdef WOLFBOOT_RISCV_MMODE void secondary_hart_entry(unsigned long hartid, HLS_DATA* hls); +void mpfs_enable_mtime(void); #endif #endif /* __ASSEMBLER__ */ diff --git a/include/user_settings.h b/include/user_settings.h index e30f81bf70..797b7442c8 100644 --- a/include/user_settings.h +++ b/include/user_settings.h @@ -596,6 +596,14 @@ extern int tolower(int c); # endif #endif +/* The RISC-V harts trap on a misaligned load or store with nothing to emulate + * it bare-metal, so make wolfCrypt's ALIGN* attributes real: without this the + * word-sized casts it makes on struct members (the AES-GCM table, for one) + * fault. */ +#ifdef ARCH_RISCV64 +# define WOLFSSL_USE_ALIGN +#endif + #if defined(EXT_ENCRYPTED) # define HAVE_PWDBASED #endif @@ -745,6 +753,19 @@ extern int tolower(int c); #define WOLFSSL_SHA256 #define WOLFSSL_SHA384 #define WOLFSSL_SHA512 + #elif defined(TARGET_mpfs250) && defined(MPFS_ATHENA) + /* wolfCrypt's DRBG, which wolfSSL seeds from the Athena NRBG, + * and every algorithm the wolfSSL port offloads. */ + #define HAVE_HASHDRBG + #define WOLFSSL_AES_COUNTER + #define HAVE_AES_ECB + #define WOLFSSL_AES_XTS + #define WOLFSSL_AES_DIRECT + #define HAVE_AES_KEYWRAP + #define WOLFSSL_AES_KEYWRAP_PADDING + #define WOLFSSL_CMAC + #define WOLFSSL_SHA224 + #define WOLFSSL_SHA3 #elif defined(TARGET_ti_am64x_r5) #ifndef WOLFSSL_TI_AM64X_R5 #define WC_NO_RNG @@ -764,7 +785,9 @@ extern int tolower(int c); #define WC_NO_HASHDRBG #define CUSTOM_RAND_GENERATE_SEED my_rng_seed_gen #define CUSTOM_RAND_GENERATE_BLOCK my_rng_seed_gen + #ifndef __ASSEMBLER__ extern int my_rng_seed_gen(unsigned char* output, unsigned int sz); + #endif /* Full test/benchmark algo set: AES ECB/CBC/CTR/GCM/XTS, * SHA-256/384/512, SHA-3. (AES-CBC/GCM added below; SHA-384/512 @@ -919,7 +942,17 @@ extern int tolower(int c); #define WOLFSSL_STATIC_MEMORY #endif #ifndef WOLFSSL_STATIC_MEMORY_TEST_SZ - #define WOLFSSL_STATIC_MEMORY_TEST_SZ (10 * 1024) + #if defined(TARGET_mpfs250) && defined(MPFS_DDR_INIT) + /* Every default bucket, including the two above 4 KB that the + * benchmark's 4 KB buffers and the test's larger contexts need; + * a pool without them hands out something else. Only with DDR: + * the L2 scratchpad app has no room for it. */ + #define WOLFSSL_STATIC_MEMORY_TEST_SZ (128 * 1024) + #elif defined(TARGET_mpfs250) + #define WOLFSSL_STATIC_MEMORY_TEST_SZ (64 * 1024) + #else + #define WOLFSSL_STATIC_MEMORY_TEST_SZ (10 * 1024) + #endif #endif #define WOLFSSL_STATIC_MEMORY_LEAN @@ -929,7 +962,9 @@ extern int tolower(int c); /* User time functions provided */ #define WOLFSSL_USER_CURRTIME #define XTIME my_time + #ifndef __ASSEMBLER__ extern unsigned long my_time(unsigned long* timer); + #endif #endif #if !defined(WOLFCRYPT_TEST) && !defined(WOLFCRYPT_BENCHMARK) @@ -941,6 +976,26 @@ extern int tolower(int c); #undef NO_KDF #endif +#if (defined(WOLFCRYPT_TEST) || defined(WOLFCRYPT_BENCHMARK)) && \ + defined(TARGET_mpfs250) && defined(MPFS_ATHENA) +#undef NO_CMAC +#endif + +/* The bootloader offloads only its image hash and cipher: keep the rest of the + * wolfSSL Athena port out of the image, which has little room to spare. */ +#if defined(MPFS_ATHENA) && !defined(WOLFCRYPT_TEST) && \ + !defined(WOLFCRYPT_BENCHMARK) +# define WC_MPFS_ATHENA_NO_SHA256 +# define WC_MPFS_ATHENA_NO_SHA512 +# define WC_MPFS_ATHENA_NO_CBC +# define WC_MPFS_ATHENA_NO_GCM +# define WC_MPFS_ATHENA_NO_KEYWRAP +# define WC_MPFS_ATHENA_NO_CMAC +# define WC_MPFS_ATHENA_NO_RNG +# define WC_MPFS_ATHENA_NO_ECC +# define WC_MPFS_ATHENA_NO_RSA +#endif + #if defined(WOLFBOOT_TZ_FWTPM) #undef NO_CMAC #undef NO_KDF diff --git a/options.mk b/options.mk index 46ae658032..181492fb43 100644 --- a/options.mk +++ b/options.mk @@ -2240,6 +2240,8 @@ ifeq ($(MPFS_ATHENA),1) # AES-256 ENCRYPT build. CFLAGS += -DMPFS_ATHENA -I$(MPFS_ATHENA_CAL_DIR) CFLAGS += -DWOLFSSL_MICROCHIP_MPFS -DWOLFSSL_MPFS_ATHENA + # The port's known-answer self-test is opt-in; the HAL's boot check runs it. + CFLAGS += -DWC_MPFS_ATHENA_SELFTEST # Matches the port's own WC_MPFS_ATHENA_DEVID default; wolfBoot tags its # cipher contexts with this one. CFLAGS += -DWOLFBOOT_DEVID_CRYPT=0xA7 diff --git a/test-app/Makefile b/test-app/Makefile index 0616529149..6d57fdd50a 100644 --- a/test-app/Makefile +++ b/test-app/Makefile @@ -96,6 +96,14 @@ ifeq ($(ARCH),PPC) endif endif +# The mpfs250 test and benchmark reserve a 64 KB stack; sp_c64.c frames reach +# about 13 KB. +ifeq ($(TARGET),mpfs250) + ifneq ($(filter 1,$(WOLFCRYPT_TEST) $(WOLFCRYPT_BENCHMARK)),) + STACK_USAGE_LIMIT:=16384 + endif +endif + ifeq ($(TARGET),ti_hercules) APP_OBJS:=app_$(TARGET).o ../test-app/libwolfboot.o CFLAGS+=-I"../include" @@ -274,6 +282,18 @@ ifeq ($(SEC_QORIQ),1) APP_OBJS+=$(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/port/nxp/sec_qoriq_baremetal.o endif +# PolarFire SoC Athena F5200. options.mk already supplies the defines, the CAL +# include path and the archive in LIBS; the port objects are added here because +# the apps build their own wolfCrypt object list. +ifeq ($(MPFS_ATHENA),1) + ifeq ($(WOLFCRYPT_TEST)$(WOLFCRYPT_BENCHMARK),) + $(warning MPFS_ATHENA=1 without WOLFCRYPT_TEST or WOLFCRYPT_BENCHMARK only runs the engine self-test in the test-app) + endif + WOLFCRYPT_SUPPORT=1 + APP_OBJS+=$(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/cryptocb.o + APP_OBJS+=$(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/port/microchip/mpfs_athena.o +endif + ifeq ($(WOLFCRYPT_TEST),1) CFLAGS+=-DWOLFCRYPT_TEST WOLFCRYPT_SUPPORT=1 @@ -1548,6 +1568,24 @@ endif ifeq ($(TARGET),mpfs250) LSCRIPT_TEMPLATE:=RISCV64-mpfs250.ld CFLAGS+=-DRAMFUNCTION='__attribute__((used,section(".ramcode")))' + # The wolfCrypt test and benchmark need far more stack than the plain app, + # which the L2 scratchpad configurations have no room to reserve. + ifneq ($(filter 1,$(WOLFCRYPT_TEST) $(WOLFCRYPT_BENCHMARK)),) + TEST_APP_STACK_SIZE?=0x00010000 + else + TEST_APP_STACK_SIZE?=0x00002000 + endif + # The Athena self-test allocates about 16 KB, and free() in syscalls.c does + # not return memory. + ifeq ($(MPFS_ATHENA),1) + TEST_APP_HEAP_SIZE?=0x00005000 + else + TEST_APP_HEAP_SIZE?=0x00002000 + endif + # Benchmark call sizes, comma separated: MPFS_BENCH_SIZES=1024,4096 + ifneq ($(MPFS_BENCH_SIZES),) + CFLAGS+='-DMPFS_BENCH_SIZES={$(MPFS_BENCH_SIZES)}' + endif endif ifeq ($(TARGET),psoc6) @@ -2061,7 +2099,9 @@ endif sed -e "s/@WOLFBOOT_PARTITION_BOOT_ADDRESS@/$(WOLFBOOT_PARTITION_BOOT_ADDRESS)/g" | \ sed -e "s/@WOLFBOOT_PARTITION_SIZE@/$(WOLFBOOT_PARTITION_SIZE)/g" | \ sed -e "s/@WOLFBOOT_PARTITION_UPDATE_ADDRESS@/$(WOLFBOOT_PARTITION_UPDATE_ADDRESS)/g" | \ - sed -e "s/@WOLFBOOT_PARTITION_SWAP_ADDRESS@/$(WOLFBOOT_PARTITION_SWAP_ADDRESS)/g" \ + sed -e "s/@WOLFBOOT_PARTITION_SWAP_ADDRESS@/$(WOLFBOOT_PARTITION_SWAP_ADDRESS)/g" | \ + sed -e "s/@TEST_APP_STACK_SIZE@/$(TEST_APP_STACK_SIZE)/g" | \ + sed -e "s/@TEST_APP_HEAP_SIZE@/$(TEST_APP_HEAP_SIZE)/g" \ > $(@) $(Q)rm -f .app-size .entry-point .wolfboot-offset .partition-size .header-size diff --git a/test-app/RISCV64-mpfs250.ld b/test-app/RISCV64-mpfs250.ld index 0ec912bd5b..a698017e85 100644 --- a/test-app/RISCV64-mpfs250.ld +++ b/test-app/RISCV64-mpfs250.ld @@ -3,8 +3,8 @@ /* Memory configuration for PolarFire SoC MPFS250 test app */ /* TODO: Update with actual memory map for MPFS250 */ -_Min_Heap_Size = 0x00002000; /* required amount of heap */ -_Min_Stack_Size = 0x00002000; /* required amount of stack */ +_Min_Heap_Size = @TEST_APP_HEAP_SIZE@; /* required amount of heap */ +_Min_Stack_Size = @TEST_APP_STACK_SIZE@; /* required amount of stack */ OUTPUT_ARCH( "riscv" ) diff --git a/test-app/app_mpfs250.c b/test-app/app_mpfs250.c index b280bd9e8f..0c72933d9d 100644 --- a/test-app/app_mpfs250.c +++ b/test-app/app_mpfs250.c @@ -46,6 +46,105 @@ int wolfcrypt_test(void *args); int benchmark_test(void *args); #endif +#ifdef WOLFCRYPT_BENCHMARK +/* Buffer sizes the whole benchmark is run at, in turn: the Athena F5200 does + * one engine transfer per call, so a sector-sized call and a bulk one amortise + * the setup very differently, and the software build runs the same set as the + * baseline. Each needs twice the size from the static pool + * (WOLFSSL_STATIC_MEMORY_TEST_SZ). */ +#ifndef MPFS_BENCH_SIZES +#ifdef MPFS_DDR_INIT +#define MPFS_BENCH_SIZES { 1024, 512, 4096 } +#else +/* The L2 scratchpad pool has no bucket for 4 KiB buffers. */ +#define MPFS_BENCH_SIZES { 1024, 512 } +#endif +#endif +#endif /* WOLFCRYPT_BENCHMARK */ + +#ifdef MPFS_ATHENA +#include +#include + +/* Nothing after a failed bring-up or self-test can be trusted to the engine, + * so stop here, as the bootloader's own self-check does. */ +static void mpfs_athena_app_halt(void) +{ + wolfBoot_printf("Athena: halted\r\n"); + while (1) { + /* halt */ + } +} + +static void mpfs_athena_app_init(void) +{ + int ret; + + /* The engine is brought up here, not by the bootloader: a wolfBoot built + * without MPFS_ATHENA never touched it, and the port's own init handles + * one that did. wolfcrypt_test and benchmark_test register the device + * again, which the port accepts. */ + ret = wolfCrypt_Init(); + if (ret != 0) { + wolfBoot_printf("Athena: wolfCrypt_Init failed (%d)\r\n", ret); + mpfs_athena_app_halt(); + } + ret = wc_MpfsAthena_RegisterDevice(WC_MPFS_ATHENA_DEVID); + if (ret != 0) { + wolfBoot_printf("Athena: RegisterDevice failed (%d)\r\n", ret); + mpfs_athena_app_halt(); + } + ret = wc_MpfsAthena_SelfTest(); + if (ret != 0) { + wolfBoot_printf("Athena: self-test failed: %s (rc %d)\r\n", + wc_MpfsAthena_SelfTestDesc(), ret); + mpfs_athena_app_halt(); + } + /* Zero calls means no offload path was built, and every later HW figure + * would be software. */ + if (wc_MpfsAthena_GetCbCount() == 0) { + wolfBoot_printf("Athena: self-test served no engine call\r\n"); + mpfs_athena_app_halt(); + } + wolfBoot_printf("Athena: self-test passed, engine served %u call(s)\r\n", + (unsigned int)wc_MpfsAthena_GetCbCount()); +} + +#endif /* MPFS_ATHENA */ + + +#ifdef WOLFBOOT_RISCV_MMODE +#include "../hal/riscv.h" +/* MTIME is not running in an M-mode boot: start it, then let the machine + * timer interrupt refresh the watchdogs every 5 s for as long as the app + * runs. */ +#define MPFS_WDT_PET_TICKS (5ULL * RTC_CLOCK_FREQ) + +/* The vector saved the caller's registers, so plain C: refresh the five MSS + * watchdogs, which the bitstream arms and which reset the chip well inside a + * test run, then rearm. */ +void isr_mtimer(void) +{ + mpfs_wdt_pet_all(); + CLINT_MTIMECMP_REG(0) = CLINT_MTIME_REG + MPFS_WDT_PET_TICKS; +} + +static void mpfs_wdt_timer_start(void) +{ + mpfs_enable_mtime(); + CLINT_MTIMECMP_REG(0) = CLINT_MTIME_REG + MPFS_WDT_PET_TICKS; + csr_write(mie, MIE_MTIE); + csr_set(mstatus, MSTATUS_MIE); +#ifdef MPFS_DDR_INIT + /* The bootloader raised the E51 to the Libero PLL rate; this copy of the + * HAL's clock variable, which times the benchmark, still holds the reset + * rate. Without DDR init the clock is never raised and that reset rate is + * the right one. */ + mpfs_cpu_freq_mhz = + (uint32_t)(LIBERO_SETTING_MSS_COREPLEX_CPU_CLK / 1000000UL); +#endif +} +#endif void main(void) { @@ -61,6 +160,14 @@ void main(void) wolfBoot_printf("GPL v3\r\n"); wolfBoot_printf("========================\r\n"); +#ifdef WOLFBOOT_RISCV_MMODE + mpfs_wdt_timer_start(); +#endif + +#ifdef MPFS_ATHENA + mpfs_athena_app_init(); +#endif + #ifdef WOLFCRYPT_TEST wolfBoot_printf("\r\nRunning wolfCrypt tests...\r\n"); wolfCrypt_Init(); @@ -72,11 +179,30 @@ void main(void) #ifdef WOLFCRYPT_BENCHMARK wolfBoot_printf("Running wolfCrypt benchmarks...\r\n"); wolfCrypt_Init(); - benchmark_test(NULL); + { + static const unsigned int sizes[] = MPFS_BENCH_SIZES; + unsigned int i; + /* benchmark_test() puts the block size back to its default on its + * first run, so take that run's setup first and configure after. */ + benchmark_init(); + benchmark_free(); + for (i = 0; i < sizeof(sizes) / sizeof(sizes[0]); i++) { + wolfBoot_printf("\r\n--- %u-byte calls ---\r\n", sizes[i]); + benchmark_configure(sizes[i]); + benchmark_test(NULL); + } + } wolfCrypt_Cleanup(); wolfBoot_printf("Benchmarks complete.\r\n\r\n"); #endif +#ifdef MPFS_ATHENA + /* A passing test or a plausible figure proves nothing about offload on + * its own: every case the port declines runs in software. */ + wolfBoot_printf("Athena: engine served %u call(s) in total\r\n", + (unsigned int)wc_MpfsAthena_GetCbCount()); +#endif + while(1) { /* Main application loop */ /* TODO: Add watchdog feed if needed */ diff --git a/test-app/startup_riscv.c b/test-app/startup_riscv.c index 13afa5fcf8..3fbcfef826 100644 --- a/test-app/startup_riscv.c +++ b/test-app/startup_riscv.c @@ -21,6 +21,7 @@ #include #include "hal/riscv.h" +#include "printf.h" extern void trap_entry(void); extern void trap_exit(void); @@ -77,14 +78,38 @@ void do_boot(const uint32_t *app_offset) } static uint32_t synctrap_cause = 0; -void __attribute__((naked)) isr_synctrap(void) -{ #ifdef WOLFBOOT_RISCV_MMODE - asm volatile("csrr %0, mcause" : "=r"(synctrap_cause)); - asm volatile("mret"); +/* The vector saved the caller's registers, so this can be plain C. Report the + * fault and stop rather than resume at the faulting instruction, which would + * trap again for ever with nothing on the console. Hardware clears mstatus.MIE + * on trap entry, so the timer interrupt that pets the MSS watchdogs cannot run + * here and they reset the board a few seconds after the message. */ +void isr_synctrap(void) +{ + unsigned long cause, epc, tval; + asm volatile("csrr %0, mcause" : "=r"(cause)); + asm volatile("csrr %0, mepc" : "=r"(epc)); + asm volatile("csrr %0, mtval" : "=r"(tval)); + synctrap_cause = (uint32_t)cause; + wolfBoot_printf("TRAP: cause=%lx epc=%lx tval=%lx\r\n", cause, epc, tval); + while (1) { + /* halt */ + } +} #else +void __attribute__((naked)) isr_synctrap(void) +{ asm volatile("csrr %0, scause" : "=r"(synctrap_cause)); asm volatile("sret"); +} +#endif + +/* Machine timer interrupt; a target that uses it defines its own. The default + * masks the timer, which would otherwise trap again as soon as it returned. */ +void __attribute__((weak)) isr_mtimer(void) +{ +#ifdef WOLFBOOT_RISCV_MMODE + csr_clear(mie, MIE_MTIE); #endif } diff --git a/test-app/syscalls.c b/test-app/syscalls.c index cd7cb5f7b2..12641d52c3 100644 --- a/test-app/syscalls.c +++ b/test-app/syscalls.c @@ -24,6 +24,7 @@ #include #include #include +#include #include /* Forward declaration of vsnprintf. We intentionally do not include @@ -49,6 +50,9 @@ int errno; extern char _end; /* Defined by linker */ extern char _Min_Heap_Size; /* Linker symbol: address is the value */ +/* malloc() alignment: 16 bytes on RV64 and AArch64, 8 on 32-bit targets */ +#define HEAP_ALIGN (2 * sizeof(void *)) + char *__env[1] = { 0 }; char **environ = __env; @@ -85,7 +89,8 @@ void *_sbrk(int incr) char *heap_limit; if (heap_end == 0) { - heap_end = &_end; + heap_end = (char *)(((uintptr_t)&_end + HEAP_ALIGN - 1) & + ~(uintptr_t)(HEAP_ALIGN - 1)); } prev_heap_end = heap_end; @@ -475,7 +480,11 @@ void *__memcpy_chk(void *dst, const void *src, unsigned int len, void *malloc(unsigned int size) { - void *p = _sbrk((int)size); + void *p; + if (size > (unsigned int)INT_MAX - (HEAP_ALIGN - 1)) + return (void *)0; + size = (size + HEAP_ALIGN - 1) & ~(unsigned int)(HEAP_ALIGN - 1); + p = _sbrk((int)size); if (p == (void *)-1) return (void *)0; return p; diff --git a/test-app/vector_riscv.S b/test-app/vector_riscv.S index 5dbb06eb8e..ca85d33064 100644 --- a/test-app/vector_riscv.S +++ b/test-app/vector_riscv.S @@ -131,7 +131,7 @@ IV: .align 2 j trap_empty .align 2 - j trap_empty + j _mtimer .align 2 j trap_empty .align 2 @@ -187,5 +187,14 @@ _synctrap: jal isr_synctrap trap_exit +_mtimer: + trap_entry + jal isr_mtimer + trap_exit + trap_empty: - nop +#ifdef WOLFBOOT_RISCV_MMODE + j _synctrap +#else + j trap_empty +#endif diff --git a/test-app/wolfcrypt_support.c b/test-app/wolfcrypt_support.c index 143b71d2e6..9a0f75a692 100644 --- a/test-app/wolfcrypt_support.c +++ b/test-app/wolfcrypt_support.c @@ -179,10 +179,7 @@ * - M-mode (no HSS): the 'time'/'cycle' user CSRs trap on the E51; read * the machine 'mcycle' counter (CPU clock) instead. */ #include "../hal/riscv.h" - #ifndef RISCV_SMODE_TIMER_FREQ - /* M-mode: mcycle counts the CPU clock (MSS_CPU_CLK, 600 MHz). */ - #define RISCV_SMODE_TIMER_FREQ 600000000UL - #endif + #include "../hal/mpfs250.h" static uint64_t mpfs_start_ticks = 0; static uint64_t mpfs_get_ticks(void) { diff --git a/tools/ci/fetch-mpfs-cal.sh b/tools/ci/fetch-mpfs-cal.sh new file mode 100755 index 0000000000..d220329b6f --- /dev/null +++ b/tools/ci/fetch-mpfs-cal.sh @@ -0,0 +1,19 @@ +#!/bin/sh +# fetch-mpfs-cal.sh -- fetch Microchip's PolarFire SoC user-crypto (CAL) +# headers and archive, services/crypto of hart-software-services, at a pinned +# release, for the MPFS_ATHENA builds (MPFS_ATHENA_CAL_DIR=/services/crypto). +# +# usage: tools/ci/fetch-mpfs-cal.sh [dir] (default: tools/ci/mpfs_cal) + +set -e + +OUT="${1:-tools/ci/mpfs_cal}" + +if [ -e "$OUT" ]; then + echo "$OUT already exists" >&2 + exit 1 +fi +git -c advice.detachedHead=false clone -q --depth 1 --branch v2026.10 \ + --filter=blob:none --sparse \ + https://github.com/polarfire-soc/hart-software-services "$OUT" +git -C "$OUT" sparse-checkout set services/crypto