|
PatchworkOS
c9fea19
A non-POSIX operating system.
|
Helpers for managing stacks. More...
Helpers for managing stacks.
Data Structures | |
| struct | stack_pointer_t |
| Structure to define a stack in memory. More... | |
Macros | |
| #define | STACK_POINTER_GUARD_PAGES 1 |
| The amount of guard pages to use for stacks. | |
Functions | |
| uint64_t | stack_pointer_init (stack_pointer_t *stack, uintptr_t maxAddress, uint64_t maxPages) |
| Initializes a stack pointer structure, does not allocate or map any memory. | |
| uint64_t | stack_pointer_init_buffer (stack_pointer_t *stack, void *buffer, uint64_t pages) |
| Initializes a stack pointer structure using a provided buffer, does not allocate or map any memory. | |
| void | stack_pointer_deinit (stack_pointer_t *stack, thread_t *thread) |
| Deinitializes a stack pointer structure and unmaps any mapped memory. | |
| void | stack_pointer_deinit_buffer (stack_pointer_t *stack) |
Deinitializes a stack pointer structure that was initialized using stack_pointer_init_buffer(). | |
| bool | stack_pointer_is_in_stack (stack_pointer_t *stack, uintptr_t addr, uint64_t length) |
| Check if an region is within the stack. | |
| bool | stack_pointer_overlaps_guard (stack_pointer_t *stack, uintptr_t addr, uint64_t length) |
| Check if an region overlaps the guard. | |
| void | stack_pointer_poke (uint64_t offset) |
| Poke the stack to ensure that a page fault will occur at the given offset. | |
| #define STACK_POINTER_GUARD_PAGES 1 |
The amount of guard pages to use for stacks.
Definition at line 19 of file stack_pointer.h.
| uint64_t stack_pointer_init | ( | stack_pointer_t * | stack, |
| uintptr_t | maxAddress, | ||
| uint64_t | maxPages | ||
| ) |
Initializes a stack pointer structure, does not allocate or map any memory.
This is used to create stacks that grow dynamically, for example the kernel and user stacks of a thread.
| stack | The stack pointer structure to initialize. |
| maxAddress | The maximum address the stack will start at, must be page aligned. |
| maxPages | The maximum amount of pages the stack can grow to, must not be 0. |
0. On failure, ERR and errno is set. Definition at line 8 of file stack_pointer.c.
| uint64_t stack_pointer_init_buffer | ( | stack_pointer_t * | stack, |
| void * | buffer, | ||
| uint64_t | pages | ||
| ) |
Initializes a stack pointer structure using a provided buffer, does not allocate or map any memory.
This is used to create stacks that do not grow dynamically, for example the exception and double fault stacks of a CPU.
These stacks will not have a guard page.
Will not take ownership of the provided buffer.
| stack | The stack pointer structure to initialize. |
| buffer | The buffer to use for the stack, must be page aligned. |
| pages | The amount of pages the stack will use, must not be 0. |
0. On failure, ERR and errno is set. Definition at line 35 of file stack_pointer.c.
| void stack_pointer_deinit | ( | stack_pointer_t * | stack, |
| thread_t * | thread | ||
| ) |
Deinitializes a stack pointer structure and unmaps any mapped memory.
| stack | The stack pointer structure to deinitialize. |
| thread | The thread that owns the stack, used to get the address space to unmap the memory from. |
Definition at line 60 of file stack_pointer.c.
| void stack_pointer_deinit_buffer | ( | stack_pointer_t * | stack | ) |
Deinitializes a stack pointer structure that was initialized using stack_pointer_init_buffer().
This will not unmap any memory as the memory was provided by the caller.
| stack | The stack pointer structure to deinitialize. |
Definition at line 76 of file stack_pointer.c.
| bool stack_pointer_is_in_stack | ( | stack_pointer_t * | stack, |
| uintptr_t | addr, | ||
| uint64_t | length | ||
| ) |
Check if an region is within the stack.
| stack | The stack pointer structure. |
| addr | The starting address of the region. |
| length | The length of the region, in bytes. |
true if the region is within the stack, false otherwise. Definition at line 90 of file stack_pointer.c.
| bool stack_pointer_overlaps_guard | ( | stack_pointer_t * | stack, |
| uintptr_t | addr, | ||
| uint64_t | length | ||
| ) |
Check if an region overlaps the guard.
| stack | The stack pointer structure. |
| addr | The starting address of the region. |
| length | The length of the region, in bytes. |
true if the region overlaps the guard, false otherwise. Definition at line 106 of file stack_pointer.c.
| void stack_pointer_poke | ( | uint64_t | offset | ) |
Poke the stack to ensure that a page fault will occur at the given offset.
Used to avoid recursive page faults when handling stack overflows. For example, to grow the stack we need the virtual memory manager, but what if we run out of stack while in the VMM? We use this function to make sure the VMM will never run out of stack, to avoid this situation.
| offset | The max offset from the current stack pointer to poke, in bytes. Will poke every PAGE_SIZE bytes up to the offset. |
Definition at line 122 of file stack_pointer.c.