mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 20:17:41 +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:
parent
63d3f5d19b
commit
9b6464010f
3 changed files with 30 additions and 5 deletions
|
@ -309,7 +309,6 @@ u32 Emulator::virt$mmap(u32 params_addr)
|
||||||
mmu().copy_from_vm(¶ms, params_addr, sizeof(params));
|
mmu().copy_from_vm(¶ms, params_addr, sizeof(params));
|
||||||
|
|
||||||
ASSERT(params.addr == 0);
|
ASSERT(params.addr == 0);
|
||||||
ASSERT(params.flags & MAP_ANONYMOUS);
|
|
||||||
|
|
||||||
// FIXME: Write a proper VM allocator
|
// FIXME: Write a proper VM allocator
|
||||||
static u32 next_address = 0x30000000;
|
static u32 next_address = 0x30000000;
|
||||||
|
@ -326,7 +325,10 @@ u32 Emulator::virt$mmap(u32 params_addr)
|
||||||
|
|
||||||
next_address = final_address + final_size;
|
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;
|
return final_address;
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,16 +30,35 @@
|
||||||
|
|
||||||
namespace UserspaceEmulator {
|
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)
|
MmapRegion::MmapRegion(u32 base, u32 size, int prot)
|
||||||
: Region(base, size)
|
: Region(base, size)
|
||||||
, m_prot(prot)
|
, m_prot(prot)
|
||||||
{
|
{
|
||||||
m_data = (u8*)calloc(1, size);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MmapRegion::~MmapRegion()
|
MmapRegion::~MmapRegion()
|
||||||
{
|
{
|
||||||
free(m_data);
|
if (m_file_backed)
|
||||||
|
munmap(m_data, size());
|
||||||
|
else
|
||||||
|
free(m_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
u8 MmapRegion::read8(FlatPtr offset)
|
u8 MmapRegion::read8(FlatPtr offset)
|
||||||
|
|
|
@ -33,7 +33,8 @@ namespace UserspaceEmulator {
|
||||||
|
|
||||||
class MmapRegion final : public SoftMMU::Region {
|
class MmapRegion final : public SoftMMU::Region {
|
||||||
public:
|
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 ~MmapRegion() override;
|
||||||
|
|
||||||
virtual u8 read8(u32 offset) override;
|
virtual u8 read8(u32 offset) override;
|
||||||
|
@ -51,8 +52,11 @@ public:
|
||||||
bool is_executable() const { return m_prot & PROT_EXEC; }
|
bool is_executable() const { return m_prot & PROT_EXEC; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
MmapRegion(u32 base, u32 size, int prot);
|
||||||
|
|
||||||
u8* m_data { nullptr };
|
u8* m_data { nullptr };
|
||||||
int m_prot { 0 };
|
int m_prot { 0 };
|
||||||
|
bool m_file_backed { false };
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue