From ff468db6754acbc455bcc14fe991716655433863 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 11 Mar 2023 10:34:08 +0100 Subject: [PATCH] kernel/ELF+Scheduler: Use VFS instead of TarStream --- kernel/src/ELF.cpp | 16 ++++++++-------- kernel/src/ELF.h | 4 ++-- kernel/src/main.cpp | 13 ++----------- kernel/src/thread/Scheduler.cpp | 4 ++-- kernel/src/thread/Scheduler.h | 4 ++-- 5 files changed, 16 insertions(+), 25 deletions(-) diff --git a/kernel/src/ELF.cpp b/kernel/src/ELF.cpp index 1af55e15..955706ea 100644 --- a/kernel/src/ELF.cpp +++ b/kernel/src/ELF.cpp @@ -26,13 +26,14 @@ static bool can_write_segment(u32 flags) namespace ELFLoader { // FIXME: Check that all calls to read_contents() read the proper amount of bytes. - Result load(const TarStream::Entry& elf_entry, const TarStream& stream) + Result load(SharedPtr inode) { Elf64_Ehdr elf_header; - usize nread = stream.read_contents(elf_entry, &elf_header, 0, sizeof elf_header); + usize nread = TRY(inode->read((u8*)&elf_header, 0, sizeof elf_header)); + if (nread < sizeof elf_header) { - kdbgln("Error while loading ELF: ELF header does not fit in entry"); + kdbgln("Error while loading ELF: ELF header does not fit in file"); return err(ENOEXEC); } @@ -78,10 +79,10 @@ namespace ELFLoader usize i; Elf64_Phdr program_header; - for (stream.read_contents(elf_entry, &program_header, elf_header.e_phoff, sizeof program_header), i = 0; + for (TRY(inode->read((u8*)&program_header, elf_header.e_phoff, sizeof program_header)), i = 0; i < elf_header.e_phnum; - i++, stream.read_contents(elf_entry, &program_header, elf_header.e_phoff + (i * elf_header.e_phentsize), - sizeof program_header)) + i++, TRY(inode->read((u8*)&program_header, elf_header.e_phoff + (i * elf_header.e_phentsize), + sizeof program_header))) { if (program_header.p_type == PT_LOAD) { @@ -103,8 +104,7 @@ namespace ELFLoader base_vaddr, get_blocks_from_size(program_header.p_memsz + vaddr_diff, ARCH_PAGE_SIZE), flags)); // Load the file section of the segment - stream.read_contents(elf_entry, (void*)program_header.p_vaddr, program_header.p_offset, - program_header.p_filesz); + inode->read((u8*)program_header.p_vaddr, program_header.p_offset, program_header.p_filesz); // Fill out the rest of the segment with 0s memset((void*)(program_header.p_vaddr + program_header.p_filesz), 0, diff --git a/kernel/src/ELF.h b/kernel/src/ELF.h index 440bae4b..d5217c48 100644 --- a/kernel/src/ELF.h +++ b/kernel/src/ELF.h @@ -1,6 +1,6 @@ #pragma once +#include "fs/VFS.h" #include -#include #include #define ELFMAG "\177ELF" @@ -54,5 +54,5 @@ struct ELFData namespace ELFLoader { - Result load(const TarStream::Entry& elf_entry, const TarStream& stream); + Result load(SharedPtr inode); }; diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp index eceebffd..6688076a 100644 --- a/kernel/src/main.cpp +++ b/kernel/src/main.cpp @@ -63,18 +63,9 @@ static void init_vfs() static Result try_init_userspace() { - TarStream::Entry entry; - g_initrd.rewind(); - while (TRY(g_initrd.read_next_entry(entry))) - { - if (entry.type == TarStream::EntryType::RegularFile) - { - kinfoln("Found file %s in initial ramdisk, of size %s and mode %#ho", entry.name, - to_dynamic_unit(entry.size).release_value().chars(), entry.mode); + auto app = TRY(VFS::resolve_path("/bin/app")); - if (!strcmp(entry.name, "bin/app")) { TRY(Scheduler::new_userspace_thread(entry, g_initrd)); } - } - } + TRY(Scheduler::new_userspace_thread(app)); return {}; } diff --git a/kernel/src/thread/Scheduler.cpp b/kernel/src/thread/Scheduler.cpp index 286e1674..0f1eb7cb 100644 --- a/kernel/src/thread/Scheduler.cpp +++ b/kernel/src/thread/Scheduler.cpp @@ -118,7 +118,7 @@ namespace Scheduler return {}; } - Result new_userspace_thread(const TarStream::Entry& entry, const TarStream& stream) + Result new_userspace_thread(SharedPtr inode) { Thread* const thread = TRY(new_thread()); @@ -139,7 +139,7 @@ namespace Scheduler thread->init_regs_user(); - const ELFData data = TRY(ELFLoader::load(entry, stream)); + const ELFData data = TRY(ELFLoader::load(inode)); thread->set_ip(data.entry); diff --git a/kernel/src/thread/Scheduler.h b/kernel/src/thread/Scheduler.h index c2dd4cfc..fecee620 100644 --- a/kernel/src/thread/Scheduler.h +++ b/kernel/src/thread/Scheduler.h @@ -1,6 +1,6 @@ #pragma once +#include "fs/VFS.h" #include "thread/Thread.h" -#include namespace Scheduler { @@ -13,7 +13,7 @@ namespace Scheduler Result new_kernel_thread(void (*func)(void)); Result new_kernel_thread(void (*func)(void*), void* arg); - Result new_userspace_thread(const TarStream::Entry& entry, const TarStream& stream); + Result new_userspace_thread(SharedPtr inode); Thread* pick_task();