Doh!
Posted: Fri Jan 16, 2009 2:09 pm
I've just spent forever investigating a stack corruption issue. For some reason my stack was getting corrupted while allocating all the PDEs for kernel space. I checked all my functions were correct, nowhere was trashing memory that it wasn't supposed to, and was completely stumped.
I sat on it for a few hours, then re-ran the code under Bochs. Then I noticed something interesting...
...The address of the PDE I was modifying was suspicioulsy similar to ESP...
...And ESP pointed somewhere inside my boot page directory!
My initial thought was that my stack was too small, but I found it difficult to believe that I was consuming 64kB of stack. Then, I noticed something.
My stack is defined in kmain.c:
My entry code does this:
Spotted it yet?
BootStackTop is the address of a pointer which points to the stack. I should have been doing
Doh! That bug has been in my kernel the entire time without me noticing it!
BootStackTop is, coincidentally, located just above the page tables.
And I now know that I use about 256 bytes of stack :p
I sat on it for a few hours, then re-ran the code under Bochs. Then I noticed something interesting...
...The address of the PDE I was modifying was suspicioulsy similar to ESP...
...And ESP pointed somewhere inside my boot page directory!
My initial thought was that my stack was too small, but I found it difficult to believe that I was consuming 64kB of stack. Then, I noticed something.
My stack is defined in kmain.c:
Code: Select all
__attribute__((aligned(32))) u32 BootStack[0x4000];
u32* BootStackTop = BootStack + 0x4000;
Code: Select all
movl $BootStackTop, %esp
BootStackTop is the address of a pointer which points to the stack. I should have been doing
Code: Select all
movl BootStackTop, %esp
BootStackTop is, coincidentally, located just above the page tables.
And I now know that I use about 256 bytes of stack :p