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