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

Last change on this file since d89b259 was 8df5f20, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 6 years ago

Rename NO_TRACE to _NO_TRACE

<trace.h> may end up transitively included from standard headers,
so it needs to be a reserved identifier.

  • Property mode set to 100644
File size: 9.6 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>
[f2ffad4]82#include <arch/faddr.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
108CHECK_INT_TYPE(8);
109CHECK_INT_TYPE(16);
110CHECK_INT_TYPE(32);
111CHECK_INT_TYPE(64);
112
[e459f12]113/** Global configuration structure. */
[55896b6]114config_t config = {
115 .identity_configured = false,
[40c8c17]116 .non_identity_configured = false,
117 .physmem_end = 0
[55896b6]118};
[e459f12]119
[3b3faf51]120/** Boot arguments. */
[3bacee1]121char bargs[CONFIG_BOOT_ARGUMENTS_BUFLEN] = { };
[3b3faf51]122
[e459f12]123/** Initial user-space tasks */
124init_t init = {
[6c441cf8]125 .cnt = 0
[e459f12]126};
[5fe5f1e]127
[61e90dd]128/** Boot allocations. */
129ballocs_t ballocs = {
[0b4a67a]130 .base = (uintptr_t) NULL,
[61e90dd]131 .size = 0
132};
133
[f761f1eb]134context_t ctx;
135
[c0855a0]136/** Lowest safe stack virtual address. */
[263bda2]137uintptr_t stack_safe = 0;
[deaa22f]138
[f761f1eb]139/*
140 * These two functions prevent stack from underflowing during the
141 * kernel boot phase when SP is set to the very top of the reserved
142 * space. The stack could get corrupted by a fooled compiler-generated
143 * pop sequence otherwise.
144 */
145static void main_bsp_separated_stack(void);
[263bda2]146
[80d2bdb]147#ifdef CONFIG_SMP
[f761f1eb]148static void main_ap_separated_stack(void);
[80d2bdb]149#endif
[f761f1eb]150
[7f0837c]151/** Main kernel routine for bootstrap CPU.
[673104e]152 *
[7a4202d]153 * The code here still runs on the boot stack, which knows nothing about
154 * preemption counts. Because of that, this function cannot directly call
155 * functions that disable or enable preemption (e.g. spinlock_lock()). The
156 * primary task of this function is to calculate address of a new stack and
157 * switch to it.
[673104e]158 *
[22f7769]159 * Assuming interrupts_disable().
[673104e]160 *
[f761f1eb]161 */
[8df5f20]162_NO_TRACE void main_bsp(void)
[f761f1eb]163{
164 config.cpu_count = 1;
165 config.cpu_active = 1;
[a35b458]166
[8a1afd2]167 config.base = (uintptr_t) kernel_load_address;
168
169 config.kernel_size =
170 ALIGN_UP((uintptr_t) kdata_end - config.base, PAGE_SIZE);
[a35b458]171
[95d45482]172 // NOTE: All kernel stacks must be aligned to STACK_SIZE,
173 // see get_stack_base().
[d1da1ff2]174
[d59718e]175 /* Place the stack after the kernel, init and ballocs. */
[d1da1ff2]176 config.stack_base =
177 ALIGN_UP(config.base + config.kernel_size, STACK_SIZE);
[d59718e]178 config.stack_size = STACK_SIZE;
[a35b458]179
[deaa22f]180 /* Avoid placing stack on top of init */
[98000fb]181 size_t i;
[deaa22f]182 for (i = 0; i < init.cnt; i++) {
[d59718e]183 uintptr_t p = init.tasks[i].paddr + init.tasks[i].size;
[d1da1ff2]184 uintptr_t bottom = PA2KA(ALIGN_UP(p, STACK_SIZE));
[d59718e]185
186 if (config.stack_base < bottom)
187 config.stack_base = bottom;
[deaa22f]188 }
[a35b458]189
[61e90dd]190 /* Avoid placing stack on top of boot allocations. */
191 if (ballocs.size) {
[d59718e]192 uintptr_t bottom =
[d1da1ff2]193 ALIGN_UP(ballocs.base + ballocs.size, STACK_SIZE);
[d59718e]194 if (config.stack_base < bottom)
195 config.stack_base = bottom;
[61e90dd]196 }
[a35b458]197
[deaa22f]198 if (config.stack_base < stack_safe)
[d1da1ff2]199 config.stack_base = ALIGN_UP(stack_safe, STACK_SIZE);
[a35b458]200
[be50915]201 context_save(&ctx);
[26aafe8]202 context_set(&ctx, FADDR(main_bsp_separated_stack),
203 config.stack_base, STACK_SIZE);
[be50915]204 context_restore(&ctx);
[f761f1eb]205 /* not reached */
206}
207
[7f0837c]208/** Main kernel routine for bootstrap CPU using new stack.
[673104e]209 *
210 * Second part of main_bsp().
211 *
212 */
[263bda2]213void main_bsp_separated_stack(void)
[fde6429]214{
[7a4202d]215 /* Keep this the first thing. */
[a6e55886]216 current_initialize(CURRENT);
[a35b458]217
[7a4202d]218 version_print();
[a35b458]219
[7e752b2]220 LOG("\nconfig.base=%p config.kernel_size=%zu"
221 "\nconfig.stack_base=%p config.stack_size=%zu",
[1caeb2d]222 (void *) config.base, config.kernel_size,
223 (void *) config.stack_base, config.stack_size);
[a35b458]224
[76fca31]225#ifdef CONFIG_KCONSOLE
[ff3b3197]226 /*
227 * kconsole data structures must be initialized very early
228 * because other subsystems will register their respective
229 * commands.
230 */
[263bda2]231 kconsole_init();
[76fca31]232#endif
[a35b458]233
[ef67bab]234 /*
235 * Exception handler initialization, before architecture
[0132630]236 * starts adding its own handlers
[aace6624]237 */
[263bda2]238 exc_init();
[a35b458]239
[fc1e4f6]240 /*
241 * Memory management subsystems initialization.
[b84aaba]242 */
[36df4109]243 ARCH_OP(pre_mm_init);
[622f409]244 km_identity_init();
[263bda2]245 frame_init();
246 slab_cache_init();
[b60615bd]247 malloc_init();
[dabbe28]248 ra_init();
[263bda2]249 sysinfo_init();
250 as_init();
251 page_init();
252 tlb_init();
[622f409]253 km_non_identity_init();
[263bda2]254 ddi_init();
[36df4109]255 ARCH_OP(post_mm_init);
[8d308b9]256 reserve_init();
[36df4109]257 ARCH_OP(pre_smp_init);
[263bda2]258 smp_init();
[a35b458]259
[f8ddd17]260 /* Slab must be initialized after we know the number of processors. */
[263bda2]261 slab_enable_cpucache();
[a35b458]262
[933cadf]263 uint64_t size;
264 const char *size_suffix;
265 bin_order_suffix(zones_total_size(), &size, &size_suffix, false);
266 printf("Detected %u CPU(s), %" PRIu64 " %s free memory\n",
267 config.cpu_count, size, size_suffix);
[a35b458]268
[263bda2]269 cpu_init();
270 calibrate_delay_loop();
[36df4109]271 ARCH_OP(post_cpu_init);
[49e6c6b4]272
[263bda2]273 clock_counter_init();
274 timeout_init();
275 scheduler_init();
[ce732e74]276 caps_init();
[263bda2]277 task_init();
278 thread_init();
[d314571]279 sys_waitq_init();
[3b3faf51]280
281 sysinfo_set_item_data("boot_args", NULL, bargs, str_size(bargs) + 1);
282
[c06eb06]283 if (init.cnt > 0) {
[98000fb]284 size_t i;
[c06eb06]285 for (i = 0; i < init.cnt; i++)
[7e752b2]286 LOG("init[%zu].addr=%p, init[%zu].size=%zu",
[edd7c63c]287 i, (void *) init.tasks[i].paddr, i, init.tasks[i].size);
[c06eb06]288 } else
[ae5aa90]289 printf("No init binaries found.\n");
[a35b458]290
[263bda2]291 ipc_init();
292 event_init();
[6fa9a99d]293 kio_init();
[91db0280]294 log_init();
[263bda2]295 stats_init();
[a35b458]296
[f761f1eb]297 /*
298 * Create kernel task.
299 */
[b84aaba]300 task_t *kernel = task_create(AS_KERNEL, "kernel");
301 if (!kernel)
[f651e80]302 panic("Cannot create kernel task.");
[a35b458]303
[f761f1eb]304 /*
305 * Create the first thread.
306 */
[6eef3c4]307 thread_t *kinit_thread = thread_create(kinit, NULL, kernel,
308 THREAD_FLAG_UNCOUNTED, "kinit");
[b84aaba]309 if (!kinit_thread)
[f651e80]310 panic("Cannot create kinit thread.");
[263bda2]311 thread_ready(kinit_thread);
[a35b458]312
[f761f1eb]313 /*
314 * This call to scheduler() will return to kinit,
315 * starting the thread of kernel threads.
316 */
317 scheduler();
318 /* not reached */
319}
320
[5f85c91]321#ifdef CONFIG_SMP
[263bda2]322
[7f0837c]323/** Main kernel routine for application CPUs.
[673104e]324 *
325 * Executed by application processors, temporary stack
[26678e5]326 * is at ctx.sp which was set during BSP boot.
[5fe5f1e]327 * This function passes control directly to
328 * main_ap_separated_stack().
[673104e]329 *
[22f7769]330 * Assuming interrupts_disable()'d.
[673104e]331 *
[f761f1eb]332 */
333void main_ap(void)
334{
335 /*
336 * Incrementing the active CPU counter will guarantee that the
[26678e5]337 * *_init() functions can find out that they need to
338 * do initialization for AP only.
[f761f1eb]339 */
340 config.cpu_active++;
[a35b458]341
[7ce9284]342 /*
[a6e55886]343 * The CURRENT structure is well defined because ctx.sp is used as stack.
[7ce9284]344 */
[a6e55886]345 current_initialize(CURRENT);
[a35b458]346
[36df4109]347 ARCH_OP(pre_mm_init);
[f761f1eb]348 frame_init();
349 page_init();
[ce031f0]350 tlb_init();
[36df4109]351 ARCH_OP(post_mm_init);
[a35b458]352
[f761f1eb]353 cpu_init();
354 calibrate_delay_loop();
[36df4109]355 ARCH_OP(post_cpu_init);
[a35b458]356
[a6e55886]357 current_copy(CURRENT, (current_t *) CPU->stack);
[a35b458]358
[f761f1eb]359 /*
360 * If we woke kmp up before we left the kernel stack, we could
361 * collide with another CPU coming up. To prevent this, we
362 * switch to this cpu's private stack prior to waking kmp up.
363 */
[b24786a]364 context_save(&CPU->saved_context);
[f8ddd17]365 context_set(&CPU->saved_context, FADDR(main_ap_separated_stack),
[26aafe8]366 (uintptr_t) CPU->stack, STACK_SIZE);
[be50915]367 context_restore(&CPU->saved_context);
[f761f1eb]368 /* not reached */
369}
370
[7f0837c]371/** Main kernel routine for application CPUs using new stack.
[673104e]372 *
373 * Second part of main_ap().
374 *
375 */
[f761f1eb]376void main_ap_separated_stack(void)
377{
378 /*
379 * Configure timeouts for this cpu.
380 */
381 timeout_init();
[a35b458]382
[f761f1eb]383 waitq_wakeup(&ap_completion_wq, WAKEUP_FIRST);
384 scheduler();
385 /* not reached */
386}
[263bda2]387
[5f85c91]388#endif /* CONFIG_SMP */
[b45c443]389
[8e3bf3e2]390/** @}
[b45c443]391 */
Note: See TracBrowser for help on using the repository browser.