diff --git a/lib/libc/include/sys/mman.h b/lib/libc/include/sys/mman.h index e4180d7..74e1a04 100644 --- a/lib/libc/include/sys/mman.h +++ b/lib/libc/include/sys/mman.h @@ -41,5 +41,6 @@ extern void *mmap( int flags, int fd, off_t offset); +extern int munmap(void *addr, size_t length); #endif diff --git a/lib/libc/io/unistd/munmap.c b/lib/libc/io/unistd/munmap.c new file mode 100644 index 0000000..27837d2 --- /dev/null +++ b/lib/libc/io/unistd/munmap.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include +#include +#include + +int munmap(void *addr, size_t length) +{ + kern_status_t status = KERN_OK; + kern_handle_t self = KERN_HANDLE_INVALID, + address_space = KERN_HANDLE_INVALID; + + status = task_self(&self); + if (status != KERN_OK) { + return __set_errno(EPERM); + } + + status = task_get_address_space(self, &address_space); + kern_handle_close(self); + if (status != KERN_OK) { + return __set_errno(EPERM); + } + + status = address_space_unmap(address_space, (virt_addr_t)addr, length); + + kern_handle_close(address_space); + if (status != KERN_OK) { + return __set_errno(__errno_from_kern_status(status)); + } + + return __set_errno(SUCCESS); +}