bluemoon wrote:Do we have different terminology here?
PMM, physical memory manager, only return physical address, which is a number, the number is not automatically mapped (LMM does this however).
No, I know what a PMM does; it returns a free address in the physical address space without mapping it anywhere.
bluemoon wrote:... allocate a page there. It is however also possible to actively invoke PMM_alloc and put the physical address on the page map
Exactly this! The problem is that, *what if* the PMM returns an address, that in the Virtual Address Space, doesn't have a page table?
Here's an example:
Code: Select all
current_addr = 0x00200000;
AllocPage()
{
current_addr += 0x1000;
return current_addr;
}
MapPage(uint64 virtual, uint64 physical)
{
...
if(!PageDirectory[PTIndex])
{
// Create new page table
uint64 addr = AllocPage();
// Assume here AllocPage() returns 2MB + 4K (0x00201000)
MapPage(addr, addr);
// Never gets here!
PageDirectory[PTIndex] = (addr & 0xFFFFF000) | 0x3;
}
}
Explanation:
Let's assume AllocPage() is an over-simplistic placement allocator.
MapPage() maps the physical address to the virtual address.
In this example, current_addr is at 2MB. For x64 paging at least, a PageTable can address 512 * 4K of memory = 2MB. Assuming during bootstrap to long mode, only the first 2MB is identity mapped. Meaning, no other PageTables/PageDirectories are created.
In the above, a call to MapPage(0x00300000, 0x00300000), will result in control entering the IF statement, because the PageTable addressing 0x00200000 -> 0x00400000 does not exist.
Therefore, MapPage() calls AllocPage(), to return a physical address to place the new PageTable in. In the above, it will return 0x00201000.
------
In your demand-paging example, you would simply write put this physical address in the PageDirectory anyway. When the time comes (later in the MapPage() function), to put the PageAddress into the PageTable, you cannot; you say you'd write to it anyway, generating a #PF which would map the faulting address somewhere and IRET.
Where would it map to? Assuming everything below 2MB in the VirtualAddressSpace is already mapped. That is, you have no existing PageStructures in which to place the faulting address... A call to MapPage() would therefore call AllocPage(), after which calling MapPage(), that calls AllocPage()...
------
Without demand-paging, much of what is described above would still happen. AllocPage() returns an address whose PageStructures are not created (say 0x00201000, assuming only 2MB was mapped at boot-time). When MapPage() is called with this address, the PageStructure addressing 2MB+4K still doesn't exist, which results in another call to MapPage()...
-- Thanks