How to fix memory manager
Posted: Sat Feb 23, 2008 6:24 am
Hi,
I haven't posted any of my problems here is a while - development has been going rather smoothly! However, I now have a bug in my (stack based) memory manager, and I cannot fathom what is causing it.
The problem is this: the stack-filling function pushes memory addresses onto the stack that is higher than the highest memory address. Let me give you an example - on bochs, the highest memory address is 0x200000, but when I allocate a page from the stack, it returns 0x27FF00 as an address! The same thing happens on real hardware - highest address should be 0x107EB000, memory allocated is at 0x107EB000.
I have some code that gets the memory map from grub, which I won't post here - I've tested it and it returns the correct base and limit for each part of useable memory (checked against the multiboot example kernel).
This is the code that fills the memory map:
And here is the code that gets stuff on and off of the alloc stack:
(POINTER is an unsigned long -> used to reference memory where I'd rather not use void* or int*)
I can provide more of the code and explanation of it if nescessary.
Thanks in advance for your help,
OScoder
I haven't posted any of my problems here is a while - development has been going rather smoothly! However, I now have a bug in my (stack based) memory manager, and I cannot fathom what is causing it.
The problem is this: the stack-filling function pushes memory addresses onto the stack that is higher than the highest memory address. Let me give you an example - on bochs, the highest memory address is 0x200000, but when I allocate a page from the stack, it returns 0x27FF00 as an address! The same thing happens on real hardware - highest address should be 0x107EB000, memory allocated is at 0x107EB000.
I have some code that gets the memory map from grub, which I won't post here - I've tested it and it returns the correct base and limit for each part of useable memory (checked against the multiboot example kernel).
This is the code that fills the memory map:
Code: Select all
/*Fill stack through use of memory map*/
for(i=0; i<memholes; i++) {
if(memhole_type[i] == 0x01 && (memhole_base[i] + memhole_limit[i])>(SYSMEM_BASE + SYSMEM_LIMIT)) { /*useable memory, that is not sysmem*/
this_base = memhole_base[i]; /*temp var's to use*/
this_limit = memhole_limit[i];
if((memhole_base[i] % PAGE_SIZE) != 0) /*make sure everything's on a page boundary*/
this_base += PAGE_SIZE - (memhole_base[i] % PAGE_SIZE);
if((memhole_limit[i] % PAGE_SIZE) != 0)
this_limit -= (memhole_limit[i] % PAGE_SIZE);
if(this_base < (SYSMEM_BASE + SYSMEM_LIMIT))
this_base = SYSMEM_BASE + SYSMEM_LIMIT;
for(j=this_base; j<(this_base+this_limit); j+=PAGE_SIZE)
pmem_stack_push(j);
}
}
Code: Select all
void* p_alloc_stackmem_page()
{
return((void *)pmem_stack_pop());
}
inline int pmem_stack_push(POINTER mem_addr)
{
if((unsigned long) stack < (stack_base+stack_limit)) {
stack++; /*increase top of stack*/
*stack = mem_addr; /*put address at the top of the stack*/
} else {
return(1);
}
return(0);
}
inline POINTER pmem_stack_pop()
{
POINTER retval;
if(((unsigned long) stack) > stack_base) {
retval = *stack; /*get top of stack value*/
stack--; /*shrink stack*/
return(retval);
} else {
return(0);
}
}
I can provide more of the code and explanation of it if nescessary.
Thanks in advance for your help,
OScoder