The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
mman.h File Reference

memory-mapping interface (userspace). More...

#include <sys/types.h>

Go to the source code of this file.

Macros

#define PROT_NONE   0x0
#define PROT_READ   0x1
#define PROT_WRITE   0x2
#define PROT_EXEC   0x4
#define MAP_SHARED   0x01
#define MAP_PRIVATE   0x02
#define MAP_ANONYMOUS   0x04
#define MAP_FIXED   0x10
#define MAP_FAILED   ((void *)-1)

Functions

void * mmap (void *addr, size_t length, int prot, int flags, int fd, off_t offset)
 map memory into the calling process's address space.
int munmap (void *addr, size_t length)
 remove the mappings in [addr, addr + length).
int mprotect (void *addr, size_t length, int prot)
 change the protection of [addr, addr + length), which must be mapped.
int shm_create (size_t size, int fd_flags)
 make a shared memory object (a ginnOS call, like Linux's memfd_create): size bytes of zeroed memory, rounded up to whole pages.

Detailed Description

memory-mapping interface (userspace).

the prot/flag values here are ABI and must match the kernel side in src/kernel/memory/mman.h exactly.

Macro Definition Documentation

◆ PROT_NONE

#define PROT_NONE   0x0

◆ PROT_READ

#define PROT_READ   0x1

◆ PROT_WRITE

#define PROT_WRITE   0x2

◆ PROT_EXEC

#define PROT_EXEC   0x4

◆ MAP_SHARED

#define MAP_SHARED   0x01

◆ MAP_PRIVATE

#define MAP_PRIVATE   0x02

◆ MAP_ANONYMOUS

#define MAP_ANONYMOUS   0x04

◆ MAP_FIXED

#define MAP_FIXED   0x10

◆ MAP_FAILED

#define MAP_FAILED   ((void *)-1)

Function Documentation

◆ mmap()

void * mmap ( void * addr,
size_t length,
int prot,
int flags,
int fd,
off_t offset )

map memory into the calling process's address space.

two forms are supported:

  • anonymous (flags has MAP_ANONYMOUS, fd < 0): zero-filled pages.
  • device-backed (fd is an open device node such as "/dev/fb0"): maps the device's memory (e.g. the linear framebuffer). with MAP_FIXED the mapping goes exactly at addr (page-aligned), replacing anything mapped there; otherwise addr is a hint the kernel uses if that range is free, else it picks a free range in its mmap region.
Parameters
addraddress (MAP_FIXED) or hint (may be NULL).
lengthnumber of bytes to map (rounded up to a page).
protprotection bits (PROT_*).
flagsmapping flags (MAP_*).
fddevice fd for a device-backed mapping, or -1 for anonymous.
offsetbyte offset into the file or device (a multiple of the page size, else EINVAL).
Returns
the mapped address on success, or MAP_FAILED on error (errno set).

◆ munmap()

int munmap ( void * addr,
size_t length )

remove the mappings in [addr, addr + length).

Parameters
addrpage-aligned start.
lengthnumber of bytes (rounded up to a page).
Returns
0 on success, -1 on error (errno set).

◆ mprotect()

int mprotect ( void * addr,
size_t length,
int prot )

change the protection of [addr, addr + length), which must be mapped.

Parameters
addrpage-aligned start.
lengthnumber of bytes (rounded up to a page).
protthe new PROT_* bits.
Returns
0 on success, -1 on error (errno set; ENOMEM if part of the range is not mapped).

◆ shm_create()

int shm_create ( size_t size,
int fd_flags )

make a shared memory object (a ginnOS call, like Linux's memfd_create): size bytes of zeroed memory, rounded up to whole pages.

every mmap of the returned fd (MAP_SHARED), in this process or another one that has the fd (through fork, or sent over a socket), maps the same memory.

Parameters
sizethe size in bytes.
fd_flagsFD_CLOEXEC or 0.
Returns
the fd (open read-write), or -1 with errno set.