mirror of
https://github.com/Atmosphere-NX/Atmosphere-libs.git
synced 2025-06-29 14:32:58 +02:00
strat: use ams::Main() instead of main(argc, argv)
This commit is contained in:
parent
8a8631ebcb
commit
2905319d46
@ -26,7 +26,7 @@ CXXFLAGS := $(CFLAGS) $(ATMOSPHERE_CXXFLAGS) -fno-use-cxa-atexit
|
|||||||
ASFLAGS := $(ATMOSPHERE_ASFLAGS) $(SETTINGS)
|
ASFLAGS := $(ATMOSPHERE_ASFLAGS) $(SETTINGS)
|
||||||
endif
|
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 \
|
export CXXWRAPS := -Wl,--wrap,__cxa_pure_virtual \
|
||||||
-Wl,--wrap,__cxa_throw \
|
-Wl,--wrap,__cxa_throw \
|
||||||
|
@ -12,7 +12,7 @@ export CFLAGS := $(ATMOSPHERE_CFLAGS) $(SETTINGS) $(DEFINES) $(INCLUDE)
|
|||||||
export CXXFLAGS := $(CFLAGS) $(ATMOSPHERE_CXXFLAGS) -fno-use-cxa-atexit
|
export CXXFLAGS := $(CFLAGS) $(ATMOSPHERE_CXXFLAGS) -fno-use-cxa-atexit
|
||||||
export ASFLAGS := $(ATMOSPHERE_ASFLAGS) $(SETTINGS) $(DEFINES) $(INCLUDE)
|
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 \
|
export CXXWRAPS := -Wl,--wrap,__cxa_pure_virtual \
|
||||||
-Wl,--wrap,__cxa_throw \
|
-Wl,--wrap,__cxa_throw \
|
||||||
|
@ -21,6 +21,15 @@ export CFLAGS = $(ATMOSPHERE_CFLAGS) $(SETTINGS) $(DEFINES) $(INCLUDE)
|
|||||||
export CXXFLAGS = $(CFLAGS) $(ATMOSPHERE_CXXFLAGS)
|
export CXXFLAGS = $(CFLAGS) $(ATMOSPHERE_CXXFLAGS)
|
||||||
export ASFLAGS = $(ATMOSPHERE_ASFLAGS) $(SETTINGS) $(DEFINES)
|
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 \
|
export CXXWRAPS := -Wl,--wrap,__cxa_pure_virtual \
|
||||||
-Wl,--wrap,__cxa_throw \
|
-Wl,--wrap,__cxa_throw \
|
||||||
-Wl,--wrap,__cxa_rethrow \
|
-Wl,--wrap,__cxa_rethrow \
|
||||||
@ -37,7 +46,7 @@ export CXXWRAPS := -Wl,--wrap,__cxa_pure_virtual \
|
|||||||
-Wl,--wrap,_ZNSt11logic_errorC2EPKc \
|
-Wl,--wrap,_ZNSt11logic_errorC2EPKc \
|
||||||
-Wl,--wrap,exit
|
-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
|
export LIBS = -lstratosphere -lnx
|
||||||
|
|
||||||
|
@ -126,6 +126,9 @@ ams_environment_weak.o: CXXFLAGS += -fno-lto
|
|||||||
pm_info_api_weak.o: CXXFLAGS += -fno-lto
|
pm_info_api_weak.o: CXXFLAGS += -fno-lto
|
||||||
hos_stratosphere_api.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
|
%_bin.h %.bin.o : %.bin
|
||||||
#---------------------------------------------------------------------------------
|
#---------------------------------------------------------------------------------
|
||||||
|
@ -62,6 +62,7 @@
|
|||||||
#include <stratosphere/htclow.hpp>
|
#include <stratosphere/htclow.hpp>
|
||||||
#include <stratosphere/htcs.hpp>
|
#include <stratosphere/htcs.hpp>
|
||||||
#include <stratosphere/i2c.hpp>
|
#include <stratosphere/i2c.hpp>
|
||||||
|
#include <stratosphere/init.hpp>
|
||||||
#include <stratosphere/kvdb.hpp>
|
#include <stratosphere/kvdb.hpp>
|
||||||
#include <stratosphere/ldr.hpp>
|
#include <stratosphere/ldr.hpp>
|
||||||
#include <stratosphere/lr.hpp>
|
#include <stratosphere/lr.hpp>
|
||||||
|
@ -62,3 +62,4 @@
|
|||||||
#include <stratosphere/fs/fs_system_data.hpp>
|
#include <stratosphere/fs/fs_system_data.hpp>
|
||||||
#include <stratosphere/fs/fs_program_index_map_info.hpp>
|
#include <stratosphere/fs/fs_program_index_map_info.hpp>
|
||||||
#include <stratosphere/fs/impl/fs_access_log_impl.hpp>
|
#include <stratosphere/fs/impl/fs_access_log_impl.hpp>
|
||||||
|
#include <stratosphere/fs/fs_api.hpp>
|
||||||
|
24
libstratosphere/include/stratosphere/fs/fs_api.hpp
Normal file
24
libstratosphere/include/stratosphere/fs/fs_api.hpp
Normal file
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
#include <vapours.hpp>
|
||||||
|
|
||||||
|
namespace ams::fs {
|
||||||
|
|
||||||
|
void InitializeForSystem();
|
||||||
|
void InitializeWithMultiSessionForSystem();
|
||||||
|
|
||||||
|
}
|
18
libstratosphere/include/stratosphere/init.hpp
Normal file
18
libstratosphere/include/stratosphere/init.hpp
Normal file
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include <stratosphere/init/init_malloc.hpp>
|
32
libstratosphere/include/stratosphere/init/init_malloc.hpp
Normal file
32
libstratosphere/include/stratosphere/init/init_malloc.hpp
Normal file
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
#include <vapours.hpp>
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
}
|
@ -52,3 +52,4 @@
|
|||||||
#include <stratosphere/os/os_barrier.hpp>
|
#include <stratosphere/os/os_barrier.hpp>
|
||||||
#include <stratosphere/os/os_io_region.hpp>
|
#include <stratosphere/os/os_io_region.hpp>
|
||||||
#include <stratosphere/os/os_multiple_wait.hpp>
|
#include <stratosphere/os/os_multiple_wait.hpp>
|
||||||
|
#include <stratosphere/os/os_argument.hpp>
|
||||||
|
@ -13,14 +13,13 @@
|
|||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
#include <stratosphere.hpp>
|
|
||||||
#include "impl/os_resource_manager.hpp"
|
#pragma once
|
||||||
|
#include <vapours.hpp>
|
||||||
|
|
||||||
namespace ams::os {
|
namespace ams::os {
|
||||||
|
|
||||||
void InitializeForStratosphereInternal() {
|
int GetHostArgc();
|
||||||
/* Initialize the global os resource manager. */
|
char **GetHostArgv();
|
||||||
os::impl::ResourceManagerHolder::InitializeResourceManagerInstance();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
@ -75,7 +75,9 @@ namespace ams::erpt::srv {
|
|||||||
g_heap_handle = lmem::CreateExpHeap(mem, mem_size, lmem::CreateOption_ThreadSafe);
|
g_heap_handle = lmem::CreateExpHeap(mem, mem_size, lmem::CreateOption_ThreadSafe);
|
||||||
AMS_ABORT_UNLESS(g_heap_handle != nullptr);
|
AMS_ABORT_UNLESS(g_heap_handle != nullptr);
|
||||||
|
|
||||||
|
fs::InitializeForSystem();
|
||||||
fs::SetAllocator(Allocate, DeallocateWithSize);
|
fs::SetAllocator(Allocate, DeallocateWithSize);
|
||||||
|
fs::SetEnabledAutoAbort(false);
|
||||||
|
|
||||||
R_ABORT_UNLESS(fs::MountSdCardErrorReportDirectoryForAtmosphere(ReportOnSdStoragePath));
|
R_ABORT_UNLESS(fs::MountSdCardErrorReportDirectoryForAtmosphere(ReportOnSdStoragePath));
|
||||||
|
|
||||||
|
38
libstratosphere/source/fs/fs_api.cpp
Normal file
38
libstratosphere/source/fs/fs_api.cpp
Normal file
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#include <stratosphere.hpp>
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
108
libstratosphere/source/init/init_libnx_shim.os.horizon.cpp
Normal file
108
libstratosphere/source/init/init_libnx_shim.os.horizon.cpp
Normal file
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#include <stratosphere.hpp>
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
147
libstratosphere/source/init/init_malloc.cpp
Normal file
147
libstratosphere/source/init/init_malloc.cpp
Normal file
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#include <stratosphere.hpp>
|
||||||
|
|
||||||
|
namespace ams::init {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
constinit void *g_malloc_region_address = nullptr;
|
||||||
|
constinit size_t g_malloc_region_size = 0;
|
||||||
|
|
||||||
|
constinit util::TypedStorage<mem::StandardAllocator> 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);
|
||||||
|
}
|
48
libstratosphere/source/init/init_operator_new.cpp
Normal file
48
libstratosphere/source/init/init_operator_new.cpp
Normal file
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#include <stratosphere.hpp>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
34
libstratosphere/source/init/init_system_module.cpp
Normal file
34
libstratosphere/source/init/init_system_module.cpp
Normal file
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#include <stratosphere.hpp>
|
||||||
|
|
||||||
|
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. */
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -103,14 +103,15 @@ namespace ams::lm::srv {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void FlushThreadFunction(void *) {
|
void FlushThreadFunction(void *) {
|
||||||
/* Disable abort. */
|
/* Initialize fs. */
|
||||||
|
fs::InitializeWithMultiSessionForSystem();
|
||||||
fs::SetEnabledAutoAbort(false);
|
fs::SetEnabledAutoAbort(false);
|
||||||
|
|
||||||
/* Create fs heap. */
|
/* Create fs heap. */
|
||||||
g_fs_heap_handle = lmem::CreateExpHeap(g_fs_heap, sizeof(g_fs_heap), lmem::CreateOption_None);
|
g_fs_heap_handle = lmem::CreateExpHeap(g_fs_heap, sizeof(g_fs_heap), lmem::CreateOption_None);
|
||||||
AMS_ABORT_UNLESS(g_fs_heap_handle != nullptr);
|
AMS_ABORT_UNLESS(g_fs_heap_handle != nullptr);
|
||||||
|
|
||||||
/* Set fs allocation functions. */
|
/* Set fs allocator functions. */
|
||||||
fs::SetAllocator(AllocateForFs, DeallocateForFs);
|
fs::SetAllocator(AllocateForFs, DeallocateForFs);
|
||||||
|
|
||||||
/* Create SD card detection event notifier. */
|
/* Create SD card detection event notifier. */
|
||||||
|
@ -353,7 +353,7 @@ namespace ams::mem::impl::heap {
|
|||||||
|
|
||||||
std::scoped_lock lk(this->lock);
|
std::scoped_lock lk(this->lock);
|
||||||
if (Span *span = GetSpanFromPointer(std::addressof(this->span_table), ptr); span != nullptr && !span->page_class) {
|
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;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
@ -387,7 +387,7 @@ namespace ams::mem::impl::heap {
|
|||||||
errno_t GetName(const void *ptr, char *dst, size_t dst_size) {
|
errno_t GetName(const void *ptr, char *dst, size_t dst_size) {
|
||||||
std::scoped_lock lk(this->lock);
|
std::scoped_lock lk(this->lock);
|
||||||
if (Span *span = GetSpanFromPointer(std::addressof(this->span_table), ptr); span != nullptr && !span->page_class) {
|
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;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
@ -397,7 +397,7 @@ namespace ams::mem::impl::heap {
|
|||||||
errno_t SetName(const void *ptr, const char *name) {
|
errno_t SetName(const void *ptr, const char *name) {
|
||||||
std::scoped_lock lk(this->lock);
|
std::scoped_lock lk(this->lock);
|
||||||
if (Span *span = GetSpanFromPointer(std::addressof(this->span_table), ptr); span != nullptr && !span->page_class) {
|
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;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
|
51
libstratosphere/source/os/os_argument.cpp
Normal file
51
libstratosphere/source/os/os_argument.cpp
Normal file
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#include <stratosphere.hpp>
|
||||||
|
|
||||||
|
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<char **>(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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
172
libstratosphere/source/os/os_stratosphere_api.os.horizon.cpp
Normal file
172
libstratosphere/source/os/os_stratosphere_api.os.horizon.cpp
Normal file
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#include <stratosphere.hpp>
|
||||||
|
#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<typename T>
|
||||||
|
T *Arrange() {
|
||||||
|
this->Align(alignof(T));
|
||||||
|
return static_cast<T *>(this->Arrange(sizeof(T)));
|
||||||
|
}
|
||||||
|
|
||||||
|
void *Arrange(size_t size) {
|
||||||
|
const auto address = m_address;
|
||||||
|
m_address += size;
|
||||||
|
return reinterpret_cast<void *>(address);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Align(size_t align) {
|
||||||
|
m_address = util::AlignUp(m_address, align);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ArrangeCharArray(size_t size) {
|
||||||
|
return reinterpret_cast<char *>(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<uintptr_t>(__argdata__);
|
||||||
|
if (HasArguments(args_region)) {
|
||||||
|
/* Create arguments memory arranger. */
|
||||||
|
MemoryArranger arranger(args_region);
|
||||||
|
|
||||||
|
/* Arrange. */
|
||||||
|
const auto &header = *arranger.Arrange<ldr::ProgramArguments>();
|
||||||
|
const char *cmd_line = arranger.ArrangeCharArray(header.arguments_size);
|
||||||
|
char *arg_buf = arranger.ArrangeCharArray(header.arguments_size + 2);
|
||||||
|
char **argv_buf = arranger.Arrange<char *>();
|
||||||
|
|
||||||
|
/* Determine extents. */
|
||||||
|
const auto arg_buf_size = reinterpret_cast<uintptr_t>(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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user