Subversion Repositories HelenOS

Rev

Rev 3170 | Rev 3174 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3170 Rev 3171
1
/*
1
/*
2
 * Copyright (c) 2008 Jiri Svoboda
2
 * Copyright (c) 2008 Jiri Svoboda
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup loader
29
/** @addtogroup loader
30
 * @brief   Loads and runs programs from VFS.
30
 * @brief   Loads and runs programs from VFS.
31
 * @{
31
 * @{
32
 */
32
 */
33
/**
33
/**
34
 * @file
34
 * @file
35
 * @brief   Loads and runs programs from VFS.
35
 * @brief   Loads and runs programs from VFS.
36
 *
36
 *
37
 * The program loader is a special init binary. Its image is used
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
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.
39
 * returns the id of a phone connected to the newly created task.
40
 *
40
 *
41
 * The caller uses this phone to send the pathname and various other
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
42
 * information to the loader. This is normally done by the C library
43
 * and completely hidden from applications.
43
 * and completely hidden from applications.
44
 */
44
 */
45
 
45
 
46
#include <stdio.h>
46
#include <stdio.h>
47
#include <stdlib.h>
47
#include <stdlib.h>
48
#include <unistd.h>
48
#include <unistd.h>
49
#include <fcntl.h>
49
#include <fcntl.h>
50
#include <sys/types.h>
50
#include <sys/types.h>
51
#include <ipc/ipc.h>
51
#include <ipc/ipc.h>
52
#include <ipc/loader.h>
52
#include <ipc/loader.h>
53
#include <loader/pcb.h>
53
#include <loader/pcb.h>
54
#include <errno.h>
54
#include <errno.h>
55
#include <async.h>
55
#include <async.h>
56
#include <as.h>
56
#include <as.h>
57
 
57
 
58
#include <elf.h>
58
#include <elf.h>
59
#include <elf_load.h>
59
#include <elf_load.h>
60
 
60
 
61
/**
61
/**
62
 * Bias used for loading the dynamic linker. This will be soon replaced
62
 * Bias used for loading the dynamic linker. This will be soon replaced
63
 * by automatic placement.
63
 * by automatic placement.
64
 */
64
 */
65
#define RTLD_BIAS 0x80000
65
#define RTLD_BIAS 0x80000
66
 
66
 
67
/** Pathname of the file that will be loaded */
67
/** Pathname of the file that will be loaded */
68
static char *pathname = NULL;
68
static char *pathname = NULL;
69
 
69
 
70
/** The Program control block */
70
/** The Program control block */
71
static pcb_t pcb;
71
static pcb_t pcb;
72
 
72
 
73
/** Receive a call setting pathname of the program to execute.
73
/** Receive a call setting pathname of the program to execute.
74
 *
74
 *
75
 * @param rid
75
 * @param rid
76
 * @param request
76
 * @param request
77
 */
77
 */
78
static void iloader_set_pathname(ipc_callid_t rid, ipc_call_t *request)
78
static void iloader_set_pathname(ipc_callid_t rid, ipc_call_t *request)
79
{
79
{
80
    ipc_callid_t callid;
80
    ipc_callid_t callid;
81
    size_t len;
81
    size_t len;
82
    char *name_buf;
82
    char *name_buf;
83
 
83
 
84
    if (!ipc_data_write_receive(&callid, &len)) {
84
    if (!ipc_data_write_receive(&callid, &len)) {
85
        ipc_answer_0(callid, EINVAL);
85
        ipc_answer_0(callid, EINVAL);
86
        ipc_answer_0(rid, EINVAL);
86
        ipc_answer_0(rid, EINVAL);
87
        return;
87
        return;
88
    }
88
    }
89
 
89
 
90
    name_buf = malloc(len + 1);
90
    name_buf = malloc(len + 1);
91
    if (!name_buf) {
91
    if (!name_buf) {
92
        ipc_answer_0(callid, ENOMEM);
92
        ipc_answer_0(callid, ENOMEM);
93
        ipc_answer_0(rid, ENOMEM);
93
        ipc_answer_0(rid, ENOMEM);
94
        return;
94
        return;
95
    }
95
    }
96
 
96
 
97
    ipc_data_write_finalize(callid, name_buf, len);
97
    ipc_data_write_finalize(callid, name_buf, len);
98
    ipc_answer_0(rid, EOK);
98
    ipc_answer_0(rid, EOK);
99
 
99
 
100
    if (pathname != NULL) {
100
    if (pathname != NULL) {
101
        free(pathname);
101
        free(pathname);
102
        pathname = NULL;
102
        pathname = NULL;
103
    }
103
    }
104
 
104
 
105
    name_buf[len] = '\0';
105
    name_buf[len] = '\0';
106
    pathname = name_buf;
106
    pathname = name_buf;
107
}
107
}
108
 
