[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
|
---|
[13a638d] | 35 | * @brief Main initialization kernel function for all processors.
|
---|
[9179d0a] | 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 |
|
---|
[f761f1eb] | 49 | #include <arch/asm.h>
|
---|
[d5d2a3f] | 50 | #include <context.h>
|
---|
[f761f1eb] | 51 | #include <print.h>
|
---|
[02a99d2] | 52 | #include <panic.h>
|
---|
[940cac0] | 53 | #include <debug.h>
|
---|
[f761f1eb] | 54 | #include <config.h>
|
---|
| 55 | #include <time/clock.h>
|
---|
[b3f8fb7] | 56 | #include <time/timeout.h>
|
---|
[f761f1eb] | 57 | #include <proc/scheduler.h>
|
---|
| 58 | #include <proc/thread.h>
|
---|
| 59 | #include <proc/task.h>
|
---|
| 60 | #include <main/kinit.h>
|
---|
[5fe5f1e] | 61 | #include <main/version.h>
|
---|
[f4338d2] | 62 | #include <console/kconsole.h>
|
---|
[5d67baa] | 63 | #include <console/console.h>
|
---|
[91db0280] | 64 | #include <log.h>
|
---|
[f761f1eb] | 65 | #include <cpu.h>
|
---|
[e0cdb7b6] | 66 | #include <align.h>
|
---|
[aace6624] | 67 | #include <interrupt.h>
|
---|
[f761f1eb] | 68 | #include <mm/frame.h>
|
---|
| 69 | #include <mm/page.h>
|
---|
[6d7ffa65] | 70 | #include <genarch/mm/page_pt.h>
|
---|
[622f409] | 71 | #include <mm/km.h>
|
---|
[169587a] | 72 | #include <mm/tlb.h>
|
---|
[20d50a1] | 73 | #include <mm/as.h>
|
---|
[4e147a6] | 74 | #include <mm/slab.h>
|
---|
[8d308b9] | 75 | #include <mm/reserve.h>
|
---|
[f761f1eb] | 76 | #include <synch/waitq.h>
|
---|
[9aa72b4] | 77 | #include <synch/futex.h>
|
---|
[393f631] | 78 | #include <arch/arch.h>
|
---|
[f761f1eb] | 79 | #include <arch.h>
|
---|
[f2ffad4] | 80 | #include <arch/faddr.h>
|
---|
[6d9c49a] | 81 | #include <ipc/ipc.h>
|
---|
[93165be] | 82 | #include <macros.h>
|
---|
[2810636] | 83 | #include <adt/btree.h>
|
---|
[5fe5f1e] | 84 | #include <smp/smp.h>
|
---|
[f8ddd17] | 85 | #include <ddi/ddi.h>
|
---|
[4b241f3] | 86 | #include <main/main.h>
|
---|
[13a638d] | 87 | #include <ipc/event.h>
|
---|
[d9fae235] | 88 | #include <sysinfo/sysinfo.h>
|
---|
[9dae191e] | 89 | #include <sysinfo/stats.h>
|
---|
[41deb2a] | 90 | #include <lib/ra.h>
|
---|
[5fe5f1e] | 91 |
|
---|
[328cc31] | 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");
|
---|
[9e40355e] | 100 |
|
---|
[328cc31] | 101 | #define CHECK_INT_TYPE(size) \
|
---|
[9e40355e] | 102 | CHECK_INT_TYPE_(int, size); \
|
---|
| 103 | CHECK_INT_TYPE_(uint, size)
|
---|
[328cc31] | 104 |
|
---|
| 105 | CHECK_INT_TYPE(8);
|
---|
| 106 | CHECK_INT_TYPE(16);
|
---|
| 107 | CHECK_INT_TYPE(32);
|
---|
| 108 | CHECK_INT_TYPE(64);
|
---|
| 109 |
|
---|
[e459f12] | 110 | /** Global configuration structure. */
|
---|
[55896b6] | 111 | config_t config = {
|
---|
| 112 | .identity_configured = false,
|
---|
[40c8c17] | 113 | .non_identity_configured = false,
|
---|
| 114 | .physmem_end = 0
|
---|
[55896b6] | 115 | };
|
---|
[e459f12] | 116 |
|
---|
| 117 | /** Initial user-space tasks */
|
---|
| 118 | init_t init = {
|
---|
[6c441cf8] | 119 | .cnt = 0
|
---|
[e459f12] | 120 | };
|
---|
[5fe5f1e] | 121 |
|
---|
[61e90dd] | 122 | /** Boot allocations. */
|
---|
| 123 | ballocs_t ballocs = {
|
---|
[0b4a67a] | 124 | .base = (uintptr_t) NULL,
|
---|
[61e90dd] | 125 | .size = 0
|
---|
| 126 | };
|
---|
| 127 |
|
---|
[f761f1eb] | 128 | context_t ctx;
|
---|
| 129 |
|
---|
[c0855a0] | 130 | /** Lowest safe stack virtual address. */
|
---|
[263bda2] | 131 | uintptr_t stack_safe = 0;
|
---|
[deaa22f] | 132 |
|
---|
[f761f1eb] | 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);
|
---|
[263bda2] | 140 |
|
---|
[80d2bdb] | 141 | #ifdef CONFIG_SMP
|
---|
[f761f1eb] | 142 | static void main_ap_separated_stack(void);
|
---|
[80d2bdb] | 143 | #endif
|
---|
[f761f1eb] | 144 |
|
---|
[7f0837c] | 145 | /** Main kernel routine for bootstrap CPU.
|
---|
[673104e] | 146 | *
|
---|
[7a4202d] | 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.
|
---|
[673104e] | 152 | *
|
---|
[22f7769] | 153 | * Assuming interrupts_disable().
|
---|
[673104e] | 154 | *
|
---|
[f761f1eb] | 155 | */
|
---|
[7a0359b] | 156 | NO_TRACE void main_bsp(void)
|
---|
[f761f1eb] | 157 | {
|
---|
| 158 | config.cpu_count = 1;
|
---|
| 159 | config.cpu_active = 1;
|
---|
[d6e8529] | 160 |
|
---|
[aed4eca] | 161 | config.base = hardcoded_load_address;
|
---|
[f8ddd17] | 162 | config.kernel_size = ALIGN_UP(hardcoded_ktext_size +
|
---|
[6f4495f5] | 163 | hardcoded_kdata_size, PAGE_SIZE);
|
---|
[26aafe8] | 164 | config.stack_size = STACK_SIZE;
|
---|
[deaa22f] | 165 |
|
---|
| 166 | /* Initialy the stack is placed just after the kernel */
|
---|
| 167 | config.stack_base = config.base + config.kernel_size;
|
---|
[b6b576c] | 168 |
|
---|
[deaa22f] | 169 | /* Avoid placing stack on top of init */
|
---|
[98000fb] | 170 | size_t i;
|
---|
[deaa22f] | 171 | for (i = 0; i < init.cnt; i++) {
|
---|
[32817cc] | 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 | }
|
---|
[deaa22f] | 183 | }
|
---|
[263bda2] | 184 |
|
---|
[61e90dd] | 185 | /* Avoid placing stack on top of boot allocations. */
|
---|
| 186 | if (ballocs.size) {
|
---|
[e2650d3] | 187 | if (PA_OVERLAPS(config.stack_base, config.stack_size,
|
---|
[6f4495f5] | 188 | ballocs.base, ballocs.size))
|
---|
[f8ddd17] | 189 | config.stack_base = ALIGN_UP(ballocs.base +
|
---|
[6f4495f5] | 190 | ballocs.size, PAGE_SIZE);
|
---|
[61e90dd] | 191 | }
|
---|
[b6b576c] | 192 |
|
---|
[deaa22f] | 193 | if (config.stack_base < stack_safe)
|
---|
| 194 | config.stack_base = ALIGN_UP(stack_safe, PAGE_SIZE);
|
---|
[393f631] | 195 |
|
---|
[be50915] | 196 | context_save(&ctx);
|
---|
[26aafe8] | 197 | context_set(&ctx, FADDR(main_bsp_separated_stack),
|
---|
| 198 | config.stack_base, STACK_SIZE);
|
---|
[be50915] | 199 | context_restore(&ctx);
|
---|
[f761f1eb] | 200 | /* not reached */
|
---|
| 201 | }
|
---|
| 202 |
|
---|
[7f0837c] | 203 | /** Main kernel routine for bootstrap CPU using new stack.
|
---|
[673104e] | 204 | *
|
---|
| 205 | * Second part of main_bsp().
|
---|
| 206 | *
|
---|
| 207 | */
|
---|
[263bda2] | 208 | void main_bsp_separated_stack(void)
|
---|
[fde6429] | 209 | {
|
---|
[7a4202d] | 210 | /* Keep this the first thing. */
|
---|
| 211 | the_initialize(THE);
|
---|
[8f91729] | 212 |
|
---|
[7a4202d] | 213 | version_print();
|
---|
| 214 |
|
---|
[7e752b2] | 215 | LOG("\nconfig.base=%p config.kernel_size=%zu"
|
---|
| 216 | "\nconfig.stack_base=%p config.stack_size=%zu",
|
---|
[1caeb2d] | 217 | (void *) config.base, config.kernel_size,
|
---|
| 218 | (void *) config.stack_base, config.stack_size);
|
---|
[3ad953c] | 219 |
|
---|
[76fca31] | 220 | #ifdef CONFIG_KCONSOLE
|
---|
[ff3b3197] | 221 | /*
|
---|
| 222 | * kconsole data structures must be initialized very early
|
---|
| 223 | * because other subsystems will register their respective
|
---|
| 224 | * commands.
|
---|
| 225 | */
|
---|
[263bda2] | 226 | kconsole_init();
|
---|
[76fca31] | 227 | #endif
|
---|
[b6b576c] | 228 |
|
---|
[ef67bab] | 229 | /*
|
---|
| 230 | * Exception handler initialization, before architecture
|
---|
[0132630] | 231 | * starts adding its own handlers
|
---|
[aace6624] | 232 | */
|
---|
[263bda2] | 233 | exc_init();
|
---|
[3ad953c] | 234 |
|
---|
[fc1e4f6] | 235 | /*
|
---|
| 236 | * Memory management subsystems initialization.
|
---|
[b84aaba] | 237 | */
|
---|
[263bda2] | 238 | arch_pre_mm_init();
|
---|
[622f409] | 239 | km_identity_init();
|
---|
[263bda2] | 240 | frame_init();
|
---|
| 241 | slab_cache_init();
|
---|
[dabbe28] | 242 | ra_init();
|
---|
[263bda2] | 243 | sysinfo_init();
|
---|
| 244 | btree_init();
|
---|
| 245 | as_init();
|
---|
| 246 | page_init();
|
---|
| 247 | tlb_init();
|
---|
[622f409] | 248 | km_non_identity_init();
|
---|
[263bda2] | 249 | ddi_init();
|
---|
| 250 | arch_post_mm_init();
|
---|
[8d308b9] | 251 | reserve_init();
|
---|
[263bda2] | 252 | arch_pre_smp_init();
|
---|
| 253 | smp_init();
|
---|
[71eef11] | 254 |
|
---|
[f8ddd17] | 255 | /* Slab must be initialized after we know the number of processors. */
|
---|
[263bda2] | 256 | slab_enable_cpucache();
|
---|
[71eef11] | 257 |
|
---|
[933cadf] | 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);
|
---|
[b84aaba] | 263 |
|
---|
[263bda2] | 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();
|
---|
[6c68b97] | 273 |
|
---|
[c06eb06] | 274 | if (init.cnt > 0) {
|
---|
[98000fb] | 275 | size_t i;
|
---|
[c06eb06] | 276 | for (i = 0; i < init.cnt; i++)
|
---|
[7e752b2] | 277 | LOG("init[%zu].addr=%p, init[%zu].size=%zu",
|
---|
[edd7c63c] | 278 | i, (void *) init.tasks[i].paddr, i, init.tasks[i].size);
|
---|
[c06eb06] | 279 | } else
|
---|
[ae5aa90] | 280 | printf("No init binaries found.\n");
|
---|
[6d9c49a] | 281 |
|
---|
[263bda2] | 282 | ipc_init();
|
---|
| 283 | event_init();
|
---|
[6fa9a99d] | 284 | kio_init();
|
---|
[91db0280] | 285 | log_init();
|
---|
[263bda2] | 286 | stats_init();
|
---|
[3ad953c] | 287 |
|
---|
[f761f1eb] | 288 | /*
|
---|
| 289 | * Create kernel task.
|
---|
| 290 | */
|
---|
[b84aaba] | 291 | task_t *kernel = task_create(AS_KERNEL, "kernel");
|
---|
| 292 | if (!kernel)
|
---|
[f651e80] | 293 | panic("Cannot create kernel task.");
|
---|
[6f8a426] | 294 |
|
---|
[f761f1eb] | 295 | /*
|
---|
| 296 | * Create the first thread.
|
---|
| 297 | */
|
---|
[6eef3c4] | 298 | thread_t *kinit_thread = thread_create(kinit, NULL, kernel,
|
---|
| 299 | THREAD_FLAG_UNCOUNTED, "kinit");
|
---|
[b84aaba] | 300 | if (!kinit_thread)
|
---|
[f651e80] | 301 | panic("Cannot create kinit thread.");
|
---|
[263bda2] | 302 | thread_ready(kinit_thread);
|
---|
[6f8a426] | 303 |
|
---|
[f761f1eb] | 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 |
|
---|
[5f85c91] | 312 | #ifdef CONFIG_SMP
|
---|
[263bda2] | 313 |
|
---|
[7f0837c] | 314 | /** Main kernel routine for application CPUs.
|
---|
[673104e] | 315 | *
|
---|
| 316 | * Executed by application processors, temporary stack
|
---|
[26678e5] | 317 | * is at ctx.sp which was set during BSP boot.
|
---|
[5fe5f1e] | 318 | * This function passes control directly to
|
---|
| 319 | * main_ap_separated_stack().
|
---|
[673104e] | 320 | *
|
---|
[22f7769] | 321 | * Assuming interrupts_disable()'d.
|
---|
[673104e] | 322 | *
|
---|
[f761f1eb] | 323 | */
|
---|
| 324 | void main_ap(void)
|
---|
| 325 | {
|
---|
| 326 | /*
|
---|
| 327 | * Incrementing the active CPU counter will guarantee that the
|
---|
[26678e5] | 328 | * *_init() functions can find out that they need to
|
---|
| 329 | * do initialization for AP only.
|
---|
[f761f1eb] | 330 | */
|
---|
| 331 | config.cpu_active++;
|
---|
[263bda2] | 332 |
|
---|
[7ce9284] | 333 | /*
|
---|
| 334 | * The THE structure is well defined because ctx.sp is used as stack.
|
---|
| 335 | */
|
---|
| 336 | the_initialize(THE);
|
---|
[c0b45fa] | 337 |
|
---|
[f07bba5] | 338 | arch_pre_mm_init();
|
---|
[f761f1eb] | 339 | frame_init();
|
---|
| 340 | page_init();
|
---|
[ce031f0] | 341 | tlb_init();
|
---|
[f07bba5] | 342 | arch_post_mm_init();
|
---|
[c0b45fa] | 343 |
|
---|
[f761f1eb] | 344 | cpu_init();
|
---|
| 345 | calibrate_delay_loop();
|
---|
[26678e5] | 346 | arch_post_cpu_init();
|
---|
[263bda2] | 347 |
|
---|
[7ce9284] | 348 | the_copy(THE, (the_t *) CPU->stack);
|
---|
[263bda2] | 349 |
|
---|
[f761f1eb] | 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 | */
|
---|
[b24786a] | 355 | context_save(&CPU->saved_context);
|
---|
[f8ddd17] | 356 | context_set(&CPU->saved_context, FADDR(main_ap_separated_stack),
|
---|
[26aafe8] | 357 | (uintptr_t) CPU->stack, STACK_SIZE);
|
---|
[be50915] | 358 | context_restore(&CPU->saved_context);
|
---|
[f761f1eb] | 359 | /* not reached */
|
---|
| 360 | }
|
---|
| 361 |
|
---|
[7f0837c] | 362 | /** Main kernel routine for application CPUs using new stack.
|
---|
[673104e] | 363 | *
|
---|
| 364 | * Second part of main_ap().
|
---|
| 365 | *
|
---|
| 366 | */
|
---|
[f761f1eb] | 367 | void main_ap_separated_stack(void)
|
---|
| 368 | {
|
---|
| 369 | /*
|
---|
| 370 | * Configure timeouts for this cpu.
|
---|
| 371 | */
|
---|
| 372 | timeout_init();
|
---|
[263bda2] | 373 |
|
---|
[f761f1eb] | 374 | waitq_wakeup(&ap_completion_wq, WAKEUP_FIRST);
|
---|
| 375 | scheduler();
|
---|
| 376 | /* not reached */
|
---|
| 377 | }
|
---|
[263bda2] | 378 |
|
---|
[5f85c91] | 379 | #endif /* CONFIG_SMP */
|
---|
[b45c443] | 380 |
|
---|
[8e3bf3e2] | 381 | /** @}
|
---|
[b45c443] | 382 | */
|
---|