Hi,
octacone wrote:
I have a question that confuses me a lot. I know what heap is, that is not my problem. What are the key differences between VMM and PMM? Is paging VMM or PMM (I think PMM)?
Can you show me an example where I would have to use them. What are the key functions I need to implement within them?
Physical memory manager is primarily "allocate_one_physical_page()" and "free_one_physical_page()". It might have less frequently used variations (that are mostly only needed for device drivers) to allocate multiple physical pages, possibly with various restrictions (e.g. "must have 32-bit physical address"), and possibly with "cache-ability controls". It might support things like large pages, and NUMA optimisations, and cache/page colouring, and maybe even hot-plug RAM and ECC "corrected/uncorrected" error reporting. It can also be used to manage the whole physical address space, including the ability to keep track of which areas of the physical memory map are used for memory mapped devices, and including being responsible for managing MTRRs.
Virtual memory management is about creating an illusion - the illusion of multiple separate address spaces. "Virtual" means the same as it does in "virtual machine". Historically, virtual memory and processes come from the idea of using a computer to emulate multiple (virtual) computers, where each process has one (or more) virtual CPUs (threads) and its own virtual address space. To create the illusion, the virtual memory manager typically uses paging to map "things" into the virtual address space (and control permissions, etc). Things that are mapped into the virtual address include physical memory (obtained from the physical memory manager), and swap space, and files, and whatever else. It also includes trickery to reduce (physical) memory consumption, like various "copy on write" tricks. You can think of it as some function/s to create and destroy virtual address spaces; plus keeping track of the "virtual type" of areas within a virtual address space (e.g. if an area is supposed to behave like it's RAM, or not); plus whatever is necessary to make areas behave according to how their virtual type says they should even when (e.g.) the actual type of a page is nothing like the virtual type (e.g. when a virtual page is supposed to behave like RAM, but it's actually been sent to swap space and isn't RAM at all).
The result of the virtual memory manager is that a process has its own address space (that might be 3 GiB, and could be part of larger address space that the process needn't know about) and it can do whatever it likes with its virtual address space (or at least, it can do whatever kernel lets it do).
Note that this gets us to the boundary between "OS" and "language". A programming language isn't just a language, it's a specification for (yet another) abstract machine. For example, for the C abstract machine there is no stack, no registers, no flags, no instructions and no interrupts/exceptions (but there is variables and statements and functions and signals).
At the boundary between OS and language there's "mystical voodoo" to make the language's abstract machine work as a process. This mystical voodoo includes compilers, libraries, etc; and it can also include virtualisation (e.g. Java's virtual machine). For C, "malloc()" and "free()" are part of the mystical voodoo that makes C's abstract machine work as a process. For C++ and Java "new" and "delete" are part of the mystical voodoo that makes their abstract machines work. The mystical voodoo for a language is designed to match the OS (and designed to use the kernel's API, and virtual memory manager that the kernel provides).
Cheers,
Brendan