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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ba1ea40 was 3b3faf51, checked in by Jakub Jermar <jakub@…>, 9 years ago

Add kernel global bargs to hold the boot arguments

  • Property mode set to 100644
File size: 9.8 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
[8e3bf3e2]29/** @addtogroup main
[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>
[d5d2a3f]50#include <context.h>
[f761f1eb]51#include <print.h>
[02a99d2]52#include <panic.h>
[940cac0]53#include <debug.h>
[f761f1eb]54#include <config.h>
55#include <time/clock.h>
[b3f8fb7]56#include <time/timeout.h>
[f761f1eb]57#include <proc/scheduler.h>
58#include <proc/thread.h>
59#include <proc/task.h>
60#include <main/kinit.h>
[5fe5f1e]61#include <main/version.h>
[f4338d2]62#include <console/kconsole.h>
[5d67baa]63#include <console/console.h>
[91db0280]64#include <log.h>
[f761f1eb]65#include <cpu.h>
[e0cdb7b6]66#include <align.h>
[aace6624]67#include <interrupt.h>
[f761f1eb]68#include <mm/frame.h>
69#include <mm/page.h>
[6d7ffa65]70#include <genarch/mm/page_pt.h>
[622f409]71#include <mm/km.h>
[169587a]72#include <mm/tlb.h>
[20d50a1]73#include <mm/as.h>
[4e147a6]74#include <mm/slab.h>
[8d308b9]75#include <mm/reserve.h>
[f761f1eb]76#include <synch/waitq.h>
[9aa72b4]77#include <synch/futex.h>
[8a64e81e]78#include <synch/workqueue.h>
[2ee1ccc]79#include <smp/smp_call.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>
[2810636]85#include <adt/btree.h>
[5fe5f1e]86#include <smp/smp.h>
[f8ddd17]87#include <ddi/ddi.h>
[4b241f3]88#include <main/main.h>
[13a638d]89#include <ipc/event.h>
[d9fae235]90#include <sysinfo/sysinfo.h>
[9dae191e]91#include <sysinfo/stats.h>
[41deb2a]92#include <lib/ra.h>
[5fe5f1e]93
[328cc31]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_VERBOSE(sizeof(signness##size##_t) * 8 == size, \
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. */
120char bargs[CONFIG_BOOT_ARGUMENTS_BUFLEN] = {};
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
[f761f1eb]133context_t ctx;
134
[c0855a0]135/** Lowest safe stack virtual address. */
[263bda2]136uintptr_t stack_safe = 0;
[deaa22f]137
[f761f1eb]138/*
139 * These two functions prevent stack from underflowing during the
140 * kernel boot phase when SP is set to the very top of the reserved
141 * space. The stack could get corrupted by a fooled compiler-generated
142 * pop sequence otherwise.
143 */
144static void main_bsp_separated_stack(void);
[263bda2]145
[80d2bdb]146#ifdef CONFIG_SMP
[f761f1eb]147static void main_ap_separated_stack(void);
[80d2bdb]148#endif
[f761f1eb]149
[7f0837c]150/** Main kernel routine for bootstrap CPU.
[673104e]151 *
[7a4202d]152 * The code here still runs on the boot stack, which knows nothing about
153 * preemption counts. Because of that, this function cannot directly call
154 * functions that disable or enable preemption (e.g. spinlock_lock()). The
155 * primary task of this function is to calculate address of a new stack and
156 * switch to it.
[673104e]157 *
[22f7769]158 * Assuming interrupts_disable().
[673104e]159 *
[f761f1eb]160 */
[7a0359b]161NO_TRACE void main_bsp(void)
[f761f1eb]162{
163 config.cpu_count = 1;
164 config.cpu_active = 1;
[d6e8529]165
[aed4eca]166 config.base = hardcoded_load_address;
[f8ddd17]167 config.kernel_size = ALIGN_UP(hardcoded_ktext_size +
[6f4495f5]168 hardcoded_kdata_size, PAGE_SIZE);
[26aafe8]169 config.stack_size = STACK_SIZE;
[deaa22f]170
171 /* Initialy the stack is placed just after the kernel */
172 config.stack_base = config.base + config.kernel_size;
[b6b576c]173
[deaa22f]174 /* Avoid placing stack on top of init */
[98000fb]175 size_t i;
[deaa22f]176 for (i = 0; i < init.cnt; i++) {
[32817cc]177 if (overlaps(KA2PA(config.stack_base), config.stack_size,
178 init.tasks[i].paddr, init.tasks[i].size)) {
179 /*
180 * The init task overlaps with the memory behind the
181 * kernel image so it must be in low memory and we can
182 * use PA2KA on the init task's physical address.
183 */
184 config.stack_base = ALIGN_UP(
185 PA2KA(init.tasks[i].paddr) + init.tasks[i].size,
186 config.stack_size);
187 }
[deaa22f]188 }
[263bda2]189
[61e90dd]190 /* Avoid placing stack on top of boot allocations. */
191 if (ballocs.size) {
[e2650d3]192 if (PA_OVERLAPS(config.stack_base, config.stack_size,
[6f4495f5]193 ballocs.base, ballocs.size))
[f8ddd17]194 config.stack_base = ALIGN_UP(ballocs.base +
[6f4495f5]195 ballocs.size, PAGE_SIZE);
[61e90dd]196 }
[b6b576c]197
[deaa22f]198 if (config.stack_base < stack_safe)
199 config.stack_base = ALIGN_UP(stack_safe, PAGE_SIZE);
[393f631]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. */
216 the_initialize(THE);
[8f91729]217
[7a4202d]218 version_print();
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);
[3ad953c]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
[b6b576c]233
[ef67bab]234 /*
235 * Exception handler initialization, before architecture
[0132630]236 * starts adding its own handlers
[aace6624]237 */
[263bda2]238 exc_init();
[3ad953c]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();
[dabbe28]247 ra_init();
[263bda2]248 sysinfo_init();
249 btree_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();
[71eef11]259
[f8ddd17]260 /* Slab must be initialized after we know the number of processors. */
[263bda2]261 slab_enable_cpucache();
[71eef11]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);
[b84aaba]268
[263bda2]269 cpu_init();
270 calibrate_delay_loop();
[36df4109]271 ARCH_OP(post_cpu_init);
[49e6c6b4]272
[2ee1ccc]273 smp_call_init();
[8a64e81e]274 workq_global_init();
[263bda2]275 clock_counter_init();
276 timeout_init();
277 scheduler_init();
278 task_init();
279 thread_init();
280 futex_init();
[3b3faf51]281
282 sysinfo_set_item_data("boot_args", NULL, bargs, str_size(bargs) + 1);
283
[c06eb06]284 if (init.cnt > 0) {
[98000fb]285 size_t i;
[c06eb06]286 for (i = 0; i < init.cnt; i++)
[7e752b2]287 LOG("init[%zu].addr=%p, init[%zu].size=%zu",
[edd7c63c]288 i, (void *) init.tasks[i].paddr, i, init.tasks[i].size);
[c06eb06]289 } else
[ae5aa90]290 printf("No init binaries found.\n");
[6d9c49a]291
[263bda2]292 ipc_init();
293 event_init();
[6fa9a99d]294 kio_init();
[91db0280]295 log_init();
[263bda2]296 stats_init();
[3ad953c]297
[f761f1eb]298 /*
299 * Create kernel task.
300 */
[b84aaba]301 task_t *kernel = task_create(AS_KERNEL, "kernel");
302 if (!kernel)
[f651e80]303 panic("Cannot create kernel task.");
[6f8a426]304
[f761f1eb]305 /*
306 * Create the first thread.
307 */
[6eef3c4]308 thread_t *kinit_thread = thread_create(kinit, NULL, kernel,
309 THREAD_FLAG_UNCOUNTED, "kinit");
[b84aaba]310 if (!kinit_thread)
[f651e80]311 panic("Cannot create kinit thread.");
[263bda2]312 thread_ready(kinit_thread);
[6f8a426]313
[f761f1eb]314 /*
315 * This call to scheduler() will return to kinit,
316 * starting the thread of kernel threads.
317 */
318 scheduler();
319 /* not reached */
320}
321
[5f85c91]322#ifdef CONFIG_SMP
[263bda2]323
[7f0837c]324/** Main kernel routine for application CPUs.
[673104e]325 *
326 * Executed by application processors, temporary stack
[26678e5]327 * is at ctx.sp which was set during BSP boot.
[5fe5f1e]328 * This function passes control directly to
329 * main_ap_separated_stack().
[673104e]330 *
[22f7769]331 * Assuming interrupts_disable()'d.
[673104e]332 *
[f761f1eb]333 */
334void main_ap(void)
335{
336 /*
337 * Incrementing the active CPU counter will guarantee that the
[26678e5]338 * *_init() functions can find out that they need to
339 * do initialization for AP only.
[f761f1eb]340 */
341 config.cpu_active++;
[263bda2]342
[7ce9284]343 /*
344 * The THE structure is well defined because ctx.sp is used as stack.
345 */
346 the_initialize(THE);
[c0b45fa]347
[36df4109]348 ARCH_OP(pre_mm_init);
[f761f1eb]349 frame_init();
350 page_init();
[ce031f0]351 tlb_init();
[36df4109]352 ARCH_OP(post_mm_init);
[c0b45fa]353
[f761f1eb]354 cpu_init();
355 calibrate_delay_loop();
[36df4109]356 ARCH_OP(post_cpu_init);
[263bda2]357
[7ce9284]358 the_copy(THE, (the_t *) CPU->stack);
[263bda2]359
[f761f1eb]360 /*
361 * If we woke kmp up before we left the kernel stack, we could
362 * collide with another CPU coming up. To prevent this, we
363 * switch to this cpu's private stack prior to waking kmp up.
364 */
[b24786a3]365 context_save(&CPU->saved_context);
[f8ddd17]366 context_set(&CPU->saved_context, FADDR(main_ap_separated_stack),
[26aafe8]367 (uintptr_t) CPU->stack, STACK_SIZE);
[be50915]368 context_restore(&CPU->saved_context);
[f761f1eb]369 /* not reached */
370}
371
[7f0837c]372/** Main kernel routine for application CPUs using new stack.
[673104e]373 *
374 * Second part of main_ap().
375 *
376 */
[f761f1eb]377void main_ap_separated_stack(void)
378{
[2ee1ccc]379 smp_call_init();
380
[f761f1eb]381 /*
382 * Configure timeouts for this cpu.
383 */
384 timeout_init();
[263bda2]385
[f761f1eb]386 waitq_wakeup(&ap_completion_wq, WAKEUP_FIRST);
387 scheduler();
388 /* not reached */
389}
[263bda2]390
[5f85c91]391#endif /* CONFIG_SMP */
[b45c443]392
[8e3bf3e2]393/** @}
[b45c443]394 */
Note: See TracBrowser for help on using the repository browser.