2023-03-16 12:17:04 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <BAN/String.h>
|
2023-03-17 21:16:22 +02:00
|
|
|
#include <BAN/StringView.h>
|
2023-03-16 12:17:04 +02:00
|
|
|
#include <BAN/Vector.h>
|
2023-06-11 19:52:13 +03:00
|
|
|
#include <kernel/Credentials.h>
|
2023-03-17 21:16:22 +02:00
|
|
|
#include <kernel/FS/Inode.h>
|
2023-05-06 18:10:38 +03:00
|
|
|
#include <kernel/Memory/FixedWidthAllocator.h>
|
2023-05-08 22:10:49 +03:00
|
|
|
#include <kernel/Memory/GeneralAllocator.h>
|
2023-04-22 16:54:46 +03:00
|
|
|
#include <kernel/Memory/Heap.h>
|
2023-05-28 16:24:41 +03:00
|
|
|
#include <kernel/Memory/VirtualRange.h>
|
2023-03-24 01:17:39 +02:00
|
|
|
#include <kernel/SpinLock.h>
|
2023-04-05 02:53:28 +03:00
|
|
|
#include <kernel/Terminal/TTY.h>
|
2023-03-16 12:17:04 +02:00
|
|
|
#include <kernel/Thread.h>
|
|
|
|
|
2023-03-26 04:30:57 +03:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2023-06-04 01:24:11 +03:00
|
|
|
namespace LibELF { class ELF; }
|
|
|
|
|
2023-03-16 12:17:04 +02:00
|
|
|
namespace Kernel
|
|
|
|
{
|
|
|
|
|
2023-04-19 00:34:18 +03:00
|
|
|
class Process
|
2023-03-16 12:17:04 +02:00
|
|
|
{
|
|
|
|
BAN_NON_COPYABLE(Process);
|
|
|
|
BAN_NON_MOVABLE(Process);
|
|
|
|
|
|
|
|
public:
|
|
|
|
using entry_t = Thread::entry_t;
|
|
|
|
|
2023-06-05 21:53:37 +03:00
|
|
|
struct userspace_info_t
|
2023-05-31 19:25:53 +03:00
|
|
|
{
|
|
|
|
uintptr_t entry { 0 };
|
|
|
|
int argc { 0 };
|
2023-06-05 20:34:08 +03:00
|
|
|
char** argv { nullptr };
|
|
|
|
char** envp { nullptr };
|
2023-05-31 19:25:53 +03:00
|
|
|
};
|
|
|
|
|
2023-03-16 12:17:04 +02:00
|
|
|
public:
|
2023-04-22 16:43:44 +03:00
|
|
|
static Process* create_kernel(entry_t, void*);
|
2023-06-11 19:52:13 +03:00
|
|
|
static BAN::ErrorOr<Process*> create_userspace(const Credentials&, BAN::StringView);
|
2023-04-09 22:18:12 +03:00
|
|
|
~Process();
|
2023-03-16 12:17:04 +02:00
|
|
|
|
2023-06-10 17:31:56 +03:00
|
|
|
[[noreturn]] void exit(int status);
|
2023-04-09 22:18:12 +03:00
|
|
|
|
2023-04-22 16:43:44 +03:00
|
|
|
void add_thread(Thread*);
|
2023-03-16 12:17:04 +02:00
|
|
|
void on_thread_exit(Thread&);
|
|
|
|
|
2023-04-05 02:53:28 +03:00
|
|
|
BAN::ErrorOr<void> set_termios(const termios&);
|
2023-04-05 01:10:25 +03:00
|
|
|
|
2023-03-16 12:17:04 +02:00
|
|
|
pid_t pid() const { return m_pid; }
|
|
|
|
|
2023-05-28 18:08:26 +03:00
|
|
|
BAN::ErrorOr<Process*> fork(uintptr_t rsp, uintptr_t rip);
|
2023-05-31 20:56:29 +03:00
|
|
|
BAN::ErrorOr<void> exec(BAN::StringView path, const char* const* argv, const char* const* envp);
|
2023-05-28 18:08:26 +03:00
|
|
|
|
2023-06-04 17:57:24 +03:00
|
|
|
int block_until_exit();
|
|
|
|
BAN::ErrorOr<pid_t> wait(pid_t pid, int* stat_loc, int options);
|
|
|
|
|
2023-06-05 22:43:26 +03:00
|
|
|
BAN::ErrorOr<void> setenvp(char** envp);
|
|
|
|
|
2023-03-17 21:16:22 +02:00
|
|
|
BAN::ErrorOr<int> open(BAN::StringView, int);
|
2023-06-11 03:27:56 +03:00
|
|
|
BAN::ErrorOr<int> openat(int, BAN::StringView, int);
|
2023-05-09 20:31:22 +03:00
|
|
|
BAN::ErrorOr<void> close(int fd);
|
2023-05-16 14:14:47 +03:00
|
|
|
BAN::ErrorOr<size_t> read(int fd, void* buffer, size_t count);
|
|
|
|
BAN::ErrorOr<size_t> write(int fd, const void* buffer, size_t count);
|
2023-05-09 20:31:22 +03:00
|
|
|
BAN::ErrorOr<void> creat(BAN::StringView name, mode_t);
|
2023-03-17 21:16:22 +02:00
|
|
|
|
2023-05-16 14:14:47 +03:00
|
|
|
BAN::ErrorOr<void> seek(int fd, off_t offset, int whence);
|
|
|
|
BAN::ErrorOr<off_t> tell(int fd);
|
|
|
|
|
2023-05-11 01:48:33 +03:00
|
|
|
BAN::ErrorOr<void> fstat(int fd, struct stat*);
|
2023-06-05 14:36:17 +03:00
|
|
|
BAN::ErrorOr<void> stat(BAN::StringView path, struct stat*, int flags);
|
2023-03-26 04:30:57 +03:00
|
|
|
|
2023-05-09 20:31:22 +03:00
|
|
|
BAN::ErrorOr<void> mount(BAN::StringView source, BAN::StringView target);
|
2023-03-30 15:06:41 +03:00
|
|
|
|
2023-06-10 23:13:41 +03:00
|
|
|
BAN::ErrorOr<void> read_next_directory_entries(int fd, DirectoryEntryList* buffer, size_t buffer_size);
|
2023-03-26 04:30:57 +03:00
|
|
|
|
2023-03-30 22:02:16 +03:00
|
|
|
BAN::ErrorOr<BAN::String> working_directory() const;
|
2023-03-17 21:16:22 +02:00
|
|
|
BAN::ErrorOr<void> set_working_directory(BAN::StringView);
|
|
|
|
|
2023-05-16 19:22:10 +03:00
|
|
|
TTY& tty() { ASSERT(m_tty); return *m_tty; }
|
|
|
|
|
2023-05-06 18:10:38 +03:00
|
|
|
BAN::ErrorOr<void*> allocate(size_t);
|
2023-05-07 01:21:50 +03:00
|
|
|
void free(void*);
|
2023-05-06 18:10:38 +03:00
|
|
|
|
2023-04-23 14:32:37 +03:00
|
|
|
void termid(char*) const;
|
|
|
|
|
2023-04-19 00:34:18 +03:00
|
|
|
static Process& current() { return Thread::current().process(); }
|
2023-03-16 12:17:04 +02:00
|
|
|
|
2023-05-29 21:06:09 +03:00
|
|
|
PageTable& page_table() { return m_page_table ? *m_page_table : PageTable::kernel(); }
|
2023-04-20 00:45:41 +03:00
|
|
|
|
2023-06-05 21:53:37 +03:00
|
|
|
const userspace_info_t& userspace_info() const { return m_userspace_info; }
|
2023-05-31 19:25:53 +03:00
|
|
|
|
2023-03-16 12:17:04 +02:00
|
|
|
private:
|
2023-06-11 19:52:13 +03:00
|
|
|
Process(const Credentials&, pid_t);
|
|
|
|
static Process* create_process(const Credentials&);
|
2023-04-22 16:43:44 +03:00
|
|
|
static void register_process(Process*);
|
2023-03-16 12:17:04 +02:00
|
|
|
|
2023-06-09 00:37:43 +03:00
|
|
|
// Load an elf file to virtual address space of the current page table
|
2023-06-11 19:52:13 +03:00
|
|
|
static BAN::ErrorOr<BAN::UniqPtr<LibELF::ELF>> load_elf_for_exec(const Credentials&, BAN::StringView file_path, const BAN::String& cwd, const BAN::Vector<BAN::StringView>& path_env);
|
2023-06-09 00:37:43 +03:00
|
|
|
|
|
|
|
// Copy an elf file from the current page table to the processes own
|
|
|
|
void load_elf_to_memory(LibELF::ELF&);
|
2023-05-31 20:56:29 +03:00
|
|
|
|
2023-03-17 21:16:22 +02:00
|
|
|
BAN::ErrorOr<BAN::String> absolute_path_of(BAN::StringView) const;
|
|
|
|
|
2023-03-16 12:17:04 +02:00
|
|
|
private:
|
2023-03-17 21:16:22 +02:00
|
|
|
struct OpenFileDescription
|
|
|
|
{
|
|
|
|
BAN::RefPtr<Inode> inode;
|
|
|
|
BAN::String path;
|
2023-05-16 14:14:47 +03:00
|
|
|
off_t offset { 0 };
|
|
|
|
uint8_t flags { 0 };
|
2023-03-17 21:16:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
BAN::ErrorOr<void> validate_fd(int);
|
|
|
|
OpenFileDescription& open_file_description(int);
|
|
|
|
BAN::ErrorOr<int> get_free_fd();
|
|
|
|
|
2023-06-04 17:57:24 +03:00
|
|
|
|
|
|
|
struct ExitStatus
|
|
|
|
{
|
|
|
|
Semaphore semaphore;
|
|
|
|
int exit_code { 0 };
|
|
|
|
bool exited { false };
|
|
|
|
int waiting { 0 };
|
|
|
|
};
|
|
|
|
|
2023-06-11 19:52:13 +03:00
|
|
|
Credentials m_credentials;
|
|
|
|
|
2023-03-17 21:16:22 +02:00
|
|
|
BAN::Vector<OpenFileDescription> m_open_files;
|
2023-05-28 16:24:41 +03:00
|
|
|
BAN::Vector<VirtualRange*> m_mapped_ranges;
|
2023-03-17 21:16:22 +02:00
|
|
|
|
2023-03-24 01:32:35 +02:00
|
|
|
mutable RecursiveSpinLock m_lock;
|
2023-03-24 01:17:39 +02:00
|
|
|
|
2023-03-24 01:43:53 +02:00
|
|
|
const pid_t m_pid = 0;
|
2023-03-16 12:17:04 +02:00
|
|
|
BAN::String m_working_directory;
|
2023-03-30 19:13:28 +03:00
|
|
|
BAN::Vector<Thread*> m_threads;
|
2023-03-16 12:17:04 +02:00
|
|
|
|
2023-06-04 01:25:57 +03:00
|
|
|
BAN::Vector<BAN::UniqPtr<FixedWidthAllocator>> m_fixed_width_allocators;
|
|
|
|
BAN::UniqPtr<GeneralAllocator> m_general_allocator;
|
2023-05-06 18:10:38 +03:00
|
|
|
|
2023-06-05 21:53:37 +03:00
|
|
|
userspace_info_t m_userspace_info;
|
2023-06-04 17:57:24 +03:00
|
|
|
ExitStatus m_exit_status;
|
2023-05-31 19:25:53 +03:00
|
|
|
|
2023-06-04 01:25:57 +03:00
|
|
|
BAN::UniqPtr<PageTable> m_page_table;
|
2023-04-05 02:53:28 +03:00
|
|
|
TTY* m_tty { nullptr };
|
2023-03-16 12:17:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|