kern: pass ini1 size from loader to kernel, remove slab memset from init0

This commit is contained in:
Michael Scire
2023-10-11 02:02:49 -07:00
committed by SciresM
parent add4b3fdc3
commit 4ca3c44e5f
6 changed files with 62 additions and 31 deletions

View File

@@ -45,7 +45,7 @@ namespace ams::kern::init {
constinit KInitArguments g_init_arguments[cpu::NumCores];
/* Globals for passing data between InitializeCorePhase1 and InitializeCorePhase2. */
constinit InitialProcessBinaryLayout g_phase2_initial_process_binary_layout{};
constinit InitialProcessBinaryLayoutWithSize g_phase2_initial_process_binary_meta{};
constinit KPhysicalAddress g_phase2_resource_end_phys_addr = Null<KPhysicalAddress>;
constinit u64 g_phase2_linear_region_phys_to_virt_diff = 0;
@@ -246,7 +246,7 @@ namespace ams::kern::init {
/* Decode the initial state. */
const auto initial_page_allocator_state = *static_cast<KInitialPageAllocator::State *>(initial_state[0]);
g_phase2_initial_process_binary_layout = *static_cast<InitialProcessBinaryLayout *>(initial_state[1]);
g_phase2_initial_process_binary_meta = *static_cast<InitialProcessBinaryLayoutWithSize *>(initial_state[1]);
/* Restore the page allocator state setup by kernel loader. */
g_initial_page_allocator.InitializeFromState(std::addressof(initial_page_allocator_state));
@@ -565,9 +565,6 @@ namespace ams::kern::init {
}
}
/* Clear the slab region. */
std::memset(GetVoidPointer(slab_region_start), 0, slab_region_size);
/* NOTE: Unknown function is called here which is ifdef'd out on retail kernel. */
/* The unknown function is immediately before the function which gets an unknown debug region size, inside this translation unit. */
/* It's likely that this is some kind of initializer for this unknown debug region. */
@@ -624,9 +621,10 @@ namespace ams::kern::init {
/* Set the initial process binary physical address. */
/* NOTE: Nintendo does this after pool partition setup, but it's a requirement that we do it before */
/* to retain compatibility with < 5.0.0. */
const KPhysicalAddress ini_address = g_phase2_initial_process_binary_layout.address;
const KPhysicalAddress ini_address = g_phase2_initial_process_binary_meta.layout.address;
const size_t ini_size = g_phase2_initial_process_binary_meta.size;
MESOSPHERE_INIT_ABORT_UNLESS(ini_address != Null<KPhysicalAddress>);
SetInitialProcessBinaryPhysicalAddress(ini_address);
SetInitialProcessBinaryPhysicalAddress(ini_address, ini_size);
/* Setup all other memory regions needed to arrange the pool partitions. */
SetupPoolPartitionMemoryRegions();
@@ -640,7 +638,7 @@ namespace ams::kern::init {
/* Check that the region contains the ini. */
MESOSPHERE_INIT_ABORT_UNLESS(ini_region->GetAddress() <= GetInteger(ini_address));
MESOSPHERE_INIT_ABORT_UNLESS(GetInteger(ini_address) + InitialProcessBinarySizeMax <= ini_region->GetEndAddress());
MESOSPHERE_INIT_ABORT_UNLESS(GetInteger(ini_address) + ini_size <= ini_region->GetEndAddress());
MESOSPHERE_INIT_ABORT_UNLESS(ini_region->GetEndAddress() != 0);
}

View File

@@ -45,7 +45,7 @@ namespace ams::kern::init::loader {
constinit KInitialPageAllocator g_initial_page_allocator;
constinit KInitialPageAllocator::State g_final_page_allocator_state;
constinit InitialProcessBinaryLayout g_initial_process_binary_layout;
constinit InitialProcessBinaryLayoutWithSize g_initial_process_binary_meta;
constinit void *g_final_state[2];
@@ -165,19 +165,31 @@ namespace ams::kern::init::loader {
const uintptr_t resource_end_address = base_address + resource_offset + resource_region_size;
/* Setup the INI1 header in memory for the kernel. */
KSystemControl::Init::GetInitialProcessBinaryLayout(std::addressof(g_initial_process_binary_layout));
MESOSPHERE_INIT_ABORT_UNLESS(g_initial_process_binary_layout.address != 0);
{
/* Get the kernel layout. */
KSystemControl::Init::GetInitialProcessBinaryLayout(std::addressof(g_initial_process_binary_meta.layout));
if (ini_base_address != g_initial_process_binary_layout.address) {
/* The INI is not at the correct address, so we need to relocate it. */
/* If there's no desired base address, use the ini in place. */
if (g_initial_process_binary_meta.layout.address == 0) {
g_initial_process_binary_meta.layout.address = ini_base_address;
}
/* Validate and potentially relocate the INI. */
const InitialProcessBinaryHeader *ini_header = reinterpret_cast<const InitialProcessBinaryHeader *>(ini_base_address);
if (ini_header->magic == InitialProcessBinaryMagic && ini_header->size <= InitialProcessBinarySizeMax) {
/* INI is valid, relocate it. */
std::memmove(reinterpret_cast<void *>(g_initial_process_binary_layout.address), ini_header, ini_header->size);
size_t ini_size = 0;
if (ini_header->magic == InitialProcessBinaryMagic && (ini_size = ini_header->size) <= InitialProcessBinarySizeMax) {
/* INI is valid, relocate it if necessary. */
if (ini_base_address != g_initial_process_binary_meta.layout.address) {
std::memmove(reinterpret_cast<void *>(g_initial_process_binary_meta.layout.address), ini_header, ini_size);
}
} else {
/* INI is invalid. Make the destination header invalid. */
std::memset(reinterpret_cast<void *>(g_initial_process_binary_layout.address), 0, sizeof(InitialProcessBinaryHeader));
std::memset(reinterpret_cast<void *>(g_initial_process_binary_meta.layout.address), 0, sizeof(InitialProcessBinaryHeader));
}
/* Set the INI size in layout. */
g_initial_process_binary_meta.size = util::AlignUp(ini_size, PageSize);
}
/* We want to start allocating page tables at the end of the resource region. */
@@ -238,7 +250,7 @@ namespace ams::kern::init::loader {
/* Setup final kernel loader state. */
g_final_state[0] = std::addressof(g_final_page_allocator_state);
g_final_state[1] = std::addressof(g_initial_process_binary_layout);
g_final_state[1] = std::addressof(g_initial_process_binary_meta);
return g_final_state;
}