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

UserspaceEmulator: Implement a proper VM allocator

This patch brings Kernel::RangeAllocator to UserspaceEmulator in a
slightly simplified form.

It supports the basic three allocation types needed by virt$mmap():
allocate_anywhere, allocate_specific, and allocate_randomized.

Porting virt$mmap() and virt$munmap() to use the allocator makes
UE work correctly once again. :^)
This commit is contained in:
Andreas Kling 2021-02-06 23:08:51 +01:00
parent 9dacd7c0ec
commit 89483a9408
10 changed files with 438 additions and 36 deletions

View file

@ -2,6 +2,8 @@ set(SOURCES
Emulator.cpp
MallocTracer.cpp
MmapRegion.cpp
Range.cpp
RangeAllocator.cpp
Region.cpp
SimpleRegion.cpp
SoftCPU.cpp

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -32,6 +32,7 @@
#include <AK/Format.h>
#include <AK/LexicalPath.h>
#include <AK/MappedFile.h>
#include <AK/Random.h>
#include <LibELF/AuxiliaryVector.h>
#include <LibELF/Image.h>
#include <LibELF/Validation.h>
@ -81,6 +82,19 @@ Emulator::Emulator(const String& executable_path, const Vector<String>& argument
, m_cpu(*this)
{
m_malloc_tracer = make<MallocTracer>(*this);
static constexpr FlatPtr userspace_range_base = 0x00800000;
static constexpr FlatPtr userspace_range_ceiling = 0xbe000000;
#ifdef UE_ASLR
static constexpr FlatPtr page_mask = 0xfffff000u;
size_t random_offset = (AK::get_random<u8>() % 32 * MiB) & page_mask;
FlatPtr base = userspace_range_base + random_offset;
#else
FlatPtr base = userspace_range_base;
#endif
m_range_allocator.initialize_with_range(VirtualAddress(base), userspace_range_ceiling - base);
ASSERT(!s_the);
s_the = this;
// setup_stack(arguments, environment);
@ -972,41 +986,31 @@ u32 Emulator::virt$munmap(FlatPtr address, u32 size)
ASSERT(region);
if (region->size() != round_up_to_power_of_two(size, PAGE_SIZE))
TODO();
m_range_allocator.deallocate(region->range());
mmu().remove_region(*region);
return 0;
}
FlatPtr Emulator::allocate_vm(size_t size, size_t alignment)
{
// FIXME: Write a proper VM allocator
static FlatPtr next_address = 0x30000000;
FlatPtr final_address;
if (!alignment)
alignment = PAGE_SIZE;
// FIXME: What if alignment is not a power of 2?
final_address = round_up_to_power_of_two(next_address, alignment);
next_address = round_up_to_power_of_two(final_address + size, PAGE_SIZE);
return final_address;
}
u32 Emulator::virt$mmap(u32 params_addr)
{
Syscall::SC_mmap_params params;
mmu().copy_from_vm(&params, params_addr, sizeof(params));
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
// because of the way we currently allocate VM.
ASSERT(params.addr == final_address);
u32 requested_size = round_up_to_power_of_two(params.size, PAGE_SIZE);
FlatPtr final_address;
Optional<Range> result;
if (params.flags & MAP_RANDOMIZED) {
result = m_range_allocator.allocate_randomized(requested_size, params.alignment);
} else if (params.flags & MAP_FIXED) {
result = m_range_allocator.allocate_specific(VirtualAddress { params.addr }, requested_size);
} else {
result = m_range_allocator.allocate_anywhere(requested_size, params.alignment);
}
if (!result.has_value())
return -ENOMEM;
final_address = result.value().base().get();
auto final_size = result.value().size();
if (params.flags & MAP_ANONYMOUS)
mmu().add_region(MmapRegion::create_anonymous(final_address, final_size, params.prot));

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -27,6 +27,7 @@
#pragma once
#include "MallocTracer.h"
#include "RangeAllocator.h"
#include "Report.h"
#include "SoftCPU.h"
#include "SoftMMU.h"
@ -174,7 +175,6 @@ private:
int virt$sendfd(int, int);
int virt$msyscall(FlatPtr);
FlatPtr allocate_vm(size_t size, size_t alignment);
bool find_malloc_symbols(const MmapRegion& libc_text);
void dispatch_one_pending_signal();
@ -213,6 +213,8 @@ private:
};
HashMap<String, CachedELF> m_dynamic_library_cache;
RangeAllocator m_range_allocator;
};
ALWAYS_INLINE bool Emulator::is_in_malloc_or_free() const

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without

View file

@ -0,0 +1,45 @@
/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "Range.h"
#include <AK/Vector.h>
namespace UserspaceEmulator {
Vector<Range, 2> Range::carve(const Range& taken)
{
ASSERT((taken.size() % PAGE_SIZE) == 0);
Vector<Range, 2> parts;
if (taken == *this)
return {};
if (taken.base() > base())
parts.append({ base(), taken.base().get() - base().get() });
if (taken.end() < end())
parts.append({ taken.end(), end().get() - taken.end().get() });
return parts;
}
}

View file

@ -0,0 +1,84 @@
/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Types.h>
#include <Kernel/VirtualAddress.h>
namespace UserspaceEmulator {
class Range {
friend class RangeAllocator;
public:
Range() = delete;
Range(VirtualAddress base, size_t size)
: m_base(base)
, m_size(size)
{
}
VirtualAddress base() const { return m_base; }
size_t size() const { return m_size; }
bool is_valid() const { return !m_base.is_null(); }
bool contains(VirtualAddress vaddr) const { return vaddr >= base() && vaddr < end(); }
VirtualAddress end() const { return m_base.offset(m_size); }
bool operator==(const Range& other) const
{
return m_base == other.m_base && m_size == other.m_size;
}
bool contains(VirtualAddress base, size_t size) const
{
if (base.offset(size) < base)
return false;
return base >= m_base && base.offset(size) <= end();
}
bool contains(const Range& other) const
{
return contains(other.base(), other.size());
}
Vector<Range, 2> carve(const Range&);
private:
VirtualAddress m_base;
size_t m_size { 0 };
};
}
namespace AK {
template<>
struct Traits<UserspaceEmulator::Range> : public GenericTraits<UserspaceEmulator::Range> {
static constexpr bool is_trivial() { return true; }
};
}

View file

@ -0,0 +1,207 @@
/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "RangeAllocator.h"
#include <AK/BinarySearch.h>
#include <AK/Checked.h>
#include <AK/QuickSort.h>
#include <AK/Random.h>
#define VM_GUARD_PAGES
#define PAGE_MASK ((FlatPtr)0xfffff000u)
namespace UserspaceEmulator {
RangeAllocator::RangeAllocator()
: m_total_range({}, 0)
{
}
void RangeAllocator::initialize_with_range(VirtualAddress base, size_t size)
{
m_total_range = { base, size };
m_available_ranges.append({ base, size });
}
RangeAllocator::~RangeAllocator()
{
}
void RangeAllocator::dump() const
{
dbgln("RangeAllocator({})", this);
for (auto& range : m_available_ranges) {
dbgln(" {:x} -> {:x}", range.base().get(), range.end().get() - 1);
}
}
void RangeAllocator::carve_at_index(int index, const Range& range)
{
auto remaining_parts = m_available_ranges[index].carve(range);
ASSERT(remaining_parts.size() >= 1);
ASSERT(m_total_range.contains(remaining_parts[0]));
m_available_ranges[index] = remaining_parts[0];
if (remaining_parts.size() == 2) {
ASSERT(m_total_range.contains(remaining_parts[1]));
m_available_ranges.insert(index + 1, move(remaining_parts[1]));
}
}
Optional<Range> RangeAllocator::allocate_randomized(size_t size, size_t alignment)
{
if (!size)
return {};
ASSERT((size % PAGE_SIZE) == 0);
ASSERT((alignment % PAGE_SIZE) == 0);
// FIXME: I'm sure there's a smarter way to do this.
static constexpr size_t maximum_randomization_attempts = 1000;
for (size_t i = 0; i < maximum_randomization_attempts; ++i) {
VirtualAddress random_address { AK::get_random<FlatPtr>() };
random_address.mask(PAGE_MASK);
if (!m_total_range.contains(random_address, size))
continue;
auto range = allocate_specific(random_address, size);
if (range.has_value())
return range;
}
return allocate_anywhere(size, alignment);
}
Optional<Range> RangeAllocator::allocate_anywhere(size_t size, size_t alignment)
{
if (!size)
return {};
ASSERT((size % PAGE_SIZE) == 0);
ASSERT((alignment % PAGE_SIZE) == 0);
#ifdef VM_GUARD_PAGES
// NOTE: We pad VM allocations with a guard page on each side.
if (Checked<size_t>::addition_would_overflow(size, PAGE_SIZE * 2))
return {};
size_t effective_size = size + PAGE_SIZE * 2;
size_t offset_from_effective_base = PAGE_SIZE;
#else
size_t effective_size = size;
size_t offset_from_effective_base = 0;
#endif
if (Checked<size_t>::addition_would_overflow(effective_size, alignment))
return {};
for (size_t i = 0; i < m_available_ranges.size(); ++i) {
auto& available_range = m_available_ranges[i];
// FIXME: This check is probably excluding some valid candidates when using a large alignment.
if (available_range.size() < (effective_size + alignment))
continue;
FlatPtr initial_base = available_range.base().offset(offset_from_effective_base).get();
FlatPtr aligned_base = round_up_to_power_of_two(initial_base, alignment);
Range allocated_range(VirtualAddress(aligned_base), size);
ASSERT(m_total_range.contains(allocated_range));
if (available_range == allocated_range) {
m_available_ranges.remove(i);
return allocated_range;
}
carve_at_index(i, allocated_range);
return allocated_range;
}
klog() << "RangeAllocator: Failed to allocate anywhere: " << size << ", " << alignment;
return {};
}
Optional<Range> RangeAllocator::allocate_specific(VirtualAddress base, size_t size)
{
if (!size)
return {};
ASSERT(base.is_page_aligned());
ASSERT((size % PAGE_SIZE) == 0);
Range allocated_range(base, size);
for (size_t i = 0; i < m_available_ranges.size(); ++i) {
auto& available_range = m_available_ranges[i];
ASSERT(m_total_range.contains(allocated_range));
if (!available_range.contains(base, size))
continue;
if (available_range == allocated_range) {
m_available_ranges.remove(i);
return allocated_range;
}
carve_at_index(i, allocated_range);
return allocated_range;
}
return {};
}
void RangeAllocator::deallocate(const Range& range)
{
ASSERT(m_total_range.contains(range));
ASSERT(range.size());
ASSERT((range.size() % PAGE_SIZE) == 0);
ASSERT(range.base() < range.end());
ASSERT(!m_available_ranges.is_empty());
size_t nearby_index = 0;
auto* existing_range = binary_search(
m_available_ranges.span(),
range,
&nearby_index,
[](auto& a, auto& b) { return a.base().get() - b.end().get(); });
size_t inserted_index = 0;
if (existing_range) {
existing_range->m_size += range.size();
inserted_index = nearby_index;
} else {
m_available_ranges.insert_before_matching(
Range(range), [&](auto& entry) {
return entry.base() >= range.end();
},
nearby_index, &inserted_index);
}
if (inserted_index < (m_available_ranges.size() - 1)) {
// We already merged with previous. Try to merge with next.
auto& inserted_range = m_available_ranges[inserted_index];
auto& next_range = m_available_ranges[inserted_index + 1];
if (inserted_range.end() == next_range.base()) {
inserted_range.m_size += next_range.size();
m_available_ranges.remove(inserted_index + 1);
return;
}
}
}
}

View file

@ -0,0 +1,57 @@
/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include "Range.h"
#include <AK/Vector.h>
namespace UserspaceEmulator {
class RangeAllocator {
public:
RangeAllocator();
~RangeAllocator();
void initialize_with_range(VirtualAddress, size_t);
Optional<Range> allocate_anywhere(size_t, size_t alignment = PAGE_SIZE);
Optional<Range> allocate_specific(VirtualAddress, size_t);
Optional<Range> allocate_randomized(size_t, size_t alignment);
void deallocate(const Range&);
void dump() const;
bool contains(const Range& range) const { return m_total_range.contains(range); }
private:
void carve_at_index(int, const Range&);
Vector<Range> m_available_ranges;
Range m_total_range;
};
}

View file

@ -31,8 +31,7 @@ namespace UserspaceEmulator {
Region::Region(u32 base, u32 size)
: m_emulator(Emulator::the())
, m_base(base)
, m_size(size)
, m_range(Range { VirtualAddress { base }, size })
{
}

View file

@ -26,6 +26,7 @@
#pragma once
#include "Range.h"
#include "ValueWithShadow.h"
#include <AK/TypeCasts.h>
#include <AK/Types.h>
@ -38,9 +39,11 @@ class Region {
public:
virtual ~Region() { }
u32 base() const { return m_base; }
u32 size() const { return m_size; }
u32 end() const { return m_base + m_size; }
const Range& range() const { return m_range; }
u32 base() const { return m_range.base().get(); }
u32 size() const { return m_range.size(); }
u32 end() const { return m_range.end().get(); }
bool contains(u32 address) const { return address >= base() && address < end(); }
@ -82,8 +85,7 @@ protected:
private:
Emulator& m_emulator;
u32 m_base { 0 };
u32 m_size { 0 };
Range m_range;
bool m_stack { false };
bool m_text { false };