L4Re - L4 Runtime Environment
name_space_svr
1 // vi:set ft=cpp: -*- Mode: C++ -*-
2 /*
3  * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
4  * Alexander Warg <warg@os.inf.tu-dresden.de>
5  * economic rights: Technische Universität Dresden (Germany)
6  *
7  * This file is part of TUD:OS and distributed under the terms of the
8  * GNU General Public License 2.
9  * Please see the COPYING-GPL-2 file for details.
10  *
11  * As a special exception, you may use this file as part of a free software
12  * library without restriction. Specifically, if other files instantiate
13  * templates or use macros or inline functions from this file, or you compile
14  * this file and link it with other files to produce an executable, this
15  * file does not by itself cause the resulting executable to be covered by
16  * the GNU General Public License. This exception does not however
17  * invalidate any other reasons why the executable file might be covered by
18  * the GNU General Public License.
19  */
20 #pragma once
21 
22 #include <l4/cxx/avl_tree>
23 #include <l4/cxx/std_ops>
24 #include <l4/sys/cxx/ipc_epiface>
25 #include <l4/cxx/string>
26 
27 #include <l4/sys/capability>
28 #include <l4/re/namespace>
29 
30 #include <cstddef>
31 
32 namespace L4Re { namespace Util {
33 
34 class Dbg;
35 class Err;
36 
37 namespace Names {
38 
42 class Name : public cxx::String
43 {
44 public:
45 
46  Name(const char *name = "") : String(name, __builtin_strlen(name)) {}
47  Name(const char *name, unsigned long len) : String(name, len) {}
48  Name(cxx::String const &n) : String(n) {}
49  char const *name() const { return start(); }
50  bool operator < (Name const &r) const;
51 };
52 
53 
57 class Obj
58 {
59 protected:
60  unsigned _f;
61  union
62  {
63  l4_cap_idx_t _cap;
64  L4::Epiface *_obj;
65  };
66 
67 
68 public:
69  enum Flags
70  {
71  F_rw = L4Re::Namespace::Rw,
72  F_strong = L4Re::Namespace::Strong,
73 
74  F_trusted = L4Re::Namespace::Trusted,
75 
76  F_rights_mask = F_rw | F_strong | F_trusted,
77 
78  F_cap = 0x100,
79  F_local = 0x200,
80  F_replacable = 0x400,
81  F_base_mask = 0xf00,
82  };
83 
84 
85  unsigned flags() const { return _f; }
86  void restrict_flags(unsigned max_rights)
87  { _f &= (~F_rights_mask | (max_rights & F_rights_mask)); }
88 
89  bool is_rw() const { return (_f & F_rw) == F_rw; }
90  bool is_strong() const { return _f & F_strong; }
91 
92  bool is_valid() const { return _f & F_cap; }
93  bool is_complete() const { return is_valid(); }
94  bool is_local() const { return _f & F_local; }
95  bool is_replacable() const { return _f & F_replacable; }
96  bool is_trusted() const { return _f & F_trusted; }
97 
98  L4::Epiface *obj() const { if (is_local()) return _obj; return 0; }
99  L4::Cap<void> cap() const
100  {
101  if (!is_local())
102  return L4::Cap<void>(_cap);
103  if (!_obj)
104  return L4::Cap<void>::Invalid;
105  return _obj->obj_cap();
106  }
107 
108 
109  void set(Obj const &o, unsigned flags)
110  {
111  *this = o;
112  restrict_flags(flags);
113  }
114 
115  explicit Obj(unsigned flags = 0)
116  : _f(flags), _cap(L4_INVALID_CAP)
117  {}
118 
119  Obj(unsigned f, L4::Cap<void> const &cap)
120  : _f((f & ~F_base_mask) | F_cap), _cap(cap.cap())
121  {}
122 
123  Obj(unsigned f, L4::Epiface *o)
124  : _f((f & ~F_base_mask) | F_cap | F_local), _obj(o)
125  {}
126 
127  void reset(unsigned flags)
128  {
129  _f = (_f & F_replacable) | (flags & ~(F_cap | F_local));
130  _cap = L4_INVALID_CAP;
131  }
132 
133 
134 };
135 
136 
140 class Entry : public cxx::Avl_tree_node
141 {
142 private:
143  friend class Name_space;
144  Name _n;
145  Obj _o;
146 
147  bool _dynamic;
148 
149 public:
150  Entry(Name const &n, Obj const &o, bool dynamic = false)
151  : _n(n), _o(o), _dynamic(dynamic) {}
152 
153  Name const &name() const { return _n; }
154  Obj const *obj() const { return &_o; }
155  Obj *obj() { return &_o; }
156  void obj(Obj const &o) { _o = o; }
157 
158  bool is_placeholder() const
159  { return !obj()->is_complete(); }
160 
161  bool is_dynamic() const { return _dynamic; }
162 
163  void set(Obj const &o);
164 
165 private:
166  void * operator new (size_t s);
167  void operator delete(void *b);
168 
169 };
170 
171 struct Names_get_key
172 {
173  typedef Name Key_type;
174  static Key_type const &key_of(Entry const *e)
175  { return e->name(); }
176 };
177 
178 
185 {
186  friend class Entry;
187 
188 private:
190  Tree _tree;
191 
192 protected:
193  L4Re::Util::Dbg const &_dbg;
194  L4Re::Util::Err const &_err;
195 
196 public:
197 
198  typedef Tree::Const_iterator Const_iterator;
199 
200  Const_iterator begin() const { return _tree.begin(); }
201  Const_iterator end() const { return _tree.end(); }
202 
203  Name_space(L4Re::Util::Dbg const &dbg, L4Re::Util::Err const &err)
204  : _dbg(dbg), _err(err)
205  {}
206 
207  virtual ~Name_space() {}
208 
209  Entry *find(Name const &name) const { return _tree.find_node(name); }
210  Entry *remove(Name const &name) { return _tree.remove(name); }
211  Entry *find_iter(Name const &name) const;
212  bool insert(Entry *e) { return _tree.insert(e).second; }
213 
214  void dump(bool rec = false, int indent = 0) const;
215 
216 protected:
217  // server support --------------------------------------------
230  virtual Entry *alloc_dynamic_entry(Name const &n, unsigned flags) = 0;
231 
237  virtual void free_dynamic_entry(Entry *e) = 0;
238 
261  virtual int get_epiface(l4_umword_t data, bool is_local, L4::Epiface **lo) = 0;
262 
275  virtual int copy_receive_cap(L4::Cap<void> *cap) = 0;
276 
285  virtual void free_capability(L4::Cap<void> cap) = 0;
286 
295  virtual void free_epiface(L4::Epiface *epiface) = 0;
296 
297  int insert_entry(Name const &name, unsigned flags, Entry **e);
298 
299 public:
300  // server interface ------------------------------------------
301  int op_query(L4Re::Namespace::Rights,
305 
306  int op_register_obj(L4Re::Namespace::Rights, unsigned flags,
308  L4::Ipc::Snd_fpage &cap);
309 
310  int op_unlink(L4Re::Namespace::Rights r,
312 };
313 
314 }}}
315 
Invalid capability selector.
Definition: consts.h:141
Node * remove(Key_param_type key)
Remove the node with key from the tree.
Definition: avl_tree:291
Pair< Node *, bool > insert(Node *new_node)
Insert a new node into this AVL tree.
Definition: avl_tree:229
Read-write.
Definition: namespace:71
AVL tree.
unsigned long l4_cap_idx_t
L4 Capability selector Type.
Definition: types.h:342
L4Re C++ Interfaces.
Definition: cmd_control:15
char const * find(char const *c) const
Find character. Return end() if not found.
Definition: string:135
Abstract server-side implementation of the L4::Namespace interface.
Definition: name_space_svr:184
L4::Cap related definitions.
Node * find_node(Key_param_type key) const
find the node with the given key.
Definition: bst.h:276
Obsolete, do not use.
Definition: namespace:75
Bst::Const_iterator Const_iterator
Constant forward iterator for the tree.
Definition: avl_tree:142
Allocation free string class with explicit length field.
Definition: string:41
l4_cap_idx_t cap() const
Return capability selector.
Definition: capability.h:52
unsigned long l4_umword_t
Unsigned machine word.
Definition: l4int.h:52
Server-side copy in buffer for Array.
Definition: ipc_array:123
Attribute for defining an optional RPC argument.
Definition: ipc_types:147
Index start() const
Pointer to first character.
Definition: string:65
int len() const
Length.
Definition: string:69
Const_iterator begin() const
Get the constant forward iterator for the first element in the set.
Definition: bst.h:179
Index end() const
Pointer to first byte behind the string.
Definition: string:67
Namespace interface.
Array reference data type for arrays located in the message.
Definition: ipc_array:39
Const_iterator end() const
Get the end marker for the constant forward iterator.
Definition: bst.h:184
Node of an AVL tree.
Definition: avl_tree:38
C++ interface for capabilities.
Definition: capability.h:13
String()
Zero-initialize. Create an invalid string.
Definition: string:62
Generic RPC wrapper for L4 flex-pages.
Definition: ipc_types:321