Created
May 9, 2026 13:14
-
-
Save DavidBuchanan314/212ad6e66593c3b6a10434bbac681141 to your computer and use it in GitHub Desktop.
freestanding memtest util (for characterising dram bus FI)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define SYS_INLINE_SYSCALL 1 | |
| static int my_errno; | |
| #define SYS_ERRNO my_errno | |
| #include "linux_syscall_support.h" | |
| typedef unsigned long uint64_t; | |
| typedef unsigned long uintptr_t; | |
| typedef unsigned long size_t; | |
| typedef long intptr_t; | |
| #define PROT_READ 0x1 | |
| #define PROT_WRITE 0x2 | |
| #define MAP_PRIVATE 0x02 | |
| #define MAP_ANONYMOUS 0x20 | |
| #define MAP_POPULATE 0x008000 | |
| #define MAP_FAILED ((void *)-1) | |
| // needs to be larger than L3 | |
| #define MAP_SIZE 0x2000000UL | |
| #define STDOUT 1 | |
| #define STDERR 2 | |
| static size_t str_len(const char *s) { | |
| size_t n = 0; | |
| while (s[n]) n++; | |
| return n; | |
| } | |
| static void write_str(int fd, const char *s) { | |
| sys_write(fd, s, str_len(s)); | |
| } | |
| static void write_hex16(int fd, uint64_t v) { | |
| char buf[16]; | |
| static const char hex[] = "0123456789abcdef"; | |
| for (int i = 15; i >= 0; i--) { | |
| buf[i] = hex[v & 0xf]; | |
| v >>= 4; | |
| } | |
| sys_write(fd, buf, 16); | |
| } | |
| static void report_error(uint64_t addr, uint64_t expected, uint64_t actual) { | |
| write_str(STDOUT, "\nERROR: addr=0x"); | |
| write_hex16(STDOUT, addr); | |
| write_str(STDOUT, " expected=0x"); | |
| write_hex16(STDOUT, expected); | |
| write_str(STDOUT, " actual=0x"); | |
| write_hex16(STDOUT, actual); | |
| write_str(STDOUT, " diff=0x"); | |
| write_hex16(STDOUT, expected ^ actual); | |
| write_str(STDOUT, "\n"); | |
| } | |
| void _start(void) { | |
| uint64_t *mapping = sys_mmap( | |
| (void *)0, MAP_SIZE, | |
| PROT_READ | PROT_WRITE, | |
| MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, | |
| -1, 0); | |
| if (mapping == MAP_FAILED) { | |
| write_str(STDERR, "mmap failed\n"); | |
| sys_exit_group(1); | |
| __builtin_unreachable(); | |
| } | |
| const size_t n = MAP_SIZE / 8; | |
| for (;;) { | |
| sys_write(STDOUT, ".", 1); | |
| uint64_t rng = 0xbeef; | |
| for (size_t i = 0; i < n; i++) { | |
| rng = rng * 6364136223846793005UL + 1; | |
| mapping[i] = rng; | |
| } | |
| rng = 0xbeef; | |
| for (size_t i = 0; i < n; i++) { | |
| rng = rng * 6364136223846793005UL + 1; | |
| uint64_t actual = mapping[i]; | |
| if (rng != actual) { | |
| report_error((uintptr_t)&mapping[i], rng, actual); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment