source: mainline/kernel/generic/src/main/main.c

Last change on this file was 597fa24, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 months ago

Enable static initialization of kernel synchronization primitives

  • Property mode set to 100644
File size: 9.0 KB
RevLine 
[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>
[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>
[6d9c49a]82#include <ipc/ipc.h>
[93165be]83#include <macros.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>
[ce732e74]91#include <cap/cap.h>
[5fe5f1e]92
[7c3fb9b]93/*
94 * Ensure [u]int*_t types are of correct size.
[328cc31]95 *
96 * Probably, this is not the best place for such tests
97 * but this file is compiled on all architectures.
98 */
99#define CHECK_INT_TYPE_(signness, size) \
[63e27ef]100 static_assert(sizeof(signness##size##_t) * 8 == size, \
[328cc31]101 #signness #size "_t does not have " #size " bits");
[9e40355e]102
[328cc31]103#define CHECK_INT_TYPE(size) \
[9e40355e]104 CHECK_INT_TYPE_(int, size); \
105 CHECK_INT_TYPE_(uint, size)
[328cc31]106
107CHECK_INT_TYPE(8);
108CHECK_INT_TYPE(16);
109CHECK_INT_TYPE(32);
110CHECK_INT_TYPE(64);
111
[f35749e]112task_t *kernel_task;
113
[e459f12]114/** Global configuration structure. */
[55896b6]115config_t config = {
116 .identity_configured = false,
[40c8c17]117 .non_identity_configured = false,
118 .physmem_end = 0
[55896b6]119};
[e459f12]120
[3b3faf51]121/** Boot arguments. */
[3bacee1]122char bargs[CONFIG_BOOT_ARGUMENTS_BUFLEN] = { };
[3b3faf51]123
[e459f12]124/** Initial user-space tasks */
125init_t init = {
[6c441cf8]126 .cnt = 0
[e459f12]127};
[5fe5f1e]128
[61e90dd]129/** Boot allocations. */
130ballocs_t ballocs = {
[0b4a67a]131 .base = (uintptr_t) NULL,
[61e90dd]132 .size = 0
133};
134
[deed510]135static context_t ctx;
[deaa22f]136
[65f3117]137// NOTE: All kernel stacks must be aligned to STACK_SIZE, see CURRENT.
[deed510]138static const size_t bootstrap_stack_size = STACK_SIZE;
139static _Alignas(STACK_SIZE) uint8_t bootstrap_stack[STACK_SIZE];
140
[65f3117]141/* Just a convenient value for some assembly code. */
[deed510]142uint8_t *const bootstrap_stack_top = bootstrap_stack + STACK_SIZE;
[65f3117]143
[f761f1eb]144/*
145 * These two functions prevent stack from underflowing during the
146 * kernel boot phase when SP is set to the very top of the reserved
147 * space. The stack could get corrupted by a fooled compiler-generated
148 * pop sequence otherwise.
149 */
150static void main_bsp_separated_stack(void);
[263bda2]151
[80d2bdb]152#ifdef CONFIG_SMP
[f761f1eb]153static void main_ap_separated_stack(void);
[80d2bdb]154#endif
[f761f1eb]155
[7f0837c]156/** Main kernel routine for bootstrap CPU.
[673104e]157 *
[7a4202d]158 * The code here still runs on the boot stack, which knows nothing about
159 * preemption counts. Because of that, this function cannot directly call
160 * functions that disable or enable preemption (e.g. spinlock_lock()). The
161 * primary task of this function is to calculate address of a new stack and
162 * switch to it.
[673104e]163 *
[22f7769]164 * Assuming interrupts_disable().
[673104e]165 *
[f761f1eb]166 */
[8df5f20]167_NO_TRACE void main_bsp(void)
[f761f1eb]168{
169 config.cpu_count = 1;
170 config.cpu_active = 1;
[a35b458]171
[8a1afd2]172 config.base = (uintptr_t) kernel_load_address;
173
174 config.kernel_size =
175 ALIGN_UP((uintptr_t) kdata_end - config.base, PAGE_SIZE);
[a35b458]176
[ed7e057]177 context_create(&ctx, 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]188void 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
[f35749e]277 kernel_task = kernel;
278
[f761f1eb]279 /*
280 * Create the first thread.
281 */
[6eef3c4]282 thread_t *kinit_thread = thread_create(kinit, NULL, kernel,
283 THREAD_FLAG_UNCOUNTED, "kinit");
[b84aaba]284 if (!kinit_thread)
[f651e80]285 panic("Cannot create kinit thread.");
[0f4f1b2]286 thread_start(kinit_thread);
287 thread_detach(kinit_thread);
[a35b458]288
[f761f1eb]289 /*
[151c050]290 * This call to scheduler_run() will return to kinit,
[f761f1eb]291 * starting the thread of kernel threads.
292 */
[25939997]293 current_copy(CURRENT, (current_t *) CPU_LOCAL->stack);
294 context_replace(scheduler_run, CPU_LOCAL->stack, STACK_SIZE);
[f761f1eb]295 /* not reached */
296}
297
[5f85c91]298#ifdef CONFIG_SMP
[263bda2]299
[7f0837c]300/** Main kernel routine for application CPUs.
[673104e]301 *
302 * Executed by application processors, temporary stack
[26678e5]303 * is at ctx.sp which was set during BSP boot.
[5fe5f1e]304 * This function passes control directly to
305 * main_ap_separated_stack().
[673104e]306 *
[22f7769]307 * Assuming interrupts_disable()'d.
[673104e]308 *
[f761f1eb]309 */
310void main_ap(void)
311{
312 /*
313 * Incrementing the active CPU counter will guarantee that the
[26678e5]314 * *_init() functions can find out that they need to
315 * do initialization for AP only.
[f761f1eb]316 */
317 config.cpu_active++;
[a35b458]318
[7ce9284]319 /*
[a6e55886]320 * The CURRENT structure is well defined because ctx.sp is used as stack.
[7ce9284]321 */
[a6e55886]322 current_initialize(CURRENT);
[a35b458]323
[36df4109]324 ARCH_OP(pre_mm_init);
[f761f1eb]325 frame_init();
326 page_init();
[ce031f0]327 tlb_init();
[36df4109]328 ARCH_OP(post_mm_init);
[a35b458]329
[f761f1eb]330 cpu_init();
331 calibrate_delay_loop();
[36df4109]332 ARCH_OP(post_cpu_init);
[a35b458]333
[f761f1eb]334 /*
335 * If we woke kmp up before we left the kernel stack, we could
336 * collide with another CPU coming up. To prevent this, we
337 * switch to this cpu's private stack prior to waking kmp up.
338 */
[25939997]339 current_copy(CURRENT, (current_t *) CPU_LOCAL->stack);
[ed7e057]340 context_replace(main_ap_separated_stack, CPU_LOCAL->stack, STACK_SIZE);
[f761f1eb]341 /* not reached */
342}
343
[7f0837c]344/** Main kernel routine for application CPUs using new stack.
[673104e]345 *
346 * Second part of main_ap().
347 *
348 */
[f761f1eb]349void main_ap_separated_stack(void)
350{
351 /*
352 * Configure timeouts for this cpu.
353 */
354 timeout_init();
[a35b458]355
[7c5320c]356 semaphore_up(&ap_completion_semaphore);
[151c050]357 scheduler_run();
[f761f1eb]358 /* not reached */
359}
[263bda2]360
[5f85c91]361#endif /* CONFIG_SMP */
[b45c443]362
[8e3bf3e2]363/** @}
[b45c443]364 */
Note: See TracBrowser for help on using the repository browser.