L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
ipc_epiface
1// vi:set ft=cpp: -*- Mode: C++ -*-
2/*
3 * (c) 2014-2015 Alexander Warg <alexander.warg@kernkonzept.com>
4 *
5 * License: see LICENSE.spdx (in this directory or the directories above)
6 */
7#pragma once
8#pragma GCC system_header
9
10#include "capability.h"
11#include "ipc_server"
12#include "ipc_string"
13#include <l4/sys/types.h>
14#include <l4/sys/utcb.h>
15#include <l4/sys/__typeinfo.h>
16#include <l4/sys/meta>
17#include <l4/cxx/result>
18#include <l4/cxx/type_traits>
19
20namespace L4 {
21
22// forward for Irqep_t
23class Irq;
24class Rcv_endpoint;
25
26namespace Ipc_svr {
27
28class Timeout;
29
37class Server_iface
38{
39private:
40 Server_iface(Server_iface const &);
41 Server_iface const &operator = (Server_iface const &);
42
43public:
50 class Mem_window final
51 {
52 public:
53 class Allocator
54 {
55 public:
56 virtual ~Allocator() = default;
57
58 virtual void dispose(l4_fpage_t fp) noexcept = 0;
59 };
60
61 Mem_window() noexcept = default;
62 Mem_window(l4_fpage_t fp, Allocator *allocator) noexcept
63 : _fp(fp), _allocator(allocator)
64 {}
65
66 ~Mem_window()
67 { reset(); }
68
69 Mem_window(Mem_window const &) = delete;
70 Mem_window &operator=(Mem_window const &) = delete;
71
72 Mem_window(Mem_window &&other) noexcept
73 : _fp(other._fp), _allocator(other._allocator)
74 {
75 other._fp = l4_fpage_invalid();
76 other._allocator = nullptr;
77 }
78
79 Mem_window &operator=(Mem_window &&other) noexcept
80 {
81 if (this == &other)
82 return *this;
83
84 reset();
85 _fp = other._fp;
86 _allocator = other._allocator;
87
88 other._fp = l4_fpage_invalid();
89 other._allocator = nullptr;
90
91 return *this;
92 }
93
94 void reset() noexcept
95 {
96 if (l4_is_fpage_valid(_fp))
97 {
98 _allocator->dispose(_fp);
99 _fp = l4_fpage_invalid();
100 _allocator = nullptr;
101 }
102 }
103
104 l4_fpage_t release() noexcept
105 {
106 l4_fpage_t ret = _fp;
107 _fp = l4_fpage_invalid();
108 _allocator = nullptr;
109 return ret;
110 }
111
112 explicit operator bool() const noexcept
113 { return l4_is_fpage_valid(_fp); }
114
115 l4_fpage_t fp() const noexcept
116 { return _fp; }
117
118 void *get() const noexcept
119 { return reinterpret_cast<void *>(l4_fpage_memaddr(_fp)); }
120
121 unsigned order() const noexcept
122 { return l4_fpage_size(_fp); }
123
124 l4_umword_t size() const noexcept
125 { return l4_umword_t{1} << order(); }
126
127 private:
129 Allocator *_allocator = nullptr;
130 };
131
134
135 Server_iface(Server_iface &&) = delete;
136 Server_iface &operator = (Server_iface &&) = delete;
137
140
141 // Destroy the server interface
142 virtual ~Server_iface() = 0;
143
153 virtual int alloc_buffer_demand(Demand const &demand) = 0;
154
163 virtual L4::Cap<void> get_rcv_cap(int index) const = 0;
164
173 virtual int realloc_rcv_cap(int index) = 0;
174
182 { return cxx::Error(-L4_ENOSYS); }
183
196 { return cxx::Error(-L4_ENOSYS); }
197
205 virtual int add_timeout(Timeout *timeout, l4_kernel_clock_t time) = 0;
206
212 virtual int remove_timeout(Timeout *timeout) = 0;
213
225 template<typename T>
226 L4::Cap<T> rcv_cap(int index) const
227 { return L4::cap_cast<T>(get_rcv_cap(index)); }
228
238 L4::Cap<void> rcv_cap(int index) const
239 { return get_rcv_cap(index); }
240};
241
242inline Server_iface::~Server_iface() {}
243
244} // namespace Ipc_svr
245
257struct Epiface
258{
259 Epiface(Epiface const &) = delete;
260 Epiface &operator = (Epiface const &) = delete;
261
266
267 class Stored_cap : public Cap<void>
268 {
269 private:
270 enum { Managed = 0x10 };
271
272 public:
273 Stored_cap() = default;
274 Stored_cap(Cap<void> const &c, bool managed = false)
275 : Cap<void>((c.cap() & L4_CAP_MASK) | (managed ? Managed : 0))
276 {
277 static_assert (!(L4_CAP_MASK & Managed), "conflicting bits used...");
278 }
279
280 bool managed() const { return cap() & Managed; }
281 };
282
284 Epiface() : _data(0) {}
285
298 virtual l4_msgtag_t dispatch(l4_msgtag_t tag, unsigned rights,
299 l4_utcb_t *utcb) = 0;
300
307 virtual Demand get_buffer_demand() const = 0; //{ return Demand(0); }
308
310 virtual ~Epiface() = 0;
311
318 Stored_cap obj_cap() const { return _cap; }
319
325 Server_iface *server_iface() const { return _data; }
326
336 int set_server(Server_iface *srv, Cap<void> cap, bool managed = false)
337 {
338 if ((srv && cap) || (!srv && !cap))
339 {
340 _data = srv;
341 _cap = Stored_cap(cap, managed);
342 return 0;
343 }
344
345 return -L4_EINVAL;
346 }
347
351 void set_obj_cap(Cap<void> const &cap) { _cap = cap; }
352
353private:
354 Server_iface *_data;
355 Stored_cap _cap;
356};
357
359
367template<typename RPC_IFACE, typename BASE = Epiface>
368struct Epiface_t0 : BASE
369{
371 using Interface = RPC_IFACE;
372
375 { return typename Kobject_typeid<RPC_IFACE>::Demand(); }
376
382 { return L4::cap_cast<RPC_IFACE>(BASE::obj_cap()); }
383};
384
392template<typename Derived, typename BASE = Epiface,
393 bool = cxx::is_polymorphic<BASE>::value>
394struct Irqep_t : Epiface_t0<void, BASE>
395{
397 {
398 static_cast<Derived*>(this)->handle_irq();
399 return l4_msgtag(-L4_ENOREPLY, 0, 0, 0);
400 }
401
407 { return L4::cap_cast<L4::Irq>(BASE::obj_cap()); }
408};
409
410template<typename Derived, typename BASE>
411struct Irqep_t<Derived, BASE, false> : Epiface_t0<void, BASE>
412{
414 {
415 static_cast<Derived*>(this)->handle_irq();
416 return l4_msgtag(-L4_ENOREPLY, 0, 0, 0);
417 }
418
423 Cap<L4::Irq> obj_cap() const
424 { return L4::cap_cast<L4::Irq>(BASE::obj_cap()); }
425};
426
435{
436public:
437 virtual ~Registry_iface() = 0;
438
451 virtual L4::Cap<void>
452 register_obj(L4::Epiface *o, char const *service) = 0;
453
468 virtual L4::Cap<void>
470
485
500
513 virtual void
514 unregister_obj(L4::Epiface *o, bool unmap = true) = 0;
515};
516
517inline Registry_iface::~Registry_iface() {}
518
519namespace Ipc {
520namespace Detail {
521
522using namespace L4::Typeid;
523
524template<typename IFACE>
525struct Meta_svr
526{
527 l4_ret_t op_num_interfaces(L4::Meta::Rights)
528 {
529 l4_ret_t ret = 0;
530 auto iter = [&ret](L4::Type_info const *)
531 {
532 ++ret;
533 return false;
534 };
535 _iter_interfaces(iter);
536 return ret;
537 }
538
539 l4_ret_t op_interface(L4::Meta::Rights, l4_umword_t idx, long &proto,
540 L4::Ipc::String<char> &name)
541 {
542 auto iter =
543 [idx, &proto, &name, num = 0U](L4::Type_info const *t) mutable
544 {
545 if (idx == num)
546 {
547 proto = t->proto();
548 if (auto *n = t->name())
549 name.copy_in(n);
550 else
551 name.copy_in("");
552
553 return true;
554 }
555
556 ++num;
557 return false;
558 };
559
560 return _iter_interfaces(iter) ? 0 : -L4_ERANGE;
561 }
562
563 l4_ret_t op_supports(L4::Meta::Rights, l4_mword_t proto)
564 { return L4::kobject_typeid<IFACE>()->has_proto(proto); }
565
566private:
567 template<typename F>
568 static bool _iter_interfaces(F &f)
569 { return _iter_interfaces<F>(L4::kobject_typeid<IFACE>(), f); }
570
571 template<typename F>
572 static bool _iter_interfaces(L4::Type_info const *t, F &f)
573 {
574 // Don't report L4::Kobject (no base) or L4::PROTO_EMPTY. The latter
575 // interfaces are not dispatched and shall remain invisible.
576 if (t->num_bases() && t->proto() != L4::PROTO_EMPTY)
577 if (f(t))
578 return true;
579
580 for (unsigned i = 0; i < t->num_bases(); ++i)
581 if (_iter_interfaces(t->base(i), f))
582 return true;
583
584 return false;
585 }
586};
587
588template<typename IFACE, typename LIST>
589struct _Dispatch;
590
591// No match dispatcher found
592template<typename IFACE>
593struct _Dispatch<IFACE, Iface_list_end>
594{
595 template< typename THIS, typename A1, typename A2 >
596 static l4_msgtag_t f(THIS *, l4_msgtag_t, A1, A2 &)
597 { return l4_msgtag(-L4_EBADPROTO, 0, 0, 0); }
598};
599
600// call matching p_dispatch() function
601template<typename IFACE, typename I, typename LIST >
602struct _Dispatch<IFACE, Iface_list<I, LIST> >
603{
604 // special handling for the meta protocol, to avoid 'using' murx
605 template< typename THIS >
606 static l4_msgtag_t _f(THIS *, l4_msgtag_t tag, unsigned r,
607 l4_utcb_t *utcb, True::type)
608 {
609 using L4::Ipc::Msg::dispatch_call;
610 using Meta = L4::Meta::Rpcs;
611 using Msvr = Meta_svr<IFACE>;
612 return dispatch_call<Meta>(static_cast<Msvr *>(nullptr), utcb, tag, r);
613 }
614
615 // normal dispatch to the op_<func> methods of \a self.
616 template< typename THIS >
617 static l4_msgtag_t _f(THIS *self, l4_msgtag_t t, unsigned r,
618 l4_utcb_t *utcb, False::type)
619 {
620 using L4::Ipc::Msg::dispatch_call;
621 return dispatch_call<typename I::iface_type::Rpcs>(self, utcb, t, r);
622 }
623
624 // dispatch function with switch for meta protocol
625 template< typename THIS >
626 static l4_msgtag_t f(THIS *self, l4_msgtag_t tag, unsigned r,
627 l4_utcb_t *utcb)
628 {
629 if (I::Proto == tag.label())
630 return _f(self, tag, r, utcb,
631 Bool<I::Proto == static_cast<long>(L4_PROTO_META)>());
632
633 return _Dispatch<IFACE, typename LIST::type>::f(self, tag, r, utcb);
634 }
635};
636
637template<typename IFACE>
638struct Dispatch :
639 _Dispatch<IFACE, typename L4::Kobject_typeid<IFACE>::Iface_list::type>
640{};
641
642} // namespace Detail
643
644template<typename EPIFACE>
645struct Dispatch : Detail::Dispatch<typename EPIFACE::Interface>
646{};
647
648} // namespace Ipc
649
656template<typename Derived, typename IFACE, typename BASE = L4::Epiface,
657 bool = cxx::is_polymorphic<BASE>::value>
658struct Epiface_t : Epiface_t0<IFACE, BASE>
659{
661 dispatch(l4_msgtag_t tag, unsigned rights, l4_utcb_t *utcb) final
662 {
663 using Dispatch = Ipc::Dispatch<Derived>;
664 return Dispatch::f(static_cast<Derived*>(this), tag, rights, utcb);
665 }
666};
667
668template<typename Derived, typename IFACE, typename BASE>
669struct Epiface_t<Derived, IFACE, BASE, false> : Epiface_t0<IFACE, BASE>
670{
672 dispatch(l4_msgtag_t tag, unsigned rights, l4_utcb_t *utcb)
673 {
674 using Dispatch = Ipc::Dispatch<Derived>;
675 return Dispatch::f(static_cast<Derived*>(this), tag, rights, utcb);
676 }
677};
678
685{
686public:
687 using Value = Epiface;
693 static Value *find(l4_umword_t label)
694 { return reinterpret_cast<Value*>(label & ~3UL); }
695
710 l4_utcb_t *utcb)
711 {
712 return find(label)->dispatch(tag, label, utcb);
713 }
714};
715
716
717} // namespace L4
Type information handling.
This registry returns the corresponding server object based on the label of an Ipc_gate.
Definition ipc_epiface:685
static l4_msgtag_t dispatch(l4_msgtag_t tag, l4_umword_t label, l4_utcb_t *utcb)
The dispatch function called by the server loop.
Definition ipc_epiface:709
static Value * find(l4_umword_t label)
Get the server object for an Ipc_gate label.
Definition ipc_epiface:693
l4_cap_idx_t cap() const noexcept
Return capability selector.
Definition capability.h:49
C++ interface for capabilities.
Definition capability.h:224
Interface for server-loop related functions.
Definition ipc_epiface:38
virtual cxx::Result< Mem_window > get_rcv_mem() noexcept
Take the current memory receive window.
Definition ipc_epiface:195
virtual int realloc_rcv_cap(int index)=0
Allocate a new capability for the given receive buffer.
virtual cxx::Result< L4::Reply_cap > take_reply_cap() noexcept
Take the currently used reply capability.
Definition ipc_epiface:181
virtual int add_timeout(Timeout *timeout, l4_kernel_clock_t time)=0
Add a timeout to the server internal timeout queue.
virtual int remove_timeout(Timeout *timeout)=0
Remove the given timeout from the timer queue.
Server_iface()
Make a server interface.
Definition ipc_epiface:139
L4::Type_info::Demand Demand
Data type expressing server-side demand for receive buffers.
Definition ipc_epiface:133
virtual L4::Cap< void > get_rcv_cap(int index) const =0
Get capability slot allocated to the given receive buffer.
L4::Cap< void > rcv_cap(int index) const
Get receive cap with the given index as generic (void) type.
Definition ipc_epiface:238
L4::Cap< T > rcv_cap(int index) const
Get given receive buffer as typed capability.
Definition ipc_epiface:226
virtual int alloc_buffer_demand(Demand const &demand)=0
Tells the server to allocate buffers for the given demand.
Callback interface for Timeout_queue.
C++ Irq interface, see IRQs for the C interface.
Definition irq:121
Interface for kernel objects that allow to receive IPC from them.
Definition rcv_endpoint:33
Abstract interface for object registries.
Definition ipc_epiface:435
virtual void unregister_obj(L4::Epiface *o, bool unmap=true)=0
Unregister the given object o from the server.
virtual L4::Cap< L4::Irq > register_irq_obj(L4::Epiface *o)=0
Register o as server-side object for asynchronous IRQs.
virtual L4::Cap< void > register_obj(L4::Epiface *o, char const *service)=0
Register an L4::Epiface for an IPC gate available in the applications environment under the name serv...
virtual L4::Cap< L4::Rcv_endpoint > register_obj(L4::Epiface *o, L4::Cap< L4::Rcv_endpoint > ep)=0
Register o as server-side object for a pre-allocated capability.
virtual L4::Cap< void > register_obj(L4::Epiface *o)=0
Register o as server-side object for synchronous RPC.
Data type for expressing the needed receive buffers at the server-side of an interface.
Definition __typeinfo.h:507
Error value.
Definition result:20
A result of a function call.
Definition result:40
unsigned long l4_umword_t
Unsigned machine word.
Definition l4int.h:40
signed long l4_mword_t
Signed machine word.
Definition l4int.h:37
l4_uint64_t l4_kernel_clock_t
Kernel clock type.
Definition l4int.h:53
#define L4_CAP_MASK
Mask to get only the relevant bits of an l4_cap_idx_t.
Definition consts.h:150
@ L4_ERANGE
Range error.
Definition err.h:49
@ L4_ENOSYS
No sys.
Definition err.h:51
@ L4_EINVAL
Invalid argument.
Definition err.h:47
@ L4_ENOREPLY
No reply.
Definition err.h:56
@ L4_EBADPROTO
Unsupported protocol.
Definition err.h:52
L4_CONSTEXPR int l4_is_fpage_valid(l4_fpage_t fp) L4_NOTHROW
Test if the flexpage is valid.
Definition __l4_fpage.h:803
L4_CONSTEXPR l4_addr_t l4_fpage_memaddr(l4_fpage_t f) L4_NOTHROW
Return the memory address from the memory flexpage.
Definition __l4_fpage.h:677
L4_CONSTEXPR unsigned l4_fpage_size(l4_fpage_t f) L4_NOTHROW
Return size (log2) from a flexpage.
Definition __l4_fpage.h:659
L4_CONSTEXPR l4_fpage_t l4_fpage_invalid(void) L4_NOTHROW
Get an invalid flexpage.
Definition __l4_fpage.h:745
Type_info const * kobject_typeid() noexcept
Get the L4::Type_info for the L4Re interface given in T.
Definition __typeinfo.h:682
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:426
@ L4_PROTO_META
Meta information protocol.
Definition types.h:77
struct l4_utcb_t l4_utcb_t
Opaque type for the UTCB.
Definition utcb.h:56
Common L4 ABI Data Types.
l4_int16_t l4_ret_t
Return value of an IPC call as well as an RPC call.
Definition types.h:28
UTCB definitions.
Helper classes for L4::Server instantiation.
Definition ipc_epiface:26
Definition of interface data-type helpers.
Definition __typeinfo.h:66
L4 low-level kernel interface.
@ PROTO_EMPTY
Empty protocol for empty APIs.
Definition __typeinfo.h:49
Cap< T > cap_cast(Cap< F > const &c) noexcept
static_cast for capabilities.
Definition capability.h:416
Epiface mixin for generic Kobject-based interfaces.
Definition ipc_epiface:369
Type_info::Demand get_buffer_demand() const
Get the server-side buffer demand based in IFACE.
Definition ipc_epiface:374
Cap< RPC_IFACE > obj_cap() const
Get the (typed) capability to this object.
Definition ipc_epiface:381
RPC_IFACE Interface
Data type of the IPC interface definition.
Definition ipc_epiface:371
Epiface implementation for Kobject-based interface implementations.
Definition ipc_epiface:659
l4_msgtag_t dispatch(l4_msgtag_t tag, unsigned rights, l4_utcb_t *utcb) final
The abstract handler for client requests to the object.
Definition ipc_epiface:661
Base class for interface implementations.
Definition ipc_epiface:258
Stored_cap obj_cap() const
Get the capability to the kernel object belonging to this object.
Definition ipc_epiface:318
Ipc_svr::Server_iface::Demand Demand
Type for server-side receive buffer demand.
Definition ipc_epiface:265
Ipc_svr::Server_iface Server_iface
Type for abstract server interface.
Definition ipc_epiface:263
virtual ~Epiface()=0
Destroy the object.
Definition ipc_epiface:358
Server_iface * server_iface() const
Get pointer to server interface at which the object is currently registered.
Definition ipc_epiface:325
virtual l4_msgtag_t dispatch(l4_msgtag_t tag, unsigned rights, l4_utcb_t *utcb)=0
The abstract handler for client requests to the object.
void set_obj_cap(Cap< void > const &cap)
Deprecated server registration function.
Definition ipc_epiface:351
virtual Demand get_buffer_demand() const =0
Get the server-side receive buffer demand for this object.
int set_server(Server_iface *srv, Cap< void > cap, bool managed=false)
Set server registration info for the object.
Definition ipc_epiface:336
Epiface()
Make a server object.
Definition ipc_epiface:284
Epiface implementation for interrupt handlers.
Definition ipc_epiface:395
Cap< L4::Irq > obj_cap() const
Get the (typed) capability to this object.
Definition ipc_epiface:406
l4_msgtag_t dispatch(l4_msgtag_t, unsigned, l4_utcb_t *) final
The abstract handler for client requests to the object.
Definition ipc_epiface:396
T::__Kobject_typeid::Demand Demand
Data type expressing the static demand of receive buffers in a server.
Definition __typeinfo.h:622
Dynamic Type Information for L4Re Interfaces.
Definition __typeinfo.h:500
Message tag data structure.
Definition types.h:266
long label() const L4_NOTHROW
Get the protocol value.
Definition types.h:270
Meta interface for getting dynamic type information about objects behind capabilities.
L4 flexpage type.
Definition __l4_fpage.h:76