PatchworkOS
Loading...
Searching...
No Matches

Thread of execution. More...

Data Structures

struct  thread_t
 Thread of execution structure. More...
 

Enumerations

enum  thread_state_t {
  THREAD_PARKED = 0 ,
  THREAD_READY ,
  THREAD_RUNNING ,
  THREAD_PRE_BLOCK ,
  THREAD_BLOCKED ,
  THREAD_UNBLOCKING
}
 Thread state enum. More...
 

Functions

thread_tthread_new (process_t *process)
 Creates a new thread structure.
 
void thread_free (thread_t *thread)
 Frees a thread structure.
 
void thread_save (thread_t *thread, const interrupt_frame_t *frame)
 Save state to a thread.
 
void thread_load (thread_t *thread, interrupt_frame_t *frame)
 Load state from a thread.
 
_NORETURN void thread_jump (thread_t *thread)
 Jump to a thread by calling thread_load() and then loading its interrupt frame.
 
void thread_get_interrupt_frame (thread_t *thread, interrupt_frame_t *frame)
 Retrieve the interrupt frame from a thread.
 
bool thread_is_note_pending (thread_t *thread)
 Check if a thread has a note pending.
 
uint64_t thread_send_note (thread_t *thread, const void *buffer, uint64_t count)
 Send a note to a thread.
 
thread_tthread_get_boot (void)
 Retrieves the boot thread.
 
uint64_t thread_handle_page_fault (const interrupt_frame_t *frame)
 Handles a page fault that occurred in the currently running thread.
 
uint64_t thread_copy_from_user (thread_t *thread, void *dest, const void *userSrc, uint64_t length)
 Safely copy data from user space.
 
uint64_t thread_copy_to_user (thread_t *thread, void *dest, const void *userSrc, uint64_t length)
 Safely copy data to user space.
 
uint64_t thread_copy_from_user_terminated (thread_t *thread, const void *userArray, const void *terminator, uint8_t objectSize, uint64_t maxCount, void **outArray, uint64_t *outCount)
 Safely copy a null-terminated array of objects from user space.
 
uint64_t thread_copy_from_user_pathname (thread_t *thread, pathname_t *pathname, const char *userPath)
 Safely copy a string from user space and use it to initialize a pathname.
 
uint64_t thread_load_atomic_from_user (thread_t *thread, atomic_uint64_t *userObj, uint64_t *outValue)
 Atomically load a 64-bit value from a user-space atomic variable.
 

Detailed Description

Thread of execution.

Enumeration Type Documentation

◆ thread_state_t

Thread state enum.

Enumerator
THREAD_PARKED 

Is doing nothing, not in a queue, not blocking, think of it as "other".

THREAD_READY 

Is ready to run and waiting to be scheduled.

THREAD_RUNNING 

Is currently running on a cpu.

THREAD_PRE_BLOCK 

Has started the process of blocking but has not yet been given to a owner cpu.

THREAD_BLOCKED 

Is blocking and waiting in one or multiple wait queues.

THREAD_UNBLOCKING 

Has started unblocking, used to prevent the same thread being unblocked multiple times.

Definition at line 28 of file thread.h.

Function Documentation

◆ thread_copy_from_user()

uint64_t thread_copy_from_user ( thread_t thread,
void *  dest,
const void *  userSrc,
uint64_t  length 
)

Safely copy data from user space.

Will pin the user pages in memory while performing the copy and expand the user stack if necessary.

Parameters
threadThe thread performing the operation.
destThe destination buffer in kernel space.
userSrcThe source buffer in user space.
lengthThe number of bytes to copy.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 230 of file thread.c.

References EINVAL, ERR, errno, memcpy(), NULL, thread_t::process, process_t::space, space_pin(), space_unpin(), and thread_t::userStack.

Referenced by SYSCALL_DEFINE().

◆ thread_copy_from_user_pathname()

uint64_t thread_copy_from_user_pathname ( thread_t thread,
pathname_t pathname,
const char *  userPath 
)

Safely copy a string from user space and use it to initialize a pathname.

