1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 17:45:09 +00:00

UserspaceEmulator: Add basic TLS (thread-local storage) support

The SoftMMU now receives full X86::LogicalAddress values from SoftCPU.
This allows the MMU to reroute TLS accesses to a special memory region.

The ELF executable's PT_TLS header tells us how to allocate the TLS.

Basically, the GS register points to a magical 4-byte area which has
a pointer to the TCB (thread control block). The TCB lives in normal
flat memory space and is accessed through the DS register.
This commit is contained in:
Andreas Kling 2020-07-12 00:54:09 +02:00
parent df95e25eaa
commit 734f63d522
4 changed files with 77 additions and 50 deletions

View file

@ -124,11 +124,23 @@ void Emulator::setup_stack()
bool Emulator::load_elf()
{
m_elf->image().for_each_program_header([&](const ELF::Image::ProgramHeader& program_header) {
if (program_header.type() != PT_LOAD)
if (program_header.type() == PT_LOAD) {
auto region = make<SimpleRegion>(program_header.vaddr().get(), program_header.size_in_memory());
memcpy(region->data(), program_header.raw_data(), program_header.size_in_image());
mmu().add_region(move(region));
return;
auto region = make<SimpleRegion>(program_header.vaddr().get(), program_header.size_in_memory());
memcpy(region->data(), program_header.raw_data(), program_header.size_in_image());
mmu().add_region(move(region));
}
if (program_header.type() == PT_TLS) {
auto tcb_region = make<SimpleRegion>(0x20000000, program_header.size_in_memory());
memcpy(tcb_region->data(), program_header.raw_data(), program_header.size_in_image());
auto tls_region = make<SimpleRegion>(0, 4);
tls_region->write32(0, tcb_region->base() + 8);
mmu().add_region(move(tcb_region));
mmu().set_tls_region(move(tls_region));
return;
}
});
m_cpu.set_eip(m_elf->image().entry().get());