Kernel: Add basic concept of Processes

We now create Shell as a process that has its own threads
This commit is contained in:
Bananymous 2023-03-16 12:17:04 +02:00
parent c012822d49
commit 1b7625581d
11 changed files with 170 additions and 48 deletions

View File

@ -62,12 +62,21 @@ namespace BAN
{ {
public: public:
RefPtr() = default; RefPtr() = default;
RefPtr(T* pointer)
{
m_pointer = pointer;
if (m_pointer)
m_pointer->ref();
}
~RefPtr() { clear(); } ~RefPtr() { clear(); }
template<typename U> template<typename U>
static RefPtr adopt(U* pointer) static RefPtr adopt(U* pointer)
{ {
return RefPtr(pointer); RefPtr ptr;
ptr.m_pointer = pointer;
return ptr;
} }
template<typename... Args> template<typename... Args>
@ -76,7 +85,7 @@ namespace BAN
T* pointer = new T(forward<Args>(args)...); T* pointer = new T(forward<Args>(args)...);
if (pointer == nullptr) if (pointer == nullptr)
return Error::from_errno(ENOMEM); return Error::from_errno(ENOMEM);
return RefPtr(pointer); return adopt(pointer);
} }
RefPtr(const RefPtr& other) { *this = other; } RefPtr(const RefPtr& other) { *this = other; }
@ -118,11 +127,6 @@ namespace BAN
m_pointer = nullptr; m_pointer = nullptr;
} }
private:
RefPtr(T* pointer)
: m_pointer(pointer)
{}
private: private:
T* m_pointer = nullptr; T* m_pointer = nullptr;
}; };

View File

@ -50,6 +50,7 @@ kernel/kmalloc.o \
kernel/PCI.o \ kernel/PCI.o \
kernel/PIC.o \ kernel/PIC.o \
kernel/PIT.o \ kernel/PIT.o \
kernel/Process.o \
kernel/RTC.o \ kernel/RTC.o \
kernel/Scheduler.o \ kernel/Scheduler.o \
kernel/Serial.o \ kernel/Serial.o \

View 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>;
};
}

View File

@ -21,11 +21,11 @@ namespace Kernel
void set_current_thread_sleeping(uint64_t); void set_current_thread_sleeping(uint64_t);
[[noreturn]] void set_current_thread_done(); [[noreturn]] void set_current_thread_done();
BAN::RefPtr<Thread> current_thread();
private: private:
Scheduler() = default; Scheduler() = default;
BAN::RefPtr<Thread> current_thread();
void wake_threads(); void wake_threads();
[[nodiscard]] bool save_current_thread(); [[nodiscard]] bool save_current_thread();
void remove_and_advance_current_thread(); void remove_and_advance_current_thread();

View File

@ -3,19 +3,23 @@
#include <BAN/Function.h> #include <BAN/Function.h>
#include <BAN/NoCopyMove.h> #include <BAN/NoCopyMove.h>
#include <sys/types.h>
namespace Kernel namespace Kernel
{ {
class Process;
class Thread : public BAN::RefCounted<Thread> class Thread : public BAN::RefCounted<Thread>
{ {
public: public:
using entry_t = void(*)(void*); using entry_t = void(*)(void*);
public: 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(); ~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_rsp(uintptr_t rsp) { m_rsp = rsp; }
void set_rip(uintptr_t rip) { m_rip = rip; } void set_rip(uintptr_t rip) { m_rip = rip; }
@ -25,17 +29,22 @@ namespace Kernel
void set_started() { m_started = true; } void set_started() { m_started = true; }
bool started() const { return m_started; } bool started() const { return m_started; }
static BAN::RefPtr<Thread> current() ;
BAN::RefPtr<Process> process();
private: private:
Thread(); Thread(pid_t tid, BAN::RefPtr<Process>);
BAN::ErrorOr<void> initialize(entry_t, void*); BAN::ErrorOr<void> initialize(entry_t, void*);
void on_exit(); void on_exit();
private: private:
void* m_stack_base = nullptr; void* m_stack_base = nullptr;
uintptr_t m_rip = 0; uintptr_t m_rip = 0;
uintptr_t m_rsp = 0; uintptr_t m_rsp = 0;
const uint32_t m_tid = 0; const pid_t m_tid = 0;
bool m_started = false; bool m_started = false;
BAN::RefPtr<Process> m_process;
friend class BAN::RefPtr<Thread>; friend class BAN::RefPtr<Thread>;
}; };

37
kernel/kernel/Process.cpp Normal file
View 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());
}
}

View File

@ -2,6 +2,7 @@
#include <kernel/Attributes.h> #include <kernel/Attributes.h>
#include <kernel/CriticalScope.h> #include <kernel/CriticalScope.h>
#include <kernel/InterruptController.h> #include <kernel/InterruptController.h>
#include <kernel/Process.h>
#include <kernel/Scheduler.h> #include <kernel/Scheduler.h>
#if 1 #if 1

