L4Re Operating System Framework
Interface and Usage Documentation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ipc_varg
1// vi:set ft=cpp: -*- Mode: C++ -*-
2/*
3 * (c) 2014 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 "types"
11#include "ipc_basics"
12
13namespace L4 { namespace Ipc L4_EXPORT {
14
15template< typename T, template <typename X> class B >
16struct Generic_va_type : B<T>
17{
18 enum { Id = B<T>::Id };
19 typedef B<T> ID;
20 typedef T const &Ret_value;
21 typedef T Value;
22
23 static Ret_value value(void const *d)
24 { return *reinterpret_cast<Value const *>(d); }
25
26 static void const *addr_of(Value const &v) { return &v; }
27
28 static unsigned size(void const *) { return sizeof(T); }
29
30 static L4_varg_type unsigned_id()
31 {
32 return static_cast<L4_varg_type>(Id & ~L4_VARG_TYPE_SIGN);
33 }
34
35 static L4_varg_type signed_id()
36 {
37 return static_cast<L4_varg_type>(Id | L4_VARG_TYPE_SIGN);
38 }
39
40 static L4_varg_type id()
41 {
42 return static_cast<L4_varg_type>(Id);
43 }
44};
45
46template< typename T > struct Va_type_id;
47template<> struct Va_type_id<l4_umword_t> { enum { Id = L4_VARG_TYPE_UMWORD }; };
48template<> struct Va_type_id<l4_mword_t> { enum { Id = L4_VARG_TYPE_MWORD }; };
49template<> struct Va_type_id<l4_fpage_t> { enum { Id = L4_VARG_TYPE_FPAGE }; };
50template<> struct Va_type_id<void> { enum { Id = L4_VARG_TYPE_NIL }; };
51template<> struct Va_type_id<char const *> { enum { Id = L4_VARG_TYPE_STRING }; };
52
53template< typename T > struct Va_type;
54
55template<> struct Va_type<l4_umword_t> : Generic_va_type<l4_umword_t, Va_type_id> {};
56template<> struct Va_type<l4_mword_t> : Generic_va_type<l4_mword_t, Va_type_id> {};
57template<> struct Va_type<l4_fpage_t> : Generic_va_type<l4_fpage_t, Va_type_id> {};
58
59template<> struct Va_type<void>
60{
61 typedef void Ret_value;
62 typedef void Value;
63
64 static void const *addr_of(void) { return 0; }
65
66 static void value(void const *) {}
67 static L4_varg_type id() { return L4_VARG_TYPE_NIL; }
68 static unsigned size(void const *) { return 0; }
69};
70
71template<> struct Va_type<char const *>
72{
73 typedef char const *Ret_value;
74 typedef char const *Value;
75
76 static void const *addr_of(Value v) { return v; }
77
78 static L4_varg_type id() { return L4_VARG_TYPE_STRING; }
79 static unsigned size(void const *s)
80 {
81 char const *_s = reinterpret_cast<char const *>(s);
82 int l = 1;
83 while (*_s)
84 {
85 ++_s; ++l;
86 }
87 return l;
88 }
89
90 static Ret_value value(void const *d) { return static_cast<char const *>(d); }
91};
92
96class Varg
97{
98private:
99 enum { Direct_data = 0x8000 };
100 l4_umword_t _tag;
101 char const *_d;
102
103public:
104
107
109 L4_varg_type type() const { return static_cast<L4_varg_type>(_tag & 0xff); }
114 unsigned length() const { return _tag >> 16; }
116 Tag tag() const { return _tag & ~Direct_data; }
118 void tag(Tag tag) { _tag = tag; }
120 void data(char const *d) { _d = d; }
121
123 char const *data() const
124 {
125 if (_tag & Direct_data)
126 {
127 union T { char const *d; char v[sizeof(char const *)]; };
128 return reinterpret_cast<T const *>(&_d)->v;
129 }
130 return _d;
131 }
132
134#if __cplusplus >= 201103L
135 Varg() = default;
136#else
137 Varg() {}
138#endif
139
141 Varg(L4_varg_type t, void const *v, int len)
142 : _tag(t | (static_cast<l4_mword_t>(len) << 16)),
143 _d(static_cast<char const *>(v))
144 {}
145
146 static Varg nil() { return Varg(L4_VARG_TYPE_NIL, 0, 0); }
147
154 template< typename V >
155 typename Va_type<V>::Ret_value value() const
156 {
157 if (_tag & Direct_data)
158 {
159 union X { char const *d; V v; };
160 return reinterpret_cast<X const &>(_d).v;
161 }
162
163 return Va_type<V>::value(_d);
164 }
165
166
168 template< typename T >
169 bool is_of() const { return Va_type<T>::id() == type(); }
170
172 bool is_nil() const { return is_of<void>(); }
173
175 bool is_of_int() const
176 { return (type() & ~L4_VARG_TYPE_SIGN) == L4_VARG_TYPE_UMWORD; }
177
184 template< typename T >
185 bool get_value(typename Va_type<T>::Value *v) const
186 {
187 if (!is_of<T>())
188 return false;
189
190 *v = this->value<T>();
191 return true;
192 }
193
195 template< typename T >
196 void set_value(void const *d)
197 {
198 typedef Va_type<T> Vt;
199 _tag = Vt::id() | (Vt::size(d) << 16);
200 _d = static_cast<char const *>(d);
201 }
202
204 template<typename T>
205 void set_direct_value(T val, typename L4::Types::Enable_if<sizeof(T) <= sizeof(char const *), bool>::type = true)
206 {
207 static_assert(sizeof(T) <= sizeof(char const *), "direct Varg value too big");
208 typedef Va_type<T> Vt;
209 _tag = Vt::id() | (sizeof(T) << 16) | Direct_data;
210 union X { char const *d; T v; };
211 reinterpret_cast<X &>(_d).v = val;
212 }
213
215 template<typename T> explicit
216 Varg(T const *data) { set_value<T>(data); }
218 Varg(char const *data) { set_value<char const *>(data); }
219
221 template<typename T> explicit
222 Varg(T data, typename L4::Types::Enable_if<sizeof(T) <= sizeof(char const *), bool>::type = true)
223 { set_direct_value<T>(data); }
224};
225
226
227template<typename T>
228class Varg_t : public Varg
229{
230public:
231 typedef typename Va_type<T>::Value Value;
232 explicit Varg_t(Value v) : Varg()
233 { _data = v; set_value<T>(Va_type<T>::addr_of(_data)); }
234
235private:
236 Value _data;
237};
238
239template<unsigned MAX = L4_UTCB_GENERIC_DATA_SIZE>
240class Varg_list;
241
254{
255private:
256 template<unsigned T>
257 friend class Varg_list;
258
260 class Iter_state
261 {
262 private:
263 using M = l4_umword_t;
264 using Mp = M const *;
265 Mp _c;
266 Mp _e;
267
269 Mp next_arg(Varg const &a) const
270 {
271 return _c + 1 + (Msg::align_to<M>(a.length()) / sizeof(M));
272 }
273
274 public:
276 Iter_state() : _c(nullptr), _e(nullptr) {}
277
279 Iter_state(Mp c, Mp e) : _c(c), _e(e)
280 {}
281
283 bool valid() const
284 { return _c && _c < _e; }
285
287 Mp begin() const { return _c; }
288
290 Mp end() const { return _e; }
291
296 Varg pop()
297 {
298 if (!valid())
299 return Varg::nil();
300
301 Varg a;
302 a.tag(_c[0]);
303 a.data(reinterpret_cast<char const *>(&_c[1]));
304 _c = next_arg(a);
305 if (_c > _e)
306 return Varg::nil();
307
308 return a;
309 }
310
312 bool operator == (Iter_state const &o) const
313 { return _c == o._c; }
314
316 bool operator != (Iter_state const &o) const
317 { return _c != o._c; }
318 };
319
320 Iter_state _s;
321
322public:
324 Varg_list_ref() = default;
325
332 Varg_list_ref(void const *start, void const *end)
333 : _s(reinterpret_cast<l4_umword_t const *>(start),
334 reinterpret_cast<l4_umword_t const *>(end))
335 {}
336
339 {
340 private:
341 Iter_state _s;
342 Varg _a;
343
344 public:
346 Iterator(Iter_state const &s)
347 : _s(s)
348 {
349 _a = _s.pop();
350 }
351
353 explicit operator bool () const
354 { return !_a.is_nil(); }
355
358 {
359 if (!_a.is_nil())
360 _a = _s.pop();
361
362 return *this;
363 }
364
367 { return _a; }
368
370 bool equals(Iterator const &o) const
371 {
372 if (_a.is_nil() && o._a.is_nil())
373 return true;
374
375 return _s == o._s;
376 }
377
378 bool operator == (Iterator const &o) const
379 { return equals(o); }
380
381 bool operator != (Iterator const &o) const
382 { return !equals(o); }
383 };
384
387 { return _s.pop(); }
388
391 L4_DEPRECATED("Use range for or pop_front.")
392 { return _s.pop(); }
393
396 { return Iterator(_s); }
397
399 Iterator end() const
400 { return Iterator(Iter_state()); }
401};
402
410template<unsigned MAX>
412{
413 l4_umword_t data[MAX];
414 Varg_list(Varg_list const &);
415
416public:
419 {
420 if (!r._s.valid())
421 return;
422
423 l4_umword_t const *rs = r._s.begin();
424 unsigned c = r._s.end() - rs;
425 for (unsigned i = 0; i < c; ++i)
426 data[i] = rs[i];
427
428 this->_s = Iter_state(data, data + c);
429 }
430};
431
432
433namespace Msg {
434template<> struct Elem<Varg const *>
435{
436 typedef Varg const *arg_type;
437 typedef Varg_list_ref svr_type;
438 typedef Varg_list_ref svr_arg_type;
439 enum { Is_optional = false };
440};
441
442template<> struct Is_valid_rpc_type<Varg> : L4::Types::False {};
443template<> struct Is_valid_rpc_type<Varg *> : L4::Types::False {};
444template<> struct Is_valid_rpc_type<Varg &> : L4::Types::False {};
445template<> struct Is_valid_rpc_type<Varg const &> : L4::Types::False {};
446
447template<> struct Direction<Varg const *> : Dir_in {};
448template<> struct Class<Varg const *> : Cls_data {};
449
450template<typename DIR, typename CLASS>
451struct Clnt_val_ops<Varg, DIR, CLASS>;
452
453template<>
454struct Clnt_val_ops<Varg, Dir_in, Cls_data> :
455 Clnt_noops<Varg const &>
456{
457 using Clnt_noops<Varg const &>::to_msg;
458 static int to_msg(char *msg, unsigned offs, unsigned limit,
459 Varg const &a, Dir_in, Cls_data)
460 {
461 for (Varg const *i = &a; i->tag(); ++i)
462 {
463 offs = align_to<l4_umword_t>(offs);
464 if (L4_UNLIKELY(!check_size<l4_umword_t>(offs, limit)))
465 return -L4_EMSGTOOLONG;
466 *reinterpret_cast<l4_umword_t*>(msg + offs) = i->tag();
467 offs += sizeof(l4_umword_t);
468 if (L4_UNLIKELY(!check_size<char>(offs, limit, i->length())))
469 return -L4_EMSGTOOLONG;
470 char const *d = i->data();
471 for (unsigned x = 0; x < i->length(); ++x)
472 msg[offs++] = *d++;
473 }
474
475 return offs;
476 }
477};
478
479template<>
480struct Svr_val_ops<Varg_list_ref, Dir_in, Cls_data> :
481 Svr_noops<Varg_list_ref>
482{
483 using Svr_noops<Varg_list_ref>::to_svr;
484 static int to_svr(char *msg, unsigned offset, unsigned limit,
485 Varg_list_ref &a, Dir_in, Cls_data)
486 {
487 unsigned start = align_to<l4_umword_t>(offset);
488 unsigned offs;
489 for (offs = start; offs < limit;)
490 {
491 unsigned noffs = align_to<l4_umword_t>(offs);
492 if (L4_UNLIKELY(!check_size<l4_umword_t>(noffs, limit)))
493 break;
494
495 offs = noffs;
496 Varg arg;
497 arg.tag(*reinterpret_cast<l4_umword_t*>(msg + offs));
498
499 if (!arg.tag())
500 break;
501
502 offs += sizeof(l4_umword_t);
503
504 if (L4_UNLIKELY(!check_size<char>(offs, limit, arg.length())))
505 return -L4_EMSGTOOLONG;
506 offs += arg.length();
507 }
508
509 a = Varg_list_ref(msg + start, msg + align_to<l4_umword_t>(offs));
510 return offs;
511 }
512};
513}
514}}
Iterator for Valists.
Definition ipc_varg:339
Iterator(Iter_state const &s)
Create a new iterator.
Definition ipc_varg:346
bool equals(Iterator const &o) const
check for equality
Definition ipc_varg:370
Iterator & operator++()
increment iterator to the next arg
Definition ipc_varg:357
Varg operator*() const
dereference the iterator, get Varg
Definition ipc_varg:366
List of variable-sized RPC parameters as received by the server.
Definition ipc_varg:254
Iterator begin() const
Returns an interator to the first Varg.
Definition ipc_varg:395
Varg pop_front()
Get the next parameter in the list.
Definition ipc_varg:386
Varg next()
Get the next parameter in the list.
Definition ipc_varg:390
Varg_list_ref(void const *start, void const *end)
Create a parameter list over a given memory region.
Definition ipc_varg:332
Iterator end() const
Returns the end of the list.
Definition ipc_varg:399
Varg_list_ref()=default
Create an empty parameter list.
Self-contained list of variable-sized RPC parameters.
Definition ipc_varg:412
Varg_list(Varg_list_ref const &r)
Create a parameter list as a copy from a referencing list.
Definition ipc_varg:418
Variably sized RPC argument.
Definition ipc_varg:97
bool is_of_int() const
Definition ipc_varg:175
Varg(char const *data)
Make Varg from null-terminated string.
Definition ipc_varg:218
char const * data() const
Definition ipc_varg:123
Varg(L4_varg_type t, void const *v, int len)
Make an indirect varg.
Definition ipc_varg:141
Varg()=default
Make uninitialized Varg.
l4_umword_t Tag
The data type for the tag.
Definition ipc_varg:106
Va_type< V >::Ret_value value() const
Definition ipc_varg:155
bool is_of() const
Definition ipc_varg:169
void set_direct_value(T val, typename L4::Types::Enable_if< sizeof(T)<=sizeof(char const *), bool >::type=true)
Set to directly stored value of type T.
Definition ipc_varg:205
unsigned length() const
Get the size of the RPC argument.
Definition ipc_varg:114
void set_value(void const *d)
Set to indirect value of type T.
Definition ipc_varg:196
Tag tag() const
Definition ipc_varg:116
L4_varg_type type() const
Definition ipc_varg:109
void tag(Tag tag)
Set Varg tag (usually from message)
Definition ipc_varg:118
Varg(T const *data)
Make Varg from indirect value (pointer)
Definition ipc_varg:216
Varg(T data, typename L4::Types::Enable_if< sizeof(T)<=sizeof(char const *), bool >::type=true)
Make Varg from direct value.
Definition ipc_varg:222
void data(char const *d)
Set Varg to indirect data value (usually in UTCB)
Definition ipc_varg:120
bool is_nil() const
Definition ipc_varg:172
bool get_value(typename Va_type< T >::Value *v) const
Get the value of the Varg as type T.
Definition ipc_varg:185
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_EMSGTOOLONG
Message too long.
Definition err.h:57
#define L4_UNLIKELY(x)
Expression is unlikely to execute.
Definition compiler.h:275
#define L4_EXPORT
Attribute to mark functions, variables, and data types as being exported from a library.
Definition compiler.h:210
#define L4_DEPRECATED(s)
Mark symbol deprecated.
Definition compiler.h:280
L4 low-level kernel interface.
False meta value.
Definition types:296
L4 flexpage type.
Definition __l4_fpage.h:76