2021-03-17 00:13:30 +00:00
|
|
|
/*
|
2021-10-04 20:59:10 +01:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2021-03-17 00:13:30 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
#include <stratosphere.hpp>
|
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
namespace ams {
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
namespace cs {
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
namespace {
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
alignas(os::ThreadStackAlignment) constinit u8 g_shell_stack[4_KB];
|
|
|
|
alignas(os::ThreadStackAlignment) constinit u8 g_runner_stack[4_KB];
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
alignas(os::MemoryPageSize) constinit u8 g_heap_memory[32_KB];
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
alignas(0x40) constinit u8 g_htcs_buffer[2_KB];
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
constinit os::SdkMutex g_heap_mutex;
|
|
|
|
constinit lmem::HeapHandle g_heap_handle;
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
void *Allocate(size_t size) {
|
|
|
|
std::scoped_lock lk(g_heap_mutex);
|
|
|
|
void *mem = lmem::AllocateFromExpHeap(g_heap_handle, size);
|
|
|
|
return mem;
|
|
|
|
}
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
void Deallocate(void *p, size_t size) {
|
|
|
|
AMS_UNUSED(size);
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
std::scoped_lock lk(g_heap_mutex);
|
|
|
|
lmem::FreeToExpHeap(g_heap_handle, p);
|
|
|
|
}
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
void InitializeHeap() {
|
|
|
|
std::scoped_lock lk(g_heap_mutex);
|
|
|
|
g_heap_handle = lmem::CreateExpHeap(g_heap_memory, sizeof(g_heap_memory), lmem::CreateOption_None);
|
|
|
|
}
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
}
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
namespace {
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
constinit ::ams::cs::CommandProcessor g_command_processor;
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
constinit ::ams::scs::ShellServer g_shell_server;
|
|
|
|
constinit ::ams::scs::ShellServer g_runner_server;
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
constinit sf::UnmanagedServiceObject<htc::tenv::IServiceManager, htc::tenv::ServiceManager> g_tenv_service_manager;
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
void InitializeCommandProcessor() {
|
|
|
|
g_command_processor.Initialize();
|
|
|
|
}
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
void InitializeShellServers() {
|
|
|
|
g_shell_server.Initialize("iywys@$cs", g_shell_stack, sizeof(g_shell_stack), std::addressof(g_command_processor));
|
|
|
|
g_shell_server.Start();
|
2021-10-07 07:22:54 +01:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
g_runner_server.Initialize("iywys@$csForRunnerTools", g_runner_stack, sizeof(g_runner_stack), std::addressof(g_command_processor));
|
|
|
|
g_runner_server.Start();
|
|
|
|
}
|
2021-03-17 00:13:30 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
namespace init {
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
void InitializeSystemModule() {
|
|
|
|
/* Initialize heap. */
|
|
|
|
cs::InitializeHeap();
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
/* Initialize our connection to sm. */
|
|
|
|
R_ABORT_UNLESS(sm::Initialize());
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
/* Initialize fs. */
|
|
|
|
fs::InitializeForSystem();
|
|
|
|
fs::SetAllocator(cs::Allocate, cs::Deallocate);
|
|
|
|
fs::SetEnabledAutoAbort(false);
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
/* Initialize other services we need. */
|
|
|
|
lr::Initialize();
|
|
|
|
R_ABORT_UNLESS(ldr::InitializeForShell());
|
|
|
|
R_ABORT_UNLESS(pgl::Initialize());
|
|
|
|
R_ABORT_UNLESS(setsysInitialize());
|
2021-04-14 07:58:10 +01:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
/* Verify that we can sanely execute. */
|
|
|
|
ams::CheckApiVersion();
|
|
|
|
}
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
void FinalizeSystemModule() { /* ... */ }
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
void Startup() { /* ... */ }
|
2021-03-17 00:13:30 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
void Main() {
|
|
|
|
/* Set thread name. */
|
|
|
|
os::SetThreadNamePointer(os::GetCurrentThread(), AMS_GET_SYSTEM_THREAD_NAME(cs, Main));
|
|
|
|
AMS_ASSERT(os::GetThreadPriority(os::GetCurrentThread()) == AMS_GET_SYSTEM_THREAD_PRIORITY(cs, Main));
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
/* Initialize htcs. */
|
|
|
|
constexpr auto HtcsSocketCountMax = 6;
|
|
|
|
const size_t buffer_size = htcs::GetWorkingMemorySize(2 * HtcsSocketCountMax);
|
|
|
|
AMS_ABORT_UNLESS(sizeof(cs::g_htcs_buffer) >= buffer_size);
|
|
|
|
htcs::InitializeForSystem(cs::g_htcs_buffer, buffer_size, HtcsSocketCountMax);
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
/* Initialize audio server. */
|
|
|
|
cs::InitializeAudioServer();
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
/* Initialize remote video server. */
|
|
|
|
cs::InitializeRemoteVideoServer();
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
/* Initialize hid server. */
|
|
|
|
cs::InitializeHidServer();
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
/* Initialize target io server. */
|
|
|
|
cs::InitializeTargetIoServer();
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
/* Initialize command processor. */
|
|
|
|
cs::InitializeCommandProcessor();
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
/* Setup scs. */
|
|
|
|
scs::InitializeShell();
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
/* Setup target environment service. */
|
|
|
|
scs::InitializeTenvServiceManager();
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
/* Initialize the shell servers. */
|
|
|
|
cs::InitializeShellServers();
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
/* Register htc:tenv. */
|
|
|
|
R_ABORT_UNLESS(scs::GetServerManager()->RegisterObjectForServer(cs::g_tenv_service_manager.GetShared(), htc::tenv::ServiceName, scs::SessionCount[scs::Port_HtcTenv]));
|
2021-03-17 00:13:30 +00:00
|
|
|
|
2021-10-08 01:44:54 +01:00
|
|
|
/* Start the scs ipc server. */
|
|
|
|
scs::StartServer();
|
2021-03-17 00:13:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|