[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 |
|
---|
[e88eb48] | 29 | /** @addtogroup kernel_generic
|
---|
[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>
|
---|
[63e27ef] | 50 | #include <debug.h>
|
---|
[d5d2a3f] | 51 | #include <context.h>
|
---|
[bab75df6] | 52 | #include <stdio.h>
|
---|
[02a99d2] | 53 | #include <panic.h>
|
---|
[63e27ef] | 54 | #include <assert.h>
|
---|
[f761f1eb] | 55 | #include <config.h>
|
---|
| 56 | #include <time/clock.h>
|
---|
[b3f8fb7] | 57 | #include <time/timeout.h>
|
---|
[f761f1eb] | 58 | #include <proc/scheduler.h>
|
---|
| 59 | #include <proc/thread.h>
|
---|
| 60 | #include <proc/task.h>
|
---|
| 61 | #include <main/kinit.h>
|
---|
[5fe5f1e] | 62 | #include <main/version.h>
|
---|
[f4338d2] | 63 | #include <console/kconsole.h>
|
---|
[5d67baa] | 64 | #include <console/console.h>
|
---|
[91db0280] | 65 | #include <log.h>
|
---|
[f761f1eb] | 66 | #include <cpu.h>
|
---|
[e0cdb7b6] | 67 | #include <align.h>
|
---|
[aace6624] | 68 | #include <interrupt.h>
|
---|
[b5746a2] | 69 | #include <str.h>
|
---|
[f761f1eb] | 70 | #include <mm/frame.h>
|
---|
| 71 | #include <mm/page.h>
|
---|
[6d7ffa65] | 72 | #include <genarch/mm/page_pt.h>
|
---|
[622f409] | 73 | #include <mm/km.h>
|
---|
[169587a] | 74 | #include <mm/tlb.h>
|
---|
[20d50a1] | 75 | #include <mm/as.h>
|
---|
[4e147a6] | 76 | #include <mm/slab.h>
|
---|
[8d308b9] | 77 | #include <mm/reserve.h>
|
---|
[f761f1eb] | 78 | #include <synch/waitq.h>
|
---|
[d314571] | 79 | #include <synch/syswaitq.h>
|
---|
[393f631] | 80 | #include <arch/arch.h>
|
---|
[f761f1eb] | 81 | #include <arch.h>
|
---|
[f2ffad4] | 82 | #include <arch/faddr.h>
|
---|
[6d9c49a] | 83 | #include <ipc/ipc.h>
|
---|
[93165be] | 84 | #include <macros.h>
|
---|
[5fe5f1e] | 85 | #include <smp/smp.h>
|
---|
[f8ddd17] | 86 | #include <ddi/ddi.h>
|
---|
[4b241f3] | 87 | #include <main/main.h>
|
---|
[13a638d] | 88 | #include <ipc/event.h>
|
---|
[d9fae235] | 89 | #include <sysinfo/sysinfo.h>
|
---|
[9dae191e] | 90 | #include <sysinfo/stats.h>
|
---|
[41deb2a] | 91 | #include <lib/ra.h>
|
---|
[ce732e74] | 92 | #include <cap/cap.h>
|
---|
[5fe5f1e] | 93 |
|
---|
[7c3fb9b] | 94 | /*
|
---|
| 95 | * Ensure [u]int*_t types are of correct size.
|
---|
[328cc31] | 96 | *
|
---|
| 97 | * Probably, this is not the best place for such tests
|
---|
| 98 | * but this file is compiled on all architectures.
|
---|
| 99 | */
|
---|
| 100 | #define CHECK_INT_TYPE_(signness, size) \
|
---|
[63e27ef] | 101 | static_assert(sizeof(signness##size##_t) * 8 == size, \
|
---|
[328cc31] | 102 | #signness #size "_t does not have " #size " bits");
|
---|
[9e40355e] | 103 |
|
---|
[328cc31] | 104 | #define CHECK_INT_TYPE(size) \
|
---|
[9e40355e] | 105 | CHECK_INT_TYPE_(int, size); \
|
---|
| 106 | CHECK_INT_TYPE_(uint, size)
|
---|
[328cc31] | 107 |
|
---|
| 108 | CHECK_INT_TYPE(8);
|
---|
| 109 | CHECK_INT_TYPE(16);
|
---|
| 110 | CHECK_INT_TYPE(32);
|
---|
| 111 | CHECK_INT_TYPE(64);
|
---|
| 112 |
|
---|
[e459f12] | 113 | /** Global configuration structure. */
|
---|
[55896b6] | 114 | config_t config = {
|
---|
| 115 | .identity_configured = false,
|
---|
[40c8c17] | 116 | .non_identity_configured = false,
|
---|
| 117 | .physmem_end = 0
|
---|
[55896b6] | 118 | };
|
---|
[e459f12] | 119 |
|
---|
[3b3faf51] | 120 | /** Boot arguments. */
|
---|
[3bacee1] | 121 | char bargs[CONFIG_BOOT_ARGUMENTS_BUFLEN] = { };
|
---|
[3b3faf51] | 122 |
|
---|
[e459f12] | 123 | /** Initial user-space tasks */
|
---|
| 124 | init_t init = {
|
---|
[6c441cf8] | 125 | .cnt = 0
|
---|
[e459f12] | 126 | };
|
---|
[5fe5f1e] | 127 |
|
---|
[61e90dd] | 128 | /** Boot allocations. */
|
---|
| 129 | ballocs_t ballocs = {
|
---|
[0b4a67a] | 130 | .base = (uintptr_t) NULL,
|
---|
[61e90dd] | 131 | .size = 0
|
---|
| 132 | };
|
---|
| 133 |
|
---|
[deed510] | 134 | static context_t ctx;
|
---|
[deaa22f] | 135 |
|
---|
[65f3117] | 136 | // NOTE: All kernel stacks must be aligned to STACK_SIZE, see CURRENT.
|
---|
[deed510] | 137 | static const size_t bootstrap_stack_size = STACK_SIZE;
|
---|
| 138 | static _Alignas(STACK_SIZE) uint8_t bootstrap_stack[STACK_SIZE];
|
---|
| 139 |
|
---|
[65f3117] | 140 | /* Just a convenient value for some assembly code. */
|
---|
[deed510] | 141 | uint8_t *const bootstrap_stack_top = bootstrap_stack + STACK_SIZE;
|
---|
[65f3117] | 142 |
|
---|
[f761f1eb] | 143 | /*
|
---|
| 144 | * These two functions prevent stack from underflowing during the
|
---|
| 145 | * kernel boot phase when SP is set to the very top of the reserved
|
---|
| 146 | * space. The stack could get corrupted by a fooled compiler-generated
|
---|
| 147 | * pop sequence otherwise.
|
---|
| 148 | */
|
---|
| 149 | static void main_bsp_separated_stack(void);
|
---|
[263bda2] | 150 |
|
---|
[80d2bdb] | 151 | #ifdef CONFIG_SMP
|
---|
[f761f1eb] | 152 | static void main_ap_separated_stack(void);
|
---|
[80d2bdb] | 153 | #endif
|
---|
[f761f1eb] | 154 |
|
---|
[7f0837c] | 155 | /** Main kernel routine for bootstrap CPU.
|
---|
[673104e] | 156 | *
|
---|
[7a4202d] | 157 | * The code here still runs on the boot stack, which knows nothing about
|
---|
| 158 | * preemption counts. Because of that, this function cannot directly call
|
---|
| 159 | * functions that disable or enable preemption (e.g. spinlock_lock()). The
|
---|
| 160 | * primary task of this function is to calculate address of a new stack and
|
---|
| 161 | * switch to it.
|
---|
[673104e] | 162 | *
|
---|
[22f7769] | 163 | * Assuming interrupts_disable().
|
---|
[673104e] | 164 | *
|
---|
[f761f1eb] | 165 | */
|
---|
[8df5f20] | 166 | _NO_TRACE void main_bsp(void)
|
---|
[f761f1eb] | 167 | {
|
---|
| 168 | config.cpu_count = 1;
|
---|
| 169 | config.cpu_active = 1;
|
---|
[a35b458] | 170 |
|
---|
[8a1afd2] | 171 | config.base = (uintptr_t) kernel_load_address;
|
---|
| 172 |
|
---|
| 173 | config.kernel_size =
|
---|
| 174 | ALIGN_UP((uintptr_t) kdata_end - config.base, PAGE_SIZE);
|
---|
[a35b458] | 175 |
|
---|
[be50915] | 176 | context_save(&ctx);
|
---|
[26aafe8] | 177 | context_set(&ctx, FADDR(main_bsp_separated_stack),
|
---|
[65f3117] | 178 | bootstrap_stack, bootstrap_stack_size);
|
---|
[be50915] | 179 | context_restore(&ctx);
|
---|
[f761f1eb] | 180 | /* not reached */
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[7f0837c] | 183 | /** Main kernel routine for bootstrap CPU using new stack.
|
---|
[673104e] | 184 | *
|
---|
| 185 | * Second part of main_bsp().
|
---|
| 186 | *
|
---|
| 187 | */
|
---|
[263bda2] | 188 | void main_bsp_separated_stack(void)
|
---|
[fde6429] | 189 | {
|
---|
[7a4202d] | 190 | /* Keep this the first thing. */
|
---|
[a6e55886] | 191 | current_initialize(CURRENT);
|
---|
[a35b458] | 192 |
|
---|
[7a4202d] | 193 | version_print();
|
---|
[a35b458] | 194 |
|
---|
[d28bdbe] | 195 | LOG("\nconfig.base=%p config.kernel_size=%zu",
|
---|
| 196 | (void *) config.base, config.kernel_size);
|
---|
[a35b458] | 197 |
|
---|
[76fca31] | 198 | #ifdef CONFIG_KCONSOLE
|
---|
[ff3b3197] | 199 | /*
|
---|
| 200 | * kconsole data structures must be initialized very early
|
---|
| 201 | * because other subsystems will register their respective
|
---|
| 202 | * commands.
|
---|
| 203 | */
|
---|
[263bda2] | 204 | kconsole_init();
|
---|
[76fca31] | 205 | #endif
|
---|
[a35b458] | 206 |
|
---|
[ef67bab] | 207 | /*
|
---|
| 208 | * Exception handler initialization, before architecture
|
---|
[0132630] | 209 | * starts adding its own handlers
|
---|
[aace6624] | 210 | */
|
---|
[263bda2] | 211 | exc_init();
|
---|
[a35b458] | 212 |
|
---|
[fc1e4f6] | 213 | /*
|
---|
| 214 | * Memory management subsystems initialization.
|
---|
[b84aaba] | 215 | */
|
---|
[36df4109] | 216 | ARCH_OP(pre_mm_init);
|
---|
[622f409] | 217 | km_identity_init();
|
---|
[263bda2] | 218 | frame_init();
|
---|
| 219 | slab_cache_init();
|
---|
[b60615bd] | 220 | malloc_init();
|
---|
[dabbe28] | 221 | ra_init();
|
---|
[263bda2] | 222 | sysinfo_init();
|
---|
| 223 | as_init();
|
---|
| 224 | page_init();
|
---|
| 225 | tlb_init();
|
---|
[622f409] | 226 | km_non_identity_init();
|
---|
[263bda2] | 227 | ddi_init();
|
---|
[36df4109] | 228 | ARCH_OP(post_mm_init);
|
---|
[8d308b9] | 229 | reserve_init();
|
---|
[36df4109] | 230 | ARCH_OP(pre_smp_init);
|
---|
[263bda2] | 231 | smp_init();
|
---|
[a35b458] | 232 |
|
---|
[f8ddd17] | 233 | /* Slab must be initialized after we know the number of processors. */
|
---|
[263bda2] | 234 | slab_enable_cpucache();
|
---|
[a35b458] | 235 |
|
---|
[933cadf] | 236 | uint64_t size;
|
---|
| 237 | const char *size_suffix;
|
---|
| 238 | bin_order_suffix(zones_total_size(), &size, &size_suffix, false);
|
---|
| 239 | printf("Detected %u CPU(s), %" PRIu64 " %s free memory\n",
|
---|
| 240 | config.cpu_count, size, size_suffix);
|
---|
[a35b458] | 241 |
|
---|
[263bda2] | 242 | cpu_init();
|
---|
| 243 | calibrate_delay_loop();
|
---|
[36df4109] | 244 | ARCH_OP(post_cpu_init);
|
---|
[49e6c6b4] | 245 |
|
---|
[263bda2] | 246 | clock_counter_init();
|
---|
| 247 | timeout_init();
|
---|
| 248 | scheduler_init();
|
---|
[ce732e74] | 249 | caps_init();
|
---|
[263bda2] | 250 | task_init();
|
---|
| 251 | thread_init();
|
---|
[d314571] | 252 | sys_waitq_init();
|
---|
[3b3faf51] | 253 |
|
---|
| 254 | sysinfo_set_item_data("boot_args", NULL, bargs, str_size(bargs) + 1);
|
---|
| 255 |
|
---|
[c06eb06] | 256 | if (init.cnt > 0) {
|
---|
[98000fb] | 257 | size_t i;
|
---|
[c06eb06] | 258 | for (i = 0; i < init.cnt; i++)
|
---|
[7e752b2] | 259 | LOG("init[%zu].addr=%p, init[%zu].size=%zu",
|
---|
[edd7c63c] | 260 | i, (void *) init.tasks[i].paddr, i, init.tasks[i].size);
|
---|
[c06eb06] | 261 | } else
|
---|
[ae5aa90] | 262 | printf("No init binaries found.\n");
|
---|
[a35b458] | 263 |
|
---|
[263bda2] | 264 | ipc_init();
|
---|
| 265 | event_init();
|
---|
[6fa9a99d] | 266 | kio_init();
|
---|
[91db0280] | 267 | log_init();
|
---|
[263bda2] | 268 | stats_init();
|
---|
[a35b458] | 269 |
|
---|
[f761f1eb] | 270 | /*
|
---|
| 271 | * Create kernel task.
|
---|
| 272 | */
|
---|
[b84aaba] | 273 | task_t *kernel = task_create(AS_KERNEL, "kernel");
|
---|
| 274 | if (!kernel)
|
---|
[f651e80] | 275 | panic("Cannot create kernel task.");
|
---|
[a35b458] | 276 |
|
---|
[f761f1eb] | 277 | /*
|
---|
| 278 | * Create the first thread.
|
---|
| 279 | */
|
---|
[6eef3c4] | 280 | thread_t *kinit_thread = thread_create(kinit, NULL, kernel,
|
---|
| 281 | THREAD_FLAG_UNCOUNTED, "kinit");
|
---|
[b84aaba] | 282 | if (!kinit_thread)
|
---|
[f651e80] | 283 | panic("Cannot create kinit thread.");
|
---|
[263bda2] | 284 | thread_ready(kinit_thread);
|
---|
[a35b458] | 285 |
|
---|
[f761f1eb] | 286 | /*
|
---|
[151c050] | 287 | * This call to scheduler_run() will return to kinit,
|
---|
[f761f1eb] | 288 | * starting the thread of kernel threads.
|
---|
| 289 | */
|
---|
[151c050] | 290 | scheduler_run();
|
---|
[f761f1eb] | 291 | /* not reached */
|
---|
| 292 | }
|
---|
| 293 |
|
---|
[5f85c91] | 294 | #ifdef CONFIG_SMP
|
---|
[263bda2] | 295 |
|
---|
[7f0837c] | 296 | /** Main kernel routine for application CPUs.
|
---|
[673104e] | 297 | *
|
---|
| 298 | * Executed by application processors, temporary stack
|
---|
[26678e5] | 299 | * is at ctx.sp which was set during BSP boot.
|
---|
[5fe5f1e] | 300 | * This function passes control directly to
|
---|
| 301 | * main_ap_separated_stack().
|
---|
[673104e] | 302 | *
|
---|
[22f7769] | 303 | * Assuming interrupts_disable()'d.
|
---|
[673104e] | 304 | *
|
---|
[f761f1eb] | 305 | */
|
---|
| 306 | void main_ap(void)
|
---|
| 307 | {
|
---|
| 308 | /*
|
---|
| 309 | * Incrementing the active CPU counter will guarantee that the
|
---|
[26678e5] | 310 | * *_init() functions can find out that they need to
|
---|
| 311 | * do initialization for AP only.
|
---|
[f761f1eb] | 312 | */
|
---|
| 313 | config.cpu_active++;
|
---|
[a35b458] | 314 |
|
---|
[7ce9284] | 315 | /*
|
---|
[a6e55886] | 316 | * The CURRENT structure is well defined because ctx.sp is used as stack.
|
---|
[7ce9284] | 317 | */
|
---|
[a6e55886] | 318 | current_initialize(CURRENT);
|
---|
[a35b458] | 319 |
|
---|
[36df4109] | 320 | ARCH_OP(pre_mm_init);
|
---|
[f761f1eb] | 321 | frame_init();
|
---|
| 322 | page_init();
|
---|
[ce031f0] | 323 | tlb_init();
|
---|
[36df4109] | 324 | ARCH_OP(post_mm_init);
|
---|
[a35b458] | 325 |
|
---|
[f761f1eb] | 326 | cpu_init();
|
---|
| 327 | calibrate_delay_loop();
|
---|
[36df4109] | 328 | ARCH_OP(post_cpu_init);
|
---|
[a35b458] | 329 |
|
---|
[4760793] | 330 | current_copy(CURRENT, (current_t *) CPU_LOCAL->stack);
|
---|
[a35b458] | 331 |
|
---|
[f761f1eb] | 332 | /*
|
---|
| 333 | * If we woke kmp up before we left the kernel stack, we could
|
---|
| 334 | * collide with another CPU coming up. To prevent this, we
|
---|
| 335 | * switch to this cpu's private stack prior to waking kmp up.
|
---|
| 336 | */
|
---|
[daadfa6] | 337 | context_t ctx;
|
---|
| 338 | context_save(&ctx);
|
---|
| 339 | context_set(&ctx, FADDR(main_ap_separated_stack),
|
---|
[4760793] | 340 | (uintptr_t) CPU_LOCAL->stack, STACK_SIZE);
|
---|
[daadfa6] | 341 | context_restore(&ctx);
|
---|
[f761f1eb] | 342 | /* not reached */
|
---|
| 343 | }
|
---|
| 344 |
|
---|
[7f0837c] | 345 | /** Main kernel routine for application CPUs using new stack.
|
---|
[673104e] | 346 | *
|
---|
| 347 | * Second part of main_ap().
|
---|
| 348 | *
|
---|
| 349 | */
|
---|
[f761f1eb] | 350 | void main_ap_separated_stack(void)
|
---|
| 351 | {
|
---|
| 352 | /*
|
---|
| 353 | * Configure timeouts for this cpu.
|
---|
| 354 | */
|
---|
| 355 | timeout_init();
|
---|
[a35b458] | 356 |
|
---|
[7c5320c] | 357 | semaphore_up(&ap_completion_semaphore);
|
---|
[151c050] | 358 | scheduler_run();
|
---|
[f761f1eb] | 359 | /* not reached */
|
---|
| 360 | }
|
---|
[263bda2] | 361 |
|
---|
[5f85c91] | 362 | #endif /* CONFIG_SMP */
|
---|
[b45c443] | 363 |
|
---|
[8e3bf3e2] | 364 | /** @}
|
---|
[b45c443] | 365 | */
|
---|