diff --git a/src/CxbxKrnl/EmuKrnlHal.cpp b/src/CxbxKrnl/EmuKrnlHal.cpp index 1ecc61c5e..7aa24e4d5 100644 --- a/src/CxbxKrnl/EmuKrnlHal.cpp +++ b/src/CxbxKrnl/EmuKrnlHal.cpp @@ -62,14 +62,6 @@ namespace xboxkrnl #include #include -// See the links below for the details about the kernel structure LIST_ENTRY and the related functions -// https://www.codeproject.com/Articles/800404/Understanding-LIST-ENTRY-Lists-and-Its-Importance -// https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/singly-and-doubly-linked-lists -#define LIST_ENTRY_DEFINE_HEAD(ListHead) xboxkrnl::LIST_ENTRY (ListHead) = { &(ListHead), &(ListHead) } -#define LIST_ENTRY_ACCESS_RECORD(address, type, field) \ -((type*)((UCHAR*)(address) - (ULONG)(&((type*)0)->field))) - - volatile DWORD HalInterruptRequestRegister = APC_LEVEL | DISPATCH_LEVEL; HalSystemInterrupt HalSystemInterrupts[MAX_BUS_INTERRUPT_LEVEL + 1]; @@ -78,7 +70,8 @@ uint8_t ResetOrShutdownCommandCode = 0; uint32_t ResetOrShutdownDataValue = 0; // global list of routines executed during a reboot -LIST_ENTRY_DEFINE_HEAD(ShutdownRoutineList); +xboxkrnl::LIST_ENTRY ShutdownRoutineList = { &ShutdownRoutineList , &ShutdownRoutineList }; // see InitializeListHead() + // ****************************************************************** // * Declaring this in a header causes errors with xboxkrnl @@ -404,7 +397,7 @@ XBSYSAPI EXPORTNUM(47) xboxkrnl::VOID NTAPI xboxkrnl::HalRegisterShutdownNotific ListEntry = ShutdownRoutineList.Flink; while (ListEntry != &ShutdownRoutineList) { - if (ShutdownRegistration->Priority > LIST_ENTRY_ACCESS_RECORD(ListEntry, HAL_SHUTDOWN_REGISTRATION, ListEntry)->Priority) + if (ShutdownRegistration->Priority > CONTAINING_RECORD(ListEntry, HAL_SHUTDOWN_REGISTRATION, ListEntry)->Priority) { InsertTailList(ListEntry, &ShutdownRegistration->ListEntry); break; @@ -422,7 +415,7 @@ XBSYSAPI EXPORTNUM(47) xboxkrnl::VOID NTAPI xboxkrnl::HalRegisterShutdownNotific ListEntry = ShutdownRoutineList.Flink; while (ListEntry != &ShutdownRoutineList) { - if (ShutdownRegistration == LIST_ENTRY_ACCESS_RECORD(ListEntry, HAL_SHUTDOWN_REGISTRATION, ListEntry)) + if (ShutdownRegistration == CONTAINING_RECORD(ListEntry, HAL_SHUTDOWN_REGISTRATION, ListEntry)) { RemoveEntryList(&ShutdownRegistration->ListEntry); break; @@ -524,7 +517,7 @@ XBSYSAPI EXPORTNUM(49) xboxkrnl::VOID DECLSPEC_NORETURN NTAPI xboxkrnl::HalRetur if (ListEntry == &ShutdownRoutineList) break; - ShutdownRegistration = LIST_ENTRY_ACCESS_RECORD(ListEntry, HAL_SHUTDOWN_REGISTRATION, ListEntry); + ShutdownRegistration = CONTAINING_RECORD(ListEntry, HAL_SHUTDOWN_REGISTRATION, ListEntry); ShutdownRegistration->NotificationRoutine(ShutdownRegistration); } #endif diff --git a/src/CxbxKrnl/PhysicalMemory.cpp b/src/CxbxKrnl/PhysicalMemory.cpp index b25360678..c1234ac0b 100644 --- a/src/CxbxKrnl/PhysicalMemory.cpp +++ b/src/CxbxKrnl/PhysicalMemory.cpp @@ -46,8 +46,11 @@ // https://www.codeproject.com/Articles/800404/Understanding-LIST-ENTRY-Lists-and-Its-Importance // https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/singly-and-doubly-linked-lists #define LIST_ENTRY_INITIALIZE(ListEntry) ((ListEntry)->Flink = (ListEntry)->Blink = nullptr) -#define LIST_ENTRY_ACCESS_RECORD(address, type, field) \ -((type*)((UCHAR*)(address) - (ULONG)(&((type*)0)->field))) + +inline FreeBlock* ListEntryToFreeBlock(xboxkrnl::PLIST_ENTRY pListEntry) +{ + return CONTAINING_RECORD(pListEntry, FreeBlock, ListEntry); +} void PhysicalMemory::InitializePageDirectory() { @@ -201,10 +204,10 @@ bool PhysicalMemory::RemoveFree(PFN_COUNT NumberOfPages, PFN* result, PFN_COUNT while (ListEntry != &FreeList) { - if (LIST_ENTRY_ACCESS_RECORD(ListEntry, FreeBlock, ListEntry)->size >= NumberOfPages) // search for a block with enough pages + if (ListEntryToFreeBlock(ListEntry)->size >= NumberOfPages) // search for a block with enough pages { - PfnStart = LIST_ENTRY_ACCESS_RECORD(ListEntry, FreeBlock, ListEntry)->start; - PfnCount = LIST_ENTRY_ACCESS_RECORD(ListEntry, FreeBlock, ListEntry)->size; + PfnStart = ListEntryToFreeBlock(ListEntry)->start; + PfnCount = ListEntryToFreeBlock(ListEntry)->size; PfnEnd = PfnStart + PfnCount - 1; IntersectionStart = start >= PfnStart ? start : PfnStart; IntersectionEnd = end <= PfnEnd ? end : PfnEnd; @@ -251,9 +254,9 @@ bool PhysicalMemory::RemoveFree(PFN_COUNT NumberOfPages, PFN* result, PFN_COUNT // delete the entry if there is no free space left RemoveEntryList(ListEntry); - delete LIST_ENTRY_ACCESS_RECORD(ListEntry, FreeBlock, ListEntry); + delete ListEntryToFreeBlock(ListEntry); } - else { LIST_ENTRY_ACCESS_RECORD(ListEntry, FreeBlock, ListEntry)->size = PfnCount; } + else { ListEntryToFreeBlock(ListEntry)->size = PfnCount; } } else { @@ -271,9 +274,9 @@ bool PhysicalMemory::RemoveFree(PFN_COUNT NumberOfPages, PFN* result, PFN_COUNT // delete the entry if there is no free space left RemoveEntryList(ListEntry); - delete LIST_ENTRY_ACCESS_RECORD(ListEntry, FreeBlock, ListEntry); + delete ListEntryToFreeBlock(ListEntry); } - else { LIST_ENTRY_ACCESS_RECORD(ListEntry, FreeBlock, ListEntry)->size = PfnCount; } + else { ListEntryToFreeBlock(ListEntry)->size = PfnCount; } } } else @@ -285,7 +288,7 @@ bool PhysicalMemory::RemoveFree(PFN_COUNT NumberOfPages, PFN* result, PFN_COUNT // The free block extends before IntersectionStart PfnCount -= NumberOfPages; - LIST_ENTRY_ACCESS_RECORD(ListEntry, FreeBlock, ListEntry)->size = PfnCount; + ListEntryToFreeBlock(ListEntry)->size = PfnCount; } else { @@ -298,7 +301,7 @@ bool PhysicalMemory::RemoveFree(PFN_COUNT NumberOfPages, PFN* result, PFN_COUNT InsertHeadList(ListEntry, &block->ListEntry); PfnCount = IntersectionEnd - PfnStart - NumberOfPages + 1; - LIST_ENTRY_ACCESS_RECORD(ListEntry, FreeBlock, ListEntry)->size = PfnCount; + ListEntryToFreeBlock(ListEntry)->size = PfnCount; } } if (m_MmLayoutDebug && (PfnStart + PfnCount >= DEBUGKIT_FIRST_UPPER_HALF_PAGE)) { @@ -328,7 +331,7 @@ void PhysicalMemory::InsertFree(PFN start, PFN end) while (true) { - if (LIST_ENTRY_ACCESS_RECORD(ListEntry, FreeBlock, ListEntry)->start < start || ListEntry == &FreeList) + if (ListEntryToFreeBlock(ListEntry)->start < start || ListEntry == &FreeList) { PFreeBlock block = new FreeBlock; block->start = start; @@ -338,35 +341,35 @@ void PhysicalMemory::InsertFree(PFN start, PFN end) // Ensure that we are not freeing a part of the previous block if (ListEntry != &FreeList) { - assert(LIST_ENTRY_ACCESS_RECORD(ListEntry, FreeBlock, ListEntry)->start + - LIST_ENTRY_ACCESS_RECORD(ListEntry, FreeBlock, ListEntry)->size - 1 < start); + assert(ListEntryToFreeBlock(ListEntry)->start + + ListEntryToFreeBlock(ListEntry)->size - 1 < start); } ListEntry = ListEntry->Flink; // move to the new created block // Ensure that we are not freeing a part of the next block if (ListEntry->Flink != &FreeList) { - assert(LIST_ENTRY_ACCESS_RECORD(ListEntry->Flink, FreeBlock, ListEntry)->start > end); + assert(ListEntryToFreeBlock(ListEntry->Flink)->start > end); } // Check if merging is possible if (ListEntry->Flink != &FreeList && - start + size == LIST_ENTRY_ACCESS_RECORD(ListEntry->Flink, FreeBlock, ListEntry)->start) + start + size == ListEntryToFreeBlock(ListEntry->Flink)->start) { // Merge forward xboxkrnl::PLIST_ENTRY temp = ListEntry->Flink; - LIST_ENTRY_ACCESS_RECORD(ListEntry, FreeBlock, ListEntry)->size += - LIST_ENTRY_ACCESS_RECORD(temp, FreeBlock, ListEntry)->size; + ListEntryToFreeBlock(ListEntry)->size += + ListEntryToFreeBlock(temp)->size; RemoveEntryList(temp); - delete LIST_ENTRY_ACCESS_RECORD(temp, FreeBlock, ListEntry); + delete ListEntryToFreeBlock(temp); } if (ListEntry->Blink != &FreeList && - LIST_ENTRY_ACCESS_RECORD(ListEntry->Blink, FreeBlock, ListEntry)->start + - LIST_ENTRY_ACCESS_RECORD(ListEntry->Blink, FreeBlock, ListEntry)->size == start) + ListEntryToFreeBlock(ListEntry->Blink)->start + + ListEntryToFreeBlock(ListEntry->Blink)->size == start) { // Merge backward - LIST_ENTRY_ACCESS_RECORD(ListEntry->Blink, FreeBlock, ListEntry)->size += - LIST_ENTRY_ACCESS_RECORD(ListEntry, FreeBlock, ListEntry)->size; + ListEntryToFreeBlock(ListEntry->Blink)->size += + ListEntryToFreeBlock(ListEntry)->size; RemoveEntryList(ListEntry); delete block; } diff --git a/src/CxbxKrnl/VMManager.cpp b/src/CxbxKrnl/VMManager.cpp index b016ea60a..170068199 100644 --- a/src/CxbxKrnl/VMManager.cpp +++ b/src/CxbxKrnl/VMManager.cpp @@ -48,10 +48,6 @@ #include "EmuKrnl.h" // For InitializeListHead(), etc. #include -// See the links below for the details about the kernel structure LIST_ENTRY and the related functions -// https://www.codeproject.com/Articles/800404/Understanding-LIST-ENTRY-Lists-and-Its-Importance -// https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/singly-and-doubly-linked-lists -#define LIST_ENTRY_INITIALIZE(ListEntry) ((ListEntry)->Flink = (ListEntry)->Blink = nullptr) VMManager g_VMManager; @@ -133,7 +129,7 @@ void VMManager::Initialize(HANDLE memory_view, HANDLE pagetables_view, int BootF PFreeBlock block = new FreeBlock; block->start = 0; block->size = m_HighestPage + 1; - LIST_ENTRY_INITIALIZE(&block->ListEntry); + block->ListEntry.Flink = block->ListEntry.Blink = nullptr; // Was LIST_ENTRY_INITIALIZE() InsertHeadList(ListEntry, &block->ListEntry); // Set up the pfn database