Kernel: Move userspace entry functions to Process instead of Thread

This commit is contained in:
Bananymous 2023-05-31 19:25:53 +03:00
parent eafa09fecf
commit 909e847369
4 changed files with 23 additions and 19 deletions

View File

@ -25,6 +25,13 @@ namespace Kernel
public:
using entry_t = Thread::entry_t;
struct userspace_entry_t
{
uintptr_t entry { 0 };
int argc { 0 };
char** argv { 0 };
};
public:
static Process* create_kernel(entry_t, void*);
static BAN::ErrorOr<Process*> create_userspace(BAN::StringView);
@ -71,6 +78,8 @@ namespace Kernel
PageTable& page_table() { return m_page_table ? *m_page_table : PageTable::kernel(); }
const userspace_entry_t& userspace_entry() const { return m_userspace_entry; }
private:
Process(pid_t);
static Process* create_process();
@ -103,6 +112,8 @@ namespace Kernel
BAN::Vector<FixedWidthAllocator*> m_fixed_width_allocators;
GeneralAllocator* m_general_allocator;
userspace_entry_t m_userspace_entry;
PageTable* m_page_table { nullptr };
TTY* m_tty { nullptr };
};

View File

@ -28,7 +28,7 @@ namespace Kernel
public:
static BAN::ErrorOr<Thread*> create_kernel(entry_t, void*, Process*);
static BAN::ErrorOr<Thread*> create_userspace(uintptr_t, Process*, int, char**);
static BAN::ErrorOr<Thread*> create_userspace(Process*);
~Thread();
BAN::ErrorOr<Thread*> clone(Process*, uintptr_t rsp, uintptr_t rip);
@ -67,14 +67,6 @@ namespace Kernel
void validate_stack() const;
private:
struct userspace_entry_t
{
uintptr_t entry;
int argc { 0 };
char** argv { 0 };
};
private:
static constexpr size_t m_kernel_stack_size = PAGE_SIZE * 1;
static constexpr size_t m_userspace_stack_size = PAGE_SIZE * 2;
@ -89,8 +81,6 @@ namespace Kernel
bool m_in_syscall { false };
bool m_is_userspace { false };
userspace_entry_t m_userspace_entry;
friend class Scheduler;
};

View File

@ -94,13 +94,18 @@ namespace Kernel
char** argv = nullptr;
{
PageTableScope _(process->page_table());
argv = (char**)MUST(process->allocate(sizeof(char**) * 1));
argv = (char**)MUST(process->allocate(sizeof(char**) * 2));
argv[0] = (char*)MUST(process->allocate(path.size() + 1));
memcpy(argv[0], path.data(), path.size());
argv[0][path.size()] = '\0';
argv[1] = nullptr;
}
auto* thread = MUST(Thread::create_userspace(elf_file_header.e_entry, process, 1, argv));
process->m_userspace_entry.argc = 1;
process->m_userspace_entry.argv = argv;
process->m_userspace_entry.entry = elf_file_header.e_entry;
auto* thread = MUST(Thread::create_userspace(process));
process->add_thread(thread);
delete elf;
@ -198,6 +203,8 @@ namespace Kernel
forked->m_open_files = m_open_files;
forked->m_userspace_entry = m_userspace_entry;
for (auto* mapped_range : m_mapped_ranges)
MUST(forked->m_mapped_ranges.push_back(mapped_range->clone(forked->page_table())));

View File

@ -43,7 +43,7 @@ namespace Kernel
return thread;
}
BAN::ErrorOr<Thread*> Thread::create_userspace(uintptr_t entry, Process* process, int argc, char** argv)
BAN::ErrorOr<Thread*> Thread::create_userspace(Process* process)
{
ASSERT(process);
@ -69,13 +69,11 @@ namespace Kernel
return BAN::Error::from_errno(ENOMEM);
}
thread->m_userspace_entry = { .entry = entry, .argc = argc, .argv = argv };
// Setup registers and entry
static entry_t entry_trampoline(
[](void*)
{
userspace_entry_t& entry = Thread::current().m_userspace_entry;
const auto& entry = Process::current().userspace_entry();
thread_userspace_trampoline(Thread::current().rsp(), entry.entry, entry.argc, entry.argv);
ASSERT_NOT_REACHED();
}
@ -140,8 +138,6 @@ namespace Kernel
thread->m_rip = rip;
thread->m_rsp = rsp;
thread->m_userspace_entry = {};
return thread;
}