Mapping dynamically allocated memory in kernel or process

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
psychobeagle12
Member
Member
Posts: 41
Joined: Wed Oct 26, 2011 9:31 am

Mapping dynamically allocated memory in kernel or process

Post 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!
My i386-based kernel: https://github.com/bmelikant/missy-kernel
Picking a name for my kernel was harder than picking my wife, so I just used her name until I decide!
psychobeagle12
Member
Member
Posts: 41
Joined: Wed Oct 26, 2011 9:31 am

Re: Mapping dynamically allocated memory in kernel or proces

Post 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!
My i386-based kernel: https://github.com/bmelikant/missy-kernel
Picking a name for my kernel was harder than picking my wife, so I just used her name until I decide!
User avatar
jojo
Member
Member
Posts: 138
Joined: Mon Apr 18, 2016 9:50 am
Libera.chat IRC: jojo
Location: New York New York

Re: Mapping dynamically allocated memory in kernel or proces

Post by jojo »

Haha, good on you.

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