[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>
|
---|
[fdfb24e] | 51 | #include <str_error.h>
|
---|
[b2fa1204] | 52 | #include <log.h>
|
---|
[c98e6ee] | 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 | *
|
---|
[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 | *
|
---|
[cde999a] | 69 | * @return EOK on success or an error code.
|
---|
[91001e2] | 70 | *
|
---|
[c98e6ee] | 71 | */
|
---|
[5a5269d] | 72 | errno_t program_create(as_t *as, uspace_addr_t entry_addr, char *name, program_t *prg)
|
---|
[c98e6ee] | 73 | {
|
---|
[7473807] | 74 | uspace_arg_t *kernel_uarg = (uspace_arg_t *)
|
---|
[11b285d] | 75 | malloc(sizeof(uspace_arg_t));
|
---|
[7473807] | 76 | if (!kernel_uarg)
|
---|
| 77 | return ENOMEM;
|
---|
| 78 |
|
---|
[bdca26a] | 79 | prg->loader_status = EOK;
|
---|
[91001e2] | 80 | prg->task = task_create(as, name);
|
---|
[7473807] | 81 | if (!prg->task) {
|
---|
| 82 | free(kernel_uarg);
|
---|
[91001e2] | 83 | return ELIMIT;
|
---|
[7473807] | 84 | }
|
---|
[a35b458] | 85 |
|
---|
[c98e6ee] | 86 | /*
|
---|
[26aafe8] | 87 | * Create the stack address space area.
|
---|
[c98e6ee] | 88 | */
|
---|
[5a5269d] | 89 | uintptr_t virt = (uintptr_t) AS_AREA_ANY;
|
---|
[d1e8440] | 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 |
|
---|
[91001e2] | 95 | as_area_t *area = as_area_create(as,
|
---|
[3b8a990] | 96 | AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE | AS_AREA_GUARD |
|
---|
[67b152e] | 97 | AS_AREA_LATE_RESERVE, STACK_SIZE_USER, AS_AREA_ATTR_NONE,
|
---|
[d1e8440] | 98 | &anon_backend, NULL, &virt, bound);
|
---|
[2902e1bb] | 99 | if (!area) {
|
---|
[7473807] | 100 | free(kernel_uarg);
|
---|
[07d4271] | 101 | task_release(prg->task);
|
---|
[e190e640] | 102 | prg->task = NULL;
|
---|
[91001e2] | 103 | return ENOMEM;
|
---|
[2902e1bb] | 104 | }
|
---|
[a35b458] | 105 |
|
---|
[5a5269d] | 106 | kernel_uarg->uspace_entry = entry_addr;
|
---|
| 107 | kernel_uarg->uspace_stack = virt;
|
---|
[67b152e] | 108 | kernel_uarg->uspace_stack_size = STACK_SIZE_USER;
|
---|
[5a5269d] | 109 | kernel_uarg->uspace_thread_function = USPACE_NULL;
|
---|
| 110 | kernel_uarg->uspace_thread_arg = USPACE_NULL;
|
---|
| 111 | kernel_uarg->uspace_uarg = USPACE_NULL;
|
---|
[a35b458] | 112 |
|
---|
[c98e6ee] | 113 | /*
|
---|
| 114 | * Create the main thread.
|
---|
| 115 | */
|
---|
[91001e2] | 116 | prg->main_thread = thread_create(uinit, kernel_uarg, prg->task,
|
---|
[6eef3c4] | 117 | THREAD_FLAG_USPACE, "uinit");
|
---|
[2902e1bb] | 118 | if (!prg->main_thread) {
|
---|
| 119 | free(kernel_uarg);
|
---|
| 120 | as_area_destroy(as, virt);
|
---|
[07d4271] | 121 | task_release(prg->task);
|
---|
[e190e640] | 122 | prg->task = NULL;
|
---|
[91001e2] | 123 | return ELIMIT;
|
---|
[2902e1bb] | 124 | }
|
---|
[a35b458] | 125 |
|
---|
[91001e2] | 126 | return EOK;
|
---|
[c98e6ee] | 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 | *
|
---|
[db675dd] | 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.
|
---|
[c98e6ee] | 141 | *
|
---|
[cde999a] | 142 | * @return EOK on success or an error code.
|
---|
[91001e2] | 143 | *
|
---|
[c98e6ee] | 144 | */
|
---|
[40eab9f] | 145 | errno_t program_create_from_image(void *image_addr, size_t image_size, char *name, program_t *prg)
|
---|
[c98e6ee] | 146 | {
|
---|
[91001e2] | 147 | as_t *as = as_create(0);
|
---|
| 148 | if (!as)
|
---|
| 149 | return ENOMEM;
|
---|
[a35b458] | 150 |
|
---|
[57d44dd] | 151 | prg->loader_status = elf_load((elf_header_t *) image_addr, as);
|
---|
[bdca26a] | 152 | if (prg->loader_status != EOK) {
|
---|
[ca21f1e2] | 153 | as_release(as);
|
---|
[91001e2] | 154 | prg->task = NULL;
|
---|
| 155 | prg->main_thread = NULL;
|
---|
[57d44dd] | 156 | return ENOTSUP;
|
---|
[c98e6ee] | 157 | }
|
---|
[a35b458] | 158 |
|
---|
[40eab9f] | 159 | errno_t rc = program_create(as, ((elf_header_t *) image_addr)->e_entry,
|
---|
[91001e2] | 160 | name, prg);
|
---|
[40eab9f] | 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;
|
---|
[c98e6ee] | 169 | }
|
---|
| 170 |
|
---|
| 171 | /** Create a task from the program loader image.
|
---|
| 172 | *
|
---|
[91001e2] | 173 | * @param prg Buffer for storing program info.
|
---|
| 174 | * @param name Name to set for the program's task.
|
---|
[24345a5] | 175 | *
|
---|
[cde999a] | 176 | * @return EOK on success or an error code.
|
---|
[91001e2] | 177 | *
|
---|
[c98e6ee] | 178 | */
|
---|
[b7fd2a0] | 179 | errno_t program_create_loader(program_t *prg, char *name)
|
---|
[c98e6ee] | 180 | {
|
---|
[91001e2] | 181 | as_t *as = as_create(0);
|
---|
| 182 | if (!as)
|
---|
| 183 | return ENOMEM;
|
---|
[a35b458] | 184 |
|
---|
[91001e2] | 185 | void *loader = program_loader;
|
---|
[c98e6ee] | 186 | if (!loader) {
|
---|
[ca21f1e2] | 187 | as_release(as);
|
---|
[b2fa1204] | 188 | log(LF_OTHER, LVL_ERROR,
|
---|
| 189 | "Cannot spawn loader as none was registered");
|
---|
[c98e6ee] | 190 | return ENOENT;
|
---|
| 191 | }
|
---|
[a35b458] | 192 |
|
---|
[57d44dd] | 193 | prg->loader_status = elf_load((elf_header_t *) program_loader, as);
|
---|
[bdca26a] | 194 | if (prg->loader_status != EOK) {
|
---|
[ca21f1e2] | 195 | as_release(as);
|
---|
[b2fa1204] | 196 | log(LF_OTHER, LVL_ERROR, "Cannot spawn loader (%s)",
|
---|
[bdca26a] | 197 | str_error(prg->loader_status));
|
---|
[f8fb03b] | 198 | return prg->loader_status;
|
---|
[c98e6ee] | 199 | }
|
---|
[a35b458] | 200 |
|
---|
[91001e2] | 201 | return program_create(as, ((elf_header_t *) program_loader)->e_entry,
|
---|
| 202 | name, prg);
|
---|
[c98e6ee] | 203 | }
|
---|
| 204 |
|
---|
| 205 | /** Make program ready.
|
---|
| 206 | *
|
---|
| 207 | * Switch program's main thread to the ready state.
|
---|
| 208 | *
|
---|
[91001e2] | 209 | * @param prg Program to make ready.
|
---|
| 210 | *
|
---|
[c98e6ee] | 211 | */
|
---|
[91001e2] | 212 | void program_ready(program_t *prg)
|
---|
[c98e6ee] | 213 | {
|
---|
[0f4f1b2] | 214 | thread_start(prg->main_thread);
|
---|
| 215 | thread_detach(prg->main_thread);
|
---|
[1871118] | 216 | prg->main_thread = NULL;
|
---|
[c98e6ee] | 217 | }
|
---|
| 218 |
|
---|
| 219 | /** Syscall for creating a new loader instance from userspace.
|
---|
| 220 | *
|
---|
[bfd1546] | 221 | * Creates a new task from the program loader image and sets
|
---|
| 222 | * the task name.
|
---|
[c98e6ee] | 223 | *
|
---|
[91001e2] | 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.
|
---|
[c98e6ee] | 229 | *
|
---|
| 230 | */
|
---|
[5a5269d] | 231 | sys_errno_t sys_program_spawn_loader(uspace_ptr_char uspace_name, size_t name_len)
|
---|
[c98e6ee] | 232 | {
|
---|
[24345a5] | 233 | /* Cap length of name and copy it from userspace. */
|
---|
[bc18d63] | 234 | if (name_len > TASK_NAME_BUFLEN - 1)
|
---|
| 235 | name_len = TASK_NAME_BUFLEN - 1;
|
---|
[a35b458] | 236 |
|
---|
[91001e2] | 237 | char namebuf[TASK_NAME_BUFLEN];
|
---|
[b7fd2a0] | 238 | errno_t rc = copy_from_uspace(namebuf, uspace_name, name_len);
|
---|
[a53ed3a] | 239 | if (rc != EOK)
|
---|
[b7fd2a0] | 240 | return (sys_errno_t) rc;
|
---|
[a35b458] | 241 |
|
---|
[b60c582] | 242 | namebuf[name_len] = 0;
|
---|
[a35b458] | 243 |
|
---|
[24345a5] | 244 | /* Spawn the new task. */
|
---|
[91001e2] | 245 | program_t prg;
|
---|
| 246 | rc = program_create_loader(&prg, namebuf);
|
---|
[a53ed3a] | 247 | if (rc != EOK)
|
---|
[c98e6ee] | 248 | return rc;
|
---|
[a35b458] | 249 |
|
---|
[1b20da0] | 250 | // FIXME: control the permissions
|
---|
[719a208] | 251 | perm_set(prg.task, perm_get(TASK));
|
---|
[91001e2] | 252 | program_ready(&prg);
|
---|
[a35b458] | 253 |
|
---|
[f8b69a1e] | 254 | task_release(prg.task);
|
---|
| 255 |
|
---|
[c98e6ee] | 256 | return EOK;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | /** @}
|
---|
| 260 | */
|
---|