Parameters
threadThe thread performing the operation.
pathnameA pointer to the pathname to initialize.
userPathThe string in user space.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 307 of file thread.c.

References EINVAL, ERR, errno, MAX_PATH, memcpy(), NULL, pathname_init(), thread_t::process, process_t::space, space_pin_terminated(), space_unpin(), and thread_t::userStack.

Referenced by SYSCALL_DEFINE(), SYSCALL_DEFINE(), SYSCALL_DEFINE(), SYSCALL_DEFINE(), SYSCALL_DEFINE(), SYSCALL_DEFINE(), SYSCALL_DEFINE(), and SYSCALL_DEFINE().

◆ thread_copy_from_user_terminated()

uint64_t thread_copy_from_user_terminated ( thread_t thread,
const void *  userArray,
const void *  terminator,
uint8_t  objectSize,
uint64_t  maxCount,
void **  outArray,
uint64_t outCount 
)

Safely copy a null-terminated array of objects from user space.

Parameters
threadThe thread performing the operation.
userArrayThe source array in user space.
terminatorA pointer to the terminator object.
objectSizeThe size of each object in the array.
maxCountThe maximum number of objects to copy.
outArrayOutput pointer to store the allocated array in kernel space, must be freed by the caller.
outCountOutput pointer to store the number of objects copied, can be NULL.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 266 of file thread.c.

References EINVAL, ENOMEM, ERR, errno, malloc(), memcpy(), NULL, thread_t::process, process_t::space, space_pin_terminated(), space_unpin(), and thread_t::userStack.

Referenced by SYSCALL_DEFINE().

◆ thread_copy_to_user()

uint64_t thread_copy_to_user ( thread_t thread,
void *  dest,
const void *  userSrc,
uint64_t  length 
)

Safely copy data to user space.

Will pin the user pages in memory while performing the copy and expand the user stack if necessary.

Parameters
threadThe thread performing the operation.
destThe destination buffer in user space.
userSrcThe source buffer in kernel space.
lengthThe number of bytes to copy.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 248 of file thread.c.

References EINVAL, ERR, errno, memcpy(), NULL, thread_t::process, process_t::space, space_pin(), space_unpin(), and thread_t::userStack.

Referenced by SYSCALL_DEFINE(), SYSCALL_DEFINE(), and SYSCALL_DEFINE().

◆ thread_free()

void thread_free ( thread_t thread)

◆ thread_get_boot()

thread_t * thread_get_boot ( void  )

Retrieves the boot thread.

The boot thread is the first thread created by the kernel. After boot it becomes the idle thread of the bootstrap CPU. Is initialized lazily on the first call to this function, which should happen during early boot.

Will never return NULL.

Returns
The boot thread.

Definition at line 159 of file thread.c.

References bootThread, bootThreadInitalized, ERR, process_t::id, thread_t::id, LOG_INFO, NULL, panic(), thread_t::process, process_get_kernel(), and thread_init().

Referenced by init_early(), and init_finalize().

◆ thread_get_interrupt_frame()

void thread_get_interrupt_frame ( thread_t thread,
interrupt_frame_t frame 
)

Retrieve the interrupt frame from a thread.

Will only retrieve the interrupt frame.

Parameters
threadThe source thread.
frameThe destination interrupt frame.

Definition at line 122 of file thread.c.

References thread_t::frame.

◆ thread_handle_page_fault()

uint64_t thread_handle_page_fault ( const interrupt_frame_t frame)

Handles a page fault that occurred in the currently running thread.

Called by the interrupt handler when a page fault occurs and allows the currently running thread to attempt to grow its stacks if the faulting address is within one of its stack regions.

Parameters
frameThe interrupt frame containing the CPU state at the time of the page fault.
Returns
If the page fault was handled and the thread can continue executing, returns 0. If the thread must be killed, ERR and errno is set.

Definition at line 173 of file thread.c.

