[f761f1eb] | 1 | /*
|
---|
[f35749e] | 2 | * Copyright (c) 2025 Jiri Svoboda
|
---|
[df4ed85] | 3 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
[f761f1eb] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[e88eb48] | 30 | /** @addtogroup kernel_generic
|
---|
[b45c443] | 31 | * @{
|
---|
| 32 | */
|
---|
| 33 |
|
---|
[9179d0a] | 34 | /**
|
---|
[b45c443] | 35 | * @file
|
---|
[13a638d] | 36 | * @brief Main initialization kernel function for all processors.
|
---|
[9179d0a] | 37 | *
|
---|
| 38 | * During kernel boot, all processors, after architecture dependent
|
---|
| 39 | * initialization, start executing code found in this file. After
|
---|
| 40 | * bringing up all subsystems, control is passed to scheduler().
|
---|
| 41 | *
|
---|
| 42 | * The bootstrap processor starts executing main_bsp() while
|
---|
| 43 | * the application processors start executing main_ap().
|
---|
| 44 | *
|
---|
| 45 | * @see scheduler()
|
---|
| 46 | * @see main_bsp()
|
---|
| 47 | * @see main_ap()
|
---|
| 48 | */
|
---|
| 49 |
|
---|
[f761f1eb] | 50 | #include <arch/asm.h>
|
---|
[63e27ef] | 51 | #include <debug.h>
|
---|
[d5d2a3f] | 52 | #include <context.h>
|
---|
[bab75df6] | 53 | #include <stdio.h>
|
---|
[02a99d2] | 54 | #include <panic.h>
|
---|
[63e27ef] | 55 | #include <assert.h>
|
---|
[f761f1eb] | 56 | #include <config.h>
|
---|
| 57 | #include <time/clock.h>
|
---|
[b3f8fb7] | 58 | #include <time/timeout.h>
|
---|
[f761f1eb] | 59 | #include <proc/scheduler.h>
|
---|
| 60 | #include <proc/thread.h>
|
---|
| 61 | #include <proc/task.h>
|
---|
| 62 | #include <main/kinit.h>
|
---|
[5fe5f1e] | 63 | #include <main/version.h>
|
---|
[f4338d2] | 64 | #include <console/kconsole.h>
|
---|
[5d67baa] | 65 | #include <console/console.h>
|
---|
[91db0280] | 66 | #include <log.h>
|
---|
[f761f1eb] | 67 | #include <cpu.h>
|
---|
[e0cdb7b6] | 68 | #include <align.h>
|
---|
[aace6624] | 69 | #include <interrupt.h>
|
---|
[b5746a2] | 70 | #include <str.h>
|
---|
[f761f1eb] | 71 | #include <mm/frame.h>
|
---|
| 72 | #include <mm/page.h>
|
---|
[6d7ffa65] | 73 | #include <genarch/mm/page_pt.h>
|
---|
[622f409] | 74 | #include <mm/km.h>
|
---|
[169587a] | 75 | #include <mm/tlb.h>
|
---|
[20d50a1] | 76 | #include <mm/as.h>
|
---|
[4e147a6] | 77 | #include <mm/slab.h>
|
---|
[8d308b9] | 78 | #include <mm/reserve.h>
|
---|
[f761f1eb] | 79 | #include <synch/waitq.h>
|
---|
[d314571] | 80 | #include <synch/syswaitq.h>
|
---|
[393f631] | 81 | #include <arch/arch.h>
|
---|
[f761f1eb] | 82 | #include <arch.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 |
|
---|
[f35749e] | 113 | task_t *kernel_task;
|
---|
| 114 |
|
---|
[e459f12] | 115 | /** Global configuration structure. */
|
---|
[55896b6] | 116 | config_t config = {
|
---|
| 117 | .identity_configured = false,
|
---|
[40c8c17] | 118 | .non_identity_configured = false,
|
---|
| 119 | .physmem_end = 0
|
---|
[55896b6] | 120 | };
|
---|
[e459f12] | 121 |
|
---|
[3b3faf51] | 122 | /** Boot arguments. */
|
---|
[3bacee1] | 123 | char bargs[CONFIG_BOOT_ARGUMENTS_BUFLEN] = { };
|
---|
[3b3faf51] | 124 |
|
---|
[e459f12] | 125 | /** Initial user-space tasks */
|
---|
| 126 | init_t init = {
|
---|
[6c441cf8] | 127 | .cnt = 0
|
---|
[e459f12] | 128 | };
|
---|
[5fe5f1e] | 129 |
|
---|
[61e90dd] | 130 | /** Boot allocations. */
|
---|
| 131 | ballocs_t ballocs = {
|
---|
[0b4a67a] | 132 | .base = (uintptr_t) NULL,
|
---|
[61e90dd] | 133 | .size = 0
|
---|
| 134 | };
|
---|
| 135 |
|
---|
[deed510] | 136 | static context_t ctx;
|
---|
[deaa22f] | 137 |
|
---|
[65f3117] | 138 | // NOTE: All kernel stacks must be aligned to STACK_SIZE, see CURRENT.
|
---|
[deed510] | 139 | static const size_t bootstrap_stack_size = STACK_SIZE;
|
---|
| 140 | static _Alignas(STACK_SIZE) uint8_t bootstrap_stack[STACK_SIZE];
|
---|
| 141 |
|
---|
[65f3117] | 142 | /* Just a convenient value for some assembly code. */
|
---|
[deed510] | 143 | uint8_t *const bootstrap_stack_top = bootstrap_stack + STACK_SIZE;
|
---|
[65f3117] | 144 |
|
---|
[f761f1eb] | 145 | /*
|
---|
| 146 | * These two functions prevent stack from underflowing during the
|
---|
| 147 | * kernel boot phase when SP is set to the very top of the reserved
|
---|
| 148 | * space. The stack could get corrupted by a fooled compiler-generated
|
---|
| 149 | * pop sequence otherwise.
|
---|
| 150 | */
|
---|
| 151 | static void main_bsp_separated_stack(void);
|
---|
[263bda2] | 152 |
|
---|
[80d2bdb] | 153 | #ifdef CONFIG_SMP
|
---|
[f761f1eb] | 154 | static void main_ap_separated_stack(void);
|
---|
[80d2bdb] | 155 | #endif
|
---|
[f761f1eb] | 156 |
|
---|
[7f0837c] | 157 | /** Main kernel routine for bootstrap CPU.
|
---|
[673104e] | 158 | *
|
---|
[7a4202d] | 159 | * The code here still runs on the boot stack, which knows nothing about
|
---|
| 160 | * preemption counts. Because of that, this function cannot directly call
|
---|
| 161 | * functions that disable or enable preemption (e.g. spinlock_lock()). The
|
---|
| 162 | * primary task of this function is to calculate address of a new stack and
|
---|
| 163 | * switch to it.
|
---|
[673104e] | 164 | *
|
---|
[22f7769] | 165 | * Assuming interrupts_disable().
|
---|
[673104e] | 166 | *
|
---|
[f761f1eb] | 167 | */
|
---|
[8df5f20] | 168 | _NO_TRACE void main_bsp(void)
|
---|
[f761f1eb] | 169 | {
|
---|
| 170 | config.cpu_count = 1;
|
---|
| 171 | config.cpu_active = 1;
|
---|
[a35b458] | 172 |
|
---|
[8a1afd2] | 173 | config.base = (uintptr_t) kernel_load_address;
|
---|
| 174 |
|
---|
| 175 | config.kernel_size =
|
---|
| 176 | ALIGN_UP((uintptr_t) kdata_end - config.base, PAGE_SIZE);
|
---|
[a35b458] | 177 |
|
---|
[ed7e057] | 178 | context_create(&ctx, main_bsp_separated_stack,
|
---|
[65f3117] | 179 | bootstrap_stack, bootstrap_stack_size);
|
---|
[be50915] | 180 | context_restore(&ctx);
|
---|
[f761f1eb] | 181 | /* not reached */
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[7f0837c] | 184 | /** Main kernel routine for bootstrap CPU using new stack.
|
---|
[673104e] | 185 | *
|
---|
| 186 | * Second part of main_bsp().
|
---|
| 187 | *
|
---|
| 188 | */
|
---|
[263bda2] | 189 | void main_bsp_separated_stack(void)
|
---|
[fde6429] | 190 | {
|
---|
[7a4202d] | 191 | /* Keep this the first thing. */
|
---|
[a6e55886] | 192 | current_initialize(CURRENT);
|
---|
[a35b458] | 193 |
|
---|
[7a4202d] | 194 | version_print();
|
---|
[a35b458] | 195 |
|
---|
[d28bdbe] | 196 | LOG("\nconfig.base=%p config.kernel_size=%zu",
|
---|
| 197 | (void *) config.base, config.kernel_size);
|
---|
[a35b458] | 198 |
|
---|
[76fca31] | 199 | #ifdef CONFIG_KCONSOLE
|
---|
[ff3b3197] | 200 | /*
|
---|
| 201 | * kconsole data structures must be initialized very early
|
---|
| 202 | * because other subsystems will register their respective
|
---|
| 203 | * commands.
|
---|
| 204 | */
|
---|
[263bda2] | 205 | kconsole_init();
|
---|
[76fca31] | 206 | #endif
|
---|
[a35b458] | 207 |
|
---|
[ef67bab] | 208 | /*
|
---|
| 209 | * Exception handler initialization, before architecture
|
---|
[0132630] | 210 | * starts adding its own handlers
|
---|
[aace6624] | 211 | */
|
---|
[263bda2] | 212 | exc_init();
|
---|
[a35b458] | 213 |
|
---|
[fc1e4f6] | 214 | /*
|
---|
| 215 | * Memory management subsystems initialization.
|
---|
[b84aaba] | 216 | */
|
---|
[36df4109] | 217 | ARCH_OP(pre_mm_init);
|
---|
[622f409] | 218 | km_identity_init();
|
---|
[263bda2] | 219 | frame_init();
|
---|
| 220 | slab_cache_init();
|
---|
[b60615bd] | 221 | malloc_init();
|
---|
[dabbe28] | 222 | ra_init();
|
---|
[263bda2] | 223 | sysinfo_init();
|
---|
| 224 | as_init();
|
---|
| 225 | page_init();
|
---|
| 226 | tlb_init();
|
---|
[622f409] | 227 | km_non_identity_init();
|
---|
[263bda2] | 228 | ddi_init();
|
---|
[36df4109] | 229 | ARCH_OP(post_mm_init);
|
---|
[8d308b9] | 230 | reserve_init();
|
---|
[36df4109] | 231 | ARCH_OP(pre_smp_init);
|
---|
[263bda2] | 232 | smp_init();
|
---|
[a35b458] | 233 |
|
---|
[f8ddd17] | 234 | /* Slab must be initialized after we know the number of processors. */
|
---|
[263bda2] | 235 | slab_enable_cpucache();
|
---|
[a35b458] | 236 |
|
---|
[933cadf] | 237 | uint64_t size;
|
---|
| 238 | const char *size_suffix;
|
---|
| 239 | bin_order_suffix(zones_total_size(), &size, &size_suffix, false);
|
---|
| 240 | printf("Detected %u CPU(s), %" PRIu64 " %s free memory\n",
|
---|
| 241 | config.cpu_count, size, size_suffix);
|
---|
[a35b458] | 242 |
|
---|
[263bda2] | 243 | cpu_init();
|
---|
| 244 | calibrate_delay_loop();
|
---|
[36df4109] | 245 | ARCH_OP(post_cpu_init);
|
---|
[49e6c6b4] | 246 |
|
---|
[263bda2] | 247 | clock_counter_init();
|
---|
| 248 | timeout_init();
|
---|
| 249 | scheduler_init();
|
---|
[ce732e74] | 250 | caps_init();
|
---|
[263bda2] | 251 | task_init();
|
---|
| 252 | thread_init();
|
---|
[d314571] | 253 | sys_waitq_init();
|
---|
[3b3faf51] | 254 |
|
---|
| 255 | sysinfo_set_item_data("boot_args", NULL, bargs, str_size(bargs) + 1);
|
---|
| 256 |
|
---|
[c06eb06] | 257 | if (init.cnt > 0) {
|
---|
[98000fb] | 258 | size_t i;
|
---|
[c06eb06] | 259 | for (i = 0; i < init.cnt; i++)
|
---|
[7e752b2] | 260 | LOG("init[%zu].addr=%p, init[%zu].size=%zu",
|
---|
[edd7c63c] | 261 | i, (void *) init.tasks[i].paddr, i, init.tasks[i].size);
|
---|
[c06eb06] | 262 | } else
|
---|
[ae5aa90] | 263 | printf("No init binaries found.\n");
|
---|
[a35b458] | 264 |
|
---|
[263bda2] | 265 | ipc_init();
|
---|
| 266 | event_init();
|
---|
[6fa9a99d] | 267 | kio_init();
|
---|
[91db0280] | 268 | log_init();
|
---|
[263bda2] | 269 | stats_init();
|
---|
[a35b458] | 270 |
|
---|
[f761f1eb] | 271 | /*
|
---|
| 272 | * Create kernel task.
|
---|
| 273 | */
|
---|
[b84aaba] | 274 | task_t *kernel = task_create(AS_KERNEL, "kernel");
|
---|
| 275 | if (!kernel)
|
---|
[f651e80] | 276 | panic("Cannot create kernel task.");
|
---|
[a35b458] | 277 |
|
---|
[f35749e] | 278 | kernel_task = kernel;
|
---|
| 279 |
|
---|
[f761f1eb] | 280 | /*
|
---|
| 281 | * Create the first thread.
|
---|
| 282 | */
|
---|
[6eef3c4] | 283 | thread_t *kinit_thread = thread_create(kinit, NULL, kernel,
|
---|
| 284 | THREAD_FLAG_UNCOUNTED, "kinit");
|
---|
[b84aaba] | 285 | if (!kinit_thread)
|
---|
[f651e80] | 286 | panic("Cannot create kinit thread.");
|
---|
[0f4f1b2] | 287 | thread_start(kinit_thread);
|
---|
| 288 | thread_detach(kinit_thread);
|
---|
[a35b458] | 289 |
|
---|
[f761f1eb] | 290 | /*
|
---|
[151c050] | 291 | * This call to scheduler_run() will return to kinit,
|
---|
[f761f1eb] | 292 | * starting the thread of kernel threads.
|
---|
| 293 | */
|
---|
[25939997] | 294 | current_copy(CURRENT, (current_t *) CPU_LOCAL->stack);
|
---|
| 295 | context_replace(scheduler_run, CPU_LOCAL->stack, STACK_SIZE);
|
---|
[f761f1eb] | 296 | /* not reached */
|
---|
| 297 | }
|
---|
| 298 |
|
---|
[5f85c91] | 299 | #ifdef CONFIG_SMP
|
---|
[263bda2] | 300 |
|
---|
[7f0837c] | 301 | /** Main kernel routine for application CPUs.
|
---|
[673104e] | 302 | *
|
---|
| 303 | * Executed by application processors, temporary stack
|
---|
[26678e5] | 304 | * is at ctx.sp which was set during BSP boot.
|
---|
[5fe5f1e] | 305 | * This function passes control directly to
|
---|
| 306 | * main_ap_separated_stack().
|
---|
[673104e] | 307 | *
|
---|
[22f7769] | 308 | * Assuming interrupts_disable()'d.
|
---|
[673104e] | 309 | *
|
---|
[f761f1eb] | 310 | */
|
---|
| 311 | void main_ap(void)
|
---|
| 312 | {
|
---|
| 313 | /*
|
---|
| 314 | * Incrementing the active CPU counter will guarantee that the
|
---|
[26678e5] | 315 | * *_init() functions can find out that they need to
|
---|
| 316 | * do initialization for AP only.
|
---|
[f761f1eb] | 317 | */
|
---|
| 318 | config.cpu_active++;
|
---|
[a35b458] | 319 |
|
---|
[7ce9284] | 320 | /*
|
---|
[a6e55886] | 321 | * The CURRENT structure is well defined because ctx.sp is used as stack.
|
---|
[7ce9284] | 322 | */
|
---|
[a6e55886] | 323 | current_initialize(CURRENT);
|
---|
[a35b458] | 324 |
|
---|
[36df4109] | 325 | ARCH_OP(pre_mm_init);
|
---|
[f761f1eb] | 326 | frame_init();
|
---|
| 327 | page_init();
|
---|
[ce031f0] | 328 | tlb_init();
|
---|
[36df4109] | 329 | ARCH_OP(post_mm_init);
|
---|
[a35b458] | 330 |
|
---|
[f761f1eb] | 331 | cpu_init();
|
---|
| 332 | calibrate_delay_loop();
|
---|
[36df4109] | 333 | ARCH_OP(post_cpu_init);
|
---|
[a35b458] | 334 |
|
---|
[f761f1eb] | 335 | /*
|
---|
| 336 | * If we woke kmp up before we left the kernel stack, we could
|
---|
| 337 | * collide with another CPU coming up. To prevent this, we
|
---|
| 338 | * switch to this cpu's private stack prior to waking kmp up.
|
---|
| 339 | */
|
---|
[25939997] | 340 | current_copy(CURRENT, (current_t *) CPU_LOCAL->stack);
|
---|
[ed7e057] | 341 | context_replace(main_ap_separated_stack, CPU_LOCAL->stack, STACK_SIZE);
|
---|
[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 | */
|
---|