1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:07:35 +00:00

Kernel+UE: Add MAP_FIXED_NOREPLACE mmap() flag

This feature was introduced in version 4.17 of the Linux kernel, and
while it's not specified by POSIX, I think it will be a nice addition to
our system.

MAP_FIXED_NOREPLACE provides a less error-prone alternative to
MAP_FIXED: while regular fixed mappings would cause any intersecting
ranges to be unmapped, MAP_FIXED_NOREPLACE returns EEXIST instead. This
ensures that we don't corrupt our process's address space if something
is already at the requested address.

Note that the more portable way to do this is to use regular
MAP_ANONYMOUS, and check afterwards whether the returned address matches
what we wanted. This, however, has a large performance impact on
programs like Wine which try to reserve large portions of the address
space at once, as the non-matching addresses have to be unmapped
separately.
This commit is contained in:
Daniel Bertalan 2021-12-22 12:23:06 +01:00 committed by Andreas Kling
parent 4195a7ef4b
commit 77f9272aaf
6 changed files with 18 additions and 15 deletions

View file

@ -36,8 +36,8 @@ NonnullOwnPtr<MmapRegion> MmapRegion::create_anonymous(u32 base, u32 size, u32 p
NonnullOwnPtr<MmapRegion> MmapRegion::create_file_backed(u32 base, u32 size, u32 prot, int flags, int fd, off_t offset, String name)
{
// Since we put the memory to an arbitrary location, do not pass MAP_FIXED to the Kernel.
auto real_flags = flags & ~MAP_FIXED;
// Since we put the memory to an arbitrary location, do not pass MAP_FIXED and MAP_FIXED_NOREPLACE to the Kernel.
auto real_flags = flags & ~(MAP_FIXED | MAP_FIXED_NOREPLACE);
auto* data = (u8*)mmap_with_name(nullptr, size, prot, real_flags, fd, offset, name.is_empty() ? nullptr : String::formatted("(UE) {}", name).characters());
VERIFY(data != MAP_FAILED);
auto* shadow_data = (u8*)mmap_initialized(size, 1, "MmapRegion ShadowData");