I'm a little confused about virtual memory management

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
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

I'm a little confused about virtual memory management

Post 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
Last edited by BMW on Fri Feb 08, 2013 8:08 pm, edited 4 times in total.
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

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

Post 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.
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

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

Post 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.
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

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

Post 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?
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

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

Post 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.
If a trainstation is where trains stop, what is a workstation ?
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

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

Post 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!!!
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

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

Post 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
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

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

Post 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?
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

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

Post 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
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

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

Post by gerryg400 »

Like this

Code: Select all

extern void _end;

void *__brk_ptr = &_end;
If a trainstation is where trains stop, what is a workstation ?
linguofreak
Member
Member
Posts: 510
Joined: Wed Mar 09, 2011 3:55 am

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

Post 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.
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

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

Post 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.
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
Post Reply