Subversion Repositories HelenOS

Rev

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

Rev 3004 Rev 3101
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.
-
 
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.
35
 */
44
 */
36
 
45
 
37
#include <stdio.h>
46
#include <stdio.h>
38
#include <stdlib.h>
47
#include <stdlib.h>
39
#include <unistd.h>
48
#include <unistd.h>
40
#include <fcntl.h>
49
#include <fcntl.h>
41
#include <sys/types.h>
50
#include <sys/types.h>
42
#include <ipc/ipc.h>
51
#include <ipc/ipc.h>
43
#include <errno.h>
52
#include <errno.h>
44
#include <as.h>
53
#include <as.h>
45
 
54
 
46
#include <elf.h>
55
#include <elf.h>
47
#include <elf_load.h>
56
#include <elf_load.h>
48
#include <pcb.h>
57
#include <pcb.h>
49
 
58
 
-
 
59
/**
-
 
60
 * Bias used for loading the dynamic linker. This will be soon replaced
-
 
61
 * by automatic placement.
-
 
62
 */
50
#define RTLD_BIAS 0x80000
63
#define RTLD_BIAS 0x80000
51
 
64
 
-
 
65
/** Pathname of the file that will be loaded */
52
static char *pathname = NULL;
66
static char *pathname = NULL;
53
 
67
 
-
 
68
/** Receive a call setting pathname of the program to execute.
-
 
69
 *
-
 
70
 * @param rid
-
 
71
 * @param request
-
 
72
 */
54
void iloader_set_pathname(ipc_callid_t rid, ipc_call_t *request)
73
void iloader_set_pathname(ipc_callid_t rid, ipc_call_t *request)
55
{
74
{
56
//  ipc_callid_t callid;
75
//  ipc_callid_t callid;
57
    size_t len;
76
    size_t len;
58
    char *name_buf;
77
    char *name_buf;
59
 
78
 
60
/*  printf("iloader_set_pathname\n");
79
/*  printf("iloader_set_pathname\n");
61
    if (!ipc_data_write_receive(&callid, &len)) {
80
    if (!ipc_data_write_receive(&callid, &len)) {
62
        ipc_answer_0(callid, EINVAL);
81
        ipc_answer_0(callid, EINVAL);
63
        ipc_answer_0(rid, EINVAL);
82
        ipc_answer_0(rid, EINVAL);
64
        return;
83
        return;
65
    }
84
    }
66
*/
85
*/
67
    len = IPC_GET_ARG2(*request);
86
    len = IPC_GET_ARG2(*request);
68
    printf("alloc %d bytes\n", len+1);
87
    printf("alloc %d bytes\n", len+1);
69
 
88
 
70
    name_buf = malloc(len + 1);
89
    name_buf = malloc(len + 1);
71
    if (!name_buf) {
90
    if (!name_buf) {
72
//      ipc_answer_0(callid, ENOMEM);
91
//      ipc_answer_0(callid, ENOMEM);
73
        ipc_answer_0(rid, ENOMEM);
92
        ipc_answer_0(rid, ENOMEM);
74
        return;
93
        return;
75
    }
94
    }
76
 
95
 
77
    printf("write_finalize\n");
96
    printf("write_finalize\n");
78
    ipc_data_write_finalize(rid, name_buf, len);
97
    ipc_data_write_finalize(rid, name_buf, len);
79
//  ipc_answer_0(rid, EOK);
98
//  ipc_answer_0(rid, EOK);
80
 
99
 
81
    if (pathname != NULL) {
100
    if (pathname != NULL) {
82
        free(pathname);
101
        free(pathname);
83
        pathname = NULL;
102
        pathname = NULL;
84
    }
103
    }
85
 
104
 
86
    pathname = name_buf;
105
    pathname = name_buf;
87
}
106
}
88
 
107
 
-
 
108
/** Load and run the previously selected program.
-
 
109
 *
-
 
110
 * @param rid
-
 
111
 * @param request
-
 
112
 * @return 0 on success, !0 on error.
-
 
113
 */
