Rev 3773 | Rev 4343 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2928 | svoboda | 1 | /* |
2 | * Copyright (c) 2008 Jiri Svoboda |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * - The name of the author may not be used to endorse or promote products |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
29 | /** @addtogroup loader |
||
30 | * @brief Loads and runs programs from VFS. |
||
31 | * @{ |
||
32 | */ |
||
33 | /** |
||
34 | * @file |
||
3101 | svoboda | 35 | * @brief Loads and runs programs from VFS. |
36 | * |
||
37 | * The program loader is a special init binary. Its image is used |
||
38 | * to create a new task upon a @c task_spawn syscall. The syscall |
||
39 | * returns the id of a phone connected to the newly created task. |
||
40 | * |
||
41 | * The caller uses this phone to send the pathname and various other |
||
42 | * information to the loader. This is normally done by the C library |
||
43 | * and completely hidden from applications. |
||
2928 | svoboda | 44 | */ |
45 | |||
46 | #include <stdio.h> |
||
3004 | svoboda | 47 | #include <stdlib.h> |
2928 | svoboda | 48 | #include <unistd.h> |
3474 | svoboda | 49 | #include <bool.h> |
2928 | svoboda | 50 | #include <fcntl.h> |
51 | #include <sys/types.h> |
||
3004 | svoboda | 52 | #include <ipc/ipc.h> |
3148 | svoboda | 53 | #include <ipc/loader.h> |
3160 | svoboda | 54 | #include <loader/pcb.h> |
3004 | svoboda | 55 | #include <errno.h> |
3148 | svoboda | 56 | #include <async.h> |
2959 | svoboda | 57 | #include <as.h> |
2928 | svoboda | 58 | |
2969 | svoboda | 59 | #include <elf.h> |
60 | #include <elf_load.h> |
||
2928 | svoboda | 61 | |
4338 | svoboda | 62 | #define DPRINTF(...) |
63 | |||
3681 | svoboda | 64 | void program_run(void *entry, pcb_t *pcb); |
65 | |||
3101 | svoboda | 66 | /** Pathname of the file that will be loaded */ |
3004 | svoboda | 67 | static char *pathname = NULL; |
68 | |||
3170 | svoboda | 69 | /** The Program control block */ |
70 | static pcb_t pcb; |
||
71 | |||
3174 | svoboda | 72 | /** Number of arguments */ |
73 | static int argc = 0; |
||
74 | /** Argument vector */ |
||
75 | static char **argv = NULL; |
||
76 | /** Buffer holding all arguments */ |
||
77 | static char *arg_buf = NULL; |
||
78 | |||
3474 | svoboda | 79 | static elf_info_t prog_info; |
80 | static elf_info_t interp_info; |
||
81 | |||
82 | static bool is_dyn_linked; |
||
83 | |||
84 | |||
85 | static void loader_get_taskid(ipc_callid_t rid, ipc_call_t *request) |
||
3448 | svoboda | 86 | { |
87 | ipc_callid_t callid; |
||
88 | task_id_t task_id; |
||
89 | size_t len; |
||
90 | |||
91 | task_id = task_get_id(); |
||
92 | |||
93 | if (!ipc_data_read_receive(&callid, &len)) { |
||
94 | ipc_answer_0(callid, EINVAL); |
||
95 | ipc_answer_0(rid, EINVAL); |
||
96 | return; |
||
97 | } |
||
98 | |||
99 | if (len > sizeof(task_id)) len = sizeof(task_id); |
||
100 | |||
3535 | svoboda | 101 | ipc_data_read_finalize(callid, &task_id, len); |
3448 | svoboda | 102 | ipc_answer_0(rid, EOK); |
103 | } |
||
104 | |||
105 | |||
3101 | svoboda | 106 | /** Receive a call setting pathname of the program to execute. |
107 | * |
||
108 | * @param rid |
||
109 | * @param request |
||
110 | */ |
||
3174 | svoboda | 111 | static void loader_set_pathname(ipc_callid_t rid, ipc_call_t *request) |
2928 | svoboda | 112 | { |
3148 | svoboda | 113 | ipc_callid_t callid; |
3004 | svoboda | 114 | size_t len; |
115 | char *name_buf; |
||
116 | |||
117 | if (!ipc_data_write_receive(&callid, &len)) { |
||
118 | ipc_answer_0(callid, EINVAL); |
||
119 | ipc_answer_0(rid, EINVAL); |
||
120 | return; |
||
121 | } |
||
3148 | svoboda | 122 | |
3004 | svoboda | 123 | name_buf = malloc(len + 1); |
124 | if (!name_buf) { |
||
3148 | svoboda | 125 | ipc_answer_0(callid, ENOMEM); |
3004 | svoboda | 126 | ipc_answer_0(rid, ENOMEM); |
127 | return; |
||
128 | } |
||
129 | |||
3148 | svoboda | 130 | ipc_data_write_finalize(callid, name_buf, len); |
131 | ipc_answer_0(rid, EOK); |
||
3004 | svoboda | 132 | |
133 | if (pathname != NULL) { |
||
134 | free(pathname); |
||
135 | pathname = NULL; |
||
136 | } |
||
137 | |||
3148 | svoboda | 138 | name_buf[len] = '\0'; |
3004 | svoboda | 139 | pathname = name_buf; |
140 | } |
||
141 | |||
3174 | svoboda | 142 | /** Receive a call setting arguments of the program to execute. |
143 | * |
||
144 | * @param rid |
||
145 | * @param request |
||
146 | */ |
||
147 | static void loader_set_args(ipc_callid_t rid, ipc_call_t *request) |
||
148 | { |
||
149 | ipc_callid_t callid; |
||
150 | size_t buf_len, arg_len; |
||
151 | char *p; |
||
152 | int n; |
||
153 | |||
154 | if (!ipc_data_write_receive(&callid, &buf_len)) { |
||
155 | ipc_answer_0(callid, EINVAL); |
||
156 | ipc_answer_0(rid, EINVAL); |
||
157 | return; |
||
158 | } |
||
159 | |||
160 | if (arg_buf != NULL) { |
||
161 | free(arg_buf); |
||
162 | arg_buf = NULL; |
||
163 | } |
||
164 | |||
165 | if (argv != NULL) { |
||
166 | free(argv); |
||
167 | argv = NULL; |
||
168 | } |
||
169 | |||
170 | arg_buf = malloc(buf_len + 1); |
||
171 | if (!arg_buf) { |
||
172 | ipc_answer_0(callid, ENOMEM); |
||
173 | ipc_answer_0(rid, ENOMEM); |
||
174 | return; |
||
175 | } |
||
176 | |||
177 | ipc_data_write_finalize(callid, arg_buf, buf_len); |
||
178 | ipc_answer_0(rid, EOK); |
||
179 | |||
180 | arg_buf[buf_len] = '\0'; |
||
181 | |||
182 | /* |
||
183 | * Count number of arguments |
||
184 | */ |
||
185 | p = arg_buf; |
||
186 | n = 0; |
||
187 | while (p < arg_buf + buf_len) { |
||
188 | arg_len = strlen(p); |
||
189 | p = p + arg_len + 1; |
||
190 | ++n; |
||
191 | } |
||
192 | |||
193 | /* Allocate argv */ |
||
194 | argv = malloc((n + 1) * sizeof(char *)); |
||
195 | |||
196 | if (argv == NULL) { |
||
197 | free(arg_buf); |
||
198 | ipc_answer_0(callid, ENOMEM); |
||
199 | ipc_answer_0(rid, ENOMEM); |
||
200 | return; |
||
201 | } |
||
202 | |||
203 | /* |
||
204 | * Fill argv with argument pointers |
||
205 | */ |
||
206 | p = arg_buf; |
||
207 | n = 0; |
||
208 | while (p < arg_buf + buf_len) { |
||
209 | argv[n] = p; |
||
210 | |||
211 | arg_len = strlen(p); |
||
212 | p = p + arg_len + 1; |
||
213 | ++n; |
||
214 | } |
||
215 | |||
216 | argc = n; |
||
217 | argv[n] = NULL; |
||
218 | } |
||
219 | |||
3474 | svoboda | 220 | /** Load the previously selected program. |
3101 | svoboda | 221 | * |
222 | * @param rid |
||
223 | * @param request |
||
224 | * @return 0 on success, !0 on error. |
||
225 | */ |
||
3474 | svoboda | 226 | static int loader_load(ipc_callid_t rid, ipc_call_t *request) |
3004 | svoboda | 227 | { |
228 | int rc; |
||
229 | |||
3552 | svoboda | 230 | rc = elf_load_file(pathname, 0, 0, &prog_info); |
2928 | svoboda | 231 | if (rc < 0) { |
4338 | svoboda | 232 | DPRINTF("Failed to load executable '%s'.\n", pathname); |
3155 | svoboda | 233 | ipc_answer_0(rid, EINVAL); |
2928 | svoboda | 234 | return 1; |
235 | } |
||
236 | |||
3170 | svoboda | 237 | elf_create_pcb(&prog_info, &pcb); |
2928 | svoboda | 238 | |
3174 | svoboda | 239 | pcb.argc = argc; |
240 | pcb.argv = argv; |
||
3171 | svoboda | 241 | |
3004 | svoboda | 242 | if (prog_info.interp == NULL) { |
243 | /* Statically linked program */ |
||
3474 | svoboda | 244 | is_dyn_linked = false; |
3155 | svoboda | 245 | ipc_answer_0(rid, EOK); |
3004 | svoboda | 246 | return 0; |
247 | } |
||
2988 | svoboda | 248 | |
3677 | svoboda | 249 | printf("Load ELF interpreter '%s'\n", prog_info.interp); |
250 | rc = elf_load_file(prog_info.interp, 0, 0, &interp_info); |
||
2959 | svoboda | 251 | if (rc < 0) { |
4338 | svoboda | 252 | DPRINTF("Failed to load interpreter '%s.'\n", |
253 | prog_info.interp); |
||
3155 | svoboda | 254 | ipc_answer_0(rid, EINVAL); |
2959 | svoboda | 255 | return 1; |
256 | } |
||
257 | |||
3677 | svoboda | 258 | printf("Run interpreter.\n"); |
3400 | svoboda | 259 | printf("entry point: 0x%lx\n", interp_info.entry); |
260 | printf("pcb address: 0x%lx\n", &pcb); |
||
3690 | svoboda | 261 | printf("prog dynamic: 0x%lx\n", prog_info.dynamic); |
3155 | svoboda | 262 | |
3474 | svoboda | 263 | is_dyn_linked = true; |
3155 | svoboda | 264 | ipc_answer_0(rid, EOK); |
2989 | svoboda | 265 | |
3004 | svoboda | 266 | return 0; |
267 | } |
||
268 | |||
3474 | svoboda | 269 | |
270 | /** Run the previously loaded program. |
||
271 | * |
||
272 | * @param rid |
||
273 | * @param request |
||
274 | * @return 0 on success, !0 on error. |
||
275 | */ |
||
276 | static void loader_run(ipc_callid_t rid, ipc_call_t *request) |
||
277 | { |
||
278 | if (is_dyn_linked == true) { |
||
279 | /* Dynamically linked program */ |
||
4338 | svoboda | 280 | DPRINTF("Run ELF interpreter.\n"); |
281 | DPRINTF("Entry point: 0x%lx\n", interp_info.entry); |
||
3474 | svoboda | 282 | close_console(); |
283 | |||
284 | ipc_answer_0(rid, EOK); |
||
3681 | svoboda | 285 | program_run(interp_info.entry, &pcb); |
3474 | svoboda | 286 | |
287 | } else { |
||
288 | /* Statically linked program */ |
||
289 | close_console(); |
||
290 | ipc_answer_0(rid, EOK); |
||
3681 | svoboda | 291 | program_run(prog_info.entry, &pcb); |
3474 | svoboda | 292 | } |
293 | |||
294 | /* Not reached */ |
||
295 | } |
||
296 | |||
3148 | svoboda | 297 | /** Handle loader connection. |
3101 | svoboda | 298 | * |
299 | * Receive and carry out commands (of which the last one should be |
||
300 | * to execute the loaded program). |
||
301 | */ |
||
3148 | svoboda | 302 | static void loader_connection(ipc_callid_t iid, ipc_call_t *icall) |
3004 | svoboda | 303 | { |
304 | ipc_callid_t callid; |
||
305 | ipc_call_t call; |
||
306 | int retval; |
||
307 | |||
3148 | svoboda | 308 | /* Ignore parameters, the connection is already open */ |
309 | (void)iid; (void)icall; |
||
310 | |||
3004 | svoboda | 311 | while (1) { |
3148 | svoboda | 312 | callid = async_get_call(&call); |
3474 | svoboda | 313 | |
3004 | svoboda | 314 | switch (IPC_GET_METHOD(call)) { |
3561 | svoboda | 315 | case IPC_M_PHONE_HUNGUP: |
316 | exit(0); |
||
3448 | svoboda | 317 | case LOADER_GET_TASKID: |
318 | loader_get_taskid(callid, &call); |
||
319 | continue; |
||
3148 | svoboda | 320 | case LOADER_SET_PATHNAME: |
3174 | svoboda | 321 | loader_set_pathname(callid, &call); |
3148 | svoboda | 322 | continue; |
3174 | svoboda | 323 | case LOADER_SET_ARGS: |
324 | loader_set_args(callid, &call); |
||
3474 | svoboda | 325 | continue; |
326 | case LOADER_LOAD: |
||
327 | loader_load(callid, &call); |
||
328 | continue; |
||
3148 | svoboda | 329 | case LOADER_RUN: |
3174 | svoboda | 330 | loader_run(callid, &call); |
3474 | svoboda | 331 | /* Not reached */ |
3004 | svoboda | 332 | default: |
333 | retval = ENOENT; |
||
334 | break; |
||
335 | } |
||
3174 | svoboda | 336 | if ((callid & IPC_CALLID_NOTIFICATION) == 0 && |
337 | IPC_GET_METHOD(call) != IPC_M_PHONE_HUNGUP) { |
||
4338 | svoboda | 338 | DPRINTF("Responding EINVAL to method %d.\n", |
3174 | svoboda | 339 | IPC_GET_METHOD(call)); |
3004 | svoboda | 340 | ipc_answer_0(callid, EINVAL); |
341 | } |
||
342 | } |
||
3148 | svoboda | 343 | } |
3004 | svoboda | 344 | |
3148 | svoboda | 345 | /** Program loader main function. |
346 | */ |
||
347 | int main(int argc, char *argv[]) |
||
348 | { |
||
349 | ipc_callid_t callid; |
||
350 | ipc_call_t call; |
||
351 | ipcarg_t phone_hash; |
||
352 | |||
353 | /* The first call only communicates the incoming phone hash */ |
||
354 | callid = ipc_wait_for_call(&call); |
||
355 | |||
356 | if (IPC_GET_METHOD(call) != LOADER_HELLO) { |
||
357 | if (IPC_GET_METHOD(call) != IPC_M_PHONE_HUNGUP) |
||
358 | ipc_answer_0(callid, EINVAL); |
||
359 | return 1; |
||
360 | } |
||
361 | |||
362 | ipc_answer_0(callid, EOK); |
||
363 | phone_hash = call.in_phone_hash; |
||
364 | |||
365 | /* |
||
3170 | svoboda | 366 | * Up until now async must not be used as it couldn't |
367 | * handle incoming requests. (Which means e.g. printf() |
||
368 | * cannot be used) |
||
3148 | svoboda | 369 | */ |
370 | async_new_connection(phone_hash, 0, NULL, loader_connection); |
||
371 | async_manager(); |
||
372 | |||
2928 | svoboda | 373 | /* not reached */ |
374 | return 0; |
||
375 | } |
||
376 | |||
377 | /** @} |
||
378 | */ |