[f761f1eb] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
[f761f1eb] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[8e3bf3e2] | 29 | /** @addtogroup main
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
[9179d0a] | 33 | /**
|
---|
[b45c443] | 34 | * @file
|
---|
[2f57690] | 35 | * @brief Kernel initialization thread.
|
---|
[9179d0a] | 36 | *
|
---|
| 37 | * This file contains kinit kernel thread which carries out
|
---|
| 38 | * high level system initialization.
|
---|
| 39 | *
|
---|
| 40 | * This file is responsible for finishing SMP configuration
|
---|
| 41 | * and creation of userspace init tasks.
|
---|
| 42 | */
|
---|
| 43 |
|
---|
[f761f1eb] | 44 | #include <main/kinit.h>
|
---|
[2677758] | 45 | #include <config.h>
|
---|
| 46 | #include <arch.h>
|
---|
[f761f1eb] | 47 | #include <proc/scheduler.h>
|
---|
| 48 | #include <proc/task.h>
|
---|
| 49 | #include <proc/thread.h>
|
---|
[c98e6ee] | 50 | #include <proc/program.h>
|
---|
[f761f1eb] | 51 | #include <panic.h>
|
---|
| 52 | #include <func.h>
|
---|
| 53 | #include <cpu.h>
|
---|
| 54 | #include <arch/asm.h>
|
---|
| 55 | #include <mm/page.h>
|
---|
| 56 | #include <arch/mm/page.h>
|
---|
[20d50a1] | 57 | #include <mm/as.h>
|
---|
[ff3b3197] | 58 | #include <mm/frame.h>
|
---|
[32817cc] | 59 | #include <mm/km.h>
|
---|
[9c0a9b3] | 60 | #include <print.h>
|
---|
[399ccd9] | 61 | #include <memstr.h>
|
---|
[ff3b3197] | 62 | #include <console/console.h>
|
---|
[aace6624] | 63 | #include <interrupt.h>
|
---|
[f4338d2] | 64 | #include <console/kconsole.h>
|
---|
[1077d91] | 65 | #include <security/cap.h>
|
---|
[d4b5542] | 66 | #include <lib/rd.h>
|
---|
[b3f8fb7] | 67 | #include <ipc/ipc.h>
|
---|
[20f1597] | 68 | #include <debug.h>
|
---|
[19f857a] | 69 | #include <str.h>
|
---|
[9dae191e] | 70 | #include <sysinfo/stats.h>
|
---|
[c8cbd39] | 71 | #include <sysinfo/sysinfo.h>
|
---|
[32817cc] | 72 | #include <align.h>
|
---|
[f761f1eb] | 73 |
|
---|
[5f85c91] | 74 | #ifdef CONFIG_SMP
|
---|
[26678e5] | 75 | #include <smp/smp.h>
|
---|
[5f85c91] | 76 | #endif /* CONFIG_SMP */
|
---|
[f761f1eb] | 77 |
|
---|
| 78 | #include <synch/waitq.h>
|
---|
| 79 | #include <synch/spinlock.h>
|
---|
| 80 |
|
---|
[c19a4169] | 81 | #define ALIVE_CHARS 4
|
---|
| 82 |
|
---|
[753d851] | 83 | #ifdef CONFIG_KCONSOLE
|
---|
[c19a4169] | 84 | static char alive[ALIVE_CHARS] = "-\\|/";
|
---|
[753d851] | 85 | #endif
|
---|
[c19a4169] | 86 |
|
---|
[da581872] | 87 | #define INIT_PREFIX "init:"
|
---|
| 88 | #define INIT_PREFIX_LEN 5
|
---|
[20f1597] | 89 |
|
---|
[880de6e] | 90 | /** Kernel initialization thread.
|
---|
| 91 | *
|
---|
| 92 | * kinit takes care of higher level kernel
|
---|
| 93 | * initialization (i.e. thread creation,
|
---|
| 94 | * userspace initialization etc.).
|
---|
| 95 | *
|
---|
| 96 | * @param arg Not used.
|
---|
| 97 | */
|
---|
[f761f1eb] | 98 | void kinit(void *arg)
|
---|
| 99 | {
|
---|
[76fca31] | 100 | thread_t *thread;
|
---|
[2f57690] | 101 |
|
---|
[2cb5e64] | 102 | /*
|
---|
| 103 | * Detach kinit as nobody will call thread_join_timeout() on it.
|
---|
| 104 | */
|
---|
| 105 | thread_detach(THREAD);
|
---|
[2f57690] | 106 |
|
---|
[22f7769] | 107 | interrupts_disable();
|
---|
[2f57690] | 108 |
|
---|
| 109 | #ifdef CONFIG_SMP
|
---|
[f761f1eb] | 110 | if (config.cpu_count > 1) {
|
---|
[26678e5] | 111 | waitq_initialize(&ap_completion_wq);
|
---|
[49eb681] | 112 |
|
---|
[f761f1eb] | 113 | /*
|
---|
| 114 | * Create the kmp thread and wait for its completion.
|
---|
| 115 | * cpu1 through cpuN-1 will come up consecutively and
|
---|
[169c408] | 116 | * not mess together with kcpulb threads.
|
---|
[f761f1eb] | 117 | * Just a beautification.
|
---|
| 118 | */
|
---|
[6eef3c4] | 119 | thread = thread_create(kmp, NULL, TASK,
|
---|
| 120 | THREAD_FLAG_UNCOUNTED, "kmp");
|
---|
[76fca31] | 121 | if (thread != NULL) {
|
---|
[6eef3c4] | 122 | thread_wire(thread, &cpus[0]);
|
---|
[76fca31] | 123 | thread_ready(thread);
|
---|
[8e3bf3e2] | 124 | } else
|
---|
[f651e80] | 125 | panic("Unable to create kmp thread.");
|
---|
[9dae191e] | 126 |
|
---|
[76fca31] | 127 | thread_join(thread);
|
---|
| 128 | thread_detach(thread);
|
---|
[0132630] | 129 |
|
---|
[76cec1e] | 130 | /*
|
---|
[f761f1eb] | 131 | * For each CPU, create its load balancing thread.
|
---|
| 132 | */
|
---|
[7e752b2] | 133 | unsigned int i;
|
---|
[49eb681] | 134 |
|
---|
[f761f1eb] | 135 | for (i = 0; i < config.cpu_count; i++) {
|
---|
[6eef3c4] | 136 | thread = thread_create(kcpulb, NULL, TASK,
|
---|
| 137 | THREAD_FLAG_UNCOUNTED, "kcpulb");
|
---|
[76fca31] | 138 | if (thread != NULL) {
|
---|
[6eef3c4] | 139 | thread_wire(thread, &cpus[i]);
|
---|
[76fca31] | 140 | thread_ready(thread);
|
---|
[8e3bf3e2] | 141 | } else
|
---|
[7e752b2] | 142 | printf("Unable to create kcpulb thread for cpu%u\n", i);
|
---|
[f761f1eb] | 143 | }
|
---|
| 144 | }
|
---|
[5f85c91] | 145 | #endif /* CONFIG_SMP */
|
---|
[76fca31] | 146 |
|
---|
[a83a802] | 147 | /*
|
---|
| 148 | * At this point SMP, if present, is configured.
|
---|
| 149 | */
|
---|
| 150 | arch_post_smp_init();
|
---|
[2f57690] | 151 |
|
---|
[9dae191e] | 152 | /* Start thread computing system load */
|
---|
[6eef3c4] | 153 | thread = thread_create(kload, NULL, TASK, THREAD_FLAG_NONE,
|
---|
| 154 | "kload");
|
---|
[9dae191e] | 155 | if (thread != NULL)
|
---|
| 156 | thread_ready(thread);
|
---|
| 157 | else
|
---|
| 158 | printf("Unable to create kload thread\n");
|
---|
| 159 |
|
---|
[76fca31] | 160 | #ifdef CONFIG_KCONSOLE
|
---|
| 161 | if (stdin) {
|
---|
| 162 | /*
|
---|
| 163 | * Create kernel console.
|
---|
| 164 | */
|
---|
[6eef3c4] | 165 | thread = thread_create(kconsole_thread, NULL, TASK,
|
---|
| 166 | THREAD_FLAG_NONE, "kconsole");
|
---|
[76fca31] | 167 | if (thread != NULL)
|
---|
| 168 | thread_ready(thread);
|
---|
| 169 | else
|
---|
| 170 | printf("Unable to create kconsole thread\n");
|
---|
| 171 | }
|
---|
| 172 | #endif /* CONFIG_KCONSOLE */
|
---|
| 173 |
|
---|
[22f7769] | 174 | interrupts_enable();
|
---|
[49eec93] | 175 |
|
---|
| 176 | /*
|
---|
| 177 | * Create user tasks, load RAM disk images.
|
---|
| 178 | */
|
---|
[98000fb] | 179 | size_t i;
|
---|
[c98e6ee] | 180 | program_t programs[CONFIG_INIT_TASKS];
|
---|
[49eec93] | 181 |
|
---|
[c8cbd39] | 182 | // FIXME: do not propagate arguments through sysinfo
|
---|
| 183 | // but pass them directly to the tasks
|
---|
| 184 | for (i = 0; i < init.cnt; i++) {
|
---|
| 185 | const char *arguments = init.tasks[i].arguments;
|
---|
| 186 | if (str_length(arguments) == 0)
|
---|
| 187 | continue;
|
---|
| 188 | if (str_length(init.tasks[i].name) == 0)
|
---|
| 189 | continue;
|
---|
| 190 | size_t arguments_size = str_size(arguments);
|
---|
| 191 |
|
---|
| 192 | void *arguments_copy = malloc(arguments_size, 0);
|
---|
| 193 | if (arguments_copy == NULL)
|
---|
| 194 | continue;
|
---|
| 195 | memcpy(arguments_copy, arguments, arguments_size);
|
---|
| 196 |
|
---|
| 197 | char item_name[CONFIG_TASK_NAME_BUFLEN + 15];
|
---|
| 198 | snprintf(item_name, CONFIG_TASK_NAME_BUFLEN + 15,
|
---|
| 199 | "init_args.%s", init.tasks[i].name);
|
---|
| 200 |
|
---|
| 201 | sysinfo_set_item_data(item_name, NULL, arguments_copy, arguments_size);
|
---|
| 202 | }
|
---|
| 203 |
|
---|
[b6b576c] | 204 | for (i = 0; i < init.cnt; i++) {
|
---|
[32817cc] | 205 | if (init.tasks[i].paddr % FRAME_SIZE) {
|
---|
[fb48a0e] | 206 | printf("init[%zu]: Address is not frame aligned\n", i);
|
---|
[2319df3] | 207 | programs[i].task = NULL;
|
---|
[228b135] | 208 | continue;
|
---|
| 209 | }
|
---|
[2f57690] | 210 |
|
---|
[20f1597] | 211 | /*
|
---|
[da581872] | 212 | * Construct task name from the 'init:' prefix and the
|
---|
[20f1597] | 213 | * name stored in the init structure (if any).
|
---|
| 214 | */
|
---|
[2f57690] | 215 |
|
---|
| 216 | char namebuf[TASK_NAME_BUFLEN];
|
---|
| 217 |
|
---|
[a000878c] | 218 | const char *name = init.tasks[i].name;
|
---|
[b60c582] | 219 | if (name[0] == 0)
|
---|
[2f57690] | 220 | name = "<unknown>";
|
---|
| 221 |
|
---|
[da581872] | 222 | ASSERT(TASK_NAME_BUFLEN >= INIT_PREFIX_LEN);
|
---|
[f4b1535] | 223 | str_cpy(namebuf, TASK_NAME_BUFLEN, INIT_PREFIX);
|
---|
| 224 | str_cpy(namebuf + INIT_PREFIX_LEN,
|
---|
| 225 | TASK_NAME_BUFLEN - INIT_PREFIX_LEN, name);
|
---|
[db675dd] | 226 |
|
---|
[32817cc] | 227 | /*
|
---|
| 228 | * Create virtual memory mappings for init task images.
|
---|
| 229 | */
|
---|
[221c9ec] | 230 | uintptr_t page = km_map(init.tasks[i].paddr,
|
---|
| 231 | init.tasks[i].size,
|
---|
| 232 | PAGE_READ | PAGE_WRITE | PAGE_CACHEABLE);
|
---|
| 233 | ASSERT(page);
|
---|
[da1bafb] | 234 |
|
---|
[32817cc] | 235 | int rc = program_create_from_image((void *) page, namebuf,
|
---|
| 236 | &programs[i]);
|
---|
[76fca31] | 237 |
|
---|
[fb48a0e] | 238 | if (rc == 0) {
|
---|
| 239 | if (programs[i].task != NULL) {
|
---|
| 240 | /*
|
---|
| 241 | * Set capabilities to init userspace tasks.
|
---|
| 242 | */
|
---|
| 243 | cap_set(programs[i].task, CAP_CAP | CAP_MEM_MANAGER |
|
---|
| 244 | CAP_IO_MANAGER | CAP_IRQ_REG);
|
---|
| 245 |
|
---|
| 246 | if (!ipc_phone_0)
|
---|
| 247 | ipc_phone_0 = &programs[i].task->answerbox;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
[1077d91] | 250 | /*
|
---|
[fb48a0e] | 251 | * If programs[i].task == NULL then it is
|
---|
| 252 | * the program loader and it was registered
|
---|
| 253 | * successfully.
|
---|
[1077d91] | 254 | */
|
---|
[fb48a0e] | 255 | } else if (i == init.cnt - 1) {
|
---|
| 256 | /*
|
---|
| 257 | * Assume the last task is the RAM disk.
|
---|
| 258 | */
|
---|
[221c9ec] | 259 | init_rd((void *) init.tasks[i].paddr, init.tasks[i].size);
|
---|
[fb48a0e] | 260 | } else
|
---|
[db675dd] | 261 | printf("init[%zu]: Init binary load failed "
|
---|
| 262 | "(error %d, loader status %u)\n", i, rc,
|
---|
| 263 | programs[i].loader_status);
|
---|
[49eec93] | 264 | }
|
---|
[da1bafb] | 265 |
|
---|
[49eec93] | 266 | /*
|
---|
[566f4cfb] | 267 | * Run user tasks.
|
---|
[49eec93] | 268 | */
|
---|
| 269 | for (i = 0; i < init.cnt; i++) {
|
---|
[566f4cfb] | 270 | if (programs[i].task != NULL)
|
---|
[c98e6ee] | 271 | program_ready(&programs[i]);
|
---|
[44c259c] | 272 | }
|
---|
[da1bafb] | 273 |
|
---|
[76fca31] | 274 | #ifdef CONFIG_KCONSOLE
|
---|
[ff3b3197] | 275 | if (!stdin) {
|
---|
[c19a4169] | 276 | thread_sleep(10);
|
---|
[eddf924] | 277 | printf("kinit: No stdin\nKernel alive: .");
|
---|
[76fca31] | 278 |
|
---|
[c19a4169] | 279 | unsigned int i = 0;
|
---|
| 280 | while (true) {
|
---|
| 281 | printf("\b%c", alive[i % ALIVE_CHARS]);
|
---|
[ff3b3197] | 282 | thread_sleep(1);
|
---|
[76fca31] | 283 | i++;
|
---|
[ff3b3197] | 284 | }
|
---|
[f761f1eb] | 285 | }
|
---|
[76fca31] | 286 | #endif /* CONFIG_KCONSOLE */
|
---|
[f761f1eb] | 287 | }
|
---|
[b45c443] | 288 |
|
---|
[8e3bf3e2] | 289 | /** @}
|
---|
[b45c443] | 290 | */
|
---|