Update userspace

This commit is contained in:
Alex
2023-10-09 01:25:55 +03:00
parent 883d2e3481
commit 17787dbc9b
17 changed files with 111 additions and 196 deletions

View File

@ -5,22 +5,15 @@
uintptr_t RequestPages(size_t Count)
{
return syscall1(sys_RequestPages, Count);
return syscall6(sc_mmap, NULL, Count * 0x1000,
sc_PROT_READ | sc_PROT_WRITE,
sc_MAP_ANONYMOUS | sc_MAP_PRIVATE,
0, 0);
}
int FreePages(uintptr_t Address, size_t Count)
{
return syscall2(sys_FreePages, Address, Count);
}
int IPC(int Command, int Type, int ID, int Flags, void *Buffer, size_t 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(sys_KernelCTL, Command, Arg1, Arg2, Arg3, Arg4);
return syscall2(sc_munmap, Address, Count * 0x1000);
}
int abs(int i) { return i < 0 ? -i : i; }
@ -112,38 +105,38 @@ int strcmp(const char *l, const char *r)
struct Elf64_Dyn ELFGetDynamicTag(char *Path, enum DynamicTags Tag)
{
int fd = syscall2(sys_FileOpen, Path, (long)"r");
int fd = syscall2(sc_open, Path, (long)"r");
if (fd < 0)
syscall1(sys_Exit, -0xF17E);
syscall1(sc_exit, -0xF17E);
Elf64_Ehdr ELFHeader;
syscall3(sys_FileRead, fd, &ELFHeader, sizeof(Elf64_Ehdr));
syscall3(sc_read, 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(sys_FileSeek, fd, ELFHeader.e_phoff + ELFHeader.e_phentsize * i, SYSCALL_SEEK_SET);
syscall3(sys_FileRead, fd, &ItrProgramHeader, sizeof(Elf64_Phdr));
syscall3(sc_lseek, fd, ELFHeader.e_phoff + ELFHeader.e_phentsize * i, sc_SEEK_SET);
syscall3(sc_read, 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(sys_FileSeek, fd, ItrProgramHeader.p_offset, SYSCALL_SEEK_SET);
syscall3(sys_FileRead, fd, &Dynamic, ItrProgramHeader.p_filesz);
syscall3(sc_lseek, fd, ItrProgramHeader.p_offset, sc_SEEK_SET);
syscall3(sc_read, 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(sys_FileClose, fd);
syscall1(sc_close, fd);
return Dynamic;
}
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));
syscall3(sc_lseek, fd, ItrProgramHeader.p_offset + (i + 1) * sizeof(struct Elf64_Dyn), sc_SEEK_SET);
syscall3(sc_read, fd, &Dynamic, sizeof(struct Elf64_Dyn));
}
}
}
syscall1(sys_FileClose, fd);
syscall1(sc_close, fd);
return (struct Elf64_Dyn){0};
}
@ -168,12 +161,12 @@ char *GetELFStringTable(Elf64_Ehdr *Header)
Elf64_Sym ELFLookupSymbol(char *Path, const char *Name)
{
int fd = syscall2(sys_FileOpen, Path, (long)"r");
int fd = syscall2(sc_open, Path, (long)"r");
if (fd < 0)
syscall1(sys_Exit, -0xF17E);
syscall1(sc_exit, -0xF17E);
Elf64_Ehdr ELFHeader;
syscall3(sys_FileRead, fd, &ELFHeader, sizeof(Elf64_Ehdr));
syscall3(sc_read, fd, &ELFHeader, sizeof(Elf64_Ehdr));
Elf64_Shdr SymbolTable;
Elf64_Shdr StringTable;
@ -183,20 +176,20 @@ Elf64_Sym ELFLookupSymbol(char *Path, const char *Name)
for (Elf64_Half i = 0; i < ELFHeader.e_shnum; i++)
{
Elf64_Shdr shdr;
syscall3(sys_FileSeek, fd,
syscall3(sc_lseek, fd,
ELFHeader.e_shoff + ELFHeader.e_shentsize * i,
SYSCALL_SEEK_SET);
syscall3(sys_FileRead, fd, &shdr, sizeof(Elf64_Shdr));
sc_SEEK_SET);
syscall3(sc_read, fd, &shdr, sizeof(Elf64_Shdr));
switch (shdr.sh_type)
{
case SHT_SYMTAB:
{
SymbolTable = shdr;
syscall3(sys_FileSeek, fd,
syscall3(sc_lseek, fd,
ELFHeader.e_shoff + ELFHeader.e_shentsize * shdr.sh_link,
SYSCALL_SEEK_SET);
syscall3(sys_FileRead, fd, &StringTable, sizeof(Elf64_Shdr));
sc_SEEK_SET);
syscall3(sc_read, fd, &StringTable, sizeof(Elf64_Shdr));
break;
}
default:
@ -208,7 +201,7 @@ Elf64_Sym ELFLookupSymbol(char *Path, const char *Name)
if (SymbolTable.sh_size == 0 || StringTable.sh_size == 0)
{
syscall1(sys_FileClose, fd);
syscall1(sc_close, fd);
return (Elf64_Sym){0};
}
@ -216,19 +209,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(sys_FileSeek, fd, SymbolTable.sh_offset + (i * sizeof(Elf64_Sym)), SYSCALL_SEEK_SET);
syscall3(sys_FileRead, fd, &Symbol, sizeof(Elf64_Sym));
syscall3(sc_lseek, fd, SymbolTable.sh_offset + (i * sizeof(Elf64_Sym)), sc_SEEK_SET);
syscall3(sc_read, fd, &Symbol, sizeof(Elf64_Sym));
syscall3(sys_FileSeek, fd, StringTable.sh_offset + Symbol.st_name, SYSCALL_SEEK_SET);
syscall3(sys_FileRead, fd, &String, sizeof(char *));
syscall3(sc_lseek, fd, StringTable.sh_offset + Symbol.st_name, sc_SEEK_SET);
syscall3(sc_read, fd, &String, sizeof(char *));
if (strcmp(String, Name) == 0)
{
syscall1(sys_FileClose, fd);
syscall1(sc_close, fd);
return Symbol;
}
}
syscall1(sys_FileClose, fd);
syscall1(sc_close, fd);
return (Elf64_Sym){0};
}

View File

@ -102,8 +102,8 @@ void (*ELF_LAZY_RESOLVE_MAIN(struct LibsCollection *Info, long RelIndex))()
Elf64_Ehdr lib_Header;
Elf64_Ehdr app_Header;
int fd_lib = syscall2(sys_FileOpen, LibraryPathBuffer, "r");
int fd_app = syscall2(sys_FileOpen, ParentPath, "r");
int fd_lib = syscall2(sc_open, LibraryPathBuffer, "r");
int fd_app = syscall2(sc_open, ParentPath, "r");
if (fd_lib < 0)
{
@ -117,31 +117,31 @@ void (*ELF_LAZY_RESOLVE_MAIN(struct LibsCollection *Info, long RelIndex))()
goto RetryNextLib;
}
syscall3(sys_FileRead, fd_lib, &lib_Header, sizeof(Elf64_Ehdr));
syscall3(sys_FileRead, fd_app, &app_Header, sizeof(Elf64_Ehdr));
syscall3(sc_read, fd_lib, &lib_Header, sizeof(Elf64_Ehdr));
syscall3(sc_read, fd_app, &app_Header, sizeof(Elf64_Ehdr));
Elf64_Phdr ItrProgramHeader;
for (Elf64_Half i = 0; i < lib_Header.e_phnum; i++)
{
syscall3(sys_FileSeek, fd_lib,
syscall3(sc_lseek, fd_lib,
lib_Header.e_phoff +
lib_Header.e_phentsize * i,
SYSCALL_SEEK_SET);
sc_SEEK_SET);
syscall3(sys_FileRead, fd_lib, &ItrProgramHeader, sizeof(Elf64_Phdr));
syscall3(sc_read, 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(sys_FileSeek, fd_app,
syscall3(sc_lseek, fd_app,
app_Header.e_phoff +
app_Header.e_phentsize * i,
SYSCALL_SEEK_SET);
sc_SEEK_SET);
syscall3(sys_FileRead, fd_app, &ItrProgramHeader, sizeof(Elf64_Phdr));
syscall3(sc_read, fd_app, &ItrProgramHeader, sizeof(Elf64_Phdr));
app_BaseAddress = MIN(app_BaseAddress, ItrProgramHeader.p_vaddr);
}
@ -265,7 +265,7 @@ FailEnd:
Print(DbgBuff);
PrintNL(" not found");
int ExitCode = 0x51801;
syscall1(sys_Exit, ExitCode);
syscall1(sc_exit, ExitCode);
while (1) // Make sure we don't return
;
}
@ -274,15 +274,15 @@ FailEnd:
int ld_main()
{
/* Prevent race condition. */
uintptr_t KCTL_ret = KernelCTL(KCTL_IS_CRITICAL, 0, 0, 0, 0);
do
{
syscall1(sys_Sleep, 250);
KCTL_ret = KernelCTL(KCTL_IS_CRITICAL, 0, 0, 0, 0);
} while (KCTL_ret == false);
// uintptr_t KCTL_ret = KernelCTL(KCTL_IS_CRITICAL, 0, 0, 0, 0);
// do
// {
// syscall1(sys_Sleep, 250);
// KCTL_ret = KernelCTL(KCTL_IS_CRITICAL, 0, 0, 0, 0);
// } while (KCTL_ret == false);
if (KCTL_ret == false)
return -1;
// if (KCTL_ret == false)
// return -1;
/* Everything is ok, continue. */
return 0;
@ -306,7 +306,7 @@ int ld_load(int argc, char *argv[], char *envp[])
{
PrintDbgNL("Calling entry point");
// void *KP = syscall2(sys_FileOpen, ParentPath, (long)"r");
// void *KP = syscall2(sc_open, ParentPath, (long)"r");
// if (KP == NULL)
// {
// PrintNL("Failed to open file");
@ -314,7 +314,7 @@ int ld_load(int argc, char *argv[], char *envp[])
// }
// Elf64_Ehdr ELFHeader;
// syscall3(sys_FileRead, KP, &ELFHeader, sizeof(Elf64_Ehdr));
// syscall3(sc_read, KP, &ELFHeader, sizeof(Elf64_Ehdr));
// Elf64_Addr Entry = ELFHeader.e_entry;