Compare commits

..

3 Commits

Author SHA1 Message Date
dependabot[bot]
d8ce4bc7e6 Bump wiiu-env/devkitppc from 20240423 to 20240505
Bumps wiiu-env/devkitppc from 20240423 to 20240505.

---
updated-dependencies:
- dependency-name: wiiu-env/devkitppc
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-05-06 10:20:01 +02:00
Maschell
86c1d20863 Remove allocMutex to avoid possible deadlocks 2024-05-03 22:33:18 +02:00
Maschell
bf99541449 Use custom function to check exp heap 2024-04-26 23:42:38 +02:00
3 changed files with 88 additions and 22 deletions

View File

@ -1,4 +1,4 @@
FROM ghcr.io/wiiu-env/devkitppc:20240423 FROM ghcr.io/wiiu-env/devkitppc:20240505
COPY --from=ghcr.io/wiiu-env/libkernel:20230621 /artifacts $DEVKITPRO COPY --from=ghcr.io/wiiu-env/libkernel:20230621 /artifacts $DEVKITPRO
COPY --from=ghcr.io/wiiu-env/libfunctionpatcher:20230621 /artifacts $DEVKITPRO COPY --from=ghcr.io/wiiu-env/libfunctionpatcher:20230621 /artifacts $DEVKITPRO

View File

