diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index af6a8f29116..5bb934088f9 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -151,12 +151,15 @@ void* Process::sys$mmap(const Syscall::SC_mmap_params* params) { if (!validate_read(params, sizeof(Syscall::SC_mmap_params))) return (void*)-EFAULT; + if (params->name && !validate_read_str(params->name)) + return (void*)-EFAULT; void* addr = (void*)params->addr; size_t size = params->size; int prot = params->prot; int flags = params->flags; int fd = params->fd; off_t offset = params->offset; + const char* name = params->name; if (size == 0) return (void*)-EINVAL; if ((dword)addr & ~PAGE_MASK) @@ -167,6 +170,8 @@ void* Process::sys$mmap(const Syscall::SC_mmap_params* params) return (void*)-ENOMEM; if (flags & MAP_SHARED) region->set_shared(true); + if (name) + region->set_name(name); return region->laddr().as_ptr(); } if (offset & ~PAGE_MASK) @@ -180,6 +185,8 @@ void* Process::sys$mmap(const Syscall::SC_mmap_params* params) auto region = region_or_error.value(); if (flags & MAP_SHARED) region->set_shared(true); + if (name) + region->set_name(name); return region->laddr().as_ptr(); } diff --git a/Kernel/Syscall.h b/Kernel/Syscall.h index d2c884bade7..5f66bb1df3a 100644 --- a/Kernel/Syscall.h +++ b/Kernel/Syscall.h @@ -134,6 +134,7 @@ struct SC_mmap_params { int32_t flags; int32_t fd; int32_t offset; // FIXME: 64-bit off_t? + const char* name { nullptr }; }; struct SC_select_params { diff --git a/LibC/grp.cpp b/LibC/grp.cpp index 0e9ebdcecda..744189bb80a 100644 --- a/LibC/grp.cpp +++ b/LibC/grp.cpp @@ -31,8 +31,7 @@ void setgrent() perror("open /etc/group"); } assert(__grdb_stream); - __grdb_entry = (struct group_with_strings*)mmap(nullptr, getpagesize(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); - set_mmap_name(__grdb_entry, getpagesize(), "setgrent"); + __grdb_entry = (struct group_with_strings*)mmap_with_name(nullptr, getpagesize(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0, "setgrent"); } } diff --git a/LibC/malloc.cpp b/LibC/malloc.cpp index 4ebd7cbefbb..9353f4c2894 100644 --- a/LibC/malloc.cpp +++ b/LibC/malloc.cpp @@ -123,9 +123,9 @@ size_t malloc_good_size(size_t size) return PAGE_ROUND_UP(size); } -static void* os_alloc(size_t size) +static void* os_alloc(size_t size, const char* name) { - return mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); + return mmap_with_name(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0, name); } static void os_free(void* ptr, size_t size) @@ -155,10 +155,9 @@ void* malloc(size_t size) } } #endif - auto* block = (BigAllocationBlock*)os_alloc(real_size); char buffer[64]; snprintf(buffer, sizeof(buffer), "malloc: BigAllocationBlock(%u)", real_size); - set_mmap_name(block, PAGE_SIZE, buffer); + auto* block = (BigAllocationBlock*)os_alloc(real_size, buffer); new (block) BigAllocationBlock(real_size); return &block->m_slot[0]; } @@ -171,10 +170,9 @@ void* malloc(size_t size) } if (!block) { - block = (ChunkedBlock*)os_alloc(PAGE_SIZE); char buffer[64]; snprintf(buffer, sizeof(buffer), "malloc: ChunkedBlock(%u)", good_size); - set_mmap_name(block, PAGE_SIZE, buffer); + block = (ChunkedBlock*)os_alloc(PAGE_SIZE, buffer); new (block) ChunkedBlock(good_size); allocator->usable_blocks.append(block); ++allocator->block_count; diff --git a/LibC/mman.cpp b/LibC/mman.cpp index 174643712ba..bd87e5989c3 100644 --- a/LibC/mman.cpp +++ b/LibC/mman.cpp @@ -7,7 +7,18 @@ extern "C" { void* mmap(void* addr, size_t size, int prot, int flags, int fd, off_t offset) { - Syscall::SC_mmap_params params { (dword)addr, size, prot, flags, fd, offset }; + Syscall::SC_mmap_params params { (dword)addr, size, prot, flags, fd, offset, nullptr }; + int rc = syscall(SC_mmap, ¶ms); + if (rc < 0 && -rc < EMAXERRNO) { + errno = -rc; + return (void*)-1; + } + return (void*)rc; +} + +void* mmap_with_name(void* addr, size_t size, int prot, int flags, int fd, off_t offset, const char* name) +{ + Syscall::SC_mmap_params params { (dword)addr, size, prot, flags, fd, offset, name }; int rc = syscall(SC_mmap, ¶ms); if (rc < 0 && -rc < EMAXERRNO) { errno = -rc; diff --git a/LibC/mman.h b/LibC/mman.h index 0defe16763b..73ea753fd92 100644 --- a/LibC/mman.h +++ b/LibC/mman.h @@ -19,6 +19,7 @@ __BEGIN_DECLS void* mmap(void* addr, size_t, int prot, int flags, int fd, off_t); +void* mmap_with_name(void* addr, size_t, int prot, int flags, int fd, off_t, const char* name); int munmap(void*, size_t); int set_mmap_name(void*, size_t, const char*); int shm_open(const char* name, int flags, mode_t); diff --git a/LibC/pwd.cpp b/LibC/pwd.cpp index d0280861e1d..9bed1d1bd02 100644 --- a/LibC/pwd.cpp +++ b/LibC/pwd.cpp @@ -32,8 +32,7 @@ void setpwent() perror("open /etc/passwd"); } assert(__pwdb_stream); - __pwdb_entry = (struct passwd_with_strings*)mmap(nullptr, getpagesize(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); - set_mmap_name(__pwdb_entry, getpagesize(), "setpwent"); + __pwdb_entry = (struct passwd_with_strings*)mmap_with_name(nullptr, getpagesize(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0, "setpwent"); } } diff --git a/SharedGraphics/GraphicsBitmap.cpp b/SharedGraphics/GraphicsBitmap.cpp index e8ec1ea8be4..027dbbdaa87 100644 --- a/SharedGraphics/GraphicsBitmap.cpp +++ b/SharedGraphics/GraphicsBitmap.cpp @@ -19,10 +19,9 @@ GraphicsBitmap::GraphicsBitmap(Format format, const Size& size) { if (format == Format::Indexed8) m_palette = new RGBA32[256]; - m_data = (RGBA32*)mmap(nullptr, size_in_bytes(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); + m_data = (RGBA32*)mmap_with_name(nullptr, size_in_bytes(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0, String::format("GraphicsBitmap [%dx%d]", width(), height()).characters()); ASSERT(m_data && m_data != (void*)-1); m_needs_munmap = true; - set_mmap_name(String::format("GraphicsBitmap [%dx%d]", width(), height()).characters()); } Retained GraphicsBitmap::create_wrapper(Format format, const Size& size, RGBA32* data)