2023-02-01 01:53:35 +02:00
|
|
|
#pragma once
|
|
|
|
|
2023-02-02 23:24:12 +02:00
|
|
|
#include <BAN/NoCopyMove.h>
|
2023-04-10 21:03:41 +03:00
|
|
|
#include <BAN/RefPtr.h>
|
2023-02-01 01:53:35 +02:00
|
|
|
|
2023-03-16 12:17:04 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2023-02-01 01:53:35 +02:00
|
|
|
namespace Kernel
|
|
|
|
{
|
|
|
|
|
2023-03-16 12:17:04 +02:00
|
|
|
class Process;
|
|
|
|
|
2023-03-30 19:13:28 +03:00
|
|
|
class Thread
|
2023-02-01 01:53:35 +02:00
|
|
|
{
|
2023-03-30 19:13:28 +03:00
|
|
|
BAN_NON_COPYABLE(Thread);
|
|
|
|
BAN_NON_MOVABLE(Thread);
|
|
|
|
|
2023-02-01 01:53:35 +02:00
|
|
|
public:
|
2023-03-09 15:25:39 +02:00
|
|
|
using entry_t = void(*)(void*);
|
|
|
|
|
2023-04-09 22:18:12 +03:00
|
|
|
enum class State
|
|
|
|
{
|
|
|
|
NotStarted,
|
|
|
|
Executing,
|
|
|
|
Terminating,
|
|
|
|
};
|
|
|
|
|
2023-03-09 15:25:39 +02:00
|
|
|
public:
|
2023-04-19 00:34:18 +03:00
|
|
|
static BAN::ErrorOr<Thread*> create(entry_t, void*, Process*);
|
2023-04-22 16:43:44 +03:00
|
|
|
static BAN::ErrorOr<Thread*> create_userspace(uintptr_t, Process*);
|
2023-02-01 01:53:35 +02:00
|
|
|
~Thread();
|
|
|
|
|
2023-04-12 17:51:36 +03:00
|
|
|
void jump_userspace(uintptr_t rip);
|
|
|
|
|
2023-03-16 12:17:04 +02:00
|
|
|
pid_t tid() const { return m_tid; }
|
2023-02-01 01:53:35 +02:00
|
|
|
|
2023-04-21 10:40:24 +03:00
|
|
|
void set_rsp(uintptr_t rsp) { m_rsp = rsp; validate_stack(); }
|
2023-02-02 23:24:12 +02:00
|
|
|
void set_rip(uintptr_t rip) { m_rip = rip; }
|
2023-02-01 01:53:35 +02:00
|
|
|
uintptr_t rsp() const { return m_rsp; }
|
2023-02-02 23:24:12 +02:00
|
|
|
uintptr_t rip() const { return m_rip; }
|
2023-03-07 19:17:49 +02:00
|
|
|
|
2023-04-09 22:18:12 +03:00
|
|
|
void set_started() { ASSERT(m_state == State::NotStarted); m_state = State::Executing; }
|
|
|
|
State state() const { return m_state; }
|
|
|
|
void terminate() { m_state = State::Terminating; }
|
2023-02-01 01:53:35 +02:00
|
|
|
|
2023-04-12 00:22:08 +03:00
|
|
|
uintptr_t stack_base() const { return (uintptr_t)m_stack_base; }
|
|
|
|
size_t stack_size() const { return m_stack_size; }
|
|
|
|
|
2023-03-30 19:13:28 +03:00
|
|
|
static Thread& current() ;
|
2023-04-19 00:34:18 +03:00
|
|
|
Process& process();
|
2023-04-20 00:45:41 +03:00
|
|
|
bool has_process() const { return m_process; }
|
2023-03-16 12:17:04 +02:00
|
|
|
|
2023-04-21 10:40:24 +03:00
|
|
|
void set_in_syscall(bool b) { m_in_syscall = b; }
|
|
|
|
|
2023-02-01 01:53:35 +02:00
|
|
|
private:
|
2023-04-19 00:34:18 +03:00
|
|
|
Thread(pid_t tid, Process*);
|
2023-03-16 12:17:04 +02:00
|
|
|
|
2023-04-25 13:40:55 +03:00
|
|
|
void validate_stack() const
|
|
|
|
{
|
|
|
|
if (!m_in_syscall)
|
|
|
|
if (!(stack_base() <= m_rsp && m_rsp <= stack_base() + stack_size()))
|
|
|
|
Kernel::panic("rsp {8H}, stack {8H}->{8H}", m_rsp, stack_base(), stack_base() + stack_size());
|
|
|
|
}
|
2023-04-21 10:40:24 +03:00
|
|
|
|
2023-03-09 15:25:39 +02:00
|
|
|
BAN::ErrorOr<void> initialize(entry_t, void*);
|
2023-02-02 23:24:12 +02:00
|
|
|
void on_exit();
|
2023-02-01 01:53:35 +02:00
|
|
|
|
|
|
|
private:
|
2023-04-12 00:22:08 +03:00
|
|
|
static constexpr size_t m_stack_size = 4096 * 1;
|
2023-04-19 00:34:18 +03:00
|
|
|
void* m_stack_base { nullptr };
|
|
|
|
uintptr_t m_rip { 0 };
|
|
|
|
uintptr_t m_rsp { 0 };
|
|
|
|
const pid_t m_tid { 0 };
|
|
|
|
State m_state { State::NotStarted };
|
|
|
|
Process* m_process { nullptr };
|
2023-04-21 10:40:24 +03:00
|
|
|
bool m_in_syscall { false };
|
2023-04-09 22:18:12 +03:00
|
|
|
|
|
|
|
friend class Scheduler;
|
2023-02-01 01:53:35 +02:00
|
|
|
};
|
2023-03-16 12:17:04 +02:00
|
|
|
|
2023-02-01 01:53:35 +02:00
|
|
|
}
|