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
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 <log.h>
65#include <cpu.h>
66#include <align.h>
67#include <interrupt.h>
68#include <mm/frame.h>
69#include <mm/page.h>
70#include <genarch/mm/page_pt.h>
71#include <mm/km.h>
72#include <mm/tlb.h>
73#include <mm/as.h>
74#include <mm/slab.h>
75#include <mm/reserve.h>
76#include <synch/waitq.h>
77#include <synch/futex.h>
78#include <synch/workqueue.h>
79#include <smp/smp_call.h>
80#include <arch/arch.h>
81#include <arch.h>
82#include <arch/faddr.h>
83#include <ipc/ipc.h>
84#include <macros.h>
85#include <adt/btree.h>
86#include <smp/smp.h>
87#include <ddi/ddi.h>
88#include <main/main.h>
89#include <ipc/event.h>
90#include <sysinfo/sysinfo.h>
91#include <sysinfo/stats.h>
92#include <lib/ra.h>
93
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");
102
103#define CHECK_INT_TYPE(size) \
104 CHECK_INT_TYPE_(int, size); \
105 CHECK_INT_TYPE_(uint, size)
106
107CHECK_INT_TYPE(8);
108CHECK_INT_TYPE(16);
109CHECK_INT_TYPE(32);
110CHECK_INT_TYPE(64);
111
112/** Global configuration structure. */
113config_t config = {
114 .identity_configured = false,
115 .non_identity_configured = false,
116 .physmem_end = 0
117};
118
119/** Boot arguments. */
120char bargs[CONFIG_BOOT_ARGUMENTS_BUFLEN] = {};
121
122/** Initial user-space tasks */
123init_t init = {
124 .cnt = 0
125};
126
127/** Boot allocations. */
128ballocs_t ballocs = {
129 .base = (uintptr_t) NULL,
130 .size = 0
131};
132
133context_t ctx;
134
135/** Lowest safe stack virtual address. */
136uintptr_t stack_safe = 0;
137
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);
145
146#ifdef CONFIG_SMP
147static void main_ap_separated_stack(void);
148#endif
149
150/** Main kernel routine for bootstrap CPU.
151 *
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.
157 *
158 * Assuming interrupts_disable().
159 *
160 */
161NO_TRACE void main_bsp(void)
162{
163 config.cpu_count = 1;
164 config.cpu_active = 1;
165
166 config.base = hardcoded_load_address;
167 config.kernel_size = ALIGN_UP(hardcoded_ktext_size +
168 hardcoded_kdata_size, PAGE_SIZE);
169 config.stack_size = STACK_SIZE;
170
171 /* Initialy the stack is placed just after the kernel */
172 config.stack_base = config.base + config.kernel_size;
173
174 /* Avoid placing stack on top of init */
175 size_t i;
176 for (i = 0; i < init.cnt; i++) {
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 }
188 }
189
190 /* Avoid placing stack on top of boot allocations. */
191 if (ballocs.size) {
192 if (PA_OVERLAPS(config.stack_base, config.stack_size,
193 ballocs.base, ballocs.size))
194 config.stack_base = ALIGN_UP(ballocs.base +
195 ballocs.size, PAGE_SIZE);
196 }
197
198 if (config.stack_base < stack_safe)
199 config.stack_base = ALIGN_UP(stack_safe, PAGE_SIZE);
200
201 context_save(&ctx);
202 context_set(&ctx, FADDR(main_bsp_separated_stack),
203 config.stack_base, STACK_SIZE);
204 context_restore(&ctx);
205 /* not reached */
206}
207
208/** Main kernel routine for bootstrap CPU using new stack.
209 *
210 * Second part of main_bsp().
211 *
212 */
213void main_bsp_separated_stack(void)
214{
215 /* Keep this the first thing. */
216 the_initialize(THE);
217
218 version_print();
219
220 LOG("\nconfig.base=%p config.kernel_size=%zu"
221 "\nconfig.stack_base=%p config.stack_size=%zu",
222 (void *) config.base, config.kernel_size,
223 (void *) config.stack_base, config.stack_size);
224
225#ifdef CONFIG_KCONSOLE
226 /*
227 * kconsole data structures must be initialized very early
228 * because other subsystems will register their respective
229 * commands.
230 */
231 kconsole_init();
232#endif
233
234 /*
235 * Exception handler initialization, before architecture
236 * starts adding its own handlers
237 */
238 exc_init();
239
240 /*
241 * Memory management subsystems initialization.
242 */
243 ARCH_OP(pre_mm_init);
244 km_identity_init();
245 frame_init();
246 slab_cache_init();
247 ra_init();
248 sysinfo_init();
249 btree_init();
250 as_init();
251 page_init();
252 tlb_init();
253 km_non_identity_init();
254 ddi_init();
255 ARCH_OP(post_mm_init);
256 reserve_init();
257 ARCH_OP(pre_smp_init);
258 smp_init();
259
260 /* Slab must be initialized after we know the number of processors. */
261 slab_enable_cpucache();
262
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);
268
269 cpu_init();
270 calibrate_delay_loop();
271 ARCH_OP(post_cpu_init);
272
273 smp_call_init();
274 workq_global_init();
275 clock_counter_init();
276 timeout_init();
277 scheduler_init();
278 task_init();
279 thread_init();
280 futex_init();
281
282 sysinfo_set_item_data("boot_args", NULL, bargs, str_size(bargs) + 1);
283
284 if (init.cnt > 0) {
285 size_t i;
286 for (i = 0; i < init.cnt; i++)
287 LOG("init[%zu].addr=%p, init[%zu].size=%zu",
288 i, (void *) init.tasks[i].paddr, i, init.tasks[i].size);
289 } else
290 printf("No init binaries found.\n");
291
292 ipc_init();
293 event_init();
294 kio_init();
295 log_init();
296 stats_init();
297
298 /*
299 * Create kernel task.
300 */
301 task_t *kernel = task_create(AS_KERNEL, "kernel");
302 if (!kernel)
303 panic("Cannot create kernel task.");
304
305 /*
306 * Create the first thread.
307 */
308 thread_t *kinit_thread = thread_create(kinit, NULL, kernel,
309 THREAD_FLAG_UNCOUNTED, "kinit");
310 if (!kinit_thread)
311 panic("Cannot create kinit thread.");
312 thread_ready(kinit_thread);
313
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
322#ifdef CONFIG_SMP
323
324/** Main kernel routine for application CPUs.
325 *
326 * Executed by application processors, temporary stack
327 * is at ctx.sp which was set during BSP boot.
328 * This function passes control directly to
329 * main_ap_separated_stack().
330 *
331 * Assuming interrupts_disable()'d.
332 *
333 */
334void main_ap(void)
335{
336 /*
337 * Incrementing the active CPU counter will guarantee that the
338 * *_init() functions can find out that they need to
339 * do initialization for AP only.
340 */
341 config.cpu_active++;
342
343 /*
344 * The THE structure is well defined because ctx.sp is used as stack.
345 */
346 the_initialize(THE);
347
348 ARCH_OP(pre_mm_init);
349 frame_init();
350 page_init();
351 tlb_init();
352 ARCH_OP(post_mm_init);
353
354 cpu_init();
355 calibrate_delay_loop();
356 ARCH_OP(post_cpu_init);
357
358 the_copy(THE, (the_t *) CPU->stack);
359
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 */
365 context_save(&CPU->saved_context);
366 context_set(&CPU->saved_context, FADDR(main_ap_separated_stack),
367 (uintptr_t) CPU->stack, STACK_SIZE);
368 context_restore(&CPU->saved_context);
369 /* not reached */
370}
371
372/** Main kernel routine for application CPUs using new stack.
373 *
374 * Second part of main_ap().
375 *
376 */
377void main_ap_separated_stack(void)
378{
379 smp_call_init();
380
381 /*
382 * Configure timeouts for this cpu.
383 */
384 timeout_init();
385
386 waitq_wakeup(&ap_completion_wq, WAKEUP_FIRST);
387 scheduler();
388 /* not reached */
389}
390
391#endif /* CONFIG_SMP */
392
393/** @}
394 */
Note: See TracBrowser for help on using the repository browser.