| 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 Main initialization kernel function for all processors.
|
|---|
| 36 | *
|
|---|
| 37 | * During kernel boot, all processors, after architecture dependent
|
|---|
| 38 | * initialization, start executing code found in this file. After
|
|---|
| 39 | * bringing up all subsystems, control is passed to scheduler().
|
|---|
| 40 | *
|
|---|
| 41 | * The bootstrap processor starts executing main_bsp() while
|
|---|
| 42 | * the application processors start executing main_ap().
|
|---|
| 43 | *
|
|---|
| 44 | * @see scheduler()
|
|---|
| 45 | * @see main_bsp()
|
|---|
| 46 | * @see main_ap()
|
|---|
| 47 | */
|
|---|
| 48 |
|
|---|
| 49 | #include <arch/asm.h>
|
|---|
| 50 | #include <context.h>
|
|---|
| 51 | #include <print.h>
|
|---|
| 52 | #include <panic.h>
|
|---|
| 53 | #include <debug.h>
|
|---|
| 54 | #include <config.h>
|
|---|
| 55 | #include <time/clock.h>
|
|---|
| 56 | #include <time/timeout.h>
|
|---|
| 57 | #include <proc/scheduler.h>
|
|---|
| 58 | #include <proc/thread.h>
|
|---|
| 59 | #include <proc/task.h>
|
|---|
| 60 | #include <main/kinit.h>
|
|---|
| 61 | #include <main/version.h>
|
|---|
| 62 | #include <console/kconsole.h>
|
|---|
| 63 | #include <console/console.h>
|
|---|
| 64 | #include <log.h>
|
|---|
| 65 | #include <cpu.h>
|
|---|
| 66 | #include <align.h>
|
|---|
| 67 | #include <interrupt.h>
|
|---|
| 68 | #include <mm/frame.h>
|
|---|
| 69 | #include <mm/page.h>
|
|---|
| 70 | #include <genarch/mm/page_pt.h>
|
|---|
| 71 | #include <mm/km.h>
|
|---|
| 72 | #include <mm/tlb.h>
|
|---|
| 73 | #include <mm/as.h>
|
|---|
| 74 | #include <mm/slab.h>
|
|---|
| 75 | #include <mm/reserve.h>
|
|---|
| 76 | #include <synch/waitq.h>
|
|---|
| 77 | #include <synch/futex.h>
|
|---|
| 78 | #include <arch/arch.h>
|
|---|
| 79 | #include <arch.h>
|
|---|
| 80 | #include <arch/faddr.h>
|
|---|
| 81 | #include <ipc/ipc.h>
|
|---|
| 82 | #include <macros.h>
|
|---|
| 83 | #include <adt/btree.h>
|
|---|
| 84 | #include <smp/smp.h>
|
|---|
| 85 | #include <ddi/ddi.h>
|
|---|
| 86 | #include <main/main.h>
|
|---|
| 87 | #include <ipc/event.h>
|
|---|
| 88 | #include <sysinfo/sysinfo.h>
|
|---|
| 89 | #include <sysinfo/stats.h>
|
|---|
| 90 | #include <lib/ra.h>
|
|---|
| 91 |
|
|---|
| 92 | /* Ensure [u]int*_t types are of correct size.
|
|---|
| 93 | *
|
|---|
| 94 | * Probably, this is not the best place for such tests
|
|---|
| 95 | * but this file is compiled on all architectures.
|
|---|
| 96 | */
|
|---|
| 97 | #define CHECK_INT_TYPE_(signness, size) \
|
|---|
| 98 | STATIC_ASSERT_VERBOSE(sizeof(signness##size##_t) * 8 == size, \
|
|---|
| 99 | #signness #size "_t does not have " #size " bits");
|
|---|
| 100 |
|
|---|
| 101 | #define CHECK_INT_TYPE(size) \
|
|---|
| 102 | CHECK_INT_TYPE_(int, size); \
|
|---|
| 103 | CHECK_INT_TYPE_(uint, size)
|
|---|
| 104 |
|
|---|
| 105 | CHECK_INT_TYPE(8);
|
|---|
| 106 | CHECK_INT_TYPE(16);
|
|---|
| 107 | CHECK_INT_TYPE(32);
|
|---|
| 108 | CHECK_INT_TYPE(64);
|
|---|
| 109 |
|
|---|
| 110 | /** Global configuration structure. */
|
|---|
| 111 | config_t config = {
|
|---|
| 112 | .identity_configured = false,
|
|---|
| 113 | .non_identity_configured = false,
|
|---|
| 114 | .physmem_end = 0
|
|---|
| 115 | };
|
|---|
| 116 |
|
|---|
| 117 | /** Initial user-space tasks */
|
|---|
| 118 | init_t init = {
|
|---|
| 119 | .cnt = 0
|
|---|
| 120 | };
|
|---|
| 121 |
|
|---|
| 122 | /** Boot allocations. */
|
|---|
| 123 | ballocs_t ballocs = {
|
|---|
| 124 | .base = (uintptr_t) NULL,
|
|---|
| 125 | .size = 0
|
|---|
| 126 | };
|
|---|
| 127 |
|
|---|
| 128 | context_t ctx;
|
|---|
| 129 |
|
|---|
| 130 | /** Lowest safe stack virtual address. */
|
|---|
| 131 | uintptr_t stack_safe = 0;
|
|---|
| 132 |
|
|---|
| 133 | /*
|
|---|
| 134 | * These two functions prevent stack from underflowing during the
|
|---|
| 135 | * kernel boot phase when SP is set to the very top of the reserved
|
|---|
| 136 | * space. The stack could get corrupted by a fooled compiler-generated
|
|---|
| 137 | * pop sequence otherwise.
|
|---|
| 138 | */
|
|---|
| 139 | static void main_bsp_separated_stack(void);
|
|---|
| 140 |
|
|---|
| 141 | #ifdef CONFIG_SMP
|
|---|
| 142 | static void main_ap_separated_stack(void);
|
|---|
| 143 | #endif
|
|---|
| 144 |
|
|---|
| 145 | /** Main kernel routine for bootstrap CPU.
|
|---|
| 146 | *
|
|---|
| 147 | * The code here still runs on the boot stack, which knows nothing about
|
|---|
| 148 | * preemption counts. Because of that, this function cannot directly call
|
|---|
| 149 | * functions that disable or enable preemption (e.g. spinlock_lock()). The
|
|---|
| 150 | * primary task of this function is to calculate address of a new stack and
|
|---|
| 151 | * switch to it.
|
|---|
| 152 | *
|
|---|
| 153 | * Assuming interrupts_disable().
|
|---|
| 154 | *
|
|---|
| 155 | */
|
|---|
| 156 | NO_TRACE void main_bsp(void)
|
|---|
| 157 | {
|
|---|
| 158 | config.cpu_count = 1;
|
|---|
| 159 | config.cpu_active = 1;
|
|---|
| 160 |
|
|---|
| 161 | config.base = hardcoded_load_address;
|
|---|
| 162 | config.kernel_size = ALIGN_UP(hardcoded_ktext_size +
|
|---|
| 163 | hardcoded_kdata_size, PAGE_SIZE);
|
|---|
| 164 | config.stack_size = STACK_SIZE;
|
|---|
| 165 |
|
|---|
| 166 | /* Initialy the stack is placed just after the kernel */
|
|---|
| 167 | config.stack_base = config.base + config.kernel_size;
|
|---|
| 168 |
|
|---|
| 169 | /* Avoid placing stack on top of init */
|
|---|
| 170 | size_t i;
|
|---|
| 171 | for (i = 0; i < init.cnt; i++) {
|
|---|
| 172 | if (overlaps(KA2PA(config.stack_base), config.stack_size,
|
|---|
| 173 | init.tasks[i].paddr, init.tasks[i].size)) {
|
|---|
| 174 | /*
|
|---|
| 175 | * The init task overlaps with the memory behind the
|
|---|
| 176 | * kernel image so it must be in low memory and we can
|
|---|
| 177 | * use PA2KA on the init task's physical address.
|
|---|
| 178 | */
|
|---|
| 179 | config.stack_base = ALIGN_UP(
|
|---|
| 180 | PA2KA(init.tasks[i].paddr) + init.tasks[i].size,
|
|---|
| 181 | config.stack_size);
|
|---|
| 182 | }
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | /* Avoid placing stack on top of boot allocations. */
|
|---|
| 186 | if (ballocs.size) {
|
|---|
| 187 | if (PA_OVERLAPS(config.stack_base, config.stack_size,
|
|---|
| 188 | ballocs.base, ballocs.size))
|
|---|
| 189 | config.stack_base = ALIGN_UP(ballocs.base +
|
|---|
| 190 | ballocs.size, PAGE_SIZE);
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | if (config.stack_base < stack_safe)
|
|---|
| 194 | config.stack_base = ALIGN_UP(stack_safe, PAGE_SIZE);
|
|---|
| 195 |
|
|---|
| 196 | context_save(&ctx);
|
|---|
| 197 | context_set(&ctx, FADDR(main_bsp_separated_stack),
|
|---|
| 198 | config.stack_base, STACK_SIZE);
|
|---|
| 199 | context_restore(&ctx);
|
|---|
| 200 | /* not reached */
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | /** Main kernel routine for bootstrap CPU using new stack.
|
|---|
| 204 | *
|
|---|
| 205 | * Second part of main_bsp().
|
|---|
| 206 | *
|
|---|
| 207 | */
|
|---|
| 208 | void main_bsp_separated_stack(void)
|
|---|
| 209 | {
|
|---|
| 210 | /* Keep this the first thing. */
|
|---|
| 211 | the_initialize(THE);
|
|---|
| 212 |
|
|---|
| 213 | version_print();
|
|---|
| 214 |
|
|---|
| 215 | LOG("\nconfig.base=%p config.kernel_size=%zu"
|
|---|
| 216 | "\nconfig.stack_base=%p config.stack_size=%zu",
|
|---|
| 217 | (void *) config.base, config.kernel_size,
|
|---|
| 218 | (void *) config.stack_base, config.stack_size);
|
|---|
| 219 |
|
|---|
| 220 | #ifdef CONFIG_KCONSOLE
|
|---|
| 221 | /*
|
|---|
| 222 | * kconsole data structures must be initialized very early
|
|---|
| 223 | * because other subsystems will register their respective
|
|---|
| 224 | * commands.
|
|---|
| 225 | */
|
|---|
| 226 | kconsole_init();
|
|---|
| 227 | #endif
|
|---|
| 228 |
|
|---|
| 229 | /*
|
|---|
| 230 | * Exception handler initialization, before architecture
|
|---|
| 231 | * starts adding its own handlers
|
|---|
| 232 | */
|
|---|
| 233 | exc_init();
|
|---|
| 234 |
|
|---|
| 235 | /*
|
|---|
| 236 | * Memory management subsystems initialization.
|
|---|
| 237 | */
|
|---|
| 238 | arch_pre_mm_init();
|
|---|
| 239 | km_identity_init();
|
|---|
| 240 | frame_init();
|
|---|
| 241 | slab_cache_init();
|
|---|
| 242 | ra_init();
|
|---|
| 243 | sysinfo_init();
|
|---|
| 244 | btree_init();
|
|---|
| 245 | as_init();
|
|---|
| 246 | page_init();
|
|---|
| 247 | tlb_init();
|
|---|
| 248 | km_non_identity_init();
|
|---|
| 249 | ddi_init();
|
|---|
| 250 | arch_post_mm_init();
|
|---|
| 251 | reserve_init();
|
|---|
| 252 | arch_pre_smp_init();
|
|---|
| 253 | smp_init();
|
|---|
| 254 |
|
|---|
| 255 | /* Slab must be initialized after we know the number of processors. */
|
|---|
| 256 | slab_enable_cpucache();
|
|---|
| 257 |
|
|---|
| 258 | uint64_t size;
|
|---|
| 259 | const char *size_suffix;
|
|---|
| 260 | bin_order_suffix(zones_total_size(), &size, &size_suffix, false);
|
|---|
| 261 | printf("Detected %u CPU(s), %" PRIu64 " %s free memory\n",
|
|---|
| 262 | config.cpu_count, size, size_suffix);
|
|---|
| 263 |
|
|---|
| 264 | cpu_init();
|
|---|
| 265 |
|
|---|
| 266 | calibrate_delay_loop();
|
|---|
| 267 | clock_counter_init();
|
|---|
| 268 | timeout_init();
|
|---|
| 269 | scheduler_init();
|
|---|
| 270 | task_init();
|
|---|
| 271 | thread_init();
|
|---|
| 272 | futex_init();
|
|---|
| 273 |
|
|---|
| 274 | if (init.cnt > 0) {
|
|---|
| 275 | size_t i;
|
|---|
| 276 | for (i = 0; i < init.cnt; i++)
|
|---|
| 277 | LOG("init[%zu].addr=%p, init[%zu].size=%zu",
|
|---|
| 278 | i, (void *) init.tasks[i].paddr, i, init.tasks[i].size);
|
|---|
| 279 | } else
|
|---|
| 280 | printf("No init binaries found.\n");
|
|---|
| 281 |
|
|---|
| 282 | ipc_init();
|
|---|
| 283 | event_init();
|
|---|
| 284 | kio_init();
|
|---|
| 285 | log_init();
|
|---|
| 286 | stats_init();
|
|---|
| 287 |
|
|---|
| 288 | /*
|
|---|
| 289 | * Create kernel task.
|
|---|
| 290 | */
|
|---|
| 291 | task_t *kernel = task_create(AS_KERNEL, "kernel");
|
|---|
| 292 | if (!kernel)
|
|---|
| 293 | panic("Cannot create kernel task.");
|
|---|
| 294 |
|
|---|
| 295 | /*
|
|---|
| 296 | * Create the first thread.
|
|---|
| 297 | */
|
|---|
| 298 | thread_t *kinit_thread = thread_create(kinit, NULL, kernel,
|
|---|
| 299 | THREAD_FLAG_UNCOUNTED, "kinit");
|
|---|
| 300 | if (!kinit_thread)
|
|---|
| 301 | panic("Cannot create kinit thread.");
|
|---|
| 302 | thread_ready(kinit_thread);
|
|---|
| 303 |
|
|---|
| 304 | /*
|
|---|
| 305 | * This call to scheduler() will return to kinit,
|
|---|
| 306 | * starting the thread of kernel threads.
|
|---|
| 307 | */
|
|---|
| 308 | scheduler();
|
|---|
| 309 | /* not reached */
|
|---|
| 310 | }
|
|---|
| 311 |
|
|---|
| 312 | #ifdef CONFIG_SMP
|
|---|
| 313 |
|
|---|
| 314 | /** Main kernel routine for application CPUs.
|
|---|
| 315 | *
|
|---|
| 316 | * Executed by application processors, temporary stack
|
|---|
| 317 | * is at ctx.sp which was set during BSP boot.
|
|---|
| 318 | * This function passes control directly to
|
|---|
| 319 | * main_ap_separated_stack().
|
|---|
| 320 | *
|
|---|
| 321 | * Assuming interrupts_disable()'d.
|
|---|
| 322 | *
|
|---|
| 323 | */
|
|---|
| 324 | void main_ap(void)
|
|---|
| 325 | {
|
|---|
| 326 | /*
|
|---|
| 327 | * Incrementing the active CPU counter will guarantee that the
|
|---|
| 328 | * *_init() functions can find out that they need to
|
|---|
| 329 | * do initialization for AP only.
|
|---|
| 330 | */
|
|---|
| 331 | config.cpu_active++;
|
|---|
| 332 |
|
|---|
| 333 | /*
|
|---|
| 334 | * The THE structure is well defined because ctx.sp is used as stack.
|
|---|
| 335 | */
|
|---|
| 336 | the_initialize(THE);
|
|---|
| 337 |
|
|---|
| 338 | arch_pre_mm_init();
|
|---|
| 339 | frame_init();
|
|---|
| 340 | page_init();
|
|---|
| 341 | tlb_init();
|
|---|
| 342 | arch_post_mm_init();
|
|---|
| 343 |
|
|---|
| 344 | cpu_init();
|
|---|
| 345 | calibrate_delay_loop();
|
|---|
| 346 | arch_post_cpu_init();
|
|---|
| 347 |
|
|---|
| 348 | the_copy(THE, (the_t *) CPU->stack);
|
|---|
| 349 |
|
|---|
| 350 | /*
|
|---|
| 351 | * If we woke kmp up before we left the kernel stack, we could
|
|---|
| 352 | * collide with another CPU coming up. To prevent this, we
|
|---|
| 353 | * switch to this cpu's private stack prior to waking kmp up.
|
|---|
| 354 | */
|
|---|
| 355 | context_save(&CPU->saved_context);
|
|---|
| 356 | context_set(&CPU->saved_context, FADDR(main_ap_separated_stack),
|
|---|
| 357 | (uintptr_t) CPU->stack, STACK_SIZE);
|
|---|
| 358 | context_restore(&CPU->saved_context);
|
|---|
| 359 | /* not reached */
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | /** Main kernel routine for application CPUs using new stack.
|
|---|
| 363 | *
|
|---|
| 364 | * Second part of main_ap().
|
|---|
| 365 | *
|
|---|
| 366 | */
|
|---|
| 367 | void main_ap_separated_stack(void)
|
|---|
| 368 | {
|
|---|
| 369 | /*
|
|---|
| 370 | * Configure timeouts for this cpu.
|
|---|
| 371 | */
|
|---|
| 372 | timeout_init();
|
|---|
| 373 |
|
|---|
| 374 | waitq_wakeup(&ap_completion_wq, WAKEUP_FIRST);
|
|---|
| 375 | scheduler();
|
|---|
| 376 | /* not reached */
|
|---|
| 377 | }
|
|---|
| 378 |
|
|---|
| 379 | #endif /* CONFIG_SMP */
|
|---|
| 380 |
|
|---|
| 381 | /** @}
|
|---|
| 382 | */
|
|---|