L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
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
59 explicit operator bool () const noexcept
60 { return !(_c & L4_INVALID_CAP_BIT); }
61
69 l4_fpage_t fpage(unsigned rights = L4_CAP_FPAGE_RWS) const noexcept
70 { return l4_obj_fpage(_c, 0, rights); }
71
82 l4_cap_idx_t base = L4_INVALID_CAP) const noexcept
83 {
84 if (base == L4_INVALID_CAP)
85 base = _c;
86 return l4_map_obj_control(base, grant);
87 }
88
89
93 bool operator == (Cap_base const &o) const noexcept
94 { return _c == o._c; }
95
99 bool operator != (Cap_base const &o) const noexcept
100 { return _c != o._c; }
101
115 inline l4_msgtag_t validate(l4_utcb_t *u = l4_utcb()) const noexcept;
116
131 inline l4_msgtag_t validate(Cap<Task> task,
132 l4_utcb_t *u = l4_utcb()) const noexcept;
133
137 void invalidate() noexcept { _c = L4_INVALID_CAP; }
138protected:
144 explicit Cap_base(l4_cap_idx_t c) noexcept : _c(c) {}
148 explicit Cap_base(Cap_type cap) noexcept : _c(cap) {}
149
155 explicit Cap_base(l4_default_caps_t cap) noexcept : _c(cap) {}
156
160 explicit Cap_base() noexcept {}
161
171 void move(Cap_base const &src) const
172 {
173 if (!is_valid() || !src.is_valid())
174 return;
175
178 }
179
187 void copy(Cap_base const &src) const
188 {
189 if (!is_valid() || !src.is_valid())
190 return;
191
193 snd_base() | L4_FPAGE_C_OBJ_RIGHTS);
194 }
195
199};
200
201
217template< typename T >
218class L4_EXPORT Cap : public Cap_base
219{
220private:
221 friend class L4::Kobject;
222
234 explicit Cap(T const *p) noexcept
235 : Cap_base(reinterpret_cast<l4_cap_idx_t>(p)) {}
236
237public:
238
245 template< typename From >
246 static void check_convertible_from() noexcept
247 {
248 using To = T;
249 [[maybe_unused]] To* t = static_cast<From*>(nullptr);
250 }
251
258 template< typename From >
259 static void check_castable_from() noexcept
260 {
261 using To = T;
262 [[maybe_unused]] To *t = static_cast<To *>(static_cast<From *>(nullptr));
263 }
264
269 template< typename O >
270 Cap(Cap<O> const &o) noexcept : Cap_base(o.cap())
271 { check_convertible_from<O>(); }
272
277 Cap(Cap_type cap) noexcept : Cap_base(cap) {}
278
283 Cap(l4_default_caps_t cap) noexcept : Cap_base(cap) {}
284
289 explicit Cap(l4_cap_idx_t idx = L4_INVALID_CAP) noexcept : Cap_base(idx) {}
290
294 explicit Cap(No_init_type) noexcept {}
295
302 Cap move(Cap const &src) const
303 {
304 Cap_base::move(src);
305 return *this;
306 }
307
312 Cap copy(Cap const &src) const
313 {
314 Cap_base::copy(src);
315 return *this;
316 }
317
321 T *operator -> () const noexcept { return reinterpret_cast<T*>(_c); }
322};
323
324
335template<>
336class L4_EXPORT Cap<void> : public Cap_base
337{
338public:
339
340 explicit Cap(void const *p) noexcept
341 : Cap_base(reinterpret_cast<l4_cap_idx_t>(p)) {}
342
346 Cap(Cap_type cap) noexcept : Cap_base(cap) {}
347
352 Cap(l4_default_caps_t cap) noexcept : Cap_base(cap) {}
353
358 explicit Cap(l4_cap_idx_t idx = L4_INVALID_CAP) noexcept : Cap_base(idx) {}
359 explicit Cap(No_init_type) noexcept {}
360
361 template< typename From >
362 static void check_convertible_from() noexcept {}
363
364 template< typename From >
365 static void check_castable_from() noexcept {}
366
373 Cap move(Cap const &src) const
374 {
375 Cap_base::move(src);
376 return *this;
377 }
378
383 Cap copy(Cap const &src) const
384 {
385 Cap_base::copy(src);
386 return *this;
387 }
388
389 template< typename T >
390 Cap(Cap<T> const &o) noexcept : Cap_base(o.cap()) {}
391};
392
409template< typename T, typename F >
410inline
411Cap<T> cap_cast(Cap<F> const &c) noexcept
412{
413 Cap<T>::template check_castable_from<F>();
414 return Cap<T>(c.cap());
415}
416
417// gracefully deal with L4::Kobject ambiguity
418template< typename T >
419inline
420Cap<T> cap_cast(Cap<L4::Kobject> const &c) noexcept
421{
422 return Cap<T>(c.cap());
423}
424
440template< typename T, typename F >
441inline
443{
444 return Cap<T>(c.cap());
445}
446
447}
Base class for all kinds of capabilities.
Definition capability.h:26
void copy(Cap_base const &src) const
Copy a capability.
Definition capability.h:187
Cap_base(l4_default_caps_t cap) noexcept
Initialize capability with one of the default capabilities.
Definition capability.h:155
Cap_base(Cap_type cap) noexcept
Constructor to create an invalid capability.
Definition capability.h:148
l4_fpage_t fpage(unsigned rights=L4_CAP_FPAGE_RWS) const noexcept
Return flex-page for the capability.
Definition capability.h:69
l4_cap_idx_t _c
The C representation of a capability selector.
Definition capability.h:198
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:171
Cap_type
Invalid capability type.
Definition capability.h:41
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:144
Cap_base() noexcept
Create an uninitialized instance.
Definition capability.h:160
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:81
C++ interface for capabilities.
Definition capability.h:219
Cap copy(Cap const &src) const
Copy a capability to this cap slot.
Definition capability.h:312
Cap(l4_cap_idx_t idx=L4_INVALID_CAP) noexcept
Initialize capability, defaults to the invalid capability selector.
Definition capability.h:289
Cap(No_init_type) noexcept
Create an uninitialized cap selector.
Definition capability.h:294
Cap(Cap_type cap) noexcept
Constructor to create an invalid capability selector.
Definition capability.h:277
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:259
Cap(l4_default_caps_t cap) noexcept
Initialize capability with one of the default capability selectors.
Definition capability.h:283
Cap(Cap< O > const &o) noexcept
Create a copy from o, supporting implicit type casting.
Definition capability.h:270
Cap move(Cap const &src) const
Move a capability to this cap slot.
Definition capability.h:302
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:246
Base class for all kinds of kernel objects and remote objects, referenced by capabilities.
Definition kobject:47
C++ interface of the Task kernel object, see Task for the C interface.
Definition task:47
unsigned long l4_umword_t
Unsigned machine word.
Definition l4int.h:51
unsigned long l4_cap_idx_t
Capability selector type.
Definition types.h:359
l4_default_caps_t
Default capabilities setup for the initial tasks.
Definition consts.h:314
@ L4_BASE_TASK_CAP
Capability selector for the current task.
Definition consts.h:316
@ L4_INVALID_CAP
Invalid capability selector.
Definition consts.h:168
l4_fpage_t l4_obj_fpage(l4_cap_idx_t obj, unsigned int order, unsigned char rights) L4_NOTHROW
Create a kernel-object flex page.
Definition __l4_fpage.h:680
@ L4_FPAGE_C_OBJ_RIGHTS
All Object-type specific right bits.
Definition __l4_fpage.h:271
@ L4_CAP_FPAGE_RWSD
Full rights for capability flex-pages.
Definition __l4_fpage.h:215
@ L4_CAP_FPAGE_RWS
Read, interface specific 'W', and 'S' rights for capability flex-pages.
Definition __l4_fpage.h:209
l4_umword_t l4_map_obj_control(l4_umword_t spot, unsigned grant) L4_NOTHROW
Create the first word for a map item for the object space.
Definition __l4_fpage.h:714
@ L4_MAP_ITEM_GRANT
Flag as grant instead of map operation.
Definition consts.h:257
@ L4_MAP_ITEM_MAP
Flag as usual map operation.
Definition consts.h:259
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:404
struct l4_utcb_t l4_utcb_t
Opaque type for the UTCB.
Definition utcb.h:67
l4_utcb_t * l4_utcb(void) L4_NOTHROW L4_PURE
Get the UTCB address.
Definition utcb.h:340
#define L4_EXPORT
Attribute to mark functions, variables, and data types as being exported from a library.
Definition compiler.h:231
Common L4 ABI Data Types.
L4 low-level kernel interface.
Definition io_regblock.h:19
Cap< T > cap_reinterpret_cast(Cap< F > const &c) noexcept
reinterpret_cast for capabilities.
Definition capability.h:442
Cap< T > cap_cast(Cap< F > const &c) noexcept
static_cast for capabilities.
Definition capability.h:411
Message tag data structure.
Definition types.h:164
L4 flexpage type.
Definition __l4_fpage.h:85