mirror of
https://github.com/Fennix-Project/Userspace.git
synced 2025-07-10 22:59:22 +00:00
Update userspace
This commit is contained in:
@ -5,22 +5,22 @@
|
||||
|
||||
uintptr_t RequestPages(size_t Count)
|
||||
{
|
||||
return syscall1(_RequestPages, Count);
|
||||
return syscall1(sys_RequestPages, Count);
|
||||
}
|
||||
|
||||
int FreePages(uintptr_t Address, size_t Count)
|
||||
{
|
||||
return syscall2(_FreePages, Address, Count);
|
||||
return syscall2(sys_FreePages, Address, Count);
|
||||
}
|
||||
|
||||
int IPC(int Command, int Type, int ID, int Flags, void *Buffer, size_t Size)
|
||||
{
|
||||
return syscall6(_IPC, (long)Command, (long)Type, (long)ID, (long)Flags, (long)Buffer, (long)Size);
|
||||
return syscall6(sys_IPC, (long)Command, (long)Type, (long)ID, (long)Flags, (long)Buffer, (long)Size);
|
||||
}
|
||||
|
||||
uintptr_t KernelCTL(int Command, uint64_t Arg1, uint64_t Arg2, uint64_t Arg3, uint64_t Arg4)
|
||||
{
|
||||
return syscall5(_KernelCTL, Command, Arg1, Arg2, Arg3, Arg4);
|
||||
return syscall5(sys_KernelCTL, Command, Arg1, Arg2, Arg3, Arg4);
|
||||
}
|
||||
|
||||
int abs(int i) { return i < 0 ? -i : i; }
|
||||
@ -112,38 +112,38 @@ int strcmp(const char *l, const char *r)
|
||||
|
||||
struct Elf64_Dyn ELFGetDynamicTag(char *Path, enum DynamicTags Tag)
|
||||
{
|
||||
void *KP = syscall2(_FileOpen, Path, (long)"r");
|
||||
if (KP == NULL)
|
||||
syscall1(_Exit, -0xF17E);
|
||||
int fd = syscall2(sys_FileOpen, Path, (long)"r");
|
||||
if (fd < 0)
|
||||
syscall1(sys_Exit, -0xF17E);
|
||||
|
||||
Elf64_Ehdr ELFHeader;
|
||||
syscall3(_FileRead, KP, &ELFHeader, sizeof(Elf64_Ehdr));
|
||||
syscall3(sys_FileRead, fd, &ELFHeader, sizeof(Elf64_Ehdr));
|
||||
|
||||
Elf64_Phdr ItrProgramHeader;
|
||||
for (Elf64_Half i = 0; i < ELFHeader.e_phnum; i++)
|
||||
{
|
||||
// memcpy(&ItrProgramHeader, (uint8_t *)ElfFile + ELFHeader.e_phoff + ELFHeader.e_phentsize * i, sizeof(Elf64_Phdr));
|
||||
syscall3(_FileSeek, KP, ELFHeader.e_phoff + ELFHeader.e_phentsize * i, SEEK_SET);
|
||||
syscall3(_FileRead, KP, &ItrProgramHeader, sizeof(Elf64_Phdr));
|
||||
syscall3(sys_FileSeek, fd, ELFHeader.e_phoff + ELFHeader.e_phentsize * i, SYSCALL_SEEK_SET);
|
||||
syscall3(sys_FileRead, fd, &ItrProgramHeader, sizeof(Elf64_Phdr));
|
||||
if (ItrProgramHeader.p_type == PT_DYNAMIC)
|
||||
{
|
||||
struct Elf64_Dyn Dynamic; // = (struct Elf64_Dyn *)((uint8_t *)ElfFile + ItrProgramHeader.p_offset);
|
||||
syscall3(_FileSeek, KP, ItrProgramHeader.p_offset, SEEK_SET);
|
||||
syscall3(_FileRead, KP, &Dynamic, ItrProgramHeader.p_filesz);
|
||||
syscall3(sys_FileSeek, fd, ItrProgramHeader.p_offset, SYSCALL_SEEK_SET);
|
||||
syscall3(sys_FileRead, fd, &Dynamic, ItrProgramHeader.p_filesz);
|
||||
for (size_t i = 0; i < ItrProgramHeader.p_filesz / sizeof(struct Elf64_Dyn); i++)
|
||||
{
|
||||
if (Dynamic.d_tag == Tag || Dynamic.d_tag == DT_NULL)
|
||||
{
|
||||
syscall1(_FileClose, KP);
|
||||
syscall1(sys_FileClose, fd);
|
||||
return Dynamic;
|
||||
}
|
||||
|
||||
syscall3(_FileSeek, KP, ItrProgramHeader.p_offset + (i + 1) * sizeof(struct Elf64_Dyn), SEEK_SET);
|
||||
syscall3(_FileRead, KP, &Dynamic, sizeof(struct Elf64_Dyn));
|
||||
syscall3(sys_FileSeek, fd, ItrProgramHeader.p_offset + (i + 1) * sizeof(struct Elf64_Dyn), SYSCALL_SEEK_SET);
|
||||
syscall3(sys_FileRead, fd, &Dynamic, sizeof(struct Elf64_Dyn));
|
||||
}
|
||||
}
|
||||
}
|
||||
syscall1(_FileClose, KP);
|
||||
syscall1(sys_FileClose, fd);
|
||||
return (struct Elf64_Dyn){0};
|
||||
}
|
||||
|
||||
@ -168,12 +168,12 @@ char *GetELFStringTable(Elf64_Ehdr *Header)
|
||||
|
||||
Elf64_Sym ELFLookupSymbol(char *Path, const char *Name)
|
||||
{
|
||||
void *KP = syscall2(_FileOpen, Path, (long)"r");
|
||||
if (KP == NULL)
|
||||
syscall1(_Exit, -0xF17E);
|
||||
int fd = syscall2(sys_FileOpen, Path, (long)"r");
|
||||
if (fd < 0)
|
||||
syscall1(sys_Exit, -0xF17E);
|
||||
|
||||
Elf64_Ehdr ELFHeader;
|
||||
syscall3(_FileRead, KP, &ELFHeader, sizeof(Elf64_Ehdr));
|
||||
syscall3(sys_FileRead, fd, &ELFHeader, sizeof(Elf64_Ehdr));
|
||||
|
||||
Elf64_Shdr SymbolTable;
|
||||
Elf64_Shdr StringTable;
|
||||
@ -183,20 +183,20 @@ Elf64_Sym ELFLookupSymbol(char *Path, const char *Name)
|
||||
for (Elf64_Half i = 0; i < ELFHeader.e_shnum; i++)
|
||||
{
|
||||
Elf64_Shdr shdr;
|
||||
syscall3(_FileSeek, KP,
|
||||
syscall3(sys_FileSeek, fd,
|
||||
ELFHeader.e_shoff + ELFHeader.e_shentsize * i,
|
||||
SEEK_SET);
|
||||
syscall3(_FileRead, KP, &shdr, sizeof(Elf64_Shdr));
|
||||
SYSCALL_SEEK_SET);
|
||||
syscall3(sys_FileRead, fd, &shdr, sizeof(Elf64_Shdr));
|
||||
|
||||
switch (shdr.sh_type)
|
||||
{
|
||||
case SHT_SYMTAB:
|
||||
{
|
||||
SymbolTable = shdr;
|
||||
syscall3(_FileSeek, KP,
|
||||
syscall3(sys_FileSeek, fd,
|
||||
ELFHeader.e_shoff + ELFHeader.e_shentsize * shdr.sh_link,
|
||||
SEEK_SET);
|
||||
syscall3(_FileRead, KP, &StringTable, sizeof(Elf64_Shdr));
|
||||
SYSCALL_SEEK_SET);
|
||||
syscall3(sys_FileRead, fd, &StringTable, sizeof(Elf64_Shdr));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
@ -208,7 +208,7 @@ Elf64_Sym ELFLookupSymbol(char *Path, const char *Name)
|
||||
|
||||
if (SymbolTable.sh_size == 0 || StringTable.sh_size == 0)
|
||||
{
|
||||
syscall1(_FileClose, KP);
|
||||
syscall1(sys_FileClose, fd);
|
||||
return (Elf64_Sym){0};
|
||||
}
|
||||
|
||||
@ -216,19 +216,19 @@ Elf64_Sym ELFLookupSymbol(char *Path, const char *Name)
|
||||
{
|
||||
// Symbol = (Elf64_Sym *)((uintptr_t)Header + SymbolTable->sh_offset + (i * sizeof(Elf64_Sym)));
|
||||
// String = (char *)((uintptr_t)Header + StringTable->sh_offset + Symbol->st_name);
|
||||
syscall3(_FileSeek, KP, SymbolTable.sh_offset + (i * sizeof(Elf64_Sym)), SEEK_SET);
|
||||
syscall3(_FileRead, KP, &Symbol, sizeof(Elf64_Sym));
|
||||
syscall3(sys_FileSeek, fd, SymbolTable.sh_offset + (i * sizeof(Elf64_Sym)), SYSCALL_SEEK_SET);
|
||||
syscall3(sys_FileRead, fd, &Symbol, sizeof(Elf64_Sym));
|
||||
|
||||
syscall3(_FileSeek, KP, StringTable.sh_offset + Symbol.st_name, SEEK_SET);
|
||||
syscall3(_FileRead, KP, &String, sizeof(char *));
|
||||
syscall3(sys_FileSeek, fd, StringTable.sh_offset + Symbol.st_name, SYSCALL_SEEK_SET);
|
||||
syscall3(sys_FileRead, fd, &String, sizeof(char *));
|
||||
|
||||
if (strcmp(String, Name) == 0)
|
||||
{
|
||||
syscall1(_FileClose, KP);
|
||||
syscall1(sys_FileClose, fd);
|
||||
return Symbol;
|
||||
}
|
||||
}
|
||||
|
||||
syscall1(_FileClose, KP);
|
||||
syscall1(sys_FileClose, fd);
|
||||
return (Elf64_Sym){0};
|
||||
}
|
||||
|
@ -90,9 +90,6 @@ void (*ELF_LAZY_RESOLVE_MAIN(struct LibsCollection *Info, long RelIndex))()
|
||||
which determines the end of the list. */
|
||||
while (CurLib->Valid)
|
||||
{
|
||||
KernelCTL(KCTL_GET_ABSOLUTE_PATH, CurLib->LibraryName,
|
||||
LibraryPathBuffer, sizeof(LibraryPathBuffer), 0);
|
||||
|
||||
PrintDbg("-- ");
|
||||
PrintDbg(LibraryPathBuffer);
|
||||
PrintDbg(" ");
|
||||
@ -105,46 +102,46 @@ void (*ELF_LAZY_RESOLVE_MAIN(struct LibsCollection *Info, long RelIndex))()
|
||||
Elf64_Ehdr lib_Header;
|
||||
Elf64_Ehdr app_Header;
|
||||
|
||||
void *KP_lib = syscall2(_FileOpen, LibraryPathBuffer, "r");
|
||||
void *KP_app = syscall2(_FileOpen, ParentPath, "r");
|
||||
int fd_lib = syscall2(sys_FileOpen, LibraryPathBuffer, "r");
|
||||
int fd_app = syscall2(sys_FileOpen, ParentPath, "r");
|
||||
|
||||
if (!KP_lib)
|
||||
if (fd_lib < 0)
|
||||
{
|
||||
PrintNL("Failed to open library");
|
||||
goto RetryNextLib;
|
||||
}
|
||||
|
||||
if (!KP_app)
|
||||
if (fd_app < 0)
|
||||
{
|
||||
PrintNL("Failed to open application");
|
||||
goto RetryNextLib;
|
||||
}
|
||||
|
||||
syscall3(_FileRead, KP_lib, &lib_Header, sizeof(Elf64_Ehdr));
|
||||
syscall3(_FileRead, KP_app, &app_Header, sizeof(Elf64_Ehdr));
|
||||
syscall3(sys_FileRead, fd_lib, &lib_Header, sizeof(Elf64_Ehdr));
|
||||
syscall3(sys_FileRead, fd_app, &app_Header, sizeof(Elf64_Ehdr));
|
||||
|
||||
Elf64_Phdr ItrProgramHeader;
|
||||
|
||||
for (Elf64_Half i = 0; i < lib_Header.e_phnum; i++)
|
||||
{
|
||||
syscall3(_FileSeek, KP_lib,
|
||||
syscall3(sys_FileSeek, fd_lib,
|
||||
lib_Header.e_phoff +
|
||||
lib_Header.e_phentsize * i,
|
||||
SEEK_SET);
|
||||
SYSCALL_SEEK_SET);
|
||||
|
||||
syscall3(_FileRead, KP_lib, &ItrProgramHeader, sizeof(Elf64_Phdr));
|
||||
syscall3(sys_FileRead, fd_lib, &ItrProgramHeader, sizeof(Elf64_Phdr));
|
||||
|
||||
lib_BaseAddress = MIN(lib_BaseAddress, ItrProgramHeader.p_vaddr);
|
||||
}
|
||||
|
||||
for (Elf64_Half i = 0; i < app_Header.e_phnum; i++)
|
||||
{
|
||||
syscall3(_FileSeek, KP_app,
|
||||
syscall3(sys_FileSeek, fd_app,
|
||||
app_Header.e_phoff +
|
||||
app_Header.e_phentsize * i,
|
||||
SEEK_SET);
|
||||
SYSCALL_SEEK_SET);
|
||||
|
||||
syscall3(_FileRead, KP_app, &ItrProgramHeader, sizeof(Elf64_Phdr));
|
||||
syscall3(sys_FileRead, fd_app, &ItrProgramHeader, sizeof(Elf64_Phdr));
|
||||
|
||||
app_BaseAddress = MIN(app_BaseAddress, ItrProgramHeader.p_vaddr);
|
||||
}
|
||||
@ -268,7 +265,7 @@ FailEnd:
|
||||
Print(DbgBuff);
|
||||
PrintNL(" not found");
|
||||
int ExitCode = 0x51801;
|
||||
syscall1(_Exit, ExitCode);
|
||||
syscall1(sys_Exit, ExitCode);
|
||||
while (1) // Make sure we don't return
|
||||
;
|
||||
}
|
||||
@ -280,9 +277,9 @@ int ld_main()
|
||||
uintptr_t KCTL_ret = KernelCTL(KCTL_IS_CRITICAL, 0, 0, 0, 0);
|
||||
do
|
||||
{
|
||||
syscall1(_Sleep, 250);
|
||||
syscall1(sys_Sleep, 250);
|
||||
KCTL_ret = KernelCTL(KCTL_IS_CRITICAL, 0, 0, 0, 0);
|
||||
} while (KCTL_ret == SYSCALL_ACCESS_DENIED);
|
||||
} while (KCTL_ret == false);
|
||||
|
||||
if (KCTL_ret == false)
|
||||
return -1;
|
||||
@ -307,111 +304,21 @@ bool ELFAddLazyResolverToGOT(void *MemoryImage, struct LibsCollection *Libs)
|
||||
/* Actual load */
|
||||
int ld_load(int argc, char *argv[], char *envp[])
|
||||
{
|
||||
PrintDbgNL("!");
|
||||
uintptr_t PageSize = KernelCTL(KCTL_GET_PAGE_SIZE, 0, 0, 0, 0);
|
||||
int PagesForIPCDataStruct = sizeof(InterpreterIPCData) / PageSize + 1;
|
||||
int PagesForLibsCollectionStruct = sizeof(struct LibsCollection) / PageSize + 1;
|
||||
|
||||
InterpreterIPCData *IPCBuffer =
|
||||
(InterpreterIPCData *)RequestPages(PagesForIPCDataStruct);
|
||||
|
||||
int IPC_ID = IPC(IPC_CREATE, IPC_TYPE_MessagePassing,
|
||||
0, 0, "LOAD", sizeof(InterpreterIPCData));
|
||||
while (true)
|
||||
{
|
||||
IPC(IPC_LISTEN, IPC_TYPE_MessagePassing, IPC_ID, 1, NULL, 0);
|
||||
IPC(IPC_WAIT, IPC_TYPE_MessagePassing, IPC_ID, 0, NULL, 0);
|
||||
int IPCResult = IPC(IPC_READ, IPC_TYPE_MessagePassing,
|
||||
IPC_ID, 0, IPCBuffer, PageSize);
|
||||
|
||||
if (IPCResult == IPC_E_CODE_Success)
|
||||
break;
|
||||
}
|
||||
|
||||
struct LibsCollection *LibsForLazyResolver =
|
||||
(struct LibsCollection *)RequestPages(PagesForLibsCollectionStruct);
|
||||
|
||||
for (short i = 0; i < 64; i++)
|
||||
{
|
||||
if (IPCBuffer->Libraries[i].Name[0] == '\0')
|
||||
break;
|
||||
|
||||
uintptr_t lib_mm_image =
|
||||
KernelCTL(KCTL_GET_ELF_LIB_MEMORY_IMAGE,
|
||||
(uint64_t)IPCBuffer->Libraries[i].Name, 0, 0, 0);
|
||||
if (lib_mm_image == 0)
|
||||
{
|
||||
enum SyscallsErrorCodes ret =
|
||||
KernelCTL(KCTL_REGISTER_ELF_LIB,
|
||||
(uint64_t)IPCBuffer->Libraries[i].Name,
|
||||
(uint64_t)IPCBuffer->Libraries[i].Name, 0, 0);
|
||||
if (ret != SYSCALL_OK)
|
||||
{
|
||||
PrintNL("Failed to register ELF lib");
|
||||
return -0x11B;
|
||||
}
|
||||
lib_mm_image = KernelCTL(KCTL_GET_ELF_LIB_MEMORY_IMAGE,
|
||||
(uint64_t)IPCBuffer->Libraries[i].Name, 0, 0, 0);
|
||||
}
|
||||
|
||||
if (LibsForLazyResolver->Next == NULL)
|
||||
{
|
||||
LibsForLazyResolver->Valid = true;
|
||||
LibsForLazyResolver->LibraryMemoryImage = (uintptr_t)lib_mm_image;
|
||||
LibsForLazyResolver->ParentMemoryImage = (uintptr_t)IPCBuffer->MemoryImage;
|
||||
for (short j = 0; j < sizeof(LibsForLazyResolver->LibraryName); j++)
|
||||
LibsForLazyResolver->LibraryName[j] = IPCBuffer->Libraries[i].Name[j];
|
||||
|
||||
LibsForLazyResolver->Next =
|
||||
(struct LibsCollection *)RequestPages(PagesForLibsCollectionStruct);
|
||||
memset(LibsForLazyResolver->Next, 0, sizeof(struct LibsCollection));
|
||||
continue;
|
||||
}
|
||||
|
||||
struct LibsCollection *CurrentLib = LibsForLazyResolver;
|
||||
while (CurrentLib->Next != NULL)
|
||||
CurrentLib = CurrentLib->Next;
|
||||
|
||||
CurrentLib->Valid = true;
|
||||
CurrentLib->LibraryMemoryImage = (uintptr_t)lib_mm_image;
|
||||
CurrentLib->ParentMemoryImage = (uintptr_t)IPCBuffer->MemoryImage;
|
||||
for (short j = 0; j < sizeof(LibsForLazyResolver->LibraryName); j++)
|
||||
CurrentLib->LibraryName[j] = IPCBuffer->Libraries[i].Name[j];
|
||||
|
||||
CurrentLib->Next =
|
||||
(struct LibsCollection *)RequestPages(PagesForLibsCollectionStruct);
|
||||
memset(CurrentLib->Next, 0, sizeof(struct LibsCollection));
|
||||
}
|
||||
|
||||
struct LibsCollection *CurrentLib = LibsForLazyResolver;
|
||||
|
||||
for (int i = 0; i < sizeof(ParentPath); i++)
|
||||
ParentPath[i] = IPCBuffer->Path[i];
|
||||
|
||||
if (!ELFAddLazyResolverToGOT(IPCBuffer->MemoryImage,
|
||||
LibsForLazyResolver))
|
||||
{
|
||||
PrintNL("Failed to add lazy resolver to GOT");
|
||||
return -0x607;
|
||||
}
|
||||
|
||||
void *KP = syscall2(_FileOpen, ParentPath, (long)"r");
|
||||
if (KP == NULL)
|
||||
{
|
||||
PrintNL("Failed to open file");
|
||||
syscall1(_Exit, -0xF17E);
|
||||
}
|
||||
|
||||
Elf64_Ehdr ELFHeader;
|
||||
syscall3(_FileRead, KP, &ELFHeader, sizeof(Elf64_Ehdr));
|
||||
|
||||
Elf64_Addr Entry = ELFHeader.e_entry;
|
||||
|
||||
syscall1(_FileClose, KP);
|
||||
|
||||
IPC(IPC_DELETE, IPC_TYPE_MessagePassing, IPC_ID, 0, NULL, 0);
|
||||
FreePages((uintptr_t)IPCBuffer, PagesForIPCDataStruct);
|
||||
|
||||
PrintDbgNL("Calling entry point");
|
||||
return ((int (*)(int, char *[], char *[]))Entry)(argc, argv, envp);
|
||||
|
||||
// void *KP = syscall2(sys_FileOpen, ParentPath, (long)"r");
|
||||
// if (KP == NULL)
|
||||
// {
|
||||
// PrintNL("Failed to open file");
|
||||
// syscall1(sys_Exit, -0xF17E);
|
||||
// }
|
||||
|
||||
// Elf64_Ehdr ELFHeader;
|
||||
// syscall3(sys_FileRead, KP, &ELFHeader, sizeof(Elf64_Ehdr));
|
||||
|
||||
// Elf64_Addr Entry = ELFHeader.e_entry;
|
||||
|
||||
// syscall1(sys_FileClose, KP);
|
||||
|
||||
// return ((int (*)(int, char *[], char *[]))Entry)(argc, argv, envp);
|
||||
}
|
||||
|
Reference in New Issue
Block a user