References cr2_read(), EEXIST, EFAULT, ERR, errno, interrupt_frame_t::errorCode, INTERRUPT_FRAME_IN_USER_SPACE, thread_t::kernelStack, memset(), NULL, PAGE_FAULT_PRESENT, PAGE_SIZE, PML_PRESENT, PML_USER, PML_WRITE, thread_t::process, ROUND_DOWN, sched_thread_unsafe(), process_t::space, stack_pointer_is_in_stack(), thread_t::userStack, vmm_alloc(), and VMM_ALLOC_FAIL_IF_MAPPED.

Referenced by exception_handler().

◆ thread_is_note_pending()

bool thread_is_note_pending ( thread_t thread)

Check if a thread has a note pending.

Parameters
threadThe thread to query.
Returns
True if there is a note pending, false otherwise.

Definition at line 127 of file thread.c.

References note_queue_length(), and thread_t::notes.

Referenced by wait_block_finalize().

◆ thread_jump()

_NORETURN void thread_jump ( thread_t thread)
extern

Jump to a thread by calling thread_load() and then loading its interrupt frame.

Must be done in assembly as it requires directly modifying registers.

Will never return instead it ends up at thread->frame.rip.

Parameters
threadThe thread to jump to.

Referenced by init_early(), and trampoline_c_entry().

◆ thread_load()

void thread_load ( thread_t thread,
interrupt_frame_t frame 
)

Load state from a thread.

Will retrieve the interrupt frame and setup the CPU with the threads contexts/data.

Parameters
threadThe source thread to load state from.
frameThe destination interrupt frame.

Definition at line 113 of file thread.c.

References thread_t::frame, thread_t::process, thread_t::simd, simd_ctx_load(), process_t::space, space_load(), thread_t::syscall, and syscall_ctx_load().

Referenced by sched_invoke().

◆ thread_load_atomic_from_user()

uint64_t thread_load_atomic_from_user ( thread_t thread,
atomic_uint64_t *  userObj,
uint64_t outValue 
)

Atomically load a 64-bit value from a user-space atomic variable.

Will pin the user pages in memory while performing the load and expand the user stack if necessary.

Parameters
threadThe thread performing the operation.
userObjThe user-space atomic variable to load from.
outValueOutput pointer to store the loaded value.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 336 of file thread.c.

References atomic_load, EINVAL, ERR, errno, NULL, thread_t::process, process_t::space, space_pin(), space_unpin(), and thread_t::userStack.

Referenced by SYSCALL_DEFINE().

◆ thread_new()

thread_t * thread_new ( process_t process)

Creates a new thread structure.

Does not push the created thread to the scheduler or similar, merely handling allocation and initialization.

Parameters
processThe parent process that the thread will execute within.
Returns
On success, returns the newly created thread. On failure, returns NULL and errno is set.

Definition at line 69 of file thread.c.

References atomic_load, ERR, free(), process_t::isDying, malloc(), NULL, and thread_init().

Referenced by loader_spawn(), loader_thread_create(), and sched_cpu_ctx_init().

◆ thread_save()

void thread_save ( thread_t thread,
const interrupt_frame_t frame 
)

Save state to a thread.

Parameters
threadThe destination thread where the state will be saved.
frameThe source frame..

Definition at line 107 of file thread.c.

References thread_t::frame, thread_t::simd, and simd_ctx_save().

Referenced by sched_invoke().

◆ thread_send_note()

uint64_t thread_send_note ( thread_t thread,
const void *  buffer,
uint64_t  count 
)

Send a note to a thread.

This function should always be used over the note_queue_push() function, as it performs additional checks, like deciding how critical the sent note is and unblocking the thread to notify it of the received note.

Parameters
threadThe destination thread.
bufferThe buffer to write.
countThe number of bytes to write.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 132 of file thread.c.

References atomic_compare_exchange_strong, buffer, count, EINTR, ERR, process_t::id, thread_t::id, LOG_DEBUG, note_queue_write(), thread_t::notes, thread_t::process, THREAD_BLOCKED, THREAD_UNBLOCKING, and wait_unblock_thread().

Referenced by process_kill(), and process_note_write().