Kernel: Add basic concept of Processes
We now create Shell as a process that has its own threads
This commit is contained in:
@@ -50,6 +50,7 @@ kernel/kmalloc.o \
|
||||
kernel/PCI.o \
|
||||
kernel/PIC.o \
|
||||
kernel/PIT.o \
|
||||
kernel/Process.o \
|
||||
kernel/RTC.o \
|
||||
kernel/Scheduler.o \
|
||||
kernel/Serial.o \
|
||||
|
||||
40
kernel/include/kernel/Process.h
Normal file
40
kernel/include/kernel/Process.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
|
||||
#include <BAN/String.h>
|
||||
#include <BAN/Vector.h>
|
||||
#include <kernel/Thread.h>
|
||||
|
||||
namespace Kernel
|
||||
{
|
||||
|
||||
class Process : BAN::RefCounted<Process>
|
||||
{
|
||||
BAN_NON_COPYABLE(Process);
|
||||
BAN_NON_MOVABLE(Process);
|
||||
|
||||
public:
|
||||
using entry_t = Thread::entry_t;
|
||||
|
||||
public:
|
||||
static BAN::ErrorOr<BAN::RefPtr<Process>> create_kernel(entry_t, void*);
|
||||
~Process() {}
|
||||
|
||||
BAN::ErrorOr<void> add_thread(entry_t, void*);
|
||||
void on_thread_exit(Thread&);
|
||||
|
||||
pid_t pid() const { return m_pid; }
|
||||
|
||||
static BAN::RefPtr<Process> current() { return Thread::current()->process(); }
|
||||
|
||||
private:
|
||||
Process(pid_t pid) : m_pid(pid) {}
|
||||
|
||||
private:
|
||||
pid_t m_pid = 0;
|
||||
BAN::String m_working_directory;
|
||||
BAN::Vector<BAN::RefPtr<Thread>> m_threads;
|
||||
|
||||
friend class BAN::RefPtr<Process>;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -21,11 +21,11 @@ namespace Kernel
|
||||
void set_current_thread_sleeping(uint64_t);
|
||||
[[noreturn]] void set_current_thread_done();
|
||||
|
||||
BAN::RefPtr<Thread> current_thread();
|
||||
|
||||
private:
|
||||
Scheduler() = default;
|
||||
|
||||
BAN::RefPtr<Thread> current_thread();
|
||||
|
||||
void wake_threads();
|
||||
[[nodiscard]] bool save_current_thread();
|
||||
void remove_and_advance_current_thread();
|
||||
|
||||
@@ -3,19 +3,23 @@
|
||||
#include <BAN/Function.h>
|
||||
#include <BAN/NoCopyMove.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
namespace Kernel
|
||||
{
|
||||
|
||||
class Process;
|
||||
|
||||
class Thread : public BAN::RefCounted<Thread>
|
||||
{
|
||||
public:
|
||||
using entry_t = void(*)(void*);
|
||||
|
||||
public:
|
||||
static BAN::ErrorOr<BAN::RefPtr<Thread>> create(entry_t, void* = nullptr);
|
||||
static BAN::ErrorOr<BAN::RefPtr<Thread>> create(entry_t, void* = nullptr, BAN::RefPtr<Process> = nullptr);
|
||||
~Thread();
|
||||
|
||||
uint32_t tid() const { return m_tid; }
|
||||
pid_t tid() const { return m_tid; }
|
||||
|
||||
void set_rsp(uintptr_t rsp) { m_rsp = rsp; }
|
||||
void set_rip(uintptr_t rip) { m_rip = rip; }
|
||||
@@ -25,19 +29,24 @@ namespace Kernel
|
||||
void set_started() { m_started = true; }
|
||||
bool started() const { return m_started; }
|
||||
|
||||
static BAN::RefPtr<Thread> current() ;
|
||||
BAN::RefPtr<Process> process();
|
||||
|
||||
private:
|
||||
Thread();
|
||||
Thread(pid_t tid, BAN::RefPtr<Process>);
|
||||
|
||||
BAN::ErrorOr<void> initialize(entry_t, void*);
|
||||
void on_exit();
|
||||
|
||||
private:
|
||||
void* m_stack_base = nullptr;
|
||||
uintptr_t m_rip = 0;
|
||||
uintptr_t m_rsp = 0;
|
||||
const uint32_t m_tid = 0;
|
||||
bool m_started = false;
|
||||
void* m_stack_base = nullptr;
|
||||
uintptr_t m_rip = 0;
|
||||
uintptr_t m_rsp = 0;
|
||||
const pid_t m_tid = 0;
|
||||
bool m_started = false;
|
||||
BAN::RefPtr<Process> m_process;
|
||||
|
||||
friend class BAN::RefPtr<Thread>;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
37
kernel/kernel/Process.cpp
Normal file
37
kernel/kernel/Process.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <BAN/StringView.h>
|
||||
#include <kernel/Process.h>
|
||||
#include <kernel/Scheduler.h>
|
||||
|
||||
namespace Kernel
|
||||
{
|
||||
|
||||
BAN::ErrorOr<BAN::RefPtr<Process>> Process::create_kernel(entry_t entry, void* data)
|
||||
{
|
||||
static pid_t next_pid = 1;
|
||||
auto process = TRY(BAN::RefPtr<Process>::create(next_pid++));
|
||||
TRY(process->add_thread(entry, data));
|
||||
TRY(process->m_working_directory.append("/"sv));
|
||||
return process;
|
||||
}
|
||||
|
||||
BAN::ErrorOr<void> Process::add_thread(entry_t entry, void* data)
|
||||
{
|
||||
auto thread = TRY(Thread::create(entry, data, this));
|
||||
TRY(m_threads.push_back(thread));
|
||||
if (auto res = Scheduler::get().add_thread(thread); res.is_error())
|
||||
{
|
||||
m_threads.pop_back();
|
||||
return res;
|
||||
}
|
||||
|
||||
dprintln("add thread {} to process {}", thread->tid(), pid());
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void Process::on_thread_exit(Thread& thread)
|
||||
{
|
||||
dprintln("thread {} exited from process", thread.tid(), pid());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <kernel/Attributes.h>
|
||||
#include <kernel/CriticalScope.h>
|
||||
#include <kernel/InterruptController.h>
|
||||
#include <kernel/Process.h>
|
||||
#include <kernel/Scheduler.h>
|
||||
|
||||
#if 1
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
#include <kernel/Input.h>
|
||||
#include <kernel/IO.h>
|
||||
#include <kernel/PIT.h>
|
||||
#include <kernel/Process.h>
|
||||
#include <kernel/RTC.h>
|
||||
#include <kernel/Scheduler.h>
|
||||
#include <kernel/Shell.h>
|
||||
|
||||
#include <kernel/FS/VirtualFileSystem.h>
|
||||
@@ -202,7 +202,7 @@ argument_done:
|
||||
SpinLock spinlock;
|
||||
thread_data_t thread_data = { this, spinlock, arguments };
|
||||
spinlock.lock();
|
||||
TRY(Scheduler::get().add_thread(TRY(Thread::create(function, &thread_data))));
|
||||
TRY(Process::current()->add_thread(function, &thread_data));
|
||||
while (spinlock.is_locked());
|
||||
}
|
||||
else if (arguments.front() == "memory")
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include <BAN/Errors.h>
|
||||
#include <kernel/Arch.h>
|
||||
#include <kernel/InterruptController.h>
|
||||
#include <kernel/kmalloc.h>
|
||||
#include <kernel/Process.h>
|
||||
#include <kernel/Scheduler.h>
|
||||
#include <kernel/Thread.h>
|
||||
|
||||
@@ -10,8 +10,6 @@
|
||||
namespace Kernel
|
||||
{
|
||||
|
||||
static uint32_t s_next_tid = 0;
|
||||
|
||||
static constexpr size_t thread_stack_size = 16384;
|
||||
|
||||
template<size_t size, typename T>
|
||||
@@ -21,17 +19,28 @@ namespace Kernel
|
||||
memcpy((void*)rsp, (void*)&value, size);
|
||||
}
|
||||
|
||||
BAN::ErrorOr<BAN::RefPtr<Thread>> Thread::create(entry_t entry, void* data)
|
||||
BAN::ErrorOr<BAN::RefPtr<Thread>> Thread::create(entry_t entry, void* data, BAN::RefPtr<Process> process)
|
||||
{
|
||||
auto thread = TRY(BAN::RefPtr<Thread>::create());
|
||||
static pid_t next_tid = 1;
|
||||
auto thread = TRY(BAN::RefPtr<Thread>::create(next_tid++, process));
|
||||
TRY(thread->initialize(entry, data));
|
||||
return thread;
|
||||
}
|
||||
|
||||
Thread::Thread()
|
||||
: m_tid(s_next_tid++)
|
||||
Thread::Thread(pid_t tid, BAN::RefPtr<Process> process)
|
||||
: m_tid(tid), m_process(process)
|
||||
{}
|
||||
|
||||
BAN::RefPtr<Thread> Thread::current()
|
||||
{
|
||||
return Scheduler::get().current_thread();
|
||||
}
|
||||
|
||||
BAN::RefPtr<Process> Thread::process()
|
||||
{
|
||||
return m_process;
|
||||
}
|
||||
|
||||
BAN::ErrorOr<void> Thread::initialize(entry_t entry, void* data)
|
||||
{
|
||||
m_stack_base = kmalloc(thread_stack_size, PAGE_SIZE);
|
||||
@@ -54,6 +63,8 @@ namespace Kernel
|
||||
|
||||
void Thread::on_exit()
|
||||
{
|
||||
if (m_process)
|
||||
m_process->on_thread_exit(*this);
|
||||
Scheduler::get().set_current_thread_done();
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include <kernel/PCI.h>
|
||||
#include <kernel/PIC.h>
|
||||
#include <kernel/PIT.h>
|
||||
#include <kernel/Process.h>
|
||||
#include <kernel/Scheduler.h>
|
||||
#include <kernel/Serial.h>
|
||||
#include <kernel/Shell.h>
|
||||
@@ -85,6 +86,8 @@ extern "C" uintptr_t g_userspace_end;
|
||||
|
||||
extern void userspace_entry();
|
||||
|
||||
void init2(void*);
|
||||
|
||||
extern "C" void kernel_main()
|
||||
{
|
||||
using namespace Kernel;
|
||||
@@ -121,6 +124,7 @@ extern "C" void kernel_main()
|
||||
ASSERT(terminal_driver);
|
||||
dprintln("VESA initialized");
|
||||
TTY* tty1 = new TTY(terminal_driver);
|
||||
ASSERT(tty1);
|
||||
|
||||
InterruptController::initialize(cmdline.force_pic);
|
||||
dprintln("Interrupt controller initialized");
|
||||
@@ -134,7 +138,7 @@ extern "C" void kernel_main()
|
||||
|
||||
MUST(Scheduler::initialize());
|
||||
Scheduler& scheduler = Scheduler::get();
|
||||
#if 1
|
||||
#if 0
|
||||
MUST(scheduler.add_thread(MUST(Thread::create(
|
||||
[] (void*)
|
||||
{
|
||||
@@ -178,27 +182,32 @@ extern "C" void kernel_main()
|
||||
}
|
||||
))));
|
||||
#else
|
||||
MUST(scheduler.add_thread(MUST(Thread::create(
|
||||
[](void* terminal_driver)
|
||||
{
|
||||
MUST(VirtualFileSystem::initialize());
|
||||
|
||||
auto font_or_error = Font::load("/usr/share/fonts/zap-ext-vga16.psf");
|
||||
if (font_or_error.is_error())
|
||||
dprintln("{}", font_or_error.error());
|
||||
else
|
||||
((TerminalDriver*)terminal_driver)->set_font(font_or_error.release_value());
|
||||
}, terminal_driver
|
||||
))));
|
||||
MUST(scheduler.add_thread(MUST(Thread::create(
|
||||
[](void* tty)
|
||||
{
|
||||
Shell* shell = new Shell((TTY*)tty);
|
||||
ASSERT(shell);
|
||||
shell->run();
|
||||
}, tty1
|
||||
))));
|
||||
MUST(scheduler.add_thread(MUST(Thread::create(init2, tty1, nullptr))));
|
||||
#endif
|
||||
scheduler.start();
|
||||
ASSERT(false);
|
||||
}
|
||||
|
||||
void init2(void* tty1_ptr)
|
||||
{
|
||||
using namespace Kernel;
|
||||
|
||||
TTY* tty1 = (TTY*)tty1_ptr;
|
||||
|
||||
MUST(VirtualFileSystem::initialize());
|
||||
|
||||
auto font_or_error = Font::load("/usr/share/fonts/zap-ext-vga16.psf");
|
||||
if (font_or_error.is_error())
|
||||
dprintln("{}", font_or_error.error());
|
||||
else
|
||||
tty1->set_font(font_or_error.release_value());
|
||||
|
||||
MUST(Process::create_kernel(
|
||||
[](void* tty1)
|
||||
{
|
||||
Shell* shell = new Shell((TTY*)tty1);
|
||||
ASSERT(shell);
|
||||
shell->run();
|
||||
}, tty1
|
||||
));
|
||||
}
|
||||
Reference in New Issue
Block a user