genode/repos/base-sel4/src/core/include/page_table_registry.h
Norman Feske 357dbdd64b sel4: allocate vm-space meta data statically
This patch removes the dynamically growing slab allocator from the
page-table registry. This has two benefits. First, we alleviate the
corner cases where the slab allocator needed to extend its backing store
while establishing a core-local memory mapping, thereby triggering a
nested core-local mapping. Without this corner case, no reentrant lock
is needed any longer. Second, it removes the dependency from the overly
large old API of the slab allocator. So we can tighten the slab
interface.
2016-04-25 10:47:54 +02:00

284 lines
6.2 KiB
C++

/*
* \brief Associate page-table and frame selectors with virtual addresses
* \author Norman Feske
* \date 2015-05-04
*/
/*
* Copyright (C) 2015 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU General Public License version 2.
*/
#ifndef _CORE__INCLUDE__PAGE_TABLE_REGISTRY_H_
#define _CORE__INCLUDE__PAGE_TABLE_REGISTRY_H_
/* Genode includes */
#include <util/list.h>
#include <base/exception.h>
/* core includes */
#include <util.h>
#include <cap_sel_alloc.h>
namespace Genode { class Page_table_registry; }
class Genode::Page_table_registry
{
public:
class Lookup_failed : Exception { };
private:
/*
* XXX use AVL tree (with virtual address as key) instead of list
*/
class Page_table : public List<Page_table>::Element
{
public:
struct Entry : List<Entry>::Element
{
addr_t const addr;
unsigned const sel;
Entry(addr_t addr, unsigned sel) : addr(addr), sel(sel) { }
};
addr_t const addr;
static constexpr bool verbose = false;
private:
List<Entry> _entries;
static addr_t _page_frame_base(addr_t addr)
{
return addr & get_page_mask();
}
bool _entry_exists(addr_t addr) const
{
for (Entry const *e = _entries.first(); e; e = e->next()) {
if (_page_frame_base(e->addr) == _page_frame_base(addr))
return true;
}
return false;
}
public:
class Lookup_failed : Exception { };
Page_table(addr_t addr) : addr(addr) { }
Entry &lookup(addr_t addr)
{
for (Entry *e = _entries.first(); e; e = e->next()) {
if (_page_frame_base(e->addr) == _page_frame_base(addr))
return *e;
}
throw Lookup_failed();
}
void insert_entry(Allocator &entry_alloc, addr_t addr, unsigned sel)
{
if (_entry_exists(addr)) {
PWRN("trying to insert page frame for 0x%lx twice", addr);
return;
}
_entries.insert(new (entry_alloc) Entry(addr, sel));
}
void remove_entry(Allocator &entry_alloc, addr_t addr)
{
try {
Entry &entry = lookup(addr);
_entries.remove(&entry);
destroy(entry_alloc, &entry);
} catch (Lookup_failed) {
if (verbose)
PWRN("trying to remove non-existing page frame for 0x%lx", addr);
}
}
};
/**
* Allocator operating on a static memory pool
*
* \param ELEM element type
* \param MAX maximum number of elements
*
* The size of a single ELEM must be a multiple of sizeof(long).
*/
template <typename ELEM, size_t MAX>
class Static_allocator : public Allocator
{
private:
Bit_allocator<MAX> _used;
struct Elem_space
{
long space[sizeof(ELEM)/sizeof(long)];
};
Elem_space _elements[MAX];
public:
class Alloc_failed { };
bool alloc(size_t size, void **out_addr) override
{
*out_addr = nullptr;
if (size > sizeof(Elem_space)) {
PERR("unexpected allocation size of %zd", size);
return false;
}
try {
*out_addr = &_elements[_used.alloc()]; }
catch (typename Bit_allocator<MAX>::Out_of_indices) {
return false; }
return true;
}
size_t overhead(size_t) const override { return 0; }
void free(void *ptr, size_t) override
{
Elem_space *elem = reinterpret_cast<Elem_space *>(ptr);
unsigned const index = elem - &_elements[0];
_used.free(index);
}
bool need_size_for_free() const { return false; }
};
Static_allocator<Page_table, 128> _page_table_alloc;
Static_allocator<Page_table::Entry, 4096> _page_table_entry_alloc;
List<Page_table> _page_tables;
static addr_t _page_table_base(addr_t addr)
{
return addr & ~(4*1024*1024 - 1);
}
bool _page_table_exists(addr_t addr) const
{
for (Page_table const *pt = _page_tables.first(); pt; pt = pt->next()) {
if (_page_table_base(pt->addr) == _page_table_base(addr))
return true;
}
return false;
}
Page_table &_lookup(addr_t addr)
{
for (Page_table *pt = _page_tables.first(); pt; pt = pt->next()) {
if (_page_table_base(pt->addr) == _page_table_base(addr))
return *pt;
}
PDBG("page-table lookup failed");
throw Lookup_failed();
}
static constexpr bool verbose = false;
public:
/**
* Constructor
*
* \param md_alloc backing store allocator for metadata
*
* XXX The md_alloc argument is currently unused as we dimension
* MAX_PAGE_TABLES and MAX_PAGE_TABLE_ENTRIES statically.
*/
Page_table_registry(Allocator &md_alloc) { }
/**
* Register page table
*
* \param addr virtual address
* \param sel page-table selector
*/
void insert_page_table(addr_t addr, Cap_sel sel)
{
/* XXX sel is unused */
if (_page_table_exists(addr)) {
PWRN("trying to insert page table for 0x%lx twice", addr);
return;
}
_page_tables.insert(new (_page_table_alloc) Page_table(addr));
}
bool has_page_table_at(addr_t addr) const
{
return _page_table_exists(addr);
}
/**
* Register page table entry
*
* \param addr virtual address
* \param sel page frame selector
*
* \throw Lookup_failed no page table for given address
*/
void insert_page_table_entry(addr_t addr, unsigned sel)
{
_lookup(addr).insert_entry(_page_table_entry_alloc, addr, sel);
}
/**
* Discard the information about the given virtual address
*/
void forget_page_table_entry(addr_t addr)
{
try {
Page_table &page_table = _lookup(addr);
page_table.remove_entry(_page_table_entry_alloc, addr);
} catch (...) {
if (verbose)
PDBG("no PT entry found for virtual address 0x%lx", addr);
}
}
/**
* Apply functor 'fn' to selector of specified virtual address
*
* \param addr virtual address
*
* The functor is called with the selector of the page table entry
* (the copy of the phys frame selector) as argument.
*/
template <typename FN>
void apply(addr_t addr, FN const &fn)
{
try {
Page_table &page_table = _lookup(addr);
Page_table::Entry &entry = page_table.lookup(addr);
fn(entry.sel);
} catch (...) {
if (verbose)
PDBG("no PT entry found for virtual address 0x%lx", addr);
}
}
};
#endif /* _CORE__INCLUDE__PAGE_TABLE_REGISTRY_H_ */