2013-12-05 14:45:14 +00:00
|
|
|
/**
|
|
|
|
* \brief Fast allocator for porting
|
|
|
|
* \author Sebastian Sumpf
|
|
|
|
* \date 2013-06-12
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2013-2014 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 _INCLUDE__UTIL__ALLOCATOR_FAP_H_
|
|
|
|
#define _INCLUDE__UTIL__ALLOCATOR_FAP_H_
|
|
|
|
|
|
|
|
#include <base/allocator_avl.h>
|
|
|
|
#include <dataspace/client.h>
|
|
|
|
#include <rm_session/connection.h>
|
2016-04-15 13:19:22 +00:00
|
|
|
#include <region_map/client.h>
|
2013-12-05 14:45:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace Allocator {
|
|
|
|
template <unsigned VM_SIZE, typename POLICY> class Backend_alloc;
|
|
|
|
template <unsigned VM_SIZE, typename POLICY> class Fap;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace Allocator {
|
|
|
|
|
|
|
|
using namespace Genode;
|
|
|
|
|
|
|
|
struct Default_allocator_policy
|
|
|
|
{
|
|
|
|
static int block() { return 0; }
|
|
|
|
static void unblock(int) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename POLICY>
|
|
|
|
struct Policy_guard
|
|
|
|
{
|
|
|
|
int val;
|
|
|
|
Policy_guard() { val = POLICY::block(); }
|
|
|
|
~Policy_guard() { POLICY::unblock(val); }
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Back-end allocator for Genode's slab allocator
|
|
|
|
*/
|
|
|
|
template <unsigned VM_SIZE, typename POLICY = Default_allocator_policy>
|
|
|
|
class Backend_alloc : public Genode::Allocator,
|
2016-04-15 13:19:22 +00:00
|
|
|
public Genode::Rm_connection,
|
|
|
|
public Genode::Region_map_client
|
2013-12-05 14:45:14 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
enum {
|
|
|
|
BLOCK_SIZE = 1024 * 1024, /* 1 MB */
|
|
|
|
ELEMENTS = VM_SIZE / BLOCK_SIZE, /* MAX number of dataspaces in VM */
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef Genode::addr_t addr_t;
|
|
|
|
typedef Genode::Ram_dataspace_capability Ram_dataspace_capability;
|
|
|
|
typedef Genode::Allocator_avl Allocator_avl;
|
|
|
|
|
|
|
|
addr_t _base; /* virt. base address */
|
2014-06-19 14:37:31 +00:00
|
|
|
Cache_attribute _cached; /* non-/cached RAM */
|
2013-12-05 14:45:14 +00:00
|
|
|
Ram_dataspace_capability _ds_cap[ELEMENTS]; /* dataspaces to put in VM */
|
|
|
|
addr_t _ds_phys[ELEMENTS]; /* physical bases of dataspaces */
|
|
|
|
int _index = 0; /* current index in ds_cap */
|
|
|
|
Allocator_avl _range; /* manage allocations */
|
|
|
|
bool _quota_exceeded = false;
|
|
|
|
|
|
|
|
bool _alloc_block()
|
|
|
|
{
|
|
|
|
if (_quota_exceeded)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (_index == ELEMENTS) {
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 17:07:09 +00:00
|
|
|
error("slab backend exhausted!");
|
2013-12-05 14:45:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Policy_guard<POLICY> guard;
|
|
|
|
|
|
|
|
try {
|
|
|
|
_ds_cap[_index] = Genode::env()->ram_session()->alloc(BLOCK_SIZE, _cached);
|
|
|
|
/* attach at index * BLOCK_SIZE */
|
2016-04-15 13:19:22 +00:00
|
|
|
Region_map_client::attach_at(_ds_cap[_index], _index * BLOCK_SIZE, BLOCK_SIZE, 0);
|
2013-12-05 14:45:14 +00:00
|
|
|
/* lookup phys. address */
|
|
|
|
_ds_phys[_index] = Genode::Dataspace_client(_ds_cap[_index]).phys_addr();
|
|
|
|
} catch (Genode::Ram_session::Quota_exceeded) {
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 17:07:09 +00:00
|
|
|
warning("backend allocator exhausted");
|
2013-12-05 14:45:14 +00:00
|
|
|
_quota_exceeded = true;
|
|
|
|
return false;
|
2016-04-15 13:19:22 +00:00
|
|
|
} catch (Genode::Region_map::Attach_failed) {
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 17:07:09 +00:00
|
|
|
warning("backend VM region exhausted");
|
2013-12-05 14:45:14 +00:00
|
|
|
_quota_exceeded = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return base + offset in VM area */
|
|
|
|
addr_t block_base = _base + (_index * BLOCK_SIZE);
|
|
|
|
++_index;
|
|
|
|
|
|
|
|
_range.add_range(block_base, BLOCK_SIZE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2014-06-19 14:37:31 +00:00
|
|
|
Backend_alloc(Cache_attribute cached)
|
2016-04-15 13:19:22 +00:00
|
|
|
:
|
|
|
|
Region_map_client(Rm_connection::create(VM_SIZE)),
|
|
|
|
_cached(cached),
|
|
|
|
_range(Genode::env()->heap())
|
2013-12-05 14:45:14 +00:00
|
|
|
{
|
|
|
|
/* reserver attach us, anywere */
|
|
|
|
_base = Genode::env()->rm_session()->attach(dataspace());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-23 13:17:54 +00:00
|
|
|
* Allocate
|
2013-12-05 14:45:14 +00:00
|
|
|
*/
|
|
|
|
bool alloc(size_t size, void **out_addr)
|
|
|
|
{
|
|
|
|
bool done = _range.alloc(size, out_addr);
|
|
|
|
|
|
|
|
if (done)
|
|
|
|
return done;
|
|
|
|
|
|
|
|
done = _alloc_block();
|
2014-04-23 13:17:54 +00:00
|
|
|
if (!done)
|
2013-12-05 14:45:14 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return _range.alloc(size, out_addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *alloc_aligned(size_t size, int align = 0)
|
|
|
|
{
|
|
|
|
void *addr;
|
|
|
|
|
2016-05-11 16:21:47 +00:00
|
|
|
if (!_range.alloc_aligned(size, &addr, align).error())
|
2013-12-05 14:45:14 +00:00
|
|
|
return addr;
|
|
|
|
|
|
|
|
if (!_alloc_block())
|
|
|
|
return 0;
|
|
|
|
|
2016-05-11 16:21:47 +00:00
|
|
|
if (_range.alloc_aligned(size, &addr, align).error()) {
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 17:07:09 +00:00
|
|
|
error("backend allocator: Unable to allocate memory "
|
|
|
|
"(size: ", size, " align: ", align, ")");
|
2013-12-05 14:45:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
2015-04-14 12:14:20 +00:00
|
|
|
void free(void *addr, size_t size) override { _range.free(addr, size); }
|
|
|
|
size_t overhead(size_t size) const override { return 0; }
|
2013-12-05 14:45:14 +00:00
|
|
|
bool need_size_for_free() const override { return false; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return phys address for given virtual addr.
|
|
|
|
*/
|
|
|
|
addr_t phys_addr(addr_t addr)
|
|
|
|
{
|
|
|
|
if (addr < _base || addr >= (_base + VM_SIZE))
|
|
|
|
return ~0UL;
|
|
|
|
|
|
|
|
int index = (addr - _base) / BLOCK_SIZE;
|
|
|
|
|
|
|
|
/* physical base of dataspace */
|
|
|
|
addr_t phys = _ds_phys[index];
|
|
|
|
|
|
|
|
if (!phys)
|
|
|
|
return ~0UL;
|
|
|
|
|
|
|
|
/* add offset */
|
|
|
|
phys += (addr - _base - (index * BLOCK_SIZE));
|
|
|
|
return phys;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool inside(addr_t addr) const { return (addr >= _base) && (addr < (_base + VM_SIZE)); }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Interface
|
|
|
|
*/
|
|
|
|
template <unsigned VM_SIZE, typename POLICY = Default_allocator_policy>
|
|
|
|
class Fap
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
typedef Allocator::Backend_alloc<VM_SIZE, POLICY> Backend_alloc;
|
|
|
|
|
|
|
|
Backend_alloc _back_allocator;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
Fap(bool cached)
|
2014-06-19 14:37:31 +00:00
|
|
|
: _back_allocator(cached ? CACHED : UNCACHED) { }
|
2013-12-05 14:45:14 +00:00
|
|
|
|
|
|
|
void *alloc(size_t size, int align = 0)
|
|
|
|
{
|
|
|
|
return _back_allocator.alloc_aligned(size, align);
|
|
|
|
}
|
|
|
|
|
|
|
|
void free(void *addr, size_t size)
|
|
|
|
{
|
|
|
|
_back_allocator.free(addr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
addr_t phys_addr(void *addr)
|
|
|
|
{
|
|
|
|
return _back_allocator.phys_addr((addr_t)addr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} /* namespace Allocator */
|
|
|
|
|
|
|
|
#endif /* _INCLUDE__UTIL__ALLOCATOR_FAP_H_ */
|