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:
33  {
37  No_init
38  };
39 
43  enum Cap_type
44  {
45  Invalid = L4_INVALID_CAP
46  };
47 
52  l4_cap_idx_t cap() const throw() { return _c; }
53 
60  bool is_valid() const throw() { return !(_c & L4_INVALID_CAP_BIT); }
61 
62  operator Invalid_conversion * () const throw()
63  { return (Invalid_conversion*)(!(_c & L4_INVALID_CAP_BIT)); }
64 
72  l4_fpage_t fpage(unsigned rights = L4_FPAGE_RWX) const throw()
73  { return l4_obj_fpage(_c, 0, rights); }
74 
83  l4_umword_t snd_base(unsigned grant = 0,
84  l4_cap_idx_t base = L4_INVALID_CAP) const throw()
85  {
86  if (base == L4_INVALID_CAP)
87  base = _c;
88  return l4_map_obj_control(base, grant);
89  }
90 
91 
95  bool operator == (Cap_base const &o) const throw()
96  { return _c == o._c; }
97 
101  bool operator != (Cap_base const &o) const throw()
102  { return _c != o._c; }
103 
115  inline l4_msgtag_t validate(l4_utcb_t *u = l4_utcb()) const throw();
116 
129  inline l4_msgtag_t validate(Cap<Task> task,
130  l4_utcb_t *u = l4_utcb()) const throw();
131 
135  void invalidate() throw() { _c = L4_INVALID_CAP; }
136 protected:
142  explicit Cap_base(l4_cap_idx_t c) throw() : _c(c) {}
146  explicit Cap_base(Cap_type cap) throw() : _c(cap) {}
147 
153  explicit Cap_base(l4_default_caps_t cap) throw() : _c(cap) {}
154 
158  explicit Cap_base() throw() {}
159 
169  void move(Cap_base const &src) const
170  {
171  if (!is_valid() || !src.is_valid())
172  return;
173 
175  snd_base(L4_MAP_ITEM_GRANT) | 0xe0);
176  }
177 
185  void copy(Cap_base const &src) const
186  {
187  if (!is_valid() || !src.is_valid())
188  return;
189 
191  snd_base() | 0xe0);
192  }
193 
197 };
198 
199 
215 template< typename T >
216 class L4_EXPORT Cap : public Cap_base
217 {
218 private:
219  friend class L4::Kobject;
220 
232  explicit Cap(T const *p) throw()
233  : Cap_base(reinterpret_cast<l4_cap_idx_t>(p)) {}
234 
235 public:
236 
241  template< typename O >
242  Cap(Cap<O> const &o) throw() : Cap_base(o.cap())
243  { T* __t = ((O*)100); (void)__t; }
244 
249  Cap(Cap_type cap) throw() : Cap_base(cap) {}
250 
256 
261  explicit Cap(l4_cap_idx_t idx = L4_INVALID_CAP) throw() : Cap_base(idx) {}
262 
266  explicit Cap(No_init_type) throw() {}
267 
274  Cap move(Cap const &src) const
275  {
276  Cap_base::move(src);
277  return *this;
278  }
279 
284  Cap copy(Cap const &src) const
285  {
286  Cap_base::copy(src);
287  return *this;
288  }
289 
293  T *operator -> () const throw() { return reinterpret_cast<T*>(_c); }
294 };
295 
296 
307 template<>
308 class L4_EXPORT Cap<void> : public Cap_base
309 {
310 public:
311 
312  explicit Cap(void const *p) throw()
313  : Cap_base(reinterpret_cast<l4_cap_idx_t>(p)) {}
314 
318  Cap(Cap_type cap) throw() : Cap_base(cap) {}
319 
324  Cap(l4_default_caps_t cap) throw() : Cap_base(cap) {}
325 
330  explicit Cap(l4_cap_idx_t idx = L4_INVALID_CAP) throw() : Cap_base(idx) {}
331  explicit Cap(No_init_type) throw() {}
332 
339  Cap move(Cap const &src) const
340  {
341  Cap_base::move(src);
342  return *this;
343  }
344 
349  Cap copy(Cap const &src) const
350  {
351  Cap_base::copy(src);
352  return *this;
353  }
354 
355  template< typename T >
356  Cap(Cap<T> const &o) throw() : Cap_base(o.cap()) {}
357 };
358 
375 template< typename T, typename F >
376 inline
377 Cap<T> cap_cast(Cap<F> const &c) throw()
378 {
379  (void)static_cast<T const *>(reinterpret_cast<F const *>(100));
380  return Cap<T>(c.cap());
381 }
382 
383 // gracefully deal with L4::Kobject ambiguity
384 template< typename T >
385 inline
386 Cap<T> cap_cast(Cap<L4::Kobject> const &c) throw()
387 {
388  return Cap<T>(c.cap());
389 }
390 
406 template< typename T, typename F >
407 inline
409 {
410  return Cap<T>(c.cap());
411 }
412 
413 }
Cap(No_init_type)
Create an uninitialized cap selector.
Definition: capability.h:266
Cap move(Cap const &src) const
Move a capability to this cap slot.
Definition: capability.h:274
No_init_type
Special value for uninitialized capability objects.
Definition: capability.h:32
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:255
Cap(Cap_type cap)
Constructor to create an invalid capability selector.
Definition: capability.h:249
l4_cap_idx_t _c
The C representation of a capability selector.
Definition: capability.h:196
Common L4 ABI Data Types.
Read-write-execute flex page.
Definition: __l4_fpage.h:114
L4 low-level kernel interface.
Definition: env:38
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:83
unsigned long l4_cap_idx_t
L4 Capability selector Type.
Definition: types.h:342
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:169
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:408
void invalidate()
Set this capability to invalid (L4_INVALID_CAP).
Definition: capability.h:135
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:52
Cap copy(Cap const &src) const
Copy a capability to this cap slot.
Definition: capability.h:284
unsigned long l4_umword_t
Unsigned machine word.
Definition: l4int.h:52
Cap_type
Invalid capability type.
Definition: capability.h:43
Cap(l4_cap_idx_t idx=L4_INVALID_CAP)
Initialize capability, defaults to the invalid capability selector.
Definition: capability.h:261
Cap_base(l4_default_caps_t cap)
Initialize capability with one of the default capabilities.
Definition: capability.h:153
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:142
l4_fpage_t fpage(unsigned rights=L4_FPAGE_RWX) const
Return flex-page for the capability.
Definition: capability.h:72
Cap_base()
Create an uninitialized instance.
Definition: capability.h:158
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:60
void copy(Cap_base const &src) const
Copy a capability.
Definition: capability.h:185
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:377
C++ interface for capabilities.
Definition: capability.h:13
Message tag data structure.
Definition: types.h:159
Cap(Cap< O > const &o)
Create a copy from o, supporting implicit type casting.
Definition: capability.h:242
Cap_base(Cap_type cap)
Constructor to create an invalid capability.
Definition: capability.h:146
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