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  Br_manager(Br_manager const &) = delete;
46  Br_manager &operator = (Br_manager const &) = delete;
47 
48  Br_manager(Br_manager &&) = default;
49  Br_manager &operator = (Br_manager &&) = default;
50 
51  ~Br_manager()
52  {
53  // Slots for received capabilities are placed at the beginning of the
54  // (shadowed) buffer registers. Free those.
55  for (unsigned i = 0; i < _caps; ++i)
57  }
58 
59  /*
60  * This implementation dynamically manages assignment of buffer registers for
61  * the necessary amount of receive buffers allocated by all calls to this
62  * function.
63  */
65  {
66  using L4::Ipc::Small_buf;
67 
68  // memory and IO port receive windows currently not supported
69  if (d.mem || d.ports)
70  return -L4_EINVAL;
71 
72  // take two extra buffers for a possible timeout and a zero terminator
73  if (d.caps + d.mem * 2 + d.ports * 2 + 3 >= L4_UTCB_GENERIC_BUFFERS_SIZE)
74  return -L4_ERANGE;
75 
76  if (d.caps > _caps)
77  {
78  while (_caps < d.caps)
79  {
81  if (!cap)
82  return -L4_ENOMEM;
83 
84  reinterpret_cast<Small_buf&>(_brs[_caps])
85  = Small_buf(cap.cap(), _cap_flags);
86  ++_caps;
87  }
88  _brs[_caps] = 0;
89  }
90 
91  return L4_EOK;
92  }
93 
94 
96  {
97  if (i < 0 || i >= _caps)
99 
100  return L4::Cap<void>(_brs[i] & L4_CAP_MASK);
101  }
102 
103  int realloc_rcv_cap(int i)
104  {
105  using L4::Ipc::Small_buf;
106 
107  if (i < 0 || i >= _caps)
108  return -L4_EINVAL;
109 
110  L4::Cap<void> cap = cap_alloc.alloc();
111  if (!cap)
112  return -L4_ENOMEM;
113 
114  reinterpret_cast<Small_buf&>(_brs[i])
115  = Small_buf(cap.cap(), _cap_flags);
116 
117  return L4_EOK;
118  }
119 
127  void set_rcv_cap_flags(unsigned long flags)
128  {
129  l4_assert(_caps == 0);
130 
131  _cap_flags = flags;
132  }
133 
136  { return -L4_ENOSYS; }
137 
140  { return -L4_ENOSYS; }
141 
144  {
145  l4_buf_regs_t *br = l4_utcb_br_u(utcb);
146  br->bdr = 0;
147  for (unsigned i = 0; i <= _caps; ++i)
148  br->br[i] = _brs[i];
149  }
150 
151 protected:
153  unsigned first_free_br() const
154  {
155  // we take the last BR here (this is c constant),
156  return L4_UTCB_GENERIC_BUFFERS_SIZE - 1;
157  // could also take _caps + _mem +_ports + 1 (would be dynamic)
158  // return _caps + _mem + _ports + 1;
159  }
160 
161 private:
162  unsigned short _caps;
163  unsigned long _cap_flags;
164 
166 };
167 
178  Br_manager
179 {};
180 
189  public L4::Ipc_svr::Timeout_queue_hooks<Br_manager_timeout_hooks, Br_manager>,
191 {
192 public:
193  static l4_kernel_clock_t now()
194  { return l4_kip_clock(l4re_kip()); }
195 };
196 
197 }}
198 
Buffer-register (BR) manager for L4::Server.
Definition: br_manager:37
unsigned first_free_br() const
Used for assigning BRs for a timeout.
Definition: br_manager:153
int realloc_rcv_cap(int i)
Allocate a new capability for the given receive buffer.
Definition: br_manager:103
void set_rcv_cap_flags(unsigned long flags)
Set the receive flags for the buffers.
Definition: br_manager:127
int remove_timeout(L4::Ipc_svr::Timeout *)
No timeouts handled by us.
Definition: br_manager:139
void setup_wait(l4_utcb_t *utcb, L4::Ipc_svr::Reply_mode)
setup_wait() used the server loop (L4::Server)
Definition: br_manager:143
Br_manager()
Make a buffer-register (BR) manager.
Definition: br_manager:43
int alloc_buffer_demand(Demand const &d)
Tells the server to allocate buffers for the given demand.
Definition: br_manager:64
L4::Cap< void > get_rcv_cap(int i) const
Get capability slot allocated to the given receive buffer.
Definition: br_manager:95
int add_timeout(L4::Ipc_svr::Timeout *, l4_kernel_clock_t)
No timeouts handled by us.
Definition: br_manager:135
L4::Cap< void > alloc() noexcept
Allocate a new capability slot.
bool free(L4::Cap< void > cap, l4_cap_idx_t task=L4_INVALID_CAP, unsigned unmap_flags=L4_FP_ALL_SPACES) noexcept
Free the capability.
l4_cap_idx_t cap() const noexcept
Return capability selector.
Definition: capability.h:52
C++ interface for capabilities.
Definition: capability.h:219
A receive item for receiving a single capability.
Definition: ipc_types:269
Interface for server-loop related functions.
Definition: ipc_epiface:48
Loop hooks mixin for integrating a timeout queue into the server loop.
Callback interface for Timeout_queue.
Data type for expressing the needed receive buffers at the server-side of an interface.
Definition: __typeinfo.h:517
unsigned char mem
number of memory receive buffers.
Definition: __typeinfo.h:526
unsigned char caps
number of capability receive buffers.
Definition: __typeinfo.h:524
unsigned char ports
number of IO-port receive buffers.
Definition: __typeinfo.h:527
l4_kernel_info_t * l4re_kip(void) L4_NOTHROW
Get Kernel Info Page.
Definition: env.h:189
Reply_mode
Reply mode for server loop.
Definition: ipc_server_loop:51
unsigned long l4_umword_t
Unsigned machine word.
Definition: l4int.h:51
l4_uint64_t l4_kernel_clock_t
Kernel clock type.
Definition: l4int.h:64
@ L4_CAP_MASK
Mask to get only the relevant bits of an l4_cap_idx_t.
Definition: consts.h:139
@ L4_ERANGE
Range error.
Definition: err.h:58
@ L4_ENOSYS
No sys.
Definition: err.h:60
@ L4_EINVAL
Invalid argument.
Definition: err.h:56
@ L4_EOK
Ok.
Definition: err.h:43
@ L4_ENOMEM
No memory.
Definition: err.h:50
@ L4_RCV_ITEM_LOCAL_ID
The receiver requests to receive a local ID instead of a mapping whenever possible.
Definition: consts.h:212
@ L4_UTCB_GENERIC_BUFFERS_SIZE
Total number of buffer registers (BRs) available.
Definition: utcb.h:50
struct l4_utcb_t l4_utcb_t
Opaque type for the UTCB.
Definition: utcb.h:67
_Cap_alloc & cap_alloc
Capability allocator.
L4Re C++ Interfaces.
Definition: cmd_control:15
Predefined server-loop hooks for a server loop using the Br_manager.
Definition: br_manager:179
Predefined server-loop hooks for a server with using the Br_manager and a timeout queue.
Definition: br_manager:191
Mix in for LOOP_HOOKS to always use compound reply and wait.
Definition: ipc_server_loop:78
Mix in for LOOP_HOOKS to use a 0 send and a infinite receive timeout.
Definition: ipc_server_loop:70
Mix in for LOOP_HOOKS to ignore IPC errors.
Definition: ipc_server_loop:62
Encapsulation of the buffer-registers block in the UTCB.
Definition: utcb.h:94
l4_umword_t br[L4_UTCB_GENERIC_BUFFERS_SIZE]
Buffer registers.
Definition: utcb.h:99
l4_umword_t bdr
Buffer descriptor.
Definition: utcb.h:96
Low-level assert implementation.
#define l4_assert(expr)
Low-level assert.
Definition: assert.h:43
l4_cpu_time_t l4_kip_clock(l4_kernel_info_t *kip) L4_NOTHROW
Return clock value from the KIP.
Definition: kip.h:142
Capability allocator.