3 * \brief Barrelfish library initialization.
7 * Copyright (c) 2007, 2008, 2009, 2010, 2011, 2012, ETH Zurich.
10 * This file is distributed under the terms in the attached LICENSE file.
11 * If you do not find this file, copies can be found by writing to:
12 * ETH Zurich D-INFK, CAB F.78, Universitaetstr. 6, CH-8092 Zurich,
13 * Attn: Systems Group.
17 #include <barrelfish/barrelfish.h>
18 #include <barrelfish/idc.h>
19 #include <barrelfish/dispatch.h>
20 #include <barrelfish/curdispatcher_arch.h>
21 #include <barrelfish/dispatcher_arch.h>
22 #include <barrelfish_kpi/dispatcher_shared.h>
23 #include <barrelfish/terminal.h>
24 #include <barrelfish/morecore.h>
25 #include <barrelfish/monitor_client.h>
26 #include <barrelfish/nameservice_client.h>
27 #include <barrelfish/spawn_client.h>
28 #include <barrelfish_kpi/domain_params.h>
29 #include <if/monitor_defs.h>
30 #include <trace/trace.h>
31 #include <octopus/init.h>
32 #include "threads_priv.h"
35 /// Are we the init domain (and thus need to take some special paths)?
36 static bool init_domain;
38 extern size_t (*_libc_terminal_read_func)(char *, size_t);
39 extern size_t (*_libc_terminal_write_func)(const char *, size_t);
40 extern void (*_libc_exit_func)(int);
41 extern void (*_libc_assert_func)(const char *, const char *, const char *, int);
45 void libc_exit(int status)
53 // Use spawnd if spawned through spawnd
54 if(disp_get_domain_id() == 0) {
55 errval_t err = cap_revoke(cap_dispatcher);
56 if (err_is_fail(err)) {
57 sys_print("revoking dispatcher failed in _Exit, spinning!", 100);
60 err = cap_delete(cap_dispatcher);
61 sys_print("deleting dispatcher failed in _Exit, spinning!", 100);
63 // XXX: Leak all other domain allocations
65 err = spawn_exit(status);
66 if(err_is_fail(err)) {
67 DEBUG_ERR(err, "spawn_exit");
71 // If we're not dead by now, we wait
75 static void libc_assert(const char *expression, const char *file,
76 const char *function, int line)
81 /* Formatting as per suggestion in C99 spec 7.2.1.1 */
82 len = snprintf(buf, sizeof(buf), "Assertion failed on core %d in %.*s: %s,"
83 " function %s, file %s, line %d.\n",
84 disp_get_core_id(), DISP_NAME_LEN,
85 disp_name(), expression, function, file, line);
86 sys_print(buf, len < sizeof(buf) ? len : sizeof(buf));
89 /* Set libc function pointers */
90 void barrelfish_libc_glue_init(void)
92 _libc_terminal_read_func = terminal_read;
93 _libc_terminal_write_func = terminal_write;
94 _libc_exit_func = libc_exit;
95 _libc_assert_func = libc_assert;
96 /* morecore func is setup by morecore_init() */
98 // XXX: set a static buffer for stdout
99 // this avoids an implicit call to malloc() on the first printf
100 static char buf[BUFSIZ];
101 setvbuf(stdout, buf, _IOLBF, sizeof(buf));
104 static void monitor_bind_cont(void *st, errval_t err, struct monitor_binding *b);
107 errval_t trace_my_setup(void)
109 #ifndef TRACING_EXISTS
114 struct capref cap = {
116 .slot = TASKCN_SLOT_TRACEBUF
119 if (disp_get_core_id() >= TRACE_COREID_LIMIT) {
120 // can't support tracing on this core. sorry :(
124 err = vspace_map_one_frame((void**)&trace_buffer_master, TRACE_BUF_SIZE,
126 if (err_is_fail(err)) {
127 DEBUG_ERR(err, "vspace_map_one_frame failed");
131 trace_buffer_va = trace_buffer_master +
132 (disp_get_core_id() * TRACE_PERCORE_BUF_SIZE);
134 dispatcher_handle_t handle = curdispatcher();
135 struct dispatcher_generic *disp = get_dispatcher_generic(handle);
136 // Update pointer to trace buffer in child's dispatcher
137 disp->trace_buf = (struct trace_buffer *)trace_buffer_va;
144 static bool request_done = false;
146 /** \brief Initialise libbarrelfish.
148 * This runs on a thread in every domain, after the dispatcher is setup but
149 * before main() runs.
151 errval_t barrelfish_init_onthread(struct spawn_domain_params *params)
155 // do we have an environment?
156 if (params != NULL && params->envp[0] != NULL) {
157 extern char **environ;
158 environ = params->envp;
161 // Init default waitset for this dispatcher
162 struct waitset *default_ws = get_default_waitset();
163 waitset_init(default_ws);
165 // Initialize ram_alloc state
167 /* All domains use smallcn to initialize */
168 err = ram_alloc_set(ram_alloc_fixed);
169 if (err_is_fail(err)) {
170 return err_push(err, LIB_ERR_RAM_ALLOC_SET);
173 err = vspace_current_init(init_domain);
174 if (err_is_fail(err)) {
175 return err_push(err, LIB_ERR_VSPACE_INIT);
178 err = slot_alloc_init();
179 if (err_is_fail(err)) {
180 return err_push(err, LIB_ERR_SLOT_ALLOC_INIT);
183 // reconstruct our pmap from data passed to us by our parent
184 if (params != NULL && params->vspace_buf != NULL) {
185 struct pmap *pmap = get_current_pmap();
186 err = pmap->f.deserialise(pmap, params->vspace_buf,
187 params->vspace_buf_len);
188 if (err_is_fail(err)) {
189 return err_push(err, LIB_ERR_VSPACE_INIT);
191 } else if (init_domain) {
192 // TODO: the kernel boots us with a deterministic pmap structure: use it
195 err = morecore_init();
196 if (err_is_fail(err)) {
197 return err_push(err, LIB_ERR_MORECORE_INIT);
202 // init domains only get partial init
207 /* bind to monitor */
208 struct monitor_lmp_binding *mcb =
209 malloc(sizeof(struct monitor_lmp_binding));
211 set_monitor_binding(&mcb->b);
213 errval_t init_complete_err;
215 request_done = false;
216 err = monitor_client_lmp_bind(mcb, monitor_bind_cont, &init_complete_err,
217 default_ws, DEFAULT_LMP_BUF_WORDS);
218 if (err_is_fail(err)) {
219 return err_push(err, LIB_ERR_MONITOR_CLIENT_BIND);
222 // dispatch events on the waitset until monitor binding completes
223 while (!request_done) {
224 err = event_dispatch(default_ws);
225 if (err_is_fail(err)) {
226 return err_push(err, LIB_ERR_EVENT_DISPATCH);
230 if(err_is_fail(init_complete_err)) {
231 USER_PANIC_ERR(err, "during initialization");
236 /* Bind with monitor's blocking rpc channel */
237 err = monitor_client_blocking_rpc_init();
238 if (err_is_fail(err)) {
239 return err_push(err, LIB_ERR_MONITOR_RPC_BIND);
242 /* XXX: Setup the channel with mem_serv and use the channel instead */
243 err = ram_alloc_set(NULL);
244 if (err_is_fail(err)) {
245 return err_push(err, LIB_ERR_RAM_ALLOC_SET);
249 err = trace_my_setup();
250 if (err_is_fail(err)) {
251 DEBUG_ERR(err, "trace_my_setup failed");
256 // try to connect to name service (may fail if we are the skb or ramfsd!)
257 err = nameservice_client_blocking_bind();
258 if (err_is_fail(err)) {
259 if (err_no(err) == LIB_ERR_GET_NAME_IREF) {
260 // skip everything else if we don't have a nameservice
263 return err_push(err, LIB_ERR_NAMESERVICE_CLIENT_INIT);
268 err = terminal_init();
269 if (err_is_fail(err)) {
270 return err_push(err, LIB_ERR_TERMINAL_INIT);
273 // Init domain spanning code
275 if (err_is_fail(err)) {
276 return err_push(err, LIB_ERR_DOMAIN_INIT);
282 static void monitor_bind_cont(void *st, errval_t err, struct monitor_binding *b)
284 // hacky errval_t state pointer used to signal completion
285 errval_t *init_complete_err = st;
287 assert(!init_domain);
288 *init_complete_err = err;
295 * \brief Initialise libbarrelfish, while disabled.
297 * This runs on the dispatcher's stack, while disabled, before the dispatcher is
298 * setup. We can't call anything that needs to be enabled (ie. cap invocations)
299 * or uses threads. This is called from crt0.
301 void barrelfish_init_disabled(dispatcher_handle_t handle, bool init_dom_arg);
302 void barrelfish_init_disabled(dispatcher_handle_t handle, bool init_dom_arg)
304 init_domain = init_dom_arg;
305 disp_init_disabled(handle);
306 thread_init_disabled(handle, init_dom_arg);