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
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
[174156fd]30/** @addtogroup kernel_generic_proc
[c98e6ee]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>
[aafed15]43#include <stdlib.h>
[c98e6ee]44#include <arch.h>
45#include <adt/list.h>
46#include <ipc/ipc.h>
47#include <ipc/ipcrsc.h>
[719a208]48#include <security/perm.h>
[e16e2ba4]49#include <lib/elf_load.h>
[bdca26a]50#include <str.h>
[b2fa1204]51#include <log.h>
[c98e6ee]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.
[fb13b44]66 * @param answerbox Answerbox where box 0 is connected to (may be NULL).
[91001e2]67 * @param prg Buffer for storing program information.
68 *
[cde999a]69 * @return EOK on success or an error code.
[91001e2]70 *
[c98e6ee]71 */
[fb13b44]72errno_t program_create(as_t *as, uspace_addr_t entry_addr, char *name,
73 struct answerbox *answerbox, program_t *prg)
[c98e6ee]74{
[7473807]75 uspace_arg_t *kernel_uarg = (uspace_arg_t *)
[11b285d]76 malloc(sizeof(uspace_arg_t));
[7473807]77 if (!kernel_uarg)
78 return ENOMEM;
79
[bdca26a]80 prg->loader_status = EOK;
[fb13b44]81 prg->task = task_create(as, name, answerbox);
[7473807]82 if (!prg->task) {
83 free(kernel_uarg);
[91001e2]84 return ELIMIT;
[7473807]85 }
[a35b458]86
[c98e6ee]87 /*
[26aafe8]88 * Create the stack address space area.
[c98e6ee]89 */
[5a5269d]90 uintptr_t virt = (uintptr_t) AS_AREA_ANY;
[d1e8440]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
[91001e2]96 as_area_t *area = as_area_create(as,
[3b8a990]97 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE | AS_AREA_GUARD |
[67b152e]98 AS_AREA_LATE_RESERVE, STACK_SIZE_USER, AS_AREA_ATTR_NONE,
[d1e8440]99 &anon_backend, NULL, &virt, bound);
[2902e1bb]100 if (!area) {
[7473807]101 free(kernel_uarg);
[2902e1bb]102 task_destroy(prg->task);
[e190e640]103 prg->task = NULL;
[91001e2]104 return ENOMEM;
[2902e1bb]105 }
[a35b458]106
[5a5269d]107 kernel_uarg->uspace_entry = entry_addr;
108 kernel_uarg->uspace_stack = virt;
[67b152e]109 kernel_uarg->uspace_stack_size = STACK_SIZE_USER;
[5a5269d]110 kernel_uarg->uspace_thread_function = USPACE_NULL;
111 kernel_uarg->uspace_thread_arg = USPACE_NULL;
112 kernel_uarg->uspace_uarg = USPACE_NULL;
[a35b458]113
[c98e6ee]114 /*
115 * Create the main thread.
116 */
[91001e2]117 prg->main_thread = thread_create(uinit, kernel_uarg, prg->task,
[6eef3c4]118 THREAD_FLAG_USPACE, "uinit");
[2902e1bb]119 if (!prg->main_thread) {
120 free(kernel_uarg);
121 as_area_destroy(as, virt);
122 task_destroy(prg->task);
[e190e640]123 prg->task = NULL;
[91001e2]124 return ELIMIT;
[2902e1bb]125 }
[a35b458]126
[91001e2]127 return EOK;
[c98e6ee]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 *
[db675dd]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.
[c98e6ee]142 *
[cde999a]143 * @return EOK on success or an error code.
[91001e2]144 *
[c98e6ee]145 */
[b7fd2a0]146errno_t program_create_from_image(void *image_addr, char *name, program_t *prg)
[c98e6ee]147{
[91001e2]148 as_t *as = as_create(0);
149 if (!as)
150 return ENOMEM;
[a35b458]151
[57d44dd]152 prg->loader_status = elf_load((elf_header_t *) image_addr, as);
[bdca26a]153 if (prg->loader_status != EOK) {
[ca21f1e2]154 as_release(as);
[91001e2]155 prg->task = NULL;
156 prg->main_thread = NULL;
[57d44dd]157 return ENOTSUP;
[c98e6ee]158 }
[a35b458]159
[91001e2]160 return program_create(as, ((elf_header_t *) image_addr)->e_entry,
[fb13b44]161 name, ipc_box_0, prg);
[c98e6ee]162}
163
164/** Create a task from the program loader image.
165 *
[91001e2]166 * @param prg Buffer for storing program info.
167 * @param name Name to set for the program's task.
[24345a5]168 *
[cde999a]169 * @return EOK on success or an error code.
[91001e2]170 *
[c98e6ee]171 */
[b7fd2a0]172errno_t program_create_loader(program_t *prg, char *name)
[c98e6ee]173{
[91001e2]174 as_t *as = as_create(0);
175 if (!as)
176 return ENOMEM;
[a35b458]177
[91001e2]178 void *loader = program_loader;
[c98e6ee]179 if (!loader) {
[ca21f1e2]180 as_release(as);
[b2fa1204]181 log(LF_OTHER, LVL_ERROR,
182 "Cannot spawn loader as none was registered");
[c98e6ee]183 return ENOENT;
184 }
[a35b458]185
[57d44dd]186 prg->loader_status = elf_load((elf_header_t *) program_loader, as);
[bdca26a]187 if (prg->loader_status != EOK) {
[ca21f1e2]188 as_release(as);
[b2fa1204]189 log(LF_OTHER, LVL_ERROR, "Cannot spawn loader (%s)",
[bdca26a]190 str_error(prg->loader_status));
[c98e6ee]191 return ENOENT;
192 }
[a35b458]193
[91001e2]194 return program_create(as, ((elf_header_t *) program_loader)->e_entry,
[fb13b44]195 name, &TASK->answerbox, prg);
[c98e6ee]196}
197
198/** Make program ready.
199 *
200 * Switch program's main thread to the ready state.
201 *
[91001e2]202 * @param prg Program to make ready.
203 *
[c98e6ee]204 */
[91001e2]205void program_ready(program_t *prg)
[c98e6ee]206{
[91001e2]207 thread_ready(prg->main_thread);
[c98e6ee]208}
209
210/** Syscall for creating a new loader instance from userspace.
211 *
[bfd1546]212 * Creates a new task from the program loader image and sets
213 * the task name.
[c98e6ee]214 *
[91001e2]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.
[c98e6ee]220 *
221 */
[5a5269d]222sys_errno_t sys_program_spawn_loader(uspace_ptr_char uspace_name, size_t name_len)
[c98e6ee]223{
[24345a5]224 /* Cap length of name and copy it from userspace. */
[bc18d63]225 if (name_len > TASK_NAME_BUFLEN - 1)
226 name_len = TASK_NAME_BUFLEN - 1;
[a35b458]227
[91001e2]228 char namebuf[TASK_NAME_BUFLEN];
[b7fd2a0]229 errno_t rc = copy_from_uspace(namebuf, uspace_name, name_len);
[a53ed3a]230 if (rc != EOK)
[b7fd2a0]231 return (sys_errno_t) rc;
[a35b458]232
[b60c582]233 namebuf[name_len] = 0;
[a35b458]234
[24345a5]235 /* Spawn the new task. */
[91001e2]236 program_t prg;
237 rc = program_create_loader(&prg, namebuf);
[a53ed3a]238 if (rc != EOK)
[c98e6ee]239 return rc;
[a35b458]240
[1b20da0]241 // FIXME: control the permissions
[719a208]242 perm_set(prg.task, perm_get(TASK));
[91001e2]243 program_ready(&prg);
[a35b458]244
[c98e6ee]245 return EOK;
246}
247
248/** @}
249 */
Note: See TracBrowser for help on using the repository browser.