Hi,
ExeTwezz wrote:Brendan wrote:Don't forget that a page table entry that's marked as "present" has a minimum of 3 bits that are available for your use (e.g. enough to keep track of whether the page is a normal page of RAM or something special), and that a page table entry that's marked as "not present" has a minimum of 31 bits that are available for your use (e.g. enough to keep track of whether the page is unused, or part of a memory mapped file or in swap space, plus 29 more bits you can use for "page number in swap space" or "entry number in memory mapped file list" or whatever).
OK. Do you mean that I can store the used bit for a page in the certain entry in a page table?
Typically as the OS gets more and more complicated the virtual memory manager evolves. Initially, all "present" pages might be normal RAM and all "not present" pages might be unused. Then you might add support for "allocation on demand" (where the same physical page full of zeros is mapped everywhere as "read only" and replaced with a normal read/write page when something tries to write to it) and now a "present" page could be either normal page of RAM or the special read-only page full of zeros, and you have to be able to tell the difference. Then you might add support for memory mapped devices, where a "present" page could be (e.g.) part of video display memory and not normal RAM. After that you might add support for memory mapped files, swap space, "copy on write" shared memory areas, etc. In the end you could end up with 5 or more different types of "present" page, where you need to tell the difference between each of them quickly.
For "not present" pages it's the same - it could be "unused", or part of memory mapped file that hasn't been loaded from disk yet, or the data might have been sent to swap space, etc.
Now; for almost everything the virtual memory manager does (allocating memory, freeing memory, creating memory mapped file areas, etc), one of the first things the virtual memory manager will do is check the page table entries for the requested area, to figure out what's is currently mapped. For performance, you want to pack as much information as you can into the page table entry, (e.g.) to avoid any additional cache misses or lookups.
So...
For "present" pages there's a minimum of 3 "available for OS use" bits in each page table entry; and you've got 5 or more different types of "present" page, and need to know what type of "present" page it is quickly. Using those 3 "available for OS use" bits to keep track of the type of present page is a very obvious solution.
For "not present" pages there are a minimum of 31 "available for OS use" bits in each page table entry. In this case you only have a few different types of "not present" pages, so you only need a few of those 31 bits to keep track of what type of page it is. However, you wouldn't waste the other bits - you'd use them to speed up other things. For a simple example, maybe you use 2 of the bits to keep track of what type of not present page, and then if the page happens to be "not present, current in swap space" you use the remaining 29 bits to store a "page number within swap space" so that if you have to fetch a page from swap space you immediately know where to fetch it from.
Note: a 29-bit "page number within swap space" would allow you to support up to 2048 GiB of swap space; which is plenty for a 32-bit OS kernel.
Note: For "plain 32-bit paging" you only have 3 available bits for "present" pages and 31 available bits for "not present" pages. For PAE and long mode you get more. For example, in long mode there are 14 available bits for "present" pages and 63 available bits for "not present" pages.
ExeTwezz wrote:Is it faster than a bitmap (I have two bitmaps (C array with elements with the size of 32 bits): the first one for superpages (1024 pages; 32 elements in the array), and the second one for pages itself (32768 elements in the array)).
I have no idea what you think you need those bitmaps for.
ExeTwezz wrote:And I want to ask something. If the physical allocator uses the stack to keep free physical pages, does the virtual allocator need to map these physical pages to the consistent virtual pages starting at the given address (e.g. 0x12345000)?
Different people implement things in different ways. I don't map free physical pages (in the physical memory allocator's stack/s) into any virtual address space (but other people do).
Cheers,
Brendan