2023-05-29 00:32:54 -07:00
|
|
|
#include "loader/init_loader.h"
|
|
|
|
|
|
|
|
#include "boot/boot_info.h"
|
|
|
|
#include "debug/debug.h"
|
|
|
|
#include "loader/elf_loader.h"
|
2023-05-30 20:55:03 -07:00
|
|
|
#include "memory/paging_util.h"
|
2023-05-29 00:32:54 -07:00
|
|
|
#include "scheduler/process.h"
|
2023-05-29 23:35:44 -07:00
|
|
|
#include "scheduler/process_manager.h"
|
2023-05-29 00:32:54 -07:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2023-05-30 20:55:03 -07:00
|
|
|
bool streq(const char* a, const char* b) {
|
|
|
|
while (true) {
|
|
|
|
if (*a == '\0' && *b == '\0') return true;
|
|
|
|
if (*a == '\0' || *b == '\0') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (*a != *b) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
a++;
|
|
|
|
b++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-06 15:04:34 -07:00
|
|
|
void DumpModules() {
|
|
|
|
const limine_module_response& resp = boot::GetModules();
|
|
|
|
dbgln("[boot] Dumping bootloader modules.");
|
|
|
|
for (uint64_t i = 0; i < resp.module_count; i++) {
|
|
|
|
const limine_file& file = *resp.modules[i];
|
|
|
|
dbgln(" %s,%m,%x", file.path, file.address, file.size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-30 20:55:03 -07:00
|
|
|
const limine_file& GetInitProgram(const char* path) {
|
2023-05-29 00:32:54 -07:00
|
|
|
const limine_module_response& resp = boot::GetModules();
|
|
|
|
for (uint64_t i = 0; i < resp.module_count; i++) {
|
|
|
|
const limine_file& file = *resp.modules[i];
|
2023-05-30 20:55:03 -07:00
|
|
|
if (streq(file.path, path)) return file;
|
2023-05-29 00:32:54 -07:00
|
|
|
}
|
2023-05-30 20:55:03 -07:00
|
|
|
panic("Program not found: %s", path);
|
2023-05-29 00:32:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void LoadInitProgram() {
|
2023-06-06 15:04:34 -07:00
|
|
|
DumpModules();
|
2023-05-30 20:55:03 -07:00
|
|
|
const limine_file& init_prog = GetInitProgram("/sys/test");
|
|
|
|
const limine_file& prog2 = GetInitProgram("/sys/test2");
|
2023-05-29 00:32:54 -07:00
|
|
|
|
2023-05-30 01:27:47 -07:00
|
|
|
SharedPtr<Process> proc = MakeShared<Process>();
|
|
|
|
gProcMan->InsertProcess(proc);
|
|
|
|
|
2023-05-30 21:39:19 -07:00
|
|
|
uint64_t entry = LoadElfProgram(
|
|
|
|
*proc, reinterpret_cast<uint64_t>(init_prog.address), init_prog.size);
|
2023-05-30 20:55:03 -07:00
|
|
|
|
|
|
|
CopyIntoNonResidentProcess(reinterpret_cast<uint64_t>(prog2.address),
|
2023-05-30 21:39:19 -07:00
|
|
|
prog2.size, *proc,
|
2023-05-30 20:55:03 -07:00
|
|
|
proc->vmm().GetNextMemMapAddr(prog2.size));
|
|
|
|
|
2023-05-30 01:27:47 -07:00
|
|
|
proc->CreateThread(entry);
|
2023-05-29 00:32:54 -07:00
|
|
|
}
|