I'm a little confused about virtual memory management
I'm a little confused about virtual memory management
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
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."
Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
Re: I'm a little confused about virtual memory management
No.BMW wrote:Is the vmmngr_alloc_page() function the same as the standard malloc() function?
Not a question.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.
Each process is isolated from other processes by having its own page directory. Yes.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?
Re: I'm a little confused about virtual memory management
Sorry, I edited the entire post and you replied while I was editing it.Nessphoro wrote:No.BMW wrote:Is the vmmngr_alloc_page() function the same as the standard malloc() function?
Not a question.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.
Each process is isolated from other processes by having its own page directory. Yes.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?
Currently developing Lithium OS (LiOS).
Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
Re: I'm a little confused about virtual memory management
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?
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."
Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
Re: I'm a little confused about virtual memory management
Most toolchains create a variable to C programs something like this 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.
Code: Select all
void _end
If a trainstation is where trains stop, what is a workstation ?
Re: I'm a little confused about virtual memory management
Ok, but how do I get the address of that variable from my malloc() function?gerryg400 wrote:Most toolchains create a variable to C programs something like thisat 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.Code: Select all
void _end
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."
Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
- gravaera
- 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
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:
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
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);
--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.
Re: I'm a little confused about virtual memory management
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."
Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
- gravaera
- 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
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
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.
Re: I'm a little confused about virtual memory management
Like this
Code: Select all
extern void _end;
void *__brk_ptr = &_end;
If a trainstation is where trains stop, what is a workstation ?
-
- Member
- Posts: 510
- Joined: Wed Mar 09, 2011 3:55 am
Re: I'm a little confused about virtual memory management
One way you can initialize the heap is something along the lines of:gerryg400 wrote:Like thisCode: Select all
extern void _end; void *__brk_ptr = &_end;
Code: Select all
free(makeblock(&_end, heap_end));
Re: I'm a little confused about virtual memory management
Good idea, thanks.linguofreak wrote: One way you can initialize the heap is something along the lines of:
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.Code: Select all
free(makeblock(&_end, heap_end));
Currently developing Lithium OS (LiOS).
Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."