Compare commits

..

No commits in common. "main" and "MemoryMappingModule-20230321-220731" have entirely different histories.

11 changed files with 43 additions and 362 deletions

View File

@ -1,10 +0,0 @@
version: 2
updates:
- package-ecosystem: "docker"
directory: "/"
schedule:
interval: "daily"
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"

View File

@ -3,13 +3,13 @@ name: CI-Release
on: on:
push: push:
branches: branches:
- main - master
jobs: jobs:
clang-format: clang-format:
runs-on: ubuntu-22.04 runs-on: ubuntu-22.04
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v3
- name: clang-format - name: clang-format
run: | run: |
docker run --rm -v ${PWD}:/src ghcr.io/wiiu-env/clang-format:13.0.0-2 -r ./source docker run --rm -v ${PWD}:/src ghcr.io/wiiu-env/clang-format:13.0.0-2 -r ./source
@ -17,7 +17,7 @@ jobs:
runs-on: ubuntu-22.04 runs-on: ubuntu-22.04
needs: clang-format needs: clang-format
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v3
- name: create version.h - name: create version.h
run: | run: |
git_hash=$(git rev-parse --short "$GITHUB_SHA") git_hash=$(git rev-parse --short "$GITHUB_SHA")
@ -48,7 +48,7 @@ jobs:
- name: zip artifact - name: zip artifact
run: zip -r ${{ env.REPOSITORY_NAME }}_${{ env.DATETIME }}.zip *.wms run: zip -r ${{ env.REPOSITORY_NAME }}_${{ env.DATETIME }}.zip *.wms
- name: Create Release - name: Create Release
uses: "softprops/action-gh-release@v2" uses: "softprops/action-gh-release@v1"
with: with:
tag_name: ${{ env.REPOSITORY_NAME }}-${{ env.DATETIME }} tag_name: ${{ env.REPOSITORY_NAME }}-${{ env.DATETIME }}
draft: falsee draft: falsee

View File

@ -6,7 +6,7 @@ jobs:
clang-format: clang-format:
runs-on: ubuntu-22.04 runs-on: ubuntu-22.04
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v3
- name: clang-format - name: clang-format
run: | run: |
docker run --rm -v ${PWD}:/src ghcr.io/wiiu-env/clang-format:13.0.0-2 -r ./source docker run --rm -v ${PWD}:/src ghcr.io/wiiu-env/clang-format:13.0.0-2 -r ./source
@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-22.04 runs-on: ubuntu-22.04
needs: clang-format needs: clang-format
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v3
- name: build binary with logging - name: build binary with logging
run: | run: |
docker build . -t builder docker build . -t builder
@ -25,7 +25,7 @@ jobs:
runs-on: ubuntu-22.04 runs-on: ubuntu-22.04
needs: clang-format needs: clang-format
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v3
- name: create version.h - name: create version.h
run: | run: |
git_hash=$(git rev-parse --short "${{ github.event.pull_request.head.sha }}") git_hash=$(git rev-parse --short "${{ github.event.pull_request.head.sha }}")

1
.gitignore vendored
View File

@ -8,4 +8,3 @@ build/
cmake-build-debug/ cmake-build-debug/
CMakeLists.txt CMakeLists.txt
*.wms *.wms
*.zip

View File

@ -1,7 +1,7 @@
FROM ghcr.io/wiiu-env/devkitppc:20240505 FROM ghcr.io/wiiu-env/devkitppc:20221228
COPY --from=ghcr.io/wiiu-env/libkernel:20230621 /artifacts $DEVKITPRO COPY --from=ghcr.io/wiiu-env/libkernel:20220904 /artifacts $DEVKITPRO
COPY --from=ghcr.io/wiiu-env/libfunctionpatcher:20230621 /artifacts $DEVKITPRO COPY --from=ghcr.io/wiiu-env/libfunctionpatcher:20230106 /artifacts $DEVKITPRO
COPY --from=ghcr.io/wiiu-env/wiiumodulesystem:20240424 /artifacts $DEVKITPRO COPY --from=ghcr.io/wiiu-env/wiiumodulesystem:20230106 /artifacts $DEVKITPRO
WORKDIR project WORKDIR project

