source: mainline/generic/src/main/main.c@ 8b08cf3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8b08cf3 was d6e5cbc, checked in by Ondrej Palkovsky <ondrap@…>, 19 years ago

Added 'realtime' clock interface.
Added some asm macros as memory barriers.
Added drift computing for mips platform.

  • Property mode set to 100644
File size: 8.1 KB
RevLine 
[f761f1eb]1/*
2 * Copyright (C) 2001-2004 Jakub Jermar
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
[9179d0a]29/**
30 * @file main.c
31 * @brief Main initialization kernel function for all processors.
32 *
33 * During kernel boot, all processors, after architecture dependent
34 * initialization, start executing code found in this file. After
35 * bringing up all subsystems, control is passed to scheduler().
36 *
37 * The bootstrap processor starts executing main_bsp() while
38 * the application processors start executing main_ap().
39 *
40 * @see scheduler()
41 * @see main_bsp()
42 * @see main_ap()
43 */
44
[f761f1eb]45#include <arch/asm.h>
[d5d2a3f]46#include <context.h>
[f761f1eb]47#include <print.h>
[02a99d2]48#include <panic.h>
[940cac0]49#include <debug.h>
[f761f1eb]50#include <config.h>
51#include <time/clock.h>
52#include <proc/scheduler.h>
53#include <proc/thread.h>
54#include <proc/task.h>
55#include <main/kinit.h>
[5fe5f1e]56#include <main/version.h>
[f4338d2]57#include <console/kconsole.h>
[f761f1eb]58#include <cpu.h>
[e0cdb7b6]59#include <align.h>
[aace6624]60#include <interrupt.h>
[a55f97f]61#include <arch/mm/memory_init.h>
[f761f1eb]62#include <mm/frame.h>
63#include <mm/page.h>
[6d7ffa65]64#include <genarch/mm/page_pt.h>
[169587a]65#include <mm/tlb.h>
[20d50a1]66#include <mm/as.h>
[4e147a6]67#include <mm/slab.h>
[f761f1eb]68#include <synch/waitq.h>
[9aa72b4]69#include <synch/futex.h>
[393f631]70#include <arch/arch.h>
[f761f1eb]71#include <arch.h>
[f2ffad4]72#include <arch/faddr.h>
[ac5d02b]73#include <typedefs.h>
[6d9c49a]74#include <ipc/ipc.h>
[93165be]75#include <macros.h>
[2810636]76#include <adt/btree.h>
[ac5d02b]77
[5fe5f1e]78#ifdef CONFIG_SMP
79#include <arch/smp/apic.h>
80#include <arch/smp/mps.h>
81#endif /* CONFIG_SMP */
82#include <smp/smp.h>
83
[e459f12]84/** Global configuration structure. */
85config_t config = {
86 .mm_initialized = false
87};
88
89/** Initial user-space tasks */
90init_t init = {
91 0
92};
[5fe5f1e]93
[f761f1eb]94context_t ctx;
95
[880de6e]96/**
97 * These 'hardcoded' variables will be intialized by
[dcbc8be]98 * the linker or the low level assembler code with
99 * appropriate sizes and addresses.
[f761f1eb]100 */
[2217ac3]101__address hardcoded_load_address = 0;
[ac5d02b]102size_t hardcoded_ktext_size = 0;
103size_t hardcoded_kdata_size = 0;
[f761f1eb]104
105void main_bsp(void);
106void main_ap(void);
107
108/*
109 * These two functions prevent stack from underflowing during the
110 * kernel boot phase when SP is set to the very top of the reserved
111 * space. The stack could get corrupted by a fooled compiler-generated
112 * pop sequence otherwise.
113 */
114static void main_bsp_separated_stack(void);
[80d2bdb]115#ifdef CONFIG_SMP
[f761f1eb]116static void main_ap_separated_stack(void);
[80d2bdb]117#endif
[f761f1eb]118
[b6d4566]119#define CONFIG_STACK_SIZE ((1<<STACK_FRAMES)*STACK_SIZE)
120
[7f0837c]121/** Main kernel routine for bootstrap CPU.
[673104e]122 *
123 * Initializes the kernel by bootstrap CPU.
[5fe5f1e]124 * This function passes control directly to
125 * main_bsp_separated_stack().
[673104e]126 *
[22f7769]127 * Assuming interrupts_disable().
[673104e]128 *
[f761f1eb]129 */
130void main_bsp(void)
131{
[085d973]132 __address stackaddr;
133
[f761f1eb]134 config.cpu_count = 1;
135 config.cpu_active = 1;
[d6e8529]136
[aed4eca]137 config.base = hardcoded_load_address;
138 config.memory_size = get_memory_size();
[393f631]139
[085d973]140 config.kernel_size = ALIGN_UP(hardcoded_ktext_size + hardcoded_kdata_size, PAGE_SIZE);
141 stackaddr = config.base + config.kernel_size;
[b6b576c]142
[085d973]143 /* Avoid placing kernel on top of init */
[b6b576c]144 count_t i;
145 bool overlap = false;
146 for (i = 0; i < init.cnt; i++)
[93165be]147 if (PA_overlaps(stackaddr, CONFIG_STACK_SIZE, init.tasks[i].addr, init.tasks[i].size)) {
[b6b576c]148 stackaddr = ALIGN_UP(init.tasks[i].addr + init.tasks[i].size, CONFIG_STACK_SIZE);
149 init.tasks[i].size = ALIGN_UP(init.tasks[i].size, CONFIG_STACK_SIZE) + CONFIG_STACK_SIZE;
150 overlap = true;
151 }
152
153 if (!overlap)
[085d973]154 config.kernel_size += CONFIG_STACK_SIZE;
[393f631]155
[be50915]156 context_save(&ctx);
[b6d4566]157 context_set(&ctx, FADDR(main_bsp_separated_stack), stackaddr, THREAD_STACK_SIZE);
[be50915]158 context_restore(&ctx);
[f761f1eb]159 /* not reached */
160}
161
[673104e]162
[7f0837c]163/** Main kernel routine for bootstrap CPU using new stack.
[673104e]164 *
165 * Second part of main_bsp().
166 *
167 */
[fde6429]168void main_bsp_separated_stack(void)
169{
[f761f1eb]170 task_t *k;
171 thread_t *t;
[9aa72b4]172 count_t i;
[8f91729]173
[bcdd9aa]174 the_initialize(THE);
[9aa72b4]175
[ff3b3197]176 /*
177 * kconsole data structures must be initialized very early
178 * because other subsystems will register their respective
179 * commands.
180 */
181 kconsole_init();
[b6b576c]182
[ef67bab]183 /*
184 * Exception handler initialization, before architecture
[0132630]185 * starts adding its own handlers
[aace6624]186 */
187 exc_init();
[fc1e4f6]188
189 /*
190 * Memory management subsystems initialization.
191 */
[1fbbcd6]192 arch_pre_mm_init();
[018d957e]193 frame_init(); /* Initialize at least 1 memory segment big enough for slab to work */
[8e1ea655]194 slab_cache_init();
[2810636]195 btree_init();
[ef67bab]196 as_init();
[7eade45]197 page_init();
198 tlb_init();
[e459f12]199 config.mm_initialized = true;
[d6e5cbc]200 arch_post_mm_init();
[0132630]201
[018d957e]202 version_print();
[dadb68e]203 printf("%.*p: hardcoded_ktext_size=%zdK, hardcoded_kdata_size=%zdK\n", sizeof(__address) * 2, config.base, hardcoded_ktext_size >> 10, hardcoded_kdata_size >> 10);
[f761f1eb]204
[7453929]205 arch_pre_smp_init();
[ed0dd65]206 smp_init();
[018d957e]207
208 slab_enable_cpucache(); /* Slab must be initialized AFTER we know the number of processors */
[c5613b72]209
[dadb68e]210 printf("config.memory_size=%zdM\n", config.memory_size >> 20);
[280a27e]211 printf("config.cpu_count=%zd\n", config.cpu_count);
[f761f1eb]212 cpu_init();
[6f8a426]213
[f761f1eb]214 calibrate_delay_loop();
[d6e5cbc]215 clock_counter_init();
[f761f1eb]216 timeout_init();
217 scheduler_init();
218 task_init();
219 thread_init();
[9aa72b4]220 futex_init();
[6c68b97]221
[b6b576c]222 for (i = 0; i < init.cnt; i++)
[dadb68e]223 printf("init[%zd].addr=%.*p, init[%zd].size=%zd\n", i, sizeof(__address) * 2, init.tasks[i].addr, i, init.tasks[i].size);
[6d9c49a]224
225 ipc_init();
[9aa72b4]226
[f761f1eb]227 /*
228 * Create kernel task.
229 */
[ff14c520]230 k = task_create(AS_KERNEL, "KERNEL");
[393f631]231 if (!k)
232 panic("can't create kernel task\n");
[6f8a426]233
[f761f1eb]234 /*
235 * Create the first thread.
236 */
[ff14c520]237 t = thread_create(kinit, NULL, k, 0, "kinit");
[393f631]238 if (!t)
239 panic("can't create kinit thread\n");
[f761f1eb]240 thread_ready(t);
[6f8a426]241
[f761f1eb]242 /*
243 * This call to scheduler() will return to kinit,
244 * starting the thread of kernel threads.
245 */
246 scheduler();
247 /* not reached */
248}
249
[673104e]250
[5f85c91]251#ifdef CONFIG_SMP
[7f0837c]252/** Main kernel routine for application CPUs.
[673104e]253 *
254 * Executed by application processors, temporary stack
255 * is at ctx.sp which was set during BP boot.
[5fe5f1e]256 * This function passes control directly to
257 * main_ap_separated_stack().
[673104e]258 *
[22f7769]259 * Assuming interrupts_disable()'d.
[673104e]260 *
[f761f1eb]261 */
262void main_ap(void)
263{
264 /*
265 * Incrementing the active CPU counter will guarantee that the
266 * pm_init() will not attempt to build GDT and IDT tables again.
267 * Neither frame_init() will do the complete thing. Neither cpu_init()
268 * will do.
269 */
270 config.cpu_active++;
271
[7ce9284]272 /*
273 * The THE structure is well defined because ctx.sp is used as stack.
274 */
275 the_initialize(THE);
[c0b45fa]276
[f07bba5]277 arch_pre_mm_init();
[f761f1eb]278 frame_init();
279 page_init();
[ce031f0]280 tlb_init();
[f07bba5]281 arch_post_mm_init();
[c0b45fa]282
[f761f1eb]283 cpu_init();
[c0b45fa]284
[f761f1eb]285 calibrate_delay_loop();
286
287 l_apic_init();
288 l_apic_debug();
289
[7ce9284]290 the_copy(THE, (the_t *) CPU->stack);
[f761f1eb]291
292 /*
293 * If we woke kmp up before we left the kernel stack, we could
294 * collide with another CPU coming up. To prevent this, we
295 * switch to this cpu's private stack prior to waking kmp up.
296 */
[4b2c872d]297 context_set(&CPU->saved_context, FADDR(main_ap_separated_stack), (__address) CPU->stack, CPU_STACK_SIZE);
[be50915]298 context_restore(&CPU->saved_context);
[f761f1eb]299 /* not reached */
300}
301
[673104e]302
[7f0837c]303/** Main kernel routine for application CPUs using new stack.
[673104e]304 *
305 * Second part of main_ap().
306 *
307 */
[f761f1eb]308void main_ap_separated_stack(void)
309{
310 /*
311 * Configure timeouts for this cpu.
312 */
313 timeout_init();
314
315 waitq_wakeup(&ap_completion_wq, WAKEUP_FIRST);
316 scheduler();
317 /* not reached */
318}
[5f85c91]319#endif /* CONFIG_SMP */
Note: See TracBrowser for help on using the repository browser.