I've got a set of most peculiar problems with my kernel. They seem to be broken up into three. First of, my memory allocation function seems to be broken. When I zero out a structure created by a page-aligned memory allocation function, I also zero out another, unrelated variable. My memory allocation code is below, where placementAddress initially is equal to the end of the kernel, and align is whether to page-align the memory (set here to true)
Code: Select all
if(placementAddress % 0x1000)
placementAddress += 0x1000 - (placementAddress % 0x1000);
if(align && (placementAddress & 0xFFFFF000))
{
placementAddress &= 0xFFFFF000;
placementAddress += 0x1000;
}
if(physicalAddress)
*physicalAddress = placementAddress;
placementAddress += size;
return placementAddress - size;
My second problem is that when I write the new (ORed by 0x80000000) CR0 value, I break my GDT and IDT - Bochs says that the GDT points to invalid memory; I assume that the IDT is broken because I get an unhandled page fault exception when I have explicitly set one up. When I break into the debugger immediately after the exception, CR0 has the paging bit set; I think that this means something broken got accessed by the invalidate page instruction, suggesting in turn that the page tables are broken. The third problem is the page fault exception itself - it occurs immediately after the LEAVE instruction, but I cannot go any further in terms of debugging until I can get a proper IDT set up
My kernel is based upon James Molloy's tutorials, but I've gone through line-by-line and nothing seems different except for a different IRQ0. Either way, it isn't set up when I activate paging. My Bochs log is attached, if it helps