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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a1ecb88 was 9e40355e, checked in by Martin Decky <martin@…>, 12 years ago

cstyle

  • Property mode set to 100644
File size: 9.5 KB
Line 
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
29/** @addtogroup main
30 * @{
31 */
32
33/**
34 * @file
35 * @brief Main initialization kernel function for all processors.
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
49#include <arch/asm.h>
50#include <context.h>
51#include <print.h>
52#include <panic.h>
53#include <debug.h>
54#include <config.h>
55#include <time/clock.h>
56#include <time/timeout.h>
57#include <proc/scheduler.h>
58#include <proc/thread.h>
59#include <proc/task.h>
60#include <main/kinit.h>
61#include <main/version.h>
62#include <console/kconsole.h>
63#include <console/console.h>
64#include <cpu.h>
65#include <align.h>
66#include <interrupt.h>
67#include <mm/frame.h>
68#include <mm/page.h>
69#include <genarch/mm/page_pt.h>
70#include <mm/km.h>
71#include <mm/tlb.h>
72#include <mm/as.h>
73#include <mm/slab.h>
74#include <mm/reserve.h>
75#include <synch/waitq.h>
76#include <synch/futex.h>
77#include <arch/arch.h>
78#include <arch.h>
79#include <arch/faddr.h>
80#include <ipc/ipc.h>
81#include <macros.h>
82#include <adt/btree.h>
83#include <smp/smp.h>
84#include <ddi/ddi.h>
85#include <main/main.h>
86#include <ipc/event.h>
87#include <sysinfo/sysinfo.h>
88#include <sysinfo/stats.h>
89#include <lib/ra.h>
90
91/* Ensure [u]int*_t types are of correct size.
92 *
93 * Probably, this is not the best place for such tests
94 * but this file is compiled on all architectures.
95 */
96#define CHECK_INT_TYPE_(signness, size) \
97 STATIC_ASSERT_VERBOSE(sizeof(signness##size##_t) * 8 == size, \
98 #signness #size "_t does not have " #size " bits");
99
100#define CHECK_INT_TYPE(size) \
101 CHECK_INT_TYPE_(int, size); \
102 CHECK_INT_TYPE_(uint, size)
103
104CHECK_INT_TYPE(8);
105CHECK_INT_TYPE(16);
106CHECK_INT_TYPE(32);
107CHECK_INT_TYPE(64);
108
109/** Global configuration structure. */
110config_t config = {
111 .identity_configured = false,
112 .non_identity_configured = false,
113 .physmem_end = 0
114};
115
116/** Initial user-space tasks */
117init_t init = {
118 .cnt = 0
119};
120
121/** Boot allocations. */
122ballocs_t ballocs = {
123 .base = (uintptr_t) NULL,
124 .size = 0
125};
126
127context_t ctx;
128
129/** Lowest safe stack virtual address. */
130uintptr_t stack_safe = 0;
131
132/*
133 * These two functions prevent stack from underflowing during the
134 * kernel boot phase when SP is set to the very top of the reserved
135 * space. The stack could get corrupted by a fooled compiler-generated
136 * pop sequence otherwise.
137 */
138static void main_bsp_separated_stack(void);
139
140#ifdef CONFIG_SMP
141static void main_ap_separated_stack(void);
142#endif
143
144/** Main kernel routine for bootstrap CPU.
145 *
146 * The code here still runs on the boot stack, which knows nothing about
147 * preemption counts. Because of that, this function cannot directly call
148 * functions that disable or enable preemption (e.g. spinlock_lock()). The
149 * primary task of this function is to calculate address of a new stack and
150 * switch to it.
151 *
152 * Assuming interrupts_disable().
153 *
154 */
155NO_TRACE void main_bsp(void)
156{
157 config.cpu_count = 1;
158 config.cpu_active = 1;
159
160 config.base = hardcoded_load_address;
161 config.kernel_size = ALIGN_UP(hardcoded_ktext_size +
162 hardcoded_kdata_size, PAGE_SIZE);
163 config.stack_size = STACK_SIZE;
164
165 /* Initialy the stack is placed just after the kernel */
166 config.stack_base = config.base + config.kernel_size;
167
168 /* Avoid placing stack on top of init */
169 size_t i;
170 for (i = 0; i < init.cnt; i++) {
171 if (overlaps(KA2PA(config.stack_base), config.stack_size,
172 init.tasks[i].paddr, init.tasks[i].size)) {
173 /*
174 * The init task overlaps with the memory behind the
175 * kernel image so it must be in low memory and we can
176 * use PA2KA on the init task's physical address.
177 */
178 config.stack_base = ALIGN_UP(
179 PA2KA(init.tasks[i].paddr) + init.tasks[i].size,
180 config.stack_size);
181 }
182 }
183
184 /* Avoid placing stack on top of boot allocations. */
185 if (ballocs.size) {
186 if (PA_OVERLAPS(config.stack_base, config.stack_size,
187 ballocs.base, ballocs.size))
188 config.stack_base = ALIGN_UP(ballocs.base +
189 ballocs.size, PAGE_SIZE);
190 }
191
192 if (config.stack_base < stack_safe)
193 config.stack_base = ALIGN_UP(stack_safe, PAGE_SIZE);
194
195 context_save(&ctx);
196 context_set(&ctx, FADDR(main_bsp_separated_stack),
197 config.stack_base, STACK_SIZE);
198 context_restore(&ctx);
199 /* not reached */
200}
201
202/** Main kernel routine for bootstrap CPU using new stack.
203 *
204 * Second part of main_bsp().
205 *
206 */
207void main_bsp_separated_stack(void)
208{
209 /* Keep this the first thing. */
210 the_initialize(THE);
211
212 version_print();
213
214 LOG("\nconfig.base=%p config.kernel_size=%zu"
215 "\nconfig.stack_base=%p config.stack_size=%zu",
216 (void *) config.base, config.kernel_size,
217 (void *) config.stack_base, config.stack_size);
218
219#ifdef CONFIG_KCONSOLE
220 /*
221 * kconsole data structures must be initialized very early
222 * because other subsystems will register their respective
223 * commands.
224 */
225 kconsole_init();
226#endif
227
228 /*
229 * Exception handler initialization, before architecture
230 * starts adding its own handlers
231 */
232 exc_init();
233
234 /*
235 * Memory management subsystems initialization.
236 */
237 arch_pre_mm_init();
238 km_identity_init();
239 frame_init();
240 slab_cache_init();
241 ra_init();
242 sysinfo_init();
243 btree_init();
244 as_init();
245 page_init();
246 tlb_init();
247 km_non_identity_init();
248 ddi_init();
249 arch_post_mm_init();
250 reserve_init();
251 arch_pre_smp_init();
252 smp_init();
253
254 /* Slab must be initialized after we know the number of processors. */
255 slab_enable_cpucache();
256
257 uint64_t size;
258 const char *size_suffix;
259 bin_order_suffix(zones_total_size(), &size, &size_suffix, false);
260 printf("Detected %u CPU(s), %" PRIu64 " %s free memory\n",
261 config.cpu_count, size, size_suffix);
262
263 cpu_init();
264
265 calibrate_delay_loop();
266 clock_counter_init();
267 timeout_init();
268 scheduler_init();
269 task_init();
270 thread_init();
271 futex_init();
272
273 if (init.cnt > 0) {
274 size_t i;
275 for (i = 0; i < init.cnt; i++)
276 LOG("init[%zu].addr=%p, init[%zu].size=%zu",
277 i, (void *) init.tasks[i].paddr, i, init.tasks[i].size);
278 } else
279 printf("No init binaries found.\n");
280
281 ipc_init();
282 event_init();
283 klog_init();
284 stats_init();
285
286 /*
287 * Create kernel task.
288 */
289 task_t *kernel = task_create(AS_KERNEL, "kernel");
290 if (!kernel)
291 panic("Cannot create kernel task.");
292
293 /*
294 * Create the first thread.
295 */
296 thread_t *kinit_thread = thread_create(kinit, NULL, kernel,
297 THREAD_FLAG_UNCOUNTED, "kinit");
298 if (!kinit_thread)
299 panic("Cannot create kinit thread.");
300 thread_ready(kinit_thread);
301
302 /*
303 * This call to scheduler() will return to kinit,
304 * starting the thread of kernel threads.
305 */
306 scheduler();
307 /* not reached */
308}
309
310#ifdef CONFIG_SMP
311
312/** Main kernel routine for application CPUs.
313 *
314 * Executed by application processors, temporary stack
315 * is at ctx.sp which was set during BSP boot.
316 * This function passes control directly to
317 * main_ap_separated_stack().
318 *
319 * Assuming interrupts_disable()'d.
320 *
321 */
322void main_ap(void)
323{
324 /*
325 * Incrementing the active CPU counter will guarantee that the
326 * *_init() functions can find out that they need to
327 * do initialization for AP only.
328 */
329 config.cpu_active++;
330
331 /*
332 * The THE structure is well defined because ctx.sp is used as stack.
333 */
334 the_initialize(THE);
335
336 arch_pre_mm_init();
337 frame_init();
338 page_init();
339 tlb_init();
340 arch_post_mm_init();
341
342 cpu_init();
343 calibrate_delay_loop();
344 arch_post_cpu_init();
345
346 the_copy(THE, (the_t *) CPU->stack);
347
348 /*
349 * If we woke kmp up before we left the kernel stack, we could
350 * collide with another CPU coming up. To prevent this, we
351 * switch to this cpu's private stack prior to waking kmp up.
352 */
353 context_save(&CPU->saved_context);
354 context_set(&CPU->saved_context, FADDR(main_ap_separated_stack),
355 (uintptr_t) CPU->stack, STACK_SIZE);
356 context_restore(&CPU->saved_context);
357 /* not reached */
358}
359
360/** Main kernel routine for application CPUs using new stack.
361 *
362 * Second part of main_ap().
363 *
364 */
365void main_ap_separated_stack(void)
366{
367 /*
368 * Configure timeouts for this cpu.
369 */
370 timeout_init();
371
372 waitq_wakeup(&ap_completion_wq, WAKEUP_FIRST);
373 scheduler();
374 /* not reached */
375}
376
377#endif /* CONFIG_SMP */
378
379/** @}
380 */
Note: See TracBrowser for help on using the repository browser.