mirror of
https://github.com/EnderIce2/Fennix.git
synced 2025-05-31 16:57:58 +00:00
Added usermode process creation (currently broken)
This commit is contained in:
parent
8cc9ff4ff3
commit
5de0a3972c
@ -1,6 +1,36 @@
|
|||||||
#include <exec.hpp>
|
#include <exec.hpp>
|
||||||
|
|
||||||
|
#include "../kernel.h"
|
||||||
|
#include "../Fex.hpp"
|
||||||
|
|
||||||
namespace Execute
|
namespace Execute
|
||||||
{
|
{
|
||||||
|
BinaryType GetBinaryType(char *Path)
|
||||||
|
{
|
||||||
|
BinaryType Type = BinaryType::BinTypeInvalid;
|
||||||
|
FileSystem::FILE *ExFile = vfs->Open(Path);
|
||||||
|
|
||||||
|
if (ExFile->Status == FileSystem::FileStatus::OK)
|
||||||
|
{
|
||||||
|
if (ExFile->Node->Flags == FileSystem::NodeFlags::FS_FILE)
|
||||||
|
{
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
Type = BinaryType::BinTypeFex;
|
||||||
|
goto Exit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ... */
|
||||||
|
|
||||||
|
Type = BinaryType::BinTypeUnknown;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Exit:
|
||||||
|
vfs->Close(ExFile);
|
||||||
|
return Type;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,81 @@
|
|||||||
#include <exec.hpp>
|
#include <exec.hpp>
|
||||||
|
|
||||||
|
#include <memory.hpp>
|
||||||
|
#include <lock.hpp>
|
||||||
|
#include <cwalk.h>
|
||||||
|
|
||||||
|
#include "../kernel.h"
|
||||||
|
#include "../Fex.hpp"
|
||||||
|
|
||||||
|
using namespace Tasking;
|
||||||
|
|
||||||
namespace Execute
|
namespace Execute
|
||||||
{
|
{
|
||||||
ExStatus Spawn(char *Path, uint64_t Arg0, uint64_t Arg1)
|
SpawnData Spawn(char *Path, uint64_t Arg0, uint64_t Arg1)
|
||||||
{
|
{
|
||||||
return ExStatus::Unknown;
|
SpawnData ret = {.Status = ExStatus::Unknown,
|
||||||
|
.Process = nullptr,
|
||||||
|
.Thread = nullptr};
|
||||||
|
FileSystem::FILE *ExFile = vfs->Open(Path);
|
||||||
|
if (ExFile->Status == FileSystem::FileStatus::OK)
|
||||||
|
{
|
||||||
|
if (ExFile->Node->Flags == FileSystem::NodeFlags::FS_FILE)
|
||||||
|
{
|
||||||
|
BinaryType Type = GetBinaryType(Path);
|
||||||
|
switch (Type)
|
||||||
|
{
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
|
||||||
|
for (uint64_t i = 0; i < TO_PAGES(ExFile->Node->Length); i++)
|
||||||
|
Memory::Virtual(Process->PageTable).Map((void *)((uint64_t)BaseImage + (i * PAGE_SIZE)), (void *)((uint64_t)BaseImage + (i * PAGE_SIZE)), 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;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
ret.Status = ExStatus::Unsupported;
|
||||||
|
goto Exit;
|
||||||
|
}
|
||||||
|
goto Exit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (ExFile->Status == FileSystem::FileStatus::NOT_FOUND)
|
||||||
|
{
|
||||||
|
ret.Status = ExStatus::InvalidFilePath;
|
||||||
|
goto Exit;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ret.Status = ExStatus::InvalidFile;
|
||||||
|
goto Exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
Exit:
|
||||||
|
vfs->Close(ExFile);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -79,13 +79,15 @@ void KernelMainThread()
|
|||||||
argc = argv.size() - 1;
|
argc = argv.size() - 1;
|
||||||
|
|
||||||
// TODO: Untested!
|
// TODO: Untested!
|
||||||
Execute::ExStatus ret = Execute::Spawn(Config.InitPath, argc, (uint64_t)argv.data());
|
Execute::SpawnData ret = Execute::Spawn(Config.InitPath, argc, (uint64_t)argv.data());
|
||||||
if (ret != Execute::ExStatus::OK)
|
if (ret.Status != Execute::ExStatus::OK)
|
||||||
{
|
{
|
||||||
KPrint("\eE85230Failed to start %s! Code: %d", Config.InitPath, ret);
|
KPrint("\eE85230Failed to start %s! Code: %d", Config.InitPath, ret);
|
||||||
CPU::Halt(true);
|
CPU::Halt(true);
|
||||||
}
|
}
|
||||||
TaskManager->GetCurrentThread()->SetPriority(1);
|
TaskManager->GetCurrentThread()->SetPriority(1);
|
||||||
|
TaskManager->WaitForThread(ret.Thread);
|
||||||
|
KPrint("\eE85230Userspace process exited with code %d", ret.Thread->GetExitCode());
|
||||||
CPU::Halt(true);
|
CPU::Halt(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,12 +3,24 @@
|
|||||||
|
|
||||||
#include <types.h>
|
#include <types.h>
|
||||||
|
|
||||||
|
#include <task.hpp>
|
||||||
|
|
||||||
namespace Execute
|
namespace Execute
|
||||||
{
|
{
|
||||||
|
enum BinaryType
|
||||||
|
{
|
||||||
|
BinTypeInvalid,
|
||||||
|
BinTypeFex,
|
||||||
|
BinTypeElf,
|
||||||
|
BinTypePE,
|
||||||
|
BinTypeUnknown
|
||||||
|
};
|
||||||
|
|
||||||
enum ExStatus
|
enum ExStatus
|
||||||
{
|
{
|
||||||
OK,
|
OK,
|
||||||
Unknown,
|
Unknown,
|
||||||
|
Unsupported,
|
||||||
InvalidFile,
|
InvalidFile,
|
||||||
InvalidFileFormat,
|
InvalidFileFormat,
|
||||||
InvalidFileHeader,
|
InvalidFileHeader,
|
||||||
@ -17,7 +29,15 @@ namespace Execute
|
|||||||
InvalidFilePath
|
InvalidFilePath
|
||||||
};
|
};
|
||||||
|
|
||||||
ExStatus Spawn(char *Path, uint64_t Arg0, uint64_t Arg1);
|
struct SpawnData
|
||||||
|
{
|
||||||
|
ExStatus Status;
|
||||||
|
Tasking::PCB *Process;
|
||||||
|
Tasking::TCB *Thread;
|
||||||
|
};
|
||||||
|
|
||||||
|
BinaryType GetBinaryType(char *Path);
|
||||||
|
SpawnData Spawn(char *Path, uint64_t Arg0, uint64_t Arg1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // !__FENNIX_KERNEL_FILE_EXECUTE_H__
|
#endif // !__FENNIX_KERNEL_FILE_EXECUTE_H__
|
||||||
|
Loading…
x
Reference in New Issue
Block a user