Loading...
Searching...
No Matches
br_manager
// vi:set ft=cpp: -*- Mode: C++ -*-
/*
* (c) 2014 Alexander Warg <alexander.warg@kernkonzept.com>
*
* License: see LICENSE.spdx (in this directory or the directories above)
*/
#pragma once
#include <l4/re/util/cap_alloc>
#include <l4/sys/cxx/ipc_server_loop>
#include <l4/cxx/ipc_timeout_queue>
#include <l4/sys/assert.h>
namespace L4Re { namespace Util {
class Br_manager : public L4::Ipc_svr::Server_iface
{
private:
enum { _mem = 0, _ports = 0 };
enum { Brs_per_timeout = sizeof(l4_kernel_clock_t) / sizeof(l4_umword_t) };
public:
Br_manager() : _caps(0), _cap_flags(L4_RCV_ITEM_LOCAL_ID) {}
Br_manager(Br_manager const &) = delete;
Br_manager &operator = (Br_manager const &) = delete;
Br_manager(Br_manager &&) = delete;
Br_manager &operator = (Br_manager &&) = delete;
~Br_manager()
{
// Slots for received capabilities are placed at the beginning of the
// (shadowed) buffer registers. Free those.
for (unsigned i = 0; i < _caps; ++i)
cap_alloc.free(L4::Cap<void>(_brs[i] & L4_CAP_MASK));
}
/*
* This implementation dynamically manages assignment of buffer registers for
* the necessary amount of receive buffers allocated by all calls to this
* function.
*/
int alloc_buffer_demand(Demand const &d) override
{
using L4::Ipc::Small_buf;
// memory and IO port receive windows currently not supported
if (d.mem || d.ports)
return -L4_EINVAL;
// take extra buffers for a possible timeout and for a zero terminator
if (d.caps + d.mem * 2 + d.ports * 2 + Brs_per_timeout + 1
> L4_UTCB_GENERIC_BUFFERS_SIZE)
return -L4_ERANGE;
if (d.caps > _caps)
{
while (_caps < d.caps)
{
L4::Cap<void> cap = cap_alloc.alloc();
if (!cap)
return -L4_ENOMEM;
reinterpret_cast<Small_buf&>(_brs[_caps])
= Small_buf(cap.cap(), _cap_flags);
++_caps;
}
_brs[_caps] = 0;
}
return L4_EOK;
}
L4::Cap<void> get_rcv_cap(int i) const override
{
if (i < 0 || i >= _caps)
return L4::Cap<void>::Invalid;
return L4::Cap<void>(_brs[i] & L4_CAP_MASK);
}
int realloc_rcv_cap(int i) override
{
using L4::Ipc::Small_buf;
if (i < 0 || i >= _caps)
return -L4_EINVAL;
L4::Cap<void> cap = cap_alloc.alloc();
if (!cap)
return -L4_ENOMEM;
reinterpret_cast<Small_buf&>(_brs[i])
= Small_buf(cap.cap(), _cap_flags);
return L4_EOK;
}
void set_rcv_cap_flags(unsigned long flags)
{
l4_assert(_caps == 0);
_cap_flags = flags;
}
int add_timeout(L4::Ipc_svr::Timeout *, l4_kernel_clock_t) override
{ return -L4_ENOSYS; }
int remove_timeout(L4::Ipc_svr::Timeout *) override
{ return -L4_ENOSYS; }
void setup_wait(l4_utcb_t *utcb, L4::Ipc_svr::Reply_mode)
{
l4_buf_regs_t *br = l4_utcb_br_u(utcb);
br->bdr = 0;
for (unsigned i = 0; i <= _caps; ++i)
br->br[i] = _brs[i];
}
protected:
unsigned first_free_br() const
{
// The last BR (64-bit) or the last two BRs (32-bit); this is constant.
return L4_UTCB_GENERIC_BUFFERS_SIZE - Brs_per_timeout;
// We could also do the following dynamic approach:
// return _caps + _mem + _ports + 1
}
private:
unsigned short _caps;
unsigned long _cap_flags;
l4_umword_t _brs[L4_UTCB_GENERIC_BUFFERS_SIZE];
};
struct Br_manager_hooks
: L4::Ipc_svr::Ignore_errors,
L4::Ipc_svr::Default_timeout,
L4::Ipc_svr::Compound_reply,
Br_manager
{};
struct Br_manager_timeout_hooks :
public L4::Ipc_svr::Timeout_queue_hooks<Br_manager_timeout_hooks, Br_manager>,
public L4::Ipc_svr::Ignore_errors
{
public:
static l4_kernel_clock_t now()
{ return l4_kip_clock(l4re_kip()); }
};
}}
L4Re::Util::Br_manager
Buffer-register (BR) manager for L4::Server.
Definition br_manager:26
L4Re::Util::Br_manager::realloc_rcv_cap
int realloc_rcv_cap(int i) override
Allocate a new capability for the given receive buffer.
Definition br_manager:94
L4Re::Util::Br_manager::first_free_br
unsigned first_free_br() const
Used for assigning BRs for a timeout.
Definition br_manager:144
L4Re::Util::Br_manager::add_timeout
int add_timeout(L4::Ipc_svr::Timeout *, l4_kernel_clock_t) override
No timeouts handled by us.
Definition br_manager:126
L4Re::Util::Br_manager::set_rcv_cap_flags
void set_rcv_cap_flags(unsigned long flags)
Set the receive flags for the buffers.
Definition br_manager:118
L4Re::Util::Br_manager::setup_wait
void setup_wait(l4_utcb_t *utcb, L4::Ipc_svr::Reply_mode)
setup_wait() used the server loop (L4::Server)
Definition br_manager:134
L4Re::Util::Br_manager::Br_manager
Br_manager()
Make a buffer-register (BR) manager.
Definition br_manager:33
L4Re::Util::Br_manager::get_rcv_cap
L4::Cap< void > get_rcv_cap(int i) const override
Get capability slot allocated to the given receive buffer.
Definition br_manager:86
L4Re::Util::Br_manager::remove_timeout
int remove_timeout(L4::Ipc_svr::Timeout *) override
No timeouts handled by us.
Definition br_manager:130
L4Re::Util::Br_manager::alloc_buffer_demand
int alloc_buffer_demand(Demand const &d) override
Tells the server to allocate buffers for the given demand.
Definition br_manager:54
L4::Cap_base::cap
l4_cap_idx_t cap() const noexcept
Return capability selector.
Definition capability.h:49
L4::Cap_base::Invalid
@ Invalid
Invalid capability selector.
Definition capability.h:42
L4::Cap
C++ interface for capabilities.
Definition capability.h:224
L4::Ipc::Small_buf
A receive item for receiving a single object capability.
Definition ipc_types:258
L4::Ipc_svr::Server_iface
Interface for server-loop related functions.
Definition ipc_epiface:37
L4::Ipc_svr::Server_iface::Demand
L4::Type_info::Demand Demand
Data type expressing server-side demand for receive buffers.
Definition ipc_epiface:44
L4::Ipc_svr::Timeout_queue_hooks
Loop hooks mixin for integrating a timeout queue into the server loop.
Definition ipc_timeout_queue:151
L4::Ipc_svr::Timeout
Callback interface for Timeout_queue.
Definition ipc_timeout_queue:19
L4::Type_info::Demand::mem
unsigned char mem
number of memory receive buffers.
Definition __typeinfo.h:516
L4::Type_info::Demand::caps
unsigned char caps
number of capability receive buffers.
Definition __typeinfo.h:514
L4::Type_info::Demand::ports
unsigned char ports
number of IO-port receive buffers.
Definition __typeinfo.h:517
l4re_kip
l4_kernel_info_t const * l4re_kip(void) L4_NOTHROW
Get Kernel Info Page.
Definition env.h:185
L4::Ipc_svr::Reply_mode
Reply_mode
Reply mode for server loop.
Definition ipc_server_loop:47
l4_umword_t
unsigned long l4_umword_t
Unsigned machine word.
Definition l4int.h:40
l4_kernel_clock_t
l4_uint64_t l4_kernel_clock_t
Kernel clock type.
Definition l4int.h:53
L4_CAP_MASK
@ L4_CAP_MASK
Mask to get only the relevant bits of an l4_cap_idx_t.
Definition consts.h:151
L4_ERANGE
@ L4_ERANGE
Range error.
Definition err.h:48
L4_ENOSYS
@ L4_ENOSYS
No sys.
Definition err.h:50
L4_EINVAL
@ L4_EINVAL
Invalid argument.
Definition err.h:46
L4_EOK
@ L4_EOK
Ok.
Definition err.h:32
L4_ENOMEM
@ L4_ENOMEM
No memory.
Definition err.h:39
l4_kip_clock
l4_cpu_time_t l4_kip_clock(l4_kernel_info_t const *kip) L4_NOTHROW
Return clock value from the KIP.
Definition kip.h:242
L4_RCV_ITEM_LOCAL_ID
@ L4_RCV_ITEM_LOCAL_ID
The receiver requests to receive a local ID instead of a mapping whenever possible.
Definition consts.h:296
l4_utcb_t
struct l4_utcb_t l4_utcb_t
Opaque type for the UTCB.
Definition utcb.h:56
L4Re::Util::cap_alloc
_Cap_alloc & cap_alloc
Capability allocator.
L4Re::Util
Documentation of the L4 Runtime Environment utility functionality in C++.
Definition l4re.dox:21
L4Re
L4Re C++ Interfaces.
Definition cmd_control:14
L4Re::Util::Br_manager_hooks
Predefined server-loop hooks for a server loop using the Br_manager.
Definition br_manager:170
L4Re::Util::Br_manager_timeout_hooks
Predefined server-loop hooks for a server with using the Br_manager and a timeout queue.
Definition br_manager:182
L4::Ipc_svr::Compound_reply
Mix in for LOOP_HOOKS to always use compound reply and wait.
Definition ipc_server_loop:74
L4::Ipc_svr::Default_timeout
Mix in for LOOP_HOOKS to use a 0 send and a infinite receive timeout.
Definition ipc_server_loop:66
L4::Ipc_svr::Ignore_errors
Mix in for LOOP_HOOKS to ignore IPC errors.
Definition ipc_server_loop:58
l4_buf_regs_t
Encapsulation of the buffer-registers block in the UTCB.
Definition utcb.h:83
l4_buf_regs_t::br
l4_umword_t br[L4_UTCB_GENERIC_BUFFERS_SIZE]
Buffer registers.
Definition utcb.h:88
l4_buf_regs_t::bdr
l4_umword_t bdr
Buffer descriptor.
Definition utcb.h:85
assert.h
Low-level assert implementation.
l4_assert
#define l4_assert(expr)
Low-level assert.
Definition assert.h:32
cap_alloc
Capability allocator.