The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
kstack.h File Reference
#include "common/stdint.h"
#include "kernel/memory/page.h"

Go to the source code of this file.

Macros

#define KSTACK_GUARD_SIZE   PAGE_SIZE
 kernel stacks with guard pages.

Functions

void kstack_init (void)
 reserve the kernel stack region.
uintptr_t kstack_alloc (void)
 allocate and map a zeroed kernel stack.
void kstack_free (uintptr_t base)
 unmap a kernel stack and return its frames and slot.
bool kstack_is_guard (uintptr_t addr)
 check whether an address falls in the guard page of any stack slot.

Macro Definition Documentation

◆ KSTACK_GUARD_SIZE

#define KSTACK_GUARD_SIZE   PAGE_SIZE

kernel stacks with guard pages.

every process's kernel stack lives in a fixed virtual region (KSTACK_REGION_BASE) split into one slot per process. a slot is an unmapped guard page followed by KERNEL_STACK_SIZE bytes of mapped stack, so running off the bottom of a stack faults on the guard page instead of silently overwriting whatever physical frame lies below it. the region's page table is reserved at boot and shared by every address space, so a stack mapped later is reachable from any process.

Function Documentation

◆ kstack_init()

void kstack_init ( void )

reserve the kernel stack region.

must run after paging_init() and before the first address space is created (process_create()).

◆ kstack_alloc()

uintptr_t kstack_alloc ( void )

allocate and map a zeroed kernel stack.

Returns
the lowest address of the stack (it grows down from base + KERNEL_STACK_SIZE), or 0 if no slot or memory is free.

◆ kstack_free()

void kstack_free ( uintptr_t base)

unmap a kernel stack and return its frames and slot.

never free the stack the CPU is currently running on.

Parameters
basethe value kstack_alloc() returned.

◆ kstack_is_guard()

bool kstack_is_guard ( uintptr_t addr)

check whether an address falls in the guard page of any stack slot.

Parameters
addrthe address to check (e.g. a page fault's CR2).
Returns
true if addr is inside a guard page.