X-OS implements a memory management system with both paging and dynamic allocation.
The memory management subsystem consists of:
- Page frame allocation: Bitmap-based physical memory tracking
- Paging system: 3-level page tables with 4KB pages
- Dynamic allocation: Custom malloc/free implementation (xosmalloc)
- Memory protection: Separate kernel and user address spaces
0x00000000 - 0x000003FF Real Mode IVT (protected)
0x00000400 - 0x000007FF BIOS Data Area
0x00000800 - 0x00007FFF Available
0x00008000 - 0x0008FFFF System structures (GDT, IDT, TSS)
0x00090000 - 0x0009FFFF Available
0x000A0000 - 0x000FFFFF Video RAM + BIOS ROM (protected)
0x00100000 - 0x???????? Kernel + Available RAM
Located in 32b/mm/mm.c, this module manages physical memory pages.
int init_mem(void) // Initialize memory bitmap
int setused(unsigned int pos) // Mark page as used
int setfree(unsigned int pos) // Mark page as free
unsigned long alloc_npages(unsigned int npages) // Allocate consecutive pages- Uses a bitmap where each bit represents a 4KB page
- Supports allocation of consecutive page ranges
- Automatically protects system memory areas
- Tracks free page count for memory statistics
- Located at physical address
_PD_ADDR_(0x80000) - Contains 1024 entries, each covering 4MB of virtual address space
- First entry points to the initial page table
- First page table at
_PT_ADDR_(0x81000) - Maps first 4MB using identity mapping
- Each entry covers 4KB of memory
- Present bit, read/write bit, and user/supervisor bit supported
int set_paging(void)
{
// Set up page directory and first page table
// Enable identity mapping for first 4MB
// Activate paging by setting CR0.PG bit
}A custom implementation of malloc/free using linked lists.
From the original comments:
"I wrote this memory allocation manager to be as simple as possible. xosmalloc is certainly not one of the most performant allocators due to linked list traversals, but I hope it's the simplest to understand."
┌─────────────────┐ ┌─────────────────┐
│ Free Blocks │ │ Used Blocks │
│ Linked List │ │ Linked List │
└─────────────────┘ └─────────────────┘
│ │
└───────┬───────────────┘
│
┌─────────────────┐
│ Cache Pages │
│ (Metadata) │
└─────────────────┘
typedef struct _cache_element {
unsigned long base; // Block base address
unsigned long size; // Block size
struct _cache_element *next; // Next element in list
} cache_element;void init_cache(void) // Initialize allocator
void* alloc(unsigned long size) // Allocate memory block
int free(unsigned long base) // Free memory block- Search free list: Find first block >= requested size
- Split if necessary: If block is larger, split and return remainder to free list
- Coalesce on free: Merge adjacent free blocks to reduce fragmentation
- Expand heap: Allocate new pages when no suitable blocks found
- Addresses 0x00000000 - 0x00400000
- Full access to all memory
- Can execute privileged instructions
- Addresses 0x00400000+
- Limited access via page table permissions
- Cannot access kernel memory directly
The system includes several debugging utilities:
int print_bit_mem(void) // Display memory bitmap
int print_mapped_mem(void) // Show memory regions
void dump_mem(char *addr) // Hex dump memory contents- Time Complexity: O(n) for finding consecutive pages
- Space Complexity: O(1) - fixed bitmap size
- Fragmentation: External fragmentation possible
- Time Complexity: O(n) for allocation/deallocation
- Space Complexity: O(n) - metadata overhead
- Fragmentation: Internal fragmentation minimized by splitting
Key constants defined in 32b/mm/mm.h:
#define _PAGE_SIZE 4096 // Page size in bytes
#define MAPSIZE 128 // Bitmap size (4MB coverage)
#define INITIAL_PAGES_NB 1024 // Initial free page count
#define _PD_ADDR_ 0x80000 // Page directory address
#define _PT_ADDR_ 0x81000 // Page table addressPotential enhancements identified:
- Buddy system allocator for better performance
- Copy-on-write page sharing
- Swapping support for virtual memory
- NUMA-aware allocation strategies