89
int iloader_run(ipc_callid_t rid, ipc_call_t *request)
114
int iloader_run(ipc_callid_t rid, ipc_call_t *request)
90
{
115
{
91
    int rc;
116
    int rc;
92
    pcb_t *pcb;
117
    pcb_t *pcb;
93
 
118
 
94
    elf_info_t prog_info;
119
    elf_info_t prog_info;
95
    elf_info_t interp_info;
120
    elf_info_t interp_info;
96
 
121
 
97
    printf("Load program '%s'\n", pathname);
122
    printf("Load program '%s'\n", pathname);
98
 
123
 
99
    rc = elf_load_file(pathname, 0, &prog_info);
124
    rc = elf_load_file(pathname, 0, &prog_info);
100
    if (rc < 0) {
125
    if (rc < 0) {
101
        printf("failed to load program\n");
126
        printf("failed to load program\n");
102
        return 1;
127
        return 1;
103
    }
128
    }
104
 
129
 
105
    printf("Create PCB\n");
130
    printf("Create PCB\n");
106
    if (elf_create_pcb(&prog_info) < 0) return 1;
131
    if (elf_create_pcb(&prog_info) < 0) return 1;
107
 
132
 
108
    if (prog_info.interp == NULL) {
133
    if (prog_info.interp == NULL) {
109
        /* Statically linked program */
134
        /* Statically linked program */
110
        printf("Run statically linked program\n");
135
        printf("Run statically linked program\n");
111
        elf_run(&prog_info);
136
        elf_run(&prog_info);
112
        return 0;
137
        return 0;
113
    }
138
    }
114
 
139
 
115
    printf("Load dynamic linker '%s'\n", prog_info.interp);
140
    printf("Load dynamic linker '%s'\n", prog_info.interp);
116
    rc = elf_load_file("/rtld.so", RTLD_BIAS, &interp_info);
141
    rc = elf_load_file("/rtld.so", RTLD_BIAS, &interp_info);
117
    if (rc < 0) {
142
    if (rc < 0) {
118
        printf("failed to load dynamic linker\n");
143
        printf("failed to load dynamic linker\n");
119
        return 1;
144
        return 1;
120
    }
145
    }
121
 
146
 
122
    /*
147
    /*
123
     * Provide dynamic linker with some useful data
148
     * Provide dynamic linker with some useful data
124
     */
149
     */
125
    pcb = (pcb_t *)PCB_ADDRESS;
150
    pcb = (pcb_t *)PCB_ADDRESS;
126
    pcb->rtld_dynamic = interp_info.dynamic;
151
    pcb->rtld_dynamic = interp_info.dynamic;
127
    pcb->rtld_bias = RTLD_BIAS;
152
    pcb->rtld_bias = RTLD_BIAS;
128
 
153
 
129
    printf("run dynamic linker\n");
154
    printf("run dynamic linker\n");
130
    elf_run(&interp_info);
155
    elf_run(&interp_info);
131
 
156
 
132
    return 0;
157
    return 0;
133
}
158
}
134
 
159
 
-
 
160
/** Program loader main function.
-
 
161
 *
-
 
162
 * Receive and carry out commands (of which the last one should be
-
 
163
 * to execute the loaded program).
-
 
164
 */
135
int main(int argc, char *argv[])
165
int main(int argc, char *argv[])
136
{
166
{
137
    ipc_callid_t callid;
167
    ipc_callid_t callid;
138
    ipc_call_t call;
168
    ipc_call_t call;
139
    int retval;
169
    int retval;
140
    int len;
170
    int len;
141
 
171
 
142
    while (1) {
172
    while (1) {
143
        callid = ipc_wait_for_call(&call);
173
        callid = ipc_wait_for_call(&call);
144
        printf("received call, method=%d\n", IPC_GET_METHOD(call));
174
        printf("received call, method=%d\n", IPC_GET_METHOD(call));
145
        switch (IPC_GET_METHOD(call)) {
175
        switch (IPC_GET_METHOD(call)) {
146
        case IPC_M_DATA_WRITE:
176
        case IPC_M_DATA_WRITE:
147
            iloader_set_pathname(callid, &call);
177
            iloader_set_pathname(callid, &call);
148
            iloader_run(callid, &call);
178
            iloader_run(callid, &call);
149
            exit(0);
179
            exit(0);
150
            continue;
180
            continue;
151
        default:
181
        default:
152
            retval = ENOENT;
182
            retval = ENOENT;
153
            break;
183
            break;
154
        }
184
        }
155
        if ((callid & IPC_CALLID_NOTIFICATION) == 0) {
185
        if ((callid & IPC_CALLID_NOTIFICATION) == 0) {
156
            printf("responding EINVAL to method %d\n", IPC_GET_METHOD(call));
186
            printf("responding EINVAL to method %d\n", IPC_GET_METHOD(call));
157
            ipc_answer_0(callid, EINVAL);
187
            ipc_answer_0(callid, EINVAL);
158
        }
188
        }
159
    }
189
    }
160
 
190
 
161
    /* not reached */
191
    /* not reached */
162
    return 0;
192
    return 0;
163
}
193
}
164
 
194
 
165
/** @}
195
/** @}
166
 */
196
 */
167
 
197