diff --git a/rpcs3/Emu/Cell/PPUInterpreter.h b/rpcs3/Emu/Cell/PPUInterpreter.h index 1f9c3b0d51..7e5d3b8b86 100644 --- a/rpcs3/Emu/Cell/PPUInterpreter.h +++ b/rpcs3/Emu/Cell/PPUInterpreter.h @@ -2255,7 +2255,7 @@ private: } void HACK(u32 index) { - execute_ps3_func_by_index(CPU, index); + execute_ppu_func_by_index(CPU, index); } void SC(u32 lev) { diff --git a/rpcs3/Emu/Cell/PPULLVMRecompiler.cpp b/rpcs3/Emu/Cell/PPULLVMRecompiler.cpp index e6d1c4a921..d7c348d03b 100644 --- a/rpcs3/Emu/Cell/PPULLVMRecompiler.cpp +++ b/rpcs3/Emu/Cell/PPULLVMRecompiler.cpp @@ -1999,7 +1999,7 @@ void Compiler::BC(u32 bo, u32 bi, s32 bd, u32 aa, u32 lk) { } void Compiler::HACK(u32 index) { - Call("execute_ps3_func_by_index", &execute_ps3_func_by_index, m_state.args[CompileTaskState::Args::State], m_ir_builder->getInt32(index)); + Call("execute_ppu_func_by_index", &execute_ppu_func_by_index, m_state.args[CompileTaskState::Args::State], m_ir_builder->getInt32(index)); } void Compiler::SC(u32 lev) { diff --git a/rpcs3/Emu/SysCalls/Modules.cpp b/rpcs3/Emu/SysCalls/Modules.cpp index 9c31c6ff2e..96ada9f27a 100644 --- a/rpcs3/Emu/SysCalls/Modules.cpp +++ b/rpcs3/Emu/SysCalls/Modules.cpp @@ -10,11 +10,11 @@ #include "ModuleManager.h" #include "Emu/Cell/PPUInstrTable.h" -std::vector g_ps3_func_list; +std::vector g_ppu_func_list; -u32 add_ps3_func(ModuleFunc func) +u32 add_ppu_func(ModuleFunc func) { - for (auto& f : g_ps3_func_list) + for (auto& f : g_ppu_func_list) { if (f.id == func.id) { @@ -30,23 +30,23 @@ u32 add_ps3_func(ModuleFunc func) f.lle_func = func.lle_func; } - return (u32)(&f - g_ps3_func_list.data()); + return (u32)(&f - g_ppu_func_list.data()); } } - g_ps3_func_list.push_back(func); - return (u32)g_ps3_func_list.size() - 1; + g_ppu_func_list.push_back(func); + return (u32)g_ppu_func_list.size() - 1; } -ModuleFunc* get_ps3_func_by_nid(u32 nid, u32* out_index) +ModuleFunc* get_ppu_func_by_nid(u32 nid, u32* out_index) { - for (auto& f : g_ps3_func_list) + for (auto& f : g_ppu_func_list) { if (f.id == nid) { if (out_index) { - *out_index = (u32)(&f - g_ps3_func_list.data()); + *out_index = (u32)(&f - g_ppu_func_list.data()); } return &f; @@ -56,19 +56,19 @@ ModuleFunc* get_ps3_func_by_nid(u32 nid, u32* out_index) return nullptr; } -ModuleFunc* get_ps3_func_by_index(u32 index) +ModuleFunc* get_ppu_func_by_index(u32 index) { - if (index >= g_ps3_func_list.size()) + if (index >= g_ppu_func_list.size()) { return nullptr; } - return &g_ps3_func_list[index]; + return &g_ppu_func_list[index]; } -void execute_ps3_func_by_index(PPUThread& CPU, u32 index) +void execute_ppu_func_by_index(PPUThread& CPU, u32 index) { - if (auto func = get_ps3_func_by_index(index)) + if (auto func = get_ppu_func_by_index(index)) { // save RTOC vm::write64(vm::cast(CPU.GPR[1] + 0x28), CPU.GPR[2]); @@ -98,9 +98,9 @@ void execute_ps3_func_by_index(PPUThread& CPU, u32 index) } } -void clear_ps3_functions() +void clear_ppu_functions() { - g_ps3_func_list.clear(); + g_ppu_func_list.clear(); } u32 get_function_id(const char* name) @@ -204,8 +204,3 @@ IdManager& Module::GetIdManager() const { return Emu.GetIdManager(); } - -void Module::PushNewFuncSub(SFunc* func) -{ - Emu.GetSFuncManager().push_back(func); -} diff --git a/rpcs3/Emu/SysCalls/Modules.h b/rpcs3/Emu/SysCalls/Modules.h index 85be6c1b06..7d3a1d5a92 100644 --- a/rpcs3/Emu/SysCalls/Modules.h +++ b/rpcs3/Emu/SysCalls/Modules.h @@ -10,10 +10,10 @@ struct ModuleFunc { u32 id; Module* module; - ps3_func_caller func; + ppu_func_caller func; vm::ptr lle_func; - ModuleFunc(u32 id, Module* module, ps3_func_caller func, vm::ptr lle_func = vm::ptr::make(0)) + ModuleFunc(u32 id, Module* module, ppu_func_caller func, vm::ptr lle_func = vm::ptr::make(0)) : id(id) , module(module) , func(func) @@ -46,7 +46,6 @@ class Module : public LogBase void(*m_init)(); IdManager& GetIdManager() const; - void PushNewFuncSub(SFunc* func); Module() = delete; @@ -110,39 +109,24 @@ public: } bool RemoveId(u32 id); - - template __forceinline u32 AddFunc(const u32 nid, T _func); - template __forceinline u32 AddFunc(const char* name, T _func); - template __forceinline u32 AddFuncSub(const char group[8], const u64 ops[], const char* name, T _func); }; -u32 add_ps3_func(ModuleFunc func); -ModuleFunc* get_ps3_func_by_nid(u32 nid, u32* out_index = nullptr); -ModuleFunc* get_ps3_func_by_index(u32 index); -void execute_ps3_func_by_index(PPUThread& CPU, u32 id); -void clear_ps3_functions(); +u32 add_ppu_func(ModuleFunc func); +ModuleFunc* get_ppu_func_by_nid(u32 nid, u32* out_index = nullptr); +ModuleFunc* get_ppu_func_by_index(u32 index); +void execute_ppu_func_by_index(PPUThread& CPU, u32 id); +void clear_ppu_functions(); u32 get_function_id(const char* name); -template -__forceinline u32 Module::AddFunc(const u32 nid, T _func) -{ - return add_ps3_func(ModuleFunc(nid, this, ppu_func_detail::_bind_func(_func))); -} +u32 add_ppu_func_sub(SFunc sf); -template -__forceinline u32 Module::AddFunc(const char* name, T _func) +__forceinline static u32 add_ppu_func_sub(const char group[8], const u64 ops[], const char* name, Module* module, ppu_func_caller func) { - return AddFunc(get_function_id(name), _func); -} - -template -__forceinline u32 Module::AddFuncSub(const char group[8], const u64 ops[], const char* name, T _func) -{ - SFunc* sf = new SFunc; - sf->index = AddFunc(name, _func); - sf->name = name; - sf->group = *(u64*)group; - sf->found = 0; + SFunc sf; + sf.index = add_ppu_func(ModuleFunc(get_function_id(name), module, func)); + sf.name = name; + sf.group = *(u64*)group; + sf.found = 0; // TODO: check for self-inclusions, use CRC for (u32 i = 0; ops[i]; i++) @@ -153,20 +137,18 @@ __forceinline u32 Module::AddFuncSub(const char group[8], const u64 ops[], const if (op.mask) op.crc &= op.mask; op.mask = re32(op.mask); op.crc = re32(op.crc); - sf->ops.push_back(op); + sf.ops.push_back(op); } - PushNewFuncSub(sf); - - return sf->index; + return add_ppu_func_sub(sf); } #define REG_SUB(module, group, name, ...) \ static const u64 name ## _table[] = {__VA_ARGS__ , 0}; \ - if (name ## _table[0]) module.AddFuncSub<_targ(name)>(group, name ## _table, #name, name) + if (name ## _table[0]) add_ppu_func_sub(group, name ## _table, #name, &module, bind_func(name)) -#define REG_FUNC(module, name) module.AddFunc<_targ(name)>(#name, name) +#define REG_FUNC(module, name) add_ppu_func(ModuleFunc(get_function_id(#name), &module, bind_func(name))) -#define REG_FUNC2(module, nid, name) module.AddFunc<_targ(name)>(nid, name) +#define REG_FUNC2(module, nid, name) add_ppu_func(ModuleFunc(nid, &module, bind_func(name))) #define UNIMPLEMENTED_FUNC(module) module.Error("%s", __FUNCTION__) diff --git a/rpcs3/Emu/SysCalls/SC_FUNC.h b/rpcs3/Emu/SysCalls/SC_FUNC.h index 5a0f91fbf4..84605cab03 100644 --- a/rpcs3/Emu/SysCalls/SC_FUNC.h +++ b/rpcs3/Emu/SysCalls/SC_FUNC.h @@ -1,11 +1,7 @@ #pragma once #include "Emu/Cell/PPUThread.h" -#if defined(_MSC_VER) -typedef void(*ps3_func_caller)(PPUThread&); -#else -typedef std::function ps3_func_caller; -#endif +typedef void(*ppu_func_caller)(PPUThread&); namespace ppu_func_detail { @@ -162,11 +158,11 @@ namespace ppu_func_detail static const bind_arg_type value = is_float ? ARG_FLOAT : (is_vector ? ARG_VECTOR : ARG_GENERAL); }; - template + template struct func_binder; - template - struct func_binder + template + struct func_binder { typedef void(*func_t)(PPUThread&, T...); @@ -174,15 +170,10 @@ namespace ppu_func_detail { call(_func, std::tuple_cat(std::tuple(CPU), iterate<0, 0, 0, T...>(CPU))); } - - static void do_call(PPUThread& CPU) - { - do_call(CPU, (func_t)func); - } }; - template - struct func_binder + template + struct func_binder { typedef void(*func_t)(T...); @@ -190,15 +181,10 @@ namespace ppu_func_detail { call(_func, iterate<0, 0, 0, T...>(CPU)); } - - static void do_call(PPUThread& CPU) - { - do_call(CPU, (func_t)func); - } }; - template - struct func_binder + template + struct func_binder { typedef RT(*func_t)(PPUThread&, T...); @@ -206,14 +192,9 @@ namespace ppu_func_detail { bind_result::value>::func(CPU, call(_func, std::tuple_cat(std::tuple(CPU), iterate<0, 0, 0, T...>(CPU)))); } - - static void do_call(PPUThread& CPU) - { - do_call(CPU, (func_t)func); - } }; - template + template struct func_binder { typedef RT(*func_t)(T...); @@ -222,28 +203,12 @@ namespace ppu_func_detail { bind_result::value>::func(CPU, call(_func, iterate<0, 0, 0, T...>(CPU))); } - - static void do_call(PPUThread& CPU) - { - do_call(CPU, (func_t)func); - } }; - - template - ps3_func_caller _bind_func(RT(*_func)(T...)) - { -#if defined(_MSC_VER) - return ppu_func_detail::func_binder::do_call; -#else - return [_func](PPUThread& CPU){ ppu_func_detail::func_binder::do_call(CPU, _func); }; -#endif - } } -#if defined(_MSC_VER) -#define _targ(name) name -#else -#define _targ(name) nullptr -#endif +template __forceinline void call_ppu_func(PPUThread& CPU, RT(*func)(T...)) +{ + ppu_func_detail::func_binder::do_call(CPU, func); +} -#define bind_func(func) ppu_func_detail::_bind_func<_targ(func)>(func) +#define bind_func(func) [](PPUThread& CPU){ call_ppu_func(CPU, func); } diff --git a/rpcs3/Emu/SysCalls/Static.cpp b/rpcs3/Emu/SysCalls/Static.cpp index d67d0d9388..5a7c03a9c5 100644 --- a/rpcs3/Emu/SysCalls/Static.cpp +++ b/rpcs3/Emu/SysCalls/Static.cpp @@ -4,6 +4,14 @@ #include "Emu/SysCalls/Modules.h" #include "Static.h" +std::vector g_ppu_func_subs; + +u32 add_ppu_func_sub(SFunc func) +{ + g_ppu_func_subs.push_back(func); + return func.index; +} + void StaticFuncManager::StaticAnalyse(void* ptr, u32 size, u32 base) { u32* data = (u32*)ptr; size /= 4; @@ -14,13 +22,13 @@ void StaticFuncManager::StaticAnalyse(void* ptr, u32 size, u32 base) // TODO: optimize search for (u32 i = 0; i < size; i++) { - for (u32 j = 0; j < m_static_funcs_list.size(); j++) + for (u32 j = 0; j < g_ppu_func_subs.size(); j++) { - if ((data[i] & m_static_funcs_list[j]->ops[0].mask) == m_static_funcs_list[j]->ops[0].crc) + if ((data[i] & g_ppu_func_subs[j].ops[0].mask) == g_ppu_func_subs[j].ops[0].crc) { bool found = true; u32 can_skip = 0; - for (u32 k = i, x = 0; x + 1 <= m_static_funcs_list[j]->ops.size(); k++, x++) + for (u32 k = i, x = 0; x + 1 <= g_ppu_func_subs[j].ops.size(); k++, x++) { if (k >= size) { @@ -35,8 +43,8 @@ void StaticFuncManager::StaticAnalyse(void* ptr, u32 size, u32 base) continue; } - const u32 mask = m_static_funcs_list[j]->ops[x].mask; - const u32 crc = m_static_funcs_list[j]->ops[x].crc; + const u32 mask = g_ppu_func_subs[j].ops[x].mask; + const u32 crc = g_ppu_func_subs[j].ops[x].crc; if (!mask) { @@ -82,10 +90,10 @@ void StaticFuncManager::StaticAnalyse(void* ptr, u32 size, u32 base) } if (found) { - LOG_NOTICE(LOADER, "Function '%s' hooked (addr=0x%x)", m_static_funcs_list[j]->name, i * 4 + base); - m_static_funcs_list[j]->found++; - data[i+0] = re32(0x04000000 | m_static_funcs_list[j]->index); // hack - data[i+2] = se32(0x4e800020); // blr + LOG_NOTICE(LOADER, "Function '%s' hooked (addr=0x%x)", g_ppu_func_subs[j].name, i * 4 + base); + g_ppu_func_subs[j].found++; + data[i + 0] = re32(0x04000000 | g_ppu_func_subs[j].index); // hack + data[i + 1] = se32(0x4e800020); // blr i += 1; // skip modified code } } @@ -93,11 +101,11 @@ void StaticFuncManager::StaticAnalyse(void* ptr, u32 size, u32 base) } // check function groups - for (u32 i = 0; i < m_static_funcs_list.size(); i++) + for (u32 i = 0; i < g_ppu_func_subs.size(); i++) { - if (m_static_funcs_list[i]->found) // start from some group + if (g_ppu_func_subs[i].found) // start from some group { - const u64 group = m_static_funcs_list[i]->group; + const u64 group = g_ppu_func_subs[i].group; enum GroupSearchResult : u32 { @@ -108,24 +116,24 @@ void StaticFuncManager::StaticAnalyse(void* ptr, u32 size, u32 base) u32 res = GSR_SUCCESS; // analyse - for (u32 j = 0; j < m_static_funcs_list.size(); j++) if (m_static_funcs_list[j]->group == group) + for (u32 j = 0; j < g_ppu_func_subs.size(); j++) if (g_ppu_func_subs[j].group == group) { - u32 count = m_static_funcs_list[j]->found; + u32 count = g_ppu_func_subs[j].found; if (count == 0) // not found { // check if this function has been found with different pattern - for (u32 k = 0; k < m_static_funcs_list.size(); k++) if (m_static_funcs_list[k]->group == group) + for (u32 k = 0; k < g_ppu_func_subs.size(); k++) if (g_ppu_func_subs[k].group == group) { - if (k != j && m_static_funcs_list[k]->index == m_static_funcs_list[j]->index) + if (k != j && g_ppu_func_subs[k].index == g_ppu_func_subs[j].index) { - count += m_static_funcs_list[k]->found; + count += g_ppu_func_subs[k].found; } } if (count == 0) { res |= GSR_MISSING; - LOG_ERROR(LOADER, "Function '%s' not found", m_static_funcs_list[j]->name); + LOG_ERROR(LOADER, "Function '%s' not found", g_ppu_func_subs[j].name); } else if (count > 1) { @@ -135,14 +143,14 @@ void StaticFuncManager::StaticAnalyse(void* ptr, u32 size, u32 base) else if (count == 1) // found { // ensure that this function has NOT been found with different pattern - for (u32 k = 0; k < m_static_funcs_list.size(); k++) if (m_static_funcs_list[k]->group == group) + for (u32 k = 0; k < g_ppu_func_subs.size(); k++) if (g_ppu_func_subs[k].group == group) { - if (k != j && m_static_funcs_list[k]->index == m_static_funcs_list[j]->index) + if (k != j && g_ppu_func_subs[k].index == g_ppu_func_subs[j].index) { - if (m_static_funcs_list[k]->found) + if (g_ppu_func_subs[k].found) { res |= GSR_EXCESS; - LOG_ERROR(LOADER, "Function '%s' hooked twice", m_static_funcs_list[j]->name); + LOG_ERROR(LOADER, "Function '%s' hooked twice", g_ppu_func_subs[j].name); } } } @@ -150,14 +158,14 @@ void StaticFuncManager::StaticAnalyse(void* ptr, u32 size, u32 base) else { res |= GSR_EXCESS; - LOG_ERROR(LOADER, "Function '%s' hooked twice", m_static_funcs_list[j]->name); + LOG_ERROR(LOADER, "Function '%s' hooked twice", g_ppu_func_subs[j].name); } } // clear data - for (u32 j = 0; j < m_static_funcs_list.size(); j++) + for (u32 j = 0; j < g_ppu_func_subs.size(); j++) { - if (m_static_funcs_list[j]->group == group) m_static_funcs_list[j]->found = 0; + if (g_ppu_func_subs[j].group == group) g_ppu_func_subs[j].found = 0; } char name[9] = "????????"; @@ -180,25 +188,5 @@ void StaticFuncManager::StaticAnalyse(void* ptr, u32 size, u32 base) void StaticFuncManager::StaticFinalize() { - for (SFunc *s : m_static_funcs_list) - { - delete s; - } - m_static_funcs_list.clear(); + g_ppu_func_subs.clear(); } - -void StaticFuncManager::push_back(SFunc *ele) -{ - m_static_funcs_list.push_back(ele); -} - -SFunc *StaticFuncManager::operator[](size_t i) -{ - return m_static_funcs_list[i]; -} - -StaticFuncManager::~StaticFuncManager() -{ - StaticFinalize(); -} - diff --git a/rpcs3/Emu/SysCalls/Static.h b/rpcs3/Emu/SysCalls/Static.h index 166b872063..665612b4f4 100644 --- a/rpcs3/Emu/SysCalls/Static.h +++ b/rpcs3/Emu/SysCalls/Static.h @@ -6,11 +6,7 @@ class PPUThread; class StaticFuncManager { - std::vector m_static_funcs_list; public: void StaticAnalyse(void* ptr, u32 size, u32 base); void StaticFinalize(); - void push_back(SFunc *ele); - SFunc *operator[](size_t i); - ~StaticFuncManager(); }; diff --git a/rpcs3/Emu/SysCalls/SysCalls.cpp b/rpcs3/Emu/SysCalls/SysCalls.cpp index 3cb80a843e..6a6ed229d9 100644 --- a/rpcs3/Emu/SysCalls/SysCalls.cpp +++ b/rpcs3/Emu/SysCalls/SysCalls.cpp @@ -51,7 +51,7 @@ const int kSyscallTableLength = 1024; // DBG = Debug // PM = Product Mode // AuthID = Authentication ID -const ps3_func_caller sc_table[1024] = +const ppu_func_caller sc_table[1024] = { null_func, bind_func(sys_process_getpid), //1 (0x001) diff --git a/rpcs3/Emu/System.cpp b/rpcs3/Emu/System.cpp index 4927d38377..06320945e8 100644 --- a/rpcs3/Emu/System.cpp +++ b/rpcs3/Emu/System.cpp @@ -379,7 +379,7 @@ void Emulator::Stop() // TODO: check finalization order - clear_ps3_functions(); + clear_ppu_functions(); SavePoints(BreakPointsDBName); m_break_points.clear(); diff --git a/rpcs3/Loader/ELF64.cpp b/rpcs3/Loader/ELF64.cpp index dfb12aee06..8c90bb408c 100644 --- a/rpcs3/Loader/ELF64.cpp +++ b/rpcs3/Loader/ELF64.cpp @@ -399,7 +399,7 @@ namespace loader for (auto& f : m.second.exports) { - add_ps3_func(ModuleFunc(f.first, module, nullptr, vm::ptr::make(f.second))); + add_ppu_func(ModuleFunc(f.first, module, nullptr, vm::ptr::make(f.second))); } for (auto& f : m.second.imports) @@ -409,13 +409,13 @@ namespace loader u32 index; - auto func = get_ps3_func_by_nid(nid, &index); + auto func = get_ppu_func_by_nid(nid, &index); if (!func) { LOG_ERROR(LOADER, "Unimplemented function '%s' (0x%x)", SysCalls::GetHLEFuncName(nid), addr); - index = add_ps3_func(ModuleFunc(nid, module, nullptr)); + index = add_ppu_func(ModuleFunc(nid, module, nullptr)); } else { @@ -630,13 +630,13 @@ namespace loader u32 index; - auto func = get_ps3_func_by_nid(nid, &index); + auto func = get_ppu_func_by_nid(nid, &index); if (!func) { LOG_ERROR(LOADER, "Unimplemented function '%s' in '%s' module (0x%x)", SysCalls::GetHLEFuncName(nid), module_name, addr); - index = add_ps3_func(ModuleFunc(nid, module, nullptr)); + index = add_ppu_func(ModuleFunc(nid, module, nullptr)); } else {