14using namespace MaxOS::system;
15using namespace MaxOS::memory;
16using namespace MaxOS::processes;
17using namespace MaxOS::common;
18using namespace MaxOS::hardwarecommunication;
47 ASSERT(m_stack_pointer != 0 && m_tss_stack_pointer != 0,
"Failed to allocate stack for thread");
61 uint64_t argc = arg_amount;
62 size_t arg_size = argc *
sizeof(
void*);
64 for (
int i = 0; i < argc; i++) {
67 size_t len = strlen(((
char**)args)[i]) + 1;
69 memcpy(((
char**)argv)[i], ((
char**)args)[i], len);
77 memset(m_io_bitmap, 0xFF,
sizeof(m_io_bitmap));
132 asm volatile(
"fxsave %0" :
"=m" (m_sse_save_region));
145 asm volatile(
"fxrstor %0" : :
"m" (m_sse_save_region));
177 "movq 0(%rsp), %rax\n" "movq %rax, 0x00(%rsi)\n"
178 "movq 8(%rsp), %rax\n" "movq %rax, 0x08(%rsi)\n"
179 "movq 16(%rsp), %rax\n" "movq %rax, 0x10(%rsi)\n"
180 "movq 24(%rsp), %rax\n" "movq %rax, 0x18(%rsi)\n"
181 "movq 32(%rsp), %rax\n" "movq %rax, 0x20(%rsi)\n"
182 "movq 40(%rsp), %rax\n" "movq %rax, 0x28(%rsi)\n"
183 "movq 48(%rsp), %rax\n" "movq %rax, 0x30(%rsi)\n"
184 "movq 56(%rsp), %rax\n" "movq %rax, 0x38(%rsi)\n"
185 "movq 64(%rsp), %rax\n" "movq %rax, 0x40(%rsi)\n"
186 "movq 72(%rsp), %rax\n" "movq %rax, 0x48(%rsi)\n"
187 "movq 80(%rsp), %rax\n" "movq %rax, 0x50(%rsi)\n"
188 "movq 88(%rsp), %rax\n" "movq %rax, 0x58(%rsi)\n"
189 "movq 96(%rsp), %rax\n" "movq %rax, 0x60(%rsi)\n"
190 "movq 104(%rsp), %rax\n" "movq %rax, 0x68(%rsi)\n"
191 "movq 112(%rsp), %rax\n" "movq %rax, 0x70(%rsi)\n"
194 "movq 120(%rsp), %rax\n"
195 "movq %rax, 0x88(%rsi)\n"
200 "movq %rax, 0x90(%rsi)\n"
205 "movq %rax, 0x98(%rsi)\n"
208 "leaq 120(%rsp), %rax\n"
209 "movq %rax, 0xA0(%rsi)\n"
214 "movq %rax, 0xA8(%rsi)\n"
250 m_io_bitmap[port / 8] &= ~(1U << (port % 8));
261 m_io_bitmap[port / 8] |= (1U << (port % 8));
281: is_kernel(is_kernel),
297Process::Process(
const string& p_name,
void (* _entry_point)(
void*),
void* args,
int arg_amount,
bool is_kernel)
302 auto* main_thread =
new Thread(_entry_point, args, arg_amount,
this);
327 auto* entry_point = (void (*)(
void*)) elf->
header()->
entry;
330 auto* main_thread =
new Thread(entry_point, args, arg_amount,
this);
343 for (
auto thread: m_threads)
364 m_threads.push_back(thread);
378 for (uint32_t i = 0; i < m_threads.size(); i++) {
381 if (m_threads[i]->tid != tid)
385 Thread* thread = m_threads[i];
389 m_threads.erase(m_threads.begin() + i);
409 for (
auto thread: m_threads)
410 thread->parent_pid =
pid;
442 for (
auto thread: m_threads)
static LogRecord DEBUG()
Gets the active logger for debug logs.
static void ForceInterruptReturn(system::cpu_status_t *state)
Force the CPU to return from an interrupt (see interrupts.s)
VirtualMemoryManager * vmm()
Handles memory allocation and deallocation.
static MemoryManager * s_kernel_memory_manager
The memory manager for any kernel processes and all kernel allocations.
static void * kmalloc(size_t size)
Allocates a block of memory in the KERNEL space.
static PhysicalMemoryManager * s_current_manager
The current physical memory manager in use.
uint64_t memory_used() const
Gets the memory currently used.
void * allocate(size_t size) final
Allocates a block of memory.
Handles the loading and parsing of 64-bit ELF files.
elf_64_header_t * header() const
Gets the header of the elf file.
void load_into_memory(memory::VirtualMemoryManager *vmm) const
Loads the elf program into memory if a valid elf file.
static Scheduler * core_scheduler()
Gets the scheduler for the currently executing core.
A process that can be scheduled by the Scheduler, wraps & manages threads as well as its own address ...
uint64_t pid() const
Gets the pid of the process.
uint64_t total_ticks()
Gets the total ticks of the threads in the process (not this does not include any past killed threads...
string name
The name of the process.
common::Vector< Thread * > threads()
Gets the threads of the process.
void add_thread(Thread *thread)
Adds a thread to the process.
~Process()
Destructor for the Process class.
Process(const string &name, bool is_kernel=false)
Base Constructor for the Process.
bool is_kernel
Whether this process is a kernel process.
void set_pid(uint64_t pid)
Sets the pid of the process once added to the queue.
void remove_thread(uint64_t tid)
Remove a thread by its tid (NOTE: this will not remove it from the scheduler)
memory::MemoryManager * memory_manager
The manager for memory used by this process.
system::cpu_status_t * schedule_next(system::cpu_status_t *status)
Schedules the next thread to run.
The execution context of a sub-process thread.
void enable_port(uint64_t port)
Sets bits in the threads TSS to allow a port for I/O.
Thread(void(*_entry_point)(void *), void *args, int arg_amount, Process *parent)
Constructor for the Thread class.
uint64_t parent_pid
The parent process ID.
void save_sse_state()
Saves the SSE, x87 FPU, and MMX states from memory to the thread.
volatile system::cpu_status_t execution_state
The CPU state of the thread.
~Thread()
Destructor for the Thread class.
void yield()
Pass execution to the next ready thread.
volatile size_t ticks
The number of ticks the thread has run for.
void save_cpu_state()
Saves the CPU state into the thread's execution_state structure.
void disable_port(uint64_t port)
Sets bits in the threads TSS to disallow a port for I/O.
volatile thread_state_t thread_state
The current state of the thread.
void restore_sse_state()
Restores the SSE, x87 FPU, and MMX states from the thread to memory.
volatile size_t wakeup_time
The time at which the thread should wake up (if sleeping)
void sleep(size_t milliseconds)
Sleeps the thread for a certain amount of time.
void load_port_bitmap()
Copy this ports.
static Core * executing_core()
Gets the core that is currently executing.
struct PACKED MaxOS::system::CPUStatus cpu_status_t
Alias for CPUStatus struct.
Defines a Logger class for logging messages with different severity levels to multiple output streams...
void save_cpu_state_naked(volatile void *location) __attribute__((naked))
Save the state of the CPU at the exit of this function.
Defines Process and Thread classes for managing processes and their execution contexts.
constexpr size_t STACK_SIZE
The size of the stack for each thread (4KB)
Defines a GlobalScheduler and Scheduler for managing processes and threads.