summaryrefslogtreecommitdiff
path: root/DevTools/UserspaceEmulator
diff options
context:
space:
mode:
authorItamar <itamar8910@gmail.com>2020-11-27 15:45:49 +0200
committerAndreas Kling <kling@serenityos.org>2020-12-14 23:05:53 +0100
commit28cda567c1fc93596fcb7167db9677f76eb0cf2b (patch)
treef3a0bce45ebc946aa2c43b70c27eecc07b3f489d /DevTools/UserspaceEmulator
parentd2262b8f6d62cc6658574df9c12bdf99d9c8fc26 (diff)
downloadserenity-28cda567c1fc93596fcb7167db9677f76eb0cf2b.zip
UserspaceEmulator: Support the name argument in mmap for file backed mappings
Diffstat (limited to 'DevTools/UserspaceEmulator')
-rw-r--r--DevTools/UserspaceEmulator/Emulator.cpp20
-rw-r--r--DevTools/UserspaceEmulator/MmapRegion.cpp9
-rw-r--r--DevTools/UserspaceEmulator/MmapRegion.h3
3 files changed, 27 insertions, 5 deletions
diff --git a/DevTools/UserspaceEmulator/Emulator.cpp b/DevTools/UserspaceEmulator/Emulator.cpp
index 9f3b6acff8..f42d73844f 100644
--- a/DevTools/UserspaceEmulator/Emulator.cpp
+++ b/DevTools/UserspaceEmulator/Emulator.cpp
@@ -874,11 +874,27 @@ u32 Emulator::virt$mmap(u32 params_addr)
u32 final_size = round_up_to_power_of_two(params.size, PAGE_SIZE);
u32 final_address = allocate_vm(final_size, params.alignment);
+ if (params.addr != 0) {
+ // NOTE: We currently do not support allocating VM at a requeted address in the emulator.
+ // The loader needs this functionality to load .data just after .text.
+ // Luckily, since the loader calls mmap for .data right after it calls mmap for .text,
+ // the emulator will allocate a chunk of memory that is just after what we allocated for .text
+ // becuase of the way we currently allocate VM.
+ ASSERT(params.addr == final_address);
+ }
if (params.flags & MAP_ANONYMOUS)
mmu().add_region(MmapRegion::create_anonymous(final_address, final_size, params.prot));
- else
- mmu().add_region(MmapRegion::create_file_backed(final_address, final_size, params.prot, params.flags, params.fd, params.offset));
+ else {
+ dbgln("chars: {:p}, len: {}", params.name.characters, params.name.length);
+ String name_str;
+ if (params.name.characters) {
+ auto name = ByteBuffer::create_uninitialized(params.name.length);
+ mmu().copy_from_vm(name.data(), (FlatPtr)params.name.characters, params.name.length);
+ name_str = { name.data(), name.size() };
+ }
+ mmu().add_region(MmapRegion::create_file_backed(final_address, final_size, params.prot, params.flags, params.fd, params.offset, name_str));
+ }
return final_address;
}
diff --git a/DevTools/UserspaceEmulator/MmapRegion.cpp b/DevTools/UserspaceEmulator/MmapRegion.cpp
index 61f6b06f18..c0ce837c38 100644
--- a/DevTools/UserspaceEmulator/MmapRegion.cpp
+++ b/DevTools/UserspaceEmulator/MmapRegion.cpp
@@ -39,11 +39,16 @@ NonnullOwnPtr<MmapRegion> MmapRegion::create_anonymous(u32 base, u32 size, u32 p
return region;
}
-NonnullOwnPtr<MmapRegion> MmapRegion::create_file_backed(u32 base, u32 size, u32 prot, int flags, int fd, off_t offset)
+NonnullOwnPtr<MmapRegion> MmapRegion::create_file_backed(u32 base, u32 size, u32 prot, int flags, int fd, off_t offset, String name)
{
auto region = adopt_own(*new MmapRegion(base, size, prot));
region->m_file_backed = true;
- region->m_data = (u8*)mmap(nullptr, size, prot, flags, fd, offset);
+ if (!name.is_empty()) {
+ dbgln("name is not empty");
+ name = String::format("%s (Emulated)", name.characters());
+ region->m_name = name;
+ }
+ region->m_data = (u8*)mmap_with_name(nullptr, size, prot, flags, fd, offset, name.is_empty() ? nullptr : name.characters());
ASSERT(region->m_data != MAP_FAILED);
return region;
}
diff --git a/DevTools/UserspaceEmulator/MmapRegion.h b/DevTools/UserspaceEmulator/MmapRegion.h
index 68c1eb864e..a94077fec7 100644
--- a/DevTools/UserspaceEmulator/MmapRegion.h
+++ b/DevTools/UserspaceEmulator/MmapRegion.h
@@ -37,7 +37,7 @@ class MallocTracer;
class MmapRegion final : public Region {
public:
static NonnullOwnPtr<MmapRegion> create_anonymous(u32 base, u32 size, u32 prot);
- static NonnullOwnPtr<MmapRegion> create_file_backed(u32 base, u32 size, u32 prot, int flags, int fd, off_t offset);
+ static NonnullOwnPtr<MmapRegion> create_file_backed(u32 base, u32 size, u32 prot, int flags, int fd, off_t offset, String name = {});
virtual ~MmapRegion() override;
virtual ValueWithShadow<u8> read8(u32 offset) override;
@@ -71,6 +71,7 @@ private:
bool m_malloc { false };
OwnPtr<MallocRegionMetadata> m_malloc_metadata;
+ String m_name;
};
}