L4Re - L4 Runtime Environment
factory
Go to the documentation of this file.
1 // vi:set ft=cpp: -*- Mode: C++ -*-
2 /**
3  * \file
4  * Common factory related definitions.
5  */
6 /*
7  * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
8  * Alexander Warg <warg@os.inf.tu-dresden.de>
9  * economic rights: Technische Universit├Ąt Dresden (Germany)
10  *
11  * This file is part of TUD:OS and distributed under the terms of the
12  * GNU General Public License 2.
13  * Please see the COPYING-GPL-2 file for details.
14  *
15  * As a special exception, you may use this file as part of a free software
16  * library without restriction. Specifically, if other files instantiate
17  * templates or use macros or inline functions from this file, or you compile
18  * this file and link it with other files to produce an executable, this
19  * file does not by itself cause the resulting executable to be covered by
20  * the GNU General Public License. This exception does not however
21  * invalidate any other reasons why the executable file might be covered by
22  * the GNU General Public License.
23  */
24 
25 #pragma once
26 
27 #include <l4/sys/factory.h>
28 #include <l4/sys/capability>
29 #include <l4/sys/cxx/ipc_iface>
30 #include <l4/sys/cxx/ipc_varg>
31 
32 namespace L4 {
33 
34 /**
35  * C++ Factory interface to create kernel objects.
36  *
37  * A factory is used to create all kinds of kernel objects:
38  * - L4::Task
39  * - L4::Thread
40  * - L4::Factory
41  * - L4::Ipc_gate
42  * - L4::Irq
43  * - L4::Vm
44  *
45  * The factory is equipped with a limit that limits the amount of kernel
46  * memory available to that factory.
47  *
48  * \note The limit does not give any guarantee for the amount of available
49  * kernel memory.
50  *
51  * \includefile{l4/sys/factory}
52  *
53  * For the C interface refer to \ref l4_factory_api.
54  */
55 class Factory : public Kobject_t<Factory, Kobject, L4_PROTO_FACTORY>
56 {
57 public:
58 
59  typedef l4_mword_t Proto;
60 
61  /**
62  * Special type to add a void argument into the factory create stream.
63  */
64  struct Nil {};
65 
66  /**
67  * Special type to add a pascal string into the factory create stream.
68  *
69  * This encapsulates a string that has an explicit length.
70  */
71  struct Lstr
72  {
73  /**
74  * The character buffer.
75  */
76  char const *s;
77 
78  /**
79  * The number of characters in the buffer.
80  */
81  int len;
82 
83  /**
84  * \param s Pointer to the c-style string.
85  * \param len Length in number of characters of the string s.
86  */
87  Lstr(char const *s, int len) : s(s), len(len) {}
88  };
89 
90  /**
91  * Stream class for the create() argument stream.
92  *
93  * This stream allows a variable number of arguments to be
94  * added to a create() call.
95  */
96  class S
97  {
98  private:
99  l4_utcb_t *u;
100  l4_msgtag_t t;
101  l4_cap_idx_t f;
102 
103  public:
104  /**
105  * Create a copy.
106  *
107  * \param o Instance of S to copy.
108  */
109  S(S const &o)
110  : u(o.u), t(o.t), f(o.f)
111  { const_cast<S&>(o).t.raw = 0; }
112 
113  /**
114  * Create a stream for a specific create() call.
115  *
116  * \param f The capability for the factory object (L4::Factory).
117  * \param obj The protocol ID to describe the type of the object
118  * that shall be created.
119  * \param[out] target The capability selector for the new object. The
120  * caller must allocate the capability slot. The kernel
121  * stores the new object's capability into this slot.
122  * \param utcb The UTCB to use for the operation.
123  */
124  S(l4_cap_idx_t f, long obj, L4::Cap<void> target,
125  l4_utcb_t *utcb) throw()
126  : u(utcb), t(l4_factory_create_start_u(obj, target.cap(), u)), f(f)
127  {}
128 
129  /**
130  * Commit the operation in the destructor to have a cool syntax for
131  * create().
132  */
133  ~S()
134  {
135  if (t.raw)
136  l4_factory_create_commit_u(f, t, u);
137  }
138 
139  /**
140  * Explicitly commits the operation and returns the result.
141  *
142  * \return The result of the create() operation.
143  */
144  operator l4_msgtag_t ()
145  {
146  l4_msgtag_t r = l4_factory_create_commit_u(f, t, u);
147  t.raw = 0;
148  return r;
149  }
150 
151  /**
152  * Put a single l4_mword_t as next argument.
153  *
154  * \param i The value to add as next argument.
155  *
156  * \return Reference to this stream.
157  */
158  S &operator << (l4_mword_t i)
159  {
160  l4_factory_create_add_int_u(i, &t, u);
161  return *this;
162  }
163 
164  /**
165  * Put a single l4_umword_t as next argument.
166  *
167  * \param i The value to add as next argument.
168  *
169  * \return Reference to this stream.
170  */
171  S &operator << (l4_umword_t i)
172  {
173  l4_factory_create_add_uint_u(i, &t, u);
174  return *this;
175  }
176 
177  /**
178  * Add a zero-terminated string as next argument.
179  *
180  * \param s The string to add as next argument.
181  *
182  * \return Reference to this stream.
183  */
184  S &operator << (char const *s)
185  {
186  l4_factory_create_add_str_u(s, &t, u);
187  return *this;
188  }
189 
190  /**
191  * Add a pascal string as next argument.
192  *
193  * \param s The string to add as next argument.
194  *
195  * \return Reference to this stream.
196  */
197  S &operator << (Lstr const &s)
198  {
199  l4_factory_create_add_lstr_u(s.s, s.len, &t, u);
200  return *this;
201  }
202 
203  /**
204  * Add an empty argument.
205  *
206  * \return Reference to this stream.
207  */
208  S &operator << (Nil)
209  {
210  l4_factory_create_add_nil_u(&t, u);
211  return *this;
212  }
213 
214  /**
215  * Add a flex page as next argument.
216  *
217  * \param d The flex page to add (there will be no map operation).
218  *
219  * \return Reference to this stream.
220  */
221  S &operator << (l4_fpage_t d)
222  {
223  l4_factory_create_add_fpage_u(d, &t, u);
224  return *this;
225  }
226  };
227 
228 
229 public:
230 
231  /**
232  * Generic create call to the factory.
233  *
234  * \param[out] target Capability selector for the new object. The caller
235  * must allocate the capability slot. The kernel stores
236  * the new objects's capability into this slot.
237  * \param obj The protocol ID that specifies which kind of object
238  * shall be created.
239  * \param utcb The UTCB to use for the operation.
240  *
241  * \return A create stream that allows adding additional arguments to the
242  * create() call.
243  *
244  * This method does currently not directly invoke the factory. It returns a
245  * stream that shall invoke the factory after adding all additional arguments.
246  *
247  * Usage:
248  * ~~~
249  * L4::Cap<L4Re::Namespace> ns = L4Re::Util::cap_alloc.alloc<L4Re::Namespace>();
250  * factory->create(ns, L4Re::Namespace::Protocol) << "Argument text";
251  * ~~~
252  */
253  S create(Cap<void> target, long obj, l4_utcb_t *utcb = l4_utcb()) throw()
254  {
255  return S(cap(), obj, target, utcb);
256  }
257 
258  /**
259  * Create call for typed capabilities.
260  *
261  * \tparam OBJ Capability type of the object to be created.
262  * \param[out] target Capability of type OBJ.
263  * \param utcb UTCB to use.
264  *
265  * \return Argument stream to call the factory.
266  *
267  * \see create
268  */
269  template<typename OBJ>
270  S create(Cap<OBJ> target, l4_utcb_t *utcb = l4_utcb()) throw()
271  {
272  return S(cap(), OBJ::Protocol, target, utcb);
273  }
274 
275  L4_INLINE_RPC_NF(
276  l4_msgtag_t, create, (L4::Ipc::Out<L4::Cap<void> > target, l4_mword_t obj,
277  L4::Ipc::Varg const *args),
278  L4::Ipc::Call_t<L4_CAP_FPAGE_S>);
279 
280  /**
281  * Create a new task.
282  *
283  * \param[out] target_cap The kernel stores the new task's capability into
284  * this slot.
285  * \param utcb_area Flexpage that describes an area in the address
286  * space of the new task, where the kernel should
287  * map the kernel-allocated kernel-user memory to.
288  * The kernel uses the kernel-user memory to store
289  * UTCBs and vCPU state-save-areas of the new task.
290  * \param utcb The UTCB to use for the operation.
291  *
292  * \return Syscall return tag
293  *
294  * \note The size of the UTCB area specifies indirectly the number
295  * of UTCBs available for this task. Refer to L4::Task::add_ku_mem
296  * / l4_task_add_ku_mem() for adding more of this type of memory.
297  *
298  * \see L4::Task
299  */
300  l4_msgtag_t create_task(Cap<Task> const & target_cap,
301  l4_fpage_t const &utcb_area,
302  l4_utcb_t *utcb = l4_utcb()) throw()
303  { return l4_factory_create_task_u(cap(), target_cap.cap(), utcb_area, utcb); }
304 
305  /**
306  * Create a new thread.
307  *
308  * \param[out] target_cap The kernel stores the new thread's capability into
309  * this slot.
310  * \param utcb The UTCB to use for the operation.
311  *
312  * \return Syscall return tag
313  *
314  * \deprecated Use `create()` with `Cap<Thread>` as argument instead.
315  *
316  * \see L4::Thread
317  */
318  l4_msgtag_t create_thread(Cap<Thread> const &target_cap,
319  l4_utcb_t *utcb = l4_utcb()) throw()
320  L4_DEPRECATED("Call create with Cap<Thread> as argument instead.")
321  { return l4_factory_create_thread_u(cap(), target_cap.cap(), utcb); }
322 
323  /**
324  * Create a new factory.
325  *
326  * \param[out] target_cap The kernel stores the new factory's capability into
327  * this slot.
328  * \param limit Limit for the new factory in bytes.
329  * \param utcb The UTCB to use for the operation.
330  *
331  * \return Syscall return tag
332  *
333  * \note The limit of the new factory is subtracted from the available amount
334  * of the factory used for creation.
335  */
336  l4_msgtag_t create_factory(Cap<Factory> const &target_cap,
337  unsigned long limit,
338  l4_utcb_t *utcb = l4_utcb()) throw()
339  { return l4_factory_create_factory_u(cap(), target_cap.cap(), limit, utcb); }
340 
341  /**
342  * Create a new IPC gate.
343  *
344  * \param[out] target_cap The kernel stores the new IPC gate's capability
345  * into this slot.
346  * \param thread_cap Optional capability selector of the thread to
347  * bind the gate to. Use #L4_INVALID_CAP to create
348  * an unbound IPC gate.
349  * \param label Optional label of the gate (is used if
350  * `thread_cap` is valid).
351  * \param utcb The UTCB to use for the operation.
352  *
353  * \return Syscall return tag containing one of the following return codes.
354  *
355  * \retval L4_EOK No error occurred.
356  * \retval -L4_ENOMEM Out-of-memory during allocation of the Ipc_gate object.
357  * \retval -L4_ENOENT `thread_cap` is void or points to something that is not
358  * a thread.
359  * \retval -L4_EPERM No write rights on `thread_cap`.
360  *
361  * An unbound IPC gate can be bound to a thread using
362  * L4::Ipc_gate::bind_thread().
363  *
364  * \see L4::Ipc_gate
365  */
366  l4_msgtag_t create_gate(Cap<void> const &target_cap,
367  Cap<Thread> const &thread_cap, l4_umword_t label,
368  l4_utcb_t *utcb = l4_utcb()) throw()
369  { return l4_factory_create_gate_u(cap(), target_cap.cap(), thread_cap.cap(), label, utcb); }
370 
371  /**
372  * Create a new IRQ.
373  *
374  * \param[out] target_cap The kernel stores the new IRQ's capability into
375  * this slot.
376  * \param utcb The UTCB to use for the operation.
377  *
378  * \return Syscall return tag
379  *
380  * \deprecated Use `create()` with `Cap<Irq>` as argument instead.
381  *
382  * \see L4::Irq
383  */
384  l4_msgtag_t create_irq(Cap<Irq>const &target_cap,
385  l4_utcb_t *utcb = l4_utcb()) throw()
386  L4_DEPRECATED("Call create with Cap<Irq> as argument instead.")
387  { return l4_factory_create_irq_u(cap(), target_cap.cap(), utcb); }
388 
389  /**
390  * Create a new virtual machine.
391  *
392  * \param[out] target_cap The kernel stores the new VM's capability into this
393  * slot.
394  * \param utcb The UTCB to use for the operation.
395  *
396  * \return Syscall return tag
397  *
398  * \deprecated Use `create()` with `Cap<Vm>` as argument instead.
399  *
400  * \see L4::Vm
401  */
402  l4_msgtag_t create_vm(Cap<Vm>const &target_cap,
403  l4_utcb_t *utcb = l4_utcb()) throw()
404  L4_DEPRECATED("Call create with Cap<Vm> as argument instead.")
405  { return l4_factory_create_vm_u(cap(), target_cap.cap(), utcb); }
406 
407  typedef L4::Typeid::Rpc_nocode<create_t> Rpcs;
408 };
409 
410 }