fssrv: skeleton server manager setup/registration

This commit is contained in:
Michael Scire 2021-12-06 17:06:47 -08:00 committed by SciresM
parent 0e08aa0a31
commit ace409ccec
14 changed files with 426 additions and 12 deletions

View file

@ -0,0 +1,83 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stratosphere.hpp>
namespace ams::fssrv {
using NotifyProcessDeferredFunction = void (*)(u64 process_id);
struct DeferredProcessEntryForDeviceError : public util::IntrusiveListBaseNode<DeferredProcessEntryForDeviceError>, public fs::impl::Newable {
os::MultiWaitHolderType *process_holder;
u64 process_id;
};
class DeferredProcessQueueForDeviceError {
NON_COPYABLE(DeferredProcessQueueForDeviceError);
NON_MOVEABLE(DeferredProcessQueueForDeviceError);
private:
using EntryList = util::IntrusiveListBaseTraits<DeferredProcessEntryForDeviceError>::ListType;
private:
EntryList m_list{};
os::SdkMutex m_mutex{};
public:
constexpr DeferredProcessQueueForDeviceError() = default;
};
class DeferredProcessEntryForPriority : public util::IntrusiveListBaseNode<DeferredProcessEntryForPriority>, public fs::impl::Newable {
private:
os::MultiWaitHolderType *m_process_holder;
FileSystemProxyServerSessionType m_session_type;
};
class DeferredProcessQueueForPriority {
NON_COPYABLE(DeferredProcessQueueForPriority);
NON_MOVEABLE(DeferredProcessQueueForPriority);
private:
using EntryList = util::IntrusiveListBaseTraits<DeferredProcessEntryForPriority>::ListType;
private:
EntryList m_list{};
os::SdkRecursiveMutex m_mutex{};
os::SdkConditionVariable m_cv{};
public:
constexpr DeferredProcessQueueForPriority() = default;
};
template<typename ServerManager, NotifyProcessDeferredFunction NotifyProcessDeferred>
class DeferredProcessManager {
NON_COPYABLE(DeferredProcessManager);
NON_MOVEABLE(DeferredProcessManager);
private:
DeferredProcessQueueForDeviceError m_queue_for_device_error{};
os::SdkMutex m_invoke_mutex_for_device_error{};
DeferredProcessQueueForPriority m_queue_for_priority{};
std::atomic_bool m_is_invoke_deferred_process_event_linked{};
os::EventType m_invoke_event{};
os::MultiWaitHolderType m_invoke_event_holder{};
bool m_initialized{false};
public:
constexpr DeferredProcessManager() = default;
void Initialize() {
/* Check pre-conditions. */
AMS_ASSERT(!m_initialized);
os::InitializeEvent(std::addressof(m_invoke_event), false, os::EventClearMode_ManualClear);
os::InitializeMultiWaitHolder(std::addressof(m_invoke_event_holder), std::addressof(m_invoke_event));
m_initialized = true;
}
};
}

View file

