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-07-19 17:47:12 +03:00
|
|
|
#include <BAN/UniqPtr.h>
|
2024-03-29 18:02:12 +02:00
|
|
|
#include <kernel/InterruptStack.h>
|
2024-07-24 00:31:01 +03:00
|
|
|
#include <kernel/Memory/VirtualRange.h>
|
|
|
|
#include <kernel/ThreadBlocker.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);
|
2024-01-24 14:43:46 +02:00
|
|
|
|
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,
|
2024-03-29 18:02:12 +02:00
|
|
|
Terminated,
|
2023-07-28 18:06:20 +03:00
|
|
|
};
|
|
|
|
|
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();
|
|
|
|
|
2024-03-22 14:48:33 +02:00
|
|
|
BAN::ErrorOr<Thread*> clone(Process*, uintptr_t sp, uintptr_t ip);
|
2023-05-31 20:56:29 +03:00
|
|
|
void setup_exec();
|
2023-07-28 18:06:20 +03:00
|
|
|
void setup_process_cleanup();
|
2023-05-28 18:08:26 +03:00
|
|
|
|
2024-05-26 20:08:35 +03:00
|
|
|
// Returns true, if thread is going to trigger signal
|
|
|
|
bool is_interrupted_by_signal() const;
|
2023-12-06 13:02:17 +02:00
|
|
|
|
|
|
|
// Returns true if pending signal can be added to thread
|
|
|
|
bool can_add_signal_to_execute() const;
|
|
|
|
bool will_execute_signal() const;
|
2023-07-29 16:54:31 +03:00
|
|
|
void handle_signal(int signal = 0);
|
|
|
|
bool add_signal(int signal);
|
2023-07-23 18:33:10 +03:00
|
|
|
|
2024-07-22 00:33:50 +03:00
|
|
|
// blocks current thread and returns either on unblock, eintr, spuriously or after timeout
|
2024-08-09 16:52:35 +03:00
|
|
|
BAN::ErrorOr<void> sleep_or_eintr_ms(uint64_t ms) { return sleep_or_eintr_ns(ms * 1'000'000); }
|
|
|
|
BAN::ErrorOr<void> sleep_or_eintr_ns(uint64_t ns);
|
2024-07-22 00:33:50 +03:00
|
|
|
BAN::ErrorOr<void> block_or_eintr_indefinite(ThreadBlocker& thread_blocker);
|
|
|
|
BAN::ErrorOr<void> block_or_eintr_or_timeout_ms(ThreadBlocker& thread_blocker, uint64_t timeout_ms, bool etimedout) { return block_or_eintr_or_timeout_ns(thread_blocker, timeout_ms * 1'000'000, etimedout); }
|
|
|
|
BAN::ErrorOr<void> block_or_eintr_or_waketime_ms(ThreadBlocker& thread_blocker, uint64_t wake_time_ms, bool etimedout) { return block_or_eintr_or_waketime_ns(thread_blocker, wake_time_ms * 1'000'000, etimedout); }
|
|
|
|
BAN::ErrorOr<void> block_or_eintr_or_timeout_ns(ThreadBlocker& thread_blocker, uint64_t timeout_ns, bool etimedout);
|
|
|
|
BAN::ErrorOr<void> block_or_eintr_or_waketime_ns(ThreadBlocker& thread_blocker, uint64_t wake_time_ns, bool etimedout);
|
2023-12-06 16:13:07 +02: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-09 22:18:12 +03:00
|
|
|
State state() const { return m_state; }
|
2023-02-01 01:53:35 +02:00
|
|
|
|
2024-03-27 15:06:24 +02:00
|
|
|
vaddr_t kernel_stack_bottom() const { return m_kernel_stack->vaddr(); }
|
|
|
|
vaddr_t kernel_stack_top() const { return m_kernel_stack->vaddr() + m_kernel_stack->size(); }
|
|
|
|
VirtualRange& kernel_stack() { return *m_kernel_stack; }
|
2023-04-12 00:22:08 +03:00
|
|
|
|
2024-04-17 00:55:45 +03:00
|
|
|
vaddr_t userspace_stack_bottom() const { return is_userspace() ? m_userspace_stack->vaddr() : UINTPTR_MAX; }
|
2024-03-27 15:06:24 +02:00
|
|
|
vaddr_t userspace_stack_top() const { return is_userspace() ? m_userspace_stack->vaddr() + m_userspace_stack->size() : 0; }
|
|
|
|
VirtualRange& userspace_stack() { ASSERT(is_userspace()); return *m_userspace_stack; }
|
2023-04-25 14:48:58 +03:00
|
|
|
|
2024-01-03 02:06:49 +02: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();
|
2024-03-26 02:46:51 +02:00
|
|
|
const Process& process() const;
|
2023-04-20 00:45:41 +03:00
|
|
|
bool has_process() const { return m_process; }
|
2023-03-16 12:17:04 +02:00
|
|
|
|
2023-05-28 20:53:10 +03:00
|
|
|
bool is_userspace() const { return m_is_userspace; }
|
|
|
|
|
2024-08-09 15:50:50 +03:00
|
|
|
size_t virtual_page_count() const { return (m_kernel_stack ? (m_kernel_stack->size() / PAGE_SIZE) : 0) + (m_userspace_stack ? (m_userspace_stack->size() / PAGE_SIZE) : 0); }
|
2023-09-30 22:11:45 +03:00
|
|
|
size_t physical_page_count() const { return virtual_page_count(); }
|
2023-09-30 21:15:46 +03:00
|
|
|
|
2024-03-29 18:02:12 +02:00
|
|
|
InterruptStack& interrupt_stack() { return m_interrupt_stack; }
|
|
|
|
InterruptRegisters& interrupt_registers() { return m_interrupt_registers; }
|
|
|
|
|
2023-09-23 02:28:25 +03:00
|
|
|
#if __enable_sse
|
2024-01-03 02:06:49 +02:00
|
|
|
void save_sse();
|
|
|
|
void load_sse();
|
2023-09-23 02:28:25 +03:00
|
|
|
#endif
|
2023-07-31 22:28:57 +03:00
|
|
|
|
2024-05-26 20:08:35 +03:00
|
|
|
void add_mutex() { m_mutex_count++; }
|
|
|
|
void remove_mutex() { m_mutex_count--; }
|
|
|
|
|
2023-02-01 01:53:35 +02:00
|
|
|
private:
|
2023-04-19 00:34:18 +03:00
|
|
|
Thread(pid_t tid, Process*);
|
2024-04-02 12:34:42 +03:00
|
|
|
|
|
|
|
static void on_exit_trampoline(Thread*);
|
2023-02-02 23:24:12 +02:00
|
|
|
void on_exit();
|
2024-01-24 14:43:46 +02:00
|
|
|
|
2023-05-16 00:27:49 +03:00
|
|
|
private:
|
2024-07-24 00:31:01 +03:00
|
|
|
static constexpr size_t m_kernel_stack_size { PAGE_SIZE * 64 };
|
|
|
|
static constexpr size_t m_userspace_stack_size { PAGE_SIZE * 64 };
|
|
|
|
BAN::UniqPtr<VirtualRange> m_kernel_stack;
|
|
|
|
BAN::UniqPtr<VirtualRange> m_userspace_stack;
|
|
|
|
const pid_t m_tid { 0 };
|
|
|
|
State m_state { State::NotStarted };
|
|
|
|
Process* m_process { nullptr };
|
|
|
|
bool m_is_userspace { false };
|
|
|
|
bool m_delete_process { false };
|
|
|
|
|
|
|
|
SchedulerQueue::Node* m_scheduler_node { nullptr };
|
|
|
|
|
|
|
|
InterruptStack m_interrupt_stack { };
|
|
|
|
InterruptRegisters m_interrupt_registers { };
|
|
|
|
|
|
|
|
uint64_t m_signal_pending_mask { 0 };
|
|
|
|
uint64_t m_signal_block_mask { 0 };
|
|
|
|
SpinLock m_signal_lock;
|
2023-07-21 18:58:17 +03:00
|
|
|
static_assert(_SIGMAX < 64);
|
|
|
|
|
2024-07-24 00:31:01 +03:00
|
|
|
BAN::Atomic<uint32_t> m_mutex_count { 0 };
|
2024-05-26 20:08:35 +03:00
|
|
|
|
2023-09-23 02:28:25 +03:00
|
|
|
#if __enable_sse
|
2023-08-11 00:26:43 +03:00
|
|
|
alignas(16) uint8_t m_sse_storage[512] {};
|
2023-09-23 02:28:25 +03:00
|
|
|
#endif
|
2023-07-31 22:28:57 +03:00
|
|
|
|
2024-03-29 18:02:12 +02:00
|
|
|
friend class Process;
|
2023-04-09 22:18:12 +03:00
|
|
|
friend class Scheduler;
|
2023-02-01 01:53:35 +02:00
|
|
|
};
|
2023-09-09 22:52:03 +03:00
|
|
|
|
2024-01-24 14:43:46 +02:00
|
|
|
}
|