SYS_MMAP
Map a region of virtual memory into the process address space. Syscall Number: 13Parameters
Suggested virtual address for the mapping. If 0, the kernel chooses the address.
Length of the mapping in bytes (rounded up to page size)
Memory protection flags (currently unused; all mappings are writable)
Mapping flags (currently unused)
Returns
- On success: virtual address of the mapped region
- On error: -1 (invalid length, no address space, or allocation failure)
Behavior
- Length alignment: Rounds
lenup to the nearest page boundary (4096 bytes) - Address selection:
- If
addr == 0: Usesprocs[current_pid].mmap_nextand increments it - If
addr != 0: Uses the provided address (fixed mapping)
- If
- Page allocation: Allocates physical frames for each page in the range
- Mapping: Maps each page with
PAGE_WRITABLE | PAGE_USERflags
Memory Layout
User mmap region spans from
0x40000000 to 0x7FFF0000. Each successful mmap advances mmap_next by len bytes.Implementation
Example
Error Conditions
SYS_MUNMAP
Unmap a region of virtual memory and free associated physical pages. Syscall Number: 14Parameters
Virtual address of the region to unmap (must be page-aligned)
Length of the region in bytes (rounded up to page size)
Returns
- On success: 0
- On error: -1 (invalid address alignment or zero length)
Behavior
- Validation: Checks that
addris page-aligned andlen > 0 - Length alignment: Rounds
lenup to the nearest page boundary - Unmapping loop: For each page in the range:
- Translates virtual address to physical address
- Frees the physical frame via
phys_free() - Unmaps the page from the page table
Implementation
Example
Safety Notes
SYS_BRK
Change the location of the program break (end of the data segment). Syscall Number: 31Parameters
New break address. If 0, queries the current break without changing it.
Returns
- On success: new break address
- On error or query: current break address
Behavior
The data segment (heap) starts at
0x10000000 (USER_BRK_BASE). Each process has its own break tracked in proc_brk[pid].Query Mode (addr == 0)
Returns the current break without modification.Set Mode (addr > 0)
- Validation: Rejects addresses below
USER_BRK_BASE - Growing heap: If new break is higher than current:
- Allocates physical frames for new pages
- Maps them with
PAGE_WRITABLE | PAGE_USERflags - Updates
proc_brk[current_pid]
- Shrinking heap: Returns current break without unmapping (current implementation doesn’t shrink)
Implementation
Example - Heap Allocator
Memory Layout with brk
Differences from POSIX
Unlike POSIX brk(), Aurora OS does not currently support shrinking the heap. Setting the break below the current value returns the current break unchanged.
Use Cases
- User-space allocators:
malloc()implementations typically usebrk()for small allocations andmmap()for large ones - Garbage collectors: GC systems can query the heap size and expand it as needed
- Static allocators: Simple bump allocators for applications without complex memory management