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