diff --git a/libstratosphere/include/stratosphere/fssrv.hpp b/libstratosphere/include/stratosphere/fssrv.hpp index c668a2b8..7fcc318e 100644 --- a/libstratosphere/include/stratosphere/fssrv.hpp +++ b/libstratosphere/include/stratosphere/fssrv.hpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include diff --git a/libstratosphere/include/stratosphere/fssrv/fssrv_access_control.hpp b/libstratosphere/include/stratosphere/fssrv/fssrv_access_control.hpp new file mode 100644 index 00000000..52623362 --- /dev/null +++ b/libstratosphere/include/stratosphere/fssrv/fssrv_access_control.hpp @@ -0,0 +1,23 @@ +/* + * 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 . + */ +#pragma once +#include + +namespace ams::fssrv { + + void SetDebugFlagEnabled(bool en); + +} diff --git a/libstratosphere/include/stratosphere/fssystem.hpp b/libstratosphere/include/stratosphere/fssystem.hpp index b86c9470..cb98f821 100644 --- a/libstratosphere/include/stratosphere/fssystem.hpp +++ b/libstratosphere/include/stratosphere/fssystem.hpp @@ -41,6 +41,7 @@ #include #include #include +#include #include #include #include diff --git a/libstratosphere/include/stratosphere/fssystem/fssystem_service_context.hpp b/libstratosphere/include/stratosphere/fssystem/fssystem_service_context.hpp new file mode 100644 index 00000000..bc17f4e0 --- /dev/null +++ b/libstratosphere/include/stratosphere/fssystem/fssystem_service_context.hpp @@ -0,0 +1,53 @@ +/* + * 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 . + */ +#pragma once +#include +#include + +namespace ams::fssystem { + + class ServiceContext { + private: + struct DeferredProcessContextForDeviceError { + u64 process_id; + bool is_process_deferred; + bool is_invoke_deferral_requested; + }; + + struct DeferredProcessContextForPriority { + int session_type; + bool is_process_deferred; + bool is_acquired; + }; + private: + fs::PriorityRaw m_priority; + DeferredProcessContextForDeviceError m_deferred_process_context_for_device_error; + DeferredProcessContextForPriority m_deferred_process_context_for_priority; + int m_storage_flag; + void *m_request_hook_context; + bool m_enable_count_failed_ideal_pooled_buffer_allocations; + public: + ServiceContext() : m_priority(fs::PriorityRaw_Normal), m_storage_flag(0), m_request_hook_context(nullptr), m_enable_count_failed_ideal_pooled_buffer_allocations(false) { + /* ... */ + } + }; + + void RegisterServiceContext(ServiceContext *context); + void UnregisterServiceContext(); + + ServiceContext *GetServiceContext(); + +} diff --git a/libstratosphere/source/fssrv/fssrv_access_control.cpp b/libstratosphere/source/fssrv/fssrv_access_control.cpp new file mode 100644 index 00000000..fbb64a6e --- /dev/null +++ b/libstratosphere/source/fssrv/fssrv_access_control.cpp @@ -0,0 +1,31 @@ +/* + * 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::fssrv { + + namespace { + + constinit bool g_is_debug_flag_enabled = false; + + } + + void SetDebugFlagEnabled(bool en) { + /* Set global debug flag. */ + g_is_debug_flag_enabled = en; + } + +} diff --git a/libstratosphere/source/fssystem/fssystem_file_system_proxy_api.cpp b/libstratosphere/source/fssystem/fssystem_file_system_proxy_api.cpp index 6a9af026..ebee5e05 100644 --- a/libstratosphere/source/fssystem/fssystem_file_system_proxy_api.cpp +++ b/libstratosphere/source/fssystem/fssystem_file_system_proxy_api.cpp @@ -22,6 +22,8 @@ namespace ams::fssystem { namespace { + /* TODO: Heap sizes need to match FS, when this is FS in master rather than ams.mitm. */ + /* Official FS has a 4.5 MB exp heap, a 6 MB buffer pool, an 8 MB device buffer manager heap, and a 14 MB buffer manager heap. */ /* We don't need so much memory for ams.mitm (as we're servicing a much more limited context). */ /* We'll give ourselves a 1.5 MB exp heap, a 1 MB buffer pool, a 1 MB device buffer manager heap, and a 1 MB buffer manager heap. */ @@ -68,6 +70,7 @@ namespace ams::fssystem { alignas(os::MemoryPageSize) u8 g_device_buffer[DeviceBufferSize]; alignas(os::MemoryPageSize) u8 g_buffer_pool[BufferPoolSize]; + util::TypedStorage g_buffer_allocator; util::TypedStorage g_allocator; @@ -87,14 +90,23 @@ namespace ams::fssystem { } void InitializeForFileSystemProxy() { - /* TODO FS-REIMPL: fssystem::RegisterServiceContext */ + /* TODO FS-REIMPL: Setup MainThreadStackUsageReporter. */ - /* TODO FS-REIMPL: spl::InitializeForFs(); */ + /* Register service context for main thread. */ + fssystem::ServiceContext context; + fssystem::RegisterServiceContext(std::addressof(context)); + + /* Initialize spl library. */ + spl::InitializeForFs(); + /* TODO FS-REIMPL: spl::SetIsAvailableAccessKeyHandler(fssrv::IsAvailableAccessKey) */ /* Determine whether we're prod or dev. */ bool is_prod = !spl::IsDevelopment(); bool is_development_function_enabled = spl::IsDevelopmentFunctionEnabled(); + /* Set debug flags. */ + fssrv::SetDebugFlagEnabled(is_development_function_enabled); + /* Setup our crypto configuration. */ SetUpKekAccessKeys(is_prod); @@ -106,6 +118,7 @@ namespace ams::fssystem { util::ConstructAt(g_allocator, GetPointer(g_buffer_allocator)); /* Set allocators. */ + /* TODO FS-REIMPL: sf::SetGlobalDefaultMemoryResource() */ fs::SetAllocator(AllocateForFileSystemProxy, DeallocateForFileSystemProxy); fssystem::InitializeAllocator(AllocateForFileSystemProxy, DeallocateForFileSystemProxy); @@ -114,11 +127,16 @@ namespace ams::fssystem { util::ConstructAt(g_buffer_manager); GetReference(g_buffer_manager).Initialize(MaxCacheCount, reinterpret_cast(g_buffer_manager_heap), BufferManagerHeapSize, BlockSize); - /* TODO FS-REIMPL: Memory Report Creators, fssrv::SetMemoryReportCreator */ + /* TODO FS-REIMPL: os::AllocateMemoryBlock(...); */ + /* TODO FS-REIMPL: fssrv::storage::CreateDeviceAddressSpace(...); */ + fssystem::InitializeBufferPool(reinterpret_cast(g_device_buffer), DeviceBufferSize); - /* TODO FS-REIMPL: Create Pooled Threads, fssystem::RegisterThreadPool. */ + /* TODO FS-REIMPL: Create Pooled Threads/Stack Usage Reporter, fssystem::RegisterThreadPool. */ + + /* TODO FS-REIMPL: fssrv::GetFileSystemProxyServices(), some service creation. */ /* Initialize fs creators. */ + /* TODO FS-REIMPL: Revise for accuracy. */ util::ConstructAt(g_rom_fs_creator, GetPointer(g_allocator)); util::ConstructAt(g_partition_fs_creator); util::ConstructAt(g_storage_on_nca_creator, GetPointer(g_allocator), *GetNcaCryptoConfiguration(is_prod), is_prod, GetPointer(g_buffer_manager)); @@ -131,18 +149,33 @@ namespace ams::fssystem { .storage_on_nca_creator = GetPointer(g_storage_on_nca_creator), }; + /* TODO FS-REIMPL: Revise above for latest firmware, all the new Services creation. */ + + /* TODO FS-REIMPL: Memory Report Creators, fssrv::SetMemoryReportCreator */ + /* TODO FS-REIMPL: Sd Card detection, speed emulation. */ /* Initialize fssrv. TODO FS-REIMPL: More arguments, more actions taken. */ fssrv::InitializeForFileSystemProxy(std::addressof(g_fs_creator_interfaces), GetPointer(g_buffer_manager), is_development_function_enabled); + /* TODO FS-REIMPL: GetFileSystemProxyServiceObject(), set current process, initialize global service object. */ + /* Disable auto-abort in fs library code. */ /* TODO: fs::SetEnabledAutoAbort(false); */ /* TODO FS-REIMPL: Initialize fsp server. */ - /* NOTE: This is done in fsp server init, normally. */ - fssystem::InitializeBufferPool(reinterpret_cast(g_device_buffer), DeviceBufferSize); + /* TODO FS-REIMPL: Cleanup calls. */ + + /* TODO FS-REIMPL: Spawn worker threads. */ + + /* TODO FS-REIMPL: Set mmc devices ready. */ + + /* TODO FS-REIMPL: fssrv::LoopPmEventServer(...); */ + + /* TODO FS-REIMPL: Wait/destroy threads. */ + + /* TODO FS-REIMPL: spl::Finalize(); */ } const ::ams::fssrv::fscreator::FileSystemCreatorInterfaces *GetFileSystemCreatorInterfaces() { diff --git a/libstratosphere/source/fssystem/fssystem_service_context.cpp b/libstratosphere/source/fssystem/fssystem_service_context.cpp new file mode 100644 index 00000000..9255ee35 --- /dev/null +++ b/libstratosphere/source/fssystem/fssystem_service_context.cpp @@ -0,0 +1,48 @@ +/* + * 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::fssystem { + + namespace { + + os::SdkThreadLocalStorage g_tls_service_context; + + } + + void RegisterServiceContext(ServiceContext *context) { + /* Check pre-conditions. */ + AMS_ASSERT(context != nullptr); + AMS_ASSERT(g_tls_service_context.GetValue() == 0); + + /* Register context. */ + g_tls_service_context.SetValue(reinterpret_cast(context)); + } + + void UnregisterServiceContext() { + /* Unregister context. */ + g_tls_service_context.SetValue(0); + } + + ServiceContext *GetServiceContext() { + /* Get context. */ + auto * const context = reinterpret_cast(g_tls_service_context.GetValue()); + AMS_ASSERT(context != nullptr); + + return context; + } + +}