From 2905319d462fa8ad66da05550d5f77c434c17b1e Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Thu, 7 Oct 2021 17:44:54 -0700 Subject: [PATCH] strat: use ams::Main() instead of main(argc, argv) --- config/templates/exosphere.mk | 2 +- config/templates/mesosphere.mk | 2 +- config/templates/stratosphere.mk | 11 +- libstratosphere/Makefile | 3 + libstratosphere/include/stratosphere.hpp | 1 + libstratosphere/include/stratosphere/fs.hpp | 1 + .../include/stratosphere/fs/fs_api.hpp | 24 +++ libstratosphere/include/stratosphere/init.hpp | 18 ++ .../include/stratosphere/init/init_malloc.hpp | 32 ++++ libstratosphere/include/stratosphere/os.hpp | 1 + .../stratosphere/os/os_argument.hpp} | 11 +- .../source/erpt/srv/erpt_srv_main.cpp | 2 + libstratosphere/source/fs/fs_api.cpp | 38 ++++ .../init/init_libnx_shim.os.horizon.cpp | 108 +++++++++++ libstratosphere/source/init/init_malloc.cpp | 147 +++++++++++++++ .../source/init/init_operator_new.cpp | 48 +++++ .../source/init/init_system_module.cpp | 34 ++++ .../source/lm/srv/lm_flush_thread.cpp | 5 +- .../heap/mem_impl_heap_tls_heap_central.hpp | 6 +- libstratosphere/source/os/os_argument.cpp | 51 ++++++ .../os/os_stratosphere_api.os.horizon.cpp | 172 ++++++++++++++++++ 21 files changed, 703 insertions(+), 14 deletions(-) create mode 100644 libstratosphere/include/stratosphere/fs/fs_api.hpp create mode 100644 libstratosphere/include/stratosphere/init.hpp create mode 100644 libstratosphere/include/stratosphere/init/init_malloc.hpp rename libstratosphere/{source/os/os_stratosphere_api.cpp => include/stratosphere/os/os_argument.hpp} (72%) create mode 100644 libstratosphere/source/fs/fs_api.cpp create mode 100644 libstratosphere/source/init/init_libnx_shim.os.horizon.cpp create mode 100644 libstratosphere/source/init/init_malloc.cpp create mode 100644 libstratosphere/source/init/init_operator_new.cpp create mode 100644 libstratosphere/source/init/init_system_module.cpp create mode 100644 libstratosphere/source/os/os_argument.cpp create mode 100644 libstratosphere/source/os/os_stratosphere_api.os.horizon.cpp diff --git a/config/templates/exosphere.mk b/config/templates/exosphere.mk index bb994cb8..47436386 100644 --- a/config/templates/exosphere.mk +++ b/config/templates/exosphere.mk @@ -26,7 +26,7 @@ CXXFLAGS := $(CFLAGS) $(ATMOSPHERE_CXXFLAGS) -fno-use-cxa-atexit ASFLAGS := $(ATMOSPHERE_ASFLAGS) $(SETTINGS) endif -export LDFLAGS = -specs=$(TOPDIR)/$(notdir $(TOPDIR)).specs -fno-asynchronous-unwind-tables -fno-unwind-tables -fno-exceptions -fno-rtti -fno-use-cxa-atexit -nostdlib -nostartfiles -g -gdwarf-4 $(SETTINGS) -Wl,-Map,$(notdir $*.map) -Wl,-z,relro,-z,now +export LDFLAGS = -specs=$(TOPDIR)/$(notdir $(TOPDIR)).specs -fno-asynchronous-unwind-tables -fno-unwind-tables -fno-exceptions -fno-rtti -fno-use-cxa-atexit -nostdlib -nostartfiles -g -gdwarf-4 $(CXXFLAGS) -Wl,-Map,$(notdir $*.map) -Wl,-z,relro,-z,now export CXXWRAPS := -Wl,--wrap,__cxa_pure_virtual \ -Wl,--wrap,__cxa_throw \ diff --git a/config/templates/mesosphere.mk b/config/templates/mesosphere.mk index c55a23db..84be7325 100644 --- a/config/templates/mesosphere.mk +++ b/config/templates/mesosphere.mk @@ -12,7 +12,7 @@ export CFLAGS := $(ATMOSPHERE_CFLAGS) $(SETTINGS) $(DEFINES) $(INCLUDE) export CXXFLAGS := $(CFLAGS) $(ATMOSPHERE_CXXFLAGS) -fno-use-cxa-atexit export ASFLAGS := $(ATMOSPHERE_ASFLAGS) $(SETTINGS) $(DEFINES) $(INCLUDE) -export LDFLAGS = -specs=$(TOPDIR)/$(notdir $(TOPDIR)).specs -fno-asynchronous-unwind-tables -fno-unwind-tables -nostdlib -nostartfiles -g -gdwarf-4 $(SETTINGS) -Wl,-Map,$(notdir $*.map) -Wl,-z,relro,-z,now +export LDFLAGS = -specs=$(TOPDIR)/$(notdir $(TOPDIR)).specs -fno-asynchronous-unwind-tables -fno-unwind-tables -nostdlib -nostartfiles -g -gdwarf-4 $(CXXFLAGS) -Wl,-Map,$(notdir $*.map) -Wl,-z,relro,-z,now export CXXWRAPS := -Wl,--wrap,__cxa_pure_virtual \ -Wl,--wrap,__cxa_throw \ diff --git a/config/templates/stratosphere.mk b/config/templates/stratosphere.mk index aff1007b..0753e3d0 100644 --- a/config/templates/stratosphere.mk +++ b/config/templates/stratosphere.mk @@ -21,6 +21,15 @@ export CFLAGS = $(ATMOSPHERE_CFLAGS) $(SETTINGS) $(DEFINES) $(INCLUDE) export CXXFLAGS = $(CFLAGS) $(ATMOSPHERE_CXXFLAGS) export ASFLAGS = $(ATMOSPHERE_ASFLAGS) $(SETTINGS) $(DEFINES) +export CXXREQUIRED := -Wl,--require-defined,__libnx_initheap \ + -Wl,--require-defined,__libnx_exception_handler \ + -Wl,--require-defined,__libnx_alloc \ + -Wl,--require-defined,__libnx_aligned_alloc \ + -Wl,--require-defined,__libnx_free \ + -Wl,--require-defined,__appInit \ + -Wl,--require-defined,__appExit \ + -Wl,--require-defined,argvSetup + export CXXWRAPS := -Wl,--wrap,__cxa_pure_virtual \ -Wl,--wrap,__cxa_throw \ -Wl,--wrap,__cxa_rethrow \ @@ -37,7 +46,7 @@ export CXXWRAPS := -Wl,--wrap,__cxa_pure_virtual \ -Wl,--wrap,_ZNSt11logic_errorC2EPKc \ -Wl,--wrap,exit -export LDFLAGS = -specs=$(ATMOSPHERE_LIBRARIES_DIR)/libstratosphere/stratosphere.specs -specs=$(DEVKITPRO)/libnx/switch.specs $(SETTINGS) $(CXXWRAPS) -Wl,-Map,$(notdir $*.map) +export LDFLAGS = -specs=$(ATMOSPHERE_LIBRARIES_DIR)/libstratosphere/stratosphere.specs -specs=$(DEVKITPRO)/libnx/switch.specs $(CXXFLAGS) $(CXXWRAPS) $(CXXREQUIRED) -Wl,-Map,$(notdir $*.map) export LIBS = -lstratosphere -lnx diff --git a/libstratosphere/Makefile b/libstratosphere/Makefile index 82a45017..a01800fc 100644 --- a/libstratosphere/Makefile +++ b/libstratosphere/Makefile @@ -126,6 +126,9 @@ ams_environment_weak.o: CXXFLAGS += -fno-lto pm_info_api_weak.o: CXXFLAGS += -fno-lto hos_stratosphere_api.o: CXXFLAGS += -fno-lto +init_operator_new.o: CXXFLAGS += -fno-lto +init_libnx_shim.os.horizon.o: CXXFLAGS += -fno-lto + #--------------------------------------------------------------------------------- %_bin.h %.bin.o : %.bin #--------------------------------------------------------------------------------- diff --git a/libstratosphere/include/stratosphere.hpp b/libstratosphere/include/stratosphere.hpp index 05847e4b..3bd4ed1e 100644 --- a/libstratosphere/include/stratosphere.hpp +++ b/libstratosphere/include/stratosphere.hpp @@ -62,6 +62,7 @@ #include #include #include +#include #include #include #include diff --git a/libstratosphere/include/stratosphere/fs.hpp b/libstratosphere/include/stratosphere/fs.hpp index 03703305..7e97397f 100644 --- a/libstratosphere/include/stratosphere/fs.hpp +++ b/libstratosphere/include/stratosphere/fs.hpp @@ -62,3 +62,4 @@ #include #include #include +#include diff --git a/libstratosphere/include/stratosphere/fs/fs_api.hpp b/libstratosphere/include/stratosphere/fs/fs_api.hpp new file mode 100644 index 00000000..48243f87 --- /dev/null +++ b/libstratosphere/include/stratosphere/fs/fs_api.hpp @@ -0,0 +1,24 @@ +/* + * 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::fs { + + void InitializeForSystem(); + void InitializeWithMultiSessionForSystem(); + +} diff --git a/libstratosphere/include/stratosphere/init.hpp b/libstratosphere/include/stratosphere/init.hpp new file mode 100644 index 00000000..6860a5ca --- /dev/null +++ b/libstratosphere/include/stratosphere/init.hpp @@ -0,0 +1,18 @@ +/* + * 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 diff --git a/libstratosphere/include/stratosphere/init/init_malloc.hpp b/libstratosphere/include/stratosphere/init/init_malloc.hpp new file mode 100644 index 00000000..f1b58596 --- /dev/null +++ b/libstratosphere/include/stratosphere/init/init_malloc.hpp @@ -0,0 +1,32 @@ +/* + * 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::mem { + + class StandardAllocator; + +} + +namespace ams::init { + + void InitializeAllocator(void *address, size_t size, bool cache_enabled); + void InitializeAllocator(void *address, size_t size); + + mem::StandardAllocator *GetAllocator(); + +} diff --git a/libstratosphere/include/stratosphere/os.hpp b/libstratosphere/include/stratosphere/os.hpp index 605cbe12..c1e05ba4 100644 --- a/libstratosphere/include/stratosphere/os.hpp +++ b/libstratosphere/include/stratosphere/os.hpp @@ -52,3 +52,4 @@ #include #include #include +#include diff --git a/libstratosphere/source/os/os_stratosphere_api.cpp b/libstratosphere/include/stratosphere/os/os_argument.hpp similarity index 72% rename from libstratosphere/source/os/os_stratosphere_api.cpp rename to libstratosphere/include/stratosphere/os/os_argument.hpp index bbeac103..a43102a5 100644 --- a/libstratosphere/source/os/os_stratosphere_api.cpp +++ b/libstratosphere/include/stratosphere/os/os_argument.hpp @@ -13,14 +13,13 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -#include -#include "impl/os_resource_manager.hpp" + +#pragma once +#include namespace ams::os { - void InitializeForStratosphereInternal() { - /* Initialize the global os resource manager. */ - os::impl::ResourceManagerHolder::InitializeResourceManagerInstance(); - } + int GetHostArgc(); + char **GetHostArgv(); } diff --git a/libstratosphere/source/erpt/srv/erpt_srv_main.cpp b/libstratosphere/source/erpt/srv/erpt_srv_main.cpp index a84dc3d4..9a29d41d 100644 --- a/libstratosphere/source/erpt/srv/erpt_srv_main.cpp +++ b/libstratosphere/source/erpt/srv/erpt_srv_main.cpp @@ -75,7 +75,9 @@ namespace ams::erpt::srv { g_heap_handle = lmem::CreateExpHeap(mem, mem_size, lmem::CreateOption_ThreadSafe); AMS_ABORT_UNLESS(g_heap_handle != nullptr); + fs::InitializeForSystem(); fs::SetAllocator(Allocate, DeallocateWithSize); + fs::SetEnabledAutoAbort(false); R_ABORT_UNLESS(fs::MountSdCardErrorReportDirectoryForAtmosphere(ReportOnSdStoragePath)); diff --git a/libstratosphere/source/fs/fs_api.cpp b/libstratosphere/source/fs/fs_api.cpp new file mode 100644 index 00000000..c9d8ce72 --- /dev/null +++ b/libstratosphere/source/fs/fs_api.cpp @@ -0,0 +1,38 @@ +/* + * 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 + +extern "C" { + + extern u32 __nx_fs_num_sessions; + +} + +namespace ams::fs { + + /* TODO: FileSystemProxySessionSetting */ + + void InitializeForSystem() { + __nx_fs_num_sessions = 1; + R_ABORT_UNLESS(::fsInitialize()); + } + + void InitializeWithMultiSessionForSystem() { + __nx_fs_num_sessions = 2; + R_ABORT_UNLESS(::fsInitialize()); + } + +} diff --git a/libstratosphere/source/init/init_libnx_shim.os.horizon.cpp b/libstratosphere/source/init/init_libnx_shim.os.horizon.cpp new file mode 100644 index 00000000..5f2d2128 --- /dev/null +++ b/libstratosphere/source/init/init_libnx_shim.os.horizon.cpp @@ -0,0 +1,108 @@ +/* + * 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 + +extern "C" { + + constinit u32 __nx_fs_num_sessions = 1; + constinit u32 __nx_applet_type = AppletType_None; + + extern int __system_argc; + extern char** __system_argv; + + alignas(16) constinit u8 __nx_exception_stack[::ams::os::MemoryPageSize]; + constinit u64 __nx_exception_stack_size = sizeof(__nx_exception_stack); + +} + +namespace ams { + + namespace hos { + + void InitializeForStratosphere(); + + } + + namespace init { + + void InitializeSystemModule(); + void FinalizeSystemModule(); + + void Startup(); + + } + + void Main(); + +} + +namespace { + + constinit char *g_empty_argv = nullptr; + +} + +extern "C" void __libnx_exception_handler(ThreadExceptionDump *ctx) { + ::ams::CrashHandler(ctx); +} + +extern "C" void __libnx_initheap(void) { + /* Stratosphere system modules do not support newlib heap. */ +} + +extern "C" void __appInit(void) { + /* The very first thing all stratosphere code must do is initialize the os library. */ + ::ams::hos::InitializeForStratosphere(); +} + +extern "C" void __appExit(void) { + /* ... */ +} + +extern "C" void argvSetup(void) { + /* We don't use newlib argc/argv, so we can clear these. */ + __system_argc = 0; + __system_argv = std::addressof(g_empty_argv); +} + +extern "C" int main(int argc, char **argv) { + /* We don't use newlib argc/argv. */ + AMS_UNUSED(argc, argv); + + /* Perform remainder of logic with system module initialized. */ + { + ::ams::init::InitializeSystemModule(); + ON_SCOPE_EXIT { ::ams::init::FinalizeSystemModule(); }; + + /* Perform miscellaneous startup. */ + ::ams::init::Startup(); + + /* Invoke ams main. */ + ::ams::Main(); + } +} + +extern "C" WEAK_SYMBOL void *__libnx_alloc(size_t) { + AMS_ABORT("__libnx_alloc was called"); +} + +extern "C" WEAK_SYMBOL void *__libnx_aligned_alloc(size_t, size_t) { + AMS_ABORT("__libnx_aligned_alloc was called"); +} + +extern "C" WEAK_SYMBOL void __libnx_free(void *) { + AMS_ABORT("__libnx_free was called"); +} \ No newline at end of file diff --git a/libstratosphere/source/init/init_malloc.cpp b/libstratosphere/source/init/init_malloc.cpp new file mode 100644 index 00000000..9d051551 --- /dev/null +++ b/libstratosphere/source/init/init_malloc.cpp @@ -0,0 +1,147 @@ +/* + * 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::init { + + namespace { + + constinit void *g_malloc_region_address = nullptr; + constinit size_t g_malloc_region_size = 0; + + constinit util::TypedStorage g_malloc_allocator; + + } + + void InitializeAllocator(void *address, size_t size, bool cache_enabled) { + /* Check pre-conditions. */ + AMS_ABORT_UNLESS(g_malloc_region_size == 0); + AMS_ABORT_UNLESS(size > 0); + + /* Construct malloc allocator. */ + util::ConstructAt(g_malloc_allocator); + + /* Initialize allocator. */ + util::GetReference(g_malloc_allocator).Initialize(address, size, cache_enabled); + + /* Set malloc globals. */ + g_malloc_region_address = address; + g_malloc_region_size = size; + } + + void InitializeAllocator(void *address, size_t size) { + return InitializeAllocator(address, size, false); + } + + mem::StandardAllocator *GetAllocator() { + /* Check pre-conditions. */ + AMS_ASSERT(g_malloc_region_size > 0); + + return util::GetPointer(g_malloc_allocator); + } + +} + +extern "C" void *malloc(size_t size) { + /* We require that an allocator region exists. */ + if (::ams::init::g_malloc_region_size == 0) { + return nullptr; + } + + /* Try to allocate. */ + void *ptr = ::ams::util::GetReference(::ams::init::g_malloc_allocator).Allocate(size); + if (ptr == nullptr) { + errno = ENOMEM; + } + + return ptr; +} + +extern "C" void free(void *ptr) { + /* We require that an allocator region exists. */ + if (::ams::init::g_malloc_region_size == 0) { + return; + } + + if (ptr != nullptr) { + ::ams::util::GetReference(::ams::init::g_malloc_allocator).Free(ptr); + } +} + +extern "C" void *calloc(size_t num, size_t size) { + /* We require that an allocator region exists. */ + if (::ams::init::g_malloc_region_size == 0) { + return nullptr; + } + + /* Allocate the total needed space. */ + const size_t total = num * size; + void *ptr = std::malloc(total); + + /* Zero the memory if needed. */ + if (ptr != nullptr) { + std::memset(ptr, 0, total); + } else { + errno = ENOMEM; + } + + return ptr; +} + +extern "C" void *realloc(void *ptr, size_t new_size) { + /* We require that an allocator region exists. */ + if (::ams::init::g_malloc_region_size == 0) { + return nullptr; + } + + /* Try to reallocate. */ + void *r = ::ams::util::GetReference(::ams::init::g_malloc_allocator).Reallocate(ptr, new_size); + if (r == nullptr) { + errno = ENOMEM; + } + + return r; +} + +extern "C" void *aligned_alloc(size_t align, size_t size) { + /* We require that an allocator region exists. */ + if (::ams::init::g_malloc_region_size == 0) { + return nullptr; + } + + /* Try to allocate. */ + void *ptr = ::ams::util::GetReference(::ams::init::g_malloc_allocator).Allocate(size, align); + if (ptr == nullptr) { + errno = ENOMEM; + } + + return ptr; +} + +extern "C" size_t malloc_usable_size(void *ptr) { + /* We require that an allocator region exists. */ + if (::ams::init::g_malloc_region_size == 0) { + return 0; + } + + /* Try to get the usable size. */ + if (ptr == nullptr) { + errno = ENOMEM; + return 0; + } + + return ::ams::util::GetReference(::ams::init::g_malloc_allocator).GetSizeOf(ptr); +} diff --git a/libstratosphere/source/init/init_operator_new.cpp b/libstratosphere/source/init/init_operator_new.cpp new file mode 100644 index 00000000..d7c55c15 --- /dev/null +++ b/libstratosphere/source/init/init_operator_new.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 + +WEAK_SYMBOL void *operator new(size_t size) { + return std::malloc(size); +} + +WEAK_SYMBOL void *operator new(size_t size, const std::nothrow_t &) { + return std::malloc(size); +} + +WEAK_SYMBOL void operator delete(void *p) { + return std::free(p); +} + +WEAK_SYMBOL void operator delete(void *p, size_t) { + return std::free(p); +} + +WEAK_SYMBOL void *operator new[](size_t size) { + return std::malloc(size); +} + +WEAK_SYMBOL void *operator new[](size_t size, const std::nothrow_t &) { + return std::malloc(size); +} + +WEAK_SYMBOL void operator delete[](void *p) { + return std::free(p); +} + +WEAK_SYMBOL void operator delete[](void *p, size_t) { + return std::free(p); +} diff --git a/libstratosphere/source/init/init_system_module.cpp b/libstratosphere/source/init/init_system_module.cpp new file mode 100644 index 00000000..75ccce4a --- /dev/null +++ b/libstratosphere/source/init/init_system_module.cpp @@ -0,0 +1,34 @@ +/* + * 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::init { + + WEAK_SYMBOL void InitializeSystemModule() { + /* TODO: What should we do here, if anything? */ + /* Nintendo does nndiagStartup(); nn::diag::InitializeSystemProcessAbortObserver(); */ + } + + WEAK_SYMBOL void FinalizeSystemModule() { + /* Do nothing by default. */ + } + + WEAK_SYMBOL void Startup() { + /* TODO: What should we do here, if anything? */ + /* Nintendo determines heap size and does init::InitializeAllocator, as relevant. */ + } + +} \ No newline at end of file diff --git a/libstratosphere/source/lm/srv/lm_flush_thread.cpp b/libstratosphere/source/lm/srv/lm_flush_thread.cpp index b8c966c3..8fac0c1b 100644 --- a/libstratosphere/source/lm/srv/lm_flush_thread.cpp +++ b/libstratosphere/source/lm/srv/lm_flush_thread.cpp @@ -103,14 +103,15 @@ namespace ams::lm::srv { } void FlushThreadFunction(void *) { - /* Disable abort. */ + /* Initialize fs. */ + fs::InitializeWithMultiSessionForSystem(); fs::SetEnabledAutoAbort(false); /* Create fs heap. */ g_fs_heap_handle = lmem::CreateExpHeap(g_fs_heap, sizeof(g_fs_heap), lmem::CreateOption_None); AMS_ABORT_UNLESS(g_fs_heap_handle != nullptr); - /* Set fs allocation functions. */ + /* Set fs allocator functions. */ fs::SetAllocator(AllocateForFs, DeallocateForFs); /* Create SD card detection event notifier. */ diff --git a/libstratosphere/source/mem/impl/heap/mem_impl_heap_tls_heap_central.hpp b/libstratosphere/source/mem/impl/heap/mem_impl_heap_tls_heap_central.hpp index 288f934b..3564de76 100644 --- a/libstratosphere/source/mem/impl/heap/mem_impl_heap_tls_heap_central.hpp +++ b/libstratosphere/source/mem/impl/heap/mem_impl_heap_tls_heap_central.hpp @@ -353,7 +353,7 @@ namespace ams::mem::impl::heap { std::scoped_lock lk(this->lock); if (Span *span = GetSpanFromPointer(std::addressof(this->span_table), ptr); span != nullptr && !span->page_class) { - *out = (span->aux.large.color[0] << 0) | (span->aux.large.color[1] << 0) | (span->aux.large.color[2] << 16); + *out = (span->aux.large.color[0] << 0) | (span->aux.large.color[1] << 8) | (span->aux.large.color[2] << 16); return 0; } else { return EINVAL; @@ -387,7 +387,7 @@ namespace ams::mem::impl::heap { errno_t GetName(const void *ptr, char *dst, size_t dst_size) { std::scoped_lock lk(this->lock); if (Span *span = GetSpanFromPointer(std::addressof(this->span_table), ptr); span != nullptr && !span->page_class) { - strlcpy(dst, span->aux.large.name, dst_size); + util::Strlcpy(dst, span->aux.large.name, dst_size); return 0; } else { return EINVAL; @@ -397,7 +397,7 @@ namespace ams::mem::impl::heap { errno_t SetName(const void *ptr, const char *name) { std::scoped_lock lk(this->lock); if (Span *span = GetSpanFromPointer(std::addressof(this->span_table), ptr); span != nullptr && !span->page_class) { - strlcpy(span->aux.large.name, name, sizeof(span->aux.large.name)); + util::Strlcpy(span->aux.large.name, name, sizeof(span->aux.large.name)); return 0; } else { return EINVAL; diff --git a/libstratosphere/source/os/os_argument.cpp b/libstratosphere/source/os/os_argument.cpp new file mode 100644 index 00000000..df83598d --- /dev/null +++ b/libstratosphere/source/os/os_argument.cpp @@ -0,0 +1,51 @@ +/* + * 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::os { + + namespace { + + struct CommandLineParameter { + int argc; + char **argv; + }; + + constinit const char *g_command_line_parameter_argv[2] = { "", nullptr }; + constinit CommandLineParameter g_command_line_parameter = { + 1, + const_cast(g_command_line_parameter_argv), + }; + + } + + void SetHostArgc(int argc) { + g_command_line_parameter.argc = argc; + } + + void SetHostArgv(char **argv) { + g_command_line_parameter.argv = argv; + } + + int GetHostArgc() { + return g_command_line_parameter.argc; + } + + char **GetHostArgv() { + return g_command_line_parameter.argv; + } + +} diff --git a/libstratosphere/source/os/os_stratosphere_api.os.horizon.cpp b/libstratosphere/source/os/os_stratosphere_api.os.horizon.cpp new file mode 100644 index 00000000..7651ae88 --- /dev/null +++ b/libstratosphere/source/os/os_stratosphere_api.os.horizon.cpp @@ -0,0 +1,172 @@ +/* + * 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 +#include "impl/os_resource_manager.hpp" + +extern "C" { extern u8 __argdata__[]; } + +namespace ams::os { + + namespace { + + class MemoryArranger { + private: + uintptr_t m_address; + public: + constexpr MemoryArranger(uintptr_t address) : m_address(address) { /* ... */ } + + template + T *Arrange() { + this->Align(alignof(T)); + return static_cast(this->Arrange(sizeof(T))); + } + + void *Arrange(size_t size) { + const auto address = m_address; + m_address += size; + return reinterpret_cast(address); + } + + void Align(size_t align) { + m_address = util::AlignUp(m_address, align); + } + + char *ArrangeCharArray(size_t size) { + return reinterpret_cast(Arrange(size)); + } + }; + + bool HasArguments(uintptr_t args_region) { + /* Check that the arguments region is read-write. */ + svc::MemoryInfo mi; + svc::PageInfo pi; + + if (R_FAILED(svc::QueryMemory(std::addressof(mi), std::addressof(pi), args_region))) { + return false; + } + + return mi.permission == svc::MemoryPermission_ReadWrite; + } + + const char *SkipSpace(const char *p, const char *end) { + while (p < end && std::isspace(*p)) { ++p; } + return p; + } + + const char *GetTokenEnd(const char *p, const char *end) { + while (p < end && !std::isspace(*p)) { ++p; } + return p; + } + + const char *GetQuotedTokenEnd(const char *p, const char *end) { + while (p < end && *p != '"') { ++p; } + return p; + } + + int MakeArgv(char **out_argv_buf, char *arg_buf, const char *cmd_line, size_t cmd_line_size, int arg_max) { + /* Prepare to parse arguments. */ + auto idx = 0; + auto src = cmd_line; + auto dst = arg_buf; + const auto end = src + cmd_line_size; + + /* Parse all tokens. */ + while (true) { + /* Advance past any spaces. */ + src = SkipSpace(src, end); + if (src >= end) { + break; + } + + /* Check that we don't have too many arguments. */ + if (idx >= arg_max) { + break; + } + + /* Find the start/end of the current argument token. */ + const char *arg_end; + const char *src_next; + if (*src == '"') { + ++src; + arg_end = GetQuotedTokenEnd(src, end); + src_next = arg_end + 1; + } else { + arg_end = GetTokenEnd(src, end); + src_next = arg_end; + } + + /* Determine token size. */ + const auto arg_size = arg_end - src; + + /* Set the argv pointer. */ + out_argv_buf[idx++] = dst; + + /* Copy the argument. */ + std::memcpy(dst, src, arg_size); + dst += arg_size; + + /* Null-terminate the argument token. */ + *(dst++) = '\x00'; + + /* Advance to next token. */ + src = src_next; + } + + /* Null terminate the final token. */ + *(dst++) = '\x00'; + + /* Null terminate argv. */ + out_argv_buf[idx] = nullptr; + + return idx; + } + + + } + + void SetHostArgc(int argc); + void SetHostArgv(char **argv); + + void InitializeForStratosphereInternal() { + /* Initialize the global os resource manager. */ + os::impl::ResourceManagerHolder::InitializeResourceManagerInstance(); + + /* Setup host argc/argv as needed. */ + const uintptr_t args_region = reinterpret_cast(__argdata__); + if (HasArguments(args_region)) { + /* Create arguments memory arranger. */ + MemoryArranger arranger(args_region); + + /* Arrange. */ + const auto &header = *arranger.Arrange(); + const char *cmd_line = arranger.ArrangeCharArray(header.arguments_size); + char *arg_buf = arranger.ArrangeCharArray(header.arguments_size + 2); + char **argv_buf = arranger.Arrange(); + + /* Determine extents. */ + const auto arg_buf_size = reinterpret_cast(argv_buf) - args_region; + const auto arg_max = (header.allocated_size - arg_buf_size) / sizeof(char *); + + /* Make argv. */ + const auto arg_count = MakeArgv(argv_buf, arg_buf, cmd_line, header.arguments_size, arg_max); + + /* Set host argc/argv. */ + os::SetHostArgc(arg_count); + os::SetHostArgv(argv_buf); + } + } + +}