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