PatchworkOS
Loading...
Searching...
No Matches
Stack Pointer

Helpers for managing stacks. More...

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.
 

Detailed Description

Helpers for managing stacks.

Macro Definition Documentation

◆ STACK_POINTER_GUARD_PAGES

#define STACK_POINTER_GUARD_PAGES   1

The amount of guard pages to use for stacks.

Definition at line 19 of file stack_pointer.h.

Function Documentation

◆ stack_pointer_deinit()

void stack_pointer_deinit ( stack_pointer_t stack,
thread_t thread 
)

Deinitializes a stack pointer structure and unmaps any mapped memory.

Parameters
stackThe stack pointer structure to deinitialize.
threadThe thread that owns the stack, used to get the address space to unmap the memory from.

Definition at line 59 of file stack_pointer.c.

References BYTES_TO_PAGES, NULL, thread_t::process, process_t::space, stack, and vmm_unmap().

◆ stack_pointer_deinit_buffer()

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.

Parameters
stackThe stack pointer structure to deinitialize.

Definition at line 75 of file stack_pointer.c.

References NULL, and stack.

Referenced by cpu_init().

◆ stack_pointer_init()

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.

Parameters
stackThe stack pointer structure to initialize.
maxAddressThe maximum address the stack will start at, must be page aligned.
maxPagesThe maximum amount of pages the stack can grow to, must not be 0.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 7 of file stack_pointer.c.

References EINVAL, EOVERFLOW, ERR, errno, maxPages, NULL, PAGE_SIZE, stack, STACK_POINTER_GUARD_PAGES, and VMM_IS_PAGE_ALIGNED.

Referenced by thread_init().

◆ stack_pointer_init_buffer()

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.

Parameters
stackThe stack pointer structure to initialize.
bufferThe buffer to use for the stack, must be page aligned.
pagesThe amount of pages the stack will use, must not be 0.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 34 of file stack_pointer.c.

References buffer, EINVAL, EOVERFLOW, ERR, errno, memset(), NULL, PAGE_SIZE, stack, and VMM_IS_PAGE_ALIGNED.

Referenced by cpu_init().

◆ stack_pointer_is_in_stack()

bool stack_pointer_is_in_stack ( stack_pointer_t stack,
uintptr_t  addr,
uint64_t  length 
)

Check if an region is within the stack.

Parameters
stackThe stack pointer structure.
addrThe starting address of the region.
lengthThe length of the region, in bytes.
Returns
true if the region is within the stack, false otherwise.

Definition at line 89 of file stack_pointer.c.

References NULL, and stack.

Referenced by space_pin(), space_pin_terminated(), and thread_handle_page_fault().

◆ stack_pointer_overlaps_guard()

bool stack_pointer_overlaps_guard ( stack_pointer_t stack,
uintptr_t  addr,
uint64_t  length 
)

Check if an region overlaps the guard.

Parameters
stackThe stack pointer structure.
addrThe starting address of the region.
lengthThe length of the region, in bytes.
Returns
true if the region overlaps the guard, false otherwise.

Definition at line 105 of file stack_pointer.c.

References NULL, and stack.