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

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

Make separate-stack-scheduler a loop with persistent context

We can see scheduler as a looping idle thread in which a thread
is repeatedly retrieved from a queue, prepared, switched to,
returned from, and cleaned up after.

IMO this is a more natural view of the process.

  • Property mode set to 100644
File size: 8.9 KB
RevLine 
[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>
[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
[e459f12]112/** Global configuration structure. */
[55896b6]113config_t config = {
114 .identity_configured = false,
[40c8c17]115 .non_identity_configured = false,
116 .physmem_end = 0
[55896b6]117};
[e459f12]118
[3b3faf51]119/** Boot arguments. */
[3bacee1]120char bargs[CONFIG_BOOT_ARGUMENTS_BUFLEN] = { };
[3b3faf51]121
[e459f12]122/** Initial user-space tasks */
123init_t init = {
[6c441cf8]124 .cnt = 0
[e459f12]125};
[5fe5f1e]126
[61e90dd]127/** Boot allocations. */
128ballocs_t ballocs = {
[0b4a67a]129 .base = (uintptr_t) NULL,
[61e90dd]130 .size = 0
131};
132
[deed510]133static context_t ctx;
[deaa22f]134
[65f3117]135// NOTE: All kernel stacks must be aligned to STACK_SIZE, see CURRENT.
[deed510]136static const size_t bootstrap_stack_size = STACK_SIZE;
137static _Alignas(STACK_SIZE) uint8_t bootstrap_stack[STACK_SIZE];
138
[65f3117]139/* Just a convenient value for some assembly code. */
[deed510]140uint8_t *const bootstrap_stack_top = bootstrap_stack + STACK_SIZE;
[65f3117]141
[f761f1eb]142/*
143 * These two functions prevent stack from underflowing during the
144 * kernel boot phase when SP is set to the very top of the reserved
145 * space. The stack could get corrupted by a fooled compiler-generated
146 * pop sequence otherwise.
147 */
148static void main_bsp_separated_stack(void);
[263bda2]149
[80d2bdb]150#ifdef CONFIG_SMP
[f761f1eb]151static void main_ap_separated_stack(void);
[80d2bdb]152#endif
[f761f1eb]153
[7f0837c]154/** Main kernel routine for bootstrap CPU.
[673104e]155 *
[7a4202d]156 * The code here still runs on the boot stack, which knows nothing about
157 * preemption counts. Because of that, this function cannot directly call
158 * functions that disable or enable preemption (e.g. spinlock_lock()). The
159 * primary task of this function is to calculate address of a new stack and
160 * switch to it.
[673104e]161 *
[22f7769]162 * Assuming interrupts_disable().
[673104e]163 *
[f761f1eb]164 */
[8df5f20]165_NO_TRACE void main_bsp(void)
[f761f1eb]166{
167 config.cpu_count = 1;
168 config.cpu_active = 1;
[a35b458]169
[8a1afd2]170 config.base = (uintptr_t) kernel_load_address;
171
172 config.kernel_size =
173 ALIGN_UP((uintptr_t) kdata_end - config.base, PAGE_SIZE);
[a35b458]174
[ed7e057]175 context_create(&ctx, main_bsp_separated_stack,
[65f3117]176 bootstrap_stack, bootstrap_stack_size);
[be50915]177 context_restore(&ctx);
[f761f1eb]178 /* not reached */
179}
180
[7f0837c]181/** Main kernel routine for bootstrap CPU using new stack.
[673104e]182 *
183 * Second part of main_bsp().
184 *
185 */
[263bda2]186void main_bsp_separated_stack(void)
[fde6429]187{
[7a4202d]188 /* Keep this the first thing. */
[a6e55886]189 current_initialize(CURRENT);
[a35b458]190
[7a4202d]191 version_print();
[a35b458]192
[d28bdbe]193 LOG("\nconfig.base=%p config.kernel_size=%zu",
194 (void *) config.base, config.kernel_size);
[a35b458]195
[76fca31]196#ifdef CONFIG_KCONSOLE
[ff3b3197]197 /*
198 * kconsole data structures must be initialized very early
199 * because other subsystems will register their respective
200 * commands.
201 */
[263bda2]202 kconsole_init();
[76fca31]203#endif
[a35b458]204
[ef67bab]205 /*
206 * Exception handler initialization, before architecture
[0132630]207 * starts adding its own handlers
[aace6624]208 */
[263bda2]209 exc_init();
[a35b458]210
[fc1e4f6]211 /*
212 * Memory management subsystems initialization.
[b84aaba]213 */
[36df4109]214 ARCH_OP(pre_mm_init);
[622f409]215 km_identity_init();
[263bda2]216 frame_init();
217 slab_cache_init();
[b60615bd]218 malloc_init();
[dabbe28]219 ra_init();
[263bda2]220 sysinfo_init();
221 as_init();
222 page_init();
223 tlb_init();
[622f409]224 km_non_identity_init();
[263bda2]225 ddi_init();
[36df4109]226 ARCH_OP(post_mm_init);
[8d308b9]227 reserve_init();
[36df4109]228 ARCH_OP(pre_smp_init);
[263bda2]229 smp_init();
[a35b458]230
[f8ddd17]231 /* Slab must be initialized after we know the number of processors. */
[263bda2]232 slab_enable_cpucache();
[a35b458]233
[933cadf]234 uint64_t size;
235 const char *size_suffix;
236 bin_order_suffix(zones_total_size(), &size, &size_suffix, false);
237 printf("Detected %u CPU(s), %" PRIu64 " %s free memory\n",
238 config.cpu_count, size, size_suffix);
[a35b458]239
[263bda2]240 cpu_init();
241 calibrate_delay_loop();
[36df4109]242 ARCH_OP(post_cpu_init);
[49e6c6b4]243
[263bda2]244 clock_counter_init();
245 timeout_init();
246 scheduler_init();
[ce732e74]247 caps_init();
[263bda2]248 task_init();
249 thread_init();
[d314571]250 sys_waitq_init();
[3b3faf51]251
252 sysinfo_set_item_data("boot_args", NULL, bargs, str_size(bargs) + 1);
253
[c06eb06]254 if (init.cnt > 0) {
[98000fb]255 size_t i;
[c06eb06]256 for (i = 0; i < init.cnt; i++)
[7e752b2]257 LOG("init[%zu].addr=%p, init[%zu].size=%zu",
[edd7c63c]258 i, (void *) init.tasks[i].paddr, i, init.tasks[i].size);
[c06eb06]259 } else
[ae5aa90]260 printf("No init binaries found.\n");
[a35b458]261
[263bda2]262 ipc_init();
263 event_init();
[6fa9a99d]264 kio_init();
[91db0280]265 log_init();
[263bda2]266 stats_init();
[a35b458]267
[f761f1eb]268 /*
269 * Create kernel task.
270 */
[b84aaba]271 task_t *kernel = task_create(AS_KERNEL, "kernel");
272 if (!kernel)
[f651e80]273 panic("Cannot create kernel task.");
[a35b458]274
[f761f1eb]275 /*
276 * Create the first thread.
277 */
[6eef3c4]278 thread_t *kinit_thread = thread_create(kinit, NULL, kernel,
279 THREAD_FLAG_UNCOUNTED, "kinit");
[b84aaba]280 if (!kinit_thread)
[f651e80]281 panic("Cannot create kinit thread.");
[0f4f1b2]282 thread_start(kinit_thread);
283 thread_detach(kinit_thread);
[a35b458]284
[f761f1eb]285 /*
[151c050]286 * This call to scheduler_run() will return to kinit,
[f761f1eb]287 * starting the thread of kernel threads.
288 */
[25939997]289 current_copy(CURRENT, (current_t *) CPU_LOCAL->stack);
290 context_replace(scheduler_run, CPU_LOCAL->stack, STACK_SIZE);
[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 */
306void 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
[f761f1eb]330 /*
331 * If we woke kmp up before we left the kernel stack, we could
332 * collide with another CPU coming up. To prevent this, we
333 * switch to this cpu's private stack prior to waking kmp up.
334 */
[25939997]335 current_copy(CURRENT, (current_t *) CPU_LOCAL->stack);
[ed7e057]336 context_replace(main_ap_separated_stack, CPU_LOCAL->stack, STACK_SIZE);
[f761f1eb]337 /* not reached */
338}
339
[7f0837c]340/** Main kernel routine for application CPUs using new stack.
[673104e]341 *
342 * Second part of main_ap().
343 *
344 */
[f761f1eb]345void main_ap_separated_stack(void)
346{
347 /*
348 * Configure timeouts for this cpu.
349 */
350 timeout_init();
[a35b458]351
[7c5320c]352 semaphore_up(&ap_completion_semaphore);
[151c050]353 scheduler_run();
[f761f1eb]354 /* not reached */
355}
[263bda2]356
[5f85c91]357#endif /* CONFIG_SMP */
[b45c443]358
[8e3bf3e2]359/** @}
[b45c443]360 */
Note: See TracBrowser for help on using the repository browser.