Luna/kernel/src/thread/Thread.h

184 lines
3.7 KiB
C
Raw Normal View History

#pragma once
2023-01-05 21:50:53 +01:00
#include "arch/MMU.h"
#include "fs/VFS.h"
#include "memory/AddressSpace.h"
2023-07-10 19:46:57 +02:00
#include <bits/signal.h>
#include <luna/LinkedList.h>
#include <luna/OwnedPtr.h>
#include <luna/Result.h>
2022-12-18 18:43:17 +01:00
#include <luna/Stack.h>
2023-03-24 21:05:38 +01:00
#include <luna/StaticString.h>
#include <luna/String.h>
#ifdef ARCH_X86_64
#include "arch/x86_64/CPU.h"
#else
#error "Unknown architecture."
#endif
2022-12-07 15:14:58 +01:00
enum class ThreadState
{
None,
2022-12-07 15:14:58 +01:00
Idle,
Runnable,
Sleeping,
Waiting,
Exited,
Dying
2022-12-07 15:14:58 +01:00
};
struct OpenFileDescription : public Shareable
{
SharedPtr<VFS::Inode> inode;
int flags { 0 };
OpenFileDescription(SharedPtr<VFS::Inode>, int);
~OpenFileDescription();
};
struct FileDescriptor
{
SharedPtr<OpenFileDescription> description;
usize offset { 0 };
int flags { 0 };
bool should_append();
2023-03-19 11:25:14 +01:00
bool should_block();
bool is_writable();
bool is_readable();
SharedPtr<VFS::Inode> inode()
{
return description->inode;
}
int& status_flags()
{
return description->flags;
}
};
static constexpr int FD_MAX = 64;
struct Credentials
{
u32 uid { 0 };
u32 euid { 0 };
u32 suid { 0 };
u32 gid { 0 };
u32 egid { 0 };
u32 sgid { 0 };
};
2022-12-19 12:43:23 +01:00
struct Thread : public LinkedListNode<Thread>
{
Registers regs;
pid_t id;
pid_t pgid { 0 };
Credentials auth;
u64 user_ticks_self = 0;
u64 kernel_ticks_self = 0;
u64 user_ticks_children = 0;
u64 kernel_ticks_children = 0;
u64 ticks_left;
2022-12-07 15:55:58 +01:00
u64 sleep_ticks_left;
2022-12-18 18:43:17 +01:00
Stack stack;
2023-01-05 21:50:53 +01:00
Stack kernel_stack;
2022-12-18 18:43:17 +01:00
OwnedPtr<AddressSpace> address_space;
Option<FileDescriptor> fd_table[FD_MAX] = {};
Result<int> allocate_fd(int min);
Result<FileDescriptor*> resolve_fd(int fd);
Result<SharedPtr<VFS::Inode>> resolve_atfile(int dirfd, const String& path, bool allow_empty_path,
bool follow_last_symlink,
SharedPtr<VFS::Inode>* parent_inode = nullptr);
2023-07-10 19:46:57 +02:00
struct sigaction signal_handlers[NSIG];
sigset_t signal_mask { 0 };
2023-07-11 11:51:07 +02:00
sigset_t pending_signals { 0 };
bool interrupted { false };
2023-07-10 19:46:57 +02:00
FPData fp_data;
2022-12-07 15:14:58 +01:00
ThreadState state = ThreadState::Runnable;
2023-01-05 21:50:53 +01:00
bool is_kernel { true };
2023-07-11 11:51:07 +02:00
bool has_called_exec { false };
2023-01-05 21:50:53 +01:00
int status { 0 };
2023-05-26 22:27:49 +02:00
mode_t umask { 0 };
2023-03-24 21:05:38 +01:00
StaticString<128> name;
String current_directory_path = {};
SharedPtr<VFS::Inode> current_directory = {};
Thread* parent { nullptr };
Option<pid_t> child_being_waited_for = {};
PageDirectory* self_directory() const
{
return address_space->page_directory();
}
PageDirectory* active_directory { nullptr };
2023-01-05 21:50:53 +01:00
[[noreturn]] void exit_and_signal_parent(int status);
2022-12-07 15:14:58 +01:00
bool is_idle()
{
return state == ThreadState::Idle;
}
void wake_up()
{
state = ThreadState::Runnable;
}
void init_regs_kernel();
void init_regs_user();
void set_arguments(u64 arg1, u64 arg2, u64 arg3, u64 arg4);
void set_ip(u64 ip);
u64 ip();
void set_sp(u64 sp);
u64 sp();
2022-12-17 10:50:49 +01:00
2023-03-18 23:45:48 +01:00
void set_return(u64 ret);
2023-07-10 19:46:57 +02:00
u64 return_register();
void process_pending_signals(Registers* current_regs);
bool will_invoke_signal_handler();
2023-07-10 19:46:57 +02:00
bool deliver_signal(int signo, Registers* current_regs);
void sigreturn(Registers* current_regs);
Result<u64> push_mem_on_stack(const u8* mem, usize size);
Result<u64> pop_mem_from_stack(u8* mem, usize size);
2023-03-18 23:45:48 +01:00
void send_signal(int signo);
2022-12-17 10:50:49 +01:00
static void init();
};
void switch_context(Thread* old_thread, Thread* new_thread, Registers* regs);
bool is_in_kernel(Registers* regs);
Result<Thread*> new_thread();
pid_t next_thread_id();
2023-01-02 13:07:29 +01:00
extern LinkedList<Thread> g_threads;