@ -14,12 +14,123 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
#include <stratosphere/fs/impl/fs_service_name.hpp>
#include "fssrv_deferred_process_manager.hpp"
namespace ams::fssrv {
namespace {
struct FileSystemProxyServerOptions {
static constexpr size_t PointerBufferSize = 0x800;
static constexpr size_t MaxDomains = 0x40;
static constexpr size_t MaxDomainObjects = 0x4000;
static constexpr bool CanDeferInvokeRequest = true;
static constexpr bool CanManageMitmServers = false;
};
enum PortIndex {
PortIndex_FileSystemProxy,
PortIndex_ProgramRegistry,
PortIndex_FileSystemProxyForLoader,
PortIndex_Count,
};
constexpr size_t FileSystemProxyMaxSessions = 59;
constexpr size_t ProgramRegistryMaxSessions = 1;
constexpr size_t FileSystemProxyForLoaderMaxSessions = 1;
constexpr size_t NumSessions = FileSystemProxyMaxSessions + ProgramRegistryMaxSessions + FileSystemProxyForLoaderMaxSessions;
constinit os::SemaphoreType g_semaphore_for_file_system_proxy_for_loader;
constinit os::SemaphoreType g_semaphore_for_program_registry;
class FileSystemProxyServerManager final : public ams::sf::hipc::ServerManager<PortIndex_Count, FileSystemProxyServerOptions, NumSessions> {
private:
virtual ams::Result OnNeedsToAccept(int port_index, Server *server) override {
switch (port_index) {
case PortIndex_FileSystemProxy:
{
return this->AcceptImpl(server, impl::GetFileSystemProxyServiceObject());
}
break;
case PortIndex_ProgramRegistry:
{
if (os::TryAcquireSemaphore(std::addressof(g_semaphore_for_program_registry))) {
auto sema_guard = SCOPE_GUARD { os::ReleaseSemaphore(std::addressof(g_semaphore_for_program_registry)); };
R_TRY(this->AcceptImpl(server, impl::GetProgramRegistryServiceObject()));
sema_guard.Cancel();
} else {
R_TRY(this->AcceptImpl(server, impl::GetInvalidProgramRegistryServiceObject()));
}
return ResultSuccess();
}
break;
case PortIndex_FileSystemProxyForLoader:
{
if (os::TryAcquireSemaphore(std::addressof(g_semaphore_for_file_system_proxy_for_loader))) {
auto sema_guard = SCOPE_GUARD { os::ReleaseSemaphore(std::addressof(g_semaphore_for_file_system_proxy_for_loader)); };
R_TRY(this->AcceptImpl(server, impl::GetFileSystemProxyForLoaderServiceObject()));
sema_guard.Cancel();
} else {
R_TRY(this->AcceptImpl(server, impl::GetInvalidFileSystemProxyForLoaderServiceObject()));
}
return ResultSuccess();
}
break;
AMS_UNREACHABLE_DEFAULT_CASE();
}
}
};
constinit util::TypedStorage<FileSystemProxyServerManager> g_server_manager_storage;
constinit FileSystemProxyServerManager *g_server_manager = nullptr;
constinit os::BarrierType g_server_loop_barrier;
constinit os::EventType g_resume_wait_event;
constinit bool g_is_suspended = false;
void TemporaryNotifyProcessDeferred(u64) { /* TODO */ }
constinit DeferredProcessManager<FileSystemProxyServerManager, TemporaryNotifyProcessDeferred> g_deferred_process_manager;
}
void InitializeForFileSystemProxy(fscreator::FileSystemCreatorInterfaces *fs_creator_interfaces, fssystem::IBufferManager *buffer_manager, bool is_development_function_enabled) {
/* TODO FS-REIMPL */
AMS_UNUSED(fs_creator_interfaces, buffer_manager, is_development_function_enabled);
}
void InitializeFileSystemProxyServer(int threads) {
/* Initialize synchronization primitives. */
os::InitializeBarrier(std::addressof(g_server_loop_barrier), threads + 1);
os::InitializeEvent(std::addressof(g_resume_wait_event), false, os::EventClearMode_ManualClear);
g_is_suspended = false;
os::InitializeSemaphore(std::addressof(g_semaphore_for_file_system_proxy_for_loader), 1, 1);
os::InitializeSemaphore(std::addressof(g_semaphore_for_program_registry), 1, 1);
/* Initialize deferred process manager. */
g_deferred_process_manager.Initialize();
/* Create the server and register our services. */
AMS_ASSERT(g_server_manager == nullptr);
g_server_manager = util::ConstructAt(g_server_manager_storage);
/* TODO: Manager handler. */
R_ABORT_UNLESS(g_server_manager->RegisterServer(PortIndex_FileSystemProxy, fs::impl::FileSystemProxyServiceName, FileSystemProxyMaxSessions));
R_ABORT_UNLESS(g_server_manager->RegisterServer(PortIndex_ProgramRegistry, fs::impl::ProgramRegistryServiceName, ProgramRegistryMaxSessions));
R_ABORT_UNLESS(g_server_manager->RegisterServer(PortIndex_FileSystemProxyForLoader, fs::impl::FileSystemProxyForLoaderServiceName, FileSystemProxyForLoaderMaxSessions));
/* Enable processing on server. */
g_server_manager->ResumeProcessing();
}
}

View file

@ -22,6 +22,8 @@ namespace ams::fssystem {
namespace {
constexpr inline auto FileSystemProxyServerThreadCount = 5;
/* 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. */
@ -161,9 +163,10 @@ namespace ams::fssystem {
/* TODO FS-REIMPL: GetFileSystemProxyServiceObject(), set current process, initialize global service object. */
/* Disable auto-abort in fs library code. */
/* TODO: fs::SetEnabledAutoAbort(false); */
fs::SetEnabledAutoAbort(false);
/* TODO FS-REIMPL: Initialize fsp server. */
/* Initialize fsp server. */
fssrv::InitializeFileSystemProxyServer(fssrv::FileSystemProxyServerActiveSessionCount);
/* TODO FS-REIMPL: Cleanup calls. */