L4Re - L4 Runtime Environment
ipc_server_loop
1 // vi:set ft=cpp: -*- Mode: C++ -*-
2 /*
3  * Copyright (C) 2015 Kernkonzept GmbH.
4  * Author(s): Alexander Warg <alexander.warg@kernkonzept.com>
5  *
6  * This file is distributed under the terms of the GNU General Public
7  * License, version 2. 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 #pragma once
19 
20 #include "ipc_epiface"
21 
22 namespace L4 {
23 
35 namespace Ipc_svr {
36 
51 {
54 };
55 
62 { static void error(l4_msgtag_t, l4_utcb_t *) {} };
63 
70 { static l4_timeout_t timeout() { return L4_IPC_SEND_TIMEOUT_0; } };
71 
78 {
79  static Reply_mode before_reply(l4_msgtag_t, l4_utcb_t *)
80  { return Reply_compound; }
81 };
82 
89 { static void setup_wait(l4_utcb_t *, Reply_mode) {} };
90 
96 template< typename HOOKS >
97 class Timed_work : public HOOKS
98 {
99 protected:
100  l4_cpu_time_t _timeout;
101 
102 public:
103  Timed_work()
104  : _timeout(HOOKS::next_timeout(HOOKS::current_time())) {}
105 
106  l4_timeout_t timeout()
107  {
109  l4_timeout_abs(_timeout, this->timeout_br()));
110  }
111 
112  void setup_wait(l4_utcb_t *utcb, Reply_mode mode)
113  {
114  if (_timeout <= this->current_time()
115  && mode == Reply_separate)
116  {
117  this->work();
118  _timeout = this->next_timeout(_timeout);
119  }
120  HOOKS::setup_wait(utcb, mode);
121  }
122 
123  Reply_mode before_reply(l4_msgtag_t, l4_utcb_t *)
124  {
125  if (_timeout <= this->current_time())
126  return Reply_separate;
127  return Reply_compound;
128  }
129 };
130 
138 template< typename R >
140 {
142  R &r;
143 
145  Direct_dispatch(R &r) : r(r) {}
146 
148  l4_msgtag_t operator () (l4_msgtag_t tag, l4_umword_t obj, l4_utcb_t *utcb)
149  { return r.dispatch(tag, obj, utcb); }
150 };
151 
159 template< typename R >
160 struct Direct_dispatch<R*>
161 {
163  R *r;
164 
166  Direct_dispatch(R *r) : r(r) {}
167 
169  l4_msgtag_t operator () (l4_msgtag_t tag, l4_umword_t obj, l4_utcb_t *utcb)
170  { return r->dispatch(tag, obj, utcb); }
171 };
172 
173 #ifdef __EXCEPTIONS
174 
183 template< typename R, typename Exc> // = L4::Runtime_error>
184 struct Exc_dispatch : private Direct_dispatch<R>
185 {
188 
192  l4_msgtag_t operator () (l4_msgtag_t tag, l4_umword_t obj, l4_utcb_t *utcb)
193  {
194  try
195  {
196  return Direct_dispatch<R>::operator () (tag, obj, utcb);
197  }
198  catch (Exc &e)
199  {
200  return l4_msgtag(e.err_no(), 0, 0, 0);
201  }
202  }
203 };
204 #endif
205 
217 {
218 public:
223  int alloc_buffer_demand(Demand const &demand)
224  {
225  if (!demand.no_demand())
226  return -L4_ENOMEM;
227  return L4_EOK;
228  }
229 
232  { return L4::Cap<void>::Invalid; }
233 
236  { return -L4_ENOMEM; }
237 
240  { return -L4_ENOSYS; }
241 
244  { return -L4_ENOSYS; }
245 
246 protected:
248  unsigned first_free_br() const
249  { return 1; }
250 
253  {
254  l4_buf_regs_t *br = l4_utcb_br_u(utcb);
255  br->bdr = 0;
256  br->br[0] = 0;
257  }
258 };
259 
269  public Ignore_errors, public Default_timeout, public Compound_reply,
271 {};
272 
273 }
274 
289 template< typename LOOP_HOOKS = Ipc_svr::Default_loop_hooks >
290 class Server :
291  public LOOP_HOOKS
292 {
293 public:
298  explicit Server(l4_utcb_t *utcb) : _utcb(utcb) {}
299 
306  template< typename DISPATCH >
307  inline L4_NORETURN void internal_loop(DISPATCH dispatch);
308 
312  template< typename R >
313  inline L4_NORETURN void loop_noexc(R r)
314  { internal_loop(Ipc_svr::Direct_dispatch<R>(r)); }
315 #ifdef __EXCEPTIONS
316 
322  template< typename EXC, typename R >
323  inline L4_NORETURN void loop(R r)
324  { internal_loop(Ipc_svr::Exc_dispatch<R, EXC>(r)); while(1) ; }
325 #endif
326 protected:
328  inline l4_msgtag_t reply_n_wait(l4_msgtag_t reply, l4_umword_t *p);
329 
330 public:
331  l4_utcb_t *_utcb;
332 };
333 
334 template< typename L >
335 inline l4_msgtag_t
337 {
338  if (reply.label() != -L4_ENOREPLY)
339  {
340  Ipc_svr::Reply_mode m = this->before_reply(reply, _utcb);
341  if (m == Ipc_svr::Reply_compound)
342  {
343  this->setup_wait(_utcb, m);
344  return l4_ipc_reply_and_wait(_utcb, reply, p, this->timeout());
345  }
346  else
347  {
348  l4_msgtag_t res = l4_ipc_send(L4_INVALID_CAP | L4_SYSF_REPLY, _utcb, reply, this->timeout());
349  if (res.has_error())
350  return res;
351  }
352  }
353  this->setup_wait(_utcb, Ipc_svr::Reply_separate);
354  return l4_ipc_wait(_utcb, p, this->timeout());
355 }
356 
357 template< typename L >
358 template< typename DISPATCH >
359 inline L4_NORETURN void
360 Server<L>::internal_loop(DISPATCH dispatch)
361 {
362  l4_msgtag_t res;
363  l4_umword_t p;
364  l4_msgtag_t r = l4_msgtag(-L4_ENOREPLY, 0, 0, 0);
365 
366  while (true)
367  {
368  res = reply_n_wait(r, &p);
369  if (res.has_error())
370  {
371  this->error(res, _utcb);
372  r = l4_msgtag(-L4_ENOREPLY, 0, 0, 0);
373  continue;
374  }
375 
376  r = dispatch(res, p, _utcb);
377  }
378 }
379 
380 } // namespace L4
l4_msgtag_t reply_n_wait(l4_msgtag_t reply, l4_umword_t *p)
Internal implementation for reply and wait.
Mix in for LOOP_HOOKS to always use compound reply and wait.
Definition: ipc_server_loop:77
l4_timeout_t l4_timeout(l4_timeout_s snd, l4_timeout_s rcv) L4_NOTHROW
Combine send and receive timeout in a timeout.
Definition: __timeout.h:221
L4_NORETURN void loop(R r)
Server loop with internal exception handling.
Data type for expressing the needed receive buffers at the server-side of an interface.
Definition: __typeinfo.h:516
R * r
stores a pointer to the registry object
Reply_mode
Reply mode for server loop.
Definition: ipc_server_loop:50
Invalid capability selector.
Definition: consts.h:141
int add_timeout(Timeout *, l4_kernel_clock_t)
Returns -L4_ENOSYS, we have no timeout queue.
Interface for server-loop related functions.
Definition: ipc_epiface:47
Server shall call reply and wait separately.
Definition: ipc_server_loop:53
L4::Cap< void > get_rcv_cap(int) const
Returns L4::Cap<void>::Invalid, we have no buffer management.
L4 low-level kernel interface.
No reply.
Definition: err.h:65
struct l4_utcb_t l4_utcb_t
Opaque type for the UTCB.
Definition: utcb.h:67
bool no_demand() const
Definition: __typeinfo.h:542
Encapsulation of the buffer-registers block in the UTCB.
Definition: utcb.h:93
Callback interface for Timeout_queue.
Timeout pair.
Definition: __timeout.h:57
long label() const
Get the protocol value.
Definition: types.h:164
Reply flag.
Definition: consts.h:89
L4_NORETURN void loop_noexc(R r)
Server loop without exception handling.
Basic server loop for handling client requests.
l4_timeout_s l4_timeout_abs(l4_kernel_clock_t pint, int br) L4_NOTHROW
Set an absolute timeout.
Definition: utcb.h:383
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
#define L4_NORETURN
Noreturn function attribute.
Definition: compiler.h:202
int alloc_buffer_demand(Demand const &demand)
Tells the server to allocate buffers for the given demand.
unsigned has_error() const
Test if flags indicate an error.
Definition: types.h:191
Ok.
Definition: err.h:43
l4_uint64_t l4_cpu_time_t
CPU clock type.
Definition: l4int.h:59
unsigned long l4_umword_t
Unsigned machine word.
Definition: l4int.h:52
l4_uint64_t l4_kernel_clock_t
Kernel clock type.
Definition: l4int.h:65
Empty implementation of Server_iface.
Exc_dispatch(R r)
Make en exception handling dispatcher.
void setup_wait(l4_utcb_t *utcb, L4::Ipc_svr::Reply_mode)
Setup wait function for the server loop (Server<>).
No memory.
Definition: err.h:50
l4_msgtag_t l4_ipc_reply_and_wait(l4_utcb_t *utcb, l4_msgtag_t tag, l4_umword_t *label, l4_timeout_t timeout) L4_NOTHROW
Reply and wait operation (uses the reply capability).
Definition: ipc.h:453
l4_msgtag_t operator()(l4_msgtag_t tag, l4_umword_t obj, l4_utcb_t *utcb)
call operator forwarding to r.dispatch()
l4_msgtag_t l4_msgtag(long label, unsigned words, unsigned items, unsigned flags) L4_NOTHROW
Create a message tag from the specified values.
Definition: types.h:408
l4_msgtag_t l4_ipc_send(l4_cap_idx_t dest, l4_utcb_t *utcb, l4_msgtag_t tag, l4_timeout_t timeout) L4_NOTHROW
Send a message to an object (do not wait for a reply).
Definition: ipc.h:470
Direct disptach helper, for forwarding dispatch calls a registry R.
#define L4_IPC_SEND_TIMEOUT_0
0 send timeout
Definition: __timeout.h:82
Direct_dispatch(R &r)
Make a direct dispatcher.
int remove_timeout(Timeout *)
Returns -L4_ENOSYS, we have no timeout queue.
l4_msgtag_t l4_ipc_wait(l4_utcb_t *utcb, l4_umword_t *label, l4_timeout_t timeout) L4_NOTHROW
Wait for an incoming message from any possible sender.
Definition: ipc.h:478
Direct_dispatch(R *r)
Make a direct dispatcher.
No sys.
Definition: err.h:60
#define L4_IPC_TIMEOUT_0
Timeout constants.
Definition: __timeout.h:77
C++ interface for capabilities.
Definition: capability.h:13
int realloc_rcv_cap(int)
Returns -L4_ENOMEM, we have no buffer management.
R & r
stores a reference to the registry object
Message tag data structure.
Definition: types.h:159
unsigned first_free_br() const
Returns 1 as first free buffer.
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
Server(l4_utcb_t *utcb)
Initializes the server loop.
Dispatch helper wrapping try {} catch {} around the dispatch call.
Mix in for LOOP_HOOKS for setup_wait no op.
Definition: ipc_server_loop:88
Server shall use a compound reply and wait (fast).
Definition: ipc_server_loop:52
L4_NORETURN void internal_loop(DISPATCH dispatch)
The server loop.