2023-02-01 01:53:35 +02:00
|
|
|
#pragma once
|
|
|
|
|
2023-07-21 18:58:17 +03:00
|
|
|
#include <BAN/CircularQueue.h>
|
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-07-19 17:47:12 +03:00
|
|
|
#include <BAN/UniqPtr.h>
|
2023-05-28 16:24:41 +03:00
|
|
|
#include <kernel/Memory/VirtualRange.h>
|
2023-02-01 01:53:35 +02:00
|
|
|
|
2023-07-21 18:58:17 +03:00
|
|
|
#include <signal.h>
|
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-05-28 16:24:41 +03:00
|
|
|
static BAN::ErrorOr<Thread*> create_kernel(entry_t, void*, Process*);
|
2023-05-31 19:25:53 +03:00
|
|
|
static BAN::ErrorOr<Thread*> create_userspace(Process*);
|
2023-02-01 01:53:35 +02:00
|
|
|
~Thread();
|
|
|
|
|
2023-05-28 18:08:26 +03:00
|
|
|
BAN::ErrorOr<Thread*> clone(Process*, uintptr_t rsp, uintptr_t rip);
|
2023-05-31 20:56:29 +03:00
|
|
|
void setup_exec();
|
2023-05-28 18:08:26 +03:00
|
|
|
|
2023-07-21 19:54:37 +03:00
|
|
|
bool has_signal_to_execute() const;
|
2023-07-23 13:09:04 +03:00
|
|
|
void set_signal_done(int signal);
|
2023-07-21 19:27:10 +03:00
|
|
|
void handle_next_signal();
|
2023-07-24 22:26:10 +03:00
|
|
|
void queue_signal(int signal);
|
2023-07-23 18:33:10 +03:00
|
|
|
|
|
|
|
void set_return_rsp(uintptr_t& rsp) { m_return_rsp = &rsp; }
|
|
|
|
void set_return_rip(uintptr_t& rip) { m_return_rip = &rip; }
|
|
|
|
uintptr_t& return_rsp() { ASSERT(m_return_rsp); return *m_return_rsp; }
|
|
|
|
uintptr_t& return_rip() { ASSERT(m_return_rip); return *m_return_rip; }
|
2023-07-21 18:58:17 +03:00
|
|
|
|
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-05-28 16:24:41 +03:00
|
|
|
vaddr_t stack_base() const { return m_stack->vaddr(); }
|
|
|
|
size_t stack_size() const { return m_stack->size(); }
|
2023-04-12 00:22:08 +03:00
|
|
|
|
2023-05-28 16:24:41 +03:00
|
|
|
vaddr_t interrupt_stack_base() const { return m_interrupt_stack ? m_interrupt_stack->vaddr() : 0; }
|
|
|
|
size_t interrupt_stack_size() const { return m_interrupt_stack ? m_interrupt_stack->size() : 0; }
|
2023-04-25 14:48:58 +03:00
|
|
|
|
2023-03-30 19:13:28 +03:00
|
|
|
static Thread& current() ;
|
2023-05-28 16:18:18 +03:00
|
|
|
static pid_t current_tid();
|
|
|
|
|
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-05-28 20:53:10 +03:00
|
|
|
bool is_userspace() const { return m_is_userspace; }
|
|
|
|
bool is_in_syscall() const { return m_in_syscall; }
|
|
|
|
|
2023-02-01 01:53:35 +02:00
|
|
|
private:
|
2023-04-19 00:34:18 +03:00
|
|
|
Thread(pid_t tid, Process*);
|
2023-02-02 23:24:12 +02:00
|
|
|
void on_exit();
|
2023-02-01 01:53:35 +02:00
|
|
|
|
2023-05-16 00:27:49 +03:00
|
|
|
void validate_stack() const;
|
|
|
|
|
|
|
|
private:
|
2023-07-19 17:47:12 +03:00
|
|
|
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_interrupt_stack_size = PAGE_SIZE * 2;
|
|
|
|
BAN::UniqPtr<VirtualRange> m_interrupt_stack;
|
|
|
|
BAN::UniqPtr<VirtualRange> m_stack;
|
|
|
|
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 };
|
|
|
|
bool m_in_syscall { false };
|
|
|
|
bool m_is_userspace { false };
|
2023-05-16 00:27:49 +03:00
|
|
|
|
2023-07-23 18:33:10 +03:00
|
|
|
uintptr_t* m_return_rsp { nullptr };
|
|
|
|
uintptr_t* m_return_rip { nullptr };
|
|
|
|
|
2023-07-21 19:54:37 +03:00
|
|
|
BAN::CircularQueue<int, 10> m_signal_queue;
|
|
|
|
uint64_t m_signal_mask { 0 };
|
2023-07-24 22:26:10 +03:00
|
|
|
int m_handling_signal { 0 };
|
2023-07-21 18:58:17 +03:00
|
|
|
static_assert(_SIGMAX < 64);
|
|
|
|
|
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
|
|
|
}
|