Update kernel

This commit is contained in:
Alex
2023-06-10 13:11:25 +03:00
parent dcdba03426
commit 41db477173
82 changed files with 6342 additions and 4079 deletions

View File

@ -23,126 +23,168 @@
#include <filesystem.hpp>
#include <task.hpp>
#include <std.hpp>
#include <vector>
#include <elf.h>
namespace Execute
{
enum BinaryType
{
BinTypeInvalid,
BinTypeFex,
BinTypeELF,
BinTypePE,
BinTypeNE,
BinTypeMZ,
BinTypeUnknown
};
enum BinaryType
{
BinTypeInvalid,
BinTypeFex,
BinTypeELF,
BinTypePE,
BinTypeNE,
BinTypeMZ,
BinTypeUnknown
};
enum ExStatus
{
Unknown,
OK,
Unsupported,
GenericError,
InvalidFile,
InvalidFileFormat,
InvalidFileHeader,
InvalidFileData,
InvalidFileEntryPoint,
InvalidFilePath
};
enum ExStatus
{
Unknown,
OK,
Unsupported,
GenericError,
LoadingProcedureFailed,
InvalidFile,
InvalidFileFormat,
InvalidFileHeader,
InvalidFileData,
InvalidFileEntryPoint,
InvalidFilePath
};
struct SpawnData
{
ExStatus Status;
Tasking::PCB *Process;
Tasking::TCB *Thread;
};
struct SpawnData
{
ExStatus Status;
Tasking::PCB *Process;
Tasking::TCB *Thread;
};
struct SharedLibraries
{
char Identifier[64];
uint64_t Timeout;
int RefCount;
struct SharedLibrary
{
char Identifier[64];
char Path[256];
uint64_t Timeout;
int RefCount;
uintptr_t Address;
uintptr_t MemoryImage;
size_t Length;
};
uintptr_t MemoryImage;
size_t Length;
};
struct ELFBaseLoad
{
bool Success;
bool Interpreter;
SpawnData sd;
Tasking::IP InstructionPointer;
struct ELFBaseLoad
{
bool Success;
bool Interpreter;
SpawnData sd;
Tasking::IP InstructionPointer;
std::vector<const char *> NeededLibraries;
void *MemoryImage;
void *VirtualMemoryImage;
std::vector<const char *> NeededLibraries;
void *MemoryImage;
void *VirtualMemoryImage;
/* This should be deleted after copying the allocated pages to the thread
Intended to be used only inside BaseLoad.cpp */
Memory::MemMgr *TmpMem;
/* This should be deleted after copying the allocated pages to the thread
Intended to be used only inside BaseLoad.cpp */
Memory::MemMgr *TmpMem;
/* Same as above, for BaseLoad.cpp only */
std::vector<AuxiliaryVector> auxv;
};
/* Same as above, for BaseLoad.cpp only */
std::vector<AuxiliaryVector> auxv;
};
struct MmImage
{
void *Physical;
void *Virtual;
};
struct MmImage
{
void *Physical;
void *Virtual;
};
BinaryType GetBinaryType(void *Image);
BinaryType GetBinaryType(char *Path);
class ELFObject
{
private:
ELFBaseLoad BaseLoadInfo{};
SpawnData Spawn(char *Path, const char **argv, const char **envp);
ELFBaseLoad LoadExec_x86_32(VirtualFileSystem::File &ElfFile,
Tasking::PCB *TargetProcess);
ELFBaseLoad ELFLoad(char *Path, const char **argv, const char **envp,
Tasking::TaskCompatibility Compatibility = Tasking::TaskCompatibility::Native);
ELFBaseLoad LoadExec_x86_64(VirtualFileSystem::File &ElfFile,
Tasking::PCB *TargetProcess);
Elf64_Shdr *GetELFSheader(Elf64_Ehdr *Header);
Elf64_Shdr *GetELFSection(Elf64_Ehdr *Header, uint64_t Index);
char *GetELFStringTable(Elf64_Ehdr *Header);
char *ELFLookupString(Elf64_Ehdr *Header, uintptr_t Offset);
Elf64_Sym *ELFLookupSymbol(Elf64_Ehdr *Header, const char *Name);
uintptr_t ELFGetSymbolValue(Elf64_Ehdr *Header, uint64_t Table, uint64_t Index);
Elf64_Dyn *ELFGetDynamicTag(void *ElfFile, enum DynamicArrayTags Tag);
ELFBaseLoad LoadDyn_x86_32(VirtualFileSystem::File &ElfFile,
Tasking::PCB *TargetProcess,
bool IsLibrary);
/**
* @brief Create a ELF Memory Image
*
* @param mem The memory manager to use
* @param pV Memory::Virtual object to use
* @param ElfFile ELF file loaded in memory (FULL FILE)
* @param Length Length of @p ElfFile
* @return The Memory Image (Physical and Virtual)
*/
MmImage ELFCreateMemoryImage(Memory::MemMgr *mem, Memory::Virtual &pV, void *ElfFile, size_t Length);
ELFBaseLoad LoadDyn_x86_64(VirtualFileSystem::File &ElfFile,
Tasking::PCB *TargetProcess,
bool IsLibrary);
uintptr_t LoadELFInterpreter(Memory::MemMgr *mem, Memory::Virtual &pV, const char *Interpreter);
public:
ELFBaseLoad GetBaseLoadInfo() { return BaseLoadInfo; }
bool IsValid() { return BaseLoadInfo.Success; }
ELFBaseLoad ELFLoadRel(void *ElfFile,
VirtualFileSystem::File &ExFile,
Tasking::PCB *Process);
ELFObject(char *AbsolutePath,
Tasking::PCB *TargetProcess,
bool IsLibrary = false);
~ELFObject();
};
ELFBaseLoad ELFLoadExec(void *ElfFile,
VirtualFileSystem::File &ExFile,
Tasking::PCB *Process);
/* Full binary size. */
BinaryType GetBinaryType(void *Image);
ELFBaseLoad ELFLoadDyn(void *ElfFile,
VirtualFileSystem::File &ExFile,
Tasking::PCB *Process);
BinaryType GetBinaryType(char *Path);
void StartExecuteService();
bool AddLibrary(char *Identifier,
void *ElfImage,
size_t Length,
const Memory::Virtual &pV = Memory::Virtual());
void SearchLibrary(char *Identifier);
SharedLibraries GetLibrary(char *Identifier);
SpawnData Spawn(char *Path, const char **argv, const char **envp);
ELFBaseLoad ELFLoad(char *Path, const char **argv, const char **envp,
Tasking::TaskCompatibility Compatibility = Tasking::TaskCompatibility::Native);
void ELFInterpreterIPCThread(Tasking::PCB *TargetProcess,
std::string *TargetPath,
void *MemoryImage,
std::vector<const char *> *NeededLibraries);
bool ELFIs64(void *Header);
Elf64_Shdr *GetELFSheader(Elf64_Ehdr *Header);
Elf64_Shdr *GetELFSection(Elf64_Ehdr *Header, uint64_t Index);
char *GetELFStringTable(Elf64_Ehdr *Header);
char *ELFLookupString(Elf64_Ehdr *Header, uintptr_t Offset);
Elf64_Sym *ELFLookupSymbol(Elf64_Ehdr *Header, const char *Name);
Elf64_Sym ELFLookupSymbol(VirtualFileSystem::File &ElfFile, const char *Name);
uintptr_t ELFGetSymbolValue(Elf64_Ehdr *Header, uint64_t Table, uint64_t Index);
std::vector<Elf64_Phdr> ELFGetSymbolType_x86_64(VirtualFileSystem::File &ElfFile, SegmentTypes Tag);
std::vector<Elf32_Phdr> ELFGetSymbolType_x86_32(VirtualFileSystem::File &ElfFile, SegmentTypes Tag);
std::vector<Elf64_Shdr> ELFGetSections_x86_64(VirtualFileSystem::File &ElfFile, const char *SectionName);
std::vector<Elf32_Shdr> ELFGetSections_x86_32(VirtualFileSystem::File &ElfFile, const char *SectionName);
std::vector<Elf64_Dyn> ELFGetDynamicTag_x86_64(VirtualFileSystem::File &ElfFile, DynamicArrayTags Tag);
std::vector<Elf32_Dyn> ELFGetDynamicTag_x86_32(VirtualFileSystem::File &ElfFile, DynamicArrayTags Tag);
void CopyLOADSegments(VirtualFileSystem::File &ElfFile, uintptr_t HdrsBase, uintptr_t PhysicalBase);
void GetBaseAndSize(VirtualFileSystem::File &ElfFile, uintptr_t &Base, size_t &Size);
/**
* @brief Create a ELF Memory Image
*
* @param mem The memory manager to use
* @param vmm Memory::Virtual object to use
* @param ElfFile The ELF File
* @param Length Length of @p ElfFile
* @return The Memory Image (Physical and Virtual)
*/
MmImage ELFCreateMemoryImage(Memory::MemMgr *mem,
Memory::Virtual &vmm,
VirtualFileSystem::File &ElfFile,
size_t Length);
uintptr_t LoadELFInterpreter(Memory::MemMgr *mem,
Memory::Virtual &vmm,
const char *Interpreter);
void LibraryManagerService();
bool AddLibrary(char *Identifier,
VirtualFileSystem::File &ExFile,
const Memory::Virtual &vmm = Memory::Virtual());
SharedLibrary GetLibrary(char *Identifier);
}
#endif // !__FENNIX_KERNEL_FILE_EXECUTE_H__