diff --git a/libstratosphere/include/stratosphere/cfg/cfg_api.hpp b/libstratosphere/include/stratosphere/cfg/cfg_api.hpp index daab6d04..c2e03a51 100644 --- a/libstratosphere/include/stratosphere/cfg/cfg_api.hpp +++ b/libstratosphere/include/stratosphere/cfg/cfg_api.hpp @@ -20,10 +20,6 @@ namespace ams::cfg { - /* Privileged Process configuration. */ - bool IsInitialProcess(); - void GetInitialProcessRange(os::ProcessId *out_min, os::ProcessId *out_max); - /* SD card configuration. */ bool IsSdCardRequiredServicesReady(); void WaitSdCardRequiredServicesReady(); diff --git a/libstratosphere/include/stratosphere/pgl/pgl_event_observer.hpp b/libstratosphere/include/stratosphere/pgl/pgl_event_observer.hpp index 5e6250ab..a0ad49e9 100644 --- a/libstratosphere/include/stratosphere/pgl/pgl_event_observer.hpp +++ b/libstratosphere/include/stratosphere/pgl/pgl_event_observer.hpp @@ -71,9 +71,9 @@ namespace ams::pgl { explicit EventObserverByTipc(Args &&... args) : m_tipc_interface(std::forward(args)...) { /* ... */ } public: virtual Result GetSystemEvent(os::SystemEventType *out) override { - ams::tipc::CopyHandle handle; + os::NativeHandle handle; R_TRY(m_tipc_interface.GetProcessEventHandle(std::addressof(handle))); - os::AttachReadableHandleToSystemEvent(out, handle.GetValue(), true, os::EventClearMode_AutoClear); + os::AttachReadableHandleToSystemEvent(out, handle, true, os::EventClearMode_AutoClear); return ResultSuccess(); } diff --git a/libstratosphere/include/stratosphere/tipc/impl/tipc_autogen_interface_macros.hpp b/libstratosphere/include/stratosphere/tipc/impl/tipc_autogen_interface_macros.hpp index 4323a8bd..845d6838 100644 --- a/libstratosphere/include/stratosphere/tipc/impl/tipc_autogen_interface_macros.hpp +++ b/libstratosphere/include/stratosphere/tipc/impl/tipc_autogen_interface_macros.hpp @@ -86,6 +86,16 @@ namespace ams::tipc::impl { } \ } + #define AMS_TIPC_IMPL_IS_FIRMWARE_VERSION_ALWAYS_VALID(CLASSNAME, CMD_ID, RETURN, NAME, ARGS, ARGNAMES, VERSION_MIN, VERSION_MAX) \ + { \ + constexpr bool MinValid = VERSION_MIN == hos::Version_Min; \ + constexpr bool MaxValid = VERSION_MAX == hos::Version_Max; \ + if (!MinValid || !MaxValid) { \ + return false; \ + } \ + } + + #define AMS_TIPC_DEFINE_INTERFACE_WITH_DEFAULT_BASE(NAMESPACE, INTERFACE, BASE, CMD_MACRO) \ namespace NAMESPACE { \ \ @@ -117,6 +127,11 @@ namespace ams::tipc::impl { CMD_MACRO(ImplType, AMS_TIPC_IMPL_PROCESS_METHOD_REQUEST_BY_ID) \ \ return this->ProcessDefaultMethod(impl, message_buffer); \ + } \ + \ + static consteval bool IsFirmwareVersionAlwaysValid() { \ + CMD_MACRO(ImplType, AMS_TIPC_IMPL_IS_FIRMWARE_VERSION_ALWAYS_VALID); \ + return true; \ } \ public: \ virtual Result ProcessRequest() override { \ @@ -132,7 +147,7 @@ namespace ams::tipc::impl { \ /* Get decision variables. */ \ const auto tag = svc::ipc::MessageBuffer::MessageHeader(message_buffer).GetTag(); \ - const auto fw_ver = hos::GetVersion(); \ + const auto fw_ver = IsFirmwareVersionAlwaysValid() ? hos::Version_Current : hos::GetVersion(); \ \ /* Process against the command ids. */ \ if (false) { } \ diff --git a/libstratosphere/include/stratosphere/tipc/impl/tipc_impl_command_serialization.hpp b/libstratosphere/include/stratosphere/tipc/impl/tipc_impl_command_serialization.hpp index a190cadd..6667dc39 100644 --- a/libstratosphere/include/stratosphere/tipc/impl/tipc_impl_command_serialization.hpp +++ b/libstratosphere/include/stratosphere/tipc/impl/tipc_impl_command_serialization.hpp @@ -76,9 +76,9 @@ namespace ams::tipc::impl { constexpr inline ArgumentType GetArgumentType = [] { if constexpr (tipc::IsBuffer) { return ArgumentType::Buffer; - } else if constexpr (std::is_base_of::value) { + } else if constexpr (std::same_as || std::same_as) { return ArgumentType::InHandle; - } else if constexpr (std::is_base_of::value) { + } else if constexpr (std::same_as || std::same_as) { return ArgumentType::OutHandle; } else if constexpr (std::is_base_of::value) { return ArgumentType::OutData; @@ -126,10 +126,10 @@ namespace ams::tipc::impl { using InCopyHandleFilter = TypeEqualityFilter; template - using OutMoveHandleFilter = TypeEqualityFilter>; + using OutMoveHandleFilter = TypeEqualityFilter; template - using OutCopyHandleFilter = TypeEqualityFilter>; + using OutCopyHandleFilter = TypeEqualityFilter; template struct BufferAttributeArrayGetter; @@ -267,6 +267,8 @@ namespace ams::tipc::impl { static_assert(NumInHandles <= 8, "Methods must take in <= 8 Handles"); static_assert(NumOutHandles <= 8, "Methods must output <= 8 Handles"); + static_assert(NumInHandles == 0, "In Handles not yet implemented!"); + /* Buffer marshalling. */ static constexpr std::array BufferAttributes = BufferAttributeArrayGetter::value; static constexpr size_t NumInBuffers = BufferAttributeCounter::GetCount(BufferAttributes); @@ -340,18 +342,18 @@ namespace ams::tipc::impl { current_info.out_raw_data_index++; } else if constexpr (arg_type == ArgumentType::InHandle) { /* New InHandle, increment the appropriate index. */ - if constexpr (std::is_same::value) { + if constexpr (std::same_as) { current_info.in_move_handle_index++; - } else if constexpr (std::is_same::value) { + } else if constexpr (std::same_as) { current_info.in_copy_handle_index++; } else { static_assert(!std::is_same::value, "Invalid InHandle kind"); } } else if constexpr (arg_type == ArgumentType::OutHandle) { /* New OutHandle, increment the appropriate index. */ - if constexpr (std::is_same>::value) { + if constexpr (std::same_as) { current_info.out_move_handle_index++; - } else if constexpr (std::is_same>::value) { + } else if constexpr (std::same_as) { current_info.out_copy_handle_index++; } else { static_assert(!std::is_same::value, "Invalid OutHandle kind"); @@ -418,25 +420,25 @@ namespace ams::tipc::impl { static constexpr size_t NumMove = _NumMove; static constexpr size_t NumCopy = _NumCopy; private: - MoveHandle move_handles[NumMove]; - CopyHandle copy_handles[NumCopy]; + os::NativeHandle move_handles[NumMove]; + os::NativeHandle copy_handles[NumCopy]; public: - constexpr ALWAYS_INLINE OutHandleHolder() : move_handles(), copy_handles() { /* ... */ } + ALWAYS_INLINE OutHandleHolder() { /* ... */ } template - constexpr ALWAYS_INLINE MoveHandle *GetMoveHandlePointer() { + constexpr ALWAYS_INLINE os::NativeHandle *GetMoveHandlePointer() { static_assert(Index < NumMove, "Index < NumMove"); return move_handles + Index; } template - constexpr ALWAYS_INLINE CopyHandle *GetCopyHandlePointer() { + constexpr ALWAYS_INLINE os::NativeHandle *GetCopyHandlePointer() { static_assert(Index < NumCopy, "Index < NumCopy"); return copy_handles + Index; } ALWAYS_INLINE void CopyTo(const svc::ipc::MessageBuffer &buffer) const { - #define _TIPC_OUT_HANDLE_HOLDER_WRITE_COPY_HANDLE(n) do { if constexpr (NumCopy > n) { buffer.SetHandle(OutIndex + n, copy_handles[n].GetValue()); } } while (0) + #define _TIPC_OUT_HANDLE_HOLDER_WRITE_COPY_HANDLE(n) do { if constexpr (NumCopy > n) { buffer.SetHandle(OutIndex + n, copy_handles[n]); } } while (0) _TIPC_OUT_HANDLE_HOLDER_WRITE_COPY_HANDLE(0); _TIPC_OUT_HANDLE_HOLDER_WRITE_COPY_HANDLE(1); _TIPC_OUT_HANDLE_HOLDER_WRITE_COPY_HANDLE(2); @@ -446,7 +448,7 @@ namespace ams::tipc::impl { _TIPC_OUT_HANDLE_HOLDER_WRITE_COPY_HANDLE(6); _TIPC_OUT_HANDLE_HOLDER_WRITE_COPY_HANDLE(7); #undef _TIPC_OUT_HANDLE_HOLDER_WRITE_COPY_HANDLE - #define _TIPC_OUT_HANDLE_HOLDER_WRITE_MOVE_HANDLE(n) do { if constexpr (NumMove > n) { buffer.SetHandle(OutIndex + NumCopy + n, move_handles[n].GetValue()); } } while (0) + #define _TIPC_OUT_HANDLE_HOLDER_WRITE_MOVE_HANDLE(n) do { if constexpr (NumMove > n) { buffer.SetHandle(OutIndex + NumCopy + n, move_handles[n]); } } while (0) _TIPC_OUT_HANDLE_HOLDER_WRITE_MOVE_HANDLE(0); _TIPC_OUT_HANDLE_HOLDER_WRITE_MOVE_HANDLE(1); _TIPC_OUT_HANDLE_HOLDER_WRITE_MOVE_HANDLE(2); @@ -507,10 +509,10 @@ namespace ams::tipc::impl { return T(out_raw_holder.template GetAddress()); } else if constexpr (Info.arg_type == ArgumentType::InHandle) { /* New InHandle. */ - if constexpr (std::is_same::value) { + if constexpr (std::same_as) { constexpr auto HandleIndex = CommandMeta::InMessageHandleIndex + CommandMeta::NumInCopyHandles + Info.in_move_handle_index; return T(message_buffer.GetHandle(HandleIndex)); - } else if constexpr (std::is_same::value) { + } else if constexpr (std::same_as) { constexpr auto HandleIndex = CommandMeta::InMessageHandleIndex + Info.in_copy_handle_index; return T(message_buffer.GetHandle(HandleIndex)); } else { @@ -518,10 +520,14 @@ namespace ams::tipc::impl { } } else if constexpr (Info.arg_type == ArgumentType::OutHandle) { /* New OutHandle. */ - if constexpr (std::is_same>::value) { - return T(out_handles_holder.template GetMoveHandlePointer()); - } else if constexpr (std::is_same>::value) { - return T(out_handles_holder.template GetCopyHandlePointer()); + if constexpr (std::same_as) { + os::NativeHandle * const ptr = out_handles_holder.template GetMoveHandlePointer(); + *ptr = os::InvalidNativeHandle; + return T(ptr); + } else if constexpr (std::same_as) { + os::NativeHandle * const ptr = out_handles_holder.template GetCopyHandlePointer(); + *ptr = os::InvalidNativeHandle; + return T(ptr); } else { static_assert(!std::is_same::value, "Invalid OutHandle kind"); } diff --git a/libstratosphere/include/stratosphere/tipc/tipc_handles.hpp b/libstratosphere/include/stratosphere/tipc/tipc_handles.hpp index 1c72ff40..bba680ec 100644 --- a/libstratosphere/include/stratosphere/tipc/tipc_handles.hpp +++ b/libstratosphere/include/stratosphere/tipc/tipc_handles.hpp @@ -20,147 +20,72 @@ namespace ams::tipc { - namespace impl { - - struct InHandleTag{}; - struct OutHandleTag{}; - - template - struct InHandle : public InHandleTag { - os::NativeHandle handle; - - constexpr InHandle() : handle(os::InvalidNativeHandle) { /* ... */ } - constexpr InHandle(os::NativeHandle h) : handle(h) { /* ... */ } - constexpr InHandle(const InHandle &o) : handle(o.handle) { /* ... */ } - - constexpr void operator=(const os::NativeHandle &h) { this->handle = h; } - constexpr void operator=(const InHandle &o) { this->handle = o.handle; } - - constexpr /* TODO: explicit? */ operator os::NativeHandle() const { return this->handle; } - constexpr os::NativeHandle GetValue() const { return this->handle; } - }; - - template - class OutHandleImpl : public OutHandleTag { - static_assert(std::is_base_of::value, "OutHandleImpl requires InHandle base"); - private: - T *m_ptr; - public: - constexpr OutHandleImpl(T *p) : m_ptr(p) { /* ... */ } - - constexpr void SetValue(const os::NativeHandle &value) { - *m_ptr = value; - } - - constexpr void SetValue(const T &value) { - *m_ptr = value; - } - - constexpr const T &GetValue() const { - return *m_ptr; - } - - constexpr T *GetPointer() const { - return m_ptr; - } - - constexpr os::NativeHandle *GetHandlePointer() const { - return &m_ptr->handle; - } - - constexpr T &operator *() const { - return *m_ptr; - } - - constexpr T *operator ->() const { - return m_ptr; - } - }; - - } - - using MoveHandle = typename impl::InHandle; - using CopyHandle = typename impl::InHandle; - - static_assert(sizeof(MoveHandle) == sizeof(os::NativeHandle), "sizeof(MoveHandle)"); - static_assert(sizeof(CopyHandle) == sizeof(os::NativeHandle), "sizeof(CopyHandle)"); - - template<> - class IsOutForceEnabled : public std::true_type{}; - template<> - class IsOutForceEnabled : public std::true_type{}; - - template<> - class Out : public impl::OutHandleImpl { + /* TODO: How do InHandles work in tipc? No examples to work off of. */ + class CopyHandle { private: - using T = MoveHandle; - using Base = impl::OutHandleImpl; + CopyHandle(); + }; + + class MoveHandle { + private: + MoveHandle(); + }; + + template<> + class Out { + private: + os::NativeHandle * const m_ptr; public: - constexpr Out(T *p) : Base(p) { /* ... */ } + ALWAYS_INLINE Out(os::NativeHandle *p) : m_ptr(p) { /* ... */ } - constexpr void SetValue(const os::NativeHandle &value) { - Base::SetValue(value); + ALWAYS_INLINE void SetValue(os::NativeHandle v) const { + *m_ptr = v; } - constexpr void SetValue(const T &value) { - Base::SetValue(value); + ALWAYS_INLINE const os::NativeHandle &GetValue() const { + return *m_ptr; } - constexpr const T &GetValue() const { - return Base::GetValue(); + ALWAYS_INLINE os::NativeHandle *GetPointer() const { + return m_ptr; } - constexpr T *GetPointer() const { - return Base::GetPointer(); + /* Convenience operators. */ + ALWAYS_INLINE os::NativeHandle &operator*() const { + return *m_ptr; } - constexpr os::NativeHandle *GetHandlePointer() const { - return Base::GetHandlePointer(); - } - - constexpr T &operator *() const { - return Base::operator*(); - } - - constexpr T *operator ->() const { - return Base::operator->(); + ALWAYS_INLINE os::NativeHandle *operator->() const { + return m_ptr; } }; template<> - class Out : public impl::OutHandleImpl { + class Out { private: - using T = CopyHandle; - using Base = impl::OutHandleImpl; + os::NativeHandle * const m_ptr; public: - constexpr Out(T *p) : Base(p) { /* ... */ } + ALWAYS_INLINE Out(os::NativeHandle *p) : m_ptr(p) { /* ... */ } - constexpr void SetValue(const os::NativeHandle &value) { - Base::SetValue(value); + ALWAYS_INLINE void SetValue(os::NativeHandle v) const { + *m_ptr = v; } - constexpr void SetValue(const T &value) { - Base::SetValue(value); + ALWAYS_INLINE const os::NativeHandle &GetValue() const { + return *m_ptr; } - constexpr const T &GetValue() const { - return Base::GetValue(); + ALWAYS_INLINE os::NativeHandle *GetPointer() const { + return m_ptr; } - constexpr T *GetPointer() const { - return Base::GetPointer(); + /* Convenience operators. */ + ALWAYS_INLINE os::NativeHandle &operator*() const { + return *m_ptr; } - constexpr os::NativeHandle *GetHandlePointer() const { - return Base::GetHandlePointer(); - } - - constexpr T &operator *() const { - return Base::operator*(); - } - - constexpr T *operator ->() const { - return Base::operator->(); + ALWAYS_INLINE os::NativeHandle *operator->() const { + return m_ptr; } }; diff --git a/libstratosphere/source/cfg/cfg_privileged_process.cpp b/libstratosphere/source/cfg/cfg_privileged_process.cpp deleted file mode 100644 index 46fbc517..00000000 --- a/libstratosphere/source/cfg/cfg_privileged_process.cpp +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (c) 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 . - */ -#include - -namespace ams::cfg { - - namespace { - - constinit os::SdkMutex g_lock; - constinit bool g_got_privileged_process_status = false; - constinit os::ProcessId g_min_initial_process_id = os::InvalidProcessId, g_max_initial_process_id = os::InvalidProcessId; - constinit os::ProcessId g_cur_process_id = os::InvalidProcessId; - - ALWAYS_INLINE void EnsurePrivilegedProcessStatusCached() { - if (AMS_LIKELY(g_got_privileged_process_status)) { - return; - } - - std::scoped_lock lk(g_lock); - - if (AMS_LIKELY(!g_got_privileged_process_status)) { - R_ABORT_UNLESS(svc::GetSystemInfo(std::addressof(g_min_initial_process_id.value), svc::SystemInfoType_InitialProcessIdRange, svc::InvalidHandle, svc::InitialProcessIdRangeInfo_Minimum)); - R_ABORT_UNLESS(svc::GetSystemInfo(std::addressof(g_max_initial_process_id.value), svc::SystemInfoType_InitialProcessIdRange, svc::InvalidHandle, svc::InitialProcessIdRangeInfo_Maximum)); - g_cur_process_id = os::GetCurrentProcessId(); - - g_got_privileged_process_status = true; - } - } - - } - - bool IsInitialProcess() { - /* Cache initial process range and extents. */ - EnsurePrivilegedProcessStatusCached(); - - /* Determine if we're Initial. */ - return g_min_initial_process_id <= g_cur_process_id && g_cur_process_id <= g_max_initial_process_id; - } - - void GetInitialProcessRange(os::ProcessId *out_min, os::ProcessId *out_max) { - /* Cache initial process range and extents. */ - EnsurePrivilegedProcessStatusCached(); - - /* Set output. */ - *out_min = g_min_initial_process_id; - *out_max = g_max_initial_process_id; - } - -} diff --git a/libstratosphere/source/pgl/srv/pgl_srv_shell_interface.cpp b/libstratosphere/source/pgl/srv/pgl_srv_shell_interface.cpp index 4aebf782..3d621512 100644 --- a/libstratosphere/source/pgl/srv/pgl_srv_shell_interface.cpp +++ b/libstratosphere/source/pgl/srv/pgl_srv_shell_interface.cpp @@ -171,7 +171,7 @@ namespace ams::pgl::srv { } Result ShellInterfaceTipc::GetShellEventObserver(ams::tipc::OutMoveHandle out) { - return pgl::srv::AllocateShellEventObserverForTipc(out.GetHandlePointer()); + return pgl::srv::AllocateShellEventObserverForTipc(out.GetPointer()); } }