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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a1b9f63 was 7c3fb9b, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix block comment formatting (ccheck).

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