From 771ffca3290bf98dfa0da3a8b8ed349e5f0cb211 Mon Sep 17 00:00:00 2001 From: Prakxo Date: Mon, 6 Mar 2023 20:27:17 +0100 Subject: [PATCH] =?UTF-8?q?=C3=B1match=20malloc,link=20memtrk?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/dol_slices.yml | 183 ++++++++++++++------------- config/symbols.yml | 12 +- include/dolphin/os/__ppc_eabi_init.h | 8 +- include/libc64/osmalloc.h | 40 ++++++ include/libultra/osMesg.h | 18 +++ include/libultra/osThread.h | 5 +- include/libultra/u64types.h | 2 + src/TRK/mem_TRK.c | 14 +- src/dolphin/os/__start.c | 26 ++-- src/libc64/malloc.c | 25 ++++ tools/ppcdis | 2 +- 11 files changed, 212 insertions(+), 123 deletions(-) create mode 100644 include/libc64/osmalloc.h create mode 100644 include/libultra/osMesg.h create mode 100644 src/libc64/malloc.c diff --git a/config/dol_slices.yml b/config/dol_slices.yml index 3183856f..a877d81d 100644 --- a/config/dol_slices.yml +++ b/config/dol_slices.yml @@ -1,90 +1,93 @@ -#dolphin/os/__start.c: -# .init: [0x80003100, 0x80003354] can't link because there's two mains -#dolphin/os/__ppc_eabi_init.cpp: -# .init: [0x80003354, 0x800033a8] -# .text: [0x8007FDFC, 0x8007fe90] -runtime/__mem.c: - .init: [0x800033a8, 0x800034e0] -#TRK/mem_TRK.c: -# .init: [0x800034e0, 0x80003510] -asm/__exception.s: - .init: [0x80003534, 0x80005468] -jaudio_NES/dummyprobe.c: - .text: [0x800083f8, 0x80008400] -#jaudio_NES/verysimple.c: -# .text: [0x80008400, 0x80008480] -# .sdata: [0x80217b80, 0x80217b88] -libforest/ReconfigBATs.c: - .text: [0x8005adac, 0x8005aed4] -#libu64/debug.c: //not match -# .text: [0x8005aed4, 0x8005af30] -# .data: [0x800dc7c8, 0x800dc7f0] -#libu64/gfxprint.c: specify ranges later -libc64/aprintf.c: - .text: [0x8005cbdc, 0x8005cc14] -#libc64/math64.c: //not match -# .text: [0x8005cc14, 0x8005cccc] -# .sdata2: [0x80219118, 0x80219130] -libc64/qrand.c: - .text: [0x8005cccc, 0x8005cd64] - .sdata: [0x80217de8, 0x80217df0] - .sbss: [0x80218640, 0x80218648] - .sdata2: [0x80219130, 0x80219138] -#libultra/ultra.c: won't link -# .text: [0x8005d01c, 0x8005d15c] -# .bss: [0x80206f60, 0x80206fa0] -#libultra/gu/scale.c: -# .text: [0x8005e7d4, 0x8005e860] -# .sdata2: [0x80219208, 0x80219210] -#libultra/gu/translate.c: -# .text: [0x8005e8ac, 0x8005e918] -libultra/gu/sins.c: - .text: [0x8005e860, 0x8005e8ac] - .data: [0x800dd360, 0x800ddb60] -#libultra/xldtob.c: -# .text: [0x8005e918, 0x8005f2a0] -# .rodata: [0x800ab110, 0x800ab158] -# .sdata: [0x80217df8, 0x80217e08] -# .sdata2: [0x80219210, 0x80219230] -#libultra/xlitob.c: -# .text: [0x8005f2a0, 0x8005f4cc] -# .data: [0x800ddb60, 0x800ddb88] -#libultra/xprintf.c: -# .text: [0x8005f4cc, 0x8005ff74] -# .rodata: [0x800ab158, 0x800ab170] -# .data: [0x800ddb88, 0x800ddd20] -# .sdata: [0x80217e08, 0x80217e10] -# .sdata2: [0x80219230, 0x80219238] -JSystem/JKernel/JKRHeap.cpp: - .text: [0x80063748, 0x80064028] - .data: [0x800ddf20, 0x800ddf98] - .sdata: [0x80217e58, 0x80217e80] - .sbss: [0x802186d8, 0x80218700] -JSystem/JKernel/JKRDisposer.cpp: - .text: [0x80065aa0, 0x80065b8c] - .data: [0x800de3a8, 0x800de3c0] - .sdata: [0x80217ec0, 0x80217ec8] -dolphin/BASE/ppcarch.c: - .text: [0x8007867c, 0x80078718] -dolphin/os/OSArena.c: - .text: [0x8007988c, 0x800798ac] - .sdata: [0x80218178, 0x80218180] - .sbss: [0x802188f8, 0x80218900] -#dolphin/os/OSCache.c: -# .text: [0x80079b40, 0x8007a01c] -# .data: [0x800dfa00, 0x800dfc30] -#dolphin/os/OSDisableInterrupts.c: - # .text: [0x8007ac24, 0x8007ac38] -dolphin/os/OSEnableInterrupts.c: - .text: [0x8007ac38, 0x8007ac4c] -dolphin/os/OSRestoreInterrupts.c: - .text: [0x8007ac4c, 0x8007ac70] -MSL_C/rand.c: - .text: [0x8009f46c, 0x8009f494] - .sdata: [0x80218260, 0x80218268] -dolphin/odenotstub/odenotstub.c: - .text: [0x800a9770, 0x800a9780] -dolphin/amcstubs/AmcExi2Stubs.c: - .text: [0x800a8cc0, 0x800a8cf0] -dolphin/gx/GXStubs.c: - .text: [0x800998d4, 0x800998d8] \ No newline at end of file +#dolphin/os/__start.c: +# .init: [0x80003100, 0x80003354] +#dolphin/os/__ppc_eabi_init.cpp: +# .init: [0x80003354, 0x800033a8] +# .text: [0x8007FDFC, 0x8007fe90] +runtime/__mem.c: + .init: [0x800033a8, 0x800034e0] +TRK/mem_TRK.c: + .init: [0x800034e0, 0x80003534] +asm/__exception.s: + .init: [0x80003534, 0x80005468] +jaudio_NES/dummyprobe.c: + .text: [0x800083f8, 0x80008400] +#jaudio_NES/verysimple.c: +# .text: [0x80008400, 0x80008480] +# .sdata: [0x80217b80, 0x80217b88] +libforest/ReconfigBATs.c: + .text: [0x8005adac, 0x8005aed4] +#libu64/debug.c: //not match +# .text: [0x8005aed4, 0x8005af30] +# .data: [0x800dc7c8, 0x800dc7f0] +#libu64/gfxprint.c: specify ranges later +libc64/aprintf.c: + .text: [0x8005cbdc, 0x8005cc14] +#libc64/math64.c: //not match +# .text: [0x8005cc14, 0x8005cccc] +# .sdata2: [0x80219118, 0x80219130] +libc64/qrand.c: + .text: [0x8005cccc, 0x8005cd64] + .sdata: [0x80217de8, 0x80217df0] + .sbss: [0x80218640, 0x80218648] + .sdata2: [0x80219130, 0x80219138] +#libc64/malloc.c: +# .text: [0x8005cf08, 0x8005d01c] +# .bss: [0x80206F30, 0x80206F60] +#libultra/ultra.c: won't link +# .text: [0x8005d01c, 0x8005d15c] +# .bss: [0x80206f60, 0x80206fa0] +#libultra/gu/scale.c: +# .text: [0x8005e7d4, 0x8005e860] +# .sdata2: [0x80219208, 0x80219210] +#libultra/gu/translate.c: +# .text: [0x8005e8ac, 0x8005e918] +libultra/gu/sins.c: + .text: [0x8005e860, 0x8005e8ac] + .data: [0x800dd360, 0x800ddb60] +#libultra/xldtob.c: +# .text: [0x8005e918, 0x8005f2a0] +# .rodata: [0x800ab110, 0x800ab158] +# .sdata: [0x80217df8, 0x80217e08] +# .sdata2: [0x80219210, 0x80219230] +#libultra/xlitob.c: +# .text: [0x8005f2a0, 0x8005f4cc] +# .data: [0x800ddb60, 0x800ddb88] +#libultra/xprintf.c: +# .text: [0x8005f4cc, 0x8005ff74] +# .rodata: [0x800ab158, 0x800ab170] +# .data: [0x800ddb88, 0x800ddd20] +# .sdata: [0x80217e08, 0x80217e10] +# .sdata2: [0x80219230, 0x80219238] +JSystem/JKernel/JKRHeap.cpp: + .text: [0x80063748, 0x80064028] + .data: [0x800ddf20, 0x800ddf98] + .sdata: [0x80217e58, 0x80217e80] + .sbss: [0x802186d8, 0x80218700] +JSystem/JKernel/JKRDisposer.cpp: + .text: [0x80065aa0, 0x80065b8c] + .data: [0x800de3a8, 0x800de3c0] + .sdata: [0x80217ec0, 0x80217ec8] +dolphin/BASE/ppcarch.c: + .text: [0x8007867c, 0x80078718] +dolphin/os/OSArena.c: + .text: [0x8007988c, 0x800798ac] + .sdata: [0x80218178, 0x80218180] + .sbss: [0x802188f8, 0x80218900] +#dolphin/os/OSCache.c: +# .text: [0x80079b40, 0x8007a01c] +# .data: [0x800dfa00, 0x800dfc30] +#dolphin/os/OSDisableInterrupts.c: + # .text: [0x8007ac24, 0x8007ac38] +dolphin/os/OSEnableInterrupts.c: + .text: [0x8007ac38, 0x8007ac4c] +dolphin/os/OSRestoreInterrupts.c: + .text: [0x8007ac4c, 0x8007ac70] +MSL_C/rand.c: + .text: [0x8009f46c, 0x8009f494] + .sdata: [0x80218260, 0x80218268] +dolphin/odenotstub/odenotstub.c: + .text: [0x800a9770, 0x800a9780] +dolphin/amcstubs/AmcExi2Stubs.c: + .text: [0x800a8cc0, 0x800a8cf0] +dolphin/gx/GXStubs.c: + .text: [0x800998d4, 0x800998d8] diff --git a/config/symbols.yml b/config/symbols.yml index 05ff9110..6bc498ec 100644 --- a/config/symbols.yml +++ b/config/symbols.yml @@ -28,6 +28,7 @@ global: 0x80005cf0: fault_callback_vimode 0x80005dc0: fault_callback_scroll 0x80006004: adjustOSArena + 0x800060f8: main 0x8000663c: ReportDiskID__Fv 0x8000665c: JW_UpdateVideoMode 0x800067b4: JW_SetProgressiveMode @@ -7895,6 +7896,7 @@ global: 0x80407A78: lbRk_HarvestMoonDay 0x80407AE8: mainproc 0x80407C70: entry + 0x80407CB9: foresta_main 0x80407CF4: padmgr_LockSerialMesgQ 0x80407D30: padmgr_UnlockSerialMesgQ 0x80407D64: padmgr_LockContData @@ -52864,10 +52866,10 @@ src/dvdfs.c: 0x802189d8: BootInfo src/OSResetSW.c: 0x80218938: LastState -src/boot.c: - 0x800060f8: main -rel/main.c: - 0x80407CB9: main +#src/boot.c: +# 0x800060f8: main +#rel/main.c: +# 0x80407CB9: main rel/ac_quest_talk_island.c: 0x8048D769: aQMgr_init_normal_info 0x8048D7A5: aQMgr_set_number_free_str @@ -58146,4 +58148,4 @@ rel/irqmgr.c: 0x812F357C: this foresta.rel: 0x80641254: _ctors - 0x80641258: _dtors \ No newline at end of file + 0x80641258: _dtors diff --git a/include/dolphin/os/__ppc_eabi_init.h b/include/dolphin/os/__ppc_eabi_init.h index 6e4438e3..8db568de 100644 --- a/include/dolphin/os/__ppc_eabi_init.h +++ b/include/dolphin/os/__ppc_eabi_init.h @@ -33,11 +33,11 @@ typedef struct ExtabIndexInfo { u32 codeSize; // at 0x10 } ExtabIndexInfo; -__declspec(section ".init") extern const RomSection _rom_copy_info[]; -__declspec(section ".init") extern const BssSection _bss_init_info[]; -__declspec(section ".init") extern const ExtabIndexInfo _eti_init_info[]; +__declspec(section ".init") extern RomSection _rom_copy_info[]; +__declspec(section ".init") extern BssSection _bss_init_info[]; +__declspec(section ".init") extern ExtabIndexInfo _eti_init_info[]; #ifdef __cplusplus } #endif -#endif \ No newline at end of file +#endif diff --git a/include/libc64/osmalloc.h b/include/libc64/osmalloc.h new file mode 100644 index 00000000..f7671586 --- /dev/null +++ b/include/libc64/osmalloc.h @@ -0,0 +1,40 @@ +#ifndef OS_MALLOC_H +#define OS_MALLOC_H +#include "types.h" +#include "libultra/osThread.h" +#include "libultra/osMesg.h" +#include "libultra/u64types.h" + +struct ArenaNode; + +typedef struct Arena { + /* 0x00 */ struct ArenaNode* head; + /* 0x04 */ void* start; + /* 0x08 */ OSMesgQueue lockQueue; + /* 0x20 */ u8 unk_20; + /* 0x21 */ u8 isInit; + /* 0x22 */ u8 flag; +} Arena; // size = 0x24 + +typedef struct ArenaNode { + /* 0x00 */ s16 magic; + /* 0x02 */ s16 isFree; + /* 0x04 */ u32 size; + /* 0x08 */ struct ArenaNode* next; + /* 0x0C */ struct ArenaNode* prev; + /* 0x10 */ const char* filename; + /* 0x14 */ s32 line; + /* 0x18 */ OSId threadId; + /* 0x1C */ Arena* arena; + /* 0x20 */ OSTime time; + /* 0x28 */ u8 unk_28[0x30-0x28]; // probably padding +} ArenaNode; // size = 0x30 + +void* __osMalloc(Arena*, size_t); +void __osFree(Arena*, void*); +void __osDisplayArena(Arena*); +void __osGetFreeArena(Arena*, u32*, u32*, u32*); +void __osMallocInit(Arena*, void*, u32); +void __osMallocCleanup(Arena*); + +#endif diff --git a/include/libultra/osMesg.h b/include/libultra/osMesg.h new file mode 100644 index 00000000..d2b960ed --- /dev/null +++ b/include/libultra/osMesg.h @@ -0,0 +1,18 @@ +#ifndef OS_MESG_H +#define OS_MESG_H + +#include "types.h" +#include "dolphin/os/OSThread.h" + +typedef void* OSMesg; + +typedef struct OSMesgQueue { + /* 0x00 */ OSThread* mtqueue; + /* 0x04 */ OSThread* fullqueue; + /* 0x08 */ s32 validCount; + /* 0x0C */ s32 first; + /* 0x10 */ s32 msgCount; + /* 0x14 */ OSMesg* msg; +} OSMesgQueue; // size = 0x18 + +#endif diff --git a/include/libultra/osThread.h b/include/libultra/osThread.h index afa36f3c..77405b67 100644 --- a/include/libultra/osThread.h +++ b/include/libultra/osThread.h @@ -2,7 +2,8 @@ #define LOS_THREAD_H #include "types.h" -#include "dolphin/OSThread.h" +#include "dolphin/os/OSThread.h" + typedef s32 OSPri; typedef s32 OSId; @@ -10,4 +11,4 @@ typedef s32 OSId; OSId osGetThreadId(OSThread*); -#endif \ No newline at end of file +#endif diff --git a/include/libultra/u64types.h b/include/libultra/u64types.h index 3ede45c3..a13de36c 100644 --- a/include/libultra/u64types.h +++ b/include/libultra/u64types.h @@ -3,6 +3,8 @@ #include "types.h" +typedef u64 OSTime; + typedef struct { u32 r:8; u32 g:8; diff --git a/src/TRK/mem_TRK.c b/src/TRK/mem_TRK.c index 5db5a47c..61b9da61 100644 --- a/src/TRK/mem_TRK.c +++ b/src/TRK/mem_TRK.c @@ -6,13 +6,13 @@ __declspec(section ".init") void* TRK_memset(void* dst, int val, size_t size){ return dst; } -__declspec(section ".init") void* TRK_memcpy(void* dst, const void* src, size_t size){ - const u8* s = (const u8*)src -1; - u8* d = (u8*)dst -1; +__declspec(section ".init") void *TRK_memcpy(void *dst, const void *src, size_t n) +{ + const unsigned char *s = (const unsigned char *)src - 1; + unsigned char *d = (unsigned char *)dst - 1; - size++; - while(--size != 0){ + n++; + while (--n != 0) *++d = *++s; return dst; - } -} \ No newline at end of file +} diff --git a/src/dolphin/os/__start.c b/src/dolphin/os/__start.c index 43ec7564..7dd94f2f 100644 --- a/src/dolphin/os/__start.c +++ b/src/dolphin/os/__start.c @@ -6,6 +6,8 @@ __declspec(section ".init")void __init_registers(void); __declspec(section ".init")void __init_data(void); +int main(int argc, char **argv); + __declspec(section ".init") void __check_pad3(void){ if((*(u16*)0x800030E4 & 0xEEF) == 0xEEF){ OSResetSystem(0,0,0); @@ -117,26 +119,22 @@ __declspec(section ".init") asm void __init_registers(void){ ori r13, r13, 0xFB80 blr } - -__declspec(section ".init") static void __copy_rom_section(void* dst, const void* src, size_t size){ - if (size == 0 || dst == src){ - return; - } - +void __copy_rom_section(void* dst, const void* src, size_t size){ + if (size && (dst != src)) { memcpy(dst, src, size); - (dst, size); + __flush_cache(dst, size); + } } -__declspec(section ".init") static void __init_bss_section(void* dst, size_t size){ - if(size == 0){ - return; - } +void __init_bss_section(void* dst, size_t size){ + if (size) { memset(dst, 0, size); + } } __declspec(section ".init") static void __init_data(void){ - const RomSection* rs; - const BssSection* bs; + RomSection* rs; + BssSection* bs; rs = _rom_copy_info; while (1) { @@ -157,4 +155,4 @@ __declspec(section ".init") static void __init_data(void){ __init_bss_section(bs->virtualOfs, bs->size); bs++; } -} \ No newline at end of file +} diff --git a/src/libc64/malloc.c b/src/libc64/malloc.c new file mode 100644 index 00000000..6d577cad --- /dev/null +++ b/src/libc64/malloc.c @@ -0,0 +1,25 @@ +#include "libc64/osmalloc.h" + +extern Arena malloc_arena; + +void* malloc(size_t size) { + return(__osMalloc(&malloc_arena, size)); +} +void free(void* ptr){ + __osFree(&malloc_arena,ptr); +} + +void DisplayArena(void){ + __osDisplayArena(&malloc_arena); +} + +void GetFreeArena(u32* max, u32* free, u32* alloc){ + __osGetFreeArena(&malloc_arena, max, free, alloc); +} +void MallocInit(void* start, u32 size){ + __osMallocInit(&malloc_arena, start, size); +} + +void MallocCleanup(void){ + __osMallocCleanup(&malloc_arena); +} diff --git a/tools/ppcdis b/tools/ppcdis index eca3f301..83260829 160000 --- a/tools/ppcdis +++ b/tools/ppcdis @@ -1 +1 @@ -Subproject commit eca3f3015a86bf0c78d1e415aeff2dda1851194f +Subproject commit 83260829c50f37dd9fe54889cf8971c8fe14c54e