| 1 | /*
|
|---|
| 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
|---|
| 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 |
|
|---|
| 29 | /** @addtogroup main
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * @file
|
|---|
| 35 | * @brief Kernel initialization thread.
|
|---|
| 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 |
|
|---|
| 44 | #include <main/kinit.h>
|
|---|
| 45 | #include <config.h>
|
|---|
| 46 | #include <arch.h>
|
|---|
| 47 | #include <proc/scheduler.h>
|
|---|
| 48 | #include <proc/task.h>
|
|---|
| 49 | #include <proc/thread.h>
|
|---|
| 50 | #include <proc/program.h>
|
|---|
| 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>
|
|---|
| 57 | #include <mm/as.h>
|
|---|
| 58 | #include <mm/frame.h>
|
|---|
| 59 | #include <print.h>
|
|---|
| 60 | #include <memstr.h>
|
|---|
| 61 | #include <console/console.h>
|
|---|
| 62 | #include <interrupt.h>
|
|---|
| 63 | #include <console/kconsole.h>
|
|---|
| 64 | #include <security/cap.h>
|
|---|
| 65 | #include <lib/rd.h>
|
|---|
| 66 | #include <ipc/ipc.h>
|
|---|
| 67 | #include <debug.h>
|
|---|
| 68 | #include <str.h>
|
|---|
| 69 | #include <sysinfo/stats.h>
|
|---|
| 70 |
|
|---|
| 71 | #ifdef CONFIG_SMP
|
|---|
| 72 | #include <smp/smp.h>
|
|---|
| 73 | #endif /* CONFIG_SMP */
|
|---|
| 74 |
|
|---|
| 75 | #include <synch/waitq.h>
|
|---|
| 76 | #include <synch/spinlock.h>
|
|---|
| 77 |
|
|---|
| 78 | #define ALIVE_CHARS 4
|
|---|
| 79 |
|
|---|
| 80 | #ifdef CONFIG_KCONSOLE
|
|---|
| 81 | static char alive[ALIVE_CHARS] = "-\\|/";
|
|---|
| 82 | #endif
|
|---|
| 83 |
|
|---|
| 84 | #define INIT_PREFIX "init:"
|
|---|
| 85 | #define INIT_PREFIX_LEN 5
|
|---|
| 86 |
|
|---|
| 87 | /** Kernel initialization thread.
|
|---|
| 88 | *
|
|---|
| 89 | * kinit takes care of higher level kernel
|
|---|
| 90 | * initialization (i.e. thread creation,
|
|---|
| 91 | * userspace initialization etc.).
|
|---|
| 92 | *
|
|---|
| 93 | * @param arg Not used.
|
|---|
| 94 | */
|
|---|
| 95 | void kinit(void *arg)
|
|---|
| 96 | {
|
|---|
| 97 | thread_t *thread;
|
|---|
| 98 |
|
|---|
| 99 | /*
|
|---|
| 100 | * Detach kinit as nobody will call thread_join_timeout() on it.
|
|---|
| 101 | */
|
|---|
| 102 | thread_detach(THREAD);
|
|---|
| 103 |
|
|---|
| 104 | interrupts_disable();
|
|---|
| 105 |
|
|---|
| 106 | #ifdef CONFIG_SMP
|
|---|
| 107 | if (config.cpu_count > 1) {
|
|---|
| 108 | waitq_initialize(&ap_completion_wq);
|
|---|
| 109 |
|
|---|
| 110 | /*
|
|---|
| 111 | * Create the kmp thread and wait for its completion.
|
|---|
| 112 | * cpu1 through cpuN-1 will come up consecutively and
|
|---|
| 113 | * not mess together with kcpulb threads.
|
|---|
| 114 | * Just a beautification.
|
|---|
| 115 | */
|
|---|
| 116 | thread = thread_create(kmp, NULL, TASK, THREAD_FLAG_WIRED, "kmp", true);
|
|---|
| 117 | if (thread != NULL) {
|
|---|
| 118 | irq_spinlock_lock(&thread->lock, false);
|
|---|
| 119 | thread->cpu = &cpus[0];
|
|---|
| 120 | irq_spinlock_unlock(&thread->lock, false);
|
|---|
| 121 | thread_ready(thread);
|
|---|
| 122 | } else
|
|---|
| 123 | panic("Unable to create kmp thread.");
|
|---|
| 124 |
|
|---|
| 125 | thread_join(thread);
|
|---|
| 126 | thread_detach(thread);
|
|---|
| 127 |
|
|---|
| 128 | /*
|
|---|
| 129 | * For each CPU, create its load balancing thread.
|
|---|
| 130 | */
|
|---|
| 131 | unsigned int i;
|
|---|
| 132 |
|
|---|
| 133 | for (i = 0; i < config.cpu_count; i++) {
|
|---|
| 134 | thread = thread_create(kcpulb, NULL, TASK, THREAD_FLAG_WIRED, "kcpulb", true);
|
|---|
| 135 | if (thread != NULL) {
|
|---|
| 136 | irq_spinlock_lock(&thread->lock, false);
|
|---|
| 137 | thread->cpu = &cpus[i];
|
|---|
| 138 | irq_spinlock_unlock(&thread->lock, false);
|
|---|
| 139 | thread_ready(thread);
|
|---|
| 140 | } else
|
|---|
| 141 | printf("Unable to create kcpulb thread for cpu%u\n", i);
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 | #endif /* CONFIG_SMP */
|
|---|
| 145 |
|
|---|
| 146 | /*
|
|---|
| 147 | * At this point SMP, if present, is configured.
|
|---|
| 148 | */
|
|---|
| 149 | arch_post_smp_init();
|
|---|
| 150 |
|
|---|
| 151 | /* Start thread computing system load */
|
|---|
| 152 | thread = thread_create(kload, NULL, TASK, 0, "kload", false);
|
|---|
| 153 | if (thread != NULL)
|
|---|
| 154 | thread_ready(thread);
|
|---|
| 155 | else
|
|---|
| 156 | printf("Unable to create kload thread\n");
|
|---|
| 157 |
|
|---|
| 158 | #ifdef CONFIG_KCONSOLE
|
|---|
| 159 | if (stdin) {
|
|---|
| 160 | /*
|
|---|
| 161 | * Create kernel console.
|
|---|
| 162 | */
|
|---|
| 163 | thread = thread_create(kconsole_thread, NULL, TASK, 0, "kconsole", false);
|
|---|
| 164 | if (thread != NULL)
|
|---|
| 165 | thread_ready(thread);
|
|---|
| 166 | else
|
|---|
| 167 | printf("Unable to create kconsole thread\n");
|
|---|
| 168 | }
|
|---|
| 169 | #endif /* CONFIG_KCONSOLE */
|
|---|
| 170 |
|
|---|
| 171 | interrupts_enable();
|
|---|
| 172 |
|
|---|
| 173 | /*
|
|---|
| 174 | * Create user tasks, load RAM disk images.
|
|---|
| 175 | */
|
|---|
| 176 | size_t i;
|
|---|
| 177 | program_t programs[CONFIG_INIT_TASKS];
|
|---|
| 178 |
|
|---|
| 179 | for (i = 0; i < init.cnt; i++) {
|
|---|
| 180 | if (init.tasks[i].addr % FRAME_SIZE) {
|
|---|
| 181 | printf("init[%zu].addr is not frame aligned\n", i);
|
|---|
| 182 | programs[i].task = NULL;
|
|---|
| 183 | continue;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | /*
|
|---|
| 187 | * Construct task name from the 'init:' prefix and the
|
|---|
| 188 | * name stored in the init structure (if any).
|
|---|
| 189 | */
|
|---|
| 190 |
|
|---|
| 191 | char namebuf[TASK_NAME_BUFLEN];
|
|---|
| 192 |
|
|---|
| 193 | const char *name = init.tasks[i].name;
|
|---|
| 194 | if (name[0] == 0)
|
|---|
| 195 | name = "<unknown>";
|
|---|
| 196 |
|
|---|
| 197 | ASSERT(TASK_NAME_BUFLEN >= INIT_PREFIX_LEN);
|
|---|
| 198 | str_cpy(namebuf, TASK_NAME_BUFLEN, INIT_PREFIX);
|
|---|
| 199 | str_cpy(namebuf + INIT_PREFIX_LEN,
|
|---|
| 200 | TASK_NAME_BUFLEN - INIT_PREFIX_LEN, name);
|
|---|
| 201 |
|
|---|
| 202 | int rc = program_create_from_image((void *) init.tasks[i].addr,
|
|---|
| 203 | namebuf, &programs[i]);
|
|---|
| 204 |
|
|---|
| 205 | if ((rc == 0) && (programs[i].task != NULL)) {
|
|---|
| 206 | /*
|
|---|
| 207 | * Set capabilities to init userspace tasks.
|
|---|
| 208 | */
|
|---|
| 209 | cap_set(programs[i].task, CAP_CAP | CAP_MEM_MANAGER |
|
|---|
| 210 | CAP_IO_MANAGER | CAP_IRQ_REG);
|
|---|
| 211 |
|
|---|
| 212 | if (!ipc_phone_0)
|
|---|
| 213 | ipc_phone_0 = &programs[i].task->answerbox;
|
|---|
| 214 | } else if (rc == 0) {
|
|---|
| 215 | /* It was the program loader and was registered */
|
|---|
| 216 | } else {
|
|---|
| 217 | /* RAM disk image */
|
|---|
| 218 | int rd = init_rd((rd_header_t *) init.tasks[i].addr, init.tasks[i].size);
|
|---|
| 219 |
|
|---|
| 220 | if (rd != RE_OK)
|
|---|
| 221 | printf("Init binary %zu not used (error %d)\n", i, rd);
|
|---|
| 222 | }
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | /*
|
|---|
| 226 | * Run user tasks.
|
|---|
| 227 | */
|
|---|
| 228 | for (i = 0; i < init.cnt; i++) {
|
|---|
| 229 | if (programs[i].task != NULL)
|
|---|
| 230 | program_ready(&programs[i]);
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | #ifdef CONFIG_KCONSOLE
|
|---|
| 234 | if (!stdin) {
|
|---|
| 235 | thread_sleep(10);
|
|---|
| 236 | printf("kinit: No stdin\nKernel alive: .");
|
|---|
| 237 |
|
|---|
| 238 | unsigned int i = 0;
|
|---|
| 239 | while (true) {
|
|---|
| 240 | printf("\b%c", alive[i % ALIVE_CHARS]);
|
|---|
| 241 | thread_sleep(1);
|
|---|
| 242 | i++;
|
|---|
| 243 | }
|
|---|
| 244 | }
|
|---|
| 245 | #endif /* CONFIG_KCONSOLE */
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | /** @}
|
|---|
| 249 | */
|
|---|