MM Bugs
Posted: Sun Apr 16, 2006 5:00 am
Well, my MM is working, except for one aspect - When I create a Ring-3 MM Context, it overwrites my current Ring-0 MM Context, causing Page-Faults. I believe the error is somewhere in the code below, but I can't find it
Could anyone help me?
Code: Select all
typedef struct
{
unsigned long flags;
unsigned long dpl;
unsigned long cr3;
} KeMMContext;
KeMMContext* KeMemManagerCreateContext(unsigned long dpl)
{
unsigned long index = 0;
for(;index < 512;index++)
{
if(contextes[index].flags == 0)
{
break;
}
}
if(index >= 512)
return 0;
unsigned long* pagedirectory = KePhysMemManagerAllocPage();
for(index=0;index<1024;index++)
* (KePhysMemManagerGetPage(pagedirectory) + index) = 0;
KePagingInitPageDirectory(pagedirectory);
contextes[index].cr3 = (unsigned long)pagedirectory;
contextes[index].dpl = dpl;
contextes[index].flags = 1;
return (contextes + index);
}
void KeMemManagerFreeContext(KeMMContext* ctx)
{
unsigned long index = 0;
for(;index<1024;index++)
if(!(*((unsigned long*)ctx->cr3 + index) & 1))
KePhysMemManagerFreePage( (unsigned long*)(*(KePhysMemManagerGetPage((unsigned long*)ctx->cr3) + index) & ~0xFFF));
KePhysMemManagerFreePage((unsigned long*)ctx->cr3);
ctx->dpl = 0;
ctx->cr3 = 0;
ctx->flags = 0;
}
void KeInitMemManager( void )
{
unsigned long index = 0;
for(; index < 512;index++)
contextes[index].flags = 0;
for(index = 0;index<524288;index++)
KernelMemoryMap[index] = 0;
krnlctx = KeMemManagerCreateContext(0);
KeMemManagerSwitchToContext(krnlctx);
}
void KeMemManagerSwitchToContext(KeMMContext* ctx)
{
KeWriteCR3(ctx->cr3);
KeWriteCR0(0x80000000 | KeReadCR0());
}