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 <errno.h>
|
---|
51 | #include <log.h>
|
---|
52 | #include <syscall/copy.h>
|
---|
53 | #include <proc/program.h>
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Points to the binary image used as the program loader. All non-initial
|
---|
57 | * tasks are created from this executable image.
|
---|
58 | */
|
---|
59 | void *program_loader = NULL;
|
---|
60 |
|
---|
61 | /** Create a program using an existing address space.
|
---|
62 | *
|
---|
63 | * @param as Address space containing a binary program image.
|
---|
64 | * @param entry_addr Program entry-point address in program address space.
|
---|
65 | * @param name Name to set for the program's task.
|
---|
66 | * @param prg Buffer for storing program information.
|
---|
67 | *
|
---|
68 | * @return EOK on success or an error code.
|
---|
69 | *
|
---|
70 | */
|
---|
71 | errno_t program_create(as_t *as, uintptr_t entry_addr, char *name, program_t *prg)
|
---|
72 | {
|
---|
73 | uspace_arg_t *kernel_uarg = (uspace_arg_t *)
|
---|
74 | malloc(sizeof(uspace_arg_t));
|
---|
75 | if (!kernel_uarg)
|
---|
76 | return ENOMEM;
|
---|
77 |
|
---|
78 | prg->loader_status = EE_OK;
|
---|
79 | prg->task = task_create(as, name);
|
---|
80 | if (!prg->task) {
|
---|
81 | free(kernel_uarg);
|
---|
82 | return ELIMIT;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /*
|
---|
86 | * Create the stack address space area.
|
---|
87 | */
|
---|
88 | uintptr_t virt = (uintptr_t) -1;
|
---|
89 | uintptr_t bound = USER_ADDRESS_SPACE_END - (STACK_SIZE_USER - 1);
|
---|
90 |
|
---|
91 | /* Adjust bound to create space for the desired guard page. */
|
---|
92 | bound -= PAGE_SIZE;
|
---|
93 |
|
---|
94 | as_area_t *area = as_area_create(as,
|
---|
95 | AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE | AS_AREA_GUARD |
|
---|
96 | AS_AREA_LATE_RESERVE, STACK_SIZE_USER, AS_AREA_ATTR_NONE,
|
---|
97 | &anon_backend, NULL, &virt, bound);
|
---|
98 | if (!area) {
|
---|
99 | free(kernel_uarg);
|
---|
100 | task_destroy(prg->task);
|
---|
101 | prg->task = NULL;
|
---|
102 | return ENOMEM;
|
---|
103 | }
|
---|
104 |
|
---|
105 | kernel_uarg->uspace_entry = (void *) entry_addr;
|
---|
106 | kernel_uarg->uspace_stack = (void *) virt;
|
---|
107 | kernel_uarg->uspace_stack_size = STACK_SIZE_USER;
|
---|
108 | kernel_uarg->uspace_thread_function = NULL;
|
---|
109 | kernel_uarg->uspace_thread_arg = NULL;
|
---|
110 | kernel_uarg->uspace_uarg = NULL;
|
---|
111 |
|
---|
112 | /*
|
---|
113 | * Create the main thread.
|
---|
114 | */
|
---|
115 | prg->main_thread = thread_create(uinit, kernel_uarg, prg->task,
|
---|
116 | THREAD_FLAG_USPACE, "uinit");
|
---|
117 | if (!prg->main_thread) {
|
---|
118 | free(kernel_uarg);
|
---|
119 | as_area_destroy(as, virt);
|
---|
120 | task_destroy(prg->task);
|
---|
121 | prg->task = NULL;
|
---|
122 | return ELIMIT;
|
---|
123 | }
|
---|
124 |
|
---|
125 | return EOK;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /** Parse an executable image in the kernel memory.
|
---|
129 | *
|
---|
130 | * If the image belongs to a program loader, it is registered as such,
|
---|
131 | * (and *task is set to NULL). Otherwise a task is created from the
|
---|
132 | * executable image. The task is returned in *task.
|
---|
133 | *
|
---|
134 | * @param[in] image_addr Address of an executable program image.
|
---|
135 | * @param[in] name Name to set for the program's task.
|
---|
136 | * @param[out] prg Buffer for storing program info.
|
---|
137 | * If image_addr points to a loader image,
|
---|
138 | * prg->task will be set to NULL and EOK
|
---|
139 | * will be returned.
|
---|
140 | *
|
---|
141 | * @return EOK on success or an error code.
|
---|
142 | *
|
---|
143 | */
|
---|
144 | errno_t program_create_from_image(void *image_addr, char *name, program_t *prg)
|
---|
145 | {
|
---|
146 | as_t *as = as_create(0);
|
---|
147 | if (!as)
|
---|
148 | return ENOMEM;
|
---|
149 |
|
---|
150 | prg->loader_status = elf_load((elf_header_t *) image_addr, as);
|
---|
151 | if (prg->loader_status != EE_OK) {
|
---|
152 | as_destroy(as);
|
---|
153 | prg->task = NULL;
|
---|
154 | prg->main_thread = NULL;
|
---|
155 | return ENOTSUP;
|
---|
156 | }
|
---|
157 |
|
---|
158 | return program_create(as, ((elf_header_t *) image_addr)->e_entry,
|
---|
159 | name, prg);
|
---|
160 | }
|
---|
161 |
|
---|
162 | /** Create a task from the program loader image.
|
---|
163 | *
|
---|
164 | * @param prg Buffer for storing program info.
|
---|
165 | * @param name Name to set for the program's task.
|
---|
166 | *
|
---|
167 | * @return EOK on success or an error code.
|
---|
168 | *
|
---|
169 | */
|
---|
170 | errno_t program_create_loader(program_t *prg, char *name)
|
---|
171 | {
|
---|
172 | as_t *as = as_create(0);
|
---|
173 | if (!as)
|
---|
174 | return ENOMEM;
|
---|
175 |
|
---|
176 | void *loader = program_loader;
|
---|
177 | if (!loader) {
|
---|
178 | as_destroy(as);
|
---|
179 | log(LF_OTHER, LVL_ERROR,
|
---|
180 | "Cannot spawn loader as none was registered");
|
---|
181 | return ENOENT;
|
---|
182 | }
|
---|
183 |
|
---|
184 | prg->loader_status = elf_load((elf_header_t *) program_loader, as);
|
---|
185 | if (prg->loader_status != EE_OK) {
|
---|
186 | as_destroy(as);
|
---|
187 | log(LF_OTHER, LVL_ERROR, "Cannot spawn loader (%s)",
|
---|
188 | elf_error(prg->loader_status));
|
---|
189 | return ENOENT;
|
---|
190 | }
|
---|
191 |
|
---|
192 | return program_create(as, ((elf_header_t *) program_loader)->e_entry,
|
---|
193 | name, prg);
|
---|
194 | }
|
---|
195 |
|
---|
196 | /** Make program ready.
|
---|
197 | *
|
---|
198 | * Switch program's main thread to the ready state.
|
---|
199 | *
|
---|
200 | * @param prg Program to make ready.
|
---|
201 | *
|
---|
202 | */
|
---|
203 | void program_ready(program_t *prg)
|
---|
204 | {
|
---|
205 | thread_ready(prg->main_thread);
|
---|
206 | }
|
---|
207 |
|
---|
208 | /** Syscall for creating a new loader instance from userspace.
|
---|
209 | *
|
---|
210 | * Creates a new task from the program loader image and sets
|
---|
211 | * the task name.
|
---|
212 | *
|
---|
213 | * @param uspace_name Name to set on the new task (typically the same
|
---|
214 | * as the command used to execute it).
|
---|
215 | * @param name_len Length of the name.
|
---|
216 | *
|
---|
217 | * @return EOK on success or an error code from @ref errno.h.
|
---|
218 | *
|
---|
219 | */
|
---|
220 | sys_errno_t sys_program_spawn_loader(char *uspace_name, size_t name_len)
|
---|
221 | {
|
---|
222 | /* Cap length of name and copy it from userspace. */
|
---|
223 | if (name_len > TASK_NAME_BUFLEN - 1)
|
---|
224 | name_len = TASK_NAME_BUFLEN - 1;
|
---|
225 |
|
---|
226 | char namebuf[TASK_NAME_BUFLEN];
|
---|
227 | errno_t rc = copy_from_uspace(namebuf, uspace_name, name_len);
|
---|
228 | if (rc != EOK)
|
---|
229 | return (sys_errno_t) rc;
|
---|
230 |
|
---|
231 | namebuf[name_len] = 0;
|
---|
232 |
|
---|
233 | /* Spawn the new task. */
|
---|
234 | program_t prg;
|
---|
235 | rc = program_create_loader(&prg, namebuf);
|
---|
236 | if (rc != EOK)
|
---|
237 | return rc;
|
---|
238 |
|
---|
239 | // FIXME: control the permissions
|
---|
240 | perm_set(prg.task, perm_get(TASK));
|
---|
241 | program_ready(&prg);
|
---|
242 |
|
---|
243 | return EOK;
|
---|
244 | }
|
---|
245 |
|
---|
246 | /** @}
|
---|
247 | */
|
---|