memory.h: sweep algorithm for checking regions

This commit is contained in:
Imbus 2025-09-06 02:36:29 +02:00
parent e762502c33
commit 21d55031d9
2 changed files with 18 additions and 26 deletions

View file

@ -1,28 +1,21 @@
#include <assert.h>
#include <memory.h>
#include <string.h>
#include <uart.h>
#include <stdlib.h>
#define MAX_PROBE_SIZE (256 * 1024 * 1024) // Probe up to 256 MiB max
#define PROBE_STEP 0x1000 // Probe every 4 KiB page
int memory_sweep(const uintptr_t start, const uintptr_t end) {
assert_msg(start < end, "Start needs to be before end.");
uintptr_t sweeper = start;
size_t probe_memory(void) {
volatile u32 *addr;
u32 test_pattern = 0xA5A5A5A5;
size_t detected = 0;
for (size_t offset = 4096 * 16; offset < MAX_PROBE_SIZE; offset += PROBE_STEP) {
addr = (volatile u32 *)(KERNBASE + offset);
u32 old = *addr;
*addr = test_pattern;
if (*addr != test_pattern) {
break; // Memory not readable/writable here, stop probing
}
*addr = old; // restore original data
detected = offset + PROBE_STEP;
while (sweeper < end) {
*(uint64_t *)sweeper = 0xFAFAFAFABCBCBCBC;
sweeper += sizeof(uint64_t);
}
return detected;
sweeper -= sizeof(uint64_t);
while (sweeper != start) {
assert(*(uint64_t *)sweeper == 0xFAFAFAFABCBCBCBC);
sweeper -= sizeof(uint64_t);
}
return EXIT_SUCCESS;
}

View file

@ -8,9 +8,8 @@
#define PHYSTOP (KERNBASE + 128 * 1024 * 1024)
/**
* Returns size in bytes of detected RAM In qemu, it requires a trap handler to
* handle the interrupt when accessing unavailable memory.
* Set and fetch routine for checking memory.
*/
size_t probe_memory(void);
int memory_sweep(const uintptr_t start, const uintptr_t end);
#endif // MEMORY_KARNEL_H
#endif // MEMORY_KERNEL_H