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