This commit is contained in:
Nekotekina 2015-02-21 14:30:26 +03:00
parent 18954ee5b8
commit faaef03604
10 changed files with 92 additions and 166 deletions

View file

@ -2255,7 +2255,7 @@ private:
} }
void HACK(u32 index) void HACK(u32 index)
{ {
execute_ps3_func_by_index(CPU, index); execute_ppu_func_by_index(CPU, index);
} }
void SC(u32 lev) void SC(u32 lev)
{ {

View file

@ -1999,7 +1999,7 @@ void Compiler::BC(u32 bo, u32 bi, s32 bd, u32 aa, u32 lk) {
} }
void Compiler::HACK(u32 index) { void Compiler::HACK(u32 index) {
Call<void>("execute_ps3_func_by_index", &execute_ps3_func_by_index, m_state.args[CompileTaskState::Args::State], m_ir_builder->getInt32(index)); Call<void>("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) { void Compiler::SC(u32 lev) {

View file

@ -10,11 +10,11 @@
#include "ModuleManager.h" #include "ModuleManager.h"
#include "Emu/Cell/PPUInstrTable.h" #include "Emu/Cell/PPUInstrTable.h"
std::vector<ModuleFunc> g_ps3_func_list; std::vector<ModuleFunc> 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) if (f.id == func.id)
{ {
@ -30,23 +30,23 @@ u32 add_ps3_func(ModuleFunc func)
f.lle_func = func.lle_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); g_ppu_func_list.push_back(func);
return (u32)g_ps3_func_list.size() - 1; 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 (f.id == nid)
{ {
if (out_index) if (out_index)
{ {
*out_index = (u32)(&f - g_ps3_func_list.data()); *out_index = (u32)(&f - g_ppu_func_list.data());
} }
return &f; return &f;
@ -56,19 +56,19 @@ ModuleFunc* get_ps3_func_by_nid(u32 nid, u32* out_index)
return nullptr; 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 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 // save RTOC
vm::write64(vm::cast(CPU.GPR[1] + 0x28), CPU.GPR[2]); 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) u32 get_function_id(const char* name)
@ -204,8 +204,3 @@ IdManager& Module::GetIdManager() const
{ {
return Emu.GetIdManager(); return Emu.GetIdManager();
} }
void Module::PushNewFuncSub(SFunc* func)
{
Emu.GetSFuncManager().push_back(func);
}

View file

@ -10,10 +10,10 @@ struct ModuleFunc
{ {
u32 id; u32 id;
Module* module; Module* module;
ps3_func_caller func; ppu_func_caller func;
vm::ptr<void()> lle_func; vm::ptr<void()> lle_func;
ModuleFunc(u32 id, Module* module, ps3_func_caller func, vm::ptr<void()> lle_func = vm::ptr<void()>::make(0)) ModuleFunc(u32 id, Module* module, ppu_func_caller func, vm::ptr<void()> lle_func = vm::ptr<void()>::make(0))
: id(id) : id(id)
, module(module) , module(module)
, func(func) , func(func)
@ -46,7 +46,6 @@ class Module : public LogBase
void(*m_init)(); void(*m_init)();
IdManager& GetIdManager() const; IdManager& GetIdManager() const;
void PushNewFuncSub(SFunc* func);
Module() = delete; Module() = delete;
@ -110,39 +109,24 @@ public:
} }
bool RemoveId(u32 id); bool RemoveId(u32 id);
template<void* func, typename T> __forceinline u32 AddFunc(const u32 nid, T _func);
template<void* func, typename T> __forceinline u32 AddFunc(const char* name, T _func);
template<void* func, typename T> __forceinline u32 AddFuncSub(const char group[8], const u64 ops[], const char* name, T _func);
}; };
u32 add_ps3_func(ModuleFunc func); u32 add_ppu_func(ModuleFunc func);
ModuleFunc* get_ps3_func_by_nid(u32 nid, u32* out_index = nullptr); ModuleFunc* get_ppu_func_by_nid(u32 nid, u32* out_index = nullptr);
ModuleFunc* get_ps3_func_by_index(u32 index); ModuleFunc* get_ppu_func_by_index(u32 index);
void execute_ps3_func_by_index(PPUThread& CPU, u32 id); void execute_ppu_func_by_index(PPUThread& CPU, u32 id);
void clear_ps3_functions(); void clear_ppu_functions();
u32 get_function_id(const char* name); u32 get_function_id(const char* name);
template<void* func, typename T> u32 add_ppu_func_sub(SFunc sf);
__forceinline u32 Module::AddFunc(const u32 nid, T _func)
{
return add_ps3_func(ModuleFunc(nid, this, ppu_func_detail::_bind_func<func>(_func)));
}
template<void* func, typename T> __forceinline static u32 add_ppu_func_sub(const char group[8], const u64 ops[], const char* name, Module* module, ppu_func_caller func)
__forceinline u32 Module::AddFunc(const char* name, T _func)
{ {
return AddFunc<func>(get_function_id(name), _func); SFunc sf;
} sf.index = add_ppu_func(ModuleFunc(get_function_id(name), module, func));
sf.name = name;
template<void* func, typename T> sf.group = *(u64*)group;
__forceinline u32 Module::AddFuncSub(const char group[8], const u64 ops[], const char* name, T _func) sf.found = 0;
{
SFunc* sf = new SFunc;
sf->index = AddFunc<func>(name, _func);
sf->name = name;
sf->group = *(u64*)group;
sf->found = 0;
// TODO: check for self-inclusions, use CRC // TODO: check for self-inclusions, use CRC
for (u32 i = 0; ops[i]; i++) 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; if (op.mask) op.crc &= op.mask;
op.mask = re32(op.mask); op.mask = re32(op.mask);
op.crc = re32(op.crc); op.crc = re32(op.crc);
sf->ops.push_back(op); sf.ops.push_back(op);
} }
PushNewFuncSub(sf); return add_ppu_func_sub(sf);
return sf->index;
} }
#define REG_SUB(module, group, name, ...) \ #define REG_SUB(module, group, name, ...) \
static const u64 name ## _table[] = {__VA_ARGS__ , 0}; \ 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__) #define UNIMPLEMENTED_FUNC(module) module.Error("%s", __FUNCTION__)

View file

@ -1,11 +1,7 @@
#pragma once #pragma once
#include "Emu/Cell/PPUThread.h" #include "Emu/Cell/PPUThread.h"
#if defined(_MSC_VER) typedef void(*ppu_func_caller)(PPUThread&);
typedef void(*ps3_func_caller)(PPUThread&);
#else
typedef std::function<void(PPUThread&)> ps3_func_caller;
#endif
namespace ppu_func_detail 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); static const bind_arg_type value = is_float ? ARG_FLOAT : (is_vector ? ARG_VECTOR : ARG_GENERAL);
}; };
template<void* func, typename RT, typename... T> template<typename RT, typename... T>
struct func_binder; struct func_binder;
template<void* func, typename... T> template<typename... T>
struct func_binder<func, void, PPUThread&, T...> struct func_binder<void, PPUThread&, T...>
{ {
typedef void(*func_t)(PPUThread&, T...); typedef void(*func_t)(PPUThread&, T...);
@ -174,15 +170,10 @@ namespace ppu_func_detail
{ {
call<void>(_func, std::tuple_cat(std::tuple<PPUThread&>(CPU), iterate<0, 0, 0, T...>(CPU))); call<void>(_func, std::tuple_cat(std::tuple<PPUThread&>(CPU), iterate<0, 0, 0, T...>(CPU)));
} }
static void do_call(PPUThread& CPU)
{
do_call(CPU, (func_t)func);
}
}; };
template<void* func, typename... T> template<typename... T>
struct func_binder<func, void, T...> struct func_binder<void, T...>
{ {
typedef void(*func_t)(T...); typedef void(*func_t)(T...);
@ -190,15 +181,10 @@ namespace ppu_func_detail
{ {
call<void>(_func, iterate<0, 0, 0, T...>(CPU)); call<void>(_func, iterate<0, 0, 0, T...>(CPU));
} }
static void do_call(PPUThread& CPU)
{
do_call(CPU, (func_t)func);
}
}; };
template<void* func, typename RT, typename... T> template<typename RT, typename... T>
struct func_binder<func, RT, PPUThread&, T...> struct func_binder<RT, PPUThread&, T...>
{ {
typedef RT(*func_t)(PPUThread&, T...); typedef RT(*func_t)(PPUThread&, T...);
@ -206,14 +192,9 @@ namespace ppu_func_detail
{ {
bind_result<RT, result_type<RT>::value>::func(CPU, call<RT>(_func, std::tuple_cat(std::tuple<PPUThread&>(CPU), iterate<0, 0, 0, T...>(CPU)))); bind_result<RT, result_type<RT>::value>::func(CPU, call<RT>(_func, std::tuple_cat(std::tuple<PPUThread&>(CPU), iterate<0, 0, 0, T...>(CPU))));
} }
static void do_call(PPUThread& CPU)
{
do_call(CPU, (func_t)func);
}
}; };
template<void* func, typename RT, typename... T> template<typename RT, typename... T>
struct func_binder struct func_binder
{ {
typedef RT(*func_t)(T...); typedef RT(*func_t)(T...);
@ -222,28 +203,12 @@ namespace ppu_func_detail
{ {
bind_result<RT, result_type<RT>::value>::func(CPU, call<RT>(_func, iterate<0, 0, 0, T...>(CPU))); bind_result<RT, result_type<RT>::value>::func(CPU, call<RT>(_func, iterate<0, 0, 0, T...>(CPU)));
} }
static void do_call(PPUThread& CPU)
{
do_call(CPU, (func_t)func);
}
}; };
template<void* func, typename RT, typename... T>
ps3_func_caller _bind_func(RT(*_func)(T...))
{
#if defined(_MSC_VER)
return ppu_func_detail::func_binder<func, RT, T...>::do_call;
#else
return [_func](PPUThread& CPU){ ppu_func_detail::func_binder<func, RT, T...>::do_call(CPU, _func); };
#endif
}
} }
#if defined(_MSC_VER) template<typename RT, typename... T> __forceinline void call_ppu_func(PPUThread& CPU, RT(*func)(T...))
#define _targ(name) name {
#else ppu_func_detail::func_binder<RT, T...>::do_call(CPU, func);
#define _targ(name) nullptr }
#endif
#define bind_func(func) ppu_func_detail::_bind_func<_targ(func)>(func) #define bind_func(func) [](PPUThread& CPU){ call_ppu_func(CPU, func); }

