diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..92399ef --- /dev/null +++ b/.clang-format @@ -0,0 +1,66 @@ +# Generated from CLion C/C++ Code Style settings +BasedOnStyle: LLVM +AccessModifierOffset: -4 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: None +AlignOperands: Align +AllowAllArgumentsOnNextLine: false +AllowAllConstructorInitializersOnNextLine: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: Always +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Always +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: true +AlwaysBreakAfterReturnType: None +AlwaysBreakTemplateDeclarations: Yes +BreakBeforeBraces: Custom +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: false + SplitEmptyRecord: true +BreakBeforeBinaryOperators: None +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeColon +BreakInheritanceList: BeforeColon +ColumnLimit: 0 +CompactNamespaces: false +ContinuationIndentWidth: 8 +IndentCaseLabels: true +IndentPPDirectives: None +IndentWidth: 4 +KeepEmptyLinesAtTheStartOfBlocks: true +MaxEmptyLinesToKeep: 2 +NamespaceIndentation: All +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PointerAlignment: Right +ReflowComments: false +SpaceAfterCStyleCast: true +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: false +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 0 +SpacesInAngles: false +SpacesInCStyleCastParentheses: false +SpacesInContainerLiterals: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +TabWidth: 4 +UseTab: Never diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2bd293b..a83349c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,8 +6,16 @@ on: - master jobs: + clang-format: + runs-on: ubuntu-18.04 + steps: + - uses: actions/checkout@v2 + - name: clang-format + run: | + docker run --rm -v ${PWD}:/src wiiuenv/clang-format:13.0.0-2 -r ./source build-binary: runs-on: ubuntu-18.04 + needs: clang-format steps: - uses: actions/checkout@v2 - name: build binary diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 0413d45..19c3b14 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -3,8 +3,16 @@ name: CI-PR on: [pull_request] jobs: + clang-format: + runs-on: ubuntu-18.04 + steps: + - uses: actions/checkout@v2 + - name: clang-format + run: | + docker run --rm -v ${PWD}:/src wiiuenv/clang-format:13.0.0-2 -r ./source build-binary: runs-on: ubuntu-18.04 + needs: clang-format steps: - uses: actions/checkout@v2 - name: build binary diff --git a/README.md b/README.md index 293382b..8f845ff 100644 --- a/README.md +++ b/README.md @@ -17,4 +17,7 @@ docker run -it --rm -v ${PWD}:/project memorymappingmodule-builder make # make clean docker run -it --rm -v ${PWD}:/project memorymappingmodule-builder make clean -``` \ No newline at end of file +``` + +## Format the code via docker +`docker run --rm -it -v ${PWD}:/src wiiuenv/clang-format:13.0.0-2 -r ./source -i` \ No newline at end of file diff --git a/source/CThread.h b/source/CThread.h index d5e8194..063b52d 100644 --- a/source/CThread.h +++ b/source/CThread.h @@ -16,10 +16,10 @@ ****************************************************************************/ #pragma once +#include +#include #include #include -#include -#include class CThread { public: @@ -27,7 +27,7 @@ public: //! constructor 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 iAttributes = iAttr; //! allocate the thread @@ -36,7 +36,9 @@ public: pThreadStack = (uint8_t *) memalign(0x20, iStackSize); //! create the thread if (pThread && pThreadStack) { - OSCreateThread(pThread, (int (*)(int, const char **)) &CThread::threadCallback, 1, (char *) this, (void *) (pThreadStack + iStackSize), iStackSize, iPriority, iAttributes); + // clang-format off + OSCreateThread(pThread, (int(*)(int, const char **)) & CThread::threadCallback, 1, (char *) this, (void *) (pThreadStack + iStackSize), iStackSize, iPriority, iAttributes); + // clang-format on } } @@ -129,6 +131,7 @@ public: eAttributeDetach = 0x08, eAttributePinnedAff = 0x10 }; + private: static int32_t threadCallback(int32_t argc, void *arg) { //! After call to start() continue with the internal function diff --git a/source/function_replacements.cpp b/source/function_replacements.cpp index d9a2fd8..ca7cea0 100644 --- a/source/function_replacements.cpp +++ b/source/function_replacements.cpp @@ -31,7 +31,6 @@ DECL_FUNCTION(uint32_t, KiPhysicalToEffectiveUncached, uint32_t addressSpace, ui uint32_t result = real_KiPhysicalToEffectiveUncached(addressSpace, virtualAddress); if (result == 0) { return MemoryMapping_PhysicalToEffective(virtualAddress); - } return result; } @@ -54,10 +53,12 @@ DECL_FUNCTION(uint32_t, KiIsEffectiveRangeValid, uint32_t addressSpace, uint32_t } -#define k_memcpy ((void (*)( void*, void*,uint32_t))(0xfff09e44)) +// clang-format off +#define k_memcpy ((void(*)(void *, void *, uint32_t))(0xfff09e44)) +// clang-format on DECL_FUNCTION(uint32_t, KiGetOrPutUserData, void *src, uint32_t size, void *dst, bool isRead) { - // + // if (isRead && MemoryMapping_EffectiveToPhysical((uint32_t) src) > 0) { k_memcpy(dst, src, size); return 1; @@ -70,6 +71,7 @@ DECL_FUNCTION(uint32_t, KiGetOrPutUserData, void *src, uint32_t size, void *dst, return real_KiGetOrPutUserData(src, size, dst, isRead); } +// clang-format off function_replacement_data_t function_replacements[] __attribute__((section(".data"))) = { REPLACE_FUNCTION_VIA_ADDRESS(sCheckDataRange, 0x3200cf60, 0x0100cf60), REPLACE_FUNCTION_VIA_ADDRESS(KiEffectiveToPhysical, 0xffee0aac, 0xffee0aac), @@ -79,5 +81,6 @@ function_replacement_data_t function_replacements[] __attribute__((section(".dat REPLACE_FUNCTION_VIA_ADDRESS(IPCKDriver_ValidatePhysicalAddress, 0xfff0cb5c, 0xfff0cb5c), REPLACE_FUNCTION_VIA_ADDRESS(KiGetOrPutUserData, 0xffee0794, 0xffee0794), }; +// clang-format on uint32_t function_replacements_size __attribute__((section(".data"))) = sizeof(function_replacements) / sizeof(function_replacement_data_t); \ No newline at end of file diff --git a/source/function_replacements.h b/source/function_replacements.h index 9d0b3d9..2c4aa73 100644 --- a/source/function_replacements.h +++ b/source/function_replacements.h @@ -1,7 +1,7 @@ #pragma once -#include #include +#include extern function_replacement_data_t function_replacements[] __attribute__((section(".data"))); diff --git a/source/logger.c b/source/logger.c index 0411db7..0ed05e6 100644 --- a/source/logger.c +++ b/source/logger.c @@ -1,13 +1,13 @@ #ifdef DEBUG #include -#include #include #include +#include uint32_t moduleLogInit = false; uint32_t cafeLogInit = false; uint32_t udpLogInit = false; -#endif // DEBUG +#endif// DEBUG void initLogging() { #ifdef DEBUG @@ -15,7 +15,7 @@ void initLogging() { cafeLogInit = WHBLogCafeInit(); udpLogInit = WHBLogUdpInit(); } -#endif // DEBUG +#endif// DEBUG } void deinitLogging() { @@ -32,5 +32,5 @@ void deinitLogging() { WHBLogUdpDeinit(); udpLogInit = false; } -#endif // DEBUG +#endif// DEBUG } \ No newline at end of file diff --git a/source/logger.h b/source/logger.h index 6ff5ae2..2321b2b 100644 --- a/source/logger.h +++ b/source/logger.h @@ -1,7 +1,7 @@ #pragma once -#include #include +#include #ifdef __cplusplus extern "C" { @@ -14,12 +14,14 @@ extern "C" { #define DEBUG_FUNCTION_LINE_VERBOSE(FMT, ARGS...) while (0) -#define DEBUG_FUNCTION_LINE(FMT, ARGS...)do { \ - WHBLogPrintf("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ +#define DEBUG_FUNCTION_LINE(FMT, ARGS...) \ + do { \ + WHBLogPrintf("[%23s]%30s@L%04d: " FMT "", __FILENAME__, __FUNCTION__, __LINE__, ##ARGS); \ } while (0) -#define DEBUG_FUNCTION_LINE_WRITE(FMT, ARGS...)do { \ - WHBLogWritef("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ +#define DEBUG_FUNCTION_LINE_WRITE(FMT, ARGS...) \ + do { \ + WHBLogWritef("[%23s]%30s@L%04d: " FMT "", __FILENAME__, __FUNCTION__, __LINE__, ##ARGS); \ } while (0) #else @@ -39,4 +41,3 @@ void deinitLogging(); #ifdef __cplusplus } #endif - diff --git a/source/main.cpp b/source/main.cpp index 336fcb1..1feb6a3 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,8 +1,8 @@ -#include -#include "memory_mapping.h" -#include #include "function_replacements.h" #include "logger.h" +#include "memory_mapping.h" +#include +#include WUMS_MODULE_EXPORT_NAME("homebrew_memorymapping"); WUMS_MODULE_SKIP_INIT_FINI(); @@ -61,10 +61,10 @@ void *MemoryMappingAllocForGX2Ex(uint32_t size, int32_t align) { return res; } -uint32_t MEMAllocFromMappedMemory __attribute__((__section__ (".data"))) = (uint32_t) MemoryMappingAlloc; -uint32_t MEMAllocFromMappedMemoryEx __attribute__((__section__ (".data"))) = (uint32_t) MemoryMappingAllocEx; -uint32_t MEMAllocFromMappedMemoryForGX2Ex __attribute__((__section__ (".data"))) = (uint32_t) MemoryMappingAllocForGX2Ex; -uint32_t MEMFreeToMappedMemory __attribute__((__section__ (".data"))) = (uint32_t) MemoryMappingFree; +uint32_t MEMAllocFromMappedMemory __attribute__((__section__(".data"))) = (uint32_t) MemoryMappingAlloc; +uint32_t MEMAllocFromMappedMemoryEx __attribute__((__section__(".data"))) = (uint32_t) MemoryMappingAllocEx; +uint32_t MEMAllocFromMappedMemoryForGX2Ex __attribute__((__section__(".data"))) = (uint32_t) MemoryMappingAllocForGX2Ex; +uint32_t MEMFreeToMappedMemory __attribute__((__section__(".data"))) = (uint32_t) MemoryMappingFree; WUMS_EXPORT_FUNCTION(MemoryMappingEffectiveToPhysical); WUMS_EXPORT_FUNCTION(MemoryMappingPhysicalToEffective); diff --git a/source/memory_mapping.cpp b/source/memory_mapping.cpp index c90fda4..c7542d6 100644 --- a/source/memory_mapping.cpp +++ b/source/memory_mapping.cpp @@ -1,22 +1,22 @@ #include "memory_mapping.h" -#include -#include #include +#include #include +#include #include -#include -#include "memory.h" -#include "logger.h" #include "CThread.h" +#include "logger.h" +#include "memory.h" #include +#include // #define DEBUG_FUNCTION_LINE(x,...) 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}; - for (int i: aff) { + for (int i : aff) { CThread thread(iAttr | i, iPriority, iStackSize, callback, callbackArg); thread.resumeThread(); } @@ -159,7 +159,6 @@ void MemoryMapping_searchEmptyMemoryRegions() { if (success) { DEBUG_FUNCTION_LINE("Test %d was successful!", i + 1); } - } DEBUG_FUNCTION_LINE("All tests done."); } @@ -274,7 +273,6 @@ void MemoryMapping_readTestValuesFromMemory() { if (success) { DEBUG_FUNCTION_LINE("Test %d was successful!", i + 1); } - } DEBUG_FUNCTION_LINE("All tests done."); } @@ -524,7 +522,7 @@ uint32_t MemoryMapping_getAreaSizeFromPageTable(uint32_t start, uint32_t maxSize cur_end_addr = (segment + 1) * 0x10000000; } if (segment != sr_start) { - cur_address = (segment) * 0x10000000; + cur_address = (segment) *0x10000000; } bool success = true; for (uint32_t addr = cur_address; addr < cur_end_addr; addr += pageSize) { @@ -645,8 +643,7 @@ void MemoryMapping_printPageTableTranslation(sr_table_t srTable, uint32_t *trans current.kp == kp && current.nx == nx && current.pp == pp && - current.phys == phys - current.size - ) { + current.phys == phys - current.size) { current.size += pageSize; //DEBUG_FUNCTION_LINE("New size of %08X is %08X",current.addr,current.size); } else { @@ -681,7 +678,7 @@ void MemoryMapping_printPageTableTranslation(sr_table_t srTable, uint32_t *trans const char *access1[] = {"read/write", "read/write", "read/write", "read only"}; const char *access2[] = {"no access", "read only", "read/write", "read only"}; - for (auto cur: pageInfos) { + for (auto cur : pageInfos) { DEBUG_FUNCTION_LINE_VERBOSE("%08X %08X -> %08X %08X. user access %s. supervisor access %s. %s", cur.addr, cur.addr + cur.size, cur.phys, cur.phys + cur.size, cur.kp ? access2[cur.pp] : access1[cur.pp], cur.ks ? access2[cur.pp] : access1[cur.pp], cur.nx ? "not executable" : "executable"); @@ -879,5 +876,3 @@ uint32_t MemoryMapping_EffectiveToPhysical(uint32_t effectiveAddress) { } return result; } - - diff --git a/source/memory_mapping.h b/source/memory_mapping.h index c42bdd9..cf5bdc4 100644 --- a/source/memory_mapping.h +++ b/source/memory_mapping.h @@ -6,9 +6,9 @@ extern "C" { #endif -#include #include #include +#include typedef struct pageInformation_ { uint32_t addr; @@ -31,47 +31,58 @@ typedef struct _memory_mapping_t { const memory_values_t *physical_addresses; } memory_mapping_t; +// clang-format off #define SEGMENT_UNIQUE_ID 0x00AABBCC // Unique ID. Chosen arbitrary. #define PAGE_INDEX_SHIFT (32-15) #define PAGE_INDEX_MASK ((1 << (28 - PAGE_INDEX_SHIFT)) - 1) -#define MEMORY_START_BASE 0x80000000 -#define MEMORY_START_VIDEO_BASE (MEMORY_START_BASE + 0x08000000) +#define MEMORY_START_BASE 0x80000000 +#define MEMORY_START_VIDEO_BASE (MEMORY_START_BASE + 0x08000000) +// clang-format on +// clang-format off const memory_values_t mem_vals_heap_1[] = { {0x28000000 + 0x06620000, 0x28000000 + 0x07F80000}, // size: 25984 kB {0, 0} }; +// clang-format on #define MEMORY_HEAP0_SIZE 0x1960000 #define MEMORY_HEAP0 MEMORY_START_BASE +// clang-format off const memory_values_t mem_vals_heap_2[] = { {0x28000000 + 0x09000000, 0x28000000 + 0x09E20000}, // size: 14464 kB {0, 0} }; +// clang-format on #define MEMORY_HEAP1_SIZE 0xE20000 #define MEMORY_HEAP1 (MEMORY_HEAP0 + MEMORY_HEAP0_SIZE) +// clang-format off const memory_values_t mem_vals_heap_3[] = { {0x28000000 + 0x058E0000, 0x28000000 + 0x06000000}, // size: 7296 kB {0, 0} }; +// clang-format on #define MEMORY_HEAP2_SIZE 0x720000 #define MEMORY_HEAP2 (MEMORY_HEAP1 + MEMORY_HEAP1_SIZE) +// clang-format off const memory_values_t mem_vals_heap_4[] = { {0x28000000 + 0x053C0000, 0x28000000 + 0x05880000}, // size: 4864 kB {0, 0} }; +// clang-format on #define MEMORY_HEAP3_SIZE 0x4C0000 #define MEMORY_HEAP3 (MEMORY_HEAP2 + MEMORY_HEAP2_SIZE) #define MEMORY_HEAP4 (MEMORY_HEAP3 + MEMORY_HEAP3_SIZE) +// clang-format off const memory_values_t mem_vals_video[] = { // The GPU doesn't have access to the 0x28000000 - 0x32000000 area, so we need memory from somewhere else. // From the SharedReadHeap of the loader. @@ -105,10 +116,14 @@ const memory_values_t mem_vals_video[] = { //{0x18000000 , 0x18000000 +0x3000000}, // size: 3840 kB {0, 0} }; +// clang-format on +// clang-format off #define MEMORY_START_VIDEO MEMORY_START_VIDEO_BASE #define MEMORY_END_VIDEO (MEMORY_START_VIDEO + 0xE60000) +// clang-format on +// clang-format off const memory_mapping_t mem_mapping[] = { {MEMORY_HEAP0, MEMORY_HEAP1, mem_vals_heap_1}, {MEMORY_HEAP1, MEMORY_HEAP2, mem_vals_heap_2}, @@ -117,8 +132,9 @@ const memory_mapping_t mem_mapping[] = { {MEMORY_START_VIDEO, MEMORY_END_VIDEO, mem_vals_video}, {0, 0, nullptr} }; +// clang-format on - +// clang-format off // Values needs to be aligned to 0x20000 and size needs to be a multiple of 0x20000 const memory_values_t mem_vals_heap[] = { // 5.5.2 EUR @@ -162,6 +178,7 @@ const memory_values_t mem_vals_heap[] = { {0, 0} }; +// clang-format on uint32_t MemoryMapping_MEMGetAllocatableSize();