1 | /*
|
---|
2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
3 | * Copyright (c) 2008 Jiri Svoboda
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
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
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
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
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
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
|
---|
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
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup kernel_generic_proc
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * @file
|
---|
36 | * @brief Running userspace programs.
|
---|
37 | */
|
---|
38 |
|
---|
39 | #include <main/uinit.h>
|
---|
40 | #include <proc/thread.h>
|
---|
41 | #include <proc/task.h>
|
---|
42 | #include <mm/as.h>
|
---|
43 | #include <stdlib.h>
|
---|
44 | #include <arch.h>
|
---|
45 | #include <adt/list.h>
|
---|
46 | #include <ipc/ipc.h>
|
---|
47 | #include <ipc/ipcrsc.h>
|
---|
48 | #include <security/perm.h>
|
---|
49 | #include <lib/elf_load.h>
|
---|
50 | #include <str.h>
|
---|
51 | #include <str_error.h>
|
---|
52 | #include <log.h>
|
---|
53 | #include <syscall/copy.h>
|
---|
54 | #include <proc/program.h>
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Points to the binary image used as the program loader. All non-initial
|
---|
58 | * tasks are created from this executable image.
|
---|
59 | */
|
---|
60 | void *program_loader = NULL;
|
---|
61 |
|
---|
62 | /** Create a program using an existing address space.
|
---|
63 | *
|
---|
64 | * @param as Address space containing a binary program image.
|
---|
65 | * @param entry_addr Program entry-point address in program address space.
|
---|
66 | * @param name Name to set for the program's task.
|
---|
67 | * @param prg Buffer for storing program information.
|
---|
68 | *
|
---|
69 | * @return EOK on success or an error code.
|
---|
70 | *
|
---|
71 | */
|
---|
72 | errno_t program_create(as_t *as, uspace_addr_t entry_addr, char *name, program_t *prg)
|
---|
73 | {
|
---|
74 | uspace_arg_t *kernel_uarg = (uspace_arg_t *)
|
---|
75 | malloc(sizeof(uspace_arg_t));
|
---|
76 | if (!kernel_uarg)
|
---|
77 | return ENOMEM;
|
---|
78 |
|
---|
79 | prg->loader_status = EOK;
|
---|
80 | prg->task = task_create(as, name);
|
---|
81 | if (!prg->task) {
|
---|
82 | free(kernel_uarg);
|
---|
83 | return ELIMIT;
|
---|
84 | }
|
---|
85 |
|
---|
86 | /*
|
---|
87 | * Create the stack address space area.
|
---|
88 | */
|
---|
89 | uintptr_t virt = (uintptr_t) AS_AREA_ANY;
|
---|
90 | uintptr_t bound = USER_ADDRESS_SPACE_END - (STACK_SIZE_USER - 1);
|
---|
91 |
|
---|
92 | /* Adjust bound to create space for the desired guard page. */
|
---|
93 | bound -= PAGE_SIZE;
|
---|
94 |
|
---|
95 | as_area_t *area = as_area_create(as,
|
---|
96 | AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE | AS_AREA_GUARD |
|
---|
97 | AS_AREA_LATE_RESERVE, STACK_SIZE_USER, AS_AREA_ATTR_NONE,
|
---|
98 | &anon_backend, NULL, &virt, bound);
|
---|
99 | if (!area) {
|
---|
100 | free(kernel_uarg);
|
---|
101 | task_destroy(prg->task);
|
---|
102 | prg->task = NULL;
|
---|
103 | return ENOMEM;
|
---|
104 | }
|
---|
105 |
|
---|
106 | kernel_uarg->uspace_entry = entry_addr;
|
---|
107 | kernel_uarg->uspace_stack = virt;
|
---|
108 | kernel_uarg->uspace_stack_size = STACK_SIZE_USER;
|
---|
109 | kernel_uarg->uspace_thread_function = USPACE_NULL;
|
---|
110 | kernel_uarg->uspace_thread_arg = USPACE_NULL;
|
---|
111 | kernel_uarg->uspace_uarg = USPACE_NULL;
|
---|
112 |
|
---|
113 | /*
|
---|
114 | * Create the main thread.
|
---|
115 | */
|
---|
116 | prg->main_thread = thread_create(uinit, kernel_uarg, prg->task,
|
---|
117 | THREAD_FLAG_USPACE, "uinit");
|
---|
118 | if (!prg->main_thread) {
|
---|
119 | free(kernel_uarg);
|
---|
120 | as_area_destroy(as, virt);
|
---|
121 | task_destroy(prg->task);
|
---|
122 | prg->task = NULL;
|
---|
123 | return ELIMIT;
|
---|
124 | }
|
---|
125 |
|
---|
126 | return EOK;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /** Parse an executable image in the kernel memory.
|
---|
130 | *
|
---|
131 | * If the image belongs to a program loader, it is registered as such,
|
---|
132 | * (and *task is set to NULL). Otherwise a task is created from the
|
---|
133 | * executable image. The task is returned in *task.
|
---|
134 | *
|
---|
135 | * @param[in] image_addr Address of an executable program image.
|
---|
136 | * @param[in] name Name to set for the program's task.
|
---|
137 | * @param[out] prg Buffer for storing program info.
|
---|
138 | * If image_addr points to a loader image,
|
---|
139 | * prg->task will be set to NULL and EOK
|
---|
140 | * will be returned.
|
---|
141 | *
|
---|
142 | * @return EOK on success or an error code.
|
---|
143 | *
|
---|
144 | */
|
---|
145 | errno_t program_create_from_image(void *image_addr, char *name, program_t *prg)
|
---|
146 | {
|
---|
147 | as_t *as = as_create(0);
|
---|
148 | if (!as)
|
---|
149 | return ENOMEM;
|
---|
150 |
|
---|
151 | prg->loader_status = elf_load((elf_header_t *) image_addr, as);
|
---|
152 | if (prg->loader_status != EOK) {
|
---|
153 | as_release(as);
|
---|
154 | prg->task = NULL;
|
---|
155 | prg->main_thread = NULL;
|
---|
156 | return ENOTSUP;
|
---|
157 | }
|
---|
158 |
|
---|
159 | return program_create(as, ((elf_header_t *) image_addr)->e_entry,
|
---|
160 | name, prg);
|
---|
161 | }
|
---|
162 |
|
---|
163 | /** Create a task from the program loader image.
|
---|
164 | *
|
---|
165 | * @param prg Buffer for storing program info.
|
---|
166 | * @param name Name to set for the program's task.
|
---|
167 | *
|
---|
168 | * @return EOK on success or an error code.
|
---|
169 | *
|
---|
170 | */
|
---|
171 | errno_t program_create_loader(program_t *prg, char *name)
|
---|
172 | {
|
---|
173 | as_t *as = as_create(0);
|
---|
174 | if (!as)
|
---|
175 | return ENOMEM;
|
---|
176 |
|
---|
177 | void *loader = program_loader;
|
---|
178 | if (!loader) {
|
---|
179 | as_release(as);
|
---|
180 | log(LF_OTHER, LVL_ERROR,
|
---|
181 | "Cannot spawn loader as none was registered");
|
---|
182 | return ENOENT;
|
---|
183 | }
|
---|
184 |
|
---|
185 | prg->loader_status = elf_load((elf_header_t *) program_loader, as);
|
---|
186 | if (prg->loader_status != EOK) {
|
---|
187 | as_release(as);
|
---|
188 | log(LF_OTHER, LVL_ERROR, "Cannot spawn loader (%s)",
|
---|
189 | str_error(prg->loader_status));
|
---|
190 | return prg->loader_status;
|
---|
191 | }
|
---|
192 |
|
---|
193 | return program_create(as, ((elf_header_t *) program_loader)->e_entry,
|
---|
194 | name, prg);
|
---|
195 | }
|
---|
196 |
|
---|
197 | /** Make program ready.
|
---|
198 | *
|
---|
199 | * Switch program's main thread to the ready state.
|
---|
200 | *
|
---|
201 | * @param prg Program to make ready.
|
---|
202 | *
|
---|
203 | */
|
---|
204 | void program_ready(program_t *prg)
|
---|
205 | {
|
---|
206 | thread_ready(prg->main_thread);
|
---|
207 | prg->main_thread = NULL;
|
---|
208 | }
|
---|
209 |
|
---|
210 | /** Syscall for creating a new loader instance from userspace.
|
---|
211 | *
|
---|
212 | * Creates a new task from the program loader image and sets
|
---|
213 | * the task name.
|
---|
214 | *
|
---|
215 | * @param uspace_name Name to set on the new task (typically the same
|
---|
216 | * as the command used to execute it).
|
---|
217 | * @param name_len Length of the name.
|
---|
218 | *
|
---|
219 | * @return EOK on success or an error code from @ref errno.h.
|
---|
220 | *
|
---|
221 | */
|
---|
222 | sys_errno_t sys_program_spawn_loader(uspace_ptr_char uspace_name, size_t name_len)
|
---|
223 | {
|
---|
224 | /* Cap length of name and copy it from userspace. */
|
---|
225 | if (name_len > TASK_NAME_BUFLEN - 1)
|
---|
226 | name_len = TASK_NAME_BUFLEN - 1;
|
---|
227 |
|
---|
228 | char namebuf[TASK_NAME_BUFLEN];
|
---|
229 | errno_t rc = copy_from_uspace(namebuf, uspace_name, name_len);
|
---|
230 | if (rc != EOK)
|
---|
231 | return (sys_errno_t) rc;
|
---|
232 |
|
---|
233 | namebuf[name_len] = 0;
|
---|
234 |
|
---|
235 | /* Spawn the new task. */
|
---|
236 | program_t prg;
|
---|
237 | rc = program_create_loader(&prg, namebuf);
|
---|
238 | if (rc != EOK)
|
---|
239 | return rc;
|
---|
240 |
|
---|
241 | // FIXME: control the permissions
|
---|
242 | perm_set(prg.task, perm_get(TASK));
|
---|
243 | program_ready(&prg);
|
---|
244 |
|
---|
245 | return EOK;
|
---|
246 | }
|
---|
247 |
|
---|
248 | /** @}
|
---|
249 | */
|
---|