From 79f9ebc2e11bcacc7915b4aee712c19db43b32ad Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Tue, 1 Jan 2013 18:08:42 -0800 Subject: [PATCH 1/5] Keep track of exported symbols and link them later. --- Core/HLE/HLE.cpp | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/Core/HLE/HLE.cpp b/Core/HLE/HLE.cpp index 2ebb7b5149..4ed00ba669 100644 --- a/Core/HLE/HLE.cpp +++ b/Core/HLE/HLE.cpp @@ -51,6 +51,7 @@ enum static std::vector moduleDB; static std::vector unresolvedSyscalls; +static std::vector exportedCalls; static int hleAfterSyscall = HLE_AFTER_NOTHING; static char hleAfterSyscallReschedReason[512]; @@ -63,6 +64,7 @@ void HLEDoState(PointerWrap &p) { Syscall sc = {0}; p.Do(unresolvedSyscalls, sc); + p.Do(exportedCalls, sc); p.DoMarker("HLE"); } @@ -71,6 +73,7 @@ void HLEShutdown() hleAfterSyscall = HLE_AFTER_NOTHING; moduleDB.clear(); unresolvedSyscalls.clear(); + exportedCalls.clear(); } void RegisterModule(const char *name, int numFunctions, const HLEFunction *funcTable) @@ -124,15 +127,26 @@ const HLEFunction *GetFunc(const char *moduleName, u32 nib) const char *GetFuncName(const char *moduleName, u32 nib) { + _dbg_assert_msg_(HLE, moduleName != NULL, "Invalid module name."); + const HLEFunction *func = GetFunc(moduleName,nib); if (func) return func->name; - else + + // Was this function exported previously? + static char temp[256]; + for (auto it = exportedCalls.begin(), end = exportedCalls.end(); it != end; ++it) { - static char temp[256]; - sprintf(temp,"[UNK: 0x%08x ]",nib); - return temp; + if (!strcmp(it->moduleName, moduleName) && it->nid == nib) + { + sprintf(temp, "[EXP: 0x%08x]", nib); + return temp; + } } + + // No good, we can't find it. + sprintf(temp,"[UNK: 0x%08x]", nib); + return temp; } u32 GetSyscallOp(const char *moduleName, u32 nib) @@ -173,17 +187,30 @@ void WriteSyscall(const char *moduleName, u32 nib, u32 address) } else { + // Did another module export this already? + for (auto it = exportedCalls.begin(), end = exportedCalls.end(); it != end; ++it) + { + if (!strcmp(it->moduleName, moduleName) && it->nid == nib) + { + Memory::Write_U32(MIPS_MAKE_J(it->symAddr), address); // j symAddr + Memory::Write_U32(MIPS_MAKE_NOP(), address + 4); // nop (delay slot) + return; + } + } + // Module inexistent.. for now; let's store the syscall for it to be resolved later INFO_LOG(HLE,"Syscall (%s,%08x) unresolved, storing for later resolving", moduleName, nib); Syscall sysc = {"", address, nib}; - strncpy(sysc.moduleName, moduleName, 32); - sysc.moduleName[31] = '\0'; + strncpy(sysc.moduleName, moduleName, KERNELOBJECT_MAX_NAME_LENGTH); + sysc.moduleName[KERNELOBJECT_MAX_NAME_LENGTH] = '\0'; unresolvedSyscalls.push_back(sysc); } } void ResolveSyscall(const char *moduleName, u32 nib, u32 address) { + _dbg_assert_msg_(HLE, moduleName != NULL, "Invalid module name."); + for (size_t i = 0; i < unresolvedSyscalls.size(); i++) { Syscall *sysc = &unresolvedSyscalls[i]; @@ -196,6 +223,11 @@ void ResolveSyscall(const char *moduleName, u32 nib, u32 address) Memory::Write_U32(MIPS_MAKE_NOP(), sysc->symAddr + 4); } } + + Syscall ex = {"", address, nib}; + strncpy(ex.moduleName, moduleName, KERNELOBJECT_MAX_NAME_LENGTH); + ex.moduleName[KERNELOBJECT_MAX_NAME_LENGTH] = '\0'; + exportedCalls.push_back(ex); } const char *GetFuncName(int moduleIndex, int func) From 8cf759bbc4f824598dcfba19ba97a1c95106146d Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Fri, 4 Jan 2013 00:36:08 -0800 Subject: [PATCH 2/5] Load the exports that tell us module info. Thanks JPCSP. --- Core/HLE/sceKernelModule.cpp | 89 +++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) diff --git a/Core/HLE/sceKernelModule.cpp b/Core/HLE/sceKernelModule.cpp index 3a5907dd40..8d866d1d4f 100644 --- a/Core/HLE/sceKernelModule.cpp +++ b/Core/HLE/sceKernelModule.cpp @@ -44,6 +44,22 @@ enum { PSP_THREAD_ATTR_USER = 0x80000000 }; +enum { + // Function exports. + NID_MODULE_START = 0xD632ACDB, + NID_MODULE_STOP = 0xCEE8593C, + NID_MODULE_REBOOT_BEFORE = 0x2F064FA6, + NID_MODULE_REBOOT_PHASE = 0xADF12745, + NID_MODULE_BOOTSTART = 0xD3744BE0, + + // Variable exports. + NID_MODULE_INFO = 0xF01D73A7, + NID_MODULE_START_THREAD_PARAMETER = 0x0F7C276C, + NID_MODULE_STOP_THREAD_PARAMETER = 0xCF0CC697, + NID_MODULE_REBOOT_BEFORE_THREAD_PARAMETER = 0xF4F4299D, + NID_MODULE_SDK_VERSION = 0x11B97506, +}; + static const char *blacklistedModules[] = { "sceATRAC3plus_Library", "sceFont_Library", @@ -229,6 +245,7 @@ Module *__KernelLoadELFFromPtr(const u8 *ptr, u32 loadAddress, std::string *erro { Module *module = new Module; kernelObjects.Create(module); + memset(&module->nm, 0, sizeof(module->nm)); u8 *newptr = 0; if (*(u32*)ptr == 0x4543537e) { // "~SCE" @@ -464,8 +481,66 @@ Module *__KernelLoadELFFromPtr(const u8 *ptr, u32 loadAddress, std::string *erro { u32 nid = residentPtr[j]; u32 exportAddr = residentPtr[ent->fcount + ent->vcount + j]; - ResolveSyscall(name, nid, exportAddr); + + switch (nid) + { + case NID_MODULE_START: + module->nm.module_start_func = exportAddr; + break; + case NID_MODULE_STOP: + module->nm.module_stop_func = exportAddr; + break; + case NID_MODULE_REBOOT_BEFORE: + module->nm.module_reboot_before_func = exportAddr; + break; + case NID_MODULE_REBOOT_PHASE: + module->nm.module_reboot_phase_func = exportAddr; + break; + case NID_MODULE_BOOTSTART: + module->nm.module_bootstart_func = exportAddr; + break; + default: + ResolveSyscall(name, nid, exportAddr); + } } + + for (u32 j = 0; j < ent->vcount; j++) + { + u32 nid = residentPtr[ent->fcount + j]; + u32 exportAddr = residentPtr[ent->fcount + ent->vcount + ent->fcount + j]; + + switch (nid) + { + case NID_MODULE_INFO: + // TODO + break; + case NID_MODULE_START_THREAD_PARAMETER: + // TODO: What's at 0? + module->nm.module_start_thread_priority = Memory::Read_U32(exportAddr + 4); + module->nm.module_start_thread_stacksize = Memory::Read_U32(exportAddr + 8); + module->nm.module_start_thread_attr = Memory::Read_U32(exportAddr + 12); + break; + case NID_MODULE_STOP_THREAD_PARAMETER: + // TODO: What's at 0? + module->nm.module_stop_thread_priority = Memory::Read_U32(exportAddr + 4); + module->nm.module_stop_thread_stacksize = Memory::Read_U32(exportAddr + 8); + module->nm.module_stop_thread_attr = Memory::Read_U32(exportAddr + 12); + break; + case NID_MODULE_REBOOT_BEFORE_THREAD_PARAMETER: + // TODO: What's at 0? + module->nm.module_reboot_before_thread_priority = Memory::Read_U32(exportAddr + 4); + module->nm.module_reboot_before_thread_stacksize = Memory::Read_U32(exportAddr + 8); + module->nm.module_reboot_before_thread_attr = Memory::Read_U32(exportAddr + 12); + break; + case NID_MODULE_SDK_VERSION: + // TODO + break; + default: + // TODO + break; + } + } + if (ent->size > 4) { ent = (PspLibEntEntry*)((u8*)ent + ent->size * 4); @@ -562,6 +637,18 @@ Module *__KernelLoadModule(u8 *fileptr, SceKernelLMOption *options, std::string void __KernelStartModule(Module *m, int args, const char *argp, SceKernelSMOption *options) { + if (m->nm.module_start_func != 0 || m->nm.module_start_func != -1) + { + if (m->nm.module_start_func != m->nm.entry_addr) + WARN_LOG(LOADER, "Main module has start func (%08x) different from entry (%08x)?", m->nm.module_start_func, m->nm.entry_addr); + if (m->nm.module_start_thread_priority != 0 && m->nm.module_start_thread_priority != options->priority) + WARN_LOG(LOADER, "Main module has different priority (%02x vs. %02x)", m->nm.module_start_thread_priority, options->priority); + if (m->nm.module_start_thread_stacksize != 0 && m->nm.module_start_thread_stacksize != options->stacksize) + WARN_LOG(LOADER, "Main module has different stack size (%08x vs. %08x)", m->nm.module_start_thread_stacksize, options->stacksize); + if (m->nm.module_start_thread_attr != 0 && m->nm.module_start_thread_attr != options->attribute) + WARN_LOG(LOADER, "Main module has different attr (%08x vs. %08x)", m->nm.module_start_thread_attr, options->attribute); + } + __KernelSetupRootThread(m->GetUID(), args, argp, options->priority, options->stacksize, options->attribute); mainModuleID = m->GetUID(); //TODO: if current thread, put it in wait state, waiting for the new thread From 559095230b47cf93be2be9d2ff77a94bb5461f7d Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Mon, 7 Jan 2013 01:05:26 -0800 Subject: [PATCH 3/5] Take the module info when starting the main module. Fixes Ys Seven, Fieldrunners, Shadow of Destiny, maybe others. --- Core/HLE/sceKernelModule.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Core/HLE/sceKernelModule.cpp b/Core/HLE/sceKernelModule.cpp index 8d866d1d4f..27675b1fbc 100644 --- a/Core/HLE/sceKernelModule.cpp +++ b/Core/HLE/sceKernelModule.cpp @@ -641,12 +641,6 @@ void __KernelStartModule(Module *m, int args, const char *argp, SceKernelSMOptio { if (m->nm.module_start_func != m->nm.entry_addr) WARN_LOG(LOADER, "Main module has start func (%08x) different from entry (%08x)?", m->nm.module_start_func, m->nm.entry_addr); - if (m->nm.module_start_thread_priority != 0 && m->nm.module_start_thread_priority != options->priority) - WARN_LOG(LOADER, "Main module has different priority (%02x vs. %02x)", m->nm.module_start_thread_priority, options->priority); - if (m->nm.module_start_thread_stacksize != 0 && m->nm.module_start_thread_stacksize != options->stacksize) - WARN_LOG(LOADER, "Main module has different stack size (%08x vs. %08x)", m->nm.module_start_thread_stacksize, options->stacksize); - if (m->nm.module_start_thread_attr != 0 && m->nm.module_start_thread_attr != options->attribute) - WARN_LOG(LOADER, "Main module has different attr (%08x vs. %08x)", m->nm.module_start_thread_attr, options->attribute); } __KernelSetupRootThread(m->GetUID(), args, argp, options->priority, options->stacksize, options->attribute); @@ -708,6 +702,14 @@ bool __KernelLoadExec(const char *filename, SceKernelLoadExecParam *param, std:: option.priority = 0x20; option.stacksize = 0x40000; // crazy? but seems to be the truth + // Replace start options with module-specified values if they exist. + if (module->nm.module_start_thread_attr != 0) + option.attribute = module->nm.module_start_thread_attr; + if (module->nm.module_start_thread_priority != 0) + option.priority = module->nm.module_start_thread_priority; + if (module->nm.module_start_thread_stacksize != 0) + option.stacksize = module->nm.module_start_thread_stacksize; + __KernelStartModule(module, (u32)strlen(filename) + 1, filename, &option); __KernelStartIdleThreads(); From 0619f230b69f2b306cbe273c8dd41179180e2625 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Mon, 7 Jan 2013 22:57:56 -0800 Subject: [PATCH 4/5] Cleanup some todos and logging in exports. --- Core/HLE/sceKernelModule.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Core/HLE/sceKernelModule.cpp b/Core/HLE/sceKernelModule.cpp index 27675b1fbc..94319a8ccc 100644 --- a/Core/HLE/sceKernelModule.cpp +++ b/Core/HLE/sceKernelModule.cpp @@ -512,31 +512,33 @@ Module *__KernelLoadELFFromPtr(const u8 *ptr, u32 loadAddress, std::string *erro switch (nid) { case NID_MODULE_INFO: - // TODO break; case NID_MODULE_START_THREAD_PARAMETER: - // TODO: What's at 0? + if (Memory::Read_U32(exportAddr) != 3) + WARN_LOG(LOADER, "Strange value at module_start_thread_parameter export: %08x", Memory::Read_U32(exportAddr)); module->nm.module_start_thread_priority = Memory::Read_U32(exportAddr + 4); module->nm.module_start_thread_stacksize = Memory::Read_U32(exportAddr + 8); module->nm.module_start_thread_attr = Memory::Read_U32(exportAddr + 12); break; case NID_MODULE_STOP_THREAD_PARAMETER: - // TODO: What's at 0? + if (Memory::Read_U32(exportAddr) != 3) + WARN_LOG(LOADER, "Strange value at module_stop_thread_parameter export: %08x", Memory::Read_U32(exportAddr)); module->nm.module_stop_thread_priority = Memory::Read_U32(exportAddr + 4); module->nm.module_stop_thread_stacksize = Memory::Read_U32(exportAddr + 8); module->nm.module_stop_thread_attr = Memory::Read_U32(exportAddr + 12); break; case NID_MODULE_REBOOT_BEFORE_THREAD_PARAMETER: - // TODO: What's at 0? + if (Memory::Read_U32(exportAddr) != 3) + WARN_LOG(LOADER, "Strange value at module_reboot_before_thread_parameter export: %08x", Memory::Read_U32(exportAddr)); module->nm.module_reboot_before_thread_priority = Memory::Read_U32(exportAddr + 4); module->nm.module_reboot_before_thread_stacksize = Memory::Read_U32(exportAddr + 8); module->nm.module_reboot_before_thread_attr = Memory::Read_U32(exportAddr + 12); break; case NID_MODULE_SDK_VERSION: - // TODO + DEBUG_LOG(LOADER, "Module SDK: %08x", Memory::Read_U32(exportAddr)); break; default: - // TODO + DEBUG_LOG(LOADER, "Unexpected variable with nid: %08x", nid); break; } } From da865e83dcbbdbe3c3b71266079e633719b23757 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Tue, 8 Jan 2013 00:35:55 -0800 Subject: [PATCH 5/5] Write an unknown syscall for unresolved imports. --- Core/HLE/HLE.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Core/HLE/HLE.cpp b/Core/HLE/HLE.cpp index 4ed00ba669..4fcb6b93c1 100644 --- a/Core/HLE/HLE.cpp +++ b/Core/HLE/HLE.cpp @@ -204,6 +204,10 @@ void WriteSyscall(const char *moduleName, u32 nib, u32 address) strncpy(sysc.moduleName, moduleName, KERNELOBJECT_MAX_NAME_LENGTH); sysc.moduleName[KERNELOBJECT_MAX_NAME_LENGTH] = '\0'; unresolvedSyscalls.push_back(sysc); + + // Write a trap so we notice this func if it's called before resolving. + Memory::Write_U32(MIPS_MAKE_JR_RA(), address); // jr ra + Memory::Write_U32(GetSyscallOp("(invalid syscall)", nib), address + 4); } }