108
 
109
/** Load and run the previously selected program.
109
/** Load and run the previously selected program.
110
 *
110
 *
111
 * @param rid
111
 * @param rid
112
 * @param request
112
 * @param request
113
 * @return 0 on success, !0 on error.
113
 * @return 0 on success, !0 on error.
114
 */
114
 */
115
static int iloader_run(ipc_callid_t rid, ipc_call_t *request)
115
static int iloader_run(ipc_callid_t rid, ipc_call_t *request)
116
{
116
{
117
    int rc;
117
    int rc;
118
 
118
 
119
    elf_info_t prog_info;
119
    elf_info_t prog_info;
120
    elf_info_t interp_info;
120
    elf_info_t interp_info;
121
 
121
 
122
//  printf("Load program '%s'\n", pathname);
122
//  printf("Load program '%s'\n", pathname);
123
 
123
 
124
    rc = elf_load_file(pathname, 0, &prog_info);
124
    rc = elf_load_file(pathname, 0, &prog_info);
125
    if (rc < 0) {
125
    if (rc < 0) {
126
        printf("failed to load program\n");
126
        printf("failed to load program\n");
127
        ipc_answer_0(rid, EINVAL);
127
        ipc_answer_0(rid, EINVAL);
128
        return 1;
128
        return 1;
129
    }
129
    }
130
 
130
 
131
//  printf("Create PCB\n");
131
//  printf("Create PCB\n");
132
    elf_create_pcb(&prog_info, &pcb);
132
    elf_create_pcb(&prog_info, &pcb);
133
 
133
 
-
 
134
    pcb.argc = 0;
-
 
135
    pcb.argv = NULL;
-
 
136
 
134
    if (prog_info.interp == NULL) {
137
    if (prog_info.interp == NULL) {
135
        /* Statically linked program */
138
        /* Statically linked program */
136
//      printf("Run statically linked program\n");
139
//      printf("Run statically linked program\n");
137
//      printf("entry point: 0x%llx\n", prog_info.entry);
140
//      printf("entry point: 0x%llx\n", prog_info.entry);
138
        ipc_answer_0(rid, EOK);
141
        ipc_answer_0(rid, EOK);
139
        close_console();
142
        close_console();
140
        elf_run(&prog_info, &pcb);
143
        elf_run(&prog_info, &pcb);
141
        return 0;
144
        return 0;
142
    }
145
    }
143
 
146
 
144
    printf("Load dynamic linker '%s'\n", prog_info.interp);
147
    printf("Load dynamic linker '%s'\n", prog_info.interp);
145
    rc = elf_load_file("/rtld.so", RTLD_BIAS, &interp_info);
148
    rc = elf_load_file("/rtld.so", RTLD_BIAS, &interp_info);
146
    if (rc < 0) {
149
    if (rc < 0) {
147
        printf("failed to load dynamic linker\n");
150
        printf("failed to load dynamic linker\n");
148
        ipc_answer_0(rid, EINVAL);
151
        ipc_answer_0(rid, EINVAL);
149
        return 1;
152
        return 1;
150
    }
153
    }
151
 
154
 
152
    /*
155
    /*
153
     * Provide dynamic linker with some useful data
156
     * Provide dynamic linker with some useful data
154
     */
157
     */
155
    pcb.rtld_dynamic = interp_info.dynamic;
158
    pcb.rtld_dynamic = interp_info.dynamic;
156
    pcb.rtld_bias = RTLD_BIAS;
159
    pcb.rtld_bias = RTLD_BIAS;
157
 
160
 
158
    printf("run dynamic linker\n");
161
    printf("run dynamic linker\n");
159
    printf("entry point: 0x%llx\n", interp_info.entry);
162
    printf("entry point: 0x%llx\n", interp_info.entry);
160
    close_console();
163
    close_console();
161
 
164
 
162
    ipc_answer_0(rid, EOK);
165
    ipc_answer_0(rid, EOK);
163
    elf_run(&interp_info, &pcb);
166
    elf_run(&interp_info, &pcb);
164
 
167
 
165
    /* Not reached */
168
    /* Not reached */
166
    return 0;
169
    return 0;
167
}
170
}
168
 
171
 
169
/** Handle loader connection.
172
/** Handle loader connection.
170
 *
173
 *
171
 * Receive and carry out commands (of which the last one should be
174
 * Receive and carry out commands (of which the last one should be
172
 * to execute the loaded program).
175
 * to execute the loaded program).
173
 */
