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

Go to the source code of this file.

Macros

#define ADDR_SPACE_NONE   ((addr_space_t)0)
 sentinel handle meaning "no address space".
#define MMU_FLAG_PRESENT   (1u << 0)
#define MMU_FLAG_WRITE   (1u << 1)
#define MMU_FLAG_USER   (1u << 2)
#define MMU_FLAG_SHARED   (1u << 3)
#define MMU_USER_RW   (MMU_FLAG_PRESENT | MMU_FLAG_WRITE | MMU_FLAG_USER)
#define ARCH_TLB_FLUSH_CEILING   33u
 the most pages arch_tlb_flush_local() drops one at a time (Linux's tlb_single_page_flush_ceiling); a larger range flushes the whole TLB
#define ARCH_FPU_STATE_SIZE   512

Typedefs

typedef uintptr_t addr_space_t
 an address space handle.

Functions

void arch_disable_interrupts (void)
 arch-neutral cpu primitives.
void arch_enable_interrupts (void)
 enable hardware interrupts.
uint32_t arch_irq_save (void)
 disable interrupts and return the prior interrupt state.
void arch_irq_restore (uint32_t flags)
 restore a previously saved interrupt state.
bool arch_irqs_enabled (void)
 check whether hardware interrupts are currently enabled.
void arch_halt (void)
 halt the cpu until the next interrupt arrives.
void arch_safe_halt (void)
 enable interrupts and halt until the next interrupt, as one step: an interrupt that comes after the last check of the caller still ends the halt (Linux's safe_halt).
void arch_halt_forever (void) __attribute__((noreturn))
 disable interrupts and halt permanently.
uint64_t arch_cycle_count (void)
 a fast counter that goes up with each CPU cycle (the TSC on x86).
bool arch_hw_random (uintptr_t *out)
 a random word from the CPU's own generator (RDSEED, else RDRAND).
uintptr_t arch_get_stack_pointer (void)
 read the current stack pointer.
int arch_stack_walk (uintptr_t frame, uintptr_t *pcs, int max)
 walk a chain of saved frame pointers and collect return addresses.
addr_space_t arch_kernel_address_space (void)
 return the handle for the kernel's own address space.
addr_space_t arch_create_address_space (void)
 create a new address space that shares the kernel mappings.
bool arch_copy_user_space (addr_space_t dst, addr_space_t src)
 copy the user half of an address space into a new, empty one (fork).
void arch_destroy_address_space (addr_space_t as)
 tear down an address space created with arch_create_address_space(), freeing its user page tables and mapped frames.
uint32_t arch_count_user_pages (addr_space_t as)
 count the pages of RAM mapped in the user half of an address space (a process's resident memory).
void arch_reserve_kernel_range (uintptr_t start, uintptr_t end)
 set aside a kernel-only virtual range whose mappings every address space shares.
void arch_map_kernel_page (uintptr_t virt, phys_addr_t phys)
 map one kernel-only, writable page inside a reserved kernel range.
void arch_map_device_page (uintptr_t virt, phys_addr_t phys)
 like arch_map_kernel_page(), but the page is not cached: for device registers, where each read and write must go to the device.
void arch_map_wc_page (uintptr_t virt, phys_addr_t phys)
 like arch_map_kernel_page(), but write-combining if the CPU can do it (uncached otherwise): for a framebuffer.
void arch_unmap_kernel_page (uintptr_t virt)
 remove a page mapped with arch_map_kernel_page().
bool arch_user_access_ok (addr_space_t as, uintptr_t addr, size_t len, bool write)
 check that user code in an address space could access a whole range: every page present and user-accessible, and writable if write is set.
int arch_map_page (addr_space_t as, uintptr_t virt, phys_addr_t phys, uint32_t flags)
 map one page into a given address space.
phys_addr_t arch_unmap_page (addr_space_t as, uintptr_t virt)
 remove a user page mapping (and its TLB entry).
bool arch_protect_page (addr_space_t as, uintptr_t virt, uint32_t flags)
 change the protection of a mapped user page, keeping its frame.
phys_addr_t arch_translate_in (addr_space_t as, uintptr_t virt)
 translate a virtual address to its physical address within a given address space.
void arch_switch_address_space (addr_space_t as)
 switch the active address space.
void arch_tlb_flush_local (uintptr_t start, uintptr_t end)
 drop the TLB entries of the core that runs the call for the pages in [start, end), or all its entries if the range has more than ARCH_TLB_FLUSH_CEILING pages.
void arch_set_tls (uintptr_t base)
 set the thread pointer user code reaches its thread-local storage through: the FS base on x86_64 (fs:0), the base of the TLS segment in GS on i686 (gs:0).
void arch_jump_to_usermode (uintptr_t entry, uintptr_t user_esp) __attribute__((noreturn))
 drop to usermode (ring 3) and begin executing at entry with the given stack.
void arch_fpu_init (void)
 turn on the FPU (and SSE) for user programs and make the clean state a new program starts with.
void arch_fpu_init_core (void)
 turn on the FPU (and SSE) on the core that runs the call.
void arch_fpu_save (void *area)
 save the CPU's FPU/SSE registers.
void arch_fpu_restore (const void *area)
 load the FPU/SSE registers.
void arch_fpu_clean (void *area)
 fill an area with the state of a fresh program.
void arch_fpu_sanitize (void *area)
 make a state that came from user memory safe to load (a signal frame).

Macro Definition Documentation

◆ ADDR_SPACE_NONE

#define ADDR_SPACE_NONE   ((addr_space_t)0)

sentinel handle meaning "no address space".

◆ MMU_FLAG_PRESENT

#define MMU_FLAG_PRESENT   (1u << 0)

◆ MMU_FLAG_WRITE

#define MMU_FLAG_WRITE   (1u << 1)

◆ MMU_FLAG_USER

#define MMU_FLAG_USER   (1u << 2)

◆ MMU_FLAG_SHARED

#define MMU_FLAG_SHARED   (1u << 3)

◆ MMU_USER_RW

#define MMU_USER_RW   (MMU_FLAG_PRESENT | MMU_FLAG_WRITE | MMU_FLAG_USER)

◆ ARCH_TLB_FLUSH_CEILING

#define ARCH_TLB_FLUSH_CEILING   33u

the most pages arch_tlb_flush_local() drops one at a time (Linux's tlb_single_page_flush_ceiling); a larger range flushes the whole TLB

◆ ARCH_FPU_STATE_SIZE

#define ARCH_FPU_STATE_SIZE   512

Typedef Documentation

◆ addr_space_t

an address space handle.

opaque to portable code: it identifies a page table root but says nothing about the underlying paging structure, so the same kernel code works whether the arch uses 2-level, 4-level, or some other table layout.

Function Documentation

◆ arch_disable_interrupts()

void arch_disable_interrupts ( void )

arch-neutral cpu primitives.

portable kernel code should include this header instead of referencing architecture-specific headers directly for basic cpu operations like halting, interrupt control, and register access.

each architecture provides its own implementation. disable hardware interrupts.

◆ arch_enable_interrupts()

void arch_enable_interrupts ( void )

enable hardware interrupts.

◆ arch_irq_save()

uint32_t arch_irq_save ( void )

disable interrupts and return the prior interrupt state.

the return value is opaque and meant only to be handed back to arch_irq_restore(). use this instead of arch_disable_interrupts() when a critical section may nest inside another one.

Returns
the interrupt state as it was before disabling.

◆ arch_irq_restore()

void arch_irq_restore ( uint32_t flags)

restore a previously saved interrupt state.

re-enables interrupts only if they were enabled when the matching arch_irq_save() ran, so nested critical sections stay correct.

Parameters
flagsthe value returned by arch_irq_save().

◆ arch_irqs_enabled()

bool arch_irqs_enabled ( void )

check whether hardware interrupts are currently enabled.

meant for assertions such as "must be called with interrupts masked".

Returns
true if interrupts are enabled.

◆ arch_halt()

void arch_halt ( void )

halt the cpu until the next interrupt arrives.

interrupts must be enabled before calling this.

◆ arch_safe_halt()

void arch_safe_halt ( void )

enable interrupts and halt until the next interrupt, as one step: an interrupt that comes after the last check of the caller still ends the halt (Linux's safe_halt).

call it with interrupts disabled; they are enabled when it returns.

◆ arch_halt_forever()

void arch_halt_forever ( void )

disable interrupts and halt permanently.

used for fatal error paths where execution must stop.

◆ arch_cycle_count()

uint64_t arch_cycle_count ( void )

a fast counter that goes up with each CPU cycle (the TSC on x86).

it is not a clock: the random pool uses its low bits.

◆ arch_hw_random()

bool arch_hw_random ( uintptr_t * out)

a random word from the CPU's own generator (RDSEED, else RDRAND).

Returns
false if the CPU has none, or it gave no value after some tries.

◆ arch_get_stack_pointer()

uintptr_t arch_get_stack_pointer ( void )

read the current stack pointer.

◆ arch_stack_walk()

int arch_stack_walk ( uintptr_t frame,
uintptr_t * pcs,
int max )

walk a chain of saved frame pointers and collect return addresses.

stops at a null or misaligned frame, an unmapped frame, a frame that does not move up the stack, or after max entries, so it is safe to call on a corrupt stack from a panic.

Parameters
framethe frame pointer to start from (e.g. __builtin_frame_address(0)).
pcsreceives the return addresses, innermost first.
maxcapacity of pcs.
Returns
the number of addresses stored.

◆ arch_kernel_address_space()

addr_space_t arch_kernel_address_space ( void )

return the handle for the kernel's own address space.

useful when there is no current process to borrow one from.

◆ arch_create_address_space()

addr_space_t arch_create_address_space ( void )

create a new address space that shares the kernel mappings.

user regions start empty; map them in with arch_map_page().

Returns
the new handle, or ADDR_SPACE_NONE on failure.

◆ arch_copy_user_space()

bool arch_copy_user_space ( addr_space_t dst,
addr_space_t src )

copy the user half of an address space into a new, empty one (fork).

writable RAM pages become copy-on-write in both: read-only, with the frame shared until one of them writes (mm_fault_in copies it then).

Parameters
dstthe new address space (from arch_create_address_space()).
srcthe address space to copy.
Returns
true on success. on failure dst holds a partial copy; destroy it.

◆ arch_destroy_address_space()

void arch_destroy_address_space ( addr_space_t as)

tear down an address space created with arch_create_address_space(), freeing its user page tables and mapped frames.

the kernel address space and ADDR_SPACE_NONE are ignored.

Parameters
asthe address space to destroy.

◆ arch_count_user_pages()

uint32_t arch_count_user_pages ( addr_space_t as)

count the pages of RAM mapped in the user half of an address space (a process's resident memory).

device mappings are not counted.

Parameters
asthe address space.
Returns
the number of pages.

◆ arch_reserve_kernel_range()

void arch_reserve_kernel_range ( uintptr_t start,
uintptr_t end )

set aside a kernel-only virtual range whose mappings every address space shares.

call during boot, before the first arch_create_address_space().

Parameters
startfirst virtual address of the range.
endfirst virtual address past the range.

◆ arch_map_kernel_page()

void arch_map_kernel_page ( uintptr_t virt,
phys_addr_t phys )

map one kernel-only, writable page inside a reserved kernel range.

the mapping is visible in every address space at once.

Parameters
virtpage-aligned virtual address inside a reserved range.
physpage-aligned physical frame.

◆ arch_map_device_page()

void arch_map_device_page ( uintptr_t virt,
phys_addr_t phys )

like arch_map_kernel_page(), but the page is not cached: for device registers, where each read and write must go to the device.

Parameters
virtpage-aligned virtual address inside a reserved range.
physpage-aligned physical address of the device memory.

◆ arch_map_wc_page()

void arch_map_wc_page ( uintptr_t virt,
phys_addr_t phys )

like arch_map_kernel_page(), but write-combining if the CPU can do it (uncached otherwise): for a framebuffer.

Parameters
virtpage-aligned virtual address inside a reserved range.
physpage-aligned physical address of the memory.

◆ arch_unmap_kernel_page()

void arch_unmap_kernel_page ( uintptr_t virt)

remove a page mapped with arch_map_kernel_page().

the frame is not freed.

Parameters
virtpage-aligned virtual address.

◆ arch_user_access_ok()

bool arch_user_access_ok ( addr_space_t as,
uintptr_t addr,
size_t len,
bool write )

check that user code in an address space could access a whole range: every page present and user-accessible, and writable if write is set.

Parameters
asthe address space to check.
addrfirst byte of the range.
lenlength in bytes.
writetrue if the range will be written.
Returns
true if every page allows the access.

◆ arch_map_page()

int arch_map_page ( addr_space_t as,
uintptr_t virt,
phys_addr_t phys,
uint32_t flags )

map one page into a given address space.

Parameters
asthe target address space.
virtpage-aligned virtual address.
physpage-aligned physical frame.
flagsMMU_FLAG_* bits describing the mapping.
Returns
0 on success, -1 on failure.

◆ arch_unmap_page()

phys_addr_t arch_unmap_page ( addr_space_t as,
uintptr_t virt )

remove a user page mapping (and its TLB entry).

the frame is not freed.

Parameters
asthe address space.
virtpage-aligned user address.
Returns
the frame that was mapped there, or 0 if the page was not mapped.

◆ arch_protect_page()

bool arch_protect_page ( addr_space_t as,
uintptr_t virt,
uint32_t flags )

change the protection of a mapped user page, keeping its frame.

Parameters
asthe address space.
virtpage-aligned user address.
flagsMMU_FLAG_* bits; without MMU_FLAG_USER the page stays mapped but user code can no longer touch it.
Returns
true if the page was mapped and changed.

◆ arch_translate_in()

phys_addr_t arch_translate_in ( addr_space_t as,
uintptr_t virt )

translate a virtual address to its physical address within a given address space.

lets the kernel reach a process's memory without switching to it (e.g. to stage argv or an ELF image before the swap).

Parameters
asthe address space to walk.
virtthe virtual address to translate.
Returns
the physical address, or 0 if the page is not mapped.

◆ arch_switch_address_space()

void arch_switch_address_space ( addr_space_t as)

switch the active address space.

Parameters
asthe address space to make current.

◆ arch_tlb_flush_local()

void arch_tlb_flush_local ( uintptr_t start,
uintptr_t end )

drop the TLB entries of the core that runs the call for the pages in [start, end), or all its entries if the range has more than ARCH_TLB_FLUSH_CEILING pages.

other cores are not changed (see kernel/memory/tlb.h).

Parameters
startthe first address, page-aligned.
endthe address after the range.

◆ arch_set_tls()

void arch_set_tls ( uintptr_t base)

set the thread pointer user code reaches its thread-local storage through: the FS base on x86_64 (fs:0), the base of the TLS segment in GS on i686 (gs:0).

the scheduler sets it for each thread it runs.

Parameters
basethe address, or 0.

◆ arch_jump_to_usermode()

void arch_jump_to_usermode ( uintptr_t entry,
uintptr_t user_esp )

drop to usermode (ring 3) and begin executing at entry with the given stack.

this function never returns.

Parameters
entryuserspace entry point address.
user_espuserspace stack pointer.

◆ arch_fpu_init()

void arch_fpu_init ( void )

turn on the FPU (and SSE) for user programs and make the clean state a new program starts with.

call once at boot.

◆ arch_fpu_init_core()

void arch_fpu_init_core ( void )

turn on the FPU (and SSE) on the core that runs the call.

arch_fpu_init() does it for the boot core; each other core calls this when it starts.

◆ arch_fpu_save()

void arch_fpu_save ( void * area)

save the CPU's FPU/SSE registers.

Parameters
areaARCH_FPU_STATE_SIZE bytes, 16-byte aligned.

◆ arch_fpu_restore()

void arch_fpu_restore ( const void * area)

load the FPU/SSE registers.

Parameters
areastate saved by arch_fpu_save (or arch_fpu_clean).

◆ arch_fpu_clean()

void arch_fpu_clean ( void * area)

fill an area with the state of a fresh program.

◆ arch_fpu_sanitize()

void arch_fpu_sanitize ( void * area)

make a state that came from user memory safe to load (a signal frame).