source: mainline/kernel/generic/src/main/main.c@ 32573ff

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

Merge from lp:~adam-hraska+lp/helenos/rcu/.

Only merge from the feature branch and resolve all conflicts.

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