mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-05-14 15:14:23 -04:00
kern: skeleton initial process loading
This commit is contained in:
parent
30d6b359f9
commit
cbc73f4407
7 changed files with 465 additions and 1 deletions
|
@ -19,6 +19,12 @@ namespace ams::kern {
|
|||
|
||||
namespace {
|
||||
|
||||
struct InitialProcessInfo {
|
||||
KProcess *process;
|
||||
size_t stack_size;
|
||||
s32 priority;
|
||||
};
|
||||
|
||||
KVirtualAddress GetInitialProcessBinaryAddress() {
|
||||
return KMemoryLayout::GetPageTableHeapRegion().GetEndAddress() - InitialProcessBinarySizeMax;
|
||||
}
|
||||
|
@ -32,6 +38,59 @@ namespace ams::kern {
|
|||
MESOSPHERE_ABORT_UNLESS(header->num_processes <= init::GetSlabResourceCounts().num_KProcess);
|
||||
}
|
||||
|
||||
void CreateProcesses(InitialProcessInfo *infos, KVirtualAddress binary_address, const InitialProcessBinaryHeader &header) {
|
||||
u8 *current = GetPointer<u8>(binary_address + sizeof(InitialProcessBinaryHeader));
|
||||
const u8 * const end = GetPointer<u8>(binary_address + header.size - sizeof(KInitialProcessHeader));
|
||||
|
||||
const size_t num_processes = header.num_processes;
|
||||
for (size_t i = 0; i < num_processes; i++) {
|
||||
/* Validate that we can read the current KIP. */
|
||||
MESOSPHERE_ABORT_UNLESS(current <= end);
|
||||
KInitialProcessReader reader;
|
||||
MESOSPHERE_ABORT_UNLESS(reader.Attach(current));
|
||||
|
||||
/* Parse process parameters and reserve memory. */
|
||||
ams::svc::CreateProcessParameter params;
|
||||
MESOSPHERE_R_ABORT_UNLESS(reader.MakeCreateProcessParameter(std::addressof(params), true));
|
||||
MESOSPHERE_TODO("Reserve memory");
|
||||
|
||||
/* Create the process, and ensure we don't leak pages. */
|
||||
{
|
||||
/* Allocate memory for the process. */
|
||||
MESOSPHERE_TODO("Allocate memory for the process");
|
||||
|
||||
/* Map the process's memory into the temporary region. */
|
||||
MESOSPHERE_TODO("Map the process's page group");
|
||||
|
||||
/* Load the process. */
|
||||
MESOSPHERE_TODO("Load the process");
|
||||
|
||||
/* Unmap the temporary mapping. */
|
||||
MESOSPHERE_TODO("Unmap the process's page group");
|
||||
|
||||
/* Create a KProcess object. */
|
||||
MESOSPHERE_TODO("Create a KProcess");
|
||||
|
||||
/* Initialize the process. */
|
||||
MESOSPHERE_TODO("Initialize the process");
|
||||
}
|
||||
|
||||
/* Set the process's memory permissions. */
|
||||
MESOSPHERE_TODO("Set process's memory permissions");
|
||||
|
||||
/* Register the process. */
|
||||
MESOSPHERE_TODO("Register the process");
|
||||
|
||||
/* Save the process info. */
|
||||
infos[i].process = /* TODO */ nullptr;
|
||||
infos[i].stack_size = reader.GetStackSize();
|
||||
infos[i].priority = reader.GetPriority();
|
||||
|
||||
/* Advance the reader. */
|
||||
current += reader.GetBinarySize();
|
||||
}
|
||||
}
|
||||
|
||||
KVirtualAddress g_initial_process_binary_address;
|
||||
InitialProcessBinaryHeader g_initial_process_binary_header;
|
||||
u64 g_initial_process_id_min = std::numeric_limits<u64>::max();
|
||||
|
@ -71,4 +130,32 @@ namespace ams::kern {
|
|||
}
|
||||
}
|
||||
|
||||
void CreateAndRunInitialProcesses() {
|
||||
/* Allocate space for the processes. */
|
||||
InitialProcessInfo *infos = static_cast<InitialProcessInfo *>(__builtin_alloca(sizeof(InitialProcessInfo) * g_initial_process_binary_header.num_processes));
|
||||
|
||||
/* Create the processes. */
|
||||
CreateProcesses(infos, g_initial_process_binary_address, g_initial_process_binary_header);
|
||||
|
||||
/* Release the memory used by the image. */
|
||||
{
|
||||
const size_t total_size = util::AlignUp(g_initial_process_binary_header.size, PageSize);
|
||||
const size_t num_pages = total_size / PageSize;
|
||||
Kernel::GetMemoryManager().Close(g_initial_process_binary_address, num_pages);
|
||||
Kernel::GetSystemResourceLimit().Release(ams::svc::LimitableResource_PhysicalMemoryMax, total_size);
|
||||
}
|
||||
|
||||
/* Determine the initial process id range. */
|
||||
for (size_t i = 0; i < g_initial_process_binary_header.num_processes; i++) {
|
||||
const auto pid = infos[i].process->GetId();
|
||||
g_initial_process_id_min = std::min(g_initial_process_id_min, pid);
|
||||
g_initial_process_id_max = std::max(g_initial_process_id_max, pid);
|
||||
}
|
||||
|
||||
/* Run the processes. */
|
||||
for (size_t i = 0; i < g_initial_process_binary_header.num_processes; i++) {
|
||||
MESOSPHERE_TODO("infos[i].process->Run(infos[i].priority, infos[i].stack_size);");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue