forked from Bananymous/banan-os
Kernel: Move userspace entry functions to Process instead of Thread
This commit is contained in:
parent
eafa09fecf
commit
909e847369
|
@ -25,6 +25,13 @@ namespace Kernel
|
||||||
public:
|
public:
|
||||||
using entry_t = Thread::entry_t;
|
using entry_t = Thread::entry_t;
|
||||||
|
|
||||||
|
struct userspace_entry_t
|
||||||
|
{
|
||||||
|
uintptr_t entry { 0 };
|
||||||
|
int argc { 0 };
|
||||||
|
char** argv { 0 };
|
||||||
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static Process* create_kernel(entry_t, void*);
|
static Process* create_kernel(entry_t, void*);
|
||||||
static BAN::ErrorOr<Process*> create_userspace(BAN::StringView);
|
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(); }
|
PageTable& page_table() { return m_page_table ? *m_page_table : PageTable::kernel(); }
|
||||||
|
|
||||||
|
const userspace_entry_t& userspace_entry() const { return m_userspace_entry; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Process(pid_t);
|
Process(pid_t);
|
||||||
static Process* create_process();
|
static Process* create_process();
|
||||||
|
@ -103,6 +112,8 @@ namespace Kernel
|
||||||
BAN::Vector<FixedWidthAllocator*> m_fixed_width_allocators;
|
BAN::Vector<FixedWidthAllocator*> m_fixed_width_allocators;
|
||||||
GeneralAllocator* m_general_allocator;
|
GeneralAllocator* m_general_allocator;
|
||||||
|
|
||||||
|
userspace_entry_t m_userspace_entry;
|
||||||
|
|
||||||
PageTable* m_page_table { nullptr };
|
PageTable* m_page_table { nullptr };
|
||||||
TTY* m_tty { nullptr };
|
TTY* m_tty { nullptr };
|
||||||
};
|
};
|
||||||
|
|
|
@ -28,7 +28,7 @@ namespace Kernel
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<Thread*> create_kernel(entry_t, void*, Process*);
|
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();
|
~Thread();
|
||||||
|
|
||||||
BAN::ErrorOr<Thread*> clone(Process*, uintptr_t rsp, uintptr_t rip);
|
BAN::ErrorOr<Thread*> clone(Process*, uintptr_t rsp, uintptr_t rip);
|
||||||
|
@ -67,14 +67,6 @@ namespace Kernel
|
||||||
|
|
||||||
void validate_stack() const;
|
void validate_stack() const;
|
||||||
|
|
||||||
private:
|
|
||||||
struct userspace_entry_t
|
|
||||||
{
|
|
||||||
uintptr_t entry;
|
|
||||||
int argc { 0 };
|
|
||||||
char** argv { 0 };
|
|
||||||
};
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static constexpr size_t m_kernel_stack_size = PAGE_SIZE * 1;
|
static constexpr size_t m_kernel_stack_size = PAGE_SIZE * 1;
|
||||||
static constexpr size_t m_userspace_stack_size = PAGE_SIZE * 2;
|
static constexpr size_t m_userspace_stack_size = PAGE_SIZE * 2;
|
||||||
|
@ -89,8 +81,6 @@ namespace Kernel
|
||||||
bool m_in_syscall { false };
|
bool m_in_syscall { false };
|
||||||
bool m_is_userspace { false };
|
bool m_is_userspace { false };
|
||||||
|
|
||||||
userspace_entry_t m_userspace_entry;
|
|
||||||
|
|
||||||
friend class Scheduler;
|
friend class Scheduler;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -94,13 +94,18 @@ namespace Kernel
|
||||||
char** argv = nullptr;
|
char** argv = nullptr;
|
||||||
{
|
{
|
||||||
PageTableScope _(process->page_table());
|
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));
|
argv[0] = (char*)MUST(process->allocate(path.size() + 1));
|
||||||
memcpy(argv[0], path.data(), path.size());
|
memcpy(argv[0], path.data(), path.size());
|
||||||
argv[0][path.size()] = '\0';
|
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);
|
process->add_thread(thread);
|
||||||
|
|
||||||
delete elf;
|
delete elf;
|
||||||
|
@ -198,6 +203,8 @@ namespace Kernel
|
||||||
|
|
||||||
forked->m_open_files = m_open_files;
|
forked->m_open_files = m_open_files;
|
||||||
|
|
||||||
|
forked->m_userspace_entry = m_userspace_entry;
|
||||||
|
|
||||||
for (auto* mapped_range : m_mapped_ranges)
|
for (auto* mapped_range : m_mapped_ranges)
|
||||||
MUST(forked->m_mapped_ranges.push_back(mapped_range->clone(forked->page_table())));
|
MUST(forked->m_mapped_ranges.push_back(mapped_range->clone(forked->page_table())));
|
||||||
|
|
||||||
|
|
|
@ -43,7 +43,7 @@ namespace Kernel
|
||||||
return thread;
|
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);
|
ASSERT(process);
|
||||||
|
|
||||||
|
@ -69,13 +69,11 @@ namespace Kernel
|
||||||
return BAN::Error::from_errno(ENOMEM);
|
return BAN::Error::from_errno(ENOMEM);
|
||||||
}
|
}
|
||||||
|
|
||||||
thread->m_userspace_entry = { .entry = entry, .argc = argc, .argv = argv };
|
|
||||||
|
|
||||||
// Setup registers and entry
|
// Setup registers and entry
|
||||||
static entry_t entry_trampoline(
|
static entry_t entry_trampoline(
|
||||||
[](void*)
|
[](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);
|
thread_userspace_trampoline(Thread::current().rsp(), entry.entry, entry.argc, entry.argv);
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
@ -140,8 +138,6 @@ namespace Kernel
|
||||||
thread->m_rip = rip;
|
thread->m_rip = rip;
|
||||||
thread->m_rsp = rsp;
|
thread->m_rsp = rsp;
|
||||||
|
|
||||||
thread->m_userspace_entry = {};
|
|
||||||
|
|
||||||
return thread;
|
return thread;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue