Subversion Repositories HelenOS

Rev

Rev 3170 | Rev 3400 | 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>
49
#include <fcntl.h>
50
#include <sys/types.h>
3004 svoboda 51
#include <ipc/ipc.h>
3148 svoboda 52
#include <ipc/loader.h>
3160 svoboda 53
#include <loader/pcb.h>
3004 svoboda 54
#include <errno.h>
3148 svoboda 55
#include <async.h>
2959 svoboda 56
#include <as.h>
2928 svoboda 57
 
2969 svoboda 58
#include <elf.h>
59
#include <elf_load.h>
2928 svoboda 60
 
3101 svoboda 61
/**
62
 * Bias used for loading the dynamic linker. This will be soon replaced
63
 * by automatic placement.
64
 */
2972 svoboda 65
#define RTLD_BIAS 0x80000
66
 
3101 svoboda 67
/** Pathname of the file that will be loaded */
3004 svoboda 68
static char *pathname = NULL;
69
 
3170 svoboda 70
/** The Program control block */
71
static pcb_t pcb;
72
 
3101 svoboda 73
/** Receive a call setting pathname of the program to execute.
74
 *
75
 * @param rid
76
 * @param request
77
 */
3148 svoboda 78
static void iloader_set_pathname(ipc_callid_t rid, ipc_call_t *request)
2928 svoboda 79
{
3148 svoboda 80
    ipc_callid_t callid;
3004 svoboda 81
    size_t len;
82
    char *name_buf;
83
 
84
    if (!ipc_data_write_receive(&callid, &len)) {
85
        ipc_answer_0(callid, EINVAL);
86
        ipc_answer_0(rid, EINVAL);
87
        return;
88
    }
3148 svoboda 89
 
3004 svoboda 90
    name_buf = malloc(len + 1);
91
    if (!name_buf) {
3148 svoboda 92
        ipc_answer_0(callid, ENOMEM);
3004 svoboda 93
        ipc_answer_0(rid, ENOMEM);
94
        return;
95
    }
96
 
3148 svoboda 97
    ipc_data_write_finalize(callid, name_buf, len);
98
    ipc_answer_0(rid, EOK);
3004 svoboda 99
 
100
    if (pathname != NULL) {
101
        free(pathname);
102
        pathname = NULL;
103
    }
104
 
3148 svoboda 105
    name_buf[len] = '\0';
3004 svoboda 106
    pathname = name_buf;
107
}
108
 
3101 svoboda 109
/** Load and run the previously selected program.
110
 *
111
 * @param rid
112
 * @param request
113
 * @return 0 on success, !0 on error.
114
 */
3148 svoboda 115
static int iloader_run(ipc_callid_t rid, ipc_call_t *request)
3004 svoboda 116
{
117
    int rc;
118
 
2964 svoboda 119
    elf_info_t prog_info;
120
    elf_info_t interp_info;
2928 svoboda 121
 
3155 svoboda 122
//  printf("Load program '%s'\n", pathname);
2928 svoboda 123
 
3004 svoboda 124
    rc = elf_load_file(pathname, 0, &prog_info);
2928 svoboda 125
    if (rc < 0) {
126
        printf("failed to load program\n");
3155 svoboda 127
        ipc_answer_0(rid, EINVAL);
2928 svoboda 128
        return 1;
129
    }
130
 
3155 svoboda 131
//  printf("Create PCB\n");
3170 svoboda 132
    elf_create_pcb(&prog_info, &pcb);
2928 svoboda 133
 
3171 svoboda 134
    pcb.argc = 0;
135
    pcb.argv = NULL;
136
 
3004 svoboda 137
    if (prog_info.interp == NULL) {
138
        /* Statically linked program */
3155 svoboda 139
//      printf("Run statically linked program\n");
140
//      printf("entry point: 0x%llx\n", prog_info.entry);
141
        ipc_answer_0(rid, EOK);
142
        close_console();
3170 svoboda 143
        elf_run(&prog_info, &pcb);
3004 svoboda 144
        return 0;
145
    }
2988 svoboda 146
 
3004 svoboda 147
    printf("Load dynamic linker '%s'\n", prog_info.interp);
148
    rc = elf_load_file("/rtld.so", RTLD_BIAS, &interp_info);
2959 svoboda 149
    if (rc < 0) {
150
        printf("failed to load dynamic linker\n");
3155 svoboda 151
        ipc_answer_0(rid, EINVAL);
2959 svoboda 152
        return 1;
153
    }
154
 
2989 svoboda 155
    /*
3004 svoboda 156
     * Provide dynamic linker with some useful data
2989 svoboda 157
     */
3170 svoboda 158
    pcb.rtld_dynamic = interp_info.dynamic;
159
    pcb.rtld_bias = RTLD_BIAS;
2989 svoboda 160
 
2959 svoboda 161
    printf("run dynamic linker\n");
3155 svoboda 162
    printf("entry point: 0x%llx\n", interp_info.entry);
163
    close_console();
164
 
165
    ipc_answer_0(rid, EOK);
3170 svoboda 166
    elf_run(&interp_info, &pcb);
2989 svoboda 167
 
3170 svoboda 168
    /* Not reached */
3004 svoboda 169
    return 0;
170
}
171
 
3148 svoboda 172
/** Handle loader connection.
3101 svoboda 173
 *
174
 * Receive and carry out commands (of which the last one should be
175
 * to execute the loaded program).
176
 */
3148 svoboda 177
static void loader_connection(ipc_callid_t iid, ipc_call_t *icall)
3004 svoboda 178
{
179
    ipc_callid_t callid;
180
    ipc_call_t call;
181
    int retval;
182
 
3148 svoboda 183
    /* Ignore parameters, the connection is already open */
184
    (void)iid; (void)icall;
185
 
3004 svoboda 186
    while (1) {
3148 svoboda 187
        callid = async_get_call(&call);
3155 svoboda 188
//      printf("received call from phone %d, method=%d\n",
189
//          call.in_phone_hash, IPC_GET_METHOD(call));
3004 svoboda 190
        switch (IPC_GET_METHOD(call)) {
3148 svoboda 191
        case LOADER_SET_PATHNAME:
3004 svoboda 192
            iloader_set_pathname(callid, &call);
3148 svoboda 193
            continue;
194
        case LOADER_RUN:
3004 svoboda 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
    }
3148 svoboda 207
}
3004 svoboda 208
 
3148 svoboda 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
    /*
3170 svoboda 230
     * Up until now async must not be used as it couldn't
231
     * handle incoming requests. (Which means e.g. printf()
232
     * cannot be used)
3148 svoboda 233
     */
234
    async_new_connection(phone_hash, 0, NULL, loader_connection);
235
    async_manager();
236
 
2928 svoboda 237
    /* not reached */
238
    return 0;
239
}
240
 
241
/** @}
242
 */