| 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 kernel_generic_proc
|
|---|
| 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 <stdlib.h>
|
|---|
| 44 | #include <arch.h>
|
|---|
| 45 | #include <adt/list.h>
|
|---|
| 46 | #include <ipc/ipc.h>
|
|---|
| 47 | #include <ipc/ipcrsc.h>
|
|---|
| 48 | #include <security/perm.h>
|
|---|
| 49 | #include <lib/elf_load.h>
|
|---|
| 50 | #include <str.h>
|
|---|
| 51 | #include <str_error.h>
|
|---|
| 52 | #include <log.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 | */
|
|---|
| 60 | void *program_loader = NULL;
|
|---|
| 61 |
|
|---|
| 62 | /** Create a program using an existing address space.
|
|---|
| 63 | *
|
|---|
| 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 an error code.
|
|---|
| 70 | *
|
|---|
| 71 | */
|
|---|
| 72 | errno_t program_create(as_t *as, uspace_addr_t entry_addr, char *name, program_t *prg)
|
|---|
| 73 | {
|
|---|
| 74 | uspace_arg_t *kernel_uarg = (uspace_arg_t *)
|
|---|
| 75 | malloc(sizeof(uspace_arg_t));
|
|---|
| 76 | if (!kernel_uarg)
|
|---|
| 77 | return ENOMEM;
|
|---|
| 78 |
|
|---|
| 79 | prg->loader_status = EOK;
|
|---|
| 80 | prg->task = task_create(as, name);
|
|---|
| 81 | if (!prg->task) {
|
|---|
| 82 | free(kernel_uarg);
|
|---|
| 83 | return ELIMIT;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | /*
|
|---|
| 87 | * Create the stack address space area.
|
|---|
| 88 | */
|
|---|
| 89 | uintptr_t virt = (uintptr_t) AS_AREA_ANY;
|
|---|
| 90 | uintptr_t bound = USER_ADDRESS_SPACE_END - (STACK_SIZE_USER - 1);
|
|---|
| 91 |
|
|---|
| 92 | /* Adjust bound to create space for the desired guard page. */
|
|---|
| 93 | bound -= PAGE_SIZE;
|
|---|
| 94 |
|
|---|
| 95 | as_area_t *area = as_area_create(as,
|
|---|
| 96 | AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE | AS_AREA_GUARD |
|
|---|
| 97 | AS_AREA_LATE_RESERVE, STACK_SIZE_USER, AS_AREA_ATTR_NONE,
|
|---|
| 98 | &anon_backend, NULL, &virt, bound);
|
|---|
| 99 | if (!area) {
|
|---|
| 100 | free(kernel_uarg);
|
|---|
| 101 | task_destroy(prg->task);
|
|---|
| 102 | prg->task = NULL;
|
|---|
| 103 | return ENOMEM;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | kernel_uarg->uspace_entry = entry_addr;
|
|---|
| 107 | kernel_uarg->uspace_stack = virt;
|
|---|
| 108 | kernel_uarg->uspace_stack_size = STACK_SIZE_USER;
|
|---|
| 109 | kernel_uarg->uspace_thread_function = USPACE_NULL;
|
|---|
| 110 | kernel_uarg->uspace_thread_arg = USPACE_NULL;
|
|---|
| 111 | kernel_uarg->uspace_uarg = USPACE_NULL;
|
|---|
| 112 |
|
|---|
| 113 | /*
|
|---|
| 114 | * Create the main thread.
|
|---|
| 115 | */
|
|---|
| 116 | prg->main_thread = thread_create(uinit, kernel_uarg, prg->task,
|
|---|
| 117 | THREAD_FLAG_USPACE, "uinit");
|
|---|
| 118 | if (!prg->main_thread) {
|
|---|
| 119 | free(kernel_uarg);
|
|---|
| 120 | as_area_destroy(as, virt);
|
|---|
| 121 | task_destroy(prg->task);
|
|---|
| 122 | prg->task = NULL;
|
|---|
| 123 | return ELIMIT;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | return EOK;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | /** Parse an executable image in the kernel memory.
|
|---|
| 130 | *
|
|---|
| 131 | * If the image belongs to a program loader, it is registered as such,
|
|---|
| 132 | * (and *task is set to NULL). Otherwise a task is created from the
|
|---|
| 133 | * executable image. The task is returned in *task.
|
|---|
| 134 | *
|
|---|
| 135 | * @param[in] image_addr Address of an executable program image.
|
|---|
| 136 | * @param[in] name Name to set for the program's task.
|
|---|
| 137 | * @param[out] prg Buffer for storing program info.
|
|---|
| 138 | * If image_addr points to a loader image,
|
|---|
| 139 | * prg->task will be set to NULL and EOK
|
|---|
| 140 | * will be returned.
|
|---|
| 141 | *
|
|---|
| 142 | * @return EOK on success or an error code.
|
|---|
| 143 | *
|
|---|
| 144 | */
|
|---|
| 145 | errno_t program_create_from_image(void *image_addr, size_t image_size, char *name, program_t *prg)
|
|---|
| 146 | {
|
|---|
| 147 | as_t *as = as_create(0);
|
|---|
| 148 | if (!as)
|
|---|
| 149 | return ENOMEM;
|
|---|
| 150 |
|
|---|
| 151 | prg->loader_status = elf_load((elf_header_t *) image_addr, as);
|
|---|
| 152 | if (prg->loader_status != EOK) {
|
|---|
| 153 | as_release(as);
|
|---|
| 154 | prg->task = NULL;
|
|---|
| 155 | prg->main_thread = NULL;
|
|---|
| 156 | return ENOTSUP;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | errno_t rc = program_create(as, ((elf_header_t *) image_addr)->e_entry,
|
|---|
| 160 | name, prg);
|
|---|
| 161 |
|
|---|
| 162 | if (rc == EOK) {
|
|---|
| 163 | prg->task->debug_sections = calloc(1, sizeof(debug_sections_t));
|
|---|
| 164 | if (prg->task->debug_sections != NULL)
|
|---|
| 165 | *prg->task->debug_sections = get_debug_sections(image_addr, image_size);
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | return rc;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | /** Create a task from the program loader image.
|
|---|
| 172 | *
|
|---|
| 173 | * @param prg Buffer for storing program info.
|
|---|
| 174 | * @param name Name to set for the program's task.
|
|---|
| 175 | *
|
|---|
| 176 | * @return EOK on success or an error code.
|
|---|
| 177 | *
|
|---|
| 178 | */
|
|---|
| 179 | errno_t program_create_loader(program_t *prg, char *name)
|
|---|
| 180 | {
|
|---|
| 181 | as_t *as = as_create(0);
|
|---|
| 182 | if (!as)
|
|---|
| 183 | return ENOMEM;
|
|---|
| 184 |
|
|---|
| 185 | void *loader = program_loader;
|
|---|
| 186 | if (!loader) {
|
|---|
| 187 | as_release(as);
|
|---|
| 188 | log(LF_OTHER, LVL_ERROR,
|
|---|
| 189 | "Cannot spawn loader as none was registered");
|
|---|
| 190 | return ENOENT;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | prg->loader_status = elf_load((elf_header_t *) program_loader, as);
|
|---|
| 194 | if (prg->loader_status != EOK) {
|
|---|
| 195 | as_release(as);
|
|---|
| 196 | log(LF_OTHER, LVL_ERROR, "Cannot spawn loader (%s)",
|
|---|
| 197 | str_error(prg->loader_status));
|
|---|
| 198 | return prg->loader_status;
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | return program_create(as, ((elf_header_t *) program_loader)->e_entry,
|
|---|
| 202 | name, prg);
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | /** Make program ready.
|
|---|
| 206 | *
|
|---|
| 207 | * Switch program's main thread to the ready state.
|
|---|
| 208 | *
|
|---|
| 209 | * @param prg Program to make ready.
|
|---|
| 210 | *
|
|---|
| 211 | */
|
|---|
| 212 | void program_ready(program_t *prg)
|
|---|
| 213 | {
|
|---|
| 214 | thread_start(prg->main_thread);
|
|---|
| 215 | thread_detach(prg->main_thread);
|
|---|
| 216 | prg->main_thread = NULL;
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | /** Syscall for creating a new loader instance from userspace.
|
|---|
| 220 | *
|
|---|
| 221 | * Creates a new task from the program loader image and sets
|
|---|
| 222 | * the task name.
|
|---|
| 223 | *
|
|---|
| 224 | * @param uspace_name Name to set on the new task (typically the same
|
|---|
| 225 | * as the command used to execute it).
|
|---|
| 226 | * @param name_len Length of the name.
|
|---|
| 227 | *
|
|---|
| 228 | * @return EOK on success or an error code from @ref errno.h.
|
|---|
| 229 | *
|
|---|
| 230 | */
|
|---|
| 231 | sys_errno_t sys_program_spawn_loader(uspace_ptr_char uspace_name, size_t name_len)
|
|---|
| 232 | {
|
|---|
| 233 | /* Cap length of name and copy it from userspace. */
|
|---|
| 234 | if (name_len > TASK_NAME_BUFLEN - 1)
|
|---|
| 235 | name_len = TASK_NAME_BUFLEN - 1;
|
|---|
| 236 |
|
|---|
| 237 | char namebuf[TASK_NAME_BUFLEN];
|
|---|
| 238 | errno_t rc = copy_from_uspace(namebuf, uspace_name, name_len);
|
|---|
| 239 | if (rc != EOK)
|
|---|
| 240 | return (sys_errno_t) rc;
|
|---|
| 241 |
|
|---|
| 242 | namebuf[name_len] = 0;
|
|---|
| 243 |
|
|---|
| 244 | /* Spawn the new task. */
|
|---|
| 245 | program_t prg;
|
|---|
| 246 | rc = program_create_loader(&prg, namebuf);
|
|---|
| 247 | if (rc != EOK)
|
|---|
| 248 | return rc;
|
|---|
| 249 |
|
|---|
| 250 | // FIXME: control the permissions
|
|---|
| 251 | perm_set(prg.task, perm_get(TASK));
|
|---|
| 252 | program_ready(&prg);
|
|---|
| 253 |
|
|---|
| 254 | return EOK;
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | /** @}
|
|---|
| 258 | */
|
|---|