From 74d6eaf7216de94ee07130e1ffb09645d8340422 Mon Sep 17 00:00:00 2001 From: SDK Chan Date: Sun, 16 Nov 2025 22:53:16 +0100 Subject: [PATCH] [gpu/nvdrv] Unstub SetErrorNotifier, add PostErrorNotification function (#2500) This commit should ensure that GPU error handling is handled correctly. Replace some unimplemented stubs with PostErrorNotification. I believe this should mitigate some hiccups encountered in unreal engine based games. Co-authored-by: Maufeat Reviewed-on: https://git.eden-emu.dev/eden-emu/eden/pulls/2500 Reviewed-by: Shinmegumi Reviewed-by: Caio Oliveira Reviewed-by: Maufeat Reviewed-by: Lizzie Co-authored-by: SDK Chan Co-committed-by: SDK Chan --- .../hle/service/nvdrv/devices/nvhost_gpu.cpp | 119 +++++++++++++++++- .../hle/service/nvdrv/devices/nvhost_gpu.h | 12 ++ 2 files changed, 126 insertions(+), 5 deletions(-) diff --git a/src/core/hle/service/nvdrv/devices/nvhost_gpu.cpp b/src/core/hle/service/nvdrv/devices/nvhost_gpu.cpp index aab4de39e4..abcebc3c2d 100644 --- a/src/core/hle/service/nvdrv/devices/nvhost_gpu.cpp +++ b/src/core/hle/service/nvdrv/devices/nvhost_gpu.cpp @@ -8,6 +8,7 @@ #include "common/assert.h" #include "common/logging/log.h" #include "core/core.h" +#include "core/hle/kernel/k_event.h" #include "core/hle/kernel/k_process.h" #include "core/hle/service/nvdrv/core/container.h" #include "core/hle/service/nvdrv/core/nvmap.h" @@ -161,11 +162,114 @@ NvResult nvhost_gpu::ZCullBind(IoctlZCullBind& params) { } NvResult nvhost_gpu::SetErrorNotifier(IoctlSetErrorNotifier& params) { - LOG_WARNING(Service_NVDRV, "(STUBBED) called, offset={:X}, size={:X}, mem={:X}", params.offset, - params.size, params.mem); + LOG_DEBUG(Service_NVDRV, "called, offset={:#X}, size={:#X}, mem={:#X}", + params.offset, params.size, params.mem); + + if (!params.mem || !params.size) { + std::scoped_lock lk(channel_mutex); + if (!channel_state->initialized) { + LOG_CRITICAL(Service_NVDRV, "No address space bound for setting up error notifier!"); + return NvResult::NotInitialized; + } + + error_notifier_params = {}; + LOG_DEBUG(Service_NVDRV, "Error notifier disabled!"); + return NvResult::Success; + } + + constexpr u64 error_notification_size = sizeof(IoctlGetErrorNotification); + if (params.size < error_notification_size) { + LOG_ERROR(Service_NVDRV, "Error notification size: {:#X} is too small. Need at least {:#X}", params.size, + error_notification_size); + return NvResult::InvalidSize; + } + + auto handle = nvmap.GetHandle(static_cast(params.mem)); + if (!handle) { + LOG_ERROR(Service_NVDRV, "Unknown nvmap handle id {:#X}", params.mem); + return NvResult::BadParameter; + } + + if (params.offset > handle->size || params.size > (handle->size - params.offset)) { + LOG_ERROR(Service_NVDRV, "Error notifier out of bounds: offset={:#X} size={:#X} handle size={:#X}", params.offset, + params.size, handle->size); + return NvResult::InvalidSize; + } + + u64 write_address, write_offset, handle_id; + { + std::scoped_lock lk(channel_mutex); + if (!channel_state->initialized) { + LOG_CRITICAL(Service_NVDRV, "No address space bound for setting up error notifier!"); + return NvResult::NotInitialized; + } + + error_notifier_params = params; + write_address = handle->address; + write_offset = params.offset; + handle_id = handle->id; + } + + if (write_address) { + IoctlGetErrorNotification error_notification{}; + error_notification.status = static_cast(NotifierStatus::NoError); + system.ApplicationMemory().WriteBlock(write_address + write_offset, &error_notification, sizeof(error_notification)); + } else { + LOG_WARNING(Service_NVDRV, + "nvmap handle id {:#X} has no virtual address assigned; " + "skipping initialization write for error notification!", + handle_id); + } + return NvResult::Success; } +void nvhost_gpu::PostErrorNotification(u32 info32, u16 info16, NotifierStatus status) { + IoctlSetErrorNotifier error_notifier_params_snapshot{}; + Kernel::KEvent *error_notifier_event_snapshot{}; + { + std::scoped_lock lk(channel_mutex); + error_notifier_params_snapshot = error_notifier_params; + error_notifier_event_snapshot = error_notifier_event; + } + + if (!error_notifier_params_snapshot.mem || error_notifier_params_snapshot.size < sizeof(IoctlGetErrorNotification)) { + LOG_DEBUG(Service_NVDRV, "PostErrorNotification: notifier not configured or too small!"); + return; + } + + auto handle = nvmap.GetHandle(static_cast(error_notifier_params_snapshot.mem)); + if (!handle || !handle->address) { + LOG_ERROR(Service_NVDRV, "PostErrorNotification: invalid handle or virtual address!"); + return; + } + + IoctlGetErrorNotification error_init{}; + error_init.info32 = info32; + error_init.info16 = info16; + error_init.status = static_cast(status); + const u64 write_size = std::min(sizeof(IoctlGetErrorNotification), + error_notifier_params_snapshot.size); + if (error_notifier_params_snapshot.offset >= handle->size || + write_size > (handle->size - error_notifier_params_snapshot.offset)) { + LOG_ERROR(Service_NVDRV, "PostErrorNotification: bounds check failed!"); + return; + } + + const u64 virtual_address = handle->address + error_notifier_params_snapshot.offset; + if (virtual_address < handle->address) { + LOG_ERROR(Service_NVDRV, "PostErrorNotification: virtual address overflow!"); + return; + } + + auto &application_memory = system.ApplicationMemory(); + application_memory.WriteBlock(virtual_address, &error_init, write_size); + + if (error_notifier_event_snapshot) { + error_notifier_event_snapshot->Signal(); + } +} + NvResult nvhost_gpu::SetChannelPriority(IoctlChannelSetPriority& params) { channel_priority = params.priority; LOG_INFO(Service_NVDRV, "called, priority={:X}", channel_priority); @@ -278,7 +382,7 @@ NvResult nvhost_gpu::AllocateObjectContext(IoctlAllocObjCtx& params) { params.flags = allowed_mask; } - s32_le ctx_class_number_index = + s32_le ctx_class_number_index = GetObjectContextClassNumberIndex(static_cast(params.class_num)); if (ctx_class_number_index < 0) { LOG_ERROR(Service_NVDRV, "Invalid class number for object context: {:#X}", @@ -357,6 +461,7 @@ NvResult nvhost_gpu::SubmitGPFIFOImpl(IoctlSubmitGpfifo& params, Tegra::CommandL if (flags.fence_wait.Value()) { if (flags.increment_value.Value()) { + PostErrorNotification(flags.raw, 0, NotifierStatus::GenericError); return NvResult::BadParameter; } @@ -390,7 +495,11 @@ NvResult nvhost_gpu::SubmitGPFIFOImpl(IoctlSubmitGpfifo& params, Tegra::CommandL NvResult nvhost_gpu::SubmitGPFIFOBase1(IoctlSubmitGpfifo& params, std::span commands, bool kickoff) { if (params.num_entries > commands.size()) { - UNIMPLEMENTED(); + LOG_ERROR(Service_NVDRV, + "SubmitGPFIFO: num_entries={:#X} > provided commands={:#X}", + params.num_entries, commands.size()); + + PostErrorNotification(params.num_entries, 0, NotifierStatus::BadGpfifo); return NvResult::InvalidSize; } @@ -409,7 +518,7 @@ NvResult nvhost_gpu::SubmitGPFIFOBase1(IoctlSubmitGpfifo& params, NvResult nvhost_gpu::SubmitGPFIFOBase2(IoctlSubmitGpfifo& params, std::span commands) { if (params.num_entries > commands.size()) { - UNIMPLEMENTED(); + PostErrorNotification(params.num_entries, 0, NotifierStatus::BadGpfifo); return NvResult::InvalidSize; } diff --git a/src/core/hle/service/nvdrv/devices/nvhost_gpu.h b/src/core/hle/service/nvdrv/devices/nvhost_gpu.h index b8854aab95..311a6a87c0 100644 --- a/src/core/hle/service/nvdrv/devices/nvhost_gpu.h +++ b/src/core/hle/service/nvdrv/devices/nvhost_gpu.h @@ -72,6 +72,16 @@ private: High = 0x96, }; + enum class NotifierStatus : u16_le { + NoError = 0xFFFF, + GenericError = 0x0001, + MmuFault = 0x0002, + IllegalMethod= 0x0003, + InvalidObject= 0x0004, + BadGpfifo = 0x0005, + TimeoutHang = 0x0006, + }; + struct IoctlSetNvmapFD { s32_le nvmap_fd{}; }; @@ -178,6 +188,8 @@ private: s32_le nvmap_fd{}; u64_le user_data{}; IoctlZCullBind zcull_params{}; + IoctlSetErrorNotifier error_notifier_params{}; + void PostErrorNotification(u32 info32, u16 info16, NotifierStatus status); std::array, 6> ctxObjs{}; u32_le channel_priority{}; u32_le channel_timeslice{};