2020-01-29 07:09:47 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020 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 <mesosphere.hpp>
|
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
2020-02-08 04:16:09 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
template<typename F>
|
|
|
|
ALWAYS_INLINE void DoOnEachCoreInOrder(s32 core_id, F f) {
|
|
|
|
cpu::SynchronizeAllCores();
|
|
|
|
for (size_t i = 0; i < cpu::NumCores; i++) {
|
|
|
|
if (static_cast<s32>(i) == core_id) {
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
cpu::SynchronizeAllCores();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-01-29 07:09:47 +01:00
|
|
|
NORETURN void HorizonKernelMain(s32 core_id) {
|
2020-01-29 23:26:24 +01:00
|
|
|
/* Setup the Core Local Region, and note that we're initializing. */
|
2020-01-31 10:53:30 +01:00
|
|
|
Kernel::InitializeCoreLocalRegion(core_id);
|
2020-01-29 23:26:24 +01:00
|
|
|
Kernel::SetState(Kernel::State::Initializing);
|
|
|
|
|
|
|
|
/* Ensure that all cores get to this point before proceeding. */
|
2020-01-29 07:09:47 +01:00
|
|
|
cpu::SynchronizeAllCores();
|
2020-01-29 23:26:24 +01:00
|
|
|
|
2020-01-30 07:06:25 +01:00
|
|
|
/* Initialize the main and idle thread for each core. */
|
2020-02-08 04:16:09 +01:00
|
|
|
DoOnEachCoreInOrder(core_id, [=]() ALWAYS_INLINE_LAMBDA {
|
|
|
|
Kernel::InitializeMainAndIdleThreads(core_id);
|
|
|
|
});
|
2020-01-30 07:06:25 +01:00
|
|
|
|
2020-01-30 10:41:59 +01:00
|
|
|
if (core_id == 0) {
|
2020-02-15 04:58:57 +01:00
|
|
|
/* Initialize the carveout and the system resource limit. */
|
|
|
|
KSystemControl::InitializePhase1();
|
2020-02-06 10:05:35 +01:00
|
|
|
|
2021-04-07 21:42:35 +02:00
|
|
|
/* Synchronize all cores before proceeding, to ensure access to the global rng is consistent. */
|
|
|
|
cpu::SynchronizeAllCores();
|
|
|
|
|
2020-02-07 05:36:26 +01:00
|
|
|
/* Initialize the memory manager and the KPageBuffer slabheap. */
|
2020-02-07 02:40:57 +01:00
|
|
|
{
|
2020-08-23 22:19:45 +02:00
|
|
|
const auto &management_region = KMemoryLayout::GetPoolManagementRegion();
|
2020-12-02 02:14:23 +01:00
|
|
|
MESOSPHERE_ABORT_UNLESS(management_region.GetEndAddress() != 0);
|
|
|
|
|
2020-08-23 22:19:45 +02:00
|
|
|
Kernel::GetMemoryManager().Initialize(management_region.GetAddress(), management_region.GetSize());
|
2020-02-07 05:36:26 +01:00
|
|
|
init::InitializeKPageBufferSlabHeap();
|
2020-02-07 02:40:57 +01:00
|
|
|
}
|
|
|
|
|
2020-02-07 13:58:35 +01:00
|
|
|
/* Copy the Initial Process Binary to safe memory. */
|
|
|
|
CopyInitialProcessBinaryToKernelMemory();
|
|
|
|
|
2020-02-09 10:16:13 +01:00
|
|
|
/* Print out information about the kernel. */
|
|
|
|
Kernel::PrintLayout();
|
|
|
|
|
2020-02-07 13:58:35 +01:00
|
|
|
/* Initialize the KObject Slab Heaps. */
|
2020-01-30 10:41:59 +01:00
|
|
|
init::InitializeSlabHeaps();
|
2020-02-07 13:58:35 +01:00
|
|
|
|
|
|
|
/* Initialize the Dynamic Slab Heaps. */
|
|
|
|
{
|
|
|
|
const auto &pt_heap_region = KMemoryLayout::GetPageTableHeapRegion();
|
2020-12-02 02:14:23 +01:00
|
|
|
MESOSPHERE_ABORT_UNLESS(pt_heap_region.GetEndAddress() != 0);
|
|
|
|
|
2020-02-07 13:58:35 +01:00
|
|
|
Kernel::InitializeResourceManagers(pt_heap_region.GetAddress(), pt_heap_region.GetSize());
|
|
|
|
}
|
2021-04-07 21:42:35 +02:00
|
|
|
} else {
|
|
|
|
/* Synchronize all cores before proceeding, to ensure access to the global rng is consistent. */
|
|
|
|
cpu::SynchronizeAllCores();
|
2020-01-30 10:41:59 +01:00
|
|
|
}
|
|
|
|
|
2020-02-08 04:16:09 +01:00
|
|
|
/* Initialize the supervisor page table for each core. */
|
|
|
|
DoOnEachCoreInOrder(core_id, [=]() ALWAYS_INLINE_LAMBDA {
|
2020-02-09 12:45:45 +01:00
|
|
|
KPageTable::Initialize(core_id);
|
|
|
|
Kernel::GetKernelPageTable().Initialize(core_id);
|
2020-02-08 04:16:09 +01:00
|
|
|
});
|
|
|
|
|
2020-02-10 11:26:00 +01:00
|
|
|
/* Activate the supervisor page table for each core. */
|
2020-02-08 04:16:09 +01:00
|
|
|
DoOnEachCoreInOrder(core_id, [=]() ALWAYS_INLINE_LAMBDA {
|
2020-02-20 05:43:19 +01:00
|
|
|
Kernel::GetKernelPageTable().ActivateForInit();
|
2020-02-08 04:16:09 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
/* NOTE: Kernel calls on each core a nullsub here on retail kernel. */
|
|
|
|
|
|
|
|
/* Register the main/idle threads and initialize the interrupt task manager. */
|
|
|
|
DoOnEachCoreInOrder(core_id, [=]() ALWAYS_INLINE_LAMBDA {
|
|
|
|
KThread::Register(std::addressof(Kernel::GetMainThread(core_id)));
|
|
|
|
KThread::Register(std::addressof(Kernel::GetIdleThread(core_id)));
|
2020-02-10 11:26:00 +01:00
|
|
|
Kernel::GetInterruptTaskManager().Initialize();
|
2020-02-08 04:16:09 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
/* Activate the scheduler and enable interrupts. */
|
|
|
|
DoOnEachCoreInOrder(core_id, [=]() ALWAYS_INLINE_LAMBDA {
|
|
|
|
Kernel::GetScheduler().Activate();
|
|
|
|
KInterruptManager::EnableInterrupts();
|
|
|
|
});
|
|
|
|
|
|
|
|
/* Initialize cpu interrupt threads. */
|
2020-02-14 11:20:33 +01:00
|
|
|
cpu::InitializeInterruptThreads(core_id);
|
2020-02-08 04:16:09 +01:00
|
|
|
|
|
|
|
/* Initialize the DPC manager. */
|
|
|
|
KDpcManager::Initialize();
|
|
|
|
cpu::SynchronizeAllCores();
|
|
|
|
|
|
|
|
/* Perform more core-0 specific initialization. */
|
|
|
|
if (core_id == 0) {
|
2020-02-15 04:58:57 +01:00
|
|
|
/* Initialize the exit worker manager, so that threads and processes may exit cleanly. */
|
2020-02-14 11:56:42 +01:00
|
|
|
Kernel::GetWorkerTaskManager(KWorkerTaskManager::WorkerType_Exit).Initialize(KWorkerTaskManager::WorkerType_Exit, KWorkerTaskManager::ExitWorkerPriority);
|
2020-02-08 04:16:09 +01:00
|
|
|
|
2020-02-15 04:58:57 +01:00
|
|
|
/* Setup so that we may sleep later, and reserve memory for secure applets. */
|
|
|
|
KSystemControl::InitializePhase2();
|
2020-02-08 04:16:09 +01:00
|
|
|
|
2020-02-15 09:00:35 +01:00
|
|
|
/* Initialize the SMMU. */
|
|
|
|
KDeviceAddressSpace::Initialize();
|
2020-02-08 04:16:09 +01:00
|
|
|
|
2020-02-15 12:44:41 +01:00
|
|
|
/* Load the initial processes. */
|
|
|
|
CreateAndRunInitialProcesses();
|
2020-02-08 04:16:09 +01:00
|
|
|
|
|
|
|
/* We're done initializing! */
|
|
|
|
Kernel::SetState(Kernel::State::Initialized);
|
|
|
|
|
2020-02-20 05:42:21 +01:00
|
|
|
/* Resume all threads suspended while we initialized. */
|
|
|
|
KThread::ResumeThreadsSuspendedForInit();
|
2020-12-02 02:14:23 +01:00
|
|
|
|
|
|
|
/* Validate that all reserved dram blocks are valid. */
|
|
|
|
for (const auto ®ion : KMemoryLayout::GetPhysicalMemoryRegionTree()) {
|
|
|
|
if (region.IsDerivedFrom(KMemoryRegionType_DramReservedBase)) {
|
|
|
|
MESOSPHERE_ABORT_UNLESS(region.GetEndAddress() != 0);
|
|
|
|
}
|
|
|
|
}
|
2020-02-08 04:16:09 +01:00
|
|
|
}
|
2020-02-06 14:34:38 +01:00
|
|
|
cpu::SynchronizeAllCores();
|
2020-02-08 04:16:09 +01:00
|
|
|
|
|
|
|
/* Set the current thread priority to idle. */
|
|
|
|
GetCurrentThread().SetPriorityToIdle();
|
|
|
|
|
|
|
|
/* Exit the main thread. */
|
|
|
|
{
|
|
|
|
auto &main_thread = Kernel::GetMainThread(core_id);
|
|
|
|
main_thread.Open();
|
|
|
|
main_thread.Exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Main() is done, and we should never get to this point. */
|
|
|
|
MESOSPHERE_PANIC("Main Thread continued after exit.");
|
2020-08-14 02:28:29 +02:00
|
|
|
AMS_INFINITE_LOOP();
|
2020-01-29 07:09:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|