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)
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.
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.
22 #include <l4/cxx/avl_tree>
23 #include <l4/cxx/std_ops>
24 #include <l4/cxx/ipc_server>
25 #include <l4/cxx/string>
27 #include <l4/sys/capability>
28 #include <l4/re/namespace>
32 namespace L4Re { namespace Util {
42 class Name : public cxx::String
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;
64 L4::Server_object *_obj;
71 F_rw = L4Re::Namespace::Rw,
72 F_strong = L4Re::Namespace::Strong,
74 F_trusted = L4Re::Namespace::Trusted,
76 F_rights_mask = F_rw | F_strong | F_trusted,
85 unsigned flags() const { return _f; }
86 void restrict_flags(unsigned max_rights)
87 { _f &= (~F_rights_mask | (max_rights & F_rights_mask)); }
89 bool is_rw() const { return _f & F_rw; }
90 bool is_strong() const { return _f & F_strong; }
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; }
98 L4::Server_object *obj() const { if (is_local()) return _obj; return 0; }
99 L4::Cap<void> cap() const
102 return L4::Cap<void>(_cap);
104 return L4::Cap<void>::Invalid;
105 return _obj->obj_cap();
109 void set(Obj const &o, unsigned flags)
112 restrict_flags(flags);
115 explicit Obj(unsigned flags = 0)
116 : _f(flags), _cap(L4_INVALID_CAP)
119 Obj(unsigned f, L4::Cap<void> const &cap)
120 : _f((f & ~F_base_mask) | F_cap), _cap(cap.cap())
123 Obj(unsigned f, L4::Server_object *o)
124 : _f((f & ~F_base_mask) | F_cap | F_local), _obj(o)
127 void reset(unsigned flags)
129 _f = (_f & F_replacable) | (flags & ~(F_cap | F_local));
130 _cap = L4_INVALID_CAP;
140 class Entry : public cxx::Avl_tree_node
143 friend class Name_space;
151 Entry(Name const &n, Obj const &o, bool dynamic = false)
152 : _n(n), _o(o), _next_link(0), _dynamic(dynamic) {}
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; }
159 Entry *next_link() const { return _next_link; }
161 void add_link(Entry *o)
162 { o->_next_link = _next_link; _next_link = o; }
164 void set_next_link(Entry *o)
167 bool is_placeholder() const
168 { return !obj()->is_complete(); }
170 bool is_dynamic() const { return _dynamic; }
172 bool link(Entry *src);
173 void set(Obj const &o);
176 void * operator new (size_t s);
177 void operator delete(void *b);
183 typedef Name Key_type;
184 static Key_type const &key_of(Entry const *e)
185 { return e->name(); }
197 typedef cxx::Avl_tree<Entry, Names_get_key> Tree;
201 L4Re::Util::Dbg const &_dbg;
202 L4Re::Util::Err const &_err;
206 typedef Tree::Const_iterator Const_iterator;
208 Const_iterator begin() const { return _tree.begin(); }
209 Const_iterator end() const { return _tree.end(); }
211 Name_space(L4Re::Util::Dbg const &dbg, L4Re::Util::Err const &err)
212 : _dbg(dbg), _err(err)
215 virtual ~Name_space() {}
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; }
222 void dump(bool rec = false, int indent = 0) const;
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;
233 int insert_entry(Name const &name, unsigned flags, Entry **e);
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,