Physical MM
Posted: Sat Aug 13, 2005 10:35 am
I wrote a MM from scratch, I think it's fine, but I'd like to know other peoples opinions. Also note that it is hardcoded not to mess with the first MB of memory. That way I don't have to protect bios ROM etc.
While I'm at it, what do you think of my idea that after my kernel enables paging, it turns itself off and loads a second-level kernel. Which really makes the first kernel a really advanced bootloader. That way I can load my 'real' kernel in the top 2GB of address space (Yes, I'm going with the W98\WME structure, but I'm going to do it properly this time
, applications won't write to it without an exception
).
Here's it:
While I'm at it, what do you think of my idea that after my kernel enables paging, it turns itself off and loads a second-level kernel. Which really makes the first kernel a really advanced bootloader. That way I can load my 'real' kernel in the top 2GB of address space (Yes, I'm going with the W98\WME structure, but I'm going to do it properly this time


Here's it:
Code: Select all
#include <stdio.h>
unsigned int *MemoryTablePtr = NULL; //Ptr to bitmap of used memory
unsigned int HalfMBAvailable = 0; //Memory in system, in half MB's
void SetMemoryTable(UINT NewPtr){ //Set bitmap location for memory
UINT i; //Counter variable
MemoryTablePtr = NewPtr; //Set our pointer to that
for(i=0; i<32768; i++)MemoryTablePtr[i] = 0; //All not in use
for(i=0; i<8; i++)MemoryTablePtr[i] = 2^32-1; //1st MB never used!
}
void SetSystemRAM(UINT num){ //Set system RAM in MB's
HalfMBAvailable = num*2; //Set it in our sub-system
}
void* PageAlloc(){ //Allocate one 4kb page, return ptr
UINT i, c; //Counter/Loop variables
if(MemoryTablePtr == NULL)return NULL; //No Bitmap? Can't continue, abort
for(i=0; i<HalfMBAvailable; i++) //Loop through all 32-part bit entries
if(MemoryTablePtr[i] != 2^32-1){ //Is there a SINGLE free page?
for(c=0; c<32; c++){ //Yes, find which one
if(MemoryTablePtr[i] & 2^c == 0){//Is it this one/is this bit unset?
MemoryTablePtr[i] |= 2^c; //Yes, now set it as used!
return (void*)((i*32+c) * 4096); //Return pointer to page!
}
}
}
return NULL; //No free pages? Sorry, NULL!
}
void PageFree(void* ptr){ //Tell us a page's use is done!
UINT index, bit; //Dword inex and bit
ptr = ptr & ~(4095); //Align to page-boundary, in case!
ptr /= 4096; //Get Page index
bit = ptr%32 //Get bit in dword in index
rem = ptr/32; //Get DWORD to lookup
MemoryTablePtr[ptr/32] &= ~(2^bit); //Set it as free
}