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
Line 
1/*
2 * Copyright (c) 2025 Jiri Svoboda
3 * Copyright (c) 2001-2004 Jakub Jermar
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
30/** @addtogroup kernel_generic
31 * @{
32 */
33
34/**
35 * @file
36 * @brief Main initialization kernel function for all processors.
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
50#include <arch/asm.h>
51#include <debug.h>
52#include <context.h>
53#include <stdio.h>
54#include <panic.h>
55#include <config.h>
56#include <time/clock.h>
57#include <time/timeout.h>
58#include <proc/scheduler.h>
59#include <proc/thread.h>
60#include <proc/task.h>
61#include <main/kinit.h>
62#include <main/version.h>
63#include <console/kconsole.h>
64#include <console/console.h>
65#include <log.h>
66#include <cpu.h>
67#include <align.h>
68#include <interrupt.h>
69#include <str.h>
70#include <mm/frame.h>
71#include <mm/page.h>
72#include <genarch/mm/page_pt.h>
73#include <mm/km.h>
74#include <mm/tlb.h>
75#include <mm/as.h>
76#include <mm/slab.h>
77#include <mm/reserve.h>
78#include <synch/waitq.h>
79#include <synch/syswaitq.h>
80#include <arch/arch.h>
81#include <arch.h>
82#include <ipc/ipc.h>
83#include <macros.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#include <cap/cap.h>
92
93/*
94 * Ensure [u]int*_t types are of correct size.
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) \
100 static_assert(sizeof(signness##size##_t) * 8 == size, \
101 #signness #size "_t does not have " #size " bits");
102
103#define CHECK_INT_TYPE(size) \
104 CHECK_INT_TYPE_(int, size); \
105 CHECK_INT_TYPE_(uint, size)
106
107CHECK_INT_TYPE(8);
108CHECK_INT_TYPE(16);
109CHECK_INT_TYPE(32);
110CHECK_INT_TYPE(64);
111
112task_t *kernel_task;
113
114/** Global configuration structure. */
115config_t config = {
116 .identity_configured = false,
117 .non_identity_configured = false,
118 .physmem_end = 0
119};
120
121/** Boot arguments. */
122char bargs[CONFIG_BOOT_ARGUMENTS_BUFLEN] = { };
123
124/** Initial user-space tasks */
125init_t init = {
126 .cnt = 0
127};
128
129/** Boot allocations. */
130ballocs_t ballocs = {
131 .base = (uintptr_t) NULL,
132 .size = 0
133};
134
135static context_t ctx;
136
137// NOTE: All kernel stacks must be aligned to STACK_SIZE, see CURRENT.
138static const size_t bootstrap_stack_size = STACK_SIZE;
139static _Alignas(STACK_SIZE) uint8_t bootstrap_stack[STACK_SIZE];
140
141/* Just a convenient value for some assembly code. */
142uint8_t *const bootstrap_stack_top = bootstrap_stack + STACK_SIZE;
143
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);
151
152#ifdef CONFIG_SMP
153static void main_ap_separated_stack(void);
154#endif
155
156/** Main kernel routine for bootstrap CPU.
157 *
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.
163 *
164 * Assuming interrupts_disable().
165 *
166 */
167_NO_TRACE void main_bsp(void)
168{
169 config.cpu_count = 1;
170 config.cpu_active = 1;
171
172 config.base = (uintptr_t) kernel_load_address;
173
174 config.kernel_size =
175 ALIGN_UP((uintptr_t) kdata_end - config.base, PAGE_SIZE);
176
177 context_create(&ctx, main_bsp_separated_stack,
178 bootstrap_stack, bootstrap_stack_size);
179 context_restore(&ctx);
180 /* not reached */
181}
182
183/** Main kernel routine for bootstrap CPU using new stack.
184 *
185 * Second part of main_bsp().
186 *
187 */
188void main_bsp_separated_stack(void)
189{
190 /* Keep this the first thing. */
191 current_initialize(CURRENT);
192
193 version_print();
194
195 LOG("\nconfig.base=%p config.kernel_size=%zu",
196 (void *) config.base, config.kernel_size);
197
198#ifdef CONFIG_KCONSOLE
199 /*
200 * kconsole data structures must be initialized very early
201 * because other subsystems will register their respective
202 * commands.
203 */
204 kconsole_init();
205#endif
206
207 /*
208 * Exception handler initialization, before architecture
209 * starts adding its own handlers
210 */
211 exc_init();
212
213 /*
214 * Memory management subsystems initialization.
215 */
216 ARCH_OP(pre_mm_init);
217 km_identity_init();
218 frame_init();
219 slab_cache_init();
220 malloc_init();
221 ra_init();
222 sysinfo_init();
223 as_init();
224 page_init();
225 tlb_init();
226 km_non_identity_init();
227 ddi_init();
228 ARCH_OP(post_mm_init);
229 reserve_init();
230 ARCH_OP(pre_smp_init);
231 smp_init();
232
233 /* Slab must be initialized after we know the number of processors. */
234 slab_enable_cpucache();
235
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);
241
242 cpu_init();
243 calibrate_delay_loop();
244 ARCH_OP(post_cpu_init);
245
246 clock_counter_init();
247 timeout_init();
248 scheduler_init();
249 caps_init();
250 task_init();
251 thread_init();
252 sys_waitq_init();
253
254 sysinfo_set_item_data("boot_args", NULL, bargs, str_size(bargs) + 1);
255
256 if (init.cnt > 0) {
257 size_t i;
258 for (i = 0; i < init.cnt; i++)
259 LOG("init[%zu].addr=%p, init[%zu].size=%zu",
260 i, (void *) init.tasks[i].paddr, i, init.tasks[i].size);
261 } else
262 printf("No init binaries found.\n");
263
264 ipc_init();
265 event_init();
266 kio_init();
267 log_init();
268 stats_init();
269
270 /*
271 * Create kernel task.
272 */
273 task_t *kernel = task_create(AS_KERNEL, "kernel");
274 if (!kernel)
275 panic("Cannot create kernel task.");
276
277 kernel_task = kernel;
278
279 /*
280 * Create the first thread.
281 */
282 thread_t *kinit_thread = thread_create(kinit, NULL, kernel,
283 THREAD_FLAG_UNCOUNTED, "kinit");
284 if (!kinit_thread)
285 panic("Cannot create kinit thread.");
286 thread_start(kinit_thread);
287 thread_detach(kinit_thread);
288
289 /*
290 * This call to scheduler_run() will return to kinit,
291 * starting the thread of kernel threads.
292 */
293 current_copy(CURRENT, (current_t *) CPU_LOCAL->stack);
294 context_replace(scheduler_run, CPU_LOCAL->stack, STACK_SIZE);
295 /* not reached */
296}
297
298#ifdef CONFIG_SMP
299
300/** Main kernel routine for application CPUs.
301 *
302 * Executed by application processors, temporary stack
303 * is at ctx.sp which was set during BSP boot.
304 * This function passes control directly to
305 * main_ap_separated_stack().
306 *
307 * Assuming interrupts_disable()'d.
308 *
309 */
310void main_ap(void)
311{
312 /*
313 * Incrementing the active CPU counter will guarantee that the
314 * *_init() functions can find out that they need to
315 * do initialization for AP only.
316 */
317 config.cpu_active++;
318
319 /*
320 * The CURRENT structure is well defined because ctx.sp is used as stack.
321 */
322 current_initialize(CURRENT);
323
324 ARCH_OP(pre_mm_init);
325 frame_init();
326 page_init();
327 tlb_init();
328 ARCH_OP(post_mm_init);
329
330 cpu_init();
331 calibrate_delay_loop();
332 ARCH_OP(post_cpu_init);
333
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 */
339 current_copy(CURRENT, (current_t *) CPU_LOCAL->stack);
340 context_replace(main_ap_separated_stack, CPU_LOCAL->stack, STACK_SIZE);
341 /* not reached */
342}
343
344/** Main kernel routine for application CPUs using new stack.
345 *
346 * Second part of main_ap().
347 *
348 */
349void main_ap_separated_stack(void)
350{
351 /*
352 * Configure timeouts for this cpu.
353 */
354 timeout_init();
355
356 semaphore_up(&ap_completion_semaphore);
357 scheduler_run();
358 /* not reached */
359}
360
361#endif /* CONFIG_SMP */
362
363/** @}
364 */
Note: See TracBrowser for help on using the repository browser.