]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/plr/server/src/app_loading.cc
update
[l4.git] / l4 / pkg / plr / server / src / app_loading.cc
1 #include "app_loading"
2 #include "locking.h"
3
4 /*
5  * app_loading.cc --
6  *
7  *    Implementation of application loading. Similar to the stuff
8  *    moe and ned do.
9  *
10  * (c) 2011-2013 Björn Döbel <doebel@os.inf.tu-dresden.de>,
11  *     economic rights: Technische Universität Dresden (Germany)
12  * This file is part of TUD:OS and distributed under the terms of the
13  * GNU General Public License 2.
14  * Please see the COPYING-GPL-2 file for details.
15  */
16
17 #define MSG() DEBUGf(Romain::Log::Loader)
18
19 Romain::App_model::Const_dataspace Romain::App_model::open_file(char const *name)
20 {
21         MSG() << name;
22
23         int err = open(name, O_RDONLY);
24         MSG() << "fopen: " << err;
25
26         cxx::Ref_ptr<L4Re::Vfs::File> fp = L4Re::Vfs::vfs_ops->get_file(err);
27         MSG() << "file ptr @ " << fp;
28
29         Romain::App_model::Dataspace ds_cap = fp->data_space();
30         MSG() << "ds @ 0x" << std::hex << ds_cap.cap();
31
32         return ds_cap;
33 }
34
35 l4_addr_t Romain::App_model::local_attach_ds(Romain::App_model::Const_dataspace ds,
36                                              unsigned long size,
37                                              unsigned long offset) const
38 {
39         Romain::Rm_guard r(rm(), 0); // we always init stuff for instance 0,
40         // rest is paged in lazily
41         // XXX: duplicate, remove
42 #if 0
43         MSG() << std::hex << ds.cap() << " "
44               << size << " "
45               << offset << " "
46               << l4_round_page(offset);
47 #endif
48         unsigned flags = L4Re::Rm::Search_addr;
49
50         /* This function is called during startup for attaching the
51          * binary DS read-only to read ELF info. However, in this case
52          * we don't have any flags indicating this and writing the DS will
53          * fail. Therefore, this hacky check adds an RO flag so that the
54          * memory manager performs the right thing.
55          */
56         if (ds == binary()) {
57                 flags |= L4Re::Rm::Read_only;
58         }
59         Romain::Region_handler handler(ds, L4_INVALID_CAP, offset, 0,
60                                     Romain::Region(0, 0));
61         l4_addr_t ret = (l4_addr_t)rm()->attach_locally((void*)0xdeadbeef,
62                                                         size, &handler, flags);
63         return ret;
64 }
65
66
67 void Romain::App_model::local_detach_ds(l4_addr_t addr, unsigned long /*size*/) const
68 {
69 #if 0
70         MSG() << (void*)addr;
71 #endif
72
73         long r = L4Re::Env::env()->rm()->detach(addr, 0);
74         _check(r != 0, "detach error");
75 }
76
77
78 int Romain::App_model::prog_reserve_area(l4_addr_t *start, unsigned long size,
79                                       unsigned flags, unsigned char align)
80 {
81         MSG() << (void*)start << " "
82               << size << " "
83               << flags << " "
84               << align;
85         _check(1, "prog_reserve_area unimplemented");
86         return -1;
87 }
88
89
90 Romain::App_model::Dataspace Romain::App_model::alloc_app_stack()
91 {
92 #if 0
93         MSG() << __func__ << " " << _stack.stack_size() << " "
94               << std::hex << (void*)_stack.ptr();
95 #endif
96         Romain::App_model::Dataspace ds = this->alloc_ds(_stack.stack_size());
97         char *addr = reinterpret_cast<char*>(this->local_attach_ds(ds, _stack.stack_size(), 0));
98         MSG() << "attached app stack: " << (void*)addr;
99         _local_stack_address = l4_addr_t(addr);
100
101         //_stack.ptr(addr);
102         _stack.set_local_top(addr + _stack.stack_size());
103         MSG() << "stack local : " << std::hex << (void*)_stack.ptr();
104         MSG() << "stack remote: " << std::hex << (void*)_stack.relocate(_stack.ptr());
105
106         /*
107          * Place UTCB area pointer at the very top of the stack.
108          * This way we know exactly what to put into the FS segment
109          * descriptor.
110          */
111         _stack.push(prog_info()->utcbs_start);
112
113         return ds;
114 }
115
116
117 void Romain::App_model::prog_attach_kip()
118 {
119         MSG() << "ATTACHING KIP: " << (void*)this->prog_info()->kip;
120         this->prog_attach_ds(this->prog_info()->kip, L4_PAGESIZE,
121                              this->local_kip_ds(), 0,
122                              L4Re::Rm::Read_only, "attaching KIP segment",
123                              (l4_addr_t)l4re_kip());
124         //enter_kdebug("kip");
125 }
126
127
128 void*
129 Romain::App_model::prog_attach_ds(l4_addr_t addr, unsigned long size,
130                                   Romain::App_model::Const_dataspace ds, unsigned long offset,
131                                   unsigned flags, char const *what, l4_addr_t local_start,
132                                   bool sharedFlag)
133 {
134         (void)what;
135         Romain::Rm_guard r(rm(), 0); // we always init stuff for instance 0,
136                                   // rest is paged in lazily
137 #if 0
138         MSG() << std::hex << (void*)addr << " "
139               << size << " "
140               << ds.cap() << " "
141               << offset << " "
142               << flags << " "
143               << local_start << " "
144               << what;
145 #endif
146
147         /*
148          * This is where we remember the initial segments.
149          */
150         void* target = rm()->attach((void*)addr, size,
151                                     Romain::Region_handler(
152                                         ds, L4_INVALID_CAP, offset, flags,
153                                         Romain::Region(local_start, local_start + size - 1)),
154                                         flags, L4_PAGESHIFT, sharedFlag);
155 #if 1
156         MSG() << std::hex << (void*)target;
157 #endif
158
159         _check(((target == 0) || (target == (void*)~0UL)), "error attaching segment");
160         return target;
161 }
162
163
164 L4Re::Env*
165 Romain::App_model::add_env()
166 {
167         _stack.push(l4re_env_cap_entry_t());
168         push_initial_caps();
169         L4Re::Env::Cap_entry *remote_cap_ptr =
170                 reinterpret_cast<L4Re::Env::Cap_entry*>(_stack.relocate(_stack.ptr()));
171         MSG() << "remote cap entries @ " << (void*)remote_cap_ptr;
172
173         L4Re::Env *env = _stack.push(L4Re::Env());
174         memcpy(env, L4Re::Env::env(), sizeof(L4Re::Env));
175         MSG() << "my first cap " << std::hex << env->first_free_cap();
176         env->initial_caps(remote_cap_ptr);
177         env->main_thread(L4::Cap<L4::Thread>(Romain::FIRST_REPLICA_CAP << L4_CAP_SHIFT));
178         env->first_free_cap(Romain::FIRST_REPLICA_CAP + 1); // First free + 1, because the first
179                                                             // free slot is used for the main thread's
180                                                             // GateAgent
181         env->utcb_area(l4_fpage(this->prog_info()->utcbs_start, this->prog_info()->utcbs_log2size, 0));
182         env->first_free_utcb(this->prog_info()->utcbs_start + L4_UTCB_OFFSET);
183
184         return env;
185 }
186
187
188 Romain::App_model::Dataspace Romain::App_model::alloc_ds(unsigned long size) const
189 {
190         Dataspace ds;
191         Romain::Region_map::allocate_ds(&ds, size);
192         return ds;
193 }
194
195 void Romain::App_model::copy_ds(Romain::App_model::Dataspace dst, unsigned long dst_offs,
196                                 Const_dataspace src, unsigned long src_offs,
197                                 unsigned long size)
198 {
199 #if 0
200         MSG() << std::hex << dst.cap() << " "
201               << dst_offs << " "
202               << src.cap() << " "
203               << src_offs << " "
204               << size;
205 #endif
206         dst->copy_in(dst_offs, src, src_offs, size);
207 #if 0
208         MSG() << "...done";
209 #endif
210 }
211
212
213 void Romain::App_model::prog_attach_stack(Romain::App_model::Dataspace app_stack)
214 {
215 #if 0
216         MSG() << __func__ << std::hex
217               << " " << app_stack.cap()
218               << " " << (void*)_stack.target_addr()
219               << " " << (void*)_stack.target_top();
220 #endif
221         this->prog_attach_ds(this->_stack.target_addr(),
222                              this->_stack.stack_size(),
223                              app_stack,
224                              0, 0, "stack segment", _local_stack_address);
225         prog_info()->stack_addr = _stack.target_addr() + _stack.stack_size();
226 #if 0
227         MSG() << std::hex << (void*)_stack.ptr() << " <-> "
228               << (void*)_stack.relocate(_stack.ptr());
229 #endif
230 }
231
232
233 void Romain::App_model::prog_reserve_utcb_area()
234 {
235         _utcb_area = this->alloc_ds(1 << prog_info()->utcbs_log2size);
236 #if 0
237         MSG() << __func__ << " " << std::hex
238               << (void*)this->prog_info()->utcbs_start;
239 #endif
240         this->prog_attach_ds(this->prog_info()->utcbs_start,
241                              1 << this->prog_info()->utcbs_log2size,
242                              _utcb_area, 0, 0, "utcb area",
243                              0);
244 }
245
246
247 void const *Romain::App_model::generate_l4aux(char const *name)
248 {
249         this->_stack.push(l4_umword_t(this->prog_info()->ldr_flags));
250         this->_stack.push(l4_umword_t(this->prog_info()->l4re_dbg));
251         this->_stack.push(l4_umword_t(local_kip_ds().cap()));
252         return this->_stack.push_local_ptr(name);
253 }