View File

@ -16,10 +16,8 @@
****************************************************************************/ ****************************************************************************/
#pragma once #pragma once
#include "globals.h"
#include <coreinit/thread.h> #include <coreinit/thread.h>
#include <cstdint> #include <cstdint>
#include <cstring>
#include <malloc.h> #include <malloc.h>
#include <unistd.h> #include <unistd.h>
@ -28,15 +26,14 @@ public:
typedef void (*Callback)(CThread *thread, void *arg); typedef void (*Callback)(CThread *thread, void *arg);
//! constructor //! constructor
explicit CThread(int32_t iAttr, int32_t iPriority = 16, int32_t stacksize = 0x8000, CThread::Callback callback = nullptr, void *callbackArg = nullptr) explicit CThread(int32_t iAttr, int32_t iPriority = 16, int32_t iStackSize = 0x8000, CThread::Callback callback = nullptr, void *callbackArg = nullptr)
: pThread(nullptr), pThreadStack(nullptr), pCallback(callback), pCallbackArg(callbackArg) { : pThread(nullptr), pThreadStack(nullptr), pCallback(callback), pCallbackArg(callbackArg) {
//! save attribute assignment //! save attribute assignment
iAttributes = iAttr; iAttributes = iAttr;
iStackSize = stacksize; //! allocate the thread
//! allocate the thread on the default Cafe OS heap pThread = (OSThread *) memalign(8, 0x1000);
pThread = (OSThread *) gMEMAllocFromDefaultHeapExForThreads(sizeof(OSThread), 0x10); //! allocate the stack
//! allocate the stack on the default Cafe OS heap pThreadStack = (uint8_t *) memalign(0x20, iStackSize);
pThreadStack = (uint8_t *) gMEMAllocFromDefaultHeapExForThreads(iStackSize, 0x20);
//! create the thread //! create the thread
if (pThread && pThreadStack) { if (pThread && pThreadStack) {
// clang-format off // clang-format off
@ -74,9 +71,7 @@ public:
//! Resume thread //! Resume thread
virtual void resumeThread() { virtual void resumeThread() {
if (!isThreadSuspended()) return; if (!isThreadSuspended()) return;
if (pThread) { if (pThread) OSResumeThread(pThread);
OSResumeThread(pThread);
}
} }
//! Set thread priority //! Set thread priority
@ -116,16 +111,12 @@ public:
} }
OSJoinThread(pThread, nullptr); OSJoinThread(pThread, nullptr);
} }
// Some games (e.g. Minecraft) expect the default heap to be empty.
// Make sure to clean up the memory after using it
//! free the thread stack buffer //! free the thread stack buffer
if (pThreadStack) { if (pThreadStack) {
memset(pThreadStack, 0, iStackSize); free(pThreadStack);
gMEMFreeToDefaultHeapForThreads(pThreadStack);
} }
if (pThread) { if (pThread) {
memset(pThread, 0, sizeof(OSThread)); free(pThread);
gMEMFreeToDefaultHeapForThreads(pThread);
} }
pThread = nullptr; pThread = nullptr;
pThreadStack = nullptr; pThreadStack = nullptr;
@ -148,7 +139,6 @@ private:
return 0; return 0;
} }
uint32_t iStackSize;
int32_t iAttributes; int32_t iAttributes;
OSThread *pThread; OSThread *pThread;
uint8_t *pThreadStack; uint8_t *pThreadStack;

View File

@ -1,4 +0,0 @@
#include "globals.h"
void *(*gMEMAllocFromDefaultHeapExForThreads)(uint32_t size, int align);
void (*gMEMFreeToDefaultHeapForThreads)(void *ptr);

View File

@ -1,5 +0,0 @@
#pragma once
#include <stdint.h>
extern void *(*gMEMAllocFromDefaultHeapExForThreads)(uint32_t size, int align);
extern void (*gMEMFreeToDefaultHeapForThreads)(void *ptr);

