diff --git a/libraries/libmesosphere/include/mesosphere/init/kern_init_layout.hpp b/libraries/libmesosphere/include/mesosphere/init/kern_init_layout.hpp index 904defc..620ef85 100644 --- a/libraries/libmesosphere/include/mesosphere/init/kern_init_layout.hpp +++ b/libraries/libmesosphere/include/mesosphere/init/kern_init_layout.hpp @@ -27,7 +27,7 @@ u32 rw_end_offset; u32 bss_offset; u32 bss_end_offset; - u32 ini_end_offset; + u32 ini_load_offset; u32 dynamic_offset; u32 init_array_offset; u32 init_array_end_offset; diff --git a/libraries/libmesosphere/source/arch/arm64/svc/kern_svc_tables.cpp b/libraries/libmesosphere/source/arch/arm64/svc/kern_svc_tables.cpp new file mode 100644 index 0000000..bc5b264 --- /dev/null +++ b/libraries/libmesosphere/source/arch/arm64/svc/kern_svc_tables.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2018-2019 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 + +/* TODO: Enable compilation of this file when the kernel supports supervisor calls. */ +#if 0 +namespace ams::kern::svc { + + namespace { + + #define DECLARE_SVC_STRUCT(ID, RETURN_TYPE, NAME, ...) \ + class NAME { \ + private: \ + using Impl = ::ams::svc::codegen::KernelSvcWrapper<::ams::kern::svc::NAME##64, ::ams::kern::svc::NAME##64From32>; \ + public: \ + static NOINLINE void Call64() { return Impl::Call64(); } \ + static NOINLINE void Call64From32() { return Impl::Call64From32(); } \ + }; + + + + /* Set omit-frame-pointer to prevent GCC from emitting MOV X29, SP instructions. */ + #pragma GCC push_options + #pragma GCC optimize ("omit-frame-pointer") + + AMS_SVC_FOREACH_KERN_DEFINITION(DECLARE_SVC_STRUCT, _) + + #pragma GCC pop_options + + } + + const std::array SvcTable64From32 = [] { + std::array table = {}; + + #define AMS_KERN_SVC_SET_TABLE_ENTRY(ID, RETURN_TYPE, NAME, ...) \ + table[ID] = NAME::Call64From32; + AMS_SVC_FOREACH_KERN_DEFINITION(AMS_KERN_SVC_SET_TABLE_ENTRY, _) + #undef AMS_KERN_SVC_SET_TABLE_ENTRY + + return table; + }(); + + const std::array SvcTable64 = [] { + std::array table = {}; + + #define AMS_KERN_SVC_SET_TABLE_ENTRY(ID, RETURN_TYPE, NAME, ...) \ + table[ID] = NAME::Call64; + AMS_SVC_FOREACH_KERN_DEFINITION(AMS_KERN_SVC_SET_TABLE_ENTRY, _) + #undef AMS_KERN_SVC_SET_TABLE_ENTRY + + return table; + }(); + +} +#endif \ No newline at end of file diff --git a/libraries/libmesosphere/source/svc/kern_svc_tables.cpp b/libraries/libmesosphere/source/svc/kern_svc_tables.cpp deleted file mode 100644 index a2fd6a2..0000000 --- a/libraries/libmesosphere/source/svc/kern_svc_tables.cpp +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (c) 2018-2019 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 - -namespace ams::kern::svc { - - namespace { - - #define DECLARE_SVC_STRUCT(ID, RETURN_TYPE, NAME, ...) \ - class NAME { \ - private: \ - using Impl = ::ams::svc::codegen::KernelSvcWrapper<::ams::kern::svc::NAME##64, ::ams::kern::svc::NAME##64From32>; \ - public: \ - static NOINLINE void Call64() { return Impl::Call64(); } \ - static NOINLINE void Call64From32() { return Impl::Call64From32(); } \ - }; - - - - /* Set omit-frame-pointer to prevent GCC from emitting MOV X29, SP instructions. */ - #pragma GCC push_options - #pragma GCC optimize ("omit-frame-pointer") - - AMS_SVC_FOREACH_KERN_DEFINITION(DECLARE_SVC_STRUCT, _) - - #pragma GCC pop_options - - } - - /* TODO: 32-bit ABI */ - const std::array SvcTable64From32 = [] { - std::array table = {}; - - #define AMS_KERN_SVC_SET_TABLE_ENTRY(ID, RETURN_TYPE, NAME, ...) \ - table[ID] = NAME::Call64From32; - AMS_SVC_FOREACH_KERN_DEFINITION(AMS_KERN_SVC_SET_TABLE_ENTRY, _) - #undef AMS_KERN_SVC_SET_TABLE_ENTRY - - return table; - }(); - - const std::array SvcTable64 = [] { - std::array table = {}; - - #define AMS_KERN_SVC_SET_TABLE_ENTRY(ID, RETURN_TYPE, NAME, ...) \ - table[ID] = NAME::Call64; - AMS_SVC_FOREACH_KERN_DEFINITION(AMS_KERN_SVC_SET_TABLE_ENTRY, _) - #undef AMS_KERN_SVC_SET_TABLE_ENTRY - - return table; - }(); - -} diff --git a/mesosphere/Makefile b/mesosphere/Makefile index 31b597a..f511441 100644 --- a/mesosphere/Makefile +++ b/mesosphere/Makefile @@ -9,7 +9,8 @@ @rm -f mesosphere.bin mesosphere.bin: $(TARGETS) - @echo "todo: py script" + @python build_mesosphere.py + @echo "Built mesosphere.bin..." $(TARGETS): $(MAKE) -C $@ diff --git a/mesosphere/build_mesosphere.py b/mesosphere/build_mesosphere.py new file mode 100644 index 0000000..d7e4087 --- /dev/null +++ b/mesosphere/build_mesosphere.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python +import sys, os +from struct import pack as pk, unpack as up + +def align_up(val, algn): + val += algn - 1 + return val - (val % algn) + + +def main(argc, argv): + if argc != 1: + print('Usage: %s' % argv[0]) + return 1 + with open('kernel_ldr/kernel_ldr.bin', 'rb') as f: + kernel_ldr = f.read() + with open('kernel/kernel.bin', 'rb') as f: + kernel = f.read() + kernel_metadata_offset = up('= len(kernel)) + + embedded_ini = b'' + embedded_ini_offset = align_up(kernel_end, 0x1000) + 0x1000 + embedded_ini_end = embedded_ini_offset + 0 # TODO: Create and embed an INI, eventually. + + kernel_ldr_offset = align_up(embedded_ini_end, 0x1000) + 0x1000 + kernel_ldr_end = kernel_ldr_offset + len(kernel_ldr) + + with open('mesosphere.bin', 'wb') as f: + f.write(kernel[:kernel_metadata_offset + 8]) + f.write(pk('bss_offset; - const uintptr_t ini_end_offset = layout->ini_end_offset; + const uintptr_t ini_load_offset = layout->ini_load_offset; const uintptr_t dynamic_offset = layout->dynamic_offset; const uintptr_t init_array_offset = layout->init_array_offset; const uintptr_t init_array_end_offset = layout->init_array_end_offset; @@ -277,9 +277,11 @@ /* Decide if Kernel should have enlarged resource region. */ const bool use_extra_resources = KSystemControl::Init::ShouldIncreaseThreadResourceLimit(); const size_t resource_region_size = KernelResourceRegionSize + (use_extra_resources ? ExtraKernelResourceSize : 0); + static_assert(KernelResourceRegionSize > InitialProcessBinarySizeMax); + static_assert(KernelResourceRegionSize + ExtraKernelResourceSize > InitialProcessBinarySizeMax); /* Setup the INI1 header in memory for the kernel. */ - const uintptr_t ini_end_address = base_address + ini_end_offset + resource_region_size; + const uintptr_t ini_end_address = base_address + ini_load_offset + resource_region_size; const uintptr_t ini_load_address = ini_end_address - InitialProcessBinarySizeMax; if (ini_base_address != ini_load_address) { /* The INI is not at the correct address, so we need to relocate it. */