The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
arch_impl.c File Reference
#include "arch/arch.h"
#include "arch/arch_cpu.h"
#include "arch/x86/cpu/io.h"
#include "arch/x86/cpu/gdt.h"
#include "arch/x86/cpu/paging.h"
#include "arch/x86/cpu/pat.h"
#include "common/string.h"
#include "kernel/cpu/cpu.h"

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)
 disable interrupts and halt permanently.
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.
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.
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).
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_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.
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.
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.
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_cpu_set_base (struct cpu *cpu)
 point the per-CPU segment of the core that runs the call at its cpu_t.
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)
 drop to usermode (ring 3) and begin executing at entry with the given stack.
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).
void arch_cpu_model (char *buffer, size_t size)
 the model name of the CPU, as the CPU gives it (the CPUID brand string on x86), or "unknown".

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_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_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_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_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_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_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_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_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_cpu_set_base()

void arch_cpu_set_base ( struct cpu * cpu)

point the per-CPU segment of the core that runs the call at its cpu_t.

the first field of a cpu_t must point at the cpu_t itself.

Parameters
cputhe data of this core.

◆ 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_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_cpu_model()

void arch_cpu_model ( char * buffer,
size_t size )

the model name of the CPU, as the CPU gives it (the CPUID brand string on x86), or "unknown".

Parameters
bufferreceives the name, terminated by 0.
sizethe size of buffer, at most ARCH_CPU_MODEL_MAX is used.