176
 */
174
static void loader_connection(ipc_callid_t iid, ipc_call_t *icall)
177
static void loader_connection(ipc_callid_t iid, ipc_call_t *icall)
175
{
178
{
176
    ipc_callid_t callid;
179
    ipc_callid_t callid;
177
    ipc_call_t call;
180
    ipc_call_t call;
178
    int retval;
181
    int retval;
179
 
182
 
180
    /* Ignore parameters, the connection is already open */
183
    /* Ignore parameters, the connection is already open */
181
    (void)iid; (void)icall;
184
    (void)iid; (void)icall;
182
 
185
 
183
    while (1) {
186
    while (1) {
184
        callid = async_get_call(&call);
187
        callid = async_get_call(&call);
185
//      printf("received call from phone %d, method=%d\n",
188
//      printf("received call from phone %d, method=%d\n",
186
//          call.in_phone_hash, IPC_GET_METHOD(call));
189
//          call.in_phone_hash, IPC_GET_METHOD(call));
187
        switch (IPC_GET_METHOD(call)) {
190
        switch (IPC_GET_METHOD(call)) {
188
        case LOADER_SET_PATHNAME:
191
        case LOADER_SET_PATHNAME:
189
            iloader_set_pathname(callid, &call);
192
            iloader_set_pathname(callid, &call);
190
            continue;
193
            continue;
191
        case LOADER_RUN:
194
        case LOADER_RUN:
192
            iloader_run(callid, &call);
195
            iloader_run(callid, &call);
193
            exit(0);
196
            exit(0);
194
            continue;
197
            continue;
195
        default:
198
        default:
196
            retval = ENOENT;
199
            retval = ENOENT;
197
            break;
200
            break;
198
        }
201
        }
199
        if ((callid & IPC_CALLID_NOTIFICATION) == 0) {
202
        if ((callid & IPC_CALLID_NOTIFICATION) == 0) {
200
            printf("responding EINVAL to method %d\n", IPC_GET_METHOD(call));
203
            printf("responding EINVAL to method %d\n", IPC_GET_METHOD(call));
201
            ipc_answer_0(callid, EINVAL);
204
            ipc_answer_0(callid, EINVAL);
202
        }
205
        }
203
    }
206
    }
204
}
207
}
205
 
208
 
206
/** Program loader main function.
209
/** Program loader main function.
207
 */
210
 */
208
int main(int argc, char *argv[])
211
int main(int argc, char *argv[])
209
{
212
{
210
    ipc_callid_t callid;
213
    ipc_callid_t callid;
211
    ipc_call_t call;
214
    ipc_call_t call;
212
    ipcarg_t phone_hash;
215
    ipcarg_t phone_hash;
213
 
216
 
214
    /* The first call only communicates the incoming phone hash */
217
    /* The first call only communicates the incoming phone hash */
215
    callid = ipc_wait_for_call(&call);
218
    callid = ipc_wait_for_call(&call);
216
 
219
 
217
    if (IPC_GET_METHOD(call) != LOADER_HELLO) {
220
    if (IPC_GET_METHOD(call) != LOADER_HELLO) {
218
        if (IPC_GET_METHOD(call) != IPC_M_PHONE_HUNGUP)
221
        if (IPC_GET_METHOD(call) != IPC_M_PHONE_HUNGUP)
219
            ipc_answer_0(callid, EINVAL);
222
            ipc_answer_0(callid, EINVAL);
220
        return 1;
223
        return 1;
221
    }
224
    }
222
 
225
 
223
    ipc_answer_0(callid, EOK);
226
    ipc_answer_0(callid, EOK);
224
    phone_hash = call.in_phone_hash;
227
    phone_hash = call.in_phone_hash;
225
 
228
 
226
    /*
229
    /*
227
     * Up until now async must not be used as it couldn't
230
     * Up until now async must not be used as it couldn't
228
     * handle incoming requests. (Which means e.g. printf()
231
     * handle incoming requests. (Which means e.g. printf()
229
     * cannot be used)
232
     * cannot be used)
230
     */
233
     */
231
    async_new_connection(phone_hash, 0, NULL, loader_connection);
234
    async_new_connection(phone_hash, 0, NULL, loader_connection);
232
    async_manager();
235
    async_manager();
233
 
236
 
234
    /* not reached */
237
    /* not reached */
235
    return 0;
238
    return 0;
236
}
239
}
237
 
240
 
238
/** @}
241
/** @}
239
 */
242
 */
240
 
243