2 * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
3 * Alexander Warg <warg@os.inf.tu-dresden.de>
4 * economic rights: Technische Universität Dresden (Germany)
6 * This file is part of TUD:OS and distributed under the terms of the
7 * GNU General Public License 2.
8 * Please see the COPYING-GPL-2 file for details.
10 #include <l4/re/namespace>
11 #include <l4/re/dataspace>
12 #include <l4/re/mem_alloc>
16 #include <l4/re/error_helper>
17 #include <l4/re/util/env_ns>
19 #include <l4/cxx/exceptions>
20 #include <l4/cxx/iostream>
21 #include <l4/cxx/l4iostream>
22 #include <l4/sys/debugger.h>
23 #include <l4/sys/scheduler>
24 #include <l4/sys/thread>
30 #include "page_alloc.h"
32 #include "loader_elf.h"
34 #include "dispatcher.h"
36 #include <l4/re/elf_aux.h>
38 L4RE_ELF_AUX_ELEM_T(l4re_elf_aux_mword_t, __stack_addr, L4RE_ELF_AUX_T_STACK_ADDR, 0xb1000000);
41 using L4Re::Dataspace;
43 extern "C" int main(int argc, char const *argv[]);
45 static Elf_loader loader;
46 Region_map __local_rm;
47 L4::Cap<void> rcv_cap;
50 public L4::Ipc_svr::Ignore_errors,
51 public L4::Ipc_svr::Default_timeout,
52 public L4::Ipc_svr::Compound_reply
55 static void setup_wait(L4::Ipc::Istream &istr, bool)
58 istr << L4::Ipc::Small_buf(rcv_cap.cap(), L4_RCV_ITEM_LOCAL_ID);
59 l4_utcb_br_u(istr.utcb())->bdr = 0;
63 extern "C" void *__libc_alloc_initial_tls(unsigned long size);
65 void *__libc_alloc_initial_tls(unsigned long size)
69 Cap<Dataspace> ds(Env::env()->first_free_cap() << L4_CAP_SHIFT);
70 ::l4re_global_env->first_free_cap += 1;
71 if (Env::env()->mem_alloc()->alloc(size, ds, 0) < 0)
74 void *addr = (void*)0xb0000000;
75 if(Env::env()->rm()->attach(&addr, size, Rm::Search_addr,
82 static L4::Server<Loop_hooks> server(l4_utcb());
84 static void insert_regions()
91 while ((n = L4Re::Env::env()->rm()->get_regions(addr, &rl)) > 0)
93 for (int i = 0; i < n; ++i)
95 Rm::Region const *r = &rl[i];
96 void *x = __local_rm.attach((void*)r->start, r->end - r->start +1,
97 Region_handler(L4::cap_reinterpret_cast<L4Re::Dataspace>(Env::env()->rm()),
98 L4_INVALID_CAP, 0, Rm::Pager), 0);
101 L4::cerr << "l4re: error while initializing region mapper\n";
111 while ((n = L4Re::Env::env()->rm()->get_areas(addr, &al)) > 0)
113 for (int i = 0; i < n; ++i)
115 Rm::Area const *r = &al[i];
116 l4_addr_t x = __local_rm.attach_area(r->start, r->end - r->start +1);
119 L4::cerr << "l4re: error while initializing region mapper\n";
128 int main(int argc, char const *argv[])
130 Dbg::set_level(Dbg::Info | Dbg::Warn);
136 l4_umword_t *envp = (l4_umword_t*)&argv[argc] + 1;
137 l4_umword_t *auxp = envp;
145 Global::envp = (char const * const*)envp;
147 L4::cout << "ARGC=" << argc << "\n"
148 << "ARGV=" << argv << "\n"
149 << "ENVP=" << envp << "\n"
150 << "AUXP=" << auxp << "\n";
152 for (int i = 0; i < argc; ++i)
153 L4::cout << " arg: '" << argv[i] << "'\n";
155 for (char const *const *e = Global::envp; *e; ++e)
156 L4::cout << " env: '" << *e << "'\n";
159 L4Re::Env *const env = const_cast<L4Re::Env*>(L4Re::Env::env());
164 Global::l4re_aux = (l4re_aux_t*)auxp[1];
169 L4::cout << "AUX=" << aux << "\n"
170 << "ENV=" << env << "\n";
173 //L4::cout << "Binary: " << aux->binary << " " << env << "\n";
174 if (!env || !Global::l4re_aux)
176 Err(Err::Fatal).printf("invalid AUX vectors...\n");
182 char *t = strstr(Global::l4re_aux->binary, "rom/");
184 strncpy(s + 1, t ? t + 4 : Global::l4re_aux->binary, sizeof(s)-1);
186 l4_debugger_set_object_name(L4_BASE_THREAD_CAP, s);
187 l4_debugger_set_object_name(L4_BASE_TASK_CAP, s + 1);
190 Dbg::set_level(Global::l4re_aux->dbg_lvl);
192 env->first_free_cap(env->first_free_cap() + Global::Max_local_rm_caps);
193 boot.printf("first free cap for application: 0x%lx\n", env->first_free_cap());
196 Cap<L4Re::Mem_alloc> obj = env->mem_alloc();
200 Err(Err::Fatal).printf("could not find the base memory allocator\n");
203 Global::allocator = obj;
206 boot.printf("setup page allocator\n");
207 Single_page_alloc_base::init(Global::allocator);
209 Global::local_rm = &__local_rm;
211 rcv_cap = Global::cap_alloc.alloc<void>();
213 boot.printf("setup local region mapping\n");
215 boot.printf("adding regions from remote region mapper\n");
218 boot.printf("initializing local page allocator\n");
219 Single_page_alloc_base::init_local_rm(Global::local_rm);
222 L4::Cap<Dataspace> file;
224 boot.printf("load binary '%s'\n", Global::l4re_aux->binary);
226 file = L4Re_app_model::open_file(Global::l4re_aux->binary);
228 loader.start(file, Global::local_rm, Global::l4re_aux);
230 // Raise RM prio to its MCP
231 env->scheduler()->run_thread(env->main_thread(), l4_sched_param(L4_SCHED_MAX_PRIO));
233 boot.printf("Start server loop\n");
234 server.loop(Dispatcher());
235 } catch (L4::Runtime_error const &e)
237 Err(Err::Fatal).printf("Exception %s: '%s'\n", e.str(), e.extra_str());
241 catch (L4::Base_exception const &e)
243 Err(Err::Fatal).printf("Exception %s\n", e.str());