|
ginnOS
The reference: libc, the libraries and the kernel
|
#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". | |
| 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.
| 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. |
| 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. |
| 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. |
| 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. |
| 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. |
| 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. |
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. |
| 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_cpu_set_base | ( | struct cpu * | cpu | ) |
| 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. |
| 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).
| 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".
| buffer | receives the name, terminated by 0. |
| size | the size of buffer, at most ARCH_CPU_MODEL_MAX is used. |