1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 21:07:34 +00:00

UserspaceEmulator: Add basic support for memory-mapped files

MmapRegion now supports using an mmap'ed file descriptor as backing.
This commit is contained in:
Andreas Kling 2020-07-13 11:58:58 +02:00
parent 63d3f5d19b
commit 9b6464010f
3 changed files with 30 additions and 5 deletions

View file

@ -309,7 +309,6 @@ u32 Emulator::virt$mmap(u32 params_addr)
mmu().copy_from_vm(&params, params_addr, sizeof(params));
ASSERT(params.addr == 0);
ASSERT(params.flags & MAP_ANONYMOUS);
// FIXME: Write a proper VM allocator
static u32 next_address = 0x30000000;
@ -326,7 +325,10 @@ u32 Emulator::virt$mmap(u32 params_addr)
next_address = final_address + final_size;
mmu().add_region(make<MmapRegion>(final_address, final_size, params.prot));
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));
return final_address;
}

View file

@ -30,15 +30,34 @@
namespace UserspaceEmulator {
NonnullOwnPtr<MmapRegion> MmapRegion::create_anonymous(u32 base, u32 size, u32 prot)
{
auto region = adopt_own(*new MmapRegion(base, size, prot));
region->m_file_backed = false;
region->m_data = (u8*)calloc(1, size);
return region;
}
NonnullOwnPtr<MmapRegion> MmapRegion::create_file_backed(u32 base, u32 size, u32 prot, int flags, int fd, off_t offset)
{
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);
ASSERT(region->m_data != MAP_FAILED);
return region;
}
MmapRegion::MmapRegion(u32 base, u32 size, int prot)
: Region(base, size)
, m_prot(prot)
{
m_data = (u8*)calloc(1, size);
}
MmapRegion::~MmapRegion()
{
if (m_file_backed)
munmap(m_data, size());
else
free(m_data);
}

View file

@ -33,7 +33,8 @@ namespace UserspaceEmulator {
class MmapRegion final : public SoftMMU::Region {
public:
MmapRegion(u32 base, u32 size, int prot);
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);
virtual ~MmapRegion() override;
virtual u8 read8(u32 offset) override;
@ -51,8 +52,11 @@ public:
bool is_executable() const { return m_prot & PROT_EXEC; }
private:
MmapRegion(u32 base, u32 size, int prot);
u8* m_data { nullptr };
int m_prot { 0 };
bool m_file_backed { false };
};
}