From 1c6ece7b5584aa1f9580cab3c918f6acfbdf8d1b Mon Sep 17 00:00:00 2001 From: Max Wash Date: Tue, 21 Apr 2026 21:15:27 +0100 Subject: [PATCH] libc: implement private/shared mapping support --- lib/libc/io/unistd/mmap.c | 18 ++++++++++++++++++ lib/libc/malloc/heap.c | 1 + 2 files changed, 19 insertions(+) diff --git a/lib/libc/io/unistd/mmap.c b/lib/libc/io/unistd/mmap.c index 7845e17..43fa7d4 100644 --- a/lib/libc/io/unistd/mmap.c +++ b/lib/libc/io/unistd/mmap.c @@ -25,6 +25,21 @@ static vm_prot_t vm_prot_from_mmap_prot(int prot) return vm_prot; } +static vm_flags_t vm_flags_from_mmap_flags(int flags) +{ + vm_flags_t out = 0; + + if (flags & MAP_PRIVATE) { + out |= VM_PRIVATE; + } + + if (flags & (MAP_SHARED | MAP_SHARED_VALIDATE)) { + out |= VM_SHARED; + } + + return out; +} + static int get_vmo_anon( int fd, int prot, @@ -109,6 +124,7 @@ void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) kern_status_t status = KERN_OK; kern_handle_t self = KERN_HANDLE_INVALID, address_space = KERN_HANDLE_INVALID; + vm_flags_t vm_flags = vm_flags_from_mmap_flags(flags); status = task_self(&self); if (status != KERN_OK) { @@ -146,6 +162,7 @@ void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) vmo, offset, length, + vm_flags, vm_prot, &map_address); } else { @@ -155,6 +172,7 @@ void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) vmo, offset, length, + vm_flags, vm_prot, &map_address); } diff --git a/lib/libc/malloc/heap.c b/lib/libc/malloc/heap.c index fc3f712..a117e40 100644 --- a/lib/libc/malloc/heap.c +++ b/lib/libc/malloc/heap.c @@ -79,6 +79,7 @@ static kern_status_t expand_heap(heap_t *heap) vmo, 0, HEAP_EXPAND_INCREMENT, + VM_PRIVATE, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_USER, &base);