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