Subversion Repositories HelenOS

Rev

Rev 3157 | Rev 3169 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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
  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.
  44.  */
  45.  
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48. #include <unistd.h>
  49. #include <fcntl.h>
  50. #include <sys/types.h>
  51. #include <ipc/ipc.h>
  52. #include <ipc/loader.h>
  53. #include <loader/pcb.h>
  54. #include <errno.h>
  55. #include <async.h>
  56. #include <as.h>
  57.  
  58. #include <elf.h>
  59. #include <elf_load.h>
  60. #include <arch/pcb.h>
  61.  
  62. /**
  63.  * Bias used for loading the dynamic linker. This will be soon replaced
  64.  * by automatic placement.
  65.  */
  66. #define RTLD_BIAS 0x80000
  67.  
  68. /** Pathname of the file that will be loaded */
  69. static char *pathname = NULL;
  70.  
  71. /** Receive a call setting pathname of the program to execute.
  72.  *
  73.  * @param rid
  74.  * @param request
  75.  */
  76. static void iloader_set_pathname(ipc_callid_t rid, ipc_call_t *request)
  77. {
  78.     ipc_callid_t callid;
  79.     size_t len;
  80.     char *name_buf;
  81.  
  82.     if (!ipc_data_write_receive(&callid, &len)) {
  83.         ipc_answer_0(callid, EINVAL);
  84.         ipc_answer_0(rid, EINVAL);
  85.         return;
  86.     }
  87.  
  88.     name_buf = malloc(len + 1);
  89.     if (!name_buf) {
  90.         ipc_answer_0(callid, ENOMEM);
  91.         ipc_answer_0(rid, ENOMEM);
  92.         return;
  93.     }
  94.  
  95.     ipc_data_write_finalize(callid, name_buf, len);
  96.     ipc_answer_0(rid, EOK);
  97.  
  98.     if (pathname != NULL) {
  99.         free(pathname);
  100.         pathname = NULL;
  101.     }
  102.  
  103.     name_buf[len] = '\0';
  104.     pathname = name_buf;
  105. }
  106.  
  107. /** Load and run the previously selected program.
  108.  *
  109.  * @param rid
  110.  * @param request
  111.  * @return 0 on success, !0 on error.
  112.  */
  113. static int iloader_run(ipc_callid_t rid, ipc_call_t *request)
  114. {
  115.     int rc;
  116.     pcb_t *pcb;
  117.  
  118.     elf_info_t prog_info;
  119.     elf_info_t interp_info;
  120.  
  121. //  printf("Load program '%s'\n", pathname);
  122.  
  123.     rc = elf_load_file(pathname, 0, &prog_info);
  124.     if (rc < 0) {
  125.         printf("failed to load program\n");
  126.         ipc_answer_0(rid, EINVAL);
  127.         return 1;
  128.     }
  129.  
  130. //  printf("Create PCB\n");
  131.     if (elf_create_pcb(&prog_info) < 0) {
  132.         ipc_answer_0(rid, ENOMEM);
  133.         return 1;
  134.     }
  135.  
  136.     if (prog_info.interp == NULL) {
  137.         /* Statically linked program */
  138. //      printf("Run statically linked program\n");
  139. //      printf("entry point: 0x%llx\n", prog_info.entry);
  140.         ipc_answer_0(rid, EOK);
  141.         close_console();
  142.         elf_run(&prog_info);
  143.         return 0;
  144.     }
  145.  
  146.     printf("Load dynamic linker '%s'\n", prog_info.interp);
  147.     rc = elf_load_file("/rtld.so", RTLD_BIAS, &interp_info);
  148.     if (rc < 0) {
  149.         printf("failed to load dynamic linker\n");
  150.         ipc_answer_0(rid, EINVAL);
  151.         return 1;
  152.     }
  153.  
  154.     /*
  155.      * Provide dynamic linker with some useful data
  156.      */
  157.     pcb = (pcb_t *)PCB_ADDRESS;
  158.     pcb->rtld_dynamic = interp_info.dynamic;
  159.     pcb->rtld_bias = RTLD_BIAS;
  160.  
  161.     printf("run dynamic linker\n");
  162.     printf("entry point: 0x%llx\n", interp_info.entry);
  163.     close_console();
  164.  
  165.     ipc_answer_0(rid, EOK);
  166.     elf_run(&interp_info);
  167.  
  168.     /* Not reached(?) */
  169.     return 0;
  170. }
  171.  
  172. /** Handle loader connection.
  173.  *
  174.  * Receive and carry out commands (of which the last one should be
  175.  * to execute the loaded program).
  176.  */
  177. static void loader_connection(ipc_callid_t iid, ipc_call_t *icall)
  178. {
  179.     ipc_callid_t callid;
  180.     ipc_call_t call;
  181.     int retval;
  182.  
  183.     /* Ignore parameters, the connection is already open */
  184.     (void)iid; (void)icall;
  185.  
  186.     while (1) {
  187.         callid = async_get_call(&call);
  188. //      printf("received call from phone %d, method=%d\n",
  189. //          call.in_phone_hash, IPC_GET_METHOD(call));
  190.         switch (IPC_GET_METHOD(call)) {
  191.         case LOADER_SET_PATHNAME:
  192.             iloader_set_pathname(callid, &call);
  193.             continue;
  194.         case LOADER_RUN:
  195.             iloader_run(callid, &call);
  196.             exit(0);
  197.             continue;
  198.         default:
  199.             retval = ENOENT;
  200.             break;
  201.         }
  202.         if ((callid & IPC_CALLID_NOTIFICATION) == 0) {
  203.             printf("responding EINVAL to method %d\n", IPC_GET_METHOD(call));
  204.             ipc_answer_0(callid, EINVAL);
  205.         }
  206.     }
  207. }
  208.  
  209. /** Program loader main function.
  210.  */
  211. int main(int argc, char *argv[])
  212. {
  213.     ipc_callid_t callid;
  214.     ipc_call_t call;
  215.     ipcarg_t phone_hash;
  216.  
  217.     /* The first call only communicates the incoming phone hash */
  218.     callid = ipc_wait_for_call(&call);
  219.  
  220.     if (IPC_GET_METHOD(call) != LOADER_HELLO) {
  221.         if (IPC_GET_METHOD(call) != IPC_M_PHONE_HUNGUP)
  222.             ipc_answer_0(callid, EINVAL);
  223.         return 1;
  224.     }
  225.  
  226.     ipc_answer_0(callid, EOK);
  227.     phone_hash = call.in_phone_hash;
  228.  
  229.     /*
  230.      * FIXME: up until now no async calls can be used!!!
  231.      * (Which means e.g. printf() cannot be used)
  232.      */
  233.     async_new_connection(phone_hash, 0, NULL, loader_connection);
  234.     async_manager();
  235.  
  236.     /* not reached */
  237.     return 0;
  238. }
  239.  
  240. /** @}
  241.  */
  242.