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 
116  inline l4_msgtag_t validate(l4_utcb_t *u = l4_utcb()) const throw();
117 
131  inline l4_msgtag_t validate(Cap<Task> task,
132  l4_utcb_t *u = l4_utcb()) const throw();
133 
137  void invalidate() throw() { _c = L4_INVALID_CAP; }
138 protected:
144  explicit Cap_base(l4_cap_idx_t c) throw() : _c(c) {}
148  explicit Cap_base(Cap_type cap) throw() : _c(cap) {}
149 
155  explicit Cap_base(l4_default_caps_t cap) throw() : _c(cap) {}
156 
160  explicit Cap_base() throw() {}
161 
171  void move(Cap_base const &src) const
172  {
173  if (!is_valid() || !src.is_valid())
174  return;
175 
177  snd_base(L4_MAP_ITEM_GRANT) | 0xe0);
178  }
179 
187  void copy(Cap_base const &src) const
188  {
189  if (!is_valid() || !src.is_valid())
190  return;
191 
193  snd_base() | 0xe0);
194  }
195 
199 };
200 
201 
217 template< typename T >
218 class L4_EXPORT Cap : public Cap_base
219 {
220 private:
221  friend class L4::Kobject;
222 
234  explicit Cap(T const *p) throw()
235  : Cap_base(reinterpret_cast<l4_cap_idx_t>(p)) {}
236 
237 public:
238 
243  template< typename O >
244  Cap(Cap<O> const &o) throw() : Cap_base(o.cap())
245  { T* __t = ((O*)100); (void)__t; }
246 
251  Cap(Cap_type cap) throw() : Cap_base(cap) {}
252 
258 
263  explicit Cap(l4_cap_idx_t idx = L4_INVALID_CAP) throw() : Cap_base(idx) {}
264 
268  explicit Cap(No_init_type) throw() {}
269 
276  Cap move(Cap const &src) const
277  {
278  Cap_base::move(src);
279  return *this;
280  }
281 
286  Cap copy(Cap const &src) const
287  {
288  Cap_base::copy(src);
289  return *this;
290  }
291 
295  T *operator -> () const throw() { return reinterpret_cast<T*>(_c); }
296 };
297 
298 
309 template<>
310 class L4_EXPORT Cap<void> : public Cap_base
311 {
312 public:
313 
314  explicit Cap(void const *p) throw()
315  : Cap_base(reinterpret_cast<l4_cap_idx_t>(p)) {}
316 
320  Cap(Cap_type cap) throw() : Cap_base(cap) {}
321 
326  Cap(l4_default_caps_t cap) throw() : Cap_base(cap) {}
327 
332  explicit Cap(l4_cap_idx_t idx = L4_INVALID_CAP) throw() : Cap_base(idx) {}
333  explicit Cap(No_init_type) throw() {}
334 
341  Cap move(Cap const &src) const
342  {
343  Cap_base::move(src);
344  return *this;
345  }
346 
351  Cap copy(Cap const &src) const
352  {
353  Cap_base::copy(src);
354  return *this;
355  }
356 
357  template< typename T >
358  Cap(Cap<T> const &o) throw() : Cap_base(o.cap()) {}
359 };
360 
377 template< typename T, typename F >
378 inline
379 Cap<T> cap_cast(Cap<F> const &c) throw()
380 {
381  (void)static_cast<T const *>(reinterpret_cast<F const *>(100));
382  return Cap<T>(c.cap());
383 }
384 
385 // gracefully deal with L4::Kobject ambiguity
386 template< typename T >
387 inline
388 Cap<T> cap_cast(Cap<L4::Kobject> const &c) throw()
389 {
390  return Cap<T>(c.cap());
391 }
392 
408 template< typename T, typename F >
409 inline
411 {
412  return Cap<T>(c.cap());
413 }
414 
415 }
Cap(No_init_type)
Create an uninitialized cap selector.
Definition: capability.h:268
Cap move(Cap const &src) const
Move a capability to this cap slot.
Definition: capability.h:276
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:257
Cap(Cap_type cap)
Constructor to create an invalid capability selector.
Definition: capability.h:251
l4_cap_idx_t _c
The C representation of a capability selector.
Definition: capability.h:198
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: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:171
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:410
void invalidate()
Set this capability to invalid (L4_INVALID_CAP).
Definition: capability.h:137
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:286
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:263
Cap_base(l4_default_caps_t cap)
Initialize capability with one of the default capabilities.
Definition: capability.h:155
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:144
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:160
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:369
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:187
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:379
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:244
Cap_base(Cap_type cap)
Constructor to create an invalid capability.
Definition: capability.h:148
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