1 /*!
2  * \file   l4re/util/libs/cap_alloc.cc
3  * \brief  Capability allactor
4  */
5 /*
6  * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
7  *               Alexander Warg <warg@os.inf.tu-dresden.de>
8  *     economic rights: Technische Universität Dresden (Germany)
9  *
10  * This file is part of TUD:OS and distributed under the terms of the
11  * GNU General Public License 2.
12  * Please see the COPYING-GPL-2 file for details.
13  *
14  * As a special exception, you may use this file as part of a free software
15  * library without restriction.  Specifically, if other files instantiate
16  * templates or use macros or inline functions from this file, or you compile
17  * this file and link it with other files to produce an executable, this
18  * file does not by itself cause the resulting executable to be covered by
19  * the GNU General Public License.  This exception does not however
20  * invalidate any other reasons why the executable file might be covered by
21  * the GNU General Public License.
22  */
23 
24 #include <l4/sys/assert.h>
25 #include <l4/crtn/initpriorities.h>
26 #include <l4/re/env>
27 #include <l4/re/util/cap_alloc>
28 #include <l4/re/cap_alloc>
29 
30 //#define L4RE_STATIC_CAP_ALLOC
31 #if defined(L4RE_STATIC_CAP_ALLOC)
32 
33 namespace {
34 L4Re::Cap_alloc_t<L4Re::Util::Cap_alloc<4096> >
35   __attribute__((init_priority(INIT_PRIO_L4RE_UTIL_CAP_ALLOC)))
36   __cap_alloc(L4Re::Env::env()->first_free_cap());
37 }
38 
39 #else
40 
41 #include <l4/re/dataspace>
42 #include <l4/re/mem_alloc>
43 
44 namespace
45 {
46   struct Ca : L4Re::Cap_alloc_t<L4Re::Util::_Cap_alloc>
47   {
48     enum { Caps = 4096 };
49     typedef L4Re::Util::_Cap_alloc::Counter_storage<Caps> Storage;
50 
51     L4::Cap<L4Re::Dataspace> _ds;
Ca__anon50c372090211::Ca52     Ca() : _ds(L4::Cap<L4Re::Dataspace>::No_init)
53     {
54       L4Re::Env const *e = L4Re::Env::env();
55       _ds = L4::Cap<L4Re::Dataspace>(e->first_free_cap() << L4_CAP_SHIFT);
56       l4_check(e->mem_alloc()->alloc(sizeof(Storage), _ds) >= 0);
57       void *a = 0;
58       l4_check(e->rm()->attach(&a, sizeof(Storage),
59                                L4Re::Rm::F::Search_addr | L4Re::Rm::F::RW,
60                                L4::Ipc::make_cap_rw(_ds)) >= 0);
61       setup(a, Caps, e->first_free_cap() + 1);
62     }
63   };
64 
65   Ca __attribute__((init_priority(INIT_PRIO_L4RE_UTIL_CAP_ALLOC))) __cap_alloc;
66 }
67 
68 #endif
69 
70 namespace L4Re {
71   namespace Util {
72     _Cap_alloc &cap_alloc = __cap_alloc;
73   }
74 #ifndef SHARED
75   Cap_alloc *virt_cap_alloc = &__cap_alloc;
76 #else
77   // defined in ldso in the case of shared libs
78   extern Cap_alloc *virt_cap_alloc __attribute__((weak));
79 
80   // however, we have to set it to our cap allocator now
81   // to enable the VFS to use the application cap allocator
82   static void __attribute__((constructor))
setup()83   setup()
84   {
85     if (&virt_cap_alloc)
86       virt_cap_alloc = &__cap_alloc;
87   }
88 #endif
89 }
90