From 19a2f12692f2ba871b4bec7c56757a455bf03e7d Mon Sep 17 00:00:00 2001 From: Kelebek1 Date: Sun, 28 Jan 2024 17:45:01 +0000 Subject: [PATCH] Use the input process handle to get the correct application's memory --- .../adsp/apps/audio_renderer/audio_renderer.cpp | 7 +++++-- .../adsp/apps/audio_renderer/audio_renderer.h | 7 ++++++- .../adsp/apps/audio_renderer/command_buffer.h | 5 +++++ .../audio_renderer/command_list_processor.cpp | 7 ++++--- .../apps/audio_renderer/command_list_processor.h | 7 ++++++- src/audio_core/renderer/audio_renderer.cpp | 6 ++++-- src/audio_core/renderer/audio_renderer.h | 6 ++++-- src/audio_core/renderer/system.cpp | 10 +++++++--- src/audio_core/renderer/system.h | 6 +++++- src/core/hle/service/audio/audren_u.cpp | 16 ++++++++++------ 10 files changed, 56 insertions(+), 21 deletions(-) diff --git a/src/audio_core/adsp/apps/audio_renderer/audio_renderer.cpp b/src/audio_core/adsp/apps/audio_renderer/audio_renderer.cpp index ef301d8b4..7a76c3d0b 100644 --- a/src/audio_core/adsp/apps/audio_renderer/audio_renderer.cpp +++ b/src/audio_core/adsp/apps/audio_renderer/audio_renderer.cpp @@ -89,11 +89,13 @@ u32 AudioRenderer::Receive(Direction dir) { } void AudioRenderer::SetCommandBuffer(s32 session_id, CpuAddr buffer, u64 size, u64 time_limit, - u64 applet_resource_user_id, bool reset) noexcept { + u64 applet_resource_user_id, Kernel::KProcess* process, + bool reset) noexcept { command_buffers[session_id].buffer = buffer; command_buffers[session_id].size = size; command_buffers[session_id].time_limit = time_limit; command_buffers[session_id].applet_resource_user_id = applet_resource_user_id; + command_buffers[session_id].process = process; command_buffers[session_id].reset_buffer = reset; } @@ -173,7 +175,8 @@ void AudioRenderer::Main(std::stop_token stop_token) { // If there are no remaining commands (from the previous list), // this is a new command list, initialize it. if (command_buffer.remaining_command_count == 0) { - command_list_processor.Initialize(system, command_buffer.buffer, + command_list_processor.Initialize(system, *command_buffer.process, + command_buffer.buffer, command_buffer.size, streams[index]); } diff --git a/src/audio_core/adsp/apps/audio_renderer/audio_renderer.h b/src/audio_core/adsp/apps/audio_renderer/audio_renderer.h index 57b89d9fe..875266f27 100644 --- a/src/audio_core/adsp/apps/audio_renderer/audio_renderer.h +++ b/src/audio_core/adsp/apps/audio_renderer/audio_renderer.h @@ -19,6 +19,10 @@ namespace Core { class System; } // namespace Core +namespace Kernel { +class KProcess; +} + namespace AudioCore { namespace Sink { class Sink; @@ -69,7 +73,8 @@ public: u32 Receive(Direction dir); void SetCommandBuffer(s32 session_id, CpuAddr buffer, u64 size, u64 time_limit, - u64 applet_resource_user_id, bool reset) noexcept; + u64 applet_resource_user_id, Kernel::KProcess* process, + bool reset) noexcept; u32 GetRemainCommandCount(s32 session_id) const noexcept; void ClearRemainCommandCount(s32 session_id) noexcept; u64 GetRenderingStartTick(s32 session_id) const noexcept; diff --git a/src/audio_core/adsp/apps/audio_renderer/command_buffer.h b/src/audio_core/adsp/apps/audio_renderer/command_buffer.h index 3fd1b09dc..d6a721f34 100644 --- a/src/audio_core/adsp/apps/audio_renderer/command_buffer.h +++ b/src/audio_core/adsp/apps/audio_renderer/command_buffer.h @@ -6,6 +6,10 @@ #include "audio_core/common/common.h" #include "common/common_types.h" +namespace Kernel { +class KProcess; +} + namespace AudioCore::ADSP::AudioRenderer { struct CommandBuffer { @@ -14,6 +18,7 @@ struct CommandBuffer { u64 size{}; u64 time_limit{}; u64 applet_resource_user_id{}; + Kernel::KProcess* process{}; bool reset_buffer{}; // Set by the DSP u32 remaining_command_count{}; diff --git a/src/audio_core/adsp/apps/audio_renderer/command_list_processor.cpp b/src/audio_core/adsp/apps/audio_renderer/command_list_processor.cpp index 24e4d0496..eef2c0b89 100644 --- a/src/audio_core/adsp/apps/audio_renderer/command_list_processor.cpp +++ b/src/audio_core/adsp/apps/audio_renderer/command_list_processor.cpp @@ -9,14 +9,15 @@ #include "common/settings.h" #include "core/core.h" #include "core/core_timing.h" +#include "core/hle/kernel/k_process.h" #include "core/memory.h" namespace AudioCore::ADSP::AudioRenderer { -void CommandListProcessor::Initialize(Core::System& system_, CpuAddr buffer, u64 size, - Sink::SinkStream* stream_) { +void CommandListProcessor::Initialize(Core::System& system_, Kernel::KProcess& process, + CpuAddr buffer, u64 size, Sink::SinkStream* stream_) { system = &system_; - memory = &system->ApplicationMemory(); + memory = &process.GetMemory(); stream = stream_; header = reinterpret_cast(buffer); commands = reinterpret_cast(buffer + sizeof(Renderer::CommandListHeader)); diff --git a/src/audio_core/adsp/apps/audio_renderer/command_list_processor.h b/src/audio_core/adsp/apps/audio_renderer/command_list_processor.h index 4e5fb793e..944e82505 100644 --- a/src/audio_core/adsp/apps/audio_renderer/command_list_processor.h +++ b/src/audio_core/adsp/apps/audio_renderer/command_list_processor.h @@ -16,6 +16,10 @@ class Memory; class System; } // namespace Core +namespace Kernel { +class KProcess; +} + namespace AudioCore { namespace Sink { class SinkStream; @@ -40,7 +44,8 @@ public: * @param size - The size of the buffer. * @param stream - The stream to be used for sending the samples. */ - void Initialize(Core::System& system, CpuAddr buffer, u64 size, Sink::SinkStream* stream); + void Initialize(Core::System& system, Kernel::KProcess& process, CpuAddr buffer, u64 size, + Sink::SinkStream* stream); /** * Set the maximum processing time for this command list. diff --git a/src/audio_core/renderer/audio_renderer.cpp b/src/audio_core/renderer/audio_renderer.cpp index 09efe9be9..df03d03aa 100644 --- a/src/audio_core/renderer/audio_renderer.cpp +++ b/src/audio_core/renderer/audio_renderer.cpp @@ -6,6 +6,7 @@ #include "audio_core/renderer/audio_renderer.h" #include "audio_core/renderer/system_manager.h" #include "core/core.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_transfer_memory.h" #include "core/hle/service/audio/errors.h" @@ -17,7 +18,8 @@ Renderer::Renderer(Core::System& system_, Manager& manager_, Kernel::KEvent* ren Result Renderer::Initialize(const AudioRendererParameterInternal& params, Kernel::KTransferMemory* transfer_memory, const u64 transfer_memory_size, const u32 process_handle, - const u64 applet_resource_user_id, const s32 session_id) { + Kernel::KProcess& process, const u64 applet_resource_user_id, + const s32 session_id) { if (params.execution_mode == ExecutionMode::Auto) { if (!manager.AddSystem(system)) { LOG_ERROR(Service_Audio, @@ -28,7 +30,7 @@ Result Renderer::Initialize(const AudioRendererParameterInternal& params, } initialized = true; - system.Initialize(params, transfer_memory, transfer_memory_size, process_handle, + system.Initialize(params, transfer_memory, transfer_memory_size, process_handle, process, applet_resource_user_id, session_id); return ResultSuccess; diff --git a/src/audio_core/renderer/audio_renderer.h b/src/audio_core/renderer/audio_renderer.h index 24650278b..1219f74ca 100644 --- a/src/audio_core/renderer/audio_renderer.h +++ b/src/audio_core/renderer/audio_renderer.h @@ -14,7 +14,8 @@ class System; namespace Kernel { class KTransferMemory; -} +class KProcess; +} // namespace Kernel namespace AudioCore { struct AudioRendererParameterInternal; @@ -44,7 +45,8 @@ public: */ Result Initialize(const AudioRendererParameterInternal& params, Kernel::KTransferMemory* transfer_memory, u64 transfer_memory_size, - u32 process_handle, u64 applet_resource_user_id, s32 session_id); + u32 process_handle, Kernel::KProcess& process, u64 applet_resource_user_id, + s32 session_id); /** * Finalize the renderer for shutdown. diff --git a/src/audio_core/renderer/system.cpp b/src/audio_core/renderer/system.cpp index 31f92087c..ca656edae 100644 --- a/src/audio_core/renderer/system.cpp +++ b/src/audio_core/renderer/system.cpp @@ -32,6 +32,7 @@ #include "core/core.h" #include "core/core_timing.h" #include "core/hle/kernel/k_event.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_transfer_memory.h" #include "core/memory.h" @@ -101,7 +102,8 @@ System::System(Core::System& core_, Kernel::KEvent* adsp_rendered_event_) Result System::Initialize(const AudioRendererParameterInternal& params, Kernel::KTransferMemory* transfer_memory, u64 transfer_memory_size, - u32 process_handle_, u64 applet_resource_user_id_, s32 session_id_) { + u32 process_handle_, Kernel::KProcess& process_, + u64 applet_resource_user_id_, s32 session_id_) { if (!CheckValidRevision(params.revision)) { return Service::Audio::ResultInvalidRevision; } @@ -117,6 +119,7 @@ Result System::Initialize(const AudioRendererParameterInternal& params, behavior.SetUserLibRevision(params.revision); process_handle = process_handle_; + process = &process_; applet_resource_user_id = applet_resource_user_id_; session_id = session_id_; @@ -129,7 +132,7 @@ Result System::Initialize(const AudioRendererParameterInternal& params, render_device = params.rendering_device; execution_mode = params.execution_mode; - core.ApplicationMemory().ZeroBlock(transfer_memory->GetSourceAddress(), transfer_memory_size); + process->GetMemory().ZeroBlock(transfer_memory->GetSourceAddress(), transfer_memory_size); // Note: We're not actually using the transfer memory because it's a pain to code for. // Allocate the memory normally instead and hope the game doesn't try to read anything back @@ -613,7 +616,8 @@ void System::SendCommandToDsp() { static_cast((time_limit_percent / 100) * 2'880'000.0 * (static_cast(render_time_limit_percent) / 100.0f))}; audio_renderer.SetCommandBuffer(session_id, translated_addr, command_size, time_limit, - applet_resource_user_id, reset_command_buffers); + applet_resource_user_id, process, + reset_command_buffers); reset_command_buffers = false; command_buffer_size = command_size; if (remaining_command_count == 0) { diff --git a/src/audio_core/renderer/system.h b/src/audio_core/renderer/system.h index 8a8341710..753a0b796 100644 --- a/src/audio_core/renderer/system.h +++ b/src/audio_core/renderer/system.h @@ -29,6 +29,7 @@ class System; namespace Kernel { class KEvent; +class KProcess; class KTransferMemory; } // namespace Kernel @@ -80,7 +81,8 @@ public: */ Result Initialize(const AudioRendererParameterInternal& params, Kernel::KTransferMemory* transfer_memory, u64 transfer_memory_size, - u32 process_handle, u64 applet_resource_user_id, s32 session_id); + u32 process_handle, Kernel::KProcess& process, u64 applet_resource_user_id, + s32 session_id); /** * Finalize the system. @@ -275,6 +277,8 @@ private: Common::Event terminate_event{}; /// Does what locks do std::mutex lock{}; + /// Process this audio render is operating within, used for memory reads/writes. + Kernel::KProcess* process{}; /// Handle for the process for this system, unused u32 process_handle{}; /// Applet resource id for this system, unused diff --git a/src/core/hle/service/audio/audren_u.cpp b/src/core/hle/service/audio/audren_u.cpp index 05581e6e0..10108abc0 100644 --- a/src/core/hle/service/audio/audren_u.cpp +++ b/src/core/hle/service/audio/audren_u.cpp @@ -35,10 +35,11 @@ public: explicit IAudioRenderer(Core::System& system_, Manager& manager_, AudioCore::AudioRendererParameterInternal& params, Kernel::KTransferMemory* transfer_memory, u64 transfer_memory_size, - u32 process_handle, u64 applet_resource_user_id, s32 session_id) + u32 process_handle, Kernel::KProcess& process_, + u64 applet_resource_user_id, s32 session_id) : ServiceFramework{system_, "IAudioRenderer"}, service_context{system_, "IAudioRenderer"}, rendered_event{service_context.CreateEvent("IAudioRendererEvent")}, manager{manager_}, - impl{std::make_unique(system_, manager, rendered_event)} { + impl{std::make_unique(system_, manager, rendered_event)}, process{process_} { // clang-format off static const FunctionInfo functions[] = { {0, &IAudioRenderer::GetSampleRate, "GetSampleRate"}, @@ -59,13 +60,15 @@ public: // clang-format on RegisterHandlers(functions); - impl->Initialize(params, transfer_memory, transfer_memory_size, process_handle, + process.Open(); + impl->Initialize(params, transfer_memory, transfer_memory_size, process_handle, process, applet_resource_user_id, session_id); } ~IAudioRenderer() override { impl->Finalize(); service_context.CloseEvent(rendered_event); + process.Close(); } private: @@ -235,6 +238,7 @@ private: Kernel::KEvent* rendered_event; Manager& manager; std::unique_ptr impl; + Kernel::KProcess& process; Common::ScratchBuffer output_buffer; Common::ScratchBuffer performance_buffer; }; @@ -455,7 +459,7 @@ void AudRenU::OpenAudioRenderer(HLERequestContext& ctx) { return; } - auto process{ctx.GetObjectFromHandle(process_handle)}; + auto process{ctx.GetObjectFromHandle(process_handle).GetPointerUnsafe()}; auto transfer_memory{ctx.GetObjectFromHandle(transfer_memory_handle)}; const auto session_id{impl->GetSessionId()}; @@ -472,7 +476,7 @@ void AudRenU::OpenAudioRenderer(HLERequestContext& ctx) { IPC::ResponseBuilder rb{ctx, 2, 0, 1}; rb.Push(ResultSuccess); rb.PushIpcInterface(system, *impl, params, transfer_memory.GetPointerUnsafe(), - transfer_memory_size, process_handle, + transfer_memory_size, process_handle, *process, applet_resource_user_id, session_id); } @@ -522,7 +526,7 @@ void AudRenU::GetAudioDeviceService(HLERequestContext& ctx) { } void AudRenU::OpenAudioRendererForManualExecution(HLERequestContext& ctx) { - LOG_DEBUG(Service_Audio, "called"); + LOG_ERROR(Service_Audio, "called. Implement me!"); } void AudRenU::GetAudioDeviceServiceWithRevisionInfo(HLERequestContext& ctx) {