From 431df5ae931b1b0229bdabd5855ee148a3baf001 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 25 Jan 2024 21:12:44 -0500 Subject: [PATCH 1/2] cmif_types: improve ergonomics of types --- src/core/hle/service/cmif_serialization.h | 6 +- src/core/hle/service/cmif_types.h | 112 +++++++++++++++++----- src/core/hle/service/jit/jit.cpp | 14 +-- src/core/hle/service/ro/ro.cpp | 3 +- 4 files changed, 97 insertions(+), 38 deletions(-) diff --git a/src/core/hle/service/cmif_serialization.h b/src/core/hle/service/cmif_serialization.h index 8e8cf2507e..84b736155b 100644 --- a/src/core/hle/service/cmif_serialization.h +++ b/src/core/hle/service/cmif_serialization.h @@ -150,7 +150,7 @@ void ReadInArgument(CallArguments& args, const u8* raw_data, HLERequestContext& return ReadInArgument(args, raw_data, ctx, temp); } else if constexpr (ArgumentTraits::Type == ArgumentType::InCopyHandle) { - std::get(args) = std::move(ctx.GetObjectFromHandle(ctx.GetCopyHandle(HandleIndex))); + std::get(args) = ctx.GetObjectFromHandle(ctx.GetCopyHandle(HandleIndex)).GetPointerUnsafe(); return ReadInArgument(args, raw_data, ctx, temp); } else if constexpr (ArgumentTraits::Type == ArgumentType::InLargeData) { @@ -253,11 +253,11 @@ void WriteOutArgument(CallArguments& args, u8* raw_data, HLERequestContext& ctx, return WriteOutArgument(args, raw_data, ctx, temp); } else if constexpr (ArgumentTraits::Type == ArgumentType::OutCopyHandle) { - ctx.AddCopyObject(std::get(args).GetPointerUnsafe()); + ctx.AddCopyObject(std::get(args)); return WriteOutArgument(args, raw_data, ctx, temp); } else if constexpr (ArgumentTraits::Type == ArgumentType::OutMoveHandle) { - ctx.AddMoveObject(std::get(args).GetPointerUnsafe()); + ctx.AddMoveObject(std::get(args)); return WriteOutArgument(args, raw_data, ctx, temp); } else if constexpr (ArgumentTraits::Type == ArgumentType::OutLargeData) { diff --git a/src/core/hle/service/cmif_types.h b/src/core/hle/service/cmif_types.h index b80028c19d..2610c49f3d 100644 --- a/src/core/hle/service/cmif_types.h +++ b/src/core/hle/service/cmif_types.h @@ -15,19 +15,21 @@ namespace Service { template class Out { public: - /* implicit */ Out(T& t) : raw(&t) {} + using Type = T; + + /* implicit */ Out(Type& t) : raw(&t) {} ~Out() = default; - T* Get() const { + Type* Get() const { return raw; } - T& operator*() { + Type& operator*() { return *raw; } private: - T* raw; + Type* raw; }; template @@ -45,51 +47,93 @@ struct ClientProcessId { u64 pid; }; +struct ProcessId { + explicit operator bool() const { + return pid != 0; + } + + const u64& operator*() const { + return pid; + } + + u64 pid; +}; + using ClientAppletResourceUserId = ClientProcessId; +using AppletResourceUserId = ProcessId; template -class InCopyHandle : public Kernel::KScopedAutoObject { +class InCopyHandle { public: using Type = T; - template - /* implicit */ InCopyHandle(Args&&... args) : Kernel::KScopedAutoObject(std::forward(args)...) {} + /* implicit */ InCopyHandle(Type* t) : raw(t) {} + /* implicit */ InCopyHandle() : raw() {} ~InCopyHandle() = default; - InCopyHandle& operator=(InCopyHandle&& rhs) { - Kernel::KScopedAutoObject::operator=(std::move(rhs)); + InCopyHandle& operator=(Type* rhs) { + raw = rhs; return *this; } + + Type* Get() const { + return raw; + } + + Type& operator*() const { + return *raw; + } + + Type* operator->() const { + return raw; + } + + explicit operator bool() const { + return raw != nullptr; + } + +private: + Type* raw; }; template -class OutCopyHandle : public Kernel::KScopedAutoObject { +class OutCopyHandle { public: - using Type = T; + using Type = T*; - template - /* implicit */ OutCopyHandle(Args&&... args) : Kernel::KScopedAutoObject(std::forward(args)...) {} + /* implicit */ OutCopyHandle(Type& t) : raw(&t) {} ~OutCopyHandle() = default; - OutCopyHandle& operator=(OutCopyHandle&& rhs) { - Kernel::KScopedAutoObject::operator=(std::move(rhs)); - return *this; + Type* Get() const { + return raw; } + + Type& operator*() { + return *raw; + } + +private: + Type* raw; }; template -class OutMoveHandle : public Kernel::KScopedAutoObject { +class OutMoveHandle { public: - using Type = T; + using Type = T*; - template - /* implicit */ OutMoveHandle(Args&&... args) : Kernel::KScopedAutoObject(std::forward(args)...) {} + /* implicit */ OutMoveHandle(Type& t) : raw(&t) {} ~OutMoveHandle() = default; - OutMoveHandle& operator=(OutMoveHandle&& rhs) { - Kernel::KScopedAutoObject::operator=(std::move(rhs)); - return *this; + Type* Get() const { + return raw; } + + Type& operator*() { + return *raw; + } + +private: + Type* raw; }; enum BufferAttr : int { @@ -105,12 +149,15 @@ enum BufferAttr : int { template struct Buffer : public std::span { - static_assert(std::is_trivial_v, "Buffer type must be trivial"); + static_assert(std::is_trivially_copyable_v, "Buffer type must be trivially copyable"); static_assert((A & BufferAttr_FixedSize) == 0, "Buffer attr must not contain FixedSize"); static_assert(((A & BufferAttr_In) == 0) ^ ((A & BufferAttr_Out) == 0), "Buffer attr must be In or Out"); static constexpr BufferAttr Attr = static_cast(A); using Type = T; + /* implicit */ Buffer(const std::span& rhs) : std::span(rhs) {} + /* implicit */ Buffer() = default; + Buffer& operator=(const std::span& rhs) { std::span::operator=(rhs); return *this; @@ -139,11 +186,14 @@ using OutArray = Buffer; template struct LargeData : public T { - static_assert(std::is_trivial_v, "LargeData type must be trivial"); + static_assert(std::is_trivially_copyable_v, "LargeData type must be trivially copyable"); static_assert((A & BufferAttr_FixedSize) != 0, "LargeData attr must contain FixedSize"); static_assert(((A & BufferAttr_In) == 0) ^ ((A & BufferAttr_Out) == 0), "LargeData attr must be In or Out"); static constexpr BufferAttr Attr = static_cast(A); using Type = T; + + /* implicit */ LargeData(const T& rhs) : T(rhs) {} + /* implicit */ LargeData() = default; }; template @@ -159,7 +209,17 @@ struct RemoveOut { template struct RemoveOut> { - using Type = T; + using Type = typename Out::Type; +}; + +template +struct RemoveOut> { + using Type = typename OutCopyHandle::Type; +}; + +template +struct RemoveOut> { + using Type = typename OutMoveHandle::Type; }; enum class ArgumentType { diff --git a/src/core/hle/service/jit/jit.cpp b/src/core/hle/service/jit/jit.cpp index d8fefff894..1f2cbcb61d 100644 --- a/src/core/hle/service/jit/jit.cpp +++ b/src/core/hle/service/jit/jit.cpp @@ -27,7 +27,7 @@ static_assert(sizeof(Struct32) == 32, "Struct32 has wrong size"); class IJitEnvironment final : public ServiceFramework { public: explicit IJitEnvironment(Core::System& system_, - Kernel::KScopedAutoObject&& process_, + Kernel::KScopedAutoObject process_, CodeMemory&& user_rx_, CodeMemory&& user_ro_) : ServiceFramework{system_, "IJitEnvironment"}, process{std::move(process_)}, user_rx{std::move(user_rx_)}, user_ro{std::move(user_ro_)}, @@ -129,7 +129,7 @@ public: Result LoadPlugin(u64 tmem_size, InCopyHandle& tmem, InBuffer nrr, InBuffer nro) { - if (tmem.IsNull()) { + if (!tmem) { LOG_ERROR(Service_JIT, "Invalid transfer memory handle!"); R_THROW(ResultUnknown); } @@ -271,15 +271,15 @@ private: u64 rx_size, u64 ro_size, InCopyHandle& process, InCopyHandle& rx_mem, InCopyHandle& ro_mem) { - if (process.IsNull()) { + if (!process) { LOG_ERROR(Service_JIT, "process is null"); R_THROW(ResultUnknown); } - if (rx_mem.IsNull()) { + if (!rx_mem) { LOG_ERROR(Service_JIT, "rx_mem is null"); R_THROW(ResultUnknown); } - if (rx_mem.IsNull()) { + if (!ro_mem) { LOG_ERROR(Service_JIT, "ro_mem is null"); R_THROW(ResultUnknown); } @@ -291,8 +291,8 @@ private: R_TRY(ro.Initialize(*process, *ro_mem, ro_size, Kernel::Svc::MemoryPermission::Read, generate_random)); - *out_jit_environment = std::make_shared(system, std::move(process), - std::move(rx), std::move(ro)); + *out_jit_environment = + std::make_shared(system, process.Get(), std::move(rx), std::move(ro)); R_SUCCEED(); } diff --git a/src/core/hle/service/ro/ro.cpp b/src/core/hle/service/ro/ro.cpp index ae62c430e7..51196170a7 100644 --- a/src/core/hle/service/ro/ro.cpp +++ b/src/core/hle/service/ro/ro.cpp @@ -551,8 +551,7 @@ public: Result RegisterProcessHandle(ClientProcessId client_pid, InCopyHandle& process) { // Register the process. - R_RETURN(m_ro->RegisterProcess(std::addressof(m_context_id), process.GetPointerUnsafe(), - *client_pid)); + R_RETURN(m_ro->RegisterProcess(std::addressof(m_context_id), process.Get(), *client_pid)); } Result RegisterProcessModuleInfo(ClientProcessId client_pid, u64 nrr_address, u64 nrr_size, From a774ff935c3b34298db17465e6053a703ed7d782 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 25 Jan 2024 21:26:44 -0500 Subject: [PATCH 2/2] cmif_serialization: support non-domain sessions on domain servers --- src/core/hle/service/cmif_serialization.h | 65 +++++++++++------------ 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/src/core/hle/service/cmif_serialization.h b/src/core/hle/service/cmif_serialization.h index 84b736155b..9eb10e8168 100644 --- a/src/core/hle/service/cmif_serialization.h +++ b/src/core/hle/service/cmif_serialization.h @@ -97,20 +97,20 @@ constexpr RequestLayout GetDomainReplyOutLayout() { }; } -template -constexpr RequestLayout GetReplyInLayout() { - return Domain ? GetDomainReplyInLayout() : GetNonDomainReplyInLayout(); +template +constexpr RequestLayout GetReplyInLayout(bool is_domain) { + return is_domain ? GetDomainReplyInLayout() : GetNonDomainReplyInLayout(); } -template -constexpr RequestLayout GetReplyOutLayout() { - return Domain ? GetDomainReplyOutLayout() : GetNonDomainReplyOutLayout(); +template +constexpr RequestLayout GetReplyOutLayout(bool is_domain) { + return is_domain ? GetDomainReplyOutLayout() : GetNonDomainReplyOutLayout(); } using OutTemporaryBuffers = std::array, 3>; -template -void ReadInArgument(CallArguments& args, const u8* raw_data, HLERequestContext& ctx, OutTemporaryBuffers& temp) { +template +void ReadInArgument(bool is_domain, CallArguments& args, const u8* raw_data, HLERequestContext& ctx, OutTemporaryBuffers& temp) { if constexpr (ArgIndex >= std::tuple_size_v) { return; } else { @@ -134,25 +134,25 @@ void ReadInArgument(CallArguments& args, const u8* raw_data, HLERequestContext& std::memcpy(&std::get(args), raw_data + ArgOffset, ArgSize); } - return ReadInArgument(args, raw_data, ctx, temp); + return ReadInArgument(is_domain, args, raw_data, ctx, temp); } else if constexpr (ArgumentTraits::Type == ArgumentType::InInterface) { constexpr size_t ArgAlign = alignof(u32); constexpr size_t ArgSize = sizeof(u32); constexpr size_t ArgOffset = Common::AlignUp(DataOffset, ArgAlign); constexpr size_t ArgEnd = ArgOffset + ArgSize; - static_assert(Domain); + ASSERT(is_domain); ASSERT(ctx.GetDomainMessageHeader().input_object_count > 0); u32 value{}; std::memcpy(&value, raw_data + ArgOffset, ArgSize); std::get(args) = ctx.GetDomainHandler(value - 1); - return ReadInArgument(args, raw_data, ctx, temp); + return ReadInArgument(is_domain, args, raw_data, ctx, temp); } else if constexpr (ArgumentTraits::Type == ArgumentType::InCopyHandle) { std::get(args) = ctx.GetObjectFromHandle(ctx.GetCopyHandle(HandleIndex)).GetPointerUnsafe(); - return ReadInArgument(args, raw_data, ctx, temp); + return ReadInArgument(is_domain, args, raw_data, ctx, temp); } else if constexpr (ArgumentTraits::Type == ArgumentType::InLargeData) { constexpr size_t BufferSize = sizeof(ArgType); @@ -172,7 +172,7 @@ void ReadInArgument(CallArguments& args, const u8* raw_data, HLERequestContext& std::memcpy(&std::get(args), buffer.data(), std::min(BufferSize, buffer.size())); - return ReadInArgument(args, raw_data, ctx, temp); + return ReadInArgument(is_domain, args, raw_data, ctx, temp); } else if constexpr (ArgumentTraits::Type == ArgumentType::InBuffer) { using ElementType = typename ArgType::Type; @@ -193,14 +193,14 @@ void ReadInArgument(CallArguments& args, const u8* raw_data, HLERequestContext& std::get(args) = std::span(ptr, size); - return ReadInArgument(args, raw_data, ctx, temp); + return ReadInArgument(is_domain, args, raw_data, ctx, temp); } else if constexpr (ArgumentTraits::Type == ArgumentType::OutLargeData) { constexpr size_t BufferSize = sizeof(ArgType); // Clear the existing data. std::memset(&std::get(args), 0, BufferSize); - return ReadInArgument(args, raw_data, ctx, temp); + return ReadInArgument(is_domain, args, raw_data, ctx, temp); } else if constexpr (ArgumentTraits::Type == ArgumentType::OutBuffer) { using ElementType = typename ArgType::Type; @@ -217,15 +217,15 @@ void ReadInArgument(CallArguments& args, const u8* raw_data, HLERequestContext& std::get(args) = std::span(ptr, size); - return ReadInArgument(args, raw_data, ctx, temp); + return ReadInArgument(is_domain, args, raw_data, ctx, temp); } else { - return ReadInArgument(args, raw_data, ctx, temp); + return ReadInArgument(is_domain, args, raw_data, ctx, temp); } } } -template -void WriteOutArgument(CallArguments& args, u8* raw_data, HLERequestContext& ctx, OutTemporaryBuffers& temp) { +template +void WriteOutArgument(bool is_domain, CallArguments& args, u8* raw_data, HLERequestContext& ctx, OutTemporaryBuffers& temp) { if constexpr (ArgIndex >= std::tuple_size_v) { return; } else { @@ -243,23 +243,23 @@ void WriteOutArgument(CallArguments& args, u8* raw_data, HLERequestContext& ctx, std::memcpy(raw_data + ArgOffset, &std::get(args), ArgSize); - return WriteOutArgument(args, raw_data, ctx, temp); + return WriteOutArgument(is_domain, args, raw_data, ctx, temp); } else if constexpr (ArgumentTraits::Type == ArgumentType::OutInterface) { - if constexpr (Domain) { + if (is_domain) { ctx.AddDomainObject(std::get(args)); } else { ctx.AddMoveInterface(std::get(args)); } - return WriteOutArgument(args, raw_data, ctx, temp); + return WriteOutArgument(is_domain, args, raw_data, ctx, temp); } else if constexpr (ArgumentTraits::Type == ArgumentType::OutCopyHandle) { ctx.AddCopyObject(std::get(args)); - return WriteOutArgument(args, raw_data, ctx, temp); + return WriteOutArgument(is_domain, args, raw_data, ctx, temp); } else if constexpr (ArgumentTraits::Type == ArgumentType::OutMoveHandle) { ctx.AddMoveObject(std::get(args)); - return WriteOutArgument(args, raw_data, ctx, temp); + return WriteOutArgument(is_domain, args, raw_data, ctx, temp); } else if constexpr (ArgumentTraits::Type == ArgumentType::OutLargeData) { constexpr size_t BufferSize = sizeof(ArgType); @@ -272,7 +272,7 @@ void WriteOutArgument(CallArguments& args, u8* raw_data, HLERequestContext& ctx, ctx.WriteBufferC(&std::get(args), BufferSize, OutBufferIndex); } - return WriteOutArgument(args, raw_data, ctx, temp); + return WriteOutArgument(is_domain, args, raw_data, ctx, temp); } else if constexpr (ArgumentTraits::Type == ArgumentType::OutBuffer) { auto& buffer = temp[OutBufferIndex]; const size_t size = buffer.size(); @@ -287,9 +287,9 @@ void WriteOutArgument(CallArguments& args, u8* raw_data, HLERequestContext& ctx, } } - return WriteOutArgument( args, raw_data, ctx, temp); + return WriteOutArgument(is_domain, args, raw_data, ctx, temp); } else { - return WriteOutArgument(args, raw_data, ctx, temp); + return WriteOutArgument(is_domain, args, raw_data, ctx, temp); } } } @@ -297,11 +297,10 @@ void WriteOutArgument(CallArguments& args, u8* raw_data, HLERequestContext& ctx, template void CmifReplyWrapImpl(HLERequestContext& ctx, T& t, Result (T::*f)(A...)) { // Verify domain state. - if constexpr (Domain) { - ASSERT_MSG(ctx.GetManager()->IsDomain(), "Domain reply used on non-domain session"); - } else { + if constexpr (!Domain) { ASSERT_MSG(!ctx.GetManager()->IsDomain(), "Non-domain reply used on domain session"); } + const bool is_domain = Domain ? ctx.GetManager()->IsDomain() : false; using MethodArguments = std::tuple...>; @@ -310,7 +309,7 @@ void CmifReplyWrapImpl(HLERequestContext& ctx, T& t, Result (T::*f)(A...)) { // Read inputs. const size_t offset_plus_command_id = ctx.GetDataPayloadOffset() + 2; - ReadInArgument(call_arguments, reinterpret_cast(ctx.CommandBuffer() + offset_plus_command_id), ctx, buffers); + ReadInArgument(is_domain, call_arguments, reinterpret_cast(ctx.CommandBuffer() + offset_plus_command_id), ctx, buffers); // Call. const auto Callable = [&](CallArgs&... args) { @@ -319,12 +318,12 @@ void CmifReplyWrapImpl(HLERequestContext& ctx, T& t, Result (T::*f)(A...)) { const Result res = std::apply(Callable, call_arguments); // Write result. - constexpr RequestLayout layout = GetReplyOutLayout(); + const RequestLayout layout = GetReplyOutLayout(is_domain); IPC::ResponseBuilder rb{ctx, 2 + Common::DivCeil(layout.cmif_raw_data_size, sizeof(u32)), layout.copy_handle_count, layout.move_handle_count + layout.domain_interface_count}; rb.Push(res); // Write out arguments. - WriteOutArgument(call_arguments, reinterpret_cast(ctx.CommandBuffer() + rb.GetCurrentOffset()), ctx, buffers); + WriteOutArgument(is_domain, call_arguments, reinterpret_cast(ctx.CommandBuffer() + rb.GetCurrentOffset()), ctx, buffers); } // clang-format on