From 922ec6e4b27e8e0f35de4f438dc4522323ac6014 Mon Sep 17 00:00:00 2001 From: Alex Jones Date: Thu, 22 May 2025 10:35:10 +0100 Subject: [PATCH] heartbleed: use 64-bit mcycle read to avoid overflow in legacy The legacy demo unintentionally aliased a 32-bit `rdcycle` function to `rdcycle64`, and used its 32-bit output as a 64-bit value. This caused an overflow in `mcycle` after a few minutes of running, which sometimes left the demo stuck and unable to progress in its legacy component. Replace this function with an actual 64-bit mcycle read using the low/high registers appropriately. Signed-off-by: Alex Jones --- examples/heartbleed/legacy/heartbleed.h | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/examples/heartbleed/legacy/heartbleed.h b/examples/heartbleed/legacy/heartbleed.h index 63cea63..c2d0b70 100644 --- a/examples/heartbleed/legacy/heartbleed.h +++ b/examples/heartbleed/legacy/heartbleed.h @@ -52,7 +52,19 @@ void write_to_uart(const char *format, ...) write_to_uart(__VA_ARGS__); \ } -#define rdcycle64 get_mcycle +uint64_t rdcycle64() +{ + uint32_t mcycle_high, mcycle_low; + __asm__ volatile("1: " + "csrr t0, mcycleh\n" + "csrr %0, mcycle\n" + "csrr %1, mcycleh\n" + "bne t0, %1, 1b" + : "=r"(mcycle_low), "=r"(mcycle_high) + : + : "t0"); + return ((uint64_t)mcycle_high << 32) | mcycle_low; +} /** * @brief Read the current GPIO joystick state.