Rev 3204 | Rev 4344 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3204 | Rev 3569 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2001-2004 Jakub Jermar |
2 | * Copyright (c) 2001-2004 Jakub Jermar |
3 | * Copyright (c) 2008 Jiri Svoboda |
3 | * Copyright (c) 2008 Jiri Svoboda |
4 | * All rights reserved. |
4 | * All rights reserved. |
5 | * |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
7 | * modification, are permitted provided that the following conditions |
8 | * are met: |
8 | * are met: |
9 | * |
9 | * |
10 | * - Redistributions of source code must retain the above copyright |
10 | * - Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. |
11 | * notice, this list of conditions and the following disclaimer. |
12 | * - Redistributions in binary form must reproduce the above copyright |
12 | * - Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
14 | * documentation and/or other materials provided with the distribution. |
15 | * - The name of the author may not be used to endorse or promote products |
15 | * - The name of the author may not be used to endorse or promote products |
16 | * derived from this software without specific prior written permission. |
16 | * derived from this software without specific prior written permission. |
17 | * |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
28 | */ |
29 | 29 | ||
30 | /** @addtogroup genericproc |
30 | /** @addtogroup genericproc |
31 | * @{ |
31 | * @{ |
32 | */ |
32 | */ |
33 | 33 | ||
34 | /** |
34 | /** |
35 | * @file |
35 | * @file |
36 | * @brief Running userspace programs. |
36 | * @brief Running userspace programs. |
37 | */ |
37 | */ |
38 | 38 | ||
39 | #include <main/uinit.h> |
39 | #include <main/uinit.h> |
40 | #include <proc/thread.h> |
40 | #include <proc/thread.h> |
41 | #include <proc/task.h> |
41 | #include <proc/task.h> |
42 | #include <proc/uarg.h> |
42 | #include <proc/uarg.h> |
43 | #include <mm/as.h> |
43 | #include <mm/as.h> |
44 | #include <mm/slab.h> |
44 | #include <mm/slab.h> |
45 | #include <arch.h> |
45 | #include <arch.h> |
46 | #include <adt/list.h> |
46 | #include <adt/list.h> |
47 | #include <ipc/ipc.h> |
47 | #include <ipc/ipc.h> |
48 | #include <ipc/ipcrsc.h> |
48 | #include <ipc/ipcrsc.h> |
49 | #include <security/cap.h> |
49 | #include <security/cap.h> |
50 | #include <lib/elf.h> |
50 | #include <lib/elf.h> |
51 | #include <errno.h> |
51 | #include <errno.h> |
52 | #include <syscall/copy.h> |
52 | #include <syscall/copy.h> |
53 | #include <proc/program.h> |
53 | #include <proc/program.h> |
54 | 54 | ||
55 | #ifndef LOADED_PROG_STACK_PAGES_NO |
55 | #ifndef LOADED_PROG_STACK_PAGES_NO |
56 | #define LOADED_PROG_STACK_PAGES_NO 1 |
56 | #define LOADED_PROG_STACK_PAGES_NO 1 |
57 | #endif |
57 | #endif |
58 | 58 | ||
59 | /** |
59 | /** |
60 | * Points to the binary image used as the program loader. All non-initial |
60 | * Points to the binary image used as the program loader. All non-initial |
61 | * tasks are created from this executable image. |
61 | * tasks are created from this executable image. |
62 | */ |
62 | */ |
63 | void *program_loader = NULL; |
63 | void *program_loader = NULL; |
64 | 64 | ||
65 | /** Create a program using an existing address space. |
65 | /** Create a program using an existing address space. |
66 | * |
66 | * |
67 | * @param as Address space containing a binary program image. |
67 | * @param as Address space containing a binary program image. |
68 | * @param entry_addr Program entry-point address in program address space. |
68 | * @param entry_addr Program entry-point address in program address space. |
- | 69 | * @param name Name to set for the program's task. |
|
69 | * @param p Buffer for storing program information. |
70 | * @param p Buffer for storing program information. |
70 | */ |
71 | */ |
71 | void program_create(as_t *as, uintptr_t entry_addr, program_t *p) |
72 | void program_create(as_t *as, uintptr_t entry_addr, char *name, program_t *p) |
72 | { |
73 | { |
73 | as_area_t *a; |
74 | as_area_t *a; |
74 | uspace_arg_t *kernel_uarg; |
75 | uspace_arg_t *kernel_uarg; |
75 | 76 | ||
76 | kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0); |
77 | kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0); |
77 | kernel_uarg->uspace_entry = (void *) entry_addr; |
78 | kernel_uarg->uspace_entry = (void *) entry_addr; |
78 | kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS; |
79 | kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS; |
79 | kernel_uarg->uspace_thread_function = NULL; |
80 | kernel_uarg->uspace_thread_function = NULL; |
80 | kernel_uarg->uspace_thread_arg = NULL; |
81 | kernel_uarg->uspace_thread_arg = NULL; |
81 | kernel_uarg->uspace_uarg = NULL; |
82 | kernel_uarg->uspace_uarg = NULL; |
82 | 83 | ||
83 | p->task = task_create(as, "app"); |
84 | p->task = task_create(as, name); |
84 | ASSERT(p->task); |
85 | ASSERT(p->task); |
85 | 86 | ||
86 | /* |
87 | /* |
87 | * Create the data as_area. |
88 | * Create the data as_area. |
88 | */ |
89 | */ |
89 | a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE, |
90 | a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE, |
90 | LOADED_PROG_STACK_PAGES_NO * PAGE_SIZE, USTACK_ADDRESS, |
91 | LOADED_PROG_STACK_PAGES_NO * PAGE_SIZE, USTACK_ADDRESS, |
91 | AS_AREA_ATTR_NONE, &anon_backend, NULL); |
92 | AS_AREA_ATTR_NONE, &anon_backend, NULL); |
92 | 93 | ||
93 | /* |
94 | /* |
94 | * Create the main thread. |
95 | * Create the main thread. |
95 | */ |
96 | */ |
96 | p->main_thread = thread_create(uinit, kernel_uarg, p->task, |
97 | p->main_thread = thread_create(uinit, kernel_uarg, p->task, |
97 | THREAD_FLAG_USPACE, "uinit", false); |
98 | THREAD_FLAG_USPACE, "uinit", false); |
98 | ASSERT(p->main_thread); |
99 | ASSERT(p->main_thread); |
99 | } |
100 | } |
100 | 101 | ||
101 | /** Parse an executable image in the kernel memory. |
102 | /** Parse an executable image in the kernel memory. |
102 | * |
103 | * |
103 | * If the image belongs to a program loader, it is registered as such, |
104 | * If the image belongs to a program loader, it is registered as such, |
104 | * (and *task is set to NULL). Otherwise a task is created from the |
105 | * (and *task is set to NULL). Otherwise a task is created from the |
105 | * executable image. The task is returned in *task. |
106 | * executable image. The task is returned in *task. |
106 | * |
107 | * |
107 | * @param image_addr Address of an executable program image. |
108 | * @param image_addr Address of an executable program image. |
- | 109 | * @param name Name to set for the program's task. |
|
108 | * @param p Buffer for storing program info. If image_addr |
110 | * @param p Buffer for storing program info. If image_addr |
109 | * points to a loader image, p->task will be set to |
111 | * points to a loader image, p->task will be set to |
110 | * NULL and EOK will be returned. |
112 | * NULL and EOK will be returned. |
111 | * |
113 | * |
112 | * @return EOK on success or negative error code. |
114 | * @return EOK on success or negative error code. |
113 | */ |
115 | */ |
114 | int program_create_from_image(void *image_addr, program_t *p) |
116 | int program_create_from_image(void *image_addr, char *name, program_t *p) |
115 | { |
117 | { |
116 | as_t *as; |
118 | as_t *as; |
117 | unsigned int rc; |
119 | unsigned int rc; |
118 | 120 | ||
119 | as = as_create(0); |
121 | as = as_create(0); |
120 | ASSERT(as); |
122 | ASSERT(as); |
121 | 123 | ||
122 | rc = elf_load((elf_header_t *) image_addr, as, 0); |
124 | rc = elf_load((elf_header_t *) image_addr, as, 0); |
123 | if (rc != EE_OK) { |
125 | if (rc != EE_OK) { |
124 | as_destroy(as); |
126 | as_destroy(as); |
125 | p->task = NULL; |
127 | p->task = NULL; |
126 | p->main_thread = NULL; |
128 | p->main_thread = NULL; |
127 | if (rc != EE_LOADER) |
129 | if (rc != EE_LOADER) |
128 | return ENOTSUP; |
130 | return ENOTSUP; |
129 | 131 | ||
130 | /* Register image as the program loader */ |
132 | /* Register image as the program loader */ |
131 | ASSERT(program_loader == NULL); |
133 | ASSERT(program_loader == NULL); |
132 | program_loader = image_addr; |
134 | program_loader = image_addr; |
133 | return EOK; |
135 | return EOK; |
134 | } |
136 | } |
135 | 137 | ||
136 | program_create(as, ((elf_header_t *) image_addr)->e_entry, p); |
138 | program_create(as, ((elf_header_t *) image_addr)->e_entry, name, p); |
137 | 139 | ||
138 | return EOK; |
140 | return EOK; |
139 | } |
141 | } |
140 | 142 | ||
141 | /** Create a task from the program loader image. |
143 | /** Create a task from the program loader image. |
142 | * |
144 | * |
143 | * @param p Buffer for storing program info. |
145 | * @param p Buffer for storing program info. |
- | 146 | * @param name Name to set for the program's task. |
|
- | 147 | * |
|
144 | * @return EOK on success or negative error code. |
148 | * @return EOK on success or negative error code. |
145 | */ |
149 | */ |
146 | int program_create_loader(program_t *p) |
150 | int program_create_loader(program_t *p, char *name) |
147 | { |
151 | { |
148 | as_t *as; |
152 | as_t *as; |
149 | unsigned int rc; |
153 | unsigned int rc; |
150 | void *loader; |
154 | void *loader; |
151 | 155 | ||
152 | as = as_create(0); |
156 | as = as_create(0); |
153 | ASSERT(as); |
157 | ASSERT(as); |
154 | 158 | ||
155 | loader = program_loader; |
159 | loader = program_loader; |
156 | if (!loader) return ENOENT; |
160 | if (!loader) return ENOENT; |
157 | 161 | ||
158 | rc = elf_load((elf_header_t *) program_loader, as, ELD_F_LOADER); |
162 | rc = elf_load((elf_header_t *) program_loader, as, ELD_F_LOADER); |
159 | if (rc != EE_OK) { |
163 | if (rc != EE_OK) { |
160 | as_destroy(as); |
164 | as_destroy(as); |
161 | return ENOENT; |
165 | return ENOENT; |
162 | } |
166 | } |
163 | 167 | ||
164 | program_create(as, ((elf_header_t *) program_loader)->e_entry, p); |
168 | program_create(as, ((elf_header_t *) program_loader)->e_entry, |
- | 169 | name, p); |
|
165 | 170 | ||
166 | return EOK; |
171 | return EOK; |
167 | } |
172 | } |
168 | 173 | ||
169 | /** Make program ready. |
174 | /** Make program ready. |
170 | * |
175 | * |
171 | * Switch program's main thread to the ready state. |
176 | * Switch program's main thread to the ready state. |
172 | * |
177 | * |
173 | * @param p Program to make ready. |
178 | * @param p Program to make ready. |
174 | */ |
179 | */ |
175 | void program_ready(program_t *p) |
180 | void program_ready(program_t *p) |
176 | { |
181 | { |
177 | thread_ready(p->main_thread); |
182 | thread_ready(p->main_thread); |
178 | } |
183 | } |
179 | 184 | ||
180 | /** Syscall for creating a new loader instance from userspace. |
185 | /** Syscall for creating a new loader instance from userspace. |
181 | * |
186 | * |
182 | * Creates a new task from the program loader image, connects a phone |
187 | * Creates a new task from the program loader image, connects a phone |
183 | * to it and stores the phone id into the provided buffer. |
188 | * to it and stores the phone id into the provided buffer. |
184 | * |
189 | * |
185 | * @param uspace_phone_id Userspace address where to store the phone id. |
190 | * @param uspace_phone_id Userspace address where to store the phone id. |
- | 191 | * @param name Name to set on the new task (typically the same |
|
- | 192 | * as the command used to execute it). |
|
186 | * |
193 | * |
187 | * @return 0 on success or an error code from @ref errno.h. |
194 | * @return 0 on success or an error code from @ref errno.h. |
188 | */ |
195 | */ |
189 | unative_t sys_program_spawn_loader(int *uspace_phone_id) |
196 | unative_t sys_program_spawn_loader(int *uspace_phone_id, char *uspace_name, |
- | 197 | size_t name_len) |
|
190 | { |
198 | { |
191 | program_t p; |
199 | program_t p; |
192 | int fake_id; |
200 | int fake_id; |
193 | int rc; |
201 | int rc; |
194 | int phone_id; |
202 | int phone_id; |
- | 203 | char namebuf[TASK_NAME_BUFLEN]; |
|
195 | 204 | ||
196 | fake_id = 0; |
205 | fake_id = 0; |
197 | 206 | ||
198 | /* Before we even try creating the task, see if we can write the id */ |
207 | /* Before we even try creating the task, see if we can write the id */ |
199 | rc = (unative_t) copy_to_uspace(uspace_phone_id, &fake_id, |
208 | rc = (unative_t) copy_to_uspace(uspace_phone_id, &fake_id, |
200 | sizeof(fake_id)); |
209 | sizeof(fake_id)); |
201 | if (rc != 0) |
210 | if (rc != 0) |
202 | return rc; |
211 | return rc; |
203 | 212 | ||
- | 213 | /* Cap length of name and copy it from userspace. */ |
|
- | 214 | ||
- | 215 | if (name_len > THREAD_NAME_BUFLEN - 1) |
|
- | 216 | name_len = THREAD_NAME_BUFLEN - 1; |
|
- | 217 | ||
- | 218 | rc = copy_from_uspace(namebuf, uspace_name, name_len); |
|
- | 219 | if (rc != 0) |
|
- | 220 | return (unative_t) rc; |
|
- | 221 | ||
- | 222 | namebuf[name_len] = '\0'; |
|
- | 223 | ||
- | 224 | /* Allocate the phone for communicating with the new task. */ |
|
- | 225 | ||
204 | phone_id = phone_alloc(); |
226 | phone_id = phone_alloc(); |
205 | if (phone_id < 0) |
227 | if (phone_id < 0) |
206 | return ELIMIT; |
228 | return ELIMIT; |
207 | 229 | ||
- | 230 | /* Spawn the new task. */ |
|
- | 231 | ||
208 | rc = program_create_loader(&p); |
232 | rc = program_create_loader(&p, namebuf); |
209 | if (rc != 0) |
233 | if (rc != 0) |
210 | return rc; |
234 | return rc; |
211 | 235 | ||
212 | phone_connect(phone_id, &p.task->answerbox); |
236 | phone_connect(phone_id, &p.task->answerbox); |
213 | 237 | ||
214 | /* No need to aquire lock before task_ready() */ |
238 | /* No need to aquire lock before task_ready() */ |
215 | rc = (unative_t) copy_to_uspace(uspace_phone_id, &phone_id, |
239 | rc = (unative_t) copy_to_uspace(uspace_phone_id, &phone_id, |
216 | sizeof(phone_id)); |
240 | sizeof(phone_id)); |
217 | if (rc != 0) { |
241 | if (rc != 0) { |
218 | /* Ooops */ |
242 | /* Ooops */ |
219 | ipc_phone_hangup(&TASK->phones[phone_id]); |
243 | ipc_phone_hangup(&TASK->phones[phone_id]); |
220 | task_kill(p.task->taskid); |
244 | task_kill(p.task->taskid); |
221 | return rc; |
245 | return rc; |
222 | } |
246 | } |
223 | 247 | ||
224 | // FIXME: control the capabilities |
248 | // FIXME: control the capabilities |
225 | cap_set(p.task, cap_get(TASK)); |
249 | cap_set(p.task, cap_get(TASK)); |
226 | 250 | ||
227 | program_ready(&p); |
251 | program_ready(&p); |
228 | 252 | ||
229 | return EOK; |
253 | return EOK; |
230 | } |
254 | } |
231 | 255 | ||
232 | /** @} |
256 | /** @} |
233 | */ |
257 | */ |
234 | 258 |