VERY experimental Nintendo Switch 2 emulator
C++
service: sm/kernel/loader: Implement QueryPointerBufferSize, automatic pointer buffer sizing, and SM service improvements
JPikachu8599c47fe06f
diff --git a/src/core/hle/kernel/k_process.h b/src/core/hle/kernel/k_process.hindex ab1358a129..f31f260d3c 100644--- a/src/core/hle/kernel/k_process.h+++ b/src/core/hle/kernel/k_process.h@@ -84,6 +84,7 @@ private:std::array<u64, 4> m_entropy{};bool m_is_signaled{};bool m_is_initialized{};+ u32 m_pointer_buffer_size = 0x8000; // Default pointer buffer size (can be game-specific later)bool m_is_application{};bool m_is_default_application_system_resource{};bool m_is_hbl{};@@ -239,6 +240,14 @@ public:m_is_suspended = suspended;}+ u32 GetPointerBufferSize() const {+ return m_pointer_buffer_size;+ }++ void SetPointerBufferSize(u32 size) {+ m_pointer_buffer_size = size;+ }+Result Terminate();bool IsTerminated() const {diff --git a/src/core/hle/service/sm/sm_controller.cpp b/src/core/hle/service/sm/sm_controller.cppindex 7f0fb91d04..9e25eae4d4 100644--- a/src/core/hle/service/sm/sm_controller.cpp+++ b/src/core/hle/service/sm/sm_controller.cpp@@ -68,13 +68,46 @@ void Controller::CloneCurrentObjectEx(HLERequestContext& ctx) {}void Controller::QueryPointerBufferSize(HLERequestContext& ctx) {- LOG_WARNING(Service, "(STUBBED) called");+ LOG_DEBUG(Service, "called");++ auto* process = Kernel::GetCurrentProcessPointer(kernel);+ ASSERT(process != nullptr);++ u32 buffer_size = process->GetPointerBufferSize();+ if (buffer_size > std::numeric_limits<u16>::max()) {+ LOG_WARNING(Service, "Pointer buffer size exceeds u16 max, clamping");+ buffer_size = std::numeric_limits<u16>::max();+ }IPC::ResponseBuilder rb{ctx, 3};rb.Push(ResultSuccess);- rb.Push<u16>(0x8000);+ rb.Push<u16>(static_cast<u16>(buffer_size));}+void Controller::SetPointerBufferSize(HLERequestContext& ctx) {+ LOG_DEBUG(Service, "called");++ auto* process = Kernel::GetCurrentProcessPointer(kernel);+ ASSERT(process != nullptr);++ IPC::RequestParser rp{ctx};++ u32 requested_size = rp.PopRaw<u32>();++ if (requested_size > std::numeric_limits<u16>::max()) {+ LOG_WARNING(Service, "Requested pointer buffer size too large, clamping to 0xFFFF");+ requested_size = std::numeric_limits<u16>::max();+ }++ process->SetPointerBufferSize(requested_size);++ LOG_INFO(Service, "Pointer buffer size dynamically updated to {:#x} bytes by process", requested_size);++ IPC::ResponseBuilder rb{ctx, 2};+ rb.Push(ResultSuccess);+}++// https://switchbrew.org/wiki/IPC_MarshallingController::Controller(Core::System& system_) : ServiceFramework{system_, "IpcController"} {static const FunctionInfo functions[] = {@@ -83,6 +116,7 @@ Controller::Controller(Core::System& system_) : ServiceFramework{system_, "IpcCo{2, &Controller::CloneCurrentObject, "CloneCurrentObject"},{3, &Controller::QueryPointerBufferSize, "QueryPointerBufferSize"},{4, &Controller::CloneCurrentObjectEx, "CloneCurrentObjectEx"},+ {5, &Controller::SetPointerBufferSize, "SetPointerBufferSize"},};RegisterHandlers(functions);}diff --git a/src/core/hle/service/sm/sm_controller.h b/src/core/hle/service/sm/sm_controller.hindex 4e748b36d9..f7e529a269 100644--- a/src/core/hle/service/sm/sm_controller.h+++ b/src/core/hle/service/sm/sm_controller.h@@ -21,6 +21,7 @@ private:void CloneCurrentObject(HLERequestContext& ctx);void CloneCurrentObjectEx(HLERequestContext& ctx);void QueryPointerBufferSize(HLERequestContext& ctx);+ void SetPointerBufferSize(HLERequestContext& ctx);};} // namespace Service::SMdiff --git a/src/core/loader/nca.cpp b/src/core/loader/nca.cppindex de27ec49e2..4a87ab53e7 100644--- a/src/core/loader/nca.cpp+++ b/src/core/loader/nca.cpp@@ -15,9 +15,20 @@#include "core/loader/deconstructed_rom_directory.h"#include "core/loader/nca.h"#include "mbedtls/sha256.h"+#include "common/literals.h"namespace Loader {+static u32 CalculatePointerBufferSize(size_t heap_size) {+ if (heap_size > 1073741824) { // Games with 1 GiB+ return 0x10000;+ } else if (heap_size > 536870912) { // Games with 512 MiB+ return 0xC000;+ } else {+ return 0x8000; // Default for all other games+ }+}+AppLoader_NCA::AppLoader_NCA(FileSys::VirtualFile file_): AppLoader(std::move(file_)), nca(std::make_unique<FileSys::NCA>(file)) {}@@ -52,8 +63,6 @@ AppLoader_NCA::LoadResult AppLoader_NCA::Load(Kernel::KProcess& process, Core::Sif (exefs == nullptr) {LOG_INFO(Loader, "No ExeFS found in NCA, looking for ExeFS from update");- // This NCA may be a sparse base of an installed title.- // Try to fetch the ExeFS from the installed update.const auto& installed = system.GetContentProvider();const auto update_nca = installed.GetEntry(FileSys::GetUpdateTitleID(nca->GetTitleId()),FileSys::ContentRecordType::Program);@@ -69,11 +78,37 @@ AppLoader_NCA::LoadResult AppLoader_NCA::Load(Kernel::KProcess& process, Core::Sdirectory_loader = std::make_unique<AppLoader_DeconstructedRomDirectory>(exefs, true);+ // Read heap size from main.npdm in ExeFS+ u64 heap_size = 0;++ if (exefs) {+ const auto npdm_file = exefs->GetFile("main.npdm");+ if (npdm_file) {+ auto npdm_data = npdm_file->ReadAllBytes();+ if (npdm_data.size() >= 0x30) {+ heap_size = *reinterpret_cast<const u64*>(&npdm_data[0x28]);+ LOG_INFO(Loader, "Read heap size {:#x} bytes from main.npdm", heap_size);+ } else {+ LOG_WARNING(Loader, "main.npdm too small to read heap size!");+ }+ } else {+ LOG_WARNING(Loader, "No main.npdm found in ExeFS!");+ }+ }++ // Set pointer buffer size based on heap size+ process.SetPointerBufferSize(CalculatePointerBufferSize(heap_size));++ // Load modulesconst auto load_result = directory_loader->Load(process, system);if (load_result.first != ResultStatus::Success) {return load_result;}+ LOG_INFO(Loader, "Set pointer buffer size to {:#x} bytes for ProgramID {:#018x} (Heap size: {:#x})",+ process.GetPointerBufferSize(), nca->GetTitleId(), heap_size);++ // Register the process in the file system controllersystem.GetFileSystemController().RegisterProcess(process.GetProcessId(), nca->GetTitleId(),std::make_shared<FileSys::RomFSFactory>(*this, system.GetContentProvider(),