View File

@ -1,6 +1,4 @@
#include "function_replacements.h" #include "function_replacements.h"
#include "globals.h"
#include "logger.h"
#include "memory_mapping.h" #include "memory_mapping.h"
#include "version.h" #include "version.h"
#include <coreinit/debug.h> #include <coreinit/debug.h>
@ -10,7 +8,7 @@
#include "logger.h" #include "logger.h"
#endif #endif
#define VERSION "v0.2.6" #define VERSION "v0.2.2"
WUMS_MODULE_EXPORT_NAME("homebrew_memorymapping"); WUMS_MODULE_EXPORT_NAME("homebrew_memorymapping");
WUMS_MODULE_SKIP_INIT_FINI(); WUMS_MODULE_SKIP_INIT_FINI();
@ -18,47 +16,15 @@ WUMS_MODULE_INIT_BEFORE_RELOCATION_DONE_HOOK();
WUMS_DEPENDS_ON(homebrew_kernel); WUMS_DEPENDS_ON(homebrew_kernel);
WUMS_DEPENDS_ON(homebrew_functionpatcher); WUMS_DEPENDS_ON(homebrew_functionpatcher);
#include <coreinit/dynload.h>
#include <coreinit/memdefaultheap.h>
// We can't use the functions from libfunctionpatcher. Defined in functionpatcher.def // We can't use the functions from libfunctionpatcher. Defined in functionpatcher.def
extern "C" FunctionPatcherStatus FPAddFunctionPatch(function_replacement_data_t *function_data, PatchedFunctionHandle *outHandle, bool *outHasBeenPatched); extern "C" FunctionPatcherStatus FPAddFunctionPatch(function_replacement_data_t *function_data, PatchedFunctionHandle *outHandle, bool *outHasBeenPatched);
void UpdateFunctionPointer() {
// We need the real MEMAllocFromDefaultHeapEx/MEMFreeToDefaultHeap function pointer to force-allocate memory on the default heap.
// Our custom heap doesn't work (yet) for threads and causes an app panic.
OSDynLoad_Module coreinitModule;
if (OSDynLoad_Acquire("coreinit", &coreinitModule) != OS_DYNLOAD_OK) {
DEBUG_FUNCTION_LINE_ERR("Failed to acquire coreinit.rpl");
OSFatal("FunctionPatcherModule: Failed to acquire coreinit.rpl");
}
/* Memory allocation functions */
uint32_t *allocPtr, *freePtr;
if (OSDynLoad_FindExport(coreinitModule, OS_DYNLOAD_EXPORT_DATA, "MEMAllocFromDefaultHeapEx", reinterpret_cast<void **>(&allocPtr)) != OS_DYNLOAD_OK) {
DEBUG_FUNCTION_LINE_ERR("OSDynLoad_FindExport for MEMAllocFromDefaultHeapEx");
OSFatal("MemoryMappingModule: OSDynLoad_FindExport for MEMAllocFromDefaultHeapEx");
}
if (OSDynLoad_FindExport(coreinitModule, OS_DYNLOAD_EXPORT_DATA, "MEMFreeToDefaultHeap", reinterpret_cast<void **>(&freePtr)) != OS_DYNLOAD_OK) {
DEBUG_FUNCTION_LINE_ERR("OSDynLoad_FindExport for MEMFreeToDefaultHeap");
OSFatal("MemoryMappingModule: OSDynLoad_FindExport for MEMFreeToDefaultHeap");
}
gMEMAllocFromDefaultHeapExForThreads = (void *(*) (uint32_t, int) ) * allocPtr;
gMEMFreeToDefaultHeapForThreads = (void (*)(void *)) * freePtr;
}
WUMS_INITIALIZE(args) { WUMS_INITIALIZE(args) {
static uint8_t ucSetupRequired = 1; static uint8_t ucSetupRequired = 1;
if (!ucSetupRequired) { if (!ucSetupRequired) {
return; return;
} }
#ifdef DEBUG
initLogging();
#endif
UpdateFunctionPointer();
ucSetupRequired = 0; ucSetupRequired = 0;
MemoryMapping_setupMemoryMapping(); MemoryMapping_setupMemoryMapping();
MemoryMapping_CreateHeaps(); MemoryMapping_CreateHeaps();
@ -75,33 +41,20 @@ WUMS_INITIALIZE(args) {
OSFatal("homebrew_memorymapping: Failed to patch function"); OSFatal("homebrew_memorymapping: Failed to patch function");
} }
} }
#ifdef DEBUG
deinitLogging();
#endif
} }
WUMS_APPLICATION_STARTS() { WUMS_APPLICATION_STARTS() {
OSReport("Running MemoryMappingModule " VERSION VERSION_EXTRA "\n");
#ifdef DEBUG #ifdef DEBUG
initLogging(); initLogging();
#endif #endif
OSReport("Running MemoryMappingModule " VERSION VERSION_EXTRA "\n");
MemoryMapping_checkHeaps();
// Now we can update the pointer with the "real" functions
gMEMAllocFromDefaultHeapExForThreads = MEMAllocFromDefaultHeapEx;
gMEMFreeToDefaultHeapForThreads = MEMFreeToDefaultHeap;
} }
WUMS_APPLICATION_ENDS() {
MemoryMapping_checkHeaps();
#ifdef DEBUG #ifdef DEBUG
WUMS_APPLICATION_REQUESTS_EXIT() {
deinitLogging(); deinitLogging();
#endif
} }
#endif
void MemoryMappingFree(void *ptr) { void MemoryMappingFree(void *ptr) {
//DEBUG_FUNCTION_LINE("[%08X] free", ptr); //DEBUG_FUNCTION_LINE("[%08X] free", ptr);

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};
@ -23,149 +23,6 @@ void runOnAllCores(CThread::Callback callback, void *callbackArg, int32_t iAttr
} }
} }
void KernelWriteU32(uint32_t addr, uint32_t value) {
ICInvalidateRange(&value, 4);
DCFlushRange(&value, 4);
auto dst = (uint32_t) OSEffectiveToPhysical(addr);
auto src = (uint32_t) OSEffectiveToPhysical((uint32_t) &value);
KernelCopyData(dst, src, 4);
DCFlushRange((void *) addr, 4);
ICInvalidateRange((void *) addr, 4);
}
void KernelWrite(uint32_t addr, const void *data, uint32_t length) {
// This is a hacky workaround, but currently it only works this way. ("data" is always on the stack, so maybe a problem with mapping values from the JIT area?)
// further testing required.
for (uint32_t i = 0; i < length; i += 4) {
KernelWriteU32(addr + i, *(uint32_t *) (((uint32_t) data) + i));
}
}
/*
static void SCSetupIBAT4DBAT5() {
asm volatile("sync; eieio; isync");
// Give our and the kernel full execution rights.
// 00800000-01000000 => 30800000-31000000 (read/write, user/supervisor)
unsigned int ibat4u = 0x008000FF;
unsigned int ibat4l = 0x30800012;
asm volatile("mtspr 560, %0" :: "r"(ibat4u));
asm volatile("mtspr 561, %0" :: "r"(ibat4l));
// Give our and the kernel full data access rights.
// 00800000-01000000 => 30800000-31000000 (read/write, user/supervisor)
unsigned int dbat5u = ibat4u;
unsigned int dbat5l = ibat4l;
asm volatile("mtspr 570, %0" :: "r"(dbat5u));
asm volatile("mtspr 571, %0" :: "r"(dbat5l));
asm volatile("eieio; isync");
}
*/
const uint32_t sSCSetupIBAT4DBAT5Buffer[] = {0x7c0004ac,
0x7c0006ac,
0x4c00012c,
0x3d400080,
0x614a00ff,
0x7d508ba6,
0x3d203080,
0x61290012,
0x7d318ba6,
0x7d5a8ba6,
0x7d3b8ba6,
0x7c0006ac,
0x4c00012c,
0x4e800020};
#define TARGET_ADDRESS_EXECUTABLE_MEM 0x017FF000
#define SCSetupIBAT4DBAT5_ADDRESS TARGET_ADDRESS_EXECUTABLE_MEM
const uint32_t sSC0x51Buffer[] = {
0x7c7082a6, // mfspr r3, 528
0x60630003, // ori r3, r3, 0x03
0x7c7083a6, // mtspr 528, r3
0x7c7282a6, // mfspr r3, 530
0x60630003, // ori r3, r3, 0x03
0x7c7283a6, // mtspr 530, r3
0x7c0006ac, // eieio
0x4c00012c, // isync
0x3c600000 | (SCSetupIBAT4DBAT5_ADDRESS >> 16), // lis r3, SCSetupIBAT4DBAT5@h
0x60630000 | (SCSetupIBAT4DBAT5_ADDRESS & 0xFFFF), // ori r3, r3, SCSetupIBAT4DBAT5@l
0x7c6903a6, // mtctr r3
0x4e800420, // bctr
};
#define SC0x51Buffer_ADDRESS (SCSetupIBAT4DBAT5_ADDRESS + sizeof(sSCSetupIBAT4DBAT5Buffer))
#define SC0x51Call_ADDRESS (SC0x51Buffer_ADDRESS + sizeof(sSC0x51Buffer))
const uint32_t sSC0x51CallBuffer[] = {
0x38005100, //li %r0, 0x5100
0x44000002, // sc
0x4e800020 //blr
};
void SetupIBAT4DBAT5OnAllCores() {
unsigned char backupBuffer[0x74];
KernelWrite((uint32_t) backupBuffer, (void *) TARGET_ADDRESS_EXECUTABLE_MEM, sizeof(backupBuffer));
static_assert(sizeof(backupBuffer) >= (sizeof(sSC0x51Buffer) + sizeof(sSCSetupIBAT4DBAT5Buffer) + sizeof(sSC0x51CallBuffer)), "Not enough memory in backup buffer");
static_assert(SCSetupIBAT4DBAT5_ADDRESS >= TARGET_ADDRESS_EXECUTABLE_MEM && SCSetupIBAT4DBAT5_ADDRESS < (TARGET_ADDRESS_EXECUTABLE_MEM + sizeof(backupBuffer)), "buffer in wrong memory region");
static_assert(SC0x51Buffer_ADDRESS >= TARGET_ADDRESS_EXECUTABLE_MEM && SC0x51Buffer_ADDRESS < (TARGET_ADDRESS_EXECUTABLE_MEM + sizeof(backupBuffer)), "buffer in wrong memory region");
static_assert(SC0x51Call_ADDRESS >= TARGET_ADDRESS_EXECUTABLE_MEM && SC0x51Call_ADDRESS < (TARGET_ADDRESS_EXECUTABLE_MEM + sizeof(backupBuffer)), "buffer in wrong memory region");
static_assert(SCSetupIBAT4DBAT5_ADDRESS != SC0x51Buffer_ADDRESS && SCSetupIBAT4DBAT5_ADDRESS != SC0x51Call_ADDRESS && SC0x51Buffer_ADDRESS != SC0x51Call_ADDRESS, "buffer are not different");
// We need copy the functions to a memory region which is executable on all 3 cores
KernelWrite(SCSetupIBAT4DBAT5_ADDRESS, sSCSetupIBAT4DBAT5Buffer, sizeof(sSCSetupIBAT4DBAT5Buffer)); // Set IBAT5 and DBAT5 to map the memory region
KernelWrite(SC0x51Buffer_ADDRESS, sSC0x51Buffer, sizeof(sSC0x51Buffer)); // Implementation of 0x51 syscall
KernelWrite(SC0x51Call_ADDRESS, sSC0x51CallBuffer, sizeof(sSC0x51CallBuffer)); // Call of 0x51 syscall
/* set our setup syscall to an unused position */
KernelPatchSyscall(0x51, SCSetupIBAT4DBAT5_ADDRESS);
// We want to run this on all 3 cores.
{
int32_t aff[] = {CThread::eAttributeAffCore2, CThread::eAttributeAffCore1, CThread::eAttributeAffCore0};
int iStackSize = 0x200;
//! allocate the thread and stack on the default Cafe OS heap
auto *pThread = (OSThread *) gMEMAllocFromDefaultHeapExForThreads(sizeof(OSThread), 0x10);
auto *pThreadStack = (uint8_t *) gMEMAllocFromDefaultHeapExForThreads(iStackSize, 0x20);
//! create the thread
if (pThread && pThreadStack) {
for (int i : aff) {
*pThread = {};
memset(pThreadStack, 0, iStackSize);
OSCreateThread(pThread, reinterpret_cast<OSThreadEntryPointFn>(SC0x51Call_ADDRESS), 0, nullptr, (void *) (pThreadStack + iStackSize), iStackSize, 16, (OSThreadAttributes) i);
OSResumeThread(pThread);
while (OSIsThreadSuspended(pThread)) {
OSResumeThread(pThread);
}
OSJoinThread(pThread, nullptr);
}
}
//! free the thread stack buffer
if (pThreadStack) {
memset(pThreadStack, 0, iStackSize);
gMEMFreeToDefaultHeapForThreads(pThreadStack);
}
if (pThread) {
memset(pThread, 0, sizeof(OSThread));
gMEMFreeToDefaultHeapForThreads(pThread);
}
}
/* repair data */
KernelWrite(TARGET_ADDRESS_EXECUTABLE_MEM, backupBuffer, sizeof(backupBuffer));
DCFlushRange((void *) TARGET_ADDRESS_EXECUTABLE_MEM, sizeof(backupBuffer));
}
void writeKernelNOPs(CThread *thread, void *arg) { void writeKernelNOPs(CThread *thread, void *arg) {
DEBUG_FUNCTION_LINE_VERBOSE("Writing kernel NOPs on core %d", OSGetThreadAffinity(OSGetCurrentThread()) / 2); DEBUG_FUNCTION_LINE_VERBOSE("Writing kernel NOPs on core %d", OSGetThreadAffinity(OSGetCurrentThread()) / 2);
@ -482,11 +339,6 @@ void MemoryMapping_memoryMappingForRegions(const memory_mapping_t *memory_mappin
} }
void MemoryMapping_setupMemoryMapping() { void MemoryMapping_setupMemoryMapping() {
/*
* We need to make sure that with have full access to the 0x0080000-0x01000000 region on all 3 cores.
*/
SetupIBAT4DBAT5OnAllCores();
// Override all writes to SR8 with nops. // Override all writes to SR8 with nops.
// Override some memory region checks inside the kernel // Override some memory region checks inside the kernel
runOnAllCores(writeKernelNOPs, nullptr); runOnAllCores(writeKernelNOPs, nullptr);
@ -494,18 +346,13 @@ void MemoryMapping_setupMemoryMapping() {
//runOnAllCores(readAndPrintSegmentRegister,nullptr,0,16,0x80000); //runOnAllCores(readAndPrintSegmentRegister,nullptr,0,16,0x80000);
sr_table_t srTableCpy; sr_table_t srTableCpy;
uint32_t pageTableCpy[0x8000];
uint32_t sizePageTable = sizeof(uint32_t) * 0x8000;
auto *pageTableCpy = (uint32_t *) gMEMAllocFromDefaultHeapExForThreads(sizePageTable, 0x10);
if (!pageTableCpy) {
OSFatal("MemoryMappingModule: Failed to alloc memory for page table");
}
KernelReadSRs(&srTableCpy); KernelReadSRs(&srTableCpy);
KernelReadPTE((uint32_t) pageTableCpy, sizePageTable); KernelReadPTE((uint32_t) pageTableCpy, sizeof(pageTableCpy));
DCFlushRange(&srTableCpy, sizeof(srTableCpy)); DCFlushRange(&srTableCpy, sizeof(srTableCpy));
DCFlushRange(pageTableCpy, sizePageTable); DCFlushRange(pageTableCpy, sizeof(pageTableCpy));
for (int32_t i = 0; i < 16; i++) { for (int32_t i = 0; i < 16; i++) {
DEBUG_FUNCTION_LINE_VERBOSE("SR[%d]=%08X", i, srTableCpy.value[i]); DEBUG_FUNCTION_LINE_VERBOSE("SR[%d]=%08X", i, srTableCpy.value[i]);
@ -537,9 +384,9 @@ void MemoryMapping_setupMemoryMapping() {
//printPageTableTranslation(srTableCpy,pageTableCpy); //printPageTableTranslation(srTableCpy,pageTableCpy);
DEBUG_FUNCTION_LINE_VERBOSE("Writing PageTable... "); DEBUG_FUNCTION_LINE_VERBOSE("Writing PageTable... ");
DCFlushRange(pageTableCpy, sizePageTable); DCFlushRange(pageTableCpy, sizeof(pageTableCpy));
KernelWritePTE((uint32_t) pageTableCpy, sizePageTable); KernelWritePTE((uint32_t) pageTableCpy, sizeof(pageTableCpy));
DCFlushRange(pageTableCpy, sizePageTable); DCFlushRange(pageTableCpy, sizeof(pageTableCpy));
DEBUG_FUNCTION_LINE_VERBOSE("done"); DEBUG_FUNCTION_LINE_VERBOSE("done");
//printPageTableTranslation(srTableCpy,pageTableCpy); //printPageTableTranslation(srTableCpy,pageTableCpy);
@ -552,14 +399,11 @@ void MemoryMapping_setupMemoryMapping() {
//readTestValuesFromMemory(); //readTestValuesFromMemory();
//runOnAllCores(writeSegmentRegister,&srTableCpy); //runOnAllCores(writeSegmentRegister,&srTableCpy);
// OSInitMutex(&allocMutex); OSInitMutex(&allocMutex);
memset(pageTableCpy, 0, sizePageTable);
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,94 +431,10 @@ 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() {
//OSLockMutex(&allocMutex);
for (int32_t i = 0; /* waiting for a break */; i++) {
if (mem_mapping[i].physical_addresses == nullptr) {
break;
}
auto heapHandle = (MEMHeapHandle) mem_mapping[i].effective_start_address;
if (!CheckMemExpHeap(reinterpret_cast<MEMExpHeap *>(heapHandle))) {
DEBUG_FUNCTION_LINE_ERR("MemoryMapping heap %08X (index %d) is corrupted.", heapHandle, i);
#ifdef DEBUG
OSFatal("MemoryMappingModule: Heap is corrupted");
#endif
}
}
//OSUnlockMutex(&allocMutex);
}
void *MemoryMapping_alloc(uint32_t size, int32_t align) { void *MemoryMapping_alloc(uint32_t size, int32_t align) {
return MemoryMapping_allocEx(size, align, false); return MemoryMapping_allocEx(size, align, false);
} }
@ -710,7 +470,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) {
@ -723,7 +483,7 @@ void MemoryMapping_free(void *ptr) {
} }
} }
OSMemoryBarrier(); OSMemoryBarrier();
//OSUnlockMutex(&allocMutex); OSUnlockMutex(&allocMutex);
} }
uint32_t MemoryMapping_MEMGetAllocatableSize() { uint32_t MemoryMapping_MEMGetAllocatableSize() {
@ -731,7 +491,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) {
@ -743,12 +503,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) {
@ -758,12 +518,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;
@ -780,11 +540,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;
@ -796,7 +556,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) {

View File

@ -201,8 +201,6 @@ void MemoryMapping_readTestValuesFromMemory();
void MemoryMapping_searchEmptyMemoryRegions(); void MemoryMapping_searchEmptyMemoryRegions();
void MemoryMapping_checkHeaps();
void *MemoryMapping_alloc(uint32_t size, int32_t align); void *MemoryMapping_alloc(uint32_t size, int32_t align);
void *MemoryMapping_allocVideoMemory(uint32_t size, int32_t align); void *MemoryMapping_allocVideoMemory(uint32_t size, int32_t align);