Page 1 of 1

I'm a little confused about virtual memory management

Posted: Fri Feb 08, 2013 6:26 pm
by BMW
Ok, I have a physical memory manager. I can call pmmngr_alloc_block(), and get a pointer to a 4KB block of physical RAM.
I have a virtual memory manager, with paging enabled. ATM I can call vmmngr_alloc_page(PageTableEntry* e) to map a page entry to a random physical address (first free block found).

But what if my kernel needs some memory (or a process). How is each processes VAS managed? Should I make another bitmap (my physical memory manager uses one) for each processes VAS?

EDIT: Oh wait... does this answer every aspect of my question? http://wiki.osdev.org/Writing_a_memory_manager

Re: I'm a little confused about virtual memory management

Posted: Fri Feb 08, 2013 8:00 pm
by Nessphoro
BMW wrote:Is the vmmngr_alloc_page() function the same as the standard malloc() function?
No.
BMW wrote:So say I need some memory for my kernel. Using that function, I would have to decide what virtual address I wanted. But I don't care what virtual address it is, I just want some memory.
Not a question.
BMW wrote:And how does each task having its own virtual address space work? Do you have to make a page directory for each process?
Each process is isolated from other processes by having its own page directory. Yes.

Re: I'm a little confused about virtual memory management

Posted: Fri Feb 08, 2013 8:05 pm
by BMW
Nessphoro wrote:
BMW wrote:Is the vmmngr_alloc_page() function the same as the standard malloc() function?
No.
BMW wrote:So say I need some memory for my kernel. Using that function, I would have to decide what virtual address I wanted. But I don't care what virtual address it is, I just want some memory.
Not a question.
BMW wrote:And how does each task having its own virtual address space work? Do you have to make a page directory for each process?
Each process is isolated from other processes by having its own page directory. Yes.
Sorry, I edited the entire post and you replied while I was editing it.

Re: I'm a little confused about virtual memory management

Posted: Fri Feb 08, 2013 8:34 pm
by BMW
Ok, I understand now, after reading this http://wiki.osdev.org/Writing_a_memory_manager .
I have one problem though. The malloc() function needs to have a pointer to the start of free memory (i.e. after the process' code). How can I get this pointer to the malloc function? When the OS loads a program, it will load it to the beginning of its virtual address space (0x00000000), and will know how big it is. How can my OS give a pointer to the end of the loaded program to the malloc function?

Re: I'm a little confused about virtual memory management

Posted: Fri Feb 08, 2013 9:01 pm
by gerryg400
Most toolchains create a variable to C programs something like this

Code: Select all

void _end
at the end of the program. Check the docs for your tools. You can take the address of that variable and start your heap just after it.

Re: I'm a little confused about virtual memory management

Posted: Fri Feb 08, 2013 9:55 pm
by BMW
gerryg400 wrote:Most toolchains create a variable to C programs something like this

Code: Select all

void _end
at the end of the program. Check the docs for your tools. You can take the address of that variable and start your heap just after it.
Ok, but how do I get the address of that variable from my malloc() function?

I like your signature BTW!!!

Re: I'm a little confused about virtual memory management

Posted: Sat Feb 09, 2013 12:49 am
by gravaera
Yo:

In short, there are three layers of memory management: physical memory management, virtual memory management and the heap (along with object caching). You've probably already completed the PMM layer of your kernel. What you found in that article (http://wiki.osdev.org/Writing_a_memory_manager) is a sloppy description of a heap.

VMM is a per-process abstraction that entails several key subcomponents: Virtual address space management, executable section and file mapping management and shared memory management. Your VMM will generally provide a function similar to:

Code: Select all

vmm_allocPages(int nPages, uint32_t allocFlags);
Parallels to contemporary kernel APIs include POSIX's "mmap()" and "sbrk()", and NT's VirtualAlloc(), etc. This is generally a page-granular API.

--Peace out,
gravaera

Re: I'm a little confused about virtual memory management

Posted: Sat Feb 09, 2013 1:10 am
by BMW
Thanks for the reply. I have completed the physical and virtual memory management layers. All I need to know now is how to work out where the end of the executable file is so I can begin the heap. gerryg400's suggestion about the void _end variable sounds good, but how do I get the address of that variable from my malloc function?

Re: I'm a little confused about virtual memory management

Posted: Sat Feb 09, 2013 2:52 am
by gravaera
Yo:

I forgot to mention -- heaps and object caches are built on top of the VMM abstraction, and they don't know anything about how large the address space is. They break up blocks of raw pages that they allocate from the VMM. A heap shouldn't know where the end of the kernel's image, or the end of its address space mapping is.

--Peace out,
gravaera

Re: I'm a little confused about virtual memory management

Posted: Sat Feb 09, 2013 5:03 am
by gerryg400
Like this

Code: Select all

extern void _end;

void *__brk_ptr = &_end;

Re: I'm a little confused about virtual memory management

Posted: Sun Feb 10, 2013 4:20 am
by linguofreak
gerryg400 wrote:Like this

Code: Select all

extern void _end;

void *__brk_ptr = &_end;
One way you can initialize the heap is something along the lines of:

Code: Select all

free(makeblock(&_end, heap_end));
Where makeblock takes the beginning and end addresses of a region of memory to be added to the heap, creates a block header (and trailer if you want) that makes it look like the entire region had previously been allocated by malloc, and returns a pointer to the first non-header byte of the block.

Re: I'm a little confused about virtual memory management

Posted: Sun Feb 10, 2013 9:37 pm
by BMW
linguofreak wrote: One way you can initialize the heap is something along the lines of:

Code: Select all

free(makeblock(&_end, heap_end));
Where makeblock takes the beginning and end addresses of a region of memory to be added to the heap, creates a block header (and trailer if you want) that makes it look like the entire region had previously been allocated by malloc, and returns a pointer to the first non-header byte of the block.
Good idea, thanks.