source: mainline/kernel/generic/src/main/main.c@ 3e7948c

Last change on this file since 3e7948c was f35749e, checked in by Jiri Svoboda <jiri@…>, 4 months ago

System restart via shutdown -r

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