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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6eef3c4 was 6eef3c4, checked in by Martin Decky <martin@…>, 13 years ago

cleanup thread_create() and thread_t structure

  • remove 'flag' bitfield from thread_t, use individual boolean flags (can be optimized later on by adding : 1)
  • use an enum for call flags, add THREAD_FLAG_NONE
  • remove the 'uncounted' argument in favour of a call flag
  • introduce thread_wire() to setup wired threads
  • Property mode set to 100644
File size: 6.8 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 genericproc
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 <mm/slab.h>
44#include <arch.h>
45#include <adt/list.h>
46#include <ipc/ipc.h>
47#include <ipc/ipcrsc.h>
48#include <security/cap.h>
49#include <lib/elf_load.h>
50#include <errno.h>
51#include <print.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 prg Buffer for storing program information.
67 *
68 * @return EOK on success or negative error code.
69 *
70 */
71int program_create(as_t *as, uintptr_t entry_addr, char *name, program_t *prg)
72{
73 prg->loader_status = EE_OK;
74 prg->task = task_create(as, name);
75 if (!prg->task)
76 return ELIMIT;
77
78 /*
79 * Create the stack address space area.
80 */
81 uintptr_t virt = USTACK_ADDRESS;
82 as_area_t *area = as_area_create(as,
83 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
84 STACK_SIZE, AS_AREA_ATTR_NONE, &anon_backend, NULL, &virt, 0);
85 if (!area) {
86 task_destroy(prg->task);
87 return ENOMEM;
88 }
89
90 uspace_arg_t *kernel_uarg = (uspace_arg_t *)
91 malloc(sizeof(uspace_arg_t), 0);
92
93 kernel_uarg->uspace_entry = (void *) entry_addr;
94 kernel_uarg->uspace_stack = (void *) virt;
95 kernel_uarg->uspace_stack_size = STACK_SIZE;
96 kernel_uarg->uspace_thread_function = NULL;
97 kernel_uarg->uspace_thread_arg = NULL;
98 kernel_uarg->uspace_uarg = NULL;
99
100 /*
101 * Create the main thread.
102 */
103 prg->main_thread = thread_create(uinit, kernel_uarg, prg->task,
104 THREAD_FLAG_USPACE, "uinit");
105 if (!prg->main_thread) {
106 free(kernel_uarg);
107 as_area_destroy(as, virt);
108 task_destroy(prg->task);
109 return ELIMIT;
110 }
111
112 return EOK;
113}
114
115/** Parse an executable image in the kernel memory.
116 *
117 * If the image belongs to a program loader, it is registered as such,
118 * (and *task is set to NULL). Otherwise a task is created from the
119 * executable image. The task is returned in *task.
120 *
121 * @param[in] image_addr Address of an executable program image.
122 * @param[in] name Name to set for the program's task.
123 * @param[out] prg Buffer for storing program info.
124 * If image_addr points to a loader image,
125 * prg->task will be set to NULL and EOK
126 * will be returned.
127 *
128 * @return EOK on success or negative error code.
129 *
130 */
131int program_create_from_image(void *image_addr, char *name, program_t *prg)
132{
133 as_t *as = as_create(0);
134 if (!as)
135 return ENOMEM;
136
137 prg->loader_status = elf_load((elf_header_t *) image_addr, as, 0);
138 if (prg->loader_status != EE_OK) {
139 as_destroy(as);
140 prg->task = NULL;
141 prg->main_thread = NULL;
142
143 if (prg->loader_status != EE_LOADER)
144 return ENOTSUP;
145
146 /* Register image as the program loader */
147 if (program_loader != NULL)
148 return ELIMIT;
149
150 program_loader = image_addr;
151 printf("Program loader at %p\n", (void *) image_addr);
152
153 return EOK;
154 }
155
156 return program_create(as, ((elf_header_t *) image_addr)->e_entry,
157 name, prg);
158}
159
160/** Create a task from the program loader image.
161 *
162 * @param prg Buffer for storing program info.
163 * @param name Name to set for the program's task.
164 *
165 * @return EOK on success or negative error code.
166 *
167 */
168int program_create_loader(program_t *prg, char *name)
169{
170 as_t *as = as_create(0);
171 if (!as)
172 return ENOMEM;
173
174 void *loader = program_loader;
175 if (!loader) {
176 as_destroy(as);
177 printf("Cannot spawn loader as none was registered\n");
178 return ENOENT;
179 }
180
181 prg->loader_status = elf_load((elf_header_t *) program_loader, as,
182 ELD_F_LOADER);
183 if (prg->loader_status != EE_OK) {
184 as_destroy(as);
185 printf("Cannot spawn loader (%s)\n",
186 elf_error(prg->loader_status));
187 return ENOENT;
188 }
189
190 return program_create(as, ((elf_header_t *) program_loader)->e_entry,
191 name, prg);
192}
193
194/** Make program ready.
195 *
196 * Switch program's main thread to the ready state.
197 *
198 * @param prg Program to make ready.
199 *
200 */
201void program_ready(program_t *prg)
202{
203 thread_ready(prg->main_thread);
204}
205
206/** Syscall for creating a new loader instance from userspace.
207 *
208 * Creates a new task from the program loader image and sets
209 * the task name.
210 *
211 * @param uspace_name Name to set on the new task (typically the same
212 * as the command used to execute it).
213 * @param name_len Length of the name.
214 *
215 * @return EOK on success or an error code from @ref errno.h.
216 *
217 */
218sysarg_t sys_program_spawn_loader(char *uspace_name, size_t name_len)
219{
220 /* Cap length of name and copy it from userspace. */
221 if (name_len > TASK_NAME_BUFLEN - 1)
222 name_len = TASK_NAME_BUFLEN - 1;
223
224 char namebuf[TASK_NAME_BUFLEN];
225 int rc = copy_from_uspace(namebuf, uspace_name, name_len);
226 if (rc != 0)
227 return (sysarg_t) rc;
228
229 namebuf[name_len] = 0;
230
231 /* Spawn the new task. */
232 program_t prg;
233 rc = program_create_loader(&prg, namebuf);
234 if (rc != 0)
235 return rc;
236
237 // FIXME: control the capabilities
238 cap_set(prg.task, cap_get(TASK));
239 program_ready(&prg);
240
241 return EOK;
242}
243
244/** @}
245 */
Note: See TracBrowser for help on using the repository browser.