L4Re - L4 Runtime Environment
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 
8 namespace L4 {
9 
10 class Task;
11 class Kobject;
12 
13 template< typename T > class L4_EXPORT Cap;
14 
25 class L4_EXPORT Cap_base
26 {
27 private:
28  struct Invalid_conversion;
29 
30 public:
32  {
36  No_init
37  };
38 
42  enum Cap_type
43  {
44  Invalid = L4_INVALID_CAP
45  };
46 
51  l4_cap_idx_t cap() const throw() { return _c; }
52 
59  bool is_valid() const throw() { return !(_c & L4_INVALID_CAP_BIT); }
60 
61  operator Invalid_conversion * () const throw()
62  { return (Invalid_conversion*)(!(_c & L4_INVALID_CAP_BIT)); }
63 
71  l4_fpage_t fpage(unsigned rights = L4_FPAGE_RWX) const throw()
72  { return l4_obj_fpage(_c, 0, rights); }
73 
82  l4_umword_t snd_base(unsigned grant = 0,
83  l4_cap_idx_t base = L4_INVALID_CAP) const throw()
84  {
85  if (base == L4_INVALID_CAP)
86  base = _c;
87  return l4_map_obj_control(base, grant);
88  }
89 
90 
94  bool operator == (Cap_base const &o) const throw()
95  { return _c == o._c; }
96 
100  bool operator != (Cap_base const &o) const throw()
101  { return _c != o._c; }
102 
114  inline l4_msgtag_t validate(l4_utcb_t *u = l4_utcb()) const throw();
115 
128  inline l4_msgtag_t validate(Cap<Task> task,
129  l4_utcb_t *u = l4_utcb()) const throw();
130 
134  void invalidate() throw() { _c = L4_INVALID_CAP; }
135 protected:
141  explicit Cap_base(l4_cap_idx_t c) throw() : _c(c) {}
145  explicit Cap_base(Cap_type cap) throw() : _c(cap) {}
146 
152  explicit Cap_base(l4_default_caps_t cap) throw() : _c(cap) {}
153 
157  explicit Cap_base() throw() {}
158 
168  void move(Cap_base const &src) const
169  {
170  if (!is_valid() || !src.is_valid())
171  return;
172 
174  snd_base(L4_MAP_ITEM_GRANT) | 0xe0);
175  }
176 
184  void copy(Cap_base const &src) const
185  {
186  if (!is_valid() || !src.is_valid())
187  return;
188 
190  snd_base() | 0xe0);
191  }
192 
196 };
197 
198 
214 template< typename T >
215 class L4_EXPORT Cap : public Cap_base
216 {
217 private:
218  friend class L4::Kobject;
219 
231  explicit Cap(T const *p) throw()
232  : Cap_base(reinterpret_cast<l4_cap_idx_t>(p)) {}
233 
234 public:
235 
240  template< typename O >
241  Cap(Cap<O> const &o) throw() : Cap_base(o.cap())
242  { T* __t = ((O*)100); (void)__t; }
243 
248  Cap(Cap_type cap) throw() : Cap_base(cap) {}
249 
255 
260  explicit Cap(l4_cap_idx_t idx = L4_INVALID_CAP) throw() : Cap_base(idx) {}
261 
265  explicit Cap(No_init_type) throw() {}
266 
273  Cap move(Cap const &src) const
274  {
275  Cap_base::move(src);
276  return *this;
277  }
278 
283  Cap copy(Cap const &src) const
284  {
285  Cap_base::copy(src);
286  return *this;
287  }
288 
292  T *operator -> () const throw() { return reinterpret_cast<T*>(_c); }
293 };
294 
295 
306 template<>
307 class L4_EXPORT Cap<void> : public Cap_base
308 {
309 public:
310 
311  explicit Cap(void const *p) throw()
312  : Cap_base(reinterpret_cast<l4_cap_idx_t>(p)) {}
313 
317  Cap(Cap_type cap) throw() : Cap_base(cap) {}
318 
323  Cap(l4_default_caps_t cap) throw() : Cap_base(cap) {}
324 
329  explicit Cap(l4_cap_idx_t idx = L4_INVALID_CAP) throw() : Cap_base(idx) {}
330  explicit Cap(No_init_type) throw() {}
331 
338  Cap move(Cap const &src) const
339  {
340  Cap_base::move(src);
341  return *this;
342  }
343 
348  Cap copy(Cap const &src) const
349  {
350  Cap_base::copy(src);
351  return *this;
352  }
353 
354  template< typename T >
355  Cap(Cap<T> const &o) throw() : Cap_base(o.cap()) {}
356 };
357 
374 template< typename T, typename F >
375 inline
376 Cap<T> cap_cast(Cap<F> const &c) throw()
377 {
378  (void)static_cast<T const *>(reinterpret_cast<F const *>(100));
379  return Cap<T>(c.cap());
380 }
381 
382 // gracefully deal with L4::Kobject ambiguity
383 template< typename T >
384 inline
385 Cap<T> cap_cast(Cap<L4::Kobject> const &c) throw()
386 {
387  return Cap<T>(c.cap());
388 }
389 
405 template< typename T, typename F >
406 inline
408 {
409  return Cap<T>(c.cap());
410 }
411 
412 }
Cap(No_init_type)
Create an uninitialized cap selector.
Definition: capability.h:265
Cap move(Cap const &src) const
Move a capability to this cap slot.
Definition: capability.h:273
Invalid capability selector.
Definition: consts.h:141
Cap(l4_default_caps_t cap)
Initialize capability with one of the default capability selectors.
Definition: capability.h:254
Cap(Cap_type cap)
Constructor to create an invalid capability selector.
Definition: capability.h:248
l4_cap_idx_t _c
The C representation of a capability selector.
Definition: capability.h:195
Common L4 ABI Data Types.
Read-write-execute flex page.
Definition: __l4_fpage.h:114
L4 low-level kernel interface.
Capability selector for the current task.
Definition: consts.h:243
l4_umword_t snd_base(unsigned grant=0, l4_cap_idx_t base=L4_INVALID_CAP) const
Return send base.
Definition: capability.h:82
unsigned long l4_cap_idx_t
L4 Capability selector Type.
Definition: types.h:341
struct l4_utcb_t l4_utcb_t
Opaque type for the UTCB.
Definition: utcb.h:67
void move(Cap_base const &src) const
Replace this capability with the contents of src.
Definition: capability.h:168
l4_default_caps_t
Default capabilities setup for the initial tasks.
Definition: consts.h:240
Cap< T > cap_reinterpret_cast(Cap< F > const &c)
reinterpret_cast for capabilities.
Definition: capability.h:407
void invalidate()
Set this capability to invalid (L4_INVALID_CAP).
Definition: capability.h:134
Base class for all kinds of kernel objects and remote objects, referenced by capabilities.
Definition: kobject:46
l4_cap_idx_t cap() const
Return capability selector.
Definition: capability.h:51
Cap copy(Cap const &src) const
Copy a capability to this cap slot.
Definition: capability.h:283
unsigned long l4_umword_t
Unsigned machine word.
Definition: l4int.h:52
Cap_type
Invalid capability type.
Definition: capability.h:42
Cap(l4_cap_idx_t idx=L4_INVALID_CAP)
Initialize capability, defaults to the invalid capability selector.
Definition: capability.h:260
Cap_base(l4_default_caps_t cap)
Initialize capability with one of the default capabilities.
Definition: capability.h:152
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:679
l4_utcb_t * l4_utcb(void) L4_NOTHROW L4_PURE
Get the UTCB address.
Definition: utcb.h:340
Common task related definitions.
Cap_base(l4_cap_idx_t c)
Generate a capability from its C representation.
Definition: capability.h:141
l4_fpage_t fpage(unsigned rights=L4_FPAGE_RWX) const
Return flex-page for the capability.
Definition: capability.h:71
Cap_base()
Create an uninitialized instance.
Definition: capability.h:157
l4_cap_idx_t cap() const
Return capability selector.
Definition: kobject:79
l4_msgtag_t l4_task_map(l4_cap_idx_t dst_task, l4_cap_idx_t src_task, l4_fpage_t snd_fpage, l4_addr_t snd_base) L4_NOTHROW
Map resources available in the source task to a destination task.
Definition: task.h:366
Base class for all kinds of capabilities.
Definition: capability.h:25
bool is_valid() const
Test whether the capability is a valid capability index (i.e., not L4_INVALID_CAP).
Definition: capability.h:59
void copy(Cap_base const &src) const
Copy a capability.
Definition: capability.h:184
L4 flexpage type.
Definition: __l4_fpage.h:81
Flag as grant instead of map operation.
Definition: consts.h:198
Cap< T > cap_cast(Cap< F > const &c)
static_cast for capabilities.
Definition: capability.h:376
C++ interface for capabilities.
Definition: capability.h:13
Message tag data structure.
Definition: types.h:158
Cap(Cap< O > const &o)
Create a copy from o, supporting implicit type casting.
Definition: capability.h:241
Cap_base(Cap_type cap)
Constructor to create an invalid capability.
Definition: capability.h:145
Full rights for capability flex-pages.
Definition: __l4_fpage.h:196
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:645