[c98e6ee] | 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
|
---|
[2f57690] | 30 | * @brief Loads and runs programs from VFS.
|
---|
[c98e6ee] | 31 | * @{
|
---|
[2f57690] | 32 | */
|
---|
[c98e6ee] | 33 | /**
|
---|
| 34 | * @file
|
---|
[2f57690] | 35 | * @brief Loads and runs programs from VFS.
|
---|
[c98e6ee] | 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.
|
---|
| 44 | */
|
---|
| 45 |
|
---|
| 46 | #include <stdio.h>
|
---|
| 47 | #include <stdlib.h>
|
---|
| 48 | #include <unistd.h>
|
---|
[3e6a98c5] | 49 | #include <stdbool.h>
|
---|
[c98e6ee] | 50 | #include <fcntl.h>
|
---|
| 51 | #include <sys/types.h>
|
---|
[bfd1546] | 52 | #include <ipc/services.h>
|
---|
[c98e6ee] | 53 | #include <ipc/loader.h>
|
---|
[79ae36dd] | 54 | #include <ns.h>
|
---|
[c98e6ee] | 55 | #include <loader/pcb.h>
|
---|
[f798178] | 56 | #include <entry_point.h>
|
---|
[c98e6ee] | 57 | #include <errno.h>
|
---|
| 58 | #include <async.h>
|
---|
[19f857a] | 59 | #include <str.h>
|
---|
[c98e6ee] | 60 | #include <as.h>
|
---|
[90b8d58] | 61 | #include <elf/elf.h>
|
---|
[bfdb5af1] | 62 | #include <elf/elf_load.h>
|
---|
[7171760] | 63 | #include <vfs/vfs.h>
|
---|
[bb9ec2d] | 64 | #include <vfs/inbox.h>
|
---|
[c98e6ee] | 65 |
|
---|
[7fb3f1c] | 66 | #define DPRINTF(...)
|
---|
[1ea99cc] | 67 |
|
---|
[bb9ec2d] | 68 | /** File that will be loaded */
|
---|
| 69 | static char *progname = NULL;
|
---|
| 70 | static int program_fd = -1;
|
---|
[c98e6ee] | 71 |
|
---|
| 72 | /** The Program control block */
|
---|
| 73 | static pcb_t pcb;
|
---|
| 74 |
|
---|
[622cdbe] | 75 | /** Current working directory */
|
---|
| 76 | static char *cwd = NULL;
|
---|
| 77 |
|
---|
[c98e6ee] | 78 | /** Number of arguments */
|
---|
| 79 | static int argc = 0;
|
---|
| 80 | /** Argument vector */
|
---|
| 81 | static char **argv = NULL;
|
---|
| 82 | /** Buffer holding all arguments */
|
---|
| 83 | static char *arg_buf = NULL;
|
---|
| 84 |
|
---|
[bb9ec2d] | 85 | /** Inbox entries. */
|
---|
| 86 | static struct pcb_inbox_entry inbox[INBOX_MAX_ENTRIES];
|
---|
| 87 | static int inbox_entries = 0;
|
---|
[bbdbf86] | 88 |
|
---|
[4470e26] | 89 | static elf_info_t prog_info;
|
---|
| 90 |
|
---|
[bfd1546] | 91 | /** Used to limit number of connections to one. */
|
---|
[007e6efa] | 92 | static bool connected = false;
|
---|
[4470e26] | 93 |
|
---|
[bbdbf86] | 94 | static void ldr_get_taskid(ipc_callid_t rid, ipc_call_t *request)
|
---|
[47e0a05b] | 95 | {
|
---|
| 96 | ipc_callid_t callid;
|
---|
| 97 | task_id_t task_id;
|
---|
| 98 | size_t len;
|
---|
[2f57690] | 99 |
|
---|
[47e0a05b] | 100 | task_id = task_get_id();
|
---|
[2f57690] | 101 |
|
---|
[0da4e41] | 102 | if (!async_data_read_receive(&callid, &len)) {
|
---|
[ffa2c8ef] | 103 | async_answer_0(callid, EINVAL);
|
---|
| 104 | async_answer_0(rid, EINVAL);
|
---|
[47e0a05b] | 105 | return;
|
---|
| 106 | }
|
---|
[2f57690] | 107 |
|
---|
| 108 | if (len > sizeof(task_id))
|
---|
| 109 | len = sizeof(task_id);
|
---|
| 110 |
|
---|
[0da4e41] | 111 | async_data_read_finalize(callid, &task_id, len);
|
---|
[ffa2c8ef] | 112 | async_answer_0(rid, EOK);
|
---|
[47e0a05b] | 113 | }
|
---|
| 114 |
|
---|
[622cdbe] | 115 | /** Receive a call setting the current working directory.
|
---|
[c98e6ee] | 116 | *
|
---|
| 117 | * @param rid
|
---|
| 118 | * @param request
|
---|
| 119 | */
|
---|
[622cdbe] | 120 | static void ldr_set_cwd(ipc_callid_t rid, ipc_call_t *request)
|
---|
[c98e6ee] | 121 | {
|
---|
[472c09d] | 122 | char *buf;
|
---|
[eda925a] | 123 | int rc = async_data_write_accept((void **) &buf, true, 0, 0, 0, NULL);
|
---|
[2f57690] | 124 |
|
---|
[472c09d] | 125 | if (rc == EOK) {
|
---|
| 126 | if (cwd != NULL)
|
---|
| 127 | free(cwd);
|
---|
| 128 |
|
---|
| 129 | cwd = buf;
|
---|
[c98e6ee] | 130 | }
|
---|
[2f57690] | 131 |
|
---|
[ffa2c8ef] | 132 | async_answer_0(rid, rc);
|
---|
[622cdbe] | 133 | }
|
---|
[47e0a05b] | 134 |
|
---|
[bb9ec2d] | 135 | /** Receive a call setting the program to execute.
|
---|
[c98e6ee] | 136 | *
|
---|
| 137 | * @param rid
|
---|
| 138 | * @param request
|
---|
| 139 | */
|
---|
[bb9ec2d] | 140 | static void ldr_set_program(ipc_callid_t rid, ipc_call_t *request)
|
---|
[c98e6ee] | 141 | {
|
---|
[bb9ec2d] | 142 | ipc_callid_t writeid;
|
---|
| 143 | size_t namesize;
|
---|
| 144 | if (!async_data_write_receive(&writeid, &namesize)) {
|
---|
| 145 | async_answer_0(rid, EINVAL);
|
---|
| 146 | return;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | char* name = malloc(namesize);
|
---|
| 150 | int rc = async_data_write_finalize(writeid, name, namesize);
|
---|
| 151 | if (rc != EOK) {
|
---|
| 152 | async_answer_0(rid, EINVAL);
|
---|
| 153 | return;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | int file = vfs_receive_handle(true);
|
---|
| 157 | if (file < 0) {
|
---|
| 158 | async_answer_0(rid, EINVAL);
|
---|
| 159 | return;
|
---|
[c98e6ee] | 160 | }
|
---|
[2f57690] | 161 |
|
---|
[bb9ec2d] | 162 | progname = name;
|
---|
| 163 | program_fd = file;
|
---|
| 164 | async_answer_0(rid, EOK);
|
---|
[c98e6ee] | 165 | }
|
---|
| 166 |
|
---|
| 167 | /** Receive a call setting arguments of the program to execute.
|
---|
| 168 | *
|
---|
| 169 | * @param rid
|
---|
| 170 | * @param request
|
---|
| 171 | */
|
---|
[bbdbf86] | 172 | static void ldr_set_args(ipc_callid_t rid, ipc_call_t *request)
|
---|
[c98e6ee] | 173 | {
|
---|
[472c09d] | 174 | char *buf;
|
---|
| 175 | size_t buf_size;
|
---|
[eda925a] | 176 | int rc = async_data_write_accept((void **) &buf, true, 0, 0, 0, &buf_size);
|
---|
[472c09d] | 177 |
|
---|
| 178 | if (rc == EOK) {
|
---|
| 179 | /*
|
---|
| 180 | * Count number of arguments
|
---|
| 181 | */
|
---|
| 182 | char *cur = buf;
|
---|
| 183 | int count = 0;
|
---|
| 184 |
|
---|
| 185 | while (cur < buf + buf_size) {
|
---|
| 186 | size_t arg_size = str_size(cur);
|
---|
| 187 | cur += arg_size + 1;
|
---|
| 188 | count++;
|
---|
| 189 | }
|
---|
[2f57690] | 190 |
|
---|
[472c09d] | 191 | /*
|
---|
| 192 | * Allocate new argv
|
---|
| 193 | */
|
---|
| 194 | char **_argv = (char **) malloc((count + 1) * sizeof(char *));
|
---|
| 195 | if (_argv == NULL) {
|
---|
| 196 | free(buf);
|
---|
[ffa2c8ef] | 197 | async_answer_0(rid, ENOMEM);
|
---|
[472c09d] | 198 | return;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | /*
|
---|
| 202 | * Fill the new argv with argument pointers
|
---|
| 203 | */
|
---|
| 204 | cur = buf;
|
---|
| 205 | count = 0;
|
---|
| 206 | while (cur < buf + buf_size) {
|
---|
| 207 | _argv[count] = cur;
|
---|
| 208 |
|
---|
| 209 | size_t arg_size = str_size(cur);
|
---|
| 210 | cur += arg_size + 1;
|
---|
| 211 | count++;
|
---|
| 212 | }
|
---|
| 213 | _argv[count] = NULL;
|
---|
[2f57690] | 214 |
|
---|
[472c09d] | 215 | /*
|
---|
| 216 | * Copy temporary data to global variables
|
---|
| 217 | */
|
---|
| 218 | if (arg_buf != NULL)
|
---|
| 219 | free(arg_buf);
|
---|
| 220 |
|
---|
| 221 | if (argv != NULL)
|
---|
| 222 | free(argv);
|
---|
| 223 |
|
---|
| 224 | argc = count;
|
---|
| 225 | arg_buf = buf;
|
---|
| 226 | argv = _argv;
|
---|
[c98e6ee] | 227 | }
|
---|
[2f57690] | 228 |
|
---|
[ffa2c8ef] | 229 | async_answer_0(rid, rc);
|
---|
[c98e6ee] | 230 | }
|
---|
| 231 |
|
---|
[bb9ec2d] | 232 | /** Receive a call setting inbox files of the program to execute.
|
---|
[bbdbf86] | 233 | *
|
---|
| 234 | * @param rid
|
---|
| 235 | * @param request
|
---|
| 236 | */
|
---|
[bb9ec2d] | 237 | static void ldr_add_inbox(ipc_callid_t rid, ipc_call_t *request)
|
---|
[bbdbf86] | 238 | {
|
---|
[bb9ec2d] | 239 | if (inbox_entries == INBOX_MAX_ENTRIES) {
|
---|
| 240 | async_answer_0(rid, ERANGE);
|
---|
[b7f69f2] | 241 | return;
|
---|
[bb9ec2d] | 242 | }
|
---|
[7171760] | 243 |
|
---|
[bb9ec2d] | 244 | ipc_callid_t writeid;
|
---|
| 245 | size_t namesize;
|
---|
| 246 | if (!async_data_write_receive(&writeid, &namesize)) {
|
---|
| 247 | async_answer_0(rid, EINVAL);
|
---|
| 248 | return;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | char* name = malloc(namesize);
|
---|
| 252 | int rc = async_data_write_finalize(writeid, name, namesize);
|
---|
| 253 | if (rc != EOK) {
|
---|
| 254 | async_answer_0(rid, EINVAL);
|
---|
| 255 | return;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | int file = vfs_receive_handle(true);
|
---|
| 259 | if (file < 0) {
|
---|
| 260 | async_answer_0(rid, EINVAL);
|
---|
| 261 | return;
|
---|
[bbdbf86] | 262 | }
|
---|
[7171760] | 263 |
|
---|
[ea56098] | 264 | /*
|
---|
| 265 | * We need to set the root early for dynamically linked binaries so
|
---|
| 266 | * that the loader can use it too.
|
---|
| 267 | */
|
---|
| 268 | if (str_cmp(name, "root") == 0)
|
---|
| 269 | vfs_root_set(file);
|
---|
| 270 |
|
---|
[bb9ec2d] | 271 | inbox[inbox_entries].name = name;
|
---|
| 272 | inbox[inbox_entries].file = file;
|
---|
| 273 | inbox_entries++;
|
---|
[ffa2c8ef] | 274 | async_answer_0(rid, EOK);
|
---|
[bbdbf86] | 275 | }
|
---|
| 276 |
|
---|
[4470e26] | 277 | /** Load the previously selected program.
|
---|
[c98e6ee] | 278 | *
|
---|
| 279 | * @param rid
|
---|
| 280 | * @param request
|
---|
| 281 | * @return 0 on success, !0 on error.
|
---|
| 282 | */
|
---|
[bbdbf86] | 283 | static int ldr_load(ipc_callid_t rid, ipc_call_t *request)
|
---|
[c98e6ee] | 284 | {
|
---|
[bb9ec2d] | 285 | int rc = elf_load(program_fd, &prog_info);
|
---|
[409b0d6] | 286 | if (rc != EE_OK) {
|
---|
[bb9ec2d] | 287 | DPRINTF("Failed to load executable for '%s'.\n", progname);
|
---|
[ffa2c8ef] | 288 | async_answer_0(rid, EINVAL);
|
---|
[c98e6ee] | 289 | return 1;
|
---|
| 290 | }
|
---|
[2f57690] | 291 |
|
---|
[17341d4] | 292 | elf_set_pcb(&prog_info, &pcb);
|
---|
[2f57690] | 293 |
|
---|
[622cdbe] | 294 | pcb.cwd = cwd;
|
---|
| 295 |
|
---|
[c98e6ee] | 296 | pcb.argc = argc;
|
---|
| 297 | pcb.argv = argv;
|
---|
[2f57690] | 298 |
|
---|
[bb9ec2d] | 299 | pcb.inbox = inbox;
|
---|
| 300 | pcb.inbox_entries = inbox_entries;
|
---|
[bbdbf86] | 301 |
|
---|
[a6dffb8] | 302 | async_answer_0(rid, rc);
|
---|
[c98e6ee] | 303 | return 0;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
[4470e26] | 306 | /** Run the previously loaded program.
|
---|
| 307 | *
|
---|
| 308 | * @param rid
|
---|
| 309 | * @param request
|
---|
| 310 | * @return 0 on success, !0 on error.
|
---|
| 311 | */
|
---|
[bbdbf86] | 312 | static void ldr_run(ipc_callid_t rid, ipc_call_t *request)
|
---|
[4470e26] | 313 | {
|
---|
[a6dffb8] | 314 | DPRINTF("Set task name\n");
|
---|
| 315 |
|
---|
[bc18d63] | 316 | /* Set the task name. */
|
---|
[bb9ec2d] | 317 | task_set_name(progname);
|
---|
[2f57690] | 318 |
|
---|
[a6dffb8] | 319 | /* Run program */
|
---|
| 320 | DPRINTF("Reply OK\n");
|
---|
| 321 | async_answer_0(rid, EOK);
|
---|
| 322 | DPRINTF("Jump to entry point at %p\n", pcb.entry);
|
---|
[17341d4] | 323 | entry_point_jmp(prog_info.finfo.entry, &pcb);
|
---|
[bbdbf86] | 324 |
|
---|
[4470e26] | 325 | /* Not reached */
|
---|
| 326 | }
|
---|
| 327 |
|
---|
[c98e6ee] | 328 | /** Handle loader connection.
|
---|
| 329 | *
|
---|
| 330 | * Receive and carry out commands (of which the last one should be
|
---|
| 331 | * to execute the loaded program).
|
---|
| 332 | */
|
---|
[9934f7d] | 333 | static void ldr_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
[c98e6ee] | 334 | {
|
---|
[bfd1546] | 335 | /* Already have a connection? */
|
---|
| 336 | if (connected) {
|
---|
[ffa2c8ef] | 337 | async_answer_0(iid, ELIMIT);
|
---|
[bfd1546] | 338 | return;
|
---|
| 339 | }
|
---|
[2f57690] | 340 |
|
---|
[bfd1546] | 341 | connected = true;
|
---|
| 342 |
|
---|
| 343 | /* Accept the connection */
|
---|
[ffa2c8ef] | 344 | async_answer_0(iid, EOK);
|
---|
[2f57690] | 345 |
|
---|
[c98e6ee] | 346 | /* Ignore parameters, the connection is already open */
|
---|
[2f57690] | 347 | (void) icall;
|
---|
| 348 |
|
---|
[79ae36dd] | 349 | while (true) {
|
---|
| 350 | int retval;
|
---|
| 351 | ipc_call_t call;
|
---|
| 352 | ipc_callid_t callid = async_get_call(&call);
|
---|
[2f57690] | 353 |
|
---|
[79ae36dd] | 354 | if (!IPC_GET_IMETHOD(call))
|
---|
[86e3d62] | 355 | exit(0);
|
---|
[79ae36dd] | 356 |
|
---|
| 357 | switch (IPC_GET_IMETHOD(call)) {
|
---|
[47e0a05b] | 358 | case LOADER_GET_TASKID:
|
---|
[bbdbf86] | 359 | ldr_get_taskid(callid, &call);
|
---|
[47e0a05b] | 360 | continue;
|
---|
[622cdbe] | 361 | case LOADER_SET_CWD:
|
---|
| 362 | ldr_set_cwd(callid, &call);
|
---|
| 363 | continue;
|
---|
[bb9ec2d] | 364 | case LOADER_SET_PROGRAM:
|
---|
| 365 | ldr_set_program(callid, &call);
|
---|
[c98e6ee] | 366 | continue;
|
---|
| 367 | case LOADER_SET_ARGS:
|
---|
[bbdbf86] | 368 | ldr_set_args(callid, &call);
|
---|
| 369 | continue;
|
---|
[bb9ec2d] | 370 | case LOADER_ADD_INBOX:
|
---|
| 371 | ldr_add_inbox(callid, &call);
|
---|
[4470e26] | 372 | continue;
|
---|
| 373 | case LOADER_LOAD:
|
---|
[bbdbf86] | 374 | ldr_load(callid, &call);
|
---|
[4470e26] | 375 | continue;
|
---|
[c98e6ee] | 376 | case LOADER_RUN:
|
---|
[bbdbf86] | 377 | ldr_run(callid, &call);
|
---|
[4470e26] | 378 | /* Not reached */
|
---|
[c98e6ee] | 379 | default:
|
---|
[8c3bc75] | 380 | retval = EINVAL;
|
---|
[c98e6ee] | 381 | break;
|
---|
| 382 | }
|
---|
[8c3bc75] | 383 |
|
---|
[79ae36dd] | 384 | async_answer_0(callid, retval);
|
---|
[c98e6ee] | 385 | }
|
---|
| 386 | }
|
---|
| 387 |
|
---|
| 388 | /** Program loader main function.
|
---|
| 389 | */
|
---|
| 390 | int main(int argc, char *argv[])
|
---|
| 391 | {
|
---|
[b688fd8] | 392 | async_set_fallback_port_handler(ldr_connection, NULL);
|
---|
[007e6efa] | 393 |
|
---|
[5d96851b] | 394 | /* Introduce this task to the NS (give it our task ID). */
|
---|
[007e6efa] | 395 | task_id_t id = task_get_id();
|
---|
[79ae36dd] | 396 | int rc = ns_intro(id);
|
---|
[5d96851b] | 397 | if (rc != EOK)
|
---|
[b39b5cb] | 398 | return rc;
|
---|
[2f57690] | 399 |
|
---|
[566992e1] | 400 | /* Create port */
|
---|
| 401 | port_id_t port;
|
---|
| 402 | rc = async_create_port(INTERFACE_LOADER, ldr_connection, NULL, &port);
|
---|
| 403 | if (rc != EOK)
|
---|
| 404 | return rc;
|
---|
| 405 |
|
---|
[bfd1546] | 406 | /* Register at naming service. */
|
---|
[566992e1] | 407 | rc = service_register(SERVICE_LOADER);
|
---|
[b39b5cb] | 408 | if (rc != EOK)
|
---|
| 409 | return rc;
|
---|
[007e6efa] | 410 |
|
---|
[c98e6ee] | 411 | async_manager();
|
---|
[2f57690] | 412 |
|
---|
[bfd1546] | 413 | /* Never reached */
|
---|
[c98e6ee] | 414 | return 0;
|
---|
| 415 | }
|
---|
| 416 |
|
---|
| 417 | /** @}
|
---|
| 418 | */
|
---|