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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d1e8440 was d1e8440, checked in by Jakub Jermar <jakub@…>, 13 years ago

Get rid of USTACK_ADDRESS.

  • Let the kernel find a suitable address itself.
  • Limit it to only one possibility using the bound argument of as_area_create().
  • Property mode set to 100644
File size: 7.0 KB
RevLine 
[c98e6ee]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
[91001e2]36 * @brief Running userspace programs.
[c98e6ee]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>
[e16e2ba4]49#include <lib/elf_load.h>
[c98e6ee]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 *
[91001e2]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 *
[c98e6ee]70 */
[91001e2]71int program_create(as_t *as, uintptr_t entry_addr, char *name, program_t *prg)
[c98e6ee]72{
[db675dd]73 prg->loader_status = EE_OK;
[91001e2]74 prg->task = task_create(as, name);
75 if (!prg->task)
76 return ELIMIT;
77
[c98e6ee]78 /*
[26aafe8]79 * Create the stack address space area.
[c98e6ee]80 */
[d1e8440]81 uintptr_t virt = (uintptr_t) -1;
82 uintptr_t bound = USER_ADDRESS_SPACE_END - (STACK_SIZE_USER - 1);
83
84 /* Adjust bound to create space for the desired guard page. */
85 bound -= PAGE_SIZE;
86
[91001e2]87 as_area_t *area = as_area_create(as,
[3b8a990]88 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE | AS_AREA_GUARD |
[67b152e]89 AS_AREA_LATE_RESERVE, STACK_SIZE_USER, AS_AREA_ATTR_NONE,
[d1e8440]90 &anon_backend, NULL, &virt, bound);
[2902e1bb]91 if (!area) {
92 task_destroy(prg->task);
[91001e2]93 return ENOMEM;
[2902e1bb]94 }
95
96 uspace_arg_t *kernel_uarg = (uspace_arg_t *)
97 malloc(sizeof(uspace_arg_t), 0);
98
99 kernel_uarg->uspace_entry = (void *) entry_addr;
100 kernel_uarg->uspace_stack = (void *) virt;
[67b152e]101 kernel_uarg->uspace_stack_size = STACK_SIZE_USER;
[2902e1bb]102 kernel_uarg->uspace_thread_function = NULL;
103 kernel_uarg->uspace_thread_arg = NULL;
104 kernel_uarg->uspace_uarg = NULL;
[91001e2]105
[c98e6ee]106 /*
107 * Create the main thread.
108 */
[91001e2]109 prg->main_thread = thread_create(uinit, kernel_uarg, prg->task,
[6eef3c4]110 THREAD_FLAG_USPACE, "uinit");
[2902e1bb]111 if (!prg->main_thread) {
112 free(kernel_uarg);
113 as_area_destroy(as, virt);
114 task_destroy(prg->task);
[91001e2]115 return ELIMIT;
[2902e1bb]116 }
[91001e2]117
118 return EOK;
[c98e6ee]119}
120
121/** Parse an executable image in the kernel memory.
122 *
123 * If the image belongs to a program loader, it is registered as such,
124 * (and *task is set to NULL). Otherwise a task is created from the
125 * executable image. The task is returned in *task.
126 *
[db675dd]127 * @param[in] image_addr Address of an executable program image.
128 * @param[in] name Name to set for the program's task.
129 * @param[out] prg Buffer for storing program info.
130 * If image_addr points to a loader image,
131 * prg->task will be set to NULL and EOK
132 * will be returned.
[c98e6ee]133 *
134 * @return EOK on success or negative error code.
[91001e2]135 *
[c98e6ee]136 */
[91001e2]137int program_create_from_image(void *image_addr, char *name, program_t *prg)
[c98e6ee]138{
[91001e2]139 as_t *as = as_create(0);
140 if (!as)
141 return ENOMEM;
142
[db675dd]143 prg->loader_status = elf_load((elf_header_t *) image_addr, as, 0);
144 if (prg->loader_status != EE_OK) {
[c98e6ee]145 as_destroy(as);
[91001e2]146 prg->task = NULL;
147 prg->main_thread = NULL;
148
[db675dd]149 if (prg->loader_status != EE_LOADER)
[c98e6ee]150 return ENOTSUP;
151
152 /* Register image as the program loader */
[91001e2]153 if (program_loader != NULL)
154 return ELIMIT;
155
[c98e6ee]156 program_loader = image_addr;
[db675dd]157 printf("Program loader at %p\n", (void *) image_addr);
[91001e2]158
[c98e6ee]159 return EOK;
160 }
[91001e2]161
162 return program_create(as, ((elf_header_t *) image_addr)->e_entry,
163 name, prg);
[c98e6ee]164}
165
166/** Create a task from the program loader image.
167 *
[91001e2]168 * @param prg Buffer for storing program info.
169 * @param name Name to set for the program's task.
[24345a5]170 *
[c98e6ee]171 * @return EOK on success or negative error code.
[91001e2]172 *
[c98e6ee]173 */
[91001e2]174int program_create_loader(program_t *prg, char *name)
[c98e6ee]175{
[91001e2]176 as_t *as = as_create(0);
177 if (!as)
178 return ENOMEM;
179
180 void *loader = program_loader;
[c98e6ee]181 if (!loader) {
[bfe43d5a]182 as_destroy(as);
[c98e6ee]183 printf("Cannot spawn loader as none was registered\n");
184 return ENOENT;
185 }
[91001e2]186
[db675dd]187 prg->loader_status = elf_load((elf_header_t *) program_loader, as,
[91001e2]188 ELD_F_LOADER);
[db675dd]189 if (prg->loader_status != EE_OK) {
[c98e6ee]190 as_destroy(as);
[db675dd]191 printf("Cannot spawn loader (%s)\n",
192 elf_error(prg->loader_status));
[c98e6ee]193 return ENOENT;
194 }
[91001e2]195
196 return program_create(as, ((elf_header_t *) program_loader)->e_entry,
197 name, prg);
[c98e6ee]198}
199
200/** Make program ready.
201 *
202 * Switch program's main thread to the ready state.
203 *
[91001e2]204 * @param prg Program to make ready.
205 *
[c98e6ee]206 */
[91001e2]207void program_ready(program_t *prg)
[c98e6ee]208{
[91001e2]209 thread_ready(prg->main_thread);
[c98e6ee]210}
211
212/** Syscall for creating a new loader instance from userspace.
213 *
[bfd1546]214 * Creates a new task from the program loader image and sets
215 * the task name.
[c98e6ee]216 *
[91001e2]217 * @param uspace_name Name to set on the new task (typically the same
218 * as the command used to execute it).
219 * @param name_len Length of the name.
220 *
221 * @return EOK on success or an error code from @ref errno.h.
[c98e6ee]222 *
223 */
[96b02eb9]224sysarg_t sys_program_spawn_loader(char *uspace_name, size_t name_len)
[c98e6ee]225{
[24345a5]226 /* Cap length of name and copy it from userspace. */
[bc18d63]227 if (name_len > TASK_NAME_BUFLEN - 1)
228 name_len = TASK_NAME_BUFLEN - 1;
[91001e2]229
230 char namebuf[TASK_NAME_BUFLEN];
231 int rc = copy_from_uspace(namebuf, uspace_name, name_len);
[24345a5]232 if (rc != 0)
[96b02eb9]233 return (sysarg_t) rc;
[91001e2]234
[b60c582]235 namebuf[name_len] = 0;
[91001e2]236
[24345a5]237 /* Spawn the new task. */
[91001e2]238 program_t prg;
239 rc = program_create_loader(&prg, namebuf);
[c98e6ee]240 if (rc != 0)
241 return rc;
[91001e2]242
[c98e6ee]243 // FIXME: control the capabilities
[91001e2]244 cap_set(prg.task, cap_get(TASK));
245 program_ready(&prg);
246
[c98e6ee]247 return EOK;
248}
249
250/** @}
251 */
Note: See TracBrowser for help on using the repository browser.