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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3666d386 was e16e2ba4, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Eliminate userspace copy of elf.h (ELF format definitions).

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