View File

@ -5,8 +5,8 @@
#include <kernel/Input.h> #include <kernel/Input.h>
#include <kernel/IO.h> #include <kernel/IO.h>
#include <kernel/PIT.h> #include <kernel/PIT.h>
#include <kernel/Process.h>
#include <kernel/RTC.h> #include <kernel/RTC.h>
#include <kernel/Scheduler.h>
#include <kernel/Shell.h> #include <kernel/Shell.h>
#include <kernel/FS/VirtualFileSystem.h> #include <kernel/FS/VirtualFileSystem.h>
@ -202,7 +202,7 @@ argument_done:
SpinLock spinlock; SpinLock spinlock;
thread_data_t thread_data = { this, spinlock, arguments }; thread_data_t thread_data = { this, spinlock, arguments };
spinlock.lock(); 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()); while (spinlock.is_locked());
} }
else if (arguments.front() == "memory") else if (arguments.front() == "memory")

View File

@ -1,7 +1,7 @@
#include <BAN/Errors.h> #include <BAN/Errors.h>
#include <kernel/Arch.h>
#include <kernel/InterruptController.h> #include <kernel/InterruptController.h>
#include <kernel/kmalloc.h> #include <kernel/kmalloc.h>
#include <kernel/Process.h>
#include <kernel/Scheduler.h> #include <kernel/Scheduler.h>
#include <kernel/Thread.h> #include <kernel/Thread.h>
@ -10,8 +10,6 @@
namespace Kernel namespace Kernel
{ {
static uint32_t s_next_tid = 0;
static constexpr size_t thread_stack_size = 16384; static constexpr size_t thread_stack_size = 16384;
template<size_t size, typename T> template<size_t size, typename T>
@ -21,17 +19,28 @@ namespace Kernel
memcpy((void*)rsp, (void*)&value, size); 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)); TRY(thread->initialize(entry, data));
return thread; return thread;
} }
Thread::Thread() Thread::Thread(pid_t tid, BAN::RefPtr<Process> process)
: m_tid(s_next_tid++) : 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) BAN::ErrorOr<void> Thread::initialize(entry_t entry, void* data)
{ {
m_stack_base = kmalloc(thread_stack_size, PAGE_SIZE); m_stack_base = kmalloc(thread_stack_size, PAGE_SIZE);
@ -54,6 +63,8 @@ namespace Kernel
void Thread::on_exit() void Thread::on_exit()
{ {
if (m_process)
m_process->on_thread_exit(*this);
Scheduler::get().set_current_thread_done(); Scheduler::get().set_current_thread_done();
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }

View File

@ -12,6 +12,7 @@
#include <kernel/PCI.h> #include <kernel/PCI.h>
#include <kernel/PIC.h> #include <kernel/PIC.h>
#include <kernel/PIT.h> #include <kernel/PIT.h>
#include <kernel/Process.h>
#include <kernel/Scheduler.h> #include <kernel/Scheduler.h>
#include <kernel/Serial.h> #include <kernel/Serial.h>
#include <kernel/Shell.h> #include <kernel/Shell.h>
@ -85,6 +86,8 @@ extern "C" uintptr_t g_userspace_end;
extern void userspace_entry(); extern void userspace_entry();
void init2(void*);
extern "C" void kernel_main() extern "C" void kernel_main()
{ {
using namespace Kernel; using namespace Kernel;
@ -121,6 +124,7 @@ extern "C" void kernel_main()
ASSERT(terminal_driver); ASSERT(terminal_driver);
dprintln("VESA initialized"); dprintln("VESA initialized");
TTY* tty1 = new TTY(terminal_driver); TTY* tty1 = new TTY(terminal_driver);
ASSERT(tty1);
InterruptController::initialize(cmdline.force_pic); InterruptController::initialize(cmdline.force_pic);
dprintln("Interrupt controller initialized"); dprintln("Interrupt controller initialized");
@ -134,7 +138,7 @@ extern "C" void kernel_main()
MUST(Scheduler::initialize()); MUST(Scheduler::initialize());
Scheduler& scheduler = Scheduler::get(); Scheduler& scheduler = Scheduler::get();
#if 1 #if 0
MUST(scheduler.add_thread(MUST(Thread::create( MUST(scheduler.add_thread(MUST(Thread::create(
[] (void*) [] (void*)
{ {
@ -178,27 +182,32 @@ extern "C" void kernel_main()
} }
)))); ))));
#else #else
MUST(scheduler.add_thread(MUST(Thread::create( MUST(scheduler.add_thread(MUST(Thread::create(init2, tty1, nullptr))));
[](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
))));
#endif #endif
scheduler.start(); scheduler.start();
ASSERT(false); 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
));
}

10
libc/include/sys/types.h Normal file
View File

@ -0,0 +1,10 @@
#pragma once
#include <stdint.h>
#include <sys/cdefs.h>
__BEGIN_DECLS
typedef int32_t pid_t;
__END_DECLS