Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::processes::Thread Class Reference

The execution context of a sub-process thread. More...

#include <process.h>

Public Member Functions

 Thread (void(*_entry_point)(void *), void *args, int arg_amount, Process *parent)
 Constructor for the Thread class.
 
 ~Thread ()
 Destructor for the Thread class.
 
void sleep (size_t milliseconds)
 Sleeps the thread for a certain amount of time.
 
void yield ()
 Pass execution to the next ready thread.
 
uintptr_t tss_pointer () const
 Gets the stack pointer to use for the TSS when switching to this thread.
 
void set_tss_pointer (uintptr_t new_tss_pointer)
 Sets the stack pointer to use for the TSS when switching to this thread.
 
void save_sse_state ()
 Saves the SSE, x87 FPU, and MMX states from memory to the thread.
 
void restore_sse_state ()
 Restores the SSE, x87 FPU, and MMX states from the thread to memory.
 
void save_cpu_state ()
 Saves the CPU state into the thread's execution_state structure.
 
void enable_port (uint64_t port)
 Sets bits in the threads TSS to allow a port for I/O.
 
void disable_port (uint64_t port)
 Sets bits in the threads TSS to disallow a port for I/O.
 
void load_port_bitmap ()
 Copy this ports.
 

Public Attributes

uint64_t tid
 The thread ID.
 
uint64_t parent_pid
 The parent process ID.
 
volatile system::cpu_status_t execution_state
 The CPU state of the thread.
 
volatile thread_state_t thread_state
 The current state of the thread.
 
volatile size_t ticks
 The number of ticks the thread has run for.
 
volatile size_t wakeup_time
 The time at which the thread should wake up (if sleeping)
 

Detailed Description

The execution context of a sub-process thread.

Todo:
io bitmap per process instead

Definition at line 50 of file process.h.

Constructor & Destructor Documentation

◆ Thread()

Thread::Thread ( void(*)(void *)  _entry_point,
void *  args,
int  arg_amount,
Process *  parent 
)

Constructor for the Thread class.

Parameters
_entry_pointThe address of the function to start executing
argsThe arguments to pass to the function
arg_amountThe number of arguments
parentThe proccess that owns this thread (started it)
Todo:
Cant use rsp0 for kernel stack storage as once scheduler starts up it may point to a userspace rsp0 (nor is it good to use the same stack for every process)
Warning
cant usee global MemoryManager::malloc() during constructor as process hasn't been registered with the scheduler yet

Definition at line 32 of file process.cpp.

33{
34
35 // Basic setup
36 thread_state = ThreadState::NEW;
37 wakeup_time = 0;
38 ticks = 0;
39
40 // Create the stack ()
41 m_stack_pointer = (uintptr_t) parent->memory_manager->allocate(STACK_SIZE) + STACK_SIZE;
42
43 // Use the kernel stack
44 m_tss_stack_pointer = parent->is_kernel ? CPU::executing_core() -> tss.rsp0 : (uintptr_t) parent->memory_manager->kmalloc(STACK_SIZE) + STACK_SIZE;
45
46 // Mak sure there is a stack
47 ASSERT(m_stack_pointer != 0 && m_tss_stack_pointer != 0, "Failed to allocate stack for thread");
48
49 // Set up the execution state
50 execution_state = {};
51 execution_state.rip = (uint64_t) _entry_point;
52 execution_state.ss = parent->is_kernel ? 0x10 : 0x23;
53 execution_state.cs = parent->is_kernel ? 0x8 : 0x1B;
54 execution_state.rflags = 0x202;
55 execution_state.interrupt_number = 0;
56 execution_state.error_code = 0;
57 execution_state.rsp = m_stack_pointer;
58 execution_state.rbp = 0;
59
60 // Copy the args into userspace
61 uint64_t argc = arg_amount;
62 size_t arg_size = argc * sizeof(void*);
63 void* argv = parent->memory_manager->allocate(arg_size);
64 for (int i = 0; i < argc; i++) {
65
66 // Copy each argument
67 size_t len = strlen(((char**)args)[i]) + 1;
68 ((char**)argv)[i] = (char*) parent->memory_manager->allocate(len);
69 memcpy(((char**)argv)[i], ((char**)args)[i], len);
70 }
71
72 execution_state.rdi = argc;
73 execution_state.rsi = (uint64_t) argv;
74 //execution_state->rdx = (uint64_t)env_args;
75
76 // All ports denied by default
77 memset(m_io_bitmap, 0xFF, sizeof(m_io_bitmap));
78
79 parent_pid = parent->pid();
80}
static void * kmalloc(size_t size)
Allocates a block of memory in the KERNEL space.
void * allocate(size_t size) final
Allocates a block of memory.
Definition bump.cpp:144
uint64_t pid() const
Gets the pid of the process.
Definition process.cpp:429
bool is_kernel
Whether this process is a kernel process.
Definition process.h:116
memory::MemoryManager * memory_manager
The manager for memory used by this process.
Definition process.h:121
uint64_t parent_pid
The parent process ID.
Definition process.h:69
volatile system::cpu_status_t execution_state
The CPU state of the thread.
Definition process.h:71
volatile size_t ticks
The number of ticks the thread has run for.
Definition process.h:74
volatile thread_state_t thread_state
The current state of the thread.
Definition process.h:72
volatile size_t wakeup_time
The time at which the thread should wake up (if sleeping)
Definition process.h:75
static Core * executing_core()
Gets the core that is currently executing.
Definition cpu.cpp:660
constexpr size_t STACK_SIZE
The size of the stack for each thread (4KB)
Definition process.h:42

