This file defines page-level memory management functions of PwnOS.
| Memory.asm | This file defines page-level memory management functions of PwnOS. |
| Variables | |
| PhysToVirtNumFree | Each dword in this array indicates the number of free pages in a 256-entry Physical To Virtual Table section. |
| MemAccessLock | This is the LOCKSTRUCT for locking access to memory management data. |
| PhysMemSizeM1 | This holds the value of one less than the # of bytes of installed memory. |
| Functions | |
| AllocatePages | This procedure allocates (64KB) physical pages at a specified virtual address (or any free virtual address if not specified), for the current process. |
| GetPhysAddr | This procedure gets the physcial address of a virtual address in the current process, or 0 if it is not in physical space. |
| SetPhysAddr | This procedure sets the physcial address of a page corresponding to the virtual address parameter by setting page table entries (allocating/recalling a page table if the page table isn’t in physical space). |
| GetVirtAddr | This procedure gets the virtual address of a physical address (whatever process it’s for). |
| RegisterNewPhysPg | This procedure finds a free physical page and maps a virtual address to it for a specific process. |
| FindFreeVirtSpace | This procedure looks through page tables to find a range of free virtual pages that is large enough. |
| FindTopFreeVirtSpace | This procedure looks through page tables to find a range of free virtual pages that is large enough. |
| FreePages | This procedure frees (64KB) virtual pages and any associated physical pages or pagefile entries. |
| Gateway Functions of Memory.asm | |
| Functions | |
| AllocatePagesU | Gateway function for AllocatePages |
| FreePagesU | Gateway function for FreePages |
PhysToVirtNumFree dword 256 dup ( PHYSTOVIRTTBL_UNALLOCATED )
Each dword in this array indicates the number of free pages in a 256-entry Physical To Virtual Table section.
A value of PHYSTOVIRTTBL_UNALLOCATED (-1) is used to indicate that the page is not allocated. Each page holds 16 sections of 256 entries. Since there are 256 such sections, there are up to 16 pages for the <Physical to Virtual Table>.
MemAccessLock LOCKSTRUCT <0,NULL,NULL,NULL>
This is the LOCKSTRUCT for locking access to memory management data.
PhysMemSizeM1 dword ?
This holds the value of one less than the # of bytes of installed memory.
This procedure allocates (64KB) physical pages at a specified virtual address (or any free virtual address if not specified), for the current process.
TODO: Issue Inter-Processor Interrupt sent to sync page table caches on this and other processors. TODO: Have this take into account the chance that the allocation is more memory than can fit into memory, so some of it will initially be in the pagefile.
| Address | virtual address to which to map the physical pages (or NULL if any address) |
| nPages | number of pages to allocate |
| AllocType | allocation type, e.g. committing or reserving; TODO: Consider removing this. |
| Protection | page access and protection flags, e.g. read-only |
This procedure gets the physcial address of a virtual address in the current process, or 0 if it is not in physical space.
Note: Only makes sense when MemAccessLock is already acquired.
| VirtAddr | the virtual address of which to get the physical address (doesn’t need to be page-aligned) |
This procedure sets the physcial address of a page corresponding to the virtual address parameter by setting page table entries (allocating/recalling a page table if the page table isn’t in physical space).
This doesn’t set <PhysToVirtTbl> entries, just page table entries and possibly a page directory entry.
Note: Only makes sense when MemAccessLock is already acquired.
TODO: Check if virtual page is in pagefile before writing the address, so that the page can be removed from the pagefile.
| VirtAddr | the virtual address of which to set the physical address (must be page-aligned) |
| PhysAddr | the physcial address to be assigned (must be page-aligned) |
This procedure gets the virtual address of a physical address (whatever process it’s for).
Note: Only makes sense when MemAccessLock is already acquired.
| PhysAddr | the virtual address of which to get the physical address |
This procedure finds a free physical page and maps a virtual address to it for a specific process.
This sets <PhysToVirtTbl> entries and page table entries, using SetPhysAddr for the page tables.
Note: Only makes sense when MemAccessLock is already acquired.
Note: Only works for current process or common process, since page tables used are current page tables.
| VirtAddr | the virtual address of which to set the physical address |
| hProcess | the process for which the page is to be mapped |
This procedure looks through page tables to find a range of free virtual pages that is large enough.
This varies from FindTopFreeVirtSpace in that this looks starting from low addresses.
Note: Only makes sense when MemAccessLock is already acquired.
| nPages | length of range in pages |
This procedure looks through page tables to find a range of free virtual pages that is large enough.
This varies from FindFreeVirtSpace in that this looks starting from high addresses.
Note: Only makes sense when MemAccessLock is already acquired.
| nPages | length of range in pages |
This procedure frees (64KB) virtual pages and any associated physical pages or pagefile entries.
Not Implemented
TODO: Issue Inter-Processor Interrupt sent to sync page table caches on this and other processors.
| Address | virtual address from which to free the virtual pages |
| nPages | number of pages to free |
| Functions | |
| AllocatePagesU | Gateway function for AllocatePages |
| FreePagesU | Gateway function for FreePages |
Gateway function for AllocatePages
This checks that the application isn’t trying to allocate system memory.
Gateway function for FreePages
This checks that the application isn’t trying to free system memory.
Each dword in this array indicates the number of free pages in a 256-entry Physical To Virtual Table section.
PhysToVirtNumFree dword 256 dup ( PHYSTOVIRTTBL_UNALLOCATED )
This is the LOCKSTRUCT for locking access to memory management data.
MemAccessLock LOCKSTRUCT <0,NULL,NULL,NULL>
This holds the value of one less than the # of bytes of installed memory.
PhysMemSizeM1 dword ?