diff options
Diffstat (limited to '')
27 files changed, 1332 insertions, 33 deletions
diff --git a/src/core/hle/kernel/k_client_port.cpp b/src/core/hle/kernel/k_client_port.cpp index 40e09e532..11b1b977e 100644 --- a/src/core/hle/kernel/k_client_port.cpp +++ b/src/core/hle/kernel/k_client_port.cpp @@ -3,6 +3,7 @@ #include "common/scope_exit.h" #include "core/hle/kernel/k_client_port.h" +#include "core/hle/kernel/k_light_session.h" #include "core/hle/kernel/k_port.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/k_scoped_resource_reservation.h" @@ -63,6 +64,7 @@ Result KClientPort::CreateSession(KClientSession** out) { R_UNLESS(session_reservation.Succeeded(), ResultLimitReached); // Allocate a session normally. + // TODO: Dynamic resource limits session = KSession::Create(m_kernel); // Check that we successfully created a session. @@ -119,4 +121,71 @@ Result KClientPort::CreateSession(KClientSession** out) { R_SUCCEED(); } +Result KClientPort::CreateLightSession(KLightClientSession** out) { + // Declare the session we're going to allocate. + KLightSession* session{}; + + // Reserve a new session from the resource limit. + KScopedResourceReservation session_reservation(GetCurrentProcessPointer(m_kernel), + Svc::LimitableResource::SessionCountMax); + R_UNLESS(session_reservation.Succeeded(), ResultLimitReached); + + // Allocate a session normally. + // TODO: Dynamic resource limits + session = KLightSession::Create(m_kernel); + + // Check that we successfully created a session. + R_UNLESS(session != nullptr, ResultOutOfResource); + + // Update the session counts. + { + ON_RESULT_FAILURE { + session->Close(); + }; + + // Atomically increment the number of sessions. + s32 new_sessions; + { + const auto max = m_max_sessions; + auto cur_sessions = m_num_sessions.load(std::memory_order_acquire); + do { + R_UNLESS(cur_sessions < max, ResultOutOfSessions); + new_sessions = cur_sessions + 1; + } while (!m_num_sessions.compare_exchange_weak(cur_sessions, new_sessions, + std::memory_order_relaxed)); + } + + // Atomically update the peak session tracking. + { + auto peak = m_peak_sessions.load(std::memory_order_acquire); + do { + if (peak >= new_sessions) { + break; + } + } while (!m_peak_sessions.compare_exchange_weak(peak, new_sessions, + std::memory_order_relaxed)); + } + } + + // Initialize the session. + session->Initialize(this, m_parent->GetName()); + + // Commit the session reservation. + session_reservation.Commit(); + + // Register the session. + KLightSession::Register(m_kernel, session); + ON_RESULT_FAILURE { + session->GetClientSession().Close(); + session->GetServerSession().Close(); + }; + + // Enqueue the session with our parent. + R_TRY(m_parent->EnqueueSession(std::addressof(session->GetServerSession()))); + + // We succeeded, so set the output. + *out = std::addressof(session->GetClientSession()); + R_SUCCEED(); +} + } // namespace Kernel diff --git a/src/core/hle/kernel/k_client_port.h b/src/core/hle/kernel/k_client_port.h index 23db06ddf..28b332608 100644 --- a/src/core/hle/kernel/k_client_port.h +++ b/src/core/hle/kernel/k_client_port.h @@ -11,6 +11,7 @@ namespace Kernel { +class KLightClientSession; class KClientSession; class KernelCore; class KPort; @@ -51,6 +52,7 @@ public: bool IsSignaled() const override; Result CreateSession(KClientSession** out); + Result CreateLightSession(KLightClientSession** out); private: std::atomic<s32> m_num_sessions{}; diff --git a/src/core/hle/kernel/k_light_client_session.cpp b/src/core/hle/kernel/k_light_client_session.cpp new file mode 100644 index 000000000..8ce3e1ae4 --- /dev/null +++ b/src/core/hle/kernel/k_light_client_session.cpp @@ -0,0 +1,31 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "core/hle/kernel/k_light_client_session.h" +#include "core/hle/kernel/k_light_session.h" +#include "core/hle/kernel/k_thread.h" + +namespace Kernel { + +KLightClientSession::KLightClientSession(KernelCore& kernel) : KAutoObject(kernel) {} + +KLightClientSession::~KLightClientSession() = default; + +void KLightClientSession::Destroy() { + m_parent->OnClientClosed(); +} + +void KLightClientSession::OnServerClosed() {} + +Result KLightClientSession::SendSyncRequest(u32* data) { + // Get the request thread. + KThread* cur_thread = GetCurrentThreadPointer(m_kernel); + + // Set the light data. + cur_thread->SetLightSessionData(data); + + // Send the request. + R_RETURN(m_parent->OnRequest(cur_thread)); +} + +} // namespace Kernel diff --git a/src/core/hle/kernel/k_light_client_session.h b/src/core/hle/kernel/k_light_client_session.h new file mode 100644 index 000000000..881a15cbd --- /dev/null +++ b/src/core/hle/kernel/k_light_client_session.h @@ -0,0 +1,39 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "core/hle/kernel/k_auto_object.h" +#include "core/hle/result.h" + +namespace Kernel { + +class KLightSession; + +class KLightClientSession final : public KAutoObject { + KERNEL_AUTOOBJECT_TRAITS(KLightClientSession, KAutoObject); + +public: + explicit KLightClientSession(KernelCore& kernel); + ~KLightClientSession(); + + void Initialize(KLightSession* parent) { + // Set member variables. + m_parent = parent; + } + + virtual void Destroy() override; + + const KLightSession* GetParent() const { + return m_parent; + } + + Result SendSyncRequest(u32* data); + + void OnServerClosed(); + +private: + KLightSession* m_parent; +}; + +} // namespace Kernel diff --git a/src/core/hle/kernel/k_light_server_session.cpp b/src/core/hle/kernel/k_light_server_session.cpp new file mode 100644 index 000000000..e5ceb01f2 --- /dev/null +++ b/src/core/hle/kernel/k_light_server_session.cpp @@ -0,0 +1,247 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "core/hle/kernel/k_light_server_session.h" +#include "core/hle/kernel/k_light_session.h" +#include "core/hle/kernel/k_thread.h" +#include "core/hle/kernel/k_thread_queue.h" +#include "core/hle/kernel/svc_results.h" + +namespace Kernel { + +namespace { + +constexpr u64 InvalidThreadId = std::numeric_limits<u64>::max(); + +class ThreadQueueImplForKLightServerSessionRequest final : public KThreadQueue { +private: + KThread::WaiterList* m_wait_list; + +public: + ThreadQueueImplForKLightServerSessionRequest(KernelCore& kernel, KThread::WaiterList* wl) + : KThreadQueue(kernel), m_wait_list(wl) {} + + virtual void EndWait(KThread* waiting_thread, Result wait_result) override { + // Remove the thread from our wait list. + m_wait_list->erase(m_wait_list->iterator_to(*waiting_thread)); + + // Invoke the base end wait handler. + KThreadQueue::EndWait(waiting_thread, wait_result); + } + + virtual void CancelWait(KThread* waiting_thread, Result wait_result, + bool cancel_timer_task) override { + // Remove the thread from our wait list. + m_wait_list->erase(m_wait_list->iterator_to(*waiting_thread)); + + // Invoke the base cancel wait handler. + KThreadQueue::CancelWait(waiting_thread, wait_result, cancel_timer_task); + } +}; + +class ThreadQueueImplForKLightServerSessionReceive final : public KThreadQueue { +private: + KThread** m_server_thread; + +public: + ThreadQueueImplForKLightServerSessionReceive(KernelCore& kernel, KThread** st) + : KThreadQueue(kernel), m_server_thread(st) {} + + virtual void EndWait(KThread* waiting_thread, Result wait_result) override { + // Clear the server thread. + *m_server_thread = nullptr; + + // Set the waiting thread as not cancelable. + waiting_thread->ClearCancellable(); + + // Invoke the base end wait handler. + KThreadQueue::EndWait(waiting_thread, wait_result); + } + + virtual void CancelWait(KThread* waiting_thread, Result wait_result, + bool cancel_timer_task) override { + // Clear the server thread. + *m_server_thread = nullptr; + + // Set the waiting thread as not cancelable. + waiting_thread->ClearCancellable(); + + // Invoke the base cancel wait handler. + KThreadQueue::CancelWait(waiting_thread, wait_result, cancel_timer_task); + } +}; + +} // namespace + +KLightServerSession::KLightServerSession(KernelCore& kernel) : KAutoObject(kernel) {} +KLightServerSession::~KLightServerSession() = default; + +void KLightServerSession::Destroy() { + this->CleanupRequests(); + + m_parent->OnServerClosed(); +} + +void KLightServerSession::OnClientClosed() { + this->CleanupRequests(); +} + +Result KLightServerSession::OnRequest(KThread* request_thread) { + ThreadQueueImplForKLightServerSessionRequest wait_queue(m_kernel, + std::addressof(m_request_list)); + + // Send the request. + { + // Lock the scheduler. + KScopedSchedulerLock sl(m_kernel); + + // Check that the server isn't closed. + R_UNLESS(!m_parent->IsServerClosed(), ResultSessionClosed); + + // Check that the request thread isn't terminating. + R_UNLESS(!request_thread->IsTerminationRequested(), ResultTerminationRequested); + + // Add the request thread to our list. + m_request_list.push_back(*request_thread); + + // Begin waiting on the request. + request_thread->SetWaitReasonForDebugging(ThreadWaitReasonForDebugging::IPC); + request_thread->BeginWait(std::addressof(wait_queue)); + + // If we have a server thread, end its wait. + if (m_server_thread != nullptr) { + m_server_thread->EndWait(ResultSuccess); + } + } + + // NOTE: Nintendo returns GetCurrentThread().GetWaitResult() here. + // This is technically incorrect, although it doesn't cause problems in practice + // because this is only ever called with request_thread = GetCurrentThreadPointer(). + R_RETURN(request_thread->GetWaitResult()); +} + +Result KLightServerSession::ReplyAndReceive(u32* data) { + // Set the server context. + GetCurrentThread(m_kernel).SetLightSessionData(data); + + // Reply, if we need to. + if (data[0] & KLightSession::ReplyFlag) { + KScopedSchedulerLock sl(m_kernel); + + // Check that we're open. + R_UNLESS(!m_parent->IsClientClosed(), ResultSessionClosed); + R_UNLESS(!m_parent->IsServerClosed(), ResultSessionClosed); + + // Check that we have a request to reply to. + R_UNLESS(m_current_request != nullptr, ResultInvalidState); + + // Check that the server thread id is correct. + R_UNLESS(m_server_thread_id == GetCurrentThread(m_kernel).GetId(), ResultInvalidState); + + // If we can reply, do so. + if (!m_current_request->IsTerminationRequested()) { + std::memcpy(m_current_request->GetLightSessionData(), + GetCurrentThread(m_kernel).GetLightSessionData(), KLightSession::DataSize); + m_current_request->EndWait(ResultSuccess); + } + + // Close our current request. + m_current_request->Close(); + + // Clear our current request. + m_current_request = nullptr; + m_server_thread_id = InvalidThreadId; + } + + // Create the wait queue for our receive. + ThreadQueueImplForKLightServerSessionReceive wait_queue(m_kernel, + std::addressof(m_server_thread)); + + // Receive. + while (true) { + // Try to receive a request. + { + KScopedSchedulerLock sl(m_kernel); + + // Check that we aren't already receiving. + R_UNLESS(m_server_thread == nullptr, ResultInvalidState); + R_UNLESS(m_server_thread_id == InvalidThreadId, ResultInvalidState); + + // Check that we're open. + R_UNLESS(!m_parent->IsClientClosed(), ResultSessionClosed); + R_UNLESS(!m_parent->IsServerClosed(), ResultSessionClosed); + + // Check that we're not terminating. + R_UNLESS(!GetCurrentThread(m_kernel).IsTerminationRequested(), + ResultTerminationRequested); + + // If we have a request available, use it. + if (auto head = m_request_list.begin(); head != m_request_list.end()) { + // Set our current request. + m_current_request = std::addressof(*head); + m_current_request->Open(); + + // Set our server thread id. + m_server_thread_id = GetCurrentThread(m_kernel).GetId(); + + // Copy the client request data. + std::memcpy(GetCurrentThread(m_kernel).GetLightSessionData(), + m_current_request->GetLightSessionData(), KLightSession::DataSize); + + // We successfully received. + R_SUCCEED(); + } + + // We need to wait for a request to come in. + + // Check if we were cancelled. + if (GetCurrentThread(m_kernel).IsWaitCancelled()) { + GetCurrentThread(m_kernel).ClearWaitCancelled(); + R_THROW(ResultCancelled); + } + + // Mark ourselves as cancellable. + GetCurrentThread(m_kernel).SetCancellable(); + + // Wait for a request to come in. + m_server_thread = GetCurrentThreadPointer(m_kernel); + GetCurrentThread(m_kernel).SetWaitReasonForDebugging(ThreadWaitReasonForDebugging::IPC); + GetCurrentThread(m_kernel).BeginWait(std::addressof(wait_queue)); + } + + // We waited to receive a request; if our wait failed, return the failing result. + R_TRY(GetCurrentThread(m_kernel).GetWaitResult()); + } +} + +void KLightServerSession::CleanupRequests() { + // Cleanup all pending requests. + { + KScopedSchedulerLock sl(m_kernel); + + // Handle the current request. + if (m_current_request != nullptr) { + // Reply to the current request. + if (!m_current_request->IsTerminationRequested()) { + m_current_request->EndWait(ResultSessionClosed); + } + + // Clear our current request. + m_current_request->Close(); + m_current_request = nullptr; + m_server_thread_id = InvalidThreadId; + } + + // Reply to all other requests. + for (auto& thread : m_request_list) { + thread.EndWait(ResultSessionClosed); + } + + // Wait up our server thread, if we have one. + if (m_server_thread != nullptr) { + m_server_thread->EndWait(ResultSessionClosed); + } + } +} + +} // namespace Kernel diff --git a/src/core/hle/kernel/k_light_server_session.h b/src/core/hle/kernel/k_light_server_session.h new file mode 100644 index 000000000..8eca3eab6 --- /dev/null +++ b/src/core/hle/kernel/k_light_server_session.h @@ -0,0 +1,49 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "core/hle/kernel/k_auto_object.h" +#include "core/hle/kernel/k_thread.h" +#include "core/hle/result.h" + +namespace Kernel { + +class KLightSession; + +class KLightServerSession final : public KAutoObject, + public Common::IntrusiveListBaseNode<KLightServerSession> { + KERNEL_AUTOOBJECT_TRAITS(KLightServerSession, KAutoObject); + +private: + KLightSession* m_parent{}; + KThread::WaiterList m_request_list{}; + KThread* m_current_request{}; + u64 m_server_thread_id{std::numeric_limits<u64>::max()}; + KThread* m_server_thread{}; + +public: + explicit KLightServerSession(KernelCore& kernel); + ~KLightServerSession(); + + void Initialize(KLightSession* parent) { + // Set member variables. */ + m_parent = parent; + } + + virtual void Destroy() override; + + constexpr const KLightSession* GetParent() const { + return m_parent; + } + + Result OnRequest(KThread* request_thread); + Result ReplyAndReceive(u32* data); + + void OnClientClosed(); + +private: + void CleanupRequests(); +}; + +} // namespace Kernel diff --git a/src/core/hle/kernel/k_light_session.cpp b/src/core/hle/kernel/k_light_session.cpp new file mode 100644 index 000000000..d8b1e6958 --- /dev/null +++ b/src/core/hle/kernel/k_light_session.cpp @@ -0,0 +1,81 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "core/hle/kernel/k_client_port.h" +#include "core/hle/kernel/k_light_client_session.h" +#include "core/hle/kernel/k_light_server_session.h" +#include "core/hle/kernel/k_light_session.h" +#include "core/hle/kernel/k_process.h" + +namespace Kernel { + +KLightSession::KLightSession(KernelCore& kernel) + : KAutoObjectWithSlabHeapAndContainer(kernel), m_server(kernel), m_client(kernel) {} +KLightSession::~KLightSession() = default; + +void KLightSession::Initialize(KClientPort* client_port, uintptr_t name) { + // Increment reference count. + // Because reference count is one on creation, this will result + // in a reference count of two. Thus, when both server and client are closed + // this object will be destroyed. + this->Open(); + + // Create our sub sessions. + KAutoObject::Create(std::addressof(m_server)); + KAutoObject::Create(std::addressof(m_client)); + + // Initialize our sub sessions. + m_server.Initialize(this); + m_client.Initialize(this); + + // Set state and name. + m_state = State::Normal; + m_name = name; + + // Set our owner process. + m_process = GetCurrentProcessPointer(m_kernel); + m_process->Open(); + + // Set our port. + m_port = client_port; + if (m_port != nullptr) { + m_port->Open(); + } + + // Mark initialized. + m_initialized = true; +} + +void KLightSession::Finalize() { + if (m_port != nullptr) { + m_port->OnSessionFinalized(); + m_port->Close(); + } +} + +void KLightSession::OnServerClosed() { + if (m_state == State::Normal) { + m_state = State::ServerClosed; + m_client.OnServerClosed(); + } + + this->Close(); +} + +void KLightSession::OnClientClosed() { + if (m_state == State::Normal) { + m_state = State::ClientClosed; + m_server.OnClientClosed(); + } + + this->Close(); +} + +void KLightSession::PostDestroy(uintptr_t arg) { + // Release the session count resource the owner process holds. + KProcess* owner = reinterpret_cast<KProcess*>(arg); + owner->ReleaseResource(Svc::LimitableResource::SessionCountMax, 1); + owner->Close(); +} + +} // namespace Kernel diff --git a/src/core/hle/kernel/k_light_session.h b/src/core/hle/kernel/k_light_session.h new file mode 100644 index 000000000..f78d8e689 --- /dev/null +++ b/src/core/hle/kernel/k_light_session.h @@ -0,0 +1,86 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "core/hle/kernel/k_light_client_session.h" +#include "core/hle/kernel/k_light_server_session.h" +#include "core/hle/kernel/slab_helpers.h" +#include "core/hle/result.h" + +namespace Kernel { + +class KClientPort; +class KProcess; + +// TODO: SupportDynamicExpansion for SlabHeap +class KLightSession final + : public KAutoObjectWithSlabHeapAndContainer<KLightSession, KAutoObjectWithList> { + KERNEL_AUTOOBJECT_TRAITS(KLightSession, KAutoObject); + +private: + enum class State : u8 { + Invalid = 0, + Normal = 1, + ClientClosed = 2, + ServerClosed = 3, + }; + +public: + static constexpr size_t DataSize = sizeof(u32) * 7; + static constexpr u32 ReplyFlag = (1U << 31); + +private: + KLightServerSession m_server; + KLightClientSession m_client; + State m_state{State::Invalid}; + KClientPort* m_port{}; + uintptr_t m_name{}; + KProcess* m_process{}; + bool m_initialized{}; + +public: + explicit KLightSession(KernelCore& kernel); + ~KLightSession(); + + void Initialize(KClientPort* client_port, uintptr_t name); + void Finalize() override; + + bool IsInitialized() const override { + return m_initialized; + } + uintptr_t GetPostDestroyArgument() const override { + return reinterpret_cast<uintptr_t>(m_process); + } + + static void PostDestroy(uintptr_t arg); + + void OnServerClosed(); + void OnClientClosed(); + + bool IsServerClosed() const { + return m_state != State::Normal; + } + bool IsClientClosed() const { + return m_state != State::Normal; + } + + Result OnRequest(KThread* request_thread) { + R_RETURN(m_server.OnRequest(request_thread)); + } + + KLightClientSession& GetClientSession() { + return m_client; + } + KLightServerSession& GetServerSession() { + return m_server; + } + const KLightClientSession& GetClientSession() const { + return m_client; + } + const KLightServerSession& GetServerSession() const { + return m_server; + } +}; + +} // namespace Kernel diff --git a/src/core/hle/kernel/k_port.cpp b/src/core/hle/kernel/k_port.cpp index 1621ca1d3..e5f5d8028 100644 --- a/src/core/hle/kernel/k_port.cpp +++ b/src/core/hle/kernel/k_port.cpp @@ -58,4 +58,13 @@ Result KPort::EnqueueSession(KServerSession* session) { R_SUCCEED(); } +Result KPort::EnqueueSession(KLightServerSession* session) { + KScopedSchedulerLock sl{m_kernel}; + + R_UNLESS(m_state == State::Normal, ResultPortClosed); + + m_server.EnqueueSession(session); + R_SUCCEED(); +} + } // namespace Kernel diff --git a/src/core/hle/kernel/k_port.h b/src/core/hle/kernel/k_port.h index 991be27ab..26f5f14ef 100644 --- a/src/core/hle/kernel/k_port.h +++ b/src/core/hle/kernel/k_port.h @@ -13,6 +13,7 @@ namespace Kernel { +class KLightServerSession; class KServerSession; class KPort final : public KAutoObjectWithSlabHeapAndContainer<KPort, KAutoObjectWithList> { @@ -38,6 +39,7 @@ public: bool IsServerClosed() const; Result EnqueueSession(KServerSession* session); + Result EnqueueSession(KLightServerSession* session); KClientPort& GetClientPort() { return m_client; diff --git a/src/core/hle/kernel/k_server_port.cpp b/src/core/hle/kernel/k_server_port.cpp index a29d34bc1..bb6632f58 100644 --- a/src/core/hle/kernel/k_server_port.cpp +++ b/src/core/hle/kernel/k_server_port.cpp @@ -27,12 +27,14 @@ bool KServerPort::IsLight() const { void KServerPort::CleanupSessions() { // Ensure our preconditions are met. if (this->IsLight()) { - UNIMPLEMENTED(); + ASSERT(m_session_list.empty()); + } else { + ASSERT(m_light_session_list.empty()); } // Cleanup the session list. while (true) { - // Get the last session in the list + // Get the last session in the list. KServerSession* session = nullptr; { KScopedSchedulerLock sl{m_kernel}; @@ -49,6 +51,26 @@ void KServerPort::CleanupSessions() { break; } } + + // Cleanup the light session list. + while (true) { + // Get the last session in the list. + KLightServerSession* session = nullptr; + { + KScopedSchedulerLock sl{m_kernel}; + if (!m_light_session_list.empty()) { + session = std::addressof(m_light_session_list.front()); + m_light_session_list.pop_front(); + } + } + + // Close the session. + if (session != nullptr) { + session->Close(); + } else { + break; + } + } } void KServerPort::Destroy() { @@ -64,8 +86,7 @@ void KServerPort::Destroy() { bool KServerPort::IsSignaled() const { if (this->IsLight()) { - UNIMPLEMENTED(); - return false; + return !m_light_session_list.empty(); } else { return !m_session_list.empty(); } @@ -83,6 +104,18 @@ void KServerPort::EnqueueSession(KServerSession* session) { } } +void KServerPort::EnqueueSession(KLightServerSession* session) { + ASSERT(this->IsLight()); + + KScopedSchedulerLock sl{m_kernel}; + + // Add the session to our queue. + m_light_session_list.push_back(*session); + if (m_light_session_list.size() == 1) { + this->NotifyAvailable(); + } +} + KServerSession* KServerPort::AcceptSession() { ASSERT(!this->IsLight()); @@ -98,4 +131,19 @@ KServerSession* KServerPort::AcceptSession() { return session; } +KLightServerSession* KServerPort::AcceptLightSession() { + ASSERT(this->IsLight()); + + KScopedSchedulerLock sl{m_kernel}; + + // Return the first session in the list. + if (m_light_session_list.empty()) { + return nullptr; + } + + KLightServerSession* session = std::addressof(m_light_session_list.front()); + m_light_session_list.pop_front(); + return session; +} + } // namespace Kernel diff --git a/src/core/hle/kernel/k_server_port.h b/src/core/hle/kernel/k_server_port.h index 625280290..72fdb6734 100644 --- a/src/core/hle/kernel/k_server_port.h +++ b/src/core/hle/kernel/k_server_port.h @@ -9,6 +9,7 @@ #include "common/intrusive_list.h" +#include "core/hle/kernel/k_light_server_session.h" #include "core/hle/kernel/k_server_session.h" #include "core/hle/kernel/k_synchronization_object.h" @@ -28,8 +29,10 @@ public: void Initialize(KPort* parent); void EnqueueSession(KServerSession* session); + void EnqueueSession(KLightServerSession* session); KServerSession* AcceptSession(); + KLightServerSession* AcceptLightSession(); const KPort* GetParent() const { return m_parent; @@ -43,10 +46,12 @@ public: private: using SessionList = Common::IntrusiveListBaseTraits<KServerSession>::ListType; + using LightSessionList = Common::IntrusiveListBaseTraits<KLightServerSession>::ListType; void CleanupSessions(); SessionList m_session_list{}; + LightSessionList m_light_session_list{}; KPort* m_parent{}; }; diff --git a/src/core/hle/kernel/k_thread.h b/src/core/hle/kernel/k_thread.h index 390db2409..e9925d231 100644 --- a/src/core/hle/kernel/k_thread.h +++ b/src/core/hle/kernel/k_thread.h @@ -385,6 +385,13 @@ public: m_cancellable = false; } + u32* GetLightSessionData() const { + return m_light_ipc_data; + } + void SetLightSessionData(u32* data) { + m_light_ipc_data = data; + } + bool IsTerminationRequested() const { return m_termination_requested || GetRawState() == ThreadState::Terminated; } diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index 032c4e093..8cb05ca0b 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -1340,6 +1340,7 @@ struct KernelCore::SlabHeapContainer { KSlabHeap<KProcess> process; KSlabHeap<KResourceLimit> resource_limit; KSlabHeap<KSession> session; + KSlabHeap<KLightSession> light_session; KSlabHeap<KSharedMemory> shared_memory; KSlabHeap<KSharedMemoryInfo> shared_memory_info; KSlabHeap<KThread> thread; @@ -1370,6 +1371,8 @@ KSlabHeap<T>& KernelCore::SlabHeap() { return slab_heap_container->resource_limit; } else if constexpr (std::is_same_v<T, KSession>) { return slab_heap_container->session; + } else if constexpr (std::is_same_v<T, KLightSession>) { + return slab_heap_container->light_session; } else if constexpr (std::is_same_v<T, KSharedMemory>) { return slab_heap_container->shared_memory; } else if constexpr (std::is_same_v<T, KSharedMemoryInfo>) { @@ -1407,6 +1410,7 @@ template KSlabHeap<KPort>& KernelCore::SlabHeap(); template KSlabHeap<KProcess>& KernelCore::SlabHeap(); template KSlabHeap<KResourceLimit>& KernelCore::SlabHeap(); template KSlabHeap<KSession>& KernelCore::SlabHeap(); +template KSlabHeap<KLightSession>& KernelCore::SlabHeap(); template KSlabHeap<KSharedMemory>& KernelCore::SlabHeap(); template KSlabHeap<KSharedMemoryInfo>& KernelCore::SlabHeap(); template KSlabHeap<KThread>& KernelCore::SlabHeap(); diff --git a/src/core/hle/kernel/svc/svc_light_ipc.cpp b/src/core/hle/kernel/svc/svc_light_ipc.cpp index d757d5af2..4772cbda1 100644 --- a/src/core/hle/kernel/svc/svc_light_ipc.cpp +++ b/src/core/hle/kernel/svc/svc_light_ipc.cpp @@ -1,21 +1,40 @@ // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later -#include "core/arm/arm_interface.h" #include "core/core.h" +#include "core/hle/kernel/k_light_client_session.h" +#include "core/hle/kernel/k_light_server_session.h" +#include "core/hle/kernel/k_process.h" +#include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/svc.h" #include "core/hle/kernel/svc_results.h" namespace Kernel::Svc { Result SendSyncRequestLight(Core::System& system, Handle session_handle, u32* args) { - UNIMPLEMENTED(); - R_THROW(ResultNotImplemented); + // Get the light client session from its handle. + KScopedAutoObject session = GetCurrentProcess(system.Kernel()) + .GetHandleTable() + .GetObject<KLightClientSession>(session_handle); + R_UNLESS(session.IsNotNull(), ResultInvalidHandle); + + // Send the request. + R_TRY(session->SendSyncRequest(args)); + + R_SUCCEED(); } Result ReplyAndReceiveLight(Core::System& system, Handle session_handle, u32* args) { - UNIMPLEMENTED(); - R_THROW(ResultNotImplemented); + // Get the light server session from its handle. + KScopedAutoObject session = GetCurrentProcess(system.Kernel()) + .GetHandleTable() + .GetObject<KLightServerSession>(session_handle); + R_UNLESS(session.IsNotNull(), ResultInvalidHandle); + + // Handle the request. + R_TRY(session->ReplyAndReceive(args)); + + R_SUCCEED(); } Result SendSyncRequestLight64(Core::System& system, Handle session_handle, u32* args) { diff --git a/src/core/hle/kernel/svc/svc_port.cpp b/src/core/hle/kernel/svc/svc_port.cpp index abba757c7..737749f7d 100644 --- a/src/core/hle/kernel/svc/svc_port.cpp +++ b/src/core/hle/kernel/svc/svc_port.cpp @@ -5,6 +5,7 @@ #include "core/core.h" #include "core/hle/kernel/k_client_port.h" #include "core/hle/kernel/k_client_session.h" +#include "core/hle/kernel/k_light_client_session.h" #include "core/hle/kernel/k_object_name.h" #include "core/hle/kernel/k_port.h" #include "core/hle/kernel/k_process.h" @@ -51,13 +52,73 @@ Result ConnectToNamedPort(Core::System& system, Handle* out, u64 user_name) { Result CreatePort(Core::System& system, Handle* out_server, Handle* out_client, int32_t max_sessions, bool is_light, uint64_t name) { - UNIMPLEMENTED(); - R_THROW(ResultNotImplemented); + auto& kernel = system.Kernel(); + + // Ensure max sessions is valid. + R_UNLESS(max_sessions > 0, ResultOutOfRange); + + // Get the current handle table. + auto& handle_table = GetCurrentProcess(kernel).GetHandleTable(); + + // Create a new port. + KPort* port = KPort::Create(kernel); + R_UNLESS(port != nullptr, ResultOutOfResource); + + // Initialize the port. + port->Initialize(max_sessions, is_light, name); + + // Ensure that we clean up the port (and its only references are handle table) on function end. + SCOPE_EXIT({ + port->GetServerPort().Close(); + port->GetClientPort().Close(); + }); + + // Register the port. + KPort::Register(kernel, port); + + // Add the client to the handle table. + R_TRY(handle_table.Add(out_client, std::addressof(port->GetClientPort()))); + + // Ensure that we maintain a clean handle state on exit. + ON_RESULT_FAILURE { + handle_table.Remove(*out_client); + }; + + // Add the server to the handle table. + R_RETURN(handle_table.Add(out_server, std::addressof(port->GetServerPort()))); } -Result ConnectToPort(Core::System& system, Handle* out_handle, Handle port) { - UNIMPLEMENTED(); - R_THROW(ResultNotImplemented); +Result ConnectToPort(Core::System& system, Handle* out, Handle port) { + // Get the current handle table. + auto& handle_table = GetCurrentProcess(system.Kernel()).GetHandleTable(); + + // Get the client port. + KScopedAutoObject client_port = handle_table.GetObject<KClientPort>(port); + R_UNLESS(client_port.IsNotNull(), ResultInvalidHandle); + + // Reserve a handle for the port. + // NOTE: Nintendo really does write directly to the output handle here. + R_TRY(handle_table.Reserve(out)); + ON_RESULT_FAILURE { + handle_table.Unreserve(*out); + }; + + // Create the session. + KAutoObject* session; + if (client_port->IsLight()) { + R_TRY(client_port->CreateLightSession( + reinterpret_cast<KLightClientSession**>(std::addressof(session)))); + } else { + R_TRY(client_port->CreateSession( + reinterpret_cast<KClientSession**>(std::addressof(session)))); + } + + // Register the session. + handle_table.Register(*out, session); + session->Close(); + + // We succeeded. + R_SUCCEED(); } Result ManageNamedPort(Core::System& system, Handle* out_server_handle, uint64_t user_name, diff --git a/src/core/hle/kernel/svc/svc_session.cpp b/src/core/hle/kernel/svc/svc_session.cpp index 01b8a52ad..2f5905f32 100644 --- a/src/core/hle/kernel/svc/svc_session.cpp +++ b/src/core/hle/kernel/svc/svc_session.cpp @@ -3,8 +3,10 @@ #include "common/scope_exit.h" #include "core/core.h" +#include "core/hle/kernel/k_light_session.h" #include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_scoped_resource_reservation.h" +#include "core/hle/kernel/k_server_port.h" #include "core/hle/kernel/k_session.h" #include "core/hle/kernel/svc.h" @@ -20,7 +22,7 @@ Result CreateSession(Core::System& system, Handle* out_server, Handle* out_clien T* session; // Reserve a new session from the process resource limit. - // FIXME: LimitableResource_SessionCountMax + // TODO: Dynamic resource limits KScopedResourceReservation session_reservation(std::addressof(process), LimitableResource::SessionCountMax); if (session_reservation.Succeeded()) { @@ -92,16 +94,42 @@ Result CreateSession(Core::System& system, Handle* out_server, Handle* out_clien Result CreateSession(Core::System& system, Handle* out_server, Handle* out_client, bool is_light, u64 name) { if (is_light) { - // return CreateSession<KLightSession>(system, out_server, out_client, name); - R_THROW(ResultNotImplemented); + R_RETURN(CreateSession<KLightSession>(system, out_server, out_client, name)); } else { R_RETURN(CreateSession<KSession>(system, out_server, out_client, name)); } } -Result AcceptSession(Core::System& system, Handle* out_handle, Handle port_handle) { - UNIMPLEMENTED(); - R_THROW(ResultNotImplemented); +Result AcceptSession(Core::System& system, Handle* out, Handle port_handle) { + // Get the current handle table. + auto& handle_table = GetCurrentProcess(system.Kernel()).GetHandleTable(); + + // Get the server port. + KScopedAutoObject port = handle_table.GetObject<KServerPort>(port_handle); + R_UNLESS(port.IsNotNull(), ResultInvalidHandle); + + // Reserve an entry for the new session. + R_TRY(handle_table.Reserve(out)); + ON_RESULT_FAILURE { + handle_table.Unreserve(*out); + }; + + // Accept the session. + KAutoObject* session; + if (port->IsLight()) { + session = port->AcceptLightSession(); + } else { + session = port->AcceptSession(); + } + + // Ensure we accepted successfully. + R_UNLESS(session != nullptr, ResultNotFound); + + // Register the session. + handle_table.Register(*out, session); + session->Close(); + + R_SUCCEED(); } Result CreateSession64(Core::System& system, Handle* out_server_session_handle, diff --git a/src/core/hle/service/hid/controllers/applet_resource.cpp b/src/core/hle/service/hid/controllers/applet_resource.cpp new file mode 100644 index 000000000..ee60d8b44 --- /dev/null +++ b/src/core/hle/service/hid/controllers/applet_resource.cpp @@ -0,0 +1,199 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "core/core.h" +#include "core/hle/kernel/k_shared_memory.h" +#include "core/hle/service/hid/controllers/applet_resource.h" +#include "core/hle/service/hid/errors.h" + +namespace Service::HID { + +AppletResource::AppletResource(Core::System& system_) : system{system_} {} + +AppletResource::~AppletResource() = default; + +Result AppletResource::CreateAppletResource(u64 aruid) { + const u64 index = GetIndexFromAruid(aruid); + + if (index >= AruidIndexMax) { + return ResultAruidNotRegistered; + } + + if (data[index].flag.is_assigned) { + return ResultAruidAlreadyRegistered; + } + + // TODO: Here shared memory is created for the process we don't quite emulate this part so + // obtain this pointer from system + auto& shared_memory = system.Kernel().GetHidSharedMem(); + + data[index].shared_memory_handle = &shared_memory; + data[index].flag.is_assigned.Assign(true); + // TODO: InitializeSixAxisControllerConfig(false); + active_aruid = aruid; + return ResultSuccess; +} + +Result AppletResource::RegisterAppletResourceUserId(u64 aruid, bool enable_input) { + const u64 index = GetIndexFromAruid(aruid); + + if (index < AruidIndexMax) { + return ResultAruidAlreadyRegistered; + } + + std::size_t data_index = AruidIndexMax; + for (std::size_t i = 0; i < AruidIndexMax; i++) { + if (!data[i].flag.is_initialized) { + data_index = i; + break; + } + } + + if (data_index == AruidIndexMax) { + return ResultAruidNoAvailableEntries; + } + + AruidData& aruid_data = data[data_index]; + + aruid_data.aruid = aruid; + aruid_data.flag.is_initialized.Assign(true); + if (enable_input) { + aruid_data.flag.enable_pad_input.Assign(true); + aruid_data.flag.enable_six_axis_sensor.Assign(true); + aruid_data.flag.bit_18.Assign(true); + aruid_data.flag.enable_touchscreen.Assign(true); + } + + data_index = AruidIndexMax; + for (std::size_t i = 0; i < AruidIndexMax; i++) { + if (registration_list.flag[i] == RegistrationStatus::Initialized) { + if (registration_list.aruid[i] != aruid) { + continue; + } + data_index = i; + break; + } + if (registration_list.flag[i] == RegistrationStatus::None) { + data_index = i; + break; + } + } + + if (data_index == AruidIndexMax) { + return ResultSuccess; + } + + registration_list.flag[data_index] = RegistrationStatus::Initialized; + registration_list.aruid[data_index] = aruid; + + return ResultSuccess; +} + +void AppletResource::UnregisterAppletResourceUserId(u64 aruid) { + u64 index = GetIndexFromAruid(aruid); + + if (index < AruidIndexMax) { + if (data[index].flag.is_assigned) { + data[index].shared_memory_handle = nullptr; + data[index].flag.is_assigned.Assign(false); + } + } + + index = GetIndexFromAruid(aruid); + if (index < AruidIndexMax) { + DestroySevenSixAxisTransferMemory(); + data[index].flag.raw = 0; + data[index].aruid = 0; + + index = GetIndexFromAruid(aruid); + if (index < AruidIndexMax) { + registration_list.flag[index] = RegistrationStatus::PendingDelete; + } + } +} + +u64 AppletResource::GetActiveAruid() { + return active_aruid; +} + +Result AppletResource::GetSharedMemoryHandle(Kernel::KSharedMemory** out_handle, u64 aruid) { + u64 index = GetIndexFromAruid(aruid); + if (index >= AruidIndexMax) { + return ResultAruidNotRegistered; + } + + *out_handle = data[index].shared_memory_handle; + return ResultSuccess; +} + +u64 AppletResource::GetIndexFromAruid(u64 aruid) { + for (std::size_t i = 0; i < AruidIndexMax; i++) { + if (registration_list.flag[i] == RegistrationStatus::Initialized && + registration_list.aruid[i] == aruid) { + return i; + } + } + return AruidIndexMax; +} + +Result AppletResource::DestroySevenSixAxisTransferMemory() { + // TODO + return ResultSuccess; +} + +void AppletResource::EnableInput(u64 aruid, bool is_enabled) { + const u64 index = GetIndexFromAruid(aruid); + if (index >= AruidIndexMax) { + return; + } + + data[index].flag.enable_pad_input.Assign(is_enabled); + data[index].flag.enable_touchscreen.Assign(is_enabled); +} + +void AppletResource::EnableSixAxisSensor(u64 aruid, bool is_enabled) { + const u64 index = GetIndexFromAruid(aruid); + if (index >= AruidIndexMax) { + return; + } + + data[index].flag.enable_six_axis_sensor.Assign(is_enabled); +} + +void AppletResource::EnablePadInput(u64 aruid, bool is_enabled) { + const u64 index = GetIndexFromAruid(aruid); + if (index >= AruidIndexMax) { + return; + } + + data[index].flag.enable_pad_input.Assign(is_enabled); +} + +void AppletResource::EnableTouchScreen(u64 aruid, bool is_enabled) { + const u64 index = GetIndexFromAruid(aruid); + if (index >= AruidIndexMax) { + return; + } + + data[index].flag.enable_touchscreen.Assign(is_enabled); +} + +void AppletResource::SetIsPalmaConnectable(u64 aruid, bool is_connectable) { + const u64 index = GetIndexFromAruid(aruid); + if (index >= AruidIndexMax) { + return; + } + + data[index].flag.is_palma_connectable.Assign(is_connectable); +} + +void AppletResource::EnablePalmaBoostMode(u64 aruid, bool is_enabled) { + const u64 index = GetIndexFromAruid(aruid); + if (index >= AruidIndexMax) { + return; + } + + data[index].flag.enable_palma_boost_mode.Assign(is_enabled); +} + +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/applet_resource.h b/src/core/hle/service/hid/controllers/applet_resource.h new file mode 100644 index 000000000..3dcec2898 --- /dev/null +++ b/src/core/hle/service/hid/controllers/applet_resource.h @@ -0,0 +1,87 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-3.0-or-later + +#pragma once + +#include <array> + +#include "common/bit_field.h" +#include "common/common_types.h" +#include "core/hle/result.h" + +namespace Core { +class System; +} + +namespace Kernel { +class KSharedMemory; +} + +namespace Service::HID { +class AppletResource { +public: + explicit AppletResource(Core::System& system_); + ~AppletResource(); + + Result CreateAppletResource(u64 aruid); + + Result RegisterAppletResourceUserId(u64 aruid, bool enable_input); + void UnregisterAppletResourceUserId(u64 aruid); + + u64 GetActiveAruid(); + Result GetSharedMemoryHandle(Kernel::KSharedMemory** out_handle, u64 aruid); + + u64 GetIndexFromAruid(u64 aruid); + + Result DestroySevenSixAxisTransferMemory(); + + void EnableInput(u64 aruid, bool is_enabled); + void EnableSixAxisSensor(u64 aruid, bool is_enabled); + void EnablePadInput(u64 aruid, bool is_enabled); + void EnableTouchScreen(u64 aruid, bool is_enabled); + void SetIsPalmaConnectable(u64 aruid, bool is_connectable); + void EnablePalmaBoostMode(u64 aruid, bool is_enabled); + +private: + static constexpr std::size_t AruidIndexMax = 0x20; + + enum RegistrationStatus : u32 { + None, + Initialized, + PendingDelete, + }; + + struct DataStatusFlag { + union { + u32 raw{}; + + BitField<0, 1, u32> is_initialized; + BitField<1, 1, u32> is_assigned; + BitField<16, 1, u32> enable_pad_input; + BitField<17, 1, u32> enable_six_axis_sensor; + BitField<18, 1, u32> bit_18; + BitField<19, 1, u32> is_palma_connectable; + BitField<20, 1, u32> enable_palma_boost_mode; + BitField<21, 1, u32> enable_touchscreen; + }; + }; + + struct AruidRegisterList { + std::array<RegistrationStatus, AruidIndexMax> flag{}; + std::array<u64, AruidIndexMax> aruid{}; + }; + static_assert(sizeof(AruidRegisterList) == 0x180, "AruidRegisterList is an invalid size"); + + struct AruidData { + DataStatusFlag flag{}; + u64 aruid{}; + Kernel::KSharedMemory* shared_memory_handle{nullptr}; + }; + + u64 active_aruid{}; + AruidRegisterList registration_list{}; + std::array<AruidData, AruidIndexMax> data{}; + + Core::System& system; +}; +} // namespace Service::HID diff --git a/src/core/hle/service/hid/errors.h b/src/core/hle/service/hid/errors.h index 9585bdaf0..f00cb831f 100644 --- a/src/core/hle/service/hid/errors.h +++ b/src/core/hle/service/hid/errors.h @@ -19,6 +19,11 @@ constexpr Result NpadIsSameType{ErrorModule::HID, 602}; constexpr Result InvalidNpadId{ErrorModule::HID, 709}; constexpr Result NpadNotConnected{ErrorModule::HID, 710}; constexpr Result InvalidArraySize{ErrorModule::HID, 715}; + +constexpr Result ResultAruidNoAvailableEntries{ErrorModule::HID, 1044}; +constexpr Result ResultAruidAlreadyRegistered{ErrorModule::HID, 1046}; +constexpr Result ResultAruidNotRegistered{ErrorModule::HID, 1047}; + constexpr Result InvalidPalmaHandle{ErrorModule::HID, 3302}; } // namespace Service::HID diff --git a/src/core/hle/service/hid/hid.cpp b/src/core/hle/service/hid/hid.cpp index 1b7381d8d..afbcb019f 100644 --- a/src/core/hle/service/hid/hid.cpp +++ b/src/core/hle/service/hid/hid.cpp @@ -1,6 +1,8 @@ // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later +#include "core/hle/kernel/k_process.h" +#include "core/hle/kernel/kernel.h" #include "core/hle/service/hid/hid.h" #include "core/hle/service/hid/hid_debug_server.h" #include "core/hle/service/hid/hid_firmware_settings.h" @@ -20,6 +22,12 @@ void LoopProcess(Core::System& system) { std::shared_ptr<HidFirmwareSettings> firmware_settings = std::make_shared<HidFirmwareSettings>(); + // TODO: Remove this hack until this service is emulated properly. + const auto process_list = system.Kernel().GetProcessList(); + if (!process_list.empty()) { + resouce_manager->RegisterAppletResourceUserId(process_list[0]->GetId(), true); + } + server_manager->RegisterNamedService( "hid", std::make_shared<IHidServer>(system, resouce_manager, firmware_settings)); server_manager->RegisterNamedService( diff --git a/src/core/hle/service/hid/hid_server.cpp b/src/core/hle/service/hid/hid_server.cpp index a7d1578d9..e0f4051aa 100644 --- a/src/core/hle/service/hid/hid_server.cpp +++ b/src/core/hle/service/hid/hid_server.cpp @@ -224,8 +224,13 @@ void IHidServer::CreateAppletResource(HLERequestContext& ctx) { LOG_DEBUG(Service_HID, "called, applet_resource_user_id={}", applet_resource_user_id); + Result result = GetResourceManager()->CreateAppletResource(applet_resource_user_id); + if (result.IsSuccess()) { + result = GetResourceManager()->GetNpad()->Activate(applet_resource_user_id); + } + IPC::ResponseBuilder rb{ctx, 2, 0, 1}; - rb.Push(ResultSuccess); + rb.Push(result); rb.PushIpcInterface<IAppletResource>(system, resource_manager); } diff --git a/src/core/hle/service/hid/hid_system_server.cpp b/src/core/hle/service/hid/hid_system_server.cpp index b56d0347a..4d33456a3 100644 --- a/src/core/hle/service/hid/hid_system_server.cpp +++ b/src/core/hle/service/hid/hid_system_server.cpp @@ -3,6 +3,7 @@ #include "core/hid/hid_core.h" #include "core/hle/service/hid/controllers/npad.h" +#include "core/hle/service/hid/controllers/palma.h" #include "core/hle/service/hid/controllers/touchscreen.h" #include "core/hle/service/hid/errors.h" #include "core/hle/service/hid/hid_system_server.h" @@ -63,13 +64,13 @@ IHidSystemServer::IHidSystemServer(Core::System& system_, std::shared_ptr<Resour {329, nullptr, "DetachAbstractedPadAll"}, {330, nullptr, "CheckAbstractedPadConnection"}, {500, nullptr, "SetAppletResourceUserId"}, - {501, nullptr, "RegisterAppletResourceUserId"}, - {502, nullptr, "UnregisterAppletResourceUserId"}, - {503, nullptr, "EnableAppletToGetInput"}, + {501, &IHidSystemServer::RegisterAppletResourceUserId, "RegisterAppletResourceUserId"}, + {502, &IHidSystemServer::UnregisterAppletResourceUserId, "UnregisterAppletResourceUserId"}, + {503, &IHidSystemServer::EnableAppletToGetInput, "EnableAppletToGetInput"}, {504, nullptr, "SetAruidValidForVibration"}, - {505, nullptr, "EnableAppletToGetSixAxisSensor"}, - {506, nullptr, "EnableAppletToGetPadInput"}, - {507, nullptr, "EnableAppletToGetTouchScreen"}, + {505, &IHidSystemServer::EnableAppletToGetSixAxisSensor, "EnableAppletToGetSixAxisSensor"}, + {506, &IHidSystemServer::EnableAppletToGetPadInput, "EnableAppletToGetPadInput"}, + {507, &IHidSystemServer::EnableAppletToGetTouchScreen, "EnableAppletToGetTouchScreen"}, {510, nullptr, "SetVibrationMasterVolume"}, {511, nullptr, "GetVibrationMasterVolume"}, {512, nullptr, "BeginPermitVibrationSession"}, @@ -420,6 +421,129 @@ void IHidSystemServer::GetIrSensorState(HLERequestContext& ctx) { IPC::ResponseBuilder rb{ctx, 2}; rb.Push(ResultSuccess); } +void IHidSystemServer::RegisterAppletResourceUserId(HLERequestContext& ctx) { + IPC::RequestParser rp{ctx}; + struct Parameters { + bool enable_input; + INSERT_PADDING_WORDS_NOINIT(1); + u64 applet_resource_user_id; + }; + static_assert(sizeof(Parameters) == 0x10, "Parameters has incorrect size."); + + const auto parameters{rp.PopRaw<Parameters>()}; + + LOG_INFO(Service_HID, "called, enable_input={}, applet_resource_user_id={}", + parameters.enable_input, parameters.applet_resource_user_id); + + Result result = GetResourceManager()->RegisterAppletResourceUserId( + parameters.applet_resource_user_id, parameters.enable_input); + + if (result.IsSuccess()) { + // result = GetResourceManager()->GetNpad()->RegisterAppletResourceUserId( + // parameters.applet_resource_user_id); + } + + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ResultSuccess); +} + +void IHidSystemServer::UnregisterAppletResourceUserId(HLERequestContext& ctx) { + IPC::RequestParser rp{ctx}; + u64 applet_resource_user_id{rp.Pop<u64>()}; + + LOG_INFO(Service_HID, "called, applet_resource_user_id={}", applet_resource_user_id); + + GetResourceManager()->UnregisterAppletResourceUserId(applet_resource_user_id); + // GetResourceManager()->GetNpad()->UnregisterAppletResourceUserId(applet_resource_user_id); + // GetResourceManager()->GetPalma()->UnregisterAppletResourceUserId(applet_resource_user_id); + + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ResultSuccess); +} + +void IHidSystemServer::EnableAppletToGetInput(HLERequestContext& ctx) { + IPC::RequestParser rp{ctx}; + struct Parameters { + bool is_enabled; + INSERT_PADDING_WORDS_NOINIT(1); + u64 applet_resource_user_id; + }; + static_assert(sizeof(Parameters) == 0x10, "Parameters has incorrect size."); + + const auto parameters{rp.PopRaw<Parameters>()}; + + LOG_INFO(Service_HID, "called, is_enabled={}, applet_resource_user_id={}", + parameters.is_enabled, parameters.applet_resource_user_id); + + GetResourceManager()->EnableInput(parameters.applet_resource_user_id, parameters.is_enabled); + // GetResourceManager()->GetNpad()->EnableInput(parameters.applet_resource_user_id); + + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ResultSuccess); +} + +void IHidSystemServer::EnableAppletToGetSixAxisSensor(HLERequestContext& ctx) { + IPC::RequestParser rp{ctx}; + struct Parameters { + bool is_enabled; + INSERT_PADDING_WORDS_NOINIT(1); + u64 applet_resource_user_id; + }; + static_assert(sizeof(Parameters) == 0x10, "Parameters has incorrect size."); + + const auto parameters{rp.PopRaw<Parameters>()}; + + LOG_INFO(Service_HID, "called, is_enabled={}, applet_resource_user_id={}", + parameters.is_enabled, parameters.applet_resource_user_id); + + GetResourceManager()->EnableTouchScreen(parameters.applet_resource_user_id, + parameters.is_enabled); + + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ResultSuccess); +} + +void IHidSystemServer::EnableAppletToGetPadInput(HLERequestContext& ctx) { + IPC::RequestParser rp{ctx}; + struct Parameters { + bool is_enabled; + INSERT_PADDING_WORDS_NOINIT(1); + u64 applet_resource_user_id; + }; + static_assert(sizeof(Parameters) == 0x10, "Parameters has incorrect size."); + + const auto parameters{rp.PopRaw<Parameters>()}; + + LOG_INFO(Service_HID, "called, is_enabled={}, applet_resource_user_id={}", + parameters.is_enabled, parameters.applet_resource_user_id); + + GetResourceManager()->EnablePadInput(parameters.applet_resource_user_id, parameters.is_enabled); + // GetResourceManager()->GetNpad()->EnableInput(parameters.applet_resource_user_id); + + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ResultSuccess); +} + +void IHidSystemServer::EnableAppletToGetTouchScreen(HLERequestContext& ctx) { + IPC::RequestParser rp{ctx}; + struct Parameters { + bool is_enabled; + INSERT_PADDING_WORDS_NOINIT(1); + u64 applet_resource_user_id; + }; + static_assert(sizeof(Parameters) == 0x10, "Parameters has incorrect size."); + + const auto parameters{rp.PopRaw<Parameters>()}; + + LOG_INFO(Service_HID, "called, is_enabled={}, applet_resource_user_id={}", + parameters.is_enabled, parameters.applet_resource_user_id); + + GetResourceManager()->EnableTouchScreen(parameters.applet_resource_user_id, + parameters.is_enabled); + + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ResultSuccess); +} void IHidSystemServer::AcquireConnectionTriggerTimeoutEvent(HLERequestContext& ctx) { LOG_INFO(Service_AM, "(STUBBED) called"); diff --git a/src/core/hle/service/hid/hid_system_server.h b/src/core/hle/service/hid/hid_system_server.h index 822d5e5b9..1e623dfc2 100644 --- a/src/core/hle/service/hid/hid_system_server.h +++ b/src/core/hle/service/hid/hid_system_server.h @@ -38,6 +38,12 @@ private: void HasLeftRightBattery(HLERequestContext& ctx); void GetUniquePadsFromNpad(HLERequestContext& ctx); void GetIrSensorState(HLERequestContext& ctx); + void RegisterAppletResourceUserId(HLERequestContext& ctx); + void UnregisterAppletResourceUserId(HLERequestContext& ctx); + void EnableAppletToGetInput(HLERequestContext& ctx); + void EnableAppletToGetSixAxisSensor(HLERequestContext& ctx); + void EnableAppletToGetPadInput(HLERequestContext& ctx); + void EnableAppletToGetTouchScreen(HLERequestContext& ctx); void AcquireConnectionTriggerTimeoutEvent(HLERequestContext& ctx); void AcquireDeviceRegisteredEventForControllerSupport(HLERequestContext& ctx); void GetRegisteredDevices(HLERequestContext& ctx); diff --git a/src/core/hle/service/hid/resource_manager.cpp b/src/core/hle/service/hid/resource_manager.cpp index e76d4eea9..60d4ef71f 100644 --- a/src/core/hle/service/hid/resource_manager.cpp +++ b/src/core/hle/service/hid/resource_manager.cpp @@ -9,6 +9,7 @@ #include "core/hle/service/hid/resource_manager.h" #include "core/hle/service/ipc_helpers.h" +#include "core/hle/service/hid/controllers/applet_resource.h" #include "core/hle/service/hid/controllers/console_six_axis.h" #include "core/hle/service/hid/controllers/debug_pad.h" #include "core/hle/service/hid/controllers/gesture.h" @@ -33,7 +34,9 @@ constexpr auto mouse_keyboard_update_ns = std::chrono::nanoseconds{8 * 1000 * 10 constexpr auto motion_update_ns = std::chrono::nanoseconds{5 * 1000 * 1000}; // (5ms, 200Hz) ResourceManager::ResourceManager(Core::System& system_) - : system{system_}, service_context{system_, "hid"} {} + : system{system_}, service_context{system_, "hid"} { + applet_resource = std::make_shared<AppletResource>(system); +} ResourceManager::~ResourceManager() = default; @@ -77,6 +80,11 @@ void ResourceManager::Initialize() { system.HIDCore().ReloadInputDevices(); is_initialized = true; } + +std::shared_ptr<AppletResource> ResourceManager::GetAppletResource() const { + return applet_resource; +} + std::shared_ptr<CaptureButton> ResourceManager::GetCaptureButton() const { return capture_button; } @@ -137,6 +145,46 @@ std::shared_ptr<UniquePad> ResourceManager::GetUniquePad() const { return unique_pad; } +Result ResourceManager::CreateAppletResource(u64 aruid) { + std::scoped_lock lock{shared_mutex}; + return applet_resource->CreateAppletResource(aruid); +} + +Result ResourceManager::RegisterAppletResourceUserId(u64 aruid, bool bool_value) { + std::scoped_lock lock{shared_mutex}; + return applet_resource->RegisterAppletResourceUserId(aruid, bool_value); +} + +void ResourceManager::UnregisterAppletResourceUserId(u64 aruid) { + std::scoped_lock lock{shared_mutex}; + applet_resource->UnregisterAppletResourceUserId(aruid); +} + +Result ResourceManager::GetSharedMemoryHandle(Kernel::KSharedMemory** out_handle, u64 aruid) { + std::scoped_lock lock{shared_mutex}; + return applet_resource->GetSharedMemoryHandle(out_handle, aruid); +} + +void ResourceManager::EnableInput(u64 aruid, bool is_enabled) { + std::scoped_lock lock{shared_mutex}; + applet_resource->EnableInput(aruid, is_enabled); +} + +void ResourceManager::EnableSixAxisSensor(u64 aruid, bool is_enabled) { + std::scoped_lock lock{shared_mutex}; + applet_resource->EnableSixAxisSensor(aruid, is_enabled); +} + +void ResourceManager::EnablePadInput(u64 aruid, bool is_enabled) { + std::scoped_lock lock{shared_mutex}; + applet_resource->EnablePadInput(aruid, is_enabled); +} + +void ResourceManager::EnableTouchScreen(u64 aruid, bool is_enabled) { + std::scoped_lock lock{shared_mutex}; + applet_resource->EnableTouchScreen(aruid, is_enabled); +} + void ResourceManager::UpdateControllers(std::uintptr_t user_data, std::chrono::nanoseconds ns_late) { auto& core_timing = system.CoreTiming(); @@ -172,14 +220,12 @@ void ResourceManager::UpdateMotion(std::uintptr_t user_data, std::chrono::nanose } IAppletResource::IAppletResource(Core::System& system_, std::shared_ptr<ResourceManager> resource) - : ServiceFramework{system_, "IAppletResource"} { + : ServiceFramework{system_, "IAppletResource"}, resource_manager{resource} { static const FunctionInfo functions[] = { {0, &IAppletResource::GetSharedMemoryHandle, "GetSharedMemoryHandle"}, }; RegisterHandlers(functions); - resource->Initialize(); - // Register update callbacks npad_update_event = Core::Timing::CreateEvent( "HID::UpdatePadCallback", @@ -233,9 +279,13 @@ IAppletResource::~IAppletResource() { void IAppletResource::GetSharedMemoryHandle(HLERequestContext& ctx) { LOG_DEBUG(Service_HID, "called"); + Kernel::KSharedMemory* handle; + const u64 applet_resource_user_id = resource_manager->GetAppletResource()->GetActiveAruid(); + const auto result = resource_manager->GetSharedMemoryHandle(&handle, applet_resource_user_id); + IPC::ResponseBuilder rb{ctx, 2, 1}; - rb.Push(ResultSuccess); - rb.PushCopyObjects(&system.Kernel().GetHidSharedMem()); + rb.Push(result); + rb.PushCopyObjects(handle); } } // namespace Service::HID diff --git a/src/core/hle/service/hid/resource_manager.h b/src/core/hle/service/hid/resource_manager.h index 2b6a9b5e6..a78e2b729 100644 --- a/src/core/hle/service/hid/resource_manager.h +++ b/src/core/hle/service/hid/resource_manager.h @@ -6,11 +6,20 @@ #include "core/hle/service/kernel_helpers.h" #include "core/hle/service/service.h" +namespace Core { +class System; +} + namespace Core::Timing { struct EventType; } +namespace Kernel { +class KSharedMemory; +} + namespace Service::HID { +class AppletResource; class Controller_Stubbed; class ConsoleSixAxis; class DebugPad; @@ -38,6 +47,7 @@ public: void Initialize(); + std::shared_ptr<AppletResource> GetAppletResource() const; std::shared_ptr<CaptureButton> GetCaptureButton() const; std::shared_ptr<ConsoleSixAxis> GetConsoleSixAxis() const; std::shared_ptr<DebugMouse> GetDebugMouse() const; @@ -54,6 +64,18 @@ public: std::shared_ptr<TouchScreen> GetTouchScreen() const; std::shared_ptr<UniquePad> GetUniquePad() const; + Result CreateAppletResource(u64 aruid); + + Result RegisterAppletResourceUserId(u64 aruid, bool bool_value); + void UnregisterAppletResourceUserId(u64 aruid); + + Result GetSharedMemoryHandle(Kernel::KSharedMemory** out_handle, u64 aruid); + + void EnableInput(u64 aruid, bool is_enabled); + void EnableSixAxisSensor(u64 aruid, bool is_enabled); + void EnablePadInput(u64 aruid, bool is_enabled); + void EnableTouchScreen(u64 aruid, bool is_enabled); + void UpdateControllers(std::uintptr_t user_data, std::chrono::nanoseconds ns_late); void UpdateNpad(std::uintptr_t user_data, std::chrono::nanoseconds ns_late); void UpdateMouseKeyboard(std::uintptr_t user_data, std::chrono::nanoseconds ns_late); @@ -62,6 +84,9 @@ public: private: bool is_initialized{false}; + mutable std::mutex shared_mutex; + std::shared_ptr<AppletResource> applet_resource = nullptr; + std::shared_ptr<CaptureButton> capture_button = nullptr; std::shared_ptr<ConsoleSixAxis> console_six_axis = nullptr; std::shared_ptr<DebugMouse> debug_mouse = nullptr; @@ -106,6 +131,8 @@ private: std::shared_ptr<Core::Timing::EventType> default_update_event; std::shared_ptr<Core::Timing::EventType> mouse_keyboard_update_event; std::shared_ptr<Core::Timing::EventType> motion_update_event; + + std::shared_ptr<ResourceManager> resource_manager; }; } // namespace Service::HID diff --git a/src/core/hle/service/nvnflinger/fb_share_buffer_manager.cpp b/src/core/hle/service/nvnflinger/fb_share_buffer_manager.cpp index d7db24f42..75bf31e32 100644 --- a/src/core/hle/service/nvnflinger/fb_share_buffer_manager.cpp +++ b/src/core/hle/service/nvnflinger/fb_share_buffer_manager.cpp @@ -171,6 +171,7 @@ void MakeGraphicBuffer(android::BufferQueueProducer& producer, u32 slot, u32 han buffer->height = SharedBufferHeight; buffer->stride = SharedBufferBlockLinearStride; buffer->format = SharedBufferBlockLinearFormat; + buffer->external_format = SharedBufferBlockLinearFormat; buffer->buffer_id = handle; buffer->offset = slot * SharedBufferSlotSize; ASSERT(producer.SetPreallocatedBuffer(slot, buffer) == android::Status::NoError); |