@ -82,6 +82,9 @@ WUMS_INITIALIZE(args) {
} }
WUMS_APPLICATION_STARTS() { WUMS_APPLICATION_STARTS() {
#ifdef DEBUG
initLogging();
#endif
OSReport("Running MemoryMappingModule " VERSION VERSION_EXTRA "\n"); OSReport("Running MemoryMappingModule " VERSION VERSION_EXTRA "\n");
MemoryMapping_checkHeaps(); MemoryMapping_checkHeaps();
@ -89,10 +92,6 @@ WUMS_APPLICATION_STARTS() {
// Now we can update the pointer with the "real" functions // Now we can update the pointer with the "real" functions
gMEMAllocFromDefaultHeapExForThreads = MEMAllocFromDefaultHeapEx; gMEMAllocFromDefaultHeapExForThreads = MEMAllocFromDefaultHeapEx;
gMEMFreeToDefaultHeapForThreads = MEMFreeToDefaultHeap; gMEMFreeToDefaultHeapForThreads = MEMFreeToDefaultHeap;
#ifdef DEBUG
initLogging();
#endif
} }

View File

@ -12,7 +12,7 @@
// #define DEBUG_FUNCTION_LINE(x,...) // #define DEBUG_FUNCTION_LINE(x,...)
OSMutex allocMutex; //OSMutex allocMutex;
void runOnAllCores(CThread::Callback callback, void *callbackArg, int32_t iAttr = 0, int32_t iPriority = 16, int32_t iStackSize = 0x8000) { void runOnAllCores(CThread::Callback callback, void *callbackArg, int32_t iAttr = 0, int32_t iPriority = 16, int32_t iStackSize = 0x8000) {
int32_t aff[] = {CThread::eAttributeAffCore2, CThread::eAttributeAffCore1, CThread::eAttributeAffCore0}; int32_t aff[] = {CThread::eAttributeAffCore2, CThread::eAttributeAffCore1, CThread::eAttributeAffCore0};
@ -552,14 +552,14 @@ void MemoryMapping_setupMemoryMapping() {
//readTestValuesFromMemory(); //readTestValuesFromMemory();
//runOnAllCores(writeSegmentRegister,&srTableCpy); //runOnAllCores(writeSegmentRegister,&srTableCpy);
OSInitMutex(&allocMutex); // OSInitMutex(&allocMutex);
memset(pageTableCpy, 0, sizePageTable); memset(pageTableCpy, 0, sizePageTable);
gMEMFreeToDefaultHeapForThreads(pageTableCpy); gMEMFreeToDefaultHeapForThreads(pageTableCpy);
} }
void *MemoryMapping_allocEx(uint32_t size, int32_t align, bool videoOnly) { void *MemoryMapping_allocEx(uint32_t size, int32_t align, bool videoOnly) {
OSLockMutex(&allocMutex); //OSLockMutex(&allocMutex);
void *res = nullptr; void *res = nullptr;
for (int32_t i = 0; /* waiting for a break */; i++) { for (int32_t i = 0; /* waiting for a break */; i++) {
if (mem_mapping[i].physical_addresses == nullptr) { if (mem_mapping[i].physical_addresses == nullptr) {
@ -587,25 +587,92 @@ void *MemoryMapping_allocEx(uint32_t size, int32_t align, bool videoOnly) {
} }
} }
OSMemoryBarrier(); OSMemoryBarrier();
OSUnlockMutex(&allocMutex); //OSUnlockMutex(&allocMutex);
return res; return res;
} }
bool CheckMemExpHeapBlock(MEMExpHeap *heap, MEMExpHeapBlockList *block, uint32_t tag, const char *listName, uint32_t &totalSizeOut) {
MEMExpHeapBlock *prevBlock = nullptr;
for (auto *cur = block->head; cur != nullptr; cur = cur->next) {
if (cur->prev != prevBlock) {
DEBUG_FUNCTION_LINE_ERR("[Exp Heap Check] \"%s\" prev is invalid. expected %08X actual %08X", listName, prevBlock, cur->prev);
return false;
}
if (cur < heap->header.dataStart || cur > heap->header.dataEnd || ((uint32_t) cur + sizeof(MEMExpHeapBlock) + cur->blockSize) > (uint32_t) heap->header.dataEnd) {
DEBUG_FUNCTION_LINE_ERR("[Exp Heap Check] Block is not inside heap. block: %08X size %d; heap start %08X heap end %08X", cur, sizeof(MEMExpHeapBlock) + cur->blockSize, heap->header.dataStart, heap->header.dataEnd);
return false;
}
if (cur->tag != tag) {
DEBUG_FUNCTION_LINE_ERR("[Exp Heap Check] Invalid block tag expected %04X, actual %04X", tag, cur->tag);
return false;
}
totalSizeOut = totalSizeOut + cur->blockSize + (cur->attribs >> 8 & 0x7fffff) + sizeof(MEMExpHeapBlock);
prevBlock = cur;
}
if (prevBlock != block->tail) {
DEBUG_FUNCTION_LINE_ERR("[Exp Heap Check] \"%s\" tail is unexpected! expected %08X, actual %08X", listName, heap->usedList.tail, prevBlock);
return false;
}
return true;
}
bool CheckMemExpHeapCore(MEMExpHeap *heap) {
uint32_t totalSize = 0;
#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
if (!CheckMemExpHeapBlock(heap, &heap->usedList, 0x5544, "used", totalSize)) {
return false;
}
#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
if (!CheckMemExpHeapBlock(heap, &heap->freeList, 0x4652, "free", totalSize)) {
return false;
}
if (totalSize != (uint32_t) heap->header.dataEnd - (uint32_t) heap->header.dataStart) {
DEBUG_FUNCTION_LINE_ERR("[Exp Heap Check] heap size is unexpected! expected %08X, actual %08X", (uint32_t) heap->header.dataEnd - (uint32_t) heap->header.dataStart, totalSize);
return false;
}
return true;
}
bool CheckMemExpHeap(MEMExpHeap *heap) {
OSMemoryBarrier();
if (heap->header.tag != MEM_EXPANDED_HEAP_TAG) {
DEBUG_FUNCTION_LINE_ERR("[Exp Heap Check] Invalid heap handle. - %08X", heap->header.tag);
return false;
}
if (heap->header.flags & MEM_HEAP_FLAG_USE_LOCK) {
#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
OSUninterruptibleSpinLock_Acquire(&(heap->header).lock);
}
auto result = CheckMemExpHeapCore(heap);
if (heap->header.flags & MEM_HEAP_FLAG_USE_LOCK) {
#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
OSUninterruptibleSpinLock_Release(&(heap->header).lock);
}
return result;
}
void MemoryMapping_checkHeaps() { void MemoryMapping_checkHeaps() {
OSLockMutex(&allocMutex); //OSLockMutex(&allocMutex);
for (int32_t i = 0; /* waiting for a break */; i++) { for (int32_t i = 0; /* waiting for a break */; i++) {
if (mem_mapping[i].physical_addresses == nullptr) { if (mem_mapping[i].physical_addresses == nullptr) {
break; break;
} }
auto heapHandle = (MEMHeapHandle) mem_mapping[i].effective_start_address; auto heapHandle = (MEMHeapHandle) mem_mapping[i].effective_start_address;
if (!MEMCheckExpHeap(heapHandle, MEM_EXP_HEAP_CHECK_FLAGS_LOG_ERRORS)) { if (!CheckMemExpHeap(reinterpret_cast<MEMExpHeap *>(heapHandle))) {
DEBUG_FUNCTION_LINE_ERR("MemoryMapping heap %08X (index %d) is corrupted.", heapHandle, i); DEBUG_FUNCTION_LINE_ERR("MemoryMapping heap %08X (index %d) is corrupted.", heapHandle, i);
#ifdef DEBUG #ifdef DEBUG
OSFatal("MemoryMappingModule: Heap is corrupted"); OSFatal("MemoryMappingModule: Heap is corrupted");
#endif #endif
} }
} }
OSUnlockMutex(&allocMutex); //OSUnlockMutex(&allocMutex);
} }
void *MemoryMapping_alloc(uint32_t size, int32_t align) { void *MemoryMapping_alloc(uint32_t size, int32_t align) {
@ -643,7 +710,7 @@ void MemoryMapping_free(void *ptr) {
if (ptr == nullptr) { if (ptr == nullptr) {
return; return;
} }
OSLockMutex(&allocMutex); //OSLockMutex(&allocMutex);
auto ptr_val = (uint32_t) ptr; auto ptr_val = (uint32_t) ptr;
for (int32_t i = 0; /* waiting for a break */; i++) { for (int32_t i = 0; /* waiting for a break */; i++) {
if (mem_mapping[i].physical_addresses == nullptr) { if (mem_mapping[i].physical_addresses == nullptr) {
@ -656,7 +723,7 @@ void MemoryMapping_free(void *ptr) {
} }
} }
OSMemoryBarrier(); OSMemoryBarrier();
OSUnlockMutex(&allocMutex); //OSUnlockMutex(&allocMutex);
} }
uint32_t MemoryMapping_MEMGetAllocatableSize() { uint32_t MemoryMapping_MEMGetAllocatableSize() {
@ -664,7 +731,7 @@ uint32_t MemoryMapping_MEMGetAllocatableSize() {
} }
uint32_t MemoryMapping_MEMGetAllocatableSizeEx(uint32_t align) { uint32_t MemoryMapping_MEMGetAllocatableSizeEx(uint32_t align) {
OSLockMutex(&allocMutex); //OSLockMutex(&allocMutex);
uint32_t res = 0; uint32_t res = 0;
for (int32_t i = 0; /* waiting for a break */; i++) { for (int32_t i = 0; /* waiting for a break */; i++) {
if (mem_mapping[i].physical_addresses == nullptr) { if (mem_mapping[i].physical_addresses == nullptr) {
@ -676,12 +743,12 @@ uint32_t MemoryMapping_MEMGetAllocatableSizeEx(uint32_t align) {
res = curRes; res = curRes;
} }
} }
OSUnlockMutex(&allocMutex); //OSUnlockMutex(&allocMutex);
return res; return res;
} }
uint32_t MemoryMapping_GetFreeSpace() { uint32_t MemoryMapping_GetFreeSpace() {
OSLockMutex(&allocMutex); //OSLockMutex(&allocMutex);
uint32_t res = 0; uint32_t res = 0;
for (int32_t i = 0; /* waiting for a break */; i++) { for (int32_t i = 0; /* waiting for a break */; i++) {
if (mem_mapping[i].physical_addresses == nullptr) { if (mem_mapping[i].physical_addresses == nullptr) {
@ -691,12 +758,12 @@ uint32_t MemoryMapping_GetFreeSpace() {
DEBUG_FUNCTION_LINE_VERBOSE("heap at %08X MEMGetTotalFreeSizeForExpHeap: %d KiB", mem_mapping[i].effective_start_address, curRes / 1024); DEBUG_FUNCTION_LINE_VERBOSE("heap at %08X MEMGetTotalFreeSizeForExpHeap: %d KiB", mem_mapping[i].effective_start_address, curRes / 1024);
res += curRes; res += curRes;
} }
OSUnlockMutex(&allocMutex); //OSUnlockMutex(&allocMutex);
return res; return res;
} }
void MemoryMapping_CreateHeaps() { void MemoryMapping_CreateHeaps() {
OSLockMutex(&allocMutex); //OSLockMutex(&allocMutex);
for (int32_t i = 0; /* waiting for a break */; i++) { for (int32_t i = 0; /* waiting for a break */; i++) {
if (mem_mapping[i].physical_addresses == nullptr) { if (mem_mapping[i].physical_addresses == nullptr) {
break; break;
@ -713,11 +780,11 @@ void MemoryMapping_CreateHeaps() {
DEBUG_FUNCTION_LINE("Created heap @%08X, size %d KiB", heap, size / 1024); DEBUG_FUNCTION_LINE("Created heap @%08X, size %d KiB", heap, size / 1024);
#endif #endif
} }
OSUnlockMutex(&allocMutex); //OSUnlockMutex(&allocMutex);
} }
void MemoryMapping_DestroyHeaps() { void MemoryMapping_DestroyHeaps() {
OSLockMutex(&allocMutex); //OSLockMutex(&allocMutex);
for (int32_t i = 0; /* waiting for a break */; i++) { for (int32_t i = 0; /* waiting for a break */; i++) {
if (mem_mapping[i].physical_addresses == nullptr) { if (mem_mapping[i].physical_addresses == nullptr) {
break; break;
@ -729,7 +796,7 @@ void MemoryMapping_DestroyHeaps() {
memset(address, 0, size); memset(address, 0, size);
DEBUG_FUNCTION_LINE_VERBOSE("Destroyed heap @%08X", address); DEBUG_FUNCTION_LINE_VERBOSE("Destroyed heap @%08X", address);
} }
OSUnlockMutex(&allocMutex); //OSUnlockMutex(&allocMutex);
} }
uint32_t MemoryMapping_getAreaSizeFromPageTable(uint32_t start, uint32_t maxSize) { uint32_t MemoryMapping_getAreaSizeFromPageTable(uint32_t start, uint32_t maxSize) {