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