The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
vma.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * virtual memory areas: what a process has mapped in its user half.
5 *
6 * each area is a page-aligned [start, end) range with protection bits and a
7 * kind (ELF segment, heap, stack, anonymous or device mmap). a process keeps
8 * its areas in a vma_table_t, sorted by start and never overlapping. the
9 * table is what the page-fault handler consults and what mmap/munmap work
10 * on.
11 *
12 * locking: the threads of a process share the table. the memory syscalls
13 * take the user space's mutex (user_space.h), so one changes it at a time.
14 * the table's spinlock (the page lock, as mm->page_table_lock in Linux)
15 * guards the table and the page tables of the space: each change holds it,
16 * so a page fault in another thread (mm_fault_in, which holds it too)
17 * never sees half of one. the functions that change the table take it;
18 * vma_find and the other readers do not, so their caller holds it, or the
19 * user space's mutex, or the space is not in use yet.
20 */
21
22#include "common/stdint.h"
23#include "common/error.h"
25
26// most areas one process can have (a thread's stack takes two: the
27// stack and its guard page)
28#define VMA_MAX 256
29
30/**
31 * what backs an area.
32 */
33typedef enum
34{
35 VMA_ELF, // a PT_LOAD segment of the program
36 VMA_HEAP, // the sbrk heap
37 VMA_STACK, // the user stack
38 VMA_ANON, // anonymous mmap (zero-filled)
39 VMA_DEVICE, // device mmap (e.g. /dev/fb0)
40 VMA_SHARED, // shared memory (shm_create), mapped up front, never copied on fork
42
43/**
44 * one area of user memory.
45 */
46typedef struct
47{
48 uintptr_t start; // first byte (page-aligned)
49 uintptr_t end; // first byte past the area (page-aligned)
50 uint32_t prot; // PROT_READ / PROT_WRITE / PROT_EXEC
51 vma_kind_t kind; // what backs it
52} vma_t;
53
54/**
55 * a process's areas, sorted by start.
56 */
57typedef struct
58{
61 spinlock_t lock; // the page lock of the space (see above)
63
64/**
65 * empty a table.
66 * @param table the table.
67 */
68void vma_table_init(vma_table_t *table);
69
70/**
71 * add an area.
72 * @param table the table.
73 * @param start first byte (page-aligned).
74 * @param end first byte past the area (page-aligned, > start).
75 * @param prot PROT_* bits.
76 * @param kind what backs it.
77 * @return KERR_OK, KERR_INVAL for a bad range, KERR_EXIST if it overlaps an
78 * existing area, KERR_NOSPC if the table is full.
79 */
81 vma_kind_t kind);
82
83/**
84 * find the area containing an address.
85 * @param table the table.
86 * @param addr the address.
87 * @return the area, or NULL if addr is not mapped by any.
88 */
89vma_t *vma_find(vma_table_t *table, uintptr_t addr);
90
91/**
92 * find the first area of a kind.
93 * @param table the table.
94 * @param kind the kind.
95 * @return the area, or NULL if there is none.
96 */
98
99/**
100 * check that no area overlaps a range.
101 * @param table the table.
102 * @param start first byte of the range.
103 * @param end first byte past the range.
104 * @return true if [start, end) is entirely unmapped.
105 */
106bool vma_range_free(const vma_table_t *table, uintptr_t start, uintptr_t end);
107
108/**
109 * find the lowest free gap of a given size inside [low, high).
110 * @param table the table.
111 * @param length bytes needed (page-aligned).
112 * @param low lowest acceptable start (page-aligned).
113 * @param high first byte past the acceptable range.
114 * @return the start of the gap, or 0 if none is large enough.
115 */
116uintptr_t vma_find_free(const vma_table_t *table, size_t length, uintptr_t low, uintptr_t high);
117
118/**
119 * unmap [start, end) from the table: areas inside it are removed, areas
120 * crossing its edges are trimmed, and an area containing it is split in two.
121 * @param table the table.
122 * @param start first byte (page-aligned).
123 * @param end first byte past the range (page-aligned).
124 * @return KERR_OK (also when nothing was mapped there), KERR_INVAL for a bad
125 * range, KERR_NOSPC if a split needs a slot and the table is full.
126 */
128
129/**
130 * vma_remove() for a caller that holds the table's lock.
131 */
133
134/**
135 * vma_protect() for a caller that holds the table's lock.
136 */
138
139/**
140 * vma_insert() for a caller that holds the table's lock.
141 */
143 vma_kind_t kind);
144
145/**
146 * copy the areas of one table into another (fork). the source's lock is
147 * held by the caller; the destination is not in use yet.
148 * @param dst the new table.
149 * @param src the table to copy.
150 */
151void vma_table_copy(vma_table_t *dst, const vma_table_t *src);
152
153/**
154 * change the protection of [start, end). every byte of the range must be in
155 * some area; areas crossing its edges are split so only the range changes.
156 * @param table the table.
157 * @param start first byte (page-aligned).
158 * @param end first byte past the range (page-aligned).
159 * @param prot the new PROT_* bits.
160 * @return KERR_OK, KERR_INVAL for a bad range, KERR_NOENT if part of the
161 * range is unmapped, KERR_NOSPC if the splits need more slots than
162 * are free. on failure nothing changes.
163 */
164kerr_t vma_protect(vma_table_t *table, uintptr_t start, uintptr_t end, uint32_t prot);
kerr_t
unified kernel error codes.
Definition error.h:14
unsigned int uint32_t
Definition fb.h:17
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:19
kernel locking rules:
Definition spinlock.h:67
one area of user memory.
Definition vma.h:47
vma_kind_t kind
Definition vma.h:51
uintptr_t start
Definition vma.h:48
uint32_t prot
Definition vma.h:50
uintptr_t end
Definition vma.h:49
a process's areas, sorted by start.
Definition vma.h:58
spinlock_t lock
Definition vma.h:61
uint32_t count
Definition vma.h:60
vma_t areas[VMA_MAX]
Definition vma.h:59
uintptr_t vma_find_free(const vma_table_t *table, size_t length, uintptr_t low, uintptr_t high)
find the lowest free gap of a given size inside [low, high).
Definition vma.c:133
vma_t * vma_find_kind(vma_table_t *table, vma_kind_t kind)
find the first area of a kind.
Definition vma.c:115
void vma_table_init(vma_table_t *table)
empty a table.
Definition vma.c:50
kerr_t vma_remove_locked(vma_table_t *table, uintptr_t start, uintptr_t end)
vma_remove() for a caller that holds the table's lock.
Definition vma.c:220
kerr_t vma_insert_locked(vma_table_t *table, uintptr_t start, uintptr_t end, uint32_t prot, vma_kind_t kind)
vma_insert() for a caller that holds the table's lock.
Definition vma.c:99
void vma_table_copy(vma_table_t *dst, const vma_table_t *src)
copy the areas of one table into another (fork).
Definition vma.c:56
vma_t * vma_find(vma_table_t *table, uintptr_t addr)
find the area containing an address.
Definition vma.c:105
kerr_t vma_protect(vma_table_t *table, uintptr_t start, uintptr_t end, uint32_t prot)
change the protection of [start, end).
Definition vma.c:283
kerr_t vma_remove(vma_table_t *table, uintptr_t start, uintptr_t end)
unmap [start, end) from the table: areas inside it are removed, areas crossing its edges are trimmed,...
Definition vma.c:212
kerr_t vma_protect_locked(vma_table_t *table, uintptr_t start, uintptr_t end, uint32_t prot)
vma_protect() for a caller that holds the table's lock.
Definition vma.c:291
kerr_t vma_insert(vma_table_t *table, uintptr_t start, uintptr_t end, uint32_t prot, vma_kind_t kind)
add an area.
Definition vma.c:90
bool vma_range_free(const vma_table_t *table, uintptr_t start, uintptr_t end)
check that no area overlaps a range.
Definition vma.c:127
vma_kind_t
what backs an area.
Definition vma.h:34
@ VMA_ANON
Definition vma.h:38
@ VMA_STACK
Definition vma.h:37
@ VMA_HEAP
Definition vma.h:36
@ VMA_ELF
Definition vma.h:35
@ VMA_DEVICE
Definition vma.h:39
@ VMA_SHARED
Definition vma.h:40
#define VMA_MAX
virtual memory areas: what a process has mapped in its user half.
Definition vma.h:28