source: mainline/kernel/generic/src/proc/program.c@ fb13b44

Last change on this file since fb13b44 was fb13b44, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

Spawned tasks' phone is connected to spawning task

  • Only boot time tasks are connected to hardcoded NS task.
  • First connected phone of other tasks is connected to the task that spawned them (spawn parent).
  • Until further changes, effectively it changes nothing (NS was the ultimate spawn parent).

Conflicts:

kernel/generic/include/proc/program.h
kernel/generic/src/ipc/ipc.c
kernel/generic/src/proc/program.c
kernel/generic/src/proc/task.c

  • Property mode set to 100644
File size: 7.0 KB
Line 
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 <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 */
59void *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 answerbox Answerbox where box 0 is connected to (may be NULL).
67 * @param prg Buffer for storing program information.
68 *
69 * @return EOK on success or an error code.
70 *
71 */
72errno_t program_create(as_t *as, uspace_addr_t entry_addr, char *name,
73 struct answerbox *answerbox, program_t *prg)
74{
75 uspace_arg_t *kernel_uarg = (uspace_arg_t *)
76 malloc(sizeof(uspace_arg_t));
77 if (!kernel_uarg)
78 return ENOMEM;
79
80 prg->loader_status = EOK;
81 prg->task = task_create(as, name, answerbox);
82 if (!prg->task) {
83 free(kernel_uarg);
84 return ELIMIT;
85 }
86
87 /*
88 * Create the stack address space area.
89 */
90 uintptr_t virt = (uintptr_t) AS_AREA_ANY;
91 uintptr_t bound = USER_ADDRESS_SPACE_END - (STACK_SIZE_USER - 1);
92
93 /* Adjust bound to create space for the desired guard page. */
94 bound -= PAGE_SIZE;
95
96 as_area_t *area = as_area_create(as,
97 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE | AS_AREA_GUARD |
98 AS_AREA_LATE_RESERVE, STACK_SIZE_USER, AS_AREA_ATTR_NONE,
99 &anon_backend, NULL, &virt, bound);
100 if (!area) {
101 free(kernel_uarg);
102 task_destroy(prg->task);
103 prg->task = NULL;
104 return ENOMEM;
105 }
106
107 kernel_uarg->uspace_entry = entry_addr;
108 kernel_uarg->uspace_stack = virt;
109 kernel_uarg->uspace_stack_size = STACK_SIZE_USER;
110 kernel_uarg->uspace_thread_function = USPACE_NULL;
111 kernel_uarg->uspace_thread_arg = USPACE_NULL;
112 kernel_uarg->uspace_uarg = USPACE_NULL;
113
114 /*
115 * Create the main thread.
116 */
117 prg->main_thread = thread_create(uinit, kernel_uarg, prg->task,
118 THREAD_FLAG_USPACE, "uinit");
119 if (!prg->main_thread) {
120 free(kernel_uarg);
121 as_area_destroy(as, virt);
122 task_destroy(prg->task);
123 prg->task = NULL;
124 return ELIMIT;
125 }
126
127 return EOK;
128}
129
130/** Parse an executable image in the kernel memory.
131 *
132 * If the image belongs to a program loader, it is registered as such,
133 * (and *task is set to NULL). Otherwise a task is created from the
134 * executable image. The task is returned in *task.
135 *
136 * @param[in] image_addr Address of an executable program image.
137 * @param[in] name Name to set for the program's task.
138 * @param[out] prg Buffer for storing program info.
139 * If image_addr points to a loader image,
140 * prg->task will be set to NULL and EOK
141 * will be returned.
142 *
143 * @return EOK on success or an error code.
144 *
145 */
146errno_t program_create_from_image(void *image_addr, char *name, program_t *prg)
147{
148 as_t *as = as_create(0);
149 if (!as)
150 return ENOMEM;
151
152 prg->loader_status = elf_load((elf_header_t *) image_addr, as);
153 if (prg->loader_status != EOK) {
154 as_release(as);
155 prg->task = NULL;
156 prg->main_thread = NULL;
157 return ENOTSUP;
158 }
159
160 return program_create(as, ((elf_header_t *) image_addr)->e_entry,
161 name, ipc_box_0, prg);
162}
163
164/** Create a task from the program loader image.
165 *
166 * @param prg Buffer for storing program info.
167 * @param name Name to set for the program's task.
168 *
169 * @return EOK on success or an error code.
170 *
171 */
172errno_t program_create_loader(program_t *prg, char *name)
173{
174 as_t *as = as_create(0);
175 if (!as)
176 return ENOMEM;
177
178 void *loader = program_loader;
179 if (!loader) {
180 as_release(as);
181 log(LF_OTHER, LVL_ERROR,
182 "Cannot spawn loader as none was registered");
183 return ENOENT;
184 }
185
186 prg->loader_status = elf_load((elf_header_t *) program_loader, as);
187 if (prg->loader_status != EOK) {
188 as_release(as);
189 log(LF_OTHER, LVL_ERROR, "Cannot spawn loader (%s)",
190 str_error(prg->loader_status));
191 return ENOENT;
192 }
193
194 return program_create(as, ((elf_header_t *) program_loader)->e_entry,
195 name, &TASK->answerbox, prg);
196}
197
198/** Make program ready.
199 *
200 * Switch program's main thread to the ready state.
201 *
202 * @param prg Program to make ready.
203 *
204 */
205void program_ready(program_t *prg)
206{
207 thread_ready(prg->main_thread);
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 */
222sys_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 */
Note: See TracBrowser for help on using the repository browser.