I have built my own physical frame allocator, using two bitmaps (for superpages and pages) for allocating real (physical) memory.
Now it's time to implement the virtual memory allocator. People told me to use a pointer to the end of the bss section (that is the heap) and use a free list of blocks. On each malloc(), I should look into this free list, and if there is no more free blocks, I should increment the heap pointer and add it to the free list. If I have found a block big enough, I should return a pointer to it.
But that's my problem: I don't have the virtual address! So how can I do this? Besides, how is a virtual address set up? I always thought it depends on which page table and page directory I use. Or can I just map a physical address to any virtual address (I determine the virtual address)?
best regards,
A. Blessing
Virtual Addresses
-
Whatever5k
Re:Virtual Addresses
I am wondering why this code should work (it's Tim Robinson's code, so it *does* work
):
He allocates physical memory with MemAlloc(), that's ok. But what does MemMap() do? Does it look for a free page table entry and map the address with flags into it? Tim, could I please see the MemMap() function? What is the first parameter for? And the second and the third?
Why can you return start as a virtual address?
Code: Select all
addr_t kernel_sbrk;
void *__morecore(size_t diff)
{
addr_t new_sbrk, start, phys;
diff = PAGE_ALIGN_UP(diff);
assert(diff < 1048576);
if (kernel_sbrk + diff >= 0xf0000000)
return NULL;
start = kernel_sbrk;
new_sbrk = kernel_sbrk + diff;
for (; kernel_sbrk < new_sbrk; kernel_sbrk += PAGE_SIZE)
{
phys = MemAlloc();
if (phys == NULL)
return (char*) -1;
if (!MemMap(kernel_sbrk, phys, kernel_sbrk + PAGE_SIZE,
PRIV_WR | PRIV_KERN | PRIV_PRES))
return NULL;
}
return (void*) start;
}
Why can you return start as a virtual address?
-
pskyboy
Re:Virtual Addresses
kernel_sbrk is a pointer to the current end of his kernel heap. so what he does is assigns this to start and returns this as the place for the calling fucntion to start using as heap memory. He then goes through a for loop adding pages to kernel_sbrk till the this points to the next area or heap memory that will be returned on the next call. Then all the memorymap funtion does is assign physical pages to the memory he has just returned as being able to be used as heap memory.
Peter
Peter
-
Whatever5k
Re:Virtual Addresses
Oh, I think that helped a lot...
But nevertheless, I would like to see the MemMap() function.
best regards and thanks,
A. Blessing
But nevertheless, I would like to see the MemMap() function.
best regards and thanks,
A. Blessing
-
pskyboy
Re:Virtual Addresses
Yeah, i would like to see it as well if tim is willing to post it.
Peter
Peter