diff --git a/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_client.cpp b/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_client.cpp index 5b9f4beae..6888c5539 100644 --- a/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_client.cpp +++ b/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_client.cpp @@ -27,6 +27,9 @@ namespace ams::htc::server::rpc { constinit os::SdkMutex g_rpc_mutex; + constinit RpcTaskIdFreeList g_task_id_free_list; + constinit RpcTaskTable g_task_table; + } RpcClient::RpcClient(driver::IDriver *driver, htclow::ChannelId channel) @@ -36,17 +39,18 @@ namespace ams::htc::server::rpc { m_receive_thread_stack(g_receive_thread_stack), m_send_thread_stack(g_send_thread_stack), m_mutex(g_rpc_mutex), - m_task_table(), + m_task_id_free_list(g_task_id_free_list), + m_task_table(g_task_table), + m_task_active(), + m_task_queue(), m_cancelled(false), m_thread_running(false) { /* Initialize all events. */ - for (size_t i = 0; i < MaxTaskCount; ++i) { - os::InitializeEvent(std::addressof(m_5F8_events[i]), false, os::EventClearMode_AutoClear); - os::InitializeEvent(std::addressof(m_1138_events[i]), false, os::EventClearMode_AutoClear); + for (size_t i = 0; i < MaxRpcCount; ++i) { + os::InitializeEvent(std::addressof(m_receive_buffer_available_events[i]), false, os::EventClearMode_AutoClear); + os::InitializeEvent(std::addressof(m_send_buffer_available_events[i]), false, os::EventClearMode_AutoClear); } - - /* TODO: Clear all of m_3C0 array to zero. */ } } diff --git a/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_client.hpp b/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_client.hpp index d626b861f..1a20439dc 100644 --- a/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_client.hpp +++ b/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_client.hpp @@ -17,10 +17,15 @@ #include #include "../driver/htc_i_driver.hpp" #include "htc_rpc_task_table.hpp" +#include "htc_rpc_task_queue.hpp" +#include "htc_rpc_task_id_free_list.hpp" namespace ams::htc::server::rpc { class RpcClient { + private: + /* TODO: where is this value coming from, again? */ + static constexpr size_t BufferSize = 0xE400; private: u64 m_00; driver::IDriver *m_driver; @@ -30,14 +35,16 @@ namespace ams::htc::server::rpc { os::ThreadType m_receive_thread; os::ThreadType m_send_thread; os::SdkMutex &m_mutex; - /* TODO: m_task_id_free_list */ - RpcTaskTable m_task_table; - /* TODO: m_3C0[MaxTaskCount] */ - /* TODO: m_rpc_task_queue */ + RpcTaskIdFreeList &m_task_id_free_list; + RpcTaskTable &m_task_table; + bool m_task_active[MaxRpcCount]; + RpcTaskQueue m_task_queue; bool m_cancelled; bool m_thread_running; - os::EventType m_5F8_events[MaxTaskCount]; - os::EventType m_1138_events[MaxTaskCount]; + os::EventType m_receive_buffer_available_events[MaxRpcCount]; + os::EventType m_send_buffer_available_events[MaxRpcCount]; + u8 m_receive_buffer[BufferSize]; + u8 m_send_buffer[BufferSize]; public: RpcClient(driver::IDriver *driver, htclow::ChannelId channel); }; diff --git a/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_task_id_free_list.hpp b/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_task_id_free_list.hpp new file mode 100644 index 000000000..ee75670c2 --- /dev/null +++ b/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_task_id_free_list.hpp @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once +#include +#include "htc_rpc_tasks.hpp" +#include "htc_htcmisc_rpc_tasks.hpp" + +namespace ams::htc::server::rpc { + + class RpcTaskIdFreeList { + private: + u32 m_task_ids[MaxRpcCount]; + u32 m_offset; + u32 m_free_count; + public: + constexpr RpcTaskIdFreeList() : m_task_ids(), m_offset(0), m_free_count(MaxRpcCount) { + for (auto i = 0; i < static_cast(MaxRpcCount); ++i) { + m_task_ids[i] = i; + } + } + + Result Allocate(u32 *out) { + /* Check that we have free tasks. */ + R_UNLESS(m_free_count > 0, htc::ResultOutOfRpcTask()); + + /* Get index. */ + const auto index = m_offset; + m_free_count = (m_free_count + 1) % MaxRpcCount; + --m_free_count; + + /* Get the task id. */ + *out = m_task_ids[index]; + return ResultSuccess(); + } + + void Free(u32 task_id) { + /* Check pre-conditions. */ + AMS_ASSERT(m_free_count < static_cast(MaxRpcCount)); + + /* Determine index. */ + const auto index = ((m_free_count++) + m_offset) % MaxRpcCount; + + /* Set the task id. */ + m_task_ids[index] = task_id; + } + }; + +} diff --git a/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_task_queue.hpp b/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_task_queue.hpp new file mode 100644 index 000000000..c7c9175b4 --- /dev/null +++ b/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_task_queue.hpp @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once +#include +#include "htc_rpc_tasks.hpp" +#include "htc_htcmisc_rpc_tasks.hpp" + +namespace ams::htc::server::rpc { + + class RpcTaskQueue { + private: + u32 m_task_ids[MaxRpcCount]; + PacketCategory m_task_categories[MaxRpcCount]; + int m_offset; + int m_count; + os::SdkConditionVariable m_cv; + os::SdkMutex m_mutex; + bool m_cancelled; + public: + constexpr RpcTaskQueue() = default; + + void Initialize() { + m_offset = 0; + m_count = 0; + m_cancelled = false; + } + + void Finalize() { + m_offset = 0; + m_count = 0; + } + + void Cancel() { + /* Lock ourselves. */ + std::scoped_lock lk(m_mutex); + + /* Cancel ourselves. */ + m_cancelled = true; + + /* Signal to consumers/producers. */ + m_cv.Signal(); + } + + void Add(u32 task_id, PacketCategory category) { + /* Lock ourselves. */ + std::scoped_lock lk(m_mutex); + + /* Check pre-conditions. */ + AMS_ASSERT(m_count < static_cast(MaxRpcCount)); + + /* Determine index. */ + const auto index = ((m_count++) + m_offset) % MaxRpcCount; + + /* Set task. */ + m_task_ids[index] = task_id; + m_task_categories[index] = category; + + /* Signal. */ + if (m_count > 0) { + m_cv.Signal(); + } + } + + Result Take(u32 *out_id, PacketCategory *out_category) { + /* Lock ourselves. */ + std::scoped_lock lk(m_mutex); + + /* Wait until we can take. */ + while (m_count == 0 && !m_cancelled) { + m_cv.Wait(m_mutex); + } + + /* Check that we're not cancelled. */ + R_UNLESS(!m_cancelled, htc::ResultCancelled()); + + /* Determine index. */ + const auto index = m_offset; + + /* Advance the queue. */ + m_offset = (m_offset + 1) % MaxRpcCount; + --m_count; + + /* Return the task info. */ + *out_id = m_task_ids[index]; + *out_category = m_task_categories[index]; + return ResultSuccess(); + } + }; + +} diff --git a/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_task_table.hpp b/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_task_table.hpp index 5d5f54e09..ffb6a7c74 100644 --- a/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_task_table.hpp +++ b/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_task_table.hpp @@ -36,8 +36,8 @@ namespace ams::htc::server::rpc { static constexpr size_t MaxTaskSize = 0xA000; using TaskStorage = typename std::aligned_storage::type; private: - bool m_valid[MaxTaskCount]; - TaskStorage m_storages[MaxTaskCount]; + bool m_valid[MaxRpcCount]; + TaskStorage m_storages[MaxRpcCount]; private: template ALWAYS_INLINE T *GetPointer(u32 index) { @@ -47,7 +47,7 @@ namespace ams::htc::server::rpc { } ALWAYS_INLINE bool IsValid(u32 index) { - return index < MaxTaskCount && m_valid[index]; + return index < MaxRpcCount && m_valid[index]; } public: constexpr RpcTaskTable() = default; diff --git a/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_tasks.hpp b/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_tasks.hpp index fb9f423ba..cd1358449 100644 --- a/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_tasks.hpp +++ b/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_tasks.hpp @@ -18,7 +18,25 @@ namespace ams::htc::server::rpc { - constexpr inline size_t MaxTaskCount = 0x48; + constexpr inline size_t MaxRpcCount = 0x48; + + enum class PacketCategory : s16 { + Request = 0, + Response = 1, + Notification = 2, + }; + + struct RpcPacket { + s16 protocol; + s16 version; + PacketCategory category; + u16 type; + s64 body_size; + u32 task_id; + u64 params[5]; + u8 data[]; + }; + static_assert(sizeof(RpcPacket) == 0x40); enum class RpcTaskCancelReason { None = 0, diff --git a/libraries/libvapours/include/vapours/results/htc_results.hpp b/libraries/libvapours/include/vapours/results/htc_results.hpp index 152e6ed08..917ff4e8e 100644 --- a/libraries/libvapours/include/vapours/results/htc_results.hpp +++ b/libraries/libvapours/include/vapours/results/htc_results.hpp @@ -23,6 +23,11 @@ namespace ams::htc { R_DEFINE_ERROR_RESULT(ConnectionFailure, 1); R_DEFINE_ERROR_RESULT(NotFound, 2); R_DEFINE_ERROR_RESULT(NotEnoughBuffer, 3); + + R_DEFINE_ERROR_RESULT(Cancelled, 101); + R_DEFINE_ERROR_RESULT(Unknown, 1023); + R_DEFINE_ERROR_RESULT(OutOfRpcTask, 2102); + }