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.
 
uintptr_t tss_pointer () const
 Gets 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.
 

Public Attributes

uint64_t tid
 The thread ID.
 
uint64_t parent_pid
 The parent process ID.
 
system::cpu_status_t execution_state
 The CPU state of the thread.
 
thread_state_t thread_state
 The current state of the thread.
 
size_t ticks
 The number of ticks the thread has run for.
 
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.

Definition at line 49 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)

Definition at line 26 of file process.cpp.

26 {
27
28 // Basic setup
29 thread_state = ThreadState::NEW;
30 wakeup_time = 0;
31 ticks = 0;
32
33 // Create the stack
34 m_stack_pointer = (uintptr_t) MemoryManager::malloc(STACK_SIZE);
35
36 // Create the TSS stack
37 if (parent->is_kernel) {
38
39 // Use the kernel stack
40 m_tss_stack_pointer = CPU::executing_core() -> tss.rsp0;
41
42 } else {
43 m_tss_stack_pointer = (uintptr_t) MemoryManager::kmalloc(STACK_SIZE) + STACK_SIZE;
44 }
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 = {};
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
62 void* argv = MemoryManager::malloc(arg_amount * sizeof(void*));
63 memcpy(argv, args, arg_amount * sizeof(void*));
64
67 //execution_state->rdx = (uint64_t)env_args;
68
69 parent_pid = parent->pid();
70}
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
static void * malloc(size_t size)
Allocates a block of memory in the current USERSPACE heap.
static void * kmalloc(size_t size)
Allocates a block of memory in the KERNEL space.
thread_state_t thread_state
The current state of the thread.
Definition process.h:68
uint64_t parent_pid
The parent process ID.
Definition process.h:65
size_t wakeup_time
The time at which the thread should wake up (if sleeping)
Definition process.h:71
system::cpu_status_t execution_state
The CPU state of the thread.
Definition process.h:67
size_t ticks
The number of ticks the thread has run for.
Definition process.h:70
static Core * executing_core()
Gets the core that is currently executing.
Definition cpu.cpp:623
#define ASSERT(condition, format,...)
If the specified condition is not met then the kernel will crash with the specified message.
Definition logger.h:100
void * memcpy(void *destination, const void *source, uint64_t num)
Copies a block of memory from one location to another.
Definition memoryIO.cpp:165
constexpr size_t STACK_SIZE
The size of the stack for each thread (4KB)
Definition process.h:43

References ASSERT, MaxOS::system::CPU::executing_core(), execution_state, MaxOS::memory::MemoryManager::kmalloc(), MaxOS::memory::MemoryManager::malloc(), memcpy(), parent_pid, MaxOS::processes::STACK_SIZE, thread_state, ticks, and wakeup_time.

Member Function Documentation

◆ restore_sse_state()

void Thread::restore_sse_state ( )

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

Definition at line 105 of file process.cpp.

105 {
106
107 // Ensure the state saving is enabled
108 if (!CPU::executing_core()->xsave_enabled)
109 return;
110
111 // Restore the state
112 asm volatile("fxrstor %0" : : "m" (m_sse_save_region));
113}

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 118 of file process.cpp.

118 {
119 asm volatile(
120 // Store return address before stack is modified
121 "movq (%%rsp), %%rax\n"
122
123 // Store RDI before stack is modified
124 "pushq %%rdi\n"
125 "movq %0, %%rdi\n"
126
127 // Store general purpose
128 "movq %%r15, 0x00(%%rdi)\n"
129 "movq %%r14, 0x08(%%rdi)\n"
130 "movq %%r13, 0x10(%%rdi)\n"
131 "movq %%r12, 0x18(%%rdi)\n"
132 "movq %%r11, 0x20(%%rdi)\n"
133 "movq %%r10, 0x28(%%rdi)\n"
134 "movq %%r9, 0x30(%%rdi)\n"
135 "movq %%r8, 0x38(%%rdi)\n"
136 "movq %%rdi, 0x40(%%rdi)\n"
137 "movq %%rsi, 0x48(%%rdi)\n"
138 "movq %%rbp, 0x50(%%rdi)\n"
139 "movq %%rdx, 0x58(%%rdi)\n"
140 "movq %%rcx, 0x60(%%rdi)\n"
141 "movq %%rbx, 0x68(%%rdi)\n"
142 "movq %%rax, 0x70(%%rdi)\n"
143
144 // Reserved
145 "movq $0, 0x78(%%rdi)\n"
146 "movq $0, 0x80(%%rdi)\n"
147
148 // RIP from earlier
149 "movq %%rax, 0x88(%%rdi)\n"
150
151 // Get cs
152 "xorq %%rax, %%rax\n"
153 "movw %%cs, %%ax\n"
154 "movq %%rax, 0x90(%%rdi)\n"
155
156 // Store flags
157 "pushfq\n"
158 "popq %%rax\n"
159 "movq %%rax, 0x98(%%rdi)\n"
160
161 // Get RSP and SS
162 "movq %%rsp, 0xA0(%%rdi)\n"
163 "xorq %%rax, %%rax\n"
164 "movw %%ss, %%ax\n"
165 "movq %%rax, 0xA8(%%rdi)\n"
166
167 // Restore RDI
168 "popq %%rdi\n"
169 :
170 : "r" (&execution_state)
171 : "rax", "cc", "memory"
172 );
173}

References execution_state.

◆ save_sse_state()

void Thread::save_sse_state ( )

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

Definition at line 92 of file process.cpp.

92 {
93
94 // Ensure the state saving is enabled
95 if (!CPU::executing_core()->xsave_enabled)
96 return;
97
98 // Save the state
99 asm volatile("fxsave %0" : "=m" (m_sse_save_region));
100}

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

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

◆ 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 82 of file process.cpp.

82 {
83
84 // Update the state
85 thread_state = ThreadState::SLEEPING;
87}

References thread_state, and wakeup_time.

◆ 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 73 of file process.h.

Member Data Documentation

◆ execution_state

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

◆ parent_pid

uint64_t MaxOS::processes::Thread::parent_pid

The parent process ID.

Definition at line 65 of file process.h.

Referenced by MaxOS::processes::Scheduler::current_process(), and Thread().

◆ thread_state

thread_state_t MaxOS::processes::Thread::thread_state

◆ ticks

size_t MaxOS::processes::Thread::ticks

The number of ticks the thread has run for.

Definition at line 70 of file process.h.

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

◆ tid

◆ wakeup_time

size_t MaxOS::processes::Thread::wakeup_time

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

Definition at line 71 of file process.h.

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


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