strat: use ams::Main() instead of main(argc, argv)

This commit is contained in:
Michael Scire
2021-10-07 17:44:54 -07:00
parent 6a53726833
commit ffc143860b
47 changed files with 2972 additions and 3609 deletions

View 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");
}

View 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);
}

View 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);
}

View 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. */
}
}