diff options
Diffstat (limited to 'src/core/hle/service/cmif_serialization.h')
-rw-r--r-- | src/core/hle/service/cmif_serialization.h | 177 |
1 files changed, 155 insertions, 22 deletions
diff --git a/src/core/hle/service/cmif_serialization.h b/src/core/hle/service/cmif_serialization.h index 9eb10e816..315475e71 100644 --- a/src/core/hle/service/cmif_serialization.h +++ b/src/core/hle/service/cmif_serialization.h @@ -12,6 +12,109 @@ namespace Service { // clang-format off +template <typename T> +struct UnwrapArg { + using Type = std::remove_cvref_t<T>; +}; + +template <typename T, int A> +struct UnwrapArg<InLargeData<T, A>> { + using Type = std::remove_cv_t<typename InLargeData<T, A>::Type>; +}; + +template <typename T> +struct UnwrapArg<Out<T>> { + using Type = AutoOut<typename Out<T>::Type>; +}; + +template <typename T> +struct UnwrapArg<OutCopyHandle<T>> { + using Type = AutoOut<typename OutCopyHandle<T>::Type>; +}; + +template <typename T> +struct UnwrapArg<OutMoveHandle<T>> { + using Type = AutoOut<typename OutMoveHandle<T>::Type>; +}; + +template <typename T, int A> +struct UnwrapArg<OutLargeData<T, A>> { + using Type = AutoOut<typename OutLargeData<T, A>::Type>; +}; + +enum class ArgumentType { + InProcessId, + InData, + InInterface, + InCopyHandle, + OutData, + OutInterface, + OutCopyHandle, + OutMoveHandle, + InBuffer, + InLargeData, + OutBuffer, + OutLargeData, +}; + +template <typename T> +struct ArgumentTraits; + +template <> +struct ArgumentTraits<ClientProcessId> { + static constexpr ArgumentType Type = ArgumentType::InProcessId; +}; + +template <typename T> +struct ArgumentTraits<SharedPointer<T>> { + static constexpr ArgumentType Type = ArgumentType::InInterface; +}; + +template <typename T> +struct ArgumentTraits<InCopyHandle<T>> { + static constexpr ArgumentType Type = ArgumentType::InCopyHandle; +}; + +template <typename T> +struct ArgumentTraits<Out<SharedPointer<T>>> { + static constexpr ArgumentType Type = ArgumentType::OutInterface; +}; + +template <typename T> +struct ArgumentTraits<Out<T>> { + static constexpr ArgumentType Type = ArgumentType::OutData; +}; + +template <typename T> +struct ArgumentTraits<OutCopyHandle<T>> { + static constexpr ArgumentType Type = ArgumentType::OutCopyHandle; +}; + +template <typename T> +struct ArgumentTraits<OutMoveHandle<T>> { + static constexpr ArgumentType Type = ArgumentType::OutMoveHandle; +}; + +template <typename T, int A> +struct ArgumentTraits<Buffer<T, A>> { + static constexpr ArgumentType Type = (A & BufferAttr_In) == 0 ? ArgumentType::OutBuffer : ArgumentType::InBuffer; +}; + +template <typename T, int A> +struct ArgumentTraits<InLargeData<T, A>> { + static constexpr ArgumentType Type = ArgumentType::InLargeData; +}; + +template <typename T, int A> +struct ArgumentTraits<OutLargeData<T, A>> { + static constexpr ArgumentType Type = ArgumentType::OutLargeData; +}; + +template <typename T> +struct ArgumentTraits { + static constexpr ArgumentType Type = ArgumentType::InData; +}; + struct RequestLayout { u32 copy_handle_count; u32 move_handle_count; @@ -19,14 +122,14 @@ struct RequestLayout { u32 domain_interface_count; }; -template <ArgumentType Type1, ArgumentType Type2, typename MethodArguments, size_t PrevAlign = 1, size_t DataOffset = 0, size_t ArgIndex = 0> -constexpr u32 GetArgumentRawDataSize() { +template <typename MethodArguments, size_t PrevAlign = 1, size_t DataOffset = 0, size_t ArgIndex = 0> +constexpr u32 GetInRawDataSize() { if constexpr (ArgIndex >= std::tuple_size_v<MethodArguments>) { return static_cast<u32>(DataOffset); } else { using ArgType = std::tuple_element_t<ArgIndex, MethodArguments>; - if constexpr (ArgumentTraits<ArgType>::Type == Type1 || ArgumentTraits<ArgType>::Type == Type2) { + if constexpr (ArgumentTraits<ArgType>::Type == ArgumentType::InData || ArgumentTraits<ArgType>::Type == ArgumentType::InProcessId) { constexpr size_t ArgAlign = alignof(ArgType); constexpr size_t ArgSize = sizeof(ArgType); @@ -35,9 +138,33 @@ constexpr u32 GetArgumentRawDataSize() { constexpr size_t ArgOffset = Common::AlignUp(DataOffset, ArgAlign); constexpr size_t ArgEnd = ArgOffset + ArgSize; - return GetArgumentRawDataSize<Type1, Type2, MethodArguments, ArgAlign, ArgEnd, ArgIndex + 1>(); + return GetInRawDataSize<MethodArguments, ArgAlign, ArgEnd, ArgIndex + 1>(); } else { - return GetArgumentRawDataSize<Type1, Type2, MethodArguments, PrevAlign, DataOffset, ArgIndex + 1>(); + return GetInRawDataSize<MethodArguments, PrevAlign, DataOffset, ArgIndex + 1>(); + } + } +} + +template <typename MethodArguments, size_t PrevAlign = 1, size_t DataOffset = 0, size_t ArgIndex = 0> +constexpr u32 GetOutRawDataSize() { + if constexpr (ArgIndex >= std::tuple_size_v<MethodArguments>) { + return static_cast<u32>(DataOffset); + } else { + using ArgType = std::tuple_element_t<ArgIndex, MethodArguments>; + + if constexpr (ArgumentTraits<ArgType>::Type == ArgumentType::OutData) { + using RawArgType = typename ArgType::Type; + constexpr size_t ArgAlign = alignof(RawArgType); + constexpr size_t ArgSize = sizeof(RawArgType); + + static_assert(PrevAlign <= ArgAlign, "Output argument is not ordered by alignment"); + + constexpr size_t ArgOffset = Common::AlignUp(DataOffset, ArgAlign); + constexpr size_t ArgEnd = ArgOffset + ArgSize; + + return GetOutRawDataSize<MethodArguments, ArgAlign, ArgEnd, ArgIndex + 1>(); + } else { + return GetOutRawDataSize<MethodArguments, PrevAlign, DataOffset, ArgIndex + 1>(); } } } @@ -62,7 +189,7 @@ constexpr RequestLayout GetNonDomainReplyInLayout() { return RequestLayout{ .copy_handle_count = GetArgumentTypeCount<ArgumentType::InCopyHandle, MethodArguments>(), .move_handle_count = 0, - .cmif_raw_data_size = GetArgumentRawDataSize<ArgumentType::InData, ArgumentType::InProcessId, MethodArguments>(), + .cmif_raw_data_size = GetInRawDataSize<MethodArguments>(), .domain_interface_count = 0, }; } @@ -72,7 +199,7 @@ constexpr RequestLayout GetDomainReplyInLayout() { return RequestLayout{ .copy_handle_count = GetArgumentTypeCount<ArgumentType::InCopyHandle, MethodArguments>(), .move_handle_count = 0, - .cmif_raw_data_size = GetArgumentRawDataSize<ArgumentType::InData, ArgumentType::InProcessId, MethodArguments>(), + .cmif_raw_data_size = GetInRawDataSize<MethodArguments>(), .domain_interface_count = GetArgumentTypeCount<ArgumentType::InInterface, MethodArguments>(), }; } @@ -82,7 +209,7 @@ constexpr RequestLayout GetNonDomainReplyOutLayout() { return RequestLayout{ .copy_handle_count = GetArgumentTypeCount<ArgumentType::OutCopyHandle, MethodArguments>(), .move_handle_count = GetArgumentTypeCount<ArgumentType::OutMoveHandle, MethodArguments>() + GetArgumentTypeCount<ArgumentType::OutInterface, MethodArguments>(), - .cmif_raw_data_size = GetArgumentRawDataSize<ArgumentType::OutData, ArgumentType::OutData, MethodArguments>(), + .cmif_raw_data_size = GetOutRawDataSize<MethodArguments>(), .domain_interface_count = 0, }; } @@ -92,7 +219,7 @@ constexpr RequestLayout GetDomainReplyOutLayout() { return RequestLayout{ .copy_handle_count = GetArgumentTypeCount<ArgumentType::OutCopyHandle, MethodArguments>(), .move_handle_count = GetArgumentTypeCount<ArgumentType::OutMoveHandle, MethodArguments>(), - .cmif_raw_data_size = GetArgumentRawDataSize<ArgumentType::OutData, ArgumentType::OutData, MethodArguments>(), + .cmif_raw_data_size = GetOutRawDataSize<MethodArguments>(), .domain_interface_count = GetArgumentTypeCount<ArgumentType::OutInterface, MethodArguments>(), }; } @@ -122,6 +249,8 @@ void ReadInArgument(bool is_domain, CallArguments& args, const u8* raw_data, HLE static_assert(PrevAlign <= ArgAlign, "Input argument is not ordered by alignment"); static_assert(!RawDataFinished, "All input interface arguments must appear after raw data"); + static_assert(!std::is_pointer_v<ArgType>, "Input raw data must not be a pointer"); + static_assert(std::is_trivially_copyable_v<ArgType>, "Input raw data must be trivially copyable"); constexpr size_t ArgOffset = Common::AlignUp(DataOffset, ArgAlign); constexpr size_t ArgEnd = ArgOffset + ArgSize; @@ -154,7 +283,7 @@ void ReadInArgument(bool is_domain, CallArguments& args, const u8* raw_data, HLE return ReadInArgument<MethodArguments, CallArguments, PrevAlign, DataOffset, HandleIndex + 1, InBufferIndex, OutBufferIndex, RawDataFinished, ArgIndex + 1>(is_domain, args, raw_data, ctx, temp); } else if constexpr (ArgumentTraits<ArgType>::Type == ArgumentType::InLargeData) { - constexpr size_t BufferSize = sizeof(ArgType); + constexpr size_t BufferSize = sizeof(typename ArgType::Type); // Clear the existing data. std::memset(&std::get<ArgIndex>(args), 0, BufferSize); @@ -195,10 +324,10 @@ void ReadInArgument(bool is_domain, CallArguments& args, const u8* raw_data, HLE return ReadInArgument<MethodArguments, CallArguments, PrevAlign, DataOffset, HandleIndex, InBufferIndex + 1, OutBufferIndex, RawDataFinished, ArgIndex + 1>(is_domain, args, raw_data, ctx, temp); } else if constexpr (ArgumentTraits<ArgType>::Type == ArgumentType::OutLargeData) { - constexpr size_t BufferSize = sizeof(ArgType); + constexpr size_t BufferSize = sizeof(typename ArgType::Type); // Clear the existing data. - std::memset(&std::get<ArgIndex>(args), 0, BufferSize); + std::memset(&std::get<ArgIndex>(args).raw, 0, BufferSize); return ReadInArgument<MethodArguments, CallArguments, PrevAlign, DataOffset, HandleIndex, InBufferIndex, OutBufferIndex + 1, RawDataFinished, ArgIndex + 1>(is_domain, args, raw_data, ctx, temp); } else if constexpr (ArgumentTraits<ArgType>::Type == ArgumentType::OutBuffer) { @@ -232,36 +361,40 @@ void WriteOutArgument(bool is_domain, CallArguments& args, u8* raw_data, HLERequ using ArgType = std::tuple_element_t<ArgIndex, MethodArguments>; if constexpr (ArgumentTraits<ArgType>::Type == ArgumentType::OutData) { - constexpr size_t ArgAlign = alignof(ArgType); - constexpr size_t ArgSize = sizeof(ArgType); + using RawArgType = decltype(std::get<ArgIndex>(args).raw); + constexpr size_t ArgAlign = alignof(RawArgType); + constexpr size_t ArgSize = sizeof(RawArgType); static_assert(PrevAlign <= ArgAlign, "Output argument is not ordered by alignment"); static_assert(!RawDataFinished, "All output interface arguments must appear after raw data"); + static_assert(!std::is_pointer_v<ArgType>, "Output raw data must not be a pointer"); + static_assert(!std::is_pointer_v<RawArgType>, "Output raw data must not be a pointer"); + static_assert(std::is_trivially_copyable_v<RawArgType>, "Output raw data must be trivially copyable"); constexpr size_t ArgOffset = Common::AlignUp(DataOffset, ArgAlign); constexpr size_t ArgEnd = ArgOffset + ArgSize; - std::memcpy(raw_data + ArgOffset, &std::get<ArgIndex>(args), ArgSize); + std::memcpy(raw_data + ArgOffset, &std::get<ArgIndex>(args).raw, ArgSize); return WriteOutArgument<MethodArguments, CallArguments, ArgAlign, ArgEnd, OutBufferIndex, false, ArgIndex + 1>(is_domain, args, raw_data, ctx, temp); } else if constexpr (ArgumentTraits<ArgType>::Type == ArgumentType::OutInterface) { if (is_domain) { - ctx.AddDomainObject(std::get<ArgIndex>(args)); + ctx.AddDomainObject(std::get<ArgIndex>(args).raw); } else { - ctx.AddMoveInterface(std::get<ArgIndex>(args)); + ctx.AddMoveInterface(std::get<ArgIndex>(args).raw); } return WriteOutArgument<MethodArguments, CallArguments, PrevAlign, DataOffset, OutBufferIndex, true, ArgIndex + 1>(is_domain, args, raw_data, ctx, temp); } else if constexpr (ArgumentTraits<ArgType>::Type == ArgumentType::OutCopyHandle) { - ctx.AddCopyObject(std::get<ArgIndex>(args)); + ctx.AddCopyObject(std::get<ArgIndex>(args).raw); return WriteOutArgument<MethodArguments, CallArguments, PrevAlign, DataOffset, OutBufferIndex, RawDataFinished, ArgIndex + 1>(is_domain, args, raw_data, ctx, temp); } else if constexpr (ArgumentTraits<ArgType>::Type == ArgumentType::OutMoveHandle) { - ctx.AddMoveObject(std::get<ArgIndex>(args)); + ctx.AddMoveObject(std::get<ArgIndex>(args).raw); return WriteOutArgument<MethodArguments, CallArguments, PrevAlign, DataOffset, OutBufferIndex, RawDataFinished, ArgIndex + 1>(is_domain, args, raw_data, ctx, temp); } else if constexpr (ArgumentTraits<ArgType>::Type == ArgumentType::OutLargeData) { - constexpr size_t BufferSize = sizeof(ArgType); + constexpr size_t BufferSize = sizeof(typename ArgType::Type); ASSERT(ctx.CanWriteBuffer(OutBufferIndex)); if constexpr (ArgType::Attr & BufferAttr_HipcAutoSelect) { @@ -302,10 +435,10 @@ void CmifReplyWrapImpl(HLERequestContext& ctx, T& t, Result (T::*f)(A...)) { } const bool is_domain = Domain ? ctx.GetManager()->IsDomain() : false; - using MethodArguments = std::tuple<std::remove_reference_t<A>...>; + using MethodArguments = std::tuple<std::remove_cvref_t<A>...>; OutTemporaryBuffers buffers{}; - auto call_arguments = std::tuple<typename RemoveOut<A>::Type...>(); + auto call_arguments = std::tuple<typename UnwrapArg<A>::Type...>(); // Read inputs. const size_t offset_plus_command_id = ctx.GetDataPayloadOffset() + 2; |