Merge pull request #2337 from ergo720/mmglobaldata

Fixed wrong type for MmGlobalData kernel export
This commit is contained in:
RadWolfie 2022-03-29 08:23:22 -05:00 committed by GitHub
commit 067aa82cc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 284 additions and 236 deletions

View File

@ -17,7 +17,102 @@
namespace xbox namespace xbox
{ {
XBSYSAPI EXPORTNUM(102) PVOID MmGlobalData[8]; typedef ulong_xt PFN;
typedef ulong_xt PFN_COUNT;
typedef ulong_xt PFN_NUMBER, *PPFN_NUMBER;
typedef struct _MMPFNFREE
{
ushort_xt PackedPfnFlink;
ushort_xt PackedPfnBlink;
} MMPFNFREE, *PMMPFNFREE;
typedef struct _MMPFNREGION
{
MMPFNFREE FreePagesByColor[32];
PFN_COUNT AvailablePages;
} MMPFNREGION, *PMMPFNREGION;
/* enum describing the usage type of the memory pages */
typedef enum _PageType
{
UnknownType, // Used by the PFN database
StackType, // Used by MmCreateKernelStack
VirtualPageTableType, // Used by the pages holding the PTs that map the user memory (lower 2 GiB)
SystemPageTableType, // Used by the pages holding the PTs that map the system memory
PoolType, // Used by ExAllocatePoolWithTag
VirtualMemoryType, // Used by NtAllocateVirtualMemory
SystemMemoryType, // Used by MmAllocateSystemMemory
ImageType, // Used to mark executable memory
CacheType, // Used by the file cache related functions
ContiguousType, // Used by MmAllocateContiguousMemoryEx and others
DebuggerType, // xbdm-related
COUNTtype // The size of the array containing the page usage per type
} PageType;
/* The Xbox PTE, modelled around the Intel 386 PTE specification */
typedef struct _XBOX_PTE
{
ulong_xt Valid : 1;
ulong_xt Write : 1;
ulong_xt Owner : 1;
ulong_xt WriteThrough : 1;
ulong_xt CacheDisable : 1;
ulong_xt Accessed : 1;
ulong_xt Dirty : 1;
ulong_xt LargePage : 1;
ulong_xt Global : 1;
ulong_xt GuardOrEnd : 1; // software field used for our own purposes
ulong_xt Persist : 1; // software field used for our own purposes
ulong_xt Unused : 1; // software field used for our own purposes
ulong_xt PFN : 20;
} XBOX_PTE, *PXBOX_PTE;
/* PTE as used by the memory manager */
typedef union _MMPTE
{
ulong_xt Default;
XBOX_PTE Hardware;
} MMPTE, *PMMPTE;
typedef PFN_NUMBER (FASTCALL *PMMREMOVE_PAGE_ROUTINE) (
IN PageType BusyType,
IN PMMPTE TargetPte
);
typedef struct _MMPTERANGE
{
MMPTE HeadPte;
PMMPTE FirstCommittedPte;
PMMPTE LastCommittedPte;
PMMPTE LastReservedPte;
PFN_COUNT *AvailablePages;
PMMREMOVE_PAGE_ROUTINE RemovePageRoutine;
} MMPTERANGE, *PMMPTERANGE;
typedef struct _MMADDRESS_NODE
{
ulong_ptr_xt StartingVpn;
ulong_ptr_xt EndingVpn;
struct _MMADDRESS_NODE *Parent;
struct _MMADDRESS_NODE *LeftChild;
struct _MMADDRESS_NODE *RightChild;
} MMADDRESS_NODE, *PMMADDRESS_NODE;
typedef struct _MMGLOBALDATA
{
PMMPFNREGION RetailPfnRegion;
PMMPTERANGE SystemPteRange;
PULONG AvailablePages;
PFN_COUNT *AllocatedPagesByUsage;
PRTL_CRITICAL_SECTION AddressSpaceLock;
PMMADDRESS_NODE *VadRoot;
PMMADDRESS_NODE *VadHint;
PMMADDRESS_NODE *VadFreeHint;
} MMGLOBALDATA, *PMMGLOBALDATA;
XBSYSAPI EXPORTNUM(102) MMGLOBALDATA MmGlobalData;
// ****************************************************************** // ******************************************************************
// * MmAllocateContiguousMemory // * MmAllocateContiguousMemory

View File

@ -49,7 +49,7 @@ namespace NtDll
// ****************************************************************** // ******************************************************************
// * 0x0066 - MmGlobalData // * 0x0066 - MmGlobalData
// ****************************************************************** // ******************************************************************
XBSYSAPI EXPORTNUM(102) xbox::PVOID xbox::MmGlobalData[8] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }; XBSYSAPI EXPORTNUM(102) xbox::MMGLOBALDATA xbox::MmGlobalData = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
// ****************************************************************** // ******************************************************************
// * 0x00A4 - LaunchDataPage // * 0x00A4 - LaunchDataPage

View File

@ -45,9 +45,9 @@ inline FreeBlock* ListEntryToFreeBlock(xbox::PLIST_ENTRY pListEntry)
void PhysicalMemory::InitializePageDirectory() void PhysicalMemory::InitializePageDirectory()
{ {
PMMPTE pPde; xbox::PMMPTE pPde;
PMMPTE pPde_end; xbox::PMMPTE pPde_end;
MMPTE TempPte; xbox::MMPTE TempPte;
// Write the pde's of the WC (tiled) memory - no page tables // Write the pde's of the WC (tiled) memory - no page tables
@ -76,7 +76,7 @@ void PhysicalMemory::InitializePageDirectory()
// TODO: map memory for the file system cache? // TODO: map memory for the file system cache?
} }
void PhysicalMemory::WritePfn(PFN pfn_start, PFN pfn_end, PMMPTE pPte, PageType BusyType, bool bZero) void PhysicalMemory::WritePfn(xbox::PFN pfn_start, xbox::PFN pfn_end, xbox::PMMPTE pPte, xbox::PageType BusyType, bool bZero)
{ {
XBOX_PFN TempPF; XBOX_PFN TempPF;
@ -101,7 +101,7 @@ void PhysicalMemory::WritePfn(PFN pfn_start, PFN pfn_end, PMMPTE pPte, PageType
TempPF.Default = 0; TempPF.Default = 0;
TempPF.Busy.Busy = 1; TempPF.Busy.Busy = 1;
TempPF.Busy.BusyType = BusyType; TempPF.Busy.BusyType = BusyType;
if (BusyType != VirtualPageTableType && BusyType != SystemPageTableType) { if (BusyType != xbox::VirtualPageTableType && BusyType != xbox::SystemPageTableType) {
TempPF.Busy.PteIndex = GetPteOffset(GetVAddrMappedByPte(pPte)); TempPF.Busy.PteIndex = GetPteOffset(GetVAddrMappedByPte(pPte));
} }
else { TempPF.PTPageFrame.PtesUsed = 0; } // we are writing a pfn of a PT else { TempPF.PTPageFrame.PtesUsed = 0; } // we are writing a pfn of a PT
@ -118,13 +118,13 @@ void PhysicalMemory::WritePfn(PFN pfn_start, PFN pfn_end, PMMPTE pPte, PageType
} }
} }
void PhysicalMemory::WritePte(PMMPTE pPteStart, PMMPTE pPteEnd, MMPTE Pte, PFN pfn, bool bZero) void PhysicalMemory::WritePte(xbox::PMMPTE pPteStart, xbox::PMMPTE pPteEnd, xbox::MMPTE Pte, xbox::PFN pfn, bool bZero)
{ {
// This function is intended to write pte's, not pde's. To write those, use (De)AllocatePT which will perform // This function is intended to write pte's, not pde's. To write those, use (De)AllocatePT which will perform
// all the necessary housekeeping. Also, the pde's mapping these pte's should have already being commited or else // all the necessary housekeeping. Also, the pde's mapping these pte's should have already being commited or else
// GetPfnOfPT will assert // GetPfnOfPT will assert
PMMPTE PointerPte = pPteStart; xbox::PMMPTE PointerPte = pPteStart;
PXBOX_PFN PTpfn; PXBOX_PFN PTpfn;
if (bZero) if (bZero)
@ -163,16 +163,16 @@ void PhysicalMemory::WritePte(PMMPTE pPteStart, PMMPTE pPteEnd, MMPTE Pte, PFN p
} }
} }
bool PhysicalMemory::RemoveFree(PFN_COUNT NumberOfPages, PFN* result, PFN_COUNT PfnAlignment, PFN start, PFN end) bool PhysicalMemory::RemoveFree(xbox::PFN_COUNT NumberOfPages, xbox::PFN* result, xbox::PFN_COUNT PfnAlignment, xbox::PFN start, xbox::PFN end)
{ {
xbox::PLIST_ENTRY ListEntry; xbox::PLIST_ENTRY ListEntry;
PFN PfnStart; xbox::PFN PfnStart;
PFN PfnEnd; xbox::PFN PfnEnd;
PFN IntersectionStart; xbox::PFN IntersectionStart;
PFN IntersectionEnd; xbox::PFN IntersectionEnd;
PFN_COUNT PfnCount; xbox::PFN_COUNT PfnCount;
PFN_COUNT PfnAlignmentMask; xbox::PFN_COUNT PfnAlignmentMask;
PFN_COUNT PfnAlignmentSubtraction; xbox::PFN_COUNT PfnAlignmentSubtraction;
// The caller should already guarantee that there are enough free pages available // The caller should already guarantee that there are enough free pages available
if (NumberOfPages == 0) { result = nullptr; return false; } if (NumberOfPages == 0) { result = nullptr; return false; }
@ -307,10 +307,10 @@ bool PhysicalMemory::RemoveFree(PFN_COUNT NumberOfPages, PFN* result, PFN_COUNT
return false; return false;
} }
void PhysicalMemory::InsertFree(PFN start, PFN end) void PhysicalMemory::InsertFree(xbox::PFN start, xbox::PFN end)
{ {
xbox::PLIST_ENTRY ListEntry; xbox::PLIST_ENTRY ListEntry;
PFN_COUNT size = end - start + 1; xbox::PFN_COUNT size = end - start + 1;
ListEntry = FreeList.Blink; // search from the top ListEntry = FreeList.Blink; // search from the top
@ -374,7 +374,7 @@ void PhysicalMemory::InsertFree(PFN start, PFN end)
} }
} }
bool PhysicalMemory::ConvertXboxToSystemPtePermissions(DWORD perms, PMMPTE pPte) bool PhysicalMemory::ConvertXboxToSystemPtePermissions(DWORD perms, xbox::PMMPTE pPte)
{ {
ULONG Mask = 0; ULONG Mask = 0;
@ -431,7 +431,7 @@ bool PhysicalMemory::ConvertXboxToSystemPtePermissions(DWORD perms, PMMPTE pPte)
return false; return false;
} }
bool PhysicalMemory::ConvertXboxToPtePermissions(DWORD perms, PMMPTE pPte) bool PhysicalMemory::ConvertXboxToPtePermissions(DWORD perms, xbox::PMMPTE pPte)
{ {
ULONG Mask = 0; ULONG Mask = 0;
ULONG LowNibble; ULONG LowNibble;
@ -644,12 +644,12 @@ DWORD PhysicalMemory::ConvertXboxToWinPermissions(DWORD Perms)
bool PhysicalMemory::AllocatePT(size_t Size, VAddr addr) bool PhysicalMemory::AllocatePT(size_t Size, VAddr addr)
{ {
PFN pfn; xbox::PFN pfn;
PMMPTE pPde; xbox::PMMPTE pPde;
MMPTE TempPte; xbox::MMPTE TempPte;
PFN_COUNT PdeNumber = PAGES_SPANNED_LARGE(addr, Size); xbox::PFN_COUNT PdeNumber = PAGES_SPANNED_LARGE(addr, Size);
PFN_COUNT PTtoCommit = 0; xbox::PFN_COUNT PTtoCommit = 0;
PageType BusyType = SystemPageTableType; xbox::PageType BusyType = xbox::SystemPageTableType;
VAddr StartingAddr = addr; VAddr StartingAddr = addr;
assert(Size); assert(Size);
@ -677,7 +677,7 @@ bool PhysicalMemory::AllocatePT(size_t Size, VAddr addr)
return false; return false;
} }
if (addr <= HIGHEST_USER_ADDRESS) { BusyType = VirtualPageTableType; } if (addr <= HIGHEST_USER_ADDRESS) { BusyType = xbox::VirtualPageTableType; }
StartingAddr = addr; StartingAddr = addr;
// Now actually commit the PT's. Note that we won't construct the vma's for the PTs since they are outside of all // Now actually commit the PT's. Note that we won't construct the vma's for the PTs since they are outside of all
@ -705,9 +705,9 @@ bool PhysicalMemory::AllocatePT(size_t Size, VAddr addr)
void PhysicalMemory::DeallocatePT(size_t Size, VAddr addr) void PhysicalMemory::DeallocatePT(size_t Size, VAddr addr)
{ {
PMMPTE pPde; xbox::PMMPTE pPde;
PXBOX_PFN PTpfn; PXBOX_PFN PTpfn;
PFN_COUNT PdeNumber = PAGES_SPANNED_LARGE(addr, Size); xbox::PFN_COUNT PdeNumber = PAGES_SPANNED_LARGE(addr, Size);
VAddr StartingAddr = addr; VAddr StartingAddr = addr;
assert(Size); assert(Size);
@ -721,14 +721,14 @@ void PhysicalMemory::DeallocatePT(size_t Size, VAddr addr)
if (PTpfn->PTPageFrame.PtesUsed == 0) if (PTpfn->PTPageFrame.PtesUsed == 0)
{ {
InsertFree(pPde->Hardware.PFN, pPde->Hardware.PFN); InsertFree(pPde->Hardware.PFN, pPde->Hardware.PFN);
WritePfn(pPde->Hardware.PFN, pPde->Hardware.PFN, pPde, (PageType)PTpfn->PTPageFrame.BusyType, true); WritePfn(pPde->Hardware.PFN, pPde->Hardware.PFN, pPde, (xbox::PageType)PTpfn->PTPageFrame.BusyType, true);
WRITE_ZERO_PTE(pPde); WRITE_ZERO_PTE(pPde);
} }
StartingAddr += LARGE_PAGE_SIZE; StartingAddr += LARGE_PAGE_SIZE;
} }
} }
bool PhysicalMemory::IsMappable(PFN_COUNT PagesRequested, bool bRetailRegion, bool bDebugRegion) bool PhysicalMemory::IsMappable(xbox::PFN_COUNT PagesRequested, bool bRetailRegion, bool bDebugRegion)
{ {
bool ret = false; bool ret = false;
if (bRetailRegion && m_PhysicalPagesAvailable >= PagesRequested) { ret = true; } if (bRetailRegion && m_PhysicalPagesAvailable >= PagesRequested) { ret = true; }
@ -738,12 +738,12 @@ bool PhysicalMemory::IsMappable(PFN_COUNT PagesRequested, bool bRetailRegion, bo
return ret; return ret;
} }
PXBOX_PFN PhysicalMemory::GetPfnOfPT(PMMPTE pPte) PXBOX_PFN PhysicalMemory::GetPfnOfPT(xbox::PMMPTE pPte)
{ {
PXBOX_PFN PTpfn; PXBOX_PFN PTpfn;
// GetPteAddress on a pte address will yield the corresponding pde which maps the supplied pte // GetPteAddress on a pte address will yield the corresponding pde which maps the supplied pte
PMMPTE PointerPde = GetPteAddress(pPte); xbox::PMMPTE PointerPde = GetPteAddress(pPte);
// PointerPde should have already been written to by AllocatePT // PointerPde should have already been written to by AllocatePT
assert(PointerPde->Hardware.Valid != 0); assert(PointerPde->Hardware.Valid != 0);
if (m_MmLayoutRetail || m_MmLayoutDebug) { if (m_MmLayoutRetail || m_MmLayoutDebug) {
@ -751,8 +751,8 @@ PXBOX_PFN PhysicalMemory::GetPfnOfPT(PMMPTE pPte)
} }
else { PTpfn = CHIHIRO_PFN_ELEMENT(PointerPde->Hardware.PFN); } else { PTpfn = CHIHIRO_PFN_ELEMENT(PointerPde->Hardware.PFN); }
assert(PTpfn->PTPageFrame.Busy == 1); assert(PTpfn->PTPageFrame.Busy == 1);
assert(PTpfn->PTPageFrame.BusyType == SystemPageTableType || assert(PTpfn->PTPageFrame.BusyType == xbox::SystemPageTableType ||
PTpfn->PTPageFrame.BusyType == VirtualPageTableType); PTpfn->PTPageFrame.BusyType == xbox::VirtualPageTableType);
return PTpfn; return PTpfn;
} }

View File

@ -40,84 +40,37 @@
typedef uintptr_t VAddr; typedef uintptr_t VAddr;
typedef uintptr_t PAddr; typedef uintptr_t PAddr;
typedef uint32_t u32; typedef uint32_t u32;
typedef unsigned int PFN;
typedef unsigned int PFN_COUNT;
/* An entry of the list tracking the free pages on the system */ /* An entry of the list tracking the free pages on the system */
typedef struct _FreeBlock typedef struct _FreeBlock
{ {
PFN start; // starting page of the block xbox::PFN start; // starting page of the block
PFN_COUNT size; // number of pages in the block xbox::PFN_COUNT size; // number of pages in the block
xbox::LIST_ENTRY ListEntry; xbox::LIST_ENTRY ListEntry;
}FreeBlock, *PFreeBlock; }FreeBlock, *PFreeBlock;
/* The Xbox PTE, modelled around the Intel 386 PTE specification */
typedef struct _XBOX_PTE
{
ULONG Valid : 1;
ULONG Write : 1;
ULONG Owner : 1;
ULONG WriteThrough : 1;
ULONG CacheDisable : 1;
ULONG Accessed : 1;
ULONG Dirty : 1;
ULONG LargePage : 1;
ULONG Global : 1;
ULONG GuardOrEnd : 1; // software field used for our own purposes
ULONG Persist : 1; // software field used for our own purposes
ULONG Unused : 1; // software field used for our own purposes
ULONG PFN : 20;
} XBOX_PTE, *PXBOX_PTE;
/* PTE as used by the memory manager */
typedef union _MMPTE
{
ULONG Default;
XBOX_PTE Hardware;
}MMPTE, *PMMPTE;
/* PFN entry used by the memory manager */ /* PFN entry used by the memory manager */
typedef union _XBOX_PFN typedef union _XBOX_PFN
{ {
ULONG Default; xbox::ulong_xt Default;
struct { struct {
ULONG LockCount : 16; // Set to prevent page relocation. Used by MmLockUnlockPhysicalPage and others xbox::ulong_xt LockCount : 16; // Set to prevent page relocation. Used by MmLockUnlockPhysicalPage and others
ULONG Busy : 1; // If set, PFN is in use xbox::ulong_xt Busy : 1; // If set, PFN is in use
ULONG Unused : 1; xbox::ulong_xt Unused : 1;
ULONG PteIndex : 10; // Offset in the PT that maps the pte (it seems to be needed only for page relocations) xbox::ulong_xt PteIndex : 10; // Offset in the PT that maps the pte (it seems to be needed only for page relocations)
ULONG BusyType : 4; // What the page is used for xbox::ulong_xt BusyType : 4; // What the page is used for
} Busy; } Busy;
struct { struct {
ULONG LockCount : 16; // Set to prevent page relocation. Used by MmLockUnlockPhysicalPage and others xbox::ulong_xt LockCount : 16; // Set to prevent page relocation. Used by MmLockUnlockPhysicalPage and others
ULONG Busy : 1; // If set, PFN is in use xbox::ulong_xt Busy : 1; // If set, PFN is in use
ULONG PtesUsed : 11; // Number of used pte's in the PT pointed by the pde xbox::ulong_xt PtesUsed : 11; // Number of used pte's in the PT pointed by the pde
ULONG BusyType : 4; // What the page is used for (must be VirtualPageTableType or SystemPageTableType) xbox::ulong_xt BusyType : 4; // What the page is used for (must be VirtualPageTableType or SystemPageTableType)
} PTPageFrame; } PTPageFrame;
}XBOX_PFN, *PXBOX_PFN; }XBOX_PFN, *PXBOX_PFN;
/* enum describing the usage type of the memory pages */
typedef enum _PageType
{
UnknownType, // Used by the PFN database
StackType, // Used by MmCreateKernelStack
VirtualPageTableType, // Used by the pages holding the PTs that map the user memory (lower 2 GiB)
SystemPageTableType, // Used by the pages holding the PTs that map the system memory
PoolType, // Used by ExAllocatePoolWithTag
VirtualMemoryType, // Used by NtAllocateVirtualMemory
SystemMemoryType, // Used by MmAllocateSystemMemory
ImageType, // Used to mark executable memory
CacheType, // Used by the file cache related functions
ContiguousType, // Used by MmAllocateContiguousMemoryEx and others
DebuggerType, // xbdm-related
COUNTtype // The size of the array containing the page usage per type
}PageType;
/* enum describing the memory layouts the memory manager can use */ /* enum describing the memory layouts the memory manager can use */
typedef enum _MmLayout typedef enum _MmLayout
{ {
@ -155,8 +108,8 @@ typedef enum _MmLayout
/* Various macros to manipulate PDE/PTE/PFN */ /* Various macros to manipulate PDE/PTE/PFN */
#define GetPdeAddress(Va) ((PMMPTE)(((((ULONG)(Va)) >> 22) << 2) + PAGE_DIRECTORY_BASE)) // (Va/4M) * 4 + PDE_BASE #define GetPdeAddress(Va) ((xbox::PMMPTE)(((((ULONG)(Va)) >> 22) << 2) + PAGE_DIRECTORY_BASE)) // (Va/4M) * 4 + PDE_BASE
#define GetPteAddress(Va) ((PMMPTE)(((((ULONG)(Va)) >> 12) << 2) + PAGE_TABLES_BASE)) // (Va/4K) * 4 + PTE_BASE #define GetPteAddress(Va) ((xbox::PMMPTE)(((((ULONG)(Va)) >> 12) << 2) + PAGE_TABLES_BASE)) // (Va/4K) * 4 + PTE_BASE
#define GetVAddrMappedByPte(Pte) ((VAddr)((ULONG_PTR)(Pte) << 10)) #define GetVAddrMappedByPte(Pte) ((VAddr)((ULONG_PTR)(Pte) << 10))
#define GetPteOffset(Va) ((((ULONG)(Va)) << 10) >> 22) #define GetPteOffset(Va) ((((ULONG)(Va)) << 10) >> 22)
#define IsPteOnPdeBoundary(Pte) (((ULONG_PTR)(Pte) & (PAGE_SIZE - 1)) == 0) #define IsPteOnPdeBoundary(Pte) (((ULONG_PTR)(Pte) & (PAGE_SIZE - 1)) == 0)
@ -201,15 +154,15 @@ class PhysicalMemory
// highest pfn available for contiguous allocations // highest pfn available for contiguous allocations
PAddr m_MaxContiguousPfn = XBOX_CONTIGUOUS_MEMORY_LIMIT; PAddr m_MaxContiguousPfn = XBOX_CONTIGUOUS_MEMORY_LIMIT;
// amount of free physical pages available for non-debugger usage // amount of free physical pages available for non-debugger usage
PFN_COUNT m_PhysicalPagesAvailable = X64M_PHYSICAL_PAGE; xbox::PFN_COUNT m_PhysicalPagesAvailable = X64M_PHYSICAL_PAGE;
// amount of free physical pages available for usage by the debugger (devkit only) // amount of free physical pages available for usage by the debugger (devkit only)
PFN_COUNT m_DebuggerPagesAvailable = 0; xbox::PFN_COUNT m_DebuggerPagesAvailable = 0;
// array containing the number of pages in use per type // array containing the number of pages in use per type
PFN_COUNT m_PagesByUsage[COUNTtype] = { 0 }; xbox::PFN_COUNT m_PagesByUsage[xbox::COUNTtype] = { 0 };
// highest page on the system // highest page on the system
PFN m_HighestPage = XBOX_HIGHEST_PHYSICAL_PAGE; xbox::PFN m_HighestPage = XBOX_HIGHEST_PHYSICAL_PAGE;
// first page of the nv2a instance memory // first page of the nv2a instance memory
PFN m_NV2AInstancePage = XBOX_INSTANCE_PHYSICAL_PAGE; xbox::PFN m_NV2AInstancePage = XBOX_INSTANCE_PHYSICAL_PAGE;
// number of allocated bytes for the nv2a instance memory // number of allocated bytes for the nv2a instance memory
size_t m_NV2AInstanceMemoryBytes = NV2A_INSTANCE_PAGE_COUNT << PAGE_SHIFT; size_t m_NV2AInstanceMemoryBytes = NV2A_INSTANCE_PAGE_COUNT << PAGE_SHIFT;
// boolean that indicates that the extra 64 MiB on a devkit can be used for heap/Nt allocations // boolean that indicates that the extra 64 MiB on a devkit can be used for heap/Nt allocations
@ -227,19 +180,19 @@ class PhysicalMemory
// set up the page directory // set up the page directory
void InitializePageDirectory(); void InitializePageDirectory();
// write a contiguous range of pfn's // write a contiguous range of pfn's
void WritePfn(PFN pfn_start, PFN pfn_end, PMMPTE pPte, PageType BusyType, bool bZero = false); void WritePfn(xbox::PFN pfn_start, xbox::PFN pfn_end, xbox::PMMPTE pPte, xbox::PageType BusyType, bool bZero = false);
// write a contiguous range of pte's // write a contiguous range of pte's
void WritePte(PMMPTE pPteStart, PMMPTE pPteEnd, MMPTE Pte, PFN pfn, bool bZero = false); void WritePte(xbox::PMMPTE pPteStart, xbox::PMMPTE pPteEnd, xbox::MMPTE Pte, xbox::PFN pfn, bool bZero = false);
// retrieves the pfn entry which maps a PT // retrieves the pfn entry which maps a PT
PXBOX_PFN GetPfnOfPT(PMMPTE pPte); PXBOX_PFN GetPfnOfPT(xbox::PMMPTE pPte);
// commit a contiguous number of pages // commit a contiguous number of pages
bool RemoveFree(PFN_COUNT NumberOfPages, PFN* result, PFN_COUNT PfnAlignment, PFN start, PFN end); bool RemoveFree(xbox::PFN_COUNT NumberOfPages, xbox::PFN* result, xbox::PFN_COUNT PfnAlignment, xbox::PFN start, xbox::PFN end);
// release a contiguous number of pages // release a contiguous number of pages
void InsertFree(PFN start, PFN end); void InsertFree(xbox::PFN start, xbox::PFN end);
// convert from Xbox to the desired system pte protection (if possible) and return it // convert from Xbox to the desired system pte protection (if possible) and return it
bool ConvertXboxToSystemPtePermissions(DWORD perms, PMMPTE pPte); bool ConvertXboxToSystemPtePermissions(DWORD perms, xbox::PMMPTE pPte);
// convert from Xbox to non-system pte protection (if possible) and return it // convert from Xbox to non-system pte protection (if possible) and return it
bool ConvertXboxToPtePermissions(DWORD perms, PMMPTE pPte); bool ConvertXboxToPtePermissions(DWORD perms, xbox::PMMPTE pPte);
// convert from pte permissions to the corresponding Xbox protection code // convert from pte permissions to the corresponding Xbox protection code
DWORD ConvertPteToXboxPermissions(ULONG PteMask); DWORD ConvertPteToXboxPermissions(ULONG PteMask);
// convert from Xbox to Windows permissions // convert from Xbox to Windows permissions
@ -251,7 +204,7 @@ class PhysicalMemory
// deallocate page tables (if possible) // deallocate page tables (if possible)
void DeallocatePT(size_t Size, VAddr addr); void DeallocatePT(size_t Size, VAddr addr);
// checks if enough free pages are available for the allocation (doesn't account for fragmentation) // checks if enough free pages are available for the allocation (doesn't account for fragmentation)
bool IsMappable(PFN_COUNT PagesRequested, bool bRetailRegion, bool bDebugRegion); bool IsMappable(xbox::PFN_COUNT PagesRequested, bool bRetailRegion, bool bDebugRegion);
}; };
#endif #endif

View File

@ -95,7 +95,7 @@ VAddr PoolManager::AllocatePool(size_t Size, uint32_t Tag)
Lock(); Lock();
PoolDesc->RunningAllocs += 1; PoolDesc->RunningAllocs += 1;
Entry = reinterpret_cast<PPOOL_HEADER>(g_VMManager.AllocateSystemMemory(PoolType, XBOX_PAGE_READWRITE, Size, false)); Entry = reinterpret_cast<PPOOL_HEADER>(g_VMManager.AllocateSystemMemory(xbox::PoolType, XBOX_PAGE_READWRITE, Size, false));
if (Entry != nullptr) { if (Entry != nullptr) {
NumberOfPages = ROUND_UP_4K(Size) >> PAGE_SHIFT; NumberOfPages = ROUND_UP_4K(Size) >> PAGE_SHIFT;
@ -202,7 +202,7 @@ VAddr PoolManager::AllocatePool(size_t Size, uint32_t Tag)
// The current pool descriptor is exhausted, so we ask the VMManager to allocate a page to create a new one // The current pool descriptor is exhausted, so we ask the VMManager to allocate a page to create a new one
Entry = reinterpret_cast<PPOOL_HEADER>(g_VMManager.AllocateSystemMemory(PoolType, XBOX_PAGE_READWRITE, PAGE_SIZE, false)); Entry = reinterpret_cast<PPOOL_HEADER>(g_VMManager.AllocateSystemMemory(xbox::PoolType, XBOX_PAGE_READWRITE, PAGE_SIZE, false));
if (Entry == nullptr) { if (Entry == nullptr) {
EmuLog(LOG_LEVEL::WARNING, "AllocatePool returns nullptr"); EmuLog(LOG_LEVEL::WARNING, "AllocatePool returns nullptr");
@ -245,7 +245,7 @@ void PoolManager::DeallocatePool(VAddr addr)
PoolDesc->RunningDeAllocs += 1; PoolDesc->RunningDeAllocs += 1;
BigPages = g_VMManager.DeallocateSystemMemory(PoolType, addr, 0); BigPages = g_VMManager.DeallocateSystemMemory(xbox::PoolType, addr, 0);
PoolDesc->TotalBigPages -= BigPages; PoolDesc->TotalBigPages -= BigPages;
@ -306,7 +306,7 @@ void PoolManager::DeallocatePool(VAddr addr)
if (CHECK_ALIGNMENT(reinterpret_cast<VAddr>(Entry), PAGE_SIZE) && if (CHECK_ALIGNMENT(reinterpret_cast<VAddr>(Entry), PAGE_SIZE) &&
(PAGE_END(reinterpret_cast<PPOOL_BLOCK>(Entry) + Entry->BlockSize) != false)) { (PAGE_END(reinterpret_cast<PPOOL_BLOCK>(Entry) + Entry->BlockSize) != false)) {
g_VMManager.DeallocateSystemMemory(PoolType, reinterpret_cast<VAddr>(Entry), 0); g_VMManager.DeallocateSystemMemory(xbox::PoolType, reinterpret_cast<VAddr>(Entry), 0);
PoolDesc->TotalPages -= 1; PoolDesc->TotalPages -= 1;
} }

View File

@ -207,10 +207,10 @@ void VMManager::DestroyMemoryRegions()
void VMManager::InitializeSystemAllocations() void VMManager::InitializeSystemAllocations()
{ {
PFN pfn; xbox::PFN pfn;
PFN pfn_end; xbox::PFN pfn_end;
PMMPTE PointerPte; xbox::PMMPTE PointerPte;
PMMPTE EndingPte; xbox::PMMPTE EndingPte;
VAddr addr; VAddr addr;
@ -248,7 +248,7 @@ void VMManager::InitializeSystemAllocations()
} }
addr = (VAddr)CONVERT_PFN_TO_CONTIGUOUS_PHYSICAL(pfn); addr = (VAddr)CONVERT_PFN_TO_CONTIGUOUS_PHYSICAL(pfn);
AllocateContiguousMemoryInternal(pfn_end - pfn + 1, pfn, pfn_end, 1, XBOX_PAGE_READWRITE, UnknownType); AllocateContiguousMemoryInternal(pfn_end - pfn + 1, pfn, pfn_end, 1, XBOX_PAGE_READWRITE, xbox::UnknownType);
PersistMemory(addr, (pfn_end - pfn + 1) << PAGE_SHIFT, true); PersistMemory(addr, (pfn_end - pfn + 1) << PAGE_SHIFT, true);
if (m_MmLayoutDebug) { m_PhysicalPagesAvailable += 16; m_DebuggerPagesAvailable -= 16; } if (m_MmLayoutDebug) { m_PhysicalPagesAvailable += 16; m_DebuggerPagesAvailable -= 16; }
@ -328,8 +328,8 @@ void VMManager::RestorePersistentMemory()
EmuLog(LOG_LEVEL::INFO, "Restored Framebuffer\n"); EmuLog(LOG_LEVEL::INFO, "Restored Framebuffer\n");
} }
MMPTE pte; xbox::MMPTE pte;
PFN pfn; xbox::PFN pfn;
size_t pfn_num_pages; size_t pfn_num_pages;
uint32_t *pfn_addr; uint32_t *pfn_addr;
if (m_MmLayoutRetail) { if (m_MmLayoutRetail) {
@ -348,7 +348,7 @@ void VMManager::RestorePersistentMemory()
for (unsigned int i = 0; i < persisted_mem->NumOfPtes; i++) { for (unsigned int i = 0; i < persisted_mem->NumOfPtes; i++) {
pte.Default = persisted_mem->Data[persisted_mem->NumOfPtes + i]; pte.Default = persisted_mem->Data[persisted_mem->NumOfPtes + i];
assert(pte.Hardware.Valid != 0 && pte.Hardware.Persist != 0); assert(pte.Hardware.Valid != 0 && pte.Hardware.Persist != 0);
memcpy(GetPteAddress(persisted_mem->Data[i]), &pte.Default, sizeof(MMPTE)); memcpy(GetPteAddress(persisted_mem->Data[i]), &pte.Default, sizeof(xbox::MMPTE));
RemoveFree(1, &pfn, 0, pte.Hardware.PFN, pte.Hardware.PFN); RemoveFree(1, &pfn, 0, pte.Hardware.PFN, pte.Hardware.PFN);
PXBOX_PFN temp_pfn = &((PXBOX_PFN)&persisted_mem->Data[(persisted_mem->NumOfPtes * 2) + (persisted_mem->NumOfPtes - pfn_num_pages) * KiB(1)])[pte.Hardware.PFN]; PXBOX_PFN temp_pfn = &((PXBOX_PFN)&persisted_mem->Data[(persisted_mem->NumOfPtes * 2) + (persisted_mem->NumOfPtes - pfn_num_pages) * KiB(1)])[pte.Hardware.PFN];
m_PagesByUsage[temp_pfn->Busy.BusyType]++; m_PagesByUsage[temp_pfn->Busy.BusyType]++;
@ -365,7 +365,7 @@ void VMManager::RestorePersistentMemory()
} }
} }
PFN_COUNT pages_num = 1; xbox::PFN_COUNT pages_num = 1;
for (unsigned int i = 0; i < persisted_mem->NumOfPtes; i++) { for (unsigned int i = 0; i < persisted_mem->NumOfPtes; i++) {
pte.Default = persisted_mem->Data[persisted_mem->NumOfPtes + i]; pte.Default = persisted_mem->Data[persisted_mem->NumOfPtes + i];
if (pte.Hardware.GuardOrEnd == 0) { if (pte.Hardware.GuardOrEnd == 0) {
@ -382,7 +382,7 @@ void VMManager::RestorePersistentMemory()
if (m_MmLayoutDebug) { m_PhysicalPagesAvailable += 16; m_DebuggerPagesAvailable -= 16; } if (m_MmLayoutDebug) { m_PhysicalPagesAvailable += 16; m_DebuggerPagesAvailable -= 16; }
PFN pfn_end; xbox::PFN pfn_end;
if (m_MmLayoutRetail || m_MmLayoutDebug) { if (m_MmLayoutRetail || m_MmLayoutDebug) {
pfn = XBOX_INSTANCE_PHYSICAL_PAGE; pfn = XBOX_INSTANCE_PHYSICAL_PAGE;
pfn_end = XBOX_INSTANCE_PHYSICAL_PAGE + NV2A_INSTANCE_PAGE_COUNT - 1; pfn_end = XBOX_INSTANCE_PHYSICAL_PAGE + NV2A_INSTANCE_PAGE_COUNT - 1;
@ -392,8 +392,8 @@ void VMManager::RestorePersistentMemory()
pfn_end = CHIHIRO_INSTANCE_PHYSICAL_PAGE + NV2A_INSTANCE_PAGE_COUNT - 1; pfn_end = CHIHIRO_INSTANCE_PHYSICAL_PAGE + NV2A_INSTANCE_PAGE_COUNT - 1;
} }
VAddr addr = (VAddr)CONVERT_PFN_TO_CONTIGUOUS_PHYSICAL(pfn); VAddr addr = (VAddr)CONVERT_PFN_TO_CONTIGUOUS_PHYSICAL(pfn);
PMMPTE PointerPte = GetPteAddress(addr); xbox::PMMPTE PointerPte = GetPteAddress(addr);
PMMPTE EndingPte = GetPteAddress(CONVERT_PFN_TO_CONTIGUOUS_PHYSICAL(pfn_end)); xbox::PMMPTE EndingPte = GetPteAddress(CONVERT_PFN_TO_CONTIGUOUS_PHYSICAL(pfn_end));
AllocateContiguousMemoryInternal(pfn_end - pfn + 1, pfn, pfn_end, 1, XBOX_PAGE_READWRITE); AllocateContiguousMemoryInternal(pfn_end - pfn + 1, pfn, pfn_end, 1, XBOX_PAGE_READWRITE);
while (PointerPte <= EndingPte) { while (PointerPte <= EndingPte) {
@ -427,10 +427,10 @@ void VMManager::SavePersistentMemory()
{ {
PersistedMemory* persisted_mem; PersistedMemory* persisted_mem;
size_t num_persisted_ptes; size_t num_persisted_ptes;
std::vector<PMMPTE> cached_persisted_ptes; std::vector<xbox::PMMPTE> cached_persisted_ptes;
LPVOID addr; LPVOID addr;
PMMPTE PointerPte; xbox::PMMPTE PointerPte;
PMMPTE EndingPte; xbox::PMMPTE EndingPte;
int i; int i;
Lock(); Lock();
@ -493,7 +493,7 @@ void VMManager::SavePersistentMemory()
Unlock(); Unlock();
} }
VAddr VMManager::DbgTestPte(VAddr addr, PMMPTE Pte, bool bWriteCheck) VAddr VMManager::DbgTestPte(VAddr addr, xbox::PMMPTE Pte, bool bWriteCheck)
{ {
LOG_FUNC_BEGIN LOG_FUNC_BEGIN
LOG_FUNC_ARG(addr) LOG_FUNC_ARG(addr)
@ -501,7 +501,7 @@ VAddr VMManager::DbgTestPte(VAddr addr, PMMPTE Pte, bool bWriteCheck)
LOG_FUNC_ARG(bWriteCheck) LOG_FUNC_ARG(bWriteCheck)
LOG_FUNC_END; LOG_FUNC_END;
PMMPTE PointerPte; xbox::PMMPTE PointerPte;
VAddr ret = 0; VAddr ret = 0;
Lock(); Lock();
@ -516,7 +516,7 @@ VAddr VMManager::DbgTestPte(VAddr addr, PMMPTE Pte, bool bWriteCheck)
if (PointerPte->Hardware.Write == 0) if (PointerPte->Hardware.Write == 0)
{ {
MMPTE TempPte = *PointerPte; xbox::MMPTE TempPte = *PointerPte;
*Pte = TempPte; *Pte = TempPte;
TempPte.Hardware.Write = 1; TempPte.Hardware.Write = 1;
WRITE_PTE(PointerPte, TempPte); WRITE_PTE(PointerPte, TempPte);
@ -538,7 +538,7 @@ VAddr VMManager::DbgTestPte(VAddr addr, PMMPTE Pte, bool bWriteCheck)
RETURN(ret); RETURN(ret);
} }
PFN_COUNT VMManager::QueryNumberOfFreeDebuggerPages() xbox::PFN_COUNT VMManager::QueryNumberOfFreeDebuggerPages()
{ {
return m_DebuggerPagesAvailable; return m_DebuggerPagesAvailable;
} }
@ -550,13 +550,13 @@ void VMManager::MemoryStatistics(xbox::PMM_STATISTICS memory_statistics)
memory_statistics->TotalPhysicalPages = g_SystemMaxMemory >> PAGE_SHIFT; memory_statistics->TotalPhysicalPages = g_SystemMaxMemory >> PAGE_SHIFT;
memory_statistics->AvailablePages = m_MmLayoutDebug && m_bAllowNonDebuggerOnTop64MiB ? memory_statistics->AvailablePages = m_MmLayoutDebug && m_bAllowNonDebuggerOnTop64MiB ?
m_PhysicalPagesAvailable + m_DebuggerPagesAvailable : m_PhysicalPagesAvailable; m_PhysicalPagesAvailable + m_DebuggerPagesAvailable : m_PhysicalPagesAvailable;
memory_statistics->VirtualMemoryBytesCommitted = (m_PagesByUsage[VirtualMemoryType] + memory_statistics->VirtualMemoryBytesCommitted = (m_PagesByUsage[xbox::VirtualMemoryType] +
m_PagesByUsage[ImageType]) << PAGE_SHIFT; m_PagesByUsage[xbox::ImageType]) << PAGE_SHIFT;
memory_statistics->VirtualMemoryBytesReserved = m_VirtualMemoryBytesReserved; memory_statistics->VirtualMemoryBytesReserved = m_VirtualMemoryBytesReserved;
memory_statistics->CachePagesCommitted = m_PagesByUsage[CacheType]; memory_statistics->CachePagesCommitted = m_PagesByUsage[xbox::CacheType];
memory_statistics->PoolPagesCommitted = m_PagesByUsage[PoolType]; memory_statistics->PoolPagesCommitted = m_PagesByUsage[xbox::PoolType];
memory_statistics->StackPagesCommitted = m_PagesByUsage[StackType]; memory_statistics->StackPagesCommitted = m_PagesByUsage[xbox::StackType];
memory_statistics->ImagePagesCommitted = m_PagesByUsage[ImageType]; memory_statistics->ImagePagesCommitted = m_PagesByUsage[xbox::ImageType];
Unlock(); Unlock();
} }
@ -579,10 +579,10 @@ VAddr VMManager::ClaimGpuMemory(size_t Size, size_t* BytesToSkip)
if (Size != MAXULONG_PTR) if (Size != MAXULONG_PTR)
{ {
PFN pfn; xbox::PFN pfn;
PFN EndingPfn; xbox::PFN EndingPfn;
PMMPTE PointerPte; xbox::PMMPTE PointerPte;
PMMPTE EndingPte; xbox::PMMPTE EndingPte;
// Actually deallocate the requested number of instance pages. Note that we can't just call DeallocateContiguous // Actually deallocate the requested number of instance pages. Note that we can't just call DeallocateContiguous
// since that function will always deallocate the entire original allocation // since that function will always deallocate the entire original allocation
@ -601,7 +601,7 @@ VAddr VMManager::ClaimGpuMemory(size_t Size, size_t* BytesToSkip)
EndingPte = GetPteAddress(CONVERT_PFN_TO_CONTIGUOUS_PHYSICAL(EndingPfn)); EndingPte = GetPteAddress(CONVERT_PFN_TO_CONTIGUOUS_PHYSICAL(EndingPfn));
WritePte(PointerPte, EndingPte, *PointerPte, 0, true); WritePte(PointerPte, EndingPte, *PointerPte, 0, true);
WritePfn(pfn, EndingPfn, PointerPte, ContiguousType, true); WritePfn(pfn, EndingPfn, PointerPte, xbox::ContiguousType, true);
InsertFree(pfn, EndingPfn); InsertFree(pfn, EndingPfn);
DestructVMA((VAddr)CONVERT_PFN_TO_CONTIGUOUS_PHYSICAL(pfn), ContiguousRegion, m_NV2AInstanceMemoryBytes - Size); DestructVMA((VAddr)CONVERT_PFN_TO_CONTIGUOUS_PHYSICAL(pfn), ContiguousRegion, m_NV2AInstanceMemoryBytes - Size);
@ -616,7 +616,7 @@ VAddr VMManager::ClaimGpuMemory(size_t Size, size_t* BytesToSkip)
EndingPte = GetPteAddress(CONVERT_PFN_TO_CONTIGUOUS_PHYSICAL(EndingPfn)); EndingPte = GetPteAddress(CONVERT_PFN_TO_CONTIGUOUS_PHYSICAL(EndingPfn));
WritePte(PointerPte, EndingPte, *PointerPte, 0, true); WritePte(PointerPte, EndingPte, *PointerPte, 0, true);
WritePfn(pfn, EndingPfn, PointerPte, ContiguousType, true); WritePfn(pfn, EndingPfn, PointerPte, xbox::ContiguousType, true);
InsertFree(pfn, EndingPfn); InsertFree(pfn, EndingPfn);
DestructVMA((VAddr)CONVERT_PFN_TO_CONTIGUOUS_PHYSICAL(pfn), ContiguousRegion, m_NV2AInstanceMemoryBytes - Size); DestructVMA((VAddr)CONVERT_PFN_TO_CONTIGUOUS_PHYSICAL(pfn), ContiguousRegion, m_NV2AInstanceMemoryBytes - Size);
} }
@ -639,8 +639,8 @@ void VMManager::PersistMemory(VAddr addr, size_t Size, bool bPersist)
LOG_FUNC_ARG(bPersist) LOG_FUNC_ARG(bPersist)
LOG_FUNC_END; LOG_FUNC_END;
PMMPTE PointerPte; xbox::PMMPTE PointerPte;
PMMPTE EndingPte; xbox::PMMPTE EndingPte;
assert(IS_PHYSICAL_ADDRESS(addr)); // only contiguous memory can be made persistent assert(IS_PHYSICAL_ADDRESS(addr)); // only contiguous memory can be made persistent
@ -667,7 +667,7 @@ void VMManager::PersistMemory(VAddr addr, size_t Size, bool bPersist)
Unlock(); Unlock();
} }
VAddr VMManager::AllocateSystemMemory(PageType BusyType, DWORD Perms, size_t Size, bool bAddGuardPage) VAddr VMManager::AllocateSystemMemory(xbox::PageType BusyType, DWORD Perms, size_t Size, bool bAddGuardPage)
{ {
LOG_FUNC_BEGIN LOG_FUNC_BEGIN
LOG_FUNC_ARG(BusyType) LOG_FUNC_ARG(BusyType)
@ -676,14 +676,14 @@ VAddr VMManager::AllocateSystemMemory(PageType BusyType, DWORD Perms, size_t Siz
LOG_FUNC_ARG(bAddGuardPage) LOG_FUNC_ARG(bAddGuardPage)
LOG_FUNC_END; LOG_FUNC_END;
MMPTE TempPte; xbox::MMPTE TempPte;
PMMPTE PointerPte; xbox::PMMPTE PointerPte;
PMMPTE EndingPte; xbox::PMMPTE EndingPte;
PFN pfn; xbox::PFN pfn;
PFN LowestAcceptablePfn; xbox::PFN LowestAcceptablePfn;
PFN HighestAcceptablePfn; xbox::PFN HighestAcceptablePfn;
PFN_COUNT PteNumber; xbox::PFN_COUNT PteNumber;
PFN_COUNT PagesNumber; xbox::PFN_COUNT PagesNumber;
VAddr addr; VAddr addr;
MemoryRegionType MemoryType; MemoryRegionType MemoryType;
@ -702,7 +702,7 @@ VAddr VMManager::AllocateSystemMemory(PageType BusyType, DWORD Perms, size_t Siz
PagesNumber = PteNumber; PagesNumber = PteNumber;
if (bAddGuardPage) { PteNumber++; } if (bAddGuardPage) { PteNumber++; }
if (BusyType == DebuggerType) if (BusyType == xbox::DebuggerType)
{ {
// Debugger pages are only allocated from the extra 64 MiB available on devkits and are mapped in the // Debugger pages are only allocated from the extra 64 MiB available on devkits and are mapped in the
// devkit system region // devkit system region
@ -770,10 +770,10 @@ VAddr VMManager::AllocateContiguousMemory(size_t Size, PAddr LowestAddress, PAdd
LOG_FUNC_ARG(Perms) LOG_FUNC_ARG(Perms)
LOG_FUNC_END; LOG_FUNC_END;
PFN PfnAlignment; xbox::PFN PfnAlignment;
PFN LowestPfn; xbox::PFN LowestPfn;
PFN HighestPfn; xbox::PFN HighestPfn;
PFN_COUNT PteNumber; xbox::PFN_COUNT PteNumber;
VAddr Addr; VAddr Addr;
if (!Size) { RETURN(NULL); } if (!Size) { RETURN(NULL); }
@ -801,13 +801,13 @@ VAddr VMManager::AllocateContiguousMemory(size_t Size, PAddr LowestAddress, PAdd
RETURN(Addr); RETURN(Addr);
} }
VAddr VMManager::AllocateContiguousMemoryInternal(PFN_COUNT NumberOfPages, PFN LowestPfn, PFN HighestPfn, PFN PfnAlignment, DWORD Perms, PageType BusyType) VAddr VMManager::AllocateContiguousMemoryInternal(xbox::PFN_COUNT NumberOfPages, xbox::PFN LowestPfn, xbox::PFN HighestPfn, xbox::PFN PfnAlignment, DWORD Perms, xbox::PageType BusyType)
{ {
MMPTE TempPte; xbox::MMPTE TempPte;
PMMPTE PointerPte; xbox::PMMPTE PointerPte;
PMMPTE EndingPte; xbox::PMMPTE EndingPte;
PFN pfn; xbox::PFN pfn;
PFN EndingPfn; xbox::PFN EndingPfn;
VAddr addr; VAddr addr;
if (!ConvertXboxToSystemPtePermissions(Perms, &TempPte)) { goto Fail; } if (!ConvertXboxToSystemPtePermissions(Perms, &TempPte)) { goto Fail; }
@ -850,11 +850,11 @@ VAddr VMManager::MapDeviceMemory(PAddr Paddr, size_t Size, DWORD Perms)
LOG_FUNC_ARG(Perms) LOG_FUNC_ARG(Perms)
LOG_FUNC_END; LOG_FUNC_END;
MMPTE TempPte; xbox::MMPTE TempPte;
PMMPTE PointerPte; xbox::PMMPTE PointerPte;
PMMPTE EndingPte; xbox::PMMPTE EndingPte;
PFN pfn; xbox::PFN pfn;
PFN_COUNT PteNumber; xbox::PFN_COUNT PteNumber;
VAddr addr; VAddr addr;
if (!Size || !ConvertXboxToSystemPtePermissions(Perms, &TempPte)) { RETURN(NULL); } if (!Size || !ConvertXboxToSystemPtePermissions(Perms, &TempPte)) { RETURN(NULL); }
@ -908,10 +908,10 @@ void VMManager::DeallocateContiguousMemory(VAddr addr)
LOG_FUNC_ARG(addr) LOG_FUNC_ARG(addr)
LOG_FUNC_END; LOG_FUNC_END;
PMMPTE StartingPte; xbox::PMMPTE StartingPte;
PMMPTE EndingPte; xbox::PMMPTE EndingPte;
PFN pfn; xbox::PFN pfn;
PFN EndingPfn; xbox::PFN EndingPfn;
VMAIter it; VMAIter it;
bool bOverflow; bool bOverflow;
@ -935,7 +935,7 @@ void VMManager::DeallocateContiguousMemory(VAddr addr)
EndingPfn = pfn + (EndingPte - StartingPte); EndingPfn = pfn + (EndingPte - StartingPte);
InsertFree(pfn, EndingPfn); InsertFree(pfn, EndingPfn);
WritePfn(pfn, EndingPfn, StartingPte, ContiguousType, true); WritePfn(pfn, EndingPfn, StartingPte, xbox::ContiguousType, true);
WritePte(StartingPte, EndingPte, *StartingPte, 0, true); WritePte(StartingPte, EndingPte, *StartingPte, 0, true);
DestructVMA(it->first, ContiguousRegion, it->second.size); DestructVMA(it->first, ContiguousRegion, it->second.size);
DeallocatePT((EndingPte - StartingPte + 1) << PAGE_SHIFT, addr); DeallocatePT((EndingPte - StartingPte + 1) << PAGE_SHIFT, addr);
@ -943,18 +943,18 @@ void VMManager::DeallocateContiguousMemory(VAddr addr)
Unlock(); Unlock();
} }
PFN_COUNT VMManager::DeallocateSystemMemory(PageType BusyType, VAddr addr, size_t Size) xbox::PFN_COUNT VMManager::DeallocateSystemMemory(xbox::PageType BusyType, VAddr addr, size_t Size)
{ {
LOG_FUNC_BEGIN LOG_FUNC_BEGIN
LOG_FUNC_ARG(addr) LOG_FUNC_ARG(addr)
LOG_FUNC_ARG(Size) LOG_FUNC_ARG(Size)
LOG_FUNC_END; LOG_FUNC_END;
PMMPTE PointerPte; xbox::PMMPTE PointerPte;
PMMPTE StartingPte; xbox::PMMPTE StartingPte;
PMMPTE EndingPte; xbox::PMMPTE EndingPte;
PFN pfn; xbox::PFN pfn;
PFN_COUNT PteNumber; xbox::PFN_COUNT PteNumber;
VMAIter it; VMAIter it;
MemoryRegionType MemoryType = SystemRegion; MemoryRegionType MemoryType = SystemRegion;
bool bGuardPageAdded = false; bool bGuardPageAdded = false;
@ -964,7 +964,7 @@ PFN_COUNT VMManager::DeallocateSystemMemory(PageType BusyType, VAddr addr, size_
Lock(); Lock();
if (BusyType == DebuggerType) if (BusyType == xbox::DebuggerType)
{ {
assert(IS_DEVKIT_ADDRESS(addr)); assert(IS_DEVKIT_ADDRESS(addr));
MemoryType = DevkitRegion; MemoryType = DevkitRegion;
@ -1005,7 +1005,7 @@ PFN_COUNT VMManager::DeallocateSystemMemory(PageType BusyType, VAddr addr, size_
} }
WritePte(StartingPte, EndingPte, *StartingPte, 0, true); WritePte(StartingPte, EndingPte, *StartingPte, 0, true);
DestructVMA(BusyType == DebuggerType ? addr : it->first, MemoryType, bGuardPageAdded ? Size + PAGE_SIZE : Size); DestructVMA(BusyType == xbox::DebuggerType ? addr : it->first, MemoryType, bGuardPageAdded ? Size + PAGE_SIZE : Size);
DeallocatePT(bGuardPageAdded ? Size + PAGE_SIZE : Size, addr); DeallocatePT(bGuardPageAdded ? Size + PAGE_SIZE : Size, addr);
Unlock(); Unlock();
@ -1023,9 +1023,9 @@ void VMManager::UnmapDeviceMemory(VAddr addr, size_t Size)
{ {
// The allocation is inside the system region, so it must have been mapped by us. Unmap it // The allocation is inside the system region, so it must have been mapped by us. Unmap it
PMMPTE StartingPte; xbox::PMMPTE StartingPte;
PMMPTE EndingPte; xbox::PMMPTE EndingPte;
PFN_COUNT PteNumber; xbox::PFN_COUNT PteNumber;
VMAIter it; VMAIter it;
bool bOverflow; bool bOverflow;
@ -1065,10 +1065,10 @@ void VMManager::Protect(VAddr addr, size_t Size, DWORD NewPerms)
LOG_FUNC_ARG(NewPerms) LOG_FUNC_ARG(NewPerms)
LOG_FUNC_END; LOG_FUNC_END;
MMPTE TempPte; xbox::MMPTE TempPte;
MMPTE NewPermsPte; xbox::MMPTE NewPermsPte;
PMMPTE PointerPte; xbox::PMMPTE PointerPte;
PMMPTE EndingPte; xbox::PMMPTE EndingPte;
assert(IS_PHYSICAL_ADDRESS(addr) || IS_SYSTEM_ADDRESS(addr)); assert(IS_PHYSICAL_ADDRESS(addr) || IS_SYSTEM_ADDRESS(addr));
@ -1102,8 +1102,8 @@ DWORD VMManager::QueryProtection(VAddr addr)
{ {
LOG_FUNC_ONE_ARG(addr); LOG_FUNC_ONE_ARG(addr);
PMMPTE PointerPte; xbox::PMMPTE PointerPte;
MMPTE TempPte; xbox::MMPTE TempPte;
DWORD Protect; DWORD Protect;
// This function can query any virtual address, even invalid ones, so we won't do any vma checks here // This function can query any virtual address, even invalid ones, so we won't do any vma checks here
@ -1148,8 +1148,8 @@ size_t VMManager::QuerySize(VAddr addr, bool bCxbxCaller)
{ {
LOG_FUNC_ONE_ARG(addr); LOG_FUNC_ONE_ARG(addr);
PMMPTE PointerPte; xbox::PMMPTE PointerPte;
PFN_COUNT PagesNumber; xbox::PFN_COUNT PagesNumber;
size_t Size = 0; size_t Size = 0;
Lock(); Lock();
@ -1211,9 +1211,9 @@ void VMManager::LockBufferOrSinglePage(PAddr paddr, VAddr addr, size_t Size, boo
LOG_FUNC_ARG(bUnLock) LOG_FUNC_ARG(bUnLock)
LOG_FUNC_END; LOG_FUNC_END;
PMMPTE PointerPte; xbox::PMMPTE PointerPte;
PMMPTE EndingPte; xbox::PMMPTE EndingPte;
PFN pfn; xbox::PFN pfn;
PXBOX_PFN PfnEntry; PXBOX_PFN PfnEntry;
ULONG LockUnit; ULONG LockUnit;
@ -1257,7 +1257,7 @@ void VMManager::LockBufferOrSinglePage(PAddr paddr, VAddr addr, size_t Size, boo
} }
else { PfnEntry = CHIHIRO_PFN_ELEMENT(pfn); } else { PfnEntry = CHIHIRO_PFN_ELEMENT(pfn); }
if (PfnEntry->Busy.BusyType != ContiguousType && pfn <= m_HighestPage) if (PfnEntry->Busy.BusyType != xbox::ContiguousType && pfn <= m_HighestPage)
{ {
LockUnit = bUnLock ? -LOCK_COUNT_UNIT : LOCK_COUNT_UNIT; LockUnit = bUnLock ? -LOCK_COUNT_UNIT : LOCK_COUNT_UNIT;
@ -1280,13 +1280,13 @@ xbox::ntstatus_xt VMManager::XbAllocateVirtualMemory(VAddr* addr, ULONG ZeroBits
LOG_FUNC_ARG(Protect) LOG_FUNC_ARG(Protect)
LOG_FUNC_END; LOG_FUNC_END;
MMPTE TempPte; xbox::MMPTE TempPte;
PMMPTE PointerPte; xbox::PMMPTE PointerPte;
PMMPTE EndingPte; xbox::PMMPTE EndingPte;
PMMPTE StartingPte; xbox::PMMPTE StartingPte;
PFN_COUNT PteNumber = 0; xbox::PFN_COUNT PteNumber = 0;
PFN TempPfn; xbox::PFN TempPfn;
PageType BusyType; xbox::PageType BusyType;
xbox::ntstatus_xt status; xbox::ntstatus_xt status;
VAddr CapturedBase = *addr; VAddr CapturedBase = *addr;
size_t CapturedSize = *Size; size_t CapturedSize = *Size;
@ -1484,7 +1484,7 @@ xbox::ntstatus_xt VMManager::XbAllocateVirtualMemory(VAddr* addr, ULONG ZeroBits
// With VirtualAlloc we grab one page at a time to avoid fragmentation issues // With VirtualAlloc we grab one page at a time to avoid fragmentation issues
BusyType = (Protect & (XBOX_PAGE_EXECUTE | XBOX_PAGE_EXECUTE_READ | XBOX_PAGE_EXECUTE_READWRITE BusyType = (Protect & (XBOX_PAGE_EXECUTE | XBOX_PAGE_EXECUTE_READ | XBOX_PAGE_EXECUTE_READWRITE
| XBOX_PAGE_EXECUTE_WRITECOPY)) ? ImageType : VirtualMemoryType; | XBOX_PAGE_EXECUTE_WRITECOPY)) ? xbox::ImageType : xbox::VirtualMemoryType;
PointerPte = StartingPte; PointerPte = StartingPte;
while (PointerPte <= EndingPte) while (PointerPte <= EndingPte)
{ {
@ -1542,11 +1542,11 @@ xbox::ntstatus_xt VMManager::XbFreeVirtualMemory(VAddr* addr, size_t* Size, DWOR
VAddr AlignedCapturedBase; VAddr AlignedCapturedBase;
size_t AlignedCapturedSize; size_t AlignedCapturedSize;
xbox::ntstatus_xt status; xbox::ntstatus_xt status;
PMMPTE PointerPte; xbox::PMMPTE PointerPte;
PMMPTE EndingPte; xbox::PMMPTE EndingPte;
PMMPTE StartingPte; xbox::PMMPTE StartingPte;
PFN TempPfn; xbox::PFN TempPfn;
PageType BusyType; xbox::PageType BusyType;
VMAIter it; VMAIter it;
bool bOverflow; bool bOverflow;
@ -1653,9 +1653,9 @@ xbox::ntstatus_xt VMManager::XbFreeVirtualMemory(VAddr* addr, size_t* Size, DWOR
TempPfn = PointerPte->Hardware.PFN; TempPfn = PointerPte->Hardware.PFN;
InsertFree(TempPfn, TempPfn); InsertFree(TempPfn, TempPfn);
if (m_MmLayoutRetail || m_MmLayoutDebug) { if (m_MmLayoutRetail || m_MmLayoutDebug) {
BusyType = (PageType)(XBOX_PFN_ELEMENT(TempPfn)->Busy.BusyType); BusyType = (xbox::PageType)(XBOX_PFN_ELEMENT(TempPfn)->Busy.BusyType);
} }
else { BusyType = (PageType)(CHIHIRO_PFN_ELEMENT(TempPfn)->Busy.BusyType); } else { BusyType = (xbox::PageType)(CHIHIRO_PFN_ELEMENT(TempPfn)->Busy.BusyType); }
WritePfn(TempPfn, TempPfn, PointerPte, BusyType, true); WritePfn(TempPfn, TempPfn, PointerPte, BusyType, true);
} }
@ -1701,13 +1701,13 @@ xbox::ntstatus_xt VMManager::XbVirtualProtect(VAddr* addr, size_t* Size, DWORD*
VAddr AlignedCapturedBase; VAddr AlignedCapturedBase;
size_t AlignedCapturedSize; size_t AlignedCapturedSize;
xbox::ntstatus_xt status; xbox::ntstatus_xt status;
PMMPTE PointerPte; xbox::PMMPTE PointerPte;
PMMPTE EndingPte; xbox::PMMPTE EndingPte;
PMMPTE StartingPte; xbox::PMMPTE StartingPte;
PMMPTE PointerPde; xbox::PMMPTE PointerPde;
MMPTE TempPte; xbox::MMPTE TempPte;
MMPTE NewPermsPte; xbox::MMPTE NewPermsPte;
MMPTE OldPermsPte; xbox::MMPTE OldPermsPte;
VMAIter it; VMAIter it;
bool bOverflow; bool bOverflow;
@ -1808,10 +1808,10 @@ xbox::ntstatus_xt VMManager::XbVirtualProtect(VAddr* addr, size_t* Size, DWORD*
xbox::ntstatus_xt VMManager::XbVirtualMemoryStatistics(VAddr addr, xbox::PMEMORY_BASIC_INFORMATION memory_statistics) xbox::ntstatus_xt VMManager::XbVirtualMemoryStatistics(VAddr addr, xbox::PMEMORY_BASIC_INFORMATION memory_statistics)
{ {
VMAIter it; VMAIter it;
PMMPTE PointerPte; xbox::PMMPTE PointerPte;
PMMPTE EndingPte; xbox::PMMPTE EndingPte;
PMMPTE StartingPte; xbox::PMMPTE StartingPte;
PMMPTE PointerPde; xbox::PMMPTE PointerPde;
DWORD CurrentProtect; DWORD CurrentProtect;
DWORD InitialProtect; DWORD InitialProtect;
DWORD CurrentState; DWORD CurrentState;
@ -1904,7 +1904,7 @@ xbox::ntstatus_xt VMManager::XbVirtualMemoryStatistics(VAddr addr, xbox::PMEMORY
if (CurrentState == XBOX_MEM_COMMIT) { break; } if (CurrentState == XBOX_MEM_COMMIT) { break; }
// Pde is invalid and we are looking for reserved memory, so skip this pde and keep searching if we are not at EndingPte yet // Pde is invalid and we are looking for reserved memory, so skip this pde and keep searching if we are not at EndingPte yet
PointerPte = (PMMPTE)GetVAddrMappedByPte(PointerPde + 1); PointerPte = (xbox::PMMPTE)GetVAddrMappedByPte(PointerPde + 1);
continue; continue;
} }
} }
@ -1945,7 +1945,7 @@ xbox::ntstatus_xt VMManager::XbVirtualMemoryStatistics(VAddr addr, xbox::PMEMORY
return X_STATUS_SUCCESS; return X_STATUS_SUCCESS;
} }
VAddr VMManager::MapMemoryBlock(MemoryRegionType Type, PFN_COUNT PteNumber, DWORD Permissions, bool b64Blocks, VAddr HighestAddress) VAddr VMManager::MapMemoryBlock(MemoryRegionType Type, xbox::PFN_COUNT PteNumber, DWORD Permissions, bool b64Blocks, VAddr HighestAddress)
{ {
VAddr addr; VAddr addr;
VMAIter it = m_MemoryRegionArray[Type].LastFree; VMAIter it = m_MemoryRegionArray[Type].LastFree;
@ -2101,7 +2101,7 @@ bool VMManager::IsValidVirtualAddress(const VAddr addr)
{ {
LOG_FUNC_ONE_ARG(addr); LOG_FUNC_ONE_ARG(addr);
PMMPTE PointerPte; xbox::PMMPTE PointerPte;
Lock(); Lock();
@ -2135,7 +2135,7 @@ PAddr VMManager::TranslateVAddrToPAddr(const VAddr addr)
LOG_FUNC_ONE_ARG(addr); LOG_FUNC_ONE_ARG(addr);
PAddr PAddr; PAddr PAddr;
PMMPTE PointerPte; xbox::PMMPTE PointerPte;
// ergo720: horrendous hack, this identity maps all allocations done by the VMManager to keep the LLE USB working. // ergo720: horrendous hack, this identity maps all allocations done by the VMManager to keep the LLE USB working.
// The problem is that if the user buffer pointed to by the TD is allocated by the VMManager with VirtualAlloc, then // The problem is that if the user buffer pointed to by the TD is allocated by the VMManager with VirtualAlloc, then

View File

@ -103,13 +103,13 @@ class VMManager : public PhysicalMemory
// retrieves memory statistics // retrieves memory statistics
void MemoryStatistics(xbox::PMM_STATISTICS memory_statistics); void MemoryStatistics(xbox::PMM_STATISTICS memory_statistics);
// allocates memory in the system region // allocates memory in the system region
VAddr AllocateSystemMemory(PageType BusyType, DWORD Perms, size_t Size, bool bAddGuardPage); VAddr AllocateSystemMemory(xbox::PageType BusyType, DWORD Perms, size_t Size, bool bAddGuardPage);
// allocates memory in the contiguous region // allocates memory in the contiguous region
VAddr AllocateContiguousMemory(size_t Size, PAddr LowestAddress, PAddr HighestAddress, ULONG Alignment, DWORD Perms); VAddr AllocateContiguousMemory(size_t Size, PAddr LowestAddress, PAddr HighestAddress, ULONG Alignment, DWORD Perms);
// maps device memory in the system region // maps device memory in the system region
VAddr MapDeviceMemory(PAddr Paddr, size_t Size, DWORD Perms); VAddr MapDeviceMemory(PAddr Paddr, size_t Size, DWORD Perms);
// deallocates memory in the system region // deallocates memory in the system region
PFN_COUNT DeallocateSystemMemory(PageType BusyType, VAddr addr, size_t Size); xbox::PFN_COUNT DeallocateSystemMemory(xbox::PageType BusyType, VAddr addr, size_t Size);
// deallocates memory in the contiguous region // deallocates memory in the contiguous region
void DeallocateContiguousMemory(VAddr addr); void DeallocateContiguousMemory(VAddr addr);
// unmaps device memory in the system region // unmaps device memory in the system region
@ -131,9 +131,9 @@ class VMManager : public PhysicalMemory
// make contiguous memory persist across a quick reboot // make contiguous memory persist across a quick reboot
void PersistMemory(VAddr addr, size_t Size, bool bPersist); void PersistMemory(VAddr addr, size_t Size, bool bPersist);
// debug function to reset a pte or check if it is writable // debug function to reset a pte or check if it is writable
VAddr DbgTestPte(VAddr addr, PMMPTE Pte, bool bWriteCheck); VAddr DbgTestPte(VAddr addr, xbox::PMMPTE Pte, bool bWriteCheck);
// retrieves the number of free debugger pages // retrieves the number of free debugger pages
PFN_COUNT QueryNumberOfFreeDebuggerPages(); xbox::PFN_COUNT QueryNumberOfFreeDebuggerPages();
// xbox implementation of NtAllocateVirtualMemory // xbox implementation of NtAllocateVirtualMemory
xbox::ntstatus_xt XbAllocateVirtualMemory(VAddr* addr, ULONG ZeroBits, size_t* Size, DWORD AllocationType, DWORD Protect); xbox::ntstatus_xt XbAllocateVirtualMemory(VAddr* addr, ULONG ZeroBits, size_t* Size, DWORD AllocationType, DWORD Protect);
// xbox implementation of NtFreeVirtualMemory // xbox implementation of NtFreeVirtualMemory
@ -161,7 +161,7 @@ class VMManager : public PhysicalMemory
void* m_PersistentMemoryHandle = nullptr; void* m_PersistentMemoryHandle = nullptr;
// same as AllocateContiguousMemory, but it allows to allocate beyond m_MaxContiguousPfn // same as AllocateContiguousMemory, but it allows to allocate beyond m_MaxContiguousPfn
VAddr AllocateContiguousMemoryInternal(PFN_COUNT NumberOfPages, PFN LowestPfn, PFN HighestPfn, PFN PfnAlignment, DWORD Perms, PageType BusyType = ContiguousType); VAddr AllocateContiguousMemoryInternal(xbox::PFN_COUNT NumberOfPages, xbox::PFN LowestPfn, xbox::PFN HighestPfn, xbox::PFN PfnAlignment, DWORD Perms, xbox::PageType BusyType = xbox::ContiguousType);
// set up the system allocations // set up the system allocations
void InitializeSystemAllocations(); void InitializeSystemAllocations();
// initializes a memory region struct // initializes a memory region struct
@ -169,7 +169,7 @@ class VMManager : public PhysicalMemory
// clears all memory region structs // clears all memory region structs
void DestroyMemoryRegions(); void DestroyMemoryRegions();
// map a memory block with the supplied allocation routine // map a memory block with the supplied allocation routine
VAddr MapMemoryBlock(MemoryRegionType Type, PFN_COUNT PteNumber, DWORD Permissions, bool b64Blocks, VAddr HighestAddress = 0); VAddr MapMemoryBlock(MemoryRegionType Type, xbox::PFN_COUNT PteNumber, DWORD Permissions, bool b64Blocks, VAddr HighestAddress = 0);
// helper function which allocates user memory with VirtualAlloc // helper function which allocates user memory with VirtualAlloc
VAddr MapHostMemory(VAddr StartingAddr, size_t Size, size_t VmaEnd, DWORD Permissions); VAddr MapHostMemory(VAddr StartingAddr, size_t Size, size_t VmaEnd, DWORD Permissions);
// constructs a vma // constructs a vma