The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
vma.h File Reference
#include "common/stdint.h"
#include "common/error.h"
#include "kernel/sync/spinlock.h"

Go to the source code of this file.

Data Structures

struct  vma_t
 one area of user memory. More...
struct  vma_table_t
 a process's areas, sorted by start. More...

Macros

#define VMA_MAX   256
 virtual memory areas: what a process has mapped in its user half.

Enumerations

enum  vma_kind_t {
  VMA_ELF , VMA_HEAP , VMA_STACK , VMA_ANON ,
  VMA_DEVICE , VMA_SHARED
}
 what backs an area. More...

Functions

void vma_table_init (vma_table_t *table)
 empty a table.
kerr_t vma_insert (vma_table_t *table, uintptr_t start, uintptr_t end, uint32_t prot, vma_kind_t kind)
 add an area.
vma_t * vma_find (vma_table_t *table, uintptr_t addr)
 find the area containing an address.
vma_t * vma_find_kind (vma_table_t *table, vma_kind_t kind)
 find the first area of a kind.
bool vma_range_free (const vma_table_t *table, uintptr_t start, uintptr_t end)
 check that no area overlaps a range.
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).
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, and an area containing it is split in two.
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.
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.
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.
void vma_table_copy (vma_table_t *dst, const vma_table_t *src)
 copy the areas of one table into another (fork).
kerr_t vma_protect (vma_table_t *table, uintptr_t start, uintptr_t end, uint32_t prot)
 change the protection of [start, end).

Macro Definition Documentation

◆ VMA_MAX

#define VMA_MAX   256

virtual memory areas: what a process has mapped in its user half.

each area is a page-aligned [start, end) range with protection bits and a kind (ELF segment, heap, stack, anonymous or device mmap). a process keeps its areas in a vma_table_t, sorted by start and never overlapping. the table is what the page-fault handler consults and what mmap/munmap work on.

locking: the threads of a process share the table. the memory syscalls take the user space's mutex (user_space.h), so one changes it at a time. the table's spinlock (the page lock, as mm->page_table_lock in Linux) guards the table and the page tables of the space: each change holds it, so a page fault in another thread (mm_fault_in, which holds it too) never sees half of one. the functions that change the table take it; vma_find and the other readers do not, so their caller holds it, or the user space's mutex, or the space is not in use yet.

Enumeration Type Documentation

◆ vma_kind_t

enum vma_kind_t

what backs an area.

Enumerator
VMA_ELF 
VMA_HEAP 
VMA_STACK 
VMA_ANON 
VMA_DEVICE 
VMA_SHARED 

Function Documentation

◆ vma_table_init()

void vma_table_init ( vma_table_t * table)

empty a table.

Parameters
tablethe table.

◆ vma_insert()

kerr_t vma_insert ( vma_table_t * table,
uintptr_t start,
uintptr_t end,
uint32_t prot,
vma_kind_t kind )

add an area.

Parameters
tablethe table.
startfirst byte (page-aligned).
endfirst byte past the area (page-aligned, > start).
protPROT_* bits.
kindwhat backs it.
Returns
KERR_OK, KERR_INVAL for a bad range, KERR_EXIST if it overlaps an existing area, KERR_NOSPC if the table is full.

◆ vma_find()

vma_t * vma_find ( vma_table_t * table,
uintptr_t addr )

find the area containing an address.

Parameters
tablethe table.
addrthe address.
Returns
the area, or NULL if addr is not mapped by any.

◆ vma_find_kind()

vma_t * vma_find_kind ( vma_table_t * table,
vma_kind_t kind )

find the first area of a kind.

Parameters
tablethe table.
kindthe kind.
Returns
the area, or NULL if there is none.

◆ vma_range_free()

bool vma_range_free ( const vma_table_t * table,
uintptr_t start,
uintptr_t end )

check that no area overlaps a range.

Parameters
tablethe table.
startfirst byte of the range.
endfirst byte past the range.
Returns
true if [start, end) is entirely unmapped.

◆ vma_find_free()

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).

Parameters
tablethe table.
lengthbytes needed (page-aligned).
lowlowest acceptable start (page-aligned).
highfirst byte past the acceptable range.
Returns
the start of the gap, or 0 if none is large enough.

◆ vma_remove()

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, and an area containing it is split in two.

Parameters
tablethe table.
startfirst byte (page-aligned).
endfirst byte past the range (page-aligned).
Returns
KERR_OK (also when nothing was mapped there), KERR_INVAL for a bad range, KERR_NOSPC if a split needs a slot and the table is full.

◆ vma_remove_locked()

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.

◆ vma_protect_locked()

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.

◆ vma_insert_locked()

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.

◆ vma_table_copy()

void vma_table_copy ( vma_table_t * dst,
const vma_table_t * src )

copy the areas of one table into another (fork).

the source's lock is held by the caller; the destination is not in use yet.

Parameters
dstthe new table.
srcthe table to copy.

◆ vma_protect()

kerr_t vma_protect ( vma_table_t * table,
uintptr_t start,
uintptr_t end,
uint32_t prot )

change the protection of [start, end).

every byte of the range must be in some area; areas crossing its edges are split so only the range changes.

Parameters
tablethe table.
startfirst byte (page-aligned).
endfirst byte past the range (page-aligned).
protthe new PROT_* bits.
Returns
KERR_OK, KERR_INVAL for a bad range, KERR_NOENT if part of the range is unmapped, KERR_NOSPC if the splits need more slots than are free. on failure nothing changes.