View file

@ -4,6 +4,14 @@
#include "Emu/SysCalls/Modules.h" #include "Emu/SysCalls/Modules.h"
#include "Static.h" #include "Static.h"
std::vector<SFunc> 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) void StaticFuncManager::StaticAnalyse(void* ptr, u32 size, u32 base)
{ {
u32* data = (u32*)ptr; size /= 4; u32* data = (u32*)ptr; size /= 4;
@ -14,13 +22,13 @@ void StaticFuncManager::StaticAnalyse(void* ptr, u32 size, u32 base)
// TODO: optimize search // TODO: optimize search
for (u32 i = 0; i < size; i++) 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; bool found = true;
u32 can_skip = 0; 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) if (k >= size)
{ {
@ -35,8 +43,8 @@ void StaticFuncManager::StaticAnalyse(void* ptr, u32 size, u32 base)
continue; continue;
} }
const u32 mask = m_static_funcs_list[j]->ops[x].mask; const u32 mask = g_ppu_func_subs[j].ops[x].mask;
const u32 crc = m_static_funcs_list[j]->ops[x].crc; const u32 crc = g_ppu_func_subs[j].ops[x].crc;
if (!mask) if (!mask)
{ {
@ -82,10 +90,10 @@ void StaticFuncManager::StaticAnalyse(void* ptr, u32 size, u32 base)
} }
if (found) if (found)
{ {
LOG_NOTICE(LOADER, "Function '%s' hooked (addr=0x%x)", m_static_funcs_list[j]->name, i * 4 + base); LOG_NOTICE(LOADER, "Function '%s' hooked (addr=0x%x)", g_ppu_func_subs[j].name, i * 4 + base);
m_static_funcs_list[j]->found++; g_ppu_func_subs[j].found++;
data[i+0] = re32(0x04000000 | m_static_funcs_list[j]->index); // hack data[i + 0] = re32(0x04000000 | g_ppu_func_subs[j].index); // hack
data[i+2] = se32(0x4e800020); // blr data[i + 1] = se32(0x4e800020); // blr
i += 1; // skip modified code i += 1; // skip modified code
} }
} }
@ -93,11 +101,11 @@ void StaticFuncManager::StaticAnalyse(void* ptr, u32 size, u32 base)
} }
// check function groups // 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 enum GroupSearchResult : u32
{ {
@ -108,24 +116,24 @@ void StaticFuncManager::StaticAnalyse(void* ptr, u32 size, u32 base)
u32 res = GSR_SUCCESS; u32 res = GSR_SUCCESS;
// analyse // 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 if (count == 0) // not found
{ {
// check if this function has been found with different pattern // 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) if (count == 0)
{ {
res |= GSR_MISSING; 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) else if (count > 1)
{ {
@ -135,14 +143,14 @@ void StaticFuncManager::StaticAnalyse(void* ptr, u32 size, u32 base)
else if (count == 1) // found else if (count == 1) // found
{ {
// ensure that this function has NOT been found with different pattern // 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; 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 else
{ {
res |= GSR_EXCESS; 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 // 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] = "????????"; char name[9] = "????????";
@ -180,25 +188,5 @@ void StaticFuncManager::StaticAnalyse(void* ptr, u32 size, u32 base)
void StaticFuncManager::StaticFinalize() void StaticFuncManager::StaticFinalize()
{ {
for (SFunc *s : m_static_funcs_list) g_ppu_func_subs.clear();
{
delete s;
}
m_static_funcs_list.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();
}

View file

@ -6,11 +6,7 @@ class PPUThread;
class StaticFuncManager class StaticFuncManager
{ {
std::vector<SFunc *> m_static_funcs_list;
public: public:
void StaticAnalyse(void* ptr, u32 size, u32 base); void StaticAnalyse(void* ptr, u32 size, u32 base);
void StaticFinalize(); void StaticFinalize();
void push_back(SFunc *ele);
SFunc *operator[](size_t i);
~StaticFuncManager();
}; };

View file

@ -51,7 +51,7 @@ const int kSyscallTableLength = 1024;
// DBG = Debug // DBG = Debug
// PM = Product Mode // PM = Product Mode
// AuthID = Authentication ID // AuthID = Authentication ID
const ps3_func_caller sc_table[1024] = const ppu_func_caller sc_table[1024] =
{ {
null_func, null_func,
bind_func(sys_process_getpid), //1 (0x001) bind_func(sys_process_getpid), //1 (0x001)

View file

@ -379,7 +379,7 @@ void Emulator::Stop()
// TODO: check finalization order // TODO: check finalization order
clear_ps3_functions(); clear_ppu_functions();
SavePoints(BreakPointsDBName); SavePoints(BreakPointsDBName);
m_break_points.clear(); m_break_points.clear();

View file

@ -399,7 +399,7 @@ namespace loader
for (auto& f : m.second.exports) for (auto& f : m.second.exports)
{ {
add_ps3_func(ModuleFunc(f.first, module, nullptr, vm::ptr<void()>::make(f.second))); add_ppu_func(ModuleFunc(f.first, module, nullptr, vm::ptr<void()>::make(f.second)));
} }
for (auto& f : m.second.imports) for (auto& f : m.second.imports)
@ -409,13 +409,13 @@ namespace loader
u32 index; u32 index;
auto func = get_ps3_func_by_nid(nid, &index); auto func = get_ppu_func_by_nid(nid, &index);
if (!func) if (!func)
{ {
LOG_ERROR(LOADER, "Unimplemented function '%s' (0x%x)", SysCalls::GetHLEFuncName(nid), addr); 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 else
{ {
@ -630,13 +630,13 @@ namespace loader
u32 index; u32 index;
auto func = get_ps3_func_by_nid(nid, &index); auto func = get_ppu_func_by_nid(nid, &index);
if (!func) if (!func)
{ {
LOG_ERROR(LOADER, "Unimplemented function '%s' in '%s' module (0x%x)", SysCalls::GetHLEFuncName(nid), module_name, addr); 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 else
{ {