Page 1 of 1

Mapping dynamically allocated memory in kernel or process

Posted: Sat May 07, 2016 9:36 am
by psychobeagle12
Ok, so maybe I am way overthinking / overcomplicating this topic in my head. I think I posted on this once before but I can't for the life of me find the post (on a different board maybe? I don't know!) I am trying to figure out how to map virtual memory to dynamically allocated physical memory but I can't come up with what seems like should be a simple answer. So my page frame allocator hands out pages on a 4kb basis, so each physical page maps to a virtual page. The userspace will be from 0x00000000 to 0xbfffffff, and above the 3gb mark belongs to the kernel (this is pretty typical from what I can tell.) The kernel is mapped in at 0xc0000000 and I would ideally like to make a small buffer space for file transfers at the 0xd0000000 mark, map device drivers in at 0xe0000000, and kernel heap at 0xf0000000. The problem is, how do I determine where the physical pages should map into the address space? For example, if I would do something like:

Code: Select all

char *buffer = kernel_pages_allocate(5);
to allocate 5 blocks of 4Kb. The function should return a virtual address somewhere in the kernel heap where 5 blocks of 4kb exist. Now, when I free this memory:

Code: Select all

kernel_pages_free(*buffer);
and reallocate a new block of memory, how do I know where to go back into the virtual address space; in other words, how do I know that frame has been unmapped? I am trying to figure out the best way to handle the virtual address mapping; how would you guys do it? Should I look at something like a bitmap to show where there are holes in the virtual address space for the heap? Am I making any sense, or do I have no clue and need to rethink my entire existence? This feels like a huge mental block to me. I just can't seem to make sense of it in my head, so any insight or clarification is appreciated. It will greatly help me to see working examples or even just some clear examples, something that will help it click. Thanks guys!

Re: Mapping dynamically allocated memory in kernel or proces

Posted: Sat May 07, 2016 12:36 pm
by psychobeagle12
Ok, so I think I answered my own question. I added a function to my virtual memory manager that will find a free area in the address space that is the correct size:

Code: Select all

virtual_addr to_map = vmmngr_find_free_frame (KERNEL_HEAP_START_ADDR);
The function starts looking for free frames at the beginning of the heap and when it finds a free one it returns it. This way when I free a block the page table entries themselves keep track for me. I was definitely overthinking it!

Re: Mapping dynamically allocated memory in kernel or proces

Posted: Mon May 09, 2016 8:11 am
by jojo
Haha, good on you.

I find that, really, overthinking tends to be one of the biggest pitfalls with this stuff.