References MaxOS::memory::allocator::BumpAllocator::allocate(), MaxOS::system::CPU::executing_core(), execution_state, MaxOS::processes::Process::is_kernel, MaxOS::memory::MemoryManager::kmalloc(), MaxOS::processes::Process::memory_manager, parent_pid, MaxOS::processes::Process::pid(), MaxOS::processes::STACK_SIZE, thread_state, ticks, and wakeup_time.

Member Function Documentation

◆ disable_port()

void Thread::disable_port ( uint64_t  port)

Sets bits in the threads TSS to disallow a port for I/O.

Definition at line 258 of file process.cpp.

258 {
259
260 // Disable the port
261 m_io_bitmap[port / 8] |= (1U << (port % 8));
263}
void load_port_bitmap()
Copy this ports.
Definition process.cpp:268

References load_port_bitmap().

Referenced by MaxOS::processes::resources::ThreadResource::write().

◆ enable_port()

void Thread::enable_port ( uint64_t  port)

Sets bits in the threads TSS to allow a port for I/O.

Definition at line 247 of file process.cpp.

247 {
248
249 // Enable the port
250 m_io_bitmap[port / 8] &= ~(1U << (port % 8));
252
253}

References load_port_bitmap().

Referenced by MaxOS::processes::resources::ThreadResource::write().

◆ load_port_bitmap()

void Thread::load_port_bitmap ( )

Copy this ports.

Definition at line 268 of file process.cpp.

268 {
269
270 memcpy(CPU::executing_core()->tss.io_bitmap, &m_io_bitmap, sizeof(m_io_bitmap));
271
272}

References MaxOS::system::CPU::executing_core().

Referenced by disable_port(), and enable_port().

◆ restore_sse_state()

void Thread::restore_sse_state ( )

Restores the SSE, x87 FPU, and MMX states from the thread to memory.

Definition at line 138 of file process.cpp.

138 {
139
140 // Ensure the state saving is enabled
141 if (!CPU::executing_core()->xsave_enabled)
142 return;
143
144 // Restore the state
145 asm volatile("fxrstor %0" : : "m" (m_sse_save_region));
146}

References MaxOS::system::CPU::executing_core().

◆ save_cpu_state()

void Thread::save_cpu_state ( )

Saves the CPU state into the thread's execution_state structure.

Definition at line 240 of file process.cpp.

240 {
242}
void save_cpu_state_naked(volatile void *location) __attribute__((naked))
Save the state of the CPU at the exit of this function.
Definition process.cpp:153

References execution_state, and save_cpu_state_naked().

Referenced by yield().

◆ save_sse_state()

void Thread::save_sse_state ( )

