Memory.asm

This file defines page-level memory management functions of PwnOS.

See Also

Authors

  • Neil G.  Dickson
Summary
Memory.asmThis file defines page-level memory management functions of PwnOS.
Variables
PhysToVirtNumFreeEach dword in this array indicates the number of free pages in a 256-entry Physical To Virtual Table section.
MemAccessLockThis is the LOCKSTRUCT for locking access to memory management data.
PhysMemSizeM1This holds the value of one less than the # of bytes of installed memory.
Functions
AllocatePagesThis procedure allocates (64KB) physical pages at a specified virtual address (or any free virtual address if not specified), for the current process.
GetPhysAddrThis procedure gets the physcial address of a virtual address in the current process, or 0 if it is not in physical space.
SetPhysAddrThis 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).
GetVirtAddrThis procedure gets the virtual address of a physical address (whatever process it’s for).
RegisterNewPhysPgThis procedure finds a free physical page and maps a virtual address to it for a specific process.
FindFreeVirtSpaceThis procedure looks through page tables to find a range of free virtual pages that is large enough.
FindTopFreeVirtSpaceThis procedure looks through page tables to find a range of free virtual pages that is large enough.
FreePagesThis procedure frees (64KB) virtual pages and any associated physical pages or pagefile entries.
Gateway Functions of Memory.asm
Functions
AllocatePagesUGateway function for AllocatePages
FreePagesUGateway function for FreePages

Variables

PhysToVirtNumFree

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

MemAccessLock LOCKSTRUCT <0,NULL,NULL,NULL>

This is the LOCKSTRUCT for locking access to memory management data.

PhysMemSizeM1

PhysMemSizeM1 dword ?

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.

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.

Parameters

Addressvirtual address to which to map the physical pages (or NULL if any address)
nPagesnumber of pages to allocate
AllocTypeallocation type, e.g. committing or reserving; TODO: Consider removing this.
Protectionpage access and protection flags, e.g. read-only

Returns

  • virtual address of the allocated pages

GetPhysAddr

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.

Parameters

VirtAddrthe virtual address of which to get the physical address (doesn’t need to be page-aligned)

Returns

  • physical address of the virtual address, or 0 if 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).

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.

Parameters

VirtAddrthe virtual address of which to set the physical address (must be page-aligned)
PhysAddrthe physcial address to be assigned (must be page-aligned)

GetVirtAddr

This procedure gets the virtual address of a physical address (whatever process it’s for).

Note: Only makes sense when MemAccessLock is already acquired.

Parameters

PhysAddrthe virtual address of which to get the physical address

Returns

  • virtual address of the physical address, PHYSPAGEINFO_FREE if physical page not used, or PHYSPAGEINFO_UNUSABLE

RegisterNewPhysPg

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.

Parameters

VirtAddrthe virtual address of which to set the physical address
hProcessthe process for which the page is to be mapped

Returns

  • physical page number

FindFreeVirtSpace

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.

Parameters

nPageslength of range in pages

Returns

  • virtual address of the range, or 0 if none large enough

FindTopFreeVirtSpace

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.

Parameters

nPageslength of range in pages

Returns

  • virtual address of the range, or 0 if none large enough

FreePages

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.

Parameters

Addressvirtual address from which to free the virtual pages
nPagesnumber of pages to free

Gateway Functions of Memory.asm

Summary
Functions
AllocatePagesUGateway function for AllocatePages
FreePagesUGateway function for FreePages

Functions

AllocatePagesU

Gateway function for AllocatePages

This checks that the application isn’t trying to allocate system memory.

FreePagesU

Gateway function for FreePages

This checks that the application isn’t trying to free system memory.

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.
MemAccessLock LOCKSTRUCT <0,NULL,NULL,NULL>
This is the LOCKSTRUCT for locking access to memory management data.
This structure is the centre of synchronization data in PwnOS, defining the state of an access lock.
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.
This procedure frees (64KB) virtual pages and any associated physical pages or pagefile entries.
This file defines pagefile memory management functions of PwnOS.
This file defines miscellaneous memory functions of PwnOS.
This file defines constants, structures, and macros for memory management functions of PwnOS.
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 procedure looks through page tables to find a range of free virtual pages that is large enough.
This procedure looks through page tables to find a range of free virtual pages that is large enough.