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