From 376ce5e76921da72e45422d19b32c704985fac2b Mon Sep 17 00:00:00 2001 From: Douglas Reis Date: Tue, 15 Oct 2024 15:14:21 +0100 Subject: [PATCH] [sw,utils] Replace write_str for log.print Signed-off-by: Douglas Reis --- sw/cheri/checks/hyperram_test.cc | 47 +++++++++++++++++--------------- sw/cheri/common/console.hh | 36 ++++++++++++++---------- sw/cheri/tests/CMakeLists.txt | 2 +- sw/cheri/tests/hyperram_tests.hh | 44 ++++++++++++++---------------- sw/cheri/tests/i2c_tests.hh | 24 +++++++--------- sw/cheri/tests/plic_tests.hh | 35 +++++++++--------------- sw/cheri/tests/spi_tests.hh | 30 +++++++++----------- sw/cheri/tests/test_runner.cc | 21 ++++++++------ sw/cheri/tests/test_runner.hh | 11 ++++---- sw/cheri/tests/uart_tests.hh | 10 +++---- sw/cheri/tests/usbdev_tests.hh | 18 +++++------- 11 files changed, 133 insertions(+), 145 deletions(-) diff --git a/sw/cheri/checks/hyperram_test.cc b/sw/cheri/checks/hyperram_test.cc index 866ba53a5..e51b6ab68 100644 --- a/sw/cheri/checks/hyperram_test.cc +++ b/sw/cheri/checks/hyperram_test.cc @@ -16,7 +16,7 @@ // clang-format on #include -#include "../common/console-utils.hh" +#include "../common/console.hh" #include "../common/uart-utils.hh" using namespace CHERI; @@ -236,14 +236,17 @@ int execute_test(Capability &hyperram_area, ds::xoroshiro::P6 Capability root{rwRoot}; // Create a bounded capability to the UART - Capability uart = root.cast(); - uart.address() = UART_ADDRESS; - uart.bounds() = UART_BOUNDS; + Capability uart0 = root.cast(); + uart0.address() = UART_ADDRESS; + uart0.bounds() = UART_BOUNDS; - uart->init(BAUD_RATE); - set_console_mode(uart, CC_BOLD); - write_str(uart, "\r\n\r\nGet hyped for hyperram!\r\n"); - set_console_mode(uart, CC_RESET); + uart0->init(BAUD_RATE); + WriteUart uart{uart0}; + Log log(uart); + + set_console_mode(log, CC_BOLD); + log.print("\r\n\r\nGet hyped for hyperram!\r\n"); + set_console_mode(log, CC_RESET); ds::xoroshiro::P64R32 prng; prng.set_state(0xDEADBEEF, 0xBAADCAFE); @@ -258,32 +261,32 @@ int execute_test(Capability &hyperram_area, ds::xoroshiro::P6 while (true) { int failures = 0; - write_str(uart, "Running RND cap test..."); + log.print("Running RND cap test..."); failures += rand_cap_test(hyperram_area, hyperram_cap_area, prng, HyperramSize / 4); - write_test_result(uart, failures); + write_test_result(log, failures); - write_str(uart, "Running RND data test..."); + log.print("Running RND data test..."); failures = rand_data_test_full(hyperram_area, prng); - write_test_result(uart, failures); + write_test_result(log, failures); - write_str(uart, "Running RND data & address test..."); + log.print("Running RND data & address test..."); failures = rand_data_addr_test(hyperram_area, prng, HyperramSize / 4); - write_test_result(uart, failures); + write_test_result(log, failures); - write_str(uart, "Running 0101 stripe test..."); + log.print("Running 0101 stripe test..."); failures = stripe_test(hyperram_area, 0x55555555); - write_test_result(uart, failures); + write_test_result(log, failures); - write_str(uart, "Running 1001 stripe test..."); + log.print("Running 1001 stripe test..."); failures = stripe_test(hyperram_area, 0x99999999); - write_test_result(uart, failures); + write_test_result(log, failures); - write_str(uart, "Running 0000_1111 stripe test..."); + log.print("Running 0000_1111 stripe test..."); failures = stripe_test(hyperram_area, 0x0F0F0F0F); - write_test_result(uart, failures); + write_test_result(log, failures); - write_str(uart, "Running Execution test..."); + log.print("Running Execution test..."); failures = execute_test(hyperram_area, prng, HyperramSize / 4); - write_test_result(uart, failures); + write_test_result(log, failures); } } diff --git a/sw/cheri/common/console.hh b/sw/cheri/common/console.hh index 944d33fae..90b085dfd 100644 --- a/sw/cheri/common/console.hh +++ b/sw/cheri/common/console.hh @@ -4,31 +4,39 @@ * SPDX-License-Identifier: Apache-2.0 */ #pragma once -// clang-format off + +#include "fmt.hh" +#include "sonata-devices.hh" #include "../../common/defs.h" -// clang-format on #include -#include "uart-utils.hh" +#include "console.hh" #define CC_BOLD "1" #define CC_RED "31" #define CC_GREEN "32" #define CC_RESET "0" -[[maybe_unused]] static void set_console_mode(volatile OpenTitanUart *uart, const char *cc) { - write_str(uart, "\x1b["); - write_str(uart, cc); - write_str(uart, "m"); -} +struct WriteUart { + UartPtr uart; + void write(const char* buf, size_t n) { + while (n--) { + uart->blocking_write(*buf++); + } + } +}; + +using Log = reisfmt::Fmt; + +[[maybe_unused]] static void set_console_mode(Log& log, const char* cc) { log.print("\x1b[{}m", cc); } -[[maybe_unused]] static void write_test_result(volatile OpenTitanUart *uart, int failures) { +[[maybe_unused]] static void write_test_result(Log& log, int failures) { if (failures == 0) { - set_console_mode(uart, CC_GREEN); - write_str(uart, "PASS!\r\n"); + set_console_mode(log, CC_GREEN); + log.println("PASS!"); } else { - set_console_mode(uart, CC_RED); - write_str(uart, "FAIL!\r\n"); + set_console_mode(log, CC_RED); + log.println("FAIL!"); } - set_console_mode(uart, CC_RESET); + set_console_mode(log, CC_RESET); } diff --git a/sw/cheri/tests/CMakeLists.txt b/sw/cheri/tests/CMakeLists.txt index 959e76d1d..a1feb82cd 100644 --- a/sw/cheri/tests/CMakeLists.txt +++ b/sw/cheri/tests/CMakeLists.txt @@ -9,7 +9,7 @@ foreach(TEST ${TESTS}) get_filename_component(NAME ${TEST} NAME_WE) add_executable(${NAME} ${TEST}) - target_include_directories(${NAME} PRIVATE ${CHERIOT_SDK_INCLUDES}) + target_include_directories(${NAME} PRIVATE ${CHERIOT_SDK_INCLUDES} "${reisfmt_SOURCE_DIR}/include") target_link_libraries(${NAME} common) install(TARGETS ${NAME}) diff --git a/sw/cheri/tests/hyperram_tests.hh b/sw/cheri/tests/hyperram_tests.hh index 7c046df0f..7a1782f76 100644 --- a/sw/cheri/tests/hyperram_tests.hh +++ b/sw/cheri/tests/hyperram_tests.hh @@ -11,10 +11,11 @@ // clang-format on #include -#include "../common/console-utils.hh" +#include "../common/console.hh" #include "../common/sonata-devices.hh" #include "../common/uart-utils.hh" #include "test_runner.hh" +#include "../common/console.hh" using namespace CHERI; @@ -256,7 +257,7 @@ int execute_test(Capability hyperram_area, ds::xoroshiro::P64 return failures; } -void hyperram_tests(CapRoot root, UartPtr console) { +void hyperram_tests(CapRoot root, Log &log) { auto hyperram_area = hyperram_ptr(root); Capability> hyperram_cap_area = root.cast>(); @@ -267,52 +268,47 @@ void hyperram_tests(CapRoot root, UartPtr console) { prng.set_state(0xDEADBEEF, 0xBAADCAFE); for (size_t i = 0; i < HYPERRAM_TEST_ITERATIONS; i++) { - write_str(console, "\r\nrunning hyperram_test: "); - write_hex8b(console, i); - write_str(console, "\\"); - write_hex8b(console, HYPERRAM_TEST_ITERATIONS - 1); - write_str(console, "\r\n "); - write_hex(console, HYPERRAM_TEST_SIZE); - write_str(console, "\r\n "); + log.println("\nrunning hyperram_test: {} \\ {}", i, HYPERRAM_TEST_ITERATIONS - 1); + log.println("HYPERRAM_TEST_SIZE: 0x{:08x}", HYPERRAM_TEST_SIZE); bool test_failed = false; int failures = 0; - write_str(console, "Running RND cap test..."); + log.print("Running RND cap test..."); failures = rand_cap_test(hyperram_area, hyperram_cap_area, prng, HYPERRAM_TEST_SIZE); test_failed |= (failures > 0); - write_test_result(console, failures); + write_test_result(log, failures); - write_str(console, "Running RND data test..."); + log.print("Running RND data test..."); failures = rand_data_test_full(hyperram_area, prng); test_failed |= (failures > 0); - write_test_result(console, failures); + write_test_result(log, failures); - write_str(console, "Running RND data & address test..."); + log.print("Running RND data & address test..."); failures = rand_data_addr_test(hyperram_area, prng, HYPERRAM_TEST_SIZE); test_failed |= (failures > 0); - write_test_result(console, failures); + write_test_result(log, failures); - write_str(console, "Running 0101 stripe test..."); + log.print("Running 0101 stripe test..."); failures = stripe_test(hyperram_area, 0x55555555); test_failed |= (failures > 0); - write_test_result(console, failures); + write_test_result(log, failures); - write_str(console, "Running 1001 stripe test..."); + log.print("Running 1001 stripe test..."); failures = stripe_test(hyperram_area, 0x99999999); test_failed |= (failures > 0); - write_test_result(console, failures); + write_test_result(log, failures); - write_str(console, "Running 0000_1111 stripe test..."); + log.print("Running 0000_1111 stripe test..."); failures = stripe_test(hyperram_area, 0x0F0F0F0F); test_failed |= (failures > 0); - write_test_result(console, failures); + write_test_result(log, failures); - write_str(console, "Running Execution test..."); + log.print("Running Execution test..."); failures = execute_test(hyperram_area, prng, HYPERRAM_TEST_SIZE); test_failed |= (failures > 0); - write_test_result(console, failures); + write_test_result(log, failures); - check_result(console, !test_failed); + check_result(log, !test_failed); } } diff --git a/sw/cheri/tests/i2c_tests.hh b/sw/cheri/tests/i2c_tests.hh index 5344c3268..c2907f537 100644 --- a/sw/cheri/tests/i2c_tests.hh +++ b/sw/cheri/tests/i2c_tests.hh @@ -3,7 +3,7 @@ #pragma once #include "../../common/defs.h" -#include "../common/console-utils.hh" +#include "../common/console.hh" #include "../common/sonata-devices.hh" #include "../common/uart-utils.hh" #include "../common/rpi-hat-eeprom.hh" @@ -312,14 +312,10 @@ int i2c_as6212_temperature_sense_test(I2cPtr i2c) { /** * Run the whole suite of I2C tests. */ -void i2c_tests(CapRoot root, UartPtr console) { +void i2c_tests(CapRoot root, Log& log) { // Execute the specified number of iterations of each test. for (size_t i = 0; i < I2C_TEST_ITERATIONS; i++) { - write_str(console, "\r\nrunning i2c_test: "); - write_hex8b(console, i); - write_str(console, "\\"); - write_hex8b(console, I2C_TEST_ITERATIONS - 1); - write_str(console, "\r\n"); + log.println("\r\nrunning i2c_test: {} \\ {}", i, I2C_TEST_ITERATIONS - 1); bool test_failed = false; int failures = 0; @@ -329,27 +325,27 @@ void i2c_tests(CapRoot root, UartPtr console) { I2cPtr i2c0 = i2c_ptr(root, 0); I2cPtr i2c1 = i2c_ptr(root, 1); - write_str(console, " Running RPI HAT ID EEPROM test... "); + log.print(" Running RPI HAT ID EEPROM test... "); failures = i2c_rpi_hat_id_eeprom_test(i2c0); test_failed |= (failures > 0); - write_test_result(console, failures); + write_test_result(log, failures); - write_str(console, " Running RPI HAT ID WHO_AM_I test... "); + log.print(" Running RPI HAT ID WHO_AM_I test... "); failures = i2c_rpi_hat_imu_whoami_test(i2c1); test_failed |= (failures > 0); - write_test_result(console, failures); + write_test_result(log, failures); } if (I2C_AS6212_AVAILABLE) { // Retrieve bounded capabilities for I2C controller 1 I2cPtr i2c1 = i2c_ptr(root, 1); - write_str(console, " Running AS6212 Temperature test... "); + log.print(" Running AS6212 Temperature test... "); failures = i2c_as6212_temperature_sense_test(i2c1); test_failed |= (failures > 0); - write_test_result(console, failures); + write_test_result(log, failures); } - check_result(console, !test_failed); + check_result(log, !test_failed); } } diff --git a/sw/cheri/tests/plic_tests.hh b/sw/cheri/tests/plic_tests.hh index 2f0477250..9afb1d855 100644 --- a/sw/cheri/tests/plic_tests.hh +++ b/sw/cheri/tests/plic_tests.hh @@ -20,7 +20,7 @@ struct PlicTest { CapRoot root; PLIC::SonataPlic *plic; - UartPtr console; + Log *log_; uint32_t error_count = 0; size_t instance = 0; @@ -51,9 +51,7 @@ struct PlicTest { }}; auto uart = uart_ptr(root, instance); - write_str(console, "testing uart: "); - write_hex8b(console, instance); - write_str(console, "\r\n"); + log_->println("testing uart: {}", instance); for (size_t i = 0; i < uartMap.size(); ++i) { ip_irq_id = uartMap[i].id; is_irq_clearable = uartMap[i].can_clear; @@ -108,9 +106,7 @@ struct PlicTest { }}; auto i2c = i2c_ptr(root, instance); - write_str(console, "testing i2c: "); - write_hex8b(console, instance); - write_str(console, "\r\n"); + log_->println("testing i2c: {}", instance); for (size_t i = 0; i < i2cMap.size(); ++i) { ip_irq_id = static_cast(i2cMap[i].id); is_irq_clearable = i2cMap[i].can_clear; @@ -155,9 +151,7 @@ struct PlicTest { }}; auto spi = spi_ptr(root, instance); - write_str(console, "testing spi: "); - write_hex8b(console, instance); - write_str(console, "\r\n"); + log_->println("testing spi: {}", instance); for (size_t i = 0; i < spiMap.size(); ++i) { ip_irq_id = spiMap[i].id; is_irq_clearable = spiMap[i].can_clear; @@ -214,7 +208,7 @@ struct PlicTest { }}; auto usbdev = usbdev_ptr(root); - write_str(console, "testing usbdev: \r\n"); + log_->println("testing usbdev"); for (size_t i = 0; i < usbdevMap.size(); ++i) { ip_irq_id = usbdevMap[i].id; is_irq_clearable = usbdevMap[i].can_clear; @@ -252,11 +246,8 @@ struct PlicTest { } void log(PLIC::Interrupts fired) { - write_str(console, "irq fired: 0x"); - write_hex8b(console, static_cast(fired)); - write_str(console, ", expected: 0x"); - write_hex8b(console, static_cast(plic_irq_id)); - write_str(console, "\r\n"); + log_->println("irq fired: {:#x}, expected: {:#x}", static_cast(fired), + static_cast(plic_irq_id)); } bool all_interrupts_test(void) { @@ -298,11 +289,11 @@ extern "C" void irq_external_handler(void) { } } -void plic_tests(CapRoot root, UartPtr console) { +void plic_tests(CapRoot root, Log &log) { PLIC::SonataPlic plic(root); - plic_test.root = root; - plic_test.plic = &plic; - plic_test.console = console; - write_str(console, "running plic_test\r\n"); - check_result(console, plic_test.all_interrupts_test()); + plic_test.root = root; + plic_test.plic = &plic; + plic_test.log_ = &log; + log.println("running plic_test"); + check_result(log, plic_test.all_interrupts_test()); } diff --git a/sw/cheri/tests/spi_tests.hh b/sw/cheri/tests/spi_tests.hh index 646e8420e..077901576 100644 --- a/sw/cheri/tests/spi_tests.hh +++ b/sw/cheri/tests/spi_tests.hh @@ -3,7 +3,7 @@ #pragma once #include "../../common/defs.h" -#include "../common/console-utils.hh" +#include "../common/console.hh" #include "../common/flash-utils.hh" #include "../common/uart-utils.hh" #include "test_runner.hh" @@ -258,8 +258,8 @@ int spi_flash_slow_clock_test(Capability spi, ds::xoroshiro: /** * Run the whole suite of SPI tests. */ -void spi_tests(CapRoot root, UartPtr console) { - // Create bounded capabilities for SPI. +void spi_tests(CapRoot root, Log &log) { + // Create bounded capabilities for SPI and GPIO. Capability spi = root.cast(); spi.address() = SPI_ADDRESS; spi.bounds() = SPI_BOUNDS; @@ -272,35 +272,31 @@ void spi_tests(CapRoot root, UartPtr console) { // Execute the specified number of iterations of each test. for (size_t i = 0; i < SPI_TEST_ITERATIONS; i++) { - write_str(console, "\r\nrunning spi_test: "); - write_hex8b(console, i); - write_str(console, "\\"); - write_hex8b(console, SPI_TEST_ITERATIONS - 1); - write_str(console, "\r\n"); + log.println("\r\nrunning spi_test: {} \\ {}", i, SPI_TEST_ITERATIONS - 1); bool test_failed = false; int failures = 0; - write_str(console, " Running Flash Jedec ID Read test... "); + log.print(" Running Flash Jedec ID Read test... "); failures = spi_read_flash_jedec_id_test(spi, spi_flash); test_failed |= (failures > 0); - write_test_result(console, failures); + write_test_result(log, failures); - write_str(console, " Running Flash Sector Erase test... "); + log.print(" Running Flash Sector Erase test... "); failures = spi_flash_erase_test(spi, prng, spi_flash); test_failed |= (failures > 0); - write_test_result(console, failures); + write_test_result(log, failures); - write_str(console, " Running Flash Random Data test... "); + log.print(" Running Flash Random Data test... "); failures = spi_flash_random_data_test(spi, prng, spi_flash); test_failed |= (failures > 0); - write_test_result(console, failures); + write_test_result(log, failures); - write_str(console, " Running Slow Clock test... "); + log.print(" Running Slow Clock test... "); failures = spi_flash_slow_clock_test(spi, prng, spi_flash); test_failed |= (failures > 0); - write_test_result(console, failures); + write_test_result(log, failures); - check_result(console, !test_failed); + check_result(log, !test_failed); } } diff --git a/sw/cheri/tests/test_runner.cc b/sw/cheri/tests/test_runner.cc index 333fbb56d..d7a013175 100644 --- a/sw/cheri/tests/test_runner.cc +++ b/sw/cheri/tests/test_runner.cc @@ -9,6 +9,7 @@ // clang-format off #include "../../common/defs.h" +#include "../common/console.hh" #include "uart_tests.hh" #include "hyperram_tests.hh" #include "plic_tests.hh" @@ -28,14 +29,16 @@ extern "C" void entry_point(void *rwRoot) { CapRoot root{rwRoot}; - auto console = uart_ptr(root); + auto uart0 = uart_ptr(root); + uart0->init(BAUD_RATE); + WriteUart uart{uart0}; + Log log(uart); - console->init(BAUD_RATE); - uart_tests(root, console); - i2c_tests(root, console); - spi_tests(root, console); - hyperram_tests(root, console); - usbdev_tests(root, console); - plic_tests(root, console); - finish_running(console, "All tests finished"); + uart_tests(root, log); + i2c_tests(root, log); + spi_tests(root, log); + hyperram_tests(root, log); + usbdev_tests(root, log); + plic_tests(root, log); + finish_running(log, "All tests finished"); } diff --git a/sw/cheri/tests/test_runner.hh b/sw/cheri/tests/test_runner.hh index fd8266e8d..94c7d77b0 100644 --- a/sw/cheri/tests/test_runner.hh +++ b/sw/cheri/tests/test_runner.hh @@ -5,18 +5,17 @@ */ #pragma once #include "../common/sonata-devices.hh" -#include "../common/uart-utils.hh" +#include "../common/console.hh" -[[noreturn]] static void finish_running(UartPtr uart, const char *message) { - write_str(uart, message); - write_str(uart, "\r\n"); +[[noreturn]] static void finish_running(Log& log, const char* message) { + log.println(message); while (true) asm volatile("wfi"); } -[[maybe_unused]] static void check_result(UartPtr console, bool result) { +[[maybe_unused]] static void check_result(Log& log, bool result) { if (result) { return; } - finish_running(console, "Test(s) Failed"); + finish_running(log, "Test(s) Failed"); } diff --git a/sw/cheri/tests/uart_tests.hh b/sw/cheri/tests/uart_tests.hh index 99724792a..16d1867ef 100644 --- a/sw/cheri/tests/uart_tests.hh +++ b/sw/cheri/tests/uart_tests.hh @@ -47,11 +47,11 @@ bool uart_interrupt_state_test(UartPtr uart) { return count == 5; } -void uart_tests(CapRoot root, UartPtr console) { +void uart_tests(CapRoot root, Log& log) { auto uart1 = uart_ptr(root, 1); - write_str(console, "running uart_loopback_test\r\n"); - check_result(console, uart_loopback_test(uart1)); - write_str(console, "running uart_interrupt_state_test\r\n"); - check_result(console, uart_interrupt_state_test(uart1)); + log.println("running uart_loopback_test"); + check_result(log, uart_loopback_test(uart1)); + log.println("running uart_interrupt_state_test"); + check_result(log, uart_interrupt_state_test(uart1)); } diff --git a/sw/cheri/tests/usbdev_tests.hh b/sw/cheri/tests/usbdev_tests.hh index 32f0c31b5..d538a734c 100644 --- a/sw/cheri/tests/usbdev_tests.hh +++ b/sw/cheri/tests/usbdev_tests.hh @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 #pragma once #include "../../common/defs.h" -#include "../common/console-utils.hh" +#include "../common/console.hh" #include "../common/sonata-devices.hh" #include "../common/timer-utils.hh" #include "../common/uart-utils.hh" @@ -98,7 +98,7 @@ static int usbdev_configure_test(Capability usbdev) { /** * Run the whole suite of USB device tests. */ -void usbdev_tests(CapRoot root, UartPtr console) { +void usbdev_tests(CapRoot root, Log &log) { // Create a bounded capability to the USB Device UsbdevPtr usbdev = usbdev_ptr(root); @@ -107,18 +107,14 @@ void usbdev_tests(CapRoot root, UartPtr console) { // Execute the specified number of iterations of each test for (size_t i = 0; i < USBDEV_TEST_ITERATIONS; i++) { - write_str(console, "\n\nrunning usbdev_test: "); - write_hex8b(console, i); - write_str(console, "\\"); - write_hex8b(console, USBDEV_TEST_ITERATIONS - 1); - write_str(console, "\r\n\x1b[35m(needs User USB connected to a Type-A USB host port)"); - write_str(console, "\x1b[0m\r\n "); + log.println("\n\nrunning usbdev_test: {} \\ {}", i, USBDEV_TEST_ITERATIONS - 1); + log.println("\x1b[35m(needs User USB connected to a Type-A USB host port)\x1b[0m"); int failures = 0; - write_str(console, "Running USBDEV configure test... "); + log.print("Running USBDEV configure test... "); failures = usbdev_configure_test(usbdev); - write_test_result(console, failures); + write_test_result(log, failures); - check_result(console, failures == 0); + check_result(log, failures == 0); } }