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