mirror of
https://github.com/Fennix-Project/Kernel.git
synced 2025-07-11 15:29:18 +00:00
Update file loading
This commit is contained in:
@ -2,7 +2,9 @@
|
||||
|
||||
#include <memory.hpp>
|
||||
#include <lock.hpp>
|
||||
#include <msexec.h>
|
||||
#include <cwalk.h>
|
||||
#include <elf.h>
|
||||
|
||||
#include "../kernel.h"
|
||||
#include "../Fex.hpp"
|
||||
@ -27,40 +29,151 @@ namespace Execute
|
||||
case BinaryType::BinTypeFex:
|
||||
{
|
||||
Fex *FexHdr = (Fex *)ExFile->Node->Address;
|
||||
if (FexHdr->Magic[0] == 'F' && FexHdr->Magic[1] == 'E' && FexHdr->Magic[2] == 'X' && FexHdr->Magic[3] == '\0')
|
||||
if (FexHdr->Type == FexFormatType::FexFormatType_Executable)
|
||||
{
|
||||
if (FexHdr->Type == FexFormatType::FexFormatType_Executable)
|
||||
const char *BaseName;
|
||||
cwk_path_get_basename(Path, &BaseName, nullptr);
|
||||
PCB *Process = TaskManager->CreateProcess(TaskManager->GetCurrentProcess(), BaseName, TaskTrustLevel::User);
|
||||
|
||||
void *BaseImage = KernelAllocator.RequestPages(TO_PAGES(ExFile->Node->Length));
|
||||
memcpy(BaseImage, (void *)ExFile->Node->Address, ExFile->Node->Length);
|
||||
|
||||
Memory::Virtual pva = Memory::Virtual(Process->PageTable);
|
||||
for (uint64_t i = 0; i < TO_PAGES(ExFile->Node->Length); i++)
|
||||
pva.Map((void *)((uint64_t)BaseImage + (i * PAGE_SIZE)), (void *)((uint64_t)BaseImage + (i * PAGE_SIZE)), Memory::PTFlag::RW | Memory::PTFlag::US);
|
||||
|
||||
TCB *Thread = TaskManager->CreateThread(Process,
|
||||
(IP)FexHdr->Pointer,
|
||||
Arg0, Arg1,
|
||||
(IPOffset)BaseImage,
|
||||
TaskArchitecture::x64,
|
||||
TaskCompatibility::Native);
|
||||
ret.Process = Process;
|
||||
ret.Thread = Thread;
|
||||
ret.Status = ExStatus::OK;
|
||||
goto Exit;
|
||||
}
|
||||
ret.Status = ExStatus::InvalidFileHeader;
|
||||
goto Exit;
|
||||
}
|
||||
case BinaryType::BinTypeELF:
|
||||
{
|
||||
const char *BaseName;
|
||||
cwk_path_get_basename(Path, &BaseName, nullptr);
|
||||
PCB *Process = TaskManager->CreateProcess(TaskManager->GetCurrentProcess(), BaseName, TaskTrustLevel::User);
|
||||
|
||||
void *BaseImage = KernelAllocator.RequestPages(TO_PAGES(ExFile->Node->Length));
|
||||
memcpy(BaseImage, (void *)ExFile->Node->Address, ExFile->Node->Length);
|
||||
|
||||
Memory::Virtual pva = Memory::Virtual(Process->PageTable);
|
||||
for (uint64_t i = 0; i < TO_PAGES(ExFile->Node->Length); i++)
|
||||
pva.Map((void *)((uint64_t)BaseImage + (i * PAGE_SIZE)), (void *)((uint64_t)BaseImage + (i * PAGE_SIZE)), Memory::PTFlag::RW | Memory::PTFlag::US);
|
||||
|
||||
Elf64_Ehdr *ELFHeader = (Elf64_Ehdr *)BaseImage;
|
||||
|
||||
TaskArchitecture Arch = TaskArchitecture::x64;
|
||||
TaskCompatibility Comp = TaskCompatibility::Native;
|
||||
if (ELFHeader->e_machine == EM_386)
|
||||
Arch = TaskArchitecture::x32;
|
||||
else if (ELFHeader->e_machine == EM_AMD64)
|
||||
Arch = TaskArchitecture::x64;
|
||||
else if (ELFHeader->e_machine == EM_AARCH64)
|
||||
Arch = TaskArchitecture::ARM64;
|
||||
else
|
||||
Arch = TaskArchitecture::UnknownArchitecture;
|
||||
|
||||
// TODO: Should I care about this?
|
||||
if (ELFHeader->e_ident[EI_CLASS] == ELFCLASS32)
|
||||
{
|
||||
if (ELFHeader->e_ident[EI_DATA] == ELFDATA2LSB)
|
||||
fixme("ELF32 LSB");
|
||||
else if (ELFHeader->e_ident[EI_DATA] == ELFDATA2MSB)
|
||||
fixme("ELF32 MSB");
|
||||
else
|
||||
fixme("ELF32 Unknown");
|
||||
}
|
||||
else if (ELFHeader->e_ident[EI_CLASS] == ELFCLASS64)
|
||||
{
|
||||
if (ELFHeader->e_ident[EI_DATA] == ELFDATA2LSB)
|
||||
fixme("ELF64 LSB");
|
||||
else if (ELFHeader->e_ident[EI_DATA] == ELFDATA2MSB)
|
||||
fixme("ELF64 MSB");
|
||||
else
|
||||
fixme("ELF64 Unknown");
|
||||
}
|
||||
else
|
||||
fixme("Unknown ELF");
|
||||
|
||||
if (ELFHeader->e_type == ET_EXEC)
|
||||
{
|
||||
trace("Executable");
|
||||
Elf64_Phdr *pheader = (Elf64_Phdr *)(((char *)BaseImage) + ELFHeader->e_phoff);
|
||||
void *Address = nullptr;
|
||||
for (int i = 0; i < ELFHeader->e_phnum; i++, pheader++)
|
||||
{
|
||||
const char *BaseName;
|
||||
cwk_path_get_basename(Path, &BaseName, nullptr);
|
||||
PCB *Process = TaskManager->CreateProcess(TaskManager->GetCurrentProcess(), BaseName, TaskTrustLevel::User);
|
||||
if (pheader->p_type != PT_LOAD)
|
||||
continue;
|
||||
Address = (void *)((uint64_t)pheader->p_vaddr + pheader->p_memsz);
|
||||
}
|
||||
void *Offset = KernelAllocator.RequestPages(TO_PAGES((uint64_t)Address));
|
||||
|
||||
void *BaseImage = KernelAllocator.RequestPages(TO_PAGES(ExFile->Node->Length));
|
||||
memcpy(BaseImage, (void *)ExFile->Node->Address, ExFile->Node->Length);
|
||||
for (uint64_t i = 0; i < TO_PAGES((uint64_t)Address); i++)
|
||||
pva.Map((void *)((uint64_t)Offset + (i * PAGE_SIZE)), (void *)((uint64_t)Offset + (i * PAGE_SIZE)), Memory::PTFlag::RW | Memory::PTFlag::US);
|
||||
|
||||
/*
|
||||
For some reason I can't map BaseImage.
|
||||
Neither can I in the Tasking->CreateThread function.
|
||||
Very strange.
|
||||
*/
|
||||
pheader = (Elf64_Phdr *)(((char *)BaseImage) + ELFHeader->e_phoff);
|
||||
for (int i = 0; i < ELFHeader->e_phnum; i++, pheader++)
|
||||
{
|
||||
if (pheader->p_type != PT_LOAD)
|
||||
continue;
|
||||
void *dst = (void *)((uint64_t)pheader->p_vaddr + (uint64_t)Offset);
|
||||
memset(dst, 0, pheader->p_memsz);
|
||||
memcpy(dst, ((char *)BaseImage) + pheader->p_offset, pheader->p_filesz);
|
||||
}
|
||||
|
||||
Memory::Virtual pva = Memory::Virtual(Process->PageTable);
|
||||
|
||||
for (uint64_t i = 0; i < TO_PAGES(ExFile->Node->Length); i++)
|
||||
pva.Map((void *)((uint64_t)BaseImage + (i * PAGE_SIZE)), (void *)((uint64_t)BaseImage + (i * PAGE_SIZE)), Memory::PTFlag::RW | Memory::PTFlag::US);
|
||||
|
||||
TCB *Thread = TaskManager->CreateThread(Process,
|
||||
(IP)FexHdr->Pointer,
|
||||
Arg0, Arg1,
|
||||
(IPOffset)BaseImage,
|
||||
TaskArchitecture::x64,
|
||||
TaskCompatibility::Native);
|
||||
ret.Process = Process;
|
||||
ret.Thread = Thread;
|
||||
ret.Status = ExStatus::OK;
|
||||
TCB *Thread = TaskManager->CreateThread(Process,
|
||||
(IP)ELFHeader->e_entry,
|
||||
Arg0, Arg1,
|
||||
(IPOffset)Offset,
|
||||
Arch,
|
||||
Comp);
|
||||
ret.Process = Process;
|
||||
ret.Thread = Thread;
|
||||
ret.Status = ExStatus::OK;
|
||||
goto Exit;
|
||||
}
|
||||
else if (ELFHeader->e_type == ET_DYN)
|
||||
{
|
||||
fixme("Shared Object");
|
||||
}
|
||||
else if (ELFHeader->e_type == ET_REL)
|
||||
{
|
||||
trace("Relocatable");
|
||||
void *EP = ELFLoadRel(ELFHeader);
|
||||
if (EP == (void *)0xdeadbeef || EP == 0x0)
|
||||
{
|
||||
ret.Status = ExStatus::InvalidFileEntryPoint;
|
||||
goto Exit;
|
||||
}
|
||||
TCB *Thread = TaskManager->CreateThread(Process,
|
||||
(IP)EP,
|
||||
Arg0, Arg1,
|
||||
(IPOffset)BaseImage,
|
||||
Arch,
|
||||
Comp);
|
||||
ret.Process = Process;
|
||||
ret.Thread = Thread;
|
||||
ret.Status = ExStatus::OK;
|
||||
goto Exit;
|
||||
}
|
||||
else if (ELFHeader->e_type == ET_CORE)
|
||||
{
|
||||
fixme("Core");
|
||||
}
|
||||
else
|
||||
{
|
||||
fixme("Unknown");
|
||||
}
|
||||
|
||||
ret.Status = ExStatus::InvalidFileHeader;
|
||||
goto Exit;
|
||||
}
|
||||
@ -83,6 +196,9 @@ namespace Execute
|
||||
}
|
||||
|
||||
Exit:
|
||||
if (ret.Status != ExStatus::OK)
|
||||
if (ret.Process)
|
||||
ret.Process->Status = TaskStatus::Terminated;
|
||||
vfs->Close(ExFile);
|
||||
return ret;
|
||||
}
|
||||
|
Reference in New Issue
Block a user