service: sm/kernel/loader: Implement QueryPointerBufferSize, automatic pointer buffer sizing, and SM service improvements

This commit introduces multiple improvements to IPC handling and system management services, enhancing game compatibility and emulator stability.

--- 1. Fully Implemented QueryPointerBufferSize Service:
- Exposes the per-process IPC pointer buffer size through `QueryPointerBufferSize` instead of returning stubbed values.
- Added `m_pointer_buffer_size` field to `KProcess`, initialized with a safe default (0x8000).
- Introduced getter and setter methods (`GetPointerBufferSize()` / `SetPointerBufferSize()`).
- Registered new handler in `sm_controller` for handling QueryPointerBufferSize requests.
- Ensures accurate buffer size reporting for games relying on this service.

--- 2. Automatic Pointer Buffer Sizing Per-Game:
- Automatically determines heap size by parsing `main.npdm` from the game’s ExeFS:
  - Heap size > 1 GiB → pointer buffer size set to `0x10000`.
  - Heap size > 512 MiB → pointer buffer size set to `0xC000`.
  - Otherwise, defaults to `0x8000`.
- Gracefully handles missing or malformed `main.npdm` by falling back to default settings.
- Automatically configures pointer buffer size during `AppLoader_NCA::Load`.
- Added logging for heap size detection and buffer size configuration for easier debugging.

--- 3. SM Service Improvements:
- Added full implementation of `QueryPointerBufferSize` within the SM service framework.
- Cleaned up stubbed methods and ensured correct domain handling.
- Registered new service commands (e.g., `SetPointerBufferSize` and `QueryPointerBufferSize`) in `sm_controller`.
- Improved session handling with proper conversion to domain objects where necessary.

--- Benefits:
- Greatly improves compatibility with games that require larger IPC pointer buffers
- Eliminates the need for manual per-game pointer buffer overrides.
- More accurate emulation of Switch system services, improving stability for both commercial titles and homebrew.
- Provides cleaner logging for easier debugging and maintenance.
- Future-proofs IPC handling for upcoming titles with higher memory demands.

--- Additional Notes:
- Default pointer buffer size remains 0x8000 for smaller titles or if heap size cannot be determined.
- Falls back to safe defaults without affecting overall emulator performance.
- All new service calls properly registered and integrated without breaking existing functionality.
This commit is contained in:
JPikachu 2025-04-26 13:19:44 +01:00 committed by JPikachu
parent e57b12d020
commit 8599c47fe0
4 changed files with 83 additions and 4 deletions

View file

@ -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 {

View file

@ -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_Marshalling
Controller::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);
}

View file

@ -21,6 +21,7 @@ private:
void CloneCurrentObject(HLERequestContext& ctx);
void CloneCurrentObjectEx(HLERequestContext& ctx);
void QueryPointerBufferSize(HLERequestContext& ctx);
void SetPointerBufferSize(HLERequestContext& ctx);
};
} // namespace Service::SM