PatchworkOS  c9fea19
A non-POSIX operating system.
Loading...
Searching...
No Matches
Stack Pointer

Helpers for managing stacks. More...

Collaboration diagram for Stack Pointer:

Detailed Description

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.
 

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_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 8 of file stack_pointer.c.

Here is the caller graph for this function:

◆ 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 35 of file stack_pointer.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 60 of file stack_pointer.c.

Here is the call graph for this function:

◆ 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 76 of file stack_pointer.c.

Here is the caller graph for this function:

◆ 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 90 of file stack_pointer.c.

Here is the caller graph for this function:

◆ 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 106 of file stack_pointer.c.

Here is the caller graph for this function:

◆ stack_pointer_poke()

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.

Parameters
offsetThe 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.

Here is the call graph for this function:
Here is the caller graph for this function: