L4Re - L4 Runtime Environment
br_manager
1 // vi:set ft=cpp: -*- Mode: C++ -*-
2 /*
3  * (c) 2014 Alexander Warg <alexander.warg@kernkonzept.com>
4  *
5  * This file is part of TUD:OS and distributed under the terms of the
6  * GNU General Public License 2.
7  * Please see the COPYING-GPL-2 file for details.
8  *
9  * As a special exception, you may use this file as part of a free software
10  * library without restriction. Specifically, if other files instantiate
11  * templates or use macros or inline functions from this file, or you compile
12  * this file and link it with other files to produce an executable, this
13  * file does not by itself cause the resulting executable to be covered by
14  * the GNU General Public License. This exception does not however
15  * invalidate any other reasons why the executable file might be covered by
16  * the GNU General Public License.
17  */
18 
19 #pragma once
20 
21 #include <l4/re/util/cap_alloc>
22 #include <l4/sys/cxx/ipc_server_loop>
23 #include <l4/cxx/ipc_timeout_queue>
24 #include <l4/sys/assert.h>
25 
26 namespace L4Re { namespace Util {
27 
37 {
38 private:
39  enum { _mem = 0, _ports = 0 };
40 
41 public:
43  Br_manager() : _caps(0), _cap_flags(L4_RCV_ITEM_LOCAL_ID) {}
44 
45  /*
46  * This implementation dynamically manages assignment of buffer registers for
47  * the necessary amount of receive buffers allocated by all calls to this
48  * function.
49  */
51  {
52  using L4::Ipc::Small_buf;
53 
54  // memory and IO port receive windows currently not supported
55  if (d.mem || d.ports)
56  return -L4_EINVAL;
57 
58  // take two extra buffers for a possible timeout and a zero terminator
59  if (d.caps + d.mem * 2 + d.ports * 2 + 3 >= L4_UTCB_GENERIC_BUFFERS_SIZE)
60  return -L4_ERANGE;
61 
62  if (d.caps > _caps)
63  {
64  while (_caps < d.caps)
65  {
67  if (!cap)
68  return -L4_ENOMEM;
69 
70  reinterpret_cast<Small_buf&>(_brs[_caps])
71  = Small_buf(cap.cap(), _cap_flags);
72  ++_caps;
73  }
74  _brs[_caps] = 0;
75  }
76 
77  return L4_EOK;
78  }
79 
80 
82  {
83  if (i < 0 || i >= _caps)
85 
86  return L4::Cap<void>(_brs[i] & L4_CAP_MASK);
87  }
88 
89  int realloc_rcv_cap(int i)
90  {
91  using L4::Ipc::Small_buf;
92 
93  if (i < 0 || i >= _caps)
94  return -L4_EINVAL;
95 
97  if (!cap)
98  return -L4_ENOMEM;
99 
100  reinterpret_cast<Small_buf&>(_brs[i])
101  = Small_buf(cap.cap(), _cap_flags);
102 
103  return L4_EOK;
104  }
105 
113  void set_rcv_cap_flags(unsigned long flags)
114  {
115  l4_assert(_caps == 0);
116 
117  _cap_flags = flags;
118  }
119 
122  { return -L4_ENOSYS; }
123 
126  { return -L4_ENOSYS; }
127 
130  {
131  l4_buf_regs_t *br = l4_utcb_br_u(utcb);
132  br->bdr = 0;
133  for (unsigned i = 0; i <= _caps; ++i)
134  br->br[i] = _brs[i];
135  }
136 
137 protected:
139  unsigned first_free_br() const
140  {
141  // we take the last BR here (this is c constant),
142  return L4_UTCB_GENERIC_BUFFERS_SIZE - 1;
143  // could also take _caps + _mem +_ports + 1 (would be dynamic)
144  // return _caps + _mem + _ports + 1;
145  }
146 
147 private:
148  unsigned short _caps;
149  unsigned long _cap_flags;
150 
152 };
153 
164  Br_manager
165 {};
166 
175  public L4::Ipc_svr::Timeout_queue_hooks<Br_manager_timeout_hooks, Br_manager>,
177 {
178 public:
179  static l4_kernel_clock_t now()
180  { return l4_kip_clock(l4re_kip()); }
181 };
182 
183 }}
184 
Mix in for LOOP_HOOKS to always use compound reply and wait.
Definition: ipc_server_loop:77
Low-level assert implementation.
void set_rcv_cap_flags(unsigned long flags)
Set the receive flags for the buffers.
Definition: br_manager:113
int alloc_buffer_demand(Demand const &d)
Tells the server to allocate buffers for the given demand.
Definition: br_manager:50
Data type for expressing the needed receive buffers at the server-side of an interface.
Definition: __typeinfo.h:516
unsigned char ports
number of IO-port receive buffers.
Definition: __typeinfo.h:527
Invalid argument.
Definition: err.h:56
Reply_mode
Reply mode for server loop.
Definition: ipc_server_loop:50
Interface for server-loop related functions.
Definition: ipc_epiface:47
Br_manager()
Make a buffer-register (BR) manager.
Definition: br_manager:43
L4::Cap< void > get_rcv_cap(int i) const
Get capability slot allocated to the given receive buffer.
Definition: br_manager:81
int add_timeout(L4::Ipc_svr::Timeout *, l4_kernel_clock_t)
No timeouts handled by us.
Definition: br_manager:121
A receive item for receiving a single capability.
Definition: ipc_types:268
l4_kernel_info_t * l4re_kip(void) L4_NOTHROW
Get Kernel Info Page.
Definition: env.h:188
L4Re C++ Interfaces.
Definition: cmd_control:15
struct l4_utcb_t l4_utcb_t
Opaque type for the UTCB.
Definition: utcb.h:67
Encapsulation of the buffer-registers block in the UTCB.
Definition: utcb.h:93
Callback interface for Timeout_queue.
The receiver requests to receive a local ID instead of a mapping whenever possible.
Definition: consts.h:212
Mix in for LOOP_HOOKS to use a 0 send and a infinite receive timeout.
Definition: ipc_server_loop:69
l4_umword_t bdr
Buffer descriptor.
Definition: utcb.h:96
Total number of buffer registers (BRs) available.
Definition: utcb.h:50
unsigned char caps
number of capability receive buffers.
Definition: __typeinfo.h:524
Ok.
Definition: err.h:43
l4_cap_idx_t cap() const
Return capability selector.
Definition: capability.h:52
_Cap_alloc & cap_alloc
Capability allocator.
unsigned long l4_umword_t
Unsigned machine word.
Definition: l4int.h:52
void setup_wait(l4_utcb_t *utcb, L4::Ipc_svr::Reply_mode)
setup_wait() used the server loop (L4::Server)
Definition: br_manager:129
l4_uint64_t l4_kernel_clock_t
Kernel clock type.
Definition: l4int.h:65
unsigned char mem
number of memory receive buffers.
Definition: __typeinfo.h:526
#define l4_assert(expr)
Low-level assert.
Definition: assert.h:43
Predefined server-loop hooks for a server with using the Br_manager and a timeout queue...
Definition: br_manager:174
Mask to get only the relevant bits of an l4_cap_idx_t.
Definition: consts.h:139
No memory.
Definition: err.h:50
L4::Cap< void > alloc()
Allocate a new capability slot.
int remove_timeout(L4::Ipc_svr::Timeout *)
No timeouts handled by us.
Definition: br_manager:125
Buffer-register (BR) manager for L4::Server.
Definition: br_manager:36
l4_cpu_time_t l4_kip_clock(l4_kernel_info_t *kip) L4_NOTHROW
Return clock value from the KIP.
Definition: kip.h:138
Predefined server-loop hooks for a server loop using the Br_manager.
Definition: br_manager:160
Capability allocator.
Range error.
Definition: err.h:58
No sys.
Definition: err.h:60
C++ interface for capabilities.
Definition: capability.h:13
l4_umword_t br[L4_UTCB_GENERIC_BUFFERS_SIZE]
Buffer registers.
Definition: utcb.h:99
Mix in for LOOP_HOOKS to ignore IPC errors.
Definition: ipc_server_loop:61
int realloc_rcv_cap(int i)
Allocate a new capability for the given receive buffer.
Definition: br_manager:89
Loop hooks mixin for integrating a timeout queue into the server loop.
unsigned first_free_br() const
Used for assigning BRs for a timeout.
Definition: br_manager:139