diff --git a/src/xenia/cpu/backend/x64/x64_emitter.cc b/src/xenia/cpu/backend/x64/x64_emitter.cc index 3b2104ada..c8053b05c 100644 --- a/src/xenia/cpu/backend/x64/x64_emitter.cc +++ b/src/xenia/cpu/backend/x64/x64_emitter.cc @@ -417,9 +417,8 @@ uint64_t UndefinedCallExtern(void* raw_context, uint64_t symbol_info_ptr) { } void X64Emitter::CallExtern(const hir::Instr* instr, const FunctionInfo* symbol_info) { - if (!symbol_info->extern_handler()) { - CallNative(UndefinedCallExtern, reinterpret_cast(symbol_info)); - } else if (symbol_info->behavior() == FunctionBehavior::kBuiltin) { + if (symbol_info->behavior() == FunctionBehavior::kBuiltin && + symbol_info->builtin_handler()) { // rcx = context // rdx = target host function // r8 = arg0 @@ -433,7 +432,8 @@ void X64Emitter::CallExtern(const hir::Instr* instr, ReloadECX(); ReloadEDX(); // rax = host return - } else if (symbol_info->behavior() == FunctionBehavior::kExtern) { + } else if (symbol_info->behavior() == FunctionBehavior::kExtern && + symbol_info->extern_handler()) { // rcx = context // rdx = target host function mov(rdx, reinterpret_cast(symbol_info->extern_handler())); @@ -444,6 +444,8 @@ void X64Emitter::CallExtern(const hir::Instr* instr, ReloadECX(); ReloadEDX(); // rax = host return + } else { + CallNative(UndefinedCallExtern, reinterpret_cast(symbol_info)); } } diff --git a/src/xenia/cpu/export_resolver.cc b/src/xenia/cpu/export_resolver.cc index db829ee7d..386f78753 100644 --- a/src/xenia/cpu/export_resolver.cc +++ b/src/xenia/cpu/export_resolver.cc @@ -19,22 +19,20 @@ ExportResolver::ExportResolver() {} ExportResolver::~ExportResolver() {} -void ExportResolver::RegisterTable(const std::string& library_name, - Export* exports, const size_t count) { - tables_.emplace_back(library_name, exports, count); +void ExportResolver::RegisterTable( + const std::string& library_name, + const std::vector* exports) { + tables_.emplace_back(library_name, exports); } Export* ExportResolver::GetExportByOrdinal(const std::string& library_name, uint16_t ordinal) { for (const auto& table : tables_) { if (table.name == library_name || table.simple_name == library_name) { - // TODO(benvanik): binary search? - for (size_t n = 0; n < table.count; n++) { - if (table.exports[n].ordinal == ordinal) { - return &table.exports[n]; - } + if (ordinal > table.exports->size()) { + return nullptr; } - return nullptr; + return table.exports->at(ordinal); } } return nullptr; diff --git a/src/xenia/cpu/export_resolver.h b/src/xenia/cpu/export_resolver.h index 19dd4dccd..153230036 100644 --- a/src/xenia/cpu/export_resolver.h +++ b/src/xenia/cpu/export_resolver.h @@ -89,8 +89,8 @@ class ExportResolver { ExportResolver(); ~ExportResolver(); - void RegisterTable(const std::string& library_name, Export* exports, - const size_t count); + void RegisterTable(const std::string& library_name, + const std::vector* exports); Export* GetExportByOrdinal(const std::string& library_name, uint16_t ordinal); @@ -105,10 +105,9 @@ class ExportResolver { struct ExportTable { std::string name; std::string simple_name; // without extension - Export* exports; - size_t count; - ExportTable(const std::string& name, Export* exports, size_t count) - : name(name), exports(exports), count(count) { + const std::vector* exports; + ExportTable(const std::string& name, const std::vector* exports) + : name(name), exports(exports) { auto dot_pos = name.find_last_of('.'); if (dot_pos != std::string::npos) { simple_name = name.substr(0, dot_pos); diff --git a/src/xenia/cpu/xex_module.cc b/src/xenia/cpu/xex_module.cc index 7c01f6810..66406d013 100644 --- a/src/xenia/cpu/xex_module.cc +++ b/src/xenia/cpu/xex_module.cc @@ -234,8 +234,13 @@ bool XexModule::SetupLibraryImports(const xe_xex2_import_library_t* library) { FunctionInfo::ExternHandler handler = 0; if (kernel_export) { - handler = - (FunctionInfo::ExternHandler)kernel_export->function_data.shim; + if (kernel_export->function_data.trampoline) { + handler = (FunctionInfo::ExternHandler) + kernel_export->function_data.trampoline; + } else { + handler = + (FunctionInfo::ExternHandler)kernel_export->function_data.shim; + } } else { handler = UndefinedImport; } diff --git a/src/xenia/kernel/xam_module.cc b/src/xenia/kernel/xam_module.cc index 8f7530999..4ec00c02a 100644 --- a/src/xenia/kernel/xam_module.cc +++ b/src/xenia/kernel/xam_module.cc @@ -33,21 +33,35 @@ XamModule::XamModule(Emulator* emulator, KernelState* kernel_state) xam::RegisterVoiceExports(export_resolver_, kernel_state_); } +std::vector xam_exports; + +xe::cpu::Export* RegisterExport_xam(xe::cpu::Export* export) { + if (xam_exports.size() <= export->ordinal) { + xam_exports.resize(xe::round_up(export->ordinal, 256)); + } + xam_exports[export->ordinal] = export; + return export; +} + void XamModule::RegisterExportTable(xe::cpu::ExportResolver* export_resolver) { assert_not_null(export_resolver); - if (!export_resolver) { - return; - } - // Build the export table used for resolution. #include "xenia/kernel/util/export_table_pre.inc" static xe::cpu::Export xam_export_table[] = { #include "xenia/kernel/xam_table.inc" }; #include "xenia/kernel/util/export_table_post.inc" - export_resolver->RegisterTable("xam.xex", xam_export_table, - xe::countof(xam_export_table)); + for (size_t i = 0; i < xe::countof(xam_export_table); ++i) { + auto& export = xam_export_table[i]; + if (xam_exports.size() <= export.ordinal) { + xam_exports.resize(xe::round_up(export.ordinal, 256)); + } + if (!xam_exports[export.ordinal]) { + xam_exports[export.ordinal] = &export; + } + } + export_resolver->RegisterTable("xam.xex", &xam_exports); } XamModule::~XamModule() {} diff --git a/src/xenia/kernel/xam_module.h b/src/xenia/kernel/xam_module.h index bb7f9b290..370ee472f 100644 --- a/src/xenia/kernel/xam_module.h +++ b/src/xenia/kernel/xam_module.h @@ -23,7 +23,6 @@ class XamModule : public XKernelModule { virtual ~XamModule(); static void RegisterExportTable(xe::cpu::ExportResolver* export_resolver); - static void RegisterExport(xe::cpu::Export* export); private: }; diff --git a/src/xenia/kernel/xam_private.h b/src/xenia/kernel/xam_private.h index 2bbfc527e..1167f68e0 100644 --- a/src/xenia/kernel/xam_private.h +++ b/src/xenia/kernel/xam_private.h @@ -18,6 +18,8 @@ namespace kernel { class KernelState; +xe::cpu::Export* RegisterExport_xam(xe::cpu::Export* export); + namespace xam { // Registration functions, one per file. void RegisterContentExports(xe::cpu::ExportResolver* export_resolver, diff --git a/src/xenia/kernel/xboxkrnl_module.cc b/src/xenia/kernel/xboxkrnl_module.cc index 583e572dd..c14cbcdfc 100644 --- a/src/xenia/kernel/xboxkrnl_module.cc +++ b/src/xenia/kernel/xboxkrnl_module.cc @@ -149,22 +149,36 @@ XboxkrnlModule::XboxkrnlModule(Emulator* emulator, KernelState* kernel_state) WT_EXECUTEINTIMERTHREAD); } +std::vector xboxkrnl_exports; + +xe::cpu::Export* RegisterExport_xboxkrnl(xe::cpu::Export* export) { + if (xboxkrnl_exports.size() <= export->ordinal) { + xboxkrnl_exports.resize(xe::round_up(export->ordinal, 256)); + } + xboxkrnl_exports[export->ordinal] = export; + return export; +} + void XboxkrnlModule::RegisterExportTable( xe::cpu::ExportResolver* export_resolver) { assert_not_null(export_resolver); - if (!export_resolver) { - return; - } - // Build the export table used for resolution. #include "xenia/kernel/util/export_table_pre.inc" static xe::cpu::Export xboxkrnl_export_table[] = { #include "xenia/kernel/xboxkrnl_table.inc" }; #include "xenia/kernel/util/export_table_post.inc" - export_resolver->RegisterTable("xboxkrnl.exe", xboxkrnl_export_table, - xe::countof(xboxkrnl_export_table)); + for (size_t i = 0; i < xe::countof(xboxkrnl_export_table); ++i) { + auto& export = xboxkrnl_export_table[i]; + if (xboxkrnl_exports.size() <= export.ordinal) { + xboxkrnl_exports.resize(xe::round_up(export.ordinal, 256)); + } + if (!xboxkrnl_exports[export.ordinal]) { + xboxkrnl_exports[export.ordinal] = &export; + } + } + export_resolver->RegisterTable("xboxkrnl.exe", &xboxkrnl_exports); } XboxkrnlModule::~XboxkrnlModule() { diff --git a/src/xenia/kernel/xboxkrnl_module.h b/src/xenia/kernel/xboxkrnl_module.h index 61870ec02..5e34e6c70 100644 --- a/src/xenia/kernel/xboxkrnl_module.h +++ b/src/xenia/kernel/xboxkrnl_module.h @@ -28,7 +28,6 @@ class XboxkrnlModule : public XKernelModule { virtual ~XboxkrnlModule(); static void RegisterExportTable(xe::cpu::ExportResolver* export_resolver); - static void RegisterExport(xe::cpu::Export* export); int LaunchModule(const char* path); diff --git a/src/xenia/kernel/xboxkrnl_private.h b/src/xenia/kernel/xboxkrnl_private.h index 042279cba..be0ff9267 100644 --- a/src/xenia/kernel/xboxkrnl_private.h +++ b/src/xenia/kernel/xboxkrnl_private.h @@ -17,6 +17,8 @@ namespace kernel { class KernelState; +xe::cpu::Export* RegisterExport_xboxkrnl(xe::cpu::Export* export); + namespace xboxkrnl { // Registration functions, one per file. void RegisterAudioExports(xe::cpu::ExportResolver* export_resolver,