L4Re Operating System Framework
Interface and Usage Documentation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
capability.h
1
2#pragma once
3
4#include <l4/sys/consts.h>
5#include <l4/sys/types.h>
6#include <l4/sys/task.h>
7
8namespace L4 {
9
10class Task;
11class Kobject;
12
13template< typename T > class L4_EXPORT Cap;
14
26{
27public:
30 {
34 No_init
35 };
36
41 {
42 Invalid = L4_INVALID_CAP
43 };
44
49 l4_cap_idx_t cap() const noexcept { return _c; }
50
57 bool is_valid() const noexcept { return !(_c & L4_INVALID_CAP_BIT); }
58
62 int invalid_cap_error() const noexcept { return _c & ~L4_INVALID_CAP_BIT; }
63
64 explicit operator bool () const noexcept
65 { return !(_c & L4_INVALID_CAP_BIT); }
66
74 l4_fpage_t fpage(unsigned rights = L4_CAP_FPAGE_RWS) const noexcept
75 { return l4_obj_fpage(_c, 0, rights); }
76
87 l4_cap_idx_t base = L4_INVALID_CAP) const noexcept
88 {
89 if (base == L4_INVALID_CAP)
90 base = _c;
91 return l4_map_obj_control(base, grant);
92 }
93
94
98 bool operator == (Cap_base const &o) const noexcept
99 { return _c == o._c; }
100
104 bool operator != (Cap_base const &o) const noexcept
105 { return _c != o._c; }
106
120 inline l4_msgtag_t validate(l4_utcb_t *u = l4_utcb()) const noexcept;
121
136 inline l4_msgtag_t validate(Cap<Task> task,
137 l4_utcb_t *u = l4_utcb()) const noexcept;
138
142 void invalidate() noexcept { _c = L4_INVALID_CAP; }
143protected:
149 explicit Cap_base(l4_cap_idx_t c) noexcept : _c(c) {}
153 explicit Cap_base(Cap_type cap) noexcept : _c(cap) {}
154
160 explicit Cap_base(l4_default_caps_t cap) noexcept : _c(cap) {}
161
165 explicit Cap_base() noexcept {}
166
176 void move(Cap_base const &src) const
177 {
178 if (!is_valid() || !src.is_valid())
179 return;
180
183 }
184
192 void copy(Cap_base const &src) const
193 {
194 if (!is_valid() || !src.is_valid())
195 return;
196
198 snd_base() | L4_FPAGE_C_OBJ_RIGHTS);
199 }
200
204};
205
206
222template< typename T >
223class L4_EXPORT Cap : public Cap_base
224{
225private:
226 friend class L4::Kobject;
227
239 explicit Cap(T const *p) noexcept
240 : Cap_base(reinterpret_cast<l4_cap_idx_t>(p)) {}
241
242public:
243
250 template< typename From >
251 static void check_convertible_from() noexcept
252 {
253 using To = T;
254 [[maybe_unused]] To* t = static_cast<From*>(nullptr);
255 }
256
263 template< typename From >
264 static void check_castable_from() noexcept
265 {
266 using To = T;
267 [[maybe_unused]] To *t = static_cast<To *>(static_cast<From *>(nullptr));
268 }
269
274 template< typename O >
275 Cap(Cap<O> const &o) noexcept : Cap_base(o.cap())
276 { check_convertible_from<O>(); }
277
282 Cap(Cap_type cap) noexcept : Cap_base(cap) {}
283
288 Cap(l4_default_caps_t cap) noexcept : Cap_base(cap) {}
289
294 explicit Cap(l4_cap_idx_t idx = L4_INVALID_CAP) noexcept : Cap_base(idx) {}
295
299 explicit Cap(No_init_type) noexcept {}
300
307 Cap move(Cap const &src) const
308 {
309 Cap_base::move(src);
310 return *this;
311 }
312
317 Cap copy(Cap const &src) const
318 {
319 Cap_base::copy(src);
320 return *this;
321 }
322
326 T *operator -> () const noexcept { return reinterpret_cast<T*>(_c); }
327};
328
329
340template<>
341class L4_EXPORT Cap<void> : public Cap_base
342{
343public:
344
345 explicit Cap(void const *p) noexcept
346 : Cap_base(reinterpret_cast<l4_cap_idx_t>(p)) {}
347
351 Cap(Cap_type cap) noexcept : Cap_base(cap) {}
352
357 Cap(l4_default_caps_t cap) noexcept : Cap_base(cap) {}
358
363 explicit Cap(l4_cap_idx_t idx = L4_INVALID_CAP) noexcept : Cap_base(idx) {}
364 explicit Cap(No_init_type) noexcept {}
365
366 template< typename From >
367 static void check_convertible_from() noexcept {}
368
369 template< typename From >
370 static void check_castable_from() noexcept {}
371
378 Cap move(Cap const &src) const
379 {
380 Cap_base::move(src);
381 return *this;
382 }
383
388 Cap copy(Cap const &src) const
389 {
390 Cap_base::copy(src);
391 return *this;
392 }
393
394 template< typename T >
395 Cap(Cap<T> const &o) noexcept : Cap_base(o.cap()) {}
396};
397
414template< typename T, typename F >
415inline
416Cap<T> cap_cast(Cap<F> const &c) noexcept
417{
418 Cap<T>::template check_castable_from<F>();
419 return Cap<T>(c.cap());
420}
421
422// gracefully deal with L4::Kobject ambiguity
423template< typename T >
424inline
425Cap<T> cap_cast(Cap<L4::Kobject> const &c) noexcept
426{
427 return Cap<T>(c.cap());
428}
429
445template< typename T, typename F >
446inline
448{
449 return Cap<T>(c.cap());
450}
451
452}
Base class for all kinds of capabilities.
Definition capability.h:26
void copy(Cap_base const &src) const
Copy a capability.
Definition capability.h:192
Cap_base(l4_default_caps_t cap) noexcept
Initialize capability with one of the default capabilities.
Definition capability.h:160
Cap_base(Cap_type cap) noexcept
Constructor to create an invalid capability.
Definition capability.h:153
l4_fpage_t fpage(unsigned rights=L4_CAP_FPAGE_RWS) const noexcept
Return flexpage for the capability.
Definition capability.h:74
l4_cap_idx_t _c
The C representation of a capability selector.
Definition capability.h:203
l4_cap_idx_t cap() const noexcept
Return capability selector.
Definition capability.h:49
bool is_valid() const noexcept
Test whether the capability is a valid capability index (i.e., not L4_INVALID_CAP).
Definition capability.h:57
void move(Cap_base const &src) const
Replace this capability with the contents of src.
Definition capability.h:176
Cap_type
Invalid capability type.
Definition capability.h:41
int invalid_cap_error() const noexcept
Return the transported error code in an invalid capability index.
Definition capability.h:62
No_init_type
Special value for uninitialized capability objects.
Definition capability.h:30
Cap_base(l4_cap_idx_t c) noexcept
Generate a capability from its C representation.
Definition capability.h:149
Cap_base() noexcept
Create an uninitialized instance.
Definition capability.h:165
l4_umword_t snd_base(unsigned grant=L4_MAP_ITEM_MAP, l4_cap_idx_t base=L4_INVALID_CAP) const noexcept
Return send base.
Definition capability.h:86
C++ interface for capabilities.
Definition capability.h:224
Cap copy(Cap const &src) const
Copy a capability to this cap slot.
Definition capability.h:317
Cap(l4_cap_idx_t idx=L4_INVALID_CAP) noexcept
Initialize capability, defaults to the invalid capability selector.
Definition capability.h:294
Cap(No_init_type) noexcept
Create an uninitialized cap selector.
Definition capability.h:299
Cap(Cap_type cap) noexcept
Constructor to create an invalid capability selector.
Definition capability.h:282
static void check_castable_from() noexcept
Perform the type conversion that needs to compile in order for a capability of type From te be castab...
Definition capability.h:264
Cap(l4_default_caps_t cap) noexcept
Initialize capability with one of the default capability selectors.
Definition capability.h:288
Cap(Cap< O > const &o) noexcept
Create a copy from o, supporting implicit type casting.
Definition capability.h:275
Cap move(Cap const &src) const
Move a capability to this cap slot.
Definition capability.h:307
static void check_convertible_from() noexcept
Perform the type conversion that needs to compile in order for a capability of type From to be conver...
Definition capability.h:251
Base class for all kinds of kernel objects and remote objects, referenced by capabilities.
Definition kobject:37
C++ interface of the Task kernel object, see Task for the C interface.
Definition task:36
unsigned long l4_umword_t
Unsigned machine word.
Definition l4int.h:40
unsigned long l4_cap_idx_t
Capability selector type.
Definition types.h:335
l4_default_caps_t
Default capabilities setup for the initial tasks.
Definition consts.h:325
@ L4_BASE_TASK_CAP
Capability selector for the current task.
Definition consts.h:327
@ L4_INVALID_CAP
Invalid capability selector.
Definition consts.h:153
l4_fpage_t l4_obj_fpage(l4_cap_idx_t obj, unsigned int order, unsigned char rights) L4_NOTHROW
Create a kernel-object flexpage.
Definition __l4_fpage.h:715
@ L4_CAP_FPAGE_RWSD
Full rights for capability flexpages.
Definition __l4_fpage.h:212
@ L4_CAP_FPAGE_RWS
Read, interface specific 'W', and 'S' rights for capability flexpages.
Definition __l4_fpage.h:206
l4_umword_t l4_map_obj_control(l4_umword_t spot, unsigned grant) L4_NOTHROW
Create the first word for a map item that is a send item for the object space.
Definition __l4_fpage.h:749
@ L4_MAP_ITEM_GRANT
Flag as grant instead of map operation.
Definition consts.h:245
@ L4_MAP_ITEM_MAP
Flag as usual map operation.
Definition consts.h:247
@ L4_FPAGE_C_OBJ_RIGHTS
All Object-type specific right bits.
Definition __l4_fpage.h:270
l4_msgtag_t l4_task_map(l4_cap_idx_t dst_task, l4_cap_idx_t src_task, l4_fpage_t snd_fpage, l4_umword_t snd_base) L4_NOTHROW
Map resources available in the source task to a destination task.
Definition task.h:418
struct l4_utcb_t l4_utcb_t
Opaque type for the UTCB.
Definition utcb.h:56
l4_utcb_t * l4_utcb(void) L4_NOTHROW L4_PURE
Get the UTCB address.
Definition utcb.h:346
#define L4_EXPORT
Attribute to mark functions, variables, and data types as being exported from a library.
Definition compiler.h:210
Common L4 ABI Data Types.
L4 low-level kernel interface.
Cap< T > cap_reinterpret_cast(Cap< F > const &c) noexcept
reinterpret_cast for capabilities.
Definition capability.h:447
Cap< T > cap_cast(Cap< F > const &c) noexcept
static_cast for capabilities.
Definition capability.h:416
Message tag data structure.
Definition types.h:153
L4 flexpage type.
Definition __l4_fpage.h:76