|
PatchworkOS
|
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_t * | thread_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_t * | thread_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. | |
Thread of execution.
| enum thread_state_t |
Thread state enum.
| 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.
| thread | The thread performing the operation. |
| dest | The destination buffer in kernel space. |
| userSrc | The source buffer in user space. |
| length | The number of bytes to copy. |
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().
| 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.
| thread | The thread performing the operation. |
| pathname | A pointer to the pathname to initialize. |
| userPath | The string in user space. |
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().
| 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.
| thread | The thread performing the operation. |
| userArray | The source array in user space. |
| terminator | A pointer to the terminator object. |
| objectSize | The size of each object in the array. |
| maxCount | The maximum number of objects to copy. |
| outArray | Output pointer to store the allocated array in kernel space, must be freed by the caller. |
| outCount | Output pointer to store the number of objects copied, can be NULL. |
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().
| 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.
| thread | The thread performing the operation. |
| dest | The destination buffer in user space. |
| userSrc | The source buffer in kernel space. |
| length | The number of bytes to copy. |
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().
| void thread_free | ( | thread_t * | thread | ) |
Frees a thread structure.
| thread | The thread to be freed. |
Definition at line 89 of file thread.c.
References assert, DEREF, free(), process_t::id, thread_t::id, process_threads_t::list, list_remove(), process_threads_t::lock, lock_acquire(), lock_release(), LOG_DEBUG, NULL, thread_t::process, thread_t::processEntry, thread_t::simd, simd_ctx_deinit(), and process_t::threads.
Referenced by sched_invoke(), and SYSCALL_DEFINE().
| 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.
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().
| 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.
| thread | The source thread. |
| frame | The destination interrupt frame. |
Definition at line 122 of file thread.c.
References thread_t::frame.
| 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.
| frame | The interrupt frame containing the CPU state at the time of the page fault. |
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().
Check if a thread has a note pending.
| thread | The thread to query. |
Definition at line 127 of file thread.c.
References note_queue_length(), and thread_t::notes.
Referenced by wait_block_finalize().
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.
| thread | The thread to jump to. |
Referenced by init_early(), and trampoline_c_entry().
| 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.
| thread | The source thread to load state from. |
| frame | The 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().
| 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.
| thread | The thread performing the operation. |
| userObj | The user-space atomic variable to load from. |
| outValue | Output pointer to store the loaded value. |
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().
Creates a new thread structure.
Does not push the created thread to the scheduler or similar, merely handling allocation and initialization.
| process | The parent process that the thread will execute within. |
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().
| void thread_save | ( | thread_t * | thread, |
| const interrupt_frame_t * | frame | ||
| ) |
Save state to a thread.
| thread | The destination thread where the state will be saved. |
| frame | The 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().
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.
| thread | The destination thread. |
| buffer | The buffer to write. |
| count | The number of bytes to write. |
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().