From b1c2f791af08b3eaba53c1ce1673fe0729fc5d26 Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 1 Jan 2024 21:58:56 -0500 Subject: [PATCH] am: rework IStorage for transfer storage --- src/core/hle/service/am/am_results.h | 1 + .../hle/service/am/library_applet_creator.cpp | 28 +++---- src/core/hle/service/am/storage.cpp | 73 ++++++++-------- src/core/hle/service/am/storage.h | 25 ++---- src/core/hle/service/am/storage_accessor.cpp | 84 ++++++++++--------- src/core/hle/service/am/storage_accessor.h | 17 +++- 6 files changed, 118 insertions(+), 110 deletions(-) diff --git a/src/core/hle/service/am/am_results.h b/src/core/hle/service/am/am_results.h index e82d391ad..a2afc9eec 100644 --- a/src/core/hle/service/am/am_results.h +++ b/src/core/hle/service/am/am_results.h @@ -10,6 +10,7 @@ namespace Service::AM { constexpr Result ResultNoDataInChannel{ErrorModule::AM, 2}; constexpr Result ResultNoMessages{ErrorModule::AM, 3}; constexpr Result ResultInvalidOffset{ErrorModule::AM, 503}; +constexpr Result ResultInvalidStorageType{ErrorModule::AM, 511}; constexpr Result ResultFatalSectionCountImbalance{ErrorModule::AM, 512}; } // namespace Service::AM diff --git a/src/core/hle/service/am/library_applet_creator.cpp b/src/core/hle/service/am/library_applet_creator.cpp index e4332e244..888b8b44b 100644 --- a/src/core/hle/service/am/library_applet_creator.cpp +++ b/src/core/hle/service/am/library_applet_creator.cpp @@ -6,6 +6,7 @@ #include "core/hle/service/am/frontend/applets.h" #include "core/hle/service/am/library_applet_accessor.h" #include "core/hle/service/am/library_applet_creator.h" +#include "core/hle/service/am/library_applet_storage.h" #include "core/hle/service/am/storage.h" #include "core/hle/service/ipc_helpers.h" #include "core/hle/service/sm/sm.h" @@ -164,28 +165,28 @@ void ILibraryAppletCreator::CreateStorage(HLERequestContext& ctx) { return; } - std::vector buffer(size); + std::vector data(size); IPC::ResponseBuilder rb{ctx, 2, 0, 1}; rb.Push(ResultSuccess); - rb.PushIpcInterface(system, std::move(buffer)); + rb.PushIpcInterface(system, AM::CreateStorage(std::move(data))); } void ILibraryAppletCreator::CreateTransferMemoryStorage(HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; struct Parameters { - u8 permissions; + bool is_writable; s64 size; }; - const auto parameters{rp.PopRaw()}; + const auto params{rp.PopRaw()}; const auto handle{ctx.GetCopyHandle(0)}; - LOG_DEBUG(Service_AM, "called, permissions={}, size={}, handle={:08X}", parameters.permissions, - parameters.size, handle); + LOG_DEBUG(Service_AM, "called, is_writable={}, size={}, handle={:08X}", params.is_writable, + params.size, handle); - if (parameters.size <= 0) { + if (params.size <= 0) { LOG_ERROR(Service_AM, "size is less than or equal to 0"); IPC::ResponseBuilder rb{ctx, 2}; rb.Push(ResultUnknown); @@ -201,12 +202,11 @@ void ILibraryAppletCreator::CreateTransferMemoryStorage(HLERequestContext& ctx) return; } - std::vector memory(transfer_mem->GetSize()); - ctx.GetMemory().ReadBlock(transfer_mem->GetSourceAddress(), memory.data(), memory.size()); - IPC::ResponseBuilder rb{ctx, 2, 0, 1}; rb.Push(ResultSuccess); - rb.PushIpcInterface(system, std::move(memory)); + rb.PushIpcInterface( + system, AM::CreateTransferMemoryStorage(ctx.GetMemory(), transfer_mem.GetPointerUnsafe(), + params.is_writable, params.size)); } void ILibraryAppletCreator::CreateHandleStorage(HLERequestContext& ctx) { @@ -233,12 +233,10 @@ void ILibraryAppletCreator::CreateHandleStorage(HLERequestContext& ctx) { return; } - std::vector memory(transfer_mem->GetSize()); - ctx.GetMemory().ReadBlock(transfer_mem->GetSourceAddress(), memory.data(), memory.size()); - IPC::ResponseBuilder rb{ctx, 2, 0, 1}; rb.Push(ResultSuccess); - rb.PushIpcInterface(system, std::move(memory)); + rb.PushIpcInterface( + system, AM::CreateHandleStorage(ctx.GetMemory(), transfer_mem.GetPointerUnsafe(), size)); } } // namespace Service::AM diff --git a/src/core/hle/service/am/storage.cpp b/src/core/hle/service/am/storage.cpp index 9a86c867a..4e82afd1c 100644 --- a/src/core/hle/service/am/storage.cpp +++ b/src/core/hle/service/am/storage.cpp @@ -1,60 +1,59 @@ // SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later +#include "core/hle/service/am/am_results.h" +#include "core/hle/service/am/library_applet_storage.h" #include "core/hle/service/am/storage.h" #include "core/hle/service/am/storage_accessor.h" #include "core/hle/service/ipc_helpers.h" namespace Service::AM { -IStorageImpl::~IStorageImpl() = default; - -class StorageDataImpl final : public IStorageImpl { -public: - explicit StorageDataImpl(std::vector&& buffer_) : buffer{std::move(buffer_)} {} - - std::vector& GetData() override { - return buffer; - } - - const std::vector& GetData() const override { - return buffer; - } - - std::size_t GetSize() const override { - return buffer.size(); - } - -private: - std::vector buffer; -}; - -IStorage::IStorage(Core::System& system_, std::vector&& buffer) - : ServiceFramework{system_, "IStorage"}, - impl{std::make_shared(std::move(buffer))} { - Register(); -} - -void IStorage::Register() { - // clang-format off - static const FunctionInfo functions[] = { - {0, &IStorage::Open, "Open"}, - {1, nullptr, "OpenTransferStorage"}, - }; - // clang-format on +IStorage::IStorage(Core::System& system_, std::shared_ptr impl_) + : ServiceFramework{system_, "IStorage"}, impl{std::move(impl_)} { + static const FunctionInfo functions[] = { + {0, &IStorage::Open, "Open"}, + {1, &IStorage::OpenTransferStorage, "OpenTransferStorage"}, + }; RegisterHandlers(functions); } +IStorage::IStorage(Core::System& system_, std::vector&& data) + : IStorage(system_, CreateStorage(std::move(data))) {} + IStorage::~IStorage() = default; void IStorage::Open(HLERequestContext& ctx) { LOG_DEBUG(Service_AM, "called"); - IPC::ResponseBuilder rb{ctx, 2, 0, 1}; + if (impl->GetHandle() != nullptr) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(AM::ResultInvalidStorageType); + return; + } + IPC::ResponseBuilder rb{ctx, 2, 0, 1}; rb.Push(ResultSuccess); - rb.PushIpcInterface(system, *this); + rb.PushIpcInterface(system, impl); +} + +void IStorage::OpenTransferStorage(HLERequestContext& ctx) { + LOG_DEBUG(Service_AM, "called"); + + if (impl->GetHandle() == nullptr) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(AM::ResultInvalidStorageType); + return; + } + + IPC::ResponseBuilder rb{ctx, 2, 0, 1}; + rb.Push(ResultSuccess); + rb.PushIpcInterface(system, impl); +} + +std::vector IStorage::GetData() const { + return impl->GetData(); } } // namespace Service::AM diff --git a/src/core/hle/service/am/storage.h b/src/core/hle/service/am/storage.h index d47a8d89f..10d00b141 100644 --- a/src/core/hle/service/am/storage.h +++ b/src/core/hle/service/am/storage.h @@ -7,36 +7,25 @@ namespace Service::AM { -class IStorageImpl { -public: - virtual ~IStorageImpl(); - virtual std::vector& GetData() = 0; - virtual const std::vector& GetData() const = 0; - virtual std::size_t GetSize() const = 0; -}; +class LibraryAppletStorage; class IStorage final : public ServiceFramework { public: + explicit IStorage(Core::System& system_, std::shared_ptr impl_); explicit IStorage(Core::System& system_, std::vector&& buffer); ~IStorage() override; - std::vector& GetData() { - return impl->GetData(); + std::shared_ptr GetImpl() const { + return impl; } - const std::vector& GetData() const { - return impl->GetData(); - } - - std::size_t GetSize() const { - return impl->GetSize(); - } + std::vector GetData() const; private: - void Register(); void Open(HLERequestContext& ctx); + void OpenTransferStorage(HLERequestContext& ctx); - std::shared_ptr impl; + const std::shared_ptr impl; }; } // namespace Service::AM diff --git a/src/core/hle/service/am/storage_accessor.cpp b/src/core/hle/service/am/storage_accessor.cpp index 7d8c82de3..a1184b065 100644 --- a/src/core/hle/service/am/storage_accessor.cpp +++ b/src/core/hle/service/am/storage_accessor.cpp @@ -1,21 +1,22 @@ // SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later +#include "core/hle/kernel/k_transfer_memory.h" #include "core/hle/service/am/am_results.h" +#include "core/hle/service/am/library_applet_storage.h" #include "core/hle/service/am/storage_accessor.h" #include "core/hle/service/ipc_helpers.h" namespace Service::AM { -IStorageAccessor::IStorageAccessor(Core::System& system_, IStorage& backing_) - : ServiceFramework{system_, "IStorageAccessor"}, backing{backing_} { - // clang-format off - static const FunctionInfo functions[] = { - {0, &IStorageAccessor::GetSize, "GetSize"}, - {10, &IStorageAccessor::Write, "Write"}, - {11, &IStorageAccessor::Read, "Read"}, - }; - // clang-format on +IStorageAccessor::IStorageAccessor(Core::System& system_, + std::shared_ptr impl_) + : ServiceFramework{system_, "IStorageAccessor"}, impl{std::move(impl_)} { + static const FunctionInfo functions[] = { + {0, &IStorageAccessor::GetSize, "GetSize"}, + {10, &IStorageAccessor::Write, "Write"}, + {11, &IStorageAccessor::Read, "Read"}, + }; RegisterHandlers(functions); } @@ -28,55 +29,62 @@ void IStorageAccessor::GetSize(HLERequestContext& ctx) { IPC::ResponseBuilder rb{ctx, 4}; rb.Push(ResultSuccess); - rb.Push(static_cast(backing.GetSize())); + rb.Push(impl->GetSize()); } void IStorageAccessor::Write(HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; - const u64 offset{rp.Pop()}; + const s64 offset{rp.Pop()}; const auto data{ctx.ReadBuffer()}; - const std::size_t size{std::min(data.size(), backing.GetSize() - offset)}; + LOG_DEBUG(Service_AM, "called, offset={}, size={}", offset, data.size()); - LOG_DEBUG(Service_AM, "called, offset={}, size={}", offset, size); - - if (offset > backing.GetSize()) { - LOG_ERROR(Service_AM, - "offset is out of bounds, backing_buffer_sz={}, data_size={}, offset={}", - backing.GetSize(), size, offset); - - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(AM::ResultInvalidOffset); - return; - } - - std::memcpy(backing.GetData().data() + offset, data.data(), size); + const auto res{impl->Write(offset, data.data(), data.size())}; IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(ResultSuccess); + rb.Push(res); } void IStorageAccessor::Read(HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; - const u64 offset{rp.Pop()}; - const std::size_t size{std::min(ctx.GetWriteBufferSize(), backing.GetSize() - offset)}; + const s64 offset{rp.Pop()}; + std::vector data(ctx.GetWriteBufferSize()); - LOG_DEBUG(Service_AM, "called, offset={}, size={}", offset, size); + LOG_DEBUG(Service_AM, "called, offset={}, size={}", offset, data.size()); - if (offset > backing.GetSize()) { - LOG_ERROR(Service_AM, "offset is out of bounds, backing_buffer_sz={}, size={}, offset={}", - backing.GetSize(), size, offset); + const auto res{impl->Read(offset, data.data(), data.size())}; - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(AM::ResultInvalidOffset); - return; - } - - ctx.WriteBuffer(backing.GetData().data() + offset, size); + ctx.WriteBuffer(data); IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(res); +} + +ITransferStorageAccessor::ITransferStorageAccessor(Core::System& system_, + std::shared_ptr impl_) + : ServiceFramework{system_, "ITransferStorageAccessor"}, impl{std::move(impl_)} { + static const FunctionInfo functions[] = { + {0, &ITransferStorageAccessor::GetSize, "GetSize"}, + {1, &ITransferStorageAccessor::GetHandle, "GetHandle"}, + }; + + RegisterHandlers(functions); +} + +ITransferStorageAccessor::~ITransferStorageAccessor() = default; + +void ITransferStorageAccessor::GetSize(HLERequestContext& ctx) { + IPC::ResponseBuilder rb{ctx, 4}; rb.Push(ResultSuccess); + rb.Push(impl->GetSize()); +} + +void ITransferStorageAccessor::GetHandle(HLERequestContext& ctx) { + IPC::ResponseBuilder rb{ctx, 4, 1}; + rb.Push(ResultSuccess); + rb.Push(impl->GetSize()); + rb.PushCopyObjects(impl->GetHandle()); } } // namespace Service::AM diff --git a/src/core/hle/service/am/storage_accessor.h b/src/core/hle/service/am/storage_accessor.h index 8648bfc13..b9aa85a66 100644 --- a/src/core/hle/service/am/storage_accessor.h +++ b/src/core/hle/service/am/storage_accessor.h @@ -10,7 +10,7 @@ namespace Service::AM { class IStorageAccessor final : public ServiceFramework { public: - explicit IStorageAccessor(Core::System& system_, IStorage& backing_); + explicit IStorageAccessor(Core::System& system_, std::shared_ptr impl_); ~IStorageAccessor() override; private: @@ -18,7 +18,20 @@ private: void Write(HLERequestContext& ctx); void Read(HLERequestContext& ctx); - IStorage& backing; + const std::shared_ptr impl; +}; + +class ITransferStorageAccessor final : public ServiceFramework { +public: + explicit ITransferStorageAccessor(Core::System& system_, + std::shared_ptr impl_); + ~ITransferStorageAccessor() override; + +private: + void GetSize(HLERequestContext& ctx); + void GetHandle(HLERequestContext& ctx); + + const std::shared_ptr impl; }; } // namespace Service::AM