]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/l4re/util/include/name_space_svr
fc99e5f066041d08ebed7c215f3d56e6763242ce
[l4.git] / l4 / pkg / l4re / util / include / name_space_svr
1 // vi:ft=cpp
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/cxx/ipc_server>
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
39 /**
40  * \brief Name class.
41  */
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
54 /**
55  * \internal
56  */
57 class Obj
58 {
59 protected:
60   unsigned _f;
61   union
62   {
63     l4_cap_idx_t _cap;
64     L4::Server_object *_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; }
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::Server_object *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::Server_object *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
137 /**
138  * \internal
139  */
140 class Entry : public cxx::Avl_tree_node
141 {
142 private:
143   friend class Name_space;
144   Name _n;
145   Obj  _o;
146
147   Entry *_next_link;
148   bool _dynamic;
149
150 public:
151   Entry(Name const &n, Obj const &o, bool dynamic = false)
152   : _n(n), _o(o), _next_link(0), _dynamic(dynamic) {}
153
154   Name const &name() const { return _n; }
155   Obj const *obj() const { return &_o; }
156   Obj *obj() { return &_o; }
157   void obj(Obj const &o) { _o = o; }
158
159   Entry *next_link() const { return _next_link; }
160
161   void add_link(Entry *o)
162   { o->_next_link = _next_link; _next_link = o; }
163
164   void set_next_link(Entry *o)
165   { _next_link = o; }
166
167   bool is_placeholder() const
168   { return !obj()->is_complete(); }
169
170   bool is_dynamic() const { return _dynamic; }
171
172   bool link(Entry *src);
173   void set(Obj const &o);
174
175 private:
176   void * operator new (size_t s);
177   void operator delete(void *b);
178
179 };
180
181 struct Names_get_key
182 {
183   typedef Name Key_type;
184   static Key_type const &key_of(Entry const *e)
185   { return e->name(); }
186 };
187
188
189 /**
190  * \internal
191  */
192 class Name_space
193 {
194   friend class Entry;
195
196 private:
197   typedef cxx::Avl_tree<Entry, Names_get_key> Tree;
198   Tree _tree;
199
200 protected:
201   L4Re::Util::Dbg const &_dbg;
202   L4Re::Util::Err const &_err;
203
204 public:
205
206   typedef Tree::Const_iterator Const_iterator;
207
208   Const_iterator begin() const { return _tree.begin(); }
209   Const_iterator end() const { return _tree.end(); }
210
211   Name_space(L4Re::Util::Dbg const &dbg, L4Re::Util::Err const &err)
212   : _dbg(dbg), _err(err)
213   {}
214
215   virtual ~Name_space() {}
216
217   Entry *find(Name const &name) const  { return _tree.find_node(name); }
218   Entry *remove(Name const &name) { return _tree.remove(name); }
219   Entry *find_iter(Name const &name) const;
220   int insert(Entry *e)  { return _tree.insert(e).second; }
221
222   void dump(bool rec = false, int indent = 0) const;
223
224 protected:
225   // server support --------------------------------------------
226   virtual Entry *alloc_dynamic_entry(Name const &n, unsigned flags) = 0;
227   virtual void free_dynamic_entry(Entry *e) = 0;
228   virtual int get_capability(L4::Snd_fpage const &cap_fp, L4::Cap<void> *cap,
229                              L4::Server_object **lo = 0) = 0;
230   virtual int save_capability(L4::Cap<void> *cap) = 0;
231   virtual void free_capability(L4::Cap<void> cap) = 0;
232
233   int insert_entry(Name const &name, unsigned flags, Entry **e);
234
235 public:
236   // server interface ------------------------------------------
237   int query(L4::Ipc_iostream &ios, char *buffer, size_t blen);
238   int link_entry(L4::Ipc_iostream &ios, char *buffer, size_t blen);
239   int register_entry(L4::Ipc_iostream &ios, char *buffer, size_t blen);
240   int unlink_entry(L4::Ipc_iostream &ios, char *buffer, size_t max_len);
241   int dispatch(l4_umword_t obj, L4::Ipc_iostream &ios, char *buffer,
242                size_t blen);
243
244 };
245
246 }}}
247