|
ginnOS
The reference: libc, the libraries and the kernel
|
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). | |
| #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 |
| typedef uintptr_t 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.
| 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.
| void arch_enable_interrupts | ( | void | ) |
enable hardware interrupts.
| 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.
| 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.
| flags | the value returned by arch_irq_save(). |
| bool arch_irqs_enabled | ( | void | ) |
check whether hardware interrupts are currently enabled.
meant for assertions such as "must be called with interrupts masked".
| void arch_halt | ( | void | ) |
halt the cpu until the next interrupt arrives.
interrupts must be enabled before calling this.
| 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.
| void arch_halt_forever | ( | void | ) |
disable interrupts and halt permanently.
used for fatal error paths where execution must stop.
| 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.
a random word from the CPU's own generator (RDSEED, else RDRAND).
| uintptr_t arch_get_stack_pointer | ( | void | ) |
read the current stack pointer.
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.
| frame | the frame pointer to start from (e.g. __builtin_frame_address(0)). |
| pcs | receives the return addresses, innermost first. |
| max | capacity of pcs. |
| 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.
| 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().
| 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).
| dst | the new address space (from arch_create_address_space()). |
| src | the address space to copy. |
| 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.
| as | the address space to destroy. |
| 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.
| as | the address space. |
set aside a kernel-only virtual range whose mappings every address space shares.
call during boot, before the first arch_create_address_space().
| start | first virtual address of the range. |
| end | first virtual address past the range. |
| 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.
| virt | page-aligned virtual address inside a reserved range. |
| phys | page-aligned physical frame. |
| 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.
| virt | page-aligned virtual address inside a reserved range. |
| phys | page-aligned physical address of the device memory. |
| 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.
| virt | page-aligned virtual address inside a reserved range. |
| phys | page-aligned physical address of the memory. |
| void arch_unmap_kernel_page | ( | uintptr_t | virt | ) |
remove a page mapped with arch_map_kernel_page().
the frame is not freed.
| virt | page-aligned virtual address. |
| 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.
| as | the address space to check. |
| addr | first byte of the range. |
| len | length in bytes. |
| write | true if the range will be written. |
| 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.
| as | the target address space. |
| virt | page-aligned virtual address. |
| phys | page-aligned physical frame. |
| flags | MMU_FLAG_* bits describing the mapping. |
| 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.
| as | the address space. |
| virt | page-aligned user address. |
| 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.
| as | the address space. |
| virt | page-aligned user address. |
| flags | MMU_FLAG_* bits; without MMU_FLAG_USER the page stays mapped but user code can no longer touch it. |
| 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).
| as | the address space to walk. |
| virt | the virtual address to translate. |
| void arch_switch_address_space | ( | addr_space_t | as | ) |
switch the active address space.
| as | the address space to make current. |
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).
| start | the first address, page-aligned. |
| end | the address after the range. |
| 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.
| base | the address, or 0. |
drop to usermode (ring 3) and begin executing at entry with the given stack.
this function never returns.
| entry | userspace entry point address. |
| user_esp | userspace stack pointer. |
| 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.
| 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.
| void arch_fpu_save | ( | void * | area | ) |
save the CPU's FPU/SSE registers.
| area | ARCH_FPU_STATE_SIZE bytes, 16-byte aligned. |
| void arch_fpu_restore | ( | const void * | area | ) |
load the FPU/SSE registers.
| area | state saved by arch_fpu_save (or arch_fpu_clean). |
| 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).