]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/l4sys/include/ipc.h
update
[l4.git] / l4 / pkg / l4sys / include / ipc.h
1 /**
2  * \file
3  * \brief Common IPC interface.
4  * \ingroup l4_api
5  */
6 /*
7  * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
8  *               Alexander Warg <warg@os.inf.tu-dresden.de>,
9  *               Björn Döbel <doebel@os.inf.tu-dresden.de>,
10  *               Torsten Frenzel <frenzel@os.inf.tu-dresden.de>
11  *     economic rights: Technische Universität Dresden (Germany)
12  *
13  * This file is part of TUD:OS and distributed under the terms of the
14  * GNU General Public License 2.
15  * Please see the COPYING-GPL-2 file for details.
16  *
17  * As a special exception, you may use this file as part of a free software
18  * library without restriction.  Specifically, if other files instantiate
19  * templates or use macros or inline functions from this file, or you compile
20  * this file and link it with other files to produce an executable, this
21  * file does not by itself cause the resulting executable to be covered by
22  * the GNU General Public License.  This exception does not however
23  * invalidate any other reasons why the executable file might be covered by
24  * the GNU General Public License.
25  */
26 #ifndef __L4SYS__INCLUDE__L4API_FIASCO__IPC_H__
27 #define __L4SYS__INCLUDE__L4API_FIASCO__IPC_H__
28
29 #include <l4/sys/types.h>
30 #include <l4/sys/utcb.h>
31 #include <l4/sys/err.h>
32
33 /**
34  * \defgroup l4_ipc_api Object Invocation
35  * \ingroup l4_api
36  * \brief API for L4 object invocation.
37  *
38  * <c>\#include <l4/sys/ipc.h></c>
39  *
40  * General abstractions for L4 object invocation. The basic principle is that
41  * all objects are denoted by a capability that is accessed via a capability
42  * selector (see \link l4_cap_api Capabilities \endlink).
43  *
44  * This set of functions is common to all kinds of objects provided by the
45  * L4 micro kernel. The concrete semantics of an invocation depends on the
46  * object that shall be invoked.
47  *
48  * Objects may be invoked in various ways, the most common way is to use
49  * a \em call operation (l4_ipc_call()). However, there are a lot more
50  * flavours available that have a semantics depending on the object.
51  *
52  * \see \ref l4_kernel_object_gate_api
53  *
54  */
55
56 /*****************************************************************************
57  *** IPC result checking
58  *****************************************************************************/
59
60 /**
61  * \defgroup l4_ipc_err_api Error Handling
62  * \ingroup l4_ipc_api
63  * \brief Error handling for L4 object invocation.
64  *
65  * <c>\#include <l4/sys/ipc.h></c>
66  */
67
68 /**
69  * \brief Error codes in the \em error TCR.
70  * \ingroup l4_ipc_err_api
71  *
72  * The error codes are accessible via the \em error TCR, see
73  * #l4_thread_regs_t.error.
74  */
75 enum l4_ipc_tcr_error_t
76 {
77   L4_IPC_ERROR_MASK       = 0x1F, /**< Mask for error bits. */
78   L4_IPC_SND_ERR_MASK     = 0x01, /**< Send error mask. */
79
80   L4_IPC_ENOT_EXISTENT    = 0x04, /**< Non-existing destination or source.
81                                    **  \ingroup l4_ipc_api
82                                    **/
83   L4_IPC_RETIMEOUT        = 0x03, /**< Timeout during receive operation.
84                                    **  \ingroup l4_ipc_api
85                                    **/
86   L4_IPC_SETIMEOUT        = 0x02, /**< Timeout during send operation.
87                                    **  \ingroup l4_ipc_api
88                                    **/
89   L4_IPC_RECANCELED       = 0x07, /**< Receive operation canceled.
90                                    **  \ingroup l4_ipc_api
91                                    **/
92   L4_IPC_SECANCELED       = 0x06, /**< Send operation canceled.
93                                    **  \ingroup l4_ipc_api
94                                    **/
95   L4_IPC_REMAPFAILED      = 0x11, /**< Map flexpage failed in receive
96                                    **  operation.
97                                    **  \ingroup l4_ipc_api
98                                    **/
99   L4_IPC_SEMAPFAILED      = 0x10, /**< Map flexpage failed in send operation.
100                                    **  \ingroup l4_ipc_api
101                                    **/
102   L4_IPC_RESNDPFTO        = 0x0b, /**< Send-pagefault timeout in receive
103                                    **  operation.
104                                    **  \ingroup l4_ipc_api
105                                    **/
106   L4_IPC_SESNDPFTO        = 0x0a, /**< Send-pagefault timeout in send
107                                    **  operation.
108                                    **  \ingroup l4_ipc_api
109                                    **/
110   L4_IPC_RERCVPFTO        = 0x0d, /**< Receive-pagefault timeout in receive
111                                    **  operation.
112                                    **  \ingroup l4_ipc_api
113                                    **/
114   L4_IPC_SERCVPFTO        = 0x0c, /**< Receive-pagefault timeout in send
115                                    **  operation.
116                                    **  \ingroup l4_ipc_api
117                                    **/
118   L4_IPC_REABORTED        = 0x0f, /**< Receive operation aborted.
119                                    **  \ingroup l4_ipc_api
120                                    **/
121   L4_IPC_SEABORTED        = 0x0e, /**< Send operation aborted.
122                                    **  \ingroup l4_ipc_api
123                                    **/
124   L4_IPC_REMSGCUT         = 0x09, /**< Cut receive message, due to
125                                    **  message buffer is too small.
126                                    **  \ingroup l4_ipc_api
127                                    **/
128   L4_IPC_SEMSGCUT         = 0x08, /**< Cut send message. due to
129                                    **  message buffer is too small,
130                                    **  \ingroup l4_ipc_api
131                                    **/
132 };
133
134
135 /**
136  * \brief Get the error code for an object invocation.
137  * \ingroup l4_ipc_err_api
138  *
139  * \param tag   Return value of the invocation.
140  * \param utcb  UTCB that was used for the invocation.
141  *
142  * \return 0 if no error condition is set,
143  *         error code otherwise (see #l4_ipc_tcr_error_t).
144  */
145 L4_INLINE l4_umword_t
146 l4_ipc_error(l4_msgtag_t tag, l4_utcb_t *utcb) L4_NOTHROW;
147
148
149 /**
150  * \brief Return error code of a system call return message tag.
151  * \ingroup l4_ipc_err_api
152  * \param tag   System call return message type
153  * \return 0 for no error, error number in case of error
154  */
155 L4_INLINE long
156 l4_error(l4_msgtag_t tag) L4_NOTHROW;
157
158 L4_INLINE long
159 l4_error_u(l4_msgtag_t tag, l4_utcb_t *utcb) L4_NOTHROW;
160
161 /*****************************************************************************
162  *** IPC results
163  *****************************************************************************/
164
165 /**
166  * \brief Returns whether an error occurred in send phase of an invocation.
167  * \ingroup l4_ipc_err_api
168  *
169  * \pre l4_msgtag_has_error(tag) == true
170  * \param utcb   UTCB to check.
171  *
172  * \return Boolean value.
173  */
174 L4_INLINE int l4_ipc_is_snd_error(l4_utcb_t *utcb) L4_NOTHROW;
175
176 /**
177  * \brief Returns whether an error occurred in receive phase of an invocation.
178  * \ingroup l4_ipc_err_api
179  *
180  * \pre l4_msgtag_has_error(tag) == true
181  * \param utcb   UTCB to check.
182  *
183  * \return Boolean value.
184  */
185 L4_INLINE int l4_ipc_is_rcv_error(l4_utcb_t *utcb) L4_NOTHROW;
186
187 /**
188  * \brief Get the error condition of the last invocation from the TCR.
189  * \ingroup l4_ipc_err_api
190  *
191  * \pre l4_msgtag_has_error(tag) == true
192  * \param utcb   UTCB to check.
193  *
194  * \return Error condition of type l4_ipc_tcr_error_t.
195  */
196 L4_INLINE int l4_ipc_error_code(l4_utcb_t *utcb) L4_NOTHROW;
197
198
199 /*****************************************************************************
200  *** IPC calls
201  *****************************************************************************/
202
203 /**
204  * \brief Send a message to an object (do \b not wait for a reply).
205  * \ingroup l4_ipc_api
206  *
207  * \param dest    Capability selector for the destination object.
208  * \param utcb    UTCB of the caller.
209  * \param tag     Descriptor for the message to be sent.
210  * \param timeout Timeout pair (see #l4_timeout_t) only send part is relevant.
211  *
212  * \return  result tag
213  *
214  * A message is sent to the destination object. There is no receive phase
215  * included. The invoker continues working after sending the message.
216  *
217  * \attention This is a special-purpose message transfer, objects usually
218  *            support only invocation via l4_ipc_call().
219  */
220 L4_INLINE l4_msgtag_t
221 l4_ipc_send(l4_cap_idx_t dest, l4_utcb_t *utcb, l4_msgtag_t tag,
222             l4_timeout_t timeout) L4_NOTHROW;
223
224
225 /**
226  * \brief Wait for an incoming message from any possible sender.
227  * \ingroup l4_ipc_api
228  *
229  * \param   utcb    UTCB of the caller.
230  * \retval  label   Label assigned to the source object (IPC gate or IRQ).
231  * \param   timeout Timeout pair (see #l4_timeout_t, only the receive part is
232  *          used).
233  *
234  * \return  return tag
235  *
236  * This operation does an open wait, and therefore needs no capability to
237  * denote the possible source of a message. This means the calling thread
238  * waits for an incoming message from any possible source.
239  * There is no send phase included in this operation.
240  *
241  * The usual usage of this function is to call that function when entering a
242  * server loop in a user-level server that implements user-level objects,
243  * see also #l4_ipc_reply_and_wait().
244  */
245 L4_INLINE l4_msgtag_t
246 l4_ipc_wait(l4_utcb_t *utcb, l4_umword_t *label,
247             l4_timeout_t timeout) L4_NOTHROW;
248
249
250 /**
251  * \brief Wait for a message from a specific source.
252  * \ingroup l4_ipc_api
253  *
254  * \param   object  Object to receive a message from.
255  * \param   timeout Timeout pair (see #l4_timeout_t, only the receive part
256  *                  matters).
257  * \param   utcb    UTCB of the caller.
258  *
259  * \return  result tag.
260  *
261  * This operation waits for a message from the specified object. Messages from
262  * other sources are not accepted by this operation. The operation does not
263  * include a send phase, this means no message is sent to the object.
264  *
265  * \note This operation is usually used to receive messages from a specific IRQ
266  *       or thread. However, it is not common to use this operation for normal
267  *       applications.
268  */
269 L4_INLINE l4_msgtag_t
270 l4_ipc_receive(l4_cap_idx_t object, l4_utcb_t *utcb,
271                l4_timeout_t timeout) L4_NOTHROW;
272
273 /**
274  * \brief Object call (usual invocation).
275  * \ingroup l4_ipc_api
276  *
277  * \param object   Capability selector for the object to call.
278  * \param utcb     UTCB of the caller.
279  * \param tag      Message tag to describe the message to be sent.
280  * \param timeout  Timeout pair for send an receive phase (see #l4_timeout_t).
281  *
282  * \return  result tag
283  *
284  * A message is sent to the object and the invoker waits for a
285  * reply from the object. Messages from other sources are not accepted.
286  * \note The send-to-receive transition needs no time, the object can reply
287  *       with a send timeout of zero.
288  */
289 L4_INLINE l4_msgtag_t
290 l4_ipc_call(l4_cap_idx_t object, l4_utcb_t *utcb, l4_msgtag_t tag,
291             l4_timeout_t timeout) L4_NOTHROW;
292
293
294 /**
295  * \brief Reply and wait operation (uses the \em reply capability).
296  * \ingroup l4_ipc_api
297  *
298  * \param   tag     Describes the message to be sent as reply.
299  * \param   utcb    UTCB of the caller.
300  * \retval  label   Label assigned to the source object of the received message.
301  * \param   timeout Timeout pair (see #l4_timeout_t).
302  * \return  result tag
303  *
304  * A message is sent to the previous caller using the implicit reply
305  * capability. Afterwards the invoking thread waits for a message from any
306  * source.
307  * \note This is the standard server operation: it sends a reply to the actual
308  *       client and waits for the next incoming request, which may come from
309  *       any other client.
310  */
311 L4_INLINE l4_msgtag_t
312 l4_ipc_reply_and_wait(l4_utcb_t *utcb, l4_msgtag_t tag,
313                       l4_umword_t *label, l4_timeout_t timeout) L4_NOTHROW;
314
315 /**
316  * \brief Send a message and do an open wait.
317  * \ingroup l4_ipc_api
318  *
319  * \param   dest    Object to send a message to.
320  * \param   utcb    UTCB of the caller.
321  * \param   tag     Describes the message that shall be sent.
322  * \retval  label   Label assigned to the source object of the receive phase.
323  * \param   timeout Timeout pair (see #l4_timeout_t).
324  * \return  result tag
325  *
326  * A message is sent to the destination object and the invoking thread waits
327  * for a reply from any source.
328  *
329  * \note This is a special-purpose operation and shall not be used in general
330  *       applications.
331  */
332 L4_INLINE l4_msgtag_t
333 l4_ipc_send_and_wait(l4_cap_idx_t dest, l4_utcb_t *utcb, l4_msgtag_t tag,
334                      l4_umword_t *label, l4_timeout_t timeout) L4_NOTHROW;
335
336 /**
337  * \defgroup l4_ipc_rt_api Realtime API
338  * \ingroup l4_ipc_api
339  * \internal
340  */
341
342 #if 0
343 /**
344  * Wait for next period.
345  * \ingroup l4_ipc_rt_api
346  *
347  * \param utcb    UTCB of the caller.
348  * \param label   Label
349  * \param timeout IPC timeout (see #l4_ipc_timeout).
350  *
351  * \return result tag
352  */
353 L4_INLINE l4_msgtag_t
354 l4_ipc_wait_next_period(l4_utcb_t *utcb,
355                         l4_umword_t *label,
356                         l4_timeout_t timeout);
357
358 #endif
359
360 /**
361  * \brief Generic L4 object invocation.
362  * \ingroup l4_ipc_api
363  *
364  * \param dest    Destination object.
365  * \param utcb    UTCB of the caller.
366  * \param flags   Invocation flags (see #l4_syscall_flags_t).
367  * \param slabel  Send label if applicable (may be seen by the receiver).
368  * \param tag     Sending message tag.
369  * \retval rlabel Receiving label.
370  * \param timeout Timeout pair (see #l4_timeout_t).
371  *
372  * \return return tag
373  */
374 L4_INLINE l4_msgtag_t
375 l4_ipc(l4_cap_idx_t dest,
376        l4_utcb_t *utcb,
377        l4_umword_t flags,
378        l4_umword_t slabel,
379        l4_msgtag_t tag,
380        l4_umword_t *rlabel,
381        l4_timeout_t timeout) L4_NOTHROW;
382
383 /**
384  * \brief Sleep for an amount of time.
385  * \ingroup l4_ipc_api
386  *
387  * \param   timeout Timeout pair (see #l4_timeout_t, the receive part matters).
388  *
389  * \return  error code:
390  *          - #L4_IPC_RETIMEOUT: success
391  *          - #L4_IPC_RECANCELED woken up by a different thread
392  *            (l4_thread_ex_regs()).
393  *
394  * The invoking thread waits until the timeout
395  * is expired or the wait was aborted by another thread by l4_thread_ex_regs().
396  */
397 L4_INLINE l4_msgtag_t
398 l4_ipc_sleep(l4_timeout_t timeout) L4_NOTHROW;
399
400 /**
401  * \brief Add a flex-page to be sent to the UTCB
402  * \ingroup l4_ipc_api
403  *
404  * \param  snd_fpage  Flex-page.
405  * \param  snd_base   Send base.
406  * \param  tag        Tag to be modified.
407  * \retval tag        Modified tag, the number of items will be increased,
408  *                    all other values in the tag will be retained.
409  *
410  * \return 0 on success, negative error code otherwise
411  */
412 L4_INLINE int
413 l4_sndfpage_add(l4_fpage_t const snd_fpage, unsigned long snd_base,
414                 l4_msgtag_t *tag) L4_NOTHROW;
415
416 /*
417  * \internal
418  * \ingroup l4_ipc_api
419  */
420 L4_INLINE int
421 l4_sndfpage_add_u(l4_fpage_t const snd_fpage, unsigned long snd_base,
422                   l4_msgtag_t *tag, l4_utcb_t *utcb) L4_NOTHROW;
423
424
425 /************************************************************************
426  * Implementations
427  **********************/
428
429 L4_INLINE l4_umword_t
430 l4_ipc_error(l4_msgtag_t tag, l4_utcb_t *utcb) L4_NOTHROW
431 {
432   if (!l4_msgtag_has_error(tag))
433     return 0;
434   return l4_utcb_tcr_u(utcb)->error & L4_IPC_ERROR_MASK;
435 }
436
437 L4_INLINE long
438 l4_error_u(l4_msgtag_t tag, l4_utcb_t *u) L4_NOTHROW
439 {
440   if (l4_msgtag_has_error(tag))
441     return -(L4_EIPC_LO + (l4_utcb_tcr_u(u)->error & L4_IPC_ERROR_MASK));
442
443   return l4_msgtag_label(tag);
444 }
445
446 L4_INLINE long
447 l4_error(l4_msgtag_t tag) L4_NOTHROW
448 {
449   return l4_error_u(tag, l4_utcb());
450 }
451
452
453 L4_INLINE int l4_ipc_is_snd_error(l4_utcb_t *u) L4_NOTHROW
454 { return !(l4_utcb_tcr_u(u)->error & 1) == 0; }
455
456 L4_INLINE int l4_ipc_is_rcv_error(l4_utcb_t *u) L4_NOTHROW
457 { return l4_utcb_tcr_u(u)->error & 1; }
458
459 L4_INLINE int l4_ipc_error_code(l4_utcb_t *u) L4_NOTHROW
460 { return l4_utcb_tcr_u(u)->error & L4_IPC_ERROR_MASK; }
461
462
463 /*
464  * \internal
465  * \ingroup l4_ipc_api
466  */
467 L4_INLINE int
468 l4_sndfpage_add_u(l4_fpage_t const snd_fpage, unsigned long snd_base,
469                   l4_msgtag_t *tag, l4_utcb_t *utcb) L4_NOTHROW
470 {
471   l4_msg_regs_t *v = l4_utcb_mr_u(utcb);
472   int i = l4_msgtag_words(*tag) + 2 * l4_msgtag_items(*tag);
473
474   if (i >= L4_UTCB_GENERIC_DATA_SIZE - 1)
475     return -L4_ENOMEM;
476
477   v->mr[i]     = snd_base | L4_ITEM_MAP | L4_ITEM_CONT;
478   v->mr[i + 1] = snd_fpage.raw;
479
480   *tag = l4_msgtag(l4_msgtag_label(*tag), l4_msgtag_words(*tag),
481                    l4_msgtag_items(*tag) + 1, l4_msgtag_flags(*tag));
482   return 0;
483 }
484
485 L4_INLINE int
486 l4_sndfpage_add(l4_fpage_t const snd_fpage, unsigned long snd_base,
487                 l4_msgtag_t *tag) L4_NOTHROW
488 {
489   return l4_sndfpage_add_u(snd_fpage, snd_base, tag, l4_utcb());
490 }
491
492
493 #endif /* ! __L4SYS__INCLUDE__L4API_FIASCO__IPC_H__ */