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

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

do not provide general access to kernel headers from uspace, only allow specific headers to be accessed or shared
externalize headers which serve as kernel/uspace API/ABI into a special tree

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