kalloc.[ch] from xv6, memory.[ch] containing memory probing algorithm and constants

This commit is contained in:
Imbus 2025-06-26 13:27:35 +02:00
parent 58ed873401
commit bd7959cc6c
5 changed files with 155 additions and 1 deletions

29
lib/memory.c Normal file
View file

@ -0,0 +1,29 @@
#include <memory.h>
#include <string.h>
#include <uart.h>
#define MAX_PROBE_SIZE (256 * 1024 * 1024) // Probe up to 256 MiB max
#define PROBE_STEP 0x1000 // Probe every 4 KiB page
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;
}
return detected;
}

16
lib/memory.h Normal file
View file

@ -0,0 +1,16 @@
#ifndef MEMORY_KERNEL_H
#define MEMORY_KERNEL_H
#include <types.h>
/* These are hardcoded for now */
#define KERNBASE 0x80000000L
#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.
*/
size_t probe_memory(void);
#endif