⚠️ VeridianOS Kernel Documentation - This is low-level kernel code. All functions are unsafe unless explicitly marked otherwise. no_std

sys_mmap

Function sys_mmap 

Source
pub fn sys_mmap(_addr: usize, _len: usize, _prot: u32) -> SyscallResult<usize>
Expand description

Map memory into the calling process’s virtual address space.

Allocates a contiguous region of virtual memory and optionally backs it with physical frames. The mapping is anonymous (zero-initialized, not file-backed). Both the requested address and length are rounded to page boundaries (4 KiB on all supported architectures).

The protection flags control access permissions on the mapped region. The kernel enforces W^X (write XOR execute) policy: a region cannot be both writable and executable simultaneously. To load executable code, first map as writable, write the code, then use a separate call to change protection to read-execute.

§Arguments

  • addr - Preferred virtual address for the mapping. Pass 0 to let the kernel choose a suitable address (recommended). If non-zero, the address must be page-aligned; the kernel may adjust it or return an error if the region is already in use.
  • len - Number of bytes to map. Will be rounded up to the nearest page boundary. Must be greater than zero.
  • prot - Protection flags as a bitmask:
    • PROT_READ (0x1) - Pages may be read.
    • PROT_WRITE (0x2) - Pages may be written.
    • PROT_EXEC (0x4) - Pages may be executed.
    • 0 (PROT_NONE) - Pages cannot be accessed (guard pages).

§Returns

  • Ok(base_addr) - The starting virtual address of the mapped region. When addr was 0, this is the kernel-chosen address.

§Errors

§Examples

use veridian_kernel::pkg::sdk::syscall_api::sys_mmap;

// Allocate 4 KiB of read-write memory at a kernel-chosen address
let prot_rw = 0x1 | 0x2; // PROT_READ | PROT_WRITE
let addr = sys_mmap(0, 4096, prot_rw).expect("mmap failed");

// Allocate a 64 KiB guard-page-bounded region
let region = sys_mmap(0, 65536, prot_rw).expect("mmap failed");