L4Re - L4 Runtime Environment
env
Go to the documentation of this file.
1 // -*- Mode: C++ -*-
2 // vim:ft=cpp
3 /**
4  * \file
5  * Environment interface
6  */
7 /*
8  * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
9  * Alexander Warg <warg@os.inf.tu-dresden.de>,
10  * Björn Döbel <doebel@os.inf.tu-dresden.de>
11  * economic rights: Technische Universität Dresden (Germany)
12  *
13  * This file is part of TUD:OS and distributed under the terms of the
14  * GNU General Public License 2.
15  * Please see the COPYING-GPL-2 file for details.
16  *
17  * As a special exception, you may use this file as part of a free software
18  * library without restriction. Specifically, if other files instantiate
19  * templates or use macros or inline functions from this file, or you compile
20  * this file and link it with other files to produce an executable, this
21  * file does not by itself cause the resulting executable to be covered by
22  * the GNU General Public License. This exception does not however
23  * invalidate any other reasons why the executable file might be covered by
24  * the GNU General Public License.
25  */
26 #pragma once
27 
28 #include <l4/sys/types.h>
29 
30 #include <l4/re/rm>
31 #include <l4/re/parent>
32 #include <l4/re/mem_alloc>
33 #include <l4/re/log>
34 #include <l4/re/consts>
35 
36 #include <l4/re/env.h>
37 
38 namespace L4 {
39 class Scheduler;
40 }
41 
42 /**
43  * L4 Runtime Environment.
44  */
45 namespace L4Re
46 {
47  /**
48  * C++ interface of the initial environment that is provided to an L4 task.
49  *
50  * The initial environment is provided to each L4 task that is started
51  * by an L4Re conform loader, such as the Moe root task. The initial
52  * environment provides access to a set of initial capabilities and
53  * some additional information about the available resources, such as
54  * free UTCBs (see \link l4_utcb_api Virtual Registers \endlink) and
55  * available entries in capability table (provided by the micro kernel).
56  *
57  * Each of the initial capabilities is stored at a fixed index in the
58  * task's capability table and the L4 runtime environment provides
59  * convenience functions to retrieve the capabilities. See the table below
60  * for an comprehensive overview.
61  *
62  * Name | Object Type | Convenience Function
63  * ------------ | --------------- | --------------------
64  * parent | L4Re::Parent | L4Re::Env::parent()
65  * user_factory | L4::Factory | L4Re::Env::user_factory()
66  * log | L4Re::Log | L4Re::Env::log()
67  * main_thread | L4::Thread | L4Re::Env::main_thread()
68  * rm | L4Re::Rm | L4Re::Env::rm()
69  * factory | L4::Factory | L4Re::Env::factory()
70  * task | L4::Task | L4Re::Env::task()
71  * scheduler | L4::Scheduler | L4Re::Env::scheduler()
72  *
73  * Additional information found in the initial environment is:
74  * - First free entry in capability table
75  * - The \link l4_utcb_api UTCB \endlink area (as flex page)
76  * - First free UTCB (address in the UTCB area)
77  *
78  * \includefile{l4/re/env}
79  *
80  * For the C interface refer to \ref api_l4re_env.
81  */
82  class L4_EXPORT Env
83  {
84  private:
85  l4re_env_t _env;
86  public:
87 
88  /**
89  * C++ type for an entry in the initial objects array.
90  */
91  typedef l4re_env_cap_entry_t Cap_entry;
92 
93  /**
94  * Returns the initial environment for the current task.
95  *
96  * \return Pointer to the initial environment class.
97  *
98  * A typical use of this function is L4Re::Env::env()-><member>()
99  */
100  static Env const *env() throw()
101  { return reinterpret_cast<Env*>(l4re_global_env); }
102 
103  /**
104  * Object-capability to the parent.
105  * \return Parent object-capability
106  */
107  L4::Cap<Parent> parent() const throw()
108  { return L4::Cap<Parent>(_env.parent); }
109  /**
110  * Object-capability to the memory allocator.
111  * \return Memory allocator object-capability
112  */
113  L4::Cap<Mem_alloc> mem_alloc() const throw()
114  { return L4::Cap<Mem_alloc>(_env.mem_alloc); }
115  /**
116  * Object-capability to the user-level object factory.
117  */
118  L4::Cap<L4::Factory> user_factory() const throw()
119  { return L4::Cap<L4::Factory>(_env.mem_alloc); }
120  /**
121  * Object-capability to the region map.
122  * \return Region map object-capability
123  */
124  L4::Cap<Rm> rm() const throw()
125  { return L4::Cap<Rm>(_env.rm); }
126  /**
127  * Object-capability to the logging service.
128  * \return Log object-capability
129  */
130  L4::Cap<Log> log() const throw()
131  { return L4::Cap<Log>(_env.log); }
132  /**
133  * Object-capability of the first user thread.
134  * \return Object-capability of the first user thread.
135  */
136  L4::Cap<L4::Thread> main_thread() const throw()
137  { return L4::Cap<L4::Thread>(_env.main_thread); }
138  /**
139  * Object-capability of the user task.
140  * \return Object-capability of the user task.
141  */
142  L4::Cap<L4::Task> task() const throw()
143  { return L4::Cap<L4::Task>(L4RE_THIS_TASK_CAP); }
144  /**
145  * Object-capability to the factory object available to the task.
146  * \return Factory object-capability
147  */
148  L4::Cap<L4::Factory> factory() const throw()
149  { return L4::Cap<L4::Factory>(_env.factory); }
150  /**
151  * First available capability selector.
152  * \return First capability selector.
153  *
154  * First capability selector available for use for in the application.
155  */
156  l4_cap_idx_t first_free_cap() const throw()
157  { return _env.first_free_cap; }
158  /**
159  * UTCB area of the task.
160  * \return UTCB area
161  */
162  l4_fpage_t utcb_area() const throw()
163  { return _env.utcb_area; }
164  /**
165  * First free UTCB.
166  * \return object-capability
167  *
168  * First free UTCB within the UTCB area available for the application to
169  * use.
170  */
171  l4_addr_t first_free_utcb() const throw()
172  { return _env.first_free_utcb; }
173 
174  /**
175  * Get a pointer to the first entry in the initial objects array.
176  * \return A pointer to the first entry in the initial objects array.
177  */
178  Cap_entry const *initial_caps() const throw()
179  { return _env.caps; }
180 
181  /**
182  * Get the Cap_entry for the object named \a name.
183  * \param name is the name of the object.
184  * \param l is the length of the name, thus \a name might not be
185  * zero terminated.
186  * \return A pointer to the Cap_entry for the object named \a name,
187  * or NULL if no such object was found.
188  */
189  Cap_entry const *get(char const *name, unsigned l) const throw()
190  { return l4re_env_get_cap_l(name, l, &_env); }
191 
192  /**
193  * Get the capability selector for the object named \a name.
194  * \param name is the name of the object.
195  * \param l is the length of the name, thus \a name might not be
196  * zero terminated.
197  * \return A capability selector for the object named \a name,
198  * or an invalid capability selector if no such object was found.
199  */
200  template< typename T >
201  L4::Cap<T> get_cap(char const *name, unsigned l) const throw()
202  {
203  if (Cap_entry const *e = get(name, l))
204  return L4::Cap<T>(e->cap);
205 
206  return L4::Cap<T>(-L4_ENOENT);
207  }
208 
209  /**
210  * Get the capability selector for the object named \a name.
211  * \param name is the name of the object (zero terminated).
212  * \return A capability selector for the object named \a name,
213  * or an invalid capability selector if no such object was found.
214  */
215  template< typename T >
216  L4::Cap<T> get_cap(char const *name) const throw()
217  { return get_cap<T>(name, __builtin_strlen(name)); }
218 
219  /**
220  * Set parent object-capability.
221  * \param c Parent object-capability
222  */
223  void parent(L4::Cap<Parent> const &c) throw()
224  { _env.parent = c.cap(); }
225  /**
226  * Set memory allocator object-capability.
227  * \param c Memory allocator object-capability
228  */
229  void mem_alloc(L4::Cap<Mem_alloc> const &c) throw()
230  { _env.mem_alloc = c.cap(); }
231  /**
232  * Set region map object-capability.
233  * \param c Region map object-capability
234  */
235  void rm(L4::Cap<Rm> const &c) throw()
236  { _env.rm = c.cap(); }
237  /**
238  * Set log object-capability.
239  * \param c Log object-capability
240  */
241  void log(L4::Cap<Log> const &c) throw()
242  { _env.log = c.cap(); }
243  /**
244  * Set object-capability of first user thread.
245  * \param c First thread's object-capability
246  */
247  void main_thread(L4::Cap<L4::Thread> const &c) throw()
248  { _env.main_thread = c.cap(); }
249  /**
250  * Set factory object-capability.
251  * \param c Factory object-capability
252  */
253  void factory(L4::Cap<L4::Factory> const &c) throw()
254  { _env.factory = c.cap(); }
255  /**
256  * Set first available capability selector.
257  * \param c First capability selector available to the application.
258  */
259  void first_free_cap(l4_cap_idx_t c) throw()
260  { _env.first_free_cap = c; }
261  /**
262  * Set UTCB area of the task.
263  * \param utcbs UTCB area
264  */
265  void utcb_area(l4_fpage_t utcbs) throw()
266  { _env.utcb_area = utcbs; }
267  /**
268  * Set first free UTCB.
269  * \param u First UTCB available for the application to use.
270  */
271  void first_free_utcb(l4_addr_t u) throw()
272  { _env.first_free_utcb = u; }
273 
274  /**
275  * Get the scheduler capability for the task.
276  * \return The capability selector for the default scheduler used for this
277  * task.
278  */
279  L4::Cap<L4::Scheduler> scheduler() const throw()
280  { return L4::Cap<L4::Scheduler>(_env.scheduler); }
281 
282  /**
283  * Set the scheduler capability.
284  * \param c is the capability to be set as scheduler.
285  */
286  void scheduler(L4::Cap<L4::Scheduler> const &c) throw()
287  { _env.scheduler = c.cap(); }
288 
289  /**
290  * Set the pointer to the first Cap_entry in the initial objects array.
291  * \param first is the first element in the array.
292  */
293  void initial_caps(Cap_entry *first) throw()
294  { _env.caps = first; }
295  };
296 };