Page 1 of 1

Best way to implement a VMM

Posted: Fri Feb 12, 2010 6:00 pm
by gzaloprgm
Hi, I am currently implementing a virtual memory manager, and I was wondering what's the best way to store a task memory usage info (the virtual pages it has and which physical frames they are mapped to), in order to free the frames when the task dies, implement COW, CrOW, swapping to disk, shared memory, etc.

I have read Teemu Voipio "VMM for dummies" pseudo tutorial, but his approach seems to use too much memory ...

How do you handle that in your kernel? What do you think is the best way to do so?

Thanks,
Gonzalo

Re: Best way to implement a VMM

Posted: Sat Feb 13, 2010 4:29 am
by Creature
Well my original idea (but I don't have multitasking yet at this time) was to have a heap for each process. This heap's memory region is (of course) mapped to physical addresses. If you are to free the process, you just free the pages used by that heap. The process itself is allocated in a shared memory region which is mapped the same for every process, so that doesn't need to be unmapped. Of course, if there is an ELF file somewhere, it might need to be unmapped as well.

Another approach could be to use the 'unused' bits in the page to determine if it is a shared page or a page owned by the process itself. If it is owned by the process, you could unmap it.

I'm just tossing idea's here though, don't know if they are of any use to you.

Re: Best way to implement a VMM

Posted: Fri Mar 05, 2010 6:24 pm
by AaronMiller
Well, in my kernel's current design, multiple processes can share the same virtual memory tree, so actually accurately mapping the usage of each process individually might be a tiny bit more complex, but in a task manager will be quite clear. (The reason for that design is to allow threads, but this is something specific to my kernel's design, and might not be what you're looking for.)

Anyway, just keep track of what pages are in use. I personally won't provide a memory manager for my OS outside of giving a process a page when it wants one. I'll let the standard C library use the services my OS provides to it so it can implement it's own heap. Saves time, and increases flexibility. As for the kernel, I'll be porting over portions of the newlib standard C library into it. (I'll obviously optimize where I can and set it up properly.)

You would calculate how many bytes a process uses by multiplying the number of pages the process uses by the number of bytes in each page. So, if each page is 4KB, the equation is num_pages_of_process * 4096. If you're just trying to get the number of KB a process uses, then num_pages_of_process * 4 would work for the 4KB page size.

Hopefully this helps. :)

Cheers,
-naota