Saves the SSE, x87 FPU, and MMX states from memory to the thread.

Definition at line 125 of file process.cpp.

125 {
126
127 // Ensure the state saving is enabled
128 if (!CPU::executing_core()->xsave_enabled)
129 return;
130
131 // Save the state
132 asm volatile("fxsave %0" : "=m" (m_sse_save_region));
133}

References MaxOS::system::CPU::executing_core().

Referenced by MaxOS::processes::Scheduler::schedule_next().

◆ set_tss_pointer()

void MaxOS::processes::Thread::set_tss_pointer ( uintptr_t  new_tss_pointer)
inline

Sets the stack pointer to use for the TSS when switching to this thread.

Definition at line 78 of file process.h.

◆ sleep()

void Thread::sleep ( size_t  milliseconds)

Sleeps the thread for a certain amount of time.

Parameters
millisecondsThe amount of time to sleep for

Definition at line 92 of file process.cpp.

92 {
93
94 // Update the state
95 thread_state = ThreadState::SLEEPING;
96 wakeup_time = milliseconds;
97 yield();
98}
void yield()
Pass execution to the next ready thread.
Definition process.cpp:103

References thread_state, wakeup_time, and yield().

Referenced by MaxOS::processes::resources::ThreadResource::write().

◆ tss_pointer()

uintptr_t MaxOS::processes::Thread::tss_pointer ( ) const
inline

Gets the stack pointer to use for the TSS when switching to this thread.

Returns
tss

Definition at line 77 of file process.h.

◆ yield()

void Thread::yield ( )

Pass execution to the next ready thread.

Definition at line 103 of file process.cpp.

103 {
104
105 // Only update the state if the caller hasn't specified the resume condition state
106 if (thread_state == ThreadState::RUNNING)
107 thread_state = ThreadState::READY;
108
109 // Save a checkpoint (resumed state is ret of the below function: execution after yield will be there)
111
112 // Schedule the replacement thread, but guard against infinite loops where thread continusly pass execution to the
113 // next thread after scheduler resumes it post yield)
114 if(thread_state != ThreadState::RUNNING){
115
116 // Yield to the next thread
119 }
120}
static void ForceInterruptReturn(system::cpu_status_t *state)
Force the CPU to return from an interrupt (see interrupts.s)
static Scheduler * core_scheduler()
Gets the scheduler for the currently executing core.
system::cpu_status_t * schedule_next(system::cpu_status_t *status)
Schedules the next thread to run.
void save_cpu_state()
Saves the CPU state into the thread's execution_state structure.
Definition process.cpp:240
struct PACKED MaxOS::system::CPUStatus cpu_status_t
Alias for CPUStatus struct.

References MaxOS::processes::GlobalScheduler::core_scheduler(), execution_state, MaxOS::hardwarecommunication::InterruptManager::ForceInterruptReturn(), save_cpu_state(), MaxOS::processes::Scheduler::schedule_next(), and thread_state.

Referenced by MaxOS::runtime::TrapFlagExcpetion::handle_interrupt(), MaxOS::runtime::BreakPointException::handle_interrupt(), MaxOS::processes::SharedMessageEndpoint::read(), MaxOS::processes::resources::BridgeHandler::send_to_bridge(), sleep(), and MaxOS::processes::resources::ThreadResource::write().

Member Data Documentation

◆ execution_state

volatile system::cpu_status_t MaxOS::processes::Thread::execution_state

◆ parent_pid

◆ thread_state

◆ ticks

volatile size_t MaxOS::processes::Thread::ticks

The number of ticks the thread has run for.

Definition at line 74 of file process.h.

Referenced by MaxOS::processes::resources::ThreadResource::read(), MaxOS::processes::Scheduler::schedule(), and Thread().

◆ tid

◆ wakeup_time

volatile size_t MaxOS::processes::Thread::wakeup_time

The time at which the thread should wake up (if sleeping)

Definition at line 75 of file process.h.

Referenced by MaxOS::processes::resources::ThreadResource::read(), MaxOS::processes::Scheduler::schedule_next(), sleep(), and Thread().


The documentation for this class was generated from the following files: