The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
arch.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * arch-neutral cpu primitives.
5 *
6 * portable kernel code should include this header instead of
7 * referencing architecture-specific headers directly for basic
8 * cpu operations like halting, interrupt control, and register access.
9 *
10 * each architecture provides its own implementation.
11 */
12
13#include "common/stdint.h"
14#include "kernel/memory/phys.h"
15
16/**
17 * disable hardware interrupts.
18 */
20
21/**
22 * enable hardware interrupts.
23 */
24void arch_enable_interrupts(void);
25
26/**
27 * disable interrupts and return the prior interrupt state.
28 * the return value is opaque and meant only to be handed back to
29 * arch_irq_restore(). use this instead of arch_disable_interrupts()
30 * when a critical section may nest inside another one.
31 * @return the interrupt state as it was before disabling.
32 */
34
35/**
36 * restore a previously saved interrupt state.
37 * re-enables interrupts only if they were enabled when the matching
38 * arch_irq_save() ran, so nested critical sections stay correct.
39 * @param flags the value returned by arch_irq_save().
40 */
42
43/**
44 * check whether hardware interrupts are currently enabled.
45 * meant for assertions such as "must be called with interrupts masked".
46 * @return true if interrupts are enabled.
47 */
48bool arch_irqs_enabled(void);
49
50/**
51 * halt the cpu until the next interrupt arrives.
52 * interrupts must be enabled before calling this.
53 */
54void arch_halt(void);
55
56/**
57 * enable interrupts and halt until the next interrupt, as one step: an
58 * interrupt that comes after the last check of the caller still ends the
59 * halt (Linux's safe_halt). call it with interrupts disabled; they are
60 * enabled when it returns.
61 */
62void arch_safe_halt(void);
63
64/**
65 * disable interrupts and halt permanently.
66 * used for fatal error paths where execution must stop.
67 */
68void arch_halt_forever(void) __attribute__((noreturn));
69
70/**
71 * a fast counter that goes up with each CPU cycle (the TSC on x86). it is
72 * not a clock: the random pool uses its low bits.
73 */
75
76/**
77 * a random word from the CPU's own generator (RDSEED, else RDRAND).
78 * @return false if the CPU has none, or it gave no value after some tries.
79 */
80bool arch_hw_random(uintptr_t *out);
81
82/**
83 * read the current stack pointer.
84 */
86
87/**
88 * walk a chain of saved frame pointers and collect return addresses.
89 * stops at a null or misaligned frame, an unmapped frame, a frame that does
90 * not move up the stack, or after max entries, so it is safe to call on a
91 * corrupt stack from a panic.
92 * @param frame the frame pointer to start from (e.g. __builtin_frame_address(0)).
93 * @param pcs receives the return addresses, innermost first.
94 * @param max capacity of pcs.
95 * @return the number of addresses stored.
96 */
97int arch_stack_walk(uintptr_t frame, uintptr_t *pcs, int max);
98
99/**
100 * an address space handle. opaque to portable code: it identifies a page
101 * table root but says nothing about the underlying paging structure, so
102 * the same kernel code works whether the arch uses 2-level, 4-level, or
103 * some other table layout.
104 */
106
107/**
108 * sentinel handle meaning "no address space".
109 */
110#define ADDR_SPACE_NONE ((addr_space_t)0)
111
112/* page protection flags for arch_map_page(), architecture-neutral. */
113#define MMU_FLAG_PRESENT (1u << 0) // the mapping is valid
114#define MMU_FLAG_WRITE (1u << 1) // writable
115#define MMU_FLAG_USER (1u << 2) // reachable from ring 3 / user mode
116#define MMU_FLAG_SHARED (1u << 3) // shared memory (MAP_SHARED): fork keeps it writable, no copy
117
118/* common combination: a writable page owned by a user process. */
119#define MMU_USER_RW (MMU_FLAG_PRESENT | MMU_FLAG_WRITE | MMU_FLAG_USER)
120
121/**
122 * return the handle for the kernel's own address space.
123 * useful when there is no current process to borrow one from.
124 */
126
127/**
128 * create a new address space that shares the kernel mappings.
129 * user regions start empty; map them in with arch_map_page().
130 * @return the new handle, or ADDR_SPACE_NONE on failure.
131 */
133
134/**
135 * copy the user half of an address space into a new, empty one (fork).
136 * writable RAM pages become copy-on-write in both: read-only, with the
137 * frame shared until one of them writes (mm_fault_in copies it then).
138 * @param dst the new address space (from arch_create_address_space()).
139 * @param src the address space to copy.
140 * @return true on success. on failure dst holds a partial copy; destroy it.
141 */
143
144/**
145 * tear down an address space created with arch_create_address_space(),
146 * freeing its user page tables and mapped frames. the kernel address
147 * space and ADDR_SPACE_NONE are ignored.
148 * @param as the address space to destroy.
149 */
151
152/**
153 * count the pages of RAM mapped in the user half of an address space (a
154 * process's resident memory). device mappings are not counted.
155 * @param as the address space.
156 * @return the number of pages.
157 */
159
160/**
161 * set aside a kernel-only virtual range whose mappings every address space
162 * shares. call during boot, before the first arch_create_address_space().
163 * @param start first virtual address of the range.
164 * @param end first virtual address past the range.
165 */
167
168/**
169 * map one kernel-only, writable page inside a reserved kernel range. the
170 * mapping is visible in every address space at once.
171 * @param virt page-aligned virtual address inside a reserved range.
172 * @param phys page-aligned physical frame.
173 */
175
176/**
177 * like arch_map_kernel_page(), but the page is not cached: for device
178 * registers, where each read and write must go to the device.
179 * @param virt page-aligned virtual address inside a reserved range.
180 * @param phys page-aligned physical address of the device memory.
181 */
183
184/**
185 * like arch_map_kernel_page(), but write-combining if the CPU can do it
186 * (uncached otherwise): for a framebuffer.
187 * @param virt page-aligned virtual address inside a reserved range.
188 * @param phys page-aligned physical address of the memory.
189 */
190void arch_map_wc_page(uintptr_t virt, phys_addr_t phys);
191
192/**
193 * remove a page mapped with arch_map_kernel_page(). the frame is not freed.
194 * @param virt page-aligned virtual address.
195 */
197
198/**
199 * check that user code in an address space could access a whole range:
200 * every page present and user-accessible, and writable if write is set.
201 * @param as the address space to check.
202 * @param addr first byte of the range.
203 * @param len length in bytes.
204 * @param write true if the range will be written.
205 * @return true if every page allows the access.
206 */
207bool arch_user_access_ok(addr_space_t as, uintptr_t addr, size_t len, bool write);
208
209/**
210 * map one page into a given address space.
211 * @param as the target address space.
212 * @param virt page-aligned virtual address.
213 * @param phys page-aligned physical frame.
214 * @param flags MMU_FLAG_* bits describing the mapping.
215 * @return 0 on success, -1 on failure.
216 */
218
219/**
220 * remove a user page mapping (and its TLB entry). the frame is not freed.
221 * @param as the address space.
222 * @param virt page-aligned user address.
223 * @return the frame that was mapped there, or 0 if the page was not mapped.
224 */
226
227/**
228 * change the protection of a mapped user page, keeping its frame.
229 * @param as the address space.
230 * @param virt page-aligned user address.
231 * @param flags MMU_FLAG_* bits; without MMU_FLAG_USER the page stays mapped
232 * but user code can no longer touch it.
233 * @return true if the page was mapped and changed.
234 */
236
237/**
238 * translate a virtual address to its physical address within a given
239 * address space. lets the kernel reach a process's memory without
240 * switching to it (e.g. to stage argv or an ELF image before the swap).
241 * @param as the address space to walk.
242 * @param virt the virtual address to translate.
243 * @return the physical address, or 0 if the page is not mapped.
244 */
246
247/**
248 * switch the active address space.
249 * @param as the address space to make current.
250 */
252
253/** the most pages arch_tlb_flush_local() drops one at a time (Linux's
254 * tlb_single_page_flush_ceiling); a larger range flushes the whole TLB */
255#define ARCH_TLB_FLUSH_CEILING 33u
256
257/**
258 * drop the TLB entries of the core that runs the call for the pages in
259 * [start, end), or all its entries if the range has more than
260 * ARCH_TLB_FLUSH_CEILING pages. other cores are not changed (see
261 * kernel/memory/tlb.h).
262 * @param start the first address, page-aligned.
263 * @param end the address after the range.
264 */
266
267/**
268 * set the thread pointer user code reaches its thread-local storage
269 * through: the FS base on x86_64 (%fs:0), the base of the TLS segment in
270 * GS on i686 (%gs:0). the scheduler sets it for each thread it runs.
271 * @param base the address, or 0.
272 */
274
275/**
276 * drop to usermode (ring 3) and begin executing at entry with the given stack.
277 * this function never returns.
278 * @param entry userspace entry point address.
279 * @param user_esp userspace stack pointer.
280 */
281void arch_jump_to_usermode(uintptr_t entry, uintptr_t user_esp) __attribute__((noreturn));
282
283// bytes of saved FPU/SSE state per process (16-byte aligned)
284#define ARCH_FPU_STATE_SIZE 512
285
286/**
287 * turn on the FPU (and SSE) for user programs and make the clean state a
288 * new program starts with. call once at boot.
289 */
290void arch_fpu_init(void);
291
292/**
293 * turn on the FPU (and SSE) on the core that runs the call. arch_fpu_init()
294 * does it for the boot core; each other core calls this when it starts.
295 */
296void arch_fpu_init_core(void);
297
298/**
299 * save the CPU's FPU/SSE registers.
300 * @param area ARCH_FPU_STATE_SIZE bytes, 16-byte aligned.
301 */
302void arch_fpu_save(void *area);
303
304/**
305 * load the FPU/SSE registers.
306 * @param area state saved by arch_fpu_save (or arch_fpu_clean).
307 */
308void arch_fpu_restore(const void *area);
309
310/**
311 * fill an area with the state of a fresh program.
312 */
313void arch_fpu_clean(void *area);
314
315/**
316 * make a state that came from user memory safe to load (a signal frame).
317 */
318void arch_fpu_sanitize(void *area);
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).
Definition arch_impl.c:165
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 fra...
Definition arch_impl.c:227
void arch_disable_interrupts(void)
arch-neutral cpu primitives.
Definition arch_impl.c:10
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.
Definition arch_impl.c:255
void arch_safe_halt(void)
enable interrupts and halt until the next interrupt, as one step: an interrupt that comes after the l...
Definition arch_impl.c:62
void arch_fpu_init_core(void)
turn on the FPU (and SSE) on the core that runs the call.
Definition fpu.c:44
void arch_fpu_sanitize(void *area)
make a state that came from user memory safe to load (a signal frame).
Definition fpu.c:109
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 wr...
Definition arch_impl.c:222
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),...
Definition arch_impl.c:139
void arch_fpu_save(void *area)
save the CPU's FPU/SSE registers.
Definition fpu.c:79
uintptr_t addr_space_t
an address space handle.
Definition arch.h:105
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...
Definition arch_impl.c:175
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.
Definition arch_impl.c:250
bool arch_hw_random(uintptr_t *out)
a random word from the CPU's own generator (RDSEED, else RDRAND).
Definition arch_impl.c:379
phys_addr_t arch_unmap_page(addr_space_t as, uintptr_t virt)
remove a user page mapping (and its TLB entry).
Definition arch_impl.c:245
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).
Definition arch_impl.c:170
void arch_halt(void)
halt the cpu until the next interrupt arrives.
Definition arch_impl.c:57
void arch_irq_restore(uint32_t flags)
restore a previously saved interrupt state.
Definition arch_impl.c:36
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-acce...
Definition arch_impl.c:240
void arch_fpu_clean(void *area)
fill an area with the state of a fresh program.
Definition fpu.c:104
void arch_enable_interrupts(void)
enable hardware interrupts.
Definition arch_impl.c:15
void arch_reserve_kernel_range(uintptr_t start, uintptr_t end)
set aside a kernel-only virtual range whose mappings every address space shares.
Definition arch_impl.c:212
uintptr_t arch_get_stack_pointer(void)
read the current stack pointer.
Definition arch_impl.c:77
void arch_halt_forever(void) __attribute__((noreturn))
disable interrupts and halt permanently.
Definition arch_impl.c:69
addr_space_t arch_kernel_address_space(void)
return the handle for the kernel's own address space.
Definition arch_impl.c:154
int arch_stack_walk(uintptr_t frame, uintptr_t *pcs, int max)
walk a chain of saved frame pointers and collect return addresses.
Definition arch_impl.c:99
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.
Definition arch_impl.c:203
void arch_unmap_kernel_page(uintptr_t virt)
remove a page mapped with arch_map_kernel_page().
Definition arch_impl.c:235
uint32_t arch_irq_save(void)
disable interrupts and return the prior interrupt state.
Definition arch_impl.c:20
void arch_switch_address_space(addr_space_t as)
switch the active address space.
Definition arch_impl.c:133
uint64_t arch_cycle_count(void)
a fast counter that goes up with each CPU cycle (the TSC on x86).
Definition arch_impl.c:338
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:...
Definition arch_impl.c:274
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.
Definition arch_impl.c:285
void arch_fpu_init(void)
turn on the FPU (and SSE) for user programs and make the clean state a new program starts with.
Definition fpu.c:69
bool arch_irqs_enabled(void)
check whether hardware interrupts are currently enabled.
Definition arch_impl.c:48
void arch_fpu_restore(const void *area)
load the FPU/SSE registers.
Definition fpu.c:92
void arch_map_kernel_page(uintptr_t virt, phys_addr_t phys)
map one kernel-only, writable page inside a reserved kernel range.
Definition arch_impl.c:217
addr_space_t arch_create_address_space(void)
create a new address space that shares the kernel mappings.
Definition arch_impl.c:159
uint32_t base
Definition gdt.c:1
uint8_t flags
Definition idt.c:3
unsigned int uint32_t
Definition fb.h:17
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:19
uintptr_t phys_addr_t
physical addresses and the conversion between physical and virtual.
Definition phys.h:18
ELF32 file header.
Definition elf.h:51
ssize_t write(int fd, const void *buf, size_t count)
write bytes to a file descriptor.