Max OS 0.3
Loading...
Searching...
No Matches
process.h
Go to the documentation of this file.
1
9#ifndef MAXOS_PROCESSES_PROCESS_H
10#define MAXOS_PROCESSES_PROCESS_H
11
12#include <libcommon/vector.h>
13#include <libcommon/string.h>
14
15#include <system/cpu.h>
16#include <memory/virtual.h>
18#include <processes/elf.h>
21
22namespace MaxOS::processes {
23 class Process;
24
32 typedef enum class ThreadState {
33 NEW,
34 RUNNING,
35 READY,
36 SLEEPING,
37 WAITING,
38 STOPPED
39 } thread_state_t;
40
42 constexpr size_t STACK_SIZE = 0x10000;
43
50 class Thread {
51
52 private:
53
54 uintptr_t m_stack_pointer;
55 uintptr_t m_tss_stack_pointer;
56
57 uint8_t m_io_bitmap[system::IO_BITMAP_SIZE];
58
59 char m_sse_save_region[512] __attribute__((aligned(16)));
60
61 public:
62 Thread(void (* _entry_point)(void*), void* args, int arg_amount, Process* parent);
64
65 void sleep(size_t milliseconds);
66 void yield();
67
68 uint64_t tid;
69 uint64_t parent_pid;
70
73
74 volatile size_t ticks;
75 volatile size_t wakeup_time;
76
77 [[nodiscard]] uintptr_t tss_pointer() const { return m_tss_stack_pointer; }
78 void set_tss_pointer(uintptr_t new_tss_pointer) { m_tss_stack_pointer = new_tss_pointer; }
79
80 void save_sse_state();
81 void restore_sse_state();
82
83 void save_cpu_state();
84
85 void enable_port(uint64_t port);
86 void disable_port(uint64_t port);
87 void load_port_bitmap();
88 };
89
94 class Process {
95
96 private:
97 common::Vector<Thread*> m_threads;
98
99 uint64_t m_pid = 0;
100 common::Spinlock m_lock;
101
102 public:
103 explicit Process(const string& name, bool is_kernel = false);
104 Process(const string& name, void (* _entry_point)(void*), void* args, int arg_amount, bool is_kernel = false);
105 Process(const string& name, void* args, int arg_amount, ELF64* elf, bool is_kernel = false);
106 ~Process();
107
108 common::Vector<Thread*> threads();
109 void add_thread(Thread* thread);
110 void remove_thread(uint64_t tid);
111
112 void set_pid(uint64_t pid);
113 [[nodiscard]] uint64_t pid() const;
114 [[nodiscard]] uint64_t total_ticks();
115
117
118 string name;
119 string working_directory = "/";
120
123 };
124}
125
126#endif // MAXOS_PROCESSES_PROCESS_H
Handles memory allocation and deallocation.
Handles the loading and parsing of 64-bit ELF files.
Definition elf.h:217
A process that can be scheduled by the Scheduler, wraps & manages threads as well as its own address ...
Definition process.h:94
uint64_t pid() const
Gets the pid of the process.
Definition process.cpp:429
uint64_t total_ticks()
Gets the total ticks of the threads in the process (not this does not include any past killed threads...
Definition process.cpp:439
string name
The name of the process.
Definition process.h:118
resources::ResourceManager resource_manager
The manger for resources used by this process.
Definition process.h:122
common::Vector< Thread * > threads()
Gets the threads of the process.
Definition process.cpp:419
void add_thread(Thread *thread)
Adds a thread to the process.
Definition process.cpp:359
~Process()
Destructor for the Process class.
Definition process.cpp:338
string working_directory
The working directory of the process.
Definition process.h:119
bool is_kernel
Whether this process is a kernel process.
Definition process.h:116
void set_pid(uint64_t pid)
Sets the pid of the process once added to the queue.
Definition process.cpp:399
void remove_thread(uint64_t tid)
Remove a thread by its tid (NOTE: this will not remove it from the scheduler)
Definition process.cpp:375
memory::MemoryManager * memory_manager
The manager for memory used by this process.
Definition process.h:121
The execution context of a sub-process thread.
Definition process.h:50
void enable_port(uint64_t port)
Sets bits in the threads TSS to allow a port for I/O.
Definition process.cpp:247
uint64_t parent_pid
The parent process ID.
Definition process.h:69
void save_sse_state()
Saves the SSE, x87 FPU, and MMX states from memory to the thread.
Definition process.cpp:125
uint64_t tid
The thread ID.
Definition process.h:68
volatile system::cpu_status_t execution_state
The CPU state of the thread.
Definition process.h:71
~Thread()
Destructor for the Thread class.
void yield()
Pass execution to the next ready thread.
Definition process.cpp:103
volatile size_t ticks
The number of ticks the thread has run for.
Definition process.h:74
void save_cpu_state()
Saves the CPU state into the thread's execution_state structure.
Definition process.cpp:240
void set_tss_pointer(uintptr_t new_tss_pointer)
Sets the stack pointer to use for the TSS when switching to this thread.
Definition process.h:78
void disable_port(uint64_t port)
Sets bits in the threads TSS to disallow a port for I/O.
Definition process.cpp:258
uintptr_t tss_pointer() const
Gets the stack pointer to use for the TSS when switching to this thread.
Definition process.h:77
volatile thread_state_t thread_state
The current state of the thread.
Definition process.h:72
void restore_sse_state()
Restores the SSE, x87 FPU, and MMX states from the thread to memory.
Definition process.cpp:138
volatile size_t wakeup_time
The time at which the thread should wake up (if sleeping)
Definition process.h:75
void sleep(size_t milliseconds)
Sleeps the thread for a certain amount of time.
Definition process.cpp:92
void load_port_bitmap()
Copy this ports.
Definition process.cpp:268
Manages the open resources for a process.
Definition resource.h:121
Defines Central Processing Unit (CPU) structures and functions for managing CPU state and features.
struct PACKED MaxOS::system::CPUStatus cpu_status_t
Alias for CPUStatus struct.
Defines structures and enums for handling ELF (Executable and Linkable Format) files in MaxOS.
Defines a InterruptManager and InterruptHandler for managing hardware and software interrupts.
Defines a MemoryManager class for handling memory allocation and deallocation.
constexpr size_t STACK_SIZE
The size of the stack for each thread (4KB)
Definition process.h:42
ThreadState
The different operational states a thread can be in.
Definition process.h:32
enum MaxOS::processes::ThreadState thread_state_t
Alias for ThreadState enum.
Defines classes for managing process resources such as files and devices.
Defines a VirtualMemoryManager class for managing virtual memory allocation and mapping to physical m...